blob: 94e49adcb3ae1276726869f3f5a87d5d24f3285e [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 */
Rob Landleyad8071f2005-04-30 03:49:37 +000011/*
Rob Landleyad8071f2005-04-30 03:49:37 +000012 * ZCIP just manages the 169.254.*.* addresses. That network is not
13 * routed at the IP level, though various proxies or bridges can
14 * certainly be used. Its naming is built over multicast DNS.
15 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010016//config:config ZCIP
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020017//config: bool "zcip (7.8 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010018//config: default y
19//config: select PLATFORM_LINUX
20//config: select FEATURE_SYSLOG
21//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: ZCIP provides ZeroConf IPv4 address selection, according to RFC 3927.
23//config: It's a daemon that allocates and defends a dynamically assigned
24//config: address on the 169.254/16 network, requiring no system administrator.
Denys Vlasenko47367e12016-11-23 09:05:14 +010025//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020026//config: See http://www.zeroconf.org for further details, and "zcip.script"
27//config: in the busybox examples.
Denys Vlasenko47367e12016-11-23 09:05:14 +010028
29//applet:IF_ZCIP(APPLET(zcip, BB_DIR_SBIN, BB_SUID_DROP))
30
31//kbuild:lib-$(CONFIG_ZCIP) += zcip.o
Rob Landleyad8071f2005-04-30 03:49:37 +000032
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000033//#define DEBUG
Rob Landleye3752e52005-05-03 03:28:55 +000034
35// TODO:
36// - more real-world usage/testing, especially daemon mode
37// - kernel packet filters to reduce scheduling noise
38// - avoid silent script failures, especially under load...
39// - link status monitoring (restart on link-up; stop on link-down)
40
Pere Orga5bc8c002011-04-11 03:29:49 +020041//usage:#define zcip_trivial_usage
42//usage: "[OPTIONS] IFACE SCRIPT"
43//usage:#define zcip_full_usage "\n\n"
44//usage: "Manage a ZeroConf IPv4 link-local address\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020045//usage: "\n -f Run in foreground"
46//usage: "\n -q Quit after obtaining address"
47//usage: "\n -r 169.254.x.x Request this address first"
Michel Stamfdd957b2014-10-30 13:43:09 +010048//usage: "\n -l x.x.0.0 Use this range instead of 169.254"
Pere Orga5bc8c002011-04-11 03:29:49 +020049//usage: "\n -v Verbose"
50//usage: "\n"
Michel Stamd3fabf82014-11-04 12:19:04 +010051//usage: "\n$LOGGING=none Suppress logging"
52//usage: "\n$LOGGING=syslog Log to syslog"
53//usage: "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020054//usage: "\nWith no -q, runs continuously monitoring for ARP conflicts,"
55//usage: "\nexits only on I/O errors (link down etc)"
56
Dan Fandrichf533ec82011-06-10 05:17:59 +020057#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020058#include "common_bufsiz.h"
Rob Landleyad8071f2005-04-30 03:49:37 +000059#include <netinet/ether.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000060#include <net/if.h>
61#include <net/if_arp.h>
Rob Landleyad8071f2005-04-30 03:49:37 +000062#include <linux/sockios.h>
Rob Landleye3752e52005-05-03 03:28:55 +000063
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000064#include <syslog.h>
Denis Vlasenkobd7bb292007-06-17 23:40:26 +000065
66/* We don't need more than 32 bits of the counter */
67#define MONOTONIC_US() ((unsigned)monotonic_us())
Rob Landleyad8071f2005-04-30 03:49:37 +000068
Bernhard Reutner-Fischerec351c32005-12-13 10:28:25 +000069struct arp_packet {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000070 struct ether_header eth;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000071 struct ether_arp arp;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000072} PACKED;
Rob Landleyad8071f2005-04-30 03:49:37 +000073
Rob Landleybc68cd12006-03-10 19:22:06 +000074enum {
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020075 /* 0-1 seconds before sending 1st probe */
Rob Landleybc68cd12006-03-10 19:22:06 +000076 PROBE_WAIT = 1,
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020077 /* 1-2 seconds between probes */
Rob Landleybc68cd12006-03-10 19:22:06 +000078 PROBE_MIN = 1,
79 PROBE_MAX = 2,
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020080 PROBE_NUM = 3, /* total probes to send */
81 ANNOUNCE_INTERVAL = 2, /* 2 seconds between announces */
82 ANNOUNCE_NUM = 3, /* announces to send */
83 /* if probe/announce sees a conflict, multiply RANDOM(NUM_CONFLICT) by... */
84 CONFLICT_MULTIPLIER = 2,
85 /* if we monitor and see a conflict, how long is defend state? */
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +020086 DEFEND_INTERVAL = 10,
Rob Landleybc68cd12006-03-10 19:22:06 +000087};
Rob Landleyad8071f2005-04-30 03:49:37 +000088
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000089/* States during the configuration process. */
90enum {
91 PROBE = 0,
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000092 ANNOUNCE,
93 MONITOR,
94 DEFEND
95};
Rob Landleyad8071f2005-04-30 03:49:37 +000096
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000097#define VDBG(...) do { } while (0)
98
99
100enum {
101 sock_fd = 3
102};
103
104struct globals {
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200105 struct sockaddr iface_sockaddr;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200106 struct ether_addr our_ethaddr;
Michel Stamfdd957b2014-10-30 13:43:09 +0100107 uint32_t localnet_ip;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100108} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200109#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200110#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000111
Rob Landleye3752e52005-05-03 03:28:55 +0000112
113/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000114 * Pick a random link local IP address on 169.254/16, except that
115 * the first and last 256 addresses are reserved.
116 */
Michel Stamfdd957b2014-10-30 13:43:09 +0100117static uint32_t pick_nip(void)
Rob Landleyad8071f2005-04-30 03:49:37 +0000118{
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000119 unsigned tmp;
Rob Landleyad8071f2005-04-30 03:49:37 +0000120
Rob Landleyad8071f2005-04-30 03:49:37 +0000121 do {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000122 tmp = rand() & IN_CLASSB_HOST;
Rob Landleyad8071f2005-04-30 03:49:37 +0000123 } while (tmp > (IN_CLASSB_HOST - 0x0200));
Michel Stamfdd957b2014-10-30 13:43:09 +0100124 return htonl((G.localnet_ip + 0x0100) + tmp);
Rob Landleyad8071f2005-04-30 03:49:37 +0000125}
126
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200127static const char *nip_to_a(uint32_t nip)
128{
129 struct in_addr in;
130 in.s_addr = nip;
131 return inet_ntoa(in);
132}
133
Rob Landleye3752e52005-05-03 03:28:55 +0000134/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000135 * Broadcast an ARP packet.
136 */
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200137static void send_arp_request(
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000138 /* int op, - always ARPOP_REQUEST */
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200139 /* const struct ether_addr *source_eth, - always &G.our_ethaddr */
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200140 uint32_t source_nip,
141 const struct ether_addr *target_eth, uint32_t target_nip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000142{
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000143 enum { op = ARPOP_REQUEST };
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200144#define source_eth (&G.our_ethaddr)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000145
Rob Landleyad8071f2005-04-30 03:49:37 +0000146 struct arp_packet p;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000147 memset(&p, 0, sizeof(p));
Rob Landleyad8071f2005-04-30 03:49:37 +0000148
Rob Landleye3752e52005-05-03 03:28:55 +0000149 // ether header
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000150 p.eth.ether_type = htons(ETHERTYPE_ARP);
151 memcpy(p.eth.ether_shost, source_eth, ETH_ALEN);
152 memset(p.eth.ether_dhost, 0xff, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000153
Rob Landleye3752e52005-05-03 03:28:55 +0000154 // arp request
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000155 p.arp.arp_hrd = htons(ARPHRD_ETHER);
156 p.arp.arp_pro = htons(ETHERTYPE_IP);
157 p.arp.arp_hln = ETH_ALEN;
158 p.arp.arp_pln = 4;
159 p.arp.arp_op = htons(op);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000160 memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN);
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200161 memcpy(&p.arp.arp_spa, &source_nip, 4);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000162 memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200163 memcpy(&p.arp.arp_tpa, &target_nip, 4);
Rob Landleyad8071f2005-04-30 03:49:37 +0000164
Rob Landleye3752e52005-05-03 03:28:55 +0000165 // send it
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200166 // Even though sock_fd is already bound to G.iface_sockaddr, just send()
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000167 // won't work, because "socket is not connected"
168 // (and connect() won't fix that, "operation not supported").
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200169 // Thus we sendto() to G.iface_sockaddr. I wonder which sockaddr
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000170 // (from bind() or from sendto()?) kernel actually uses
171 // to determine iface to emit the packet from...
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200172 xsendto(sock_fd, &p, sizeof(p), &G.iface_sockaddr, sizeof(G.iface_sockaddr));
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000173#undef source_eth
Rob Landleyad8071f2005-04-30 03:49:37 +0000174}
175
Rob Landleye3752e52005-05-03 03:28:55 +0000176/**
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000177 * Run a script.
178 * argv[0]:intf argv[1]:script_name argv[2]:junk argv[3]:NULL
Rob Landleyad8071f2005-04-30 03:49:37 +0000179 */
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200180static int run(char *argv[3], const char *param, uint32_t nip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000181{
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000182 int status;
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200183 const char *addr = addr; /* for gcc */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000184 const char *fmt = "%s %s %s" + 3;
Denys Vlasenkoe4f6bfd2017-07-22 03:04:20 +0200185 char *env_ip = env_ip;
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);
Denys Vlasenkoe4f6bfd2017-07-22 03:04:20 +0200193 /* Must not use setenv() repeatedly, it leaks memory. Use putenv() */
194 env_ip = xasprintf("ip=%s", addr);
195 putenv(env_ip);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000196 fmt -= 3;
Rob Landleyad8071f2005-04-30 03:49:37 +0000197 }
Denys Vlasenkocde11992016-03-30 16:22:13 +0200198 bb_error_msg(fmt, argv[2], argv[0], addr);
Denys Vlasenko8531d762010-03-18 22:44:00 +0100199 status = spawn_and_wait(argv + 1);
Denys Vlasenkoe4f6bfd2017-07-22 03:04:20 +0200200 if (nip != 0)
201 bb_unsetenv_and_free(env_ip);
202
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000203 if (status < 0) {
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000204 bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000205 return -errno;
206 }
207 if (status != 0)
Denys Vlasenko8531d762010-03-18 22:44:00 +0100208 bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
Rob Landleyad8071f2005-04-30 03:49:37 +0000209 return status;
210}
211
Rob Landleye3752e52005-05-03 03:28:55 +0000212/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000213 * Return milliseconds of random delay, up to "secs" seconds.
214 */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000215static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000216{
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200217 return (unsigned)rand() % (secs * 1000);
Rob Landleyad8071f2005-04-30 03:49:37 +0000218}
219
Rob Landleye3752e52005-05-03 03:28:55 +0000220/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000221 * main program
222 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000223int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100224int zcip_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyad8071f2005-04-30 03:49:37 +0000225{
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000226 char *r_opt;
Michel Stamfdd957b2014-10-30 13:43:09 +0100227 const char *l_opt = "169.254.0.0";
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200228 int state;
229 int nsent;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000230 unsigned opts;
231
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200232 // Ugly trick, but I want these zeroed in one go
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000233 struct {
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200234 const struct ether_addr null_ethaddr;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000235 struct ifreq ifr;
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200236 uint32_t chosen_nip;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200237 int conflicts;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200238 int timeout_ms; // must be signed
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000239 int verbose;
240 } L;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200241#define null_ethaddr (L.null_ethaddr)
242#define ifr (L.ifr )
243#define chosen_nip (L.chosen_nip )
244#define conflicts (L.conflicts )
245#define timeout_ms (L.timeout_ms )
246#define verbose (L.verbose )
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000247
248 memset(&L, 0, sizeof(L));
Denys Vlasenko16714242011-09-21 01:59:15 +0200249 INIT_G();
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000250
251#define FOREGROUND (opts & 1)
252#define QUIT (opts & 2)
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200253 // Parse commandline: prog [options] ifname script
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000254 // exactly 2 args; -v accumulates and implies -f
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200255 opts = getopt32(argv, "^" "fqr:l:v" "\0" "=2:vv:vf",
256 &r_opt, &l_opt, &verbose
257 );
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000258#if !BB_MMU
259 // on NOMMU reexec early (or else we will rerun things twice)
260 if (!FOREGROUND)
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000261 bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv);
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000262#endif
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200263 // Open an ARP socket
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000264 // (need to do it before openlog to prevent openlog from taking
265 // fd 3 (sock_fd==3))
266 xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000267 if (!FOREGROUND) {
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000268 // do it before all bb_xx_msg calls
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000269 openlog(applet_name, 0, LOG_DAEMON);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000270 logmode |= LOGMODE_SYSLOG;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000271 }
Michel Stamd3fabf82014-11-04 12:19:04 +0100272 bb_logenv_override();
273
Michel Stamfdd957b2014-10-30 13:43:09 +0100274 { // -l n.n.n.n
275 struct in_addr net;
276 if (inet_aton(l_opt, &net) == 0
277 || (net.s_addr & htonl(IN_CLASSB_NET)) != net.s_addr
278 ) {
279 bb_error_msg_and_die("invalid network address");
280 }
281 G.localnet_ip = ntohl(net.s_addr);
282 }
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000283 if (opts & 4) { // -r n.n.n.n
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200284 struct in_addr ip;
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000285 if (inet_aton(r_opt, &ip) == 0
Michel Stamfdd957b2014-10-30 13:43:09 +0100286 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != G.localnet_ip
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000287 ) {
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000288 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000289 }
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200290 chosen_nip = ip.s_addr;
Rob Landleyad8071f2005-04-30 03:49:37 +0000291 }
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000292 argv += optind - 1;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000293
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000294 /* Now: argv[0]:junk argv[1]:intf argv[2]:script argv[3]:NULL */
295 /* We need to make space for script argument: */
296 argv[0] = argv[1];
297 argv[1] = argv[2];
298 /* Now: argv[0]:intf argv[1]:script argv[2]:junk argv[3]:NULL */
299#define argv_intf (argv[0])
300
301 xsetenv("interface", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000302
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200303 // Initialize the interface (modprobe, ifup, etc)
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200304 if (run(argv, "init", 0))
Rob Landleyad8071f2005-04-30 03:49:37 +0000305 return EXIT_FAILURE;
306
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200307 // Initialize G.iface_sockaddr
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200308 // G.iface_sockaddr is: { u16 sa_family; u8 sa_data[14]; }
309 //memset(&G.iface_sockaddr, 0, sizeof(G.iface_sockaddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000310 //TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200311 safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000312
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200313 // Bind to the interface's ARP socket
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200314 xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr));
Denis Vlasenko035aae52006-09-03 12:23:56 +0000315
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200316 // Get the interface's ethernet address
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000317 //memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000318 strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000319 xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200320 memcpy(&G.our_ethaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000321
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200322 // Start with some stable ip address, either a function of
Denis Vlasenko035aae52006-09-03 12:23:56 +0000323 // the hardware address or else the last address we used.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000324 // we are taking low-order four bytes, as top-order ones
325 // aren't random enough.
Denis Vlasenko035aae52006-09-03 12:23:56 +0000326 // NOTE: the sequence of addresses we try changes only
327 // depending on when we detect conflicts.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000328 {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000329 uint32_t t;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200330 move_from_unaligned32(t, ((char *)&G.our_ethaddr + 2));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000331 srand(t);
332 }
Rob Landleye3752e52005-05-03 03:28:55 +0000333 // FIXME cases to handle:
334 // - zcip already running!
335 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000336
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200337 // Daemonize now; don't delay system startup
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000338 if (!FOREGROUND) {
Denis Vlasenko473dae02007-04-11 07:04:23 +0000339#if BB_MMU
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000340 bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000341#endif
Denys Vlasenkocde11992016-03-30 16:22:13 +0200342 bb_error_msg("start, interface %s", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000343 }
344
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200345 // Run the dynamic address negotiation protocol,
Rob Landleye3752e52005-05-03 03:28:55 +0000346 // restarting after address conflicts:
347 // - start with some address we want to try
348 // - short random delay
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000349 // - arp probes to see if another host uses it
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200350 // 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 +0000351 // - arp announcements that we're claiming it
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200352 // 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 +0000353 // - use it
354 // - defend it, within limits
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000355 // exit if:
356 // - address is successfully obtained and -q was given:
357 // run "<script> config", then exit with exitcode 0
358 // - poll error (when does this happen?)
359 // - read error (when does this happen?)
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200360 // - sendto error (in send_arp_request()) (when does this happen?)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000361 // - revents & POLLERR (link down). run "<script> deconfig" first
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200362 if (chosen_nip == 0) {
363 new_nip_and_PROBE:
364 chosen_nip = pick_nip();
365 }
366 nsent = 0;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000367 state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000368 while (1) {
369 struct pollfd fds[1];
Denys Vlasenko334e12a2015-10-24 19:15:44 +0200370 unsigned deadline_us = deadline_us;
Rob Landleyad8071f2005-04-30 03:49:37 +0000371 struct arp_packet p;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200372 int ip_conflict;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200373 int n;
Rob Landleyad8071f2005-04-30 03:49:37 +0000374
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000375 fds[0].fd = sock_fd;
Rob Landleyad8071f2005-04-30 03:49:37 +0000376 fds[0].events = POLLIN;
377 fds[0].revents = 0;
378
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200379 // Poll, being ready to adjust current timeout
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000380 if (!timeout_ms) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000381 timeout_ms = random_delay_ms(PROBE_WAIT);
382 // FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to
Rob Landleye3752e52005-05-03 03:28:55 +0000383 // make the kernel filter out all packets except
384 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000385 }
Denys Vlasenko49d51592015-10-24 19:14:04 +0200386 if (timeout_ms >= 0) {
387 // Set deadline_us to the point in time when we timeout
388 deadline_us = MONOTONIC_US() + timeout_ms * 1000;
389 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000390
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200391 VDBG("...wait %d %s nsent=%u\n",
392 timeout_ms, argv_intf, nsent);
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000393
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200394 n = safe_poll(fds, 1, timeout_ms);
395 if (n < 0) {
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000396 //bb_perror_msg("poll"); - done in safe_poll
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000397 return EXIT_FAILURE;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200398 }
399 if (n == 0) { // timed out?
400 VDBG("state:%d\n", state);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000401 switch (state) {
402 case PROBE:
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200403 // No conflicting ARP packets were seen:
404 // we can progress through the states
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200405 if (nsent < PROBE_NUM) {
406 nsent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000407 VDBG("probe/%u %s@%s\n",
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200408 nsent, argv_intf, nip_to_a(chosen_nip));
Ken Sharpa4d564a2014-07-20 14:01:49 +0200409 timeout_ms = PROBE_MIN * 1000;
410 timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200411 send_arp_request(0, &null_ethaddr, chosen_nip);
412 continue;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000413 }
Denys Vlasenko6390a3a2015-10-13 01:51:37 +0200414 // Switch to announce state
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200415 nsent = 0;
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200416 state = ANNOUNCE;
417 goto send_announce;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000418 case ANNOUNCE:
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200419 // No conflicting ARP packets were seen:
420 // we can progress through the states
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200421 if (nsent < ANNOUNCE_NUM) {
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200422 send_announce:
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200423 nsent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000424 VDBG("announce/%u %s@%s\n",
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200425 nsent, argv_intf, nip_to_a(chosen_nip));
Ken Sharpa4d564a2014-07-20 14:01:49 +0200426 timeout_ms = ANNOUNCE_INTERVAL * 1000;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200427 send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
428 continue;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000429 }
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200430 // Switch to monitor state
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200431 // FIXME update filters
432 run(argv, "config", chosen_nip);
433 // NOTE: all other exit paths should deconfig...
434 if (QUIT)
435 return EXIT_SUCCESS;
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200436 // fall through: switch to MONITOR
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200437 default:
438 // case DEFEND:
439 // case MONITOR: (shouldn't happen, MONITOR timeout is infinite)
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200440 // Defend period ended with no ARP replies - we won
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200441 timeout_ms = -1; // never timeout in monitor state
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200442 state = MONITOR;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200443 continue;
444 }
445 }
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000446
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200447 // Packet arrived, or link went down.
448 // We need to adjust the timeout in case we didn't receive
449 // a conflicting packet.
450 if (timeout_ms > 0) {
451 unsigned diff = deadline_us - MONOTONIC_US();
452 if ((int)(diff) < 0) {
453 // Current time is greater than the expected timeout time.
454 diff = 0;
455 }
456 VDBG("adjusting timeout\n");
457 timeout_ms = (diff / 1000) | 1; // never 0
458 }
459
460 if ((fds[0].revents & POLLIN) == 0) {
461 if (fds[0].revents & POLLERR) {
462 // FIXME: links routinely go down;
463 // this shouldn't necessarily exit.
464 bb_error_msg("iface %s is down", argv_intf);
465 if (state >= MONITOR) {
466 // Only if we are in MONITOR or DEFEND
467 run(argv, "deconfig", chosen_nip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000468 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200469 return EXIT_FAILURE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000470 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200471 continue;
472 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000473
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200474 // Read ARP packet
475 if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
476 bb_perror_msg_and_die(bb_msg_read_error);
477 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000478
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200479 if (p.eth.ether_type != htons(ETHERTYPE_ARP))
480 continue;
481 if (p.arp.arp_op != htons(ARPOP_REQUEST)
482 && p.arp.arp_op != htons(ARPOP_REPLY)
483 ) {
484 continue;
485 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000486#ifdef DEBUG
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200487 {
488 struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
489 struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha;
490 struct in_addr *spa = (struct in_addr *) p.arp.arp_spa;
491 struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
492 VDBG("source=%s %s\n", ether_ntoa(sha), inet_ntoa(*spa));
493 VDBG("target=%s %s\n", ether_ntoa(tha), inet_ntoa(*tpa));
494 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000495#endif
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200496 ip_conflict = 0;
497 if (memcmp(&p.arp.arp_sha, &G.our_ethaddr, ETH_ALEN) != 0) {
498 if (memcmp(p.arp.arp_spa, &chosen_nip, 4) == 0) {
499 // A probe or reply with source_ip == chosen ip
500 ip_conflict = 1;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000501 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200502 if (p.arp.arp_op == htons(ARPOP_REQUEST)
503 && memcmp(p.arp.arp_spa, &const_int_0, 4) == 0
504 && memcmp(p.arp.arp_tpa, &chosen_nip, 4) == 0
505 ) {
506 // A probe with source_ip == 0.0.0.0, target_ip == chosen ip:
507 // another host trying to claim this ip!
508 ip_conflict |= 2;
509 }
510 }
511 VDBG("state:%d ip_conflict:%d\n", state, ip_conflict);
512 if (!ip_conflict)
513 continue;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200514
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200515 // Either src or target IP conflict exists
516 if (state <= ANNOUNCE) {
517 // PROBE or ANNOUNCE
518 conflicts++;
519 timeout_ms = PROBE_MIN * 1000
520 + CONFLICT_MULTIPLIER * random_delay_ms(conflicts);
521 goto new_nip_and_PROBE;
522 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000523
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200524 // MONITOR or DEFEND: only src IP conflict is a problem
525 if (ip_conflict & 1) {
526 if (state == MONITOR) {
527 // Src IP conflict, defend with a single ARP probe
528 VDBG("monitor conflict - defending\n");
529 timeout_ms = DEFEND_INTERVAL * 1000;
530 state = DEFEND;
531 send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
532 continue;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200533 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200534 // state == DEFEND
535 // Another src IP conflict, start over
536 VDBG("defend conflict - starting over\n");
537 run(argv, "deconfig", chosen_nip);
538 conflicts = 0;
539 timeout_ms = 0;
540 goto new_nip_and_PROBE;
541 }
542 // Note: if we only have a target IP conflict here (ip_conflict & 2),
543 // IOW: if we just saw this sort of ARP packet:
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200544 // 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 +0200545 // we expect _kernel_ to respond to that, because <chosen_nip>
546 // is (expected to be) configured on this iface.
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000547 } // while (1)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000548#undef argv_intf
Rob Landleyad8071f2005-04-30 03:49:37 +0000549}