1 /* vi: set sw=4 ts=4: */
3 * RFC3927 ZeroConf IPv4 Link-Local addressing
4 * (see <http://www.zeroconf.org/>)
6 * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com)
7 * Copyright (C) 2004 by David Brownell
9 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
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.
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)
35 #include <netinet/ether.h>
36 #include <net/ethernet.h>
38 #include <net/if_arp.h>
40 #include <linux/if_packet.h>
41 #include <linux/sockios.h>
45 struct ether_header hdr;
46 // FIXME this part is netinet/if_ether.h "struct ether_arp"
48 struct ether_addr source_addr;
49 struct in_addr source_ip;
50 struct ether_addr target_addr;
51 struct in_addr target_ip;
56 LINKLOCAL_ADDR = 0xa9fe0000,
58 /* protocol timeout parameters, specified in seconds */
64 RATE_LIMIT_INTERVAL = 60,
67 ANNOUNCE_INTERVAL = 2,
71 static const struct in_addr null_ip = { 0 };
72 static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} };
74 static int verbose = 0;
76 #define DBG(fmt,args...) \
81 * Pick a random link local IP address on 169.254/16, except that
82 * the first and last 256 addresses are reserved.
84 static void pick(struct in_addr *ip)
88 /* use cheaper math than lrand48() mod N */
90 tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
91 } while (tmp > (IN_CLASSB_HOST - 0x0200));
92 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
96 * Broadcast an ARP packet.
98 static int arp(int fd, struct sockaddr *saddr, int op,
99 const struct ether_addr *source_addr, struct in_addr source_ip,
100 const struct ether_addr *target_addr, struct in_addr target_ip)
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);
110 p.arp.ar_hrd = htons(ARPHRD_ETHER);
111 p.arp.ar_pro = htons(ETHERTYPE_IP);
112 p.arp.ar_hln = ETH_ALEN;
114 p.arp.ar_op = htons(op);
115 memcpy(&p.source_addr, source_addr, ETH_ALEN);
116 memcpy(&p.source_ip, &source_ip, sizeof (p.source_ip));
117 memcpy(&p.target_addr, target_addr, ETH_ALEN);
118 memcpy(&p.target_ip, &target_ip, sizeof (p.target_ip));
121 if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
131 static int run(char *script, char *arg, char *intf, struct in_addr *ip)
136 if (script != NULL) {
137 VDBG("%s run %s %s\n", intf, script, arg);
139 char *addr = inet_ntoa(*ip);
140 setenv("ip", addr, 1);
141 syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
145 if (pid < 0) { // error
148 } else if (pid == 0) { // child
149 execl(script, script, arg, NULL);
154 if (waitpid(pid, &status, 0) <= 0) {
158 if (WEXITSTATUS(status) != 0) {
159 bb_error_msg("script %s failed, exit=%d\n",
160 script, WEXITSTATUS(status));
167 syslog(LOG_ERR, "%s %s, %s error: %s",
168 arg, intf, why, strerror(errno));
174 * Return milliseconds of random delay, up to "secs" seconds.
176 static inline unsigned ms_rdelay(unsigned secs)
178 return lrand48() % (secs * 1000);
185 int zcip_main(int argc, char *argv[])
193 struct sockaddr saddr;
194 struct ether_addr addr;
195 struct in_addr ip = { 0 };
198 suseconds_t timeout = 0; // milliseconds
200 unsigned conflicts = 0;
201 unsigned nprobes = 0;
202 unsigned nclaims = 0;
205 // parse commandline: prog [options] ifname script
206 while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
215 if (inet_aton(optarg, &ip) == 0
216 || (ntohl(ip.s_addr) & IN_CLASSB_NET)
218 bb_error_msg_and_die("invalid link address");
226 bb_error_msg_and_die("bad option");
229 if (optind < argc - 1) {
230 intf = argv[optind++];
231 setenv("interface", intf, 1);
232 script = argv[optind++];
234 if (optind != argc || !intf)
236 openlog(bb_applet_name, 0, LOG_DAEMON);
238 // initialize the interface (modprobe, ifup, etc)
239 if (run(script, "init", intf, NULL) < 0)
243 memset(&saddr, 0, sizeof (saddr));
244 safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
246 // open an ARP socket
247 if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) {
253 // bind to the interface's ARP socket
254 if (bind(fd, &saddr, sizeof (saddr)) < 0) {
259 unsigned short seed[3];
261 // get the interface's ethernet address
262 memset(&ifr, 0, sizeof (ifr));
263 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
264 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
265 why = "get ethernet address";
268 memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
270 // start with some stable ip address, either a function of
271 // the hardware address or else the last address we used.
272 // NOTE: the sequence of addresses we try changes only
273 // depending on when we detect conflicts.
274 memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
280 // FIXME cases to handle:
281 // - zcip already running!
282 // - link already has local address... just defend/update
284 // daemonize now; don't delay system startup
286 if (daemon(0, verbose) < 0) {
290 syslog(LOG_INFO, "start, interface %s", intf);
293 // run the dynamic address negotiation protocol,
294 // restarting after address conflicts:
295 // - start with some address we want to try
296 // - short random delay
297 // - arp probes to see if another host else uses it
298 // - arp announcements that we're claiming it
300 // - defend it, within limits
302 struct pollfd fds[1];
307 fds[0].events = POLLIN;
310 // poll, being ready to adjust current timeout
312 timeout = ms_rdelay(PROBE_WAIT);
313 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
314 // make the kernel filter out all packets except
315 // ones we'd care about.
317 gettimeofday(&tv1, NULL);
318 tv1.tv_usec += (timeout % 1000) * 1000;
319 while (tv1.tv_usec > 1000000) {
320 tv1.tv_usec -= 1000000;
323 tv1.tv_sec += timeout / 1000;
325 VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
326 timeout, intf, nprobes, nclaims);
327 switch (poll(fds, 1, timeout)) {
329 // timeouts trigger protocol transitions
332 if (nprobes < PROBE_NUM) {
334 VDBG("probe/%d %s@%s\n",
335 nprobes, intf, inet_ntoa(ip));
336 (void)arp(fd, &saddr, ARPOP_REQUEST,
339 if (nprobes < PROBE_NUM) {
340 timeout = PROBE_MIN * 1000;
341 timeout += ms_rdelay(PROBE_MAX
344 timeout = ANNOUNCE_WAIT * 1000;
346 // then announcements
347 else if (nclaims < ANNOUNCE_NUM) {
349 VDBG("announce/%d %s@%s\n",
350 nclaims, intf, inet_ntoa(ip));
351 (void)arp(fd, &saddr, ARPOP_REQUEST,
354 if (nclaims < ANNOUNCE_NUM) {
355 timeout = ANNOUNCE_INTERVAL * 1000;
357 // link is ok to use earlier
358 run(script, "config", intf, &ip);
363 // NOTE: all other exit paths
364 // should deconfig ...
367 // FIXME update filters
374 // maybe adjust timeout
378 gettimeofday(&tv2, NULL);
379 if (timercmp(&tv1, &tv2, <)) {
382 timersub(&tv1, &tv2, &tv1);
383 timeout = 1000 * tv1.tv_sec
384 + tv1.tv_usec / 1000;
387 if ((fds[0].revents & POLLIN) == 0) {
388 if (fds[0].revents & POLLERR) {
389 // FIXME: links routinely go down;
390 // this shouldn't necessarily exit.
391 bb_error_msg("%s: poll error\n", intf);
393 run(script, "deconfig",
401 if (recv(fd, &p, sizeof (p), 0) < 0) {
405 if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
408 VDBG("%s recv arp type=%d, op=%d,\n",
409 intf, ntohs(p.hdr.ether_type),
411 VDBG("\tsource=%s %s\n",
412 ether_ntoa(&p.source_addr),
413 inet_ntoa(p.source_ip));
414 VDBG("\ttarget=%s %s\n",
415 ether_ntoa(&p.target_addr),
416 inet_ntoa(p.target_ip));
417 if (p.arp.ar_op != htons(ARPOP_REQUEST)
418 && p.arp.ar_op != htons(ARPOP_REPLY))
421 // some cases are always conflicts
422 if ((p.source_ip.s_addr == ip.s_addr)
423 && (memcmp(&addr, &p.source_addr,
426 VDBG("%s ARP conflict from %s\n", intf,
427 ether_ntoa(&p.source_addr));
429 time_t now = time(0);
431 if ((defend + DEFEND_INTERVAL)
434 (void)arp(fd, &saddr,
438 VDBG("%s defend\n", intf);
444 run(script, "deconfig", intf, &ip);
445 // FIXME rm filters: setsockopt(fd,
446 // SO_DETACH_FILTER, ...)
449 if (conflicts >= MAX_CONFLICTS) {
450 VDBG("%s ratelimit\n", intf);
451 sleep(RATE_LIMIT_INTERVAL);
453 // restart the whole protocol
459 // two hosts probing one address is a collision too
460 else if (p.target_ip.s_addr == ip.s_addr
462 && p.arp.ar_op == htons(ARPOP_REQUEST)
463 && memcmp(&addr, &p.target_addr,
478 syslog(LOG_ERR, "%s %s, %s error: %s",
479 bb_applet_name, intf, why, strerror(errno));