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>
72 #include <sys/kernel.h>
74 #include <sys/malloc.h>
76 #include <sys/protosw.h>
77 #include <sys/signalvar.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
81 #include <sys/syslog.h>
82 #include <sys/systm.h>
84 #include <sys/vimage.h>
87 #include <net/if_dl.h>
88 #include <net/if_llatbl.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
93 #include <netinet/in.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip6.h>
97 #include <netinet/icmp6.h>
98 #include <netinet/tcp_var.h>
99 #include <netinet/vinet.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>
108 #include <netinet6/vinet6.h>
111 #include <netipsec/ipsec.h>
112 #include <netipsec/key.h>
115 extern struct domain inet6domain;
117 #ifdef VIMAGE_GLOBALS
118 extern struct inpcbinfo ripcbinfo;
119 extern struct inpcbhead ripcb;
120 extern int icmp6errppslim;
121 extern int icmp6_nodeinfo;
123 struct icmp6stat icmp6stat;
124 static int icmp6errpps_count;
125 static struct timeval icmp6errppslim_last;
128 static void icmp6_errcount(struct icmp6errstat *, int, int);
129 static int icmp6_rip6_input(struct mbuf **, int);
130 static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
131 static const char *icmp6_redirect_diag __P((struct in6_addr *,
132 struct in6_addr *, struct in6_addr *));
133 static struct mbuf *ni6_input(struct mbuf *, int);
134 static struct mbuf *ni6_nametodns(const char *, int, int);
135 static int ni6_dnsmatch(const char *, int, const char *, int);
136 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
137 struct ifnet **, struct in6_addr *));
138 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
139 struct ifnet *, int));
140 static int icmp6_notify_error(struct mbuf **, int, int, int);
146 INIT_VNET_INET6(curvnet);
148 V_icmp6errpps_count = 0;
154 icmp6_errcount(struct icmp6errstat *stat, int type, int code)
157 case ICMP6_DST_UNREACH:
159 case ICMP6_DST_UNREACH_NOROUTE:
160 stat->icp6errs_dst_unreach_noroute++;
162 case ICMP6_DST_UNREACH_ADMIN:
163 stat->icp6errs_dst_unreach_admin++;
165 case ICMP6_DST_UNREACH_BEYONDSCOPE:
166 stat->icp6errs_dst_unreach_beyondscope++;
168 case ICMP6_DST_UNREACH_ADDR:
169 stat->icp6errs_dst_unreach_addr++;
171 case ICMP6_DST_UNREACH_NOPORT:
172 stat->icp6errs_dst_unreach_noport++;
176 case ICMP6_PACKET_TOO_BIG:
177 stat->icp6errs_packet_too_big++;
179 case ICMP6_TIME_EXCEEDED:
181 case ICMP6_TIME_EXCEED_TRANSIT:
182 stat->icp6errs_time_exceed_transit++;
184 case ICMP6_TIME_EXCEED_REASSEMBLY:
185 stat->icp6errs_time_exceed_reassembly++;
189 case ICMP6_PARAM_PROB:
191 case ICMP6_PARAMPROB_HEADER:
192 stat->icp6errs_paramprob_header++;
194 case ICMP6_PARAMPROB_NEXTHEADER:
195 stat->icp6errs_paramprob_nextheader++;
197 case ICMP6_PARAMPROB_OPTION:
198 stat->icp6errs_paramprob_option++;
203 stat->icp6errs_redirect++;
206 stat->icp6errs_unknown++;
210 * A wrapper function for icmp6_error() necessary when the erroneous packet
211 * may not contain enough scope zone information.
214 icmp6_error2(struct mbuf *m, int type, int code, int param,
217 INIT_VNET_INET6(curvnet);
223 #ifndef PULLDOWN_TEST
224 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
226 if (m->m_len < sizeof(struct ip6_hdr)) {
227 m = m_pullup(m, sizeof(struct ip6_hdr));
233 ip6 = mtod(m, struct ip6_hdr *);
235 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
237 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
240 icmp6_error(m, type, code, param);
244 * Generate an error packet of type error in response to bad IP6 packet.
247 icmp6_error(struct mbuf *m, int type, int code, int param)
249 INIT_VNET_INET6(curvnet);
250 struct ip6_hdr *oip6, *nip6;
251 struct icmp6_hdr *icmp6;
256 V_icmp6stat.icp6s_error++;
258 /* count per-type-code statistics */
259 icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, type, code);
261 #ifdef M_DECRYPTED /*not openbsd*/
262 if (m->m_flags & M_DECRYPTED) {
263 V_icmp6stat.icp6s_canterror++;
268 #ifndef PULLDOWN_TEST
269 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
271 if (m->m_len < sizeof(struct ip6_hdr)) {
272 m = m_pullup(m, sizeof(struct ip6_hdr));
277 oip6 = mtod(m, struct ip6_hdr *);
280 * If the destination address of the erroneous packet is a multicast
281 * address, or the packet was sent using link-layer multicast,
282 * we should basically suppress sending an error (RFC 2463, Section
284 * We have two exceptions (the item e.2 in that section):
285 * - the Pakcet Too Big message can be sent for path MTU discovery.
286 * - the Parameter Problem Message that can be allowed an icmp6 error
287 * in the option type field. This check has been done in
288 * ip6_unknown_opt(), so we can just check the type and code.
290 if ((m->m_flags & (M_BCAST|M_MCAST) ||
291 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
292 (type != ICMP6_PACKET_TOO_BIG &&
293 (type != ICMP6_PARAM_PROB ||
294 code != ICMP6_PARAMPROB_OPTION)))
298 * RFC 2463, 2.4 (e.5): source address check.
299 * XXX: the case of anycast source?
301 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
302 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
306 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
310 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
311 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
312 struct icmp6_hdr *icp;
314 #ifndef PULLDOWN_TEST
315 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
316 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
318 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
321 V_icmp6stat.icp6s_tooshort++;
325 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
326 icp->icmp6_type == ND_REDIRECT) {
329 * Special case: for redirect (which is
330 * informational) we must not send icmp6 error.
332 V_icmp6stat.icp6s_canterror++;
335 /* ICMPv6 informational - send the error */
338 /* non-ICMPv6 - send the error */
341 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
343 /* Finally, do rate limitation check. */
344 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
345 V_icmp6stat.icp6s_toofreq++;
350 * OK, ICMP6 can be generated.
353 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
354 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
356 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
357 M_PREPEND(m, preplen, M_DONTWAIT);
358 if (m && m->m_len < preplen)
359 m = m_pullup(m, preplen);
361 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
365 nip6 = mtod(m, struct ip6_hdr *);
366 nip6->ip6_src = oip6->ip6_src;
367 nip6->ip6_dst = oip6->ip6_dst;
369 in6_clearscope(&oip6->ip6_src);
370 in6_clearscope(&oip6->ip6_dst);
372 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
373 icmp6->icmp6_type = type;
374 icmp6->icmp6_code = code;
375 icmp6->icmp6_pptr = htonl((u_int32_t)param);
378 * icmp6_reflect() is designed to be in the input path.
379 * icmp6_error() can be called from both input and output path,
380 * and if we are in output path rcvif could contain bogus value.
381 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
382 * information in ip header (nip6).
384 m->m_pkthdr.rcvif = NULL;
386 V_icmp6stat.icp6s_outhist[type]++;
387 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
393 * If we can't tell whether or not we can generate ICMP6, free it.
399 * Process a received ICMP6 message.
402 icmp6_input(struct mbuf **mp, int *offp, int proto)
404 INIT_VNET_INET6(curvnet);
405 INIT_VPROCG(TD_TO_VPROCG(curthread)); /* XXX V_hostname needs this */
406 struct mbuf *m = *mp, *n;
407 struct ip6_hdr *ip6, *nip6;
408 struct icmp6_hdr *icmp6, *nicmp6;
410 int icmp6len = m->m_pkthdr.len - *offp;
412 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
414 #ifndef PULLDOWN_TEST
415 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
416 /* m might change if M_LOOP. So, call mtod after this */
420 * Locate icmp6 structure in mbuf, and check
421 * that not corrupted and of at least minimum length
424 ip6 = mtod(m, struct ip6_hdr *);
425 if (icmp6len < sizeof(struct icmp6_hdr)) {
426 V_icmp6stat.icp6s_tooshort++;
431 * calculate the checksum
433 #ifndef PULLDOWN_TEST
434 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
436 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
438 V_icmp6stat.icp6s_tooshort++;
442 code = icmp6->icmp6_code;
444 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
446 "ICMP6 checksum error(%d|%x) %s\n",
447 icmp6->icmp6_type, sum,
448 ip6_sprintf(ip6bufs, &ip6->ip6_src)));
449 V_icmp6stat.icp6s_checksum++;
453 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
455 * Deliver very specific ICMP6 type only.
456 * This is important to deliver TOOBIG. Otherwise PMTUD
459 switch (icmp6->icmp6_type) {
460 case ICMP6_DST_UNREACH:
461 case ICMP6_PACKET_TOO_BIG:
462 case ICMP6_TIME_EXCEEDED:
469 V_icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
470 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
471 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
472 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
474 switch (icmp6->icmp6_type) {
475 case ICMP6_DST_UNREACH:
476 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
478 case ICMP6_DST_UNREACH_NOROUTE:
479 code = PRC_UNREACH_NET;
481 case ICMP6_DST_UNREACH_ADMIN:
482 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
483 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
485 case ICMP6_DST_UNREACH_ADDR:
488 case ICMP6_DST_UNREACH_BEYONDSCOPE:
489 /* I mean "source address was incorrect." */
490 code = PRC_PARAMPROB;
492 case ICMP6_DST_UNREACH_NOPORT:
493 code = PRC_UNREACH_PORT;
501 case ICMP6_PACKET_TOO_BIG:
502 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
504 /* validation is made in icmp6_mtudisc_update */
509 * Updating the path MTU will be done after examining
510 * intermediate extension headers.
515 case ICMP6_TIME_EXCEEDED:
516 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
518 case ICMP6_TIME_EXCEED_TRANSIT:
519 code = PRC_TIMXCEED_INTRANS;
521 case ICMP6_TIME_EXCEED_REASSEMBLY:
522 code = PRC_TIMXCEED_REASS;
530 case ICMP6_PARAM_PROB:
531 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
533 case ICMP6_PARAMPROB_NEXTHEADER:
534 code = PRC_UNREACH_PROTOCOL;
536 case ICMP6_PARAMPROB_HEADER:
537 case ICMP6_PARAMPROB_OPTION:
538 code = PRC_PARAMPROB;
546 case ICMP6_ECHO_REQUEST:
547 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
550 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
554 if ((n->m_flags & M_EXT) != 0
555 || n->m_len < off + sizeof(struct icmp6_hdr)) {
557 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
560 MGETHDR(n, M_DONTWAIT, n0->m_type);
561 n0len = n0->m_pkthdr.len; /* save for use below */
563 M_MOVE_PKTHDR(n, n0);
564 if (n && maxlen >= MHLEN) {
565 MCLGET(n, M_DONTWAIT);
566 if ((n->m_flags & M_EXT) == 0) {
577 * Copy IPv6 and ICMPv6 only.
579 nip6 = mtod(n, struct ip6_hdr *);
580 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
581 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
582 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
583 noff = sizeof(struct ip6_hdr);
584 /* new mbuf contains only ipv6+icmpv6 headers */
585 n->m_len = noff + sizeof(struct icmp6_hdr);
587 * Adjust mbuf. ip6_plen will be adjusted in
590 m_adj(n0, off + sizeof(struct icmp6_hdr));
591 /* recalculate complete packet size */
592 n->m_pkthdr.len = n0len + (noff - off);
595 nip6 = mtod(n, struct ip6_hdr *);
596 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
600 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
601 nicmp6->icmp6_code = 0;
603 V_icmp6stat.icp6s_reflect++;
604 V_icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
605 icmp6_reflect(n, noff);
609 case ICMP6_ECHO_REPLY:
610 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
615 case MLD_LISTENER_QUERY:
616 case MLD_LISTENER_REPORT:
617 if (icmp6len < sizeof(struct mld_hdr))
619 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
620 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
622 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
623 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
633 case MLD_LISTENER_DONE:
634 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
635 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
637 break; /* nothing to be done in kernel */
639 case MLD_MTRACE_RESP:
641 /* XXX: these two are experimental. not officially defined. */
642 /* XXX: per-interface statistics? */
643 break; /* just pass it to applications */
645 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
647 enum { WRU, FQDN } mode;
649 if (!V_icmp6_nodeinfo)
652 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
654 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
659 #define hostnamelen strlen(V_hostname)
661 #ifndef PULLDOWN_TEST
662 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
665 n = m_copy(m, 0, M_COPYALL);
667 n = ni6_input(n, off);
668 /* XXX meaningless if n == NULL */
669 noff = sizeof(struct ip6_hdr);
675 * XXX: this combination of flags is pointless,
676 * but should we keep this for compatibility?
678 if ((V_icmp6_nodeinfo & 5) != 5)
683 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
684 if (maxlen >= MCLBYTES) {
688 MGETHDR(n, M_DONTWAIT, m->m_type);
689 if (n && maxlen > MHLEN) {
690 MCLGET(n, M_DONTWAIT);
691 if ((n->m_flags & M_EXT) == 0) {
696 if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) {
698 * Previous code did a blind M_COPY_PKTHDR
699 * and said "just for rcvif". If true, then
700 * we could tolerate the dup failing (due to
701 * the deep copy of the tag chain). For now
702 * be conservative and just fail.
711 n->m_pkthdr.rcvif = NULL;
713 maxhlen = M_TRAILINGSPACE(n) - maxlen;
714 mtx_lock(&hostname_mtx);
715 if (maxhlen > hostnamelen)
716 maxhlen = hostnamelen;
718 * Copy IPv6 and ICMPv6 only.
720 nip6 = mtod(n, struct ip6_hdr *);
721 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
722 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
723 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
724 p = (u_char *)(nicmp6 + 1);
726 bcopy(V_hostname, p + 4, maxhlen); /* meaningless TTL */
727 mtx_unlock(&hostname_mtx);
728 noff = sizeof(struct ip6_hdr);
729 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
730 sizeof(struct icmp6_hdr) + 4 + maxhlen;
731 nicmp6->icmp6_type = ICMP6_WRUREPLY;
732 nicmp6->icmp6_code = 0;
736 V_icmp6stat.icp6s_reflect++;
737 V_icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
738 icmp6_reflect(n, noff);
748 case ND_ROUTER_SOLICIT:
749 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
752 if (icmp6len < sizeof(struct nd_router_solicit))
754 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
756 nd6_rs_input(m, off, icmp6len);
760 nd6_rs_input(n, off, icmp6len);
764 case ND_ROUTER_ADVERT:
765 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
768 if (icmp6len < sizeof(struct nd_router_advert))
770 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
772 nd6_ra_input(m, off, icmp6len);
776 nd6_ra_input(n, off, icmp6len);
780 case ND_NEIGHBOR_SOLICIT:
781 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
784 if (icmp6len < sizeof(struct nd_neighbor_solicit))
786 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
788 nd6_ns_input(m, off, icmp6len);
792 nd6_ns_input(n, off, icmp6len);
796 case ND_NEIGHBOR_ADVERT:
797 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
800 if (icmp6len < sizeof(struct nd_neighbor_advert))
802 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
804 nd6_na_input(m, off, icmp6len);
808 nd6_na_input(n, off, icmp6len);
813 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
816 if (icmp6len < sizeof(struct nd_redirect))
818 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
820 icmp6_redirect_input(m, off);
824 icmp6_redirect_input(n, off);
828 case ICMP6_ROUTER_RENUMBERING:
829 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
830 code != ICMP6_ROUTER_RENUMBERING_RESULT)
832 if (icmp6len < sizeof(struct icmp6_router_renum))
838 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
839 icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
840 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
841 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
842 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
843 /* ICMPv6 error: MUST deliver it by spec... */
847 /* ICMPv6 informational: MUST not deliver */
851 if (icmp6_notify_error(&m, off, icmp6len, code)) {
852 /* In this case, m should've been freed. */
853 return (IPPROTO_DONE);
858 V_icmp6stat.icp6s_badcode++;
862 V_icmp6stat.icp6s_badlen++;
866 /* deliver the packet to appropriate sockets */
867 icmp6_rip6_input(&m, *offp);
877 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code)
879 INIT_VNET_INET6(curvnet);
880 struct mbuf *m = *mp;
881 struct icmp6_hdr *icmp6;
882 struct ip6_hdr *eip6;
884 struct sockaddr_in6 icmp6src, icmp6dst;
886 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
887 V_icmp6stat.icp6s_tooshort++;
890 #ifndef PULLDOWN_TEST
891 IP6_EXTHDR_CHECK(m, off,
892 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
893 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
895 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
896 sizeof(*icmp6) + sizeof(struct ip6_hdr));
898 V_icmp6stat.icp6s_tooshort++;
902 eip6 = (struct ip6_hdr *)(icmp6 + 1);
904 /* Detect the upper level protocol */
906 void (*ctlfunc)(int, struct sockaddr *, void *);
907 u_int8_t nxt = eip6->ip6_nxt;
908 int eoff = off + sizeof(struct icmp6_hdr) +
909 sizeof(struct ip6_hdr);
910 struct ip6ctlparam ip6cp;
911 struct in6_addr *finaldst = NULL;
912 int icmp6type = icmp6->icmp6_type;
914 struct ip6_rthdr *rth;
915 struct ip6_rthdr0 *rth0;
918 while (1) { /* XXX: should avoid infinite loop explicitly? */
922 case IPPROTO_HOPOPTS:
923 case IPPROTO_DSTOPTS:
925 #ifndef PULLDOWN_TEST
926 IP6_EXTHDR_CHECK(m, 0,
927 eoff + sizeof(struct ip6_ext), -1);
928 eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
930 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
933 V_icmp6stat.icp6s_tooshort++;
938 if (nxt == IPPROTO_AH)
939 eoff += (eh->ip6e_len + 2) << 2;
941 eoff += (eh->ip6e_len + 1) << 3;
944 case IPPROTO_ROUTING:
946 * When the erroneous packet contains a
947 * routing header, we should examine the
948 * header to determine the final destination.
949 * Otherwise, we can't properly update
950 * information that depends on the final
951 * destination (e.g. path MTU).
953 #ifndef PULLDOWN_TEST
954 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
955 rth = (struct ip6_rthdr *)
956 (mtod(m, caddr_t) + eoff);
958 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
961 V_icmp6stat.icp6s_tooshort++;
965 rthlen = (rth->ip6r_len + 1) << 3;
967 * XXX: currently there is no
968 * officially defined type other
970 * Note that if the segment left field
971 * is 0, all intermediate hops must
974 if (rth->ip6r_segleft &&
975 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
978 #ifndef PULLDOWN_TEST
979 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
980 rth0 = (struct ip6_rthdr0 *)
981 (mtod(m, caddr_t) + eoff);
984 struct ip6_rthdr0 *, m,
987 V_icmp6stat.icp6s_tooshort++;
991 /* just ignore a bogus header */
992 if ((rth0->ip6r0_len % 2) == 0 &&
993 (hops = rth0->ip6r0_len/2))
994 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
999 case IPPROTO_FRAGMENT:
1000 #ifndef PULLDOWN_TEST
1001 IP6_EXTHDR_CHECK(m, 0, eoff +
1002 sizeof(struct ip6_frag), -1);
1003 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1006 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1009 V_icmp6stat.icp6s_tooshort++;
1014 * Data after a fragment header is meaningless
1015 * unless it is the first fragment, but
1016 * we'll go to the notify label for path MTU
1019 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1022 eoff += sizeof(struct ip6_frag);
1027 * This case includes ESP and the No Next
1028 * Header. In such cases going to the notify
1029 * label does not have any meaning
1030 * (i.e. ctlfunc will be NULL), but we go
1031 * anyway since we might have to update
1032 * path MTU information.
1038 #ifndef PULLDOWN_TEST
1039 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1041 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1042 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1043 if (icmp6 == NULL) {
1044 V_icmp6stat.icp6s_tooshort++;
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,
1109 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1111 INIT_VNET_INET6(curvnet);
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_flags |= INC_ISIPV6;
1152 inc.inc6_faddr = *dst;
1153 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1156 if (mtu < tcp_maxmtu6(&inc, NULL)) {
1157 tcp_hc_updatemtu(&inc, mtu);
1158 V_icmp6stat.icp6s_pmtuchg++;
1163 * Process a Node Information Query packet, based on
1164 * draft-ietf-ipngwg-icmp-name-lookups-07.
1166 * Spec incompatibilities:
1167 * - IPv6 Subject address handling
1168 * - IPv4 Subject address handling support missing
1169 * - Proxy reply (answer even if it's not for me)
1170 * - joins NI group address at in6_ifattach() time only, does not cope
1171 * with hostname changes by sethostname(3)
1173 #define hostnamelen strlen(V_hostname)
1174 static struct mbuf *
1175 ni6_input(struct mbuf *m, int off)
1177 INIT_VNET_INET6(curvnet);
1178 INIT_VPROCG(TD_TO_VPROCG(curthread)); /* XXX V_hostname needs this */
1179 struct icmp6_nodeinfo *ni6, *nni6;
1180 struct mbuf *n = NULL;
1183 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1184 struct ni_reply_fqdn *fqdn;
1185 int addrs; /* for NI_QTYPE_NODEADDR */
1186 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1187 struct in6_addr in6_subj; /* subject address */
1188 struct ip6_hdr *ip6;
1189 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1191 struct in6_ifaddr *ia6 = NULL;
1193 ip6 = mtod(m, struct ip6_hdr *);
1194 #ifndef PULLDOWN_TEST
1195 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1197 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1199 /* m is already reclaimed */
1205 * Validate IPv6 source address.
1206 * The default configuration MUST be to refuse answering queries from
1207 * global-scope addresses according to RFC4602.
1209 * - it's not very clear what "refuse" means; this implementation
1211 * - it's not very easy to identify global-scope (unicast) addresses
1212 * since there are many prefixes for them. It should be safer
1213 * and in practice sufficient to check "all" but loopback and
1214 * link-local (note that site-local unicast was deprecated and
1215 * ULA is defined as global scope-wise)
1217 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1218 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1219 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1223 * Validate IPv6 destination address.
1225 * The Responder must discard the Query without further processing
1226 * unless it is one of the Responder's unicast or anycast addresses, or
1227 * a link-local scope multicast address which the Responder has joined.
1228 * [RFC4602, Section 5.]
1230 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1231 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1233 /* else it's a link-local multicast, fine */
1234 } else { /* unicast or anycast */
1235 if ((ia6 = ip6_getdstifaddr(m)) == NULL)
1236 goto bad; /* XXX impossible */
1238 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1239 !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1240 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1241 "a temporary address in %s:%d",
1242 __FILE__, __LINE__));
1247 /* validate query Subject field. */
1248 qtype = ntohs(ni6->ni_qtype);
1249 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1252 case NI_QTYPE_SUPTYPES:
1254 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1258 case NI_QTYPE_NODEADDR:
1259 case NI_QTYPE_IPV4ADDR:
1260 switch (ni6->ni_code) {
1261 case ICMP6_NI_SUBJ_IPV6:
1262 #if ICMP6_NI_SUBJ_IPV6 != 0
1266 * backward compatibility - try to accept 03 draft
1267 * format, where no Subject is present.
1269 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1274 #if ICMP6_NI_SUBJ_IPV6 != 0
1275 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1279 if (subjlen != sizeof(struct in6_addr))
1283 * Validate Subject address.
1285 * Not sure what exactly "address belongs to the node"
1286 * means in the spec, is it just unicast, or what?
1288 * At this moment we consider Subject address as
1289 * "belong to the node" if the Subject address equals
1290 * to the IPv6 destination address; validation for
1291 * IPv6 destination address should have done enough
1294 * We do not do proxy at this moment.
1296 /* m_pulldown instead of copy? */
1297 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1298 subjlen, (caddr_t)&in6_subj);
1299 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1302 subj = (char *)&in6_subj;
1303 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1307 * XXX if we are to allow other cases, we should really
1308 * be careful about scope here.
1309 * basically, we should disallow queries toward IPv6
1310 * destination X with subject Y,
1311 * if scope(X) > scope(Y).
1312 * if we allow scope(X) > scope(Y), it will result in
1313 * information leakage across scope boundary.
1317 case ICMP6_NI_SUBJ_FQDN:
1319 * Validate Subject name with gethostname(3).
1321 * The behavior may need some debate, since:
1322 * - we are not sure if the node has FQDN as
1323 * hostname (returned by gethostname(3)).
1324 * - the code does wildcard match for truncated names.
1325 * however, we are not sure if we want to perform
1326 * wildcard match, if gethostname(3) side has
1327 * truncated hostname.
1329 mtx_lock(&hostname_mtx);
1330 n = ni6_nametodns(V_hostname, hostnamelen, 0);
1331 mtx_unlock(&hostname_mtx);
1332 if (!n || n->m_next || n->m_len == 0)
1334 IP6_EXTHDR_GET(subj, char *, m,
1335 off + sizeof(struct icmp6_nodeinfo), subjlen);
1338 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1346 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1353 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1356 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1359 case NI_QTYPE_NODEADDR:
1360 case NI_QTYPE_IPV4ADDR:
1361 if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1366 /* guess reply length */
1369 break; /* no reply data */
1370 case NI_QTYPE_SUPTYPES:
1371 replylen += sizeof(u_int32_t);
1374 /* XXX will append an mbuf */
1375 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1377 case NI_QTYPE_NODEADDR:
1378 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1379 if ((replylen += addrs * (sizeof(struct in6_addr) +
1380 sizeof(u_int32_t))) > MCLBYTES)
1381 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1383 case NI_QTYPE_IPV4ADDR:
1384 /* unsupported - should respond with unknown Qtype? */
1388 * XXX: We must return a reply with the ICMP6 code
1389 * `unknown Qtype' in this case. However we regard the case
1390 * as an FQDN query for backward compatibility.
1391 * Older versions set a random value to this field,
1392 * so it rarely varies in the defined qtypes.
1393 * But the mechanism is not reliable...
1394 * maybe we should obsolete older versions.
1396 qtype = NI_QTYPE_FQDN;
1397 /* XXX will append an mbuf */
1398 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1403 /* allocate an mbuf to reply. */
1404 MGETHDR(n, M_DONTWAIT, m->m_type);
1409 M_MOVE_PKTHDR(n, m); /* just for recvif */
1410 if (replylen > MHLEN) {
1411 if (replylen > MCLBYTES) {
1413 * XXX: should we try to allocate more? But MCLBYTES
1414 * is probably much larger than IPV6_MMTU...
1418 MCLGET(n, M_DONTWAIT);
1419 if ((n->m_flags & M_EXT) == 0) {
1423 n->m_pkthdr.len = n->m_len = replylen;
1425 /* copy mbuf header and IPv6 + Node Information base headers */
1426 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1427 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1428 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1430 /* qtype dependent procedure */
1433 nni6->ni_code = ICMP6_NI_SUCCESS;
1436 case NI_QTYPE_SUPTYPES:
1439 nni6->ni_code = ICMP6_NI_SUCCESS;
1440 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1441 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1442 v = (u_int32_t)htonl(0x0000000f);
1443 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1447 nni6->ni_code = ICMP6_NI_SUCCESS;
1448 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1449 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1450 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1451 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1453 * XXX do we really have FQDN in variable "hostname"?
1455 mtx_lock(&hostname_mtx);
1456 n->m_next = ni6_nametodns(V_hostname, hostnamelen, oldfqdn);
1457 mtx_unlock(&hostname_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;
1496 * make a mbuf with DNS-encoded string. no compression support.
1498 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1499 * treated as truncated name (two \0 at the end). this is a wild guess.
1501 * old - return pascal string if non-zero
1503 static struct mbuf *
1504 ni6_nametodns(const char *name, int namelen, int old)
1516 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1517 MGET(m, M_DONTWAIT, MT_DATA);
1518 if (m && len > MLEN) {
1519 MCLGET(m, M_DONTWAIT);
1520 if ((m->m_flags & M_EXT) == 0)
1529 *mtod(m, char *) = namelen;
1530 bcopy(name, mtod(m, char *) + 1, namelen);
1534 cp = mtod(m, char *);
1535 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1537 /* if not certain about my name, return empty buffer */
1542 * guess if it looks like shortened hostname, or FQDN.
1543 * shortened hostname needs two trailing "\0".
1546 for (p = name; p < name + namelen; p++) {
1547 if (*p && *p == '.')
1556 while (cp < ep && p < name + namelen) {
1558 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1560 /* result does not fit into mbuf */
1561 if (cp + i + 1 >= ep)
1564 * DNS label length restriction, RFC1035 page 8.
1565 * "i == 0" case is included here to avoid returning
1566 * 0-length label on "foo..bar".
1568 if (i <= 0 || i >= 64)
1574 if (p < name + namelen && *p == '.')
1578 if (cp + nterm >= ep)
1582 m->m_len = cp - mtod(m, char *);
1586 panic("should not reach here");
1596 * check if two DNS-encoded string matches. takes care of truncated
1597 * form (with \0\0 at the end). no compression support.
1598 * XXX upper/lowercase match (see RFC2065)
1601 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1603 const char *a0, *b0;
1606 /* simplest case - need validation? */
1607 if (alen == blen && bcmp(a, b, alen) == 0)
1613 /* termination is mandatory */
1614 if (alen < 2 || blen < 2)
1616 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1621 while (a - a0 < alen && b - b0 < blen) {
1622 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1625 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1627 /* we don't support compression yet */
1628 if (a[0] >= 64 || b[0] >= 64)
1631 /* truncated case */
1632 if (a[0] == 0 && a - a0 == alen - 1)
1634 if (b[0] == 0 && b - b0 == blen - 1)
1636 if (a[0] == 0 || b[0] == 0)
1642 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1644 if (bcmp(a + 1, b + 1, l) != 0)
1651 if (a - a0 == alen && b - b0 == blen)
1658 * calculate the number of addresses to be returned in the node info reply.
1661 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1662 struct in6_addr *subj)
1664 INIT_VNET_NET(curvnet);
1665 INIT_VNET_INET6(curvnet);
1667 struct in6_ifaddr *ifa6;
1669 int addrs = 0, addrsofif, iffound = 0;
1670 int niflags = ni6->ni_flags;
1672 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1673 switch (ni6->ni_code) {
1674 case ICMP6_NI_SUBJ_IPV6:
1675 if (subj == NULL) /* must be impossible... */
1680 * XXX: we only support IPv6 subject address for
1688 for (ifp = TAILQ_FIRST(&V_ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1690 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1691 if (ifa->ifa_addr->sa_family != AF_INET6)
1693 ifa6 = (struct in6_ifaddr *)ifa;
1695 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1696 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1700 * IPv4-mapped addresses can only be returned by a
1701 * Node Information proxy, since they represent
1702 * addresses of IPv4-only nodes, which perforce do
1703 * not implement this protocol.
1704 * [icmp-name-lookups-07, Section 5.4]
1705 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1706 * this function at this moment.
1709 /* What do we have to do about ::1? */
1710 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1711 case IPV6_ADDR_SCOPE_LINKLOCAL:
1712 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1715 case IPV6_ADDR_SCOPE_SITELOCAL:
1716 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1719 case IPV6_ADDR_SCOPE_GLOBAL:
1720 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1728 * check if anycast is okay.
1729 * XXX: just experimental. not in the spec.
1731 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1732 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1733 continue; /* we need only unicast addresses */
1734 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1735 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1738 addrsofif++; /* count the address */
1754 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1755 struct ifnet *ifp0, int resid)
1757 INIT_VNET_NET(curvnet);
1758 INIT_VNET_INET6(curvnet);
1759 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet);
1760 struct in6_ifaddr *ifa6;
1762 struct ifnet *ifp_dep = NULL;
1763 int copied = 0, allow_deprecated = 0;
1764 u_char *cp = (u_char *)(nni6 + 1);
1765 int niflags = ni6->ni_flags;
1768 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1769 return (0); /* needless to copy */
1774 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1775 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1776 ifa = ifa->ifa_list.tqe_next) {
1777 if (ifa->ifa_addr->sa_family != AF_INET6)
1779 ifa6 = (struct in6_ifaddr *)ifa;
1781 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1782 allow_deprecated == 0) {
1784 * prefererred address should be put before
1785 * deprecated addresses.
1788 /* record the interface for later search */
1789 if (ifp_dep == NULL)
1793 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1794 allow_deprecated != 0)
1795 continue; /* we now collect deprecated addrs */
1797 /* What do we have to do about ::1? */
1798 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1799 case IPV6_ADDR_SCOPE_LINKLOCAL:
1800 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1803 case IPV6_ADDR_SCOPE_SITELOCAL:
1804 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1807 case IPV6_ADDR_SCOPE_GLOBAL:
1808 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1816 * check if anycast is okay.
1817 * XXX: just experimental. not in the spec.
1819 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1820 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1822 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1823 (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1827 /* now we can copy the address */
1828 if (resid < sizeof(struct in6_addr) +
1829 sizeof(u_int32_t)) {
1831 * We give up much more copy.
1832 * Set the truncate flag and return.
1834 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1840 * Set the TTL of the address.
1841 * The TTL value should be one of the following
1842 * according to the specification:
1844 * 1. The remaining lifetime of a DHCP lease on the
1846 * 2. The remaining Valid Lifetime of a prefix from
1847 * which the address was derived through Stateless
1848 * Autoconfiguration.
1850 * Note that we currently do not support stateful
1851 * address configuration by DHCPv6, so the former
1852 * case can't happen.
1854 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1855 ltime = ND6_INFINITE_LIFETIME;
1857 if (ifa6->ia6_lifetime.ia6t_expire >
1859 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1864 bcopy(<ime, cp, sizeof(u_int32_t));
1865 cp += sizeof(u_int32_t);
1867 /* copy the address itself */
1868 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1869 sizeof(struct in6_addr));
1870 in6_clearscope((struct in6_addr *)cp); /* XXX */
1871 cp += sizeof(struct in6_addr);
1873 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1874 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1876 if (ifp0) /* we need search only on the specified IF */
1880 if (allow_deprecated == 0 && ifp_dep != NULL) {
1882 allow_deprecated = 1;
1893 * XXX almost dup'ed code with rip6_input.
1896 icmp6_rip6_input(struct mbuf **mp, int off)
1898 INIT_VNET_INET(curvnet);
1899 INIT_VNET_INET6(curvnet);
1900 struct mbuf *m = *mp;
1901 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1903 struct inpcb *last = NULL;
1904 struct sockaddr_in6 fromsa;
1905 struct icmp6_hdr *icmp6;
1906 struct mbuf *opts = NULL;
1908 #ifndef PULLDOWN_TEST
1909 /* this is assumed to be safe. */
1910 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1912 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1913 if (icmp6 == NULL) {
1914 /* m is already reclaimed */
1915 return (IPPROTO_DONE);
1920 * XXX: the address may have embedded scope zone ID, which should be
1921 * hidden from applications.
1923 bzero(&fromsa, sizeof(fromsa));
1924 fromsa.sin6_family = AF_INET6;
1925 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1926 fromsa.sin6_addr = ip6->ip6_src;
1927 if (sa6_recoverscope(&fromsa)) {
1929 return (IPPROTO_DONE);
1932 INP_INFO_RLOCK(&V_ripcbinfo);
1933 LIST_FOREACH(in6p, &V_ripcb, inp_list) {
1934 if ((in6p->inp_vflag & INP_IPV6) == 0)
1936 if (in6p->inp_ip_p != IPPROTO_ICMPV6)
1938 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1939 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1941 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1942 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1945 if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1946 in6p->in6p_icmp6filt)) {
1951 struct mbuf *n = NULL;
1954 * Recent network drivers tend to allocate a single
1955 * mbuf cluster, rather than to make a couple of
1956 * mbufs without clusters. Also, since the IPv6 code
1957 * path tries to avoid m_pullup(), it is highly
1958 * probable that we still have an mbuf cluster here
1959 * even though the necessary length can be stored in an
1960 * mbuf's internal buffer.
1961 * Meanwhile, the default size of the receive socket
1962 * buffer for raw sockets is not so large. This means
1963 * the possibility of packet loss is relatively higher
1964 * than before. To avoid this scenario, we copy the
1965 * received data to a separate mbuf that does not use
1966 * a cluster, if possible.
1967 * XXX: it is better to copy the data after stripping
1968 * intermediate headers.
1970 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1971 m->m_len <= MHLEN) {
1972 MGET(n, M_DONTWAIT, m->m_type);
1974 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1975 bcopy(m->m_data, n->m_data,
1977 n->m_len = m->m_len;
1985 (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1986 if (last->inp_flags & INP_CONTROLOPTS)
1987 ip6_savecontrol(last, n, &opts);
1988 /* strip intermediate headers */
1990 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
1991 if (sbappendaddr_locked(
1992 &last->inp_socket->so_rcv,
1993 (struct sockaddr *)&fromsa, n, opts)
1995 /* should notify about lost packet */
2001 &last->inp_socket->so_rcv);
2003 sorwakeup_locked(last->inp_socket);
2010 INP_INFO_RUNLOCK(&V_ripcbinfo);
2012 if (last->inp_flags & INP_CONTROLOPTS)
2013 ip6_savecontrol(last, m, &opts);
2014 /* strip intermediate headers */
2017 /* avoid using mbuf clusters if possible (see above) */
2018 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2019 m->m_len <= MHLEN) {
2022 MGET(n, M_DONTWAIT, m->m_type);
2024 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2025 bcopy(m->m_data, n->m_data, m->m_len);
2026 n->m_len = m->m_len;
2036 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2037 if (sbappendaddr_locked(&last->inp_socket->so_rcv,
2038 (struct sockaddr *)&fromsa, m, opts) == 0) {
2042 SOCKBUF_UNLOCK(&last->inp_socket->so_rcv);
2044 sorwakeup_locked(last->inp_socket);
2048 V_ip6stat.ip6s_delivered--;
2050 return IPPROTO_DONE;
2054 * Reflect the ip6 packet back to the source.
2055 * OFF points to the icmp6 header, counted from the top of the mbuf.
2058 icmp6_reflect(struct mbuf *m, size_t off)
2060 INIT_VNET_INET6(curvnet);
2061 struct ip6_hdr *ip6;
2062 struct icmp6_hdr *icmp6;
2063 struct in6_ifaddr *ia;
2066 struct ifnet *outif = NULL;
2067 struct in6_addr origdst, *src = NULL;
2069 /* too short to reflect */
2070 if (off < sizeof(struct ip6_hdr)) {
2072 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2073 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2074 __FILE__, __LINE__));
2079 * If there are extra headers between IPv6 and ICMPv6, strip
2080 * off that header first.
2083 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2084 panic("assumption failed in icmp6_reflect");
2086 if (off > sizeof(struct ip6_hdr)) {
2088 struct ip6_hdr nip6;
2090 l = off - sizeof(struct ip6_hdr);
2091 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2093 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2095 if ((m = m_pullup(m, l)) == NULL)
2098 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2099 } else /* off == sizeof(struct ip6_hdr) */ {
2101 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2103 if ((m = m_pullup(m, l)) == NULL)
2107 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2108 ip6 = mtod(m, struct ip6_hdr *);
2109 ip6->ip6_nxt = IPPROTO_ICMPV6;
2110 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2111 type = icmp6->icmp6_type; /* keep type for statistics */
2112 code = icmp6->icmp6_code; /* ditto. */
2114 origdst = ip6->ip6_dst;
2116 * ip6_input() drops a packet if its src is multicast.
2117 * So, the src is never multicast.
2119 ip6->ip6_dst = ip6->ip6_src;
2122 * If the incoming packet was addressed directly to us (i.e. unicast),
2123 * use dst as the src for the reply.
2124 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2125 * (for example) when we encounter an error while forwarding procedure
2126 * destined to a duplicated address of ours.
2127 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2128 * procedure of an outgoing packet of our own, in which case we need
2129 * to search in the ifaddr list.
2131 if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
2132 if ((ia = ip6_getdstifaddr(m))) {
2133 if (!(ia->ia6_flags &
2134 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2135 src = &ia->ia_addr.sin6_addr;
2137 struct sockaddr_in6 d;
2139 bzero(&d, sizeof(d));
2140 d.sin6_family = AF_INET6;
2141 d.sin6_len = sizeof(d);
2142 d.sin6_addr = origdst;
2143 ia = (struct in6_ifaddr *)
2144 ifa_ifwithaddr((struct sockaddr *)&d);
2147 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2148 src = &ia->ia_addr.sin6_addr;
2155 struct sockaddr_in6 sin6;
2156 struct route_in6 ro;
2159 * This case matches to multicasts, our anycast, or unicasts
2160 * that we do not own. Select a source address based on the
2161 * source address of the erroneous packet.
2163 bzero(&sin6, sizeof(sin6));
2164 sin6.sin6_family = AF_INET6;
2165 sin6.sin6_len = sizeof(sin6);
2166 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2168 bzero(&ro, sizeof(ro));
2169 src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e);
2171 RTFREE(ro.ro_rt); /* XXX: we could use this */
2173 char ip6buf[INET6_ADDRSTRLEN];
2175 "icmp6_reflect: source can't be determined: "
2176 "dst=%s, error=%d\n",
2177 ip6_sprintf(ip6buf, &sin6.sin6_addr), e));
2182 ip6->ip6_src = *src;
2184 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2185 ip6->ip6_vfc |= IPV6_VERSION;
2186 ip6->ip6_nxt = IPPROTO_ICMPV6;
2188 ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2189 else if (m->m_pkthdr.rcvif) {
2190 /* XXX: This may not be the outgoing interface */
2191 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2193 ip6->ip6_hlim = V_ip6_defhlim;
2195 icmp6->icmp6_cksum = 0;
2196 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2197 sizeof(struct ip6_hdr), plen);
2200 * XXX option handling
2203 m->m_flags &= ~(M_BCAST|M_MCAST);
2205 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2207 icmp6_ifoutstat_inc(outif, type, code);
2217 icmp6_fasttimo(void)
2224 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2225 struct in6_addr *tgt6)
2227 static char buf[1024];
2228 char ip6bufs[INET6_ADDRSTRLEN];
2229 char ip6bufd[INET6_ADDRSTRLEN];
2230 char ip6buft[INET6_ADDRSTRLEN];
2231 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2232 ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2233 ip6_sprintf(ip6buft, tgt6));
2238 icmp6_redirect_input(struct mbuf *m, int off)
2240 INIT_VNET_INET6(curvnet);
2242 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2243 struct nd_redirect *nd_rd;
2244 int icmp6len = ntohs(ip6->ip6_plen);
2245 char *lladdr = NULL;
2247 u_char *redirhdr = NULL;
2248 int redirhdrlen = 0;
2249 struct rtentry *rt = NULL;
2252 struct in6_addr src6 = ip6->ip6_src;
2253 struct in6_addr redtgt6;
2254 struct in6_addr reddst6;
2255 union nd_opts ndopts;
2256 char ip6buf[INET6_ADDRSTRLEN];
2261 ifp = m->m_pkthdr.rcvif;
2266 /* XXX if we are router, we don't update route by icmp6 redirect */
2267 if (V_ip6_forwarding)
2269 if (!V_icmp6_rediraccept)
2272 #ifndef PULLDOWN_TEST
2273 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2274 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2276 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2277 if (nd_rd == NULL) {
2278 V_icmp6stat.icp6s_tooshort++;
2282 redtgt6 = nd_rd->nd_rd_target;
2283 reddst6 = nd_rd->nd_rd_dst;
2285 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2286 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2291 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2293 "ICMP6 redirect sent from %s rejected; "
2294 "must be from linklocal\n",
2295 ip6_sprintf(ip6buf, &src6)));
2298 if (ip6->ip6_hlim != 255) {
2300 "ICMP6 redirect sent from %s rejected; "
2301 "hlim=%d (must be 255)\n",
2302 ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
2306 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2307 struct sockaddr_in6 sin6;
2308 struct in6_addr *gw6;
2310 bzero(&sin6, sizeof(sin6));
2311 sin6.sin6_family = AF_INET6;
2312 sin6.sin6_len = sizeof(struct sockaddr_in6);
2313 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2314 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
2316 if (rt->rt_gateway == NULL ||
2317 rt->rt_gateway->sa_family != AF_INET6) {
2319 "ICMP6 redirect rejected; no route "
2320 "with inet6 gateway found for redirect dst: %s\n",
2321 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2326 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2327 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2329 "ICMP6 redirect rejected; "
2330 "not equal to gw-for-src=%s (must be same): "
2332 ip6_sprintf(ip6buf, gw6),
2333 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2339 "ICMP6 redirect rejected; "
2340 "no route found for redirect dst: %s\n",
2341 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2347 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2349 "ICMP6 redirect rejected; "
2350 "redirect dst must be unicast: %s\n",
2351 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2355 is_router = is_onlink = 0;
2356 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2357 is_router = 1; /* router case */
2358 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2359 is_onlink = 1; /* on-link destination case */
2360 if (!is_router && !is_onlink) {
2362 "ICMP6 redirect rejected; "
2363 "neither router case nor onlink case: %s\n",
2364 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2367 /* validation passed */
2369 icmp6len -= sizeof(*nd_rd);
2370 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2371 if (nd6_options(&ndopts) < 0) {
2372 nd6log((LOG_INFO, "icmp6_redirect_input: "
2373 "invalid ND option, rejected: %s\n",
2374 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2375 /* nd6_options have incremented stats */
2379 if (ndopts.nd_opts_tgt_lladdr) {
2380 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2381 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2384 if (ndopts.nd_opts_rh) {
2385 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2386 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2389 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2391 "icmp6_redirect_input: lladdrlen mismatch for %s "
2392 "(if %d, icmp6 packet %d): %s\n",
2393 ip6_sprintf(ip6buf, &redtgt6),
2394 ifp->if_addrlen, lladdrlen - 2,
2395 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2400 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2401 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2403 if (!is_onlink) { /* better router case. perform rtredirect. */
2404 /* perform rtredirect */
2405 struct sockaddr_in6 sdst;
2406 struct sockaddr_in6 sgw;
2407 struct sockaddr_in6 ssrc;
2409 bzero(&sdst, sizeof(sdst));
2410 bzero(&sgw, sizeof(sgw));
2411 bzero(&ssrc, sizeof(ssrc));
2412 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2413 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2414 sizeof(struct sockaddr_in6);
2415 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2416 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2417 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2418 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2419 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2420 (struct sockaddr *)&ssrc);
2422 /* finally update cached route in each socket via pfctlinput */
2424 struct sockaddr_in6 sdst;
2426 bzero(&sdst, sizeof(sdst));
2427 sdst.sin6_family = AF_INET6;
2428 sdst.sin6_len = sizeof(struct sockaddr_in6);
2429 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2430 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2432 key_sa_routechange((struct sockaddr *)&sdst);
2441 V_icmp6stat.icp6s_badredirect++;
2446 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2448 INIT_VNET_INET6(curvnet);
2449 struct ifnet *ifp; /* my outgoing interface */
2450 struct in6_addr *ifp_ll6;
2451 struct in6_addr *router_ll6;
2452 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2453 struct mbuf *m = NULL; /* newly allocated one */
2454 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2455 struct nd_redirect *nd_rd;
2456 struct llentry *ln = NULL;
2459 struct ifnet *outif = NULL;
2460 struct sockaddr_in6 src_sa;
2462 icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2464 /* if we are not router, we don't send icmp6 redirect */
2465 if (!V_ip6_forwarding)
2469 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2474 * the source address must identify a neighbor, and
2475 * the destination address must not be a multicast address
2476 * [RFC 2461, sec 8.2]
2478 sip6 = mtod(m0, struct ip6_hdr *);
2479 bzero(&src_sa, sizeof(src_sa));
2480 src_sa.sin6_family = AF_INET6;
2481 src_sa.sin6_len = sizeof(src_sa);
2482 src_sa.sin6_addr = sip6->ip6_src;
2483 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2485 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2486 goto fail; /* what should we do here? */
2489 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2493 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2494 * we almost always ask for an mbuf cluster for simplicity.
2495 * (MHLEN < IPV6_MMTU is almost always true)
2497 #if IPV6_MMTU >= MCLBYTES
2498 # error assumption failed about IPV6_MMTU and MCLBYTES
2500 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2501 if (m && IPV6_MMTU >= MHLEN)
2502 MCLGET(m, M_DONTWAIT);
2505 m->m_pkthdr.rcvif = NULL;
2507 maxlen = M_TRAILINGSPACE(m);
2508 maxlen = min(IPV6_MMTU, maxlen);
2509 /* just for safety */
2510 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2511 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2516 /* get ip6 linklocal address for ifp(my outgoing interface). */
2517 struct in6_ifaddr *ia;
2518 if ((ia = in6ifa_ifpforlinklocal(ifp,
2520 IN6_IFF_ANYCAST)) == NULL)
2522 ifp_ll6 = &ia->ia_addr.sin6_addr;
2525 /* get ip6 linklocal address for the router. */
2526 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2527 struct sockaddr_in6 *sin6;
2528 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2529 router_ll6 = &sin6->sin6_addr;
2530 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2531 router_ll6 = (struct in6_addr *)NULL;
2533 router_ll6 = (struct in6_addr *)NULL;
2536 ip6 = mtod(m, struct ip6_hdr *);
2538 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2539 ip6->ip6_vfc |= IPV6_VERSION;
2540 /* ip6->ip6_plen will be set later */
2541 ip6->ip6_nxt = IPPROTO_ICMPV6;
2542 ip6->ip6_hlim = 255;
2543 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2544 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2545 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2548 nd_rd = (struct nd_redirect *)(ip6 + 1);
2549 nd_rd->nd_rd_type = ND_REDIRECT;
2550 nd_rd->nd_rd_code = 0;
2551 nd_rd->nd_rd_reserved = 0;
2552 if (rt->rt_flags & RTF_GATEWAY) {
2554 * nd_rd->nd_rd_target must be a link-local address in
2555 * better router cases.
2559 bcopy(router_ll6, &nd_rd->nd_rd_target,
2560 sizeof(nd_rd->nd_rd_target));
2561 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2562 sizeof(nd_rd->nd_rd_dst));
2564 /* make sure redtgt == reddst */
2565 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2566 sizeof(nd_rd->nd_rd_target));
2567 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2568 sizeof(nd_rd->nd_rd_dst));
2571 p = (u_char *)(nd_rd + 1);
2577 /* target lladdr option */
2579 struct nd_opt_hdr *nd_opt;
2582 IF_AFDATA_LOCK(ifp);
2583 ln = nd6_lookup(router_ll6, 0, ifp);
2584 IF_AFDATA_UNLOCK(ifp);
2588 len = sizeof(*nd_opt) + ifp->if_addrlen;
2589 len = (len + 7) & ~7; /* round by 8 */
2591 if (len + (p - (u_char *)ip6) > maxlen)
2594 if (ln->la_flags & LLE_VALID) {
2595 nd_opt = (struct nd_opt_hdr *)p;
2596 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2597 nd_opt->nd_opt_len = len >> 3;
2598 lladdr = (char *)(nd_opt + 1);
2599 bcopy(&ln->ll_addr, lladdr, ifp->if_addrlen);
2607 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2609 /* just to be safe */
2610 #ifdef M_DECRYPTED /*not openbsd*/
2611 if (m0->m_flags & M_DECRYPTED)
2614 if (p - (u_char *)ip6 > maxlen)
2618 /* redirected header option */
2620 struct nd_opt_rd_hdr *nd_opt_rh;
2623 * compute the maximum size for icmp6 redirect header option.
2624 * XXX room for auth header?
2626 len = maxlen - (p - (u_char *)ip6);
2629 /* This is just for simplicity. */
2630 if (m0->m_pkthdr.len != m0->m_len) {
2632 m_freem(m0->m_next);
2635 m0->m_pkthdr.len = m0->m_len;
2639 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2640 * about padding/truncate rule for the original IP packet.
2641 * From the discussion on IPv6imp in Feb 1999,
2642 * the consensus was:
2643 * - "attach as much as possible" is the goal
2644 * - pad if not aligned (original size can be guessed by
2645 * original ip6 header)
2646 * Following code adds the padding if it is simple enough,
2647 * and truncates if not.
2649 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2650 panic("assumption failed in %s:%d", __FILE__,
2653 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2654 /* not enough room, truncate */
2655 m0->m_pkthdr.len = m0->m_len = len -
2658 /* enough room, pad or truncate */
2661 extra = m0->m_pkthdr.len % 8;
2663 /* pad if easy enough, truncate if not */
2664 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2666 m0->m_len += (8 - extra);
2667 m0->m_pkthdr.len += (8 - extra);
2670 m0->m_pkthdr.len -= extra;
2674 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2675 m0->m_pkthdr.len = m0->m_len = len -
2679 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2680 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2681 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2682 nd_opt_rh->nd_opt_rh_len = len >> 3;
2683 p += sizeof(*nd_opt_rh);
2684 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2686 /* connect m0 to m */
2687 m_tag_delete_chain(m0, NULL);
2688 m0->m_flags &= ~M_PKTHDR;
2690 m->m_pkthdr.len = m->m_len + m0->m_len;
2699 /* XXX: clear embedded link IDs in the inner header */
2700 in6_clearscope(&sip6->ip6_src);
2701 in6_clearscope(&sip6->ip6_dst);
2702 in6_clearscope(&nd_rd->nd_rd_target);
2703 in6_clearscope(&nd_rd->nd_rd_dst);
2705 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2707 nd_rd->nd_rd_cksum = 0;
2708 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2709 sizeof(*ip6), ntohs(ip6->ip6_plen));
2711 /* send the packet to outside... */
2712 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2714 icmp6_ifstat_inc(outif, ifs6_out_msg);
2715 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2717 V_icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2729 * ICMPv6 socket option processing.
2732 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2736 struct inpcb *inp = sotoinpcb(so);
2737 int level, op, optname;
2740 level = sopt->sopt_level;
2741 op = sopt->sopt_dir;
2742 optname = sopt->sopt_name;
2743 optlen = sopt->sopt_valsize;
2745 level = op = optname = optlen = 0;
2747 if (level != IPPROTO_ICMPV6) {
2756 struct icmp6_filter ic6f;
2758 if (optlen != sizeof(ic6f)) {
2762 error = sooptcopyin(sopt, &ic6f, optlen, optlen);
2765 *inp->in6p_icmp6filt = ic6f;
2772 error = ENOPROTOOPT;
2781 struct icmp6_filter ic6f;
2784 ic6f = *inp->in6p_icmp6filt;
2786 error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
2791 error = ENOPROTOOPT;
2801 * Perform rate limit check.
2802 * Returns 0 if it is okay to send the icmp6 packet.
2803 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2806 * XXX per-destination/type check necessary?
2808 * dst - not used at this moment
2809 * type - not used at this moment
2810 * code - not used at this moment
2813 icmp6_ratelimit(const struct in6_addr *dst, const int type,
2816 INIT_VNET_INET6(curvnet);
2819 ret = 0; /* okay to send */
2822 if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count,
2823 V_icmp6errppslim)) {
2824 /* The packet is subject to rate limit */