1 /* $KAME: icmp6.c,v 1.77 2000/04/13 11:31:39 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
64 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
67 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
69 #ifdef __NetBSD__ /*XXX*/
70 #include "opt_ipsec.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
82 #include <sys/kernel.h>
83 #include <sys/syslog.h>
84 #include <sys/domain.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
89 #include <net/if_types.h>
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>
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>
104 #include <netinet/in_pcb.h>
106 #include <netinet6/nd6.h>
107 #include <netinet6/in6_ifattach.h>
108 #include <netinet6/ip6protosw.h>
110 #ifdef __OpenBSD__ /*KAME IPSEC*/
115 #include <netinet6/ipsec.h>
116 #include <netkey/key.h>
117 #include <netkey/key_debug.h>
122 #include <net/net_osdep.h>
124 extern struct domain inet6domain
;
125 extern struct ip6protosw inet6sw
[];
126 extern struct ip6protosw
*ip6_protox
[];
128 struct icmp6stat icmp6stat
;
130 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
131 extern struct in6pcb rawin6pcb
;
133 extern struct inpcbhead ripcb
;
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
;
142 #ifndef HAVE_NRL_INPCB
143 static int icmp6_rip6_input
__P((struct mbuf
**, int));
145 static void icmp6_mtudisc_update
__P((struct in6_addr
*, struct icmp6_hdr
*,
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
*,
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
*));
160 #ifdef COMPAT_RFC1885
161 static struct route_in6 icmp6_reflect_rt
;
163 static struct timeval icmp6_nextsend
= {0, 0};
166 int (*mip6_icmp6_input_hook
)(struct mbuf
*m
, int off
) = NULL
;
173 #if defined(__NetBSD__) || defined(__OpenBSD__)
174 icmp6_mtudisc_timeout_q
= rt_timer_queue_create(pmtu_expire
);
179 * Generate an error packet of type error in response to bad IP6 packet.
182 icmp6_error(m
, type
, code
, param
)
184 int type
, code
, param
;
186 struct ip6_hdr
*oip6
, *nip6
;
187 struct icmp6_hdr
*icmp6
;
192 icmp6stat
.icp6s_error
++;
194 #ifdef M_DECRYPTED /*not openbsd*/
195 if (m
->m_flags
& M_DECRYPTED
) {
196 icmp6stat
.icp6s_canterror
++;
201 #ifndef PULLDOWN_TEST
202 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), );
204 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
205 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
210 oip6
= mtod(m
, struct ip6_hdr
*);
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.
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
)))
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
))
230 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
234 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
235 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
236 struct icmp6_hdr
*icp
;
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
);
242 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
245 icmp6stat
.icp6s_tooshort
++;
249 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
250 icp
->icmp6_type
== ND_REDIRECT
) {
253 * Special case: for redirect (which is
254 * informational) we must not send icmp6 error.
256 icmp6stat
.icp6s_canterror
++;
259 /* ICMPv6 informational - send the error */
262 /* non-ICMPv6 - send the error */
265 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
267 /* Finally, do rate limitation check. */
268 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
269 icmp6stat
.icp6s_toofreq
++;
274 * OK, ICMP6 can be generated.
277 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
)
278 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
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
);
285 printf("ENOBUFS in icmp6_error %d\n", __LINE__
);
289 nip6
= mtod(m
, struct ip6_hdr
*);
290 nip6
->ip6_src
= oip6
->ip6_src
;
291 nip6
->ip6_dst
= oip6
->ip6_dst
;
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;
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
);
303 icmp6stat
.icp6s_outhist
[type
]++;
304 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /*header order: IPv6 - ICMPv6*/
310 * If we can't tell wheter or not we can generate ICMP6, free it.
316 * Process a received ICMP6 message.
319 icmp6_input(mp
, offp
, proto
)
323 struct mbuf
*m
= *mp
, *n
;
324 struct ip6_hdr
*ip6
, *nip6
;
325 struct icmp6_hdr
*icmp6
, *nicmp6
;
327 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
329 struct sockaddr_in6 icmp6src
;
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 */
337 * Locate icmp6 structure in mbuf, and check
338 * that not corrupted and of at least minimum length
341 ip6
= mtod(m
, struct ip6_hdr
*);
342 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
343 icmp6stat
.icp6s_tooshort
++;
348 * calculate the checksum
350 #ifndef PULLDOWN_TEST
351 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
353 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
355 icmp6stat
.icp6s_tooshort
++;
359 code
= icmp6
->icmp6_code
;
361 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
363 "ICMP6 checksum error(%d|%x) %s\n",
366 ip6_sprintf(&ip6
->ip6_src
));
367 icmp6stat
.icp6s_checksum
++;
371 #if defined(NFAITH) && 0 < NFAITH
372 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
374 * Deliver very specific ICMP6 type only.
375 * This is important to deilver TOOBIG. Otherwise PMTUD
378 switch (icmp6
->icmp6_type
) {
379 case ICMP6_DST_UNREACH
:
380 case ICMP6_PACKET_TOO_BIG
:
381 case ICMP6_TIME_EXCEEDED
:
390 /* drop it if it does not match the default policy */
391 if (ipsec6_in_reject(m
, NULL
)) {
392 ipsecstat
.in_polvio
++;
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
);
406 * Check for ICMP errors and modifications and extensions to Router
409 if (mip6_icmp6_input_hook
) {
410 if ((*mip6_icmp6_input_hook
)(m
, off
) != 0)
415 switch (icmp6
->icmp6_type
) {
417 case ICMP6_DST_UNREACH
:
418 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
420 case ICMP6_DST_UNREACH_NOROUTE
:
421 code
= PRC_UNREACH_NET
;
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? */
427 case ICMP6_DST_UNREACH_ADDR
:
430 #ifdef COMPAT_RFC1885
431 case ICMP6_DST_UNREACH_NOTNEIGHBOR
:
432 code
= PRC_UNREACH_SRCFAIL
;
435 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
436 /* I mean "source address was incorrect." */
437 code
= PRC_PARAMPROB
;
440 case ICMP6_DST_UNREACH_NOPORT
:
441 code
= PRC_UNREACH_PORT
;
449 case ICMP6_PACKET_TOO_BIG
:
450 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
457 * Updating the path MTU will be done after examining
458 * intermediate extension headers.
463 case ICMP6_TIME_EXCEEDED
:
464 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
466 case ICMP6_TIME_EXCEED_TRANSIT
:
467 case ICMP6_TIME_EXCEED_REASSEMBLY
:
468 code
+= PRC_TIMXCEED_INTRANS
;
476 case ICMP6_PARAM_PROB
:
477 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
479 case ICMP6_PARAMPROB_NEXTHEADER
:
480 code
= PRC_UNREACH_PROTOCOL
;
482 case ICMP6_PARAMPROB_HEADER
:
483 case ICMP6_PARAMPROB_OPTION
:
484 code
= PRC_PARAMPROB
;
492 case ICMP6_ECHO_REQUEST
:
493 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
496 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
500 if ((n
->m_flags
& M_EXT
) != 0
501 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
503 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
506 * Prepare an internal mbuf. m_pullup() doesn't
507 * always copy the length we specified.
509 if (maxlen
>= MCLBYTES
) {
511 printf("MCLBYTES too small\n");
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) {
530 M_COPY_PKTHDR(n
, n0
);
532 * Copy IPv6 and ICMPv6 only.
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
);
542 * Adjust mbuf. ip6_plen will be adjusted in
545 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
546 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
548 n0
->m_flags
&= ~M_PKTHDR
;
550 nip6
= mtod(n
, struct ip6_hdr
*);
551 nicmp6
= (struct icmp6_hdr
*)((caddr_t
)nip6
+ off
);
554 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
555 nicmp6
->icmp6_code
= 0;
557 icmp6stat
.icp6s_reflect
++;
558 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
559 icmp6_reflect(n
, noff
);
563 case ICMP6_ECHO_REPLY
:
564 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
569 case MLD6_LISTENER_QUERY
:
570 case MLD6_LISTENER_REPORT
:
571 if (icmp6len
< sizeof(struct mld6_hdr
))
573 if (icmp6
->icmp6_type
== MLD6_LISTENER_QUERY
) /* XXX: ugly... */
574 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
576 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
577 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
587 case MLD6_LISTENER_DONE
:
588 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
589 if (icmp6len
< sizeof(struct mld6_hdr
)) /* necessary? */
591 break; /* nothing to be done in kernel */
593 case MLD6_MTRACE_RESP
:
595 /* XXX: these two are experimental. not officially defind. */
596 /* XXX: per-interface statistics? */
597 break; /* just pass it to applications */
599 case ICMP6_WRUREQUEST
: /* ICMP6_FQDN_QUERY */
601 enum { WRU
, FQDN
} mode
;
608 if (icmp6len
== sizeof(struct icmp6_hdr
) + 4)
610 else if (icmp6len
>= sizeof(struct icmp6_hdr
) + 8) /* XXX */
615 #if defined( __FreeBSD__) || defined (__APPLE__)
616 #define hostnamelen strlen(hostname)
619 #ifndef PULLDOWN_TEST
620 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
623 n
= m_copy(m
, 0, M_COPYALL
);
625 n
= ni6_input(n
, off
);
627 noff
= sizeof(struct ip6_hdr
);
632 maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
) + 4;
633 if (maxlen
>= MCLBYTES
) {
635 printf("MCLBYTES too small\n");
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) {
653 maxhlen
= M_TRAILINGSPACE(n
) - maxlen
;
654 if (maxhlen
> hostnamelen
)
655 maxhlen
= hostnamelen
;
657 * Copy IPv6 and ICMPv6 only.
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);
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;
675 icmp6stat
.icp6s_reflect
++;
676 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
677 icmp6_reflect(n
, noff
);
687 case ND_ROUTER_SOLICIT
:
688 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
691 if (icmp6len
< sizeof(struct nd_router_solicit
))
693 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
695 nd6_rs_input(m
, off
, icmp6len
);
699 nd6_rs_input(n
, off
, icmp6len
);
703 case ND_ROUTER_ADVERT
:
704 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
707 if (icmp6len
< sizeof(struct nd_router_advert
))
709 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
711 nd6_ra_input(m
, off
, icmp6len
);
715 nd6_ra_input(n
, off
, icmp6len
);
719 case ND_NEIGHBOR_SOLICIT
:
720 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
723 if (icmp6len
< sizeof(struct nd_neighbor_solicit
))
725 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
727 nd6_ns_input(m
, off
, icmp6len
);
731 nd6_ns_input(n
, off
, icmp6len
);
735 case ND_NEIGHBOR_ADVERT
:
736 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
739 if (icmp6len
< sizeof(struct nd_neighbor_advert
))
741 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
743 nd6_na_input(m
, off
, icmp6len
);
747 nd6_na_input(n
, off
, icmp6len
);
752 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
755 if (icmp6len
< sizeof(struct nd_redirect
))
757 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
759 icmp6_redirect_input(m
, off
);
763 icmp6_redirect_input(n
, off
);
767 case ICMP6_ROUTER_RENUMBERING
:
768 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
769 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
)
771 if (icmp6len
< sizeof(struct icmp6_router_renum
))
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... */
785 /* ICMPv6 informational: MUST not deliver */
789 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
790 icmp6stat
.icp6s_tooshort
++;
793 #ifndef PULLDOWN_TEST
794 IP6_EXTHDR_CHECK(m
, off
,
795 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
797 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
799 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
800 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
802 icmp6stat
.icp6s_tooshort
++;
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
;
811 /* Detect the upper level protocol */
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
;
822 struct ip6_rthdr
*rth
;
823 struct ip6_rthdr0
*rth0
;
826 while (1) { /* XXX: should avoid inf. loop explicitly? */
830 case IPPROTO_HOPOPTS
:
831 case IPPROTO_DSTOPTS
:
833 #ifndef PULLDOWN_TEST
834 IP6_EXTHDR_CHECK(m
, 0, eoff
+
835 sizeof(struct ip6_ext
),
837 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
840 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
843 icmp6stat
.icp6s_tooshort
++;
848 if (nxt
== IPPROTO_AH
)
849 eoff
+= (eh
->ip6e_len
+ 2) << 2;
851 eoff
+= (eh
->ip6e_len
+ 1) << 3;
854 case IPPROTO_ROUTING
:
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).
863 #ifndef PULLDOWN_TEST
864 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
866 rth
= (struct ip6_rthdr
*)(mtod(m
, caddr_t
)
869 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
872 icmp6stat
.icp6s_tooshort
++;
876 rthlen
= (rth
->ip6r_len
+ 1) << 3;
878 * XXX: currently there is no
879 * officially defined type other
881 * Note that if the segment left field
882 * is 0, all intermediate hops must
885 if (rth
->ip6r_segleft
&&
886 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
889 #ifndef PULLDOWN_TEST
890 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
892 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
895 struct ip6_rthdr0
*, m
,
898 icmp6stat
.icp6s_tooshort
++;
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);
910 case IPPROTO_FRAGMENT
:
911 #ifndef PULLDOWN_TEST
912 IP6_EXTHDR_CHECK(m
, 0, eoff
+
913 sizeof(struct ip6_frag
),
915 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
918 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
921 icmp6stat
.icp6s_tooshort
++;
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
931 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
)
934 eoff
+= sizeof(struct ip6_frag
);
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.
950 #ifndef PULLDOWN_TEST
951 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
953 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
954 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
956 icmp6stat
.icp6s_tooshort
++;
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
);
966 ctlfunc
= (void (*) __P((int, struct sockaddr
*, void *)))
967 (ip6_protox
[nxt
]->pr_ctlinput
);
970 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
971 ip6cp
.ip6c_off
= eoff
;
972 (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6src
, &ip6cp
);
978 icmp6stat
.icp6s_badcode
++;
982 icmp6stat
.icp6s_badlen
++;
987 rip6_input(&m
, offp
, IPPROTO_ICMPV6
);
989 icmp6_rip6_input(&m
, *offp
);
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 */
1004 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1005 struct rtentry
*rt
= NULL
;
1006 struct sockaddr_in6 sin6
;
1008 struct route_in6 ro6
;
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) {
1021 rt
= icmp6_mtudisc_clone((struct sockaddr
*)&sin6
);
1024 #if defined(__FreeBSD__) || defined (__APPLE__)
1025 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0,
1026 RTF_CLONING
| RTF_PRCLONING
);
1029 bcopy(&sin6
, &ro6
.ro_dst
, sizeof(struct sockaddr_in6
));
1031 rtcalloc((struct route
*)&ro6
);
1034 #error no case for this particular operating system
1039 if (rt
&& (rt
->rt_flags
& RTF_HOST
)
1040 && !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1041 if (mtu
< IPV6_MMTU
) {
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
;
1054 * Process a Node Information Query
1056 #if defined(__FreeBSD__) || defined (__APPLE__)
1057 #define hostnamelen strlen(hostname)
1059 #ifndef offsetof /* XXX */
1060 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
1063 static struct mbuf
*
1068 struct icmp6_nodeinfo
*ni6
, *nni6
;
1069 struct mbuf
*n
= NULL
;
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 */
1076 #ifndef PULLDOWN_TEST
1077 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1079 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1081 /* m is already reclaimed */
1085 qtype
= ntohs(ni6
->ni_qtype
);
1089 break; /* no reply data */
1090 case NI_QTYPE_SUPTYPES
:
1091 goto bad
; /* xxx: to be implemented */
1094 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_name
) +
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 */
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.
1113 qtype
= NI_QTYPE_FQDN
;
1114 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_name
) +
1119 /* allocate a mbuf to reply. */
1120 MGETHDR(n
, M_DONTWAIT
, m
->m_type
);
1125 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1126 if (replylen
> MHLEN
) {
1127 if (replylen
> MCLBYTES
)
1129 * XXX: should we try to allocate more? But MCLBYTES is
1130 * probably much larger than IPV6_MMTU...
1133 MCLGET(n
, M_DONTWAIT
);
1134 if ((n
->m_flags
& M_EXT
) == 0) {
1138 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
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
));
1145 /* qtype dependent procedure */
1150 case NI_QTYPE_SUPTYPES
:
1151 goto bad
; /* xxx: to be implemented */
1154 if (hostnamelen
> 255) { /* XXX: rare case, but may happen */
1155 printf("ni6_input: "
1156 "hostname length(%d) is too large for reply\n",
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
);
1168 case NI_QTYPE_NODEADDR
:
1172 if (n
->m_flags
& M_EXT
)
1173 lenlim
= MCLBYTES
- sizeof(struct ip6_hdr
) -
1174 sizeof(struct icmp6_nodeinfo
);
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
;
1185 break; /* XXX impossible! */
1188 nni6
->ni_type
= ICMP6_NI_REPLY
;
1189 nni6
->ni_code
= ICMP6_NI_SUCESS
;
1202 * calculate the number of addresses to be returned in the node info reply.
1205 ni6_addrs(ni6
, m
, ifpp
)
1206 struct icmp6_nodeinfo
*ni6
;
1208 struct ifnet
**ifpp
;
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;
1216 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1217 for (ifp
= ifnet
; ifp
; ifp
= ifp
->if_next
)
1219 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
1223 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1224 for (ifa
= ifp
->if_addrlist
; ifa
; ifa
= ifa
->ifa_next
)
1226 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1227 ifa
= ifa
->ifa_list
.tqe_next
)
1230 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1232 ifa6
= (struct in6_ifaddr
*)ifa
;
1234 if (!(ni6
->ni_flags
& NI_NODEADDR_FLAG_ALL
) &&
1235 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
1236 &ifa6
->ia_addr
.sin6_addr
))
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.
1249 if (ifa6
->ia6_flags
& IN6_IFF_ANYCAST
)
1250 continue; /* we need only unicast addresses */
1252 if ((ni6
->ni_flags
& (NI_NODEADDR_FLAG_LINKLOCAL
|
1253 NI_NODEADDR_FLAG_SITELOCAL
|
1254 NI_NODEADDR_FLAG_GLOBAL
)) == 0)
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
)
1263 case IPV6_ADDR_SCOPE_SITELOCAL
:
1264 if (ni6
->ni_flags
& NI_NODEADDR_FLAG_SITELOCAL
)
1267 case IPV6_ADDR_SCOPE_GLOBAL
:
1268 if (ni6
->ni_flags
& NI_NODEADDR_FLAG_GLOBAL
)
1287 ni6_store_addrs(ni6
, nni6
, ifp0
, resid
)
1288 struct icmp6_nodeinfo
*ni6
, *nni6
;
1292 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1293 register struct ifnet
*ifp
= ifp0
? ifp0
: ifnet
;
1295 register struct ifnet
*ifp
= ifp0
? ifp0
: TAILQ_FIRST(&ifnet
);
1297 register struct in6_ifaddr
*ifa6
;
1298 register struct ifaddr
*ifa
;
1299 int docopy
, copied
= 0;
1300 u_char
*cp
= (u_char
*)(nni6
+ 1);
1302 if (ifp0
== NULL
&& !(ni6
->ni_flags
& NI_NODEADDR_FLAG_ALL
))
1303 return(0); /* needless to copy */
1305 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1306 for (; ifp
; ifp
= ifp
->if_next
)
1308 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
1311 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
1312 for (ifa
= ifp
->if_addrlist
; ifa
; ifa
= ifa
->ifa_next
)
1314 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1315 ifa
= ifa
->ifa_list
.tqe_next
)
1320 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1322 ifa6
= (struct in6_ifaddr
*)ifa
;
1324 if (ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) {
1325 /* just experimental. not in the spec. */
1326 if (ni6
->ni_flags
& NI_NODEADDR_FLAG_ANYCAST
)
1331 else { /* unicast address */
1332 if (ni6
->ni_flags
& NI_NODEADDR_FLAG_ANYCAST
)
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
)
1344 case IPV6_ADDR_SCOPE_SITELOCAL
:
1345 if (ni6
->ni_flags
& NI_NODEADDR_FLAG_SITELOCAL
)
1348 case IPV6_ADDR_SCOPE_GLOBAL
:
1349 if (ni6
->ni_flags
& NI_NODEADDR_FLAG_GLOBAL
)
1357 if (resid
< sizeof(struct in6_addr
)) {
1359 * We give up much more copy.
1360 * Set the truncate flag and return.
1363 NI_NODEADDR_FLAG_TRUNCATE
;
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
);
1376 if (ifp0
) /* we need search only on the specified IF */
1383 #ifndef HAVE_NRL_INPCB
1385 * XXX almost dup'ed code with rip6_input.
1388 icmp6_rip6_input(mp
, off
)
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
;
1400 #ifndef PULLDOWN_TEST
1401 /* this is assumed to be safe. */
1402 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
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
;
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
;
1422 rip6src
.sin6_scope_id
= 0;
1424 rip6src
.sin6_scope_id
= 0;
1426 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
1427 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
1429 for (in6p
= rawin6pcb
.in6p_next
;
1430 in6p
!= &rawin6pcb
; in6p
= in6p
->in6p_next
)
1433 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
1434 if ((in6p
->inp_vflag
& INP_IPV6
) == NULL
)
1437 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
)
1439 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
1440 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
))
1442 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
1443 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
))
1445 if (in6p
->in6p_icmp6filt
1446 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
1447 in6p
->in6p_icmp6filt
))
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
,
1455 /* strip intermediate headers */
1457 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1458 (struct sockaddr
*)&rip6src
,
1459 n
, opts
.head
) == 0) {
1460 /* should notify about lost packet */
1466 sorwakeup(last
->in6p_socket
);
1467 bzero(&opts
, sizeof(opts
));
1473 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1474 ip6_savecontrol(last
, ip6
, m
, &opts
, NULL
);
1475 /* strip intermediate headers */
1477 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1478 (struct sockaddr
*)&rip6src
,
1479 m
, opts
.head
) == 0) {
1484 sorwakeup(last
->in6p_socket
);
1487 ip6stat
.ip6s_delivered
--;
1489 return IPPROTO_DONE
;
1494 * Reflect the ip6 packet back to the source.
1495 * OFF points to the icmp6 header, counted from the top of the mbuf.
1498 icmp6_reflect(m
, off
)
1502 struct ip6_hdr
*ip6
;
1503 struct icmp6_hdr
*icmp6
;
1504 struct in6_ifaddr
*ia
;
1505 struct in6_addr t
, *src
= 0;
1508 struct ifnet
*outif
= NULL
;
1509 #ifdef COMPAT_RFC1885
1510 int mtu
= IPV6_MMTU
;
1511 struct sockaddr_in6
*sin6
= &icmp6_reflect_rt
.ro_dst
;
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__
);
1523 * If there are extra headers between IPv6 and ICMPv6, strip
1524 * off that header first.
1526 if (off
> sizeof(struct ip6_hdr
)) {
1528 struct ip6_hdr nip6
;
1530 l
= off
- sizeof(struct ip6_hdr
);
1531 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
1533 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
1535 if ((m
= m_pullup(m
, l
)) == NULL
)
1538 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
1539 } else /* off == sizeof(struct ip6_hdr) */ {
1541 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
1543 if ((m
= m_pullup(m
, l
)) == NULL
)
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. */
1556 * ip6_input() drops a packet if its src is multicast.
1557 * So, the src is never multicast.
1559 ip6
->ip6_dst
= ip6
->ip6_src
;
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
);
1568 #ifdef COMPAT_RFC1885
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.
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
);
1582 rtfree(icmp6_reflect_rt
.ro_rt
);
1584 icmp6_reflect_rt
.ro_rt
= 0;
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
;
1591 #if defined(__FreeBSD__) || defined(__APPLE__)
1592 rtalloc_ign((struct route
*)&icmp6_reflect_rt
.ro_rt
,
1595 rtalloc((struct route
*)&icmp6_reflect_rt
.ro_rt
);
1599 if (icmp6_reflect_rt
.ro_rt
== 0)
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
;
1606 if (mtu
< m
->m_pkthdr
.len
) {
1607 plen
-= (m
->m_pkthdr
.len
- mtu
);
1608 m_adj(m
, mtu
- m
->m_pkthdr
.len
);
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.
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) {
1624 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) && (m
->m_flags
& M_LOOP
)) {
1626 * This is the case if the dst is our link-local address
1627 * and the sender is also ourseleves.
1634 * This case matches to multicasts, our anycast, or unicasts
1635 * that we do not own. Select a source address which has the
1637 * XXX: for (non link-local) multicast addresses, this might
1638 * not be a good choice.
1640 if ((ia
= in6_ifawithscope(m
->m_pkthdr
.rcvif
, &t
)) != 0)
1641 src
= &IA6_SIN6(ia
)->sin6_addr
;
1646 ip6
->ip6_src
= *src
;
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
;
1657 icmp6
->icmp6_cksum
= 0;
1658 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
1659 sizeof(struct ip6_hdr
), plen
);
1662 * xxx option handling
1665 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
1667 /* Don't lookup socket */
1668 ipsec_setsocket(m
, NULL
);
1671 #ifdef COMPAT_RFC1885
1672 ip6_output(m
, NULL
, &icmp6_reflect_rt
, 0, NULL
, &outif
);
1674 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
);
1677 icmp6_ifoutstat_inc(outif
, type
, code
);
1693 icmp6_redirect_diag(src6
, dst6
, tgt6
)
1694 struct in6_addr
*src6
;
1695 struct in6_addr
*dst6
;
1696 struct in6_addr
*tgt6
;
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
));
1703 sprintf(buf
, "(src=%s dst=%s tgt=%s)",
1704 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
1710 icmp6_redirect_input(m
, off
)
1711 register struct mbuf
*m
;
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
;
1720 u_char
*redirhdr
= NULL
;
1721 int redirhdrlen
= 0;
1722 struct rtentry
*rt
= NULL
;
1725 struct in6_addr src6
= ip6
->ip6_src
;
1726 struct in6_addr redtgt6
;
1727 struct in6_addr reddst6
;
1728 union nd_opts ndopts
;
1733 /* XXX if we are router, we don't update route by icmp6 redirect */
1736 if (!icmp6_rediraccept
)
1739 #ifndef PULLDOWN_TEST
1740 IP6_EXTHDR_CHECK(m
, off
, icmp6len
,);
1741 nd_rd
= (struct nd_redirect
*)((caddr_t
)ip6
+ off
);
1743 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
1744 if (nd_rd
== NULL
) {
1745 icmp6stat
.icp6s_tooshort
++;
1749 redtgt6
= nd_rd
->nd_rd_target
;
1750 reddst6
= nd_rd
->nd_rd_dst
;
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
);
1758 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
1760 "ICMP6 redirect sent from %s rejected; "
1761 "must be from linklocal\n", ip6_sprintf(&src6
));
1764 if (ip6
->ip6_hlim
!= 255) {
1766 "ICMP6 redirect sent from %s rejected; "
1767 "hlim=%d (must be 255)\n",
1768 ip6_sprintf(&src6
), ip6
->ip6_hlim
);
1772 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
1773 struct sockaddr_in6 sin6
;
1774 struct in6_addr
*gw6
;
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__)
1786 gw6
= &(((struct sockaddr_in6
*)rt
->rt_gateway
)->sin6_addr
);
1787 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
1789 "ICMP6 redirect rejected; "
1790 "not equal to gw-for-src=%s (must be same): "
1793 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
1799 "ICMP6 redirect rejected; "
1800 "no route found for redirect dst: %s\n",
1801 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
1807 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
1809 "ICMP6 redirect rejected; "
1810 "redirect dst must be unicast: %s\n",
1811 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
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
) {
1822 "ICMP6 redirect rejected; "
1823 "neither router case nor onlink case: %s\n",
1824 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
));
1827 /* validation passed */
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
));
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;
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 */
1848 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
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
));
1857 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
1858 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
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
;
1866 extern int icmp_redirtimeout
; /*XXX*/
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
,
1884 (struct rtentry
**)NULL
1885 #endif /*__FreeBSD__, __NetBSD__, __bsdi__*/
1888 /* finally update cached route in each socket via pfctlinput */
1890 struct sockaddr_in6 sdst
;
1893 struct ip6protosw
*pr
;
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
));
1901 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
1904 * do not use pfctlinput() here, we have different prototype for
1905 * xx_ctlinput() in ip6proto.
1907 for (pr
= (struct ip6protosw
*)inet6domain
.dom_protosw
;
1908 pr
< (struct ip6protosw
*)inet6domain
.dom_protoswNPROTOSW
;
1910 if (pr
->pr_ctlinput
) {
1911 (*pr
->pr_ctlinput
)(PRC_REDIRECT_HOST
,
1912 (struct sockaddr
*)&sdst
, NULL
, NULL
, 0);
1917 key_sa_routechange((struct sockaddr
*)&sdst
);
1926 icmp6_redirect_output(m0
, rt
)
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
;
1939 struct ifnet
*outif
= NULL
;
1941 /* if we are not router, we don't send icmp6 redirect */
1942 if (!ip6_forwarding
|| ip6_accept_rtadv
)
1946 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
))
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]
1955 sip6
= mtod(m0
, struct ip6_hdr
*);
1956 if (nd6_is_addr_neighbor(&sip6
->ip6_src
, ifp
) == 0)
1958 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
))
1959 goto fail
; /* what should we do here? */
1962 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0))
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)
1970 #if IPV6_MMTU >= MCLBYTES
1971 # error assumption failed about IPV6_MMTU and MCLBYTES
1973 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1974 if (m
&& IPV6_MMTU
>= MHLEN
)
1975 MCLGET(m
, M_DONTWAIT
);
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)) {
1987 /* get ip6 linklocal address for ifp(my outgoing interface). */
1988 struct in6_ifaddr
*ia
;
1989 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
1991 IN6_IFF_ANYCAST
)) == NULL
)
1993 ifp_ll6
= &ia
->ia_addr
.sin6_addr
;
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
;
2004 router_ll6
= (struct in6_addr
*)NULL
;
2007 ip6
= mtod(m
, struct ip6_hdr
*);
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
));
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
) {
2025 * nd_rd->nd_rd_target must be a link-local address in
2026 * better router cases.
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
));
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
));
2042 p
= (u_char
*)(nd_rd
+ 1);
2048 /* target lladdr option */
2049 struct rtentry
*rt_router
= NULL
;
2051 struct sockaddr_dl
*sdl
;
2052 struct nd_opt_hdr
*nd_opt
;
2055 rt_router
= nd6_lookup(router_ll6
, 0, ifp
);
2058 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2059 len
= (len
+ 7) & ~7; /*round by 8*/
2061 if (len
+ (p
- (u_char
*)ip6
) > maxlen
)
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
) &&
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
);
2078 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2080 /* just to be safe */
2081 #if M_DECRYPTED /*not openbsd*/
2082 if (m0
->m_flags
& M_DECRYPTED
)
2085 if (p
- (u_char
*)ip6
> maxlen
)
2089 /* redirected header option */
2091 struct nd_opt_rd_hdr
*nd_opt_rh
;
2094 * compute the maximum size for icmp6 redirect header option.
2095 * XXX room for auth header?
2097 len
= maxlen
- (p
- (u_char
*)ip6
);
2100 /* This is just for simplicity. */
2101 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2103 m_freem(m0
->m_next
);
2106 m0
->m_pkthdr
.len
= m0
->m_len
;
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
2116 * Following code adds the padding if it is simple enough,
2117 * and truncates if not.
2119 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
)
2120 panic("assumption failed in %s:%d\n", __FILE__
, __LINE__
);
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
);
2126 /* enough room, pad or truncate */
2129 extra
= m0
->m_pkthdr
.len
% 8;
2131 /* pad if easy enough, truncate if not */
2132 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2134 m0
->m_len
+= (8 - extra
);
2135 m0
->m_pkthdr
.len
+= (8 - extra
);
2138 m0
->m_pkthdr
.len
-= extra
;
2142 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2143 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
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
;
2153 /* connect m0 to m */
2155 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
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;
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;
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;
2174 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
2176 nd_rd
->nd_rd_cksum
= 0;
2178 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
2180 /* send the packet to outside... */
2182 /* Don't lookup socket */
2183 ipsec_setsocket(m
, NULL
);
2185 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
);
2187 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
2188 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
2190 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
2201 #ifndef HAVE_NRL_INPCB
2203 * ICMPv6 socket option processing.
2205 * NOTE: for OSes that use NRL inpcb (bsdi4/openbsd), do not forget to modify
2206 * sys/netinet6/raw_ipv6.c:rip6_ctloutput().
2209 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2210 icmp6_ctloutput(so
, sopt
)
2212 struct sockopt
*sopt
;
2214 icmp6_ctloutput(op
, so
, level
, optname
, mp
)
2223 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2224 register struct inpcb
*inp
= sotoinpcb(so
);
2225 int level
, op
, optname
;
2228 level
= sopt
->sopt_level
;
2229 op
= sopt
->sopt_dir
;
2230 optname
= sopt
->sopt_name
;
2231 optlen
= sopt
->sopt_valsize
;
2233 level
= op
= optname
= optlen
= 0;
2235 register struct in6pcb
*in6p
= sotoin6pcb(so
);
2236 register struct mbuf
*m
= *mp
;
2238 optlen
= m
? m
->m_len
: 0;
2241 if (level
!= IPPROTO_ICMPV6
) {
2242 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
2243 if (op
== PRCO_SETOPT
&& m
)
2254 struct icmp6_filter
*p
;
2256 if (optlen
!= sizeof(*p
)) {
2260 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2261 if (inp
->in6p_icmp6filt
== NULL
) {
2265 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
2268 p
= mtod(m
, struct icmp6_filter
*);
2269 if (!p
|| !in6p
->in6p_icmp6filt
) {
2273 bcopy(p
, in6p
->in6p_icmp6filt
,
2274 sizeof(struct icmp6_filter
));
2281 error
= ENOPROTOOPT
;
2284 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
2294 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2295 if (inp
->in6p_icmp6filt
== NULL
) {
2299 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
2300 sizeof(struct icmp6_filter
));
2302 struct icmp6_filter
*p
;
2304 if (!in6p
->in6p_icmp6filt
) {
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
));
2319 error
= ENOPROTOOPT
;
2327 #endif /*NRL inpcb*/
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
2335 * XXX per-destination/type check necessary?
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 */
2344 long sec_diff
, usec_diff
;
2346 /* If we are not doing rate limitation, it is always okay to send */
2347 if (!icmp6errratelim
)
2350 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
2352 tp
.tv_sec
= time_second
;
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 */
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;
2370 /* it is okay to send this */
2374 #if defined(__NetBSD__) || defined(__OpenBSD__)
2375 static struct rtentry
*
2376 icmp6_mtudisc_clone(dst
)
2377 struct sockaddr
*dst
;
2382 rt
= rtalloc1(dst
, 1);
2386 /* If we didn't get a host route, allocate one */
2387 if ((rt
->rt_flags
& RTF_HOST
) == 0) {
2388 struct rtentry
*nrt
;
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
);
2399 nrt
->rt_rmx
= rt
->rt_rmx
;
2403 error
= rt_timer_add(rt
, icmp6_mtudisc_timeout
,
2404 icmp6_mtudisc_timeout_q
);
2410 return rt
; /* caller need to call rtfree() */
2414 icmp6_mtudisc_timeout(rt
, r
)
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);
2425 if ((rt
->rt_rmx
.rmx_locks
& RTV_MTU
) == 0) {
2426 rt
->rt_rmx
.rmx_mtu
= 0;
2430 #endif /*__NetBSD__ || __OpenBSD__*/
2434 icmp6_mtuexpire(rt
, rtt
)
2436 struct rttimer
*rtt
;
2438 rt
->rt_flags
|= RTF_PROBEMTU
;
2442 int *icmp6_sysvars
[] = ICMPV6CTL_VARS
;
2445 icmp6_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
2453 if (name
[0] >= ICMPV6CTL_MAXID
)
2454 return (EOPNOTSUPP
);
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. */
2464 case ICMPV6CTL_STATS
:
2465 return sysctl_rdtrunc(oldp
, oldlenp
, newp
, &icmp6stat
,
2469 return (sysctl_int_arr(icmp6_sysvars
, name
, namelen
,
2470 oldp
, oldlenp
, newp
, newlen
));
2475 #if defined(__NetBSD__) || defined(__OpenBSD__)
2477 #include <sys/sysctl.h>
2479 icmp6_sysctl(name
, namelen
, oldp
, oldlenp
, newp
, newlen
)
2488 /* All sysctl names at this level are terminal. */
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
,
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
,
2517 case ICMPV6CTL_NODEINFO
:
2518 return sysctl_int(oldp
, oldlenp
, newp
, newlen
, &icmp6_nodeinfo
);
2524 #endif /* __NetBSD__ */