blob: 11b2db526fb12ded520a3c44c15d6869329f9a30 [file] [log] [blame]
Rob Landleyad8071f2005-04-30 03:49:37 +00001/*
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 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Rob Landleyad8071f2005-04-30 03:49:37 +00009 */
10
11/*
12 * This can build as part of BusyBox or by itself:
13 *
14 * $(CROSS_COMPILE)cc -Os -Wall -DNO_BUSYBOX -DDEBUG -o zcip zcip.c
15 *
16 * ZCIP just manages the 169.254.*.* addresses. That network is not
17 * routed at the IP level, though various proxies or bridges can
18 * certainly be used. Its naming is built over multicast DNS.
19 */
20
Rob Landleye3752e52005-05-03 03:28:55 +000021// #define DEBUG
22
23// TODO:
24// - more real-world usage/testing, especially daemon mode
25// - kernel packet filters to reduce scheduling noise
26// - avoid silent script failures, especially under load...
27// - link status monitoring (restart on link-up; stop on link-down)
28
Rob Landleyad8071f2005-04-30 03:49:37 +000029#include <errno.h>
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <syslog.h>
34#include <poll.h>
35#include <time.h>
36#include <unistd.h>
37
38#include <sys/ioctl.h>
39#include <sys/types.h>
40#include <sys/wait.h>
41#include <sys/time.h>
42#include <sys/socket.h>
43
44#include <arpa/inet.h>
45#include <netinet/in.h>
46#include <netinet/ether.h>
47#include <net/ethernet.h>
48#include <net/if.h>
49#include <net/if_arp.h>
50
51#include <linux/if_packet.h>
52#include <linux/sockios.h>
Rob Landleye3752e52005-05-03 03:28:55 +000053
Rob Landleyad8071f2005-04-30 03:49:37 +000054
Bernhard Reutner-Fischerec351c32005-12-13 10:28:25 +000055struct arp_packet {
Rob Landleyad8071f2005-04-30 03:49:37 +000056 struct ether_header hdr;
Rob Landleye3752e52005-05-03 03:28:55 +000057 // FIXME this part is netinet/if_ether.h "struct ether_arp"
Rob Landleyad8071f2005-04-30 03:49:37 +000058 struct arphdr arp;
59 struct ether_addr source_addr;
60 struct in_addr source_ip;
61 struct ether_addr target_addr;
62 struct in_addr target_ip;
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000063} ATTRIBUTE_PACKED;
Rob Landleyad8071f2005-04-30 03:49:37 +000064
65/* 169.254.0.0 */
66static const uint32_t LINKLOCAL_ADDR = 0xa9fe0000;
67
68/* protocol timeout parameters, specified in seconds */
69static const unsigned PROBE_WAIT = 1;
70static const unsigned PROBE_MIN = 1;
71static const unsigned PROBE_MAX = 2;
72static const unsigned PROBE_NUM = 3;
73static const unsigned MAX_CONFLICTS = 10;
74static const unsigned RATE_LIMIT_INTERVAL = 60;
75static const unsigned ANNOUNCE_WAIT = 2;
76static const unsigned ANNOUNCE_NUM = 2;
77static const unsigned ANNOUNCE_INTERVAL = 2;
78static const time_t DEFEND_INTERVAL = 10;
79
Rob Landleye3752e52005-05-03 03:28:55 +000080static const unsigned char ZCIP_VERSION[] = "0.75 (18 April 2005)";
81static char *prog;
Rob Landleyad8071f2005-04-30 03:49:37 +000082
83static const struct in_addr null_ip = { 0 };
84static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} };
85
Rob Landleye3752e52005-05-03 03:28:55 +000086static int verbose = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +000087
Rob Landleye3752e52005-05-03 03:28:55 +000088#ifdef DEBUG
89
90#define DBG(fmt,args...) \
91 fprintf(stderr, "%s: " fmt , prog , ## args)
92#define VDBG(fmt,args...) do { \
93 if (verbose) fprintf(stderr, "%s: " fmt , prog ,## args); \
94 } while (0)
95#else
96
97#define DBG(fmt,args...) \
98 do { } while (0)
99#define VDBG DBG
100#endif /* DEBUG */
101
102/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000103 * Pick a random link local IP address on 169.254/16, except that
104 * the first and last 256 addresses are reserved.
105 */
106static void
107pick(struct in_addr *ip)
108{
109 unsigned tmp;
110
111 /* use cheaper math than lrand48() mod N */
112 do {
113 tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
114 } while (tmp > (IN_CLASSB_HOST - 0x0200));
115 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
116}
117
Rob Landleye3752e52005-05-03 03:28:55 +0000118/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000119 * Broadcast an ARP packet.
120 */
121static int
122arp(int fd, struct sockaddr *saddr, int op,
123 const struct ether_addr *source_addr, struct in_addr source_ip,
124 const struct ether_addr *target_addr, struct in_addr target_ip)
125{
126 struct arp_packet p;
127
Rob Landleye3752e52005-05-03 03:28:55 +0000128 // ether header
Rob Landleyad8071f2005-04-30 03:49:37 +0000129 p.hdr.ether_type = htons(ETHERTYPE_ARP);
130 memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN);
131 memset(p.hdr.ether_dhost, 0xff, ETH_ALEN);
132
Rob Landleye3752e52005-05-03 03:28:55 +0000133 // arp request
Rob Landleyad8071f2005-04-30 03:49:37 +0000134 p.arp.ar_hrd = htons(ARPHRD_ETHER);
135 p.arp.ar_pro = htons(ETHERTYPE_IP);
136 p.arp.ar_hln = ETH_ALEN;
137 p.arp.ar_pln = 4;
138 p.arp.ar_op = htons(op);
139 memcpy(&p.source_addr, source_addr, ETH_ALEN);
140 memcpy(&p.source_ip, &source_ip, sizeof (p.source_ip));
141 memcpy(&p.target_addr, target_addr, ETH_ALEN);
142 memcpy(&p.target_ip, &target_ip, sizeof (p.target_ip));
143
Rob Landleye3752e52005-05-03 03:28:55 +0000144 // send it
Rob Landleyad8071f2005-04-30 03:49:37 +0000145 if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
Rob Landleye3752e52005-05-03 03:28:55 +0000146 perror("sendto");
Rob Landleyad8071f2005-04-30 03:49:37 +0000147 return -errno;
148 }
149 return 0;
150}
151
Rob Landleye3752e52005-05-03 03:28:55 +0000152/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000153 * Run a script.
154 */
155static int
156run(char *script, char *arg, char *intf, struct in_addr *ip)
157{
158 int pid, status;
159 char *why;
160
161 if (script != NULL) {
Rob Landleye3752e52005-05-03 03:28:55 +0000162 VDBG("%s run %s %s\n", intf, script, arg);
Rob Landleyad8071f2005-04-30 03:49:37 +0000163 if (ip != NULL) {
164 char *addr = inet_ntoa(*ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000165 setenv("ip", addr, 1);
Rob Landleyad8071f2005-04-30 03:49:37 +0000166 syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
167 }
168
169 pid = vfork();
Rob Landleye3752e52005-05-03 03:28:55 +0000170 if (pid < 0) { // error
Rob Landleyad8071f2005-04-30 03:49:37 +0000171 why = "vfork";
172 goto bad;
Rob Landleye3752e52005-05-03 03:28:55 +0000173 } else if (pid == 0) { // child
Rob Landleyad8071f2005-04-30 03:49:37 +0000174 execl(script, script, arg, NULL);
Rob Landleye3752e52005-05-03 03:28:55 +0000175 perror("execl");
176 _exit(EXIT_FAILURE);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000177 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000178
179 if (waitpid(pid, &status, 0) <= 0) {
180 why = "waitpid";
181 goto bad;
182 }
183 if (WEXITSTATUS(status) != 0) {
Rob Landleye3752e52005-05-03 03:28:55 +0000184 fprintf(stderr, "%s: script %s failed, exit=%d\n",
185 prog, script, WEXITSTATUS(status));
Rob Landleyad8071f2005-04-30 03:49:37 +0000186 return -errno;
187 }
188 }
189 return 0;
190bad:
191 status = -errno;
192 syslog(LOG_ERR, "%s %s, %s error: %s",
193 arg, intf, why, strerror(errno));
194 return status;
195}
196
Rob Landleye3752e52005-05-03 03:28:55 +0000197#ifndef NO_BUSYBOX
198#include "busybox.h"
199#endif
200
201/**
202 * Print usage information.
203 */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000204static void ATTRIBUTE_NORETURN
Bernhard Reutner-Fischerb7d53422005-12-12 10:21:35 +0000205zcip_usage(const char *msg)
Rob Landleye3752e52005-05-03 03:28:55 +0000206{
207 fprintf(stderr, "%s: %s\n", prog, msg);
208#ifdef NO_BUSYBOX
209 fprintf(stderr, "Usage: %s [OPTIONS] ifname script\n"
210 "\t-f foreground mode (implied by -v)\n"
211 "\t-q quit after address (no daemon)\n"
212 "\t-r 169.254.x.x request this address first\n"
213 "\t-v verbose; show version\n",
214 prog);
215 exit(0);
216#else
217 bb_show_usage();
218#endif
219}
220
221/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000222 * Return milliseconds of random delay, up to "secs" seconds.
223 */
224static inline unsigned
225ms_rdelay(unsigned secs)
226{
227 return lrand48() % (secs * 1000);
228}
229
Rob Landleye3752e52005-05-03 03:28:55 +0000230/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000231 * main program
232 */
Bernhard Reutner-Fischer38d66152005-10-21 10:43:11 +0000233
234#ifdef NO_BUSYBOX
Rob Landleye3752e52005-05-03 03:28:55 +0000235int
236main(int argc, char *argv[])
237 __attribute__ ((weak, alias ("zcip_main")));
Bernhard Reutner-Fischer38d66152005-10-21 10:43:11 +0000238#endif
Rob Landleyad8071f2005-04-30 03:49:37 +0000239
240int zcip_main(int argc, char *argv[])
241{
242 char *intf = NULL;
243 char *script = NULL;
Rob Landleye3752e52005-05-03 03:28:55 +0000244 int quit = 0;
245 int foreground = 0;
246
Rob Landleyad8071f2005-04-30 03:49:37 +0000247 char *why;
248 struct sockaddr saddr;
249 struct ether_addr addr;
250 struct in_addr ip = { 0 };
251 int fd;
252 int ready = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000253 suseconds_t timeout = 0; // milliseconds
Rob Landleyad8071f2005-04-30 03:49:37 +0000254 time_t defend = 0;
255 unsigned conflicts = 0;
256 unsigned nprobes = 0;
257 unsigned nclaims = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000258 int t;
Rob Landleyad8071f2005-04-30 03:49:37 +0000259
Rob Landleye3752e52005-05-03 03:28:55 +0000260 // parse commandline: prog [options] ifname script
261 prog = argv[0];
262 while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
263 switch (t) {
264 case 'f':
265 foreground = 1;
266 continue;
267 case 'q':
268 quit = 1;
269 continue;
270 case 'r':
271 if (inet_aton(optarg, &ip) == 0
272 || (ntohl(ip.s_addr) & IN_CLASSB_NET)
273 != LINKLOCAL_ADDR) {
Bernhard Reutner-Fischerb7d53422005-12-12 10:21:35 +0000274 zcip_usage("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000275 }
276 continue;
277 case 'v':
278 if (!verbose)
279 printf("%s: version %s\n", prog, ZCIP_VERSION);
280 verbose++;
281 foreground = 1;
282 continue;
283 default:
Bernhard Reutner-Fischerb7d53422005-12-12 10:21:35 +0000284 zcip_usage("bad option");
Rob Landleye3752e52005-05-03 03:28:55 +0000285 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000286 }
Rob Landleye3752e52005-05-03 03:28:55 +0000287 if (optind < argc - 1) {
288 intf = argv[optind++];
289 setenv("interface", intf, 1);
290 script = argv[optind++];
Rob Landleyad8071f2005-04-30 03:49:37 +0000291 }
Rob Landleye3752e52005-05-03 03:28:55 +0000292 if (optind != argc || !intf)
Bernhard Reutner-Fischerb7d53422005-12-12 10:21:35 +0000293 zcip_usage("wrong number of arguments");
Rob Landleye3752e52005-05-03 03:28:55 +0000294 openlog(prog, 0, LOG_DAEMON);
Rob Landleyad8071f2005-04-30 03:49:37 +0000295
Rob Landleye3752e52005-05-03 03:28:55 +0000296 // initialize the interface (modprobe, ifup, etc)
Rob Landleyad8071f2005-04-30 03:49:37 +0000297 if (run(script, "init", intf, NULL) < 0)
298 return EXIT_FAILURE;
299
Rob Landleye3752e52005-05-03 03:28:55 +0000300 // initialize saddr
Rob Landleyad8071f2005-04-30 03:49:37 +0000301 memset(&saddr, 0, sizeof (saddr));
302 strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
303
Rob Landleye3752e52005-05-03 03:28:55 +0000304 // open an ARP socket
Rob Landleyad8071f2005-04-30 03:49:37 +0000305 if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) {
306 why = "open";
307fail:
Rob Landleye3752e52005-05-03 03:28:55 +0000308 foreground = 1;
Rob Landleyad8071f2005-04-30 03:49:37 +0000309 goto bad;
310 }
Rob Landleye3752e52005-05-03 03:28:55 +0000311 // bind to the interface's ARP socket
Rob Landleyad8071f2005-04-30 03:49:37 +0000312 if (bind(fd, &saddr, sizeof (saddr)) < 0) {
313 why = "bind";
314 goto fail;
315 } else {
316 struct ifreq ifr;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000317 unsigned short seed[3];
Rob Landleyad8071f2005-04-30 03:49:37 +0000318
Rob Landleye3752e52005-05-03 03:28:55 +0000319 // get the interface's ethernet address
Rob Landleyad8071f2005-04-30 03:49:37 +0000320 memset(&ifr, 0, sizeof (ifr));
321 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
322 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
323 why = "get ethernet address";
324 goto fail;
325 }
326 memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
327
Rob Landleye3752e52005-05-03 03:28:55 +0000328 // start with some stable ip address, either a function of
329 // the hardware address or else the last address we used.
330 // NOTE: the sequence of addresses we try changes only
331 // depending on when we detect conflicts.
Rob Landleyad8071f2005-04-30 03:49:37 +0000332 memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
333 seed48(seed);
334 if (ip.s_addr == 0)
335 pick(&ip);
336 }
337
Rob Landleye3752e52005-05-03 03:28:55 +0000338 // FIXME cases to handle:
339 // - zcip already running!
340 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000341
Rob Landleye3752e52005-05-03 03:28:55 +0000342 // daemonize now; don't delay system startup
343 if (!foreground) {
344 if (daemon(0, verbose) < 0) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000345 why = "daemon";
346 goto bad;
347 }
348 syslog(LOG_INFO, "start, interface %s", intf);
349 }
350
Rob Landleye3752e52005-05-03 03:28:55 +0000351 // run the dynamic address negotiation protocol,
352 // restarting after address conflicts:
353 // - start with some address we want to try
354 // - short random delay
355 // - arp probes to see if another host else uses it
356 // - arp announcements that we're claiming it
357 // - use it
358 // - defend it, within limits
Rob Landleyad8071f2005-04-30 03:49:37 +0000359 while (1) {
360 struct pollfd fds[1];
361 struct timeval tv1;
362 struct arp_packet p;
363
364 fds[0].fd = fd;
365 fds[0].events = POLLIN;
366 fds[0].revents = 0;
367
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000368 // poll, being ready to adjust current timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000369 if (timeout > 0) {
370 gettimeofday(&tv1, NULL);
371 tv1.tv_usec += (timeout % 1000) * 1000;
372 while (tv1.tv_usec > 1000000) {
373 tv1.tv_usec -= 1000000;
374 tv1.tv_sec++;
375 }
376 tv1.tv_sec += timeout / 1000;
377 } else if (timeout == 0) {
378 timeout = ms_rdelay(PROBE_WAIT);
Rob Landleye3752e52005-05-03 03:28:55 +0000379 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
380 // make the kernel filter out all packets except
381 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000382 }
Rob Landleye3752e52005-05-03 03:28:55 +0000383 VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
384 timeout, intf, nprobes, nclaims);
Rob Landleyad8071f2005-04-30 03:49:37 +0000385 switch (poll(fds, 1, timeout)) {
386
Rob Landleye3752e52005-05-03 03:28:55 +0000387 // timeouts trigger protocol transitions
Rob Landleyad8071f2005-04-30 03:49:37 +0000388 case 0:
Rob Landleye3752e52005-05-03 03:28:55 +0000389 // probes
Rob Landleyad8071f2005-04-30 03:49:37 +0000390 if (nprobes < PROBE_NUM) {
391 nprobes++;
Rob Landleye3752e52005-05-03 03:28:55 +0000392 VDBG("probe/%d %s@%s\n",
393 nprobes, intf, inet_ntoa(ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000394 (void)arp(fd, &saddr, ARPOP_REQUEST,
395 &addr, null_ip,
396 &null_addr, ip);
397 if (nprobes < PROBE_NUM) {
398 timeout = PROBE_MIN * 1000;
399 timeout += ms_rdelay(PROBE_MAX
400 - PROBE_MIN);
401 } else
402 timeout = ANNOUNCE_WAIT * 1000;
403 }
Rob Landleye3752e52005-05-03 03:28:55 +0000404 // then announcements
Rob Landleyad8071f2005-04-30 03:49:37 +0000405 else if (nclaims < ANNOUNCE_NUM) {
406 nclaims++;
Rob Landleye3752e52005-05-03 03:28:55 +0000407 VDBG("announce/%d %s@%s\n",
408 nclaims, intf, inet_ntoa(ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000409 (void)arp(fd, &saddr, ARPOP_REQUEST,
410 &addr, ip,
411 &addr, ip);
412 if (nclaims < ANNOUNCE_NUM) {
413 timeout = ANNOUNCE_INTERVAL * 1000;
414 } else {
Rob Landleye3752e52005-05-03 03:28:55 +0000415 // link is ok to use earlier
Rob Landleyad8071f2005-04-30 03:49:37 +0000416 run(script, "config", intf, &ip);
417 ready = 1;
418 conflicts = 0;
419 timeout = -1;
420
Rob Landleye3752e52005-05-03 03:28:55 +0000421 // NOTE: all other exit paths
422 // should deconfig ...
423 if (quit)
Rob Landleyad8071f2005-04-30 03:49:37 +0000424 return EXIT_SUCCESS;
Rob Landleye3752e52005-05-03 03:28:55 +0000425 // FIXME update filters
Rob Landleyad8071f2005-04-30 03:49:37 +0000426 }
427 }
428 break;
429
Rob Landleye3752e52005-05-03 03:28:55 +0000430 // packets arriving
Rob Landleyad8071f2005-04-30 03:49:37 +0000431 case 1:
Rob Landleye3752e52005-05-03 03:28:55 +0000432 // maybe adjust timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000433 if (timeout > 0) {
434 struct timeval tv2;
Rob Landleye3752e52005-05-03 03:28:55 +0000435
Rob Landleyad8071f2005-04-30 03:49:37 +0000436 gettimeofday(&tv2, NULL);
437 if (timercmp(&tv1, &tv2, <)) {
438 timeout = -1;
439 } else {
440 timersub(&tv1, &tv2, &tv1);
441 timeout = 1000 * tv1.tv_sec
442 + tv1.tv_usec / 1000;
443 }
444 }
445 if ((fds[0].revents & POLLIN) == 0) {
446 if (fds[0].revents & POLLERR) {
Rob Landleye3752e52005-05-03 03:28:55 +0000447 // FIXME: links routinely go down;
448 // this shouldn't necessarily exit.
449 fprintf(stderr, "%s %s: poll error\n",
450 prog, intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000451 if (ready) {
Rob Landleye3752e52005-05-03 03:28:55 +0000452 run(script, "deconfig",
453 intf, &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000454 }
455 return EXIT_FAILURE;
456 }
457 continue;
458 }
Rob Landleye3752e52005-05-03 03:28:55 +0000459 // read ARP packet
Rob Landleyad8071f2005-04-30 03:49:37 +0000460 if (recv(fd, &p, sizeof (p), 0) < 0) {
461 why = "recv";
462 goto bad;
463 }
464 if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
465 continue;
Rob Landleye3752e52005-05-03 03:28:55 +0000466
467 VDBG("%s recv arp type=%d, op=%d,\n",
468 intf, ntohs(p.hdr.ether_type),
469 ntohs(p.arp.ar_op));
470 VDBG("\tsource=%s %s\n",
471 ether_ntoa(&p.source_addr),
472 inet_ntoa(p.source_ip));
473 VDBG("\ttarget=%s %s\n",
474 ether_ntoa(&p.target_addr),
475 inet_ntoa(p.target_ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000476 if (p.arp.ar_op != htons(ARPOP_REQUEST)
477 && p.arp.ar_op != htons(ARPOP_REPLY))
478 continue;
479
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000480 // some cases are always conflicts
Rob Landleyad8071f2005-04-30 03:49:37 +0000481 if ((p.source_ip.s_addr == ip.s_addr)
482 && (memcmp(&addr, &p.source_addr,
483 ETH_ALEN) != 0)) {
484collision:
Rob Landleye3752e52005-05-03 03:28:55 +0000485 VDBG("%s ARP conflict from %s\n", intf,
486 ether_ntoa(&p.source_addr));
Rob Landleyad8071f2005-04-30 03:49:37 +0000487 if (ready) {
488 time_t now = time(0);
489
490 if ((defend + DEFEND_INTERVAL)
491 < now) {
492 defend = now;
493 (void)arp(fd, &saddr,
494 ARPOP_REQUEST,
495 &addr, ip,
496 &addr, ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000497 VDBG("%s defend\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000498 timeout = -1;
499 continue;
500 }
501 defend = now;
502 ready = 0;
503 run(script, "deconfig", intf, &ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000504 // FIXME rm filters: setsockopt(fd,
505 // SO_DETACH_FILTER, ...)
Rob Landleyad8071f2005-04-30 03:49:37 +0000506 }
507 conflicts++;
508 if (conflicts >= MAX_CONFLICTS) {
Rob Landleye3752e52005-05-03 03:28:55 +0000509 VDBG("%s ratelimit\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000510 sleep(RATE_LIMIT_INTERVAL);
511 }
Rob Landleye3752e52005-05-03 03:28:55 +0000512 // restart the whole protocol
Rob Landleyad8071f2005-04-30 03:49:37 +0000513 pick(&ip);
514 timeout = 0;
515 nprobes = 0;
516 nclaims = 0;
517 }
Rob Landleye3752e52005-05-03 03:28:55 +0000518 // two hosts probing one address is a collision too
Rob Landleyad8071f2005-04-30 03:49:37 +0000519 else if (p.target_ip.s_addr == ip.s_addr
520 && nclaims == 0
521 && p.arp.ar_op == htons(ARPOP_REQUEST)
522 && memcmp(&addr, &p.target_addr,
523 ETH_ALEN) != 0) {
524 goto collision;
525 }
526 break;
527
528 default:
529 why = "poll";
530 goto bad;
531 }
532 }
533bad:
Rob Landleye3752e52005-05-03 03:28:55 +0000534 if (foreground)
535 perror(why);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000536 else
Rob Landleyad8071f2005-04-30 03:49:37 +0000537 syslog(LOG_ERR, "%s %s, %s error: %s",
Rob Landleye3752e52005-05-03 03:28:55 +0000538 prog, intf, why, strerror(errno));
Rob Landleyad8071f2005-04-30 03:49:37 +0000539 return EXIT_FAILURE;
540}