Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RFC3927 ZeroConf IPv4 Link-Local addressing |
| 3 | * (see <http://www.zeroconf.org/>) |
| 4 | * |
| 5 | * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com) |
| 6 | * Copyright (C) 2004 by David Brownell |
| 7 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 8 | * 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] | 9 | */ |
| 10 | |
| 11 | /* |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 12 | * ZCIP just manages the 169.254.*.* addresses. That network is not |
| 13 | * routed at the IP level, though various proxies or bridges can |
| 14 | * certainly be used. Its naming is built over multicast DNS. |
| 15 | */ |
| 16 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 17 | // #define DEBUG |
| 18 | |
| 19 | // TODO: |
| 20 | // - more real-world usage/testing, especially daemon mode |
| 21 | // - kernel packet filters to reduce scheduling noise |
| 22 | // - avoid silent script failures, especially under load... |
| 23 | // - link status monitoring (restart on link-up; stop on link-down) |
| 24 | |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 25 | #include "busybox.h" |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <syslog.h> |
| 29 | #include <poll.h> |
| 30 | #include <time.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 31 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 32 | #include <sys/wait.h> |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 33 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 34 | #include <netinet/ether.h> |
| 35 | #include <net/ethernet.h> |
| 36 | #include <net/if.h> |
| 37 | #include <net/if_arp.h> |
| 38 | |
| 39 | #include <linux/if_packet.h> |
| 40 | #include <linux/sockios.h> |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 41 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 42 | |
Bernhard Reutner-Fischer | ec351c3 | 2005-12-13 10:28:25 +0000 | [diff] [blame] | 43 | struct arp_packet { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 44 | struct ether_header hdr; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 45 | // FIXME this part is netinet/if_ether.h "struct ether_arp" |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 46 | struct arphdr arp; |
| 47 | struct ether_addr source_addr; |
| 48 | struct in_addr source_ip; |
| 49 | struct ether_addr target_addr; |
| 50 | struct in_addr target_ip; |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 51 | } ATTRIBUTE_PACKED; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 52 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 53 | enum { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 54 | /* 169.254.0.0 */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 55 | LINKLOCAL_ADDR = 0xa9fe0000, |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 56 | |
| 57 | /* protocol timeout parameters, specified in seconds */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 58 | PROBE_WAIT = 1, |
| 59 | PROBE_MIN = 1, |
| 60 | PROBE_MAX = 2, |
| 61 | PROBE_NUM = 3, |
| 62 | MAX_CONFLICTS = 10, |
| 63 | RATE_LIMIT_INTERVAL = 60, |
| 64 | ANNOUNCE_WAIT = 2, |
| 65 | ANNOUNCE_NUM = 2, |
| 66 | ANNOUNCE_INTERVAL = 2, |
| 67 | DEFEND_INTERVAL = 10 |
| 68 | }; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 69 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 70 | static const struct in_addr null_ip = { 0 }; |
| 71 | static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} }; |
| 72 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 73 | static int verbose = 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 74 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 75 | #define DBG(fmt,args...) \ |
| 76 | do { } while (0) |
| 77 | #define VDBG DBG |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 78 | |
| 79 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 80 | * Pick a random link local IP address on 169.254/16, except that |
| 81 | * the first and last 256 addresses are reserved. |
| 82 | */ |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 83 | static void pick(struct in_addr *ip) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 84 | { |
| 85 | unsigned tmp; |
| 86 | |
| 87 | /* use cheaper math than lrand48() mod N */ |
| 88 | do { |
| 89 | tmp = (lrand48() >> 16) & IN_CLASSB_HOST; |
| 90 | } while (tmp > (IN_CLASSB_HOST - 0x0200)); |
| 91 | ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp); |
| 92 | } |
| 93 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 94 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 95 | * Broadcast an ARP packet. |
| 96 | */ |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 97 | static int arp(int fd, struct sockaddr *saddr, int op, |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 98 | const struct ether_addr *source_addr, struct in_addr source_ip, |
| 99 | const struct ether_addr *target_addr, struct in_addr target_ip) |
| 100 | { |
| 101 | struct arp_packet p; |
| 102 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 103 | // ether header |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 104 | p.hdr.ether_type = htons(ETHERTYPE_ARP); |
| 105 | memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN); |
| 106 | memset(p.hdr.ether_dhost, 0xff, ETH_ALEN); |
| 107 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 108 | // arp request |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 109 | p.arp.ar_hrd = htons(ARPHRD_ETHER); |
| 110 | p.arp.ar_pro = htons(ETHERTYPE_IP); |
| 111 | p.arp.ar_hln = ETH_ALEN; |
| 112 | p.arp.ar_pln = 4; |
| 113 | p.arp.ar_op = htons(op); |
| 114 | memcpy(&p.source_addr, source_addr, ETH_ALEN); |
| 115 | memcpy(&p.source_ip, &source_ip, sizeof (p.source_ip)); |
| 116 | memcpy(&p.target_addr, target_addr, ETH_ALEN); |
| 117 | memcpy(&p.target_ip, &target_ip, sizeof (p.target_ip)); |
| 118 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 119 | // send it |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 120 | if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 121 | perror("sendto"); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 122 | return -errno; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 127 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 128 | * Run a script. |
| 129 | */ |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 130 | 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] | 131 | { |
| 132 | int pid, status; |
| 133 | char *why; |
| 134 | |
| 135 | if (script != NULL) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 136 | VDBG("%s run %s %s\n", intf, script, arg); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 137 | if (ip != NULL) { |
| 138 | char *addr = inet_ntoa(*ip); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 139 | setenv("ip", addr, 1); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 140 | syslog(LOG_INFO, "%s %s %s", arg, intf, addr); |
| 141 | } |
| 142 | |
| 143 | pid = vfork(); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 144 | if (pid < 0) { // error |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 145 | why = "vfork"; |
| 146 | goto bad; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 147 | } else if (pid == 0) { // child |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 148 | execl(script, script, arg, NULL); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 149 | perror("execl"); |
| 150 | _exit(EXIT_FAILURE); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 151 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 152 | |
| 153 | if (waitpid(pid, &status, 0) <= 0) { |
| 154 | why = "waitpid"; |
| 155 | goto bad; |
| 156 | } |
| 157 | if (WEXITSTATUS(status) != 0) { |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 158 | bb_error_msg("script %s failed, exit=%d\n", |
| 159 | script, WEXITSTATUS(status)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 160 | return -errno; |
| 161 | } |
| 162 | } |
| 163 | return 0; |
| 164 | bad: |
| 165 | status = -errno; |
| 166 | syslog(LOG_ERR, "%s %s, %s error: %s", |
| 167 | arg, intf, why, strerror(errno)); |
| 168 | return status; |
| 169 | } |
| 170 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 171 | |
| 172 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 173 | * Return milliseconds of random delay, up to "secs" seconds. |
| 174 | */ |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 175 | static inline unsigned ms_rdelay(unsigned secs) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 176 | { |
| 177 | return lrand48() % (secs * 1000); |
| 178 | } |
| 179 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 180 | /** |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 181 | * main program |
| 182 | */ |
Bernhard Reutner-Fischer | 38d6615 | 2005-10-21 10:43:11 +0000 | [diff] [blame] | 183 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 184 | int zcip_main(int argc, char *argv[]) |
| 185 | { |
| 186 | char *intf = NULL; |
| 187 | char *script = NULL; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 188 | int quit = 0; |
| 189 | int foreground = 0; |
| 190 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 191 | char *why; |
| 192 | struct sockaddr saddr; |
| 193 | struct ether_addr addr; |
| 194 | struct in_addr ip = { 0 }; |
| 195 | int fd; |
| 196 | int ready = 0; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 197 | suseconds_t timeout = 0; // milliseconds |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 198 | time_t defend = 0; |
| 199 | unsigned conflicts = 0; |
| 200 | unsigned nprobes = 0; |
| 201 | unsigned nclaims = 0; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 202 | int t; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 203 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 204 | // parse commandline: prog [options] ifname script |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 205 | while ((t = getopt(argc, argv, "fqr:v")) != EOF) { |
| 206 | switch (t) { |
| 207 | case 'f': |
| 208 | foreground = 1; |
| 209 | continue; |
| 210 | case 'q': |
| 211 | quit = 1; |
| 212 | continue; |
| 213 | case 'r': |
| 214 | if (inet_aton(optarg, &ip) == 0 |
| 215 | || (ntohl(ip.s_addr) & IN_CLASSB_NET) |
| 216 | != LINKLOCAL_ADDR) { |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 217 | bb_error_msg_and_die("invalid link address"); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 218 | } |
| 219 | continue; |
| 220 | case 'v': |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 221 | verbose++; |
| 222 | foreground = 1; |
| 223 | continue; |
| 224 | default: |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 225 | bb_error_msg_and_die("bad option"); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 226 | } |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 227 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 228 | if (optind < argc - 1) { |
| 229 | intf = argv[optind++]; |
| 230 | setenv("interface", intf, 1); |
| 231 | script = argv[optind++]; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 232 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 233 | if (optind != argc || !intf) |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 234 | bb_show_usage(); |
| 235 | openlog(bb_applet_name, 0, LOG_DAEMON); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 236 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 237 | // initialize the interface (modprobe, ifup, etc) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 238 | if (run(script, "init", intf, NULL) < 0) |
| 239 | return EXIT_FAILURE; |
| 240 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 241 | // initialize saddr |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 242 | memset(&saddr, 0, sizeof (saddr)); |
Rob Landley | 768945b | 2006-06-25 00:34:52 +0000 | [diff] [blame] | 243 | safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 244 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 245 | // open an ARP socket |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 246 | if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) { |
| 247 | why = "open"; |
| 248 | fail: |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 249 | foreground = 1; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 250 | goto bad; |
| 251 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 252 | // bind to the interface's ARP socket |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 253 | if (bind(fd, &saddr, sizeof (saddr)) < 0) { |
| 254 | why = "bind"; |
| 255 | goto fail; |
| 256 | } else { |
| 257 | struct ifreq ifr; |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 258 | unsigned short seed[3]; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 259 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 260 | // get the interface's ethernet address |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 261 | memset(&ifr, 0, sizeof (ifr)); |
| 262 | strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name)); |
| 263 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { |
| 264 | why = "get ethernet address"; |
| 265 | goto fail; |
| 266 | } |
| 267 | memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
| 268 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 269 | // start with some stable ip address, either a function of |
| 270 | // the hardware address or else the last address we used. |
| 271 | // NOTE: the sequence of addresses we try changes only |
| 272 | // depending on when we detect conflicts. |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 273 | memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
| 274 | seed48(seed); |
| 275 | if (ip.s_addr == 0) |
| 276 | pick(&ip); |
| 277 | } |
| 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) { |
| 285 | if (daemon(0, verbose) < 0) { |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 286 | why = "daemon"; |
| 287 | goto bad; |
| 288 | } |
| 289 | syslog(LOG_INFO, "start, interface %s", intf); |
| 290 | } |
| 291 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 292 | // run the dynamic address negotiation protocol, |
| 293 | // restarting after address conflicts: |
| 294 | // - start with some address we want to try |
| 295 | // - short random delay |
| 296 | // - arp probes to see if another host else uses it |
| 297 | // - arp announcements that we're claiming it |
| 298 | // - use it |
| 299 | // - defend it, within limits |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 300 | while (1) { |
| 301 | struct pollfd fds[1]; |
| 302 | struct timeval tv1; |
| 303 | struct arp_packet p; |
| 304 | |
| 305 | fds[0].fd = fd; |
| 306 | fds[0].events = POLLIN; |
| 307 | fds[0].revents = 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 | } |
Rob Landley | 00c051e | 2006-06-30 14:05:19 +0000 | [diff] [blame] | 316 | gettimeofday(&tv1, NULL); |
| 317 | tv1.tv_usec += (timeout % 1000) * 1000; |
| 318 | while (tv1.tv_usec > 1000000) { |
| 319 | tv1.tv_usec -= 1000000; |
| 320 | tv1.tv_sec++; |
| 321 | } |
| 322 | tv1.tv_sec += timeout / 1000; |
| 323 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 324 | VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n", |
| 325 | timeout, intf, nprobes, nclaims); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 326 | switch (poll(fds, 1, timeout)) { |
| 327 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 328 | // timeouts trigger protocol transitions |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 329 | case 0: |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 330 | // probes |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 331 | if (nprobes < PROBE_NUM) { |
| 332 | nprobes++; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 333 | VDBG("probe/%d %s@%s\n", |
| 334 | nprobes, intf, inet_ntoa(ip)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 335 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
| 336 | &addr, null_ip, |
| 337 | &null_addr, ip); |
| 338 | if (nprobes < PROBE_NUM) { |
| 339 | timeout = PROBE_MIN * 1000; |
| 340 | timeout += ms_rdelay(PROBE_MAX |
| 341 | - PROBE_MIN); |
| 342 | } else |
| 343 | timeout = ANNOUNCE_WAIT * 1000; |
| 344 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 345 | // then announcements |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 346 | else if (nclaims < ANNOUNCE_NUM) { |
| 347 | nclaims++; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 348 | VDBG("announce/%d %s@%s\n", |
| 349 | nclaims, intf, inet_ntoa(ip)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 350 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
| 351 | &addr, ip, |
| 352 | &addr, ip); |
| 353 | if (nclaims < ANNOUNCE_NUM) { |
| 354 | timeout = ANNOUNCE_INTERVAL * 1000; |
| 355 | } else { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 356 | // link is ok to use earlier |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 357 | run(script, "config", intf, &ip); |
| 358 | ready = 1; |
| 359 | conflicts = 0; |
| 360 | timeout = -1; |
| 361 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 362 | // NOTE: all other exit paths |
| 363 | // should deconfig ... |
| 364 | if (quit) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 365 | return EXIT_SUCCESS; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 366 | // FIXME update filters |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | break; |
| 370 | |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 371 | // packets arriving |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 372 | case 1: |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 373 | // maybe adjust timeout |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 374 | if (timeout > 0) { |
| 375 | struct timeval tv2; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 376 | |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 377 | gettimeofday(&tv2, NULL); |
| 378 | if (timercmp(&tv1, &tv2, <)) { |
Paul Fox | ef81ce6 | 2006-03-29 23:01:33 +0000 | [diff] [blame] | 379 | timeout = 0; |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 380 | } else { |
| 381 | timersub(&tv1, &tv2, &tv1); |
| 382 | timeout = 1000 * tv1.tv_sec |
| 383 | + tv1.tv_usec / 1000; |
| 384 | } |
| 385 | } |
| 386 | if ((fds[0].revents & POLLIN) == 0) { |
| 387 | if (fds[0].revents & POLLERR) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 388 | // FIXME: links routinely go down; |
| 389 | // this shouldn't necessarily exit. |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 390 | bb_error_msg("%s: poll error\n", intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 391 | if (ready) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 392 | run(script, "deconfig", |
| 393 | intf, &ip); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 394 | } |
| 395 | return EXIT_FAILURE; |
| 396 | } |
| 397 | continue; |
| 398 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 399 | // read ARP packet |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 400 | if (recv(fd, &p, sizeof (p), 0) < 0) { |
| 401 | why = "recv"; |
| 402 | goto bad; |
| 403 | } |
| 404 | if (p.hdr.ether_type != htons(ETHERTYPE_ARP)) |
| 405 | continue; |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 406 | |
| 407 | VDBG("%s recv arp type=%d, op=%d,\n", |
| 408 | intf, ntohs(p.hdr.ether_type), |
| 409 | ntohs(p.arp.ar_op)); |
| 410 | VDBG("\tsource=%s %s\n", |
| 411 | ether_ntoa(&p.source_addr), |
| 412 | inet_ntoa(p.source_ip)); |
| 413 | VDBG("\ttarget=%s %s\n", |
| 414 | ether_ntoa(&p.target_addr), |
| 415 | inet_ntoa(p.target_ip)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 416 | if (p.arp.ar_op != htons(ARPOP_REQUEST) |
| 417 | && p.arp.ar_op != htons(ARPOP_REPLY)) |
| 418 | continue; |
| 419 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 420 | // some cases are always conflicts |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 421 | if ((p.source_ip.s_addr == ip.s_addr) |
| 422 | && (memcmp(&addr, &p.source_addr, |
| 423 | ETH_ALEN) != 0)) { |
| 424 | collision: |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 425 | VDBG("%s ARP conflict from %s\n", intf, |
| 426 | ether_ntoa(&p.source_addr)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 427 | if (ready) { |
| 428 | time_t now = time(0); |
| 429 | |
| 430 | if ((defend + DEFEND_INTERVAL) |
| 431 | < now) { |
| 432 | defend = now; |
| 433 | (void)arp(fd, &saddr, |
| 434 | ARPOP_REQUEST, |
| 435 | &addr, ip, |
| 436 | &addr, ip); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 437 | VDBG("%s defend\n", intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 438 | timeout = -1; |
| 439 | continue; |
| 440 | } |
| 441 | defend = now; |
| 442 | ready = 0; |
| 443 | run(script, "deconfig", intf, &ip); |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 444 | // FIXME rm filters: setsockopt(fd, |
| 445 | // SO_DETACH_FILTER, ...) |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 446 | } |
| 447 | conflicts++; |
| 448 | if (conflicts >= MAX_CONFLICTS) { |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 449 | VDBG("%s ratelimit\n", intf); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 450 | sleep(RATE_LIMIT_INTERVAL); |
| 451 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 452 | // restart the whole protocol |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 453 | pick(&ip); |
| 454 | timeout = 0; |
| 455 | nprobes = 0; |
| 456 | nclaims = 0; |
| 457 | } |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 458 | // two hosts probing one address is a collision too |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 459 | else if (p.target_ip.s_addr == ip.s_addr |
| 460 | && nclaims == 0 |
| 461 | && p.arp.ar_op == htons(ARPOP_REQUEST) |
| 462 | && memcmp(&addr, &p.target_addr, |
| 463 | ETH_ALEN) != 0) { |
| 464 | goto collision; |
| 465 | } |
| 466 | break; |
| 467 | |
| 468 | default: |
| 469 | why = "poll"; |
| 470 | goto bad; |
| 471 | } |
| 472 | } |
| 473 | bad: |
Rob Landley | e3752e5 | 2005-05-03 03:28:55 +0000 | [diff] [blame] | 474 | if (foreground) |
| 475 | perror(why); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 476 | else |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 477 | syslog(LOG_ERR, "%s %s, %s error: %s", |
Rob Landley | 53433b3 | 2006-06-22 22:28:29 +0000 | [diff] [blame] | 478 | bb_applet_name, intf, why, strerror(errno)); |
Rob Landley | ad8071f | 2005-04-30 03:49:37 +0000 | [diff] [blame] | 479 | return EXIT_FAILURE; |
| 480 | } |