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