blob: 716c4a5e1fa9918defb803c35d3536cb8b49dea3 [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
Rob Landleybc68cd12006-03-10 19:22:06 +000065enum {
Rob Landleyad8071f2005-04-30 03:49:37 +000066/* 169.254.0.0 */
Rob Landleybc68cd12006-03-10 19:22:06 +000067 LINKLOCAL_ADDR = 0xa9fe0000,
Rob Landleyad8071f2005-04-30 03:49:37 +000068
69/* protocol timeout parameters, specified in seconds */
Rob Landleybc68cd12006-03-10 19:22:06 +000070 PROBE_WAIT = 1,
71 PROBE_MIN = 1,
72 PROBE_MAX = 2,
73 PROBE_NUM = 3,
74 MAX_CONFLICTS = 10,
75 RATE_LIMIT_INTERVAL = 60,
76 ANNOUNCE_WAIT = 2,
77 ANNOUNCE_NUM = 2,
78 ANNOUNCE_INTERVAL = 2,
79 DEFEND_INTERVAL = 10
80};
Rob Landleyad8071f2005-04-30 03:49:37 +000081
Rob Landleye3752e52005-05-03 03:28:55 +000082static const unsigned char ZCIP_VERSION[] = "0.75 (18 April 2005)";
83static char *prog;
Rob Landleyad8071f2005-04-30 03:49:37 +000084
85static const struct in_addr null_ip = { 0 };
86static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} };
87
Rob Landleye3752e52005-05-03 03:28:55 +000088static int verbose = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +000089
Rob Landleye3752e52005-05-03 03:28:55 +000090#ifdef DEBUG
91
92#define DBG(fmt,args...) \
93 fprintf(stderr, "%s: " fmt , prog , ## args)
94#define VDBG(fmt,args...) do { \
95 if (verbose) fprintf(stderr, "%s: " fmt , prog ,## args); \
96 } while (0)
97#else
98
99#define DBG(fmt,args...) \
100 do { } while (0)
101#define VDBG DBG
102#endif /* DEBUG */
103
104/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000105 * Pick a random link local IP address on 169.254/16, except that
106 * the first and last 256 addresses are reserved.
107 */
108static void
109pick(struct in_addr *ip)
110{
111 unsigned tmp;
112
113 /* use cheaper math than lrand48() mod N */
114 do {
115 tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
116 } while (tmp > (IN_CLASSB_HOST - 0x0200));
117 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
118}
119
Rob Landleye3752e52005-05-03 03:28:55 +0000120/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000121 * Broadcast an ARP packet.
122 */
123static int
124arp(int fd, struct sockaddr *saddr, int op,
125 const struct ether_addr *source_addr, struct in_addr source_ip,
126 const struct ether_addr *target_addr, struct in_addr target_ip)
127{
128 struct arp_packet p;
129
Rob Landleye3752e52005-05-03 03:28:55 +0000130 // ether header
Rob Landleyad8071f2005-04-30 03:49:37 +0000131 p.hdr.ether_type = htons(ETHERTYPE_ARP);
132 memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN);
133 memset(p.hdr.ether_dhost, 0xff, ETH_ALEN);
134
Rob Landleye3752e52005-05-03 03:28:55 +0000135 // arp request
Rob Landleyad8071f2005-04-30 03:49:37 +0000136 p.arp.ar_hrd = htons(ARPHRD_ETHER);
137 p.arp.ar_pro = htons(ETHERTYPE_IP);
138 p.arp.ar_hln = ETH_ALEN;
139 p.arp.ar_pln = 4;
140 p.arp.ar_op = htons(op);
141 memcpy(&p.source_addr, source_addr, ETH_ALEN);
142 memcpy(&p.source_ip, &source_ip, sizeof (p.source_ip));
143 memcpy(&p.target_addr, target_addr, ETH_ALEN);
144 memcpy(&p.target_ip, &target_ip, sizeof (p.target_ip));
145
Rob Landleye3752e52005-05-03 03:28:55 +0000146 // send it
Rob Landleyad8071f2005-04-30 03:49:37 +0000147 if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
Rob Landleye3752e52005-05-03 03:28:55 +0000148 perror("sendto");
Rob Landleyad8071f2005-04-30 03:49:37 +0000149 return -errno;
150 }
151 return 0;
152}
153
Rob Landleye3752e52005-05-03 03:28:55 +0000154/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000155 * Run a script.
156 */
157static int
158run(char *script, char *arg, char *intf, struct in_addr *ip)
159{
160 int pid, status;
161 char *why;
162
163 if (script != NULL) {
Rob Landleye3752e52005-05-03 03:28:55 +0000164 VDBG("%s run %s %s\n", intf, script, arg);
Rob Landleyad8071f2005-04-30 03:49:37 +0000165 if (ip != NULL) {
166 char *addr = inet_ntoa(*ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000167 setenv("ip", addr, 1);
Rob Landleyad8071f2005-04-30 03:49:37 +0000168 syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
169 }
170
171 pid = vfork();
Rob Landleye3752e52005-05-03 03:28:55 +0000172 if (pid < 0) { // error
Rob Landleyad8071f2005-04-30 03:49:37 +0000173 why = "vfork";
174 goto bad;
Rob Landleye3752e52005-05-03 03:28:55 +0000175 } else if (pid == 0) { // child
Rob Landleyad8071f2005-04-30 03:49:37 +0000176 execl(script, script, arg, NULL);
Rob Landleye3752e52005-05-03 03:28:55 +0000177 perror("execl");
178 _exit(EXIT_FAILURE);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000179 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000180
181 if (waitpid(pid, &status, 0) <= 0) {
182 why = "waitpid";
183 goto bad;
184 }
185 if (WEXITSTATUS(status) != 0) {
Rob Landleye3752e52005-05-03 03:28:55 +0000186 fprintf(stderr, "%s: script %s failed, exit=%d\n",
187 prog, script, WEXITSTATUS(status));
Rob Landleyad8071f2005-04-30 03:49:37 +0000188 return -errno;
189 }
190 }
191 return 0;
192bad:
193 status = -errno;
194 syslog(LOG_ERR, "%s %s, %s error: %s",
195 arg, intf, why, strerror(errno));
196 return status;
197}
198
Rob Landleye3752e52005-05-03 03:28:55 +0000199#ifndef NO_BUSYBOX
200#include "busybox.h"
201#endif
202
203/**
204 * Print usage information.
205 */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000206static void ATTRIBUTE_NORETURN
Bernhard Reutner-Fischerb7d53422005-12-12 10:21:35 +0000207zcip_usage(const char *msg)
Rob Landleye3752e52005-05-03 03:28:55 +0000208{
209 fprintf(stderr, "%s: %s\n", prog, msg);
210#ifdef NO_BUSYBOX
211 fprintf(stderr, "Usage: %s [OPTIONS] ifname script\n"
212 "\t-f foreground mode (implied by -v)\n"
213 "\t-q quit after address (no daemon)\n"
214 "\t-r 169.254.x.x request this address first\n"
215 "\t-v verbose; show version\n",
216 prog);
217 exit(0);
218#else
219 bb_show_usage();
220#endif
221}
222
223/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000224 * Return milliseconds of random delay, up to "secs" seconds.
225 */
226static inline unsigned
227ms_rdelay(unsigned secs)
228{
229 return lrand48() % (secs * 1000);
230}
231
Rob Landleye3752e52005-05-03 03:28:55 +0000232/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000233 * main program
234 */
Bernhard Reutner-Fischer38d66152005-10-21 10:43:11 +0000235
236#ifdef NO_BUSYBOX
Rob Landleye3752e52005-05-03 03:28:55 +0000237int
238main(int argc, char *argv[])
239 __attribute__ ((weak, alias ("zcip_main")));
Bernhard Reutner-Fischer38d66152005-10-21 10:43:11 +0000240#endif
Rob Landleyad8071f2005-04-30 03:49:37 +0000241
242int zcip_main(int argc, char *argv[])
243{
244 char *intf = NULL;
245 char *script = NULL;
Rob Landleye3752e52005-05-03 03:28:55 +0000246 int quit = 0;
247 int foreground = 0;
248
Rob Landleyad8071f2005-04-30 03:49:37 +0000249 char *why;
250 struct sockaddr saddr;
251 struct ether_addr addr;
252 struct in_addr ip = { 0 };
253 int fd;
254 int ready = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000255 suseconds_t timeout = 0; // milliseconds
Rob Landleyad8071f2005-04-30 03:49:37 +0000256 time_t defend = 0;
257 unsigned conflicts = 0;
258 unsigned nprobes = 0;
259 unsigned nclaims = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000260 int t;
Rob Landleyad8071f2005-04-30 03:49:37 +0000261
Rob Landleye3752e52005-05-03 03:28:55 +0000262 // parse commandline: prog [options] ifname script
263 prog = argv[0];
264 while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
265 switch (t) {
266 case 'f':
267 foreground = 1;
268 continue;
269 case 'q':
270 quit = 1;
271 continue;
272 case 'r':
273 if (inet_aton(optarg, &ip) == 0
274 || (ntohl(ip.s_addr) & IN_CLASSB_NET)
275 != LINKLOCAL_ADDR) {
Bernhard Reutner-Fischerb7d53422005-12-12 10:21:35 +0000276 zcip_usage("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000277 }
278 continue;
279 case 'v':
280 if (!verbose)
281 printf("%s: version %s\n", prog, ZCIP_VERSION);
282 verbose++;
283 foreground = 1;
284 continue;
285 default:
Bernhard Reutner-Fischerb7d53422005-12-12 10:21:35 +0000286 zcip_usage("bad option");
Rob Landleye3752e52005-05-03 03:28:55 +0000287 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000288 }
Rob Landleye3752e52005-05-03 03:28:55 +0000289 if (optind < argc - 1) {
290 intf = argv[optind++];
291 setenv("interface", intf, 1);
292 script = argv[optind++];
Rob Landleyad8071f2005-04-30 03:49:37 +0000293 }
Rob Landleye3752e52005-05-03 03:28:55 +0000294 if (optind != argc || !intf)
Bernhard Reutner-Fischerb7d53422005-12-12 10:21:35 +0000295 zcip_usage("wrong number of arguments");
Rob Landleye3752e52005-05-03 03:28:55 +0000296 openlog(prog, 0, LOG_DAEMON);
Rob Landleyad8071f2005-04-30 03:49:37 +0000297
Rob Landleye3752e52005-05-03 03:28:55 +0000298 // initialize the interface (modprobe, ifup, etc)
Rob Landleyad8071f2005-04-30 03:49:37 +0000299 if (run(script, "init", intf, NULL) < 0)
300 return EXIT_FAILURE;
301
Rob Landleye3752e52005-05-03 03:28:55 +0000302 // initialize saddr
Rob Landleyad8071f2005-04-30 03:49:37 +0000303 memset(&saddr, 0, sizeof (saddr));
304 strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
305
Rob Landleye3752e52005-05-03 03:28:55 +0000306 // open an ARP socket
Rob Landleyad8071f2005-04-30 03:49:37 +0000307 if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) {
308 why = "open";
309fail:
Rob Landleye3752e52005-05-03 03:28:55 +0000310 foreground = 1;
Rob Landleyad8071f2005-04-30 03:49:37 +0000311 goto bad;
312 }
Rob Landleye3752e52005-05-03 03:28:55 +0000313 // bind to the interface's ARP socket
Rob Landleyad8071f2005-04-30 03:49:37 +0000314 if (bind(fd, &saddr, sizeof (saddr)) < 0) {
315 why = "bind";
316 goto fail;
317 } else {
318 struct ifreq ifr;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000319 unsigned short seed[3];
Rob Landleyad8071f2005-04-30 03:49:37 +0000320
Rob Landleye3752e52005-05-03 03:28:55 +0000321 // get the interface's ethernet address
Rob Landleyad8071f2005-04-30 03:49:37 +0000322 memset(&ifr, 0, sizeof (ifr));
323 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
324 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
325 why = "get ethernet address";
326 goto fail;
327 }
328 memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
329
Rob Landleye3752e52005-05-03 03:28:55 +0000330 // start with some stable ip address, either a function of
331 // the hardware address or else the last address we used.
332 // NOTE: the sequence of addresses we try changes only
333 // depending on when we detect conflicts.
Rob Landleyad8071f2005-04-30 03:49:37 +0000334 memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
335 seed48(seed);
336 if (ip.s_addr == 0)
337 pick(&ip);
338 }
339
Rob Landleye3752e52005-05-03 03:28:55 +0000340 // FIXME cases to handle:
341 // - zcip already running!
342 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000343
Rob Landleye3752e52005-05-03 03:28:55 +0000344 // daemonize now; don't delay system startup
345 if (!foreground) {
346 if (daemon(0, verbose) < 0) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000347 why = "daemon";
348 goto bad;
349 }
350 syslog(LOG_INFO, "start, interface %s", intf);
351 }
352
Rob Landleye3752e52005-05-03 03:28:55 +0000353 // run the dynamic address negotiation protocol,
354 // restarting after address conflicts:
355 // - start with some address we want to try
356 // - short random delay
357 // - arp probes to see if another host else uses it
358 // - arp announcements that we're claiming it
359 // - use it
360 // - defend it, within limits
Rob Landleyad8071f2005-04-30 03:49:37 +0000361 while (1) {
362 struct pollfd fds[1];
363 struct timeval tv1;
364 struct arp_packet p;
365
366 fds[0].fd = fd;
367 fds[0].events = POLLIN;
368 fds[0].revents = 0;
369
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000370 // poll, being ready to adjust current timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000371 if (timeout > 0) {
372 gettimeofday(&tv1, NULL);
373 tv1.tv_usec += (timeout % 1000) * 1000;
374 while (tv1.tv_usec > 1000000) {
375 tv1.tv_usec -= 1000000;
376 tv1.tv_sec++;
377 }
378 tv1.tv_sec += timeout / 1000;
379 } else if (timeout == 0) {
380 timeout = ms_rdelay(PROBE_WAIT);
Rob Landleye3752e52005-05-03 03:28:55 +0000381 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
382 // make the kernel filter out all packets except
383 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000384 }
Rob Landleye3752e52005-05-03 03:28:55 +0000385 VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
386 timeout, intf, nprobes, nclaims);
Rob Landleyad8071f2005-04-30 03:49:37 +0000387 switch (poll(fds, 1, timeout)) {
388
Rob Landleye3752e52005-05-03 03:28:55 +0000389 // timeouts trigger protocol transitions
Rob Landleyad8071f2005-04-30 03:49:37 +0000390 case 0:
Rob Landleye3752e52005-05-03 03:28:55 +0000391 // probes
Rob Landleyad8071f2005-04-30 03:49:37 +0000392 if (nprobes < PROBE_NUM) {
393 nprobes++;
Rob Landleye3752e52005-05-03 03:28:55 +0000394 VDBG("probe/%d %s@%s\n",
395 nprobes, intf, inet_ntoa(ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000396 (void)arp(fd, &saddr, ARPOP_REQUEST,
397 &addr, null_ip,
398 &null_addr, ip);
399 if (nprobes < PROBE_NUM) {
400 timeout = PROBE_MIN * 1000;
401 timeout += ms_rdelay(PROBE_MAX
402 - PROBE_MIN);
403 } else
404 timeout = ANNOUNCE_WAIT * 1000;
405 }
Rob Landleye3752e52005-05-03 03:28:55 +0000406 // then announcements
Rob Landleyad8071f2005-04-30 03:49:37 +0000407 else if (nclaims < ANNOUNCE_NUM) {
408 nclaims++;
Rob Landleye3752e52005-05-03 03:28:55 +0000409 VDBG("announce/%d %s@%s\n",
410 nclaims, intf, inet_ntoa(ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000411 (void)arp(fd, &saddr, ARPOP_REQUEST,
412 &addr, ip,
413 &addr, ip);
414 if (nclaims < ANNOUNCE_NUM) {
415 timeout = ANNOUNCE_INTERVAL * 1000;
416 } else {
Rob Landleye3752e52005-05-03 03:28:55 +0000417 // link is ok to use earlier
Rob Landleyad8071f2005-04-30 03:49:37 +0000418 run(script, "config", intf, &ip);
419 ready = 1;
420 conflicts = 0;
421 timeout = -1;
422
Rob Landleye3752e52005-05-03 03:28:55 +0000423 // NOTE: all other exit paths
424 // should deconfig ...
425 if (quit)
Rob Landleyad8071f2005-04-30 03:49:37 +0000426 return EXIT_SUCCESS;
Rob Landleye3752e52005-05-03 03:28:55 +0000427 // FIXME update filters
Rob Landleyad8071f2005-04-30 03:49:37 +0000428 }
429 }
430 break;
431
Rob Landleye3752e52005-05-03 03:28:55 +0000432 // packets arriving
Rob Landleyad8071f2005-04-30 03:49:37 +0000433 case 1:
Rob Landleye3752e52005-05-03 03:28:55 +0000434 // maybe adjust timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000435 if (timeout > 0) {
436 struct timeval tv2;
Rob Landleye3752e52005-05-03 03:28:55 +0000437
Rob Landleyad8071f2005-04-30 03:49:37 +0000438 gettimeofday(&tv2, NULL);
439 if (timercmp(&tv1, &tv2, <)) {
440 timeout = -1;
441 } else {
442 timersub(&tv1, &tv2, &tv1);
443 timeout = 1000 * tv1.tv_sec
444 + tv1.tv_usec / 1000;
445 }
446 }
447 if ((fds[0].revents & POLLIN) == 0) {
448 if (fds[0].revents & POLLERR) {
Rob Landleye3752e52005-05-03 03:28:55 +0000449 // FIXME: links routinely go down;
450 // this shouldn't necessarily exit.
451 fprintf(stderr, "%s %s: poll error\n",
452 prog, intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000453 if (ready) {
Rob Landleye3752e52005-05-03 03:28:55 +0000454 run(script, "deconfig",
455 intf, &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000456 }
457 return EXIT_FAILURE;
458 }
459 continue;
460 }
Rob Landleye3752e52005-05-03 03:28:55 +0000461 // read ARP packet
Rob Landleyad8071f2005-04-30 03:49:37 +0000462 if (recv(fd, &p, sizeof (p), 0) < 0) {
463 why = "recv";
464 goto bad;
465 }
466 if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
467 continue;
Rob Landleye3752e52005-05-03 03:28:55 +0000468
469 VDBG("%s recv arp type=%d, op=%d,\n",
470 intf, ntohs(p.hdr.ether_type),
471 ntohs(p.arp.ar_op));
472 VDBG("\tsource=%s %s\n",
473 ether_ntoa(&p.source_addr),
474 inet_ntoa(p.source_ip));
475 VDBG("\ttarget=%s %s\n",
476 ether_ntoa(&p.target_addr),
477 inet_ntoa(p.target_ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000478 if (p.arp.ar_op != htons(ARPOP_REQUEST)
479 && p.arp.ar_op != htons(ARPOP_REPLY))
480 continue;
481
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000482 // some cases are always conflicts
Rob Landleyad8071f2005-04-30 03:49:37 +0000483 if ((p.source_ip.s_addr == ip.s_addr)
484 && (memcmp(&addr, &p.source_addr,
485 ETH_ALEN) != 0)) {
486collision:
Rob Landleye3752e52005-05-03 03:28:55 +0000487 VDBG("%s ARP conflict from %s\n", intf,
488 ether_ntoa(&p.source_addr));
Rob Landleyad8071f2005-04-30 03:49:37 +0000489 if (ready) {
490 time_t now = time(0);
491
492 if ((defend + DEFEND_INTERVAL)
493 < now) {
494 defend = now;
495 (void)arp(fd, &saddr,
496 ARPOP_REQUEST,
497 &addr, ip,
498 &addr, ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000499 VDBG("%s defend\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000500 timeout = -1;
501 continue;
502 }
503 defend = now;
504 ready = 0;
505 run(script, "deconfig", intf, &ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000506 // FIXME rm filters: setsockopt(fd,
507 // SO_DETACH_FILTER, ...)
Rob Landleyad8071f2005-04-30 03:49:37 +0000508 }
509 conflicts++;
510 if (conflicts >= MAX_CONFLICTS) {
Rob Landleye3752e52005-05-03 03:28:55 +0000511 VDBG("%s ratelimit\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000512 sleep(RATE_LIMIT_INTERVAL);
513 }
Rob Landleye3752e52005-05-03 03:28:55 +0000514 // restart the whole protocol
Rob Landleyad8071f2005-04-30 03:49:37 +0000515 pick(&ip);
516 timeout = 0;
517 nprobes = 0;
518 nclaims = 0;
519 }
Rob Landleye3752e52005-05-03 03:28:55 +0000520 // two hosts probing one address is a collision too
Rob Landleyad8071f2005-04-30 03:49:37 +0000521 else if (p.target_ip.s_addr == ip.s_addr
522 && nclaims == 0
523 && p.arp.ar_op == htons(ARPOP_REQUEST)
524 && memcmp(&addr, &p.target_addr,
525 ETH_ALEN) != 0) {
526 goto collision;
527 }
528 break;
529
530 default:
531 why = "poll";
532 goto bad;
533 }
534 }
535bad:
Rob Landleye3752e52005-05-03 03:28:55 +0000536 if (foreground)
537 perror(why);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000538 else
Rob Landleyad8071f2005-04-30 03:49:37 +0000539 syslog(LOG_ERR, "%s %s, %s error: %s",
Rob Landleye3752e52005-05-03 03:28:55 +0000540 prog, intf, why, strerror(errno));
Rob Landleyad8071f2005-04-30 03:49:37 +0000541 return EXIT_FAILURE;
542}