blob: c930826194a3644fe0f383d994ee7c81be10f582 [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 {
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020059 /* 0-1 seconds before sending 1st probe */
Rob Landleybc68cd12006-03-10 19:22:06 +000060 PROBE_WAIT = 1,
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020061 /* 1-2 seconds between probes */
Rob Landleybc68cd12006-03-10 19:22:06 +000062 PROBE_MIN = 1,
63 PROBE_MAX = 2,
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020064 PROBE_NUM = 3, /* total probes to send */
65 ANNOUNCE_INTERVAL = 2, /* 2 seconds between announces */
66 ANNOUNCE_NUM = 3, /* announces to send */
67 /* if probe/announce sees a conflict, multiply RANDOM(NUM_CONFLICT) by... */
68 CONFLICT_MULTIPLIER = 2,
69 /* if we monitor and see a conflict, how long is defend state? */
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +020070 DEFEND_INTERVAL = 10,
Rob Landleybc68cd12006-03-10 19:22:06 +000071};
Rob Landleyad8071f2005-04-30 03:49:37 +000072
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000073/* States during the configuration process. */
74enum {
75 PROBE = 0,
Denis Vlasenko87d80dc2006-09-03 12:20:36 +000076 ANNOUNCE,
77 MONITOR,
78 DEFEND
79};
Rob Landleyad8071f2005-04-30 03:49:37 +000080
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000081#define VDBG(...) do { } while (0)
82
83
84enum {
85 sock_fd = 3
86};
87
88struct globals {
Denys Vlasenko16aa7a72015-08-04 03:27:56 +020089 struct sockaddr iface_sockaddr;
Denys Vlasenkoe3475832015-08-04 14:30:31 +020090 struct ether_addr our_ethaddr;
Michel Stamfdd957b2014-10-30 13:43:09 +010091 uint32_t localnet_ip;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010092} FIX_ALIASING;
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000093#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenko16714242011-09-21 01:59:15 +020094#define INIT_G() do { } while (0)
Denis Vlasenkoa09300a2007-11-25 12:40:56 +000095
Rob Landleye3752e52005-05-03 03:28:55 +000096
97/**
Rob Landleyad8071f2005-04-30 03:49:37 +000098 * Pick a random link local IP address on 169.254/16, except that
99 * the first and last 256 addresses are reserved.
100 */
Michel Stamfdd957b2014-10-30 13:43:09 +0100101static uint32_t pick_nip(void)
Rob Landleyad8071f2005-04-30 03:49:37 +0000102{
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000103 unsigned tmp;
Rob Landleyad8071f2005-04-30 03:49:37 +0000104
Rob Landleyad8071f2005-04-30 03:49:37 +0000105 do {
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000106 tmp = rand() & IN_CLASSB_HOST;
Rob Landleyad8071f2005-04-30 03:49:37 +0000107 } while (tmp > (IN_CLASSB_HOST - 0x0200));
Michel Stamfdd957b2014-10-30 13:43:09 +0100108 return htonl((G.localnet_ip + 0x0100) + tmp);
Rob Landleyad8071f2005-04-30 03:49:37 +0000109}
110
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200111static const char *nip_to_a(uint32_t nip)
112{
113 struct in_addr in;
114 in.s_addr = nip;
115 return inet_ntoa(in);
116}
117
Rob Landleye3752e52005-05-03 03:28:55 +0000118/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000119 * Broadcast an ARP packet.
120 */
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200121static void send_arp_request(
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000122 /* int op, - always ARPOP_REQUEST */
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200123 /* const struct ether_addr *source_eth, - always &G.our_ethaddr */
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200124 uint32_t source_nip,
125 const struct ether_addr *target_eth, uint32_t target_nip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000126{
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000127 enum { op = ARPOP_REQUEST };
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200128#define source_eth (&G.our_ethaddr)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000129
Rob Landleyad8071f2005-04-30 03:49:37 +0000130 struct arp_packet p;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000131 memset(&p, 0, sizeof(p));
Rob Landleyad8071f2005-04-30 03:49:37 +0000132
Rob Landleye3752e52005-05-03 03:28:55 +0000133 // ether header
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000134 p.eth.ether_type = htons(ETHERTYPE_ARP);
135 memcpy(p.eth.ether_shost, source_eth, ETH_ALEN);
136 memset(p.eth.ether_dhost, 0xff, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000137
Rob Landleye3752e52005-05-03 03:28:55 +0000138 // arp request
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000139 p.arp.arp_hrd = htons(ARPHRD_ETHER);
140 p.arp.arp_pro = htons(ETHERTYPE_IP);
141 p.arp.arp_hln = ETH_ALEN;
142 p.arp.arp_pln = 4;
143 p.arp.arp_op = htons(op);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000144 memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN);
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200145 memcpy(&p.arp.arp_spa, &source_nip, 4);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000146 memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200147 memcpy(&p.arp.arp_tpa, &target_nip, 4);
Rob Landleyad8071f2005-04-30 03:49:37 +0000148
Rob Landleye3752e52005-05-03 03:28:55 +0000149 // send it
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200150 // Even though sock_fd is already bound to G.iface_sockaddr, just send()
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000151 // won't work, because "socket is not connected"
152 // (and connect() won't fix that, "operation not supported").
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200153 // Thus we sendto() to G.iface_sockaddr. I wonder which sockaddr
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000154 // (from bind() or from sendto()?) kernel actually uses
155 // to determine iface to emit the packet from...
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200156 xsendto(sock_fd, &p, sizeof(p), &G.iface_sockaddr, sizeof(G.iface_sockaddr));
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000157#undef source_eth
Rob Landleyad8071f2005-04-30 03:49:37 +0000158}
159
Rob Landleye3752e52005-05-03 03:28:55 +0000160/**
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000161 * Run a script.
162 * argv[0]:intf argv[1]:script_name argv[2]:junk argv[3]:NULL
Rob Landleyad8071f2005-04-30 03:49:37 +0000163 */
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200164static int run(char *argv[3], const char *param, uint32_t nip)
Rob Landleyad8071f2005-04-30 03:49:37 +0000165{
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000166 int status;
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200167 const char *addr = addr; /* for gcc */
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000168 const char *fmt = "%s %s %s" + 3;
Rob Landleyad8071f2005-04-30 03:49:37 +0000169
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000170 argv[2] = (char*)param;
171
172 VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]);
Rob Landleyad8071f2005-04-30 03:49:37 +0000173
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200174 if (nip != 0) {
175 addr = nip_to_a(nip);
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000176 xsetenv("ip", addr);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000177 fmt -= 3;
Rob Landleyad8071f2005-04-30 03:49:37 +0000178 }
Denys Vlasenkocde11992016-03-30 16:22:13 +0200179 bb_error_msg(fmt, argv[2], argv[0], addr);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000180
Denys Vlasenko8531d762010-03-18 22:44:00 +0100181 status = spawn_and_wait(argv + 1);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000182 if (status < 0) {
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000183 bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000184 return -errno;
185 }
186 if (status != 0)
Denys Vlasenko8531d762010-03-18 22:44:00 +0100187 bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
Rob Landleyad8071f2005-04-30 03:49:37 +0000188 return status;
189}
190
Rob Landleye3752e52005-05-03 03:28:55 +0000191/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000192 * Return milliseconds of random delay, up to "secs" seconds.
193 */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000194static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
Rob Landleyad8071f2005-04-30 03:49:37 +0000195{
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200196 return (unsigned)rand() % (secs * 1000);
Rob Landleyad8071f2005-04-30 03:49:37 +0000197}
198
Rob Landleye3752e52005-05-03 03:28:55 +0000199/**
Rob Landleyad8071f2005-04-30 03:49:37 +0000200 * main program
201 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000202int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100203int zcip_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyad8071f2005-04-30 03:49:37 +0000204{
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000205 char *r_opt;
Michel Stamfdd957b2014-10-30 13:43:09 +0100206 const char *l_opt = "169.254.0.0";
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200207 int state;
208 int nsent;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000209 unsigned opts;
210
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200211 // Ugly trick, but I want these zeroed in one go
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000212 struct {
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200213 const struct ether_addr null_ethaddr;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000214 struct ifreq ifr;
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200215 uint32_t chosen_nip;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200216 int conflicts;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200217 int timeout_ms; // must be signed
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000218 int verbose;
219 } L;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200220#define null_ethaddr (L.null_ethaddr)
221#define ifr (L.ifr )
222#define chosen_nip (L.chosen_nip )
223#define conflicts (L.conflicts )
224#define timeout_ms (L.timeout_ms )
225#define verbose (L.verbose )
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000226
227 memset(&L, 0, sizeof(L));
Denys Vlasenko16714242011-09-21 01:59:15 +0200228 INIT_G();
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000229
230#define FOREGROUND (opts & 1)
231#define QUIT (opts & 2)
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200232 // Parse commandline: prog [options] ifname script
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000233 // exactly 2 args; -v accumulates and implies -f
234 opt_complementary = "=2:vv:vf";
Michel Stamfdd957b2014-10-30 13:43:09 +0100235 opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose);
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000236#if !BB_MMU
237 // on NOMMU reexec early (or else we will rerun things twice)
238 if (!FOREGROUND)
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000239 bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv);
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000240#endif
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200241 // Open an ARP socket
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000242 // (need to do it before openlog to prevent openlog from taking
243 // fd 3 (sock_fd==3))
244 xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000245 if (!FOREGROUND) {
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000246 // do it before all bb_xx_msg calls
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000247 openlog(applet_name, 0, LOG_DAEMON);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000248 logmode |= LOGMODE_SYSLOG;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000249 }
Michel Stamd3fabf82014-11-04 12:19:04 +0100250 bb_logenv_override();
251
Michel Stamfdd957b2014-10-30 13:43:09 +0100252 { // -l n.n.n.n
253 struct in_addr net;
254 if (inet_aton(l_opt, &net) == 0
255 || (net.s_addr & htonl(IN_CLASSB_NET)) != net.s_addr
256 ) {
257 bb_error_msg_and_die("invalid network address");
258 }
259 G.localnet_ip = ntohl(net.s_addr);
260 }
Denis Vlasenkoaf906a32006-09-03 12:29:53 +0000261 if (opts & 4) { // -r n.n.n.n
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200262 struct in_addr ip;
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000263 if (inet_aton(r_opt, &ip) == 0
Michel Stamfdd957b2014-10-30 13:43:09 +0100264 || (ntohl(ip.s_addr) & IN_CLASSB_NET) != G.localnet_ip
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000265 ) {
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000266 bb_error_msg_and_die("invalid link address");
Rob Landleye3752e52005-05-03 03:28:55 +0000267 }
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200268 chosen_nip = ip.s_addr;
Rob Landleyad8071f2005-04-30 03:49:37 +0000269 }
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000270 argv += optind - 1;
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000271
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000272 /* Now: argv[0]:junk argv[1]:intf argv[2]:script argv[3]:NULL */
273 /* We need to make space for script argument: */
274 argv[0] = argv[1];
275 argv[1] = argv[2];
276 /* Now: argv[0]:intf argv[1]:script argv[2]:junk argv[3]:NULL */
277#define argv_intf (argv[0])
278
279 xsetenv("interface", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000280
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200281 // Initialize the interface (modprobe, ifup, etc)
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200282 if (run(argv, "init", 0))
Rob Landleyad8071f2005-04-30 03:49:37 +0000283 return EXIT_FAILURE;
284
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200285 // Initialize G.iface_sockaddr
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200286 // G.iface_sockaddr is: { u16 sa_family; u8 sa_data[14]; }
287 //memset(&G.iface_sockaddr, 0, sizeof(G.iface_sockaddr));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000288 //TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200289 safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data));
Rob Landleyad8071f2005-04-30 03:49:37 +0000290
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200291 // Bind to the interface's ARP socket
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200292 xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr));
Denis Vlasenko035aae52006-09-03 12:23:56 +0000293
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200294 // Get the interface's ethernet address
Denis Vlasenko9ab26582007-02-14 20:50:22 +0000295 //memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000296 strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000297 xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200298 memcpy(&G.our_ethaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Rob Landleyad8071f2005-04-30 03:49:37 +0000299
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200300 // Start with some stable ip address, either a function of
Denis Vlasenko035aae52006-09-03 12:23:56 +0000301 // the hardware address or else the last address we used.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000302 // we are taking low-order four bytes, as top-order ones
303 // aren't random enough.
Denis Vlasenko035aae52006-09-03 12:23:56 +0000304 // NOTE: the sequence of addresses we try changes only
305 // depending on when we detect conflicts.
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000306 {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000307 uint32_t t;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200308 move_from_unaligned32(t, ((char *)&G.our_ethaddr + 2));
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000309 srand(t);
310 }
Rob Landleye3752e52005-05-03 03:28:55 +0000311 // FIXME cases to handle:
312 // - zcip already running!
313 // - link already has local address... just defend/update
Rob Landleyad8071f2005-04-30 03:49:37 +0000314
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200315 // Daemonize now; don't delay system startup
Denis Vlasenko65dbd872006-09-03 12:27:25 +0000316 if (!FOREGROUND) {
Denis Vlasenko473dae02007-04-11 07:04:23 +0000317#if BB_MMU
Denis Vlasenkob29028e2008-02-28 18:02:23 +0000318 bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/);
Denis Vlasenkoafa70232007-03-26 17:25:33 +0000319#endif
Denys Vlasenkocde11992016-03-30 16:22:13 +0200320 bb_error_msg("start, interface %s", argv_intf);
Rob Landleyad8071f2005-04-30 03:49:37 +0000321 }
322
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200323 // Run the dynamic address negotiation protocol,
Rob Landleye3752e52005-05-03 03:28:55 +0000324 // restarting after address conflicts:
325 // - start with some address we want to try
326 // - short random delay
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000327 // - arp probes to see if another host uses it
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200328 // 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 +0000329 // - arp announcements that we're claiming it
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200330 // 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 +0000331 // - use it
332 // - defend it, within limits
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000333 // exit if:
334 // - address is successfully obtained and -q was given:
335 // run "<script> config", then exit with exitcode 0
336 // - poll error (when does this happen?)
337 // - read error (when does this happen?)
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200338 // - sendto error (in send_arp_request()) (when does this happen?)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000339 // - revents & POLLERR (link down). run "<script> deconfig" first
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200340 if (chosen_nip == 0) {
341 new_nip_and_PROBE:
342 chosen_nip = pick_nip();
343 }
344 nsent = 0;
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000345 state = PROBE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000346 while (1) {
347 struct pollfd fds[1];
Denys Vlasenko334e12a2015-10-24 19:15:44 +0200348 unsigned deadline_us = deadline_us;
Rob Landleyad8071f2005-04-30 03:49:37 +0000349 struct arp_packet p;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200350 int ip_conflict;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200351 int n;
Rob Landleyad8071f2005-04-30 03:49:37 +0000352
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000353 fds[0].fd = sock_fd;
Rob Landleyad8071f2005-04-30 03:49:37 +0000354 fds[0].events = POLLIN;
355 fds[0].revents = 0;
356
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200357 // Poll, being ready to adjust current timeout
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000358 if (!timeout_ms) {
Denis Vlasenkoa09300a2007-11-25 12:40:56 +0000359 timeout_ms = random_delay_ms(PROBE_WAIT);
360 // FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to
Rob Landleye3752e52005-05-03 03:28:55 +0000361 // make the kernel filter out all packets except
362 // ones we'd care about.
Rob Landleyad8071f2005-04-30 03:49:37 +0000363 }
Denys Vlasenko49d51592015-10-24 19:14:04 +0200364 if (timeout_ms >= 0) {
365 // Set deadline_us to the point in time when we timeout
366 deadline_us = MONOTONIC_US() + timeout_ms * 1000;
367 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000368
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200369 VDBG("...wait %d %s nsent=%u\n",
370 timeout_ms, argv_intf, nsent);
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000371
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200372 n = safe_poll(fds, 1, timeout_ms);
373 if (n < 0) {
Denis Vlasenkof3f33eb2008-02-04 09:42:05 +0000374 //bb_perror_msg("poll"); - done in safe_poll
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000375 return EXIT_FAILURE;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200376 }
377 if (n == 0) { // timed out?
378 VDBG("state:%d\n", state);
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000379 switch (state) {
380 case PROBE:
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200381 // No conflicting ARP packets were seen:
382 // we can progress through the states
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200383 if (nsent < PROBE_NUM) {
384 nsent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000385 VDBG("probe/%u %s@%s\n",
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200386 nsent, argv_intf, nip_to_a(chosen_nip));
Ken Sharpa4d564a2014-07-20 14:01:49 +0200387 timeout_ms = PROBE_MIN * 1000;
388 timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200389 send_arp_request(0, &null_ethaddr, chosen_nip);
390 continue;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000391 }
Denys Vlasenko6390a3a2015-10-13 01:51:37 +0200392 // Switch to announce state
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200393 nsent = 0;
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200394 state = ANNOUNCE;
395 goto send_announce;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000396 case ANNOUNCE:
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200397 // No conflicting ARP packets were seen:
398 // we can progress through the states
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200399 if (nsent < ANNOUNCE_NUM) {
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200400 send_announce:
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200401 nsent++;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000402 VDBG("announce/%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 = ANNOUNCE_INTERVAL * 1000;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200405 send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
406 continue;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000407 }
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200408 // Switch to monitor state
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200409 // FIXME update filters
410 run(argv, "config", chosen_nip);
411 // NOTE: all other exit paths should deconfig...
412 if (QUIT)
413 return EXIT_SUCCESS;
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200414 // fall through: switch to MONITOR
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200415 default:
416 // case DEFEND:
417 // case MONITOR: (shouldn't happen, MONITOR timeout is infinite)
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200418 // Defend period ended with no ARP replies - we won
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200419 timeout_ms = -1; // never timeout in monitor state
Denys Vlasenko16aa7a72015-08-04 03:27:56 +0200420 state = MONITOR;
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200421 continue;
422 }
423 }
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000424
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200425 // Packet arrived, or link went down.
426 // We need to adjust the timeout in case we didn't receive
427 // a conflicting packet.
428 if (timeout_ms > 0) {
429 unsigned diff = deadline_us - MONOTONIC_US();
430 if ((int)(diff) < 0) {
431 // Current time is greater than the expected timeout time.
432 diff = 0;
433 }
434 VDBG("adjusting timeout\n");
435 timeout_ms = (diff / 1000) | 1; // never 0
436 }
437
438 if ((fds[0].revents & POLLIN) == 0) {
439 if (fds[0].revents & POLLERR) {
440 // FIXME: links routinely go down;
441 // this shouldn't necessarily exit.
442 bb_error_msg("iface %s is down", argv_intf);
443 if (state >= MONITOR) {
444 // Only if we are in MONITOR or DEFEND
445 run(argv, "deconfig", chosen_nip);
Rob Landleyad8071f2005-04-30 03:49:37 +0000446 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200447 return EXIT_FAILURE;
Rob Landleyad8071f2005-04-30 03:49:37 +0000448 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200449 continue;
450 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000451
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200452 // Read ARP packet
453 if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
454 bb_perror_msg_and_die(bb_msg_read_error);
455 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000456
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200457 if (p.eth.ether_type != htons(ETHERTYPE_ARP))
458 continue;
459 if (p.arp.arp_op != htons(ARPOP_REQUEST)
460 && p.arp.arp_op != htons(ARPOP_REPLY)
461 ) {
462 continue;
463 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000464#ifdef DEBUG
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200465 {
466 struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
467 struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha;
468 struct in_addr *spa = (struct in_addr *) p.arp.arp_spa;
469 struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
470 VDBG("source=%s %s\n", ether_ntoa(sha), inet_ntoa(*spa));
471 VDBG("target=%s %s\n", ether_ntoa(tha), inet_ntoa(*tpa));
472 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000473#endif
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200474 ip_conflict = 0;
475 if (memcmp(&p.arp.arp_sha, &G.our_ethaddr, ETH_ALEN) != 0) {
476 if (memcmp(p.arp.arp_spa, &chosen_nip, 4) == 0) {
477 // A probe or reply with source_ip == chosen ip
478 ip_conflict = 1;
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000479 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200480 if (p.arp.arp_op == htons(ARPOP_REQUEST)
481 && memcmp(p.arp.arp_spa, &const_int_0, 4) == 0
482 && memcmp(p.arp.arp_tpa, &chosen_nip, 4) == 0
483 ) {
484 // A probe with source_ip == 0.0.0.0, target_ip == chosen ip:
485 // another host trying to claim this ip!
486 ip_conflict |= 2;
487 }
488 }
489 VDBG("state:%d ip_conflict:%d\n", state, ip_conflict);
490 if (!ip_conflict)
491 continue;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200492
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200493 // Either src or target IP conflict exists
494 if (state <= ANNOUNCE) {
495 // PROBE or ANNOUNCE
496 conflicts++;
497 timeout_ms = PROBE_MIN * 1000
498 + CONFLICT_MULTIPLIER * random_delay_ms(conflicts);
499 goto new_nip_and_PROBE;
500 }
Denis Vlasenko87d80dc2006-09-03 12:20:36 +0000501
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200502 // MONITOR or DEFEND: only src IP conflict is a problem
503 if (ip_conflict & 1) {
504 if (state == MONITOR) {
505 // Src IP conflict, defend with a single ARP probe
506 VDBG("monitor conflict - defending\n");
507 timeout_ms = DEFEND_INTERVAL * 1000;
508 state = DEFEND;
509 send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
510 continue;
Denys Vlasenko99e30be2015-08-04 12:28:21 +0200511 }
Denys Vlasenkoe3475832015-08-04 14:30:31 +0200512 // state == DEFEND
513 // Another src IP conflict, start over
514 VDBG("defend conflict - starting over\n");
515 run(argv, "deconfig", chosen_nip);
516 conflicts = 0;
517 timeout_ms = 0;
518 goto new_nip_and_PROBE;
519 }
520 // Note: if we only have a target IP conflict here (ip_conflict & 2),
521 // IOW: if we just saw this sort of ARP packet:
Denys Vlasenkoa83e3ae2015-08-04 15:06:38 +0200522 // 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 +0200523 // we expect _kernel_ to respond to that, because <chosen_nip>
524 // is (expected to be) configured on this iface.
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000525 } // while (1)
Denis Vlasenkocdd1f732008-10-10 11:28:39 +0000526#undef argv_intf
Rob Landleyad8071f2005-04-30 03:49:37 +0000527}