blob: 962ba2e6059c4c6e8785bc36b34c22df47051006 [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"
Michel Stamfdd957b2014-10-30 13:43:09 +010033//usage: "\n -l x.x.0.0 Use this range instead of 169.254"
Pere Orga5bc8c002011-04-11 03:29:49 +020034//usage: "\n -v Verbose"
35//usage: "\n"
Michel Stamd3fabf82014-11-04 12:19:04 +010036//usage: "\n$LOGGING=none Suppress logging"
37//usage: "\n$LOGGING=syslog Log to syslog"
38//usage: "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020039//usage: "\nWith no -q, runs continuously monitoring for ARP conflicts,"
40//usage: "\nexits only on I/O errors (link down etc)"
41
Dan Fandrichf533ec82011-06-10 05:17:59 +020042#include "libbb.h"
Rob Landleyad8071f2005-04-30 03:49:37 +000043#include <netinet/ether.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000044#include <net/if.h>
45#include <net/if_arp.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000046#include <linux/sockios.h>
Rob Landleye3752e52005-05-03 03:28:55 +000047
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000048#include <syslog.h>
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000049
50/* We don't need more than 32 bits of the counter */
51#define MONOTONIC_US() ((unsigned)monotonic_us())
Rob Landleyad8071f2005-04-30 03:49:37 +000052
Bernhard Reutner-Fischerec351c32005-12-13 10:28:25 +000053struct arp_packet {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000054 struct ether_header eth;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000055 struct ether_arp arp;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000056} PACKED;
Rob Landleyad8071f2005-04-30 03:49:37 +000057
Rob Landleybc68cd12006-03-10 19:22:06 +000058enum {
Rob Landleyad8071f2005-04-30 03:49:37 +000059/* 169.254.0.0 */
Rob Landleybc68cd12006-03-10 19:22:06 +000060 LINKLOCAL_ADDR = 0xa9fe0000,
Rob Landleyad8071f2005-04-30 03:49:37 +000061
62/* protocol timeout parameters, specified in seconds */
Rob Landleybc68cd12006-03-10 19:22:06 +000063 PROBE_WAIT = 1,
64 PROBE_MIN = 1,
65 PROBE_MAX = 2,
66 PROBE_NUM = 3,
67 MAX_CONFLICTS = 10,
68 RATE_LIMIT_INTERVAL = 60,
69 ANNOUNCE_WAIT = 2,
70 ANNOUNCE_NUM = 2,
71 ANNOUNCE_INTERVAL = 2,
72 DEFEND_INTERVAL = 10
73};
Rob Landleyad8071f2005-04-30 03:49:37 +000074
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000075/* States during the configuration process. */
76enum {
77 PROBE = 0,
78 RATE_LIMIT_PROBE,
79 ANNOUNCE,
80 MONITOR,
81 DEFEND
82};
Rob Landleyad8071f2005-04-30 03:49:37 +000083
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000084#define VDBG(...) do { } while (0)
85
86
87enum {
88 sock_fd = 3
89};
90
91struct globals {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000092 struct sockaddr saddr;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +000093 struct ether_addr eth_addr;
Michel Stamfdd957b2014-10-30 13:43:09 +010094 uint32_t localnet_ip;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010095} FIX_ALIASING;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000096#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +000097#define saddr (G.saddr )
98#define eth_addr (G.eth_addr)
Denys Vlasenko16714242011-09-21 01:59:15 +020099#define INIT_G() do { } while (0)
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000100
Rob Landleye3752e52005-05-03 03:28:55 +0000101
102/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000103 * Pick a random link local IP address on 169.254/16, except that
104 * the first and last 256 addresses are reserved.
105 */
Michel Stamfdd957b2014-10-30 13:43:09 +0100106static uint32_t pick_nip(void)
Rob Landleyad8071f2005-04-30 03:49:37 +0000107{
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000108 unsigned tmp;
Rob Landleyad8071f2005-04-30 03:49:37 +0000109
Rob Landleyad8071f2005-04-30 03:49:37 +0000110 do {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000111 tmp = rand() & IN_CLASSB_HOST;
Rob Landleyad8071f2005-04-30 03:49:37 +0000112 } while (tmp > (IN_CLASSB_HOST - 0x0200));
Michel Stamfdd957b2014-10-30 13:43:09 +0100113 return htonl((G.localnet_ip + 0x0100) + tmp);
Rob Landleyad8071f2005-04-30 03:49:37 +0000114}
115
Rob Landleye3752e52005-05-03 03:28:55 +0000116/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000117 * Broadcast an ARP packet.
118 */
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000119static void arp(
120 /* int op, - always ARPOP_REQUEST */
121 /* const struct ether_addr *source_eth, - always &eth_addr */
122 struct in_addr source_ip,
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000123 const struct ether_addr *target_eth, struct in_addr target_ip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000124{
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000125 enum { op = ARPOP_REQUEST };
126#define source_eth (&eth_addr)
127
Rob Landleyad8071f2005-04-30 03:49:37 +0000128 struct arp_packet p;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000129 memset(&p, 0, sizeof(p));
Rob Landleyad8071f2005-04-30 03:49:37 +0000130
Rob Landleye3752e52005-05-03 03:28:55 +0000131 // ether header
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000132 p.eth.ether_type = htons(ETHERTYPE_ARP);
133 memcpy(p.eth.ether_shost, source_eth, ETH_ALEN);
134 memset(p.eth.ether_dhost, 0xff, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000135
Rob Landleye3752e52005-05-03 03:28:55 +0000136 // arp request
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000137 p.arp.arp_hrd = htons(ARPHRD_ETHER);
138 p.arp.arp_pro = htons(ETHERTYPE_IP);
139 p.arp.arp_hln = ETH_ALEN;
140 p.arp.arp_pln = 4;
141 p.arp.arp_op = htons(op);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000142 memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN);
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000143 memcpy(&p.arp.arp_spa, &source_ip, sizeof(p.arp.arp_spa));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000144 memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000145 memcpy(&p.arp.arp_tpa, &target_ip, sizeof(p.arp.arp_tpa));
Rob Landleyad8071f2005-04-30 03:49:37 +0000146
Rob Landleye3752e52005-05-03 03:28:55 +0000147 // send it
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000148 // Even though sock_fd is already bound to saddr, just send()
149 // won't work, because "socket is not connected"
150 // (and connect() won't fix that, "operation not supported").
151 // Thus we sendto() to saddr. I wonder which sockaddr
152 // (from bind() or from sendto()?) kernel actually uses
153 // to determine iface to emit the packet from...
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000154 xsendto(sock_fd, &p, sizeof(p), &saddr, sizeof(saddr));
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000155#undef source_eth
Rob Landleyad8071f2005-04-30 03:49:37 +0000156}
157
Rob Landleye3752e52005-05-03 03:28:55 +0000158/**
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000159 * Run a script.
160 * argv[0]:intf argv[1]:script_name argv[2]:junk argv[3]:NULL
Rob Landleyad8071f2005-04-30 03:49:37 +0000161 */
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000162static int run(char *argv[3], const char *param, struct in_addr *ip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000163{
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000164 int status;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000165 char *addr = addr; /* for gcc */
166 const char *fmt = "%s %s %s" + 3;
Rob Landleyad8071f2005-04-30 03:49:37 +0000167
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000168 argv[2] = (char*)param;
169
170 VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]);
Rob Landleyad8071f2005-04-30 03:49:37 +0000171
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000172 if (ip) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000173 addr = inet_ntoa(*ip);
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000174 xsetenv("ip", addr);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000175 fmt -= 3;
Rob Landleyad8071f2005-04-30 03:49:37 +0000176 }
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000177 bb_info_msg(fmt, argv[2], argv[0], addr);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000178
Denys Vlasenko8531d762010-03-18 22:44:00 +0100179 status = spawn_and_wait(argv + 1);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000180 if (status < 0) {
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000181 bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000182 return -errno;
183 }
184 if (status != 0)
Denys Vlasenko8531d762010-03-18 22:44:00 +0100185 bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
Rob Landleyad8071f2005-04-30 03:49:37 +0000186 return status;
187}
188
Rob Landleye3752e52005-05-03 03:28:55 +0000189/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000190 * Return milliseconds of random delay, up to "secs" seconds.
191 */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000192static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000193{
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000194 return rand() % (secs * 1000);
Rob Landleyad8071f2005-04-30 03:49:37 +0000195}
196
Rob Landleye3752e52005-05-03 03:28:55 +0000197/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000198 * main program
199 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000200int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100201int zcip_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyad8071f2005-04-30 03:49:37 +0000202{
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000203 int state;
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000204 char *r_opt;
Michel Stamfdd957b2014-10-30 13:43:09 +0100205 const char *l_opt = "169.254.0.0";
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000206 unsigned opts;
207
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000208 // ugly trick, but I want these zeroed in one go
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000209 struct {
210 const struct in_addr null_ip;
211 const struct ether_addr null_addr;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000212 struct in_addr ip;
213 struct ifreq ifr;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000214 int timeout_ms; /* must be signed */
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000215 unsigned conflicts;
216 unsigned nprobes;
217 unsigned nclaims;
218 int ready;
219 int verbose;
220 } L;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000221#define null_ip (L.null_ip )
222#define null_addr (L.null_addr )
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000223#define ip (L.ip )
224#define ifr (L.ifr )
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000225#define timeout_ms (L.timeout_ms)
226#define conflicts (L.conflicts )
227#define nprobes (L.nprobes )
228#define nclaims (L.nclaims )
229#define ready (L.ready )
230#define verbose (L.verbose )
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000231
232 memset(&L, 0, sizeof(L));
Denys Vlasenko16714242011-09-21 01:59:15 +0200233 INIT_G();
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000234
235#define FOREGROUND (opts & 1)
236#define QUIT (opts & 2)
237 // parse commandline: prog [options] ifname script
238 // exactly 2 args; -v accumulates and implies -f
239 opt_complementary = "=2:vv:vf";
Michel Stamfdd957b2014-10-30 13:43:09 +0100240 opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose);
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000241#if !BB_MMU
242 // on NOMMU reexec early (or else we will rerun things twice)
243 if (!FOREGROUND)
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000244 bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv);
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000245#endif
246 // open an ARP socket
247 // (need to do it before openlog to prevent openlog from taking
248 // fd 3 (sock_fd==3))
249 xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000250 if (!FOREGROUND) {
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000251 // do it before all bb_xx_msg calls
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000252 openlog(applet_name, 0, LOG_DAEMON);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000253 logmode |= LOGMODE_SYSLOG;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000254 }
Michel Stamd3fabf82014-11-04 12:19:04 +0100255 bb_logenv_override();
256
Michel Stamfdd957b2014-10-30 13:43:09 +0100257 { // -l n.n.n.n
258 struct in_addr net;
259 if (inet_aton(l_opt, &net) == 0
260 || (net.s_addr & htonl(IN_CLASSB_NET)) != net.s_addr
261 ) {
262 bb_error_msg_and_die("invalid network address");
263 }
264 G.localnet_ip = ntohl(net.s_addr);
265 }
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000266 if (opts & 4) { // -r n.n.n.n
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000267 if (inet_aton(r_opt, &ip) == 0
Michel Stamfdd957b2014-10-30 13:43:09 +0100268 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != G.localnet_ip
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000269 ) {
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000270 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000271 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000272 }
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000273 argv += optind - 1;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000274
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000275 /* Now: argv[0]:junk argv[1]:intf argv[2]:script argv[3]:NULL */
276 /* We need to make space for script argument: */
277 argv[0] = argv[1];
278 argv[1] = argv[2];
279 /* Now: argv[0]:intf argv[1]:script argv[2]:junk argv[3]:NULL */
280#define argv_intf (argv[0])
281
282 xsetenv("interface", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000283
Rob Landleye3752e52005-05-03 03:28:55 +0000284 // initialize the interface (modprobe, ifup, etc)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000285 if (run(argv, "init", NULL))
Rob Landleyad8071f2005-04-30 03:49:37 +0000286 return EXIT_FAILURE;
287
Rob Landleye3752e52005-05-03 03:28:55 +0000288 // initialize saddr
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000289 // saddr is: { u16 sa_family; u8 sa_data[14]; }
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000290 //memset(&saddr, 0, sizeof(saddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000291 //TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000292 safe_strncpy(saddr.sa_data, argv_intf, sizeof(saddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000293
Denis Vlasenko035aae52006-09-03 12:23:56 +0000294 // bind to the interface's ARP socket
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000295 xbind(sock_fd, &saddr, sizeof(saddr));
Denis Vlasenko035aae52006-09-03 12:23:56 +0000296
297 // get the interface's ethernet address
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000298 //memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000299 strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000300 xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000301 memcpy(&eth_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000302
Denis Vlasenko035aae52006-09-03 12:23:56 +0000303 // start with some stable ip address, either a function of
304 // the hardware address or else the last address we used.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000305 // we are taking low-order four bytes, as top-order ones
306 // aren't random enough.
Denis Vlasenko035aae52006-09-03 12:23:56 +0000307 // NOTE: the sequence of addresses we try changes only
308 // depending on when we detect conflicts.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000309 {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000310 uint32_t t;
311 move_from_unaligned32(t, ((char *)&eth_addr + 2));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000312 srand(t);
313 }
Denis Vlasenko035aae52006-09-03 12:23:56 +0000314 if (ip.s_addr == 0)
Michel Stamfdd957b2014-10-30 13:43:09 +0100315 ip.s_addr = pick_nip();
Rob Landleyad8071f2005-04-30 03:49:37 +0000316
Rob Landleye3752e52005-05-03 03:28:55 +0000317 // FIXME cases to handle:
318 // - zcip already running!
319 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000320
Rob Landleye3752e52005-05-03 03:28:55 +0000321 // daemonize now; don't delay system startup
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000322 if (!FOREGROUND) {
Denis Vlasenko473dae02007-04-11 07:04:23 +0000323#if BB_MMU
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000324 bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000325#endif
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000326 bb_info_msg("start, interface %s", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000327 }
328
Rob Landleye3752e52005-05-03 03:28:55 +0000329 // run the dynamic address negotiation protocol,
330 // restarting after address conflicts:
331 // - start with some address we want to try
332 // - short random delay
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000333 // - arp probes to see if another host uses it
Rob Landleye3752e52005-05-03 03:28:55 +0000334 // - arp announcements that we're claiming it
335 // - use it
336 // - defend it, within limits
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000337 // exit if:
338 // - address is successfully obtained and -q was given:
339 // run "<script> config", then exit with exitcode 0
340 // - poll error (when does this happen?)
341 // - read error (when does this happen?)
342 // - sendto error (in arp()) (when does this happen?)
343 // - revents & POLLERR (link down). run "<script> deconfig" first
344 state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000345 while (1) {
346 struct pollfd fds[1];
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000347 unsigned deadline_us;
Rob Landleyad8071f2005-04-30 03:49:37 +0000348 struct arp_packet p;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000349 int source_ip_conflict;
350 int target_ip_conflict;
Rob Landleyad8071f2005-04-30 03:49:37 +0000351
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000352 fds[0].fd = sock_fd;
Rob Landleyad8071f2005-04-30 03:49:37 +0000353 fds[0].events = POLLIN;
354 fds[0].revents = 0;
355
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000356 // poll, being ready to adjust current timeout
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000357 if (!timeout_ms) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000358 timeout_ms = random_delay_ms(PROBE_WAIT);
359 // FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to
Rob Landleye3752e52005-05-03 03:28:55 +0000360 // make the kernel filter out all packets except
361 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000362 }
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000363 // set deadline_us to the point in time when we timeout
364 deadline_us = MONOTONIC_US() + timeout_ms * 1000;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000365
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000366 VDBG("...wait %d %s nprobes=%u, nclaims=%u\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000367 timeout_ms, argv_intf, nprobes, nclaims);
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000368
369 switch (safe_poll(fds, 1, timeout_ms)) {
370
371 default:
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000372 //bb_perror_msg("poll"); - done in safe_poll
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000373 return EXIT_FAILURE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000374
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000375 // timeout
Rob Landleyad8071f2005-04-30 03:49:37 +0000376 case 0:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000377 VDBG("state = %d\n", state);
378 switch (state) {
379 case PROBE:
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000380 // timeouts in the PROBE state mean no conflicting ARP packets
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000381 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000382 if (nprobes < PROBE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000383 nprobes++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000384 VDBG("probe/%u %s@%s\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000385 nprobes, argv_intf, inet_ntoa(ip));
Ken Sharpa4d564a2014-07-20 14:01:49 +0200386 timeout_ms = PROBE_MIN * 1000;
387 timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000388 arp(/* ARPOP_REQUEST, */
389 /* &eth_addr, */ null_ip,
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000390 &null_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000391 }
392 else {
393 // Switch to announce state.
394 state = ANNOUNCE;
395 nclaims = 0;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000396 VDBG("announce/%u %s@%s\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000397 nclaims, argv_intf, inet_ntoa(ip));
Ken Sharpa4d564a2014-07-20 14:01:49 +0200398 timeout_ms = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000399 arp(/* ARPOP_REQUEST, */
400 /* &eth_addr, */ ip,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000401 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000402 }
403 break;
404 case RATE_LIMIT_PROBE:
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000405 // timeouts in the RATE_LIMIT_PROBE state mean no conflicting ARP packets
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000406 // have been received, so we can move immediately to the announce state
407 state = ANNOUNCE;
408 nclaims = 0;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000409 VDBG("announce/%u %s@%s\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000410 nclaims, argv_intf, inet_ntoa(ip));
Ken Sharpa4d564a2014-07-20 14:01:49 +0200411 timeout_ms = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000412 arp(/* ARPOP_REQUEST, */
413 /* &eth_addr, */ ip,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000414 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000415 break;
416 case ANNOUNCE:
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000417 // timeouts in the ANNOUNCE state mean no conflicting ARP packets
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000418 // have been received, so we can progress through the states
Rob Landleyad8071f2005-04-30 03:49:37 +0000419 if (nclaims < ANNOUNCE_NUM) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000420 nclaims++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000421 VDBG("announce/%u %s@%s\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000422 nclaims, argv_intf, inet_ntoa(ip));
Ken Sharpa4d564a2014-07-20 14:01:49 +0200423 timeout_ms = ANNOUNCE_INTERVAL * 1000;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000424 arp(/* ARPOP_REQUEST, */
425 /* &eth_addr, */ ip,
Denis Vlasenko60e3dd62006-09-03 12:26:06 +0000426 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000427 }
428 else {
429 // Switch to monitor state.
430 state = MONITOR;
Rob Landleye3752e52005-05-03 03:28:55 +0000431 // link is ok to use earlier
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000432 // FIXME update filters
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000433 run(argv, "config", &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000434 ready = 1;
435 conflicts = 0;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000436 timeout_ms = -1; // Never timeout in the monitor state.
Rob Landleyad8071f2005-04-30 03:49:37 +0000437
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000438 // NOTE: all other exit paths
Rob Landleye3752e52005-05-03 03:28:55 +0000439 // should deconfig ...
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000440 if (QUIT)
Rob Landleyad8071f2005-04-30 03:49:37 +0000441 return EXIT_SUCCESS;
Rob Landleyad8071f2005-04-30 03:49:37 +0000442 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000443 break;
444 case DEFEND:
445 // We won! No ARP replies, so just go back to monitor.
446 state = MONITOR;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000447 timeout_ms = -1;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000448 conflicts = 0;
449 break;
450 default:
451 // Invalid, should never happen. Restart the whole protocol.
452 state = PROBE;
Michel Stamfdd957b2014-10-30 13:43:09 +0100453 ip.s_addr = pick_nip();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000454 timeout_ms = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000455 nprobes = 0;
456 nclaims = 0;
457 break;
458 } // switch (state)
459 break; // case 0 (timeout)
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000460
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000461 // packets arriving, or link went down
Rob Landleyad8071f2005-04-30 03:49:37 +0000462 case 1:
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000463 // We need to adjust the timeout in case we didn't receive
464 // a conflicting packet.
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000465 if (timeout_ms > 0) {
466 unsigned diff = deadline_us - MONOTONIC_US();
467 if ((int)(diff) < 0) {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000468 // Current time is greater than the expected timeout time.
469 // Should never happen.
470 VDBG("missed an expected timeout\n");
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000471 timeout_ms = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000472 } else {
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000473 VDBG("adjusting timeout\n");
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000474 timeout_ms = (diff / 1000) | 1; /* never 0 */
Rob Landleyad8071f2005-04-30 03:49:37 +0000475 }
476 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000477
Rob Landleyad8071f2005-04-30 03:49:37 +0000478 if ((fds[0].revents & POLLIN) == 0) {
479 if (fds[0].revents & POLLERR) {
Rob Landleye3752e52005-05-03 03:28:55 +0000480 // FIXME: links routinely go down;
481 // this shouldn't necessarily exit.
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000482 bb_error_msg("iface %s is down", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000483 if (ready) {
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000484 run(argv, "deconfig", &ip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000485 }
486 return EXIT_FAILURE;
487 }
488 continue;
489 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000490
Rob Landleye3752e52005-05-03 03:28:55 +0000491 // read ARP packet
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000492 if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
Denis Vlasenkob1bac0d2007-11-25 12:53:25 +0000493 bb_perror_msg_and_die(bb_msg_read_error);
Rob Landleyad8071f2005-04-30 03:49:37 +0000494 }
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000495 if (p.eth.ether_type != htons(ETHERTYPE_ARP))
Rob Landleyad8071f2005-04-30 03:49:37 +0000496 continue;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000497#ifdef DEBUG
498 {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000499 struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
500 struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha;
501 struct in_addr *spa = (struct in_addr *) p.arp.arp_spa;
502 struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000503 VDBG("%s recv arp type=%d, op=%d,\n",
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000504 argv_intf, ntohs(p.eth.ether_type),
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000505 ntohs(p.arp.arp_op));
506 VDBG("\tsource=%s %s\n",
507 ether_ntoa(sha),
508 inet_ntoa(*spa));
509 VDBG("\ttarget=%s %s\n",
510 ether_ntoa(tha),
511 inet_ntoa(*tpa));
512 }
513#endif
514 if (p.arp.arp_op != htons(ARPOP_REQUEST)
Ken Sharpa4d564a2014-07-20 14:01:49 +0200515 && p.arp.arp_op != htons(ARPOP_REPLY)
516 ) {
Rob Landleyad8071f2005-04-30 03:49:37 +0000517 continue;
Ken Sharpa4d564a2014-07-20 14:01:49 +0200518 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000519
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000520 source_ip_conflict = 0;
521 target_ip_conflict = 0;
522
Ken Sharpa4d564a2014-07-20 14:01:49 +0200523 if (memcmp(&p.arp.arp_sha, &eth_addr, ETH_ALEN) != 0) {
Vladislav Grishenko4d150682015-03-19 16:19:35 +0500524 if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0) {
Ken Sharpa4d564a2014-07-20 14:01:49 +0200525 /* A probe or reply with source_ip == chosen ip */
526 source_ip_conflict = 1;
527 }
528 if (p.arp.arp_op == htons(ARPOP_REQUEST)
529 && memcmp(p.arp.arp_spa, &null_ip, sizeof(struct in_addr)) == 0
530 && memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0
531 ) {
532 /* A probe with source_ip == 0.0.0.0, target_ip == chosen ip:
533 * another host trying to claim this ip!
534 */
535 target_ip_conflict = 1;
536 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000537 }
Rob Landleyad8071f2005-04-30 03:49:37 +0000538
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000539 VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000540 state, source_ip_conflict, target_ip_conflict);
541 switch (state) {
542 case PROBE:
543 case ANNOUNCE:
544 // When probing or announcing, check for source IP conflicts
545 // and other hosts doing ARP probes (target IP conflicts).
546 if (source_ip_conflict || target_ip_conflict) {
547 conflicts++;
548 if (conflicts >= MAX_CONFLICTS) {
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000549 VDBG("%s ratelimit\n", argv_intf);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000550 timeout_ms = RATE_LIMIT_INTERVAL * 1000;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000551 state = RATE_LIMIT_PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000552 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000553
554 // restart the whole protocol
Michel Stamfdd957b2014-10-30 13:43:09 +0100555 ip.s_addr = pick_nip();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000556 timeout_ms = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000557 nprobes = 0;
558 nclaims = 0;
559 }
560 break;
561 case MONITOR:
562 // If a conflict, we try to defend with a single ARP probe.
563 if (source_ip_conflict) {
564 VDBG("monitor conflict -- defending\n");
565 state = DEFEND;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000566 timeout_ms = DEFEND_INTERVAL * 1000;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000567 arp(/* ARPOP_REQUEST, */
568 /* &eth_addr, */ ip,
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000569 &eth_addr, ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000570 }
571 break;
572 case DEFEND:
573 // Well, we tried. Start over (on conflict).
574 if (source_ip_conflict) {
575 state = PROBE;
576 VDBG("defend conflict -- starting over\n");
Rob Landleyad8071f2005-04-30 03:49:37 +0000577 ready = 0;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000578 run(argv, "deconfig", &ip);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000579
580 // restart the whole protocol
Michel Stamfdd957b2014-10-30 13:43:09 +0100581 ip.s_addr = pick_nip();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000582 timeout_ms = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000583 nprobes = 0;
584 nclaims = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000585 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000586 break;
587 default:
588 // Invalid, should never happen. Restart the whole protocol.
589 VDBG("invalid state -- starting over\n");
590 state = PROBE;
Michel Stamfdd957b2014-10-30 13:43:09 +0100591 ip.s_addr = pick_nip();
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000592 timeout_ms = 0;
Rob Landleyad8071f2005-04-30 03:49:37 +0000593 nprobes = 0;
594 nclaims = 0;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000595 break;
596 } // switch state
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000597 break; // case 1 (packets arriving)
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000598 } // switch poll
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000599 } // while (1)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000600#undef argv_intf
Rob Landleyad8071f2005-04-30 03:49:37 +0000601}