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