1 /* $FreeBSD: src/sys/netinet6/icmp6.c,v 1.6.2.13 2003/05/06 06:46:58 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/icmp6.c,v 1.2 2003/06/17 04:28:51 dillon Exp $ */
3 /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
81 #include <sys/kernel.h>
82 #include <sys/syslog.h>
83 #include <sys/domain.h>
86 #include <net/route.h>
87 #include <net/if_dl.h>
88 #include <net/if_types.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet/icmp6.h>
95 #include <netinet6/mld6_var.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet6/nd6.h>
99 #include <netinet6/in6_ifattach.h>
100 #include <netinet6/ip6protosw.h>
103 #include <netinet6/ipsec.h>
104 #include <netkey/key.h>
108 #include <netipsec/ipsec.h>
109 #include <netipsec/key.h>
113 #include <net/net_osdep.h>
115 #ifdef HAVE_NRL_INPCB
118 #define in6p_laddr inp_laddr6
119 #define in6p_faddr inp_faddr6
120 #define in6p_icmp6filt inp_icmp6filt
121 #define in6p_route inp_route
122 #define in6p_socket inp_socket
123 #define in6p_flags inp_flags
124 #define in6p_moptions inp_moptions6
125 #define in6p_outputopts inp_outputopts6
126 #define in6p_ip6 inp_ipv6
127 #define in6p_flowinfo inp_flowinfo
128 #define in6p_sp inp_sp
129 #define in6p_next inp_next
130 #define in6p_prev inp_prev
132 #define sotoin6pcb sotoinpcb
134 #define in6_pcbdetach in_pcbdetach
135 #define in6_rtchange in_rtchange
138 * for KAME src sync over BSD*'s. XXX: FreeBSD (>=3) are VERY different from
141 #define in6p_ip6_nxt inp_ipv6.ip6_nxt
144 extern struct domain inet6domain;
145 extern struct ip6protosw inet6sw[];
146 extern u_char ip6_protox[];
148 struct icmp6stat icmp6stat;
150 extern struct inpcbhead ripcb;
151 extern int icmp6errppslim;
152 static int icmp6errpps_count = 0;
153 static struct timeval icmp6errppslim_last;
154 extern int icmp6_nodeinfo;
156 static void icmp6_errcount __P((struct icmp6errstat *, int, int));
157 static int icmp6_rip6_input __P((struct mbuf **, int));
158 static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
159 static const char *icmp6_redirect_diag __P((struct in6_addr *,
160 struct in6_addr *, struct in6_addr *));
161 #ifndef HAVE_PPSRATECHECK
162 static int ppsratecheck __P((struct timeval *, int *, int));
164 static struct mbuf *ni6_input __P((struct mbuf *, int));
165 static struct mbuf *ni6_nametodns __P((const char *, int, int));
166 static int ni6_dnsmatch __P((const char *, int, const char *, int));
167 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
168 struct ifnet **, char *));
169 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
170 struct ifnet *, int));
171 static int icmp6_notify_error __P((struct mbuf *, int, int, int));
173 #ifdef COMPAT_RFC1885
174 static struct route_in6 icmp6_reflect_rt;
185 icmp6_errcount(stat, type, code)
186 struct icmp6errstat *stat;
190 case ICMP6_DST_UNREACH:
192 case ICMP6_DST_UNREACH_NOROUTE:
193 stat->icp6errs_dst_unreach_noroute++;
195 case ICMP6_DST_UNREACH_ADMIN:
196 stat->icp6errs_dst_unreach_admin++;
198 case ICMP6_DST_UNREACH_BEYONDSCOPE:
199 stat->icp6errs_dst_unreach_beyondscope++;
201 case ICMP6_DST_UNREACH_ADDR:
202 stat->icp6errs_dst_unreach_addr++;
204 case ICMP6_DST_UNREACH_NOPORT:
205 stat->icp6errs_dst_unreach_noport++;
209 case ICMP6_PACKET_TOO_BIG:
210 stat->icp6errs_packet_too_big++;
212 case ICMP6_TIME_EXCEEDED:
214 case ICMP6_TIME_EXCEED_TRANSIT:
215 stat->icp6errs_time_exceed_transit++;
217 case ICMP6_TIME_EXCEED_REASSEMBLY:
218 stat->icp6errs_time_exceed_reassembly++;
222 case ICMP6_PARAM_PROB:
224 case ICMP6_PARAMPROB_HEADER:
225 stat->icp6errs_paramprob_header++;
227 case ICMP6_PARAMPROB_NEXTHEADER:
228 stat->icp6errs_paramprob_nextheader++;
230 case ICMP6_PARAMPROB_OPTION:
231 stat->icp6errs_paramprob_option++;
236 stat->icp6errs_redirect++;
239 stat->icp6errs_unknown++;
243 * Generate an error packet of type error in response to bad IP6 packet.
246 icmp6_error(m, type, code, param)
248 int type, code, param;
250 struct ip6_hdr *oip6, *nip6;
251 struct icmp6_hdr *icmp6;
256 icmp6stat.icp6s_error++;
258 /* count per-type-code statistics */
259 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
261 #ifdef M_DECRYPTED /*not openbsd*/
262 if (m->m_flags & M_DECRYPTED) {
263 icmp6stat.icp6s_canterror++;
268 #ifndef PULLDOWN_TEST
269 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
271 if (m->m_len < sizeof(struct ip6_hdr)) {
272 m = m_pullup(m, sizeof(struct ip6_hdr));
277 oip6 = mtod(m, struct ip6_hdr *);
280 * Multicast destination check. For unrecognized option errors,
281 * this check has already done in ip6_unknown_opt(), so we can
282 * check only for other errors.
284 if ((m->m_flags & (M_BCAST|M_MCAST) ||
285 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
286 (type != ICMP6_PACKET_TOO_BIG &&
287 (type != ICMP6_PARAM_PROB ||
288 code != ICMP6_PARAMPROB_OPTION)))
291 /* Source address check. XXX: the case of anycast source? */
292 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
293 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
297 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
301 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
302 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
303 struct icmp6_hdr *icp;
305 #ifndef PULLDOWN_TEST
306 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
307 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
309 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
312 icmp6stat.icp6s_tooshort++;
316 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
317 icp->icmp6_type == ND_REDIRECT) {
320 * Special case: for redirect (which is
321 * informational) we must not send icmp6 error.
323 icmp6stat.icp6s_canterror++;
326 /* ICMPv6 informational - send the error */
329 /* non-ICMPv6 - send the error */
332 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
334 /* Finally, do rate limitation check. */
335 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
336 icmp6stat.icp6s_toofreq++;
341 * OK, ICMP6 can be generated.
344 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
345 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
347 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
348 M_PREPEND(m, preplen, M_DONTWAIT);
349 if (m && m->m_len < preplen)
350 m = m_pullup(m, preplen);
352 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
356 nip6 = mtod(m, struct ip6_hdr *);
357 nip6->ip6_src = oip6->ip6_src;
358 nip6->ip6_dst = oip6->ip6_dst;
360 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
361 oip6->ip6_src.s6_addr16[1] = 0;
362 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
363 oip6->ip6_dst.s6_addr16[1] = 0;
365 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
366 icmp6->icmp6_type = type;
367 icmp6->icmp6_code = code;
368 icmp6->icmp6_pptr = htonl((u_int32_t)param);
371 * icmp6_reflect() is designed to be in the input path.
372 * icmp6_error() can be called from both input and outut path,
373 * and if we are in output path rcvif could contain bogus value.
374 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
375 * information in ip header (nip6).
377 m->m_pkthdr.rcvif = NULL;
379 icmp6stat.icp6s_outhist[type]++;
380 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
386 * If we can't tell wheter or not we can generate ICMP6, free it.
392 * Process a received ICMP6 message.
395 icmp6_input(mp, offp, proto)
399 struct mbuf *m = *mp, *n;
400 struct ip6_hdr *ip6, *nip6;
401 struct icmp6_hdr *icmp6, *nicmp6;
403 int icmp6len = m->m_pkthdr.len - *offp;
406 #ifndef PULLDOWN_TEST
407 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
408 /* m might change if M_LOOP. So, call mtod after this */
412 * Locate icmp6 structure in mbuf, and check
413 * that not corrupted and of at least minimum length
416 ip6 = mtod(m, struct ip6_hdr *);
417 if (icmp6len < sizeof(struct icmp6_hdr)) {
418 icmp6stat.icp6s_tooshort++;
423 * calculate the checksum
425 #ifndef PULLDOWN_TEST
426 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
428 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
430 icmp6stat.icp6s_tooshort++;
434 code = icmp6->icmp6_code;
436 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
438 "ICMP6 checksum error(%d|%x) %s\n",
439 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
440 icmp6stat.icp6s_checksum++;
444 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
446 * Deliver very specific ICMP6 type only.
447 * This is important to deilver TOOBIG. Otherwise PMTUD
450 switch (icmp6->icmp6_type) {
451 case ICMP6_DST_UNREACH:
452 case ICMP6_PACKET_TOO_BIG:
453 case ICMP6_TIME_EXCEEDED:
460 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
461 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
462 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
463 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
465 switch (icmp6->icmp6_type) {
466 case ICMP6_DST_UNREACH:
467 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
469 case ICMP6_DST_UNREACH_NOROUTE:
470 code = PRC_UNREACH_NET;
472 case ICMP6_DST_UNREACH_ADMIN:
473 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
474 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
476 case ICMP6_DST_UNREACH_ADDR:
479 #ifdef COMPAT_RFC1885
480 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
481 code = PRC_UNREACH_SRCFAIL;
484 case ICMP6_DST_UNREACH_BEYONDSCOPE:
485 /* I mean "source address was incorrect." */
486 code = PRC_PARAMPROB;
489 case ICMP6_DST_UNREACH_NOPORT:
490 code = PRC_UNREACH_PORT;
498 case ICMP6_PACKET_TOO_BIG:
499 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
506 * Updating the path MTU will be done after examining
507 * intermediate extension headers.
512 case ICMP6_TIME_EXCEEDED:
513 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
515 case ICMP6_TIME_EXCEED_TRANSIT:
516 case ICMP6_TIME_EXCEED_REASSEMBLY:
517 code += PRC_TIMXCEED_INTRANS;
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);
556 * Prepare an internal mbuf. m_pullup() doesn't
557 * always copy the length we specified.
559 if (maxlen >= MCLBYTES) {
564 MGETHDR(n, M_DONTWAIT, n0->m_type);
565 n0len = n0->m_pkthdr.len; /* save for use below */
567 M_MOVE_PKTHDR(n, n0);
568 if (n && maxlen >= MHLEN) {
569 MCLGET(n, M_DONTWAIT);
570 if ((n->m_flags & M_EXT) == 0) {
581 * Copy IPv6 and ICMPv6 only.
583 nip6 = mtod(n, struct ip6_hdr *);
584 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
585 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
586 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
587 noff = sizeof(struct ip6_hdr);
588 /* new mbuf contains only ipv6+icmpv6 headers */
589 n->m_len = noff + sizeof(struct icmp6_hdr);
591 * Adjust mbuf. ip6_plen will be adjusted in
594 m_adj(n0, off + sizeof(struct icmp6_hdr));
595 /* recalculate complete packet size */
596 n->m_pkthdr.len = n0len + (noff - off);
599 nip6 = mtod(n, struct ip6_hdr *);
600 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
603 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
604 nicmp6->icmp6_code = 0;
606 icmp6stat.icp6s_reflect++;
607 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
608 icmp6_reflect(n, noff);
612 case ICMP6_ECHO_REPLY:
613 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
618 case MLD_LISTENER_QUERY:
619 case MLD_LISTENER_REPORT:
620 if (icmp6len < sizeof(struct mld_hdr))
622 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
623 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
625 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
626 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
636 case MLD_LISTENER_DONE:
637 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
638 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
640 break; /* nothing to be done in kernel */
642 case MLD_MTRACE_RESP:
644 /* XXX: these two are experimental. not officially defind. */
645 /* XXX: per-interface statistics? */
646 break; /* just pass it to applications */
648 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
650 enum { WRU, FQDN } mode;
655 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
657 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
662 #define hostnamelen strlen(hostname)
664 #ifndef PULLDOWN_TEST
665 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
668 n = m_copy(m, 0, M_COPYALL);
670 n = ni6_input(n, off);
671 /* XXX meaningless if n == NULL */
672 noff = sizeof(struct ip6_hdr);
677 if ((icmp6_nodeinfo & 5) != 5)
682 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
683 if (maxlen >= MCLBYTES) {
687 MGETHDR(n, M_DONTWAIT, m->m_type);
688 if (n && maxlen > MHLEN) {
689 MCLGET(n, M_DONTWAIT);
690 if ((n->m_flags & M_EXT) == 0) {
695 if (!m_dup_pkthdr(n, m, M_DONTWAIT)) {
697 * Previous code did a blind M_COPY_PKTHDR
698 * and said "just for rcvif". If true, then
699 * we could tolerate the dup failing (due to
700 * the deep copy of the tag chain). For now
701 * be conservative and just fail.
710 n->m_pkthdr.rcvif = NULL;
712 maxhlen = M_TRAILINGSPACE(n) - maxlen;
713 if (maxhlen > hostnamelen)
714 maxhlen = hostnamelen;
716 * Copy IPv6 and ICMPv6 only.
718 nip6 = mtod(n, struct ip6_hdr *);
719 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
720 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
721 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
722 p = (u_char *)(nicmp6 + 1);
724 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
725 noff = sizeof(struct ip6_hdr);
726 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
727 sizeof(struct icmp6_hdr) + 4 + maxhlen;
728 nicmp6->icmp6_type = ICMP6_WRUREPLY;
729 nicmp6->icmp6_code = 0;
733 icmp6stat.icp6s_reflect++;
734 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
735 icmp6_reflect(n, noff);
745 case ND_ROUTER_SOLICIT:
746 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
749 if (icmp6len < sizeof(struct nd_router_solicit))
751 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
753 nd6_rs_input(m, off, icmp6len);
757 nd6_rs_input(n, off, icmp6len);
761 case ND_ROUTER_ADVERT:
762 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
765 if (icmp6len < sizeof(struct nd_router_advert))
767 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
769 nd6_ra_input(m, off, icmp6len);
773 nd6_ra_input(n, off, icmp6len);
777 case ND_NEIGHBOR_SOLICIT:
778 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
781 if (icmp6len < sizeof(struct nd_neighbor_solicit))
783 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
785 nd6_ns_input(m, off, icmp6len);
789 nd6_ns_input(n, off, icmp6len);
793 case ND_NEIGHBOR_ADVERT:
794 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
797 if (icmp6len < sizeof(struct nd_neighbor_advert))
799 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
801 nd6_na_input(m, off, icmp6len);
805 nd6_na_input(n, off, icmp6len);
810 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
813 if (icmp6len < sizeof(struct nd_redirect))
815 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
817 icmp6_redirect_input(m, off);
821 icmp6_redirect_input(n, off);
825 case ICMP6_ROUTER_RENUMBERING:
826 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
827 code != ICMP6_ROUTER_RENUMBERING_RESULT)
829 if (icmp6len < sizeof(struct icmp6_router_renum))
835 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
836 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
837 ip6_sprintf(&ip6->ip6_dst),
838 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
839 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
840 /* ICMPv6 error: MUST deliver it by spec... */
844 /* ICMPv6 informational: MUST not deliver */
848 if (icmp6_notify_error(m, off, icmp6len, code)) {
849 /* In this case, m should've been freed. */
850 return(IPPROTO_DONE);
855 icmp6stat.icp6s_badcode++;
859 icmp6stat.icp6s_badlen++;
863 /* deliver the packet to appropriate sockets */
864 icmp6_rip6_input(&m, *offp);
874 icmp6_notify_error(m, off, icmp6len, code)
878 struct icmp6_hdr *icmp6;
879 struct ip6_hdr *eip6;
881 struct sockaddr_in6 icmp6src, icmp6dst;
883 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
884 icmp6stat.icp6s_tooshort++;
887 #ifndef PULLDOWN_TEST
888 IP6_EXTHDR_CHECK(m, off,
889 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
891 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
893 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
894 sizeof(*icmp6) + sizeof(struct ip6_hdr));
896 icmp6stat.icp6s_tooshort++;
900 eip6 = (struct ip6_hdr *)(icmp6 + 1);
902 /* Detect the upper level protocol */
904 void (*ctlfunc) __P((int, struct sockaddr *, void *));
905 u_int8_t nxt = eip6->ip6_nxt;
906 int eoff = off + sizeof(struct icmp6_hdr) +
907 sizeof(struct ip6_hdr);
908 struct ip6ctlparam ip6cp;
909 struct in6_addr *finaldst = NULL;
910 int icmp6type = icmp6->icmp6_type;
912 struct ip6_rthdr *rth;
913 struct ip6_rthdr0 *rth0;
916 while (1) { /* XXX: should avoid infinite loop explicitly? */
920 case IPPROTO_HOPOPTS:
921 case IPPROTO_DSTOPTS:
923 #ifndef PULLDOWN_TEST
924 IP6_EXTHDR_CHECK(m, 0, eoff +
925 sizeof(struct ip6_ext),
927 eh = (struct ip6_ext *)(mtod(m, caddr_t)
930 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
933 icmp6stat.icp6s_tooshort++;
938 if (nxt == IPPROTO_AH)
939 eoff += (eh->ip6e_len + 2) << 2;
941 eoff += (eh->ip6e_len + 1) << 3;
944 case IPPROTO_ROUTING:
946 * When the erroneous packet contains a
947 * routing header, we should examine the
948 * header to determine the final destination.
949 * Otherwise, we can't properly update
950 * information that depends on the final
951 * destination (e.g. path MTU).
953 #ifndef PULLDOWN_TEST
954 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
956 rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
959 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
962 icmp6stat.icp6s_tooshort++;
966 rthlen = (rth->ip6r_len + 1) << 3;
968 * XXX: currently there is no
969 * officially defined type other
971 * Note that if the segment left field
972 * is 0, all intermediate hops must
975 if (rth->ip6r_segleft &&
976 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
979 #ifndef PULLDOWN_TEST
980 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen,
982 rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff);
985 struct ip6_rthdr0 *, m,
988 icmp6stat.icp6s_tooshort++;
992 /* just ignore a bogus header */
993 if ((rth0->ip6r0_len % 2) == 0 &&
994 (hops = rth0->ip6r0_len/2))
995 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
1000 case IPPROTO_FRAGMENT:
1001 #ifndef PULLDOWN_TEST
1002 IP6_EXTHDR_CHECK(m, 0, eoff +
1003 sizeof(struct ip6_frag),
1005 fh = (struct ip6_frag *)(mtod(m, caddr_t)
1008 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1011 icmp6stat.icp6s_tooshort++;
1016 * Data after a fragment header is meaningless
1017 * unless it is the first fragment, but
1018 * we'll go to the notify label for path MTU
1021 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1024 eoff += sizeof(struct ip6_frag);
1029 * This case includes ESP and the No Next
1030 * Header. In such cases going to the notify
1031 * label does not have any meaning
1032 * (i.e. ctlfunc will be NULL), but we go
1033 * anyway since we might have to update
1034 * path MTU information.
1040 #ifndef PULLDOWN_TEST
1041 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1043 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1044 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1045 if (icmp6 == NULL) {
1046 icmp6stat.icp6s_tooshort++;
1051 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1052 bzero(&icmp6dst, sizeof(icmp6dst));
1053 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1054 icmp6dst.sin6_family = AF_INET6;
1055 if (finaldst == NULL)
1056 icmp6dst.sin6_addr = eip6->ip6_dst;
1058 icmp6dst.sin6_addr = *finaldst;
1059 icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1060 &icmp6dst.sin6_addr);
1061 #ifndef SCOPEDROUTING
1062 if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
1064 /* should be impossbile */
1066 "icmp6_notify_error: in6_embedscope failed\n"));
1072 * retrieve parameters from the inner IPv6 header, and convert
1073 * them into sockaddr structures.
1075 bzero(&icmp6src, sizeof(icmp6src));
1076 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1077 icmp6src.sin6_family = AF_INET6;
1078 icmp6src.sin6_addr = eip6->ip6_src;
1079 icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1080 &icmp6src.sin6_addr);
1081 #ifndef SCOPEDROUTING
1082 if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
1084 /* should be impossbile */
1086 "icmp6_notify_error: in6_embedscope failed\n"));
1090 icmp6src.sin6_flowinfo =
1091 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1093 if (finaldst == NULL)
1094 finaldst = &eip6->ip6_dst;
1096 ip6cp.ip6c_icmp6 = icmp6;
1097 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1098 ip6cp.ip6c_off = eoff;
1099 ip6cp.ip6c_finaldst = finaldst;
1100 ip6cp.ip6c_src = &icmp6src;
1101 ip6cp.ip6c_nxt = nxt;
1103 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1104 notifymtu = ntohl(icmp6->icmp6_mtu);
1105 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1106 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1109 ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
1110 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1112 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1124 icmp6_mtudisc_update(ip6cp, validated)
1125 struct ip6ctlparam *ip6cp;
1128 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1129 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1130 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1131 u_int mtu = ntohl(icmp6->icmp6_mtu);
1132 struct rtentry *rt = NULL;
1133 struct sockaddr_in6 sin6;
1138 bzero(&sin6, sizeof(sin6));
1139 sin6.sin6_family = PF_INET6;
1140 sin6.sin6_len = sizeof(struct sockaddr_in6);
1141 sin6.sin6_addr = *dst;
1142 /* XXX normally, this won't happen */
1143 if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1144 sin6.sin6_addr.s6_addr16[1] =
1145 htons(m->m_pkthdr.rcvif->if_index);
1147 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1148 rt = rtalloc1((struct sockaddr *)&sin6, 0,
1149 RTF_CLONING | RTF_PRCLONING);
1151 if (rt && (rt->rt_flags & RTF_HOST)
1152 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1153 if (mtu < IPV6_MMTU) {
1155 rt->rt_rmx.rmx_locks |= RTV_MTU;
1156 } else if (mtu < rt->rt_ifp->if_mtu &&
1157 rt->rt_rmx.rmx_mtu > mtu) {
1158 icmp6stat.icp6s_pmtuchg++;
1159 rt->rt_rmx.rmx_mtu = mtu;
1162 if (rt) { /* XXX: need braces to avoid conflict with else in RTFREE. */
1168 * Process a Node Information Query packet, based on
1169 * draft-ietf-ipngwg-icmp-name-lookups-07.
1171 * Spec incompatibilities:
1172 * - IPv6 Subject address handling
1173 * - IPv4 Subject address handling support missing
1174 * - Proxy reply (answer even if it's not for me)
1175 * - joins NI group address at in6_ifattach() time only, does not cope
1176 * with hostname changes by sethostname(3)
1178 #define hostnamelen strlen(hostname)
1179 static struct mbuf *
1184 struct icmp6_nodeinfo *ni6, *nni6;
1185 struct mbuf *n = NULL;
1188 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1189 struct ni_reply_fqdn *fqdn;
1190 int addrs; /* for NI_QTYPE_NODEADDR */
1191 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1192 struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1193 struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
1194 struct ip6_hdr *ip6;
1195 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1197 struct in6_ifaddr *ia6 = NULL;
1199 ip6 = mtod(m, struct ip6_hdr *);
1200 #ifndef PULLDOWN_TEST
1201 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1203 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1205 /* m is already reclaimed */
1211 * Validate IPv6 destination address.
1213 * The Responder must discard the Query without further processing
1214 * unless it is one of the Responder's unicast or anycast addresses, or
1215 * a link-local scope multicast address which the Responder has joined.
1216 * [icmp-name-lookups-07, Section 4.]
1218 bzero(&sin6, sizeof(sin6));
1219 sin6.sin6_family = AF_INET6;
1220 sin6.sin6_len = sizeof(struct sockaddr_in6);
1221 bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1223 if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
1224 /* unicast/anycast, fine */
1225 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1226 (icmp6_nodeinfo & 4) == 0) {
1227 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1228 "a temporary address in %s:%d",
1229 __FILE__, __LINE__));
1232 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1233 ; /* link-local multicast, fine */
1237 /* validate query Subject field. */
1238 qtype = ntohs(ni6->ni_qtype);
1239 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1242 case NI_QTYPE_SUPTYPES:
1244 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1248 case NI_QTYPE_NODEADDR:
1249 switch (ni6->ni_code) {
1250 case ICMP6_NI_SUBJ_IPV6:
1251 #if ICMP6_NI_SUBJ_IPV6 != 0
1255 * backward compatibility - try to accept 03 draft
1256 * format, where no Subject is present.
1258 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1263 #if ICMP6_NI_SUBJ_IPV6 != 0
1264 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1268 if (subjlen != sizeof(sin6.sin6_addr))
1272 * Validate Subject address.
1274 * Not sure what exactly "address belongs to the node"
1275 * means in the spec, is it just unicast, or what?
1277 * At this moment we consider Subject address as
1278 * "belong to the node" if the Subject address equals
1279 * to the IPv6 destination address; validation for
1280 * IPv6 destination address should have done enough
1283 * We do not do proxy at this moment.
1285 /* m_pulldown instead of copy? */
1286 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1287 subjlen, (caddr_t)&sin6.sin6_addr);
1288 sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1290 #ifndef SCOPEDROUTING
1291 in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL);
1293 bzero(&sin6_d, sizeof(sin6_d));
1294 sin6_d.sin6_family = AF_INET6; /* not used, actually */
1295 sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
1296 sin6_d.sin6_addr = ip6->ip6_dst;
1297 sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1299 #ifndef SCOPEDROUTING
1300 in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL);
1302 subj = (char *)&sin6;
1303 if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
1307 * XXX if we are to allow other cases, we should really
1308 * be careful about scope here.
1309 * basically, we should disallow queries toward IPv6
1310 * destination X with subject Y, if scope(X) > scope(Y).
1311 * if we allow scope(X) > scope(Y), it will result in
1312 * information leakage across scope boundary.
1316 case ICMP6_NI_SUBJ_FQDN:
1318 * Validate Subject name with gethostname(3).
1320 * The behavior may need some debate, since:
1321 * - we are not sure if the node has FQDN as
1322 * hostname (returned by gethostname(3)).
1323 * - the code does wildcard match for truncated names.
1324 * however, we are not sure if we want to perform
1325 * wildcard match, if gethostname(3) side has
1326 * truncated hostname.
1328 n = ni6_nametodns(hostname, hostnamelen, 0);
1329 if (!n || n->m_next || n->m_len == 0)
1331 IP6_EXTHDR_GET(subj, char *, m,
1332 off + sizeof(struct icmp6_nodeinfo), subjlen);
1335 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1343 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1350 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1353 if ((icmp6_nodeinfo & 1) == 0)
1356 case NI_QTYPE_NODEADDR:
1357 if ((icmp6_nodeinfo & 2) == 0)
1362 /* guess reply length */
1365 break; /* no reply data */
1366 case NI_QTYPE_SUPTYPES:
1367 replylen += sizeof(u_int32_t);
1370 /* XXX will append an mbuf */
1371 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1373 case NI_QTYPE_NODEADDR:
1374 addrs = ni6_addrs(ni6, m, &ifp, subj);
1375 if ((replylen += addrs * (sizeof(struct in6_addr) +
1376 sizeof(u_int32_t))) > MCLBYTES)
1377 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1381 * XXX: We must return a reply with the ICMP6 code
1382 * `unknown Qtype' in this case. However we regard the case
1383 * as an FQDN query for backward compatibility.
1384 * Older versions set a random value to this field,
1385 * so it rarely varies in the defined qtypes.
1386 * But the mechanism is not reliable...
1387 * maybe we should obsolete older versions.
1389 qtype = NI_QTYPE_FQDN;
1390 /* XXX will append an mbuf */
1391 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1396 /* allocate an mbuf to reply. */
1397 MGETHDR(n, M_DONTWAIT, m->m_type);
1402 M_MOVE_PKTHDR(n, m); /* just for recvif */
1403 if (replylen > MHLEN) {
1404 if (replylen > MCLBYTES) {
1406 * XXX: should we try to allocate more? But MCLBYTES
1407 * is probably much larger than IPV6_MMTU...
1411 MCLGET(n, M_DONTWAIT);
1412 if ((n->m_flags & M_EXT) == 0) {
1416 n->m_pkthdr.len = n->m_len = replylen;
1418 /* copy mbuf header and IPv6 + Node Information base headers */
1419 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1420 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1421 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1423 /* qtype dependent procedure */
1426 nni6->ni_code = ICMP6_NI_SUCCESS;
1429 case NI_QTYPE_SUPTYPES:
1432 nni6->ni_code = ICMP6_NI_SUCCESS;
1433 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1434 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1435 v = (u_int32_t)htonl(0x0000000f);
1436 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1440 nni6->ni_code = ICMP6_NI_SUCCESS;
1441 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1442 sizeof(struct ip6_hdr) +
1443 sizeof(struct icmp6_nodeinfo));
1444 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1445 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1447 * XXX do we really have FQDN in variable "hostname"?
1449 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1450 if (n->m_next == NULL)
1452 /* XXX we assume that n->m_next is not a chain */
1453 if (n->m_next->m_next != NULL)
1455 n->m_pkthdr.len += n->m_next->m_len;
1457 case NI_QTYPE_NODEADDR:
1461 nni6->ni_code = ICMP6_NI_SUCCESS;
1462 n->m_pkthdr.len = n->m_len =
1463 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1464 lenlim = M_TRAILINGSPACE(n);
1465 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1466 /* XXX: reset mbuf length */
1467 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1468 sizeof(struct icmp6_nodeinfo) + copied;
1472 break; /* XXX impossible! */
1475 nni6->ni_type = ICMP6_NI_REPLY;
1488 * make a mbuf with DNS-encoded string. no compression support.
1490 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1491 * treated as truncated name (two \0 at the end). this is a wild guess.
1493 static struct mbuf *
1494 ni6_nametodns(name, namelen, old)
1497 int old; /* return pascal string if non-zero */
1509 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1510 MGET(m, M_DONTWAIT, MT_DATA);
1511 if (m && len > MLEN) {
1512 MCLGET(m, M_DONTWAIT);
1513 if ((m->m_flags & M_EXT) == 0)
1522 *mtod(m, char *) = namelen;
1523 bcopy(name, mtod(m, char *) + 1, namelen);
1527 cp = mtod(m, char *);
1528 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1530 /* if not certain about my name, return empty buffer */
1535 * guess if it looks like shortened hostname, or FQDN.
1536 * shortened hostname needs two trailing "\0".
1539 for (p = name; p < name + namelen; p++) {
1540 if (*p && *p == '.')
1549 while (cp < ep && p < name + namelen) {
1551 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1553 /* result does not fit into mbuf */
1554 if (cp + i + 1 >= ep)
1557 * DNS label length restriction, RFC1035 page 8.
1558 * "i == 0" case is included here to avoid returning
1559 * 0-length label on "foo..bar".
1561 if (i <= 0 || i >= 64)
1567 if (p < name + namelen && *p == '.')
1571 if (cp + nterm >= ep)
1575 m->m_len = cp - mtod(m, char *);
1579 panic("should not reach here");
1589 * check if two DNS-encoded string matches. takes care of truncated
1590 * form (with \0\0 at the end). no compression support.
1591 * XXX upper/lowercase match (see RFC2065)
1594 ni6_dnsmatch(a, alen, b, blen)
1600 const char *a0, *b0;
1603 /* simplest case - need validation? */
1604 if (alen == blen && bcmp(a, b, alen) == 0)
1610 /* termination is mandatory */
1611 if (alen < 2 || blen < 2)
1613 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1618 while (a - a0 < alen && b - b0 < blen) {
1619 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1622 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1624 /* we don't support compression yet */
1625 if (a[0] >= 64 || b[0] >= 64)
1628 /* truncated case */
1629 if (a[0] == 0 && a - a0 == alen - 1)
1631 if (b[0] == 0 && b - b0 == blen - 1)
1633 if (a[0] == 0 || b[0] == 0)
1639 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1641 if (bcmp(a + 1, b + 1, l) != 0)
1648 if (a - a0 == alen && b - b0 == blen)
1655 * calculate the number of addresses to be returned in the node info reply.
1658 ni6_addrs(ni6, m, ifpp, subj)
1659 struct icmp6_nodeinfo *ni6;
1661 struct ifnet **ifpp;
1665 struct in6_ifaddr *ifa6;
1667 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1668 int addrs = 0, addrsofif, iffound = 0;
1669 int niflags = ni6->ni_flags;
1671 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1672 switch (ni6->ni_code) {
1673 case ICMP6_NI_SUBJ_IPV6:
1674 if (subj == NULL) /* must be impossible... */
1676 subj_ip6 = (struct sockaddr_in6 *)subj;
1680 * XXX: we only support IPv6 subject address for
1687 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1690 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1692 if (ifa->ifa_addr->sa_family != AF_INET6)
1694 ifa6 = (struct in6_ifaddr *)ifa;
1696 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1697 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1698 &ifa6->ia_addr.sin6_addr))
1702 * IPv4-mapped addresses can only be returned by a
1703 * Node Information proxy, since they represent
1704 * addresses of IPv4-only nodes, which perforce do
1705 * not implement this protocol.
1706 * [icmp-name-lookups-07, Section 5.4]
1707 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1708 * this function at this moment.
1711 /* What do we have to do about ::1? */
1712 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1713 case IPV6_ADDR_SCOPE_LINKLOCAL:
1714 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1717 case IPV6_ADDR_SCOPE_SITELOCAL:
1718 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1721 case IPV6_ADDR_SCOPE_GLOBAL:
1722 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1730 * check if anycast is okay.
1731 * XXX: just experimental. not in the spec.
1733 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1734 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1735 continue; /* we need only unicast addresses */
1736 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1737 (icmp6_nodeinfo & 4) == 0) {
1740 addrsofif++; /* count the address */
1754 ni6_store_addrs(ni6, nni6, ifp0, resid)
1755 struct icmp6_nodeinfo *ni6, *nni6;
1759 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1760 struct in6_ifaddr *ifa6;
1762 struct ifnet *ifp_dep = NULL;
1763 int copied = 0, allow_deprecated = 0;
1764 u_char *cp = (u_char *)(nni6 + 1);
1765 int niflags = ni6->ni_flags;
1768 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1769 return(0); /* needless to copy */
1773 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1775 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1776 ifa = ifa->ifa_list.tqe_next)
1778 if (ifa->ifa_addr->sa_family != AF_INET6)
1780 ifa6 = (struct in6_ifaddr *)ifa;
1782 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1783 allow_deprecated == 0) {
1785 * prefererred address should be put before
1786 * deprecated addresses.
1789 /* record the interface for later search */
1790 if (ifp_dep == NULL)
1795 else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1796 allow_deprecated != 0)
1797 continue; /* we now collect deprecated addrs */
1799 /* What do we have to do about ::1? */
1800 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1801 case IPV6_ADDR_SCOPE_LINKLOCAL:
1802 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1805 case IPV6_ADDR_SCOPE_SITELOCAL:
1806 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1809 case IPV6_ADDR_SCOPE_GLOBAL:
1810 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1818 * check if anycast is okay.
1819 * XXX: just experimental. not in the spec.
1821 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1822 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1824 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1825 (icmp6_nodeinfo & 4) == 0) {
1829 /* now we can copy the address */
1830 if (resid < sizeof(struct in6_addr) +
1831 sizeof(u_int32_t)) {
1833 * We give up much more copy.
1834 * Set the truncate flag and return.
1837 NI_NODEADDR_FLAG_TRUNCATE;
1842 * Set the TTL of the address.
1843 * The TTL value should be one of the following
1844 * according to the specification:
1846 * 1. The remaining lifetime of a DHCP lease on the
1848 * 2. The remaining Valid Lifetime of a prefix from
1849 * which the address was derived through Stateless
1850 * Autoconfiguration.
1852 * Note that we currently do not support stateful
1853 * address configuration by DHCPv6, so the former
1854 * case can't happen.
1856 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1857 ltime = ND6_INFINITE_LIFETIME;
1859 if (ifa6->ia6_lifetime.ia6t_expire >
1861 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1866 bcopy(<ime, cp, sizeof(u_int32_t));
1867 cp += sizeof(u_int32_t);
1869 /* copy the address itself */
1870 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1871 sizeof(struct in6_addr));
1872 /* XXX: KAME link-local hack; remove ifindex */
1873 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1874 ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1875 cp += sizeof(struct in6_addr);
1877 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1878 copied += (sizeof(struct in6_addr) +
1881 if (ifp0) /* we need search only on the specified IF */
1885 if (allow_deprecated == 0 && ifp_dep != NULL) {
1887 allow_deprecated = 1;
1896 * XXX almost dup'ed code with rip6_input.
1899 icmp6_rip6_input(mp, off)
1903 struct mbuf *m = *mp;
1904 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1905 struct in6pcb *in6p;
1906 struct in6pcb *last = NULL;
1907 struct sockaddr_in6 rip6src;
1908 struct icmp6_hdr *icmp6;
1909 struct mbuf *opts = NULL;
1911 #ifndef PULLDOWN_TEST
1912 /* this is assumed to be safe. */
1913 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1915 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1916 if (icmp6 == NULL) {
1917 /* m is already reclaimed */
1918 return IPPROTO_DONE;
1922 bzero(&rip6src, sizeof(rip6src));
1923 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1924 rip6src.sin6_family = AF_INET6;
1925 /* KAME hack: recover scopeid */
1926 (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1928 LIST_FOREACH(in6p, &ripcb, inp_list)
1930 if ((in6p->inp_vflag & INP_IPV6) == 0)
1932 #ifdef HAVE_NRL_INPCB
1933 if (!(in6p->in6p_flags & INP_IPV6))
1936 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1938 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1939 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1941 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1942 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1944 if (in6p->in6p_icmp6filt
1945 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1946 in6p->in6p_icmp6filt))
1950 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1951 if (last->in6p_flags & IN6P_CONTROLOPTS)
1952 ip6_savecontrol(last, &opts, ip6, n);
1953 /* strip intermediate headers */
1955 if (sbappendaddr(&last->in6p_socket->so_rcv,
1956 (struct sockaddr *)&rip6src,
1958 /* should notify about lost packet */
1964 sorwakeup(last->in6p_socket);
1971 if (last->in6p_flags & IN6P_CONTROLOPTS)
1972 ip6_savecontrol(last, &opts, ip6, m);
1973 /* strip intermediate headers */
1975 if (sbappendaddr(&last->in6p_socket->so_rcv,
1976 (struct sockaddr *)&rip6src, m, opts) == 0) {
1981 sorwakeup(last->in6p_socket);
1984 ip6stat.ip6s_delivered--;
1986 return IPPROTO_DONE;
1990 * Reflect the ip6 packet back to the source.
1991 * OFF points to the icmp6 header, counted from the top of the mbuf.
1994 icmp6_reflect(m, off)
1998 struct ip6_hdr *ip6;
1999 struct icmp6_hdr *icmp6;
2000 struct in6_ifaddr *ia;
2001 struct in6_addr t, *src = 0;
2004 struct ifnet *outif = NULL;
2005 struct sockaddr_in6 sa6_src, sa6_dst;
2006 #ifdef COMPAT_RFC1885
2007 int mtu = IPV6_MMTU;
2008 struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
2011 /* too short to reflect */
2012 if (off < sizeof(struct ip6_hdr)) {
2014 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2015 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2016 __FILE__, __LINE__));
2021 * If there are extra headers between IPv6 and ICMPv6, strip
2022 * off that header first.
2025 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2026 panic("assumption failed in icmp6_reflect");
2028 if (off > sizeof(struct ip6_hdr)) {
2030 struct ip6_hdr nip6;
2032 l = off - sizeof(struct ip6_hdr);
2033 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2035 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2037 if ((m = m_pullup(m, l)) == NULL)
2040 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2041 } else /* off == sizeof(struct ip6_hdr) */ {
2043 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2045 if ((m = m_pullup(m, l)) == NULL)
2049 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2050 ip6 = mtod(m, struct ip6_hdr *);
2051 ip6->ip6_nxt = IPPROTO_ICMPV6;
2052 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2053 type = icmp6->icmp6_type; /* keep type for statistics */
2054 code = icmp6->icmp6_code; /* ditto. */
2058 * ip6_input() drops a packet if its src is multicast.
2059 * So, the src is never multicast.
2061 ip6->ip6_dst = ip6->ip6_src;
2064 * XXX: make sure to embed scope zone information, using
2065 * already embedded IDs or the received interface (if any).
2066 * Note that rcvif may be NULL.
2067 * TODO: scoped routing case (XXX).
2069 bzero(&sa6_src, sizeof(sa6_src));
2070 sa6_src.sin6_family = AF_INET6;
2071 sa6_src.sin6_len = sizeof(sa6_src);
2072 sa6_src.sin6_addr = ip6->ip6_dst;
2073 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2074 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
2075 bzero(&sa6_dst, sizeof(sa6_dst));
2076 sa6_dst.sin6_family = AF_INET6;
2077 sa6_dst.sin6_len = sizeof(sa6_dst);
2078 sa6_dst.sin6_addr = t;
2079 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2080 in6_embedscope(&t, &sa6_dst, NULL, NULL);
2082 #ifdef COMPAT_RFC1885
2085 * RFC 1885 requires that echo reply should be truncated if it
2086 * does not fit in with (return) path MTU, but the description was
2087 * removed in the new spec.
2089 if (icmp6_reflect_rt.ro_rt == 0 ||
2090 ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
2091 if (icmp6_reflect_rt.ro_rt) {
2092 RTFREE(icmp6_reflect_rt.ro_rt);
2093 icmp6_reflect_rt.ro_rt = 0;
2095 bzero(sin6, sizeof(*sin6));
2096 sin6->sin6_family = PF_INET6;
2097 sin6->sin6_len = sizeof(struct sockaddr_in6);
2098 sin6->sin6_addr = ip6->ip6_dst;
2100 rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
2104 if (icmp6_reflect_rt.ro_rt == 0)
2107 if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
2108 && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
2109 mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
2111 if (mtu < m->m_pkthdr.len) {
2112 plen -= (m->m_pkthdr.len - mtu);
2113 m_adj(m, mtu - m->m_pkthdr.len);
2117 * If the incoming packet was addressed directly to us(i.e. unicast),
2118 * use dst as the src for the reply.
2119 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2120 * (for example) when we encounter an error while forwarding procedure
2121 * destined to a duplicated address of ours.
2123 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2124 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2125 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2129 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2131 * This is the case if the dst is our link-local address
2132 * and the sender is also ourselves.
2139 struct route_in6 ro;
2142 * This case matches to multicasts, our anycast, or unicasts
2143 * that we do not own. Select a source address based on the
2144 * source address of the erroneous packet.
2146 bzero(&ro, sizeof(ro));
2147 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e);
2149 RTFREE(ro.ro_rt); /* XXX: we could use this */
2152 "icmp6_reflect: source can't be determined: "
2153 "dst=%s, error=%d\n",
2154 ip6_sprintf(&sa6_src.sin6_addr), e));
2159 ip6->ip6_src = *src;
2162 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2163 ip6->ip6_vfc |= IPV6_VERSION;
2164 ip6->ip6_nxt = IPPROTO_ICMPV6;
2165 if (m->m_pkthdr.rcvif) {
2166 /* XXX: This may not be the outgoing interface */
2167 ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim;
2169 ip6->ip6_hlim = ip6_defhlim;
2171 icmp6->icmp6_cksum = 0;
2172 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2173 sizeof(struct ip6_hdr), plen);
2176 * XXX option handling
2179 m->m_flags &= ~(M_BCAST|M_MCAST);
2181 #ifdef COMPAT_RFC1885
2182 ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
2184 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2187 icmp6_ifoutstat_inc(outif, type, code);
2204 icmp6_redirect_diag(src6, dst6, tgt6)
2205 struct in6_addr *src6;
2206 struct in6_addr *dst6;
2207 struct in6_addr *tgt6;
2209 static char buf[1024];
2210 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2211 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2216 icmp6_redirect_input(m, off)
2220 struct ifnet *ifp = m->m_pkthdr.rcvif;
2221 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2222 struct nd_redirect *nd_rd;
2223 int icmp6len = ntohs(ip6->ip6_plen);
2224 char *lladdr = NULL;
2226 u_char *redirhdr = NULL;
2227 int redirhdrlen = 0;
2228 struct rtentry *rt = NULL;
2231 struct in6_addr src6 = ip6->ip6_src;
2232 struct in6_addr redtgt6;
2233 struct in6_addr reddst6;
2234 union nd_opts ndopts;
2239 /* XXX if we are router, we don't update route by icmp6 redirect */
2242 if (!icmp6_rediraccept)
2245 #ifndef PULLDOWN_TEST
2246 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2247 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2249 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2250 if (nd_rd == NULL) {
2251 icmp6stat.icp6s_tooshort++;
2255 redtgt6 = nd_rd->nd_rd_target;
2256 reddst6 = nd_rd->nd_rd_dst;
2258 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2259 redtgt6.s6_addr16[1] = htons(ifp->if_index);
2260 if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2261 reddst6.s6_addr16[1] = htons(ifp->if_index);
2264 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2266 "ICMP6 redirect sent from %s rejected; "
2267 "must be from linklocal\n", ip6_sprintf(&src6)));
2270 if (ip6->ip6_hlim != 255) {
2272 "ICMP6 redirect sent from %s rejected; "
2273 "hlim=%d (must be 255)\n",
2274 ip6_sprintf(&src6), ip6->ip6_hlim));
2278 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2279 struct sockaddr_in6 sin6;
2280 struct in6_addr *gw6;
2282 bzero(&sin6, sizeof(sin6));
2283 sin6.sin6_family = AF_INET6;
2284 sin6.sin6_len = sizeof(struct sockaddr_in6);
2285 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2286 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
2288 if (rt->rt_gateway == NULL ||
2289 rt->rt_gateway->sa_family != AF_INET6) {
2291 "ICMP6 redirect rejected; no route "
2292 "with inet6 gateway found for redirect dst: %s\n",
2293 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2298 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2299 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2301 "ICMP6 redirect rejected; "
2302 "not equal to gw-for-src=%s (must be same): "
2305 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2311 "ICMP6 redirect rejected; "
2312 "no route found for redirect dst: %s\n",
2313 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2319 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2321 "ICMP6 redirect rejected; "
2322 "redirect dst must be unicast: %s\n",
2323 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2327 is_router = is_onlink = 0;
2328 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2329 is_router = 1; /* router case */
2330 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2331 is_onlink = 1; /* on-link destination case */
2332 if (!is_router && !is_onlink) {
2334 "ICMP6 redirect rejected; "
2335 "neither router case nor onlink case: %s\n",
2336 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2339 /* validation passed */
2341 icmp6len -= sizeof(*nd_rd);
2342 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2343 if (nd6_options(&ndopts) < 0) {
2344 nd6log((LOG_INFO, "icmp6_redirect_input: "
2345 "invalid ND option, rejected: %s\n",
2346 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2347 /* nd6_options have incremented stats */
2351 if (ndopts.nd_opts_tgt_lladdr) {
2352 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2353 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2356 if (ndopts.nd_opts_rh) {
2357 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2358 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2361 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2363 "icmp6_redirect_input: lladdrlen mismatch for %s "
2364 "(if %d, icmp6 packet %d): %s\n",
2365 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2366 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2371 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2372 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2374 if (!is_onlink) { /* better router case. perform rtredirect. */
2375 /* perform rtredirect */
2376 struct sockaddr_in6 sdst;
2377 struct sockaddr_in6 sgw;
2378 struct sockaddr_in6 ssrc;
2380 bzero(&sdst, sizeof(sdst));
2381 bzero(&sgw, sizeof(sgw));
2382 bzero(&ssrc, sizeof(ssrc));
2383 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2384 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2385 sizeof(struct sockaddr_in6);
2386 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2387 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2388 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2389 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2390 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2391 (struct sockaddr *)&ssrc,
2392 (struct rtentry **)NULL);
2394 /* finally update cached route in each socket via pfctlinput */
2396 struct sockaddr_in6 sdst;
2398 bzero(&sdst, sizeof(sdst));
2399 sdst.sin6_family = AF_INET6;
2400 sdst.sin6_len = sizeof(struct sockaddr_in6);
2401 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2402 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2404 key_sa_routechange((struct sockaddr *)&sdst);
2413 icmp6stat.icp6s_badredirect++;
2418 icmp6_redirect_output(m0, rt)
2422 struct ifnet *ifp; /* my outgoing interface */
2423 struct in6_addr *ifp_ll6;
2424 struct in6_addr *router_ll6;
2425 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2426 struct mbuf *m = NULL; /* newly allocated one */
2427 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2428 struct nd_redirect *nd_rd;
2431 struct ifnet *outif = NULL;
2432 struct sockaddr_in6 src_sa;
2434 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2436 /* if we are not router, we don't send icmp6 redirect */
2437 if (!ip6_forwarding || ip6_accept_rtadv)
2441 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2446 * the source address must identify a neighbor, and
2447 * the destination address must not be a multicast address
2448 * [RFC 2461, sec 8.2]
2450 sip6 = mtod(m0, struct ip6_hdr *);
2451 bzero(&src_sa, sizeof(src_sa));
2452 src_sa.sin6_family = AF_INET6;
2453 src_sa.sin6_len = sizeof(src_sa);
2454 src_sa.sin6_addr = sip6->ip6_src;
2455 /* we don't currently use sin6_scope_id, but eventually use it */
2456 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2457 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2459 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2460 goto fail; /* what should we do here? */
2463 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2467 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2468 * we almost always ask for an mbuf cluster for simplicity.
2469 * (MHLEN < IPV6_MMTU is almost always true)
2471 #if IPV6_MMTU >= MCLBYTES
2472 # error assumption failed about IPV6_MMTU and MCLBYTES
2474 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2475 if (m && IPV6_MMTU >= MHLEN)
2476 MCLGET(m, M_DONTWAIT);
2479 m->m_pkthdr.rcvif = NULL;
2481 maxlen = M_TRAILINGSPACE(m);
2482 maxlen = min(IPV6_MMTU, maxlen);
2483 /* just for safety */
2484 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2485 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2490 /* get ip6 linklocal address for ifp(my outgoing interface). */
2491 struct in6_ifaddr *ia;
2492 if ((ia = in6ifa_ifpforlinklocal(ifp,
2494 IN6_IFF_ANYCAST)) == NULL)
2496 ifp_ll6 = &ia->ia_addr.sin6_addr;
2499 /* get ip6 linklocal address for the router. */
2500 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2501 struct sockaddr_in6 *sin6;
2502 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2503 router_ll6 = &sin6->sin6_addr;
2504 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2505 router_ll6 = (struct in6_addr *)NULL;
2507 router_ll6 = (struct in6_addr *)NULL;
2510 ip6 = mtod(m, struct ip6_hdr *);
2512 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2513 ip6->ip6_vfc |= IPV6_VERSION;
2514 /* ip6->ip6_plen will be set later */
2515 ip6->ip6_nxt = IPPROTO_ICMPV6;
2516 ip6->ip6_hlim = 255;
2517 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2518 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2519 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2522 nd_rd = (struct nd_redirect *)(ip6 + 1);
2523 nd_rd->nd_rd_type = ND_REDIRECT;
2524 nd_rd->nd_rd_code = 0;
2525 nd_rd->nd_rd_reserved = 0;
2526 if (rt->rt_flags & RTF_GATEWAY) {
2528 * nd_rd->nd_rd_target must be a link-local address in
2529 * better router cases.
2533 bcopy(router_ll6, &nd_rd->nd_rd_target,
2534 sizeof(nd_rd->nd_rd_target));
2535 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2536 sizeof(nd_rd->nd_rd_dst));
2538 /* make sure redtgt == reddst */
2539 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2540 sizeof(nd_rd->nd_rd_target));
2541 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2542 sizeof(nd_rd->nd_rd_dst));
2545 p = (u_char *)(nd_rd + 1);
2551 /* target lladdr option */
2552 struct rtentry *rt_router = NULL;
2554 struct sockaddr_dl *sdl;
2555 struct nd_opt_hdr *nd_opt;
2558 rt_router = nd6_lookup(router_ll6, 0, ifp);
2561 len = sizeof(*nd_opt) + ifp->if_addrlen;
2562 len = (len + 7) & ~7; /* round by 8 */
2564 if (len + (p - (u_char *)ip6) > maxlen)
2566 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2567 (rt_router->rt_flags & RTF_LLINFO) &&
2568 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2569 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2571 nd_opt = (struct nd_opt_hdr *)p;
2572 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2573 nd_opt->nd_opt_len = len >> 3;
2574 lladdr = (char *)(nd_opt + 1);
2575 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2581 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2583 /* just to be safe */
2584 #ifdef M_DECRYPTED /*not openbsd*/
2585 if (m0->m_flags & M_DECRYPTED)
2588 if (p - (u_char *)ip6 > maxlen)
2592 /* redirected header option */
2594 struct nd_opt_rd_hdr *nd_opt_rh;
2597 * compute the maximum size for icmp6 redirect header option.
2598 * XXX room for auth header?
2600 len = maxlen - (p - (u_char *)ip6);
2603 /* This is just for simplicity. */
2604 if (m0->m_pkthdr.len != m0->m_len) {
2606 m_freem(m0->m_next);
2609 m0->m_pkthdr.len = m0->m_len;
2613 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2614 * about padding/truncate rule for the original IP packet.
2615 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2616 * - "attach as much as possible" is the goal
2617 * - pad if not aligned (original size can be guessed by original
2619 * Following code adds the padding if it is simple enough,
2620 * and truncates if not.
2622 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2623 panic("assumption failed in %s:%d", __FILE__, __LINE__);
2625 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2626 /* not enough room, truncate */
2627 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2629 /* enough room, pad or truncate */
2632 extra = m0->m_pkthdr.len % 8;
2634 /* pad if easy enough, truncate if not */
2635 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2637 m0->m_len += (8 - extra);
2638 m0->m_pkthdr.len += (8 - extra);
2641 m0->m_pkthdr.len -= extra;
2645 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2646 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2649 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2650 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2651 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2652 nd_opt_rh->nd_opt_rh_len = len >> 3;
2653 p += sizeof(*nd_opt_rh);
2654 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2656 /* connect m0 to m */
2658 m->m_pkthdr.len = m->m_len + m0->m_len;
2662 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2663 sip6->ip6_src.s6_addr16[1] = 0;
2664 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2665 sip6->ip6_dst.s6_addr16[1] = 0;
2667 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2668 ip6->ip6_src.s6_addr16[1] = 0;
2669 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2670 ip6->ip6_dst.s6_addr16[1] = 0;
2672 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2673 nd_rd->nd_rd_target.s6_addr16[1] = 0;
2674 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2675 nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2677 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2679 nd_rd->nd_rd_cksum = 0;
2681 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2683 /* send the packet to outside... */
2684 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2686 icmp6_ifstat_inc(outif, ifs6_out_msg);
2687 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2689 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2700 #ifdef HAVE_NRL_INPCB
2701 #define sotoin6pcb sotoinpcb
2702 #define in6pcb inpcb
2703 #define in6p_icmp6filt inp_icmp6filt
2706 * ICMPv6 socket option processing.
2709 icmp6_ctloutput(so, sopt)
2711 struct sockopt *sopt;
2715 struct inpcb *inp = sotoinpcb(so);
2716 int level, op, optname;
2719 level = sopt->sopt_level;
2720 op = sopt->sopt_dir;
2721 optname = sopt->sopt_name;
2722 optlen = sopt->sopt_valsize;
2724 level = op = optname = optlen = 0;
2726 if (level != IPPROTO_ICMPV6) {
2735 struct icmp6_filter *p;
2737 if (optlen != sizeof(*p)) {
2741 if (inp->in6p_icmp6filt == NULL) {
2745 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2751 error = ENOPROTOOPT;
2760 if (inp->in6p_icmp6filt == NULL) {
2764 error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2765 sizeof(struct icmp6_filter));
2770 error = ENOPROTOOPT;
2778 #ifdef HAVE_NRL_INPCB
2781 #undef in6p_icmp6filt
2784 #ifndef HAVE_PPSRATECHECK
2786 #define timersub(tvp, uvp, vvp) \
2788 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2789 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2790 if ((vvp)->tv_usec < 0) { \
2792 (vvp)->tv_usec += 1000000; \
2798 * ppsratecheck(): packets (or events) per second limitation.
2801 ppsratecheck(lasttime, curpps, maxpps)
2802 struct timeval *lasttime;
2804 int maxpps; /* maximum pps allowed */
2806 struct timeval tv, delta;
2813 timersub(&tv, lasttime, &delta);
2816 * Check for 0,0 so that the message will be seen at least once.
2817 * If more than one second has passed since the last update of
2818 * lasttime, reset the counter.
2820 * We do increment *curpps even in *curpps < maxpps case, as some may
2821 * try to use *curpps for stat purposes as well.
2823 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
2824 delta.tv_sec >= 1) {
2828 } else if (maxpps < 0)
2830 else if (*curpps < maxpps)
2835 #if 1 /* DIAGNOSTIC? */
2836 /* be careful about wrap-around */
2837 if (*curpps + 1 > *curpps)
2838 *curpps = *curpps + 1;
2841 * assume that there's not too many calls to this function.
2842 * not sure if the assumption holds, as it depends on *caller's*
2843 * behavior, not the behavior of this function.
2844 * IMHO it is wrong to make assumption on the caller's behavior,
2845 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
2847 *curpps = *curpps + 1;
2855 * Perform rate limit check.
2856 * Returns 0 if it is okay to send the icmp6 packet.
2857 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2860 * XXX per-destination/type check necessary?
2863 icmp6_ratelimit(dst, type, code)
2864 const struct in6_addr *dst; /* not used at this moment */
2865 const int type; /* not used at this moment */
2866 const int code; /* not used at this moment */
2870 ret = 0; /* okay to send */
2873 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2875 /* The packet is subject to rate limit */