"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 | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 26 | #include "busybox.h" |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 27 | #include <errno.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <syslog.h> |
| 30 | #include <poll.h> |
| 31 | #include <time.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 32 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 33 | #include <sys/wait.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 34 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 35 | #include <netinet/ether.h> |
| 36 | #include <net/ethernet.h> |
| 37 | #include <net/if.h> |
| 38 | #include <net/if_arp.h> |
| 39 | |
| 40 | #include <linux/if_packet.h> |
| 41 | #include <linux/sockios.h> |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 42 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 43 | |
Bernhard Reutner-Fischer | ec351c3 | 2005-12-13 10:28:25 +0000 | [diff] [blame] | 44 | struct arp_packet { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 45 | struct ether_header hdr; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 46 | struct ether_arp arp; |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 47 | } ATTRIBUTE_PACKED; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 48 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 49 | enum { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 50 | /* 169.254.0.0 */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 51 | LINKLOCAL_ADDR = 0xa9fe0000, |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 52 | |
| 53 | /* protocol timeout parameters, specified in seconds */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 54 | PROBE_WAIT = 1, |
| 55 | PROBE_MIN = 1, |
| 56 | PROBE_MAX = 2, |
| 57 | PROBE_NUM = 3, |
| 58 | MAX_CONFLICTS = 10, |
| 59 | RATE_LIMIT_INTERVAL = 60, |
| 60 | ANNOUNCE_WAIT = 2, |
| 61 | ANNOUNCE_NUM = 2, |
| 62 | ANNOUNCE_INTERVAL = 2, |
| 63 | DEFEND_INTERVAL = 10 |
| 64 | }; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 66 | /* States during the configuration process. */ |
| 67 | enum { |
| 68 | PROBE = 0, |
| 69 | RATE_LIMIT_PROBE, |
| 70 | ANNOUNCE, |
| 71 | MONITOR, |
| 72 | DEFEND |
| 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 | /* Implicitly zero-initialized */ |
| 76 | static const struct in_addr null_ip; |
| 77 | static const struct ether_addr null_addr; |
| 78 | static int verbose; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 79 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 80 | #define DBG(fmt,args...) \ |
| 81 | do { } while (0) |
| 82 | #define VDBG DBG |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 83 | |
| 84 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 85 | * Pick a random link local IP address on 169.254/16, except that |
| 86 | * the first and last 256 addresses are reserved. |
| 87 | */ |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 88 | static void pick(struct in_addr *ip) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 89 | { |
| 90 | unsigned tmp; |
| 91 | |
| 92 | /* use cheaper math than lrand48() mod N */ |
| 93 | do { |
| 94 | tmp = (lrand48() >> 16) & IN_CLASSB_HOST; |
| 95 | } while (tmp > (IN_CLASSB_HOST - 0x0200)); |
| 96 | ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp); |
| 97 | } |
| 98 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 99 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 100 | * Broadcast an ARP packet. |
| 101 | */ |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 102 | static int arp(int fd, struct sockaddr *saddr, int op, |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 103 | const struct ether_addr *source_addr, struct in_addr source_ip, |
| 104 | const struct ether_addr *target_addr, struct in_addr target_ip) |
| 105 | { |
| 106 | struct arp_packet p; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 107 | memset(&p, 0, sizeof(p)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 108 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 109 | // ether header |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 110 | p.hdr.ether_type = htons(ETHERTYPE_ARP); |
| 111 | memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN); |
| 112 | memset(p.hdr.ether_dhost, 0xff, ETH_ALEN); |
| 113 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 114 | // arp request |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 115 | p.arp.arp_hrd = htons(ARPHRD_ETHER); |
| 116 | p.arp.arp_pro = htons(ETHERTYPE_IP); |
| 117 | p.arp.arp_hln = ETH_ALEN; |
| 118 | p.arp.arp_pln = 4; |
| 119 | p.arp.arp_op = htons(op); |
| 120 | memcpy(&p.arp.arp_sha, source_addr, ETH_ALEN); |
| 121 | memcpy(&p.arp.arp_spa, &source_ip, sizeof (p.arp.arp_spa)); |
| 122 | memcpy(&p.arp.arp_tha, target_addr, ETH_ALEN); |
| 123 | memcpy(&p.arp.arp_tpa, &target_ip, sizeof (p.arp.arp_tpa)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 124 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 125 | // send it |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 126 | if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 127 | perror("sendto"); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 128 | return -errno; |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 133 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 134 | * Run a script. |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame^] | 135 | * TODO: sort out stderr/syslog reporting. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 136 | */ |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 137 | static int run(char *script, char *arg, char *intf, struct in_addr *ip) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 138 | { |
| 139 | int pid, status; |
| 140 | char *why; |
| 141 | |
| 142 | if (script != NULL) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 143 | VDBG("%s run %s %s\n", intf, script, arg); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 144 | if (ip != NULL) { |
| 145 | char *addr = inet_ntoa(*ip); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 146 | setenv("ip", addr, 1); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 147 | syslog(LOG_INFO, "%s %s %s", arg, intf, addr); |
| 148 | } |
| 149 | |
| 150 | pid = vfork(); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 151 | if (pid < 0) { // error |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 152 | why = "vfork"; |
| 153 | goto bad; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 154 | } else if (pid == 0) { // child |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 155 | execl(script, script, arg, NULL); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 156 | perror("execl"); |
| 157 | _exit(EXIT_FAILURE); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 158 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 159 | |
| 160 | if (waitpid(pid, &status, 0) <= 0) { |
| 161 | why = "waitpid"; |
| 162 | goto bad; |
| 163 | } |
| 164 | if (WEXITSTATUS(status) != 0) { |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 165 | bb_error_msg("script %s failed, exit=%d\n", |
| 166 | script, WEXITSTATUS(status)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 167 | return -errno; |
| 168 | } |
| 169 | } |
| 170 | return 0; |
| 171 | bad: |
| 172 | status = -errno; |
| 173 | syslog(LOG_ERR, "%s %s, %s error: %s", |
| 174 | arg, intf, why, strerror(errno)); |
| 175 | return status; |
| 176 | } |
| 177 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 178 | |
| 179 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 180 | * Return milliseconds of random delay, up to "secs" seconds. |
| 181 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 182 | static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 183 | { |
| 184 | return lrand48() % (secs * 1000); |
| 185 | } |
| 186 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 187 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 188 | * main program |
| 189 | */ |
Bernhard Reutner-Fischer | 38d6615 | 2005-10-21 10:43:11 +0000 | [diff] [blame] | 190 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 191 | int zcip_main(int argc, char *argv[]) |
| 192 | { |
| 193 | char *intf = NULL; |
| 194 | char *script = NULL; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 195 | int quit = 0; |
| 196 | int foreground = 0; |
| 197 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 198 | char *why; |
| 199 | struct sockaddr saddr; |
| 200 | struct ether_addr addr; |
| 201 | struct in_addr ip = { 0 }; |
| 202 | int fd; |
| 203 | int ready = 0; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 204 | suseconds_t timeout = 0; // milliseconds |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 205 | unsigned conflicts = 0; |
| 206 | unsigned nprobes = 0; |
| 207 | unsigned nclaims = 0; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 208 | int t; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 209 | int state = PROBE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 210 | |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame^] | 211 | struct ifreq ifr; |
| 212 | unsigned short seed[3]; |
| 213 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 214 | // parse commandline: prog [options] ifname script |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 215 | while ((t = getopt(argc, argv, "fqr:v")) != EOF) { |
| 216 | switch (t) { |
| 217 | case 'f': |
| 218 | foreground = 1; |
| 219 | continue; |
| 220 | case 'q': |
| 221 | quit = 1; |
| 222 | continue; |
| 223 | case 'r': |
| 224 | if (inet_aton(optarg, &ip) == 0 |
| 225 | || (ntohl(ip.s_addr) & IN_CLASSB_NET) |
| 226 | != LINKLOCAL_ADDR) { |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 227 | bb_error_msg_and_die("invalid link address"); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 228 | } |
| 229 | continue; |
| 230 | case 'v': |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 231 | verbose++; |
| 232 | foreground = 1; |
| 233 | continue; |
| 234 | default: |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 235 | bb_error_msg_and_die("bad option"); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 236 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 237 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 238 | if (optind < argc - 1) { |
| 239 | intf = argv[optind++]; |
| 240 | setenv("interface", intf, 1); |
| 241 | script = argv[optind++]; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 242 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 243 | if (optind != argc || !intf) |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 244 | bb_show_usage(); |
| 245 | openlog(bb_applet_name, 0, LOG_DAEMON); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 246 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 247 | // initialize the interface (modprobe, ifup, etc) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 248 | if (run(script, "init", intf, NULL) < 0) |
| 249 | return EXIT_FAILURE; |
| 250 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 251 | // initialize saddr |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 252 | memset(&saddr, 0, sizeof (saddr)); |
Rob Landley | 768945b | 2006-06-25 00:34:52 +0000 | [diff] [blame] | 253 | safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data)); |
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 | // open an ARP socket |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame^] | 256 | fd = xsocket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); |
| 257 | // bind to the interface's ARP socket |
| 258 | xbind(fd, &saddr, sizeof (saddr); |
| 259 | |
| 260 | // get the interface's ethernet address |
| 261 | memset(&ifr, 0, sizeof (ifr)); |
| 262 | strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name)); |
| 263 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 264 | foreground = 1; |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame^] | 265 | why = "get ethernet address"; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 266 | goto bad; |
| 267 | } |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame^] | 268 | memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 269 | |
Denis Vlasenko | 035aae5 | 2006-09-03 12:23:56 +0000 | [diff] [blame^] | 270 | // start with some stable ip address, either a function of |
| 271 | // the hardware address or else the last address we used. |
| 272 | // NOTE: the sequence of addresses we try changes only |
| 273 | // depending on when we detect conflicts. |
| 274 | memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
| 275 | seed48(seed); |
| 276 | if (ip.s_addr == 0) |
| 277 | pick(&ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 278 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 279 | // FIXME cases to handle: |
| 280 | // - zcip already running! |
| 281 | // - link already has local address... just defend/update |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 282 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 283 | // daemonize now; don't delay system startup |
| 284 | if (!foreground) { |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 285 | xdaemon(0, verbose); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 286 | syslog(LOG_INFO, "start, interface %s", intf); |
| 287 | } |
| 288 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 289 | // run the dynamic address negotiation protocol, |
| 290 | // restarting after address conflicts: |
| 291 | // - start with some address we want to try |
| 292 | // - short random delay |
| 293 | // - arp probes to see if another host else uses it |
| 294 | // - arp announcements that we're claiming it |
| 295 | // - use it |
| 296 | // - defend it, within limits |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 297 | while (1) { |
| 298 | struct pollfd fds[1]; |
| 299 | struct timeval tv1; |
| 300 | struct arp_packet p; |
| 301 | |
| 302 | fds[0].fd = fd; |
| 303 | fds[0].events = POLLIN; |
| 304 | fds[0].revents = 0; |
| 305 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 306 | int source_ip_conflict = 0; |
| 307 | int target_ip_conflict = 0; |
| 308 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 309 | // poll, being ready to adjust current timeout |
Rob Landley | 00c051e | 2006-06-30 14:05:19 +0000 | [diff] [blame] | 310 | if (!timeout) { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 311 | timeout = ms_rdelay(PROBE_WAIT); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 312 | // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to |
| 313 | // make the kernel filter out all packets except |
| 314 | // ones we'd care about. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 315 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 316 | // set tv1 to the point in time when we timeout |
Rob Landley | 00c051e | 2006-06-30 14:05:19 +0000 | [diff] [blame] | 317 | gettimeofday(&tv1, NULL); |
| 318 | tv1.tv_usec += (timeout % 1000) * 1000; |
| 319 | while (tv1.tv_usec > 1000000) { |
| 320 | tv1.tv_usec -= 1000000; |
| 321 | tv1.tv_sec++; |
| 322 | } |
| 323 | tv1.tv_sec += timeout / 1000; |
| 324 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 325 | VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n", |
| 326 | timeout, intf, nprobes, nclaims); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 327 | switch (poll(fds, 1, timeout)) { |
| 328 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 329 | // timeout |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 330 | case 0: |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 331 | VDBG("state = %d\n", state); |
| 332 | switch (state) { |
| 333 | case PROBE: |
| 334 | // timeouts in the PROBE state means no conflicting ARP packets |
| 335 | // have been received, so we can progress through the states |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 336 | if (nprobes < PROBE_NUM) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 337 | nprobes++; |
| 338 | VDBG("probe/%d %s@%s\n", |
| 339 | nprobes, intf, inet_ntoa(ip)); |
| 340 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
| 341 | &addr, null_ip, |
| 342 | &null_addr, ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 343 | timeout = PROBE_MIN * 1000; |
| 344 | timeout += ms_rdelay(PROBE_MAX |
| 345 | - PROBE_MIN); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 346 | } |
| 347 | else { |
| 348 | // Switch to announce state. |
| 349 | state = ANNOUNCE; |
| 350 | nclaims = 0; |
| 351 | VDBG("announce/%d %s@%s\n", |
| 352 | nclaims, intf, inet_ntoa(ip)); |
| 353 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
| 354 | &addr, ip, |
| 355 | &addr, ip); |
| 356 | timeout = ANNOUNCE_INTERVAL * 1000; |
| 357 | } |
| 358 | break; |
| 359 | case RATE_LIMIT_PROBE: |
| 360 | // timeouts in the RATE_LIMIT_PROBE state means no conflicting ARP packets |
| 361 | // have been received, so we can move immediately to the announce state |
| 362 | state = ANNOUNCE; |
| 363 | nclaims = 0; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 364 | VDBG("announce/%d %s@%s\n", |
| 365 | nclaims, intf, inet_ntoa(ip)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 366 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
| 367 | &addr, ip, |
| 368 | &addr, ip); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 369 | timeout = ANNOUNCE_INTERVAL * 1000; |
| 370 | break; |
| 371 | case ANNOUNCE: |
| 372 | // timeouts in the ANNOUNCE state means no conflicting ARP packets |
| 373 | // have been received, so we can progress through the states |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 374 | if (nclaims < ANNOUNCE_NUM) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 375 | nclaims++; |
| 376 | VDBG("announce/%d %s@%s\n", |
| 377 | nclaims, intf, inet_ntoa(ip)); |
| 378 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
| 379 | &addr, ip, |
| 380 | &addr, ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 381 | timeout = ANNOUNCE_INTERVAL * 1000; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 382 | } |
| 383 | else { |
| 384 | // Switch to monitor state. |
| 385 | state = MONITOR; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 386 | // link is ok to use earlier |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 387 | // FIXME update filters |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 388 | run(script, "config", intf, &ip); |
| 389 | ready = 1; |
| 390 | conflicts = 0; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 391 | timeout = -1; // Never timeout in the monitor state. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 392 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 393 | // NOTE: all other exit paths |
| 394 | // should deconfig ... |
| 395 | if (quit) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 396 | return EXIT_SUCCESS; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 397 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 398 | break; |
| 399 | case DEFEND: |
| 400 | // We won! No ARP replies, so just go back to monitor. |
| 401 | state = MONITOR; |
| 402 | timeout = -1; |
| 403 | conflicts = 0; |
| 404 | break; |
| 405 | default: |
| 406 | // Invalid, should never happen. Restart the whole protocol. |
| 407 | state = PROBE; |
| 408 | pick(&ip); |
| 409 | timeout = 0; |
| 410 | nprobes = 0; |
| 411 | nclaims = 0; |
| 412 | break; |
| 413 | } // switch (state) |
| 414 | break; // case 0 (timeout) |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 415 | // packets arriving |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 416 | case 1: |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 417 | // We need to adjust the timeout in case we didn't receive |
| 418 | // a conflicting packet. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 419 | if (timeout > 0) { |
| 420 | struct timeval tv2; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 421 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 422 | gettimeofday(&tv2, NULL); |
| 423 | if (timercmp(&tv1, &tv2, <)) { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 424 | // Current time is greater than the expected timeout time. |
| 425 | // Should never happen. |
| 426 | VDBG("missed an expected timeout\n"); |
Paul Fox | ef81ce6 | 2006-03-29 23:01:33 +0000 | [diff] [blame] | 427 | timeout = 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 428 | } else { |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 429 | VDBG("adjusting timeout\n"); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 430 | timersub(&tv1, &tv2, &tv1); |
| 431 | timeout = 1000 * tv1.tv_sec |
| 432 | + tv1.tv_usec / 1000; |
| 433 | } |
| 434 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 435 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 436 | if ((fds[0].revents & POLLIN) == 0) { |
| 437 | if (fds[0].revents & POLLERR) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 438 | // FIXME: links routinely go down; |
| 439 | // this shouldn't necessarily exit. |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 440 | bb_error_msg("%s: poll error\n", intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 441 | if (ready) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 442 | run(script, "deconfig", |
| 443 | intf, &ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 444 | } |
| 445 | return EXIT_FAILURE; |
| 446 | } |
| 447 | continue; |
| 448 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 449 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 450 | // read ARP packet |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 451 | if (recv(fd, &p, sizeof (p), 0) < 0) { |
| 452 | why = "recv"; |
| 453 | goto bad; |
| 454 | } |
| 455 | if (p.hdr.ether_type != htons(ETHERTYPE_ARP)) |
| 456 | continue; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 458 | #ifdef DEBUG |
| 459 | { |
| 460 | struct ether_addr * sha = (struct ether_addr *) p.arp.arp_sha; |
| 461 | struct ether_addr * tha = (struct ether_addr *) p.arp.arp_tha; |
| 462 | struct in_addr * spa = (struct in_addr *) p.arp.arp_spa; |
| 463 | struct in_addr * tpa = (struct in_addr *) p.arp.arp_tpa; |
| 464 | VDBG("%s recv arp type=%d, op=%d,\n", |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 465 | intf, ntohs(p.hdr.ether_type), |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 466 | ntohs(p.arp.arp_op)); |
| 467 | VDBG("\tsource=%s %s\n", |
| 468 | ether_ntoa(sha), |
| 469 | inet_ntoa(*spa)); |
| 470 | VDBG("\ttarget=%s %s\n", |
| 471 | ether_ntoa(tha), |
| 472 | inet_ntoa(*tpa)); |
| 473 | } |
| 474 | #endif |
| 475 | if (p.arp.arp_op != htons(ARPOP_REQUEST) |
| 476 | && p.arp.arp_op != htons(ARPOP_REPLY)) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 477 | continue; |
| 478 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 479 | if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0 && |
| 480 | memcmp(&addr, &p.arp.arp_sha, ETH_ALEN) != 0) { |
| 481 | source_ip_conflict = 1; |
| 482 | } |
| 483 | if (memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0 && |
| 484 | p.arp.arp_op == htons(ARPOP_REQUEST) && |
| 485 | memcmp(&addr, &p.arp.arp_tha, ETH_ALEN) != 0) { |
| 486 | target_ip_conflict = 1; |
| 487 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 488 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 489 | VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n", |
| 490 | state, source_ip_conflict, target_ip_conflict); |
| 491 | switch (state) { |
| 492 | case PROBE: |
| 493 | case ANNOUNCE: |
| 494 | // When probing or announcing, check for source IP conflicts |
| 495 | // and other hosts doing ARP probes (target IP conflicts). |
| 496 | if (source_ip_conflict || target_ip_conflict) { |
| 497 | conflicts++; |
| 498 | if (conflicts >= MAX_CONFLICTS) { |
| 499 | VDBG("%s ratelimit\n", intf); |
| 500 | timeout = RATE_LIMIT_INTERVAL * 1000; |
| 501 | state = RATE_LIMIT_PROBE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 502 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 503 | |
| 504 | // restart the whole protocol |
| 505 | pick(&ip); |
| 506 | timeout = 0; |
| 507 | nprobes = 0; |
| 508 | nclaims = 0; |
| 509 | } |
| 510 | break; |
| 511 | case MONITOR: |
| 512 | // If a conflict, we try to defend with a single ARP probe. |
| 513 | if (source_ip_conflict) { |
| 514 | VDBG("monitor conflict -- defending\n"); |
| 515 | state = DEFEND; |
| 516 | timeout = DEFEND_INTERVAL * 1000; |
| 517 | (void)arp(fd, &saddr, |
| 518 | ARPOP_REQUEST, |
| 519 | &addr, ip, |
| 520 | &addr, ip); |
| 521 | } |
| 522 | break; |
| 523 | case DEFEND: |
| 524 | // Well, we tried. Start over (on conflict). |
| 525 | if (source_ip_conflict) { |
| 526 | state = PROBE; |
| 527 | VDBG("defend conflict -- starting over\n"); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 528 | ready = 0; |
| 529 | run(script, "deconfig", intf, &ip); |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 530 | |
| 531 | // restart the whole protocol |
| 532 | pick(&ip); |
| 533 | timeout = 0; |
| 534 | nprobes = 0; |
| 535 | nclaims = 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 536 | } |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 537 | break; |
| 538 | default: |
| 539 | // Invalid, should never happen. Restart the whole protocol. |
| 540 | VDBG("invalid state -- starting over\n"); |
| 541 | state = PROBE; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 542 | pick(&ip); |
| 543 | timeout = 0; |
| 544 | nprobes = 0; |
| 545 | nclaims = 0; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 546 | break; |
| 547 | } // switch state |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 548 | |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 549 | break; // case 1 (packets arriving) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 550 | default: |
| 551 | why = "poll"; |
| 552 | goto bad; |
Denis Vlasenko | 87d80dc | 2006-09-03 12:20:36 +0000 | [diff] [blame] | 553 | } // switch poll |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 554 | } |
| 555 | bad: |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 556 | if (foreground) |
| 557 | perror(why); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 558 | else |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 559 | syslog(LOG_ERR, "%s %s, %s error: %s", |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 560 | bb_applet_name, intf, why, strerror(errno)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 561 | return EXIT_FAILURE; |
| 562 | } |