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
|
|
|
|
*
|
2006-01-22 22:55:11 +00:00
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
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.
|
|
|
|
*/
|
|
|
|
|
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)
|
|
|
|
|
2005-04-30 03:49:37 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
#include <linux/if_packet.h>
|
|
|
|
#include <linux/sockios.h>
|
2005-05-03 03:28:55 +00:00
|
|
|
|
2007-06-17 23:40:26 +00:00
|
|
|
#include "libbb.h"
|
|
|
|
|
|
|
|
/* 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;
|
2006-01-22 22:55:11 +00:00
|
|
|
} ATTRIBUTE_PACKED;
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2006-03-10 19:22:06 +00:00
|
|
|
enum {
|
2005-04-30 03:49:37 +00:00
|
|
|
/* 169.254.0.0 */
|
2006-03-10 19:22:06 +00:00
|
|
|
LINKLOCAL_ADDR = 0xa9fe0000,
|
2005-04-30 03:49:37 +00:00
|
|
|
|
|
|
|
/* protocol timeout parameters, specified in seconds */
|
2006-03-10 19:22:06 +00:00
|
|
|
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 03:49:37 +00:00
|
|
|
|
2006-09-03 12:20:36 +00:00
|
|
|
/* States during the configuration process. */
|
|
|
|
enum {
|
|
|
|
PROBE = 0,
|
|
|
|
RATE_LIMIT_PROBE,
|
|
|
|
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 {
|
|
|
|
char *intf;
|
|
|
|
struct sockaddr saddr;
|
|
|
|
};
|
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
|
|
|
#define intf (G.intf )
|
|
|
|
#define saddr (G.saddr)
|
|
|
|
|
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.
|
|
|
|
*/
|
2006-06-22 22:28:29 +00:00
|
|
|
static void pick(struct in_addr *ip)
|
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));
|
|
|
|
ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
|
|
|
|
}
|
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
/**
|
2005-04-30 03:49:37 +00:00
|
|
|
* Broadcast an ARP packet.
|
|
|
|
*/
|
2007-11-25 12:40:56 +00:00
|
|
|
static void arp(int op,
|
|
|
|
const struct ether_addr *source_eth, struct in_addr source_ip,
|
|
|
|
const struct ether_addr *target_eth, struct in_addr target_ip)
|
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);
|
2007-02-14 20:50:22 +00:00
|
|
|
memcpy(&p.arp.arp_spa, &source_ip, sizeof(p.arp.arp_spa));
|
2007-11-25 12:40:56 +00:00
|
|
|
memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
|
2007-02-14 20:50:22 +00:00
|
|
|
memcpy(&p.arp.arp_tpa, &target_ip, sizeof(p.arp.arp_tpa));
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
// send it
|
2007-11-25 12:40:56 +00:00
|
|
|
xsendto(sock_fd, &p, sizeof(p), &saddr, sizeof(saddr));
|
2007-04-12 11:34:39 +00:00
|
|
|
|
2006-09-03 12:29:53 +00:00
|
|
|
// Currently all callers ignore errors, that's why returns are
|
|
|
|
// commented out...
|
|
|
|
//return 0;
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
/**
|
2007-03-26 17:25:33 +00:00
|
|
|
* Run a script. argv[2] is already NULL.
|
2005-04-30 03:49:37 +00:00
|
|
|
*/
|
2007-11-25 12:40:56 +00:00
|
|
|
static int run(char *argv[3], struct in_addr *ip)
|
2005-04-30 03:49:37 +00:00
|
|
|
{
|
2007-03-26 17:25:33 +00:00
|
|
|
int status;
|
2007-11-25 12:40:56 +00:00
|
|
|
char *addr = addr; /* for gcc */
|
|
|
|
const char *fmt = "%s %s %s" + 3;
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2007-03-26 17:25:33 +00:00
|
|
|
VDBG("%s run %s %s\n", intf, argv[0], argv[1]);
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2007-03-26 17:25:33 +00:00
|
|
|
if (ip) {
|
2007-11-25 12:40:56 +00:00
|
|
|
addr = inet_ntoa(*ip);
|
2007-03-26 17:25:33 +00:00
|
|
|
setenv("ip", addr, 1);
|
2007-11-25 12:40:56 +00:00
|
|
|
fmt -= 3;
|
2007-03-26 17:25:33 +00:00
|
|
|
}
|
2007-11-25 12:40:56 +00:00
|
|
|
bb_info_msg(fmt, argv[1], intf, addr);
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2007-03-26 17:25:33 +00:00
|
|
|
status = wait4pid(spawn(argv));
|
|
|
|
if (status < 0) {
|
2007-11-25 12:40:56 +00:00
|
|
|
bb_perror_msg("%s %s %s" + 3, argv[1], intf);
|
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)
|
|
|
|
bb_error_msg("script %s %s failed, exitcode=%d", argv[0], argv[1], status);
|
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.
|
|
|
|
*/
|
2007-11-25 12:40:56 +00:00
|
|
|
static unsigned ALWAYS_INLINE random_delay_ms(unsigned secs)
|
2005-04-30 03:49:37 +00:00
|
|
|
{
|
2007-06-17 23:40:26 +00:00
|
|
|
return 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;
|
2007-04-04 20:52:03 +00:00
|
|
|
int zcip_main(int argc, char **argv)
|
2005-04-30 03:49:37 +00:00
|
|
|
{
|
2007-03-26 17:25:33 +00:00
|
|
|
int state = PROBE;
|
2007-11-25 12:40:56 +00:00
|
|
|
struct ether_addr eth_addr;
|
2007-03-26 17:25:33 +00:00
|
|
|
char *r_opt;
|
|
|
|
unsigned opts;
|
|
|
|
|
|
|
|
/* Ugly trick, but I want these zeroed in one go */
|
|
|
|
struct {
|
|
|
|
const struct in_addr null_ip;
|
|
|
|
const struct ether_addr null_addr;
|
|
|
|
struct in_addr ip;
|
|
|
|
struct ifreq ifr;
|
|
|
|
char *script_av[3];
|
2007-06-17 23:40:26 +00:00
|
|
|
int timeout_ms; /* must be signed */
|
2007-03-26 17:25:33 +00:00
|
|
|
unsigned conflicts;
|
|
|
|
unsigned nprobes;
|
|
|
|
unsigned nclaims;
|
|
|
|
int ready;
|
|
|
|
int verbose;
|
|
|
|
} L;
|
2007-06-17 23:40:26 +00:00
|
|
|
#define null_ip (L.null_ip )
|
|
|
|
#define null_addr (L.null_addr )
|
|
|
|
#define ip (L.ip )
|
|
|
|
#define ifr (L.ifr )
|
|
|
|
#define script_av (L.script_av )
|
|
|
|
#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 17:25:33 +00:00
|
|
|
|
|
|
|
memset(&L, 0, sizeof(L));
|
2006-09-03 12:23:56 +00:00
|
|
|
|
2007-03-26 17:25:33 +00:00
|
|
|
#define FOREGROUND (opts & 1)
|
|
|
|
#define QUIT (opts & 2)
|
2005-05-03 03:28:55 +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";
|
2007-08-18 15:32:12 +00:00
|
|
|
opts = getopt32(argv, "fqr:v", &r_opt, &verbose);
|
2006-09-06 18:36:50 +00:00
|
|
|
if (!FOREGROUND) {
|
|
|
|
/* Do it early, 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
|
|
|
}
|
2006-09-03 12:29:53 +00:00
|
|
|
if (opts & 4) { // -r n.n.n.n
|
2006-09-03 12:27:25 +00:00
|
|
|
if (inet_aton(r_opt, &ip) == 0
|
2007-01-19 21:19:35 +00:00
|
|
|
|| (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR
|
|
|
|
) {
|
2006-09-03 12:27:25 +00:00
|
|
|
bb_error_msg_and_die("invalid link address");
|
2005-05-03 03:28:55 +00:00
|
|
|
}
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
2007-03-26 17:25:33 +00:00
|
|
|
// On NOMMU reexec early (or else we will rerun things twice)
|
2007-04-11 07:04:23 +00:00
|
|
|
#if !BB_MMU
|
2007-03-26 17:25:33 +00:00
|
|
|
if (!FOREGROUND)
|
|
|
|
bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
|
|
|
|
#endif
|
2006-09-03 12:27:25 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2007-03-26 17:25:33 +00:00
|
|
|
|
2006-09-03 12:27:25 +00:00
|
|
|
intf = argv[0];
|
2007-03-26 17:25:33 +00:00
|
|
|
script_av[0] = argv[1];
|
2006-09-03 12:27:25 +00:00
|
|
|
setenv("interface", intf, 1);
|
2005-05-03 03:28:55 +00:00
|
|
|
|
|
|
|
// initialize the interface (modprobe, ifup, etc)
|
2007-03-26 17:25:33 +00:00
|
|
|
script_av[1] = (char*)"init";
|
2007-11-25 12:40:56 +00:00
|
|
|
if (run(script_av, NULL))
|
2005-04-30 03:49:37 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
// initialize saddr
|
2007-11-25 12:40:56 +00:00
|
|
|
// saddr is: { u16 sa_family; u8 sa_data[14]; }
|
2007-02-14 20:50:22 +00:00
|
|
|
//memset(&saddr, 0, sizeof(saddr));
|
2007-11-25 12:40:56 +00:00
|
|
|
//TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
|
2007-02-14 20:50:22 +00:00
|
|
|
safe_strncpy(saddr.sa_data, intf, sizeof(saddr.sa_data));
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
// open an ARP socket
|
2007-11-25 12:40:56 +00:00
|
|
|
xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
|
2006-09-03 12:23:56 +00:00
|
|
|
// bind to the interface's ARP socket
|
2007-11-25 12:40:56 +00:00
|
|
|
xbind(sock_fd, &saddr, sizeof(saddr));
|
2006-09-03 12:23:56 +00:00
|
|
|
|
|
|
|
// get the interface's ethernet address
|
2007-02-14 20:50:22 +00:00
|
|
|
//memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, intf, sizeof(ifr.ifr_name));
|
2007-11-25 12:40:56 +00:00
|
|
|
xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
|
2006-09-03 12:26:06 +00:00
|
|
|
memcpy(ð_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
|
2006-09-03 12:23:56 +00:00
|
|
|
|
|
|
|
// start with some stable ip address, either a function of
|
|
|
|
// 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
|
|
|
{
|
|
|
|
uint32_t t;
|
|
|
|
memcpy(&t, (char*)ð_addr + 2, 4);
|
|
|
|
srand(t);
|
|
|
|
}
|
2006-09-03 12:23:56 +00:00
|
|
|
if (ip.s_addr == 0)
|
|
|
|
pick(&ip);
|
2005-04-30 03:49:37 +00:00
|
|
|
|
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
|
|
|
|
2005-05-03 03:28:55 +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
|
2007-03-26 13:20:54 +00:00
|
|
|
bb_daemonize(DAEMON_CHDIR_ROOT);
|
2007-03-26 17:25:33 +00:00
|
|
|
#endif
|
2006-09-06 18:36:50 +00:00
|
|
|
bb_info_msg("start, interface %s", intf);
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
// run the dynamic address negotiation protocol,
|
|
|
|
// restarting after address conflicts:
|
|
|
|
// - start with some address we want to try
|
|
|
|
// - short random delay
|
|
|
|
// - arp probes to see if another host else uses it
|
|
|
|
// - arp announcements that we're claiming it
|
|
|
|
// - use it
|
|
|
|
// - defend it, within limits
|
2005-04-30 03:49:37 +00:00
|
|
|
while (1) {
|
|
|
|
struct pollfd fds[1];
|
2007-06-17 23:40:26 +00:00
|
|
|
unsigned deadline_us;
|
2005-04-30 03:49:37 +00:00
|
|
|
struct arp_packet p;
|
2007-11-25 12:40:56 +00:00
|
|
|
int source_ip_conflict;
|
|
|
|
int target_ip_conflict;
|
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;
|
|
|
|
|
2006-01-25 00:08:53 +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
|
|
|
}
|
2007-06-17 23:40:26 +00:00
|
|
|
// 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
|
|
|
|
2007-06-17 23:40:26 +00:00
|
|
|
VDBG("...wait %d %s nprobes=%u, nclaims=%u\n",
|
|
|
|
timeout_ms, intf, nprobes, nclaims);
|
2007-09-27 10:09:59 +00:00
|
|
|
|
|
|
|
switch (safe_poll(fds, 1, timeout_ms)) {
|
|
|
|
|
|
|
|
default:
|
|
|
|
/*bb_perror_msg("poll"); - done in safe_poll */
|
|
|
|
return EXIT_FAILURE;
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2006-09-03 12:20:36 +00:00
|
|
|
// timeout
|
2005-04-30 03:49:37 +00:00
|
|
|
case 0:
|
2006-09-03 12:20:36 +00:00
|
|
|
VDBG("state = %d\n", state);
|
|
|
|
switch (state) {
|
|
|
|
case PROBE:
|
2006-09-03 12:29:53 +00:00
|
|
|
// timeouts in the PROBE state mean no conflicting ARP packets
|
2006-09-03 12:20:36 +00:00
|
|
|
// have been received, so we can progress through the states
|
2005-04-30 03:49:37 +00:00
|
|
|
if (nprobes < PROBE_NUM) {
|
2006-09-03 12:20:36 +00:00
|
|
|
nprobes++;
|
2007-06-17 23:40:26 +00:00
|
|
|
VDBG("probe/%u %s@%s\n",
|
2006-09-03 12:20:36 +00:00
|
|
|
nprobes, intf, inet_ntoa(ip));
|
2007-11-25 12:40:56 +00:00
|
|
|
arp(ARPOP_REQUEST,
|
2006-09-03 12:26:06 +00:00
|
|
|
ð_addr, null_ip,
|
2006-09-03 12:20:36 +00:00
|
|
|
&null_addr, ip);
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = PROBE_MIN * 1000;
|
2007-11-25 12:40:56 +00:00
|
|
|
timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
|
2006-09-03 12:20:36 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Switch to announce state.
|
|
|
|
state = ANNOUNCE;
|
|
|
|
nclaims = 0;
|
2007-06-17 23:40:26 +00:00
|
|
|
VDBG("announce/%u %s@%s\n",
|
2006-09-03 12:20:36 +00:00
|
|
|
nclaims, intf, inet_ntoa(ip));
|
2007-11-25 12:40:56 +00:00
|
|
|
arp(ARPOP_REQUEST,
|
2006-09-03 12:26:06 +00:00
|
|
|
ð_addr, ip,
|
|
|
|
ð_addr, ip);
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = ANNOUNCE_INTERVAL * 1000;
|
2006-09-03 12:20:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RATE_LIMIT_PROBE:
|
2006-09-03 12:29:53 +00:00
|
|
|
// timeouts in the RATE_LIMIT_PROBE state mean no conflicting ARP packets
|
2006-09-03 12:20:36 +00:00
|
|
|
// have been received, so we can move immediately to the announce state
|
|
|
|
state = ANNOUNCE;
|
|
|
|
nclaims = 0;
|
2007-06-17 23:40:26 +00:00
|
|
|
VDBG("announce/%u %s@%s\n",
|
2005-05-03 03:28:55 +00:00
|
|
|
nclaims, intf, inet_ntoa(ip));
|
2007-11-25 12:40:56 +00:00
|
|
|
arp(ARPOP_REQUEST,
|
2006-09-03 12:26:06 +00:00
|
|
|
ð_addr, ip,
|
|
|
|
ð_addr, ip);
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = ANNOUNCE_INTERVAL * 1000;
|
2006-09-03 12:20:36 +00:00
|
|
|
break;
|
|
|
|
case ANNOUNCE:
|
2006-09-03 12:29:53 +00:00
|
|
|
// timeouts in the ANNOUNCE state mean no conflicting ARP packets
|
2006-09-03 12:20:36 +00:00
|
|
|
// have been received, so we can progress through the states
|
2005-04-30 03:49:37 +00:00
|
|
|
if (nclaims < ANNOUNCE_NUM) {
|
2006-09-03 12:20:36 +00:00
|
|
|
nclaims++;
|
2007-06-17 23:40:26 +00:00
|
|
|
VDBG("announce/%u %s@%s\n",
|
2006-09-03 12:20:36 +00:00
|
|
|
nclaims, intf, inet_ntoa(ip));
|
2007-11-25 12:40:56 +00:00
|
|
|
arp(ARPOP_REQUEST,
|
2006-09-03 12:26:06 +00:00
|
|
|
ð_addr, ip,
|
|
|
|
ð_addr, ip);
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = ANNOUNCE_INTERVAL * 1000;
|
2006-09-03 12:20:36 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Switch to monitor state.
|
|
|
|
state = MONITOR;
|
2005-05-03 03:28:55 +00:00
|
|
|
// link is ok to use earlier
|
2006-09-03 12:20:36 +00:00
|
|
|
// FIXME update filters
|
2007-03-26 17:25:33 +00:00
|
|
|
script_av[1] = (char*)"config";
|
2007-11-25 12:40:56 +00:00
|
|
|
run(script_av, &ip);
|
2005-04-30 03:49:37 +00:00
|
|
|
ready = 1;
|
|
|
|
conflicts = 0;
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = -1; // Never timeout in the monitor state.
|
2005-04-30 03:49:37 +00:00
|
|
|
|
2006-09-03 12:29:53 +00:00
|
|
|
// NOTE: all other exit paths
|
2005-05-03 03:28:55 +00:00
|
|
|
// should deconfig ...
|
2006-09-03 12:27:25 +00:00
|
|
|
if (QUIT)
|
2005-04-30 03:49:37 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2006-09-03 12:20:36 +00:00
|
|
|
break;
|
|
|
|
case DEFEND:
|
|
|
|
// We won! No ARP replies, so just go back to monitor.
|
|
|
|
state = MONITOR;
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = -1;
|
2006-09-03 12:20:36 +00:00
|
|
|
conflicts = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Invalid, should never happen. Restart the whole protocol.
|
|
|
|
state = PROBE;
|
|
|
|
pick(&ip);
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = 0;
|
2006-09-03 12:20:36 +00:00
|
|
|
nprobes = 0;
|
|
|
|
nclaims = 0;
|
|
|
|
break;
|
|
|
|
} // switch (state)
|
|
|
|
break; // case 0 (timeout)
|
2007-09-27 10:09:59 +00:00
|
|
|
|
2007-11-25 12:40:56 +00:00
|
|
|
// packets arriving, or link went down
|
2005-04-30 03:49:37 +00:00
|
|
|
case 1:
|
2006-09-03 12:20:36 +00:00
|
|
|
// We need to adjust the timeout in case we didn't receive
|
|
|
|
// a conflicting packet.
|
2007-06-17 23:40:26 +00:00
|
|
|
if (timeout_ms > 0) {
|
|
|
|
unsigned diff = deadline_us - MONOTONIC_US();
|
|
|
|
if ((int)(diff) < 0) {
|
2006-09-03 12:20:36 +00:00
|
|
|
// Current time is greater than the expected timeout time.
|
|
|
|
// Should never happen.
|
|
|
|
VDBG("missed an expected timeout\n");
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = 0;
|
2005-04-30 03:49:37 +00:00
|
|
|
} else {
|
2006-09-03 12:20:36 +00:00
|
|
|
VDBG("adjusting timeout\n");
|
2007-11-25 12:40:56 +00:00
|
|
|
timeout_ms = (diff / 1000) | 1; /* never 0 */
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
2006-09-03 12:20:36 +00:00
|
|
|
|
2005-04-30 03:49:37 +00:00
|
|
|
if ((fds[0].revents & POLLIN) == 0) {
|
|
|
|
if (fds[0].revents & POLLERR) {
|
2005-05-03 03:28:55 +00:00
|
|
|
// FIXME: links routinely go down;
|
|
|
|
// this shouldn't necessarily exit.
|
2007-11-25 12:40:56 +00:00
|
|
|
bb_error_msg("iface %s is down", intf);
|
2005-04-30 03:49:37 +00:00
|
|
|
if (ready) {
|
2007-03-26 17:25:33 +00:00
|
|
|
script_av[1] = (char*)"deconfig";
|
2007-11-25 12:40:56 +00:00
|
|
|
run(script_av, &ip);
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-03 12:20:36 +00:00
|
|
|
|
2005-05-03 03:28:55 +00:00
|
|
|
// read ARP packet
|
2007-11-25 12:40:56 +00:00
|
|
|
if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
|
2007-11-25 12:53:25 +00:00
|
|
|
bb_perror_msg_and_die(bb_msg_read_error);
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
2007-11-25 12:40:56 +00:00
|
|
|
if (p.eth.ether_type != htons(ETHERTYPE_ARP))
|
2005-04-30 03:49:37 +00:00
|
|
|
continue;
|
2006-09-03 12:20:36 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2007-11-25 12:40:56 +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;
|
2006-09-03 12:20:36 +00:00
|
|
|
VDBG("%s recv arp type=%d, op=%d,\n",
|
2007-11-25 12:40:56 +00:00
|
|
|
intf, ntohs(p.eth.ether_type),
|
2006-09-03 12:20:36 +00:00
|
|
|
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)
|
2007-11-25 12:40:56 +00:00
|
|
|
&& p.arp.arp_op != htons(ARPOP_REPLY))
|
2005-04-30 03:49:37 +00:00
|
|
|
continue;
|
|
|
|
|
2007-11-25 12:40:56 +00:00
|
|
|
source_ip_conflict = 0;
|
|
|
|
target_ip_conflict = 0;
|
|
|
|
|
|
|
|
if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0
|
|
|
|
&& memcmp(&p.arp.arp_sha, ð_addr, ETH_ALEN) != 0
|
|
|
|
) {
|
2006-09-03 12:20:36 +00:00
|
|
|
source_ip_conflict = 1;
|
|
|
|
}
|
2007-11-25 12:40:56 +00:00
|
|
|
if (p.arp.arp_op == htons(ARPOP_REQUEST)
|
|
|
|
&& memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0
|
|
|
|
&& memcmp(&p.arp.arp_tha, ð_addr, ETH_ALEN) != 0
|
|
|
|
) {
|
2006-09-03 12:20:36 +00:00
|
|
|
target_ip_conflict = 1;
|
|
|
|
}
|
|
|
|
|
2006-09-17 16:28:10 +00:00
|
|
|
VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
|
2006-09-03 12:20:36 +00:00
|
|
|
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) {
|
|
|
|
VDBG("%s ratelimit\n", intf);
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = RATE_LIMIT_INTERVAL * 1000;
|
2006-09-03 12:20:36 +00:00
|
|
|
state = RATE_LIMIT_PROBE;
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
2006-09-03 12:20:36 +00:00
|
|
|
|
|
|
|
// restart the whole protocol
|
|
|
|
pick(&ip);
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = 0;
|
2006-09-03 12:20:36 +00:00
|
|
|
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-17 23:40:26 +00:00
|
|
|
timeout_ms = DEFEND_INTERVAL * 1000;
|
2007-11-25 12:40:56 +00:00
|
|
|
arp(ARPOP_REQUEST,
|
|
|
|
ð_addr, ip,
|
|
|
|
ð_addr, ip);
|
2006-09-03 12:20:36 +00:00
|
|
|
}
|
|
|
|
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 03:49:37 +00:00
|
|
|
ready = 0;
|
2007-03-26 17:25:33 +00:00
|
|
|
script_av[1] = (char*)"deconfig";
|
2007-11-25 12:40:56 +00:00
|
|
|
run(script_av, &ip);
|
2006-09-03 12:20:36 +00:00
|
|
|
|
|
|
|
// restart the whole protocol
|
|
|
|
pick(&ip);
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = 0;
|
2006-09-03 12:20:36 +00:00
|
|
|
nprobes = 0;
|
|
|
|
nclaims = 0;
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|
2006-09-03 12:20:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Invalid, should never happen. Restart the whole protocol.
|
|
|
|
VDBG("invalid state -- starting over\n");
|
|
|
|
state = PROBE;
|
2005-04-30 03:49:37 +00:00
|
|
|
pick(&ip);
|
2007-06-17 23:40:26 +00:00
|
|
|
timeout_ms = 0;
|
2005-04-30 03:49:37 +00:00
|
|
|
nprobes = 0;
|
|
|
|
nclaims = 0;
|
2006-09-03 12:20:36 +00:00
|
|
|
break;
|
|
|
|
} // switch state
|
|
|
|
break; // case 1 (packets arriving)
|
|
|
|
} // switch poll
|
2007-09-27 10:09:59 +00:00
|
|
|
} // while (1)
|
2005-04-30 03:49:37 +00:00
|
|
|
}
|