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