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