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