2 * RFC3927 ZeroConf IPv4 Link-Local addressing
3 * (see <http://www.zeroconf.org/>)
5 * Copyright (C) 2003 by Arthur van Hoff (avh@strangeberry.com)
6 * Copyright (C) 2004 by David Brownell
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
12 * ZCIP just manages the 169.254.*.* addresses. That network is not
13 * routed at the IP level, though various proxies or bridges can
14 * certainly be used. Its naming is built over multicast DNS.
20 // - more real-world usage/testing, especially daemon mode
21 // - kernel packet filters to reduce scheduling noise
22 // - avoid silent script failures, especially under load...
23 // - link status monitoring (restart on link-up; stop on link-down)
34 #include <netinet/ether.h>
35 #include <net/ethernet.h>
37 #include <net/if_arp.h>
39 #include <linux/if_packet.h>
40 #include <linux/sockios.h>
44 struct ether_header hdr;
45 // FIXME this part is netinet/if_ether.h "struct ether_arp"
47 struct ether_addr source_addr;
48 struct in_addr source_ip;
49 struct ether_addr target_addr;
50 struct in_addr target_ip;
55 LINKLOCAL_ADDR = 0xa9fe0000,
57 /* protocol timeout parameters, specified in seconds */
63 RATE_LIMIT_INTERVAL = 60,
66 ANNOUNCE_INTERVAL = 2,
70 static const struct in_addr null_ip = { 0 };
71 static const struct ether_addr null_addr = { {0, 0, 0, 0, 0, 0} };
73 static int verbose = 0;
75 #define DBG(fmt,args...) \
80 * Pick a random link local IP address on 169.254/16, except that
81 * the first and last 256 addresses are reserved.
83 static void pick(struct in_addr *ip)
87 /* use cheaper math than lrand48() mod N */
89 tmp = (lrand48() >> 16) & IN_CLASSB_HOST;
90 } while (tmp > (IN_CLASSB_HOST - 0x0200));
91 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
95 * Broadcast an ARP packet.
97 static int arp(int fd, struct sockaddr *saddr, int op,
98 const struct ether_addr *source_addr, struct in_addr source_ip,
99 const struct ether_addr *target_addr, struct in_addr target_ip)
104 p.hdr.ether_type = htons(ETHERTYPE_ARP);
105 memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN);
106 memset(p.hdr.ether_dhost, 0xff, ETH_ALEN);
109 p.arp.ar_hrd = htons(ARPHRD_ETHER);
110 p.arp.ar_pro = htons(ETHERTYPE_IP);
111 p.arp.ar_hln = ETH_ALEN;
113 p.arp.ar_op = htons(op);
114 memcpy(&p.source_addr, source_addr, ETH_ALEN);
115 memcpy(&p.source_ip, &source_ip, sizeof (p.source_ip));
116 memcpy(&p.target_addr, target_addr, ETH_ALEN);
117 memcpy(&p.target_ip, &target_ip, sizeof (p.target_ip));
120 if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
130 static int run(char *script, char *arg, char *intf, struct in_addr *ip)
135 if (script != NULL) {
136 VDBG("%s run %s %s\n", intf, script, arg);
138 char *addr = inet_ntoa(*ip);
139 setenv("ip", addr, 1);
140 syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
144 if (pid < 0) { // error
147 } else if (pid == 0) { // child
148 execl(script, script, arg, NULL);
153 if (waitpid(pid, &status, 0) <= 0) {
157 if (WEXITSTATUS(status) != 0) {
158 bb_error_msg("script %s failed, exit=%d\n",
159 script, WEXITSTATUS(status));
166 syslog(LOG_ERR, "%s %s, %s error: %s",
167 arg, intf, why, strerror(errno));
173 * Return milliseconds of random delay, up to "secs" seconds.
175 static inline unsigned ms_rdelay(unsigned secs)
177 return lrand48() % (secs * 1000);
184 int zcip_main(int argc, char *argv[])
192 struct sockaddr saddr;
193 struct ether_addr addr;
194 struct in_addr ip = { 0 };
197 suseconds_t timeout = 0; // milliseconds
199 unsigned conflicts = 0;
200 unsigned nprobes = 0;
201 unsigned nclaims = 0;
204 // parse commandline: prog [options] ifname script
205 while ((t = getopt(argc, argv, "fqr:v")) != EOF) {
214 if (inet_aton(optarg, &ip) == 0
215 || (ntohl(ip.s_addr) & IN_CLASSB_NET)
217 bb_error_msg_and_die("invalid link address");
225 bb_error_msg_and_die("bad option");
228 if (optind < argc - 1) {
229 intf = argv[optind++];
230 setenv("interface", intf, 1);
231 script = argv[optind++];
233 if (optind != argc || !intf)
235 openlog(bb_applet_name, 0, LOG_DAEMON);
237 // initialize the interface (modprobe, ifup, etc)
238 if (run(script, "init", intf, NULL) < 0)
242 memset(&saddr, 0, sizeof (saddr));
243 safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data));
245 // open an ARP socket
246 if ((fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) < 0) {
252 // bind to the interface's ARP socket
253 if (bind(fd, &saddr, sizeof (saddr)) < 0) {
258 unsigned short seed[3];
260 // get the interface's ethernet address
261 memset(&ifr, 0, sizeof (ifr));
262 strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name));
263 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
264 why = "get ethernet address";
267 memcpy(&addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
269 // start with some stable ip address, either a function of
270 // the hardware address or else the last address we used.
271 // NOTE: the sequence of addresses we try changes only
272 // depending on when we detect conflicts.
273 memcpy(seed, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
279 // FIXME cases to handle:
280 // - zcip already running!
281 // - link already has local address... just defend/update
283 // daemonize now; don't delay system startup
285 if (daemon(0, verbose) < 0) {
289 syslog(LOG_INFO, "start, interface %s", intf);
292 // run the dynamic address negotiation protocol,
293 // restarting after address conflicts:
294 // - start with some address we want to try
295 // - short random delay
296 // - arp probes to see if another host else uses it
297 // - arp announcements that we're claiming it
299 // - defend it, within limits
301 struct pollfd fds[1];
306 fds[0].events = POLLIN;
309 // poll, being ready to adjust current timeout
311 timeout = ms_rdelay(PROBE_WAIT);
312 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
313 // make the kernel filter out all packets except
314 // ones we'd care about.
316 gettimeofday(&tv1, NULL);
317 tv1.tv_usec += (timeout % 1000) * 1000;
318 while (tv1.tv_usec > 1000000) {
319 tv1.tv_usec -= 1000000;
322 tv1.tv_sec += timeout / 1000;
324 VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n",
325 timeout, intf, nprobes, nclaims);
326 switch (poll(fds, 1, timeout)) {
328 // timeouts trigger protocol transitions
331 if (nprobes < PROBE_NUM) {
333 VDBG("probe/%d %s@%s\n",
334 nprobes, intf, inet_ntoa(ip));
335 (void)arp(fd, &saddr, ARPOP_REQUEST,
338 if (nprobes < PROBE_NUM) {
339 timeout = PROBE_MIN * 1000;
340 timeout += ms_rdelay(PROBE_MAX
343 timeout = ANNOUNCE_WAIT * 1000;
345 // then announcements
346 else if (nclaims < ANNOUNCE_NUM) {
348 VDBG("announce/%d %s@%s\n",
349 nclaims, intf, inet_ntoa(ip));
350 (void)arp(fd, &saddr, ARPOP_REQUEST,
353 if (nclaims < ANNOUNCE_NUM) {
354 timeout = ANNOUNCE_INTERVAL * 1000;
356 // link is ok to use earlier
357 run(script, "config", intf, &ip);
362 // NOTE: all other exit paths
363 // should deconfig ...
366 // FIXME update filters
373 // maybe adjust timeout
377 gettimeofday(&tv2, NULL);
378 if (timercmp(&tv1, &tv2, <)) {
381 timersub(&tv1, &tv2, &tv1);
382 timeout = 1000 * tv1.tv_sec
383 + tv1.tv_usec / 1000;
386 if ((fds[0].revents & POLLIN) == 0) {
387 if (fds[0].revents & POLLERR) {
388 // FIXME: links routinely go down;
389 // this shouldn't necessarily exit.
390 bb_error_msg("%s: poll error\n", intf);
392 run(script, "deconfig",
400 if (recv(fd, &p, sizeof (p), 0) < 0) {
404 if (p.hdr.ether_type != htons(ETHERTYPE_ARP))
407 VDBG("%s recv arp type=%d, op=%d,\n",
408 intf, ntohs(p.hdr.ether_type),
410 VDBG("\tsource=%s %s\n",
411 ether_ntoa(&p.source_addr),
412 inet_ntoa(p.source_ip));
413 VDBG("\ttarget=%s %s\n",
414 ether_ntoa(&p.target_addr),
415 inet_ntoa(p.target_ip));
416 if (p.arp.ar_op != htons(ARPOP_REQUEST)
417 && p.arp.ar_op != htons(ARPOP_REPLY))
420 // some cases are always conflicts
421 if ((p.source_ip.s_addr == ip.s_addr)
422 && (memcmp(&addr, &p.source_addr,
425 VDBG("%s ARP conflict from %s\n", intf,
426 ether_ntoa(&p.source_addr));
428 time_t now = time(0);
430 if ((defend + DEFEND_INTERVAL)
433 (void)arp(fd, &saddr,
437 VDBG("%s defend\n", intf);
443 run(script, "deconfig", intf, &ip);
444 // FIXME rm filters: setsockopt(fd,
445 // SO_DETACH_FILTER, ...)
448 if (conflicts >= MAX_CONFLICTS) {
449 VDBG("%s ratelimit\n", intf);
450 sleep(RATE_LIMIT_INTERVAL);
452 // restart the whole protocol
458 // two hosts probing one address is a collision too
459 else if (p.target_ip.s_addr == ip.s_addr
461 && p.arp.ar_op == htons(ARPOP_REQUEST)
462 && memcmp(&addr, &p.target_addr,
477 syslog(LOG_ERR, "%s %s, %s error: %s",
478 bb_applet_name, intf, why, strerror(errno));