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>
47 #define DPRINTF(level, ...) if (debug >= level) fprintf(stderr, __VA_ARGS__);
49 #define DPRINTF(...) do {} while(0)
53 #define __packed __attribute__((packed))
56 #define __uc(c) ((unsigned char *)(c))
58 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
59 #define MAC_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3], __uc(_c)[4], __uc(_c)[5]
61 #define IP_FMT "%d.%d.%d.%d"
62 #define IP_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3]
64 #define DUMMY_IP ((uint8_t *) "\x01\x01\x01\x01")
66 #define DHCP_FLAG_BROADCAST (1 << 15)
68 struct relayd_interface
{
69 struct list_head list
;
71 struct uloop_fd bcast_fd
;
72 struct sockaddr_ll sll
;
73 struct sockaddr_ll bcast_sll
;
74 char ifname
[IFNAMSIZ
];
75 struct list_head hosts
;
81 struct list_head list
;
82 struct relayd_interface
*rif
;
83 uint8_t lladdr
[ETH_ALEN
];
85 struct uloop_timeout timeout
;
90 struct ether_header eth
;
95 struct ether_header eth
;
100 uint8_t op
, htype
, hlen
, hops
;
102 uint16_t secs
, flags
;
103 struct in_addr ciaddr
, yiaddr
, siaddr
, giaddr
;
104 unsigned char chaddr
[16];
105 unsigned char sname
[64];
106 unsigned char file
[128];
115 static LIST_HEAD(interfaces
);
116 static int host_timeout
;
117 static int inet_sock
;
118 static int forward_bcast
;
119 static int forward_dhcp
;
120 static struct uloop_fd rtnl_sock
;
121 static unsigned int rtnl_seq
, rtnl_dump_seq
;
123 static struct relayd_host
*find_host_by_ipaddr(struct relayd_interface
*rif
, const uint8_t *ipaddr
)
125 struct relayd_host
*host
;
128 list_for_each_entry(rif
, &interfaces
, list
) {
129 host
= find_host_by_ipaddr(rif
, ipaddr
);
138 list_for_each_entry(host
, &rif
->hosts
, list
) {
139 if (memcmp(ipaddr
, host
->ipaddr
, sizeof(host
->ipaddr
)) != 0)
147 static void add_arp(struct relayd_host
*host
)
149 struct sockaddr_in
*sin
;
152 strncpy(arp
.arp_dev
, host
->rif
->ifname
, sizeof(arp
.arp_dev
));
153 arp
.arp_flags
= ATF_COM
;
155 arp
.arp_ha
.sa_family
= ARPHRD_ETHER
;
156 memcpy(arp
.arp_ha
.sa_data
, host
->lladdr
, ETH_ALEN
);
158 sin
= (struct sockaddr_in
*) &arp
.arp_pa
;
159 sin
->sin_family
= AF_INET
;
160 memcpy(&sin
->sin_addr
, host
->ipaddr
, sizeof(host
->ipaddr
));
162 ioctl(inet_sock
, SIOCSARP
, &arp
);
165 static void rtnl_route_set(struct relayd_host
*host
, bool add
)
180 memset(&req
, 0, sizeof(req
));
182 req
.nl
.nlmsg_len
= sizeof(req
);
183 req
.rt
.rtm_family
= AF_INET
;
184 req
.rt
.rtm_dst_len
= 32;
186 req
.dst
.rta
.rta_type
= RTA_DST
;
187 req
.dst
.rta
.rta_len
= sizeof(req
.dst
);
188 memcpy(req
.dst
.ipaddr
, host
->ipaddr
, sizeof(req
.dst
.ipaddr
));
190 req
.dev
.rta
.rta_type
= RTA_OIF
;
191 req
.dev
.rta
.rta_len
= sizeof(req
.dev
);
192 req
.dev
.ifindex
= host
->rif
->sll
.sll_ifindex
;
194 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
195 req
.rt
.rtm_table
= RT_TABLE_MAIN
;
197 req
.nl
.nlmsg_type
= RTM_NEWROUTE
;
198 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_REPLACE
;
200 req
.rt
.rtm_protocol
= RTPROT_BOOT
;
201 req
.rt
.rtm_scope
= RT_SCOPE_LINK
;
202 req
.rt
.rtm_type
= RTN_UNICAST
;
204 req
.nl
.nlmsg_type
= RTM_DELROUTE
;
205 req
.rt
.rtm_scope
= RT_SCOPE_NOWHERE
;
208 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
211 static void add_route(struct relayd_host
*host
)
213 rtnl_route_set(host
, true);
216 static void del_route(struct relayd_host
*host
)
218 rtnl_route_set(host
, false);
221 static void del_host(struct relayd_host
*host
)
223 DPRINTF(1, "%s: deleting host "IP_FMT
" ("MAC_FMT
")\n", host
->rif
->ifname
,
224 IP_BUF(host
->ipaddr
), MAC_BUF(host
->lladdr
));
226 if (host
->rif
->managed
)
228 list_del(&host
->list
);
232 static void fill_arp_request(struct arp_packet
*pkt
, struct relayd_interface
*rif
,
233 uint8_t spa
[4], uint8_t tpa
[4])
235 memset(pkt
, 0, sizeof(*pkt
));
237 pkt
->eth
.ether_type
= htons(ETHERTYPE_ARP
);
238 memcpy(pkt
->eth
.ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
240 memcpy(pkt
->arp
.arp_sha
, rif
->sll
.sll_addr
, ETH_ALEN
);
241 memcpy(pkt
->arp
.arp_spa
, spa
, 4);
242 memcpy(pkt
->arp
.arp_tpa
, tpa
, 4);
244 pkt
->arp
.arp_hrd
= htons(ARPHRD_ETHER
);
245 pkt
->arp
.arp_pro
= htons(ETH_P_IP
);
246 pkt
->arp
.arp_hln
= ETH_ALEN
;
247 pkt
->arp
.arp_pln
= 4;
250 static void send_arp_request(struct relayd_host
*host
)
252 struct relayd_interface
*rif
= host
->rif
;
253 struct arp_packet pkt
;
255 fill_arp_request(&pkt
, host
->rif
, host
->rif
->src_ip
, host
->ipaddr
);
257 pkt
.arp
.arp_op
= htons(ARPOP_REQUEST
);
258 memcpy(pkt
.arp
.arp_spa
, rif
->src_ip
, ETH_ALEN
);
259 memset(pkt
.arp
.arp_tha
, 0, ETH_ALEN
);
260 memset(pkt
.eth
.ether_dhost
, 0xff, ETH_ALEN
);
262 DPRINTF(2, "%s: sending ARP who-has "IP_FMT
", tell "IP_FMT
" ("MAC_FMT
")\n",
263 rif
->ifname
, IP_BUF(pkt
.arp
.arp_tpa
),
264 IP_BUF(pkt
.arp
.arp_spa
), MAC_BUF(pkt
.eth
.ether_shost
));
266 sendto(rif
->fd
.fd
, &pkt
, sizeof(pkt
), 0,
267 (struct sockaddr
*) &rif
->sll
, sizeof(rif
->sll
));
270 static void send_arp_reply(struct relayd_interface
*rif
, uint8_t spa
[4],
271 uint8_t tha
[ETH_ALEN
], uint8_t tpa
[4])
273 struct arp_packet pkt
;
275 fill_arp_request(&pkt
, rif
, spa
, tpa
);
277 pkt
.arp
.arp_op
= htons(ARPOP_REPLY
);
278 memcpy(pkt
.eth
.ether_dhost
, tha
, ETH_ALEN
);
279 memcpy(pkt
.arp
.arp_tha
, tha
, ETH_ALEN
);
281 DPRINTF(2, "%s: sending ARP reply to "IP_FMT
", "IP_FMT
" is at ("MAC_FMT
")\n",
282 rif
->ifname
, IP_BUF(pkt
.arp
.arp_tpa
),
283 IP_BUF(pkt
.arp
.arp_spa
), MAC_BUF(pkt
.eth
.ether_shost
));
285 sendto(rif
->fd
.fd
, &pkt
, sizeof(pkt
), 0,
286 (struct sockaddr
*) &rif
->sll
, sizeof(rif
->sll
));
289 static void host_entry_timeout(struct uloop_timeout
*timeout
)
291 struct relayd_host
*host
= container_of(timeout
, struct relayd_host
, timeout
);
294 * When a host is behind a managed interface, we must not expire its host
295 * entry prematurely, as this will cause routes to the node to expire,
296 * leading to loss of connectivity from the other side.
297 * When the timeout is reached, try pinging the host a few times before
300 if (host
->rif
->managed
&& host
->cleanup_pending
< 2) {
301 send_arp_request(host
);
302 host
->cleanup_pending
++;
303 uloop_timeout_set(&host
->timeout
, 1000);
309 static struct relayd_host
*add_host(struct relayd_interface
*rif
, const uint8_t *lladdr
, const uint8_t *ipaddr
)
311 struct relayd_host
*host
;
313 DPRINTF(1, "%s: adding host "IP_FMT
" ("MAC_FMT
")\n", rif
->ifname
,
314 IP_BUF(ipaddr
), MAC_BUF(lladdr
));
316 host
= calloc(1, sizeof(*host
));
318 memcpy(host
->ipaddr
, ipaddr
, sizeof(host
->ipaddr
));
319 memcpy(host
->lladdr
, lladdr
, sizeof(host
->lladdr
));
320 list_add(&host
->list
, &rif
->hosts
);
321 host
->timeout
.cb
= host_entry_timeout
;
322 uloop_timeout_set(&host
->timeout
, host_timeout
* 1000);
331 static struct relayd_host
*refresh_host(struct relayd_interface
*rif
, const uint8_t *lladdr
, const uint8_t *ipaddr
)
333 struct relayd_host
*host
;
335 host
= find_host_by_ipaddr(rif
, ipaddr
);
337 host
= find_host_by_ipaddr(NULL
, ipaddr
);
340 * When we suddenly see the host appearing on a different interface,
341 * reduce the timeout to make the old entry expire faster, in case the
343 * If the old entry is behind a managed interface, it will be pinged
344 * before we expire it
346 if (host
&& !host
->cleanup_pending
)
347 uloop_timeout_set(&host
->timeout
, 1);
349 host
= add_host(rif
, lladdr
, ipaddr
);
351 host
->cleanup_pending
= false;
352 uloop_timeout_set(&host
->timeout
, host_timeout
* 1000);
358 static void relay_arp_request(struct relayd_interface
*from_rif
, struct arp_packet
*pkt
)
360 struct relayd_interface
*rif
;
361 struct arp_packet reqpkt
;
363 memcpy(&reqpkt
, pkt
, sizeof(reqpkt
));
364 list_for_each_entry(rif
, &interfaces
, list
) {
368 memcpy(reqpkt
.eth
.ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
369 memcpy(reqpkt
.arp
.arp_sha
, rif
->sll
.sll_addr
, ETH_ALEN
);
371 DPRINTF(2, "%s: sending ARP who-has "IP_FMT
", tell "IP_FMT
" ("MAC_FMT
")\n",
372 rif
->ifname
, IP_BUF(reqpkt
.arp
.arp_tpa
),
373 IP_BUF(reqpkt
.arp
.arp_spa
), MAC_BUF(reqpkt
.eth
.ether_shost
));
375 sendto(rif
->fd
.fd
, &reqpkt
, sizeof(reqpkt
), 0,
376 (struct sockaddr
*) &rif
->sll
, sizeof(rif
->sll
));
380 static void recv_arp_request(struct relayd_interface
*rif
, struct arp_packet
*pkt
)
382 struct relayd_host
*host
;
384 DPRINTF(2, "%s: ARP who-has "IP_FMT
", tell "IP_FMT
" ("MAC_FMT
")\n",
386 IP_BUF(pkt
->arp
.arp_tpa
),
387 IP_BUF(pkt
->arp
.arp_spa
),
388 MAC_BUF(pkt
->eth
.ether_shost
));
390 if (!memcmp(pkt
->arp
.arp_spa
, "\x00\x00\x00\x00", 4))
393 refresh_host(rif
, pkt
->eth
.ether_shost
, pkt
->arp
.arp_spa
);
395 host
= find_host_by_ipaddr(NULL
, pkt
->arp
.arp_tpa
);
398 * If a host is being pinged because of a timeout, do not use the cached
399 * entry here. That way we can avoid giving out stale data in case the node
400 * has moved. We shouldn't relay requests here either, as we might miss our
401 * chance to create a host route.
403 if (host
&& host
->cleanup_pending
)
406 relay_arp_request(rif
, pkt
);
410 static void recv_arp_reply(struct relayd_interface
*rif
, struct arp_packet
*pkt
)
412 struct relayd_host
*host
;
414 DPRINTF(2, "%s: received ARP reply for "IP_FMT
" from "MAC_FMT
", deliver to "IP_FMT
"\n",
416 IP_BUF(pkt
->arp
.arp_spa
),
417 MAC_BUF(pkt
->eth
.ether_shost
),
418 IP_BUF(pkt
->arp
.arp_tpa
));
420 refresh_host(rif
, pkt
->arp
.arp_sha
, pkt
->arp
.arp_spa
);
422 if (!memcmp(pkt
->arp
.arp_tpa
, rif
->src_ip
, 4))
425 host
= find_host_by_ipaddr(NULL
, pkt
->arp
.arp_tpa
);
429 if (host
->rif
== rif
)
432 send_arp_reply(host
->rif
, pkt
->arp
.arp_spa
, host
->lladdr
, host
->ipaddr
);
435 static void recv_packet(struct uloop_fd
*fd
, unsigned int events
)
437 struct relayd_interface
*rif
= container_of(fd
, struct relayd_interface
, fd
);
438 struct arp_packet
*pkt
;
439 static char pktbuf
[4096];
446 pktlen
= recv(rif
->fd
.fd
, pktbuf
, sizeof(pktbuf
), 0);
457 pkt
= (void *)pktbuf
;
458 if (pkt
->arp
.arp_op
== htons(ARPOP_REPLY
))
459 recv_arp_reply(rif
, pkt
);
460 else if (pkt
->arp
.arp_op
== htons(ARPOP_REQUEST
))
461 recv_arp_request(rif
, pkt
);
463 DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt
->arp
.arp_op
));
468 static void forward_bcast_packet(struct relayd_interface
*from_rif
, void *packet
, int len
)
470 struct relayd_interface
*rif
;
471 struct ether_header
*eth
= packet
;
473 list_for_each_entry(rif
, &interfaces
, list
) {
477 DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif
->ifname
, rif
->ifname
);
478 memcpy(eth
->ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
479 send(rif
->bcast_fd
.fd
, packet
, len
, 0);
484 chksum(uint16_t sum
, const uint8_t *data
, uint16_t len
)
489 last
= data
+ len
- 1;
492 t
= (data
[0] << 8) + data
[1];
500 t
= (data
[0] << 8) + 0;
509 static bool forward_dhcp_packet(struct relayd_interface
*rif
, void *data
, int len
)
511 struct ip_packet
*pkt
= data
;
513 struct dhcp_header
*dhcp
;
517 if (pkt
->eth
.ether_type
!= htons(ETH_P_IP
))
520 if (pkt
->iph
.version
!= 4)
523 if (pkt
->iph
.protocol
!= IPPROTO_UDP
)
526 udp
= (void *) ((char *) &pkt
->iph
+ (pkt
->iph
.ihl
<< 2));
527 dhcp
= (void *) (udp
+ 1);
529 udplen
= ntohs(udp
->len
);
530 if (udplen
> len
- ((char *) udp
- (char *) data
))
533 if (udp
->dest
!= htons(67) && udp
->source
!= htons(67))
536 if (dhcp
->op
!= 1 && dhcp
->op
!= 2)
543 refresh_host(rif
, pkt
->eth
.ether_shost
, (void *) &pkt
->iph
.saddr
);
545 DPRINTF(2, "%s: handling DHCP %s\n", rif
->ifname
, (dhcp
->op
== 1 ? "request" : "response"));
547 dhcp
->flags
|= htons(DHCP_FLAG_BROADCAST
);
550 sum
= udplen
+ IPPROTO_UDP
;
551 sum
= chksum(sum
, (void *) &pkt
->iph
.saddr
, 8);
552 sum
= chksum(sum
, (void *) udp
, udplen
);
556 udp
->check
= htons(~sum
);
558 forward_bcast_packet(rif
, data
, len
);
563 static void recv_bcast_packet(struct uloop_fd
*fd
, unsigned int events
)
565 struct relayd_interface
*rif
= container_of(fd
, struct relayd_interface
, bcast_fd
);
566 static char pktbuf
[4096];
573 pktlen
= recv(rif
->bcast_fd
.fd
, pktbuf
, sizeof(pktbuf
), 0);
584 if (!forward_bcast
&& !forward_dhcp
)
587 if (forward_dhcp_packet(rif
, pktbuf
, pktlen
))
591 forward_bcast_packet(rif
, pktbuf
, pktlen
);
596 static int init_interface(struct relayd_interface
*rif
)
598 struct sockaddr_ll
*sll
= &rif
->sll
;
599 struct sockaddr_in
*sin
;
602 #ifdef PACKET_RECV_TYPE
603 unsigned int pkt_type
;
606 fd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_ARP
));
612 memset(&ifr
, 0, sizeof(ifr
));
613 strcpy(ifr
.ifr_name
, rif
->ifname
);
615 if (ioctl(fd
, SIOCGIFHWADDR
, &ifr
) < 0) {
616 perror("ioctl(SIOCGIFHWADDR)");
620 memcpy(sll
->sll_addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
621 sll
->sll_family
= AF_PACKET
;
622 sll
->sll_protocol
= htons(ETH_P_ARP
);
623 sll
->sll_pkttype
= PACKET_BROADCAST
;
624 sll
->sll_hatype
= ARPHRD_ETHER
;
625 sll
->sll_halen
= ETH_ALEN
;
627 if (ioctl(fd
, SIOCGIFINDEX
, &ifr
) < 0) {
628 perror("ioctl(SIOCGIFINDEX)");
632 sll
->sll_ifindex
= ifr
.ifr_ifindex
;
634 if (ioctl(fd
, SIOCGIFADDR
, &ifr
) < 0) {
635 memcpy(rif
->src_ip
, DUMMY_IP
, sizeof(rif
->src_ip
));
637 sin
= (struct sockaddr_in
*) &ifr
.ifr_addr
;
638 memcpy(rif
->src_ip
, &sin
->sin_addr
.s_addr
, sizeof(rif
->src_ip
));
641 if (bind(fd
, (struct sockaddr
*)sll
, sizeof(struct sockaddr_ll
)) < 0) {
642 perror("bind(ETH_P_ARP)");
646 rif
->fd
.cb
= recv_packet
;
647 uloop_fd_add(&rif
->fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
649 if (!forward_bcast
&& !forward_dhcp
)
652 fd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_IP
));
656 rif
->bcast_fd
.fd
= fd
;
657 rif
->bcast_fd
.cb
= recv_bcast_packet
;
659 memcpy(&rif
->bcast_sll
, &rif
->sll
, sizeof(rif
->bcast_sll
));
660 sll
= &rif
->bcast_sll
;
661 sll
->sll_protocol
= htons(ETH_P_IP
);
663 if (bind(fd
, (struct sockaddr
*)sll
, sizeof(struct sockaddr_ll
)) < 0) {
664 perror("bind(ETH_P_IP)");
668 #ifdef PACKET_RECV_TYPE
669 pkt_type
= (1 << PACKET_BROADCAST
);
670 setsockopt(fd
, SOL_PACKET
, PACKET_RECV_TYPE
, &pkt_type
, sizeof(pkt_type
));
673 uloop_fd_add(&rif
->bcast_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
677 static int init_interfaces(void)
679 struct relayd_interface
*rif
;
682 list_for_each_entry(rif
, &interfaces
, list
) {
683 ret
= init_interface(rif
);
691 static void del_interface(struct relayd_interface
*rif
)
693 struct relayd_host
*host
, *htmp
;
695 list_for_each_entry_safe(host
, htmp
, &rif
->hosts
, list
) {
701 static void cleanup_interfaces(void)
703 struct relayd_interface
*rif
, *rtmp
;
705 list_for_each_entry_safe(rif
, rtmp
, &interfaces
, list
) {
710 static int alloc_interface(const char *ifname
, bool managed
)
712 struct relayd_interface
*rif
;
714 if (strlen(ifname
) >= IFNAMSIZ
)
717 rif
= calloc(1, sizeof(*rif
));
721 INIT_LIST_HEAD(&rif
->list
);
722 INIT_LIST_HEAD(&rif
->hosts
);
723 strcpy(rif
->ifname
, ifname
);
724 list_add(&rif
->list
, &interfaces
);
725 rif
->managed
= managed
;
732 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
735 static void rtnl_parse_newneigh(struct nlmsghdr
*h
)
737 struct relayd_interface
*rif
= NULL
;
738 struct ndmsg
*r
= NLMSG_DATA(h
);
739 const uint8_t *lladdr
= NULL
;
740 const uint8_t *ipaddr
= NULL
;
744 if (r
->ndm_family
!= AF_INET
)
747 list_for_each_entry(rif
, &interfaces
, list
) {
748 if (rif
->sll
.sll_ifindex
== r
->ndm_ifindex
)
749 goto found_interface
;
754 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
));
755 for (rta
= NDA_RTA(r
); RTA_OK(rta
, len
); rta
= RTA_NEXT(rta
, len
)) {
756 switch(rta
->rta_type
) {
758 lladdr
= RTA_DATA(rta
);
761 ipaddr
= RTA_DATA(rta
);
768 if (!lladdr
|| !ipaddr
|| (r
->ndm_state
& (NUD_INCOMPLETE
|NUD_FAILED
)))
771 if (!memcmp(lladdr
, "\x00\x00\x00\x00\x00\x00", ETH_ALEN
))
774 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT
" ("MAC_FMT
")\n",
775 rif
->ifname
, IP_BUF(ipaddr
), MAC_BUF(lladdr
));
776 refresh_host(rif
, lladdr
, ipaddr
);
779 static void rtnl_parse_packet(void *data
, int len
)
783 for (h
= data
; NLMSG_OK(h
, len
); h
= NLMSG_NEXT(h
, len
)) {
784 if (h
->nlmsg_type
== NLMSG_DONE
||
785 h
->nlmsg_type
== NLMSG_ERROR
)
788 if (h
->nlmsg_seq
!= rtnl_dump_seq
)
791 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
792 rtnl_parse_newneigh(h
);
796 static void rtnl_cb(struct uloop_fd
*fd
, unsigned int events
)
798 struct sockaddr_nl nladdr
;
799 static uint8_t buf
[16384];
802 .iov_len
= sizeof(buf
),
804 struct msghdr msg
= {
806 .msg_namelen
= sizeof(nladdr
),
814 len
= recvmsg(rtnl_sock
.fd
, &msg
, 0);
825 if (nladdr
.nl_pid
!= 0)
828 rtnl_parse_packet(buf
, len
);
832 static int rtnl_init(void)
834 struct sockaddr_nl snl_local
;
840 .nlmsg_len
= sizeof(req
),
841 .nlmsg_type
= RTM_GETNEIGH
,
842 .nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
,
845 .g
.rtgen_family
= AF_INET
,
848 rtnl_sock
.fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
849 if (rtnl_sock
.fd
< 0) {
850 perror("socket(AF_NETLINK)");
854 snl_local
.nl_family
= AF_NETLINK
;
856 if (bind(rtnl_sock
.fd
, (struct sockaddr
*) &snl_local
, sizeof(struct sockaddr_nl
)) < 0) {
862 rtnl_sock
.cb
= rtnl_cb
;
863 uloop_fd_add(&rtnl_sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
865 rtnl_seq
= time(NULL
);
866 rtnl_dump_seq
= rtnl_seq
;
867 req
.nlh
.nlmsg_seq
= rtnl_seq
;
868 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
873 static void die(int signo
)
876 * When we hit SIGTERM, clean up interfaces directly, so that we
877 * won't leave our routing in an invalid state.
879 cleanup_interfaces();
883 static int usage(const char *progname
)
885 fprintf(stderr
, "Usage: %s <options>\n"
888 " -d Enable debug messages\n"
889 " -i <ifname> Add an interface for relaying\n"
890 " -I <ifname> Same as -i, except with ARP cache and host route management\n"
891 " You need to specify at least two interfaces\n"
892 " -t <timeout> Host entry expiry timeout\n"
893 " -B Enable broadcast forwarding\n"
894 " -D Enable DHCP forwarding\n"
900 int main(int argc
, char **argv
)
907 inet_sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
909 perror("socket(AF_INET)");
917 while ((ch
= getopt(argc
, argv
, "I:i:t:BDd")) != -1) {
924 if (alloc_interface(optarg
, managed
) < 0)
930 host_timeout
= atoi(optarg
);
931 if (host_timeout
<= 0)
932 return usage(argv
[0]);
945 return usage(argv
[0]);
949 if (list_empty(&interfaces
))
950 return usage(argv
[0]);
953 fprintf(stderr
, "ERROR: Need at least 2 interfaces for relaying\n");
960 signal(SIGTERM
, die
);
962 signal(SIGUSR1
, die
);
963 signal(SIGUSR2
, die
);
965 if (init_interfaces() < 0)
974 cleanup_interfaces();
975 uloop_fd_delete(&rtnl_sock
);