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/ip6protosw.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 struct domain inet6domain;
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(struct inpcbhead, ripcb);
128 VNET_DECLARE(int, icmp6errppslim);
129 VNET_DEFINE_STATIC(int, icmp6errpps_count) = 0;
130 VNET_DEFINE_STATIC(struct timeval, icmp6errppslim_last);
131 VNET_DECLARE(int, icmp6_nodeinfo);
133 #define V_ripcbinfo VNET(ripcbinfo)
134 #define V_ripcb VNET(ripcb)
135 #define V_icmp6errppslim VNET(icmp6errppslim)
136 #define V_icmp6errpps_count VNET(icmp6errpps_count)
137 #define V_icmp6errppslim_last VNET(icmp6errppslim_last)
138 #define V_icmp6_nodeinfo VNET(icmp6_nodeinfo)
140 static void icmp6_errcount(int, int);
141 static int icmp6_rip6_input(struct mbuf **, int);
142 static void icmp6_reflect(struct mbuf *, size_t);
143 static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
144 static const char *icmp6_redirect_diag(struct in6_addr *,
145 struct in6_addr *, struct in6_addr *);
146 static struct mbuf *ni6_input(struct mbuf *, int, struct prison *);
147 static struct mbuf *ni6_nametodns(const char *, int, int);
148 static int ni6_dnsmatch(const char *, int, const char *, int);
149 static int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *,
150 struct ifnet **, struct in6_addr *);
151 static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
152 struct ifnet *, int);
153 static int icmp6_notify_error(struct mbuf **, int, int, int);
156 * Kernel module interface for updating icmp6stat. The argument is an index
157 * into icmp6stat treated as an array of u_quad_t. While this encodes the
158 * general layout of icmp6stat into the caller, it doesn't encode its
159 * location, so that future changes to add, for example, per-CPU stats
160 * support won't cause binary compatibility problems for kernel modules.
163 kmod_icmp6stat_inc(int statnum)
166 counter_u64_add(VNET(icmp6stat)[statnum], 1);
170 icmp6_errcount(int type, int code)
173 case ICMP6_DST_UNREACH:
175 case ICMP6_DST_UNREACH_NOROUTE:
176 ICMP6STAT_INC(icp6s_odst_unreach_noroute);
178 case ICMP6_DST_UNREACH_ADMIN:
179 ICMP6STAT_INC(icp6s_odst_unreach_admin);
181 case ICMP6_DST_UNREACH_BEYONDSCOPE:
182 ICMP6STAT_INC(icp6s_odst_unreach_beyondscope);
184 case ICMP6_DST_UNREACH_ADDR:
185 ICMP6STAT_INC(icp6s_odst_unreach_addr);
187 case ICMP6_DST_UNREACH_NOPORT:
188 ICMP6STAT_INC(icp6s_odst_unreach_noport);
192 case ICMP6_PACKET_TOO_BIG:
193 ICMP6STAT_INC(icp6s_opacket_too_big);
195 case ICMP6_TIME_EXCEEDED:
197 case ICMP6_TIME_EXCEED_TRANSIT:
198 ICMP6STAT_INC(icp6s_otime_exceed_transit);
200 case ICMP6_TIME_EXCEED_REASSEMBLY:
201 ICMP6STAT_INC(icp6s_otime_exceed_reassembly);
205 case ICMP6_PARAM_PROB:
207 case ICMP6_PARAMPROB_HEADER:
208 ICMP6STAT_INC(icp6s_oparamprob_header);
210 case ICMP6_PARAMPROB_NEXTHEADER:
211 ICMP6STAT_INC(icp6s_oparamprob_nextheader);
213 case ICMP6_PARAMPROB_OPTION:
214 ICMP6STAT_INC(icp6s_oparamprob_option);
219 ICMP6STAT_INC(icp6s_oredirect);
222 ICMP6STAT_INC(icp6s_ounknown);
226 * A wrapper function for icmp6_error() necessary when the erroneous packet
227 * may not contain enough scope zone information.
230 icmp6_error2(struct mbuf *m, int type, int code, int param,
238 if (m->m_len < sizeof(struct ip6_hdr)) {
239 m = m_pullup(m, sizeof(struct ip6_hdr));
241 IP6STAT_INC(ip6s_exthdrtoolong);
245 ip6 = mtod(m, struct ip6_hdr *);
247 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
249 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
252 icmp6_error(m, type, code, param);
256 * Generate an error packet of type error in response to bad IP6 packet.
259 icmp6_error(struct mbuf *m, int type, int code, int param)
261 struct ip6_hdr *oip6, *nip6;
262 struct icmp6_hdr *icmp6;
267 ICMP6STAT_INC(icp6s_error);
269 /* count per-type-code statistics */
270 icmp6_errcount(type, code);
272 #ifdef M_DECRYPTED /*not openbsd*/
273 if (m->m_flags & M_DECRYPTED) {
274 ICMP6STAT_INC(icp6s_canterror);
279 if (m->m_len < sizeof(struct ip6_hdr)) {
280 m = m_pullup(m, sizeof(struct ip6_hdr));
282 IP6STAT_INC(ip6s_exthdrtoolong);
286 oip6 = mtod(m, struct ip6_hdr *);
289 * If the destination address of the erroneous packet is a multicast
290 * address, or the packet was sent using link-layer multicast,
291 * we should basically suppress sending an error (RFC 2463, Section
293 * We have two exceptions (the item e.2 in that section):
294 * - the Packet Too Big message can be sent for path MTU discovery.
295 * - the Parameter Problem Message that can be allowed an icmp6 error
296 * in the option type field. This check has been done in
297 * ip6_unknown_opt(), so we can just check the type and code.
299 if ((m->m_flags & (M_BCAST|M_MCAST) ||
300 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
301 (type != ICMP6_PACKET_TOO_BIG &&
302 (type != ICMP6_PARAM_PROB ||
303 code != ICMP6_PARAMPROB_OPTION)))
307 * RFC 2463, 2.4 (e.5): source address check.
308 * XXX: the case of anycast source?
310 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
311 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
315 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
319 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
320 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
321 struct icmp6_hdr *icp;
323 if (m->m_len < off + sizeof(struct icmp6_hdr)) {
324 m = m_pullup(m, off + sizeof(struct icmp6_hdr));
326 IP6STAT_INC(ip6s_exthdrtoolong);
330 oip6 = mtod(m, struct ip6_hdr *);
331 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
333 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
334 icp->icmp6_type == ND_REDIRECT) {
337 * Special case: for redirect (which is
338 * informational) we must not send icmp6 error.
340 ICMP6STAT_INC(icp6s_canterror);
343 /* ICMPv6 informational - send the error */
346 /* non-ICMPv6 - send the error */
349 /* Finally, do rate limitation check. */
350 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
351 ICMP6STAT_INC(icp6s_toofreq);
356 * OK, ICMP6 can be generated.
359 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
360 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
362 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
363 M_PREPEND(m, preplen, M_NOWAIT); /* FIB is also copied over. */
365 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
369 nip6 = mtod(m, struct ip6_hdr *);
370 nip6->ip6_src = oip6->ip6_src;
371 nip6->ip6_dst = oip6->ip6_dst;
373 in6_clearscope(&oip6->ip6_src);
374 in6_clearscope(&oip6->ip6_dst);
376 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
377 icmp6->icmp6_type = type;
378 icmp6->icmp6_code = code;
379 icmp6->icmp6_pptr = htonl((u_int32_t)param);
381 ICMP6STAT_INC(icp6s_outhist[type]);
382 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 * Process a received ICMP6 message.
397 icmp6_input(struct mbuf **mp, int *offp, int proto)
401 struct ip6_hdr *ip6, *nip6;
402 struct icmp6_hdr *icmp6, *nicmp6;
403 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
404 int code, error, icmp6len, ip6len, noff, off, sum;
411 if (m->m_len < off + sizeof(struct icmp6_hdr)) {
412 m = m_pullup(m, off + sizeof(struct icmp6_hdr));
414 IP6STAT_INC(ip6s_exthdrtoolong);
416 return (IPPROTO_DONE);
421 * Locate icmp6 structure in mbuf, and check
422 * that not corrupted and of at least minimum length
425 icmp6len = m->m_pkthdr.len - off;
426 if (icmp6len < sizeof(struct icmp6_hdr)) {
427 ICMP6STAT_INC(icp6s_tooshort);
431 ip6 = mtod(m, struct ip6_hdr *);
432 ifp = m->m_pkthdr.rcvif;
434 * Check multicast group membership.
435 * Note: SSM filters are not applied for ICMPv6 traffic.
437 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
438 struct in6_multi *inm;
440 inm = in6m_lookup(ifp, &ip6->ip6_dst);
442 IP6STAT_INC(ip6s_notmember);
443 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
448 /* Calculate the checksum. */
449 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
450 code = icmp6->icmp6_code;
451 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
453 "ICMP6 checksum error(%d|%x) %s\n",
454 icmp6->icmp6_type, sum,
455 ip6_sprintf(ip6bufs, &ip6->ip6_src)));
456 ICMP6STAT_INC(icp6s_checksum);
460 ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]);
461 icmp6_ifstat_inc(ifp, ifs6_in_msg);
462 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
463 icmp6_ifstat_inc(ifp, ifs6_in_error);
465 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
466 switch (icmp6->icmp6_type) {
467 case ICMP6_DST_UNREACH:
468 icmp6_ifstat_inc(ifp, ifs6_in_dstunreach);
470 case ICMP6_DST_UNREACH_NOROUTE:
471 case ICMP6_DST_UNREACH_ADDR: /* PRC_HOSTDEAD is a DOS */
472 code = PRC_UNREACH_NET;
474 case ICMP6_DST_UNREACH_ADMIN:
475 icmp6_ifstat_inc(ifp, ifs6_in_adminprohib);
476 code = PRC_UNREACH_ADMIN_PROHIB;
478 case ICMP6_DST_UNREACH_BEYONDSCOPE:
479 /* I mean "source address was incorrect." */
480 code = PRC_PARAMPROB;
482 case ICMP6_DST_UNREACH_NOPORT:
483 code = PRC_UNREACH_PORT;
491 case ICMP6_PACKET_TOO_BIG:
492 icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig);
494 /* validation is made in icmp6_mtudisc_update */
499 * Updating the path MTU will be done after examining
500 * intermediate extension headers.
505 case ICMP6_TIME_EXCEEDED:
506 icmp6_ifstat_inc(ifp, ifs6_in_timeexceed);
508 case ICMP6_TIME_EXCEED_TRANSIT:
509 code = PRC_TIMXCEED_INTRANS;
511 case ICMP6_TIME_EXCEED_REASSEMBLY:
512 code = PRC_TIMXCEED_REASS;
520 case ICMP6_PARAM_PROB:
521 icmp6_ifstat_inc(ifp, ifs6_in_paramprob);
523 case ICMP6_PARAMPROB_NEXTHEADER:
524 code = PRC_UNREACH_PROTOCOL;
526 case ICMP6_PARAMPROB_HEADER:
527 case ICMP6_PARAMPROB_OPTION:
528 code = PRC_PARAMPROB;
536 case ICMP6_ECHO_REQUEST:
537 icmp6_ifstat_inc(ifp, ifs6_in_echo);
540 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
545 || n->m_len < off + sizeof(struct icmp6_hdr)) {
549 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) <= MHLEN);
550 n = m_gethdr(M_NOWAIT, n0->m_type);
557 m_move_pkthdr(n, n0); /* FIB copied. */
558 n0len = n0->m_pkthdr.len; /* save for use below */
560 * Copy IPv6 and ICMPv6 only.
562 nip6 = mtod(n, struct ip6_hdr *);
563 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
564 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
565 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
566 noff = sizeof(struct ip6_hdr);
567 /* new mbuf contains only ipv6+icmpv6 headers */
568 n->m_len = noff + sizeof(struct icmp6_hdr);
570 * Adjust mbuf. ip6_plen will be adjusted in
573 m_adj(n0, off + sizeof(struct icmp6_hdr));
574 /* recalculate complete packet size */
575 n->m_pkthdr.len = n0len + (noff - off);
578 if (n->m_len < off + sizeof(*nicmp6)) {
579 n = m_pullup(n, off + sizeof(*nicmp6));
581 IP6STAT_INC(ip6s_exthdrtoolong);
585 nicmp6 = (struct icmp6_hdr *)(mtod(n, caddr_t) + off);
589 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
590 nicmp6->icmp6_code = 0;
591 ICMP6STAT_INC(icp6s_reflect);
592 ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]);
593 icmp6_reflect(n, noff);
597 case ICMP6_ECHO_REPLY:
598 icmp6_ifstat_inc(ifp, ifs6_in_echoreply);
603 case MLD_LISTENER_QUERY:
604 case MLD_LISTENER_REPORT:
605 case MLD_LISTENER_DONE:
606 case MLDV2_LISTENER_REPORT:
608 * Drop MLD traffic which is not link-local, has a hop limit
609 * of greater than 1 hop, or which does not have the
610 * IPv6 HBH Router Alert option.
611 * As IPv6 HBH options are stripped in ip6_input() we must
612 * check an mbuf header flag.
613 * XXX Should we also sanity check that these messages
614 * were directed to a link-local multicast prefix?
616 if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0)
618 if (mld_input(&m, off, icmp6len) != 0) {
620 return (IPPROTO_DONE);
625 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
627 enum { WRU, FQDN } mode;
630 if (!V_icmp6_nodeinfo)
633 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
635 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
641 sx_slock(&allprison_lock);
642 TAILQ_FOREACH(pr, &allprison, pr_list)
643 if (pr->pr_vnet == ifp->if_vnet)
645 sx_sunlock(&allprison_lock);
647 pr = curthread->td_ucred->cr_prison;
649 if (m->m_len < off + sizeof(struct icmp6_nodeinfo)) {
650 m = m_pullup(m, off +
651 sizeof(struct icmp6_nodeinfo));
653 IP6STAT_INC(ip6s_exthdrtoolong);
655 return (IPPROTO_DONE);
658 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
660 n = ni6_input(n, off, pr);
661 /* XXX meaningless if n == NULL */
662 noff = sizeof(struct ip6_hdr);
668 * XXX: this combination of flags is pointless,
669 * but should we keep this for compatibility?
671 if ((V_icmp6_nodeinfo & (ICMP6_NODEINFO_FQDNOK |
672 ICMP6_NODEINFO_TMPADDROK)) !=
673 (ICMP6_NODEINFO_FQDNOK | ICMP6_NODEINFO_TMPADDROK))
679 CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) + 4 <= MHLEN);
680 n = m_gethdr(M_NOWAIT, m->m_type);
685 if (!m_dup_pkthdr(n, m, M_NOWAIT)) {
687 * Previous code did a blind M_COPY_PKTHDR
688 * and said "just for rcvif". If true, then
689 * we could tolerate the dup failing (due to
690 * the deep copy of the tag chain). For now
691 * be conservative and just fail.
698 * Copy IPv6 and ICMPv6 only.
700 nip6 = mtod(n, struct ip6_hdr *);
701 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
702 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
703 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
704 p = (u_char *)(nicmp6 + 1);
707 maxhlen = M_TRAILINGSPACE(n) -
708 (sizeof(*nip6) + sizeof(*nicmp6) + 4);
709 mtx_lock(&pr->pr_mtx);
710 hlen = strlen(pr->pr_hostname);
713 /* meaningless TTL */
714 bcopy(pr->pr_hostname, p + 4, maxhlen);
715 mtx_unlock(&pr->pr_mtx);
716 noff = sizeof(struct ip6_hdr);
717 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
718 sizeof(struct icmp6_hdr) + 4 + maxhlen;
719 nicmp6->icmp6_type = ICMP6_WRUREPLY;
720 nicmp6->icmp6_code = 0;
723 ICMP6STAT_INC(icp6s_reflect);
724 ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]);
725 icmp6_reflect(n, noff);
735 case ND_ROUTER_SOLICIT:
736 icmp6_ifstat_inc(ifp, ifs6_in_routersolicit);
739 if (icmp6len < sizeof(struct nd_router_solicit))
741 if (send_sendso_input_hook != NULL) {
742 if (m->m_len < off + icmp6len) {
743 m = m_pullup(m, off + icmp6len);
745 IP6STAT_INC(ip6s_exthdrtoolong);
747 return (IPPROTO_DONE);
750 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
756 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
757 nd6_rs_input(m, off, icmp6len);
763 case ND_ROUTER_ADVERT:
764 icmp6_ifstat_inc(ifp, ifs6_in_routeradvert);
767 if (icmp6len < sizeof(struct nd_router_advert))
769 if (send_sendso_input_hook != NULL) {
770 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
776 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
777 nd6_ra_input(m, off, icmp6len);
783 case ND_NEIGHBOR_SOLICIT:
784 icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit);
787 if (icmp6len < sizeof(struct nd_neighbor_solicit))
789 if (send_sendso_input_hook != NULL) {
790 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
796 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
797 nd6_ns_input(m, off, icmp6len);
803 case ND_NEIGHBOR_ADVERT:
804 icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert);
807 if (icmp6len < sizeof(struct nd_neighbor_advert))
809 if (send_sendso_input_hook != NULL) {
810 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
816 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
817 nd6_na_input(m, off, icmp6len);
824 icmp6_ifstat_inc(ifp, ifs6_in_redirect);
827 if (icmp6len < sizeof(struct nd_redirect))
829 if (send_sendso_input_hook != NULL) {
830 error = send_sendso_input_hook(m, ifp, SND_IN, ip6len);
836 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
837 icmp6_redirect_input(m, off);
843 case ICMP6_ROUTER_RENUMBERING:
844 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
845 code != ICMP6_ROUTER_RENUMBERING_RESULT)
847 if (icmp6len < sizeof(struct icmp6_router_renum))
853 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
854 icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
855 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
856 ifp ? ifp->if_index : 0));
857 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
858 /* ICMPv6 error: MUST deliver it by spec... */
862 /* ICMPv6 informational: MUST not deliver */
866 if (icmp6_notify_error(&m, off, icmp6len, code) != 0) {
867 /* In this case, m should've been freed. */
869 return (IPPROTO_DONE);
874 ICMP6STAT_INC(icp6s_badcode);
878 ICMP6STAT_INC(icp6s_badlen);
882 /* deliver the packet to appropriate sockets */
883 icmp6_rip6_input(&m, *offp);
886 return (IPPROTO_DONE);
891 return (IPPROTO_DONE);
895 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code)
898 struct icmp6_hdr *icmp6;
899 struct ip6_hdr *eip6;
901 struct sockaddr_in6 icmp6src, icmp6dst;
905 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
906 ICMP6STAT_INC(icp6s_tooshort);
910 if (m->m_len < off + sizeof(*icmp6) + sizeof(struct ip6_hdr)) {
911 m = m_pullup(m, off + sizeof(*icmp6) + sizeof(struct ip6_hdr));
913 IP6STAT_INC(ip6s_exthdrtoolong);
918 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
919 eip6 = (struct ip6_hdr *)(icmp6 + 1);
921 /* Detect the upper level protocol */
923 void (*ctlfunc)(int, struct sockaddr *, void *);
924 u_int8_t nxt = eip6->ip6_nxt;
925 int eoff = off + sizeof(struct icmp6_hdr) +
926 sizeof(struct ip6_hdr);
927 struct ip6ctlparam ip6cp;
928 struct in6_addr *finaldst = NULL;
929 int icmp6type = icmp6->icmp6_type;
931 struct ip6_rthdr *rth;
932 struct ip6_rthdr0 *rth0;
935 while (1) { /* XXX: should avoid infinite loop explicitly? */
939 case IPPROTO_HOPOPTS:
940 case IPPROTO_DSTOPTS:
942 if (m->m_len < eoff + sizeof(struct ip6_ext)) {
943 m = m_pullup(m, eoff +
944 sizeof(struct ip6_ext));
946 IP6STAT_INC(ip6s_exthdrtoolong);
951 eh = (struct ip6_ext *)
952 (mtod(m, caddr_t) + eoff);
953 if (nxt == IPPROTO_AH)
954 eoff += (eh->ip6e_len + 2) << 2;
956 eoff += (eh->ip6e_len + 1) << 3;
959 case IPPROTO_ROUTING:
961 * When the erroneous packet contains a
962 * routing header, we should examine the
963 * header to determine the final destination.
964 * Otherwise, we can't properly update
965 * information that depends on the final
966 * destination (e.g. path MTU).
968 if (m->m_len < eoff + sizeof(*rth)) {
969 m = m_pullup(m, eoff + sizeof(*rth));
971 IP6STAT_INC(ip6s_exthdrtoolong);
976 rth = (struct ip6_rthdr *)
977 (mtod(m, caddr_t) + eoff);
978 rthlen = (rth->ip6r_len + 1) << 3;
980 * XXX: currently there is no
981 * officially defined type other
983 * Note that if the segment left field
984 * is 0, all intermediate hops must
987 if (rth->ip6r_segleft &&
988 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
991 if (m->m_len < eoff + rthlen) {
992 m = m_pullup(m, eoff + rthlen);
1000 rth0 = (struct ip6_rthdr0 *)
1001 (mtod(m, caddr_t) + eoff);
1002 /* just ignore a bogus header */
1003 if ((rth0->ip6r0_len % 2) == 0 &&
1004 (hops = rth0->ip6r0_len/2))
1005 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
1008 nxt = rth->ip6r_nxt;
1010 case IPPROTO_FRAGMENT:
1011 if (m->m_len < eoff + sizeof(struct ip6_frag)) {
1012 m = m_pullup(m, eoff +
1013 sizeof(struct ip6_frag));
1015 IP6STAT_INC(ip6s_exthdrtoolong);
1020 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1023 * Data after a fragment header is meaningless
1024 * unless it is the first fragment, but
1025 * we'll go to the notify label for path MTU
1028 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1031 eoff += sizeof(struct ip6_frag);
1036 * This case includes ESP and the No Next
1037 * Header. In such cases going to the notify
1038 * label does not have any meaning
1039 * (i.e. ctlfunc will be NULL), but we go
1040 * anyway since we might have to update
1041 * path MTU information.
1047 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1050 * retrieve parameters from the inner IPv6 header, and convert
1051 * them into sockaddr structures.
1052 * XXX: there is no guarantee that the source or destination
1053 * addresses of the inner packet are in the same scope as
1054 * the addresses of the icmp packet. But there is no other
1055 * way to determine the zone.
1057 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1059 bzero(&icmp6dst, sizeof(icmp6dst));
1060 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1061 icmp6dst.sin6_family = AF_INET6;
1062 if (finaldst == NULL)
1063 icmp6dst.sin6_addr = eip6->ip6_dst;
1065 icmp6dst.sin6_addr = *finaldst;
1066 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1068 bzero(&icmp6src, sizeof(icmp6src));
1069 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1070 icmp6src.sin6_family = AF_INET6;
1071 icmp6src.sin6_addr = eip6->ip6_src;
1072 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1074 icmp6src.sin6_flowinfo =
1075 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1077 if (finaldst == NULL)
1078 finaldst = &eip6->ip6_dst;
1080 ip6cp.ip6c_icmp6 = icmp6;
1081 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1082 ip6cp.ip6c_off = eoff;
1083 ip6cp.ip6c_finaldst = finaldst;
1084 ip6cp.ip6c_src = &icmp6src;
1085 ip6cp.ip6c_nxt = nxt;
1087 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1088 notifymtu = ntohl(icmp6->icmp6_mtu);
1089 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1090 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1093 ctlfunc = (void (*)(int, struct sockaddr *, void *))
1094 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1096 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1110 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1112 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1113 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1114 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1115 u_int mtu = ntohl(icmp6->icmp6_mtu);
1116 struct in_conninfo inc;
1120 * RFC2460 section 5, last paragraph.
1121 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1122 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1123 * due to packet translator in the middle.
1124 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1127 if (mtu < IPV6_MMTU)
1132 * we reject ICMPv6 too big with abnormally small value.
1133 * XXX what is the good definition of "abnormally small"?
1135 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1142 * In case the suggested mtu is less than IPV6_MMTU, we
1143 * only need to remember that it was for above mentioned
1144 * "alwaysfrag" case.
1145 * Try to be as close to the spec as possible.
1147 if (mtu < IPV6_MMTU)
1148 mtu = IPV6_MMTU - 8;
1150 bzero(&inc, sizeof(inc));
1151 inc.inc_fibnum = M_GETFIB(m);
1152 inc.inc_flags |= INC_ISIPV6;
1153 inc.inc6_faddr = *dst;
1154 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1157 if (mtu < tcp_maxmtu6(&inc, NULL)) {
1158 tcp_hc_updatemtu(&inc, mtu);
1159 ICMP6STAT_INC(icp6s_pmtuchg);
1164 * Process a Node Information Query packet, based on
1165 * draft-ietf-ipngwg-icmp-name-lookups-07.
1167 * Spec incompatibilities:
1168 * - IPv6 Subject address handling
1169 * - IPv4 Subject address handling support missing
1170 * - Proxy reply (answer even if it's not for me)
1171 * - joins NI group address at in6_ifattach() time only, does not cope
1172 * with hostname changes by sethostname(3)
1174 static struct mbuf *
1175 ni6_input(struct mbuf *m, int off, struct prison *pr)
1177 struct icmp6_nodeinfo *ni6, *nni6;
1178 struct mbuf *n = NULL;
1181 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1182 struct ni_reply_fqdn *fqdn;
1183 int addrs; /* for NI_QTYPE_NODEADDR */
1184 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1185 struct in6_addr in6_subj; /* subject address */
1186 struct ip6_hdr *ip6;
1187 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1189 struct in6_ifaddr *ia6 = NULL;
1191 ip6 = mtod(m, struct ip6_hdr *);
1192 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1195 * Validate IPv6 source address.
1196 * The default configuration MUST be to refuse answering queries from
1197 * global-scope addresses according to RFC4602.
1199 * - it's not very clear what "refuse" means; this implementation
1201 * - it's not very easy to identify global-scope (unicast) addresses
1202 * since there are many prefixes for them. It should be safer
1203 * and in practice sufficient to check "all" but loopback and
1204 * link-local (note that site-local unicast was deprecated and
1205 * ULA is defined as global scope-wise)
1207 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1208 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1209 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1213 * Validate IPv6 destination address.
1215 * The Responder must discard the Query without further processing
1216 * unless it is one of the Responder's unicast or anycast addresses, or
1217 * a link-local scope multicast address which the Responder has joined.
1218 * [RFC4602, Section 5.]
1220 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1221 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1223 /* else it's a link-local multicast, fine */
1224 } else { /* unicast or anycast */
1225 ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
1227 goto bad; /* XXX impossible */
1229 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1230 !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1231 ifa_free(&ia6->ia_ifa);
1232 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1233 "a temporary address in %s:%d",
1234 __FILE__, __LINE__));
1237 ifa_free(&ia6->ia_ifa);
1240 /* validate query Subject field. */
1241 qtype = ntohs(ni6->ni_qtype);
1242 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1245 case NI_QTYPE_SUPTYPES:
1247 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1251 case NI_QTYPE_NODEADDR:
1252 case NI_QTYPE_IPV4ADDR:
1253 switch (ni6->ni_code) {
1254 case ICMP6_NI_SUBJ_IPV6:
1255 #if ICMP6_NI_SUBJ_IPV6 != 0
1259 * backward compatibility - try to accept 03 draft
1260 * format, where no Subject is present.
1262 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1267 #if ICMP6_NI_SUBJ_IPV6 != 0
1268 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1272 if (subjlen != sizeof(struct in6_addr))
1276 * Validate Subject address.
1278 * Not sure what exactly "address belongs to the node"
1279 * means in the spec, is it just unicast, or what?
1281 * At this moment we consider Subject address as
1282 * "belong to the node" if the Subject address equals
1283 * to the IPv6 destination address; validation for
1284 * IPv6 destination address should have done enough
1287 * We do not do proxy at this moment.
1289 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1290 subjlen, (caddr_t)&in6_subj);
1291 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1294 subj = (char *)&in6_subj;
1295 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1299 * XXX if we are to allow other cases, we should really
1300 * be careful about scope here.
1301 * basically, we should disallow queries toward IPv6
1302 * destination X with subject Y,
1303 * if scope(X) > scope(Y).
1304 * if we allow scope(X) > scope(Y), it will result in
1305 * information leakage across scope boundary.
1309 case ICMP6_NI_SUBJ_FQDN:
1311 * Validate Subject name with gethostname(3).
1313 * The behavior may need some debate, since:
1314 * - we are not sure if the node has FQDN as
1315 * hostname (returned by gethostname(3)).
1316 * - the code does wildcard match for truncated names.
1317 * however, we are not sure if we want to perform
1318 * wildcard match, if gethostname(3) side has
1319 * truncated hostname.
1321 mtx_lock(&pr->pr_mtx);
1322 n = ni6_nametodns(pr->pr_hostname,
1323 strlen(pr->pr_hostname), 0);
1324 mtx_unlock(&pr->pr_mtx);
1325 if (!n || n->m_next || n->m_len == 0)
1327 if (m->m_len < off + sizeof(struct icmp6_nodeinfo) +
1329 m = m_pullup(m, off +
1330 sizeof(struct icmp6_nodeinfo) + subjlen);
1332 IP6STAT_INC(ip6s_exthdrtoolong);
1336 /* ip6 possibly invalid but not used after. */
1337 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1338 subj = (char *)(mtod(m, caddr_t) + off +
1339 sizeof(struct icmp6_nodeinfo));
1340 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1348 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1355 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1358 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1361 case NI_QTYPE_NODEADDR:
1362 case NI_QTYPE_IPV4ADDR:
1363 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1368 /* guess reply length */
1371 break; /* no reply data */
1372 case NI_QTYPE_SUPTYPES:
1373 replylen += sizeof(u_int32_t);
1376 /* XXX will append an mbuf */
1377 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1379 case NI_QTYPE_NODEADDR:
1380 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1381 if ((replylen += addrs * (sizeof(struct in6_addr) +
1382 sizeof(u_int32_t))) > MCLBYTES)
1383 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1385 case NI_QTYPE_IPV4ADDR:
1386 /* unsupported - should respond with unknown Qtype? */
1390 * XXX: We must return a reply with the ICMP6 code
1391 * `unknown Qtype' in this case. However we regard the case
1392 * as an FQDN query for backward compatibility.
1393 * Older versions set a random value to this field,
1394 * so it rarely varies in the defined qtypes.
1395 * But the mechanism is not reliable...
1396 * maybe we should obsolete older versions.
1398 qtype = NI_QTYPE_FQDN;
1399 /* XXX will append an mbuf */
1400 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1405 /* Allocate an mbuf to reply. */
1406 if (replylen > MCLBYTES) {
1408 * XXX: should we try to allocate more? But MCLBYTES
1409 * is probably much larger than IPV6_MMTU...
1413 if (replylen > MHLEN)
1414 n = m_getcl(M_NOWAIT, m->m_type, M_PKTHDR);
1416 n = m_gethdr(M_NOWAIT, m->m_type);
1421 m_move_pkthdr(n, m); /* just for recvif and FIB */
1422 n->m_pkthdr.len = n->m_len = replylen;
1424 /* copy mbuf header and IPv6 + Node Information base headers */
1425 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1426 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1427 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1429 /* qtype dependent procedure */
1432 nni6->ni_code = ICMP6_NI_SUCCESS;
1435 case NI_QTYPE_SUPTYPES:
1438 nni6->ni_code = ICMP6_NI_SUCCESS;
1439 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1440 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1441 v = (u_int32_t)htonl(0x0000000f);
1442 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1446 nni6->ni_code = ICMP6_NI_SUCCESS;
1447 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1448 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1449 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1450 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1452 * XXX do we really have FQDN in hostname?
1454 mtx_lock(&pr->pr_mtx);
1455 n->m_next = ni6_nametodns(pr->pr_hostname,
1456 strlen(pr->pr_hostname), oldfqdn);
1457 mtx_unlock(&pr->pr_mtx);
1458 if (n->m_next == NULL)
1460 /* XXX we assume that n->m_next is not a chain */
1461 if (n->m_next->m_next != NULL)
1463 n->m_pkthdr.len += n->m_next->m_len;
1465 case NI_QTYPE_NODEADDR:
1469 nni6->ni_code = ICMP6_NI_SUCCESS;
1470 n->m_pkthdr.len = n->m_len =
1471 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1472 lenlim = M_TRAILINGSPACE(n);
1473 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1474 /* XXX: reset mbuf length */
1475 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1476 sizeof(struct icmp6_nodeinfo) + copied;
1480 break; /* XXX impossible! */
1483 nni6->ni_type = ICMP6_NI_REPLY;
1495 * make a mbuf with DNS-encoded string. no compression support.
1497 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1498 * treated as truncated name (two \0 at the end). this is a wild guess.
1500 * old - return pascal string if non-zero
1502 static struct mbuf *
1503 ni6_nametodns(const char *name, int namelen, int old)
1515 /* Because MAXHOSTNAMELEN is usually 256, we use cluster mbuf. */
1517 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1519 m = m_get(M_NOWAIT, MT_DATA);
1525 *mtod(m, char *) = namelen;
1526 bcopy(name, mtod(m, char *) + 1, namelen);
1530 cp = mtod(m, char *);
1531 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1533 /* if not certain about my name, return empty buffer */
1538 * guess if it looks like shortened hostname, or FQDN.
1539 * shortened hostname needs two trailing "\0".
1542 for (p = name; p < name + namelen; p++) {
1543 if (*p && *p == '.')
1552 while (cp < ep && p < name + namelen) {
1554 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1556 /* result does not fit into mbuf */
1557 if (cp + i + 1 >= ep)
1560 * DNS label length restriction, RFC1035 page 8.
1561 * "i == 0" case is included here to avoid returning
1562 * 0-length label on "foo..bar".
1564 if (i <= 0 || i >= 64)
1570 if (p < name + namelen && *p == '.')
1574 if (cp + nterm >= ep)
1578 m->m_len = cp - mtod(m, char *);
1582 panic("should not reach here");
1592 * check if two DNS-encoded string matches. takes care of truncated
1593 * form (with \0\0 at the end). no compression support.
1594 * XXX upper/lowercase match (see RFC2065)
1597 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1599 const char *a0, *b0;
1602 /* simplest case - need validation? */
1603 if (alen == blen && bcmp(a, b, alen) == 0)
1609 /* termination is mandatory */
1610 if (alen < 2 || blen < 2)
1612 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1617 while (a - a0 < alen && b - b0 < blen) {
1618 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1621 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1623 /* we don't support compression yet */
1624 if (a[0] >= 64 || b[0] >= 64)
1627 /* truncated case */
1628 if (a[0] == 0 && a - a0 == alen - 1)
1630 if (b[0] == 0 && b - b0 == blen - 1)
1632 if (a[0] == 0 || b[0] == 0)
1638 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1640 if (bcmp(a + 1, b + 1, l) != 0)
1647 if (a - a0 == alen && b - b0 == blen)
1654 * calculate the number of addresses to be returned in the node info reply.
1657 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1658 struct in6_addr *subj)
1661 struct in6_ifaddr *ifa6;
1663 int addrs = 0, addrsofif, iffound = 0;
1664 int niflags = ni6->ni_flags;
1668 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1669 switch (ni6->ni_code) {
1670 case ICMP6_NI_SUBJ_IPV6:
1671 if (subj == NULL) /* must be impossible... */
1676 * XXX: we only support IPv6 subject address for
1683 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1685 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1686 if (ifa->ifa_addr->sa_family != AF_INET6)
1688 ifa6 = (struct in6_ifaddr *)ifa;
1690 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1691 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1695 * IPv4-mapped addresses can only be returned by a
1696 * Node Information proxy, since they represent
1697 * addresses of IPv4-only nodes, which perforce do
1698 * not implement this protocol.
1699 * [icmp-name-lookups-07, Section 5.4]
1700 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1701 * this function at this moment.
1704 /* What do we have to do about ::1? */
1705 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1706 case IPV6_ADDR_SCOPE_LINKLOCAL:
1707 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1710 case IPV6_ADDR_SCOPE_SITELOCAL:
1711 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1714 case IPV6_ADDR_SCOPE_GLOBAL:
1715 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1723 * check if anycast is okay.
1724 * XXX: just experimental. not in the spec.
1726 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1727 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1728 continue; /* we need only unicast addresses */
1729 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1730 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1733 addrsofif++; /* count the address */
1747 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1748 struct ifnet *ifp0, int resid)
1751 struct in6_ifaddr *ifa6;
1753 struct ifnet *ifp_dep = NULL;
1754 int copied = 0, allow_deprecated = 0;
1755 u_char *cp = (u_char *)(nni6 + 1);
1756 int niflags = ni6->ni_flags;
1761 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1762 return (0); /* needless to copy */
1764 ifp = ifp0 ? ifp0 : CK_STAILQ_FIRST(&V_ifnet);
1767 for (; ifp; ifp = CK_STAILQ_NEXT(ifp, if_link)) {
1768 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1769 if (ifa->ifa_addr->sa_family != AF_INET6)
1771 ifa6 = (struct in6_ifaddr *)ifa;
1773 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1774 allow_deprecated == 0) {
1776 * prefererred address should be put before
1777 * deprecated addresses.
1780 /* record the interface for later search */
1781 if (ifp_dep == NULL)
1785 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1786 allow_deprecated != 0)
1787 continue; /* we now collect deprecated addrs */
1789 /* What do we have to do about ::1? */
1790 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1791 case IPV6_ADDR_SCOPE_LINKLOCAL:
1792 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1795 case IPV6_ADDR_SCOPE_SITELOCAL:
1796 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1799 case IPV6_ADDR_SCOPE_GLOBAL:
1800 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1808 * check if anycast is okay.
1809 * XXX: just experimental. not in the spec.
1811 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1812 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1814 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1815 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1819 /* now we can copy the address */
1820 if (resid < sizeof(struct in6_addr) +
1821 sizeof(u_int32_t)) {
1823 * We give up much more copy.
1824 * Set the truncate flag and return.
1826 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1831 * Set the TTL of the address.
1832 * The TTL value should be one of the following
1833 * according to the specification:
1835 * 1. The remaining lifetime of a DHCP lease on the
1837 * 2. The remaining Valid Lifetime of a prefix from
1838 * which the address was derived through Stateless
1839 * Autoconfiguration.
1841 * Note that we currently do not support stateful
1842 * address configuration by DHCPv6, so the former
1843 * case can't happen.
1845 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1846 ltime = ND6_INFINITE_LIFETIME;
1848 if (ifa6->ia6_lifetime.ia6t_expire >
1850 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_uptime);
1855 bcopy(<ime, cp, sizeof(u_int32_t));
1856 cp += sizeof(u_int32_t);
1858 /* copy the address itself */
1859 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1860 sizeof(struct in6_addr));
1861 in6_clearscope((struct in6_addr *)cp); /* XXX */
1862 cp += sizeof(struct in6_addr);
1864 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1865 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1867 if (ifp0) /* we need search only on the specified IF */
1871 if (allow_deprecated == 0 && ifp_dep != NULL) {
1873 allow_deprecated = 1;
1882 * XXX almost dup'ed code with rip6_input.
1885 icmp6_rip6_input(struct mbuf **mp, int off)
1887 struct mbuf *m = *mp;
1888 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1890 struct inpcb *last = NULL;
1891 struct sockaddr_in6 fromsa;
1892 struct icmp6_hdr *icmp6;
1893 struct mbuf *opts = NULL;
1897 /* This is assumed to be safe; icmp6_input() does a pullup. */
1898 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1901 * XXX: the address may have embedded scope zone ID, which should be
1902 * hidden from applications.
1904 bzero(&fromsa, sizeof(fromsa));
1905 fromsa.sin6_family = AF_INET6;
1906 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1907 fromsa.sin6_addr = ip6->ip6_src;
1908 if (sa6_recoverscope(&fromsa)) {
1911 return (IPPROTO_DONE);
1914 CK_LIST_FOREACH(inp, &V_ripcb, inp_list) {
1915 if ((inp->inp_vflag & INP_IPV6) == 0)
1917 if (inp->inp_ip_p != IPPROTO_ICMPV6)
1919 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1920 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst))
1922 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1923 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src))
1926 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1930 if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1931 inp->in6p_icmp6filt)) {
1936 struct mbuf *n = NULL;
1939 * Recent network drivers tend to allocate a single
1940 * mbuf cluster, rather than to make a couple of
1941 * mbufs without clusters. Also, since the IPv6 code
1942 * path tries to avoid m_pullup(), it is highly
1943 * probable that we still have an mbuf cluster here
1944 * even though the necessary length can be stored in an
1945 * mbuf's internal buffer.
1946 * Meanwhile, the default size of the receive socket
1947 * buffer for raw sockets is not so large. This means
1948 * the possibility of packet loss is relatively higher
1949 * than before. To avoid this scenario, we copy the
1950 * received data to a separate mbuf that does not use
1951 * a cluster, if possible.
1952 * XXX: it is better to copy the data after stripping
1953 * intermediate headers.
1955 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1956 m->m_len <= MHLEN) {
1957 n = m_get(M_NOWAIT, m->m_type);
1959 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1960 bcopy(m->m_data, n->m_data,
1962 n->m_len = m->m_len;
1970 (n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
1971 if (last->inp_flags & INP_CONTROLOPTS)
1972 ip6_savecontrol(last, n, &opts);
1973 /* strip intermediate headers */
1975 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
1976 if (sbappendaddr_locked(
1977 &last->inp_socket->so_rcv,
1978 (struct sockaddr *)&fromsa, n, opts)
1980 /* should notify about lost packet */
1986 &last->inp_socket->so_rcv);
1988 sorwakeup_locked(last->inp_socket);
1996 if (last->inp_flags & INP_CONTROLOPTS)
1997 ip6_savecontrol(last, m, &opts);
1998 /* strip intermediate headers */
2001 /* avoid using mbuf clusters if possible (see above) */
2002 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2003 m->m_len <= MHLEN) {
2006 n = m_get(M_NOWAIT, m->m_type);
2008 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2009 bcopy(m->m_data, n->m_data, m->m_len);
2010 n->m_len = m->m_len;
2020 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2021 if (sbappendaddr_locked(&last->inp_socket->so_rcv,
2022 (struct sockaddr *)&fromsa, m, opts) == 0) {
2026 SOCKBUF_UNLOCK(&last->inp_socket->so_rcv);
2028 sorwakeup_locked(last->inp_socket);
2032 IP6STAT_DEC(ip6s_delivered);
2035 return (IPPROTO_DONE);
2039 * Reflect the ip6 packet back to the source.
2040 * OFF points to the icmp6 header, counted from the top of the mbuf.
2043 icmp6_reflect(struct mbuf *m, size_t off)
2045 struct in6_addr src6, *srcp;
2046 struct ip6_hdr *ip6;
2047 struct icmp6_hdr *icmp6;
2048 struct in6_ifaddr *ia = NULL;
2049 struct ifnet *outif = NULL;
2051 int type, code, hlim;
2053 /* too short to reflect */
2054 if (off < sizeof(struct ip6_hdr)) {
2056 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2057 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2058 __FILE__, __LINE__));
2063 * If there are extra headers between IPv6 and ICMPv6, strip
2064 * off that header first.
2067 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2068 panic("assumption failed in icmp6_reflect");
2070 if (off > sizeof(struct ip6_hdr)) {
2072 struct ip6_hdr nip6;
2074 l = off - sizeof(struct ip6_hdr);
2075 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2077 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2079 if ((m = m_pullup(m, l)) == NULL)
2082 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2083 } else /* off == sizeof(struct ip6_hdr) */ {
2085 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2087 if ((m = m_pullup(m, l)) == NULL)
2091 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2092 ip6 = mtod(m, struct ip6_hdr *);
2093 ip6->ip6_nxt = IPPROTO_ICMPV6;
2094 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2095 type = icmp6->icmp6_type; /* keep type for statistics */
2096 code = icmp6->icmp6_code; /* ditto. */
2101 * If the incoming packet was addressed directly to us (i.e. unicast),
2102 * use dst as the src for the reply.
2103 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2104 * (for example) when we encounter an error while forwarding procedure
2105 * destined to a duplicated address of ours.
2107 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
2108 ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
2109 if (ia != NULL && !(ia->ia6_flags &
2110 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2111 src6 = ia->ia_addr.sin6_addr;
2114 if (m->m_pkthdr.rcvif != NULL) {
2115 /* XXX: This may not be the outgoing interface */
2116 hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2118 hlim = V_ip6_defhlim;
2121 ifa_free(&ia->ia_ifa);
2126 struct in6_addr dst6;
2130 * This case matches to multicasts, our anycast, or unicasts
2131 * that we do not own. Select a source address based on the
2132 * source address of the erroneous packet.
2134 in6_splitscope(&ip6->ip6_src, &dst6, &scopeid);
2135 error = in6_selectsrc_addr(M_GETFIB(m), &dst6,
2136 scopeid, NULL, &src6, &hlim);
2139 char ip6buf[INET6_ADDRSTRLEN];
2141 "icmp6_reflect: source can't be determined: "
2142 "dst=%s, error=%d\n",
2143 ip6_sprintf(ip6buf, &ip6->ip6_dst), error));
2149 * ip6_input() drops a packet if its src is multicast.
2150 * So, the src is never multicast.
2152 ip6->ip6_dst = ip6->ip6_src;
2153 ip6->ip6_src = *srcp;
2155 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2156 ip6->ip6_vfc |= IPV6_VERSION;
2157 ip6->ip6_nxt = IPPROTO_ICMPV6;
2158 ip6->ip6_hlim = hlim;
2160 icmp6->icmp6_cksum = 0;
2161 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2162 sizeof(struct ip6_hdr), plen);
2165 * XXX option handling
2168 m->m_flags &= ~(M_BCAST|M_MCAST);
2169 m->m_pkthdr.rcvif = NULL;
2170 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2172 icmp6_ifoutstat_inc(outif, type, code);
2182 icmp6_fasttimo(void)
2189 icmp6_slowtimo(void)
2196 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2197 struct in6_addr *tgt6)
2199 static char buf[1024];
2200 char ip6bufs[INET6_ADDRSTRLEN];
2201 char ip6bufd[INET6_ADDRSTRLEN];
2202 char ip6buft[INET6_ADDRSTRLEN];
2203 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2204 ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2205 ip6_sprintf(ip6buft, tgt6));
2210 icmp6_redirect_input(struct mbuf *m, int off)
2213 struct ip6_hdr *ip6;
2214 struct nd_redirect *nd_rd;
2215 struct in6_addr src6, redtgt6, reddst6;
2216 union nd_opts ndopts;
2217 char ip6buf[INET6_ADDRSTRLEN];
2219 int icmp6len, is_onlink, is_router, lladdrlen;
2222 KASSERT(m->m_pkthdr.rcvif != NULL, ("%s: no rcvif", __func__));
2224 /* XXX if we are router, we don't update route by icmp6 redirect */
2225 if (V_ip6_forwarding)
2227 if (!V_icmp6_rediraccept)
2230 /* RFC 6980: Nodes MUST silently ignore fragments */
2231 if(m->m_flags & M_FRAGMENTED)
2234 ip6 = mtod(m, struct ip6_hdr *);
2235 icmp6len = ntohs(ip6->ip6_plen);
2236 if (m->m_len < off + icmp6len) {
2237 m = m_pullup(m, off + icmp6len);
2239 IP6STAT_INC(ip6s_exthdrtoolong);
2243 ip6 = mtod(m, struct ip6_hdr *);
2244 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2246 ifp = m->m_pkthdr.rcvif;
2247 redtgt6 = nd_rd->nd_rd_target;
2248 reddst6 = nd_rd->nd_rd_dst;
2250 if (in6_setscope(&redtgt6, ifp, NULL) ||
2251 in6_setscope(&reddst6, ifp, NULL)) {
2256 src6 = ip6->ip6_src;
2257 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2259 "ICMP6 redirect sent from %s rejected; "
2260 "must be from linklocal\n",
2261 ip6_sprintf(ip6buf, &src6)));
2264 if (ip6->ip6_hlim != 255) {
2266 "ICMP6 redirect sent from %s rejected; "
2267 "hlim=%d (must be 255)\n",
2268 ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
2272 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2273 struct nhop_object *nh;
2274 struct in6_addr kdst;
2277 in6_splitscope(&reddst6, &kdst, &scopeid);
2279 nh = fib6_lookup(ifp->if_fib, &kdst, scopeid, 0, 0);
2281 struct in6_addr nh_addr;
2282 nh_addr = ifatoia6(nh->nh_ifa)->ia_addr.sin6_addr;
2283 if ((nh->nh_flags & NHF_GATEWAY) == 0) {
2285 "ICMP6 redirect rejected; no route "
2286 "with inet6 gateway found for redirect dst: %s\n",
2287 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2292 * Embed scope zone id into next hop address.
2294 nh_addr = nh->gw6_sa.sin6_addr;
2296 if (IN6_ARE_ADDR_EQUAL(&src6, &nh_addr) == 0) {
2298 "ICMP6 redirect rejected; "
2299 "not equal to gw-for-src=%s (must be same): "
2301 ip6_sprintf(ip6buf, &nh_addr),
2302 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2307 "ICMP6 redirect rejected; "
2308 "no route found for redirect dst: %s\n",
2309 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2313 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2315 "ICMP6 redirect rejected; "
2316 "redirect dst must be unicast: %s\n",
2317 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2321 is_router = is_onlink = 0;
2322 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2323 is_router = 1; /* router case */
2324 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2325 is_onlink = 1; /* on-link destination case */
2326 if (!is_router && !is_onlink) {
2328 "ICMP6 redirect rejected; "
2329 "neither router case nor onlink case: %s\n",
2330 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2334 icmp6len -= sizeof(*nd_rd);
2335 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2336 if (nd6_options(&ndopts) < 0) {
2337 nd6log((LOG_INFO, "%s: invalid ND option, rejected: %s\n",
2338 __func__, icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2339 /* nd6_options have incremented stats */
2345 if (ndopts.nd_opts_tgt_lladdr) {
2346 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2347 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2350 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2351 nd6log((LOG_INFO, "%s: lladdrlen mismatch for %s "
2352 "(if %d, icmp6 packet %d): %s\n",
2353 __func__, ip6_sprintf(ip6buf, &redtgt6),
2354 ifp->if_addrlen, lladdrlen - 2,
2355 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2359 /* Validation passed. */
2362 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2363 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2366 * Install a gateway route in the better-router case or an interface
2367 * route in the on-link-destination case.
2370 struct sockaddr_in6 sdst;
2371 struct sockaddr_in6 sgw;
2372 struct sockaddr_in6 ssrc;
2373 struct sockaddr *gw;
2377 bzero(&sdst, sizeof(sdst));
2378 bzero(&ssrc, sizeof(ssrc));
2379 sdst.sin6_family = ssrc.sin6_family = AF_INET6;
2380 sdst.sin6_len = ssrc.sin6_len = sizeof(struct sockaddr_in6);
2381 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2382 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2385 bzero(&sgw, sizeof(sgw));
2386 sgw.sin6_family = AF_INET6;
2387 sgw.sin6_len = sizeof(struct sockaddr_in6);
2388 bcopy(&redtgt6, &sgw.sin6_addr,
2389 sizeof(struct in6_addr));
2390 gw = (struct sockaddr *)&sgw;
2391 rt_flags |= RTF_GATEWAY;
2393 gw = ifp->if_addr->ifa_addr;
2394 for (fibnum = 0; fibnum < rt_numfibs; fibnum++)
2395 rib_add_redirect(fibnum, (struct sockaddr *)&sdst, gw,
2396 (struct sockaddr *)&ssrc, ifp, rt_flags,
2397 V_icmp6_redirtimeout);
2399 /* finally update cached route in each socket via pfctlinput */
2401 struct sockaddr_in6 sdst;
2403 bzero(&sdst, sizeof(sdst));
2404 sdst.sin6_family = AF_INET6;
2405 sdst.sin6_len = sizeof(struct sockaddr_in6);
2406 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2407 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2415 ICMP6STAT_INC(icp6s_badredirect);
2420 icmp6_redirect_output(struct mbuf *m0, struct nhop_object *nh)
2422 struct ifnet *ifp; /* my outgoing interface */
2423 struct in6_addr *ifp_ll6;
2424 struct in6_addr *router_ll6;
2425 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2426 struct mbuf *m = NULL; /* newly allocated one */
2428 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2429 struct nd_redirect *nd_rd;
2430 struct llentry *ln = NULL;
2433 struct ifnet *outif = NULL;
2434 struct sockaddr_in6 src_sa;
2436 icmp6_errcount(ND_REDIRECT, 0);
2438 /* if we are not router, we don't send icmp6 redirect */
2439 if (!V_ip6_forwarding)
2443 if (!m0 || !nh || !(NH_IS_VALID(nh)) || !(ifp = nh->nh_ifp))
2448 * the source address must identify a neighbor, and
2449 * the destination address must not be a multicast address
2450 * [RFC 2461, sec 8.2]
2452 sip6 = mtod(m0, struct ip6_hdr *);
2453 bzero(&src_sa, sizeof(src_sa));
2454 src_sa.sin6_family = AF_INET6;
2455 src_sa.sin6_len = sizeof(src_sa);
2456 src_sa.sin6_addr = sip6->ip6_src;
2457 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2459 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2460 goto fail; /* what should we do here? */
2463 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2467 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2468 * we almost always ask for an mbuf cluster for simplicity.
2469 * (MHLEN < IPV6_MMTU is almost always true)
2471 #if IPV6_MMTU >= MCLBYTES
2472 # error assumption failed about IPV6_MMTU and MCLBYTES
2474 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2477 M_SETFIB(m, M_GETFIB(m0));
2478 maxlen = M_TRAILINGSPACE(m);
2479 maxlen = min(IPV6_MMTU, maxlen);
2480 /* just for safety */
2481 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2482 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2487 /* get ip6 linklocal address for ifp(my outgoing interface). */
2488 struct in6_ifaddr *ia;
2489 if ((ia = in6ifa_ifpforlinklocal(ifp,
2491 IN6_IFF_ANYCAST)) == NULL)
2493 ifp_ll6 = &ia->ia_addr.sin6_addr;
2494 /* XXXRW: reference released prematurely. */
2495 ifa_free(&ia->ia_ifa);
2498 /* get ip6 linklocal address for the router. */
2499 if (nh->nh_flags & NHF_GATEWAY) {
2500 struct sockaddr_in6 *sin6;
2502 router_ll6 = &sin6->sin6_addr;
2503 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2504 router_ll6 = (struct in6_addr *)NULL;
2506 router_ll6 = (struct in6_addr *)NULL;
2509 ip6 = mtod(m, struct ip6_hdr *);
2511 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2512 ip6->ip6_vfc |= IPV6_VERSION;
2513 /* ip6->ip6_plen will be set later */
2514 ip6->ip6_nxt = IPPROTO_ICMPV6;
2515 ip6->ip6_hlim = 255;
2516 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2517 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2518 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2521 nd_rd = (struct nd_redirect *)(ip6 + 1);
2522 nd_rd->nd_rd_type = ND_REDIRECT;
2523 nd_rd->nd_rd_code = 0;
2524 nd_rd->nd_rd_reserved = 0;
2525 if (nh->nh_flags & NHF_GATEWAY) {
2527 * nd_rd->nd_rd_target must be a link-local address in
2528 * better router cases.
2532 bcopy(router_ll6, &nd_rd->nd_rd_target,
2533 sizeof(nd_rd->nd_rd_target));
2534 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2535 sizeof(nd_rd->nd_rd_dst));
2537 /* make sure redtgt == reddst */
2538 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2539 sizeof(nd_rd->nd_rd_target));
2540 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2541 sizeof(nd_rd->nd_rd_dst));
2544 p = (u_char *)(nd_rd + 1);
2550 /* target lladdr option */
2552 struct nd_opt_hdr *nd_opt;
2555 ln = nd6_lookup(router_ll6, 0, ifp);
2559 len = sizeof(*nd_opt) + ifp->if_addrlen;
2560 len = (len + 7) & ~7; /* round by 8 */
2562 if (len + (p - (u_char *)ip6) > maxlen)
2565 if (ln->la_flags & LLE_VALID) {
2566 nd_opt = (struct nd_opt_hdr *)p;
2567 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2568 nd_opt->nd_opt_len = len >> 3;
2569 lladdr = (char *)(nd_opt + 1);
2570 bcopy(ln->ll_addr, lladdr, ifp->if_addrlen);
2578 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2580 /* just to be safe */
2581 #ifdef M_DECRYPTED /*not openbsd*/
2582 if (m0->m_flags & M_DECRYPTED)
2585 if (p - (u_char *)ip6 > maxlen)
2589 /* redirected header option */
2591 struct nd_opt_rd_hdr *nd_opt_rh;
2594 * compute the maximum size for icmp6 redirect header option.
2595 * XXX room for auth header?
2597 len = maxlen - (p - (u_char *)ip6);
2600 /* This is just for simplicity. */
2601 if (m0->m_pkthdr.len != m0->m_len) {
2603 m_freem(m0->m_next);
2606 m0->m_pkthdr.len = m0->m_len;
2610 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2611 * about padding/truncate rule for the original IP packet.
2612 * From the discussion on IPv6imp in Feb 1999,
2613 * the consensus was:
2614 * - "attach as much as possible" is the goal
2615 * - pad if not aligned (original size can be guessed by
2616 * original ip6 header)
2617 * Following code adds the padding if it is simple enough,
2618 * and truncates if not.
2620 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2621 panic("assumption failed in %s:%d", __FILE__,
2624 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2625 /* not enough room, truncate */
2626 m0->m_pkthdr.len = m0->m_len = len -
2629 /* enough room, pad or truncate */
2632 extra = m0->m_pkthdr.len % 8;
2634 /* pad if easy enough, truncate if not */
2635 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2637 m0->m_len += (8 - extra);
2638 m0->m_pkthdr.len += (8 - extra);
2641 m0->m_pkthdr.len -= extra;
2645 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2646 m0->m_pkthdr.len = m0->m_len = len -
2650 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2651 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2652 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2653 nd_opt_rh->nd_opt_rh_len = len >> 3;
2654 p += sizeof(*nd_opt_rh);
2655 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2657 /* connect m0 to m */
2658 m_tag_delete_chain(m0, NULL);
2659 m0->m_flags &= ~M_PKTHDR;
2661 m->m_pkthdr.len = m->m_len + m0->m_len;
2670 /* XXX: clear embedded link IDs in the inner header */
2671 in6_clearscope(&sip6->ip6_src);
2672 in6_clearscope(&sip6->ip6_dst);
2673 in6_clearscope(&nd_rd->nd_rd_target);
2674 in6_clearscope(&nd_rd->nd_rd_dst);
2676 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2678 nd_rd->nd_rd_cksum = 0;
2679 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2680 sizeof(*ip6), ntohs(ip6->ip6_plen));
2682 if (send_sendso_input_hook != NULL) {
2683 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short),
2687 *(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type;
2688 m_tag_prepend(m, mtag);
2691 /* send the packet to outside... */
2692 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2694 icmp6_ifstat_inc(outif, ifs6_out_msg);
2695 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2697 ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]);
2709 * ICMPv6 socket option processing.
2712 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2716 struct inpcb *inp = sotoinpcb(so);
2717 int level, op, optname;
2720 level = sopt->sopt_level;
2721 op = sopt->sopt_dir;
2722 optname = sopt->sopt_name;
2723 optlen = sopt->sopt_valsize;
2725 level = op = optname = optlen = 0;
2727 if (level != IPPROTO_ICMPV6) {
2736 struct icmp6_filter ic6f;
2738 if (optlen != sizeof(ic6f)) {
2742 error = sooptcopyin(sopt, &ic6f, optlen, optlen);
2745 *inp->in6p_icmp6filt = ic6f;
2752 error = ENOPROTOOPT;
2761 struct icmp6_filter ic6f;
2764 ic6f = *inp->in6p_icmp6filt;
2766 error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
2771 error = ENOPROTOOPT;
2781 * Perform rate limit check.
2782 * Returns 0 if it is okay to send the icmp6 packet.
2783 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2786 * XXX per-destination/type check necessary?
2788 * dst - not used at this moment
2789 * type - not used at this moment
2790 * code - not used at this moment
2793 icmp6_ratelimit(const struct in6_addr *dst, const int type,
2798 ret = 0; /* okay to send */
2801 if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count,
2802 V_icmp6errppslim)) {
2803 /* The packet is subject to rate limit */