a29be2c1e733830c787e7680ac3faeba2e25eb4f
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 send_arp_reply(host
->rif
, pkt
->arp
.arp_spa
, host
->lladdr
, host
->ipaddr
);
432 static void recv_packet(struct uloop_fd
*fd
, unsigned int events
)
434 struct relayd_interface
*rif
= container_of(fd
, struct relayd_interface
, fd
);
435 struct arp_packet
*pkt
;
436 static char pktbuf
[4096];
443 pktlen
= recv(rif
->fd
.fd
, pktbuf
, sizeof(pktbuf
), 0);
454 pkt
= (void *)pktbuf
;
455 if (pkt
->arp
.arp_op
== htons(ARPOP_REPLY
))
456 recv_arp_reply(rif
, pkt
);
457 else if (pkt
->arp
.arp_op
== htons(ARPOP_REQUEST
))
458 recv_arp_request(rif
, pkt
);
460 DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt
->arp
.arp_op
));
465 static void forward_bcast_packet(struct relayd_interface
*from_rif
, void *packet
, int len
)
467 struct relayd_interface
*rif
;
468 struct ether_header
*eth
= packet
;
470 list_for_each_entry(rif
, &interfaces
, list
) {
474 DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif
->ifname
, rif
->ifname
);
475 memcpy(eth
->ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
476 send(rif
->bcast_fd
.fd
, packet
, len
, 0);
481 chksum(uint16_t sum
, const uint8_t *data
, uint16_t len
)
486 last
= data
+ len
- 1;
489 t
= (data
[0] << 8) + data
[1];
497 t
= (data
[0] << 8) + 0;
506 static bool forward_dhcp_packet(struct relayd_interface
*rif
, void *data
, int len
)
508 struct ip_packet
*pkt
= data
;
510 struct dhcp_header
*dhcp
;
514 if (pkt
->eth
.ether_type
!= htons(ETH_P_IP
))
517 if (pkt
->iph
.version
!= 4)
520 if (pkt
->iph
.protocol
!= IPPROTO_UDP
)
523 udp
= (void *) ((char *) &pkt
->iph
+ (pkt
->iph
.ihl
<< 2));
524 dhcp
= (void *) (udp
+ 1);
526 udplen
= ntohs(udp
->len
);
527 if (udplen
> len
- ((char *) udp
- (char *) data
))
530 if (udp
->dest
!= htons(67) && udp
->source
!= htons(67))
533 if (dhcp
->op
!= 1 && dhcp
->op
!= 2)
540 refresh_host(rif
, pkt
->eth
.ether_shost
, (void *) &pkt
->iph
.saddr
);
542 DPRINTF(2, "%s: handling DHCP %s\n", rif
->ifname
, (dhcp
->op
== 1 ? "request" : "response"));
544 dhcp
->flags
|= htons(DHCP_FLAG_BROADCAST
);
547 sum
= udplen
+ IPPROTO_UDP
;
548 sum
= chksum(sum
, (void *) &pkt
->iph
.saddr
, 8);
549 sum
= chksum(sum
, (void *) udp
, udplen
);
553 udp
->check
= htons(~sum
);
555 forward_bcast_packet(rif
, data
, len
);
560 static void recv_bcast_packet(struct uloop_fd
*fd
, unsigned int events
)
562 struct relayd_interface
*rif
= container_of(fd
, struct relayd_interface
, bcast_fd
);
563 static char pktbuf
[4096];
570 pktlen
= recv(rif
->bcast_fd
.fd
, pktbuf
, sizeof(pktbuf
), 0);
581 if (!forward_bcast
&& !forward_dhcp
)
584 if (forward_dhcp_packet(rif
, pktbuf
, pktlen
))
588 forward_bcast_packet(rif
, pktbuf
, pktlen
);
593 static int init_interface(struct relayd_interface
*rif
)
595 struct sockaddr_ll
*sll
= &rif
->sll
;
596 struct sockaddr_in
*sin
;
599 #ifdef PACKET_RECV_TYPE
600 unsigned int pkt_type
;
603 fd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_ARP
));
609 memset(&ifr
, 0, sizeof(ifr
));
610 strcpy(ifr
.ifr_name
, rif
->ifname
);
612 if (ioctl(fd
, SIOCGIFHWADDR
, &ifr
) < 0) {
613 perror("ioctl(SIOCGIFHWADDR)");
617 memcpy(sll
->sll_addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
618 sll
->sll_family
= AF_PACKET
;
619 sll
->sll_protocol
= htons(ETH_P_ARP
);
620 sll
->sll_pkttype
= PACKET_BROADCAST
;
621 sll
->sll_hatype
= ARPHRD_ETHER
;
622 sll
->sll_halen
= ETH_ALEN
;
624 if (ioctl(fd
, SIOCGIFINDEX
, &ifr
) < 0) {
625 perror("ioctl(SIOCGIFINDEX)");
629 sll
->sll_ifindex
= ifr
.ifr_ifindex
;
631 if (ioctl(fd
, SIOCGIFADDR
, &ifr
) < 0) {
632 memcpy(rif
->src_ip
, DUMMY_IP
, sizeof(rif
->src_ip
));
634 sin
= (struct sockaddr_in
*) &ifr
.ifr_addr
;
635 memcpy(rif
->src_ip
, &sin
->sin_addr
.s_addr
, sizeof(rif
->src_ip
));
638 if (bind(fd
, (struct sockaddr
*)sll
, sizeof(struct sockaddr_ll
)) < 0) {
639 perror("bind(ETH_P_ARP)");
643 rif
->fd
.cb
= recv_packet
;
644 uloop_fd_add(&rif
->fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
646 if (!forward_bcast
&& !forward_dhcp
)
649 fd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_IP
));
653 rif
->bcast_fd
.fd
= fd
;
654 rif
->bcast_fd
.cb
= recv_bcast_packet
;
656 memcpy(&rif
->bcast_sll
, &rif
->sll
, sizeof(rif
->bcast_sll
));
657 sll
= &rif
->bcast_sll
;
658 sll
->sll_protocol
= htons(ETH_P_IP
);
660 if (bind(fd
, (struct sockaddr
*)sll
, sizeof(struct sockaddr_ll
)) < 0) {
661 perror("bind(ETH_P_IP)");
665 #ifdef PACKET_RECV_TYPE
666 pkt_type
= (1 << PACKET_BROADCAST
);
667 setsockopt(fd
, SOL_PACKET
, PACKET_RECV_TYPE
, &pkt_type
, sizeof(pkt_type
));
670 uloop_fd_add(&rif
->bcast_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
674 static int init_interfaces(void)
676 struct relayd_interface
*rif
;
679 list_for_each_entry(rif
, &interfaces
, list
) {
680 ret
= init_interface(rif
);
688 static void del_interface(struct relayd_interface
*rif
)
690 struct relayd_host
*host
, *htmp
;
692 list_for_each_entry_safe(host
, htmp
, &rif
->hosts
, list
) {
698 static void cleanup_interfaces(void)
700 struct relayd_interface
*rif
, *rtmp
;
702 list_for_each_entry_safe(rif
, rtmp
, &interfaces
, list
) {
707 static int alloc_interface(const char *ifname
, bool managed
)
709 struct relayd_interface
*rif
;
711 if (strlen(ifname
) >= IFNAMSIZ
)
714 rif
= calloc(1, sizeof(*rif
));
718 INIT_LIST_HEAD(&rif
->list
);
719 INIT_LIST_HEAD(&rif
->hosts
);
720 strcpy(rif
->ifname
, ifname
);
721 list_add(&rif
->list
, &interfaces
);
722 rif
->managed
= managed
;
729 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
732 static void rtnl_parse_newneigh(struct nlmsghdr
*h
)
734 struct relayd_interface
*rif
= NULL
;
735 struct ndmsg
*r
= NLMSG_DATA(h
);
736 const uint8_t *lladdr
= NULL
;
737 const uint8_t *ipaddr
= NULL
;
741 if (r
->ndm_family
!= AF_INET
)
744 list_for_each_entry(rif
, &interfaces
, list
) {
745 if (rif
->sll
.sll_ifindex
== r
->ndm_ifindex
)
746 goto found_interface
;
751 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
));
752 for (rta
= NDA_RTA(r
); RTA_OK(rta
, len
); rta
= RTA_NEXT(rta
, len
)) {
753 switch(rta
->rta_type
) {
755 lladdr
= RTA_DATA(rta
);
758 ipaddr
= RTA_DATA(rta
);
765 if (!lladdr
|| !ipaddr
|| (r
->ndm_state
& (NUD_INCOMPLETE
|NUD_FAILED
)))
768 if (!memcmp(lladdr
, "\x00\x00\x00\x00\x00\x00", ETH_ALEN
))
771 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT
" ("MAC_FMT
")\n",
772 rif
->ifname
, IP_BUF(ipaddr
), MAC_BUF(lladdr
));
773 refresh_host(rif
, lladdr
, ipaddr
);
776 static void rtnl_parse_packet(void *data
, int len
)
780 for (h
= data
; NLMSG_OK(h
, len
); h
= NLMSG_NEXT(h
, len
)) {
781 if (h
->nlmsg_type
== NLMSG_DONE
||
782 h
->nlmsg_type
== NLMSG_ERROR
)
785 if (h
->nlmsg_seq
!= rtnl_dump_seq
)
788 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
789 rtnl_parse_newneigh(h
);
793 static void rtnl_cb(struct uloop_fd
*fd
, unsigned int events
)
795 struct sockaddr_nl nladdr
;
796 static uint8_t buf
[16384];
799 .iov_len
= sizeof(buf
),
801 struct msghdr msg
= {
803 .msg_namelen
= sizeof(nladdr
),
811 len
= recvmsg(rtnl_sock
.fd
, &msg
, 0);
822 if (nladdr
.nl_pid
!= 0)
825 rtnl_parse_packet(buf
, len
);
829 static int rtnl_init(void)
831 struct sockaddr_nl snl_local
;
837 .nlmsg_len
= sizeof(req
),
838 .nlmsg_type
= RTM_GETNEIGH
,
839 .nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
,
842 .g
.rtgen_family
= AF_INET
,
845 rtnl_sock
.fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
846 if (rtnl_sock
.fd
< 0) {
847 perror("socket(AF_NETLINK)");
851 snl_local
.nl_family
= AF_NETLINK
;
853 if (bind(rtnl_sock
.fd
, (struct sockaddr
*) &snl_local
, sizeof(struct sockaddr_nl
)) < 0) {
859 rtnl_sock
.cb
= rtnl_cb
;
860 uloop_fd_add(&rtnl_sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
862 rtnl_seq
= time(NULL
);
863 rtnl_dump_seq
= rtnl_seq
;
864 req
.nlh
.nlmsg_seq
= rtnl_seq
;
865 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
870 static void die(int signo
)
873 * When we hit SIGTERM, clean up interfaces directly, so that we
874 * won't leave our routing in an invalid state.
876 cleanup_interfaces();
880 static int usage(const char *progname
)
882 fprintf(stderr
, "Usage: %s <options>\n"
885 " -d Enable debug messages\n"
886 " -i <ifname> Add an interface for relaying\n"
887 " -I <ifname> Same as -i, except with ARP cache and host route management\n"
888 " You need to specify at least two interfaces\n"
889 " -t <timeout> Host entry expiry timeout\n"
890 " -B Enable broadcast forwarding\n"
891 " -D Enable DHCP forwarding\n"
897 int main(int argc
, char **argv
)
904 inet_sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
906 perror("socket(AF_INET)");
914 while ((ch
= getopt(argc
, argv
, "I:i:t:BDd")) != -1) {
921 if (alloc_interface(optarg
, managed
) < 0)
927 host_timeout
= atoi(optarg
);
928 if (host_timeout
<= 0)
929 return usage(argv
[0]);
942 return usage(argv
[0]);
946 if (list_empty(&interfaces
))
947 return usage(argv
[0]);
950 fprintf(stderr
, "ERROR: Need at least 2 interfaces for relaying\n");
957 signal(SIGTERM
, die
);
959 signal(SIGUSR1
, die
);
960 signal(SIGUSR2
, die
);
962 if (init_interfaces() < 0)
971 cleanup_interfaces();
972 uloop_fd_delete(&rtnl_sock
);