1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018 Lothar Felten, lothar.felten@gmail.com
10 #include <environment.h>
13 static ulong wol_timeout = WOL_DEFAULT_TIMEOUT;
16 * Check incoming Wake-on-LAN packet for:
18 * - sixteen copies of the target MAC address
20 * @param wol Wake-on-LAN packet
21 * @param len Packet length
23 static int wol_check_magic(struct wol_hdr *wol, unsigned int len)
27 if (len < sizeof(struct wol_hdr))
30 for (i = 0; i < WOL_SYNC_COUNT; i++)
31 if (wol->wol_sync[i] != WOL_SYNC_BYTE)
34 for (i = 0; i < WOL_MAC_REPETITIONS; i++)
35 if (memcmp(&wol->wol_dest[i * ARP_HLEN],
36 net_ethaddr, ARP_HLEN) != 0)
42 void wol_receive(struct ip_udp_hdr *ip, unsigned int len)
46 wol = (struct wol_hdr *)ip;
48 if (!wol_check_magic(wol, len))
51 /* save the optional password using the ether-wake formats */
52 /* don't check for exact length, the packet might have padding */
53 if (len >= (sizeof(struct wol_hdr) + WOL_PASSWORD_6B)) {
54 eth_env_set_enetaddr("wolpassword", wol->wol_passwd);
55 } else if (len >= (sizeof(struct wol_hdr) + WOL_PASSWORD_4B)) {
57 struct in_addr *ip = (struct in_addr *)(wol->wol_passwd);
59 ip_to_string(*ip, buffer);
60 env_set("wolpassword", buffer);
62 net_set_state(NETLOOP_SUCCESS);
65 static void wol_udp_handler(uchar *pkt, unsigned int dest, struct in_addr sip,
66 unsigned int src, unsigned int len)
70 wol = (struct wol_hdr *)pkt;
72 /* UDP destination port must be 0, 7 or 9 */
73 if (dest != 0 && dest != 7 && dest != 9)
76 if (!wol_check_magic(wol, len))
79 net_set_state(NETLOOP_SUCCESS);
82 void wol_set_timeout(ulong timeout)
84 wol_timeout = timeout;
87 static void wol_timeout_handler(void)
90 net_set_state(NETLOOP_FAIL);
95 net_set_timeout_handler(wol_timeout, wol_timeout_handler);
96 net_set_udp_handler(wol_udp_handler);