2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
70 #include <sys/param.h>
71 #include <sys/domain.h>
73 #include <sys/kernel.h>
75 #include <sys/malloc.h>
78 #include <sys/protosw.h>
79 #include <sys/signalvar.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
83 #include <sys/syslog.h>
84 #include <sys/systm.h>
88 #include <net/if_dl.h>
89 #include <net/if_llatbl.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
94 #include <netinet/in.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip6.h>
98 #include <netinet/icmp6.h>
99 #include <netinet/tcp_var.h>
101 #include <netinet6/in6_ifattach.h>
102 #include <netinet6/in6_pcb.h>
103 #include <netinet6/ip6protosw.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet6/scope6_var.h>
106 #include <netinet6/mld6_var.h>
107 #include <netinet6/nd6.h>
110 #include <netipsec/ipsec.h>
111 #include <netipsec/key.h>
114 extern struct domain inet6domain;
116 VNET_DEFINE(struct icmp6stat, icmp6stat);
118 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
119 VNET_DECLARE(struct inpcbhead, ripcb);
120 VNET_DECLARE(int, icmp6errppslim);
121 static VNET_DEFINE(int, icmp6errpps_count) = 0;
122 static VNET_DEFINE(struct timeval, icmp6errppslim_last);
123 VNET_DECLARE(int, icmp6_nodeinfo);
125 #define V_ripcbinfo VNET(ripcbinfo)
126 #define V_ripcb VNET(ripcb)
127 #define V_icmp6errppslim VNET(icmp6errppslim)
128 #define V_icmp6errpps_count VNET(icmp6errpps_count)
129 #define V_icmp6errppslim_last VNET(icmp6errppslim_last)
130 #define V_icmp6_nodeinfo VNET(icmp6_nodeinfo)
132 static void icmp6_errcount(struct icmp6errstat *, int, int);
133 static int icmp6_rip6_input(struct mbuf **, int);
134 static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
135 static const char *icmp6_redirect_diag __P((struct in6_addr *,
136 struct in6_addr *, struct in6_addr *));
137 static struct mbuf *ni6_input(struct mbuf *, int);
138 static struct mbuf *ni6_nametodns(const char *, int, int);
139 static int ni6_dnsmatch(const char *, int, const char *, int);
140 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
141 struct ifnet **, struct in6_addr *));
142 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
143 struct ifnet *, int));
144 static int icmp6_notify_error(struct mbuf **, int, int, int);
147 * Kernel module interface for updating icmp6stat. The argument is an index
148 * into icmp6stat treated as an array of u_quad_t. While this encodes the
149 * general layout of icmp6stat into the caller, it doesn't encode its
150 * location, so that future changes to add, for example, per-CPU stats
151 * support won't cause binary compatibility problems for kernel modules.
154 kmod_icmp6stat_inc(int statnum)
157 (*((u_quad_t *)&V_icmp6stat + statnum))++;
161 icmp6_errcount(struct icmp6errstat *stat, int type, int code)
164 case ICMP6_DST_UNREACH:
166 case ICMP6_DST_UNREACH_NOROUTE:
167 stat->icp6errs_dst_unreach_noroute++;
169 case ICMP6_DST_UNREACH_ADMIN:
170 stat->icp6errs_dst_unreach_admin++;
172 case ICMP6_DST_UNREACH_BEYONDSCOPE:
173 stat->icp6errs_dst_unreach_beyondscope++;
175 case ICMP6_DST_UNREACH_ADDR:
176 stat->icp6errs_dst_unreach_addr++;
178 case ICMP6_DST_UNREACH_NOPORT:
179 stat->icp6errs_dst_unreach_noport++;
183 case ICMP6_PACKET_TOO_BIG:
184 stat->icp6errs_packet_too_big++;
186 case ICMP6_TIME_EXCEEDED:
188 case ICMP6_TIME_EXCEED_TRANSIT:
189 stat->icp6errs_time_exceed_transit++;
191 case ICMP6_TIME_EXCEED_REASSEMBLY:
192 stat->icp6errs_time_exceed_reassembly++;
196 case ICMP6_PARAM_PROB:
198 case ICMP6_PARAMPROB_HEADER:
199 stat->icp6errs_paramprob_header++;
201 case ICMP6_PARAMPROB_NEXTHEADER:
202 stat->icp6errs_paramprob_nextheader++;
204 case ICMP6_PARAMPROB_OPTION:
205 stat->icp6errs_paramprob_option++;
210 stat->icp6errs_redirect++;
213 stat->icp6errs_unknown++;
217 * A wrapper function for icmp6_error() necessary when the erroneous packet
218 * may not contain enough scope zone information.
221 icmp6_error2(struct mbuf *m, int type, int code, int param,
229 #ifndef PULLDOWN_TEST
230 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
232 if (m->m_len < sizeof(struct ip6_hdr)) {
233 m = m_pullup(m, sizeof(struct ip6_hdr));
239 ip6 = mtod(m, struct ip6_hdr *);
241 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
243 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
246 icmp6_error(m, type, code, param);
250 * Generate an error packet of type error in response to bad IP6 packet.
253 icmp6_error(struct mbuf *m, int type, int code, int param)
255 struct ip6_hdr *oip6, *nip6;
256 struct icmp6_hdr *icmp6;
261 ICMP6STAT_INC(icp6s_error);
263 /* count per-type-code statistics */
264 icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, type, code);
266 #ifdef M_DECRYPTED /*not openbsd*/
267 if (m->m_flags & M_DECRYPTED) {
268 ICMP6STAT_INC(icp6s_canterror);
273 #ifndef PULLDOWN_TEST
274 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
276 if (m->m_len < sizeof(struct ip6_hdr)) {
277 m = m_pullup(m, sizeof(struct ip6_hdr));
282 oip6 = mtod(m, struct ip6_hdr *);
285 * If the destination address of the erroneous packet is a multicast
286 * address, or the packet was sent using link-layer multicast,
287 * we should basically suppress sending an error (RFC 2463, Section
289 * We have two exceptions (the item e.2 in that section):
290 * - the Packet Too Big message can be sent for path MTU discovery.
291 * - the Parameter Problem Message that can be allowed an icmp6 error
292 * in the option type field. This check has been done in
293 * ip6_unknown_opt(), so we can just check the type and code.
295 if ((m->m_flags & (M_BCAST|M_MCAST) ||
296 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
297 (type != ICMP6_PACKET_TOO_BIG &&
298 (type != ICMP6_PARAM_PROB ||
299 code != ICMP6_PARAMPROB_OPTION)))
303 * RFC 2463, 2.4 (e.5): source address check.
304 * XXX: the case of anycast source?
306 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
307 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
311 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
315 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
316 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
317 struct icmp6_hdr *icp;
319 #ifndef PULLDOWN_TEST
320 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
321 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
323 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
326 ICMP6STAT_INC(icp6s_tooshort);
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 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
348 /* Finally, do rate limitation check. */
349 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
350 ICMP6STAT_INC(icp6s_toofreq);
355 * OK, ICMP6 can be generated.
358 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
359 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
361 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
362 M_PREPEND(m, preplen, M_DONTWAIT);
363 if (m && m->m_len < preplen)
364 m = m_pullup(m, preplen);
366 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
370 nip6 = mtod(m, struct ip6_hdr *);
371 nip6->ip6_src = oip6->ip6_src;
372 nip6->ip6_dst = oip6->ip6_dst;
374 in6_clearscope(&oip6->ip6_src);
375 in6_clearscope(&oip6->ip6_dst);
377 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
378 icmp6->icmp6_type = type;
379 icmp6->icmp6_code = code;
380 icmp6->icmp6_pptr = htonl((u_int32_t)param);
383 * icmp6_reflect() is designed to be in the input path.
384 * icmp6_error() can be called from both input and output path,
385 * and if we are in output path rcvif could contain bogus value.
386 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
387 * information in ip header (nip6).
389 m->m_pkthdr.rcvif = NULL;
391 ICMP6STAT_INC(icp6s_outhist[type]);
392 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
398 * If we can't tell whether or not we can generate ICMP6, free it.
404 * Process a received ICMP6 message.
407 icmp6_input(struct mbuf **mp, int *offp, int proto)
409 struct mbuf *m = *mp, *n;
411 struct ip6_hdr *ip6, *nip6;
412 struct icmp6_hdr *icmp6, *nicmp6;
414 int icmp6len = m->m_pkthdr.len - *offp;
416 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
418 ifp = m->m_pkthdr.rcvif;
420 #ifndef PULLDOWN_TEST
421 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
422 /* m might change if M_LOOP. So, call mtod after this */
426 * Locate icmp6 structure in mbuf, and check
427 * that not corrupted and of at least minimum length
430 ip6 = mtod(m, struct ip6_hdr *);
431 if (icmp6len < sizeof(struct icmp6_hdr)) {
432 ICMP6STAT_INC(icp6s_tooshort);
437 * Check multicast group membership.
438 * Note: SSM filters are not applied for ICMPv6 traffic.
440 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
441 struct in6_multi *inm;
443 inm = in6m_lookup(ifp, &ip6->ip6_dst);
445 IP6STAT_INC(ip6s_notmember);
446 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
452 * calculate the checksum
454 #ifndef PULLDOWN_TEST
455 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
457 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
459 ICMP6STAT_INC(icp6s_tooshort);
463 code = icmp6->icmp6_code;
465 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
467 "ICMP6 checksum error(%d|%x) %s\n",
468 icmp6->icmp6_type, sum,
469 ip6_sprintf(ip6bufs, &ip6->ip6_src)));
470 ICMP6STAT_INC(icp6s_checksum);
474 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
476 * Deliver very specific ICMP6 type only.
477 * This is important to deliver TOOBIG. Otherwise PMTUD
480 switch (icmp6->icmp6_type) {
481 case ICMP6_DST_UNREACH:
482 case ICMP6_PACKET_TOO_BIG:
483 case ICMP6_TIME_EXCEEDED:
490 ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]);
491 icmp6_ifstat_inc(ifp, ifs6_in_msg);
492 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
493 icmp6_ifstat_inc(ifp, ifs6_in_error);
495 switch (icmp6->icmp6_type) {
496 case ICMP6_DST_UNREACH:
497 icmp6_ifstat_inc(ifp, ifs6_in_dstunreach);
499 case ICMP6_DST_UNREACH_NOROUTE:
500 code = PRC_UNREACH_NET;
502 case ICMP6_DST_UNREACH_ADMIN:
503 icmp6_ifstat_inc(ifp, ifs6_in_adminprohib);
504 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
506 case ICMP6_DST_UNREACH_ADDR:
509 case ICMP6_DST_UNREACH_BEYONDSCOPE:
510 /* I mean "source address was incorrect." */
511 code = PRC_PARAMPROB;
513 case ICMP6_DST_UNREACH_NOPORT:
514 code = PRC_UNREACH_PORT;
522 case ICMP6_PACKET_TOO_BIG:
523 icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig);
525 /* validation is made in icmp6_mtudisc_update */
530 * Updating the path MTU will be done after examining
531 * intermediate extension headers.
536 case ICMP6_TIME_EXCEEDED:
537 icmp6_ifstat_inc(ifp, ifs6_in_timeexceed);
539 case ICMP6_TIME_EXCEED_TRANSIT:
540 code = PRC_TIMXCEED_INTRANS;
542 case ICMP6_TIME_EXCEED_REASSEMBLY:
543 code = PRC_TIMXCEED_REASS;
551 case ICMP6_PARAM_PROB:
552 icmp6_ifstat_inc(ifp, ifs6_in_paramprob);
554 case ICMP6_PARAMPROB_NEXTHEADER:
555 code = PRC_UNREACH_PROTOCOL;
557 case ICMP6_PARAMPROB_HEADER:
558 case ICMP6_PARAMPROB_OPTION:
559 code = PRC_PARAMPROB;
567 case ICMP6_ECHO_REQUEST:
568 icmp6_ifstat_inc(ifp, ifs6_in_echo);
571 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
575 if ((n->m_flags & M_EXT) != 0
576 || n->m_len < off + sizeof(struct icmp6_hdr)) {
578 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
581 MGETHDR(n, M_DONTWAIT, n0->m_type);
582 n0len = n0->m_pkthdr.len; /* save for use below */
584 M_MOVE_PKTHDR(n, n0);
585 if (n && maxlen >= MHLEN) {
586 MCLGET(n, M_DONTWAIT);
587 if ((n->m_flags & M_EXT) == 0) {
598 * Copy IPv6 and ICMPv6 only.
600 nip6 = mtod(n, struct ip6_hdr *);
601 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
602 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
603 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
604 noff = sizeof(struct ip6_hdr);
605 /* new mbuf contains only ipv6+icmpv6 headers */
606 n->m_len = noff + sizeof(struct icmp6_hdr);
608 * Adjust mbuf. ip6_plen will be adjusted in
611 m_adj(n0, off + sizeof(struct icmp6_hdr));
612 /* recalculate complete packet size */
613 n->m_pkthdr.len = n0len + (noff - off);
616 nip6 = mtod(n, struct ip6_hdr *);
617 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
621 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
622 nicmp6->icmp6_code = 0;
624 ICMP6STAT_INC(icp6s_reflect);
625 ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]);
626 icmp6_reflect(n, noff);
630 case ICMP6_ECHO_REPLY:
631 icmp6_ifstat_inc(ifp, ifs6_in_echoreply);
636 case MLD_LISTENER_QUERY:
637 case MLD_LISTENER_REPORT:
638 case MLD_LISTENER_DONE:
639 case MLDV2_LISTENER_REPORT:
641 * Drop MLD traffic which is not link-local, has a hop limit
642 * of greater than 1 hop, or which does not have the
643 * IPv6 HBH Router Alert option.
644 * As IPv6 HBH options are stripped in ip6_input() we must
645 * check an mbuf header flag.
646 * XXX Should we also sanity check that these messages
647 * were directed to a link-local multicast prefix?
649 if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0)
651 if (mld_input(m, off, icmp6len) != 0)
652 return (IPPROTO_DONE);
656 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
658 enum { WRU, FQDN } mode;
660 if (!V_icmp6_nodeinfo)
663 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
665 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
671 #ifndef PULLDOWN_TEST
672 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
675 n = m_copy(m, 0, M_COPYALL);
677 n = ni6_input(n, off);
678 /* XXX meaningless if n == NULL */
679 noff = sizeof(struct ip6_hdr);
683 int maxlen, maxhlen, hlen;
686 * XXX: this combination of flags is pointless,
687 * but should we keep this for compatibility?
689 if ((V_icmp6_nodeinfo & 5) != 5)
694 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
695 if (maxlen >= MCLBYTES) {
699 MGETHDR(n, M_DONTWAIT, m->m_type);
700 if (n && maxlen > MHLEN) {
701 MCLGET(n, M_DONTWAIT);
702 if ((n->m_flags & M_EXT) == 0) {
707 if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) {
709 * Previous code did a blind M_COPY_PKTHDR
710 * and said "just for rcvif". If true, then
711 * we could tolerate the dup failing (due to
712 * the deep copy of the tag chain). For now
713 * be conservative and just fail.
722 n->m_pkthdr.rcvif = NULL;
724 maxhlen = M_TRAILINGSPACE(n) - maxlen;
725 pr = curthread->td_ucred->cr_prison;
726 mtx_lock(&pr->pr_mtx);
727 hlen = strlen(pr->pr_hostname);
731 * Copy IPv6 and ICMPv6 only.
733 nip6 = mtod(n, struct ip6_hdr *);
734 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
735 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
736 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
737 p = (u_char *)(nicmp6 + 1);
739 /* meaningless TTL */
740 bcopy(pr->pr_hostname, p + 4, maxhlen);
741 mtx_unlock(&pr->pr_mtx);
742 noff = sizeof(struct ip6_hdr);
743 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
744 sizeof(struct icmp6_hdr) + 4 + maxhlen;
745 nicmp6->icmp6_type = ICMP6_WRUREPLY;
746 nicmp6->icmp6_code = 0;
749 ICMP6STAT_INC(icp6s_reflect);
750 ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]);
751 icmp6_reflect(n, noff);
761 case ND_ROUTER_SOLICIT:
762 icmp6_ifstat_inc(ifp, ifs6_in_routersolicit);
765 if (icmp6len < sizeof(struct nd_router_solicit))
767 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
769 nd6_rs_input(m, off, icmp6len);
773 nd6_rs_input(n, 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 ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
785 nd6_ra_input(m, off, icmp6len);
789 nd6_ra_input(n, off, icmp6len);
793 case ND_NEIGHBOR_SOLICIT:
794 icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit);
797 if (icmp6len < sizeof(struct nd_neighbor_solicit))
799 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
801 nd6_ns_input(m, off, icmp6len);
805 nd6_ns_input(n, off, icmp6len);
809 case ND_NEIGHBOR_ADVERT:
810 icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert);
813 if (icmp6len < sizeof(struct nd_neighbor_advert))
815 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
817 nd6_na_input(m, off, icmp6len);
821 nd6_na_input(n, off, icmp6len);
826 icmp6_ifstat_inc(ifp, ifs6_in_redirect);
829 if (icmp6len < sizeof(struct nd_redirect))
831 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
833 icmp6_redirect_input(m, off);
837 icmp6_redirect_input(n, off);
841 case ICMP6_ROUTER_RENUMBERING:
842 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
843 code != ICMP6_ROUTER_RENUMBERING_RESULT)
845 if (icmp6len < sizeof(struct icmp6_router_renum))
851 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
852 icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
853 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
854 ifp ? ifp->if_index : 0));
855 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
856 /* ICMPv6 error: MUST deliver it by spec... */
860 /* ICMPv6 informational: MUST not deliver */
864 if (icmp6_notify_error(&m, off, icmp6len, code) != 0) {
865 /* In this case, m should've been freed. */
866 return (IPPROTO_DONE);
871 ICMP6STAT_INC(icp6s_badcode);
875 ICMP6STAT_INC(icp6s_badlen);
879 /* deliver the packet to appropriate sockets */
880 icmp6_rip6_input(&m, *offp);
890 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code)
892 struct mbuf *m = *mp;
893 struct icmp6_hdr *icmp6;
894 struct ip6_hdr *eip6;
896 struct sockaddr_in6 icmp6src, icmp6dst;
898 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
899 ICMP6STAT_INC(icp6s_tooshort);
902 #ifndef PULLDOWN_TEST
903 IP6_EXTHDR_CHECK(m, off,
904 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
905 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
907 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
908 sizeof(*icmp6) + sizeof(struct ip6_hdr));
910 ICMP6STAT_INC(icp6s_tooshort);
914 eip6 = (struct ip6_hdr *)(icmp6 + 1);
916 /* Detect the upper level protocol */
918 void (*ctlfunc)(int, struct sockaddr *, void *);
919 u_int8_t nxt = eip6->ip6_nxt;
920 int eoff = off + sizeof(struct icmp6_hdr) +
921 sizeof(struct ip6_hdr);
922 struct ip6ctlparam ip6cp;
923 struct in6_addr *finaldst = NULL;
924 int icmp6type = icmp6->icmp6_type;
926 struct ip6_rthdr *rth;
927 struct ip6_rthdr0 *rth0;
930 while (1) { /* XXX: should avoid infinite loop explicitly? */
934 case IPPROTO_HOPOPTS:
935 case IPPROTO_DSTOPTS:
937 #ifndef PULLDOWN_TEST
938 IP6_EXTHDR_CHECK(m, 0,
939 eoff + sizeof(struct ip6_ext), -1);
940 eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
942 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
945 ICMP6STAT_INC(icp6s_tooshort);
950 if (nxt == IPPROTO_AH)
951 eoff += (eh->ip6e_len + 2) << 2;
953 eoff += (eh->ip6e_len + 1) << 3;
956 case IPPROTO_ROUTING:
958 * When the erroneous packet contains a
959 * routing header, we should examine the
960 * header to determine the final destination.
961 * Otherwise, we can't properly update
962 * information that depends on the final
963 * destination (e.g. path MTU).
965 #ifndef PULLDOWN_TEST
966 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
967 rth = (struct ip6_rthdr *)
968 (mtod(m, caddr_t) + eoff);
970 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
973 ICMP6STAT_INC(icp6s_tooshort);
977 rthlen = (rth->ip6r_len + 1) << 3;
979 * XXX: currently there is no
980 * officially defined type other
982 * Note that if the segment left field
983 * is 0, all intermediate hops must
986 if (rth->ip6r_segleft &&
987 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
990 #ifndef PULLDOWN_TEST
991 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
992 rth0 = (struct ip6_rthdr0 *)
993 (mtod(m, caddr_t) + eoff);
996 struct ip6_rthdr0 *, m,
999 ICMP6STAT_INC(icp6s_tooshort);
1003 /* just ignore a bogus header */
1004 if ((rth0->ip6r0_len % 2) == 0 &&
1005 (hops = rth0->ip6r0_len/2))
1006 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
1009 nxt = rth->ip6r_nxt;
1011 case IPPROTO_FRAGMENT:
1012 #ifndef PULLDOWN_TEST
1013 IP6_EXTHDR_CHECK(m, 0, eoff +
1014 sizeof(struct ip6_frag), -1);
1015 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1018 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1021 ICMP6STAT_INC(icp6s_tooshort);
1026 * Data after a fragment header is meaningless
1027 * unless it is the first fragment, but
1028 * we'll go to the notify label for path MTU
1031 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1034 eoff += sizeof(struct ip6_frag);
1039 * This case includes ESP and the No Next
1040 * Header. In such cases going to the notify
1041 * label does not have any meaning
1042 * (i.e. ctlfunc will be NULL), but we go
1043 * anyway since we might have to update
1044 * path MTU information.
1050 #ifndef PULLDOWN_TEST
1051 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1053 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1054 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1055 if (icmp6 == NULL) {
1056 ICMP6STAT_INC(icp6s_tooshort);
1062 * retrieve parameters from the inner IPv6 header, and convert
1063 * them into sockaddr structures.
1064 * XXX: there is no guarantee that the source or destination
1065 * addresses of the inner packet are in the same scope as
1066 * the addresses of the icmp packet. But there is no other
1067 * way to determine the zone.
1069 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1071 bzero(&icmp6dst, sizeof(icmp6dst));
1072 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1073 icmp6dst.sin6_family = AF_INET6;
1074 if (finaldst == NULL)
1075 icmp6dst.sin6_addr = eip6->ip6_dst;
1077 icmp6dst.sin6_addr = *finaldst;
1078 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1080 bzero(&icmp6src, sizeof(icmp6src));
1081 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1082 icmp6src.sin6_family = AF_INET6;
1083 icmp6src.sin6_addr = eip6->ip6_src;
1084 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1086 icmp6src.sin6_flowinfo =
1087 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1089 if (finaldst == NULL)
1090 finaldst = &eip6->ip6_dst;
1092 ip6cp.ip6c_icmp6 = icmp6;
1093 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1094 ip6cp.ip6c_off = eoff;
1095 ip6cp.ip6c_finaldst = finaldst;
1096 ip6cp.ip6c_src = &icmp6src;
1097 ip6cp.ip6c_nxt = nxt;
1099 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1100 notifymtu = ntohl(icmp6->icmp6_mtu);
1101 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1102 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1105 ctlfunc = (void (*)(int, struct sockaddr *, void *))
1106 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1108 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1121 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1123 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1124 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1125 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1126 u_int mtu = ntohl(icmp6->icmp6_mtu);
1127 struct in_conninfo inc;
1131 * RFC2460 section 5, last paragraph.
1132 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1133 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1134 * due to packet translator in the middle.
1135 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1138 if (mtu < IPV6_MMTU)
1143 * we reject ICMPv6 too big with abnormally small value.
1144 * XXX what is the good definition of "abnormally small"?
1146 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1153 * In case the suggested mtu is less than IPV6_MMTU, we
1154 * only need to remember that it was for above mentioned
1155 * "alwaysfrag" case.
1156 * Try to be as close to the spec as possible.
1158 if (mtu < IPV6_MMTU)
1159 mtu = IPV6_MMTU - 8;
1161 bzero(&inc, sizeof(inc));
1162 inc.inc_flags |= INC_ISIPV6;
1163 inc.inc6_faddr = *dst;
1164 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1167 if (mtu < tcp_maxmtu6(&inc, NULL)) {
1168 tcp_hc_updatemtu(&inc, mtu);
1169 ICMP6STAT_INC(icp6s_pmtuchg);
1174 * Process a Node Information Query packet, based on
1175 * draft-ietf-ipngwg-icmp-name-lookups-07.
1177 * Spec incompatibilities:
1178 * - IPv6 Subject address handling
1179 * - IPv4 Subject address handling support missing
1180 * - Proxy reply (answer even if it's not for me)
1181 * - joins NI group address at in6_ifattach() time only, does not cope
1182 * with hostname changes by sethostname(3)
1184 static struct mbuf *
1185 ni6_input(struct mbuf *m, int off)
1187 struct icmp6_nodeinfo *ni6, *nni6;
1188 struct mbuf *n = NULL;
1192 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1193 struct ni_reply_fqdn *fqdn;
1194 int addrs; /* for NI_QTYPE_NODEADDR */
1195 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1196 struct in6_addr in6_subj; /* subject address */
1197 struct ip6_hdr *ip6;
1198 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1200 struct in6_ifaddr *ia6 = NULL;
1202 ip6 = mtod(m, struct ip6_hdr *);
1203 #ifndef PULLDOWN_TEST
1204 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1206 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1208 /* m is already reclaimed */
1214 * Validate IPv6 source address.
1215 * The default configuration MUST be to refuse answering queries from
1216 * global-scope addresses according to RFC4602.
1218 * - it's not very clear what "refuse" means; this implementation
1220 * - it's not very easy to identify global-scope (unicast) addresses
1221 * since there are many prefixes for them. It should be safer
1222 * and in practice sufficient to check "all" but loopback and
1223 * link-local (note that site-local unicast was deprecated and
1224 * ULA is defined as global scope-wise)
1226 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1227 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1228 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1232 * Validate IPv6 destination address.
1234 * The Responder must discard the Query without further processing
1235 * unless it is one of the Responder's unicast or anycast addresses, or
1236 * a link-local scope multicast address which the Responder has joined.
1237 * [RFC4602, Section 5.]
1239 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1240 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1242 /* else it's a link-local multicast, fine */
1243 } else { /* unicast or anycast */
1244 if ((ia6 = ip6_getdstifaddr(m)) == NULL)
1245 goto bad; /* XXX impossible */
1247 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1248 !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1249 ifa_free(&ia6->ia_ifa);
1250 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1251 "a temporary address in %s:%d",
1252 __FILE__, __LINE__));
1255 ifa_free(&ia6->ia_ifa);
1258 /* validate query Subject field. */
1259 qtype = ntohs(ni6->ni_qtype);
1260 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1263 case NI_QTYPE_SUPTYPES:
1265 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1269 case NI_QTYPE_NODEADDR:
1270 case NI_QTYPE_IPV4ADDR:
1271 switch (ni6->ni_code) {
1272 case ICMP6_NI_SUBJ_IPV6:
1273 #if ICMP6_NI_SUBJ_IPV6 != 0
1277 * backward compatibility - try to accept 03 draft
1278 * format, where no Subject is present.
1280 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1285 #if ICMP6_NI_SUBJ_IPV6 != 0
1286 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1290 if (subjlen != sizeof(struct in6_addr))
1294 * Validate Subject address.
1296 * Not sure what exactly "address belongs to the node"
1297 * means in the spec, is it just unicast, or what?
1299 * At this moment we consider Subject address as
1300 * "belong to the node" if the Subject address equals
1301 * to the IPv6 destination address; validation for
1302 * IPv6 destination address should have done enough
1305 * We do not do proxy at this moment.
1307 /* m_pulldown instead of copy? */
1308 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1309 subjlen, (caddr_t)&in6_subj);
1310 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1313 subj = (char *)&in6_subj;
1314 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1318 * XXX if we are to allow other cases, we should really
1319 * be careful about scope here.
1320 * basically, we should disallow queries toward IPv6
1321 * destination X with subject Y,
1322 * if scope(X) > scope(Y).
1323 * if we allow scope(X) > scope(Y), it will result in
1324 * information leakage across scope boundary.
1328 case ICMP6_NI_SUBJ_FQDN:
1330 * Validate Subject name with gethostname(3).
1332 * The behavior may need some debate, since:
1333 * - we are not sure if the node has FQDN as
1334 * hostname (returned by gethostname(3)).
1335 * - the code does wildcard match for truncated names.
1336 * however, we are not sure if we want to perform
1337 * wildcard match, if gethostname(3) side has
1338 * truncated hostname.
1340 pr = curthread->td_ucred->cr_prison;
1341 mtx_lock(&pr->pr_mtx);
1342 n = ni6_nametodns(pr->pr_hostname,
1343 strlen(pr->pr_hostname), 0);
1344 mtx_unlock(&pr->pr_mtx);
1345 if (!n || n->m_next || n->m_len == 0)
1347 IP6_EXTHDR_GET(subj, char *, m,
1348 off + sizeof(struct icmp6_nodeinfo), subjlen);
1351 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1359 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1366 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1369 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1372 case NI_QTYPE_NODEADDR:
1373 case NI_QTYPE_IPV4ADDR:
1374 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1379 /* guess reply length */
1382 break; /* no reply data */
1383 case NI_QTYPE_SUPTYPES:
1384 replylen += sizeof(u_int32_t);
1387 /* XXX will append an mbuf */
1388 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1390 case NI_QTYPE_NODEADDR:
1391 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1392 if ((replylen += addrs * (sizeof(struct in6_addr) +
1393 sizeof(u_int32_t))) > MCLBYTES)
1394 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1396 case NI_QTYPE_IPV4ADDR:
1397 /* unsupported - should respond with unknown Qtype? */
1401 * XXX: We must return a reply with the ICMP6 code
1402 * `unknown Qtype' in this case. However we regard the case
1403 * as an FQDN query for backward compatibility.
1404 * Older versions set a random value to this field,
1405 * so it rarely varies in the defined qtypes.
1406 * But the mechanism is not reliable...
1407 * maybe we should obsolete older versions.
1409 qtype = NI_QTYPE_FQDN;
1410 /* XXX will append an mbuf */
1411 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1416 /* allocate an mbuf to reply. */
1417 MGETHDR(n, M_DONTWAIT, m->m_type);
1422 M_MOVE_PKTHDR(n, m); /* just for recvif */
1423 if (replylen > MHLEN) {
1424 if (replylen > MCLBYTES) {
1426 * XXX: should we try to allocate more? But MCLBYTES
1427 * is probably much larger than IPV6_MMTU...
1431 MCLGET(n, M_DONTWAIT);
1432 if ((n->m_flags & M_EXT) == 0) {
1436 n->m_pkthdr.len = n->m_len = replylen;
1438 /* copy mbuf header and IPv6 + Node Information base headers */
1439 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1440 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1441 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1443 /* qtype dependent procedure */
1446 nni6->ni_code = ICMP6_NI_SUCCESS;
1449 case NI_QTYPE_SUPTYPES:
1452 nni6->ni_code = ICMP6_NI_SUCCESS;
1453 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1454 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1455 v = (u_int32_t)htonl(0x0000000f);
1456 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1460 nni6->ni_code = ICMP6_NI_SUCCESS;
1461 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1462 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1463 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1464 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1466 * XXX do we really have FQDN in hostname?
1468 pr = curthread->td_ucred->cr_prison;
1469 mtx_lock(&pr->pr_mtx);
1470 n->m_next = ni6_nametodns(pr->pr_hostname,
1471 strlen(pr->pr_hostname), oldfqdn);
1472 mtx_unlock(&pr->pr_mtx);
1473 if (n->m_next == NULL)
1475 /* XXX we assume that n->m_next is not a chain */
1476 if (n->m_next->m_next != NULL)
1478 n->m_pkthdr.len += n->m_next->m_len;
1480 case NI_QTYPE_NODEADDR:
1484 nni6->ni_code = ICMP6_NI_SUCCESS;
1485 n->m_pkthdr.len = n->m_len =
1486 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1487 lenlim = M_TRAILINGSPACE(n);
1488 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1489 /* XXX: reset mbuf length */
1490 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1491 sizeof(struct icmp6_nodeinfo) + copied;
1495 break; /* XXX impossible! */
1498 nni6->ni_type = ICMP6_NI_REPLY;
1510 * make a mbuf with DNS-encoded string. no compression support.
1512 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1513 * treated as truncated name (two \0 at the end). this is a wild guess.
1515 * old - return pascal string if non-zero
1517 static struct mbuf *
1518 ni6_nametodns(const char *name, int namelen, int old)
1530 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1531 MGET(m, M_DONTWAIT, MT_DATA);
1532 if (m && len > MLEN) {
1533 MCLGET(m, M_DONTWAIT);
1534 if ((m->m_flags & M_EXT) == 0)
1543 *mtod(m, char *) = namelen;
1544 bcopy(name, mtod(m, char *) + 1, namelen);
1548 cp = mtod(m, char *);
1549 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1551 /* if not certain about my name, return empty buffer */
1556 * guess if it looks like shortened hostname, or FQDN.
1557 * shortened hostname needs two trailing "\0".
1560 for (p = name; p < name + namelen; p++) {
1561 if (*p && *p == '.')
1570 while (cp < ep && p < name + namelen) {
1572 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1574 /* result does not fit into mbuf */
1575 if (cp + i + 1 >= ep)
1578 * DNS label length restriction, RFC1035 page 8.
1579 * "i == 0" case is included here to avoid returning
1580 * 0-length label on "foo..bar".
1582 if (i <= 0 || i >= 64)
1588 if (p < name + namelen && *p == '.')
1592 if (cp + nterm >= ep)
1596 m->m_len = cp - mtod(m, char *);
1600 panic("should not reach here");
1610 * check if two DNS-encoded string matches. takes care of truncated
1611 * form (with \0\0 at the end). no compression support.
1612 * XXX upper/lowercase match (see RFC2065)
1615 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1617 const char *a0, *b0;
1620 /* simplest case - need validation? */
1621 if (alen == blen && bcmp(a, b, alen) == 0)
1627 /* termination is mandatory */
1628 if (alen < 2 || blen < 2)
1630 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1635 while (a - a0 < alen && b - b0 < blen) {
1636 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1639 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1641 /* we don't support compression yet */
1642 if (a[0] >= 64 || b[0] >= 64)
1645 /* truncated case */
1646 if (a[0] == 0 && a - a0 == alen - 1)
1648 if (b[0] == 0 && b - b0 == blen - 1)
1650 if (a[0] == 0 || b[0] == 0)
1656 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1658 if (bcmp(a + 1, b + 1, l) != 0)
1665 if (a - a0 == alen && b - b0 == blen)
1672 * calculate the number of addresses to be returned in the node info reply.
1675 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1676 struct in6_addr *subj)
1679 struct in6_ifaddr *ifa6;
1681 int addrs = 0, addrsofif, iffound = 0;
1682 int niflags = ni6->ni_flags;
1684 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1685 switch (ni6->ni_code) {
1686 case ICMP6_NI_SUBJ_IPV6:
1687 if (subj == NULL) /* must be impossible... */
1692 * XXX: we only support IPv6 subject address for
1699 IFNET_RLOCK_NOSLEEP();
1700 for (ifp = TAILQ_FIRST(&V_ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1703 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1704 if (ifa->ifa_addr->sa_family != AF_INET6)
1706 ifa6 = (struct in6_ifaddr *)ifa;
1708 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1709 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1713 * IPv4-mapped addresses can only be returned by a
1714 * Node Information proxy, since they represent
1715 * addresses of IPv4-only nodes, which perforce do
1716 * not implement this protocol.
1717 * [icmp-name-lookups-07, Section 5.4]
1718 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1719 * this function at this moment.
1722 /* What do we have to do about ::1? */
1723 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1724 case IPV6_ADDR_SCOPE_LINKLOCAL:
1725 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1728 case IPV6_ADDR_SCOPE_SITELOCAL:
1729 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1732 case IPV6_ADDR_SCOPE_GLOBAL:
1733 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1741 * check if anycast is okay.
1742 * XXX: just experimental. not in the spec.
1744 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1745 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1746 continue; /* we need only unicast addresses */
1747 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1748 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1751 addrsofif++; /* count the address */
1753 IF_ADDR_UNLOCK(ifp);
1756 IFNET_RUNLOCK_NOSLEEP();
1762 IFNET_RUNLOCK_NOSLEEP();
1768 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1769 struct ifnet *ifp0, int resid)
1771 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet);
1772 struct in6_ifaddr *ifa6;
1774 struct ifnet *ifp_dep = NULL;
1775 int copied = 0, allow_deprecated = 0;
1776 u_char *cp = (u_char *)(nni6 + 1);
1777 int niflags = ni6->ni_flags;
1780 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1781 return (0); /* needless to copy */
1783 IFNET_RLOCK_NOSLEEP();
1786 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1788 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1789 if (ifa->ifa_addr->sa_family != AF_INET6)
1791 ifa6 = (struct in6_ifaddr *)ifa;
1793 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1794 allow_deprecated == 0) {
1796 * prefererred address should be put before
1797 * deprecated addresses.
1800 /* record the interface for later search */
1801 if (ifp_dep == NULL)
1805 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1806 allow_deprecated != 0)
1807 continue; /* we now collect deprecated addrs */
1809 /* What do we have to do about ::1? */
1810 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1811 case IPV6_ADDR_SCOPE_LINKLOCAL:
1812 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1815 case IPV6_ADDR_SCOPE_SITELOCAL:
1816 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1819 case IPV6_ADDR_SCOPE_GLOBAL:
1820 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1828 * check if anycast is okay.
1829 * XXX: just experimental. not in the spec.
1831 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1832 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1834 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1835 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1839 /* now we can copy the address */
1840 if (resid < sizeof(struct in6_addr) +
1841 sizeof(u_int32_t)) {
1842 IF_ADDR_UNLOCK(ifp);
1844 * We give up much more copy.
1845 * Set the truncate flag and return.
1847 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1848 IFNET_RUNLOCK_NOSLEEP();
1853 * Set the TTL of the address.
1854 * The TTL value should be one of the following
1855 * according to the specification:
1857 * 1. The remaining lifetime of a DHCP lease on the
1859 * 2. The remaining Valid Lifetime of a prefix from
1860 * which the address was derived through Stateless
1861 * Autoconfiguration.
1863 * Note that we currently do not support stateful
1864 * address configuration by DHCPv6, so the former
1865 * case can't happen.
1867 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1868 ltime = ND6_INFINITE_LIFETIME;
1870 if (ifa6->ia6_lifetime.ia6t_expire >
1872 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1877 bcopy(<ime, cp, sizeof(u_int32_t));
1878 cp += sizeof(u_int32_t);
1880 /* copy the address itself */
1881 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1882 sizeof(struct in6_addr));
1883 in6_clearscope((struct in6_addr *)cp); /* XXX */
1884 cp += sizeof(struct in6_addr);
1886 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1887 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1889 IF_ADDR_UNLOCK(ifp);
1890 if (ifp0) /* we need search only on the specified IF */
1894 if (allow_deprecated == 0 && ifp_dep != NULL) {
1896 allow_deprecated = 1;
1901 IFNET_RUNLOCK_NOSLEEP();
1907 * XXX almost dup'ed code with rip6_input.
1910 icmp6_rip6_input(struct mbuf **mp, int off)
1912 struct mbuf *m = *mp;
1913 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1915 struct inpcb *last = NULL;
1916 struct sockaddr_in6 fromsa;
1917 struct icmp6_hdr *icmp6;
1918 struct mbuf *opts = NULL;
1920 #ifndef PULLDOWN_TEST
1921 /* this is assumed to be safe. */
1922 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1924 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1925 if (icmp6 == NULL) {
1926 /* m is already reclaimed */
1927 return (IPPROTO_DONE);
1932 * XXX: the address may have embedded scope zone ID, which should be
1933 * hidden from applications.
1935 bzero(&fromsa, sizeof(fromsa));
1936 fromsa.sin6_family = AF_INET6;
1937 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1938 fromsa.sin6_addr = ip6->ip6_src;
1939 if (sa6_recoverscope(&fromsa)) {
1941 return (IPPROTO_DONE);
1944 INP_INFO_RLOCK(&V_ripcbinfo);
1945 LIST_FOREACH(in6p, &V_ripcb, inp_list) {
1946 if ((in6p->inp_vflag & INP_IPV6) == 0)
1948 if (in6p->inp_ip_p != IPPROTO_ICMPV6)
1950 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1951 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1953 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1954 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1957 if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1958 in6p->in6p_icmp6filt)) {
1963 struct mbuf *n = NULL;
1966 * Recent network drivers tend to allocate a single
1967 * mbuf cluster, rather than to make a couple of
1968 * mbufs without clusters. Also, since the IPv6 code
1969 * path tries to avoid m_pullup(), it is highly
1970 * probable that we still have an mbuf cluster here
1971 * even though the necessary length can be stored in an
1972 * mbuf's internal buffer.
1973 * Meanwhile, the default size of the receive socket
1974 * buffer for raw sockets is not so large. This means
1975 * the possibility of packet loss is relatively higher
1976 * than before. To avoid this scenario, we copy the
1977 * received data to a separate mbuf that does not use
1978 * a cluster, if possible.
1979 * XXX: it is better to copy the data after stripping
1980 * intermediate headers.
1982 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1983 m->m_len <= MHLEN) {
1984 MGET(n, M_DONTWAIT, m->m_type);
1986 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1987 bcopy(m->m_data, n->m_data,
1989 n->m_len = m->m_len;
1997 (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1998 if (last->inp_flags & INP_CONTROLOPTS)
1999 ip6_savecontrol(last, n, &opts);
2000 /* strip intermediate headers */
2002 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2003 if (sbappendaddr_locked(
2004 &last->inp_socket->so_rcv,
2005 (struct sockaddr *)&fromsa, n, opts)
2007 /* should notify about lost packet */
2013 &last->inp_socket->so_rcv);
2015 sorwakeup_locked(last->inp_socket);
2022 INP_INFO_RUNLOCK(&V_ripcbinfo);
2024 if (last->inp_flags & INP_CONTROLOPTS)
2025 ip6_savecontrol(last, m, &opts);
2026 /* strip intermediate headers */
2029 /* avoid using mbuf clusters if possible (see above) */
2030 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2031 m->m_len <= MHLEN) {
2034 MGET(n, M_DONTWAIT, m->m_type);
2036 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2037 bcopy(m->m_data, n->m_data, m->m_len);
2038 n->m_len = m->m_len;
2048 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2049 if (sbappendaddr_locked(&last->inp_socket->so_rcv,
2050 (struct sockaddr *)&fromsa, m, opts) == 0) {
2054 SOCKBUF_UNLOCK(&last->inp_socket->so_rcv);
2056 sorwakeup_locked(last->inp_socket);
2060 IP6STAT_DEC(ip6s_delivered);
2062 return IPPROTO_DONE;
2066 * Reflect the ip6 packet back to the source.
2067 * OFF points to the icmp6 header, counted from the top of the mbuf.
2070 icmp6_reflect(struct mbuf *m, size_t off)
2072 struct ip6_hdr *ip6;
2073 struct icmp6_hdr *icmp6;
2074 struct in6_ifaddr *ia = NULL;
2077 struct ifnet *outif = NULL;
2078 struct in6_addr origdst, src, *srcp = NULL;
2080 /* too short to reflect */
2081 if (off < sizeof(struct ip6_hdr)) {
2083 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2084 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2085 __FILE__, __LINE__));
2090 * If there are extra headers between IPv6 and ICMPv6, strip
2091 * off that header first.
2094 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2095 panic("assumption failed in icmp6_reflect");
2097 if (off > sizeof(struct ip6_hdr)) {
2099 struct ip6_hdr nip6;
2101 l = off - sizeof(struct ip6_hdr);
2102 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2104 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2106 if ((m = m_pullup(m, l)) == NULL)
2109 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2110 } else /* off == sizeof(struct ip6_hdr) */ {
2112 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2114 if ((m = m_pullup(m, l)) == NULL)
2118 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2119 ip6 = mtod(m, struct ip6_hdr *);
2120 ip6->ip6_nxt = IPPROTO_ICMPV6;
2121 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2122 type = icmp6->icmp6_type; /* keep type for statistics */
2123 code = icmp6->icmp6_code; /* ditto. */
2125 origdst = ip6->ip6_dst;
2127 * ip6_input() drops a packet if its src is multicast.
2128 * So, the src is never multicast.
2130 ip6->ip6_dst = ip6->ip6_src;
2133 * If the incoming packet was addressed directly to us (i.e. unicast),
2134 * use dst as the src for the reply.
2135 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2136 * (for example) when we encounter an error while forwarding procedure
2137 * destined to a duplicated address of ours.
2138 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2139 * procedure of an outgoing packet of our own, in which case we need
2140 * to search in the ifaddr list.
2142 if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
2143 if ((ia = ip6_getdstifaddr(m))) {
2144 if (!(ia->ia6_flags &
2145 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2146 srcp = &ia->ia_addr.sin6_addr;
2148 struct sockaddr_in6 d;
2150 bzero(&d, sizeof(d));
2151 d.sin6_family = AF_INET6;
2152 d.sin6_len = sizeof(d);
2153 d.sin6_addr = origdst;
2154 ia = (struct in6_ifaddr *)
2155 ifa_ifwithaddr((struct sockaddr *)&d);
2158 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2159 srcp = &ia->ia_addr.sin6_addr;
2164 if ((srcp != NULL) &&
2165 (in6_addrscope(srcp) != in6_addrscope(&ip6->ip6_src)))
2170 struct sockaddr_in6 sin6;
2171 struct route_in6 ro;
2174 * This case matches to multicasts, our anycast, or unicasts
2175 * that we do not own. Select a source address based on the
2176 * source address of the erroneous packet.
2178 bzero(&sin6, sizeof(sin6));
2179 sin6.sin6_family = AF_INET6;
2180 sin6.sin6_len = sizeof(sin6);
2181 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2183 bzero(&ro, sizeof(ro));
2184 e = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &src);
2186 RTFREE(ro.ro_rt); /* XXX: we could use this */
2188 char ip6buf[INET6_ADDRSTRLEN];
2190 "icmp6_reflect: source can't be determined: "
2191 "dst=%s, error=%d\n",
2192 ip6_sprintf(ip6buf, &sin6.sin6_addr), e));
2198 ip6->ip6_src = *srcp;
2200 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2201 ip6->ip6_vfc |= IPV6_VERSION;
2202 ip6->ip6_nxt = IPPROTO_ICMPV6;
2204 ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2205 else if (m->m_pkthdr.rcvif) {
2206 /* XXX: This may not be the outgoing interface */
2207 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2209 ip6->ip6_hlim = V_ip6_defhlim;
2211 icmp6->icmp6_cksum = 0;
2212 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2213 sizeof(struct ip6_hdr), plen);
2216 * XXX option handling
2219 m->m_flags &= ~(M_BCAST|M_MCAST);
2221 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2223 icmp6_ifoutstat_inc(outif, type, code);
2226 ifa_free(&ia->ia_ifa);
2231 ifa_free(&ia->ia_ifa);
2237 icmp6_fasttimo(void)
2244 icmp6_slowtimo(void)
2251 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2252 struct in6_addr *tgt6)
2254 static char buf[1024];
2255 char ip6bufs[INET6_ADDRSTRLEN];
2256 char ip6bufd[INET6_ADDRSTRLEN];
2257 char ip6buft[INET6_ADDRSTRLEN];
2258 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2259 ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2260 ip6_sprintf(ip6buft, tgt6));
2265 icmp6_redirect_input(struct mbuf *m, int off)
2268 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2269 struct nd_redirect *nd_rd;
2270 int icmp6len = ntohs(ip6->ip6_plen);
2271 char *lladdr = NULL;
2273 u_char *redirhdr = NULL;
2274 int redirhdrlen = 0;
2275 struct rtentry *rt = NULL;
2278 struct in6_addr src6 = ip6->ip6_src;
2279 struct in6_addr redtgt6;
2280 struct in6_addr reddst6;
2281 union nd_opts ndopts;
2282 char ip6buf[INET6_ADDRSTRLEN];
2287 ifp = m->m_pkthdr.rcvif;
2292 /* XXX if we are router, we don't update route by icmp6 redirect */
2293 if (V_ip6_forwarding)
2295 if (!V_icmp6_rediraccept)
2298 #ifndef PULLDOWN_TEST
2299 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2300 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2302 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2303 if (nd_rd == NULL) {
2304 ICMP6STAT_INC(icp6s_tooshort);
2308 redtgt6 = nd_rd->nd_rd_target;
2309 reddst6 = nd_rd->nd_rd_dst;
2311 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2312 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2317 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2319 "ICMP6 redirect sent from %s rejected; "
2320 "must be from linklocal\n",
2321 ip6_sprintf(ip6buf, &src6)));
2324 if (ip6->ip6_hlim != 255) {
2326 "ICMP6 redirect sent from %s rejected; "
2327 "hlim=%d (must be 255)\n",
2328 ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
2332 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2333 struct sockaddr_in6 sin6;
2334 struct in6_addr *gw6;
2336 bzero(&sin6, sizeof(sin6));
2337 sin6.sin6_family = AF_INET6;
2338 sin6.sin6_len = sizeof(struct sockaddr_in6);
2339 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2340 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
2342 if (rt->rt_gateway == NULL ||
2343 rt->rt_gateway->sa_family != AF_INET6) {
2345 "ICMP6 redirect rejected; no route "
2346 "with inet6 gateway found for redirect dst: %s\n",
2347 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2352 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2353 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2355 "ICMP6 redirect rejected; "
2356 "not equal to gw-for-src=%s (must be same): "
2358 ip6_sprintf(ip6buf, gw6),
2359 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2365 "ICMP6 redirect rejected; "
2366 "no route found for redirect dst: %s\n",
2367 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2373 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2375 "ICMP6 redirect rejected; "
2376 "redirect dst must be unicast: %s\n",
2377 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2381 is_router = is_onlink = 0;
2382 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2383 is_router = 1; /* router case */
2384 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2385 is_onlink = 1; /* on-link destination case */
2386 if (!is_router && !is_onlink) {
2388 "ICMP6 redirect rejected; "
2389 "neither router case nor onlink case: %s\n",
2390 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2393 /* validation passed */
2395 icmp6len -= sizeof(*nd_rd);
2396 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2397 if (nd6_options(&ndopts) < 0) {
2398 nd6log((LOG_INFO, "icmp6_redirect_input: "
2399 "invalid ND option, rejected: %s\n",
2400 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2401 /* nd6_options have incremented stats */
2405 if (ndopts.nd_opts_tgt_lladdr) {
2406 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2407 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2410 if (ndopts.nd_opts_rh) {
2411 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2412 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2415 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2417 "icmp6_redirect_input: lladdrlen mismatch for %s "
2418 "(if %d, icmp6 packet %d): %s\n",
2419 ip6_sprintf(ip6buf, &redtgt6),
2420 ifp->if_addrlen, lladdrlen - 2,
2421 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2426 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2427 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2429 if (!is_onlink) { /* better router case. perform rtredirect. */
2430 /* perform rtredirect */
2431 struct sockaddr_in6 sdst;
2432 struct sockaddr_in6 sgw;
2433 struct sockaddr_in6 ssrc;
2435 bzero(&sdst, sizeof(sdst));
2436 bzero(&sgw, sizeof(sgw));
2437 bzero(&ssrc, sizeof(ssrc));
2438 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2439 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2440 sizeof(struct sockaddr_in6);
2441 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2442 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2443 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2444 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2445 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2446 (struct sockaddr *)&ssrc);
2448 /* finally update cached route in each socket via pfctlinput */
2450 struct sockaddr_in6 sdst;
2452 bzero(&sdst, sizeof(sdst));
2453 sdst.sin6_family = AF_INET6;
2454 sdst.sin6_len = sizeof(struct sockaddr_in6);
2455 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2456 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2458 key_sa_routechange((struct sockaddr *)&sdst);
2467 ICMP6STAT_INC(icp6s_badredirect);
2472 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2474 struct ifnet *ifp; /* my outgoing interface */
2475 struct in6_addr *ifp_ll6;
2476 struct in6_addr *router_ll6;
2477 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2478 struct mbuf *m = NULL; /* newly allocated one */
2479 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2480 struct nd_redirect *nd_rd;
2481 struct llentry *ln = NULL;
2484 struct ifnet *outif = NULL;
2485 struct sockaddr_in6 src_sa;
2487 icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2489 /* if we are not router, we don't send icmp6 redirect */
2490 if (!V_ip6_forwarding)
2494 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2499 * the source address must identify a neighbor, and
2500 * the destination address must not be a multicast address
2501 * [RFC 2461, sec 8.2]
2503 sip6 = mtod(m0, struct ip6_hdr *);
2504 bzero(&src_sa, sizeof(src_sa));
2505 src_sa.sin6_family = AF_INET6;
2506 src_sa.sin6_len = sizeof(src_sa);
2507 src_sa.sin6_addr = sip6->ip6_src;
2508 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2510 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2511 goto fail; /* what should we do here? */
2514 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2518 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2519 * we almost always ask for an mbuf cluster for simplicity.
2520 * (MHLEN < IPV6_MMTU is almost always true)
2522 #if IPV6_MMTU >= MCLBYTES
2523 # error assumption failed about IPV6_MMTU and MCLBYTES
2525 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2526 if (m && IPV6_MMTU >= MHLEN)
2527 MCLGET(m, M_DONTWAIT);
2530 m->m_pkthdr.rcvif = NULL;
2532 maxlen = M_TRAILINGSPACE(m);
2533 maxlen = min(IPV6_MMTU, maxlen);
2534 /* just for safety */
2535 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2536 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2541 /* get ip6 linklocal address for ifp(my outgoing interface). */
2542 struct in6_ifaddr *ia;
2543 if ((ia = in6ifa_ifpforlinklocal(ifp,
2545 IN6_IFF_ANYCAST)) == NULL)
2547 ifp_ll6 = &ia->ia_addr.sin6_addr;
2548 /* XXXRW: reference released prematurely. */
2549 ifa_free(&ia->ia_ifa);
2552 /* get ip6 linklocal address for the router. */
2553 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2554 struct sockaddr_in6 *sin6;
2555 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2556 router_ll6 = &sin6->sin6_addr;
2557 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2558 router_ll6 = (struct in6_addr *)NULL;
2560 router_ll6 = (struct in6_addr *)NULL;
2563 ip6 = mtod(m, struct ip6_hdr *);
2565 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2566 ip6->ip6_vfc |= IPV6_VERSION;
2567 /* ip6->ip6_plen will be set later */
2568 ip6->ip6_nxt = IPPROTO_ICMPV6;
2569 ip6->ip6_hlim = 255;
2570 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2571 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2572 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2575 nd_rd = (struct nd_redirect *)(ip6 + 1);
2576 nd_rd->nd_rd_type = ND_REDIRECT;
2577 nd_rd->nd_rd_code = 0;
2578 nd_rd->nd_rd_reserved = 0;
2579 if (rt->rt_flags & RTF_GATEWAY) {
2581 * nd_rd->nd_rd_target must be a link-local address in
2582 * better router cases.
2586 bcopy(router_ll6, &nd_rd->nd_rd_target,
2587 sizeof(nd_rd->nd_rd_target));
2588 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2589 sizeof(nd_rd->nd_rd_dst));
2591 /* make sure redtgt == reddst */
2592 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2593 sizeof(nd_rd->nd_rd_target));
2594 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2595 sizeof(nd_rd->nd_rd_dst));
2598 p = (u_char *)(nd_rd + 1);
2604 /* target lladdr option */
2606 struct nd_opt_hdr *nd_opt;
2609 IF_AFDATA_LOCK(ifp);
2610 ln = nd6_lookup(router_ll6, 0, ifp);
2611 IF_AFDATA_UNLOCK(ifp);
2615 len = sizeof(*nd_opt) + ifp->if_addrlen;
2616 len = (len + 7) & ~7; /* round by 8 */
2618 if (len + (p - (u_char *)ip6) > maxlen)
2621 if (ln->la_flags & LLE_VALID) {
2622 nd_opt = (struct nd_opt_hdr *)p;
2623 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2624 nd_opt->nd_opt_len = len >> 3;
2625 lladdr = (char *)(nd_opt + 1);
2626 bcopy(&ln->ll_addr, lladdr, ifp->if_addrlen);
2634 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2636 /* just to be safe */
2637 #ifdef M_DECRYPTED /*not openbsd*/
2638 if (m0->m_flags & M_DECRYPTED)
2641 if (p - (u_char *)ip6 > maxlen)
2645 /* redirected header option */
2647 struct nd_opt_rd_hdr *nd_opt_rh;
2650 * compute the maximum size for icmp6 redirect header option.
2651 * XXX room for auth header?
2653 len = maxlen - (p - (u_char *)ip6);
2656 /* This is just for simplicity. */
2657 if (m0->m_pkthdr.len != m0->m_len) {
2659 m_freem(m0->m_next);
2662 m0->m_pkthdr.len = m0->m_len;
2666 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2667 * about padding/truncate rule for the original IP packet.
2668 * From the discussion on IPv6imp in Feb 1999,
2669 * the consensus was:
2670 * - "attach as much as possible" is the goal
2671 * - pad if not aligned (original size can be guessed by
2672 * original ip6 header)
2673 * Following code adds the padding if it is simple enough,
2674 * and truncates if not.
2676 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2677 panic("assumption failed in %s:%d", __FILE__,
2680 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2681 /* not enough room, truncate */
2682 m0->m_pkthdr.len = m0->m_len = len -
2685 /* enough room, pad or truncate */
2688 extra = m0->m_pkthdr.len % 8;
2690 /* pad if easy enough, truncate if not */
2691 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2693 m0->m_len += (8 - extra);
2694 m0->m_pkthdr.len += (8 - extra);
2697 m0->m_pkthdr.len -= extra;
2701 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2702 m0->m_pkthdr.len = m0->m_len = len -
2706 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2707 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2708 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2709 nd_opt_rh->nd_opt_rh_len = len >> 3;
2710 p += sizeof(*nd_opt_rh);
2711 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2713 /* connect m0 to m */
2714 m_tag_delete_chain(m0, NULL);
2715 m0->m_flags &= ~M_PKTHDR;
2717 m->m_pkthdr.len = m->m_len + m0->m_len;
2726 /* XXX: clear embedded link IDs in the inner header */
2727 in6_clearscope(&sip6->ip6_src);
2728 in6_clearscope(&sip6->ip6_dst);
2729 in6_clearscope(&nd_rd->nd_rd_target);
2730 in6_clearscope(&nd_rd->nd_rd_dst);
2732 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2734 nd_rd->nd_rd_cksum = 0;
2735 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2736 sizeof(*ip6), ntohs(ip6->ip6_plen));
2738 /* send the packet to outside... */
2739 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2741 icmp6_ifstat_inc(outif, ifs6_out_msg);
2742 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2744 ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]);
2756 * ICMPv6 socket option processing.
2759 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2763 struct inpcb *inp = sotoinpcb(so);
2764 int level, op, optname;
2767 level = sopt->sopt_level;
2768 op = sopt->sopt_dir;
2769 optname = sopt->sopt_name;
2770 optlen = sopt->sopt_valsize;
2772 level = op = optname = optlen = 0;
2774 if (level != IPPROTO_ICMPV6) {
2783 struct icmp6_filter ic6f;
2785 if (optlen != sizeof(ic6f)) {
2789 error = sooptcopyin(sopt, &ic6f, optlen, optlen);
2792 *inp->in6p_icmp6filt = ic6f;
2799 error = ENOPROTOOPT;
2808 struct icmp6_filter ic6f;
2811 ic6f = *inp->in6p_icmp6filt;
2813 error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
2818 error = ENOPROTOOPT;
2828 * Perform rate limit check.
2829 * Returns 0 if it is okay to send the icmp6 packet.
2830 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2833 * XXX per-destination/type check necessary?
2835 * dst - not used at this moment
2836 * type - not used at this moment
2837 * code - not used at this moment
2840 icmp6_ratelimit(const struct in6_addr *dst, const int type,
2845 ret = 0; /* okay to send */
2848 if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count,
2849 V_icmp6errppslim)) {
2850 /* The packet is subject to rate limit */