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