1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87#include "libbb.h"
88#include <netpacket/packet.h>
89#include <netinet/ether.h>
90#include <linux/if.h>
91
92
93
94
95
96#ifdef PF_PACKET
97# define whereto_t sockaddr_ll
98# define make_socket() xsocket(PF_PACKET, SOCK_RAW, 0)
99#else
100# define whereto_t sockaddr
101# define make_socket() xsocket(AF_INET, SOCK_PACKET, SOCK_PACKET)
102#endif
103
104#ifdef DEBUG
105# define bb_debug_msg(fmt, args...) fprintf(stderr, fmt, ## args)
106void bb_debug_dump_packet(unsigned char *outpack, int pktsize)
107{
108 int i;
109 printf("packet dump:\n");
110 for (i = 0; i < pktsize; ++i) {
111 printf("%2.2x ", outpack[i]);
112 if (i % 20 == 19) bb_putchar('\n');
113 }
114 printf("\n\n");
115}
116#else
117# define bb_debug_msg(fmt, args...) ((void)0)
118# define bb_debug_dump_packet(outpack, pktsize) ((void)0)
119#endif
120
121
122
123
124
125
126
127static void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
128{
129 struct ether_addr *eap;
130
131 eap = ether_aton_r(hostid, eaddr);
132 if (eap) {
133 bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap));
134#if !defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
135 } else if (ether_hostton(hostid, eaddr) == 0) {
136 bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
137#endif
138 } else {
139 bb_show_usage();
140 }
141}
142
143#define PKT_HEADER_SIZE (20 + 16*6)
144static int fill_pkt_header(unsigned char *pkt, struct ether_addr *eaddr, int broadcast)
145{
146 int i;
147 unsigned char *station_addr = eaddr->ether_addr_octet;
148
149 memset(pkt, 0xff, 6);
150 if (!broadcast)
151 memcpy(pkt, station_addr, 6);
152 pkt += 6;
153
154 memcpy(pkt, station_addr, 6);
155 pkt += 6;
156
157 *pkt++ = 0x08;
158 *pkt++ = 0x42;
159
160 memset(pkt, 0xff, 6);
161
162 for (i = 0; i < 16; ++i) {
163 pkt += 6;
164 memcpy(pkt, station_addr, 6);
165 }
166
167 return PKT_HEADER_SIZE;
168}
169
170static int get_wol_pw(const char *ethoptarg, unsigned char *wol_passwd)
171{
172 unsigned passwd[6];
173 int byte_cnt, i;
174
175
176 byte_cnt = sscanf(ethoptarg, "%2x:%2x:%2x:%2x:%2x:%2x",
177 &passwd[0], &passwd[1], &passwd[2],
178 &passwd[3], &passwd[4], &passwd[5]);
179
180
181 if (byte_cnt < 4)
182 byte_cnt = sscanf(ethoptarg, "%u.%u.%u.%u",
183 &passwd[0], &passwd[1], &passwd[2], &passwd[3]);
184 if (byte_cnt < 4) {
185 bb_error_msg("can't read Wake-On-LAN pass");
186 return 0;
187 }
188
189 for (i = 0; i < byte_cnt; ++i)
190 wol_passwd[i] = passwd[i];
191
192 bb_debug_msg("password: %2.2x %2.2x %2.2x %2.2x (%d)\n\n",
193 wol_passwd[0], wol_passwd[1], wol_passwd[2], wol_passwd[3],
194 byte_cnt);
195
196 return byte_cnt;
197}
198
199int ether_wake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
200int ether_wake_main(int argc UNUSED_PARAM, char **argv)
201{
202 const char *ifname = "eth0";
203 char *pass;
204 unsigned flags;
205 unsigned char wol_passwd[6];
206 int wol_passwd_sz = 0;
207 int s;
208 int pktsize;
209 unsigned char outpack[PKT_HEADER_SIZE + 6 + 16 ];
210
211 struct ether_addr eaddr;
212 struct whereto_t whereto;
213
214
215 flags = getopt32(argv, "^" "bi:p:" "\0" "=1", &ifname, &pass);
216 if (flags & 4)
217 wol_passwd_sz = get_wol_pw(pass, wol_passwd);
218 flags &= 1;
219
220
221 s = make_socket();
222
223
224
225
226
227 get_dest_addr(argv[optind], &eaddr);
228
229
230 pktsize = fill_pkt_header(outpack, &eaddr, flags );
231
232 bb_debug_dump_packet(outpack, pktsize);
233
234
235#ifdef __linux__
236 {
237 struct ifreq if_hwaddr;
238
239 strncpy_IFNAMSIZ(if_hwaddr.ifr_name, ifname);
240 ioctl_or_perror_and_die(s, SIOCGIFHWADDR, &if_hwaddr, "SIOCGIFHWADDR on %s failed", ifname);
241
242 memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
243
244# ifdef DEBUG
245 {
246 unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
247 printf("The hardware address (SIOCGIFHWADDR) of %s is type %d "
248 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n\n", ifname,
249 if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
250 hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
251 }
252# endif
253 }
254#endif
255
256 bb_debug_dump_packet(outpack, pktsize);
257
258
259 if (wol_passwd_sz > 0) {
260 memcpy(outpack+pktsize, wol_passwd, wol_passwd_sz);
261 pktsize += wol_passwd_sz;
262 }
263
264 bb_debug_dump_packet(outpack, pktsize);
265
266
267 if (flags ) {
268 if (setsockopt_broadcast(s) != 0)
269 bb_perror_msg("SO_BROADCAST");
270 }
271
272#if defined(PF_PACKET)
273 {
274 struct ifreq ifr;
275 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
276 xioctl(s, SIOCGIFINDEX, &ifr);
277 memset(&whereto, 0, sizeof(whereto));
278 whereto.sll_family = AF_PACKET;
279 whereto.sll_ifindex = ifr.ifr_ifindex;
280
281
282 whereto.sll_halen = ETH_ALEN;
283 memcpy(whereto.sll_addr, outpack, ETH_ALEN);
284 }
285#else
286 whereto.sa_family = 0;
287 strcpy(whereto.sa_data, ifname);
288#endif
289 xsendto(s, outpack, pktsize, (struct sockaddr *)&whereto, sizeof(whereto));
290 if (ENABLE_FEATURE_CLEAN_UP)
291 close(s);
292 return EXIT_SUCCESS;
293}
294