--- trunk/mkinitrd-magellan/busybox/networking/zcip.c 2009/04/24 18:32:46 815 +++ trunk/mkinitrd-magellan/busybox/networking/zcip.c 2009/04/24 18:33:46 816 @@ -23,23 +23,23 @@ // - avoid silent script failures, especially under load... // - link status monitoring (restart on link-up; stop on link-down) -#include "busybox.h" -#include -#include -#include #include #include #include #include - #include #include +#include "libbb.h" +#include + +/* We don't need more than 32 bits of the counter */ +#define MONOTONIC_US() ((unsigned)monotonic_us()) struct arp_packet { - struct ether_header hdr; + struct ether_header eth; struct ether_arp arp; -} ATTRIBUTE_PACKED; +} PACKED; enum { /* 169.254.0.0 */ @@ -67,44 +67,55 @@ DEFEND }; -#define VDBG(fmt,args...) \ - do { } while (0) +#define VDBG(...) do { } while (0) + + +enum { + sock_fd = 3 +}; + +struct globals { + struct sockaddr saddr; + struct ether_addr eth_addr; +}; +#define G (*(struct globals*)&bb_common_bufsiz1) +#define saddr (G.saddr ) +#define eth_addr (G.eth_addr) -static unsigned opts; -#define FOREGROUND (opts & 1) -#define QUIT (opts & 2) /** * Pick a random link local IP address on 169.254/16, except that * the first and last 256 addresses are reserved. */ -static void pick(struct in_addr *ip) +static uint32_t pick(void) { unsigned tmp; - /* use cheaper math than lrand48() mod N */ do { - tmp = (lrand48() >> 16) & IN_CLASSB_HOST; + tmp = rand() & IN_CLASSB_HOST; } while (tmp > (IN_CLASSB_HOST - 0x0200)); - ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp); + return htonl((LINKLOCAL_ADDR + 0x0100) + tmp); } -/* TODO: we need a flag to direct bb_[p]error_msg output to stderr. */ - /** * Broadcast an ARP packet. */ -static void arp(int fd, struct sockaddr *saddr, int op, - const struct ether_addr *source_addr, struct in_addr source_ip, - const struct ether_addr *target_addr, struct in_addr target_ip) +static void arp( + /* int op, - always ARPOP_REQUEST */ + /* const struct ether_addr *source_eth, - always ð_addr */ + struct in_addr source_ip, + const struct ether_addr *target_eth, struct in_addr target_ip) { + enum { op = ARPOP_REQUEST }; +#define source_eth (ð_addr) + struct arp_packet p; memset(&p, 0, sizeof(p)); // ether header - p.hdr.ether_type = htons(ETHERTYPE_ARP); - memcpy(p.hdr.ether_shost, source_addr, ETH_ALEN); - memset(p.hdr.ether_dhost, 0xff, ETH_ALEN); + p.eth.ether_type = htons(ETHERTYPE_ARP); + memcpy(p.eth.ether_shost, source_eth, ETH_ALEN); + memset(p.eth.ether_dhost, 0xff, ETH_ALEN); // arp request p.arp.arp_hrd = htons(ARPHRD_ETHER); @@ -112,112 +123,116 @@ p.arp.arp_hln = ETH_ALEN; p.arp.arp_pln = 4; p.arp.arp_op = htons(op); - memcpy(&p.arp.arp_sha, source_addr, ETH_ALEN); - memcpy(&p.arp.arp_spa, &source_ip, sizeof (p.arp.arp_spa)); - memcpy(&p.arp.arp_tha, target_addr, ETH_ALEN); - memcpy(&p.arp.arp_tpa, &target_ip, sizeof (p.arp.arp_tpa)); + memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN); + memcpy(&p.arp.arp_spa, &source_ip, sizeof(p.arp.arp_spa)); + memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN); + memcpy(&p.arp.arp_tpa, &target_ip, sizeof(p.arp.arp_tpa)); // send it - if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { - bb_perror_msg("sendto"); - //return -errno; - } - // Currently all callers ignore errors, that's why returns are - // commented out... - //return 0; + // Even though sock_fd is already bound to saddr, just send() + // won't work, because "socket is not connected" + // (and connect() won't fix that, "operation not supported"). + // Thus we sendto() to saddr. I wonder which sockaddr + // (from bind() or from sendto()?) kernel actually uses + // to determine iface to emit the packet from... + xsendto(sock_fd, &p, sizeof(p), &saddr, sizeof(saddr)); +#undef source_eth } /** * Run a script. + * argv[0]:intf argv[1]:script_name argv[2]:junk argv[3]:NULL */ -static int run(char *script, char *arg, char *intf, struct in_addr *ip) +static int run(char *argv[3], const char *param, struct in_addr *ip) { - int pid, status; - char *why; - - if(1) { //always true: if (script != NULL) - VDBG("%s run %s %s\n", intf, script, arg); - if (ip != NULL) { - char *addr = inet_ntoa(*ip); - setenv("ip", addr, 1); - bb_info_msg("%s %s %s", arg, intf, addr); - } - - pid = vfork(); - if (pid < 0) { // error - why = "vfork"; - goto bad; - } else if (pid == 0) { // child - execl(script, script, arg, NULL); - bb_perror_msg("execl"); - _exit(EXIT_FAILURE); - } + int status; + char *addr = addr; /* for gcc */ + const char *fmt = "%s %s %s" + 3; + + argv[2] = (char*)param; + + VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]); + + if (ip) { + addr = inet_ntoa(*ip); + xsetenv("ip", addr); + fmt -= 3; + } + bb_info_msg(fmt, argv[2], argv[0], addr); - if (waitpid(pid, &status, 0) <= 0) { - why = "waitpid"; - goto bad; - } - if (WEXITSTATUS(status) != 0) { - bb_error_msg("script %s failed, exit=%d", - script, WEXITSTATUS(status)); - return -errno; - } + status = wait4pid(spawn(argv + 1)); + if (status < 0) { + bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]); + return -errno; } - return 0; -bad: - status = -errno; - bb_perror_msg("%s %s, %s", arg, intf, why); + if (status != 0) + bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status); return status; } - /** * Return milliseconds of random delay, up to "secs" seconds. */ -static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs) +static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs) { - return lrand48() % (secs * 1000); + return rand() % (secs * 1000); } /** * main program */ +int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int zcip_main(int argc, char **argv) +{ + int state; + char *r_opt; + unsigned opts; -/* Used to be auto variables on main() stack, but - * most of them were zero-inited. Moving them to bss - * is more space-efficient. - */ -static const struct in_addr null_ip; // = { 0 }; -static const struct ether_addr null_addr; // = { {0, 0, 0, 0, 0, 0} }; + // ugly trick, but I want these zeroed in one go + struct { + const struct in_addr null_ip; + const struct ether_addr null_addr; + struct in_addr ip; + struct ifreq ifr; + int timeout_ms; /* must be signed */ + unsigned conflicts; + unsigned nprobes; + unsigned nclaims; + int ready; + int verbose; + } L; +#define null_ip (L.null_ip ) +#define null_addr (L.null_addr ) +#define ip (L.ip ) +#define ifr (L.ifr ) +#define timeout_ms (L.timeout_ms) +#define conflicts (L.conflicts ) +#define nprobes (L.nprobes ) +#define nclaims (L.nclaims ) +#define ready (L.ready ) +#define verbose (L.verbose ) -static struct sockaddr saddr; // memset(0); -static struct in_addr ip; // = { 0 }; -static struct ifreq ifr; //memset(0); - -static char *intf; // = NULL; -static char *script; // = NULL; -static suseconds_t timeout; // = 0; // milliseconds -static unsigned conflicts; // = 0; -static unsigned nprobes; // = 0; -static unsigned nclaims; // = 0; -static int ready; // = 0; -static int verbose; // = 0; -static int state = PROBE; - -int zcip_main(int argc, char *argv[]) -{ - struct ether_addr eth_addr; - char *why; - int fd; + memset(&L, 0, sizeof(L)); +#define FOREGROUND (opts & 1) +#define QUIT (opts & 2) // parse commandline: prog [options] ifname script - char *r_opt; - opt_complementary = "vv:vf"; // -v accumulates and implies -f - opts = getopt32(argc, argv, "fqr:v", &r_opt, &verbose); + // exactly 2 args; -v accumulates and implies -f + opt_complementary = "=2:vv:vf"; + opts = getopt32(argv, "fqr:v", &r_opt, &verbose); +#if !BB_MMU + // on NOMMU reexec early (or else we will rerun things twice) + if (!FOREGROUND) + bb_daemonize_or_rexec(0 /*was: DAEMON_CHDIR_ROOT*/, argv); +#endif + // open an ARP socket + // (need to do it before openlog to prevent openlog from taking + // fd 3 (sock_fd==3)) + xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd); if (!FOREGROUND) { - /* Do it early, before all bb_xx_msg calls */ - logmode = LOGMODE_SYSLOG; + // do it before all bb_xx_msg calls openlog(applet_name, 0, LOG_DAEMON); + logmode |= LOGMODE_SYSLOG; } if (opts & 4) { // -r n.n.n.n if (inet_aton(r_opt, &ip) == 0 @@ -227,42 +242,48 @@ } } argc -= optind; - argv += optind; - if (argc != 2) - bb_show_usage(); - intf = argv[0]; - script = argv[1]; - setenv("interface", intf, 1); + argv += optind - 1; + + /* Now: argv[0]:junk argv[1]:intf argv[2]:script argv[3]:NULL */ + /* We need to make space for script argument: */ + argv[0] = argv[1]; + argv[1] = argv[2]; + /* Now: argv[0]:intf argv[1]:script argv[2]:junk argv[3]:NULL */ +#define argv_intf (argv[0]) + + xsetenv("interface", argv_intf); // initialize the interface (modprobe, ifup, etc) - if (run(script, "init", intf, NULL) < 0) + if (run(argv, "init", NULL)) return EXIT_FAILURE; // initialize saddr - //memset(&saddr, 0, sizeof (saddr)); - safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data)); + // saddr is: { u16 sa_family; u8 sa_data[14]; } + //memset(&saddr, 0, sizeof(saddr)); + //TODO: are we leaving sa_family == 0 (AF_UNSPEC)?! + safe_strncpy(saddr.sa_data, argv_intf, sizeof(saddr.sa_data)); - // open an ARP socket - fd = xsocket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); // bind to the interface's ARP socket - xbind(fd, &saddr, sizeof (saddr)); + xbind(sock_fd, &saddr, sizeof(saddr)); // get the interface's ethernet address - //memset(&ifr, 0, sizeof (ifr)); - strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name)); - if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { - bb_perror_msg_and_die("get ethernet address"); - } + //memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, argv_intf, sizeof(ifr.ifr_name)); + xioctl(sock_fd, SIOCGIFHWADDR, &ifr); memcpy(ð_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); // start with some stable ip address, either a function of // the hardware address or else the last address we used. + // we are taking low-order four bytes, as top-order ones + // aren't random enough. // NOTE: the sequence of addresses we try changes only // depending on when we detect conflicts. - // (SVID 3 bogon: who says that "short" is always 16 bits?) - seed48( (unsigned short*)&ifr.ifr_hwaddr.sa_data ); + { + uint32_t t = get_unaligned_u32p((uint32_t *) ((char *)ð_addr + 2)); + srand(t); + } if (ip.s_addr == 0) - pick(&ip); + ip.s_addr = pick(); // FIXME cases to handle: // - zcip already running! @@ -270,50 +291,57 @@ // daemonize now; don't delay system startup if (!FOREGROUND) { - setsid(); - bb_daemonize(); - bb_info_msg("start, interface %s", intf); +#if BB_MMU + bb_daemonize(0 /*was: DAEMON_CHDIR_ROOT*/); +#endif + bb_info_msg("start, interface %s", argv_intf); } // run the dynamic address negotiation protocol, // restarting after address conflicts: // - start with some address we want to try // - short random delay - // - arp probes to see if another host else uses it + // - arp probes to see if another host uses it // - arp announcements that we're claiming it // - use it // - defend it, within limits + // exit if: + // - address is successfully obtained and -q was given: + // run "