2 * Copyright (c) 2008 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>
128 #include <netinet6/ipsec.h>
129 #include <netkey/key.h>
131 extern int ipsec_bypass
;
134 #include <net/net_osdep.h>
136 extern struct domain inet6domain
;
137 extern struct ip6protosw inet6sw
[];
138 extern struct ip6protosw
*ip6_protox
[];
140 extern uint32_t rip_sendspace
;
141 extern uint32_t rip_recvspace
;
143 struct icmp6stat icmp6stat
;
145 extern struct inpcbhead ripcb
;
146 extern int icmp6errppslim
;
147 static int icmp6errpps_count
= 0;
148 static struct timeval icmp6errppslim_last
;
149 extern int icmp6_nodeinfo
;
150 extern struct inpcbinfo ripcbinfo
;
151 extern lck_mtx_t
*ip6_mutex
;
152 extern lck_mtx_t
*nd6_mutex
;
153 extern lck_mtx_t
*inet6_domain_mutex
;
155 static void icmp6_errcount(struct icmp6errstat
*, int, int);
156 static int icmp6_rip6_input(struct mbuf
**, int);
157 static int icmp6_ratelimit(const struct in6_addr
*, const int, const int);
158 static const char *icmp6_redirect_diag(struct in6_addr
*,
159 struct in6_addr
*, struct in6_addr
*);
160 #ifndef HAVE_PPSRATECHECK
161 static int ppsratecheck(struct timeval
*, int *, int);
163 static struct mbuf
*ni6_input(struct mbuf
*, int);
164 static struct mbuf
*ni6_nametodns(const char *, int, int);
165 static int ni6_dnsmatch(const char *, int, const char *, int);
166 static int ni6_addrs(struct icmp6_nodeinfo
*,
167 struct ifnet
**, char *);
168 static int ni6_store_addrs(struct icmp6_nodeinfo
*, struct icmp6_nodeinfo
*,
169 struct ifnet
*, int);
170 static int icmp6_notify_error(struct mbuf
*, int, int, int);
172 #ifdef COMPAT_RFC1885
174 * XXX: Compiled out for now, but if enabled we must use a lock for accesses,
175 * or easier, define it locally inside icmp6_reflect() and don't cache.
177 static struct route_in6 icmp6_reflect_rt
;
188 icmp6_errcount(stat
, type
, code
)
189 struct icmp6errstat
*stat
;
193 case ICMP6_DST_UNREACH
:
195 case ICMP6_DST_UNREACH_NOROUTE
:
196 stat
->icp6errs_dst_unreach_noroute
++;
198 case ICMP6_DST_UNREACH_ADMIN
:
199 stat
->icp6errs_dst_unreach_admin
++;
201 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
202 stat
->icp6errs_dst_unreach_beyondscope
++;
204 case ICMP6_DST_UNREACH_ADDR
:
205 stat
->icp6errs_dst_unreach_addr
++;
207 case ICMP6_DST_UNREACH_NOPORT
:
208 stat
->icp6errs_dst_unreach_noport
++;
212 case ICMP6_PACKET_TOO_BIG
:
213 stat
->icp6errs_packet_too_big
++;
215 case ICMP6_TIME_EXCEEDED
:
217 case ICMP6_TIME_EXCEED_TRANSIT
:
218 stat
->icp6errs_time_exceed_transit
++;
220 case ICMP6_TIME_EXCEED_REASSEMBLY
:
221 stat
->icp6errs_time_exceed_reassembly
++;
225 case ICMP6_PARAM_PROB
:
227 case ICMP6_PARAMPROB_HEADER
:
228 stat
->icp6errs_paramprob_header
++;
230 case ICMP6_PARAMPROB_NEXTHEADER
:
231 stat
->icp6errs_paramprob_nextheader
++;
233 case ICMP6_PARAMPROB_OPTION
:
234 stat
->icp6errs_paramprob_option
++;
239 stat
->icp6errs_redirect
++;
242 stat
->icp6errs_unknown
++;
246 * Generate an error packet of type error in response to bad IP6 packet.
249 icmp6_error(m
, type
, code
, param
)
251 int type
, code
, param
;
253 struct ip6_hdr
*oip6
, *nip6
;
254 struct icmp6_hdr
*icmp6
;
259 icmp6stat
.icp6s_error
++;
261 lck_mtx_assert(ip6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
262 /* count per-type-code statistics */
263 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, type
, code
);
265 #ifdef M_DECRYPTED /*not openbsd*/
266 if (m
->m_flags
& M_DECRYPTED
) {
267 icmp6stat
.icp6s_canterror
++;
272 #ifndef PULLDOWN_TEST
273 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return);
275 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
276 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
281 oip6
= mtod(m
, struct ip6_hdr
*);
284 * Multicast destination check. For unrecognized option errors,
285 * this check has already done in ip6_unknown_opt(), so we can
286 * check only for other errors.
288 if ((m
->m_flags
& (M_BCAST
|M_MCAST
) ||
289 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_dst
)) &&
290 (type
!= ICMP6_PACKET_TOO_BIG
&&
291 (type
!= ICMP6_PARAM_PROB
||
292 code
!= ICMP6_PARAMPROB_OPTION
)))
295 /* Source address check. XXX: the case of anycast source? */
296 if (IN6_IS_ADDR_UNSPECIFIED(&oip6
->ip6_src
) ||
297 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_src
))
301 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
305 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
306 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
307 struct icmp6_hdr
*icp
;
309 #ifndef PULLDOWN_TEST
310 IP6_EXTHDR_CHECK(m
, 0, off
+ sizeof(struct icmp6_hdr
), return);
311 icp
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
313 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
316 icmp6stat
.icp6s_tooshort
++;
320 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
321 icp
->icmp6_type
== ND_REDIRECT
) {
324 * Special case: for redirect (which is
325 * informational) we must not send icmp6 error.
327 icmp6stat
.icp6s_canterror
++;
330 /* ICMPv6 informational - send the error */
333 /* non-ICMPv6 - send the error */
336 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
338 /* Finally, do rate limitation check. */
339 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
340 icmp6stat
.icp6s_toofreq
++;
345 * OK, ICMP6 can be generated.
348 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
)
349 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
351 preplen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
352 M_PREPEND(m
, preplen
, M_DONTWAIT
);
353 if (m
&& m
->m_len
< preplen
)
354 m
= m_pullup(m
, preplen
);
356 nd6log((LOG_DEBUG
, "ENOBUFS in icmp6_error %d\n", __LINE__
));
360 nip6
= mtod(m
, struct ip6_hdr
*);
361 nip6
->ip6_src
= oip6
->ip6_src
;
362 nip6
->ip6_dst
= oip6
->ip6_dst
;
364 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
365 oip6
->ip6_src
.s6_addr16
[1] = 0;
366 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
367 oip6
->ip6_dst
.s6_addr16
[1] = 0;
369 icmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
370 icmp6
->icmp6_type
= type
;
371 icmp6
->icmp6_code
= code
;
372 icmp6
->icmp6_pptr
= htonl((u_int32_t
)param
);
375 * icmp6_reflect() is designed to be in the input path.
376 * icmp6_error() can be called from both input and outut path,
377 * and if we are in output path rcvif could contain bogus value.
378 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
379 * information in ip header (nip6).
381 m
->m_pkthdr
.rcvif
= NULL
;
383 icmp6stat
.icp6s_outhist
[type
]++;
384 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /* header order: IPv6 - ICMPv6 */
390 * If we can't tell wheter or not we can generate ICMP6, free it.
396 * Process a received ICMP6 message.
399 icmp6_input(mp
, offp
)
403 struct mbuf
*m
= *mp
, *n
;
404 struct ip6_hdr
*ip6
, *nip6
;
405 struct icmp6_hdr
*icmp6
, *nicmp6
;
407 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
410 #ifndef PULLDOWN_TEST
411 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_hdr
), return IPPROTO_DONE
);
412 /* m might change if M_LOOP. So, call mtod after this */
416 * Locate icmp6 structure in mbuf, and check
417 * that not corrupted and of at least minimum length
420 ip6
= mtod(m
, struct ip6_hdr
*);
421 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
422 icmp6stat
.icp6s_tooshort
++;
427 * calculate the checksum
429 #ifndef PULLDOWN_TEST
430 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
432 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
434 icmp6stat
.icp6s_tooshort
++;
438 code
= icmp6
->icmp6_code
;
440 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
442 "ICMP6 checksum error(%d|%x) %s\n",
443 icmp6
->icmp6_type
, sum
, ip6_sprintf(&ip6
->ip6_src
)));
444 icmp6stat
.icp6s_checksum
++;
448 #if defined(NFAITH) && 0 < NFAITH
449 if (faithprefix(&ip6
->ip6_dst
)) {
451 * Deliver very specific ICMP6 type only.
452 * This is important to deilver TOOBIG. Otherwise PMTUD
455 switch (icmp6
->icmp6_type
) {
456 case ICMP6_DST_UNREACH
:
457 case ICMP6_PACKET_TOO_BIG
:
458 case ICMP6_TIME_EXCEEDED
:
466 icmp6stat
.icp6s_inhist
[icmp6
->icmp6_type
]++;
467 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_msg
);
468 if (icmp6
->icmp6_type
< ICMP6_INFOMSG_MASK
)
469 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_error
);
472 switch (icmp6
->icmp6_type
) {
473 case ICMP6_DST_UNREACH
:
474 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
476 case ICMP6_DST_UNREACH_NOROUTE
:
477 code
= PRC_UNREACH_NET
;
479 case ICMP6_DST_UNREACH_ADMIN
:
480 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_adminprohib
);
481 code
= PRC_UNREACH_PROTOCOL
; /* is this a good code? */
483 case ICMP6_DST_UNREACH_ADDR
:
486 #ifdef COMPAT_RFC1885
487 case ICMP6_DST_UNREACH_NOTNEIGHBOR
:
488 code
= PRC_UNREACH_SRCFAIL
;
491 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
492 /* I mean "source address was incorrect." */
493 code
= PRC_PARAMPROB
;
496 case ICMP6_DST_UNREACH_NOPORT
:
497 code
= PRC_UNREACH_PORT
;
506 case ICMP6_PACKET_TOO_BIG
:
507 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
514 * Updating the path MTU will be done after examining
515 * intermediate extension headers.
520 case ICMP6_TIME_EXCEEDED
:
521 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
523 case ICMP6_TIME_EXCEED_TRANSIT
:
524 case ICMP6_TIME_EXCEED_REASSEMBLY
:
525 code
+= PRC_TIMXCEED_INTRANS
;
533 case ICMP6_PARAM_PROB
:
534 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
536 case ICMP6_PARAMPROB_NEXTHEADER
:
537 code
= PRC_UNREACH_PROTOCOL
;
539 case ICMP6_PARAMPROB_HEADER
:
540 case ICMP6_PARAMPROB_OPTION
:
541 code
= PRC_PARAMPROB
;
549 case ICMP6_ECHO_REQUEST
:
550 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
554 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
555 icmp6stat
.icp6s_toofreq
++;
559 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
561 goto rate_limit_checked
;
564 if ((n
->m_flags
& M_EXT
) != 0
565 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
567 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
570 * Prepare an internal mbuf. m_pullup() doesn't
571 * always copy the length we specified.
573 if (maxlen
>= MCLBYTES
) {
576 goto rate_limit_checked
;
579 MGETHDR(n
, M_DONTWAIT
, n0
->m_type
); /* MAC-OK */
580 if (n
&& maxlen
>= MHLEN
) {
581 MCLGET(n
, M_DONTWAIT
);
582 if ((n
->m_flags
& M_EXT
) == 0) {
590 goto rate_limit_checked
;
593 M_COPY_PKTHDR(n
, n0
);
595 * Copy IPv6 and ICMPv6 only.
597 nip6
= mtod(n
, struct ip6_hdr
*);
598 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
599 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
600 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
601 noff
= sizeof(struct ip6_hdr
);
602 n
->m_pkthdr
.len
= n
->m_len
=
603 noff
+ sizeof(struct icmp6_hdr
);
605 * Adjust mbuf. ip6_plen will be adjusted in
608 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
609 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
611 n0
->m_flags
&= ~M_PKTHDR
;
613 nip6
= mtod(n
, struct ip6_hdr
*);
614 IP6_EXTHDR_GET(nicmp6
, struct icmp6_hdr
*, n
, off
,
618 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
619 nicmp6
->icmp6_code
= 0;
621 icmp6stat
.icp6s_reflect
++;
622 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
623 icmp6_reflect(n
, noff
);
625 goto rate_limit_checked
;
628 case ICMP6_ECHO_REPLY
:
629 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
634 case MLD6_LISTENER_QUERY
:
635 case MLD6_LISTENER_REPORT
:
636 if (icmp6len
< sizeof(struct mld6_hdr
))
638 if (icmp6
->icmp6_type
== MLD6_LISTENER_QUERY
) /* XXX: ugly... */
639 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
641 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
643 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
644 icmp6stat
.icp6s_toofreq
++;
648 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
656 goto rate_limit_checked
;
659 case MLD6_LISTENER_DONE
:
660 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
661 if (icmp6len
< sizeof(struct mld6_hdr
)) /* necessary? */
663 break; /* nothing to be done in kernel */
665 case MLD6_MTRACE_RESP
:
667 /* XXX: these two are experimental. not officially defind. */
668 /* XXX: per-interface statistics? */
669 break; /* just pass it to applications */
675 /* By RFC 4620 refuse to answer queries from global scope addresses */
676 if ((icmp6_nodeinfo
& 8) != 8 && in6_addrscope(&ip6
->ip6_src
) == IPV6_ADDR_SCOPE_GLOBAL
)
679 if (icmp6len
< sizeof(struct icmp6_nodeinfo
))
682 #ifndef PULLDOWN_TEST
683 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
684 return IPPROTO_DONE
);
686 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
687 icmp6stat
.icp6s_toofreq
++;
691 n
= m_copy(m
, 0, M_COPYALL
);
693 n
= ni6_input(n
, off
);
695 noff
= sizeof(struct ip6_hdr
);
696 icmp6stat
.icp6s_reflect
++;
697 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
698 icmp6_reflect(n
, noff
);
700 goto rate_limit_checked
;
708 case ND_ROUTER_SOLICIT
:
709 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
712 if (icmp6len
< sizeof(struct nd_router_solicit
))
715 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
716 icmp6stat
.icp6s_toofreq
++;
720 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
722 nd6_rs_input(m
, off
, icmp6len
);
726 nd6_rs_input(n
, off
, icmp6len
);
728 goto rate_limit_checked
;
731 case ND_ROUTER_ADVERT
:
732 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
735 if (icmp6len
< sizeof(struct nd_router_advert
))
738 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
739 icmp6stat
.icp6s_toofreq
++;
743 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
745 nd6_ra_input(m
, off
, icmp6len
);
749 nd6_ra_input(n
, off
, icmp6len
);
751 goto rate_limit_checked
;
754 case ND_NEIGHBOR_SOLICIT
:
755 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
758 if (icmp6len
< sizeof(struct nd_neighbor_solicit
))
761 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
762 icmp6stat
.icp6s_toofreq
++;
766 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
768 nd6_ns_input(m
, off
, icmp6len
);
772 nd6_ns_input(n
, off
, icmp6len
);
774 goto rate_limit_checked
;
777 case ND_NEIGHBOR_ADVERT
:
778 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
781 if (icmp6len
< sizeof(struct nd_neighbor_advert
))
784 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
785 icmp6stat
.icp6s_toofreq
++;
789 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
791 nd6_na_input(m
, off
, icmp6len
);
795 nd6_na_input(n
, off
, icmp6len
);
797 goto rate_limit_checked
;
801 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
804 if (icmp6len
< sizeof(struct nd_redirect
))
807 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
808 icmp6stat
.icp6s_toofreq
++;
812 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
814 icmp6_redirect_input(m
, off
);
818 icmp6_redirect_input(n
, off
);
820 goto rate_limit_checked
;
823 case ICMP6_ROUTER_RENUMBERING
:
824 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
825 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
)
827 if (icmp6len
< sizeof(struct icmp6_router_renum
))
832 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
833 icmp6stat
.icp6s_toofreq
++;
838 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
839 icmp6
->icmp6_type
, ip6_sprintf(&ip6
->ip6_src
),
840 ip6_sprintf(&ip6
->ip6_dst
),
841 m
->m_pkthdr
.rcvif
? m
->m_pkthdr
.rcvif
->if_index
: 0));
842 if (icmp6
->icmp6_type
< ICMP6_ECHO_REQUEST
) {
843 /* ICMPv6 error: MUST deliver it by spec... */
847 /* ICMPv6 informational: MUST not deliver */
848 goto rate_limit_checked
;
852 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
853 icmp6stat
.icp6s_toofreq
++;
857 if (icmp6_notify_error(m
, off
, icmp6len
, code
)) {
858 /* In this case, m should've been freed. */
859 return(IPPROTO_DONE
);
864 icmp6stat
.icp6s_badcode
++;
868 icmp6stat
.icp6s_badlen
++;
872 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
873 icmp6stat
.icp6s_toofreq
++;
877 /* deliver the packet to appropriate sockets */
878 icmp6_rip6_input(&m
, *offp
);
888 icmp6_notify_error(m
, off
, icmp6len
, code
)
890 int off
, icmp6len
, code
;
892 struct icmp6_hdr
*icmp6
;
893 struct ip6_hdr
*eip6
;
895 struct sockaddr_in6 icmp6src
, icmp6dst
;
897 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
898 icmp6stat
.icp6s_tooshort
++;
901 #ifndef PULLDOWN_TEST
902 IP6_EXTHDR_CHECK(m
, off
,
903 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
905 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
907 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
908 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
910 icmp6stat
.icp6s_tooshort
++;
914 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
916 /* Detect the upper level protocol */
918 void (*ctlfunc
)(int, struct sockaddr
*, void *);
919 u_int8_t nxt
= eip6
->ip6_nxt
;
920 int eoff
= off
+ sizeof(struct icmp6_hdr
) +
921 sizeof(struct ip6_hdr
);
922 struct ip6ctlparam ip6cp
;
923 struct in6_addr
*finaldst
= NULL
;
924 int icmp6type
= icmp6
->icmp6_type
;
926 struct ip6_rthdr
*rth
;
927 struct ip6_rthdr0
*rth0
;
930 while (1) { /* XXX: should avoid infinite loop explicitly? */
934 case IPPROTO_HOPOPTS
:
935 case IPPROTO_DSTOPTS
:
937 #ifndef PULLDOWN_TEST
938 IP6_EXTHDR_CHECK(m
, 0, eoff
+
939 sizeof(struct ip6_ext
),
941 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
944 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
947 icmp6stat
.icp6s_tooshort
++;
952 if (nxt
== IPPROTO_AH
)
953 eoff
+= (eh
->ip6e_len
+ 2) << 2;
955 eoff
+= (eh
->ip6e_len
+ 1) << 3;
958 case IPPROTO_ROUTING
:
960 * When the erroneous packet contains a
961 * routing header, we should examine the
962 * header to determine the final destination.
963 * Otherwise, we can't properly update
964 * information that depends on the final
965 * destination (e.g. path MTU).
967 #ifndef PULLDOWN_TEST
968 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
970 rth
= (struct ip6_rthdr
*)(mtod(m
, caddr_t
)
973 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
976 icmp6stat
.icp6s_tooshort
++;
980 rthlen
= (rth
->ip6r_len
+ 1) << 3;
982 * XXX: currently there is no
983 * officially defined type other
985 * Note that if the segment left field
986 * is 0, all intermediate hops must
989 if (rth
->ip6r_segleft
&&
990 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
993 #ifndef PULLDOWN_TEST
994 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
996 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
999 struct ip6_rthdr0
*, m
,
1002 icmp6stat
.icp6s_tooshort
++;
1006 /* just ignore a bogus header */
1007 if ((rth0
->ip6r0_len
% 2) == 0 &&
1008 (hops
= rth0
->ip6r0_len
/2))
1009 finaldst
= (struct in6_addr
*)(rth0
+ 1) + (hops
- 1);
1012 nxt
= rth
->ip6r_nxt
;
1014 case IPPROTO_FRAGMENT
:
1015 #ifndef PULLDOWN_TEST
1016 IP6_EXTHDR_CHECK(m
, 0, eoff
+
1017 sizeof(struct ip6_frag
),
1019 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
1022 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
1025 icmp6stat
.icp6s_tooshort
++;
1030 * Data after a fragment header is meaningless
1031 * unless it is the first fragment, but
1032 * we'll go to the notify label for path MTU
1035 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
)
1038 eoff
+= sizeof(struct ip6_frag
);
1043 * This case includes ESP and the No Next
1044 * Header. In such cases going to the notify
1045 * label does not have any meaning
1046 * (i.e. ctlfunc will be NULL), but we go
1047 * anyway since we might have to update
1048 * path MTU information.
1054 #ifndef PULLDOWN_TEST
1055 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
1057 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
1058 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
1059 if (icmp6
== NULL
) {
1060 icmp6stat
.icp6s_tooshort
++;
1065 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1066 bzero(&icmp6dst
, sizeof(icmp6dst
));
1067 icmp6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
1068 icmp6dst
.sin6_family
= AF_INET6
;
1069 if (finaldst
== NULL
)
1070 icmp6dst
.sin6_addr
= eip6
->ip6_dst
;
1072 icmp6dst
.sin6_addr
= *finaldst
;
1073 icmp6dst
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1074 &icmp6dst
.sin6_addr
);
1075 #ifndef SCOPEDROUTING
1076 if (in6_embedscope(&icmp6dst
.sin6_addr
, &icmp6dst
,
1078 /* should be impossbile */
1080 "icmp6_notify_error: in6_embedscope failed\n"));
1086 * retrieve parameters from the inner IPv6 header, and convert
1087 * them into sockaddr structures.
1089 bzero(&icmp6src
, sizeof(icmp6src
));
1090 icmp6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1091 icmp6src
.sin6_family
= AF_INET6
;
1092 icmp6src
.sin6_addr
= eip6
->ip6_src
;
1093 icmp6src
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1094 &icmp6src
.sin6_addr
);
1095 #ifndef SCOPEDROUTING
1096 if (in6_embedscope(&icmp6src
.sin6_addr
, &icmp6src
,
1098 /* should be impossbile */
1100 "icmp6_notify_error: in6_embedscope failed\n"));
1104 icmp6src
.sin6_flowinfo
=
1105 (eip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
);
1107 if (finaldst
== NULL
)
1108 finaldst
= &eip6
->ip6_dst
;
1110 ip6cp
.ip6c_icmp6
= icmp6
;
1111 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1112 ip6cp
.ip6c_off
= eoff
;
1113 ip6cp
.ip6c_finaldst
= finaldst
;
1114 ip6cp
.ip6c_src
= &icmp6src
;
1115 ip6cp
.ip6c_nxt
= nxt
;
1117 if (icmp6type
== ICMP6_PACKET_TOO_BIG
) {
1118 notifymtu
= ntohl(icmp6
->icmp6_mtu
);
1119 ip6cp
.ip6c_cmdarg
= (void *)¬ifymtu
;
1120 icmp6_mtudisc_update(&ip6cp
, 1); /*XXX*/
1123 ctlfunc
= (void (*)(int, struct sockaddr
*, void *))
1124 (ip6_protox
[nxt
]->pr_ctlinput
);
1126 (void) (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6dst
,
1138 icmp6_mtudisc_update(ip6cp
, validated
)
1139 struct ip6ctlparam
*ip6cp
;
1142 struct in6_addr
*dst
= ip6cp
->ip6c_finaldst
;
1143 struct icmp6_hdr
*icmp6
= ip6cp
->ip6c_icmp6
;
1144 struct mbuf
*m
= ip6cp
->ip6c_m
; /* will be necessary for scope issue */
1145 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1146 struct rtentry
*rt
= NULL
;
1147 struct sockaddr_in6 sin6
;
1152 * In case the suggested mtu is less than IPV6_MMTU, we
1153 * only need to remember that it was for above mentioned
1154 * "alwaysfrag" case.
1155 * Try to be as close to the spec as possible.
1157 if (mtu
< IPV6_MMTU
)
1158 mtu
= IPV6_MMTU
- 8;
1160 bzero(&sin6
, sizeof(sin6
));
1161 sin6
.sin6_family
= PF_INET6
;
1162 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1163 sin6
.sin6_addr
= *dst
;
1164 /* XXX normally, this won't happen */
1165 if (IN6_IS_ADDR_LINKLOCAL(dst
)) {
1166 sin6
.sin6_addr
.s6_addr16
[1] =
1167 htons(m
->m_pkthdr
.rcvif
->if_index
);
1169 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1170 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, RTF_CLONING
| RTF_PRCLONING
);
1173 if ((rt
->rt_flags
& RTF_HOST
) &&
1174 !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1175 if (mtu
< IPV6_MMTU
) {
1177 rt
->rt_rmx
.rmx_locks
|= RTV_MTU
;
1178 } else if (mtu
< rt
->rt_ifp
->if_mtu
&&
1179 rt
->rt_rmx
.rmx_mtu
> mtu
) {
1180 icmp6stat
.icp6s_pmtuchg
++;
1181 rt
->rt_rmx
.rmx_mtu
= mtu
;
1190 * Process a Node Information Query packet, based on
1191 * draft-ietf-ipngwg-icmp-name-lookups-07.
1193 * Spec incompatibilities:
1194 * - IPv6 Subject address handling
1195 * - IPv4 Subject address handling support missing
1196 * - Proxy reply (answer even if it's not for me)
1197 * - joins NI group address at in6_ifattach() time only, does not cope
1198 * with hostname changes by sethostname(3)
1200 #define hostnamelen strlen(hostname)
1201 static struct mbuf
*
1206 struct icmp6_nodeinfo
*ni6
, *nni6
;
1207 struct mbuf
*n
= NULL
;
1210 int replylen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1211 struct ni_reply_fqdn
*fqdn
;
1212 int addrs
; /* for NI_QTYPE_NODEADDR */
1213 struct ifnet
*ifp
= NULL
; /* for NI_QTYPE_NODEADDR */
1214 struct sockaddr_in6 sin6
; /* double meaning; ip6_dst and subjectaddr */
1215 struct sockaddr_in6 sin6_d
; /* XXX: we should retrieve this from m_aux */
1216 struct ip6_hdr
*ip6
;
1217 int oldfqdn
= 0; /* if 1, return pascal string (03 draft) */
1219 struct in6_ifaddr
*ia6
= NULL
;
1221 ip6
= mtod(m
, struct ip6_hdr
*);
1222 #ifndef PULLDOWN_TEST
1223 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1225 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1227 /* m is already reclaimed */
1233 * Validate IPv6 destination address.
1235 * The Responder must discard the Query without further processing
1236 * unless it is one of the Responder's unicast or anycast addresses, or
1237 * a link-local scope multicast address which the Responder has joined.
1238 * [icmp-name-lookups-07, Section 4.]
1240 bzero(&sin6
, sizeof(sin6
));
1241 sin6
.sin6_family
= AF_INET6
;
1242 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1243 bcopy(&ip6
->ip6_dst
, &sin6
.sin6_addr
, sizeof(sin6
.sin6_addr
));
1245 if ((ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr((struct sockaddr
*)&sin6
)) != NULL
) {
1246 /* unicast/anycast, fine */
1247 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1248 (icmp6_nodeinfo
& 4) == 0) {
1249 ifafree(&ia6
->ia_ifa
);
1251 nd6log((LOG_DEBUG
, "ni6_input: ignore node info to "
1252 "a temporary address in %s:%d",
1253 __FILE__
, __LINE__
));
1256 ifafree(&ia6
->ia_ifa
);
1258 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
))
1259 ; /* link-local multicast, fine */
1263 /* validate query Subject field. */
1264 qtype
= ntohs(ni6
->ni_qtype
);
1265 subjlen
= m
->m_pkthdr
.len
- off
- sizeof(struct icmp6_nodeinfo
);
1268 case NI_QTYPE_SUPTYPES
:
1270 if (ni6
->ni_code
== ICMP6_NI_SUBJ_FQDN
&& subjlen
== 0)
1274 case NI_QTYPE_NODEADDR
:
1275 switch (ni6
->ni_code
) {
1276 case ICMP6_NI_SUBJ_IPV6
:
1277 #if ICMP6_NI_SUBJ_IPV6 != 0
1281 * backward compatibility - try to accept 03 draft
1282 * format, where no Subject is present.
1284 if (qtype
== NI_QTYPE_FQDN
&& ni6
->ni_code
== 0 &&
1289 #if ICMP6_NI_SUBJ_IPV6 != 0
1290 if (ni6
->ni_code
!= ICMP6_NI_SUBJ_IPV6
)
1294 if (subjlen
!= sizeof(sin6
.sin6_addr
))
1298 * Validate Subject address.
1300 * Not sure what exactly "address belongs to the node"
1301 * means in the spec, is it just unicast, or what?
1303 * At this moment we consider Subject address as
1304 * "belong to the node" if the Subject address equals
1305 * to the IPv6 destination address; validation for
1306 * IPv6 destination address should have done enough
1309 * We do not do proxy at this moment.
1311 /* m_pulldown instead of copy? */
1312 m_copydata(m
, off
+ sizeof(struct icmp6_nodeinfo
),
1313 subjlen
, (caddr_t
)&sin6
.sin6_addr
);
1314 sin6
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1316 #ifndef SCOPEDROUTING
1317 in6_embedscope(&sin6
.sin6_addr
, &sin6
, NULL
, NULL
);
1319 bzero(&sin6_d
, sizeof(sin6_d
));
1320 sin6_d
.sin6_family
= AF_INET6
; /* not used, actually */
1321 sin6_d
.sin6_len
= sizeof(sin6_d
); /* ditto */
1322 sin6_d
.sin6_addr
= ip6
->ip6_dst
;
1323 sin6_d
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1325 #ifndef SCOPEDROUTING
1326 in6_embedscope(&sin6_d
.sin6_addr
, &sin6_d
, NULL
, NULL
);
1328 subj
= (char *)&sin6
;
1329 if (SA6_ARE_ADDR_EQUAL(&sin6
, &sin6_d
))
1333 * XXX if we are to allow other cases, we should really
1334 * be careful about scope here.
1335 * basically, we should disallow queries toward IPv6
1336 * destination X with subject Y, if scope(X) > scope(Y).
1337 * if we allow scope(X) > scope(Y), it will result in
1338 * information leakage across scope boundary.
1342 case ICMP6_NI_SUBJ_FQDN
:
1344 * Validate Subject name with gethostname(3).
1346 * The behavior may need some debate, since:
1347 * - we are not sure if the node has FQDN as
1348 * hostname (returned by gethostname(3)).
1349 * - the code does wildcard match for truncated names.
1350 * however, we are not sure if we want to perform
1351 * wildcard match, if gethostname(3) side has
1352 * truncated hostname.
1354 n
= ni6_nametodns(hostname
, hostnamelen
, 0);
1355 if (!n
|| n
->m_next
|| n
->m_len
== 0)
1357 IP6_EXTHDR_GET(subj
, char *, m
,
1358 off
+ sizeof(struct icmp6_nodeinfo
), subjlen
);
1361 if (!ni6_dnsmatch(subj
, subjlen
, mtod(n
, const char *),
1369 case ICMP6_NI_SUBJ_IPV4
: /* XXX: to be implemented? */
1376 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1379 if ((icmp6_nodeinfo
& 1) == 0)
1382 case NI_QTYPE_NODEADDR
:
1383 if ((icmp6_nodeinfo
& 2) == 0)
1388 /* guess reply length */
1391 break; /* no reply data */
1392 case NI_QTYPE_SUPTYPES
:
1393 replylen
+= sizeof(u_int32_t
);
1396 /* XXX will append an mbuf */
1397 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1399 case NI_QTYPE_NODEADDR
:
1400 addrs
= ni6_addrs(ni6
, &ifp
, subj
);
1401 if ((replylen
+= addrs
* (sizeof(struct in6_addr
) +
1402 sizeof(u_int32_t
))) > MCLBYTES
)
1403 replylen
= MCLBYTES
; /* XXX: will truncate pkt later */
1407 * XXX: We must return a reply with the ICMP6 code
1408 * `unknown Qtype' in this case. However we regard the case
1409 * as an FQDN query for backward compatibility.
1410 * Older versions set a random value to this field,
1411 * so it rarely varies in the defined qtypes.
1412 * But the mechanism is not reliable...
1413 * maybe we should obsolete older versions.
1415 qtype
= NI_QTYPE_FQDN
;
1416 /* XXX will append an mbuf */
1417 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1422 /* allocate an mbuf to reply. */
1423 MGETHDR(n
, M_DONTWAIT
, m
->m_type
); /* MAC-OK */
1428 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1429 if (replylen
> MHLEN
) {
1430 if (replylen
> MCLBYTES
) {
1432 * XXX: should we try to allocate more? But MCLBYTES
1433 * is probably much larger than IPV6_MMTU...
1437 MCLGET(n
, M_DONTWAIT
);
1438 if ((n
->m_flags
& M_EXT
) == 0) {
1442 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
1444 /* copy mbuf header and IPv6 + Node Information base headers */
1445 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), sizeof(struct ip6_hdr
));
1446 nni6
= (struct icmp6_nodeinfo
*)(mtod(n
, struct ip6_hdr
*) + 1);
1447 bcopy((caddr_t
)ni6
, (caddr_t
)nni6
, sizeof(struct icmp6_nodeinfo
));
1449 /* qtype dependent procedure */
1452 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1455 case NI_QTYPE_SUPTYPES
:
1458 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1459 nni6
->ni_flags
= htons(0x0000); /* raw bitmap */
1460 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1461 v
= (u_int32_t
)htonl(0x0000000f);
1462 bcopy(&v
, nni6
+ 1, sizeof(u_int32_t
));
1466 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1467 fqdn
= (struct ni_reply_fqdn
*)(mtod(n
, caddr_t
) +
1468 sizeof(struct ip6_hdr
) +
1469 sizeof(struct icmp6_nodeinfo
));
1470 nni6
->ni_flags
= 0; /* XXX: meaningless TTL */
1471 fqdn
->ni_fqdn_ttl
= 0; /* ditto. */
1473 * XXX do we really have FQDN in variable "hostname"?
1475 n
->m_next
= ni6_nametodns(hostname
, hostnamelen
, oldfqdn
);
1476 if (n
->m_next
== NULL
)
1478 /* XXX we assume that n->m_next is not a chain */
1479 if (n
->m_next
->m_next
!= NULL
)
1481 n
->m_pkthdr
.len
+= n
->m_next
->m_len
;
1483 case NI_QTYPE_NODEADDR
:
1487 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1488 n
->m_pkthdr
.len
= n
->m_len
=
1489 sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1490 lenlim
= M_TRAILINGSPACE(n
);
1491 copied
= ni6_store_addrs(ni6
, nni6
, ifp
, lenlim
);
1492 /* XXX: reset mbuf length */
1493 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
1494 sizeof(struct icmp6_nodeinfo
) + copied
;
1498 break; /* XXX impossible! */
1501 nni6
->ni_type
= ICMP6_NI_REPLY
;
1514 * make a mbuf with DNS-encoded string. no compression support.
1516 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1517 * treated as truncated name (two \0 at the end). this is a wild guess.
1519 static struct mbuf
*
1520 ni6_nametodns(name
, namelen
, old
)
1523 int old
; /* return pascal string if non-zero */
1535 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1536 MGET(m
, M_DONTWAIT
, MT_DATA
);
1537 if (m
&& len
> MLEN
) {
1538 MCLGET(m
, M_DONTWAIT
);
1539 if ((m
->m_flags
& M_EXT
) == 0)
1548 *mtod(m
, char *) = namelen
;
1549 bcopy(name
, mtod(m
, char *) + 1, namelen
);
1553 cp
= mtod(m
, char *);
1554 ep
= mtod(m
, char *) + M_TRAILINGSPACE(m
);
1556 /* if not certain about my name, return empty buffer */
1561 * guess if it looks like shortened hostname, or FQDN.
1562 * shortened hostname needs two trailing "\0".
1565 for (p
= name
; p
< name
+ namelen
; p
++) {
1566 if (*p
&& *p
== '.')
1575 while (cp
< ep
&& p
< name
+ namelen
) {
1577 for (q
= p
; q
< name
+ namelen
&& *q
&& *q
!= '.'; q
++)
1579 /* result does not fit into mbuf */
1580 if (cp
+ i
+ 1 >= ep
)
1583 * DNS label length restriction, RFC1035 page 8.
1584 * "i == 0" case is included here to avoid returning
1585 * 0-length label on "foo..bar".
1587 if (i
<= 0 || i
>= 64)
1593 if (p
< name
+ namelen
&& *p
== '.')
1597 if (cp
+ nterm
>= ep
)
1601 m
->m_len
= cp
- mtod(m
, char *);
1605 panic("should not reach here");
1615 * check if two DNS-encoded string matches. takes care of truncated
1616 * form (with \0\0 at the end). no compression support.
1617 * XXX upper/lowercase match (see RFC2065)
1620 ni6_dnsmatch(a
, alen
, b
, blen
)
1626 const char *a0
, *b0
;
1629 /* simplest case - need validation? */
1630 if (alen
== blen
&& bcmp(a
, b
, alen
) == 0)
1636 /* termination is mandatory */
1637 if (alen
< 2 || blen
< 2)
1639 if (a0
[alen
- 1] != '\0' || b0
[blen
- 1] != '\0')
1644 while (a
- a0
< alen
&& b
- b0
< blen
) {
1645 if (a
- a0
+ 1 > alen
|| b
- b0
+ 1 > blen
)
1648 if ((signed char)a
[0] < 0 || (signed char)b
[0] < 0)
1650 /* we don't support compression yet */
1651 if (a
[0] >= 64 || b
[0] >= 64)
1654 /* truncated case */
1655 if (a
[0] == 0 && a
- a0
== alen
- 1)
1657 if (b
[0] == 0 && b
- b0
== blen
- 1)
1659 if (a
[0] == 0 || b
[0] == 0)
1665 if (a
- a0
+ 1 + l
> alen
|| b
- b0
+ 1 + l
> blen
)
1667 if (bcmp(a
+ 1, b
+ 1, l
) != 0)
1674 if (a
- a0
== alen
&& b
- b0
== blen
)
1681 * calculate the number of addresses to be returned in the node info reply.
1684 ni6_addrs(ni6
, ifpp
, subj
)
1685 struct icmp6_nodeinfo
*ni6
;
1686 struct ifnet
**ifpp
;
1690 struct in6_ifaddr
*ifa6
;
1692 struct sockaddr_in6
*subj_ip6
= NULL
; /* XXX pedant */
1693 int addrs
= 0, addrsofif
, iffound
= 0;
1694 int niflags
= ni6
->ni_flags
;
1696 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0) {
1697 switch (ni6
->ni_code
) {
1698 case ICMP6_NI_SUBJ_IPV6
:
1699 if (subj
== NULL
) /* must be impossible... */
1701 subj_ip6
= (struct sockaddr_in6
*)subj
;
1705 * XXX: we only support IPv6 subject address for
1712 ifnet_head_lock_shared();
1713 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
1715 ifnet_lock_shared(ifp
);
1716 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1718 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1720 ifa6
= (struct in6_ifaddr
*)ifa
;
1722 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0 &&
1723 IN6_ARE_ADDR_EQUAL(&subj_ip6
->sin6_addr
,
1724 &ifa6
->ia_addr
.sin6_addr
))
1728 * IPv4-mapped addresses can only be returned by a
1729 * Node Information proxy, since they represent
1730 * addresses of IPv4-only nodes, which perforce do
1731 * not implement this protocol.
1732 * [icmp-name-lookups-07, Section 5.4]
1733 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1734 * this function at this moment.
1737 /* What do we have to do about ::1? */
1738 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1739 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1740 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1743 case IPV6_ADDR_SCOPE_SITELOCAL
:
1744 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1747 case IPV6_ADDR_SCOPE_GLOBAL
:
1748 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1756 * check if anycast is okay.
1757 * XXX: just experimental. not in the spec.
1759 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1760 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1761 continue; /* we need only unicast addresses */
1762 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1763 (icmp6_nodeinfo
& 4) == 0) {
1766 addrsofif
++; /* count the address */
1768 ifnet_lock_done(ifp
);
1783 ni6_store_addrs(ni6
, nni6
, ifp0
, resid
)
1784 struct icmp6_nodeinfo
*ni6
, *nni6
;
1788 struct ifnet
*ifp
= ifp0
;
1789 struct in6_ifaddr
*ifa6
;
1791 struct ifnet
*ifp_dep
= NULL
;
1792 int copied
= 0, allow_deprecated
= 0;
1793 u_char
*cp
= (u_char
*)(nni6
+ 1);
1794 int niflags
= ni6
->ni_flags
;
1796 struct timeval timenow
;
1798 getmicrotime(&timenow
);
1800 if (ifp0
== NULL
&& !(niflags
& NI_NODEADDR_FLAG_ALL
))
1801 return(0); /* needless to copy */
1805 ifnet_head_lock_shared();
1806 if (ifp
== NULL
) ifp
= TAILQ_FIRST(&ifnet_head
);
1808 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1809 ifnet_lock_shared(ifp
);
1810 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1811 ifa
= ifa
->ifa_list
.tqe_next
)
1813 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1815 ifa6
= (struct in6_ifaddr
*)ifa
;
1817 if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0 &&
1818 allow_deprecated
== 0) {
1820 * prefererred address should be put before
1821 * deprecated addresses.
1824 /* record the interface for later search */
1825 if (ifp_dep
== NULL
)
1830 else if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) == 0 &&
1831 allow_deprecated
!= 0)
1832 continue; /* we now collect deprecated addrs */
1834 /* What do we have to do about ::1? */
1835 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1836 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1837 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1840 case IPV6_ADDR_SCOPE_SITELOCAL
:
1841 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1844 case IPV6_ADDR_SCOPE_GLOBAL
:
1845 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1853 * check if anycast is okay.
1854 * XXX: just experimental. not in the spec.
1856 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1857 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1859 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1860 (icmp6_nodeinfo
& 4) == 0) {
1864 /* now we can copy the address */
1865 if (resid
< sizeof(struct in6_addr
) +
1866 sizeof(u_int32_t
)) {
1868 * We give up much more copy.
1869 * Set the truncate flag and return.
1872 NI_NODEADDR_FLAG_TRUNCATE
;
1873 ifnet_lock_done(ifp
);
1879 * Set the TTL of the address.
1880 * The TTL value should be one of the following
1881 * according to the specification:
1883 * 1. The remaining lifetime of a DHCP lease on the
1885 * 2. The remaining Valid Lifetime of a prefix from
1886 * which the address was derived through Stateless
1887 * Autoconfiguration.
1889 * Note that we currently do not support stateful
1890 * address configuration by DHCPv6, so the former
1891 * case can't happen.
1893 if (ifa6
->ia6_lifetime
.ia6t_expire
== 0)
1894 ltime
= ND6_INFINITE_LIFETIME
;
1896 if (ifa6
->ia6_lifetime
.ia6t_expire
>
1898 ltime
= htonl(ifa6
->ia6_lifetime
.ia6t_expire
- timenow
.tv_sec
);
1903 bcopy(<ime
, cp
, sizeof(u_int32_t
));
1904 cp
+= sizeof(u_int32_t
);
1906 /* copy the address itself */
1907 bcopy(&ifa6
->ia_addr
.sin6_addr
, cp
,
1908 sizeof(struct in6_addr
));
1909 /* XXX: KAME link-local hack; remove ifindex */
1910 if (IN6_IS_ADDR_LINKLOCAL(&ifa6
->ia_addr
.sin6_addr
))
1911 ((struct in6_addr
*)cp
)->s6_addr16
[1] = 0;
1912 cp
+= sizeof(struct in6_addr
);
1914 resid
-= (sizeof(struct in6_addr
) + sizeof(u_int32_t
));
1915 copied
+= (sizeof(struct in6_addr
) +
1918 ifnet_lock_done(ifp
);
1919 if (ifp0
) /* we need search only on the specified IF */
1924 if (allow_deprecated
== 0 && ifp_dep
!= NULL
) {
1926 allow_deprecated
= 1;
1935 * XXX almost dup'ed code with rip6_input.
1938 icmp6_rip6_input(mp
, off
)
1942 struct mbuf
*m
= *mp
;
1943 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1944 struct in6pcb
*in6p
;
1945 struct in6pcb
*last
= NULL
;
1946 struct sockaddr_in6 rip6src
;
1947 struct icmp6_hdr
*icmp6
;
1948 struct mbuf
*opts
= NULL
;
1950 #ifndef PULLDOWN_TEST
1951 /* this is assumed to be safe. */
1952 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
1954 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
1955 if (icmp6
== NULL
) {
1956 /* m is already reclaimed */
1957 return IPPROTO_DONE
;
1961 bzero(&rip6src
, sizeof(rip6src
));
1962 rip6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1963 rip6src
.sin6_family
= AF_INET6
;
1964 /* KAME hack: recover scopeid */
1965 (void)in6_recoverscope(&rip6src
, &ip6
->ip6_src
, m
->m_pkthdr
.rcvif
);
1967 lck_rw_lock_shared(ripcbinfo
.mtx
);
1968 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
1970 if ((in6p
->inp_vflag
& INP_IPV6
) == 0)
1973 if (!(in6p
->in6p_flags
& INP_IPV6
))
1976 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
)
1978 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
1979 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
))
1981 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
1982 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
))
1984 if (in6p
->in6p_icmp6filt
1985 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
1986 in6p
->in6p_icmp6filt
))
1990 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
1991 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1992 ip6_savecontrol(last
, &opts
, ip6
, n
);
1993 /* strip intermediate headers */
1995 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1996 (struct sockaddr
*)&rip6src
,
1997 n
, opts
, NULL
) != 0) {
1998 sorwakeup(last
->in6p_socket
);
2005 lck_rw_done(ripcbinfo
.mtx
);
2007 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
2008 ip6_savecontrol(last
, &opts
, ip6
, m
);
2009 /* strip intermediate headers */
2011 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
2012 (struct sockaddr
*)&rip6src
, m
, opts
, NULL
) != 0) {
2013 sorwakeup(last
->in6p_socket
);
2017 ip6stat
.ip6s_delivered
--;
2019 return IPPROTO_DONE
;
2023 * Reflect the ip6 packet back to the source.
2024 * OFF points to the icmp6 header, counted from the top of the mbuf.
2027 icmp6_reflect(m
, off
)
2031 struct ip6_hdr
*ip6
;
2032 struct icmp6_hdr
*icmp6
;
2033 struct in6_ifaddr
*ia
;
2034 struct in6_addr t
, src_storage
, *src
= 0;
2037 struct ifnet
*outif
= NULL
;
2038 struct sockaddr_in6 sa6_src
, sa6_dst
;
2039 #ifdef COMPAT_RFC1885
2040 int mtu
= IPV6_MMTU
;
2041 struct sockaddr_in6
*sin6
= &icmp6_reflect_rt
.ro_dst
;
2045 /* too short to reflect */
2046 if (off
< sizeof(struct ip6_hdr
)) {
2048 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2049 (u_int32_t
)off
, (u_int32_t
)sizeof(struct ip6_hdr
),
2050 __FILE__
, __LINE__
));
2055 * If there are extra headers between IPv6 and ICMPv6, strip
2056 * off that header first.
2059 if (sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) > MHLEN
)
2060 panic("assumption failed in icmp6_reflect");
2062 if (off
> sizeof(struct ip6_hdr
)) {
2064 struct ip6_hdr nip6
;
2066 l
= off
- sizeof(struct ip6_hdr
);
2067 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
2069 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2071 if ((m
= m_pullup(m
, l
)) == NULL
)
2074 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
2075 } else /* off == sizeof(struct ip6_hdr) */ {
2077 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2079 if ((m
= m_pullup(m
, l
)) == NULL
)
2083 plen
= m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2084 ip6
= mtod(m
, struct ip6_hdr
*);
2085 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2086 icmp6
= (struct icmp6_hdr
*)(ip6
+ 1);
2087 type
= icmp6
->icmp6_type
; /* keep type for statistics */
2088 code
= icmp6
->icmp6_code
; /* ditto. */
2092 * ip6_input() drops a packet if its src is multicast.
2093 * So, the src is never multicast.
2095 ip6
->ip6_dst
= ip6
->ip6_src
;
2098 * XXX: make sure to embed scope zone information, using
2099 * already embedded IDs or the received interface (if any).
2100 * Note that rcvif may be NULL.
2101 * TODO: scoped routing case (XXX).
2103 bzero(&sa6_src
, sizeof(sa6_src
));
2104 sa6_src
.sin6_family
= AF_INET6
;
2105 sa6_src
.sin6_len
= sizeof(sa6_src
);
2106 sa6_src
.sin6_addr
= ip6
->ip6_dst
;
2107 in6_recoverscope(&sa6_src
, &ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
);
2108 in6_embedscope(&ip6
->ip6_dst
, &sa6_src
, NULL
, NULL
);
2109 bzero(&sa6_dst
, sizeof(sa6_dst
));
2110 sa6_dst
.sin6_family
= AF_INET6
;
2111 sa6_dst
.sin6_len
= sizeof(sa6_dst
);
2112 sa6_dst
.sin6_addr
= t
;
2113 in6_recoverscope(&sa6_dst
, &t
, m
->m_pkthdr
.rcvif
);
2114 in6_embedscope(&t
, &sa6_dst
, NULL
, NULL
);
2116 #ifdef COMPAT_RFC1885
2119 * RFC 1885 requires that echo reply should be truncated if it
2120 * does not fit in with (return) path MTU, but the description was
2121 * removed in the new spec.
2123 if (icmp6_reflect_rt
.ro_rt
== NULL
||
2124 !(icmp6_reflect_rt
.ro_rt
->rt_flags
& RTF_UP
) ||
2125 icmp6_reflect_rt
.ro_rt
->generation_id
!= route_generation
||
2126 ! (IN6_ARE_ADDR_EQUAL(&sin6
->sin6_addr
, &ip6
->ip6_dst
))) {
2127 if (icmp6_reflect_rt
.ro_rt
) {
2128 rtfree(icmp6_reflect_rt
.ro_rt
);
2129 icmp6_reflect_rt
.ro_rt
= 0;
2131 bzero(sin6
, sizeof(*sin6
));
2132 sin6
->sin6_family
= PF_INET6
;
2133 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2134 sin6
->sin6_addr
= ip6
->ip6_dst
;
2136 rtalloc_ign((struct route
*)&icmp6_reflect_rt
.ro_rt
,
2140 if (icmp6_reflect_rt
.ro_rt
== 0)
2143 RT_LOCK(icmp6_reflect_rt
.ro_rt
);
2144 if ((icmp6_reflect_rt
.ro_rt
->rt_flags
& RTF_HOST
)
2145 && mtu
< icmp6_reflect_rt
.ro_rt
->rt_ifp
->if_mtu
)
2146 mtu
= icmp6_reflect_rt
.ro_rt
->rt_rmx
.rmx_mtu
;
2147 RT_UNLOCK(icmp6_reflect_rt
.ro_rt
);
2149 if (mtu
< m
->m_pkthdr
.len
) {
2150 plen
-= (m
->m_pkthdr
.len
- mtu
);
2151 m_adj(m
, mtu
- m
->m_pkthdr
.len
);
2155 * If the incoming packet was addressed directly to us(i.e. unicast),
2156 * use dst as the src for the reply.
2157 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2158 * (for example) when we encounter an error while forwarding procedure
2159 * destined to a duplicated address of ours.
2161 lck_mtx_lock(nd6_mutex
);
2162 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
)
2163 if (IN6_ARE_ADDR_EQUAL(&t
, &ia
->ia_addr
.sin6_addr
) &&
2164 (ia
->ia6_flags
& (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
)) == 0) {
2168 lck_mtx_unlock(nd6_mutex
);
2169 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) && (m
->m_flags
& M_LOOP
)) {
2171 * This is the case if the dst is our link-local address
2172 * and the sender is also ourselves.
2179 struct route_in6 ro
;
2182 * This case matches to multicasts, our anycast, or unicasts
2183 * that we do not own. Select a source address based on the
2184 * source address of the erroneous packet.
2186 bzero(&ro
, sizeof(ro
));
2187 src
= in6_selectsrc(&sa6_src
, NULL
, NULL
, &ro
, NULL
, &src_storage
, &e
);
2189 rtfree(ro
.ro_rt
); /* XXX: we could use this */
2192 "icmp6_reflect: source can't be determined: "
2193 "dst=%s, error=%d\n",
2194 ip6_sprintf(&sa6_src
.sin6_addr
), e
));
2199 ip6
->ip6_src
= *src
;
2201 oflow
= ip6
->ip6_flow
; /* Save for later */
2203 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2204 ip6
->ip6_vfc
|= IPV6_VERSION
;
2205 if (icmp6
->icmp6_type
== ICMP6_ECHO_REPLY
&& icmp6
->icmp6_code
== 0) {
2206 ip6
->ip6_flow
|= (oflow
& htonl(0x0ff00000));
2208 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2209 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_index
< nd_ifinfo_indexlim
) {
2210 /* XXX: This may not be the outgoing interface */
2211 lck_rw_lock_shared(nd_if_rwlock
);
2212 ip6
->ip6_hlim
= nd_ifinfo
[m
->m_pkthdr
.rcvif
->if_index
].chlim
;
2213 lck_rw_done(nd_if_rwlock
);
2215 ip6
->ip6_hlim
= ip6_defhlim
;
2217 /* Use the same traffic class as in the request to match IPv4 */
2218 icmp6
->icmp6_cksum
= 0;
2219 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
2220 sizeof(struct ip6_hdr
), plen
);
2223 * XXX option handling
2226 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
2228 /* Don't lookup socket */
2229 if (ipsec_bypass
== 0)
2230 (void)ipsec_setsocket(m
, NULL
);
2233 #ifdef COMPAT_RFC1885
2234 ip6_output(m
, NULL
, &icmp6_reflect_rt
, 0, NULL
, &outif
, 0);
2236 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
, 0);
2239 icmp6_ifoutstat_inc(outif
, type
, code
);
2256 icmp6_redirect_diag(src6
, dst6
, tgt6
)
2257 struct in6_addr
*src6
;
2258 struct in6_addr
*dst6
;
2259 struct in6_addr
*tgt6
;
2261 static char buf
[1024];
2262 snprintf(buf
, sizeof(buf
), "(src=%s dst=%s tgt=%s)",
2263 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
2268 icmp6_redirect_input(m
, off
)
2272 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2273 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2274 struct nd_redirect
*nd_rd
;
2275 int icmp6len
= ntohs(ip6
->ip6_plen
);
2276 char *lladdr
= NULL
;
2278 u_char
*redirhdr
= NULL
;
2279 int redirhdrlen
= 0;
2280 struct rtentry
*rt
= NULL
;
2283 struct in6_addr src6
= ip6
->ip6_src
;
2284 struct in6_addr redtgt6
;
2285 struct in6_addr reddst6
;
2286 union nd_opts ndopts
;
2291 /* XXX if we are router, we don't update route by icmp6 redirect */
2294 if (!icmp6_rediraccept
)
2297 #ifndef PULLDOWN_TEST
2298 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
2299 nd_rd
= (struct nd_redirect
*)((caddr_t
)ip6
+ off
);
2301 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
2302 if (nd_rd
== NULL
) {
2303 icmp6stat
.icp6s_tooshort
++;
2307 redtgt6
= nd_rd
->nd_rd_target
;
2308 reddst6
= nd_rd
->nd_rd_dst
;
2310 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2311 redtgt6
.s6_addr16
[1] = htons(ifp
->if_index
);
2312 if (IN6_IS_ADDR_LINKLOCAL(&reddst6
))
2313 reddst6
.s6_addr16
[1] = htons(ifp
->if_index
);
2316 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
2318 "ICMP6 redirect sent from %s rejected; "
2319 "must be from linklocal\n", ip6_sprintf(&src6
)));
2322 if (ip6
->ip6_hlim
!= 255) {
2324 "ICMP6 redirect sent from %s rejected; "
2325 "hlim=%d (must be 255)\n",
2326 ip6_sprintf(&src6
), ip6
->ip6_hlim
));
2330 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2331 struct sockaddr_in6 sin6
;
2332 struct in6_addr
*gw6
;
2334 bzero(&sin6
, sizeof(sin6
));
2335 sin6
.sin6_family
= AF_INET6
;
2336 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2337 bcopy(&reddst6
, &sin6
.sin6_addr
, sizeof(reddst6
));
2338 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
2341 if (rt
->rt_gateway
== NULL
||
2342 rt
->rt_gateway
->sa_family
!= AF_INET6
) {
2344 "ICMP6 redirect rejected; no route "
2345 "with inet6 gateway found for redirect dst: %s\n",
2346 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2352 gw6
= &(((struct sockaddr_in6
*)rt
->rt_gateway
)->sin6_addr
);
2353 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
2355 "ICMP6 redirect rejected; "
2356 "not equal to gw-for-src=%s (must be same): "
2359 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2366 "ICMP6 redirect rejected; "
2367 "no route found for redirect dst: %s\n",
2368 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2375 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
2377 "ICMP6 redirect rejected; "
2378 "redirect dst must be unicast: %s\n",
2379 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2383 is_router
= is_onlink
= 0;
2384 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2385 is_router
= 1; /* router case */
2386 if (bcmp(&redtgt6
, &reddst6
, sizeof(redtgt6
)) == 0)
2387 is_onlink
= 1; /* on-link destination case */
2388 if (!is_router
&& !is_onlink
) {
2390 "ICMP6 redirect rejected; "
2391 "neither router case nor onlink case: %s\n",
2392 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2395 /* validation passed */
2397 icmp6len
-= sizeof(*nd_rd
);
2398 nd6_option_init(nd_rd
+ 1, icmp6len
, &ndopts
);
2399 if (nd6_options(&ndopts
) < 0) {
2400 nd6log((LOG_INFO
, "icmp6_redirect_input: "
2401 "invalid ND option, rejected: %s\n",
2402 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2403 /* nd6_options have incremented stats */
2407 if (ndopts
.nd_opts_tgt_lladdr
) {
2408 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
2409 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
2412 if (ndopts
.nd_opts_rh
) {
2413 redirhdrlen
= ndopts
.nd_opts_rh
->nd_opt_rh_len
;
2414 redirhdr
= (u_char
*)(ndopts
.nd_opts_rh
+ 1); /* xxx */
2417 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
2419 "icmp6_redirect_input: lladdrlen mismatch for %s "
2420 "(if %d, icmp6 packet %d): %s\n",
2421 ip6_sprintf(&redtgt6
), ifp
->if_addrlen
, lladdrlen
- 2,
2422 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2427 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
2428 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
2430 if (!is_onlink
) { /* better router case. perform rtredirect. */
2431 /* perform rtredirect */
2432 struct sockaddr_in6 sdst
;
2433 struct sockaddr_in6 sgw
;
2434 struct sockaddr_in6 ssrc
;
2436 bzero(&sdst
, sizeof(sdst
));
2437 bzero(&sgw
, sizeof(sgw
));
2438 bzero(&ssrc
, sizeof(ssrc
));
2439 sdst
.sin6_family
= sgw
.sin6_family
= ssrc
.sin6_family
= AF_INET6
;
2440 sdst
.sin6_len
= sgw
.sin6_len
= ssrc
.sin6_len
=
2441 sizeof(struct sockaddr_in6
);
2442 bcopy(&redtgt6
, &sgw
.sin6_addr
, sizeof(struct in6_addr
));
2443 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2444 bcopy(&src6
, &ssrc
.sin6_addr
, sizeof(struct in6_addr
));
2445 rtredirect(ifp
, (struct sockaddr
*)&sdst
,
2446 (struct sockaddr
*)&sgw
, NULL
, RTF_GATEWAY
| RTF_HOST
,
2447 (struct sockaddr
*)&ssrc
, NULL
);
2449 /* finally update cached route in each socket via pfctlinput */
2451 struct sockaddr_in6 sdst
;
2453 bzero(&sdst
, sizeof(sdst
));
2454 sdst
.sin6_family
= AF_INET6
;
2455 sdst
.sin6_len
= sizeof(struct sockaddr_in6
);
2456 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2460 * Release the IPv6 domain lock because we are going to take domain_proto_mtx
2461 * and could otherwise cause a deadlock with other threads taking these locks
2462 * in the reverse order -- e.g. frag6_slowtimo() from pfslowtimo()
2464 lck_mtx_unlock(inet6_domain_mutex
);
2465 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
2466 lck_mtx_lock(inet6_domain_mutex
);
2468 key_sa_routechange((struct sockaddr
*)&sdst
);
2477 icmp6stat
.icp6s_badredirect
++;
2482 icmp6_redirect_output(m0
, rt
)
2486 struct ifnet
*ifp
; /* my outgoing interface */
2487 struct in6_addr ifp_ll6
;
2488 struct in6_addr
*router_ll6
;
2489 struct ip6_hdr
*sip6
; /* m0 as struct ip6_hdr */
2490 struct mbuf
*m
= NULL
; /* newly allocated one */
2491 struct ip6_hdr
*ip6
; /* m as struct ip6_hdr */
2492 struct nd_redirect
*nd_rd
;
2495 struct ifnet
*outif
= NULL
;
2496 struct sockaddr_in6 src_sa
;
2498 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, ND_REDIRECT
, 0);
2504 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
))
2507 /* if we are not router, we don't send icmp6 redirect */
2508 if (!ip6_forwarding
|| ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))
2513 * the source address must identify a neighbor, and
2514 * the destination address must not be a multicast address
2515 * [RFC 2461, sec 8.2]
2517 sip6
= mtod(m0
, struct ip6_hdr
*);
2518 bzero(&src_sa
, sizeof(src_sa
));
2519 src_sa
.sin6_family
= AF_INET6
;
2520 src_sa
.sin6_len
= sizeof(src_sa
);
2521 src_sa
.sin6_addr
= sip6
->ip6_src
;
2522 /* we don't currently use sin6_scope_id, but eventually use it */
2523 src_sa
.sin6_scope_id
= in6_addr2scopeid(ifp
, &sip6
->ip6_src
);
2525 if (nd6_is_addr_neighbor(&src_sa
, ifp
, 0) == 0) {
2526 /* already unlocked */
2531 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
))
2532 goto fail
; /* what should we do here? */
2535 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0))
2539 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2540 * we almost always ask for an mbuf cluster for simplicity.
2541 * (MHLEN < IPV6_MMTU is almost always true)
2543 #if IPV6_MMTU >= MCLBYTES
2544 # error assumption failed about IPV6_MMTU and MCLBYTES
2546 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
2547 if (m
&& IPV6_MMTU
>= MHLEN
)
2548 MCLGET(m
, M_DONTWAIT
);
2551 m
->m_pkthdr
.rcvif
= NULL
;
2553 maxlen
= M_TRAILINGSPACE(m
);
2554 maxlen
= min(IPV6_MMTU
, maxlen
);
2555 /* just for safety */
2556 if (maxlen
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) +
2557 ((sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7)) {
2562 /* get ip6 linklocal address for ifp(my outgoing interface). */
2563 struct in6_ifaddr
*ia
;
2564 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
2566 IN6_IFF_ANYCAST
)) == NULL
)
2568 ifp_ll6
= ia
->ia_addr
.sin6_addr
;
2569 ifafree(&ia
->ia_ifa
);
2572 /* get ip6 linklocal address for the router. */
2573 if (rt
->rt_gateway
&& (rt
->rt_flags
& RTF_GATEWAY
)) {
2574 struct sockaddr_in6
*sin6
;
2575 sin6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
2576 router_ll6
= &sin6
->sin6_addr
;
2577 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6
))
2578 router_ll6
= (struct in6_addr
*)NULL
;
2580 router_ll6
= (struct in6_addr
*)NULL
;
2583 ip6
= mtod(m
, struct ip6_hdr
*);
2585 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2586 ip6
->ip6_vfc
|= IPV6_VERSION
;
2587 /* ip6->ip6_plen will be set later */
2588 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2589 ip6
->ip6_hlim
= 255;
2590 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2591 bcopy(&ifp_ll6
, &ip6
->ip6_src
, sizeof(struct in6_addr
));
2592 bcopy(&sip6
->ip6_src
, &ip6
->ip6_dst
, sizeof(struct in6_addr
));
2595 nd_rd
= (struct nd_redirect
*)(ip6
+ 1);
2596 nd_rd
->nd_rd_type
= ND_REDIRECT
;
2597 nd_rd
->nd_rd_code
= 0;
2598 nd_rd
->nd_rd_reserved
= 0;
2599 if (rt
->rt_flags
& RTF_GATEWAY
) {
2601 * nd_rd->nd_rd_target must be a link-local address in
2602 * better router cases.
2606 bcopy(router_ll6
, &nd_rd
->nd_rd_target
,
2607 sizeof(nd_rd
->nd_rd_target
));
2608 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2609 sizeof(nd_rd
->nd_rd_dst
));
2611 /* make sure redtgt == reddst */
2612 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_target
,
2613 sizeof(nd_rd
->nd_rd_target
));
2614 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2615 sizeof(nd_rd
->nd_rd_dst
));
2620 p
= (u_char
*)(nd_rd
+ 1);
2626 /* target lladdr option */
2627 struct rtentry
*rt_router
= NULL
;
2629 struct sockaddr_dl
*sdl
;
2630 struct nd_opt_hdr
*nd_opt
;
2633 /* Callee returns a locked route upon success */
2634 rt_router
= nd6_lookup(router_ll6
, 0, ifp
, 0);
2637 RT_LOCK_ASSERT_HELD(rt_router
);
2638 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2639 len
= (len
+ 7) & ~7; /* round by 8 */
2641 if (len
+ (p
- (u_char
*)ip6
) > maxlen
) {
2642 RT_REMREF_LOCKED(rt_router
);
2643 RT_UNLOCK(rt_router
);
2646 if (!(rt_router
->rt_flags
& RTF_GATEWAY
) &&
2647 (rt_router
->rt_flags
& RTF_LLINFO
) &&
2648 (rt_router
->rt_gateway
->sa_family
== AF_LINK
) &&
2649 (sdl
= (struct sockaddr_dl
*)rt_router
->rt_gateway
) &&
2651 nd_opt
= (struct nd_opt_hdr
*)p
;
2652 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
2653 nd_opt
->nd_opt_len
= len
>> 3;
2654 lladdr
= (char *)(nd_opt
+ 1);
2655 bcopy(LLADDR(sdl
), lladdr
, ifp
->if_addrlen
);
2658 RT_REMREF_LOCKED(rt_router
);
2659 RT_UNLOCK(rt_router
);
2663 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2665 /* just to be safe */
2666 #ifdef M_DECRYPTED /*not openbsd*/
2667 if (m0
->m_flags
& M_DECRYPTED
)
2670 if (p
- (u_char
*)ip6
> maxlen
)
2674 /* redirected header option */
2676 struct nd_opt_rd_hdr
*nd_opt_rh
;
2679 * compute the maximum size for icmp6 redirect header option.
2680 * XXX room for auth header?
2682 len
= maxlen
- (p
- (u_char
*)ip6
);
2685 /* This is just for simplicity. */
2686 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2688 m_freem(m0
->m_next
);
2691 m0
->m_pkthdr
.len
= m0
->m_len
;
2695 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2696 * about padding/truncate rule for the original IP packet.
2697 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2698 * - "attach as much as possible" is the goal
2699 * - pad if not aligned (original size can be guessed by original
2701 * Following code adds the padding if it is simple enough,
2702 * and truncates if not.
2704 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
)
2705 panic("assumption failed in %s:%d\n", __FILE__
, __LINE__
);
2707 if (len
- sizeof(*nd_opt_rh
) < m0
->m_pkthdr
.len
) {
2708 /* not enough room, truncate */
2709 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2711 /* enough room, pad or truncate */
2714 extra
= m0
->m_pkthdr
.len
% 8;
2716 /* pad if easy enough, truncate if not */
2717 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2719 m0
->m_len
+= (8 - extra
);
2720 m0
->m_pkthdr
.len
+= (8 - extra
);
2723 m0
->m_pkthdr
.len
-= extra
;
2727 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2728 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2731 nd_opt_rh
= (struct nd_opt_rd_hdr
*)p
;
2732 bzero(nd_opt_rh
, sizeof(*nd_opt_rh
));
2733 nd_opt_rh
->nd_opt_rh_type
= ND_OPT_REDIRECTED_HEADER
;
2734 nd_opt_rh
->nd_opt_rh_len
= len
>> 3;
2735 p
+= sizeof(*nd_opt_rh
);
2736 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2738 /* connect m0 to m */
2740 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
2744 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_src
))
2745 sip6
->ip6_src
.s6_addr16
[1] = 0;
2746 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_dst
))
2747 sip6
->ip6_dst
.s6_addr16
[1] = 0;
2749 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_src
))
2750 ip6
->ip6_src
.s6_addr16
[1] = 0;
2751 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_dst
))
2752 ip6
->ip6_dst
.s6_addr16
[1] = 0;
2754 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_target
))
2755 nd_rd
->nd_rd_target
.s6_addr16
[1] = 0;
2756 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_dst
))
2757 nd_rd
->nd_rd_dst
.s6_addr16
[1] = 0;
2759 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
2761 nd_rd
->nd_rd_cksum
= 0;
2763 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
2765 /* send the packet to outside... */
2767 /* Don't lookup socket */
2768 if (ipsec_bypass
== 0)
2769 (void)ipsec_setsocket(m
, NULL
);
2771 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
, 0);
2773 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
2774 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
2776 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
2790 #define sotoin6pcb sotoinpcb
2791 #define in6pcb inpcb
2792 #define in6p_icmp6filt inp_icmp6filt
2795 * ICMPv6 socket option processing.
2798 icmp6_ctloutput(so
, sopt
)
2800 struct sockopt
*sopt
;
2804 struct inpcb
*inp
= sotoinpcb(so
);
2805 int level
, op
, optname
;
2808 level
= sopt
->sopt_level
;
2809 op
= sopt
->sopt_dir
;
2810 optname
= sopt
->sopt_name
;
2811 optlen
= sopt
->sopt_valsize
;
2813 level
= op
= optname
= optlen
= 0;
2815 if (level
!= IPPROTO_ICMPV6
) {
2824 struct icmp6_filter
*p
;
2826 if (optlen
!= sizeof(*p
)) {
2830 if (inp
->in6p_icmp6filt
== NULL
) {
2834 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
2840 error
= ENOPROTOOPT
;
2849 if (inp
->in6p_icmp6filt
== NULL
) {
2853 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
2854 sizeof(struct icmp6_filter
));
2859 error
= ENOPROTOOPT
;
2870 #undef in6p_icmp6filt
2874 * ICMPv6 socket datagram option processing.
2877 icmp6_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2879 if (so
->so_uid
== 0)
2880 return icmp6_ctloutput(so
, sopt
);
2882 if (sopt
->sopt_level
== IPPROTO_ICMPV6
) {
2883 switch (sopt
->sopt_name
) {
2885 return icmp6_ctloutput(so
, sopt
);
2891 if (sopt
->sopt_level
!= IPPROTO_IPV6
)
2894 switch (sopt
->sopt_name
) {
2895 case IPV6_PKTOPTIONS
:
2896 case IPV6_UNICAST_HOPS
:
2905 case IPV6_MULTICAST_IF
:
2906 case IPV6_MULTICAST_HOPS
:
2907 case IPV6_MULTICAST_LOOP
:
2908 case IPV6_JOIN_GROUP
:
2909 case IPV6_LEAVE_GROUP
:
2910 case IPV6_PORTRANGE
:
2911 case IPV6_IPSEC_POLICY
:
2912 case IPV6_RECVTCLASS
:
2914 return ip6_ctloutput(so
, sopt
);
2923 __private_extern__
int
2924 icmp6_dgram_send(struct socket
*so
, __unused
int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
2925 struct mbuf
*control
, __unused
struct proc
*p
)
2928 struct inpcb
*inp
= sotoinpcb(so
);
2929 struct sockaddr_in6 tmp
;
2930 struct sockaddr_in6
*dst
;
2931 struct icmp6_hdr
*icmp6
;
2933 if (so
->so_uid
== 0)
2934 return rip6_output(m
, so
, (struct sockaddr_in6
*) nam
, control
);
2936 /* always copy sockaddr to avoid overwrites */
2937 if (so
->so_state
& SS_ISCONNECTED
) {
2943 bzero(&tmp
, sizeof(tmp
));
2944 tmp
.sin6_family
= AF_INET6
;
2945 tmp
.sin6_len
= sizeof(struct sockaddr_in6
);
2946 bcopy(&inp
->in6p_faddr
, &tmp
.sin6_addr
,
2947 sizeof(struct in6_addr
));
2954 tmp
= *(struct sockaddr_in6
*)nam
;
2959 * For an ICMPv6 packet, we should know its type and code
2961 if (so
->so_proto
->pr_protocol
== IPPROTO_ICMPV6
) {
2962 if (m
->m_len
< sizeof(struct icmp6_hdr
) &&
2963 (m
= m_pullup(m
, sizeof(struct icmp6_hdr
))) == NULL
) {
2967 icmp6
= mtod(m
, struct icmp6_hdr
*);
2970 * Allow only to send echo request and node information request
2971 * See RFC 2463 for Echo Request Message format
2973 if ((icmp6
->icmp6_type
== ICMP6_ECHO_REQUEST
&& icmp6
->icmp6_code
== 0) ||
2974 (icmp6
->icmp6_type
== ICMP6_NI_QUERY
&&
2975 (icmp6
->icmp6_code
== ICMP6_NI_SUBJ_IPV6
||
2976 icmp6
->icmp6_code
== ICMP6_NI_SUBJ_FQDN
))) {
2985 #if ENABLE_DEFAULT_SCOPE
2986 if (dst
->sin6_scope_id
== 0) { /* not change if specified */
2987 dst
->sin6_scope_id
= scope6_addr2default(&dst
->sin6_addr
);
2991 return rip6_output(m
, so
, (struct sockaddr_in6
*) nam
, control
);
2997 /* Like rip6_attach but without root privilege enforcement */
2998 __private_extern__
int
2999 icmp6_dgram_attach(struct socket
*so
, int proto
, struct proc
*p
)
3004 inp
= sotoinpcb(so
);
3006 panic("icmp6_dgram_attach");
3008 if (proto
!= IPPROTO_ICMPV6
)
3011 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
3014 error
= in_pcballoc(so
, &ripcbinfo
, p
);
3017 inp
= (struct inpcb
*)so
->so_pcb
;
3018 inp
->inp_vflag
|= INP_IPV6
;
3019 inp
->in6p_ip6_nxt
= IPPROTO_ICMPV6
;
3020 inp
->in6p_hops
= -1; /* use kernel default */
3021 inp
->in6p_cksum
= -1;
3022 MALLOC(inp
->in6p_icmp6filt
, struct icmp6_filter
*,
3023 sizeof(struct icmp6_filter
), M_PCB
, M_WAITOK
);
3024 if (inp
->in6p_icmp6filt
== NULL
)
3026 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
3032 #ifndef HAVE_PPSRATECHECK
3034 #define timersub(tvp, uvp, vvp) \
3036 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
3037 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
3038 if ((vvp)->tv_usec < 0) { \
3040 (vvp)->tv_usec += 1000000; \
3046 * ppsratecheck(): packets (or events) per second limitation.
3049 ppsratecheck(lasttime
, curpps
, maxpps
)
3050 struct timeval
*lasttime
;
3052 int maxpps
; /* maximum pps allowed */
3054 struct timeval tv
, delta
;
3059 timersub(&tv
, lasttime
, &delta
);
3062 * Check for 0,0 so that the message will be seen at least once.
3063 * If more than one second has passed since the last update of
3064 * lasttime, reset the counter.
3066 * we do increment *curpps even in *curpps < maxpps case, as some may
3067 * try to use *curpps for stat purposes as well.
3069 if ((lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0) ||
3070 delta
.tv_sec
>= 1) {
3074 } else if (maxpps
< 0)
3076 else if (*curpps
< maxpps
)
3081 #if 1 /* DIAGNOSTIC? */
3082 /* be careful about wrap-around */
3083 if (*curpps
+ 1 > 0)
3084 *curpps
= *curpps
+ 1;
3087 * assume that there's not too many calls to this function.
3088 * not sure if the assumption holds, as it depends on *caller's*
3089 * behavior, not the behavior of this function.
3090 * IMHO it is wrong to make assumption on the caller's behavior,
3091 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
3093 *curpps
= *curpps
+ 1;
3101 * Perform rate limit check.
3102 * Returns 0 if it is okay to send the icmp6 packet.
3103 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
3106 * XXX per-destination/type check necessary?
3110 __unused
const struct in6_addr
*dst
, /* not used at this moment */
3111 __unused
const int type
, /* not used at this moment */
3112 __unused
const int code
) /* not used at this moment */
3116 ret
= 0; /* okay to send */
3119 if (!ppsratecheck(&icmp6errppslim_last
, &icmp6errpps_count
,
3121 /* The packet is subject to rate limit */