Move some data structures and definitions to a header file
[project/relayd.git] / main.c
1 /*
2  *   Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18 #include <sys/ioctl.h>
19
20 #include <arpa/inet.h>
21 #include <net/if.h>
22 #include <net/ethernet.h>
23 #include <netinet/if_ether.h>
24 #include <netinet/ip.h>
25 #include <netinet/udp.h>
26
27 #include <linux/if_packet.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/neighbour.h>
30
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include <errno.h>
40 #include <signal.h>
41
42 #include "uloop.h"
43 #include "list.h"
44 #include "relayd.h"
45
46 LIST_HEAD(interfaces);
47 int debug;
48
49 static int host_timeout;
50 static int inet_sock;
51 static int forward_bcast;
52 static int forward_dhcp;
53 static struct uloop_fd rtnl_sock;
54 static unsigned int rtnl_seq, rtnl_dump_seq;
55
56 static struct relayd_host *find_host_by_ipaddr(struct relayd_interface *rif, const uint8_t *ipaddr)
57 {
58         struct relayd_host *host;
59
60         if (!rif) {
61                 list_for_each_entry(rif, &interfaces, list) {
62                         host = find_host_by_ipaddr(rif, ipaddr);
63                         if (!host)
64                                 continue;
65
66                         return host;
67                 }
68                 return NULL;
69         }
70
71         list_for_each_entry(host, &rif->hosts, list) {
72                 if (memcmp(ipaddr, host->ipaddr, sizeof(host->ipaddr)) != 0)
73                         continue;
74
75                 return host;
76         }
77         return NULL;
78 }
79
80 static void add_arp(struct relayd_host *host)
81 {
82         struct sockaddr_in *sin;
83         struct arpreq arp;
84
85         strncpy(arp.arp_dev, host->rif->ifname, sizeof(arp.arp_dev));
86         arp.arp_flags = ATF_COM;
87
88         arp.arp_ha.sa_family = ARPHRD_ETHER;
89         memcpy(arp.arp_ha.sa_data, host->lladdr, ETH_ALEN);
90
91         sin = (struct sockaddr_in *) &arp.arp_pa;
92         sin->sin_family = AF_INET;
93         memcpy(&sin->sin_addr, host->ipaddr, sizeof(host->ipaddr));
94
95         ioctl(inet_sock, SIOCSARP, &arp);
96 }
97
98 static void rtnl_route_set(struct relayd_host *host, bool add)
99 {
100         static struct {
101                 struct nlmsghdr nl;
102                 struct rtmsg rt;
103                 struct {
104                         struct rtattr rta;
105                         uint8_t ipaddr[4];
106                 } __packed dst;
107                 struct {
108                         struct rtattr rta;
109                         int ifindex;
110                 } __packed dev;
111         } __packed req;
112
113         memset(&req, 0, sizeof(req));
114
115         req.nl.nlmsg_len = sizeof(req);
116         req.rt.rtm_family = AF_INET;
117         req.rt.rtm_dst_len = 32;
118
119         req.dst.rta.rta_type = RTA_DST;
120         req.dst.rta.rta_len = sizeof(req.dst);
121         memcpy(req.dst.ipaddr, host->ipaddr, sizeof(req.dst.ipaddr));
122
123         req.dev.rta.rta_type = RTA_OIF;
124         req.dev.rta.rta_len = sizeof(req.dev);
125         req.dev.ifindex = host->rif->sll.sll_ifindex;
126
127         req.nl.nlmsg_flags = NLM_F_REQUEST;
128         req.rt.rtm_table = RT_TABLE_MAIN;
129         if (add) {
130                 req.nl.nlmsg_type = RTM_NEWROUTE;
131                 req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
132
133                 req.rt.rtm_protocol = RTPROT_BOOT;
134                 req.rt.rtm_scope = RT_SCOPE_LINK;
135                 req.rt.rtm_type = RTN_UNICAST;
136         } else {
137                 req.nl.nlmsg_type = RTM_DELROUTE;
138                 req.rt.rtm_scope = RT_SCOPE_NOWHERE;
139         }
140
141         send(rtnl_sock.fd, &req, sizeof(req), 0);
142 }
143
144 static void add_route(struct relayd_host *host)
145 {
146         rtnl_route_set(host, true);
147 }
148
149 static void del_route(struct relayd_host *host)
150 {
151         rtnl_route_set(host, false);
152 }
153
154 static void del_host(struct relayd_host *host)
155 {
156         DPRINTF(1, "%s: deleting host "IP_FMT" ("MAC_FMT")\n", host->rif->ifname,
157                 IP_BUF(host->ipaddr), MAC_BUF(host->lladdr));
158
159         if (host->rif->managed)
160                 del_route(host);
161         list_del(&host->list);
162         free(host);
163 }
164
165 static void fill_arp_request(struct arp_packet *pkt, struct relayd_interface *rif,
166                              uint8_t spa[4], uint8_t tpa[4])
167 {
168         memset(pkt, 0, sizeof(*pkt));
169
170         pkt->eth.ether_type = htons(ETHERTYPE_ARP);
171         memcpy(pkt->eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
172
173         memcpy(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
174         memcpy(pkt->arp.arp_spa, spa, 4);
175         memcpy(pkt->arp.arp_tpa, tpa, 4);
176
177         pkt->arp.arp_hrd = htons(ARPHRD_ETHER);
178         pkt->arp.arp_pro = htons(ETH_P_IP);
179         pkt->arp.arp_hln = ETH_ALEN;
180         pkt->arp.arp_pln = 4;
181 }
182
183 static void send_arp_request(struct relayd_host *host)
184 {
185         struct relayd_interface *rif = host->rif;
186         struct arp_packet pkt;
187
188         fill_arp_request(&pkt, host->rif, host->rif->src_ip, host->ipaddr);
189
190         pkt.arp.arp_op = htons(ARPOP_REQUEST);
191         memcpy(pkt.arp.arp_spa, rif->src_ip, ETH_ALEN);
192         memset(pkt.arp.arp_tha, 0, ETH_ALEN);
193         memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN);
194
195         DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
196                 rif->ifname, IP_BUF(pkt.arp.arp_tpa),
197                 IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
198
199         sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
200                 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
201 }
202
203 static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4],
204                            uint8_t tha[ETH_ALEN], uint8_t tpa[4])
205 {
206         struct arp_packet pkt;
207
208         fill_arp_request(&pkt, rif, spa, tpa);
209
210         pkt.arp.arp_op = htons(ARPOP_REPLY);
211         memcpy(pkt.eth.ether_dhost, tha, ETH_ALEN);
212         memcpy(pkt.arp.arp_tha, tha, ETH_ALEN);
213
214         DPRINTF(2, "%s: sending ARP reply to "IP_FMT", "IP_FMT" is at ("MAC_FMT")\n",
215                 rif->ifname, IP_BUF(pkt.arp.arp_tpa),
216                 IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
217
218         sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
219                 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
220 }
221
222 static void host_entry_timeout(struct uloop_timeout *timeout)
223 {
224         struct relayd_host *host = container_of(timeout, struct relayd_host, timeout);
225
226         /*
227          * When a host is behind a managed interface, we must not expire its host
228          * entry prematurely, as this will cause routes to the node to expire,
229          * leading to loss of connectivity from the other side.
230          * When the timeout is reached, try pinging the host a few times before
231          * giving up on it.
232          */
233         if (host->rif->managed && host->cleanup_pending < 2) {
234                 send_arp_request(host);
235                 host->cleanup_pending++;
236                 uloop_timeout_set(&host->timeout, 1000);
237                 return;
238         }
239         del_host(host);
240 }
241
242 static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
243 {
244         struct relayd_host *host;
245
246         DPRINTF(1, "%s: adding host "IP_FMT" ("MAC_FMT")\n", rif->ifname,
247                         IP_BUF(ipaddr), MAC_BUF(lladdr));
248
249         host = calloc(1, sizeof(*host));
250         host->rif = rif;
251         memcpy(host->ipaddr, ipaddr, sizeof(host->ipaddr));
252         memcpy(host->lladdr, lladdr, sizeof(host->lladdr));
253         list_add(&host->list, &rif->hosts);
254         host->timeout.cb = host_entry_timeout;
255         uloop_timeout_set(&host->timeout, host_timeout * 1000);
256
257         add_arp(host);
258         if (rif->managed)
259                 add_route(host);
260
261         return host;
262 }
263
264 static struct relayd_host *refresh_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
265 {
266         struct relayd_host *host;
267
268         host = find_host_by_ipaddr(rif, ipaddr);
269         if (!host) {
270                 host = find_host_by_ipaddr(NULL, ipaddr);
271
272                 /* 
273                  * When we suddenly see the host appearing on a different interface,
274                  * reduce the timeout to make the old entry expire faster, in case the
275                  * host has moved.
276                  * If the old entry is behind a managed interface, it will be pinged
277                  * before we expire it
278                  */
279                 if (host && !host->cleanup_pending)
280                         uloop_timeout_set(&host->timeout, 1);
281
282                 host = add_host(rif, lladdr, ipaddr);
283         } else {
284                 host->cleanup_pending = false;
285                 uloop_timeout_set(&host->timeout, host_timeout * 1000);
286         }
287
288         return host;
289 }
290
291 static void relay_arp_request(struct relayd_interface *from_rif, struct arp_packet *pkt)
292 {
293         struct relayd_interface *rif;
294         struct arp_packet reqpkt;
295
296         memcpy(&reqpkt, pkt, sizeof(reqpkt));
297         list_for_each_entry(rif, &interfaces, list) {
298                 if (rif == from_rif)
299                         continue;
300
301                 memcpy(reqpkt.eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
302                 memcpy(reqpkt.arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
303
304                 DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
305                         rif->ifname, IP_BUF(reqpkt.arp.arp_tpa),
306                         IP_BUF(reqpkt.arp.arp_spa), MAC_BUF(reqpkt.eth.ether_shost));
307
308                 sendto(rif->fd.fd, &reqpkt, sizeof(reqpkt), 0,
309                         (struct sockaddr *) &rif->sll, sizeof(rif->sll));
310         }
311 }
312
313 static void recv_arp_request(struct relayd_interface *rif, struct arp_packet *pkt)
314 {
315         struct relayd_host *host;
316
317         DPRINTF(2, "%s: ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
318                 rif->ifname,
319                 IP_BUF(pkt->arp.arp_tpa),
320                 IP_BUF(pkt->arp.arp_spa),
321                 MAC_BUF(pkt->eth.ether_shost));
322
323         if (!memcmp(pkt->arp.arp_spa, "\x00\x00\x00\x00", 4))
324                 return;
325
326         refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa);
327
328         host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
329
330         /*
331          * If a host is being pinged because of a timeout, do not use the cached
332          * entry here. That way we can avoid giving out stale data in case the node
333          * has moved. We shouldn't relay requests here either, as we might miss our
334          * chance to create a host route.
335          */
336         if (host && host->cleanup_pending)
337                 return;
338
339         relay_arp_request(rif, pkt);
340 }
341
342
343 static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt)
344 {
345         struct relayd_host *host;
346
347         DPRINTF(2, "%s: received ARP reply for "IP_FMT" from "MAC_FMT", deliver to "IP_FMT"\n",
348                 rif->ifname,
349                 IP_BUF(pkt->arp.arp_spa),
350                 MAC_BUF(pkt->eth.ether_shost),
351                 IP_BUF(pkt->arp.arp_tpa));
352
353         refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
354
355         if (!memcmp(pkt->arp.arp_tpa, rif->src_ip, 4))
356                 return;
357
358         host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
359         if (!host)
360                 return;
361
362         if (host->rif == rif)
363                 return;
364
365         send_arp_reply(host->rif, pkt->arp.arp_spa, host->lladdr, host->ipaddr);
366 }
367
368 static void recv_packet(struct uloop_fd *fd, unsigned int events)
369 {
370         struct relayd_interface *rif = container_of(fd, struct relayd_interface, fd);
371         struct arp_packet *pkt;
372         static char pktbuf[4096];
373         int pktlen;
374
375         do {
376                 if (rif->fd.error)
377                         uloop_end();
378
379                 pktlen = recv(rif->fd.fd, pktbuf, sizeof(pktbuf), 0);
380                 if (pktlen < 0) {
381                         if (errno == EINTR)
382                                 continue;
383
384                         break;
385                 }
386
387                 if (!pktlen)
388                         break;
389
390                 pkt = (void *)pktbuf;
391                 if (pkt->arp.arp_op == htons(ARPOP_REPLY))
392                         recv_arp_reply(rif, pkt);
393                 else if (pkt->arp.arp_op == htons(ARPOP_REQUEST))
394                         recv_arp_request(rif, pkt);
395                 else
396                         DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt->arp.arp_op));
397
398         } while (1);
399 }
400
401 static void forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len)
402 {
403         struct relayd_interface *rif;
404         struct ether_header *eth = packet;
405
406         list_for_each_entry(rif, &interfaces, list) {
407                 if (rif == from_rif)
408                         continue;
409
410                 DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif->ifname, rif->ifname);
411                 memcpy(eth->ether_shost, rif->sll.sll_addr, ETH_ALEN);
412                 send(rif->bcast_fd.fd, packet, len, 0);
413         }
414 }
415
416 static uint16_t
417 chksum(uint16_t sum, const uint8_t *data, uint16_t len)
418 {
419         const uint8_t *last;
420         uint16_t t;
421
422         last = data + len - 1;
423
424         while(data < last) {
425                 t = (data[0] << 8) + data[1];
426                 sum += t;
427                 if(sum < t)
428                         sum++;
429                 data += 2;
430         }
431
432         if(data == last) {
433                 t = (data[0] << 8) + 0;
434                 sum += t;
435                 if(sum < t)
436                         sum++;
437         }
438
439         return sum;
440 }
441
442 static bool forward_dhcp_packet(struct relayd_interface *rif, void *data, int len)
443 {
444         struct ip_packet *pkt = data;
445         struct udphdr *udp;
446         struct dhcp_header *dhcp;
447         int udplen;
448         uint16_t sum;
449
450         if (pkt->eth.ether_type != htons(ETH_P_IP))
451                 return false;
452
453         if (pkt->iph.version != 4)
454                 return false;
455
456         if (pkt->iph.protocol != IPPROTO_UDP)
457                 return false;
458
459         udp = (void *) ((char *) &pkt->iph + (pkt->iph.ihl << 2));
460         dhcp = (void *) (udp + 1);
461
462         udplen = ntohs(udp->len);
463         if (udplen > len - ((char *) udp - (char *) data))
464                 return false;
465
466         if (udp->dest != htons(67) && udp->source != htons(67))
467                 return false;
468
469         if (dhcp->op != 1 && dhcp->op != 2)
470                 return false;
471
472         if (!forward_dhcp)
473                 return true;
474
475         if (dhcp->op == 2)
476                 refresh_host(rif, pkt->eth.ether_shost, (void *) &pkt->iph.saddr);
477
478         DPRINTF(2, "%s: handling DHCP %s\n", rif->ifname, (dhcp->op == 1 ? "request" : "response"));
479
480         dhcp->flags |= htons(DHCP_FLAG_BROADCAST);
481
482         udp->check = 0;
483         sum = udplen + IPPROTO_UDP;
484         sum = chksum(sum, (void *) &pkt->iph.saddr, 8);
485         sum = chksum(sum, (void *) udp, udplen);
486         if (sum == 0)
487                 sum = 0xffff;
488
489         udp->check = htons(~sum);
490
491         forward_bcast_packet(rif, data, len);
492
493         return true;
494 }
495
496 static void recv_bcast_packet(struct uloop_fd *fd, unsigned int events)
497 {
498         struct relayd_interface *rif = container_of(fd, struct relayd_interface, bcast_fd);
499         static char pktbuf[4096];
500         int pktlen;
501
502         do {
503                 if (rif->fd.error)
504                         uloop_end();
505
506                 pktlen = recv(rif->bcast_fd.fd, pktbuf, sizeof(pktbuf), 0);
507                 if (pktlen < 0) {
508                         if (errno == EINTR)
509                                 continue;
510
511                         break;
512                 }
513
514                 if (!pktlen)
515                         break;
516
517                 if (!forward_bcast && !forward_dhcp)
518                         continue;
519
520                 if (forward_dhcp_packet(rif, pktbuf, pktlen))
521                         continue;
522
523                 if (forward_bcast)
524                         forward_bcast_packet(rif, pktbuf, pktlen);
525         } while (1);
526 }
527
528
529 static int init_interface(struct relayd_interface *rif)
530 {
531         struct sockaddr_ll *sll = &rif->sll;
532         struct sockaddr_in *sin;
533         struct ifreq ifr;
534         int fd = rif->fd.fd;
535 #ifdef PACKET_RECV_TYPE
536         unsigned int pkt_type;
537 #endif
538
539         fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
540         if (fd < 0)
541                 return -1;
542
543         rif->fd.fd = fd;
544
545         memset(&ifr, 0, sizeof(ifr));
546         strcpy(ifr.ifr_name, rif->ifname);
547
548         if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
549                 perror("ioctl(SIOCGIFHWADDR)");
550                 return -1;
551         }
552
553         memcpy(sll->sll_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
554         sll->sll_family = AF_PACKET;
555         sll->sll_protocol = htons(ETH_P_ARP);
556         sll->sll_pkttype = PACKET_BROADCAST;
557         sll->sll_hatype = ARPHRD_ETHER;
558         sll->sll_halen = ETH_ALEN;
559
560         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
561                 perror("ioctl(SIOCGIFINDEX)");
562                 return -1;
563         }
564
565         sll->sll_ifindex = ifr.ifr_ifindex;
566
567         if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
568                 memcpy(rif->src_ip, DUMMY_IP, sizeof(rif->src_ip));
569         } else {
570                 sin = (struct sockaddr_in *) &ifr.ifr_addr;
571                 memcpy(rif->src_ip, &sin->sin_addr.s_addr, sizeof(rif->src_ip));
572         }
573
574         if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
575                 perror("bind(ETH_P_ARP)");
576                 return -1;
577         }
578
579         rif->fd.cb = recv_packet;
580         uloop_fd_add(&rif->fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
581
582         if (!forward_bcast && !forward_dhcp)
583                 return 0;
584
585         fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
586         if (fd < 0)
587                 return 0;
588
589         rif->bcast_fd.fd = fd;
590         rif->bcast_fd.cb = recv_bcast_packet;
591
592         memcpy(&rif->bcast_sll, &rif->sll, sizeof(rif->bcast_sll));
593         sll = &rif->bcast_sll;
594         sll->sll_protocol = htons(ETH_P_IP);
595
596         if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
597                 perror("bind(ETH_P_IP)");
598                 return 0;
599         }
600
601 #ifdef PACKET_RECV_TYPE
602         pkt_type = (1 << PACKET_BROADCAST);
603         setsockopt(fd, SOL_PACKET, PACKET_RECV_TYPE, &pkt_type, sizeof(pkt_type));
604 #endif
605
606         uloop_fd_add(&rif->bcast_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
607         return 0;
608 }
609
610 static int init_interfaces(void)
611 {
612         struct relayd_interface *rif;
613         int ret;
614
615         list_for_each_entry(rif, &interfaces, list) {
616                 ret = init_interface(rif);
617                 if (ret < 0)
618                         return ret;
619         }
620
621         return 0;
622 }
623
624 static void del_interface(struct relayd_interface *rif)
625 {
626         struct relayd_host *host, *htmp;
627
628         list_for_each_entry_safe(host, htmp, &rif->hosts, list) {
629                 del_host(host);
630         }
631         free(rif);
632 }
633
634 static void cleanup_interfaces(void)
635 {
636         struct relayd_interface *rif, *rtmp;
637
638         list_for_each_entry_safe(rif, rtmp, &interfaces, list) {
639                 del_interface(rif);
640         }
641 }
642
643 static int alloc_interface(const char *ifname, bool managed)
644 {
645         struct relayd_interface *rif;
646
647         if (strlen(ifname) >= IFNAMSIZ)
648                 return -1;
649
650         rif = calloc(1, sizeof(*rif));
651         if (!rif)
652                 return -1;
653
654         INIT_LIST_HEAD(&rif->list);
655         INIT_LIST_HEAD(&rif->hosts);
656         strcpy(rif->ifname, ifname);
657         list_add(&rif->list, &interfaces);
658         rif->managed = managed;
659
660         return 0;
661 }
662
663 #ifndef NDA_RTA
664 #define NDA_RTA(r) \
665     ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
666 #endif
667
668 static void rtnl_parse_newneigh(struct nlmsghdr *h)
669 {
670         struct relayd_interface *rif = NULL;
671         struct ndmsg *r = NLMSG_DATA(h);
672         const uint8_t *lladdr = NULL;
673         const uint8_t *ipaddr = NULL;
674         struct rtattr *rta;
675         int len;
676
677         if (r->ndm_family != AF_INET)
678                 return;
679
680         list_for_each_entry(rif, &interfaces, list) {
681                 if (rif->sll.sll_ifindex == r->ndm_ifindex)
682                         goto found_interface;
683         }
684         return;
685
686 found_interface:
687         len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
688         for (rta = NDA_RTA(r); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
689                 switch(rta->rta_type) {
690                 case NDA_LLADDR:
691                         lladdr = RTA_DATA(rta);
692                         break;
693                 case NDA_DST:
694                         ipaddr = RTA_DATA(rta);
695                         break;
696                 default:
697                         break;
698                 }
699         }
700
701         if (!lladdr || !ipaddr || (r->ndm_state & (NUD_INCOMPLETE|NUD_FAILED)))
702                 return;
703
704         if (!memcmp(lladdr, "\x00\x00\x00\x00\x00\x00", ETH_ALEN))
705                 return;
706
707         DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT" ("MAC_FMT")\n",
708                 rif->ifname, IP_BUF(ipaddr), MAC_BUF(lladdr));
709         refresh_host(rif, lladdr, ipaddr);
710 }
711
712 static void rtnl_parse_packet(void *data, int len)
713 {
714         struct nlmsghdr *h;
715
716         for (h = data; NLMSG_OK(h, len); h = NLMSG_NEXT(h, len)) {
717                 if (h->nlmsg_type == NLMSG_DONE ||
718                     h->nlmsg_type == NLMSG_ERROR)
719                         return;
720
721                 if (h->nlmsg_seq != rtnl_dump_seq)
722                         continue;
723
724                 if (h->nlmsg_type == RTM_NEWNEIGH)
725                         rtnl_parse_newneigh(h);
726         }
727 }
728
729 static void rtnl_cb(struct uloop_fd *fd, unsigned int events)
730 {
731         struct sockaddr_nl nladdr;
732         static uint8_t buf[16384];
733         struct iovec iov = {
734                 .iov_base = buf,
735                 .iov_len = sizeof(buf),
736         };
737         struct msghdr msg = {
738                 .msg_name = &nladdr,
739                 .msg_namelen = sizeof(nladdr),
740                 .msg_iov = &iov,
741                 .msg_iovlen = 1,
742         };
743
744         do {
745                 int len;
746
747                 len = recvmsg(rtnl_sock.fd, &msg, 0);
748                 if (len < 0) {
749                         if (errno == EINTR)
750                                 continue;
751
752                         return;
753                 }
754
755                 if (!len)
756                         break;
757
758                 if (nladdr.nl_pid != 0)
759                         continue;
760
761                 rtnl_parse_packet(buf, len);
762         } while (1);
763 }
764
765 static int rtnl_init(void)
766 {
767         struct sockaddr_nl snl_local;
768         static struct {
769                 struct nlmsghdr nlh;
770                 struct rtgenmsg g;
771         } req = {
772                 .nlh = {
773                         .nlmsg_len = sizeof(req),
774                         .nlmsg_type = RTM_GETNEIGH,
775                         .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
776                         .nlmsg_pid = 0,
777                 },
778                 .g.rtgen_family = AF_INET,
779         };
780
781         rtnl_sock.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
782         if (rtnl_sock.fd < 0) {
783                 perror("socket(AF_NETLINK)");
784                 return -1;
785         }
786
787         snl_local.nl_family = AF_NETLINK;
788
789         if (bind(rtnl_sock.fd, (struct sockaddr *) &snl_local, sizeof(struct sockaddr_nl)) < 0) {
790                 perror("bind");
791                 close(rtnl_sock.fd);
792                 return -1;
793         }
794
795         rtnl_sock.cb = rtnl_cb;
796         uloop_fd_add(&rtnl_sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
797
798         rtnl_seq = time(NULL);
799         rtnl_dump_seq = rtnl_seq;
800         req.nlh.nlmsg_seq = rtnl_seq;
801         send(rtnl_sock.fd, &req, sizeof(req), 0);
802
803         return 0;
804 }
805
806 static void die(int signo)
807 {
808         /*
809          * When we hit SIGTERM, clean up interfaces directly, so that we
810          * won't leave our routing in an invalid state.
811          */
812         cleanup_interfaces();
813         exit(1);
814 }
815
816 static int usage(const char *progname)
817 {
818         fprintf(stderr, "Usage: %s <options>\n"
819                         "\n"
820                         "Options:\n"
821                         "       -d              Enable debug messages\n"
822                         "       -i <ifname>     Add an interface for relaying\n"
823                         "       -I <ifname>     Same as -i, except with ARP cache and host route management\n"
824                         "                       You need to specify at least two interfaces\n"
825                         "       -t <timeout>    Host entry expiry timeout\n"
826                         "       -B              Enable broadcast forwarding\n"
827                         "       -D              Enable DHCP forwarding\n"
828                         "\n",
829                 progname);
830         return -1;
831 }
832
833 int main(int argc, char **argv)
834 {
835         bool managed;
836         int ifnum = 0;
837         int ch;
838
839         debug = 0;
840         inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
841         if (inet_sock < 0) {
842                 perror("socket(AF_INET)");
843                 return 1;
844         }
845
846         host_timeout = 60;
847         forward_bcast = 0;
848         uloop_init();
849
850         while ((ch = getopt(argc, argv, "I:i:t:BDd")) != -1) {
851                 switch(ch) {
852                 case 'I':
853                         managed = true;
854                         /* fall through */
855                 case 'i':
856                         ifnum++;
857                         if (alloc_interface(optarg, managed) < 0)
858                                 return 1;
859
860                         managed = false;
861                         break;
862                 case 't':
863                         host_timeout = atoi(optarg);
864                         if (host_timeout <= 0)
865                                 return usage(argv[0]);
866                         break;
867                 case 'd':
868                         debug++;
869                         break;
870                 case 'B':
871                         forward_bcast = 1;
872                         break;
873                 case 'D':
874                         forward_dhcp = 1;
875                         break;
876                 case '?':
877                 default:
878                         return usage(argv[0]);
879                 }
880         }
881
882         if (list_empty(&interfaces))
883                 return usage(argv[0]);
884
885         if (ifnum < 2) {
886                 fprintf(stderr, "ERROR: Need at least 2 interfaces for relaying\n");
887                 return -1;
888         }
889
890         argc -= optind;
891         argv += optind;
892
893         signal(SIGTERM, die);
894         signal(SIGHUP, die);
895         signal(SIGUSR1, die);
896         signal(SIGUSR2, die);
897
898         if (init_interfaces() < 0)
899                 return 1;
900
901         if (rtnl_init() < 0)
902                 return 1;
903
904         uloop_run();
905         uloop_done();
906
907         cleanup_interfaces();
908         uloop_fd_delete(&rtnl_sock);
909         close(rtnl_sock.fd);
910         close(inet_sock);
911
912         return 0;
913 }