123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- #include "libbb.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 {
- LINKLOCAL_ADDR = 0xa9fe0000,
- PROBE_WAIT = 1,
- PROBE_MIN = 1,
- PROBE_MAX = 2,
- PROBE_NUM = 3,
- MAX_CONFLICTS = 10,
- RATE_LIMIT_INTERVAL = 60,
- ANNOUNCE_WAIT = 2,
- ANNOUNCE_NUM = 2,
- ANNOUNCE_INTERVAL = 2,
- DEFEND_INTERVAL = 10
- };
- enum {
- PROBE = 0,
- RATE_LIMIT_PROBE,
- ANNOUNCE,
- MONITOR,
- DEFEND
- };
- #define VDBG(...) do { } while (0)
- enum {
- sock_fd = 3
- };
- struct globals {
- struct sockaddr saddr;
- struct ether_addr eth_addr;
- } FIX_ALIASING;
- #define G (*(struct globals*)&bb_common_bufsiz1)
- #define saddr (G.saddr )
- #define eth_addr (G.eth_addr)
- #define INIT_G() do { } while (0)
- static uint32_t pick(void)
- {
- unsigned tmp;
- do {
- tmp = rand() & IN_CLASSB_HOST;
- } while (tmp > (IN_CLASSB_HOST - 0x0200));
- return htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
- }
- static void arp(
-
-
- 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));
-
- 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_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));
-
-
-
-
-
-
-
- xsendto(sock_fd, &p, sizeof(p), &saddr, sizeof(saddr));
- #undef source_eth
- }
- static int run(char *argv[3], const char *param, struct in_addr *ip)
- {
- int status;
- char *addr = addr;
- 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);
- status = spawn_and_wait(argv + 1);
- 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 rand() % (secs * 1000);
- }
- int zcip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int zcip_main(int argc UNUSED_PARAM, char **argv)
- {
- int state;
- char *r_opt;
- unsigned opts;
-
- struct {
- const struct in_addr null_ip;
- const struct ether_addr null_addr;
- struct in_addr ip;
- struct ifreq ifr;
- int timeout_ms;
- 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 )
- memset(&L, 0, sizeof(L));
- INIT_G();
- #define FOREGROUND (opts & 1)
- #define QUIT (opts & 2)
-
-
- opt_complementary = "=2:vv:vf";
- opts = getopt32(argv, "fqr:v", &r_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;
- }
- if (opts & 4) {
- if (inet_aton(r_opt, &ip) == 0
- || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR
- ) {
- bb_error_msg_and_die("invalid link address");
- }
- }
- argv += optind - 1;
-
-
- argv[0] = argv[1];
- argv[1] = argv[2];
-
- #define argv_intf (argv[0])
- xsetenv("interface", argv_intf);
-
- if (run(argv, "init", NULL))
- return EXIT_FAILURE;
-
-
-
-
- safe_strncpy(saddr.sa_data, argv_intf, sizeof(saddr.sa_data));
-
- xbind(sock_fd, &saddr, sizeof(saddr));
-
-
- strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
- xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
- memcpy(ð_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
-
-
-
-
-
-
- {
- uint32_t t;
- move_from_unaligned32(t, ((char *)ð_addr + 2));
- srand(t);
- }
- if (ip.s_addr == 0)
- ip.s_addr = pick();
-
-
-
-
- if (!FOREGROUND) {
- #if BB_MMU
- bb_daemonize(0 );
- #endif
- bb_info_msg("start, interface %s", argv_intf);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- state = PROBE;
- while (1) {
- struct pollfd fds[1];
- unsigned deadline_us;
- struct arp_packet p;
- int source_ip_conflict;
- int target_ip_conflict;
- fds[0].fd = sock_fd;
- fds[0].events = POLLIN;
- fds[0].revents = 0;
-
- if (!timeout_ms) {
- timeout_ms = random_delay_ms(PROBE_WAIT);
-
-
-
- }
-
- deadline_us = MONOTONIC_US() + timeout_ms * 1000;
- VDBG("...wait %d %s nprobes=%u, nclaims=%u\n",
- timeout_ms, argv_intf, nprobes, nclaims);
- switch (safe_poll(fds, 1, timeout_ms)) {
- default:
-
- return EXIT_FAILURE;
-
- case 0:
- VDBG("state = %d\n", state);
- switch (state) {
- case PROBE:
-
-
- if (nprobes < PROBE_NUM) {
- nprobes++;
- VDBG("probe/%u %s@%s\n",
- nprobes, argv_intf, inet_ntoa(ip));
- arp(
- null_ip,
- &null_addr, ip);
- timeout_ms = PROBE_MIN * 1000;
- timeout_ms += random_delay_ms(PROBE_MAX - PROBE_MIN);
- }
- else {
-
- state = ANNOUNCE;
- nclaims = 0;
- VDBG("announce/%u %s@%s\n",
- nclaims, argv_intf, inet_ntoa(ip));
- arp(
- ip,
- ð_addr, ip);
- timeout_ms = ANNOUNCE_INTERVAL * 1000;
- }
- break;
- case RATE_LIMIT_PROBE:
-
-
- state = ANNOUNCE;
- nclaims = 0;
- VDBG("announce/%u %s@%s\n",
- nclaims, argv_intf, inet_ntoa(ip));
- arp(
- ip,
- ð_addr, ip);
- timeout_ms = ANNOUNCE_INTERVAL * 1000;
- break;
- case ANNOUNCE:
-
-
- if (nclaims < ANNOUNCE_NUM) {
- nclaims++;
- VDBG("announce/%u %s@%s\n",
- nclaims, argv_intf, inet_ntoa(ip));
- arp(
- ip,
- ð_addr, ip);
- timeout_ms = ANNOUNCE_INTERVAL * 1000;
- }
- else {
-
- state = MONITOR;
-
-
- run(argv, "config", &ip);
- ready = 1;
- conflicts = 0;
- timeout_ms = -1;
-
-
- if (QUIT)
- return EXIT_SUCCESS;
- }
- break;
- case DEFEND:
-
- state = MONITOR;
- timeout_ms = -1;
- conflicts = 0;
- break;
- default:
-
- state = PROBE;
- ip.s_addr = pick();
- timeout_ms = 0;
- nprobes = 0;
- nclaims = 0;
- break;
- }
- break;
-
- case 1:
-
-
- if (timeout_ms > 0) {
- unsigned diff = deadline_us - MONOTONIC_US();
- if ((int)(diff) < 0) {
-
-
- VDBG("missed an expected timeout\n");
- timeout_ms = 0;
- } else {
- 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 (ready) {
- run(argv, "deconfig", &ip);
- }
- 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;
- #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("%s recv arp type=%d, op=%d,\n",
- argv_intf, ntohs(p.eth.ether_type),
- ntohs(p.arp.arp_op));
- VDBG("\tsource=%s %s\n",
- ether_ntoa(sha),
- inet_ntoa(*spa));
- VDBG("\ttarget=%s %s\n",
- ether_ntoa(tha),
- inet_ntoa(*tpa));
- }
- #endif
- if (p.arp.arp_op != htons(ARPOP_REQUEST)
- && p.arp.arp_op != htons(ARPOP_REPLY))
- continue;
- source_ip_conflict = 0;
- target_ip_conflict = 0;
- if (memcmp(p.arp.arp_spa, &ip.s_addr, sizeof(struct in_addr)) == 0
- && memcmp(&p.arp.arp_sha, ð_addr, ETH_ALEN) != 0
- ) {
- source_ip_conflict = 1;
- }
- if (p.arp.arp_op == htons(ARPOP_REQUEST)
- && memcmp(p.arp.arp_tpa, &ip.s_addr, sizeof(struct in_addr)) == 0
- && memcmp(&p.arp.arp_tha, ð_addr, ETH_ALEN) != 0
- ) {
- target_ip_conflict = 1;
- }
- VDBG("state = %d, source ip conflict = %d, target ip conflict = %d\n",
- state, source_ip_conflict, target_ip_conflict);
- switch (state) {
- case PROBE:
- case ANNOUNCE:
-
-
- if (source_ip_conflict || target_ip_conflict) {
- conflicts++;
- if (conflicts >= MAX_CONFLICTS) {
- VDBG("%s ratelimit\n", argv_intf);
- timeout_ms = RATE_LIMIT_INTERVAL * 1000;
- state = RATE_LIMIT_PROBE;
- }
-
- ip.s_addr = pick();
- timeout_ms = 0;
- nprobes = 0;
- nclaims = 0;
- }
- break;
- case MONITOR:
-
- if (source_ip_conflict) {
- VDBG("monitor conflict -- defending\n");
- state = DEFEND;
- timeout_ms = DEFEND_INTERVAL * 1000;
- arp(
- ip,
- ð_addr, ip);
- }
- break;
- case DEFEND:
-
- if (source_ip_conflict) {
- state = PROBE;
- VDBG("defend conflict -- starting over\n");
- ready = 0;
- run(argv, "deconfig", &ip);
-
- ip.s_addr = pick();
- timeout_ms = 0;
- nprobes = 0;
- nclaims = 0;
- }
- break;
- default:
-
- VDBG("invalid state -- starting over\n");
- state = PROBE;
- ip.s_addr = pick();
- timeout_ms = 0;
- nprobes = 0;
- nclaims = 0;
- break;
- }
- break;
- }
- }
- #undef argv_intf
- }
|