]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/icmp6.c
1291a6ea653449b1ccb640cfd8f8914488226b1f
[apple/xnu.git] / bsd / netinet6 / icmp6.c
1 /* $KAME: icmp6.c,v 1.77 2000/04/13 11:31:39 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
65 */
66
67 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
68 #include "opt_inet.h"
69 #ifdef __NetBSD__ /*XXX*/
70 #include "opt_ipsec.h"
71 #endif
72 #endif
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/time.h>
82 #include <sys/kernel.h>
83 #include <sys/syslog.h>
84 #include <sys/domain.h>
85
86 #include <net/if.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
89 #include <net/if_types.h>
90
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #if defined(__OpenBSD__) || (defined(__bsdi__) && _BSDI_VERSION >= 199802)
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #endif
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/mld6_var.h>
101 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__OpenBSD__) && !(defined(__bsdi__) && _BSDI_VERSION >= 199802) && !defined(__APPLE__)
102 #include <netinet6/in6_pcb.h>
103 #else
104 #include <netinet/in_pcb.h>
105 #endif
106 #include <netinet6/nd6.h>
107 #include <netinet6/in6_ifattach.h>
108 #include <netinet6/ip6protosw.h>
109
110 #ifdef __OpenBSD__ /*KAME IPSEC*/
111 #undef IPSEC
112 #endif
113
114 #if IPSEC
115 #include <netinet6/ipsec.h>
116 #include <netkey/key.h>
117 #include <netkey/key_debug.h>
118 #endif
119
120 #include "faith.h"
121
122 #include <net/net_osdep.h>
123
124 extern struct domain inet6domain;
125 extern struct ip6protosw inet6sw[];
126 extern struct ip6protosw *ip6_protox[];
127
128 struct icmp6stat icmp6stat;
129
130 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
131 extern struct in6pcb rawin6pcb;
132 #else
133 extern struct inpcbhead ripcb;
134 #endif
135 extern u_int icmp6errratelim;
136 extern int icmp6_nodeinfo;
137 #if defined(__NetBSD__) || defined(__OpenBSD__)
138 static struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL;
139 extern int pmtu_expire;
140 #endif
141
142 #ifndef HAVE_NRL_INPCB
143 static int icmp6_rip6_input __P((struct mbuf **, int));
144 #endif
145 static void icmp6_mtudisc_update __P((struct in6_addr *, struct icmp6_hdr *,
146 struct mbuf *));
147 static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
148 static const char *icmp6_redirect_diag __P((struct in6_addr *,
149 struct in6_addr *, struct in6_addr *));
150 static struct mbuf * ni6_input __P((struct mbuf *, int));
151 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
152 struct ifnet **));
153 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
154 struct ifnet *, int));
155 #if defined(__NetBSD__) || defined(__OpenBSD__)
156 static struct rtentry *icmp6_mtudisc_clone __P((struct sockaddr *));
157 static void icmp6_mtudisc_timeout __P((struct rtentry *, struct rttimer *));
158 #endif
159
160 #ifdef COMPAT_RFC1885
161 static struct route_in6 icmp6_reflect_rt;
162 #endif
163 static struct timeval icmp6_nextsend = {0, 0};
164
165 #if MIP6
166 int (*mip6_icmp6_input_hook)(struct mbuf *m, int off) = NULL;
167 #endif /* MIP6 */
168
169 void
170 icmp6_init()
171 {
172 mld6_init();
173 #if defined(__NetBSD__) || defined(__OpenBSD__)
174 icmp6_mtudisc_timeout_q = rt_timer_queue_create(pmtu_expire);
175 #endif
176 }
177
178 /*
179 * Generate an error packet of type error in response to bad IP6 packet.
180 */
181 void
182 icmp6_error(m, type, code, param)
183 struct mbuf *m;
184 int type, code, param;
185 {
186 struct ip6_hdr *oip6, *nip6;
187 struct icmp6_hdr *icmp6;
188 u_int preplen;
189 int off;
190 int nxt;
191
192 icmp6stat.icp6s_error++;
193
194 #ifdef M_DECRYPTED /*not openbsd*/
195 if (m->m_flags & M_DECRYPTED) {
196 icmp6stat.icp6s_canterror++;
197 goto freeit;
198 }
199 #endif
200
201 #ifndef PULLDOWN_TEST
202 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
203 #else
204 if (m->m_len < sizeof(struct ip6_hdr)) {
205 m = m_pullup(m, sizeof(struct ip6_hdr));
206 if (m == NULL)
207 return;
208 }
209 #endif
210 oip6 = mtod(m, struct ip6_hdr *);
211
212 /*
213 * Multicast destination check. For unrecognized option errors,
214 * this check has already done in ip6_unknown_opt(), so we can
215 * check only for other errors.
216 */
217 if ((m->m_flags & (M_BCAST|M_MCAST) ||
218 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
219 (type != ICMP6_PACKET_TOO_BIG &&
220 (type != ICMP6_PARAM_PROB ||
221 code != ICMP6_PARAMPROB_OPTION)))
222 goto freeit;
223
224 /* Source address check. XXX: the case of anycast source? */
225 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
226 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
227 goto freeit;
228
229 /*
230 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
231 * don't do it.
232 */
233 nxt = -1;
234 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
235 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
236 struct icmp6_hdr *icp;
237
238 #ifndef PULLDOWN_TEST
239 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
240 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
241 #else
242 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
243 sizeof(*icp));
244 if (icp == NULL) {
245 icmp6stat.icp6s_tooshort++;
246 return;
247 }
248 #endif
249 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
250 icp->icmp6_type == ND_REDIRECT) {
251 /*
252 * ICMPv6 error
253 * Special case: for redirect (which is
254 * informational) we must not send icmp6 error.
255 */
256 icmp6stat.icp6s_canterror++;
257 goto freeit;
258 } else {
259 /* ICMPv6 informational - send the error */
260 }
261 } else {
262 /* non-ICMPv6 - send the error */
263 }
264
265 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
266
267 /* Finally, do rate limitation check. */
268 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
269 icmp6stat.icp6s_toofreq++;
270 goto freeit;
271 }
272
273 /*
274 * OK, ICMP6 can be generated.
275 */
276
277 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
278 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
279
280 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
281 M_PREPEND(m, preplen, M_DONTWAIT);
282 if (m && m->m_len < preplen)
283 m = m_pullup(m, preplen);
284 if (m == NULL) {
285 printf("ENOBUFS in icmp6_error %d\n", __LINE__);
286 return;
287 }
288
289 nip6 = mtod(m, struct ip6_hdr *);
290 nip6->ip6_src = oip6->ip6_src;
291 nip6->ip6_dst = oip6->ip6_dst;
292
293 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
294 oip6->ip6_src.s6_addr16[1] = 0;
295 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
296 oip6->ip6_dst.s6_addr16[1] = 0;
297
298 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
299 icmp6->icmp6_type = type;
300 icmp6->icmp6_code = code;
301 icmp6->icmp6_pptr = htonl((u_int32_t)param);
302
303 icmp6stat.icp6s_outhist[type]++;
304 icmp6_reflect(m, sizeof(struct ip6_hdr)); /*header order: IPv6 - ICMPv6*/
305
306 return;
307
308 freeit:
309 /*
310 * If we can't tell wheter or not we can generate ICMP6, free it.
311 */
312 m_freem(m);
313 }
314
315 /*
316 * Process a received ICMP6 message.
317 */
318 int
319 icmp6_input(mp, offp, proto)
320 struct mbuf **mp;
321 int *offp, proto;
322 {
323 struct mbuf *m = *mp, *n;
324 struct ip6_hdr *ip6, *nip6;
325 struct icmp6_hdr *icmp6, *nicmp6;
326 int off = *offp;
327 int icmp6len = m->m_pkthdr.len - *offp;
328 int code, sum, noff;
329 struct sockaddr_in6 icmp6src;
330
331 #ifndef PULLDOWN_TEST
332 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
333 /* m might change if M_LOOP. So, call mtod after this */
334 #endif
335
336 /*
337 * Locate icmp6 structure in mbuf, and check
338 * that not corrupted and of at least minimum length
339 */
340
341 ip6 = mtod(m, struct ip6_hdr *);
342 if (icmp6len < sizeof(struct icmp6_hdr)) {
343 icmp6stat.icp6s_tooshort++;
344 goto freeit;
345 }
346
347 /*
348 * calculate the checksum
349 */
350 #ifndef PULLDOWN_TEST
351 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
352 #else
353 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
354 if (icmp6 == NULL) {
355 icmp6stat.icp6s_tooshort++;
356 return IPPROTO_DONE;
357 }
358 #endif
359 code = icmp6->icmp6_code;
360
361 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
362 log(LOG_ERR,
363 "ICMP6 checksum error(%d|%x) %s\n",
364 icmp6->icmp6_type,
365 sum,
366 ip6_sprintf(&ip6->ip6_src));
367 icmp6stat.icp6s_checksum++;
368 goto freeit;
369 }
370
371 #if defined(NFAITH) && 0 < NFAITH
372 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
373 /*
374 * Deliver very specific ICMP6 type only.
375 * This is important to deilver TOOBIG. Otherwise PMTUD
376 * will not work.
377 */
378 switch (icmp6->icmp6_type) {
379 case ICMP6_DST_UNREACH:
380 case ICMP6_PACKET_TOO_BIG:
381 case ICMP6_TIME_EXCEEDED:
382 break;
383 default:
384 goto freeit;
385 }
386 }
387 #endif
388
389 #if IPSEC
390 /* drop it if it does not match the default policy */
391 if (ipsec6_in_reject(m, NULL)) {
392 ipsecstat.in_polvio++;
393 goto freeit;
394 }
395 #endif
396
397 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
398 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
399 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
400 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
401
402 #if MIP6
403 /*
404 * Mobile IPv6
405 *
406 * Check for ICMP errors and modifications and extensions to Router
407 * Advertisement.
408 */
409 if (mip6_icmp6_input_hook) {
410 if ((*mip6_icmp6_input_hook)(m, off) != 0)
411 goto freeit;
412 }
413 #endif /* MIP6 */
414
415 switch (icmp6->icmp6_type) {
416
417 case ICMP6_DST_UNREACH:
418 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
419 switch (code) {
420 case ICMP6_DST_UNREACH_NOROUTE:
421 code = PRC_UNREACH_NET;
422 break;
423 case ICMP6_DST_UNREACH_ADMIN:
424 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
425 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
426 break;
427 case ICMP6_DST_UNREACH_ADDR:
428 code = PRC_HOSTDEAD;
429 break;
430 #ifdef COMPAT_RFC1885
431 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
432 code = PRC_UNREACH_SRCFAIL;
433 break;
434 #else
435 case ICMP6_DST_UNREACH_BEYONDSCOPE:
436 /* I mean "source address was incorrect." */
437 code = PRC_PARAMPROB;
438 break;
439 #endif
440 case ICMP6_DST_UNREACH_NOPORT:
441 code = PRC_UNREACH_PORT;
442 break;
443 default:
444 goto badcode;
445 }
446 goto deliver;
447 break;
448
449 case ICMP6_PACKET_TOO_BIG:
450 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
451 if (code != 0)
452 goto badcode;
453
454 code = PRC_MSGSIZE;
455
456 /*
457 * Updating the path MTU will be done after examining
458 * intermediate extension headers.
459 */
460 goto deliver;
461 break;
462
463 case ICMP6_TIME_EXCEEDED:
464 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
465 switch (code) {
466 case ICMP6_TIME_EXCEED_TRANSIT:
467 case ICMP6_TIME_EXCEED_REASSEMBLY:
468 code += PRC_TIMXCEED_INTRANS;
469 break;
470 default:
471 goto badcode;
472 }
473 goto deliver;
474 break;
475
476 case ICMP6_PARAM_PROB:
477 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
478 switch (code) {
479 case ICMP6_PARAMPROB_NEXTHEADER:
480 code = PRC_UNREACH_PROTOCOL;
481 break;
482 case ICMP6_PARAMPROB_HEADER:
483 case ICMP6_PARAMPROB_OPTION:
484 code = PRC_PARAMPROB;
485 break;
486 default:
487 goto badcode;
488 }
489 goto deliver;
490 break;
491
492 case ICMP6_ECHO_REQUEST:
493 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
494 if (code != 0)
495 goto badcode;
496 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
497 /* Give up remote */
498 break;
499 }
500 if ((n->m_flags & M_EXT) != 0
501 || n->m_len < off + sizeof(struct icmp6_hdr)) {
502 struct mbuf *n0 = n;
503 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
504
505 /*
506 * Prepare an internal mbuf. m_pullup() doesn't
507 * always copy the length we specified.
508 */
509 if (maxlen >= MCLBYTES) {
510 #if DIAGNOSTIC
511 printf("MCLBYTES too small\n");
512 #endif
513 /* Give up remote */
514 m_freem(n0);
515 break;
516 }
517 MGETHDR(n, M_DONTWAIT, n0->m_type);
518 if (n && maxlen >= MHLEN) {
519 MCLGET(n, M_DONTWAIT);
520 if ((n->m_flags & M_EXT) == 0) {
521 m_free(n);
522 n = NULL;
523 }
524 }
525 if (n == NULL) {
526 /* Give up remote */
527 m_freem(n0);
528 break;
529 }
530 M_COPY_PKTHDR(n, n0);
531 /*
532 * Copy IPv6 and ICMPv6 only.
533 */
534 nip6 = mtod(n, struct ip6_hdr *);
535 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
536 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
537 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
538 noff = sizeof(struct ip6_hdr);
539 n->m_pkthdr.len = n->m_len =
540 noff + sizeof(struct icmp6_hdr);
541 /*
542 * Adjust mbuf. ip6_plen will be adjusted in
543 * ip6_output().
544 */
545 m_adj(n0, off + sizeof(struct icmp6_hdr));
546 n->m_pkthdr.len += n0->m_pkthdr.len;
547 n->m_next = n0;
548 n0->m_flags &= ~M_PKTHDR;
549 } else {
550 nip6 = mtod(n, struct ip6_hdr *);
551 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
552 noff = off;
553 }
554 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
555 nicmp6->icmp6_code = 0;
556 if (n) {
557 icmp6stat.icp6s_reflect++;
558 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
559 icmp6_reflect(n, noff);
560 }
561 break;
562
563 case ICMP6_ECHO_REPLY:
564 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
565 if (code != 0)
566 goto badcode;
567 break;
568
569 case MLD6_LISTENER_QUERY:
570 case MLD6_LISTENER_REPORT:
571 if (icmp6len < sizeof(struct mld6_hdr))
572 goto badlen;
573 if (icmp6->icmp6_type == MLD6_LISTENER_QUERY) /* XXX: ugly... */
574 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
575 else
576 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
577 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
578 /* give up local */
579 mld6_input(m, off);
580 m = NULL;
581 goto freeit;
582 }
583 mld6_input(n, off);
584 /* m stays. */
585 break;
586
587 case MLD6_LISTENER_DONE:
588 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
589 if (icmp6len < sizeof(struct mld6_hdr)) /* necessary? */
590 goto badlen;
591 break; /* nothing to be done in kernel */
592
593 case MLD6_MTRACE_RESP:
594 case MLD6_MTRACE:
595 /* XXX: these two are experimental. not officially defind. */
596 /* XXX: per-interface statistics? */
597 break; /* just pass it to applications */
598
599 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
600 {
601 enum { WRU, FQDN } mode;
602
603 if (code != 0)
604 goto badcode;
605 if (!icmp6_nodeinfo)
606 break;
607
608 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
609 mode = WRU;
610 else if (icmp6len >= sizeof(struct icmp6_hdr) + 8) /* XXX */
611 mode = FQDN;
612 else
613 goto badlen;
614
615 #if defined( __FreeBSD__) || defined (__APPLE__)
616 #define hostnamelen strlen(hostname)
617 #endif
618 if (mode == FQDN) {
619 #ifndef PULLDOWN_TEST
620 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
621 IPPROTO_DONE);
622 #endif
623 n = m_copy(m, 0, M_COPYALL);
624 if (n)
625 n = ni6_input(n, off);
626 if (n)
627 noff = sizeof(struct ip6_hdr);
628 } else {
629 u_char *p;
630 int maxlen, maxhlen;
631
632 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
633 if (maxlen >= MCLBYTES) {
634 #if DIAGNOSTIC
635 printf("MCLBYTES too small\n");
636 #endif
637 /* Give up remote */
638 break;
639 }
640 MGETHDR(n, M_DONTWAIT, m->m_type);
641 if (n && maxlen > MHLEN) {
642 MCLGET(n, M_DONTWAIT);
643 if ((n->m_flags & M_EXT) == 0) {
644 m_free(n);
645 n = NULL;
646 }
647 }
648 if (n == NULL) {
649 /* Give up remote */
650 break;
651 }
652 n->m_len = 0;
653 maxhlen = M_TRAILINGSPACE(n) - maxlen;
654 if (maxhlen > hostnamelen)
655 maxhlen = hostnamelen;
656 /*
657 * Copy IPv6 and ICMPv6 only.
658 */
659 nip6 = mtod(n, struct ip6_hdr *);
660 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
661 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
662 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
663 p = (u_char *)(nicmp6 + 1);
664 bzero(p, 4);
665 bcopy(hostname, p + 4, maxhlen);
666 noff = sizeof(struct ip6_hdr);
667 M_COPY_PKTHDR(n, m); /* just for recvif */
668 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
669 sizeof(struct icmp6_hdr) + 4 + maxhlen;
670 nicmp6->icmp6_type = ICMP6_WRUREPLY;
671 nicmp6->icmp6_code = 0;
672 }
673 #undef hostnamelen
674 if (n) {
675 icmp6stat.icp6s_reflect++;
676 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
677 icmp6_reflect(n, noff);
678 }
679 break;
680 }
681
682 case ICMP6_WRUREPLY:
683 if (code != 0)
684 goto badcode;
685 break;
686
687 case ND_ROUTER_SOLICIT:
688 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
689 if (code != 0)
690 goto badcode;
691 if (icmp6len < sizeof(struct nd_router_solicit))
692 goto badlen;
693 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
694 /* give up local */
695 nd6_rs_input(m, off, icmp6len);
696 m = NULL;
697 goto freeit;
698 }
699 nd6_rs_input(n, off, icmp6len);
700 /* m stays. */
701 break;
702
703 case ND_ROUTER_ADVERT:
704 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
705 if (code != 0)
706 goto badcode;
707 if (icmp6len < sizeof(struct nd_router_advert))
708 goto badlen;
709 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
710 /* give up local */
711 nd6_ra_input(m, off, icmp6len);
712 m = NULL;
713 goto freeit;
714 }
715 nd6_ra_input(n, off, icmp6len);
716 /* m stays. */
717 break;
718
719 case ND_NEIGHBOR_SOLICIT:
720 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
721 if (code != 0)
722 goto badcode;
723 if (icmp6len < sizeof(struct nd_neighbor_solicit))
724 goto badlen;
725 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
726 /* give up local */
727 nd6_ns_input(m, off, icmp6len);
728 m = NULL;
729 goto freeit;
730 }
731 nd6_ns_input(n, off, icmp6len);
732 /* m stays. */
733 break;
734
735 case ND_NEIGHBOR_ADVERT:
736 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
737 if (code != 0)
738 goto badcode;
739 if (icmp6len < sizeof(struct nd_neighbor_advert))
740 goto badlen;
741 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
742 /* give up local */
743 nd6_na_input(m, off, icmp6len);
744 m = NULL;
745 goto freeit;
746 }
747 nd6_na_input(n, off, icmp6len);
748 /* m stays. */
749 break;
750
751 case ND_REDIRECT:
752 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
753 if (code != 0)
754 goto badcode;
755 if (icmp6len < sizeof(struct nd_redirect))
756 goto badlen;
757 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
758 /* give up local */
759 icmp6_redirect_input(m, off);
760 m = NULL;
761 goto freeit;
762 }
763 icmp6_redirect_input(n, off);
764 /* m stays. */
765 break;
766
767 case ICMP6_ROUTER_RENUMBERING:
768 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
769 code != ICMP6_ROUTER_RENUMBERING_RESULT)
770 goto badcode;
771 if (icmp6len < sizeof(struct icmp6_router_renum))
772 goto badlen;
773 break;
774
775 default:
776 printf("icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
777 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
778 ip6_sprintf(&ip6->ip6_dst),
779 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0);
780 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
781 /* ICMPv6 error: MUST deliver it by spec... */
782 code = PRC_NCMDS;
783 /* deliver */
784 } else {
785 /* ICMPv6 informational: MUST not deliver */
786 break;
787 }
788 deliver:
789 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
790 icmp6stat.icp6s_tooshort++;
791 goto freeit;
792 }
793 #ifndef PULLDOWN_TEST
794 IP6_EXTHDR_CHECK(m, off,
795 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
796 IPPROTO_DONE);
797 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
798 #else
799 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
800 sizeof(*icmp6) + sizeof(struct ip6_hdr));
801 if (icmp6 == NULL) {
802 icmp6stat.icp6s_tooshort++;
803 return IPPROTO_DONE;
804 }
805 #endif
806 bzero(&icmp6src, sizeof(icmp6src));
807 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
808 icmp6src.sin6_family = AF_INET6;
809 icmp6src.sin6_addr = ((struct ip6_hdr *)(icmp6 + 1))->ip6_dst;
810
811 /* Detect the upper level protocol */
812 {
813 void (*ctlfunc) __P((int, struct sockaddr *, void *));
814 struct ip6_hdr *eip6 = (struct ip6_hdr *)(icmp6 + 1);
815 u_int8_t nxt = eip6->ip6_nxt;
816 int eoff = off + sizeof(struct icmp6_hdr) +
817 sizeof(struct ip6_hdr);
818 struct ip6ctlparam ip6cp;
819 struct in6_addr *finaldst = NULL;
820 int icmp6type = icmp6->icmp6_type;
821 struct ip6_frag *fh;
822 struct ip6_rthdr *rth;
823 struct ip6_rthdr0 *rth0;
824 int rthlen;
825
826 while (1) { /* XXX: should avoid inf. loop explicitly? */
827 struct ip6_ext *eh;
828
829 switch(nxt) {
830 case IPPROTO_HOPOPTS:
831 case IPPROTO_DSTOPTS:
832 case IPPROTO_AH:
833 #ifndef PULLDOWN_TEST
834 IP6_EXTHDR_CHECK(m, 0, eoff +
835 sizeof(struct ip6_ext),
836 IPPROTO_DONE);
837 eh = (struct ip6_ext *)(mtod(m, caddr_t)
838 + eoff);
839 #else
840 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
841 eoff, sizeof(*eh));
842 if (eh == NULL) {
843 icmp6stat.icp6s_tooshort++;
844 return IPPROTO_DONE;
845 }
846 #endif
847
848 if (nxt == IPPROTO_AH)
849 eoff += (eh->ip6e_len + 2) << 2;
850 else
851 eoff += (eh->ip6e_len + 1) << 3;
852 nxt = eh->ip6e_nxt;
853 break;
854 case IPPROTO_ROUTING:
855 /*
856 * When the erroneous packet contains a
857 * routing header, we should examine the
858 * header to determine the final destination.
859 * Otherwise, we can't properly update
860 * information that depends on the final
861 * destination (e.g. path MTU).
862 */
863 #ifndef PULLDOWN_TEST
864 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
865 IPPROTO_DONE);
866 rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
867 + eoff);
868 #else
869 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
870 eoff, sizeof(*rth));
871 if (rth == NULL) {
872 icmp6stat.icp6s_tooshort++;
873 return IPPROTO_DONE;
874 }
875 #endif
876 rthlen = (rth->ip6r_len + 1) << 3;
877 /*
878 * XXX: currently there is no
879 * officially defined type other
880 * than type-0.
881 * Note that if the segment left field
882 * is 0, all intermediate hops must
883 * have been passed.
884 */
885 if (rth->ip6r_segleft &&
886 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
887 int hops;
888
889 #ifndef PULLDOWN_TEST
890 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen,
891 IPPROTO_DONE);
892 rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff);
893 #else
894 IP6_EXTHDR_GET(rth0,
895 struct ip6_rthdr0 *, m,
896 eoff, rthlen);
897 if (rth0 == NULL) {
898 icmp6stat.icp6s_tooshort++;
899 return IPPROTO_DONE;
900 }
901 #endif
902 /* just ignore a bogus header */
903 if ((rth0->ip6r0_len % 2) == 0 &&
904 (hops = rth0->ip6r0_len/2))
905 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
906 }
907 eoff += rthlen;
908 nxt = rth->ip6r_nxt;
909 break;
910 case IPPROTO_FRAGMENT:
911 #ifndef PULLDOWN_TEST
912 IP6_EXTHDR_CHECK(m, 0, eoff +
913 sizeof(struct ip6_frag),
914 IPPROTO_DONE);
915 fh = (struct ip6_frag *)(mtod(m, caddr_t)
916 + eoff);
917 #else
918 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
919 eoff, sizeof(*fh));
920 if (fh == NULL) {
921 icmp6stat.icp6s_tooshort++;
922 return IPPROTO_DONE;
923 }
924 #endif
925 /*
926 * Data after a fragment header is meaningless
927 * unless it is the first fragment, but
928 * we'll go to the notify label for path MTU
929 * discovery.
930 */
931 if (fh->ip6f_offlg & IP6F_OFF_MASK)
932 goto notify;
933
934 eoff += sizeof(struct ip6_frag);
935 nxt = fh->ip6f_nxt;
936 break;
937 default:
938 /*
939 * This case includes ESP and the No Next
940 * Header. In such cases going to the notify
941 * label does not have any meaning
942 * (i.e. ctlfunc will be NULL), but we go
943 * anyway since we might have to update
944 * path MTU information.
945 */
946 goto notify;
947 }
948 }
949 notify:
950 #ifndef PULLDOWN_TEST
951 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
952 #else
953 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
954 sizeof(*icmp6) + sizeof(struct ip6_hdr));
955 if (icmp6 == NULL) {
956 icmp6stat.icp6s_tooshort++;
957 return IPPROTO_DONE;
958 }
959 #endif
960 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
961 if (finaldst == NULL)
962 finaldst = &((struct ip6_hdr *)(icmp6 + 1))->ip6_dst;
963 icmp6_mtudisc_update(finaldst, icmp6, m);
964 }
965
966 ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
967 (ip6_protox[nxt]->pr_ctlinput);
968 if (ctlfunc) {
969 ip6cp.ip6c_m = m;
970 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
971 ip6cp.ip6c_off = eoff;
972 (*ctlfunc)(code, (struct sockaddr *)&icmp6src, &ip6cp);
973 }
974 }
975 break;
976
977 badcode:
978 icmp6stat.icp6s_badcode++;
979 break;
980
981 badlen:
982 icmp6stat.icp6s_badlen++;
983 break;
984 }
985
986 #if HAVE_NRL_INPCB
987 rip6_input(&m, offp, IPPROTO_ICMPV6);
988 #else
989 icmp6_rip6_input(&m, *offp);
990 #endif
991 return IPPROTO_DONE;
992
993 freeit:
994 m_freem(m);
995 return IPPROTO_DONE;
996 }
997
998 static void
999 icmp6_mtudisc_update(dst, icmp6, m)
1000 struct in6_addr *dst;
1001 struct icmp6_hdr *icmp6;/* we can assume the validity of the pointer */
1002 struct mbuf *m; /* currently unused but added for scoped addrs */
1003 {
1004 u_int mtu = ntohl(icmp6->icmp6_mtu);
1005 struct rtentry *rt = NULL;
1006 struct sockaddr_in6 sin6;
1007 #ifdef __bsdi__
1008 struct route_in6 ro6;
1009 #endif
1010
1011 bzero(&sin6, sizeof(sin6));
1012 sin6.sin6_family = PF_INET6;
1013 sin6.sin6_len = sizeof(struct sockaddr_in6);
1014 sin6.sin6_addr = *dst;
1015 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1016 #if defined(__NetBSD__) || defined(__OpenBSD__)
1017 rt = rtalloc1((struct sockaddr *)&sin6, 1); /*clone*/
1018 if (!rt || (rt->rt_flags & RTF_HOST) == 0) {
1019 if (rt)
1020 RTFREE(rt);
1021 rt = icmp6_mtudisc_clone((struct sockaddr *)&sin6);
1022 }
1023 #else
1024 #if defined(__FreeBSD__) || defined (__APPLE__)
1025 rt = rtalloc1((struct sockaddr *)&sin6, 0,
1026 RTF_CLONING | RTF_PRCLONING);
1027 #else
1028 #ifdef __bsdi__
1029 bcopy(&sin6, &ro6.ro_dst, sizeof(struct sockaddr_in6));
1030 ro6.ro_rt = 0;
1031 rtcalloc((struct route *)&ro6);
1032 rt = ro6.ro_rt;
1033 #else
1034 #error no case for this particular operating system
1035 #endif
1036 #endif
1037 #endif
1038
1039 if (rt && (rt->rt_flags & RTF_HOST)
1040 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1041 if (mtu < IPV6_MMTU) {
1042 /* xxx */
1043 rt->rt_rmx.rmx_locks |= RTV_MTU;
1044 } else if (mtu < rt->rt_ifp->if_mtu &&
1045 rt->rt_rmx.rmx_mtu > mtu) {
1046 rt->rt_rmx.rmx_mtu = mtu;
1047 }
1048 }
1049 if (rt)
1050 RTFREE(rt);
1051 }
1052
1053 /*
1054 * Process a Node Information Query
1055 */
1056 #if defined(__FreeBSD__) || defined (__APPLE__)
1057 #define hostnamelen strlen(hostname)
1058 #endif
1059 #ifndef offsetof /* XXX */
1060 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
1061 #endif
1062
1063 static struct mbuf *
1064 ni6_input(m, off)
1065 struct mbuf *m;
1066 int off;
1067 {
1068 struct icmp6_nodeinfo *ni6, *nni6;
1069 struct mbuf *n = NULL;
1070 u_int16_t qtype;
1071 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1072 struct ni_reply_fqdn *fqdn;
1073 int addrs; /* for NI_QTYPE_NODEADDR */
1074 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1075
1076 #ifndef PULLDOWN_TEST
1077 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1078 #else
1079 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1080 if (ni6 == NULL) {
1081 /* m is already reclaimed */
1082 return NULL;
1083 }
1084 #endif
1085 qtype = ntohs(ni6->ni_qtype);
1086
1087 switch(qtype) {
1088 case NI_QTYPE_NOOP:
1089 break; /* no reply data */
1090 case NI_QTYPE_SUPTYPES:
1091 goto bad; /* xxx: to be implemented */
1092 break;
1093 case NI_QTYPE_FQDN:
1094 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_name) +
1095 hostnamelen;
1096 break;
1097 case NI_QTYPE_NODEADDR:
1098 addrs = ni6_addrs(ni6, m, &ifp);
1099 if ((replylen += addrs * sizeof(struct in6_addr)) > MCLBYTES)
1100 replylen = MCLBYTES; /* XXX: we'll truncate later */
1101
1102 break;
1103 default:
1104 /*
1105 * XXX: We must return a reply with the ICMP6 code
1106 * `unknown Qtype' in this case. However we regard the case
1107 * as an FQDN query for backward compatibility.
1108 * Older versions set a random value to this field,
1109 * so it rarely varies in the defined qtypes.
1110 * But the mechanism is not reliable...
1111 * maybe we should obsolete older versions.
1112 */
1113 qtype = NI_QTYPE_FQDN;
1114 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_name) +
1115 hostnamelen;
1116 break;
1117 }
1118
1119 /* allocate a mbuf to reply. */
1120 MGETHDR(n, M_DONTWAIT, m->m_type);
1121 if (n == NULL) {
1122 m_freem(m);
1123 return(NULL);
1124 }
1125 M_COPY_PKTHDR(n, m); /* just for recvif */
1126 if (replylen > MHLEN) {
1127 if (replylen > MCLBYTES)
1128 /*
1129 * XXX: should we try to allocate more? But MCLBYTES is
1130 * probably much larger than IPV6_MMTU...
1131 */
1132 goto bad;
1133 MCLGET(n, M_DONTWAIT);
1134 if ((n->m_flags & M_EXT) == 0) {
1135 goto bad;
1136 }
1137 }
1138 n->m_pkthdr.len = n->m_len = replylen;
1139
1140 /* copy mbuf header and IPv6 + Node Information base headers */
1141 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1142 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1143 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1144
1145 /* qtype dependent procedure */
1146 switch (qtype) {
1147 case NI_QTYPE_NOOP:
1148 nni6->ni_flags = 0;
1149 break;
1150 case NI_QTYPE_SUPTYPES:
1151 goto bad; /* xxx: to be implemented */
1152 break;
1153 case NI_QTYPE_FQDN:
1154 if (hostnamelen > 255) { /* XXX: rare case, but may happen */
1155 printf("ni6_input: "
1156 "hostname length(%d) is too large for reply\n",
1157 hostnamelen);
1158 goto bad;
1159 }
1160 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1161 sizeof(struct ip6_hdr) +
1162 sizeof(struct icmp6_nodeinfo));
1163 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1164 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1165 fqdn->ni_fqdn_namelen = hostnamelen;
1166 bcopy(hostname, &fqdn->ni_fqdn_name[0], hostnamelen);
1167 break;
1168 case NI_QTYPE_NODEADDR:
1169 {
1170 int lenlim, copied;
1171
1172 if (n->m_flags & M_EXT)
1173 lenlim = MCLBYTES - sizeof(struct ip6_hdr) -
1174 sizeof(struct icmp6_nodeinfo);
1175 else
1176 lenlim = MHLEN - sizeof(struct ip6_hdr) -
1177 sizeof(struct icmp6_nodeinfo);
1178 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1179 /* XXX: reset mbuf length */
1180 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1181 sizeof(struct icmp6_nodeinfo) + copied;
1182 break;
1183 }
1184 default:
1185 break; /* XXX impossible! */
1186 }
1187
1188 nni6->ni_type = ICMP6_NI_REPLY;
1189 nni6->ni_code = ICMP6_NI_SUCESS;
1190 m_freem(m);
1191 return(n);
1192
1193 bad:
1194 m_freem(m);
1195 if (n)
1196 m_freem(n);
1197 return(NULL);
1198 }
1199 #undef hostnamelen
1200
1201 /*
1202 * calculate the number of addresses to be returned in the node info reply.
1203 */
1204 static int
1205 ni6_addrs(ni6, m, ifpp)
1206 struct icmp6_nodeinfo *ni6;
1207 struct mbuf *m;
1208 struct ifnet **ifpp;
1209 {
1210 register struct ifnet *ifp;
1211 register struct in6_ifaddr *ifa6;
1212 register struct ifaddr *ifa;
1213 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1214 int addrs = 0, addrsofif, iffound = 0;
1215
1216 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1217 for (ifp = ifnet; ifp; ifp = ifp->if_next)
1218 #else
1219 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1220 #endif
1221 {
1222 addrsofif = 0;
1223 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1224 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
1225 #else
1226 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1227 ifa = ifa->ifa_list.tqe_next)
1228 #endif
1229 {
1230 if (ifa->ifa_addr->sa_family != AF_INET6)
1231 continue;
1232 ifa6 = (struct in6_ifaddr *)ifa;
1233
1234 if (!(ni6->ni_flags & NI_NODEADDR_FLAG_ALL) &&
1235 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1236 &ifa6->ia_addr.sin6_addr))
1237 iffound = 1;
1238
1239 /*
1240 * IPv4-mapped addresses can only be returned by a
1241 * Node Information proxy, since they represent
1242 * addresses of IPv4-only nodes, which perforce do
1243 * not implement this protocol.
1244 * [icmp-name-lookups-05]
1245 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1246 * this function at this moment.
1247 */
1248
1249 if (ifa6->ia6_flags & IN6_IFF_ANYCAST)
1250 continue; /* we need only unicast addresses */
1251
1252 if ((ni6->ni_flags & (NI_NODEADDR_FLAG_LINKLOCAL |
1253 NI_NODEADDR_FLAG_SITELOCAL |
1254 NI_NODEADDR_FLAG_GLOBAL)) == 0)
1255 continue;
1256
1257 /* What do we have to do about ::1? */
1258 switch(in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1259 case IPV6_ADDR_SCOPE_LINKLOCAL:
1260 if (ni6->ni_flags & NI_NODEADDR_FLAG_LINKLOCAL)
1261 addrsofif++;
1262 break;
1263 case IPV6_ADDR_SCOPE_SITELOCAL:
1264 if (ni6->ni_flags & NI_NODEADDR_FLAG_SITELOCAL)
1265 addrsofif++;
1266 break;
1267 case IPV6_ADDR_SCOPE_GLOBAL:
1268 if (ni6->ni_flags & NI_NODEADDR_FLAG_GLOBAL)
1269 addrsofif++;
1270 break;
1271 default:
1272 continue;
1273 }
1274 }
1275 if (iffound) {
1276 *ifpp = ifp;
1277 return(addrsofif);
1278 }
1279
1280 addrs += addrsofif;
1281 }
1282
1283 return(addrs);
1284 }
1285
1286 static int
1287 ni6_store_addrs(ni6, nni6, ifp0, resid)
1288 struct icmp6_nodeinfo *ni6, *nni6;
1289 struct ifnet *ifp0;
1290 int resid;
1291 {
1292 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1293 register struct ifnet *ifp = ifp0 ? ifp0 : ifnet;
1294 #else
1295 register struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1296 #endif
1297 register struct in6_ifaddr *ifa6;
1298 register struct ifaddr *ifa;
1299 int docopy, copied = 0;
1300 u_char *cp = (u_char *)(nni6 + 1);
1301
1302 if (ifp0 == NULL && !(ni6->ni_flags & NI_NODEADDR_FLAG_ALL))
1303 return(0); /* needless to copy */
1304
1305 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1306 for (; ifp; ifp = ifp->if_next)
1307 #else
1308 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1309 #endif
1310 {
1311 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1312 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
1313 #else
1314 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1315 ifa = ifa->ifa_list.tqe_next)
1316 #endif
1317 {
1318 docopy = 0;
1319
1320 if (ifa->ifa_addr->sa_family != AF_INET6)
1321 continue;
1322 ifa6 = (struct in6_ifaddr *)ifa;
1323
1324 if (ifa6->ia6_flags & IN6_IFF_ANYCAST) {
1325 /* just experimental. not in the spec. */
1326 if (ni6->ni_flags & NI_NODEADDR_FLAG_ANYCAST)
1327 docopy = 1;
1328 else
1329 continue;
1330 }
1331 else { /* unicast address */
1332 if (ni6->ni_flags & NI_NODEADDR_FLAG_ANYCAST)
1333 continue;
1334 else
1335 docopy = 1;
1336 }
1337
1338 /* What do we have to do about ::1? */
1339 switch(in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1340 case IPV6_ADDR_SCOPE_LINKLOCAL:
1341 if (ni6->ni_flags & NI_NODEADDR_FLAG_LINKLOCAL)
1342 docopy = 1;
1343 break;
1344 case IPV6_ADDR_SCOPE_SITELOCAL:
1345 if (ni6->ni_flags & NI_NODEADDR_FLAG_SITELOCAL)
1346 docopy = 1;
1347 break;
1348 case IPV6_ADDR_SCOPE_GLOBAL:
1349 if (ni6->ni_flags & NI_NODEADDR_FLAG_GLOBAL)
1350 docopy = 1;
1351 break;
1352 default:
1353 continue;
1354 }
1355
1356 if (docopy) {
1357 if (resid < sizeof(struct in6_addr)) {
1358 /*
1359 * We give up much more copy.
1360 * Set the truncate flag and return.
1361 */
1362 nni6->ni_flags |=
1363 NI_NODEADDR_FLAG_TRUNCATE;
1364 return(copied);
1365 }
1366 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1367 sizeof(struct in6_addr));
1368 /* XXX: KAME link-local hack; remove ifindex */
1369 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1370 ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1371 cp += sizeof(struct in6_addr);
1372 resid -= sizeof(struct in6_addr);
1373 copied += sizeof(struct in6_addr);
1374 }
1375 }
1376 if (ifp0) /* we need search only on the specified IF */
1377 break;
1378 }
1379
1380 return(copied);
1381 }
1382
1383 #ifndef HAVE_NRL_INPCB
1384 /*
1385 * XXX almost dup'ed code with rip6_input.
1386 */
1387 static int
1388 icmp6_rip6_input(mp, off)
1389 struct mbuf **mp;
1390 int off;
1391 {
1392 struct mbuf *m = *mp;
1393 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1394 register struct in6pcb *in6p;
1395 struct in6pcb *last = NULL;
1396 struct sockaddr_in6 rip6src;
1397 struct icmp6_hdr *icmp6;
1398 struct ip6_recvpktopts opts;
1399
1400 #ifndef PULLDOWN_TEST
1401 /* this is assumed to be safe. */
1402 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1403 #else
1404 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1405 if (icmp6 == NULL) {
1406 /* m is already reclaimed */
1407 return IPPROTO_DONE;
1408 }
1409 #endif
1410
1411 bzero(&opts, sizeof(opts));
1412 bzero(&rip6src, sizeof(rip6src));
1413 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1414 rip6src.sin6_family = AF_INET6;
1415 rip6src.sin6_addr = ip6->ip6_src;
1416 if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
1417 rip6src.sin6_addr.s6_addr16[1] = 0;
1418 if (m->m_pkthdr.rcvif) {
1419 if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
1420 rip6src.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
1421 else
1422 rip6src.sin6_scope_id = 0;
1423 } else
1424 rip6src.sin6_scope_id = 0;
1425
1426 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
1427 LIST_FOREACH(in6p, &ripcb, inp_list)
1428 #else
1429 for (in6p = rawin6pcb.in6p_next;
1430 in6p != &rawin6pcb; in6p = in6p->in6p_next)
1431 #endif
1432 {
1433 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
1434 if ((in6p->inp_vflag & INP_IPV6) == NULL)
1435 continue;
1436 #endif
1437 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1438 continue;
1439 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1440 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1441 continue;
1442 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1443 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1444 continue;
1445 if (in6p->in6p_icmp6filt
1446 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1447 in6p->in6p_icmp6filt))
1448 continue;
1449 if (last) {
1450 struct mbuf *n;
1451 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1452 if (last->in6p_flags & IN6P_CONTROLOPTS)
1453 ip6_savecontrol(last, ip6, n, &opts,
1454 NULL);
1455 /* strip intermediate headers */
1456 m_adj(n, off);
1457 if (sbappendaddr(&last->in6p_socket->so_rcv,
1458 (struct sockaddr *)&rip6src,
1459 n, opts.head) == 0) {
1460 /* should notify about lost packet */
1461 m_freem(n);
1462 if (opts.head) {
1463 m_freem(opts.head);
1464 }
1465 } else
1466 sorwakeup(last->in6p_socket);
1467 bzero(&opts, sizeof(opts));
1468 }
1469 }
1470 last = in6p;
1471 }
1472 if (last) {
1473 if (last->in6p_flags & IN6P_CONTROLOPTS)
1474 ip6_savecontrol(last, ip6, m, &opts, NULL);
1475 /* strip intermediate headers */
1476 m_adj(m, off);
1477 if (sbappendaddr(&last->in6p_socket->so_rcv,
1478 (struct sockaddr *)&rip6src,
1479 m, opts.head) == 0) {
1480 m_freem(m);
1481 if (opts.head)
1482 m_freem(opts.head);
1483 } else
1484 sorwakeup(last->in6p_socket);
1485 } else {
1486 m_freem(m);
1487 ip6stat.ip6s_delivered--;
1488 }
1489 return IPPROTO_DONE;
1490 }
1491 #endif /*OpenBSD*/
1492
1493 /*
1494 * Reflect the ip6 packet back to the source.
1495 * OFF points to the icmp6 header, counted from the top of the mbuf.
1496 */
1497 void
1498 icmp6_reflect(m, off)
1499 struct mbuf *m;
1500 size_t off;
1501 {
1502 struct ip6_hdr *ip6;
1503 struct icmp6_hdr *icmp6;
1504 struct in6_ifaddr *ia;
1505 struct in6_addr t, *src = 0;
1506 int plen;
1507 int type, code;
1508 struct ifnet *outif = NULL;
1509 #ifdef COMPAT_RFC1885
1510 int mtu = IPV6_MMTU;
1511 struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
1512 #endif
1513
1514 /* too short to reflect */
1515 if (off < sizeof(struct ip6_hdr)) {
1516 printf("sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1517 (u_long)off, (u_long)sizeof(struct ip6_hdr),
1518 __FILE__, __LINE__);
1519 goto bad;
1520 }
1521
1522 /*
1523 * If there are extra headers between IPv6 and ICMPv6, strip
1524 * off that header first.
1525 */
1526 if (off > sizeof(struct ip6_hdr)) {
1527 size_t l;
1528 struct ip6_hdr nip6;
1529
1530 l = off - sizeof(struct ip6_hdr);
1531 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
1532 m_adj(m, l);
1533 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1534 if (m->m_len < l) {
1535 if ((m = m_pullup(m, l)) == NULL)
1536 return;
1537 }
1538 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
1539 } else /* off == sizeof(struct ip6_hdr) */ {
1540 size_t l;
1541 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1542 if (m->m_len < l) {
1543 if ((m = m_pullup(m, l)) == NULL)
1544 return;
1545 }
1546 }
1547 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
1548 ip6 = mtod(m, struct ip6_hdr *);
1549 ip6->ip6_nxt = IPPROTO_ICMPV6;
1550 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
1551 type = icmp6->icmp6_type; /* keep type for statistics */
1552 code = icmp6->icmp6_code; /* ditto. */
1553
1554 t = ip6->ip6_dst;
1555 /*
1556 * ip6_input() drops a packet if its src is multicast.
1557 * So, the src is never multicast.
1558 */
1559 ip6->ip6_dst = ip6->ip6_src;
1560
1561 /* XXX hack for link-local addresses */
1562 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
1563 ip6->ip6_dst.s6_addr16[1] =
1564 htons(m->m_pkthdr.rcvif->if_index);
1565 if (IN6_IS_ADDR_LINKLOCAL(&t))
1566 t.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1567
1568 #ifdef COMPAT_RFC1885
1569 /*
1570 * xxx guess MTU
1571 * RFC 1885 requires that echo reply should be truncated if it
1572 * does not fit in with (return) path MTU, but the description was
1573 * removed in the new spec.
1574 */
1575 if (icmp6_reflect_rt.ro_rt == 0 ||
1576 ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
1577 if (icmp6_reflect_rt.ro_rt) {
1578 #if defined(__FreeBSD__) || defined(__APPLE__)
1579 RTFREE(icmp6_reflect_rt.ro_rt);
1580 #endif
1581 #ifdef __bsdi__
1582 rtfree(icmp6_reflect_rt.ro_rt);
1583 #endif
1584 icmp6_reflect_rt.ro_rt = 0;
1585 }
1586 bzero(sin6, sizeof(*sin6));
1587 sin6->sin6_family = PF_INET6;
1588 sin6->sin6_len = sizeof(struct sockaddr_in6);
1589 sin6->sin6_addr = ip6->ip6_dst;
1590
1591 #if defined(__FreeBSD__) || defined(__APPLE__)
1592 rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
1593 RTF_PRCLONING);
1594 #else
1595 rtalloc((struct route *)&icmp6_reflect_rt.ro_rt);
1596 #endif
1597 }
1598
1599 if (icmp6_reflect_rt.ro_rt == 0)
1600 goto bad;
1601
1602 if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
1603 && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
1604 mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
1605
1606 if (mtu < m->m_pkthdr.len) {
1607 plen -= (m->m_pkthdr.len - mtu);
1608 m_adj(m, mtu - m->m_pkthdr.len);
1609 }
1610 #endif
1611 /*
1612 * If the incoming packet was addressed directly to us(i.e. unicast),
1613 * use dst as the src for the reply.
1614 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible when
1615 * (for example) when we encounter an error while forwarding procedure
1616 * destined to a duplicated address of ours.
1617 */
1618 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1619 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
1620 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
1621 src = &t;
1622 break;
1623 }
1624 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
1625 /*
1626 * This is the case if the dst is our link-local address
1627 * and the sender is also ourseleves.
1628 */
1629 src = &t;
1630 }
1631
1632 if (src == 0)
1633 /*
1634 * This case matches to multicasts, our anycast, or unicasts
1635 * that we do not own. Select a source address which has the
1636 * same scope.
1637 * XXX: for (non link-local) multicast addresses, this might
1638 * not be a good choice.
1639 */
1640 if ((ia = in6_ifawithscope(m->m_pkthdr.rcvif, &t)) != 0)
1641 src = &IA6_SIN6(ia)->sin6_addr;
1642
1643 if (src == 0)
1644 goto bad;
1645
1646 ip6->ip6_src = *src;
1647
1648 ip6->ip6_flow = 0;
1649 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1650 ip6->ip6_vfc |= IPV6_VERSION;
1651 ip6->ip6_nxt = IPPROTO_ICMPV6;
1652 if (m->m_pkthdr.rcvif) {
1653 /* XXX: This may not be the outgoing interface */
1654 ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim;
1655 }
1656
1657 icmp6->icmp6_cksum = 0;
1658 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1659 sizeof(struct ip6_hdr), plen);
1660
1661 /*
1662 * xxx option handling
1663 */
1664
1665 m->m_flags &= ~(M_BCAST|M_MCAST);
1666 #if IPSEC
1667 /* Don't lookup socket */
1668 ipsec_setsocket(m, NULL);
1669 #endif /*IPSEC*/
1670
1671 #ifdef COMPAT_RFC1885
1672 ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif);
1673 #else
1674 ip6_output(m, NULL, NULL, 0, NULL, &outif);
1675 #endif
1676 if (outif)
1677 icmp6_ifoutstat_inc(outif, type, code);
1678
1679 return;
1680
1681 bad:
1682 m_freem(m);
1683 return;
1684 }
1685
1686 void
1687 icmp6_fasttimo()
1688 {
1689 mld6_fasttimeo();
1690 }
1691
1692 static const char *
1693 icmp6_redirect_diag(src6, dst6, tgt6)
1694 struct in6_addr *src6;
1695 struct in6_addr *dst6;
1696 struct in6_addr *tgt6;
1697 {
1698 static char buf[1024];
1699 #if !defined(__OpenBSD__) && !defined(__bsdi__)
1700 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
1701 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
1702 #else
1703 sprintf(buf, "(src=%s dst=%s tgt=%s)",
1704 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
1705 #endif
1706 return buf;
1707 }
1708
1709 void
1710 icmp6_redirect_input(m, off)
1711 register struct mbuf *m;
1712 int off;
1713 {
1714 struct ifnet *ifp = m->m_pkthdr.rcvif;
1715 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1716 struct nd_redirect *nd_rd;
1717 int icmp6len = ntohs(ip6->ip6_plen);
1718 char *lladdr = NULL;
1719 int lladdrlen = 0;
1720 u_char *redirhdr = NULL;
1721 int redirhdrlen = 0;
1722 struct rtentry *rt = NULL;
1723 int is_router;
1724 int is_onlink;
1725 struct in6_addr src6 = ip6->ip6_src;
1726 struct in6_addr redtgt6;
1727 struct in6_addr reddst6;
1728 union nd_opts ndopts;
1729
1730 if (!m || !ifp)
1731 return;
1732
1733 /* XXX if we are router, we don't update route by icmp6 redirect */
1734 if (ip6_forwarding)
1735 goto freeit;
1736 if (!icmp6_rediraccept)
1737 goto freeit;
1738
1739 #ifndef PULLDOWN_TEST
1740 IP6_EXTHDR_CHECK(m, off, icmp6len,);
1741 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
1742 #else
1743 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
1744 if (nd_rd == NULL) {
1745 icmp6stat.icp6s_tooshort++;
1746 return;
1747 }
1748 #endif
1749 redtgt6 = nd_rd->nd_rd_target;
1750 reddst6 = nd_rd->nd_rd_dst;
1751
1752 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
1753 redtgt6.s6_addr16[1] = htons(ifp->if_index);
1754 if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
1755 reddst6.s6_addr16[1] = htons(ifp->if_index);
1756
1757 /* validation */
1758 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
1759 log(LOG_ERR,
1760 "ICMP6 redirect sent from %s rejected; "
1761 "must be from linklocal\n", ip6_sprintf(&src6));
1762 goto freeit;
1763 }
1764 if (ip6->ip6_hlim != 255) {
1765 log(LOG_ERR,
1766 "ICMP6 redirect sent from %s rejected; "
1767 "hlim=%d (must be 255)\n",
1768 ip6_sprintf(&src6), ip6->ip6_hlim);
1769 goto freeit;
1770 }
1771 {
1772 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
1773 struct sockaddr_in6 sin6;
1774 struct in6_addr *gw6;
1775
1776 bzero(&sin6, sizeof(sin6));
1777 sin6.sin6_family = AF_INET6;
1778 sin6.sin6_len = sizeof(struct sockaddr_in6);
1779 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
1780 rt = rtalloc1((struct sockaddr *)&sin6, 0
1781 #if defined(__FreeBSD__) || defined (__APPLE__)
1782 , 0UL
1783 #endif
1784 );
1785 if (rt) {
1786 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
1787 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
1788 log(LOG_ERR,
1789 "ICMP6 redirect rejected; "
1790 "not equal to gw-for-src=%s (must be same): "
1791 "%s\n",
1792 ip6_sprintf(gw6),
1793 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
1794 RTFREE(rt);
1795 goto freeit;
1796 }
1797 } else {
1798 log(LOG_ERR,
1799 "ICMP6 redirect rejected; "
1800 "no route found for redirect dst: %s\n",
1801 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
1802 goto freeit;
1803 }
1804 RTFREE(rt);
1805 rt = NULL;
1806 }
1807 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
1808 log(LOG_ERR,
1809 "ICMP6 redirect rejected; "
1810 "redirect dst must be unicast: %s\n",
1811 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
1812 goto freeit;
1813 }
1814
1815 is_router = is_onlink = 0;
1816 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
1817 is_router = 1; /* router case */
1818 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
1819 is_onlink = 1; /* on-link destination case */
1820 if (!is_router && !is_onlink) {
1821 log(LOG_ERR,
1822 "ICMP6 redirect rejected; "
1823 "neither router case nor onlink case: %s\n",
1824 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
1825 goto freeit;
1826 }
1827 /* validation passed */
1828
1829 icmp6len -= sizeof(*nd_rd);
1830 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
1831 if (nd6_options(&ndopts) < 0) {
1832 log(LOG_INFO, "icmp6_redirect_input: "
1833 "invalid ND option, rejected: %s\n",
1834 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
1835 goto freeit;
1836 }
1837
1838 if (ndopts.nd_opts_tgt_lladdr) {
1839 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
1840 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
1841 }
1842
1843 if (ndopts.nd_opts_rh) {
1844 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
1845 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
1846 }
1847
1848 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
1849 log(LOG_INFO,
1850 "icmp6_redirect_input: lladdrlen mismatch for %s "
1851 "(if %d, icmp6 packet %d): %s\n",
1852 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
1853 icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
1854 }
1855
1856 /* RFC 2461 8.3 */
1857 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
1858 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
1859
1860 if (!is_onlink) { /* better router case. perform rtredirect. */
1861 /* perform rtredirect */
1862 struct sockaddr_in6 sdst;
1863 struct sockaddr_in6 sgw;
1864 struct sockaddr_in6 ssrc;
1865 #ifdef __bsdi__
1866 extern int icmp_redirtimeout; /*XXX*/
1867 #endif
1868
1869 bzero(&sdst, sizeof(sdst));
1870 bzero(&sgw, sizeof(sgw));
1871 bzero(&ssrc, sizeof(ssrc));
1872 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
1873 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
1874 sizeof(struct sockaddr_in6);
1875 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
1876 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
1877 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
1878 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
1879 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
1880 (struct sockaddr *)&ssrc,
1881 #ifdef __bsdi__
1882 icmp_redirtimeout
1883 #else
1884 (struct rtentry **)NULL
1885 #endif /*__FreeBSD__, __NetBSD__, __bsdi__*/
1886 );
1887 }
1888 /* finally update cached route in each socket via pfctlinput */
1889 {
1890 struct sockaddr_in6 sdst;
1891 #if 1
1892 #else
1893 struct ip6protosw *pr;
1894 #endif
1895
1896 bzero(&sdst, sizeof(sdst));
1897 sdst.sin6_family = AF_INET6;
1898 sdst.sin6_len = sizeof(struct sockaddr_in6);
1899 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
1900 #if 1
1901 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
1902 #else
1903 /*
1904 * do not use pfctlinput() here, we have different prototype for
1905 * xx_ctlinput() in ip6proto.
1906 */
1907 for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
1908 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW;
1909 pr++) {
1910 if (pr->pr_ctlinput) {
1911 (*pr->pr_ctlinput)(PRC_REDIRECT_HOST,
1912 (struct sockaddr *)&sdst, NULL, NULL, 0);
1913 }
1914 }
1915 #endif
1916 #if IPSEC
1917 key_sa_routechange((struct sockaddr *)&sdst);
1918 #endif
1919 }
1920
1921 freeit:
1922 m_freem(m);
1923 }
1924
1925 void
1926 icmp6_redirect_output(m0, rt)
1927 struct mbuf *m0;
1928 struct rtentry *rt;
1929 {
1930 struct ifnet *ifp; /* my outgoing interface */
1931 struct in6_addr *ifp_ll6;
1932 struct in6_addr *router_ll6;
1933 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
1934 struct mbuf *m = NULL; /* newly allocated one */
1935 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
1936 struct nd_redirect *nd_rd;
1937 size_t maxlen;
1938 u_char *p;
1939 struct ifnet *outif = NULL;
1940
1941 /* if we are not router, we don't send icmp6 redirect */
1942 if (!ip6_forwarding || ip6_accept_rtadv)
1943 goto fail;
1944
1945 /* sanity check */
1946 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
1947 goto fail;
1948
1949 /*
1950 * Address check:
1951 * the source address must identify a neighbor, and
1952 * the destination address must not be a multicast address
1953 * [RFC 2461, sec 8.2]
1954 */
1955 sip6 = mtod(m0, struct ip6_hdr *);
1956 if (nd6_is_addr_neighbor(&sip6->ip6_src, ifp) == 0)
1957 goto fail;
1958 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
1959 goto fail; /* what should we do here? */
1960
1961 /* rate limit */
1962 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
1963 goto fail;
1964
1965 /*
1966 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
1967 * we almost always ask for an mbuf cluster for simplicity.
1968 * (MHLEN < IPV6_MMTU is almost always true)
1969 */
1970 #if IPV6_MMTU >= MCLBYTES
1971 # error assumption failed about IPV6_MMTU and MCLBYTES
1972 #endif
1973 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1974 if (m && IPV6_MMTU >= MHLEN)
1975 MCLGET(m, M_DONTWAIT);
1976 if (!m)
1977 goto fail;
1978 maxlen = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1979 maxlen = min(IPV6_MMTU, maxlen);
1980 /* just for safety */
1981 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
1982 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
1983 goto fail;
1984 }
1985
1986 {
1987 /* get ip6 linklocal address for ifp(my outgoing interface). */
1988 struct in6_ifaddr *ia;
1989 if ((ia = in6ifa_ifpforlinklocal(ifp,
1990 IN6_IFF_NOTREADY|
1991 IN6_IFF_ANYCAST)) == NULL)
1992 goto fail;
1993 ifp_ll6 = &ia->ia_addr.sin6_addr;
1994 }
1995
1996 /* get ip6 linklocal address for the router. */
1997 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
1998 struct sockaddr_in6 *sin6;
1999 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2000 router_ll6 = &sin6->sin6_addr;
2001 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2002 router_ll6 = (struct in6_addr *)NULL;
2003 } else
2004 router_ll6 = (struct in6_addr *)NULL;
2005
2006 /* ip6 */
2007 ip6 = mtod(m, struct ip6_hdr *);
2008 ip6->ip6_flow = 0;
2009 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2010 ip6->ip6_vfc |= IPV6_VERSION;
2011 /* ip6->ip6_plen will be set later */
2012 ip6->ip6_nxt = IPPROTO_ICMPV6;
2013 ip6->ip6_hlim = 255;
2014 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2015 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2016 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2017
2018 /* ND Redirect */
2019 nd_rd = (struct nd_redirect *)(ip6 + 1);
2020 nd_rd->nd_rd_type = ND_REDIRECT;
2021 nd_rd->nd_rd_code = 0;
2022 nd_rd->nd_rd_reserved = 0;
2023 if (rt->rt_flags & RTF_GATEWAY) {
2024 /*
2025 * nd_rd->nd_rd_target must be a link-local address in
2026 * better router cases.
2027 */
2028 if (!router_ll6)
2029 goto fail;
2030 bcopy(router_ll6, &nd_rd->nd_rd_target,
2031 sizeof(nd_rd->nd_rd_target));
2032 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2033 sizeof(nd_rd->nd_rd_dst));
2034 } else {
2035 /* make sure redtgt == reddst */
2036 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2037 sizeof(nd_rd->nd_rd_target));
2038 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2039 sizeof(nd_rd->nd_rd_dst));
2040 }
2041
2042 p = (u_char *)(nd_rd + 1);
2043
2044 if (!router_ll6)
2045 goto nolladdropt;
2046
2047 {
2048 /* target lladdr option */
2049 struct rtentry *rt_router = NULL;
2050 int len;
2051 struct sockaddr_dl *sdl;
2052 struct nd_opt_hdr *nd_opt;
2053 char *lladdr;
2054
2055 rt_router = nd6_lookup(router_ll6, 0, ifp);
2056 if (!rt_router)
2057 goto nolladdropt;
2058 len = sizeof(*nd_opt) + ifp->if_addrlen;
2059 len = (len + 7) & ~7; /*round by 8*/
2060 /* safety check */
2061 if (len + (p - (u_char *)ip6) > maxlen)
2062 goto nolladdropt;
2063 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2064 (rt_router->rt_flags & RTF_LLINFO) &&
2065 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2066 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2067 sdl->sdl_alen) {
2068 nd_opt = (struct nd_opt_hdr *)p;
2069 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2070 nd_opt->nd_opt_len = len >> 3;
2071 lladdr = (char *)(nd_opt + 1);
2072 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2073 p += len;
2074 }
2075 }
2076 nolladdropt:;
2077
2078 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2079
2080 /* just to be safe */
2081 #if M_DECRYPTED /*not openbsd*/
2082 if (m0->m_flags & M_DECRYPTED)
2083 goto noredhdropt;
2084 #endif
2085 if (p - (u_char *)ip6 > maxlen)
2086 goto noredhdropt;
2087
2088 {
2089 /* redirected header option */
2090 int len;
2091 struct nd_opt_rd_hdr *nd_opt_rh;
2092
2093 /*
2094 * compute the maximum size for icmp6 redirect header option.
2095 * XXX room for auth header?
2096 */
2097 len = maxlen - (p - (u_char *)ip6);
2098 len &= ~7;
2099
2100 /* This is just for simplicity. */
2101 if (m0->m_pkthdr.len != m0->m_len) {
2102 if (m0->m_next) {
2103 m_freem(m0->m_next);
2104 m0->m_next = NULL;
2105 }
2106 m0->m_pkthdr.len = m0->m_len;
2107 }
2108
2109 /*
2110 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2111 * about padding/truncate rule for the original IP packet.
2112 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2113 * - "attach as much as possible" is the goal
2114 * - pad if not aligned (original size can be guessed by original
2115 * ip6 header)
2116 * Following code adds the padding if it is simple enough,
2117 * and truncates if not.
2118 */
2119 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2120 panic("assumption failed in %s:%d\n", __FILE__, __LINE__);
2121
2122 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2123 /* not enough room, truncate */
2124 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2125 } else {
2126 /* enough room, pad or truncate */
2127 size_t extra;
2128
2129 extra = m0->m_pkthdr.len % 8;
2130 if (extra) {
2131 /* pad if easy enough, truncate if not */
2132 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2133 /* pad */
2134 m0->m_len += (8 - extra);
2135 m0->m_pkthdr.len += (8 - extra);
2136 } else {
2137 /* truncate */
2138 m0->m_pkthdr.len -= extra;
2139 m0->m_len -= extra;
2140 }
2141 }
2142 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2143 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2144 }
2145
2146 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2147 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2148 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2149 nd_opt_rh->nd_opt_rh_len = len >> 3;
2150 p += sizeof(*nd_opt_rh);
2151 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2152
2153 /* connect m0 to m */
2154 m->m_next = m0;
2155 m->m_pkthdr.len = m->m_len + m0->m_len;
2156 }
2157 noredhdropt:;
2158
2159 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2160 sip6->ip6_src.s6_addr16[1] = 0;
2161 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2162 sip6->ip6_dst.s6_addr16[1] = 0;
2163 #if 0
2164 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2165 ip6->ip6_src.s6_addr16[1] = 0;
2166 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2167 ip6->ip6_dst.s6_addr16[1] = 0;
2168 #endif
2169 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2170 nd_rd->nd_rd_target.s6_addr16[1] = 0;
2171 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2172 nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2173
2174 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2175
2176 nd_rd->nd_rd_cksum = 0;
2177 nd_rd->nd_rd_cksum
2178 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2179
2180 /* send the packet to outside... */
2181 #if IPSEC
2182 /* Don't lookup socket */
2183 ipsec_setsocket(m, NULL);
2184 #endif /*IPSEC*/
2185 ip6_output(m, NULL, NULL, 0, NULL, &outif);
2186 if (outif) {
2187 icmp6_ifstat_inc(outif, ifs6_out_msg);
2188 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2189 }
2190 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2191
2192 return;
2193
2194 fail:
2195 if (m)
2196 m_freem(m);
2197 if (m0)
2198 m_freem(m0);
2199 }
2200
2201 #ifndef HAVE_NRL_INPCB
2202 /*
2203 * ICMPv6 socket option processing.
2204 *
2205 * NOTE: for OSes that use NRL inpcb (bsdi4/openbsd), do not forget to modify
2206 * sys/netinet6/raw_ipv6.c:rip6_ctloutput().
2207 */
2208 int
2209 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2210 icmp6_ctloutput(so, sopt)
2211 struct socket *so;
2212 struct sockopt *sopt;
2213 #else
2214 icmp6_ctloutput(op, so, level, optname, mp)
2215 int op;
2216 struct socket *so;
2217 int level, optname;
2218 struct mbuf **mp;
2219 #endif
2220 {
2221 int error = 0;
2222 int optlen;
2223 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2224 register struct inpcb *inp = sotoinpcb(so);
2225 int level, op, optname;
2226
2227 if (sopt) {
2228 level = sopt->sopt_level;
2229 op = sopt->sopt_dir;
2230 optname = sopt->sopt_name;
2231 optlen = sopt->sopt_valsize;
2232 } else
2233 level = op = optname = optlen = 0;
2234 #else
2235 register struct in6pcb *in6p = sotoin6pcb(so);
2236 register struct mbuf *m = *mp;
2237
2238 optlen = m ? m->m_len : 0;
2239 #endif
2240
2241 if (level != IPPROTO_ICMPV6) {
2242 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
2243 if (op == PRCO_SETOPT && m)
2244 (void)m_free(m);
2245 #endif
2246 return EINVAL;
2247 }
2248
2249 switch(op) {
2250 case PRCO_SETOPT:
2251 switch (optname) {
2252 case ICMP6_FILTER:
2253 {
2254 struct icmp6_filter *p;
2255
2256 if (optlen != sizeof(*p)) {
2257 error = EMSGSIZE;
2258 break;
2259 }
2260 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2261 if (inp->in6p_icmp6filt == NULL) {
2262 error = EINVAL;
2263 break;
2264 }
2265 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2266 optlen);
2267 #else
2268 p = mtod(m, struct icmp6_filter *);
2269 if (!p || !in6p->in6p_icmp6filt) {
2270 error = EINVAL;
2271 break;
2272 }
2273 bcopy(p, in6p->in6p_icmp6filt,
2274 sizeof(struct icmp6_filter));
2275 error = 0;
2276 #endif
2277 break;
2278 }
2279
2280 default:
2281 error = ENOPROTOOPT;
2282 break;
2283 }
2284 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
2285 if (m)
2286 (void)m_freem(m);
2287 #endif
2288 break;
2289
2290 case PRCO_GETOPT:
2291 switch (optname) {
2292 case ICMP6_FILTER:
2293 {
2294 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2295 if (inp->in6p_icmp6filt == NULL) {
2296 error = EINVAL;
2297 break;
2298 }
2299 error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2300 sizeof(struct icmp6_filter));
2301 #else
2302 struct icmp6_filter *p;
2303
2304 if (!in6p->in6p_icmp6filt) {
2305 error = EINVAL;
2306 break;
2307 }
2308 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2309 m->m_len = sizeof(struct icmp6_filter);
2310 p = mtod(m, struct icmp6_filter *);
2311 bcopy(in6p->in6p_icmp6filt, p,
2312 sizeof(struct icmp6_filter));
2313 error = 0;
2314 #endif
2315 break;
2316 }
2317
2318 default:
2319 error = ENOPROTOOPT;
2320 break;
2321 }
2322 break;
2323 }
2324
2325 return(error);
2326 }
2327 #endif /*NRL inpcb*/
2328
2329 /*
2330 * Perform rate limit check.
2331 * Returns 0 if it is okay to send the icmp6 packet.
2332 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2333 * limitation.
2334 *
2335 * XXX per-destination/type check necessary?
2336 */
2337 static int
2338 icmp6_ratelimit(dst, type, code)
2339 const struct in6_addr *dst; /* not used at this moment */
2340 const int type; /* not used at this moment */
2341 const int code; /* not used at this moment */
2342 {
2343 struct timeval tp;
2344 long sec_diff, usec_diff;
2345
2346 /* If we are not doing rate limitation, it is always okay to send */
2347 if (!icmp6errratelim)
2348 return 0;
2349
2350 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2351 microtime(&tp);
2352 tp.tv_sec = time_second;
2353 #else
2354 tp = time;
2355 #endif
2356 if (tp.tv_sec < icmp6_nextsend.tv_sec
2357 || (tp.tv_sec == icmp6_nextsend.tv_sec
2358 && tp.tv_usec < icmp6_nextsend.tv_usec)) {
2359 /* The packet is subject to rate limit */
2360 return 1;
2361 }
2362 sec_diff = icmp6errratelim / 1000000;
2363 usec_diff = icmp6errratelim % 1000000;
2364 icmp6_nextsend.tv_sec = tp.tv_sec + sec_diff;
2365 if ((tp.tv_usec = tp.tv_usec + usec_diff) >= 1000000) {
2366 icmp6_nextsend.tv_sec++;
2367 icmp6_nextsend.tv_usec -= 1000000;
2368 }
2369
2370 /* it is okay to send this */
2371 return 0;
2372 }
2373
2374 #if defined(__NetBSD__) || defined(__OpenBSD__)
2375 static struct rtentry *
2376 icmp6_mtudisc_clone(dst)
2377 struct sockaddr *dst;
2378 {
2379 struct rtentry *rt;
2380 int error;
2381
2382 rt = rtalloc1(dst, 1);
2383 if (rt == 0)
2384 return NULL;
2385
2386 /* If we didn't get a host route, allocate one */
2387 if ((rt->rt_flags & RTF_HOST) == 0) {
2388 struct rtentry *nrt;
2389
2390 error = rtrequest((int) RTM_ADD, dst,
2391 (struct sockaddr *) rt->rt_gateway,
2392 (struct sockaddr *) 0,
2393 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
2394 if (error) {
2395 rtfree(rt);
2396 rtfree(nrt);
2397 return NULL;
2398 }
2399 nrt->rt_rmx = rt->rt_rmx;
2400 rtfree(rt);
2401 rt = nrt;
2402 }
2403 error = rt_timer_add(rt, icmp6_mtudisc_timeout,
2404 icmp6_mtudisc_timeout_q);
2405 if (error) {
2406 rtfree(rt);
2407 return NULL;
2408 }
2409
2410 return rt; /* caller need to call rtfree() */
2411 }
2412
2413 static void
2414 icmp6_mtudisc_timeout(rt, r)
2415 struct rtentry *rt;
2416 struct rttimer *r;
2417 {
2418 if (rt == NULL)
2419 panic("icmp6_mtudisc_timeout: bad route to timeout");
2420 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
2421 (RTF_DYNAMIC | RTF_HOST)) {
2422 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2423 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2424 } else {
2425 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
2426 rt->rt_rmx.rmx_mtu = 0;
2427 }
2428 }
2429 }
2430 #endif /*__NetBSD__ || __OpenBSD__*/
2431
2432 #ifdef __bsdi__
2433 void
2434 icmp6_mtuexpire(rt, rtt)
2435 struct rtentry *rt;
2436 struct rttimer *rtt;
2437 {
2438 rt->rt_flags |= RTF_PROBEMTU;
2439 Free(rtt);
2440 }
2441
2442 int *icmp6_sysvars[] = ICMPV6CTL_VARS;
2443
2444 int
2445 icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
2446 int *name;
2447 u_int namelen;
2448 void *oldp;
2449 size_t *oldlenp;
2450 void *newp;
2451 size_t newlen;
2452 {
2453 if (name[0] >= ICMPV6CTL_MAXID)
2454 return (EOPNOTSUPP);
2455 switch (name[0]) {
2456 #if 0
2457 ICMPV6CTL_ND6_PRUNE:
2458 ICMPV6CTL_ND6_DELAY:
2459 ICMPV6CTL_ND6_UMAXTRIES:
2460 ICMPV6CTL_ND6_MMAXTRIES:
2461 ICMPV6CTL_ND6_USELOOPBACK:
2462 /* need to check the value. */
2463 #endif
2464 case ICMPV6CTL_STATS:
2465 return sysctl_rdtrunc(oldp, oldlenp, newp, &icmp6stat,
2466 sizeof(icmp6stat));
2467
2468 default:
2469 return (sysctl_int_arr(icmp6_sysvars, name, namelen,
2470 oldp, oldlenp, newp, newlen));
2471 }
2472 }
2473 #endif /*__bsdi__*/
2474
2475 #if defined(__NetBSD__) || defined(__OpenBSD__)
2476 #include <vm/vm.h>
2477 #include <sys/sysctl.h>
2478 int
2479 icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
2480 int *name;
2481 u_int namelen;
2482 void *oldp;
2483 size_t *oldlenp;
2484 void *newp;
2485 size_t newlen;
2486 {
2487
2488 /* All sysctl names at this level are terminal. */
2489 if (namelen != 1)
2490 return ENOTDIR;
2491
2492 switch (name[0]) {
2493
2494 case ICMPV6CTL_REDIRACCEPT:
2495 return sysctl_int(oldp, oldlenp, newp, newlen,
2496 &icmp6_rediraccept);
2497 case ICMPV6CTL_REDIRTIMEOUT:
2498 return sysctl_int(oldp, oldlenp, newp, newlen,
2499 &icmp6_redirtimeout);
2500 case ICMPV6CTL_STATS:
2501 return sysctl_rdstruct(oldp, oldlenp, newp,
2502 &icmp6stat, sizeof(icmp6stat));
2503 case ICMPV6CTL_ERRRATELIMIT:
2504 return sysctl_int(oldp, oldlenp, newp, newlen,
2505 &icmp6errratelim);
2506 case ICMPV6CTL_ND6_PRUNE:
2507 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_prune);
2508 case ICMPV6CTL_ND6_DELAY:
2509 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_delay);
2510 case ICMPV6CTL_ND6_UMAXTRIES:
2511 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_umaxtries);
2512 case ICMPV6CTL_ND6_MMAXTRIES:
2513 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_mmaxtries);
2514 case ICMPV6CTL_ND6_USELOOPBACK:
2515 return sysctl_int(oldp, oldlenp, newp, newlen,
2516 &nd6_useloopback);
2517 case ICMPV6CTL_NODEINFO:
2518 return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6_nodeinfo);
2519 default:
2520 return ENOPROTOOPT;
2521 }
2522 /* NOTREACHED */
2523 }
2524 #endif /* __NetBSD__ */