9361c97febd4cfa21caadcb31479be448095b84e
[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 #include <sys/socket.h>
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <string.h>
31
32 #include "relayd.h"
33
34 static LIST_HEAD(pending_routes);
35 LIST_HEAD(interfaces);
36 int debug;
37
38 static int host_timeout;
39 static int host_ping_tries;
40 static int inet_sock;
41 static int forward_bcast;
42 static int forward_dhcp;
43
44 uint8_t local_addr[4];
45 int local_route_table;
46
47 struct relayd_pending_route {
48         struct relayd_route rt;
49         struct uloop_timeout timeout;
50         uint8_t gateway[4];
51 };
52
53 static struct relayd_host *find_host_by_ipaddr(struct relayd_interface *rif, const uint8_t *ipaddr)
54 {
55         struct relayd_host *host;
56
57         if (!rif) {
58                 list_for_each_entry(rif, &interfaces, list) {
59                         host = find_host_by_ipaddr(rif, ipaddr);
60                         if (!host)
61                                 continue;
62
63                         return host;
64                 }
65                 return NULL;
66         }
67
68         list_for_each_entry(host, &rif->hosts, list) {
69                 if (memcmp(ipaddr, host->ipaddr, sizeof(host->ipaddr)) != 0)
70                         continue;
71
72                 return host;
73         }
74         return NULL;
75 }
76
77 static void add_arp(struct relayd_host *host)
78 {
79         struct sockaddr_in *sin;
80         struct arpreq arp;
81
82         strncpy(arp.arp_dev, host->rif->ifname, sizeof(arp.arp_dev));
83         arp.arp_flags = ATF_COM;
84
85         arp.arp_ha.sa_family = ARPHRD_ETHER;
86         memcpy(arp.arp_ha.sa_data, host->lladdr, ETH_ALEN);
87
88         sin = (struct sockaddr_in *) &arp.arp_pa;
89         sin->sin_family = AF_INET;
90         memcpy(&sin->sin_addr, host->ipaddr, sizeof(host->ipaddr));
91
92         ioctl(inet_sock, SIOCSARP, &arp);
93 }
94
95 static void timeout_host_route(struct uloop_timeout *timeout)
96 {
97         struct relayd_pending_route *rt;
98
99         rt = container_of(timeout, struct relayd_pending_route, timeout);
100         list_del(&rt->rt.list);
101         free(rt);
102 }
103
104 void relayd_add_host_route(struct relayd_host *host, const uint8_t *dest, uint8_t mask)
105 {
106         struct relayd_route *rt;
107
108         list_for_each_entry(rt, &host->routes, list) {
109                 if (!memcmp(rt->dest, dest, sizeof(rt->dest)) && rt->mask == mask)
110                         return;
111         }
112
113         rt = calloc(1, sizeof(*rt));
114         if (!rt)
115                 return;
116
117         list_add(&rt->list, &host->routes);
118         memcpy(rt->dest, dest, sizeof(rt->dest));
119         rt->mask = mask;
120         relayd_add_route(host, rt);
121 }
122
123 static void del_host(struct relayd_host *host)
124 {
125         struct relayd_route *route, *tmp;
126
127         DPRINTF(1, "%s: deleting host "IP_FMT" ("MAC_FMT")\n", host->rif->ifname,
128                 IP_BUF(host->ipaddr), MAC_BUF(host->lladdr));
129
130         list_for_each_entry_safe(route, tmp, &host->routes, list) {
131                 relayd_del_route(host, route);
132                 list_del(&route->list);
133                 free(route);
134         }
135         if (host->rif->managed)
136                 relayd_del_route(host, NULL);
137         uloop_timeout_cancel(&host->timeout);
138         list_del(&host->list);
139         free(host);
140 }
141
142 static void fill_arp_packet(struct arp_packet *pkt, struct relayd_interface *rif,
143                              const uint8_t spa[4], const uint8_t tpa[4])
144 {
145         memset(pkt, 0, sizeof(*pkt));
146
147         pkt->eth.ether_type = htons(ETHERTYPE_ARP);
148         memcpy(pkt->eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
149
150         memcpy(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
151         memcpy(pkt->arp.arp_spa, spa, 4);
152         memcpy(pkt->arp.arp_tpa, tpa, 4);
153
154         pkt->arp.arp_hrd = htons(ARPHRD_ETHER);
155         pkt->arp.arp_pro = htons(ETH_P_IP);
156         pkt->arp.arp_hln = ETH_ALEN;
157         pkt->arp.arp_pln = 4;
158 }
159
160 static void send_arp_request(struct relayd_interface *rif, const uint8_t *ipaddr)
161 {
162         struct arp_packet pkt;
163
164         fill_arp_packet(&pkt, rif, rif->src_ip, ipaddr);
165
166         pkt.arp.arp_op = htons(ARPOP_REQUEST);
167         memcpy(pkt.arp.arp_spa, rif->src_ip, ETH_ALEN);
168         memset(pkt.arp.arp_tha, 0, ETH_ALEN);
169         memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN);
170
171         DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
172                 rif->ifname, IP_BUF(pkt.arp.arp_tpa),
173                 IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
174
175         sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
176                 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
177 }
178
179 void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8_t mask, int timeout)
180 {
181         struct relayd_pending_route *rt;
182         struct relayd_interface *rif;
183         struct relayd_host *host;
184
185         host = find_host_by_ipaddr(NULL, gateway);
186         if (host) {
187                 relayd_add_host_route(host, dest, mask);
188                 return;
189         }
190
191         rt = calloc(1, sizeof(*rt));
192         if (!rt)
193                 return;
194
195         memcpy(rt->gateway, gateway, sizeof(rt->gateway));
196         memcpy(rt->rt.dest, dest, sizeof(rt->rt.dest));
197         rt->rt.mask = mask;
198         list_add(&rt->rt.list, &pending_routes);
199         if (timeout <= 0)
200                 return;
201
202         rt->timeout.cb = timeout_host_route;
203         uloop_timeout_set(&rt->timeout, 10000);
204         list_for_each_entry(rif, &interfaces, list) {
205                 send_arp_request(rif, gateway);
206         }
207 }
208
209 static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4],
210                            uint8_t tha[ETH_ALEN], uint8_t tpa[4])
211 {
212         struct arp_packet pkt;
213
214         fill_arp_packet(&pkt, rif, spa, tpa);
215
216         pkt.arp.arp_op = htons(ARPOP_REPLY);
217         if (tha) {
218                 memcpy(pkt.eth.ether_dhost, tha, ETH_ALEN);
219                 memcpy(pkt.arp.arp_tha, tha, ETH_ALEN);
220
221                 DPRINTF(2, "%s: sending ARP reply to "IP_FMT", "IP_FMT" is at ("MAC_FMT")\n",
222                         rif->ifname, IP_BUF(pkt.arp.arp_tpa),
223                         IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
224         } else {
225                 memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN);
226                 memset(pkt.arp.arp_tha, 0, ETH_ALEN);
227
228                 DPRINTF(2, "%s: sending gratuitous ARP: "IP_FMT" is at ("MAC_FMT")\n",
229                         rif->ifname, IP_BUF(pkt.arp.arp_tpa),
230                         MAC_BUF(pkt.eth.ether_shost));
231         }
232
233         sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
234                 (struct sockaddr *) &rif->sll, sizeof(rif->sll));
235 }
236
237 static void host_entry_timeout(struct uloop_timeout *timeout)
238 {
239         struct relayd_host *host = container_of(timeout, struct relayd_host, timeout);
240
241         /*
242          * When a host is behind a managed interface, we must not expire its host
243          * entry prematurely, as this will cause routes to the node to expire,
244          * leading to loss of connectivity from the other side.
245          * When the timeout is reached, try pinging the host a few times before
246          * giving up on it.
247          */
248         if (host->rif->managed && host->cleanup_pending < host_ping_tries) {
249                 send_arp_request(host->rif, host->ipaddr);
250                 host->cleanup_pending++;
251                 uloop_timeout_set(&host->timeout, 1000);
252                 return;
253         }
254         del_host(host);
255 }
256
257 static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
258 {
259         struct relayd_host *host;
260         struct relayd_pending_route *route, *rtmp;
261
262         DPRINTF(1, "%s: adding host "IP_FMT" ("MAC_FMT")\n", rif->ifname,
263                         IP_BUF(ipaddr), MAC_BUF(lladdr));
264
265         host = calloc(1, sizeof(*host));
266         INIT_LIST_HEAD(&host->routes);
267         host->rif = rif;
268         memcpy(host->ipaddr, ipaddr, sizeof(host->ipaddr));
269         memcpy(host->lladdr, lladdr, sizeof(host->lladdr));
270         list_add(&host->list, &rif->hosts);
271         host->timeout.cb = host_entry_timeout;
272         uloop_timeout_set(&host->timeout, host_timeout * 1000);
273
274         add_arp(host);
275         if (rif->managed)
276                 relayd_add_route(host, NULL);
277
278         list_for_each_entry_safe(route, rtmp, &pending_routes, rt.list) {
279                 if (memcmp(route->gateway, ipaddr, 4) != 0)
280                         continue;
281
282                 relayd_add_host_route(host, route->rt.dest, route->rt.mask);
283                 if (!route->timeout.pending)
284                         continue;
285
286                 uloop_timeout_cancel(&route->timeout);
287                 list_del(&route->rt.list);
288                 free(route);
289         }
290
291         return host;
292 }
293
294 struct relayd_host *relayd_refresh_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
295 {
296         struct relayd_host *host;
297
298         host = find_host_by_ipaddr(rif, ipaddr);
299         if (!host) {
300                 host = find_host_by_ipaddr(NULL, ipaddr);
301
302                 /* 
303                  * When we suddenly see the host appearing on a different interface,
304                  * reduce the timeout to make the old entry expire faster, in case the
305                  * host has moved.
306                  * If the old entry is behind a managed interface, it will be pinged
307                  * before we expire it
308                  */
309                 if (host && !host->cleanup_pending) {
310                         uloop_timeout_set(&host->timeout, 1);
311                         return NULL;
312                 }
313
314                 host = add_host(rif, lladdr, ipaddr);
315         } else {
316                 host->cleanup_pending = false;
317                 uloop_timeout_set(&host->timeout, host_timeout * 1000);
318         }
319
320         return host;
321 }
322
323 static void relay_arp_request(struct relayd_interface *from_rif, struct arp_packet *pkt)
324 {
325         struct relayd_interface *rif;
326         struct arp_packet reqpkt;
327
328         memcpy(&reqpkt, pkt, sizeof(reqpkt));
329         list_for_each_entry(rif, &interfaces, list) {
330                 if (rif == from_rif)
331                         continue;
332
333                 memcpy(reqpkt.eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
334                 memcpy(reqpkt.arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
335
336                 DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
337                         rif->ifname, IP_BUF(reqpkt.arp.arp_tpa),
338                         IP_BUF(reqpkt.arp.arp_spa), MAC_BUF(reqpkt.eth.ether_shost));
339
340                 sendto(rif->fd.fd, &reqpkt, sizeof(reqpkt), 0,
341                         (struct sockaddr *) &rif->sll, sizeof(rif->sll));
342         }
343 }
344
345 static void recv_arp_request(struct relayd_interface *rif, struct arp_packet *pkt)
346 {
347         struct relayd_host *host;
348
349         DPRINTF(2, "%s: ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
350                 rif->ifname,
351                 IP_BUF(pkt->arp.arp_tpa),
352                 IP_BUF(pkt->arp.arp_spa),
353                 MAC_BUF(pkt->eth.ether_shost));
354
355         if (!memcmp(pkt->arp.arp_spa, "\x00\x00\x00\x00", 4))
356                 return;
357
358         if (local_route_table && !memcmp(pkt->arp.arp_tpa, local_addr, sizeof(local_addr))) {
359                 send_arp_reply(rif, local_addr, pkt->arp.arp_sha, pkt->arp.arp_spa);
360                 return;
361         }
362
363         relayd_refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa);
364
365         host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
366
367         /*
368          * If a host is being pinged because of a timeout, do not use the cached
369          * entry here. That way we can avoid giving out stale data in case the node
370          * has moved. We shouldn't relay requests here either, as we might miss our
371          * chance to create a host route.
372          */
373         if (host && host->cleanup_pending)
374                 return;
375
376         relay_arp_request(rif, pkt);
377 }
378
379
380 static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt)
381 {
382         struct relayd_interface *to_rif;
383         struct relayd_host *host;
384
385         DPRINTF(2, "%s: received ARP reply for "IP_FMT" from "MAC_FMT", deliver to "IP_FMT"\n",
386                 rif->ifname,
387                 IP_BUF(pkt->arp.arp_spa),
388                 MAC_BUF(pkt->eth.ether_shost),
389                 IP_BUF(pkt->arp.arp_tpa));
390
391         if (memcmp(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN) != 0)
392                 relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
393
394         if (!memcmp(pkt->arp.arp_tpa, rif->src_ip, 4)) {
395                 /*
396                  * locally initiated lookup, relay as gratuitous ARP
397                  * to all other interfaces
398                  */
399                 list_for_each_entry(to_rif, &interfaces, list) {
400                         if (rif == to_rif)
401                                 continue;
402
403                         send_arp_reply(to_rif, pkt->arp.arp_spa, NULL, pkt->arp.arp_spa);
404                 }
405                 return;
406         }
407
408         host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
409         if (!host)
410                 return;
411
412         if (host->rif == rif)
413                 return;
414
415         send_arp_reply(host->rif, pkt->arp.arp_spa, host->lladdr, host->ipaddr);
416 }
417
418 static void recv_packet(struct uloop_fd *fd, unsigned int events)
419 {
420         struct relayd_interface *rif = container_of(fd, struct relayd_interface, fd);
421         struct arp_packet *pkt;
422         static char pktbuf[4096];
423         int pktlen;
424
425         do {
426                 if (rif->fd.error)
427                         uloop_end();
428
429                 pktlen = recv(rif->fd.fd, pktbuf, sizeof(pktbuf), 0);
430                 if (pktlen < 0) {
431                         if (errno == EINTR)
432                                 continue;
433
434                         break;
435                 }
436
437                 if (!pktlen)
438                         break;
439
440                 pkt = (void *)pktbuf;
441                 if (pkt->arp.arp_op == htons(ARPOP_REPLY))
442                         recv_arp_reply(rif, pkt);
443                 else if (pkt->arp.arp_op == htons(ARPOP_REQUEST))
444                         recv_arp_request(rif, pkt);
445                 else
446                         DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt->arp.arp_op));
447
448         } while (1);
449 }
450
451 void relayd_forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len)
452 {
453         struct relayd_interface *rif;
454         struct ether_header *eth = packet;
455
456         list_for_each_entry(rif, &interfaces, list) {
457                 if (rif == from_rif)
458                         continue;
459
460                 DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif->ifname, rif->ifname);
461                 memcpy(eth->ether_shost, rif->sll.sll_addr, ETH_ALEN);
462                 send(rif->bcast_fd.fd, packet, len, 0);
463         }
464 }
465
466 static void recv_bcast_packet(struct uloop_fd *fd, unsigned int events)
467 {
468         struct relayd_interface *rif = container_of(fd, struct relayd_interface, bcast_fd);
469         static char pktbuf[4096];
470         int pktlen;
471
472         do {
473                 if (rif->fd.error)
474                         uloop_end();
475
476                 pktlen = recv(rif->bcast_fd.fd, pktbuf, sizeof(pktbuf), 0);
477                 if (pktlen < 0) {
478                         if (errno == EINTR)
479                                 continue;
480
481                         break;
482                 }
483
484                 if (!pktlen)
485                         break;
486
487                 if (!forward_bcast && !forward_dhcp)
488                         continue;
489
490                 if (relayd_handle_dhcp_packet(rif, pktbuf, pktlen, forward_dhcp))
491                         continue;
492
493                 if (forward_bcast)
494                         relayd_forward_bcast_packet(rif, pktbuf, pktlen);
495         } while (1);
496 }
497
498
499 static int init_interface(struct relayd_interface *rif)
500 {
501         struct sockaddr_ll *sll = &rif->sll;
502         struct sockaddr_in *sin;
503         struct ifreq ifr;
504         int fd = rif->fd.fd;
505 #ifdef PACKET_RECV_TYPE
506         unsigned int pkt_type;
507 #endif
508
509         fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
510         if (fd < 0)
511                 return -1;
512
513         rif->fd.fd = fd;
514
515         memset(&ifr, 0, sizeof(ifr));
516         strcpy(ifr.ifr_name, rif->ifname);
517
518         if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
519                 perror("ioctl(SIOCGIFHWADDR)");
520                 return -1;
521         }
522
523         memcpy(sll->sll_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
524         sll->sll_family = AF_PACKET;
525         sll->sll_protocol = htons(ETH_P_ARP);
526         sll->sll_pkttype = PACKET_BROADCAST;
527         sll->sll_hatype = ARPHRD_ETHER;
528         sll->sll_halen = ETH_ALEN;
529
530         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
531                 perror("ioctl(SIOCGIFINDEX)");
532                 return -1;
533         }
534
535         sll->sll_ifindex = ifr.ifr_ifindex;
536
537         if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
538                 memcpy(rif->src_ip, DUMMY_IP, sizeof(rif->src_ip));
539         } else {
540                 sin = (struct sockaddr_in *) &ifr.ifr_addr;
541                 memcpy(rif->src_ip, &sin->sin_addr.s_addr, sizeof(rif->src_ip));
542         }
543
544         if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
545                 perror("bind(ETH_P_ARP)");
546                 return -1;
547         }
548
549         rif->fd.cb = recv_packet;
550         uloop_fd_add(&rif->fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
551
552         if (!forward_bcast && !forward_dhcp)
553                 return 0;
554
555         fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
556         if (fd < 0)
557                 return 0;
558
559         rif->bcast_fd.fd = fd;
560         rif->bcast_fd.cb = recv_bcast_packet;
561
562         memcpy(&rif->bcast_sll, &rif->sll, sizeof(rif->bcast_sll));
563         sll = &rif->bcast_sll;
564         sll->sll_protocol = htons(ETH_P_IP);
565
566         if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
567                 perror("bind(ETH_P_IP)");
568                 return 0;
569         }
570
571 #ifdef PACKET_RECV_TYPE
572         pkt_type = (1 << PACKET_BROADCAST);
573         setsockopt(fd, SOL_PACKET, PACKET_RECV_TYPE, &pkt_type, sizeof(pkt_type));
574 #endif
575
576         uloop_fd_add(&rif->bcast_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
577         relayd_add_interface_routes(rif);
578         return 0;
579 }
580
581 static void ping_static_routes(void)
582 {
583         struct relayd_pending_route *rt;
584         struct relayd_interface *rif;
585
586         list_for_each_entry(rt, &pending_routes, rt.list)
587                 list_for_each_entry(rif, &interfaces, list)
588                         send_arp_request(rif, rt->gateway);
589 }
590
591 static int init_interfaces(void)
592 {
593         struct relayd_interface *rif;
594         int ret;
595
596         list_for_each_entry(rif, &interfaces, list) {
597                 ret = init_interface(rif);
598                 if (ret < 0)
599                         return ret;
600         }
601
602         return 0;
603 }
604
605 static void cleanup_hosts(void)
606 {
607         struct relayd_interface *rif;
608         struct relayd_host *host, *tmp;
609
610         list_for_each_entry(rif, &interfaces, list) {
611                 list_for_each_entry_safe(host, tmp, &rif->hosts, list) {
612                         del_host(host);
613                 }
614         }
615 }
616
617 static void free_interfaces(void)
618 {
619         struct relayd_interface *rif, *rtmp;
620
621         list_for_each_entry_safe(rif, rtmp, &interfaces, list) {
622                 relayd_del_interface_routes(rif);
623                 list_del(&rif->list);
624                 free(rif);
625         }
626 }
627
628 static struct relayd_interface *alloc_interface(const char *ifname, bool managed)
629 {
630         struct relayd_interface *rif;
631
632         if (strlen(ifname) >= IFNAMSIZ)
633                 return NULL;
634
635         rif = calloc(1, sizeof(*rif));
636         if (!rif)
637                 return NULL;
638
639         INIT_LIST_HEAD(&rif->hosts);
640         strcpy(rif->ifname, ifname);
641         list_add(&rif->list, &interfaces);
642         rif->managed = managed;
643
644         return rif;
645 }
646
647 static void die(int signo)
648 {
649         /*
650          * When we hit SIGTERM, clean up interfaces directly, so that we
651          * won't leave our routing in an invalid state.
652          */
653         cleanup_hosts();
654         free_interfaces();
655         exit(1);
656 }
657
658 static int usage(const char *progname)
659 {
660         fprintf(stderr, "Usage: %s <options>\n"
661                         "\n"
662                         "Options:\n"
663                         "       -d              Enable debug messages\n"
664                         "       -i <ifname>     Add an interface for relaying\n"
665                         "       -I <ifname>     Same as -i, except with ARP cache and host route management\n"
666                         "                       You need to specify at least two interfaces\n"
667                         "       -G <ip>         Set a gateway IP for clients\n"
668                         "       -R <gateway>:<net>/<mask>\n"
669                         "                       Add a static route for <net>/<mask> via <gateway>\n"
670                         "       -t <timeout>    Host entry expiry timeout\n"
671                         "       -p <tries>      Number of ARP ping attempts before considering a host dead\n"
672                         "       -T <table>      Set routing table number for automatically added routes\n"
673                         "       -B              Enable broadcast forwarding\n"
674                         "       -D              Enable DHCP forwarding\n"
675                         "       -L <ipaddr>     Enable local access using <ipaddr> as source address\n"
676                         "\n",
677                 progname);
678         return -1;
679 }
680
681 int main(int argc, char **argv)
682 {
683         struct relayd_interface *rif = NULL;
684         struct in_addr addr, addr2;
685         bool local_addr_valid = false;
686         bool managed;
687         int ifnum = 0;
688         char *s, *s2;
689         int mask;
690         int ch;
691
692         debug = 0;
693         inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
694         if (inet_sock < 0) {
695                 perror("socket(AF_INET)");
696                 return 1;
697         }
698
699         host_timeout = 30;
700         host_ping_tries = 5;
701         forward_bcast = 0;
702         local_route_table = 0;
703         uloop_init();
704
705         while ((ch = getopt(argc, argv, "I:i:t:BDdT:G:R:L:")) != -1) {
706                 switch(ch) {
707                 case 'I':
708                         managed = true;
709                         /* fall through */
710                 case 'i':
711                         ifnum++;
712                         rif = alloc_interface(optarg, managed);
713                         if (!rif)
714                                 return 1;
715
716                         managed = false;
717                         break;
718                 case 't':
719                         host_timeout = atoi(optarg);
720                         if (host_timeout <= 0)
721                                 return usage(argv[0]);
722                         break;
723                 case 'p':
724                         host_ping_tries = atoi(optarg);
725                         if (host_ping_tries <= 0)
726                                 return usage(argv[0]);
727                         break;
728                 case 'd':
729                         debug++;
730                         break;
731                 case 'B':
732                         forward_bcast = 1;
733                         break;
734                 case 'D':
735                         forward_dhcp = 1;
736                         break;
737                 case 'T':
738                         route_table = atoi(optarg);
739                         if (route_table <= 0)
740                                 return usage(argv[0]);
741                         break;
742                 case 'G':
743                         if (!inet_aton(optarg, &addr)) {
744                                 fprintf(stderr, "Address '%s' not found\n", optarg);
745                                 return 1;
746                         }
747                         relayd_add_pending_route((uint8_t *) &addr.s_addr, (const uint8_t *) "\x00\x00\x00\x00", 0, 0);
748                         break;
749                 case 'L':
750                         if (!inet_aton(optarg, &addr)) {
751                                 fprintf(stderr, "Address '%s' not found\n", optarg);
752                                 return 1;
753                         }
754                         memcpy(&local_addr, &addr.s_addr, sizeof(local_addr));
755                         local_addr_valid = true;
756                         break;
757                 case 'R':
758                         s = strchr(optarg, ':');
759                         if (!s)
760                                 return usage(argv[0]);
761
762                         *(s++) = 0;
763                         if (!inet_aton(optarg, &addr)) {
764                                 fprintf(stderr, "Address '%s' not found\n", optarg);
765                                 return 1;
766                         }
767
768                         s2 = strchr(s, '/');
769                         if (!s2)
770                                 return usage(argv[0]);
771
772                         *(s2++) = 0;
773                         if (!inet_aton(s, &addr2)) {
774                                 fprintf(stderr, "Address '%s' not found\n", s);
775                                 return 1;
776                         }
777
778                         mask = atoi(s2);
779                         if (mask < 0 || mask > 32)
780                                 return usage(argv[0]);
781
782                         relayd_add_pending_route((uint8_t *) &addr.s_addr, (uint8_t *) &addr2.s_addr, mask, 0);
783                         break;
784                 case '?':
785                 default:
786                         return usage(argv[0]);
787                 }
788         }
789
790         if (list_empty(&interfaces))
791                 return usage(argv[0]);
792
793         if (ifnum < 2) {
794                 fprintf(stderr, "ERROR: Need at least 2 interfaces for relaying\n");
795                 return -1;
796         }
797
798         argc -= optind;
799         argv += optind;
800
801         signal(SIGTERM, die);
802         signal(SIGHUP, die);
803         signal(SIGUSR1, die);
804         signal(SIGUSR2, die);
805
806         if (local_addr_valid)
807                 local_route_table = route_table++;
808
809         if (relayd_rtnl_init() < 0)
810                 return 1;
811
812         if (init_interfaces() < 0)
813                 return 1;
814
815         ping_static_routes();
816
817         uloop_run();
818         uloop_done();
819
820         cleanup_hosts();
821         free_interfaces();
822         relayd_rtnl_done();
823         close(inet_sock);
824
825         return 0;
826 }