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