2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
68 #define MBUF_PRIVATE /* XXXRW: Optimisation tries to avoid M_EXT mbufs */
71 #include "opt_inet6.h"
73 #include <sys/param.h>
74 #include <sys/domain.h>
76 #include <sys/kernel.h>
78 #include <sys/malloc.h>
81 #include <sys/protosw.h>
82 #include <sys/signalvar.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
86 #include <sys/syslog.h>
87 #include <sys/systm.h>
91 #include <net/if_var.h>
92 #include <net/if_dl.h>
93 #include <net/if_llatbl.h>
94 #include <net/if_private.h>
95 #include <net/if_types.h>
96 #include <net/route.h>
97 #include <net/route/route_ctl.h>
98 #include <net/route/nhop.h>
101 #include <netinet/in.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip6.h>
105 #include <netinet/icmp6.h>
106 #include <netinet/tcp_var.h>
108 #include <netinet6/in6_fib.h>
109 #include <netinet6/in6_ifattach.h>
110 #include <netinet6/in6_pcb.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/scope6_var.h>
113 #include <netinet6/mld6_var.h>
114 #include <netinet6/nd6.h>
115 #include <netinet6/send.h>
117 extern ip6proto_ctlinput_t *ip6_ctlprotox[];
119 VNET_PCPUSTAT_DEFINE(struct icmp6stat, icmp6stat);
120 VNET_PCPUSTAT_SYSINIT(icmp6stat);
123 VNET_PCPUSTAT_SYSUNINIT(icmp6stat);
126 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
127 VNET_DECLARE(int, icmp6errppslim);
128 VNET_DEFINE_STATIC(int, icmp6errpps_count) = 0;
129 VNET_DEFINE_STATIC(struct timeval, icmp6errppslim_last);
130 VNET_DECLARE(int, icmp6_nodeinfo);
132 #define V_ripcbinfo VNET(ripcbinfo)
133 #define V_icmp6errppslim VNET(icmp6errppslim)
134 #define V_icmp6errpps_count VNET(icmp6errpps_count)
135 #define V_icmp6errppslim_last VNET(icmp6errppslim_last)
136 #define V_icmp6_nodeinfo VNET(icmp6_nodeinfo)
138 static void icmp6_errcount(int, int);
139 static int icmp6_rip6_input(struct mbuf **, int);
140 static void icmp6_reflect(struct mbuf *, size_t);
141 static const char *icmp6_redirect_diag(struct in6_addr *,
142 struct in6_addr *, struct in6_addr *);
143 static struct mbuf *ni6_input(struct mbuf *, int, struct prison *);
144 static struct mbuf *ni6_nametodns(const char *, int, int);
145 static int ni6_dnsmatch(const char *, int, const char *, int);
146 static int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *,
147 struct ifnet **, struct in6_addr *);
148 static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
149 struct ifnet *, int);
150 static int icmp6_notify_error(struct mbuf **, int, int);
153 * Kernel module interface for updating icmp6stat. The argument is an index
154 * into icmp6stat treated as an array of u_quad_t. While this encodes the
155 * general layout of icmp6stat into the caller, it doesn't encode its
156 * location, so that future changes to add, for example, per-CPU stats
157 * support won't cause binary compatibility problems for kernel modules.
160 kmod_icmp6stat_inc(int statnum)
163 counter_u64_add(VNET(icmp6stat)[statnum], 1);
167 icmp6_errcount(int type, int code)
170 case ICMP6_DST_UNREACH:
172 case ICMP6_DST_UNREACH_NOROUTE:
173 ICMP6STAT_INC(icp6s_odst_unreach_noroute);
175 case ICMP6_DST_UNREACH_ADMIN:
176 ICMP6STAT_INC(icp6s_odst_unreach_admin);
178 case ICMP6_DST_UNREACH_BEYONDSCOPE:
179 ICMP6STAT_INC(icp6s_odst_unreach_beyondscope);
181 case ICMP6_DST_UNREACH_ADDR:
182 ICMP6STAT_INC(icp6s_odst_unreach_addr);
184 case ICMP6_DST_UNREACH_NOPORT:
185 ICMP6STAT_INC(icp6s_odst_unreach_noport);
189 case ICMP6_PACKET_TOO_BIG:
190 ICMP6STAT_INC(icp6s_opacket_too_big);
192 case ICMP6_TIME_EXCEEDED:
194 case ICMP6_TIME_EXCEED_TRANSIT:
195 ICMP6STAT_INC(icp6s_otime_exceed_transit);
197 case ICMP6_TIME_EXCEED_REASSEMBLY:
198 ICMP6STAT_INC(icp6s_otime_exceed_reassembly);
202 case ICMP6_PARAM_PROB:
204 case ICMP6_PARAMPROB_HEADER:
205 ICMP6STAT_INC(icp6s_oparamprob_header);
207 case ICMP6_PARAMPROB_NEXTHEADER:
208 ICMP6STAT_INC(icp6s_oparamprob_nextheader);
210 case ICMP6_PARAMPROB_OPTION:
211 ICMP6STAT_INC(icp6s_oparamprob_option);
216 ICMP6STAT_INC(icp6s_oredirect);
219 ICMP6STAT_INC(icp6s_ounknown);
223 * A wrapper function for icmp6_error() necessary when the erroneous packet
224 * may not contain enough scope zone information.
227 icmp6_error2(struct mbuf *m, int type, int code, int param,
235 if (m->m_len < sizeof(struct ip6_hdr)) {
236 m = m_pullup(m, sizeof(struct ip6_hdr));
238 IP6STAT_INC(ip6s_exthdrtoolong);
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;
260 struct epoch_tracker et;
265 ICMP6STAT_INC(icp6s_error);
267 /* count per-type-code statistics */
268 icmp6_errcount(type, code);
270 #ifdef M_DECRYPTED /*not openbsd*/
271 if (m->m_flags & M_DECRYPTED) {
272 ICMP6STAT_INC(icp6s_canterror);
277 if (m->m_len < sizeof(struct ip6_hdr)) {
278 m = m_pullup(m, sizeof(struct ip6_hdr));
280 IP6STAT_INC(ip6s_exthdrtoolong);
284 oip6 = mtod(m, struct ip6_hdr *);
287 * If the destination address of the erroneous packet is a multicast
288 * address, or the packet was sent using link-layer multicast,
289 * we should basically suppress sending an error (RFC 2463, Section
291 * We have two exceptions (the item e.2 in that section):
292 * - the Packet Too Big message can be sent for path MTU discovery.
293 * - the Parameter Problem Message that can be allowed an icmp6 error
294 * in the option type field. This check has been done in
295 * ip6_unknown_opt(), so we can just check the type and code.
297 if ((m->m_flags & (M_BCAST|M_MCAST) ||
298 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
299 (type != ICMP6_PACKET_TOO_BIG &&
300 (type != ICMP6_PARAM_PROB ||
301 code != ICMP6_PARAMPROB_OPTION)))
305 * RFC 2463, 2.4 (e.5): source address check.
306 * XXX: the case of anycast source?
308 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
309 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
313 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
317 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
318 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
319 struct icmp6_hdr *icp;
321 if (m->m_len < off + sizeof(struct icmp6_hdr)) {
322 m = m_pullup(m, off + sizeof(struct icmp6_hdr));
324 IP6STAT_INC(ip6s_exthdrtoolong);
328 oip6 = mtod(m, struct ip6_hdr *);
329 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
331 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
332 icp->icmp6_type == ND_REDIRECT) {
335 * Special case: for redirect (which is
336 * informational) we must not send icmp6 error.
338 ICMP6STAT_INC(icp6s_canterror);
341 /* ICMPv6 informational - send the error */
344 /* non-ICMPv6 - send the error */
347 /* Finally, do rate limitation check. */
348 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
349 ICMP6STAT_INC(icp6s_toofreq);
354 * OK, ICMP6 can be generated.
357 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
358 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
360 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
361 M_PREPEND(m, preplen, M_NOWAIT); /* FIB is also copied over. */
363 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
367 nip6 = mtod(m, struct ip6_hdr *);
368 nip6->ip6_src = oip6->ip6_src;
369 nip6->ip6_dst = oip6->ip6_dst;
371 in6_clearscope(&oip6->ip6_src);
372 in6_clearscope(&oip6->ip6_dst);
374 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
375 icmp6->icmp6_type = type;
376 icmp6->icmp6_code = code;
377 icmp6->icmp6_pptr = htonl((u_int32_t)param);
379 ICMP6STAT_INC(icp6s_outhist[type]);
381 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
388 * If we can't tell whether or not we can generate ICMP6, free it.
394 icmp6_errmap(const struct icmp6_hdr *icmp6)
397 switch (icmp6->icmp6_type) {
398 case ICMP6_DST_UNREACH:
399 switch (icmp6->icmp6_code) {
400 case ICMP6_DST_UNREACH_NOROUTE:
401 case ICMP6_DST_UNREACH_ADDR:
402 return (EHOSTUNREACH);
403 case ICMP6_DST_UNREACH_NOPORT:
404 case ICMP6_DST_UNREACH_ADMIN:
405 return (ECONNREFUSED);
406 case ICMP6_DST_UNREACH_BEYONDSCOPE:
407 return (ENOPROTOOPT);
409 return (0); /* Shouldn't happen. */
411 case ICMP6_PACKET_TOO_BIG:
413 case ICMP6_TIME_EXCEEDED:
414 switch (icmp6->icmp6_code) {
415 case ICMP6_TIME_EXCEED_TRANSIT:
416 return (EHOSTUNREACH);
417 case ICMP6_TIME_EXCEED_REASSEMBLY:
420 return (0); /* Shouldn't happen. */
422 case ICMP6_PARAM_PROB:
423 switch (icmp6->icmp6_code) {
424 case ICMP6_PARAMPROB_NEXTHEADER:
425 return (ECONNREFUSED);
426 case ICMP6_PARAMPROB_HEADER:
427 case ICMP6_PARAMPROB_OPTION:
428 return (ENOPROTOOPT);
430 return (0); /* Shouldn't happen. */
438 * Process a received ICMP6 message.
441 icmp6_input(struct mbuf **mp, int *offp, int proto)
445 struct ip6_hdr *ip6, *nip6;
446 struct icmp6_hdr *icmp6, *nicmp6;
447 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
448 int code, error, icmp6len, ip6len, noff, off, sum;
455 if (m->m_len < off + sizeof(struct icmp6_hdr)) {
456 m = m_pullup(m, off + sizeof(struct icmp6_hdr));
458 IP6STAT_INC(ip6s_exthdrtoolong);
460 return (IPPROTO_DONE);
465 * Locate icmp6 structure in mbuf, and check
466 * that not corrupted and of at least minimum length
469 icmp6len = m->m_pkthdr.len - off;
470 if (icmp6len < sizeof(struct icmp6_hdr)) {
471 ICMP6STAT_INC(icp6s_tooshort);
475 ip6 = mtod(m, struct ip6_hdr *);
476 ifp = m->m_pkthdr.rcvif;
478 * Check multicast group membership.
479 * Note: SSM filters are not applied for ICMPv6 traffic.
481 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
482 struct in6_multi *inm;
484 inm = in6m_lookup(ifp, &ip6->ip6_dst);
486 IP6STAT_INC(ip6s_notmember);
487 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
492 /* Calculate the checksum. */
493 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
494 code = icmp6->icmp6_code;
495 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
497 "ICMP6 checksum error(%d|%x) %s\n",
498 icmp6->icmp6_type, sum,
499 ip6_sprintf(ip6bufs, &ip6->ip6_src)));
500 ICMP6STAT_INC(icp6s_checksum);
504 ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]);
505 icmp6_ifstat_inc(ifp, ifs6_in_msg);
506 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
507 icmp6_ifstat_inc(ifp, ifs6_in_error);
509 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
510 switch (icmp6->icmp6_type) {
511 case ICMP6_DST_UNREACH:
512 icmp6_ifstat_inc(ifp, ifs6_in_dstunreach);
514 case ICMP6_DST_UNREACH_ADMIN:
515 icmp6_ifstat_inc(ifp, ifs6_in_adminprohib);
516 case ICMP6_DST_UNREACH_NOROUTE:
517 case ICMP6_DST_UNREACH_ADDR:
518 case ICMP6_DST_UNREACH_BEYONDSCOPE:
519 case ICMP6_DST_UNREACH_NOPORT:
524 case ICMP6_PACKET_TOO_BIG:
525 icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig);
527 * Validation is made in icmp6_mtudisc_update.
528 * Updating the path MTU will be done after examining
529 * intermediate extension headers.
532 case ICMP6_TIME_EXCEEDED:
533 icmp6_ifstat_inc(ifp, ifs6_in_timeexceed);
535 case ICMP6_TIME_EXCEED_TRANSIT:
536 case ICMP6_TIME_EXCEED_REASSEMBLY:
541 case ICMP6_PARAM_PROB:
542 icmp6_ifstat_inc(ifp, ifs6_in_paramprob);
544 case ICMP6_PARAMPROB_NEXTHEADER:
545 case ICMP6_PARAMPROB_HEADER:
546 case ICMP6_PARAMPROB_OPTION:
551 case ICMP6_ECHO_REQUEST:
552 icmp6_ifstat_inc(ifp, ifs6_in_echo);
555 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
560 || n->m_len < off + sizeof(struct icmp6_hdr)) {
564 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) <= MHLEN);
565 n = m_gethdr(M_NOWAIT, n0->m_type);
572 m_move_pkthdr(n, n0); /* FIB copied. */
573 n0len = n0->m_pkthdr.len; /* save for use below */
575 * Copy IPv6 and ICMPv6 only.
577 nip6 = mtod(n, struct ip6_hdr *);
578 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
579 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
580 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
581 noff = sizeof(struct ip6_hdr);
582 /* new mbuf contains only ipv6+icmpv6 headers */
583 n->m_len = noff + sizeof(struct icmp6_hdr);
585 * Adjust mbuf. ip6_plen will be adjusted in
588 m_adj(n0, off + sizeof(struct icmp6_hdr));
589 /* recalculate complete packet size */
590 n->m_pkthdr.len = n0len + (noff - off);
593 if (n->m_len < off + sizeof(*nicmp6)) {
594 n = m_pullup(n, off + sizeof(*nicmp6));
596 IP6STAT_INC(ip6s_exthdrtoolong);
600 nicmp6 = (struct icmp6_hdr *)(mtod(n, caddr_t) + off);
604 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
605 nicmp6->icmp6_code = 0;
606 ICMP6STAT_INC(icp6s_reflect);
607 ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]);
608 icmp6_reflect(n, noff);
612 case ICMP6_ECHO_REPLY:
613 icmp6_ifstat_inc(ifp, ifs6_in_echoreply);
618 case MLD_LISTENER_QUERY:
619 case MLD_LISTENER_REPORT:
620 case MLD_LISTENER_DONE:
621 case MLDV2_LISTENER_REPORT:
623 * Drop MLD traffic which is not link-local, has a hop limit
624 * of greater than 1 hop, or which does not have the
625 * IPv6 HBH Router Alert option.
626 * As IPv6 HBH options are stripped in ip6_input() we must
627 * check an mbuf header flag.
628 * XXX Should we also sanity check that these messages
629 * were directed to a link-local multicast prefix?
631 if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0)
633 if (mld_input(&m, off, icmp6len) != 0) {
635 return (IPPROTO_DONE);
640 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
642 enum { WRU, FQDN } mode;
645 if (!V_icmp6_nodeinfo)
648 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
650 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
656 sx_slock(&allprison_lock);
657 TAILQ_FOREACH(pr, &allprison, pr_list)
658 if (pr->pr_vnet == ifp->if_vnet)
660 sx_sunlock(&allprison_lock);
662 pr = curthread->td_ucred->cr_prison;
664 if (m->m_len < off + sizeof(struct icmp6_nodeinfo)) {
665 m = m_pullup(m, off +
666 sizeof(struct icmp6_nodeinfo));
668 IP6STAT_INC(ip6s_exthdrtoolong);
670 return (IPPROTO_DONE);
673 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
675 n = ni6_input(n, off, pr);
676 /* XXX meaningless if n == NULL */
677 noff = sizeof(struct ip6_hdr);
683 * XXX: this combination of flags is pointless,
684 * but should we keep this for compatibility?
686 if ((V_icmp6_nodeinfo & (ICMP6_NODEINFO_FQDNOK |
687 ICMP6_NODEINFO_TMPADDROK)) !=
688 (ICMP6_NODEINFO_FQDNOK | ICMP6_NODEINFO_TMPADDROK))
694 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) + 4 <= MHLEN);
695 n = m_gethdr(M_NOWAIT, m->m_type);
700 if (!m_dup_pkthdr(n, m, M_NOWAIT)) {
702 * Previous code did a blind M_COPY_PKTHDR
703 * and said "just for rcvif". If true, then
704 * we could tolerate the dup failing (due to
705 * the deep copy of the tag chain). For now
706 * be conservative and just fail.
713 * Copy IPv6 and ICMPv6 only.
715 nip6 = mtod(n, struct ip6_hdr *);
716 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
717 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
718 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
719 p = (u_char *)(nicmp6 + 1);
722 maxhlen = M_TRAILINGSPACE(n) -
723 (sizeof(*nip6) + sizeof(*nicmp6) + 4);
724 mtx_lock(&pr->pr_mtx);
725 hlen = strlen(pr->pr_hostname);
728 /* meaningless TTL */
729 bcopy(pr->pr_hostname, p + 4, maxhlen);
730 mtx_unlock(&pr->pr_mtx);
731 noff = sizeof(struct ip6_hdr);
732 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
733 sizeof(struct icmp6_hdr) + 4 + maxhlen;
734 nicmp6->icmp6_type = ICMP6_WRUREPLY;
735 nicmp6->icmp6_code = 0;
738 ICMP6STAT_INC(icp6s_reflect);
739 ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]);
740 icmp6_reflect(n, noff);
750 case ND_ROUTER_SOLICIT:
751 icmp6_ifstat_inc(ifp, ifs6_in_routersolicit);
754 if (icmp6len < sizeof(struct nd_router_solicit))
756 if (send_sendso_input_hook != NULL) {
757 if (m->m_len < off + icmp6len) {
758 m = m_pullup(m, off + icmp6len);
760 IP6STAT_INC(ip6s_exthdrtoolong);
762 return (IPPROTO_DONE);
765 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
771 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
772 nd6_rs_input(m, off, icmp6len);
778 case ND_ROUTER_ADVERT:
779 icmp6_ifstat_inc(ifp, ifs6_in_routeradvert);
782 if (icmp6len < sizeof(struct nd_router_advert))
784 if (send_sendso_input_hook != NULL) {
785 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
791 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
792 nd6_ra_input(m, off, icmp6len);
798 case ND_NEIGHBOR_SOLICIT:
799 icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit);
802 if (icmp6len < sizeof(struct nd_neighbor_solicit))
804 if (send_sendso_input_hook != NULL) {
805 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
811 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
812 nd6_ns_input(m, off, icmp6len);
818 case ND_NEIGHBOR_ADVERT:
819 icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert);
822 if (icmp6len < sizeof(struct nd_neighbor_advert))
824 if (send_sendso_input_hook != NULL) {
825 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
831 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
832 nd6_na_input(m, off, icmp6len);
839 icmp6_ifstat_inc(ifp, ifs6_in_redirect);
842 if (icmp6len < sizeof(struct nd_redirect))
844 if (send_sendso_input_hook != NULL) {
845 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
851 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
852 icmp6_redirect_input(m, off);
858 case ICMP6_ROUTER_RENUMBERING:
859 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
860 code != ICMP6_ROUTER_RENUMBERING_RESULT)
862 if (icmp6len < sizeof(struct icmp6_router_renum))
868 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
869 icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
870 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
871 ifp ? ifp->if_index : 0));
872 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
873 /* ICMPv6 error: MUST deliver it by spec... */
876 /* ICMPv6 informational: MUST not deliver */
880 if (icmp6_notify_error(&m, off, icmp6len) != 0) {
881 /* In this case, m should've been freed. */
883 return (IPPROTO_DONE);
888 ICMP6STAT_INC(icp6s_badcode);
892 ICMP6STAT_INC(icp6s_badlen);
896 /* deliver the packet to appropriate sockets */
897 icmp6_rip6_input(&m, *offp);
900 return (IPPROTO_DONE);
905 return (IPPROTO_DONE);
909 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len)
912 struct icmp6_hdr *icmp6;
913 struct ip6_hdr *eip6;
915 struct sockaddr_in6 icmp6src, icmp6dst;
919 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
920 ICMP6STAT_INC(icp6s_tooshort);
924 if (m->m_len < off + sizeof(*icmp6) + sizeof(struct ip6_hdr)) {
925 m = m_pullup(m, off + sizeof(*icmp6) + sizeof(struct ip6_hdr));
927 IP6STAT_INC(ip6s_exthdrtoolong);
932 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
933 eip6 = (struct ip6_hdr *)(icmp6 + 1);
934 bzero(&icmp6dst, sizeof(icmp6dst));
936 /* Detect the upper level protocol */
938 u_int8_t nxt = eip6->ip6_nxt;
939 int eoff = off + sizeof(struct icmp6_hdr) +
940 sizeof(struct ip6_hdr);
941 struct ip6ctlparam ip6cp;
942 int icmp6type = icmp6->icmp6_type;
944 struct ip6_rthdr *rth;
945 struct ip6_rthdr0 *rth0;
948 while (1) { /* XXX: should avoid infinite loop explicitly? */
952 case IPPROTO_HOPOPTS:
953 case IPPROTO_DSTOPTS:
955 if (m->m_len < eoff + sizeof(struct ip6_ext)) {
956 m = m_pullup(m, eoff +
957 sizeof(struct ip6_ext));
959 IP6STAT_INC(ip6s_exthdrtoolong);
964 eh = (struct ip6_ext *)
965 (mtod(m, caddr_t) + eoff);
966 if (nxt == IPPROTO_AH)
967 eoff += (eh->ip6e_len + 2) << 2;
969 eoff += (eh->ip6e_len + 1) << 3;
972 case IPPROTO_ROUTING:
974 * When the erroneous packet contains a
975 * routing header, we should examine the
976 * header to determine the final destination.
977 * Otherwise, we can't properly update
978 * information that depends on the final
979 * destination (e.g. path MTU).
981 if (m->m_len < eoff + sizeof(*rth)) {
982 m = m_pullup(m, eoff + sizeof(*rth));
984 IP6STAT_INC(ip6s_exthdrtoolong);
989 rth = (struct ip6_rthdr *)
990 (mtod(m, caddr_t) + eoff);
991 rthlen = (rth->ip6r_len + 1) << 3;
993 * XXX: currently there is no
994 * officially defined type other
996 * Note that if the segment left field
997 * is 0, all intermediate hops must
1000 if (rth->ip6r_segleft &&
1001 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
1004 if (m->m_len < eoff + rthlen) {
1005 m = m_pullup(m, eoff + rthlen);
1008 ip6s_exthdrtoolong);
1013 rth0 = (struct ip6_rthdr0 *)
1014 (mtod(m, caddr_t) + eoff);
1016 /* just ignore a bogus header */
1017 if ((rth0->ip6r0_len % 2) == 0 &&
1018 (hops = rth0->ip6r0_len/2))
1019 icmp6dst.sin6_addr = *((struct in6_addr *)(rth0 + 1) + (hops - 1));
1022 nxt = rth->ip6r_nxt;
1024 case IPPROTO_FRAGMENT:
1025 if (m->m_len < eoff + sizeof(struct ip6_frag)) {
1026 m = m_pullup(m, eoff +
1027 sizeof(struct ip6_frag));
1029 IP6STAT_INC(ip6s_exthdrtoolong);
1034 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1037 * Data after a fragment header is meaningless
1038 * unless it is the first fragment, but
1039 * we'll go to the notify label for path MTU
1042 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1045 eoff += sizeof(struct ip6_frag);
1050 * This case includes ESP and the No Next
1051 * Header. In such cases going to the notify
1052 * label does not have any meaning
1053 * (i.e. ctlfunc will be NULL), but we go
1054 * anyway since we might have to update
1055 * path MTU information.
1061 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1064 * retrieve parameters from the inner IPv6 header, and convert
1065 * them into sockaddr structures.
1066 * XXX: there is no guarantee that the source or destination
1067 * addresses of the inner packet are in the same scope as
1068 * the addresses of the icmp packet. But there is no other
1069 * way to determine the zone.
1071 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1073 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1074 icmp6dst.sin6_family = AF_INET6;
1075 if (IN6_IS_ADDR_UNSPECIFIED(&icmp6dst.sin6_addr))
1076 icmp6dst.sin6_addr = eip6->ip6_dst;
1077 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1079 bzero(&icmp6src, sizeof(icmp6src));
1080 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1081 icmp6src.sin6_family = AF_INET6;
1082 icmp6src.sin6_addr = eip6->ip6_src;
1083 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1085 icmp6src.sin6_flowinfo =
1086 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1089 ip6cp.ip6c_icmp6 = icmp6;
1090 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1091 ip6cp.ip6c_off = eoff;
1092 ip6cp.ip6c_finaldst = &icmp6dst;
1093 ip6cp.ip6c_src = &icmp6src;
1094 ip6cp.ip6c_nxt = nxt;
1096 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1097 notifymtu = ntohl(icmp6->icmp6_mtu);
1098 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1099 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1102 if (ip6_ctlprotox[nxt] != NULL)
1103 ip6_ctlprotox[nxt](&ip6cp);
1115 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1117 struct in6_addr *dst = &ip6cp->ip6c_finaldst->sin6_addr;
1118 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1119 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1120 u_int mtu = ntohl(icmp6->icmp6_mtu);
1121 struct in_conninfo inc;
1126 * RFC2460 section 5, last paragraph.
1127 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1128 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1129 * due to packet translator in the middle.
1130 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1133 if (mtu < IPV6_MMTU)
1138 * we reject ICMPv6 too big with abnormally small value.
1139 * XXX what is the good definition of "abnormally small"?
1141 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1148 * In case the suggested mtu is less than IPV6_MMTU, we
1149 * only need to remember that it was for above mentioned
1150 * "alwaysfrag" case.
1151 * Try to be as close to the spec as possible.
1153 if (mtu < IPV6_MMTU)
1154 mtu = IPV6_MMTU - 8;
1156 bzero(&inc, sizeof(inc));
1157 inc.inc_fibnum = M_GETFIB(m);
1158 inc.inc_flags |= INC_ISIPV6;
1159 inc.inc6_faddr = *dst;
1160 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1163 max_mtu = tcp_hc_getmtu(&inc);
1165 max_mtu = tcp_maxmtu6(&inc, NULL);
1167 if (mtu < max_mtu) {
1168 tcp_hc_updatemtu(&inc, mtu);
1169 ICMP6STAT_INC(icp6s_pmtuchg);
1174 * Process a Node Information Query packet, based on
1175 * draft-ietf-ipngwg-icmp-name-lookups-07.
1177 * Spec incompatibilities:
1178 * - IPv6 Subject address handling
1179 * - IPv4 Subject address handling support missing
1180 * - Proxy reply (answer even if it's not for me)
1181 * - joins NI group address at in6_ifattach() time only, does not cope
1182 * with hostname changes by sethostname(3)
1184 static struct mbuf *
1185 ni6_input(struct mbuf *m, int off, struct prison *pr)
1187 struct icmp6_nodeinfo *ni6, *nni6;
1188 struct mbuf *n = NULL;
1191 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1192 struct ni_reply_fqdn *fqdn;
1193 int addrs; /* for NI_QTYPE_NODEADDR */
1194 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1195 struct in6_addr in6_subj; /* subject address */
1196 struct ip6_hdr *ip6;
1197 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1199 struct in6_ifaddr *ia6 = NULL;
1201 ip6 = mtod(m, struct ip6_hdr *);
1202 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1205 * Validate IPv6 source address.
1206 * The default configuration MUST be to refuse answering queries from
1207 * global-scope addresses according to RFC4602.
1209 * - it's not very clear what "refuse" means; this implementation
1211 * - it's not very easy to identify global-scope (unicast) addresses
1212 * since there are many prefixes for them. It should be safer
1213 * and in practice sufficient to check "all" but loopback and
1214 * link-local (note that site-local unicast was deprecated and
1215 * ULA is defined as global scope-wise)
1217 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1218 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1219 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1223 * Validate IPv6 destination address.
1225 * The Responder must discard the Query without further processing
1226 * unless it is one of the Responder's unicast or anycast addresses, or
1227 * a link-local scope multicast address which the Responder has joined.
1228 * [RFC4602, Section 5.]
1230 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1231 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1233 /* else it's a link-local multicast, fine */
1234 } else { /* unicast or anycast */
1235 ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
1237 goto bad; /* XXX impossible */
1239 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1240 !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1241 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1242 "a temporary address in %s:%d",
1243 __FILE__, __LINE__));
1248 /* validate query Subject field. */
1249 qtype = ntohs(ni6->ni_qtype);
1250 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1253 case NI_QTYPE_SUPTYPES:
1255 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1259 case NI_QTYPE_NODEADDR:
1260 case NI_QTYPE_IPV4ADDR:
1261 switch (ni6->ni_code) {
1262 case ICMP6_NI_SUBJ_IPV6:
1263 #if ICMP6_NI_SUBJ_IPV6 != 0
1267 * backward compatibility - try to accept 03 draft
1268 * format, where no Subject is present.
1270 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1275 #if ICMP6_NI_SUBJ_IPV6 != 0
1276 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1280 if (subjlen != sizeof(struct in6_addr))
1284 * Validate Subject address.
1286 * Not sure what exactly "address belongs to the node"
1287 * means in the spec, is it just unicast, or what?
1289 * At this moment we consider Subject address as
1290 * "belong to the node" if the Subject address equals
1291 * to the IPv6 destination address; validation for
1292 * IPv6 destination address should have done enough
1295 * We do not do proxy at this moment.
1297 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1298 subjlen, (caddr_t)&in6_subj);
1299 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1302 subj = (char *)&in6_subj;
1303 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1307 * XXX if we are to allow other cases, we should really
1308 * be careful about scope here.
1309 * basically, we should disallow queries toward IPv6
1310 * destination X with subject Y,
1311 * if scope(X) > scope(Y).
1312 * if we allow scope(X) > scope(Y), it will result in
1313 * information leakage across scope boundary.
1317 case ICMP6_NI_SUBJ_FQDN:
1319 * Validate Subject name with gethostname(3).
1321 * The behavior may need some debate, since:
1322 * - we are not sure if the node has FQDN as
1323 * hostname (returned by gethostname(3)).
1324 * - the code does wildcard match for truncated names.
1325 * however, we are not sure if we want to perform
1326 * wildcard match, if gethostname(3) side has
1327 * truncated hostname.
1329 mtx_lock(&pr->pr_mtx);
1330 n = ni6_nametodns(pr->pr_hostname,
1331 strlen(pr->pr_hostname), 0);
1332 mtx_unlock(&pr->pr_mtx);
1333 if (!n || n->m_next || n->m_len == 0)
1335 if (m->m_len < off + sizeof(struct icmp6_nodeinfo) +
1337 m = m_pullup(m, off +
1338 sizeof(struct icmp6_nodeinfo) + subjlen);
1340 IP6STAT_INC(ip6s_exthdrtoolong);
1344 /* ip6 possibly invalid but not used after. */
1345 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1346 subj = (char *)(mtod(m, caddr_t) + off +
1347 sizeof(struct icmp6_nodeinfo));
1348 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1356 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1363 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1366 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1369 case NI_QTYPE_NODEADDR:
1370 case NI_QTYPE_IPV4ADDR:
1371 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1376 /* guess reply length */
1379 break; /* no reply data */
1380 case NI_QTYPE_SUPTYPES:
1381 replylen += sizeof(u_int32_t);
1384 /* XXX will append an mbuf */
1385 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1387 case NI_QTYPE_NODEADDR:
1388 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1389 if ((replylen += addrs * (sizeof(struct in6_addr) +
1390 sizeof(u_int32_t))) > MCLBYTES)
1391 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1393 case NI_QTYPE_IPV4ADDR:
1394 /* unsupported - should respond with unknown Qtype? */
1398 * XXX: We must return a reply with the ICMP6 code
1399 * `unknown Qtype' in this case. However we regard the case
1400 * as an FQDN query for backward compatibility.
1401 * Older versions set a random value to this field,
1402 * so it rarely varies in the defined qtypes.
1403 * But the mechanism is not reliable...
1404 * maybe we should obsolete older versions.
1406 qtype = NI_QTYPE_FQDN;
1407 /* XXX will append an mbuf */
1408 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1413 /* Allocate an mbuf to reply. */
1414 if (replylen > MCLBYTES) {
1416 * XXX: should we try to allocate more? But MCLBYTES
1417 * is probably much larger than IPV6_MMTU...
1421 if (replylen > MHLEN)
1422 n = m_getcl(M_NOWAIT, m->m_type, M_PKTHDR);
1424 n = m_gethdr(M_NOWAIT, m->m_type);
1429 m_move_pkthdr(n, m); /* just for recvif and FIB */
1430 n->m_pkthdr.len = n->m_len = replylen;
1432 /* copy mbuf header and IPv6 + Node Information base headers */
1433 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1434 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1435 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1437 /* qtype dependent procedure */
1440 nni6->ni_code = ICMP6_NI_SUCCESS;
1443 case NI_QTYPE_SUPTYPES:
1446 nni6->ni_code = ICMP6_NI_SUCCESS;
1447 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1448 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1449 v = (u_int32_t)htonl(0x0000000f);
1450 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1454 nni6->ni_code = ICMP6_NI_SUCCESS;
1455 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1456 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1457 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1458 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1460 * XXX do we really have FQDN in hostname?
1462 mtx_lock(&pr->pr_mtx);
1463 n->m_next = ni6_nametodns(pr->pr_hostname,
1464 strlen(pr->pr_hostname), oldfqdn);
1465 mtx_unlock(&pr->pr_mtx);
1466 if (n->m_next == NULL)
1468 /* XXX we assume that n->m_next is not a chain */
1469 if (n->m_next->m_next != NULL)
1471 n->m_pkthdr.len += n->m_next->m_len;
1473 case NI_QTYPE_NODEADDR:
1477 nni6->ni_code = ICMP6_NI_SUCCESS;
1478 n->m_pkthdr.len = n->m_len =
1479 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1480 lenlim = M_TRAILINGSPACE(n);
1481 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1482 /* XXX: reset mbuf length */
1483 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1484 sizeof(struct icmp6_nodeinfo) + copied;
1488 break; /* XXX impossible! */
1491 nni6->ni_type = ICMP6_NI_REPLY;
1503 * make a mbuf with DNS-encoded string. no compression support.
1505 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1506 * treated as truncated name (two \0 at the end). this is a wild guess.
1508 * old - return pascal string if non-zero
1510 static struct mbuf *
1511 ni6_nametodns(const char *name, int namelen, int old)
1523 /* Because MAXHOSTNAMELEN is usually 256, we use cluster mbuf. */
1525 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1527 m = m_get(M_NOWAIT, MT_DATA);
1533 *mtod(m, char *) = namelen;
1534 bcopy(name, mtod(m, char *) + 1, namelen);
1538 cp = mtod(m, char *);
1539 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1541 /* if not certain about my name, return empty buffer */
1546 * guess if it looks like shortened hostname, or FQDN.
1547 * shortened hostname needs two trailing "\0".
1550 for (p = name; p < name + namelen; p++) {
1551 if (*p && *p == '.')
1560 while (cp < ep && p < name + namelen) {
1562 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1564 /* result does not fit into mbuf */
1565 if (cp + i + 1 >= ep)
1568 * DNS label length restriction, RFC1035 page 8.
1569 * "i == 0" case is included here to avoid returning
1570 * 0-length label on "foo..bar".
1572 if (i <= 0 || i >= 64)
1578 if (p < name + namelen && *p == '.')
1582 if (cp + nterm >= ep)
1586 m->m_len = cp - mtod(m, char *);
1590 panic("should not reach here");
1600 * check if two DNS-encoded string matches. takes care of truncated
1601 * form (with \0\0 at the end). no compression support.
1602 * XXX upper/lowercase match (see RFC2065)
1605 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1607 const char *a0, *b0;
1610 /* simplest case - need validation? */
1611 if (alen == blen && bcmp(a, b, alen) == 0)
1617 /* termination is mandatory */
1618 if (alen < 2 || blen < 2)
1620 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1625 while (a - a0 < alen && b - b0 < blen) {
1626 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1629 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1631 /* we don't support compression yet */
1632 if (a[0] >= 64 || b[0] >= 64)
1635 /* truncated case */
1636 if (a[0] == 0 && a - a0 == alen - 1)
1638 if (b[0] == 0 && b - b0 == blen - 1)
1640 if (a[0] == 0 || b[0] == 0)
1646 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1648 if (bcmp(a + 1, b + 1, l) != 0)
1655 if (a - a0 == alen && b - b0 == blen)
1662 * calculate the number of addresses to be returned in the node info reply.
1665 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1666 struct in6_addr *subj)
1669 struct in6_ifaddr *ifa6;
1671 int addrs = 0, addrsofif, iffound = 0;
1672 int niflags = ni6->ni_flags;
1676 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1677 switch (ni6->ni_code) {
1678 case ICMP6_NI_SUBJ_IPV6:
1679 if (subj == NULL) /* must be impossible... */
1684 * XXX: we only support IPv6 subject address for
1691 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1693 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1694 if (ifa->ifa_addr->sa_family != AF_INET6)
1696 ifa6 = (struct in6_ifaddr *)ifa;
1698 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1699 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1703 * IPv4-mapped addresses can only be returned by a
1704 * Node Information proxy, since they represent
1705 * addresses of IPv4-only nodes, which perforce do
1706 * not implement this protocol.
1707 * [icmp-name-lookups-07, Section 5.4]
1708 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1709 * this function at this moment.
1712 /* What do we have to do about ::1? */
1713 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1714 case IPV6_ADDR_SCOPE_LINKLOCAL:
1715 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1718 case IPV6_ADDR_SCOPE_SITELOCAL:
1719 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1722 case IPV6_ADDR_SCOPE_GLOBAL:
1723 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1731 * check if anycast is okay.
1732 * XXX: just experimental. not in the spec.
1734 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1735 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1736 continue; /* we need only unicast addresses */
1737 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1738 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1741 addrsofif++; /* count the address */
1755 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1756 struct ifnet *ifp0, int resid)
1759 struct in6_ifaddr *ifa6;
1761 struct ifnet *ifp_dep = NULL;
1762 int copied = 0, allow_deprecated = 0;
1763 u_char *cp = (u_char *)(nni6 + 1);
1764 int niflags = ni6->ni_flags;
1769 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1770 return (0); /* needless to copy */
1772 ifp = ifp0 ? ifp0 : CK_STAILQ_FIRST(&V_ifnet);
1775 for (; ifp; ifp = CK_STAILQ_NEXT(ifp, if_link)) {
1776 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1777 if (ifa->ifa_addr->sa_family != AF_INET6)
1779 ifa6 = (struct in6_ifaddr *)ifa;
1781 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1782 allow_deprecated == 0) {
1784 * prefererred address should be put before
1785 * deprecated addresses.
1788 /* record the interface for later search */
1789 if (ifp_dep == NULL)
1793 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1794 allow_deprecated != 0)
1795 continue; /* we now collect deprecated addrs */
1797 /* What do we have to do about ::1? */
1798 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1799 case IPV6_ADDR_SCOPE_LINKLOCAL:
1800 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1803 case IPV6_ADDR_SCOPE_SITELOCAL:
1804 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1807 case IPV6_ADDR_SCOPE_GLOBAL:
1808 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1816 * check if anycast is okay.
1817 * XXX: just experimental. not in the spec.
1819 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1820 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1822 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1823 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1827 /* now we can copy the address */
1828 if (resid < sizeof(struct in6_addr) +
1829 sizeof(u_int32_t)) {
1831 * We give up much more copy.
1832 * Set the truncate flag and return.
1834 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1839 * Set the TTL of the address.
1840 * The TTL value should be one of the following
1841 * according to the specification:
1843 * 1. The remaining lifetime of a DHCP lease on the
1845 * 2. The remaining Valid Lifetime of a prefix from
1846 * which the address was derived through Stateless
1847 * Autoconfiguration.
1849 * Note that we currently do not support stateful
1850 * address configuration by DHCPv6, so the former
1851 * case can't happen.
1853 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1854 ltime = ND6_INFINITE_LIFETIME;
1856 if (ifa6->ia6_lifetime.ia6t_expire >
1858 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_uptime);
1863 bcopy(<ime, cp, sizeof(u_int32_t));
1864 cp += sizeof(u_int32_t);
1866 /* copy the address itself */
1867 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1868 sizeof(struct in6_addr));
1869 in6_clearscope((struct in6_addr *)cp); /* XXX */
1870 cp += sizeof(struct in6_addr);
1872 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1873 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1875 if (ifp0) /* we need search only on the specified IF */
1879 if (allow_deprecated == 0 && ifp_dep != NULL) {
1881 allow_deprecated = 1;
1890 icmp6_rip6_match(const struct inpcb *inp, void *v)
1892 struct ip6_hdr *ip6 = v;
1894 if ((inp->inp_vflag & INP_IPV6) == 0)
1896 if (inp->inp_ip_p != IPPROTO_ICMPV6)
1898 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1899 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst))
1901 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1902 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src))
1908 * XXX almost dup'ed code with rip6_input.
1911 icmp6_rip6_input(struct mbuf **mp, int off)
1913 struct mbuf *n, *m = *mp;
1914 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1915 struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo,
1916 INPLOOKUP_RLOCKPCB, icmp6_rip6_match, ip6);
1918 struct sockaddr_in6 fromsa;
1919 struct icmp6_hdr *icmp6;
1920 struct mbuf *opts = NULL;
1923 /* This is assumed to be safe; icmp6_input() does a pullup. */
1924 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1927 * XXX: the address may have embedded scope zone ID, which should be
1928 * hidden from applications.
1930 bzero(&fromsa, sizeof(fromsa));
1931 fromsa.sin6_family = AF_INET6;
1932 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1933 fromsa.sin6_addr = ip6->ip6_src;
1934 if (sa6_recoverscope(&fromsa)) {
1937 return (IPPROTO_DONE);
1940 while ((inp = inp_next(&inpi)) != NULL) {
1941 if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1942 inp->in6p_icmp6filt))
1945 * Recent network drivers tend to allocate a single
1946 * mbuf cluster, rather than to make a couple of
1947 * mbufs without clusters. Also, since the IPv6 code
1948 * path tries to avoid m_pullup(), it is highly
1949 * probable that we still have an mbuf cluster here
1950 * even though the necessary length can be stored in an
1951 * mbuf's internal buffer.
1952 * Meanwhile, the default size of the receive socket
1953 * buffer for raw sockets is not so large. This means
1954 * the possibility of packet loss is relatively higher
1955 * than before. To avoid this scenario, we copy the
1956 * received data to a separate mbuf that does not use
1957 * a cluster, if possible.
1958 * XXX: it is better to copy the data after stripping
1959 * intermediate headers.
1961 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1962 m->m_len <= MHLEN) {
1963 n = m_get(M_NOWAIT, m->m_type);
1965 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1966 bcopy(m->m_data, n->m_data, m->m_len);
1967 n->m_len = m->m_len;
1974 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1977 if (inp->inp_flags & INP_CONTROLOPTS)
1978 ip6_savecontrol(inp, n, &opts);
1979 /* strip intermediate headers */
1981 SOCKBUF_LOCK(&inp->inp_socket->so_rcv);
1982 if (sbappendaddr_locked(&inp->inp_socket->so_rcv,
1983 (struct sockaddr *)&fromsa, n, opts) == 0) {
1984 soroverflow_locked(inp->inp_socket);
1989 sorwakeup_locked(inp->inp_socket);
1997 IP6STAT_DEC(ip6s_delivered);
1998 return (IPPROTO_DONE);
2002 * Reflect the ip6 packet back to the source.
2003 * OFF points to the icmp6 header, counted from the top of the mbuf.
2006 icmp6_reflect(struct mbuf *m, size_t off)
2008 struct in6_addr src6, *srcp;
2009 struct ip6_hdr *ip6;
2010 struct icmp6_hdr *icmp6;
2011 struct in6_ifaddr *ia = NULL;
2012 struct ifnet *outif = NULL;
2014 int type, code, hlim;
2016 /* too short to reflect */
2017 if (off < sizeof(struct ip6_hdr)) {
2019 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2020 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2021 __FILE__, __LINE__));
2026 * If there are extra headers between IPv6 and ICMPv6, strip
2027 * off that header first.
2030 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2031 panic("assumption failed in icmp6_reflect");
2033 if (off > sizeof(struct ip6_hdr)) {
2035 struct ip6_hdr nip6;
2037 l = off - sizeof(struct ip6_hdr);
2038 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2040 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2042 if ((m = m_pullup(m, l)) == NULL)
2045 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2046 } else /* off == sizeof(struct ip6_hdr) */ {
2048 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2050 if ((m = m_pullup(m, l)) == NULL)
2054 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2055 ip6 = mtod(m, struct ip6_hdr *);
2056 ip6->ip6_nxt = IPPROTO_ICMPV6;
2057 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2058 type = icmp6->icmp6_type; /* keep type for statistics */
2059 code = icmp6->icmp6_code; /* ditto. */
2064 * If the incoming packet was addressed directly to us (i.e. unicast),
2065 * use dst as the src for the reply.
2066 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2067 * (for example) when we encounter an error while forwarding procedure
2068 * destined to a duplicated address of ours.
2070 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
2071 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
2072 if (ia != NULL && !(ia->ia6_flags &
2073 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2074 src6 = ia->ia_addr.sin6_addr;
2077 if (m->m_pkthdr.rcvif != NULL) {
2078 /* XXX: This may not be the outgoing interface */
2079 hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2081 hlim = V_ip6_defhlim;
2087 struct in6_addr dst6;
2091 * This case matches to multicasts, our anycast, or unicasts
2092 * that we do not own. Select a source address based on the
2093 * source address of the erroneous packet.
2095 in6_splitscope(&ip6->ip6_src, &dst6, &scopeid);
2096 error = in6_selectsrc_addr(M_GETFIB(m), &dst6,
2097 scopeid, NULL, &src6, &hlim);
2100 char ip6buf[INET6_ADDRSTRLEN];
2102 "icmp6_reflect: source can't be determined: "
2103 "dst=%s, error=%d\n",
2104 ip6_sprintf(ip6buf, &ip6->ip6_dst), error));
2110 * ip6_input() drops a packet if its src is multicast.
2111 * So, the src is never multicast.
2113 ip6->ip6_dst = ip6->ip6_src;
2114 ip6->ip6_src = *srcp;
2116 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2117 ip6->ip6_vfc |= IPV6_VERSION;
2118 ip6->ip6_nxt = IPPROTO_ICMPV6;
2119 ip6->ip6_hlim = hlim;
2121 icmp6->icmp6_cksum = 0;
2122 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2123 sizeof(struct ip6_hdr), plen);
2126 * XXX option handling
2129 m->m_flags &= ~(M_BCAST|M_MCAST);
2130 m->m_pkthdr.rcvif = NULL;
2131 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2133 icmp6_ifoutstat_inc(outif, type, code);
2143 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2144 struct in6_addr *tgt6)
2146 static char buf[1024];
2147 char ip6bufs[INET6_ADDRSTRLEN];
2148 char ip6bufd[INET6_ADDRSTRLEN];
2149 char ip6buft[INET6_ADDRSTRLEN];
2150 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2151 ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2152 ip6_sprintf(ip6buft, tgt6));
2157 icmp6_redirect_input(struct mbuf *m, int off)
2160 struct ip6_hdr *ip6;
2161 struct nd_redirect *nd_rd;
2162 struct in6_addr src6, redtgt6, reddst6;
2163 union nd_opts ndopts;
2164 char ip6buf[INET6_ADDRSTRLEN];
2166 int icmp6len, is_onlink, is_router, lladdrlen;
2169 KASSERT(m->m_pkthdr.rcvif != NULL, ("%s: no rcvif", __func__));
2171 /* XXX if we are router, we don't update route by icmp6 redirect */
2172 if (V_ip6_forwarding)
2174 if (!V_icmp6_rediraccept)
2177 /* RFC 6980: Nodes MUST silently ignore fragments */
2178 if(m->m_flags & M_FRAGMENTED)
2181 ip6 = mtod(m, struct ip6_hdr *);
2182 icmp6len = ntohs(ip6->ip6_plen);
2183 if (m->m_len < off + icmp6len) {
2184 m = m_pullup(m, off + icmp6len);
2186 IP6STAT_INC(ip6s_exthdrtoolong);
2190 ip6 = mtod(m, struct ip6_hdr *);
2191 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2193 ifp = m->m_pkthdr.rcvif;
2194 redtgt6 = nd_rd->nd_rd_target;
2195 reddst6 = nd_rd->nd_rd_dst;
2197 if (in6_setscope(&redtgt6, ifp, NULL) ||
2198 in6_setscope(&reddst6, ifp, NULL)) {
2203 src6 = ip6->ip6_src;
2204 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2206 "ICMP6 redirect sent from %s rejected; "
2207 "must be from linklocal\n",
2208 ip6_sprintf(ip6buf, &src6)));
2211 if (__predict_false(ip6->ip6_hlim != 255)) {
2212 ICMP6STAT_INC(icp6s_invlhlim);
2214 "ICMP6 redirect sent from %s rejected; "
2215 "hlim=%d (must be 255)\n",
2216 ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
2220 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2221 struct nhop_object *nh;
2222 struct in6_addr kdst;
2225 in6_splitscope(&reddst6, &kdst, &scopeid);
2227 nh = fib6_lookup(ifp->if_fib, &kdst, scopeid, 0, 0);
2229 struct in6_addr nh_addr;
2230 nh_addr = ifatoia6(nh->nh_ifa)->ia_addr.sin6_addr;
2231 if ((nh->nh_flags & NHF_GATEWAY) == 0) {
2233 "ICMP6 redirect rejected; no route "
2234 "with inet6 gateway found for redirect dst: %s\n",
2235 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2240 * Embed scope zone id into next hop address.
2242 nh_addr = nh->gw6_sa.sin6_addr;
2244 if (IN6_ARE_ADDR_EQUAL(&src6, &nh_addr) == 0) {
2246 "ICMP6 redirect rejected; "
2247 "not equal to gw-for-src=%s (must be same): "
2249 ip6_sprintf(ip6buf, &nh_addr),
2250 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2255 "ICMP6 redirect rejected; "
2256 "no route found for redirect dst: %s\n",
2257 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2261 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2263 "ICMP6 redirect rejected; "
2264 "redirect dst must be unicast: %s\n",
2265 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2269 is_router = is_onlink = 0;
2270 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2271 is_router = 1; /* router case */
2272 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2273 is_onlink = 1; /* on-link destination case */
2274 if (!is_router && !is_onlink) {
2276 "ICMP6 redirect rejected; "
2277 "neither router case nor onlink case: %s\n",
2278 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2282 icmp6len -= sizeof(*nd_rd);
2283 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2284 if (nd6_options(&ndopts) < 0) {
2285 nd6log((LOG_INFO, "%s: invalid ND option, rejected: %s\n",
2286 __func__, icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2287 /* nd6_options have incremented stats */
2293 if (ndopts.nd_opts_tgt_lladdr) {
2294 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2295 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2298 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2299 nd6log((LOG_INFO, "%s: lladdrlen mismatch for %s "
2300 "(if %d, icmp6 packet %d): %s\n",
2301 __func__, ip6_sprintf(ip6buf, &redtgt6),
2302 ifp->if_addrlen, lladdrlen - 2,
2303 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2307 /* Validation passed. */
2310 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2311 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2314 * Install a gateway route in the better-router case or an interface
2315 * route in the on-link-destination case.
2318 struct sockaddr_in6 sdst;
2319 struct sockaddr_in6 sgw;
2320 struct sockaddr_in6 ssrc;
2321 struct sockaddr *gw;
2325 bzero(&sdst, sizeof(sdst));
2326 bzero(&ssrc, sizeof(ssrc));
2327 sdst.sin6_family = ssrc.sin6_family = AF_INET6;
2328 sdst.sin6_len = ssrc.sin6_len = sizeof(struct sockaddr_in6);
2329 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2330 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2333 bzero(&sgw, sizeof(sgw));
2334 sgw.sin6_family = AF_INET6;
2335 sgw.sin6_len = sizeof(struct sockaddr_in6);
2336 bcopy(&redtgt6, &sgw.sin6_addr,
2337 sizeof(struct in6_addr));
2338 gw = (struct sockaddr *)&sgw;
2339 rt_flags |= RTF_GATEWAY;
2341 gw = ifp->if_addr->ifa_addr;
2342 for (fibnum = 0; fibnum < rt_numfibs; fibnum++)
2343 rib_add_redirect(fibnum, (struct sockaddr *)&sdst, gw,
2344 (struct sockaddr *)&ssrc, ifp, rt_flags,
2345 V_icmp6_redirtimeout);
2353 ICMP6STAT_INC(icp6s_badredirect);
2358 icmp6_redirect_output(struct mbuf *m0, struct nhop_object *nh)
2360 struct ifnet *ifp; /* my outgoing interface */
2361 struct in6_addr *ifp_ll6;
2362 struct in6_addr *router_ll6;
2363 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2364 struct mbuf *m = NULL; /* newly allocated one */
2366 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2367 struct nd_redirect *nd_rd;
2368 struct llentry *ln = NULL;
2371 struct ifnet *outif = NULL;
2372 struct sockaddr_in6 src_sa;
2374 icmp6_errcount(ND_REDIRECT, 0);
2376 /* if we are not router, we don't send icmp6 redirect */
2377 if (!V_ip6_forwarding)
2381 if (!m0 || !nh || !(NH_IS_VALID(nh)) || !(ifp = nh->nh_ifp))
2386 * the source address must identify a neighbor, and
2387 * the destination address must not be a multicast address
2388 * [RFC 2461, sec 8.2]
2390 sip6 = mtod(m0, struct ip6_hdr *);
2391 bzero(&src_sa, sizeof(src_sa));
2392 src_sa.sin6_family = AF_INET6;
2393 src_sa.sin6_len = sizeof(src_sa);
2394 src_sa.sin6_addr = sip6->ip6_src;
2395 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2397 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2398 goto fail; /* what should we do here? */
2401 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2405 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2406 * we almost always ask for an mbuf cluster for simplicity.
2407 * (MHLEN < IPV6_MMTU is almost always true)
2409 #if IPV6_MMTU >= MCLBYTES
2410 # error assumption failed about IPV6_MMTU and MCLBYTES
2412 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2415 M_SETFIB(m, M_GETFIB(m0));
2416 maxlen = M_TRAILINGSPACE(m);
2417 maxlen = min(IPV6_MMTU, maxlen);
2418 /* just for safety */
2419 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2420 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2425 /* get ip6 linklocal address for ifp(my outgoing interface). */
2426 struct in6_ifaddr *ia;
2427 if ((ia = in6ifa_ifpforlinklocal(ifp,
2429 IN6_IFF_ANYCAST)) == NULL)
2431 ifp_ll6 = &ia->ia_addr.sin6_addr;
2432 /* XXXRW: reference released prematurely. */
2433 ifa_free(&ia->ia_ifa);
2436 /* get ip6 linklocal address for the router. */
2437 if (nh->nh_flags & NHF_GATEWAY) {
2438 struct sockaddr_in6 *sin6;
2440 router_ll6 = &sin6->sin6_addr;
2441 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2442 router_ll6 = (struct in6_addr *)NULL;
2444 router_ll6 = (struct in6_addr *)NULL;
2447 ip6 = mtod(m, struct ip6_hdr *);
2449 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2450 ip6->ip6_vfc |= IPV6_VERSION;
2451 /* ip6->ip6_plen will be set later */
2452 ip6->ip6_nxt = IPPROTO_ICMPV6;
2453 ip6->ip6_hlim = 255;
2454 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2455 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2456 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2459 nd_rd = (struct nd_redirect *)(ip6 + 1);
2460 nd_rd->nd_rd_type = ND_REDIRECT;
2461 nd_rd->nd_rd_code = 0;
2462 nd_rd->nd_rd_reserved = 0;
2463 if (nh->nh_flags & NHF_GATEWAY) {
2465 * nd_rd->nd_rd_target must be a link-local address in
2466 * better router cases.
2470 bcopy(router_ll6, &nd_rd->nd_rd_target,
2471 sizeof(nd_rd->nd_rd_target));
2472 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2473 sizeof(nd_rd->nd_rd_dst));
2475 /* make sure redtgt == reddst */
2476 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2477 sizeof(nd_rd->nd_rd_target));
2478 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2479 sizeof(nd_rd->nd_rd_dst));
2482 p = (u_char *)(nd_rd + 1);
2488 /* target lladdr option */
2490 struct nd_opt_hdr *nd_opt;
2493 ln = nd6_lookup(router_ll6, LLE_SF(AF_INET6, 0), ifp);
2497 len = sizeof(*nd_opt) + ifp->if_addrlen;
2498 len = (len + 7) & ~7; /* round by 8 */
2500 if (len + (p - (u_char *)ip6) > maxlen)
2503 if (ln->la_flags & LLE_VALID) {
2504 nd_opt = (struct nd_opt_hdr *)p;
2505 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2506 nd_opt->nd_opt_len = len >> 3;
2507 lladdr = (char *)(nd_opt + 1);
2508 bcopy(ln->ll_addr, lladdr, ifp->if_addrlen);
2516 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2518 /* just to be safe */
2519 #ifdef M_DECRYPTED /*not openbsd*/
2520 if (m0->m_flags & M_DECRYPTED)
2523 if (p - (u_char *)ip6 > maxlen)
2527 /* redirected header option */
2529 struct nd_opt_rd_hdr *nd_opt_rh;
2532 * compute the maximum size for icmp6 redirect header option.
2533 * XXX room for auth header?
2535 len = maxlen - (p - (u_char *)ip6);
2538 /* This is just for simplicity. */
2539 if (m0->m_pkthdr.len != m0->m_len) {
2541 m_freem(m0->m_next);
2544 m0->m_pkthdr.len = m0->m_len;
2548 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2549 * about padding/truncate rule for the original IP packet.
2550 * From the discussion on IPv6imp in Feb 1999,
2551 * the consensus was:
2552 * - "attach as much as possible" is the goal
2553 * - pad if not aligned (original size can be guessed by
2554 * original ip6 header)
2555 * Following code adds the padding if it is simple enough,
2556 * and truncates if not.
2558 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2559 panic("assumption failed in %s:%d", __FILE__,
2562 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2563 /* not enough room, truncate */
2564 m0->m_pkthdr.len = m0->m_len = len -
2567 /* enough room, pad or truncate */
2570 extra = m0->m_pkthdr.len % 8;
2572 /* pad if easy enough, truncate if not */
2573 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2575 m0->m_len += (8 - extra);
2576 m0->m_pkthdr.len += (8 - extra);
2579 m0->m_pkthdr.len -= extra;
2583 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2584 m0->m_pkthdr.len = m0->m_len = len -
2588 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2589 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2590 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2591 nd_opt_rh->nd_opt_rh_len = len >> 3;
2592 p += sizeof(*nd_opt_rh);
2593 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2595 /* connect m0 to m */
2596 m_tag_delete_chain(m0, NULL);
2597 m0->m_flags &= ~M_PKTHDR;
2599 m->m_pkthdr.len = m->m_len + m0->m_len;
2608 /* XXX: clear embedded link IDs in the inner header */
2609 in6_clearscope(&sip6->ip6_src);
2610 in6_clearscope(&sip6->ip6_dst);
2611 in6_clearscope(&nd_rd->nd_rd_target);
2612 in6_clearscope(&nd_rd->nd_rd_dst);
2614 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2616 nd_rd->nd_rd_cksum = 0;
2617 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2618 sizeof(*ip6), ntohs(ip6->ip6_plen));
2620 if (send_sendso_input_hook != NULL) {
2621 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short),
2625 *(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type;
2626 m_tag_prepend(m, mtag);
2629 /* send the packet to outside... */
2630 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2632 icmp6_ifstat_inc(outif, ifs6_out_msg);
2633 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2635 ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]);
2647 * ICMPv6 socket option processing.
2650 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2654 struct inpcb *inp = sotoinpcb(so);
2655 int level, op, optname;
2658 level = sopt->sopt_level;
2659 op = sopt->sopt_dir;
2660 optname = sopt->sopt_name;
2661 optlen = sopt->sopt_valsize;
2663 level = op = optname = optlen = 0;
2665 if (level != IPPROTO_ICMPV6) {
2674 struct icmp6_filter ic6f;
2676 if (optlen != sizeof(ic6f)) {
2680 error = sooptcopyin(sopt, &ic6f, optlen, optlen);
2683 *inp->in6p_icmp6filt = ic6f;
2690 error = ENOPROTOOPT;
2699 struct icmp6_filter ic6f;
2702 ic6f = *inp->in6p_icmp6filt;
2704 error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
2709 error = ENOPROTOOPT;
2719 * Perform rate limit check.
2720 * Returns 0 if it is okay to send the icmp6 packet.
2721 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2724 * XXX per-destination/type check necessary?
2726 * dst - not used at this moment
2727 * type - not used at this moment
2728 * code - not used at this moment
2731 icmp6_ratelimit(const struct in6_addr *dst, const int type,
2736 ret = 0; /* okay to send */
2739 if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count,
2740 V_icmp6errppslim)) {
2741 /* The packet is subject to rate limit */