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.21 2006/01/14 11:44:24 swildner 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>
108 #include <netproto/ipsec/ipsec.h>
109 #include <netproto/ipsec/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 in6_pcbdetach in_pcbdetach
133 #define in6_rtchange in_rtchange
136 * for KAME src sync over BSD*'s. XXX: FreeBSD (>=3) are VERY different from
139 #define in6p_ip6_nxt inp_ipv6.ip6_nxt
143 * ppratecheck() is available, so define it here.
145 #define HAVE_PPSRATECHECK
147 extern struct domain inet6domain;
148 extern struct ip6protosw inet6sw[];
149 extern u_char ip6_protox[];
151 struct icmp6stat icmp6stat;
153 extern struct inpcbinfo ripcbinfo;
154 extern int icmp6errppslim;
155 static int icmp6errpps_count = 0;
156 static struct timeval icmp6errppslim_last;
157 extern int icmp6_nodeinfo;
159 static void icmp6_errcount (struct icmp6errstat *, int, int);
160 static int icmp6_rip6_input (struct mbuf **, int);
161 static int icmp6_ratelimit (const struct in6_addr *, const int, const int);
162 static const char *icmp6_redirect_diag (struct in6_addr *,
163 struct in6_addr *, struct in6_addr *);
164 #ifndef HAVE_PPSRATECHECK
165 static int ppsratecheck (struct timeval *, int *, int);
167 static struct mbuf *ni6_input (struct mbuf *, int);
168 static struct mbuf *ni6_nametodns (const char *, int, int);
169 static int ni6_dnsmatch (const char *, int, const char *, int);
170 static int ni6_addrs (struct icmp6_nodeinfo *, struct mbuf *,
171 struct ifnet **, char *);
172 static int ni6_store_addrs (struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
173 struct ifnet *, int);
174 static int icmp6_notify_error (struct mbuf *, int, int, int);
176 #ifdef COMPAT_RFC1885
177 static struct route_in6 icmp6_reflect_rt;
188 icmp6_errcount(struct icmp6errstat *stat, int type, int code)
191 case ICMP6_DST_UNREACH:
193 case ICMP6_DST_UNREACH_NOROUTE:
194 stat->icp6errs_dst_unreach_noroute++;
196 case ICMP6_DST_UNREACH_ADMIN:
197 stat->icp6errs_dst_unreach_admin++;
199 case ICMP6_DST_UNREACH_BEYONDSCOPE:
200 stat->icp6errs_dst_unreach_beyondscope++;
202 case ICMP6_DST_UNREACH_ADDR:
203 stat->icp6errs_dst_unreach_addr++;
205 case ICMP6_DST_UNREACH_NOPORT:
206 stat->icp6errs_dst_unreach_noport++;
210 case ICMP6_PACKET_TOO_BIG:
211 stat->icp6errs_packet_too_big++;
213 case ICMP6_TIME_EXCEEDED:
215 case ICMP6_TIME_EXCEED_TRANSIT:
216 stat->icp6errs_time_exceed_transit++;
218 case ICMP6_TIME_EXCEED_REASSEMBLY:
219 stat->icp6errs_time_exceed_reassembly++;
223 case ICMP6_PARAM_PROB:
225 case ICMP6_PARAMPROB_HEADER:
226 stat->icp6errs_paramprob_header++;
228 case ICMP6_PARAMPROB_NEXTHEADER:
229 stat->icp6errs_paramprob_nextheader++;
231 case ICMP6_PARAMPROB_OPTION:
232 stat->icp6errs_paramprob_option++;
237 stat->icp6errs_redirect++;
240 stat->icp6errs_unknown++;
244 * Generate an error packet of type error in response to bad IP6 packet.
247 icmp6_error(struct mbuf *m, int type, int code, int param)
249 struct ip6_hdr *oip6, *nip6;
250 struct icmp6_hdr *icmp6;
255 icmp6stat.icp6s_error++;
257 /* count per-type-code statistics */
258 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
260 #ifdef M_DECRYPTED /*not openbsd*/
261 if (m->m_flags & M_DECRYPTED) {
262 icmp6stat.icp6s_canterror++;
267 #ifndef PULLDOWN_TEST
268 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
270 if (m->m_len < sizeof(struct ip6_hdr)) {
271 m = m_pullup(m, sizeof(struct ip6_hdr));
276 oip6 = mtod(m, struct ip6_hdr *);
279 * Multicast destination check. For unrecognized option errors,
280 * this check has already done in ip6_unknown_opt(), so we can
281 * check only for other errors.
283 if ((m->m_flags & (M_BCAST|M_MCAST) ||
284 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
285 (type != ICMP6_PACKET_TOO_BIG &&
286 (type != ICMP6_PARAM_PROB ||
287 code != ICMP6_PARAMPROB_OPTION)))
290 /* Source address check. XXX: the case of anycast source? */
291 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
292 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
296 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
300 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
301 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
302 struct icmp6_hdr *icp;
304 #ifndef PULLDOWN_TEST
305 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
306 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
308 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
311 icmp6stat.icp6s_tooshort++;
315 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
316 icp->icmp6_type == ND_REDIRECT) {
319 * Special case: for redirect (which is
320 * informational) we must not send icmp6 error.
322 icmp6stat.icp6s_canterror++;
325 /* ICMPv6 informational - send the error */
328 /* non-ICMPv6 - send the error */
331 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
333 /* Finally, do rate limitation check. */
334 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
335 icmp6stat.icp6s_toofreq++;
340 * OK, ICMP6 can be generated.
343 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
344 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
346 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
347 M_PREPEND(m, preplen, MB_DONTWAIT);
348 if (m && m->m_len < preplen)
349 m = m_pullup(m, preplen);
351 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
355 nip6 = mtod(m, struct ip6_hdr *);
356 nip6->ip6_src = oip6->ip6_src;
357 nip6->ip6_dst = oip6->ip6_dst;
359 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
360 oip6->ip6_src.s6_addr16[1] = 0;
361 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
362 oip6->ip6_dst.s6_addr16[1] = 0;
364 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
365 icmp6->icmp6_type = type;
366 icmp6->icmp6_code = code;
367 icmp6->icmp6_pptr = htonl((u_int32_t)param);
370 * icmp6_reflect() is designed to be in the input path.
371 * icmp6_error() can be called from both input and outut path,
372 * and if we are in output path rcvif could contain bogus value.
373 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
374 * information in ip header (nip6).
376 m->m_pkthdr.rcvif = NULL;
378 icmp6stat.icp6s_outhist[type]++;
379 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
385 * If we can't tell wheter or not we can generate ICMP6, free it.
391 * Process a received ICMP6 message.
394 icmp6_input(struct mbuf **mp, int *offp, int proto)
396 struct mbuf *m = *mp, *n;
397 struct ip6_hdr *ip6, *nip6;
398 struct icmp6_hdr *icmp6, *nicmp6;
400 int icmp6len = m->m_pkthdr.len - *offp;
403 #ifndef PULLDOWN_TEST
404 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
405 /* m might change if M_LOOP. So, call mtod after this */
409 * Locate icmp6 structure in mbuf, and check
410 * that not corrupted and of at least minimum length
413 ip6 = mtod(m, struct ip6_hdr *);
414 if (icmp6len < sizeof(struct icmp6_hdr)) {
415 icmp6stat.icp6s_tooshort++;
420 * calculate the checksum
422 #ifndef PULLDOWN_TEST
423 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
425 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
427 icmp6stat.icp6s_tooshort++;
431 code = icmp6->icmp6_code;
433 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
435 "ICMP6 checksum error(%d|%x) %s\n",
436 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
437 icmp6stat.icp6s_checksum++;
441 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
443 * Deliver very specific ICMP6 type only.
444 * This is important to deilver TOOBIG. Otherwise PMTUD
447 switch (icmp6->icmp6_type) {
448 case ICMP6_DST_UNREACH:
449 case ICMP6_PACKET_TOO_BIG:
450 case ICMP6_TIME_EXCEEDED:
457 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
458 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
459 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
460 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
462 switch (icmp6->icmp6_type) {
463 case ICMP6_DST_UNREACH:
464 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
466 case ICMP6_DST_UNREACH_NOROUTE:
467 code = PRC_UNREACH_NET;
469 case ICMP6_DST_UNREACH_ADMIN:
470 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
471 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
473 case ICMP6_DST_UNREACH_ADDR:
476 #ifdef COMPAT_RFC1885
477 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
478 code = PRC_UNREACH_SRCFAIL;
481 case ICMP6_DST_UNREACH_BEYONDSCOPE:
482 /* I mean "source address was incorrect." */
483 code = PRC_PARAMPROB;
486 case ICMP6_DST_UNREACH_NOPORT:
487 code = PRC_UNREACH_PORT;
495 case ICMP6_PACKET_TOO_BIG:
496 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
503 * Updating the path MTU will be done after examining
504 * intermediate extension headers.
509 case ICMP6_TIME_EXCEEDED:
510 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
512 case ICMP6_TIME_EXCEED_TRANSIT:
513 case ICMP6_TIME_EXCEED_REASSEMBLY:
514 code += PRC_TIMXCEED_INTRANS;
522 case ICMP6_PARAM_PROB:
523 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
525 case ICMP6_PARAMPROB_NEXTHEADER:
526 code = PRC_UNREACH_PROTOCOL;
528 case ICMP6_PARAMPROB_HEADER:
529 case ICMP6_PARAMPROB_OPTION:
530 code = PRC_PARAMPROB;
538 case ICMP6_ECHO_REQUEST:
539 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
542 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
546 if ((n->m_flags & M_EXT) != 0
547 || n->m_len < off + sizeof(struct icmp6_hdr)) {
549 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
553 * Prepare an internal mbuf. m_pullup() doesn't
554 * always copy the length we specified.
556 if (maxlen >= MCLBYTES) {
561 MGETHDR(n, MB_DONTWAIT, n0->m_type);
562 n0len = n0->m_pkthdr.len; /* save for use below */
564 M_MOVE_PKTHDR(n, n0);
565 if (n && maxlen >= MHLEN) {
566 MCLGET(n, MB_DONTWAIT);
567 if ((n->m_flags & M_EXT) == 0) {
578 * Copy IPv6 and ICMPv6 only.
580 nip6 = mtod(n, struct ip6_hdr *);
581 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
582 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
583 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
584 noff = sizeof(struct ip6_hdr);
585 /* new mbuf contains only ipv6+icmpv6 headers */
586 n->m_len = noff + sizeof(struct icmp6_hdr);
588 * Adjust mbuf. ip6_plen will be adjusted in
591 m_adj(n0, off + sizeof(struct icmp6_hdr));
592 /* recalculate complete packet size */
593 n->m_pkthdr.len = n0len + (noff - off);
596 nip6 = mtod(n, struct ip6_hdr *);
597 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
600 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
601 nicmp6->icmp6_code = 0;
603 icmp6stat.icp6s_reflect++;
604 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
605 icmp6_reflect(n, noff);
609 case ICMP6_ECHO_REPLY:
610 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
615 case MLD_LISTENER_QUERY:
616 case MLD_LISTENER_REPORT:
617 if (icmp6len < sizeof(struct mld_hdr))
619 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
620 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
622 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
623 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
633 case MLD_LISTENER_DONE:
634 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
635 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
637 break; /* nothing to be done in kernel */
639 case MLD_MTRACE_RESP:
641 /* XXX: these two are experimental. not officially defind. */
642 /* XXX: per-interface statistics? */
643 break; /* just pass it to applications */
645 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
647 enum { WRU, FQDN } mode;
652 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
654 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
659 #define hostnamelen strlen(hostname)
661 #ifndef PULLDOWN_TEST
662 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
665 n = m_copy(m, 0, M_COPYALL);
667 n = ni6_input(n, off);
668 /* XXX meaningless if n == NULL */
669 noff = sizeof(struct ip6_hdr);
674 if ((icmp6_nodeinfo & 5) != 5)
679 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
680 if (maxlen >= MCLBYTES) {
684 MGETHDR(n, MB_DONTWAIT, m->m_type);
685 if (n && maxlen > MHLEN) {
686 MCLGET(n, MB_DONTWAIT);
687 if ((n->m_flags & M_EXT) == 0) {
692 if (!m_dup_pkthdr(n, m, MB_DONTWAIT)) {
694 * Previous code did a blind M_COPY_PKTHDR
695 * and said "just for rcvif". If true, then
696 * we could tolerate the dup failing (due to
697 * the deep copy of the tag chain). For now
698 * be conservative and just fail.
707 n->m_pkthdr.rcvif = NULL;
709 maxhlen = M_TRAILINGSPACE(n) - maxlen;
710 if (maxhlen > hostnamelen)
711 maxhlen = hostnamelen;
713 * Copy IPv6 and ICMPv6 only.
715 nip6 = mtod(n, struct ip6_hdr *);
716 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
717 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
718 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
719 p = (u_char *)(nicmp6 + 1);
721 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
722 noff = sizeof(struct ip6_hdr);
723 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
724 sizeof(struct icmp6_hdr) + 4 + maxhlen;
725 nicmp6->icmp6_type = ICMP6_WRUREPLY;
726 nicmp6->icmp6_code = 0;
730 icmp6stat.icp6s_reflect++;
731 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
732 icmp6_reflect(n, noff);
742 case ND_ROUTER_SOLICIT:
743 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
746 if (icmp6len < sizeof(struct nd_router_solicit))
748 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
750 nd6_rs_input(m, off, icmp6len);
754 nd6_rs_input(n, off, icmp6len);
758 case ND_ROUTER_ADVERT:
759 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
762 if (icmp6len < sizeof(struct nd_router_advert))
764 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
766 nd6_ra_input(m, off, icmp6len);
770 nd6_ra_input(n, off, icmp6len);
774 case ND_NEIGHBOR_SOLICIT:
775 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
778 if (icmp6len < sizeof(struct nd_neighbor_solicit))
780 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
782 nd6_ns_input(m, off, icmp6len);
786 nd6_ns_input(n, off, icmp6len);
790 case ND_NEIGHBOR_ADVERT:
791 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
794 if (icmp6len < sizeof(struct nd_neighbor_advert))
796 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
798 nd6_na_input(m, off, icmp6len);
802 nd6_na_input(n, off, icmp6len);
807 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
810 if (icmp6len < sizeof(struct nd_redirect))
812 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
814 icmp6_redirect_input(m, off);
818 icmp6_redirect_input(n, off);
822 case ICMP6_ROUTER_RENUMBERING:
823 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
824 code != ICMP6_ROUTER_RENUMBERING_RESULT)
826 if (icmp6len < sizeof(struct icmp6_router_renum))
832 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
833 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
834 ip6_sprintf(&ip6->ip6_dst),
835 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
836 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
837 /* ICMPv6 error: MUST deliver it by spec... */
841 /* ICMPv6 informational: MUST not deliver */
845 if (icmp6_notify_error(m, off, icmp6len, code)) {
846 /* In this case, m should've been freed. */
847 return(IPPROTO_DONE);
852 icmp6stat.icp6s_badcode++;
856 icmp6stat.icp6s_badlen++;
860 /* deliver the packet to appropriate sockets */
861 icmp6_rip6_input(&m, *offp);
871 icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
873 struct icmp6_hdr *icmp6;
874 struct ip6_hdr *eip6;
876 struct sockaddr_in6 icmp6src, icmp6dst;
878 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
879 icmp6stat.icp6s_tooshort++;
882 #ifndef PULLDOWN_TEST
883 IP6_EXTHDR_CHECK(m, off,
884 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
886 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
888 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
889 sizeof(*icmp6) + sizeof(struct ip6_hdr));
891 icmp6stat.icp6s_tooshort++;
895 eip6 = (struct ip6_hdr *)(icmp6 + 1);
897 /* Detect the upper level protocol */
899 void (*ctlfunc) (int, struct sockaddr *, void *);
900 u_int8_t nxt = eip6->ip6_nxt;
901 int eoff = off + sizeof(struct icmp6_hdr) +
902 sizeof(struct ip6_hdr);
903 struct ip6ctlparam ip6cp;
904 struct in6_addr *finaldst = NULL;
905 int icmp6type = icmp6->icmp6_type;
907 struct ip6_rthdr *rth;
908 struct ip6_rthdr0 *rth0;
911 while (1) { /* XXX: should avoid infinite loop explicitly? */
915 case IPPROTO_HOPOPTS:
916 case IPPROTO_DSTOPTS:
918 #ifndef PULLDOWN_TEST
919 IP6_EXTHDR_CHECK(m, 0, eoff +
920 sizeof(struct ip6_ext),
922 eh = (struct ip6_ext *)(mtod(m, caddr_t)
925 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
928 icmp6stat.icp6s_tooshort++;
933 if (nxt == IPPROTO_AH)
934 eoff += (eh->ip6e_len + 2) << 2;
936 eoff += (eh->ip6e_len + 1) << 3;
939 case IPPROTO_ROUTING:
941 * When the erroneous packet contains a
942 * routing header, we should examine the
943 * header to determine the final destination.
944 * Otherwise, we can't properly update
945 * information that depends on the final
946 * destination (e.g. path MTU).
948 #ifndef PULLDOWN_TEST
949 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
951 rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
954 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
957 icmp6stat.icp6s_tooshort++;
961 rthlen = (rth->ip6r_len + 1) << 3;
963 * XXX: currently there is no
964 * officially defined type other
966 * Note that if the segment left field
967 * is 0, all intermediate hops must
970 if (rth->ip6r_segleft &&
971 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
974 #ifndef PULLDOWN_TEST
975 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen,
977 rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff);
980 struct ip6_rthdr0 *, m,
983 icmp6stat.icp6s_tooshort++;
987 /* just ignore a bogus header */
988 if ((rth0->ip6r0_len % 2) == 0 &&
989 (hops = rth0->ip6r0_len/2))
990 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
995 case IPPROTO_FRAGMENT:
996 #ifndef PULLDOWN_TEST
997 IP6_EXTHDR_CHECK(m, 0, eoff +
998 sizeof(struct ip6_frag),
1000 fh = (struct ip6_frag *)(mtod(m, caddr_t)
1003 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1006 icmp6stat.icp6s_tooshort++;
1011 * Data after a fragment header is meaningless
1012 * unless it is the first fragment, but
1013 * we'll go to the notify label for path MTU
1016 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1019 eoff += sizeof(struct ip6_frag);
1024 * This case includes ESP and the No Next
1025 * Header. In such cases going to the notify
1026 * label does not have any meaning
1027 * (i.e. ctlfunc will be NULL), but we go
1028 * anyway since we might have to update
1029 * path MTU information.
1035 #ifndef PULLDOWN_TEST
1036 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1038 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1039 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1040 if (icmp6 == NULL) {
1041 icmp6stat.icp6s_tooshort++;
1046 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1047 bzero(&icmp6dst, sizeof(icmp6dst));
1048 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1049 icmp6dst.sin6_family = AF_INET6;
1050 if (finaldst == NULL)
1051 icmp6dst.sin6_addr = eip6->ip6_dst;
1053 icmp6dst.sin6_addr = *finaldst;
1054 icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1055 &icmp6dst.sin6_addr);
1056 if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
1058 /* should be impossbile */
1060 "icmp6_notify_error: in6_embedscope failed\n"));
1065 * retrieve parameters from the inner IPv6 header, and convert
1066 * them into sockaddr structures.
1068 bzero(&icmp6src, sizeof(icmp6src));
1069 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1070 icmp6src.sin6_family = AF_INET6;
1071 icmp6src.sin6_addr = eip6->ip6_src;
1072 icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1073 &icmp6src.sin6_addr);
1074 if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
1076 /* should be impossbile */
1078 "icmp6_notify_error: in6_embedscope failed\n"));
1081 icmp6src.sin6_flowinfo =
1082 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1084 if (finaldst == NULL)
1085 finaldst = &eip6->ip6_dst;
1087 ip6cp.ip6c_icmp6 = icmp6;
1088 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1089 ip6cp.ip6c_off = eoff;
1090 ip6cp.ip6c_finaldst = finaldst;
1091 ip6cp.ip6c_src = &icmp6src;
1092 ip6cp.ip6c_nxt = nxt;
1094 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1095 notifymtu = ntohl(icmp6->icmp6_mtu);
1096 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1097 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1100 ctlfunc = (void (*) (int, struct sockaddr *, void *))
1101 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1103 (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, &ip6cp);
1114 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1116 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1117 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1118 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1119 u_int mtu = ntohl(icmp6->icmp6_mtu);
1120 struct rtentry *rt = NULL;
1121 struct sockaddr_in6 sin6;
1126 bzero(&sin6, sizeof(sin6));
1127 sin6.sin6_family = PF_INET6;
1128 sin6.sin6_len = sizeof(struct sockaddr_in6);
1129 sin6.sin6_addr = *dst;
1130 /* XXX normally, this won't happen */
1131 if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1132 sin6.sin6_addr.s6_addr16[1] =
1133 htons(m->m_pkthdr.rcvif->if_index);
1135 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1136 rt = rtpurelookup((struct sockaddr *)&sin6);
1137 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
1138 !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1139 if (mtu < IPV6_MMTU) { /* XXX */
1140 rt->rt_rmx.rmx_locks |= RTV_MTU;
1141 } else if (mtu < rt->rt_ifp->if_mtu &&
1142 rt->rt_rmx.rmx_mtu > mtu) {
1143 icmp6stat.icp6s_pmtuchg++;
1144 rt->rt_rmx.rmx_mtu = mtu;
1152 * Process a Node Information Query packet, based on
1153 * draft-ietf-ipngwg-icmp-name-lookups-07.
1155 * Spec incompatibilities:
1156 * - IPv6 Subject address handling
1157 * - IPv4 Subject address handling support missing
1158 * - Proxy reply (answer even if it's not for me)
1159 * - joins NI group address at in6_ifattach() time only, does not cope
1160 * with hostname changes by sethostname(3)
1162 #define hostnamelen strlen(hostname)
1163 static struct mbuf *
1164 ni6_input(struct mbuf *m, int off)
1166 struct icmp6_nodeinfo *ni6, *nni6;
1167 struct mbuf *n = NULL;
1170 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1171 struct ni_reply_fqdn *fqdn;
1172 int addrs; /* for NI_QTYPE_NODEADDR */
1173 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1174 struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1175 struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
1176 struct ip6_hdr *ip6;
1177 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1179 struct in6_ifaddr *ia6 = NULL;
1181 ip6 = mtod(m, struct ip6_hdr *);
1182 #ifndef PULLDOWN_TEST
1183 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1185 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1187 /* m is already reclaimed */
1193 * Validate IPv6 destination address.
1195 * The Responder must discard the Query without further processing
1196 * unless it is one of the Responder's unicast or anycast addresses, or
1197 * a link-local scope multicast address which the Responder has joined.
1198 * [icmp-name-lookups-07, Section 4.]
1200 bzero(&sin6, sizeof(sin6));
1201 sin6.sin6_family = AF_INET6;
1202 sin6.sin6_len = sizeof(struct sockaddr_in6);
1203 bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1205 if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
1206 /* unicast/anycast, fine */
1207 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1208 (icmp6_nodeinfo & 4) == 0) {
1209 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1210 "a temporary address in %s:%d",
1211 __FILE__, __LINE__));
1214 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1215 ; /* link-local multicast, fine */
1219 /* validate query Subject field. */
1220 qtype = ntohs(ni6->ni_qtype);
1221 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1224 case NI_QTYPE_SUPTYPES:
1226 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1230 case NI_QTYPE_NODEADDR:
1231 switch (ni6->ni_code) {
1232 case ICMP6_NI_SUBJ_IPV6:
1233 #if ICMP6_NI_SUBJ_IPV6 != 0
1237 * backward compatibility - try to accept 03 draft
1238 * format, where no Subject is present.
1240 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1245 #if ICMP6_NI_SUBJ_IPV6 != 0
1246 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1250 if (subjlen != sizeof(sin6.sin6_addr))
1254 * Validate Subject address.
1256 * Not sure what exactly "address belongs to the node"
1257 * means in the spec, is it just unicast, or what?
1259 * At this moment we consider Subject address as
1260 * "belong to the node" if the Subject address equals
1261 * to the IPv6 destination address; validation for
1262 * IPv6 destination address should have done enough
1265 * We do not do proxy at this moment.
1267 /* m_pulldown instead of copy? */
1268 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1269 subjlen, (caddr_t)&sin6.sin6_addr);
1270 sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1272 in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL);
1273 bzero(&sin6_d, sizeof(sin6_d));
1274 sin6_d.sin6_family = AF_INET6; /* not used, actually */
1275 sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
1276 sin6_d.sin6_addr = ip6->ip6_dst;
1277 sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1279 in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL);
1280 subj = (char *)&sin6;
1281 if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
1285 * XXX if we are to allow other cases, we should really
1286 * be careful about scope here.
1287 * basically, we should disallow queries toward IPv6
1288 * destination X with subject Y, if scope(X) > scope(Y).
1289 * if we allow scope(X) > scope(Y), it will result in
1290 * information leakage across scope boundary.
1294 case ICMP6_NI_SUBJ_FQDN:
1296 * Validate Subject name with gethostname(3).
1298 * The behavior may need some debate, since:
1299 * - we are not sure if the node has FQDN as
1300 * hostname (returned by gethostname(3)).
1301 * - the code does wildcard match for truncated names.
1302 * however, we are not sure if we want to perform
1303 * wildcard match, if gethostname(3) side has
1304 * truncated hostname.
1306 n = ni6_nametodns(hostname, hostnamelen, 0);
1307 if (!n || n->m_next || n->m_len == 0)
1309 IP6_EXTHDR_GET(subj, char *, m,
1310 off + sizeof(struct icmp6_nodeinfo), subjlen);
1313 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1321 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1328 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1331 if ((icmp6_nodeinfo & 1) == 0)
1334 case NI_QTYPE_NODEADDR:
1335 if ((icmp6_nodeinfo & 2) == 0)
1340 /* guess reply length */
1343 break; /* no reply data */
1344 case NI_QTYPE_SUPTYPES:
1345 replylen += sizeof(u_int32_t);
1348 /* XXX will append an mbuf */
1349 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1351 case NI_QTYPE_NODEADDR:
1352 addrs = ni6_addrs(ni6, m, &ifp, subj);
1353 if ((replylen += addrs * (sizeof(struct in6_addr) +
1354 sizeof(u_int32_t))) > MCLBYTES)
1355 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1359 * XXX: We must return a reply with the ICMP6 code
1360 * `unknown Qtype' in this case. However we regard the case
1361 * as an FQDN query for backward compatibility.
1362 * Older versions set a random value to this field,
1363 * so it rarely varies in the defined qtypes.
1364 * But the mechanism is not reliable...
1365 * maybe we should obsolete older versions.
1367 qtype = NI_QTYPE_FQDN;
1368 /* XXX will append an mbuf */
1369 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1374 /* allocate an mbuf to reply. */
1375 MGETHDR(n, MB_DONTWAIT, m->m_type);
1380 M_MOVE_PKTHDR(n, m); /* just for recvif */
1381 if (replylen > MHLEN) {
1382 if (replylen > MCLBYTES) {
1384 * XXX: should we try to allocate more? But MCLBYTES
1385 * is probably much larger than IPV6_MMTU...
1389 MCLGET(n, MB_DONTWAIT);
1390 if ((n->m_flags & M_EXT) == 0) {
1394 n->m_pkthdr.len = n->m_len = replylen;
1396 /* copy mbuf header and IPv6 + Node Information base headers */
1397 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1398 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1399 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1401 /* qtype dependent procedure */
1404 nni6->ni_code = ICMP6_NI_SUCCESS;
1407 case NI_QTYPE_SUPTYPES:
1410 nni6->ni_code = ICMP6_NI_SUCCESS;
1411 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1412 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1413 v = (u_int32_t)htonl(0x0000000f);
1414 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1418 nni6->ni_code = ICMP6_NI_SUCCESS;
1419 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1420 sizeof(struct ip6_hdr) +
1421 sizeof(struct icmp6_nodeinfo));
1422 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1423 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1425 * XXX do we really have FQDN in variable "hostname"?
1427 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1428 if (n->m_next == NULL)
1430 /* XXX we assume that n->m_next is not a chain */
1431 if (n->m_next->m_next != NULL)
1433 n->m_pkthdr.len += n->m_next->m_len;
1435 case NI_QTYPE_NODEADDR:
1439 nni6->ni_code = ICMP6_NI_SUCCESS;
1440 n->m_pkthdr.len = n->m_len =
1441 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1442 lenlim = M_TRAILINGSPACE(n);
1443 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1444 /* XXX: reset mbuf length */
1445 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1446 sizeof(struct icmp6_nodeinfo) + copied;
1450 break; /* XXX impossible! */
1453 nni6->ni_type = ICMP6_NI_REPLY;
1466 * make a mbuf with DNS-encoded string. no compression support.
1468 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1469 * treated as truncated name (two \0 at the end). this is a wild guess.
1471 static struct mbuf *
1472 ni6_nametodns(const char *name, int namelen,
1473 int old) /* return pascal string if non-zero */
1485 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1486 MGET(m, MB_DONTWAIT, MT_DATA);
1487 if (m && len > MLEN) {
1488 MCLGET(m, MB_DONTWAIT);
1489 if ((m->m_flags & M_EXT) == 0)
1498 *mtod(m, char *) = namelen;
1499 bcopy(name, mtod(m, char *) + 1, namelen);
1503 cp = mtod(m, char *);
1504 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1506 /* if not certain about my name, return empty buffer */
1511 * guess if it looks like shortened hostname, or FQDN.
1512 * shortened hostname needs two trailing "\0".
1515 for (p = name; p < name + namelen; p++) {
1516 if (*p && *p == '.')
1525 while (cp < ep && p < name + namelen) {
1527 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1529 /* result does not fit into mbuf */
1530 if (cp + i + 1 >= ep)
1533 * DNS label length restriction, RFC1035 page 8.
1534 * "i == 0" case is included here to avoid returning
1535 * 0-length label on "foo..bar".
1537 if (i <= 0 || i >= 64)
1543 if (p < name + namelen && *p == '.')
1547 if (cp + nterm >= ep)
1551 m->m_len = cp - mtod(m, char *);
1555 panic("should not reach here");
1565 * check if two DNS-encoded string matches. takes care of truncated
1566 * form (with \0\0 at the end). no compression support.
1567 * XXX upper/lowercase match (see RFC2065)
1570 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1572 const char *a0, *b0;
1575 /* simplest case - need validation? */
1576 if (alen == blen && bcmp(a, b, alen) == 0)
1582 /* termination is mandatory */
1583 if (alen < 2 || blen < 2)
1585 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1590 while (a - a0 < alen && b - b0 < blen) {
1591 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1594 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1596 /* we don't support compression yet */
1597 if (a[0] >= 64 || b[0] >= 64)
1600 /* truncated case */
1601 if (a[0] == 0 && a - a0 == alen - 1)
1603 if (b[0] == 0 && b - b0 == blen - 1)
1605 if (a[0] == 0 || b[0] == 0)
1611 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1613 if (bcmp(a + 1, b + 1, l) != 0)
1620 if (a - a0 == alen && b - b0 == blen)
1627 * calculate the number of addresses to be returned in the node info reply.
1630 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1634 struct in6_ifaddr *ifa6;
1636 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1637 int addrs = 0, addrsofif, iffound = 0;
1638 int niflags = ni6->ni_flags;
1640 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1641 switch (ni6->ni_code) {
1642 case ICMP6_NI_SUBJ_IPV6:
1643 if (subj == NULL) /* must be impossible... */
1645 subj_ip6 = (struct sockaddr_in6 *)subj;
1649 * XXX: we only support IPv6 subject address for
1656 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1659 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1661 if (ifa->ifa_addr->sa_family != AF_INET6)
1663 ifa6 = (struct in6_ifaddr *)ifa;
1665 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1666 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1667 &ifa6->ia_addr.sin6_addr))
1671 * IPv4-mapped addresses can only be returned by a
1672 * Node Information proxy, since they represent
1673 * addresses of IPv4-only nodes, which perforce do
1674 * not implement this protocol.
1675 * [icmp-name-lookups-07, Section 5.4]
1676 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1677 * this function at this moment.
1680 /* What do we have to do about ::1? */
1681 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1682 case IPV6_ADDR_SCOPE_LINKLOCAL:
1683 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1686 case IPV6_ADDR_SCOPE_SITELOCAL:
1687 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1690 case IPV6_ADDR_SCOPE_GLOBAL:
1691 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1699 * check if anycast is okay.
1700 * XXX: just experimental. not in the spec.
1702 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1703 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1704 continue; /* we need only unicast addresses */
1705 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1706 (icmp6_nodeinfo & 4) == 0) {
1709 addrsofif++; /* count the address */
1723 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1724 struct ifnet *ifp0, int resid)
1726 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1727 struct in6_ifaddr *ifa6;
1729 struct ifnet *ifp_dep = NULL;
1730 int copied = 0, allow_deprecated = 0;
1731 u_char *cp = (u_char *)(nni6 + 1);
1732 int niflags = ni6->ni_flags;
1735 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1736 return(0); /* needless to copy */
1740 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1742 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1743 if (ifa->ifa_addr->sa_family != AF_INET6)
1745 ifa6 = (struct in6_ifaddr *)ifa;
1747 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1748 allow_deprecated == 0) {
1750 * prefererred address should be put before
1751 * deprecated addresses.
1754 /* record the interface for later search */
1755 if (ifp_dep == NULL)
1760 else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1761 allow_deprecated != 0)
1762 continue; /* we now collect deprecated addrs */
1764 /* What do we have to do about ::1? */
1765 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1766 case IPV6_ADDR_SCOPE_LINKLOCAL:
1767 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1770 case IPV6_ADDR_SCOPE_SITELOCAL:
1771 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1774 case IPV6_ADDR_SCOPE_GLOBAL:
1775 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1783 * check if anycast is okay.
1784 * XXX: just experimental. not in the spec.
1786 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1787 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1789 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1790 (icmp6_nodeinfo & 4) == 0) {
1794 /* now we can copy the address */
1795 if (resid < sizeof(struct in6_addr) +
1796 sizeof(u_int32_t)) {
1798 * We give up much more copy.
1799 * Set the truncate flag and return.
1802 NI_NODEADDR_FLAG_TRUNCATE;
1807 * Set the TTL of the address.
1808 * The TTL value should be one of the following
1809 * according to the specification:
1811 * 1. The remaining lifetime of a DHCP lease on the
1813 * 2. The remaining Valid Lifetime of a prefix from
1814 * which the address was derived through Stateless
1815 * Autoconfiguration.
1817 * Note that we currently do not support stateful
1818 * address configuration by DHCPv6, so the former
1819 * case can't happen.
1821 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1822 ltime = ND6_INFINITE_LIFETIME;
1824 if (ifa6->ia6_lifetime.ia6t_expire >
1826 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1831 bcopy(<ime, cp, sizeof(u_int32_t));
1832 cp += sizeof(u_int32_t);
1834 /* copy the address itself */
1835 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1836 sizeof(struct in6_addr));
1837 /* XXX: KAME link-local hack; remove ifindex */
1838 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1839 ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1840 cp += sizeof(struct in6_addr);
1842 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1843 copied += (sizeof(struct in6_addr) +
1846 if (ifp0) /* we need search only on the specified IF */
1850 if (allow_deprecated == 0 && ifp_dep != NULL) {
1852 allow_deprecated = 1;
1861 * XXX almost dup'ed code with rip6_input.
1864 icmp6_rip6_input(struct mbuf **mp, int off)
1866 struct mbuf *m = *mp;
1867 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1868 struct in6pcb *in6p;
1869 struct in6pcb *last = NULL;
1870 struct sockaddr_in6 rip6src;
1871 struct icmp6_hdr *icmp6;
1872 struct mbuf *opts = NULL;
1874 #ifndef PULLDOWN_TEST
1875 /* this is assumed to be safe. */
1876 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1878 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1879 if (icmp6 == NULL) {
1880 /* m is already reclaimed */
1881 return IPPROTO_DONE;
1885 bzero(&rip6src, sizeof(rip6src));
1886 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1887 rip6src.sin6_family = AF_INET6;
1888 /* KAME hack: recover scopeid */
1889 in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1891 LIST_FOREACH(in6p, &ripcbinfo.pcblisthead, inp_list)
1893 if (in6p->inp_flags & INP_PLACEMARKER)
1895 if ((in6p->inp_vflag & INP_IPV6) == 0)
1897 #ifdef HAVE_NRL_INPCB
1898 if (!(in6p->in6p_flags & INP_IPV6))
1901 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1903 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1904 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1906 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1907 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1909 if (in6p->in6p_icmp6filt
1910 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1911 in6p->in6p_icmp6filt))
1915 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1916 if (last->in6p_flags & IN6P_CONTROLOPTS)
1917 ip6_savecontrol(last, &opts, ip6, n);
1918 /* strip intermediate headers */
1920 if (sbappendaddr(&last->in6p_socket->so_rcv,
1921 (struct sockaddr *)&rip6src,
1923 /* should notify about lost packet */
1929 sorwakeup(last->in6p_socket);
1936 if (last->in6p_flags & IN6P_CONTROLOPTS)
1937 ip6_savecontrol(last, &opts, ip6, m);
1938 /* strip intermediate headers */
1940 if (sbappendaddr(&last->in6p_socket->so_rcv,
1941 (struct sockaddr *)&rip6src, m, opts) == 0) {
1946 sorwakeup(last->in6p_socket);
1949 ip6stat.ip6s_delivered--;
1951 return IPPROTO_DONE;
1955 * Reflect the ip6 packet back to the source.
1956 * OFF points to the icmp6 header, counted from the top of the mbuf.
1959 icmp6_reflect(struct mbuf *m, size_t off)
1961 struct ip6_hdr *ip6;
1962 struct icmp6_hdr *icmp6;
1963 struct in6_ifaddr *ia;
1964 struct in6_addr t, *src = 0;
1967 struct ifnet *outif = NULL;
1968 struct sockaddr_in6 sa6_src, sa6_dst;
1969 #ifdef COMPAT_RFC1885
1970 int mtu = IPV6_MMTU;
1971 struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
1974 /* too short to reflect */
1975 if (off < sizeof(struct ip6_hdr)) {
1977 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1978 (u_long)off, (u_long)sizeof(struct ip6_hdr),
1979 __FILE__, __LINE__));
1984 * If there are extra headers between IPv6 and ICMPv6, strip
1985 * off that header first.
1988 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
1989 panic("assumption failed in icmp6_reflect");
1991 if (off > sizeof(struct ip6_hdr)) {
1993 struct ip6_hdr nip6;
1995 l = off - sizeof(struct ip6_hdr);
1996 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
1998 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2000 if ((m = m_pullup(m, l)) == NULL)
2003 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2004 } else /* off == sizeof(struct ip6_hdr) */ {
2006 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2008 if ((m = m_pullup(m, l)) == NULL)
2012 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2013 ip6 = mtod(m, struct ip6_hdr *);
2014 ip6->ip6_nxt = IPPROTO_ICMPV6;
2015 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2016 type = icmp6->icmp6_type; /* keep type for statistics */
2017 code = icmp6->icmp6_code; /* ditto. */
2021 * ip6_input() drops a packet if its src is multicast.
2022 * So, the src is never multicast.
2024 ip6->ip6_dst = ip6->ip6_src;
2027 * XXX: make sure to embed scope zone information, using
2028 * already embedded IDs or the received interface (if any).
2029 * Note that rcvif may be NULL.
2030 * TODO: scoped routing case (XXX).
2032 bzero(&sa6_src, sizeof(sa6_src));
2033 sa6_src.sin6_family = AF_INET6;
2034 sa6_src.sin6_len = sizeof(sa6_src);
2035 sa6_src.sin6_addr = ip6->ip6_dst;
2036 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2037 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
2038 bzero(&sa6_dst, sizeof(sa6_dst));
2039 sa6_dst.sin6_family = AF_INET6;
2040 sa6_dst.sin6_len = sizeof(sa6_dst);
2041 sa6_dst.sin6_addr = t;
2042 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2043 in6_embedscope(&t, &sa6_dst, NULL, NULL);
2045 #ifdef COMPAT_RFC1885
2048 * RFC 1885 requires that echo reply should be truncated if it
2049 * does not fit in with (return) path MTU, but the description was
2050 * removed in the new spec.
2052 if (icmp6_reflect_rt.ro_rt == 0 ||
2053 ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
2054 if (icmp6_reflect_rt.ro_rt) {
2055 RTFREE(icmp6_reflect_rt.ro_rt);
2056 icmp6_reflect_rt.ro_rt = 0;
2058 bzero(sin6, sizeof(*sin6));
2059 sin6->sin6_family = PF_INET6;
2060 sin6->sin6_len = sizeof(struct sockaddr_in6);
2061 sin6->sin6_addr = ip6->ip6_dst;
2063 rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
2067 if (icmp6_reflect_rt.ro_rt == 0)
2070 if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
2071 && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
2072 mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
2074 if (mtu < m->m_pkthdr.len) {
2075 plen -= (m->m_pkthdr.len - mtu);
2076 m_adj(m, mtu - m->m_pkthdr.len);
2080 * If the incoming packet was addressed directly to us(i.e. unicast),
2081 * use dst as the src for the reply.
2082 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2083 * (for example) when we encounter an error while forwarding procedure
2084 * destined to a duplicated address of ours.
2086 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2087 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2088 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2092 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2094 * This is the case if the dst is our link-local address
2095 * and the sender is also ourselves.
2102 struct route_in6 ro;
2105 * This case matches to multicasts, our anycast, or unicasts
2106 * that we do not own. Select a source address based on the
2107 * source address of the erroneous packet.
2109 bzero(&ro, sizeof(ro));
2110 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e);
2112 RTFREE(ro.ro_rt); /* XXX: we could use this */
2115 "icmp6_reflect: source can't be determined: "
2116 "dst=%s, error=%d\n",
2117 ip6_sprintf(&sa6_src.sin6_addr), e));
2122 ip6->ip6_src = *src;
2125 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2126 ip6->ip6_vfc |= IPV6_VERSION;
2127 ip6->ip6_nxt = IPPROTO_ICMPV6;
2128 if (m->m_pkthdr.rcvif) {
2129 /* XXX: This may not be the outgoing interface */
2130 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2132 ip6->ip6_hlim = ip6_defhlim;
2134 icmp6->icmp6_cksum = 0;
2135 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2136 sizeof(struct ip6_hdr), plen);
2139 * XXX option handling
2142 m->m_flags &= ~(M_BCAST|M_MCAST);
2144 #ifdef COMPAT_RFC1885
2145 ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
2147 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2150 icmp6_ifoutstat_inc(outif, type, code);
2160 icmp6_fasttimo(void)
2167 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2168 struct in6_addr *tgt6)
2170 static char buf[1024];
2171 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2172 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2177 icmp6_redirect_input(struct mbuf *m, int off)
2179 struct ifnet *ifp = m->m_pkthdr.rcvif;
2180 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2181 struct nd_redirect *nd_rd;
2182 int icmp6len = ntohs(ip6->ip6_plen);
2183 char *lladdr = NULL;
2185 u_char *redirhdr = NULL;
2186 int redirhdrlen = 0;
2187 struct rtentry *rt = NULL;
2190 struct in6_addr src6 = ip6->ip6_src;
2191 struct in6_addr redtgt6;
2192 struct in6_addr reddst6;
2193 union nd_opts ndopts;
2198 /* XXX if we are router, we don't update route by icmp6 redirect */
2201 if (!icmp6_rediraccept)
2204 #ifndef PULLDOWN_TEST
2205 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2206 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2208 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2209 if (nd_rd == NULL) {
2210 icmp6stat.icp6s_tooshort++;
2214 redtgt6 = nd_rd->nd_rd_target;
2215 reddst6 = nd_rd->nd_rd_dst;
2217 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2218 redtgt6.s6_addr16[1] = htons(ifp->if_index);
2219 if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2220 reddst6.s6_addr16[1] = htons(ifp->if_index);
2223 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2225 "ICMP6 redirect sent from %s rejected; "
2226 "must be from linklocal\n", ip6_sprintf(&src6)));
2229 if (ip6->ip6_hlim != 255) {
2231 "ICMP6 redirect sent from %s rejected; "
2232 "hlim=%d (must be 255)\n",
2233 ip6_sprintf(&src6), ip6->ip6_hlim));
2237 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2238 struct sockaddr_in6 sin6;
2239 struct in6_addr *gw6;
2241 bzero(&sin6, sizeof(sin6));
2242 sin6.sin6_family = AF_INET6;
2243 sin6.sin6_len = sizeof(struct sockaddr_in6);
2244 bcopy(&reddst6, &sin6.sin6_addr, sizeof reddst6);
2245 rt = rtpurelookup((struct sockaddr *)&sin6);
2247 if (rt->rt_gateway == NULL ||
2248 rt->rt_gateway->sa_family != AF_INET6) {
2250 "ICMP6 redirect rejected; no route "
2251 "with inet6 gateway found for redirect dst: %s\n",
2252 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2257 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2258 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2260 "ICMP6 redirect rejected; "
2261 "not equal to gw-for-src=%s (must be same): "
2264 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2270 "ICMP6 redirect rejected; "
2271 "no route found for redirect dst: %s\n",
2272 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2278 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2280 "ICMP6 redirect rejected; "
2281 "redirect dst must be unicast: %s\n",
2282 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2286 is_router = is_onlink = 0;
2287 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2288 is_router = 1; /* router case */
2289 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2290 is_onlink = 1; /* on-link destination case */
2291 if (!is_router && !is_onlink) {
2293 "ICMP6 redirect rejected; "
2294 "neither router case nor onlink case: %s\n",
2295 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2298 /* validation passed */
2300 icmp6len -= sizeof(*nd_rd);
2301 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2302 if (nd6_options(&ndopts) < 0) {
2303 nd6log((LOG_INFO, "icmp6_redirect_input: "
2304 "invalid ND option, rejected: %s\n",
2305 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2306 /* nd6_options have incremented stats */
2310 if (ndopts.nd_opts_tgt_lladdr) {
2311 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2312 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2315 if (ndopts.nd_opts_rh) {
2316 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2317 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2320 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2322 "icmp6_redirect_input: lladdrlen mismatch for %s "
2323 "(if %d, icmp6 packet %d): %s\n",
2324 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2325 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2330 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2331 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2333 if (!is_onlink) { /* better router case. perform rtredirect. */
2334 /* perform rtredirect */
2335 struct sockaddr_in6 sdst;
2336 struct sockaddr_in6 sgw;
2337 struct sockaddr_in6 ssrc;
2339 bzero(&sdst, sizeof(sdst));
2340 bzero(&sgw, sizeof(sgw));
2341 bzero(&ssrc, sizeof(ssrc));
2342 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2343 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2344 sizeof(struct sockaddr_in6);
2345 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2346 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2347 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2348 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2349 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2350 (struct sockaddr *)&ssrc);
2352 /* finally update cached route in each socket via pfctlinput */
2354 struct sockaddr_in6 sdst;
2356 bzero(&sdst, sizeof(sdst));
2357 sdst.sin6_family = AF_INET6;
2358 sdst.sin6_len = sizeof(struct sockaddr_in6);
2359 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2360 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2362 key_sa_routechange((struct sockaddr *)&sdst);
2371 icmp6stat.icp6s_badredirect++;
2376 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2378 struct ifnet *ifp; /* my outgoing interface */
2379 struct in6_addr *ifp_ll6;
2380 struct in6_addr *router_ll6;
2381 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2382 struct mbuf *m = NULL; /* newly allocated one */
2383 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2384 struct nd_redirect *nd_rd;
2387 struct ifnet *outif = NULL;
2388 struct sockaddr_in6 src_sa;
2390 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2392 /* if we are not router, we don't send icmp6 redirect */
2393 if (!ip6_forwarding || ip6_accept_rtadv)
2397 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2402 * the source address must identify a neighbor, and
2403 * the destination address must not be a multicast address
2404 * [RFC 2461, sec 8.2]
2406 sip6 = mtod(m0, struct ip6_hdr *);
2407 bzero(&src_sa, sizeof(src_sa));
2408 src_sa.sin6_family = AF_INET6;
2409 src_sa.sin6_len = sizeof(src_sa);
2410 src_sa.sin6_addr = sip6->ip6_src;
2411 /* we don't currently use sin6_scope_id, but eventually use it */
2412 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2413 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2415 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2416 goto fail; /* what should we do here? */
2419 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2423 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2424 * we almost always ask for an mbuf cluster for simplicity.
2425 * (MHLEN < IPV6_MMTU is almost always true)
2427 #if IPV6_MMTU >= MCLBYTES
2428 # error assumption failed about IPV6_MMTU and MCLBYTES
2430 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
2431 if (m && IPV6_MMTU >= MHLEN)
2432 MCLGET(m, MB_DONTWAIT);
2435 m->m_pkthdr.rcvif = NULL;
2437 maxlen = M_TRAILINGSPACE(m);
2438 maxlen = min(IPV6_MMTU, maxlen);
2439 /* just for safety */
2440 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2441 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2446 /* get ip6 linklocal address for ifp(my outgoing interface). */
2447 struct in6_ifaddr *ia;
2448 if ((ia = in6ifa_ifpforlinklocal(ifp,
2450 IN6_IFF_ANYCAST)) == NULL)
2452 ifp_ll6 = &ia->ia_addr.sin6_addr;
2455 /* get ip6 linklocal address for the router. */
2456 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2457 struct sockaddr_in6 *sin6;
2458 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2459 router_ll6 = &sin6->sin6_addr;
2460 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2461 router_ll6 = (struct in6_addr *)NULL;
2463 router_ll6 = (struct in6_addr *)NULL;
2466 ip6 = mtod(m, struct ip6_hdr *);
2468 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2469 ip6->ip6_vfc |= IPV6_VERSION;
2470 /* ip6->ip6_plen will be set later */
2471 ip6->ip6_nxt = IPPROTO_ICMPV6;
2472 ip6->ip6_hlim = 255;
2473 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2474 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2475 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2478 nd_rd = (struct nd_redirect *)(ip6 + 1);
2479 nd_rd->nd_rd_type = ND_REDIRECT;
2480 nd_rd->nd_rd_code = 0;
2481 nd_rd->nd_rd_reserved = 0;
2482 if (rt->rt_flags & RTF_GATEWAY) {
2484 * nd_rd->nd_rd_target must be a link-local address in
2485 * better router cases.
2489 bcopy(router_ll6, &nd_rd->nd_rd_target,
2490 sizeof(nd_rd->nd_rd_target));
2491 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2492 sizeof(nd_rd->nd_rd_dst));
2494 /* make sure redtgt == reddst */
2495 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2496 sizeof(nd_rd->nd_rd_target));
2497 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2498 sizeof(nd_rd->nd_rd_dst));
2501 p = (u_char *)(nd_rd + 1);
2507 /* target lladdr option */
2508 struct rtentry *rt_router = NULL;
2510 struct sockaddr_dl *sdl;
2511 struct nd_opt_hdr *nd_opt;
2514 rt_router = nd6_lookup(router_ll6, 0, ifp);
2517 len = sizeof(*nd_opt) + ifp->if_addrlen;
2518 len = (len + 7) & ~7; /* round by 8 */
2520 if (len + (p - (u_char *)ip6) > maxlen)
2522 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2523 (rt_router->rt_flags & RTF_LLINFO) &&
2524 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2525 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2527 nd_opt = (struct nd_opt_hdr *)p;
2528 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2529 nd_opt->nd_opt_len = len >> 3;
2530 lladdr = (char *)(nd_opt + 1);
2531 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2537 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2539 /* just to be safe */
2540 #ifdef M_DECRYPTED /*not openbsd*/
2541 if (m0->m_flags & M_DECRYPTED)
2544 if (p - (u_char *)ip6 > maxlen)
2548 /* redirected header option */
2550 struct nd_opt_rd_hdr *nd_opt_rh;
2553 * compute the maximum size for icmp6 redirect header option.
2554 * XXX room for auth header?
2556 len = maxlen - (p - (u_char *)ip6);
2559 /* This is just for simplicity. */
2560 if (m0->m_pkthdr.len != m0->m_len) {
2562 m_freem(m0->m_next);
2565 m0->m_pkthdr.len = m0->m_len;
2569 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2570 * about padding/truncate rule for the original IP packet.
2571 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2572 * - "attach as much as possible" is the goal
2573 * - pad if not aligned (original size can be guessed by original
2575 * Following code adds the padding if it is simple enough,
2576 * and truncates if not.
2578 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2579 panic("assumption failed in %s:%d", __FILE__, __LINE__);
2581 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2582 /* not enough room, truncate */
2583 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2585 /* enough room, pad or truncate */
2588 extra = m0->m_pkthdr.len % 8;
2590 /* pad if easy enough, truncate if not */
2591 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2593 m0->m_len += (8 - extra);
2594 m0->m_pkthdr.len += (8 - extra);
2597 m0->m_pkthdr.len -= extra;
2601 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2602 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2605 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2606 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2607 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2608 nd_opt_rh->nd_opt_rh_len = len >> 3;
2609 p += sizeof(*nd_opt_rh);
2610 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2612 /* connect m0 to m */
2614 m->m_pkthdr.len = m->m_len + m0->m_len;
2618 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2619 sip6->ip6_src.s6_addr16[1] = 0;
2620 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2621 sip6->ip6_dst.s6_addr16[1] = 0;
2623 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2624 ip6->ip6_src.s6_addr16[1] = 0;
2625 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2626 ip6->ip6_dst.s6_addr16[1] = 0;
2628 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2629 nd_rd->nd_rd_target.s6_addr16[1] = 0;
2630 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2631 nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2633 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2635 nd_rd->nd_rd_cksum = 0;
2637 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2639 /* send the packet to outside... */
2640 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2642 icmp6_ifstat_inc(outif, ifs6_out_msg);
2643 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2645 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2656 #ifdef HAVE_NRL_INPCB
2657 #define in6pcb inpcb
2658 #define in6p_icmp6filt inp_icmp6filt
2661 * ICMPv6 socket option processing.
2664 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2668 struct inpcb *inp = so->so_pcb;
2669 int level, op, optname;
2672 level = sopt->sopt_level;
2673 op = sopt->sopt_dir;
2674 optname = sopt->sopt_name;
2675 optlen = sopt->sopt_valsize;
2677 level = op = optname = optlen = 0;
2679 if (level != IPPROTO_ICMPV6) {
2688 struct icmp6_filter *p;
2690 if (optlen != sizeof(*p)) {
2694 if (inp->in6p_icmp6filt == NULL) {
2698 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2704 error = ENOPROTOOPT;
2713 if (inp->in6p_icmp6filt == NULL) {
2717 error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2718 sizeof(struct icmp6_filter));
2723 error = ENOPROTOOPT;
2731 #ifdef HAVE_NRL_INPCB
2733 #undef in6p_icmp6filt
2736 #ifndef HAVE_PPSRATECHECK
2738 #define timersub(tvp, uvp, vvp) \
2740 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2741 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2742 if ((vvp)->tv_usec < 0) { \
2744 (vvp)->tv_usec += 1000000; \
2750 * ppsratecheck(): packets (or events) per second limitation.
2753 ppsratecheck(struct timeval *lasttime, int *curpps,
2754 int maxpps) /* maximum pps allowed */
2756 struct timeval tv, delta;
2761 timersub(&tv, lasttime, &delta);
2764 * Check for 0,0 so that the message will be seen at least once.
2765 * If more than one second has passed since the last update of
2766 * lasttime, reset the counter.
2768 * We do increment *curpps even in *curpps < maxpps case, as some may
2769 * try to use *curpps for stat purposes as well.
2771 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
2772 delta.tv_sec >= 1) {
2776 } else if (maxpps < 0)
2778 else if (*curpps < maxpps)
2783 #if 1 /* DIAGNOSTIC? */
2784 /* be careful about wrap-around */
2785 if (*curpps + 1 > *curpps)
2786 *curpps = *curpps + 1;
2789 * assume that there's not too many calls to this function.
2790 * not sure if the assumption holds, as it depends on *caller's*
2791 * behavior, not the behavior of this function.
2792 * IMHO it is wrong to make assumption on the caller's behavior,
2793 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
2795 *curpps = *curpps + 1;
2803 * Perform rate limit check.
2804 * Returns 0 if it is okay to send the icmp6 packet.
2805 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2808 * XXX per-destination/type check necessary?
2811 icmp6_ratelimit(const struct in6_addr *dst, /* not used at this moment */
2812 const int type, /* not used at this moment */
2813 const int code) /* not used at this moment */
2817 ret = 0; /* okay to send */
2820 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2822 /* The packet is subject to rate limit */