2 /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
65 #include "opt_inet6.h"
66 #include "opt_ipsec.h"
68 #include <sys/param.h>
69 #include <sys/domain.h>
70 #include <sys/kernel.h>
72 #include <sys/malloc.h>
74 #include <sys/protosw.h>
75 #include <sys/signalvar.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
79 #include <sys/syslog.h>
80 #include <sys/systm.h>
84 #include <net/if_dl.h>
85 #include <net/if_types.h>
86 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet/tcp_var.h>
94 #include <netinet6/in6_ifattach.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/ip6protosw.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/scope6_var.h>
99 #include <netinet6/mld6_var.h>
100 #include <netinet6/nd6.h>
103 #include <netinet6/ipsec.h>
104 #include <netkey/key.h>
108 #include <netipsec/ipsec.h>
109 #include <netipsec/key.h>
112 #include <net/net_osdep.h>
114 extern struct domain inet6domain;
116 struct icmp6stat icmp6stat;
118 extern struct inpcbinfo ripcbinfo;
119 extern struct inpcbhead ripcb;
120 extern int icmp6errppslim;
121 static int icmp6errpps_count = 0;
122 static struct timeval icmp6errppslim_last;
123 extern int icmp6_nodeinfo;
125 static void icmp6_errcount __P((struct icmp6errstat *, int, int));
126 static int icmp6_rip6_input __P((struct mbuf **, int));
127 static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
128 static const char *icmp6_redirect_diag __P((struct in6_addr *,
129 struct in6_addr *, struct in6_addr *));
130 static struct mbuf *ni6_input __P((struct mbuf *, int));
131 static struct mbuf *ni6_nametodns __P((const char *, int, int));
132 static int ni6_dnsmatch __P((const char *, int, const char *, int));
133 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
134 struct ifnet **, struct in6_addr *));
135 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
136 struct ifnet *, int));
137 static int icmp6_notify_error __P((struct mbuf **, int, int, int));
147 icmp6_errcount(stat, type, code)
148 struct icmp6errstat *stat;
152 case ICMP6_DST_UNREACH:
154 case ICMP6_DST_UNREACH_NOROUTE:
155 stat->icp6errs_dst_unreach_noroute++;
157 case ICMP6_DST_UNREACH_ADMIN:
158 stat->icp6errs_dst_unreach_admin++;
160 case ICMP6_DST_UNREACH_BEYONDSCOPE:
161 stat->icp6errs_dst_unreach_beyondscope++;
163 case ICMP6_DST_UNREACH_ADDR:
164 stat->icp6errs_dst_unreach_addr++;
166 case ICMP6_DST_UNREACH_NOPORT:
167 stat->icp6errs_dst_unreach_noport++;
171 case ICMP6_PACKET_TOO_BIG:
172 stat->icp6errs_packet_too_big++;
174 case ICMP6_TIME_EXCEEDED:
176 case ICMP6_TIME_EXCEED_TRANSIT:
177 stat->icp6errs_time_exceed_transit++;
179 case ICMP6_TIME_EXCEED_REASSEMBLY:
180 stat->icp6errs_time_exceed_reassembly++;
184 case ICMP6_PARAM_PROB:
186 case ICMP6_PARAMPROB_HEADER:
187 stat->icp6errs_paramprob_header++;
189 case ICMP6_PARAMPROB_NEXTHEADER:
190 stat->icp6errs_paramprob_nextheader++;
192 case ICMP6_PARAMPROB_OPTION:
193 stat->icp6errs_paramprob_option++;
198 stat->icp6errs_redirect++;
201 stat->icp6errs_unknown++;
205 * A wrapper function for icmp6_error() necessary when the erroneous packet
206 * may not contain enough scope zone information.
209 icmp6_error2(m, type, code, param, ifp)
211 int type, code, param;
219 #ifndef PULLDOWN_TEST
220 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
222 if (m->m_len < sizeof(struct ip6_hdr)) {
223 m = m_pullup(m, sizeof(struct ip6_hdr));
229 ip6 = mtod(m, struct ip6_hdr *);
231 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
233 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
236 icmp6_error(m, type, code, param);
240 * Generate an error packet of type error in response to bad IP6 packet.
243 icmp6_error(m, type, code, param)
245 int type, code, param;
247 struct ip6_hdr *oip6, *nip6;
248 struct icmp6_hdr *icmp6;
253 icmp6stat.icp6s_error++;
255 /* count per-type-code statistics */
256 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
258 #ifdef M_DECRYPTED /*not openbsd*/
259 if (m->m_flags & M_DECRYPTED) {
260 icmp6stat.icp6s_canterror++;
265 #ifndef PULLDOWN_TEST
266 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
268 if (m->m_len < sizeof(struct ip6_hdr)) {
269 m = m_pullup(m, sizeof(struct ip6_hdr));
274 oip6 = mtod(m, struct ip6_hdr *);
277 * If the destination address of the erroneous packet is a multicast
278 * address, or the packet was sent using link-layer multicast,
279 * we should basically suppress sending an error (RFC 2463, Section
281 * We have two exceptions (the item e.2 in that section):
282 * - the Pakcet Too Big message can be sent for path MTU discovery.
283 * - the Parameter Problem Message that can be allowed an icmp6 error
284 * in the option type field. This check has been done in
285 * ip6_unknown_opt(), so we can just check the type and code.
287 if ((m->m_flags & (M_BCAST|M_MCAST) ||
288 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
289 (type != ICMP6_PACKET_TOO_BIG &&
290 (type != ICMP6_PARAM_PROB ||
291 code != ICMP6_PARAMPROB_OPTION)))
295 * RFC 2463, 2.4 (e.5): source address check.
296 * XXX: the case of anycast source?
298 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
299 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
303 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
307 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
308 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
309 struct icmp6_hdr *icp;
311 #ifndef PULLDOWN_TEST
312 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
313 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
315 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
318 icmp6stat.icp6s_tooshort++;
322 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
323 icp->icmp6_type == ND_REDIRECT) {
326 * Special case: for redirect (which is
327 * informational) we must not send icmp6 error.
329 icmp6stat.icp6s_canterror++;
332 /* ICMPv6 informational - send the error */
335 /* non-ICMPv6 - send the error */
338 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
340 /* Finally, do rate limitation check. */
341 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
342 icmp6stat.icp6s_toofreq++;
347 * OK, ICMP6 can be generated.
350 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
351 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
353 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
354 M_PREPEND(m, preplen, M_DONTWAIT);
355 if (m && m->m_len < preplen)
356 m = m_pullup(m, preplen);
358 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
362 nip6 = mtod(m, struct ip6_hdr *);
363 nip6->ip6_src = oip6->ip6_src;
364 nip6->ip6_dst = oip6->ip6_dst;
366 in6_clearscope(&oip6->ip6_src);
367 in6_clearscope(&oip6->ip6_dst);
369 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
370 icmp6->icmp6_type = type;
371 icmp6->icmp6_code = code;
372 icmp6->icmp6_pptr = htonl((u_int32_t)param);
375 * icmp6_reflect() is designed to be in the input path.
376 * icmp6_error() can be called from both input and output path,
377 * and if we are in output path rcvif could contain bogus value.
378 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
379 * information in ip header (nip6).
381 m->m_pkthdr.rcvif = NULL;
383 icmp6stat.icp6s_outhist[type]++;
384 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
390 * If we can't tell whether or not we can generate ICMP6, free it.
396 * Process a received ICMP6 message.
399 icmp6_input(mp, offp, proto)
403 struct mbuf *m = *mp, *n;
404 struct ip6_hdr *ip6, *nip6;
405 struct icmp6_hdr *icmp6, *nicmp6;
407 int icmp6len = m->m_pkthdr.len - *offp;
410 #ifndef PULLDOWN_TEST
411 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
412 /* m might change if M_LOOP. So, call mtod after this */
416 * Locate icmp6 structure in mbuf, and check
417 * that not corrupted and of at least minimum length
420 ip6 = mtod(m, struct ip6_hdr *);
421 if (icmp6len < sizeof(struct icmp6_hdr)) {
422 icmp6stat.icp6s_tooshort++;
427 * calculate the checksum
429 #ifndef PULLDOWN_TEST
430 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
432 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
434 icmp6stat.icp6s_tooshort++;
438 code = icmp6->icmp6_code;
440 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
442 "ICMP6 checksum error(%d|%x) %s\n",
443 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
444 icmp6stat.icp6s_checksum++;
448 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
450 * Deliver very specific ICMP6 type only.
451 * This is important to deliver TOOBIG. Otherwise PMTUD
454 switch (icmp6->icmp6_type) {
455 case ICMP6_DST_UNREACH:
456 case ICMP6_PACKET_TOO_BIG:
457 case ICMP6_TIME_EXCEEDED:
464 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
465 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
466 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
467 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
469 switch (icmp6->icmp6_type) {
470 case ICMP6_DST_UNREACH:
471 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
473 case ICMP6_DST_UNREACH_NOROUTE:
474 code = PRC_UNREACH_NET;
476 case ICMP6_DST_UNREACH_ADMIN:
477 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
478 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
480 case ICMP6_DST_UNREACH_ADDR:
483 case ICMP6_DST_UNREACH_BEYONDSCOPE:
484 /* I mean "source address was incorrect." */
485 code = PRC_PARAMPROB;
487 case ICMP6_DST_UNREACH_NOPORT:
488 code = PRC_UNREACH_PORT;
496 case ICMP6_PACKET_TOO_BIG:
497 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
499 /* validation is made in icmp6_mtudisc_update */
504 * Updating the path MTU will be done after examining
505 * intermediate extension headers.
510 case ICMP6_TIME_EXCEEDED:
511 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
513 case ICMP6_TIME_EXCEED_TRANSIT:
514 code = PRC_TIMXCEED_INTRANS;
516 case ICMP6_TIME_EXCEED_REASSEMBLY:
517 code = PRC_TIMXCEED_REASS;
525 case ICMP6_PARAM_PROB:
526 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
528 case ICMP6_PARAMPROB_NEXTHEADER:
529 code = PRC_UNREACH_PROTOCOL;
531 case ICMP6_PARAMPROB_HEADER:
532 case ICMP6_PARAMPROB_OPTION:
533 code = PRC_PARAMPROB;
541 case ICMP6_ECHO_REQUEST:
542 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
545 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
549 if ((n->m_flags & M_EXT) != 0
550 || n->m_len < off + sizeof(struct icmp6_hdr)) {
552 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
555 MGETHDR(n, M_DONTWAIT, n0->m_type);
556 n0len = n0->m_pkthdr.len; /* save for use below */
558 M_MOVE_PKTHDR(n, n0);
559 if (n && maxlen >= MHLEN) {
560 MCLGET(n, M_DONTWAIT);
561 if ((n->m_flags & M_EXT) == 0) {
572 * Copy IPv6 and ICMPv6 only.
574 nip6 = mtod(n, struct ip6_hdr *);
575 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
576 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
577 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
578 noff = sizeof(struct ip6_hdr);
579 /* new mbuf contains only ipv6+icmpv6 headers */
580 n->m_len = noff + sizeof(struct icmp6_hdr);
582 * Adjust mbuf. ip6_plen will be adjusted in
585 m_adj(n0, off + sizeof(struct icmp6_hdr));
586 /* recalculate complete packet size */
587 n->m_pkthdr.len = n0len + (noff - off);
590 nip6 = mtod(n, struct ip6_hdr *);
591 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
594 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
595 nicmp6->icmp6_code = 0;
597 icmp6stat.icp6s_reflect++;
598 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
599 icmp6_reflect(n, noff);
603 case ICMP6_ECHO_REPLY:
604 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
609 case MLD_LISTENER_QUERY:
610 case MLD_LISTENER_REPORT:
611 if (icmp6len < sizeof(struct mld_hdr))
613 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
614 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
616 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
617 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
627 case MLD_LISTENER_DONE:
628 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
629 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
631 break; /* nothing to be done in kernel */
633 case MLD_MTRACE_RESP:
635 /* XXX: these two are experimental. not officially defined. */
636 /* XXX: per-interface statistics? */
637 break; /* just pass it to applications */
639 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
641 enum { WRU, FQDN } mode;
646 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
648 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
653 #define hostnamelen strlen(hostname)
655 #ifndef PULLDOWN_TEST
656 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
659 n = m_copy(m, 0, M_COPYALL);
661 n = ni6_input(n, off);
662 /* XXX meaningless if n == NULL */
663 noff = sizeof(struct ip6_hdr);
668 if ((icmp6_nodeinfo & 5) != 5)
673 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
674 if (maxlen >= MCLBYTES) {
678 MGETHDR(n, M_DONTWAIT, m->m_type);
679 if (n && maxlen > MHLEN) {
680 MCLGET(n, M_DONTWAIT);
681 if ((n->m_flags & M_EXT) == 0) {
686 if (!m_dup_pkthdr(n, m, M_DONTWAIT)) {
688 * Previous code did a blind M_COPY_PKTHDR
689 * and said "just for rcvif". If true, then
690 * we could tolerate the dup failing (due to
691 * the deep copy of the tag chain). For now
692 * be conservative and just fail.
701 n->m_pkthdr.rcvif = NULL;
703 maxhlen = M_TRAILINGSPACE(n) - maxlen;
704 if (maxhlen > hostnamelen)
705 maxhlen = hostnamelen;
707 * Copy IPv6 and ICMPv6 only.
709 nip6 = mtod(n, struct ip6_hdr *);
710 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
711 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
712 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
713 p = (u_char *)(nicmp6 + 1);
715 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
716 noff = sizeof(struct ip6_hdr);
717 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
718 sizeof(struct icmp6_hdr) + 4 + maxhlen;
719 nicmp6->icmp6_type = ICMP6_WRUREPLY;
720 nicmp6->icmp6_code = 0;
724 icmp6stat.icp6s_reflect++;
725 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
726 icmp6_reflect(n, noff);
736 case ND_ROUTER_SOLICIT:
737 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
740 if (icmp6len < sizeof(struct nd_router_solicit))
742 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
744 nd6_rs_input(m, off, icmp6len);
748 nd6_rs_input(n, off, icmp6len);
752 case ND_ROUTER_ADVERT:
753 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
756 if (icmp6len < sizeof(struct nd_router_advert))
758 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
760 nd6_ra_input(m, off, icmp6len);
764 nd6_ra_input(n, off, icmp6len);
768 case ND_NEIGHBOR_SOLICIT:
769 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
772 if (icmp6len < sizeof(struct nd_neighbor_solicit))
774 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
776 nd6_ns_input(m, off, icmp6len);
780 nd6_ns_input(n, off, icmp6len);
784 case ND_NEIGHBOR_ADVERT:
785 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
788 if (icmp6len < sizeof(struct nd_neighbor_advert))
790 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
792 nd6_na_input(m, off, icmp6len);
796 nd6_na_input(n, off, icmp6len);
801 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
804 if (icmp6len < sizeof(struct nd_redirect))
806 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
808 icmp6_redirect_input(m, off);
812 icmp6_redirect_input(n, off);
816 case ICMP6_ROUTER_RENUMBERING:
817 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
818 code != ICMP6_ROUTER_RENUMBERING_RESULT)
820 if (icmp6len < sizeof(struct icmp6_router_renum))
826 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
827 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
828 ip6_sprintf(&ip6->ip6_dst),
829 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
830 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
831 /* ICMPv6 error: MUST deliver it by spec... */
835 /* ICMPv6 informational: MUST not deliver */
839 if (icmp6_notify_error(&m, off, icmp6len, code)) {
840 /* In this case, m should've been freed. */
841 return (IPPROTO_DONE);
846 icmp6stat.icp6s_badcode++;
850 icmp6stat.icp6s_badlen++;
854 /* deliver the packet to appropriate sockets */
855 icmp6_rip6_input(&m, *offp);
865 icmp6_notify_error(mp, off, icmp6len, code)
867 int off, icmp6len, code;
869 struct mbuf *m = *mp;
870 struct icmp6_hdr *icmp6;
871 struct ip6_hdr *eip6;
873 struct sockaddr_in6 icmp6src, icmp6dst;
875 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
876 icmp6stat.icp6s_tooshort++;
879 #ifndef PULLDOWN_TEST
880 IP6_EXTHDR_CHECK(m, off,
881 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
882 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
884 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
885 sizeof(*icmp6) + sizeof(struct ip6_hdr));
887 icmp6stat.icp6s_tooshort++;
891 eip6 = (struct ip6_hdr *)(icmp6 + 1);
893 /* Detect the upper level protocol */
895 void (*ctlfunc) __P((int, struct sockaddr *, void *));
896 u_int8_t nxt = eip6->ip6_nxt;
897 int eoff = off + sizeof(struct icmp6_hdr) +
898 sizeof(struct ip6_hdr);
899 struct ip6ctlparam ip6cp;
900 struct in6_addr *finaldst = NULL;
901 int icmp6type = icmp6->icmp6_type;
903 struct ip6_rthdr *rth;
904 struct ip6_rthdr0 *rth0;
907 while (1) { /* XXX: should avoid infinite loop explicitly? */
911 case IPPROTO_HOPOPTS:
912 case IPPROTO_DSTOPTS:
914 #ifndef PULLDOWN_TEST
915 IP6_EXTHDR_CHECK(m, 0,
916 eoff + sizeof(struct ip6_ext), -1);
917 eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
919 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
922 icmp6stat.icp6s_tooshort++;
927 if (nxt == IPPROTO_AH)
928 eoff += (eh->ip6e_len + 2) << 2;
930 eoff += (eh->ip6e_len + 1) << 3;
933 case IPPROTO_ROUTING:
935 * When the erroneous packet contains a
936 * routing header, we should examine the
937 * header to determine the final destination.
938 * Otherwise, we can't properly update
939 * information that depends on the final
940 * destination (e.g. path MTU).
942 #ifndef PULLDOWN_TEST
943 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
944 rth = (struct ip6_rthdr *)
945 (mtod(m, caddr_t) + eoff);
947 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
950 icmp6stat.icp6s_tooshort++;
954 rthlen = (rth->ip6r_len + 1) << 3;
956 * XXX: currently there is no
957 * officially defined type other
959 * Note that if the segment left field
960 * is 0, all intermediate hops must
963 if (rth->ip6r_segleft &&
964 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
967 #ifndef PULLDOWN_TEST
968 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
969 rth0 = (struct ip6_rthdr0 *)
970 (mtod(m, caddr_t) + eoff);
973 struct ip6_rthdr0 *, m,
976 icmp6stat.icp6s_tooshort++;
980 /* just ignore a bogus header */
981 if ((rth0->ip6r0_len % 2) == 0 &&
982 (hops = rth0->ip6r0_len/2))
983 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
988 case IPPROTO_FRAGMENT:
989 #ifndef PULLDOWN_TEST
990 IP6_EXTHDR_CHECK(m, 0, eoff +
991 sizeof(struct ip6_frag), -1);
992 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
995 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
998 icmp6stat.icp6s_tooshort++;
1003 * Data after a fragment header is meaningless
1004 * unless it is the first fragment, but
1005 * we'll go to the notify label for path MTU
1008 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1011 eoff += sizeof(struct ip6_frag);
1016 * This case includes ESP and the No Next
1017 * Header. In such cases going to the notify
1018 * label does not have any meaning
1019 * (i.e. ctlfunc will be NULL), but we go
1020 * anyway since we might have to update
1021 * path MTU information.
1027 #ifndef PULLDOWN_TEST
1028 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1030 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1031 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1032 if (icmp6 == NULL) {
1033 icmp6stat.icp6s_tooshort++;
1039 * retrieve parameters from the inner IPv6 header, and convert
1040 * them into sockaddr structures.
1041 * XXX: there is no guarantee that the source or destination
1042 * addresses of the inner packet are in the same scope as
1043 * the addresses of the icmp packet. But there is no other
1044 * way to determine the zone.
1046 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1048 bzero(&icmp6dst, sizeof(icmp6dst));
1049 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1050 icmp6dst.sin6_family = AF_INET6;
1051 if (finaldst == NULL)
1052 icmp6dst.sin6_addr = eip6->ip6_dst;
1054 icmp6dst.sin6_addr = *finaldst;
1055 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1057 bzero(&icmp6src, sizeof(icmp6src));
1058 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1059 icmp6src.sin6_family = AF_INET6;
1060 icmp6src.sin6_addr = eip6->ip6_src;
1061 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1063 icmp6src.sin6_flowinfo =
1064 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1066 if (finaldst == NULL)
1067 finaldst = &eip6->ip6_dst;
1069 ip6cp.ip6c_icmp6 = icmp6;
1070 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1071 ip6cp.ip6c_off = eoff;
1072 ip6cp.ip6c_finaldst = finaldst;
1073 ip6cp.ip6c_src = &icmp6src;
1074 ip6cp.ip6c_nxt = nxt;
1076 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1077 notifymtu = ntohl(icmp6->icmp6_mtu);
1078 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1079 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1082 ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
1083 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1085 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1098 icmp6_mtudisc_update(ip6cp, validated)
1099 struct ip6ctlparam *ip6cp;
1102 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1103 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1104 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1105 u_int mtu = ntohl(icmp6->icmp6_mtu);
1106 struct in_conninfo inc;
1110 * RFC2460 section 5, last paragraph.
1111 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1112 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1113 * due to packet translator in the middle.
1114 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1117 if (mtu < IPV6_MMTU)
1122 * we reject ICMPv6 too big with abnormally small value.
1123 * XXX what is the good definition of "abnormally small"?
1125 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1131 bzero(&inc, sizeof(inc));
1132 inc.inc_flags = 1; /* IPv6 */
1133 inc.inc6_faddr = *dst;
1134 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1137 if (mtu < tcp_maxmtu6(&inc)) {
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 *
1160 struct icmp6_nodeinfo *ni6, *nni6;
1161 struct mbuf *n = NULL;
1164 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1165 struct ni_reply_fqdn *fqdn;
1166 int addrs; /* for NI_QTYPE_NODEADDR */
1167 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1168 struct in6_addr in6_subj; /* subject address */
1169 struct ip6_hdr *ip6;
1170 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1172 struct in6_ifaddr *ia6 = NULL;
1174 ip6 = mtod(m, struct ip6_hdr *);
1175 #ifndef PULLDOWN_TEST
1176 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1178 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1180 /* m is already reclaimed */
1186 * Validate IPv6 destination address.
1188 * The Responder must discard the Query without further processing
1189 * unless it is one of the Responder's unicast or anycast addresses, or
1190 * a link-local scope multicast address which the Responder has joined.
1191 * [icmp-name-lookups-08, Section 4.]
1193 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1194 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1196 /* else it's a link-local multicast, fine */
1197 } else { /* unicast or anycast */
1198 if ((ia6 = ip6_getdstifaddr(m)) == NULL)
1199 goto bad; /* XXX impossible */
1201 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1202 !(icmp6_nodeinfo & 4)) {
1203 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1204 "a temporary address in %s:%d",
1205 __FILE__, __LINE__));
1210 /* validate query Subject field. */
1211 qtype = ntohs(ni6->ni_qtype);
1212 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1215 case NI_QTYPE_SUPTYPES:
1217 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1221 case NI_QTYPE_NODEADDR:
1222 case NI_QTYPE_IPV4ADDR:
1223 switch (ni6->ni_code) {
1224 case ICMP6_NI_SUBJ_IPV6:
1225 #if ICMP6_NI_SUBJ_IPV6 != 0
1229 * backward compatibility - try to accept 03 draft
1230 * format, where no Subject is present.
1232 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1237 #if ICMP6_NI_SUBJ_IPV6 != 0
1238 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1242 if (subjlen != sizeof(struct in6_addr))
1246 * Validate Subject address.
1248 * Not sure what exactly "address belongs to the node"
1249 * means in the spec, is it just unicast, or what?
1251 * At this moment we consider Subject address as
1252 * "belong to the node" if the Subject address equals
1253 * to the IPv6 destination address; validation for
1254 * IPv6 destination address should have done enough
1257 * We do not do proxy at this moment.
1259 /* m_pulldown instead of copy? */
1260 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1261 subjlen, (caddr_t)&in6_subj);
1262 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1265 subj = (char *)&in6_subj;
1266 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1270 * XXX if we are to allow other cases, we should really
1271 * be careful about scope here.
1272 * basically, we should disallow queries toward IPv6
1273 * destination X with subject Y,
1274 * if scope(X) > scope(Y).
1275 * if we allow scope(X) > scope(Y), it will result in
1276 * information leakage across scope boundary.
1280 case ICMP6_NI_SUBJ_FQDN:
1282 * Validate Subject name with gethostname(3).
1284 * The behavior may need some debate, since:
1285 * - we are not sure if the node has FQDN as
1286 * hostname (returned by gethostname(3)).
1287 * - the code does wildcard match for truncated names.
1288 * however, we are not sure if we want to perform
1289 * wildcard match, if gethostname(3) side has
1290 * truncated hostname.
1292 n = ni6_nametodns(hostname, hostnamelen, 0);
1293 if (!n || n->m_next || n->m_len == 0)
1295 IP6_EXTHDR_GET(subj, char *, m,
1296 off + sizeof(struct icmp6_nodeinfo), subjlen);
1299 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1307 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1314 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1317 if ((icmp6_nodeinfo & 1) == 0)
1320 case NI_QTYPE_NODEADDR:
1321 case NI_QTYPE_IPV4ADDR:
1322 if ((icmp6_nodeinfo & 2) == 0)
1327 /* guess reply length */
1330 break; /* no reply data */
1331 case NI_QTYPE_SUPTYPES:
1332 replylen += sizeof(u_int32_t);
1335 /* XXX will append an mbuf */
1336 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1338 case NI_QTYPE_NODEADDR:
1339 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1340 if ((replylen += addrs * (sizeof(struct in6_addr) +
1341 sizeof(u_int32_t))) > MCLBYTES)
1342 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1344 case NI_QTYPE_IPV4ADDR:
1345 /* unsupported - should respond with unknown Qtype? */
1349 * XXX: We must return a reply with the ICMP6 code
1350 * `unknown Qtype' in this case. However we regard the case
1351 * as an FQDN query for backward compatibility.
1352 * Older versions set a random value to this field,
1353 * so it rarely varies in the defined qtypes.
1354 * But the mechanism is not reliable...
1355 * maybe we should obsolete older versions.
1357 qtype = NI_QTYPE_FQDN;
1358 /* XXX will append an mbuf */
1359 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1364 /* allocate an mbuf to reply. */
1365 MGETHDR(n, M_DONTWAIT, m->m_type);
1370 M_MOVE_PKTHDR(n, m); /* just for recvif */
1371 if (replylen > MHLEN) {
1372 if (replylen > MCLBYTES) {
1374 * XXX: should we try to allocate more? But MCLBYTES
1375 * is probably much larger than IPV6_MMTU...
1379 MCLGET(n, M_DONTWAIT);
1380 if ((n->m_flags & M_EXT) == 0) {
1384 n->m_pkthdr.len = n->m_len = replylen;
1386 /* copy mbuf header and IPv6 + Node Information base headers */
1387 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1388 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1389 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1391 /* qtype dependent procedure */
1394 nni6->ni_code = ICMP6_NI_SUCCESS;
1397 case NI_QTYPE_SUPTYPES:
1400 nni6->ni_code = ICMP6_NI_SUCCESS;
1401 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1402 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1403 v = (u_int32_t)htonl(0x0000000f);
1404 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1408 nni6->ni_code = ICMP6_NI_SUCCESS;
1409 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1410 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1411 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1412 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1414 * XXX do we really have FQDN in variable "hostname"?
1416 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1417 if (n->m_next == NULL)
1419 /* XXX we assume that n->m_next is not a chain */
1420 if (n->m_next->m_next != NULL)
1422 n->m_pkthdr.len += n->m_next->m_len;
1424 case NI_QTYPE_NODEADDR:
1428 nni6->ni_code = ICMP6_NI_SUCCESS;
1429 n->m_pkthdr.len = n->m_len =
1430 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1431 lenlim = M_TRAILINGSPACE(n);
1432 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1433 /* XXX: reset mbuf length */
1434 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1435 sizeof(struct icmp6_nodeinfo) + copied;
1439 break; /* XXX impossible! */
1442 nni6->ni_type = ICMP6_NI_REPLY;
1455 * make a mbuf with DNS-encoded string. no compression support.
1457 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1458 * treated as truncated name (two \0 at the end). this is a wild guess.
1460 static struct mbuf *
1461 ni6_nametodns(name, namelen, old)
1464 int old; /* return pascal string if non-zero */
1476 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1477 MGET(m, M_DONTWAIT, MT_DATA);
1478 if (m && len > MLEN) {
1479 MCLGET(m, M_DONTWAIT);
1480 if ((m->m_flags & M_EXT) == 0)
1489 *mtod(m, char *) = namelen;
1490 bcopy(name, mtod(m, char *) + 1, namelen);
1494 cp = mtod(m, char *);
1495 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1497 /* if not certain about my name, return empty buffer */
1502 * guess if it looks like shortened hostname, or FQDN.
1503 * shortened hostname needs two trailing "\0".
1506 for (p = name; p < name + namelen; p++) {
1507 if (*p && *p == '.')
1516 while (cp < ep && p < name + namelen) {
1518 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1520 /* result does not fit into mbuf */
1521 if (cp + i + 1 >= ep)
1524 * DNS label length restriction, RFC1035 page 8.
1525 * "i == 0" case is included here to avoid returning
1526 * 0-length label on "foo..bar".
1528 if (i <= 0 || i >= 64)
1534 if (p < name + namelen && *p == '.')
1538 if (cp + nterm >= ep)
1542 m->m_len = cp - mtod(m, char *);
1546 panic("should not reach here");
1556 * check if two DNS-encoded string matches. takes care of truncated
1557 * form (with \0\0 at the end). no compression support.
1558 * XXX upper/lowercase match (see RFC2065)
1561 ni6_dnsmatch(a, alen, b, blen)
1567 const char *a0, *b0;
1570 /* simplest case - need validation? */
1571 if (alen == blen && bcmp(a, b, alen) == 0)
1577 /* termination is mandatory */
1578 if (alen < 2 || blen < 2)
1580 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1585 while (a - a0 < alen && b - b0 < blen) {
1586 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1589 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1591 /* we don't support compression yet */
1592 if (a[0] >= 64 || b[0] >= 64)
1595 /* truncated case */
1596 if (a[0] == 0 && a - a0 == alen - 1)
1598 if (b[0] == 0 && b - b0 == blen - 1)
1600 if (a[0] == 0 || b[0] == 0)
1606 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1608 if (bcmp(a + 1, b + 1, l) != 0)
1615 if (a - a0 == alen && b - b0 == blen)
1622 * calculate the number of addresses to be returned in the node info reply.
1625 ni6_addrs(ni6, m, ifpp, subj)
1626 struct icmp6_nodeinfo *ni6;
1628 struct ifnet **ifpp;
1629 struct in6_addr *subj;
1632 struct in6_ifaddr *ifa6;
1634 int addrs = 0, addrsofif, iffound = 0;
1635 int niflags = ni6->ni_flags;
1637 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1638 switch (ni6->ni_code) {
1639 case ICMP6_NI_SUBJ_IPV6:
1640 if (subj == NULL) /* must be impossible... */
1645 * XXX: we only support IPv6 subject address for
1653 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1655 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1656 if (ifa->ifa_addr->sa_family != AF_INET6)
1658 ifa6 = (struct in6_ifaddr *)ifa;
1660 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1661 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1665 * IPv4-mapped addresses can only be returned by a
1666 * Node Information proxy, since they represent
1667 * addresses of IPv4-only nodes, which perforce do
1668 * not implement this protocol.
1669 * [icmp-name-lookups-07, Section 5.4]
1670 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1671 * this function at this moment.
1674 /* What do we have to do about ::1? */
1675 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1676 case IPV6_ADDR_SCOPE_LINKLOCAL:
1677 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1680 case IPV6_ADDR_SCOPE_SITELOCAL:
1681 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1684 case IPV6_ADDR_SCOPE_GLOBAL:
1685 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1693 * check if anycast is okay.
1694 * XXX: just experimental. not in the spec.
1696 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1697 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1698 continue; /* we need only unicast addresses */
1699 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1700 (icmp6_nodeinfo & 4) == 0) {
1703 addrsofif++; /* count the address */
1719 ni6_store_addrs(ni6, nni6, ifp0, resid)
1720 struct icmp6_nodeinfo *ni6, *nni6;
1724 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1725 struct in6_ifaddr *ifa6;
1727 struct ifnet *ifp_dep = NULL;
1728 int copied = 0, allow_deprecated = 0;
1729 u_char *cp = (u_char *)(nni6 + 1);
1730 int niflags = ni6->ni_flags;
1733 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1734 return (0); /* needless to copy */
1739 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1740 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1741 ifa = ifa->ifa_list.tqe_next) {
1742 if (ifa->ifa_addr->sa_family != AF_INET6)
1744 ifa6 = (struct in6_ifaddr *)ifa;
1746 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1747 allow_deprecated == 0) {
1749 * prefererred address should be put before
1750 * deprecated addresses.
1753 /* record the interface for later search */
1754 if (ifp_dep == NULL)
1758 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1759 allow_deprecated != 0)
1760 continue; /* we now collect deprecated addrs */
1762 /* What do we have to do about ::1? */
1763 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1764 case IPV6_ADDR_SCOPE_LINKLOCAL:
1765 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1768 case IPV6_ADDR_SCOPE_SITELOCAL:
1769 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1772 case IPV6_ADDR_SCOPE_GLOBAL:
1773 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1781 * check if anycast is okay.
1782 * XXX: just experimental. not in the spec.
1784 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1785 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1787 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1788 (icmp6_nodeinfo & 4) == 0) {
1792 /* now we can copy the address */
1793 if (resid < sizeof(struct in6_addr) +
1794 sizeof(u_int32_t)) {
1796 * We give up much more copy.
1797 * Set the truncate flag and return.
1799 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1805 * Set the TTL of the address.
1806 * The TTL value should be one of the following
1807 * according to the specification:
1809 * 1. The remaining lifetime of a DHCP lease on the
1811 * 2. The remaining Valid Lifetime of a prefix from
1812 * which the address was derived through Stateless
1813 * Autoconfiguration.
1815 * Note that we currently do not support stateful
1816 * address configuration by DHCPv6, so the former
1817 * case can't happen.
1819 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1820 ltime = ND6_INFINITE_LIFETIME;
1822 if (ifa6->ia6_lifetime.ia6t_expire >
1824 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1829 bcopy(<ime, cp, sizeof(u_int32_t));
1830 cp += sizeof(u_int32_t);
1832 /* copy the address itself */
1833 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1834 sizeof(struct in6_addr));
1835 in6_clearscope((struct in6_addr *)cp); /* XXX */
1836 cp += sizeof(struct in6_addr);
1838 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1839 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1841 if (ifp0) /* we need search only on the specified IF */
1845 if (allow_deprecated == 0 && ifp_dep != NULL) {
1847 allow_deprecated = 1;
1858 * XXX almost dup'ed code with rip6_input.
1861 icmp6_rip6_input(mp, off)
1865 struct mbuf *m = *mp;
1866 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1867 struct in6pcb *in6p;
1868 struct in6pcb *last = NULL;
1869 struct sockaddr_in6 fromsa;
1870 struct icmp6_hdr *icmp6;
1871 struct mbuf *opts = NULL;
1873 #ifndef PULLDOWN_TEST
1874 /* this is assumed to be safe. */
1875 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1877 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1878 if (icmp6 == NULL) {
1879 /* m is already reclaimed */
1880 return (IPPROTO_DONE);
1885 * XXX: the address may have embedded scope zone ID, which should be
1886 * hidden from applications.
1888 bzero(&fromsa, sizeof(fromsa));
1889 fromsa.sin6_family = AF_INET6;
1890 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1891 fromsa.sin6_addr = ip6->ip6_src;
1892 if (sa6_recoverscope(&fromsa)) {
1894 return (IPPROTO_DONE);
1897 INP_INFO_RLOCK(&ripcbinfo);
1898 LIST_FOREACH(in6p, &ripcb, inp_list) {
1900 if ((in6p->inp_vflag & INP_IPV6) == 0) {
1905 if (in6p->in6p_ip6_nxt != 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))
1913 if (in6p->in6p_icmp6filt
1914 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1915 in6p->in6p_icmp6filt))
1918 struct mbuf *n = NULL;
1921 * Recent network drivers tend to allocate a single
1922 * mbuf cluster, rather than to make a couple of
1923 * mbufs without clusters. Also, since the IPv6 code
1924 * path tries to avoid m_pullup(), it is highly
1925 * probable that we still have an mbuf cluster here
1926 * even though the necessary length can be stored in an
1927 * mbuf's internal buffer.
1928 * Meanwhile, the default size of the receive socket
1929 * buffer for raw sockets is not so large. This means
1930 * the possibility of packet loss is relatively higher
1931 * than before. To avoid this scenario, we copy the
1932 * received data to a separate mbuf that does not use
1933 * a cluster, if possible.
1934 * XXX: it is better to copy the data after stripping
1935 * intermediate headers.
1937 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1938 m->m_len <= MHLEN) {
1939 MGET(n, M_DONTWAIT, m->m_type);
1941 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1942 bcopy(m->m_data, n->m_data,
1944 n->m_len = m->m_len;
1952 (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1953 if (last->in6p_flags & IN6P_CONTROLOPTS)
1954 ip6_savecontrol(last, n, &opts);
1955 /* strip intermediate headers */
1957 if (sbappendaddr(&last->in6p_socket->so_rcv,
1958 (struct sockaddr *)&fromsa, n, opts)
1960 /* should notify about lost packet */
1966 sorwakeup(last->in6p_socket);
1974 if (last->in6p_flags & IN6P_CONTROLOPTS)
1975 ip6_savecontrol(last, m, &opts);
1976 /* strip intermediate headers */
1979 /* avoid using mbuf clusters if possible (see above) */
1980 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1981 m->m_len <= MHLEN) {
1984 MGET(n, M_DONTWAIT, m->m_type);
1986 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1987 bcopy(m->m_data, n->m_data, m->m_len);
1988 n->m_len = m->m_len;
1998 if (sbappendaddr(&last->in6p_socket->so_rcv,
1999 (struct sockaddr *)&fromsa, m, opts) == 0) {
2004 sorwakeup(last->in6p_socket);
2008 ip6stat.ip6s_delivered--;
2010 INP_INFO_RUNLOCK(&ripcbinfo);
2011 return IPPROTO_DONE;
2015 * Reflect the ip6 packet back to the source.
2016 * OFF points to the icmp6 header, counted from the top of the mbuf.
2019 icmp6_reflect(m, off)
2023 struct ip6_hdr *ip6;
2024 struct icmp6_hdr *icmp6;
2025 struct in6_ifaddr *ia;
2028 struct ifnet *outif = NULL;
2029 struct in6_addr origdst, *src = NULL;
2031 /* too short to reflect */
2032 if (off < sizeof(struct ip6_hdr)) {
2034 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2035 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2036 __FILE__, __LINE__));
2041 * If there are extra headers between IPv6 and ICMPv6, strip
2042 * off that header first.
2045 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2046 panic("assumption failed in icmp6_reflect");
2048 if (off > sizeof(struct ip6_hdr)) {
2050 struct ip6_hdr nip6;
2052 l = off - sizeof(struct ip6_hdr);
2053 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2055 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2057 if ((m = m_pullup(m, l)) == NULL)
2060 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2061 } else /* off == sizeof(struct ip6_hdr) */ {
2063 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2065 if ((m = m_pullup(m, l)) == NULL)
2069 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2070 ip6 = mtod(m, struct ip6_hdr *);
2071 ip6->ip6_nxt = IPPROTO_ICMPV6;
2072 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2073 type = icmp6->icmp6_type; /* keep type for statistics */
2074 code = icmp6->icmp6_code; /* ditto. */
2076 origdst = ip6->ip6_dst;
2078 * ip6_input() drops a packet if its src is multicast.
2079 * So, the src is never multicast.
2081 ip6->ip6_dst = ip6->ip6_src;
2084 * If the incoming packet was addressed directly to us (i.e. unicast),
2085 * use dst as the src for the reply.
2086 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2087 * (for example) when we encounter an error while forwarding procedure
2088 * destined to a duplicated address of ours.
2089 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2090 * procedure of an outgoing packet of our own, in which case we need
2091 * to search in the ifaddr list.
2093 if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
2094 if ((ia = ip6_getdstifaddr(m))) {
2095 if (!(ia->ia6_flags &
2096 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2097 src = &ia->ia_addr.sin6_addr;
2099 struct sockaddr_in6 d;
2101 bzero(&d, sizeof(d));
2102 d.sin6_family = AF_INET6;
2103 d.sin6_len = sizeof(d);
2104 d.sin6_addr = origdst;
2105 ia = (struct in6_ifaddr *)
2106 ifa_ifwithaddr((struct sockaddr *)&d);
2109 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2110 src = &ia->ia_addr.sin6_addr;
2117 struct sockaddr_in6 sin6;
2118 struct route_in6 ro;
2121 * This case matches to multicasts, our anycast, or unicasts
2122 * that we do not own. Select a source address based on the
2123 * source address of the erroneous packet.
2125 bzero(&sin6, sizeof(sin6));
2126 sin6.sin6_family = AF_INET6;
2127 sin6.sin6_len = sizeof(sin6);
2128 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2130 bzero(&ro, sizeof(ro));
2131 src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e);
2133 RTFREE(ro.ro_rt); /* XXX: we could use this */
2136 "icmp6_reflect: source can't be determined: "
2137 "dst=%s, error=%d\n",
2138 ip6_sprintf(&sin6.sin6_addr), e));
2143 ip6->ip6_src = *src;
2145 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2146 ip6->ip6_vfc |= IPV6_VERSION;
2147 ip6->ip6_nxt = IPPROTO_ICMPV6;
2149 ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2150 else if (m->m_pkthdr.rcvif) {
2151 /* XXX: This may not be the outgoing interface */
2152 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2154 ip6->ip6_hlim = ip6_defhlim;
2156 icmp6->icmp6_cksum = 0;
2157 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2158 sizeof(struct ip6_hdr), plen);
2161 * XXX option handling
2164 m->m_flags &= ~(M_BCAST|M_MCAST);
2166 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2168 icmp6_ifoutstat_inc(outif, type, code);
2185 icmp6_redirect_diag(src6, dst6, tgt6)
2186 struct in6_addr *src6;
2187 struct in6_addr *dst6;
2188 struct in6_addr *tgt6;
2190 static char buf[1024];
2191 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2192 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2197 icmp6_redirect_input(m, off)
2201 struct ifnet *ifp = m->m_pkthdr.rcvif;
2202 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2203 struct nd_redirect *nd_rd;
2204 int icmp6len = ntohs(ip6->ip6_plen);
2205 char *lladdr = NULL;
2207 u_char *redirhdr = NULL;
2208 int redirhdrlen = 0;
2209 struct rtentry *rt = NULL;
2212 struct in6_addr src6 = ip6->ip6_src;
2213 struct in6_addr redtgt6;
2214 struct in6_addr reddst6;
2215 union nd_opts ndopts;
2220 /* XXX if we are router, we don't update route by icmp6 redirect */
2223 if (!icmp6_rediraccept)
2226 #ifndef PULLDOWN_TEST
2227 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2228 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2230 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2231 if (nd_rd == NULL) {
2232 icmp6stat.icp6s_tooshort++;
2236 redtgt6 = nd_rd->nd_rd_target;
2237 reddst6 = nd_rd->nd_rd_dst;
2239 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2240 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2245 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2247 "ICMP6 redirect sent from %s rejected; "
2248 "must be from linklocal\n",
2249 ip6_sprintf(&src6)));
2252 if (ip6->ip6_hlim != 255) {
2254 "ICMP6 redirect sent from %s rejected; "
2255 "hlim=%d (must be 255)\n",
2256 ip6_sprintf(&src6), ip6->ip6_hlim));
2260 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2261 struct sockaddr_in6 sin6;
2262 struct in6_addr *gw6;
2264 bzero(&sin6, sizeof(sin6));
2265 sin6.sin6_family = AF_INET6;
2266 sin6.sin6_len = sizeof(struct sockaddr_in6);
2267 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2268 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
2270 if (rt->rt_gateway == NULL ||
2271 rt->rt_gateway->sa_family != AF_INET6) {
2273 "ICMP6 redirect rejected; no route "
2274 "with inet6 gateway found for redirect dst: %s\n",
2275 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2280 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2281 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2283 "ICMP6 redirect rejected; "
2284 "not equal to gw-for-src=%s (must be same): "
2287 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2293 "ICMP6 redirect rejected; "
2294 "no route found for redirect dst: %s\n",
2295 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2301 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2303 "ICMP6 redirect rejected; "
2304 "redirect dst must be unicast: %s\n",
2305 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2309 is_router = is_onlink = 0;
2310 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2311 is_router = 1; /* router case */
2312 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2313 is_onlink = 1; /* on-link destination case */
2314 if (!is_router && !is_onlink) {
2316 "ICMP6 redirect rejected; "
2317 "neither router case nor onlink case: %s\n",
2318 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2321 /* validation passed */
2323 icmp6len -= sizeof(*nd_rd);
2324 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2325 if (nd6_options(&ndopts) < 0) {
2326 nd6log((LOG_INFO, "icmp6_redirect_input: "
2327 "invalid ND option, rejected: %s\n",
2328 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2329 /* nd6_options have incremented stats */
2333 if (ndopts.nd_opts_tgt_lladdr) {
2334 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2335 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2338 if (ndopts.nd_opts_rh) {
2339 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2340 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2343 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2345 "icmp6_redirect_input: lladdrlen mismatch for %s "
2346 "(if %d, icmp6 packet %d): %s\n",
2347 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2348 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2353 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2354 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2356 if (!is_onlink) { /* better router case. perform rtredirect. */
2357 /* perform rtredirect */
2358 struct sockaddr_in6 sdst;
2359 struct sockaddr_in6 sgw;
2360 struct sockaddr_in6 ssrc;
2362 bzero(&sdst, sizeof(sdst));
2363 bzero(&sgw, sizeof(sgw));
2364 bzero(&ssrc, sizeof(ssrc));
2365 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2366 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2367 sizeof(struct sockaddr_in6);
2368 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2369 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2370 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2371 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2372 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2373 (struct sockaddr *)&ssrc);
2375 /* finally update cached route in each socket via pfctlinput */
2377 struct sockaddr_in6 sdst;
2379 bzero(&sdst, sizeof(sdst));
2380 sdst.sin6_family = AF_INET6;
2381 sdst.sin6_len = sizeof(struct sockaddr_in6);
2382 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2383 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2384 #if defined(IPSEC) || defined(FAST_IPSEC)
2385 key_sa_routechange((struct sockaddr *)&sdst);
2394 icmp6stat.icp6s_badredirect++;
2399 icmp6_redirect_output(m0, rt)
2403 struct ifnet *ifp; /* my outgoing interface */
2404 struct in6_addr *ifp_ll6;
2405 struct in6_addr *router_ll6;
2406 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2407 struct mbuf *m = NULL; /* newly allocated one */
2408 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2409 struct nd_redirect *nd_rd;
2412 struct ifnet *outif = NULL;
2413 struct sockaddr_in6 src_sa;
2415 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2417 /* if we are not router, we don't send icmp6 redirect */
2418 if (!ip6_forwarding)
2422 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2427 * the source address must identify a neighbor, and
2428 * the destination address must not be a multicast address
2429 * [RFC 2461, sec 8.2]
2431 sip6 = mtod(m0, struct ip6_hdr *);
2432 bzero(&src_sa, sizeof(src_sa));
2433 src_sa.sin6_family = AF_INET6;
2434 src_sa.sin6_len = sizeof(src_sa);
2435 src_sa.sin6_addr = sip6->ip6_src;
2436 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2438 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2439 goto fail; /* what should we do here? */
2442 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2446 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2447 * we almost always ask for an mbuf cluster for simplicity.
2448 * (MHLEN < IPV6_MMTU is almost always true)
2450 #if IPV6_MMTU >= MCLBYTES
2451 # error assumption failed about IPV6_MMTU and MCLBYTES
2453 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2454 if (m && IPV6_MMTU >= MHLEN)
2455 MCLGET(m, M_DONTWAIT);
2458 m->m_pkthdr.rcvif = NULL;
2460 maxlen = M_TRAILINGSPACE(m);
2461 maxlen = min(IPV6_MMTU, maxlen);
2462 /* just for safety */
2463 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2464 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2469 /* get ip6 linklocal address for ifp(my outgoing interface). */
2470 struct in6_ifaddr *ia;
2471 if ((ia = in6ifa_ifpforlinklocal(ifp,
2473 IN6_IFF_ANYCAST)) == NULL)
2475 ifp_ll6 = &ia->ia_addr.sin6_addr;
2478 /* get ip6 linklocal address for the router. */
2479 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2480 struct sockaddr_in6 *sin6;
2481 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2482 router_ll6 = &sin6->sin6_addr;
2483 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2484 router_ll6 = (struct in6_addr *)NULL;
2486 router_ll6 = (struct in6_addr *)NULL;
2489 ip6 = mtod(m, struct ip6_hdr *);
2491 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2492 ip6->ip6_vfc |= IPV6_VERSION;
2493 /* ip6->ip6_plen will be set later */
2494 ip6->ip6_nxt = IPPROTO_ICMPV6;
2495 ip6->ip6_hlim = 255;
2496 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2497 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2498 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2501 nd_rd = (struct nd_redirect *)(ip6 + 1);
2502 nd_rd->nd_rd_type = ND_REDIRECT;
2503 nd_rd->nd_rd_code = 0;
2504 nd_rd->nd_rd_reserved = 0;
2505 if (rt->rt_flags & RTF_GATEWAY) {
2507 * nd_rd->nd_rd_target must be a link-local address in
2508 * better router cases.
2512 bcopy(router_ll6, &nd_rd->nd_rd_target,
2513 sizeof(nd_rd->nd_rd_target));
2514 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2515 sizeof(nd_rd->nd_rd_dst));
2517 /* make sure redtgt == reddst */
2518 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2519 sizeof(nd_rd->nd_rd_target));
2520 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2521 sizeof(nd_rd->nd_rd_dst));
2524 p = (u_char *)(nd_rd + 1);
2530 /* target lladdr option */
2531 struct rtentry *rt_router = NULL;
2533 struct sockaddr_dl *sdl;
2534 struct nd_opt_hdr *nd_opt;
2537 rt_router = nd6_lookup(router_ll6, 0, ifp);
2540 len = sizeof(*nd_opt) + ifp->if_addrlen;
2541 len = (len + 7) & ~7; /* round by 8 */
2543 if (len + (p - (u_char *)ip6) > maxlen)
2545 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2546 (rt_router->rt_flags & RTF_LLINFO) &&
2547 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2548 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2550 nd_opt = (struct nd_opt_hdr *)p;
2551 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2552 nd_opt->nd_opt_len = len >> 3;
2553 lladdr = (char *)(nd_opt + 1);
2554 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2560 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2562 /* just to be safe */
2563 #ifdef M_DECRYPTED /*not openbsd*/
2564 if (m0->m_flags & M_DECRYPTED)
2567 if (p - (u_char *)ip6 > maxlen)
2571 /* redirected header option */
2573 struct nd_opt_rd_hdr *nd_opt_rh;
2576 * compute the maximum size for icmp6 redirect header option.
2577 * XXX room for auth header?
2579 len = maxlen - (p - (u_char *)ip6);
2582 /* This is just for simplicity. */
2583 if (m0->m_pkthdr.len != m0->m_len) {
2585 m_freem(m0->m_next);
2588 m0->m_pkthdr.len = m0->m_len;
2592 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2593 * about padding/truncate rule for the original IP packet.
2594 * From the discussion on IPv6imp in Feb 1999,
2595 * the consensus was:
2596 * - "attach as much as possible" is the goal
2597 * - pad if not aligned (original size can be guessed by
2598 * original ip6 header)
2599 * Following code adds the padding if it is simple enough,
2600 * and truncates if not.
2602 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2603 panic("assumption failed in %s:%d", __FILE__,
2606 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2607 /* not enough room, truncate */
2608 m0->m_pkthdr.len = m0->m_len = len -
2611 /* enough room, pad or truncate */
2614 extra = m0->m_pkthdr.len % 8;
2616 /* pad if easy enough, truncate if not */
2617 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2619 m0->m_len += (8 - extra);
2620 m0->m_pkthdr.len += (8 - extra);
2623 m0->m_pkthdr.len -= extra;
2627 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2628 m0->m_pkthdr.len = m0->m_len = len -
2632 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2633 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2634 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2635 nd_opt_rh->nd_opt_rh_len = len >> 3;
2636 p += sizeof(*nd_opt_rh);
2637 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2639 /* connect m0 to m */
2640 m_tag_delete_chain(m0, NULL);
2641 m0->m_flags &= ~M_PKTHDR;
2643 m->m_pkthdr.len = m->m_len + m0->m_len;
2652 /* XXX: clear embedded link IDs in the inner header */
2653 in6_clearscope(&sip6->ip6_src);
2654 in6_clearscope(&sip6->ip6_dst);
2655 in6_clearscope(&nd_rd->nd_rd_target);
2656 in6_clearscope(&nd_rd->nd_rd_dst);
2658 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2660 nd_rd->nd_rd_cksum = 0;
2661 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2662 sizeof(*ip6), ntohs(ip6->ip6_plen));
2664 /* send the packet to outside... */
2665 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2667 icmp6_ifstat_inc(outif, ifs6_out_msg);
2668 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2670 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2682 * ICMPv6 socket option processing.
2685 icmp6_ctloutput(so, sopt)
2687 struct sockopt *sopt;
2691 struct inpcb *inp = sotoinpcb(so);
2692 int level, op, optname;
2695 level = sopt->sopt_level;
2696 op = sopt->sopt_dir;
2697 optname = sopt->sopt_name;
2698 optlen = sopt->sopt_valsize;
2700 level = op = optname = optlen = 0;
2702 if (level != IPPROTO_ICMPV6) {
2711 struct icmp6_filter *p;
2713 if (optlen != sizeof(*p)) {
2717 if (inp->in6p_icmp6filt == NULL) {
2721 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2727 error = ENOPROTOOPT;
2736 if (inp->in6p_icmp6filt == NULL) {
2740 error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2741 sizeof(struct icmp6_filter));
2746 error = ENOPROTOOPT;
2756 * Perform rate limit check.
2757 * Returns 0 if it is okay to send the icmp6 packet.
2758 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2761 * XXX per-destination/type check necessary?
2764 icmp6_ratelimit(dst, type, code)
2765 const struct in6_addr *dst; /* not used at this moment */
2766 const int type; /* not used at this moment */
2767 const int code; /* not used at this moment */
2771 ret = 0; /* okay to send */
2774 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2776 /* The packet is subject to rate limit */