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 <netipsec/ipsec.h>
106 #include <netipsec/key.h>
109 extern struct domain inet6domain;
111 struct icmp6stat icmp6stat;
113 extern struct inpcbinfo ripcbinfo;
114 extern struct inpcbhead ripcb;
115 extern int icmp6errppslim;
116 static int icmp6errpps_count = 0;
117 static struct timeval icmp6errppslim_last;
118 extern int icmp6_nodeinfo;
120 static void icmp6_errcount(struct icmp6errstat *, int, int);
121 static int icmp6_rip6_input(struct mbuf **, int);
122 static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
123 static const char *icmp6_redirect_diag __P((struct in6_addr *,
124 struct in6_addr *, struct in6_addr *));
125 static struct mbuf *ni6_input(struct mbuf *, int);
126 static struct mbuf *ni6_nametodns(const char *, int, int);
127 static int ni6_dnsmatch(const char *, int, const char *, int);
128 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
129 struct ifnet **, struct in6_addr *));
130 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
131 struct ifnet *, int));
132 static int icmp6_notify_error(struct mbuf **, int, int, int);
143 icmp6_errcount(struct icmp6errstat *stat, int type, int code)
146 case ICMP6_DST_UNREACH:
148 case ICMP6_DST_UNREACH_NOROUTE:
149 stat->icp6errs_dst_unreach_noroute++;
151 case ICMP6_DST_UNREACH_ADMIN:
152 stat->icp6errs_dst_unreach_admin++;
154 case ICMP6_DST_UNREACH_BEYONDSCOPE:
155 stat->icp6errs_dst_unreach_beyondscope++;
157 case ICMP6_DST_UNREACH_ADDR:
158 stat->icp6errs_dst_unreach_addr++;
160 case ICMP6_DST_UNREACH_NOPORT:
161 stat->icp6errs_dst_unreach_noport++;
165 case ICMP6_PACKET_TOO_BIG:
166 stat->icp6errs_packet_too_big++;
168 case ICMP6_TIME_EXCEEDED:
170 case ICMP6_TIME_EXCEED_TRANSIT:
171 stat->icp6errs_time_exceed_transit++;
173 case ICMP6_TIME_EXCEED_REASSEMBLY:
174 stat->icp6errs_time_exceed_reassembly++;
178 case ICMP6_PARAM_PROB:
180 case ICMP6_PARAMPROB_HEADER:
181 stat->icp6errs_paramprob_header++;
183 case ICMP6_PARAMPROB_NEXTHEADER:
184 stat->icp6errs_paramprob_nextheader++;
186 case ICMP6_PARAMPROB_OPTION:
187 stat->icp6errs_paramprob_option++;
192 stat->icp6errs_redirect++;
195 stat->icp6errs_unknown++;
199 * A wrapper function for icmp6_error() necessary when the erroneous packet
200 * may not contain enough scope zone information.
203 icmp6_error2(struct mbuf *m, int type, int code, int param,
211 #ifndef PULLDOWN_TEST
212 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
214 if (m->m_len < sizeof(struct ip6_hdr)) {
215 m = m_pullup(m, sizeof(struct ip6_hdr));
221 ip6 = mtod(m, struct ip6_hdr *);
223 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
225 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
228 icmp6_error(m, type, code, param);
232 * Generate an error packet of type error in response to bad IP6 packet.
235 icmp6_error(struct mbuf *m, int type, int code, int param)
237 struct ip6_hdr *oip6, *nip6;
238 struct icmp6_hdr *icmp6;
243 icmp6stat.icp6s_error++;
245 /* count per-type-code statistics */
246 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
248 #ifdef M_DECRYPTED /*not openbsd*/
249 if (m->m_flags & M_DECRYPTED) {
250 icmp6stat.icp6s_canterror++;
255 #ifndef PULLDOWN_TEST
256 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
258 if (m->m_len < sizeof(struct ip6_hdr)) {
259 m = m_pullup(m, sizeof(struct ip6_hdr));
264 oip6 = mtod(m, struct ip6_hdr *);
267 * If the destination address of the erroneous packet is a multicast
268 * address, or the packet was sent using link-layer multicast,
269 * we should basically suppress sending an error (RFC 2463, Section
271 * We have two exceptions (the item e.2 in that section):
272 * - the Pakcet Too Big message can be sent for path MTU discovery.
273 * - the Parameter Problem Message that can be allowed an icmp6 error
274 * in the option type field. This check has been done in
275 * ip6_unknown_opt(), so we can just check the type and code.
277 if ((m->m_flags & (M_BCAST|M_MCAST) ||
278 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
279 (type != ICMP6_PACKET_TOO_BIG &&
280 (type != ICMP6_PARAM_PROB ||
281 code != ICMP6_PARAMPROB_OPTION)))
285 * RFC 2463, 2.4 (e.5): source address check.
286 * XXX: the case of anycast source?
288 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
289 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
293 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
297 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
298 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
299 struct icmp6_hdr *icp;
301 #ifndef PULLDOWN_TEST
302 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
303 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
305 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
308 icmp6stat.icp6s_tooshort++;
312 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
313 icp->icmp6_type == ND_REDIRECT) {
316 * Special case: for redirect (which is
317 * informational) we must not send icmp6 error.
319 icmp6stat.icp6s_canterror++;
322 /* ICMPv6 informational - send the error */
325 /* non-ICMPv6 - send the error */
328 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
330 /* Finally, do rate limitation check. */
331 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
332 icmp6stat.icp6s_toofreq++;
337 * OK, ICMP6 can be generated.
340 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
341 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
343 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
344 M_PREPEND(m, preplen, M_DONTWAIT);
345 if (m && m->m_len < preplen)
346 m = m_pullup(m, preplen);
348 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
352 nip6 = mtod(m, struct ip6_hdr *);
353 nip6->ip6_src = oip6->ip6_src;
354 nip6->ip6_dst = oip6->ip6_dst;
356 in6_clearscope(&oip6->ip6_src);
357 in6_clearscope(&oip6->ip6_dst);
359 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
360 icmp6->icmp6_type = type;
361 icmp6->icmp6_code = code;
362 icmp6->icmp6_pptr = htonl((u_int32_t)param);
365 * icmp6_reflect() is designed to be in the input path.
366 * icmp6_error() can be called from both input and output path,
367 * and if we are in output path rcvif could contain bogus value.
368 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
369 * information in ip header (nip6).
371 m->m_pkthdr.rcvif = NULL;
373 icmp6stat.icp6s_outhist[type]++;
374 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
380 * If we can't tell whether or not we can generate ICMP6, free it.
386 * Process a received ICMP6 message.
389 icmp6_input(struct mbuf **mp, int *offp, int proto)
391 struct mbuf *m = *mp, *n;
392 struct ip6_hdr *ip6, *nip6;
393 struct icmp6_hdr *icmp6, *nicmp6;
395 int icmp6len = m->m_pkthdr.len - *offp;
397 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
399 #ifndef PULLDOWN_TEST
400 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
401 /* m might change if M_LOOP. So, call mtod after this */
405 * Locate icmp6 structure in mbuf, and check
406 * that not corrupted and of at least minimum length
409 ip6 = mtod(m, struct ip6_hdr *);
410 if (icmp6len < sizeof(struct icmp6_hdr)) {
411 icmp6stat.icp6s_tooshort++;
416 * calculate the checksum
418 #ifndef PULLDOWN_TEST
419 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
421 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
423 icmp6stat.icp6s_tooshort++;
427 code = icmp6->icmp6_code;
429 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
431 "ICMP6 checksum error(%d|%x) %s\n",
432 icmp6->icmp6_type, sum,
433 ip6_sprintf(ip6bufs, &ip6->ip6_src)));
434 icmp6stat.icp6s_checksum++;
438 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
440 * Deliver very specific ICMP6 type only.
441 * This is important to deliver TOOBIG. Otherwise PMTUD
444 switch (icmp6->icmp6_type) {
445 case ICMP6_DST_UNREACH:
446 case ICMP6_PACKET_TOO_BIG:
447 case ICMP6_TIME_EXCEEDED:
454 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
455 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
456 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
457 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
459 switch (icmp6->icmp6_type) {
460 case ICMP6_DST_UNREACH:
461 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
463 case ICMP6_DST_UNREACH_NOROUTE:
464 code = PRC_UNREACH_NET;
466 case ICMP6_DST_UNREACH_ADMIN:
467 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
468 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
470 case ICMP6_DST_UNREACH_ADDR:
473 case ICMP6_DST_UNREACH_BEYONDSCOPE:
474 /* I mean "source address was incorrect." */
475 code = PRC_PARAMPROB;
477 case ICMP6_DST_UNREACH_NOPORT:
478 code = PRC_UNREACH_PORT;
486 case ICMP6_PACKET_TOO_BIG:
487 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
489 /* validation is made in icmp6_mtudisc_update */
494 * Updating the path MTU will be done after examining
495 * intermediate extension headers.
500 case ICMP6_TIME_EXCEEDED:
501 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
503 case ICMP6_TIME_EXCEED_TRANSIT:
504 code = PRC_TIMXCEED_INTRANS;
506 case ICMP6_TIME_EXCEED_REASSEMBLY:
507 code = PRC_TIMXCEED_REASS;
515 case ICMP6_PARAM_PROB:
516 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
518 case ICMP6_PARAMPROB_NEXTHEADER:
519 code = PRC_UNREACH_PROTOCOL;
521 case ICMP6_PARAMPROB_HEADER:
522 case ICMP6_PARAMPROB_OPTION:
523 code = PRC_PARAMPROB;
531 case ICMP6_ECHO_REQUEST:
532 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
535 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
539 if ((n->m_flags & M_EXT) != 0
540 || n->m_len < off + sizeof(struct icmp6_hdr)) {
542 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
545 MGETHDR(n, M_DONTWAIT, n0->m_type);
546 n0len = n0->m_pkthdr.len; /* save for use below */
548 M_MOVE_PKTHDR(n, n0);
549 if (n && maxlen >= MHLEN) {
550 MCLGET(n, M_DONTWAIT);
551 if ((n->m_flags & M_EXT) == 0) {
562 * Copy IPv6 and ICMPv6 only.
564 nip6 = mtod(n, struct ip6_hdr *);
565 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
566 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
567 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
568 noff = sizeof(struct ip6_hdr);
569 /* new mbuf contains only ipv6+icmpv6 headers */
570 n->m_len = noff + sizeof(struct icmp6_hdr);
572 * Adjust mbuf. ip6_plen will be adjusted in
575 m_adj(n0, off + sizeof(struct icmp6_hdr));
576 /* recalculate complete packet size */
577 n->m_pkthdr.len = n0len + (noff - off);
580 nip6 = mtod(n, struct ip6_hdr *);
581 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
585 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
586 nicmp6->icmp6_code = 0;
588 icmp6stat.icp6s_reflect++;
589 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
590 icmp6_reflect(n, noff);
594 case ICMP6_ECHO_REPLY:
595 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
600 case MLD_LISTENER_QUERY:
601 case MLD_LISTENER_REPORT:
602 if (icmp6len < sizeof(struct mld_hdr))
604 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
605 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
607 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
608 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
618 case MLD_LISTENER_DONE:
619 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
620 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
622 break; /* nothing to be done in kernel */
624 case MLD_MTRACE_RESP:
626 /* XXX: these two are experimental. not officially defined. */
627 /* XXX: per-interface statistics? */
628 break; /* just pass it to applications */
630 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
632 enum { WRU, FQDN } mode;
637 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
639 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
644 #define hostnamelen strlen(hostname)
646 #ifndef PULLDOWN_TEST
647 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
650 n = m_copy(m, 0, M_COPYALL);
652 n = ni6_input(n, off);
653 /* XXX meaningless if n == NULL */
654 noff = sizeof(struct ip6_hdr);
660 * XXX: this combination of flags is pointless,
661 * but should we keep this for compatibility?
663 if ((icmp6_nodeinfo & 5) != 5)
668 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
669 if (maxlen >= MCLBYTES) {
673 MGETHDR(n, M_DONTWAIT, m->m_type);
674 if (n && maxlen > MHLEN) {
675 MCLGET(n, M_DONTWAIT);
676 if ((n->m_flags & M_EXT) == 0) {
681 if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) {
683 * Previous code did a blind M_COPY_PKTHDR
684 * and said "just for rcvif". If true, then
685 * we could tolerate the dup failing (due to
686 * the deep copy of the tag chain). For now
687 * be conservative and just fail.
696 n->m_pkthdr.rcvif = NULL;
698 maxhlen = M_TRAILINGSPACE(n) - maxlen;
699 if (maxhlen > hostnamelen)
700 maxhlen = hostnamelen;
702 * Copy IPv6 and ICMPv6 only.
704 nip6 = mtod(n, struct ip6_hdr *);
705 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
706 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
707 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
708 p = (u_char *)(nicmp6 + 1);
710 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
711 noff = sizeof(struct ip6_hdr);
712 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
713 sizeof(struct icmp6_hdr) + 4 + maxhlen;
714 nicmp6->icmp6_type = ICMP6_WRUREPLY;
715 nicmp6->icmp6_code = 0;
719 icmp6stat.icp6s_reflect++;
720 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
721 icmp6_reflect(n, noff);
731 case ND_ROUTER_SOLICIT:
732 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
735 if (icmp6len < sizeof(struct nd_router_solicit))
737 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
739 nd6_rs_input(m, off, icmp6len);
743 nd6_rs_input(n, off, icmp6len);
747 case ND_ROUTER_ADVERT:
748 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
751 if (icmp6len < sizeof(struct nd_router_advert))
753 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
755 nd6_ra_input(m, off, icmp6len);
759 nd6_ra_input(n, off, icmp6len);
763 case ND_NEIGHBOR_SOLICIT:
764 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
767 if (icmp6len < sizeof(struct nd_neighbor_solicit))
769 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
771 nd6_ns_input(m, off, icmp6len);
775 nd6_ns_input(n, off, icmp6len);
779 case ND_NEIGHBOR_ADVERT:
780 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
783 if (icmp6len < sizeof(struct nd_neighbor_advert))
785 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
787 nd6_na_input(m, off, icmp6len);
791 nd6_na_input(n, off, icmp6len);
796 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
799 if (icmp6len < sizeof(struct nd_redirect))
801 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
803 icmp6_redirect_input(m, off);
807 icmp6_redirect_input(n, off);
811 case ICMP6_ROUTER_RENUMBERING:
812 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
813 code != ICMP6_ROUTER_RENUMBERING_RESULT)
815 if (icmp6len < sizeof(struct icmp6_router_renum))
821 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
822 icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
823 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
824 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
825 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
826 /* ICMPv6 error: MUST deliver it by spec... */
830 /* ICMPv6 informational: MUST not deliver */
834 if (icmp6_notify_error(&m, off, icmp6len, code)) {
835 /* In this case, m should've been freed. */
836 return (IPPROTO_DONE);
841 icmp6stat.icp6s_badcode++;
845 icmp6stat.icp6s_badlen++;
849 /* deliver the packet to appropriate sockets */
850 icmp6_rip6_input(&m, *offp);
860 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code)
862 struct mbuf *m = *mp;
863 struct icmp6_hdr *icmp6;
864 struct ip6_hdr *eip6;
866 struct sockaddr_in6 icmp6src, icmp6dst;
868 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
869 icmp6stat.icp6s_tooshort++;
872 #ifndef PULLDOWN_TEST
873 IP6_EXTHDR_CHECK(m, off,
874 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
875 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
877 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
878 sizeof(*icmp6) + sizeof(struct ip6_hdr));
880 icmp6stat.icp6s_tooshort++;
884 eip6 = (struct ip6_hdr *)(icmp6 + 1);
886 /* Detect the upper level protocol */
888 void (*ctlfunc)(int, struct sockaddr *, void *);
889 u_int8_t nxt = eip6->ip6_nxt;
890 int eoff = off + sizeof(struct icmp6_hdr) +
891 sizeof(struct ip6_hdr);
892 struct ip6ctlparam ip6cp;
893 struct in6_addr *finaldst = NULL;
894 int icmp6type = icmp6->icmp6_type;
896 struct ip6_rthdr *rth;
897 struct ip6_rthdr0 *rth0;
900 while (1) { /* XXX: should avoid infinite loop explicitly? */
904 case IPPROTO_HOPOPTS:
905 case IPPROTO_DSTOPTS:
907 #ifndef PULLDOWN_TEST
908 IP6_EXTHDR_CHECK(m, 0,
909 eoff + sizeof(struct ip6_ext), -1);
910 eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
912 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
915 icmp6stat.icp6s_tooshort++;
920 if (nxt == IPPROTO_AH)
921 eoff += (eh->ip6e_len + 2) << 2;
923 eoff += (eh->ip6e_len + 1) << 3;
926 case IPPROTO_ROUTING:
928 * When the erroneous packet contains a
929 * routing header, we should examine the
930 * header to determine the final destination.
931 * Otherwise, we can't properly update
932 * information that depends on the final
933 * destination (e.g. path MTU).
935 #ifndef PULLDOWN_TEST
936 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
937 rth = (struct ip6_rthdr *)
938 (mtod(m, caddr_t) + eoff);
940 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
943 icmp6stat.icp6s_tooshort++;
947 rthlen = (rth->ip6r_len + 1) << 3;
949 * XXX: currently there is no
950 * officially defined type other
952 * Note that if the segment left field
953 * is 0, all intermediate hops must
956 if (rth->ip6r_segleft &&
957 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
960 #ifndef PULLDOWN_TEST
961 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
962 rth0 = (struct ip6_rthdr0 *)
963 (mtod(m, caddr_t) + eoff);
966 struct ip6_rthdr0 *, m,
969 icmp6stat.icp6s_tooshort++;
973 /* just ignore a bogus header */
974 if ((rth0->ip6r0_len % 2) == 0 &&
975 (hops = rth0->ip6r0_len/2))
976 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
981 case IPPROTO_FRAGMENT:
982 #ifndef PULLDOWN_TEST
983 IP6_EXTHDR_CHECK(m, 0, eoff +
984 sizeof(struct ip6_frag), -1);
985 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
988 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
991 icmp6stat.icp6s_tooshort++;
996 * Data after a fragment header is meaningless
997 * unless it is the first fragment, but
998 * we'll go to the notify label for path MTU
1001 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1004 eoff += sizeof(struct ip6_frag);
1009 * This case includes ESP and the No Next
1010 * Header. In such cases going to the notify
1011 * label does not have any meaning
1012 * (i.e. ctlfunc will be NULL), but we go
1013 * anyway since we might have to update
1014 * path MTU information.
1020 #ifndef PULLDOWN_TEST
1021 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1023 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1024 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1025 if (icmp6 == NULL) {
1026 icmp6stat.icp6s_tooshort++;
1032 * retrieve parameters from the inner IPv6 header, and convert
1033 * them into sockaddr structures.
1034 * XXX: there is no guarantee that the source or destination
1035 * addresses of the inner packet are in the same scope as
1036 * the addresses of the icmp packet. But there is no other
1037 * way to determine the zone.
1039 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1041 bzero(&icmp6dst, sizeof(icmp6dst));
1042 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1043 icmp6dst.sin6_family = AF_INET6;
1044 if (finaldst == NULL)
1045 icmp6dst.sin6_addr = eip6->ip6_dst;
1047 icmp6dst.sin6_addr = *finaldst;
1048 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1050 bzero(&icmp6src, sizeof(icmp6src));
1051 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1052 icmp6src.sin6_family = AF_INET6;
1053 icmp6src.sin6_addr = eip6->ip6_src;
1054 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1056 icmp6src.sin6_flowinfo =
1057 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1059 if (finaldst == NULL)
1060 finaldst = &eip6->ip6_dst;
1062 ip6cp.ip6c_icmp6 = icmp6;
1063 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1064 ip6cp.ip6c_off = eoff;
1065 ip6cp.ip6c_finaldst = finaldst;
1066 ip6cp.ip6c_src = &icmp6src;
1067 ip6cp.ip6c_nxt = nxt;
1069 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1070 notifymtu = ntohl(icmp6->icmp6_mtu);
1071 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1072 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1075 ctlfunc = (void (*)(int, struct sockaddr *, void *))
1076 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1078 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1091 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1093 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1094 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1095 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1096 u_int mtu = ntohl(icmp6->icmp6_mtu);
1097 struct in_conninfo inc;
1101 * RFC2460 section 5, last paragraph.
1102 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1103 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1104 * due to packet translator in the middle.
1105 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1108 if (mtu < IPV6_MMTU)
1113 * we reject ICMPv6 too big with abnormally small value.
1114 * XXX what is the good definition of "abnormally small"?
1116 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1123 * In case the suggested mtu is less than IPV6_MMTU, we
1124 * only need to remember that it was for above mentioned
1125 * "alwaysfrag" case.
1126 * Try to be as close to the spec as possible.
1128 if (mtu < IPV6_MMTU)
1129 mtu = IPV6_MMTU - 8;
1131 bzero(&inc, sizeof(inc));
1132 inc.inc_flags |= INC_ISIPV6;
1133 inc.inc6_faddr = *dst;
1134 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1137 if (mtu < tcp_maxmtu6(&inc, NULL)) {
1138 tcp_hc_updatemtu(&inc, mtu);
1139 icmp6stat.icp6s_pmtuchg++;
1144 * Process a Node Information Query packet, based on
1145 * draft-ietf-ipngwg-icmp-name-lookups-07.
1147 * Spec incompatibilities:
1148 * - IPv6 Subject address handling
1149 * - IPv4 Subject address handling support missing
1150 * - Proxy reply (answer even if it's not for me)
1151 * - joins NI group address at in6_ifattach() time only, does not cope
1152 * with hostname changes by sethostname(3)
1154 #define hostnamelen strlen(hostname)
1155 static struct mbuf *
1156 ni6_input(struct mbuf *m, int off)
1158 struct icmp6_nodeinfo *ni6, *nni6;
1159 struct mbuf *n = NULL;
1162 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1163 struct ni_reply_fqdn *fqdn;
1164 int addrs; /* for NI_QTYPE_NODEADDR */
1165 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1166 struct in6_addr in6_subj; /* subject address */
1167 struct ip6_hdr *ip6;
1168 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1170 struct in6_ifaddr *ia6 = NULL;
1172 ip6 = mtod(m, struct ip6_hdr *);
1173 #ifndef PULLDOWN_TEST
1174 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1176 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1178 /* m is already reclaimed */
1184 * Validate IPv6 source address.
1185 * The default configuration MUST be to refuse answering queries from
1186 * global-scope addresses according to RFC4602.
1188 * - it's not very clear what "refuse" means; this implementation
1190 * - it's not very easy to identify global-scope (unicast) addresses
1191 * since there are many prefixes for them. It should be safer
1192 * and in practice sufficient to check "all" but loopback and
1193 * link-local (note that site-local unicast was deprecated and
1194 * ULA is defined as global scope-wise)
1196 if ((icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1197 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1198 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1202 * Validate IPv6 destination address.
1204 * The Responder must discard the Query without further processing
1205 * unless it is one of the Responder's unicast or anycast addresses, or
1206 * a link-local scope multicast address which the Responder has joined.
1207 * [RFC4602, Section 5.]
1209 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1210 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1212 /* else it's a link-local multicast, fine */
1213 } else { /* unicast or anycast */
1214 if ((ia6 = ip6_getdstifaddr(m)) == NULL)
1215 goto bad; /* XXX impossible */
1217 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1218 !(icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1219 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1220 "a temporary address in %s:%d",
1221 __FILE__, __LINE__));
1226 /* validate query Subject field. */
1227 qtype = ntohs(ni6->ni_qtype);
1228 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1231 case NI_QTYPE_SUPTYPES:
1233 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1237 case NI_QTYPE_NODEADDR:
1238 case NI_QTYPE_IPV4ADDR:
1239 switch (ni6->ni_code) {
1240 case ICMP6_NI_SUBJ_IPV6:
1241 #if ICMP6_NI_SUBJ_IPV6 != 0
1245 * backward compatibility - try to accept 03 draft
1246 * format, where no Subject is present.
1248 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1253 #if ICMP6_NI_SUBJ_IPV6 != 0
1254 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1258 if (subjlen != sizeof(struct in6_addr))
1262 * Validate Subject address.
1264 * Not sure what exactly "address belongs to the node"
1265 * means in the spec, is it just unicast, or what?
1267 * At this moment we consider Subject address as
1268 * "belong to the node" if the Subject address equals
1269 * to the IPv6 destination address; validation for
1270 * IPv6 destination address should have done enough
1273 * We do not do proxy at this moment.
1275 /* m_pulldown instead of copy? */
1276 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1277 subjlen, (caddr_t)&in6_subj);
1278 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1281 subj = (char *)&in6_subj;
1282 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1286 * XXX if we are to allow other cases, we should really
1287 * be careful about scope here.
1288 * basically, we should disallow queries toward IPv6
1289 * destination X with subject Y,
1290 * if scope(X) > scope(Y).
1291 * if we allow scope(X) > scope(Y), it will result in
1292 * information leakage across scope boundary.
1296 case ICMP6_NI_SUBJ_FQDN:
1298 * Validate Subject name with gethostname(3).
1300 * The behavior may need some debate, since:
1301 * - we are not sure if the node has FQDN as
1302 * hostname (returned by gethostname(3)).
1303 * - the code does wildcard match for truncated names.
1304 * however, we are not sure if we want to perform
1305 * wildcard match, if gethostname(3) side has
1306 * truncated hostname.
1308 n = ni6_nametodns(hostname, hostnamelen, 0);
1309 if (!n || n->m_next || n->m_len == 0)
1311 IP6_EXTHDR_GET(subj, char *, m,
1312 off + sizeof(struct icmp6_nodeinfo), subjlen);
1315 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1323 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1330 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1333 if ((icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1336 case NI_QTYPE_NODEADDR:
1337 case NI_QTYPE_IPV4ADDR:
1338 if ((icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1343 /* guess reply length */
1346 break; /* no reply data */
1347 case NI_QTYPE_SUPTYPES:
1348 replylen += sizeof(u_int32_t);
1351 /* XXX will append an mbuf */
1352 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1354 case NI_QTYPE_NODEADDR:
1355 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1356 if ((replylen += addrs * (sizeof(struct in6_addr) +
1357 sizeof(u_int32_t))) > MCLBYTES)
1358 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1360 case NI_QTYPE_IPV4ADDR:
1361 /* unsupported - should respond with unknown Qtype? */
1365 * XXX: We must return a reply with the ICMP6 code
1366 * `unknown Qtype' in this case. However we regard the case
1367 * as an FQDN query for backward compatibility.
1368 * Older versions set a random value to this field,
1369 * so it rarely varies in the defined qtypes.
1370 * But the mechanism is not reliable...
1371 * maybe we should obsolete older versions.
1373 qtype = NI_QTYPE_FQDN;
1374 /* XXX will append an mbuf */
1375 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1380 /* allocate an mbuf to reply. */
1381 MGETHDR(n, M_DONTWAIT, m->m_type);
1386 M_MOVE_PKTHDR(n, m); /* just for recvif */
1387 if (replylen > MHLEN) {
1388 if (replylen > MCLBYTES) {
1390 * XXX: should we try to allocate more? But MCLBYTES
1391 * is probably much larger than IPV6_MMTU...
1395 MCLGET(n, M_DONTWAIT);
1396 if ((n->m_flags & M_EXT) == 0) {
1400 n->m_pkthdr.len = n->m_len = replylen;
1402 /* copy mbuf header and IPv6 + Node Information base headers */
1403 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1404 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1405 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1407 /* qtype dependent procedure */
1410 nni6->ni_code = ICMP6_NI_SUCCESS;
1413 case NI_QTYPE_SUPTYPES:
1416 nni6->ni_code = ICMP6_NI_SUCCESS;
1417 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1418 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1419 v = (u_int32_t)htonl(0x0000000f);
1420 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1424 nni6->ni_code = ICMP6_NI_SUCCESS;
1425 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1426 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1427 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1428 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1430 * XXX do we really have FQDN in variable "hostname"?
1432 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1433 if (n->m_next == NULL)
1435 /* XXX we assume that n->m_next is not a chain */
1436 if (n->m_next->m_next != NULL)
1438 n->m_pkthdr.len += n->m_next->m_len;
1440 case NI_QTYPE_NODEADDR:
1444 nni6->ni_code = ICMP6_NI_SUCCESS;
1445 n->m_pkthdr.len = n->m_len =
1446 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1447 lenlim = M_TRAILINGSPACE(n);
1448 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1449 /* XXX: reset mbuf length */
1450 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1451 sizeof(struct icmp6_nodeinfo) + copied;
1455 break; /* XXX impossible! */
1458 nni6->ni_type = ICMP6_NI_REPLY;
1471 * make a mbuf with DNS-encoded string. no compression support.
1473 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1474 * treated as truncated name (two \0 at the end). this is a wild guess.
1476 * old - return pascal string if non-zero
1478 static struct mbuf *
1479 ni6_nametodns(const char *name, int namelen, int old)
1491 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1492 MGET(m, M_DONTWAIT, MT_DATA);
1493 if (m && len > MLEN) {
1494 MCLGET(m, M_DONTWAIT);
1495 if ((m->m_flags & M_EXT) == 0)
1504 *mtod(m, char *) = namelen;
1505 bcopy(name, mtod(m, char *) + 1, namelen);
1509 cp = mtod(m, char *);
1510 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1512 /* if not certain about my name, return empty buffer */
1517 * guess if it looks like shortened hostname, or FQDN.
1518 * shortened hostname needs two trailing "\0".
1521 for (p = name; p < name + namelen; p++) {
1522 if (*p && *p == '.')
1531 while (cp < ep && p < name + namelen) {
1533 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1535 /* result does not fit into mbuf */
1536 if (cp + i + 1 >= ep)
1539 * DNS label length restriction, RFC1035 page 8.
1540 * "i == 0" case is included here to avoid returning
1541 * 0-length label on "foo..bar".
1543 if (i <= 0 || i >= 64)
1549 if (p < name + namelen && *p == '.')
1553 if (cp + nterm >= ep)
1557 m->m_len = cp - mtod(m, char *);
1561 panic("should not reach here");
1571 * check if two DNS-encoded string matches. takes care of truncated
1572 * form (with \0\0 at the end). no compression support.
1573 * XXX upper/lowercase match (see RFC2065)
1576 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1578 const char *a0, *b0;
1581 /* simplest case - need validation? */
1582 if (alen == blen && bcmp(a, b, alen) == 0)
1588 /* termination is mandatory */
1589 if (alen < 2 || blen < 2)
1591 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1596 while (a - a0 < alen && b - b0 < blen) {
1597 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1600 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1602 /* we don't support compression yet */
1603 if (a[0] >= 64 || b[0] >= 64)
1606 /* truncated case */
1607 if (a[0] == 0 && a - a0 == alen - 1)
1609 if (b[0] == 0 && b - b0 == blen - 1)
1611 if (a[0] == 0 || b[0] == 0)
1617 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1619 if (bcmp(a + 1, b + 1, l) != 0)
1626 if (a - a0 == alen && b - b0 == blen)
1633 * calculate the number of addresses to be returned in the node info reply.
1636 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1637 struct in6_addr *subj)
1640 struct in6_ifaddr *ifa6;
1642 int addrs = 0, addrsofif, iffound = 0;
1643 int niflags = ni6->ni_flags;
1645 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1646 switch (ni6->ni_code) {
1647 case ICMP6_NI_SUBJ_IPV6:
1648 if (subj == NULL) /* must be impossible... */
1653 * XXX: we only support IPv6 subject address for
1661 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1663 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1664 if (ifa->ifa_addr->sa_family != AF_INET6)
1666 ifa6 = (struct in6_ifaddr *)ifa;
1668 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1669 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1673 * IPv4-mapped addresses can only be returned by a
1674 * Node Information proxy, since they represent
1675 * addresses of IPv4-only nodes, which perforce do
1676 * not implement this protocol.
1677 * [icmp-name-lookups-07, Section 5.4]
1678 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1679 * this function at this moment.
1682 /* What do we have to do about ::1? */
1683 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1684 case IPV6_ADDR_SCOPE_LINKLOCAL:
1685 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1688 case IPV6_ADDR_SCOPE_SITELOCAL:
1689 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1692 case IPV6_ADDR_SCOPE_GLOBAL:
1693 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1701 * check if anycast is okay.
1702 * XXX: just experimental. not in the spec.
1704 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1705 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1706 continue; /* we need only unicast addresses */
1707 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1708 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1711 addrsofif++; /* count the address */
1727 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1728 struct ifnet *ifp0, int resid)
1730 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1731 struct in6_ifaddr *ifa6;
1733 struct ifnet *ifp_dep = NULL;
1734 int copied = 0, allow_deprecated = 0;
1735 u_char *cp = (u_char *)(nni6 + 1);
1736 int niflags = ni6->ni_flags;
1739 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1740 return (0); /* needless to copy */
1745 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1746 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1747 ifa = ifa->ifa_list.tqe_next) {
1748 if (ifa->ifa_addr->sa_family != AF_INET6)
1750 ifa6 = (struct in6_ifaddr *)ifa;
1752 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1753 allow_deprecated == 0) {
1755 * prefererred address should be put before
1756 * deprecated addresses.
1759 /* record the interface for later search */
1760 if (ifp_dep == NULL)
1764 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1765 allow_deprecated != 0)
1766 continue; /* we now collect deprecated addrs */
1768 /* What do we have to do about ::1? */
1769 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1770 case IPV6_ADDR_SCOPE_LINKLOCAL:
1771 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1774 case IPV6_ADDR_SCOPE_SITELOCAL:
1775 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1778 case IPV6_ADDR_SCOPE_GLOBAL:
1779 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1787 * check if anycast is okay.
1788 * XXX: just experimental. not in the spec.
1790 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1791 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1793 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1794 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1798 /* now we can copy the address */
1799 if (resid < sizeof(struct in6_addr) +
1800 sizeof(u_int32_t)) {
1802 * We give up much more copy.
1803 * Set the truncate flag and return.
1805 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1811 * Set the TTL of the address.
1812 * The TTL value should be one of the following
1813 * according to the specification:
1815 * 1. The remaining lifetime of a DHCP lease on the
1817 * 2. The remaining Valid Lifetime of a prefix from
1818 * which the address was derived through Stateless
1819 * Autoconfiguration.
1821 * Note that we currently do not support stateful
1822 * address configuration by DHCPv6, so the former
1823 * case can't happen.
1825 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1826 ltime = ND6_INFINITE_LIFETIME;
1828 if (ifa6->ia6_lifetime.ia6t_expire >
1830 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1835 bcopy(<ime, cp, sizeof(u_int32_t));
1836 cp += sizeof(u_int32_t);
1838 /* copy the address itself */
1839 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1840 sizeof(struct in6_addr));
1841 in6_clearscope((struct in6_addr *)cp); /* XXX */
1842 cp += sizeof(struct in6_addr);
1844 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1845 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1847 if (ifp0) /* we need search only on the specified IF */
1851 if (allow_deprecated == 0 && ifp_dep != NULL) {
1853 allow_deprecated = 1;
1864 * XXX almost dup'ed code with rip6_input.
1867 icmp6_rip6_input(struct mbuf **mp, int off)
1869 struct mbuf *m = *mp;
1870 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1872 struct inpcb *last = NULL;
1873 struct sockaddr_in6 fromsa;
1874 struct icmp6_hdr *icmp6;
1875 struct mbuf *opts = NULL;
1877 #ifndef PULLDOWN_TEST
1878 /* this is assumed to be safe. */
1879 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1881 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1882 if (icmp6 == NULL) {
1883 /* m is already reclaimed */
1884 return (IPPROTO_DONE);
1889 * XXX: the address may have embedded scope zone ID, which should be
1890 * hidden from applications.
1892 bzero(&fromsa, sizeof(fromsa));
1893 fromsa.sin6_family = AF_INET6;
1894 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1895 fromsa.sin6_addr = ip6->ip6_src;
1896 if (sa6_recoverscope(&fromsa)) {
1898 return (IPPROTO_DONE);
1901 INP_INFO_RLOCK(&ripcbinfo);
1902 LIST_FOREACH(in6p, &ripcb, inp_list) {
1903 if ((in6p->inp_vflag & INP_IPV6) == 0)
1905 if (in6p->inp_ip_p != IPPROTO_ICMPV6)
1907 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1908 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1910 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1911 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1914 if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1915 in6p->in6p_icmp6filt)) {
1920 struct mbuf *n = NULL;
1923 * Recent network drivers tend to allocate a single
1924 * mbuf cluster, rather than to make a couple of
1925 * mbufs without clusters. Also, since the IPv6 code
1926 * path tries to avoid m_pullup(), it is highly
1927 * probable that we still have an mbuf cluster here
1928 * even though the necessary length can be stored in an
1929 * mbuf's internal buffer.
1930 * Meanwhile, the default size of the receive socket
1931 * buffer for raw sockets is not so large. This means
1932 * the possibility of packet loss is relatively higher
1933 * than before. To avoid this scenario, we copy the
1934 * received data to a separate mbuf that does not use
1935 * a cluster, if possible.
1936 * XXX: it is better to copy the data after stripping
1937 * intermediate headers.
1939 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1940 m->m_len <= MHLEN) {
1941 MGET(n, M_DONTWAIT, m->m_type);
1943 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1944 bcopy(m->m_data, n->m_data,
1946 n->m_len = m->m_len;
1954 (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1955 if (last->inp_flags & INP_CONTROLOPTS)
1956 ip6_savecontrol(last, n, &opts);
1957 /* strip intermediate headers */
1959 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
1960 if (sbappendaddr_locked(
1961 &last->inp_socket->so_rcv,
1962 (struct sockaddr *)&fromsa, n, opts)
1964 /* should notify about lost packet */
1970 &last->inp_socket->so_rcv);
1972 sorwakeup_locked(last->inp_socket);
1979 INP_INFO_RUNLOCK(&ripcbinfo);
1981 if (last->inp_flags & INP_CONTROLOPTS)
1982 ip6_savecontrol(last, m, &opts);
1983 /* strip intermediate headers */
1986 /* avoid using mbuf clusters if possible (see above) */
1987 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1988 m->m_len <= MHLEN) {
1991 MGET(n, M_DONTWAIT, m->m_type);
1993 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1994 bcopy(m->m_data, n->m_data, m->m_len);
1995 n->m_len = m->m_len;
2005 SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2006 if (sbappendaddr_locked(&last->inp_socket->so_rcv,
2007 (struct sockaddr *)&fromsa, m, opts) == 0) {
2011 SOCKBUF_UNLOCK(&last->inp_socket->so_rcv);
2013 sorwakeup_locked(last->inp_socket);
2017 ip6stat.ip6s_delivered--;
2019 return IPPROTO_DONE;
2023 * Reflect the ip6 packet back to the source.
2024 * OFF points to the icmp6 header, counted from the top of the mbuf.
2027 icmp6_reflect(struct mbuf *m, size_t off)
2029 struct ip6_hdr *ip6;
2030 struct icmp6_hdr *icmp6;
2031 struct in6_ifaddr *ia;
2034 struct ifnet *outif = NULL;
2035 struct in6_addr origdst, *src = NULL;
2037 /* too short to reflect */
2038 if (off < sizeof(struct ip6_hdr)) {
2040 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2041 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2042 __FILE__, __LINE__));
2047 * If there are extra headers between IPv6 and ICMPv6, strip
2048 * off that header first.
2051 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2052 panic("assumption failed in icmp6_reflect");
2054 if (off > sizeof(struct ip6_hdr)) {
2056 struct ip6_hdr nip6;
2058 l = off - sizeof(struct ip6_hdr);
2059 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2061 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2063 if ((m = m_pullup(m, l)) == NULL)
2066 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2067 } else /* off == sizeof(struct ip6_hdr) */ {
2069 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2071 if ((m = m_pullup(m, l)) == NULL)
2075 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2076 ip6 = mtod(m, struct ip6_hdr *);
2077 ip6->ip6_nxt = IPPROTO_ICMPV6;
2078 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2079 type = icmp6->icmp6_type; /* keep type for statistics */
2080 code = icmp6->icmp6_code; /* ditto. */
2082 origdst = ip6->ip6_dst;
2084 * ip6_input() drops a packet if its src is multicast.
2085 * So, the src is never multicast.
2087 ip6->ip6_dst = ip6->ip6_src;
2090 * If the incoming packet was addressed directly to us (i.e. unicast),
2091 * use dst as the src for the reply.
2092 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2093 * (for example) when we encounter an error while forwarding procedure
2094 * destined to a duplicated address of ours.
2095 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2096 * procedure of an outgoing packet of our own, in which case we need
2097 * to search in the ifaddr list.
2099 if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
2100 if ((ia = ip6_getdstifaddr(m))) {
2101 if (!(ia->ia6_flags &
2102 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2103 src = &ia->ia_addr.sin6_addr;
2105 struct sockaddr_in6 d;
2107 bzero(&d, sizeof(d));
2108 d.sin6_family = AF_INET6;
2109 d.sin6_len = sizeof(d);
2110 d.sin6_addr = origdst;
2111 ia = (struct in6_ifaddr *)
2112 ifa_ifwithaddr((struct sockaddr *)&d);
2115 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2116 src = &ia->ia_addr.sin6_addr;
2123 struct sockaddr_in6 sin6;
2124 struct route_in6 ro;
2127 * This case matches to multicasts, our anycast, or unicasts
2128 * that we do not own. Select a source address based on the
2129 * source address of the erroneous packet.
2131 bzero(&sin6, sizeof(sin6));
2132 sin6.sin6_family = AF_INET6;
2133 sin6.sin6_len = sizeof(sin6);
2134 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2136 bzero(&ro, sizeof(ro));
2137 src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e);
2139 RTFREE(ro.ro_rt); /* XXX: we could use this */
2141 char ip6buf[INET6_ADDRSTRLEN];
2143 "icmp6_reflect: source can't be determined: "
2144 "dst=%s, error=%d\n",
2145 ip6_sprintf(ip6buf, &sin6.sin6_addr), e));
2150 ip6->ip6_src = *src;
2152 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2153 ip6->ip6_vfc |= IPV6_VERSION;
2154 ip6->ip6_nxt = IPPROTO_ICMPV6;
2156 ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2157 else if (m->m_pkthdr.rcvif) {
2158 /* XXX: This may not be the outgoing interface */
2159 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2161 ip6->ip6_hlim = ip6_defhlim;
2163 icmp6->icmp6_cksum = 0;
2164 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2165 sizeof(struct ip6_hdr), plen);
2168 * XXX option handling
2171 m->m_flags &= ~(M_BCAST|M_MCAST);
2173 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2175 icmp6_ifoutstat_inc(outif, type, code);
2185 icmp6_fasttimo(void)
2192 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2193 struct in6_addr *tgt6)
2195 static char buf[1024];
2196 char ip6bufs[INET6_ADDRSTRLEN];
2197 char ip6bufd[INET6_ADDRSTRLEN];
2198 char ip6buft[INET6_ADDRSTRLEN];
2199 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2200 ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2201 ip6_sprintf(ip6buft, tgt6));
2206 icmp6_redirect_input(struct mbuf *m, int off)
2209 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2210 struct nd_redirect *nd_rd;
2211 int icmp6len = ntohs(ip6->ip6_plen);
2212 char *lladdr = NULL;
2214 u_char *redirhdr = NULL;
2215 int redirhdrlen = 0;
2216 struct rtentry *rt = NULL;
2219 struct in6_addr src6 = ip6->ip6_src;
2220 struct in6_addr redtgt6;
2221 struct in6_addr reddst6;
2222 union nd_opts ndopts;
2223 char ip6buf[INET6_ADDRSTRLEN];
2228 ifp = m->m_pkthdr.rcvif;
2233 /* XXX if we are router, we don't update route by icmp6 redirect */
2236 if (!icmp6_rediraccept)
2239 #ifndef PULLDOWN_TEST
2240 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2241 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2243 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2244 if (nd_rd == NULL) {
2245 icmp6stat.icp6s_tooshort++;
2249 redtgt6 = nd_rd->nd_rd_target;
2250 reddst6 = nd_rd->nd_rd_dst;
2252 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2253 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2258 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2260 "ICMP6 redirect sent from %s rejected; "
2261 "must be from linklocal\n",
2262 ip6_sprintf(ip6buf, &src6)));
2265 if (ip6->ip6_hlim != 255) {
2267 "ICMP6 redirect sent from %s rejected; "
2268 "hlim=%d (must be 255)\n",
2269 ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
2273 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2274 struct sockaddr_in6 sin6;
2275 struct in6_addr *gw6;
2277 bzero(&sin6, sizeof(sin6));
2278 sin6.sin6_family = AF_INET6;
2279 sin6.sin6_len = sizeof(struct sockaddr_in6);
2280 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2281 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
2283 if (rt->rt_gateway == NULL ||
2284 rt->rt_gateway->sa_family != AF_INET6) {
2286 "ICMP6 redirect rejected; no route "
2287 "with inet6 gateway found for redirect dst: %s\n",
2288 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2293 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2294 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2296 "ICMP6 redirect rejected; "
2297 "not equal to gw-for-src=%s (must be same): "
2299 ip6_sprintf(ip6buf, gw6),
2300 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2306 "ICMP6 redirect rejected; "
2307 "no route found for redirect dst: %s\n",
2308 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2314 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2316 "ICMP6 redirect rejected; "
2317 "redirect dst must be unicast: %s\n",
2318 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2322 is_router = is_onlink = 0;
2323 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2324 is_router = 1; /* router case */
2325 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2326 is_onlink = 1; /* on-link destination case */
2327 if (!is_router && !is_onlink) {
2329 "ICMP6 redirect rejected; "
2330 "neither router case nor onlink case: %s\n",
2331 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2334 /* validation passed */
2336 icmp6len -= sizeof(*nd_rd);
2337 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2338 if (nd6_options(&ndopts) < 0) {
2339 nd6log((LOG_INFO, "icmp6_redirect_input: "
2340 "invalid ND option, rejected: %s\n",
2341 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2342 /* nd6_options have incremented stats */
2346 if (ndopts.nd_opts_tgt_lladdr) {
2347 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2348 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2351 if (ndopts.nd_opts_rh) {
2352 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2353 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2356 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2358 "icmp6_redirect_input: lladdrlen mismatch for %s "
2359 "(if %d, icmp6 packet %d): %s\n",
2360 ip6_sprintf(ip6buf, &redtgt6),
2361 ifp->if_addrlen, lladdrlen - 2,
2362 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2367 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2368 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2370 if (!is_onlink) { /* better router case. perform rtredirect. */
2371 /* perform rtredirect */
2372 struct sockaddr_in6 sdst;
2373 struct sockaddr_in6 sgw;
2374 struct sockaddr_in6 ssrc;
2376 bzero(&sdst, sizeof(sdst));
2377 bzero(&sgw, sizeof(sgw));
2378 bzero(&ssrc, sizeof(ssrc));
2379 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2380 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2381 sizeof(struct sockaddr_in6);
2382 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2383 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2384 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2385 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2386 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2387 (struct sockaddr *)&ssrc);
2389 /* finally update cached route in each socket via pfctlinput */
2391 struct sockaddr_in6 sdst;
2393 bzero(&sdst, sizeof(sdst));
2394 sdst.sin6_family = AF_INET6;
2395 sdst.sin6_len = sizeof(struct sockaddr_in6);
2396 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2397 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2399 key_sa_routechange((struct sockaddr *)&sdst);
2408 icmp6stat.icp6s_badredirect++;
2413 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2415 struct ifnet *ifp; /* my outgoing interface */
2416 struct in6_addr *ifp_ll6;
2417 struct in6_addr *router_ll6;
2418 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2419 struct mbuf *m = NULL; /* newly allocated one */
2420 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2421 struct nd_redirect *nd_rd;
2424 struct ifnet *outif = NULL;
2425 struct sockaddr_in6 src_sa;
2427 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2429 /* if we are not router, we don't send icmp6 redirect */
2430 if (!ip6_forwarding)
2434 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2439 * the source address must identify a neighbor, and
2440 * the destination address must not be a multicast address
2441 * [RFC 2461, sec 8.2]
2443 sip6 = mtod(m0, struct ip6_hdr *);
2444 bzero(&src_sa, sizeof(src_sa));
2445 src_sa.sin6_family = AF_INET6;
2446 src_sa.sin6_len = sizeof(src_sa);
2447 src_sa.sin6_addr = sip6->ip6_src;
2448 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2450 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2451 goto fail; /* what should we do here? */
2454 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2458 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2459 * we almost always ask for an mbuf cluster for simplicity.
2460 * (MHLEN < IPV6_MMTU is almost always true)
2462 #if IPV6_MMTU >= MCLBYTES
2463 # error assumption failed about IPV6_MMTU and MCLBYTES
2465 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2466 if (m && IPV6_MMTU >= MHLEN)
2467 MCLGET(m, M_DONTWAIT);
2470 m->m_pkthdr.rcvif = NULL;
2472 maxlen = M_TRAILINGSPACE(m);
2473 maxlen = min(IPV6_MMTU, maxlen);
2474 /* just for safety */
2475 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2476 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2481 /* get ip6 linklocal address for ifp(my outgoing interface). */
2482 struct in6_ifaddr *ia;
2483 if ((ia = in6ifa_ifpforlinklocal(ifp,
2485 IN6_IFF_ANYCAST)) == NULL)
2487 ifp_ll6 = &ia->ia_addr.sin6_addr;
2490 /* get ip6 linklocal address for the router. */
2491 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2492 struct sockaddr_in6 *sin6;
2493 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2494 router_ll6 = &sin6->sin6_addr;
2495 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2496 router_ll6 = (struct in6_addr *)NULL;
2498 router_ll6 = (struct in6_addr *)NULL;
2501 ip6 = mtod(m, struct ip6_hdr *);
2503 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2504 ip6->ip6_vfc |= IPV6_VERSION;
2505 /* ip6->ip6_plen will be set later */
2506 ip6->ip6_nxt = IPPROTO_ICMPV6;
2507 ip6->ip6_hlim = 255;
2508 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2509 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2510 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2513 nd_rd = (struct nd_redirect *)(ip6 + 1);
2514 nd_rd->nd_rd_type = ND_REDIRECT;
2515 nd_rd->nd_rd_code = 0;
2516 nd_rd->nd_rd_reserved = 0;
2517 if (rt->rt_flags & RTF_GATEWAY) {
2519 * nd_rd->nd_rd_target must be a link-local address in
2520 * better router cases.
2524 bcopy(router_ll6, &nd_rd->nd_rd_target,
2525 sizeof(nd_rd->nd_rd_target));
2526 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2527 sizeof(nd_rd->nd_rd_dst));
2529 /* make sure redtgt == reddst */
2530 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2531 sizeof(nd_rd->nd_rd_target));
2532 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2533 sizeof(nd_rd->nd_rd_dst));
2536 p = (u_char *)(nd_rd + 1);
2542 /* target lladdr option */
2543 struct rtentry *rt_router = NULL;
2545 struct sockaddr_dl *sdl;
2546 struct nd_opt_hdr *nd_opt;
2549 rt_router = nd6_lookup(router_ll6, 0, ifp);
2552 len = sizeof(*nd_opt) + ifp->if_addrlen;
2553 len = (len + 7) & ~7; /* round by 8 */
2555 if (len + (p - (u_char *)ip6) > maxlen)
2557 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2558 (rt_router->rt_flags & RTF_LLINFO) &&
2559 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2560 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2562 nd_opt = (struct nd_opt_hdr *)p;
2563 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2564 nd_opt->nd_opt_len = len >> 3;
2565 lladdr = (char *)(nd_opt + 1);
2566 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2572 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2574 /* just to be safe */
2575 #ifdef M_DECRYPTED /*not openbsd*/
2576 if (m0->m_flags & M_DECRYPTED)
2579 if (p - (u_char *)ip6 > maxlen)
2583 /* redirected header option */
2585 struct nd_opt_rd_hdr *nd_opt_rh;
2588 * compute the maximum size for icmp6 redirect header option.
2589 * XXX room for auth header?
2591 len = maxlen - (p - (u_char *)ip6);
2594 /* This is just for simplicity. */
2595 if (m0->m_pkthdr.len != m0->m_len) {
2597 m_freem(m0->m_next);
2600 m0->m_pkthdr.len = m0->m_len;
2604 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2605 * about padding/truncate rule for the original IP packet.
2606 * From the discussion on IPv6imp in Feb 1999,
2607 * the consensus was:
2608 * - "attach as much as possible" is the goal
2609 * - pad if not aligned (original size can be guessed by
2610 * original ip6 header)
2611 * Following code adds the padding if it is simple enough,
2612 * and truncates if not.
2614 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2615 panic("assumption failed in %s:%d", __FILE__,
2618 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2619 /* not enough room, truncate */
2620 m0->m_pkthdr.len = m0->m_len = len -
2623 /* enough room, pad or truncate */
2626 extra = m0->m_pkthdr.len % 8;
2628 /* pad if easy enough, truncate if not */
2629 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2631 m0->m_len += (8 - extra);
2632 m0->m_pkthdr.len += (8 - extra);
2635 m0->m_pkthdr.len -= extra;
2639 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2640 m0->m_pkthdr.len = m0->m_len = len -
2644 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2645 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2646 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2647 nd_opt_rh->nd_opt_rh_len = len >> 3;
2648 p += sizeof(*nd_opt_rh);
2649 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2651 /* connect m0 to m */
2652 m_tag_delete_chain(m0, NULL);
2653 m0->m_flags &= ~M_PKTHDR;
2655 m->m_pkthdr.len = m->m_len + m0->m_len;
2664 /* XXX: clear embedded link IDs in the inner header */
2665 in6_clearscope(&sip6->ip6_src);
2666 in6_clearscope(&sip6->ip6_dst);
2667 in6_clearscope(&nd_rd->nd_rd_target);
2668 in6_clearscope(&nd_rd->nd_rd_dst);
2670 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2672 nd_rd->nd_rd_cksum = 0;
2673 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2674 sizeof(*ip6), ntohs(ip6->ip6_plen));
2676 /* send the packet to outside... */
2677 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2679 icmp6_ifstat_inc(outif, ifs6_out_msg);
2680 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2682 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2694 * ICMPv6 socket option processing.
2697 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2701 struct inpcb *inp = sotoinpcb(so);
2702 int level, op, optname;
2705 level = sopt->sopt_level;
2706 op = sopt->sopt_dir;
2707 optname = sopt->sopt_name;
2708 optlen = sopt->sopt_valsize;
2710 level = op = optname = optlen = 0;
2712 if (level != IPPROTO_ICMPV6) {
2721 struct icmp6_filter ic6f;
2723 if (optlen != sizeof(ic6f)) {
2727 error = sooptcopyin(sopt, &ic6f, optlen, optlen);
2730 *inp->in6p_icmp6filt = ic6f;
2737 error = ENOPROTOOPT;
2746 struct icmp6_filter ic6f;
2749 ic6f = *inp->in6p_icmp6filt;
2751 error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
2756 error = ENOPROTOOPT;
2766 * Perform rate limit check.
2767 * Returns 0 if it is okay to send the icmp6 packet.
2768 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2771 * XXX per-destination/type check necessary?
2773 * dst - not used at this moment
2774 * type - not used at this moment
2775 * code - not used at this moment
2778 icmp6_ratelimit(const struct in6_addr *dst, const int type,
2783 ret = 0; /* okay to send */
2786 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2788 /* The packet is subject to rate limit */