blob: 176bc01017cc0bfee661c9a1764df83484482dff [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.
Denis Vlasenko035aae52006-09-03 12:23:56 +0000135 * TODO: sort out stderr/syslog reporting.
Rob Landleyad8071f2005-04-30 03:49:37 +0000136 */
Rob Landley53433b32006-06-22 22:28:29 +0000137static int run(char *script, char *arg, char *intf, struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000138{
139 int pid, status;
140 char *why;
141
142 if (script != NULL) {
Rob Landleye3752e52005-05-03 03:28:55 +0000143 VDBG("%s run %s %s\n", intf, script, arg);
Rob Landleyad8071f2005-04-30 03:49:37 +0000144 if (ip != NULL) {
145 char *addr = inet_ntoa(*ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000146 setenv("ip", addr, 1);
Rob Landleyad8071f2005-04-30 03:49:37 +0000147 syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
148 }
149
150 pid = vfork();
Rob Landleye3752e52005-05-03 03:28:55 +0000151 if (pid < 0) { // error
Rob Landleyad8071f2005-04-30 03:49:37 +0000152 why = "vfork";
153 goto bad;
Rob Landleye3752e52005-05-03 03:28:55 +0000154 } else if (pid == 0) { // child
Rob Landleyad8071f2005-04-30 03:49:37 +0000155 execl(script, script, arg, NULL);
Rob Landleye3752e52005-05-03 03:28:55 +0000156 perror("execl");
157 _exit(EXIT_FAILURE);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000158 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000159
160 if (waitpid(pid, &status, 0) <= 0) {
161 why = "waitpid";
162 goto bad;
163 }
164 if (WEXITSTATUS(status) != 0) {
Rob Landley53433b32006-06-22 22:28:29 +0000165 bb_error_msg("script %s failed, exit=%d\n",
166 script, WEXITSTATUS(status));
Rob Landleyad8071f2005-04-30 03:49:37 +0000167 return -errno;
168 }
169 }
170 return 0;
171bad:
172 status = -errno;
173 syslog(LOG_ERR, "%s %s, %s error: %s",
174 arg, intf, why, strerror(errno));
175 return status;
176}
177
Rob Landleye3752e52005-05-03 03:28:55 +0000178
179/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000180 * Return milliseconds of random delay, up to "secs" seconds.
181 */
Rob Landley88621d72006-08-29 19:41:06 +0000182static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000183{
184 return lrand48() % (secs * 1000);
185}
186
Rob Landleye3752e52005-05-03 03:28:55 +0000187/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000188 * main program
189 */
Bernhard Reutner-Fischer38d66152005-10-21 10:43:11 +0000190
Rob Landleyad8071f2005-04-30 03:49:37 +0000191int zcip_main(int argc, char *argv[])
192{
193 char *intf = NULL;
194 char *script = NULL;
Rob Landleye3752e52005-05-03 03:28:55 +0000195 int quit = 0;
196 int foreground = 0;
197
Rob Landleyad8071f2005-04-30 03:49:37 +0000198 char *why;
199 struct sockaddr saddr;
200 struct ether_addr addr;
201 struct in_addr ip = { 0 };
202 int fd;
203 int ready = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000204 suseconds_t timeout = 0; // milliseconds
Rob Landleyad8071f2005-04-30 03:49:37 +0000205 unsigned conflicts = 0;
206 unsigned nprobes = 0;
207 unsigned nclaims = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000208 int t;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000209 int state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000210
Denis Vlasenko035aae52006-09-03 12:23:56 +0000211 struct ifreq ifr;
212 unsigned short seed[3];
213
Rob Landleye3752e52005-05-03 03:28:55 +0000214 // parse commandline: prog [options] ifname script
Rob Landleye3752e52005-05-03 03:28:55 +0000215 while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
216 switch (t) {
217 case 'f':
218 foreground = 1;
219 continue;
220 case 'q':
221 quit = 1;
222 continue;
223 case 'r':
224 if (inet_aton(optarg, &ip) == 0
225 || (ntohl(ip.s_addr) & IN_CLASSB_NET)
226 != LINKLOCAL_ADDR) {
Rob Landley53433b32006-06-22 22:28:29 +0000227 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000228 }
229 continue;
230 case 'v':
Rob Landleye3752e52005-05-03 03:28:55 +0000231 verbose++;
232 foreground = 1;
233 continue;
234 default:
Rob Landley53433b32006-06-22 22:28:29 +0000235 bb_error_msg_and_die("bad option");
Rob Landleye3752e52005-05-03 03:28:55 +0000236 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000237 }
Rob Landleye3752e52005-05-03 03:28:55 +0000238 if (optind < argc - 1) {
239 intf = argv[optind++];
240 setenv("interface", intf, 1);
241 script = argv[optind++];
Rob Landleyad8071f2005-04-30 03:49:37 +0000242 }
Rob Landleye3752e52005-05-03 03:28:55 +0000243 if (optind != argc || !intf)
Rob Landley53433b32006-06-22 22:28:29 +0000244 bb_show_usage();
245 openlog(bb_applet_name, 0, LOG_DAEMON);
Rob Landleyad8071f2005-04-30 03:49:37 +0000246
Rob Landleye3752e52005-05-03 03:28:55 +0000247 // initialize the interface (modprobe, ifup, etc)
Rob Landleyad8071f2005-04-30 03:49:37 +0000248 if (run(script, "init", intf, NULL) < 0)
249 return EXIT_FAILURE;
250
Rob Landleye3752e52005-05-03 03:28:55 +0000251 // initialize saddr
Rob Landleyad8071f2005-04-30 03:49:37 +0000252 memset(&saddr, 0, sizeof (saddr));
Rob Landley768945b2006-06-25 00:34:52 +0000253 safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000254
Rob Landleye3752e52005-05-03 03:28:55 +0000255 // open an ARP socket
Denis Vlasenko035aae52006-09-03 12:23:56 +0000256 fd = xsocket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
257 // bind to the interface's ARP socket
258 xbind(fd, &saddr, sizeof (saddr);
259
260 // get the interface's ethernet address
261 memset(&ifr, 0, sizeof (ifr));
262 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
263 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
Rob Landleye3752e52005-05-03 03:28:55 +0000264 foreground = 1;
Denis Vlasenko035aae52006-09-03 12:23:56 +0000265 why = "get ethernet address";
Rob Landleyad8071f2005-04-30 03:49:37 +0000266 goto bad;
267 }
Denis Vlasenko035aae52006-09-03 12:23:56 +0000268 memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000269
Denis Vlasenko035aae52006-09-03 12:23:56 +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.
274 memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
275 seed48(seed);
276 if (ip.s_addr == 0)
277 pick(&ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000278
Rob Landleye3752e52005-05-03 03:28:55 +0000279 // FIXME cases to handle:
280 // - zcip already running!
281 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000282
Rob Landleye3752e52005-05-03 03:28:55 +0000283 // daemonize now; don't delay system startup
284 if (!foreground) {
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000285 xdaemon(0, verbose);
Rob Landleyad8071f2005-04-30 03:49:37 +0000286 syslog(LOG_INFO, "start, interface %s", intf);
287 }
288
Rob Landleye3752e52005-05-03 03:28:55 +0000289 // run the dynamic address negotiation protocol,
290 // restarting after address conflicts:
291 // - start with some address we want to try
292 // - short random delay
293 // - arp probes to see if another host else uses it
294 // - arp announcements that we're claiming it
295 // - use it
296 // - defend it, within limits
Rob Landleyad8071f2005-04-30 03:49:37 +0000297 while (1) {
298 struct pollfd fds[1];
299 struct timeval tv1;
300 struct arp_packet p;
301
302 fds[0].fd = fd;
303 fds[0].events = POLLIN;
304 fds[0].revents = 0;
305
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000306 int source_ip_conflict = 0;
307 int target_ip_conflict = 0;
308
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000309 // poll, being ready to adjust current timeout
Rob Landley00c051e2006-06-30 14:05:19 +0000310 if (!timeout) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000311 timeout = ms_rdelay(PROBE_WAIT);
Rob Landleye3752e52005-05-03 03:28:55 +0000312 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
313 // make the kernel filter out all packets except
314 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000315 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000316 // set tv1 to the point in time when we timeout
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
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000329 // timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000330 case 0:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000331 VDBG("state = %d\n", state);
332 switch (state) {
333 case PROBE:
334 // timeouts in the PROBE state means no conflicting ARP packets
335 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000336 if (nprobes < PROBE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000337 nprobes++;
338 VDBG("probe/%d %s@%s\n",
339 nprobes, intf, inet_ntoa(ip));
340 (void)arp(fd, &saddr, ARPOP_REQUEST,
341 &addr, null_ip,
342 &null_addr, ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000343 timeout = PROBE_MIN * 1000;
344 timeout += ms_rdelay(PROBE_MAX
345 - PROBE_MIN);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000346 }
347 else {
348 // Switch to announce state.
349 state = ANNOUNCE;
350 nclaims = 0;
351 VDBG("announce/%d %s@%s\n",
352 nclaims, intf, inet_ntoa(ip));
353 (void)arp(fd, &saddr, ARPOP_REQUEST,
354 &addr, ip,
355 &addr, ip);
356 timeout = ANNOUNCE_INTERVAL * 1000;
357 }
358 break;
359 case RATE_LIMIT_PROBE:
360 // timeouts in the RATE_LIMIT_PROBE state means no conflicting ARP packets
361 // have been received, so we can move immediately to the announce state
362 state = ANNOUNCE;
363 nclaims = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000364 VDBG("announce/%d %s@%s\n",
365 nclaims, intf, inet_ntoa(ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000366 (void)arp(fd, &saddr, ARPOP_REQUEST,
367 &addr, ip,
368 &addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000369 timeout = ANNOUNCE_INTERVAL * 1000;
370 break;
371 case ANNOUNCE:
372 // timeouts in the ANNOUNCE state means no conflicting ARP packets
373 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000374 if (nclaims < ANNOUNCE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000375 nclaims++;
376 VDBG("announce/%d %s@%s\n",
377 nclaims, intf, inet_ntoa(ip));
378 (void)arp(fd, &saddr, ARPOP_REQUEST,
379 &addr, ip,
380 &addr, ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000381 timeout = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000382 }
383 else {
384 // Switch to monitor state.
385 state = MONITOR;
Rob Landleye3752e52005-05-03 03:28:55 +0000386 // link is ok to use earlier
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000387 // FIXME update filters
Rob Landleyad8071f2005-04-30 03:49:37 +0000388 run(script, "config", intf, &ip);
389 ready = 1;
390 conflicts = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000391 timeout = -1; // Never timeout in the monitor state.
Rob Landleyad8071f2005-04-30 03:49:37 +0000392
Rob Landleye3752e52005-05-03 03:28:55 +0000393 // NOTE: all other exit paths
394 // should deconfig ...
395 if (quit)
Rob Landleyad8071f2005-04-30 03:49:37 +0000396 return EXIT_SUCCESS;
Rob Landleyad8071f2005-04-30 03:49:37 +0000397 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000398 break;
399 case DEFEND:
400 // We won! No ARP replies, so just go back to monitor.
401 state = MONITOR;
402 timeout = -1;
403 conflicts = 0;
404 break;
405 default:
406 // Invalid, should never happen. Restart the whole protocol.
407 state = PROBE;
408 pick(&ip);
409 timeout = 0;
410 nprobes = 0;
411 nclaims = 0;
412 break;
413 } // switch (state)
414 break; // case 0 (timeout)
Rob Landleye3752e52005-05-03 03:28:55 +0000415 // packets arriving
Rob Landleyad8071f2005-04-30 03:49:37 +0000416 case 1:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000417 // We need to adjust the timeout in case we didn't receive
418 // a conflicting packet.
Rob Landleyad8071f2005-04-30 03:49:37 +0000419 if (timeout > 0) {
420 struct timeval tv2;
Rob Landleye3752e52005-05-03 03:28:55 +0000421
Rob Landleyad8071f2005-04-30 03:49:37 +0000422 gettimeofday(&tv2, NULL);
423 if (timercmp(&tv1, &tv2, <)) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000424 // Current time is greater than the expected timeout time.
425 // Should never happen.
426 VDBG("missed an expected timeout\n");
Paul Foxef81ce62006-03-29 23:01:33 +0000427 timeout = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000428 } else {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000429 VDBG("adjusting timeout\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000430 timersub(&tv1, &tv2, &tv1);
431 timeout = 1000 * tv1.tv_sec
432 + tv1.tv_usec / 1000;
433 }
434 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000435
Rob Landleyad8071f2005-04-30 03:49:37 +0000436 if ((fds[0].revents & POLLIN) == 0) {
437 if (fds[0].revents & POLLERR) {
Rob Landleye3752e52005-05-03 03:28:55 +0000438 // FIXME: links routinely go down;
439 // this shouldn't necessarily exit.
Rob Landley53433b32006-06-22 22:28:29 +0000440 bb_error_msg("%s: poll error\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000441 if (ready) {
Rob Landleye3752e52005-05-03 03:28:55 +0000442 run(script, "deconfig",
443 intf, &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000444 }
445 return EXIT_FAILURE;
446 }
447 continue;
448 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000449
Rob Landleye3752e52005-05-03 03:28:55 +0000450 // read ARP packet
Rob Landleyad8071f2005-04-30 03:49:37 +0000451 if (recv(fd, &p, sizeof (p), 0) < 0) {
452 why = "recv";
453 goto bad;
454 }
455 if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
456 continue;
Rob Landleye3752e52005-05-03 03:28:55 +0000457
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000458#ifdef DEBUG
459 {
460 struct ether_addr * sha = (struct ether_addr *) p.arp.arp_sha;
461 struct ether_addr * tha = (struct ether_addr *) p.arp.arp_tha;
462 struct in_addr * spa = (struct in_addr *) p.arp.arp_spa;
463 struct in_addr * tpa = (struct in_addr *) p.arp.arp_tpa;
464 VDBG("%s recv arp type=%d, op=%d,\n",
Rob Landleye3752e52005-05-03 03:28:55 +0000465 intf, ntohs(p.hdr.ether_type),
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000466 ntohs(p.arp.arp_op));
467 VDBG("\tsource=%s %s\n",
468 ether_ntoa(sha),
469 inet_ntoa(*spa));
470 VDBG("\ttarget=%s %s\n",
471 ether_ntoa(tha),
472 inet_ntoa(*tpa));
473 }
474#endif
475 if (p.arp.arp_op != htons(ARPOP_REQUEST)
476 && p.arp.arp_op != htons(ARPOP_REPLY))
Rob Landleyad8071f2005-04-30 03:49:37 +0000477 continue;
478
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000479 if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
480 memcmp(&addr, &p.arp.arp_sha, ETH_ALEN) != 0) {
481 source_ip_conflict = 1;
482 }
483 if (memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
484 p.arp.arp_op == htons(ARPOP_REQUEST) &&
485 memcmp(&addr, &p.arp.arp_tha, ETH_ALEN) != 0) {
486 target_ip_conflict = 1;
487 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000488
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000489 VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
490 state, source_ip_conflict, target_ip_conflict);
491 switch (state) {
492 case PROBE:
493 case ANNOUNCE:
494 // When probing or announcing, check for source IP conflicts
495 // and other hosts doing ARP probes (target IP conflicts).
496 if (source_ip_conflict || target_ip_conflict) {
497 conflicts++;
498 if (conflicts >= MAX_CONFLICTS) {
499 VDBG("%s ratelimit\n", intf);
500 timeout = RATE_LIMIT_INTERVAL * 1000;
501 state = RATE_LIMIT_PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000502 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000503
504 // restart the whole protocol
505 pick(&ip);
506 timeout = 0;
507 nprobes = 0;
508 nclaims = 0;
509 }
510 break;
511 case MONITOR:
512 // If a conflict, we try to defend with a single ARP probe.
513 if (source_ip_conflict) {
514 VDBG("monitor conflict -- defending\n");
515 state = DEFEND;
516 timeout = DEFEND_INTERVAL * 1000;
517 (void)arp(fd, &saddr,
518 ARPOP_REQUEST,
519 &addr, ip,
520 &addr, ip);
521 }
522 break;
523 case DEFEND:
524 // Well, we tried. Start over (on conflict).
525 if (source_ip_conflict) {
526 state = PROBE;
527 VDBG("defend conflict -- starting over\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000528 ready = 0;
529 run(script, "deconfig", intf, &ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000530
531 // restart the whole protocol
532 pick(&ip);
533 timeout = 0;
534 nprobes = 0;
535 nclaims = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000536 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000537 break;
538 default:
539 // Invalid, should never happen. Restart the whole protocol.
540 VDBG("invalid state -- starting over\n");
541 state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000542 pick(&ip);
543 timeout = 0;
544 nprobes = 0;
545 nclaims = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000546 break;
547 } // switch state
Rob Landleyad8071f2005-04-30 03:49:37 +0000548
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000549 break; // case 1 (packets arriving)
Rob Landleyad8071f2005-04-30 03:49:37 +0000550 default:
551 why = "poll";
552 goto bad;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000553 } // switch poll
Rob Landleyad8071f2005-04-30 03:49:37 +0000554 }
555bad:
Rob Landleye3752e52005-05-03 03:28:55 +0000556 if (foreground)
557 perror(why);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000558 else
Rob Landleyad8071f2005-04-30 03:49:37 +0000559 syslog(LOG_ERR, "%s %s, %s error: %s",
Rob Landley53433b32006-06-22 22:28:29 +0000560 bb_applet_name, intf, why, strerror(errno));
Rob Landleyad8071f2005-04-30 03:49:37 +0000561 return EXIT_FAILURE;
562}