X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;f=main.c;h=b3c13f7f7a49f3693df2efa1adfcae8dbaba56cf;hb=HEAD;hp=e677f49ba2a5770e93e79aa0b7ae271fa31e77ff;hpb=3771bad14d2423062f8ae6ef8734b52182618e92;p=project%2Frelayd.git diff --git a/main.c b/main.c index e677f49..b3c13f7 100644 --- a/main.c +++ b/main.c @@ -36,9 +36,11 @@ LIST_HEAD(interfaces); int debug; static int host_timeout; +static int host_ping_tries; static int inet_sock; static int forward_bcast; static int forward_dhcp; +static int parse_dhcp; uint8_t local_addr[4]; int local_route_table; @@ -163,7 +165,7 @@ static void send_arp_request(struct relayd_interface *rif, const uint8_t *ipaddr fill_arp_packet(&pkt, rif, rif->src_ip, ipaddr); pkt.arp.arp_op = htons(ARPOP_REQUEST); - memcpy(pkt.arp.arp_spa, rif->src_ip, ETH_ALEN); + memcpy(pkt.arp.arp_spa, rif->src_ip, sizeof(pkt.arp.arp_spa)); memset(pkt.arp.arp_tha, 0, ETH_ALEN); memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN); @@ -205,15 +207,15 @@ void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8 } } -static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4], - uint8_t tha[ETH_ALEN], uint8_t tpa[4]) +static void send_arp_reply(struct relayd_interface *rif, const uint8_t spa[4], + const uint8_t tha[ETH_ALEN], const uint8_t tpa[4]) { struct arp_packet pkt; fill_arp_packet(&pkt, rif, spa, tpa); - pkt.arp.arp_op = htons(ARPOP_REPLY); if (tha) { + pkt.arp.arp_op = htons(ARPOP_REPLY); memcpy(pkt.eth.ether_dhost, tha, ETH_ALEN); memcpy(pkt.arp.arp_tha, tha, ETH_ALEN); @@ -221,8 +223,9 @@ static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4], rif->ifname, IP_BUF(pkt.arp.arp_tpa), IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost)); } else { + pkt.arp.arp_op = htons(ARPOP_REQUEST); memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN); - memset(pkt.arp.arp_tha, 0, ETH_ALEN); + memset(pkt.arp.arp_tha, 0xff, ETH_ALEN); DPRINTF(2, "%s: sending gratuitous ARP: "IP_FMT" is at ("MAC_FMT")\n", rif->ifname, IP_BUF(pkt.arp.arp_tpa), @@ -231,11 +234,26 @@ static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4], sendto(rif->fd.fd, &pkt, sizeof(pkt), 0, (struct sockaddr *) &rif->sll, sizeof(rif->sll)); + + if (tha) + return; + + /* + * Gratuitous ARP comes in two flavours, request and reply. + * Some operating systems only accept request, some only reply. + * Let's just send both... + */ + pkt.arp.arp_op = htons(ARPOP_REPLY); + + sendto(rif->fd.fd, &pkt, sizeof(pkt), 0, + (struct sockaddr *) &rif->sll, sizeof(rif->sll)); + } static void host_entry_timeout(struct uloop_timeout *timeout) { struct relayd_host *host = container_of(timeout, struct relayd_host, timeout); + struct relayd_interface *rif; /* * When a host is behind a managed interface, we must not expire its host @@ -244,8 +262,10 @@ static void host_entry_timeout(struct uloop_timeout *timeout) * When the timeout is reached, try pinging the host a few times before * giving up on it. */ - if (host->rif->managed && host->cleanup_pending < 2) { - send_arp_request(host->rif, host->ipaddr); + if (host->rif->managed && host->cleanup_pending < host_ping_tries) { + list_for_each_entry(rif, &interfaces, list) { + send_arp_request(rif, host->ipaddr); + } host->cleanup_pending++; uloop_timeout_set(&host->timeout, 1000); return; @@ -290,6 +310,19 @@ static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t return host; } +static void send_gratuitous_arp(struct relayd_interface *rif, const uint8_t *spa) +{ + struct relayd_interface *to_rif; + + list_for_each_entry(to_rif, &interfaces, list) { + if (rif == to_rif) + continue; + + send_arp_reply(to_rif, spa, NULL, spa); + } +} + + struct relayd_host *relayd_refresh_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr) { struct relayd_host *host; @@ -314,6 +347,7 @@ struct relayd_host *relayd_refresh_host(struct relayd_interface *rif, const uint } else { host->cleanup_pending = false; uloop_timeout_set(&host->timeout, host_timeout * 1000); + send_gratuitous_arp(rif, ipaddr); } return host; @@ -330,7 +364,9 @@ static void relay_arp_request(struct relayd_interface *from_rif, struct arp_pack continue; memcpy(reqpkt.eth.ether_shost, rif->sll.sll_addr, ETH_ALEN); + memset(reqpkt.eth.ether_dhost, 0xff, ETH_ALEN); memcpy(reqpkt.arp.arp_sha, rif->sll.sll_addr, ETH_ALEN); + memset(reqpkt.arp.arp_tha, 0, ETH_ALEN); DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n", rif->ifname, IP_BUF(reqpkt.arp.arp_tpa), @@ -354,13 +390,15 @@ static void recv_arp_request(struct relayd_interface *rif, struct arp_packet *pk if (!memcmp(pkt->arp.arp_spa, "\x00\x00\x00\x00", 4)) return; + host = find_host_by_ipaddr(NULL, pkt->arp.arp_spa); + if (!host || host->rif != rif) + relayd_refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa); + if (local_route_table && !memcmp(pkt->arp.arp_tpa, local_addr, sizeof(local_addr))) { send_arp_reply(rif, local_addr, pkt->arp.arp_sha, pkt->arp.arp_spa); return; } - relayd_refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa); - host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa); /* @@ -375,10 +413,8 @@ static void recv_arp_request(struct relayd_interface *rif, struct arp_packet *pk relay_arp_request(rif, pkt); } - static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt) { - struct relayd_interface *to_rif; struct relayd_host *host; DPRINTF(2, "%s: received ARP reply for "IP_FMT" from "MAC_FMT", deliver to "IP_FMT"\n", @@ -390,20 +426,6 @@ static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt) if (memcmp(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN) != 0) relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa); - if (!memcmp(pkt->arp.arp_tpa, rif->src_ip, 4)) { - /* - * locally initiated lookup, relay as gratuitous ARP - * to all other interfaces - */ - list_for_each_entry(to_rif, &interfaces, list) { - if (rif == to_rif) - continue; - - send_arp_reply(to_rif, pkt->arp.arp_spa, NULL, pkt->arp.arp_spa); - } - return; - } - host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa); if (!host) return; @@ -486,7 +508,7 @@ static void recv_bcast_packet(struct uloop_fd *fd, unsigned int events) if (!forward_bcast && !forward_dhcp) continue; - if (relayd_handle_dhcp_packet(rif, pktbuf, pktlen, forward_dhcp)) + if (relayd_handle_dhcp_packet(rif, pktbuf, pktlen, forward_dhcp, parse_dhcp)) continue; if (forward_bcast) @@ -568,7 +590,7 @@ static int init_interface(struct relayd_interface *rif) } #ifdef PACKET_RECV_TYPE - pkt_type = (1 << PACKET_BROADCAST); + pkt_type = (1 << PACKET_BROADCAST) | (1 << PACKET_MULTICAST); setsockopt(fd, SOL_PACKET, PACKET_RECV_TYPE, &pkt_type, sizeof(pkt_type)); #endif @@ -631,6 +653,11 @@ static struct relayd_interface *alloc_interface(const char *ifname, bool managed if (strlen(ifname) >= IFNAMSIZ) return NULL; + list_for_each_entry(rif, &interfaces, list) { + if (!strncmp(rif->ifname, ifname, IFNAMSIZ)) + return rif; + } + rif = calloc(1, sizeof(*rif)); if (!rif) return NULL; @@ -649,9 +676,7 @@ static void die(int signo) * When we hit SIGTERM, clean up interfaces directly, so that we * won't leave our routing in an invalid state. */ - cleanup_hosts(); - free_interfaces(); - exit(1); + uloop_end(); } static int usage(const char *progname) @@ -667,9 +692,11 @@ static int usage(const char *progname) " -R :/\n" " Add a static route for / via \n" " -t Host entry expiry timeout\n" + " -p Number of ARP ping attempts before considering a host dead\n" " -T Set routing table number for automatically added routes\n" " -B Enable broadcast forwarding\n" " -D Enable DHCP forwarding\n" + " -P Disable DHCP options parsing\n" " -L Enable local access using as source address\n" "\n", progname); @@ -681,7 +708,7 @@ int main(int argc, char **argv) struct relayd_interface *rif = NULL; struct in_addr addr, addr2; bool local_addr_valid = false; - bool managed; + bool managed = false; int ifnum = 0; char *s, *s2; int mask; @@ -694,12 +721,14 @@ int main(int argc, char **argv) return 1; } - host_timeout = 60; + host_timeout = 30; + host_ping_tries = 5; forward_bcast = 0; local_route_table = 0; + parse_dhcp = 1; uloop_init(); - while ((ch = getopt(argc, argv, "I:i:t:BDdT:G:R:L:")) != -1) { + while ((ch = getopt(argc, argv, "I:i:t:p:BDPdT:G:R:L:")) != -1) { switch(ch) { case 'I': managed = true; @@ -717,6 +746,11 @@ int main(int argc, char **argv) if (host_timeout <= 0) return usage(argv[0]); break; + case 'p': + host_ping_tries = atoi(optarg); + if (host_ping_tries <= 0) + return usage(argv[0]); + break; case 'd': debug++; break; @@ -726,6 +760,9 @@ int main(int argc, char **argv) case 'D': forward_dhcp = 1; break; + case 'P': + parse_dhcp = 0; + break; case 'T': route_table = atoi(optarg); if (route_table <= 0)