1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018 Lothar Felten, lothar.felten@gmail.com
11 static ulong wol_timeout = WOL_DEFAULT_TIMEOUT;
14 * Check incoming Wake-on-LAN packet for:
16 * - sixteen copies of the target MAC address
18 * @param wol Wake-on-LAN packet
19 * @param len Packet length
21 static int wol_check_magic(struct wol_hdr *wol, unsigned int len)
25 if (len < sizeof(struct wol_hdr))
28 for (i = 0; i < WOL_SYNC_COUNT; i++)
29 if (wol->wol_sync[i] != WOL_SYNC_BYTE)
32 for (i = 0; i < WOL_MAC_REPETITIONS; i++)
33 if (memcmp(&wol->wol_dest[i * ARP_HLEN],
34 net_ethaddr, ARP_HLEN) != 0)
40 void wol_receive(struct ip_udp_hdr *ip, unsigned int len)
44 wol = (struct wol_hdr *)ip;
46 if (!wol_check_magic(wol, len))
49 /* save the optional password using the ether-wake formats */
50 /* don't check for exact length, the packet might have padding */
51 if (len >= (sizeof(struct wol_hdr) + WOL_PASSWORD_6B)) {
52 eth_env_set_enetaddr("wolpassword", wol->wol_passwd);
53 } else if (len >= (sizeof(struct wol_hdr) + WOL_PASSWORD_4B)) {
55 struct in_addr *ip = (struct in_addr *)(wol->wol_passwd);
57 ip_to_string(*ip, buffer);
58 env_set("wolpassword", buffer);
60 net_set_state(NETLOOP_SUCCESS);
63 static void wol_udp_handler(uchar *pkt, unsigned int dest, struct in_addr sip,
64 unsigned int src, unsigned int len)
68 wol = (struct wol_hdr *)pkt;
70 /* UDP destination port must be 0, 7 or 9 */
71 if (dest != 0 && dest != 7 && dest != 9)
74 if (!wol_check_magic(wol, len))
77 net_set_state(NETLOOP_SUCCESS);
80 void wol_set_timeout(ulong timeout)
82 wol_timeout = timeout;
85 static void wol_timeout_handler(void)
88 net_set_state(NETLOOP_FAIL);
93 net_set_timeout_handler(wol_timeout, wol_timeout_handler);
94 net_set_udp_handler(wol_udp_handler);