blob: 8e6d8792d3d3b88185de21c032c9cd442d8bbfab [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
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000018//#define DEBUG
Rob Landleye3752e52005-05-03 03:28:55 +000019
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;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000046 struct ether_arp arp;
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000047} ATTRIBUTE_PACKED;
Rob Landleyad8071f2005-04-30 03:49:37 +000048
Rob Landleybc68cd12006-03-10 19:22:06 +000049enum {
Rob Landleyad8071f2005-04-30 03:49:37 +000050/* 169.254.0.0 */
Rob Landleybc68cd12006-03-10 19:22:06 +000051 LINKLOCAL_ADDR = 0xa9fe0000,
Rob Landleyad8071f2005-04-30 03:49:37 +000052
53/* protocol timeout parameters, specified in seconds */
Rob Landleybc68cd12006-03-10 19:22:06 +000054 PROBE_WAIT = 1,
55 PROBE_MIN = 1,
56 PROBE_MAX = 2,
57 PROBE_NUM = 3,
58 MAX_CONFLICTS = 10,
59 RATE_LIMIT_INTERVAL = 60,
60 ANNOUNCE_WAIT = 2,
61 ANNOUNCE_NUM = 2,
62 ANNOUNCE_INTERVAL = 2,
63 DEFEND_INTERVAL = 10
64};
Rob Landleyad8071f2005-04-30 03:49:37 +000065
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000066/* States during the configuration process. */
67enum {
68 PROBE = 0,
69 RATE_LIMIT_PROBE,
70 ANNOUNCE,
71 MONITOR,
72 DEFEND
73};
Rob Landleyad8071f2005-04-30 03:49:37 +000074
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000075/* Implicitly zero-initialized */
76static const struct in_addr null_ip;
77static const struct ether_addr null_addr;
78static int verbose;
Rob Landleyad8071f2005-04-30 03:49:37 +000079
Rob Landleye3752e52005-05-03 03:28:55 +000080#define DBG(fmt,args...) \
81 do { } while (0)
82#define VDBG DBG
Rob Landleye3752e52005-05-03 03:28:55 +000083
84/**
Rob Landleyad8071f2005-04-30 03:49:37 +000085 * Pick a random link local IP address on 169.254/16, except that
86 * the first and last 256 addresses are reserved.
87 */
Rob Landley53433b32006-06-22 22:28:29 +000088static void pick(struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +000089{
90 unsigned tmp;
91
92 /* use cheaper math than lrand48() mod N */
93 do {
94 tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
95 } while (tmp > (IN_CLASSB_HOST - 0x0200));
96 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
97}
98
Rob Landleye3752e52005-05-03 03:28:55 +000099/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000100 * Broadcast an ARP packet.
101 */
Rob Landley53433b32006-06-22 22:28:29 +0000102static int arp(int fd, struct sockaddr *saddr, int op,
Rob Landleyad8071f2005-04-30 03:49:37 +0000103 const struct ether_addr *source_addr, struct in_addr source_ip,
104 const struct ether_addr *target_addr, struct in_addr target_ip)
105{
106 struct arp_packet p;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000107 memset(&p, 0, sizeof(p));
Rob Landleyad8071f2005-04-30 03:49:37 +0000108
Rob Landleye3752e52005-05-03 03:28:55 +0000109 // ether header
Rob Landleyad8071f2005-04-30 03:49:37 +0000110 p.hdr.ether_type = htons(ETHERTYPE_ARP);
111 memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN);
112 memset(p.hdr.ether_dhost, 0xff, ETH_ALEN);
113
Rob Landleye3752e52005-05-03 03:28:55 +0000114 // arp request
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000115 p.arp.arp_hrd = htons(ARPHRD_ETHER);
116 p.arp.arp_pro = htons(ETHERTYPE_IP);
117 p.arp.arp_hln = ETH_ALEN;
118 p.arp.arp_pln = 4;
119 p.arp.arp_op = htons(op);
120 memcpy(&p.arp.arp_sha, source_addr, ETH_ALEN);
121 memcpy(&p.arp.arp_spa, &source_ip, sizeof (p.arp.arp_spa));
122 memcpy(&p.arp.arp_tha, target_addr, ETH_ALEN);
123 memcpy(&p.arp.arp_tpa, &target_ip, sizeof (p.arp.arp_tpa));
Rob Landleyad8071f2005-04-30 03:49:37 +0000124
Rob Landleye3752e52005-05-03 03:28:55 +0000125 // send it
Rob Landleyad8071f2005-04-30 03:49:37 +0000126 if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
Rob Landleye3752e52005-05-03 03:28:55 +0000127 perror("sendto");
Rob Landleyad8071f2005-04-30 03:49:37 +0000128 return -errno;
129 }
130 return 0;
131}
132
Rob Landleye3752e52005-05-03 03:28:55 +0000133/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000134 * Run a script.
135 */
Rob Landley53433b32006-06-22 22:28:29 +0000136static int run(char *script, char *arg, char *intf, struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000137{
138 int pid, status;
139 char *why;
140
141 if (script != NULL) {
Rob Landleye3752e52005-05-03 03:28:55 +0000142 VDBG("%s run %s %s\n", intf, script, arg);
Rob Landleyad8071f2005-04-30 03:49:37 +0000143 if (ip != NULL) {
144 char *addr = inet_ntoa(*ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000145 setenv("ip", addr, 1);
Rob Landleyad8071f2005-04-30 03:49:37 +0000146 syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
147 }
148
149 pid = vfork();
Rob Landleye3752e52005-05-03 03:28:55 +0000150 if (pid < 0) { // error
Rob Landleyad8071f2005-04-30 03:49:37 +0000151 why = "vfork";
152 goto bad;
Rob Landleye3752e52005-05-03 03:28:55 +0000153 } else if (pid == 0) { // child
Rob Landleyad8071f2005-04-30 03:49:37 +0000154 execl(script, script, arg, NULL);
Rob Landleye3752e52005-05-03 03:28:55 +0000155 perror("execl");
156 _exit(EXIT_FAILURE);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000157 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000158
159 if (waitpid(pid, &status, 0) <= 0) {
160 why = "waitpid";
161 goto bad;
162 }
163 if (WEXITSTATUS(status) != 0) {
Rob Landley53433b32006-06-22 22:28:29 +0000164 bb_error_msg("script %s failed, exit=%d\n",
165 script, WEXITSTATUS(status));
Rob Landleyad8071f2005-04-30 03:49:37 +0000166 return -errno;
167 }
168 }
169 return 0;
170bad:
171 status = -errno;
172 syslog(LOG_ERR, "%s %s, %s error: %s",
173 arg, intf, why, strerror(errno));
174 return status;
175}
176
Rob Landleye3752e52005-05-03 03:28:55 +0000177
178/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000179 * Return milliseconds of random delay, up to "secs" seconds.
180 */
Rob Landley88621d72006-08-29 19:41:06 +0000181static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000182{
183 return lrand48() % (secs * 1000);
184}
185
Rob Landleye3752e52005-05-03 03:28:55 +0000186/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000187 * main program
188 */
Bernhard Reutner-Fischer38d66152005-10-21 10:43:11 +0000189
Rob Landleyad8071f2005-04-30 03:49:37 +0000190int zcip_main(int argc, char *argv[])
191{
192 char *intf = NULL;
193 char *script = NULL;
Rob Landleye3752e52005-05-03 03:28:55 +0000194 int quit = 0;
195 int foreground = 0;
196
Rob Landleyad8071f2005-04-30 03:49:37 +0000197 char *why;
198 struct sockaddr saddr;
199 struct ether_addr addr;
200 struct in_addr ip = { 0 };
201 int fd;
202 int ready = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000203 suseconds_t timeout = 0; // milliseconds
Rob Landleyad8071f2005-04-30 03:49:37 +0000204 unsigned conflicts = 0;
205 unsigned nprobes = 0;
206 unsigned nclaims = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000207 int t;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000208 int state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000209
Rob Landleye3752e52005-05-03 03:28:55 +0000210 // parse commandline: prog [options] ifname script
Rob Landleye3752e52005-05-03 03:28:55 +0000211 while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
212 switch (t) {
213 case 'f':
214 foreground = 1;
215 continue;
216 case 'q':
217 quit = 1;
218 continue;
219 case 'r':
220 if (inet_aton(optarg, &ip) == 0
221 || (ntohl(ip.s_addr) & IN_CLASSB_NET)
222 != LINKLOCAL_ADDR) {
Rob Landley53433b32006-06-22 22:28:29 +0000223 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000224 }
225 continue;
226 case 'v':
Rob Landleye3752e52005-05-03 03:28:55 +0000227 verbose++;
228 foreground = 1;
229 continue;
230 default:
Rob Landley53433b32006-06-22 22:28:29 +0000231 bb_error_msg_and_die("bad option");
Rob Landleye3752e52005-05-03 03:28:55 +0000232 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000233 }
Rob Landleye3752e52005-05-03 03:28:55 +0000234 if (optind < argc - 1) {
235 intf = argv[optind++];
236 setenv("interface", intf, 1);
237 script = argv[optind++];
Rob Landleyad8071f2005-04-30 03:49:37 +0000238 }
Rob Landleye3752e52005-05-03 03:28:55 +0000239 if (optind != argc || !intf)
Rob Landley53433b32006-06-22 22:28:29 +0000240 bb_show_usage();
241 openlog(bb_applet_name, 0, LOG_DAEMON);
Rob Landleyad8071f2005-04-30 03:49:37 +0000242
Rob Landleye3752e52005-05-03 03:28:55 +0000243 // initialize the interface (modprobe, ifup, etc)
Rob Landleyad8071f2005-04-30 03:49:37 +0000244 if (run(script, "init", intf, NULL) < 0)
245 return EXIT_FAILURE;
246
Rob Landleye3752e52005-05-03 03:28:55 +0000247 // initialize saddr
Rob Landleyad8071f2005-04-30 03:49:37 +0000248 memset(&saddr, 0, sizeof (saddr));
Rob Landley768945b2006-06-25 00:34:52 +0000249 safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000250
Rob Landleye3752e52005-05-03 03:28:55 +0000251 // open an ARP socket
Rob Landleyad8071f2005-04-30 03:49:37 +0000252 if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) {
253 why = "open";
254fail:
Rob Landleye3752e52005-05-03 03:28:55 +0000255 foreground = 1;
Rob Landleyad8071f2005-04-30 03:49:37 +0000256 goto bad;
257 }
Rob Landleye3752e52005-05-03 03:28:55 +0000258 // bind to the interface's ARP socket
Rob Landleyad8071f2005-04-30 03:49:37 +0000259 if (bind(fd, &saddr, sizeof (saddr)) < 0) {
260 why = "bind";
261 goto fail;
262 } else {
263 struct ifreq ifr;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000264 unsigned short seed[3];
Rob Landleyad8071f2005-04-30 03:49:37 +0000265
Rob Landleye3752e52005-05-03 03:28:55 +0000266 // get the interface's ethernet address
Rob Landleyad8071f2005-04-30 03:49:37 +0000267 memset(&ifr, 0, sizeof (ifr));
268 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
269 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
270 why = "get ethernet address";
271 goto fail;
272 }
273 memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
274
Rob Landleye3752e52005-05-03 03:28:55 +0000275 // start with some stable ip address, either a function of
276 // the hardware address or else the last address we used.
277 // NOTE: the sequence of addresses we try changes only
278 // depending on when we detect conflicts.
Rob Landleyad8071f2005-04-30 03:49:37 +0000279 memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
280 seed48(seed);
281 if (ip.s_addr == 0)
282 pick(&ip);
283 }
284
Rob Landleye3752e52005-05-03 03:28:55 +0000285 // FIXME cases to handle:
286 // - zcip already running!
287 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000288
Rob Landleye3752e52005-05-03 03:28:55 +0000289 // daemonize now; don't delay system startup
290 if (!foreground) {
291 if (daemon(0, verbose) < 0) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000292 why = "daemon";
293 goto bad;
294 }
295 syslog(LOG_INFO, "start, interface %s", intf);
296 }
297
Rob Landleye3752e52005-05-03 03:28:55 +0000298 // run the dynamic address negotiation protocol,
299 // restarting after address conflicts:
300 // - start with some address we want to try
301 // - short random delay
302 // - arp probes to see if another host else uses it
303 // - arp announcements that we're claiming it
304 // - use it
305 // - defend it, within limits
Rob Landleyad8071f2005-04-30 03:49:37 +0000306 while (1) {
307 struct pollfd fds[1];
308 struct timeval tv1;
309 struct arp_packet p;
310
311 fds[0].fd = fd;
312 fds[0].events = POLLIN;
313 fds[0].revents = 0;
314
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000315 int source_ip_conflict = 0;
316 int target_ip_conflict = 0;
317
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000318 // poll, being ready to adjust current timeout
Rob Landley00c051e2006-06-30 14:05:19 +0000319 if (!timeout) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000320 timeout = ms_rdelay(PROBE_WAIT);
Rob Landleye3752e52005-05-03 03:28:55 +0000321 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
322 // make the kernel filter out all packets except
323 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000324 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000325 // set tv1 to the point in time when we timeout
Rob Landley00c051e2006-06-30 14:05:19 +0000326 gettimeofday(&tv1, NULL);
327 tv1.tv_usec += (timeout % 1000) * 1000;
328 while (tv1.tv_usec > 1000000) {
329 tv1.tv_usec -= 1000000;
330 tv1.tv_sec++;
331 }
332 tv1.tv_sec += timeout / 1000;
333
Rob Landleye3752e52005-05-03 03:28:55 +0000334 VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
335 timeout, intf, nprobes, nclaims);
Rob Landleyad8071f2005-04-30 03:49:37 +0000336 switch (poll(fds, 1, timeout)) {
337
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000338 // timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000339 case 0:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000340 VDBG("state = %d\n", state);
341 switch (state) {
342 case PROBE:
343 // timeouts in the PROBE state means no conflicting ARP packets
344 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000345 if (nprobes < PROBE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000346 nprobes++;
347 VDBG("probe/%d %s@%s\n",
348 nprobes, intf, inet_ntoa(ip));
349 (void)arp(fd, &saddr, ARPOP_REQUEST,
350 &addr, null_ip,
351 &null_addr, ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000352 timeout = PROBE_MIN * 1000;
353 timeout += ms_rdelay(PROBE_MAX
354 - PROBE_MIN);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000355 }
356 else {
357 // Switch to announce state.
358 state = ANNOUNCE;
359 nclaims = 0;
360 VDBG("announce/%d %s@%s\n",
361 nclaims, intf, inet_ntoa(ip));
362 (void)arp(fd, &saddr, ARPOP_REQUEST,
363 &addr, ip,
364 &addr, ip);
365 timeout = ANNOUNCE_INTERVAL * 1000;
366 }
367 break;
368 case RATE_LIMIT_PROBE:
369 // timeouts in the RATE_LIMIT_PROBE state means no conflicting ARP packets
370 // have been received, so we can move immediately to the announce state
371 state = ANNOUNCE;
372 nclaims = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000373 VDBG("announce/%d %s@%s\n",
374 nclaims, intf, inet_ntoa(ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000375 (void)arp(fd, &saddr, ARPOP_REQUEST,
376 &addr, ip,
377 &addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000378 timeout = ANNOUNCE_INTERVAL * 1000;
379 break;
380 case ANNOUNCE:
381 // timeouts in the ANNOUNCE state means no conflicting ARP packets
382 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000383 if (nclaims < ANNOUNCE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000384 nclaims++;
385 VDBG("announce/%d %s@%s\n",
386 nclaims, intf, inet_ntoa(ip));
387 (void)arp(fd, &saddr, ARPOP_REQUEST,
388 &addr, ip,
389 &addr, ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000390 timeout = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000391 }
392 else {
393 // Switch to monitor state.
394 state = MONITOR;
Rob Landleye3752e52005-05-03 03:28:55 +0000395 // link is ok to use earlier
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000396 // FIXME update filters
Rob Landleyad8071f2005-04-30 03:49:37 +0000397 run(script, "config", intf, &ip);
398 ready = 1;
399 conflicts = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000400 timeout = -1; // Never timeout in the monitor state.
Rob Landleyad8071f2005-04-30 03:49:37 +0000401
Rob Landleye3752e52005-05-03 03:28:55 +0000402 // NOTE: all other exit paths
403 // should deconfig ...
404 if (quit)
Rob Landleyad8071f2005-04-30 03:49:37 +0000405 return EXIT_SUCCESS;
Rob Landleyad8071f2005-04-30 03:49:37 +0000406 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000407 break;
408 case DEFEND:
409 // We won! No ARP replies, so just go back to monitor.
410 state = MONITOR;
411 timeout = -1;
412 conflicts = 0;
413 break;
414 default:
415 // Invalid, should never happen. Restart the whole protocol.
416 state = PROBE;
417 pick(&ip);
418 timeout = 0;
419 nprobes = 0;
420 nclaims = 0;
421 break;
422 } // switch (state)
423 break; // case 0 (timeout)
Rob Landleye3752e52005-05-03 03:28:55 +0000424 // packets arriving
Rob Landleyad8071f2005-04-30 03:49:37 +0000425 case 1:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000426 // We need to adjust the timeout in case we didn't receive
427 // a conflicting packet.
Rob Landleyad8071f2005-04-30 03:49:37 +0000428 if (timeout > 0) {
429 struct timeval tv2;
Rob Landleye3752e52005-05-03 03:28:55 +0000430
Rob Landleyad8071f2005-04-30 03:49:37 +0000431 gettimeofday(&tv2, NULL);
432 if (timercmp(&tv1, &tv2, <)) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000433 // Current time is greater than the expected timeout time.
434 // Should never happen.
435 VDBG("missed an expected timeout\n");
Paul Foxef81ce62006-03-29 23:01:33 +0000436 timeout = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000437 } else {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000438 VDBG("adjusting timeout\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000439 timersub(&tv1, &tv2, &tv1);
440 timeout = 1000 * tv1.tv_sec
441 + tv1.tv_usec / 1000;
442 }
443 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000444
Rob Landleyad8071f2005-04-30 03:49:37 +0000445 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.
Rob Landley53433b32006-06-22 22:28:29 +0000449 bb_error_msg("%s: poll error\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000450 if (ready) {
Rob Landleye3752e52005-05-03 03:28:55 +0000451 run(script, "deconfig",
452 intf, &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000453 }
454 return EXIT_FAILURE;
455 }
456 continue;
457 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000458
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
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000467#ifdef DEBUG
468 {
469 struct ether_addr * sha = (struct ether_addr *) p.arp.arp_sha;
470 struct ether_addr * tha = (struct ether_addr *) p.arp.arp_tha;
471 struct in_addr * spa = (struct in_addr *) p.arp.arp_spa;
472 struct in_addr * tpa = (struct in_addr *) p.arp.arp_tpa;
473 VDBG("%s recv arp type=%d, op=%d,\n",
Rob Landleye3752e52005-05-03 03:28:55 +0000474 intf, ntohs(p.hdr.ether_type),
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000475 ntohs(p.arp.arp_op));
476 VDBG("\tsource=%s %s\n",
477 ether_ntoa(sha),
478 inet_ntoa(*spa));
479 VDBG("\ttarget=%s %s\n",
480 ether_ntoa(tha),
481 inet_ntoa(*tpa));
482 }
483#endif
484 if (p.arp.arp_op != htons(ARPOP_REQUEST)
485 && p.arp.arp_op != htons(ARPOP_REPLY))
Rob Landleyad8071f2005-04-30 03:49:37 +0000486 continue;
487
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000488 if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
489 memcmp(&addr, &p.arp.arp_sha, ETH_ALEN) != 0) {
490 source_ip_conflict = 1;
491 }
492 if (memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
493 p.arp.arp_op == htons(ARPOP_REQUEST) &&
494 memcmp(&addr, &p.arp.arp_tha, ETH_ALEN) != 0) {
495 target_ip_conflict = 1;
496 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000497
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000498 VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
499 state, source_ip_conflict, target_ip_conflict);
500 switch (state) {
501 case PROBE:
502 case ANNOUNCE:
503 // When probing or announcing, check for source IP conflicts
504 // and other hosts doing ARP probes (target IP conflicts).
505 if (source_ip_conflict || target_ip_conflict) {
506 conflicts++;
507 if (conflicts >= MAX_CONFLICTS) {
508 VDBG("%s ratelimit\n", intf);
509 timeout = RATE_LIMIT_INTERVAL * 1000;
510 state = RATE_LIMIT_PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000511 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000512
513 // restart the whole protocol
514 pick(&ip);
515 timeout = 0;
516 nprobes = 0;
517 nclaims = 0;
518 }
519 break;
520 case MONITOR:
521 // If a conflict, we try to defend with a single ARP probe.
522 if (source_ip_conflict) {
523 VDBG("monitor conflict -- defending\n");
524 state = DEFEND;
525 timeout = DEFEND_INTERVAL * 1000;
526 (void)arp(fd, &saddr,
527 ARPOP_REQUEST,
528 &addr, ip,
529 &addr, ip);
530 }
531 break;
532 case DEFEND:
533 // Well, we tried. Start over (on conflict).
534 if (source_ip_conflict) {
535 state = PROBE;
536 VDBG("defend conflict -- starting over\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000537 ready = 0;
538 run(script, "deconfig", intf, &ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000539
540 // restart the whole protocol
541 pick(&ip);
542 timeout = 0;
543 nprobes = 0;
544 nclaims = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000545 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000546 break;
547 default:
548 // Invalid, should never happen. Restart the whole protocol.
549 VDBG("invalid state -- starting over\n");
550 state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000551 pick(&ip);
552 timeout = 0;
553 nprobes = 0;
554 nclaims = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000555 break;
556 } // switch state
Rob Landleyad8071f2005-04-30 03:49:37 +0000557
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000558 break; // case 1 (packets arriving)
Rob Landleyad8071f2005-04-30 03:49:37 +0000559 default:
560 why = "poll";
561 goto bad;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000562 } // switch poll
Rob Landleyad8071f2005-04-30 03:49:37 +0000563 }
564bad:
Rob Landleye3752e52005-05-03 03:28:55 +0000565 if (foreground)
566 perror(why);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000567 else
Rob Landleyad8071f2005-04-30 03:49:37 +0000568 syslog(LOG_ERR, "%s %s, %s error: %s",
Rob Landley53433b32006-06-22 22:28:29 +0000569 bb_applet_name, intf, why, strerror(errno));
Rob Landleyad8071f2005-04-30 03:49:37 +0000570 return EXIT_FAILURE;
571}