blob: 7314ff8db7509b57f75eef48d50b2ade4cb911bb [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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
Pere Orga5bc8c002011-04-11 03:29:49 +020026//usage:#define zcip_trivial_usage
27//usage: "[OPTIONS] IFACE SCRIPT"
28//usage:#define zcip_full_usage "\n\n"
29//usage: "Manage a ZeroConf IPv4 link-local address\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020030//usage: "\n -f Run in foreground"
31//usage: "\n -q Quit after obtaining address"
32//usage: "\n -r 169.254.x.x Request this address first"
33//usage: "\n -v Verbose"
34//usage: "\n"
35//usage: "\nWith no -q, runs continuously monitoring for ARP conflicts,"
36//usage: "\nexits only on I/O errors (link down etc)"
37
Dan Fandrichf533ec82011-06-10 05:17:59 +020038#include "libbb.h"
Rob Landleyad8071f2005-04-30 03:49:37 +000039#include <netinet/ether.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000040#include <net/if.h>
41#include <net/if_arp.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000042#include <linux/sockios.h>
Rob Landleye3752e52005-05-03 03:28:55 +000043
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000044#include <syslog.h>
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000045
46/* We don't need more than 32 bits of the counter */
47#define MONOTONIC_US() ((unsigned)monotonic_us())
Rob Landleyad8071f2005-04-30 03:49:37 +000048
Bernhard Reutner-Fischerec351c32005-12-13 10:28:25 +000049struct arp_packet {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000050 struct ether_header eth;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000051 struct ether_arp arp;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000052} PACKED;
Rob Landleyad8071f2005-04-30 03:49:37 +000053
Rob Landleybc68cd12006-03-10 19:22:06 +000054enum {
Rob Landleyad8071f2005-04-30 03:49:37 +000055/* 169.254.0.0 */
Rob Landleybc68cd12006-03-10 19:22:06 +000056 LINKLOCAL_ADDR = 0xa9fe0000,
Rob Landleyad8071f2005-04-30 03:49:37 +000057
58/* protocol timeout parameters, specified in seconds */
Rob Landleybc68cd12006-03-10 19:22:06 +000059 PROBE_WAIT = 1,
60 PROBE_MIN = 1,
61 PROBE_MAX = 2,
62 PROBE_NUM = 3,
63 MAX_CONFLICTS = 10,
64 RATE_LIMIT_INTERVAL = 60,
65 ANNOUNCE_WAIT = 2,
66 ANNOUNCE_NUM = 2,
67 ANNOUNCE_INTERVAL = 2,
68 DEFEND_INTERVAL = 10
69};
Rob Landleyad8071f2005-04-30 03:49:37 +000070
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000071/* States during the configuration process. */
72enum {
73 PROBE = 0,
74 RATE_LIMIT_PROBE,
75 ANNOUNCE,
76 MONITOR,
77 DEFEND
78};
Rob Landleyad8071f2005-04-30 03:49:37 +000079
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000080#define VDBG(...) do { } while (0)
81
82
83enum {
84 sock_fd = 3
85};
86
87struct globals {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000088 struct sockaddr saddr;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +000089 struct ether_addr eth_addr;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010090} FIX_ALIASING;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000091#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +000092#define saddr (G.saddr )
93#define eth_addr (G.eth_addr)
Denys Vlasenko16714242011-09-21 01:59:15 +020094#define INIT_G() do { } while (0)
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000095
Rob Landleye3752e52005-05-03 03:28:55 +000096
97/**
Rob Landleyad8071f2005-04-30 03:49:37 +000098 * Pick a random link local IP address on 169.254/16, except that
99 * the first and last 256 addresses are reserved.
100 */
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000101static uint32_t pick(void)
Rob Landleyad8071f2005-04-30 03:49:37 +0000102{
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000103 unsigned tmp;
Rob Landleyad8071f2005-04-30 03:49:37 +0000104
Rob Landleyad8071f2005-04-30 03:49:37 +0000105 do {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000106 tmp = rand() & IN_CLASSB_HOST;
Rob Landleyad8071f2005-04-30 03:49:37 +0000107 } while (tmp > (IN_CLASSB_HOST - 0x0200));
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000108 return htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
Rob Landleyad8071f2005-04-30 03:49:37 +0000109}
110
Rob Landleye3752e52005-05-03 03:28:55 +0000111/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000112 * Broadcast an ARP packet.
113 */
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000114static void arp(
115 /* int op, - always ARPOP_REQUEST */
116 /* const struct ether_addr *source_eth, - always &eth_addr */
117 struct in_addr source_ip,
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000118 const struct ether_addr *target_eth, struct in_addr target_ip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000119{
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000120 enum { op = ARPOP_REQUEST };
121#define source_eth (&eth_addr)
122
Rob Landleyad8071f2005-04-30 03:49:37 +0000123 struct arp_packet p;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000124 memset(&p, 0, sizeof(p));
Rob Landleyad8071f2005-04-30 03:49:37 +0000125
Rob Landleye3752e52005-05-03 03:28:55 +0000126 // ether header
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000127 p.eth.ether_type = htons(ETHERTYPE_ARP);
128 memcpy(p.eth.ether_shost, source_eth, ETH_ALEN);
129 memset(p.eth.ether_dhost, 0xff, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000130
Rob Landleye3752e52005-05-03 03:28:55 +0000131 // arp request
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000132 p.arp.arp_hrd = htons(ARPHRD_ETHER);
133 p.arp.arp_pro = htons(ETHERTYPE_IP);
134 p.arp.arp_hln = ETH_ALEN;
135 p.arp.arp_pln = 4;
136 p.arp.arp_op = htons(op);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000137 memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN);
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000138 memcpy(&p.arp.arp_spa, &source_ip, sizeof(p.arp.arp_spa));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000139 memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000140 memcpy(&p.arp.arp_tpa, &target_ip, sizeof(p.arp.arp_tpa));
Rob Landleyad8071f2005-04-30 03:49:37 +0000141
Rob Landleye3752e52005-05-03 03:28:55 +0000142 // send it
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000143 // Even though sock_fd is already bound to saddr, just send()
144 // won't work, because "socket is not connected"
145 // (and connect() won't fix that, "operation not supported").
146 // Thus we sendto() to saddr. I wonder which sockaddr
147 // (from bind() or from sendto()?) kernel actually uses
148 // to determine iface to emit the packet from...
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000149 xsendto(sock_fd, &p, sizeof(p), &saddr, sizeof(saddr));
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000150#undef source_eth
Rob Landleyad8071f2005-04-30 03:49:37 +0000151}
152
Rob Landleye3752e52005-05-03 03:28:55 +0000153/**
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000154 * Run a script.
155 * argv[0]:intf argv[1]:script_name argv[2]:junk argv[3]:NULL
Rob Landleyad8071f2005-04-30 03:49:37 +0000156 */
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000157static int run(char *argv[3], const char *param, struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000158{
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000159 int status;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000160 char *addr = addr; /* for gcc */
161 const char *fmt = "%s %s %s" + 3;
Rob Landleyad8071f2005-04-30 03:49:37 +0000162
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000163 argv[2] = (char*)param;
164
165 VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]);
Rob Landleyad8071f2005-04-30 03:49:37 +0000166
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000167 if (ip) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000168 addr = inet_ntoa(*ip);
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000169 xsetenv("ip", addr);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000170 fmt -= 3;
Rob Landleyad8071f2005-04-30 03:49:37 +0000171 }
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000172 bb_info_msg(fmt, argv[2], argv[0], addr);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000173
Denys Vlasenko8531d762010-03-18 22:44:00 +0100174 status = spawn_and_wait(argv + 1);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000175 if (status < 0) {
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000176 bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000177 return -errno;
178 }
179 if (status != 0)
Denys Vlasenko8531d762010-03-18 22:44:00 +0100180 bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
Rob Landleyad8071f2005-04-30 03:49:37 +0000181 return status;
182}
183
Rob Landleye3752e52005-05-03 03:28:55 +0000184/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000185 * Return milliseconds of random delay, up to "secs" seconds.
186 */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000187static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000188{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000189 return rand() % (secs * 1000);
Rob Landleyad8071f2005-04-30 03:49:37 +0000190}
191
Rob Landleye3752e52005-05-03 03:28:55 +0000192/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000193 * main program
194 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000195int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100196int zcip_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyad8071f2005-04-30 03:49:37 +0000197{
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000198 int state;
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000199 char *r_opt;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000200 unsigned opts;
201
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000202 // ugly trick, but I want these zeroed in one go
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000203 struct {
204 const struct in_addr null_ip;
205 const struct ether_addr null_addr;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000206 struct in_addr ip;
207 struct ifreq ifr;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000208 int timeout_ms; /* must be signed */
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000209 unsigned conflicts;
210 unsigned nprobes;
211 unsigned nclaims;
212 int ready;
213 int verbose;
214 } L;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000215#define null_ip (L.null_ip )
216#define null_addr (L.null_addr )
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000217#define ip (L.ip )
218#define ifr (L.ifr )
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000219#define timeout_ms (L.timeout_ms)
220#define conflicts (L.conflicts )
221#define nprobes (L.nprobes )
222#define nclaims (L.nclaims )
223#define ready (L.ready )
224#define verbose (L.verbose )
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000225
226 memset(&L, 0, sizeof(L));
Denys Vlasenko16714242011-09-21 01:59:15 +0200227 INIT_G();
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000228
229#define FOREGROUND (opts & 1)
230#define QUIT (opts & 2)
231 // parse commandline: prog [options] ifname script
232 // exactly 2 args; -v accumulates and implies -f
233 opt_complementary = "=2:vv:vf";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000234 opts = getopt32(argv, "fqr:v", &r_opt, &verbose);
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000235#if !BB_MMU
236 // on NOMMU reexec early (or else we will rerun things twice)
237 if (!FOREGROUND)
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000238 bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv);
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000239#endif
240 // open an ARP socket
241 // (need to do it before openlog to prevent openlog from taking
242 // fd 3 (sock_fd==3))
243 xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000244 if (!FOREGROUND) {
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000245 // do it before all bb_xx_msg calls
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000246 openlog(applet_name, 0, LOG_DAEMON);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000247 logmode |= LOGMODE_SYSLOG;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000248 }
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000249 if (opts & 4) { // -r n.n.n.n
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000250 if (inet_aton(r_opt, &ip) == 0
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000251 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR
252 ) {
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000253 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000254 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000255 }
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000256 argv += optind - 1;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000257
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000258 /* Now: argv[0]:junk argv[1]:intf argv[2]:script argv[3]:NULL */
259 /* We need to make space for script argument: */
260 argv[0] = argv[1];
261 argv[1] = argv[2];
262 /* Now: argv[0]:intf argv[1]:script argv[2]:junk argv[3]:NULL */
263#define argv_intf (argv[0])
264
265 xsetenv("interface", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000266
Rob Landleye3752e52005-05-03 03:28:55 +0000267 // initialize the interface (modprobe, ifup, etc)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000268 if (run(argv, "init", NULL))
Rob Landleyad8071f2005-04-30 03:49:37 +0000269 return EXIT_FAILURE;
270
Rob Landleye3752e52005-05-03 03:28:55 +0000271 // initialize saddr
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000272 // saddr is: { u16 sa_family; u8 sa_data[14]; }
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000273 //memset(&saddr, 0, sizeof(saddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000274 //TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000275 safe_strncpy(saddr.sa_data, argv_intf, sizeof(saddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000276
Denis Vlasenko035aae52006-09-03 12:23:56 +0000277 // bind to the interface's ARP socket
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000278 xbind(sock_fd, &saddr, sizeof(saddr));
Denis Vlasenko035aae52006-09-03 12:23:56 +0000279
280 // get the interface's ethernet address
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000281 //memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000282 strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000283 xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000284 memcpy(&eth_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000285
Denis Vlasenko035aae52006-09-03 12:23:56 +0000286 // start with some stable ip address, either a function of
287 // the hardware address or else the last address we used.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000288 // we are taking low-order four bytes, as top-order ones
289 // aren't random enough.
Denis Vlasenko035aae52006-09-03 12:23:56 +0000290 // NOTE: the sequence of addresses we try changes only
291 // depending on when we detect conflicts.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000292 {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000293 uint32_t t;
294 move_from_unaligned32(t, ((char *)&eth_addr + 2));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000295 srand(t);
296 }
Denis Vlasenko035aae52006-09-03 12:23:56 +0000297 if (ip.s_addr == 0)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000298 ip.s_addr = pick();
Rob Landleyad8071f2005-04-30 03:49:37 +0000299
Rob Landleye3752e52005-05-03 03:28:55 +0000300 // FIXME cases to handle:
301 // - zcip already running!
302 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000303
Rob Landleye3752e52005-05-03 03:28:55 +0000304 // daemonize now; don't delay system startup
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000305 if (!FOREGROUND) {
Denis Vlasenko473dae02007-04-11 07:04:23 +0000306#if BB_MMU
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000307 bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000308#endif
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000309 bb_info_msg("start, interface %s", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000310 }
311
Rob Landleye3752e52005-05-03 03:28:55 +0000312 // run the dynamic address negotiation protocol,
313 // restarting after address conflicts:
314 // - start with some address we want to try
315 // - short random delay
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000316 // - arp probes to see if another host uses it
Rob Landleye3752e52005-05-03 03:28:55 +0000317 // - arp announcements that we're claiming it
318 // - use it
319 // - defend it, within limits
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000320 // exit if:
321 // - address is successfully obtained and -q was given:
322 // run "<script> config", then exit with exitcode 0
323 // - poll error (when does this happen?)
324 // - read error (when does this happen?)
325 // - sendto error (in arp()) (when does this happen?)
326 // - revents & POLLERR (link down). run "<script> deconfig" first
327 state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000328 while (1) {
329 struct pollfd fds[1];
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000330 unsigned deadline_us;
Rob Landleyad8071f2005-04-30 03:49:37 +0000331 struct arp_packet p;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000332 int source_ip_conflict;
333 int target_ip_conflict;
Rob Landleyad8071f2005-04-30 03:49:37 +0000334
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000335 fds[0].fd = sock_fd;
Rob Landleyad8071f2005-04-30 03:49:37 +0000336 fds[0].events = POLLIN;
337 fds[0].revents = 0;
338
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000339 // poll, being ready to adjust current timeout
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000340 if (!timeout_ms) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000341 timeout_ms = random_delay_ms(PROBE_WAIT);
342 // FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to
Rob Landleye3752e52005-05-03 03:28:55 +0000343 // make the kernel filter out all packets except
344 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000345 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000346 // set deadline_us to the point in time when we timeout
347 deadline_us = MONOTONIC_US() + timeout_ms * 1000;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000348
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000349 VDBG("...wait %d %s nprobes=%u, nclaims=%u\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000350 timeout_ms, argv_intf, nprobes, nclaims);
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000351
352 switch (safe_poll(fds, 1, timeout_ms)) {
353
354 default:
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000355 //bb_perror_msg("poll"); - done in safe_poll
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000356 return EXIT_FAILURE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000357
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000358 // timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000359 case 0:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000360 VDBG("state = %d\n", state);
361 switch (state) {
362 case PROBE:
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000363 // timeouts in the PROBE state mean no conflicting ARP packets
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000364 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000365 if (nprobes < PROBE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000366 nprobes++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000367 VDBG("probe/%u %s@%s\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000368 nprobes, argv_intf, inet_ntoa(ip));
369 arp(/* ARPOP_REQUEST, */
370 /* &eth_addr, */ null_ip,
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000371 &null_addr, ip);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000372 timeout_ms = PROBE_MIN * 1000;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000373 timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000374 }
375 else {
376 // Switch to announce state.
377 state = ANNOUNCE;
378 nclaims = 0;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000379 VDBG("announce/%u %s@%s\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000380 nclaims, argv_intf, inet_ntoa(ip));
381 arp(/* ARPOP_REQUEST, */
382 /* &eth_addr, */ ip,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000383 &eth_addr, ip);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000384 timeout_ms = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000385 }
386 break;
387 case RATE_LIMIT_PROBE:
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000388 // timeouts in the RATE_LIMIT_PROBE state mean no conflicting ARP packets
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000389 // have been received, so we can move immediately to the announce state
390 state = ANNOUNCE;
391 nclaims = 0;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000392 VDBG("announce/%u %s@%s\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000393 nclaims, argv_intf, inet_ntoa(ip));
394 arp(/* ARPOP_REQUEST, */
395 /* &eth_addr, */ ip,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000396 &eth_addr, ip);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000397 timeout_ms = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000398 break;
399 case ANNOUNCE:
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000400 // timeouts in the ANNOUNCE state mean no conflicting ARP packets
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000401 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000402 if (nclaims < ANNOUNCE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000403 nclaims++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000404 VDBG("announce/%u %s@%s\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000405 nclaims, argv_intf, inet_ntoa(ip));
406 arp(/* ARPOP_REQUEST, */
407 /* &eth_addr, */ ip,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000408 &eth_addr, ip);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000409 timeout_ms = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000410 }
411 else {
412 // Switch to monitor state.
413 state = MONITOR;
Rob Landleye3752e52005-05-03 03:28:55 +0000414 // link is ok to use earlier
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000415 // FIXME update filters
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000416 run(argv, "config", &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000417 ready = 1;
418 conflicts = 0;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000419 timeout_ms = -1; // Never timeout in the monitor state.
Rob Landleyad8071f2005-04-30 03:49:37 +0000420
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000421 // NOTE: all other exit paths
Rob Landleye3752e52005-05-03 03:28:55 +0000422 // should deconfig ...
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000423 if (QUIT)
Rob Landleyad8071f2005-04-30 03:49:37 +0000424 return EXIT_SUCCESS;
Rob Landleyad8071f2005-04-30 03:49:37 +0000425 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000426 break;
427 case DEFEND:
428 // We won! No ARP replies, so just go back to monitor.
429 state = MONITOR;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000430 timeout_ms = -1;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000431 conflicts = 0;
432 break;
433 default:
434 // Invalid, should never happen. Restart the whole protocol.
435 state = PROBE;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000436 ip.s_addr = pick();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000437 timeout_ms = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000438 nprobes = 0;
439 nclaims = 0;
440 break;
441 } // switch (state)
442 break; // case 0 (timeout)
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000443
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000444 // packets arriving, or link went down
Rob Landleyad8071f2005-04-30 03:49:37 +0000445 case 1:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000446 // We need to adjust the timeout in case we didn't receive
447 // a conflicting packet.
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000448 if (timeout_ms > 0) {
449 unsigned diff = deadline_us - MONOTONIC_US();
450 if ((int)(diff) < 0) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000451 // Current time is greater than the expected timeout time.
452 // Should never happen.
453 VDBG("missed an expected timeout\n");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000454 timeout_ms = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000455 } else {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000456 VDBG("adjusting timeout\n");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000457 timeout_ms = (diff / 1000) | 1; /* never 0 */
Rob Landleyad8071f2005-04-30 03:49:37 +0000458 }
459 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000460
Rob Landleyad8071f2005-04-30 03:49:37 +0000461 if ((fds[0].revents & POLLIN) == 0) {
462 if (fds[0].revents & POLLERR) {
Rob Landleye3752e52005-05-03 03:28:55 +0000463 // FIXME: links routinely go down;
464 // this shouldn't necessarily exit.
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000465 bb_error_msg("iface %s is down", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000466 if (ready) {
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000467 run(argv, "deconfig", &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000468 }
469 return EXIT_FAILURE;
470 }
471 continue;
472 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000473
Rob Landleye3752e52005-05-03 03:28:55 +0000474 // read ARP packet
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000475 if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
Denis Vlasenkob1bac0d2007-11-25 12:53:25 +0000476 bb_perror_msg_and_die(bb_msg_read_error);
Rob Landleyad8071f2005-04-30 03:49:37 +0000477 }
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000478 if (p.eth.ether_type != htons(ETHERTYPE_ARP))
Rob Landleyad8071f2005-04-30 03:49:37 +0000479 continue;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000480#ifdef DEBUG
481 {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000482 struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
483 struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha;
484 struct in_addr *spa = (struct in_addr *) p.arp.arp_spa;
485 struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000486 VDBG("%s recv arp type=%d, op=%d,\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000487 argv_intf, ntohs(p.eth.ether_type),
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000488 ntohs(p.arp.arp_op));
489 VDBG("\tsource=%s %s\n",
490 ether_ntoa(sha),
491 inet_ntoa(*spa));
492 VDBG("\ttarget=%s %s\n",
493 ether_ntoa(tha),
494 inet_ntoa(*tpa));
495 }
496#endif
497 if (p.arp.arp_op != htons(ARPOP_REQUEST)
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000498 && p.arp.arp_op != htons(ARPOP_REPLY))
Rob Landleyad8071f2005-04-30 03:49:37 +0000499 continue;
500
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000501 source_ip_conflict = 0;
502 target_ip_conflict = 0;
503
504 if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0
505 && memcmp(&p.arp.arp_sha, &eth_addr, ETH_ALEN) != 0
506 ) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000507 source_ip_conflict = 1;
508 }
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000509 if (p.arp.arp_op == htons(ARPOP_REQUEST)
510 && memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0
511 && memcmp(&p.arp.arp_tha, &eth_addr, ETH_ALEN) != 0
512 ) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000513 target_ip_conflict = 1;
514 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000515
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000516 VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000517 state, source_ip_conflict, target_ip_conflict);
518 switch (state) {
519 case PROBE:
520 case ANNOUNCE:
521 // When probing or announcing, check for source IP conflicts
522 // and other hosts doing ARP probes (target IP conflicts).
523 if (source_ip_conflict || target_ip_conflict) {
524 conflicts++;
525 if (conflicts >= MAX_CONFLICTS) {
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000526 VDBG("%s ratelimit\n", argv_intf);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000527 timeout_ms = RATE_LIMIT_INTERVAL * 1000;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000528 state = RATE_LIMIT_PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000529 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000530
531 // restart the whole protocol
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000532 ip.s_addr = pick();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000533 timeout_ms = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000534 nprobes = 0;
535 nclaims = 0;
536 }
537 break;
538 case MONITOR:
539 // If a conflict, we try to defend with a single ARP probe.
540 if (source_ip_conflict) {
541 VDBG("monitor conflict -- defending\n");
542 state = DEFEND;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000543 timeout_ms = DEFEND_INTERVAL * 1000;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000544 arp(/* ARPOP_REQUEST, */
545 /* &eth_addr, */ ip,
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000546 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000547 }
548 break;
549 case DEFEND:
550 // Well, we tried. Start over (on conflict).
551 if (source_ip_conflict) {
552 state = PROBE;
553 VDBG("defend conflict -- starting over\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000554 ready = 0;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000555 run(argv, "deconfig", &ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000556
557 // restart the whole protocol
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000558 ip.s_addr = pick();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000559 timeout_ms = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000560 nprobes = 0;
561 nclaims = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000562 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000563 break;
564 default:
565 // Invalid, should never happen. Restart the whole protocol.
566 VDBG("invalid state -- starting over\n");
567 state = PROBE;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000568 ip.s_addr = pick();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000569 timeout_ms = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000570 nprobes = 0;
571 nclaims = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000572 break;
573 } // switch state
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000574 break; // case 1 (packets arriving)
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000575 } // switch poll
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000576 } // while (1)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000577#undef argv_intf
Rob Landleyad8071f2005-04-30 03:49:37 +0000578}