2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/netinet6/icmp6.c,v 1.6.2.6 2001/07/10 09:44:16 ume Exp $ */
30 /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * 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. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * Copyright (c) 1982, 1986, 1988, 1993
63 * The Regents of the University of California. All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. All advertising materials mentioning features or use of this software
74 * must display the following acknowledgement:
75 * This product includes software developed by the University of
76 * California, Berkeley and its contributors.
77 * 4. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
97 #include <sys/param.h>
98 #include <sys/systm.h>
100 #include <sys/malloc.h>
101 #include <sys/mbuf.h>
102 #include <sys/protosw.h>
103 #include <sys/socket.h>
104 #include <sys/socketvar.h>
105 #include <sys/time.h>
106 #include <sys/kernel.h>
107 #include <sys/syslog.h>
108 #include <sys/domain.h>
111 #include <net/route.h>
112 #include <net/if_dl.h>
113 #include <net/if_types.h>
115 #include <netinet/in.h>
116 #include <netinet/in_var.h>
117 #include <netinet/ip6.h>
118 #include <netinet6/ip6_var.h>
119 #include <netinet/icmp6.h>
120 #include <netinet6/mld6_var.h>
121 #include <netinet/in_pcb.h>
122 #include <netinet6/in6_pcb.h>
123 #include <netinet6/nd6.h>
124 #include <netinet6/in6_ifattach.h>
125 #include <netinet6/ip6protosw.h>
126 #include <netinet6/scope6_var.h>
129 #include <netinet6/ipsec.h>
130 #include <netkey/key.h>
132 extern int ipsec_bypass
;
135 #include <net/net_osdep.h>
137 extern struct domain inet6domain
;
138 extern struct ip6protosw inet6sw
[];
139 extern struct ip6protosw
*ip6_protox
[];
141 extern uint32_t rip_sendspace
;
142 extern uint32_t rip_recvspace
;
144 struct icmp6stat icmp6stat
;
146 extern struct inpcbhead ripcb
;
147 extern int icmp6errppslim
;
148 static int icmp6errpps_count
= 0;
149 static struct timeval icmp6errppslim_last
;
150 extern int icmp6_nodeinfo
;
151 extern struct inpcbinfo ripcbinfo
;
152 extern lck_mtx_t
*inet6_domain_mutex
;
154 static void icmp6_errcount(struct icmp6errstat
*, int, int);
155 static int icmp6_rip6_input(struct mbuf
**, int);
156 static int icmp6_ratelimit(const struct in6_addr
*, const int, const int);
157 static const char *icmp6_redirect_diag(struct in6_addr
*,
158 struct in6_addr
*, struct in6_addr
*);
159 #ifndef HAVE_PPSRATECHECK
160 static int ppsratecheck(struct timeval
*, int *, int);
162 static struct mbuf
*ni6_input(struct mbuf
*, int);
163 static struct mbuf
*ni6_nametodns(const char *, int, int);
164 static int ni6_dnsmatch(const char *, int, const char *, int);
165 static int ni6_addrs(struct icmp6_nodeinfo
*,
166 struct ifnet
**, char *);
167 static int ni6_store_addrs(struct icmp6_nodeinfo
*, struct icmp6_nodeinfo
*,
168 struct ifnet
*, int);
169 static int icmp6_notify_error(struct mbuf
*, int, int, int);
180 icmp6_errcount(stat
, type
, code
)
181 struct icmp6errstat
*stat
;
185 case ICMP6_DST_UNREACH
:
187 case ICMP6_DST_UNREACH_NOROUTE
:
188 stat
->icp6errs_dst_unreach_noroute
++;
190 case ICMP6_DST_UNREACH_ADMIN
:
191 stat
->icp6errs_dst_unreach_admin
++;
193 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
194 stat
->icp6errs_dst_unreach_beyondscope
++;
196 case ICMP6_DST_UNREACH_ADDR
:
197 stat
->icp6errs_dst_unreach_addr
++;
199 case ICMP6_DST_UNREACH_NOPORT
:
200 stat
->icp6errs_dst_unreach_noport
++;
204 case ICMP6_PACKET_TOO_BIG
:
205 stat
->icp6errs_packet_too_big
++;
207 case ICMP6_TIME_EXCEEDED
:
209 case ICMP6_TIME_EXCEED_TRANSIT
:
210 stat
->icp6errs_time_exceed_transit
++;
212 case ICMP6_TIME_EXCEED_REASSEMBLY
:
213 stat
->icp6errs_time_exceed_reassembly
++;
217 case ICMP6_PARAM_PROB
:
219 case ICMP6_PARAMPROB_HEADER
:
220 stat
->icp6errs_paramprob_header
++;
222 case ICMP6_PARAMPROB_NEXTHEADER
:
223 stat
->icp6errs_paramprob_nextheader
++;
225 case ICMP6_PARAMPROB_OPTION
:
226 stat
->icp6errs_paramprob_option
++;
231 stat
->icp6errs_redirect
++;
234 stat
->icp6errs_unknown
++;
238 * A wrapper function for icmp6_error() necessary when the erroneous packet
239 * may not contain enough scope zone information.
242 icmp6_error2(struct mbuf
*m
, int type
, int code
, int param
,
250 #ifndef PULLDOWN_TEST
251 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
),return );
253 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
254 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
260 ip6
= mtod(m
, struct ip6_hdr
*);
262 if (in6_setscope(&ip6
->ip6_src
, ifp
, NULL
) != 0)
264 if (in6_setscope(&ip6
->ip6_dst
, ifp
, NULL
) != 0)
267 icmp6_error(m
, type
, code
, param
);
271 * Generate an error packet of type error in response to bad IP6 packet.
274 icmp6_error(struct mbuf
*m
, int type
, int code
, int param
)
276 struct ip6_hdr
*oip6
, *nip6
;
277 struct icmp6_hdr
*icmp6
;
282 icmp6stat
.icp6s_error
++;
284 /* count per-type-code statistics */
285 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, type
, code
);
287 #ifdef M_DECRYPTED /*not openbsd*/
288 if (m
->m_flags
& M_DECRYPTED
) {
289 icmp6stat
.icp6s_canterror
++;
294 #ifndef PULLDOWN_TEST
295 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return);
297 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
298 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
303 oip6
= mtod(m
, struct ip6_hdr
*);
306 * If the destination address of the erroneous packet is a multicast
307 * address, or the packet was sent using link-layer multicast,
308 * we should basically suppress sending an error (RFC 2463, Section
310 * We have two exceptions (the item e.2 in that section):
311 * - the Pakcet Too Big message can be sent for path MTU discovery.
312 * - the Parameter Problem Message that can be allowed an icmp6 error
313 * in the option type field. This check has been done in
314 * ip6_unknown_opt(), so we can just check the type and code.
316 if ((m
->m_flags
& (M_BCAST
|M_MCAST
) ||
317 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_dst
)) &&
318 (type
!= ICMP6_PACKET_TOO_BIG
&&
319 (type
!= ICMP6_PARAM_PROB
||
320 code
!= ICMP6_PARAMPROB_OPTION
)))
324 * RFC 2463, 2.4 (e.5): source address check.
325 * XXX: the case of anycast source?
327 if (IN6_IS_ADDR_UNSPECIFIED(&oip6
->ip6_src
) ||
328 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_src
))
332 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
336 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
337 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
338 struct icmp6_hdr
*icp
;
340 #ifndef PULLDOWN_TEST
341 IP6_EXTHDR_CHECK(m
, 0, off
+ sizeof(struct icmp6_hdr
), return);
342 icp
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
344 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
347 icmp6stat
.icp6s_tooshort
++;
351 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
352 icp
->icmp6_type
== ND_REDIRECT
) {
355 * Special case: for redirect (which is
356 * informational) we must not send icmp6 error.
358 icmp6stat
.icp6s_canterror
++;
361 /* ICMPv6 informational - send the error */
364 /* non-ICMPv6 - send the error */
367 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
369 /* Finally, do rate limitation check. */
370 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
371 icmp6stat
.icp6s_toofreq
++;
376 * OK, ICMP6 can be generated.
379 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
)
380 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
382 preplen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
383 M_PREPEND(m
, preplen
, M_DONTWAIT
);
384 if (m
&& m
->m_len
< preplen
)
385 m
= m_pullup(m
, preplen
);
387 nd6log((LOG_DEBUG
, "ENOBUFS in icmp6_error %d\n", __LINE__
));
391 nip6
= mtod(m
, struct ip6_hdr
*);
392 nip6
->ip6_src
= oip6
->ip6_src
;
393 nip6
->ip6_dst
= oip6
->ip6_dst
;
395 in6_clearscope(&oip6
->ip6_src
);
396 in6_clearscope(&oip6
->ip6_dst
);
398 icmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
399 icmp6
->icmp6_type
= type
;
400 icmp6
->icmp6_code
= code
;
401 icmp6
->icmp6_pptr
= htonl((u_int32_t
)param
);
404 * icmp6_reflect() is designed to be in the input path.
405 * icmp6_error() can be called from both input and output path,
406 * and if we are in output path rcvif could contain bogus value.
407 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
408 * information in ip header (nip6).
410 m
->m_pkthdr
.rcvif
= NULL
;
412 icmp6stat
.icp6s_outhist
[type
]++;
413 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /* header order: IPv6 - ICMPv6 */
419 * If we can't tell whether or not we can generate ICMP6, free it.
425 * Process a received ICMP6 message.
428 icmp6_input(struct mbuf
**mp
, int *offp
, int proto
)
430 #pragma unused(proto)
431 struct mbuf
*m
= *mp
, *n
;
433 struct ip6_hdr
*ip6
, *nip6
;
434 struct icmp6_hdr
*icmp6
, *nicmp6
;
436 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
439 ifp
= m
->m_pkthdr
.rcvif
;
441 #ifndef PULLDOWN_TEST
442 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_hdr
), return IPPROTO_DONE
);
443 /* m might change if M_LOOP. So, call mtod after this */
447 * Locate icmp6 structure in mbuf, and check
448 * that not corrupted and of at least minimum length
451 ip6
= mtod(m
, struct ip6_hdr
*);
452 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
453 icmp6stat
.icp6s_tooshort
++;
458 * Check multicast group membership.
459 * Note: SSM filters are not applied for ICMPv6 traffic.
461 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
462 struct in6_multi
*inm
;
464 in6_multihead_lock_shared();
465 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, inm
);
466 in6_multihead_lock_done();
469 ip6stat
.ip6s_notmember
++;
470 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_discard
);
478 * calculate the checksum
480 #ifndef PULLDOWN_TEST
481 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
483 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
485 icmp6stat
.icp6s_tooshort
++;
489 code
= icmp6
->icmp6_code
;
491 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
493 "ICMP6 checksum error(%d|%x) %s\n",
494 icmp6
->icmp6_type
, sum
, ip6_sprintf(&ip6
->ip6_src
)));
495 icmp6stat
.icp6s_checksum
++;
499 #if defined(NFAITH) && 0 < NFAITH
500 if (faithprefix(&ip6
->ip6_dst
)) {
502 * Deliver very specific ICMP6 type only.
503 * This is important to deliver TOOBIG. Otherwise PMTUD
506 switch (icmp6
->icmp6_type
) {
507 case ICMP6_DST_UNREACH
:
508 case ICMP6_PACKET_TOO_BIG
:
509 case ICMP6_TIME_EXCEEDED
:
517 icmp6stat
.icp6s_inhist
[icmp6
->icmp6_type
]++;
518 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_msg
);
519 if (icmp6
->icmp6_type
< ICMP6_INFOMSG_MASK
)
520 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_error
);
522 switch (icmp6
->icmp6_type
) {
523 case ICMP6_DST_UNREACH
:
524 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
526 case ICMP6_DST_UNREACH_NOROUTE
:
527 code
= PRC_UNREACH_NET
;
529 case ICMP6_DST_UNREACH_ADMIN
:
530 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_adminprohib
);
531 code
= PRC_UNREACH_PROTOCOL
; /* is this a good code? */
533 case ICMP6_DST_UNREACH_ADDR
:
536 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
537 /* I mean "source address was incorrect." */
538 code
= PRC_PARAMPROB
;
540 case ICMP6_DST_UNREACH_NOPORT
:
541 code
= PRC_UNREACH_PORT
;
549 case ICMP6_PACKET_TOO_BIG
:
550 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
555 * Updating the path MTU will be done after examining
556 * intermediate extension headers.
561 case ICMP6_TIME_EXCEEDED
:
562 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
564 case ICMP6_TIME_EXCEED_TRANSIT
:
565 code
= PRC_TIMXCEED_INTRANS
;
567 case ICMP6_TIME_EXCEED_REASSEMBLY
:
568 code
= PRC_TIMXCEED_REASS
;
576 case ICMP6_PARAM_PROB
:
577 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
579 case ICMP6_PARAMPROB_NEXTHEADER
:
580 code
= PRC_UNREACH_PROTOCOL
;
582 case ICMP6_PARAMPROB_HEADER
:
583 case ICMP6_PARAMPROB_OPTION
:
584 code
= PRC_PARAMPROB
;
592 case ICMP6_ECHO_REQUEST
:
593 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
597 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
598 icmp6stat
.icp6s_toofreq
++;
602 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
604 goto rate_limit_checked
;
607 if ((n
->m_flags
& M_EXT
) != 0
608 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
610 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
613 * Prepare an internal mbuf. m_pullup() doesn't
614 * always copy the length we specified.
616 if (maxlen
>= MCLBYTES
) {
619 goto rate_limit_checked
;
622 MGETHDR(n
, M_DONTWAIT
, n0
->m_type
); /* MAC-OK */
623 if (n
&& maxlen
>= MHLEN
) {
624 MCLGET(n
, M_DONTWAIT
);
625 if ((n
->m_flags
& M_EXT
) == 0) {
633 goto rate_limit_checked
;
636 M_COPY_PKTHDR(n
, n0
);
638 * Copy IPv6 and ICMPv6 only.
640 nip6
= mtod(n
, struct ip6_hdr
*);
641 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
642 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
643 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
644 noff
= sizeof(struct ip6_hdr
);
645 n
->m_pkthdr
.len
= n
->m_len
=
646 noff
+ sizeof(struct icmp6_hdr
);
648 * Adjust mbuf. ip6_plen will be adjusted in
651 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
652 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
654 n0
->m_flags
&= ~M_PKTHDR
;
656 nip6
= mtod(n
, struct ip6_hdr
*);
657 IP6_EXTHDR_GET(nicmp6
, struct icmp6_hdr
*, n
, off
,
661 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
662 nicmp6
->icmp6_code
= 0;
664 icmp6stat
.icp6s_reflect
++;
665 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
666 icmp6_reflect(n
, noff
);
668 goto rate_limit_checked
;
671 case ICMP6_ECHO_REPLY
:
672 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
677 case MLD_LISTENER_QUERY
:
678 case MLD_LISTENER_REPORT
:
680 if (icmp6len
< sizeof(struct mld_hdr
))
682 if (icmp6
->icmp6_type
== MLD_LISTENER_QUERY
) /* XXX: ugly... */
683 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
685 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
687 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
688 icmp6stat
.icp6s_toofreq
++;
692 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
694 if (mld_input(m
, off
, icmp6len
) == IPPROTO_DONE
)
698 if (mld_input(n
, off
, icmp6len
) != IPPROTO_DONE
)
701 goto rate_limit_checked
;
704 case MLD_LISTENER_DONE
:
705 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
706 if (icmp6len
< sizeof(struct mld_hdr
)) /* necessary? */
708 break; /* nothing to be done in kernel */
710 case MLD_MTRACE_RESP
:
712 /* XXX: these two are experimental. not officially defined. */
713 /* XXX: per-interface statistics? */
714 break; /* just pass it to applications */
719 //### LD 10/20 Check fbsd differences here. Not sure we're more advanced or not.
720 /* By RFC 4620 refuse to answer queries from global scope addresses */
721 if ((icmp6_nodeinfo
& 8) != 8 && in6_addrscope(&ip6
->ip6_src
) == IPV6_ADDR_SCOPE_GLOBAL
)
724 if (icmp6len
< sizeof(struct icmp6_nodeinfo
))
727 #ifndef PULLDOWN_TEST
728 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
729 return IPPROTO_DONE
);
731 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
732 icmp6stat
.icp6s_toofreq
++;
736 n
= m_copy(m
, 0, M_COPYALL
);
738 n
= ni6_input(n
, off
);
740 noff
= sizeof(struct ip6_hdr
);
741 icmp6stat
.icp6s_reflect
++;
742 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
743 icmp6_reflect(n
, noff
);
745 goto rate_limit_checked
;
753 case ND_ROUTER_SOLICIT
:
754 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
757 if (icmp6len
< sizeof(struct nd_router_solicit
))
760 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
761 icmp6stat
.icp6s_toofreq
++;
765 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
767 nd6_rs_input(m
, off
, icmp6len
);
771 nd6_rs_input(n
, off
, icmp6len
);
773 goto rate_limit_checked
;
776 case ND_ROUTER_ADVERT
:
777 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
780 if (icmp6len
< sizeof(struct nd_router_advert
))
783 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
784 icmp6stat
.icp6s_toofreq
++;
788 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
790 nd6_ra_input(m
, off
, icmp6len
);
794 nd6_ra_input(n
, off
, icmp6len
);
796 goto rate_limit_checked
;
799 case ND_NEIGHBOR_SOLICIT
:
800 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
803 if (icmp6len
< sizeof(struct nd_neighbor_solicit
))
806 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
807 icmp6stat
.icp6s_toofreq
++;
811 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
813 nd6_ns_input(m
, off
, icmp6len
);
817 nd6_ns_input(n
, off
, icmp6len
);
819 goto rate_limit_checked
;
822 case ND_NEIGHBOR_ADVERT
:
823 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
826 if (icmp6len
< sizeof(struct nd_neighbor_advert
))
829 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
830 icmp6stat
.icp6s_toofreq
++;
834 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
836 nd6_na_input(m
, off
, icmp6len
);
840 nd6_na_input(n
, off
, icmp6len
);
842 goto rate_limit_checked
;
846 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
849 if (icmp6len
< sizeof(struct nd_redirect
))
852 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
853 icmp6stat
.icp6s_toofreq
++;
857 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
859 icmp6_redirect_input(m
, off
);
863 icmp6_redirect_input(n
, off
);
865 goto rate_limit_checked
;
868 case ICMP6_ROUTER_RENUMBERING
:
869 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
870 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
)
872 if (icmp6len
< sizeof(struct icmp6_router_renum
))
877 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
878 icmp6stat
.icp6s_toofreq
++;
883 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
884 icmp6
->icmp6_type
, ip6_sprintf(&ip6
->ip6_src
),
885 ip6_sprintf(&ip6
->ip6_dst
),
886 m
->m_pkthdr
.rcvif
? m
->m_pkthdr
.rcvif
->if_index
: 0));
887 if (icmp6
->icmp6_type
< ICMP6_ECHO_REQUEST
) {
888 /* ICMPv6 error: MUST deliver it by spec... */
892 /* ICMPv6 informational: MUST not deliver */
893 goto rate_limit_checked
;
897 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
898 icmp6stat
.icp6s_toofreq
++;
902 if (icmp6_notify_error(m
, off
, icmp6len
, code
)) {
903 /* In this case, m should've been freed. */
904 return(IPPROTO_DONE
);
909 icmp6stat
.icp6s_badcode
++;
913 icmp6stat
.icp6s_badlen
++;
917 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
918 icmp6stat
.icp6s_toofreq
++;
922 /* deliver the packet to appropriate sockets */
923 icmp6_rip6_input(&m
, *offp
);
933 icmp6_notify_error(m
, off
, icmp6len
, code
)
935 int off
, icmp6len
, code
;
937 struct icmp6_hdr
*icmp6
;
938 struct ip6_hdr
*eip6
;
940 struct sockaddr_in6 icmp6src
, icmp6dst
;
942 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
943 icmp6stat
.icp6s_tooshort
++;
946 #ifndef PULLDOWN_TEST
947 IP6_EXTHDR_CHECK(m
, off
,
948 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
950 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
952 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
953 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
955 icmp6stat
.icp6s_tooshort
++;
959 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
961 /* Detect the upper level protocol */
963 void (*ctlfunc
)(int, struct sockaddr
*, void *);
964 u_int8_t nxt
= eip6
->ip6_nxt
;
965 int eoff
= off
+ sizeof(struct icmp6_hdr
) +
966 sizeof(struct ip6_hdr
);
967 struct ip6ctlparam ip6cp
;
968 struct in6_addr
*finaldst
= NULL
;
969 int icmp6type
= icmp6
->icmp6_type
;
971 struct ip6_rthdr
*rth
;
972 struct ip6_rthdr0
*rth0
;
975 while (1) { /* XXX: should avoid infinite loop explicitly? */
979 case IPPROTO_HOPOPTS
:
980 case IPPROTO_DSTOPTS
:
982 #ifndef PULLDOWN_TEST
983 IP6_EXTHDR_CHECK(m
, 0, eoff
+
984 sizeof(struct ip6_ext
),
986 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
989 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
992 icmp6stat
.icp6s_tooshort
++;
997 if (nxt
== IPPROTO_AH
)
998 eoff
+= (eh
->ip6e_len
+ 2) << 2;
1000 eoff
+= (eh
->ip6e_len
+ 1) << 3;
1003 case IPPROTO_ROUTING
:
1005 * When the erroneous packet contains a
1006 * routing header, we should examine the
1007 * header to determine the final destination.
1008 * Otherwise, we can't properly update
1009 * information that depends on the final
1010 * destination (e.g. path MTU).
1012 #ifndef PULLDOWN_TEST
1013 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
1015 rth
= (struct ip6_rthdr
*)(mtod(m
, caddr_t
)
1018 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
1019 eoff
, sizeof(*rth
));
1021 icmp6stat
.icp6s_tooshort
++;
1025 rthlen
= (rth
->ip6r_len
+ 1) << 3;
1027 * XXX: currently there is no
1028 * officially defined type other
1030 * Note that if the segment left field
1031 * is 0, all intermediate hops must
1034 if (rth
->ip6r_segleft
&&
1035 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
1038 #ifndef PULLDOWN_TEST
1039 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
1041 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
1043 IP6_EXTHDR_GET(rth0
,
1044 struct ip6_rthdr0
*, m
,
1047 icmp6stat
.icp6s_tooshort
++;
1051 /* just ignore a bogus header */
1052 if ((rth0
->ip6r0_len
% 2) == 0 &&
1053 (hops
= rth0
->ip6r0_len
/2))
1054 finaldst
= (struct in6_addr
*)(rth0
+ 1) + (hops
- 1);
1057 nxt
= rth
->ip6r_nxt
;
1059 case IPPROTO_FRAGMENT
:
1060 #ifndef PULLDOWN_TEST
1061 IP6_EXTHDR_CHECK(m
, 0, eoff
+
1062 sizeof(struct ip6_frag
),
1064 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
1067 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
1070 icmp6stat
.icp6s_tooshort
++;
1075 * Data after a fragment header is meaningless
1076 * unless it is the first fragment, but
1077 * we'll go to the notify label for path MTU
1080 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
)
1083 eoff
+= sizeof(struct ip6_frag
);
1088 * This case includes ESP and the No Next
1089 * Header. In such cases going to the notify
1090 * label does not have any meaning
1091 * (i.e. ctlfunc will be NULL), but we go
1092 * anyway since we might have to update
1093 * path MTU information.
1099 #ifndef PULLDOWN_TEST
1100 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
1102 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
1103 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
1104 if (icmp6
== NULL
) {
1105 icmp6stat
.icp6s_tooshort
++;
1111 * retrieve parameters from the inner IPv6 header, and convert
1112 * them into sockaddr structures.
1113 * XXX: there is no guarantee that the source or destination
1114 * addresses of the inner packet are in the same scope as
1115 * the addresses of the icmp packet. But there is no other
1116 * way to determine the zone.
1118 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1120 bzero(&icmp6dst
, sizeof(icmp6dst
));
1121 icmp6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
1122 icmp6dst
.sin6_family
= AF_INET6
;
1123 if (finaldst
== NULL
)
1124 icmp6dst
.sin6_addr
= eip6
->ip6_dst
;
1126 icmp6dst
.sin6_addr
= *finaldst
;
1127 if (in6_setscope(&icmp6dst
.sin6_addr
, m
->m_pkthdr
.rcvif
, NULL
))
1129 bzero(&icmp6src
, sizeof(icmp6src
));
1130 icmp6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1131 icmp6src
.sin6_family
= AF_INET6
;
1132 icmp6src
.sin6_addr
= eip6
->ip6_src
;
1133 if (in6_setscope(&icmp6src
.sin6_addr
, m
->m_pkthdr
.rcvif
, NULL
))
1135 icmp6src
.sin6_flowinfo
=
1136 (eip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
);
1138 if (finaldst
== NULL
)
1139 finaldst
= &eip6
->ip6_dst
;
1141 ip6cp
.ip6c_icmp6
= icmp6
;
1142 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1143 ip6cp
.ip6c_off
= eoff
;
1144 ip6cp
.ip6c_finaldst
= finaldst
;
1145 ip6cp
.ip6c_src
= &icmp6src
;
1146 ip6cp
.ip6c_nxt
= nxt
;
1148 if (icmp6type
== ICMP6_PACKET_TOO_BIG
) {
1149 notifymtu
= ntohl(icmp6
->icmp6_mtu
);
1150 ip6cp
.ip6c_cmdarg
= (void *)¬ifymtu
;
1151 icmp6_mtudisc_update(&ip6cp
, 1); /*XXX*/
1154 ctlfunc
= (void (*)(int, struct sockaddr
*, void *))
1155 (ip6_protox
[nxt
]->pr_ctlinput
);
1157 (void) (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6dst
,
1169 icmp6_mtudisc_update(ip6cp
, validated
)
1170 struct ip6ctlparam
*ip6cp
;
1173 struct in6_addr
*dst
= ip6cp
->ip6c_finaldst
;
1174 struct icmp6_hdr
*icmp6
= ip6cp
->ip6c_icmp6
;
1175 struct mbuf
*m
= ip6cp
->ip6c_m
; /* will be necessary for scope issue */
1176 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1177 struct rtentry
*rt
= NULL
;
1178 struct sockaddr_in6 sin6
;
1180 * we reject ICMPv6 too big with abnormally small value.
1181 * XXX what is the good definition of "abnormally small"?
1183 if (mtu
< sizeof(struct ip6_hdr
) + sizeof(struct ip6_frag
) + 8)
1190 * In case the suggested mtu is less than IPV6_MMTU, we
1191 * only need to remember that it was for above mentioned
1192 * "alwaysfrag" case.
1193 * Try to be as close to the spec as possible.
1195 if (mtu
< IPV6_MMTU
)
1196 mtu
= IPV6_MMTU
- 8;
1198 bzero(&sin6
, sizeof(sin6
));
1199 sin6
.sin6_family
= PF_INET6
;
1200 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1201 sin6
.sin6_addr
= *dst
;
1202 /* XXX normally, this won't happen */
1203 if (IN6_IS_ADDR_LINKLOCAL(dst
)) {
1204 sin6
.sin6_addr
.s6_addr16
[1] =
1205 htons(m
->m_pkthdr
.rcvif
->if_index
);
1207 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1208 rt
= rtalloc1_scoped((struct sockaddr
*)&sin6
, 0,
1209 RTF_CLONING
| RTF_PRCLONING
, m
->m_pkthdr
.rcvif
->if_index
);
1212 if ((rt
->rt_flags
& RTF_HOST
) &&
1213 !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) &&
1214 mtu
< IN6_LINKMTU(rt
->rt_ifp
) &&
1215 rt
->rt_rmx
.rmx_mtu
> mtu
) {
1216 icmp6stat
.icp6s_pmtuchg
++;
1217 rt
->rt_rmx
.rmx_mtu
= mtu
;
1225 * Process a Node Information Query packet, based on
1226 * draft-ietf-ipngwg-icmp-name-lookups-07.
1228 * Spec incompatibilities:
1229 * - IPv6 Subject address handling
1230 * - IPv4 Subject address handling support missing
1231 * - Proxy reply (answer even if it's not for me)
1232 * - joins NI group address at in6_ifattach() time only, does not cope
1233 * with hostname changes by sethostname(3)
1235 #define hostnamelen strlen(hostname)
1236 static struct mbuf
*
1241 struct icmp6_nodeinfo
*ni6
, *nni6
;
1242 struct mbuf
*n
= NULL
;
1245 int replylen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1246 struct ni_reply_fqdn
*fqdn
;
1247 int addrs
; /* for NI_QTYPE_NODEADDR */
1248 struct ifnet
*ifp
= NULL
; /* for NI_QTYPE_NODEADDR */
1249 struct sockaddr_in6 sin6
; /* double meaning; ip6_dst and subjectaddr */
1250 struct sockaddr_in6 sin6_d
; /* XXX: we should retrieve this from m_aux */
1251 struct ip6_hdr
*ip6
;
1252 int oldfqdn
= 0; /* if 1, return pascal string (03 draft) */
1255 ip6
= mtod(m
, struct ip6_hdr
*);
1256 #ifndef PULLDOWN_TEST
1257 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1259 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1261 /* m is already reclaimed */
1267 * Validate IPv6 source address.
1268 * The default configuration MUST be to refuse answering queries from
1269 * global-scope addresses according to RFC4602.
1271 * - it's not very clear what "refuse" means; this implementation
1273 * - it's not very easy to identify global-scope (unicast) addresses
1274 * since there are many prefixes for them. It should be safer
1275 * and in practice sufficient to check "all" but loopback and
1276 * link-local (note that site-local unicast was deprecated and
1277 * ULA is defined as global scope-wise)
1279 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_GLOBALOK
) == 0 &&
1280 !IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) &&
1281 !IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_src
))
1285 * Validate IPv6 destination address.
1287 * The Responder must discard the Query without further processing
1288 * unless it is one of the Responder's unicast or anycast addresses, or
1289 * a link-local scope multicast address which the Responder has joined.
1290 * [RFC4602, Section 5.]
1292 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1293 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6
->ip6_dst
))
1295 /* else it's a link-local multicast, fine */
1296 } else { /* unicast or anycast */
1297 struct in6_ifaddr
*ia6
;
1299 if ((ia6
= ip6_getdstifaddr(m
)) == NULL
)
1300 goto bad
; /* XXX impossible */
1302 IFA_LOCK(&ia6
->ia_ifa
);
1303 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
1304 !(icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
)) {
1305 nd6log((LOG_DEBUG
, "ni6_input: ignore node info to "
1306 "a temporary address in %s:%d",
1307 __FILE__
, __LINE__
));
1308 IFA_UNLOCK(&ia6
->ia_ifa
);
1309 IFA_REMREF(&ia6
->ia_ifa
);
1312 IFA_UNLOCK(&ia6
->ia_ifa
);
1313 IFA_REMREF(&ia6
->ia_ifa
);
1316 /* validate query Subject field. */
1317 qtype
= ntohs(ni6
->ni_qtype
);
1318 subjlen
= m
->m_pkthdr
.len
- off
- sizeof(struct icmp6_nodeinfo
);
1321 case NI_QTYPE_SUPTYPES
:
1323 if (ni6
->ni_code
== ICMP6_NI_SUBJ_FQDN
&& subjlen
== 0)
1327 case NI_QTYPE_NODEADDR
:
1328 case NI_QTYPE_IPV4ADDR
:
1329 switch (ni6
->ni_code
) {
1330 case ICMP6_NI_SUBJ_IPV6
:
1331 #if ICMP6_NI_SUBJ_IPV6 != 0
1335 * backward compatibility - try to accept 03 draft
1336 * format, where no Subject is present.
1338 if (qtype
== NI_QTYPE_FQDN
&& ni6
->ni_code
== 0 &&
1343 #if ICMP6_NI_SUBJ_IPV6 != 0
1344 if (ni6
->ni_code
!= ICMP6_NI_SUBJ_IPV6
)
1348 if (subjlen
!= sizeof(struct in6_addr
))
1352 * Validate Subject address.
1354 * Not sure what exactly "address belongs to the node"
1355 * means in the spec, is it just unicast, or what?
1357 * At this moment we consider Subject address as
1358 * "belong to the node" if the Subject address equals
1359 * to the IPv6 destination address; validation for
1360 * IPv6 destination address should have done enough
1363 * We do not do proxy at this moment.
1365 /* m_pulldown instead of copy? */
1366 m_copydata(m
, off
+ sizeof(struct icmp6_nodeinfo
),
1367 subjlen
, (caddr_t
)&sin6
.sin6_addr
);
1368 sin6
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1370 in6_embedscope(&sin6
.sin6_addr
, &sin6
, NULL
, NULL
,
1372 bzero(&sin6_d
, sizeof(sin6_d
));
1373 sin6_d
.sin6_family
= AF_INET6
; /* not used, actually */
1374 sin6_d
.sin6_len
= sizeof(sin6_d
); /* ditto */
1375 sin6_d
.sin6_addr
= ip6
->ip6_dst
;
1376 sin6_d
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1378 in6_embedscope(&sin6_d
.sin6_addr
, &sin6_d
, NULL
, NULL
,
1380 subj
= (char *)&sin6
;
1381 if (SA6_ARE_ADDR_EQUAL(&sin6
, &sin6_d
))
1385 * XXX if we are to allow other cases, we should really
1386 * be careful about scope here.
1387 * basically, we should disallow queries toward IPv6
1388 * destination X with subject Y,
1389 * if scope(X) > scope(Y).
1390 * if we allow scope(X) > scope(Y), it will result in
1391 * information leakage across scope boundary.
1395 case ICMP6_NI_SUBJ_FQDN
:
1397 * Validate Subject name with gethostname(3).
1399 * The behavior may need some debate, since:
1400 * - we are not sure if the node has FQDN as
1401 * hostname (returned by gethostname(3)).
1402 * - the code does wildcard match for truncated names.
1403 * however, we are not sure if we want to perform
1404 * wildcard match, if gethostname(3) side has
1405 * truncated hostname.
1407 n
= ni6_nametodns(hostname
, hostnamelen
, 0);
1408 if (!n
|| n
->m_next
|| n
->m_len
== 0)
1410 IP6_EXTHDR_GET(subj
, char *, m
,
1411 off
+ sizeof(struct icmp6_nodeinfo
), subjlen
);
1414 if (!ni6_dnsmatch(subj
, subjlen
, mtod(n
, const char *),
1422 case ICMP6_NI_SUBJ_IPV4
: /* XXX: to be implemented? */
1429 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1432 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_FQDNOK
) == 0)
1435 case NI_QTYPE_NODEADDR
:
1436 case NI_QTYPE_IPV4ADDR
:
1437 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_NODEADDROK
) == 0)
1442 /* guess reply length */
1445 break; /* no reply data */
1446 case NI_QTYPE_SUPTYPES
:
1447 replylen
+= sizeof(u_int32_t
);
1450 /* XXX will append an mbuf */
1451 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1453 case NI_QTYPE_NODEADDR
:
1454 addrs
= ni6_addrs(ni6
, &ifp
, subj
);
1455 if ((replylen
+= addrs
* (sizeof(struct in6_addr
) +
1456 sizeof(u_int32_t
))) > MCLBYTES
)
1457 replylen
= MCLBYTES
; /* XXX: will truncate pkt later */
1459 case NI_QTYPE_IPV4ADDR
:
1460 /* unsupported - should respond with unknown Qtype? */
1464 * XXX: We must return a reply with the ICMP6 code
1465 * `unknown Qtype' in this case. However we regard the case
1466 * as an FQDN query for backward compatibility.
1467 * Older versions set a random value to this field,
1468 * so it rarely varies in the defined qtypes.
1469 * But the mechanism is not reliable...
1470 * maybe we should obsolete older versions.
1472 qtype
= NI_QTYPE_FQDN
;
1473 /* XXX will append an mbuf */
1474 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1479 /* allocate an mbuf to reply. */
1480 MGETHDR(n
, M_DONTWAIT
, m
->m_type
); /* MAC-OK */
1487 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1488 if (replylen
> MHLEN
) {
1489 if (replylen
> MCLBYTES
) {
1491 * XXX: should we try to allocate more? But MCLBYTES
1492 * is probably much larger than IPV6_MMTU...
1496 MCLGET(n
, M_DONTWAIT
);
1497 if ((n
->m_flags
& M_EXT
) == 0) {
1501 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
1503 /* copy mbuf header and IPv6 + Node Information base headers */
1504 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), sizeof(struct ip6_hdr
));
1505 nni6
= (struct icmp6_nodeinfo
*)(mtod(n
, struct ip6_hdr
*) + 1);
1506 bcopy((caddr_t
)ni6
, (caddr_t
)nni6
, sizeof(struct icmp6_nodeinfo
));
1508 /* qtype dependent procedure */
1511 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1514 case NI_QTYPE_SUPTYPES
:
1517 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1518 nni6
->ni_flags
= htons(0x0000); /* raw bitmap */
1519 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1520 v
= (u_int32_t
)htonl(0x0000000f);
1521 bcopy(&v
, nni6
+ 1, sizeof(u_int32_t
));
1525 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1526 fqdn
= (struct ni_reply_fqdn
*)(mtod(n
, caddr_t
) +
1527 sizeof(struct ip6_hdr
) +
1528 sizeof(struct icmp6_nodeinfo
));
1529 nni6
->ni_flags
= 0; /* XXX: meaningless TTL */
1530 fqdn
->ni_fqdn_ttl
= 0; /* ditto. */
1532 * XXX do we really have FQDN in variable "hostname"?
1534 n
->m_next
= ni6_nametodns(hostname
, hostnamelen
, oldfqdn
);
1535 if (n
->m_next
== NULL
)
1537 /* XXX we assume that n->m_next is not a chain */
1538 if (n
->m_next
->m_next
!= NULL
)
1540 n
->m_pkthdr
.len
+= n
->m_next
->m_len
;
1542 case NI_QTYPE_NODEADDR
:
1546 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1547 n
->m_pkthdr
.len
= n
->m_len
=
1548 sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1549 lenlim
= M_TRAILINGSPACE(n
);
1550 copied
= ni6_store_addrs(ni6
, nni6
, ifp
, lenlim
);
1551 /* XXX: reset mbuf length */
1552 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
1553 sizeof(struct icmp6_nodeinfo
) + copied
;
1557 break; /* XXX impossible! */
1560 nni6
->ni_type
= ICMP6_NI_REPLY
;
1577 * make a mbuf with DNS-encoded string. no compression support.
1579 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1580 * treated as truncated name (two \0 at the end). this is a wild guess.
1582 static struct mbuf
*
1583 ni6_nametodns(name
, namelen
, old
)
1586 int old
; /* return pascal string if non-zero */
1598 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1599 MGET(m
, M_DONTWAIT
, MT_DATA
);
1600 if (m
&& len
> MLEN
) {
1601 MCLGET(m
, M_DONTWAIT
);
1602 if ((m
->m_flags
& M_EXT
) == 0)
1611 *mtod(m
, char *) = namelen
;
1612 bcopy(name
, mtod(m
, char *) + 1, namelen
);
1616 cp
= mtod(m
, char *);
1617 ep
= mtod(m
, char *) + M_TRAILINGSPACE(m
);
1619 /* if not certain about my name, return empty buffer */
1624 * guess if it looks like shortened hostname, or FQDN.
1625 * shortened hostname needs two trailing "\0".
1628 for (p
= name
; p
< name
+ namelen
; p
++) {
1629 if (*p
&& *p
== '.')
1638 while (cp
< ep
&& p
< name
+ namelen
) {
1640 for (q
= p
; q
< name
+ namelen
&& *q
&& *q
!= '.'; q
++)
1642 /* result does not fit into mbuf */
1643 if (cp
+ i
+ 1 >= ep
)
1646 * DNS label length restriction, RFC1035 page 8.
1647 * "i == 0" case is included here to avoid returning
1648 * 0-length label on "foo..bar".
1650 if (i
<= 0 || i
>= 64)
1656 if (p
< name
+ namelen
&& *p
== '.')
1660 if (cp
+ nterm
>= ep
)
1664 m
->m_len
= cp
- mtod(m
, char *);
1668 panic("should not reach here");
1678 * check if two DNS-encoded string matches. takes care of truncated
1679 * form (with \0\0 at the end). no compression support.
1680 * XXX upper/lowercase match (see RFC2065)
1683 ni6_dnsmatch(a
, alen
, b
, blen
)
1689 const char *a0
, *b0
;
1692 /* simplest case - need validation? */
1693 if (alen
== blen
&& bcmp(a
, b
, alen
) == 0)
1699 /* termination is mandatory */
1700 if (alen
< 2 || blen
< 2)
1702 if (a0
[alen
- 1] != '\0' || b0
[blen
- 1] != '\0')
1707 while (a
- a0
< alen
&& b
- b0
< blen
) {
1708 if (a
- a0
+ 1 > alen
|| b
- b0
+ 1 > blen
)
1711 if ((signed char)a
[0] < 0 || (signed char)b
[0] < 0)
1713 /* we don't support compression yet */
1714 if (a
[0] >= 64 || b
[0] >= 64)
1717 /* truncated case */
1718 if (a
[0] == 0 && a
- a0
== alen
- 1)
1720 if (b
[0] == 0 && b
- b0
== blen
- 1)
1722 if (a
[0] == 0 || b
[0] == 0)
1728 if (a
- a0
+ 1 + l
> alen
|| b
- b0
+ 1 + l
> blen
)
1730 if (bcmp(a
+ 1, b
+ 1, l
) != 0)
1737 if (a
- a0
== alen
&& b
- b0
== blen
)
1744 * calculate the number of addresses to be returned in the node info reply.
1747 ni6_addrs(ni6
, ifpp
, subj
)
1748 struct icmp6_nodeinfo
*ni6
;
1749 struct ifnet
**ifpp
;
1753 struct in6_ifaddr
*ifa6
;
1755 struct sockaddr_in6
*subj_ip6
= NULL
; /* XXX pedant */
1756 int addrs
= 0, addrsofif
, iffound
= 0;
1757 int niflags
= ni6
->ni_flags
;
1762 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0) {
1763 switch (ni6
->ni_code
) {
1764 case ICMP6_NI_SUBJ_IPV6
:
1765 if (subj
== NULL
) /* must be impossible... */
1767 subj_ip6
= (struct sockaddr_in6
*)subj
;
1771 * XXX: we only support IPv6 subject address for
1778 ifnet_head_lock_shared();
1779 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
1781 ifnet_lock_shared(ifp
);
1782 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1785 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1789 ifa6
= (struct in6_ifaddr
*)ifa
;
1791 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0 &&
1792 IN6_ARE_ADDR_EQUAL(&subj_ip6
->sin6_addr
,
1793 &ifa6
->ia_addr
.sin6_addr
))
1797 * IPv4-mapped addresses can only be returned by a
1798 * Node Information proxy, since they represent
1799 * addresses of IPv4-only nodes, which perforce do
1800 * not implement this protocol.
1801 * [icmp-name-lookups-07, Section 5.4]
1802 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1803 * this function at this moment.
1806 /* What do we have to do about ::1? */
1807 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1808 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1809 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0) {
1814 case IPV6_ADDR_SCOPE_SITELOCAL
:
1815 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0) {
1820 case IPV6_ADDR_SCOPE_GLOBAL
:
1821 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0) {
1832 * check if anycast is okay.
1833 * XXX: just experimental. not in the spec.
1835 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1836 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0) {
1838 continue; /* we need only unicast addresses */
1840 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1841 (icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
) == 0) {
1845 addrsofif
++; /* count the address */
1848 ifnet_lock_done(ifp
);
1852 ifnet_reference(ifp
);
1866 ni6_store_addrs(ni6
, nni6
, ifp0
, resid
)
1867 struct icmp6_nodeinfo
*ni6
, *nni6
;
1871 struct ifnet
*ifp
= ifp0
;
1872 struct in6_ifaddr
*ifa6
;
1874 struct ifnet
*ifp_dep
= NULL
;
1875 int copied
= 0, allow_deprecated
= 0;
1876 u_char
*cp
= (u_char
*)(nni6
+ 1);
1877 int niflags
= ni6
->ni_flags
;
1879 struct timeval timenow
;
1881 getmicrotime(&timenow
);
1883 if (ifp0
== NULL
&& !(niflags
& NI_NODEADDR_FLAG_ALL
))
1884 return (0); /* needless to copy */
1888 ifnet_head_lock_shared();
1890 ifp
= TAILQ_FIRST(&ifnet_head
);
1892 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1893 ifnet_lock_shared(ifp
);
1894 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1895 ifa
= ifa
->ifa_list
.tqe_next
) {
1897 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1901 ifa6
= (struct in6_ifaddr
*)ifa
;
1903 if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0 &&
1904 allow_deprecated
== 0) {
1906 * prefererred address should be put before
1907 * deprecated addresses.
1910 /* record the interface for later search */
1911 if (ifp_dep
== NULL
)
1916 } else if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) == 0 &&
1917 allow_deprecated
!= 0) {
1919 continue; /* we now collect deprecated addrs */
1921 /* What do we have to do about ::1? */
1922 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1923 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1924 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0) {
1929 case IPV6_ADDR_SCOPE_SITELOCAL
:
1930 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0) {
1935 case IPV6_ADDR_SCOPE_GLOBAL
:
1936 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0) {
1947 * check if anycast is okay.
1948 * XXX: just experimental. not in the spec.
1950 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1951 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0) {
1955 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1956 (icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
) == 0) {
1961 /* now we can copy the address */
1962 if (resid
< sizeof(struct in6_addr
) +
1963 sizeof(u_int32_t
)) {
1966 * We give up much more copy.
1967 * Set the truncate flag and return.
1970 NI_NODEADDR_FLAG_TRUNCATE
;
1971 ifnet_lock_done(ifp
);
1977 * Set the TTL of the address.
1978 * The TTL value should be one of the following
1979 * according to the specification:
1981 * 1. The remaining lifetime of a DHCP lease on the
1983 * 2. The remaining Valid Lifetime of a prefix from
1984 * which the address was derived through Stateless
1985 * Autoconfiguration.
1987 * Note that we currently do not support stateful
1988 * address configuration by DHCPv6, so the former
1989 * case can't happen.
1991 if (ifa6
->ia6_lifetime
.ia6t_expire
== 0 &&
1992 (ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) == 0)
1993 ltime
= ND6_INFINITE_LIFETIME
;
1995 if (ifa6
->ia6_lifetime
.ia6t_expire
>
1997 ltime
= htonl(ifa6
->ia6_lifetime
.ia6t_expire
- timenow
.tv_sec
);
2002 bcopy(<ime
, cp
, sizeof(u_int32_t
));
2003 cp
+= sizeof(u_int32_t
);
2005 /* copy the address itself */
2006 bcopy(&ifa6
->ia_addr
.sin6_addr
, cp
,
2007 sizeof(struct in6_addr
));
2008 /* XXX: KAME link-local hack; remove ifindex */
2009 if (IN6_IS_ADDR_LINKLOCAL(&ifa6
->ia_addr
.sin6_addr
))
2010 ((struct in6_addr
*)cp
)->s6_addr16
[1] = 0;
2011 cp
+= sizeof(struct in6_addr
);
2013 resid
-= (sizeof(struct in6_addr
) + sizeof(u_int32_t
));
2014 copied
+= (sizeof(struct in6_addr
) +
2018 ifnet_lock_done(ifp
);
2019 if (ifp0
) /* we need search only on the specified IF */
2024 if (allow_deprecated
== 0 && ifp_dep
!= NULL
) {
2026 allow_deprecated
= 1;
2035 * XXX almost dup'ed code with rip6_input.
2038 icmp6_rip6_input(mp
, off
)
2042 struct mbuf
*m
= *mp
;
2043 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2044 struct in6pcb
*in6p
;
2045 struct in6pcb
*last
= NULL
;
2046 struct sockaddr_in6 rip6src
;
2047 struct icmp6_hdr
*icmp6
;
2048 struct mbuf
*opts
= NULL
;
2051 #ifndef PULLDOWN_TEST
2052 /* this is assumed to be safe. */
2053 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
2055 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
2056 if (icmp6
== NULL
) {
2057 /* m is already reclaimed */
2058 return IPPROTO_DONE
;
2063 * XXX: the address may have embedded scope zone ID, which should be
2064 * hidden from applications.
2066 bzero(&rip6src
, sizeof(rip6src
));
2067 rip6src
.sin6_family
= AF_INET6
;
2068 rip6src
.sin6_len
= sizeof(struct sockaddr_in6
);
2069 rip6src
.sin6_addr
= ip6
->ip6_src
;
2070 if (sa6_recoverscope(&rip6src
))
2071 return (IPPROTO_DONE
);
2073 lck_rw_lock_shared(ripcbinfo
.mtx
);
2074 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
2076 if ((in6p
->inp_vflag
& INP_IPV6
) == 0)
2078 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
)
2080 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
2081 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
))
2083 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
2084 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
))
2086 if (in6p
->in6p_icmp6filt
2087 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
2088 in6p
->in6p_icmp6filt
))
2092 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
2093 if ((last
->in6p_flags
& IN6P_CONTROLOPTS
) != 0 ||
2094 (last
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
2095 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
2096 ret
= ip6_savecontrol(last
, n
, &opts
);
2104 /* strip intermediate headers */
2106 so_recv_data_stat(last
->in6p_socket
, m
, 0);
2107 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
2108 (struct sockaddr
*)&rip6src
,
2109 n
, opts
, NULL
) != 0) {
2110 sorwakeup(last
->in6p_socket
);
2118 if ((last
->in6p_flags
& INP_CONTROLOPTS
) != 0 ||
2119 (last
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
2120 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
2121 ret
= ip6_savecontrol(last
, m
, &opts
);
2126 /* strip intermediate headers */
2128 so_recv_data_stat(last
->in6p_socket
, m
, 0);
2129 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
2130 (struct sockaddr
*)&rip6src
, m
, opts
, NULL
) != 0) {
2131 sorwakeup(last
->in6p_socket
);
2136 lck_rw_done(ripcbinfo
.mtx
);
2137 return IPPROTO_DONE
;
2140 lck_rw_done(ripcbinfo
.mtx
);
2143 ip6stat
.ip6s_delivered
--;
2144 return IPPROTO_DONE
;
2149 * Reflect the ip6 packet back to the source.
2150 * OFF points to the icmp6 header, counted from the top of the mbuf.
2153 icmp6_reflect(m
, off
)
2157 struct ip6_hdr
*ip6
;
2158 struct icmp6_hdr
*icmp6
;
2159 struct in6_ifaddr
*ia
;
2160 struct in6_addr t
, src_storage
, *src
= 0;
2163 struct ifnet
*outif
= NULL
;
2164 struct sockaddr_in6 sa6_src
, sa6_dst
;
2166 struct ip6_out_args ip6oa
= { IFSCOPE_NONE
, 0 };
2168 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.rcvif
!= NULL
)
2169 ip6oa
.ip6oa_boundif
= m
->m_pkthdr
.rcvif
->if_index
;
2171 /* too short to reflect */
2172 if (off
< sizeof(struct ip6_hdr
)) {
2174 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2175 (u_int32_t
)off
, (u_int32_t
)sizeof(struct ip6_hdr
),
2176 __FILE__
, __LINE__
));
2181 * If there are extra headers between IPv6 and ICMPv6, strip
2182 * off that header first.
2185 if (sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) > MHLEN
)
2186 panic("assumption failed in icmp6_reflect");
2188 if (off
> sizeof(struct ip6_hdr
)) {
2190 struct ip6_hdr nip6
;
2192 l
= off
- sizeof(struct ip6_hdr
);
2193 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
2195 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2197 if ((m
= m_pullup(m
, l
)) == NULL
)
2200 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
2201 } else /* off == sizeof(struct ip6_hdr) */ {
2203 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2205 if ((m
= m_pullup(m
, l
)) == NULL
)
2209 plen
= m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2210 ip6
= mtod(m
, struct ip6_hdr
*);
2211 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2212 icmp6
= (struct icmp6_hdr
*)(ip6
+ 1);
2213 type
= icmp6
->icmp6_type
; /* keep type for statistics */
2214 code
= icmp6
->icmp6_code
; /* ditto. */
2218 * ip6_input() drops a packet if its src is multicast.
2219 * So, the src is never multicast.
2221 ip6
->ip6_dst
= ip6
->ip6_src
;
2224 * XXX: make sure to embed scope zone information, using
2225 * already embedded IDs or the received interface (if any).
2226 * Note that rcvif may be NULL.
2228 bzero(&sa6_src
, sizeof(sa6_src
));
2229 sa6_src
.sin6_family
= AF_INET6
;
2230 sa6_src
.sin6_len
= sizeof(sa6_src
);
2231 sa6_src
.sin6_addr
= ip6
->ip6_dst
;
2232 in6_recoverscope(&sa6_src
, &ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
);
2233 in6_embedscope(&ip6
->ip6_dst
, &sa6_src
, NULL
, NULL
, NULL
);
2234 bzero(&sa6_dst
, sizeof(sa6_dst
));
2235 sa6_dst
.sin6_family
= AF_INET6
;
2236 sa6_dst
.sin6_len
= sizeof(sa6_dst
);
2237 sa6_dst
.sin6_addr
= t
;
2238 in6_recoverscope(&sa6_dst
, &t
, m
->m_pkthdr
.rcvif
);
2239 in6_embedscope(&t
, &sa6_dst
, NULL
, NULL
, NULL
);
2242 * If the incoming packet was addressed directly to us(i.e. unicast),
2243 * use dst as the src for the reply.
2244 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2245 * (for example) when we encounter an error while forwarding procedure
2246 * destined to a duplicated address of ours.
2247 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2248 * procedure of an outgoing packet of our own, in which case we need
2249 * to search in the ifaddr list.
2251 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2252 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2253 IFA_LOCK(&ia
->ia_ifa
);
2254 if (IN6_ARE_ADDR_EQUAL(&t
, &ia
->ia_addr
.sin6_addr
) &&
2255 (ia
->ia6_flags
& (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
)) == 0) {
2256 IFA_UNLOCK(&ia
->ia_ifa
);
2260 IFA_UNLOCK(&ia
->ia_ifa
);
2262 lck_rw_done(&in6_ifaddr_rwlock
);
2263 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) && (m
->m_flags
& M_LOOP
)) {
2265 * This is the case if the dst is our link-local address
2266 * and the sender is also ourselves.
2273 struct sockaddr_in6 sin6
;
2274 struct route_in6 ro
;
2277 * This case matches to multicasts, our anycast, or unicasts
2278 * that we do not own. Select a source address based on the
2279 * source address of the erroneous packet.
2281 bzero(&sin6
, sizeof(sin6
));
2282 sin6
.sin6_family
= AF_INET6
;
2283 sin6
.sin6_len
= sizeof(sin6
);
2284 sin6
.sin6_addr
= ip6
->ip6_dst
; /* zone ID should be embedded */
2286 bzero(&ro
, sizeof(ro
));
2287 src
= in6_selectsrc(&sin6
, NULL
, NULL
, &ro
, &outif
,
2288 &src_storage
, ip6oa
.ip6oa_boundif
, &e
);
2290 rtfree(ro
.ro_rt
); /* XXX: we could use this */
2293 "icmp6_reflect: source can't be determined: "
2294 "dst=%s, error=%d\n",
2295 ip6_sprintf(&sa6_src
.sin6_addr
), e
));
2299 oflow
= ip6
->ip6_flow
; /* Save for later */
2300 ip6
->ip6_src
= *src
;
2302 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2303 ip6
->ip6_vfc
|= IPV6_VERSION
;
2304 if (icmp6
->icmp6_type
== ICMP6_ECHO_REPLY
&& icmp6
->icmp6_code
== 0) {
2305 ip6
->ip6_flow
|= (oflow
& htonl(0x0ff00000));
2307 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2308 lck_rw_lock_shared(nd_if_rwlock
);
2310 ip6
->ip6_hlim
= ND_IFINFO(outif
)->chlim
;
2311 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_index
< nd_ifinfo_indexlim
) {
2312 /* XXX: This may not be the outgoing interface */
2313 ip6
->ip6_hlim
= nd_ifinfo
[m
->m_pkthdr
.rcvif
->if_index
].chlim
;
2315 ip6
->ip6_hlim
= ip6_defhlim
;
2317 lck_rw_done(nd_if_rwlock
);
2318 /* Use the same traffic class as in the request to match IPv4 */
2319 icmp6
->icmp6_cksum
= 0;
2320 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
2321 sizeof(struct ip6_hdr
), plen
);
2324 * XXX option handling
2327 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
2329 /* Don't lookup socket */
2330 if (ipsec_bypass
== 0)
2331 (void)ipsec_setsocket(m
, NULL
);
2334 if (outif
!= NULL
) {
2335 ifnet_release(outif
);
2338 ip6_output(m
, NULL
, NULL
, IPV6_OUTARGS
, NULL
, &outif
, &ip6oa
);
2339 if (outif
!= NULL
) {
2340 icmp6_ifoutstat_inc(outif
, type
, code
);
2341 ifnet_release(outif
);
2348 ifnet_release(outif
);
2353 icmp6_redirect_diag(src6
, dst6
, tgt6
)
2354 struct in6_addr
*src6
;
2355 struct in6_addr
*dst6
;
2356 struct in6_addr
*tgt6
;
2358 static char buf
[1024];
2359 snprintf(buf
, sizeof(buf
), "(src=%s dst=%s tgt=%s)",
2360 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
2365 icmp6_redirect_input(m
, off
)
2369 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2370 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2371 struct nd_redirect
*nd_rd
;
2372 int icmp6len
= ntohs(ip6
->ip6_plen
);
2373 char *lladdr
= NULL
;
2375 u_char
*redirhdr
= NULL
;
2376 int redirhdrlen
= 0;
2377 struct rtentry
*rt
= NULL
;
2380 struct in6_addr src6
= ip6
->ip6_src
;
2381 struct in6_addr redtgt6
;
2382 struct in6_addr reddst6
;
2383 union nd_opts ndopts
;
2388 /* XXX if we are router, we don't update route by icmp6 redirect */
2391 if (!icmp6_rediraccept
)
2394 #ifndef PULLDOWN_TEST
2395 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
2396 nd_rd
= (struct nd_redirect
*)((caddr_t
)ip6
+ off
);
2398 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
2399 if (nd_rd
== NULL
) {
2400 icmp6stat
.icp6s_tooshort
++;
2404 redtgt6
= nd_rd
->nd_rd_target
;
2405 reddst6
= nd_rd
->nd_rd_dst
;
2407 if (in6_setscope(&redtgt6
, m
->m_pkthdr
.rcvif
, NULL
) ||
2408 in6_setscope(&reddst6
, m
->m_pkthdr
.rcvif
, NULL
)) {
2413 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
2415 "ICMP6 redirect sent from %s rejected; "
2416 "must be from linklocal\n", ip6_sprintf(&src6
)));
2419 if (ip6
->ip6_hlim
!= 255) {
2421 "ICMP6 redirect sent from %s rejected; "
2422 "hlim=%d (must be 255)\n",
2423 ip6_sprintf(&src6
), ip6
->ip6_hlim
));
2427 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2428 struct sockaddr_in6 sin6
;
2429 struct in6_addr
*gw6
;
2431 bzero(&sin6
, sizeof(sin6
));
2432 sin6
.sin6_family
= AF_INET6
;
2433 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2434 bcopy(&reddst6
, &sin6
.sin6_addr
, sizeof(reddst6
));
2435 rt
= rtalloc1_scoped((struct sockaddr
*)&sin6
, 0, 0, ifp
->if_index
);
2438 if (rt
->rt_gateway
== NULL
||
2439 rt
->rt_gateway
->sa_family
!= AF_INET6
) {
2441 "ICMP6 redirect rejected; no route "
2442 "with inet6 gateway found for redirect dst: %s\n",
2443 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2449 gw6
= &(((struct sockaddr_in6
*)rt
->rt_gateway
)->sin6_addr
);
2450 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
2452 "ICMP6 redirect rejected; "
2453 "not equal to gw-for-src=%s (must be same): "
2456 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2463 "ICMP6 redirect rejected; "
2464 "no route found for redirect dst: %s\n",
2465 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2472 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
2474 "ICMP6 redirect rejected; "
2475 "redirect dst must be unicast: %s\n",
2476 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2480 is_router
= is_onlink
= 0;
2481 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2482 is_router
= 1; /* router case */
2483 if (bcmp(&redtgt6
, &reddst6
, sizeof(redtgt6
)) == 0)
2484 is_onlink
= 1; /* on-link destination case */
2485 if (!is_router
&& !is_onlink
) {
2487 "ICMP6 redirect rejected; "
2488 "neither router case nor onlink case: %s\n",
2489 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2492 /* validation passed */
2494 icmp6len
-= sizeof(*nd_rd
);
2495 nd6_option_init(nd_rd
+ 1, icmp6len
, &ndopts
);
2496 if (nd6_options(&ndopts
) < 0) {
2497 nd6log((LOG_INFO
, "icmp6_redirect_input: "
2498 "invalid ND option, rejected: %s\n",
2499 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2500 /* nd6_options have incremented stats */
2504 if (ndopts
.nd_opts_tgt_lladdr
) {
2505 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
2506 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
2509 if (ndopts
.nd_opts_rh
) {
2510 redirhdrlen
= ndopts
.nd_opts_rh
->nd_opt_rh_len
;
2511 redirhdr
= (u_char
*)(ndopts
.nd_opts_rh
+ 1); /* xxx */
2514 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
2516 "icmp6_redirect_input: lladdrlen mismatch for %s "
2517 "(if %d, icmp6 packet %d): %s\n",
2518 ip6_sprintf(&redtgt6
), ifp
->if_addrlen
, lladdrlen
- 2,
2519 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2524 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
2525 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
2527 if (!is_onlink
) { /* better router case. perform rtredirect. */
2528 /* perform rtredirect */
2529 struct sockaddr_in6 sdst
;
2530 struct sockaddr_in6 sgw
;
2531 struct sockaddr_in6 ssrc
;
2533 bzero(&sdst
, sizeof(sdst
));
2534 bzero(&sgw
, sizeof(sgw
));
2535 bzero(&ssrc
, sizeof(ssrc
));
2536 sdst
.sin6_family
= sgw
.sin6_family
= ssrc
.sin6_family
= AF_INET6
;
2537 sdst
.sin6_len
= sgw
.sin6_len
= ssrc
.sin6_len
=
2538 sizeof(struct sockaddr_in6
);
2539 bcopy(&redtgt6
, &sgw
.sin6_addr
, sizeof(struct in6_addr
));
2540 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2541 bcopy(&src6
, &ssrc
.sin6_addr
, sizeof(struct in6_addr
));
2542 rtredirect(ifp
, (struct sockaddr
*)&sdst
,
2543 (struct sockaddr
*)&sgw
, NULL
, RTF_GATEWAY
| RTF_HOST
,
2544 (struct sockaddr
*)&ssrc
, NULL
);
2546 /* finally update cached route in each socket via pfctlinput */
2548 struct sockaddr_in6 sdst
;
2550 bzero(&sdst
, sizeof(sdst
));
2551 sdst
.sin6_family
= AF_INET6
;
2552 sdst
.sin6_len
= sizeof(struct sockaddr_in6
);
2553 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2557 * Release the IPv6 domain lock because we are going to take domain_proto_mtx
2558 * and could otherwise cause a deadlock with other threads taking these locks
2559 * in the reverse order -- e.g. frag6_slowtimo() from pfslowtimo()
2561 lck_mtx_unlock(inet6_domain_mutex
);
2562 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
2563 lck_mtx_lock(inet6_domain_mutex
);
2565 key_sa_routechange((struct sockaddr
*)&sdst
);
2574 icmp6stat
.icp6s_badredirect
++;
2579 icmp6_redirect_output(m0
, rt
)
2583 struct ifnet
*ifp
; /* my outgoing interface */
2584 struct in6_addr ifp_ll6
;
2585 struct in6_addr
*router_ll6
;
2586 struct ip6_hdr
*sip6
; /* m0 as struct ip6_hdr */
2587 struct mbuf
*m
= NULL
; /* newly allocated one */
2588 struct ip6_hdr
*ip6
; /* m as struct ip6_hdr */
2589 struct nd_redirect
*nd_rd
;
2592 struct ifnet
*outif
= NULL
;
2593 struct sockaddr_in6 src_sa
;
2594 struct ip6_out_args ip6oa
= { IFSCOPE_NONE
, 0 };
2596 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, ND_REDIRECT
, 0);
2602 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
))
2605 /* if we are not router, we don't send icmp6 redirect */
2606 if (!ip6_forwarding
|| ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))
2611 * the source address must identify a neighbor, and
2612 * the destination address must not be a multicast address
2613 * [RFC 2461, sec 8.2]
2615 sip6
= mtod(m0
, struct ip6_hdr
*);
2616 bzero(&src_sa
, sizeof(src_sa
));
2617 src_sa
.sin6_family
= AF_INET6
;
2618 src_sa
.sin6_len
= sizeof(src_sa
);
2619 src_sa
.sin6_addr
= sip6
->ip6_src
;
2620 /* we don't currently use sin6_scope_id, but eventually use it */
2621 src_sa
.sin6_scope_id
= in6_addr2scopeid(ifp
, &sip6
->ip6_src
);
2623 if (nd6_is_addr_neighbor(&src_sa
, ifp
, 0) == 0) {
2624 /* already unlocked */
2629 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
))
2630 goto fail
; /* what should we do here? */
2633 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0))
2637 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2638 * we almost always ask for an mbuf cluster for simplicity.
2639 * (MHLEN < IPV6_MMTU is almost always true)
2641 #if IPV6_MMTU >= MCLBYTES
2642 # error assumption failed about IPV6_MMTU and MCLBYTES
2644 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
2645 if (m
&& IPV6_MMTU
>= MHLEN
)
2646 MCLGET(m
, M_DONTWAIT
);
2649 m
->m_pkthdr
.rcvif
= NULL
;
2651 maxlen
= M_TRAILINGSPACE(m
);
2652 maxlen
= min(IPV6_MMTU
, maxlen
);
2653 /* just for safety */
2654 if (maxlen
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) +
2655 ((sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7)) {
2660 /* get ip6 linklocal address for ifp(my outgoing interface). */
2661 struct in6_ifaddr
*ia
;
2662 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
2664 IN6_IFF_ANYCAST
)) == NULL
)
2666 IFA_LOCK(&ia
->ia_ifa
);
2667 ifp_ll6
= ia
->ia_addr
.sin6_addr
;
2668 IFA_UNLOCK(&ia
->ia_ifa
);
2669 IFA_REMREF(&ia
->ia_ifa
);
2672 /* get ip6 linklocal address for the router. */
2673 if (rt
->rt_gateway
&& (rt
->rt_flags
& RTF_GATEWAY
)) {
2674 struct sockaddr_in6
*sin6
;
2675 sin6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
2676 router_ll6
= &sin6
->sin6_addr
;
2677 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6
))
2678 router_ll6
= (struct in6_addr
*)NULL
;
2680 router_ll6
= (struct in6_addr
*)NULL
;
2683 ip6
= mtod(m
, struct ip6_hdr
*);
2685 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2686 ip6
->ip6_vfc
|= IPV6_VERSION
;
2687 /* ip6->ip6_plen will be set later */
2688 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2689 ip6
->ip6_hlim
= 255;
2690 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2691 bcopy(&ifp_ll6
, &ip6
->ip6_src
, sizeof(struct in6_addr
));
2692 bcopy(&sip6
->ip6_src
, &ip6
->ip6_dst
, sizeof(struct in6_addr
));
2695 nd_rd
= (struct nd_redirect
*)(ip6
+ 1);
2696 nd_rd
->nd_rd_type
= ND_REDIRECT
;
2697 nd_rd
->nd_rd_code
= 0;
2698 nd_rd
->nd_rd_reserved
= 0;
2699 if (rt
->rt_flags
& RTF_GATEWAY
) {
2701 * nd_rd->nd_rd_target must be a link-local address in
2702 * better router cases.
2706 bcopy(router_ll6
, &nd_rd
->nd_rd_target
,
2707 sizeof(nd_rd
->nd_rd_target
));
2708 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2709 sizeof(nd_rd
->nd_rd_dst
));
2711 /* make sure redtgt == reddst */
2712 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_target
,
2713 sizeof(nd_rd
->nd_rd_target
));
2714 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2715 sizeof(nd_rd
->nd_rd_dst
));
2720 p
= (u_char
*)(nd_rd
+ 1);
2726 /* target lladdr option */
2727 struct rtentry
*rt_router
= NULL
;
2729 struct sockaddr_dl
*sdl
;
2730 struct nd_opt_hdr
*nd_opt
;
2733 /* Callee returns a locked route upon success */
2734 rt_router
= nd6_lookup(router_ll6
, 0, ifp
, 0);
2737 RT_LOCK_ASSERT_HELD(rt_router
);
2738 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2739 len
= (len
+ 7) & ~7; /* round by 8 */
2741 if (len
+ (p
- (u_char
*)ip6
) > maxlen
) {
2742 RT_REMREF_LOCKED(rt_router
);
2743 RT_UNLOCK(rt_router
);
2747 if (!(rt_router
->rt_flags
& RTF_GATEWAY
) &&
2748 (rt_router
->rt_flags
& RTF_LLINFO
) &&
2749 (rt_router
->rt_gateway
->sa_family
== AF_LINK
) &&
2750 (sdl
= (struct sockaddr_dl
*)rt_router
->rt_gateway
) &&
2752 nd_opt
= (struct nd_opt_hdr
*)p
;
2753 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
2754 nd_opt
->nd_opt_len
= len
>> 3;
2755 lladdr
= (char *)(nd_opt
+ 1);
2756 bcopy(LLADDR(sdl
), lladdr
, ifp
->if_addrlen
);
2759 RT_REMREF_LOCKED(rt_router
);
2760 RT_UNLOCK(rt_router
);
2765 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2767 /* just to be safe */
2768 #ifdef M_DECRYPTED /*not openbsd*/
2769 if (m0
->m_flags
& M_DECRYPTED
)
2772 if (p
- (u_char
*)ip6
> maxlen
)
2776 /* redirected header option */
2778 struct nd_opt_rd_hdr
*nd_opt_rh
;
2781 * compute the maximum size for icmp6 redirect header option.
2782 * XXX room for auth header?
2784 len
= maxlen
- (p
- (u_char
*)ip6
);
2787 /* This is just for simplicity. */
2788 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2790 m_freem(m0
->m_next
);
2793 m0
->m_pkthdr
.len
= m0
->m_len
;
2797 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2798 * about padding/truncate rule for the original IP packet.
2799 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2800 * - "attach as much as possible" is the goal
2801 * - pad if not aligned (original size can be guessed by original
2803 * Following code adds the padding if it is simple enough,
2804 * and truncates if not.
2806 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
)
2807 panic("assumption failed in %s:%d\n", __FILE__
, __LINE__
);
2809 if (len
- sizeof(*nd_opt_rh
) < m0
->m_pkthdr
.len
) {
2810 /* not enough room, truncate */
2811 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2813 /* enough room, pad or truncate */
2816 extra
= m0
->m_pkthdr
.len
% 8;
2818 /* pad if easy enough, truncate if not */
2819 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2821 m0
->m_len
+= (8 - extra
);
2822 m0
->m_pkthdr
.len
+= (8 - extra
);
2825 m0
->m_pkthdr
.len
-= extra
;
2829 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2830 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2833 nd_opt_rh
= (struct nd_opt_rd_hdr
*)p
;
2834 bzero(nd_opt_rh
, sizeof(*nd_opt_rh
));
2835 nd_opt_rh
->nd_opt_rh_type
= ND_OPT_REDIRECTED_HEADER
;
2836 nd_opt_rh
->nd_opt_rh_len
= len
>> 3;
2837 p
+= sizeof(*nd_opt_rh
);
2838 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2840 /* connect m0 to m */
2842 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
2846 /* XXX: clear embedded link IDs in the inner header */
2847 in6_clearscope(&sip6
->ip6_src
);
2848 in6_clearscope(&sip6
->ip6_dst
);
2849 in6_clearscope(&nd_rd
->nd_rd_target
);
2850 in6_clearscope(&nd_rd
->nd_rd_dst
);
2852 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
2854 nd_rd
->nd_rd_cksum
= 0;
2856 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
2858 /* send the packet to outside... */
2860 /* Don't lookup socket */
2861 if (ipsec_bypass
== 0)
2862 (void)ipsec_setsocket(m
, NULL
);
2865 ip6oa
.ip6oa_boundif
= ifp
->if_index
;
2867 ip6_output(m
, NULL
, NULL
, IPV6_OUTARGS
, NULL
, &outif
, &ip6oa
);
2869 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
2870 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
2871 ifnet_release(outif
);
2873 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
2887 * ICMPv6 socket option processing.
2890 icmp6_ctloutput(so
, sopt
)
2892 struct sockopt
*sopt
;
2896 struct inpcb
*inp
= sotoinpcb(so
);
2897 int level
, op
, optname
;
2900 level
= sopt
->sopt_level
;
2901 op
= sopt
->sopt_dir
;
2902 optname
= sopt
->sopt_name
;
2903 optlen
= sopt
->sopt_valsize
;
2905 level
= op
= optname
= optlen
= 0;
2907 if (level
!= IPPROTO_ICMPV6
) {
2916 struct icmp6_filter
*p
;
2918 if (optlen
!= 0 && optlen
!= sizeof(*p
)) {
2922 if (inp
->in6p_icmp6filt
== NULL
) {
2928 /* According to RFC 3542, an installed filter can be
2929 * cleared by issuing a setsockopt for ICMP6_FILTER
2930 * with a zero length.
2932 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
2934 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
2941 error
= ENOPROTOOPT
;
2950 if (inp
->in6p_icmp6filt
== NULL
) {
2954 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
2955 min(sizeof(struct icmp6_filter
), optlen
));
2960 error
= ENOPROTOOPT
;
2970 * ICMPv6 socket datagram option processing.
2973 icmp6_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2975 if (so
->so_uid
== 0)
2976 return icmp6_ctloutput(so
, sopt
);
2978 if (sopt
->sopt_level
== IPPROTO_ICMPV6
) {
2979 switch (sopt
->sopt_name
) {
2981 return icmp6_ctloutput(so
, sopt
);
2987 if (sopt
->sopt_level
!= IPPROTO_IPV6
)
2990 switch (sopt
->sopt_name
) {
2991 case IPV6_UNICAST_HOPS
:
2995 case IPV6_USE_MIN_MTU
:
2996 case IPV6_RECVRTHDR
:
2997 case IPV6_RECVPKTINFO
:
2998 case IPV6_RECVHOPLIMIT
:
3004 case IPV6_MULTICAST_IF
:
3005 case IPV6_MULTICAST_HOPS
:
3006 case IPV6_MULTICAST_LOOP
:
3007 case IPV6_JOIN_GROUP
:
3008 case IPV6_LEAVE_GROUP
:
3009 case IPV6_PORTRANGE
:
3010 case IPV6_IPSEC_POLICY
:
3011 case IPV6_RECVTCLASS
:
3013 case IPV6_2292PKTOPTIONS
:
3014 case IPV6_2292PKTINFO
:
3015 case IPV6_2292HOPLIMIT
:
3016 case IPV6_2292HOPOPTS
:
3017 case IPV6_2292DSTOPTS
:
3018 case IPV6_2292RTHDR
:
3020 case IPV6_NO_IFT_CELLULAR
:
3022 return ip6_ctloutput(so
, sopt
);
3031 __private_extern__
int
3032 icmp6_dgram_send(struct socket
*so
, int flags
, struct mbuf
*m
,
3033 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
3035 #pragma unused(flags, p)
3037 struct inpcb
*inp
= sotoinpcb(so
);
3038 struct sockaddr_in6 tmp
;
3039 struct sockaddr_in6
*dst
= (struct sockaddr_in6
*)nam
;
3040 struct icmp6_hdr
*icmp6
;
3042 if (so
->so_uid
== 0)
3043 return rip6_output(m
, so
, (struct sockaddr_in6
*) nam
, control
, 0);
3045 /* always copy sockaddr to avoid overwrites */
3046 if (so
->so_state
& SS_ISCONNECTED
) {
3052 bzero(&tmp
, sizeof(tmp
));
3053 tmp
.sin6_family
= AF_INET6
;
3054 tmp
.sin6_len
= sizeof(struct sockaddr_in6
);
3055 bcopy(&inp
->in6p_faddr
, &tmp
.sin6_addr
,
3056 sizeof(struct in6_addr
));
3063 tmp
= *(struct sockaddr_in6
*)nam
;
3068 * For an ICMPv6 packet, we should know its type and code
3070 if (so
->so_proto
->pr_protocol
== IPPROTO_ICMPV6
) {
3071 if (m
->m_len
< sizeof(struct icmp6_hdr
) &&
3072 (m
= m_pullup(m
, sizeof(struct icmp6_hdr
))) == NULL
) {
3076 icmp6
= mtod(m
, struct icmp6_hdr
*);
3079 * Allow only to send echo request and node information request
3080 * See RFC 2463 for Echo Request Message format
3082 if ((icmp6
->icmp6_type
== ICMP6_ECHO_REQUEST
&& icmp6
->icmp6_code
== 0) ||
3083 (icmp6
->icmp6_type
== ICMP6_NI_QUERY
&&
3084 (icmp6
->icmp6_code
== ICMP6_NI_SUBJ_IPV6
||
3085 icmp6
->icmp6_code
== ICMP6_NI_SUBJ_FQDN
))) {
3094 #if ENABLE_DEFAULT_SCOPE
3095 if (dst
->sin6_scope_id
== 0) { /* not change if specified */
3096 dst
->sin6_scope_id
= scope6_addr2default(&dst
->sin6_addr
);
3100 return rip6_output(m
, so
, (struct sockaddr_in6
*) nam
, control
, 0);
3106 /* Like rip6_attach but without root privilege enforcement */
3107 __private_extern__
int
3108 icmp6_dgram_attach(struct socket
*so
, int proto
, struct proc
*p
)
3113 inp
= sotoinpcb(so
);
3115 panic("icmp6_dgram_attach");
3117 if (proto
!= IPPROTO_ICMPV6
)
3120 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
3123 error
= in_pcballoc(so
, &ripcbinfo
, p
);
3126 inp
= (struct inpcb
*)so
->so_pcb
;
3127 inp
->inp_vflag
|= INP_IPV6
;
3128 inp
->in6p_ip6_nxt
= IPPROTO_ICMPV6
;
3129 inp
->in6p_hops
= -1; /* use kernel default */
3130 inp
->in6p_cksum
= -1;
3131 MALLOC(inp
->in6p_icmp6filt
, struct icmp6_filter
*,
3132 sizeof(struct icmp6_filter
), M_PCB
, M_WAITOK
);
3133 if (inp
->in6p_icmp6filt
== NULL
)
3135 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
3141 #ifndef HAVE_PPSRATECHECK
3143 #define timersub(tvp, uvp, vvp) \
3145 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
3146 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
3147 if ((vvp)->tv_usec < 0) { \
3149 (vvp)->tv_usec += 1000000; \
3155 * ppsratecheck(): packets (or events) per second limitation.
3158 ppsratecheck(lasttime
, curpps
, maxpps
)
3159 struct timeval
*lasttime
;
3161 int maxpps
; /* maximum pps allowed */
3163 struct timeval tv
, delta
;
3168 timersub(&tv
, lasttime
, &delta
);
3171 * Check for 0,0 so that the message will be seen at least once.
3172 * If more than one second has passed since the last update of
3173 * lasttime, reset the counter.
3175 * we do increment *curpps even in *curpps < maxpps case, as some may
3176 * try to use *curpps for stat purposes as well.
3178 if ((lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0) ||
3179 delta
.tv_sec
>= 1) {
3183 } else if (maxpps
< 0)
3185 else if (*curpps
< maxpps
)
3190 #if 1 /* DIAGNOSTIC? */
3191 /* be careful about wrap-around */
3192 if (*curpps
+ 1 > 0)
3193 *curpps
= *curpps
+ 1;
3196 * assume that there's not too many calls to this function.
3197 * not sure if the assumption holds, as it depends on *caller's*
3198 * behavior, not the behavior of this function.
3199 * IMHO it is wrong to make assumption on the caller's behavior,
3200 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
3202 *curpps
= *curpps
+ 1;
3210 * Perform rate limit check.
3211 * Returns 0 if it is okay to send the icmp6 packet.
3212 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
3215 * XXX per-destination/type check necessary?
3219 __unused
const struct in6_addr
*dst
, /* not used at this moment */
3220 __unused
const int type
, /* not used at this moment */
3221 __unused
const int code
) /* not used at this moment */
3225 ret
= 0; /* okay to send */
3228 if (!ppsratecheck(&icmp6errppslim_last
, &icmp6errpps_count
,
3230 /* The packet is subject to rate limit */