"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" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 43 | #include "common_bufsiz.h" |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 44 | #include <netinet/ether.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 45 | #include <net/if.h> |
| 46 | #include <net/if_arp.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 47 | #include <linux/sockios.h> |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 48 | |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 49 | #include <syslog.h> |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 50 | |
| 51 | /* We don't need more than 32 bits of the counter */ |
| 52 | #define MONOTONIC_US() ((unsigned)monotonic_us()) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 53 | |
Bernhard Reutner-Fischer | ec351c3 | 2005-12-13 10:28:25 +0000 | [diff] [blame] | 54 | struct arp_packet { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 55 | struct ether_header eth; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 56 | struct ether_arp arp; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 57 | } PACKED; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 58 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 59 | enum { |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 60 | /* 0-1 seconds before sending 1st probe */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 61 | PROBE_WAIT = 1, |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 62 | /* 1-2 seconds between probes */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 63 | PROBE_MIN = 1, |
| 64 | PROBE_MAX = 2, |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 65 | PROBE_NUM = 3, /* total probes to send */ |
| 66 | ANNOUNCE_INTERVAL = 2, /* 2 seconds between announces */ |
| 67 | ANNOUNCE_NUM = 3, /* announces to send */ |
| 68 | /* if probe/announce sees a conflict, multiply RANDOM(NUM_CONFLICT) by... */ |
| 69 | CONFLICT_MULTIPLIER = 2, |
| 70 | /* if we monitor and see a conflict, how long is defend state? */ |
Denys Vlasenko | a83e3ae | 2015-08-04 15:06:38 +0200 | [diff] [blame] | 71 | DEFEND_INTERVAL = 10, |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 72 | }; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 74 | /* States during the configuration process. */ |
| 75 | enum { |
| 76 | PROBE = 0, |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 77 | ANNOUNCE, |
| 78 | MONITOR, |
| 79 | DEFEND |
| 80 | }; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 82 | #define VDBG(...) do { } while (0) |
| 83 | |
| 84 | |
| 85 | enum { |
| 86 | sock_fd = 3 |
| 87 | }; |
| 88 | |
| 89 | struct globals { |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 90 | struct sockaddr iface_sockaddr; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 91 | struct ether_addr our_ethaddr; |
Michel Stam | fdd957b | 2014-10-30 13:43:09 +0100 | [diff] [blame] | 92 | uint32_t localnet_ip; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 93 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 94 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame^] | 95 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 96 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 99 | * Pick a random link local IP address on 169.254/16, except that |
| 100 | * the first and last 256 addresses are reserved. |
| 101 | */ |
Michel Stam | fdd957b | 2014-10-30 13:43:09 +0100 | [diff] [blame] | 102 | static uint32_t pick_nip(void) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 103 | { |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 104 | unsigned tmp; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 105 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 106 | do { |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 107 | tmp = rand() & IN_CLASSB_HOST; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 108 | } while (tmp > (IN_CLASSB_HOST - 0x0200)); |
Michel Stam | fdd957b | 2014-10-30 13:43:09 +0100 | [diff] [blame] | 109 | return htonl((G.localnet_ip + 0x0100) + tmp); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 112 | static const char *nip_to_a(uint32_t nip) |
| 113 | { |
| 114 | struct in_addr in; |
| 115 | in.s_addr = nip; |
| 116 | return inet_ntoa(in); |
| 117 | } |
| 118 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 119 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 120 | * Broadcast an ARP packet. |
| 121 | */ |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 122 | static void send_arp_request( |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 123 | /* int op, - always ARPOP_REQUEST */ |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 124 | /* const struct ether_addr *source_eth, - always &G.our_ethaddr */ |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 125 | uint32_t source_nip, |
| 126 | const struct ether_addr *target_eth, uint32_t target_nip) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 127 | { |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 128 | enum { op = ARPOP_REQUEST }; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 129 | #define source_eth (&G.our_ethaddr) |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 130 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 131 | struct arp_packet p; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 132 | memset(&p, 0, sizeof(p)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 133 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 134 | // ether header |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 135 | p.eth.ether_type = htons(ETHERTYPE_ARP); |
| 136 | memcpy(p.eth.ether_shost, source_eth, ETH_ALEN); |
| 137 | memset(p.eth.ether_dhost, 0xff, ETH_ALEN); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 138 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 139 | // arp request |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 140 | p.arp.arp_hrd = htons(ARPHRD_ETHER); |
| 141 | p.arp.arp_pro = htons(ETHERTYPE_IP); |
| 142 | p.arp.arp_hln = ETH_ALEN; |
| 143 | p.arp.arp_pln = 4; |
| 144 | p.arp.arp_op = htons(op); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 145 | memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN); |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 146 | memcpy(&p.arp.arp_spa, &source_nip, 4); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 147 | memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN); |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 148 | memcpy(&p.arp.arp_tpa, &target_nip, 4); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 149 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 150 | // send it |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 151 | // Even though sock_fd is already bound to G.iface_sockaddr, just send() |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 152 | // won't work, because "socket is not connected" |
| 153 | // (and connect() won't fix that, "operation not supported"). |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 154 | // Thus we sendto() to G.iface_sockaddr. I wonder which sockaddr |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 155 | // (from bind() or from sendto()?) kernel actually uses |
| 156 | // to determine iface to emit the packet from... |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 157 | xsendto(sock_fd, &p, sizeof(p), &G.iface_sockaddr, sizeof(G.iface_sockaddr)); |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 158 | #undef source_eth |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 161 | /** |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 162 | * Run a script. |
| 163 | * 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] | 164 | */ |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 165 | static int run(char *argv[3], const char *param, uint32_t nip) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 166 | { |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 167 | int status; |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 168 | const char *addr = addr; /* for gcc */ |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 169 | const char *fmt = "%s %s %s" + 3; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 170 | |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 171 | argv[2] = (char*)param; |
| 172 | |
| 173 | VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 174 | |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 175 | if (nip != 0) { |
| 176 | addr = nip_to_a(nip); |
Denis Vlasenko | b8d1a4c | 2008-09-20 16:28:59 +0000 | [diff] [blame] | 177 | xsetenv("ip", addr); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 178 | fmt -= 3; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 179 | } |
Denys Vlasenko | cde1199 | 2016-03-30 16:22:13 +0200 | [diff] [blame] | 180 | bb_error_msg(fmt, argv[2], argv[0], addr); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 181 | |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 182 | status = spawn_and_wait(argv + 1); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 183 | if (status < 0) { |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 184 | bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 185 | return -errno; |
| 186 | } |
| 187 | if (status != 0) |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 188 | 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] | 189 | return status; |
| 190 | } |
| 191 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 192 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 193 | * Return milliseconds of random delay, up to "secs" seconds. |
| 194 | */ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 195 | static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 196 | { |
Denys Vlasenko | a83e3ae | 2015-08-04 15:06:38 +0200 | [diff] [blame] | 197 | return (unsigned)rand() % (secs * 1000); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 200 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 201 | * main program |
| 202 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 203 | int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 204 | int zcip_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 205 | { |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 206 | char *r_opt; |
Michel Stam | fdd957b | 2014-10-30 13:43:09 +0100 | [diff] [blame] | 207 | const char *l_opt = "169.254.0.0"; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 208 | int state; |
| 209 | int nsent; |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 210 | unsigned opts; |
| 211 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 212 | // Ugly trick, but I want these zeroed in one go |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 213 | struct { |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 214 | const struct ether_addr null_ethaddr; |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 215 | struct ifreq ifr; |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 216 | uint32_t chosen_nip; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 217 | int conflicts; |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 218 | int timeout_ms; // must be signed |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 219 | int verbose; |
| 220 | } L; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 221 | #define null_ethaddr (L.null_ethaddr) |
| 222 | #define ifr (L.ifr ) |
| 223 | #define chosen_nip (L.chosen_nip ) |
| 224 | #define conflicts (L.conflicts ) |
| 225 | #define timeout_ms (L.timeout_ms ) |
| 226 | #define verbose (L.verbose ) |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 227 | |
| 228 | memset(&L, 0, sizeof(L)); |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 229 | INIT_G(); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 230 | |
| 231 | #define FOREGROUND (opts & 1) |
| 232 | #define QUIT (opts & 2) |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 233 | // Parse commandline: prog [options] ifname script |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 234 | // exactly 2 args; -v accumulates and implies -f |
| 235 | opt_complementary = "=2:vv:vf"; |
Michel Stam | fdd957b | 2014-10-30 13:43:09 +0100 | [diff] [blame] | 236 | opts = getopt32(argv, "fqr:l:v", &r_opt, &l_opt, &verbose); |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 237 | #if !BB_MMU |
| 238 | // on NOMMU reexec early (or else we will rerun things twice) |
| 239 | if (!FOREGROUND) |
Denis Vlasenko | b29028e | 2008-02-28 18:02:23 +0000 | [diff] [blame] | 240 | bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv); |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 241 | #endif |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 242 | // Open an ARP socket |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 243 | // (need to do it before openlog to prevent openlog from taking |
| 244 | // fd 3 (sock_fd==3)) |
| 245 | 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] | 246 | if (!FOREGROUND) { |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 247 | // do it before all bb_xx_msg calls |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 248 | openlog(applet_name, 0, LOG_DAEMON); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 249 | logmode |= LOGMODE_SYSLOG; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 250 | } |
Michel Stam | d3fabf8 | 2014-11-04 12:19:04 +0100 | [diff] [blame] | 251 | bb_logenv_override(); |
| 252 | |
Michel Stam | fdd957b | 2014-10-30 13:43:09 +0100 | [diff] [blame] | 253 | { // -l n.n.n.n |
| 254 | struct in_addr net; |
| 255 | if (inet_aton(l_opt, &net) == 0 |
| 256 | || (net.s_addr & htonl(IN_CLASSB_NET)) != net.s_addr |
| 257 | ) { |
| 258 | bb_error_msg_and_die("invalid network address"); |
| 259 | } |
| 260 | G.localnet_ip = ntohl(net.s_addr); |
| 261 | } |
Denis Vlasenko | af906a3 | 2006-09-03 12:29:53 +0000 | [diff] [blame] | 262 | if (opts & 4) { // -r n.n.n.n |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 263 | struct in_addr ip; |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 264 | if (inet_aton(r_opt, &ip) == 0 |
Michel Stam | fdd957b | 2014-10-30 13:43:09 +0100 | [diff] [blame] | 265 | || (ntohl(ip.s_addr) & IN_CLASSB_NET) != G.localnet_ip |
Denis Vlasenko | 9af7c9d | 2007-01-19 21:19:35 +0000 | [diff] [blame] | 266 | ) { |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 267 | bb_error_msg_and_die("invalid link address"); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 268 | } |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 269 | chosen_nip = ip.s_addr; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 270 | } |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 271 | argv += optind - 1; |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 272 | |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 273 | /* Now: argv[0]:junk argv[1]:intf argv[2]:script argv[3]:NULL */ |
| 274 | /* We need to make space for script argument: */ |
| 275 | argv[0] = argv[1]; |
| 276 | argv[1] = argv[2]; |
| 277 | /* Now: argv[0]:intf argv[1]:script argv[2]:junk argv[3]:NULL */ |
| 278 | #define argv_intf (argv[0]) |
| 279 | |
| 280 | xsetenv("interface", argv_intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 281 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 282 | // Initialize the interface (modprobe, ifup, etc) |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 283 | if (run(argv, "init", 0)) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 284 | return EXIT_FAILURE; |
| 285 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 286 | // Initialize G.iface_sockaddr |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 287 | // G.iface_sockaddr is: { u16 sa_family; u8 sa_data[14]; } |
| 288 | //memset(&G.iface_sockaddr, 0, sizeof(G.iface_sockaddr)); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 289 | //TODO: are we leaving sa_family == 0 (AF_UNSPEC)?! |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 290 | safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 291 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 292 | // Bind to the interface's ARP socket |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 293 | xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr)); |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 294 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 295 | // Get the interface's ethernet address |
Denis Vlasenko | 9ab2658 | 2007-02-14 20:50:22 +0000 | [diff] [blame] | 296 | //memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 297 | strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 298 | xioctl(sock_fd, SIOCGIFHWADDR, &ifr); |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 299 | memcpy(&G.our_ethaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 300 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 301 | // Start with some stable ip address, either a function of |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 302 | // the hardware address or else the last address we used. |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 303 | // we are taking low-order four bytes, as top-order ones |
| 304 | // aren't random enough. |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame] | 305 | // NOTE: the sequence of addresses we try changes only |
| 306 | // depending on when we detect conflicts. |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 307 | { |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 308 | uint32_t t; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 309 | move_from_unaligned32(t, ((char *)&G.our_ethaddr + 2)); |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 310 | srand(t); |
| 311 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 312 | // FIXME cases to handle: |
| 313 | // - zcip already running! |
| 314 | // - link already has local address... just defend/update |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 315 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 316 | // Daemonize now; don't delay system startup |
Denis Vlasenko | 65dbd87 | 2006-09-03 12:27:25 +0000 | [diff] [blame] | 317 | if (!FOREGROUND) { |
Denis Vlasenko | 473dae0 | 2007-04-11 07:04:23 +0000 | [diff] [blame] | 318 | #if BB_MMU |
Denis Vlasenko | b29028e | 2008-02-28 18:02:23 +0000 | [diff] [blame] | 319 | bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/); |
Denis Vlasenko | afa7023 | 2007-03-26 17:25:33 +0000 | [diff] [blame] | 320 | #endif |
Denys Vlasenko | cde1199 | 2016-03-30 16:22:13 +0200 | [diff] [blame] | 321 | bb_error_msg("start, interface %s", argv_intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 324 | // Run the dynamic address negotiation protocol, |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 325 | // restarting after address conflicts: |
| 326 | // - start with some address we want to try |
| 327 | // - short random delay |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 328 | // - arp probes to see if another host uses it |
Denys Vlasenko | a83e3ae | 2015-08-04 15:06:38 +0200 | [diff] [blame] | 329 | // 00:04:e2:64:23:c2 > ff:ff:ff:ff:ff:ff arp who-has 169.254.194.171 tell 0.0.0.0 |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 330 | // - arp announcements that we're claiming it |
Denys Vlasenko | a83e3ae | 2015-08-04 15:06:38 +0200 | [diff] [blame] | 331 | // 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 |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 332 | // - use it |
| 333 | // - defend it, within limits |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 334 | // exit if: |
| 335 | // - address is successfully obtained and -q was given: |
| 336 | // run "<script> config", then exit with exitcode 0 |
| 337 | // - poll error (when does this happen?) |
| 338 | // - read error (when does this happen?) |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 339 | // - sendto error (in send_arp_request()) (when does this happen?) |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 340 | // - revents & POLLERR (link down). run "<script> deconfig" first |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 341 | if (chosen_nip == 0) { |
| 342 | new_nip_and_PROBE: |
| 343 | chosen_nip = pick_nip(); |
| 344 | } |
| 345 | nsent = 0; |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 346 | state = PROBE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 347 | while (1) { |
| 348 | struct pollfd fds[1]; |
Denys Vlasenko | 334e12a | 2015-10-24 19:15:44 +0200 | [diff] [blame] | 349 | unsigned deadline_us = deadline_us; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 350 | struct arp_packet p; |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 351 | int ip_conflict; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 352 | int n; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 353 | |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 354 | fds[0].fd = sock_fd; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 355 | fds[0].events = POLLIN; |
| 356 | fds[0].revents = 0; |
| 357 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 358 | // Poll, being ready to adjust current timeout |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 359 | if (!timeout_ms) { |
Denis Vlasenko | a09300a | 2007-11-25 12:40:56 +0000 | [diff] [blame] | 360 | timeout_ms = random_delay_ms(PROBE_WAIT); |
| 361 | // FIXME setsockopt(sock_fd, SO_ATTACH_FILTER, ...) to |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 362 | // make the kernel filter out all packets except |
| 363 | // ones we'd care about. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 364 | } |
Denys Vlasenko | 49d5159 | 2015-10-24 19:14:04 +0200 | [diff] [blame] | 365 | if (timeout_ms >= 0) { |
| 366 | // Set deadline_us to the point in time when we timeout |
| 367 | deadline_us = MONOTONIC_US() + timeout_ms * 1000; |
| 368 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 369 | |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 370 | VDBG("...wait %d %s nsent=%u\n", |
| 371 | timeout_ms, argv_intf, nsent); |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 372 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 373 | n = safe_poll(fds, 1, timeout_ms); |
| 374 | if (n < 0) { |
Denis Vlasenko | f3f33eb | 2008-02-04 09:42:05 +0000 | [diff] [blame] | 375 | //bb_perror_msg("poll"); - done in safe_poll |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 376 | return EXIT_FAILURE; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 377 | } |
| 378 | if (n == 0) { // timed out? |
| 379 | VDBG("state:%d\n", state); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 380 | switch (state) { |
| 381 | case PROBE: |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 382 | // No conflicting ARP packets were seen: |
| 383 | // we can progress through the states |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 384 | if (nsent < PROBE_NUM) { |
| 385 | nsent++; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 386 | VDBG("probe/%u %s@%s\n", |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 387 | nsent, argv_intf, nip_to_a(chosen_nip)); |
Ken Sharp | a4d564a | 2014-07-20 14:01:49 +0200 | [diff] [blame] | 388 | timeout_ms = PROBE_MIN * 1000; |
| 389 | timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN); |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 390 | send_arp_request(0, &null_ethaddr, chosen_nip); |
| 391 | continue; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 392 | } |
Denys Vlasenko | 6390a3a | 2015-10-13 01:51:37 +0200 | [diff] [blame] | 393 | // Switch to announce state |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 394 | nsent = 0; |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 395 | state = ANNOUNCE; |
| 396 | goto send_announce; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 397 | case ANNOUNCE: |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 398 | // No conflicting ARP packets were seen: |
| 399 | // we can progress through the states |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 400 | if (nsent < ANNOUNCE_NUM) { |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 401 | send_announce: |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 402 | nsent++; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 403 | VDBG("announce/%u %s@%s\n", |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 404 | nsent, argv_intf, nip_to_a(chosen_nip)); |
Ken Sharp | a4d564a | 2014-07-20 14:01:49 +0200 | [diff] [blame] | 405 | timeout_ms = ANNOUNCE_INTERVAL * 1000; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 406 | send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip); |
| 407 | continue; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 408 | } |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 409 | // Switch to monitor state |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 410 | // FIXME update filters |
| 411 | run(argv, "config", chosen_nip); |
| 412 | // NOTE: all other exit paths should deconfig... |
| 413 | if (QUIT) |
| 414 | return EXIT_SUCCESS; |
Denys Vlasenko | a83e3ae | 2015-08-04 15:06:38 +0200 | [diff] [blame] | 415 | // fall through: switch to MONITOR |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 416 | default: |
| 417 | // case DEFEND: |
| 418 | // case MONITOR: (shouldn't happen, MONITOR timeout is infinite) |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 419 | // Defend period ended with no ARP replies - we won |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 420 | timeout_ms = -1; // never timeout in monitor state |
Denys Vlasenko | 16aa7a7 | 2015-08-04 03:27:56 +0200 | [diff] [blame] | 421 | state = MONITOR; |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 422 | continue; |
| 423 | } |
| 424 | } |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 425 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 426 | // Packet arrived, or link went down. |
| 427 | // We need to adjust the timeout in case we didn't receive |
| 428 | // a conflicting packet. |
| 429 | if (timeout_ms > 0) { |
| 430 | unsigned diff = deadline_us - MONOTONIC_US(); |
| 431 | if ((int)(diff) < 0) { |
| 432 | // Current time is greater than the expected timeout time. |
| 433 | diff = 0; |
| 434 | } |
| 435 | VDBG("adjusting timeout\n"); |
| 436 | timeout_ms = (diff / 1000) | 1; // never 0 |
| 437 | } |
| 438 | |
| 439 | if ((fds[0].revents & POLLIN) == 0) { |
| 440 | if (fds[0].revents & POLLERR) { |
| 441 | // FIXME: links routinely go down; |
| 442 | // this shouldn't necessarily exit. |
| 443 | bb_error_msg("iface %s is down", argv_intf); |
| 444 | if (state >= MONITOR) { |
| 445 | // Only if we are in MONITOR or DEFEND |
| 446 | run(argv, "deconfig", chosen_nip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 447 | } |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 448 | return EXIT_FAILURE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 449 | } |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 450 | continue; |
| 451 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 452 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 453 | // Read ARP packet |
| 454 | if (safe_read(sock_fd, &p, sizeof(p)) < 0) { |
| 455 | bb_perror_msg_and_die(bb_msg_read_error); |
| 456 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 457 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 458 | if (p.eth.ether_type != htons(ETHERTYPE_ARP)) |
| 459 | continue; |
| 460 | if (p.arp.arp_op != htons(ARPOP_REQUEST) |
| 461 | && p.arp.arp_op != htons(ARPOP_REPLY) |
| 462 | ) { |
| 463 | continue; |
| 464 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 465 | #ifdef DEBUG |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 466 | { |
| 467 | struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha; |
| 468 | struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha; |
| 469 | struct in_addr *spa = (struct in_addr *) p.arp.arp_spa; |
| 470 | struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa; |
| 471 | VDBG("source=%s %s\n", ether_ntoa(sha), inet_ntoa(*spa)); |
| 472 | VDBG("target=%s %s\n", ether_ntoa(tha), inet_ntoa(*tpa)); |
| 473 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 474 | #endif |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 475 | ip_conflict = 0; |
| 476 | if (memcmp(&p.arp.arp_sha, &G.our_ethaddr, ETH_ALEN) != 0) { |
| 477 | if (memcmp(p.arp.arp_spa, &chosen_nip, 4) == 0) { |
| 478 | // A probe or reply with source_ip == chosen ip |
| 479 | ip_conflict = 1; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 480 | } |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 481 | if (p.arp.arp_op == htons(ARPOP_REQUEST) |
| 482 | && memcmp(p.arp.arp_spa, &const_int_0, 4) == 0 |
| 483 | && memcmp(p.arp.arp_tpa, &chosen_nip, 4) == 0 |
| 484 | ) { |
| 485 | // A probe with source_ip == 0.0.0.0, target_ip == chosen ip: |
| 486 | // another host trying to claim this ip! |
| 487 | ip_conflict |= 2; |
| 488 | } |
| 489 | } |
| 490 | VDBG("state:%d ip_conflict:%d\n", state, ip_conflict); |
| 491 | if (!ip_conflict) |
| 492 | continue; |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 493 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 494 | // Either src or target IP conflict exists |
| 495 | if (state <= ANNOUNCE) { |
| 496 | // PROBE or ANNOUNCE |
| 497 | conflicts++; |
| 498 | timeout_ms = PROBE_MIN * 1000 |
| 499 | + CONFLICT_MULTIPLIER * random_delay_ms(conflicts); |
| 500 | goto new_nip_and_PROBE; |
| 501 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 502 | |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 503 | // MONITOR or DEFEND: only src IP conflict is a problem |
| 504 | if (ip_conflict & 1) { |
| 505 | if (state == MONITOR) { |
| 506 | // Src IP conflict, defend with a single ARP probe |
| 507 | VDBG("monitor conflict - defending\n"); |
| 508 | timeout_ms = DEFEND_INTERVAL * 1000; |
| 509 | state = DEFEND; |
| 510 | send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip); |
| 511 | continue; |
Denys Vlasenko | 99e30be | 2015-08-04 12:28:21 +0200 | [diff] [blame] | 512 | } |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 513 | // state == DEFEND |
| 514 | // Another src IP conflict, start over |
| 515 | VDBG("defend conflict - starting over\n"); |
| 516 | run(argv, "deconfig", chosen_nip); |
| 517 | conflicts = 0; |
| 518 | timeout_ms = 0; |
| 519 | goto new_nip_and_PROBE; |
| 520 | } |
| 521 | // Note: if we only have a target IP conflict here (ip_conflict & 2), |
| 522 | // IOW: if we just saw this sort of ARP packet: |
Denys Vlasenko | a83e3ae | 2015-08-04 15:06:38 +0200 | [diff] [blame] | 523 | // aa:bb:cc:dd:ee:ff > xx:xx:xx:xx:xx:xx arp who-has <chosen_nip> tell 0.0.0.0 |
Denys Vlasenko | e347583 | 2015-08-04 14:30:31 +0200 | [diff] [blame] | 524 | // we expect _kernel_ to respond to that, because <chosen_nip> |
| 525 | // is (expected to be) configured on this iface. |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 526 | } // while (1) |
Denis Vlasenko | cdd1f73 | 2008-10-10 11:28:39 +0000 | [diff] [blame] | 527 | #undef argv_intf |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 528 | } |