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#include "libbb.h"
59#include "common_bufsiz.h"
60#include <netinet/ether.h>
61#include <net/if.h>
62#include <net/if_arp.h>
63#include <linux/sockios.h>
64
65#include <syslog.h>
66
67
68#define MONOTONIC_US() ((unsigned)monotonic_us())
69
70struct arp_packet {
71 struct ether_header eth;
72 struct ether_arp arp;
73} PACKED;
74
75enum {
76
77 PROBE_WAIT = 1,
78
79 PROBE_MIN = 1,
80 PROBE_MAX = 2,
81 PROBE_NUM = 3,
82 ANNOUNCE_INTERVAL = 2,
83 ANNOUNCE_NUM = 3,
84
85 CONFLICT_MULTIPLIER = 2,
86
87 DEFEND_INTERVAL = 10,
88};
89
90
91enum {
92 PROBE = 0,
93 ANNOUNCE,
94 MONITOR,
95 DEFEND
96};
97
98#define VDBG(...) do { } while (0)
99
100
101enum {
102 sock_fd = 3
103};
104
105struct globals {
106 struct sockaddr iface_sockaddr;
107 struct ether_addr our_ethaddr;
108 uint32_t localnet_ip;
109} FIX_ALIASING;
110#define G (*(struct globals*)bb_common_bufsiz1)
111#define INIT_G() do { setup_common_bufsiz(); } while (0)
112
113
114
115
116
117
118static uint32_t pick_nip(void)
119{
120 unsigned tmp;
121
122 do {
123 tmp = rand() & IN_CLASSB_HOST;
124 } while (tmp > (IN_CLASSB_HOST - 0x0200));
125 return htonl((G.localnet_ip + 0x0100) + tmp);
126}
127
128static const char *nip_to_a(uint32_t nip)
129{
130 struct in_addr in;
131 in.s_addr = nip;
132 return inet_ntoa(in);
133}
134
135
136
137
138static void send_arp_request(
139
140
141 uint32_t source_nip,
142 const struct ether_addr *target_eth, uint32_t target_nip)
143{
144 enum { op = ARPOP_REQUEST };
145#define source_eth (&G.our_ethaddr)
146
147 struct arp_packet p;
148 memset(&p, 0, sizeof(p));
149
150
151 p.eth.ether_type = htons(ETHERTYPE_ARP);
152 memcpy(p.eth.ether_shost, source_eth, ETH_ALEN);
153 memset(p.eth.ether_dhost, 0xff, ETH_ALEN);
154
155
156 p.arp.arp_hrd = htons(ARPHRD_ETHER);
157 p.arp.arp_pro = htons(ETHERTYPE_IP);
158 p.arp.arp_hln = ETH_ALEN;
159 p.arp.arp_pln = 4;
160 p.arp.arp_op = htons(op);
161 memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN);
162 memcpy(&p.arp.arp_spa, &source_nip, 4);
163 memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
164 memcpy(&p.arp.arp_tpa, &target_nip, 4);
165
166
167
168
169
170
171
172
173 xsendto(sock_fd, &p, sizeof(p), &G.iface_sockaddr, sizeof(G.iface_sockaddr));
174#undef source_eth
175}
176
177
178
179
180
181static int run(char *argv[3], const char *param, uint32_t nip)
182{
183 int status;
184 const char *addr = addr;
185 const char *fmt = "%s %s %s" + 3;
186
187 argv[2] = (char*)param;
188
189 VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]);
190
191 if (nip != 0) {
192 addr = nip_to_a(nip);
193 xsetenv("ip", addr);
194 fmt -= 3;
195 }
196 bb_error_msg(fmt, argv[2], argv[0], addr);
197
198 status = spawn_and_wait(argv + 1);
199 if (status < 0) {
200 bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
201 return -errno;
202 }
203 if (status != 0)
204 bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
205 return status;
206}
207
208
209
210
211static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
212{
213 return (unsigned)rand() % (secs * 1000);
214}
215
216
217
218
219int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
220int zcip_main(int argc UNUSED_PARAM, char **argv)
221{
222 char *r_opt;
223 const char *l_opt = "169.254.0.0";
224 int state;
225 int nsent;
226 unsigned opts;
227
228
229 struct {
230 const struct ether_addr null_ethaddr;
231 struct ifreq ifr;
232 uint32_t chosen_nip;
233 int conflicts;
234 int timeout_ms;
235 int verbose;
236 } L;
237#define null_ethaddr (L.null_ethaddr)
238#define ifr (L.ifr )
239#define chosen_nip (L.chosen_nip )
240#define conflicts (L.conflicts )
241#define timeout_ms (L.timeout_ms )
242#define verbose (L.verbose )
243
244 memset(&L, 0, sizeof(L));
245 INIT_G();
246
247#define FOREGROUND (opts & 1)
248#define QUIT (opts & 2)
249
250
251 opt_complementary = "=2:vv:vf";
252 opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose);
253#if !BB_MMU
254
255 if (!FOREGROUND)
256 bb_daemonize_or_rexec(0 , argv);
257#endif
258
259
260
261 xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
262 if (!FOREGROUND) {
263
264 openlog(applet_name, 0, LOG_DAEMON);
265 logmode |= LOGMODE_SYSLOG;
266 }
267 bb_logenv_override();
268
269 {
270 struct in_addr net;
271 if (inet_aton(l_opt, &net) == 0
272 || (net.s_addr & htonl(IN_CLASSB_NET)) != net.s_addr
273 ) {
274 bb_error_msg_and_die("invalid network address");
275 }
276 G.localnet_ip = ntohl(net.s_addr);
277 }
278 if (opts & 4) {
279 struct in_addr ip;
280 if (inet_aton(r_opt, &ip) == 0
281 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != G.localnet_ip
282 ) {
283 bb_error_msg_and_die("invalid link address");
284 }
285 chosen_nip = ip.s_addr;
286 }
287 argv += optind - 1;
288
289
290
291 argv[0] = argv[1];
292 argv[1] = argv[2];
293
294#define argv_intf (argv[0])
295
296 xsetenv("interface", argv_intf);
297
298
299 if (run(argv, "init", 0))
300 return EXIT_FAILURE;
301
302
303
304
305
306 safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data));
307
308
309 xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr));
310
311
312
313 strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
314 xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
315 memcpy(&G.our_ethaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
316
317
318
319
320
321
322
323 {
324 uint32_t t;
325 move_from_unaligned32(t, ((char *)&G.our_ethaddr + 2));
326 srand(t);
327 }
328
329
330
331
332
333 if (!FOREGROUND) {
334#if BB_MMU
335 bb_daemonize(0 );
336#endif
337 bb_error_msg("start, interface %s", argv_intf);
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 if (chosen_nip == 0) {
358 new_nip_and_PROBE:
359 chosen_nip = pick_nip();
360 }
361 nsent = 0;
362 state = PROBE;
363 while (1) {
364 struct pollfd fds[1];
365 unsigned deadline_us = deadline_us;
366 struct arp_packet p;
367 int ip_conflict;
368 int n;
369
370 fds[0].fd = sock_fd;
371 fds[0].events = POLLIN;
372 fds[0].revents = 0;
373
374
375 if (!timeout_ms) {
376 timeout_ms = random_delay_ms(PROBE_WAIT);
377
378
379
380 }
381 if (timeout_ms >= 0) {
382
383 deadline_us = MONOTONIC_US() + timeout_ms * 1000;
384 }
385
386 VDBG("...wait %d %s nsent=%u\n",
387 timeout_ms, argv_intf, nsent);
388
389 n = safe_poll(fds, 1, timeout_ms);
390 if (n < 0) {
391
392 return EXIT_FAILURE;
393 }
394 if (n == 0) {
395 VDBG("state:%d\n", state);
396 switch (state) {
397 case PROBE:
398
399
400 if (nsent < PROBE_NUM) {
401 nsent++;
402 VDBG("probe/%u %s@%s\n",
403 nsent, argv_intf, nip_to_a(chosen_nip));
404 timeout_ms = PROBE_MIN * 1000;
405 timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
406 send_arp_request(0, &null_ethaddr, chosen_nip);
407 continue;
408 }
409
410 nsent = 0;
411 state = ANNOUNCE;
412 goto send_announce;
413 case ANNOUNCE:
414
415
416 if (nsent < ANNOUNCE_NUM) {
417 send_announce:
418 nsent++;
419 VDBG("announce/%u %s@%s\n",
420 nsent, argv_intf, nip_to_a(chosen_nip));
421 timeout_ms = ANNOUNCE_INTERVAL * 1000;
422 send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
423 continue;
424 }
425
426
427 run(argv, "config", chosen_nip);
428
429 if (QUIT)
430 return EXIT_SUCCESS;
431
432 default:
433
434
435
436 timeout_ms = -1;
437 state = MONITOR;
438 continue;
439 }
440 }
441
442
443
444
445 if (timeout_ms > 0) {
446 unsigned diff = deadline_us - MONOTONIC_US();
447 if ((int)(diff) < 0) {
448
449 diff = 0;
450 }
451 VDBG("adjusting timeout\n");
452 timeout_ms = (diff / 1000) | 1;
453 }
454
455 if ((fds[0].revents & POLLIN) == 0) {
456 if (fds[0].revents & POLLERR) {
457
458
459 bb_error_msg("iface %s is down", argv_intf);
460 if (state >= MONITOR) {
461
462 run(argv, "deconfig", chosen_nip);
463 }
464 return EXIT_FAILURE;
465 }
466 continue;
467 }
468
469
470 if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
471 bb_perror_msg_and_die(bb_msg_read_error);
472 }
473
474 if (p.eth.ether_type != htons(ETHERTYPE_ARP))
475 continue;
476 if (p.arp.arp_op != htons(ARPOP_REQUEST)
477 && p.arp.arp_op != htons(ARPOP_REPLY)
478 ) {
479 continue;
480 }
481#ifdef DEBUG
482 {
483 struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
484 struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha;
485 struct in_addr *spa = (struct in_addr *) p.arp.arp_spa;
486 struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
487 VDBG("source=%s %s\n", ether_ntoa(sha), inet_ntoa(*spa));
488 VDBG("target=%s %s\n", ether_ntoa(tha), inet_ntoa(*tpa));
489 }
490#endif
491 ip_conflict = 0;
492 if (memcmp(&p.arp.arp_sha, &G.our_ethaddr, ETH_ALEN) != 0) {
493 if (memcmp(p.arp.arp_spa, &chosen_nip, 4) == 0) {
494
495 ip_conflict = 1;
496 }
497 if (p.arp.arp_op == htons(ARPOP_REQUEST)
498 && memcmp(p.arp.arp_spa, &const_int_0, 4) == 0
499 && memcmp(p.arp.arp_tpa, &chosen_nip, 4) == 0
500 ) {
501
502
503 ip_conflict |= 2;
504 }
505 }
506 VDBG("state:%d ip_conflict:%d\n", state, ip_conflict);
507 if (!ip_conflict)
508 continue;
509
510
511 if (state <= ANNOUNCE) {
512
513 conflicts++;
514 timeout_ms = PROBE_MIN * 1000
515 + CONFLICT_MULTIPLIER * random_delay_ms(conflicts);
516 goto new_nip_and_PROBE;
517 }
518
519
520 if (ip_conflict & 1) {
521 if (state == MONITOR) {
522
523 VDBG("monitor conflict - defending\n");
524 timeout_ms = DEFEND_INTERVAL * 1000;
525 state = DEFEND;
526 send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
527 continue;
528 }
529
530
531 VDBG("defend conflict - starting over\n");
532 run(argv, "deconfig", chosen_nip);
533 conflicts = 0;
534 timeout_ms = 0;
535 goto new_nip_and_PROBE;
536 }
537
538
539
540
541
542 }
543#undef argv_intf
544}
545