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