2 * Copyright (c) 2000-2012 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/mcache.h>
102 #include <sys/mbuf.h>
103 #include <sys/protosw.h>
104 #include <sys/socket.h>
105 #include <sys/socketvar.h>
106 #include <sys/time.h>
107 #include <sys/kernel.h>
108 #include <sys/syslog.h>
109 #include <sys/domain.h>
110 #include <sys/kauth.h>
113 #include <net/route.h>
114 #include <net/if_dl.h>
115 #include <net/if_types.h>
117 #include <netinet/in.h>
118 #include <netinet/in_var.h>
119 #include <netinet/ip6.h>
120 #include <netinet6/ip6_var.h>
121 #include <netinet/icmp6.h>
122 #include <netinet6/mld6_var.h>
123 #include <netinet/in_pcb.h>
124 #include <netinet6/in6_pcb.h>
125 #include <netinet6/nd6.h>
126 #include <netinet6/in6_ifattach.h>
127 #include <netinet6/ip6protosw.h>
128 #include <netinet6/scope6_var.h>
131 #include <netinet6/ipsec.h>
132 #include <netkey/key.h>
134 extern int ipsec_bypass
;
137 #include <net/net_osdep.h>
139 extern struct domain inet6domain
;
140 extern struct ip6protosw inet6sw
[];
141 extern struct ip6protosw
*ip6_protox
[];
143 extern uint32_t rip_sendspace
;
144 extern uint32_t rip_recvspace
;
146 struct icmp6stat icmp6stat
;
148 extern struct inpcbhead ripcb
;
149 extern int icmp6errppslim
;
150 static int icmp6errpps_count
= 0;
151 static struct timeval icmp6errppslim_last
;
152 extern int icmp6_nodeinfo
;
153 extern struct inpcbinfo ripcbinfo
;
154 extern lck_mtx_t
*inet6_domain_mutex
;
156 static void icmp6_errcount(struct icmp6errstat
*, int, int);
157 static int icmp6_rip6_input(struct mbuf
**, int);
158 static int icmp6_ratelimit(const struct in6_addr
*, const int, const int);
159 static const char *icmp6_redirect_diag(struct in6_addr
*,
160 struct in6_addr
*, struct in6_addr
*);
161 #ifndef HAVE_PPSRATECHECK
162 static int ppsratecheck(struct timeval
*, int *, int);
164 static struct mbuf
*ni6_input(struct mbuf
*, int);
165 static struct mbuf
*ni6_nametodns(const char *, int, int);
166 static int ni6_dnsmatch(const char *, int, const char *, int);
167 static int ni6_addrs(struct icmp6_nodeinfo
*,
168 struct ifnet
**, char *);
169 static int ni6_store_addrs(struct icmp6_nodeinfo
*, struct icmp6_nodeinfo
*,
170 struct ifnet
*, int);
171 static int icmp6_notify_error(struct mbuf
*, int, int, int);
182 icmp6_errcount(stat
, type
, code
)
183 struct icmp6errstat
*stat
;
187 case ICMP6_DST_UNREACH
:
189 case ICMP6_DST_UNREACH_NOROUTE
:
190 stat
->icp6errs_dst_unreach_noroute
++;
192 case ICMP6_DST_UNREACH_ADMIN
:
193 stat
->icp6errs_dst_unreach_admin
++;
195 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
196 stat
->icp6errs_dst_unreach_beyondscope
++;
198 case ICMP6_DST_UNREACH_ADDR
:
199 stat
->icp6errs_dst_unreach_addr
++;
201 case ICMP6_DST_UNREACH_NOPORT
:
202 stat
->icp6errs_dst_unreach_noport
++;
206 case ICMP6_PACKET_TOO_BIG
:
207 stat
->icp6errs_packet_too_big
++;
209 case ICMP6_TIME_EXCEEDED
:
211 case ICMP6_TIME_EXCEED_TRANSIT
:
212 stat
->icp6errs_time_exceed_transit
++;
214 case ICMP6_TIME_EXCEED_REASSEMBLY
:
215 stat
->icp6errs_time_exceed_reassembly
++;
219 case ICMP6_PARAM_PROB
:
221 case ICMP6_PARAMPROB_HEADER
:
222 stat
->icp6errs_paramprob_header
++;
224 case ICMP6_PARAMPROB_NEXTHEADER
:
225 stat
->icp6errs_paramprob_nextheader
++;
227 case ICMP6_PARAMPROB_OPTION
:
228 stat
->icp6errs_paramprob_option
++;
233 stat
->icp6errs_redirect
++;
236 stat
->icp6errs_unknown
++;
240 * A wrapper function for icmp6_error() necessary when the erroneous packet
241 * may not contain enough scope zone information.
244 icmp6_error2(struct mbuf
*m
, int type
, int code
, int param
,
252 #ifndef PULLDOWN_TEST
253 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
),return );
255 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
256 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
262 ip6
= mtod(m
, struct ip6_hdr
*);
264 if (in6_setscope(&ip6
->ip6_src
, ifp
, NULL
) != 0)
266 if (in6_setscope(&ip6
->ip6_dst
, ifp
, NULL
) != 0)
269 icmp6_error(m
, type
, code
, param
);
273 * Generate an error packet of type error in response to bad IP6 packet.
276 icmp6_error(struct mbuf
*m
, int type
, int code
, int param
)
278 struct ip6_hdr
*oip6
, *nip6
;
279 struct icmp6_hdr
*icmp6
;
284 icmp6stat
.icp6s_error
++;
286 /* count per-type-code statistics */
287 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, type
, code
);
289 #ifdef M_DECRYPTED /*not openbsd*/
290 if (m
->m_flags
& M_DECRYPTED
) {
291 icmp6stat
.icp6s_canterror
++;
296 #ifndef PULLDOWN_TEST
297 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return);
299 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
300 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
305 oip6
= mtod(m
, struct ip6_hdr
*);
308 * If the destination address of the erroneous packet is a multicast
309 * address, or the packet was sent using link-layer multicast,
310 * we should basically suppress sending an error (RFC 2463, Section
312 * We have two exceptions (the item e.2 in that section):
313 * - the Pakcet Too Big message can be sent for path MTU discovery.
314 * - the Parameter Problem Message that can be allowed an icmp6 error
315 * in the option type field. This check has been done in
316 * ip6_unknown_opt(), so we can just check the type and code.
318 if ((m
->m_flags
& (M_BCAST
|M_MCAST
) ||
319 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_dst
)) &&
320 (type
!= ICMP6_PACKET_TOO_BIG
&&
321 (type
!= ICMP6_PARAM_PROB
||
322 code
!= ICMP6_PARAMPROB_OPTION
)))
326 * RFC 2463, 2.4 (e.5): source address check.
327 * XXX: the case of anycast source?
329 if (IN6_IS_ADDR_UNSPECIFIED(&oip6
->ip6_src
) ||
330 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_src
))
334 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
338 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
339 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
340 struct icmp6_hdr
*icp
;
342 #ifndef PULLDOWN_TEST
343 IP6_EXTHDR_CHECK(m
, 0, off
+ sizeof(struct icmp6_hdr
), return);
344 icp
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
346 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
349 icmp6stat
.icp6s_tooshort
++;
353 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
354 icp
->icmp6_type
== ND_REDIRECT
) {
357 * Special case: for redirect (which is
358 * informational) we must not send icmp6 error.
360 icmp6stat
.icp6s_canterror
++;
363 /* ICMPv6 informational - send the error */
366 /* non-ICMPv6 - send the error */
369 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
371 /* Finally, do rate limitation check. */
372 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
373 icmp6stat
.icp6s_toofreq
++;
378 * OK, ICMP6 can be generated.
381 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
)
382 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
384 preplen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
385 M_PREPEND(m
, preplen
, M_DONTWAIT
);
386 if (m
&& m
->m_len
< preplen
)
387 m
= m_pullup(m
, preplen
);
389 nd6log((LOG_DEBUG
, "ENOBUFS in icmp6_error %d\n", __LINE__
));
393 nip6
= mtod(m
, struct ip6_hdr
*);
394 nip6
->ip6_src
= oip6
->ip6_src
;
395 nip6
->ip6_dst
= oip6
->ip6_dst
;
397 in6_clearscope(&oip6
->ip6_src
);
398 in6_clearscope(&oip6
->ip6_dst
);
400 icmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
401 icmp6
->icmp6_type
= type
;
402 icmp6
->icmp6_code
= code
;
403 icmp6
->icmp6_pptr
= htonl((u_int32_t
)param
);
406 * icmp6_reflect() is designed to be in the input path.
407 * icmp6_error() can be called from both input and output path,
408 * and if we are in output path rcvif could contain bogus value.
409 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
410 * information in ip header (nip6).
412 m
->m_pkthdr
.rcvif
= NULL
;
414 icmp6stat
.icp6s_outhist
[type
]++;
415 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /* header order: IPv6 - ICMPv6 */
421 * If we can't tell whether or not we can generate ICMP6, free it.
427 * Process a received ICMP6 message.
430 icmp6_input(struct mbuf
**mp
, int *offp
, int proto
)
432 #pragma unused(proto)
433 struct mbuf
*m
= *mp
, *n
;
435 struct ip6_hdr
*ip6
, *nip6
;
436 struct icmp6_hdr
*icmp6
, *nicmp6
;
438 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
439 int code
, sum
, noff
, proxy
= 0;
441 ifp
= m
->m_pkthdr
.rcvif
;
443 #ifndef PULLDOWN_TEST
444 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_hdr
), return IPPROTO_DONE
);
445 /* m might change if M_LOOP. So, call mtod after this */
448 /* Expect 32-bit aligned data pointer on strict-align platforms */
449 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
452 * Locate icmp6 structure in mbuf, and check
453 * that not corrupted and of at least minimum length
455 ip6
= mtod(m
, struct ip6_hdr
*);
456 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
457 icmp6stat
.icp6s_tooshort
++;
462 * Check multicast group membership.
463 * Note: SSM filters are not applied for ICMPv6 traffic.
465 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
466 struct in6_multi
*inm
;
468 in6_multihead_lock_shared();
469 IN6_LOOKUP_MULTI(&ip6
->ip6_dst
, ifp
, inm
);
470 in6_multihead_lock_done();
474 * Don't discard if this is a Neighbor Solicitation
475 * that needs to be proxied (see check down below.)
477 if (!(m
->m_pkthdr
.aux_flags
& MAUXF_PROXY_DST
)) {
478 ip6stat
.ip6s_notmember
++;
479 in6_ifstat_inc(m
->m_pkthdr
.rcvif
,
489 * calculate the checksum
491 #ifndef PULLDOWN_TEST
492 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
494 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
496 icmp6stat
.icp6s_tooshort
++;
500 code
= icmp6
->icmp6_code
;
502 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
504 "ICMP6 checksum error(%d|%x) %s\n",
505 icmp6
->icmp6_type
, sum
, ip6_sprintf(&ip6
->ip6_src
)));
506 icmp6stat
.icp6s_checksum
++;
510 if (m
->m_pkthdr
.aux_flags
& MAUXF_PROXY_DST
) {
512 * This is the special case of proxying NS (dst is either
513 * solicited-node multicast or unicast); process it locally
514 * but don't deliver it to sockets. It practically lets us
515 * steer the packet to nd6_prproxy_ns_input, where more
516 * specific tests and actions will be taken.
518 switch (icmp6
->icmp6_type
) {
519 case ND_NEIGHBOR_SOLICIT
:
527 icmp6stat
.icp6s_inhist
[icmp6
->icmp6_type
]++;
528 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_msg
);
529 if (icmp6
->icmp6_type
< ICMP6_INFOMSG_MASK
)
530 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_error
);
532 switch (icmp6
->icmp6_type
) {
533 case ICMP6_DST_UNREACH
:
534 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
536 case ICMP6_DST_UNREACH_NOROUTE
:
537 code
= PRC_UNREACH_NET
;
539 case ICMP6_DST_UNREACH_ADMIN
:
540 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_adminprohib
);
541 code
= PRC_UNREACH_PROTOCOL
; /* is this a good code? */
543 case ICMP6_DST_UNREACH_ADDR
:
546 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
547 /* I mean "source address was incorrect." */
548 code
= PRC_PARAMPROB
;
550 case ICMP6_DST_UNREACH_NOPORT
:
551 code
= PRC_UNREACH_PORT
;
559 case ICMP6_PACKET_TOO_BIG
:
560 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
565 * Updating the path MTU will be done after examining
566 * intermediate extension headers.
571 case ICMP6_TIME_EXCEEDED
:
572 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
574 case ICMP6_TIME_EXCEED_TRANSIT
:
575 code
= PRC_TIMXCEED_INTRANS
;
577 case ICMP6_TIME_EXCEED_REASSEMBLY
:
578 code
= PRC_TIMXCEED_REASS
;
586 case ICMP6_PARAM_PROB
:
587 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
589 case ICMP6_PARAMPROB_NEXTHEADER
:
590 code
= PRC_UNREACH_PROTOCOL
;
592 case ICMP6_PARAMPROB_HEADER
:
593 case ICMP6_PARAMPROB_OPTION
:
594 code
= PRC_PARAMPROB
;
602 case ICMP6_ECHO_REQUEST
:
603 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
607 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
608 icmp6stat
.icp6s_toofreq
++;
612 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
614 goto rate_limit_checked
;
617 if ((n
->m_flags
& M_EXT
) != 0
618 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
620 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
623 * Prepare an internal mbuf. m_pullup() doesn't
624 * always copy the length we specified.
626 if (maxlen
>= MCLBYTES
) {
629 goto rate_limit_checked
;
632 MGETHDR(n
, M_DONTWAIT
, n0
->m_type
); /* MAC-OK */
633 if (n
&& maxlen
>= MHLEN
) {
634 MCLGET(n
, M_DONTWAIT
);
635 if ((n
->m_flags
& M_EXT
) == 0) {
643 goto rate_limit_checked
;
646 M_COPY_PKTHDR(n
, n0
);
648 * Copy IPv6 and ICMPv6 only.
650 nip6
= mtod(n
, struct ip6_hdr
*);
651 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
652 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
653 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
654 noff
= sizeof(struct ip6_hdr
);
655 n
->m_pkthdr
.len
= n
->m_len
=
656 noff
+ sizeof(struct icmp6_hdr
);
658 * Adjust mbuf. ip6_plen will be adjusted in
661 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
662 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
664 n0
->m_flags
&= ~M_PKTHDR
;
666 nip6
= mtod(n
, struct ip6_hdr
*);
667 IP6_EXTHDR_GET(nicmp6
, struct icmp6_hdr
*, n
, off
,
672 panic("nicmp6 is NULL in %s, which isn't good!\n", __FUNCTION__
);
674 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
675 nicmp6
->icmp6_code
= 0;
678 icmp6stat
.icp6s_reflect
++;
679 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
680 icmp6_reflect(n
, noff
);
682 goto rate_limit_checked
;
685 case ICMP6_ECHO_REPLY
:
686 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
691 case MLD_LISTENER_QUERY
:
692 case MLD_LISTENER_REPORT
:
694 if (icmp6len
< sizeof(struct mld_hdr
))
696 if (icmp6
->icmp6_type
== MLD_LISTENER_QUERY
) /* XXX: ugly... */
697 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
699 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
701 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
702 icmp6stat
.icp6s_toofreq
++;
706 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
708 if (mld_input(m
, off
, icmp6len
) == IPPROTO_DONE
)
712 if (mld_input(n
, off
, icmp6len
) != IPPROTO_DONE
)
715 goto rate_limit_checked
;
718 case MLD_LISTENER_DONE
:
719 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
720 if (icmp6len
< sizeof(struct mld_hdr
)) /* necessary? */
722 break; /* nothing to be done in kernel */
724 case MLD_MTRACE_RESP
:
726 /* XXX: these two are experimental. not officially defined. */
727 /* XXX: per-interface statistics? */
728 break; /* just pass it to applications */
733 //### LD 10/20 Check fbsd differences here. Not sure we're more advanced or not.
734 /* By RFC 4620 refuse to answer queries from global scope addresses */
735 if ((icmp6_nodeinfo
& 8) != 8 && in6_addrscope(&ip6
->ip6_src
) == IPV6_ADDR_SCOPE_GLOBAL
)
738 if (icmp6len
< sizeof(struct icmp6_nodeinfo
))
741 #ifndef PULLDOWN_TEST
742 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
743 return IPPROTO_DONE
);
745 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
746 icmp6stat
.icp6s_toofreq
++;
750 n
= m_copy(m
, 0, M_COPYALL
);
752 n
= ni6_input(n
, off
);
754 noff
= sizeof(struct ip6_hdr
);
755 icmp6stat
.icp6s_reflect
++;
756 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
757 icmp6_reflect(n
, noff
);
759 goto rate_limit_checked
;
767 case ND_ROUTER_SOLICIT
:
768 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
771 if (icmp6len
< sizeof(struct nd_router_solicit
))
774 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
775 icmp6stat
.icp6s_toofreq
++;
779 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
781 nd6_rs_input(m
, off
, icmp6len
);
785 nd6_rs_input(n
, off
, icmp6len
);
787 goto rate_limit_checked
;
790 case ND_ROUTER_ADVERT
:
791 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
794 if (icmp6len
< sizeof(struct nd_router_advert
))
797 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
798 icmp6stat
.icp6s_toofreq
++;
802 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
804 nd6_ra_input(m
, off
, icmp6len
);
808 nd6_ra_input(n
, off
, icmp6len
);
810 goto rate_limit_checked
;
813 case ND_NEIGHBOR_SOLICIT
:
814 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
817 if (icmp6len
< sizeof(struct nd_neighbor_solicit
))
820 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
821 icmp6stat
.icp6s_toofreq
++;
825 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
827 nd6_ns_input(m
, off
, icmp6len
);
831 nd6_ns_input(n
, off
, icmp6len
);
833 goto rate_limit_checked
;
836 case ND_NEIGHBOR_ADVERT
:
837 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
840 if (icmp6len
< sizeof(struct nd_neighbor_advert
))
843 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
844 icmp6stat
.icp6s_toofreq
++;
848 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
850 nd6_na_input(m
, off
, icmp6len
);
854 nd6_na_input(n
, off
, icmp6len
);
856 goto rate_limit_checked
;
860 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
863 if (icmp6len
< sizeof(struct nd_redirect
))
866 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
867 icmp6stat
.icp6s_toofreq
++;
871 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
873 icmp6_redirect_input(m
, off
);
877 icmp6_redirect_input(n
, off
);
879 goto rate_limit_checked
;
882 case ICMP6_ROUTER_RENUMBERING
:
883 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
884 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
)
886 if (icmp6len
< sizeof(struct icmp6_router_renum
))
891 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
892 icmp6stat
.icp6s_toofreq
++;
897 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
898 icmp6
->icmp6_type
, ip6_sprintf(&ip6
->ip6_src
),
899 ip6_sprintf(&ip6
->ip6_dst
),
900 m
->m_pkthdr
.rcvif
? m
->m_pkthdr
.rcvif
->if_index
: 0));
901 if (icmp6
->icmp6_type
< ICMP6_ECHO_REQUEST
) {
902 /* ICMPv6 error: MUST deliver it by spec... */
906 /* ICMPv6 informational: MUST not deliver */
907 goto rate_limit_checked
;
911 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
912 icmp6stat
.icp6s_toofreq
++;
916 if (icmp6_notify_error(m
, off
, icmp6len
, code
)) {
917 /* In this case, m should've been freed. */
918 return(IPPROTO_DONE
);
923 icmp6stat
.icp6s_badcode
++;
927 icmp6stat
.icp6s_badlen
++;
931 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
932 icmp6stat
.icp6s_toofreq
++;
936 /* deliver the packet to appropriate sockets (unless proxying) */
938 icmp6_rip6_input(&m
, *offp
);
948 icmp6_notify_error(m
, off
, icmp6len
, code
)
950 int off
, icmp6len
, code
;
952 struct icmp6_hdr
*icmp6
;
953 struct ip6_hdr
*eip6
;
955 struct sockaddr_in6 icmp6src
, icmp6dst
;
957 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
958 icmp6stat
.icp6s_tooshort
++;
961 #ifndef PULLDOWN_TEST
962 IP6_EXTHDR_CHECK(m
, off
,
963 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
965 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
967 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
968 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
970 icmp6stat
.icp6s_tooshort
++;
974 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
976 /* Detect the upper level protocol */
978 void (*ctlfunc
)(int, struct sockaddr
*, void *);
979 u_int8_t nxt
= eip6
->ip6_nxt
;
980 int eoff
= off
+ sizeof(struct icmp6_hdr
) +
981 sizeof(struct ip6_hdr
);
982 struct ip6ctlparam ip6cp
;
983 struct in6_addr
*finaldst
= NULL
;
984 int icmp6type
= icmp6
->icmp6_type
;
986 struct ip6_rthdr
*rth
;
987 struct ip6_rthdr0
*rth0
;
990 while (1) { /* XXX: should avoid infinite loop explicitly? */
994 case IPPROTO_HOPOPTS
:
995 case IPPROTO_DSTOPTS
:
997 #ifndef PULLDOWN_TEST
998 IP6_EXTHDR_CHECK(m
, 0, eoff
+
999 sizeof(struct ip6_ext
),
1001 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
1004 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
1007 icmp6stat
.icp6s_tooshort
++;
1012 if (nxt
== IPPROTO_AH
)
1013 eoff
+= (eh
->ip6e_len
+ 2) << 2;
1015 eoff
+= (eh
->ip6e_len
+ 1) << 3;
1018 case IPPROTO_ROUTING
:
1020 * When the erroneous packet contains a
1021 * routing header, we should examine the
1022 * header to determine the final destination.
1023 * Otherwise, we can't properly update
1024 * information that depends on the final
1025 * destination (e.g. path MTU).
1027 #ifndef PULLDOWN_TEST
1028 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
1030 rth
= (struct ip6_rthdr
*)(mtod(m
, caddr_t
)
1033 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
1034 eoff
, sizeof(*rth
));
1036 icmp6stat
.icp6s_tooshort
++;
1040 rthlen
= (rth
->ip6r_len
+ 1) << 3;
1042 * XXX: currently there is no
1043 * officially defined type other
1045 * Note that if the segment left field
1046 * is 0, all intermediate hops must
1049 if (rth
->ip6r_segleft
&&
1050 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
1053 #ifndef PULLDOWN_TEST
1054 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
1056 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
1058 IP6_EXTHDR_GET(rth0
,
1059 struct ip6_rthdr0
*, m
,
1062 icmp6stat
.icp6s_tooshort
++;
1066 /* just ignore a bogus header */
1067 if ((rth0
->ip6r0_len
% 2) == 0 &&
1068 (hops
= rth0
->ip6r0_len
/2))
1069 finaldst
= (struct in6_addr
*)(void *)(rth0
+ 1) + (hops
- 1);
1072 nxt
= rth
->ip6r_nxt
;
1074 case IPPROTO_FRAGMENT
:
1075 #ifndef PULLDOWN_TEST
1076 IP6_EXTHDR_CHECK(m
, 0, eoff
+
1077 sizeof(struct ip6_frag
),
1079 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
1082 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
1085 icmp6stat
.icp6s_tooshort
++;
1090 * Data after a fragment header is meaningless
1091 * unless it is the first fragment, but
1092 * we'll go to the notify label for path MTU
1095 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
)
1098 eoff
+= sizeof(struct ip6_frag
);
1103 * This case includes ESP and the No Next
1104 * Header. In such cases going to the notify
1105 * label does not have any meaning
1106 * (i.e. ctlfunc will be NULL), but we go
1107 * anyway since we might have to update
1108 * path MTU information.
1114 #ifndef PULLDOWN_TEST
1115 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
1117 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
1118 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
1119 if (icmp6
== NULL
) {
1120 icmp6stat
.icp6s_tooshort
++;
1126 * retrieve parameters from the inner IPv6 header, and convert
1127 * them into sockaddr structures.
1128 * XXX: there is no guarantee that the source or destination
1129 * addresses of the inner packet are in the same scope as
1130 * the addresses of the icmp packet. But there is no other
1131 * way to determine the zone.
1133 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1135 bzero(&icmp6dst
, sizeof(icmp6dst
));
1136 icmp6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
1137 icmp6dst
.sin6_family
= AF_INET6
;
1138 if (finaldst
== NULL
)
1139 icmp6dst
.sin6_addr
= eip6
->ip6_dst
;
1141 icmp6dst
.sin6_addr
= *finaldst
;
1142 if (in6_setscope(&icmp6dst
.sin6_addr
, m
->m_pkthdr
.rcvif
, NULL
))
1144 bzero(&icmp6src
, sizeof(icmp6src
));
1145 icmp6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1146 icmp6src
.sin6_family
= AF_INET6
;
1147 icmp6src
.sin6_addr
= eip6
->ip6_src
;
1148 if (in6_setscope(&icmp6src
.sin6_addr
, m
->m_pkthdr
.rcvif
, NULL
))
1150 icmp6src
.sin6_flowinfo
=
1151 (eip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
);
1153 if (finaldst
== NULL
)
1154 finaldst
= &eip6
->ip6_dst
;
1156 ip6cp
.ip6c_icmp6
= icmp6
;
1157 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1158 ip6cp
.ip6c_off
= eoff
;
1159 ip6cp
.ip6c_finaldst
= finaldst
;
1160 ip6cp
.ip6c_src
= &icmp6src
;
1161 ip6cp
.ip6c_nxt
= nxt
;
1163 if (icmp6type
== ICMP6_PACKET_TOO_BIG
) {
1164 notifymtu
= ntohl(icmp6
->icmp6_mtu
);
1165 ip6cp
.ip6c_cmdarg
= (void *)¬ifymtu
;
1166 icmp6_mtudisc_update(&ip6cp
, 1); /*XXX*/
1169 ctlfunc
= (void (*)(int, struct sockaddr
*, void *))
1170 (ip6_protox
[nxt
]->pr_ctlinput
);
1172 (void) (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6dst
,
1184 icmp6_mtudisc_update(ip6cp
, validated
)
1185 struct ip6ctlparam
*ip6cp
;
1188 struct in6_addr
*dst
= ip6cp
->ip6c_finaldst
;
1189 struct icmp6_hdr
*icmp6
= ip6cp
->ip6c_icmp6
;
1190 struct mbuf
*m
= ip6cp
->ip6c_m
; /* will be necessary for scope issue */
1191 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1192 struct rtentry
*rt
= NULL
;
1193 struct sockaddr_in6 sin6
;
1195 * we reject ICMPv6 too big with abnormally small value.
1196 * XXX what is the good definition of "abnormally small"?
1198 if (mtu
< sizeof(struct ip6_hdr
) + sizeof(struct ip6_frag
) + 8)
1205 * In case the suggested mtu is less than IPV6_MMTU, we
1206 * only need to remember that it was for above mentioned
1207 * "alwaysfrag" case.
1208 * Try to be as close to the spec as possible.
1210 if (mtu
< IPV6_MMTU
)
1211 mtu
= IPV6_MMTU
- 8;
1213 bzero(&sin6
, sizeof(sin6
));
1214 sin6
.sin6_family
= PF_INET6
;
1215 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1216 sin6
.sin6_addr
= *dst
;
1217 /* XXX normally, this won't happen */
1218 if (IN6_IS_ADDR_LINKLOCAL(dst
)) {
1219 sin6
.sin6_addr
.s6_addr16
[1] =
1220 htons(m
->m_pkthdr
.rcvif
->if_index
);
1222 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1223 rt
= rtalloc1_scoped((struct sockaddr
*)&sin6
, 0,
1224 RTF_CLONING
| RTF_PRCLONING
, m
->m_pkthdr
.rcvif
->if_index
);
1227 if ((rt
->rt_flags
& RTF_HOST
) &&
1228 !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
) &&
1229 mtu
< IN6_LINKMTU(rt
->rt_ifp
) &&
1230 rt
->rt_rmx
.rmx_mtu
> mtu
) {
1231 icmp6stat
.icp6s_pmtuchg
++;
1232 rt
->rt_rmx
.rmx_mtu
= mtu
;
1240 * Process a Node Information Query packet, based on
1241 * draft-ietf-ipngwg-icmp-name-lookups-07.
1243 * Spec incompatibilities:
1244 * - IPv6 Subject address handling
1245 * - IPv4 Subject address handling support missing
1246 * - Proxy reply (answer even if it's not for me)
1247 * - joins NI group address at in6_ifattach() time only, does not cope
1248 * with hostname changes by sethostname(3)
1250 #define hostnamelen strlen(hostname)
1251 static struct mbuf
*
1256 struct icmp6_nodeinfo
*ni6
, *nni6
;
1257 struct mbuf
*n
= NULL
;
1260 int replylen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1261 struct ni_reply_fqdn
*fqdn
;
1262 int addrs
; /* for NI_QTYPE_NODEADDR */
1263 struct ifnet
*ifp
= NULL
; /* for NI_QTYPE_NODEADDR */
1264 struct sockaddr_in6 sin6
; /* double meaning; ip6_dst and subjectaddr */
1265 struct sockaddr_in6 sin6_d
; /* XXX: we should retrieve this from m_aux */
1266 struct ip6_hdr
*ip6
;
1267 int oldfqdn
= 0; /* if 1, return pascal string (03 draft) */
1270 ip6
= mtod(m
, struct ip6_hdr
*);
1271 #ifndef PULLDOWN_TEST
1272 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1274 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1276 /* m is already reclaimed */
1282 * Validate IPv6 source address.
1283 * The default configuration MUST be to refuse answering queries from
1284 * global-scope addresses according to RFC4602.
1286 * - it's not very clear what "refuse" means; this implementation
1288 * - it's not very easy to identify global-scope (unicast) addresses
1289 * since there are many prefixes for them. It should be safer
1290 * and in practice sufficient to check "all" but loopback and
1291 * link-local (note that site-local unicast was deprecated and
1292 * ULA is defined as global scope-wise)
1294 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_GLOBALOK
) == 0 &&
1295 !IN6_IS_ADDR_LOOPBACK(&ip6
->ip6_src
) &&
1296 !IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_src
))
1300 * Validate IPv6 destination address.
1302 * The Responder must discard the Query without further processing
1303 * unless it is one of the Responder's unicast or anycast addresses, or
1304 * a link-local scope multicast address which the Responder has joined.
1305 * [RFC4602, Section 5.]
1307 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
1308 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6
->ip6_dst
))
1310 /* else it's a link-local multicast, fine */
1311 } else { /* unicast or anycast */
1312 struct in6_ifaddr
*ia6
;
1314 if ((ia6
= ip6_getdstifaddr(m
)) == NULL
)
1315 goto bad
; /* XXX impossible */
1317 IFA_LOCK(&ia6
->ia_ifa
);
1318 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) &&
1319 !(icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
)) {
1320 nd6log((LOG_DEBUG
, "ni6_input: ignore node info to "
1321 "a temporary address in %s:%d",
1322 __FILE__
, __LINE__
));
1323 IFA_UNLOCK(&ia6
->ia_ifa
);
1324 IFA_REMREF(&ia6
->ia_ifa
);
1327 IFA_UNLOCK(&ia6
->ia_ifa
);
1328 IFA_REMREF(&ia6
->ia_ifa
);
1331 /* validate query Subject field. */
1332 qtype
= ntohs(ni6
->ni_qtype
);
1333 subjlen
= m
->m_pkthdr
.len
- off
- sizeof(struct icmp6_nodeinfo
);
1336 case NI_QTYPE_SUPTYPES
:
1338 if (ni6
->ni_code
== ICMP6_NI_SUBJ_FQDN
&& subjlen
== 0)
1342 case NI_QTYPE_NODEADDR
:
1343 case NI_QTYPE_IPV4ADDR
:
1344 switch (ni6
->ni_code
) {
1345 case ICMP6_NI_SUBJ_IPV6
:
1346 #if ICMP6_NI_SUBJ_IPV6 != 0
1350 * backward compatibility - try to accept 03 draft
1351 * format, where no Subject is present.
1353 if (qtype
== NI_QTYPE_FQDN
&& ni6
->ni_code
== 0 &&
1358 #if ICMP6_NI_SUBJ_IPV6 != 0
1359 if (ni6
->ni_code
!= ICMP6_NI_SUBJ_IPV6
)
1363 if (subjlen
!= sizeof(struct in6_addr
))
1367 * Validate Subject address.
1369 * Not sure what exactly "address belongs to the node"
1370 * means in the spec, is it just unicast, or what?
1372 * At this moment we consider Subject address as
1373 * "belong to the node" if the Subject address equals
1374 * to the IPv6 destination address; validation for
1375 * IPv6 destination address should have done enough
1378 * We do not do proxy at this moment.
1380 /* m_pulldown instead of copy? */
1381 m_copydata(m
, off
+ sizeof(struct icmp6_nodeinfo
),
1382 subjlen
, (caddr_t
)&sin6
.sin6_addr
);
1383 sin6
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1385 in6_embedscope(&sin6
.sin6_addr
, &sin6
, NULL
, NULL
,
1387 bzero(&sin6_d
, sizeof(sin6_d
));
1388 sin6_d
.sin6_family
= AF_INET6
; /* not used, actually */
1389 sin6_d
.sin6_len
= sizeof(sin6_d
); /* ditto */
1390 sin6_d
.sin6_addr
= ip6
->ip6_dst
;
1391 sin6_d
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1393 in6_embedscope(&sin6_d
.sin6_addr
, &sin6_d
, NULL
, NULL
,
1395 subj
= (char *)&sin6
;
1396 if (SA6_ARE_ADDR_EQUAL(&sin6
, &sin6_d
))
1400 * XXX if we are to allow other cases, we should really
1401 * be careful about scope here.
1402 * basically, we should disallow queries toward IPv6
1403 * destination X with subject Y,
1404 * if scope(X) > scope(Y).
1405 * if we allow scope(X) > scope(Y), it will result in
1406 * information leakage across scope boundary.
1410 case ICMP6_NI_SUBJ_FQDN
:
1412 * Validate Subject name with gethostname(3).
1414 * The behavior may need some debate, since:
1415 * - we are not sure if the node has FQDN as
1416 * hostname (returned by gethostname(3)).
1417 * - the code does wildcard match for truncated names.
1418 * however, we are not sure if we want to perform
1419 * wildcard match, if gethostname(3) side has
1420 * truncated hostname.
1422 n
= ni6_nametodns(hostname
, hostnamelen
, 0);
1423 if (!n
|| n
->m_next
|| n
->m_len
== 0)
1425 IP6_EXTHDR_GET(subj
, char *, m
,
1426 off
+ sizeof(struct icmp6_nodeinfo
), subjlen
);
1429 if (!ni6_dnsmatch(subj
, subjlen
, mtod(n
, const char *),
1437 case ICMP6_NI_SUBJ_IPV4
: /* XXX: to be implemented? */
1444 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1447 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_FQDNOK
) == 0)
1450 case NI_QTYPE_NODEADDR
:
1451 case NI_QTYPE_IPV4ADDR
:
1452 if ((icmp6_nodeinfo
& ICMP6_NODEINFO_NODEADDROK
) == 0)
1457 /* guess reply length */
1460 break; /* no reply data */
1461 case NI_QTYPE_SUPTYPES
:
1462 replylen
+= sizeof(u_int32_t
);
1465 /* XXX will append an mbuf */
1466 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1468 case NI_QTYPE_NODEADDR
:
1469 addrs
= ni6_addrs(ni6
, &ifp
, subj
);
1470 if ((replylen
+= addrs
* (sizeof(struct in6_addr
) +
1471 sizeof(u_int32_t
))) > MCLBYTES
)
1472 replylen
= MCLBYTES
; /* XXX: will truncate pkt later */
1474 case NI_QTYPE_IPV4ADDR
:
1475 /* unsupported - should respond with unknown Qtype? */
1479 * XXX: We must return a reply with the ICMP6 code
1480 * `unknown Qtype' in this case. However we regard the case
1481 * as an FQDN query for backward compatibility.
1482 * Older versions set a random value to this field,
1483 * so it rarely varies in the defined qtypes.
1484 * But the mechanism is not reliable...
1485 * maybe we should obsolete older versions.
1487 qtype
= NI_QTYPE_FQDN
;
1488 /* XXX will append an mbuf */
1489 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1494 /* allocate an mbuf to reply. */
1495 MGETHDR(n
, M_DONTWAIT
, m
->m_type
); /* MAC-OK */
1502 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1503 if (replylen
> MHLEN
) {
1504 if (replylen
> MCLBYTES
) {
1506 * XXX: should we try to allocate more? But MCLBYTES
1507 * is probably much larger than IPV6_MMTU...
1511 MCLGET(n
, M_DONTWAIT
);
1512 if ((n
->m_flags
& M_EXT
) == 0) {
1516 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
1518 /* copy mbuf header and IPv6 + Node Information base headers */
1519 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), sizeof(struct ip6_hdr
));
1520 nni6
= (struct icmp6_nodeinfo
*)(mtod(n
, struct ip6_hdr
*) + 1);
1521 bcopy((caddr_t
)ni6
, (caddr_t
)nni6
, sizeof(struct icmp6_nodeinfo
));
1523 /* qtype dependent procedure */
1526 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1529 case NI_QTYPE_SUPTYPES
:
1532 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1533 nni6
->ni_flags
= htons(0x0000); /* raw bitmap */
1534 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1535 v
= (u_int32_t
)htonl(0x0000000f);
1536 bcopy(&v
, nni6
+ 1, sizeof(u_int32_t
));
1540 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1541 fqdn
= (struct ni_reply_fqdn
*)(mtod(n
, caddr_t
) +
1542 sizeof(struct ip6_hdr
) +
1543 sizeof(struct icmp6_nodeinfo
));
1544 nni6
->ni_flags
= 0; /* XXX: meaningless TTL */
1545 fqdn
->ni_fqdn_ttl
= 0; /* ditto. */
1547 * XXX do we really have FQDN in variable "hostname"?
1549 n
->m_next
= ni6_nametodns(hostname
, hostnamelen
, oldfqdn
);
1550 if (n
->m_next
== NULL
)
1552 /* XXX we assume that n->m_next is not a chain */
1553 if (n
->m_next
->m_next
!= NULL
)
1555 n
->m_pkthdr
.len
+= n
->m_next
->m_len
;
1557 case NI_QTYPE_NODEADDR
:
1561 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1562 n
->m_pkthdr
.len
= n
->m_len
=
1563 sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1564 lenlim
= M_TRAILINGSPACE(n
);
1565 copied
= ni6_store_addrs(ni6
, nni6
, ifp
, lenlim
);
1566 /* XXX: reset mbuf length */
1567 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
1568 sizeof(struct icmp6_nodeinfo
) + copied
;
1572 break; /* XXX impossible! */
1575 nni6
->ni_type
= ICMP6_NI_REPLY
;
1592 * make a mbuf with DNS-encoded string. no compression support.
1594 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1595 * treated as truncated name (two \0 at the end). this is a wild guess.
1597 static struct mbuf
*
1598 ni6_nametodns(name
, namelen
, old
)
1601 int old
; /* return pascal string if non-zero */
1613 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1614 MGET(m
, M_DONTWAIT
, MT_DATA
);
1615 if (m
&& len
> MLEN
) {
1616 MCLGET(m
, M_DONTWAIT
);
1617 if ((m
->m_flags
& M_EXT
) == 0)
1626 *mtod(m
, char *) = namelen
;
1627 bcopy(name
, mtod(m
, char *) + 1, namelen
);
1631 cp
= mtod(m
, char *);
1632 ep
= mtod(m
, char *) + M_TRAILINGSPACE(m
);
1634 /* if not certain about my name, return empty buffer */
1639 * guess if it looks like shortened hostname, or FQDN.
1640 * shortened hostname needs two trailing "\0".
1643 for (p
= name
; p
< name
+ namelen
; p
++) {
1644 if (*p
&& *p
== '.')
1653 while (cp
< ep
&& p
< name
+ namelen
) {
1655 for (q
= p
; q
< name
+ namelen
&& *q
&& *q
!= '.'; q
++)
1657 /* result does not fit into mbuf */
1658 if (cp
+ i
+ 1 >= ep
)
1661 * DNS label length restriction, RFC1035 page 8.
1662 * "i == 0" case is included here to avoid returning
1663 * 0-length label on "foo..bar".
1665 if (i
<= 0 || i
>= 64)
1671 if (p
< name
+ namelen
&& *p
== '.')
1675 if (cp
+ nterm
>= ep
)
1679 m
->m_len
= cp
- mtod(m
, char *);
1683 panic("should not reach here");
1693 * check if two DNS-encoded string matches. takes care of truncated
1694 * form (with \0\0 at the end). no compression support.
1695 * XXX upper/lowercase match (see RFC2065)
1698 ni6_dnsmatch(a
, alen
, b
, blen
)
1704 const char *a0
, *b0
;
1707 /* simplest case - need validation? */
1708 if (alen
== blen
&& bcmp(a
, b
, alen
) == 0)
1714 /* termination is mandatory */
1715 if (alen
< 2 || blen
< 2)
1717 if (a0
[alen
- 1] != '\0' || b0
[blen
- 1] != '\0')
1722 while (a
- a0
< alen
&& b
- b0
< blen
) {
1723 if (a
- a0
+ 1 > alen
|| b
- b0
+ 1 > blen
)
1726 if ((signed char)a
[0] < 0 || (signed char)b
[0] < 0)
1728 /* we don't support compression yet */
1729 if (a
[0] >= 64 || b
[0] >= 64)
1732 /* truncated case */
1733 if (a
[0] == 0 && a
- a0
== alen
- 1)
1735 if (b
[0] == 0 && b
- b0
== blen
- 1)
1737 if (a
[0] == 0 || b
[0] == 0)
1743 if (a
- a0
+ 1 + l
> alen
|| b
- b0
+ 1 + l
> blen
)
1745 if (bcmp(a
+ 1, b
+ 1, l
) != 0)
1752 if (a
- a0
== alen
&& b
- b0
== blen
)
1759 * calculate the number of addresses to be returned in the node info reply.
1762 ni6_addrs(ni6
, ifpp
, subj
)
1763 struct icmp6_nodeinfo
*ni6
;
1764 struct ifnet
**ifpp
;
1768 struct in6_ifaddr
*ifa6
;
1770 struct sockaddr_in6
*subj_ip6
= NULL
; /* XXX pedant */
1771 int addrs
= 0, addrsofif
, iffound
= 0;
1772 int niflags
= ni6
->ni_flags
;
1777 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0) {
1778 switch (ni6
->ni_code
) {
1779 case ICMP6_NI_SUBJ_IPV6
:
1780 if (subj
== NULL
) /* must be impossible... */
1782 subj_ip6
= (struct sockaddr_in6
*)(void *)subj
;
1786 * XXX: we only support IPv6 subject address for
1793 ifnet_head_lock_shared();
1794 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
1796 ifnet_lock_shared(ifp
);
1797 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1800 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1804 ifa6
= (struct in6_ifaddr
*)ifa
;
1806 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0 &&
1807 IN6_ARE_ADDR_EQUAL(&subj_ip6
->sin6_addr
,
1808 &ifa6
->ia_addr
.sin6_addr
))
1812 * IPv4-mapped addresses can only be returned by a
1813 * Node Information proxy, since they represent
1814 * addresses of IPv4-only nodes, which perforce do
1815 * not implement this protocol.
1816 * [icmp-name-lookups-07, Section 5.4]
1817 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1818 * this function at this moment.
1821 /* What do we have to do about ::1? */
1822 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1823 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1824 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0) {
1829 case IPV6_ADDR_SCOPE_SITELOCAL
:
1830 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0) {
1835 case IPV6_ADDR_SCOPE_GLOBAL
:
1836 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0) {
1847 * check if anycast is okay.
1848 * XXX: just experimental. not in the spec.
1850 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1851 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0) {
1853 continue; /* we need only unicast addresses */
1855 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1856 (icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
) == 0) {
1860 addrsofif
++; /* count the address */
1863 ifnet_lock_done(ifp
);
1867 ifnet_reference(ifp
);
1881 ni6_store_addrs(ni6
, nni6
, ifp0
, resid
)
1882 struct icmp6_nodeinfo
*ni6
, *nni6
;
1886 struct ifnet
*ifp
= ifp0
;
1887 struct in6_ifaddr
*ifa6
;
1889 struct ifnet
*ifp_dep
= NULL
;
1890 int copied
= 0, allow_deprecated
= 0;
1891 u_char
*cp
= (u_char
*)(nni6
+ 1);
1892 int niflags
= ni6
->ni_flags
;
1894 struct timeval timenow
;
1896 getmicrotime(&timenow
);
1898 if (ifp0
== NULL
&& !(niflags
& NI_NODEADDR_FLAG_ALL
))
1899 return (0); /* needless to copy */
1903 ifnet_head_lock_shared();
1905 ifp
= TAILQ_FIRST(&ifnet_head
);
1907 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1908 ifnet_lock_shared(ifp
);
1909 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1910 ifa
= ifa
->ifa_list
.tqe_next
) {
1912 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
1916 ifa6
= (struct in6_ifaddr
*)ifa
;
1918 if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0 &&
1919 allow_deprecated
== 0) {
1921 * prefererred address should be put before
1922 * deprecated addresses.
1925 /* record the interface for later search */
1926 if (ifp_dep
== NULL
)
1931 } else if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) == 0 &&
1932 allow_deprecated
!= 0) {
1934 continue; /* we now collect deprecated addrs */
1936 /* What do we have to do about ::1? */
1937 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1938 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1939 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0) {
1944 case IPV6_ADDR_SCOPE_SITELOCAL
:
1945 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0) {
1950 case IPV6_ADDR_SCOPE_GLOBAL
:
1951 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0) {
1962 * check if anycast is okay.
1963 * XXX: just experimental. not in the spec.
1965 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1966 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0) {
1970 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1971 (icmp6_nodeinfo
& ICMP6_NODEINFO_TMPADDROK
) == 0) {
1976 /* now we can copy the address */
1977 if (resid
< sizeof(struct in6_addr
) +
1978 sizeof(u_int32_t
)) {
1981 * We give up much more copy.
1982 * Set the truncate flag and return.
1985 NI_NODEADDR_FLAG_TRUNCATE
;
1986 ifnet_lock_done(ifp
);
1992 * Set the TTL of the address.
1993 * The TTL value should be one of the following
1994 * according to the specification:
1996 * 1. The remaining lifetime of a DHCP lease on the
1998 * 2. The remaining Valid Lifetime of a prefix from
1999 * which the address was derived through Stateless
2000 * Autoconfiguration.
2002 * Note that we currently do not support stateful
2003 * address configuration by DHCPv6, so the former
2004 * case can't happen.
2006 if (ifa6
->ia6_lifetime
.ia6t_expire
== 0 &&
2007 (ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) == 0)
2008 ltime
= ND6_INFINITE_LIFETIME
;
2010 if (ifa6
->ia6_lifetime
.ia6t_expire
>
2012 ltime
= htonl(ifa6
->ia6_lifetime
.ia6t_expire
- timenow
.tv_sec
);
2017 bcopy(<ime
, cp
, sizeof(u_int32_t
));
2018 cp
+= sizeof(u_int32_t
);
2020 /* copy the address itself */
2021 bcopy(&ifa6
->ia_addr
.sin6_addr
, cp
,
2022 sizeof(struct in6_addr
));
2023 /* XXX: KAME link-local hack; remove ifindex */
2024 if (IN6_IS_ADDR_LINKLOCAL(&ifa6
->ia_addr
.sin6_addr
))
2025 ((struct in6_addr
*)(void *)cp
)->s6_addr16
[1] = 0;
2026 cp
+= sizeof(struct in6_addr
);
2028 resid
-= (sizeof(struct in6_addr
) + sizeof(u_int32_t
));
2029 copied
+= (sizeof(struct in6_addr
) +
2033 ifnet_lock_done(ifp
);
2034 if (ifp0
) /* we need search only on the specified IF */
2039 if (allow_deprecated
== 0 && ifp_dep
!= NULL
) {
2041 allow_deprecated
= 1;
2050 * XXX almost dup'ed code with rip6_input.
2053 icmp6_rip6_input(mp
, off
)
2057 struct mbuf
*m
= *mp
;
2058 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2059 struct in6pcb
*in6p
;
2060 struct in6pcb
*last
= NULL
;
2061 struct sockaddr_in6 rip6src
;
2062 struct icmp6_hdr
*icmp6
;
2063 struct mbuf
*opts
= NULL
;
2066 #ifndef PULLDOWN_TEST
2067 /* this is assumed to be safe. */
2068 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
2070 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
2071 if (icmp6
== NULL
) {
2072 /* m is already reclaimed */
2073 return IPPROTO_DONE
;
2078 * XXX: the address may have embedded scope zone ID, which should be
2079 * hidden from applications.
2081 bzero(&rip6src
, sizeof(rip6src
));
2082 rip6src
.sin6_family
= AF_INET6
;
2083 rip6src
.sin6_len
= sizeof(struct sockaddr_in6
);
2084 rip6src
.sin6_addr
= ip6
->ip6_src
;
2085 if (sa6_recoverscope(&rip6src
, TRUE
))
2086 return (IPPROTO_DONE
);
2088 lck_rw_lock_shared(ripcbinfo
.mtx
);
2089 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
2091 if ((in6p
->inp_vflag
& INP_IPV6
) == 0)
2093 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
)
2095 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
2096 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
))
2098 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
2099 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
))
2101 if (in6p
->in6p_icmp6filt
2102 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
2103 in6p
->in6p_icmp6filt
))
2107 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
2108 if ((last
->in6p_flags
& IN6P_CONTROLOPTS
) != 0 ||
2109 (last
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
2110 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
2111 ret
= ip6_savecontrol(last
, n
, &opts
);
2119 /* strip intermediate headers */
2121 so_recv_data_stat(last
->in6p_socket
, m
, 0);
2122 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
2123 (struct sockaddr
*)&rip6src
,
2124 n
, opts
, NULL
) != 0) {
2125 sorwakeup(last
->in6p_socket
);
2133 if ((last
->in6p_flags
& INP_CONTROLOPTS
) != 0 ||
2134 (last
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
2135 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
2136 ret
= ip6_savecontrol(last
, m
, &opts
);
2141 /* strip intermediate headers */
2143 so_recv_data_stat(last
->in6p_socket
, m
, 0);
2144 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
2145 (struct sockaddr
*)&rip6src
, m
, opts
, NULL
) != 0) {
2146 sorwakeup(last
->in6p_socket
);
2151 lck_rw_done(ripcbinfo
.mtx
);
2152 return IPPROTO_DONE
;
2155 lck_rw_done(ripcbinfo
.mtx
);
2158 ip6stat
.ip6s_delivered
--;
2159 return IPPROTO_DONE
;
2163 * Reflect the ip6 packet back to the source.
2164 * OFF points to the icmp6 header, counted from the top of the mbuf.
2167 icmp6_reflect(m
, off
)
2171 struct ip6_hdr
*ip6
;
2172 struct icmp6_hdr
*icmp6
;
2173 struct in6_ifaddr
*ia
;
2174 struct in6_addr t
, src_storage
, *src
= 0;
2177 struct ifnet
*outif
= NULL
;
2178 struct sockaddr_in6 sa6_src
, sa6_dst
;
2179 struct nd_ifinfo
*ndi
;
2181 struct ip6_out_args ip6oa
=
2182 { IFSCOPE_NONE
, { 0 }, IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
};
2184 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.rcvif
!= NULL
) {
2185 ip6oa
.ip6oa_boundif
= m
->m_pkthdr
.rcvif
->if_index
;
2186 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
2189 /* too short to reflect */
2190 if (off
< sizeof(struct ip6_hdr
)) {
2192 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2193 (u_int32_t
)off
, (u_int32_t
)sizeof(struct ip6_hdr
),
2194 __FILE__
, __LINE__
));
2199 * If there are extra headers between IPv6 and ICMPv6, strip
2200 * off that header first.
2203 if (sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) > MHLEN
)
2204 panic("assumption failed in icmp6_reflect");
2206 if (off
> sizeof(struct ip6_hdr
)) {
2208 struct ip6_hdr nip6
;
2210 l
= off
- sizeof(struct ip6_hdr
);
2211 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
2213 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2215 if ((m
= m_pullup(m
, l
)) == NULL
)
2218 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
2219 } else /* off == sizeof(struct ip6_hdr) */ {
2221 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2223 if ((m
= m_pullup(m
, l
)) == NULL
)
2227 plen
= m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2228 ip6
= mtod(m
, struct ip6_hdr
*);
2229 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2230 icmp6
= (struct icmp6_hdr
*)(ip6
+ 1);
2231 type
= icmp6
->icmp6_type
; /* keep type for statistics */
2232 code
= icmp6
->icmp6_code
; /* ditto. */
2236 * ip6_input() drops a packet if its src is multicast.
2237 * So, the src is never multicast.
2239 ip6
->ip6_dst
= ip6
->ip6_src
;
2242 * XXX: make sure to embed scope zone information, using
2243 * already embedded IDs or the received interface (if any).
2244 * Note that rcvif may be NULL.
2246 bzero(&sa6_src
, sizeof(sa6_src
));
2247 sa6_src
.sin6_family
= AF_INET6
;
2248 sa6_src
.sin6_len
= sizeof(sa6_src
);
2249 sa6_src
.sin6_addr
= ip6
->ip6_dst
;
2250 in6_recoverscope(&sa6_src
, &ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
);
2251 in6_embedscope(&ip6
->ip6_dst
, &sa6_src
, NULL
, NULL
, NULL
);
2252 bzero(&sa6_dst
, sizeof(sa6_dst
));
2253 sa6_dst
.sin6_family
= AF_INET6
;
2254 sa6_dst
.sin6_len
= sizeof(sa6_dst
);
2255 sa6_dst
.sin6_addr
= t
;
2256 in6_recoverscope(&sa6_dst
, &t
, m
->m_pkthdr
.rcvif
);
2257 in6_embedscope(&t
, &sa6_dst
, NULL
, NULL
, NULL
);
2260 * If the incoming packet was addressed directly to us(i.e. unicast),
2261 * use dst as the src for the reply.
2262 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2263 * (for example) when we encounter an error while forwarding procedure
2264 * destined to a duplicated address of ours.
2265 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2266 * procedure of an outgoing packet of our own, in which case we need
2267 * to search in the ifaddr list.
2269 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2270 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2271 IFA_LOCK(&ia
->ia_ifa
);
2272 if (IN6_ARE_ADDR_EQUAL(&t
, &ia
->ia_addr
.sin6_addr
) &&
2273 (ia
->ia6_flags
& (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
)) == 0) {
2274 IFA_UNLOCK(&ia
->ia_ifa
);
2278 IFA_UNLOCK(&ia
->ia_ifa
);
2280 lck_rw_done(&in6_ifaddr_rwlock
);
2281 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) && (m
->m_flags
& M_LOOP
)) {
2283 * This is the case if the dst is our link-local address
2284 * and the sender is also ourselves.
2291 struct sockaddr_in6 sin6
;
2292 struct route_in6 ro
;
2295 * This case matches to multicasts, our anycast, or unicasts
2296 * that we do not own. Select a source address based on the
2297 * source address of the erroneous packet.
2299 bzero(&sin6
, sizeof(sin6
));
2300 sin6
.sin6_family
= AF_INET6
;
2301 sin6
.sin6_len
= sizeof(sin6
);
2302 sin6
.sin6_addr
= ip6
->ip6_dst
; /* zone ID should be embedded */
2304 bzero(&ro
, sizeof(ro
));
2306 * in6_selectsrc() might return outif with its reference held
2307 * even in the error case, so we always need to release it
2310 src
= in6_selectsrc(&sin6
, NULL
, NULL
, &ro
, &outif
,
2311 &src_storage
, ip6oa
.ip6oa_boundif
, &e
);
2313 rtfree(ro
.ro_rt
); /* XXX: we could use this */
2316 "icmp6_reflect: source can't be determined: "
2317 "dst=%s, error=%d\n",
2318 ip6_sprintf(&sa6_src
.sin6_addr
), e
));
2322 oflow
= ip6
->ip6_flow
; /* Save for later */
2323 ip6
->ip6_src
= *src
;
2325 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2326 ip6
->ip6_vfc
|= IPV6_VERSION
;
2327 if (icmp6
->icmp6_type
== ICMP6_ECHO_REPLY
&& icmp6
->icmp6_code
== 0) {
2328 ip6
->ip6_flow
|= (oflow
& htonl(0x0ff00000));
2330 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2331 lck_rw_lock_shared(nd_if_rwlock
);
2332 if (outif
!= NULL
&& (ndi
= ND_IFINFO(outif
)) != NULL
&&
2334 lck_mtx_lock(&ndi
->lock
);
2335 ip6
->ip6_hlim
= ndi
->chlim
;
2336 lck_mtx_unlock(&ndi
->lock
);
2338 if (m
->m_pkthdr
.rcvif
!= NULL
&&
2339 (ndi
= ND_IFINFO(m
->m_pkthdr
.rcvif
)) != NULL
&&
2341 /* XXX: This may not be the outgoing interface */
2342 lck_mtx_lock(&ndi
->lock
);
2343 ip6
->ip6_hlim
= ndi
->chlim
;
2344 lck_mtx_unlock(&ndi
->lock
);
2346 ip6
->ip6_hlim
= ip6_defhlim
;
2348 lck_rw_done(nd_if_rwlock
);
2349 /* Use the same traffic class as in the request to match IPv4 */
2350 icmp6
->icmp6_cksum
= 0;
2351 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
2352 sizeof(struct ip6_hdr
), plen
);
2355 * XXX option handling
2358 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
2360 /* Don't lookup socket */
2361 if (ipsec_bypass
== 0)
2362 (void)ipsec_setsocket(m
, NULL
);
2365 if (outif
!= NULL
) {
2366 ifnet_release(outif
);
2369 m
->m_pkthdr
.rcvif
= NULL
;
2370 m
->m_pkthdr
.csum_data
= 0;
2371 m
->m_pkthdr
.csum_flags
= 0;
2372 ip6_output(m
, NULL
, NULL
, IPV6_OUTARGS
, NULL
, &outif
, &ip6oa
);
2373 if (outif
!= NULL
) {
2374 icmp6_ifoutstat_inc(outif
, type
, code
);
2375 ifnet_release(outif
);
2382 ifnet_release(outif
);
2387 icmp6_redirect_diag(src6
, dst6
, tgt6
)
2388 struct in6_addr
*src6
;
2389 struct in6_addr
*dst6
;
2390 struct in6_addr
*tgt6
;
2392 static char buf
[1024];
2393 snprintf(buf
, sizeof(buf
), "(src=%s dst=%s tgt=%s)",
2394 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
2399 icmp6_redirect_input(m
, off
)
2403 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2404 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2405 struct nd_redirect
*nd_rd
;
2406 int icmp6len
= ntohs(ip6
->ip6_plen
);
2407 char *lladdr
= NULL
;
2409 u_char
*redirhdr
= NULL
;
2410 int redirhdrlen
= 0;
2411 struct rtentry
*rt
= NULL
;
2414 struct in6_addr src6
= ip6
->ip6_src
;
2415 struct in6_addr redtgt6
;
2416 struct in6_addr reddst6
;
2417 union nd_opts ndopts
;
2423 * If we are an advertising router on this interface,
2424 * don't update route by icmp6 redirect.
2426 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
)
2428 if (!icmp6_rediraccept
)
2431 #ifndef PULLDOWN_TEST
2432 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
2433 nd_rd
= (struct nd_redirect
*)((caddr_t
)ip6
+ off
);
2435 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
2436 if (nd_rd
== NULL
) {
2437 icmp6stat
.icp6s_tooshort
++;
2441 redtgt6
= nd_rd
->nd_rd_target
;
2442 reddst6
= nd_rd
->nd_rd_dst
;
2444 if (in6_setscope(&redtgt6
, m
->m_pkthdr
.rcvif
, NULL
) ||
2445 in6_setscope(&reddst6
, m
->m_pkthdr
.rcvif
, NULL
)) {
2450 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
2452 "ICMP6 redirect sent from %s rejected; "
2453 "must be from linklocal\n", ip6_sprintf(&src6
)));
2456 if (ip6
->ip6_hlim
!= 255) {
2458 "ICMP6 redirect sent from %s rejected; "
2459 "hlim=%d (must be 255)\n",
2460 ip6_sprintf(&src6
), ip6
->ip6_hlim
));
2464 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2465 struct sockaddr_in6 sin6
;
2466 struct in6_addr
*gw6
;
2468 bzero(&sin6
, sizeof(sin6
));
2469 sin6
.sin6_family
= AF_INET6
;
2470 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2471 bcopy(&reddst6
, &sin6
.sin6_addr
, sizeof(reddst6
));
2472 rt
= rtalloc1_scoped((struct sockaddr
*)&sin6
, 0, 0, ifp
->if_index
);
2475 if (rt
->rt_gateway
== NULL
||
2476 rt
->rt_gateway
->sa_family
!= AF_INET6
) {
2478 "ICMP6 redirect rejected; no route "
2479 "with inet6 gateway found for redirect dst: %s\n",
2480 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2486 gw6
= &(((struct sockaddr_in6
*)(void *)
2487 rt
->rt_gateway
)->sin6_addr
);
2488 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
2490 "ICMP6 redirect rejected; "
2491 "not equal to gw-for-src=%s (must be same): "
2494 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2501 "ICMP6 redirect rejected; "
2502 "no route found for redirect dst: %s\n",
2503 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2510 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
2512 "ICMP6 redirect rejected; "
2513 "redirect dst must be unicast: %s\n",
2514 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2518 is_router
= is_onlink
= 0;
2519 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2520 is_router
= 1; /* router case */
2521 if (bcmp(&redtgt6
, &reddst6
, sizeof(redtgt6
)) == 0)
2522 is_onlink
= 1; /* on-link destination case */
2523 if (!is_router
&& !is_onlink
) {
2525 "ICMP6 redirect rejected; "
2526 "neither router case nor onlink case: %s\n",
2527 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2530 /* validation passed */
2532 icmp6len
-= sizeof(*nd_rd
);
2533 nd6_option_init(nd_rd
+ 1, icmp6len
, &ndopts
);
2534 if (nd6_options(&ndopts
) < 0) {
2535 nd6log((LOG_INFO
, "icmp6_redirect_input: "
2536 "invalid ND option, rejected: %s\n",
2537 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2538 /* nd6_options have incremented stats */
2542 if (ndopts
.nd_opts_tgt_lladdr
) {
2543 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
2544 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
2547 if (ndopts
.nd_opts_rh
) {
2548 redirhdrlen
= ndopts
.nd_opts_rh
->nd_opt_rh_len
;
2549 redirhdr
= (u_char
*)(ndopts
.nd_opts_rh
+ 1); /* xxx */
2552 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
2554 "icmp6_redirect_input: lladdrlen mismatch for %s "
2555 "(if %d, icmp6 packet %d): %s\n",
2556 ip6_sprintf(&redtgt6
), ifp
->if_addrlen
, lladdrlen
- 2,
2557 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2562 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
2563 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
2565 if (!is_onlink
) { /* better router case. perform rtredirect. */
2566 /* perform rtredirect */
2567 struct sockaddr_in6 sdst
;
2568 struct sockaddr_in6 sgw
;
2569 struct sockaddr_in6 ssrc
;
2571 bzero(&sdst
, sizeof(sdst
));
2572 bzero(&sgw
, sizeof(sgw
));
2573 bzero(&ssrc
, sizeof(ssrc
));
2574 sdst
.sin6_family
= sgw
.sin6_family
= ssrc
.sin6_family
= AF_INET6
;
2575 sdst
.sin6_len
= sgw
.sin6_len
= ssrc
.sin6_len
=
2576 sizeof(struct sockaddr_in6
);
2577 bcopy(&redtgt6
, &sgw
.sin6_addr
, sizeof(struct in6_addr
));
2578 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2579 bcopy(&src6
, &ssrc
.sin6_addr
, sizeof(struct in6_addr
));
2580 rtredirect(ifp
, (struct sockaddr
*)&sdst
,
2581 (struct sockaddr
*)&sgw
, NULL
, RTF_GATEWAY
| RTF_HOST
,
2582 (struct sockaddr
*)&ssrc
, NULL
);
2584 /* finally update cached route in each socket via pfctlinput */
2586 struct sockaddr_in6 sdst
;
2588 bzero(&sdst
, sizeof(sdst
));
2589 sdst
.sin6_family
= AF_INET6
;
2590 sdst
.sin6_len
= sizeof(struct sockaddr_in6
);
2591 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2595 * Release the IPv6 domain lock because we are going to take domain_proto_mtx
2596 * and could otherwise cause a deadlock with other threads taking these locks
2597 * in the reverse order -- e.g. frag6_slowtimo() from pfslowtimo()
2599 lck_mtx_unlock(inet6_domain_mutex
);
2600 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
2601 lck_mtx_lock(inet6_domain_mutex
);
2603 key_sa_routechange((struct sockaddr
*)&sdst
);
2612 icmp6stat
.icp6s_badredirect
++;
2617 icmp6_redirect_output(m0
, rt
)
2621 struct ifnet
*ifp
; /* my outgoing interface */
2622 struct in6_addr ifp_ll6
;
2623 struct in6_addr
*router_ll6
;
2624 struct ip6_hdr
*sip6
; /* m0 as struct ip6_hdr */
2625 struct mbuf
*m
= NULL
; /* newly allocated one */
2626 struct ip6_hdr
*ip6
; /* m as struct ip6_hdr */
2627 struct nd_redirect
*nd_rd
;
2630 struct ifnet
*outif
= NULL
;
2631 struct sockaddr_in6 src_sa
;
2632 struct ip6_out_args ip6oa
=
2633 { IFSCOPE_NONE
, { 0 }, IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
};
2635 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, ND_REDIRECT
, 0);
2641 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
))
2645 * If we are not a router to begin with, or not an advertising
2646 * router on this interface, don't send icmp6 redirect.
2648 if (!ip6_forwarding
|| !(ifp
->if_eflags
& IFEF_IPV6_ROUTER
))
2653 * the source address must identify a neighbor, and
2654 * the destination address must not be a multicast address
2655 * [RFC 2461, sec 8.2]
2657 sip6
= mtod(m0
, struct ip6_hdr
*);
2658 bzero(&src_sa
, sizeof(src_sa
));
2659 src_sa
.sin6_family
= AF_INET6
;
2660 src_sa
.sin6_len
= sizeof(src_sa
);
2661 src_sa
.sin6_addr
= sip6
->ip6_src
;
2662 /* we don't currently use sin6_scope_id, but eventually use it */
2663 src_sa
.sin6_scope_id
= in6_addr2scopeid(ifp
, &sip6
->ip6_src
);
2665 if (nd6_is_addr_neighbor(&src_sa
, ifp
, 0) == 0) {
2666 /* already unlocked */
2671 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
))
2672 goto fail
; /* what should we do here? */
2675 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0))
2679 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2680 * we almost always ask for an mbuf cluster for simplicity.
2681 * (MHLEN < IPV6_MMTU is almost always true)
2683 #if IPV6_MMTU >= MCLBYTES
2684 # error assumption failed about IPV6_MMTU and MCLBYTES
2686 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
2687 if (m
&& IPV6_MMTU
>= MHLEN
)
2688 MCLGET(m
, M_DONTWAIT
);
2691 m
->m_pkthdr
.rcvif
= NULL
;
2693 maxlen
= M_TRAILINGSPACE(m
);
2694 maxlen
= min(IPV6_MMTU
, maxlen
);
2695 /* just for safety */
2696 if (maxlen
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) +
2697 ((sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7)) {
2702 /* get ip6 linklocal address for ifp(my outgoing interface). */
2703 struct in6_ifaddr
*ia
;
2704 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
2706 IN6_IFF_ANYCAST
)) == NULL
)
2708 IFA_LOCK(&ia
->ia_ifa
);
2709 ifp_ll6
= ia
->ia_addr
.sin6_addr
;
2710 IFA_UNLOCK(&ia
->ia_ifa
);
2711 IFA_REMREF(&ia
->ia_ifa
);
2714 /* get ip6 linklocal address for the router. */
2715 if (rt
->rt_gateway
&& (rt
->rt_flags
& RTF_GATEWAY
)) {
2716 struct sockaddr_in6
*sin6
;
2717 sin6
= (struct sockaddr_in6
*)(void *)rt
->rt_gateway
;
2718 router_ll6
= &sin6
->sin6_addr
;
2719 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6
))
2720 router_ll6
= (struct in6_addr
*)NULL
;
2722 router_ll6
= (struct in6_addr
*)NULL
;
2725 ip6
= mtod(m
, struct ip6_hdr
*);
2727 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2728 ip6
->ip6_vfc
|= IPV6_VERSION
;
2729 /* ip6->ip6_plen will be set later */
2730 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2731 ip6
->ip6_hlim
= 255;
2732 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2733 bcopy(&ifp_ll6
, &ip6
->ip6_src
, sizeof(struct in6_addr
));
2734 bcopy(&sip6
->ip6_src
, &ip6
->ip6_dst
, sizeof(struct in6_addr
));
2737 nd_rd
= (struct nd_redirect
*)(ip6
+ 1);
2738 nd_rd
->nd_rd_type
= ND_REDIRECT
;
2739 nd_rd
->nd_rd_code
= 0;
2740 nd_rd
->nd_rd_reserved
= 0;
2741 if (rt
->rt_flags
& RTF_GATEWAY
) {
2743 * nd_rd->nd_rd_target must be a link-local address in
2744 * better router cases.
2748 bcopy(router_ll6
, &nd_rd
->nd_rd_target
,
2749 sizeof(nd_rd
->nd_rd_target
));
2750 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2751 sizeof(nd_rd
->nd_rd_dst
));
2753 /* make sure redtgt == reddst */
2754 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_target
,
2755 sizeof(nd_rd
->nd_rd_target
));
2756 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2757 sizeof(nd_rd
->nd_rd_dst
));
2762 p
= (u_char
*)(nd_rd
+ 1);
2768 /* target lladdr option */
2769 struct rtentry
*rt_router
= NULL
;
2771 struct sockaddr_dl
*sdl
;
2772 struct nd_opt_hdr
*nd_opt
;
2775 /* Callee returns a locked route upon success */
2776 rt_router
= nd6_lookup(router_ll6
, 0, ifp
, 0);
2779 RT_LOCK_ASSERT_HELD(rt_router
);
2780 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2781 len
= (len
+ 7) & ~7; /* round by 8 */
2783 if (len
+ (p
- (u_char
*)ip6
) > maxlen
) {
2784 RT_REMREF_LOCKED(rt_router
);
2785 RT_UNLOCK(rt_router
);
2789 if (!(rt_router
->rt_flags
& RTF_GATEWAY
) &&
2790 (rt_router
->rt_flags
& RTF_LLINFO
) &&
2791 (rt_router
->rt_gateway
->sa_family
== AF_LINK
) &&
2792 (sdl
= (struct sockaddr_dl
*)(void *)
2793 rt_router
->rt_gateway
) && sdl
->sdl_alen
) {
2794 nd_opt
= (struct nd_opt_hdr
*)p
;
2795 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
2796 nd_opt
->nd_opt_len
= len
>> 3;
2797 lladdr
= (char *)(nd_opt
+ 1);
2798 bcopy(LLADDR(sdl
), lladdr
, ifp
->if_addrlen
);
2801 RT_REMREF_LOCKED(rt_router
);
2802 RT_UNLOCK(rt_router
);
2807 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2809 /* just to be safe */
2810 #ifdef M_DECRYPTED /*not openbsd*/
2811 if (m0
->m_flags
& M_DECRYPTED
)
2814 if (p
- (u_char
*)ip6
> maxlen
)
2818 /* redirected header option */
2820 struct nd_opt_rd_hdr
*nd_opt_rh
;
2823 * compute the maximum size for icmp6 redirect header option.
2824 * XXX room for auth header?
2826 len
= maxlen
- (p
- (u_char
*)ip6
);
2829 /* This is just for simplicity. */
2830 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2832 m_freem(m0
->m_next
);
2835 m0
->m_pkthdr
.len
= m0
->m_len
;
2839 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2840 * about padding/truncate rule for the original IP packet.
2841 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2842 * - "attach as much as possible" is the goal
2843 * - pad if not aligned (original size can be guessed by original
2845 * Following code adds the padding if it is simple enough,
2846 * and truncates if not.
2848 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
)
2849 panic("assumption failed in %s:%d\n", __FILE__
, __LINE__
);
2851 if (len
- sizeof(*nd_opt_rh
) < m0
->m_pkthdr
.len
) {
2852 /* not enough room, truncate */
2853 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2855 /* enough room, pad or truncate */
2858 extra
= m0
->m_pkthdr
.len
% 8;
2860 /* pad if easy enough, truncate if not */
2861 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2863 m0
->m_len
+= (8 - extra
);
2864 m0
->m_pkthdr
.len
+= (8 - extra
);
2867 m0
->m_pkthdr
.len
-= extra
;
2871 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2872 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2875 nd_opt_rh
= (struct nd_opt_rd_hdr
*)p
;
2876 bzero(nd_opt_rh
, sizeof(*nd_opt_rh
));
2877 nd_opt_rh
->nd_opt_rh_type
= ND_OPT_REDIRECTED_HEADER
;
2878 nd_opt_rh
->nd_opt_rh_len
= len
>> 3;
2879 p
+= sizeof(*nd_opt_rh
);
2880 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2882 /* connect m0 to m */
2884 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
2888 /* XXX: clear embedded link IDs in the inner header */
2889 in6_clearscope(&sip6
->ip6_src
);
2890 in6_clearscope(&sip6
->ip6_dst
);
2891 in6_clearscope(&nd_rd
->nd_rd_target
);
2892 in6_clearscope(&nd_rd
->nd_rd_dst
);
2894 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
2896 nd_rd
->nd_rd_cksum
= 0;
2898 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
2900 /* send the packet to outside... */
2902 /* Don't lookup socket */
2903 if (ipsec_bypass
== 0)
2904 (void)ipsec_setsocket(m
, NULL
);
2907 ip6oa
.ip6oa_boundif
= ifp
->if_index
;
2908 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
2910 ip6_output(m
, NULL
, NULL
, IPV6_OUTARGS
, NULL
, &outif
, &ip6oa
);
2912 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
2913 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
2914 ifnet_release(outif
);
2916 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
2930 * ICMPv6 socket option processing.
2933 icmp6_ctloutput(so
, sopt
)
2935 struct sockopt
*sopt
;
2939 struct inpcb
*inp
= sotoinpcb(so
);
2940 int level
, op
, optname
;
2943 level
= sopt
->sopt_level
;
2944 op
= sopt
->sopt_dir
;
2945 optname
= sopt
->sopt_name
;
2946 optlen
= sopt
->sopt_valsize
;
2948 level
= op
= optname
= optlen
= 0;
2950 if (level
!= IPPROTO_ICMPV6
) {
2959 struct icmp6_filter
*p
;
2961 if (optlen
!= 0 && optlen
!= sizeof(*p
)) {
2965 if (inp
->in6p_icmp6filt
== NULL
) {
2971 /* According to RFC 3542, an installed filter can be
2972 * cleared by issuing a setsockopt for ICMP6_FILTER
2973 * with a zero length.
2975 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
2977 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
2984 error
= ENOPROTOOPT
;
2993 if (inp
->in6p_icmp6filt
== NULL
) {
2997 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
2998 min(sizeof(struct icmp6_filter
), optlen
));
3003 error
= ENOPROTOOPT
;
3013 * ICMPv6 socket datagram option processing.
3016 icmp6_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
3018 if (kauth_cred_issuser(so
->so_cred
))
3019 return icmp6_ctloutput(so
, sopt
);
3021 if (sopt
->sopt_level
== IPPROTO_ICMPV6
) {
3022 switch (sopt
->sopt_name
) {
3024 return icmp6_ctloutput(so
, sopt
);
3030 if (sopt
->sopt_level
!= IPPROTO_IPV6
)
3033 switch (sopt
->sopt_name
) {
3034 case IPV6_UNICAST_HOPS
:
3037 case IPV6_USE_MIN_MTU
:
3038 case IPV6_RECVRTHDR
:
3039 case IPV6_RECVPKTINFO
:
3040 case IPV6_RECVHOPLIMIT
:
3046 case IPV6_MULTICAST_IF
:
3047 case IPV6_MULTICAST_HOPS
:
3048 case IPV6_MULTICAST_LOOP
:
3049 case IPV6_JOIN_GROUP
:
3050 case IPV6_LEAVE_GROUP
:
3051 case IPV6_PORTRANGE
:
3052 case IPV6_IPSEC_POLICY
:
3053 case IPV6_RECVTCLASS
:
3055 case IPV6_2292PKTOPTIONS
:
3056 case IPV6_2292PKTINFO
:
3057 case IPV6_2292HOPLIMIT
:
3058 case IPV6_2292HOPOPTS
:
3059 case IPV6_2292DSTOPTS
:
3060 case IPV6_2292RTHDR
:
3062 case IPV6_NO_IFT_CELLULAR
:
3064 return ip6_ctloutput(so
, sopt
);
3071 __private_extern__
int
3072 icmp6_dgram_send(struct socket
*so
, int flags
, struct mbuf
*m
,
3073 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
3075 #pragma unused(flags, p)
3077 struct inpcb
*inp
= sotoinpcb(so
);
3078 struct sockaddr_in6 tmp
;
3079 struct sockaddr_in6
*dst
= (struct sockaddr_in6
*)(void *)nam
;
3080 struct icmp6_hdr
*icmp6
;
3082 if (kauth_cred_issuser(so
->so_cred
))
3083 return rip6_output(m
, so
, (struct sockaddr_in6
*)(void *)nam
,
3086 /* always copy sockaddr to avoid overwrites */
3087 if (so
->so_state
& SS_ISCONNECTED
) {
3093 bzero(&tmp
, sizeof(tmp
));
3094 tmp
.sin6_family
= AF_INET6
;
3095 tmp
.sin6_len
= sizeof(struct sockaddr_in6
);
3096 bcopy(&inp
->in6p_faddr
, &tmp
.sin6_addr
,
3097 sizeof(struct in6_addr
));
3104 tmp
= *(struct sockaddr_in6
*)(void *)nam
;
3109 * For an ICMPv6 packet, we should know its type and code
3111 if (so
->so_proto
->pr_protocol
== IPPROTO_ICMPV6
) {
3112 if (m
->m_len
< sizeof(struct icmp6_hdr
) &&
3113 (m
= m_pullup(m
, sizeof(struct icmp6_hdr
))) == NULL
) {
3117 icmp6
= mtod(m
, struct icmp6_hdr
*);
3120 * Allow only to send echo request and node information request
3121 * See RFC 2463 for Echo Request Message format
3123 if ((icmp6
->icmp6_type
== ICMP6_ECHO_REQUEST
&& icmp6
->icmp6_code
== 0) ||
3124 (icmp6
->icmp6_type
== ICMP6_NI_QUERY
&&
3125 (icmp6
->icmp6_code
== ICMP6_NI_SUBJ_IPV6
||
3126 icmp6
->icmp6_code
== ICMP6_NI_SUBJ_FQDN
))) {
3135 #if ENABLE_DEFAULT_SCOPE
3136 if (dst
->sin6_scope_id
== 0) { /* not change if specified */
3137 dst
->sin6_scope_id
= scope6_addr2default(&dst
->sin6_addr
);
3141 return rip6_output(m
, so
, (struct sockaddr_in6
*)(void *)nam
,
3148 /* Like rip6_attach but without root privilege enforcement */
3149 __private_extern__
int
3150 icmp6_dgram_attach(struct socket
*so
, int proto
, struct proc
*p
)
3155 inp
= sotoinpcb(so
);
3157 panic("icmp6_dgram_attach");
3159 if (proto
!= IPPROTO_ICMPV6
)
3162 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
3165 error
= in_pcballoc(so
, &ripcbinfo
, p
);
3168 inp
= (struct inpcb
*)so
->so_pcb
;
3169 inp
->inp_vflag
|= INP_IPV6
;
3170 inp
->in6p_ip6_nxt
= IPPROTO_ICMPV6
;
3171 inp
->in6p_hops
= -1; /* use kernel default */
3172 inp
->in6p_cksum
= -1;
3173 MALLOC(inp
->in6p_icmp6filt
, struct icmp6_filter
*,
3174 sizeof(struct icmp6_filter
), M_PCB
, M_WAITOK
);
3175 if (inp
->in6p_icmp6filt
== NULL
)
3177 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
3183 #ifndef HAVE_PPSRATECHECK
3185 #define timersub(tvp, uvp, vvp) \
3187 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
3188 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
3189 if ((vvp)->tv_usec < 0) { \
3191 (vvp)->tv_usec += 1000000; \
3197 * ppsratecheck(): packets (or events) per second limitation.
3200 ppsratecheck(lasttime
, curpps
, maxpps
)
3201 struct timeval
*lasttime
;
3203 int maxpps
; /* maximum pps allowed */
3205 struct timeval tv
, delta
;
3210 timersub(&tv
, lasttime
, &delta
);
3213 * Check for 0,0 so that the message will be seen at least once.
3214 * If more than one second has passed since the last update of
3215 * lasttime, reset the counter.
3217 * we do increment *curpps even in *curpps < maxpps case, as some may
3218 * try to use *curpps for stat purposes as well.
3220 if ((lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0) ||
3221 delta
.tv_sec
>= 1) {
3225 } else if (maxpps
< 0)
3227 else if (*curpps
< maxpps
)
3232 #if 1 /* DIAGNOSTIC? */
3233 /* be careful about wrap-around */
3234 if (*curpps
+ 1 > 0)
3235 *curpps
= *curpps
+ 1;
3238 * assume that there's not too many calls to this function.
3239 * not sure if the assumption holds, as it depends on *caller's*
3240 * behavior, not the behavior of this function.
3241 * IMHO it is wrong to make assumption on the caller's behavior,
3242 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
3244 *curpps
= *curpps
+ 1;
3252 * Perform rate limit check.
3253 * Returns 0 if it is okay to send the icmp6 packet.
3254 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
3257 * XXX per-destination/type check necessary?
3261 __unused
const struct in6_addr
*dst
, /* not used at this moment */
3262 __unused
const int type
, /* not used at this moment */
3263 __unused
const int code
) /* not used at this moment */
3267 ret
= 0; /* okay to send */
3270 if (!ppsratecheck(&icmp6errppslim_last
, &icmp6errpps_count
,
3272 /* The packet is subject to rate limit */