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 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 21 | * 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * This can build as part of BusyBox or by itself: |
| 26 | * |
| 27 | * $(CROSS_COMPILE)cc -Os -Wall -DNO_BUSYBOX -DDEBUG -o zcip zcip.c |
| 28 | * |
| 29 | * ZCIP just manages the 169.254.*.* addresses. That network is not |
| 30 | * routed at the IP level, though various proxies or bridges can |
| 31 | * certainly be used. Its naming is built over multicast DNS. |
| 32 | */ |
| 33 | |
| 34 | // #define DEBUG |
| 35 | |
| 36 | // TODO: |
| 37 | // - more real-world usage/testing, especially daemon mode |
| 38 | // - kernel packet filters to reduce scheduling noise |
| 39 | // - avoid silent script failures, especially under load... |
| 40 | // - link status monitoring (restart on link-up; stop on link-down) |
| 41 | |
| 42 | #include <errno.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <stdio.h> |
| 45 | #include <string.h> |
| 46 | #include <syslog.h> |
| 47 | #include <poll.h> |
| 48 | #include <time.h> |
| 49 | #include <unistd.h> |
| 50 | |
| 51 | #include <sys/ioctl.h> |
| 52 | #include <sys/types.h> |
| 53 | #include <sys/wait.h> |
| 54 | #include <sys/time.h> |
| 55 | #include <sys/socket.h> |
| 56 | |
| 57 | #include <arpa/inet.h> |
| 58 | #include <netinet/in.h> |
| 59 | #include <netinet/ether.h> |
| 60 | #include <net/ethernet.h> |
| 61 | #include <net/if.h> |
| 62 | #include <net/if_arp.h> |
| 63 | |
| 64 | #include <linux/if_packet.h> |
| 65 | #include <linux/sockios.h> |
| 66 | |
| 67 | |
| 68 | struct arp_packet { |
| 69 | struct ether_header hdr; |
| 70 | // FIXME this part is netinet/if_ether.h "struct ether_arp" |
| 71 | struct arphdr arp; |
| 72 | struct ether_addr source_addr; |
| 73 | struct in_addr source_ip; |
| 74 | struct ether_addr target_addr; |
| 75 | struct in_addr target_ip; |
| 76 | } __attribute__ ((__packed__)); |
| 77 | |
| 78 | /* 169.254.0.0 */ |
| 79 | static const uint32_t LINKLOCAL_ADDR = 0xa9fe0000; |
| 80 | |
| 81 | /* protocol timeout parameters, specified in seconds */ |
| 82 | static const unsigned PROBE_WAIT = 1; |
| 83 | static const unsigned PROBE_MIN = 1; |
| 84 | static const unsigned PROBE_MAX = 2; |
| 85 | static const unsigned PROBE_NUM = 3; |
| 86 | static const unsigned MAX_CONFLICTS = 10; |
| 87 | static const unsigned RATE_LIMIT_INTERVAL = 60; |
| 88 | static const unsigned ANNOUNCE_WAIT = 2; |
| 89 | static const unsigned ANNOUNCE_NUM = 2; |
| 90 | static const unsigned ANNOUNCE_INTERVAL = 2; |
| 91 | static const time_t DEFEND_INTERVAL = 10; |
| 92 | |
| 93 | static const unsigned char ZCIP_VERSION[] = "0.75 (18 April 2005)"; |
| 94 | static char *prog; |
| 95 | |
| 96 | static const struct in_addr null_ip = { 0 }; |
| 97 | static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} }; |
| 98 | |
| 99 | static int verbose = 0; |
| 100 | |
| 101 | #ifdef DEBUG |
| 102 | |
| 103 | #define DBG(fmt,args...) \ |
| 104 | fprintf(stderr, "%s: " fmt , prog , ## args) |
| 105 | #define VDBG(fmt,args...) do { \ |
| 106 | if (verbose) fprintf(stderr, "%s: " fmt , prog ,## args); \ |
| 107 | } while (0) |
| 108 | #else |
| 109 | |
| 110 | #define DBG(fmt,args...) \ |
| 111 | do { } while (0) |
| 112 | #define VDBG DBG |
| 113 | #endif /* DEBUG */ |
| 114 | |
| 115 | /** |
| 116 | * Pick a random link local IP address on 169.254/16, except that |
| 117 | * the first and last 256 addresses are reserved. |
| 118 | */ |
| 119 | static void |
| 120 | pick(struct in_addr *ip) |
| 121 | { |
| 122 | unsigned tmp; |
| 123 | |
| 124 | /* use cheaper math than lrand48() mod N */ |
| 125 | do { |
| 126 | tmp = (lrand48() >> 16) & IN_CLASSB_HOST; |
| 127 | } while (tmp > (IN_CLASSB_HOST - 0x0200)); |
| 128 | ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Broadcast an ARP packet. |
| 133 | */ |
| 134 | static int |
| 135 | arp(int fd, struct sockaddr *saddr, int op, |
| 136 | const struct ether_addr *source_addr, struct in_addr source_ip, |
| 137 | const struct ether_addr *target_addr, struct in_addr target_ip) |
| 138 | { |
| 139 | struct arp_packet p; |
| 140 | |
| 141 | // ether header |
| 142 | p.hdr.ether_type = htons(ETHERTYPE_ARP); |
| 143 | memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN); |
| 144 | memset(p.hdr.ether_dhost, 0xff, ETH_ALEN); |
| 145 | |
| 146 | // arp request |
| 147 | p.arp.ar_hrd = htons(ARPHRD_ETHER); |
| 148 | p.arp.ar_pro = htons(ETHERTYPE_IP); |
| 149 | p.arp.ar_hln = ETH_ALEN; |
| 150 | p.arp.ar_pln = 4; |
| 151 | p.arp.ar_op = htons(op); |
| 152 | memcpy(&p.source_addr, source_addr, ETH_ALEN); |
| 153 | memcpy(&p.source_ip, &source_ip, sizeof (p.source_ip)); |
| 154 | memcpy(&p.target_addr, target_addr, ETH_ALEN); |
| 155 | memcpy(&p.target_ip, &target_ip, sizeof (p.target_ip)); |
| 156 | |
| 157 | // send it |
| 158 | if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { |
| 159 | perror("sendto"); |
| 160 | return -errno; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Run a script. |
| 167 | */ |
| 168 | static int |
| 169 | run(char *script, char *arg, char *intf, struct in_addr *ip) |
| 170 | { |
| 171 | int pid, status; |
| 172 | char *why; |
| 173 | |
| 174 | if (script != NULL) { |
| 175 | VDBG("%s run %s %s\n", intf, script, arg); |
| 176 | if (ip != NULL) { |
| 177 | char *addr = inet_ntoa(*ip); |
| 178 | setenv("ip", addr, 1); |
| 179 | syslog(LOG_INFO, "%s %s %s", arg, intf, addr); |
| 180 | } |
| 181 | |
| 182 | pid = vfork(); |
| 183 | if (pid < 0) { // error |
| 184 | why = "vfork"; |
| 185 | goto bad; |
| 186 | } else if (pid == 0) { // child |
| 187 | execl(script, script, arg, NULL); |
| 188 | perror("execl"); |
| 189 | _exit(EXIT_FAILURE); |
| 190 | } |
| 191 | |
| 192 | if (waitpid(pid, &status, 0) <= 0) { |
| 193 | why = "waitpid"; |
| 194 | goto bad; |
| 195 | } |
| 196 | if (WEXITSTATUS(status) != 0) { |
| 197 | fprintf(stderr, "%s: script %s failed, exit=%d\n", |
| 198 | prog, script, WEXITSTATUS(status)); |
| 199 | return -errno; |
| 200 | } |
| 201 | } |
| 202 | return 0; |
| 203 | bad: |
| 204 | status = -errno; |
| 205 | syslog(LOG_ERR, "%s %s, %s error: %s", |
| 206 | arg, intf, why, strerror(errno)); |
| 207 | return status; |
| 208 | } |
| 209 | |
| 210 | #ifndef NO_BUSYBOX |
| 211 | #include "busybox.h" |
| 212 | #endif |
| 213 | |
| 214 | /** |
| 215 | * Print usage information. |
| 216 | */ |
| 217 | static void __attribute__ ((noreturn)) |
| 218 | usage(const char *msg) |
| 219 | { |
| 220 | fprintf(stderr, "%s: %s\n", prog, msg); |
| 221 | #ifdef NO_BUSYBOX |
| 222 | fprintf(stderr, "Usage: %s [OPTIONS] ifname script\n" |
| 223 | "\t-f foreground mode (implied by -v)\n" |
| 224 | "\t-q quit after address (no daemon)\n" |
| 225 | "\t-r 169.254.x.x request this address first\n" |
| 226 | "\t-v verbose; show version\n", |
| 227 | prog); |
| 228 | exit(0); |
| 229 | #else |
| 230 | bb_show_usage(); |
| 231 | #endif |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Return milliseconds of random delay, up to "secs" seconds. |
| 236 | */ |
| 237 | static inline unsigned |
| 238 | ms_rdelay(unsigned secs) |
| 239 | { |
| 240 | return lrand48() % (secs * 1000); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * main program |
| 245 | */ |
| 246 | int |
| 247 | main(int argc, char *argv[]) |
| 248 | __attribute__ ((weak, alias ("zcip_main"))); |
| 249 | |
| 250 | int zcip_main(int argc, char *argv[]) |
| 251 | { |
| 252 | char *intf = NULL; |
| 253 | char *script = NULL; |
| 254 | int quit = 0; |
| 255 | int foreground = 0; |
| 256 | |
| 257 | char *why; |
| 258 | struct sockaddr saddr; |
| 259 | struct ether_addr addr; |
| 260 | struct in_addr ip = { 0 }; |
| 261 | int fd; |
| 262 | int ready = 0; |
| 263 | suseconds_t timeout = 0; // milliseconds |
| 264 | time_t defend = 0; |
| 265 | unsigned conflicts = 0; |
| 266 | unsigned nprobes = 0; |
| 267 | unsigned nclaims = 0; |
| 268 | int t; |
| 269 | |
| 270 | // parse commandline: prog [options] ifname script |
| 271 | prog = argv[0]; |
| 272 | while ((t = getopt(argc, argv, "fqr:v")) != EOF) { |
| 273 | switch (t) { |
| 274 | case 'f': |
| 275 | foreground = 1; |
| 276 | continue; |
| 277 | case 'q': |
| 278 | quit = 1; |
| 279 | continue; |
| 280 | case 'r': |
| 281 | if (inet_aton(optarg, &ip) == 0 |
| 282 | || (ntohl(ip.s_addr) & IN_CLASSB_NET) |
| 283 | != LINKLOCAL_ADDR) { |
| 284 | usage("invalid link address"); |
| 285 | } |
| 286 | continue; |
| 287 | case 'v': |
| 288 | if (!verbose) |
| 289 | printf("%s: version %s\n", prog, ZCIP_VERSION); |
| 290 | verbose++; |
| 291 | foreground = 1; |
| 292 | continue; |
| 293 | default: |
| 294 | usage("bad option"); |
| 295 | } |
| 296 | } |
| 297 | if (optind < argc - 1) { |
| 298 | intf = argv[optind++]; |
| 299 | setenv("interface", intf, 1); |
| 300 | script = argv[optind++]; |
| 301 | } |
| 302 | if (optind != argc || !intf) |
| 303 | usage("wrong number of arguments"); |
| 304 | openlog(prog, 0, LOG_DAEMON); |
| 305 | |
| 306 | // initialize the interface (modprobe, ifup, etc) |
| 307 | if (run(script, "init", intf, NULL) < 0) |
| 308 | return EXIT_FAILURE; |
| 309 | |
| 310 | // initialize saddr |
| 311 | memset(&saddr, 0, sizeof (saddr)); |
| 312 | strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data)); |
| 313 | |
| 314 | // open an ARP socket |
| 315 | if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) { |
| 316 | why = "open"; |
| 317 | fail: |
| 318 | foreground = 1; |
| 319 | goto bad; |
| 320 | } |
| 321 | // bind to the interface's ARP socket |
| 322 | if (bind(fd, &saddr, sizeof (saddr)) < 0) { |
| 323 | why = "bind"; |
| 324 | goto fail; |
| 325 | } else { |
| 326 | struct ifreq ifr; |
| 327 | short seed[3]; |
| 328 | |
| 329 | // get the interface's ethernet address |
| 330 | memset(&ifr, 0, sizeof (ifr)); |
| 331 | strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name)); |
| 332 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { |
| 333 | why = "get ethernet address"; |
| 334 | goto fail; |
| 335 | } |
| 336 | memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
| 337 | |
| 338 | // start with some stable ip address, either a function of |
| 339 | // the hardware address or else the last address we used. |
| 340 | // NOTE: the sequence of addresses we try changes only |
| 341 | // depending on when we detect conflicts. |
| 342 | memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
| 343 | seed48(seed); |
| 344 | if (ip.s_addr == 0) |
| 345 | pick(&ip); |
| 346 | } |
| 347 | |
| 348 | // FIXME cases to handle: |
| 349 | // - zcip already running! |
| 350 | // - link already has local address... just defend/update |
| 351 | |
| 352 | // daemonize now; don't delay system startup |
| 353 | if (!foreground) { |
| 354 | if (daemon(0, verbose) < 0) { |
| 355 | why = "daemon"; |
| 356 | goto bad; |
| 357 | } |
| 358 | syslog(LOG_INFO, "start, interface %s", intf); |
| 359 | } |
| 360 | |
| 361 | // run the dynamic address negotiation protocol, |
| 362 | // restarting after address conflicts: |
| 363 | // - start with some address we want to try |
| 364 | // - short random delay |
| 365 | // - arp probes to see if another host else uses it |
| 366 | // - arp announcements that we're claiming it |
| 367 | // - use it |
| 368 | // - defend it, within limits |
| 369 | while (1) { |
| 370 | struct pollfd fds[1]; |
| 371 | struct timeval tv1; |
| 372 | struct arp_packet p; |
| 373 | |
| 374 | fds[0].fd = fd; |
| 375 | fds[0].events = POLLIN; |
| 376 | fds[0].revents = 0; |
| 377 | |
| 378 | // poll, being ready to adjust current timeout |
| 379 | if (timeout > 0) { |
| 380 | gettimeofday(&tv1, NULL); |
| 381 | tv1.tv_usec += (timeout % 1000) * 1000; |
| 382 | while (tv1.tv_usec > 1000000) { |
| 383 | tv1.tv_usec -= 1000000; |
| 384 | tv1.tv_sec++; |
| 385 | } |
| 386 | tv1.tv_sec += timeout / 1000; |
| 387 | } else if (timeout == 0) { |
| 388 | timeout = ms_rdelay(PROBE_WAIT); |
| 389 | // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to |
| 390 | // make the kernel filter out all packets except |
| 391 | // ones we'd care about. |
| 392 | } |
| 393 | VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n", |
| 394 | timeout, intf, nprobes, nclaims); |
| 395 | switch (poll(fds, 1, timeout)) { |
| 396 | |
| 397 | // timeouts trigger protocol transitions |
| 398 | case 0: |
| 399 | // probes |
| 400 | if (nprobes < PROBE_NUM) { |
| 401 | nprobes++; |
| 402 | VDBG("probe/%d %s@%s\n", |
| 403 | nprobes, intf, inet_ntoa(ip)); |
| 404 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
| 405 | &addr, null_ip, |
| 406 | &null_addr, ip); |
| 407 | if (nprobes < PROBE_NUM) { |
| 408 | timeout = PROBE_MIN * 1000; |
| 409 | timeout += ms_rdelay(PROBE_MAX |
| 410 | - PROBE_MIN); |
| 411 | } else |
| 412 | timeout = ANNOUNCE_WAIT * 1000; |
| 413 | } |
| 414 | // then announcements |
| 415 | else if (nclaims < ANNOUNCE_NUM) { |
| 416 | nclaims++; |
| 417 | VDBG("announce/%d %s@%s\n", |
| 418 | nclaims, intf, inet_ntoa(ip)); |
| 419 | (void)arp(fd, &saddr, ARPOP_REQUEST, |
| 420 | &addr, ip, |
| 421 | &addr, ip); |
| 422 | if (nclaims < ANNOUNCE_NUM) { |
| 423 | timeout = ANNOUNCE_INTERVAL * 1000; |
| 424 | } else { |
| 425 | // link is ok to use earlier |
| 426 | run(script, "config", intf, &ip); |
| 427 | ready = 1; |
| 428 | conflicts = 0; |
| 429 | timeout = -1; |
| 430 | |
| 431 | // NOTE: all other exit paths |
| 432 | // should deconfig ... |
| 433 | if (quit) |
| 434 | return EXIT_SUCCESS; |
| 435 | // FIXME update filters |
| 436 | } |
| 437 | } |
| 438 | break; |
| 439 | |
| 440 | // packets arriving |
| 441 | case 1: |
| 442 | // maybe adjust timeout |
| 443 | if (timeout > 0) { |
| 444 | struct timeval tv2; |
| 445 | |
| 446 | gettimeofday(&tv2, NULL); |
| 447 | if (timercmp(&tv1, &tv2, <)) { |
| 448 | timeout = -1; |
| 449 | } else { |
| 450 | timersub(&tv1, &tv2, &tv1); |
| 451 | timeout = 1000 * tv1.tv_sec |
| 452 | + tv1.tv_usec / 1000; |
| 453 | } |
| 454 | } |
| 455 | if ((fds[0].revents & POLLIN) == 0) { |
| 456 | if (fds[0].revents & POLLERR) { |
| 457 | // FIXME: links routinely go down; |
| 458 | // this shouldn't necessarily exit. |
| 459 | fprintf(stderr, "%s %s: poll error\n", |
| 460 | prog, intf); |
| 461 | if (ready) { |
| 462 | run(script, "deconfig", |
| 463 | intf, &ip); |
| 464 | } |
| 465 | return EXIT_FAILURE; |
| 466 | } |
| 467 | continue; |
| 468 | } |
| 469 | // read ARP packet |
| 470 | if (recv(fd, &p, sizeof (p), 0) < 0) { |
| 471 | why = "recv"; |
| 472 | goto bad; |
| 473 | } |
| 474 | if (p.hdr.ether_type != htons(ETHERTYPE_ARP)) |
| 475 | continue; |
| 476 | |
| 477 | VDBG("%s recv arp type=%d, op=%d,\n", |
| 478 | intf, ntohs(p.hdr.ether_type), |
| 479 | ntohs(p.arp.ar_op)); |
| 480 | VDBG("\tsource=%s %s\n", |
| 481 | ether_ntoa(&p.source_addr), |
| 482 | inet_ntoa(p.source_ip)); |
| 483 | VDBG("\ttarget=%s %s\n", |
| 484 | ether_ntoa(&p.target_addr), |
| 485 | inet_ntoa(p.target_ip)); |
| 486 | if (p.arp.ar_op != htons(ARPOP_REQUEST) |
| 487 | && p.arp.ar_op != htons(ARPOP_REPLY)) |
| 488 | continue; |
| 489 | |
| 490 | // some cases are always conflicts |
| 491 | if ((p.source_ip.s_addr == ip.s_addr) |
| 492 | && (memcmp(&addr, &p.source_addr, |
| 493 | ETH_ALEN) != 0)) { |
| 494 | collision: |
| 495 | VDBG("%s ARP conflict from %s\n", intf, |
| 496 | ether_ntoa(&p.source_addr)); |
| 497 | if (ready) { |
| 498 | time_t now = time(0); |
| 499 | |
| 500 | if ((defend + DEFEND_INTERVAL) |
| 501 | < now) { |
| 502 | defend = now; |
| 503 | (void)arp(fd, &saddr, |
| 504 | ARPOP_REQUEST, |
| 505 | &addr, ip, |
| 506 | &addr, ip); |
| 507 | VDBG("%s defend\n", intf); |
| 508 | timeout = -1; |
| 509 | continue; |
| 510 | } |
| 511 | defend = now; |
| 512 | ready = 0; |
| 513 | run(script, "deconfig", intf, &ip); |
| 514 | // FIXME rm filters: setsockopt(fd, |
| 515 | // SO_DETACH_FILTER, ...) |
| 516 | } |
| 517 | conflicts++; |
| 518 | if (conflicts >= MAX_CONFLICTS) { |
| 519 | VDBG("%s ratelimit\n", intf); |
| 520 | sleep(RATE_LIMIT_INTERVAL); |
| 521 | } |
| 522 | // restart the whole protocol |
| 523 | pick(&ip); |
| 524 | timeout = 0; |
| 525 | nprobes = 0; |
| 526 | nclaims = 0; |
| 527 | } |
| 528 | // two hosts probing one address is a collision too |
| 529 | else if (p.target_ip.s_addr == ip.s_addr |
| 530 | && nclaims == 0 |
| 531 | && p.arp.ar_op == htons(ARPOP_REQUEST) |
| 532 | && memcmp(&addr, &p.target_addr, |
| 533 | ETH_ALEN) != 0) { |
| 534 | goto collision; |
| 535 | } |
| 536 | break; |
| 537 | |
| 538 | default: |
| 539 | why = "poll"; |
| 540 | goto bad; |
| 541 | } |
| 542 | } |
| 543 | bad: |
| 544 | if (foreground) |
| 545 | perror(why); |
| 546 | else |
| 547 | syslog(LOG_ERR, "%s %s, %s error: %s", |
| 548 | prog, intf, why, strerror(errno)); |
| 549 | return EXIT_FAILURE; |
| 550 | } |