Bug Summary

File:src/usr.sbin/route6d/route6d.c
Warning:line 2944, column 27
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

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