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
|
|
|
|
*
|
2006-01-23 04:25:11 +05:30
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
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)
|
|
|
|
|
2006-06-23 03:58:29 +05:30
|
|
|
#include "busybox.h"
|
2005-04-30 09:19:37 +05:30
|
|
|
#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 08:58:55 +05:30
|
|
|
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2005-12-13 15:58:25 +05:30
|
|
|
struct arp_packet {
|
2005-04-30 09:19:37 +05:30
|
|
|
struct ether_header hdr;
|
2006-09-03 17:50:36 +05:30
|
|
|
struct ether_arp arp;
|
2006-01-23 04:25:11 +05:30
|
|
|
} ATTRIBUTE_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
|
|
|
|
2006-09-03 17:59:53 +05:30
|
|
|
#define VDBG(fmt,args...) \
|
2005-05-03 08:58:55 +05:30
|
|
|
do { } while (0)
|
|
|
|
|
2006-10-04 02:30:06 +05:30
|
|
|
static unsigned opts;
|
2006-09-03 17:58:32 +05:30
|
|
|
#define FOREGROUND (opts & 1)
|
|
|
|
#define QUIT (opts & 2)
|
|
|
|
|
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.
|
|
|
|
*/
|
2006-06-23 03:58:29 +05:30
|
|
|
static void pick(struct in_addr *ip)
|
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
|
|
|
|
|
|
|
/* use cheaper math than lrand48() mod N */
|
|
|
|
do {
|
|
|
|
tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
|
|
|
|
} while (tmp > (IN_CLASSB_HOST - 0x0200));
|
|
|
|
ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
|
|
|
|
}
|
|
|
|
|
2006-09-03 17:59:53 +05:30
|
|
|
/* TODO: we need a flag to direct bb_[p]error_msg output to stderr. */
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
/**
|
2005-04-30 09:19:37 +05:30
|
|
|
* Broadcast an ARP packet.
|
|
|
|
*/
|
2006-09-03 17:59:53 +05:30
|
|
|
static void arp(int fd, struct sockaddr *saddr, int op,
|
2005-04-30 09:19:37 +05:30
|
|
|
const struct ether_addr *source_addr, struct in_addr source_ip,
|
|
|
|
const struct ether_addr *target_addr, struct in_addr target_ip)
|
|
|
|
{
|
|
|
|
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
|
2005-04-30 09:19:37 +05:30
|
|
|
p.hdr.ether_type = htons(ETHERTYPE_ARP);
|
|
|
|
memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN);
|
|
|
|
memset(p.hdr.ether_dhost, 0xff, ETH_ALEN);
|
|
|
|
|
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);
|
|
|
|
memcpy(&p.arp.arp_sha, source_addr, ETH_ALEN);
|
2007-02-15 02:20:22 +05:30
|
|
|
memcpy(&p.arp.arp_spa, &source_ip, sizeof(p.arp.arp_spa));
|
2006-09-03 17:50:36 +05:30
|
|
|
memcpy(&p.arp.arp_tha, target_addr, 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
|
2007-02-15 02:20:22 +05:30
|
|
|
if (sendto(fd, &p, sizeof(p), 0, saddr, sizeof(*saddr)) < 0) {
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_perror_msg("sendto");
|
2006-09-03 17:59:53 +05:30
|
|
|
//return -errno;
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
2006-09-03 17:59:53 +05:30
|
|
|
// Currently all callers ignore errors, that's why returns are
|
|
|
|
// commented out...
|
|
|
|
//return 0;
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
/**
|
2005-04-30 09:19:37 +05:30
|
|
|
* Run a script.
|
|
|
|
*/
|
2007-01-30 05:13:18 +05:30
|
|
|
static int run(const char *script, const char *arg, const char *intf, struct in_addr *ip)
|
2005-04-30 09:19:37 +05:30
|
|
|
{
|
|
|
|
int pid, status;
|
2007-01-30 05:13:18 +05:30
|
|
|
const char *why;
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2006-09-03 17:58:32 +05:30
|
|
|
if(1) { //always true: if (script != NULL)
|
2005-05-03 08:58:55 +05:30
|
|
|
VDBG("%s run %s %s\n", intf, script, arg);
|
2005-04-30 09:19:37 +05:30
|
|
|
if (ip != NULL) {
|
|
|
|
char *addr = inet_ntoa(*ip);
|
2005-05-03 08:58:55 +05:30
|
|
|
setenv("ip", addr, 1);
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("%s %s %s", arg, intf, addr);
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
pid = vfork();
|
2005-05-03 08:58:55 +05:30
|
|
|
if (pid < 0) { // error
|
2005-04-30 09:19:37 +05:30
|
|
|
why = "vfork";
|
|
|
|
goto bad;
|
2005-05-03 08:58:55 +05:30
|
|
|
} else if (pid == 0) { // child
|
2005-04-30 09:19:37 +05:30
|
|
|
execl(script, script, arg, NULL);
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_perror_msg("execl");
|
2005-05-03 08:58:55 +05:30
|
|
|
_exit(EXIT_FAILURE);
|
2006-01-25 05:38:53 +05:30
|
|
|
}
|
2005-04-30 09:19:37 +05:30
|
|
|
|
|
|
|
if (waitpid(pid, &status, 0) <= 0) {
|
|
|
|
why = "waitpid";
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (WEXITSTATUS(status) != 0) {
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_error_msg("script %s failed, exit=%d",
|
|
|
|
script, WEXITSTATUS(status));
|
2005-04-30 09:19:37 +05:30
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
bad:
|
|
|
|
status = -errno;
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_perror_msg("%s %s, %s", arg, intf, why);
|
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.
|
|
|
|
*/
|
2006-08-30 01:11:06 +05:30
|
|
|
static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs)
|
2005-04-30 09:19:37 +05:30
|
|
|
{
|
|
|
|
return lrand48() % (secs * 1000);
|
|
|
|
}
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
/**
|
2005-04-30 09:19:37 +05:30
|
|
|
* main program
|
|
|
|
*/
|
2005-10-21 16:13:11 +05:30
|
|
|
|
2006-09-03 17:56:06 +05:30
|
|
|
/* Used to be auto variables on main() stack, but
|
|
|
|
* most of them were zero-inited. Moving them to bss
|
|
|
|
* is more space-efficient.
|
|
|
|
*/
|
|
|
|
static const struct in_addr null_ip; // = { 0 };
|
|
|
|
static const struct ether_addr null_addr; // = { {0, 0, 0, 0, 0, 0} };
|
|
|
|
|
|
|
|
static struct sockaddr saddr; // memset(0);
|
|
|
|
static struct in_addr ip; // = { 0 };
|
|
|
|
static struct ifreq ifr; //memset(0);
|
|
|
|
|
|
|
|
static char *intf; // = NULL;
|
|
|
|
static char *script; // = NULL;
|
|
|
|
static suseconds_t timeout; // = 0; // milliseconds
|
|
|
|
static unsigned conflicts; // = 0;
|
|
|
|
static unsigned nprobes; // = 0;
|
|
|
|
static unsigned nclaims; // = 0;
|
|
|
|
static int ready; // = 0;
|
|
|
|
static int verbose; // = 0;
|
|
|
|
static int state = PROBE;
|
|
|
|
|
2007-02-03 22:58:39 +05:30
|
|
|
int zcip_main(int argc, char *argv[]);
|
2005-04-30 09:19:37 +05:30
|
|
|
int zcip_main(int argc, char *argv[])
|
|
|
|
{
|
2006-09-03 17:56:06 +05:30
|
|
|
struct ether_addr eth_addr;
|
2007-01-30 05:13:18 +05:30
|
|
|
const char *why;
|
2005-04-30 09:19:37 +05:30
|
|
|
int fd;
|
2006-09-03 17:53:56 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// parse commandline: prog [options] ifname script
|
2006-09-03 17:57:25 +05:30
|
|
|
char *r_opt;
|
2006-10-04 02:30:06 +05:30
|
|
|
opt_complementary = "vv:vf"; // -v accumulates and implies -f
|
|
|
|
opts = getopt32(argc, argv, "fqr:v", &r_opt, &verbose);
|
2006-09-07 00:06:50 +05:30
|
|
|
if (!FOREGROUND) {
|
|
|
|
/* Do it early, before all bb_xx_msg calls */
|
|
|
|
logmode = LOGMODE_SYSLOG;
|
2006-10-04 02:30:43 +05:30
|
|
|
openlog(applet_name, 0, LOG_DAEMON);
|
2006-09-07 00:06:50 +05:30
|
|
|
}
|
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
|
2007-01-20 02:49:35 +05:30
|
|
|
|| (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR
|
|
|
|
) {
|
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
|
|
|
}
|
2006-09-03 17:57:25 +05:30
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc != 2)
|
2006-06-23 03:58:29 +05:30
|
|
|
bb_show_usage();
|
2006-09-03 17:57:25 +05:30
|
|
|
intf = argv[0];
|
|
|
|
script = argv[1];
|
|
|
|
setenv("interface", intf, 1);
|
2005-05-03 08:58:55 +05:30
|
|
|
|
|
|
|
// initialize the interface (modprobe, ifup, etc)
|
2005-04-30 09:19:37 +05:30
|
|
|
if (run(script, "init", intf, NULL) < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// initialize saddr
|
2007-02-15 02:20:22 +05:30
|
|
|
//memset(&saddr, 0, sizeof(saddr));
|
|
|
|
safe_strncpy(saddr.sa_data, intf, sizeof(saddr.sa_data));
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
// open an ARP socket
|
2006-09-03 17:53:56 +05:30
|
|
|
fd = xsocket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
|
|
|
|
// bind to the interface's ARP socket
|
2007-02-15 02:20:22 +05:30
|
|
|
xbind(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));
|
|
|
|
strncpy(ifr.ifr_name, intf, sizeof(ifr.ifr_name));
|
2006-09-03 17:53:56 +05:30
|
|
|
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
|
2006-09-03 17:57:25 +05:30
|
|
|
bb_perror_msg_and_die("get ethernet address");
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
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.
|
|
|
|
// NOTE: the sequence of addresses we try changes only
|
|
|
|
// depending on when we detect conflicts.
|
2006-09-03 17:56:06 +05:30
|
|
|
// (SVID 3 bogon: who says that "short" is always 16 bits?)
|
|
|
|
seed48( (unsigned short*)&ifr.ifr_hwaddr.sa_data );
|
2006-09-03 17:53:56 +05:30
|
|
|
if (ip.s_addr == 0)
|
|
|
|
pick(&ip);
|
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-02-15 02:20:22 +05:30
|
|
|
/* bb_daemonize(); - bad, will close fd! */
|
2007-03-26 04:51:05 +05:30
|
|
|
//NOMMU
|
2007-02-15 02:20:22 +05:30
|
|
|
xdaemon(0, 0);
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_info_msg("start, interface %s", 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
|
|
|
|
// - 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 09:19:37 +05:30
|
|
|
while (1) {
|
|
|
|
struct pollfd fds[1];
|
|
|
|
struct timeval tv1;
|
|
|
|
struct arp_packet p;
|
|
|
|
|
2006-09-03 17:56:06 +05:30
|
|
|
int source_ip_conflict = 0;
|
|
|
|
int target_ip_conflict = 0;
|
|
|
|
|
2005-04-30 09:19:37 +05:30
|
|
|
fds[0].fd = fd;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
fds[0].revents = 0;
|
|
|
|
|
2006-01-25 05:38:53 +05:30
|
|
|
// poll, being ready to adjust current timeout
|
2006-06-30 19:35:19 +05:30
|
|
|
if (!timeout) {
|
2005-04-30 09:19:37 +05:30
|
|
|
timeout = ms_rdelay(PROBE_WAIT);
|
2005-05-03 08:58:55 +05:30
|
|
|
// FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
|
|
|
|
// make the kernel filter out all packets except
|
|
|
|
// ones we'd care about.
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
2006-09-03 17:50:36 +05:30
|
|
|
// set tv1 to the point in time when we timeout
|
2006-06-30 19:35:19 +05:30
|
|
|
gettimeofday(&tv1, NULL);
|
|
|
|
tv1.tv_usec += (timeout % 1000) * 1000;
|
|
|
|
while (tv1.tv_usec > 1000000) {
|
|
|
|
tv1.tv_usec -= 1000000;
|
|
|
|
tv1.tv_sec++;
|
|
|
|
}
|
|
|
|
tv1.tv_sec += timeout / 1000;
|
2006-09-17 21:58:10 +05:30
|
|
|
|
2005-05-03 08:58:55 +05:30
|
|
|
VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
|
|
|
|
timeout, intf, nprobes, nclaims);
|
2005-04-30 09:19:37 +05:30
|
|
|
switch (poll(fds, 1, timeout)) {
|
|
|
|
|
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++;
|
|
|
|
VDBG("probe/%d %s@%s\n",
|
|
|
|
nprobes, intf, inet_ntoa(ip));
|
2006-09-03 17:59:53 +05:30
|
|
|
arp(fd, &saddr, ARPOP_REQUEST,
|
2006-09-03 17:56:06 +05:30
|
|
|
ð_addr, null_ip,
|
2006-09-03 17:50:36 +05:30
|
|
|
&null_addr, ip);
|
2005-04-30 09:19:37 +05:30
|
|
|
timeout = PROBE_MIN * 1000;
|
|
|
|
timeout += ms_rdelay(PROBE_MAX
|
|
|
|
- PROBE_MIN);
|
2006-09-03 17:50:36 +05:30
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Switch to announce state.
|
|
|
|
state = ANNOUNCE;
|
|
|
|
nclaims = 0;
|
|
|
|
VDBG("announce/%d %s@%s\n",
|
|
|
|
nclaims, intf, inet_ntoa(ip));
|
2006-09-03 17:59:53 +05:30
|
|
|
arp(fd, &saddr, ARPOP_REQUEST,
|
2006-09-03 17:56:06 +05:30
|
|
|
ð_addr, ip,
|
|
|
|
ð_addr, ip);
|
2006-09-03 17:50:36 +05:30
|
|
|
timeout = ANNOUNCE_INTERVAL * 1000;
|
|
|
|
}
|
|
|
|
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;
|
2005-05-03 08:58:55 +05:30
|
|
|
VDBG("announce/%d %s@%s\n",
|
|
|
|
nclaims, intf, inet_ntoa(ip));
|
2006-09-03 17:59:53 +05:30
|
|
|
arp(fd, &saddr, ARPOP_REQUEST,
|
2006-09-03 17:56:06 +05:30
|
|
|
ð_addr, ip,
|
|
|
|
ð_addr, ip);
|
2006-09-03 17:50:36 +05:30
|
|
|
timeout = ANNOUNCE_INTERVAL * 1000;
|
|
|
|
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++;
|
|
|
|
VDBG("announce/%d %s@%s\n",
|
|
|
|
nclaims, intf, inet_ntoa(ip));
|
2006-09-03 17:59:53 +05:30
|
|
|
arp(fd, &saddr, ARPOP_REQUEST,
|
2006-09-03 17:56:06 +05:30
|
|
|
ð_addr, ip,
|
|
|
|
ð_addr, ip);
|
2005-04-30 09:19:37 +05:30
|
|
|
timeout = ANNOUNCE_INTERVAL * 1000;
|
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
|
2005-04-30 09:19:37 +05:30
|
|
|
run(script, "config", intf, &ip);
|
|
|
|
ready = 1;
|
|
|
|
conflicts = 0;
|
2006-09-03 17:50:36 +05:30
|
|
|
timeout = -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;
|
|
|
|
timeout = -1;
|
|
|
|
conflicts = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Invalid, should never happen. Restart the whole protocol.
|
|
|
|
state = PROBE;
|
|
|
|
pick(&ip);
|
|
|
|
timeout = 0;
|
|
|
|
nprobes = 0;
|
|
|
|
nclaims = 0;
|
|
|
|
break;
|
|
|
|
} // switch (state)
|
|
|
|
break; // case 0 (timeout)
|
2005-05-03 08:58:55 +05:30
|
|
|
// packets arriving
|
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.
|
2005-04-30 09:19:37 +05:30
|
|
|
if (timeout > 0) {
|
|
|
|
struct timeval tv2;
|
2005-05-03 08:58:55 +05:30
|
|
|
|
2005-04-30 09:19:37 +05:30
|
|
|
gettimeofday(&tv2, NULL);
|
|
|
|
if (timercmp(&tv1, &tv2, <)) {
|
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");
|
2006-03-30 04:31:33 +05:30
|
|
|
timeout = 0;
|
2005-04-30 09:19:37 +05:30
|
|
|
} else {
|
2006-09-03 17:50:36 +05:30
|
|
|
VDBG("adjusting timeout\n");
|
2005-04-30 09:19:37 +05:30
|
|
|
timersub(&tv1, &tv2, &tv1);
|
|
|
|
timeout = 1000 * tv1.tv_sec
|
|
|
|
+ tv1.tv_usec / 1000;
|
|
|
|
}
|
|
|
|
}
|
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.
|
2006-09-03 18:01:59 +05:30
|
|
|
bb_error_msg("%s: poll error", intf);
|
2005-04-30 09:19:37 +05:30
|
|
|
if (ready) {
|
2005-05-03 08:58:55 +05:30
|
|
|
run(script, "deconfig",
|
|
|
|
intf, &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-02-15 02:20:22 +05:30
|
|
|
if (recv(fd, &p, sizeof(p), 0) < 0) {
|
2005-04-30 09:19:37 +05:30
|
|
|
why = "recv";
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
|
|
|
|
continue;
|
2005-05-03 08:58:55 +05:30
|
|
|
|
2006-09-03 17:50:36 +05:30
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
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("%s recv arp type=%d, op=%d,\n",
|
2005-05-03 08:58:55 +05:30
|
|
|
intf, ntohs(p.hdr.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)
|
|
|
|
&& p.arp.arp_op != htons(ARPOP_REPLY))
|
2005-04-30 09:19:37 +05:30
|
|
|
continue;
|
|
|
|
|
2006-09-03 17:50:36 +05:30
|
|
|
if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
|
2006-09-03 17:56:06 +05:30
|
|
|
memcmp(ð_addr, &p.arp.arp_sha, ETH_ALEN) != 0) {
|
2006-09-03 17:50:36 +05:30
|
|
|
source_ip_conflict = 1;
|
|
|
|
}
|
|
|
|
if (memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0 &&
|
|
|
|
p.arp.arp_op == htons(ARPOP_REQUEST) &&
|
2006-09-03 17:56:06 +05:30
|
|
|
memcmp(ð_addr, &p.arp.arp_tha, ETH_ALEN) != 0) {
|
2006-09-03 17:50:36 +05:30
|
|
|
target_ip_conflict = 1;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
VDBG("%s ratelimit\n", intf);
|
|
|
|
timeout = RATE_LIMIT_INTERVAL * 1000;
|
|
|
|
state = RATE_LIMIT_PROBE;
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
2006-09-03 17:50:36 +05:30
|
|
|
|
|
|
|
// restart the whole protocol
|
|
|
|
pick(&ip);
|
|
|
|
timeout = 0;
|
|
|
|
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;
|
|
|
|
timeout = DEFEND_INTERVAL * 1000;
|
2006-09-03 17:59:53 +05:30
|
|
|
arp(fd, &saddr,
|
2006-09-03 17:50:36 +05:30
|
|
|
ARPOP_REQUEST,
|
2006-09-03 17:56:06 +05:30
|
|
|
ð_addr, ip,
|
|
|
|
ð_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;
|
|
|
|
run(script, "deconfig", intf, &ip);
|
2006-09-03 17:50:36 +05:30
|
|
|
|
|
|
|
// restart the whole protocol
|
|
|
|
pick(&ip);
|
|
|
|
timeout = 0;
|
|
|
|
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;
|
2005-04-30 09:19:37 +05:30
|
|
|
pick(&ip);
|
|
|
|
timeout = 0;
|
|
|
|
nprobes = 0;
|
|
|
|
nclaims = 0;
|
2006-09-03 17:50:36 +05:30
|
|
|
break;
|
|
|
|
} // switch state
|
2005-04-30 09:19:37 +05:30
|
|
|
|
2006-09-03 17:50:36 +05:30
|
|
|
break; // case 1 (packets arriving)
|
2005-04-30 09:19:37 +05:30
|
|
|
default:
|
|
|
|
why = "poll";
|
|
|
|
goto bad;
|
2006-09-03 17:50:36 +05:30
|
|
|
} // switch poll
|
2005-04-30 09:19:37 +05:30
|
|
|
}
|
|
|
|
bad:
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_perror_msg("%s, %s", intf, why);
|
2005-04-30 09:19:37 +05:30
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|