2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #define MBUF_PRIVATE /* XXXRW: Optimisation tries to avoid M_EXT mbufs */
69 #include "opt_inet6.h"
71 #include <sys/param.h>
72 #include <sys/domain.h>
74 #include <sys/kernel.h>
76 #include <sys/malloc.h>
79 #include <sys/protosw.h>
80 #include <sys/signalvar.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
84 #include <sys/syslog.h>
85 #include <sys/systm.h>
89 #include <net/if_var.h>
90 #include <net/if_dl.h>
91 #include <net/if_llatbl.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet/icmp6.h>
101 #include <netinet/tcp_var.h>
103 #include <netinet6/in6_ifattach.h>
104 #include <netinet6/in6_pcb.h>
105 #include <netinet6/ip6protosw.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/scope6_var.h>
108 #include <netinet6/mld6_var.h>
109 #include <netinet6/nd6.h>
110 #include <netinet6/send.h>
112 extern struct domain inet6domain;
114 VNET_PCPUSTAT_DEFINE(struct icmp6stat, icmp6stat);
115 VNET_PCPUSTAT_SYSINIT(icmp6stat);
118 VNET_PCPUSTAT_SYSUNINIT(icmp6stat);
121 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
122 VNET_DECLARE(struct inpcbhead, ripcb);
123 VNET_DECLARE(int, icmp6errppslim);
124 static VNET_DEFINE(int, icmp6errpps_count) = 0;
125 static VNET_DEFINE(struct timeval, icmp6errppslim_last);
126 VNET_DECLARE(int, icmp6_nodeinfo);
128 #define V_ripcbinfo VNET(ripcbinfo)
129 #define V_ripcb VNET(ripcb)
130 #define V_icmp6errppslim VNET(icmp6errppslim)
131 #define V_icmp6errpps_count VNET(icmp6errpps_count)
132 #define V_icmp6errppslim_last VNET(icmp6errppslim_last)
133 #define V_icmp6_nodeinfo VNET(icmp6_nodeinfo)
135 static void icmp6_errcount(int, int);
136 static int icmp6_rip6_input(struct mbuf **, int);
137 static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
138 static const char *icmp6_redirect_diag(struct in6_addr *,
139 struct in6_addr *, struct in6_addr *);
140 static struct mbuf *ni6_input(struct mbuf *, int);
141 static struct mbuf *ni6_nametodns(const char *, int, int);
142 static int ni6_dnsmatch(const char *, int, const char *, int);
143 static int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *,
144 struct ifnet **, struct in6_addr *);
145 static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
146 struct ifnet *, int);
147 static int icmp6_notify_error(struct mbuf **, int, int, int);
150 * Kernel module interface for updating icmp6stat. The argument is an index
151 * into icmp6stat treated as an array of u_quad_t. While this encodes the
152 * general layout of icmp6stat into the caller, it doesn't encode its
153 * location, so that future changes to add, for example, per-CPU stats
154 * support won't cause binary compatibility problems for kernel modules.
157 kmod_icmp6stat_inc(int statnum)
160 counter_u64_add(VNET(icmp6stat)[statnum], 1);
164 icmp6_errcount(int type, int code)
167 case ICMP6_DST_UNREACH:
169 case ICMP6_DST_UNREACH_NOROUTE:
170 ICMP6STAT_INC(icp6s_odst_unreach_noroute);
172 case ICMP6_DST_UNREACH_ADMIN:
173 ICMP6STAT_INC(icp6s_odst_unreach_admin);
175 case ICMP6_DST_UNREACH_BEYONDSCOPE:
176 ICMP6STAT_INC(icp6s_odst_unreach_beyondscope);
178 case ICMP6_DST_UNREACH_ADDR:
179 ICMP6STAT_INC(icp6s_odst_unreach_addr);
181 case ICMP6_DST_UNREACH_NOPORT:
182 ICMP6STAT_INC(icp6s_odst_unreach_noport);
186 case ICMP6_PACKET_TOO_BIG:
187 ICMP6STAT_INC(icp6s_opacket_too_big);
189 case ICMP6_TIME_EXCEEDED:
191 case ICMP6_TIME_EXCEED_TRANSIT:
192 ICMP6STAT_INC(icp6s_otime_exceed_transit);
194 case ICMP6_TIME_EXCEED_REASSEMBLY:
195 ICMP6STAT_INC(icp6s_otime_exceed_reassembly);
199 case ICMP6_PARAM_PROB:
201 case ICMP6_PARAMPROB_HEADER:
202 ICMP6STAT_INC(icp6s_oparamprob_header);
204 case ICMP6_PARAMPROB_NEXTHEADER:
205 ICMP6STAT_INC(icp6s_oparamprob_nextheader);
207 case ICMP6_PARAMPROB_OPTION:
208 ICMP6STAT_INC(icp6s_oparamprob_option);
213 ICMP6STAT_INC(icp6s_oredirect);
216 ICMP6STAT_INC(icp6s_ounknown);
220 * A wrapper function for icmp6_error() necessary when the erroneous packet
221 * may not contain enough scope zone information.
224 icmp6_error2(struct mbuf *m, int type, int code, int param,
232 #ifndef PULLDOWN_TEST
233 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
235 if (m->m_len < sizeof(struct ip6_hdr)) {
236 m = m_pullup(m, sizeof(struct ip6_hdr));
242 ip6 = mtod(m, struct ip6_hdr *);
244 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
246 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
249 icmp6_error(m, type, code, param);
253 * Generate an error packet of type error in response to bad IP6 packet.
256 icmp6_error(struct mbuf *m, int type, int code, int param)
258 struct ip6_hdr *oip6, *nip6;
259 struct icmp6_hdr *icmp6;
264 ICMP6STAT_INC(icp6s_error);
266 /* count per-type-code statistics */
267 icmp6_errcount(type, code);
269 #ifdef M_DECRYPTED /*not openbsd*/
270 if (m->m_flags & M_DECRYPTED) {
271 ICMP6STAT_INC(icp6s_canterror);
276 #ifndef PULLDOWN_TEST
277 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
279 if (m->m_len < sizeof(struct ip6_hdr)) {
280 m = m_pullup(m, sizeof(struct ip6_hdr));
285 oip6 = mtod(m, struct ip6_hdr *);
288 * If the destination address of the erroneous packet is a multicast
289 * address, or the packet was sent using link-layer multicast,
290 * we should basically suppress sending an error (RFC 2463, Section
292 * We have two exceptions (the item e.2 in that section):
293 * - the Packet Too Big message can be sent for path MTU discovery.
294 * - the Parameter Problem Message that can be allowed an icmp6 error
295 * in the option type field. This check has been done in
296 * ip6_unknown_opt(), so we can just check the type and code.
298 if ((m->m_flags & (M_BCAST|M_MCAST) ||
299 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
300 (type != ICMP6_PACKET_TOO_BIG &&
301 (type != ICMP6_PARAM_PROB ||
302 code != ICMP6_PARAMPROB_OPTION)))
306 * RFC 2463, 2.4 (e.5): source address check.
307 * XXX: the case of anycast source?
309 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
310 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
314 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
318 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
319 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
320 struct icmp6_hdr *icp;
322 #ifndef PULLDOWN_TEST
323 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
324 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
326 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
329 ICMP6STAT_INC(icp6s_tooshort);
333 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
334 icp->icmp6_type == ND_REDIRECT) {
337 * Special case: for redirect (which is
338 * informational) we must not send icmp6 error.
340 ICMP6STAT_INC(icp6s_canterror);
343 /* ICMPv6 informational - send the error */
346 /* non-ICMPv6 - send the error */
349 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
351 /* Finally, do rate limitation check. */
352 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
353 ICMP6STAT_INC(icp6s_toofreq);
358 * OK, ICMP6 can be generated.
361 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
362 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
364 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
365 M_PREPEND(m, preplen, M_NOWAIT); /* FIB is also copied over. */
367 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
371 nip6 = mtod(m, struct ip6_hdr *);
372 nip6->ip6_src = oip6->ip6_src;
373 nip6->ip6_dst = oip6->ip6_dst;
375 in6_clearscope(&oip6->ip6_src);
376 in6_clearscope(&oip6->ip6_dst);
378 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
379 icmp6->icmp6_type = type;
380 icmp6->icmp6_code = code;
381 icmp6->icmp6_pptr = htonl((u_int32_t)param);
384 * icmp6_reflect() is designed to be in the input path.
385 * icmp6_error() can be called from both input and output path,
386 * and if we are in output path rcvif could contain bogus value.
387 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
388 * information in ip header (nip6).
390 m->m_pkthdr.rcvif = NULL;
392 ICMP6STAT_INC(icp6s_outhist[type]);
393 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
399 * If we can't tell whether or not we can generate ICMP6, free it.
405 * Process a received ICMP6 message.
408 icmp6_input(struct mbuf **mp, int *offp, int proto)
410 struct mbuf *m = *mp, *n;
412 struct ip6_hdr *ip6, *nip6;
413 struct icmp6_hdr *icmp6, *nicmp6;
415 int icmp6len = m->m_pkthdr.len - *offp;
417 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
420 ifp = m->m_pkthdr.rcvif;
422 #ifndef PULLDOWN_TEST
423 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
424 /* m might change if M_LOOP. So, call mtod after this */
428 * Locate icmp6 structure in mbuf, and check
429 * that not corrupted and of at least minimum length
432 ip6 = mtod(m, struct ip6_hdr *);
433 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
434 if (icmp6len < sizeof(struct icmp6_hdr)) {
435 ICMP6STAT_INC(icp6s_tooshort);
440 * Check multicast group membership.
441 * Note: SSM filters are not applied for ICMPv6 traffic.
443 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
444 struct in6_multi *inm;
446 inm = in6m_lookup(ifp, &ip6->ip6_dst);
448 IP6STAT_INC(ip6s_notmember);
449 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
455 * calculate the checksum
457 #ifndef PULLDOWN_TEST
458 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
460 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
462 ICMP6STAT_INC(icp6s_tooshort);
466 code = icmp6->icmp6_code;
468 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
470 "ICMP6 checksum error(%d|%x) %s\n",
471 icmp6->icmp6_type, sum,
472 ip6_sprintf(ip6bufs, &ip6->ip6_src)));
473 ICMP6STAT_INC(icp6s_checksum);
477 ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]);
478 icmp6_ifstat_inc(ifp, ifs6_in_msg);
479 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
480 icmp6_ifstat_inc(ifp, ifs6_in_error);
482 switch (icmp6->icmp6_type) {
483 case ICMP6_DST_UNREACH:
484 icmp6_ifstat_inc(ifp, ifs6_in_dstunreach);
486 case ICMP6_DST_UNREACH_NOROUTE:
487 code = PRC_UNREACH_NET;
489 case ICMP6_DST_UNREACH_ADMIN:
490 icmp6_ifstat_inc(ifp, ifs6_in_adminprohib);
491 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
493 case ICMP6_DST_UNREACH_ADDR:
496 case ICMP6_DST_UNREACH_BEYONDSCOPE:
497 /* I mean "source address was incorrect." */
498 code = PRC_PARAMPROB;
500 case ICMP6_DST_UNREACH_NOPORT:
501 code = PRC_UNREACH_PORT;
509 case ICMP6_PACKET_TOO_BIG:
510 icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig);
512 /* validation is made in icmp6_mtudisc_update */
517 * Updating the path MTU will be done after examining
518 * intermediate extension headers.
523 case ICMP6_TIME_EXCEEDED:
524 icmp6_ifstat_inc(ifp, ifs6_in_timeexceed);
526 case ICMP6_TIME_EXCEED_TRANSIT:
527 code = PRC_TIMXCEED_INTRANS;
529 case ICMP6_TIME_EXCEED_REASSEMBLY:
530 code = PRC_TIMXCEED_REASS;
538 case ICMP6_PARAM_PROB:
539 icmp6_ifstat_inc(ifp, ifs6_in_paramprob);
541 case ICMP6_PARAMPROB_NEXTHEADER:
542 code = PRC_UNREACH_PROTOCOL;
544 case ICMP6_PARAMPROB_HEADER:
545 case ICMP6_PARAMPROB_OPTION:
546 code = PRC_PARAMPROB;
554 case ICMP6_ECHO_REQUEST:
555 icmp6_ifstat_inc(ifp, ifs6_in_echo);
558 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
563 || n->m_len < off + sizeof(struct icmp6_hdr)) {
567 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) <= MHLEN);
568 n = m_gethdr(M_NOWAIT, n0->m_type);
575 m_move_pkthdr(n, n0); /* FIB copied. */
576 n0len = n0->m_pkthdr.len; /* save for use below */
578 * Copy IPv6 and ICMPv6 only.
580 nip6 = mtod(n, struct ip6_hdr *);
581 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
582 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
583 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
584 noff = sizeof(struct ip6_hdr);
585 /* new mbuf contains only ipv6+icmpv6 headers */
586 n->m_len = noff + sizeof(struct icmp6_hdr);
588 * Adjust mbuf. ip6_plen will be adjusted in
591 m_adj(n0, off + sizeof(struct icmp6_hdr));
592 /* recalculate complete packet size */
593 n->m_pkthdr.len = n0len + (noff - off);
596 nip6 = mtod(n, struct ip6_hdr *);
597 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
601 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
602 nicmp6->icmp6_code = 0;
604 ICMP6STAT_INC(icp6s_reflect);
605 ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]);
606 icmp6_reflect(n, noff);
610 case ICMP6_ECHO_REPLY:
611 icmp6_ifstat_inc(ifp, ifs6_in_echoreply);
616 case MLD_LISTENER_QUERY:
617 case MLD_LISTENER_REPORT:
618 case MLD_LISTENER_DONE:
619 case MLDV2_LISTENER_REPORT:
621 * Drop MLD traffic which is not link-local, has a hop limit
622 * of greater than 1 hop, or which does not have the
623 * IPv6 HBH Router Alert option.
624 * As IPv6 HBH options are stripped in ip6_input() we must
625 * check an mbuf header flag.
626 * XXX Should we also sanity check that these messages
627 * were directed to a link-local multicast prefix?
629 if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0)
631 if (mld_input(m, off, icmp6len) != 0)
632 return (IPPROTO_DONE);
636 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
638 enum { WRU, FQDN } mode;
640 if (!V_icmp6_nodeinfo)
643 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
645 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
651 #ifndef PULLDOWN_TEST
652 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
655 n = m_copy(m, 0, M_COPYALL);
657 n = ni6_input(n, off);
658 /* XXX meaningless if n == NULL */
659 noff = sizeof(struct ip6_hdr);
666 * XXX: this combination of flags is pointless,
667 * but should we keep this for compatibility?
669 if ((V_icmp6_nodeinfo & 5) != 5)
675 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) + 4 <= MHLEN);
676 n = m_gethdr(M_NOWAIT, m->m_type);
681 if (!m_dup_pkthdr(n, m, M_NOWAIT)) {
683 * Previous code did a blind M_COPY_PKTHDR
684 * and said "just for rcvif". If true, then
685 * we could tolerate the dup failing (due to
686 * the deep copy of the tag chain). For now
687 * be conservative and just fail.
692 maxhlen = M_TRAILINGSPACE(n) -
693 (sizeof(*nip6) + sizeof(*nicmp6) + 4);
694 pr = curthread->td_ucred->cr_prison;
695 mtx_lock(&pr->pr_mtx);
696 hlen = strlen(pr->pr_hostname);
700 * Copy IPv6 and ICMPv6 only.
702 nip6 = mtod(n, struct ip6_hdr *);
703 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
704 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
705 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
706 p = (u_char *)(nicmp6 + 1);
708 /* meaningless TTL */
709 bcopy(pr->pr_hostname, p + 4, maxhlen);
710 mtx_unlock(&pr->pr_mtx);
711 noff = sizeof(struct ip6_hdr);
712 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
713 sizeof(struct icmp6_hdr) + 4 + maxhlen;
714 nicmp6->icmp6_type = ICMP6_WRUREPLY;
715 nicmp6->icmp6_code = 0;
718 ICMP6STAT_INC(icp6s_reflect);
719 ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]);
720 icmp6_reflect(n, noff);
730 case ND_ROUTER_SOLICIT:
731 icmp6_ifstat_inc(ifp, ifs6_in_routersolicit);
734 if (icmp6len < sizeof(struct nd_router_solicit))
736 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
739 /* Send incoming SeND packet to user space. */
740 if (send_sendso_input_hook != NULL) {
741 IP6_EXTHDR_CHECK(m, off,
742 icmp6len, IPPROTO_DONE);
743 error = send_sendso_input_hook(m, ifp,
745 /* -1 == no app on SEND socket */
747 return (IPPROTO_DONE);
748 nd6_rs_input(m, off, icmp6len);
750 nd6_rs_input(m, off, icmp6len);
754 if (send_sendso_input_hook != NULL) {
755 IP6_EXTHDR_CHECK(n, off,
756 icmp6len, IPPROTO_DONE);
757 error = send_sendso_input_hook(n, ifp,
761 /* -1 == no app on SEND socket */
762 nd6_rs_input(n, off, icmp6len);
764 nd6_rs_input(n, off, icmp6len);
768 case ND_ROUTER_ADVERT:
769 icmp6_ifstat_inc(ifp, ifs6_in_routeradvert);
772 if (icmp6len < sizeof(struct nd_router_advert))
774 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
776 /* Send incoming SeND-protected/ND packet to user space. */
777 if (send_sendso_input_hook != NULL) {
778 error = send_sendso_input_hook(m, ifp,
781 return (IPPROTO_DONE);
782 nd6_ra_input(m, off, icmp6len);
784 nd6_ra_input(m, off, icmp6len);
788 if (send_sendso_input_hook != NULL) {
789 error = send_sendso_input_hook(n, ifp,
793 nd6_ra_input(n, off, icmp6len);
795 nd6_ra_input(n, off, icmp6len);
799 case ND_NEIGHBOR_SOLICIT:
800 icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit);
803 if (icmp6len < sizeof(struct nd_neighbor_solicit))
805 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
806 if (send_sendso_input_hook != NULL) {
807 error = send_sendso_input_hook(m, ifp,
810 return (IPPROTO_DONE);
811 nd6_ns_input(m, off, icmp6len);
813 nd6_ns_input(m, off, icmp6len);
817 if (send_sendso_input_hook != NULL) {
818 error = send_sendso_input_hook(n, ifp,
822 nd6_ns_input(n, off, icmp6len);
824 nd6_ns_input(n, off, icmp6len);
828 case ND_NEIGHBOR_ADVERT:
829 icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert);
832 if (icmp6len < sizeof(struct nd_neighbor_advert))
834 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
836 /* Send incoming SeND-protected/ND packet to user space. */
837 if (send_sendso_input_hook != NULL) {
838 error = send_sendso_input_hook(m, ifp,
841 return (IPPROTO_DONE);
842 nd6_na_input(m, off, icmp6len);
844 nd6_na_input(m, off, icmp6len);
848 if (send_sendso_input_hook != NULL) {
849 error = send_sendso_input_hook(n, ifp,
853 nd6_na_input(n, off, icmp6len);
855 nd6_na_input(n, off, icmp6len);
860 icmp6_ifstat_inc(ifp, ifs6_in_redirect);
863 if (icmp6len < sizeof(struct nd_redirect))
865 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
866 if (send_sendso_input_hook != NULL) {
867 error = send_sendso_input_hook(m, ifp,
870 return (IPPROTO_DONE);
871 icmp6_redirect_input(m, off);
873 icmp6_redirect_input(m, off);
877 if (send_sendso_input_hook != NULL) {
878 error = send_sendso_input_hook(n, ifp,
882 icmp6_redirect_input(n, off);
884 icmp6_redirect_input(n, off);
888 case ICMP6_ROUTER_RENUMBERING:
889 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
890 code != ICMP6_ROUTER_RENUMBERING_RESULT)
892 if (icmp6len < sizeof(struct icmp6_router_renum))
898 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
899 icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
900 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
901 ifp ? ifp->if_index : 0));
902 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
903 /* ICMPv6 error: MUST deliver it by spec... */
907 /* ICMPv6 informational: MUST not deliver */
911 if (icmp6_notify_error(&m, off, icmp6len, code) != 0) {
912 /* In this case, m should've been freed. */
913 return (IPPROTO_DONE);
918 ICMP6STAT_INC(icp6s_badcode);
922 ICMP6STAT_INC(icp6s_badlen);
926 /* deliver the packet to appropriate sockets */
927 icmp6_rip6_input(&m, *offp);
937 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code)
939 struct mbuf *m = *mp;
940 struct icmp6_hdr *icmp6;
941 struct ip6_hdr *eip6;
943 struct sockaddr_in6 icmp6src, icmp6dst;
945 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
946 ICMP6STAT_INC(icp6s_tooshort);
949 #ifndef PULLDOWN_TEST
950 IP6_EXTHDR_CHECK(m, off,
951 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
952 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
954 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
955 sizeof(*icmp6) + sizeof(struct ip6_hdr));
957 ICMP6STAT_INC(icp6s_tooshort);
961 eip6 = (struct ip6_hdr *)(icmp6 + 1);
963 /* Detect the upper level protocol */
965 void (*ctlfunc)(int, struct sockaddr *, void *);
966 u_int8_t nxt = eip6->ip6_nxt;
967 int eoff = off + sizeof(struct icmp6_hdr) +
968 sizeof(struct ip6_hdr);
969 struct ip6ctlparam ip6cp;
970 struct in6_addr *finaldst = NULL;
971 int icmp6type = icmp6->icmp6_type;
973 struct ip6_rthdr *rth;
974 struct ip6_rthdr0 *rth0;
977 while (1) { /* XXX: should avoid infinite loop explicitly? */
981 case IPPROTO_HOPOPTS:
982 case IPPROTO_DSTOPTS:
984 #ifndef PULLDOWN_TEST
985 IP6_EXTHDR_CHECK(m, 0,
986 eoff + sizeof(struct ip6_ext), -1);
987 eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
989 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
992 ICMP6STAT_INC(icp6s_tooshort);
997 if (nxt == IPPROTO_AH)
998 eoff += (eh->ip6e_len + 2) << 2;
1000 eoff += (eh->ip6e_len + 1) << 3;
1003 case IPPROTO_ROUTING:
1005 * When the erroneous packet contains a
1006 * routing header, we should examine the
1007 * header to determine the final destination.
1008 * Otherwise, we can't properly update
1009 * information that depends on the final
1010 * destination (e.g. path MTU).
1012 #ifndef PULLDOWN_TEST
1013 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
1014 rth = (struct ip6_rthdr *)
1015 (mtod(m, caddr_t) + eoff);
1017 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
1018 eoff, sizeof(*rth));
1020 ICMP6STAT_INC(icp6s_tooshort);
1024 rthlen = (rth->ip6r_len + 1) << 3;
1026 * XXX: currently there is no
1027 * officially defined type other
1029 * Note that if the segment left field
1030 * is 0, all intermediate hops must
1033 if (rth->ip6r_segleft &&
1034 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
1037 #ifndef PULLDOWN_TEST
1038 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
1039 rth0 = (struct ip6_rthdr0 *)
1040 (mtod(m, caddr_t) + eoff);
1042 IP6_EXTHDR_GET(rth0,
1043 struct ip6_rthdr0 *, m,
1046 ICMP6STAT_INC(icp6s_tooshort);
1050 /* just ignore a bogus header */
1051 if ((rth0->ip6r0_len % 2) == 0 &&
1052 (hops = rth0->ip6r0_len/2))
1053 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
1056 nxt = rth->ip6r_nxt;
1058 case IPPROTO_FRAGMENT:
1059 #ifndef PULLDOWN_TEST
1060 IP6_EXTHDR_CHECK(m, 0, eoff +
1061 sizeof(struct ip6_frag), -1);
1062 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1065 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1068 ICMP6STAT_INC(icp6s_tooshort);
1073 * Data after a fragment header is meaningless
1074 * unless it is the first fragment, but
1075 * we'll go to the notify label for path MTU
1078 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1081 eoff += sizeof(struct ip6_frag);
1086 * This case includes ESP and the No Next
1087 * Header. In such cases going to the notify
1088 * label does not have any meaning
1089 * (i.e. ctlfunc will be NULL), but we go
1090 * anyway since we might have to update
1091 * path MTU information.
1097 #ifndef PULLDOWN_TEST
1098 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1100 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1101 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1102 if (icmp6 == NULL) {
1103 ICMP6STAT_INC(icp6s_tooshort);
1109 * retrieve parameters from the inner IPv6 header, and convert
1110 * them into sockaddr structures.
1111 * XXX: there is no guarantee that the source or destination
1112 * addresses of the inner packet are in the same scope as
1113 * the addresses of the icmp packet. But there is no other
1114 * way to determine the zone.
1116 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1118 bzero(&icmp6dst, sizeof(icmp6dst));
1119 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1120 icmp6dst.sin6_family = AF_INET6;
1121 if (finaldst == NULL)
1122 icmp6dst.sin6_addr = eip6->ip6_dst;
1124 icmp6dst.sin6_addr = *finaldst;
1125 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1127 bzero(&icmp6src, sizeof(icmp6src));
1128 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1129 icmp6src.sin6_family = AF_INET6;
1130 icmp6src.sin6_addr = eip6->ip6_src;
1131 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1133 icmp6src.sin6_flowinfo =
1134 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1136 if (finaldst == NULL)
1137 finaldst = &eip6->ip6_dst;
1139 ip6cp.ip6c_icmp6 = icmp6;
1140 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1141 ip6cp.ip6c_off = eoff;
1142 ip6cp.ip6c_finaldst = finaldst;
1143 ip6cp.ip6c_src = &icmp6src;
1144 ip6cp.ip6c_nxt = nxt;
1146 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1147 notifymtu = ntohl(icmp6->icmp6_mtu);
1148 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1149 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1152 ctlfunc = (void (*)(int, struct sockaddr *, void *))
1153 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1155 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1168 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1170 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1171 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1172 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1173 u_int mtu = ntohl(icmp6->icmp6_mtu);
1174 struct in_conninfo inc;
1178 * RFC2460 section 5, last paragraph.
1179 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1180 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1181 * due to packet translator in the middle.
1182 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1185 if (mtu < IPV6_MMTU)
1190 * we reject ICMPv6 too big with abnormally small value.
1191 * XXX what is the good definition of "abnormally small"?
1193 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1200 * In case the suggested mtu is less than IPV6_MMTU, we
1201 * only need to remember that it was for above mentioned
1202 * "alwaysfrag" case.
1203 * Try to be as close to the spec as possible.
1205 if (mtu < IPV6_MMTU)
1206 mtu = IPV6_MMTU - 8;
1208 bzero(&inc, sizeof(inc));
1209 inc.inc_fibnum = M_GETFIB(m);
1210 inc.inc_flags |= INC_ISIPV6;
1211 inc.inc6_faddr = *dst;
1212 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1215 if (mtu < tcp_maxmtu6(&inc, NULL)) {
1216 tcp_hc_updatemtu(&inc, mtu);
1217 ICMP6STAT_INC(icp6s_pmtuchg);
1222 * Process a Node Information Query packet, based on
1223 * draft-ietf-ipngwg-icmp-name-lookups-07.
1225 * Spec incompatibilities:
1226 * - IPv6 Subject address handling
1227 * - IPv4 Subject address handling support missing
1228 * - Proxy reply (answer even if it's not for me)
1229 * - joins NI group address at in6_ifattach() time only, does not cope
1230 * with hostname changes by sethostname(3)
1232 static struct mbuf *
1233 ni6_input(struct mbuf *m, int off)
1235 struct icmp6_nodeinfo *ni6, *nni6;
1236 struct mbuf *n = NULL;
1240 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1241 struct ni_reply_fqdn *fqdn;
1242 int addrs; /* for NI_QTYPE_NODEADDR */
1243 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1244 struct in6_addr in6_subj; /* subject address */
1245 struct ip6_hdr *ip6;
1246 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1248 struct in6_ifaddr *ia6 = NULL;
1250 ip6 = mtod(m, struct ip6_hdr *);
1251 #ifndef PULLDOWN_TEST
1252 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1254 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1256 /* m is already reclaimed */
1262 * Validate IPv6 source address.
1263 * The default configuration MUST be to refuse answering queries from
1264 * global-scope addresses according to RFC4602.
1266 * - it's not very clear what "refuse" means; this implementation
1268 * - it's not very easy to identify global-scope (unicast) addresses
1269 * since there are many prefixes for them. It should be safer
1270 * and in practice sufficient to check "all" but loopback and
1271 * link-local (note that site-local unicast was deprecated and
1272 * ULA is defined as global scope-wise)
1274 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1275 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1276 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1280 * Validate IPv6 destination address.
1282 * The Responder must discard the Query without further processing
1283 * unless it is one of the Responder's unicast or anycast addresses, or
1284 * a link-local scope multicast address which the Responder has joined.
1285 * [RFC4602, Section 5.]
1287 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1288 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1290 /* else it's a link-local multicast, fine */
1291 } else { /* unicast or anycast */
1292 ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
1294 goto bad; /* XXX impossible */
1296 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1297 !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1298 ifa_free(&ia6->ia_ifa);
1299 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1300 "a temporary address in %s:%d",
1301 __FILE__, __LINE__));
1304 ifa_free(&ia6->ia_ifa);
1307 /* validate query Subject field. */
1308 qtype = ntohs(ni6->ni_qtype);
1309 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1312 case NI_QTYPE_SUPTYPES:
1314 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1318 case NI_QTYPE_NODEADDR:
1319 case NI_QTYPE_IPV4ADDR:
1320 switch (ni6->ni_code) {
1321 case ICMP6_NI_SUBJ_IPV6:
1322 #if ICMP6_NI_SUBJ_IPV6 != 0
1326 * backward compatibility - try to accept 03 draft
1327 * format, where no Subject is present.
1329 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1334 #if ICMP6_NI_SUBJ_IPV6 != 0
1335 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1339 if (subjlen != sizeof(struct in6_addr))
1343 * Validate Subject address.
1345 * Not sure what exactly "address belongs to the node"
1346 * means in the spec, is it just unicast, or what?
1348 * At this moment we consider Subject address as
1349 * "belong to the node" if the Subject address equals
1350 * to the IPv6 destination address; validation for
1351 * IPv6 destination address should have done enough
1354 * We do not do proxy at this moment.
1356 /* m_pulldown instead of copy? */
1357 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1358 subjlen, (caddr_t)&in6_subj);
1359 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1362 subj = (char *)&in6_subj;
1363 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1367 * XXX if we are to allow other cases, we should really
1368 * be careful about scope here.
1369 * basically, we should disallow queries toward IPv6
1370 * destination X with subject Y,
1371 * if scope(X) > scope(Y).
1372 * if we allow scope(X) > scope(Y), it will result in
1373 * information leakage across scope boundary.
1377 case ICMP6_NI_SUBJ_FQDN:
1379 * Validate Subject name with gethostname(3).
1381 * The behavior may need some debate, since:
1382 * - we are not sure if the node has FQDN as
1383 * hostname (returned by gethostname(3)).
1384 * - the code does wildcard match for truncated names.
1385 * however, we are not sure if we want to perform
1386 * wildcard match, if gethostname(3) side has
1387 * truncated hostname.
1389 pr = curthread->td_ucred->cr_prison;
1390 mtx_lock(&pr->pr_mtx);
1391 n = ni6_nametodns(pr->pr_hostname,
1392 strlen(pr->pr_hostname), 0);
1393 mtx_unlock(&pr->pr_mtx);
1394 if (!n || n->m_next || n->m_len == 0)
1396 IP6_EXTHDR_GET(subj, char *, m,
1397 off + sizeof(struct icmp6_nodeinfo), subjlen);
1400 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1408 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1415 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1418 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1421 case NI_QTYPE_NODEADDR:
1422 case NI_QTYPE_IPV4ADDR:
1423 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1428 /* guess reply length */
1431 break; /* no reply data */
1432 case NI_QTYPE_SUPTYPES:
1433 replylen += sizeof(u_int32_t);
1436 /* XXX will append an mbuf */
1437 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1439 case NI_QTYPE_NODEADDR:
1440 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1441 if ((replylen += addrs * (sizeof(struct in6_addr) +
1442 sizeof(u_int32_t))) > MCLBYTES)
1443 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1445 case NI_QTYPE_IPV4ADDR:
1446 /* unsupported - should respond with unknown Qtype? */
1450 * XXX: We must return a reply with the ICMP6 code
1451 * `unknown Qtype' in this case. However we regard the case
1452 * as an FQDN query for backward compatibility.
1453 * Older versions set a random value to this field,
1454 * so it rarely varies in the defined qtypes.
1455 * But the mechanism is not reliable...
1456 * maybe we should obsolete older versions.
1458 qtype = NI_QTYPE_FQDN;
1459 /* XXX will append an mbuf */
1460 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1465 /* Allocate an mbuf to reply. */
1466 if (replylen > MCLBYTES) {
1468 * XXX: should we try to allocate more? But MCLBYTES
1469 * is probably much larger than IPV6_MMTU...
1473 if (replylen > MHLEN)
1474 n = m_getcl(M_NOWAIT, m->m_type, M_PKTHDR);
1476 n = m_gethdr(M_NOWAIT, m->m_type);
1481 m_move_pkthdr(n, m); /* just for recvif and FIB */
1482 n->m_pkthdr.len = n->m_len = replylen;
1484 /* copy mbuf header and IPv6 + Node Information base headers */
1485 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1486 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1487 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1489 /* qtype dependent procedure */
1492 nni6->ni_code = ICMP6_NI_SUCCESS;
1495 case NI_QTYPE_SUPTYPES:
1498 nni6->ni_code = ICMP6_NI_SUCCESS;
1499 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1500 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1501 v = (u_int32_t)htonl(0x0000000f);
1502 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1506 nni6->ni_code = ICMP6_NI_SUCCESS;
1507 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1508 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1509 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1510 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1512 * XXX do we really have FQDN in hostname?
1514 pr = curthread->td_ucred->cr_prison;
1515 mtx_lock(&pr->pr_mtx);
1516 n->m_next = ni6_nametodns(pr->pr_hostname,
1517 strlen(pr->pr_hostname), oldfqdn);
1518 mtx_unlock(&pr->pr_mtx);
1519 if (n->m_next == NULL)
1521 /* XXX we assume that n->m_next is not a chain */
1522 if (n->m_next->m_next != NULL)
1524 n->m_pkthdr.len += n->m_next->m_len;
1526 case NI_QTYPE_NODEADDR:
1530 nni6->ni_code = ICMP6_NI_SUCCESS;
1531 n->m_pkthdr.len = n->m_len =
1532 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1533 lenlim = M_TRAILINGSPACE(n);
1534 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1535 /* XXX: reset mbuf length */
1536 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1537 sizeof(struct icmp6_nodeinfo) + copied;
1541 break; /* XXX impossible! */
1544 nni6->ni_type = ICMP6_NI_REPLY;
1556 * make a mbuf with DNS-encoded string. no compression support.
1558 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1559 * treated as truncated name (two \0 at the end). this is a wild guess.
1561 * old - return pascal string if non-zero
1563 static struct mbuf *
1564 ni6_nametodns(const char *name, int namelen, int old)
1576 /* Because MAXHOSTNAMELEN is usually 256, we use cluster mbuf. */
1578 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1580 m = m_get(M_NOWAIT, MT_DATA);
1586 *mtod(m, char *) = namelen;
1587 bcopy(name, mtod(m, char *) + 1, namelen);
1591 cp = mtod(m, char *);
1592 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1594 /* if not certain about my name, return empty buffer */
1599 * guess if it looks like shortened hostname, or FQDN.
1600 * shortened hostname needs two trailing "\0".
1603 for (p = name; p < name + namelen; p++) {
1604 if (*p && *p == '.')
1613 while (cp < ep && p < name + namelen) {
1615 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1617 /* result does not fit into mbuf */
1618 if (cp + i + 1 >= ep)
1621 * DNS label length restriction, RFC1035 page 8.
1622 * "i == 0" case is included here to avoid returning
1623 * 0-length label on "foo..bar".
1625 if (i <= 0 || i >= 64)
1631 if (p < name + namelen && *p == '.')
1635 if (cp + nterm >= ep)
1639 m->m_len = cp - mtod(m, char *);
1643 panic("should not reach here");
1653 * check if two DNS-encoded string matches. takes care of truncated
1654 * form (with \0\0 at the end). no compression support.
1655 * XXX upper/lowercase match (see RFC2065)
1658 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1660 const char *a0, *b0;
1663 /* simplest case - need validation? */
1664 if (alen == blen && bcmp(a, b, alen) == 0)
1670 /* termination is mandatory */
1671 if (alen < 2 || blen < 2)
1673 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1678 while (a - a0 < alen && b - b0 < blen) {
1679 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1682 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1684 /* we don't support compression yet */
1685 if (a[0] >= 64 || b[0] >= 64)
1688 /* truncated case */
1689 if (a[0] == 0 && a - a0 == alen - 1)
1691 if (b[0] == 0 && b - b0 == blen - 1)
1693 if (a[0] == 0 || b[0] == 0)
1699 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1701 if (bcmp(a + 1, b + 1, l) != 0)
1708 if (a - a0 == alen && b - b0 == blen)
1715 * calculate the number of addresses to be returned in the node info reply.
1718 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1719 struct in6_addr *subj)
1722 struct in6_ifaddr *ifa6;
1724 int addrs = 0, addrsofif, iffound = 0;
1725 int niflags = ni6->ni_flags;
1727 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1728 switch (ni6->ni_code) {
1729 case ICMP6_NI_SUBJ_IPV6:
1730 if (subj == NULL) /* must be impossible... */
1735 * XXX: we only support IPv6 subject address for
1742 IFNET_RLOCK_NOSLEEP();
1743 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1746 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1747 if (ifa->ifa_addr->sa_family != AF_INET6)
1749 ifa6 = (struct in6_ifaddr *)ifa;
1751 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1752 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1756 * IPv4-mapped addresses can only be returned by a
1757 * Node Information proxy, since they represent
1758 * addresses of IPv4-only nodes, which perforce do
1759 * not implement this protocol.
1760 * [icmp-name-lookups-07, Section 5.4]
1761 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1762 * this function at this moment.
1765 /* What do we have to do about ::1? */
1766 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1767 case IPV6_ADDR_SCOPE_LINKLOCAL:
1768 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1771 case IPV6_ADDR_SCOPE_SITELOCAL:
1772 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1775 case IPV6_ADDR_SCOPE_GLOBAL:
1776 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1784 * check if anycast is okay.
1785 * XXX: just experimental. not in the spec.
1787 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1788 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1789 continue; /* we need only unicast addresses */
1790 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1791 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1794 addrsofif++; /* count the address */
1796 IF_ADDR_RUNLOCK(ifp);
1799 IFNET_RUNLOCK_NOSLEEP();
1805 IFNET_RUNLOCK_NOSLEEP();
1811 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1812 struct ifnet *ifp0, int resid)
1815 struct in6_ifaddr *ifa6;
1817 struct ifnet *ifp_dep = NULL;
1818 int copied = 0, allow_deprecated = 0;
1819 u_char *cp = (u_char *)(nni6 + 1);
1820 int niflags = ni6->ni_flags;
1823 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1824 return (0); /* needless to copy */
1826 IFNET_RLOCK_NOSLEEP();
1827 ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet);
1830 for (; ifp; ifp = TAILQ_NEXT(ifp, if_link)) {
1832 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1833 if (ifa->ifa_addr->sa_family != AF_INET6)
1835 ifa6 = (struct in6_ifaddr *)ifa;
1837 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1838 allow_deprecated == 0) {
1840 * prefererred address should be put before
1841 * deprecated addresses.
1844 /* record the interface for later search */
1845 if (ifp_dep == NULL)
1849 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1850 allow_deprecated != 0)
1851 continue; /* we now collect deprecated addrs */
1853 /* What do we have to do about ::1? */
1854 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1855 case IPV6_ADDR_SCOPE_LINKLOCAL:
1856 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1859 case IPV6_ADDR_SCOPE_SITELOCAL:
1860 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1863 case IPV6_ADDR_SCOPE_GLOBAL:
1864 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1872 * check if anycast is okay.
1873 * XXX: just experimental. not in the spec.
1875 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1876 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1878 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1879 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1883 /* now we can copy the address */
1884 if (resid < sizeof(struct in6_addr) +
1885 sizeof(u_int32_t)) {
1886 IF_ADDR_RUNLOCK(ifp);
1888 * We give up much more copy.
1889 * Set the truncate flag and return.
1891 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1892 IFNET_RUNLOCK_NOSLEEP();
1897 * Set the TTL of the address.
1898 * The TTL value should be one of the following
1899 * according to the specification:
1901 * 1. The remaining lifetime of a DHCP lease on the
1903 * 2. The remaining Valid Lifetime of a prefix from
1904 * which the address was derived through Stateless
1905 * Autoconfiguration.
1907 * Note that we currently do not support stateful
1908 * address configuration by DHCPv6, so the former
1909 * case can't happen.
1911 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1912 ltime = ND6_INFINITE_LIFETIME;
1914 if (ifa6->ia6_lifetime.ia6t_expire >
1916 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_uptime);
1921 bcopy(<ime, cp, sizeof(u_int32_t));
1922 cp += sizeof(u_int32_t);
1924 /* copy the address itself */
1925 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1926 sizeof(struct in6_addr));
1927 in6_clearscope((struct in6_addr *)cp); /* XXX */
1928 cp += sizeof(struct in6_addr);
1930 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1931 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1933 IF_ADDR_RUNLOCK(ifp);
1934 if (ifp0) /* we need search only on the specified IF */
1938 if (allow_deprecated == 0 && ifp_dep != NULL) {
1940 allow_deprecated = 1;
1945 IFNET_RUNLOCK_NOSLEEP();
1951 * XXX almost dup'ed code with rip6_input.
1954 icmp6_rip6_input(struct mbuf **mp, int off)
1956 struct mbuf *m = *mp;
1957 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1959 struct inpcb *last = NULL;
1960 struct sockaddr_in6 fromsa;
1961 struct icmp6_hdr *icmp6;
1962 struct mbuf *opts = NULL;
1964 #ifndef PULLDOWN_TEST
1965 /* this is assumed to be safe. */
1966 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1968 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1969 if (icmp6 == NULL) {
1970 /* m is already reclaimed */
1971 return (IPPROTO_DONE);
1976 * XXX: the address may have embedded scope zone ID, which should be
1977 * hidden from applications.
1979 bzero(&fromsa, sizeof(fromsa));
1980 fromsa.sin6_family = AF_INET6;
1981 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1982 fromsa.sin6_addr = ip6->ip6_src;
1983 if (sa6_recoverscope(&fromsa)) {
1985 return (IPPROTO_DONE);
1988 INP_INFO_RLOCK(&V_ripcbinfo);
1989 LIST_FOREACH(in6p, &V_ripcb, inp_list) {
1990 if ((in6p->inp_vflag & INP_IPV6) == 0)
1992 if (in6p->inp_ip_p != IPPROTO_ICMPV6)
1994 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1995 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1997 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1998 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
2001 if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
2002 in6p->in6p_icmp6filt)) {
2007 struct mbuf *n = NULL;
2010 * Recent network drivers tend to allocate a single
2011 * mbuf cluster, rather than to make a couple of
2012 * mbufs without clusters. Also, since the IPv6 code
2013 * path tries to avoid m_pullup(), it is highly
2014 * probable that we still have an mbuf cluster here
2015 * even though the necessary length can be stored in an
2016 * mbuf's internal buffer.
2017 * Meanwhile, the default size of the receive socket
2018 * buffer for raw sockets is not so large. This means
2019 * the possibility of packet loss is relatively higher
2020 * than before. To avoid this scenario, we copy the
2021 * received data to a separate mbuf that does not use
2022 * a cluster, if possible.
2023 * XXX: it is better to copy the data after stripping
2024 * intermediate headers.
2026 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2027 m->m_len <= MHLEN) {
2028 n = m_get(M_NOWAIT, m->m_type);
2030 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2031 bcopy(m->m_data, n->m_data,
2033 n->m_len = m->m_len;
2041 (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
2042 if (last->inp_flags & INP_CONTROLOPTS)
2043 ip6_savecontrol(last, n, &opts);
2044 /* strip intermediate headers */
2046 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2047 if (sbappendaddr_locked(
2048 &last->inp_socket->so_rcv,
2049 (struct sockaddr *)&fromsa, n, opts)
2051 /* should notify about lost packet */
2057 &last->inp_socket->so_rcv);
2059 sorwakeup_locked(last->inp_socket);
2066 INP_INFO_RUNLOCK(&V_ripcbinfo);
2068 if (last->inp_flags & INP_CONTROLOPTS)
2069 ip6_savecontrol(last, m, &opts);
2070 /* strip intermediate headers */
2073 /* avoid using mbuf clusters if possible (see above) */
2074 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2075 m->m_len <= MHLEN) {
2078 n = m_get(M_NOWAIT, m->m_type);
2080 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2081 bcopy(m->m_data, n->m_data, m->m_len);
2082 n->m_len = m->m_len;
2092 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2093 if (sbappendaddr_locked(&last->inp_socket->so_rcv,
2094 (struct sockaddr *)&fromsa, m, opts) == 0) {
2098 SOCKBUF_UNLOCK(&last->inp_socket->so_rcv);
2100 sorwakeup_locked(last->inp_socket);
2104 IP6STAT_DEC(ip6s_delivered);
2106 return IPPROTO_DONE;
2110 * Reflect the ip6 packet back to the source.
2111 * OFF points to the icmp6 header, counted from the top of the mbuf.
2114 icmp6_reflect(struct mbuf *m, size_t off)
2116 struct in6_addr src, *srcp = NULL;
2117 struct ip6_hdr *ip6;
2118 struct icmp6_hdr *icmp6;
2119 struct in6_ifaddr *ia = NULL;
2120 struct ifnet *outif = NULL;
2124 /* too short to reflect */
2125 if (off < sizeof(struct ip6_hdr)) {
2127 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2128 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2129 __FILE__, __LINE__));
2134 * If there are extra headers between IPv6 and ICMPv6, strip
2135 * off that header first.
2138 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2139 panic("assumption failed in icmp6_reflect");
2141 if (off > sizeof(struct ip6_hdr)) {
2143 struct ip6_hdr nip6;
2145 l = off - sizeof(struct ip6_hdr);
2146 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2148 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2150 if ((m = m_pullup(m, l)) == NULL)
2153 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2154 } else /* off == sizeof(struct ip6_hdr) */ {
2156 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2158 if ((m = m_pullup(m, l)) == NULL)
2162 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2163 ip6 = mtod(m, struct ip6_hdr *);
2164 ip6->ip6_nxt = IPPROTO_ICMPV6;
2165 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2166 type = icmp6->icmp6_type; /* keep type for statistics */
2167 code = icmp6->icmp6_code; /* ditto. */
2170 * If the incoming packet was addressed directly to us (i.e. unicast),
2171 * use dst as the src for the reply.
2172 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2173 * (for example) when we encounter an error while forwarding procedure
2174 * destined to a duplicated address of ours.
2176 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
2177 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
2178 if (ia != NULL && !(ia->ia6_flags &
2179 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2180 srcp = &ia->ia_addr.sin6_addr;
2185 struct sockaddr_in6 sin6;
2186 struct route_in6 ro;
2189 * This case matches to multicasts, our anycast, or unicasts
2190 * that we do not own. Select a source address based on the
2191 * source address of the erroneous packet.
2193 bzero(&sin6, sizeof(sin6));
2194 sin6.sin6_family = AF_INET6;
2195 sin6.sin6_len = sizeof(sin6);
2196 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2198 bzero(&ro, sizeof(ro));
2199 e = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &src);
2201 RTFREE(ro.ro_rt); /* XXX: we could use this */
2203 char ip6buf[INET6_ADDRSTRLEN];
2205 "icmp6_reflect: source can't be determined: "
2206 "dst=%s, error=%d\n",
2207 ip6_sprintf(ip6buf, &sin6.sin6_addr), e));
2213 * ip6_input() drops a packet if its src is multicast.
2214 * So, the src is never multicast.
2216 ip6->ip6_dst = ip6->ip6_src;
2217 ip6->ip6_src = *srcp;
2219 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2220 ip6->ip6_vfc |= IPV6_VERSION;
2221 ip6->ip6_nxt = IPPROTO_ICMPV6;
2223 ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2224 else if (m->m_pkthdr.rcvif) {
2225 /* XXX: This may not be the outgoing interface */
2226 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2228 ip6->ip6_hlim = V_ip6_defhlim;
2230 icmp6->icmp6_cksum = 0;
2231 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2232 sizeof(struct ip6_hdr), plen);
2235 * XXX option handling
2238 m->m_flags &= ~(M_BCAST|M_MCAST);
2240 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2242 icmp6_ifoutstat_inc(outif, type, code);
2245 ifa_free(&ia->ia_ifa);
2250 ifa_free(&ia->ia_ifa);
2256 icmp6_fasttimo(void)
2263 icmp6_slowtimo(void)
2270 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2271 struct in6_addr *tgt6)
2273 static char buf[1024];
2274 char ip6bufs[INET6_ADDRSTRLEN];
2275 char ip6bufd[INET6_ADDRSTRLEN];
2276 char ip6buft[INET6_ADDRSTRLEN];
2277 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2278 ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2279 ip6_sprintf(ip6buft, tgt6));
2284 icmp6_redirect_input(struct mbuf *m, int off)
2287 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2288 struct nd_redirect *nd_rd;
2289 int icmp6len = ntohs(ip6->ip6_plen);
2290 char *lladdr = NULL;
2292 struct rtentry *rt = NULL;
2295 struct in6_addr src6 = ip6->ip6_src;
2296 struct in6_addr redtgt6;
2297 struct in6_addr reddst6;
2298 union nd_opts ndopts;
2299 char ip6buf[INET6_ADDRSTRLEN];
2302 KASSERT(m->m_pkthdr.rcvif != NULL, ("%s: no rcvif", __func__));
2304 ifp = m->m_pkthdr.rcvif;
2306 /* XXX if we are router, we don't update route by icmp6 redirect */
2307 if (V_ip6_forwarding)
2309 if (!V_icmp6_rediraccept)
2312 #ifndef PULLDOWN_TEST
2313 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2314 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2316 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2317 if (nd_rd == NULL) {
2318 ICMP6STAT_INC(icp6s_tooshort);
2322 redtgt6 = nd_rd->nd_rd_target;
2323 reddst6 = nd_rd->nd_rd_dst;
2325 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2326 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2331 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2333 "ICMP6 redirect sent from %s rejected; "
2334 "must be from linklocal\n",
2335 ip6_sprintf(ip6buf, &src6)));
2338 if (ip6->ip6_hlim != 255) {
2340 "ICMP6 redirect sent from %s rejected; "
2341 "hlim=%d (must be 255)\n",
2342 ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
2346 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2347 struct sockaddr_in6 sin6;
2348 struct in6_addr *gw6;
2350 bzero(&sin6, sizeof(sin6));
2351 sin6.sin6_family = AF_INET6;
2352 sin6.sin6_len = sizeof(struct sockaddr_in6);
2353 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2354 rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL, RT_DEFAULT_FIB);
2356 if (rt->rt_gateway == NULL ||
2357 rt->rt_gateway->sa_family != AF_INET6) {
2360 "ICMP6 redirect rejected; no route "
2361 "with inet6 gateway found for redirect dst: %s\n",
2362 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2366 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2367 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2370 "ICMP6 redirect rejected; "
2371 "not equal to gw-for-src=%s (must be same): "
2373 ip6_sprintf(ip6buf, gw6),
2374 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2379 "ICMP6 redirect rejected; "
2380 "no route found for redirect dst: %s\n",
2381 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2387 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2389 "ICMP6 redirect rejected; "
2390 "redirect dst must be unicast: %s\n",
2391 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2395 is_router = is_onlink = 0;
2396 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2397 is_router = 1; /* router case */
2398 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2399 is_onlink = 1; /* on-link destination case */
2400 if (!is_router && !is_onlink) {
2402 "ICMP6 redirect rejected; "
2403 "neither router case nor onlink case: %s\n",
2404 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2408 icmp6len -= sizeof(*nd_rd);
2409 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2410 if (nd6_options(&ndopts) < 0) {
2411 nd6log((LOG_INFO, "%s: invalid ND option, rejected: %s\n",
2412 __func__, icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2413 /* nd6_options have incremented stats */
2417 if (ndopts.nd_opts_tgt_lladdr) {
2418 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2419 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2422 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2423 nd6log((LOG_INFO, "%s: lladdrlen mismatch for %s "
2424 "(if %d, icmp6 packet %d): %s\n",
2425 __func__, ip6_sprintf(ip6buf, &redtgt6),
2426 ifp->if_addrlen, lladdrlen - 2,
2427 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2431 /* Validation passed. */
2434 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2435 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2438 * Install a gateway route in the better-router case or an interface
2439 * route in the on-link-destination case.
2442 struct sockaddr_in6 sdst;
2443 struct sockaddr_in6 sgw;
2444 struct sockaddr_in6 ssrc;
2445 struct sockaddr *gw;
2449 bzero(&sdst, sizeof(sdst));
2450 bzero(&ssrc, sizeof(ssrc));
2451 sdst.sin6_family = ssrc.sin6_family = AF_INET6;
2452 sdst.sin6_len = ssrc.sin6_len = sizeof(struct sockaddr_in6);
2453 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2454 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2455 rt_flags = RTF_HOST;
2457 bzero(&sgw, sizeof(sgw));
2458 sgw.sin6_family = AF_INET6;
2459 sgw.sin6_len = sizeof(struct sockaddr_in6);
2460 bcopy(&redtgt6, &sgw.sin6_addr,
2461 sizeof(struct in6_addr));
2462 gw = (struct sockaddr *)&sgw;
2463 rt_flags |= RTF_GATEWAY;
2465 gw = ifp->if_addr->ifa_addr;
2466 for (fibnum = 0; fibnum < rt_numfibs; fibnum++)
2467 in6_rtredirect((struct sockaddr *)&sdst, gw,
2468 (struct sockaddr *)NULL, rt_flags,
2469 (struct sockaddr *)&ssrc, fibnum);
2471 /* finally update cached route in each socket via pfctlinput */
2473 struct sockaddr_in6 sdst;
2475 bzero(&sdst, sizeof(sdst));
2476 sdst.sin6_family = AF_INET6;
2477 sdst.sin6_len = sizeof(struct sockaddr_in6);
2478 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2479 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2487 ICMP6STAT_INC(icp6s_badredirect);
2492 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2494 struct ifnet *ifp; /* my outgoing interface */
2495 struct in6_addr *ifp_ll6;
2496 struct in6_addr *router_ll6;
2497 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2498 struct mbuf *m = NULL; /* newly allocated one */
2500 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2501 struct nd_redirect *nd_rd;
2502 struct llentry *ln = NULL;
2505 struct ifnet *outif = NULL;
2506 struct sockaddr_in6 src_sa;
2508 icmp6_errcount(ND_REDIRECT, 0);
2510 /* if we are not router, we don't send icmp6 redirect */
2511 if (!V_ip6_forwarding)
2515 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2520 * the source address must identify a neighbor, and
2521 * the destination address must not be a multicast address
2522 * [RFC 2461, sec 8.2]
2524 sip6 = mtod(m0, struct ip6_hdr *);
2525 bzero(&src_sa, sizeof(src_sa));
2526 src_sa.sin6_family = AF_INET6;
2527 src_sa.sin6_len = sizeof(src_sa);
2528 src_sa.sin6_addr = sip6->ip6_src;
2529 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2531 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2532 goto fail; /* what should we do here? */
2535 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2539 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2540 * we almost always ask for an mbuf cluster for simplicity.
2541 * (MHLEN < IPV6_MMTU is almost always true)
2543 #if IPV6_MMTU >= MCLBYTES
2544 # error assumption failed about IPV6_MMTU and MCLBYTES
2546 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2549 M_SETFIB(m, rt->rt_fibnum);
2550 maxlen = M_TRAILINGSPACE(m);
2551 maxlen = min(IPV6_MMTU, maxlen);
2552 /* just for safety */
2553 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2554 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2559 /* get ip6 linklocal address for ifp(my outgoing interface). */
2560 struct in6_ifaddr *ia;
2561 if ((ia = in6ifa_ifpforlinklocal(ifp,
2563 IN6_IFF_ANYCAST)) == NULL)
2565 ifp_ll6 = &ia->ia_addr.sin6_addr;
2566 /* XXXRW: reference released prematurely. */
2567 ifa_free(&ia->ia_ifa);
2570 /* get ip6 linklocal address for the router. */
2571 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2572 struct sockaddr_in6 *sin6;
2573 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2574 router_ll6 = &sin6->sin6_addr;
2575 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2576 router_ll6 = (struct in6_addr *)NULL;
2578 router_ll6 = (struct in6_addr *)NULL;
2581 ip6 = mtod(m, struct ip6_hdr *);
2583 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2584 ip6->ip6_vfc |= IPV6_VERSION;
2585 /* ip6->ip6_plen will be set later */
2586 ip6->ip6_nxt = IPPROTO_ICMPV6;
2587 ip6->ip6_hlim = 255;
2588 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2589 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2590 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2593 nd_rd = (struct nd_redirect *)(ip6 + 1);
2594 nd_rd->nd_rd_type = ND_REDIRECT;
2595 nd_rd->nd_rd_code = 0;
2596 nd_rd->nd_rd_reserved = 0;
2597 if (rt->rt_flags & RTF_GATEWAY) {
2599 * nd_rd->nd_rd_target must be a link-local address in
2600 * better router cases.
2604 bcopy(router_ll6, &nd_rd->nd_rd_target,
2605 sizeof(nd_rd->nd_rd_target));
2606 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2607 sizeof(nd_rd->nd_rd_dst));
2609 /* make sure redtgt == reddst */
2610 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2611 sizeof(nd_rd->nd_rd_target));
2612 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2613 sizeof(nd_rd->nd_rd_dst));
2616 p = (u_char *)(nd_rd + 1);
2622 /* target lladdr option */
2624 struct nd_opt_hdr *nd_opt;
2627 IF_AFDATA_RLOCK(ifp);
2628 ln = nd6_lookup(router_ll6, 0, ifp);
2629 IF_AFDATA_RUNLOCK(ifp);
2633 len = sizeof(*nd_opt) + ifp->if_addrlen;
2634 len = (len + 7) & ~7; /* round by 8 */
2636 if (len + (p - (u_char *)ip6) > maxlen)
2639 if (ln->la_flags & LLE_VALID) {
2640 nd_opt = (struct nd_opt_hdr *)p;
2641 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2642 nd_opt->nd_opt_len = len >> 3;
2643 lladdr = (char *)(nd_opt + 1);
2644 bcopy(&ln->ll_addr, lladdr, ifp->if_addrlen);
2652 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2654 /* just to be safe */
2655 #ifdef M_DECRYPTED /*not openbsd*/
2656 if (m0->m_flags & M_DECRYPTED)
2659 if (p - (u_char *)ip6 > maxlen)
2663 /* redirected header option */
2665 struct nd_opt_rd_hdr *nd_opt_rh;
2668 * compute the maximum size for icmp6 redirect header option.
2669 * XXX room for auth header?
2671 len = maxlen - (p - (u_char *)ip6);
2674 /* This is just for simplicity. */
2675 if (m0->m_pkthdr.len != m0->m_len) {
2677 m_freem(m0->m_next);
2680 m0->m_pkthdr.len = m0->m_len;
2684 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2685 * about padding/truncate rule for the original IP packet.
2686 * From the discussion on IPv6imp in Feb 1999,
2687 * the consensus was:
2688 * - "attach as much as possible" is the goal
2689 * - pad if not aligned (original size can be guessed by
2690 * original ip6 header)
2691 * Following code adds the padding if it is simple enough,
2692 * and truncates if not.
2694 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2695 panic("assumption failed in %s:%d", __FILE__,
2698 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2699 /* not enough room, truncate */
2700 m0->m_pkthdr.len = m0->m_len = len -
2703 /* enough room, pad or truncate */
2706 extra = m0->m_pkthdr.len % 8;
2708 /* pad if easy enough, truncate if not */
2709 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2711 m0->m_len += (8 - extra);
2712 m0->m_pkthdr.len += (8 - extra);
2715 m0->m_pkthdr.len -= extra;
2719 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2720 m0->m_pkthdr.len = m0->m_len = len -
2724 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2725 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2726 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2727 nd_opt_rh->nd_opt_rh_len = len >> 3;
2728 p += sizeof(*nd_opt_rh);
2729 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2731 /* connect m0 to m */
2732 m_tag_delete_chain(m0, NULL);
2733 m0->m_flags &= ~M_PKTHDR;
2735 m->m_pkthdr.len = m->m_len + m0->m_len;
2744 /* XXX: clear embedded link IDs in the inner header */
2745 in6_clearscope(&sip6->ip6_src);
2746 in6_clearscope(&sip6->ip6_dst);
2747 in6_clearscope(&nd_rd->nd_rd_target);
2748 in6_clearscope(&nd_rd->nd_rd_dst);
2750 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2752 nd_rd->nd_rd_cksum = 0;
2753 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2754 sizeof(*ip6), ntohs(ip6->ip6_plen));
2756 if (send_sendso_input_hook != NULL) {
2757 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short),
2761 *(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type;
2762 m_tag_prepend(m, mtag);
2765 /* send the packet to outside... */
2766 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2768 icmp6_ifstat_inc(outif, ifs6_out_msg);
2769 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2771 ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]);
2783 * ICMPv6 socket option processing.
2786 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2790 struct inpcb *inp = sotoinpcb(so);
2791 int level, op, optname;
2794 level = sopt->sopt_level;
2795 op = sopt->sopt_dir;
2796 optname = sopt->sopt_name;
2797 optlen = sopt->sopt_valsize;
2799 level = op = optname = optlen = 0;
2801 if (level != IPPROTO_ICMPV6) {
2810 struct icmp6_filter ic6f;
2812 if (optlen != sizeof(ic6f)) {
2816 error = sooptcopyin(sopt, &ic6f, optlen, optlen);
2819 *inp->in6p_icmp6filt = ic6f;
2826 error = ENOPROTOOPT;
2835 struct icmp6_filter ic6f;
2838 ic6f = *inp->in6p_icmp6filt;
2840 error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
2845 error = ENOPROTOOPT;
2855 * Perform rate limit check.
2856 * Returns 0 if it is okay to send the icmp6 packet.
2857 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2860 * XXX per-destination/type check necessary?
2862 * dst - not used at this moment
2863 * type - not used at this moment
2864 * code - not used at this moment
2867 icmp6_ratelimit(const struct in6_addr *dst, const int type,
2872 ret = 0; /* okay to send */
2875 if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count,
2876 V_icmp6errppslim)) {
2877 /* The packet is subject to rate limit */