blob: a7f19d5db60b281923c9ea055216bff354e7e57a [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 Vlasenkoaf906a32006-09-03 12:29:53 +000075#define VDBG(fmt,args...) \
Rob Landleye3752e52005-05-03 03:28:55 +000076 do { } while (0)
Rob Landleye3752e52005-05-03 03:28:55 +000077
Denis Vlasenkoa9abecd2006-09-03 12:28:32 +000078static unsigned long opts;
79#define FOREGROUND (opts & 1)
80#define QUIT (opts & 2)
81
Rob Landleye3752e52005-05-03 03:28:55 +000082/**
Rob Landleyad8071f2005-04-30 03:49:37 +000083 * Pick a random link local IP address on 169.254/16, except that
84 * the first and last 256 addresses are reserved.
85 */
Rob Landley53433b32006-06-22 22:28:29 +000086static void pick(struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +000087{
Denis Vlasenkoaf906a32006-09-03 12:29:53 +000088 unsigned tmp;
Rob Landleyad8071f2005-04-30 03:49:37 +000089
90 /* use cheaper math than lrand48() mod N */
91 do {
92 tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
93 } while (tmp > (IN_CLASSB_HOST - 0x0200));
94 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
95}
96
Denis Vlasenkoaf906a32006-09-03 12:29:53 +000097/* TODO: we need a flag to direct bb_[p]error_msg output to stderr. */
98
Rob Landleye3752e52005-05-03 03:28:55 +000099/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000100 * Broadcast an ARP packet.
101 */
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000102static void 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) {
Denis Vlasenkoa9abecd2006-09-03 12:28:32 +0000127 if (FOREGROUND)
128 perror("sendto");
129 else
130 syslog(LOG_ERR, "sendto: %s", strerror(errno));
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000131 //return -errno;
Rob Landleyad8071f2005-04-30 03:49:37 +0000132 }
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000133 // Currently all callers ignore errors, that's why returns are
134 // commented out...
135 //return 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000136}
137
Rob Landleye3752e52005-05-03 03:28:55 +0000138/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000139 * Run a script.
140 */
Rob Landley53433b32006-06-22 22:28:29 +0000141static int run(char *script, char *arg, char *intf, struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000142{
143 int pid, status;
144 char *why;
145
Denis Vlasenkoa9abecd2006-09-03 12:28:32 +0000146 if(1) { //always true: if (script != NULL)
Rob Landleye3752e52005-05-03 03:28:55 +0000147 VDBG("%s run %s %s\n", intf, script, arg);
Rob Landleyad8071f2005-04-30 03:49:37 +0000148 if (ip != NULL) {
149 char *addr = inet_ntoa(*ip);
Rob Landleye3752e52005-05-03 03:28:55 +0000150 setenv("ip", addr, 1);
Denis Vlasenkoa9abecd2006-09-03 12:28:32 +0000151 if (!FOREGROUND)
152 syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
Rob Landleyad8071f2005-04-30 03:49:37 +0000153 }
154
155 pid = vfork();
Rob Landleye3752e52005-05-03 03:28:55 +0000156 if (pid < 0) { // error
Rob Landleyad8071f2005-04-30 03:49:37 +0000157 why = "vfork";
158 goto bad;
Rob Landleye3752e52005-05-03 03:28:55 +0000159 } else if (pid == 0) { // child
Rob Landleyad8071f2005-04-30 03:49:37 +0000160 execl(script, script, arg, NULL);
Denis Vlasenkoa9abecd2006-09-03 12:28:32 +0000161 if (FOREGROUND)
162 perror("execl");
163 else
164 syslog(LOG_ERR, "execl: %s", strerror(errno));
Rob Landleye3752e52005-05-03 03:28:55 +0000165 _exit(EXIT_FAILURE);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000166 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000167
168 if (waitpid(pid, &status, 0) <= 0) {
169 why = "waitpid";
170 goto bad;
171 }
172 if (WEXITSTATUS(status) != 0) {
Denis Vlasenkoa9abecd2006-09-03 12:28:32 +0000173 if (FOREGROUND)
174 bb_error_msg("script %s failed, exit=%d\n",
175 script, WEXITSTATUS(status));
176 else
177 syslog(LOG_ERR, "script %s failed, exit=%d",
Rob Landley53433b32006-06-22 22:28:29 +0000178 script, WEXITSTATUS(status));
Rob Landleyad8071f2005-04-30 03:49:37 +0000179 return -errno;
180 }
181 }
182 return 0;
183bad:
184 status = -errno;
Denis Vlasenkoa9abecd2006-09-03 12:28:32 +0000185 if (FOREGROUND)
186 bb_perror_msg("%s %s, %s",
187 arg, intf, why);
188 else
189 syslog(LOG_ERR, "%s %s, %s: %s",
190 arg, intf, why, strerror(errno));
Rob Landleyad8071f2005-04-30 03:49:37 +0000191 return status;
192}
193
Rob Landleye3752e52005-05-03 03:28:55 +0000194
195/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000196 * Return milliseconds of random delay, up to "secs" seconds.
197 */
Rob Landley88621d72006-08-29 19:41:06 +0000198static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000199{
200 return lrand48() % (secs * 1000);
201}
202
Rob Landleye3752e52005-05-03 03:28:55 +0000203/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000204 * main program
205 */
Bernhard Reutner-Fischer38d66152005-10-21 10:43:11 +0000206
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000207/* Used to be auto variables on main() stack, but
208 * most of them were zero-inited. Moving them to bss
209 * is more space-efficient.
210 */
211static const struct in_addr null_ip; // = { 0 };
212static const struct ether_addr null_addr; // = { {0, 0, 0, 0, 0, 0} };
213
214static struct sockaddr saddr; // memset(0);
215static struct in_addr ip; // = { 0 };
216static struct ifreq ifr; //memset(0);
217
218static char *intf; // = NULL;
219static char *script; // = NULL;
220static suseconds_t timeout; // = 0; // milliseconds
221static unsigned conflicts; // = 0;
222static unsigned nprobes; // = 0;
223static unsigned nclaims; // = 0;
224static int ready; // = 0;
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000225static int verbose; // = 0;
226static int state = PROBE;
227
Rob Landleyad8071f2005-04-30 03:49:37 +0000228int zcip_main(int argc, char *argv[])
229{
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000230 struct ether_addr eth_addr;
Rob Landleyad8071f2005-04-30 03:49:37 +0000231 char *why;
Rob Landleyad8071f2005-04-30 03:49:37 +0000232 int fd;
Denis Vlasenko035aae52006-09-03 12:23:56 +0000233
Rob Landleye3752e52005-05-03 03:28:55 +0000234 // parse commandline: prog [options] ifname script
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000235 char *r_opt;
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000236 bb_opt_complementally = "vv"; // -v options accumulate
237 opts = bb_getopt_ulflags(argc, argv, "fqr:v", &r_opt, &verbose);
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000238 if (opts & 4) { // -r n.n.n.n
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000239 if (inet_aton(r_opt, &ip) == 0
240 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
241 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000242 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000243 }
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000244 if (verbose) opts |= 1; // -v implies -f
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000245 argc -= optind;
246 argv += optind;
247 if (argc != 2)
Rob Landley53433b32006-06-22 22:28:29 +0000248 bb_show_usage();
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000249 intf = argv[0];
250 script = argv[1];
251 setenv("interface", intf, 1);
Rob Landleyad8071f2005-04-30 03:49:37 +0000252
Rob Landleye3752e52005-05-03 03:28:55 +0000253 // initialize the interface (modprobe, ifup, etc)
Rob Landleyad8071f2005-04-30 03:49:37 +0000254 if (run(script, "init", intf, NULL) < 0)
255 return EXIT_FAILURE;
256
Rob Landleye3752e52005-05-03 03:28:55 +0000257 // initialize saddr
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000258 //memset(&saddr, 0, sizeof (saddr));
Rob Landley768945b2006-06-25 00:34:52 +0000259 safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000260
Rob Landleye3752e52005-05-03 03:28:55 +0000261 // open an ARP socket
Denis Vlasenko035aae52006-09-03 12:23:56 +0000262 fd = xsocket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
263 // bind to the interface's ARP socket
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000264 xbind(fd, &saddr, sizeof (saddr));
Denis Vlasenko035aae52006-09-03 12:23:56 +0000265
266 // get the interface's ethernet address
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000267 //memset(&ifr, 0, sizeof (ifr));
Denis Vlasenko035aae52006-09-03 12:23:56 +0000268 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
269 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000270 bb_perror_msg_and_die("get ethernet address");
Rob Landleyad8071f2005-04-30 03:49:37 +0000271 }
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000272 memcpy(&eth_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000273
Denis Vlasenko035aae52006-09-03 12:23:56 +0000274 // start with some stable ip address, either a function of
275 // the hardware address or else the last address we used.
276 // NOTE: the sequence of addresses we try changes only
277 // depending on when we detect conflicts.
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000278 // (SVID 3 bogon: who says that "short" is always 16 bits?)
279 seed48( (unsigned short*)&ifr.ifr_hwaddr.sa_data );
Denis Vlasenko035aae52006-09-03 12:23:56 +0000280 if (ip.s_addr == 0)
281 pick(&ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000282
Rob Landleye3752e52005-05-03 03:28:55 +0000283 // FIXME cases to handle:
284 // - zcip already running!
285 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000286
Rob Landleye3752e52005-05-03 03:28:55 +0000287 // daemonize now; don't delay system startup
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000288 if (!FOREGROUND) {
Denis Vlasenko27af5a02006-09-03 12:21:59 +0000289 xdaemon(0, verbose);
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000290 openlog(bb_applet_name, 0, LOG_DAEMON);
Rob Landleyad8071f2005-04-30 03:49:37 +0000291 syslog(LOG_INFO, "start, interface %s", intf);
292 }
293
Rob Landleye3752e52005-05-03 03:28:55 +0000294 // run the dynamic address negotiation protocol,
295 // restarting after address conflicts:
296 // - start with some address we want to try
297 // - short random delay
298 // - arp probes to see if another host else uses it
299 // - arp announcements that we're claiming it
300 // - use it
301 // - defend it, within limits
Rob Landleyad8071f2005-04-30 03:49:37 +0000302 while (1) {
303 struct pollfd fds[1];
304 struct timeval tv1;
305 struct arp_packet p;
306
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000307 int source_ip_conflict = 0;
308 int target_ip_conflict = 0;
309
Rob Landleyad8071f2005-04-30 03:49:37 +0000310 fds[0].fd = fd;
311 fds[0].events = POLLIN;
312 fds[0].revents = 0;
313
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000314 // poll, being ready to adjust current timeout
Rob Landley00c051e2006-06-30 14:05:19 +0000315 if (!timeout) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000316 timeout = ms_rdelay(PROBE_WAIT);
Rob Landleye3752e52005-05-03 03:28:55 +0000317 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
318 // make the kernel filter out all packets except
319 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000320 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000321 // set tv1 to the point in time when we timeout
Rob Landley00c051e2006-06-30 14:05:19 +0000322 gettimeofday(&tv1, NULL);
323 tv1.tv_usec += (timeout % 1000) * 1000;
324 while (tv1.tv_usec > 1000000) {
325 tv1.tv_usec -= 1000000;
326 tv1.tv_sec++;
327 }
328 tv1.tv_sec += timeout / 1000;
329
Rob Landleye3752e52005-05-03 03:28:55 +0000330 VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
331 timeout, intf, nprobes, nclaims);
Rob Landleyad8071f2005-04-30 03:49:37 +0000332 switch (poll(fds, 1, timeout)) {
333
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000334 // timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000335 case 0:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000336 VDBG("state = %d\n", state);
337 switch (state) {
338 case PROBE:
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000339 // timeouts in the PROBE state mean no conflicting ARP packets
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000340 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000341 if (nprobes < PROBE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000342 nprobes++;
343 VDBG("probe/%d %s@%s\n",
344 nprobes, intf, inet_ntoa(ip));
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000345 arp(fd, &saddr, ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000346 &eth_addr, null_ip,
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000347 &null_addr, ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000348 timeout = PROBE_MIN * 1000;
349 timeout += ms_rdelay(PROBE_MAX
350 - PROBE_MIN);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000351 }
352 else {
353 // Switch to announce state.
354 state = ANNOUNCE;
355 nclaims = 0;
356 VDBG("announce/%d %s@%s\n",
357 nclaims, intf, inet_ntoa(ip));
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000358 arp(fd, &saddr, ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000359 &eth_addr, ip,
360 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000361 timeout = ANNOUNCE_INTERVAL * 1000;
362 }
363 break;
364 case RATE_LIMIT_PROBE:
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000365 // timeouts in the RATE_LIMIT_PROBE state mean no conflicting ARP packets
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000366 // have been received, so we can move immediately to the announce state
367 state = ANNOUNCE;
368 nclaims = 0;
Rob Landleye3752e52005-05-03 03:28:55 +0000369 VDBG("announce/%d %s@%s\n",
370 nclaims, intf, inet_ntoa(ip));
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000371 arp(fd, &saddr, ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000372 &eth_addr, ip,
373 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000374 timeout = ANNOUNCE_INTERVAL * 1000;
375 break;
376 case ANNOUNCE:
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000377 // timeouts in the ANNOUNCE state mean no conflicting ARP packets
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000378 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000379 if (nclaims < ANNOUNCE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000380 nclaims++;
381 VDBG("announce/%d %s@%s\n",
382 nclaims, intf, inet_ntoa(ip));
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000383 arp(fd, &saddr, ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000384 &eth_addr, ip,
385 &eth_addr, ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000386 timeout = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000387 }
388 else {
389 // Switch to monitor state.
390 state = MONITOR;
Rob Landleye3752e52005-05-03 03:28:55 +0000391 // link is ok to use earlier
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000392 // FIXME update filters
Rob Landleyad8071f2005-04-30 03:49:37 +0000393 run(script, "config", intf, &ip);
394 ready = 1;
395 conflicts = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000396 timeout = -1; // Never timeout in the monitor state.
Rob Landleyad8071f2005-04-30 03:49:37 +0000397
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000398 // NOTE: all other exit paths
Rob Landleye3752e52005-05-03 03:28:55 +0000399 // should deconfig ...
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000400 if (QUIT)
Rob Landleyad8071f2005-04-30 03:49:37 +0000401 return EXIT_SUCCESS;
Rob Landleyad8071f2005-04-30 03:49:37 +0000402 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000403 break;
404 case DEFEND:
405 // We won! No ARP replies, so just go back to monitor.
406 state = MONITOR;
407 timeout = -1;
408 conflicts = 0;
409 break;
410 default:
411 // Invalid, should never happen. Restart the whole protocol.
412 state = PROBE;
413 pick(&ip);
414 timeout = 0;
415 nprobes = 0;
416 nclaims = 0;
417 break;
418 } // switch (state)
419 break; // case 0 (timeout)
Rob Landleye3752e52005-05-03 03:28:55 +0000420 // packets arriving
Rob Landleyad8071f2005-04-30 03:49:37 +0000421 case 1:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000422 // We need to adjust the timeout in case we didn't receive
423 // a conflicting packet.
Rob Landleyad8071f2005-04-30 03:49:37 +0000424 if (timeout > 0) {
425 struct timeval tv2;
Rob Landleye3752e52005-05-03 03:28:55 +0000426
Rob Landleyad8071f2005-04-30 03:49:37 +0000427 gettimeofday(&tv2, NULL);
428 if (timercmp(&tv1, &tv2, <)) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000429 // Current time is greater than the expected timeout time.
430 // Should never happen.
431 VDBG("missed an expected timeout\n");
Paul Foxef81ce62006-03-29 23:01:33 +0000432 timeout = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000433 } else {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000434 VDBG("adjusting timeout\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000435 timersub(&tv1, &tv2, &tv1);
436 timeout = 1000 * tv1.tv_sec
437 + tv1.tv_usec / 1000;
438 }
439 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000440
Rob Landleyad8071f2005-04-30 03:49:37 +0000441 if ((fds[0].revents & POLLIN) == 0) {
442 if (fds[0].revents & POLLERR) {
Rob Landleye3752e52005-05-03 03:28:55 +0000443 // FIXME: links routinely go down;
444 // this shouldn't necessarily exit.
Rob Landley53433b32006-06-22 22:28:29 +0000445 bb_error_msg("%s: poll error\n", intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000446 if (ready) {
Rob Landleye3752e52005-05-03 03:28:55 +0000447 run(script, "deconfig",
448 intf, &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000449 }
450 return EXIT_FAILURE;
451 }
452 continue;
453 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000454
Rob Landleye3752e52005-05-03 03:28:55 +0000455 // read ARP packet
Rob Landleyad8071f2005-04-30 03:49:37 +0000456 if (recv(fd, &p, sizeof (p), 0) < 0) {
457 why = "recv";
458 goto bad;
459 }
460 if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
461 continue;
Rob Landleye3752e52005-05-03 03:28:55 +0000462
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000463#ifdef DEBUG
464 {
465 struct ether_addr * sha = (struct ether_addr *) p.arp.arp_sha;
466 struct ether_addr * tha = (struct ether_addr *) p.arp.arp_tha;
467 struct in_addr * spa = (struct in_addr *) p.arp.arp_spa;
468 struct in_addr * tpa = (struct in_addr *) p.arp.arp_tpa;
469 VDBG("%s recv arp type=%d, op=%d,\n",
Rob Landleye3752e52005-05-03 03:28:55 +0000470 intf, ntohs(p.hdr.ether_type),
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000471 ntohs(p.arp.arp_op));
472 VDBG("\tsource=%s %s\n",
473 ether_ntoa(sha),
474 inet_ntoa(*spa));
475 VDBG("\ttarget=%s %s\n",
476 ether_ntoa(tha),
477 inet_ntoa(*tpa));
478 }
479#endif
480 if (p.arp.arp_op != htons(ARPOP_REQUEST)
481 && p.arp.arp_op != htons(ARPOP_REPLY))
Rob Landleyad8071f2005-04-30 03:49:37 +0000482 continue;
483
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000484 if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000485 memcmp(&eth_addr, &p.arp.arp_sha, ETH_ALEN) != 0) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000486 source_ip_conflict = 1;
487 }
488 if (memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
489 p.arp.arp_op == htons(ARPOP_REQUEST) &&
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000490 memcmp(&eth_addr, &p.arp.arp_tha, ETH_ALEN) != 0) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000491 target_ip_conflict = 1;
492 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000493
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000494 VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
495 state, source_ip_conflict, target_ip_conflict);
496 switch (state) {
497 case PROBE:
498 case ANNOUNCE:
499 // When probing or announcing, check for source IP conflicts
500 // and other hosts doing ARP probes (target IP conflicts).
501 if (source_ip_conflict || target_ip_conflict) {
502 conflicts++;
503 if (conflicts >= MAX_CONFLICTS) {
504 VDBG("%s ratelimit\n", intf);
505 timeout = RATE_LIMIT_INTERVAL * 1000;
506 state = RATE_LIMIT_PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000507 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000508
509 // restart the whole protocol
510 pick(&ip);
511 timeout = 0;
512 nprobes = 0;
513 nclaims = 0;
514 }
515 break;
516 case MONITOR:
517 // If a conflict, we try to defend with a single ARP probe.
518 if (source_ip_conflict) {
519 VDBG("monitor conflict -- defending\n");
520 state = DEFEND;
521 timeout = DEFEND_INTERVAL * 1000;
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000522 arp(fd, &saddr,
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000523 ARPOP_REQUEST,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000524 &eth_addr, ip,
525 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000526 }
527 break;
528 case DEFEND:
529 // Well, we tried. Start over (on conflict).
530 if (source_ip_conflict) {
531 state = PROBE;
532 VDBG("defend conflict -- starting over\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000533 ready = 0;
534 run(script, "deconfig", intf, &ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000535
536 // restart the whole protocol
537 pick(&ip);
538 timeout = 0;
539 nprobes = 0;
540 nclaims = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000541 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000542 break;
543 default:
544 // Invalid, should never happen. Restart the whole protocol.
545 VDBG("invalid state -- starting over\n");
546 state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000547 pick(&ip);
548 timeout = 0;
549 nprobes = 0;
550 nclaims = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000551 break;
552 } // switch state
Rob Landleyad8071f2005-04-30 03:49:37 +0000553
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000554 break; // case 1 (packets arriving)
Rob Landleyad8071f2005-04-30 03:49:37 +0000555 default:
556 why = "poll";
557 goto bad;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000558 } // switch poll
Rob Landleyad8071f2005-04-30 03:49:37 +0000559 }
560bad:
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000561 if (FOREGROUND)
Rob Landleye3752e52005-05-03 03:28:55 +0000562 perror(why);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000563 else
Rob Landleyad8071f2005-04-30 03:49:37 +0000564 syslog(LOG_ERR, "%s %s, %s error: %s",
Rob Landley53433b32006-06-22 22:28:29 +0000565 bb_applet_name, intf, why, strerror(errno));
Rob Landleyad8071f2005-04-30 03:49:37 +0000566 return EXIT_FAILURE;
567}