implement local ip access through policy routing
[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 relayd_refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa);
349
350 host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
351
352 /*
353 * If a host is being pinged because of a timeout, do not use the cached
354 * entry here. That way we can avoid giving out stale data in case the node
355 * has moved. We shouldn't relay requests here either, as we might miss our
356 * chance to create a host route.
357 */
358 if (host && host->cleanup_pending)
359 return;
360
361 relay_arp_request(rif, pkt);
362 }
363
364
365 static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt)
366 {
367 struct relayd_host *host;
368
369 DPRINTF(2, "%s: received ARP reply for "IP_FMT" from "MAC_FMT", deliver to "IP_FMT"\n",
370 rif->ifname,
371 IP_BUF(pkt->arp.arp_spa),
372 MAC_BUF(pkt->eth.ether_shost),
373 IP_BUF(pkt->arp.arp_tpa));
374
375 if (memcmp(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN) != 0)
376 relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
377
378 if (!memcmp(pkt->arp.arp_tpa, rif->src_ip, 4))
379 return;
380
381 host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
382 if (!host)
383 return;
384
385 if (host->rif == rif)
386 return;
387
388 send_arp_reply(host->rif, pkt->arp.arp_spa, host->lladdr, host->ipaddr);
389 }
390
391 static void recv_packet(struct uloop_fd *fd, unsigned int events)
392 {
393 struct relayd_interface *rif = container_of(fd, struct relayd_interface, fd);
394 struct arp_packet *pkt;
395 static char pktbuf[4096];
396 int pktlen;
397
398 do {
399 if (rif->fd.error)
400 uloop_end();
401
402 pktlen = recv(rif->fd.fd, pktbuf, sizeof(pktbuf), 0);
403 if (pktlen < 0) {
404 if (errno == EINTR)
405 continue;
406
407 break;
408 }
409
410 if (!pktlen)
411 break;
412
413 pkt = (void *)pktbuf;
414 if (pkt->arp.arp_op == htons(ARPOP_REPLY))
415 recv_arp_reply(rif, pkt);
416 else if (pkt->arp.arp_op == htons(ARPOP_REQUEST))
417 recv_arp_request(rif, pkt);
418 else
419 DPRINTF(1, "received unknown packet type: %04x\n", ntohs(pkt->arp.arp_op));
420
421 } while (1);
422 }
423
424 void relayd_forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len)
425 {
426 struct relayd_interface *rif;
427 struct ether_header *eth = packet;
428
429 list_for_each_entry(rif, &interfaces, list) {
430 if (rif == from_rif)
431 continue;
432
433 DPRINTF(3, "%s: forwarding broadcast packet to %s\n", from_rif->ifname, rif->ifname);
434 memcpy(eth->ether_shost, rif->sll.sll_addr, ETH_ALEN);
435 send(rif->bcast_fd.fd, packet, len, 0);
436 }
437 }
438
439 static void recv_bcast_packet(struct uloop_fd *fd, unsigned int events)
440 {
441 struct relayd_interface *rif = container_of(fd, struct relayd_interface, bcast_fd);
442 static char pktbuf[4096];
443 int pktlen;
444
445 do {
446 if (rif->fd.error)
447 uloop_end();
448
449 pktlen = recv(rif->bcast_fd.fd, pktbuf, sizeof(pktbuf), 0);
450 if (pktlen < 0) {
451 if (errno == EINTR)
452 continue;
453
454 break;
455 }
456
457 if (!pktlen)
458 break;
459
460 if (!forward_bcast && !forward_dhcp)
461 continue;
462
463 if (relayd_handle_dhcp_packet(rif, pktbuf, pktlen, forward_dhcp))
464 continue;
465
466 if (forward_bcast)
467 relayd_forward_bcast_packet(rif, pktbuf, pktlen);
468 } while (1);
469 }
470
471
472 static int init_interface(struct relayd_interface *rif)
473 {
474 struct sockaddr_ll *sll = &rif->sll;
475 struct sockaddr_in *sin;
476 struct ifreq ifr;
477 int fd = rif->fd.fd;
478 #ifdef PACKET_RECV_TYPE
479 unsigned int pkt_type;
480 #endif
481
482 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
483 if (fd < 0)
484 return -1;
485
486 rif->fd.fd = fd;
487
488 memset(&ifr, 0, sizeof(ifr));
489 strcpy(ifr.ifr_name, rif->ifname);
490
491 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
492 perror("ioctl(SIOCGIFHWADDR)");
493 return -1;
494 }
495
496 memcpy(sll->sll_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
497 sll->sll_family = AF_PACKET;
498 sll->sll_protocol = htons(ETH_P_ARP);
499 sll->sll_pkttype = PACKET_BROADCAST;
500 sll->sll_hatype = ARPHRD_ETHER;
501 sll->sll_halen = ETH_ALEN;
502
503 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
504 perror("ioctl(SIOCGIFINDEX)");
505 return -1;
506 }
507
508 sll->sll_ifindex = ifr.ifr_ifindex;
509
510 if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
511 memcpy(rif->src_ip, DUMMY_IP, sizeof(rif->src_ip));
512 } else {
513 sin = (struct sockaddr_in *) &ifr.ifr_addr;
514 memcpy(rif->src_ip, &sin->sin_addr.s_addr, sizeof(rif->src_ip));
515 }
516
517 if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
518 perror("bind(ETH_P_ARP)");
519 return -1;
520 }
521
522 rif->fd.cb = recv_packet;
523 uloop_fd_add(&rif->fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
524
525 if (!forward_bcast && !forward_dhcp)
526 return 0;
527
528 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
529 if (fd < 0)
530 return 0;
531
532 rif->bcast_fd.fd = fd;
533 rif->bcast_fd.cb = recv_bcast_packet;
534
535 memcpy(&rif->bcast_sll, &rif->sll, sizeof(rif->bcast_sll));
536 sll = &rif->bcast_sll;
537 sll->sll_protocol = htons(ETH_P_IP);
538
539 if (bind(fd, (struct sockaddr *)sll, sizeof(struct sockaddr_ll)) < 0) {
540 perror("bind(ETH_P_IP)");
541 return 0;
542 }
543
544 #ifdef PACKET_RECV_TYPE
545 pkt_type = (1 << PACKET_BROADCAST);
546 setsockopt(fd, SOL_PACKET, PACKET_RECV_TYPE, &pkt_type, sizeof(pkt_type));
547 #endif
548
549 uloop_fd_add(&rif->bcast_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
550 relayd_add_interface_routes(rif);
551 return 0;
552 }
553
554 static void ping_static_routes(void)
555 {
556 struct relayd_pending_route *rt;
557 struct relayd_interface *rif;
558
559 list_for_each_entry(rt, &pending_routes, rt.list)
560 list_for_each_entry(rif, &interfaces, list)
561 send_arp_request(rif, rt->gateway);
562 }
563
564 static int init_interfaces(void)
565 {
566 struct relayd_interface *rif;
567 int ret;
568
569 list_for_each_entry(rif, &interfaces, list) {
570 ret = init_interface(rif);
571 if (ret < 0)
572 return ret;
573 }
574
575 return 0;
576 }
577
578 static void cleanup_hosts(void)
579 {
580 struct relayd_interface *rif;
581 struct relayd_host *host, *tmp;
582
583 list_for_each_entry(rif, &interfaces, list) {
584 list_for_each_entry_safe(host, tmp, &rif->hosts, list) {
585 del_host(host);
586 }
587 }
588 }
589
590 static void free_interfaces(void)
591 {
592 struct relayd_interface *rif, *rtmp;
593
594 list_for_each_entry_safe(rif, rtmp, &interfaces, list) {
595 relayd_del_interface_routes(rif);
596 list_del(&rif->list);
597 free(rif);
598 }
599 }
600
601 static struct relayd_interface *alloc_interface(const char *ifname, bool managed)
602 {
603 struct relayd_interface *rif;
604
605 if (strlen(ifname) >= IFNAMSIZ)
606 return NULL;
607
608 rif = calloc(1, sizeof(*rif));
609 if (!rif)
610 return NULL;
611
612 INIT_LIST_HEAD(&rif->hosts);
613 strcpy(rif->ifname, ifname);
614 list_add(&rif->list, &interfaces);
615 rif->managed = managed;
616
617 return rif;
618 }
619
620 static void die(int signo)
621 {
622 /*
623 * When we hit SIGTERM, clean up interfaces directly, so that we
624 * won't leave our routing in an invalid state.
625 */
626 cleanup_hosts();
627 free_interfaces();
628 exit(1);
629 }
630
631 static int usage(const char *progname)
632 {
633 fprintf(stderr, "Usage: %s <options>\n"
634 "\n"
635 "Options:\n"
636 " -d Enable debug messages\n"
637 " -i <ifname> Add an interface for relaying\n"
638 " -I <ifname> Same as -i, except with ARP cache and host route management\n"
639 " You need to specify at least two interfaces\n"
640 " -G <ip> Set a gateway IP for clients\n"
641 " -R <gateway>:<net>/<mask>\n"
642 " Add a static route for <net>/<mask> via <gateway>\n"
643 " -t <timeout> Host entry expiry timeout\n"
644 " -T <table> Set routing table number for automatically added routes\n"
645 " -B Enable broadcast forwarding\n"
646 " -D Enable DHCP forwarding\n"
647 " -L <ipaddr> Enable local access using <ipaddr> as source address\n"
648 "\n",
649 progname);
650 return -1;
651 }
652
653 int main(int argc, char **argv)
654 {
655 struct relayd_interface *rif = NULL;
656 struct in_addr addr, addr2;
657 bool local_addr_valid = false;
658 bool managed;
659 int ifnum = 0;
660 char *s, *s2;
661 int mask;
662 int ch;
663
664 debug = 0;
665 inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
666 if (inet_sock < 0) {
667 perror("socket(AF_INET)");
668 return 1;
669 }
670
671 host_timeout = 60;
672 forward_bcast = 0;
673 local_route_table = 0;
674 uloop_init();
675
676 while ((ch = getopt(argc, argv, "I:i:t:BDdT:G:R:L:")) != -1) {
677 switch(ch) {
678 case 'I':
679 managed = true;
680 /* fall through */
681 case 'i':
682 ifnum++;
683 rif = alloc_interface(optarg, managed);
684 if (!rif)
685 return 1;
686
687 managed = false;
688 break;
689 case 't':
690 host_timeout = atoi(optarg);
691 if (host_timeout <= 0)
692 return usage(argv[0]);
693 break;
694 case 'd':
695 debug++;
696 break;
697 case 'B':
698 forward_bcast = 1;
699 break;
700 case 'D':
701 forward_dhcp = 1;
702 break;
703 case 'T':
704 route_table = atoi(optarg);
705 if (route_table <= 0)
706 return usage(argv[0]);
707 break;
708 case 'G':
709 if (!inet_aton(optarg, &addr)) {
710 fprintf(stderr, "Address '%s' not found\n", optarg);
711 return 1;
712 }
713 relayd_add_pending_route((uint8_t *) &addr.s_addr, (const uint8_t *) "\x00\x00\x00\x00", 0, 0);
714 break;
715 case 'L':
716 if (!inet_aton(optarg, &addr)) {
717 fprintf(stderr, "Address '%s' not found\n", optarg);
718 return 1;
719 }
720 memcpy(&local_addr, &addr.s_addr, sizeof(local_addr));
721 local_addr_valid = true;
722 break;
723 case 'R':
724 s = strchr(optarg, ':');
725 if (!s)
726 return usage(argv[0]);
727
728 *(s++) = 0;
729 if (!inet_aton(optarg, &addr)) {
730 fprintf(stderr, "Address '%s' not found\n", optarg);
731 return 1;
732 }
733
734 s2 = strchr(s, '/');
735 if (!s2)
736 return usage(argv[0]);
737
738 *(s2++) = 0;
739 if (!inet_aton(s, &addr2)) {
740 fprintf(stderr, "Address '%s' not found\n", s);
741 return 1;
742 }
743
744 mask = atoi(s2);
745 if (mask < 0 || mask > 32)
746 return usage(argv[0]);
747
748 relayd_add_pending_route((uint8_t *) &addr.s_addr, (uint8_t *) &addr2.s_addr, mask, 0);
749 break;
750 case '?':
751 default:
752 return usage(argv[0]);
753 }
754 }
755
756 if (list_empty(&interfaces))
757 return usage(argv[0]);
758
759 if (ifnum < 2) {
760 fprintf(stderr, "ERROR: Need at least 2 interfaces for relaying\n");
761 return -1;
762 }
763
764 argc -= optind;
765 argv += optind;
766
767 signal(SIGTERM, die);
768 signal(SIGHUP, die);
769 signal(SIGUSR1, die);
770 signal(SIGUSR2, die);
771
772 if (local_addr_valid)
773 local_route_table = route_table++;
774
775 if (relayd_rtnl_init() < 0)
776 return 1;
777
778 if (init_interfaces() < 0)
779 return 1;
780
781 ping_static_routes();
782
783 uloop_run();
784 uloop_done();
785
786 cleanup_hosts();
787 free_interfaces();
788 relayd_rtnl_done();
789 close(inet_sock);
790
791 return 0;
792 }