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