blob: e781a5882e6bb7c82e01c89a9ae95979602012bc [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
Rob Landleye3752e52005-05-03 03:28:55 +000075#define DBG(fmt,args...) \
76 do { } while (0)
77#define VDBG DBG
Rob Landleye3752e52005-05-03 03:28:55 +000078
79/**
Rob Landleyad8071f2005-04-30 03:49:37 +000080 * Pick a random link local IP address on 169.254/16, except that
81 * the first and last 256 addresses are reserved.
82 */
Rob Landley53433b32006-06-22 22:28:29 +000083static void pick(struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +000084{
85 unsigned tmp;
86
87 /* use cheaper math than lrand48() mod N */
88 do {
89 tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
90 } while (tmp > (IN_CLASSB_HOST - 0x0200));
91 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
92}
93
Rob Landleye3752e52005-05-03 03:28:55 +000094/**
Rob Landleyad8071f2005-04-30 03:49:37 +000095 * Broadcast an ARP packet.
96 */
Rob Landley53433b32006-06-22 22:28:29 +000097static int arp(int fd, struct sockaddr *saddr, int op,
Rob Landleyad8071f2005-04-30 03:49:37 +000098 const struct ether_addr *source_addr, struct in_addr source_ip,
99 const struct ether_addr *target_addr, struct in_addr target_ip)
100{
101 struct arp_packet p;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000102 memset(&p, 0, sizeof(p));
Rob Landleyad8071f2005-04-30 03:49:37 +0000103
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
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000110 p.arp.arp_hrd = htons(ARPHRD_ETHER);
111 p.arp.arp_pro = htons(ETHERTYPE_IP);
112 p.arp.arp_hln = ETH_ALEN;
113 p.arp.arp_pln = 4;
114 p.arp.arp_op = htons(op);
115 memcpy(&p.arp.arp_sha, source_addr, ETH_ALEN);
116 memcpy(&p.arp.arp_spa, &source_ip, sizeof (p.arp.arp_spa));
117 memcpy(&p.arp.arp_tha, target_addr, ETH_ALEN);
118 memcpy(&p.arp.arp_tpa, &target_ip, sizeof (p.arp.arp_tpa));
Rob Landleyad8071f2005-04-30 03:49:37 +0000119
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.
Denis Vlasenko035aae52006-09-03 12:23:56 +0000130 * TODO: sort out stderr/syslog reporting.
Rob Landleyad8071f2005-04-30 03:49:37 +0000131 */
Rob Landley53433b32006-06-22 22:28:29 +0000132static int run(char *script, char *arg, char *intf, struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000133{
134 int pid, status;
135 char *why;
136
137 if (script != NULL) {
Rob Landleye3752e52005-05-03 03:28:55 +0000138 VDBG("%s run %s %s\n", intf, script, arg);
Rob Landleyad8071f2005-04-30 03:49:37 +0000139 if (ip != NULL) {
140 char *addr = inet_ntoa(*ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000141 setenv("ip", addr, 1);
Rob Landleyad8071f2005-04-30 03:49:37 +0000142 syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
143 }
144
145 pid = vfork();
Rob Landleye3752e52005-05-03 03:28:55 +0000146 if (pid < 0) { // error
Rob Landleyad8071f2005-04-30 03:49:37 +0000147 why = "vfork";
148 goto bad;
Rob Landleye3752e52005-05-03 03:28:55 +0000149 } else if (pid == 0) { // child
Rob Landleyad8071f2005-04-30 03:49:37 +0000150 execl(script, script, arg, NULL);
Rob Landleye3752e52005-05-03 03:28:55 +0000151 perror("execl");
152 _exit(EXIT_FAILURE);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000153 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000154
155 if (waitpid(pid, &status, 0) <= 0) {
156 why = "waitpid";
157 goto bad;
158 }
159 if (WEXITSTATUS(status) != 0) {
Rob Landley53433b32006-06-22 22:28:29 +0000160 bb_error_msg("script %s failed, exit=%d\n",
161 script, WEXITSTATUS(status));
Rob Landleyad8071f2005-04-30 03:49:37 +0000162 return -errno;
163 }
164 }
165 return 0;
166bad:
167 status = -errno;
168 syslog(LOG_ERR, "%s %s, %s error: %s",
169 arg, intf, why, strerror(errno));
170 return status;
171}
172
Rob Landleye3752e52005-05-03 03:28:55 +0000173
174/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000175 * Return milliseconds of random delay, up to "secs" seconds.
176 */
Rob Landley88621d72006-08-29 19:41:06 +0000177static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000178{
179 return lrand48() % (secs * 1000);
180}
181
Rob Landleye3752e52005-05-03 03:28:55 +0000182/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000183 * main program
184 */
Bernhard Reutner-Fischer38d66152005-10-21 10:43:11 +0000185
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000186/* Used to be auto variables on main() stack, but
187 * most of them were zero-inited. Moving them to bss
188 * is more space-efficient.
189 */
190static const struct in_addr null_ip; // = { 0 };
191static const struct ether_addr null_addr; // = { {0, 0, 0, 0, 0, 0} };
192
193static struct sockaddr saddr; // memset(0);
194static struct in_addr ip; // = { 0 };
195static struct ifreq ifr; //memset(0);
196
197static char *intf; // = NULL;
198static char *script; // = NULL;
199static suseconds_t timeout; // = 0; // milliseconds
200static unsigned conflicts; // = 0;
201static unsigned nprobes; // = 0;
202static unsigned nclaims; // = 0;
203static int ready; // = 0;
204static int quit; // = 0;
205static int foreground; // = 0;
206static int verbose; // = 0;
207static int state = PROBE;
208
Rob Landleyad8071f2005-04-30 03:49:37 +0000209int zcip_main(int argc, char *argv[])
210{
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000211 struct ether_addr eth_addr;
Rob Landleyad8071f2005-04-30 03:49:37 +0000212 char *why;
Rob Landleyad8071f2005-04-30 03:49:37 +0000213 int fd;
Rob Landleye3752e52005-05-03 03:28:55 +0000214 int t;
Denis Vlasenko035aae52006-09-03 12:23:56 +0000215
Rob Landleye3752e52005-05-03 03:28:55 +0000216 // parse commandline: prog [options] ifname script
Rob Landleye3752e52005-05-03 03:28:55 +0000217 while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
218 switch (t) {
219 case 'f':
220 foreground = 1;
221 continue;
222 case 'q':
223 quit = 1;
224 continue;
225 case 'r':
226 if (inet_aton(optarg, &ip) == 0
227 || (ntohl(ip.s_addr) & IN_CLASSB_NET)
228 != LINKLOCAL_ADDR) {
Rob Landley53433b32006-06-22 22:28:29 +0000229 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000230 }
231 continue;
232 case 'v':
Rob Landleye3752e52005-05-03 03:28:55 +0000233 verbose++;
234 foreground = 1;
235 continue;
236 default:
Rob Landley53433b32006-06-22 22:28:29 +0000237 bb_error_msg_and_die("bad option");
Rob Landleye3752e52005-05-03 03:28:55 +0000238 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000239 }
Rob Landleye3752e52005-05-03 03:28:55 +0000240 if (optind < argc - 1) {
241 intf = argv[optind++];
242 setenv("interface", intf, 1);
243 script = argv[optind++];
Rob Landleyad8071f2005-04-30 03:49:37 +0000244 }
Rob Landleye3752e52005-05-03 03:28:55 +0000245 if (optind != argc || !intf)
Rob Landley53433b32006-06-22 22:28:29 +0000246 bb_show_usage();
247 openlog(bb_applet_name, 0, LOG_DAEMON);
Rob Landleyad8071f2005-04-30 03:49:37 +0000248
Rob Landleye3752e52005-05-03 03:28:55 +0000249 // initialize the interface (modprobe, ifup, etc)
Rob Landleyad8071f2005-04-30 03:49:37 +0000250 if (run(script, "init", intf, NULL) < 0)
251 return EXIT_FAILURE;
252
Rob Landleye3752e52005-05-03 03:28:55 +0000253 // initialize saddr
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000254 //memset(&saddr, 0, sizeof (saddr));
Rob Landley768945b2006-06-25 00:34:52 +0000255 safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000256
Rob Landleye3752e52005-05-03 03:28:55 +0000257 // open an ARP socket
Denis Vlasenko035aae52006-09-03 12:23:56 +0000258 fd = xsocket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
259 // bind to the interface's ARP socket
260 xbind(fd, &saddr, sizeof (saddr);
261
262 // get the interface's ethernet address
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000263 //memset(&ifr, 0, sizeof (ifr));
Denis Vlasenko035aae52006-09-03 12:23:56 +0000264 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
265 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
Rob Landleye3752e52005-05-03 03:28:55 +0000266 foreground = 1;
Denis Vlasenko035aae52006-09-03 12:23:56 +0000267 why = "get ethernet address";
Rob Landleyad8071f2005-04-30 03:49:37 +0000268 goto bad;
269 }
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000270 memcpy(&eth_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000271
Denis Vlasenko035aae52006-09-03 12:23:56 +0000272 // start with some stable ip address, either a function of
273 // the hardware address or else the last address we used.
274 // NOTE: the sequence of addresses we try changes only
275 // depending on when we detect conflicts.
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000276 // (SVID 3 bogon: who says that "short" is always 16 bits?)
277 seed48( (unsigned short*)&ifr.ifr_hwaddr.sa_data );
Denis Vlasenko035aae52006-09-03 12:23:56 +0000278 if (ip.s_addr == 0)
279 pick(&ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000280
Rob Landleye3752e52005-05-03 03:28:55 +0000281 // FIXME cases to handle:
282 // - zcip already running!
283 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000284
Rob Landleye3752e52005-05-03 03:28:55 +0000285 // daemonize now; don't delay system startup
286 if (!foreground) {
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000287 xdaemon(0, verbose);
Rob Landleyad8071f2005-04-30 03:49:37 +0000288 syslog(LOG_INFO, "start, interface %s", intf);
289 }
290
Rob Landleye3752e52005-05-03 03:28:55 +0000291 // run the dynamic address negotiation protocol,
292 // restarting after address conflicts:
293 // - start with some address we want to try
294 // - short random delay
295 // - arp probes to see if another host else uses it
296 // - arp announcements that we're claiming it
297 // - use it
298 // - defend it, within limits
Rob Landleyad8071f2005-04-30 03:49:37 +0000299 while (1) {
300 struct pollfd fds[1];
301 struct timeval tv1;
302 struct arp_packet p;
303
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000304 int source_ip_conflict = 0;
305 int target_ip_conflict = 0;
306
Rob Landleyad8071f2005-04-30 03:49:37 +0000307 fds[0].fd = fd;
308 fds[0].events = POLLIN;
309 fds[0].revents = 0;
310
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000311 // poll, being ready to adjust current timeout
Rob Landley00c051e2006-06-30 14:05:19 +0000312 if (!timeout) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000313 timeout = ms_rdelay(PROBE_WAIT);
Rob Landleye3752e52005-05-03 03:28:55 +0000314 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
315 // make the kernel filter out all packets except
316 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000317 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000318 // set tv1 to the point in time when we timeout
Rob Landley00c051e2006-06-30 14:05:19 +0000319 gettimeofday(&tv1, NULL);
320 tv1.tv_usec += (timeout % 1000) * 1000;
321 while (tv1.tv_usec > 1000000) {
322 tv1.tv_usec -= 1000000;
323 tv1.tv_sec++;
324 }
325 tv1.tv_sec += timeout / 1000;
326
Rob Landleye3752e52005-05-03 03:28:55 +0000327 VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
328 timeout, intf, nprobes, nclaims);
Rob Landleyad8071f2005-04-30 03:49:37 +0000329 switch (poll(fds, 1, timeout)) {
330
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000331 // timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000332 case 0:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000333 VDBG("state = %d\n", state);
334 switch (state) {
335 case PROBE:
336 // timeouts in the PROBE state means no conflicting ARP packets
337 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000338 if (nprobes < PROBE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000339 nprobes++;
340 VDBG("probe/%d %s@%s\n",
341 nprobes, intf, inet_ntoa(ip));
342 (void)arp(fd, &saddr, ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000343 &eth_addr, null_ip,
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000344 &null_addr, ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000345 timeout = PROBE_MIN * 1000;
346 timeout += ms_rdelay(PROBE_MAX
347 - PROBE_MIN);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000348 }
349 else {
350 // Switch to announce state.
351 state = ANNOUNCE;
352 nclaims = 0;
353 VDBG("announce/%d %s@%s\n",
354 nclaims, intf, inet_ntoa(ip));
355 (void)arp(fd, &saddr, ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000356 &eth_addr, ip,
357 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000358 timeout = ANNOUNCE_INTERVAL * 1000;
359 }
360 break;
361 case RATE_LIMIT_PROBE:
362 // timeouts in the RATE_LIMIT_PROBE state means no conflicting ARP packets
363 // have been received, so we can move immediately to the announce state
364 state = ANNOUNCE;
365 nclaims = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000366 VDBG("announce/%d %s@%s\n",
367 nclaims, intf, inet_ntoa(ip));
Rob Landleyad8071f2005-04-30 03:49:37 +0000368 (void)arp(fd, &saddr, ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000369 &eth_addr, ip,
370 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000371 timeout = ANNOUNCE_INTERVAL * 1000;
372 break;
373 case ANNOUNCE:
374 // timeouts in the ANNOUNCE state means no conflicting ARP packets
375 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000376 if (nclaims < ANNOUNCE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000377 nclaims++;
378 VDBG("announce/%d %s@%s\n",
379 nclaims, intf, inet_ntoa(ip));
380 (void)arp(fd, &saddr, ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000381 &eth_addr, ip,
382 &eth_addr, ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000383 timeout = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000384 }
385 else {
386 // Switch to monitor state.
387 state = MONITOR;
Rob Landleye3752e52005-05-03 03:28:55 +0000388 // link is ok to use earlier
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000389 // FIXME update filters
Rob Landleyad8071f2005-04-30 03:49:37 +0000390 run(script, "config", intf, &ip);
391 ready = 1;
392 conflicts = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000393 timeout = -1; // Never timeout in the monitor state.
Rob Landleyad8071f2005-04-30 03:49:37 +0000394
Rob Landleye3752e52005-05-03 03:28:55 +0000395 // NOTE: all other exit paths
396 // should deconfig ...
397 if (quit)
Rob Landleyad8071f2005-04-30 03:49:37 +0000398 return EXIT_SUCCESS;
Rob Landleyad8071f2005-04-30 03:49:37 +0000399 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000400 break;
401 case DEFEND:
402 // We won! No ARP replies, so just go back to monitor.
403 state = MONITOR;
404 timeout = -1;
405 conflicts = 0;
406 break;
407 default:
408 // Invalid, should never happen. Restart the whole protocol.
409 state = PROBE;
410 pick(&ip);
411 timeout = 0;
412 nprobes = 0;
413 nclaims = 0;
414 break;
415 } // switch (state)
416 break; // case 0 (timeout)
Rob Landleye3752e52005-05-03 03:28:55 +0000417 // packets arriving
Rob Landleyad8071f2005-04-30 03:49:37 +0000418 case 1:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000419 // We need to adjust the timeout in case we didn't receive
420 // a conflicting packet.
Rob Landleyad8071f2005-04-30 03:49:37 +0000421 if (timeout > 0) {
422 struct timeval tv2;
Rob Landleye3752e52005-05-03 03:28:55 +0000423
Rob Landleyad8071f2005-04-30 03:49:37 +0000424 gettimeofday(&tv2, NULL);
425 if (timercmp(&tv1, &tv2, <)) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000426 // Current time is greater than the expected timeout time.
427 // Should never happen.
428 VDBG("missed an expected timeout\n");
Paul Foxef81ce62006-03-29 23:01:33 +0000429 timeout = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000430 } else {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000431 VDBG("adjusting timeout\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000432 timersub(&tv1, &tv2, &tv1);
433 timeout = 1000 * tv1.tv_sec
434 + tv1.tv_usec / 1000;
435 }
436 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000437
Rob Landleyad8071f2005-04-30 03:49:37 +0000438 if ((fds[0].revents & POLLIN) == 0) {
439 if (fds[0].revents & POLLERR) {
Rob Landleye3752e52005-05-03 03:28:55 +0000440 // FIXME: links routinely go down;
441 // this shouldn't necessarily exit.
Rob Landley53433b32006-06-22 22:28:29 +0000442 bb_error_msg("%s: poll error\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000443 if (ready) {
Rob Landleye3752e52005-05-03 03:28:55 +0000444 run(script, "deconfig",
445 intf, &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000446 }
447 return EXIT_FAILURE;
448 }
449 continue;
450 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000451
Rob Landleye3752e52005-05-03 03:28:55 +0000452 // read ARP packet
Rob Landleyad8071f2005-04-30 03:49:37 +0000453 if (recv(fd, &p, sizeof (p), 0) < 0) {
454 why = "recv";
455 goto bad;
456 }
457 if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
458 continue;
Rob Landleye3752e52005-05-03 03:28:55 +0000459
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000460#ifdef DEBUG
461 {
462 struct ether_addr * sha = (struct ether_addr *) p.arp.arp_sha;
463 struct ether_addr * tha = (struct ether_addr *) p.arp.arp_tha;
464 struct in_addr * spa = (struct in_addr *) p.arp.arp_spa;
465 struct in_addr * tpa = (struct in_addr *) p.arp.arp_tpa;
466 VDBG("%s recv arp type=%d, op=%d,\n",
Rob Landleye3752e52005-05-03 03:28:55 +0000467 intf, ntohs(p.hdr.ether_type),
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000468 ntohs(p.arp.arp_op));
469 VDBG("\tsource=%s %s\n",
470 ether_ntoa(sha),
471 inet_ntoa(*spa));
472 VDBG("\ttarget=%s %s\n",
473 ether_ntoa(tha),
474 inet_ntoa(*tpa));
475 }
476#endif
477 if (p.arp.arp_op != htons(ARPOP_REQUEST)
478 && p.arp.arp_op != htons(ARPOP_REPLY))
Rob Landleyad8071f2005-04-30 03:49:37 +0000479 continue;
480
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000481 if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000482 memcmp(&eth_addr, &p.arp.arp_sha, ETH_ALEN) != 0) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000483 source_ip_conflict = 1;
484 }
485 if (memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
486 p.arp.arp_op == htons(ARPOP_REQUEST) &&
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000487 memcmp(&eth_addr, &p.arp.arp_tha, ETH_ALEN) != 0) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000488 target_ip_conflict = 1;
489 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000490
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000491 VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
492 state, source_ip_conflict, target_ip_conflict);
493 switch (state) {
494 case PROBE:
495 case ANNOUNCE:
496 // When probing or announcing, check for source IP conflicts
497 // and other hosts doing ARP probes (target IP conflicts).
498 if (source_ip_conflict || target_ip_conflict) {
499 conflicts++;
500 if (conflicts >= MAX_CONFLICTS) {
501 VDBG("%s ratelimit\n", intf);
502 timeout = RATE_LIMIT_INTERVAL * 1000;
503 state = RATE_LIMIT_PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000504 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000505
506 // restart the whole protocol
507 pick(&ip);
508 timeout = 0;
509 nprobes = 0;
510 nclaims = 0;
511 }
512 break;
513 case MONITOR:
514 // If a conflict, we try to defend with a single ARP probe.
515 if (source_ip_conflict) {
516 VDBG("monitor conflict -- defending\n");
517 state = DEFEND;
518 timeout = DEFEND_INTERVAL * 1000;
519 (void)arp(fd, &saddr,
520 ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000521 &eth_addr, ip,
522 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000523 }
524 break;
525 case DEFEND:
526 // Well, we tried. Start over (on conflict).
527 if (source_ip_conflict) {
528 state = PROBE;
529 VDBG("defend conflict -- starting over\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000530 ready = 0;
531 run(script, "deconfig", intf, &ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000532
533 // restart the whole protocol
534 pick(&ip);
535 timeout = 0;
536 nprobes = 0;
537 nclaims = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000538 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000539 break;
540 default:
541 // Invalid, should never happen. Restart the whole protocol.
542 VDBG("invalid state -- starting over\n");
543 state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000544 pick(&ip);
545 timeout = 0;
546 nprobes = 0;
547 nclaims = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000548 break;
549 } // switch state
Rob Landleyad8071f2005-04-30 03:49:37 +0000550
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000551 break; // case 1 (packets arriving)
Rob Landleyad8071f2005-04-30 03:49:37 +0000552 default:
553 why = "poll";
554 goto bad;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000555 } // switch poll
Rob Landleyad8071f2005-04-30 03:49:37 +0000556 }
557bad:
Rob Landleye3752e52005-05-03 03:28:55 +0000558 if (foreground)
559 perror(why);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000560 else
Rob Landleyad8071f2005-04-30 03:49:37 +0000561 syslog(LOG_ERR, "%s %s, %s error: %s",
Rob Landley53433b32006-06-22 22:28:29 +0000562 bb_applet_name, intf, why, strerror(errno));
Rob Landleyad8071f2005-04-30 03:49:37 +0000563 return EXIT_FAILURE;
564}