1 | /*
|
---|
2 | * RFC3927 ZeroConf IPv4 Link-Local addressing
|
---|
3 | * (see <http://www.zeroconf.org/>)
|
---|
4 | *
|
---|
5 | * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com)
|
---|
6 | * Copyright (C) 2004 by David Brownell
|
---|
7 | *
|
---|
8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
---|
9 | */
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * ZCIP just manages the 169.254.*.* addresses. That network is not
|
---|
13 | * routed at the IP level, though various proxies or bridges can
|
---|
14 | * certainly be used. Its naming is built over multicast DNS.
|
---|
15 | */
|
---|
16 |
|
---|
17 | //#define DEBUG
|
---|
18 |
|
---|
19 | // TODO:
|
---|
20 | // - more real-world usage/testing, especially daemon mode
|
---|
21 | // - kernel packet filters to reduce scheduling noise
|
---|
22 | // - avoid silent script failures, especially under load...
|
---|
23 | // - link status monitoring (restart on link-up; stop on link-down)
|
---|
24 |
|
---|
25 | #include "busybox.h"
|
---|
26 | #include <errno.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <syslog.h>
|
---|
29 | #include <poll.h>
|
---|
30 | #include <time.h>
|
---|
31 |
|
---|
32 | #include <sys/wait.h>
|
---|
33 |
|
---|
34 | #include <netinet/ether.h>
|
---|
35 | #include <net/ethernet.h>
|
---|
36 | #include <net/if.h>
|
---|
37 | #include <net/if_arp.h>
|
---|
38 |
|
---|
39 | #include <linux/if_packet.h>
|
---|
40 | #include <linux/sockios.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | struct arp_packet {
|
---|
44 | struct ether_header hdr;
|
---|
45 | struct ether_arp arp;
|
---|
46 | } ATTRIBUTE_PACKED;
|
---|
47 |
|
---|
48 | enum {
|
---|
49 | /* 169.254.0.0 */
|
---|
50 | LINKLOCAL_ADDR = 0xa9fe0000,
|
---|
51 |
|
---|
52 | /* protocol timeout parameters, specified in seconds */
|
---|
53 | PROBE_WAIT = 1,
|
---|
54 | PROBE_MIN = 1,
|
---|
55 | PROBE_MAX = 2,
|
---|
56 | PROBE_NUM = 3,
|
---|
57 | MAX_CONFLICTS = 10,
|
---|
58 | RATE_LIMIT_INTERVAL = 60,
|
---|
59 | ANNOUNCE_WAIT = 2,
|
---|
60 | ANNOUNCE_NUM = 2,
|
---|
61 | ANNOUNCE_INTERVAL = 2,
|
---|
62 | DEFEND_INTERVAL = 10
|
---|
63 | };
|
---|
64 |
|
---|
65 | /* States during the configuration process. */
|
---|
66 | enum {
|
---|
67 | PROBE = 0,
|
---|
68 | RATE_LIMIT_PROBE,
|
---|
69 | ANNOUNCE,
|
---|
70 | MONITOR,
|
---|
71 | DEFEND
|
---|
72 | };
|
---|
73 |
|
---|
74 | /* Implicitly zero-initialized */
|
---|
75 | static const struct in_addr null_ip;
|
---|
76 | static const struct ether_addr null_addr;
|
---|
77 | static int verbose;
|
---|
78 |
|
---|
79 | #define DBG(fmt,args...) \
|
---|
80 | do { } while (0)
|
---|
81 | #define VDBG DBG
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Pick a random link local IP address on 169.254/16, except that
|
---|
85 | * the first and last 256 addresses are reserved.
|
---|
86 | */
|
---|
87 | static void pick(struct in_addr *ip)
|
---|
88 | {
|
---|
89 | unsigned tmp;
|
---|
90 |
|
---|
91 | /* use cheaper math than lrand48() mod N */
|
---|
92 | do {
|
---|
93 | tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
|
---|
94 | } while (tmp > (IN_CLASSB_HOST - 0x0200));
|
---|
95 | ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Broadcast an ARP packet.
|
---|
100 | */
|
---|
101 | static int arp(int fd, struct sockaddr *saddr, int op,
|
---|
102 | const struct ether_addr *source_addr, struct in_addr source_ip,
|
---|
103 | const struct ether_addr *target_addr, struct in_addr target_ip)
|
---|
104 | {
|
---|
105 | struct arp_packet p;
|
---|
106 | memset(&p, 0, sizeof(p));
|
---|
107 |
|
---|
108 | // ether header
|
---|
109 | p.hdr.ether_type = htons(ETHERTYPE_ARP);
|
---|
110 | memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN);
|
---|
111 | memset(p.hdr.ether_dhost, 0xff, ETH_ALEN);
|
---|
112 |
|
---|
113 | // arp request
|
---|
114 | p.arp.arp_hrd = htons(ARPHRD_ETHER);
|
---|
115 | p.arp.arp_pro = htons(ETHERTYPE_IP);
|
---|
116 | p.arp.arp_hln = ETH_ALEN;
|
---|
117 | p.arp.arp_pln = 4;
|
---|
118 | p.arp.arp_op = htons(op);
|
---|
119 | memcpy(&p.arp.arp_sha, source_addr, ETH_ALEN);
|
---|
120 | memcpy(&p.arp.arp_spa, &source_ip, sizeof (p.arp.arp_spa));
|
---|
121 | memcpy(&p.arp.arp_tha, target_addr, ETH_ALEN);
|
---|
122 | memcpy(&p.arp.arp_tpa, &target_ip, sizeof (p.arp.arp_tpa));
|
---|
123 |
|
---|
124 | // send it
|
---|
125 | if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
|
---|
126 | perror("sendto");
|
---|
127 | return -errno;
|
---|
128 | }
|
---|
129 | return 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Run a script.
|
---|
134 | */
|
---|
135 | static int run(char *script, char *arg, char *intf, struct in_addr *ip)
|
---|
136 | {
|
---|
137 | int pid, status;
|
---|
138 | char *why;
|
---|
139 |
|
---|
140 | if (script != NULL) {
|
---|
141 | VDBG("%s run %s %s\n", intf, script, arg);
|
---|
142 | if (ip != NULL) {
|
---|
143 | char *addr = inet_ntoa(*ip);
|
---|
144 | setenv("ip", addr, 1);
|
---|
145 | syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
|
---|
146 | }
|
---|
147 |
|
---|
148 | pid = vfork();
|
---|
149 | if (pid < 0) { // error
|
---|
150 | why = "vfork";
|
---|
151 | goto bad;
|
---|
152 | } else if (pid == 0) { // child
|
---|
153 | execl(script, script, arg, NULL);
|
---|
154 | perror("execl");
|
---|
155 | _exit(EXIT_FAILURE);
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (waitpid(pid, &status, 0) <= 0) {
|
---|
159 | why = "waitpid";
|
---|
160 | goto bad;
|
---|
161 | }
|
---|
162 | if (WEXITSTATUS(status) != 0) {
|
---|
163 | bb_error_msg("script %s failed, exit=%d\n",
|
---|
164 | script, WEXITSTATUS(status));
|
---|
165 | return -errno;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | return 0;
|
---|
169 | bad:
|
---|
170 | status = -errno;
|
---|
171 | syslog(LOG_ERR, "%s %s, %s error: %s",
|
---|
172 | arg, intf, why, strerror(errno));
|
---|
173 | return status;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Return milliseconds of random delay, up to "secs" seconds.
|
---|
179 | */
|
---|
180 | static inline unsigned ms_rdelay(unsigned secs)
|
---|
181 | {
|
---|
182 | return lrand48() % (secs * 1000);
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * main program
|
---|
187 | */
|
---|
188 |
|
---|
189 | int zcip_main(int argc, char *argv[])
|
---|
190 | {
|
---|
191 | char *intf = NULL;
|
---|
192 | char *script = NULL;
|
---|
193 | int quit = 0;
|
---|
194 | int foreground = 0;
|
---|
195 |
|
---|
196 | char *why;
|
---|
197 | struct sockaddr saddr;
|
---|
198 | struct ether_addr addr;
|
---|
199 | struct in_addr ip = { 0 };
|
---|
200 | int fd;
|
---|
201 | int ready = 0;
|
---|
202 | suseconds_t timeout = 0; // milliseconds
|
---|
203 | unsigned conflicts = 0;
|
---|
204 | unsigned nprobes = 0;
|
---|
205 | unsigned nclaims = 0;
|
---|
206 | int t;
|
---|
207 | int state = PROBE;
|
---|
208 |
|
---|
209 | // parse commandline: prog [options] ifname script
|
---|
210 | while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
|
---|
211 | switch (t) {
|
---|
212 | case 'f':
|
---|
213 | foreground = 1;
|
---|
214 | continue;
|
---|
215 | case 'q':
|
---|
216 | quit = 1;
|
---|
217 | continue;
|
---|
218 | case 'r':
|
---|
219 | if (inet_aton(optarg, &ip) == 0
|
---|
220 | || (ntohl(ip.s_addr) & IN_CLASSB_NET)
|
---|
221 | != LINKLOCAL_ADDR) {
|
---|
222 | bb_error_msg_and_die("invalid link address");
|
---|
223 | }
|
---|
224 | continue;
|
---|
225 | case 'v':
|
---|
226 | verbose++;
|
---|
227 | foreground = 1;
|
---|
228 | continue;
|
---|
229 | default:
|
---|
230 | bb_error_msg_and_die("bad option");
|
---|
231 | }
|
---|
232 | }
|
---|
233 | if (optind < argc - 1) {
|
---|
234 | intf = argv[optind++];
|
---|
235 | setenv("interface", intf, 1);
|
---|
236 | script = argv[optind++];
|
---|
237 | }
|
---|
238 | if (optind != argc || !intf)
|
---|
239 | bb_show_usage();
|
---|
240 | openlog(bb_applet_name, 0, LOG_DAEMON);
|
---|
241 |
|
---|
242 | // initialize the interface (modprobe, ifup, etc)
|
---|
243 | if (run(script, "init", intf, NULL) < 0)
|
---|
244 | return EXIT_FAILURE;
|
---|
245 |
|
---|
246 | // initialize saddr
|
---|
247 | memset(&saddr, 0, sizeof (saddr));
|
---|
248 | safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
|
---|
249 |
|
---|
250 | // open an ARP socket
|
---|
251 | if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) {
|
---|
252 | why = "open";
|
---|
253 | fail:
|
---|
254 | foreground = 1;
|
---|
255 | goto bad;
|
---|
256 | }
|
---|
257 | // bind to the interface's ARP socket
|
---|
258 | if (bind(fd, &saddr, sizeof (saddr)) < 0) {
|
---|
259 | why = "bind";
|
---|
260 | goto fail;
|
---|
261 | } else {
|
---|
262 | struct ifreq ifr;
|
---|
263 | unsigned short seed[3];
|
---|
264 |
|
---|
265 | // get the interface's ethernet address
|
---|
266 | memset(&ifr, 0, sizeof (ifr));
|
---|
267 | strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
|
---|
268 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
|
---|
269 | why = "get ethernet address";
|
---|
270 | goto fail;
|
---|
271 | }
|
---|
272 | memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
|
---|
273 |
|
---|
274 | // start with some stable ip address, either a function of
|
---|
275 | // the hardware address or else the last address we used.
|
---|
276 | // NOTE: the sequence of addresses we try changes only
|
---|
277 | // depending on when we detect conflicts.
|
---|
278 | memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
|
---|
279 | seed48(seed);
|
---|
280 | if (ip.s_addr == 0)
|
---|
281 | pick(&ip);
|
---|
282 | }
|
---|
283 |
|
---|
284 | // FIXME cases to handle:
|
---|
285 | // - zcip already running!
|
---|
286 | // - link already has local address... just defend/update
|
---|
287 |
|
---|
288 | // daemonize now; don't delay system startup
|
---|
289 | if (!foreground) {
|
---|
290 | if (daemon(0, verbose) < 0) {
|
---|
291 | why = "daemon";
|
---|
292 | goto bad;
|
---|
293 | }
|
---|
294 | syslog(LOG_INFO, "start, interface %s", intf);
|
---|
295 | }
|
---|
296 |
|
---|
297 | // run the dynamic address negotiation protocol,
|
---|
298 | // restarting after address conflicts:
|
---|
299 | // - start with some address we want to try
|
---|
300 | // - short random delay
|
---|
301 | // - arp probes to see if another host else uses it
|
---|
302 | // - arp announcements that we're claiming it
|
---|
303 | // - use it
|
---|
304 | // - defend it, within limits
|
---|
305 | while (1) {
|
---|
306 | struct pollfd fds[1];
|
---|
307 | struct timeval tv1;
|
---|
308 | struct arp_packet p;
|
---|
309 |
|
---|
310 | fds[0].fd = fd;
|
---|
311 | fds[0].events = POLLIN;
|
---|
312 | fds[0].revents = 0;
|
---|
313 |
|
---|
314 | int source_ip_conflict = 0;
|
---|
315 | int target_ip_conflict = 0;
|
---|
316 |
|
---|
317 | // poll, being ready to adjust current timeout
|
---|
318 | if (!timeout) {
|
---|
319 | timeout = ms_rdelay(PROBE_WAIT);
|
---|
320 | // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
|
---|
321 | // make the kernel filter out all packets except
|
---|
322 | // ones we'd care about.
|
---|
323 | }
|
---|
324 | // set tv1 to the point in time when we timeout
|
---|
325 | gettimeofday(&tv1, NULL);
|
---|
326 | tv1.tv_usec += (timeout % 1000) * 1000;
|
---|
327 | while (tv1.tv_usec > 1000000) {
|
---|
328 | tv1.tv_usec -= 1000000;
|
---|
329 | tv1.tv_sec++;
|
---|
330 | }
|
---|
331 | tv1.tv_sec += timeout / 1000;
|
---|
332 |
|
---|
333 | VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
|
---|
334 | timeout, intf, nprobes, nclaims);
|
---|
335 | switch (poll(fds, 1, timeout)) {
|
---|
336 |
|
---|
337 | // timeout
|
---|
338 | case 0:
|
---|
339 | VDBG("state = %d\n", state);
|
---|
340 | switch (state) {
|
---|
341 | case PROBE:
|
---|
342 | // timeouts in the PROBE state means no conflicting ARP packets
|
---|
343 | // have been received, so we can progress through the states
|
---|
344 | if (nprobes < PROBE_NUM) {
|
---|
345 | nprobes++;
|
---|
346 | VDBG("probe/%d %s@%s\n",
|
---|
347 | nprobes, intf, inet_ntoa(ip));
|
---|
348 | (void)arp(fd, &saddr, ARPOP_REQUEST,
|
---|
349 | &addr, null_ip,
|
---|
350 | &null_addr, ip);
|
---|
351 | timeout = PROBE_MIN * 1000;
|
---|
352 | timeout += ms_rdelay(PROBE_MAX
|
---|
353 | - PROBE_MIN);
|
---|
354 | }
|
---|
355 | else {
|
---|
356 | // Switch to announce state.
|
---|
357 | state = ANNOUNCE;
|
---|
358 | nclaims = 0;
|
---|
359 | VDBG("announce/%d %s@%s\n",
|
---|
360 | nclaims, intf, inet_ntoa(ip));
|
---|
361 | (void)arp(fd, &saddr, ARPOP_REQUEST,
|
---|
362 | &addr, ip,
|
---|
363 | &addr, ip);
|
---|
364 | timeout = ANNOUNCE_INTERVAL * 1000;
|
---|
365 | }
|
---|
366 | break;
|
---|
367 | case RATE_LIMIT_PROBE:
|
---|
368 | // timeouts in the RATE_LIMIT_PROBE state means no conflicting ARP packets
|
---|
369 | // have been received, so we can move immediately to the announce state
|
---|
370 | state = ANNOUNCE;
|
---|
371 | nclaims = 0;
|
---|
372 | VDBG("announce/%d %s@%s\n",
|
---|
373 | nclaims, intf, inet_ntoa(ip));
|
---|
374 | (void)arp(fd, &saddr, ARPOP_REQUEST,
|
---|
375 | &addr, ip,
|
---|
376 | &addr, ip);
|
---|
377 | timeout = ANNOUNCE_INTERVAL * 1000;
|
---|
378 | break;
|
---|
379 | case ANNOUNCE:
|
---|
380 | // timeouts in the ANNOUNCE state means no conflicting ARP packets
|
---|
381 | // have been received, so we can progress through the states
|
---|
382 | if (nclaims < ANNOUNCE_NUM) {
|
---|
383 | nclaims++;
|
---|
384 | VDBG("announce/%d %s@%s\n",
|
---|
385 | nclaims, intf, inet_ntoa(ip));
|
---|
386 | (void)arp(fd, &saddr, ARPOP_REQUEST,
|
---|
387 | &addr, ip,
|
---|
388 | &addr, ip);
|
---|
389 | timeout = ANNOUNCE_INTERVAL * 1000;
|
---|
390 | }
|
---|
391 | else {
|
---|
392 | // Switch to monitor state.
|
---|
393 | state = MONITOR;
|
---|
394 | // link is ok to use earlier
|
---|
395 | // FIXME update filters
|
---|
396 | run(script, "config", intf, &ip);
|
---|
397 | ready = 1;
|
---|
398 | conflicts = 0;
|
---|
399 | timeout = -1; // Never timeout in the monitor state.
|
---|
400 |
|
---|
401 | // NOTE: all other exit paths
|
---|
402 | // should deconfig ...
|
---|
403 | if (quit)
|
---|
404 | return EXIT_SUCCESS;
|
---|
405 | }
|
---|
406 | break;
|
---|
407 | case DEFEND:
|
---|
408 | // We won! No ARP replies, so just go back to monitor.
|
---|
409 | state = MONITOR;
|
---|
410 | timeout = -1;
|
---|
411 | conflicts = 0;
|
---|
412 | break;
|
---|
413 | default:
|
---|
414 | // Invalid, should never happen. Restart the whole protocol.
|
---|
415 | state = PROBE;
|
---|
416 | pick(&ip);
|
---|
417 | timeout = 0;
|
---|
418 | nprobes = 0;
|
---|
419 | nclaims = 0;
|
---|
420 | break;
|
---|
421 | } // switch (state)
|
---|
422 | break; // case 0 (timeout)
|
---|
423 | // packets arriving
|
---|
424 | case 1:
|
---|
425 | // We need to adjust the timeout in case we didn't receive
|
---|
426 | // a conflicting packet.
|
---|
427 | if (timeout > 0) {
|
---|
428 | struct timeval tv2;
|
---|
429 |
|
---|
430 | gettimeofday(&tv2, NULL);
|
---|
431 | if (timercmp(&tv1, &tv2, <)) {
|
---|
432 | // Current time is greater than the expected timeout time.
|
---|
433 | // Should never happen.
|
---|
434 | VDBG("missed an expected timeout\n");
|
---|
435 | timeout = 0;
|
---|
436 | } else {
|
---|
437 | VDBG("adjusting timeout\n");
|
---|
438 | timersub(&tv1, &tv2, &tv1);
|
---|
439 | timeout = 1000 * tv1.tv_sec
|
---|
440 | + tv1.tv_usec / 1000;
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | if ((fds[0].revents & POLLIN) == 0) {
|
---|
445 | if (fds[0].revents & POLLERR) {
|
---|
446 | // FIXME: links routinely go down;
|
---|
447 | // this shouldn't necessarily exit.
|
---|
448 | bb_error_msg("%s: poll error\n", intf);
|
---|
449 | if (ready) {
|
---|
450 | run(script, "deconfig",
|
---|
451 | intf, &ip);
|
---|
452 | }
|
---|
453 | return EXIT_FAILURE;
|
---|
454 | }
|
---|
455 | continue;
|
---|
456 | }
|
---|
457 |
|
---|
458 | // read ARP packet
|
---|
459 | if (recv(fd, &p, sizeof (p), 0) < 0) {
|
---|
460 | why = "recv";
|
---|
461 | goto bad;
|
---|
462 | }
|
---|
463 | if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
|
---|
464 | continue;
|
---|
465 |
|
---|
466 | #ifdef DEBUG
|
---|
467 | {
|
---|
468 | struct ether_addr * sha = (struct ether_addr *) p.arp.arp_sha;
|
---|
469 | struct ether_addr * tha = (struct ether_addr *) p.arp.arp_tha;
|
---|
470 | struct in_addr * spa = (struct in_addr *) p.arp.arp_spa;
|
---|
471 | struct in_addr * tpa = (struct in_addr *) p.arp.arp_tpa;
|
---|
472 | VDBG("%s recv arp type=%d, op=%d,\n",
|
---|
473 | intf, ntohs(p.hdr.ether_type),
|
---|
474 | ntohs(p.arp.arp_op));
|
---|
475 | VDBG("\tsource=%s %s\n",
|
---|
476 | ether_ntoa(sha),
|
---|
477 | inet_ntoa(*spa));
|
---|
478 | VDBG("\ttarget=%s %s\n",
|
---|
479 | ether_ntoa(tha),
|
---|
480 | inet_ntoa(*tpa));
|
---|
481 | }
|
---|
482 | #endif
|
---|
483 | if (p.arp.arp_op != htons(ARPOP_REQUEST)
|
---|
484 | && p.arp.arp_op != htons(ARPOP_REPLY))
|
---|
485 | continue;
|
---|
486 |
|
---|
487 | if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
|
---|
488 | memcmp(&addr, &p.arp.arp_sha, ETH_ALEN) != 0) {
|
---|
489 | source_ip_conflict = 1;
|
---|
490 | }
|
---|
491 | if (memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
|
---|
492 | p.arp.arp_op == htons(ARPOP_REQUEST) &&
|
---|
493 | memcmp(&addr, &p.arp.arp_tha, ETH_ALEN) != 0) {
|
---|
494 | target_ip_conflict = 1;
|
---|
495 | }
|
---|
496 |
|
---|
497 | VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
|
---|
498 | state, source_ip_conflict, target_ip_conflict);
|
---|
499 | switch (state) {
|
---|
500 | case PROBE:
|
---|
501 | case ANNOUNCE:
|
---|
502 | // When probing or announcing, check for source IP conflicts
|
---|
503 | // and other hosts doing ARP probes (target IP conflicts).
|
---|
504 | if (source_ip_conflict || target_ip_conflict) {
|
---|
505 | conflicts++;
|
---|
506 | if (conflicts >= MAX_CONFLICTS) {
|
---|
507 | VDBG("%s ratelimit\n", intf);
|
---|
508 | timeout = RATE_LIMIT_INTERVAL * 1000;
|
---|
509 | state = RATE_LIMIT_PROBE;
|
---|
510 | }
|
---|
511 |
|
---|
512 | // restart the whole protocol
|
---|
513 | pick(&ip);
|
---|
514 | timeout = 0;
|
---|
515 | nprobes = 0;
|
---|
516 | nclaims = 0;
|
---|
517 | }
|
---|
518 | break;
|
---|
519 | case MONITOR:
|
---|
520 | // If a conflict, we try to defend with a single ARP probe.
|
---|
521 | if (source_ip_conflict) {
|
---|
522 | VDBG("monitor conflict -- defending\n");
|
---|
523 | state = DEFEND;
|
---|
524 | timeout = DEFEND_INTERVAL * 1000;
|
---|
525 | (void)arp(fd, &saddr,
|
---|
526 | ARPOP_REQUEST,
|
---|
527 | &addr, ip,
|
---|
528 | &addr, ip);
|
---|
529 | }
|
---|
530 | break;
|
---|
531 | case DEFEND:
|
---|
532 | // Well, we tried. Start over (on conflict).
|
---|
533 | if (source_ip_conflict) {
|
---|
534 | state = PROBE;
|
---|
535 | VDBG("defend conflict -- starting over\n");
|
---|
536 | ready = 0;
|
---|
537 | run(script, "deconfig", intf, &ip);
|
---|
538 |
|
---|
539 | // restart the whole protocol
|
---|
540 | pick(&ip);
|
---|
541 | timeout = 0;
|
---|
542 | nprobes = 0;
|
---|
543 | nclaims = 0;
|
---|
544 | }
|
---|
545 | break;
|
---|
546 | default:
|
---|
547 | // Invalid, should never happen. Restart the whole protocol.
|
---|
548 | VDBG("invalid state -- starting over\n");
|
---|
549 | state = PROBE;
|
---|
550 | pick(&ip);
|
---|
551 | timeout = 0;
|
---|
552 | nprobes = 0;
|
---|
553 | nclaims = 0;
|
---|
554 | break;
|
---|
555 | } // switch state
|
---|
556 |
|
---|
557 | break; // case 1 (packets arriving)
|
---|
558 | default:
|
---|
559 | why = "poll";
|
---|
560 | goto bad;
|
---|
561 | } // switch poll
|
---|
562 | }
|
---|
563 | bad:
|
---|
564 | if (foreground)
|
---|
565 | perror(why);
|
---|
566 | else
|
---|
567 | syslog(LOG_ERR, "%s %s, %s error: %s",
|
---|
568 | bb_applet_name, intf, why, strerror(errno));
|
---|
569 | return EXIT_FAILURE;
|
---|
570 | }
|
---|