1 /* $KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static const char _rcsid[] = "$KAME: route6d.c,v 1.104 2003/10/31 00:30:20 itojun Exp $";
36 #endif
37
38 #include <sys/param.h>
39 #include <sys/file.h>
40 #include <sys/ioctl.h>
41 #include <sys/queue.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44 #include <sys/uio.h>
45 #include <arpa/inet.h>
46 #include <net/if.h>
47 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip6.h>
51 #include <netinet/udp.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fnmatch.h>
55 #include <ifaddrs.h>
56 #include <netdb.h>
57 #ifdef HAVE_POLL_H
58 #include <poll.h>
59 #endif
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdarg.h>
63 #include <stddef.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <syslog.h>
67 #include <time.h>
68 #include <unistd.h>
69
70 #include "route6d.h"
71
72 #define MAXFILTER 40
73 #define RT_DUMP_MAXRETRY 15
74
75 #ifdef DEBUG
76 #define INIT_INTERVAL6 6
77 #else
78 #define INIT_INTERVAL6 10 /* Wait to submit an initial riprequest */
79 #endif
80
81 /* alignment constraint for routing socket */
82 #define ROUNDUP(a) \
83 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
84 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
85
86 struct ifc { /* Configuration of an interface */
87 TAILQ_ENTRY(ifc) ifc_next;
88
89 char ifc_name[IFNAMSIZ]; /* if name */
90 int ifc_index; /* if index */
91 int ifc_mtu; /* if mtu */
92 int ifc_metric; /* if metric */
93 u_int ifc_flags; /* flags */
94 short ifc_cflags; /* IFC_XXX */
95 struct in6_addr ifc_mylladdr; /* my link-local address */
96 struct sockaddr_in6 ifc_ripsin; /* rip multicast address */
97 TAILQ_HEAD(, ifac) ifc_ifac_head; /* list of AF_INET6 addrs */
98 TAILQ_HEAD(, iff) ifc_iff_head; /* list of filters */
99 int ifc_joined; /* joined to ff02::9 */
100 };
101 static TAILQ_HEAD(, ifc) ifc_head = TAILQ_HEAD_INITIALIZER(ifc_head);
102
103 struct ifac { /* Address associated to an interface */
104 TAILQ_ENTRY(ifac) ifac_next;
105
106 struct ifc *ifac_ifc; /* back pointer */
107 struct in6_addr ifac_addr; /* address */
108 struct in6_addr ifac_raddr; /* remote address, valid in p2p */
109 int ifac_scope_id; /* scope id */
110 int ifac_plen; /* prefix length */
111 };
112
113 struct iff { /* Filters for an interface */
114 TAILQ_ENTRY(iff) iff_next;
115
116 int iff_type;
117 struct in6_addr iff_addr;
118 int iff_plen;
119 };
120
121 static struct ifc **index2ifc;
122 static unsigned int nindex2ifc;
123 static struct ifc *loopifcp = NULL; /* pointing to loopback */
124 #ifdef HAVE_POLL_H
125 static struct pollfd set[2];
126 #else
127 static fd_set *sockvecp; /* vector to select() for receiving */
128 static fd_set *recvecp;
129 static int fdmasks;
130 static int maxfd; /* maximum fd for select() */
131 #endif
132 static int rtsock; /* the routing socket */
133 static int ripsock; /* socket to send/receive RIP datagram */
134
135 static struct rip6 *ripbuf; /* packet buffer for sending */
136
137 /*
138 * Maintain the routes in a linked list. When the number of the routes
139 * grows, somebody would like to introduce a hash based or a radix tree
140 * based structure. I believe the number of routes handled by RIP is
141 * limited and I don't have to manage a complex data structure, however.
142 *
143 * One of the major drawbacks of the linear linked list is the difficulty
144 * of representing the relationship between a couple of routes. This may
145 * be a significant problem when we have to support route aggregation with
146 * suppressing the specifics covered by the aggregate.
147 */
148
149 struct riprt {
150 TAILQ_ENTRY(riprt) rrt_next; /* next destination */
151
152 struct riprt *rrt_same; /* same destination - future use */
153 struct netinfo6 rrt_info; /* network info */
154 struct in6_addr rrt_gw; /* gateway */
155 u_long rrt_flags; /* kernel routing table flags */
156 u_long rrt_rflags; /* route6d routing table flags */
157 time_t rrt_t; /* when the route validated */
158 int rrt_index; /* ifindex from which this route got */
159 };
160 static TAILQ_HEAD(, riprt) riprt_head = TAILQ_HEAD_INITIALIZER(riprt_head);
161
162 static int dflag = 0; /* debug flag */
163 static int qflag = 0; /* quiet flag */
164 static int nflag = 0; /* don't update kernel routing table */
165 static int aflag = 0; /* age out even the statically defined routes */
166 static int hflag = 0; /* don't split horizon */
167 static int lflag = 0; /* exchange site local routes */
168 static int Pflag = 0; /* don't age out routes with RTF_PROTO[123] */
169 static int Qflag = RTF_PROTO2; /* set RTF_PROTO[123] flag to routes by RIPng */
170 static int sflag = 0; /* announce static routes w/ split horizon */
171 static int Sflag = 0; /* announce static routes to every interface */
172 static unsigned long routetag = 0; /* route tag attached on originating case */
173
174 static char *filter[MAXFILTER];
175 static int filtertype[MAXFILTER];
176 static int nfilter = 0;
177
178 static pid_t pid;
179
180 static struct sockaddr_storage ripsin;
181
182 static int interval = 1;
183 static time_t nextalarm = 0;
184 #if 0
185 static time_t sup_trig_update = 0;
186 #endif
187
188 static FILE *rtlog = NULL;
189
190 static int logopened = 0;
191
192 static int seq = 0;
193
194 static volatile sig_atomic_t seenalrm;
195 static volatile sig_atomic_t seenquit;
196 static volatile sig_atomic_t seenusr1;
197
198 #define RRTF_AGGREGATE 0x08000000
199 #define RRTF_NOADVERTISE 0x10000000
200 #define RRTF_NH_NOT_LLADDR 0x20000000
201 #define RRTF_SENDANYWAY 0x40000000
202 #define RRTF_CHANGED 0x80000000
203
204 static void sighandler(int);
205 static void ripalarm(void);
206 static void riprecv(void);
207 static void ripsend(struct ifc *, struct sockaddr_in6 *, int);
208 static int out_filter(struct riprt *, struct ifc *);
209 static void init(void);
210 static void ifconfig(void);
211 static int ifconfig1(const char *, const struct sockaddr *, struct ifc *, int);
212 static void rtrecv(void);
213 static int rt_del(const struct sockaddr_in6 *, const struct sockaddr_in6 *,
214 const struct sockaddr_in6 *);
215 static int rt_deladdr(struct ifc *, const struct sockaddr_in6 *,
216 const struct sockaddr_in6 *);
217 static void filterconfig(void);
218 static int getifmtu(int);
219 static const char *rttypes(struct rt_msghdr *);
220 static const char *rtflags(struct rt_msghdr *);
221 static const char *ifflags(int);
222 static int ifrt(struct ifc *, int);
223 static void ifrt_p2p(struct ifc *, int);
224 static void applyplen(struct in6_addr *, int);
225 static void ifrtdump(int);
226 static void ifdump(int);
227 static void ifdump0(FILE *, const struct ifc *);
228 static void ifremove(int);
229 static void rtdump(int);
230 static void rt_entry(struct rt_msghdr *, int);
231 static void rtdexit(void);
232 static void riprequest(struct ifc *, struct netinfo6 *, int,
233 struct sockaddr_in6 *);
234 static void ripflush(struct ifc *, struct sockaddr_in6 *, int, struct netinfo6 *np);
235 static void sendrequest(struct ifc *);
236 static int sin6mask2len(const struct sockaddr_in6 *);
237 static int mask2len(const struct in6_addr *, int);
238 static int sendpacket(struct sockaddr_in6 *, int);
239 static int addroute(struct riprt *, const struct in6_addr *, struct ifc *);
240 static int delroute(struct netinfo6 *, struct in6_addr *);
241 #if 0
242 static struct in6_addr *getroute(struct netinfo6 *, struct in6_addr *);
243 #endif
244 static void krtread(int);
245 static int tobeadv(struct riprt *, struct ifc *);
246 static char *allocopy(char *);
247 static char *hms(void);
248 static const char *inet6_n2p(const struct in6_addr *);
249 static struct ifac *ifa_match(const struct ifc *, const struct in6_addr *, int);
250 static struct in6_addr *plen2mask(int);
251 static struct riprt *rtsearch(struct netinfo6 *);
252 static int ripinterval(int);
253 #if 0
254 static time_t ripsuptrig(void);
255 #endif
256 static void fatal(const char *, ...)
257 __attribute__((__format__(__printf__, 1, 2)));
258 static void trace(int, const char *, ...)
259 __attribute__((__format__(__printf__, 2, 3)));
260 static void tracet(int, const char *, ...)
261 __attribute__((__format__(__printf__, 2, 3)));
262 static struct ifc *ifc_find(char *);
263 static struct iff *iff_find(struct ifc *, int);
264 static void setindex2ifc(int, struct ifc *);
265
266 #define MALLOC(type) ((type *)malloc(sizeof(type)))
267
268 #define IFIL_TYPE_ANY 0x0
269 #define IFIL_TYPE_A 'A'
270 #define IFIL_TYPE_N 'N'
271 #define IFIL_TYPE_T 'T'
272 #define IFIL_TYPE_O 'O'
273 #define IFIL_TYPE_L 'L'
274
275 int
main(int argc,char * argv[])276 main(int argc, char *argv[])
277 {
278 int ch;
279 int error = 0;
280 unsigned long proto;
281 struct ifc *ifcp;
282 sigset_t mask, omask;
283 const char *pidfile = ROUTE6D_PID;
284 FILE *pidfh;
285 char *progname;
286 char *ep;
287
288 progname = strrchr(*argv, '/');
289 if (progname)
290 progname++;
291 else
292 progname = *argv;
293
294 pid = getpid();
295 while ((ch = getopt(argc, argv, "A:N:O:R:T:L:t:adDhlnp:P:Q:qsS")) != -1) {
296 switch (ch) {
297 case 'A':
298 case 'N':
299 case 'O':
300 case 'T':
301 case 'L':
302 if (nfilter >= MAXFILTER) {
303 fatal("Exceeds MAXFILTER");
304 /*NOTREACHED*/
305 }
306 filtertype[nfilter] = ch;
307 filter[nfilter++] = allocopy(optarg);
308 break;
309 case 't':
310 ep = NULL;
311 routetag = strtoul(optarg, &ep, 0);
312 if (!ep || *ep != '\0' || (routetag & ~0xffff) != 0) {
313 fatal("invalid route tag");
314 /*NOTREACHED*/
315 }
316 break;
317 case 'p':
318 pidfile = optarg;
319 break;
320 case 'P':
321 ep = NULL;
322 proto = strtoul(optarg, &ep, 0);
323 if (!ep || *ep != '\0' || 3 < proto) {
324 fatal("invalid P flag");
325 /*NOTREACHED*/
326 }
327 if (proto == 0)
328 Pflag = 0;
329 if (proto == 1)
330 Pflag |= RTF_PROTO1;
331 if (proto == 2)
332 Pflag |= RTF_PROTO2;
333 if (proto == 3)
334 Pflag |= RTF_PROTO3;
335 break;
336 case 'Q':
337 ep = NULL;
338 proto = strtoul(optarg, &ep, 0);
339 if (!ep || *ep != '\0' || 3 < proto) {
340 fatal("invalid Q flag");
341 /*NOTREACHED*/
342 }
343 if (proto == 0)
344 Qflag = 0;
345 if (proto == 1)
346 Qflag |= RTF_PROTO1;
347 if (proto == 2)
348 Qflag |= RTF_PROTO2;
349 if (proto == 3)
350 Qflag |= RTF_PROTO3;
351 break;
352 case 'R':
353 if ((rtlog = fopen(optarg, "w")) == NULL) {
354 fatal("Can not write to routelog");
355 /*NOTREACHED*/
356 }
357 break;
358 #define FLAG(c, flag, n) case c: do { flag = n; break; } while(0)
359 FLAG('a', aflag, 1); break;
360 FLAG('d', dflag, 1); break;
361 FLAG('D', dflag, 2); break;
362 FLAG('h', hflag, 1); break;
363 FLAG('l', lflag, 1); break;
364 FLAG('n', nflag, 1); break;
365 FLAG('q', qflag, 1); break;
366 FLAG('s', sflag, 1); break;
367 FLAG('S', Sflag, 1); break;
368 #undef FLAG
369 default:
370 fatal("Invalid option specified, terminating");
371 /*NOTREACHED*/
372 }
373 }
374 argc -= optind;
375 argv += optind;
376 if (argc > 0) {
377 fatal("bogus extra arguments");
378 /*NOTREACHED*/
379 }
380
381 if (geteuid()) {
382 nflag = 1;
383 fprintf(stderr, "No kernel update is allowed\n");
384 }
385
386 if (dflag == 0) {
387 if (daemon(0, 0) < 0) {
388 fatal("daemon");
389 /*NOTREACHED*/
390 }
391 }
392
393 openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
394 logopened++;
395
396 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL)
397 fatal("malloc");
398 memset(ripbuf, 0, RIP6_MAXMTU);
399 ripbuf->rip6_cmd = RIP6_RESPONSE;
400 ripbuf->rip6_vers = RIP6_VERSION;
401 ripbuf->rip6_res1[0] = 0;
402 ripbuf->rip6_res1[1] = 0;
403
404 init();
405 ifconfig();
406 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
407 if (ifcp->ifc_index < 0) {
408 fprintf(stderr, "No ifindex found at %s "
409 "(no link-local address?)\n", ifcp->ifc_name);
410 error++;
411 }
412 }
413 if (error)
414 exit(1);
415 if (loopifcp == NULL) {
416 fatal("No loopback found");
417 /*NOTREACHED*/
418 }
419 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
420 ifrt(ifcp, 0);
421 }
422 filterconfig();
423 krtread(0);
424 if (dflag)
425 ifrtdump(0);
426
427 pid = getpid();
428 if ((pidfh = fopen(pidfile, "w")) != NULL) {
429 fprintf(pidfh, "%d\n", pid);
430 fclose(pidfh);
431 }
432
433 if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
434 fatal("malloc");
435 /*NOTREACHED*/
436 }
437 memset(ripbuf, 0, RIP6_MAXMTU);
438 ripbuf->rip6_cmd = RIP6_RESPONSE;
439 ripbuf->rip6_vers = RIP6_VERSION;
440 ripbuf->rip6_res1[0] = 0;
441 ripbuf->rip6_res1[1] = 0;
442
443 if (signal(SIGALRM, sighandler) == SIG_ERR ||
444 signal(SIGQUIT, sighandler) == SIG_ERR ||
445 signal(SIGTERM, sighandler) == SIG_ERR ||
446 signal(SIGUSR1, sighandler) == SIG_ERR ||
447 signal(SIGHUP, sighandler) == SIG_ERR ||
448 signal(SIGINT, sighandler) == SIG_ERR) {
449 fatal("signal");
450 /*NOTREACHED*/
451 }
452 /*
453 * To avoid rip packet congestion (not on a cable but in this
454 * process), wait for a moment to send the first RIP6_RESPONSE
455 * packets.
456 */
457 alarm(ripinterval(INIT_INTERVAL6));
458
459 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
460 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
461 continue;
462 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
463 sendrequest(ifcp);
464 }
465
466 syslog(LOG_INFO, "**** Started ****");
467 sigemptyset(&mask);
468 sigaddset(&mask, SIGALRM);
469 while (1) {
470 if (seenalrm) {
471 ripalarm();
472 seenalrm = 0;
473 continue;
474 }
475 if (seenquit) {
476 rtdexit();
477 seenquit = 0;
478 continue;
479 }
480 if (seenusr1) {
481 ifrtdump(SIGUSR1);
482 seenusr1 = 0;
483 continue;
484 }
485
486 #ifdef HAVE_POLL_H
487 switch (poll(set, 2, INFTIM))
488 #else
489 memcpy(recvecp, sockvecp, fdmasks);
490 switch (select(maxfd + 1, recvecp, 0, 0, 0))
491 #endif
492 {
493 case -1:
494 if (errno != EINTR) {
495 fatal("select");
496 /*NOTREACHED*/
497 }
498 continue;
499 case 0:
500 continue;
501 default:
502 #ifdef HAVE_POLL_H
503 if (set[0].revents & POLLIN)
504 #else
505 if (FD_ISSET(ripsock, recvecp))
506 #endif
507 {
508 sigprocmask(SIG_BLOCK, &mask, &omask);
509 riprecv();
510 sigprocmask(SIG_SETMASK, &omask, NULL);
511 }
512 #ifdef HAVE_POLL_H
513 if (set[1].revents & POLLIN)
514 #else
515 if (FD_ISSET(rtsock, recvecp))
516 #endif
517 {
518 sigprocmask(SIG_BLOCK, &mask, &omask);
519 rtrecv();
520 sigprocmask(SIG_SETMASK, &omask, NULL);
521 }
522 }
523 }
524 }
525
526 static void
sighandler(int signo)527 sighandler(int signo)
528 {
529
530 switch (signo) {
531 case SIGALRM:
532 seenalrm++;
533 break;
534 case SIGQUIT:
535 case SIGTERM:
536 seenquit++;
537 break;
538 case SIGUSR1:
539 case SIGHUP:
540 case SIGINT:
541 seenusr1++;
542 break;
543 }
544 }
545
546 /*
547 * gracefully exits after resetting sockopts.
548 */
549 /* ARGSUSED */
550 static void
rtdexit(void)551 rtdexit(void)
552 {
553 struct riprt *rrt;
554
555 alarm(0);
556 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
557 if (rrt->rrt_rflags & RRTF_AGGREGATE) {
558 delroute(&rrt->rrt_info, &rrt->rrt_gw);
559 }
560 }
561 close(ripsock);
562 close(rtsock);
563 syslog(LOG_INFO, "**** Terminated ****");
564 closelog();
565 exit(1);
566 }
567
568 /*
569 * Called periodically:
570 * 1. age out the learned route. remove it if necessary.
571 * 2. submit RIP6_RESPONSE packets.
572 * Invoked in every SUPPLY_INTERVAL6 (30) seconds. I believe we don't have
573 * to invoke this function in every 1 or 5 or 10 seconds only to age the
574 * routes more precisely.
575 */
576 /* ARGSUSED */
577 static void
ripalarm(void)578 ripalarm(void)
579 {
580 struct ifc *ifcp;
581 struct riprt *rrt, *rrt_tmp;
582 time_t t_lifetime, t_holddown;
583
584 /* age the RIP routes */
585 t_lifetime = time(NULL) - RIP_LIFETIME;
586 t_holddown = t_lifetime - RIP_HOLDDOWN;
587 TAILQ_FOREACH_SAFE(rrt, &riprt_head, rrt_next, rrt_tmp) {
588 if (rrt->rrt_t == 0)
589 continue;
590 else if (rrt->rrt_t < t_holddown) {
591 TAILQ_REMOVE(&riprt_head, rrt, rrt_next);
592 delroute(&rrt->rrt_info, &rrt->rrt_gw);
593 free(rrt);
594 } else if (rrt->rrt_t < t_lifetime)
595 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
596 }
597 /* Supply updates */
598 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
599 if (ifcp->ifc_index > 0 && (ifcp->ifc_flags & IFF_UP))
600 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
601 }
602 alarm(ripinterval(SUPPLY_INTERVAL6));
603 }
604
605 static void
init(void)606 init(void)
607 {
608 int error;
609 const int int0 = 0, int1 = 1, int255 = 255;
610 struct addrinfo hints, *res;
611 char port[NI_MAXSERV];
612
613 TAILQ_INIT(&ifc_head);
614 nindex2ifc = 0; /*initial guess*/
615 index2ifc = NULL;
616 snprintf(port, sizeof(port), "%u", RIP6_PORT);
617
618 memset(&hints, 0, sizeof(hints));
619 hints.ai_family = PF_INET6;
620 hints.ai_socktype = SOCK_DGRAM;
621 hints.ai_protocol = IPPROTO_UDP;
622 hints.ai_flags = AI_PASSIVE;
623 error = getaddrinfo(NULL, port, &hints, &res);
624 if (error) {
625 fatal("%s", gai_strerror(error));
626 /*NOTREACHED*/
627 }
628 if (res->ai_next) {
629 fatal(":: resolved to multiple address");
630 /*NOTREACHED*/
631 }
632
633 ripsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
634 if (ripsock < 0) {
635 fatal("rip socket");
636 /*NOTREACHED*/
637 }
638 #ifdef IPV6_V6ONLY
639 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_V6ONLY,
640 &int1, sizeof(int1)) < 0) {
641 fatal("rip IPV6_V6ONLY");
642 /*NOTREACHED*/
643 }
644 #endif
645 if (bind(ripsock, res->ai_addr, res->ai_addrlen) < 0) {
646 fatal("rip bind");
647 /*NOTREACHED*/
648 }
649 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
650 &int255, sizeof(int255)) < 0) {
651 fatal("rip IPV6_MULTICAST_HOPS");
652 /*NOTREACHED*/
653 }
654 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
655 &int0, sizeof(int0)) < 0) {
656 fatal("rip IPV6_MULTICAST_LOOP");
657 /*NOTREACHED*/
658 }
659
660 #ifdef IPV6_RECVPKTINFO
661 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
662 &int1, sizeof(int1)) < 0) {
663 fatal("rip IPV6_RECVPKTINFO");
664 /*NOTREACHED*/
665 }
666 #else /* old adv. API */
667 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_PKTINFO,
668 &int1, sizeof(int1)) < 0) {
669 fatal("rip IPV6_PKTINFO");
670 /*NOTREACHED*/
671 }
672 #endif
673
674 #ifdef IPV6_RECVPKTINFO
675 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
676 &int1, sizeof(int1)) < 0) {
677 fatal("rip IPV6_RECVHOPLIMIT");
678 /*NOTREACHED*/
679 }
680 #else /* old adv. API */
681 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_HOPLIMIT,
682 &int1, sizeof(int1)) < 0) {
683 fatal("rip IPV6_HOPLIMIT");
684 /*NOTREACHED*/
685 }
686 #endif
687 freeaddrinfo(res);
688
689 memset(&hints, 0, sizeof(hints));
690 hints.ai_family = PF_INET6;
691 hints.ai_socktype = SOCK_DGRAM;
692 hints.ai_protocol = IPPROTO_UDP;
693 error = getaddrinfo(RIP6_DEST, port, &hints, &res);
694 if (error) {
695 fatal("%s", gai_strerror(error));
696 /*NOTREACHED*/
697 }
698 if (res->ai_next) {
699 fatal("%s resolved to multiple address", RIP6_DEST);
700 /*NOTREACHED*/
701 }
702 memcpy(&ripsin, res->ai_addr, res->ai_addrlen);
703 freeaddrinfo(res);
704
705 #ifdef HAVE_POLL_H
706 set[0].fd = ripsock;
707 set[0].events = POLLIN;
708 #else
709 maxfd = ripsock;
710 #endif
711
712 if (nflag == 0) {
713 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
714 fatal("route socket");
715 /*NOTREACHED*/
716 }
717 #ifdef HAVE_POLL_H
718 set[1].fd = rtsock;
719 set[1].events = POLLIN;
720 #else
721 if (rtsock > maxfd)
722 maxfd = rtsock;
723 #endif
724 } else {
725 #ifdef HAVE_POLL_H
726 set[1].fd = -1;
727 #else
728 rtsock = -1; /*just for safety */
729 #endif
730 }
731
732 #ifndef HAVE_POLL_H
733 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
734 if ((sockvecp = malloc(fdmasks)) == NULL) {
735 fatal("malloc");
736 /*NOTREACHED*/
737 }
738 if ((recvecp = malloc(fdmasks)) == NULL) {
739 fatal("malloc");
740 /*NOTREACHED*/
741 }
742 memset(sockvecp, 0, fdmasks);
743 FD_SET(ripsock, sockvecp);
744 if (rtsock >= 0)
745 FD_SET(rtsock, sockvecp);
746 #endif
747 }
748
749 #define RIPSIZE(n) \
750 (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))
751
752 /*
753 * ripflush flushes the rip datagram stored in the rip buffer
754 */
755 static void
ripflush(struct ifc * ifcp,struct sockaddr_in6 * sin6,int nrt,struct netinfo6 * np)756 ripflush(struct ifc *ifcp, struct sockaddr_in6 *sin6, int nrt, struct netinfo6 *np)
757 {
758 int i;
759 int error;
760
761 if (ifcp)
762 tracet(1, "Send(%s): info(%d) to %s.%d\n",
763 ifcp->ifc_name, nrt,
764 inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
765 else
766 tracet(1, "Send: info(%d) to %s.%d\n",
767 nrt, inet6_n2p(&sin6->sin6_addr), ntohs(sin6->sin6_port));
768 if (dflag >= 2) {
769 np = ripbuf->rip6_nets;
770 for (i = 0; i < nrt; i++, np++) {
771 if (np->rip6_metric == NEXTHOP_METRIC) {
772 if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest))
773 trace(2, " NextHop reset");
774 else {
775 trace(2, " NextHop %s",
776 inet6_n2p(&np->rip6_dest));
777 }
778 } else {
779 trace(2, " %s/%d[%d]",
780 inet6_n2p(&np->rip6_dest),
781 np->rip6_plen, np->rip6_metric);
782 }
783 if (np->rip6_tag) {
784 trace(2, " tag=0x%04x",
785 ntohs(np->rip6_tag) & 0xffff);
786 }
787 trace(2, "\n");
788 }
789 }
790 error = sendpacket(sin6, RIPSIZE(nrt));
791 if (error == EAFNOSUPPORT) {
792 /* Protocol not supported */
793 if (ifcp != NULL) {
794 tracet(1, "Could not send info to %s (%s): "
795 "set IFF_UP to 0\n",
796 ifcp->ifc_name,
797 inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
798 /* As if down for AF_INET6 */
799 ifcp->ifc_flags &= ~IFF_UP;
800 } else {
801 tracet(1, "Could not send info to %s\n",
802 inet6_n2p(&sin6->sin6_addr));
803 }
804 }
805 }
806
807 /*
808 * Generate RIP6_RESPONSE packets and send them.
809 */
810 static void
ripsend(struct ifc * ifcp,struct sockaddr_in6 * sin6,int flag)811 ripsend(struct ifc *ifcp, struct sockaddr_in6 *sin6, int flag)
812 {
813 struct riprt *rrt;
814 struct in6_addr *nh; /* next hop */
815 struct netinfo6 *np;
816 int maxrte;
817 int nrt;
818
819 if (qflag)
820 return;
821
822 if (ifcp == NULL) {
823 /*
824 * Request from non-link local address is not
825 * a regular route6d update.
826 */
827 maxrte = (IFMINMTU - sizeof(struct ip6_hdr) -
828 sizeof(struct udphdr) -
829 sizeof(struct rip6) + sizeof(struct netinfo6)) /
830 sizeof(struct netinfo6);
831 nh = NULL;
832 nrt = 0;
833 np = ripbuf->rip6_nets;
834 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
835 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
836 continue;
837 /* Put the route to the buffer */
838 *np = rrt->rrt_info;
839 np++; nrt++;
840 if (nrt == maxrte) {
841 ripflush(NULL, sin6, nrt, np);
842 nh = NULL;
843 nrt = 0;
844 np = ripbuf->rip6_nets;
845 }
846 }
847 if (nrt) /* Send last packet */
848 ripflush(NULL, sin6, nrt, np);
849 return;
850 }
851
852 if ((flag & RRTF_SENDANYWAY) == 0 &&
853 (qflag || (ifcp->ifc_flags & IFF_LOOPBACK)))
854 return;
855
856 /* -N: no use */
857 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
858 return;
859
860 /* -T: generate default route only */
861 if (iff_find(ifcp, IFIL_TYPE_T) != NULL) {
862 struct netinfo6 rrt_info;
863 memset(&rrt_info, 0, sizeof(struct netinfo6));
864 rrt_info.rip6_dest = in6addr_any;
865 rrt_info.rip6_plen = 0;
866 rrt_info.rip6_metric = 1;
867 rrt_info.rip6_metric += ifcp->ifc_metric;
868 rrt_info.rip6_tag = htons(routetag & 0xffff);
869 np = ripbuf->rip6_nets;
870 *np = rrt_info;
871 nrt = 1;
872 ripflush(ifcp, sin6, nrt, np);
873 return;
874 }
875
876 maxrte = (ifcp->ifc_mtu - sizeof(struct ip6_hdr) -
877 sizeof(struct udphdr) -
878 sizeof(struct rip6) + sizeof(struct netinfo6)) /
879 sizeof(struct netinfo6);
880
881 nrt = 0; np = ripbuf->rip6_nets; nh = NULL;
882 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
883 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
884 continue;
885
886 /* Need to check filter here */
887 if (out_filter(rrt, ifcp) == 0)
888 continue;
889
890 /* Check split horizon and other conditions */
891 if (tobeadv(rrt, ifcp) == 0)
892 continue;
893
894 /* Only considers the routes with flag if specified */
895 if ((flag & RRTF_CHANGED) &&
896 (rrt->rrt_rflags & RRTF_CHANGED) == 0)
897 continue;
898
899 /* Check nexthop */
900 if (rrt->rrt_index == ifcp->ifc_index &&
901 !IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_gw) &&
902 (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR) == 0) {
903 if (nh == NULL || !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw)) {
904 if (nrt == maxrte - 2) {
905 ripflush(ifcp, sin6, nrt, np);
906 nh = NULL;
907 nrt = 0;
908 np = ripbuf->rip6_nets;
909 }
910
911 np->rip6_dest = rrt->rrt_gw;
912 np->rip6_plen = 0;
913 np->rip6_tag = 0;
914 np->rip6_metric = NEXTHOP_METRIC;
915 nh = &rrt->rrt_gw;
916 np++; nrt++;
917 }
918 } else if (nh && (rrt->rrt_index != ifcp->ifc_index ||
919 !IN6_ARE_ADDR_EQUAL(nh, &rrt->rrt_gw) ||
920 rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)) {
921 /* Reset nexthop */
922 if (nrt == maxrte - 2) {
923 ripflush(ifcp, sin6, nrt, np);
924 nh = NULL;
925 nrt = 0;
926 np = ripbuf->rip6_nets;
927 }
928 memset(np, 0, sizeof(struct netinfo6));
929 np->rip6_metric = NEXTHOP_METRIC;
930 nh = NULL;
931 np++; nrt++;
932 }
933
934 /* Put the route to the buffer */
935 *np = rrt->rrt_info;
936 np++; nrt++;
937 if (nrt == maxrte) {
938 ripflush(ifcp, sin6, nrt, np);
939 nh = NULL;
940 nrt = 0;
941 np = ripbuf->rip6_nets;
942 }
943 }
944 if (nrt) /* Send last packet */
945 ripflush(ifcp, sin6, nrt, np);
946 }
947
948 /*
949 * outbound filter logic, per-route/interface.
950 */
951 static int
out_filter(struct riprt * rrt,struct ifc * ifcp)952 out_filter(struct riprt *rrt, struct ifc *ifcp)
953 {
954 struct iff *iffp;
955 struct in6_addr ia;
956 int ok;
957
958 /*
959 * -A: filter out less specific routes, if we have aggregated
960 * route configured.
961 */
962 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
963 if (iffp->iff_type != 'A')
964 continue;
965 if (rrt->rrt_info.rip6_plen <= iffp->iff_plen)
966 continue;
967 ia = rrt->rrt_info.rip6_dest;
968 applyplen(&ia, iffp->iff_plen);
969 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr))
970 return 0;
971 }
972
973 /*
974 * if it is an aggregated route, advertise it only to the
975 * interfaces specified on -A.
976 */
977 if ((rrt->rrt_rflags & RRTF_AGGREGATE) != 0) {
978 ok = 0;
979 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
980 if (iffp->iff_type != 'A')
981 continue;
982 if (rrt->rrt_info.rip6_plen == iffp->iff_plen &&
983 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
984 &iffp->iff_addr)) {
985 ok = 1;
986 break;
987 }
988 }
989 if (!ok)
990 return 0;
991 }
992
993 /*
994 * -O: advertise only if prefix matches the configured prefix.
995 */
996 if (iff_find(ifcp, IFIL_TYPE_O) != NULL) {
997 ok = 0;
998 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
999 if (iffp->iff_type != 'O')
1000 continue;
1001 if (rrt->rrt_info.rip6_plen < iffp->iff_plen)
1002 continue;
1003 ia = rrt->rrt_info.rip6_dest;
1004 applyplen(&ia, iffp->iff_plen);
1005 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1006 ok = 1;
1007 break;
1008 }
1009 }
1010 if (!ok)
1011 return 0;
1012 }
1013
1014 /* the prefix should be advertised */
1015 return 1;
1016 }
1017
1018 /*
1019 * Determine if the route is to be advertised on the specified interface.
1020 * It checks options specified in the arguments and the split horizon rule.
1021 */
1022 static int
tobeadv(struct riprt * rrt,struct ifc * ifcp)1023 tobeadv(struct riprt *rrt, struct ifc *ifcp)
1024 {
1025
1026 /* Special care for static routes */
1027 if (rrt->rrt_flags & RTF_STATIC) {
1028 /* XXX don't advertise reject/blackhole routes */
1029 if (rrt->rrt_flags & (RTF_REJECT | RTF_BLACKHOLE))
1030 return 0;
1031
1032 if (Sflag) /* Yes, advertise it anyway */
1033 return 1;
1034 if (sflag && rrt->rrt_index != ifcp->ifc_index)
1035 return 1;
1036 return 0;
1037 }
1038 /* Regular split horizon */
1039 if (hflag == 0 && rrt->rrt_index == ifcp->ifc_index)
1040 return 0;
1041 return 1;
1042 }
1043
1044 /*
1045 * Send a rip packet actually.
1046 */
1047 static int
sendpacket(struct sockaddr_in6 * sin6,int len)1048 sendpacket(struct sockaddr_in6 *sin6, int len)
1049 {
1050 struct msghdr m;
1051 struct cmsghdr *cm;
1052 struct iovec iov[2];
1053 struct in6_pktinfo *pi;
1054 u_char cmsgbuf[256];
1055 int idx;
1056 struct sockaddr_in6 sincopy;
1057
1058 /* do not overwrite the given sin */
1059 sincopy = *sin6;
1060 sin6 = &sincopy;
1061
1062 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1063 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1064 idx = sin6->sin6_scope_id;
1065 else
1066 idx = 0;
1067
1068 m.msg_name = (caddr_t)sin6;
1069 m.msg_namelen = sizeof(*sin6);
1070 iov[0].iov_base = (caddr_t)ripbuf;
1071 iov[0].iov_len = len;
1072 m.msg_iov = iov;
1073 m.msg_iovlen = 1;
1074 m.msg_flags = 0;
1075 if (!idx) {
1076 m.msg_control = NULL;
1077 m.msg_controllen = 0;
1078 } else {
1079 memset(cmsgbuf, 0, sizeof(cmsgbuf));
1080 cm = (struct cmsghdr *)(void *)cmsgbuf;
1081 m.msg_control = (caddr_t)cm;
1082 m.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
1083
1084 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1085 cm->cmsg_level = IPPROTO_IPV6;
1086 cm->cmsg_type = IPV6_PKTINFO;
1087 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
1088 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*::*/
1089 pi->ipi6_ifindex = idx;
1090 }
1091
1092 if (sendmsg(ripsock, &m, 0 /*MSG_DONTROUTE*/) < 0) {
1093 trace(1, "sendmsg: %s\n", strerror(errno));
1094 return errno;
1095 }
1096
1097 return 0;
1098 }
1099
1100 /*
1101 * Receive and process RIP packets. Update the routes/kernel forwarding
1102 * table if necessary.
1103 */
1104 static void
riprecv(void)1105 riprecv(void)
1106 {
1107 struct ifc *ifcp, *ic;
1108 struct sockaddr_in6 fsock;
1109 struct in6_addr nh; /* next hop */
1110 struct rip6 *rp;
1111 struct netinfo6 *np, *nq;
1112 struct riprt *rrt;
1113 ssize_t len, nn;
1114 unsigned int need_trigger, idx;
1115 char buf[4 * RIP6_MAXMTU];
1116 time_t t;
1117 struct msghdr m;
1118 struct cmsghdr *cm;
1119 struct iovec iov[2];
1120 u_char cmsgbuf[256];
1121 struct in6_pktinfo *pi = NULL;
1122 int *hlimp = NULL;
1123 struct iff *iffp;
1124 struct in6_addr ia;
1125 int ok;
1126 time_t t_half_lifetime;
1127
1128 need_trigger = 0;
1129
1130 m.msg_name = (caddr_t)&fsock;
1131 m.msg_namelen = sizeof(fsock);
1132 iov[0].iov_base = (caddr_t)buf;
1133 iov[0].iov_len = sizeof(buf);
1134 m.msg_iov = iov;
1135 m.msg_iovlen = 1;
1136 cm = (struct cmsghdr *)(void *)cmsgbuf;
1137 m.msg_control = (caddr_t)cm;
1138 m.msg_controllen = sizeof(cmsgbuf);
1139 m.msg_flags = 0;
1140 if ((len = recvmsg(ripsock, &m, 0)) < 0) {
1141 fatal("recvmsg");
1142 /*NOTREACHED*/
1143 }
1144 idx = 0;
1145 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&m);
1146 cm;
1147 cm = (struct cmsghdr *)CMSG_NXTHDR(&m, cm)) {
1148 if (cm->cmsg_level != IPPROTO_IPV6)
1149 continue;
1150 switch (cm->cmsg_type) {
1151 case IPV6_PKTINFO:
1152 if (cm->cmsg_len != CMSG_LEN(sizeof(*pi))) {
1153 trace(1,
1154 "invalid cmsg length for IPV6_PKTINFO\n");
1155 return;
1156 }
1157 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
1158 idx = pi->ipi6_ifindex;
1159 break;
1160 case IPV6_HOPLIMIT:
1161 if (cm->cmsg_len != CMSG_LEN(sizeof(int))) {
1162 trace(1,
1163 "invalid cmsg length for IPV6_HOPLIMIT\n");
1164 return;
1165 }
1166 hlimp = (int *)(void *)CMSG_DATA(cm);
1167 break;
1168 }
1169 }
1170
1171 if ((size_t)len < sizeof(struct rip6)) {
1172 trace(1, "Packet too short\n");
1173 return;
1174 }
1175
1176 if (pi == NULL || hlimp == NULL) {
1177 /*
1178 * This can happen when the kernel failed to allocate memory
1179 * for the ancillary data. Although we might be able to handle
1180 * some cases without this info, those are minor and not so
1181 * important, so it's better to discard the packet for safer
1182 * operation.
1183 */
1184 trace(1, "IPv6 packet information cannot be retrieved\n");
1185 return;
1186 }
1187
1188 nh = fsock.sin6_addr;
1189 nn = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1190 sizeof(struct netinfo6);
1191 rp = (struct rip6 *)(void *)buf;
1192 np = rp->rip6_nets;
1193
1194 if (rp->rip6_vers != RIP6_VERSION) {
1195 trace(1, "Incorrect RIP version %d\n", rp->rip6_vers);
1196 return;
1197 }
1198 if (rp->rip6_cmd == RIP6_REQUEST) {
1199 if (idx && idx < nindex2ifc) {
1200 ifcp = index2ifc[idx];
1201 riprequest(ifcp, np, nn, &fsock);
1202 } else {
1203 riprequest(NULL, np, nn, &fsock);
1204 }
1205 return;
1206 }
1207
1208 if (!IN6_IS_ADDR_LINKLOCAL(&fsock.sin6_addr)) {
1209 trace(1, "Response from non-ll addr: %s\n",
1210 inet6_n2p(&fsock.sin6_addr));
1211 return; /* Ignore packets from non-link-local addr */
1212 }
1213 if (ntohs(fsock.sin6_port) != RIP6_PORT) {
1214 trace(1, "Response from non-rip port from %s\n",
1215 inet6_n2p(&fsock.sin6_addr));
1216 return;
1217 }
1218 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && *hlimp != 255) {
1219 trace(1,
1220 "Response packet with a smaller hop limit (%d) from %s\n",
1221 *hlimp, inet6_n2p(&fsock.sin6_addr));
1222 return;
1223 }
1224 /*
1225 * Further validation: since this program does not send off-link
1226 * requests, an incoming response must always come from an on-link
1227 * node. Although this is normally ensured by the source address
1228 * check above, it may not 100% be safe because there are router
1229 * implementations that (invalidly) allow a packet with a link-local
1230 * source address to be forwarded to a different link.
1231 * So we also check whether the destination address is a link-local
1232 * address or the hop limit is 255. Note that RFC2080 does not require
1233 * the specific hop limit for a unicast response, so we cannot assume
1234 * the limitation.
1235 */
1236 if (!IN6_IS_ADDR_LINKLOCAL(&pi->ipi6_addr) && *hlimp != 255) {
1237 trace(1,
1238 "Response packet possibly from an off-link node: "
1239 "from %s to %s hlim=%d\n",
1240 inet6_n2p(&fsock.sin6_addr),
1241 inet6_n2p(&pi->ipi6_addr), *hlimp);
1242 return;
1243 }
1244
1245 idx = fsock.sin6_scope_id;
1246 ifcp = (idx < nindex2ifc) ? index2ifc[idx] : NULL;
1247 if (!ifcp) {
1248 trace(1, "Packets to unknown interface index %d\n", idx);
1249 return; /* Ignore it */
1250 }
1251 if (IN6_ARE_ADDR_EQUAL(&ifcp->ifc_mylladdr, &fsock.sin6_addr))
1252 return; /* The packet is from me; ignore */
1253 if (rp->rip6_cmd != RIP6_RESPONSE) {
1254 trace(1, "Invalid command %d\n", rp->rip6_cmd);
1255 return;
1256 }
1257
1258 /* -N: no use */
1259 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
1260 return;
1261
1262 tracet(1, "Recv(%s): from %s.%d info(%zd)\n",
1263 ifcp->ifc_name, inet6_n2p(&nh), ntohs(fsock.sin6_port), nn);
1264
1265 t = time(NULL);
1266 t_half_lifetime = t - (RIP_LIFETIME/2);
1267 for (; nn; nn--, np++) {
1268 if (np->rip6_metric == NEXTHOP_METRIC) {
1269 /* modify neighbor address */
1270 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1271 nh = np->rip6_dest;
1272 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1273 } else if (IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest)) {
1274 nh = fsock.sin6_addr;
1275 trace(1, "\tNexthop: %s\n", inet6_n2p(&nh));
1276 } else {
1277 nh = fsock.sin6_addr;
1278 trace(1, "\tInvalid Nexthop: %s\n",
1279 inet6_n2p(&np->rip6_dest));
1280 }
1281 continue;
1282 }
1283 if (IN6_IS_ADDR_MULTICAST(&np->rip6_dest)) {
1284 trace(1, "\tMulticast netinfo6: %s/%d [%d]\n",
1285 inet6_n2p(&np->rip6_dest),
1286 np->rip6_plen, np->rip6_metric);
1287 continue;
1288 }
1289 if (IN6_IS_ADDR_LOOPBACK(&np->rip6_dest)) {
1290 trace(1, "\tLoopback netinfo6: %s/%d [%d]\n",
1291 inet6_n2p(&np->rip6_dest),
1292 np->rip6_plen, np->rip6_metric);
1293 continue;
1294 }
1295 if (IN6_IS_ADDR_LINKLOCAL(&np->rip6_dest)) {
1296 trace(1, "\tLink Local netinfo6: %s/%d [%d]\n",
1297 inet6_n2p(&np->rip6_dest),
1298 np->rip6_plen, np->rip6_metric);
1299 continue;
1300 }
1301 /* may need to pass sitelocal prefix in some case, however*/
1302 if (IN6_IS_ADDR_SITELOCAL(&np->rip6_dest) && !lflag) {
1303 trace(1, "\tSite Local netinfo6: %s/%d [%d]\n",
1304 inet6_n2p(&np->rip6_dest),
1305 np->rip6_plen, np->rip6_metric);
1306 continue;
1307 }
1308 trace(2, "\tnetinfo6: %s/%d [%d]",
1309 inet6_n2p(&np->rip6_dest),
1310 np->rip6_plen, np->rip6_metric);
1311 if (np->rip6_tag)
1312 trace(2, " tag=0x%04x", ntohs(np->rip6_tag) & 0xffff);
1313 if (dflag >= 2) {
1314 ia = np->rip6_dest;
1315 applyplen(&ia, np->rip6_plen);
1316 if (!IN6_ARE_ADDR_EQUAL(&ia, &np->rip6_dest))
1317 trace(2, " [junk outside prefix]");
1318 }
1319
1320 /*
1321 * -L: listen only if the prefix matches the configuration
1322 */
1323 ok = 1; /* if there's no L filter, it is ok */
1324 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
1325 if (iffp->iff_type != IFIL_TYPE_L)
1326 continue;
1327 ok = 0;
1328 if (np->rip6_plen < iffp->iff_plen)
1329 continue;
1330 /* special rule: ::/0 means default, not "in /0" */
1331 if (iffp->iff_plen == 0 && np->rip6_plen > 0)
1332 continue;
1333 ia = np->rip6_dest;
1334 applyplen(&ia, iffp->iff_plen);
1335 if (IN6_ARE_ADDR_EQUAL(&ia, &iffp->iff_addr)) {
1336 ok = 1;
1337 break;
1338 }
1339 }
1340 if (!ok) {
1341 trace(2, " (filtered)\n");
1342 continue;
1343 }
1344
1345 trace(2, "\n");
1346 np->rip6_metric++;
1347 np->rip6_metric += ifcp->ifc_metric;
1348 if (np->rip6_metric > HOPCNT_INFINITY6)
1349 np->rip6_metric = HOPCNT_INFINITY6;
1350
1351 applyplen(&np->rip6_dest, np->rip6_plen);
1352 if ((rrt = rtsearch(np)) != NULL) {
1353 if (rrt->rrt_t == 0)
1354 continue; /* Intf route has priority */
1355 nq = &rrt->rrt_info;
1356 if (nq->rip6_metric > np->rip6_metric) {
1357 if (rrt->rrt_index == ifcp->ifc_index &&
1358 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1359 /* Small metric from the same gateway */
1360 nq->rip6_metric = np->rip6_metric;
1361 } else {
1362 /* Better route found */
1363 rrt->rrt_index = ifcp->ifc_index;
1364 /* Update routing table */
1365 delroute(nq, &rrt->rrt_gw);
1366 rrt->rrt_gw = nh;
1367 *nq = *np;
1368 addroute(rrt, &nh, ifcp);
1369 }
1370 rrt->rrt_rflags |= RRTF_CHANGED;
1371 rrt->rrt_t = t;
1372 need_trigger = 1;
1373 } else if (nq->rip6_metric < np->rip6_metric &&
1374 rrt->rrt_index == ifcp->ifc_index &&
1375 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1376 /* Got worse route from same gw */
1377 nq->rip6_metric = np->rip6_metric;
1378 rrt->rrt_t = t;
1379 rrt->rrt_rflags |= RRTF_CHANGED;
1380 need_trigger = 1;
1381 } else if (nq->rip6_metric == np->rip6_metric &&
1382 np->rip6_metric < HOPCNT_INFINITY6) {
1383 if (rrt->rrt_index == ifcp->ifc_index &&
1384 IN6_ARE_ADDR_EQUAL(&nh, &rrt->rrt_gw)) {
1385 /* same metric, same route from same gw */
1386 rrt->rrt_t = t;
1387 } else if (rrt->rrt_t < t_half_lifetime) {
1388 /* Better route found */
1389 rrt->rrt_index = ifcp->ifc_index;
1390 /* Update routing table */
1391 delroute(nq, &rrt->rrt_gw);
1392 rrt->rrt_gw = nh;
1393 *nq = *np;
1394 addroute(rrt, &nh, ifcp);
1395 rrt->rrt_rflags |= RRTF_CHANGED;
1396 rrt->rrt_t = t;
1397 }
1398 }
1399 /*
1400 * if nq->rip6_metric == HOPCNT_INFINITY6 then
1401 * do not update age value. Do nothing.
1402 */
1403 } else if (np->rip6_metric < HOPCNT_INFINITY6) {
1404 /* Got a new valid route */
1405 if ((rrt = MALLOC(struct riprt)) == NULL) {
1406 fatal("malloc: struct riprt");
1407 /*NOTREACHED*/
1408 }
1409 memset(rrt, 0, sizeof(*rrt));
1410 nq = &rrt->rrt_info;
1411
1412 rrt->rrt_same = NULL;
1413 rrt->rrt_index = ifcp->ifc_index;
1414 rrt->rrt_flags = RTF_UP|RTF_GATEWAY;
1415 rrt->rrt_gw = nh;
1416 *nq = *np;
1417 applyplen(&nq->rip6_dest, nq->rip6_plen);
1418 if (nq->rip6_plen == sizeof(struct in6_addr) * 8)
1419 rrt->rrt_flags |= RTF_HOST;
1420
1421 /* Update routing table */
1422 addroute(rrt, &nh, ifcp);
1423 rrt->rrt_rflags |= RRTF_CHANGED;
1424 need_trigger = 1;
1425 rrt->rrt_t = t;
1426
1427 /* Put the route to the list */
1428 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
1429 }
1430 }
1431 /* XXX need to care the interval between triggered updates */
1432 if (need_trigger) {
1433 if (nextalarm > time(NULL) + RIP_TRIG_INT6_MAX) {
1434 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1435 if (ifcp->ifc_index == ic->ifc_index)
1436 continue;
1437 if (ic->ifc_flags & IFF_UP)
1438 ripsend(ic, &ic->ifc_ripsin,
1439 RRTF_CHANGED);
1440 }
1441 }
1442 /* Reset the flag */
1443 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1444 rrt->rrt_rflags &= ~RRTF_CHANGED;
1445 }
1446 }
1447 }
1448
1449 /*
1450 * Send all routes request packet to the specified interface.
1451 */
1452 static void
sendrequest(struct ifc * ifcp)1453 sendrequest(struct ifc *ifcp)
1454 {
1455 struct netinfo6 *np;
1456 int error;
1457
1458 if (ifcp->ifc_flags & IFF_LOOPBACK)
1459 return;
1460 ripbuf->rip6_cmd = RIP6_REQUEST;
1461 np = ripbuf->rip6_nets;
1462 memset(np, 0, sizeof(struct netinfo6));
1463 np->rip6_metric = HOPCNT_INFINITY6;
1464 tracet(1, "Send rtdump Request to %s (%s)\n",
1465 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1466 error = sendpacket(&ifcp->ifc_ripsin, RIPSIZE(1));
1467 if (error == EAFNOSUPPORT) {
1468 /* Protocol not supported */
1469 tracet(1, "Could not send rtdump Request to %s (%s): "
1470 "set IFF_UP to 0\n",
1471 ifcp->ifc_name, inet6_n2p(&ifcp->ifc_ripsin.sin6_addr));
1472 ifcp->ifc_flags &= ~IFF_UP; /* As if down for AF_INET6 */
1473 }
1474 ripbuf->rip6_cmd = RIP6_RESPONSE;
1475 }
1476
1477 /*
1478 * Process a RIP6_REQUEST packet.
1479 */
1480 static void
riprequest(struct ifc * ifcp,struct netinfo6 * np,int nn,struct sockaddr_in6 * sin6)1481 riprequest(struct ifc *ifcp,
1482 struct netinfo6 *np,
1483 int nn,
1484 struct sockaddr_in6 *sin6)
1485 {
1486 int i;
1487 struct riprt *rrt;
1488
1489 if (!(nn == 1 && IN6_IS_ADDR_UNSPECIFIED(&np->rip6_dest) &&
1490 np->rip6_plen == 0 && np->rip6_metric == HOPCNT_INFINITY6)) {
1491 /* Specific response, don't split-horizon */
1492 trace(1, "\tRIP Request\n");
1493 for (i = 0; i < nn; i++, np++) {
1494 rrt = rtsearch(np);
1495 if (rrt)
1496 np->rip6_metric = rrt->rrt_info.rip6_metric;
1497 else
1498 np->rip6_metric = HOPCNT_INFINITY6;
1499 }
1500 (void)sendpacket(sin6, RIPSIZE(nn));
1501 return;
1502 }
1503 /* Whole routing table dump */
1504 trace(1, "\tRIP Request -- whole routing table\n");
1505 ripsend(ifcp, sin6, RRTF_SENDANYWAY);
1506 }
1507
1508 /*
1509 * Get information of each interface.
1510 */
1511 static void
ifconfig(void)1512 ifconfig(void)
1513 {
1514 struct ifaddrs *ifap, *ifa;
1515 struct ifc *ifcp;
1516 struct ipv6_mreq mreq;
1517 int s;
1518
1519 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1520 fatal("socket");
1521 /*NOTREACHED*/
1522 }
1523
1524 if (getifaddrs(&ifap) != 0) {
1525 fatal("getifaddrs");
1526 /*NOTREACHED*/
1527 }
1528
1529 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1530 if (ifa->ifa_addr->sa_family != AF_INET6)
1531 continue;
1532 ifcp = ifc_find(ifa->ifa_name);
1533 /* we are interested in multicast-capable interfaces */
1534 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
1535 continue;
1536 if (!ifcp) {
1537 /* new interface */
1538 if ((ifcp = MALLOC(struct ifc)) == NULL) {
1539 fatal("malloc: struct ifc");
1540 /*NOTREACHED*/
1541 }
1542 memset(ifcp, 0, sizeof(*ifcp));
1543
1544 ifcp->ifc_index = -1;
1545 strlcpy(ifcp->ifc_name, ifa->ifa_name,
1546 sizeof(ifcp->ifc_name));
1547 TAILQ_INIT(&ifcp->ifc_ifac_head);
1548 TAILQ_INIT(&ifcp->ifc_iff_head);
1549 ifcp->ifc_flags = ifa->ifa_flags;
1550 TAILQ_INSERT_HEAD(&ifc_head, ifcp, ifc_next);
1551 trace(1, "newif %s <%s>\n", ifcp->ifc_name,
1552 ifflags(ifcp->ifc_flags));
1553 if (!strcmp(ifcp->ifc_name, LOOPBACK_IF))
1554 loopifcp = ifcp;
1555 } else {
1556 /* update flag, this may be up again */
1557 if (ifcp->ifc_flags != ifa->ifa_flags) {
1558 trace(1, "%s: <%s> -> ", ifcp->ifc_name,
1559 ifflags(ifcp->ifc_flags));
1560 trace(1, "<%s>\n", ifflags(ifa->ifa_flags));
1561 ifcp->ifc_cflags |= IFC_CHANGED;
1562 }
1563 ifcp->ifc_flags = ifa->ifa_flags;
1564 }
1565 if (ifconfig1(ifa->ifa_name, ifa->ifa_addr, ifcp, s) < 0) {
1566 /* maybe temporary failure */
1567 continue;
1568 }
1569 if ((ifcp->ifc_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP
1570 && 0 < ifcp->ifc_index && !ifcp->ifc_joined) {
1571 mreq.ipv6mr_multiaddr = ifcp->ifc_ripsin.sin6_addr;
1572 mreq.ipv6mr_interface = ifcp->ifc_index;
1573 if (setsockopt(ripsock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1574 &mreq, sizeof(mreq)) < 0) {
1575 fatal("IPV6_JOIN_GROUP");
1576 /*NOTREACHED*/
1577 }
1578 trace(1, "join %s %s\n", ifcp->ifc_name, RIP6_DEST);
1579 ifcp->ifc_joined++;
1580 }
1581 }
1582 close(s);
1583 freeifaddrs(ifap);
1584 }
1585
1586 static int
ifconfig1(const char * name,const struct sockaddr * sa,struct ifc * ifcp,int s)1587 ifconfig1(const char *name,
1588 const struct sockaddr *sa,
1589 struct ifc *ifcp,
1590 int s)
1591 {
1592 struct in6_ifreq ifr;
1593 const struct sockaddr_in6 *sin6;
1594 struct ifac *ifac;
1595 int plen;
1596 char buf[BUFSIZ];
1597
1598 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
1599 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !lflag)
1600 return (-1);
1601 ifr.ifr_addr = *sin6;
1602 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1603 if (ioctl(s, SIOCGIFNETMASK_IN6, (char *)&ifr) < 0) {
1604 syslog(LOG_INFO, "ioctl: SIOCGIFNETMASK_IN6");
1605 return (-1);
1606 }
1607 plen = sin6mask2len(&ifr.ifr_addr);
1608 if ((ifac = ifa_match(ifcp, &sin6->sin6_addr, plen)) != NULL) {
1609 /* same interface found */
1610 /* need check if something changed */
1611 /* XXX not yet implemented */
1612 return (-1);
1613 }
1614 /*
1615 * New address is found
1616 */
1617 if ((ifac = MALLOC(struct ifac)) == NULL) {
1618 fatal("malloc: struct ifac");
1619 /*NOTREACHED*/
1620 }
1621 memset(ifac, 0, sizeof(*ifac));
1622
1623 ifac->ifac_ifc = ifcp;
1624 ifac->ifac_addr = sin6->sin6_addr;
1625 ifac->ifac_plen = plen;
1626 ifac->ifac_scope_id = sin6->sin6_scope_id;
1627 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
1628 ifr.ifr_addr = *sin6;
1629 if (ioctl(s, SIOCGIFDSTADDR_IN6, (char *)&ifr) < 0) {
1630 fatal("ioctl: SIOCGIFDSTADDR_IN6");
1631 /*NOTREACHED*/
1632 }
1633 ifac->ifac_raddr = ifr.ifr_dstaddr.sin6_addr;
1634 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr, buf,
1635 sizeof(buf));
1636 trace(1, "found address %s/%d -- %s\n",
1637 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen, buf);
1638 } else {
1639 trace(1, "found address %s/%d\n",
1640 inet6_n2p(&ifac->ifac_addr), ifac->ifac_plen);
1641 }
1642 if (ifcp->ifc_index < 0 && IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
1643 ifcp->ifc_mylladdr = ifac->ifac_addr;
1644 ifcp->ifc_index = ifac->ifac_scope_id;
1645 memcpy(&ifcp->ifc_ripsin, &ripsin, ripsin.ss_len);
1646 ifcp->ifc_ripsin.sin6_scope_id = ifcp->ifc_index;
1647 setindex2ifc(ifcp->ifc_index, ifcp);
1648 ifcp->ifc_mtu = getifmtu(ifcp->ifc_index);
1649 if (ifcp->ifc_mtu > RIP6_MAXMTU)
1650 ifcp->ifc_mtu = RIP6_MAXMTU;
1651 if (ioctl(s, SIOCGIFMETRIC, (char *)&ifr) < 0) {
1652 fatal("ioctl: SIOCGIFMETRIC");
1653 /*NOTREACHED*/
1654 }
1655 ifcp->ifc_metric = ifr.ifr_metric;
1656 trace(1, "\tindex: %d, mtu: %d, metric: %d\n",
1657 ifcp->ifc_index, ifcp->ifc_mtu, ifcp->ifc_metric);
1658 } else
1659 ifcp->ifc_cflags |= IFC_CHANGED;
1660
1661 TAILQ_INSERT_HEAD(&ifcp->ifc_ifac_head, ifac, ifac_next);
1662
1663 return 0;
1664 }
1665
1666 static void
ifremove(int ifindex)1667 ifremove(int ifindex)
1668 {
1669 struct ifc *ifcp;
1670 struct riprt *rrt;
1671
1672 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1673 if (ifcp->ifc_index == ifindex)
1674 break;
1675 }
1676 if (ifcp == NULL)
1677 return;
1678
1679 tracet(1, "ifremove: %s is departed.\n", ifcp->ifc_name);
1680 TAILQ_REMOVE(&ifc_head, ifcp, ifc_next);
1681
1682 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1683 if (rrt->rrt_index == ifcp->ifc_index &&
1684 rrt->rrt_rflags & RRTF_AGGREGATE)
1685 delroute(&rrt->rrt_info, &rrt->rrt_gw);
1686 }
1687 free(ifcp);
1688 }
1689
1690 /*
1691 * Receive and process routing messages.
1692 * Update interface information as necesssary.
1693 */
1694 static void
rtrecv(void)1695 rtrecv(void)
1696 {
1697 char buf[BUFSIZ];
1698 char *p, *q = NULL;
1699 struct rt_msghdr *rtm;
1700 struct ifa_msghdr *ifam;
1701 struct if_msghdr *ifm;
1702 struct if_announcemsghdr *ifan;
1703 int len;
1704 struct ifc *ifcp, *ic;
1705 int iface = 0, rtable = 0;
1706 struct sockaddr_in6 *rta[RTAX_MAX];
1707 struct sockaddr_in6 mask;
1708 int i, addrs = 0;
1709 struct riprt *rrt;
1710
1711 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
1712 perror("read from rtsock");
1713 exit(1);
1714 }
1715 if (len == 0)
1716 return;
1717 #if 0
1718 if (len < sizeof(*rtm)) {
1719 trace(1, "short read from rtsock: %d (should be > %lu)\n",
1720 len, (u_long)sizeof(*rtm));
1721 return;
1722 }
1723 #endif
1724 if (dflag >= 2) {
1725 fprintf(stderr, "rtmsg:\n");
1726 for (i = 0; i < len; i++) {
1727 fprintf(stderr, "%02x ", buf[i] & 0xff);
1728 if (i % 16 == 15) fprintf(stderr, "\n");
1729 }
1730 fprintf(stderr, "\n");
1731 }
1732
1733 for (p = buf; p - buf < len; p +=
1734 ((struct rt_msghdr *)(void *)p)->rtm_msglen) {
1735 if (((struct rt_msghdr *)(void *)p)->rtm_version != RTM_VERSION)
1736 continue;
1737
1738 /* safety against bogus message */
1739 if (((struct rt_msghdr *)(void *)p)->rtm_msglen <= 0) {
1740 trace(1, "bogus rtmsg: length=%d\n",
1741 ((struct rt_msghdr *)(void *)p)->rtm_msglen);
1742 break;
1743 }
1744 rtm = NULL;
1745 ifam = NULL;
1746 ifm = NULL;
1747 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1748 case RTM_NEWADDR:
1749 case RTM_DELADDR:
1750 ifam = (struct ifa_msghdr *)(void *)p;
1751 addrs = ifam->ifam_addrs;
1752 q = (char *)(ifam + 1);
1753 break;
1754 case RTM_IFINFO:
1755 ifm = (struct if_msghdr *)(void *)p;
1756 addrs = ifm->ifm_addrs;
1757 q = (char *)(ifm + 1);
1758 break;
1759 case RTM_IFANNOUNCE:
1760 ifan = (struct if_announcemsghdr *)(void *)p;
1761 switch (ifan->ifan_what) {
1762 case IFAN_ARRIVAL:
1763 iface++;
1764 break;
1765 case IFAN_DEPARTURE:
1766 ifremove(ifan->ifan_index);
1767 iface++;
1768 break;
1769 }
1770 break;
1771 default:
1772 rtm = (struct rt_msghdr *)(void *)p;
1773 if (rtm->rtm_version != RTM_VERSION) {
1774 trace(1, "unexpected rtmsg version %d "
1775 "(should be %d)\n",
1776 rtm->rtm_version, RTM_VERSION);
1777 continue;
1778 }
1779 /*
1780 * Only messages that use the struct rt_msghdr
1781 * format are allowed beyond this point.
1782 */
1783 if (rtm->rtm_type > RTM_RESOLVE) {
1784 trace(1, "rtmsg type %d ignored\n",
1785 rtm->rtm_type);
1786 continue;
1787 }
1788 addrs = rtm->rtm_addrs;
1789 q = (char *)(rtm + 1);
1790 if (rtm->rtm_pid == pid) {
1791 #if 0
1792 trace(1, "rtmsg looped back to me, ignored\n");
1793 #endif
1794 continue;
1795 }
1796 break;
1797 }
1798 memset(&rta, 0, sizeof(rta));
1799 for (i = 0; i < RTAX_MAX; i++) {
1800 if (addrs & (1 << i)) {
1801 rta[i] = (struct sockaddr_in6 *)(void *)q;
1802 q += ROUNDUP(rta[i]->sin6_len);
1803 }
1804 }
1805
1806 trace(1, "rtsock: %s (addrs=%x)\n",
1807 rttypes((struct rt_msghdr *)(void *)p), addrs);
1808 if (dflag >= 2) {
1809 for (i = 0;
1810 i < ((struct rt_msghdr *)(void *)p)->rtm_msglen;
1811 i++) {
1812 fprintf(stderr, "%02x ", p[i] & 0xff);
1813 if (i % 16 == 15) fprintf(stderr, "\n");
1814 }
1815 fprintf(stderr, "\n");
1816 }
1817
1818 /*
1819 * Easy ones first.
1820 *
1821 * We may be able to optimize by using ifm->ifm_index or
1822 * ifam->ifam_index. For simplicity we don't do that here.
1823 */
1824 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1825 case RTM_NEWADDR:
1826 case RTM_IFINFO:
1827 iface++;
1828 continue;
1829 case RTM_ADD:
1830 rtable++;
1831 continue;
1832 case RTM_LOSING:
1833 case RTM_MISS:
1834 case RTM_GET:
1835 case RTM_LOCK:
1836 /* nothing to be done here */
1837 trace(1, "\tnothing to be done, ignored\n");
1838 continue;
1839 }
1840
1841 #if 0
1842 if (rta[RTAX_DST] == NULL) {
1843 trace(1, "\tno destination, ignored\n");
1844 continue;
1845 }
1846 if (rta[RTAX_DST]->sin6_family != AF_INET6) {
1847 trace(1, "\taf mismatch, ignored\n");
1848 continue;
1849 }
1850 if (IN6_IS_ADDR_LINKLOCAL(&rta[RTAX_DST]->sin6_addr)) {
1851 trace(1, "\tlinklocal destination, ignored\n");
1852 continue;
1853 }
1854 if (IN6_ARE_ADDR_EQUAL(&rta[RTAX_DST]->sin6_addr, &in6addr_loopback)) {
1855 trace(1, "\tloopback destination, ignored\n");
1856 continue; /* Loopback */
1857 }
1858 if (IN6_IS_ADDR_MULTICAST(&rta[RTAX_DST]->sin6_addr)) {
1859 trace(1, "\tmulticast destination, ignored\n");
1860 continue;
1861 }
1862 #endif
1863
1864 /* hard ones */
1865 switch (((struct rt_msghdr *)(void *)p)->rtm_type) {
1866 case RTM_NEWADDR:
1867 case RTM_IFINFO:
1868 case RTM_ADD:
1869 case RTM_LOSING:
1870 case RTM_MISS:
1871 case RTM_GET:
1872 case RTM_LOCK:
1873 /* should already be handled */
1874 fatal("rtrecv: never reach here");
1875 /*NOTREACHED*/
1876 case RTM_DELETE:
1877 if (!rta[RTAX_DST] || !rta[RTAX_GATEWAY]) {
1878 trace(1, "\tsome of dst/gw/netamsk are "
1879 "unavailable, ignored\n");
1880 break;
1881 }
1882 if ((rtm->rtm_flags & RTF_HOST) != 0) {
1883 mask.sin6_len = sizeof(mask);
1884 memset(&mask.sin6_addr, 0xff,
1885 sizeof(mask.sin6_addr));
1886 rta[RTAX_NETMASK] = &mask;
1887 } else if (!rta[RTAX_NETMASK]) {
1888 trace(1, "\tsome of dst/gw/netamsk are "
1889 "unavailable, ignored\n");
1890 break;
1891 }
1892 if (rt_del(rta[RTAX_DST], rta[RTAX_GATEWAY],
1893 rta[RTAX_NETMASK]) == 0) {
1894 rtable++; /*just to be sure*/
1895 }
1896 break;
1897 case RTM_CHANGE:
1898 case RTM_REDIRECT:
1899 trace(1, "\tnot supported yet, ignored\n");
1900 break;
1901 case RTM_DELADDR:
1902 if (!rta[RTAX_NETMASK] || !rta[RTAX_IFA]) {
1903 trace(1, "\tno netmask or ifa given, ignored\n");
1904 break;
1905 }
1906 if (ifam->ifam_index < nindex2ifc)
1907 ifcp = index2ifc[ifam->ifam_index];
1908 else
1909 ifcp = NULL;
1910 if (!ifcp) {
1911 trace(1, "\tinvalid ifam_index %d, ignored\n",
1912 ifam->ifam_index);
1913 break;
1914 }
1915 if (!rt_deladdr(ifcp, rta[RTAX_IFA], rta[RTAX_NETMASK]))
1916 iface++;
1917 break;
1918 }
1919
1920 }
1921
1922 if (iface) {
1923 trace(1, "rtsock: reconfigure interfaces, refresh interface routes\n");
1924 ifconfig();
1925 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
1926 if (ifcp->ifc_cflags & IFC_CHANGED) {
1927 if (ifrt(ifcp, 1)) {
1928 TAILQ_FOREACH(ic, &ifc_head, ifc_next) {
1929 if (ifcp->ifc_index == ic->ifc_index)
1930 continue;
1931 if (ic->ifc_flags & IFF_UP)
1932 ripsend(ic, &ic->ifc_ripsin,
1933 RRTF_CHANGED);
1934 }
1935 /* Reset the flag */
1936 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1937 rrt->rrt_rflags &= ~RRTF_CHANGED;
1938 }
1939 }
1940 ifcp->ifc_cflags &= ~IFC_CHANGED;
1941 }
1942 }
1943 }
1944 if (rtable) {
1945 trace(1, "rtsock: read routing table again\n");
1946 krtread(1);
1947 }
1948 }
1949
1950 /*
1951 * remove specified route from the internal routing table.
1952 */
1953 static int
rt_del(const struct sockaddr_in6 * sdst,const struct sockaddr_in6 * sgw,const struct sockaddr_in6 * smask)1954 rt_del(const struct sockaddr_in6 *sdst,
1955 const struct sockaddr_in6 *sgw,
1956 const struct sockaddr_in6 *smask)
1957 {
1958 const struct in6_addr *dst = NULL;
1959 const struct in6_addr *gw = NULL;
1960 int prefix;
1961 struct netinfo6 ni6;
1962 struct riprt *rrt = NULL;
1963 time_t t_lifetime;
1964
1965 if (sdst->sin6_family != AF_INET6) {
1966 trace(1, "\tother AF, ignored\n");
1967 return -1;
1968 }
1969 if (IN6_IS_ADDR_LINKLOCAL(&sdst->sin6_addr)
1970 || IN6_ARE_ADDR_EQUAL(&sdst->sin6_addr, &in6addr_loopback)
1971 || IN6_IS_ADDR_MULTICAST(&sdst->sin6_addr)) {
1972 trace(1, "\taddress %s not interesting, ignored\n",
1973 inet6_n2p(&sdst->sin6_addr));
1974 return -1;
1975 }
1976 dst = &sdst->sin6_addr;
1977 if (sgw->sin6_family == AF_INET6) {
1978 /* easy case */
1979 gw = &sgw->sin6_addr;
1980 prefix = sin6mask2len(smask);
1981 } else if (sgw->sin6_family == AF_LINK) {
1982 /*
1983 * Interface route... a hard case. We need to get the prefix
1984 * length from the kernel, but we now are parsing rtmsg.
1985 * We'll purge matching routes from my list, then get the
1986 * fresh list.
1987 */
1988 struct riprt *longest;
1989 trace(1, "\t%s is an interface route, guessing prefixlen\n",
1990 inet6_n2p(dst));
1991 longest = NULL;
1992 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
1993 if (IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
1994 &sdst->sin6_addr)
1995 && IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw)) {
1996 if (!longest
1997 || longest->rrt_info.rip6_plen <
1998 rrt->rrt_info.rip6_plen) {
1999 longest = rrt;
2000 }
2001 }
2002 }
2003 rrt = longest;
2004 if (!rrt) {
2005 trace(1, "\tno matching interface route found\n");
2006 return -1;
2007 }
2008 gw = &in6addr_loopback;
2009 prefix = rrt->rrt_info.rip6_plen;
2010 } else {
2011 trace(1, "\tunsupported af: (gw=%d)\n", sgw->sin6_family);
2012 return -1;
2013 }
2014
2015 trace(1, "\tdeleting %s/%d ", inet6_n2p(dst), prefix);
2016 trace(1, "gw %s\n", inet6_n2p(gw));
2017 t_lifetime = time(NULL) - RIP_LIFETIME;
2018 /* age route for interface address */
2019 memset(&ni6, 0, sizeof(ni6));
2020 ni6.rip6_dest = *dst;
2021 ni6.rip6_plen = prefix;
2022 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2023 trace(1, "\tfind route %s/%d\n", inet6_n2p(&ni6.rip6_dest),
2024 ni6.rip6_plen);
2025 if (!rrt && (rrt = rtsearch(&ni6)) == NULL) {
2026 trace(1, "\tno route found\n");
2027 return -1;
2028 }
2029 #if 0
2030 if ((rrt->rrt_flags & RTF_STATIC) == 0) {
2031 trace(1, "\tyou can delete static routes only\n");
2032 } else
2033 #endif
2034 if (!IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, gw)) {
2035 trace(1, "\tgw mismatch: %s <-> ",
2036 inet6_n2p(&rrt->rrt_gw));
2037 trace(1, "%s\n", inet6_n2p(gw));
2038 } else {
2039 trace(1, "\troute found, age it\n");
2040 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2041 rrt->rrt_t = t_lifetime;
2042 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2043 }
2044 }
2045 return 0;
2046 }
2047
2048 /*
2049 * remove specified address from internal interface/routing table.
2050 */
2051 static int
rt_deladdr(struct ifc * ifcp,const struct sockaddr_in6 * sifa,const struct sockaddr_in6 * smask)2052 rt_deladdr(struct ifc *ifcp,
2053 const struct sockaddr_in6 *sifa,
2054 const struct sockaddr_in6 *smask)
2055 {
2056 const struct in6_addr *addr = NULL;
2057 int prefix;
2058 struct ifac *ifac = NULL;
2059 struct netinfo6 ni6;
2060 struct riprt *rrt = NULL;
2061 time_t t_lifetime;
2062 int updated = 0;
2063
2064 if (sifa->sin6_family != AF_INET6) {
2065 trace(1, "\tother AF, ignored\n");
2066 return -1;
2067 }
2068 addr = &sifa->sin6_addr;
2069 prefix = sin6mask2len(smask);
2070
2071 trace(1, "\tdeleting %s/%d from %s\n",
2072 inet6_n2p(addr), prefix, ifcp->ifc_name);
2073 ifac = ifa_match(ifcp, addr, prefix);
2074 if (!ifac) {
2075 trace(1, "\tno matching ifa found for %s/%d on %s\n",
2076 inet6_n2p(addr), prefix, ifcp->ifc_name);
2077 return -1;
2078 }
2079 if (ifac->ifac_ifc != ifcp) {
2080 trace(1, "\taddress table corrupt: back pointer does not match "
2081 "(%s != %s)\n",
2082 ifcp->ifc_name, ifac->ifac_ifc->ifc_name);
2083 return -1;
2084 }
2085 TAILQ_REMOVE(&ifcp->ifc_ifac_head, ifac, ifac_next);
2086 t_lifetime = time(NULL) - RIP_LIFETIME;
2087 /* age route for interface address */
2088 memset(&ni6, 0, sizeof(ni6));
2089 ni6.rip6_dest = ifac->ifac_addr;
2090 ni6.rip6_plen = ifac->ifac_plen;
2091 applyplen(&ni6.rip6_dest, ni6.rip6_plen);
2092 trace(1, "\tfind interface route %s/%d on %d\n",
2093 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen, ifcp->ifc_index);
2094 if ((rrt = rtsearch(&ni6)) != NULL) {
2095 struct in6_addr none;
2096 memset(&none, 0, sizeof(none));
2097 if (rrt->rrt_index == ifcp->ifc_index &&
2098 (IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw, &none) ||
2099 IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw))) {
2100 trace(1, "\troute found, age it\n");
2101 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2102 rrt->rrt_t = t_lifetime;
2103 rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2104 }
2105 updated++;
2106 } else {
2107 trace(1, "\tnon-interface route found: %s/%d on %d\n",
2108 inet6_n2p(&rrt->rrt_info.rip6_dest),
2109 rrt->rrt_info.rip6_plen,
2110 rrt->rrt_index);
2111 }
2112 } else
2113 trace(1, "\tno interface route found\n");
2114 /* age route for p2p destination */
2115 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2116 memset(&ni6, 0, sizeof(ni6));
2117 ni6.rip6_dest = ifac->ifac_raddr;
2118 ni6.rip6_plen = 128;
2119 applyplen(&ni6.rip6_dest, ni6.rip6_plen); /*to be sure*/
2120 trace(1, "\tfind p2p route %s/%d on %d\n",
2121 inet6_n2p(&ni6.rip6_dest), ni6.rip6_plen,
2122 ifcp->ifc_index);
2123 if ((rrt = rtsearch(&ni6)) != NULL) {
2124 if (rrt->rrt_index == ifcp->ifc_index &&
2125 IN6_ARE_ADDR_EQUAL(&rrt->rrt_gw,
2126 &ifac->ifac_addr)) {
2127 trace(1, "\troute found, age it\n");
2128 if (rrt->rrt_t == 0 || rrt->rrt_t > t_lifetime) {
2129 rrt->rrt_t = t_lifetime;
2130 rrt->rrt_info.rip6_metric =
2131 HOPCNT_INFINITY6;
2132 updated++;
2133 }
2134 } else {
2135 trace(1, "\tnon-p2p route found: %s/%d on %d\n",
2136 inet6_n2p(&rrt->rrt_info.rip6_dest),
2137 rrt->rrt_info.rip6_plen,
2138 rrt->rrt_index);
2139 }
2140 } else
2141 trace(1, "\tno p2p route found\n");
2142 }
2143 free(ifac);
2144
2145 return ((updated) ? 0 : -1);
2146 }
2147
2148 /*
2149 * Get each interface address and put those interface routes to the route
2150 * list.
2151 */
2152 static int
ifrt(struct ifc * ifcp,int again)2153 ifrt(struct ifc *ifcp, int again)
2154 {
2155 struct ifac *ifac;
2156 struct riprt *rrt = NULL, *search_rrt, *loop_rrt;
2157 struct netinfo6 *np;
2158 time_t t_lifetime;
2159 int need_trigger = 0;
2160
2161 #if 0
2162 if (ifcp->ifc_flags & IFF_LOOPBACK)
2163 return 0; /* ignore loopback */
2164 #endif
2165
2166 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
2167 ifrt_p2p(ifcp, again);
2168 return 0;
2169 }
2170
2171 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2172 if (IN6_IS_ADDR_LINKLOCAL(&ifac->ifac_addr)) {
2173 #if 0
2174 trace(1, "route: %s on %s: "
2175 "skip linklocal interface address\n",
2176 inet6_n2p(&ifac->ifac_addr), ifcp->ifc_name);
2177 #endif
2178 continue;
2179 }
2180 if (IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_addr)) {
2181 #if 0
2182 trace(1, "route: %s: skip unspec interface address\n",
2183 ifcp->ifc_name);
2184 #endif
2185 continue;
2186 }
2187 if (IN6_IS_ADDR_LOOPBACK(&ifac->ifac_addr)) {
2188 #if 0
2189 trace(1, "route: %s: skip loopback address\n",
2190 ifcp->ifc_name);
2191 #endif
2192 continue;
2193 }
2194 if (ifcp->ifc_flags & IFF_UP) {
2195 if ((rrt = MALLOC(struct riprt)) == NULL)
2196 fatal("malloc: struct riprt");
2197 memset(rrt, 0, sizeof(*rrt));
2198 rrt->rrt_same = NULL;
2199 rrt->rrt_index = ifcp->ifc_index;
2200 rrt->rrt_t = 0; /* don't age */
2201 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2202 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2203 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2204 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2205 rrt->rrt_flags = RTF_HOST;
2206 rrt->rrt_rflags |= RRTF_CHANGED;
2207 applyplen(&rrt->rrt_info.rip6_dest, ifac->ifac_plen);
2208 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2209 rrt->rrt_gw = ifac->ifac_addr;
2210 np = &rrt->rrt_info;
2211 search_rrt = rtsearch(np);
2212 if (search_rrt != NULL) {
2213 if (search_rrt->rrt_info.rip6_metric <=
2214 rrt->rrt_info.rip6_metric) {
2215 /* Already have better route */
2216 if (!again) {
2217 trace(1, "route: %s/%d: "
2218 "already registered (%s)\n",
2219 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2220 ifcp->ifc_name);
2221 }
2222 goto next;
2223 }
2224
2225 TAILQ_REMOVE(&riprt_head, search_rrt, rrt_next);
2226 delroute(&search_rrt->rrt_info,
2227 &search_rrt->rrt_gw);
2228 free(search_rrt);
2229 }
2230 /* Attach the route to the list */
2231 trace(1, "route: %s/%d: register route (%s)\n",
2232 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2233 ifcp->ifc_name);
2234 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2235 addroute(rrt, &rrt->rrt_gw, ifcp);
2236 rrt = NULL;
2237 sendrequest(ifcp);
2238 ripsend(ifcp, &ifcp->ifc_ripsin, 0);
2239 need_trigger = 1;
2240 } else {
2241 TAILQ_FOREACH(loop_rrt, &riprt_head, rrt_next) {
2242 if (loop_rrt->rrt_index == ifcp->ifc_index) {
2243 t_lifetime = time(NULL) - RIP_LIFETIME;
2244 if (loop_rrt->rrt_t == 0 || loop_rrt->rrt_t > t_lifetime) {
2245 loop_rrt->rrt_t = t_lifetime;
2246 loop_rrt->rrt_info.rip6_metric = HOPCNT_INFINITY6;
2247 loop_rrt->rrt_rflags |= RRTF_CHANGED;
2248 need_trigger = 1;
2249 }
2250 }
2251 }
2252 }
2253 next:
2254 if (rrt)
2255 free(rrt);
2256 }
2257 return need_trigger;
2258 }
2259
2260 /*
2261 * there are couple of p2p interface routing models. "behavior" lets
2262 * you pick one. it looks that gated behavior fits best with BSDs,
2263 * since BSD kernels do not look at prefix length on p2p interfaces.
2264 */
2265 static void
ifrt_p2p(struct ifc * ifcp,int again)2266 ifrt_p2p(struct ifc *ifcp, int again)
2267 {
2268 struct ifac *ifac;
2269 struct riprt *rrt, *orrt;
2270 struct netinfo6 *np;
2271 struct in6_addr addr, dest;
2272 int advert, ignore, i;
2273 #define P2PADVERT_NETWORK 1
2274 #define P2PADVERT_ADDR 2
2275 #define P2PADVERT_DEST 4
2276 #define P2PADVERT_MAX 4
2277 const enum { CISCO, GATED, ROUTE6D } behavior = GATED;
2278 const char *category = "";
2279 const char *noadv;
2280
2281 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
2282 addr = ifac->ifac_addr;
2283 dest = ifac->ifac_raddr;
2284 applyplen(&addr, ifac->ifac_plen);
2285 applyplen(&dest, ifac->ifac_plen);
2286 advert = ignore = 0;
2287 switch (behavior) {
2288 case CISCO:
2289 /*
2290 * honor addr/plen, just like normal shared medium
2291 * interface. this may cause trouble if you reuse
2292 * addr/plen on other interfaces.
2293 *
2294 * advertise addr/plen.
2295 */
2296 advert |= P2PADVERT_NETWORK;
2297 break;
2298 case GATED:
2299 /*
2300 * prefixlen on p2p interface is meaningless.
2301 * advertise addr/128 and dest/128.
2302 *
2303 * do not install network route to route6d routing
2304 * table (if we do, it would prevent route installation
2305 * for other p2p interface that shares addr/plen).
2306 *
2307 * XXX what should we do if dest is ::? it will not
2308 * get announced anyways (see following filter),
2309 * but we need to think.
2310 */
2311 advert |= P2PADVERT_ADDR;
2312 advert |= P2PADVERT_DEST;
2313 ignore |= P2PADVERT_NETWORK;
2314 break;
2315 case ROUTE6D:
2316 /*
2317 * just for testing. actually the code is redundant
2318 * given the current p2p interface address assignment
2319 * rule for kame kernel.
2320 *
2321 * intent:
2322 * A/n -> announce A/n
2323 * A B/n, A and B share prefix -> A/n (= B/n)
2324 * A B/n, do not share prefix -> A/128 and B/128
2325 * actually, A/64 and A B/128 are the only cases
2326 * permitted by the kernel:
2327 * A/64 -> A/64
2328 * A B/128 -> A/128 and B/128
2329 */
2330 if (!IN6_IS_ADDR_UNSPECIFIED(&ifac->ifac_raddr)) {
2331 if (IN6_ARE_ADDR_EQUAL(&addr, &dest))
2332 advert |= P2PADVERT_NETWORK;
2333 else {
2334 advert |= P2PADVERT_ADDR;
2335 advert |= P2PADVERT_DEST;
2336 ignore |= P2PADVERT_NETWORK;
2337 }
2338 } else
2339 advert |= P2PADVERT_NETWORK;
2340 break;
2341 }
2342
2343 for (i = 1; i <= P2PADVERT_MAX; i *= 2) {
2344 if ((ignore & i) != 0)
2345 continue;
2346 if ((rrt = MALLOC(struct riprt)) == NULL) {
2347 fatal("malloc: struct riprt");
2348 /*NOTREACHED*/
2349 }
2350 memset(rrt, 0, sizeof(*rrt));
2351 rrt->rrt_same = NULL;
2352 rrt->rrt_index = ifcp->ifc_index;
2353 rrt->rrt_t = 0; /* don't age */
2354 switch (i) {
2355 case P2PADVERT_NETWORK:
2356 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2357 rrt->rrt_info.rip6_plen = ifac->ifac_plen;
2358 applyplen(&rrt->rrt_info.rip6_dest,
2359 ifac->ifac_plen);
2360 category = "network";
2361 break;
2362 case P2PADVERT_ADDR:
2363 rrt->rrt_info.rip6_dest = ifac->ifac_addr;
2364 rrt->rrt_info.rip6_plen = 128;
2365 rrt->rrt_gw = in6addr_loopback;
2366 category = "addr";
2367 break;
2368 case P2PADVERT_DEST:
2369 rrt->rrt_info.rip6_dest = ifac->ifac_raddr;
2370 rrt->rrt_info.rip6_plen = 128;
2371 rrt->rrt_gw = ifac->ifac_addr;
2372 category = "dest";
2373 break;
2374 }
2375 if (IN6_IS_ADDR_UNSPECIFIED(&rrt->rrt_info.rip6_dest) ||
2376 IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_info.rip6_dest)) {
2377 #if 0
2378 trace(1, "route: %s: skip unspec/linklocal "
2379 "(%s on %s)\n", category, ifcp->ifc_name);
2380 #endif
2381 free(rrt);
2382 continue;
2383 }
2384 if ((advert & i) == 0) {
2385 rrt->rrt_rflags |= RRTF_NOADVERTISE;
2386 noadv = ", NO-ADV";
2387 } else
2388 noadv = "";
2389 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
2390 rrt->rrt_info.rip6_metric = 1 + ifcp->ifc_metric;
2391 np = &rrt->rrt_info;
2392 orrt = rtsearch(np);
2393 if (!orrt) {
2394 /* Attach the route to the list */
2395 trace(1, "route: %s/%d: register route "
2396 "(%s on %s%s)\n",
2397 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2398 category, ifcp->ifc_name, noadv);
2399 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2400 } else if (rrt->rrt_index != orrt->rrt_index ||
2401 rrt->rrt_info.rip6_metric != orrt->rrt_info.rip6_metric) {
2402 /* replace route */
2403 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2404 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2405 free(orrt);
2406
2407 trace(1, "route: %s/%d: update (%s on %s%s)\n",
2408 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2409 category, ifcp->ifc_name, noadv);
2410 } else {
2411 /* Already found */
2412 if (!again) {
2413 trace(1, "route: %s/%d: "
2414 "already registered (%s on %s%s)\n",
2415 inet6_n2p(&np->rip6_dest),
2416 np->rip6_plen, category,
2417 ifcp->ifc_name, noadv);
2418 }
2419 free(rrt);
2420 }
2421 }
2422 }
2423 #undef P2PADVERT_NETWORK
2424 #undef P2PADVERT_ADDR
2425 #undef P2PADVERT_DEST
2426 #undef P2PADVERT_MAX
2427 }
2428
2429 static int
getifmtu(int ifindex)2430 getifmtu(int ifindex)
2431 {
2432 int mib[6];
2433 char *buf;
2434 size_t msize;
2435 struct if_msghdr *ifm;
2436 int mtu;
2437
2438 mib[0] = CTL_NET;
2439 mib[1] = PF_ROUTE;
2440 mib[2] = 0;
2441 mib[3] = AF_INET6;
2442 mib[4] = NET_RT_IFLIST;
2443 mib[5] = ifindex;
2444 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2445 fatal("sysctl estimate NET_RT_IFLIST");
2446 /*NOTREACHED*/
2447 }
2448 if ((buf = malloc(msize)) == NULL) {
2449 fatal("malloc");
2450 /*NOTREACHED*/
2451 }
2452 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2453 fatal("sysctl NET_RT_IFLIST");
2454 /*NOTREACHED*/
2455 }
2456 ifm = (struct if_msghdr *)(void *)buf;
2457 mtu = ifm->ifm_data.ifi_mtu;
2458 if (ifindex != ifm->ifm_index) {
2459 fatal("ifindex does not match with ifm_index");
2460 /*NOTREACHED*/
2461 }
2462 free(buf);
2463 return mtu;
2464 }
2465
2466 static const char *
rttypes(struct rt_msghdr * rtm)2467 rttypes(struct rt_msghdr *rtm)
2468 {
2469 #define RTTYPE(s, f) \
2470 do { \
2471 if (rtm->rtm_type == (f)) \
2472 return (s); \
2473 } while (0)
2474 RTTYPE("ADD", RTM_ADD);
2475 RTTYPE("DELETE", RTM_DELETE);
2476 RTTYPE("CHANGE", RTM_CHANGE);
2477 RTTYPE("GET", RTM_GET);
2478 RTTYPE("LOSING", RTM_LOSING);
2479 RTTYPE("REDIRECT", RTM_REDIRECT);
2480 RTTYPE("MISS", RTM_MISS);
2481 RTTYPE("LOCK", RTM_LOCK);
2482 RTTYPE("NEWADDR", RTM_NEWADDR);
2483 RTTYPE("DELADDR", RTM_DELADDR);
2484 RTTYPE("IFINFO", RTM_IFINFO);
2485 #ifdef RTM_OIFINFO
2486 RTTYPE("OIFINFO", RTM_OIFINFO);
2487 #endif
2488 #ifdef RTM_IFANNOUNCE
2489 RTTYPE("IFANNOUNCE", RTM_IFANNOUNCE);
2490 #endif
2491 #ifdef RTM_NEWMADDR
2492 RTTYPE("NEWMADDR", RTM_NEWMADDR);
2493 #endif
2494 #ifdef RTM_DELMADDR
2495 RTTYPE("DELMADDR", RTM_DELMADDR);
2496 #endif
2497 #undef RTTYPE
2498 return NULL;
2499 }
2500
2501 static const char *
rtflags(struct rt_msghdr * rtm)2502 rtflags(struct rt_msghdr *rtm)
2503 {
2504 static char buf[BUFSIZ];
2505
2506 /*
2507 * letter conflict should be okay. painful when *BSD diverges...
2508 */
2509 strlcpy(buf, "", sizeof(buf));
2510 #define RTFLAG(s, f) \
2511 do { \
2512 if (rtm->rtm_flags & (f)) \
2513 strlcat(buf, (s), sizeof(buf)); \
2514 } while (0)
2515 RTFLAG("U", RTF_UP);
2516 RTFLAG("G", RTF_GATEWAY);
2517 RTFLAG("H", RTF_HOST);
2518 RTFLAG("R", RTF_REJECT);
2519 RTFLAG("D", RTF_DYNAMIC);
2520 RTFLAG("M", RTF_MODIFIED);
2521 RTFLAG("d", RTF_DONE);
2522 #ifdef RTF_MASK
2523 RTFLAG("m", RTF_MASK);
2524 #endif
2525 #ifdef RTF_CLONED
2526 RTFLAG("c", RTF_CLONED);
2527 #endif
2528 RTFLAG("X", RTF_XRESOLVE);
2529 #ifdef RTF_LLINFO
2530 RTFLAG("L", RTF_LLINFO);
2531 #endif
2532 RTFLAG("S", RTF_STATIC);
2533 RTFLAG("B", RTF_BLACKHOLE);
2534 #ifdef RTF_PROTO3
2535 RTFLAG("3", RTF_PROTO3);
2536 #endif
2537 RTFLAG("2", RTF_PROTO2);
2538 RTFLAG("1", RTF_PROTO1);
2539 #ifdef RTF_BROADCAST
2540 RTFLAG("b", RTF_BROADCAST);
2541 #endif
2542 #ifdef RTF_DEFAULT
2543 RTFLAG("d", RTF_DEFAULT);
2544 #endif
2545 #ifdef RTF_ISAROUTER
2546 RTFLAG("r", RTF_ISAROUTER);
2547 #endif
2548 #ifdef RTF_TUNNEL
2549 RTFLAG("T", RTF_TUNNEL);
2550 #endif
2551 #ifdef RTF_AUTH
2552 RTFLAG("A", RTF_AUTH);
2553 #endif
2554 #ifdef RTF_CRYPT
2555 RTFLAG("E", RTF_CRYPT);
2556 #endif
2557 #undef RTFLAG
2558 return buf;
2559 }
2560
2561 static const char *
ifflags(int flags)2562 ifflags(int flags)
2563 {
2564 static char buf[BUFSIZ];
2565
2566 strlcpy(buf, "", sizeof(buf));
2567 #define IFFLAG(s, f) \
2568 do { \
2569 if (flags & (f)) { \
2570 if (buf[0]) \
2571 strlcat(buf, ",", sizeof(buf)); \
2572 strlcat(buf, (s), sizeof(buf)); \
2573 } \
2574 } while (0)
2575 IFFLAG("UP", IFF_UP);
2576 IFFLAG("BROADCAST", IFF_BROADCAST);
2577 IFFLAG("DEBUG", IFF_DEBUG);
2578 IFFLAG("LOOPBACK", IFF_LOOPBACK);
2579 IFFLAG("POINTOPOINT", IFF_POINTOPOINT);
2580 #ifdef IFF_NOTRAILERS
2581 IFFLAG("NOTRAILERS", IFF_NOTRAILERS);
2582 #endif
2583 IFFLAG("RUNNING", IFF_RUNNING);
2584 IFFLAG("NOARP", IFF_NOARP);
2585 IFFLAG("PROMISC", IFF_PROMISC);
2586 IFFLAG("ALLMULTI", IFF_ALLMULTI);
2587 IFFLAG("OACTIVE", IFF_OACTIVE);
2588 IFFLAG("SIMPLEX", IFF_SIMPLEX);
2589 IFFLAG("LINK0", IFF_LINK0);
2590 IFFLAG("LINK1", IFF_LINK1);
2591 IFFLAG("LINK2", IFF_LINK2);
2592 IFFLAG("MULTICAST", IFF_MULTICAST);
2593 #undef IFFLAG
2594 return buf;
2595 }
2596
2597 static void
krtread(int again)2598 krtread(int again)
2599 {
2600 int mib[6];
2601 size_t msize;
2602 char *buf, *p, *lim;
2603 struct rt_msghdr *rtm;
2604 int retry;
2605 const char *errmsg;
2606
2607 retry = 0;
2608 buf = NULL;
2609 mib[0] = CTL_NET;
2610 mib[1] = PF_ROUTE;
2611 mib[2] = 0;
2612 mib[3] = AF_INET6; /* Address family */
2613 mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
2614 mib[5] = 0; /* No flags */
2615 do {
2616 if (retry)
2617 sleep(1);
2618 retry++;
2619 errmsg = NULL;
2620 if (buf) {
2621 free(buf);
2622 buf = NULL;
2623 }
2624 if (sysctl(mib, nitems(mib), NULL, &msize, NULL, 0) < 0) {
2625 errmsg = "sysctl estimate";
2626 continue;
2627 }
2628 if ((buf = malloc(msize)) == NULL) {
2629 errmsg = "malloc";
2630 continue;
2631 }
2632 if (sysctl(mib, nitems(mib), buf, &msize, NULL, 0) < 0) {
2633 errmsg = "sysctl NET_RT_DUMP";
2634 continue;
2635 }
2636 } while (retry < RT_DUMP_MAXRETRY && errmsg != NULL);
2637 if (errmsg) {
2638 fatal("%s (with %d retries, msize=%lu)", errmsg, retry,
2639 (u_long)msize);
2640 /*NOTREACHED*/
2641 } else if (1 < retry)
2642 syslog(LOG_INFO, "NET_RT_DUMP %d retires", retry);
2643
2644 lim = buf + msize;
2645 for (p = buf; p < lim; p += rtm->rtm_msglen) {
2646 rtm = (struct rt_msghdr *)(void *)p;
2647 rt_entry(rtm, again);
2648 }
2649 free(buf);
2650 }
2651
2652 static void
rt_entry(struct rt_msghdr * rtm,int again)2653 rt_entry(struct rt_msghdr *rtm, int again)
2654 {
2655 struct sockaddr_in6 *sin6_dst, *sin6_gw, *sin6_mask;
2656 struct sockaddr_in6 *sin6_genmask, *sin6_ifp;
2657 char *rtmp, *ifname = NULL;
2658 struct riprt *rrt, *orrt;
2659 struct netinfo6 *np;
2660 int ifindex;
2661
2662 sin6_dst = sin6_gw = sin6_mask = sin6_genmask = sin6_ifp = 0;
2663 if ((rtm->rtm_flags & RTF_UP) == 0 || rtm->rtm_flags &
2664 (RTF_XRESOLVE|RTF_BLACKHOLE)) {
2665 return; /* not interested in the link route */
2666 }
2667 /* do not look at cloned routes */
2668 #ifdef RTF_WASCLONED
2669 if (rtm->rtm_flags & RTF_WASCLONED)
2670 return;
2671 #endif
2672 #ifdef RTF_CLONED
2673 if (rtm->rtm_flags & RTF_CLONED)
2674 return;
2675 #endif
2676 /* XXX: Ignore connected routes. */
2677 if (!(rtm->rtm_flags & (RTF_GATEWAY|RTF_HOST|RTF_STATIC)))
2678 return;
2679 /*
2680 * do not look at dynamic routes.
2681 * netbsd/openbsd cloned routes have UGHD.
2682 */
2683 if (rtm->rtm_flags & RTF_DYNAMIC)
2684 return;
2685 rtmp = (char *)(rtm + 1);
2686 /* Destination */
2687 if ((rtm->rtm_addrs & RTA_DST) == 0)
2688 return; /* ignore routes without destination address */
2689 sin6_dst = (struct sockaddr_in6 *)(void *)rtmp;
2690 rtmp += ROUNDUP(sin6_dst->sin6_len);
2691 if (rtm->rtm_addrs & RTA_GATEWAY) {
2692 sin6_gw = (struct sockaddr_in6 *)(void *)rtmp;
2693 rtmp += ROUNDUP(sin6_gw->sin6_len);
2694 }
2695 if (rtm->rtm_addrs & RTA_NETMASK) {
2696 sin6_mask = (struct sockaddr_in6 *)(void *)rtmp;
2697 rtmp += ROUNDUP(sin6_mask->sin6_len);
2698 }
2699 if (rtm->rtm_addrs & RTA_GENMASK) {
2700 sin6_genmask = (struct sockaddr_in6 *)(void *)rtmp;
2701 rtmp += ROUNDUP(sin6_genmask->sin6_len);
2702 }
2703 if (rtm->rtm_addrs & RTA_IFP) {
2704 sin6_ifp = (struct sockaddr_in6 *)(void *)rtmp;
2705 rtmp += ROUNDUP(sin6_ifp->sin6_len);
2706 }
2707
2708 /* Destination */
2709 if (sin6_dst->sin6_family != AF_INET6)
2710 return;
2711 if (IN6_IS_ADDR_LINKLOCAL(&sin6_dst->sin6_addr))
2712 return; /* Link-local */
2713 if (IN6_ARE_ADDR_EQUAL(&sin6_dst->sin6_addr, &in6addr_loopback))
2714 return; /* Loopback */
2715 if (IN6_IS_ADDR_MULTICAST(&sin6_dst->sin6_addr))
2716 return;
2717
2718 if ((rrt = MALLOC(struct riprt)) == NULL) {
2719 fatal("malloc: struct riprt");
2720 /*NOTREACHED*/
2721 }
2722 memset(rrt, 0, sizeof(*rrt));
2723 np = &rrt->rrt_info;
2724 rrt->rrt_same = NULL;
2725 rrt->rrt_t = time(NULL);
2726 if (aflag == 0 && (rtm->rtm_flags & RTF_STATIC))
2727 rrt->rrt_t = 0; /* Don't age static routes */
2728 if (rtm->rtm_flags & Pflag)
2729 rrt->rrt_t = 0; /* Don't age PROTO[123] routes */
2730 if ((rtm->rtm_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST)
2731 rrt->rrt_t = 0; /* Don't age non-gateway host routes */
2732 np->rip6_tag = 0;
2733 np->rip6_metric = rtm->rtm_rmx.rmx_hopcount;
2734 if (np->rip6_metric < 1)
2735 np->rip6_metric = 1;
2736 rrt->rrt_flags = rtm->rtm_flags;
2737 np->rip6_dest = sin6_dst->sin6_addr;
2738
2739 /* Mask or plen */
2740 if (rtm->rtm_flags & RTF_HOST)
2741 np->rip6_plen = 128; /* Host route */
2742 else if (sin6_mask)
2743 np->rip6_plen = sin6mask2len(sin6_mask);
2744 else
2745 np->rip6_plen = 0;
2746
2747 orrt = rtsearch(np);
2748 if (orrt && orrt->rrt_info.rip6_metric != HOPCNT_INFINITY6) {
2749 /* Already found */
2750 if (!again) {
2751 trace(1, "route: %s/%d flags %s: already registered\n",
2752 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2753 rtflags(rtm));
2754 }
2755 free(rrt);
2756 return;
2757 }
2758 /* Gateway */
2759 if (!sin6_gw)
2760 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2761 else {
2762 if (sin6_gw->sin6_family == AF_INET6)
2763 rrt->rrt_gw = sin6_gw->sin6_addr;
2764 else if (sin6_gw->sin6_family == AF_LINK) {
2765 /* XXX in case ppp link? */
2766 rrt->rrt_gw = in6addr_loopback;
2767 } else
2768 memset(&rrt->rrt_gw, 0, sizeof(struct in6_addr));
2769 }
2770 trace(1, "route: %s/%d flags %s",
2771 inet6_n2p(&np->rip6_dest), np->rip6_plen, rtflags(rtm));
2772 trace(1, " gw %s", inet6_n2p(&rrt->rrt_gw));
2773
2774 /* Interface */
2775 ifindex = rtm->rtm_index;
2776 if ((unsigned int)ifindex < nindex2ifc && index2ifc[ifindex])
2777 ifname = index2ifc[ifindex]->ifc_name;
2778 else {
2779 trace(1, " not configured\n");
2780 free(rrt);
2781 return;
2782 }
2783 trace(1, " if %s sock %d", ifname, ifindex);
2784 rrt->rrt_index = ifindex;
2785
2786 trace(1, "\n");
2787
2788 /* Check gateway */
2789 if (!IN6_IS_ADDR_LINKLOCAL(&rrt->rrt_gw) &&
2790 !IN6_IS_ADDR_LOOPBACK(&rrt->rrt_gw) &&
2791 (rrt->rrt_flags & RTF_LOCAL) == 0) {
2792 trace(0, "***** Gateway %s is not a link-local address.\n",
2793 inet6_n2p(&rrt->rrt_gw));
2794 trace(0, "***** dest(%s) if(%s) -- Not optimized.\n",
2795 inet6_n2p(&rrt->rrt_info.rip6_dest), ifname);
2796 rrt->rrt_rflags |= RRTF_NH_NOT_LLADDR;
2797 }
2798
2799 /* Put it to the route list */
2800 if (orrt && orrt->rrt_info.rip6_metric == HOPCNT_INFINITY6) {
2801 /* replace route list */
2802 TAILQ_INSERT_BEFORE(orrt, rrt, rrt_next);
2803 TAILQ_REMOVE(&riprt_head, orrt, rrt_next);
2804
2805 trace(1, "route: %s/%d flags %s: replace new route\n",
2806 inet6_n2p(&np->rip6_dest), np->rip6_plen,
2807 rtflags(rtm));
2808 free(orrt);
2809 } else
2810 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
2811 }
2812
2813 static int
addroute(struct riprt * rrt,const struct in6_addr * gw,struct ifc * ifcp)2814 addroute(struct riprt *rrt,
2815 const struct in6_addr *gw,
2816 struct ifc *ifcp)
2817 {
2818 struct netinfo6 *np;
2819 u_char buf[BUFSIZ], buf1[BUFSIZ], buf2[BUFSIZ];
2820 struct rt_msghdr *rtm;
2821 struct sockaddr_in6 *sin6;
2822 int len;
2823
2824 np = &rrt->rrt_info;
2825 inet_ntop(AF_INET6, (const void *)gw, (char *)buf1, sizeof(buf1));
2826 inet_ntop(AF_INET6, (void *)&ifcp->ifc_mylladdr, (char *)buf2, sizeof(buf2));
2827 tracet(1, "ADD: %s/%d gw %s [%d] ifa %s\n",
2828 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2829 np->rip6_metric - 1, buf2);
2830 if (rtlog)
2831 fprintf(rtlog, "%s: ADD: %s/%d gw %s [%d] ifa %s\n", hms(),
2832 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1,
2833 np->rip6_metric - 1, buf2);
2834 if (nflag)
2835 return 0;
2836
2837 memset(buf, 0, sizeof(buf));
2838 rtm = (struct rt_msghdr *)(void *)buf;
2839 rtm->rtm_type = RTM_ADD;
2840 rtm->rtm_version = RTM_VERSION;
2841 rtm->rtm_seq = ++seq;
2842 rtm->rtm_pid = pid;
2843 rtm->rtm_flags = rrt->rrt_flags;
2844 rtm->rtm_flags |= Qflag;
2845 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2846 rtm->rtm_rmx.rmx_hopcount = np->rip6_metric - 1;
2847 rtm->rtm_inits = RTV_HOPCOUNT;
2848 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2849 /* Destination */
2850 sin6->sin6_len = sizeof(struct sockaddr_in6);
2851 sin6->sin6_family = AF_INET6;
2852 sin6->sin6_addr = np->rip6_dest;
2853 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2854 /* Gateway */
2855 sin6->sin6_len = sizeof(struct sockaddr_in6);
2856 sin6->sin6_family = AF_INET6;
2857 sin6->sin6_addr = *gw;
2858 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2859 sin6->sin6_scope_id = ifcp->ifc_index;
2860 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2861 /* Netmask */
2862 sin6->sin6_len = sizeof(struct sockaddr_in6);
2863 sin6->sin6_family = AF_INET6;
2864 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2865 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2866
2867 len = (char *)sin6 - (char *)buf;
2868 rtm->rtm_msglen = len;
2869 if (write(rtsock, buf, len) > 0)
2870 return 0;
2871
2872 if (errno == EEXIST) {
2873 trace(0, "ADD: Route already exists %s/%d gw %s\n",
2874 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2875 if (rtlog)
2876 fprintf(rtlog, "ADD: Route already exists %s/%d gw %s\n",
2877 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf1);
2878 } else {
2879 trace(0, "Can not write to rtsock (addroute): %s\n",
2880 strerror(errno));
2881 if (rtlog)
2882 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2883 strerror(errno));
2884 }
2885 return -1;
2886 }
2887
2888 static int
delroute(struct netinfo6 * np,struct in6_addr * gw)2889 delroute(struct netinfo6 *np, struct in6_addr *gw)
2890 {
2891 u_char buf[BUFSIZ], buf2[BUFSIZ];
2892 struct rt_msghdr *rtm;
2893 struct sockaddr_in6 *sin6;
2894 int len;
2895
2896 inet_ntop(AF_INET6, (void *)gw, (char *)buf2, sizeof(buf2));
2897 tracet(1, "DEL: %s/%d gw %s\n", inet6_n2p(&np->rip6_dest),
2898 np->rip6_plen, buf2);
2899 if (rtlog)
2900 fprintf(rtlog, "%s: DEL: %s/%d gw %s\n",
2901 hms(), inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2902 if (nflag)
2903 return 0;
2904
2905 memset(buf, 0, sizeof(buf));
2906 rtm = (struct rt_msghdr *)(void *)buf;
2907 rtm->rtm_type = RTM_DELETE;
2908 rtm->rtm_version = RTM_VERSION;
2909 rtm->rtm_seq = ++seq;
2910 rtm->rtm_pid = pid;
2911 rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
2912 rtm->rtm_flags |= Qflag;
2913 if (np->rip6_plen == sizeof(struct in6_addr) * 8)
2914 rtm->rtm_flags |= RTF_HOST;
2915 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2916 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2917 /* Destination */
2918 sin6->sin6_len = sizeof(struct sockaddr_in6);
2919 sin6->sin6_family = AF_INET6;
2920 sin6->sin6_addr = np->rip6_dest;
2921 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2922 /* Gateway */
2923 sin6->sin6_len = sizeof(struct sockaddr_in6);
2924 sin6->sin6_family = AF_INET6;
2925 sin6->sin6_addr = *gw;
2926 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2927 /* Netmask */
2928 sin6->sin6_len = sizeof(struct sockaddr_in6);
2929 sin6->sin6_family = AF_INET6;
2930 sin6->sin6_addr = *(plen2mask(np->rip6_plen));
2931 sin6 = (struct sockaddr_in6 *)(void *)((char *)sin6 + ROUNDUP(sin6->sin6_len));
2932
2933 len = (char *)sin6 - (char *)buf;
2934 rtm->rtm_msglen = len;
2935 if (write(rtsock, buf, len) >= 0)
2936 return 0;
2937
2938 if (errno == ESRCH) {
2939 trace(0, "RTDEL: Route does not exist: %s/%d gw %s\n",
2940 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2941 if (rtlog)
2942 fprintf(rtlog, "RTDEL: Route does not exist: %s/%d gw %s\n",
2943 inet6_n2p(&np->rip6_dest), np->rip6_plen, buf2);
2944 } else {
2945 trace(0, "Can not write to rtsock (delroute): %s\n",
2946 strerror(errno));
2947 if (rtlog)
2948 fprintf(rtlog, "\tCan not write to rtsock: %s\n",
2949 strerror(errno));
2950 }
2951 return -1;
2952 }
2953
2954 #if 0
2955 static struct in6_addr *
2956 getroute(struct netinfo6 *np, struct in6_addr *gw)
2957 {
2958 u_char buf[BUFSIZ];
2959 int myseq;
2960 int len;
2961 struct rt_msghdr *rtm;
2962 struct sockaddr_in6 *sin6;
2963
2964 rtm = (struct rt_msghdr *)(void *)buf;
2965 len = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in6);
2966 memset(rtm, 0, len);
2967 rtm->rtm_type = RTM_GET;
2968 rtm->rtm_version = RTM_VERSION;
2969 myseq = ++seq;
2970 rtm->rtm_seq = myseq;
2971 rtm->rtm_addrs = RTA_DST;
2972 rtm->rtm_msglen = len;
2973 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2974 sin6->sin6_len = sizeof(struct sockaddr_in6);
2975 sin6->sin6_family = AF_INET6;
2976 sin6->sin6_addr = np->rip6_dest;
2977 if (write(rtsock, buf, len) < 0) {
2978 if (errno == ESRCH) /* No such route found */
2979 return NULL;
2980 perror("write to rtsock");
2981 exit(1);
2982 }
2983 do {
2984 if ((len = read(rtsock, buf, sizeof(buf))) < 0) {
2985 perror("read from rtsock");
2986 exit(1);
2987 }
2988 rtm = (struct rt_msghdr *)(void *)buf;
2989 } while (rtm->rtm_type != RTM_GET || rtm->rtm_seq != myseq ||
2990 rtm->rtm_pid != pid);
2991 sin6 = (struct sockaddr_in6 *)(void *)&buf[sizeof(struct rt_msghdr)];
2992 if (rtm->rtm_addrs & RTA_DST) {
2993 sin6 = (struct sockaddr_in6 *)(void *)
2994 ((char *)sin6 + ROUNDUP(sin6->sin6_len));
2995 }
2996 if (rtm->rtm_addrs & RTA_GATEWAY) {
2997 *gw = sin6->sin6_addr;
2998 return gw;
2999 }
3000 return NULL;
3001 }
3002 #endif
3003
3004 static const char *
inet6_n2p(const struct in6_addr * p)3005 inet6_n2p(const struct in6_addr *p)
3006 {
3007 static char buf[BUFSIZ];
3008
3009 return inet_ntop(AF_INET6, (const void *)p, buf, sizeof(buf));
3010 }
3011
3012 static void
ifrtdump(int sig)3013 ifrtdump(int sig)
3014 {
3015
3016 ifdump(sig);
3017 rtdump(sig);
3018 }
3019
3020 static void
ifdump(int sig)3021 ifdump(int sig)
3022 {
3023 struct ifc *ifcp;
3024 FILE *dump;
3025 int nifc = 0;
3026
3027 if (sig == 0)
3028 dump = stderr;
3029 else
3030 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3031 dump = stderr;
3032
3033 fprintf(dump, "%s: Interface Table Dump\n", hms());
3034 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next)
3035 nifc++;
3036 fprintf(dump, " Number of interfaces: %d\n", nifc);
3037
3038 fprintf(dump, " advertising interfaces:\n");
3039 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3040 if ((ifcp->ifc_flags & IFF_UP) == 0)
3041 continue;
3042 if (iff_find(ifcp, IFIL_TYPE_N) != NULL)
3043 continue;
3044 ifdump0(dump, ifcp);
3045 }
3046 fprintf(dump, "\n");
3047 fprintf(dump, " non-advertising interfaces:\n");
3048 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3049 if ((ifcp->ifc_flags & IFF_UP) &&
3050 (iff_find(ifcp, IFIL_TYPE_N) == NULL))
3051 continue;
3052 ifdump0(dump, ifcp);
3053 }
3054 fprintf(dump, "\n");
3055 if (dump != stderr)
3056 fclose(dump);
3057 }
3058
3059 static void
ifdump0(FILE * dump,const struct ifc * ifcp)3060 ifdump0(FILE *dump, const struct ifc *ifcp)
3061 {
3062 struct ifac *ifac;
3063 struct iff *iffp;
3064 char buf[BUFSIZ];
3065 const char *ft;
3066 int addr;
3067
3068 fprintf(dump, " %s: index(%d) flags(%s) addr(%s) mtu(%d) metric(%d)\n",
3069 ifcp->ifc_name, ifcp->ifc_index, ifflags(ifcp->ifc_flags),
3070 inet6_n2p(&ifcp->ifc_mylladdr),
3071 ifcp->ifc_mtu, ifcp->ifc_metric);
3072 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3073 if (ifcp->ifc_flags & IFF_POINTOPOINT) {
3074 inet_ntop(AF_INET6, (void *)&ifac->ifac_raddr,
3075 buf, sizeof(buf));
3076 fprintf(dump, "\t%s/%d -- %s\n",
3077 inet6_n2p(&ifac->ifac_addr),
3078 ifac->ifac_plen, buf);
3079 } else {
3080 fprintf(dump, "\t%s/%d\n",
3081 inet6_n2p(&ifac->ifac_addr),
3082 ifac->ifac_plen);
3083 }
3084 }
3085
3086 fprintf(dump, "\tFilter:\n");
3087 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3088 addr = 0;
3089 switch (iffp->iff_type) {
3090 case IFIL_TYPE_A:
3091 ft = "Aggregate"; addr++; break;
3092 case IFIL_TYPE_N:
3093 ft = "No-use"; break;
3094 case IFIL_TYPE_O:
3095 ft = "Advertise-only"; addr++; break;
3096 case IFIL_TYPE_T:
3097 ft = "Default-only"; break;
3098 case IFIL_TYPE_L:
3099 ft = "Listen-only"; addr++; break;
3100 default:
3101 snprintf(buf, sizeof(buf), "Unknown-%c", iffp->iff_type);
3102 ft = buf;
3103 addr++;
3104 break;
3105 }
3106 fprintf(dump, "\t\t%s", ft);
3107 if (addr)
3108 fprintf(dump, "(%s/%d)", inet6_n2p(&iffp->iff_addr),
3109 iffp->iff_plen);
3110 fprintf(dump, "\n");
3111 }
3112 fprintf(dump, "\n");
3113 }
3114
3115 static void
rtdump(int sig)3116 rtdump(int sig)
3117 {
3118 struct riprt *rrt;
3119 char buf[BUFSIZ];
3120 FILE *dump;
3121 time_t t, age;
3122
3123 if (sig == 0)
3124 dump = stderr;
3125 else
3126 if ((dump = fopen(ROUTE6D_DUMP, "a")) == NULL)
3127 dump = stderr;
3128
3129 t = time(NULL);
3130 fprintf(dump, "\n%s: Routing Table Dump\n", hms());
3131 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3132 if (rrt->rrt_t == 0)
3133 age = 0;
3134 else
3135 age = t - rrt->rrt_t;
3136 inet_ntop(AF_INET6, (void *)&rrt->rrt_info.rip6_dest,
3137 buf, sizeof(buf));
3138 fprintf(dump, " %s/%d if(%d:%s) gw(%s) [%d] age(%ld)",
3139 buf, rrt->rrt_info.rip6_plen, rrt->rrt_index,
3140 index2ifc[rrt->rrt_index]->ifc_name,
3141 inet6_n2p(&rrt->rrt_gw),
3142 rrt->rrt_info.rip6_metric, (long)age);
3143 if (rrt->rrt_info.rip6_tag) {
3144 fprintf(dump, " tag(0x%04x)",
3145 ntohs(rrt->rrt_info.rip6_tag) & 0xffff);
3146 }
3147 if (rrt->rrt_rflags & RRTF_NH_NOT_LLADDR)
3148 fprintf(dump, " NOT-LL");
3149 if (rrt->rrt_rflags & RRTF_NOADVERTISE)
3150 fprintf(dump, " NO-ADV");
3151 fprintf(dump, "\n");
3152 }
3153 fprintf(dump, "\n");
3154 if (dump != stderr)
3155 fclose(dump);
3156 }
3157
3158 /*
3159 * Parse the -A (and -O) options and put corresponding filter object to the
3160 * specified interface structures. Each of the -A/O option has the following
3161 * syntax: -A 5f09:c400::/32,ef0,ef1 (aggregate)
3162 * -O 5f09:c400::/32,ef0,ef1 (only when match)
3163 */
3164 static void
filterconfig(void)3165 filterconfig(void)
3166 {
3167 int i;
3168 char *p, *ap, *iflp, *ifname, *ep;
3169 struct iff iff, *iffp;
3170 struct ifc *ifcp;
3171 struct riprt *rrt;
3172 #if 0
3173 struct in6_addr gw;
3174 #endif
3175 u_long plen;
3176
3177 for (i = 0; i < nfilter; i++) {
3178 ap = filter[i];
3179 iflp = NULL;
3180 iffp = ⇔
3181 memset(iffp, 0, sizeof(*iffp));
3182 if (filtertype[i] == 'N' || filtertype[i] == 'T') {
3183 iflp = ap;
3184 goto ifonly;
3185 }
3186 if ((p = strchr(ap, ',')) != NULL) {
3187 *p++ = '\0';
3188 iflp = p;
3189 }
3190 if ((p = strchr(ap, '/')) == NULL) {
3191 fatal("no prefixlen specified for '%s'", ap);
3192 /*NOTREACHED*/
3193 }
3194 *p++ = '\0';
3195 if (inet_pton(AF_INET6, ap, &iffp->iff_addr) != 1) {
3196 fatal("invalid prefix specified for '%s'", ap);
3197 /*NOTREACHED*/
3198 }
3199 errno = 0;
3200 ep = NULL;
3201 plen = strtoul(p, &ep, 10);
3202 if (errno || !*p || *ep || plen > sizeof(iffp->iff_addr) * 8) {
3203 fatal("invalid prefix length specified for '%s'", ap);
3204 /*NOTREACHED*/
3205 }
3206 iffp->iff_plen = plen;
3207 applyplen(&iffp->iff_addr, iffp->iff_plen);
3208 ifonly:
3209 iffp->iff_type = filtertype[i];
3210 if (iflp == NULL || *iflp == '\0') {
3211 fatal("no interface specified for '%s'", ap);
3212 /*NOTREACHED*/
3213 }
3214 /* parse the interface listing portion */
3215 while (iflp) {
3216 ifname = iflp;
3217 if ((iflp = strchr(iflp, ',')) != NULL)
3218 *iflp++ = '\0';
3219
3220 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3221 if (fnmatch(ifname, ifcp->ifc_name, 0) != 0)
3222 continue;
3223
3224 iffp = malloc(sizeof(*iffp));
3225 if (iffp == NULL) {
3226 fatal("malloc of iff");
3227 /*NOTREACHED*/
3228 }
3229 memcpy(iffp, &iff, sizeof(*iffp));
3230 #if 0
3231 syslog(LOG_INFO, "Add filter: type %d, ifname %s.", iffp->iff_type, ifname);
3232 #endif
3233 TAILQ_INSERT_HEAD(&ifcp->ifc_iff_head, iffp, iff_next);
3234 }
3235 }
3236
3237 /*
3238 * -A: aggregate configuration.
3239 */
3240 if (filtertype[i] != IFIL_TYPE_A)
3241 continue;
3242 /* put the aggregate to the kernel routing table */
3243 rrt = (struct riprt *)malloc(sizeof(struct riprt));
3244 if (rrt == NULL) {
3245 fatal("malloc: rrt");
3246 /*NOTREACHED*/
3247 }
3248 memset(rrt, 0, sizeof(struct riprt));
3249 rrt->rrt_info.rip6_dest = iff.iff_addr;
3250 rrt->rrt_info.rip6_plen = iff.iff_plen;
3251 rrt->rrt_info.rip6_metric = 1;
3252 rrt->rrt_info.rip6_tag = htons(routetag & 0xffff);
3253 rrt->rrt_gw = in6addr_loopback;
3254 rrt->rrt_flags = RTF_UP | RTF_REJECT;
3255 rrt->rrt_rflags = RRTF_AGGREGATE;
3256 rrt->rrt_t = 0;
3257 rrt->rrt_index = loopifcp->ifc_index;
3258 #if 0
3259 if (getroute(&rrt->rrt_info, &gw)) {
3260 #if 0
3261 /*
3262 * When the address has already been registered in the
3263 * kernel routing table, it should be removed
3264 */
3265 delroute(&rrt->rrt_info, &gw);
3266 #else
3267 /* it is safer behavior */
3268 errno = EINVAL;
3269 fatal("%s/%u already in routing table, "
3270 "cannot aggregate",
3271 inet6_n2p(&rrt->rrt_info.rip6_dest),
3272 rrt->rrt_info.rip6_plen);
3273 /*NOTREACHED*/
3274 #endif
3275 }
3276 #endif
3277 /* Put the route to the list */
3278 TAILQ_INSERT_HEAD(&riprt_head, rrt, rrt_next);
3279 trace(1, "Aggregate: %s/%d for %s\n",
3280 inet6_n2p(&iff.iff_addr), iff.iff_plen,
3281 loopifcp->ifc_name);
3282 /* Add this route to the kernel */
3283 if (nflag) /* do not modify kernel routing table */
3284 continue;
3285 addroute(rrt, &in6addr_loopback, loopifcp);
3286 }
3287 }
3288
3289 /***************** utility functions *****************/
3290
3291 /*
3292 * Returns a pointer to ifac whose address and prefix length matches
3293 * with the address and prefix length specified in the arguments.
3294 */
3295 static struct ifac *
ifa_match(const struct ifc * ifcp,const struct in6_addr * ia,int plen)3296 ifa_match(const struct ifc *ifcp,
3297 const struct in6_addr *ia,
3298 int plen)
3299 {
3300 struct ifac *ifac;
3301
3302 TAILQ_FOREACH(ifac, &ifcp->ifc_ifac_head, ifac_next) {
3303 if (IN6_ARE_ADDR_EQUAL(&ifac->ifac_addr, ia) &&
3304 ifac->ifac_plen == plen)
3305 break;
3306 }
3307
3308 return (ifac);
3309 }
3310
3311 /*
3312 * Return a pointer to riprt structure whose address and prefix length
3313 * matches with the address and prefix length found in the argument.
3314 * Note: This is not a rtalloc(). Therefore exact match is necessary.
3315 */
3316 static struct riprt *
rtsearch(struct netinfo6 * np)3317 rtsearch(struct netinfo6 *np)
3318 {
3319 struct riprt *rrt;
3320
3321 TAILQ_FOREACH(rrt, &riprt_head, rrt_next) {
3322 if (rrt->rrt_info.rip6_plen == np->rip6_plen &&
3323 IN6_ARE_ADDR_EQUAL(&rrt->rrt_info.rip6_dest,
3324 &np->rip6_dest))
3325 break;
3326 }
3327
3328 return (rrt);
3329 }
3330
3331 static int
sin6mask2len(const struct sockaddr_in6 * sin6)3332 sin6mask2len(const struct sockaddr_in6 *sin6)
3333 {
3334
3335 return mask2len(&sin6->sin6_addr,
3336 sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr));
3337 }
3338
3339 static int
mask2len(const struct in6_addr * addr,int lenlim)3340 mask2len(const struct in6_addr *addr, int lenlim)
3341 {
3342 int i = 0, j;
3343 const u_char *p = (const u_char *)addr;
3344
3345 for (j = 0; j < lenlim; j++, p++) {
3346 if (*p != 0xff)
3347 break;
3348 i += 8;
3349 }
3350 if (j < lenlim) {
3351 switch (*p) {
3352 #define MASKLEN(m, l) case m: do { i += l; break; } while (0)
3353 MASKLEN(0xfe, 7); break;
3354 MASKLEN(0xfc, 6); break;
3355 MASKLEN(0xf8, 5); break;
3356 MASKLEN(0xf0, 4); break;
3357 MASKLEN(0xe0, 3); break;
3358 MASKLEN(0xc0, 2); break;
3359 MASKLEN(0x80, 1); break;
3360 #undef MASKLEN
3361 }
3362 }
3363 return i;
3364 }
3365
3366 static const u_char plent[8] = {
3367 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
3368 };
3369
3370 static void
applyplen(struct in6_addr * ia,int plen)3371 applyplen(struct in6_addr *ia, int plen)
3372 {
3373 u_char *p;
3374 int i;
3375
3376 p = ia->s6_addr;
3377 for (i = 0; i < 16; i++) {
3378 if (plen <= 0)
3379 *p = 0;
3380 else if (plen < 8)
3381 *p &= plent[plen];
3382 p++, plen -= 8;
3383 }
3384 }
3385
3386 static const int pl2m[9] = {
3387 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
3388 };
3389
3390 static struct in6_addr *
plen2mask(int n)3391 plen2mask(int n)
3392 {
3393 static struct in6_addr ia;
3394 u_char *p;
3395 int i;
3396
3397 memset(&ia, 0, sizeof(struct in6_addr));
3398 p = (u_char *)&ia;
3399 for (i = 0; i < 16; i++, p++, n -= 8) {
3400 if (n >= 8) {
3401 *p = 0xff;
3402 continue;
3403 }
3404 *p = pl2m[n];
3405 break;
3406 }
3407 return &ia;
3408 }
3409
3410 static char *
allocopy(char * p)3411 allocopy(char *p)
3412 {
3413 int len = strlen(p) + 1;
3414 char *q = (char *)malloc(len);
3415
3416 if (!q) {
3417 fatal("malloc");
3418 /*NOTREACHED*/
3419 }
3420
3421 strlcpy(q, p, len);
3422 return q;
3423 }
3424
3425 static char *
hms(void)3426 hms(void)
3427 {
3428 static char buf[BUFSIZ];
3429 time_t t;
3430 struct tm *tm;
3431
3432 t = time(NULL);
3433 if ((tm = localtime(&t)) == 0) {
3434 fatal("localtime");
3435 /*NOTREACHED*/
3436 }
3437 snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
3438 tm->tm_sec);
3439 return buf;
3440 }
3441
3442 #define RIPRANDDEV 1.0 /* 30 +- 15, max - min = 30 */
3443
3444 static int
ripinterval(int timer)3445 ripinterval(int timer)
3446 {
3447 double r = rand();
3448
3449 interval = (int)(timer + timer * RIPRANDDEV * (r / RAND_MAX - 0.5));
3450 nextalarm = time(NULL) + interval;
3451 return interval;
3452 }
3453
3454 #if 0
3455 static time_t
3456 ripsuptrig(void)
3457 {
3458 time_t t;
3459
3460 double r = rand();
3461 t = (int)(RIP_TRIG_INT6_MIN +
3462 (RIP_TRIG_INT6_MAX - RIP_TRIG_INT6_MIN) * (r / RAND_MAX));
3463 sup_trig_update = time(NULL) + t;
3464 return t;
3465 }
3466 #endif
3467
3468 static void
fatal(const char * fmt,...)3469 fatal(const char *fmt, ...)
3470 {
3471 va_list ap;
3472 char buf[1024];
3473
3474 va_start(ap, fmt);
3475 vsnprintf(buf, sizeof(buf), fmt, ap);
3476 va_end(ap);
3477 perror(buf);
3478 if (errno)
3479 syslog(LOG_ERR, "%s: %s", buf, strerror(errno));
3480 else
3481 syslog(LOG_ERR, "%s", buf);
3482 rtdexit();
3483 }
3484
3485 static void
tracet(int level,const char * fmt,...)3486 tracet(int level, const char *fmt, ...)
3487 {
3488 va_list ap;
3489
3490 if (level <= dflag) {
3491 va_start(ap, fmt);
3492 fprintf(stderr, "%s: ", hms());
3493 vfprintf(stderr, fmt, ap);
3494 va_end(ap);
3495 }
3496 if (dflag) {
3497 va_start(ap, fmt);
3498 if (level > 0)
3499 vsyslog(LOG_DEBUG, fmt, ap);
3500 else
3501 vsyslog(LOG_WARNING, fmt, ap);
3502 va_end(ap);
3503 }
3504 }
3505
3506 static void
trace(int level,const char * fmt,...)3507 trace(int level, const char *fmt, ...)
3508 {
3509 va_list ap;
3510
3511 if (level <= dflag) {
3512 va_start(ap, fmt);
3513 vfprintf(stderr, fmt, ap);
3514 va_end(ap);
3515 }
3516 if (dflag) {
3517 va_start(ap, fmt);
3518 if (level > 0)
3519 vsyslog(LOG_DEBUG, fmt, ap);
3520 else
3521 vsyslog(LOG_WARNING, fmt, ap);
3522 va_end(ap);
3523 }
3524 }
3525
3526 static struct ifc *
ifc_find(char * name)3527 ifc_find(char *name)
3528 {
3529 struct ifc *ifcp;
3530
3531 TAILQ_FOREACH(ifcp, &ifc_head, ifc_next) {
3532 if (strcmp(name, ifcp->ifc_name) == 0)
3533 break;
3534 }
3535 return (ifcp);
3536 }
3537
3538 static struct iff *
iff_find(struct ifc * ifcp,int type)3539 iff_find(struct ifc *ifcp, int type)
3540 {
3541 struct iff *iffp;
3542
3543 TAILQ_FOREACH(iffp, &ifcp->ifc_iff_head, iff_next) {
3544 if (type == IFIL_TYPE_ANY ||
3545 type == iffp->iff_type)
3546 break;
3547 }
3548
3549 return (iffp);
3550 }
3551
3552 static void
setindex2ifc(int idx,struct ifc * ifcp)3553 setindex2ifc(int idx, struct ifc *ifcp)
3554 {
3555 int n, nsize;
3556 struct ifc **p;
3557
3558 if (!index2ifc) {
3559 nindex2ifc = 5; /*initial guess*/
3560 index2ifc = (struct ifc **)
3561 malloc(sizeof(*index2ifc) * nindex2ifc);
3562 if (index2ifc == NULL) {
3563 fatal("malloc");
3564 /*NOTREACHED*/
3565 }
3566 memset(index2ifc, 0, sizeof(*index2ifc) * nindex2ifc);
3567 }
3568 n = nindex2ifc;
3569 for (nsize = nindex2ifc; nsize <= idx; nsize *= 2)
3570 ;
3571 if (n != nsize) {
3572 p = (struct ifc **)realloc(index2ifc,
3573 sizeof(*index2ifc) * nsize);
3574 if (p == NULL) {
3575 fatal("realloc");
3576 /*NOTREACHED*/
3577 }
3578 memset(p + n, 0, sizeof(*index2ifc) * (nindex2ifc - n));
3579 index2ifc = p;
3580 nindex2ifc = nsize;
3581 }
3582 index2ifc[idx] = ifcp;
3583 }
3584