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