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