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.11 2004/06/07 07:02:42 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 <netproto/key/key.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
145 * ppratecheck() is available, so define it here.
147 #define HAVE_PPSRATECHECK
149 extern struct domain inet6domain;
150 extern struct ip6protosw inet6sw[];
151 extern u_char ip6_protox[];
153 struct icmp6stat icmp6stat;
155 extern struct inpcbinfo ripcbinfo;
156 extern int icmp6errppslim;
157 static int icmp6errpps_count = 0;
158 static struct timeval icmp6errppslim_last;
159 extern int icmp6_nodeinfo;
161 static void icmp6_errcount (struct icmp6errstat *, int, int);
162 static int icmp6_rip6_input (struct mbuf **, int);
163 static int icmp6_ratelimit (const struct in6_addr *, const int, const int);
164 static const char *icmp6_redirect_diag (struct in6_addr *,
165 struct in6_addr *, struct in6_addr *);
166 #ifndef HAVE_PPSRATECHECK
167 static int ppsratecheck (struct timeval *, int *, int);
169 static struct mbuf *ni6_input (struct mbuf *, int);
170 static struct mbuf *ni6_nametodns (const char *, int, int);
171 static int ni6_dnsmatch (const char *, int, const char *, int);
172 static int ni6_addrs (struct icmp6_nodeinfo *, struct mbuf *,
173 struct ifnet **, char *);
174 static int ni6_store_addrs (struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
175 struct ifnet *, int);
176 static int icmp6_notify_error (struct mbuf *, int, int, int);
178 #ifdef COMPAT_RFC1885
179 static struct route_in6 icmp6_reflect_rt;
190 icmp6_errcount(struct icmp6errstat *stat, int type, int code)
193 case ICMP6_DST_UNREACH:
195 case ICMP6_DST_UNREACH_NOROUTE:
196 stat->icp6errs_dst_unreach_noroute++;
198 case ICMP6_DST_UNREACH_ADMIN:
199 stat->icp6errs_dst_unreach_admin++;
201 case ICMP6_DST_UNREACH_BEYONDSCOPE:
202 stat->icp6errs_dst_unreach_beyondscope++;
204 case ICMP6_DST_UNREACH_ADDR:
205 stat->icp6errs_dst_unreach_addr++;
207 case ICMP6_DST_UNREACH_NOPORT:
208 stat->icp6errs_dst_unreach_noport++;
212 case ICMP6_PACKET_TOO_BIG:
213 stat->icp6errs_packet_too_big++;
215 case ICMP6_TIME_EXCEEDED:
217 case ICMP6_TIME_EXCEED_TRANSIT:
218 stat->icp6errs_time_exceed_transit++;
220 case ICMP6_TIME_EXCEED_REASSEMBLY:
221 stat->icp6errs_time_exceed_reassembly++;
225 case ICMP6_PARAM_PROB:
227 case ICMP6_PARAMPROB_HEADER:
228 stat->icp6errs_paramprob_header++;
230 case ICMP6_PARAMPROB_NEXTHEADER:
231 stat->icp6errs_paramprob_nextheader++;
233 case ICMP6_PARAMPROB_OPTION:
234 stat->icp6errs_paramprob_option++;
239 stat->icp6errs_redirect++;
242 stat->icp6errs_unknown++;
246 * Generate an error packet of type error in response to bad IP6 packet.
249 icmp6_error(struct mbuf *m, int type, int code, int param)
251 struct ip6_hdr *oip6, *nip6;
252 struct icmp6_hdr *icmp6;
257 icmp6stat.icp6s_error++;
259 /* count per-type-code statistics */
260 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
262 #ifdef M_DECRYPTED /*not openbsd*/
263 if (m->m_flags & M_DECRYPTED) {
264 icmp6stat.icp6s_canterror++;
269 #ifndef PULLDOWN_TEST
270 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
272 if (m->m_len < sizeof(struct ip6_hdr)) {
273 m = m_pullup(m, sizeof(struct ip6_hdr));
278 oip6 = mtod(m, struct ip6_hdr *);
281 * Multicast destination check. For unrecognized option errors,
282 * this check has already done in ip6_unknown_opt(), so we can
283 * check only for other errors.
285 if ((m->m_flags & (M_BCAST|M_MCAST) ||
286 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
287 (type != ICMP6_PACKET_TOO_BIG &&
288 (type != ICMP6_PARAM_PROB ||
289 code != ICMP6_PARAMPROB_OPTION)))
292 /* Source address check. XXX: the case of anycast source? */
293 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
294 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
298 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
302 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
303 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
304 struct icmp6_hdr *icp;
306 #ifndef PULLDOWN_TEST
307 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
308 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
310 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
313 icmp6stat.icp6s_tooshort++;
317 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
318 icp->icmp6_type == ND_REDIRECT) {
321 * Special case: for redirect (which is
322 * informational) we must not send icmp6 error.
324 icmp6stat.icp6s_canterror++;
327 /* ICMPv6 informational - send the error */
330 /* non-ICMPv6 - send the error */
333 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
335 /* Finally, do rate limitation check. */
336 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
337 icmp6stat.icp6s_toofreq++;
342 * OK, ICMP6 can be generated.
345 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
346 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
348 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
349 M_PREPEND(m, preplen, MB_DONTWAIT);
350 if (m && m->m_len < preplen)
351 m = m_pullup(m, preplen);
353 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
357 nip6 = mtod(m, struct ip6_hdr *);
358 nip6->ip6_src = oip6->ip6_src;
359 nip6->ip6_dst = oip6->ip6_dst;
361 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
362 oip6->ip6_src.s6_addr16[1] = 0;
363 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
364 oip6->ip6_dst.s6_addr16[1] = 0;
366 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
367 icmp6->icmp6_type = type;
368 icmp6->icmp6_code = code;
369 icmp6->icmp6_pptr = htonl((u_int32_t)param);
372 * icmp6_reflect() is designed to be in the input path.
373 * icmp6_error() can be called from both input and outut path,
374 * and if we are in output path rcvif could contain bogus value.
375 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
376 * information in ip header (nip6).
378 m->m_pkthdr.rcvif = NULL;
380 icmp6stat.icp6s_outhist[type]++;
381 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
387 * If we can't tell wheter or not we can generate ICMP6, free it.
393 * Process a received ICMP6 message.
396 icmp6_input(struct mbuf **mp, int *offp, int proto)
398 struct mbuf *m = *mp, *n;
399 struct ip6_hdr *ip6, *nip6;
400 struct icmp6_hdr *icmp6, *nicmp6;
402 int icmp6len = m->m_pkthdr.len - *offp;
405 #ifndef PULLDOWN_TEST
406 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
407 /* m might change if M_LOOP. So, call mtod after this */
411 * Locate icmp6 structure in mbuf, and check
412 * that not corrupted and of at least minimum length
415 ip6 = mtod(m, struct ip6_hdr *);
416 if (icmp6len < sizeof(struct icmp6_hdr)) {
417 icmp6stat.icp6s_tooshort++;
422 * calculate the checksum
424 #ifndef PULLDOWN_TEST
425 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
427 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
429 icmp6stat.icp6s_tooshort++;
433 code = icmp6->icmp6_code;
435 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
437 "ICMP6 checksum error(%d|%x) %s\n",
438 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
439 icmp6stat.icp6s_checksum++;
443 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
445 * Deliver very specific ICMP6 type only.
446 * This is important to deilver TOOBIG. Otherwise PMTUD
449 switch (icmp6->icmp6_type) {
450 case ICMP6_DST_UNREACH:
451 case ICMP6_PACKET_TOO_BIG:
452 case ICMP6_TIME_EXCEEDED:
459 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
460 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
461 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
462 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
464 switch (icmp6->icmp6_type) {
465 case ICMP6_DST_UNREACH:
466 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
468 case ICMP6_DST_UNREACH_NOROUTE:
469 code = PRC_UNREACH_NET;
471 case ICMP6_DST_UNREACH_ADMIN:
472 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
473 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
475 case ICMP6_DST_UNREACH_ADDR:
478 #ifdef COMPAT_RFC1885
479 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
480 code = PRC_UNREACH_SRCFAIL;
483 case ICMP6_DST_UNREACH_BEYONDSCOPE:
484 /* I mean "source address was incorrect." */
485 code = PRC_PARAMPROB;
488 case ICMP6_DST_UNREACH_NOPORT:
489 code = PRC_UNREACH_PORT;
497 case ICMP6_PACKET_TOO_BIG:
498 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
505 * Updating the path MTU will be done after examining
506 * intermediate extension headers.
511 case ICMP6_TIME_EXCEEDED:
512 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
514 case ICMP6_TIME_EXCEED_TRANSIT:
515 case ICMP6_TIME_EXCEED_REASSEMBLY:
516 code += PRC_TIMXCEED_INTRANS;
524 case ICMP6_PARAM_PROB:
525 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
527 case ICMP6_PARAMPROB_NEXTHEADER:
528 code = PRC_UNREACH_PROTOCOL;
530 case ICMP6_PARAMPROB_HEADER:
531 case ICMP6_PARAMPROB_OPTION:
532 code = PRC_PARAMPROB;
540 case ICMP6_ECHO_REQUEST:
541 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
544 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
548 if ((n->m_flags & M_EXT) != 0
549 || n->m_len < off + sizeof(struct icmp6_hdr)) {
551 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
555 * Prepare an internal mbuf. m_pullup() doesn't
556 * always copy the length we specified.
558 if (maxlen >= MCLBYTES) {
563 MGETHDR(n, MB_DONTWAIT, n0->m_type);
564 n0len = n0->m_pkthdr.len; /* save for use below */
566 M_MOVE_PKTHDR(n, n0);
567 if (n && maxlen >= MHLEN) {
568 MCLGET(n, MB_DONTWAIT);
569 if ((n->m_flags & M_EXT) == 0) {
580 * Copy IPv6 and ICMPv6 only.
582 nip6 = mtod(n, struct ip6_hdr *);
583 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
584 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
585 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
586 noff = sizeof(struct ip6_hdr);
587 /* new mbuf contains only ipv6+icmpv6 headers */
588 n->m_len = noff + sizeof(struct icmp6_hdr);
590 * Adjust mbuf. ip6_plen will be adjusted in
593 m_adj(n0, off + sizeof(struct icmp6_hdr));
594 /* recalculate complete packet size */
595 n->m_pkthdr.len = n0len + (noff - off);
598 nip6 = mtod(n, struct ip6_hdr *);
599 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
602 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
603 nicmp6->icmp6_code = 0;
605 icmp6stat.icp6s_reflect++;
606 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
607 icmp6_reflect(n, noff);
611 case ICMP6_ECHO_REPLY:
612 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
617 case MLD_LISTENER_QUERY:
618 case MLD_LISTENER_REPORT:
619 if (icmp6len < sizeof(struct mld_hdr))
621 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
622 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
624 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
625 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
635 case MLD_LISTENER_DONE:
636 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
637 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
639 break; /* nothing to be done in kernel */
641 case MLD_MTRACE_RESP:
643 /* XXX: these two are experimental. not officially defind. */
644 /* XXX: per-interface statistics? */
645 break; /* just pass it to applications */
647 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
649 enum { WRU, FQDN } mode;
654 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
656 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
661 #define hostnamelen strlen(hostname)
663 #ifndef PULLDOWN_TEST
664 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
667 n = m_copy(m, 0, M_COPYALL);
669 n = ni6_input(n, off);
670 /* XXX meaningless if n == NULL */
671 noff = sizeof(struct ip6_hdr);
676 if ((icmp6_nodeinfo & 5) != 5)
681 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
682 if (maxlen >= MCLBYTES) {
686 MGETHDR(n, MB_DONTWAIT, m->m_type);
687 if (n && maxlen > MHLEN) {
688 MCLGET(n, MB_DONTWAIT);
689 if ((n->m_flags & M_EXT) == 0) {
694 if (!m_dup_pkthdr(n, m, MB_DONTWAIT)) {
696 * Previous code did a blind M_COPY_PKTHDR
697 * and said "just for rcvif". If true, then
698 * we could tolerate the dup failing (due to
699 * the deep copy of the tag chain). For now
700 * be conservative and just fail.
709 n->m_pkthdr.rcvif = NULL;
711 maxhlen = M_TRAILINGSPACE(n) - maxlen;
712 if (maxhlen > hostnamelen)
713 maxhlen = hostnamelen;
715 * Copy IPv6 and ICMPv6 only.
717 nip6 = mtod(n, struct ip6_hdr *);
718 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
719 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
720 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
721 p = (u_char *)(nicmp6 + 1);
723 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
724 noff = sizeof(struct ip6_hdr);
725 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
726 sizeof(struct icmp6_hdr) + 4 + maxhlen;
727 nicmp6->icmp6_type = ICMP6_WRUREPLY;
728 nicmp6->icmp6_code = 0;
732 icmp6stat.icp6s_reflect++;
733 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
734 icmp6_reflect(n, noff);
744 case ND_ROUTER_SOLICIT:
745 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
748 if (icmp6len < sizeof(struct nd_router_solicit))
750 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
752 nd6_rs_input(m, off, icmp6len);
756 nd6_rs_input(n, off, icmp6len);
760 case ND_ROUTER_ADVERT:
761 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
764 if (icmp6len < sizeof(struct nd_router_advert))
766 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
768 nd6_ra_input(m, off, icmp6len);
772 nd6_ra_input(n, off, icmp6len);
776 case ND_NEIGHBOR_SOLICIT:
777 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
780 if (icmp6len < sizeof(struct nd_neighbor_solicit))
782 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
784 nd6_ns_input(m, off, icmp6len);
788 nd6_ns_input(n, off, icmp6len);
792 case ND_NEIGHBOR_ADVERT:
793 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
796 if (icmp6len < sizeof(struct nd_neighbor_advert))
798 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
800 nd6_na_input(m, off, icmp6len);
804 nd6_na_input(n, off, icmp6len);
809 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
812 if (icmp6len < sizeof(struct nd_redirect))
814 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
816 icmp6_redirect_input(m, off);
820 icmp6_redirect_input(n, off);
824 case ICMP6_ROUTER_RENUMBERING:
825 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
826 code != ICMP6_ROUTER_RENUMBERING_RESULT)
828 if (icmp6len < sizeof(struct icmp6_router_renum))
834 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
835 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
836 ip6_sprintf(&ip6->ip6_dst),
837 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
838 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
839 /* ICMPv6 error: MUST deliver it by spec... */
843 /* ICMPv6 informational: MUST not deliver */
847 if (icmp6_notify_error(m, off, icmp6len, code)) {
848 /* In this case, m should've been freed. */
849 return(IPPROTO_DONE);
854 icmp6stat.icp6s_badcode++;
858 icmp6stat.icp6s_badlen++;
862 /* deliver the packet to appropriate sockets */
863 icmp6_rip6_input(&m, *offp);
873 icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
875 struct icmp6_hdr *icmp6;
876 struct ip6_hdr *eip6;
878 struct sockaddr_in6 icmp6src, icmp6dst;
880 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
881 icmp6stat.icp6s_tooshort++;
884 #ifndef PULLDOWN_TEST
885 IP6_EXTHDR_CHECK(m, off,
886 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
888 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
890 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
891 sizeof(*icmp6) + sizeof(struct ip6_hdr));
893 icmp6stat.icp6s_tooshort++;
897 eip6 = (struct ip6_hdr *)(icmp6 + 1);
899 /* Detect the upper level protocol */
901 void (*ctlfunc) (int, struct sockaddr *, void *);
902 u_int8_t nxt = eip6->ip6_nxt;
903 int eoff = off + sizeof(struct icmp6_hdr) +
904 sizeof(struct ip6_hdr);
905 struct ip6ctlparam ip6cp;
906 struct in6_addr *finaldst = NULL;
907 int icmp6type = icmp6->icmp6_type;
909 struct ip6_rthdr *rth;
910 struct ip6_rthdr0 *rth0;
913 while (1) { /* XXX: should avoid infinite loop explicitly? */
917 case IPPROTO_HOPOPTS:
918 case IPPROTO_DSTOPTS:
920 #ifndef PULLDOWN_TEST
921 IP6_EXTHDR_CHECK(m, 0, eoff +
922 sizeof(struct ip6_ext),
924 eh = (struct ip6_ext *)(mtod(m, caddr_t)
927 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
930 icmp6stat.icp6s_tooshort++;
935 if (nxt == IPPROTO_AH)
936 eoff += (eh->ip6e_len + 2) << 2;
938 eoff += (eh->ip6e_len + 1) << 3;
941 case IPPROTO_ROUTING:
943 * When the erroneous packet contains a
944 * routing header, we should examine the
945 * header to determine the final destination.
946 * Otherwise, we can't properly update
947 * information that depends on the final
948 * destination (e.g. path MTU).
950 #ifndef PULLDOWN_TEST
951 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
953 rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
956 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
959 icmp6stat.icp6s_tooshort++;
963 rthlen = (rth->ip6r_len + 1) << 3;
965 * XXX: currently there is no
966 * officially defined type other
968 * Note that if the segment left field
969 * is 0, all intermediate hops must
972 if (rth->ip6r_segleft &&
973 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
976 #ifndef PULLDOWN_TEST
977 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen,
979 rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff);
982 struct ip6_rthdr0 *, m,
985 icmp6stat.icp6s_tooshort++;
989 /* just ignore a bogus header */
990 if ((rth0->ip6r0_len % 2) == 0 &&
991 (hops = rth0->ip6r0_len/2))
992 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
997 case IPPROTO_FRAGMENT:
998 #ifndef PULLDOWN_TEST
999 IP6_EXTHDR_CHECK(m, 0, eoff +
1000 sizeof(struct ip6_frag),
1002 fh = (struct ip6_frag *)(mtod(m, caddr_t)
1005 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1008 icmp6stat.icp6s_tooshort++;
1013 * Data after a fragment header is meaningless
1014 * unless it is the first fragment, but
1015 * we'll go to the notify label for path MTU
1018 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1021 eoff += sizeof(struct ip6_frag);
1026 * This case includes ESP and the No Next
1027 * Header. In such cases going to the notify
1028 * label does not have any meaning
1029 * (i.e. ctlfunc will be NULL), but we go
1030 * anyway since we might have to update
1031 * path MTU information.
1037 #ifndef PULLDOWN_TEST
1038 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1040 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1041 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1042 if (icmp6 == NULL) {
1043 icmp6stat.icp6s_tooshort++;
1048 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1049 bzero(&icmp6dst, sizeof(icmp6dst));
1050 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1051 icmp6dst.sin6_family = AF_INET6;
1052 if (finaldst == NULL)
1053 icmp6dst.sin6_addr = eip6->ip6_dst;
1055 icmp6dst.sin6_addr = *finaldst;
1056 icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1057 &icmp6dst.sin6_addr);
1058 #ifndef SCOPEDROUTING
1059 if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
1061 /* should be impossbile */
1063 "icmp6_notify_error: in6_embedscope failed\n"));
1069 * retrieve parameters from the inner IPv6 header, and convert
1070 * them into sockaddr structures.
1072 bzero(&icmp6src, sizeof(icmp6src));
1073 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1074 icmp6src.sin6_family = AF_INET6;
1075 icmp6src.sin6_addr = eip6->ip6_src;
1076 icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1077 &icmp6src.sin6_addr);
1078 #ifndef SCOPEDROUTING
1079 if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
1081 /* should be impossbile */
1083 "icmp6_notify_error: in6_embedscope failed\n"));
1087 icmp6src.sin6_flowinfo =
1088 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1090 if (finaldst == NULL)
1091 finaldst = &eip6->ip6_dst;
1093 ip6cp.ip6c_icmp6 = icmp6;
1094 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1095 ip6cp.ip6c_off = eoff;
1096 ip6cp.ip6c_finaldst = finaldst;
1097 ip6cp.ip6c_src = &icmp6src;
1098 ip6cp.ip6c_nxt = nxt;
1100 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1101 notifymtu = ntohl(icmp6->icmp6_mtu);
1102 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1103 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1106 ctlfunc = (void (*) (int, struct sockaddr *, void *))
1107 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1109 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1121 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1123 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1124 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1125 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1126 u_int mtu = ntohl(icmp6->icmp6_mtu);
1127 struct rtentry *rt = NULL;
1128 struct sockaddr_in6 sin6;
1133 bzero(&sin6, sizeof(sin6));
1134 sin6.sin6_family = PF_INET6;
1135 sin6.sin6_len = sizeof(struct sockaddr_in6);
1136 sin6.sin6_addr = *dst;
1137 /* XXX normally, this won't happen */
1138 if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1139 sin6.sin6_addr.s6_addr16[1] =
1140 htons(m->m_pkthdr.rcvif->if_index);
1142 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1143 rt = rtalloc1((struct sockaddr *)&sin6, 0,
1144 RTF_CLONING | RTF_PRCLONING);
1146 if (rt && (rt->rt_flags & RTF_HOST)
1147 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1148 if (mtu < IPV6_MMTU) {
1150 rt->rt_rmx.rmx_locks |= RTV_MTU;
1151 } else if (mtu < rt->rt_ifp->if_mtu &&
1152 rt->rt_rmx.rmx_mtu > mtu) {
1153 icmp6stat.icp6s_pmtuchg++;
1154 rt->rt_rmx.rmx_mtu = mtu;
1157 if (rt) { /* XXX: need braces to avoid conflict with else in RTFREE. */
1163 * Process a Node Information Query packet, based on
1164 * draft-ietf-ipngwg-icmp-name-lookups-07.
1166 * Spec incompatibilities:
1167 * - IPv6 Subject address handling
1168 * - IPv4 Subject address handling support missing
1169 * - Proxy reply (answer even if it's not for me)
1170 * - joins NI group address at in6_ifattach() time only, does not cope
1171 * with hostname changes by sethostname(3)
1173 #define hostnamelen strlen(hostname)
1174 static struct mbuf *
1175 ni6_input(struct mbuf *m, int off)
1177 struct icmp6_nodeinfo *ni6, *nni6;
1178 struct mbuf *n = NULL;
1181 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1182 struct ni_reply_fqdn *fqdn;
1183 int addrs; /* for NI_QTYPE_NODEADDR */
1184 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1185 struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1186 struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
1187 struct ip6_hdr *ip6;
1188 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1190 struct in6_ifaddr *ia6 = NULL;
1192 ip6 = mtod(m, struct ip6_hdr *);
1193 #ifndef PULLDOWN_TEST
1194 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1196 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1198 /* m is already reclaimed */
1204 * Validate IPv6 destination address.
1206 * The Responder must discard the Query without further processing
1207 * unless it is one of the Responder's unicast or anycast addresses, or
1208 * a link-local scope multicast address which the Responder has joined.
1209 * [icmp-name-lookups-07, Section 4.]
1211 bzero(&sin6, sizeof(sin6));
1212 sin6.sin6_family = AF_INET6;
1213 sin6.sin6_len = sizeof(struct sockaddr_in6);
1214 bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1216 if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
1217 /* unicast/anycast, fine */
1218 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1219 (icmp6_nodeinfo & 4) == 0) {
1220 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1221 "a temporary address in %s:%d",
1222 __FILE__, __LINE__));
1225 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1226 ; /* link-local multicast, fine */
1230 /* validate query Subject field. */
1231 qtype = ntohs(ni6->ni_qtype);
1232 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1235 case NI_QTYPE_SUPTYPES:
1237 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1241 case NI_QTYPE_NODEADDR:
1242 switch (ni6->ni_code) {
1243 case ICMP6_NI_SUBJ_IPV6:
1244 #if ICMP6_NI_SUBJ_IPV6 != 0
1248 * backward compatibility - try to accept 03 draft
1249 * format, where no Subject is present.
1251 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1256 #if ICMP6_NI_SUBJ_IPV6 != 0
1257 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1261 if (subjlen != sizeof(sin6.sin6_addr))
1265 * Validate Subject address.
1267 * Not sure what exactly "address belongs to the node"
1268 * means in the spec, is it just unicast, or what?
1270 * At this moment we consider Subject address as
1271 * "belong to the node" if the Subject address equals
1272 * to the IPv6 destination address; validation for
1273 * IPv6 destination address should have done enough
1276 * We do not do proxy at this moment.
1278 /* m_pulldown instead of copy? */
1279 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1280 subjlen, (caddr_t)&sin6.sin6_addr);
1281 sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1283 #ifndef SCOPEDROUTING
1284 in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL);
1286 bzero(&sin6_d, sizeof(sin6_d));
1287 sin6_d.sin6_family = AF_INET6; /* not used, actually */
1288 sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
1289 sin6_d.sin6_addr = ip6->ip6_dst;
1290 sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1292 #ifndef SCOPEDROUTING
1293 in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL);
1295 subj = (char *)&sin6;
1296 if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
1300 * XXX if we are to allow other cases, we should really
1301 * be careful about scope here.
1302 * basically, we should disallow queries toward IPv6
1303 * destination X with subject Y, if scope(X) > scope(Y).
1304 * if we allow scope(X) > scope(Y), it will result in
1305 * information leakage across scope boundary.
1309 case ICMP6_NI_SUBJ_FQDN:
1311 * Validate Subject name with gethostname(3).
1313 * The behavior may need some debate, since:
1314 * - we are not sure if the node has FQDN as
1315 * hostname (returned by gethostname(3)).
1316 * - the code does wildcard match for truncated names.
1317 * however, we are not sure if we want to perform
1318 * wildcard match, if gethostname(3) side has
1319 * truncated hostname.
1321 n = ni6_nametodns(hostname, hostnamelen, 0);
1322 if (!n || n->m_next || n->m_len == 0)
1324 IP6_EXTHDR_GET(subj, char *, m,
1325 off + sizeof(struct icmp6_nodeinfo), subjlen);
1328 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1336 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1343 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1346 if ((icmp6_nodeinfo & 1) == 0)
1349 case NI_QTYPE_NODEADDR:
1350 if ((icmp6_nodeinfo & 2) == 0)
1355 /* guess reply length */
1358 break; /* no reply data */
1359 case NI_QTYPE_SUPTYPES:
1360 replylen += sizeof(u_int32_t);
1363 /* XXX will append an mbuf */
1364 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1366 case NI_QTYPE_NODEADDR:
1367 addrs = ni6_addrs(ni6, m, &ifp, subj);
1368 if ((replylen += addrs * (sizeof(struct in6_addr) +
1369 sizeof(u_int32_t))) > MCLBYTES)
1370 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1374 * XXX: We must return a reply with the ICMP6 code
1375 * `unknown Qtype' in this case. However we regard the case
1376 * as an FQDN query for backward compatibility.
1377 * Older versions set a random value to this field,
1378 * so it rarely varies in the defined qtypes.
1379 * But the mechanism is not reliable...
1380 * maybe we should obsolete older versions.
1382 qtype = NI_QTYPE_FQDN;
1383 /* XXX will append an mbuf */
1384 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1389 /* allocate an mbuf to reply. */
1390 MGETHDR(n, MB_DONTWAIT, m->m_type);
1395 M_MOVE_PKTHDR(n, m); /* just for recvif */
1396 if (replylen > MHLEN) {
1397 if (replylen > MCLBYTES) {
1399 * XXX: should we try to allocate more? But MCLBYTES
1400 * is probably much larger than IPV6_MMTU...
1404 MCLGET(n, MB_DONTWAIT);
1405 if ((n->m_flags & M_EXT) == 0) {
1409 n->m_pkthdr.len = n->m_len = replylen;
1411 /* copy mbuf header and IPv6 + Node Information base headers */
1412 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1413 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1414 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1416 /* qtype dependent procedure */
1419 nni6->ni_code = ICMP6_NI_SUCCESS;
1422 case NI_QTYPE_SUPTYPES:
1425 nni6->ni_code = ICMP6_NI_SUCCESS;
1426 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1427 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1428 v = (u_int32_t)htonl(0x0000000f);
1429 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1433 nni6->ni_code = ICMP6_NI_SUCCESS;
1434 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1435 sizeof(struct ip6_hdr) +
1436 sizeof(struct icmp6_nodeinfo));
1437 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1438 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1440 * XXX do we really have FQDN in variable "hostname"?
1442 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1443 if (n->m_next == NULL)
1445 /* XXX we assume that n->m_next is not a chain */
1446 if (n->m_next->m_next != NULL)
1448 n->m_pkthdr.len += n->m_next->m_len;
1450 case NI_QTYPE_NODEADDR:
1454 nni6->ni_code = ICMP6_NI_SUCCESS;
1455 n->m_pkthdr.len = n->m_len =
1456 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1457 lenlim = M_TRAILINGSPACE(n);
1458 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1459 /* XXX: reset mbuf length */
1460 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1461 sizeof(struct icmp6_nodeinfo) + copied;
1465 break; /* XXX impossible! */
1468 nni6->ni_type = ICMP6_NI_REPLY;
1481 * make a mbuf with DNS-encoded string. no compression support.
1483 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1484 * treated as truncated name (two \0 at the end). this is a wild guess.
1486 static struct mbuf *
1487 ni6_nametodns(const char *name, int namelen,
1488 int old) /* return pascal string if non-zero */
1500 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1501 MGET(m, MB_DONTWAIT, MT_DATA);
1502 if (m && len > MLEN) {
1503 MCLGET(m, MB_DONTWAIT);
1504 if ((m->m_flags & M_EXT) == 0)
1513 *mtod(m, char *) = namelen;
1514 bcopy(name, mtod(m, char *) + 1, namelen);
1518 cp = mtod(m, char *);
1519 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1521 /* if not certain about my name, return empty buffer */
1526 * guess if it looks like shortened hostname, or FQDN.
1527 * shortened hostname needs two trailing "\0".
1530 for (p = name; p < name + namelen; p++) {
1531 if (*p && *p == '.')
1540 while (cp < ep && p < name + namelen) {
1542 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1544 /* result does not fit into mbuf */
1545 if (cp + i + 1 >= ep)
1548 * DNS label length restriction, RFC1035 page 8.
1549 * "i == 0" case is included here to avoid returning
1550 * 0-length label on "foo..bar".
1552 if (i <= 0 || i >= 64)
1558 if (p < name + namelen && *p == '.')
1562 if (cp + nterm >= ep)
1566 m->m_len = cp - mtod(m, char *);
1570 panic("should not reach here");
1580 * check if two DNS-encoded string matches. takes care of truncated
1581 * form (with \0\0 at the end). no compression support.
1582 * XXX upper/lowercase match (see RFC2065)
1585 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1587 const char *a0, *b0;
1590 /* simplest case - need validation? */
1591 if (alen == blen && bcmp(a, b, alen) == 0)
1597 /* termination is mandatory */
1598 if (alen < 2 || blen < 2)
1600 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1605 while (a - a0 < alen && b - b0 < blen) {
1606 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1609 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1611 /* we don't support compression yet */
1612 if (a[0] >= 64 || b[0] >= 64)
1615 /* truncated case */
1616 if (a[0] == 0 && a - a0 == alen - 1)
1618 if (b[0] == 0 && b - b0 == blen - 1)
1620 if (a[0] == 0 || b[0] == 0)
1626 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1628 if (bcmp(a + 1, b + 1, l) != 0)
1635 if (a - a0 == alen && b - b0 == blen)
1642 * calculate the number of addresses to be returned in the node info reply.
1645 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1649 struct in6_ifaddr *ifa6;
1651 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1652 int addrs = 0, addrsofif, iffound = 0;
1653 int niflags = ni6->ni_flags;
1655 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1656 switch (ni6->ni_code) {
1657 case ICMP6_NI_SUBJ_IPV6:
1658 if (subj == NULL) /* must be impossible... */
1660 subj_ip6 = (struct sockaddr_in6 *)subj;
1664 * XXX: we only support IPv6 subject address for
1671 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1674 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1676 if (ifa->ifa_addr->sa_family != AF_INET6)
1678 ifa6 = (struct in6_ifaddr *)ifa;
1680 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1681 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1682 &ifa6->ia_addr.sin6_addr))
1686 * IPv4-mapped addresses can only be returned by a
1687 * Node Information proxy, since they represent
1688 * addresses of IPv4-only nodes, which perforce do
1689 * not implement this protocol.
1690 * [icmp-name-lookups-07, Section 5.4]
1691 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1692 * this function at this moment.
1695 /* What do we have to do about ::1? */
1696 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1697 case IPV6_ADDR_SCOPE_LINKLOCAL:
1698 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1701 case IPV6_ADDR_SCOPE_SITELOCAL:
1702 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1705 case IPV6_ADDR_SCOPE_GLOBAL:
1706 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1714 * check if anycast is okay.
1715 * XXX: just experimental. not in the spec.
1717 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1718 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1719 continue; /* we need only unicast addresses */
1720 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1721 (icmp6_nodeinfo & 4) == 0) {
1724 addrsofif++; /* count the address */
1738 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1739 struct ifnet *ifp0, int resid)
1741 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1742 struct in6_ifaddr *ifa6;
1744 struct ifnet *ifp_dep = NULL;
1745 int copied = 0, allow_deprecated = 0;
1746 u_char *cp = (u_char *)(nni6 + 1);
1747 int niflags = ni6->ni_flags;
1750 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1751 return(0); /* needless to copy */
1755 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1757 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1758 ifa = ifa->ifa_list.tqe_next)
1760 if (ifa->ifa_addr->sa_family != AF_INET6)
1762 ifa6 = (struct in6_ifaddr *)ifa;
1764 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1765 allow_deprecated == 0) {
1767 * prefererred address should be put before
1768 * deprecated addresses.
1771 /* record the interface for later search */
1772 if (ifp_dep == NULL)
1777 else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1778 allow_deprecated != 0)
1779 continue; /* we now collect deprecated addrs */
1781 /* What do we have to do about ::1? */
1782 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1783 case IPV6_ADDR_SCOPE_LINKLOCAL:
1784 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1787 case IPV6_ADDR_SCOPE_SITELOCAL:
1788 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1791 case IPV6_ADDR_SCOPE_GLOBAL:
1792 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1800 * check if anycast is okay.
1801 * XXX: just experimental. not in the spec.
1803 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1804 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1806 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1807 (icmp6_nodeinfo & 4) == 0) {
1811 /* now we can copy the address */
1812 if (resid < sizeof(struct in6_addr) +
1813 sizeof(u_int32_t)) {
1815 * We give up much more copy.
1816 * Set the truncate flag and return.
1819 NI_NODEADDR_FLAG_TRUNCATE;
1824 * Set the TTL of the address.
1825 * The TTL value should be one of the following
1826 * according to the specification:
1828 * 1. The remaining lifetime of a DHCP lease on the
1830 * 2. The remaining Valid Lifetime of a prefix from
1831 * which the address was derived through Stateless
1832 * Autoconfiguration.
1834 * Note that we currently do not support stateful
1835 * address configuration by DHCPv6, so the former
1836 * case can't happen.
1838 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1839 ltime = ND6_INFINITE_LIFETIME;
1841 if (ifa6->ia6_lifetime.ia6t_expire >
1843 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1848 bcopy(<ime, cp, sizeof(u_int32_t));
1849 cp += sizeof(u_int32_t);
1851 /* copy the address itself */
1852 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1853 sizeof(struct in6_addr));
1854 /* XXX: KAME link-local hack; remove ifindex */
1855 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1856 ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1857 cp += sizeof(struct in6_addr);
1859 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1860 copied += (sizeof(struct in6_addr) +
1863 if (ifp0) /* we need search only on the specified IF */
1867 if (allow_deprecated == 0 && ifp_dep != NULL) {
1869 allow_deprecated = 1;
1878 * XXX almost dup'ed code with rip6_input.
1881 icmp6_rip6_input(struct mbuf **mp, int off)
1883 struct mbuf *m = *mp;
1884 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1885 struct in6pcb *in6p;
1886 struct in6pcb *last = NULL;
1887 struct sockaddr_in6 rip6src;
1888 struct icmp6_hdr *icmp6;
1889 struct mbuf *opts = NULL;
1891 #ifndef PULLDOWN_TEST
1892 /* this is assumed to be safe. */
1893 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1895 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1896 if (icmp6 == NULL) {
1897 /* m is already reclaimed */
1898 return IPPROTO_DONE;
1902 bzero(&rip6src, sizeof(rip6src));
1903 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1904 rip6src.sin6_family = AF_INET6;
1905 /* KAME hack: recover scopeid */
1906 (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1908 LIST_FOREACH(in6p, &ripcbinfo.pcblisthead, inp_list)
1910 if (in6p->inp_flags & INP_PLACEMARKER)
1912 if ((in6p->inp_vflag & INP_IPV6) == 0)
1914 #ifdef HAVE_NRL_INPCB
1915 if (!(in6p->in6p_flags & INP_IPV6))
1918 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1920 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1921 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1923 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1924 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1926 if (in6p->in6p_icmp6filt
1927 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1928 in6p->in6p_icmp6filt))
1932 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1933 if (last->in6p_flags & IN6P_CONTROLOPTS)
1934 ip6_savecontrol(last, &opts, ip6, n);
1935 /* strip intermediate headers */
1937 if (sbappendaddr(&last->in6p_socket->so_rcv,
1938 (struct sockaddr *)&rip6src,
1940 /* should notify about lost packet */
1946 sorwakeup(last->in6p_socket);
1953 if (last->in6p_flags & IN6P_CONTROLOPTS)
1954 ip6_savecontrol(last, &opts, ip6, m);
1955 /* strip intermediate headers */
1957 if (sbappendaddr(&last->in6p_socket->so_rcv,
1958 (struct sockaddr *)&rip6src, m, opts) == 0) {
1963 sorwakeup(last->in6p_socket);
1966 ip6stat.ip6s_delivered--;
1968 return IPPROTO_DONE;
1972 * Reflect the ip6 packet back to the source.
1973 * OFF points to the icmp6 header, counted from the top of the mbuf.
1976 icmp6_reflect(struct mbuf *m, size_t off)
1978 struct ip6_hdr *ip6;
1979 struct icmp6_hdr *icmp6;
1980 struct in6_ifaddr *ia;
1981 struct in6_addr t, *src = 0;
1984 struct ifnet *outif = NULL;
1985 struct sockaddr_in6 sa6_src, sa6_dst;
1986 #ifdef COMPAT_RFC1885
1987 int mtu = IPV6_MMTU;
1988 struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
1991 /* too short to reflect */
1992 if (off < sizeof(struct ip6_hdr)) {
1994 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1995 (u_long)off, (u_long)sizeof(struct ip6_hdr),
1996 __FILE__, __LINE__));
2001 * If there are extra headers between IPv6 and ICMPv6, strip
2002 * off that header first.
2005 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2006 panic("assumption failed in icmp6_reflect");
2008 if (off > sizeof(struct ip6_hdr)) {
2010 struct ip6_hdr nip6;
2012 l = off - sizeof(struct ip6_hdr);
2013 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2015 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2017 if ((m = m_pullup(m, l)) == NULL)
2020 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2021 } else /* off == sizeof(struct ip6_hdr) */ {
2023 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2025 if ((m = m_pullup(m, l)) == NULL)
2029 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2030 ip6 = mtod(m, struct ip6_hdr *);
2031 ip6->ip6_nxt = IPPROTO_ICMPV6;
2032 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2033 type = icmp6->icmp6_type; /* keep type for statistics */
2034 code = icmp6->icmp6_code; /* ditto. */
2038 * ip6_input() drops a packet if its src is multicast.
2039 * So, the src is never multicast.
2041 ip6->ip6_dst = ip6->ip6_src;
2044 * XXX: make sure to embed scope zone information, using
2045 * already embedded IDs or the received interface (if any).
2046 * Note that rcvif may be NULL.
2047 * TODO: scoped routing case (XXX).
2049 bzero(&sa6_src, sizeof(sa6_src));
2050 sa6_src.sin6_family = AF_INET6;
2051 sa6_src.sin6_len = sizeof(sa6_src);
2052 sa6_src.sin6_addr = ip6->ip6_dst;
2053 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2054 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
2055 bzero(&sa6_dst, sizeof(sa6_dst));
2056 sa6_dst.sin6_family = AF_INET6;
2057 sa6_dst.sin6_len = sizeof(sa6_dst);
2058 sa6_dst.sin6_addr = t;
2059 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2060 in6_embedscope(&t, &sa6_dst, NULL, NULL);
2062 #ifdef COMPAT_RFC1885
2065 * RFC 1885 requires that echo reply should be truncated if it
2066 * does not fit in with (return) path MTU, but the description was
2067 * removed in the new spec.
2069 if (icmp6_reflect_rt.ro_rt == 0 ||
2070 ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
2071 if (icmp6_reflect_rt.ro_rt) {
2072 RTFREE(icmp6_reflect_rt.ro_rt);
2073 icmp6_reflect_rt.ro_rt = 0;
2075 bzero(sin6, sizeof(*sin6));
2076 sin6->sin6_family = PF_INET6;
2077 sin6->sin6_len = sizeof(struct sockaddr_in6);
2078 sin6->sin6_addr = ip6->ip6_dst;
2080 rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
2084 if (icmp6_reflect_rt.ro_rt == 0)
2087 if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
2088 && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
2089 mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
2091 if (mtu < m->m_pkthdr.len) {
2092 plen -= (m->m_pkthdr.len - mtu);
2093 m_adj(m, mtu - m->m_pkthdr.len);
2097 * If the incoming packet was addressed directly to us(i.e. unicast),
2098 * use dst as the src for the reply.
2099 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2100 * (for example) when we encounter an error while forwarding procedure
2101 * destined to a duplicated address of ours.
2103 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2104 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2105 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2109 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2111 * This is the case if the dst is our link-local address
2112 * and the sender is also ourselves.
2119 struct route_in6 ro;
2122 * This case matches to multicasts, our anycast, or unicasts
2123 * that we do not own. Select a source address based on the
2124 * source address of the erroneous packet.
2126 bzero(&ro, sizeof(ro));
2127 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e);
2129 RTFREE(ro.ro_rt); /* XXX: we could use this */
2132 "icmp6_reflect: source can't be determined: "
2133 "dst=%s, error=%d\n",
2134 ip6_sprintf(&sa6_src.sin6_addr), e));
2139 ip6->ip6_src = *src;
2142 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2143 ip6->ip6_vfc |= IPV6_VERSION;
2144 ip6->ip6_nxt = IPPROTO_ICMPV6;
2145 if (m->m_pkthdr.rcvif) {
2146 /* XXX: This may not be the outgoing interface */
2147 ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim;
2149 ip6->ip6_hlim = ip6_defhlim;
2151 icmp6->icmp6_cksum = 0;
2152 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2153 sizeof(struct ip6_hdr), plen);
2156 * XXX option handling
2159 m->m_flags &= ~(M_BCAST|M_MCAST);
2161 #ifdef COMPAT_RFC1885
2162 ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
2164 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2167 icmp6_ifoutstat_inc(outif, type, code);
2177 icmp6_fasttimo(void)
2184 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2185 struct in6_addr *tgt6)
2187 static char buf[1024];
2188 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2189 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2194 icmp6_redirect_input(struct mbuf *m, int off)
2196 struct ifnet *ifp = m->m_pkthdr.rcvif;
2197 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2198 struct nd_redirect *nd_rd;
2199 int icmp6len = ntohs(ip6->ip6_plen);
2200 char *lladdr = NULL;
2202 u_char *redirhdr = NULL;
2203 int redirhdrlen = 0;
2204 struct rtentry *rt = NULL;
2207 struct in6_addr src6 = ip6->ip6_src;
2208 struct in6_addr redtgt6;
2209 struct in6_addr reddst6;
2210 union nd_opts ndopts;
2215 /* XXX if we are router, we don't update route by icmp6 redirect */
2218 if (!icmp6_rediraccept)
2221 #ifndef PULLDOWN_TEST
2222 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2223 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2225 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2226 if (nd_rd == NULL) {
2227 icmp6stat.icp6s_tooshort++;
2231 redtgt6 = nd_rd->nd_rd_target;
2232 reddst6 = nd_rd->nd_rd_dst;
2234 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2235 redtgt6.s6_addr16[1] = htons(ifp->if_index);
2236 if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2237 reddst6.s6_addr16[1] = htons(ifp->if_index);
2240 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2242 "ICMP6 redirect sent from %s rejected; "
2243 "must be from linklocal\n", ip6_sprintf(&src6)));
2246 if (ip6->ip6_hlim != 255) {
2248 "ICMP6 redirect sent from %s rejected; "
2249 "hlim=%d (must be 255)\n",
2250 ip6_sprintf(&src6), ip6->ip6_hlim));
2254 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2255 struct sockaddr_in6 sin6;
2256 struct in6_addr *gw6;
2258 bzero(&sin6, sizeof(sin6));
2259 sin6.sin6_family = AF_INET6;
2260 sin6.sin6_len = sizeof(struct sockaddr_in6);
2261 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2262 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
2264 if (rt->rt_gateway == NULL ||
2265 rt->rt_gateway->sa_family != AF_INET6) {
2267 "ICMP6 redirect rejected; no route "
2268 "with inet6 gateway found for redirect dst: %s\n",
2269 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2274 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2275 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2277 "ICMP6 redirect rejected; "
2278 "not equal to gw-for-src=%s (must be same): "
2281 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2287 "ICMP6 redirect rejected; "
2288 "no route found for redirect dst: %s\n",
2289 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2295 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2297 "ICMP6 redirect rejected; "
2298 "redirect dst must be unicast: %s\n",
2299 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2303 is_router = is_onlink = 0;
2304 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2305 is_router = 1; /* router case */
2306 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2307 is_onlink = 1; /* on-link destination case */
2308 if (!is_router && !is_onlink) {
2310 "ICMP6 redirect rejected; "
2311 "neither router case nor onlink case: %s\n",
2312 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2315 /* validation passed */
2317 icmp6len -= sizeof(*nd_rd);
2318 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2319 if (nd6_options(&ndopts) < 0) {
2320 nd6log((LOG_INFO, "icmp6_redirect_input: "
2321 "invalid ND option, rejected: %s\n",
2322 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2323 /* nd6_options have incremented stats */
2327 if (ndopts.nd_opts_tgt_lladdr) {
2328 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2329 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2332 if (ndopts.nd_opts_rh) {
2333 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2334 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2337 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2339 "icmp6_redirect_input: lladdrlen mismatch for %s "
2340 "(if %d, icmp6 packet %d): %s\n",
2341 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2342 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2347 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2348 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2350 if (!is_onlink) { /* better router case. perform rtredirect. */
2351 /* perform rtredirect */
2352 struct sockaddr_in6 sdst;
2353 struct sockaddr_in6 sgw;
2354 struct sockaddr_in6 ssrc;
2356 bzero(&sdst, sizeof(sdst));
2357 bzero(&sgw, sizeof(sgw));
2358 bzero(&ssrc, sizeof(ssrc));
2359 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2360 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2361 sizeof(struct sockaddr_in6);
2362 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2363 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2364 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2365 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2366 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2367 (struct sockaddr *)&ssrc,
2368 (struct rtentry **)NULL);
2370 /* finally update cached route in each socket via pfctlinput */
2372 struct sockaddr_in6 sdst;
2374 bzero(&sdst, sizeof(sdst));
2375 sdst.sin6_family = AF_INET6;
2376 sdst.sin6_len = sizeof(struct sockaddr_in6);
2377 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2378 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2380 key_sa_routechange((struct sockaddr *)&sdst);
2389 icmp6stat.icp6s_badredirect++;
2394 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2396 struct ifnet *ifp; /* my outgoing interface */
2397 struct in6_addr *ifp_ll6;
2398 struct in6_addr *router_ll6;
2399 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2400 struct mbuf *m = NULL; /* newly allocated one */
2401 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2402 struct nd_redirect *nd_rd;
2405 struct ifnet *outif = NULL;
2406 struct sockaddr_in6 src_sa;
2408 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2410 /* if we are not router, we don't send icmp6 redirect */
2411 if (!ip6_forwarding || ip6_accept_rtadv)
2415 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2420 * the source address must identify a neighbor, and
2421 * the destination address must not be a multicast address
2422 * [RFC 2461, sec 8.2]
2424 sip6 = mtod(m0, struct ip6_hdr *);
2425 bzero(&src_sa, sizeof(src_sa));
2426 src_sa.sin6_family = AF_INET6;
2427 src_sa.sin6_len = sizeof(src_sa);
2428 src_sa.sin6_addr = sip6->ip6_src;
2429 /* we don't currently use sin6_scope_id, but eventually use it */
2430 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2431 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2433 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2434 goto fail; /* what should we do here? */
2437 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2441 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2442 * we almost always ask for an mbuf cluster for simplicity.
2443 * (MHLEN < IPV6_MMTU is almost always true)
2445 #if IPV6_MMTU >= MCLBYTES
2446 # error assumption failed about IPV6_MMTU and MCLBYTES
2448 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
2449 if (m && IPV6_MMTU >= MHLEN)
2450 MCLGET(m, MB_DONTWAIT);
2453 m->m_pkthdr.rcvif = NULL;
2455 maxlen = M_TRAILINGSPACE(m);
2456 maxlen = min(IPV6_MMTU, maxlen);
2457 /* just for safety */
2458 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2459 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2464 /* get ip6 linklocal address for ifp(my outgoing interface). */
2465 struct in6_ifaddr *ia;
2466 if ((ia = in6ifa_ifpforlinklocal(ifp,
2468 IN6_IFF_ANYCAST)) == NULL)
2470 ifp_ll6 = &ia->ia_addr.sin6_addr;
2473 /* get ip6 linklocal address for the router. */
2474 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2475 struct sockaddr_in6 *sin6;
2476 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2477 router_ll6 = &sin6->sin6_addr;
2478 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2479 router_ll6 = (struct in6_addr *)NULL;
2481 router_ll6 = (struct in6_addr *)NULL;
2484 ip6 = mtod(m, struct ip6_hdr *);
2486 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2487 ip6->ip6_vfc |= IPV6_VERSION;
2488 /* ip6->ip6_plen will be set later */
2489 ip6->ip6_nxt = IPPROTO_ICMPV6;
2490 ip6->ip6_hlim = 255;
2491 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2492 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2493 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2496 nd_rd = (struct nd_redirect *)(ip6 + 1);
2497 nd_rd->nd_rd_type = ND_REDIRECT;
2498 nd_rd->nd_rd_code = 0;
2499 nd_rd->nd_rd_reserved = 0;
2500 if (rt->rt_flags & RTF_GATEWAY) {
2502 * nd_rd->nd_rd_target must be a link-local address in
2503 * better router cases.
2507 bcopy(router_ll6, &nd_rd->nd_rd_target,
2508 sizeof(nd_rd->nd_rd_target));
2509 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2510 sizeof(nd_rd->nd_rd_dst));
2512 /* make sure redtgt == reddst */
2513 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2514 sizeof(nd_rd->nd_rd_target));
2515 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2516 sizeof(nd_rd->nd_rd_dst));
2519 p = (u_char *)(nd_rd + 1);
2525 /* target lladdr option */
2526 struct rtentry *rt_router = NULL;
2528 struct sockaddr_dl *sdl;
2529 struct nd_opt_hdr *nd_opt;
2532 rt_router = nd6_lookup(router_ll6, 0, ifp);
2535 len = sizeof(*nd_opt) + ifp->if_addrlen;
2536 len = (len + 7) & ~7; /* round by 8 */
2538 if (len + (p - (u_char *)ip6) > maxlen)
2540 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2541 (rt_router->rt_flags & RTF_LLINFO) &&
2542 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2543 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2545 nd_opt = (struct nd_opt_hdr *)p;
2546 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2547 nd_opt->nd_opt_len = len >> 3;
2548 lladdr = (char *)(nd_opt + 1);
2549 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2555 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2557 /* just to be safe */
2558 #ifdef M_DECRYPTED /*not openbsd*/
2559 if (m0->m_flags & M_DECRYPTED)
2562 if (p - (u_char *)ip6 > maxlen)
2566 /* redirected header option */
2568 struct nd_opt_rd_hdr *nd_opt_rh;
2571 * compute the maximum size for icmp6 redirect header option.
2572 * XXX room for auth header?
2574 len = maxlen - (p - (u_char *)ip6);
2577 /* This is just for simplicity. */
2578 if (m0->m_pkthdr.len != m0->m_len) {
2580 m_freem(m0->m_next);
2583 m0->m_pkthdr.len = m0->m_len;
2587 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2588 * about padding/truncate rule for the original IP packet.
2589 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2590 * - "attach as much as possible" is the goal
2591 * - pad if not aligned (original size can be guessed by original
2593 * Following code adds the padding if it is simple enough,
2594 * and truncates if not.
2596 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2597 panic("assumption failed in %s:%d", __FILE__, __LINE__);
2599 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2600 /* not enough room, truncate */
2601 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2603 /* enough room, pad or truncate */
2606 extra = m0->m_pkthdr.len % 8;
2608 /* pad if easy enough, truncate if not */
2609 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2611 m0->m_len += (8 - extra);
2612 m0->m_pkthdr.len += (8 - extra);
2615 m0->m_pkthdr.len -= extra;
2619 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2620 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2623 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2624 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2625 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2626 nd_opt_rh->nd_opt_rh_len = len >> 3;
2627 p += sizeof(*nd_opt_rh);
2628 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2630 /* connect m0 to m */
2632 m->m_pkthdr.len = m->m_len + m0->m_len;
2636 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2637 sip6->ip6_src.s6_addr16[1] = 0;
2638 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2639 sip6->ip6_dst.s6_addr16[1] = 0;
2641 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2642 ip6->ip6_src.s6_addr16[1] = 0;
2643 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2644 ip6->ip6_dst.s6_addr16[1] = 0;
2646 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2647 nd_rd->nd_rd_target.s6_addr16[1] = 0;
2648 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2649 nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2651 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2653 nd_rd->nd_rd_cksum = 0;
2655 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2657 /* send the packet to outside... */
2658 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2660 icmp6_ifstat_inc(outif, ifs6_out_msg);
2661 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2663 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2674 #ifdef HAVE_NRL_INPCB
2675 #define sotoin6pcb sotoinpcb
2676 #define in6pcb inpcb
2677 #define in6p_icmp6filt inp_icmp6filt
2680 * ICMPv6 socket option processing.
2683 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2687 struct inpcb *inp = sotoinpcb(so);
2688 int level, op, optname;
2691 level = sopt->sopt_level;
2692 op = sopt->sopt_dir;
2693 optname = sopt->sopt_name;
2694 optlen = sopt->sopt_valsize;
2696 level = op = optname = optlen = 0;
2698 if (level != IPPROTO_ICMPV6) {
2707 struct icmp6_filter *p;
2709 if (optlen != sizeof(*p)) {
2713 if (inp->in6p_icmp6filt == NULL) {
2717 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2723 error = ENOPROTOOPT;
2732 if (inp->in6p_icmp6filt == NULL) {
2736 error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2737 sizeof(struct icmp6_filter));
2742 error = ENOPROTOOPT;
2750 #ifdef HAVE_NRL_INPCB
2753 #undef in6p_icmp6filt
2756 #ifndef HAVE_PPSRATECHECK
2758 #define timersub(tvp, uvp, vvp) \
2760 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2761 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2762 if ((vvp)->tv_usec < 0) { \
2764 (vvp)->tv_usec += 1000000; \
2770 * ppsratecheck(): packets (or events) per second limitation.
2773 ppsratecheck(struct timeval *lasttime, int *curpps,
2774 int maxpps) /* maximum pps allowed */
2776 struct timeval tv, delta;
2781 timersub(&tv, lasttime, &delta);
2784 * Check for 0,0 so that the message will be seen at least once.
2785 * If more than one second has passed since the last update of
2786 * lasttime, reset the counter.
2788 * We do increment *curpps even in *curpps < maxpps case, as some may
2789 * try to use *curpps for stat purposes as well.
2791 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
2792 delta.tv_sec >= 1) {
2796 } else if (maxpps < 0)
2798 else if (*curpps < maxpps)
2803 #if 1 /* DIAGNOSTIC? */
2804 /* be careful about wrap-around */
2805 if (*curpps + 1 > *curpps)
2806 *curpps = *curpps + 1;
2809 * assume that there's not too many calls to this function.
2810 * not sure if the assumption holds, as it depends on *caller's*
2811 * behavior, not the behavior of this function.
2812 * IMHO it is wrong to make assumption on the caller's behavior,
2813 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
2815 *curpps = *curpps + 1;
2823 * Perform rate limit check.
2824 * Returns 0 if it is okay to send the icmp6 packet.
2825 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2828 * XXX per-destination/type check necessary?
2831 icmp6_ratelimit(const struct in6_addr *dst, /* not used at this moment */
2832 const int type, /* not used at this moment */
2833 const int code) /* not used at this moment */
2837 ret = 0; /* okay to send */
2840 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2842 /* The packet is subject to rate limit */