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
;
126 static void icmp6_errcount(struct icmp6errstat
*, int, int);
127 static int icmp6_rip6_input(struct mbuf
**, int);
128 static int icmp6_ratelimit(const struct in6_addr
*, const int, const int);
129 static const char *icmp6_redirect_diag(struct in6_addr
*,
130 struct in6_addr
*, struct in6_addr
*);
131 #ifndef HAVE_PPSRATECHECK
132 static int ppsratecheck(struct timeval
*, int *, int);
134 static struct mbuf
*ni6_input(struct mbuf
*, int);
135 static struct mbuf
*ni6_nametodns(const char *, int, int);
136 static int ni6_dnsmatch(const char *, int, const char *, int);
137 static int ni6_addrs(struct icmp6_nodeinfo
*,
138 struct ifnet
**, char *);
139 static int ni6_store_addrs(struct icmp6_nodeinfo
*, struct icmp6_nodeinfo
*,
140 struct ifnet
*, int);
141 static int icmp6_notify_error(struct mbuf
*, int, int, int);
143 #ifdef COMPAT_RFC1885
144 static struct route_in6 icmp6_reflect_rt
;
155 icmp6_errcount(stat
, type
, code
)
156 struct icmp6errstat
*stat
;
160 case ICMP6_DST_UNREACH
:
162 case ICMP6_DST_UNREACH_NOROUTE
:
163 stat
->icp6errs_dst_unreach_noroute
++;
165 case ICMP6_DST_UNREACH_ADMIN
:
166 stat
->icp6errs_dst_unreach_admin
++;
168 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
169 stat
->icp6errs_dst_unreach_beyondscope
++;
171 case ICMP6_DST_UNREACH_ADDR
:
172 stat
->icp6errs_dst_unreach_addr
++;
174 case ICMP6_DST_UNREACH_NOPORT
:
175 stat
->icp6errs_dst_unreach_noport
++;
179 case ICMP6_PACKET_TOO_BIG
:
180 stat
->icp6errs_packet_too_big
++;
182 case ICMP6_TIME_EXCEEDED
:
184 case ICMP6_TIME_EXCEED_TRANSIT
:
185 stat
->icp6errs_time_exceed_transit
++;
187 case ICMP6_TIME_EXCEED_REASSEMBLY
:
188 stat
->icp6errs_time_exceed_reassembly
++;
192 case ICMP6_PARAM_PROB
:
194 case ICMP6_PARAMPROB_HEADER
:
195 stat
->icp6errs_paramprob_header
++;
197 case ICMP6_PARAMPROB_NEXTHEADER
:
198 stat
->icp6errs_paramprob_nextheader
++;
200 case ICMP6_PARAMPROB_OPTION
:
201 stat
->icp6errs_paramprob_option
++;
206 stat
->icp6errs_redirect
++;
209 stat
->icp6errs_unknown
++;
213 * Generate an error packet of type error in response to bad IP6 packet.
216 icmp6_error(m
, type
, code
, param
)
218 int type
, code
, param
;
220 struct ip6_hdr
*oip6
, *nip6
;
221 struct icmp6_hdr
*icmp6
;
226 icmp6stat
.icp6s_error
++;
228 lck_mtx_assert(ip6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
229 /* count per-type-code statistics */
230 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, type
, code
);
232 #ifdef M_DECRYPTED /*not openbsd*/
233 if (m
->m_flags
& M_DECRYPTED
) {
234 icmp6stat
.icp6s_canterror
++;
239 #ifndef PULLDOWN_TEST
240 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return);
242 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
243 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
248 oip6
= mtod(m
, struct ip6_hdr
*);
251 * Multicast destination check. For unrecognized option errors,
252 * this check has already done in ip6_unknown_opt(), so we can
253 * check only for other errors.
255 if ((m
->m_flags
& (M_BCAST
|M_MCAST
) ||
256 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_dst
)) &&
257 (type
!= ICMP6_PACKET_TOO_BIG
&&
258 (type
!= ICMP6_PARAM_PROB
||
259 code
!= ICMP6_PARAMPROB_OPTION
)))
262 /* Source address check. XXX: the case of anycast source? */
263 if (IN6_IS_ADDR_UNSPECIFIED(&oip6
->ip6_src
) ||
264 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_src
))
268 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
272 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
273 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
274 struct icmp6_hdr
*icp
;
276 #ifndef PULLDOWN_TEST
277 IP6_EXTHDR_CHECK(m
, 0, off
+ sizeof(struct icmp6_hdr
), return);
278 icp
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
280 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
283 icmp6stat
.icp6s_tooshort
++;
287 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
288 icp
->icmp6_type
== ND_REDIRECT
) {
291 * Special case: for redirect (which is
292 * informational) we must not send icmp6 error.
294 icmp6stat
.icp6s_canterror
++;
297 /* ICMPv6 informational - send the error */
300 /* non-ICMPv6 - send the error */
303 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
305 /* Finally, do rate limitation check. */
306 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
307 icmp6stat
.icp6s_toofreq
++;
312 * OK, ICMP6 can be generated.
315 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
)
316 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
318 preplen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
319 M_PREPEND(m
, preplen
, M_DONTWAIT
);
320 if (m
&& m
->m_len
< preplen
)
321 m
= m_pullup(m
, preplen
);
323 nd6log((LOG_DEBUG
, "ENOBUFS in icmp6_error %d\n", __LINE__
));
327 nip6
= mtod(m
, struct ip6_hdr
*);
328 nip6
->ip6_src
= oip6
->ip6_src
;
329 nip6
->ip6_dst
= oip6
->ip6_dst
;
331 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
332 oip6
->ip6_src
.s6_addr16
[1] = 0;
333 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
334 oip6
->ip6_dst
.s6_addr16
[1] = 0;
336 icmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
337 icmp6
->icmp6_type
= type
;
338 icmp6
->icmp6_code
= code
;
339 icmp6
->icmp6_pptr
= htonl((u_int32_t
)param
);
342 * icmp6_reflect() is designed to be in the input path.
343 * icmp6_error() can be called from both input and outut path,
344 * and if we are in output path rcvif could contain bogus value.
345 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
346 * information in ip header (nip6).
348 m
->m_pkthdr
.rcvif
= NULL
;
350 icmp6stat
.icp6s_outhist
[type
]++;
351 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /* header order: IPv6 - ICMPv6 */
357 * If we can't tell wheter or not we can generate ICMP6, free it.
363 * Process a received ICMP6 message.
366 icmp6_input(mp
, offp
)
370 struct mbuf
*m
= *mp
, *n
;
371 struct ip6_hdr
*ip6
, *nip6
;
372 struct icmp6_hdr
*icmp6
, *nicmp6
;
374 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
377 #ifndef PULLDOWN_TEST
378 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_hdr
), return IPPROTO_DONE
);
379 /* m might change if M_LOOP. So, call mtod after this */
383 * Locate icmp6 structure in mbuf, and check
384 * that not corrupted and of at least minimum length
387 ip6
= mtod(m
, struct ip6_hdr
*);
388 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
389 icmp6stat
.icp6s_tooshort
++;
394 * calculate the checksum
396 #ifndef PULLDOWN_TEST
397 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
399 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
401 icmp6stat
.icp6s_tooshort
++;
405 code
= icmp6
->icmp6_code
;
407 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
409 "ICMP6 checksum error(%d|%x) %s\n",
410 icmp6
->icmp6_type
, sum
, ip6_sprintf(&ip6
->ip6_src
)));
411 icmp6stat
.icp6s_checksum
++;
415 #if defined(NFAITH) && 0 < NFAITH
416 if (faithprefix(&ip6
->ip6_dst
)) {
418 * Deliver very specific ICMP6 type only.
419 * This is important to deilver TOOBIG. Otherwise PMTUD
422 switch (icmp6
->icmp6_type
) {
423 case ICMP6_DST_UNREACH
:
424 case ICMP6_PACKET_TOO_BIG
:
425 case ICMP6_TIME_EXCEEDED
:
433 icmp6stat
.icp6s_inhist
[icmp6
->icmp6_type
]++;
434 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_msg
);
435 if (icmp6
->icmp6_type
< ICMP6_INFOMSG_MASK
)
436 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_error
);
438 switch (icmp6
->icmp6_type
) {
439 case ICMP6_DST_UNREACH
:
440 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
442 case ICMP6_DST_UNREACH_NOROUTE
:
443 code
= PRC_UNREACH_NET
;
445 case ICMP6_DST_UNREACH_ADMIN
:
446 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_adminprohib
);
447 code
= PRC_UNREACH_PROTOCOL
; /* is this a good code? */
449 case ICMP6_DST_UNREACH_ADDR
:
452 #ifdef COMPAT_RFC1885
453 case ICMP6_DST_UNREACH_NOTNEIGHBOR
:
454 code
= PRC_UNREACH_SRCFAIL
;
457 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
458 /* I mean "source address was incorrect." */
459 code
= PRC_PARAMPROB
;
462 case ICMP6_DST_UNREACH_NOPORT
:
463 code
= PRC_UNREACH_PORT
;
471 case ICMP6_PACKET_TOO_BIG
:
472 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
479 * Updating the path MTU will be done after examining
480 * intermediate extension headers.
485 case ICMP6_TIME_EXCEEDED
:
486 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
488 case ICMP6_TIME_EXCEED_TRANSIT
:
489 case ICMP6_TIME_EXCEED_REASSEMBLY
:
490 code
+= PRC_TIMXCEED_INTRANS
;
498 case ICMP6_PARAM_PROB
:
499 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
501 case ICMP6_PARAMPROB_NEXTHEADER
:
502 code
= PRC_UNREACH_PROTOCOL
;
504 case ICMP6_PARAMPROB_HEADER
:
505 case ICMP6_PARAMPROB_OPTION
:
506 code
= PRC_PARAMPROB
;
514 case ICMP6_ECHO_REQUEST
:
515 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
518 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
522 if ((n
->m_flags
& M_EXT
) != 0
523 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
525 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
528 * Prepare an internal mbuf. m_pullup() doesn't
529 * always copy the length we specified.
531 if (maxlen
>= MCLBYTES
) {
536 MGETHDR(n
, M_DONTWAIT
, n0
->m_type
); /* MAC-OK */
537 if (n
&& maxlen
>= MHLEN
) {
538 MCLGET(n
, M_DONTWAIT
);
539 if ((n
->m_flags
& M_EXT
) == 0) {
549 M_COPY_PKTHDR(n
, n0
);
551 * Copy IPv6 and ICMPv6 only.
553 nip6
= mtod(n
, struct ip6_hdr
*);
554 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
555 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
556 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
557 noff
= sizeof(struct ip6_hdr
);
558 n
->m_pkthdr
.len
= n
->m_len
=
559 noff
+ sizeof(struct icmp6_hdr
);
561 * Adjust mbuf. ip6_plen will be adjusted in
564 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
565 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
567 n0
->m_flags
&= ~M_PKTHDR
;
569 nip6
= mtod(n
, struct ip6_hdr
*);
570 IP6_EXTHDR_GET(nicmp6
, struct icmp6_hdr
*, n
, off
,
574 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
575 nicmp6
->icmp6_code
= 0;
577 icmp6stat
.icp6s_reflect
++;
578 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
579 icmp6_reflect(n
, noff
);
583 case ICMP6_ECHO_REPLY
:
584 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
589 case MLD6_LISTENER_QUERY
:
590 case MLD6_LISTENER_REPORT
:
591 if (icmp6len
< sizeof(struct mld6_hdr
))
593 if (icmp6
->icmp6_type
== MLD6_LISTENER_QUERY
) /* XXX: ugly... */
594 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
596 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
597 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
607 case MLD6_LISTENER_DONE
:
608 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
609 if (icmp6len
< sizeof(struct mld6_hdr
)) /* necessary? */
611 break; /* nothing to be done in kernel */
613 case MLD6_MTRACE_RESP
:
615 /* XXX: these two are experimental. not officially defind. */
616 /* XXX: per-interface statistics? */
617 break; /* just pass it to applications */
623 /* By RFC 4620 refuse to answer queries from global scope addresses */
624 if ((icmp6_nodeinfo
& 8) != 8 && in6_addrscope(&ip6
->ip6_src
) == IPV6_ADDR_SCOPE_GLOBAL
)
627 if (icmp6len
< sizeof(struct icmp6_nodeinfo
))
630 #ifndef PULLDOWN_TEST
631 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
632 return IPPROTO_DONE
);
634 n
= m_copy(m
, 0, M_COPYALL
);
636 n
= ni6_input(n
, off
);
638 noff
= sizeof(struct ip6_hdr
);
639 icmp6stat
.icp6s_reflect
++;
640 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
641 icmp6_reflect(n
, noff
);
650 case ND_ROUTER_SOLICIT
:
651 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
654 if (icmp6len
< sizeof(struct nd_router_solicit
))
656 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
658 nd6_rs_input(m
, off
, icmp6len
);
662 nd6_rs_input(n
, off
, icmp6len
);
666 case ND_ROUTER_ADVERT
:
667 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
670 if (icmp6len
< sizeof(struct nd_router_advert
))
672 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
674 nd6_ra_input(m
, off
, icmp6len
);
678 nd6_ra_input(n
, off
, icmp6len
);
682 case ND_NEIGHBOR_SOLICIT
:
683 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
686 if (icmp6len
< sizeof(struct nd_neighbor_solicit
))
688 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
690 nd6_ns_input(m
, off
, icmp6len
);
694 nd6_ns_input(n
, off
, icmp6len
);
698 case ND_NEIGHBOR_ADVERT
:
699 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
702 if (icmp6len
< sizeof(struct nd_neighbor_advert
))
704 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
706 nd6_na_input(m
, off
, icmp6len
);
710 nd6_na_input(n
, off
, icmp6len
);
715 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
718 if (icmp6len
< sizeof(struct nd_redirect
))
720 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
722 icmp6_redirect_input(m
, off
);
726 icmp6_redirect_input(n
, off
);
730 case ICMP6_ROUTER_RENUMBERING
:
731 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
732 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
)
734 if (icmp6len
< sizeof(struct icmp6_router_renum
))
740 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
741 icmp6
->icmp6_type
, ip6_sprintf(&ip6
->ip6_src
),
742 ip6_sprintf(&ip6
->ip6_dst
),
743 m
->m_pkthdr
.rcvif
? m
->m_pkthdr
.rcvif
->if_index
: 0));
744 if (icmp6
->icmp6_type
< ICMP6_ECHO_REQUEST
) {
745 /* ICMPv6 error: MUST deliver it by spec... */
749 /* ICMPv6 informational: MUST not deliver */
753 if (icmp6_notify_error(m
, off
, icmp6len
, code
)) {
754 /* In this case, m should've been freed. */
755 return(IPPROTO_DONE
);
760 icmp6stat
.icp6s_badcode
++;
764 icmp6stat
.icp6s_badlen
++;
768 /* deliver the packet to appropriate sockets */
769 icmp6_rip6_input(&m
, *offp
);
779 icmp6_notify_error(m
, off
, icmp6len
, code
)
781 int off
, icmp6len
, code
;
783 struct icmp6_hdr
*icmp6
;
784 struct ip6_hdr
*eip6
;
786 struct sockaddr_in6 icmp6src
, icmp6dst
;
788 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
789 icmp6stat
.icp6s_tooshort
++;
792 #ifndef PULLDOWN_TEST
793 IP6_EXTHDR_CHECK(m
, off
,
794 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
796 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
798 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
799 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
801 icmp6stat
.icp6s_tooshort
++;
805 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
807 /* Detect the upper level protocol */
809 void (*ctlfunc
)(int, struct sockaddr
*, void *);
810 u_int8_t nxt
= eip6
->ip6_nxt
;
811 int eoff
= off
+ sizeof(struct icmp6_hdr
) +
812 sizeof(struct ip6_hdr
);
813 struct ip6ctlparam ip6cp
;
814 struct in6_addr
*finaldst
= NULL
;
815 int icmp6type
= icmp6
->icmp6_type
;
817 struct ip6_rthdr
*rth
;
818 struct ip6_rthdr0
*rth0
;
821 while (1) { /* XXX: should avoid infinite loop explicitly? */
825 case IPPROTO_HOPOPTS
:
826 case IPPROTO_DSTOPTS
:
828 #ifndef PULLDOWN_TEST
829 IP6_EXTHDR_CHECK(m
, 0, eoff
+
830 sizeof(struct ip6_ext
),
832 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
835 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
838 icmp6stat
.icp6s_tooshort
++;
843 if (nxt
== IPPROTO_AH
)
844 eoff
+= (eh
->ip6e_len
+ 2) << 2;
846 eoff
+= (eh
->ip6e_len
+ 1) << 3;
849 case IPPROTO_ROUTING
:
851 * When the erroneous packet contains a
852 * routing header, we should examine the
853 * header to determine the final destination.
854 * Otherwise, we can't properly update
855 * information that depends on the final
856 * destination (e.g. path MTU).
858 #ifndef PULLDOWN_TEST
859 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
861 rth
= (struct ip6_rthdr
*)(mtod(m
, caddr_t
)
864 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
867 icmp6stat
.icp6s_tooshort
++;
871 rthlen
= (rth
->ip6r_len
+ 1) << 3;
873 * XXX: currently there is no
874 * officially defined type other
876 * Note that if the segment left field
877 * is 0, all intermediate hops must
880 if (rth
->ip6r_segleft
&&
881 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
884 #ifndef PULLDOWN_TEST
885 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
887 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
890 struct ip6_rthdr0
*, m
,
893 icmp6stat
.icp6s_tooshort
++;
897 /* just ignore a bogus header */
898 if ((rth0
->ip6r0_len
% 2) == 0 &&
899 (hops
= rth0
->ip6r0_len
/2))
900 finaldst
= (struct in6_addr
*)(rth0
+ 1) + (hops
- 1);
905 case IPPROTO_FRAGMENT
:
906 #ifndef PULLDOWN_TEST
907 IP6_EXTHDR_CHECK(m
, 0, eoff
+
908 sizeof(struct ip6_frag
),
910 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
913 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
916 icmp6stat
.icp6s_tooshort
++;
921 * Data after a fragment header is meaningless
922 * unless it is the first fragment, but
923 * we'll go to the notify label for path MTU
926 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
)
929 eoff
+= sizeof(struct ip6_frag
);
934 * This case includes ESP and the No Next
935 * Header. In such cases going to the notify
936 * label does not have any meaning
937 * (i.e. ctlfunc will be NULL), but we go
938 * anyway since we might have to update
939 * path MTU information.
945 #ifndef PULLDOWN_TEST
946 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
948 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
949 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
951 icmp6stat
.icp6s_tooshort
++;
956 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
957 bzero(&icmp6dst
, sizeof(icmp6dst
));
958 icmp6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
959 icmp6dst
.sin6_family
= AF_INET6
;
960 if (finaldst
== NULL
)
961 icmp6dst
.sin6_addr
= eip6
->ip6_dst
;
963 icmp6dst
.sin6_addr
= *finaldst
;
964 icmp6dst
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
965 &icmp6dst
.sin6_addr
);
966 #ifndef SCOPEDROUTING
967 if (in6_embedscope(&icmp6dst
.sin6_addr
, &icmp6dst
,
969 /* should be impossbile */
971 "icmp6_notify_error: in6_embedscope failed\n"));
977 * retrieve parameters from the inner IPv6 header, and convert
978 * them into sockaddr structures.
980 bzero(&icmp6src
, sizeof(icmp6src
));
981 icmp6src
.sin6_len
= sizeof(struct sockaddr_in6
);
982 icmp6src
.sin6_family
= AF_INET6
;
983 icmp6src
.sin6_addr
= eip6
->ip6_src
;
984 icmp6src
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
985 &icmp6src
.sin6_addr
);
986 #ifndef SCOPEDROUTING
987 if (in6_embedscope(&icmp6src
.sin6_addr
, &icmp6src
,
989 /* should be impossbile */
991 "icmp6_notify_error: in6_embedscope failed\n"));
995 icmp6src
.sin6_flowinfo
=
996 (eip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
);
998 if (finaldst
== NULL
)
999 finaldst
= &eip6
->ip6_dst
;
1001 ip6cp
.ip6c_icmp6
= icmp6
;
1002 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1003 ip6cp
.ip6c_off
= eoff
;
1004 ip6cp
.ip6c_finaldst
= finaldst
;
1005 ip6cp
.ip6c_src
= &icmp6src
;
1006 ip6cp
.ip6c_nxt
= nxt
;
1008 if (icmp6type
== ICMP6_PACKET_TOO_BIG
) {
1009 notifymtu
= ntohl(icmp6
->icmp6_mtu
);
1010 ip6cp
.ip6c_cmdarg
= (void *)¬ifymtu
;
1011 icmp6_mtudisc_update(&ip6cp
, 1); /*XXX*/
1014 ctlfunc
= (void (*)(int, struct sockaddr
*, void *))
1015 (ip6_protox
[nxt
]->pr_ctlinput
);
1017 (void) (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6dst
,
1029 icmp6_mtudisc_update(ip6cp
, validated
)
1030 struct ip6ctlparam
*ip6cp
;
1033 struct in6_addr
*dst
= ip6cp
->ip6c_finaldst
;
1034 struct icmp6_hdr
*icmp6
= ip6cp
->ip6c_icmp6
;
1035 struct mbuf
*m
= ip6cp
->ip6c_m
; /* will be necessary for scope issue */
1036 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1037 struct rtentry
*rt
= NULL
;
1038 struct sockaddr_in6 sin6
;
1043 bzero(&sin6
, sizeof(sin6
));
1044 sin6
.sin6_family
= PF_INET6
;
1045 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1046 sin6
.sin6_addr
= *dst
;
1047 /* XXX normally, this won't happen */
1048 if (IN6_IS_ADDR_LINKLOCAL(dst
)) {
1049 sin6
.sin6_addr
.s6_addr16
[1] =
1050 htons(m
->m_pkthdr
.rcvif
->if_index
);
1052 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1053 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, RTF_CLONING
| RTF_PRCLONING
);
1055 if (rt
&& (rt
->rt_flags
& RTF_HOST
)
1056 && !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1057 if (mtu
< IPV6_MMTU
) {
1059 rt
->rt_rmx
.rmx_locks
|= RTV_MTU
;
1060 } else if (mtu
< rt
->rt_ifp
->if_mtu
&&
1061 rt
->rt_rmx
.rmx_mtu
> mtu
) {
1062 icmp6stat
.icp6s_pmtuchg
++;
1063 rt
->rt_rmx
.rmx_mtu
= mtu
;
1071 * Process a Node Information Query packet, based on
1072 * draft-ietf-ipngwg-icmp-name-lookups-07.
1074 * Spec incompatibilities:
1075 * - IPv6 Subject address handling
1076 * - IPv4 Subject address handling support missing
1077 * - Proxy reply (answer even if it's not for me)
1078 * - joins NI group address at in6_ifattach() time only, does not cope
1079 * with hostname changes by sethostname(3)
1081 #define hostnamelen strlen(hostname)
1082 static struct mbuf
*
1087 struct icmp6_nodeinfo
*ni6
, *nni6
;
1088 struct mbuf
*n
= NULL
;
1091 int replylen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1092 struct ni_reply_fqdn
*fqdn
;
1093 int addrs
; /* for NI_QTYPE_NODEADDR */
1094 struct ifnet
*ifp
= NULL
; /* for NI_QTYPE_NODEADDR */
1095 struct sockaddr_in6 sin6
; /* double meaning; ip6_dst and subjectaddr */
1096 struct sockaddr_in6 sin6_d
; /* XXX: we should retrieve this from m_aux */
1097 struct ip6_hdr
*ip6
;
1098 int oldfqdn
= 0; /* if 1, return pascal string (03 draft) */
1100 struct in6_ifaddr
*ia6
= NULL
;
1102 ip6
= mtod(m
, struct ip6_hdr
*);
1103 #ifndef PULLDOWN_TEST
1104 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1106 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1108 /* m is already reclaimed */
1114 * Validate IPv6 destination address.
1116 * The Responder must discard the Query without further processing
1117 * unless it is one of the Responder's unicast or anycast addresses, or
1118 * a link-local scope multicast address which the Responder has joined.
1119 * [icmp-name-lookups-07, Section 4.]
1121 bzero(&sin6
, sizeof(sin6
));
1122 sin6
.sin6_family
= AF_INET6
;
1123 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1124 bcopy(&ip6
->ip6_dst
, &sin6
.sin6_addr
, sizeof(sin6
.sin6_addr
));
1126 if ((ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr((struct sockaddr
*)&sin6
)) != NULL
) {
1127 /* unicast/anycast, fine */
1128 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1129 (icmp6_nodeinfo
& 4) == 0) {
1130 ifafree(&ia6
->ia_ifa
);
1132 nd6log((LOG_DEBUG
, "ni6_input: ignore node info to "
1133 "a temporary address in %s:%d",
1134 __FILE__
, __LINE__
));
1137 ifafree(&ia6
->ia_ifa
);
1139 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
))
1140 ; /* link-local multicast, fine */
1144 /* validate query Subject field. */
1145 qtype
= ntohs(ni6
->ni_qtype
);
1146 subjlen
= m
->m_pkthdr
.len
- off
- sizeof(struct icmp6_nodeinfo
);
1149 case NI_QTYPE_SUPTYPES
:
1151 if (ni6
->ni_code
== ICMP6_NI_SUBJ_FQDN
&& subjlen
== 0)
1155 case NI_QTYPE_NODEADDR
:
1156 switch (ni6
->ni_code
) {
1157 case ICMP6_NI_SUBJ_IPV6
:
1158 #if ICMP6_NI_SUBJ_IPV6 != 0
1162 * backward compatibility - try to accept 03 draft
1163 * format, where no Subject is present.
1165 if (qtype
== NI_QTYPE_FQDN
&& ni6
->ni_code
== 0 &&
1170 #if ICMP6_NI_SUBJ_IPV6 != 0
1171 if (ni6
->ni_code
!= ICMP6_NI_SUBJ_IPV6
)
1175 if (subjlen
!= sizeof(sin6
.sin6_addr
))
1179 * Validate Subject address.
1181 * Not sure what exactly "address belongs to the node"
1182 * means in the spec, is it just unicast, or what?
1184 * At this moment we consider Subject address as
1185 * "belong to the node" if the Subject address equals
1186 * to the IPv6 destination address; validation for
1187 * IPv6 destination address should have done enough
1190 * We do not do proxy at this moment.
1192 /* m_pulldown instead of copy? */
1193 m_copydata(m
, off
+ sizeof(struct icmp6_nodeinfo
),
1194 subjlen
, (caddr_t
)&sin6
.sin6_addr
);
1195 sin6
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1197 #ifndef SCOPEDROUTING
1198 in6_embedscope(&sin6
.sin6_addr
, &sin6
, NULL
, NULL
);
1200 bzero(&sin6_d
, sizeof(sin6_d
));
1201 sin6_d
.sin6_family
= AF_INET6
; /* not used, actually */
1202 sin6_d
.sin6_len
= sizeof(sin6_d
); /* ditto */
1203 sin6_d
.sin6_addr
= ip6
->ip6_dst
;
1204 sin6_d
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1206 #ifndef SCOPEDROUTING
1207 in6_embedscope(&sin6_d
.sin6_addr
, &sin6_d
, NULL
, NULL
);
1209 subj
= (char *)&sin6
;
1210 if (SA6_ARE_ADDR_EQUAL(&sin6
, &sin6_d
))
1214 * XXX if we are to allow other cases, we should really
1215 * be careful about scope here.
1216 * basically, we should disallow queries toward IPv6
1217 * destination X with subject Y, if scope(X) > scope(Y).
1218 * if we allow scope(X) > scope(Y), it will result in
1219 * information leakage across scope boundary.
1223 case ICMP6_NI_SUBJ_FQDN
:
1225 * Validate Subject name with gethostname(3).
1227 * The behavior may need some debate, since:
1228 * - we are not sure if the node has FQDN as
1229 * hostname (returned by gethostname(3)).
1230 * - the code does wildcard match for truncated names.
1231 * however, we are not sure if we want to perform
1232 * wildcard match, if gethostname(3) side has
1233 * truncated hostname.
1235 n
= ni6_nametodns(hostname
, hostnamelen
, 0);
1236 if (!n
|| n
->m_next
|| n
->m_len
== 0)
1238 IP6_EXTHDR_GET(subj
, char *, m
,
1239 off
+ sizeof(struct icmp6_nodeinfo
), subjlen
);
1242 if (!ni6_dnsmatch(subj
, subjlen
, mtod(n
, const char *),
1250 case ICMP6_NI_SUBJ_IPV4
: /* XXX: to be implemented? */
1257 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1260 if ((icmp6_nodeinfo
& 1) == 0)
1263 case NI_QTYPE_NODEADDR
:
1264 if ((icmp6_nodeinfo
& 2) == 0)
1269 /* guess reply length */
1272 break; /* no reply data */
1273 case NI_QTYPE_SUPTYPES
:
1274 replylen
+= sizeof(u_int32_t
);
1277 /* XXX will append an mbuf */
1278 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1280 case NI_QTYPE_NODEADDR
:
1281 addrs
= ni6_addrs(ni6
, &ifp
, subj
);
1282 if ((replylen
+= addrs
* (sizeof(struct in6_addr
) +
1283 sizeof(u_int32_t
))) > MCLBYTES
)
1284 replylen
= MCLBYTES
; /* XXX: will truncate pkt later */
1288 * XXX: We must return a reply with the ICMP6 code
1289 * `unknown Qtype' in this case. However we regard the case
1290 * as an FQDN query for backward compatibility.
1291 * Older versions set a random value to this field,
1292 * so it rarely varies in the defined qtypes.
1293 * But the mechanism is not reliable...
1294 * maybe we should obsolete older versions.
1296 qtype
= NI_QTYPE_FQDN
;
1297 /* XXX will append an mbuf */
1298 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1303 /* allocate an mbuf to reply. */
1304 MGETHDR(n
, M_DONTWAIT
, m
->m_type
); /* MAC-OK */
1309 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1310 if (replylen
> MHLEN
) {
1311 if (replylen
> MCLBYTES
) {
1313 * XXX: should we try to allocate more? But MCLBYTES
1314 * is probably much larger than IPV6_MMTU...
1318 MCLGET(n
, M_DONTWAIT
);
1319 if ((n
->m_flags
& M_EXT
) == 0) {
1323 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
1325 /* copy mbuf header and IPv6 + Node Information base headers */
1326 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), sizeof(struct ip6_hdr
));
1327 nni6
= (struct icmp6_nodeinfo
*)(mtod(n
, struct ip6_hdr
*) + 1);
1328 bcopy((caddr_t
)ni6
, (caddr_t
)nni6
, sizeof(struct icmp6_nodeinfo
));
1330 /* qtype dependent procedure */
1333 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1336 case NI_QTYPE_SUPTYPES
:
1339 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1340 nni6
->ni_flags
= htons(0x0000); /* raw bitmap */
1341 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1342 v
= (u_int32_t
)htonl(0x0000000f);
1343 bcopy(&v
, nni6
+ 1, sizeof(u_int32_t
));
1347 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1348 fqdn
= (struct ni_reply_fqdn
*)(mtod(n
, caddr_t
) +
1349 sizeof(struct ip6_hdr
) +
1350 sizeof(struct icmp6_nodeinfo
));
1351 nni6
->ni_flags
= 0; /* XXX: meaningless TTL */
1352 fqdn
->ni_fqdn_ttl
= 0; /* ditto. */
1354 * XXX do we really have FQDN in variable "hostname"?
1356 n
->m_next
= ni6_nametodns(hostname
, hostnamelen
, oldfqdn
);
1357 if (n
->m_next
== NULL
)
1359 /* XXX we assume that n->m_next is not a chain */
1360 if (n
->m_next
->m_next
!= NULL
)
1362 n
->m_pkthdr
.len
+= n
->m_next
->m_len
;
1364 case NI_QTYPE_NODEADDR
:
1368 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1369 n
->m_pkthdr
.len
= n
->m_len
=
1370 sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1371 lenlim
= M_TRAILINGSPACE(n
);
1372 copied
= ni6_store_addrs(ni6
, nni6
, ifp
, lenlim
);
1373 /* XXX: reset mbuf length */
1374 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
1375 sizeof(struct icmp6_nodeinfo
) + copied
;
1379 break; /* XXX impossible! */
1382 nni6
->ni_type
= ICMP6_NI_REPLY
;
1395 * make a mbuf with DNS-encoded string. no compression support.
1397 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1398 * treated as truncated name (two \0 at the end). this is a wild guess.
1400 static struct mbuf
*
1401 ni6_nametodns(name
, namelen
, old
)
1404 int old
; /* return pascal string if non-zero */
1416 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1417 MGET(m
, M_DONTWAIT
, MT_DATA
);
1418 if (m
&& len
> MLEN
) {
1419 MCLGET(m
, M_DONTWAIT
);
1420 if ((m
->m_flags
& M_EXT
) == 0)
1429 *mtod(m
, char *) = namelen
;
1430 bcopy(name
, mtod(m
, char *) + 1, namelen
);
1434 cp
= mtod(m
, char *);
1435 ep
= mtod(m
, char *) + M_TRAILINGSPACE(m
);
1437 /* if not certain about my name, return empty buffer */
1442 * guess if it looks like shortened hostname, or FQDN.
1443 * shortened hostname needs two trailing "\0".
1446 for (p
= name
; p
< name
+ namelen
; p
++) {
1447 if (*p
&& *p
== '.')
1456 while (cp
< ep
&& p
< name
+ namelen
) {
1458 for (q
= p
; q
< name
+ namelen
&& *q
&& *q
!= '.'; q
++)
1460 /* result does not fit into mbuf */
1461 if (cp
+ i
+ 1 >= ep
)
1464 * DNS label length restriction, RFC1035 page 8.
1465 * "i == 0" case is included here to avoid returning
1466 * 0-length label on "foo..bar".
1468 if (i
<= 0 || i
>= 64)
1474 if (p
< name
+ namelen
&& *p
== '.')
1478 if (cp
+ nterm
>= ep
)
1482 m
->m_len
= cp
- mtod(m
, char *);
1486 panic("should not reach here");
1496 * check if two DNS-encoded string matches. takes care of truncated
1497 * form (with \0\0 at the end). no compression support.
1498 * XXX upper/lowercase match (see RFC2065)
1501 ni6_dnsmatch(a
, alen
, b
, blen
)
1507 const char *a0
, *b0
;
1510 /* simplest case - need validation? */
1511 if (alen
== blen
&& bcmp(a
, b
, alen
) == 0)
1517 /* termination is mandatory */
1518 if (alen
< 2 || blen
< 2)
1520 if (a0
[alen
- 1] != '\0' || b0
[blen
- 1] != '\0')
1525 while (a
- a0
< alen
&& b
- b0
< blen
) {
1526 if (a
- a0
+ 1 > alen
|| b
- b0
+ 1 > blen
)
1529 if ((signed char)a
[0] < 0 || (signed char)b
[0] < 0)
1531 /* we don't support compression yet */
1532 if (a
[0] >= 64 || b
[0] >= 64)
1535 /* truncated case */
1536 if (a
[0] == 0 && a
- a0
== alen
- 1)
1538 if (b
[0] == 0 && b
- b0
== blen
- 1)
1540 if (a
[0] == 0 || b
[0] == 0)
1546 if (a
- a0
+ 1 + l
> alen
|| b
- b0
+ 1 + l
> blen
)
1548 if (bcmp(a
+ 1, b
+ 1, l
) != 0)
1555 if (a
- a0
== alen
&& b
- b0
== blen
)
1562 * calculate the number of addresses to be returned in the node info reply.
1565 ni6_addrs(ni6
, ifpp
, subj
)
1566 struct icmp6_nodeinfo
*ni6
;
1567 struct ifnet
**ifpp
;
1571 struct in6_ifaddr
*ifa6
;
1573 struct sockaddr_in6
*subj_ip6
= NULL
; /* XXX pedant */
1574 int addrs
= 0, addrsofif
, iffound
= 0;
1575 int niflags
= ni6
->ni_flags
;
1577 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0) {
1578 switch (ni6
->ni_code
) {
1579 case ICMP6_NI_SUBJ_IPV6
:
1580 if (subj
== NULL
) /* must be impossible... */
1582 subj_ip6
= (struct sockaddr_in6
*)subj
;
1586 * XXX: we only support IPv6 subject address for
1593 ifnet_head_lock_shared();
1594 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
1596 ifnet_lock_shared(ifp
);
1597 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1599 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1601 ifa6
= (struct in6_ifaddr
*)ifa
;
1603 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0 &&
1604 IN6_ARE_ADDR_EQUAL(&subj_ip6
->sin6_addr
,
1605 &ifa6
->ia_addr
.sin6_addr
))
1609 * IPv4-mapped addresses can only be returned by a
1610 * Node Information proxy, since they represent
1611 * addresses of IPv4-only nodes, which perforce do
1612 * not implement this protocol.
1613 * [icmp-name-lookups-07, Section 5.4]
1614 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1615 * this function at this moment.
1618 /* What do we have to do about ::1? */
1619 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1620 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1621 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1624 case IPV6_ADDR_SCOPE_SITELOCAL
:
1625 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1628 case IPV6_ADDR_SCOPE_GLOBAL
:
1629 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1637 * check if anycast is okay.
1638 * XXX: just experimental. not in the spec.
1640 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1641 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1642 continue; /* we need only unicast addresses */
1643 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1644 (icmp6_nodeinfo
& 4) == 0) {
1647 addrsofif
++; /* count the address */
1649 ifnet_lock_done(ifp
);
1664 ni6_store_addrs(ni6
, nni6
, ifp0
, resid
)
1665 struct icmp6_nodeinfo
*ni6
, *nni6
;
1669 struct ifnet
*ifp
= ifp0
;
1670 struct in6_ifaddr
*ifa6
;
1672 struct ifnet
*ifp_dep
= NULL
;
1673 int copied
= 0, allow_deprecated
= 0;
1674 u_char
*cp
= (u_char
*)(nni6
+ 1);
1675 int niflags
= ni6
->ni_flags
;
1677 struct timeval timenow
;
1679 getmicrotime(&timenow
);
1681 if (ifp0
== NULL
&& !(niflags
& NI_NODEADDR_FLAG_ALL
))
1682 return(0); /* needless to copy */
1686 ifnet_head_lock_shared();
1687 if (ifp
== NULL
) ifp
= TAILQ_FIRST(&ifnet_head
);
1689 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1690 ifnet_lock_shared(ifp
);
1691 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1692 ifa
= ifa
->ifa_list
.tqe_next
)
1694 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1696 ifa6
= (struct in6_ifaddr
*)ifa
;
1698 if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0 &&
1699 allow_deprecated
== 0) {
1701 * prefererred address should be put before
1702 * deprecated addresses.
1705 /* record the interface for later search */
1706 if (ifp_dep
== NULL
)
1711 else if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) == 0 &&
1712 allow_deprecated
!= 0)
1713 continue; /* we now collect deprecated addrs */
1715 /* What do we have to do about ::1? */
1716 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1717 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1718 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1721 case IPV6_ADDR_SCOPE_SITELOCAL
:
1722 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1725 case IPV6_ADDR_SCOPE_GLOBAL
:
1726 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1734 * check if anycast is okay.
1735 * XXX: just experimental. not in the spec.
1737 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1738 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1740 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1741 (icmp6_nodeinfo
& 4) == 0) {
1745 /* now we can copy the address */
1746 if (resid
< sizeof(struct in6_addr
) +
1747 sizeof(u_int32_t
)) {
1749 * We give up much more copy.
1750 * Set the truncate flag and return.
1753 NI_NODEADDR_FLAG_TRUNCATE
;
1754 ifnet_lock_done(ifp
);
1760 * Set the TTL of the address.
1761 * The TTL value should be one of the following
1762 * according to the specification:
1764 * 1. The remaining lifetime of a DHCP lease on the
1766 * 2. The remaining Valid Lifetime of a prefix from
1767 * which the address was derived through Stateless
1768 * Autoconfiguration.
1770 * Note that we currently do not support stateful
1771 * address configuration by DHCPv6, so the former
1772 * case can't happen.
1774 if (ifa6
->ia6_lifetime
.ia6t_expire
== 0)
1775 ltime
= ND6_INFINITE_LIFETIME
;
1777 if (ifa6
->ia6_lifetime
.ia6t_expire
>
1779 ltime
= htonl(ifa6
->ia6_lifetime
.ia6t_expire
- timenow
.tv_sec
);
1784 bcopy(<ime
, cp
, sizeof(u_int32_t
));
1785 cp
+= sizeof(u_int32_t
);
1787 /* copy the address itself */
1788 bcopy(&ifa6
->ia_addr
.sin6_addr
, cp
,
1789 sizeof(struct in6_addr
));
1790 /* XXX: KAME link-local hack; remove ifindex */
1791 if (IN6_IS_ADDR_LINKLOCAL(&ifa6
->ia_addr
.sin6_addr
))
1792 ((struct in6_addr
*)cp
)->s6_addr16
[1] = 0;
1793 cp
+= sizeof(struct in6_addr
);
1795 resid
-= (sizeof(struct in6_addr
) + sizeof(u_int32_t
));
1796 copied
+= (sizeof(struct in6_addr
) +
1799 ifnet_lock_done(ifp
);
1800 if (ifp0
) /* we need search only on the specified IF */
1805 if (allow_deprecated
== 0 && ifp_dep
!= NULL
) {
1807 allow_deprecated
= 1;
1816 * XXX almost dup'ed code with rip6_input.
1819 icmp6_rip6_input(mp
, off
)
1823 struct mbuf
*m
= *mp
;
1824 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1825 struct in6pcb
*in6p
;
1826 struct in6pcb
*last
= NULL
;
1827 struct sockaddr_in6 rip6src
;
1828 struct icmp6_hdr
*icmp6
;
1829 struct mbuf
*opts
= NULL
;
1831 #ifndef PULLDOWN_TEST
1832 /* this is assumed to be safe. */
1833 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
1835 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
1836 if (icmp6
== NULL
) {
1837 /* m is already reclaimed */
1838 return IPPROTO_DONE
;
1842 bzero(&rip6src
, sizeof(rip6src
));
1843 rip6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1844 rip6src
.sin6_family
= AF_INET6
;
1845 /* KAME hack: recover scopeid */
1846 (void)in6_recoverscope(&rip6src
, &ip6
->ip6_src
, m
->m_pkthdr
.rcvif
);
1848 lck_rw_lock_shared(ripcbinfo
.mtx
);
1849 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
1851 if ((in6p
->inp_vflag
& INP_IPV6
) == 0)
1854 if (!(in6p
->in6p_flags
& INP_IPV6
))
1857 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
)
1859 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
1860 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
))
1862 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
1863 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
))
1865 if (in6p
->in6p_icmp6filt
1866 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
1867 in6p
->in6p_icmp6filt
))
1871 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
1872 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1873 ip6_savecontrol(last
, &opts
, ip6
, n
);
1874 /* strip intermediate headers */
1876 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1877 (struct sockaddr
*)&rip6src
,
1878 n
, opts
, NULL
) != 0) {
1879 sorwakeup(last
->in6p_socket
);
1886 lck_rw_done(ripcbinfo
.mtx
);
1888 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1889 ip6_savecontrol(last
, &opts
, ip6
, m
);
1890 /* strip intermediate headers */
1892 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1893 (struct sockaddr
*)&rip6src
, m
, opts
, NULL
) != 0) {
1894 sorwakeup(last
->in6p_socket
);
1898 ip6stat
.ip6s_delivered
--;
1900 return IPPROTO_DONE
;
1904 * Reflect the ip6 packet back to the source.
1905 * OFF points to the icmp6 header, counted from the top of the mbuf.
1908 icmp6_reflect(m
, off
)
1912 struct ip6_hdr
*ip6
;
1913 struct icmp6_hdr
*icmp6
;
1914 struct in6_ifaddr
*ia
;
1915 struct in6_addr t
, src_storage
, *src
= 0;
1918 struct ifnet
*outif
= NULL
;
1919 struct sockaddr_in6 sa6_src
, sa6_dst
;
1920 #ifdef COMPAT_RFC1885
1921 int mtu
= IPV6_MMTU
;
1922 struct sockaddr_in6
*sin6
= &icmp6_reflect_rt
.ro_dst
;
1925 /* too short to reflect */
1926 if (off
< sizeof(struct ip6_hdr
)) {
1928 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1929 (u_long
)off
, (u_long
)sizeof(struct ip6_hdr
),
1930 __FILE__
, __LINE__
));
1935 * If there are extra headers between IPv6 and ICMPv6, strip
1936 * off that header first.
1939 if (sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) > MHLEN
)
1940 panic("assumption failed in icmp6_reflect");
1942 if (off
> sizeof(struct ip6_hdr
)) {
1944 struct ip6_hdr nip6
;
1946 l
= off
- sizeof(struct ip6_hdr
);
1947 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
1949 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
1951 if ((m
= m_pullup(m
, l
)) == NULL
)
1954 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
1955 } else /* off == sizeof(struct ip6_hdr) */ {
1957 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
1959 if ((m
= m_pullup(m
, l
)) == NULL
)
1963 plen
= m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
1964 ip6
= mtod(m
, struct ip6_hdr
*);
1965 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
1966 icmp6
= (struct icmp6_hdr
*)(ip6
+ 1);
1967 type
= icmp6
->icmp6_type
; /* keep type for statistics */
1968 code
= icmp6
->icmp6_code
; /* ditto. */
1972 * ip6_input() drops a packet if its src is multicast.
1973 * So, the src is never multicast.
1975 ip6
->ip6_dst
= ip6
->ip6_src
;
1978 * XXX: make sure to embed scope zone information, using
1979 * already embedded IDs or the received interface (if any).
1980 * Note that rcvif may be NULL.
1981 * TODO: scoped routing case (XXX).
1983 bzero(&sa6_src
, sizeof(sa6_src
));
1984 sa6_src
.sin6_family
= AF_INET6
;
1985 sa6_src
.sin6_len
= sizeof(sa6_src
);
1986 sa6_src
.sin6_addr
= ip6
->ip6_dst
;
1987 in6_recoverscope(&sa6_src
, &ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
);
1988 in6_embedscope(&ip6
->ip6_dst
, &sa6_src
, NULL
, NULL
);
1989 bzero(&sa6_dst
, sizeof(sa6_dst
));
1990 sa6_dst
.sin6_family
= AF_INET6
;
1991 sa6_dst
.sin6_len
= sizeof(sa6_dst
);
1992 sa6_dst
.sin6_addr
= t
;
1993 in6_recoverscope(&sa6_dst
, &t
, m
->m_pkthdr
.rcvif
);
1994 in6_embedscope(&t
, &sa6_dst
, NULL
, NULL
);
1996 #ifdef COMPAT_RFC1885
1999 * RFC 1885 requires that echo reply should be truncated if it
2000 * does not fit in with (return) path MTU, but the description was
2001 * removed in the new spec.
2003 if (icmp6_reflect_rt
.ro_rt
== 0 ||
2004 ! (IN6_ARE_ADDR_EQUAL(&sin6
->sin6_addr
, &ip6
->ip6_dst
))) {
2005 if (icmp6_reflect_rt
.ro_rt
) {
2006 rtfree(icmp6_reflect_rt
.ro_rt
);
2007 icmp6_reflect_rt
.ro_rt
= 0;
2009 bzero(sin6
, sizeof(*sin6
));
2010 sin6
->sin6_family
= PF_INET6
;
2011 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2012 sin6
->sin6_addr
= ip6
->ip6_dst
;
2014 rtalloc_ign((struct route
*)&icmp6_reflect_rt
.ro_rt
,
2018 if (icmp6_reflect_rt
.ro_rt
== 0)
2021 if ((icmp6_reflect_rt
.ro_rt
->rt_flags
& RTF_HOST
)
2022 && mtu
< icmp6_reflect_rt
.ro_rt
->rt_ifp
->if_mtu
)
2023 mtu
= icmp6_reflect_rt
.ro_rt
->rt_rmx
.rmx_mtu
;
2025 if (mtu
< m
->m_pkthdr
.len
) {
2026 plen
-= (m
->m_pkthdr
.len
- mtu
);
2027 m_adj(m
, mtu
- m
->m_pkthdr
.len
);
2031 * If the incoming packet was addressed directly to us(i.e. unicast),
2032 * use dst as the src for the reply.
2033 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2034 * (for example) when we encounter an error while forwarding procedure
2035 * destined to a duplicated address of ours.
2037 lck_mtx_lock(nd6_mutex
);
2038 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
)
2039 if (IN6_ARE_ADDR_EQUAL(&t
, &ia
->ia_addr
.sin6_addr
) &&
2040 (ia
->ia6_flags
& (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
)) == 0) {
2044 lck_mtx_unlock(nd6_mutex
);
2045 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) && (m
->m_flags
& M_LOOP
)) {
2047 * This is the case if the dst is our link-local address
2048 * and the sender is also ourselves.
2055 struct route_in6 ro
;
2058 * This case matches to multicasts, our anycast, or unicasts
2059 * that we do not own. Select a source address based on the
2060 * source address of the erroneous packet.
2062 bzero(&ro
, sizeof(ro
));
2063 src
= in6_selectsrc(&sa6_src
, NULL
, NULL
, &ro
, NULL
, &src_storage
, &e
);
2065 rtfree(ro
.ro_rt
); /* XXX: we could use this */
2068 "icmp6_reflect: source can't be determined: "
2069 "dst=%s, error=%d\n",
2070 ip6_sprintf(&sa6_src
.sin6_addr
), e
));
2075 ip6
->ip6_src
= *src
;
2078 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2079 ip6
->ip6_vfc
|= IPV6_VERSION
;
2080 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2081 if (m
->m_pkthdr
.rcvif
) {
2082 /* XXX: This may not be the outgoing interface */
2083 ip6
->ip6_hlim
= nd_ifinfo
[m
->m_pkthdr
.rcvif
->if_index
].chlim
;
2085 ip6
->ip6_hlim
= ip6_defhlim
;
2087 icmp6
->icmp6_cksum
= 0;
2088 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
2089 sizeof(struct ip6_hdr
), plen
);
2092 * XXX option handling
2095 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
2097 /* Don't lookup socket */
2098 if (ipsec_bypass
== 0)
2099 (void)ipsec_setsocket(m
, NULL
);
2102 #ifdef COMPAT_RFC1885
2103 ip6_output(m
, NULL
, &icmp6_reflect_rt
, 0, NULL
, &outif
, 0);
2105 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
, 0);
2108 icmp6_ifoutstat_inc(outif
, type
, code
);
2125 icmp6_redirect_diag(src6
, dst6
, tgt6
)
2126 struct in6_addr
*src6
;
2127 struct in6_addr
*dst6
;
2128 struct in6_addr
*tgt6
;
2130 static char buf
[1024];
2131 snprintf(buf
, sizeof(buf
), "(src=%s dst=%s tgt=%s)",
2132 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
2137 icmp6_redirect_input(m
, off
)
2141 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2142 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2143 struct nd_redirect
*nd_rd
;
2144 int icmp6len
= ntohs(ip6
->ip6_plen
);
2145 char *lladdr
= NULL
;
2147 u_char
*redirhdr
= NULL
;
2148 int redirhdrlen
= 0;
2149 struct rtentry
*rt
= NULL
;
2152 struct in6_addr src6
= ip6
->ip6_src
;
2153 struct in6_addr redtgt6
;
2154 struct in6_addr reddst6
;
2155 union nd_opts ndopts
;
2160 /* XXX if we are router, we don't update route by icmp6 redirect */
2163 if (!icmp6_rediraccept
)
2166 #ifndef PULLDOWN_TEST
2167 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
2168 nd_rd
= (struct nd_redirect
*)((caddr_t
)ip6
+ off
);
2170 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
2171 if (nd_rd
== NULL
) {
2172 icmp6stat
.icp6s_tooshort
++;
2176 redtgt6
= nd_rd
->nd_rd_target
;
2177 reddst6
= nd_rd
->nd_rd_dst
;
2179 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2180 redtgt6
.s6_addr16
[1] = htons(ifp
->if_index
);
2181 if (IN6_IS_ADDR_LINKLOCAL(&reddst6
))
2182 reddst6
.s6_addr16
[1] = htons(ifp
->if_index
);
2185 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
2187 "ICMP6 redirect sent from %s rejected; "
2188 "must be from linklocal\n", ip6_sprintf(&src6
)));
2191 if (ip6
->ip6_hlim
!= 255) {
2193 "ICMP6 redirect sent from %s rejected; "
2194 "hlim=%d (must be 255)\n",
2195 ip6_sprintf(&src6
), ip6
->ip6_hlim
));
2199 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2200 struct sockaddr_in6 sin6
;
2201 struct in6_addr
*gw6
;
2203 bzero(&sin6
, sizeof(sin6
));
2204 sin6
.sin6_family
= AF_INET6
;
2205 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2206 bcopy(&reddst6
, &sin6
.sin6_addr
, sizeof(reddst6
));
2207 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0UL);
2209 if (rt
->rt_gateway
== NULL
||
2210 rt
->rt_gateway
->sa_family
!= AF_INET6
) {
2212 "ICMP6 redirect rejected; no route "
2213 "with inet6 gateway found for redirect dst: %s\n",
2214 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2219 gw6
= &(((struct sockaddr_in6
*)rt
->rt_gateway
)->sin6_addr
);
2220 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
2222 "ICMP6 redirect rejected; "
2223 "not equal to gw-for-src=%s (must be same): "
2226 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2232 "ICMP6 redirect rejected; "
2233 "no route found for redirect dst: %s\n",
2234 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2240 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
2242 "ICMP6 redirect rejected; "
2243 "redirect dst must be unicast: %s\n",
2244 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2248 is_router
= is_onlink
= 0;
2249 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2250 is_router
= 1; /* router case */
2251 if (bcmp(&redtgt6
, &reddst6
, sizeof(redtgt6
)) == 0)
2252 is_onlink
= 1; /* on-link destination case */
2253 if (!is_router
&& !is_onlink
) {
2255 "ICMP6 redirect rejected; "
2256 "neither router case nor onlink case: %s\n",
2257 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2260 /* validation passed */
2262 icmp6len
-= sizeof(*nd_rd
);
2263 nd6_option_init(nd_rd
+ 1, icmp6len
, &ndopts
);
2264 if (nd6_options(&ndopts
) < 0) {
2265 nd6log((LOG_INFO
, "icmp6_redirect_input: "
2266 "invalid ND option, rejected: %s\n",
2267 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2268 /* nd6_options have incremented stats */
2272 if (ndopts
.nd_opts_tgt_lladdr
) {
2273 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
2274 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
2277 if (ndopts
.nd_opts_rh
) {
2278 redirhdrlen
= ndopts
.nd_opts_rh
->nd_opt_rh_len
;
2279 redirhdr
= (u_char
*)(ndopts
.nd_opts_rh
+ 1); /* xxx */
2282 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
2284 "icmp6_redirect_input: lladdrlen mismatch for %s "
2285 "(if %d, icmp6 packet %d): %s\n",
2286 ip6_sprintf(&redtgt6
), ifp
->if_addrlen
, lladdrlen
- 2,
2287 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2292 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
2293 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
2295 if (!is_onlink
) { /* better router case. perform rtredirect. */
2296 /* perform rtredirect */
2297 struct sockaddr_in6 sdst
;
2298 struct sockaddr_in6 sgw
;
2299 struct sockaddr_in6 ssrc
;
2301 bzero(&sdst
, sizeof(sdst
));
2302 bzero(&sgw
, sizeof(sgw
));
2303 bzero(&ssrc
, sizeof(ssrc
));
2304 sdst
.sin6_family
= sgw
.sin6_family
= ssrc
.sin6_family
= AF_INET6
;
2305 sdst
.sin6_len
= sgw
.sin6_len
= ssrc
.sin6_len
=
2306 sizeof(struct sockaddr_in6
);
2307 bcopy(&redtgt6
, &sgw
.sin6_addr
, sizeof(struct in6_addr
));
2308 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2309 bcopy(&src6
, &ssrc
.sin6_addr
, sizeof(struct in6_addr
));
2310 rtredirect((struct sockaddr
*)&sdst
, (struct sockaddr
*)&sgw
,
2311 (struct sockaddr
*)NULL
, RTF_GATEWAY
| RTF_HOST
,
2312 (struct sockaddr
*)&ssrc
,
2313 (struct rtentry
**)NULL
);
2315 /* finally update cached route in each socket via pfctlinput */
2317 struct sockaddr_in6 sdst
;
2319 bzero(&sdst
, sizeof(sdst
));
2320 sdst
.sin6_family
= AF_INET6
;
2321 sdst
.sin6_len
= sizeof(struct sockaddr_in6
);
2322 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2323 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
2325 key_sa_routechange((struct sockaddr
*)&sdst
);
2334 icmp6stat
.icp6s_badredirect
++;
2339 icmp6_redirect_output(m0
, rt
)
2343 struct ifnet
*ifp
; /* my outgoing interface */
2344 struct in6_addr ifp_ll6
;
2345 struct in6_addr
*router_ll6
;
2346 struct ip6_hdr
*sip6
; /* m0 as struct ip6_hdr */
2347 struct mbuf
*m
= NULL
; /* newly allocated one */
2348 struct ip6_hdr
*ip6
; /* m as struct ip6_hdr */
2349 struct nd_redirect
*nd_rd
;
2352 struct ifnet
*outif
= NULL
;
2353 struct sockaddr_in6 src_sa
;
2355 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, ND_REDIRECT
, 0);
2358 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
))
2361 /* if we are not router, we don't send icmp6 redirect */
2362 if (!ip6_forwarding
|| ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))
2367 * the source address must identify a neighbor, and
2368 * the destination address must not be a multicast address
2369 * [RFC 2461, sec 8.2]
2371 sip6
= mtod(m0
, struct ip6_hdr
*);
2372 bzero(&src_sa
, sizeof(src_sa
));
2373 src_sa
.sin6_family
= AF_INET6
;
2374 src_sa
.sin6_len
= sizeof(src_sa
);
2375 src_sa
.sin6_addr
= sip6
->ip6_src
;
2376 /* we don't currently use sin6_scope_id, but eventually use it */
2377 src_sa
.sin6_scope_id
= in6_addr2scopeid(ifp
, &sip6
->ip6_src
);
2378 if (nd6_is_addr_neighbor(&src_sa
, ifp
, 0) == 0)
2380 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
))
2381 goto fail
; /* what should we do here? */
2384 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0))
2388 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2389 * we almost always ask for an mbuf cluster for simplicity.
2390 * (MHLEN < IPV6_MMTU is almost always true)
2392 #if IPV6_MMTU >= MCLBYTES
2393 # error assumption failed about IPV6_MMTU and MCLBYTES
2395 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
2396 if (m
&& IPV6_MMTU
>= MHLEN
)
2397 MCLGET(m
, M_DONTWAIT
);
2400 m
->m_pkthdr
.rcvif
= NULL
;
2402 maxlen
= M_TRAILINGSPACE(m
);
2403 maxlen
= min(IPV6_MMTU
, maxlen
);
2404 /* just for safety */
2405 if (maxlen
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) +
2406 ((sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7)) {
2411 /* get ip6 linklocal address for ifp(my outgoing interface). */
2412 struct in6_ifaddr
*ia
;
2413 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
2415 IN6_IFF_ANYCAST
)) == NULL
)
2417 ifp_ll6
= ia
->ia_addr
.sin6_addr
;
2418 ifafree(&ia
->ia_ifa
);
2421 /* get ip6 linklocal address for the router. */
2422 if (rt
->rt_gateway
&& (rt
->rt_flags
& RTF_GATEWAY
)) {
2423 struct sockaddr_in6
*sin6
;
2424 sin6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
2425 router_ll6
= &sin6
->sin6_addr
;
2426 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6
))
2427 router_ll6
= (struct in6_addr
*)NULL
;
2429 router_ll6
= (struct in6_addr
*)NULL
;
2432 ip6
= mtod(m
, struct ip6_hdr
*);
2434 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2435 ip6
->ip6_vfc
|= IPV6_VERSION
;
2436 /* ip6->ip6_plen will be set later */
2437 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2438 ip6
->ip6_hlim
= 255;
2439 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2440 bcopy(&ifp_ll6
, &ip6
->ip6_src
, sizeof(struct in6_addr
));
2441 bcopy(&sip6
->ip6_src
, &ip6
->ip6_dst
, sizeof(struct in6_addr
));
2444 nd_rd
= (struct nd_redirect
*)(ip6
+ 1);
2445 nd_rd
->nd_rd_type
= ND_REDIRECT
;
2446 nd_rd
->nd_rd_code
= 0;
2447 nd_rd
->nd_rd_reserved
= 0;
2448 if (rt
->rt_flags
& RTF_GATEWAY
) {
2450 * nd_rd->nd_rd_target must be a link-local address in
2451 * better router cases.
2455 bcopy(router_ll6
, &nd_rd
->nd_rd_target
,
2456 sizeof(nd_rd
->nd_rd_target
));
2457 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2458 sizeof(nd_rd
->nd_rd_dst
));
2460 /* make sure redtgt == reddst */
2461 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_target
,
2462 sizeof(nd_rd
->nd_rd_target
));
2463 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2464 sizeof(nd_rd
->nd_rd_dst
));
2467 p
= (u_char
*)(nd_rd
+ 1);
2473 /* target lladdr option */
2474 struct rtentry
*rt_router
= NULL
;
2476 struct sockaddr_dl
*sdl
;
2477 struct nd_opt_hdr
*nd_opt
;
2480 rt_router
= nd6_lookup(router_ll6
, 0, ifp
, 0);
2483 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2484 len
= (len
+ 7) & ~7; /* round by 8 */
2486 if (len
+ (p
- (u_char
*)ip6
) > maxlen
)
2488 if (!(rt_router
->rt_flags
& RTF_GATEWAY
) &&
2489 (rt_router
->rt_flags
& RTF_LLINFO
) &&
2490 (rt_router
->rt_gateway
->sa_family
== AF_LINK
) &&
2491 (sdl
= (struct sockaddr_dl
*)rt_router
->rt_gateway
) &&
2493 nd_opt
= (struct nd_opt_hdr
*)p
;
2494 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
2495 nd_opt
->nd_opt_len
= len
>> 3;
2496 lladdr
= (char *)(nd_opt
+ 1);
2497 bcopy(LLADDR(sdl
), lladdr
, ifp
->if_addrlen
);
2503 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2505 /* just to be safe */
2506 #ifdef M_DECRYPTED /*not openbsd*/
2507 if (m0
->m_flags
& M_DECRYPTED
)
2510 if (p
- (u_char
*)ip6
> maxlen
)
2514 /* redirected header option */
2516 struct nd_opt_rd_hdr
*nd_opt_rh
;
2519 * compute the maximum size for icmp6 redirect header option.
2520 * XXX room for auth header?
2522 len
= maxlen
- (p
- (u_char
*)ip6
);
2525 /* This is just for simplicity. */
2526 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2528 m_freem(m0
->m_next
);
2531 m0
->m_pkthdr
.len
= m0
->m_len
;
2535 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2536 * about padding/truncate rule for the original IP packet.
2537 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2538 * - "attach as much as possible" is the goal
2539 * - pad if not aligned (original size can be guessed by original
2541 * Following code adds the padding if it is simple enough,
2542 * and truncates if not.
2544 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
)
2545 panic("assumption failed in %s:%d\n", __FILE__
, __LINE__
);
2547 if (len
- sizeof(*nd_opt_rh
) < m0
->m_pkthdr
.len
) {
2548 /* not enough room, truncate */
2549 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2551 /* enough room, pad or truncate */
2554 extra
= m0
->m_pkthdr
.len
% 8;
2556 /* pad if easy enough, truncate if not */
2557 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2559 m0
->m_len
+= (8 - extra
);
2560 m0
->m_pkthdr
.len
+= (8 - extra
);
2563 m0
->m_pkthdr
.len
-= extra
;
2567 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2568 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2571 nd_opt_rh
= (struct nd_opt_rd_hdr
*)p
;
2572 bzero(nd_opt_rh
, sizeof(*nd_opt_rh
));
2573 nd_opt_rh
->nd_opt_rh_type
= ND_OPT_REDIRECTED_HEADER
;
2574 nd_opt_rh
->nd_opt_rh_len
= len
>> 3;
2575 p
+= sizeof(*nd_opt_rh
);
2576 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2578 /* connect m0 to m */
2580 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
2584 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_src
))
2585 sip6
->ip6_src
.s6_addr16
[1] = 0;
2586 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_dst
))
2587 sip6
->ip6_dst
.s6_addr16
[1] = 0;
2589 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_src
))
2590 ip6
->ip6_src
.s6_addr16
[1] = 0;
2591 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_dst
))
2592 ip6
->ip6_dst
.s6_addr16
[1] = 0;
2594 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_target
))
2595 nd_rd
->nd_rd_target
.s6_addr16
[1] = 0;
2596 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_dst
))
2597 nd_rd
->nd_rd_dst
.s6_addr16
[1] = 0;
2599 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
2601 nd_rd
->nd_rd_cksum
= 0;
2603 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
2605 /* send the packet to outside... */
2607 /* Don't lookup socket */
2608 if (ipsec_bypass
== 0)
2609 (void)ipsec_setsocket(m
, NULL
);
2611 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
, 0);
2613 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
2614 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
2616 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
2628 #define sotoin6pcb sotoinpcb
2629 #define in6pcb inpcb
2630 #define in6p_icmp6filt inp_icmp6filt
2633 * ICMPv6 socket option processing.
2636 icmp6_ctloutput(so
, sopt
)
2638 struct sockopt
*sopt
;
2642 struct inpcb
*inp
= sotoinpcb(so
);
2643 int level
, op
, optname
;
2646 level
= sopt
->sopt_level
;
2647 op
= sopt
->sopt_dir
;
2648 optname
= sopt
->sopt_name
;
2649 optlen
= sopt
->sopt_valsize
;
2651 level
= op
= optname
= optlen
= 0;
2653 if (level
!= IPPROTO_ICMPV6
) {
2662 struct icmp6_filter
*p
;
2664 if (optlen
!= sizeof(*p
)) {
2668 if (inp
->in6p_icmp6filt
== NULL
) {
2672 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
2678 error
= ENOPROTOOPT
;
2687 if (inp
->in6p_icmp6filt
== NULL
) {
2691 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
2692 sizeof(struct icmp6_filter
));
2697 error
= ENOPROTOOPT
;
2708 #undef in6p_icmp6filt
2712 * ICMPv6 socket datagram option processing.
2715 icmp6_dgram_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2717 if (so
->so_uid
== 0)
2718 return icmp6_ctloutput(so
, sopt
);
2720 if (sopt
->sopt_level
== IPPROTO_ICMPV6
) {
2721 switch (sopt
->sopt_name
) {
2723 return icmp6_ctloutput(so
, sopt
);
2729 if (sopt
->sopt_level
!= IPPROTO_IPV6
)
2732 switch (sopt
->sopt_name
) {
2733 case IPV6_PKTOPTIONS
:
2734 case IPV6_UNICAST_HOPS
:
2743 case IPV6_MULTICAST_IF
:
2744 case IPV6_MULTICAST_HOPS
:
2745 case IPV6_MULTICAST_LOOP
:
2746 case IPV6_JOIN_GROUP
:
2747 case IPV6_LEAVE_GROUP
:
2748 case IPV6_PORTRANGE
:
2749 case IPV6_IPSEC_POLICY
:
2750 return ip6_ctloutput(so
, sopt
);
2759 __private_extern__
int
2760 icmp6_dgram_send(struct socket
*so
, __unused
int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
2761 struct mbuf
*control
, __unused
struct proc
*p
)
2764 struct inpcb
*inp
= sotoinpcb(so
);
2765 struct sockaddr_in6 tmp
;
2766 struct sockaddr_in6
*dst
;
2767 struct icmp6_hdr
*icmp6
;
2769 if (so
->so_uid
== 0)
2770 return rip6_output(m
, so
, (struct sockaddr_in6
*) nam
, control
);
2772 /* always copy sockaddr to avoid overwrites */
2773 if (so
->so_state
& SS_ISCONNECTED
) {
2779 bzero(&tmp
, sizeof(tmp
));
2780 tmp
.sin6_family
= AF_INET6
;
2781 tmp
.sin6_len
= sizeof(struct sockaddr_in6
);
2782 bcopy(&inp
->in6p_faddr
, &tmp
.sin6_addr
,
2783 sizeof(struct in6_addr
));
2790 tmp
= *(struct sockaddr_in6
*)nam
;
2795 * For an ICMPv6 packet, we should know its type and code
2797 if (so
->so_proto
->pr_protocol
== IPPROTO_ICMPV6
) {
2798 if (m
->m_len
< sizeof(struct icmp6_hdr
) &&
2799 (m
= m_pullup(m
, sizeof(struct icmp6_hdr
))) == NULL
) {
2803 icmp6
= mtod(m
, struct icmp6_hdr
*);
2806 * Allow only to send echo request type 128 with code 0
2807 * See RFC 2463 for Echo Request Message format
2809 if (icmp6
->icmp6_type
!= 128 || icmp6
->icmp6_code
!= 0) {
2815 #if ENABLE_DEFAULT_SCOPE
2816 if (dst
->sin6_scope_id
== 0) { /* not change if specified */
2817 dst
->sin6_scope_id
= scope6_addr2default(&dst
->sin6_addr
);
2821 return rip6_output(m
, so
, (struct sockaddr_in6
*) nam
, control
);
2827 /* Like rip6_attach but without root privilege enforcement */
2828 __private_extern__
int
2829 icmp6_dgram_attach(struct socket
*so
, int proto
, struct proc
*p
)
2834 inp
= sotoinpcb(so
);
2836 panic("icmp6_dgram_attach");
2838 if (proto
!= IPPROTO_ICMPV6
)
2841 error
= soreserve(so
, rip_sendspace
, rip_recvspace
);
2844 error
= in_pcballoc(so
, &ripcbinfo
, p
);
2847 inp
= (struct inpcb
*)so
->so_pcb
;
2848 inp
->inp_vflag
|= INP_IPV6
;
2849 inp
->in6p_ip6_nxt
= IPPROTO_ICMPV6
;
2850 inp
->in6p_hops
= -1; /* use kernel default */
2851 inp
->in6p_cksum
= -1;
2852 MALLOC(inp
->in6p_icmp6filt
, struct icmp6_filter
*,
2853 sizeof(struct icmp6_filter
), M_PCB
, M_WAITOK
);
2854 if (inp
->in6p_icmp6filt
== NULL
)
2856 ICMP6_FILTER_SETPASSALL(inp
->in6p_icmp6filt
);
2862 #ifndef HAVE_PPSRATECHECK
2864 #define timersub(tvp, uvp, vvp) \
2866 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2867 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2868 if ((vvp)->tv_usec < 0) { \
2870 (vvp)->tv_usec += 1000000; \
2876 * ppsratecheck(): packets (or events) per second limitation.
2879 ppsratecheck(lasttime
, curpps
, maxpps
)
2880 struct timeval
*lasttime
;
2882 int maxpps
; /* maximum pps allowed */
2884 struct timeval tv
, delta
;
2889 timersub(&tv
, lasttime
, &delta
);
2892 * Check for 0,0 so that the message will be seen at least once.
2893 * If more than one second has passed since the last update of
2894 * lasttime, reset the counter.
2896 * we do increment *curpps even in *curpps < maxpps case, as some may
2897 * try to use *curpps for stat purposes as well.
2899 if ((lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0) ||
2900 delta
.tv_sec
>= 1) {
2904 } else if (maxpps
< 0)
2906 else if (*curpps
< maxpps
)
2911 #if 1 /* DIAGNOSTIC? */
2912 /* be careful about wrap-around */
2913 if (*curpps
+ 1 > 0)
2914 *curpps
= *curpps
+ 1;
2917 * assume that there's not too many calls to this function.
2918 * not sure if the assumption holds, as it depends on *caller's*
2919 * behavior, not the behavior of this function.
2920 * IMHO it is wrong to make assumption on the caller's behavior,
2921 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
2923 *curpps
= *curpps
+ 1;
2931 * Perform rate limit check.
2932 * Returns 0 if it is okay to send the icmp6 packet.
2933 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2936 * XXX per-destination/type check necessary?
2940 __unused
const struct in6_addr
*dst
, /* not used at this moment */
2941 __unused
const int type
, /* not used at this moment */
2942 __unused
const int code
) /* not used at this moment */
2946 ret
= 0; /* okay to send */
2949 if (!ppsratecheck(&icmp6errppslim_last
, &icmp6errpps_count
,
2951 /* The packet is subject to rate limit */