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