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