123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- #include <netinet/ether.h>
- #include <net/if.h>
- #include <net/if_arp.h>
- #include <linux/sockios.h>
- #include <syslog.h>
- #define MONOTONIC_US() ((unsigned)monotonic_us())
- struct arp_packet {
- struct ether_header eth;
- struct ether_arp arp;
- } PACKED;
- enum {
-
- PROBE_WAIT = 1,
-
- PROBE_MIN = 1,
- PROBE_MAX = 2,
- PROBE_NUM = 3,
- ANNOUNCE_INTERVAL = 2,
- ANNOUNCE_NUM = 3,
-
- CONFLICT_MULTIPLIER = 2,
-
- DEFEND_INTERVAL = 10,
- };
- enum {
- PROBE = 0,
- ANNOUNCE,
- MONITOR,
- DEFEND
- };
- #define VDBG(...) do { } while (0)
- enum {
- sock_fd = 3
- };
- struct globals {
- struct sockaddr iface_sockaddr;
- struct ether_addr our_ethaddr;
- uint32_t localnet_ip;
- } FIX_ALIASING;
- #define G (*(struct globals*)bb_common_bufsiz1)
- #define INIT_G() do { setup_common_bufsiz(); } while (0)
- static uint32_t pick_nip(void)
- {
- unsigned tmp;
- do {
- tmp = rand() & IN_CLASSB_HOST;
- } while (tmp > (IN_CLASSB_HOST - 0x0200));
- return htonl((G.localnet_ip + 0x0100) + tmp);
- }
- static const char *nip_to_a(uint32_t nip)
- {
- struct in_addr in;
- in.s_addr = nip;
- return inet_ntoa(in);
- }
- static void send_arp_request(
-
-
- uint32_t source_nip,
- const struct ether_addr *target_eth, uint32_t target_nip)
- {
- enum { op = ARPOP_REQUEST };
- #define source_eth (&G.our_ethaddr)
- struct arp_packet p;
- memset(&p, 0, sizeof(p));
-
- p.eth.ether_type = htons(ETHERTYPE_ARP);
- memcpy(p.eth.ether_shost, source_eth, ETH_ALEN);
- memset(p.eth.ether_dhost, 0xff, ETH_ALEN);
-
- p.arp.arp_hrd = htons(ARPHRD_ETHER);
- p.arp.arp_pro = htons(ETHERTYPE_IP);
- p.arp.arp_hln = ETH_ALEN;
- p.arp.arp_pln = 4;
- p.arp.arp_op = htons(op);
- memcpy(&p.arp.arp_sha, source_eth, ETH_ALEN);
- memcpy(&p.arp.arp_spa, &source_nip, 4);
- memcpy(&p.arp.arp_tha, target_eth, ETH_ALEN);
- memcpy(&p.arp.arp_tpa, &target_nip, 4);
-
-
-
-
-
-
-
- xsendto(sock_fd, &p, sizeof(p), &G.iface_sockaddr, sizeof(G.iface_sockaddr));
- #undef source_eth
- }
- static int run(char *argv[3], const char *param, uint32_t nip)
- {
- int status;
- const char *addr = addr;
- const char *fmt = "%s %s %s" + 3;
- char *env_ip = env_ip;
- argv[2] = (char*)param;
- VDBG("%s run %s %s\n", argv[0], argv[1], argv[2]);
- if (nip != 0) {
- addr = nip_to_a(nip);
-
- env_ip = xasprintf("ip=%s", addr);
- putenv(env_ip);
- fmt -= 3;
- }
- bb_error_msg(fmt, argv[2], argv[0], addr);
- status = spawn_and_wait(argv + 1);
- if (nip != 0)
- bb_unsetenv_and_free(env_ip);
- if (status < 0) {
- bb_perror_msg("%s %s %s" + 3, argv[2], argv[0]);
- return -errno;
- }
- if (status != 0)
- bb_error_msg("script %s %s failed, exitcode=%d", argv[1], argv[2], status & 0xff);
- return status;
- }
- static ALWAYS_INLINE unsigned random_delay_ms(unsigned secs)
- {
- return (unsigned)rand() % (secs * 1000);
- }
- int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int zcip_main(int argc UNUSED_PARAM, char **argv)
- {
- char *r_opt;
- const char *l_opt = "169.254.0.0";
- int state;
- int nsent;
- unsigned opts;
-
- struct {
- const struct ether_addr null_ethaddr;
- struct ifreq ifr;
- uint32_t chosen_nip;
- int conflicts;
- int timeout_ms;
- int verbose;
- } L;
- #define null_ethaddr (L.null_ethaddr)
- #define ifr (L.ifr )
- #define chosen_nip (L.chosen_nip )
- #define conflicts (L.conflicts )
- #define timeout_ms (L.timeout_ms )
- #define verbose (L.verbose )
- memset(&L, 0, sizeof(L));
- INIT_G();
- #define FOREGROUND (opts & 1)
- #define QUIT (opts & 2)
-
-
- opts = getopt32(argv, "^" "fqr:l:v" "\0" "=2:vv:vf",
- &r_opt, &l_opt, &verbose
- );
- #if !BB_MMU
-
- if (!FOREGROUND)
- bb_daemonize_or_rexec(0 , argv);
- #endif
-
-
-
- xmove_fd(xsocket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)), sock_fd);
- if (!FOREGROUND) {
-
- openlog(applet_name, 0, LOG_DAEMON);
- logmode |= LOGMODE_SYSLOG;
- }
- bb_logenv_override();
- {
- struct in_addr net;
- if (inet_aton(l_opt, &net) == 0
- || (net.s_addr & htonl(IN_CLASSB_NET)) != net.s_addr
- ) {
- bb_error_msg_and_die("invalid network address");
- }
- G.localnet_ip = ntohl(net.s_addr);
- }
- if (opts & 4) {
- struct in_addr ip;
- if (inet_aton(r_opt, &ip) == 0
- || (ntohl(ip.s_addr) & IN_CLASSB_NET) != G.localnet_ip
- ) {
- bb_error_msg_and_die("invalid link address");
- }
- chosen_nip = ip.s_addr;
- }
- argv += optind - 1;
-
-
- argv[0] = argv[1];
- argv[1] = argv[2];
-
- #define argv_intf (argv[0])
- xsetenv("interface", argv_intf);
-
- if (run(argv, "init", 0))
- return EXIT_FAILURE;
-
-
-
-
- safe_strncpy(G.iface_sockaddr.sa_data, argv_intf, sizeof(G.iface_sockaddr.sa_data));
-
- xbind(sock_fd, &G.iface_sockaddr, sizeof(G.iface_sockaddr));
-
-
- strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
- xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
- memcpy(&G.our_ethaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
-
-
-
-
-
-
- {
- uint32_t t;
- move_from_unaligned32(t, ((char *)&G.our_ethaddr + 2));
- srand(t);
- }
-
-
-
-
- if (!FOREGROUND) {
- #if BB_MMU
- bb_daemonize(0 );
- #endif
- bb_error_msg("start, interface %s", argv_intf);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (chosen_nip == 0) {
- new_nip_and_PROBE:
- chosen_nip = pick_nip();
- }
- nsent = 0;
- state = PROBE;
- while (1) {
- struct pollfd fds[1];
- unsigned deadline_us = deadline_us;
- struct arp_packet p;
- int ip_conflict;
- int n;
- fds[0].fd = sock_fd;
- fds[0].events = POLLIN;
- fds[0].revents = 0;
-
- if (!timeout_ms) {
- timeout_ms = random_delay_ms(PROBE_WAIT);
-
-
-
- }
- if (timeout_ms >= 0) {
-
- deadline_us = MONOTONIC_US() + timeout_ms * 1000;
- }
- VDBG("...wait %d %s nsent=%u\n",
- timeout_ms, argv_intf, nsent);
- n = safe_poll(fds, 1, timeout_ms);
- if (n < 0) {
-
- return EXIT_FAILURE;
- }
- if (n == 0) {
- VDBG("state:%d\n", state);
- switch (state) {
- case PROBE:
-
-
- if (nsent < PROBE_NUM) {
- nsent++;
- VDBG("probe/%u %s@%s\n",
- nsent, argv_intf, nip_to_a(chosen_nip));
- timeout_ms = PROBE_MIN * 1000;
- timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
- send_arp_request(0, &null_ethaddr, chosen_nip);
- continue;
- }
-
- nsent = 0;
- state = ANNOUNCE;
- goto send_announce;
- case ANNOUNCE:
-
-
- if (nsent < ANNOUNCE_NUM) {
- send_announce:
- nsent++;
- VDBG("announce/%u %s@%s\n",
- nsent, argv_intf, nip_to_a(chosen_nip));
- timeout_ms = ANNOUNCE_INTERVAL * 1000;
- send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
- continue;
- }
-
-
- run(argv, "config", chosen_nip);
-
- if (QUIT)
- return EXIT_SUCCESS;
-
- default:
-
-
-
- timeout_ms = -1;
- state = MONITOR;
- continue;
- }
- }
-
-
-
- if (timeout_ms > 0) {
- unsigned diff = deadline_us - MONOTONIC_US();
- if ((int)(diff) < 0) {
-
- diff = 0;
- }
- VDBG("adjusting timeout\n");
- timeout_ms = (diff / 1000) | 1;
- }
- if ((fds[0].revents & POLLIN) == 0) {
- if (fds[0].revents & POLLERR) {
-
-
- bb_error_msg("iface %s is down", argv_intf);
- if (state >= MONITOR) {
-
- run(argv, "deconfig", chosen_nip);
- }
- return EXIT_FAILURE;
- }
- continue;
- }
-
- if (safe_read(sock_fd, &p, sizeof(p)) < 0) {
- bb_perror_msg_and_die(bb_msg_read_error);
- }
- if (p.eth.ether_type != htons(ETHERTYPE_ARP))
- continue;
- if (p.arp.arp_op != htons(ARPOP_REQUEST)
- && p.arp.arp_op != htons(ARPOP_REPLY)
- ) {
- continue;
- }
- #ifdef DEBUG
- {
- struct ether_addr *sha = (struct ether_addr *) p.arp.arp_sha;
- struct ether_addr *tha = (struct ether_addr *) p.arp.arp_tha;
- struct in_addr *spa = (struct in_addr *) p.arp.arp_spa;
- struct in_addr *tpa = (struct in_addr *) p.arp.arp_tpa;
- VDBG("source=%s %s\n", ether_ntoa(sha), inet_ntoa(*spa));
- VDBG("target=%s %s\n", ether_ntoa(tha), inet_ntoa(*tpa));
- }
- #endif
- ip_conflict = 0;
- if (memcmp(&p.arp.arp_sha, &G.our_ethaddr, ETH_ALEN) != 0) {
- if (memcmp(p.arp.arp_spa, &chosen_nip, 4) == 0) {
-
- ip_conflict = 1;
- }
- if (p.arp.arp_op == htons(ARPOP_REQUEST)
- && memcmp(p.arp.arp_spa, &const_int_0, 4) == 0
- && memcmp(p.arp.arp_tpa, &chosen_nip, 4) == 0
- ) {
-
-
- ip_conflict |= 2;
- }
- }
- VDBG("state:%d ip_conflict:%d\n", state, ip_conflict);
- if (!ip_conflict)
- continue;
-
- if (state <= ANNOUNCE) {
-
- conflicts++;
- timeout_ms = PROBE_MIN * 1000
- + CONFLICT_MULTIPLIER * random_delay_ms(conflicts);
- goto new_nip_and_PROBE;
- }
-
- if (ip_conflict & 1) {
- if (state == MONITOR) {
-
- VDBG("monitor conflict - defending\n");
- timeout_ms = DEFEND_INTERVAL * 1000;
- state = DEFEND;
- send_arp_request(chosen_nip, &G.our_ethaddr, chosen_nip);
- continue;
- }
-
-
- VDBG("defend conflict - starting over\n");
- run(argv, "deconfig", chosen_nip);
- conflicts = 0;
- timeout_ms = 0;
- goto new_nip_and_PROBE;
- }
-
-
-
-
-
- }
- #undef argv_intf
- }
|