"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * RFC3927 ZeroConf IPv4 Link-Local addressing |
| 4 | * (see <http://www.zeroconf.org/>) |
| 5 | * |
| 6 | * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com) |
| 7 | * Copyright (C) 2004 by David Brownell |
| 8 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 9 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 13 | * ZCIP just manages the 169.254.*.* addresses. That network is not |
| 14 | * routed at the IP level, though various proxies or bridges can |
| 15 | * certainly be used. Its naming is built over multicast DNS. |
| 16 | */ |
| 17 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 18 | //#define DEBUG |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 19 | |
| 20 | // TODO: |
| 21 | // - more real-world usage/testing, especially daemon mode |
| 22 | // - kernel packet filters to reduce scheduling noise |
| 23 | // - avoid silent script failures, especially under load... |
| 24 | // - link status monitoring (restart on link-up; stop on link-down) |
| 25 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 26 | #include <netinet/ether.h> |
| 27 | #include <net/ethernet.h> |
| 28 | #include <net/if.h> |
| 29 | #include <net/if_arp.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 30 | #include <linux/if_packet.h> |
| 31 | #include <linux/sockios.h> |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 33 | #include "libbb.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 34 | #include <syslog.h> |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 35 | |
| 36 | /* We don't need more than 32 bits of the counter */ |
| 37 | #define MONOTONIC_US() ((unsigned)monotonic_us()) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 38 | |
Bernhard Reutner-Fischer | ec351c3 | 2005-12-13 10:28:25 +0000 | [diff] [blame] | 39 | struct arp_packet { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 40 | struct ether_header eth; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 41 | struct ether_arp arp; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 42 | } PACKED; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 43 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 44 | enum { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 45 | /* 169.254.0.0 */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 46 | LINKLOCAL_ADDR = 0xa9fe0000, |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 47 | |
| 48 | /* protocol timeout parameters, specified in seconds */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 49 | PROBE_WAIT = 1, |
| 50 | PROBE_MIN = 1, |
| 51 | PROBE_MAX = 2, |
| 52 | PROBE_NUM = 3, |
| 53 | MAX_CONFLICTS = 10, |
| 54 | RATE_LIMIT_INTERVAL = 60, |
| 55 | ANNOUNCE_WAIT = 2, |
| 56 | ANNOUNCE_NUM = 2, |
| 57 | ANNOUNCE_INTERVAL = 2, |
| 58 | DEFEND_INTERVAL = 10 |
| 59 | }; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 61 | /* States during the configuration process. */ |
| 62 | enum { |
| 63 | PROBE = 0, |
| 64 | RATE_LIMIT_PROBE, |
| 65 | ANNOUNCE, |
| 66 | MONITOR, |
| 67 | DEFEND |
| 68 | }; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 70 | #define VDBG(...) do { } while (0) |
| 71 | |
| 72 | |
| 73 | enum { |
| 74 | sock_fd = 3 |
| 75 | }; |
| 76 | |
| 77 | struct globals { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 78 | struct sockaddr saddr; |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 79 | struct ether_addr eth_addr; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 80 | } FIX_ALIASING; |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 81 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 82 | #define saddr (G.saddr ) |
| 83 | #define eth_addr (G.eth_addr) |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 84 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 87 | * Pick a random link local IP address on 169.254/16, except that |
| 88 | * the first and last 256 addresses are reserved. |
| 89 | */ |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 90 | static uint32_t pick(void) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 91 | { |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 92 | unsigned tmp; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 93 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 94 | do { |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 95 | tmp = rand() & IN_CLASSB_HOST; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 96 | } while (tmp > (IN_CLASSB_HOST - 0x0200)); |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 97 | return htonl((LINKLOCAL_ADDR + 0x0100) + tmp); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 100 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 101 | * Broadcast an ARP packet. |
| 102 | */ |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 103 | static void arp( |
| 104 | /* int op, - always ARPOP_REQUEST */ |
| 105 | /* const struct ether_addr *source_eth, - always ð_addr */ |
| 106 | struct in_addr source_ip, |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 107 | const struct ether_addr *target_eth, struct in_addr target_ip) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 108 | { |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 109 | enum { op = ARPOP_REQUEST }; |
| 110 | #define source_eth (ð_addr) |
| 111 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 112 | struct arp_packet p; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 113 | memset(&p, 0, sizeof(p)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 114 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 115 | // ether header |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 116 | p.eth.ether_type = htons(ETHERTYPE_ARP); |
| 117 | memcpy(p.eth.ether_shost, source_eth, ETH_ALEN); |
| 118 | memset(p.eth.ether_dhost, 0xff, ETH_ALEN); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 119 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 120 | // arp request |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 121 | p.arp.arp_hrd = htons(ARPHRD_ETHER); |
| 122 | p.arp.arp_pro = htons(ETHERTYPE_IP); |
| 123 | p.arp.arp_hln = ETH_ALEN; |
| 124 | p.arp.arp_pln = 4; |
| 125 | p.arp.arp_op = htons(op); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 126 | memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN); |
Denis Vlasenko | 9ab2658 | 2007-02-14 20:50:22 +0000 | [diff] [blame] | 127 | memcpy(&p.arp.arp_spa, &source_ip, sizeof(p.arp.arp_spa)); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 128 | memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN); |
Denis Vlasenko | 9ab2658 | 2007-02-14 20:50:22 +0000 | [diff] [blame] | 129 | memcpy(&p.arp.arp_tpa, &target_ip, sizeof(p.arp.arp_tpa)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 130 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 131 | // send it |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 132 | // Even though sock_fd is already bound to saddr, just send() |
| 133 | // won't work, because "socket is not connected" |
| 134 | // (and connect() won't fix that, "operation not supported"). |
| 135 | // Thus we sendto() to saddr. I wonder which sockaddr |
| 136 | // (from bind() or from sendto()?) kernel actually uses |
| 137 | // to determine iface to emit the packet from... |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 138 | xsendto(sock_fd, &p, sizeof(p), &saddr, sizeof(saddr)); |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 139 | #undef source_eth |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 142 | /** |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 143 | * Run a script. |
| 144 | * argv[0]:intf argv[1]:script_name argv[2]:junk argv[3]:NULL |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 145 | */ |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 146 | static int run(char *argv[3], const char *param, struct in_addr *ip) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 147 | { |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 148 | int status; |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 149 | char *addr = addr; /* for gcc */ |
| 150 | const char *fmt = "%s %s %s" + 3; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 151 | |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 152 | argv[2] = (char*)param; |
| 153 | |
| 154 | VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 155 | |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 156 | if (ip) { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 157 | addr = inet_ntoa(*ip); |
Denis Vlasenko | b8d1a4c | 2008-09-20 16:28:59 +0000 | [diff] [blame] | 158 | xsetenv("ip", addr); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 159 | fmt -= 3; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 160 | } |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 161 | bb_info_msg(fmt, argv[2], argv[0], addr); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 162 | |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame^] | 163 | status = spawn_and_wait(argv + 1); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 164 | if (status < 0) { |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 165 | bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 166 | return -errno; |
| 167 | } |
| 168 | if (status != 0) |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame^] | 169 | bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 170 | return status; |
| 171 | } |
| 172 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 173 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 174 | * Return milliseconds of random delay, up to "secs" seconds. |
| 175 | */ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 176 | static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 177 | { |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 178 | return rand() % (secs * 1000); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 181 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 182 | * main program |
| 183 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 184 | int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 185 | int zcip_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 186 | { |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 187 | int state; |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 188 | char *r_opt; |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 189 | unsigned opts; |
| 190 | |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 191 | // ugly trick, but I want these zeroed in one go |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 192 | struct { |
| 193 | const struct in_addr null_ip; |
| 194 | const struct ether_addr null_addr; |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 195 | struct in_addr ip; |
| 196 | struct ifreq ifr; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 197 | int timeout_ms; /* must be signed */ |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 198 | unsigned conflicts; |
| 199 | unsigned nprobes; |
| 200 | unsigned nclaims; |
| 201 | int ready; |
| 202 | int verbose; |
| 203 | } L; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 204 | #define null_ip (L.null_ip ) |
| 205 | #define null_addr (L.null_addr ) |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 206 | #define ip (L.ip ) |
| 207 | #define ifr (L.ifr ) |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 208 | #define timeout_ms (L.timeout_ms) |
| 209 | #define conflicts (L.conflicts ) |
| 210 | #define nprobes (L.nprobes ) |
| 211 | #define nclaims (L.nclaims ) |
| 212 | #define ready (L.ready ) |
| 213 | #define verbose (L.verbose ) |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 214 | |
| 215 | memset(&L, 0, sizeof(L)); |
| 216 | |
| 217 | #define FOREGROUND (opts & 1) |
| 218 | #define QUIT (opts & 2) |
| 219 | // parse commandline: prog [options] ifname script |
| 220 | // exactly 2 args; -v accumulates and implies -f |
| 221 | opt_complementary = "=2:vv:vf"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 222 | opts = getopt32(argv, "fqr:v", &r_opt, &verbose); |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 223 | #if !BB_MMU |
| 224 | // on NOMMU reexec early (or else we will rerun things twice) |
| 225 | if (!FOREGROUND) |
Denis Vlasenko | b29028e | 2008-02-28 18:02:23 +0000 | [diff] [blame] | 226 | bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv); |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 227 | #endif |
| 228 | // open an ARP socket |
| 229 | // (need to do it before openlog to prevent openlog from taking |
| 230 | // fd 3 (sock_fd==3)) |
| 231 | xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 232 | if (!FOREGROUND) { |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 233 | // do it before all bb_xx_msg calls |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 234 | openlog(applet_name, 0, LOG_DAEMON); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 235 | logmode |= LOGMODE_SYSLOG; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 236 | } |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 237 | if (opts & 4) { // -r n.n.n.n |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 238 | if (inet_aton(r_opt, &ip) == 0 |
Denis Vlasenko | 9af7c9d | 2007-01-19 21:19:35 +0000 | [diff] [blame] | 239 | || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR |
| 240 | ) { |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 241 | bb_error_msg_and_die("invalid link address"); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 242 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 243 | } |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 244 | argv += optind - 1; |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 245 | |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 246 | /* Now: argv[0]:junk argv[1]:intf argv[2]:script argv[3]:NULL */ |
| 247 | /* We need to make space for script argument: */ |
| 248 | argv[0] = argv[1]; |
| 249 | argv[1] = argv[2]; |
| 250 | /* Now: argv[0]:intf argv[1]:script argv[2]:junk argv[3]:NULL */ |
| 251 | #define argv_intf (argv[0]) |
| 252 | |
| 253 | xsetenv("interface", argv_intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 254 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 255 | // initialize the interface (modprobe, ifup, etc) |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 256 | if (run(argv, "init", NULL)) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 257 | return EXIT_FAILURE; |
| 258 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 259 | // initialize saddr |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 260 | // saddr is: { u16 sa_family; u8 sa_data[14]; } |
Denis Vlasenko | 9ab2658 | 2007-02-14 20:50:22 +0000 | [diff] [blame] | 261 | //memset(&saddr, 0, sizeof(saddr)); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 262 | //TODO: are we leaving sa_family == 0 (AF_UNSPEC)?! |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 263 | safe_strncpy(saddr.sa_data, argv_intf, sizeof(saddr.sa_data)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 264 | |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 265 | // bind to the interface's ARP socket |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 266 | xbind(sock_fd, &saddr, sizeof(saddr)); |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 267 | |
| 268 | // get the interface's ethernet address |
Denis Vlasenko | 9ab2658 | 2007-02-14 20:50:22 +0000 | [diff] [blame] | 269 | //memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 270 | strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 271 | xioctl(sock_fd, SIOCGIFHWADDR, &ifr); |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 272 | memcpy(ð_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 273 | |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 274 | // start with some stable ip address, either a function of |
| 275 | // the hardware address or else the last address we used. |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 276 | // we are taking low-order four bytes, as top-order ones |
| 277 | // aren't random enough. |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 278 | // NOTE: the sequence of addresses we try changes only |
| 279 | // depending on when we detect conflicts. |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 280 | { |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 281 | uint32_t t; |
| 282 | move_from_unaligned32(t, ((char *)ð_addr + 2)); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 283 | srand(t); |
| 284 | } |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 285 | if (ip.s_addr == 0) |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 286 | ip.s_addr = pick(); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 287 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 288 | // FIXME cases to handle: |
| 289 | // - zcip already running! |
| 290 | // - link already has local address... just defend/update |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 291 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 292 | // daemonize now; don't delay system startup |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 293 | if (!FOREGROUND) { |
Denis Vlasenko | 473dae0 | 2007-04-11 07:04:23 +0000 | [diff] [blame] | 294 | #if BB_MMU |
Denis Vlasenko | b29028e | 2008-02-28 18:02:23 +0000 | [diff] [blame] | 295 | bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 296 | #endif |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 297 | bb_info_msg("start, interface %s", argv_intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 300 | // run the dynamic address negotiation protocol, |
| 301 | // restarting after address conflicts: |
| 302 | // - start with some address we want to try |
| 303 | // - short random delay |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 304 | // - arp probes to see if another host uses it |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 305 | // - arp announcements that we're claiming it |
| 306 | // - use it |
| 307 | // - defend it, within limits |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 308 | // exit if: |
| 309 | // - address is successfully obtained and -q was given: |
| 310 | // run "<script> config", then exit with exitcode 0 |
| 311 | // - poll error (when does this happen?) |
| 312 | // - read error (when does this happen?) |
| 313 | // - sendto error (in arp()) (when does this happen?) |
| 314 | // - revents & POLLERR (link down). run "<script> deconfig" first |
| 315 | state = PROBE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 316 | while (1) { |
| 317 | struct pollfd fds[1]; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 318 | unsigned deadline_us; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 319 | struct arp_packet p; |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 320 | int source_ip_conflict; |
| 321 | int target_ip_conflict; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 322 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 323 | fds[0].fd = sock_fd; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 324 | fds[0].events = POLLIN; |
| 325 | fds[0].revents = 0; |
| 326 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 327 | // poll, being ready to adjust current timeout |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 328 | if (!timeout_ms) { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 329 | timeout_ms = random_delay_ms(PROBE_WAIT); |
| 330 | // FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 331 | // make the kernel filter out all packets except |
| 332 | // ones we'd care about. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 333 | } |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 334 | // set deadline_us to the point in time when we timeout |
| 335 | deadline_us = MONOTONIC_US() + timeout_ms * 1000; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 336 | |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 337 | VDBG("...wait %d %s nprobes=%u, nclaims=%u\n", |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 338 | timeout_ms, argv_intf, nprobes, nclaims); |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 339 | |
| 340 | switch (safe_poll(fds, 1, timeout_ms)) { |
| 341 | |
| 342 | default: |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 343 | //bb_perror_msg("poll"); - done in safe_poll |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 344 | return EXIT_FAILURE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 345 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 346 | // timeout |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 347 | case 0: |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 348 | VDBG("state = %d\n", state); |
| 349 | switch (state) { |
| 350 | case PROBE: |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 351 | // timeouts in the PROBE state mean no conflicting ARP packets |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 352 | // have been received, so we can progress through the states |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 353 | if (nprobes < PROBE_NUM) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 354 | nprobes++; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 355 | VDBG("probe/%u %s@%s\n", |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 356 | nprobes, argv_intf, inet_ntoa(ip)); |
| 357 | arp(/* ARPOP_REQUEST, */ |
| 358 | /* ð_addr, */ null_ip, |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 359 | &null_addr, ip); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 360 | timeout_ms = PROBE_MIN * 1000; |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 361 | timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 362 | } |
| 363 | else { |
| 364 | // Switch to announce state. |
| 365 | state = ANNOUNCE; |
| 366 | nclaims = 0; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 367 | VDBG("announce/%u %s@%s\n", |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 368 | nclaims, argv_intf, inet_ntoa(ip)); |
| 369 | arp(/* ARPOP_REQUEST, */ |
| 370 | /* ð_addr, */ ip, |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 371 | ð_addr, ip); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 372 | timeout_ms = ANNOUNCE_INTERVAL * 1000; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 373 | } |
| 374 | break; |
| 375 | case RATE_LIMIT_PROBE: |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 376 | // timeouts in the RATE_LIMIT_PROBE state mean no conflicting ARP packets |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 377 | // have been received, so we can move immediately to the announce state |
| 378 | state = ANNOUNCE; |
| 379 | nclaims = 0; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 380 | VDBG("announce/%u %s@%s\n", |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 381 | nclaims, argv_intf, inet_ntoa(ip)); |
| 382 | arp(/* ARPOP_REQUEST, */ |
| 383 | /* ð_addr, */ ip, |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 384 | ð_addr, ip); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 385 | timeout_ms = ANNOUNCE_INTERVAL * 1000; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 386 | break; |
| 387 | case ANNOUNCE: |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 388 | // timeouts in the ANNOUNCE state mean no conflicting ARP packets |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 389 | // have been received, so we can progress through the states |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 390 | if (nclaims < ANNOUNCE_NUM) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 391 | nclaims++; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 392 | VDBG("announce/%u %s@%s\n", |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 393 | nclaims, argv_intf, inet_ntoa(ip)); |
| 394 | arp(/* ARPOP_REQUEST, */ |
| 395 | /* ð_addr, */ ip, |
Denis Vlasenko | 60e3dd6 | 2006-09-03 12:26:06 +0000 | [diff] [blame] | 396 | ð_addr, ip); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 397 | timeout_ms = ANNOUNCE_INTERVAL * 1000; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 398 | } |
| 399 | else { |
| 400 | // Switch to monitor state. |
| 401 | state = MONITOR; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 402 | // link is ok to use earlier |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 403 | // FIXME update filters |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 404 | run(argv, "config", &ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 405 | ready = 1; |
| 406 | conflicts = 0; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 407 | timeout_ms = -1; // Never timeout in the monitor state. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 408 | |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 409 | // NOTE: all other exit paths |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 410 | // should deconfig ... |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 411 | if (QUIT) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 412 | return EXIT_SUCCESS; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 413 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 414 | break; |
| 415 | case DEFEND: |
| 416 | // We won! No ARP replies, so just go back to monitor. |
| 417 | state = MONITOR; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 418 | timeout_ms = -1; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 419 | conflicts = 0; |
| 420 | break; |
| 421 | default: |
| 422 | // Invalid, should never happen. Restart the whole protocol. |
| 423 | state = PROBE; |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 424 | ip.s_addr = pick(); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 425 | timeout_ms = 0; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 426 | nprobes = 0; |
| 427 | nclaims = 0; |
| 428 | break; |
| 429 | } // switch (state) |
| 430 | break; // case 0 (timeout) |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 431 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 432 | // packets arriving, or link went down |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 433 | case 1: |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 434 | // We need to adjust the timeout in case we didn't receive |
| 435 | // a conflicting packet. |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 436 | if (timeout_ms > 0) { |
| 437 | unsigned diff = deadline_us - MONOTONIC_US(); |
| 438 | if ((int)(diff) < 0) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 439 | // Current time is greater than the expected timeout time. |
| 440 | // Should never happen. |
| 441 | VDBG("missed an expected timeout\n"); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 442 | timeout_ms = 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 443 | } else { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 444 | VDBG("adjusting timeout\n"); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 445 | timeout_ms = (diff / 1000) | 1; /* never 0 */ |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 448 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 449 | if ((fds[0].revents & POLLIN) == 0) { |
| 450 | if (fds[0].revents & POLLERR) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 451 | // FIXME: links routinely go down; |
| 452 | // this shouldn't necessarily exit. |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 453 | bb_error_msg("iface %s is down", argv_intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 454 | if (ready) { |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 455 | run(argv, "deconfig", &ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 456 | } |
| 457 | return EXIT_FAILURE; |
| 458 | } |
| 459 | continue; |
| 460 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 461 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 462 | // read ARP packet |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 463 | if (safe_read(sock_fd, &p, sizeof(p)) < 0) { |
Denis Vlasenko | b1bac0d | 2007-11-25 12:53:25 +0000 | [diff] [blame] | 464 | bb_perror_msg_and_die(bb_msg_read_error); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 465 | } |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 466 | if (p.eth.ether_type != htons(ETHERTYPE_ARP)) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 467 | continue; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 468 | #ifdef DEBUG |
| 469 | { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 470 | struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha; |
| 471 | struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha; |
| 472 | struct in_addr *spa = (struct in_addr *) p.arp.arp_spa; |
| 473 | struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 474 | VDBG("%s recv arp type=%d, op=%d,\n", |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 475 | argv_intf, ntohs(p.eth.ether_type), |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 476 | ntohs(p.arp.arp_op)); |
| 477 | VDBG("\tsource=%s %s\n", |
| 478 | ether_ntoa(sha), |
| 479 | inet_ntoa(*spa)); |
| 480 | VDBG("\ttarget=%s %s\n", |
| 481 | ether_ntoa(tha), |
| 482 | inet_ntoa(*tpa)); |
| 483 | } |
| 484 | #endif |
| 485 | if (p.arp.arp_op != htons(ARPOP_REQUEST) |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 486 | && p.arp.arp_op != htons(ARPOP_REPLY)) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 487 | continue; |
| 488 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 489 | source_ip_conflict = 0; |
| 490 | target_ip_conflict = 0; |
| 491 | |
| 492 | if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0 |
| 493 | && memcmp(&p.arp.arp_sha, ð_addr, ETH_ALEN) != 0 |
| 494 | ) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 495 | source_ip_conflict = 1; |
| 496 | } |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 497 | if (p.arp.arp_op == htons(ARPOP_REQUEST) |
| 498 | && memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0 |
| 499 | && memcmp(&p.arp.arp_tha, ð_addr, ETH_ALEN) != 0 |
| 500 | ) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 501 | target_ip_conflict = 1; |
| 502 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 503 | |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 504 | VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n", |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 505 | state, source_ip_conflict, target_ip_conflict); |
| 506 | switch (state) { |
| 507 | case PROBE: |
| 508 | case ANNOUNCE: |
| 509 | // When probing or announcing, check for source IP conflicts |
| 510 | // and other hosts doing ARP probes (target IP conflicts). |
| 511 | if (source_ip_conflict || target_ip_conflict) { |
| 512 | conflicts++; |
| 513 | if (conflicts >= MAX_CONFLICTS) { |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 514 | VDBG("%s ratelimit\n", argv_intf); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 515 | timeout_ms = RATE_LIMIT_INTERVAL * 1000; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 516 | state = RATE_LIMIT_PROBE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 517 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 518 | |
| 519 | // restart the whole protocol |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 520 | ip.s_addr = pick(); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 521 | timeout_ms = 0; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 522 | nprobes = 0; |
| 523 | nclaims = 0; |
| 524 | } |
| 525 | break; |
| 526 | case MONITOR: |
| 527 | // If a conflict, we try to defend with a single ARP probe. |
| 528 | if (source_ip_conflict) { |
| 529 | VDBG("monitor conflict -- defending\n"); |
| 530 | state = DEFEND; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 531 | timeout_ms = DEFEND_INTERVAL * 1000; |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 532 | arp(/* ARPOP_REQUEST, */ |
| 533 | /* ð_addr, */ ip, |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 534 | ð_addr, ip); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 535 | } |
| 536 | break; |
| 537 | case DEFEND: |
| 538 | // Well, we tried. Start over (on conflict). |
| 539 | if (source_ip_conflict) { |
| 540 | state = PROBE; |
| 541 | VDBG("defend conflict -- starting over\n"); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 542 | ready = 0; |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 543 | run(argv, "deconfig", &ip); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 544 | |
| 545 | // restart the whole protocol |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 546 | ip.s_addr = pick(); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 547 | timeout_ms = 0; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 548 | nprobes = 0; |
| 549 | nclaims = 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 550 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 551 | break; |
| 552 | default: |
| 553 | // Invalid, should never happen. Restart the whole protocol. |
| 554 | VDBG("invalid state -- starting over\n"); |
| 555 | state = PROBE; |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 556 | ip.s_addr = pick(); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 557 | timeout_ms = 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 558 | nprobes = 0; |
| 559 | nclaims = 0; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 560 | break; |
| 561 | } // switch state |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 562 | break; // case 1 (packets arriving) |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 563 | } // switch poll |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 564 | } // while (1) |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 565 | #undef argv_intf |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 566 | } |