2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2005-04-30 03:49:37 +00:00
|
|
|
/*
|
|
|
|
* RFC3927 ZeroConf IPv4 Link-Local addressing
|
|
|
|
* (see <http://www.zeroconf.org/>)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com)
|
|
|
|
* Copyright (C) 2004 by David Brownell
|
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2005-04-30 03:49:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ZCIP just manages the 169.254.*.* addresses. That network is not
|
|
|
|
* routed at the IP level, though various proxies or bridges can
|
|
|
|
* certainly be used. Its naming is built over multicast DNS.
|
|
|
|
*/
|
2016-11-23 08:05:14 +00:00
|
|
|
//config:config ZCIP
|
|
|
|
//config: bool "zcip"
|
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: select FEATURE_SYSLOG
|
|
|
|
//config: help
|
|
|
|
//config: ZCIP provides ZeroConf IPv4 address selection, according to RFC 3927.
|
|
|
|
//config: It's a daemon that allocates and defends a dynamically assigned
|
|
|
|
//config: address on the 169.254/16 network, requiring no system administrator.
|
|
|
|
//config:
|
|
|
|
//config: See http://www.zeroconf.org for further details, and "zcip.script"
|
|
|
|
//config: in the busybox examples.
|
|
|
|
|
|
|
|
//applet:IF_ZCIP(APPLET(zcip, BB_DIR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_ZCIP) += zcip.o
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2006-09-03 12:20:36 +00:00
|
|
|
//#define DEBUG
|
2005-05-03 03:28:55 +00:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// - more real-world usage/testing, especially daemon mode
|
|
|
|
// - kernel packet filters to reduce scheduling noise
|
|
|
|
// - avoid silent script failures, especially under load...
|
|
|
|
// - link status monitoring (restart on link-up; stop on link-down)
|
|
|
|
|
2011-04-11 01:29:49 +00:00
|
|
|
//usage:#define zcip_trivial_usage
|
|
|
|
//usage: "[OPTIONS] IFACE SCRIPT"
|
|
|
|
//usage:#define zcip_full_usage "\n\n"
|
|
|
|
//usage: "Manage a ZeroConf IPv4 link-local address\n"
|
|
|
|
//usage: "\n -f Run in foreground"
|
|
|
|
//usage: "\n -q Quit after obtaining address"
|
|
|
|
//usage: "\n -r 169.254.x.x Request this address first"
|
2014-10-30 12:43:09 +00:00
|
|
|
//usage: "\n -l x.x.0.0 Use this range instead of 169.254"
|
2011-04-11 01:29:49 +00:00
|
|
|
//usage: "\n -v Verbose"
|
|
|
|
//usage: "\n"
|
2014-11-04 11:19:04 +00:00
|
|
|
//usage: "\n$LOGGING=none Suppress logging"
|
|
|
|
//usage: "\n$LOGGING=syslog Log to syslog"
|
|
|
|
//usage: "\n"
|
2011-04-11 01:29:49 +00:00
|
|
|
//usage: "\nWith no -q, runs continuously monitoring for ARP conflicts,"
|
|
|
|
//usage: "\nexits only on I/O errors (link down etc)"
|
|
|
|
|
2011-06-10 03:17:59 +00:00
|
|
|
#include "libbb.h"
|
2016-04-21 14:26:30 +00:00
|
|
|
#include "common_bufsiz.h"
|
2005-04-30 03:49:37 +00:00
|
|
|
#include <netinet/ether.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
#include <linux/sockios.h>
|
2005-05-03 03:28:55 +00:00
|
|
|
|
2008-01-27 12:50:12 +00:00
|
|
|
#include <syslog.h>
|
2007-06-17 23:40:26 +00:00
|
|
|
|
|
|
|
/* We don't need more than 32 bits of the counter */
|
|
|
|
#define MONOTONIC_US() ((unsigned)monotonic_us())
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2005-12-13 10:28:25 +00:00
|
|
|
struct arp_packet {
|
2007-11-25 12:40:56 +00:00
|
|
|
struct ether_header eth;
|
2006-09-03 12:20:36 +00:00
|
|
|
struct ether_arp arp;
|
2008-07-05 09:18:54 +00:00
|
|
|
} PACKED;
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2006-03-10 19:22:06 +00:00
|
|
|
enum {
|
2015-08-04 01:27:56 +00:00
|
|
|
/* 0-1 seconds before sending 1st probe */
|
2006-03-10 19:22:06 +00:00
|
|
|
PROBE_WAIT = 1,
|
2015-08-04 01:27:56 +00:00
|
|
|
/* 1-2 seconds between probes */
|
2006-03-10 19:22:06 +00:00
|
|
|
PROBE_MIN = 1,
|
|
|
|
PROBE_MAX = 2,
|
2015-08-04 01:27:56 +00:00
|
|
|
PROBE_NUM = 3, /* total probes to send */
|
|
|
|
ANNOUNCE_INTERVAL = 2, /* 2 seconds between announces */
|
|
|
|
ANNOUNCE_NUM = 3, /* announces to send */
|
|
|
|
/* if probe/announce sees a conflict, multiply RANDOM(NUM_CONFLICT) by... */
|
|
|
|
CONFLICT_MULTIPLIER = 2,
|
|
|
|
/* if we monitor and see a conflict, how long is defend state? */
|
2015-08-04 13:06:38 +00:00
|
|
|
DEFEND_INTERVAL = 10,
|
2006-03-10 19:22:06 +00:00
|
|
|
};
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2006-09-03 12:20:36 +00:00
|
|
|
/* States during the configuration process. */
|
|
|
|
enum {
|
|
|
|
PROBE = 0,
|
|
|
|
ANNOUNCE,
|
|
|
|
MONITOR,
|
|
|
|
DEFEND
|
|
|
|
};
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2007-11-25 12:40:56 +00:00
|
|
|
#define VDBG(...) do { } while (0)
|
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
sock_fd = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
struct globals {
|
2015-08-04 01:27:56 +00:00
|
|
|
struct sockaddr iface_sockaddr;
|
2015-08-04 12:30:31 +00:00
|
|
|
struct ether_addr our_ethaddr;
|
2014-10-30 12:43:09 +00:00
|
|
|
uint32_t localnet_ip;
|
2010-02-04 14:00:15 +00:00
|
|
|
} FIX_ALIASING;
|
2016-04-21 14:26:30 +00:00
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2016-04-21 16:18:48 +00:00
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
2007-11-25 12:40:56 +00:00
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
|
|
|
|
/**
|
2005-04-30 03:49:37 +00:00
|
|
|
* Pick a random link local IP address on 169.254/16, except that
|
|
|
|
* the first and last 256 addresses are reserved.
|
|
|
|
*/
|
2014-10-30 12:43:09 +00:00
|
|
|
static uint32_t pick_nip(void)
|
2005-04-30 03:49:37 +00:00
|
|
|
{
|
2006-09-03 12:29:53 +00:00
|
|
|
unsigned tmp;
|
2005-04-30 03:49:37 +00:00
|
|
|
|
|
|
|
do {
|
2007-06-17 23:40:26 +00:00
|
|
|
tmp = rand() & IN_CLASSB_HOST;
|
2005-04-30 03:49:37 +00:00
|
|
|
} while (tmp > (IN_CLASSB_HOST - 0x0200));
|
2014-10-30 12:43:09 +00:00
|
|
|
return htonl((G.localnet_ip + 0x0100) + tmp);
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 01:27:56 +00:00
|
|
|
static const char *nip_to_a(uint32_t nip)
|
|
|
|
{
|
|
|
|
struct in_addr in;
|
|
|
|
in.s_addr = nip;
|
|
|
|
return inet_ntoa(in);
|
|
|
|
}
|
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
/**
|
2005-04-30 03:49:37 +00:00
|
|
|
* Broadcast an ARP packet.
|
|
|
|
*/
|
2015-08-04 12:30:31 +00:00
|
|
|
static void send_arp_request(
|
2008-10-10 11:28:39 +00:00
|
|
|
/* int op, - always ARPOP_REQUEST */
|
2015-08-04 12:30:31 +00:00
|
|
|
/* const struct ether_addr *source_eth, - always &G.our_ethaddr */
|
2015-08-04 01:27:56 +00:00
|
|
|
uint32_t source_nip,
|
|
|
|
const struct ether_addr *target_eth, uint32_t target_nip)
|
2005-04-30 03:49:37 +00:00
|
|
|
{
|
2008-10-10 11:28:39 +00:00
|
|
|
enum { op = ARPOP_REQUEST };
|
2015-08-04 12:30:31 +00:00
|
|
|
#define source_eth (&G.our_ethaddr)
|
2008-10-10 11:28:39 +00:00
|
|
|
|
2005-04-30 03:49:37 +00:00
|
|
|
struct arp_packet p;
|
2006-09-03 12:20:36 +00:00
|
|
|
memset(&p, 0, sizeof(p));
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
// ether header
|
2007-11-25 12:40:56 +00:00
|
|
|
p.eth.ether_type = htons(ETHERTYPE_ARP);
|
|
|
|
memcpy(p.eth.ether_shost, source_eth, ETH_ALEN);
|
|
|
|
memset(p.eth.ether_dhost, 0xff, ETH_ALEN);
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
// arp request
|
2006-09-03 12:20:36 +00:00
|
|
|
p.arp.arp_hrd = htons(ARPHRD_ETHER);
|
|
|
|
p.arp.arp_pro = htons(ETHERTYPE_IP);
|
|
|
|
p.arp.arp_hln = ETH_ALEN;
|
|
|
|
p.arp.arp_pln = 4;
|
|
|
|
p.arp.arp_op = htons(op);
|
2007-11-25 12:40:56 +00:00
|
|
|
memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN);
|
2015-08-04 01:27:56 +00:00
|
|
|
memcpy(&p.arp.arp_spa, &source_nip, 4);
|
2007-11-25 12:40:56 +00:00
|
|
|
memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
|
2015-08-04 01:27:56 +00:00
|
|
|
memcpy(&p.arp.arp_tpa, &target_nip, 4);
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
// send it
|
2015-08-04 01:27:56 +00:00
|
|
|
// Even though sock_fd is already bound to G.iface_sockaddr, just send()
|
2008-10-10 11:28:39 +00:00
|
|
|
// won't work, because "socket is not connected"
|
|
|
|
// (and connect() won't fix that, "operation not supported").
|
2015-08-04 01:27:56 +00:00
|
|
|
// Thus we sendto() to G.iface_sockaddr. I wonder which sockaddr
|
2008-10-10 11:28:39 +00:00
|
|
|
// (from bind() or from sendto()?) kernel actually uses
|
|
|
|
// to determine iface to emit the packet from...
|
2015-08-04 01:27:56 +00:00
|
|
|
xsendto(sock_fd, &p, sizeof(p), &G.iface_sockaddr, sizeof(G.iface_sockaddr));
|
2008-10-10 11:28:39 +00:00
|
|
|
#undef source_eth
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
/**
|
2008-10-10 11:28:39 +00:00
|
|
|
* Run a script.
|
|
|
|
* argv[0]:intf argv[1]:script_name argv[2]:junk argv[3]:NULL
|
2005-04-30 03:49:37 +00:00
|
|
|
*/
|
2015-08-04 01:27:56 +00:00
|
|
|
static int run(char *argv[3], const char *param, uint32_t nip)
|
2005-04-30 03:49:37 +00:00
|
|
|
{
|
2007-03-26 17:25:33 +00:00
|
|
|
int status;
|
2015-08-04 01:27:56 +00:00
|
|
|
const char *addr = addr; /* for gcc */
|
2007-11-25 12:40:56 +00:00
|
|
|
const char *fmt = "%s %s %s" + 3;
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2008-10-10 11:28:39 +00:00
|
|
|
argv[2] = (char*)param;
|
|
|
|
|
|
|
|
VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]);
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2015-08-04 01:27:56 +00:00
|
|
|
if (nip != 0) {
|
|
|
|
addr = nip_to_a(nip);
|
2008-09-20 16:28:59 +00:00
|
|
|
xsetenv("ip", addr);
|
2007-11-25 12:40:56 +00:00
|
|
|
fmt -= 3;
|
2007-03-26 17:25:33 +00:00
|
|
|
}
|
2016-03-30 14:22:13 +00:00
|
|
|
bb_error_msg(fmt, argv[2], argv[0], addr);
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2010-03-18 21:44:00 +00:00
|
|
|
status = spawn_and_wait(argv + 1);
|
2007-03-26 17:25:33 +00:00
|
|
|
if (status < 0) {
|
2008-10-10 11:28:39 +00:00
|
|
|
bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
|
2007-03-26 17:25:33 +00:00
|
|
|
return -errno;
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
2007-03-26 17:25:33 +00:00
|
|
|
if (status != 0)
|
2010-03-18 21:44:00 +00:00
|
|
|
bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
|
2005-04-30 03:49:37 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
/**
|
2005-04-30 03:49:37 +00:00
|
|
|
* Return milliseconds of random delay, up to "secs" seconds.
|
|
|
|
*/
|
2008-05-15 21:30:45 +00:00
|
|
|
static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
|
2005-04-30 03:49:37 +00:00
|
|
|
{
|
2015-08-04 13:06:38 +00:00
|
|
|
return (unsigned)rand() % (secs * 1000);
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
/**
|
2005-04-30 03:49:37 +00:00
|
|
|
* main program
|
|
|
|
*/
|
2007-10-11 10:05:36 +00:00
|
|
|
int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2010-01-04 13:15:38 +00:00
|
|
|
int zcip_main(int argc UNUSED_PARAM, char **argv)
|
2005-04-30 03:49:37 +00:00
|
|
|
{
|
2007-03-26 17:25:33 +00:00
|
|
|
char *r_opt;
|
2014-10-30 12:43:09 +00:00
|
|
|
const char *l_opt = "169.254.0.0";
|
2015-08-04 12:30:31 +00:00
|
|
|
int state;
|
|
|
|
int nsent;
|
2007-03-26 17:25:33 +00:00
|
|
|
unsigned opts;
|
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Ugly trick, but I want these zeroed in one go
|
2007-03-26 17:25:33 +00:00
|
|
|
struct {
|
2015-08-04 12:30:31 +00:00
|
|
|
const struct ether_addr null_ethaddr;
|
2007-03-26 17:25:33 +00:00
|
|
|
struct ifreq ifr;
|
2015-08-04 01:27:56 +00:00
|
|
|
uint32_t chosen_nip;
|
2015-08-04 12:30:31 +00:00
|
|
|
int conflicts;
|
2015-08-04 10:28:21 +00:00
|
|
|
int timeout_ms; // must be signed
|
2007-03-26 17:25:33 +00:00
|
|
|
int verbose;
|
|
|
|
} L;
|
2015-08-04 12:30:31 +00:00
|
|
|
#define null_ethaddr (L.null_ethaddr)
|
|
|
|
#define ifr (L.ifr )
|
|
|
|
#define chosen_nip (L.chosen_nip )
|
|
|
|
#define conflicts (L.conflicts )
|
|
|
|
#define timeout_ms (L.timeout_ms )
|
|
|
|
#define verbose (L.verbose )
|
2007-03-26 17:25:33 +00:00
|
|
|
|
|
|
|
memset(&L, 0, sizeof(L));
|
2011-09-20 23:59:15 +00:00
|
|
|
INIT_G();
|
2006-09-03 12:23:56 +00:00
|
|
|
|
2007-03-26 17:25:33 +00:00
|
|
|
#define FOREGROUND (opts & 1)
|
|
|
|
#define QUIT (opts & 2)
|
2015-08-04 12:30:31 +00:00
|
|
|
// Parse commandline: prog [options] ifname script
|
2007-03-26 17:25:33 +00:00
|
|
|
// exactly 2 args; -v accumulates and implies -f
|
|
|
|
opt_complementary = "=2:vv:vf";
|
2014-10-30 12:43:09 +00:00
|
|
|
opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose);
|
2008-02-04 09:42:05 +00:00
|
|
|
#if !BB_MMU
|
|
|
|
// on NOMMU reexec early (or else we will rerun things twice)
|
|
|
|
if (!FOREGROUND)
|
2008-02-28 18:02:23 +00:00
|
|
|
bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv);
|
2008-02-04 09:42:05 +00:00
|
|
|
#endif
|
2015-08-04 12:30:31 +00:00
|
|
|
// Open an ARP socket
|
2008-02-04 09:42:05 +00:00
|
|
|
// (need to do it before openlog to prevent openlog from taking
|
|
|
|
// fd 3 (sock_fd==3))
|
|
|
|
xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
|
2006-09-06 18:36:50 +00:00
|
|
|
if (!FOREGROUND) {
|
2008-02-04 09:42:05 +00:00
|
|
|
// do it before all bb_xx_msg calls
|
2006-10-03 21:00:43 +00:00
|
|
|
openlog(applet_name, 0, LOG_DAEMON);
|
2007-03-26 17:25:33 +00:00
|
|
|
logmode |= LOGMODE_SYSLOG;
|
2006-09-06 18:36:50 +00:00
|
|
|
}
|
2014-11-04 11:19:04 +00:00
|
|
|
bb_logenv_override();
|
|
|
|
|
2014-10-30 12:43:09 +00:00
|
|
|
{ // -l n.n.n.n
|
|
|
|
struct in_addr net;
|
|
|
|
if (inet_aton(l_opt, &net) == 0
|
|
|
|
|| (net.s_addr & htonl(IN_CLASSB_NET)) != net.s_addr
|
|
|
|
) {
|
|
|
|
bb_error_msg_and_die("invalid network address");
|
|
|
|
}
|
|
|
|
G.localnet_ip = ntohl(net.s_addr);
|
|
|
|
}
|
2006-09-03 12:29:53 +00:00
|
|
|
if (opts & 4) { // -r n.n.n.n
|
2015-08-04 01:27:56 +00:00
|
|
|
struct in_addr ip;
|
2006-09-03 12:27:25 +00:00
|
|
|
if (inet_aton(r_opt, &ip) == 0
|
2014-10-30 12:43:09 +00:00
|
|
|
|| (ntohl(ip.s_addr) & IN_CLASSB_NET) != G.localnet_ip
|
2007-01-19 21:19:35 +00:00
|
|
|
) {
|
2006-09-03 12:27:25 +00:00
|
|
|
bb_error_msg_and_die("invalid link address");
|
2005-05-03 03:28:55 +00:00
|
|
|
}
|
2015-08-04 01:27:56 +00:00
|
|
|
chosen_nip = ip.s_addr;
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
2008-10-10 11:28:39 +00:00
|
|
|
argv += optind - 1;
|
|
|
|
|
|
|
|
/* Now: argv[0]:junk argv[1]:intf argv[2]:script argv[3]:NULL */
|
|
|
|
/* We need to make space for script argument: */
|
|
|
|
argv[0] = argv[1];
|
|
|
|
argv[1] = argv[2];
|
|
|
|
/* Now: argv[0]:intf argv[1]:script argv[2]:junk argv[3]:NULL */
|
|
|
|
#define argv_intf (argv[0])
|
2007-03-26 17:25:33 +00:00
|
|
|
|
2008-10-10 11:28:39 +00:00
|
|
|
xsetenv("interface", argv_intf);
|
2005-05-03 03:28:55 +00:00
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Initialize the interface (modprobe, ifup, etc)
|
2015-08-04 01:27:56 +00:00
|
|
|
if (run(argv, "init", 0))
|
2005-04-30 03:49:37 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Initialize G.iface_sockaddr
|
2015-08-04 01:27:56 +00:00
|
|
|
// G.iface_sockaddr is: { u16 sa_family; u8 sa_data[14]; }
|
|
|
|
//memset(&G.iface_sockaddr, 0, sizeof(G.iface_sockaddr));
|
2007-11-25 12:40:56 +00:00
|
|
|
//TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
|
2015-08-04 01:27:56 +00:00
|
|
|
safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data));
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Bind to the interface's ARP socket
|
2015-08-04 01:27:56 +00:00
|
|
|
xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr));
|
2006-09-03 12:23:56 +00:00
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Get the interface's ethernet address
|
2007-02-14 20:50:22 +00:00
|
|
|
//memset(&ifr, 0, sizeof(ifr));
|
2008-12-02 18:18:50 +00:00
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
|
2007-11-25 12:40:56 +00:00
|
|
|
xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
|
2015-08-04 12:30:31 +00:00
|
|
|
memcpy(&G.our_ethaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
|
2006-09-03 12:23:56 +00:00
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Start with some stable ip address, either a function of
|
2006-09-03 12:23:56 +00:00
|
|
|
// the hardware address or else the last address we used.
|
2007-11-25 12:40:56 +00:00
|
|
|
// we are taking low-order four bytes, as top-order ones
|
|
|
|
// aren't random enough.
|
2006-09-03 12:23:56 +00:00
|
|
|
// NOTE: the sequence of addresses we try changes only
|
|
|
|
// depending on when we detect conflicts.
|
2007-11-25 12:40:56 +00:00
|
|
|
{
|
2008-12-08 22:56:18 +00:00
|
|
|
uint32_t t;
|
2015-08-04 12:30:31 +00:00
|
|
|
move_from_unaligned32(t, ((char *)&G.our_ethaddr + 2));
|
2007-11-25 12:40:56 +00:00
|
|
|
srand(t);
|
|
|
|
}
|
2005-05-03 03:28:55 +00:00
|
|
|
// FIXME cases to handle:
|
|
|
|
// - zcip already running!
|
|
|
|
// - link already has local address... just defend/update
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Daemonize now; don't delay system startup
|
2006-09-03 12:27:25 +00:00
|
|
|
if (!FOREGROUND) {
|
2007-04-11 07:04:23 +00:00
|
|
|
#if BB_MMU
|
2008-02-28 18:02:23 +00:00
|
|
|
bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/);
|
2007-03-26 17:25:33 +00:00
|
|
|
#endif
|
2016-03-30 14:22:13 +00:00
|
|
|
bb_error_msg("start, interface %s", argv_intf);
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Run the dynamic address negotiation protocol,
|
2005-05-03 03:28:55 +00:00
|
|
|
// restarting after address conflicts:
|
|
|
|
// - start with some address we want to try
|
|
|
|
// - short random delay
|
2008-02-04 09:42:05 +00:00
|
|
|
// - arp probes to see if another host uses it
|
2015-08-04 13:06:38 +00:00
|
|
|
// 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff arp who-has 169.254.194.171 tell 0.0.0.0
|
2005-05-03 03:28:55 +00:00
|
|
|
// - arp announcements that we're claiming it
|
2015-08-04 13:06:38 +00:00
|
|
|
// 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
|
2005-05-03 03:28:55 +00:00
|
|
|
// - use it
|
|
|
|
// - defend it, within limits
|
2008-10-10 11:28:39 +00:00
|
|
|
// exit if:
|
|
|
|
// - address is successfully obtained and -q was given:
|
|
|
|
// run "<script> config", then exit with exitcode 0
|
|
|
|
// - poll error (when does this happen?)
|
|
|
|
// - read error (when does this happen?)
|
2015-08-04 12:30:31 +00:00
|
|
|
// - sendto error (in send_arp_request()) (when does this happen?)
|
2008-10-10 11:28:39 +00:00
|
|
|
// - revents & POLLERR (link down). run "<script> deconfig" first
|
2015-08-04 12:30:31 +00:00
|
|
|
if (chosen_nip == 0) {
|
|
|
|
new_nip_and_PROBE:
|
|
|
|
chosen_nip = pick_nip();
|
|
|
|
}
|
|
|
|
nsent = 0;
|
2008-10-10 11:28:39 +00:00
|
|
|
state = PROBE;
|
2005-04-30 03:49:37 +00:00
|
|
|
while (1) {
|
|
|
|
struct pollfd fds[1];
|
2015-10-24 17:15:44 +00:00
|
|
|
unsigned deadline_us = deadline_us;
|
2005-04-30 03:49:37 +00:00
|
|
|
struct arp_packet p;
|
2015-08-04 10:28:21 +00:00
|
|
|
int ip_conflict;
|
2015-08-04 12:30:31 +00:00
|
|
|
int n;
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2007-11-25 12:40:56 +00:00
|
|
|
fds[0].fd = sock_fd;
|
2005-04-30 03:49:37 +00:00
|
|
|
fds[0].events = POLLIN;
|
|
|
|
fds[0].revents = 0;
|
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Poll, being ready to adjust current timeout
|
2007-06-17 23:40:26 +00:00
|
|
|
if (!timeout_ms) {
|
2007-11-25 12:40:56 +00:00
|
|
|
timeout_ms = random_delay_ms(PROBE_WAIT);
|
|
|
|
// FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to
|
2005-05-03 03:28:55 +00:00
|
|
|
// make the kernel filter out all packets except
|
|
|
|
// ones we'd care about.
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
2015-10-24 17:14:04 +00:00
|
|
|
if (timeout_ms >= 0) {
|
|
|
|
// Set deadline_us to the point in time when we timeout
|
|
|
|
deadline_us = MONOTONIC_US() + timeout_ms * 1000;
|
|
|
|
}
|
2006-09-17 16:28:10 +00:00
|
|
|
|
2015-08-04 10:28:21 +00:00
|
|
|
VDBG("...wait %d %s nsent=%u\n",
|
|
|
|
timeout_ms, argv_intf, nsent);
|
2007-09-27 10:09:59 +00:00
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
n = safe_poll(fds, 1, timeout_ms);
|
|
|
|
if (n < 0) {
|
2008-02-04 09:42:05 +00:00
|
|
|
//bb_perror_msg("poll"); - done in safe_poll
|
2007-09-27 10:09:59 +00:00
|
|
|
return EXIT_FAILURE;
|
2015-08-04 12:30:31 +00:00
|
|
|
}
|
|
|
|
if (n == 0) { // timed out?
|
|
|
|
VDBG("state:%d\n", state);
|
2006-09-03 12:20:36 +00:00
|
|
|
switch (state) {
|
|
|
|
case PROBE:
|
2015-08-04 12:30:31 +00:00
|
|
|
// No conflicting ARP packets were seen:
|
|
|
|
// we can progress through the states
|
2015-08-04 10:28:21 +00:00
|
|
|
if (nsent < PROBE_NUM) {
|
|
|
|
nsent++;
|
2007-06-17 23:40:26 +00:00
|
|
|
VDBG("probe/%u %s@%s\n",
|
2015-08-04 10:28:21 +00:00
|
|
|
nsent, argv_intf, nip_to_a(chosen_nip));
|
2014-07-20 12:01:49 +00:00
|
|
|
timeout_ms = PROBE_MIN * 1000;
|
|
|
|
timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
|
2015-08-04 12:30:31 +00:00
|
|
|
send_arp_request(0, &null_ethaddr, chosen_nip);
|
|
|
|
continue;
|
2006-09-03 12:20:36 +00:00
|
|
|
}
|
2015-10-12 23:51:37 +00:00
|
|
|
// Switch to announce state
|
2015-08-04 10:28:21 +00:00
|
|
|
nsent = 0;
|
2015-08-04 01:27:56 +00:00
|
|
|
state = ANNOUNCE;
|
|
|
|
goto send_announce;
|
2006-09-03 12:20:36 +00:00
|
|
|
case ANNOUNCE:
|
2015-08-04 12:30:31 +00:00
|
|
|
// No conflicting ARP packets were seen:
|
|
|
|
// we can progress through the states
|
2015-08-04 10:28:21 +00:00
|
|
|
if (nsent < ANNOUNCE_NUM) {
|
2015-08-04 01:27:56 +00:00
|
|
|
send_announce:
|
2015-08-04 10:28:21 +00:00
|
|
|
nsent++;
|
2007-06-17 23:40:26 +00:00
|
|
|
VDBG("announce/%u %s@%s\n",
|
2015-08-04 10:28:21 +00:00
|
|
|
nsent, argv_intf, nip_to_a(chosen_nip));
|
2014-07-20 12:01:49 +00:00
|
|
|
timeout_ms = ANNOUNCE_INTERVAL * 1000;
|
2015-08-04 12:30:31 +00:00
|
|
|
send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
|
|
|
|
continue;
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
2015-08-04 10:28:21 +00:00
|
|
|
// Switch to monitor state
|
2015-08-04 01:27:56 +00:00
|
|
|
// FIXME update filters
|
|
|
|
run(argv, "config", chosen_nip);
|
|
|
|
// NOTE: all other exit paths should deconfig...
|
|
|
|
if (QUIT)
|
|
|
|
return EXIT_SUCCESS;
|
2015-08-04 13:06:38 +00:00
|
|
|
// fall through: switch to MONITOR
|
2015-08-04 12:30:31 +00:00
|
|
|
default:
|
|
|
|
// case DEFEND:
|
|
|
|
// case MONITOR: (shouldn't happen, MONITOR timeout is infinite)
|
2015-08-04 10:28:21 +00:00
|
|
|
// Defend period ended with no ARP replies - we won
|
2015-08-04 12:30:31 +00:00
|
|
|
timeout_ms = -1; // never timeout in monitor state
|
2015-08-04 01:27:56 +00:00
|
|
|
state = MONITOR;
|
2005-04-30 03:49:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-08-04 12:30:31 +00:00
|
|
|
}
|
2006-09-03 12:20:36 +00:00
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
// Packet arrived, or link went down.
|
|
|
|
// We need to adjust the timeout in case we didn't receive
|
|
|
|
// a conflicting packet.
|
|
|
|
if (timeout_ms > 0) {
|
|
|
|
unsigned diff = deadline_us - MONOTONIC_US();
|
|
|
|
if ((int)(diff) < 0) {
|
|
|
|
// Current time is greater than the expected timeout time.
|
|
|
|
diff = 0;
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
2015-08-04 12:30:31 +00:00
|
|
|
VDBG("adjusting timeout\n");
|
|
|
|
timeout_ms = (diff / 1000) | 1; // never 0
|
|
|
|
}
|
2015-08-04 01:27:56 +00:00
|
|
|
|
2015-08-04 12:30:31 +00:00
|
|
|
if ((fds[0].revents & POLLIN) == 0) {
|
|
|
|
if (fds[0].revents & POLLERR) {
|
|
|
|
// FIXME: links routinely go down;
|
|
|
|
// this shouldn't necessarily exit.
|
|
|
|
bb_error_msg("iface %s is down", argv_intf);
|
|
|
|
if (state >= MONITOR) {
|
|
|
|
// Only if we are in MONITOR or DEFEND
|
|
|
|
run(argv, "deconfig", chosen_nip);
|
|
|
|
}
|
|
|
|
return EXIT_FAILURE;
|
2015-08-04 01:27:56 +00:00
|
|
|
}
|
2015-08-04 12:30:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read ARP packet
|
|
|
|
if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
|
|
|
|
bb_perror_msg_and_die(bb_msg_read_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.eth.ether_type != htons(ETHERTYPE_ARP))
|
|
|
|
continue;
|
|
|
|
if (p.arp.arp_op != htons(ARPOP_REQUEST)
|
|
|
|
&& p.arp.arp_op != htons(ARPOP_REPLY)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-03 12:20:36 +00:00
|
|
|
#ifdef DEBUG
|
2015-08-04 12:30:31 +00:00
|
|
|
{
|
|
|
|
struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
|
|
|
|
struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha;
|
|
|
|
struct in_addr *spa = (struct in_addr *) p.arp.arp_spa;
|
|
|
|
struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
|
|
|
|
VDBG("source=%s %s\n", ether_ntoa(sha), inet_ntoa(*spa));
|
|
|
|
VDBG("target=%s %s\n", ether_ntoa(tha), inet_ntoa(*tpa));
|
|
|
|
}
|
2006-09-03 12:20:36 +00:00
|
|
|
#endif
|
2015-08-04 12:30:31 +00:00
|
|
|
ip_conflict = 0;
|
|
|
|
if (memcmp(&p.arp.arp_sha, &G.our_ethaddr, ETH_ALEN) != 0) {
|
|
|
|
if (memcmp(p.arp.arp_spa, &chosen_nip, 4) == 0) {
|
|
|
|
// A probe or reply with source_ip == chosen ip
|
|
|
|
ip_conflict = 1;
|
2006-09-03 12:20:36 +00:00
|
|
|
}
|
2015-08-04 12:30:31 +00:00
|
|
|
if (p.arp.arp_op == htons(ARPOP_REQUEST)
|
|
|
|
&& memcmp(p.arp.arp_spa, &const_int_0, 4) == 0
|
|
|
|
&& memcmp(p.arp.arp_tpa, &chosen_nip, 4) == 0
|
|
|
|
) {
|
|
|
|
// A probe with source_ip == 0.0.0.0, target_ip == chosen ip:
|
|
|
|
// another host trying to claim this ip!
|
|
|
|
ip_conflict |= 2;
|
2015-08-04 10:28:21 +00:00
|
|
|
}
|
2015-08-04 12:30:31 +00:00
|
|
|
}
|
|
|
|
VDBG("state:%d ip_conflict:%d\n", state, ip_conflict);
|
|
|
|
if (!ip_conflict)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Either src or target IP conflict exists
|
|
|
|
if (state <= ANNOUNCE) {
|
|
|
|
// PROBE or ANNOUNCE
|
|
|
|
conflicts++;
|
|
|
|
timeout_ms = PROBE_MIN * 1000
|
|
|
|
+ CONFLICT_MULTIPLIER * random_delay_ms(conflicts);
|
|
|
|
goto new_nip_and_PROBE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// MONITOR or DEFEND: only src IP conflict is a problem
|
|
|
|
if (ip_conflict & 1) {
|
|
|
|
if (state == MONITOR) {
|
|
|
|
// Src IP conflict, defend with a single ARP probe
|
|
|
|
VDBG("monitor conflict - defending\n");
|
|
|
|
timeout_ms = DEFEND_INTERVAL * 1000;
|
|
|
|
state = DEFEND;
|
|
|
|
send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
|
|
|
|
continue;
|
2015-08-04 10:28:21 +00:00
|
|
|
}
|
2015-08-04 12:30:31 +00:00
|
|
|
// state == DEFEND
|
|
|
|
// Another src IP conflict, start over
|
|
|
|
VDBG("defend conflict - starting over\n");
|
|
|
|
run(argv, "deconfig", chosen_nip);
|
|
|
|
conflicts = 0;
|
|
|
|
timeout_ms = 0;
|
|
|
|
goto new_nip_and_PROBE;
|
|
|
|
}
|
|
|
|
// Note: if we only have a target IP conflict here (ip_conflict & 2),
|
|
|
|
// IOW: if we just saw this sort of ARP packet:
|
2015-08-04 13:06:38 +00:00
|
|
|
// aa:bb:cc:dd:ee:ff > xx:xx:xx:xx:xx:xx arp who-has <chosen_nip> tell 0.0.0.0
|
2015-08-04 12:30:31 +00:00
|
|
|
// we expect _kernel_ to respond to that, because <chosen_nip>
|
|
|
|
// is (expected to be) configured on this iface.
|
2007-09-27 10:09:59 +00:00
|
|
|
} // while (1)
|
2008-10-10 11:28:39 +00:00
|
|
|
#undef argv_intf
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|