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>
86 #include <net/if_dl.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/in_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet/icmp6.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet6/in6_ifattach.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet6/ip6protosw.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/scope6_var.h>
101 #include <netinet6/mld6_var.h>
102 #include <netinet6/nd6.h>
105 #include <netinet6/ipsec.h>
106 #include <netkey/key.h>
110 #include <netipsec/ipsec.h>
111 #include <netipsec/key.h>
114 #include <net/net_osdep.h>
116 extern struct domain inet6domain;
118 struct icmp6stat icmp6stat;
120 extern struct inpcbinfo ripcbinfo;
121 extern struct inpcbhead ripcb;
122 extern int icmp6errppslim;
123 static int icmp6errpps_count = 0;
124 static struct timeval icmp6errppslim_last;
125 extern int icmp6_nodeinfo;
127 static void icmp6_errcount(struct icmp6errstat *, int, int);
128 static int icmp6_rip6_input(struct mbuf **, int);
129 static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
130 static const char *icmp6_redirect_diag __P((struct in6_addr *,
131 struct in6_addr *, struct in6_addr *));
132 static struct mbuf *ni6_input(struct mbuf *, int);
133 static struct mbuf *ni6_nametodns(const char *, int, int);
134 static int ni6_dnsmatch(const char *, int, const char *, int);
135 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
136 struct ifnet **, struct in6_addr *));
137 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
138 struct ifnet *, int));
139 static int icmp6_notify_error(struct mbuf **, int, int, int);
149 icmp6_errcount(stat, type, code)
150 struct icmp6errstat *stat;
154 case ICMP6_DST_UNREACH:
156 case ICMP6_DST_UNREACH_NOROUTE:
157 stat->icp6errs_dst_unreach_noroute++;
159 case ICMP6_DST_UNREACH_ADMIN:
160 stat->icp6errs_dst_unreach_admin++;
162 case ICMP6_DST_UNREACH_BEYONDSCOPE:
163 stat->icp6errs_dst_unreach_beyondscope++;
165 case ICMP6_DST_UNREACH_ADDR:
166 stat->icp6errs_dst_unreach_addr++;
168 case ICMP6_DST_UNREACH_NOPORT:
169 stat->icp6errs_dst_unreach_noport++;
173 case ICMP6_PACKET_TOO_BIG:
174 stat->icp6errs_packet_too_big++;
176 case ICMP6_TIME_EXCEEDED:
178 case ICMP6_TIME_EXCEED_TRANSIT:
179 stat->icp6errs_time_exceed_transit++;
181 case ICMP6_TIME_EXCEED_REASSEMBLY:
182 stat->icp6errs_time_exceed_reassembly++;
186 case ICMP6_PARAM_PROB:
188 case ICMP6_PARAMPROB_HEADER:
189 stat->icp6errs_paramprob_header++;
191 case ICMP6_PARAMPROB_NEXTHEADER:
192 stat->icp6errs_paramprob_nextheader++;
194 case ICMP6_PARAMPROB_OPTION:
195 stat->icp6errs_paramprob_option++;
200 stat->icp6errs_redirect++;
203 stat->icp6errs_unknown++;
207 * A wrapper function for icmp6_error() necessary when the erroneous packet
208 * may not contain enough scope zone information.
211 icmp6_error2(m, type, code, param, ifp)
213 int type, code, param;
221 #ifndef PULLDOWN_TEST
222 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
224 if (m->m_len < sizeof(struct ip6_hdr)) {
225 m = m_pullup(m, sizeof(struct ip6_hdr));
231 ip6 = mtod(m, struct ip6_hdr *);
233 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
235 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
238 icmp6_error(m, type, code, param);
242 * Generate an error packet of type error in response to bad IP6 packet.
245 icmp6_error(m, type, code, param)
247 int type, code, param;
249 struct ip6_hdr *oip6, *nip6;
250 struct icmp6_hdr *icmp6;
255 icmp6stat.icp6s_error++;
257 /* count per-type-code statistics */
258 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
260 #ifdef M_DECRYPTED /*not openbsd*/
261 if (m->m_flags & M_DECRYPTED) {
262 icmp6stat.icp6s_canterror++;
267 #ifndef PULLDOWN_TEST
268 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
270 if (m->m_len < sizeof(struct ip6_hdr)) {
271 m = m_pullup(m, sizeof(struct ip6_hdr));
276 oip6 = mtod(m, struct ip6_hdr *);
279 * If the destination address of the erroneous packet is a multicast
280 * address, or the packet was sent using link-layer multicast,
281 * we should basically suppress sending an error (RFC 2463, Section
283 * We have two exceptions (the item e.2 in that section):
284 * - the Pakcet Too Big message can be sent for path MTU discovery.
285 * - the Parameter Problem Message that can be allowed an icmp6 error
286 * in the option type field. This check has been done in
287 * ip6_unknown_opt(), so we can just check the type and code.
289 if ((m->m_flags & (M_BCAST|M_MCAST) ||
290 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
291 (type != ICMP6_PACKET_TOO_BIG &&
292 (type != ICMP6_PARAM_PROB ||
293 code != ICMP6_PARAMPROB_OPTION)))
297 * RFC 2463, 2.4 (e.5): source address check.
298 * XXX: the case of anycast source?
300 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
301 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
305 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
309 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
310 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
311 struct icmp6_hdr *icp;
313 #ifndef PULLDOWN_TEST
314 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
315 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
317 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
320 icmp6stat.icp6s_tooshort++;
324 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
325 icp->icmp6_type == ND_REDIRECT) {
328 * Special case: for redirect (which is
329 * informational) we must not send icmp6 error.
331 icmp6stat.icp6s_canterror++;
334 /* ICMPv6 informational - send the error */
337 /* non-ICMPv6 - send the error */
340 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
342 /* Finally, do rate limitation check. */
343 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
344 icmp6stat.icp6s_toofreq++;
349 * OK, ICMP6 can be generated.
352 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
353 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
355 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
356 M_PREPEND(m, preplen, M_DONTWAIT);
357 if (m && m->m_len < preplen)
358 m = m_pullup(m, preplen);
360 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
364 nip6 = mtod(m, struct ip6_hdr *);
365 nip6->ip6_src = oip6->ip6_src;
366 nip6->ip6_dst = oip6->ip6_dst;
368 in6_clearscope(&oip6->ip6_src);
369 in6_clearscope(&oip6->ip6_dst);
371 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
372 icmp6->icmp6_type = type;
373 icmp6->icmp6_code = code;
374 icmp6->icmp6_pptr = htonl((u_int32_t)param);
377 * icmp6_reflect() is designed to be in the input path.
378 * icmp6_error() can be called from both input and output path,
379 * and if we are in output path rcvif could contain bogus value.
380 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
381 * information in ip header (nip6).
383 m->m_pkthdr.rcvif = NULL;
385 icmp6stat.icp6s_outhist[type]++;
386 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
392 * If we can't tell whether or not we can generate ICMP6, free it.
398 * Process a received ICMP6 message.
401 icmp6_input(mp, offp, proto)
405 struct mbuf *m = *mp, *n;
406 struct ip6_hdr *ip6, *nip6;
407 struct icmp6_hdr *icmp6, *nicmp6;
409 int icmp6len = m->m_pkthdr.len - *offp;
412 #ifndef PULLDOWN_TEST
413 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
414 /* m might change if M_LOOP. So, call mtod after this */
418 * Locate icmp6 structure in mbuf, and check
419 * that not corrupted and of at least minimum length
422 ip6 = mtod(m, struct ip6_hdr *);
423 if (icmp6len < sizeof(struct icmp6_hdr)) {
424 icmp6stat.icp6s_tooshort++;
429 * calculate the checksum
431 #ifndef PULLDOWN_TEST
432 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
434 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
436 icmp6stat.icp6s_tooshort++;
440 code = icmp6->icmp6_code;
442 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
444 "ICMP6 checksum error(%d|%x) %s\n",
445 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
446 icmp6stat.icp6s_checksum++;
450 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
452 * Deliver very specific ICMP6 type only.
453 * This is important to deliver TOOBIG. Otherwise PMTUD
456 switch (icmp6->icmp6_type) {
457 case ICMP6_DST_UNREACH:
458 case ICMP6_PACKET_TOO_BIG:
459 case ICMP6_TIME_EXCEEDED:
466 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
467 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
468 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
469 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
471 switch (icmp6->icmp6_type) {
472 case ICMP6_DST_UNREACH:
473 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
475 case ICMP6_DST_UNREACH_NOROUTE:
476 code = PRC_UNREACH_NET;
478 case ICMP6_DST_UNREACH_ADMIN:
479 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
480 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
482 case ICMP6_DST_UNREACH_ADDR:
485 case ICMP6_DST_UNREACH_BEYONDSCOPE:
486 /* I mean "source address was incorrect." */
487 code = PRC_PARAMPROB;
489 case ICMP6_DST_UNREACH_NOPORT:
490 code = PRC_UNREACH_PORT;
498 case ICMP6_PACKET_TOO_BIG:
499 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
501 /* validation is made in icmp6_mtudisc_update */
506 * Updating the path MTU will be done after examining
507 * intermediate extension headers.
512 case ICMP6_TIME_EXCEEDED:
513 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
515 case ICMP6_TIME_EXCEED_TRANSIT:
516 code = PRC_TIMXCEED_INTRANS;
518 case ICMP6_TIME_EXCEED_REASSEMBLY:
519 code = PRC_TIMXCEED_REASS;
527 case ICMP6_PARAM_PROB:
528 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
530 case ICMP6_PARAMPROB_NEXTHEADER:
531 code = PRC_UNREACH_PROTOCOL;
533 case ICMP6_PARAMPROB_HEADER:
534 case ICMP6_PARAMPROB_OPTION:
535 code = PRC_PARAMPROB;
543 case ICMP6_ECHO_REQUEST:
544 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
547 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
551 if ((n->m_flags & M_EXT) != 0
552 || n->m_len < off + sizeof(struct icmp6_hdr)) {
554 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
557 MGETHDR(n, M_DONTWAIT, n0->m_type);
558 n0len = n0->m_pkthdr.len; /* save for use below */
560 M_MOVE_PKTHDR(n, n0);
561 if (n && maxlen >= MHLEN) {
562 MCLGET(n, M_DONTWAIT);
563 if ((n->m_flags & M_EXT) == 0) {
574 * Copy IPv6 and ICMPv6 only.
576 nip6 = mtod(n, struct ip6_hdr *);
577 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
578 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
579 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
580 noff = sizeof(struct ip6_hdr);
581 /* new mbuf contains only ipv6+icmpv6 headers */
582 n->m_len = noff + sizeof(struct icmp6_hdr);
584 * Adjust mbuf. ip6_plen will be adjusted in
587 m_adj(n0, off + sizeof(struct icmp6_hdr));
588 /* recalculate complete packet size */
589 n->m_pkthdr.len = n0len + (noff - off);
592 nip6 = mtod(n, struct ip6_hdr *);
593 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
597 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
598 nicmp6->icmp6_code = 0;
600 icmp6stat.icp6s_reflect++;
601 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
602 icmp6_reflect(n, noff);
606 case ICMP6_ECHO_REPLY:
607 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
612 case MLD_LISTENER_QUERY:
613 case MLD_LISTENER_REPORT:
614 if (icmp6len < sizeof(struct mld_hdr))
616 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
617 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
619 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
620 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
630 case MLD_LISTENER_DONE:
631 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
632 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
634 break; /* nothing to be done in kernel */
636 case MLD_MTRACE_RESP:
638 /* XXX: these two are experimental. not officially defined. */
639 /* XXX: per-interface statistics? */
640 break; /* just pass it to applications */
642 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
644 enum { WRU, FQDN } mode;
649 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
651 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
656 #define hostnamelen strlen(hostname)
658 #ifndef PULLDOWN_TEST
659 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
662 n = m_copy(m, 0, M_COPYALL);
664 n = ni6_input(n, off);
665 /* XXX meaningless if n == NULL */
666 noff = sizeof(struct ip6_hdr);
672 * XXX: this combination of flags is pointless,
673 * but should we keep this for compatibility?
675 if ((icmp6_nodeinfo & 5) != 5)
680 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
681 if (maxlen >= MCLBYTES) {
685 MGETHDR(n, M_DONTWAIT, m->m_type);
686 if (n && maxlen > MHLEN) {
687 MCLGET(n, M_DONTWAIT);
688 if ((n->m_flags & M_EXT) == 0) {
693 if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) {
695 * Previous code did a blind M_COPY_PKTHDR
696 * and said "just for rcvif". If true, then
697 * we could tolerate the dup failing (due to
698 * the deep copy of the tag chain). For now
699 * be conservative and just fail.
708 n->m_pkthdr.rcvif = NULL;
710 maxhlen = M_TRAILINGSPACE(n) - maxlen;
711 if (maxhlen > hostnamelen)
712 maxhlen = hostnamelen;
714 * Copy IPv6 and ICMPv6 only.
716 nip6 = mtod(n, struct ip6_hdr *);
717 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
718 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
719 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
720 p = (u_char *)(nicmp6 + 1);
722 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
723 noff = sizeof(struct ip6_hdr);
724 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
725 sizeof(struct icmp6_hdr) + 4 + maxhlen;
726 nicmp6->icmp6_type = ICMP6_WRUREPLY;
727 nicmp6->icmp6_code = 0;
731 icmp6stat.icp6s_reflect++;
732 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
733 icmp6_reflect(n, noff);
743 case ND_ROUTER_SOLICIT:
744 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
747 if (icmp6len < sizeof(struct nd_router_solicit))
749 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
751 nd6_rs_input(m, off, icmp6len);
755 nd6_rs_input(n, off, icmp6len);
759 case ND_ROUTER_ADVERT:
760 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
763 if (icmp6len < sizeof(struct nd_router_advert))
765 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
767 nd6_ra_input(m, off, icmp6len);
771 nd6_ra_input(n, off, icmp6len);
775 case ND_NEIGHBOR_SOLICIT:
776 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
779 if (icmp6len < sizeof(struct nd_neighbor_solicit))
781 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
783 nd6_ns_input(m, off, icmp6len);
787 nd6_ns_input(n, off, icmp6len);
791 case ND_NEIGHBOR_ADVERT:
792 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
795 if (icmp6len < sizeof(struct nd_neighbor_advert))
797 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
799 nd6_na_input(m, off, icmp6len);
803 nd6_na_input(n, off, icmp6len);
808 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
811 if (icmp6len < sizeof(struct nd_redirect))
813 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
815 icmp6_redirect_input(m, off);
819 icmp6_redirect_input(n, off);
823 case ICMP6_ROUTER_RENUMBERING:
824 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
825 code != ICMP6_ROUTER_RENUMBERING_RESULT)
827 if (icmp6len < sizeof(struct icmp6_router_renum))
833 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
834 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
835 ip6_sprintf(&ip6->ip6_dst),
836 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
837 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
838 /* ICMPv6 error: MUST deliver it by spec... */
842 /* ICMPv6 informational: MUST not deliver */
846 if (icmp6_notify_error(&m, off, icmp6len, code)) {
847 /* In this case, m should've been freed. */
848 return (IPPROTO_DONE);
853 icmp6stat.icp6s_badcode++;
857 icmp6stat.icp6s_badlen++;
861 /* deliver the packet to appropriate sockets */
862 icmp6_rip6_input(&m, *offp);
872 icmp6_notify_error(mp, off, icmp6len, code)
874 int off, icmp6len, code;
876 struct mbuf *m = *mp;
877 struct icmp6_hdr *icmp6;
878 struct ip6_hdr *eip6;
880 struct sockaddr_in6 icmp6src, icmp6dst;
882 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
883 icmp6stat.icp6s_tooshort++;
886 #ifndef PULLDOWN_TEST
887 IP6_EXTHDR_CHECK(m, off,
888 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
889 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
891 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
892 sizeof(*icmp6) + sizeof(struct ip6_hdr));
894 icmp6stat.icp6s_tooshort++;
898 eip6 = (struct ip6_hdr *)(icmp6 + 1);
900 /* Detect the upper level protocol */
902 void (*ctlfunc)(int, struct sockaddr *, void *);
903 u_int8_t nxt = eip6->ip6_nxt;
904 int eoff = off + sizeof(struct icmp6_hdr) +
905 sizeof(struct ip6_hdr);
906 struct ip6ctlparam ip6cp;
907 struct in6_addr *finaldst = NULL;
908 int icmp6type = icmp6->icmp6_type;
910 struct ip6_rthdr *rth;
911 struct ip6_rthdr0 *rth0;
914 while (1) { /* XXX: should avoid infinite loop explicitly? */
918 case IPPROTO_HOPOPTS:
919 case IPPROTO_DSTOPTS:
921 #ifndef PULLDOWN_TEST
922 IP6_EXTHDR_CHECK(m, 0,
923 eoff + sizeof(struct ip6_ext), -1);
924 eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
926 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
929 icmp6stat.icp6s_tooshort++;
934 if (nxt == IPPROTO_AH)
935 eoff += (eh->ip6e_len + 2) << 2;
937 eoff += (eh->ip6e_len + 1) << 3;
940 case IPPROTO_ROUTING:
942 * When the erroneous packet contains a
943 * routing header, we should examine the
944 * header to determine the final destination.
945 * Otherwise, we can't properly update
946 * information that depends on the final
947 * destination (e.g. path MTU).
949 #ifndef PULLDOWN_TEST
950 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
951 rth = (struct ip6_rthdr *)
952 (mtod(m, caddr_t) + eoff);
954 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
957 icmp6stat.icp6s_tooshort++;
961 rthlen = (rth->ip6r_len + 1) << 3;
963 * XXX: currently there is no
964 * officially defined type other
966 * Note that if the segment left field
967 * is 0, all intermediate hops must
970 if (rth->ip6r_segleft &&
971 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
974 #ifndef PULLDOWN_TEST
975 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
976 rth0 = (struct ip6_rthdr0 *)
977 (mtod(m, caddr_t) + eoff);
980 struct ip6_rthdr0 *, m,
983 icmp6stat.icp6s_tooshort++;
987 /* just ignore a bogus header */
988 if ((rth0->ip6r0_len % 2) == 0 &&
989 (hops = rth0->ip6r0_len/2))
990 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
995 case IPPROTO_FRAGMENT:
996 #ifndef PULLDOWN_TEST
997 IP6_EXTHDR_CHECK(m, 0, eoff +
998 sizeof(struct ip6_frag), -1);
999 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1002 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1005 icmp6stat.icp6s_tooshort++;
1010 * Data after a fragment header is meaningless
1011 * unless it is the first fragment, but
1012 * we'll go to the notify label for path MTU
1015 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1018 eoff += sizeof(struct ip6_frag);
1023 * This case includes ESP and the No Next
1024 * Header. In such cases going to the notify
1025 * label does not have any meaning
1026 * (i.e. ctlfunc will be NULL), but we go
1027 * anyway since we might have to update
1028 * path MTU information.
1034 #ifndef PULLDOWN_TEST
1035 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1037 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1038 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1039 if (icmp6 == NULL) {
1040 icmp6stat.icp6s_tooshort++;
1046 * retrieve parameters from the inner IPv6 header, and convert
1047 * them into sockaddr structures.
1048 * XXX: there is no guarantee that the source or destination
1049 * addresses of the inner packet are in the same scope as
1050 * the addresses of the icmp packet. But there is no other
1051 * way to determine the zone.
1053 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1055 bzero(&icmp6dst, sizeof(icmp6dst));
1056 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1057 icmp6dst.sin6_family = AF_INET6;
1058 if (finaldst == NULL)
1059 icmp6dst.sin6_addr = eip6->ip6_dst;
1061 icmp6dst.sin6_addr = *finaldst;
1062 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1064 bzero(&icmp6src, sizeof(icmp6src));
1065 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1066 icmp6src.sin6_family = AF_INET6;
1067 icmp6src.sin6_addr = eip6->ip6_src;
1068 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1070 icmp6src.sin6_flowinfo =
1071 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1073 if (finaldst == NULL)
1074 finaldst = &eip6->ip6_dst;
1076 ip6cp.ip6c_icmp6 = icmp6;
1077 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1078 ip6cp.ip6c_off = eoff;
1079 ip6cp.ip6c_finaldst = finaldst;
1080 ip6cp.ip6c_src = &icmp6src;
1081 ip6cp.ip6c_nxt = nxt;
1083 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1084 notifymtu = ntohl(icmp6->icmp6_mtu);
1085 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1086 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1089 ctlfunc = (void (*)(int, struct sockaddr *, void *))
1090 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1092 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1105 icmp6_mtudisc_update(ip6cp, validated)
1106 struct ip6ctlparam *ip6cp;
1109 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1110 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1111 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1112 u_int mtu = ntohl(icmp6->icmp6_mtu);
1113 struct in_conninfo inc;
1117 * RFC2460 section 5, last paragraph.
1118 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1119 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1120 * due to packet translator in the middle.
1121 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1124 if (mtu < IPV6_MMTU)
1129 * we reject ICMPv6 too big with abnormally small value.
1130 * XXX what is the good definition of "abnormally small"?
1132 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1139 * In case the suggested mtu is less than IPV6_MMTU, we
1140 * only need to remember that it was for above mentioned
1141 * "alwaysfrag" case.
1142 * Try to be as close to the spec as possible.
1144 if (mtu < IPV6_MMTU)
1145 mtu = IPV6_MMTU - 8;
1147 bzero(&inc, sizeof(inc));
1148 inc.inc_flags = 1; /* IPv6 */
1149 inc.inc6_faddr = *dst;
1150 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1153 if (mtu < tcp_maxmtu6(&inc)) {
1154 tcp_hc_updatemtu(&inc, mtu);
1155 icmp6stat.icp6s_pmtuchg++;
1160 * Process a Node Information Query packet, based on
1161 * draft-ietf-ipngwg-icmp-name-lookups-07.
1163 * Spec incompatibilities:
1164 * - IPv6 Subject address handling
1165 * - IPv4 Subject address handling support missing
1166 * - Proxy reply (answer even if it's not for me)
1167 * - joins NI group address at in6_ifattach() time only, does not cope
1168 * with hostname changes by sethostname(3)
1170 #define hostnamelen strlen(hostname)
1171 static struct mbuf *
1176 struct icmp6_nodeinfo *ni6, *nni6;
1177 struct mbuf *n = NULL;
1180 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1181 struct ni_reply_fqdn *fqdn;
1182 int addrs; /* for NI_QTYPE_NODEADDR */
1183 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1184 struct in6_addr in6_subj; /* subject address */
1185 struct ip6_hdr *ip6;
1186 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1188 struct in6_ifaddr *ia6 = NULL;
1190 ip6 = mtod(m, struct ip6_hdr *);
1191 #ifndef PULLDOWN_TEST
1192 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1194 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1196 /* m is already reclaimed */
1202 * Validate IPv6 source address.
1203 * The default configuration MUST be to refuse answering queries from
1204 * global-scope addresses according to RFC4602.
1206 * - it's not very clear what "refuse" means; this implementation
1208 * - it's not very easy to identify global-scope (unicast) addresses
1209 * since there are many prefixes for them. It should be safer
1210 * and in practice sufficient to check "all" but loopback and
1211 * link-local (note that site-local unicast was deprecated and
1212 * ULA is defined as global scope-wise)
1214 if ((icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1215 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1216 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1220 * Validate IPv6 destination address.
1222 * The Responder must discard the Query without further processing
1223 * unless it is one of the Responder's unicast or anycast addresses, or
1224 * a link-local scope multicast address which the Responder has joined.
1225 * [RFC4602, Section 5.]
1227 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1228 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1230 /* else it's a link-local multicast, fine */
1231 } else { /* unicast or anycast */
1232 if ((ia6 = ip6_getdstifaddr(m)) == NULL)
1233 goto bad; /* XXX impossible */
1235 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1236 !(icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1237 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1238 "a temporary address in %s:%d",
1239 __FILE__, __LINE__));
1244 /* validate query Subject field. */
1245 qtype = ntohs(ni6->ni_qtype);
1246 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1249 case NI_QTYPE_SUPTYPES:
1251 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1255 case NI_QTYPE_NODEADDR:
1256 case NI_QTYPE_IPV4ADDR:
1257 switch (ni6->ni_code) {
1258 case ICMP6_NI_SUBJ_IPV6:
1259 #if ICMP6_NI_SUBJ_IPV6 != 0
1263 * backward compatibility - try to accept 03 draft
1264 * format, where no Subject is present.
1266 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1271 #if ICMP6_NI_SUBJ_IPV6 != 0
1272 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1276 if (subjlen != sizeof(struct in6_addr))
1280 * Validate Subject address.
1282 * Not sure what exactly "address belongs to the node"
1283 * means in the spec, is it just unicast, or what?
1285 * At this moment we consider Subject address as
1286 * "belong to the node" if the Subject address equals
1287 * to the IPv6 destination address; validation for
1288 * IPv6 destination address should have done enough
1291 * We do not do proxy at this moment.
1293 /* m_pulldown instead of copy? */
1294 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1295 subjlen, (caddr_t)&in6_subj);
1296 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1299 subj = (char *)&in6_subj;
1300 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1304 * XXX if we are to allow other cases, we should really
1305 * be careful about scope here.
1306 * basically, we should disallow queries toward IPv6
1307 * destination X with subject Y,
1308 * if scope(X) > scope(Y).
1309 * if we allow scope(X) > scope(Y), it will result in
1310 * information leakage across scope boundary.
1314 case ICMP6_NI_SUBJ_FQDN:
1316 * Validate Subject name with gethostname(3).
1318 * The behavior may need some debate, since:
1319 * - we are not sure if the node has FQDN as
1320 * hostname (returned by gethostname(3)).
1321 * - the code does wildcard match for truncated names.
1322 * however, we are not sure if we want to perform
1323 * wildcard match, if gethostname(3) side has
1324 * truncated hostname.
1326 n = ni6_nametodns(hostname, hostnamelen, 0);
1327 if (!n || n->m_next || n->m_len == 0)
1329 IP6_EXTHDR_GET(subj, char *, m,
1330 off + sizeof(struct icmp6_nodeinfo), subjlen);
1333 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1341 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1348 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1351 if ((icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1354 case NI_QTYPE_NODEADDR:
1355 case NI_QTYPE_IPV4ADDR:
1356 if ((icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1361 /* guess reply length */
1364 break; /* no reply data */
1365 case NI_QTYPE_SUPTYPES:
1366 replylen += sizeof(u_int32_t);
1369 /* XXX will append an mbuf */
1370 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1372 case NI_QTYPE_NODEADDR:
1373 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1374 if ((replylen += addrs * (sizeof(struct in6_addr) +
1375 sizeof(u_int32_t))) > MCLBYTES)
1376 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1378 case NI_QTYPE_IPV4ADDR:
1379 /* unsupported - should respond with unknown Qtype? */
1383 * XXX: We must return a reply with the ICMP6 code
1384 * `unknown Qtype' in this case. However we regard the case
1385 * as an FQDN query for backward compatibility.
1386 * Older versions set a random value to this field,
1387 * so it rarely varies in the defined qtypes.
1388 * But the mechanism is not reliable...
1389 * maybe we should obsolete older versions.
1391 qtype = NI_QTYPE_FQDN;
1392 /* XXX will append an mbuf */
1393 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1398 /* allocate an mbuf to reply. */
1399 MGETHDR(n, M_DONTWAIT, m->m_type);
1404 M_MOVE_PKTHDR(n, m); /* just for recvif */
1405 if (replylen > MHLEN) {
1406 if (replylen > MCLBYTES) {
1408 * XXX: should we try to allocate more? But MCLBYTES
1409 * is probably much larger than IPV6_MMTU...
1413 MCLGET(n, M_DONTWAIT);
1414 if ((n->m_flags & M_EXT) == 0) {
1418 n->m_pkthdr.len = n->m_len = replylen;
1420 /* copy mbuf header and IPv6 + Node Information base headers */
1421 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1422 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1423 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1425 /* qtype dependent procedure */
1428 nni6->ni_code = ICMP6_NI_SUCCESS;
1431 case NI_QTYPE_SUPTYPES:
1434 nni6->ni_code = ICMP6_NI_SUCCESS;
1435 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1436 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1437 v = (u_int32_t)htonl(0x0000000f);
1438 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1442 nni6->ni_code = ICMP6_NI_SUCCESS;
1443 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1444 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1445 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1446 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1448 * XXX do we really have FQDN in variable "hostname"?
1450 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1451 if (n->m_next == NULL)
1453 /* XXX we assume that n->m_next is not a chain */
1454 if (n->m_next->m_next != NULL)
1456 n->m_pkthdr.len += n->m_next->m_len;
1458 case NI_QTYPE_NODEADDR:
1462 nni6->ni_code = ICMP6_NI_SUCCESS;
1463 n->m_pkthdr.len = n->m_len =
1464 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1465 lenlim = M_TRAILINGSPACE(n);
1466 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1467 /* XXX: reset mbuf length */
1468 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1469 sizeof(struct icmp6_nodeinfo) + copied;
1473 break; /* XXX impossible! */
1476 nni6->ni_type = ICMP6_NI_REPLY;
1489 * make a mbuf with DNS-encoded string. no compression support.
1491 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1492 * treated as truncated name (two \0 at the end). this is a wild guess.
1494 static struct mbuf *
1495 ni6_nametodns(name, namelen, old)
1498 int old; /* return pascal string if non-zero */
1510 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1511 MGET(m, M_DONTWAIT, MT_DATA);
1512 if (m && len > MLEN) {
1513 MCLGET(m, M_DONTWAIT);
1514 if ((m->m_flags & M_EXT) == 0)
1523 *mtod(m, char *) = namelen;
1524 bcopy(name, mtod(m, char *) + 1, namelen);
1528 cp = mtod(m, char *);
1529 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1531 /* if not certain about my name, return empty buffer */
1536 * guess if it looks like shortened hostname, or FQDN.
1537 * shortened hostname needs two trailing "\0".
1540 for (p = name; p < name + namelen; p++) {
1541 if (*p && *p == '.')
1550 while (cp < ep && p < name + namelen) {
1552 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1554 /* result does not fit into mbuf */
1555 if (cp + i + 1 >= ep)
1558 * DNS label length restriction, RFC1035 page 8.
1559 * "i == 0" case is included here to avoid returning
1560 * 0-length label on "foo..bar".
1562 if (i <= 0 || i >= 64)
1568 if (p < name + namelen && *p == '.')
1572 if (cp + nterm >= ep)
1576 m->m_len = cp - mtod(m, char *);
1580 panic("should not reach here");
1590 * check if two DNS-encoded string matches. takes care of truncated
1591 * form (with \0\0 at the end). no compression support.
1592 * XXX upper/lowercase match (see RFC2065)
1595 ni6_dnsmatch(a, alen, b, blen)
1601 const char *a0, *b0;
1604 /* simplest case - need validation? */
1605 if (alen == blen && bcmp(a, b, alen) == 0)
1611 /* termination is mandatory */
1612 if (alen < 2 || blen < 2)
1614 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1619 while (a - a0 < alen && b - b0 < blen) {
1620 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1623 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1625 /* we don't support compression yet */
1626 if (a[0] >= 64 || b[0] >= 64)
1629 /* truncated case */
1630 if (a[0] == 0 && a - a0 == alen - 1)
1632 if (b[0] == 0 && b - b0 == blen - 1)
1634 if (a[0] == 0 || b[0] == 0)
1640 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1642 if (bcmp(a + 1, b + 1, l) != 0)
1649 if (a - a0 == alen && b - b0 == blen)
1656 * calculate the number of addresses to be returned in the node info reply.
1659 ni6_addrs(ni6, m, ifpp, subj)
1660 struct icmp6_nodeinfo *ni6;
1662 struct ifnet **ifpp;
1663 struct in6_addr *subj;
1666 struct in6_ifaddr *ifa6;
1668 int addrs = 0, addrsofif, iffound = 0;
1669 int niflags = ni6->ni_flags;
1671 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1672 switch (ni6->ni_code) {
1673 case ICMP6_NI_SUBJ_IPV6:
1674 if (subj == NULL) /* must be impossible... */
1679 * XXX: we only support IPv6 subject address for
1687 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1689 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1690 if (ifa->ifa_addr->sa_family != AF_INET6)
1692 ifa6 = (struct in6_ifaddr *)ifa;
1694 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1695 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1699 * IPv4-mapped addresses can only be returned by a
1700 * Node Information proxy, since they represent
1701 * addresses of IPv4-only nodes, which perforce do
1702 * not implement this protocol.
1703 * [icmp-name-lookups-07, Section 5.4]
1704 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1705 * this function at this moment.
1708 /* What do we have to do about ::1? */
1709 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1710 case IPV6_ADDR_SCOPE_LINKLOCAL:
1711 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1714 case IPV6_ADDR_SCOPE_SITELOCAL:
1715 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1718 case IPV6_ADDR_SCOPE_GLOBAL:
1719 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1727 * check if anycast is okay.
1728 * XXX: just experimental. not in the spec.
1730 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1731 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1732 continue; /* we need only unicast addresses */
1733 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1734 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1737 addrsofif++; /* count the address */
1753 ni6_store_addrs(ni6, nni6, ifp0, resid)
1754 struct icmp6_nodeinfo *ni6, *nni6;
1758 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1759 struct in6_ifaddr *ifa6;
1761 struct ifnet *ifp_dep = NULL;
1762 int copied = 0, allow_deprecated = 0;
1763 u_char *cp = (u_char *)(nni6 + 1);
1764 int niflags = ni6->ni_flags;
1767 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1768 return (0); /* needless to copy */
1773 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1774 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1775 ifa = ifa->ifa_list.tqe_next) {
1776 if (ifa->ifa_addr->sa_family != AF_INET6)
1778 ifa6 = (struct in6_ifaddr *)ifa;
1780 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1781 allow_deprecated == 0) {
1783 * prefererred address should be put before
1784 * deprecated addresses.
1787 /* record the interface for later search */
1788 if (ifp_dep == NULL)
1792 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1793 allow_deprecated != 0)
1794 continue; /* we now collect deprecated addrs */
1796 /* What do we have to do about ::1? */
1797 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1798 case IPV6_ADDR_SCOPE_LINKLOCAL:
1799 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1802 case IPV6_ADDR_SCOPE_SITELOCAL:
1803 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1806 case IPV6_ADDR_SCOPE_GLOBAL:
1807 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1815 * check if anycast is okay.
1816 * XXX: just experimental. not in the spec.
1818 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1819 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1821 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1822 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1826 /* now we can copy the address */
1827 if (resid < sizeof(struct in6_addr) +
1828 sizeof(u_int32_t)) {
1830 * We give up much more copy.
1831 * Set the truncate flag and return.
1833 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1839 * Set the TTL of the address.
1840 * The TTL value should be one of the following
1841 * according to the specification:
1843 * 1. The remaining lifetime of a DHCP lease on the
1845 * 2. The remaining Valid Lifetime of a prefix from
1846 * which the address was derived through Stateless
1847 * Autoconfiguration.
1849 * Note that we currently do not support stateful
1850 * address configuration by DHCPv6, so the former
1851 * case can't happen.
1853 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1854 ltime = ND6_INFINITE_LIFETIME;
1856 if (ifa6->ia6_lifetime.ia6t_expire >
1858 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1863 bcopy(<ime, cp, sizeof(u_int32_t));
1864 cp += sizeof(u_int32_t);
1866 /* copy the address itself */
1867 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1868 sizeof(struct in6_addr));
1869 in6_clearscope((struct in6_addr *)cp); /* XXX */
1870 cp += sizeof(struct in6_addr);
1872 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1873 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1875 if (ifp0) /* we need search only on the specified IF */
1879 if (allow_deprecated == 0 && ifp_dep != NULL) {
1881 allow_deprecated = 1;
1892 * XXX almost dup'ed code with rip6_input.
1895 icmp6_rip6_input(mp, off)
1899 struct mbuf *m = *mp;
1900 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1901 struct in6pcb *in6p;
1902 struct in6pcb *last = NULL;
1903 struct sockaddr_in6 fromsa;
1904 struct icmp6_hdr *icmp6;
1905 struct mbuf *opts = NULL;
1907 #ifndef PULLDOWN_TEST
1908 /* this is assumed to be safe. */
1909 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1911 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1912 if (icmp6 == NULL) {
1913 /* m is already reclaimed */
1914 return (IPPROTO_DONE);
1919 * XXX: the address may have embedded scope zone ID, which should be
1920 * hidden from applications.
1922 bzero(&fromsa, sizeof(fromsa));
1923 fromsa.sin6_family = AF_INET6;
1924 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1925 fromsa.sin6_addr = ip6->ip6_src;
1926 if (sa6_recoverscope(&fromsa)) {
1928 return (IPPROTO_DONE);
1931 INP_INFO_RLOCK(&ripcbinfo);
1932 LIST_FOREACH(in6p, &ripcb, inp_list) {
1934 if ((in6p->inp_vflag & INP_IPV6) == 0) {
1939 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1941 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1942 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1944 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1945 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1947 if (in6p->in6p_icmp6filt
1948 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1949 in6p->in6p_icmp6filt))
1952 struct mbuf *n = NULL;
1955 * Recent network drivers tend to allocate a single
1956 * mbuf cluster, rather than to make a couple of
1957 * mbufs without clusters. Also, since the IPv6 code
1958 * path tries to avoid m_pullup(), it is highly
1959 * probable that we still have an mbuf cluster here
1960 * even though the necessary length can be stored in an
1961 * mbuf's internal buffer.
1962 * Meanwhile, the default size of the receive socket
1963 * buffer for raw sockets is not so large. This means
1964 * the possibility of packet loss is relatively higher
1965 * than before. To avoid this scenario, we copy the
1966 * received data to a separate mbuf that does not use
1967 * a cluster, if possible.
1968 * XXX: it is better to copy the data after stripping
1969 * intermediate headers.
1971 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1972 m->m_len <= MHLEN) {
1973 MGET(n, M_DONTWAIT, m->m_type);
1975 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1976 bcopy(m->m_data, n->m_data,
1978 n->m_len = m->m_len;
1986 (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1987 if (last->in6p_flags & IN6P_CONTROLOPTS)
1988 ip6_savecontrol(last, n, &opts);
1989 /* strip intermediate headers */
1991 if (sbappendaddr(&last->in6p_socket->so_rcv,
1992 (struct sockaddr *)&fromsa, n, opts)
1994 /* should notify about lost packet */
2000 sorwakeup(last->in6p_socket);
2008 if (last->in6p_flags & IN6P_CONTROLOPTS)
2009 ip6_savecontrol(last, m, &opts);
2010 /* strip intermediate headers */
2013 /* avoid using mbuf clusters if possible (see above) */
2014 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2015 m->m_len <= MHLEN) {
2018 MGET(n, M_DONTWAIT, m->m_type);
2020 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2021 bcopy(m->m_data, n->m_data, m->m_len);
2022 n->m_len = m->m_len;
2032 if (sbappendaddr(&last->in6p_socket->so_rcv,
2033 (struct sockaddr *)&fromsa, m, opts) == 0) {
2038 sorwakeup(last->in6p_socket);
2042 ip6stat.ip6s_delivered--;
2044 INP_INFO_RUNLOCK(&ripcbinfo);
2045 return IPPROTO_DONE;
2049 * Reflect the ip6 packet back to the source.
2050 * OFF points to the icmp6 header, counted from the top of the mbuf.
2053 icmp6_reflect(m, off)
2057 struct ip6_hdr *ip6;
2058 struct icmp6_hdr *icmp6;
2059 struct in6_ifaddr *ia;
2062 struct ifnet *outif = NULL;
2063 struct in6_addr origdst, *src = NULL;
2065 /* too short to reflect */
2066 if (off < sizeof(struct ip6_hdr)) {
2068 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2069 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2070 __FILE__, __LINE__));
2075 * If there are extra headers between IPv6 and ICMPv6, strip
2076 * off that header first.
2079 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2080 panic("assumption failed in icmp6_reflect");
2082 if (off > sizeof(struct ip6_hdr)) {
2084 struct ip6_hdr nip6;
2086 l = off - sizeof(struct ip6_hdr);
2087 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2089 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2091 if ((m = m_pullup(m, l)) == NULL)
2094 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2095 } else /* off == sizeof(struct ip6_hdr) */ {
2097 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2099 if ((m = m_pullup(m, l)) == NULL)
2103 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2104 ip6 = mtod(m, struct ip6_hdr *);
2105 ip6->ip6_nxt = IPPROTO_ICMPV6;
2106 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2107 type = icmp6->icmp6_type; /* keep type for statistics */
2108 code = icmp6->icmp6_code; /* ditto. */
2110 origdst = ip6->ip6_dst;
2112 * ip6_input() drops a packet if its src is multicast.
2113 * So, the src is never multicast.
2115 ip6->ip6_dst = ip6->ip6_src;
2118 * If the incoming packet was addressed directly to us (i.e. unicast),
2119 * use dst as the src for the reply.
2120 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2121 * (for example) when we encounter an error while forwarding procedure
2122 * destined to a duplicated address of ours.
2123 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2124 * procedure of an outgoing packet of our own, in which case we need
2125 * to search in the ifaddr list.
2127 if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
2128 if ((ia = ip6_getdstifaddr(m))) {
2129 if (!(ia->ia6_flags &
2130 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2131 src = &ia->ia_addr.sin6_addr;
2133 struct sockaddr_in6 d;
2135 bzero(&d, sizeof(d));
2136 d.sin6_family = AF_INET6;
2137 d.sin6_len = sizeof(d);
2138 d.sin6_addr = origdst;
2139 ia = (struct in6_ifaddr *)
2140 ifa_ifwithaddr((struct sockaddr *)&d);
2143 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2144 src = &ia->ia_addr.sin6_addr;
2151 struct sockaddr_in6 sin6;
2152 struct route_in6 ro;
2155 * This case matches to multicasts, our anycast, or unicasts
2156 * that we do not own. Select a source address based on the
2157 * source address of the erroneous packet.
2159 bzero(&sin6, sizeof(sin6));
2160 sin6.sin6_family = AF_INET6;
2161 sin6.sin6_len = sizeof(sin6);
2162 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2164 bzero(&ro, sizeof(ro));
2165 src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e);
2167 RTFREE(ro.ro_rt); /* XXX: we could use this */
2170 "icmp6_reflect: source can't be determined: "
2171 "dst=%s, error=%d\n",
2172 ip6_sprintf(&sin6.sin6_addr), e));
2177 ip6->ip6_src = *src;
2179 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2180 ip6->ip6_vfc |= IPV6_VERSION;
2181 ip6->ip6_nxt = IPPROTO_ICMPV6;
2183 ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2184 else if (m->m_pkthdr.rcvif) {
2185 /* XXX: This may not be the outgoing interface */
2186 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2188 ip6->ip6_hlim = ip6_defhlim;
2190 icmp6->icmp6_cksum = 0;
2191 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2192 sizeof(struct ip6_hdr), plen);
2195 * XXX option handling
2198 m->m_flags &= ~(M_BCAST|M_MCAST);
2200 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2202 icmp6_ifoutstat_inc(outif, type, code);
2219 icmp6_redirect_diag(src6, dst6, tgt6)
2220 struct in6_addr *src6;
2221 struct in6_addr *dst6;
2222 struct in6_addr *tgt6;
2224 static char buf[1024];
2225 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2226 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2231 icmp6_redirect_input(m, off)
2235 struct ifnet *ifp = m->m_pkthdr.rcvif;
2236 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2237 struct nd_redirect *nd_rd;
2238 int icmp6len = ntohs(ip6->ip6_plen);
2239 char *lladdr = NULL;
2241 u_char *redirhdr = NULL;
2242 int redirhdrlen = 0;
2243 struct rtentry *rt = NULL;
2246 struct in6_addr src6 = ip6->ip6_src;
2247 struct in6_addr redtgt6;
2248 struct in6_addr reddst6;
2249 union nd_opts ndopts;
2254 /* XXX if we are router, we don't update route by icmp6 redirect */
2257 if (!icmp6_rediraccept)
2260 #ifndef PULLDOWN_TEST
2261 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2262 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2264 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2265 if (nd_rd == NULL) {
2266 icmp6stat.icp6s_tooshort++;
2270 redtgt6 = nd_rd->nd_rd_target;
2271 reddst6 = nd_rd->nd_rd_dst;
2273 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2274 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2279 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2281 "ICMP6 redirect sent from %s rejected; "
2282 "must be from linklocal\n",
2283 ip6_sprintf(&src6)));
2286 if (ip6->ip6_hlim != 255) {
2288 "ICMP6 redirect sent from %s rejected; "
2289 "hlim=%d (must be 255)\n",
2290 ip6_sprintf(&src6), ip6->ip6_hlim));
2294 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2295 struct sockaddr_in6 sin6;
2296 struct in6_addr *gw6;
2298 bzero(&sin6, sizeof(sin6));
2299 sin6.sin6_family = AF_INET6;
2300 sin6.sin6_len = sizeof(struct sockaddr_in6);
2301 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2302 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
2304 if (rt->rt_gateway == NULL ||
2305 rt->rt_gateway->sa_family != AF_INET6) {
2307 "ICMP6 redirect rejected; no route "
2308 "with inet6 gateway found for redirect dst: %s\n",
2309 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2314 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2315 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2317 "ICMP6 redirect rejected; "
2318 "not equal to gw-for-src=%s (must be same): "
2321 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2327 "ICMP6 redirect rejected; "
2328 "no route found for redirect dst: %s\n",
2329 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2335 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2337 "ICMP6 redirect rejected; "
2338 "redirect dst must be unicast: %s\n",
2339 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2343 is_router = is_onlink = 0;
2344 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2345 is_router = 1; /* router case */
2346 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2347 is_onlink = 1; /* on-link destination case */
2348 if (!is_router && !is_onlink) {
2350 "ICMP6 redirect rejected; "
2351 "neither router case nor onlink case: %s\n",
2352 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2355 /* validation passed */
2357 icmp6len -= sizeof(*nd_rd);
2358 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2359 if (nd6_options(&ndopts) < 0) {
2360 nd6log((LOG_INFO, "icmp6_redirect_input: "
2361 "invalid ND option, rejected: %s\n",
2362 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2363 /* nd6_options have incremented stats */
2367 if (ndopts.nd_opts_tgt_lladdr) {
2368 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2369 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2372 if (ndopts.nd_opts_rh) {
2373 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2374 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2377 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2379 "icmp6_redirect_input: lladdrlen mismatch for %s "
2380 "(if %d, icmp6 packet %d): %s\n",
2381 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2382 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2387 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2388 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2390 if (!is_onlink) { /* better router case. perform rtredirect. */
2391 /* perform rtredirect */
2392 struct sockaddr_in6 sdst;
2393 struct sockaddr_in6 sgw;
2394 struct sockaddr_in6 ssrc;
2396 bzero(&sdst, sizeof(sdst));
2397 bzero(&sgw, sizeof(sgw));
2398 bzero(&ssrc, sizeof(ssrc));
2399 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2400 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2401 sizeof(struct sockaddr_in6);
2402 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2403 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2404 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2405 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2406 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2407 (struct sockaddr *)&ssrc);
2409 /* finally update cached route in each socket via pfctlinput */
2411 struct sockaddr_in6 sdst;
2413 bzero(&sdst, sizeof(sdst));
2414 sdst.sin6_family = AF_INET6;
2415 sdst.sin6_len = sizeof(struct sockaddr_in6);
2416 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2417 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2418 #if defined(IPSEC) || defined(FAST_IPSEC)
2419 key_sa_routechange((struct sockaddr *)&sdst);
2428 icmp6stat.icp6s_badredirect++;
2433 icmp6_redirect_output(m0, rt)
2437 struct ifnet *ifp; /* my outgoing interface */
2438 struct in6_addr *ifp_ll6;
2439 struct in6_addr *router_ll6;
2440 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2441 struct mbuf *m = NULL; /* newly allocated one */
2442 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2443 struct nd_redirect *nd_rd;
2446 struct ifnet *outif = NULL;
2447 struct sockaddr_in6 src_sa;
2449 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2451 /* if we are not router, we don't send icmp6 redirect */
2452 if (!ip6_forwarding)
2456 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2461 * the source address must identify a neighbor, and
2462 * the destination address must not be a multicast address
2463 * [RFC 2461, sec 8.2]
2465 sip6 = mtod(m0, struct ip6_hdr *);
2466 bzero(&src_sa, sizeof(src_sa));
2467 src_sa.sin6_family = AF_INET6;
2468 src_sa.sin6_len = sizeof(src_sa);
2469 src_sa.sin6_addr = sip6->ip6_src;
2470 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2472 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2473 goto fail; /* what should we do here? */
2476 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2480 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2481 * we almost always ask for an mbuf cluster for simplicity.
2482 * (MHLEN < IPV6_MMTU is almost always true)
2484 #if IPV6_MMTU >= MCLBYTES
2485 # error assumption failed about IPV6_MMTU and MCLBYTES
2487 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2488 if (m && IPV6_MMTU >= MHLEN)
2489 MCLGET(m, M_DONTWAIT);
2492 m->m_pkthdr.rcvif = NULL;
2494 maxlen = M_TRAILINGSPACE(m);
2495 maxlen = min(IPV6_MMTU, maxlen);
2496 /* just for safety */
2497 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2498 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2503 /* get ip6 linklocal address for ifp(my outgoing interface). */
2504 struct in6_ifaddr *ia;
2505 if ((ia = in6ifa_ifpforlinklocal(ifp,
2507 IN6_IFF_ANYCAST)) == NULL)
2509 ifp_ll6 = &ia->ia_addr.sin6_addr;
2512 /* get ip6 linklocal address for the router. */
2513 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2514 struct sockaddr_in6 *sin6;
2515 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2516 router_ll6 = &sin6->sin6_addr;
2517 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2518 router_ll6 = (struct in6_addr *)NULL;
2520 router_ll6 = (struct in6_addr *)NULL;
2523 ip6 = mtod(m, struct ip6_hdr *);
2525 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2526 ip6->ip6_vfc |= IPV6_VERSION;
2527 /* ip6->ip6_plen will be set later */
2528 ip6->ip6_nxt = IPPROTO_ICMPV6;
2529 ip6->ip6_hlim = 255;
2530 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2531 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2532 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2535 nd_rd = (struct nd_redirect *)(ip6 + 1);
2536 nd_rd->nd_rd_type = ND_REDIRECT;
2537 nd_rd->nd_rd_code = 0;
2538 nd_rd->nd_rd_reserved = 0;
2539 if (rt->rt_flags & RTF_GATEWAY) {
2541 * nd_rd->nd_rd_target must be a link-local address in
2542 * better router cases.
2546 bcopy(router_ll6, &nd_rd->nd_rd_target,
2547 sizeof(nd_rd->nd_rd_target));
2548 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2549 sizeof(nd_rd->nd_rd_dst));
2551 /* make sure redtgt == reddst */
2552 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2553 sizeof(nd_rd->nd_rd_target));
2554 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2555 sizeof(nd_rd->nd_rd_dst));
2558 p = (u_char *)(nd_rd + 1);
2564 /* target lladdr option */
2565 struct rtentry *rt_router = NULL;
2567 struct sockaddr_dl *sdl;
2568 struct nd_opt_hdr *nd_opt;
2571 rt_router = nd6_lookup(router_ll6, 0, ifp);
2574 len = sizeof(*nd_opt) + ifp->if_addrlen;
2575 len = (len + 7) & ~7; /* round by 8 */
2577 if (len + (p - (u_char *)ip6) > maxlen)
2579 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2580 (rt_router->rt_flags & RTF_LLINFO) &&
2581 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2582 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2584 nd_opt = (struct nd_opt_hdr *)p;
2585 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2586 nd_opt->nd_opt_len = len >> 3;
2587 lladdr = (char *)(nd_opt + 1);
2588 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2594 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2596 /* just to be safe */
2597 #ifdef M_DECRYPTED /*not openbsd*/
2598 if (m0->m_flags & M_DECRYPTED)
2601 if (p - (u_char *)ip6 > maxlen)
2605 /* redirected header option */
2607 struct nd_opt_rd_hdr *nd_opt_rh;
2610 * compute the maximum size for icmp6 redirect header option.
2611 * XXX room for auth header?
2613 len = maxlen - (p - (u_char *)ip6);
2616 /* This is just for simplicity. */
2617 if (m0->m_pkthdr.len != m0->m_len) {
2619 m_freem(m0->m_next);
2622 m0->m_pkthdr.len = m0->m_len;
2626 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2627 * about padding/truncate rule for the original IP packet.
2628 * From the discussion on IPv6imp in Feb 1999,
2629 * the consensus was:
2630 * - "attach as much as possible" is the goal
2631 * - pad if not aligned (original size can be guessed by
2632 * original ip6 header)
2633 * Following code adds the padding if it is simple enough,
2634 * and truncates if not.
2636 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2637 panic("assumption failed in %s:%d", __FILE__,
2640 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2641 /* not enough room, truncate */
2642 m0->m_pkthdr.len = m0->m_len = len -
2645 /* enough room, pad or truncate */
2648 extra = m0->m_pkthdr.len % 8;
2650 /* pad if easy enough, truncate if not */
2651 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2653 m0->m_len += (8 - extra);
2654 m0->m_pkthdr.len += (8 - extra);
2657 m0->m_pkthdr.len -= extra;
2661 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2662 m0->m_pkthdr.len = m0->m_len = len -
2666 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2667 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2668 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2669 nd_opt_rh->nd_opt_rh_len = len >> 3;
2670 p += sizeof(*nd_opt_rh);
2671 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2673 /* connect m0 to m */
2674 m_tag_delete_chain(m0, NULL);
2675 m0->m_flags &= ~M_PKTHDR;
2677 m->m_pkthdr.len = m->m_len + m0->m_len;
2686 /* XXX: clear embedded link IDs in the inner header */
2687 in6_clearscope(&sip6->ip6_src);
2688 in6_clearscope(&sip6->ip6_dst);
2689 in6_clearscope(&nd_rd->nd_rd_target);
2690 in6_clearscope(&nd_rd->nd_rd_dst);
2692 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2694 nd_rd->nd_rd_cksum = 0;
2695 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2696 sizeof(*ip6), ntohs(ip6->ip6_plen));
2698 /* send the packet to outside... */
2699 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2701 icmp6_ifstat_inc(outif, ifs6_out_msg);
2702 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2704 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2716 * ICMPv6 socket option processing.
2719 icmp6_ctloutput(so, sopt)
2721 struct sockopt *sopt;
2725 struct inpcb *inp = sotoinpcb(so);
2726 int level, op, optname;
2729 level = sopt->sopt_level;
2730 op = sopt->sopt_dir;
2731 optname = sopt->sopt_name;
2732 optlen = sopt->sopt_valsize;
2734 level = op = optname = optlen = 0;
2736 if (level != IPPROTO_ICMPV6) {
2745 struct icmp6_filter *p;
2747 if (optlen != sizeof(*p)) {
2751 if (inp->in6p_icmp6filt == NULL) {
2755 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2761 error = ENOPROTOOPT;
2770 if (inp->in6p_icmp6filt == NULL) {
2774 error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2775 sizeof(struct icmp6_filter));
2780 error = ENOPROTOOPT;
2790 * Perform rate limit check.
2791 * Returns 0 if it is okay to send the icmp6 packet.
2792 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2795 * XXX per-destination/type check necessary?
2798 icmp6_ratelimit(dst, type, code)
2799 const struct in6_addr *dst; /* not used at this moment */
2800 const int type; /* not used at this moment */
2801 const int code; /* not used at this moment */
2805 ret = 0; /* okay to send */
2808 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2810 /* The packet is subject to rate limit */