2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 #include <sys/ioctl.h>
20 #include <arpa/inet.h>
22 #include <net/ethernet.h>
23 #include <netinet/if_ether.h>
24 #include <netinet/ip.h>
25 #include <netinet/udp.h>
27 #include <linux/if_packet.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/neighbour.h>
46 LIST_HEAD(interfaces);
49 static int host_timeout;
51 static int forward_bcast;
52 static int forward_dhcp;
53 static struct uloop_fd rtnl_sock;
54 static unsigned int rtnl_seq, rtnl_dump_seq;
56 static struct relayd_host *find_host_by_ipaddr(struct relayd_interface *rif, const uint8_t *ipaddr)
58 struct relayd_host *host;
61 list_for_each_entry(rif, &interfaces, list) {
62 host = find_host_by_ipaddr(rif, ipaddr);
71 list_for_each_entry(host, &rif->hosts, list) {
72 if (memcmp(ipaddr, host->ipaddr, sizeof(host->ipaddr)) != 0)
80 static void add_arp(struct relayd_host *host)
82 struct sockaddr_in *sin;
85 strncpy(arp.arp_dev, host->rif->ifname, sizeof(arp.arp_dev));
86 arp.arp_flags = ATF_COM;
88 arp.arp_ha.sa_family = ARPHRD_ETHER;
89 memcpy(arp.arp_ha.sa_data, host->lladdr, ETH_ALEN);
91 sin = (struct sockaddr_in *) &arp.arp_pa;
92 sin->sin_family = AF_INET;
93 memcpy(&sin->sin_addr, host->ipaddr, sizeof(host->ipaddr));
95 ioctl(inet_sock, SIOCSARP, &arp);
98 static void rtnl_route_set(struct relayd_host *host, bool add)
113 memset(&req, 0, sizeof(req));
115 req.nl.nlmsg_len = sizeof(req);
116 req.rt.rtm_family = AF_INET;
117 req.rt.rtm_dst_len = 32;
119 req.dst.rta.rta_type = RTA_DST;
120 req.dst.rta.rta_len = sizeof(req.dst);
121 memcpy(req.dst.ipaddr, host->ipaddr, sizeof(req.dst.ipaddr));
123 req.dev.rta.rta_type = RTA_OIF;
124 req.dev.rta.rta_len = sizeof(req.dev);
125 req.dev.ifindex = host->rif->sll.sll_ifindex;
127 req.nl.nlmsg_flags = NLM_F_REQUEST;
128 req.rt.rtm_table = RT_TABLE_MAIN;
130 req.nl.nlmsg_type = RTM_NEWROUTE;
131 req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
133 req.rt.rtm_protocol = RTPROT_BOOT;
134 req.rt.rtm_scope = RT_SCOPE_LINK;
135 req.rt.rtm_type = RTN_UNICAST;
137 req.nl.nlmsg_type = RTM_DELROUTE;
138 req.rt.rtm_scope = RT_SCOPE_NOWHERE;
141 send(rtnl_sock.fd, &req, sizeof(req), 0);
144 static void add_route(struct relayd_host *host)
146 rtnl_route_set(host, true);
149 static void del_route(struct relayd_host *host)
151 rtnl_route_set(host, false);
154 static void del_host(struct relayd_host *host)
156 DPRINTF(1, "%s: deleting host "IP_FMT" ("MAC_FMT")\n", host->rif->ifname,
157 IP_BUF(host->ipaddr), MAC_BUF(host->lladdr));
159 if (host->rif->managed)
161 list_del(&host->list);
165 static void fill_arp_request(struct arp_packet *pkt, struct relayd_interface *rif,
166 uint8_t spa[4], uint8_t tpa[4])
168 memset(pkt, 0, sizeof(*pkt));
170 pkt->eth.ether_type = htons(ETHERTYPE_ARP);
171 memcpy(pkt->eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
173 memcpy(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
174 memcpy(pkt->arp.arp_spa, spa, 4);
175 memcpy(pkt->arp.arp_tpa, tpa, 4);
177 pkt->arp.arp_hrd = htons(ARPHRD_ETHER);
178 pkt->arp.arp_pro = htons(ETH_P_IP);
179 pkt->arp.arp_hln = ETH_ALEN;
180 pkt->arp.arp_pln = 4;
183 static void send_arp_request(struct relayd_host *host)
185 struct relayd_interface *rif = host->rif;
186 struct arp_packet pkt;
188 fill_arp_request(&pkt, host->rif, host->rif->src_ip, host->ipaddr);
190 pkt.arp.arp_op = htons(ARPOP_REQUEST);
191 memcpy(pkt.arp.arp_spa, rif->src_ip, ETH_ALEN);
192 memset(pkt.arp.arp_tha, 0, ETH_ALEN);
193 memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN);
195 DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
196 rif->ifname, IP_BUF(pkt.arp.arp_tpa),
197 IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
199 sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
200 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
203 static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4],
204 uint8_t tha[ETH_ALEN], uint8_t tpa[4])
206 struct arp_packet pkt;
208 fill_arp_request(&pkt, rif, spa, tpa);
210 pkt.arp.arp_op = htons(ARPOP_REPLY);
211 memcpy(pkt.eth.ether_dhost, tha, ETH_ALEN);
212 memcpy(pkt.arp.arp_tha, tha, ETH_ALEN);
214 DPRINTF(2, "%s: sending ARP reply to "IP_FMT", "IP_FMT" is at ("MAC_FMT")\n",
215 rif->ifname, IP_BUF(pkt.arp.arp_tpa),
216 IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
218 sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
219 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
222 static void host_entry_timeout(struct uloop_timeout *timeout)
224 struct relayd_host *host = container_of(timeout, struct relayd_host, timeout);
227 * When a host is behind a managed interface, we must not expire its host
228 * entry prematurely, as this will cause routes to the node to expire,
229 * leading to loss of connectivity from the other side.
230 * When the timeout is reached, try pinging the host a few times before
233 if (host->rif->managed && host->cleanup_pending < 2) {
234 send_arp_request(host);
235 host->cleanup_pending++;
236 uloop_timeout_set(&host->timeout, 1000);
242 static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
244 struct relayd_host *host;
246 DPRINTF(1, "%s: adding host "IP_FMT" ("MAC_FMT")\n", rif->ifname,
247 IP_BUF(ipaddr), MAC_BUF(lladdr));
249 host = calloc(1, sizeof(*host));
251 memcpy(host->ipaddr, ipaddr, sizeof(host->ipaddr));
252 memcpy(host->lladdr, lladdr, sizeof(host->lladdr));
253 list_add(&host->list, &rif->hosts);
254 host->timeout.cb = host_entry_timeout;
255 uloop_timeout_set(&host->timeout, host_timeout * 1000);
264 static struct relayd_host *refresh_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
266 struct relayd_host *host;
268 host = find_host_by_ipaddr(rif, ipaddr);
270 host = find_host_by_ipaddr(NULL, ipaddr);
273 * When we suddenly see the host appearing on a different interface,
274 * reduce the timeout to make the old entry expire faster, in case the
276 * If the old entry is behind a managed interface, it will be pinged
277 * before we expire it
279 if (host && !host->cleanup_pending)
280 uloop_timeout_set(&host->timeout, 1);
282 host = add_host(rif, lladdr, ipaddr);
284 host->cleanup_pending = false;
285 uloop_timeout_set(&host->timeout, host_timeout * 1000);
291 static void relay_arp_request(struct relayd_interface *from_rif, struct arp_packet *pkt)
293 struct relayd_interface *rif;
294 struct arp_packet reqpkt;
296 memcpy(&reqpkt, pkt, sizeof(reqpkt));
297 list_for_each_entry(rif, &interfaces, list) {
301 memcpy(reqpkt.eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
302 memcpy(reqpkt.arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
304 DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
305 rif->ifname, IP_BUF(reqpkt.arp.arp_tpa),
306 IP_BUF(reqpkt.arp.arp_spa), MAC_BUF(reqpkt.eth.ether_shost));
308 sendto(rif->fd.fd, &reqpkt, sizeof(reqpkt), 0,
309 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
313 static void recv_arp_request(struct relayd_interface *rif, struct arp_packet *pkt)
315 struct relayd_host *host;
317 DPRINTF(2, "%s: ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
319 IP_BUF(pkt->arp.arp_tpa),
320 IP_BUF(pkt->arp.arp_spa),
321 MAC_BUF(pkt->eth.ether_shost));
323 if (!memcmp(pkt->arp.arp_spa, "\x00\x00\x00\x00", 4))
326 refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa);
328 host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
331 * If a host is being pinged because of a timeout, do not use the cached
332 * entry here. That way we can avoid giving out stale data in case the node
333 * has moved. We shouldn't relay requests here either, as we might miss our
334 * chance to create a host route.
336 if (host && host->cleanup_pending)
339 relay_arp_request(rif, pkt);
343 static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt)
345 struct relayd_host *host;
347 DPRINTF(2, "%s: received ARP reply for "IP_FMT" from "MAC_FMT", deliver to "IP_FMT"\n",
349 IP_BUF(pkt->arp.arp_spa),
350 MAC_BUF(pkt->eth.ether_shost),
351 IP_BUF(pkt->arp.arp_tpa));
353 refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
355 if (!memcmp(pkt->arp.arp_tpa, rif->src_ip, 4))
358 host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
362 if (host->rif == rif)
365 send_arp_reply(host->rif, pkt->arp.arp_spa, host->lladdr, host->ipaddr);
368 static void recv_packet(struct uloop_fd *fd, unsigned int events)
370 struct relayd_interface *rif = container_of(fd, struct relayd_interface, fd);
371 struct arp_packet *pkt;
372 static char pktbuf[4096];
379 pktlen = recv(rif->fd.fd, pktbuf, sizeof(pktbuf), 0);
390 pkt = (void *)pktbuf;
391 if (pkt->arp.arp_op == htons(ARPOP_REPLY))
392 recv_arp_reply(rif, pkt);
393 else if (pkt->arp.arp_op == htons(ARPOP_REQUEST))
394 recv_arp_request(rif, pkt);
396 DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt->arp.arp_op));
401 static void forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len)
403 struct relayd_interface *rif;
404 struct ether_header *eth = packet;
406 list_for_each_entry(rif, &interfaces, list) {
410 DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif->ifname, rif->ifname);
411 memcpy(eth->ether_shost, rif->sll.sll_addr, ETH_ALEN);
412 send(rif->bcast_fd.fd, packet, len, 0);
417 chksum(uint16_t sum, const uint8_t *data, uint16_t len)
422 last = data + len - 1;
425 t = (data[0] << 8) + data[1];
433 t = (data[0] << 8) + 0;
442 static bool forward_dhcp_packet(struct relayd_interface *rif, void *data, int len)
444 struct ip_packet *pkt = data;
446 struct dhcp_header *dhcp;
450 if (pkt->eth.ether_type != htons(ETH_P_IP))
453 if (pkt->iph.version != 4)
456 if (pkt->iph.protocol != IPPROTO_UDP)
459 udp = (void *) ((char *) &pkt->iph + (pkt->iph.ihl << 2));
460 dhcp = (void *) (udp + 1);
462 udplen = ntohs(udp->len);
463 if (udplen > len - ((char *) udp - (char *) data))
466 if (udp->dest != htons(67) && udp->source != htons(67))
469 if (dhcp->op != 1 && dhcp->op != 2)
476 refresh_host(rif, pkt->eth.ether_shost, (void *) &pkt->iph.saddr);
478 DPRINTF(2, "%s: handling DHCP %s\n", rif->ifname, (dhcp->op == 1 ? "request" : "response"));
480 dhcp->flags |= htons(DHCP_FLAG_BROADCAST);
483 sum = udplen + IPPROTO_UDP;
484 sum = chksum(sum, (void *) &pkt->iph.saddr, 8);
485 sum = chksum(sum, (void *) udp, udplen);
489 udp->check = htons(~sum);
491 forward_bcast_packet(rif, data, len);
496 static void recv_bcast_packet(struct uloop_fd *fd, unsigned int events)
498 struct relayd_interface *rif = container_of(fd, struct relayd_interface, bcast_fd);
499 static char pktbuf[4096];
506 pktlen = recv(rif->bcast_fd.fd, pktbuf, sizeof(pktbuf), 0);
517 if (!forward_bcast && !forward_dhcp)
520 if (forward_dhcp_packet(rif, pktbuf, pktlen))
524 forward_bcast_packet(rif, pktbuf, pktlen);
529 static int init_interface(struct relayd_interface *rif)
531 struct sockaddr_ll *sll = &rif->sll;
532 struct sockaddr_in *sin;
535 #ifdef PACKET_RECV_TYPE
536 unsigned int pkt_type;
539 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
545 memset(&ifr, 0, sizeof(ifr));
546 strcpy(ifr.ifr_name, rif->ifname);
548 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
549 perror("ioctl(SIOCGIFHWADDR)");
553 memcpy(sll->sll_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
554 sll->sll_family = AF_PACKET;
555 sll->sll_protocol = htons(ETH_P_ARP);
556 sll->sll_pkttype = PACKET_BROADCAST;
557 sll->sll_hatype = ARPHRD_ETHER;
558 sll->sll_halen = ETH_ALEN;
560 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
561 perror("ioctl(SIOCGIFINDEX)");
565 sll->sll_ifindex = ifr.ifr_ifindex;
567 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
568 memcpy(rif->src_ip, DUMMY_IP, sizeof(rif->src_ip));
570 sin = (struct sockaddr_in *) &ifr.ifr_addr;
571 memcpy(rif->src_ip, &sin->sin_addr.s_addr, sizeof(rif->src_ip));
574 if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
575 perror("bind(ETH_P_ARP)");
579 rif->fd.cb = recv_packet;
580 uloop_fd_add(&rif->fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
582 if (!forward_bcast && !forward_dhcp)
585 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
589 rif->bcast_fd.fd = fd;
590 rif->bcast_fd.cb = recv_bcast_packet;
592 memcpy(&rif->bcast_sll, &rif->sll, sizeof(rif->bcast_sll));
593 sll = &rif->bcast_sll;
594 sll->sll_protocol = htons(ETH_P_IP);
596 if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
597 perror("bind(ETH_P_IP)");
601 #ifdef PACKET_RECV_TYPE
602 pkt_type = (1 << PACKET_BROADCAST);
603 setsockopt(fd, SOL_PACKET, PACKET_RECV_TYPE, &pkt_type, sizeof(pkt_type));
606 uloop_fd_add(&rif->bcast_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
610 static int init_interfaces(void)
612 struct relayd_interface *rif;
615 list_for_each_entry(rif, &interfaces, list) {
616 ret = init_interface(rif);
624 static void del_interface(struct relayd_interface *rif)
626 struct relayd_host *host, *htmp;
628 list_for_each_entry_safe(host, htmp, &rif->hosts, list) {
634 static void cleanup_interfaces(void)
636 struct relayd_interface *rif, *rtmp;
638 list_for_each_entry_safe(rif, rtmp, &interfaces, list) {
643 static int alloc_interface(const char *ifname, bool managed)
645 struct relayd_interface *rif;
647 if (strlen(ifname) >= IFNAMSIZ)
650 rif = calloc(1, sizeof(*rif));
654 INIT_LIST_HEAD(&rif->list);
655 INIT_LIST_HEAD(&rif->hosts);
656 strcpy(rif->ifname, ifname);
657 list_add(&rif->list, &interfaces);
658 rif->managed = managed;
665 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
668 static void rtnl_parse_newneigh(struct nlmsghdr *h)
670 struct relayd_interface *rif = NULL;
671 struct ndmsg *r = NLMSG_DATA(h);
672 const uint8_t *lladdr = NULL;
673 const uint8_t *ipaddr = NULL;
677 if (r->ndm_family != AF_INET)
680 list_for_each_entry(rif, &interfaces, list) {
681 if (rif->sll.sll_ifindex == r->ndm_ifindex)
682 goto found_interface;
687 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
688 for (rta = NDA_RTA(r); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
689 switch(rta->rta_type) {
691 lladdr = RTA_DATA(rta);
694 ipaddr = RTA_DATA(rta);
701 if (!lladdr || !ipaddr || (r->ndm_state & (NUD_INCOMPLETE|NUD_FAILED)))
704 if (!memcmp(lladdr, "\x00\x00\x00\x00\x00\x00", ETH_ALEN))
707 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT" ("MAC_FMT")\n",
708 rif->ifname, IP_BUF(ipaddr), MAC_BUF(lladdr));
709 refresh_host(rif, lladdr, ipaddr);
712 static void rtnl_parse_packet(void *data, int len)
716 for (h = data; NLMSG_OK(h, len); h = NLMSG_NEXT(h, len)) {
717 if (h->nlmsg_type == NLMSG_DONE ||
718 h->nlmsg_type == NLMSG_ERROR)
721 if (h->nlmsg_seq != rtnl_dump_seq)
724 if (h->nlmsg_type == RTM_NEWNEIGH)
725 rtnl_parse_newneigh(h);
729 static void rtnl_cb(struct uloop_fd *fd, unsigned int events)
731 struct sockaddr_nl nladdr;
732 static uint8_t buf[16384];
735 .iov_len = sizeof(buf),
737 struct msghdr msg = {
739 .msg_namelen = sizeof(nladdr),
747 len = recvmsg(rtnl_sock.fd, &msg, 0);
758 if (nladdr.nl_pid != 0)
761 rtnl_parse_packet(buf, len);
765 static int rtnl_init(void)
767 struct sockaddr_nl snl_local;
773 .nlmsg_len = sizeof(req),
774 .nlmsg_type = RTM_GETNEIGH,
775 .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
778 .g.rtgen_family = AF_INET,
781 rtnl_sock.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
782 if (rtnl_sock.fd < 0) {
783 perror("socket(AF_NETLINK)");
787 snl_local.nl_family = AF_NETLINK;
789 if (bind(rtnl_sock.fd, (struct sockaddr *) &snl_local, sizeof(struct sockaddr_nl)) < 0) {
795 rtnl_sock.cb = rtnl_cb;
796 uloop_fd_add(&rtnl_sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
798 rtnl_seq = time(NULL);
799 rtnl_dump_seq = rtnl_seq;
800 req.nlh.nlmsg_seq = rtnl_seq;
801 send(rtnl_sock.fd, &req, sizeof(req), 0);
806 static void die(int signo)
809 * When we hit SIGTERM, clean up interfaces directly, so that we
810 * won't leave our routing in an invalid state.
812 cleanup_interfaces();
816 static int usage(const char *progname)
818 fprintf(stderr, "Usage: %s <options>\n"
821 " -d Enable debug messages\n"
822 " -i <ifname> Add an interface for relaying\n"
823 " -I <ifname> Same as -i, except with ARP cache and host route management\n"
824 " You need to specify at least two interfaces\n"
825 " -t <timeout> Host entry expiry timeout\n"
826 " -B Enable broadcast forwarding\n"
827 " -D Enable DHCP forwarding\n"
833 int main(int argc, char **argv)
840 inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
842 perror("socket(AF_INET)");
850 while ((ch = getopt(argc, argv, "I:i:t:BDd")) != -1) {
857 if (alloc_interface(optarg, managed) < 0)
863 host_timeout = atoi(optarg);
864 if (host_timeout <= 0)
865 return usage(argv[0]);
878 return usage(argv[0]);
882 if (list_empty(&interfaces))
883 return usage(argv[0]);
886 fprintf(stderr, "ERROR: Need at least 2 interfaces for relaying\n");
893 signal(SIGTERM, die);
895 signal(SIGUSR1, die);
896 signal(SIGUSR2, die);
898 if (init_interfaces() < 0)
907 cleanup_interfaces();
908 uloop_fd_delete(&rtnl_sock);