1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018 Lothar Felten, lothar.felten@gmail.com
12 static ulong wol_timeout = WOL_DEFAULT_TIMEOUT;
15 * Check incoming Wake-on-LAN packet for:
17 * - sixteen copies of the target MAC address
19 * @param wol Wake-on-LAN packet
20 * @param len Packet length
22 static int wol_check_magic(struct wol_hdr *wol, unsigned int len)
26 if (len < sizeof(struct wol_hdr))
29 for (i = 0; i < WOL_SYNC_COUNT; i++)
30 if (wol->wol_sync[i] != WOL_SYNC_BYTE)
33 for (i = 0; i < WOL_MAC_REPETITIONS; i++)
34 if (memcmp(&wol->wol_dest[i * ARP_HLEN],
35 net_ethaddr, ARP_HLEN) != 0)
41 void wol_receive(struct ip_udp_hdr *ip, unsigned int len)
45 wol = (struct wol_hdr *)ip;
47 if (!wol_check_magic(wol, len))
50 /* save the optional password using the ether-wake formats */
51 /* don't check for exact length, the packet might have padding */
52 if (len >= (sizeof(struct wol_hdr) + WOL_PASSWORD_6B)) {
53 eth_env_set_enetaddr("wolpassword", wol->wol_passwd);
54 } else if (len >= (sizeof(struct wol_hdr) + WOL_PASSWORD_4B)) {
56 struct in_addr *ip = (struct in_addr *)(wol->wol_passwd);
58 ip_to_string(*ip, buffer);
59 env_set("wolpassword", buffer);
61 net_set_state(NETLOOP_SUCCESS);
64 static void wol_udp_handler(uchar *pkt, unsigned int dest, struct in_addr sip,
65 unsigned int src, unsigned int len)
69 wol = (struct wol_hdr *)pkt;
71 /* UDP destination port must be 0, 7 or 9 */
72 if (dest != 0 && dest != 7 && dest != 9)
75 if (!wol_check_magic(wol, len))
78 net_set_state(NETLOOP_SUCCESS);
81 void wol_set_timeout(ulong timeout)
83 wol_timeout = timeout;
86 static void wol_timeout_handler(void)
89 net_set_state(NETLOOP_FAIL);
94 net_set_timeout_handler(wol_timeout, wol_timeout_handler);
95 net_set_udp_handler(wol_udp_handler);