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>
19 #include <sys/socket.h>
34 LIST_HEAD(interfaces);
37 static int host_timeout;
39 static int forward_bcast;
40 static int forward_dhcp;
42 static struct relayd_host *find_host_by_ipaddr(struct relayd_interface *rif, const uint8_t *ipaddr)
44 struct relayd_host *host;
47 list_for_each_entry(rif, &interfaces, list) {
48 host = find_host_by_ipaddr(rif, ipaddr);
57 list_for_each_entry(host, &rif->hosts, list) {
58 if (memcmp(ipaddr, host->ipaddr, sizeof(host->ipaddr)) != 0)
66 static void add_arp(struct relayd_host *host)
68 struct sockaddr_in *sin;
71 strncpy(arp.arp_dev, host->rif->ifname, sizeof(arp.arp_dev));
72 arp.arp_flags = ATF_COM;
74 arp.arp_ha.sa_family = ARPHRD_ETHER;
75 memcpy(arp.arp_ha.sa_data, host->lladdr, ETH_ALEN);
77 sin = (struct sockaddr_in *) &arp.arp_pa;
78 sin->sin_family = AF_INET;
79 memcpy(&sin->sin_addr, host->ipaddr, sizeof(host->ipaddr));
81 ioctl(inet_sock, SIOCSARP, &arp);
84 static void del_host(struct relayd_host *host)
86 DPRINTF(1, "%s: deleting host "IP_FMT" ("MAC_FMT")\n", host->rif->ifname,
87 IP_BUF(host->ipaddr), MAC_BUF(host->lladdr));
89 if (host->rif->managed)
90 relayd_del_route(host);
91 uloop_timeout_cancel(&host->timeout);
92 list_del(&host->list);
96 static void fill_arp_request(struct arp_packet *pkt, struct relayd_interface *rif,
97 uint8_t spa[4], uint8_t tpa[4])
99 memset(pkt, 0, sizeof(*pkt));
101 pkt->eth.ether_type = htons(ETHERTYPE_ARP);
102 memcpy(pkt->eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
104 memcpy(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
105 memcpy(pkt->arp.arp_spa, spa, 4);
106 memcpy(pkt->arp.arp_tpa, tpa, 4);
108 pkt->arp.arp_hrd = htons(ARPHRD_ETHER);
109 pkt->arp.arp_pro = htons(ETH_P_IP);
110 pkt->arp.arp_hln = ETH_ALEN;
111 pkt->arp.arp_pln = 4;
114 static void send_arp_request(struct relayd_host *host)
116 struct relayd_interface *rif = host->rif;
117 struct arp_packet pkt;
119 fill_arp_request(&pkt, host->rif, host->rif->src_ip, host->ipaddr);
121 pkt.arp.arp_op = htons(ARPOP_REQUEST);
122 memcpy(pkt.arp.arp_spa, rif->src_ip, ETH_ALEN);
123 memset(pkt.arp.arp_tha, 0, ETH_ALEN);
124 memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN);
126 DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
127 rif->ifname, IP_BUF(pkt.arp.arp_tpa),
128 IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
130 sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
131 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
134 static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4],
135 uint8_t tha[ETH_ALEN], uint8_t tpa[4])
137 struct arp_packet pkt;
139 fill_arp_request(&pkt, rif, spa, tpa);
141 pkt.arp.arp_op = htons(ARPOP_REPLY);
142 memcpy(pkt.eth.ether_dhost, tha, ETH_ALEN);
143 memcpy(pkt.arp.arp_tha, tha, ETH_ALEN);
145 DPRINTF(2, "%s: sending ARP reply to "IP_FMT", "IP_FMT" is at ("MAC_FMT")\n",
146 rif->ifname, IP_BUF(pkt.arp.arp_tpa),
147 IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
149 sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
150 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
153 static void host_entry_timeout(struct uloop_timeout *timeout)
155 struct relayd_host *host = container_of(timeout, struct relayd_host, timeout);
158 * When a host is behind a managed interface, we must not expire its host
159 * entry prematurely, as this will cause routes to the node to expire,
160 * leading to loss of connectivity from the other side.
161 * When the timeout is reached, try pinging the host a few times before
164 if (host->rif->managed && host->cleanup_pending < 2) {
165 send_arp_request(host);
166 host->cleanup_pending++;
167 uloop_timeout_set(&host->timeout, 1000);
173 static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
175 struct relayd_host *host;
177 DPRINTF(1, "%s: adding host "IP_FMT" ("MAC_FMT")\n", rif->ifname,
178 IP_BUF(ipaddr), MAC_BUF(lladdr));
180 host = calloc(1, sizeof(*host));
182 memcpy(host->ipaddr, ipaddr, sizeof(host->ipaddr));
183 memcpy(host->lladdr, lladdr, sizeof(host->lladdr));
184 list_add(&host->list, &rif->hosts);
185 host->timeout.cb = host_entry_timeout;
186 uloop_timeout_set(&host->timeout, host_timeout * 1000);
190 relayd_add_route(host);
195 struct relayd_host *relayd_refresh_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
197 struct relayd_host *host;
199 host = find_host_by_ipaddr(rif, ipaddr);
201 host = find_host_by_ipaddr(NULL, ipaddr);
204 * When we suddenly see the host appearing on a different interface,
205 * reduce the timeout to make the old entry expire faster, in case the
207 * If the old entry is behind a managed interface, it will be pinged
208 * before we expire it
210 if (host && !host->cleanup_pending)
211 uloop_timeout_set(&host->timeout, 1);
213 host = add_host(rif, lladdr, ipaddr);
215 host->cleanup_pending = false;
216 uloop_timeout_set(&host->timeout, host_timeout * 1000);
222 static void relay_arp_request(struct relayd_interface *from_rif, struct arp_packet *pkt)
224 struct relayd_interface *rif;
225 struct arp_packet reqpkt;
227 memcpy(&reqpkt, pkt, sizeof(reqpkt));
228 list_for_each_entry(rif, &interfaces, list) {
232 memcpy(reqpkt.eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
233 memcpy(reqpkt.arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
235 DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
236 rif->ifname, IP_BUF(reqpkt.arp.arp_tpa),
237 IP_BUF(reqpkt.arp.arp_spa), MAC_BUF(reqpkt.eth.ether_shost));
239 sendto(rif->fd.fd, &reqpkt, sizeof(reqpkt), 0,
240 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
244 static void recv_arp_request(struct relayd_interface *rif, struct arp_packet *pkt)
246 struct relayd_host *host;
248 DPRINTF(2, "%s: ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
250 IP_BUF(pkt->arp.arp_tpa),
251 IP_BUF(pkt->arp.arp_spa),
252 MAC_BUF(pkt->eth.ether_shost));
254 if (!memcmp(pkt->arp.arp_spa, "\x00\x00\x00\x00", 4))
257 relayd_refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa);
259 host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
262 * If a host is being pinged because of a timeout, do not use the cached
263 * entry here. That way we can avoid giving out stale data in case the node
264 * has moved. We shouldn't relay requests here either, as we might miss our
265 * chance to create a host route.
267 if (host && host->cleanup_pending)
270 relay_arp_request(rif, pkt);
274 static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt)
276 struct relayd_host *host;
278 DPRINTF(2, "%s: received ARP reply for "IP_FMT" from "MAC_FMT", deliver to "IP_FMT"\n",
280 IP_BUF(pkt->arp.arp_spa),
281 MAC_BUF(pkt->eth.ether_shost),
282 IP_BUF(pkt->arp.arp_tpa));
284 relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
286 if (!memcmp(pkt->arp.arp_tpa, rif->src_ip, 4))
289 host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
293 if (host->rif == rif)
296 send_arp_reply(host->rif, pkt->arp.arp_spa, host->lladdr, host->ipaddr);
299 static void recv_packet(struct uloop_fd *fd, unsigned int events)
301 struct relayd_interface *rif = container_of(fd, struct relayd_interface, fd);
302 struct arp_packet *pkt;
303 static char pktbuf[4096];
310 pktlen = recv(rif->fd.fd, pktbuf, sizeof(pktbuf), 0);
321 pkt = (void *)pktbuf;
322 if (pkt->arp.arp_op == htons(ARPOP_REPLY))
323 recv_arp_reply(rif, pkt);
324 else if (pkt->arp.arp_op == htons(ARPOP_REQUEST))
325 recv_arp_request(rif, pkt);
327 DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt->arp.arp_op));
332 void relayd_forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len)
334 struct relayd_interface *rif;
335 struct ether_header *eth = packet;
337 list_for_each_entry(rif, &interfaces, list) {
341 DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif->ifname, rif->ifname);
342 memcpy(eth->ether_shost, rif->sll.sll_addr, ETH_ALEN);
343 send(rif->bcast_fd.fd, packet, len, 0);
347 static void recv_bcast_packet(struct uloop_fd *fd, unsigned int events)
349 struct relayd_interface *rif = container_of(fd, struct relayd_interface, bcast_fd);
350 static char pktbuf[4096];
357 pktlen = recv(rif->bcast_fd.fd, pktbuf, sizeof(pktbuf), 0);
368 if (!forward_bcast && !forward_dhcp)
371 if (relayd_handle_dhcp_packet(rif, pktbuf, pktlen, forward_dhcp))
375 relayd_forward_bcast_packet(rif, pktbuf, pktlen);
380 static int init_interface(struct relayd_interface *rif)
382 struct sockaddr_ll *sll = &rif->sll;
383 struct sockaddr_in *sin;
386 #ifdef PACKET_RECV_TYPE
387 unsigned int pkt_type;
390 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
396 memset(&ifr, 0, sizeof(ifr));
397 strcpy(ifr.ifr_name, rif->ifname);
399 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
400 perror("ioctl(SIOCGIFHWADDR)");
404 memcpy(sll->sll_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
405 sll->sll_family = AF_PACKET;
406 sll->sll_protocol = htons(ETH_P_ARP);
407 sll->sll_pkttype = PACKET_BROADCAST;
408 sll->sll_hatype = ARPHRD_ETHER;
409 sll->sll_halen = ETH_ALEN;
411 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
412 perror("ioctl(SIOCGIFINDEX)");
416 sll->sll_ifindex = ifr.ifr_ifindex;
418 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
419 memcpy(rif->src_ip, DUMMY_IP, sizeof(rif->src_ip));
421 sin = (struct sockaddr_in *) &ifr.ifr_addr;
422 memcpy(rif->src_ip, &sin->sin_addr.s_addr, sizeof(rif->src_ip));
425 if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
426 perror("bind(ETH_P_ARP)");
430 rif->fd.cb = recv_packet;
431 uloop_fd_add(&rif->fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
433 if (!forward_bcast && !forward_dhcp)
436 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
440 rif->bcast_fd.fd = fd;
441 rif->bcast_fd.cb = recv_bcast_packet;
443 memcpy(&rif->bcast_sll, &rif->sll, sizeof(rif->bcast_sll));
444 sll = &rif->bcast_sll;
445 sll->sll_protocol = htons(ETH_P_IP);
447 if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
448 perror("bind(ETH_P_IP)");
452 #ifdef PACKET_RECV_TYPE
453 pkt_type = (1 << PACKET_BROADCAST);
454 setsockopt(fd, SOL_PACKET, PACKET_RECV_TYPE, &pkt_type, sizeof(pkt_type));
457 uloop_fd_add(&rif->bcast_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
458 relayd_add_interface_routes(rif);
462 static int init_interfaces(void)
464 struct relayd_interface *rif;
467 list_for_each_entry(rif, &interfaces, list) {
468 ret = init_interface(rif);
476 static void cleanup_hosts(void)
478 struct relayd_interface *rif;
479 struct relayd_host *host, *tmp;
481 list_for_each_entry(rif, &interfaces, list) {
482 list_for_each_entry_safe(host, tmp, &rif->hosts, list) {
488 static void free_interfaces(void)
490 struct relayd_interface *rif, *rtmp;
492 list_for_each_entry_safe(rif, rtmp, &interfaces, list) {
493 relayd_del_interface_routes(rif);
494 list_del(&rif->list);
499 static int alloc_interface(const char *ifname, bool managed)
501 struct relayd_interface *rif;
503 if (strlen(ifname) >= IFNAMSIZ)
506 rif = calloc(1, sizeof(*rif));
510 INIT_LIST_HEAD(&rif->list);
511 INIT_LIST_HEAD(&rif->hosts);
512 strcpy(rif->ifname, ifname);
513 list_add(&rif->list, &interfaces);
514 rif->managed = managed;
519 static void die(int signo)
522 * When we hit SIGTERM, clean up interfaces directly, so that we
523 * won't leave our routing in an invalid state.
530 static int usage(const char *progname)
532 fprintf(stderr, "Usage: %s <options>\n"
535 " -d Enable debug messages\n"
536 " -i <ifname> Add an interface for relaying\n"
537 " -I <ifname> Same as -i, except with ARP cache and host route management\n"
538 " You need to specify at least two interfaces\n"
539 " -t <timeout> Host entry expiry timeout\n"
540 " -T <table> Set routing table number for automatically added routes\n"
541 " -B Enable broadcast forwarding\n"
542 " -D Enable DHCP forwarding\n"
548 int main(int argc, char **argv)
555 inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
557 perror("socket(AF_INET)");
565 while ((ch = getopt(argc, argv, "I:i:t:BDdT:")) != -1) {
572 if (alloc_interface(optarg, managed) < 0)
578 host_timeout = atoi(optarg);
579 if (host_timeout <= 0)
580 return usage(argv[0]);
592 route_table = atoi(optarg);
593 if (route_table <= 0)
594 return usage(argv[0]);
598 return usage(argv[0]);
602 if (list_empty(&interfaces))
603 return usage(argv[0]);
606 fprintf(stderr, "ERROR: Need at least 2 interfaces for relaying\n");
613 signal(SIGTERM, die);
615 signal(SIGUSR1, die);
616 signal(SIGUSR2, die);
618 if (relayd_rtnl_init() < 0)
621 if (init_interfaces() < 0)