blob: 5e10673779a77f7da1f536c49e97703444058c3d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landleyad8071f2005-04-30 03:49:37 +00002/*
3 * RFC3927 ZeroConf IPv4 Link-Local addressing
4 * (see <http://www.zeroconf.org/>)
5 *
6 * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com)
7 * Copyright (C) 2004 by David Brownell
8 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00009 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Rob Landleyad8071f2005-04-30 03:49:37 +000010 */
11
12/*
Rob Landleyad8071f2005-04-30 03:49:37 +000013 * ZCIP just manages the 169.254.*.* addresses. That network is not
14 * routed at the IP level, though various proxies or bridges can
15 * certainly be used. Its naming is built over multicast DNS.
16 */
17
Rob Landleye3752e52005-05-03 03:28:55 +000018// #define DEBUG
19
20// TODO:
21// - more real-world usage/testing, especially daemon mode
22// - kernel packet filters to reduce scheduling noise
23// - avoid silent script failures, especially under load...
24// - link status monitoring (restart on link-up; stop on link-down)
25
Rob Landley53433b32006-06-22 22:28:29 +000026#include "busybox.h"
Rob Landleyad8071f2005-04-30 03:49:37 +000027#include <errno.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000028#include <string.h>
29#include <syslog.h>
30#include <poll.h>
31#include <time.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000032
Rob Landleyad8071f2005-04-30 03:49:37 +000033#include <sys/wait.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000034
Rob Landleyad8071f2005-04-30 03:49:37 +000035#include <netinet/ether.h>
36#include <net/ethernet.h>
37#include <net/if.h>
38#include <net/if_arp.h>
39
40#include <linux/if_packet.h>
41#include <linux/sockios.h>
Rob Landleye3752e52005-05-03 03:28:55 +000042
Rob Landleyad8071f2005-04-30 03:49:37 +000043
Bernhard Reutner-Fischerec351c32005-12-13 10:28:25 +000044struct arp_packet {
Rob Landleyad8071f2005-04-30 03:49:37 +000045 struct ether_header hdr;
Rob Landleye3752e52005-05-03 03:28:55 +000046 // FIXME this part is netinet/if_ether.h "struct ether_arp"
Rob Landleyad8071f2005-04-30 03:49:37 +000047 struct arphdr arp;
48 struct ether_addr source_addr;
49 struct in_addr source_ip;
50 struct ether_addr target_addr;
51 struct in_addr target_ip;
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000052} ATTRIBUTE_PACKED;
Rob Landleyad8071f2005-04-30 03:49:37 +000053
Rob Landleybc68cd12006-03-10 19:22:06 +000054enum {
Rob Landleyad8071f2005-04-30 03:49:37 +000055/* 169.254.0.0 */
Rob Landleybc68cd12006-03-10 19:22:06 +000056 LINKLOCAL_ADDR = 0xa9fe0000,
Rob Landleyad8071f2005-04-30 03:49:37 +000057
58/* protocol timeout parameters, specified in seconds */
Rob Landleybc68cd12006-03-10 19:22:06 +000059 PROBE_WAIT = 1,
60 PROBE_MIN = 1,
61 PROBE_MAX = 2,
62 PROBE_NUM = 3,
63 MAX_CONFLICTS = 10,
64 RATE_LIMIT_INTERVAL = 60,
65 ANNOUNCE_WAIT = 2,
66 ANNOUNCE_NUM = 2,
67 ANNOUNCE_INTERVAL = 2,
68 DEFEND_INTERVAL = 10
69};
Rob Landleyad8071f2005-04-30 03:49:37 +000070
Rob Landleyad8071f2005-04-30 03:49:37 +000071static const struct in_addr null_ip = { 0 };
72static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} };
73
Rob Landleye3752e52005-05-03 03:28:55 +000074static int verbose = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +000075
Rob Landleye3752e52005-05-03 03:28:55 +000076#define DBG(fmt,args...) \
77 do { } while (0)
78#define VDBG DBG
Rob Landleye3752e52005-05-03 03:28:55 +000079
80/**
Rob Landleyad8071f2005-04-30 03:49:37 +000081 * Pick a random link local IP address on 169.254/16, except that
82 * the first and last 256 addresses are reserved.
83 */
Rob Landley53433b32006-06-22 22:28:29 +000084static void pick(struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +000085{
86 unsigned tmp;
87
88 /* use cheaper math than lrand48() mod N */
89 do {
90 tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
91 } while (tmp > (IN_CLASSB_HOST - 0x0200));
92 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
93}
94
Rob Landleye3752e52005-05-03 03:28:55 +000095/**
Rob Landleyad8071f2005-04-30 03:49:37 +000096 * Broadcast an ARP packet.
97 */
Rob Landley53433b32006-06-22 22:28:29 +000098static int arp(int fd, struct sockaddr *saddr, int op,
Rob Landleyad8071f2005-04-30 03:49:37 +000099 const struct ether_addr *source_addr, struct in_addr source_ip,
100 const struct ether_addr *target_addr, struct in_addr target_ip)
101{
102 struct arp_packet p;
103
Rob Landleye3752e52005-05-03 03:28:55 +0000104 // ether header
Rob Landleyad8071f2005-04-30 03:49:37 +0000105 p.hdr.ether_type = htons(ETHERTYPE_ARP);
106 memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN);
107 memset(p.hdr.ether_dhost, 0xff, ETH_ALEN);
108
Rob Landleye3752e52005-05-03 03:28:55 +0000109 // arp request
Rob Landleyad8071f2005-04-30 03:49:37 +0000110 p.arp.ar_hrd = htons(ARPHRD_ETHER);
111 p.arp.ar_pro = htons(ETHERTYPE_IP);
112 p.arp.ar_hln = ETH_ALEN;
113 p.arp.ar_pln = 4;
114 p.arp.ar_op = htons(op);
115 memcpy(&p.source_addr, source_addr, ETH_ALEN);
116 memcpy(&p.source_ip, &source_ip, sizeof (p.source_ip));
117 memcpy(&p.target_addr, target_addr, ETH_ALEN);
118 memcpy(&p.target_ip, &target_ip, sizeof (p.target_ip));
119
Rob Landleye3752e52005-05-03 03:28:55 +0000120 // send it
Rob Landleyad8071f2005-04-30 03:49:37 +0000121 if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
Rob Landleye3752e52005-05-03 03:28:55 +0000122 perror("sendto");
Rob Landleyad8071f2005-04-30 03:49:37 +0000123 return -errno;
124 }
125 return 0;
126}
127
Rob Landleye3752e52005-05-03 03:28:55 +0000128/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000129 * Run a script.
130 */
Rob Landley53433b32006-06-22 22:28:29 +0000131static int run(char *script, char *arg, char *intf, struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000132{
133 int pid, status;
134 char *why;
135
136 if (script != NULL) {
Rob Landleye3752e52005-05-03 03:28:55 +0000137 VDBG("%s run %s %s\n", intf, script, arg);
Rob Landleyad8071f2005-04-30 03:49:37 +0000138 if (ip != NULL) {
139 char *addr = inet_ntoa(*ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000140 setenv("ip", addr, 1);
Rob Landleyad8071f2005-04-30 03:49:37 +0000141 syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
142 }
143
144 pid = vfork();
Rob Landleye3752e52005-05-03 03:28:55 +0000145 if (pid < 0) { // error
Rob Landleyad8071f2005-04-30 03:49:37 +0000146 why = "vfork";
147 goto bad;
Rob Landleye3752e52005-05-03 03:28:55 +0000148 } else if (pid == 0) { // child
Rob Landleyad8071f2005-04-30 03:49:37 +0000149 execl(script, script, arg, NULL);
Rob Landleye3752e52005-05-03 03:28:55 +0000150 perror("execl");
151 _exit(EXIT_FAILURE);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000152 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000153
154 if (waitpid(pid, &status, 0) <= 0) {
155 why = "waitpid";
156 goto bad;
157 }
158 if (WEXITSTATUS(status) != 0) {
Rob Landley53433b32006-06-22 22:28:29 +0000159 bb_error_msg("script %s failed, exit=%d\n",
160 script, WEXITSTATUS(status));
Rob Landleyad8071f2005-04-30 03:49:37 +0000161 return -errno;
162 }
163 }
164 return 0;
165bad:
166 status = -errno;
167 syslog(LOG_ERR, "%s %s, %s error: %s",
168 arg, intf, why, strerror(errno));
169 return status;
170}
171
Rob Landleye3752e52005-05-03 03:28:55 +0000172
173/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000174 * Return milliseconds of random delay, up to "secs" seconds.
175 */
Rob Landley53433b32006-06-22 22:28:29 +0000176static inline unsigned ms_rdelay(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000177{
178 return lrand48() % (secs * 1000);
179}
180
Rob Landleye3752e52005-05-03 03:28:55 +0000181/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000182 * main program
183 */
Bernhard Reutner-Fischer38d66152005-10-21 10:43:11 +0000184
Rob Landleyad8071f2005-04-30 03:49:37 +0000185int zcip_main(int argc, char *argv[])
186{
187 char *intf = NULL;
188 char *script = NULL;
Rob Landleye3752e52005-05-03 03:28:55 +0000189 int quit = 0;
190 int foreground = 0;
191
Rob Landleyad8071f2005-04-30 03:49:37 +0000192 char *why;
193 struct sockaddr saddr;
194 struct ether_addr addr;
195 struct in_addr ip = { 0 };
196 int fd;
197 int ready = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000198 suseconds_t timeout = 0; // milliseconds
Rob Landleyad8071f2005-04-30 03:49:37 +0000199 time_t defend = 0;
200 unsigned conflicts = 0;
201 unsigned nprobes = 0;
202 unsigned nclaims = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000203 int t;
Rob Landleyad8071f2005-04-30 03:49:37 +0000204
Rob Landleye3752e52005-05-03 03:28:55 +0000205 // parse commandline: prog [options] ifname script
Rob Landleye3752e52005-05-03 03:28:55 +0000206 while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
207 switch (t) {
208 case 'f':
209 foreground = 1;
210 continue;
211 case 'q':
212 quit = 1;
213 continue;
214 case 'r':
215 if (inet_aton(optarg, &ip) == 0
216 || (ntohl(ip.s_addr) & IN_CLASSB_NET)
217 != LINKLOCAL_ADDR) {
Rob Landley53433b32006-06-22 22:28:29 +0000218 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000219 }
220 continue;
221 case 'v':
Rob Landleye3752e52005-05-03 03:28:55 +0000222 verbose++;
223 foreground = 1;
224 continue;
225 default:
Rob Landley53433b32006-06-22 22:28:29 +0000226 bb_error_msg_and_die("bad option");
Rob Landleye3752e52005-05-03 03:28:55 +0000227 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000228 }
Rob Landleye3752e52005-05-03 03:28:55 +0000229 if (optind < argc - 1) {
230 intf = argv[optind++];
231 setenv("interface", intf, 1);
232 script = argv[optind++];
Rob Landleyad8071f2005-04-30 03:49:37 +0000233 }
Rob Landleye3752e52005-05-03 03:28:55 +0000234 if (optind != argc || !intf)
Rob Landley53433b32006-06-22 22:28:29 +0000235 bb_show_usage();
236 openlog(bb_applet_name, 0, LOG_DAEMON);
Rob Landleyad8071f2005-04-30 03:49:37 +0000237
Rob Landleye3752e52005-05-03 03:28:55 +0000238 // initialize the interface (modprobe, ifup, etc)
Rob Landleyad8071f2005-04-30 03:49:37 +0000239 if (run(script, "init", intf, NULL) < 0)
240 return EXIT_FAILURE;
241
Rob Landleye3752e52005-05-03 03:28:55 +0000242 // initialize saddr
Rob Landleyad8071f2005-04-30 03:49:37 +0000243 memset(&saddr, 0, sizeof (saddr));
Rob Landley768945b2006-06-25 00:34:52 +0000244 safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000245
Rob Landleye3752e52005-05-03 03:28:55 +0000246 // open an ARP socket
Rob Landleyad8071f2005-04-30 03:49:37 +0000247 if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) {
248 why = "open";
249fail:
Rob Landleye3752e52005-05-03 03:28:55 +0000250 foreground = 1;
Rob Landleyad8071f2005-04-30 03:49:37 +0000251 goto bad;
252 }
Rob Landleye3752e52005-05-03 03:28:55 +0000253 // bind to the interface's ARP socket
Rob Landleyad8071f2005-04-30 03:49:37 +0000254 if (bind(fd, &saddr, sizeof (saddr)) < 0) {
255 why = "bind";
256 goto fail;
257 } else {
258 struct ifreq ifr;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000259 unsigned short seed[3];
Rob Landleyad8071f2005-04-30 03:49:37 +0000260
Rob Landleye3752e52005-05-03 03:28:55 +0000261 // get the interface's ethernet address
Rob Landleyad8071f2005-04-30 03:49:37 +0000262 memset(&ifr, 0, sizeof (ifr));
263 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
264 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
265 why = "get ethernet address";
266 goto fail;
267 }
268 memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
269
Rob Landleye3752e52005-05-03 03:28:55 +0000270 // start with some stable ip address, either a function of
271 // the hardware address or else the last address we used.
272 // NOTE: the sequence of addresses we try changes only
273 // depending on when we detect conflicts.
Rob Landleyad8071f2005-04-30 03:49:37 +0000274 memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
275 seed48(seed);
276 if (ip.s_addr == 0)
277 pick(&ip);
278 }
279
Rob Landleye3752e52005-05-03 03:28:55 +0000280 // FIXME cases to handle:
281 // - zcip already running!
282 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000283
Rob Landleye3752e52005-05-03 03:28:55 +0000284 // daemonize now; don't delay system startup
285 if (!foreground) {
286 if (daemon(0, verbose) < 0) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000287 why = "daemon";
288 goto bad;
289 }
290 syslog(LOG_INFO, "start, interface %s", intf);
291 }
292
Rob Landleye3752e52005-05-03 03:28:55 +0000293 // run the dynamic address negotiation protocol,
294 // restarting after address conflicts:
295 // - start with some address we want to try
296 // - short random delay
297 // - arp probes to see if another host else uses it
298 // - arp announcements that we're claiming it
299 // - use it
300 // - defend it, within limits
Rob Landleyad8071f2005-04-30 03:49:37 +0000301 while (1) {
302 struct pollfd fds[1];
303 struct timeval tv1;
304 struct arp_packet p;
305
306 fds[0].fd = fd;
307 fds[0].events = POLLIN;
308 fds[0].revents = 0;
309
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000310 // poll, being ready to adjust current timeout
Rob Landley00c051e2006-06-30 14:05:19 +0000311 if (!timeout) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000312 timeout = ms_rdelay(PROBE_WAIT);
Rob Landleye3752e52005-05-03 03:28:55 +0000313 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
314 // make the kernel filter out all packets except
315 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000316 }
Rob Landley00c051e2006-06-30 14:05:19 +0000317 gettimeofday(&tv1, NULL);
318 tv1.tv_usec += (timeout % 1000) * 1000;
319 while (tv1.tv_usec > 1000000) {
320 tv1.tv_usec -= 1000000;
321 tv1.tv_sec++;
322 }
323 tv1.tv_sec += timeout / 1000;
324
Rob Landleye3752e52005-05-03 03:28:55 +0000325 VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
326 timeout, intf, nprobes, nclaims);
Rob Landleyad8071f2005-04-30 03:49:37 +0000327 switch (poll(fds, 1, timeout)) {
328
Rob Landleye3752e52005-05-03 03:28:55 +0000329 // timeouts trigger protocol transitions
Rob Landleyad8071f2005-04-30 03:49:37 +0000330 case 0:
Rob Landleye3752e52005-05-03 03:28:55 +0000331 // probes
Rob Landleyad8071f2005-04-30 03:49:37 +0000332 if (nprobes < PROBE_NUM) {
333 nprobes++;
Rob Landleye3752e52005-05-03 03:28:55 +0000334 VDBG("probe/%d %s@%s\n",
335 nprobes, intf, inet_ntoa(ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000336 (void)arp(fd, &saddr, ARPOP_REQUEST,
337 &addr, null_ip,
338 &null_addr, ip);
339 if (nprobes < PROBE_NUM) {
340 timeout = PROBE_MIN * 1000;
341 timeout += ms_rdelay(PROBE_MAX
342 - PROBE_MIN);
343 } else
344 timeout = ANNOUNCE_WAIT * 1000;
345 }
Rob Landleye3752e52005-05-03 03:28:55 +0000346 // then announcements
Rob Landleyad8071f2005-04-30 03:49:37 +0000347 else if (nclaims < ANNOUNCE_NUM) {
348 nclaims++;
Rob Landleye3752e52005-05-03 03:28:55 +0000349 VDBG("announce/%d %s@%s\n",
350 nclaims, intf, inet_ntoa(ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000351 (void)arp(fd, &saddr, ARPOP_REQUEST,
352 &addr, ip,
353 &addr, ip);
354 if (nclaims < ANNOUNCE_NUM) {
355 timeout = ANNOUNCE_INTERVAL * 1000;
356 } else {
Rob Landleye3752e52005-05-03 03:28:55 +0000357 // link is ok to use earlier
Rob Landleyad8071f2005-04-30 03:49:37 +0000358 run(script, "config", intf, &ip);
359 ready = 1;
360 conflicts = 0;
361 timeout = -1;
362
Rob Landleye3752e52005-05-03 03:28:55 +0000363 // NOTE: all other exit paths
364 // should deconfig ...
365 if (quit)
Rob Landleyad8071f2005-04-30 03:49:37 +0000366 return EXIT_SUCCESS;
Rob Landleye3752e52005-05-03 03:28:55 +0000367 // FIXME update filters
Rob Landleyad8071f2005-04-30 03:49:37 +0000368 }
369 }
370 break;
371
Rob Landleye3752e52005-05-03 03:28:55 +0000372 // packets arriving
Rob Landleyad8071f2005-04-30 03:49:37 +0000373 case 1:
Rob Landleye3752e52005-05-03 03:28:55 +0000374 // maybe adjust timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000375 if (timeout > 0) {
376 struct timeval tv2;
Rob Landleye3752e52005-05-03 03:28:55 +0000377
Rob Landleyad8071f2005-04-30 03:49:37 +0000378 gettimeofday(&tv2, NULL);
379 if (timercmp(&tv1, &tv2, <)) {
Paul Foxef81ce62006-03-29 23:01:33 +0000380 timeout = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000381 } else {
382 timersub(&tv1, &tv2, &tv1);
383 timeout = 1000 * tv1.tv_sec
384 + tv1.tv_usec / 1000;
385 }
386 }
387 if ((fds[0].revents & POLLIN) == 0) {
388 if (fds[0].revents & POLLERR) {
Rob Landleye3752e52005-05-03 03:28:55 +0000389 // FIXME: links routinely go down;
390 // this shouldn't necessarily exit.
Rob Landley53433b32006-06-22 22:28:29 +0000391 bb_error_msg("%s: poll error\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000392 if (ready) {
Rob Landleye3752e52005-05-03 03:28:55 +0000393 run(script, "deconfig",
394 intf, &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000395 }
396 return EXIT_FAILURE;
397 }
398 continue;
399 }
Rob Landleye3752e52005-05-03 03:28:55 +0000400 // read ARP packet
Rob Landleyad8071f2005-04-30 03:49:37 +0000401 if (recv(fd, &p, sizeof (p), 0) < 0) {
402 why = "recv";
403 goto bad;
404 }
405 if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
406 continue;
Rob Landleye3752e52005-05-03 03:28:55 +0000407
408 VDBG("%s recv arp type=%d, op=%d,\n",
409 intf, ntohs(p.hdr.ether_type),
410 ntohs(p.arp.ar_op));
411 VDBG("\tsource=%s %s\n",
412 ether_ntoa(&p.source_addr),
413 inet_ntoa(p.source_ip));
414 VDBG("\ttarget=%s %s\n",
415 ether_ntoa(&p.target_addr),
416 inet_ntoa(p.target_ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000417 if (p.arp.ar_op != htons(ARPOP_REQUEST)
418 && p.arp.ar_op != htons(ARPOP_REPLY))
419 continue;
420
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000421 // some cases are always conflicts
Rob Landleyad8071f2005-04-30 03:49:37 +0000422 if ((p.source_ip.s_addr == ip.s_addr)
423 && (memcmp(&addr, &p.source_addr,
424 ETH_ALEN) != 0)) {
425collision:
Rob Landleye3752e52005-05-03 03:28:55 +0000426 VDBG("%s ARP conflict from %s\n", intf,
427 ether_ntoa(&p.source_addr));
Rob Landleyad8071f2005-04-30 03:49:37 +0000428 if (ready) {
429 time_t now = time(0);
430
431 if ((defend + DEFEND_INTERVAL)
432 < now) {
433 defend = now;
434 (void)arp(fd, &saddr,
435 ARPOP_REQUEST,
436 &addr, ip,
437 &addr, ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000438 VDBG("%s defend\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000439 timeout = -1;
440 continue;
441 }
442 defend = now;
443 ready = 0;
444 run(script, "deconfig", intf, &ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000445 // FIXME rm filters: setsockopt(fd,
446 // SO_DETACH_FILTER, ...)
Rob Landleyad8071f2005-04-30 03:49:37 +0000447 }
448 conflicts++;
449 if (conflicts >= MAX_CONFLICTS) {
Rob Landleye3752e52005-05-03 03:28:55 +0000450 VDBG("%s ratelimit\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000451 sleep(RATE_LIMIT_INTERVAL);
452 }
Rob Landleye3752e52005-05-03 03:28:55 +0000453 // restart the whole protocol
Rob Landleyad8071f2005-04-30 03:49:37 +0000454 pick(&ip);
455 timeout = 0;
456 nprobes = 0;
457 nclaims = 0;
458 }
Rob Landleye3752e52005-05-03 03:28:55 +0000459 // two hosts probing one address is a collision too
Rob Landleyad8071f2005-04-30 03:49:37 +0000460 else if (p.target_ip.s_addr == ip.s_addr
461 && nclaims == 0
462 && p.arp.ar_op == htons(ARPOP_REQUEST)
463 && memcmp(&addr, &p.target_addr,
464 ETH_ALEN) != 0) {
465 goto collision;
466 }
467 break;
468
469 default:
470 why = "poll";
471 goto bad;
472 }
473 }
474bad:
Rob Landleye3752e52005-05-03 03:28:55 +0000475 if (foreground)
476 perror(why);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000477 else
Rob Landleyad8071f2005-04-30 03:49:37 +0000478 syslog(LOG_ERR, "%s %s, %s error: %s",
Rob Landley53433b32006-06-22 22:28:29 +0000479 bb_applet_name, intf, why, strerror(errno));
Rob Landleyad8071f2005-04-30 03:49:37 +0000480 return EXIT_FAILURE;
481}