5 #include <net/ethernet.h>
6 #include <netinet/if_ether.h>
7 #include <netinet/ip.h>
8 #include <netinet/udp.h>
10 #include <linux/if_packet.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/neighbour.h>
30 #define DPRINTF(level, ...) if (debug >= level) fprintf(stderr, __VA_ARGS__);
32 #define DPRINTF(...) do {} while(0)
36 #define __packed __attribute__((packed))
39 #define __uc(c) ((unsigned char *)(c))
41 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
42 #define MAC_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3], __uc(_c)[4], __uc(_c)[5]
44 #define IP_FMT "%d.%d.%d.%d"
45 #define IP_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3]
47 #define DUMMY_IP ((uint8_t *) "\x01\x01\x01\x01")
49 #define DHCP_FLAG_BROADCAST (1 << 15)
51 struct relayd_interface
{
52 struct list_head list
;
54 struct uloop_fd bcast_fd
;
55 struct sockaddr_ll sll
;
56 struct sockaddr_ll bcast_sll
;
57 char ifname
[IFNAMSIZ
];
58 struct list_head hosts
;
64 struct list_head list
;
65 struct relayd_interface
*rif
;
66 uint8_t lladdr
[ETH_ALEN
];
68 struct uloop_timeout timeout
;
73 struct ether_header eth
;
78 struct ether_header eth
;
83 uint8_t op
, htype
, hlen
, hops
;
86 struct in_addr ciaddr
, yiaddr
, siaddr
, giaddr
;
87 unsigned char chaddr
[16];
88 unsigned char sname
[64];
89 unsigned char file
[128];
98 static LIST_HEAD(interfaces
);
99 static int host_timeout
;
100 static int inet_sock
;
101 static int forward_bcast
;
102 static int forward_dhcp
;
103 static struct uloop_fd rtnl_sock
;
104 static unsigned int rtnl_seq
, rtnl_dump_seq
;
106 static struct relayd_host
*find_host_by_ipaddr(struct relayd_interface
*rif
, const uint8_t *ipaddr
)
108 struct relayd_host
*host
;
111 list_for_each_entry(rif
, &interfaces
, list
) {
112 host
= find_host_by_ipaddr(rif
, ipaddr
);
121 list_for_each_entry(host
, &rif
->hosts
, list
) {
122 if (memcmp(ipaddr
, host
->ipaddr
, sizeof(host
->ipaddr
)) != 0)
130 static void add_arp(struct relayd_host
*host
)
132 struct sockaddr_in
*sin
;
135 strncpy(arp
.arp_dev
, host
->rif
->ifname
, sizeof(arp
.arp_dev
));
136 arp
.arp_flags
= ATF_COM
;
138 arp
.arp_ha
.sa_family
= ARPHRD_ETHER
;
139 memcpy(arp
.arp_ha
.sa_data
, host
->lladdr
, ETH_ALEN
);
141 sin
= (struct sockaddr_in
*) &arp
.arp_pa
;
142 sin
->sin_family
= AF_INET
;
143 memcpy(&sin
->sin_addr
, host
->ipaddr
, sizeof(host
->ipaddr
));
145 ioctl(inet_sock
, SIOCSARP
, &arp
);
148 static void rtnl_route_set(struct relayd_host
*host
, bool add
)
163 memset(&req
, 0, sizeof(req
));
165 req
.nl
.nlmsg_len
= sizeof(req
);
166 req
.rt
.rtm_family
= AF_INET
;
167 req
.rt
.rtm_dst_len
= 32;
169 req
.dst
.rta
.rta_type
= RTA_DST
;
170 req
.dst
.rta
.rta_len
= sizeof(req
.dst
);
171 memcpy(req
.dst
.ipaddr
, host
->ipaddr
, sizeof(req
.dst
.ipaddr
));
173 req
.dev
.rta
.rta_type
= RTA_OIF
;
174 req
.dev
.rta
.rta_len
= sizeof(req
.dev
);
175 req
.dev
.ifindex
= host
->rif
->sll
.sll_ifindex
;
177 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
178 req
.rt
.rtm_table
= RT_TABLE_MAIN
;
180 req
.nl
.nlmsg_type
= RTM_NEWROUTE
;
181 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_REPLACE
;
183 req
.rt
.rtm_protocol
= RTPROT_BOOT
;
184 req
.rt
.rtm_scope
= RT_SCOPE_LINK
;
185 req
.rt
.rtm_type
= RTN_UNICAST
;
187 req
.nl
.nlmsg_type
= RTM_DELROUTE
;
188 req
.rt
.rtm_scope
= RT_SCOPE_NOWHERE
;
191 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
194 static void add_route(struct relayd_host
*host
)
196 rtnl_route_set(host
, true);
199 static void del_route(struct relayd_host
*host
)
201 rtnl_route_set(host
, false);
204 static void del_host(struct relayd_host
*host
)
206 DPRINTF(1, "%s: deleting host "IP_FMT
" ("MAC_FMT
")\n", host
->rif
->ifname
,
207 IP_BUF(host
->ipaddr
), MAC_BUF(host
->lladdr
));
209 if (host
->rif
->managed
)
211 list_del(&host
->list
);
215 static void fill_arp_request(struct arp_packet
*pkt
, struct relayd_interface
*rif
,
216 uint8_t spa
[4], uint8_t tpa
[4])
218 memset(pkt
, 0, sizeof(*pkt
));
220 pkt
->eth
.ether_type
= htons(ETHERTYPE_ARP
);
221 memcpy(pkt
->eth
.ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
223 memcpy(pkt
->arp
.arp_sha
, rif
->sll
.sll_addr
, ETH_ALEN
);
224 memcpy(pkt
->arp
.arp_spa
, spa
, 4);
225 memcpy(pkt
->arp
.arp_tpa
, tpa
, 4);
227 pkt
->arp
.arp_hrd
= htons(ARPHRD_ETHER
);
228 pkt
->arp
.arp_pro
= htons(ETH_P_IP
);
229 pkt
->arp
.arp_hln
= ETH_ALEN
;
230 pkt
->arp
.arp_pln
= 4;
233 static void send_arp_request(struct relayd_host
*host
)
235 struct relayd_interface
*rif
= host
->rif
;
236 struct arp_packet pkt
;
238 fill_arp_request(&pkt
, host
->rif
, host
->rif
->src_ip
, host
->ipaddr
);
240 pkt
.arp
.arp_op
= htons(ARPOP_REQUEST
);
241 memcpy(pkt
.arp
.arp_spa
, rif
->src_ip
, ETH_ALEN
);
242 memset(pkt
.arp
.arp_tha
, 0, ETH_ALEN
);
243 memset(pkt
.eth
.ether_dhost
, 0xff, ETH_ALEN
);
245 DPRINTF(2, "%s: sending ARP who-has "IP_FMT
", tell "IP_FMT
" ("MAC_FMT
")\n",
246 rif
->ifname
, IP_BUF(pkt
.arp
.arp_tpa
),
247 IP_BUF(pkt
.arp
.arp_spa
), MAC_BUF(pkt
.eth
.ether_shost
));
249 sendto(rif
->fd
.fd
, &pkt
, sizeof(pkt
), 0,
250 (struct sockaddr
*) &rif
->sll
, sizeof(rif
->sll
));
253 static void send_arp_reply(struct relayd_interface
*rif
, uint8_t spa
[4],
254 uint8_t tha
[ETH_ALEN
], uint8_t tpa
[4])
256 struct arp_packet pkt
;
258 fill_arp_request(&pkt
, rif
, spa
, tpa
);
260 pkt
.arp
.arp_op
= htons(ARPOP_REPLY
);
261 memcpy(pkt
.eth
.ether_dhost
, tha
, ETH_ALEN
);
262 memcpy(pkt
.arp
.arp_tha
, tha
, ETH_ALEN
);
264 DPRINTF(2, "%s: sending ARP reply to "IP_FMT
", "IP_FMT
" is at ("MAC_FMT
")\n",
265 rif
->ifname
, IP_BUF(pkt
.arp
.arp_tpa
),
266 IP_BUF(pkt
.arp
.arp_spa
), MAC_BUF(pkt
.eth
.ether_shost
));
268 sendto(rif
->fd
.fd
, &pkt
, sizeof(pkt
), 0,
269 (struct sockaddr
*) &rif
->sll
, sizeof(rif
->sll
));
272 static void host_entry_timeout(struct uloop_timeout
*timeout
)
274 struct relayd_host
*host
= container_of(timeout
, struct relayd_host
, timeout
);
277 * When a host is behind a managed interface, we must not expire its host
278 * entry prematurely, as this will cause routes to the node to expire,
279 * leading to loss of connectivity from the other side.
280 * When the timeout is reached, try pinging the host a few times before
283 if (host
->rif
->managed
&& host
->cleanup_pending
< 2) {
284 send_arp_request(host
);
285 host
->cleanup_pending
++;
286 uloop_timeout_set(&host
->timeout
, 1000);
292 static struct relayd_host
*add_host(struct relayd_interface
*rif
, const uint8_t *lladdr
, const uint8_t *ipaddr
)
294 struct relayd_host
*host
;
296 DPRINTF(1, "%s: adding host "IP_FMT
" ("MAC_FMT
")\n", rif
->ifname
,
297 IP_BUF(ipaddr
), MAC_BUF(lladdr
));
299 host
= calloc(1, sizeof(*host
));
301 memcpy(host
->ipaddr
, ipaddr
, sizeof(host
->ipaddr
));
302 memcpy(host
->lladdr
, lladdr
, sizeof(host
->lladdr
));
303 list_add(&host
->list
, &rif
->hosts
);
304 host
->timeout
.cb
= host_entry_timeout
;
305 uloop_timeout_set(&host
->timeout
, host_timeout
* 1000);
314 static struct relayd_host
*refresh_host(struct relayd_interface
*rif
, const uint8_t *lladdr
, const uint8_t *ipaddr
)
316 struct relayd_host
*host
;
318 host
= find_host_by_ipaddr(rif
, ipaddr
);
320 host
= find_host_by_ipaddr(NULL
, ipaddr
);
323 * When we suddenly see the host appearing on a different interface,
324 * reduce the timeout to make the old entry expire faster, in case the
326 * If the old entry is behind a managed interface, it will be pinged
327 * before we expire it
329 if (host
&& !host
->cleanup_pending
)
330 uloop_timeout_set(&host
->timeout
, 1);
332 host
= add_host(rif
, lladdr
, ipaddr
);
334 host
->cleanup_pending
= false;
335 uloop_timeout_set(&host
->timeout
, host_timeout
* 1000);
341 static void relay_arp_request(struct relayd_interface
*from_rif
, struct arp_packet
*pkt
)
343 struct relayd_interface
*rif
;
344 struct arp_packet reqpkt
;
346 memcpy(&reqpkt
, pkt
, sizeof(reqpkt
));
347 list_for_each_entry(rif
, &interfaces
, list
) {
351 memcpy(reqpkt
.eth
.ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
352 memcpy(reqpkt
.arp
.arp_sha
, rif
->sll
.sll_addr
, ETH_ALEN
);
354 DPRINTF(2, "%s: sending ARP who-has "IP_FMT
", tell "IP_FMT
" ("MAC_FMT
")\n",
355 rif
->ifname
, IP_BUF(reqpkt
.arp
.arp_tpa
),
356 IP_BUF(reqpkt
.arp
.arp_spa
), MAC_BUF(reqpkt
.eth
.ether_shost
));
358 sendto(rif
->fd
.fd
, &reqpkt
, sizeof(reqpkt
), 0,
359 (struct sockaddr
*) &rif
->sll
, sizeof(rif
->sll
));
363 static void recv_arp_request(struct relayd_interface
*rif
, struct arp_packet
*pkt
)
365 struct relayd_host
*host
;
367 DPRINTF(2, "%s: ARP who-has "IP_FMT
", tell "IP_FMT
" ("MAC_FMT
")\n",
369 IP_BUF(pkt
->arp
.arp_tpa
),
370 IP_BUF(pkt
->arp
.arp_spa
),
371 MAC_BUF(pkt
->eth
.ether_shost
));
373 if (!memcmp(pkt
->arp
.arp_spa
, "\x00\x00\x00\x00", 4))
376 refresh_host(rif
, pkt
->eth
.ether_shost
, pkt
->arp
.arp_spa
);
378 host
= find_host_by_ipaddr(NULL
, pkt
->arp
.arp_tpa
);
381 * If a host is being pinged because of a timeout, do not use the cached
382 * entry here. That way we can avoid giving out stale data in case the node
383 * has moved. We shouldn't relay requests here either, as we might miss our
384 * chance to create a host route.
386 if (host
&& host
->cleanup_pending
)
389 relay_arp_request(rif
, pkt
);
393 static void recv_arp_reply(struct relayd_interface
*rif
, struct arp_packet
*pkt
)
395 struct relayd_host
*host
;
397 DPRINTF(2, "%s: received ARP reply for "IP_FMT
" from "MAC_FMT
", deliver to "IP_FMT
"\n",
399 IP_BUF(pkt
->arp
.arp_spa
),
400 MAC_BUF(pkt
->eth
.ether_shost
),
401 IP_BUF(pkt
->arp
.arp_tpa
));
403 refresh_host(rif
, pkt
->arp
.arp_sha
, pkt
->arp
.arp_spa
);
405 if (!memcmp(pkt
->arp
.arp_tpa
, rif
->src_ip
, 4))
408 host
= find_host_by_ipaddr(NULL
, pkt
->arp
.arp_tpa
);
412 send_arp_reply(host
->rif
, pkt
->arp
.arp_spa
, host
->lladdr
, host
->ipaddr
);
415 static void recv_packet(struct uloop_fd
*fd
, unsigned int events
)
417 struct relayd_interface
*rif
= container_of(fd
, struct relayd_interface
, fd
);
418 struct arp_packet
*pkt
;
419 static char pktbuf
[4096];
426 pktlen
= recv(rif
->fd
.fd
, pktbuf
, sizeof(pktbuf
), 0);
437 pkt
= (void *)pktbuf
;
438 if (pkt
->arp
.arp_op
== htons(ARPOP_REPLY
))
439 recv_arp_reply(rif
, pkt
);
440 else if (pkt
->arp
.arp_op
== htons(ARPOP_REQUEST
))
441 recv_arp_request(rif
, pkt
);
443 DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt
->arp
.arp_op
));
448 static void forward_bcast_packet(struct relayd_interface
*from_rif
, void *packet
, int len
)
450 struct relayd_interface
*rif
;
451 struct ether_header
*eth
= packet
;
453 list_for_each_entry(rif
, &interfaces
, list
) {
457 DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif
->ifname
, rif
->ifname
);
458 memcpy(eth
->ether_shost
, rif
->sll
.sll_addr
, ETH_ALEN
);
459 send(rif
->bcast_fd
.fd
, packet
, len
, 0);
464 chksum(uint16_t sum
, const uint8_t *data
, uint16_t len
)
469 last
= data
+ len
- 1;
472 t
= (data
[0] << 8) + data
[1];
480 t
= (data
[0] << 8) + 0;
489 static bool forward_dhcp_packet(struct relayd_interface
*rif
, void *data
, int len
)
491 struct ip_packet
*pkt
= data
;
493 struct dhcp_header
*dhcp
;
497 if (pkt
->eth
.ether_type
!= htons(ETH_P_IP
))
500 if (pkt
->iph
.version
!= 4)
503 if (pkt
->iph
.protocol
!= IPPROTO_UDP
)
506 udp
= (void *) ((char *) &pkt
->iph
+ (pkt
->iph
.ihl
<< 2));
507 dhcp
= (void *) (udp
+ 1);
509 udplen
= ntohs(udp
->len
);
510 if (udplen
> len
- ((char *) udp
- (char *) data
))
513 if (udp
->dest
!= htons(67) && udp
->source
!= htons(67))
516 if (dhcp
->op
!= 1 && dhcp
->op
!= 2)
522 DPRINTF(2, "%s: handling DHCP %s\n", rif
->ifname
, (dhcp
->op
== 1 ? "request" : "response"));
524 dhcp
->flags
|= htons(DHCP_FLAG_BROADCAST
);
527 sum
= udplen
+ IPPROTO_UDP
;
528 sum
= chksum(sum
, (void *) &pkt
->iph
.saddr
, 8);
529 sum
= chksum(sum
, (void *) udp
, udplen
);
533 udp
->check
= htons(~sum
);
535 forward_bcast_packet(rif
, data
, len
);
540 static void recv_bcast_packet(struct uloop_fd
*fd
, unsigned int events
)
542 struct relayd_interface
*rif
= container_of(fd
, struct relayd_interface
, bcast_fd
);
543 static char pktbuf
[4096];
550 pktlen
= recv(rif
->bcast_fd
.fd
, pktbuf
, sizeof(pktbuf
), 0);
561 if (!forward_bcast
&& !forward_dhcp
)
564 if (forward_dhcp_packet(rif
, pktbuf
, pktlen
))
568 forward_bcast_packet(rif
, pktbuf
, pktlen
);
573 static int init_interface(struct relayd_interface
*rif
)
575 struct sockaddr_ll
*sll
= &rif
->sll
;
576 struct sockaddr_in
*sin
;
579 #ifdef PACKET_RECV_TYPE
580 unsigned int pkt_type
;
583 fd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_ARP
));
589 memset(&ifr
, 0, sizeof(ifr
));
590 strcpy(ifr
.ifr_name
, rif
->ifname
);
592 if (ioctl(fd
, SIOCGIFHWADDR
, &ifr
) < 0) {
593 perror("ioctl(SIOCGIFHWADDR)");
597 memcpy(sll
->sll_addr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
598 sll
->sll_family
= AF_PACKET
;
599 sll
->sll_protocol
= htons(ETH_P_ARP
);
600 sll
->sll_pkttype
= PACKET_BROADCAST
;
601 sll
->sll_hatype
= ARPHRD_ETHER
;
602 sll
->sll_halen
= ETH_ALEN
;
604 if (ioctl(fd
, SIOCGIFINDEX
, &ifr
) < 0) {
605 perror("ioctl(SIOCGIFINDEX)");
609 sll
->sll_ifindex
= ifr
.ifr_ifindex
;
611 if (ioctl(fd
, SIOCGIFADDR
, &ifr
) < 0) {
612 memcpy(rif
->src_ip
, DUMMY_IP
, sizeof(rif
->src_ip
));
614 sin
= (struct sockaddr_in
*) &ifr
.ifr_addr
;
615 memcpy(rif
->src_ip
, &sin
->sin_addr
.s_addr
, sizeof(rif
->src_ip
));
618 if (bind(fd
, (struct sockaddr
*)sll
, sizeof(struct sockaddr_ll
)) < 0) {
619 perror("bind(ETH_P_ARP)");
623 rif
->fd
.cb
= recv_packet
;
624 uloop_fd_add(&rif
->fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
626 if (!forward_bcast
&& !forward_dhcp
)
629 fd
= socket(AF_PACKET
, SOCK_RAW
, htons(ETH_P_IP
));
633 rif
->bcast_fd
.fd
= fd
;
634 rif
->bcast_fd
.cb
= recv_bcast_packet
;
636 memcpy(&rif
->bcast_sll
, &rif
->sll
, sizeof(rif
->bcast_sll
));
637 sll
= &rif
->bcast_sll
;
638 sll
->sll_protocol
= htons(ETH_P_IP
);
640 if (bind(fd
, (struct sockaddr
*)sll
, sizeof(struct sockaddr_ll
)) < 0) {
641 perror("bind(ETH_P_IP)");
645 #ifdef PACKET_RECV_TYPE
646 pkt_type
= (1 << PACKET_BROADCAST
);
647 setsockopt(fd
, SOL_PACKET
, PACKET_RECV_TYPE
, &pkt_type
, sizeof(pkt_type
));
650 uloop_fd_add(&rif
->bcast_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
654 static int init_interfaces(void)
656 struct relayd_interface
*rif
;
659 list_for_each_entry(rif
, &interfaces
, list
) {
660 ret
= init_interface(rif
);
668 static void del_interface(struct relayd_interface
*rif
)
670 struct relayd_host
*host
, *htmp
;
672 list_for_each_entry_safe(host
, htmp
, &rif
->hosts
, list
) {
678 static void cleanup_interfaces(void)
680 struct relayd_interface
*rif
, *rtmp
;
682 list_for_each_entry_safe(rif
, rtmp
, &interfaces
, list
) {
687 static int alloc_interface(const char *ifname
, bool managed
)
689 struct relayd_interface
*rif
;
691 if (strlen(ifname
) >= IFNAMSIZ
)
694 rif
= calloc(1, sizeof(*rif
));
698 INIT_LIST_HEAD(&rif
->list
);
699 INIT_LIST_HEAD(&rif
->hosts
);
700 strcpy(rif
->ifname
, ifname
);
701 list_add(&rif
->list
, &interfaces
);
702 rif
->managed
= managed
;
709 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
712 static void rtnl_parse_newneigh(struct nlmsghdr
*h
)
714 struct relayd_interface
*rif
= NULL
;
715 struct ndmsg
*r
= NLMSG_DATA(h
);
716 const uint8_t *lladdr
= NULL
;
717 const uint8_t *ipaddr
= NULL
;
721 if (r
->ndm_family
!= AF_INET
)
724 list_for_each_entry(rif
, &interfaces
, list
) {
725 if (rif
->sll
.sll_ifindex
== r
->ndm_ifindex
)
726 goto found_interface
;
731 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
));
732 for (rta
= NDA_RTA(r
); RTA_OK(rta
, len
); rta
= RTA_NEXT(rta
, len
)) {
733 switch(rta
->rta_type
) {
735 lladdr
= RTA_DATA(rta
);
738 ipaddr
= RTA_DATA(rta
);
745 if (!lladdr
|| !ipaddr
|| (r
->ndm_state
& (NUD_INCOMPLETE
|NUD_FAILED
)))
748 if (!memcmp(lladdr
, "\x00\x00\x00\x00\x00\x00", ETH_ALEN
))
751 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT
" ("MAC_FMT
")\n",
752 rif
->ifname
, IP_BUF(ipaddr
), MAC_BUF(lladdr
));
753 refresh_host(rif
, lladdr
, ipaddr
);
756 static void rtnl_parse_packet(void *data
, int len
)
760 for (h
= data
; NLMSG_OK(h
, len
); h
= NLMSG_NEXT(h
, len
)) {
761 if (h
->nlmsg_type
== NLMSG_DONE
||
762 h
->nlmsg_type
== NLMSG_ERROR
)
765 if (h
->nlmsg_seq
!= rtnl_dump_seq
)
768 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
769 rtnl_parse_newneigh(h
);
773 static void rtnl_cb(struct uloop_fd
*fd
, unsigned int events
)
775 struct sockaddr_nl nladdr
;
776 static uint8_t buf
[16384];
779 .iov_len
= sizeof(buf
),
781 struct msghdr msg
= {
783 .msg_namelen
= sizeof(nladdr
),
791 len
= recvmsg(rtnl_sock
.fd
, &msg
, 0);
802 if (nladdr
.nl_pid
!= 0)
805 rtnl_parse_packet(buf
, len
);
809 static int rtnl_init(void)
811 struct sockaddr_nl snl_local
;
817 .nlmsg_len
= sizeof(req
),
818 .nlmsg_type
= RTM_GETNEIGH
,
819 .nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
,
822 .g
.rtgen_family
= AF_INET
,
825 rtnl_sock
.fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
826 if (rtnl_sock
.fd
< 0) {
827 perror("socket(AF_NETLINK)");
831 snl_local
.nl_family
= AF_NETLINK
;
833 if (bind(rtnl_sock
.fd
, (struct sockaddr
*) &snl_local
, sizeof(struct sockaddr_nl
)) < 0) {
839 rtnl_sock
.cb
= rtnl_cb
;
840 uloop_fd_add(&rtnl_sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
842 rtnl_seq
= time(NULL
);
843 rtnl_dump_seq
= rtnl_seq
;
844 req
.nlh
.nlmsg_seq
= rtnl_seq
;
845 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
850 static void die(int signo
)
853 * When we hit SIGTERM, clean up interfaces directly, so that we
854 * won't leave our routing in an invalid state.
856 cleanup_interfaces();
860 static int usage(const char *progname
)
862 fprintf(stderr
, "Usage: %s <options>\n"
865 " -d Enable debug messages\n"
866 " -i <ifname> Add an interface for relaying\n"
867 " -I <ifname> Same as -i, except with ARP cache and host route management\n"
868 " You need to specify at least two interfaces\n"
869 " -t <timeout> Host entry expiry timeout\n"
870 " -B Enable broadcast forwarding\n"
871 " -D Enable DHCP forwarding\n"
877 int main(int argc
, char **argv
)
884 inet_sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
886 perror("socket(AF_INET)");
894 while ((ch
= getopt(argc
, argv
, "I:i:t:BDd")) != -1) {
901 if (alloc_interface(optarg
, managed
) < 0)
907 host_timeout
= atoi(optarg
);
908 if (host_timeout
<= 0)
909 return usage(argv
[0]);
922 return usage(argv
[0]);
926 if (list_empty(&interfaces
))
927 return usage(argv
[0]);
930 fprintf(stderr
, "ERROR: Need at least 2 interfaces for relaying\n");
937 signal(SIGTERM
, die
);
939 signal(SIGUSR1
, die
);
940 signal(SIGUSR2
, die
);
942 if (init_interfaces() < 0)
951 cleanup_interfaces();
952 uloop_fd_delete(&rtnl_sock
);