X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;ds=sidebyside;f=networking%2Fzcip.c;h=eb0a7ba417893e2e5757aad2592d32fd52674a0c;hb=7b72fc12000c878e11d5f0b245f83c0d71b29f58;hp=e781a5882e6bb7c82e01c89a9ae95979602012bc;hpb=60e3dd60160e0a3192fd0f2ed02cf6407ef36ed4;p=oweals%2Fbusybox.git diff --git a/networking/zcip.c b/networking/zcip.c index e781a5882..eb0a7ba41 100644 --- a/networking/zcip.c +++ b/networking/zcip.c @@ -23,15 +23,10 @@ // - avoid silent script failures, especially under load... // - link status monitoring (restart on link-up; stop on link-down) -#include "busybox.h" -#include -#include +#include "libbb.h" #include #include -#include - #include - #include #include #include @@ -72,9 +67,8 @@ enum { DEFEND }; -#define DBG(fmt,args...) \ +#define VDBG(fmt,args...) \ do { } while (0) -#define VDBG DBG /** * Pick a random link local IP address on 169.254/16, except that @@ -82,7 +76,7 @@ enum { */ static void pick(struct in_addr *ip) { - unsigned tmp; + unsigned tmp; /* use cheaper math than lrand48() mod N */ do { @@ -91,10 +85,12 @@ static void pick(struct in_addr *ip) ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp); } +/* TODO: we need a flag to direct bb_[p]error_msg output to stderr. */ + /** * Broadcast an ARP packet. */ -static int arp(int fd, struct sockaddr *saddr, int op, +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) { @@ -113,68 +109,47 @@ static int arp(int fd, struct sockaddr *saddr, int op, 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_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_tpa, &target_ip, sizeof(p.arp.arp_tpa)); // send it - if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { - perror("sendto"); - return -errno; - } - return 0; + xsendto(fd, &p, sizeof(p), saddr, sizeof(*saddr)); + + // Currently all callers ignore errors, that's why returns are + // commented out... + //return 0; } /** - * Run a script. - * TODO: sort out stderr/syslog reporting. + * Run a script. argv[2] is already NULL. */ -static int run(char *script, char *arg, char *intf, struct in_addr *ip) +static int run(char *argv[3], const char *intf, struct in_addr *ip) { - int pid, status; - char *why; - - if (script != NULL) { - VDBG("%s run %s %s\n", intf, script, arg); - if (ip != NULL) { - char *addr = inet_ntoa(*ip); - setenv("ip", addr, 1); - syslog(LOG_INFO, "%s %s %s", arg, intf, addr); - } + int status; - pid = vfork(); - if (pid < 0) { // error - why = "vfork"; - goto bad; - } else if (pid == 0) { // child - execl(script, script, arg, NULL); - perror("execl"); - _exit(EXIT_FAILURE); - } + VDBG("%s run %s %s\n", intf, argv[0], argv[1]); - if (waitpid(pid, &status, 0) <= 0) { - why = "waitpid"; - goto bad; - } - if (WEXITSTATUS(status) != 0) { - bb_error_msg("script %s failed, exit=%d\n", - script, WEXITSTATUS(status)); - return -errno; - } + if (ip) { + char *addr = inet_ntoa(*ip); + setenv("ip", addr, 1); + bb_info_msg("%s %s %s", argv[1], intf, addr); } - return 0; -bad: - status = -errno; - syslog(LOG_ERR, "%s %s, %s error: %s", - arg, intf, why, strerror(errno)); + + status = wait4pid(spawn(argv)); + if (status < 0) { + bb_perror_msg("%s %s", argv[1], intf); + return -errno; + } + if (status != 0) + bb_error_msg("script %s %s failed, exitcode=%d", argv[0], argv[1], status); return status; } - /** * Return milliseconds of random delay, up to "secs" seconds. */ -static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs) +static unsigned ALWAYS_INLINE ms_rdelay(unsigned secs) { return lrand48() % (secs * 1000); } @@ -182,90 +157,97 @@ static unsigned ATTRIBUTE_ALWAYS_INLINE ms_rdelay(unsigned secs) /** * main program */ - -/* 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} }; - -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 quit; // = 0; -static int foreground; // = 0; -static int verbose; // = 0; -static int state = PROBE; - -int zcip_main(int argc, char *argv[]) +int zcip_main(int argc, char **argv); +int zcip_main(int argc, char **argv) { + int state = PROBE; struct ether_addr eth_addr; - char *why; + const char *why; int fd; - int t; - + char *r_opt; + unsigned opts; + + /* Ugly trick, but I want these zeroed in one go */ + struct { + const struct in_addr null_ip; + const struct ether_addr null_addr; + struct sockaddr saddr; + struct in_addr ip; + struct ifreq ifr; + char *intf; + char *script_av[3]; + suseconds_t timeout; // milliseconds + unsigned conflicts; + unsigned nprobes; + unsigned nclaims; + int ready; + int verbose; + } L; +#define null_ip (L.null_ip ) +#define null_addr (L.null_addr) +#define saddr (L.saddr ) +#define ip (L.ip ) +#define ifr (L.ifr ) +#define intf (L.intf ) +#define script_av (L.script_av) +#define timeout (L.timeout ) +#define conflicts (L.conflicts) +#define nprobes (L.nprobes ) +#define nclaims (L.nclaims ) +#define ready (L.ready ) +#define verbose (L.verbose ) + + memset(&L, 0, sizeof(L)); + +#define FOREGROUND (opts & 1) +#define QUIT (opts & 2) // parse commandline: prog [options] ifname script - while ((t = getopt(argc, argv, "fqr:v")) != EOF) { - switch (t) { - case 'f': - foreground = 1; - continue; - case 'q': - quit = 1; - continue; - case 'r': - if (inet_aton(optarg, &ip) == 0 - || (ntohl(ip.s_addr) & IN_CLASSB_NET) - != LINKLOCAL_ADDR) { - bb_error_msg_and_die("invalid link address"); - } - continue; - case 'v': - verbose++; - foreground = 1; - continue; - default: - bb_error_msg_and_die("bad option"); - } + // exactly 2 args; -v accumulates and implies -f + opt_complementary = "=2:vv:vf"; + opts = getopt32(argc, argv, "fqr:v", &r_opt, &verbose); + if (!FOREGROUND) { + /* Do it early, before all bb_xx_msg calls */ + openlog(applet_name, 0, LOG_DAEMON); + logmode |= LOGMODE_SYSLOG; } - if (optind < argc - 1) { - intf = argv[optind++]; - setenv("interface", intf, 1); - script = argv[optind++]; + if (opts & 4) { // -r n.n.n.n + if (inet_aton(r_opt, &ip) == 0 + || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR + ) { + bb_error_msg_and_die("invalid link address"); + } } - if (optind != argc || !intf) - bb_show_usage(); - openlog(bb_applet_name, 0, LOG_DAEMON); + // On NOMMU reexec early (or else we will rerun things twice) +#if !BB_MMU + if (!FOREGROUND) + bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); +#endif + argc -= optind; + argv += optind; + + intf = argv[0]; + script_av[0] = argv[1]; + setenv("interface", intf, 1); // initialize the interface (modprobe, ifup, etc) - if (run(script, "init", intf, NULL) < 0) + script_av[1] = (char*)"init"; + if (run(script_av, intf, NULL)) return EXIT_FAILURE; // initialize saddr - //memset(&saddr, 0, sizeof (saddr)); - safe_strncpy(saddr.sa_data, intf, sizeof (saddr.sa_data)); + //memset(&saddr, 0, sizeof(saddr)); + safe_strncpy(saddr.sa_data, 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(fd, &saddr, sizeof(saddr)); // get the interface's ethernet address - //memset(&ifr, 0, sizeof (ifr)); - strncpy(ifr.ifr_name, intf, sizeof (ifr.ifr_name)); + //memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, intf, sizeof(ifr.ifr_name)); if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { - foreground = 1; - why = "get ethernet address"; - goto bad; + bb_perror_msg_and_die("get ethernet address"); } memcpy(ð_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); @@ -283,9 +265,11 @@ int zcip_main(int argc, char *argv[]) // - link already has local address... just defend/update // daemonize now; don't delay system startup - if (!foreground) { - xdaemon(0, verbose); - syslog(LOG_INFO, "start, interface %s", intf); + if (!FOREGROUND) { +#if BB_MMU + bb_daemonize(DAEMON_CHDIR_ROOT); +#endif + bb_info_msg("start, interface %s", intf); } // run the dynamic address negotiation protocol, @@ -323,7 +307,7 @@ int zcip_main(int argc, char *argv[]) tv1.tv_sec++; } tv1.tv_sec += timeout / 1000; - + VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n", timeout, intf, nprobes, nclaims); switch (poll(fds, 1, timeout)) { @@ -333,13 +317,13 @@ int zcip_main(int argc, char *argv[]) VDBG("state = %d\n", state); switch (state) { case PROBE: - // timeouts in the PROBE state means no conflicting ARP packets + // timeouts in the PROBE state mean no conflicting ARP packets // have been received, so we can progress through the states if (nprobes < PROBE_NUM) { nprobes++; VDBG("probe/%d %s@%s\n", nprobes, intf, inet_ntoa(ip)); - (void)arp(fd, &saddr, ARPOP_REQUEST, + arp(fd, &saddr, ARPOP_REQUEST, ð_addr, null_ip, &null_addr, ip); timeout = PROBE_MIN * 1000; @@ -352,32 +336,32 @@ int zcip_main(int argc, char *argv[]) nclaims = 0; VDBG("announce/%d %s@%s\n", nclaims, intf, inet_ntoa(ip)); - (void)arp(fd, &saddr, ARPOP_REQUEST, + arp(fd, &saddr, ARPOP_REQUEST, ð_addr, ip, ð_addr, ip); timeout = ANNOUNCE_INTERVAL * 1000; } break; case RATE_LIMIT_PROBE: - // timeouts in the RATE_LIMIT_PROBE state means no conflicting ARP packets + // timeouts in the RATE_LIMIT_PROBE state mean no conflicting ARP packets // have been received, so we can move immediately to the announce state state = ANNOUNCE; nclaims = 0; VDBG("announce/%d %s@%s\n", nclaims, intf, inet_ntoa(ip)); - (void)arp(fd, &saddr, ARPOP_REQUEST, + arp(fd, &saddr, ARPOP_REQUEST, ð_addr, ip, ð_addr, ip); timeout = ANNOUNCE_INTERVAL * 1000; break; case ANNOUNCE: - // timeouts in the ANNOUNCE state means no conflicting ARP packets + // timeouts in the ANNOUNCE state mean no conflicting ARP packets // have been received, so we can progress through the states if (nclaims < ANNOUNCE_NUM) { nclaims++; VDBG("announce/%d %s@%s\n", nclaims, intf, inet_ntoa(ip)); - (void)arp(fd, &saddr, ARPOP_REQUEST, + arp(fd, &saddr, ARPOP_REQUEST, ð_addr, ip, ð_addr, ip); timeout = ANNOUNCE_INTERVAL * 1000; @@ -387,14 +371,15 @@ int zcip_main(int argc, char *argv[]) state = MONITOR; // link is ok to use earlier // FIXME update filters - run(script, "config", intf, &ip); + script_av[1] = (char*)"config"; + run(script_av, intf, &ip); ready = 1; conflicts = 0; timeout = -1; // Never timeout in the monitor state. - // NOTE: all other exit paths + // NOTE: all other exit paths // should deconfig ... - if (quit) + if (QUIT) return EXIT_SUCCESS; } break; @@ -439,10 +424,10 @@ int zcip_main(int argc, char *argv[]) if (fds[0].revents & POLLERR) { // FIXME: links routinely go down; // this shouldn't necessarily exit. - bb_error_msg("%s: poll error\n", intf); + bb_error_msg("%s: poll error", intf); if (ready) { - run(script, "deconfig", - intf, &ip); + script_av[1] = (char*)"deconfig"; + run(script_av, intf, &ip); } return EXIT_FAILURE; } @@ -450,7 +435,7 @@ int zcip_main(int argc, char *argv[]) } // read ARP packet - if (recv(fd, &p, sizeof (p), 0) < 0) { + if (recv(fd, &p, sizeof(p), 0) < 0) { why = "recv"; goto bad; } @@ -488,7 +473,7 @@ int zcip_main(int argc, char *argv[]) target_ip_conflict = 1; } - VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n", + VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n", state, source_ip_conflict, target_ip_conflict); switch (state) { case PROBE: @@ -516,7 +501,7 @@ int zcip_main(int argc, char *argv[]) VDBG("monitor conflict -- defending\n"); state = DEFEND; timeout = DEFEND_INTERVAL * 1000; - (void)arp(fd, &saddr, + arp(fd, &saddr, ARPOP_REQUEST, ð_addr, ip, ð_addr, ip); @@ -528,7 +513,8 @@ int zcip_main(int argc, char *argv[]) state = PROBE; VDBG("defend conflict -- starting over\n"); ready = 0; - run(script, "deconfig", intf, &ip); + script_av[1] = (char*)"deconfig"; + run(script_av, intf, &ip); // restart the whole protocol pick(&ip); @@ -554,11 +540,7 @@ int zcip_main(int argc, char *argv[]) goto bad; } // switch poll } -bad: - if (foreground) - perror(why); - else - syslog(LOG_ERR, "%s %s, %s error: %s", - bb_applet_name, intf, why, strerror(errno)); + bad: + bb_perror_msg("%s, %s", intf, why); return EXIT_FAILURE; }