Lothar Felten | 776fc10 | 2018-06-22 22:29:54 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2018 Lothar Felten, lothar.felten@gmail.com |
| 4 | */ |
| 5 | |
Lothar Felten | 776fc10 | 2018-06-22 22:29:54 +0200 | [diff] [blame] | 6 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 7 | #include <env.h> |
Lothar Felten | 776fc10 | 2018-06-22 22:29:54 +0200 | [diff] [blame] | 8 | #include <net.h> |
Lothar Felten | 776fc10 | 2018-06-22 22:29:54 +0200 | [diff] [blame] | 9 | #include "wol.h" |
| 10 | |
| 11 | static ulong wol_timeout = WOL_DEFAULT_TIMEOUT; |
| 12 | |
| 13 | /* |
| 14 | * Check incoming Wake-on-LAN packet for: |
| 15 | * - sync bytes |
| 16 | * - sixteen copies of the target MAC address |
| 17 | * |
| 18 | * @param wol Wake-on-LAN packet |
| 19 | * @param len Packet length |
| 20 | */ |
| 21 | static int wol_check_magic(struct wol_hdr *wol, unsigned int len) |
| 22 | { |
| 23 | int i; |
| 24 | |
| 25 | if (len < sizeof(struct wol_hdr)) |
| 26 | return 0; |
| 27 | |
| 28 | for (i = 0; i < WOL_SYNC_COUNT; i++) |
| 29 | if (wol->wol_sync[i] != WOL_SYNC_BYTE) |
| 30 | return 0; |
| 31 | |
| 32 | for (i = 0; i < WOL_MAC_REPETITIONS; i++) |
| 33 | if (memcmp(&wol->wol_dest[i * ARP_HLEN], |
| 34 | net_ethaddr, ARP_HLEN) != 0) |
| 35 | return 0; |
| 36 | |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | void wol_receive(struct ip_udp_hdr *ip, unsigned int len) |
| 41 | { |
| 42 | struct wol_hdr *wol; |
| 43 | |
| 44 | wol = (struct wol_hdr *)ip; |
| 45 | |
| 46 | if (!wol_check_magic(wol, len)) |
| 47 | return; |
| 48 | |
| 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)) { |
| 54 | char buffer[16]; |
| 55 | struct in_addr *ip = (struct in_addr *)(wol->wol_passwd); |
| 56 | |
| 57 | ip_to_string(*ip, buffer); |
| 58 | env_set("wolpassword", buffer); |
| 59 | } |
| 60 | net_set_state(NETLOOP_SUCCESS); |
| 61 | } |
| 62 | |
| 63 | static void wol_udp_handler(uchar *pkt, unsigned int dest, struct in_addr sip, |
| 64 | unsigned int src, unsigned int len) |
| 65 | { |
| 66 | struct wol_hdr *wol; |
| 67 | |
| 68 | wol = (struct wol_hdr *)pkt; |
| 69 | |
| 70 | /* UDP destination port must be 0, 7 or 9 */ |
| 71 | if (dest != 0 && dest != 7 && dest != 9) |
| 72 | return; |
| 73 | |
| 74 | if (!wol_check_magic(wol, len)) |
| 75 | return; |
| 76 | |
| 77 | net_set_state(NETLOOP_SUCCESS); |
| 78 | } |
| 79 | |
| 80 | void wol_set_timeout(ulong timeout) |
| 81 | { |
| 82 | wol_timeout = timeout; |
| 83 | } |
| 84 | |
| 85 | static void wol_timeout_handler(void) |
| 86 | { |
| 87 | eth_halt(); |
| 88 | net_set_state(NETLOOP_FAIL); |
| 89 | } |
| 90 | |
| 91 | void wol_start(void) |
| 92 | { |
| 93 | net_set_timeout_handler(wol_timeout, wol_timeout_handler); |
| 94 | net_set_udp_handler(wol_udp_handler); |
| 95 | } |