1 /* $FreeBSD: src/sys/netinet6/icmp6.c,v 1.6.2.6 2001/07/10 09:44:16 ume Exp $ */
2 /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/malloc.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
78 #include <sys/kernel.h>
79 #include <sys/syslog.h>
80 #include <sys/domain.h>
83 #include <net/route.h>
84 #include <net/if_dl.h>
85 #include <net/if_types.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/mld6_var.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/nd6.h>
96 #include <netinet6/in6_ifattach.h>
97 #include <netinet6/ip6protosw.h>
100 #include <netinet6/ipsec.h>
101 #include <netkey/key.h>
103 extern int ipsec_bypass
;
106 #include <net/net_osdep.h>
108 extern struct domain inet6domain
;
109 extern struct ip6protosw inet6sw
[];
110 extern struct ip6protosw
*ip6_protox
[];
112 extern u_long rip_sendspace
;
113 extern u_long rip_recvspace
;
115 struct icmp6stat icmp6stat
;
117 extern struct inpcbhead ripcb
;
118 extern int icmp6errppslim
;
119 static int icmp6errpps_count
= 0;
120 static struct timeval icmp6errppslim_last
;
121 extern int icmp6_nodeinfo
;
122 extern struct inpcbinfo ripcbinfo
;
123 extern lck_mtx_t
*ip6_mutex
;
124 extern lck_mtx_t
*nd6_mutex
;
125 extern lck_mtx_t
*inet6_domain_mutex
;
127 static void icmp6_errcount(struct icmp6errstat
*, int, int);
128 static int icmp6_rip6_input(struct mbuf
**, int);
129 static int icmp6_ratelimit(const struct in6_addr
*, const int, const int);
130 static const char *icmp6_redirect_diag(struct in6_addr
*,
131 struct in6_addr
*, struct in6_addr
*);
132 #ifndef HAVE_PPSRATECHECK
133 static int ppsratecheck(struct timeval
*, int *, int);
135 static struct mbuf
*ni6_input(struct mbuf
*, int);
136 static struct mbuf
*ni6_nametodns(const char *, int, int);
137 static int ni6_dnsmatch(const char *, int, const char *, int);
138 static int ni6_addrs(struct icmp6_nodeinfo
*,
139 struct ifnet
**, char *);
140 static int ni6_store_addrs(struct icmp6_nodeinfo
*, struct icmp6_nodeinfo
*,
141 struct ifnet
*, int);
142 static int icmp6_notify_error(struct mbuf
*, int, int, int);
144 #ifdef COMPAT_RFC1885
145 static struct route_in6 icmp6_reflect_rt
;
156 icmp6_errcount(stat
, type
, code
)
157 struct icmp6errstat
*stat
;
161 case ICMP6_DST_UNREACH
:
163 case ICMP6_DST_UNREACH_NOROUTE
:
164 stat
->icp6errs_dst_unreach_noroute
++;
166 case ICMP6_DST_UNREACH_ADMIN
:
167 stat
->icp6errs_dst_unreach_admin
++;
169 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
170 stat
->icp6errs_dst_unreach_beyondscope
++;
172 case ICMP6_DST_UNREACH_ADDR
:
173 stat
->icp6errs_dst_unreach_addr
++;
175 case ICMP6_DST_UNREACH_NOPORT
:
176 stat
->icp6errs_dst_unreach_noport
++;
180 case ICMP6_PACKET_TOO_BIG
:
181 stat
->icp6errs_packet_too_big
++;
183 case ICMP6_TIME_EXCEEDED
:
185 case ICMP6_TIME_EXCEED_TRANSIT
:
186 stat
->icp6errs_time_exceed_transit
++;
188 case ICMP6_TIME_EXCEED_REASSEMBLY
:
189 stat
->icp6errs_time_exceed_reassembly
++;
193 case ICMP6_PARAM_PROB
:
195 case ICMP6_PARAMPROB_HEADER
:
196 stat
->icp6errs_paramprob_header
++;
198 case ICMP6_PARAMPROB_NEXTHEADER
:
199 stat
->icp6errs_paramprob_nextheader
++;
201 case ICMP6_PARAMPROB_OPTION
:
202 stat
->icp6errs_paramprob_option
++;
207 stat
->icp6errs_redirect
++;
210 stat
->icp6errs_unknown
++;
214 * Generate an error packet of type error in response to bad IP6 packet.
217 icmp6_error(m
, type
, code
, param
)
219 int type
, code
, param
;
221 struct ip6_hdr
*oip6
, *nip6
;
222 struct icmp6_hdr
*icmp6
;
227 icmp6stat
.icp6s_error
++;
229 lck_mtx_assert(ip6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
230 /* count per-type-code statistics */
231 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, type
, code
);
233 #ifdef M_DECRYPTED /*not openbsd*/
234 if (m
->m_flags
& M_DECRYPTED
) {
235 icmp6stat
.icp6s_canterror
++;
240 #ifndef PULLDOWN_TEST
241 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return);
243 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
244 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
249 oip6
= mtod(m
, struct ip6_hdr
*);
252 * Multicast destination check. For unrecognized option errors,
253 * this check has already done in ip6_unknown_opt(), so we can
254 * check only for other errors.
256 if ((m
->m_flags
& (M_BCAST
|M_MCAST
) ||
257 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_dst
)) &&
258 (type
!= ICMP6_PACKET_TOO_BIG
&&
259 (type
!= ICMP6_PARAM_PROB
||
260 code
!= ICMP6_PARAMPROB_OPTION
)))
263 /* Source address check. XXX: the case of anycast source? */
264 if (IN6_IS_ADDR_UNSPECIFIED(&oip6
->ip6_src
) ||
265 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_src
))
269 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
273 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
274 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
275 struct icmp6_hdr
*icp
;
277 #ifndef PULLDOWN_TEST
278 IP6_EXTHDR_CHECK(m
, 0, off
+ sizeof(struct icmp6_hdr
), return);
279 icp
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
281 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
284 icmp6stat
.icp6s_tooshort
++;
288 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
289 icp
->icmp6_type
== ND_REDIRECT
) {
292 * Special case: for redirect (which is
293 * informational) we must not send icmp6 error.
295 icmp6stat
.icp6s_canterror
++;
298 /* ICMPv6 informational - send the error */
301 /* non-ICMPv6 - send the error */
304 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
306 /* Finally, do rate limitation check. */
307 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
308 icmp6stat
.icp6s_toofreq
++;
313 * OK, ICMP6 can be generated.
316 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
)
317 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
319 preplen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
320 M_PREPEND(m
, preplen
, M_DONTWAIT
);
321 if (m
&& m
->m_len
< preplen
)
322 m
= m_pullup(m
, preplen
);
324 nd6log((LOG_DEBUG
, "ENOBUFS in icmp6_error %d\n", __LINE__
));
328 nip6
= mtod(m
, struct ip6_hdr
*);
329 nip6
->ip6_src
= oip6
->ip6_src
;
330 nip6
->ip6_dst
= oip6
->ip6_dst
;
332 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
333 oip6
->ip6_src
.s6_addr16
[1] = 0;
334 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
335 oip6
->ip6_dst
.s6_addr16
[1] = 0;
337 icmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
338 icmp6
->icmp6_type
= type
;
339 icmp6
->icmp6_code
= code
;
340 icmp6
->icmp6_pptr
= htonl((u_int32_t
)param
);
343 * icmp6_reflect() is designed to be in the input path.
344 * icmp6_error() can be called from both input and outut path,
345 * and if we are in output path rcvif could contain bogus value.
346 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
347 * information in ip header (nip6).
349 m
->m_pkthdr
.rcvif
= NULL
;
351 icmp6stat
.icp6s_outhist
[type
]++;
352 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /* header order: IPv6 - ICMPv6 */
358 * If we can't tell wheter or not we can generate ICMP6, free it.
364 * Process a received ICMP6 message.
367 icmp6_input(mp
, offp
)
371 struct mbuf
*m
= *mp
, *n
;
372 struct ip6_hdr
*ip6
, *nip6
;
373 struct icmp6_hdr
*icmp6
, *nicmp6
;
375 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
378 #ifndef PULLDOWN_TEST
379 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_hdr
), return IPPROTO_DONE
);
380 /* m might change if M_LOOP. So, call mtod after this */
384 * Locate icmp6 structure in mbuf, and check
385 * that not corrupted and of at least minimum length
388 ip6
= mtod(m
, struct ip6_hdr
*);
389 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
390 icmp6stat
.icp6s_tooshort
++;
395 * calculate the checksum
397 #ifndef PULLDOWN_TEST
398 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
400 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
402 icmp6stat
.icp6s_tooshort
++;
406 code
= icmp6
->icmp6_code
;
408 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
410 "ICMP6 checksum error(%d|%x) %s\n",
411 icmp6
->icmp6_type
, sum
, ip6_sprintf(&ip6
->ip6_src
)));
412 icmp6stat
.icp6s_checksum
++;
416 #if defined(NFAITH) && 0 < NFAITH
417 if (faithprefix(&ip6
->ip6_dst
)) {
419 * Deliver very specific ICMP6 type only.
420 * This is important to deilver TOOBIG. Otherwise PMTUD
423 switch (icmp6
->icmp6_type
) {
424 case ICMP6_DST_UNREACH
:
425 case ICMP6_PACKET_TOO_BIG
:
426 case ICMP6_TIME_EXCEEDED
:
434 icmp6stat
.icp6s_inhist
[icmp6
->icmp6_type
]++;
435 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_msg
);
436 if (icmp6
->icmp6_type
< ICMP6_INFOMSG_MASK
)
437 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_error
);
439 switch (icmp6
->icmp6_type
) {
440 case ICMP6_DST_UNREACH
:
441 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
443 case ICMP6_DST_UNREACH_NOROUTE
:
444 code
= PRC_UNREACH_NET
;
446 case ICMP6_DST_UNREACH_ADMIN
:
447 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_adminprohib
);
448 code
= PRC_UNREACH_PROTOCOL
; /* is this a good code? */
450 case ICMP6_DST_UNREACH_ADDR
:
453 #ifdef COMPAT_RFC1885
454 case ICMP6_DST_UNREACH_NOTNEIGHBOR
:
455 code
= PRC_UNREACH_SRCFAIL
;
458 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
459 /* I mean "source address was incorrect." */
460 code
= PRC_PARAMPROB
;
463 case ICMP6_DST_UNREACH_NOPORT
:
464 code
= PRC_UNREACH_PORT
;
472 case ICMP6_PACKET_TOO_BIG
:
473 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
480 * Updating the path MTU will be done after examining
481 * intermediate extension headers.
486 case ICMP6_TIME_EXCEEDED
:
487 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
489 case ICMP6_TIME_EXCEED_TRANSIT
:
490 case ICMP6_TIME_EXCEED_REASSEMBLY
:
491 code
+= PRC_TIMXCEED_INTRANS
;
499 case ICMP6_PARAM_PROB
:
500 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
502 case ICMP6_PARAMPROB_NEXTHEADER
:
503 code
= PRC_UNREACH_PROTOCOL
;
505 case ICMP6_PARAMPROB_HEADER
:
506 case ICMP6_PARAMPROB_OPTION
:
507 code
= PRC_PARAMPROB
;
515 case ICMP6_ECHO_REQUEST
:
516 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
520 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
521 icmp6stat
.icp6s_toofreq
++;
525 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
527 goto rate_limit_checked
;
530 if ((n
->m_flags
& M_EXT
) != 0
531 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
533 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
536 * Prepare an internal mbuf. m_pullup() doesn't
537 * always copy the length we specified.
539 if (maxlen
>= MCLBYTES
) {
542 goto rate_limit_checked
;
545 MGETHDR(n
, M_DONTWAIT
, n0
->m_type
); /* MAC-OK */
546 if (n
&& maxlen
>= MHLEN
) {
547 MCLGET(n
, M_DONTWAIT
);
548 if ((n
->m_flags
& M_EXT
) == 0) {
556 goto rate_limit_checked
;
559 M_COPY_PKTHDR(n
, n0
);
561 * Copy IPv6 and ICMPv6 only.
563 nip6
= mtod(n
, struct ip6_hdr
*);
564 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
565 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
566 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
567 noff
= sizeof(struct ip6_hdr
);
568 n
->m_pkthdr
.len
= n
->m_len
=
569 noff
+ sizeof(struct icmp6_hdr
);
571 * Adjust mbuf. ip6_plen will be adjusted in
574 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
575 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
577 n0
->m_flags
&= ~M_PKTHDR
;
579 nip6
= mtod(n
, struct ip6_hdr
*);
580 IP6_EXTHDR_GET(nicmp6
, struct icmp6_hdr
*, n
, off
,
584 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
585 nicmp6
->icmp6_code
= 0;
587 icmp6stat
.icp6s_reflect
++;
588 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
589 icmp6_reflect(n
, noff
);
591 goto rate_limit_checked
;
594 case ICMP6_ECHO_REPLY
:
595 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
600 case MLD6_LISTENER_QUERY
:
601 case MLD6_LISTENER_REPORT
:
602 if (icmp6len
< sizeof(struct mld6_hdr
))
604 if (icmp6
->icmp6_type
== MLD6_LISTENER_QUERY
) /* XXX: ugly... */
605 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
607 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
609 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
610 icmp6stat
.icp6s_toofreq
++;
614 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
622 goto rate_limit_checked
;
625 case MLD6_LISTENER_DONE
:
626 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
627 if (icmp6len
< sizeof(struct mld6_hdr
)) /* necessary? */
629 break; /* nothing to be done in kernel */
631 case MLD6_MTRACE_RESP
:
633 /* XXX: these two are experimental. not officially defind. */
634 /* XXX: per-interface statistics? */
635 break; /* just pass it to applications */
641 /* By RFC 4620 refuse to answer queries from global scope addresses */
642 if ((icmp6_nodeinfo
& 8) != 8 && in6_addrscope(&ip6
->ip6_src
) == IPV6_ADDR_SCOPE_GLOBAL
)
645 if (icmp6len
< sizeof(struct icmp6_nodeinfo
))
648 #ifndef PULLDOWN_TEST
649 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
650 return IPPROTO_DONE
);
652 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
653 icmp6stat
.icp6s_toofreq
++;
657 n
= m_copy(m
, 0, M_COPYALL
);
659 n
= ni6_input(n
, off
);
661 noff
= sizeof(struct ip6_hdr
);
662 icmp6stat
.icp6s_reflect
++;
663 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
664 icmp6_reflect(n
, noff
);
666 goto rate_limit_checked
;
674 case ND_ROUTER_SOLICIT
:
675 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
678 if (icmp6len
< sizeof(struct nd_router_solicit
))
681 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
682 icmp6stat
.icp6s_toofreq
++;
686 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
688 nd6_rs_input(m
, off
, icmp6len
);
692 nd6_rs_input(n
, off
, icmp6len
);
694 goto rate_limit_checked
;
697 case ND_ROUTER_ADVERT
:
698 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
701 if (icmp6len
< sizeof(struct nd_router_advert
))
704 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
705 icmp6stat
.icp6s_toofreq
++;
709 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
711 nd6_ra_input(m
, off
, icmp6len
);
715 nd6_ra_input(n
, off
, icmp6len
);
717 goto rate_limit_checked
;
720 case ND_NEIGHBOR_SOLICIT
:
721 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
724 if (icmp6len
< sizeof(struct nd_neighbor_solicit
))
727 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
728 icmp6stat
.icp6s_toofreq
++;
732 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
734 nd6_ns_input(m
, off
, icmp6len
);
738 nd6_ns_input(n
, off
, icmp6len
);
740 goto rate_limit_checked
;
743 case ND_NEIGHBOR_ADVERT
:
744 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
747 if (icmp6len
< sizeof(struct nd_neighbor_advert
))
750 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
751 icmp6stat
.icp6s_toofreq
++;
755 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
757 nd6_na_input(m
, off
, icmp6len
);
761 nd6_na_input(n
, off
, icmp6len
);
763 goto rate_limit_checked
;
767 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
770 if (icmp6len
< sizeof(struct nd_redirect
))
773 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
774 icmp6stat
.icp6s_toofreq
++;
778 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
780 icmp6_redirect_input(m
, off
);
784 icmp6_redirect_input(n
, off
);
786 goto rate_limit_checked
;
789 case ICMP6_ROUTER_RENUMBERING
:
790 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
791 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
)
793 if (icmp6len
< sizeof(struct icmp6_router_renum
))
798 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
799 icmp6stat
.icp6s_toofreq
++;
804 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
805 icmp6
->icmp6_type
, ip6_sprintf(&ip6
->ip6_src
),
806 ip6_sprintf(&ip6
->ip6_dst
),
807 m
->m_pkthdr
.rcvif
? m
->m_pkthdr
.rcvif
->if_index
: 0));
808 if (icmp6
->icmp6_type
< ICMP6_ECHO_REQUEST
) {
809 /* ICMPv6 error: MUST deliver it by spec... */
813 /* ICMPv6 informational: MUST not deliver */
814 goto rate_limit_checked
;
818 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
819 icmp6stat
.icp6s_toofreq
++;
823 if (icmp6_notify_error(m
, off
, icmp6len
, code
)) {
824 /* In this case, m should've been freed. */
825 return(IPPROTO_DONE
);
830 icmp6stat
.icp6s_badcode
++;
834 icmp6stat
.icp6s_badlen
++;
838 if (icmp6_ratelimit(&ip6
->ip6_dst
, icmp6
->icmp6_type
, code
)) {
839 icmp6stat
.icp6s_toofreq
++;
843 /* deliver the packet to appropriate sockets */
844 icmp6_rip6_input(&m
, *offp
);
854 icmp6_notify_error(m
, off
, icmp6len
, code
)
856 int off
, icmp6len
, code
;
858 struct icmp6_hdr
*icmp6
;
859 struct ip6_hdr
*eip6
;
861 struct sockaddr_in6 icmp6src
, icmp6dst
;
863 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
864 icmp6stat
.icp6s_tooshort
++;
867 #ifndef PULLDOWN_TEST
868 IP6_EXTHDR_CHECK(m
, off
,
869 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
871 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
873 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
874 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
876 icmp6stat
.icp6s_tooshort
++;
880 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
882 /* Detect the upper level protocol */
884 void (*ctlfunc
)(int, struct sockaddr
*, void *);
885 u_int8_t nxt
= eip6
->ip6_nxt
;
886 int eoff
= off
+ sizeof(struct icmp6_hdr
) +
887 sizeof(struct ip6_hdr
);
888 struct ip6ctlparam ip6cp
;
889 struct in6_addr
*finaldst
= NULL
;
890 int icmp6type
= icmp6
->icmp6_type
;
892 struct ip6_rthdr
*rth
;
893 struct ip6_rthdr0
*rth0
;
896 while (1) { /* XXX: should avoid infinite loop explicitly? */
900 case IPPROTO_HOPOPTS
:
901 case IPPROTO_DSTOPTS
:
903 #ifndef PULLDOWN_TEST
904 IP6_EXTHDR_CHECK(m
, 0, eoff
+
905 sizeof(struct ip6_ext
),
907 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
910 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
913 icmp6stat
.icp6s_tooshort
++;
918 if (nxt
== IPPROTO_AH
)
919 eoff
+= (eh
->ip6e_len
+ 2) << 2;
921 eoff
+= (eh
->ip6e_len
+ 1) << 3;
924 case IPPROTO_ROUTING
:
926 * When the erroneous packet contains a
927 * routing header, we should examine the
928 * header to determine the final destination.
929 * Otherwise, we can't properly update
930 * information that depends on the final
931 * destination (e.g. path MTU).
933 #ifndef PULLDOWN_TEST
934 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
936 rth
= (struct ip6_rthdr
*)(mtod(m
, caddr_t
)
939 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
942 icmp6stat
.icp6s_tooshort
++;
946 rthlen
= (rth
->ip6r_len
+ 1) << 3;
948 * XXX: currently there is no
949 * officially defined type other
951 * Note that if the segment left field
952 * is 0, all intermediate hops must
955 if (rth
->ip6r_segleft
&&
956 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
959 #ifndef PULLDOWN_TEST
960 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
962 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
965 struct ip6_rthdr0
*, m
,
968 icmp6stat
.icp6s_tooshort
++;
972 /* just ignore a bogus header */
973 if ((rth0
->ip6r0_len
% 2) == 0 &&
974 (hops
= rth0
->ip6r0_len
/2))
975 finaldst
= (struct in6_addr
*)(rth0
+ 1) + (hops
- 1);
980 case IPPROTO_FRAGMENT
:
981 #ifndef PULLDOWN_TEST
982 IP6_EXTHDR_CHECK(m
, 0, eoff
+
983 sizeof(struct ip6_frag
),
985 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
988 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
991 icmp6stat
.icp6s_tooshort
++;
996 * Data after a fragment header is meaningless
997 * unless it is the first fragment, but
998 * we'll go to the notify label for path MTU
1001 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
)
1004 eoff
+= sizeof(struct ip6_frag
);
1009 * This case includes ESP and the No Next
1010 * Header. In such cases going to the notify
1011 * label does not have any meaning
1012 * (i.e. ctlfunc will be NULL), but we go
1013 * anyway since we might have to update
1014 * path MTU information.
1020 #ifndef PULLDOWN_TEST
1021 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
1023 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
1024 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
1025 if (icmp6
== NULL
) {
1026 icmp6stat
.icp6s_tooshort
++;
1031 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1032 bzero(&icmp6dst
, sizeof(icmp6dst
));
1033 icmp6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
1034 icmp6dst
.sin6_family
= AF_INET6
;
1035 if (finaldst
== NULL
)
1036 icmp6dst
.sin6_addr
= eip6
->ip6_dst
;
1038 icmp6dst
.sin6_addr
= *finaldst
;
1039 icmp6dst
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1040 &icmp6dst
.sin6_addr
);
1041 #ifndef SCOPEDROUTING
1042 if (in6_embedscope(&icmp6dst
.sin6_addr
, &icmp6dst
,
1044 /* should be impossbile */
1046 "icmp6_notify_error: in6_embedscope failed\n"));
1052 * retrieve parameters from the inner IPv6 header, and convert
1053 * them into sockaddr structures.
1055 bzero(&icmp6src
, sizeof(icmp6src
));
1056 icmp6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1057 icmp6src
.sin6_family
= AF_INET6
;
1058 icmp6src
.sin6_addr
= eip6
->ip6_src
;
1059 icmp6src
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1060 &icmp6src
.sin6_addr
);
1061 #ifndef SCOPEDROUTING
1062 if (in6_embedscope(&icmp6src
.sin6_addr
, &icmp6src
,
1064 /* should be impossbile */
1066 "icmp6_notify_error: in6_embedscope failed\n"));
1070 icmp6src
.sin6_flowinfo
=
1071 (eip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
);
1073 if (finaldst
== NULL
)
1074 finaldst
= &eip6
->ip6_dst
;
1076 ip6cp
.ip6c_icmp6
= icmp6
;
1077 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1078 ip6cp
.ip6c_off
= eoff
;
1079 ip6cp
.ip6c_finaldst
= finaldst
;
1080 ip6cp
.ip6c_src
= &icmp6src
;
1081 ip6cp
.ip6c_nxt
= nxt
;
1083 if (icmp6type
== ICMP6_PACKET_TOO_BIG
) {
1084 notifymtu
= ntohl(icmp6
->icmp6_mtu
);
1085 ip6cp
.ip6c_cmdarg
= (void *)¬ifymtu
;
1086 icmp6_mtudisc_update(&ip6cp
, 1); /*XXX*/
1089 ctlfunc
= (void (*)(int, struct sockaddr
*, void *))
1090 (ip6_protox
[nxt
]->pr_ctlinput
);
1092 (void) (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6dst
,
1104 icmp6_mtudisc_update(ip6cp
, validated
)
1105 struct ip6ctlparam
*ip6cp
;
1108 struct in6_addr
*dst
= ip6cp
->ip6c_finaldst
;
1109 struct icmp6_hdr
*icmp6
= ip6cp
->ip6c_icmp6
;
1110 struct mbuf
*m
= ip6cp
->ip6c_m
; /* will be necessary for scope issue */
1111 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1112 struct rtentry
*rt
= NULL
;
1113 struct sockaddr_in6 sin6
;
1119 * In case the suggested mtu is less than IPV6_MMTU, we
1120 * only need to remember that it was for above mentioned
1121 * "alwaysfrag" case.
1122 * Try to be as close to the spec as possible.
1124 if (mtu
< IPV6_MMTU
)
1125 mtu
= IPV6_MMTU
- 8;
1128 bzero(&sin6
, sizeof(sin6
));
1129 sin6
.sin6_family
= PF_INET6
;
1130 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1131 sin6
.sin6_addr
= *dst
;
1132 /* XXX normally, this won't happen */
1133 if (IN6_IS_ADDR_LINKLOCAL(dst
)) {
1134 sin6
.sin6_addr
.s6_addr16
[1] =
1135 htons(m
->m_pkthdr
.rcvif
->if_index
);
1137 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1138 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, RTF_CLONING
| RTF_PRCLONING
);
1140 if (rt
&& (rt
->rt_flags
& RTF_HOST
)
1141 && !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1142 if (mtu
< IPV6_MMTU
) {
1144 rt
->rt_rmx
.rmx_locks
|= RTV_MTU
;
1145 } else if (mtu
< rt
->rt_ifp
->if_mtu
&&
1146 rt
->rt_rmx
.rmx_mtu
> mtu
) {
1147 icmp6stat
.icp6s_pmtuchg
++;
1148 rt
->rt_rmx
.rmx_mtu
= mtu
;
1156 * Process a Node Information Query packet, based on
1157 * draft-ietf-ipngwg-icmp-name-lookups-07.
1159 * Spec incompatibilities:
1160 * - IPv6 Subject address handling
1161 * - IPv4 Subject address handling support missing
1162 * - Proxy reply (answer even if it's not for me)
1163 * - joins NI group address at in6_ifattach() time only, does not cope
1164 * with hostname changes by sethostname(3)
1166 #define hostnamelen strlen(hostname)
1167 static struct mbuf
*
1172 struct icmp6_nodeinfo
*ni6
, *nni6
;
1173 struct mbuf
*n
= NULL
;
1176 int replylen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1177 struct ni_reply_fqdn
*fqdn
;
1178 int addrs
; /* for NI_QTYPE_NODEADDR */
1179 struct ifnet
*ifp
= NULL
; /* for NI_QTYPE_NODEADDR */
1180 struct sockaddr_in6 sin6
; /* double meaning; ip6_dst and subjectaddr */
1181 struct sockaddr_in6 sin6_d
; /* XXX: we should retrieve this from m_aux */
1182 struct ip6_hdr
*ip6
;
1183 int oldfqdn
= 0; /* if 1, return pascal string (03 draft) */
1185 struct in6_ifaddr
*ia6
= NULL
;
1187 ip6
= mtod(m
, struct ip6_hdr
*);
1188 #ifndef PULLDOWN_TEST
1189 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1191 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1193 /* m is already reclaimed */
1199 * Validate IPv6 destination address.
1201 * The Responder must discard the Query without further processing
1202 * unless it is one of the Responder's unicast or anycast addresses, or
1203 * a link-local scope multicast address which the Responder has joined.
1204 * [icmp-name-lookups-07, Section 4.]
1206 bzero(&sin6
, sizeof(sin6
));
1207 sin6
.sin6_family
= AF_INET6
;
1208 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1209 bcopy(&ip6
->ip6_dst
, &sin6
.sin6_addr
, sizeof(sin6
.sin6_addr
));
1211 if ((ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr((struct sockaddr
*)&sin6
)) != NULL
) {
1212 /* unicast/anycast, fine */
1213 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1214 (icmp6_nodeinfo
& 4) == 0) {
1215 ifafree(&ia6
->ia_ifa
);
1217 nd6log((LOG_DEBUG
, "ni6_input: ignore node info to "
1218 "a temporary address in %s:%d",
1219 __FILE__
, __LINE__
));
1222 ifafree(&ia6
->ia_ifa
);
1224 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
))
1225 ; /* link-local multicast, fine */
1229 /* validate query Subject field. */
1230 qtype
= ntohs(ni6
->ni_qtype
);
1231 subjlen
= m
->m_pkthdr
.len
- off
- sizeof(struct icmp6_nodeinfo
);
1234 case NI_QTYPE_SUPTYPES
:
1236 if (ni6
->ni_code
== ICMP6_NI_SUBJ_FQDN
&& subjlen
== 0)
1240 case NI_QTYPE_NODEADDR
:
1241 switch (ni6
->ni_code
) {
1242 case ICMP6_NI_SUBJ_IPV6
:
1243 #if ICMP6_NI_SUBJ_IPV6 != 0
1247 * backward compatibility - try to accept 03 draft
1248 * format, where no Subject is present.
1250 if (qtype
== NI_QTYPE_FQDN
&& ni6
->ni_code
== 0 &&
1255 #if ICMP6_NI_SUBJ_IPV6 != 0
1256 if (ni6
->ni_code
!= ICMP6_NI_SUBJ_IPV6
)
1260 if (subjlen
!= sizeof(sin6
.sin6_addr
))
1264 * Validate Subject address.
1266 * Not sure what exactly "address belongs to the node"
1267 * means in the spec, is it just unicast, or what?
1269 * At this moment we consider Subject address as
1270 * "belong to the node" if the Subject address equals
1271 * to the IPv6 destination address; validation for
1272 * IPv6 destination address should have done enough
1275 * We do not do proxy at this moment.
1277 /* m_pulldown instead of copy? */
1278 m_copydata(m
, off
+ sizeof(struct icmp6_nodeinfo
),
1279 subjlen
, (caddr_t
)&sin6
.sin6_addr
);
1280 sin6
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1282 #ifndef SCOPEDROUTING
1283 in6_embedscope(&sin6
.sin6_addr
, &sin6
, NULL
, NULL
);
1285 bzero(&sin6_d
, sizeof(sin6_d
));
1286 sin6_d
.sin6_family
= AF_INET6
; /* not used, actually */
1287 sin6_d
.sin6_len
= sizeof(sin6_d
); /* ditto */
1288 sin6_d
.sin6_addr
= ip6
->ip6_dst
;
1289 sin6_d
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1291 #ifndef SCOPEDROUTING
1292 in6_embedscope(&sin6_d
.sin6_addr
, &sin6_d
, NULL
, NULL
);
1294 subj
= (char *)&sin6
;
1295 if (SA6_ARE_ADDR_EQUAL(&sin6
, &sin6_d
))
1299 * XXX if we are to allow other cases, we should really
1300 * be careful about scope here.
1301 * basically, we should disallow queries toward IPv6
1302 * destination X with subject Y, if scope(X) > scope(Y).
1303 * if we allow scope(X) > scope(Y), it will result in
1304 * information leakage across scope boundary.
1308 case ICMP6_NI_SUBJ_FQDN
:
1310 * Validate Subject name with gethostname(3).
1312 * The behavior may need some debate, since:
1313 * - we are not sure if the node has FQDN as
1314 * hostname (returned by gethostname(3)).
1315 * - the code does wildcard match for truncated names.
1316 * however, we are not sure if we want to perform
1317 * wildcard match, if gethostname(3) side has
1318 * truncated hostname.
1320 n
= ni6_nametodns(hostname
, hostnamelen
, 0);
1321 if (!n
|| n
->m_next
|| n
->m_len
== 0)
1323 IP6_EXTHDR_GET(subj
, char *, m
,
1324 off
+ sizeof(struct icmp6_nodeinfo
), subjlen
);
1327 if (!ni6_dnsmatch(subj
, subjlen
, mtod(n
, const char *),
1335 case ICMP6_NI_SUBJ_IPV4
: /* XXX: to be implemented? */
1342 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1345 if ((icmp6_nodeinfo
& 1) == 0)
1348 case NI_QTYPE_NODEADDR
:
1349 if ((icmp6_nodeinfo
& 2) == 0)
1354 /* guess reply length */
1357 break; /* no reply data */
1358 case NI_QTYPE_SUPTYPES
:
1359 replylen
+= sizeof(u_int32_t
);
1362 /* XXX will append an mbuf */
1363 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1365 case NI_QTYPE_NODEADDR
:
1366 addrs
= ni6_addrs(ni6
, &ifp
, subj
);
1367 if ((replylen
+= addrs
* (sizeof(struct in6_addr
) +
1368 sizeof(u_int32_t
))) > MCLBYTES
)
1369 replylen
= MCLBYTES
; /* XXX: will truncate pkt later */
1373 * XXX: We must return a reply with the ICMP6 code
1374 * `unknown Qtype' in this case. However we regard the case
1375 * as an FQDN query for backward compatibility.
1376 * Older versions set a random value to this field,
1377 * so it rarely varies in the defined qtypes.
1378 * But the mechanism is not reliable...
1379 * maybe we should obsolete older versions.
1381 qtype
= NI_QTYPE_FQDN
;
1382 /* XXX will append an mbuf */
1383 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1388 /* allocate an mbuf to reply. */
1389 MGETHDR(n
, M_DONTWAIT
, m
->m_type
); /* MAC-OK */
1394 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1395 if (replylen
> MHLEN
) {
1396 if (replylen
> MCLBYTES
) {
1398 * XXX: should we try to allocate more? But MCLBYTES
1399 * is probably much larger than IPV6_MMTU...
1403 MCLGET(n
, M_DONTWAIT
);
1404 if ((n
->m_flags
& M_EXT
) == 0) {
1408 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
1410 /* copy mbuf header and IPv6 + Node Information base headers */
1411 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), sizeof(struct ip6_hdr
));
1412 nni6
= (struct icmp6_nodeinfo
*)(mtod(n
, struct ip6_hdr
*) + 1);
1413 bcopy((caddr_t
)ni6
, (caddr_t
)nni6
, sizeof(struct icmp6_nodeinfo
));
1415 /* qtype dependent procedure */
1418 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1421 case NI_QTYPE_SUPTYPES
:
1424 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1425 nni6
->ni_flags
= htons(0x0000); /* raw bitmap */
1426 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1427 v
= (u_int32_t
)htonl(0x0000000f);
1428 bcopy(&v
, nni6
+ 1, sizeof(u_int32_t
));
1432 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1433 fqdn
= (struct ni_reply_fqdn
*)(mtod(n
, caddr_t
) +
1434 sizeof(struct ip6_hdr
) +
1435 sizeof(struct icmp6_nodeinfo
));
1436 nni6
->ni_flags
= 0; /* XXX: meaningless TTL */
1437 fqdn
->ni_fqdn_ttl
= 0; /* ditto. */
1439 * XXX do we really have FQDN in variable "hostname"?
1441 n
->m_next
= ni6_nametodns(hostname
, hostnamelen
, oldfqdn
);
1442 if (n
->m_next
== NULL
)
1444 /* XXX we assume that n->m_next is not a chain */
1445 if (n
->m_next
->m_next
!= NULL
)
1447 n
->m_pkthdr
.len
+= n
->m_next
->m_len
;
1449 case NI_QTYPE_NODEADDR
:
1453 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1454 n
->m_pkthdr
.len
= n
->m_len
=
1455 sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1456 lenlim
= M_TRAILINGSPACE(n
);
1457 copied
= ni6_store_addrs(ni6
, nni6
, ifp
, lenlim
);
1458 /* XXX: reset mbuf length */
1459 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
1460 sizeof(struct icmp6_nodeinfo
) + copied
;
1464 break; /* XXX impossible! */
1467 nni6
->ni_type
= ICMP6_NI_REPLY
;
1480 * make a mbuf with DNS-encoded string. no compression support.
1482 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1483 * treated as truncated name (two \0 at the end). this is a wild guess.
1485 static struct mbuf
*
1486 ni6_nametodns(name
, namelen
, old
)
1489 int old
; /* return pascal string if non-zero */
1501 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1502 MGET(m
, M_DONTWAIT
, MT_DATA
);
1503 if (m
&& len
> MLEN
) {
1504 MCLGET(m
, M_DONTWAIT
);
1505 if ((m
->m_flags
& M_EXT
) == 0)
1514 *mtod(m
, char *) = namelen
;
1515 bcopy(name
, mtod(m
, char *) + 1, namelen
);
1519 cp
= mtod(m
, char *);
1520 ep
= mtod(m
, char *) + M_TRAILINGSPACE(m
);
1522 /* if not certain about my name, return empty buffer */
1527 * guess if it looks like shortened hostname, or FQDN.
1528 * shortened hostname needs two trailing "\0".
1531 for (p
= name
; p
< name
+ namelen
; p
++) {
1532 if (*p
&& *p
== '.')
1541 while (cp
< ep
&& p
< name
+ namelen
) {
1543 for (q
= p
; q
< name
+ namelen
&& *q
&& *q
!= '.'; q
++)
1545 /* result does not fit into mbuf */
1546 if (cp
+ i
+ 1 >= ep
)
1549 * DNS label length restriction, RFC1035 page 8.
1550 * "i == 0" case is included here to avoid returning
1551 * 0-length label on "foo..bar".
1553 if (i
<= 0 || i
>= 64)
1559 if (p
< name
+ namelen
&& *p
== '.')
1563 if (cp
+ nterm
>= ep
)
1567 m
->m_len
= cp
- mtod(m
, char *);
1571 panic("should not reach here");
1581 * check if two DNS-encoded string matches. takes care of truncated
1582 * form (with \0\0 at the end). no compression support.
1583 * XXX upper/lowercase match (see RFC2065)
1586 ni6_dnsmatch(a
, alen
, b
, blen
)
1592 const char *a0
, *b0
;
1595 /* simplest case - need validation? */
1596 if (alen
== blen
&& bcmp(a
, b
, alen
) == 0)
1602 /* termination is mandatory */
1603 if (alen
< 2 || blen
< 2)
1605 if (a0
[alen
- 1] != '\0' || b0
[blen
- 1] != '\0')
1610 while (a
- a0
< alen
&& b
- b0
< blen
) {
1611 if (a
- a0
+ 1 > alen
|| b
- b0
+ 1 > blen
)
1614 if ((signed char)a
[0] < 0 || (signed char)b
[0] < 0)
1616 /* we don't support compression yet */
1617 if (a
[0] >= 64 || b
[0] >= 64)
1620 /* truncated case */
1621 if (a
[0] == 0 && a
- a0
== alen
- 1)
1623 if (b
[0] == 0 && b
- b0
== blen
- 1)
1625 if (a
[0] == 0 || b
[0] == 0)
1631 if (a
- a0
+ 1 + l
> alen
|| b
- b0
+ 1 + l
> blen
)
1633 if (bcmp(a
+ 1, b
+ 1, l
) != 0)
1640 if (a
- a0
== alen
&& b
- b0
== blen
)
1647 * calculate the number of addresses to be returned in the node info reply.
1650 ni6_addrs(ni6
, ifpp
, subj
)
1651 struct icmp6_nodeinfo
*ni6
;
1652 struct ifnet
**ifpp
;
1656 struct in6_ifaddr
*ifa6
;
1658 struct sockaddr_in6
*subj_ip6
= NULL
; /* XXX pedant */
1659 int addrs
= 0, addrsofif
, iffound
= 0;
1660 int niflags
= ni6
->ni_flags
;
1662 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0) {
1663 switch (ni6
->ni_code
) {
1664 case ICMP6_NI_SUBJ_IPV6
:
1665 if (subj
== NULL
) /* must be impossible... */
1667 subj_ip6
= (struct sockaddr_in6
*)subj
;
1671 * XXX: we only support IPv6 subject address for
1678 ifnet_head_lock_shared();
1679 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
1681 ifnet_lock_shared(ifp
);
1682 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1684 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1686 ifa6
= (struct in6_ifaddr
*)ifa
;
1688 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0 &&
1689 IN6_ARE_ADDR_EQUAL(&subj_ip6
->sin6_addr
,
1690 &ifa6
->ia_addr
.sin6_addr
))
1694 * IPv4-mapped addresses can only be returned by a
1695 * Node Information proxy, since they represent
1696 * addresses of IPv4-only nodes, which perforce do
1697 * not implement this protocol.
1698 * [icmp-name-lookups-07, Section 5.4]
1699 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1700 * this function at this moment.
1703 /* What do we have to do about ::1? */
1704 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1705 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1706 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1709 case IPV6_ADDR_SCOPE_SITELOCAL
:
1710 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1713 case IPV6_ADDR_SCOPE_GLOBAL
:
1714 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1722 * check if anycast is okay.
1723 * XXX: just experimental. not in the spec.
1725 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1726 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1727 continue; /* we need only unicast addresses */
1728 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1729 (icmp6_nodeinfo
& 4) == 0) {
1732 addrsofif
++; /* count the address */
1734 ifnet_lock_done(ifp
);
1749 ni6_store_addrs(ni6
, nni6
, ifp0
, resid
)
1750 struct icmp6_nodeinfo
*ni6
, *nni6
;
1754 struct ifnet
*ifp
= ifp0
;
1755 struct in6_ifaddr
*ifa6
;
1757 struct ifnet
*ifp_dep
= NULL
;
1758 int copied
= 0, allow_deprecated
= 0;
1759 u_char
*cp
= (u_char
*)(nni6
+ 1);
1760 int niflags
= ni6
->ni_flags
;
1762 struct timeval timenow
;
1764 getmicrotime(&timenow
);
1766 if (ifp0
== NULL
&& !(niflags
& NI_NODEADDR_FLAG_ALL
))
1767 return(0); /* needless to copy */
1771 ifnet_head_lock_shared();
1772 if (ifp
== NULL
) ifp
= TAILQ_FIRST(&ifnet_head
);
1774 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1775 ifnet_lock_shared(ifp
);
1776 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1777 ifa
= ifa
->ifa_list
.tqe_next
)
1779 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1781 ifa6
= (struct in6_ifaddr
*)ifa
;
1783 if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0 &&
1784 allow_deprecated
== 0) {
1786 * prefererred address should be put before
1787 * deprecated addresses.
1790 /* record the interface for later search */
1791 if (ifp_dep
== NULL
)
1796 else if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) == 0 &&
1797 allow_deprecated
!= 0)
1798 continue; /* we now collect deprecated addrs */
1800 /* What do we have to do about ::1? */
1801 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1802 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1803 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1806 case IPV6_ADDR_SCOPE_SITELOCAL
:
1807 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1810 case IPV6_ADDR_SCOPE_GLOBAL
:
1811 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1819 * check if anycast is okay.
1820 * XXX: just experimental. not in the spec.
1822 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1823 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1825 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1826 (icmp6_nodeinfo
& 4) == 0) {
1830 /* now we can copy the address */
1831 if (resid
< sizeof(struct in6_addr
) +
1832 sizeof(u_int32_t
)) {
1834 * We give up much more copy.
1835 * Set the truncate flag and return.
1838 NI_NODEADDR_FLAG_TRUNCATE
;
1839 ifnet_lock_done(ifp
);
1845 * Set the TTL of the address.
1846 * The TTL value should be one of the following
1847 * according to the specification:
1849 * 1. The remaining lifetime of a DHCP lease on the
1851 * 2. The remaining Valid Lifetime of a prefix from
1852 * which the address was derived through Stateless
1853 * Autoconfiguration.
1855 * Note that we currently do not support stateful
1856 * address configuration by DHCPv6, so the former
1857 * case can't happen.
1859 if (ifa6
->ia6_lifetime
.ia6t_expire
== 0)
1860 ltime
= ND6_INFINITE_LIFETIME
;
1862 if (ifa6
->ia6_lifetime
.ia6t_expire
>
1864 ltime
= htonl(ifa6
->ia6_lifetime
.ia6t_expire
- timenow
.tv_sec
);
1869 bcopy(<ime
, cp
, sizeof(u_int32_t
));
1870 cp
+= sizeof(u_int32_t
);
1872 /* copy the address itself */
1873 bcopy(&ifa6
->ia_addr
.sin6_addr
, cp
,
1874 sizeof(struct in6_addr
));
1875 /* XXX: KAME link-local hack; remove ifindex */
1876 if (IN6_IS_ADDR_LINKLOCAL(&ifa6
->ia_addr
.sin6_addr
))
1877 ((struct in6_addr
*)cp
)->s6_addr16
[1] = 0;
1878 cp
+= sizeof(struct in6_addr
);
1880 resid
-= (sizeof(struct in6_addr
) + sizeof(u_int32_t
));
1881 copied
+= (sizeof(struct in6_addr
) +
1884 ifnet_lock_done(ifp
);
1885 if (ifp0
) /* we need search only on the specified IF */
1890 if (allow_deprecated
== 0 && ifp_dep
!= NULL
) {
1892 allow_deprecated
= 1;
1901 * XXX almost dup'ed code with rip6_input.
1904 icmp6_rip6_input(mp
, off
)
1908 struct mbuf
*m
= *mp
;
1909 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1910 struct in6pcb
*in6p
;
1911 struct in6pcb
*last
= NULL
;
1912 struct sockaddr_in6 rip6src
;
1913 struct icmp6_hdr
*icmp6
;
1914 struct mbuf
*opts
= NULL
;
1916 #ifndef PULLDOWN_TEST
1917 /* this is assumed to be safe. */
1918 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
1920 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
1921 if (icmp6
== NULL
) {
1922 /* m is already reclaimed */
1923 return IPPROTO_DONE
;
1927 bzero(&rip6src
, sizeof(rip6src
));
1928 rip6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1929 rip6src
.sin6_family
= AF_INET6
;
1930 /* KAME hack: recover scopeid */
1931 (void)in6_recoverscope(&rip6src
, &ip6
->ip6_src
, m
->m_pkthdr
.rcvif
);
1933 lck_rw_lock_shared(ripcbinfo
.mtx
);
1934 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
1936 if ((in6p
->inp_vflag
& INP_IPV6
) == 0)
1939 if (!(in6p
->in6p_flags
& INP_IPV6
))
1942 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
)
1944 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
1945 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
))
1947 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
1948 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
))
1950 if (in6p
->in6p_icmp6filt
1951 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
1952 in6p
->in6p_icmp6filt
))
1956 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
1957 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1958 ip6_savecontrol(last
, &opts
, ip6
, n
);
1959 /* strip intermediate headers */
1961 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1962 (struct sockaddr
*)&rip6src
,
1963 n
, opts
, NULL
) != 0) {
1964 sorwakeup(last
->in6p_socket
);
1971 lck_rw_done(ripcbinfo
.mtx
);
1973 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1974 ip6_savecontrol(last
, &opts
, ip6
, m
);
1975 /* strip intermediate headers */
1977 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1978 (struct sockaddr
*)&rip6src
, m
, opts
, NULL
) != 0) {
1979 sorwakeup(last
->in6p_socket
);
1983 ip6stat
.ip6s_delivered
--;
1985 return IPPROTO_DONE
;
1989 * Reflect the ip6 packet back to the source.
1990 * OFF points to the icmp6 header, counted from the top of the mbuf.
1993 icmp6_reflect(m
, off
)
1997 struct ip6_hdr
*ip6
;
1998 struct icmp6_hdr
*icmp6
;
1999 struct in6_ifaddr
*ia
;
2000 struct in6_addr t
, src_storage
, *src
= 0;
2003 struct ifnet
*outif
= NULL
;
2004 struct sockaddr_in6 sa6_src
, sa6_dst
;
2005 #ifdef COMPAT_RFC1885
2006 int mtu
= IPV6_MMTU
;
2007 struct sockaddr_in6
*sin6
= &icmp6_reflect_rt
.ro_dst
;
2010 /* too short to reflect */
2011 if (off
< sizeof(struct ip6_hdr
)) {
2013 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2014 (u_long
)off
, (u_long
)sizeof(struct ip6_hdr
),
2015 __FILE__
, __LINE__
));
2020 * If there are extra headers between IPv6 and ICMPv6, strip
2021 * off that header first.
2024 if (sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) > MHLEN
)
2025 panic("assumption failed in icmp6_reflect");
2027 if (off
> sizeof(struct ip6_hdr
)) {
2029 struct ip6_hdr nip6
;
2031 l
= off
- sizeof(struct ip6_hdr
);
2032 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
2034 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2036 if ((m
= m_pullup(m
, l
)) == NULL
)
2039 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
2040 } else /* off == sizeof(struct ip6_hdr) */ {
2042 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2044 if ((m
= m_pullup(m
, l
)) == NULL
)
2048 plen
= m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2049 ip6
= mtod(m
, struct ip6_hdr
*);
2050 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2051 icmp6
= (struct icmp6_hdr
*)(ip6
+ 1);
2052 type
= icmp6
->icmp6_type
; /* keep type for statistics */
2053 code
= icmp6
->icmp6_code
; /* ditto. */
2057 * ip6_input() drops a packet if its src is multicast.
2058 * So, the src is never multicast.
2060 ip6
->ip6_dst
= ip6
->ip6_src
;
2063 * XXX: make sure to embed scope zone information, using
2064 * already embedded IDs or the received interface (if any).
2065 * Note that rcvif may be NULL.
2066 * TODO: scoped routing case (XXX).
2068 bzero(&sa6_src
, sizeof(sa6_src
));
2069 sa6_src
.sin6_family
= AF_INET6
;
2070 sa6_src
.sin6_len
= sizeof(sa6_src
);
2071 sa6_src
.sin6_addr
= ip6
->ip6_dst
;
2072 in6_recoverscope(&sa6_src
, &ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
);
2073 in6_embedscope(&ip6
->ip6_dst
, &sa6_src
, NULL
, NULL
);
2074 bzero(&sa6_dst
, sizeof(sa6_dst
));
2075 sa6_dst
.sin6_family
= AF_INET6
;
2076 sa6_dst
.sin6_len
= sizeof(sa6_dst
);
2077 sa6_dst
.sin6_addr
= t
;
2078 in6_recoverscope(&sa6_dst
, &t
, m
->m_pkthdr
.rcvif
);
2079 in6_embedscope(&t
, &sa6_dst
, NULL
, NULL
);
2081 #ifdef COMPAT_RFC1885
2084 * RFC 1885 requires that echo reply should be truncated if it
2085 * does not fit in with (return) path MTU, but the description was
2086 * removed in the new spec.
2088 if (icmp6_reflect_rt
.ro_rt
== 0 ||
2089 ! (IN6_ARE_ADDR_EQUAL(&sin6
->sin6_addr
, &ip6
->ip6_dst
))) {
2090 if (icmp6_reflect_rt
.ro_rt
) {
2091 rtfree(icmp6_reflect_rt
.ro_rt
);
2092 icmp6_reflect_rt
.ro_rt
= 0;
2094 bzero(sin6
, sizeof(*sin6
));
2095 sin6
->sin6_family
= PF_INET6
;
2096 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2097 sin6
->sin6_addr
= ip6
->ip6_dst
;
2099 rtalloc_ign((struct route
*)&icmp6_reflect_rt
.ro_rt
,
2103 if (icmp6_reflect_rt
.ro_rt
== 0)
2106 if ((icmp6_reflect_rt
.ro_rt
->rt_flags
& RTF_HOST
)
2107 && mtu
< icmp6_reflect_rt
.ro_rt
->rt_ifp
->if_mtu
)
2108 mtu
= icmp6_reflect_rt
.ro_rt
->rt_rmx
.rmx_mtu
;
2110 if (mtu
< m
->m_pkthdr
.len
) {
2111 plen
-= (m
->m_pkthdr
.len
- mtu
);
2112 m_adj(m
, mtu
- m
->m_pkthdr
.len
);
2116 * If the incoming packet was addressed directly to us(i.e. unicast),
2117 * use dst as the src for the reply.
2118 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2119 * (for example) when we encounter an error while forwarding procedure
2120 * destined to a duplicated address of ours.
2122 lck_mtx_lock(nd6_mutex
);
2123 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
)
2124 if (IN6_ARE_ADDR_EQUAL(&t
, &ia
->ia_addr
.sin6_addr
) &&
2125 (ia
->ia6_flags
& (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
)) == 0) {
2129 lck_mtx_unlock(nd6_mutex
);
2130 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) && (m
->m_flags
& M_LOOP
)) {
2132 * This is the case if the dst is our link-local address
2133 * and the sender is also ourselves.
2140 struct route_in6 ro
;
2143 * This case matches to multicasts, our anycast, or unicasts
2144 * that we do not own. Select a source address based on the
2145 * source address of the erroneous packet.
2147 bzero(&ro
, sizeof(ro
));
2148 src
= in6_selectsrc(&sa6_src
, NULL
, NULL
, &ro
, NULL
, &src_storage
, &e
);
2150 rtfree(ro
.ro_rt
); /* XXX: we could use this */
2155 "icmp6_reflect: source can't be determined: "
2156 "dst=%s, error=%d\n",
2157 ip6_sprintf(&sa6_src
.sin6_addr
), e
));
2162 ip6
->ip6_src
= *src
;
2165 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2166 ip6
->ip6_vfc
|= IPV6_VERSION
;
2167 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2168 if (m
->m_pkthdr
.rcvif
) {
2169 /* XXX: This may not be the outgoing interface */
2170 ip6
->ip6_hlim
= nd_ifinfo
[m
->m_pkthdr
.rcvif
->if_index
].chlim
;
2172 ip6
->ip6_hlim
= ip6_defhlim
;
2174 icmp6
->icmp6_cksum
= 0;
2175 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
2176 sizeof(struct ip6_hdr
), plen
);
2179 * XXX option handling
2182 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
2184 /* Don't lookup socket */
2185 if (ipsec_bypass
== 0)
2186 (void)ipsec_setsocket(m
, NULL
);
2189 #ifdef COMPAT_RFC1885
2190 ip6_output(m
, NULL
, &icmp6_reflect_rt
, 0, NULL
, &outif
, 0);
2192 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
, 0);
2195 icmp6_ifoutstat_inc(outif
, type
, code
);
2212 icmp6_redirect_diag(src6
, dst6
, tgt6
)
2213 struct in6_addr
*src6
;
2214 struct in6_addr
*dst6
;
2215 struct in6_addr
*tgt6
;
2217 static char buf
[1024];
2218 snprintf(buf
, sizeof(buf
), "(src=%s dst=%s tgt=%s)",
2219 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
2224 icmp6_redirect_input(m
, off
)
2228 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2229 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2230 struct nd_redirect
*nd_rd
;
2231 int icmp6len
= ntohs(ip6
->ip6_plen
);
2232 char *lladdr
= NULL
;
2234 u_char
*redirhdr
= NULL
;
2235 int redirhdrlen
= 0;
2236 struct rtentry
*rt
= NULL
;
2239 struct in6_addr src6
= ip6
->ip6_src
;
2240 struct in6_addr redtgt6
;
2241 struct in6_addr reddst6
;
2242 union nd_opts ndopts
;
2247 /* XXX if we are router, we don't update route by icmp6 redirect */
2250 if (!icmp6_rediraccept
)
2253 #ifndef PULLDOWN_TEST
2254 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
2255 nd_rd
= (struct nd_redirect
*)((caddr_t
)ip6
+ off
);
2257 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
2258 if (nd_rd
== NULL
) {
2259 icmp6stat
.icp6s_tooshort
++;
2263 redtgt6
= nd_rd
->nd_rd_target
;
2264 reddst6
= nd_rd
->nd_rd_dst
;
2266 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2267 redtgt6
.s6_addr16
[1] = htons(ifp
->if_index
);
2268 if (IN6_IS_ADDR_LINKLOCAL(&reddst6
))
2269 reddst6
.s6_addr16
[1] = htons(ifp
->if_index
);
2272 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
2274 "ICMP6 redirect sent from %s rejected; "
2275 "must be from linklocal\n", ip6_sprintf(&src6
)));
2278 if (ip6
->ip6_hlim
!= 255) {
2280 "ICMP6 redirect sent from %s rejected; "
2281 "hlim=%d (must be 255)\n",
2282 ip6_sprintf(&src6
), ip6
->ip6_hlim
));
2286 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2287 struct sockaddr_in6 sin6
;
2288 struct in6_addr
*gw6
;
2290 bzero(&sin6
, sizeof(sin6
));
2291 sin6
.sin6_family
= AF_INET6
;
2292 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2293 bcopy(&reddst6
, &sin6
.sin6_addr
, sizeof(reddst6
));
2294 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0UL);
2296 if (rt
->rt_gateway
== NULL
||
2297 rt
->rt_gateway
->sa_family
!= AF_INET6
) {
2299 "ICMP6 redirect rejected; no route "
2300 "with inet6 gateway found for redirect dst: %s\n",
2301 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2306 gw6
= &(((struct sockaddr_in6
*)rt
->rt_gateway
)->sin6_addr
);
2307 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
2309 "ICMP6 redirect rejected; "
2310 "not equal to gw-for-src=%s (must be same): "
2313 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2319 "ICMP6 redirect rejected; "
2320 "no route found for redirect dst: %s\n",
2321 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2327 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
2329 "ICMP6 redirect rejected; "
2330 "redirect dst must be unicast: %s\n",
2331 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2335 is_router
= is_onlink
= 0;
2336 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2337 is_router
= 1; /* router case */
2338 if (bcmp(&redtgt6
, &reddst6
, sizeof(redtgt6
)) == 0)
2339 is_onlink
= 1; /* on-link destination case */
2340 if (!is_router
&& !is_onlink
) {
2342 "ICMP6 redirect rejected; "
2343 "neither router case nor onlink case: %s\n",
2344 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2347 /* validation passed */
2349 icmp6len
-= sizeof(*nd_rd
);
2350 nd6_option_init(nd_rd
+ 1, icmp6len
, &ndopts
);
2351 if (nd6_options(&ndopts
) < 0) {
2352 nd6log((LOG_INFO
, "icmp6_redirect_input: "
2353 "invalid ND option, rejected: %s\n",
2354 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2355 /* nd6_options have incremented stats */
2359 if (ndopts
.nd_opts_tgt_lladdr
) {
2360 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
2361 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
2364 if (ndopts
.nd_opts_rh
) {
2365 redirhdrlen
= ndopts
.nd_opts_rh
->nd_opt_rh_len
;
2366 redirhdr
= (u_char
*)(ndopts
.nd_opts_rh
+ 1); /* xxx */
2369 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
2371 "icmp6_redirect_input: lladdrlen mismatch for %s "
2372 "(if %d, icmp6 packet %d): %s\n",
2373 ip6_sprintf(&redtgt6
), ifp
->if_addrlen
, lladdrlen
- 2,
2374 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2379 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
2380 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
2382 if (!is_onlink
) { /* better router case. perform rtredirect. */
2383 /* perform rtredirect */
2384 struct sockaddr_in6 sdst
;
2385 struct sockaddr_in6 sgw
;
2386 struct sockaddr_in6 ssrc
;
2388 bzero(&sdst
, sizeof(sdst
));
2389 bzero(&sgw
, sizeof(sgw
));
2390 bzero(&ssrc
, sizeof(ssrc
));
2391 sdst
.sin6_family
= sgw
.sin6_family
= ssrc
.sin6_family
= AF_INET6
;
2392 sdst
.sin6_len
= sgw
.sin6_len
= ssrc
.sin6_len
=
2393 sizeof(struct sockaddr_in6
);
2394 bcopy(&redtgt6
, &sgw
.sin6_addr
, sizeof(struct in6_addr
));
2395 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2396 bcopy(&src6
, &ssrc
.sin6_addr
, sizeof(struct in6_addr
));
2397 rtredirect(ifp
, (struct sockaddr
*)&sdst
,
2398 (struct sockaddr
*)&sgw
, NULL
, RTF_GATEWAY
| RTF_HOST
,
2399 (struct sockaddr
*)&ssrc
, NULL
);
2401 /* finally update cached route in each socket via pfctlinput */
2403 struct sockaddr_in6 sdst
;
2405 bzero(&sdst
, sizeof(sdst
));
2406 sdst
.sin6_family
= AF_INET6
;
2407 sdst
.sin6_len
= sizeof(struct sockaddr_in6
);
2408 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2412 * Release the IPv6 domain lock because we are going to take domain_proto_mtx
2413 * and could otherwise cause a deadlock with other threads taking these locks
2414 * in the reverse order -- e.g. frag6_slowtimo() from pfslowtimo()
2416 lck_mtx_unlock(inet6_domain_mutex
);
2417 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
2418 lck_mtx_lock(inet6_domain_mutex
);
2420 key_sa_routechange((struct sockaddr
*)&sdst
);
2429 icmp6stat
.icp6s_badredirect
++;
2434 icmp6_redirect_output(m0
, rt
)
2438 struct ifnet
*ifp
; /* my outgoing interface */
2439 struct in6_addr ifp_ll6
;
2440 struct in6_addr
*router_ll6
;
2441 struct ip6_hdr
*sip6
; /* m0 as struct ip6_hdr */
2442 struct mbuf
*m
= NULL
; /* newly allocated one */
2443 struct ip6_hdr
*ip6
; /* m as struct ip6_hdr */
2444 struct nd_redirect
*nd_rd
;
2447 struct ifnet
*outif
= NULL
;
2448 struct sockaddr_in6 src_sa
;
2450 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, ND_REDIRECT
, 0);
2453 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
))
2456 /* if we are not router, we don't send icmp6 redirect */
2457 if (!ip6_forwarding
|| ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))
2462 * the source address must identify a neighbor, and
2463 * the destination address must not be a multicast address
2464 * [RFC 2461, sec 8.2]
2466 sip6
= mtod(m0
, struct ip6_hdr
*);
2467 bzero(&src_sa
, sizeof(src_sa
));
2468 src_sa
.sin6_family
= AF_INET6
;
2469 src_sa
.sin6_len
= sizeof(src_sa
);
2470 src_sa
.sin6_addr
= sip6
->ip6_src
;
2471 /* we don't currently use sin6_scope_id, but eventually use it */
2472 src_sa
.sin6_scope_id
= in6_addr2scopeid(ifp
, &sip6
->ip6_src
);
2473 if (nd6_is_addr_neighbor(&src_sa
, ifp
, 0) == 0)
2475 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
))
2476 goto fail
; /* what should we do here? */
2479 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0))
2483 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2484 * we almost always ask for an mbuf cluster for simplicity.
2485 * (MHLEN < IPV6_MMTU is almost always true)
2487 #if IPV6_MMTU >= MCLBYTES
2488 # error assumption failed about IPV6_MMTU and MCLBYTES
2490 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
2491 if (m
&& IPV6_MMTU
>= MHLEN
)
2492 MCLGET(m
, M_DONTWAIT
);
2495 m
->m_pkthdr
.rcvif
= NULL
;
2497 maxlen
= M_TRAILINGSPACE(m
);
2498 maxlen
= min(IPV6_MMTU
, maxlen
);
2499 /* just for safety */
2500 if (maxlen
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) +
2501 ((sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7)) {
2506 /* get ip6 linklocal address for ifp(my outgoing interface). */
2507 struct in6_ifaddr
*ia
;
2508 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
2510 IN6_IFF_ANYCAST
)) == NULL
)
2512 ifp_ll6
= ia
->ia_addr
.sin6_addr
;
2513 ifafree(&ia
->ia_ifa
);
2516 /* get ip6 linklocal address for the router. */
2517 if (rt
->rt_gateway
&& (rt
->rt_flags
& RTF_GATEWAY
)) {
2518 struct sockaddr_in6
*sin6
;
2519 sin6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
2520 router_ll6
= &sin6
->sin6_addr
;
2521 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6
))
2522 router_ll6
= (struct in6_addr
*)NULL
;
2524 router_ll6
= (struct in6_addr
*)NULL
;
2527 ip6
= mtod(m
, struct ip6_hdr
*);
2529 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2530 ip6
->ip6_vfc
|= IPV6_VERSION
;
2531 /* ip6->ip6_plen will be set later */
2532 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2533 ip6
->ip6_hlim
= 255;
2534 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2535 bcopy(&ifp_ll6
, &ip6
->ip6_src
, sizeof(struct in6_addr
));
2536 bcopy(&sip6
->ip6_src
, &ip6
->ip6_dst
, sizeof(struct in6_addr
));
2539 nd_rd
= (struct nd_redirect
*)(ip6
+ 1);
2540 nd_rd
->nd_rd_type
= ND_REDIRECT
;
2541 nd_rd
->nd_rd_code
= 0;
2542 nd_rd
->nd_rd_reserved
= 0;
2543 if (rt
->rt_flags
& RTF_GATEWAY
) {
2545 * nd_rd->nd_rd_target must be a link-local address in
2546 * better router cases.
2550 bcopy(router_ll6
, &nd_rd
->nd_rd_target
,
2551 sizeof(nd_rd
->nd_rd_target
));
2552 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2553 sizeof(nd_rd
->nd_rd_dst
));
2555 /* make sure redtgt == reddst */
2556 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_target
,
2557 sizeof(nd_rd
->nd_rd_target
));
2558 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2559 sizeof(nd_rd
->nd_rd_dst
));
2562 p
= (u_char
*)(nd_rd
+ 1);
2568 /* target lladdr option */
2569 struct rtentry
*rt_router
= NULL
;
2571 struct sockaddr_dl
*sdl
;
2572 struct nd_opt_hdr
*nd_opt
;
2575 rt_router
= nd6_lookup(router_ll6
, 0, ifp
, 0);
2578 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2579 len
= (len
+ 7) & ~7; /* round by 8 */
2581 if (len
+ (p
- (u_char
*)ip6
) > maxlen
)
2583 if (!(rt_router
->rt_flags
& RTF_GATEWAY
) &&
2584 (rt_router
->rt_flags
& RTF_LLINFO
) &&
2585 (rt_router
->rt_gateway
->sa_family
== AF_LINK
) &&
2586 (sdl
= (struct sockaddr_dl
*)rt_router
->rt_gateway
) &&
2588 nd_opt
= (struct nd_opt_hdr
*)p
;
2589 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
2590 nd_opt
->nd_opt_len
= len
>> 3;
2591 lladdr
= (char *)(nd_opt
+ 1);
2592 bcopy(LLADDR(sdl
), lladdr
, ifp
->if_addrlen
);
2598 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2600 /* just to be safe */
2601 #ifdef M_DECRYPTED /*not openbsd*/
2602 if (m0
->m_flags
& M_DECRYPTED
)
2605 if (p
- (u_char
*)ip6
> maxlen
)
2609 /* redirected header option */
2611 struct nd_opt_rd_hdr
*nd_opt_rh
;
2614 * compute the maximum size for icmp6 redirect header option.
2615 * XXX room for auth header?
2617 len
= maxlen
- (p
- (u_char
*)ip6
);
2620 /* This is just for simplicity. */
2621 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2623 m_freem(m0
->m_next
);
2626 m0
->m_pkthdr
.len
= m0
->m_len
;
2630 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2631 * about padding/truncate rule for the original IP packet.
2632 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2633 * - "attach as much as possible" is the goal
2634 * - pad if not aligned (original size can be guessed by original
2636 * Following code adds the padding if it is simple enough,
2637 * and truncates if not.
2639 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
)
2640 panic("assumption failed in %s:%d\n", __FILE__
, __LINE__
);
2642 if (len
- sizeof(*nd_opt_rh
) < m0
->m_pkthdr
.len
) {
2643 /* not enough room, truncate */
2644 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2646 /* enough room, pad or truncate */
2649 extra
= m0
->m_pkthdr
.len
% 8;
2651 /* pad if easy enough, truncate if not */
2652 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2654 m0
->m_len
+= (8 - extra
);
2655 m0
->m_pkthdr
.len
+= (8 - extra
);
2658 m0
->m_pkthdr
.len
-= extra
;
2662 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2663 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2666 nd_opt_rh
= (struct nd_opt_rd_hdr
*)p
;
2667 bzero(nd_opt_rh
, sizeof(*nd_opt_rh
));
2668 nd_opt_rh
->nd_opt_rh_type
= ND_OPT_REDIRECTED_HEADER
;
2669 nd_opt_rh
->nd_opt_rh_len
= len
>> 3;
2670 p
+= sizeof(*nd_opt_rh
);
2671 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2673 /* connect m0 to m */
2675 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
2679 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_src
))
2680 sip6
->ip6_src
.s6_addr16
[1] = 0;
2681 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_dst
))
2682 sip6
->ip6_dst
.s6_addr16
[1] = 0;
2684 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_src
))
2685 ip6
->ip6_src
.s6_addr16
[1] = 0;
2686 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_dst
))
2687 ip6
->ip6_dst
.s6_addr16
[1] = 0;
2689 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_target
))
2690 nd_rd
->nd_rd_target
.s6_addr16
[1] = 0;
2691 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_dst
))
2692 nd_rd
->nd_rd_dst
.s6_addr16
[1] = 0;
2694 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
2696 nd_rd
->nd_rd_cksum
= 0;
2698 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
2700 /* send the packet to outside... */
2702 /* Don't lookup socket */
2703 if (ipsec_bypass
== 0)
2704 (void)ipsec_setsocket(m
, NULL
);
2706 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
, 0);
2708 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
2709 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
2711 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
2723 #define sotoin6pcb sotoinpcb
2724 #define in6pcb inpcb
2725 #define in6p_icmp6filt inp_icmp6filt
2728 * ICMPv6 socket option processing.
2731 icmp6_ctloutput(so
, sopt
)
2733 struct sockopt
*sopt
;
2737 struct inpcb
*inp
= sotoinpcb(so
);
2738 int level
, op
, optname
;
2741 level
= sopt
->sopt_level
;
2742 op
= sopt
->sopt_dir
;
2743 optname
= sopt
->sopt_name
;
2744 optlen
= sopt
->sopt_valsize
;
2746 level
= op
= optname
= optlen
= 0;
2748 if (level
!= IPPROTO_ICMPV6
) {
2757 struct icmp6_filter
*p
;
2759 if (optlen
!= sizeof(*p
)) {
2763 if (inp
->in6p_icmp6filt
== NULL
) {
2767 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
2773 error
= ENOPROTOOPT
;
2782 if (inp
->in6p_icmp6filt
== NULL
) {
2786 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
2787 sizeof(struct icmp6_filter
));
2792 error
= ENOPROTOOPT
;
2803 #undef in6p_icmp6filt
2807 * ICMPv6 socket datagram option processing.
2810 icmp6_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2812 if (so
->so_uid
== 0)
2813 return icmp6_ctloutput(so
, sopt
);
2815 if (sopt
->sopt_level
== IPPROTO_ICMPV6
) {
2816 switch (sopt
->sopt_name
) {
2818 return icmp6_ctloutput(so
, sopt
);
2824 if (sopt
->sopt_level
!= IPPROTO_IPV6
)
2827 switch (sopt
->sopt_name
) {
2828 case IPV6_PKTOPTIONS
:
2829 case IPV6_UNICAST_HOPS
:
2838 case IPV6_MULTICAST_IF
:
2839 case IPV6_MULTICAST_HOPS
:
2840 case IPV6_MULTICAST_LOOP
:
2841 case IPV6_JOIN_GROUP
:
2842 case IPV6_LEAVE_GROUP
:
2843 case IPV6_PORTRANGE
:
2844 case IPV6_IPSEC_POLICY
:
2845 return ip6_ctloutput(so
, sopt
);
2854 __private_extern__
int
2855 icmp6_dgram_send(struct socket
*so
, __unused
int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
2856 struct mbuf
*control
, __unused
struct proc
*p
)
2859 struct inpcb
*inp
= sotoinpcb(so
);
2860 struct sockaddr_in6 tmp
;
2861 struct sockaddr_in6
*dst
;
2862 struct icmp6_hdr
*icmp6
;
2864 if (so
->so_uid
== 0)
2865 return rip6_output(m
, so
, (struct sockaddr_in6
*) nam
, control
);
2867 /* always copy sockaddr to avoid overwrites */
2868 if (so
->so_state
& SS_ISCONNECTED
) {
2874 bzero(&tmp
, sizeof(tmp
));
2875 tmp
.sin6_family
= AF_INET6
;
2876 tmp
.sin6_len
= sizeof(struct sockaddr_in6
);
2877 bcopy(&inp
->in6p_faddr
, &tmp
.sin6_addr
,
2878 sizeof(struct in6_addr
));
2885 tmp
= *(struct sockaddr_in6
*)nam
;
2890 * For an ICMPv6 packet, we should know its type and code
2892 if (so
->so_proto
->pr_protocol
== IPPROTO_ICMPV6
) {
2893 if (m
->m_len
< sizeof(struct icmp6_hdr
) &&
2894 (m
= m_pullup(m
, sizeof(struct icmp6_hdr
))) == NULL
) {
2898 icmp6
= mtod(m
, struct icmp6_hdr
*);
2901 * Allow only to send echo request type 128 with code 0
2902 * See RFC 2463 for Echo Request Message format
2904 if (icmp6
->icmp6_type
!= 128 || icmp6
->icmp6_code
!= 0) {
2910 #if ENABLE_DEFAULT_SCOPE
2911 if (dst
->sin6_scope_id
== 0) { /* not change if specified */
2912 dst
->sin6_scope_id
= scope6_addr2default(&dst
->sin6_addr
);
2916 return rip6_output(m
, so
, (struct sockaddr_in6
*) nam
, control
);
2922 /* Like rip6_attach but without root privilege enforcement */
2923 __private_extern__
int
2924 icmp6_dgram_attach(struct socket
*so
, int proto
, struct proc
*p
)
2929 inp
= sotoinpcb(so
);
2931 panic("icmp6_dgram_attach");
2933 if (proto
!= IPPROTO_ICMPV6
)
2936 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
2939 error
= in_pcballoc(so
, &ripcbinfo
, p
);
2942 inp
= (struct inpcb
*)so
->so_pcb
;
2943 inp
->inp_vflag
|= INP_IPV6
;
2944 inp
->in6p_ip6_nxt
= IPPROTO_ICMPV6
;
2945 inp
->in6p_hops
= -1; /* use kernel default */
2946 inp
->in6p_cksum
= -1;
2947 MALLOC(inp
->in6p_icmp6filt
, struct icmp6_filter
*,
2948 sizeof(struct icmp6_filter
), M_PCB
, M_WAITOK
);
2949 if (inp
->in6p_icmp6filt
== NULL
)
2951 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
2957 #ifndef HAVE_PPSRATECHECK
2959 #define timersub(tvp, uvp, vvp) \
2961 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2962 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2963 if ((vvp)->tv_usec < 0) { \
2965 (vvp)->tv_usec += 1000000; \
2971 * ppsratecheck(): packets (or events) per second limitation.
2974 ppsratecheck(lasttime
, curpps
, maxpps
)
2975 struct timeval
*lasttime
;
2977 int maxpps
; /* maximum pps allowed */
2979 struct timeval tv
, delta
;
2984 timersub(&tv
, lasttime
, &delta
);
2987 * Check for 0,0 so that the message will be seen at least once.
2988 * If more than one second has passed since the last update of
2989 * lasttime, reset the counter.
2991 * we do increment *curpps even in *curpps < maxpps case, as some may
2992 * try to use *curpps for stat purposes as well.
2994 if ((lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0) ||
2995 delta
.tv_sec
>= 1) {
2999 } else if (maxpps
< 0)
3001 else if (*curpps
< maxpps
)
3006 #if 1 /* DIAGNOSTIC? */
3007 /* be careful about wrap-around */
3008 if (*curpps
+ 1 > 0)
3009 *curpps
= *curpps
+ 1;
3012 * assume that there's not too many calls to this function.
3013 * not sure if the assumption holds, as it depends on *caller's*
3014 * behavior, not the behavior of this function.
3015 * IMHO it is wrong to make assumption on the caller's behavior,
3016 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
3018 *curpps
= *curpps
+ 1;
3026 * Perform rate limit check.
3027 * Returns 0 if it is okay to send the icmp6 packet.
3028 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
3031 * XXX per-destination/type check necessary?
3035 __unused
const struct in6_addr
*dst
, /* not used at this moment */
3036 __unused
const int type
, /* not used at this moment */
3037 __unused
const int code
) /* not used at this moment */
3041 ret
= 0; /* okay to send */
3044 if (!ppsratecheck(&icmp6errppslim_last
, &icmp6errpps_count
,
3046 /* The packet is subject to rate limit */