c30526ef2cdd9963531f044ceaac09538d5f742d
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 static LIST_HEAD(pending_routes
);
35 LIST_HEAD(interfaces
);
38 static int host_timeout
;
40 static int forward_bcast
;
41 static int forward_dhcp
;
43 uint8_t local_addr
[4];
44 int local_route_table
;
46 struct relayd_pending_route
{
47 struct relayd_route rt
;
48 struct uloop_timeout timeout
;
52 static struct relayd_host
*find_host_by_ipaddr(struct relayd_interface
*rif
, const uint8_t *ipaddr
)
54 struct relayd_host
*host
;
57 list_for_each_entry(rif
, &interfaces
, list
) {
58 host
= find_host_by_ipaddr(rif
, ipaddr
);
67 list_for_each_entry(host
, &rif
->hosts
, list
) {
68 if (memcmp(ipaddr
, host
->ipaddr
, sizeof(host
->ipaddr
)) != 0)
76 static void add_arp(struct relayd_host
*host
)
78 struct sockaddr_in
*sin
;
81 strncpy(arp
.arp_dev
, host
->rif
->ifname
, sizeof(arp
.arp_dev
));
82 arp
.arp_flags
= ATF_COM
;
84 arp
.arp_ha
.sa_family
= ARPHRD_ETHER
;
85 memcpy(arp
.arp_ha
.sa_data
, host
->lladdr
, ETH_ALEN
);
87 sin
= (struct sockaddr_in
*) &arp
.arp_pa
;
88 sin
->sin_family
= AF_INET
;
89 memcpy(&sin
->sin_addr
, host
->ipaddr
, sizeof(host
->ipaddr
));
91 ioctl(inet_sock
, SIOCSARP
, &arp
);
94 static void timeout_host_route(struct uloop_timeout
*timeout
)
96 struct relayd_pending_route
*rt
;
98 rt
= container_of(timeout
, struct relayd_pending_route
, timeout
);
99 list_del(&rt
->rt
.list
);
103 void relayd_add_host_route(struct relayd_host
*host
, const uint8_t *dest
, uint8_t mask
)
105 struct relayd_route
*rt
;
107 list_for_each_entry(rt
, &host
->routes
, list
) {
108 if (!memcmp(rt
->dest
, dest
, sizeof(rt
->dest
)) && rt
->mask
== mask
)
112 rt
= calloc(1, sizeof(*rt
));
116 list_add(&rt
->list
, &host
->routes
);
117 memcpy(rt
->dest
, dest
, sizeof(rt
->dest
));
119 relayd_add_route(host
, rt
);
122 static void del_host(struct relayd_host
*host
)
124 struct relayd_route
*route
, *tmp
;
126 DPRINTF(1, "%s: deleting host "IP_FMT
" ("MAC_FMT
")\n", host
->rif
->ifname
,
127 IP_BUF(host
->ipaddr
), MAC_BUF(host
->lladdr
));
129 list_for_each_entry_safe(route
, tmp
, &host
->routes
, list
) {
130 relayd_del_route(host
, route
);
131 list_del(&route
->list
);
134 if (host
->rif
->managed
)
135 relayd_del_route(host
, NULL
);
136 uloop_timeout_cancel(&host
->timeout
);
137 list_del(&host
->list
);
141 static void fill_arp_request(struct arp_packet
*pkt
, struct relayd_interface
*rif
,
142 const uint8_t spa
[4], const uint8_t tpa
[4])
144 memset(pkt
, 0, sizeof(*pkt
));
146 pkt
->eth
.ether_type
= htons(ETHERTYPE_ARP
);
147 memcpy(pkt
->eth
.ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
149 memcpy(pkt
->arp
.arp_sha
, rif
->sll
.sll_addr
, ETH_ALEN
);
150 memcpy(pkt
->arp
.arp_spa
, spa
, 4);
151 memcpy(pkt
->arp
.arp_tpa
, tpa
, 4);
153 pkt
->arp
.arp_hrd
= htons(ARPHRD_ETHER
);
154 pkt
->arp
.arp_pro
= htons(ETH_P_IP
);
155 pkt
->arp
.arp_hln
= ETH_ALEN
;
156 pkt
->arp
.arp_pln
= 4;
159 static void send_arp_request(struct relayd_interface
*rif
, const uint8_t *ipaddr
)
161 struct arp_packet pkt
;
163 fill_arp_request(&pkt
, rif
, rif
->src_ip
, ipaddr
);
165 pkt
.arp
.arp_op
= htons(ARPOP_REQUEST
);
166 memcpy(pkt
.arp
.arp_spa
, rif
->src_ip
, ETH_ALEN
);
167 memset(pkt
.arp
.arp_tha
, 0, ETH_ALEN
);
168 memset(pkt
.eth
.ether_dhost
, 0xff, ETH_ALEN
);
170 DPRINTF(2, "%s: sending ARP who-has "IP_FMT
", tell "IP_FMT
" ("MAC_FMT
")\n",
171 rif
->ifname
, IP_BUF(pkt
.arp
.arp_tpa
),
172 IP_BUF(pkt
.arp
.arp_spa
), MAC_BUF(pkt
.eth
.ether_shost
));
174 sendto(rif
->fd
.fd
, &pkt
, sizeof(pkt
), 0,
175 (struct sockaddr
*) &rif
->sll
, sizeof(rif
->sll
));
178 void relayd_add_pending_route(const uint8_t *gateway
, const uint8_t *dest
, uint8_t mask
, int timeout
)
180 struct relayd_pending_route
*rt
;
181 struct relayd_interface
*rif
;
182 struct relayd_host
*host
;
184 host
= find_host_by_ipaddr(NULL
, gateway
);
186 relayd_add_host_route(host
, dest
, mask
);
190 rt
= calloc(1, sizeof(*rt
));
194 memcpy(rt
->gateway
, gateway
, sizeof(rt
->gateway
));
195 memcpy(rt
->rt
.dest
, dest
, sizeof(rt
->rt
.dest
));
197 list_add(&rt
->rt
.list
, &pending_routes
);
201 rt
->timeout
.cb
= timeout_host_route
;
202 uloop_timeout_set(&rt
->timeout
, 10000);
203 list_for_each_entry(rif
, &interfaces
, list
) {
204 send_arp_request(rif
, gateway
);
208 static void send_arp_reply(struct relayd_interface
*rif
, uint8_t spa
[4],
209 uint8_t tha
[ETH_ALEN
], uint8_t tpa
[4])
211 struct arp_packet pkt
;
213 fill_arp_request(&pkt
, rif
, spa
, tpa
);
215 pkt
.arp
.arp_op
= htons(ARPOP_REPLY
);
216 memcpy(pkt
.eth
.ether_dhost
, tha
, ETH_ALEN
);
217 memcpy(pkt
.arp
.arp_tha
, tha
, ETH_ALEN
);
219 DPRINTF(2, "%s: sending ARP reply to "IP_FMT
", "IP_FMT
" is at ("MAC_FMT
")\n",
220 rif
->ifname
, IP_BUF(pkt
.arp
.arp_tpa
),
221 IP_BUF(pkt
.arp
.arp_spa
), MAC_BUF(pkt
.eth
.ether_shost
));
223 sendto(rif
->fd
.fd
, &pkt
, sizeof(pkt
), 0,
224 (struct sockaddr
*) &rif
->sll
, sizeof(rif
->sll
));
227 static void host_entry_timeout(struct uloop_timeout
*timeout
)
229 struct relayd_host
*host
= container_of(timeout
, struct relayd_host
, timeout
);
232 * When a host is behind a managed interface, we must not expire its host
233 * entry prematurely, as this will cause routes to the node to expire,
234 * leading to loss of connectivity from the other side.
235 * When the timeout is reached, try pinging the host a few times before
238 if (host
->rif
->managed
&& host
->cleanup_pending
< 2) {
239 send_arp_request(host
->rif
, host
->ipaddr
);
240 host
->cleanup_pending
++;
241 uloop_timeout_set(&host
->timeout
, 1000);
247 static struct relayd_host
*add_host(struct relayd_interface
*rif
, const uint8_t *lladdr
, const uint8_t *ipaddr
)
249 struct relayd_host
*host
;
250 struct relayd_pending_route
*route
, *rtmp
;
252 DPRINTF(1, "%s: adding host "IP_FMT
" ("MAC_FMT
")\n", rif
->ifname
,
253 IP_BUF(ipaddr
), MAC_BUF(lladdr
));
255 host
= calloc(1, sizeof(*host
));
256 INIT_LIST_HEAD(&host
->routes
);
258 memcpy(host
->ipaddr
, ipaddr
, sizeof(host
->ipaddr
));
259 memcpy(host
->lladdr
, lladdr
, sizeof(host
->lladdr
));
260 list_add(&host
->list
, &rif
->hosts
);
261 host
->timeout
.cb
= host_entry_timeout
;
262 uloop_timeout_set(&host
->timeout
, host_timeout
* 1000);
266 relayd_add_route(host
, NULL
);
268 list_for_each_entry_safe(route
, rtmp
, &pending_routes
, rt
.list
) {
269 if (memcmp(route
->gateway
, ipaddr
, 4) != 0)
272 relayd_add_host_route(host
, route
->rt
.dest
, route
->rt
.mask
);
273 if (!route
->timeout
.pending
)
276 uloop_timeout_cancel(&route
->timeout
);
277 list_del(&route
->rt
.list
);
284 struct relayd_host
*relayd_refresh_host(struct relayd_interface
*rif
, const uint8_t *lladdr
, const uint8_t *ipaddr
)
286 struct relayd_host
*host
;
288 host
= find_host_by_ipaddr(rif
, ipaddr
);
290 host
= find_host_by_ipaddr(NULL
, ipaddr
);
293 * When we suddenly see the host appearing on a different interface,
294 * reduce the timeout to make the old entry expire faster, in case the
296 * If the old entry is behind a managed interface, it will be pinged
297 * before we expire it
299 if (host
&& !host
->cleanup_pending
) {
300 uloop_timeout_set(&host
->timeout
, 1);
304 host
= add_host(rif
, lladdr
, ipaddr
);
306 host
->cleanup_pending
= false;
307 uloop_timeout_set(&host
->timeout
, host_timeout
* 1000);
313 static void relay_arp_request(struct relayd_interface
*from_rif
, struct arp_packet
*pkt
)
315 struct relayd_interface
*rif
;
316 struct arp_packet reqpkt
;
318 memcpy(&reqpkt
, pkt
, sizeof(reqpkt
));
319 list_for_each_entry(rif
, &interfaces
, list
) {
323 memcpy(reqpkt
.eth
.ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
324 memcpy(reqpkt
.arp
.arp_sha
, rif
->sll
.sll_addr
, ETH_ALEN
);
326 DPRINTF(2, "%s: sending ARP who-has "IP_FMT
", tell "IP_FMT
" ("MAC_FMT
")\n",
327 rif
->ifname
, IP_BUF(reqpkt
.arp
.arp_tpa
),
328 IP_BUF(reqpkt
.arp
.arp_spa
), MAC_BUF(reqpkt
.eth
.ether_shost
));
330 sendto(rif
->fd
.fd
, &reqpkt
, sizeof(reqpkt
), 0,
331 (struct sockaddr
*) &rif
->sll
, sizeof(rif
->sll
));
335 static void recv_arp_request(struct relayd_interface
*rif
, struct arp_packet
*pkt
)
337 struct relayd_host
*host
;
339 DPRINTF(2, "%s: ARP who-has "IP_FMT
", tell "IP_FMT
" ("MAC_FMT
")\n",
341 IP_BUF(pkt
->arp
.arp_tpa
),
342 IP_BUF(pkt
->arp
.arp_spa
),
343 MAC_BUF(pkt
->eth
.ether_shost
));
345 if (!memcmp(pkt
->arp
.arp_spa
, "\x00\x00\x00\x00", 4))
348 if (local_route_table
&& !memcmp(pkt
->arp
.arp_tpa
, local_addr
, sizeof(local_addr
))) {
349 send_arp_reply(rif
, local_addr
, pkt
->arp
.arp_sha
, pkt
->arp
.arp_spa
);
353 relayd_refresh_host(rif
, pkt
->eth
.ether_shost
, pkt
->arp
.arp_spa
);
355 host
= find_host_by_ipaddr(NULL
, pkt
->arp
.arp_tpa
);
358 * If a host is being pinged because of a timeout, do not use the cached
359 * entry here. That way we can avoid giving out stale data in case the node
360 * has moved. We shouldn't relay requests here either, as we might miss our
361 * chance to create a host route.
363 if (host
&& host
->cleanup_pending
)
366 relay_arp_request(rif
, pkt
);
370 static void recv_arp_reply(struct relayd_interface
*rif
, struct arp_packet
*pkt
)
372 struct relayd_host
*host
;
374 DPRINTF(2, "%s: received ARP reply for "IP_FMT
" from "MAC_FMT
", deliver to "IP_FMT
"\n",
376 IP_BUF(pkt
->arp
.arp_spa
),
377 MAC_BUF(pkt
->eth
.ether_shost
),
378 IP_BUF(pkt
->arp
.arp_tpa
));
380 if (memcmp(pkt
->arp
.arp_sha
, rif
->sll
.sll_addr
, ETH_ALEN
) != 0)
381 relayd_refresh_host(rif
, pkt
->arp
.arp_sha
, pkt
->arp
.arp_spa
);
383 if (!memcmp(pkt
->arp
.arp_tpa
, rif
->src_ip
, 4))
386 host
= find_host_by_ipaddr(NULL
, pkt
->arp
.arp_tpa
);
390 if (host
->rif
== rif
)
393 send_arp_reply(host
->rif
, pkt
->arp
.arp_spa
, host
->lladdr
, host
->ipaddr
);
396 static void recv_packet(struct uloop_fd
*fd
, unsigned int events
)
398 struct relayd_interface
*rif
= container_of(fd
, struct relayd_interface
, fd
);
399 struct arp_packet
*pkt
;
400 static char pktbuf
[4096];
407 pktlen
= recv(rif
->fd
.fd
, pktbuf
, sizeof(pktbuf
), 0);
418 pkt
= (void *)pktbuf
;
419 if (pkt
->arp
.arp_op
== htons(ARPOP_REPLY
))
420 recv_arp_reply(rif
, pkt
);
421 else if (pkt
->arp
.arp_op
== htons(ARPOP_REQUEST
))
422 recv_arp_request(rif
, pkt
);
424 DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt
->arp
.arp_op
));
429 void relayd_forward_bcast_packet(struct relayd_interface
*from_rif
, void *packet
, int len
)
431 struct relayd_interface
*rif
;
432 struct ether_header
*eth
= packet
;
434 list_for_each_entry(rif
, &interfaces
, list
) {
438 DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif
->ifname
, rif
->ifname
);
439 memcpy(eth
->ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
440 send(rif
->bcast_fd
.fd
, packet
, len
, 0);
444 static void recv_bcast_packet(struct uloop_fd
*fd
, unsigned int events
)
446 struct relayd_interface
*rif
= container_of(fd
, struct relayd_interface
, bcast_fd
);
447 static char pktbuf
[4096];
454 pktlen
= recv(rif
->bcast_fd
.fd
, pktbuf
, sizeof(pktbuf
), 0);
465 if (!forward_bcast
&& !forward_dhcp
)
468 if (relayd_handle_dhcp_packet(rif
, pktbuf
, pktlen
, forward_dhcp
))
472 relayd_forward_bcast_packet(rif
, pktbuf
, pktlen
);
477 static int init_interface(struct relayd_interface
*rif
)
479 struct sockaddr_ll
*sll
= &rif
->sll
;
480 struct sockaddr_in
*sin
;
483 #ifdef PACKET_RECV_TYPE
484 unsigned int pkt_type
;
487 fd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_ARP
));
493 memset(&ifr
, 0, sizeof(ifr
));
494 strcpy(ifr
.ifr_name
, rif
->ifname
);
496 if (ioctl(fd
, SIOCGIFHWADDR
, &ifr
) < 0) {
497 perror("ioctl(SIOCGIFHWADDR)");
501 memcpy(sll
->sll_addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
502 sll
->sll_family
= AF_PACKET
;
503 sll
->sll_protocol
= htons(ETH_P_ARP
);
504 sll
->sll_pkttype
= PACKET_BROADCAST
;
505 sll
->sll_hatype
= ARPHRD_ETHER
;
506 sll
->sll_halen
= ETH_ALEN
;
508 if (ioctl(fd
, SIOCGIFINDEX
, &ifr
) < 0) {
509 perror("ioctl(SIOCGIFINDEX)");
513 sll
->sll_ifindex
= ifr
.ifr_ifindex
;
515 if (ioctl(fd
, SIOCGIFADDR
, &ifr
) < 0) {
516 memcpy(rif
->src_ip
, DUMMY_IP
, sizeof(rif
->src_ip
));
518 sin
= (struct sockaddr_in
*) &ifr
.ifr_addr
;
519 memcpy(rif
->src_ip
, &sin
->sin_addr
.s_addr
, sizeof(rif
->src_ip
));
522 if (bind(fd
, (struct sockaddr
*)sll
, sizeof(struct sockaddr_ll
)) < 0) {
523 perror("bind(ETH_P_ARP)");
527 rif
->fd
.cb
= recv_packet
;
528 uloop_fd_add(&rif
->fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
530 if (!forward_bcast
&& !forward_dhcp
)
533 fd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_IP
));
537 rif
->bcast_fd
.fd
= fd
;
538 rif
->bcast_fd
.cb
= recv_bcast_packet
;
540 memcpy(&rif
->bcast_sll
, &rif
->sll
, sizeof(rif
->bcast_sll
));
541 sll
= &rif
->bcast_sll
;
542 sll
->sll_protocol
= htons(ETH_P_IP
);
544 if (bind(fd
, (struct sockaddr
*)sll
, sizeof(struct sockaddr_ll
)) < 0) {
545 perror("bind(ETH_P_IP)");
549 #ifdef PACKET_RECV_TYPE
550 pkt_type
= (1 << PACKET_BROADCAST
);
551 setsockopt(fd
, SOL_PACKET
, PACKET_RECV_TYPE
, &pkt_type
, sizeof(pkt_type
));
554 uloop_fd_add(&rif
->bcast_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
555 relayd_add_interface_routes(rif
);
559 static void ping_static_routes(void)
561 struct relayd_pending_route
*rt
;
562 struct relayd_interface
*rif
;
564 list_for_each_entry(rt
, &pending_routes
, rt
.list
)
565 list_for_each_entry(rif
, &interfaces
, list
)
566 send_arp_request(rif
, rt
->gateway
);
569 static int init_interfaces(void)
571 struct relayd_interface
*rif
;
574 list_for_each_entry(rif
, &interfaces
, list
) {
575 ret
= init_interface(rif
);
583 static void cleanup_hosts(void)
585 struct relayd_interface
*rif
;
586 struct relayd_host
*host
, *tmp
;
588 list_for_each_entry(rif
, &interfaces
, list
) {
589 list_for_each_entry_safe(host
, tmp
, &rif
->hosts
, list
) {
595 static void free_interfaces(void)
597 struct relayd_interface
*rif
, *rtmp
;
599 list_for_each_entry_safe(rif
, rtmp
, &interfaces
, list
) {
600 relayd_del_interface_routes(rif
);
601 list_del(&rif
->list
);
606 static struct relayd_interface
*alloc_interface(const char *ifname
, bool managed
)
608 struct relayd_interface
*rif
;
610 if (strlen(ifname
) >= IFNAMSIZ
)
613 rif
= calloc(1, sizeof(*rif
));
617 INIT_LIST_HEAD(&rif
->hosts
);
618 strcpy(rif
->ifname
, ifname
);
619 list_add(&rif
->list
, &interfaces
);
620 rif
->managed
= managed
;
625 static void die(int signo
)
628 * When we hit SIGTERM, clean up interfaces directly, so that we
629 * won't leave our routing in an invalid state.
636 static int usage(const char *progname
)
638 fprintf(stderr
, "Usage: %s <options>\n"
641 " -d Enable debug messages\n"
642 " -i <ifname> Add an interface for relaying\n"
643 " -I <ifname> Same as -i, except with ARP cache and host route management\n"
644 " You need to specify at least two interfaces\n"
645 " -G <ip> Set a gateway IP for clients\n"
646 " -R <gateway>:<net>/<mask>\n"
647 " Add a static route for <net>/<mask> via <gateway>\n"
648 " -t <timeout> Host entry expiry timeout\n"
649 " -T <table> Set routing table number for automatically added routes\n"
650 " -B Enable broadcast forwarding\n"
651 " -D Enable DHCP forwarding\n"
652 " -L <ipaddr> Enable local access using <ipaddr> as source address\n"
658 int main(int argc
, char **argv
)
660 struct relayd_interface
*rif
= NULL
;
661 struct in_addr addr
, addr2
;
662 bool local_addr_valid
= false;
670 inet_sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
672 perror("socket(AF_INET)");
678 local_route_table
= 0;
681 while ((ch
= getopt(argc
, argv
, "I:i:t:BDdT:G:R:L:")) != -1) {
688 rif
= alloc_interface(optarg
, managed
);
695 host_timeout
= atoi(optarg
);
696 if (host_timeout
<= 0)
697 return usage(argv
[0]);
709 route_table
= atoi(optarg
);
710 if (route_table
<= 0)
711 return usage(argv
[0]);
714 if (!inet_aton(optarg
, &addr
)) {
715 fprintf(stderr
, "Address '%s' not found\n", optarg
);
718 relayd_add_pending_route((uint8_t *) &addr
.s_addr
, (const uint8_t *) "\x00\x00\x00\x00", 0, 0);
721 if (!inet_aton(optarg
, &addr
)) {
722 fprintf(stderr
, "Address '%s' not found\n", optarg
);
725 memcpy(&local_addr
, &addr
.s_addr
, sizeof(local_addr
));
726 local_addr_valid
= true;
729 s
= strchr(optarg
, ':');
731 return usage(argv
[0]);
734 if (!inet_aton(optarg
, &addr
)) {
735 fprintf(stderr
, "Address '%s' not found\n", optarg
);
741 return usage(argv
[0]);
744 if (!inet_aton(s
, &addr2
)) {
745 fprintf(stderr
, "Address '%s' not found\n", s
);
750 if (mask
< 0 || mask
> 32)
751 return usage(argv
[0]);
753 relayd_add_pending_route((uint8_t *) &addr
.s_addr
, (uint8_t *) &addr2
.s_addr
, mask
, 0);
757 return usage(argv
[0]);
761 if (list_empty(&interfaces
))
762 return usage(argv
[0]);
765 fprintf(stderr
, "ERROR: Need at least 2 interfaces for relaying\n");
772 signal(SIGTERM
, die
);
774 signal(SIGUSR1
, die
);
775 signal(SIGUSR2
, die
);
777 if (local_addr_valid
)
778 local_route_table
= route_table
++;
780 if (relayd_rtnl_init() < 0)
783 if (init_interfaces() < 0)
786 ping_static_routes();