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