mirror of
https://github.com/sheumann/hush.git
synced 2024-06-01 00:41:33 +00:00
zcip: another code shrink
function old new delta send_arp_request - 185 +185 zcip_main 1273 1272 -1 pick_nip 40 - -40 arp 185 - -185 ------------------------------------------------------------------------------ (add/remove: 1/2 grow/shrink: 0/1 up/down: 185/-226) Total: -41 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
99e30be38b
commit
e347583835
|
@ -90,7 +90,7 @@ enum {
|
||||||
|
|
||||||
struct globals {
|
struct globals {
|
||||||
struct sockaddr iface_sockaddr;
|
struct sockaddr iface_sockaddr;
|
||||||
struct ether_addr eth_addr;
|
struct ether_addr our_ethaddr;
|
||||||
uint32_t localnet_ip;
|
uint32_t localnet_ip;
|
||||||
} FIX_ALIASING;
|
} FIX_ALIASING;
|
||||||
#define G (*(struct globals*)&bb_common_bufsiz1)
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
||||||
|
@ -121,14 +121,14 @@ static const char *nip_to_a(uint32_t nip)
|
||||||
/**
|
/**
|
||||||
* Broadcast an ARP packet.
|
* Broadcast an ARP packet.
|
||||||
*/
|
*/
|
||||||
static void arp(
|
static void send_arp_request(
|
||||||
/* int op, - always ARPOP_REQUEST */
|
/* int op, - always ARPOP_REQUEST */
|
||||||
/* const struct ether_addr *source_eth, - always &G.eth_addr */
|
/* const struct ether_addr *source_eth, - always &G.our_ethaddr */
|
||||||
uint32_t source_nip,
|
uint32_t source_nip,
|
||||||
const struct ether_addr *target_eth, uint32_t target_nip)
|
const struct ether_addr *target_eth, uint32_t target_nip)
|
||||||
{
|
{
|
||||||
enum { op = ARPOP_REQUEST };
|
enum { op = ARPOP_REQUEST };
|
||||||
#define source_eth (&G.eth_addr)
|
#define source_eth (&G.our_ethaddr)
|
||||||
|
|
||||||
struct arp_packet p;
|
struct arp_packet p;
|
||||||
memset(&p, 0, sizeof(p));
|
memset(&p, 0, sizeof(p));
|
||||||
|
@ -205,35 +205,34 @@ static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
|
||||||
int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||||
int zcip_main(int argc UNUSED_PARAM, char **argv)
|
int zcip_main(int argc UNUSED_PARAM, char **argv)
|
||||||
{
|
{
|
||||||
int state;
|
|
||||||
char *r_opt;
|
char *r_opt;
|
||||||
const char *l_opt = "169.254.0.0";
|
const char *l_opt = "169.254.0.0";
|
||||||
|
int state;
|
||||||
|
int nsent;
|
||||||
unsigned opts;
|
unsigned opts;
|
||||||
|
|
||||||
// ugly trick, but I want these zeroed in one go
|
// Ugly trick, but I want these zeroed in one go
|
||||||
struct {
|
struct {
|
||||||
const struct ether_addr null_addr;
|
const struct ether_addr null_ethaddr;
|
||||||
struct ifreq ifr;
|
struct ifreq ifr;
|
||||||
uint32_t chosen_nip;
|
uint32_t chosen_nip;
|
||||||
|
int conflicts;
|
||||||
int timeout_ms; // must be signed
|
int timeout_ms; // must be signed
|
||||||
unsigned conflicts;
|
|
||||||
unsigned nsent;
|
|
||||||
int verbose;
|
int verbose;
|
||||||
} L;
|
} L;
|
||||||
#define null_addr (L.null_addr )
|
#define null_ethaddr (L.null_ethaddr)
|
||||||
#define ifr (L.ifr )
|
#define ifr (L.ifr )
|
||||||
#define chosen_nip (L.chosen_nip)
|
#define chosen_nip (L.chosen_nip )
|
||||||
#define timeout_ms (L.timeout_ms)
|
#define conflicts (L.conflicts )
|
||||||
#define conflicts (L.conflicts )
|
#define timeout_ms (L.timeout_ms )
|
||||||
#define nsent (L.nsent )
|
#define verbose (L.verbose )
|
||||||
#define verbose (L.verbose )
|
|
||||||
|
|
||||||
memset(&L, 0, sizeof(L));
|
memset(&L, 0, sizeof(L));
|
||||||
INIT_G();
|
INIT_G();
|
||||||
|
|
||||||
#define FOREGROUND (opts & 1)
|
#define FOREGROUND (opts & 1)
|
||||||
#define QUIT (opts & 2)
|
#define QUIT (opts & 2)
|
||||||
// parse commandline: prog [options] ifname script
|
// Parse commandline: prog [options] ifname script
|
||||||
// exactly 2 args; -v accumulates and implies -f
|
// exactly 2 args; -v accumulates and implies -f
|
||||||
opt_complementary = "=2:vv:vf";
|
opt_complementary = "=2:vv:vf";
|
||||||
opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose);
|
opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose);
|
||||||
|
@ -242,7 +241,7 @@ int zcip_main(int argc UNUSED_PARAM, char **argv)
|
||||||
if (!FOREGROUND)
|
if (!FOREGROUND)
|
||||||
bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv);
|
bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv);
|
||||||
#endif
|
#endif
|
||||||
// open an ARP socket
|
// Open an ARP socket
|
||||||
// (need to do it before openlog to prevent openlog from taking
|
// (need to do it before openlog to prevent openlog from taking
|
||||||
// fd 3 (sock_fd==3))
|
// fd 3 (sock_fd==3))
|
||||||
xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
|
xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
|
||||||
|
@ -282,26 +281,26 @@ int zcip_main(int argc UNUSED_PARAM, char **argv)
|
||||||
|
|
||||||
xsetenv("interface", argv_intf);
|
xsetenv("interface", argv_intf);
|
||||||
|
|
||||||
// initialize the interface (modprobe, ifup, etc)
|
// Initialize the interface (modprobe, ifup, etc)
|
||||||
if (run(argv, "init", 0))
|
if (run(argv, "init", 0))
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
// initialize G.iface_sockaddr
|
// Initialize G.iface_sockaddr
|
||||||
// G.iface_sockaddr is: { u16 sa_family; u8 sa_data[14]; }
|
// G.iface_sockaddr is: { u16 sa_family; u8 sa_data[14]; }
|
||||||
//memset(&G.iface_sockaddr, 0, sizeof(G.iface_sockaddr));
|
//memset(&G.iface_sockaddr, 0, sizeof(G.iface_sockaddr));
|
||||||
//TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
|
//TODO: are we leaving sa_family == 0 (AF_UNSPEC)?!
|
||||||
safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data));
|
safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data));
|
||||||
|
|
||||||
// bind to the interface's ARP socket
|
// Bind to the interface's ARP socket
|
||||||
xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr));
|
xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr));
|
||||||
|
|
||||||
// get the interface's ethernet address
|
// Get the interface's ethernet address
|
||||||
//memset(&ifr, 0, sizeof(ifr));
|
//memset(&ifr, 0, sizeof(ifr));
|
||||||
strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
|
strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
|
||||||
xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
|
xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
|
||||||
memcpy(&G.eth_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
|
memcpy(&G.our_ethaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
|
||||||
|
|
||||||
// start with some stable ip address, either a function of
|
// Start with some stable ip address, either a function of
|
||||||
// the hardware address or else the last address we used.
|
// the hardware address or else the last address we used.
|
||||||
// we are taking low-order four bytes, as top-order ones
|
// we are taking low-order four bytes, as top-order ones
|
||||||
// aren't random enough.
|
// aren't random enough.
|
||||||
|
@ -309,17 +308,14 @@ int zcip_main(int argc UNUSED_PARAM, char **argv)
|
||||||
// depending on when we detect conflicts.
|
// depending on when we detect conflicts.
|
||||||
{
|
{
|
||||||
uint32_t t;
|
uint32_t t;
|
||||||
move_from_unaligned32(t, ((char *)&G.eth_addr + 2));
|
move_from_unaligned32(t, ((char *)&G.our_ethaddr + 2));
|
||||||
srand(t);
|
srand(t);
|
||||||
}
|
}
|
||||||
if (chosen_nip == 0)
|
|
||||||
chosen_nip = pick_nip();
|
|
||||||
|
|
||||||
// FIXME cases to handle:
|
// FIXME cases to handle:
|
||||||
// - zcip already running!
|
// - zcip already running!
|
||||||
// - link already has local address... just defend/update
|
// - link already has local address... just defend/update
|
||||||
|
|
||||||
// daemonize now; don't delay system startup
|
// Daemonize now; don't delay system startup
|
||||||
if (!FOREGROUND) {
|
if (!FOREGROUND) {
|
||||||
#if BB_MMU
|
#if BB_MMU
|
||||||
bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/);
|
bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/);
|
||||||
|
@ -327,14 +323,14 @@ int zcip_main(int argc UNUSED_PARAM, char **argv)
|
||||||
bb_info_msg("start, interface %s", argv_intf);
|
bb_info_msg("start, interface %s", argv_intf);
|
||||||
}
|
}
|
||||||
|
|
||||||
// run the dynamic address negotiation protocol,
|
// Run the dynamic address negotiation protocol,
|
||||||
// restarting after address conflicts:
|
// restarting after address conflicts:
|
||||||
// - start with some address we want to try
|
// - start with some address we want to try
|
||||||
// - short random delay
|
// - short random delay
|
||||||
// - arp probes to see if another host uses it
|
// - arp probes to see if another host uses it
|
||||||
// 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff, ARP (0x0806): arp who-has 169.254.194.171 tell 0.0.0.0
|
// 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff: arp who-has 169.254.194.171 tell 0.0.0.0
|
||||||
// - arp announcements that we're claiming it
|
// - arp announcements that we're claiming it
|
||||||
// 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff, ARP (0x0806): arp who-has 169.254.194.171 (00:04:e2:64:23:c2) tell 169.254.194.171
|
// 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
|
||||||
// - use it
|
// - use it
|
||||||
// - defend it, within limits
|
// - defend it, within limits
|
||||||
// exit if:
|
// exit if:
|
||||||
|
@ -342,73 +338,73 @@ int zcip_main(int argc UNUSED_PARAM, char **argv)
|
||||||
// run "<script> config", then exit with exitcode 0
|
// run "<script> config", then exit with exitcode 0
|
||||||
// - poll error (when does this happen?)
|
// - poll error (when does this happen?)
|
||||||
// - read error (when does this happen?)
|
// - read error (when does this happen?)
|
||||||
// - sendto error (in arp()) (when does this happen?)
|
// - sendto error (in send_arp_request()) (when does this happen?)
|
||||||
// - revents & POLLERR (link down). run "<script> deconfig" first
|
// - revents & POLLERR (link down). run "<script> deconfig" first
|
||||||
|
if (chosen_nip == 0) {
|
||||||
|
new_nip_and_PROBE:
|
||||||
|
chosen_nip = pick_nip();
|
||||||
|
}
|
||||||
|
nsent = 0;
|
||||||
state = PROBE;
|
state = PROBE;
|
||||||
while (1) {
|
while (1) {
|
||||||
struct pollfd fds[1];
|
struct pollfd fds[1];
|
||||||
unsigned deadline_us;
|
unsigned deadline_us;
|
||||||
struct arp_packet p;
|
struct arp_packet p;
|
||||||
int ip_conflict;
|
int ip_conflict;
|
||||||
|
int n;
|
||||||
|
|
||||||
fds[0].fd = sock_fd;
|
fds[0].fd = sock_fd;
|
||||||
fds[0].events = POLLIN;
|
fds[0].events = POLLIN;
|
||||||
fds[0].revents = 0;
|
fds[0].revents = 0;
|
||||||
|
|
||||||
// poll, being ready to adjust current timeout
|
// Poll, being ready to adjust current timeout
|
||||||
if (!timeout_ms) {
|
if (!timeout_ms) {
|
||||||
timeout_ms = random_delay_ms(PROBE_WAIT);
|
timeout_ms = random_delay_ms(PROBE_WAIT);
|
||||||
// FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to
|
// FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to
|
||||||
// make the kernel filter out all packets except
|
// make the kernel filter out all packets except
|
||||||
// ones we'd care about.
|
// ones we'd care about.
|
||||||
}
|
}
|
||||||
// set deadline_us to the point in time when we timeout
|
// Set deadline_us to the point in time when we timeout
|
||||||
deadline_us = MONOTONIC_US() + timeout_ms * 1000;
|
deadline_us = MONOTONIC_US() + timeout_ms * 1000;
|
||||||
|
|
||||||
VDBG("...wait %d %s nsent=%u\n",
|
VDBG("...wait %d %s nsent=%u\n",
|
||||||
timeout_ms, argv_intf, nsent);
|
timeout_ms, argv_intf, nsent);
|
||||||
|
|
||||||
switch (safe_poll(fds, 1, timeout_ms)) {
|
n = safe_poll(fds, 1, timeout_ms);
|
||||||
|
if (n < 0) {
|
||||||
default:
|
|
||||||
//bb_perror_msg("poll"); - done in safe_poll
|
//bb_perror_msg("poll"); - done in safe_poll
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
// timeout
|
if (n == 0) { // timed out?
|
||||||
case 0:
|
VDBG("state:%d\n", state);
|
||||||
VDBG("state = %d\n", state);
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case PROBE:
|
case PROBE:
|
||||||
// timeouts in the PROBE state mean no conflicting ARP packets
|
// No conflicting ARP packets were seen:
|
||||||
// have been received, so we can progress through the states
|
// we can progress through the states
|
||||||
if (nsent < PROBE_NUM) {
|
if (nsent < PROBE_NUM) {
|
||||||
nsent++;
|
nsent++;
|
||||||
VDBG("probe/%u %s@%s\n",
|
VDBG("probe/%u %s@%s\n",
|
||||||
nsent, argv_intf, nip_to_a(chosen_nip));
|
nsent, argv_intf, nip_to_a(chosen_nip));
|
||||||
timeout_ms = PROBE_MIN * 1000;
|
timeout_ms = PROBE_MIN * 1000;
|
||||||
timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
|
timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
|
||||||
arp(/* ARPOP_REQUEST, */
|
send_arp_request(0, &null_ethaddr, chosen_nip);
|
||||||
/* &G.eth_addr, */ 0,
|
continue;
|
||||||
&null_addr, chosen_nip);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
// Switch to announce state
|
// Switch to announce state
|
||||||
nsent = 0;
|
nsent = 0;
|
||||||
state = ANNOUNCE;
|
state = ANNOUNCE;
|
||||||
goto send_announce;
|
goto send_announce;
|
||||||
case ANNOUNCE:
|
case ANNOUNCE:
|
||||||
// timeouts in the ANNOUNCE state mean no conflicting ARP packets
|
// No conflicting ARP packets were seen:
|
||||||
// have been received, so we can progress through the states
|
// we can progress through the states
|
||||||
if (nsent < ANNOUNCE_NUM) {
|
if (nsent < ANNOUNCE_NUM) {
|
||||||
send_announce:
|
send_announce:
|
||||||
nsent++;
|
nsent++;
|
||||||
VDBG("announce/%u %s@%s\n",
|
VDBG("announce/%u %s@%s\n",
|
||||||
nsent, argv_intf, nip_to_a(chosen_nip));
|
nsent, argv_intf, nip_to_a(chosen_nip));
|
||||||
timeout_ms = ANNOUNCE_INTERVAL * 1000;
|
timeout_ms = ANNOUNCE_INTERVAL * 1000;
|
||||||
arp(/* ARPOP_REQUEST, */
|
send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
|
||||||
/* &G.eth_addr, */ chosen_nip,
|
continue;
|
||||||
&G.eth_addr, chosen_nip);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
// Switch to monitor state
|
// Switch to monitor state
|
||||||
// FIXME update filters
|
// FIXME update filters
|
||||||
|
@ -416,124 +412,117 @@ int zcip_main(int argc UNUSED_PARAM, char **argv)
|
||||||
// NOTE: all other exit paths should deconfig...
|
// NOTE: all other exit paths should deconfig...
|
||||||
if (QUIT)
|
if (QUIT)
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
conflicts = 0;
|
// fall through: switch_to_MONITOR
|
||||||
timeout_ms = -1; // Never timeout in the monitor state.
|
default:
|
||||||
state = MONITOR;
|
// case DEFEND:
|
||||||
break;
|
// case MONITOR: (shouldn't happen, MONITOR timeout is infinite)
|
||||||
case DEFEND:
|
|
||||||
// Defend period ended with no ARP replies - we won
|
// Defend period ended with no ARP replies - we won
|
||||||
conflicts = 0;
|
timeout_ms = -1; // never timeout in monitor state
|
||||||
timeout_ms = -1;
|
|
||||||
state = MONITOR;
|
state = MONITOR;
|
||||||
break;
|
continue;
|
||||||
} // switch (state)
|
}
|
||||||
break; // case 0 (timeout)
|
}
|
||||||
|
|
||||||
// packets arriving, or link went down
|
// Packet arrived, or link went down.
|
||||||
case 1:
|
// We need to adjust the timeout in case we didn't receive
|
||||||
// We need to adjust the timeout in case we didn't receive
|
// a conflicting packet.
|
||||||
// a conflicting packet.
|
if (timeout_ms > 0) {
|
||||||
if (timeout_ms > 0) {
|
unsigned diff = deadline_us - MONOTONIC_US();
|
||||||
unsigned diff = deadline_us - MONOTONIC_US();
|
if ((int)(diff) < 0) {
|
||||||
if ((int)(diff) < 0) {
|
// Current time is greater than the expected timeout time.
|
||||||
// Current time is greater than the expected timeout time.
|
diff = 0;
|
||||||
diff = 0;
|
}
|
||||||
|
VDBG("adjusting timeout\n");
|
||||||
|
timeout_ms = (diff / 1000) | 1; // never 0
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
VDBG("adjusting timeout\n");
|
return EXIT_FAILURE;
|
||||||
timeout_ms = (diff / 1000) | 1; // never 0
|
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ((fds[0].revents & POLLIN) == 0) {
|
// Read ARP packet
|
||||||
if (fds[0].revents & POLLERR) {
|
if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
|
||||||
// FIXME: links routinely go down;
|
bb_perror_msg_and_die(bb_msg_read_error);
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// read ARP packet
|
if (p.eth.ether_type != htons(ETHERTYPE_ARP))
|
||||||
if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
|
continue;
|
||||||
bb_perror_msg_and_die(bb_msg_read_error);
|
if (p.arp.arp_op != htons(ARPOP_REQUEST)
|
||||||
}
|
&& p.arp.arp_op != htons(ARPOP_REPLY)
|
||||||
|
) {
|
||||||
if (p.eth.ether_type != htons(ETHERTYPE_ARP))
|
continue;
|
||||||
continue;
|
}
|
||||||
if (p.arp.arp_op != htons(ARPOP_REQUEST)
|
|
||||||
&& p.arp.arp_op != htons(ARPOP_REPLY)
|
|
||||||
) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
{
|
{
|
||||||
struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
|
struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
|
||||||
struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha;
|
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 *spa = (struct in_addr *) p.arp.arp_spa;
|
||||||
struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
|
struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
|
||||||
VDBG("source=%s %s\n", ether_ntoa(sha), inet_ntoa(*spa));
|
VDBG("source=%s %s\n", ether_ntoa(sha), inet_ntoa(*spa));
|
||||||
VDBG("target=%s %s\n", ether_ntoa(tha), inet_ntoa(*tpa));
|
VDBG("target=%s %s\n", ether_ntoa(tha), inet_ntoa(*tpa));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
ip_conflict = 0;
|
ip_conflict = 0;
|
||||||
if (memcmp(&p.arp.arp_sha, &G.eth_addr, ETH_ALEN) != 0) {
|
if (memcmp(&p.arp.arp_sha, &G.our_ethaddr, ETH_ALEN) != 0) {
|
||||||
if (memcmp(p.arp.arp_spa, &chosen_nip, 4) == 0) {
|
if (memcmp(p.arp.arp_spa, &chosen_nip, 4) == 0) {
|
||||||
// A probe or reply with source_ip == chosen ip
|
// A probe or reply with source_ip == chosen ip
|
||||||
ip_conflict = 1;
|
ip_conflict = 1;
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
VDBG("state:%d ip_conflict:%d\n", state, ip_conflict);
|
if (p.arp.arp_op == htons(ARPOP_REQUEST)
|
||||||
if (!ip_conflict)
|
&& memcmp(p.arp.arp_spa, &const_int_0, 4) == 0
|
||||||
break;
|
&& 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VDBG("state:%d ip_conflict:%d\n", state, ip_conflict);
|
||||||
|
if (!ip_conflict)
|
||||||
|
continue;
|
||||||
|
|
||||||
// Either src or target IP conflict exists
|
// Either src or target IP conflict exists
|
||||||
if (state <= ANNOUNCE) {
|
if (state <= ANNOUNCE) {
|
||||||
// PROBE or ANNOUNCE
|
// PROBE or ANNOUNCE
|
||||||
conflicts++;
|
conflicts++;
|
||||||
timeout_ms = PROBE_MIN * 1000
|
timeout_ms = PROBE_MIN * 1000
|
||||||
+ CONFLICT_MULTIPLIER * random_delay_ms(conflicts);
|
+ CONFLICT_MULTIPLIER * random_delay_ms(conflicts);
|
||||||
chosen_nip = pick_nip();
|
goto new_nip_and_PROBE;
|
||||||
nsent = 0;
|
}
|
||||||
state = PROBE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// 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;
|
|
||||||
arp(/* ARPOP_REQUEST, */
|
|
||||||
/* &G.eth_addr, */ chosen_nip,
|
|
||||||
&G.eth_addr, chosen_nip);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// state == DEFEND
|
|
||||||
// Another src IP conflict, start over
|
|
||||||
VDBG("defend conflict - starting over\n");
|
|
||||||
run(argv, "deconfig", chosen_nip);
|
|
||||||
|
|
||||||
// restart the whole protocol
|
// MONITOR or DEFEND: only src IP conflict is a problem
|
||||||
timeout_ms = 0;
|
if (ip_conflict & 1) {
|
||||||
chosen_nip = pick_nip();
|
if (state == MONITOR) {
|
||||||
nsent = 0;
|
// Src IP conflict, defend with a single ARP probe
|
||||||
state = PROBE;
|
VDBG("monitor conflict - defending\n");
|
||||||
|
timeout_ms = DEFEND_INTERVAL * 1000;
|
||||||
|
state = DEFEND;
|
||||||
|
send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
break; // case 1 (packet arrived)
|
// state == DEFEND
|
||||||
} // switch (poll)
|
// 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:
|
||||||
|
// aa:bb:cc:dd:ee:ff > xx:xx:xx:xx:xx:xx: arp who-has <chosen_nip> tell 0.0.0.0
|
||||||
|
// we expect _kernel_ to respond to that, because <chosen_nip>
|
||||||
|
// is (expected to be) configured on this iface.
|
||||||
} // while (1)
|
} // while (1)
|
||||||
#undef argv_intf
|
#undef argv_intf
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user