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_types.h>
95 #include <net/route.h>
96 #include <net/route/route_ctl.h>
97 #include <net/route/nhop.h>
100 #include <netinet/in.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet/in_var.h>
103 #include <netinet/ip6.h>
104 #include <netinet/icmp6.h>
105 #include <netinet/tcp_var.h>
107 #include <netinet6/in6_fib.h>
108 #include <netinet6/in6_ifattach.h>
109 #include <netinet6/in6_pcb.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet6/scope6_var.h>
112 #include <netinet6/mld6_var.h>
113 #include <netinet6/nd6.h>
114 #include <netinet6/send.h>
116 extern ip6proto_ctlinput_t *ip6_ctlprotox[];
118 VNET_PCPUSTAT_DEFINE(struct icmp6stat, icmp6stat);
119 VNET_PCPUSTAT_SYSINIT(icmp6stat);
122 VNET_PCPUSTAT_SYSUNINIT(icmp6stat);
125 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
126 VNET_DECLARE(int, icmp6errppslim);
127 VNET_DEFINE_STATIC(int, icmp6errpps_count) = 0;
128 VNET_DEFINE_STATIC(struct timeval, icmp6errppslim_last);
129 VNET_DECLARE(int, icmp6_nodeinfo);
131 #define V_ripcbinfo VNET(ripcbinfo)
132 #define V_icmp6errppslim VNET(icmp6errppslim)
133 #define V_icmp6errpps_count VNET(icmp6errpps_count)
134 #define V_icmp6errppslim_last VNET(icmp6errppslim_last)
135 #define V_icmp6_nodeinfo VNET(icmp6_nodeinfo)
137 static void icmp6_errcount(int, int);
138 static int icmp6_rip6_input(struct mbuf **, int);
139 static void icmp6_reflect(struct mbuf *, size_t);
140 static const char *icmp6_redirect_diag(struct in6_addr *,
141 struct in6_addr *, struct in6_addr *);
142 static struct mbuf *ni6_input(struct mbuf *, int, struct prison *);
143 static struct mbuf *ni6_nametodns(const char *, int, int);
144 static int ni6_dnsmatch(const char *, int, const char *, int);
145 static int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *,
146 struct ifnet **, struct in6_addr *);
147 static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
148 struct ifnet *, int);
149 static int icmp6_notify_error(struct mbuf **, int, int);
152 * Kernel module interface for updating icmp6stat. The argument is an index
153 * into icmp6stat treated as an array of u_quad_t. While this encodes the
154 * general layout of icmp6stat into the caller, it doesn't encode its
155 * location, so that future changes to add, for example, per-CPU stats
156 * support won't cause binary compatibility problems for kernel modules.
159 kmod_icmp6stat_inc(int statnum)
162 counter_u64_add(VNET(icmp6stat)[statnum], 1);
166 icmp6_errcount(int type, int code)
169 case ICMP6_DST_UNREACH:
171 case ICMP6_DST_UNREACH_NOROUTE:
172 ICMP6STAT_INC(icp6s_odst_unreach_noroute);
174 case ICMP6_DST_UNREACH_ADMIN:
175 ICMP6STAT_INC(icp6s_odst_unreach_admin);
177 case ICMP6_DST_UNREACH_BEYONDSCOPE:
178 ICMP6STAT_INC(icp6s_odst_unreach_beyondscope);
180 case ICMP6_DST_UNREACH_ADDR:
181 ICMP6STAT_INC(icp6s_odst_unreach_addr);
183 case ICMP6_DST_UNREACH_NOPORT:
184 ICMP6STAT_INC(icp6s_odst_unreach_noport);
188 case ICMP6_PACKET_TOO_BIG:
189 ICMP6STAT_INC(icp6s_opacket_too_big);
191 case ICMP6_TIME_EXCEEDED:
193 case ICMP6_TIME_EXCEED_TRANSIT:
194 ICMP6STAT_INC(icp6s_otime_exceed_transit);
196 case ICMP6_TIME_EXCEED_REASSEMBLY:
197 ICMP6STAT_INC(icp6s_otime_exceed_reassembly);
201 case ICMP6_PARAM_PROB:
203 case ICMP6_PARAMPROB_HEADER:
204 ICMP6STAT_INC(icp6s_oparamprob_header);
206 case ICMP6_PARAMPROB_NEXTHEADER:
207 ICMP6STAT_INC(icp6s_oparamprob_nextheader);
209 case ICMP6_PARAMPROB_OPTION:
210 ICMP6STAT_INC(icp6s_oparamprob_option);
215 ICMP6STAT_INC(icp6s_oredirect);
218 ICMP6STAT_INC(icp6s_ounknown);
222 * A wrapper function for icmp6_error() necessary when the erroneous packet
223 * may not contain enough scope zone information.
226 icmp6_error2(struct mbuf *m, int type, int code, int param,
234 if (m->m_len < sizeof(struct ip6_hdr)) {
235 m = m_pullup(m, sizeof(struct ip6_hdr));
237 IP6STAT_INC(ip6s_exthdrtoolong);
241 ip6 = mtod(m, struct ip6_hdr *);
243 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
245 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
248 icmp6_error(m, type, code, param);
252 * Generate an error packet of type error in response to bad IP6 packet.
255 icmp6_error(struct mbuf *m, int type, int code, int param)
257 struct ip6_hdr *oip6, *nip6;
258 struct icmp6_hdr *icmp6;
259 struct epoch_tracker et;
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 if (m->m_len < sizeof(struct ip6_hdr)) {
277 m = m_pullup(m, sizeof(struct ip6_hdr));
279 IP6STAT_INC(ip6s_exthdrtoolong);
283 oip6 = mtod(m, struct ip6_hdr *);
286 * If the destination address of the erroneous packet is a multicast
287 * address, or the packet was sent using link-layer multicast,
288 * we should basically suppress sending an error (RFC 2463, Section
290 * We have two exceptions (the item e.2 in that section):
291 * - the Packet Too Big message can be sent for path MTU discovery.
292 * - the Parameter Problem Message that can be allowed an icmp6 error
293 * in the option type field. This check has been done in
294 * ip6_unknown_opt(), so we can just check the type and code.
296 if ((m->m_flags & (M_BCAST|M_MCAST) ||
297 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
298 (type != ICMP6_PACKET_TOO_BIG &&
299 (type != ICMP6_PARAM_PROB ||
300 code != ICMP6_PARAMPROB_OPTION)))
304 * RFC 2463, 2.4 (e.5): source address check.
305 * XXX: the case of anycast source?
307 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
308 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
312 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
316 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
317 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
318 struct icmp6_hdr *icp;
320 if (m->m_len < off + sizeof(struct icmp6_hdr)) {
321 m = m_pullup(m, off + sizeof(struct icmp6_hdr));
323 IP6STAT_INC(ip6s_exthdrtoolong);
327 oip6 = mtod(m, struct ip6_hdr *);
328 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
330 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
331 icp->icmp6_type == ND_REDIRECT) {
334 * Special case: for redirect (which is
335 * informational) we must not send icmp6 error.
337 ICMP6STAT_INC(icp6s_canterror);
340 /* ICMPv6 informational - send the error */
343 /* non-ICMPv6 - send the error */
346 /* Finally, do rate limitation check. */
347 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
348 ICMP6STAT_INC(icp6s_toofreq);
353 * OK, ICMP6 can be generated.
356 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
357 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
359 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
360 M_PREPEND(m, preplen, M_NOWAIT); /* FIB is also copied over. */
362 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
366 nip6 = mtod(m, struct ip6_hdr *);
367 nip6->ip6_src = oip6->ip6_src;
368 nip6->ip6_dst = oip6->ip6_dst;
370 in6_clearscope(&oip6->ip6_src);
371 in6_clearscope(&oip6->ip6_dst);
373 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
374 icmp6->icmp6_type = type;
375 icmp6->icmp6_code = code;
376 icmp6->icmp6_pptr = htonl((u_int32_t)param);
378 ICMP6STAT_INC(icp6s_outhist[type]);
380 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
387 * If we can't tell whether or not we can generate ICMP6, free it.
393 icmp6_errmap(const struct icmp6_hdr *icmp6)
396 switch (icmp6->icmp6_type) {
397 case ICMP6_DST_UNREACH:
398 switch (icmp6->icmp6_code) {
399 case ICMP6_DST_UNREACH_NOROUTE:
400 case ICMP6_DST_UNREACH_ADDR:
401 return (EHOSTUNREACH);
402 case ICMP6_DST_UNREACH_NOPORT:
403 case ICMP6_DST_UNREACH_ADMIN:
404 return (ECONNREFUSED);
405 case ICMP6_DST_UNREACH_BEYONDSCOPE:
406 return (ENOPROTOOPT);
408 return (0); /* Shouldn't happen. */
410 case ICMP6_PACKET_TOO_BIG:
412 case ICMP6_TIME_EXCEEDED:
413 switch (icmp6->icmp6_code) {
414 case ICMP6_TIME_EXCEED_TRANSIT:
415 return (EHOSTUNREACH);
416 case ICMP6_TIME_EXCEED_REASSEMBLY:
419 return (0); /* Shouldn't happen. */
421 case ICMP6_PARAM_PROB:
422 switch (icmp6->icmp6_code) {
423 case ICMP6_PARAMPROB_NEXTHEADER:
424 return (ECONNREFUSED);
425 case ICMP6_PARAMPROB_HEADER:
426 case ICMP6_PARAMPROB_OPTION:
427 return (ENOPROTOOPT);
429 return (0); /* Shouldn't happen. */
437 * Process a received ICMP6 message.
440 icmp6_input(struct mbuf **mp, int *offp, int proto)
444 struct ip6_hdr *ip6, *nip6;
445 struct icmp6_hdr *icmp6, *nicmp6;
446 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
447 int code, error, icmp6len, ip6len, noff, off, sum;
454 if (m->m_len < off + sizeof(struct icmp6_hdr)) {
455 m = m_pullup(m, off + sizeof(struct icmp6_hdr));
457 IP6STAT_INC(ip6s_exthdrtoolong);
459 return (IPPROTO_DONE);
464 * Locate icmp6 structure in mbuf, and check
465 * that not corrupted and of at least minimum length
468 icmp6len = m->m_pkthdr.len - off;
469 if (icmp6len < sizeof(struct icmp6_hdr)) {
470 ICMP6STAT_INC(icp6s_tooshort);
474 ip6 = mtod(m, struct ip6_hdr *);
475 ifp = m->m_pkthdr.rcvif;
477 * Check multicast group membership.
478 * Note: SSM filters are not applied for ICMPv6 traffic.
480 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
481 struct in6_multi *inm;
483 inm = in6m_lookup(ifp, &ip6->ip6_dst);
485 IP6STAT_INC(ip6s_notmember);
486 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
491 /* Calculate the checksum. */
492 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
493 code = icmp6->icmp6_code;
494 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
496 "ICMP6 checksum error(%d|%x) %s\n",
497 icmp6->icmp6_type, sum,
498 ip6_sprintf(ip6bufs, &ip6->ip6_src)));
499 ICMP6STAT_INC(icp6s_checksum);
503 ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]);
504 icmp6_ifstat_inc(ifp, ifs6_in_msg);
505 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
506 icmp6_ifstat_inc(ifp, ifs6_in_error);
508 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
509 switch (icmp6->icmp6_type) {
510 case ICMP6_DST_UNREACH:
511 icmp6_ifstat_inc(ifp, ifs6_in_dstunreach);
513 case ICMP6_DST_UNREACH_ADMIN:
514 icmp6_ifstat_inc(ifp, ifs6_in_adminprohib);
515 case ICMP6_DST_UNREACH_NOROUTE:
516 case ICMP6_DST_UNREACH_ADDR:
517 case ICMP6_DST_UNREACH_BEYONDSCOPE:
518 case ICMP6_DST_UNREACH_NOPORT:
523 case ICMP6_PACKET_TOO_BIG:
524 icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig);
526 * Validation is made in icmp6_mtudisc_update.
527 * Updating the path MTU will be done after examining
528 * intermediate extension headers.
531 case ICMP6_TIME_EXCEEDED:
532 icmp6_ifstat_inc(ifp, ifs6_in_timeexceed);
534 case ICMP6_TIME_EXCEED_TRANSIT:
535 case ICMP6_TIME_EXCEED_REASSEMBLY:
540 case ICMP6_PARAM_PROB:
541 icmp6_ifstat_inc(ifp, ifs6_in_paramprob);
543 case ICMP6_PARAMPROB_NEXTHEADER:
544 case ICMP6_PARAMPROB_HEADER:
545 case ICMP6_PARAMPROB_OPTION:
550 case ICMP6_ECHO_REQUEST:
551 icmp6_ifstat_inc(ifp, ifs6_in_echo);
554 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
559 || n->m_len < off + sizeof(struct icmp6_hdr)) {
563 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) <= MHLEN);
564 n = m_gethdr(M_NOWAIT, n0->m_type);
571 m_move_pkthdr(n, n0); /* FIB copied. */
572 n0len = n0->m_pkthdr.len; /* save for use below */
574 * Copy IPv6 and ICMPv6 only.
576 nip6 = mtod(n, struct ip6_hdr *);
577 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
578 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
579 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
580 noff = sizeof(struct ip6_hdr);
581 /* new mbuf contains only ipv6+icmpv6 headers */
582 n->m_len = noff + sizeof(struct icmp6_hdr);
584 * Adjust mbuf. ip6_plen will be adjusted in
587 m_adj(n0, off + sizeof(struct icmp6_hdr));
588 /* recalculate complete packet size */
589 n->m_pkthdr.len = n0len + (noff - off);
592 if (n->m_len < off + sizeof(*nicmp6)) {
593 n = m_pullup(n, off + sizeof(*nicmp6));
595 IP6STAT_INC(ip6s_exthdrtoolong);
599 nicmp6 = (struct icmp6_hdr *)(mtod(n, caddr_t) + off);
603 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
604 nicmp6->icmp6_code = 0;
605 ICMP6STAT_INC(icp6s_reflect);
606 ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]);
607 icmp6_reflect(n, noff);
611 case ICMP6_ECHO_REPLY:
612 icmp6_ifstat_inc(ifp, ifs6_in_echoreply);
617 case MLD_LISTENER_QUERY:
618 case MLD_LISTENER_REPORT:
619 case MLD_LISTENER_DONE:
620 case MLDV2_LISTENER_REPORT:
622 * Drop MLD traffic which is not link-local, has a hop limit
623 * of greater than 1 hop, or which does not have the
624 * IPv6 HBH Router Alert option.
625 * As IPv6 HBH options are stripped in ip6_input() we must
626 * check an mbuf header flag.
627 * XXX Should we also sanity check that these messages
628 * were directed to a link-local multicast prefix?
630 if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0)
632 if (mld_input(&m, off, icmp6len) != 0) {
634 return (IPPROTO_DONE);
639 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
641 enum { WRU, FQDN } mode;
644 if (!V_icmp6_nodeinfo)
647 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
649 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
655 sx_slock(&allprison_lock);
656 TAILQ_FOREACH(pr, &allprison, pr_list)
657 if (pr->pr_vnet == ifp->if_vnet)
659 sx_sunlock(&allprison_lock);
661 pr = curthread->td_ucred->cr_prison;
663 if (m->m_len < off + sizeof(struct icmp6_nodeinfo)) {
664 m = m_pullup(m, off +
665 sizeof(struct icmp6_nodeinfo));
667 IP6STAT_INC(ip6s_exthdrtoolong);
669 return (IPPROTO_DONE);
672 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
674 n = ni6_input(n, off, pr);
675 /* XXX meaningless if n == NULL */
676 noff = sizeof(struct ip6_hdr);
682 * XXX: this combination of flags is pointless,
683 * but should we keep this for compatibility?
685 if ((V_icmp6_nodeinfo & (ICMP6_NODEINFO_FQDNOK |
686 ICMP6_NODEINFO_TMPADDROK)) !=
687 (ICMP6_NODEINFO_FQDNOK | ICMP6_NODEINFO_TMPADDROK))
693 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) + 4 <= MHLEN);
694 n = m_gethdr(M_NOWAIT, m->m_type);
699 if (!m_dup_pkthdr(n, m, M_NOWAIT)) {
701 * Previous code did a blind M_COPY_PKTHDR
702 * and said "just for rcvif". If true, then
703 * we could tolerate the dup failing (due to
704 * the deep copy of the tag chain). For now
705 * be conservative and just fail.
712 * Copy IPv6 and ICMPv6 only.
714 nip6 = mtod(n, struct ip6_hdr *);
715 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
716 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
717 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
718 p = (u_char *)(nicmp6 + 1);
721 maxhlen = M_TRAILINGSPACE(n) -
722 (sizeof(*nip6) + sizeof(*nicmp6) + 4);
723 mtx_lock(&pr->pr_mtx);
724 hlen = strlen(pr->pr_hostname);
727 /* meaningless TTL */
728 bcopy(pr->pr_hostname, p + 4, maxhlen);
729 mtx_unlock(&pr->pr_mtx);
730 noff = sizeof(struct ip6_hdr);
731 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
732 sizeof(struct icmp6_hdr) + 4 + maxhlen;
733 nicmp6->icmp6_type = ICMP6_WRUREPLY;
734 nicmp6->icmp6_code = 0;
737 ICMP6STAT_INC(icp6s_reflect);
738 ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]);
739 icmp6_reflect(n, noff);
749 case ND_ROUTER_SOLICIT:
750 icmp6_ifstat_inc(ifp, ifs6_in_routersolicit);
753 if (icmp6len < sizeof(struct nd_router_solicit))
755 if (send_sendso_input_hook != NULL) {
756 if (m->m_len < off + icmp6len) {
757 m = m_pullup(m, off + icmp6len);
759 IP6STAT_INC(ip6s_exthdrtoolong);
761 return (IPPROTO_DONE);
764 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
770 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
771 nd6_rs_input(m, off, icmp6len);
777 case ND_ROUTER_ADVERT:
778 icmp6_ifstat_inc(ifp, ifs6_in_routeradvert);
781 if (icmp6len < sizeof(struct nd_router_advert))
783 if (send_sendso_input_hook != NULL) {
784 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
790 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
791 nd6_ra_input(m, off, icmp6len);
797 case ND_NEIGHBOR_SOLICIT:
798 icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit);
801 if (icmp6len < sizeof(struct nd_neighbor_solicit))
803 if (send_sendso_input_hook != NULL) {
804 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
810 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
811 nd6_ns_input(m, off, icmp6len);
817 case ND_NEIGHBOR_ADVERT:
818 icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert);
821 if (icmp6len < sizeof(struct nd_neighbor_advert))
823 if (send_sendso_input_hook != NULL) {
824 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
830 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
831 nd6_na_input(m, off, icmp6len);
838 icmp6_ifstat_inc(ifp, ifs6_in_redirect);
841 if (icmp6len < sizeof(struct nd_redirect))
843 if (send_sendso_input_hook != NULL) {
844 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
850 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
851 icmp6_redirect_input(m, off);
857 case ICMP6_ROUTER_RENUMBERING:
858 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
859 code != ICMP6_ROUTER_RENUMBERING_RESULT)
861 if (icmp6len < sizeof(struct icmp6_router_renum))
867 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
868 icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
869 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
870 ifp ? ifp->if_index : 0));
871 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
872 /* ICMPv6 error: MUST deliver it by spec... */
875 /* ICMPv6 informational: MUST not deliver */
879 if (icmp6_notify_error(&m, off, icmp6len) != 0) {
880 /* In this case, m should've been freed. */
882 return (IPPROTO_DONE);
887 ICMP6STAT_INC(icp6s_badcode);
891 ICMP6STAT_INC(icp6s_badlen);
895 /* deliver the packet to appropriate sockets */
896 icmp6_rip6_input(&m, *offp);
899 return (IPPROTO_DONE);
904 return (IPPROTO_DONE);
908 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len)
911 struct icmp6_hdr *icmp6;
912 struct ip6_hdr *eip6;
914 struct sockaddr_in6 icmp6src, icmp6dst;
918 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
919 ICMP6STAT_INC(icp6s_tooshort);
923 if (m->m_len < off + sizeof(*icmp6) + sizeof(struct ip6_hdr)) {
924 m = m_pullup(m, off + sizeof(*icmp6) + sizeof(struct ip6_hdr));
926 IP6STAT_INC(ip6s_exthdrtoolong);
931 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
932 eip6 = (struct ip6_hdr *)(icmp6 + 1);
933 bzero(&icmp6dst, sizeof(icmp6dst));
935 /* Detect the upper level protocol */
937 u_int8_t nxt = eip6->ip6_nxt;
938 int eoff = off + sizeof(struct icmp6_hdr) +
939 sizeof(struct ip6_hdr);
940 struct ip6ctlparam ip6cp;
941 int icmp6type = icmp6->icmp6_type;
943 struct ip6_rthdr *rth;
944 struct ip6_rthdr0 *rth0;
947 while (1) { /* XXX: should avoid infinite loop explicitly? */
951 case IPPROTO_HOPOPTS:
952 case IPPROTO_DSTOPTS:
954 if (m->m_len < eoff + sizeof(struct ip6_ext)) {
955 m = m_pullup(m, eoff +
956 sizeof(struct ip6_ext));
958 IP6STAT_INC(ip6s_exthdrtoolong);
963 eh = (struct ip6_ext *)
964 (mtod(m, caddr_t) + eoff);
965 if (nxt == IPPROTO_AH)
966 eoff += (eh->ip6e_len + 2) << 2;
968 eoff += (eh->ip6e_len + 1) << 3;
971 case IPPROTO_ROUTING:
973 * When the erroneous packet contains a
974 * routing header, we should examine the
975 * header to determine the final destination.
976 * Otherwise, we can't properly update
977 * information that depends on the final
978 * destination (e.g. path MTU).
980 if (m->m_len < eoff + sizeof(*rth)) {
981 m = m_pullup(m, eoff + sizeof(*rth));
983 IP6STAT_INC(ip6s_exthdrtoolong);
988 rth = (struct ip6_rthdr *)
989 (mtod(m, caddr_t) + eoff);
990 rthlen = (rth->ip6r_len + 1) << 3;
992 * XXX: currently there is no
993 * officially defined type other
995 * Note that if the segment left field
996 * is 0, all intermediate hops must
999 if (rth->ip6r_segleft &&
1000 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
1003 if (m->m_len < eoff + rthlen) {
1004 m = m_pullup(m, eoff + rthlen);
1007 ip6s_exthdrtoolong);
1012 rth0 = (struct ip6_rthdr0 *)
1013 (mtod(m, caddr_t) + eoff);
1015 /* just ignore a bogus header */
1016 if ((rth0->ip6r0_len % 2) == 0 &&
1017 (hops = rth0->ip6r0_len/2))
1018 icmp6dst.sin6_addr = *((struct in6_addr *)(rth0 + 1) + (hops - 1));
1021 nxt = rth->ip6r_nxt;
1023 case IPPROTO_FRAGMENT:
1024 if (m->m_len < eoff + sizeof(struct ip6_frag)) {
1025 m = m_pullup(m, eoff +
1026 sizeof(struct ip6_frag));
1028 IP6STAT_INC(ip6s_exthdrtoolong);
1033 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1036 * Data after a fragment header is meaningless
1037 * unless it is the first fragment, but
1038 * we'll go to the notify label for path MTU
1041 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1044 eoff += sizeof(struct ip6_frag);
1049 * This case includes ESP and the No Next
1050 * Header. In such cases going to the notify
1051 * label does not have any meaning
1052 * (i.e. ctlfunc will be NULL), but we go
1053 * anyway since we might have to update
1054 * path MTU information.
1060 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1063 * retrieve parameters from the inner IPv6 header, and convert
1064 * them into sockaddr structures.
1065 * XXX: there is no guarantee that the source or destination
1066 * addresses of the inner packet are in the same scope as
1067 * the addresses of the icmp packet. But there is no other
1068 * way to determine the zone.
1070 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1072 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1073 icmp6dst.sin6_family = AF_INET6;
1074 if (IN6_IS_ADDR_UNSPECIFIED(&icmp6dst.sin6_addr))
1075 icmp6dst.sin6_addr = eip6->ip6_dst;
1076 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1078 bzero(&icmp6src, sizeof(icmp6src));
1079 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1080 icmp6src.sin6_family = AF_INET6;
1081 icmp6src.sin6_addr = eip6->ip6_src;
1082 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1084 icmp6src.sin6_flowinfo =
1085 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1088 ip6cp.ip6c_icmp6 = icmp6;
1089 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1090 ip6cp.ip6c_off = eoff;
1091 ip6cp.ip6c_finaldst = &icmp6dst;
1092 ip6cp.ip6c_src = &icmp6src;
1093 ip6cp.ip6c_nxt = nxt;
1095 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1096 notifymtu = ntohl(icmp6->icmp6_mtu);
1097 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1098 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1101 if (ip6_ctlprotox[nxt] != NULL)
1102 ip6_ctlprotox[nxt](&ip6cp);
1114 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1116 struct in6_addr *dst = &ip6cp->ip6c_finaldst->sin6_addr;
1117 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1118 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1119 u_int mtu = ntohl(icmp6->icmp6_mtu);
1120 struct in_conninfo inc;
1125 * RFC2460 section 5, last paragraph.
1126 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1127 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1128 * due to packet translator in the middle.
1129 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1132 if (mtu < IPV6_MMTU)
1137 * we reject ICMPv6 too big with abnormally small value.
1138 * XXX what is the good definition of "abnormally small"?
1140 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1147 * In case the suggested mtu is less than IPV6_MMTU, we
1148 * only need to remember that it was for above mentioned
1149 * "alwaysfrag" case.
1150 * Try to be as close to the spec as possible.
1152 if (mtu < IPV6_MMTU)
1153 mtu = IPV6_MMTU - 8;
1155 bzero(&inc, sizeof(inc));
1156 inc.inc_fibnum = M_GETFIB(m);
1157 inc.inc_flags |= INC_ISIPV6;
1158 inc.inc6_faddr = *dst;
1159 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1162 max_mtu = tcp_hc_getmtu(&inc);
1164 max_mtu = tcp_maxmtu6(&inc, NULL);
1166 if (mtu < max_mtu) {
1167 tcp_hc_updatemtu(&inc, mtu);
1168 ICMP6STAT_INC(icp6s_pmtuchg);
1173 * Process a Node Information Query packet, based on
1174 * draft-ietf-ipngwg-icmp-name-lookups-07.
1176 * Spec incompatibilities:
1177 * - IPv6 Subject address handling
1178 * - IPv4 Subject address handling support missing
1179 * - Proxy reply (answer even if it's not for me)
1180 * - joins NI group address at in6_ifattach() time only, does not cope
1181 * with hostname changes by sethostname(3)
1183 static struct mbuf *
1184 ni6_input(struct mbuf *m, int off, struct prison *pr)
1186 struct icmp6_nodeinfo *ni6, *nni6;
1187 struct mbuf *n = NULL;
1190 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1191 struct ni_reply_fqdn *fqdn;
1192 int addrs; /* for NI_QTYPE_NODEADDR */
1193 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1194 struct in6_addr in6_subj; /* subject address */
1195 struct ip6_hdr *ip6;
1196 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1198 struct in6_ifaddr *ia6 = NULL;
1200 ip6 = mtod(m, struct ip6_hdr *);
1201 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1204 * Validate IPv6 source address.
1205 * The default configuration MUST be to refuse answering queries from
1206 * global-scope addresses according to RFC4602.
1208 * - it's not very clear what "refuse" means; this implementation
1210 * - it's not very easy to identify global-scope (unicast) addresses
1211 * since there are many prefixes for them. It should be safer
1212 * and in practice sufficient to check "all" but loopback and
1213 * link-local (note that site-local unicast was deprecated and
1214 * ULA is defined as global scope-wise)
1216 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1217 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1218 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1222 * Validate IPv6 destination address.
1224 * The Responder must discard the Query without further processing
1225 * unless it is one of the Responder's unicast or anycast addresses, or
1226 * a link-local scope multicast address which the Responder has joined.
1227 * [RFC4602, Section 5.]
1229 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1230 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1232 /* else it's a link-local multicast, fine */
1233 } else { /* unicast or anycast */
1234 ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
1236 goto bad; /* XXX impossible */
1238 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1239 !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1240 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1241 "a temporary address in %s:%d",
1242 __FILE__, __LINE__));
1247 /* validate query Subject field. */
1248 qtype = ntohs(ni6->ni_qtype);
1249 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1252 case NI_QTYPE_SUPTYPES:
1254 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1258 case NI_QTYPE_NODEADDR:
1259 case NI_QTYPE_IPV4ADDR:
1260 switch (ni6->ni_code) {
1261 case ICMP6_NI_SUBJ_IPV6:
1262 #if ICMP6_NI_SUBJ_IPV6 != 0
1266 * backward compatibility - try to accept 03 draft
1267 * format, where no Subject is present.
1269 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1274 #if ICMP6_NI_SUBJ_IPV6 != 0
1275 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1279 if (subjlen != sizeof(struct in6_addr))
1283 * Validate Subject address.
1285 * Not sure what exactly "address belongs to the node"
1286 * means in the spec, is it just unicast, or what?
1288 * At this moment we consider Subject address as
1289 * "belong to the node" if the Subject address equals
1290 * to the IPv6 destination address; validation for
1291 * IPv6 destination address should have done enough
1294 * We do not do proxy at this moment.
1296 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1297 subjlen, (caddr_t)&in6_subj);
1298 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1301 subj = (char *)&in6_subj;
1302 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1306 * XXX if we are to allow other cases, we should really
1307 * be careful about scope here.
1308 * basically, we should disallow queries toward IPv6
1309 * destination X with subject Y,
1310 * if scope(X) > scope(Y).
1311 * if we allow scope(X) > scope(Y), it will result in
1312 * information leakage across scope boundary.
1316 case ICMP6_NI_SUBJ_FQDN:
1318 * Validate Subject name with gethostname(3).
1320 * The behavior may need some debate, since:
1321 * - we are not sure if the node has FQDN as
1322 * hostname (returned by gethostname(3)).
1323 * - the code does wildcard match for truncated names.
1324 * however, we are not sure if we want to perform
1325 * wildcard match, if gethostname(3) side has
1326 * truncated hostname.
1328 mtx_lock(&pr->pr_mtx);
1329 n = ni6_nametodns(pr->pr_hostname,
1330 strlen(pr->pr_hostname), 0);
1331 mtx_unlock(&pr->pr_mtx);
1332 if (!n || n->m_next || n->m_len == 0)
1334 if (m->m_len < off + sizeof(struct icmp6_nodeinfo) +
1336 m = m_pullup(m, off +
1337 sizeof(struct icmp6_nodeinfo) + subjlen);
1339 IP6STAT_INC(ip6s_exthdrtoolong);
1343 /* ip6 possibly invalid but not used after. */
1344 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1345 subj = (char *)(mtod(m, caddr_t) + off +
1346 sizeof(struct icmp6_nodeinfo));
1347 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1355 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1362 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1365 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1368 case NI_QTYPE_NODEADDR:
1369 case NI_QTYPE_IPV4ADDR:
1370 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1375 /* guess reply length */
1378 break; /* no reply data */
1379 case NI_QTYPE_SUPTYPES:
1380 replylen += sizeof(u_int32_t);
1383 /* XXX will append an mbuf */
1384 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1386 case NI_QTYPE_NODEADDR:
1387 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1388 if ((replylen += addrs * (sizeof(struct in6_addr) +
1389 sizeof(u_int32_t))) > MCLBYTES)
1390 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1392 case NI_QTYPE_IPV4ADDR:
1393 /* unsupported - should respond with unknown Qtype? */
1397 * XXX: We must return a reply with the ICMP6 code
1398 * `unknown Qtype' in this case. However we regard the case
1399 * as an FQDN query for backward compatibility.
1400 * Older versions set a random value to this field,
1401 * so it rarely varies in the defined qtypes.
1402 * But the mechanism is not reliable...
1403 * maybe we should obsolete older versions.
1405 qtype = NI_QTYPE_FQDN;
1406 /* XXX will append an mbuf */
1407 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1412 /* Allocate an mbuf to reply. */
1413 if (replylen > MCLBYTES) {
1415 * XXX: should we try to allocate more? But MCLBYTES
1416 * is probably much larger than IPV6_MMTU...
1420 if (replylen > MHLEN)
1421 n = m_getcl(M_NOWAIT, m->m_type, M_PKTHDR);
1423 n = m_gethdr(M_NOWAIT, m->m_type);
1428 m_move_pkthdr(n, m); /* just for recvif and FIB */
1429 n->m_pkthdr.len = n->m_len = replylen;
1431 /* copy mbuf header and IPv6 + Node Information base headers */
1432 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1433 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1434 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1436 /* qtype dependent procedure */
1439 nni6->ni_code = ICMP6_NI_SUCCESS;
1442 case NI_QTYPE_SUPTYPES:
1445 nni6->ni_code = ICMP6_NI_SUCCESS;
1446 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1447 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1448 v = (u_int32_t)htonl(0x0000000f);
1449 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1453 nni6->ni_code = ICMP6_NI_SUCCESS;
1454 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1455 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1456 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1457 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1459 * XXX do we really have FQDN in hostname?
1461 mtx_lock(&pr->pr_mtx);
1462 n->m_next = ni6_nametodns(pr->pr_hostname,
1463 strlen(pr->pr_hostname), oldfqdn);
1464 mtx_unlock(&pr->pr_mtx);
1465 if (n->m_next == NULL)
1467 /* XXX we assume that n->m_next is not a chain */
1468 if (n->m_next->m_next != NULL)
1470 n->m_pkthdr.len += n->m_next->m_len;
1472 case NI_QTYPE_NODEADDR:
1476 nni6->ni_code = ICMP6_NI_SUCCESS;
1477 n->m_pkthdr.len = n->m_len =
1478 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1479 lenlim = M_TRAILINGSPACE(n);
1480 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1481 /* XXX: reset mbuf length */
1482 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1483 sizeof(struct icmp6_nodeinfo) + copied;
1487 break; /* XXX impossible! */
1490 nni6->ni_type = ICMP6_NI_REPLY;
1502 * make a mbuf with DNS-encoded string. no compression support.
1504 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1505 * treated as truncated name (two \0 at the end). this is a wild guess.
1507 * old - return pascal string if non-zero
1509 static struct mbuf *
1510 ni6_nametodns(const char *name, int namelen, int old)
1522 /* Because MAXHOSTNAMELEN is usually 256, we use cluster mbuf. */
1524 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1526 m = m_get(M_NOWAIT, MT_DATA);
1532 *mtod(m, char *) = namelen;
1533 bcopy(name, mtod(m, char *) + 1, namelen);
1537 cp = mtod(m, char *);
1538 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1540 /* if not certain about my name, return empty buffer */
1545 * guess if it looks like shortened hostname, or FQDN.
1546 * shortened hostname needs two trailing "\0".
1549 for (p = name; p < name + namelen; p++) {
1550 if (*p && *p == '.')
1559 while (cp < ep && p < name + namelen) {
1561 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1563 /* result does not fit into mbuf */
1564 if (cp + i + 1 >= ep)
1567 * DNS label length restriction, RFC1035 page 8.
1568 * "i == 0" case is included here to avoid returning
1569 * 0-length label on "foo..bar".
1571 if (i <= 0 || i >= 64)
1577 if (p < name + namelen && *p == '.')
1581 if (cp + nterm >= ep)
1585 m->m_len = cp - mtod(m, char *);
1589 panic("should not reach here");
1599 * check if two DNS-encoded string matches. takes care of truncated
1600 * form (with \0\0 at the end). no compression support.
1601 * XXX upper/lowercase match (see RFC2065)
1604 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1606 const char *a0, *b0;
1609 /* simplest case - need validation? */
1610 if (alen == blen && bcmp(a, b, alen) == 0)
1616 /* termination is mandatory */
1617 if (alen < 2 || blen < 2)
1619 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1624 while (a - a0 < alen && b - b0 < blen) {
1625 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1628 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1630 /* we don't support compression yet */
1631 if (a[0] >= 64 || b[0] >= 64)
1634 /* truncated case */
1635 if (a[0] == 0 && a - a0 == alen - 1)
1637 if (b[0] == 0 && b - b0 == blen - 1)
1639 if (a[0] == 0 || b[0] == 0)
1645 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1647 if (bcmp(a + 1, b + 1, l) != 0)
1654 if (a - a0 == alen && b - b0 == blen)
1661 * calculate the number of addresses to be returned in the node info reply.
1664 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1665 struct in6_addr *subj)
1668 struct in6_ifaddr *ifa6;
1670 int addrs = 0, addrsofif, iffound = 0;
1671 int niflags = ni6->ni_flags;
1675 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1676 switch (ni6->ni_code) {
1677 case ICMP6_NI_SUBJ_IPV6:
1678 if (subj == NULL) /* must be impossible... */
1683 * XXX: we only support IPv6 subject address for
1690 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1692 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1693 if (ifa->ifa_addr->sa_family != AF_INET6)
1695 ifa6 = (struct in6_ifaddr *)ifa;
1697 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1698 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1702 * IPv4-mapped addresses can only be returned by a
1703 * Node Information proxy, since they represent
1704 * addresses of IPv4-only nodes, which perforce do
1705 * not implement this protocol.
1706 * [icmp-name-lookups-07, Section 5.4]
1707 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1708 * this function at this moment.
1711 /* What do we have to do about ::1? */
1712 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1713 case IPV6_ADDR_SCOPE_LINKLOCAL:
1714 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1717 case IPV6_ADDR_SCOPE_SITELOCAL:
1718 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1721 case IPV6_ADDR_SCOPE_GLOBAL:
1722 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1730 * check if anycast is okay.
1731 * XXX: just experimental. not in the spec.
1733 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1734 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1735 continue; /* we need only unicast addresses */
1736 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1737 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1740 addrsofif++; /* count the address */
1754 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1755 struct ifnet *ifp0, int resid)
1758 struct in6_ifaddr *ifa6;
1760 struct ifnet *ifp_dep = NULL;
1761 int copied = 0, allow_deprecated = 0;
1762 u_char *cp = (u_char *)(nni6 + 1);
1763 int niflags = ni6->ni_flags;
1768 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1769 return (0); /* needless to copy */
1771 ifp = ifp0 ? ifp0 : CK_STAILQ_FIRST(&V_ifnet);
1774 for (; ifp; ifp = CK_STAILQ_NEXT(ifp, if_link)) {
1775 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1776 if (ifa->ifa_addr->sa_family != AF_INET6)
1778 ifa6 = (struct in6_ifaddr *)ifa;
1780 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1781 allow_deprecated == 0) {
1783 * prefererred address should be put before
1784 * deprecated addresses.
1787 /* record the interface for later search */
1788 if (ifp_dep == NULL)
1792 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1793 allow_deprecated != 0)
1794 continue; /* we now collect deprecated addrs */
1796 /* What do we have to do about ::1? */
1797 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1798 case IPV6_ADDR_SCOPE_LINKLOCAL:
1799 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1802 case IPV6_ADDR_SCOPE_SITELOCAL:
1803 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1806 case IPV6_ADDR_SCOPE_GLOBAL:
1807 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1815 * check if anycast is okay.
1816 * XXX: just experimental. not in the spec.
1818 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1819 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1821 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1822 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1826 /* now we can copy the address */
1827 if (resid < sizeof(struct in6_addr) +
1828 sizeof(u_int32_t)) {
1830 * We give up much more copy.
1831 * Set the truncate flag and return.
1833 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1838 * Set the TTL of the address.
1839 * The TTL value should be one of the following
1840 * according to the specification:
1842 * 1. The remaining lifetime of a DHCP lease on the
1844 * 2. The remaining Valid Lifetime of a prefix from
1845 * which the address was derived through Stateless
1846 * Autoconfiguration.
1848 * Note that we currently do not support stateful
1849 * address configuration by DHCPv6, so the former
1850 * case can't happen.
1852 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1853 ltime = ND6_INFINITE_LIFETIME;
1855 if (ifa6->ia6_lifetime.ia6t_expire >
1857 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_uptime);
1862 bcopy(<ime, cp, sizeof(u_int32_t));
1863 cp += sizeof(u_int32_t);
1865 /* copy the address itself */
1866 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1867 sizeof(struct in6_addr));
1868 in6_clearscope((struct in6_addr *)cp); /* XXX */
1869 cp += sizeof(struct in6_addr);
1871 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1872 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1874 if (ifp0) /* we need search only on the specified IF */
1878 if (allow_deprecated == 0 && ifp_dep != NULL) {
1880 allow_deprecated = 1;
1889 icmp6_rip6_match(const struct inpcb *inp, void *v)
1891 struct ip6_hdr *ip6 = v;
1893 if ((inp->inp_vflag & INP_IPV6) == 0)
1895 if (inp->inp_ip_p != IPPROTO_ICMPV6)
1897 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1898 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst))
1900 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1901 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src))
1907 * XXX almost dup'ed code with rip6_input.
1910 icmp6_rip6_input(struct mbuf **mp, int off)
1912 struct mbuf *n, *m = *mp;
1913 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1914 struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo,
1915 INPLOOKUP_RLOCKPCB, icmp6_rip6_match, ip6);
1917 struct sockaddr_in6 fromsa;
1918 struct icmp6_hdr *icmp6;
1919 struct mbuf *opts = NULL;
1922 /* This is assumed to be safe; icmp6_input() does a pullup. */
1923 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1926 * XXX: the address may have embedded scope zone ID, which should be
1927 * hidden from applications.
1929 bzero(&fromsa, sizeof(fromsa));
1930 fromsa.sin6_family = AF_INET6;
1931 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1932 fromsa.sin6_addr = ip6->ip6_src;
1933 if (sa6_recoverscope(&fromsa)) {
1936 return (IPPROTO_DONE);
1939 while ((inp = inp_next(&inpi)) != NULL) {
1940 if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1941 inp->in6p_icmp6filt))
1944 * Recent network drivers tend to allocate a single
1945 * mbuf cluster, rather than to make a couple of
1946 * mbufs without clusters. Also, since the IPv6 code
1947 * path tries to avoid m_pullup(), it is highly
1948 * probable that we still have an mbuf cluster here
1949 * even though the necessary length can be stored in an
1950 * mbuf's internal buffer.
1951 * Meanwhile, the default size of the receive socket
1952 * buffer for raw sockets is not so large. This means
1953 * the possibility of packet loss is relatively higher
1954 * than before. To avoid this scenario, we copy the
1955 * received data to a separate mbuf that does not use
1956 * a cluster, if possible.
1957 * XXX: it is better to copy the data after stripping
1958 * intermediate headers.
1960 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1961 m->m_len <= MHLEN) {
1962 n = m_get(M_NOWAIT, m->m_type);
1964 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1965 bcopy(m->m_data, n->m_data, m->m_len);
1966 n->m_len = m->m_len;
1973 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1976 if (inp->inp_flags & INP_CONTROLOPTS)
1977 ip6_savecontrol(inp, n, &opts);
1978 /* strip intermediate headers */
1980 SOCKBUF_LOCK(&inp->inp_socket->so_rcv);
1981 if (sbappendaddr_locked(&inp->inp_socket->so_rcv,
1982 (struct sockaddr *)&fromsa, n, opts) == 0) {
1983 soroverflow_locked(inp->inp_socket);
1988 sorwakeup_locked(inp->inp_socket);
1996 IP6STAT_DEC(ip6s_delivered);
1997 return (IPPROTO_DONE);
2001 * Reflect the ip6 packet back to the source.
2002 * OFF points to the icmp6 header, counted from the top of the mbuf.
2005 icmp6_reflect(struct mbuf *m, size_t off)
2007 struct in6_addr src6, *srcp;
2008 struct ip6_hdr *ip6;
2009 struct icmp6_hdr *icmp6;
2010 struct in6_ifaddr *ia = NULL;
2011 struct ifnet *outif = NULL;
2013 int type, code, hlim;
2015 /* too short to reflect */
2016 if (off < sizeof(struct ip6_hdr)) {
2018 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2019 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2020 __FILE__, __LINE__));
2025 * If there are extra headers between IPv6 and ICMPv6, strip
2026 * off that header first.
2029 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2030 panic("assumption failed in icmp6_reflect");
2032 if (off > sizeof(struct ip6_hdr)) {
2034 struct ip6_hdr nip6;
2036 l = off - sizeof(struct ip6_hdr);
2037 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2039 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2041 if ((m = m_pullup(m, l)) == NULL)
2044 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2045 } else /* off == sizeof(struct ip6_hdr) */ {
2047 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2049 if ((m = m_pullup(m, l)) == NULL)
2053 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2054 ip6 = mtod(m, struct ip6_hdr *);
2055 ip6->ip6_nxt = IPPROTO_ICMPV6;
2056 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2057 type = icmp6->icmp6_type; /* keep type for statistics */
2058 code = icmp6->icmp6_code; /* ditto. */
2063 * If the incoming packet was addressed directly to us (i.e. unicast),
2064 * use dst as the src for the reply.
2065 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2066 * (for example) when we encounter an error while forwarding procedure
2067 * destined to a duplicated address of ours.
2069 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
2070 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
2071 if (ia != NULL && !(ia->ia6_flags &
2072 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2073 src6 = ia->ia_addr.sin6_addr;
2076 if (m->m_pkthdr.rcvif != NULL) {
2077 /* XXX: This may not be the outgoing interface */
2078 hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2080 hlim = V_ip6_defhlim;
2086 struct in6_addr dst6;
2090 * This case matches to multicasts, our anycast, or unicasts
2091 * that we do not own. Select a source address based on the
2092 * source address of the erroneous packet.
2094 in6_splitscope(&ip6->ip6_src, &dst6, &scopeid);
2095 error = in6_selectsrc_addr(M_GETFIB(m), &dst6,
2096 scopeid, NULL, &src6, &hlim);
2099 char ip6buf[INET6_ADDRSTRLEN];
2101 "icmp6_reflect: source can't be determined: "
2102 "dst=%s, error=%d\n",
2103 ip6_sprintf(ip6buf, &ip6->ip6_dst), error));
2109 * ip6_input() drops a packet if its src is multicast.
2110 * So, the src is never multicast.
2112 ip6->ip6_dst = ip6->ip6_src;
2113 ip6->ip6_src = *srcp;
2115 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2116 ip6->ip6_vfc |= IPV6_VERSION;
2117 ip6->ip6_nxt = IPPROTO_ICMPV6;
2118 ip6->ip6_hlim = hlim;
2120 icmp6->icmp6_cksum = 0;
2121 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2122 sizeof(struct ip6_hdr), plen);
2125 * XXX option handling
2128 m->m_flags &= ~(M_BCAST|M_MCAST);
2129 m->m_pkthdr.rcvif = NULL;
2130 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2132 icmp6_ifoutstat_inc(outif, type, code);
2142 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2143 struct in6_addr *tgt6)
2145 static char buf[1024];
2146 char ip6bufs[INET6_ADDRSTRLEN];
2147 char ip6bufd[INET6_ADDRSTRLEN];
2148 char ip6buft[INET6_ADDRSTRLEN];
2149 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2150 ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2151 ip6_sprintf(ip6buft, tgt6));
2156 icmp6_redirect_input(struct mbuf *m, int off)
2159 struct ip6_hdr *ip6;
2160 struct nd_redirect *nd_rd;
2161 struct in6_addr src6, redtgt6, reddst6;
2162 union nd_opts ndopts;
2163 char ip6buf[INET6_ADDRSTRLEN];
2165 int icmp6len, is_onlink, is_router, lladdrlen;
2168 KASSERT(m->m_pkthdr.rcvif != NULL, ("%s: no rcvif", __func__));
2170 /* XXX if we are router, we don't update route by icmp6 redirect */
2171 if (V_ip6_forwarding)
2173 if (!V_icmp6_rediraccept)
2176 /* RFC 6980: Nodes MUST silently ignore fragments */
2177 if(m->m_flags & M_FRAGMENTED)
2180 ip6 = mtod(m, struct ip6_hdr *);
2181 icmp6len = ntohs(ip6->ip6_plen);
2182 if (m->m_len < off + icmp6len) {
2183 m = m_pullup(m, off + icmp6len);
2185 IP6STAT_INC(ip6s_exthdrtoolong);
2189 ip6 = mtod(m, struct ip6_hdr *);
2190 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2192 ifp = m->m_pkthdr.rcvif;
2193 redtgt6 = nd_rd->nd_rd_target;
2194 reddst6 = nd_rd->nd_rd_dst;
2196 if (in6_setscope(&redtgt6, ifp, NULL) ||
2197 in6_setscope(&reddst6, ifp, NULL)) {
2202 src6 = ip6->ip6_src;
2203 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2205 "ICMP6 redirect sent from %s rejected; "
2206 "must be from linklocal\n",
2207 ip6_sprintf(ip6buf, &src6)));
2210 if (__predict_false(ip6->ip6_hlim != 255)) {
2211 ICMP6STAT_INC(icp6s_invlhlim);
2213 "ICMP6 redirect sent from %s rejected; "
2214 "hlim=%d (must be 255)\n",
2215 ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
2219 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2220 struct nhop_object *nh;
2221 struct in6_addr kdst;
2224 in6_splitscope(&reddst6, &kdst, &scopeid);
2226 nh = fib6_lookup(ifp->if_fib, &kdst, scopeid, 0, 0);
2228 struct in6_addr nh_addr;
2229 nh_addr = ifatoia6(nh->nh_ifa)->ia_addr.sin6_addr;
2230 if ((nh->nh_flags & NHF_GATEWAY) == 0) {
2232 "ICMP6 redirect rejected; no route "
2233 "with inet6 gateway found for redirect dst: %s\n",
2234 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2239 * Embed scope zone id into next hop address.
2241 nh_addr = nh->gw6_sa.sin6_addr;
2243 if (IN6_ARE_ADDR_EQUAL(&src6, &nh_addr) == 0) {
2245 "ICMP6 redirect rejected; "
2246 "not equal to gw-for-src=%s (must be same): "
2248 ip6_sprintf(ip6buf, &nh_addr),
2249 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2254 "ICMP6 redirect rejected; "
2255 "no route found for redirect dst: %s\n",
2256 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2260 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2262 "ICMP6 redirect rejected; "
2263 "redirect dst must be unicast: %s\n",
2264 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2268 is_router = is_onlink = 0;
2269 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2270 is_router = 1; /* router case */
2271 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2272 is_onlink = 1; /* on-link destination case */
2273 if (!is_router && !is_onlink) {
2275 "ICMP6 redirect rejected; "
2276 "neither router case nor onlink case: %s\n",
2277 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2281 icmp6len -= sizeof(*nd_rd);
2282 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2283 if (nd6_options(&ndopts) < 0) {
2284 nd6log((LOG_INFO, "%s: invalid ND option, rejected: %s\n",
2285 __func__, icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2286 /* nd6_options have incremented stats */
2292 if (ndopts.nd_opts_tgt_lladdr) {
2293 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2294 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2297 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2298 nd6log((LOG_INFO, "%s: lladdrlen mismatch for %s "
2299 "(if %d, icmp6 packet %d): %s\n",
2300 __func__, ip6_sprintf(ip6buf, &redtgt6),
2301 ifp->if_addrlen, lladdrlen - 2,
2302 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2306 /* Validation passed. */
2309 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2310 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2313 * Install a gateway route in the better-router case or an interface
2314 * route in the on-link-destination case.
2317 struct sockaddr_in6 sdst;
2318 struct sockaddr_in6 sgw;
2319 struct sockaddr_in6 ssrc;
2320 struct sockaddr *gw;
2324 bzero(&sdst, sizeof(sdst));
2325 bzero(&ssrc, sizeof(ssrc));
2326 sdst.sin6_family = ssrc.sin6_family = AF_INET6;
2327 sdst.sin6_len = ssrc.sin6_len = sizeof(struct sockaddr_in6);
2328 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2329 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2332 bzero(&sgw, sizeof(sgw));
2333 sgw.sin6_family = AF_INET6;
2334 sgw.sin6_len = sizeof(struct sockaddr_in6);
2335 bcopy(&redtgt6, &sgw.sin6_addr,
2336 sizeof(struct in6_addr));
2337 gw = (struct sockaddr *)&sgw;
2338 rt_flags |= RTF_GATEWAY;
2340 gw = ifp->if_addr->ifa_addr;
2341 for (fibnum = 0; fibnum < rt_numfibs; fibnum++)
2342 rib_add_redirect(fibnum, (struct sockaddr *)&sdst, gw,
2343 (struct sockaddr *)&ssrc, ifp, rt_flags,
2344 V_icmp6_redirtimeout);
2352 ICMP6STAT_INC(icp6s_badredirect);
2357 icmp6_redirect_output(struct mbuf *m0, struct nhop_object *nh)
2359 struct ifnet *ifp; /* my outgoing interface */
2360 struct in6_addr *ifp_ll6;
2361 struct in6_addr *router_ll6;
2362 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2363 struct mbuf *m = NULL; /* newly allocated one */
2365 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2366 struct nd_redirect *nd_rd;
2367 struct llentry *ln = NULL;
2370 struct ifnet *outif = NULL;
2371 struct sockaddr_in6 src_sa;
2373 icmp6_errcount(ND_REDIRECT, 0);
2375 /* if we are not router, we don't send icmp6 redirect */
2376 if (!V_ip6_forwarding)
2380 if (!m0 || !nh || !(NH_IS_VALID(nh)) || !(ifp = nh->nh_ifp))
2385 * the source address must identify a neighbor, and
2386 * the destination address must not be a multicast address
2387 * [RFC 2461, sec 8.2]
2389 sip6 = mtod(m0, struct ip6_hdr *);
2390 bzero(&src_sa, sizeof(src_sa));
2391 src_sa.sin6_family = AF_INET6;
2392 src_sa.sin6_len = sizeof(src_sa);
2393 src_sa.sin6_addr = sip6->ip6_src;
2394 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2396 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2397 goto fail; /* what should we do here? */
2400 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2404 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2405 * we almost always ask for an mbuf cluster for simplicity.
2406 * (MHLEN < IPV6_MMTU is almost always true)
2408 #if IPV6_MMTU >= MCLBYTES
2409 # error assumption failed about IPV6_MMTU and MCLBYTES
2411 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2414 M_SETFIB(m, M_GETFIB(m0));
2415 maxlen = M_TRAILINGSPACE(m);
2416 maxlen = min(IPV6_MMTU, maxlen);
2417 /* just for safety */
2418 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2419 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2424 /* get ip6 linklocal address for ifp(my outgoing interface). */
2425 struct in6_ifaddr *ia;
2426 if ((ia = in6ifa_ifpforlinklocal(ifp,
2428 IN6_IFF_ANYCAST)) == NULL)
2430 ifp_ll6 = &ia->ia_addr.sin6_addr;
2431 /* XXXRW: reference released prematurely. */
2432 ifa_free(&ia->ia_ifa);
2435 /* get ip6 linklocal address for the router. */
2436 if (nh->nh_flags & NHF_GATEWAY) {
2437 struct sockaddr_in6 *sin6;
2439 router_ll6 = &sin6->sin6_addr;
2440 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2441 router_ll6 = (struct in6_addr *)NULL;
2443 router_ll6 = (struct in6_addr *)NULL;
2446 ip6 = mtod(m, struct ip6_hdr *);
2448 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2449 ip6->ip6_vfc |= IPV6_VERSION;
2450 /* ip6->ip6_plen will be set later */
2451 ip6->ip6_nxt = IPPROTO_ICMPV6;
2452 ip6->ip6_hlim = 255;
2453 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2454 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2455 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2458 nd_rd = (struct nd_redirect *)(ip6 + 1);
2459 nd_rd->nd_rd_type = ND_REDIRECT;
2460 nd_rd->nd_rd_code = 0;
2461 nd_rd->nd_rd_reserved = 0;
2462 if (nh->nh_flags & NHF_GATEWAY) {
2464 * nd_rd->nd_rd_target must be a link-local address in
2465 * better router cases.
2469 bcopy(router_ll6, &nd_rd->nd_rd_target,
2470 sizeof(nd_rd->nd_rd_target));
2471 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2472 sizeof(nd_rd->nd_rd_dst));
2474 /* make sure redtgt == reddst */
2475 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2476 sizeof(nd_rd->nd_rd_target));
2477 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2478 sizeof(nd_rd->nd_rd_dst));
2481 p = (u_char *)(nd_rd + 1);
2487 /* target lladdr option */
2489 struct nd_opt_hdr *nd_opt;
2492 ln = nd6_lookup(router_ll6, LLE_SF(AF_INET6, 0), ifp);
2496 len = sizeof(*nd_opt) + ifp->if_addrlen;
2497 len = (len + 7) & ~7; /* round by 8 */
2499 if (len + (p - (u_char *)ip6) > maxlen)
2502 if (ln->la_flags & LLE_VALID) {
2503 nd_opt = (struct nd_opt_hdr *)p;
2504 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2505 nd_opt->nd_opt_len = len >> 3;
2506 lladdr = (char *)(nd_opt + 1);
2507 bcopy(ln->ll_addr, lladdr, ifp->if_addrlen);
2515 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2517 /* just to be safe */
2518 #ifdef M_DECRYPTED /*not openbsd*/
2519 if (m0->m_flags & M_DECRYPTED)
2522 if (p - (u_char *)ip6 > maxlen)
2526 /* redirected header option */
2528 struct nd_opt_rd_hdr *nd_opt_rh;
2531 * compute the maximum size for icmp6 redirect header option.
2532 * XXX room for auth header?
2534 len = maxlen - (p - (u_char *)ip6);
2537 /* This is just for simplicity. */
2538 if (m0->m_pkthdr.len != m0->m_len) {
2540 m_freem(m0->m_next);
2543 m0->m_pkthdr.len = m0->m_len;
2547 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2548 * about padding/truncate rule for the original IP packet.
2549 * From the discussion on IPv6imp in Feb 1999,
2550 * the consensus was:
2551 * - "attach as much as possible" is the goal
2552 * - pad if not aligned (original size can be guessed by
2553 * original ip6 header)
2554 * Following code adds the padding if it is simple enough,
2555 * and truncates if not.
2557 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2558 panic("assumption failed in %s:%d", __FILE__,
2561 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2562 /* not enough room, truncate */
2563 m0->m_pkthdr.len = m0->m_len = len -
2566 /* enough room, pad or truncate */
2569 extra = m0->m_pkthdr.len % 8;
2571 /* pad if easy enough, truncate if not */
2572 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2574 m0->m_len += (8 - extra);
2575 m0->m_pkthdr.len += (8 - extra);
2578 m0->m_pkthdr.len -= extra;
2582 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2583 m0->m_pkthdr.len = m0->m_len = len -
2587 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2588 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2589 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2590 nd_opt_rh->nd_opt_rh_len = len >> 3;
2591 p += sizeof(*nd_opt_rh);
2592 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2594 /* connect m0 to m */
2595 m_tag_delete_chain(m0, NULL);
2596 m0->m_flags &= ~M_PKTHDR;
2598 m->m_pkthdr.len = m->m_len + m0->m_len;
2607 /* XXX: clear embedded link IDs in the inner header */
2608 in6_clearscope(&sip6->ip6_src);
2609 in6_clearscope(&sip6->ip6_dst);
2610 in6_clearscope(&nd_rd->nd_rd_target);
2611 in6_clearscope(&nd_rd->nd_rd_dst);
2613 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2615 nd_rd->nd_rd_cksum = 0;
2616 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2617 sizeof(*ip6), ntohs(ip6->ip6_plen));
2619 if (send_sendso_input_hook != NULL) {
2620 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short),
2624 *(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type;
2625 m_tag_prepend(m, mtag);
2628 /* send the packet to outside... */
2629 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2631 icmp6_ifstat_inc(outif, ifs6_out_msg);
2632 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2634 ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]);
2646 * ICMPv6 socket option processing.
2649 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2653 struct inpcb *inp = sotoinpcb(so);
2654 int level, op, optname;
2657 level = sopt->sopt_level;
2658 op = sopt->sopt_dir;
2659 optname = sopt->sopt_name;
2660 optlen = sopt->sopt_valsize;
2662 level = op = optname = optlen = 0;
2664 if (level != IPPROTO_ICMPV6) {
2673 struct icmp6_filter ic6f;
2675 if (optlen != sizeof(ic6f)) {
2679 error = sooptcopyin(sopt, &ic6f, optlen, optlen);
2682 *inp->in6p_icmp6filt = ic6f;
2689 error = ENOPROTOOPT;
2698 struct icmp6_filter ic6f;
2701 ic6f = *inp->in6p_icmp6filt;
2703 error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
2708 error = ENOPROTOOPT;
2718 * Perform rate limit check.
2719 * Returns 0 if it is okay to send the icmp6 packet.
2720 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2723 * XXX per-destination/type check necessary?
2725 * dst - not used at this moment
2726 * type - not used at this moment
2727 * code - not used at this moment
2730 icmp6_ratelimit(const struct in6_addr *dst, const int type,
2735 ret = 0; /* okay to send */
2738 if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count,
2739 V_icmp6errppslim)) {
2740 /* The packet is subject to rate limit */