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