blob: 9122bd6819a50df72904db840c0ab4abbe0ee9d4 [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 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010017//config:config ZCIP
18//config: bool "zcip"
19//config: default y
20//config: select PLATFORM_LINUX
21//config: select FEATURE_SYSLOG
22//config: help
23//config: ZCIP provides ZeroConf IPv4 address selection, according to RFC 3927.
24//config: It's a daemon that allocates and defends a dynamically assigned
25//config: address on the 169.254/16 network, requiring no system administrator.
26//config:
27//config: See http://www.zeroconf.org for further details, and "zcip.script"
28//config: in the busybox examples.
29
30//applet:IF_ZCIP(APPLET(zcip, BB_DIR_SBIN, BB_SUID_DROP))
31
32//kbuild:lib-$(CONFIG_ZCIP) += zcip.o
Rob Landleyad8071f2005-04-30 03:49:37 +000033
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000034//#define DEBUG
Rob Landleye3752e52005-05-03 03:28:55 +000035
36// TODO:
37// - more real-world usage/testing, especially daemon mode
38// - kernel packet filters to reduce scheduling noise
39// - avoid silent script failures, especially under load...
40// - link status monitoring (restart on link-up; stop on link-down)
41
Pere Orga5bc8c002011-04-11 03:29:49 +020042//usage:#define zcip_trivial_usage
43//usage: "[OPTIONS] IFACE SCRIPT"
44//usage:#define zcip_full_usage "\n\n"
45//usage: "Manage a ZeroConf IPv4 link-local address\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020046//usage: "\n -f Run in foreground"
47//usage: "\n -q Quit after obtaining address"
48//usage: "\n -r 169.254.x.x Request this address first"
Michel Stamfdd957b2014-10-30 13:43:09 +010049//usage: "\n -l x.x.0.0 Use this range instead of 169.254"
Pere Orga5bc8c002011-04-11 03:29:49 +020050//usage: "\n -v Verbose"
51//usage: "\n"
Michel Stamd3fabf82014-11-04 12:19:04 +010052//usage: "\n$LOGGING=none Suppress logging"
53//usage: "\n$LOGGING=syslog Log to syslog"
54//usage: "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020055//usage: "\nWith no -q, runs continuously monitoring for ARP conflicts,"
56//usage: "\nexits only on I/O errors (link down etc)"
57
Dan Fandrichf533ec82011-06-10 05:17:59 +020058#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020059#include "common_bufsiz.h"
Rob Landleyad8071f2005-04-30 03:49:37 +000060#include <netinet/ether.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000061#include <net/if.h>
62#include <net/if_arp.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000063#include <linux/sockios.h>
Rob Landleye3752e52005-05-03 03:28:55 +000064
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000065#include <syslog.h>
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000066
67/* We don't need more than 32 bits of the counter */
68#define MONOTONIC_US() ((unsigned)monotonic_us())
Rob Landleyad8071f2005-04-30 03:49:37 +000069
Bernhard Reutner-Fischerec351c32005-12-13 10:28:25 +000070struct arp_packet {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000071 struct ether_header eth;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000072 struct ether_arp arp;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000073} PACKED;
Rob Landleyad8071f2005-04-30 03:49:37 +000074
Rob Landleybc68cd12006-03-10 19:22:06 +000075enum {
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020076 /* 0-1 seconds before sending 1st probe */
Rob Landleybc68cd12006-03-10 19:22:06 +000077 PROBE_WAIT = 1,
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020078 /* 1-2 seconds between probes */
Rob Landleybc68cd12006-03-10 19:22:06 +000079 PROBE_MIN = 1,
80 PROBE_MAX = 2,
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020081 PROBE_NUM = 3, /* total probes to send */
82 ANNOUNCE_INTERVAL = 2, /* 2 seconds between announces */
83 ANNOUNCE_NUM = 3, /* announces to send */
84 /* if probe/announce sees a conflict, multiply RANDOM(NUM_CONFLICT) by... */
85 CONFLICT_MULTIPLIER = 2,
86 /* if we monitor and see a conflict, how long is defend state? */
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +020087 DEFEND_INTERVAL = 10,
Rob Landleybc68cd12006-03-10 19:22:06 +000088};
Rob Landleyad8071f2005-04-30 03:49:37 +000089
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000090/* States during the configuration process. */
91enum {
92 PROBE = 0,
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000093 ANNOUNCE,
94 MONITOR,
95 DEFEND
96};
Rob Landleyad8071f2005-04-30 03:49:37 +000097
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000098#define VDBG(...) do { } while (0)
99
100
101enum {
102 sock_fd = 3
103};
104
105struct globals {
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200106 struct sockaddr iface_sockaddr;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200107 struct ether_addr our_ethaddr;
Michel Stamfdd957b2014-10-30 13:43:09 +0100108 uint32_t localnet_ip;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100109} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200110#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200111#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000112
Rob Landleye3752e52005-05-03 03:28:55 +0000113
114/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000115 * Pick a random link local IP address on 169.254/16, except that
116 * the first and last 256 addresses are reserved.
117 */
Michel Stamfdd957b2014-10-30 13:43:09 +0100118static uint32_t pick_nip(void)
Rob Landleyad8071f2005-04-30 03:49:37 +0000119{
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000120 unsigned tmp;
Rob Landleyad8071f2005-04-30 03:49:37 +0000121
Rob Landleyad8071f2005-04-30 03:49:37 +0000122 do {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000123 tmp = rand() & IN_CLASSB_HOST;
Rob Landleyad8071f2005-04-30 03:49:37 +0000124 } while (tmp > (IN_CLASSB_HOST - 0x0200));
Michel Stamfdd957b2014-10-30 13:43:09 +0100125 return htonl((G.localnet_ip + 0x0100) + tmp);
Rob Landleyad8071f2005-04-30 03:49:37 +0000126}
127
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200128static const char *nip_to_a(uint32_t nip)
129{
130 struct in_addr in;
131 in.s_addr = nip;
132 return inet_ntoa(in);
133}
134
Rob Landleye3752e52005-05-03 03:28:55 +0000135/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000136 * Broadcast an ARP packet.
137 */
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200138static void send_arp_request(
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000139 /* int op, - always ARPOP_REQUEST */
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200140 /* const struct ether_addr *source_eth, - always &G.our_ethaddr */
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200141 uint32_t source_nip,
142 const struct ether_addr *target_eth, uint32_t target_nip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000143{
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000144 enum { op = ARPOP_REQUEST };
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200145#define source_eth (&G.our_ethaddr)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000146
Rob Landleyad8071f2005-04-30 03:49:37 +0000147 struct arp_packet p;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000148 memset(&p, 0, sizeof(p));
Rob Landleyad8071f2005-04-30 03:49:37 +0000149
Rob Landleye3752e52005-05-03 03:28:55 +0000150 // ether header
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000151 p.eth.ether_type = htons(ETHERTYPE_ARP);
152 memcpy(p.eth.ether_shost, source_eth, ETH_ALEN);
153 memset(p.eth.ether_dhost, 0xff, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000154
Rob Landleye3752e52005-05-03 03:28:55 +0000155 // arp request
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000156 p.arp.arp_hrd = htons(ARPHRD_ETHER);
157 p.arp.arp_pro = htons(ETHERTYPE_IP);
158 p.arp.arp_hln = ETH_ALEN;
159 p.arp.arp_pln = 4;
160 p.arp.arp_op = htons(op);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000161 memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN);
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200162 memcpy(&p.arp.arp_spa, &source_nip, 4);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000163 memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200164 memcpy(&p.arp.arp_tpa, &target_nip, 4);
Rob Landleyad8071f2005-04-30 03:49:37 +0000165
Rob Landleye3752e52005-05-03 03:28:55 +0000166 // send it
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200167 // Even though sock_fd is already bound to G.iface_sockaddr, just send()
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000168 // won't work, because "socket is not connected"
169 // (and connect() won't fix that, "operation not supported").
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200170 // Thus we sendto() to G.iface_sockaddr. I wonder which sockaddr
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000171 // (from bind() or from sendto()?) kernel actually uses
172 // to determine iface to emit the packet from...
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200173 xsendto(sock_fd, &p, sizeof(p), &G.iface_sockaddr, sizeof(G.iface_sockaddr));
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000174#undef source_eth
Rob Landleyad8071f2005-04-30 03:49:37 +0000175}
176
Rob Landleye3752e52005-05-03 03:28:55 +0000177/**
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000178 * Run a script.
179 * argv[0]:intf argv[1]:script_name argv[2]:junk argv[3]:NULL
Rob Landleyad8071f2005-04-30 03:49:37 +0000180 */
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200181static int run(char *argv[3], const char *param, uint32_t nip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000182{
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000183 int status;
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200184 const char *addr = addr; /* for gcc */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000185 const char *fmt = "%s %s %s" + 3;
Rob Landleyad8071f2005-04-30 03:49:37 +0000186
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000187 argv[2] = (char*)param;
188
189 VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]);
Rob Landleyad8071f2005-04-30 03:49:37 +0000190
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200191 if (nip != 0) {
192 addr = nip_to_a(nip);
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000193 xsetenv("ip", addr);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000194 fmt -= 3;
Rob Landleyad8071f2005-04-30 03:49:37 +0000195 }
Denys Vlasenkocde11992016-03-30 16:22:13 +0200196 bb_error_msg(fmt, argv[2], argv[0], addr);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000197
Denys Vlasenko8531d762010-03-18 22:44:00 +0100198 status = spawn_and_wait(argv + 1);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000199 if (status < 0) {
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000200 bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000201 return -errno;
202 }
203 if (status != 0)
Denys Vlasenko8531d762010-03-18 22:44:00 +0100204 bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
Rob Landleyad8071f2005-04-30 03:49:37 +0000205 return status;
206}
207
Rob Landleye3752e52005-05-03 03:28:55 +0000208/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000209 * Return milliseconds of random delay, up to "secs" seconds.
210 */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000211static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000212{
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200213 return (unsigned)rand() % (secs * 1000);
Rob Landleyad8071f2005-04-30 03:49:37 +0000214}
215
Rob Landleye3752e52005-05-03 03:28:55 +0000216/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000217 * main program
218 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000219int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100220int zcip_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyad8071f2005-04-30 03:49:37 +0000221{
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000222 char *r_opt;
Michel Stamfdd957b2014-10-30 13:43:09 +0100223 const char *l_opt = "169.254.0.0";
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200224 int state;
225 int nsent;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000226 unsigned opts;
227
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200228 // Ugly trick, but I want these zeroed in one go
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000229 struct {
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200230 const struct ether_addr null_ethaddr;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000231 struct ifreq ifr;
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200232 uint32_t chosen_nip;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200233 int conflicts;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200234 int timeout_ms; // must be signed
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000235 int verbose;
236 } L;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200237#define null_ethaddr (L.null_ethaddr)
238#define ifr (L.ifr )
239#define chosen_nip (L.chosen_nip )
240#define conflicts (L.conflicts )
241#define timeout_ms (L.timeout_ms )
242#define verbose (L.verbose )
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000243
244 memset(&L, 0, sizeof(L));
Denys Vlasenko16714242011-09-21 01:59:15 +0200245 INIT_G();
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000246
247#define FOREGROUND (opts & 1)
248#define QUIT (opts & 2)
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200249 // Parse commandline: prog [options] ifname script
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000250 // exactly 2 args; -v accumulates and implies -f
251 opt_complementary = "=2:vv:vf";
Michel Stamfdd957b2014-10-30 13:43:09 +0100252 opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose);
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000253#if !BB_MMU
254 // on NOMMU reexec early (or else we will rerun things twice)
255 if (!FOREGROUND)
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000256 bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv);
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000257#endif
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200258 // Open an ARP socket
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000259 // (need to do it before openlog to prevent openlog from taking
260 // fd 3 (sock_fd==3))
261 xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000262 if (!FOREGROUND) {
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000263 // do it before all bb_xx_msg calls
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000264 openlog(applet_name, 0, LOG_DAEMON);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000265 logmode |= LOGMODE_SYSLOG;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000266 }
Michel Stamd3fabf82014-11-04 12:19:04 +0100267 bb_logenv_override();
268
Michel Stamfdd957b2014-10-30 13:43:09 +0100269 { // -l n.n.n.n
270 struct in_addr net;
271 if (inet_aton(l_opt, &net) == 0
272 || (net.s_addr & htonl(IN_CLASSB_NET)) != net.s_addr
273 ) {
274 bb_error_msg_and_die("invalid network address");
275 }
276 G.localnet_ip = ntohl(net.s_addr);
277 }
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000278 if (opts & 4) { // -r n.n.n.n
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200279 struct in_addr ip;
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000280 if (inet_aton(r_opt, &ip) == 0
Michel Stamfdd957b2014-10-30 13:43:09 +0100281 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != G.localnet_ip
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000282 ) {
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000283 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000284 }
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200285 chosen_nip = ip.s_addr;
Rob Landleyad8071f2005-04-30 03:49:37 +0000286 }
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000287 argv += optind - 1;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000288
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000289 /* Now: argv[0]:junk argv[1]:intf argv[2]:script argv[3]:NULL */
290 /* We need to make space for script argument: */
291 argv[0] = argv[1];
292 argv[1] = argv[2];
293 /* Now: argv[0]:intf argv[1]:script argv[2]:junk argv[3]:NULL */
294#define argv_intf (argv[0])
295
296 xsetenv("interface", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000297
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200298 // Initialize the interface (modprobe, ifup, etc)
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200299 if (run(argv, "init", 0))
Rob Landleyad8071f2005-04-30 03:49:37 +0000300 return EXIT_FAILURE;
301
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200302 // Initialize G.iface_sockaddr
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200303 // G.iface_sockaddr is: { u16 sa_family; u8 sa_data[14]; }
304 //memset(&G.iface_sockaddr, 0, sizeof(G.iface_sockaddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000305 //TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200306 safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000307
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200308 // Bind to the interface's ARP socket
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200309 xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr));
Denis Vlasenko035aae52006-09-03 12:23:56 +0000310
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200311 // Get the interface's ethernet address
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000312 //memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000313 strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000314 xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200315 memcpy(&G.our_ethaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000316
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200317 // Start with some stable ip address, either a function of
Denis Vlasenko035aae52006-09-03 12:23:56 +0000318 // the hardware address or else the last address we used.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000319 // we are taking low-order four bytes, as top-order ones
320 // aren't random enough.
Denis Vlasenko035aae52006-09-03 12:23:56 +0000321 // NOTE: the sequence of addresses we try changes only
322 // depending on when we detect conflicts.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000323 {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000324 uint32_t t;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200325 move_from_unaligned32(t, ((char *)&G.our_ethaddr + 2));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000326 srand(t);
327 }
Rob Landleye3752e52005-05-03 03:28:55 +0000328 // FIXME cases to handle:
329 // - zcip already running!
330 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000331
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200332 // Daemonize now; don't delay system startup
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000333 if (!FOREGROUND) {
Denis Vlasenko473dae02007-04-11 07:04:23 +0000334#if BB_MMU
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000335 bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000336#endif
Denys Vlasenkocde11992016-03-30 16:22:13 +0200337 bb_error_msg("start, interface %s", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000338 }
339
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200340 // Run the dynamic address negotiation protocol,
Rob Landleye3752e52005-05-03 03:28:55 +0000341 // restarting after address conflicts:
342 // - start with some address we want to try
343 // - short random delay
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000344 // - arp probes to see if another host uses it
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200345 // 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff arp who-has 169.254.194.171 tell 0.0.0.0
Rob Landleye3752e52005-05-03 03:28:55 +0000346 // - arp announcements that we're claiming it
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200347 // 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff arp who-has 169.254.194.171 (00:04:e2:64:23:c2) tell 169.254.194.171
Rob Landleye3752e52005-05-03 03:28:55 +0000348 // - use it
349 // - defend it, within limits
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000350 // exit if:
351 // - address is successfully obtained and -q was given:
352 // run "<script> config", then exit with exitcode 0
353 // - poll error (when does this happen?)
354 // - read error (when does this happen?)
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200355 // - sendto error (in send_arp_request()) (when does this happen?)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000356 // - revents & POLLERR (link down). run "<script> deconfig" first
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200357 if (chosen_nip == 0) {
358 new_nip_and_PROBE:
359 chosen_nip = pick_nip();
360 }
361 nsent = 0;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000362 state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000363 while (1) {
364 struct pollfd fds[1];
Denys Vlasenko334e12a2015-10-24 19:15:44 +0200365 unsigned deadline_us = deadline_us;
Rob Landleyad8071f2005-04-30 03:49:37 +0000366 struct arp_packet p;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200367 int ip_conflict;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200368 int n;
Rob Landleyad8071f2005-04-30 03:49:37 +0000369
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000370 fds[0].fd = sock_fd;
Rob Landleyad8071f2005-04-30 03:49:37 +0000371 fds[0].events = POLLIN;
372 fds[0].revents = 0;
373
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200374 // Poll, being ready to adjust current timeout
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000375 if (!timeout_ms) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000376 timeout_ms = random_delay_ms(PROBE_WAIT);
377 // FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to
Rob Landleye3752e52005-05-03 03:28:55 +0000378 // make the kernel filter out all packets except
379 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000380 }
Denys Vlasenko49d51592015-10-24 19:14:04 +0200381 if (timeout_ms >= 0) {
382 // Set deadline_us to the point in time when we timeout
383 deadline_us = MONOTONIC_US() + timeout_ms * 1000;
384 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000385
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200386 VDBG("...wait %d %s nsent=%u\n",
387 timeout_ms, argv_intf, nsent);
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000388
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200389 n = safe_poll(fds, 1, timeout_ms);
390 if (n < 0) {
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000391 //bb_perror_msg("poll"); - done in safe_poll
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000392 return EXIT_FAILURE;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200393 }
394 if (n == 0) { // timed out?
395 VDBG("state:%d\n", state);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000396 switch (state) {
397 case PROBE:
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200398 // No conflicting ARP packets were seen:
399 // we can progress through the states
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200400 if (nsent < PROBE_NUM) {
401 nsent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000402 VDBG("probe/%u %s@%s\n",
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200403 nsent, argv_intf, nip_to_a(chosen_nip));
Ken Sharpa4d564a2014-07-20 14:01:49 +0200404 timeout_ms = PROBE_MIN * 1000;
405 timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200406 send_arp_request(0, &null_ethaddr, chosen_nip);
407 continue;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000408 }
Denys Vlasenko6390a3a2015-10-13 01:51:37 +0200409 // Switch to announce state
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200410 nsent = 0;
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200411 state = ANNOUNCE;
412 goto send_announce;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000413 case ANNOUNCE:
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200414 // No conflicting ARP packets were seen:
415 // we can progress through the states
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200416 if (nsent < ANNOUNCE_NUM) {
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200417 send_announce:
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200418 nsent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000419 VDBG("announce/%u %s@%s\n",
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200420 nsent, argv_intf, nip_to_a(chosen_nip));
Ken Sharpa4d564a2014-07-20 14:01:49 +0200421 timeout_ms = ANNOUNCE_INTERVAL * 1000;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200422 send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
423 continue;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000424 }
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200425 // Switch to monitor state
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200426 // FIXME update filters
427 run(argv, "config", chosen_nip);
428 // NOTE: all other exit paths should deconfig...
429 if (QUIT)
430 return EXIT_SUCCESS;
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200431 // fall through: switch to MONITOR
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200432 default:
433 // case DEFEND:
434 // case MONITOR: (shouldn't happen, MONITOR timeout is infinite)
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200435 // Defend period ended with no ARP replies - we won
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200436 timeout_ms = -1; // never timeout in monitor state
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200437 state = MONITOR;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200438 continue;
439 }
440 }
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000441
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200442 // Packet arrived, or link went down.
443 // We need to adjust the timeout in case we didn't receive
444 // a conflicting packet.
445 if (timeout_ms > 0) {
446 unsigned diff = deadline_us - MONOTONIC_US();
447 if ((int)(diff) < 0) {
448 // Current time is greater than the expected timeout time.
449 diff = 0;
450 }
451 VDBG("adjusting timeout\n");
452 timeout_ms = (diff / 1000) | 1; // never 0
453 }
454
455 if ((fds[0].revents & POLLIN) == 0) {
456 if (fds[0].revents & POLLERR) {
457 // FIXME: links routinely go down;
458 // this shouldn't necessarily exit.
459 bb_error_msg("iface %s is down", argv_intf);
460 if (state >= MONITOR) {
461 // Only if we are in MONITOR or DEFEND
462 run(argv, "deconfig", chosen_nip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000463 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200464 return EXIT_FAILURE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000465 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200466 continue;
467 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000468
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200469 // Read ARP packet
470 if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
471 bb_perror_msg_and_die(bb_msg_read_error);
472 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000473
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200474 if (p.eth.ether_type != htons(ETHERTYPE_ARP))
475 continue;
476 if (p.arp.arp_op != htons(ARPOP_REQUEST)
477 && p.arp.arp_op != htons(ARPOP_REPLY)
478 ) {
479 continue;
480 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000481#ifdef DEBUG
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200482 {
483 struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
484 struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha;
485 struct in_addr *spa = (struct in_addr *) p.arp.arp_spa;
486 struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
487 VDBG("source=%s %s\n", ether_ntoa(sha), inet_ntoa(*spa));
488 VDBG("target=%s %s\n", ether_ntoa(tha), inet_ntoa(*tpa));
489 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000490#endif
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200491 ip_conflict = 0;
492 if (memcmp(&p.arp.arp_sha, &G.our_ethaddr, ETH_ALEN) != 0) {
493 if (memcmp(p.arp.arp_spa, &chosen_nip, 4) == 0) {
494 // A probe or reply with source_ip == chosen ip
495 ip_conflict = 1;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000496 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200497 if (p.arp.arp_op == htons(ARPOP_REQUEST)
498 && memcmp(p.arp.arp_spa, &const_int_0, 4) == 0
499 && memcmp(p.arp.arp_tpa, &chosen_nip, 4) == 0
500 ) {
501 // A probe with source_ip == 0.0.0.0, target_ip == chosen ip:
502 // another host trying to claim this ip!
503 ip_conflict |= 2;
504 }
505 }
506 VDBG("state:%d ip_conflict:%d\n", state, ip_conflict);
507 if (!ip_conflict)
508 continue;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200509
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200510 // Either src or target IP conflict exists
511 if (state <= ANNOUNCE) {
512 // PROBE or ANNOUNCE
513 conflicts++;
514 timeout_ms = PROBE_MIN * 1000
515 + CONFLICT_MULTIPLIER * random_delay_ms(conflicts);
516 goto new_nip_and_PROBE;
517 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000518
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200519 // MONITOR or DEFEND: only src IP conflict is a problem
520 if (ip_conflict & 1) {
521 if (state == MONITOR) {
522 // Src IP conflict, defend with a single ARP probe
523 VDBG("monitor conflict - defending\n");
524 timeout_ms = DEFEND_INTERVAL * 1000;
525 state = DEFEND;
526 send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
527 continue;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200528 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200529 // state == DEFEND
530 // Another src IP conflict, start over
531 VDBG("defend conflict - starting over\n");
532 run(argv, "deconfig", chosen_nip);
533 conflicts = 0;
534 timeout_ms = 0;
535 goto new_nip_and_PROBE;
536 }
537 // Note: if we only have a target IP conflict here (ip_conflict & 2),
538 // IOW: if we just saw this sort of ARP packet:
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200539 // aa:bb:cc:dd:ee:ff > xx:xx:xx:xx:xx:xx arp who-has <chosen_nip> tell 0.0.0.0
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200540 // we expect _kernel_ to respond to that, because <chosen_nip>
541 // is (expected to be) configured on this iface.
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000542 } // while (1)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000543#undef argv_intf
Rob Landleyad8071f2005-04-30 03:49:37 +0000544}