2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2005-04-30 09:19:37 +05:30
|
|
|
/*
|
|
|
|
* 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 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2005-04-30 09:19:37 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-09-03 17:50:36 +05:30
|
|
|
//#define DEBUG
|
2005-05-03 08:58:55 +05:30
|
|
|
|
|
|
|
// 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 06:59:49 +05:30
|
|
|
//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 18:13:09 +05:30
|
|
|
//usage: "\n -l x.x.0.0 Use this range instead of 169.254"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -v Verbose"
|
|
|
|
//usage: "\n"
|
2014-11-04 16:49:04 +05:30
|
|
|
//usage: "\n$LOGGING=none Suppress logging"
|
|
|
|
//usage: "\n$LOGGING=syslog Log to syslog"
|
|
|
|
//usage: "\n"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\nWith no -q, runs continuously monitoring for ARP conflicts,"
|
|
|
|
//usage: "\nexits only on I/O errors (link down etc)"
|
|
|
|
|
2011-06-10 08:47:59 +05:30
|
|
|
#include "libbb.h"
|
2005-04-30 09:19:37 +05:30
|
|
|
#include <netinet/ether.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
#include <linux/sockios.h>
|
2005-05-03 08:58:55 +05:30
|
|
|
|
2008-01-27 18:20:12 +05:30
|
|
|
#include <syslog.h>
|
2007-06-18 05:10:26 +05:30
|
|
|
|
|
|
|
/* We don't need more than 32 bits of the counter */
|
|
|
|
#define MONOTONIC_US() ((unsigned)monotonic_us())
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2005-12-13 15:58:25 +05:30
|
|
|
struct arp_packet {
|
2007-11-25 18:10:56 +05:30
|
|
|
struct ether_header eth;
|
2006-09-03 17:50:36 +05:30
|
|
|
struct ether_arp arp;
|
2008-07-05 14:48:54 +05:30
|
|
|
} PACKED;
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2006-03-11 00:52:06 +05:30
|
|
|
enum {
|
2005-04-30 09:19:37 +05:30
|
|
|
/* 169.254.0.0 */
|
2006-03-11 00:52:06 +05:30
|
|
|
LINKLOCAL_ADDR = 0xa9fe0000,
|
2005-04-30 09:19:37 +05:30
|
|
|
|
|
|
|
/* protocol timeout parameters, specified in seconds */
|
2006-03-11 00:52:06 +05:30
|
|
|
PROBE_WAIT = 1,
|
|
|
|
PROBE_MIN = 1,
|
|
|
|
PROBE_MAX = 2,
|
|
|
|
PROBE_NUM = 3,
|
|
|
|
MAX_CONFLICTS = 10,
|
|
|
|
RATE_LIMIT_INTERVAL = 60,
|
|
|
|
ANNOUNCE_WAIT = 2,
|
|
|
|
ANNOUNCE_NUM = 2,
|
|
|
|
ANNOUNCE_INTERVAL = 2,
|
|
|
|
DEFEND_INTERVAL = 10
|
|
|
|
};
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2006-09-03 17:50:36 +05:30
|
|
|
/* States during the configuration process. */
|
|
|
|
enum {
|
|
|
|
PROBE = 0,
|
|
|
|
RATE_LIMIT_PROBE,
|
|
|
|
ANNOUNCE,
|
|
|
|
MONITOR,
|
|
|
|
DEFEND
|
|
|
|
};
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2007-11-25 18:10:56 +05:30
|
|
|
#define VDBG(...) do { } while (0)
|
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
sock_fd = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
struct globals {
|
|
|
|
struct sockaddr saddr;
|
2008-10-10 16:58:39 +05:30
|
|
|
struct ether_addr eth_addr;
|
2014-10-30 18:13:09 +05:30
|
|
|
uint32_t localnet_ip;
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2007-11-25 18:10:56 +05:30
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
2008-10-10 16:58:39 +05:30
|
|
|
#define saddr (G.saddr )
|
|
|
|
#define eth_addr (G.eth_addr)
|
2011-09-21 05:29:15 +05:30
|
|
|
#define INIT_G() do { } while (0)
|
2007-11-25 18:10:56 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
|
|
|
|
/**
|
2005-04-30 09:19:37 +05:30
|
|
|
* Pick a random link local IP address on 169.254/16, except that
|
|
|
|
* the first and last 256 addresses are reserved.
|
|
|
|
*/
|
2014-10-30 18:13:09 +05:30
|
|
|
static uint32_t pick_nip(void)
|
2005-04-30 09:19:37 +05:30
|
|
|
{
|
2006-09-03 17:59:53 +05:30
|
|
|
unsigned tmp;
|
2005-04-30 09:19:37 +05:30
|
|
|
|
|
|
|
do {
|
2007-06-18 05:10:26 +05:30
|
|
|
tmp = rand() & IN_CLASSB_HOST;
|
2005-04-30 09:19:37 +05:30
|
|
|
} while (tmp > (IN_CLASSB_HOST - 0x0200));
|
2014-10-30 18:13:09 +05:30
|
|
|
return htonl((G.localnet_ip + 0x0100) + tmp);
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
/**
|
2005-04-30 09:19:37 +05:30
|
|
|
* Broadcast an ARP packet.
|
|
|
|
*/
|
2008-10-10 16:58:39 +05:30
|
|
|
static void arp(
|
|
|
|
/* int op, - always ARPOP_REQUEST */
|
|
|
|
/* const struct ether_addr *source_eth, - always ð_addr */
|
|
|
|
struct in_addr source_ip,
|
2007-11-25 18:10:56 +05:30
|
|
|
const struct ether_addr *target_eth, struct in_addr target_ip)
|
2005-04-30 09:19:37 +05:30
|
|
|
{
|
2008-10-10 16:58:39 +05:30
|
|
|
enum { op = ARPOP_REQUEST };
|
|
|
|
#define source_eth (ð_addr)
|
|
|
|
|
2005-04-30 09:19:37 +05:30
|
|
|
struct arp_packet p;
|
2006-09-03 17:50:36 +05:30
|
|
|
memset(&p, 0, sizeof(p));
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// ether header
|
2007-11-25 18:10:56 +05:30
|
|
|
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 09:19:37 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// arp request
|
2006-09-03 17:50:36 +05:30
|
|
|
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 18:10:56 +05:30
|
|
|
memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN);
|
2007-02-15 02:20:22 +05:30
|
|
|
memcpy(&p.arp.arp_spa, &source_ip, sizeof(p.arp.arp_spa));
|
2007-11-25 18:10:56 +05:30
|
|
|
memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
|
2007-02-15 02:20:22 +05:30
|
|
|
memcpy(&p.arp.arp_tpa, &target_ip, sizeof(p.arp.arp_tpa));
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// send it
|
2008-10-10 16:58:39 +05:30
|
|
|
// Even though sock_fd is already bound to saddr, just send()
|
|
|
|
// won't work, because "socket is not connected"
|
|
|
|
// (and connect() won't fix that, "operation not supported").
|
|
|
|
// Thus we sendto() to saddr. I wonder which sockaddr
|
|
|
|
// (from bind() or from sendto()?) kernel actually uses
|
|
|
|
// to determine iface to emit the packet from...
|
2007-11-25 18:10:56 +05:30
|
|
|
xsendto(sock_fd, &p, sizeof(p), &saddr, sizeof(saddr));
|
2008-10-10 16:58:39 +05:30
|
|
|
#undef source_eth
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
/**
|
2008-10-10 16:58:39 +05:30
|
|
|
* Run a script.
|
|
|
|
* argv[0]:intf argv[1]:script_name argv[2]:junk argv[3]:NULL
|
2005-04-30 09:19:37 +05:30
|
|
|
*/
|
2008-10-10 16:58:39 +05:30
|
|
|
static int run(char *argv[3], const char *param, struct in_addr *ip)
|
2005-04-30 09:19:37 +05:30
|
|
|
{
|
2007-03-26 22:55:33 +05:30
|
|
|
int status;
|
2007-11-25 18:10:56 +05:30
|
|
|
char *addr = addr; /* for gcc */
|
|
|
|
const char *fmt = "%s %s %s" + 3;
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2008-10-10 16:58:39 +05:30
|
|
|
argv[2] = (char*)param;
|
|
|
|
|
|
|
|
VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]);
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2007-03-26 22:55:33 +05:30
|
|
|
if (ip) {
|
2007-11-25 18:10:56 +05:30
|
|
|
addr = inet_ntoa(*ip);
|
2008-09-20 21:58:59 +05:30
|
|
|
xsetenv("ip", addr);
|
2007-11-25 18:10:56 +05:30
|
|
|
fmt -= 3;
|
2007-03-26 22:55:33 +05:30
|
|
|
}
|
2008-10-10 16:58:39 +05:30
|
|
|
bb_info_msg(fmt, argv[2], argv[0], addr);
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2010-03-19 03:14:00 +05:30
|
|
|
status = spawn_and_wait(argv + 1);
|
2007-03-26 22:55:33 +05:30
|
|
|
if (status < 0) {
|
2008-10-10 16:58:39 +05:30
|
|
|
bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
|
2007-03-26 22:55:33 +05:30
|
|
|
return -errno;
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
2007-03-26 22:55:33 +05:30
|
|
|
if (status != 0)
|
2010-03-19 03:14:00 +05:30
|
|
|
bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
|
2005-04-30 09:19:37 +05:30
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
/**
|
2005-04-30 09:19:37 +05:30
|
|
|
* Return milliseconds of random delay, up to "secs" seconds.
|
|
|
|
*/
|
2008-05-16 03:00:45 +05:30
|
|
|
static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
|
2005-04-30 09:19:37 +05:30
|
|
|
{
|
2007-06-18 05:10:26 +05:30
|
|
|
return rand() % (secs * 1000);
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
/**
|
2005-04-30 09:19:37 +05:30
|
|
|
* main program
|
|
|
|
*/
|
2007-10-11 15:35:36 +05:30
|
|
|
int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2010-01-04 18:45:38 +05:30
|
|
|
int zcip_main(int argc UNUSED_PARAM, char **argv)
|
2005-04-30 09:19:37 +05:30
|
|
|
{
|
2008-10-10 16:58:39 +05:30
|
|
|
int state;
|
2007-03-26 22:55:33 +05:30
|
|
|
char *r_opt;
|
2014-10-30 18:13:09 +05:30
|
|
|
const char *l_opt = "169.254.0.0";
|
2007-03-26 22:55:33 +05:30
|
|
|
unsigned opts;
|
|
|
|
|
2008-02-04 15:12:05 +05:30
|
|
|
// ugly trick, but I want these zeroed in one go
|
2007-03-26 22:55:33 +05:30
|
|
|
struct {
|
|
|
|
const struct in_addr null_ip;
|
|
|
|
const struct ether_addr null_addr;
|
|
|
|
struct in_addr ip;
|
|
|
|
struct ifreq ifr;
|
2007-06-18 05:10:26 +05:30
|
|
|
int timeout_ms; /* must be signed */
|
2007-03-26 22:55:33 +05:30
|
|
|
unsigned conflicts;
|
|
|
|
unsigned nprobes;
|
|
|
|
unsigned nclaims;
|
|
|
|
int ready;
|
|
|
|
int verbose;
|
|
|
|
} L;
|
2007-06-18 05:10:26 +05:30
|
|
|
#define null_ip (L.null_ip )
|
|
|
|
#define null_addr (L.null_addr )
|
|
|
|
#define ip (L.ip )
|
|
|
|
#define ifr (L.ifr )
|
|
|
|
#define timeout_ms (L.timeout_ms)
|
|
|
|
#define conflicts (L.conflicts )
|
|
|
|
#define nprobes (L.nprobes )
|
|
|
|
#define nclaims (L.nclaims )
|
|
|
|
#define ready (L.ready )
|
|
|
|
#define verbose (L.verbose )
|
2007-03-26 22:55:33 +05:30
|
|
|
|
|
|
|
memset(&L, 0, sizeof(L));
|
2011-09-21 05:29:15 +05:30
|
|
|
INIT_G();
|
2006-09-03 17:53:56 +05:30
|
|
|
|
2007-03-26 22:55:33 +05:30
|
|
|
#define FOREGROUND (opts & 1)
|
|
|
|
#define QUIT (opts & 2)
|
2005-05-03 08:58:55 +05:30
|
|
|
// parse commandline: prog [options] ifname script
|
2007-03-26 22:55:33 +05:30
|
|
|
// exactly 2 args; -v accumulates and implies -f
|
|
|
|
opt_complementary = "=2:vv:vf";
|
2014-10-30 18:13:09 +05:30
|
|
|
opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose);
|
2008-02-04 15:12:05 +05:30
|
|
|
#if !BB_MMU
|
|
|
|
// on NOMMU reexec early (or else we will rerun things twice)
|
|
|
|
if (!FOREGROUND)
|
2008-02-28 23:32:23 +05:30
|
|
|
bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv);
|
2008-02-04 15:12:05 +05:30
|
|
|
#endif
|
|
|
|
// open an ARP socket
|
|
|
|
// (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-07 00:06:50 +05:30
|
|
|
if (!FOREGROUND) {
|
2008-02-04 15:12:05 +05:30
|
|
|
// do it before all bb_xx_msg calls
|
2006-10-04 02:30:43 +05:30
|
|
|
openlog(applet_name, 0, LOG_DAEMON);
|
2007-03-26 22:55:33 +05:30
|
|
|
logmode |= LOGMODE_SYSLOG;
|
2006-09-07 00:06:50 +05:30
|
|
|
}
|
2014-11-04 16:49:04 +05:30
|
|
|
bb_logenv_override();
|
|
|
|
|
2014-10-30 18:13:09 +05:30
|
|
|
{ // -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 17:59:53 +05:30
|
|
|
if (opts & 4) { // -r n.n.n.n
|
2006-09-03 17:57:25 +05:30
|
|
|
if (inet_aton(r_opt, &ip) == 0
|
2014-10-30 18:13:09 +05:30
|
|
|
|| (ntohl(ip.s_addr) & IN_CLASSB_NET) != G.localnet_ip
|
2007-01-20 02:49:35 +05:30
|
|
|
) {
|
2006-09-03 17:57:25 +05:30
|
|
|
bb_error_msg_and_die("invalid link address");
|
2005-05-03 08:58:55 +05:30
|
|
|
}
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
2008-10-10 16:58:39 +05:30
|
|
|
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 22:55:33 +05:30
|
|
|
|
2008-10-10 16:58:39 +05:30
|
|
|
xsetenv("interface", argv_intf);
|
2005-05-03 08:58:55 +05:30
|
|
|
|
|
|
|
// initialize the interface (modprobe, ifup, etc)
|
2008-10-10 16:58:39 +05:30
|
|
|
if (run(argv, "init", NULL))
|
2005-04-30 09:19:37 +05:30
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// initialize saddr
|
2007-11-25 18:10:56 +05:30
|
|
|
// saddr is: { u16 sa_family; u8 sa_data[14]; }
|
2007-02-15 02:20:22 +05:30
|
|
|
//memset(&saddr, 0, sizeof(saddr));
|
2007-11-25 18:10:56 +05:30
|
|
|
//TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
|
2008-10-10 16:58:39 +05:30
|
|
|
safe_strncpy(saddr.sa_data, argv_intf, sizeof(saddr.sa_data));
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2006-09-03 17:53:56 +05:30
|
|
|
// bind to the interface's ARP socket
|
2007-11-25 18:10:56 +05:30
|
|
|
xbind(sock_fd, &saddr, sizeof(saddr));
|
2006-09-03 17:53:56 +05:30
|
|
|
|
|
|
|
// get the interface's ethernet address
|
2007-02-15 02:20:22 +05:30
|
|
|
//memset(&ifr, 0, sizeof(ifr));
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
|
2007-11-25 18:10:56 +05:30
|
|
|
xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
|
2006-09-03 17:56:06 +05:30
|
|
|
memcpy(ð_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
|
2006-09-03 17:53:56 +05:30
|
|
|
|
|
|
|
// start with some stable ip address, either a function of
|
|
|
|
// the hardware address or else the last address we used.
|
2007-11-25 18:10:56 +05:30
|
|
|
// we are taking low-order four bytes, as top-order ones
|
|
|
|
// aren't random enough.
|
2006-09-03 17:53:56 +05:30
|
|
|
// NOTE: the sequence of addresses we try changes only
|
|
|
|
// depending on when we detect conflicts.
|
2007-11-25 18:10:56 +05:30
|
|
|
{
|
2008-12-09 04:26:18 +05:30
|
|
|
uint32_t t;
|
|
|
|
move_from_unaligned32(t, ((char *)ð_addr + 2));
|
2007-11-25 18:10:56 +05:30
|
|
|
srand(t);
|
|
|
|
}
|
2006-09-03 17:53:56 +05:30
|
|
|
if (ip.s_addr == 0)
|
2014-10-30 18:13:09 +05:30
|
|
|
ip.s_addr = pick_nip();
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// FIXME cases to handle:
|
|
|
|
// - zcip already running!
|
|
|
|
// - link already has local address... just defend/update
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// daemonize now; don't delay system startup
|
2006-09-03 17:57:25 +05:30
|
|
|
if (!FOREGROUND) {
|
2007-04-11 12:34:23 +05:30
|
|
|
#if BB_MMU
|
2008-02-28 23:32:23 +05:30
|
|
|
bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/);
|
2007-03-26 22:55:33 +05:30
|
|
|
#endif
|
2008-10-10 16:58:39 +05:30
|
|
|
bb_info_msg("start, interface %s", argv_intf);
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// run the dynamic address negotiation protocol,
|
|
|
|
// restarting after address conflicts:
|
|
|
|
// - start with some address we want to try
|
|
|
|
// - short random delay
|
2008-02-04 15:12:05 +05:30
|
|
|
// - arp probes to see if another host uses it
|
2005-05-03 08:58:55 +05:30
|
|
|
// - arp announcements that we're claiming it
|
|
|
|
// - use it
|
|
|
|
// - defend it, within limits
|
2008-10-10 16:58:39 +05:30
|
|
|
// 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?)
|
|
|
|
// - sendto error (in arp()) (when does this happen?)
|
|
|
|
// - revents & POLLERR (link down). run "<script> deconfig" first
|
|
|
|
state = PROBE;
|
2005-04-30 09:19:37 +05:30
|
|
|
while (1) {
|
|
|
|
struct pollfd fds[1];
|
2007-06-18 05:10:26 +05:30
|
|
|
unsigned deadline_us;
|
2005-04-30 09:19:37 +05:30
|
|
|
struct arp_packet p;
|
2007-11-25 18:10:56 +05:30
|
|
|
int source_ip_conflict;
|
|
|
|
int target_ip_conflict;
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2007-11-25 18:10:56 +05:30
|
|
|
fds[0].fd = sock_fd;
|
2005-04-30 09:19:37 +05:30
|
|
|
fds[0].events = POLLIN;
|
|
|
|
fds[0].revents = 0;
|
|
|
|
|
2006-01-25 05:38:53 +05:30
|
|
|
// poll, being ready to adjust current timeout
|
2007-06-18 05:10:26 +05:30
|
|
|
if (!timeout_ms) {
|
2007-11-25 18:10:56 +05:30
|
|
|
timeout_ms = random_delay_ms(PROBE_WAIT);
|
|
|
|
// FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to
|
2005-05-03 08:58:55 +05:30
|
|
|
// make the kernel filter out all packets except
|
|
|
|
// ones we'd care about.
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
2007-06-18 05:10:26 +05:30
|
|
|
// set deadline_us to the point in time when we timeout
|
|
|
|
deadline_us = MONOTONIC_US() + timeout_ms * 1000;
|
2006-09-17 21:58:10 +05:30
|
|
|
|
2007-06-18 05:10:26 +05:30
|
|
|
VDBG("...wait %d %s nprobes=%u, nclaims=%u\n",
|
2008-10-10 16:58:39 +05:30
|
|
|
timeout_ms, argv_intf, nprobes, nclaims);
|
2007-09-27 15:39:59 +05:30
|
|
|
|
|
|
|
switch (safe_poll(fds, 1, timeout_ms)) {
|
|
|
|
|
|
|
|
default:
|
2008-02-04 15:12:05 +05:30
|
|
|
//bb_perror_msg("poll"); - done in safe_poll
|
2007-09-27 15:39:59 +05:30
|
|
|
return EXIT_FAILURE;
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2006-09-03 17:50:36 +05:30
|
|
|
// timeout
|
2005-04-30 09:19:37 +05:30
|
|
|
case 0:
|
2006-09-03 17:50:36 +05:30
|
|
|
VDBG("state = %d\n", state);
|
|
|
|
switch (state) {
|
|
|
|
case PROBE:
|
2006-09-03 17:59:53 +05:30
|
|
|
// timeouts in the PROBE state mean no conflicting ARP packets
|
2006-09-03 17:50:36 +05:30
|
|
|
// have been received, so we can progress through the states
|
2005-04-30 09:19:37 +05:30
|
|
|
if (nprobes < PROBE_NUM) {
|
2006-09-03 17:50:36 +05:30
|
|
|
nprobes++;
|
2007-06-18 05:10:26 +05:30
|
|
|
VDBG("probe/%u %s@%s\n",
|
2008-10-10 16:58:39 +05:30
|
|
|
nprobes, argv_intf, inet_ntoa(ip));
|
2014-07-20 17:31:49 +05:30
|
|
|
timeout_ms = PROBE_MIN * 1000;
|
|
|
|
timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
|
2008-10-10 16:58:39 +05:30
|
|
|
arp(/* ARPOP_REQUEST, */
|
|
|
|
/* ð_addr, */ null_ip,
|
2006-09-03 17:50:36 +05:30
|
|
|
&null_addr, ip);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Switch to announce state.
|
|
|
|
state = ANNOUNCE;
|
|
|
|
nclaims = 0;
|
2007-06-18 05:10:26 +05:30
|
|
|
VDBG("announce/%u %s@%s\n",
|
2008-10-10 16:58:39 +05:30
|
|
|
nclaims, argv_intf, inet_ntoa(ip));
|
2014-07-20 17:31:49 +05:30
|
|
|
timeout_ms = ANNOUNCE_INTERVAL * 1000;
|
2008-10-10 16:58:39 +05:30
|
|
|
arp(/* ARPOP_REQUEST, */
|
|
|
|
/* ð_addr, */ ip,
|
2006-09-03 17:56:06 +05:30
|
|
|
ð_addr, ip);
|
2006-09-03 17:50:36 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RATE_LIMIT_PROBE:
|
2006-09-03 17:59:53 +05:30
|
|
|
// timeouts in the RATE_LIMIT_PROBE state mean no conflicting ARP packets
|
2006-09-03 17:50:36 +05:30
|
|
|
// have been received, so we can move immediately to the announce state
|
|
|
|
state = ANNOUNCE;
|
|
|
|
nclaims = 0;
|
2007-06-18 05:10:26 +05:30
|
|
|
VDBG("announce/%u %s@%s\n",
|
2008-10-10 16:58:39 +05:30
|
|
|
nclaims, argv_intf, inet_ntoa(ip));
|
2014-07-20 17:31:49 +05:30
|
|
|
timeout_ms = ANNOUNCE_INTERVAL * 1000;
|
2008-10-10 16:58:39 +05:30
|
|
|
arp(/* ARPOP_REQUEST, */
|
|
|
|
/* ð_addr, */ ip,
|
2006-09-03 17:56:06 +05:30
|
|
|
ð_addr, ip);
|
2006-09-03 17:50:36 +05:30
|
|
|
break;
|
|
|
|
case ANNOUNCE:
|
2006-09-03 17:59:53 +05:30
|
|
|
// timeouts in the ANNOUNCE state mean no conflicting ARP packets
|
2006-09-03 17:50:36 +05:30
|
|
|
// have been received, so we can progress through the states
|
2005-04-30 09:19:37 +05:30
|
|
|
if (nclaims < ANNOUNCE_NUM) {
|
2006-09-03 17:50:36 +05:30
|
|
|
nclaims++;
|
2007-06-18 05:10:26 +05:30
|
|
|
VDBG("announce/%u %s@%s\n",
|
2008-10-10 16:58:39 +05:30
|
|
|
nclaims, argv_intf, inet_ntoa(ip));
|
2014-07-20 17:31:49 +05:30
|
|
|
timeout_ms = ANNOUNCE_INTERVAL * 1000;
|
2008-10-10 16:58:39 +05:30
|
|
|
arp(/* ARPOP_REQUEST, */
|
|
|
|
/* ð_addr, */ ip,
|
2006-09-03 17:56:06 +05:30
|
|
|
ð_addr, ip);
|
2006-09-03 17:50:36 +05:30
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Switch to monitor state.
|
|
|
|
state = MONITOR;
|
2005-05-03 08:58:55 +05:30
|
|
|
// link is ok to use earlier
|
2006-09-03 17:50:36 +05:30
|
|
|
// FIXME update filters
|
2008-10-10 16:58:39 +05:30
|
|
|
run(argv, "config", &ip);
|
2005-04-30 09:19:37 +05:30
|
|
|
ready = 1;
|
|
|
|
conflicts = 0;
|
2007-06-18 05:10:26 +05:30
|
|
|
timeout_ms = -1; // Never timeout in the monitor state.
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2006-09-03 17:59:53 +05:30
|
|
|
// NOTE: all other exit paths
|
2005-05-03 08:58:55 +05:30
|
|
|
// should deconfig ...
|
2006-09-03 17:57:25 +05:30
|
|
|
if (QUIT)
|
2005-04-30 09:19:37 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2006-09-03 17:50:36 +05:30
|
|
|
break;
|
|
|
|
case DEFEND:
|
|
|
|
// We won! No ARP replies, so just go back to monitor.
|
|
|
|
state = MONITOR;
|
2007-06-18 05:10:26 +05:30
|
|
|
timeout_ms = -1;
|
2006-09-03 17:50:36 +05:30
|
|
|
conflicts = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Invalid, should never happen. Restart the whole protocol.
|
|
|
|
state = PROBE;
|
2014-10-30 18:13:09 +05:30
|
|
|
ip.s_addr = pick_nip();
|
2007-06-18 05:10:26 +05:30
|
|
|
timeout_ms = 0;
|
2006-09-03 17:50:36 +05:30
|
|
|
nprobes = 0;
|
|
|
|
nclaims = 0;
|
|
|
|
break;
|
|
|
|
} // switch (state)
|
|
|
|
break; // case 0 (timeout)
|
2007-09-27 15:39:59 +05:30
|
|
|
|
2007-11-25 18:10:56 +05:30
|
|
|
// packets arriving, or link went down
|
2005-04-30 09:19:37 +05:30
|
|
|
case 1:
|
2006-09-03 17:50:36 +05:30
|
|
|
// We need to adjust the timeout in case we didn't receive
|
|
|
|
// a conflicting packet.
|
2007-06-18 05:10:26 +05:30
|
|
|
if (timeout_ms > 0) {
|
|
|
|
unsigned diff = deadline_us - MONOTONIC_US();
|
|
|
|
if ((int)(diff) < 0) {
|
2006-09-03 17:50:36 +05:30
|
|
|
// Current time is greater than the expected timeout time.
|
|
|
|
// Should never happen.
|
|
|
|
VDBG("missed an expected timeout\n");
|
2007-06-18 05:10:26 +05:30
|
|
|
timeout_ms = 0;
|
2005-04-30 09:19:37 +05:30
|
|
|
} else {
|
2006-09-03 17:50:36 +05:30
|
|
|
VDBG("adjusting timeout\n");
|
2007-11-25 18:10:56 +05:30
|
|
|
timeout_ms = (diff / 1000) | 1; /* never 0 */
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
|
|
|
}
|
2006-09-03 17:50:36 +05:30
|
|
|
|
2005-04-30 09:19:37 +05:30
|
|
|
if ((fds[0].revents & POLLIN) == 0) {
|
|
|
|
if (fds[0].revents & POLLERR) {
|
2005-05-03 08:58:55 +05:30
|
|
|
// FIXME: links routinely go down;
|
|
|
|
// this shouldn't necessarily exit.
|
2008-10-10 16:58:39 +05:30
|
|
|
bb_error_msg("iface %s is down", argv_intf);
|
2005-04-30 09:19:37 +05:30
|
|
|
if (ready) {
|
2008-10-10 16:58:39 +05:30
|
|
|
run(argv, "deconfig", &ip);
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-03 17:50:36 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// read ARP packet
|
2007-11-25 18:10:56 +05:30
|
|
|
if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
|
2007-11-25 18:23:25 +05:30
|
|
|
bb_perror_msg_and_die(bb_msg_read_error);
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
2007-11-25 18:10:56 +05:30
|
|
|
if (p.eth.ether_type != htons(ETHERTYPE_ARP))
|
2005-04-30 09:19:37 +05:30
|
|
|
continue;
|
2006-09-03 17:50:36 +05:30
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2007-11-25 18:10:56 +05:30
|
|
|
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;
|
2006-09-03 17:50:36 +05:30
|
|
|
VDBG("%s recv arp type=%d, op=%d,\n",
|
2008-10-10 16:58:39 +05:30
|
|
|
argv_intf, ntohs(p.eth.ether_type),
|
2006-09-03 17:50:36 +05:30
|
|
|
ntohs(p.arp.arp_op));
|
|
|
|
VDBG("\tsource=%s %s\n",
|
|
|
|
ether_ntoa(sha),
|
|
|
|
inet_ntoa(*spa));
|
|
|
|
VDBG("\ttarget=%s %s\n",
|
|
|
|
ether_ntoa(tha),
|
|
|
|
inet_ntoa(*tpa));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (p.arp.arp_op != htons(ARPOP_REQUEST)
|
2014-07-20 17:31:49 +05:30
|
|
|
&& p.arp.arp_op != htons(ARPOP_REPLY)
|
|
|
|
) {
|
2005-04-30 09:19:37 +05:30
|
|
|
continue;
|
2014-07-20 17:31:49 +05:30
|
|
|
}
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2007-11-25 18:10:56 +05:30
|
|
|
source_ip_conflict = 0;
|
|
|
|
target_ip_conflict = 0;
|
|
|
|
|
2014-07-20 17:31:49 +05:30
|
|
|
if (memcmp(&p.arp.arp_sha, ð_addr, ETH_ALEN) != 0) {
|
|
|
|
if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr))) {
|
|
|
|
/* A probe or reply with source_ip == chosen ip */
|
|
|
|
source_ip_conflict = 1;
|
|
|
|
}
|
|
|
|
if (p.arp.arp_op == htons(ARPOP_REQUEST)
|
|
|
|
&& memcmp(p.arp.arp_spa, &null_ip, sizeof(struct in_addr)) == 0
|
|
|
|
&& memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0
|
|
|
|
) {
|
|
|
|
/* A probe with source_ip == 0.0.0.0, target_ip == chosen ip:
|
|
|
|
* another host trying to claim this ip!
|
|
|
|
*/
|
|
|
|
target_ip_conflict = 1;
|
|
|
|
}
|
2006-09-03 17:50:36 +05:30
|
|
|
}
|
|
|
|
|
2006-09-17 21:58:10 +05:30
|
|
|
VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
|
2006-09-03 17:50:36 +05:30
|
|
|
state, source_ip_conflict, target_ip_conflict);
|
|
|
|
switch (state) {
|
|
|
|
case PROBE:
|
|
|
|
case ANNOUNCE:
|
|
|
|
// When probing or announcing, check for source IP conflicts
|
|
|
|
// and other hosts doing ARP probes (target IP conflicts).
|
|
|
|
if (source_ip_conflict || target_ip_conflict) {
|
|
|
|
conflicts++;
|
|
|
|
if (conflicts >= MAX_CONFLICTS) {
|
2008-10-10 16:58:39 +05:30
|
|
|
VDBG("%s ratelimit\n", argv_intf);
|
2007-06-18 05:10:26 +05:30
|
|
|
timeout_ms = RATE_LIMIT_INTERVAL * 1000;
|
2006-09-03 17:50:36 +05:30
|
|
|
state = RATE_LIMIT_PROBE;
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
2006-09-03 17:50:36 +05:30
|
|
|
|
|
|
|
// restart the whole protocol
|
2014-10-30 18:13:09 +05:30
|
|
|
ip.s_addr = pick_nip();
|
2007-06-18 05:10:26 +05:30
|
|
|
timeout_ms = 0;
|
2006-09-03 17:50:36 +05:30
|
|
|
nprobes = 0;
|
|
|
|
nclaims = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MONITOR:
|
|
|
|
// If a conflict, we try to defend with a single ARP probe.
|
|
|
|
if (source_ip_conflict) {
|
|
|
|
VDBG("monitor conflict -- defending\n");
|
|
|
|
state = DEFEND;
|
2007-06-18 05:10:26 +05:30
|
|
|
timeout_ms = DEFEND_INTERVAL * 1000;
|
2008-10-10 16:58:39 +05:30
|
|
|
arp(/* ARPOP_REQUEST, */
|
|
|
|
/* ð_addr, */ ip,
|
2007-11-25 18:10:56 +05:30
|
|
|
ð_addr, ip);
|
2006-09-03 17:50:36 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DEFEND:
|
|
|
|
// Well, we tried. Start over (on conflict).
|
|
|
|
if (source_ip_conflict) {
|
|
|
|
state = PROBE;
|
|
|
|
VDBG("defend conflict -- starting over\n");
|
2005-04-30 09:19:37 +05:30
|
|
|
ready = 0;
|
2008-10-10 16:58:39 +05:30
|
|
|
run(argv, "deconfig", &ip);
|
2006-09-03 17:50:36 +05:30
|
|
|
|
|
|
|
// restart the whole protocol
|
2014-10-30 18:13:09 +05:30
|
|
|
ip.s_addr = pick_nip();
|
2007-06-18 05:10:26 +05:30
|
|
|
timeout_ms = 0;
|
2006-09-03 17:50:36 +05:30
|
|
|
nprobes = 0;
|
|
|
|
nclaims = 0;
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
2006-09-03 17:50:36 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Invalid, should never happen. Restart the whole protocol.
|
|
|
|
VDBG("invalid state -- starting over\n");
|
|
|
|
state = PROBE;
|
2014-10-30 18:13:09 +05:30
|
|
|
ip.s_addr = pick_nip();
|
2007-06-18 05:10:26 +05:30
|
|
|
timeout_ms = 0;
|
2005-04-30 09:19:37 +05:30
|
|
|
nprobes = 0;
|
|
|
|
nclaims = 0;
|
2006-09-03 17:50:36 +05:30
|
|
|
break;
|
|
|
|
} // switch state
|
|
|
|
break; // case 1 (packets arriving)
|
|
|
|
} // switch poll
|
2007-09-27 15:39:59 +05:30
|
|
|
} // while (1)
|
2008-10-10 16:58:39 +05:30
|
|
|
#undef argv_intf
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|