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.31 2008/10/27 02:56:30 sephe 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>
80 #include <sys/socketops.h>
82 #include <sys/kernel.h>
83 #include <sys/syslog.h>
84 #include <sys/domain.h>
86 #include <sys/thread2.h>
87 #include <sys/msgport2.h>
90 #include <net/route.h>
91 #include <net/if_dl.h>
92 #include <net/if_types.h>
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet/icmp6.h>
99 #include <netinet6/mld6_var.h>
100 #include <netinet/in_pcb.h>
101 #include <netinet6/in6_pcb.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/in6_ifattach.h>
104 #include <netinet6/ip6protosw.h>
107 #include <netinet6/ipsec.h>
108 #include <netproto/key/key.h>
112 #include <netproto/ipsec/ipsec.h>
113 #include <netproto/ipsec/key.h>
117 #include <net/net_osdep.h>
119 #ifdef HAVE_NRL_INPCB
122 #define in6p_laddr inp_laddr6
123 #define in6p_faddr inp_faddr6
124 #define in6p_icmp6filt inp_icmp6filt
125 #define in6p_route inp_route
126 #define in6p_socket inp_socket
127 #define in6p_flags inp_flags
128 #define in6p_moptions inp_moptions6
129 #define in6p_outputopts inp_outputopts6
130 #define in6p_ip6 inp_ipv6
131 #define in6p_flowinfo inp_flowinfo
132 #define in6p_sp inp_sp
133 #define in6p_next inp_next
134 #define in6p_prev inp_prev
136 #define in6_pcbdetach in_pcbdetach
137 #define in6_rtchange in_rtchange
140 * for KAME src sync over BSD*'s. XXX: FreeBSD (>=3) are VERY different from
143 #define in6p_ip6_nxt inp_ipv6.ip6_nxt
147 * ppratecheck() is available, so define it here.
149 #define HAVE_PPSRATECHECK
151 extern struct domain inet6domain;
152 extern struct protosw inet6sw[];
153 extern u_char ip6_protox[];
155 struct icmp6stat icmp6stat;
157 extern struct inpcbinfo ripcbinfo;
158 extern int icmp6errppslim;
159 static int icmp6errpps_count = 0;
160 static struct timeval icmp6errppslim_last;
161 extern int icmp6_nodeinfo;
163 static void icmp6_errcount (struct icmp6errstat *, int, int);
164 static int icmp6_rip6_input (struct mbuf **, int);
165 static int icmp6_ratelimit (const struct in6_addr *, const int, const int);
166 static const char *icmp6_redirect_diag (struct in6_addr *,
167 struct in6_addr *, struct in6_addr *);
168 #ifndef HAVE_PPSRATECHECK
169 static int ppsratecheck (struct timeval *, int *, int);
171 static struct mbuf *ni6_input (struct mbuf *, int);
172 static struct mbuf *ni6_nametodns (const char *, int, int);
173 static int ni6_dnsmatch (const char *, int, const char *, int);
174 static int ni6_addrs (struct icmp6_nodeinfo *, struct mbuf *,
175 struct ifnet **, char *);
176 static int ni6_store_addrs (struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
177 struct ifnet *, int);
178 static int icmp6_notify_error (struct mbuf *, int, int, int);
180 #ifdef COMPAT_RFC1885
181 static struct route_in6 icmp6_reflect_rt;
192 icmp6_errcount(struct icmp6errstat *stat, int type, int code)
195 case ICMP6_DST_UNREACH:
197 case ICMP6_DST_UNREACH_NOROUTE:
198 stat->icp6errs_dst_unreach_noroute++;
200 case ICMP6_DST_UNREACH_ADMIN:
201 stat->icp6errs_dst_unreach_admin++;
203 case ICMP6_DST_UNREACH_BEYONDSCOPE:
204 stat->icp6errs_dst_unreach_beyondscope++;
206 case ICMP6_DST_UNREACH_ADDR:
207 stat->icp6errs_dst_unreach_addr++;
209 case ICMP6_DST_UNREACH_NOPORT:
210 stat->icp6errs_dst_unreach_noport++;
214 case ICMP6_PACKET_TOO_BIG:
215 stat->icp6errs_packet_too_big++;
217 case ICMP6_TIME_EXCEEDED:
219 case ICMP6_TIME_EXCEED_TRANSIT:
220 stat->icp6errs_time_exceed_transit++;
222 case ICMP6_TIME_EXCEED_REASSEMBLY:
223 stat->icp6errs_time_exceed_reassembly++;
227 case ICMP6_PARAM_PROB:
229 case ICMP6_PARAMPROB_HEADER:
230 stat->icp6errs_paramprob_header++;
232 case ICMP6_PARAMPROB_NEXTHEADER:
233 stat->icp6errs_paramprob_nextheader++;
235 case ICMP6_PARAMPROB_OPTION:
236 stat->icp6errs_paramprob_option++;
241 stat->icp6errs_redirect++;
244 stat->icp6errs_unknown++;
248 * Generate an error packet of type error in response to bad IP6 packet.
251 icmp6_error(struct mbuf *m, int type, int code, int param)
253 struct ip6_hdr *oip6, *nip6;
254 struct icmp6_hdr *icmp6;
259 icmp6stat.icp6s_error++;
261 /* count per-type-code statistics */
262 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
264 #ifdef M_DECRYPTED /*not openbsd*/
265 if (m->m_flags & M_DECRYPTED) {
266 icmp6stat.icp6s_canterror++;
271 #ifndef PULLDOWN_TEST
272 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
274 if (m->m_len < sizeof(struct ip6_hdr)) {
275 m = m_pullup(m, sizeof(struct ip6_hdr));
280 oip6 = mtod(m, struct ip6_hdr *);
283 * Multicast destination check. For unrecognized option errors,
284 * this check has already done in ip6_unknown_opt(), so we can
285 * check only for other errors.
287 if ((m->m_flags & (M_BCAST|M_MCAST) ||
288 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
289 (type != ICMP6_PACKET_TOO_BIG &&
290 (type != ICMP6_PARAM_PROB ||
291 code != ICMP6_PARAMPROB_OPTION)))
294 /* Source address check. XXX: the case of anycast source? */
295 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
296 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
300 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
304 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
305 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
306 struct icmp6_hdr *icp;
308 #ifndef PULLDOWN_TEST
309 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
310 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
312 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
315 icmp6stat.icp6s_tooshort++;
319 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
320 icp->icmp6_type == ND_REDIRECT) {
323 * Special case: for redirect (which is
324 * informational) we must not send icmp6 error.
326 icmp6stat.icp6s_canterror++;
329 /* ICMPv6 informational - send the error */
332 /* non-ICMPv6 - send the error */
335 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
337 /* Finally, do rate limitation check. */
338 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
339 icmp6stat.icp6s_toofreq++;
344 * OK, ICMP6 can be generated.
347 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
348 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
350 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
351 M_PREPEND(m, preplen, MB_DONTWAIT);
352 if (m && m->m_len < preplen)
353 m = m_pullup(m, preplen);
355 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
359 nip6 = mtod(m, struct ip6_hdr *);
360 nip6->ip6_src = oip6->ip6_src;
361 nip6->ip6_dst = oip6->ip6_dst;
363 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
364 oip6->ip6_src.s6_addr16[1] = 0;
365 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
366 oip6->ip6_dst.s6_addr16[1] = 0;
368 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
369 icmp6->icmp6_type = type;
370 icmp6->icmp6_code = code;
371 icmp6->icmp6_pptr = htonl((u_int32_t)param);
374 * icmp6_reflect() is designed to be in the input path.
375 * icmp6_error() can be called from both input and outut path,
376 * and if we are in output path rcvif could contain bogus value.
377 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
378 * information in ip header (nip6).
380 m->m_pkthdr.rcvif = NULL;
382 icmp6stat.icp6s_outhist[type]++;
383 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
389 * If we can't tell wheter or not we can generate ICMP6, free it.
395 * Process a received ICMP6 message.
398 icmp6_input(struct mbuf **mp, int *offp, int proto)
400 struct mbuf *m = *mp, *n;
401 struct ip6_hdr *ip6, *nip6;
402 struct icmp6_hdr *icmp6, *nicmp6;
404 int icmp6len = m->m_pkthdr.len - *offp;
407 #ifndef PULLDOWN_TEST
408 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
409 /* m might change if M_LOOP. So, call mtod after this */
413 * Locate icmp6 structure in mbuf, and check
414 * that not corrupted and of at least minimum length
417 ip6 = mtod(m, struct ip6_hdr *);
418 if (icmp6len < sizeof(struct icmp6_hdr)) {
419 icmp6stat.icp6s_tooshort++;
424 * calculate the checksum
426 #ifndef PULLDOWN_TEST
427 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
429 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
431 icmp6stat.icp6s_tooshort++;
435 code = icmp6->icmp6_code;
437 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
439 "ICMP6 checksum error(%d|%x) %s\n",
440 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
441 icmp6stat.icp6s_checksum++;
445 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
447 * Deliver very specific ICMP6 type only.
448 * This is important to deilver TOOBIG. Otherwise PMTUD
451 switch (icmp6->icmp6_type) {
452 case ICMP6_DST_UNREACH:
453 case ICMP6_PACKET_TOO_BIG:
454 case ICMP6_TIME_EXCEEDED:
461 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
462 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
463 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
464 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
466 switch (icmp6->icmp6_type) {
467 case ICMP6_DST_UNREACH:
468 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
470 case ICMP6_DST_UNREACH_NOROUTE:
471 code = PRC_UNREACH_NET;
473 case ICMP6_DST_UNREACH_ADMIN:
474 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
475 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
477 case ICMP6_DST_UNREACH_ADDR:
480 #ifdef COMPAT_RFC1885
481 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
482 code = PRC_UNREACH_SRCFAIL;
485 case ICMP6_DST_UNREACH_BEYONDSCOPE:
486 /* I mean "source address was incorrect." */
487 code = PRC_PARAMPROB;
490 case ICMP6_DST_UNREACH_NOPORT:
491 code = PRC_UNREACH_PORT;
499 case ICMP6_PACKET_TOO_BIG:
500 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
507 * Updating the path MTU will be done after examining
508 * intermediate extension headers.
513 case ICMP6_TIME_EXCEEDED:
514 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
516 case ICMP6_TIME_EXCEED_TRANSIT:
517 case ICMP6_TIME_EXCEED_REASSEMBLY:
518 code += PRC_TIMXCEED_INTRANS;
526 case ICMP6_PARAM_PROB:
527 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
529 case ICMP6_PARAMPROB_NEXTHEADER:
530 code = PRC_UNREACH_PROTOCOL;
532 case ICMP6_PARAMPROB_HEADER:
533 case ICMP6_PARAMPROB_OPTION:
534 code = PRC_PARAMPROB;
542 case ICMP6_ECHO_REQUEST:
543 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
546 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
550 if ((n->m_flags & M_EXT) ||
551 n->m_len < off + sizeof(struct icmp6_hdr)) {
553 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
557 * Prepare an internal mbuf. m_pullup() doesn't
558 * always copy the length we specified.
560 if (maxlen >= MCLBYTES) {
565 n = m_getb(maxlen, MB_DONTWAIT, n0->m_type, M_PKTHDR);
571 n0len = n0->m_pkthdr.len; /* save for use below */
572 M_MOVE_PKTHDR(n, n0);
574 * Copy IPv6 and ICMPv6 only.
576 nip6 = mtod(n, struct ip6_hdr *);
577 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
578 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
579 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
580 noff = sizeof(struct ip6_hdr);
581 /* new mbuf contains only ipv6+icmpv6 headers */
582 n->m_len = noff + sizeof(struct icmp6_hdr);
584 * Adjust mbuf. ip6_plen will be adjusted in
587 m_adj(n0, off + sizeof(struct icmp6_hdr));
588 /* recalculate complete packet size */
589 n->m_pkthdr.len = n0len + (noff - off);
592 nip6 = mtod(n, struct ip6_hdr *);
593 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
596 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
597 nicmp6->icmp6_code = 0;
599 icmp6stat.icp6s_reflect++;
600 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
601 icmp6_reflect(n, noff);
605 case ICMP6_ECHO_REPLY:
606 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
611 case MLD_LISTENER_QUERY:
612 case MLD_LISTENER_REPORT:
613 if (icmp6len < sizeof(struct mld_hdr))
615 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
616 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
618 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
619 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
629 case MLD_LISTENER_DONE:
630 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
631 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
633 break; /* nothing to be done in kernel */
635 case MLD_MTRACE_RESP:
637 /* XXX: these two are experimental. not officially defind. */
638 /* XXX: per-interface statistics? */
639 break; /* just pass it to applications */
641 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
643 enum { WRU, FQDN } mode;
648 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
650 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
655 #define hostnamelen strlen(hostname)
657 #ifndef PULLDOWN_TEST
658 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
661 n = m_copy(m, 0, M_COPYALL);
663 n = ni6_input(n, off);
664 /* XXX meaningless if n == NULL */
665 noff = sizeof(struct ip6_hdr);
670 if ((icmp6_nodeinfo & 5) != 5)
675 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
676 if (maxlen >= MCLBYTES) {
680 n = m_getb(maxlen, MB_DONTWAIT, m->m_type, M_PKTHDR);
685 if (!m_dup_pkthdr(n, m, MB_DONTWAIT)) {
687 * Previous code did a blind M_COPY_PKTHDR
688 * and said "just for rcvif". If true, then
689 * we could tolerate the dup failing (due to
690 * the deep copy of the tag chain). For now
691 * be conservative and just fail.
697 maxhlen = M_TRAILINGSPACE(n) - maxlen;
698 if (maxhlen > hostnamelen)
699 maxhlen = hostnamelen;
701 * Copy IPv6 and ICMPv6 only.
703 nip6 = mtod(n, struct ip6_hdr *);
704 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
705 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
706 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
707 p = (u_char *)(nicmp6 + 1);
709 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
710 noff = sizeof(struct ip6_hdr);
711 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
712 sizeof(struct icmp6_hdr) + 4 + maxhlen;
713 nicmp6->icmp6_type = ICMP6_WRUREPLY;
714 nicmp6->icmp6_code = 0;
718 icmp6stat.icp6s_reflect++;
719 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
720 icmp6_reflect(n, noff);
730 case ND_ROUTER_SOLICIT:
731 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
734 if (icmp6len < sizeof(struct nd_router_solicit))
736 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
738 nd6_rs_input(m, off, icmp6len);
742 nd6_rs_input(n, off, icmp6len);
746 case ND_ROUTER_ADVERT:
747 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
750 if (icmp6len < sizeof(struct nd_router_advert))
752 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
754 nd6_ra_input(m, off, icmp6len);
758 nd6_ra_input(n, off, icmp6len);
762 case ND_NEIGHBOR_SOLICIT:
763 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
766 if (icmp6len < sizeof(struct nd_neighbor_solicit))
768 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
770 nd6_ns_input(m, off, icmp6len);
774 nd6_ns_input(n, off, icmp6len);
778 case ND_NEIGHBOR_ADVERT:
779 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
782 if (icmp6len < sizeof(struct nd_neighbor_advert))
784 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
786 nd6_na_input(m, off, icmp6len);
790 nd6_na_input(n, off, icmp6len);
795 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
798 if (icmp6len < sizeof(struct nd_redirect))
800 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
802 icmp6_redirect_input(m, off);
806 icmp6_redirect_input(n, off);
810 case ICMP6_ROUTER_RENUMBERING:
811 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
812 code != ICMP6_ROUTER_RENUMBERING_RESULT)
814 if (icmp6len < sizeof(struct icmp6_router_renum))
820 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
821 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
822 ip6_sprintf(&ip6->ip6_dst),
823 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
824 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
825 /* ICMPv6 error: MUST deliver it by spec... */
829 /* ICMPv6 informational: MUST not deliver */
833 if (icmp6_notify_error(m, off, icmp6len, code)) {
834 /* In this case, m should've been freed. */
835 return (IPPROTO_DONE);
840 icmp6stat.icp6s_badcode++;
844 icmp6stat.icp6s_badlen++;
848 /* deliver the packet to appropriate sockets */
849 icmp6_rip6_input(&m, *offp);
859 icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
861 struct icmp6_hdr *icmp6;
862 struct ip6_hdr *eip6;
864 struct sockaddr_in6 icmp6src, icmp6dst;
866 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
867 icmp6stat.icp6s_tooshort++;
870 #ifndef PULLDOWN_TEST
871 IP6_EXTHDR_CHECK(m, off,
872 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
874 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
876 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
877 sizeof(*icmp6) + sizeof(struct ip6_hdr));
879 icmp6stat.icp6s_tooshort++;
883 eip6 = (struct ip6_hdr *)(icmp6 + 1);
885 /* Detect the upper level protocol */
887 u_int8_t nxt = eip6->ip6_nxt;
888 int eoff = off + sizeof(struct icmp6_hdr) +
889 sizeof(struct ip6_hdr);
890 struct ip6ctlparam ip6cp;
891 struct in6_addr *finaldst = NULL;
892 int icmp6type = icmp6->icmp6_type;
894 struct ip6_rthdr *rth;
897 while (1) { /* XXX: should avoid infinite loop explicitly? */
901 case IPPROTO_HOPOPTS:
902 case IPPROTO_DSTOPTS:
904 #ifndef PULLDOWN_TEST
905 IP6_EXTHDR_CHECK(m, 0, eoff +
906 sizeof(struct ip6_ext),
908 eh = (struct ip6_ext *)(mtod(m, caddr_t)
911 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
914 icmp6stat.icp6s_tooshort++;
919 if (nxt == IPPROTO_AH)
920 eoff += (eh->ip6e_len + 2) << 2;
922 eoff += (eh->ip6e_len + 1) << 3;
925 case IPPROTO_ROUTING:
927 * When the erroneous packet contains a
928 * routing header, we should examine the
929 * header to determine the final destination.
930 * Otherwise, we can't properly update
931 * information that depends on the final
932 * destination (e.g. path MTU).
934 #ifndef PULLDOWN_TEST
935 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
937 rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
940 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
943 icmp6stat.icp6s_tooshort++;
947 rthlen = (rth->ip6r_len + 1) << 3;
951 case IPPROTO_FRAGMENT:
952 #ifndef PULLDOWN_TEST
953 IP6_EXTHDR_CHECK(m, 0, eoff +
954 sizeof(struct ip6_frag),
956 fh = (struct ip6_frag *)(mtod(m, caddr_t)
959 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
962 icmp6stat.icp6s_tooshort++;
967 * Data after a fragment header is meaningless
968 * unless it is the first fragment, but
969 * we'll go to the notify label for path MTU
972 if (fh->ip6f_offlg & IP6F_OFF_MASK)
975 eoff += sizeof(struct ip6_frag);
980 * This case includes ESP and the No Next
981 * Header. In such cases going to the notify
982 * label does not have any meaning
983 * (i.e. pr_ctlinput will be NULL), but we go
984 * anyway since we might have to update
985 * path MTU information.
991 #ifndef PULLDOWN_TEST
992 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
994 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
995 sizeof(*icmp6) + sizeof(struct ip6_hdr));
997 icmp6stat.icp6s_tooshort++;
1002 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1003 bzero(&icmp6dst, sizeof(icmp6dst));
1004 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1005 icmp6dst.sin6_family = AF_INET6;
1006 if (finaldst == NULL)
1007 icmp6dst.sin6_addr = eip6->ip6_dst;
1009 icmp6dst.sin6_addr = *finaldst;
1010 icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1011 &icmp6dst.sin6_addr);
1012 if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
1014 /* should be impossbile */
1016 "icmp6_notify_error: in6_embedscope failed\n"));
1021 * retrieve parameters from the inner IPv6 header, and convert
1022 * them into sockaddr structures.
1024 bzero(&icmp6src, sizeof(icmp6src));
1025 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1026 icmp6src.sin6_family = AF_INET6;
1027 icmp6src.sin6_addr = eip6->ip6_src;
1028 icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1029 &icmp6src.sin6_addr);
1030 if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
1032 /* should be impossbile */
1034 "icmp6_notify_error: in6_embedscope failed\n"));
1037 icmp6src.sin6_flowinfo =
1038 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1040 if (finaldst == NULL)
1041 finaldst = &eip6->ip6_dst;
1043 ip6cp.ip6c_icmp6 = icmp6;
1044 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1045 ip6cp.ip6c_off = eoff;
1046 ip6cp.ip6c_finaldst = finaldst;
1047 ip6cp.ip6c_src = &icmp6src;
1048 ip6cp.ip6c_nxt = nxt;
1050 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1051 notifymtu = ntohl(icmp6->icmp6_mtu);
1052 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1053 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1057 (struct protosw *)&inet6sw[ip6_protox[nxt]],
1058 code, (struct sockaddr *)&icmp6dst, &ip6cp);
1068 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1070 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1071 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1072 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1073 u_int mtu = ntohl(icmp6->icmp6_mtu);
1074 struct rtentry *rt = NULL;
1075 struct sockaddr_in6 sin6;
1080 bzero(&sin6, sizeof(sin6));
1081 sin6.sin6_family = PF_INET6;
1082 sin6.sin6_len = sizeof(struct sockaddr_in6);
1083 sin6.sin6_addr = *dst;
1084 /* XXX normally, this won't happen */
1085 if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1086 sin6.sin6_addr.s6_addr16[1] =
1087 htons(m->m_pkthdr.rcvif->if_index);
1089 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1090 rt = rtpurelookup((struct sockaddr *)&sin6);
1091 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
1092 !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1093 if (mtu < IPV6_MMTU) { /* XXX */
1094 rt->rt_rmx.rmx_locks |= RTV_MTU;
1095 } else if (mtu < rt->rt_ifp->if_mtu &&
1096 rt->rt_rmx.rmx_mtu > mtu) {
1097 icmp6stat.icp6s_pmtuchg++;
1098 rt->rt_rmx.rmx_mtu = mtu;
1106 * Process a Node Information Query packet, based on
1107 * draft-ietf-ipngwg-icmp-name-lookups-07.
1109 * Spec incompatibilities:
1110 * - IPv6 Subject address handling
1111 * - IPv4 Subject address handling support missing
1112 * - Proxy reply (answer even if it's not for me)
1113 * - joins NI group address at in6_ifattach() time only, does not cope
1114 * with hostname changes by sethostname(3)
1116 #define hostnamelen strlen(hostname)
1117 static struct mbuf *
1118 ni6_input(struct mbuf *m, int off)
1120 struct icmp6_nodeinfo *ni6, *nni6;
1121 struct mbuf *n = NULL;
1124 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1125 struct ni_reply_fqdn *fqdn;
1126 int addrs; /* for NI_QTYPE_NODEADDR */
1127 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1128 struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1129 struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
1130 struct ip6_hdr *ip6;
1131 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1133 struct in6_ifaddr *ia6 = NULL;
1135 ip6 = mtod(m, struct ip6_hdr *);
1136 #ifndef PULLDOWN_TEST
1137 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1139 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1141 /* m is already reclaimed */
1147 * Validate IPv6 destination address.
1149 * The Responder must discard the Query without further processing
1150 * unless it is one of the Responder's unicast or anycast addresses, or
1151 * a link-local scope multicast address which the Responder has joined.
1152 * [icmp-name-lookups-07, Section 4.]
1154 bzero(&sin6, sizeof(sin6));
1155 sin6.sin6_family = AF_INET6;
1156 sin6.sin6_len = sizeof(struct sockaddr_in6);
1157 bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1159 if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
1160 /* unicast/anycast, fine */
1161 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1162 (icmp6_nodeinfo & 4) == 0) {
1163 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1164 "a temporary address in %s:%d",
1165 __FILE__, __LINE__));
1168 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1169 ; /* link-local multicast, fine */
1173 /* validate query Subject field. */
1174 qtype = ntohs(ni6->ni_qtype);
1175 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1178 case NI_QTYPE_SUPTYPES:
1180 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1184 case NI_QTYPE_NODEADDR:
1185 switch (ni6->ni_code) {
1186 case ICMP6_NI_SUBJ_IPV6:
1187 #if ICMP6_NI_SUBJ_IPV6 != 0
1191 * backward compatibility - try to accept 03 draft
1192 * format, where no Subject is present.
1194 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1199 #if ICMP6_NI_SUBJ_IPV6 != 0
1200 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1204 if (subjlen != sizeof(sin6.sin6_addr))
1208 * Validate Subject address.
1210 * Not sure what exactly "address belongs to the node"
1211 * means in the spec, is it just unicast, or what?
1213 * At this moment we consider Subject address as
1214 * "belong to the node" if the Subject address equals
1215 * to the IPv6 destination address; validation for
1216 * IPv6 destination address should have done enough
1219 * We do not do proxy at this moment.
1221 /* m_pulldown instead of copy? */
1222 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1223 subjlen, (caddr_t)&sin6.sin6_addr);
1224 sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1226 in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL);
1227 bzero(&sin6_d, sizeof(sin6_d));
1228 sin6_d.sin6_family = AF_INET6; /* not used, actually */
1229 sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
1230 sin6_d.sin6_addr = ip6->ip6_dst;
1231 sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1233 in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL);
1234 subj = (char *)&sin6;
1235 if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
1239 * XXX if we are to allow other cases, we should really
1240 * be careful about scope here.
1241 * basically, we should disallow queries toward IPv6
1242 * destination X with subject Y, if scope(X) > scope(Y).
1243 * if we allow scope(X) > scope(Y), it will result in
1244 * information leakage across scope boundary.
1248 case ICMP6_NI_SUBJ_FQDN:
1250 * Validate Subject name with gethostname(3).
1252 * The behavior may need some debate, since:
1253 * - we are not sure if the node has FQDN as
1254 * hostname (returned by gethostname(3)).
1255 * - the code does wildcard match for truncated names.
1256 * however, we are not sure if we want to perform
1257 * wildcard match, if gethostname(3) side has
1258 * truncated hostname.
1260 n = ni6_nametodns(hostname, hostnamelen, 0);
1261 if (!n || n->m_next || n->m_len == 0)
1263 IP6_EXTHDR_GET(subj, char *, m,
1264 off + sizeof(struct icmp6_nodeinfo), subjlen);
1267 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1275 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1282 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1285 if ((icmp6_nodeinfo & 1) == 0)
1288 case NI_QTYPE_NODEADDR:
1289 if ((icmp6_nodeinfo & 2) == 0)
1294 /* guess reply length */
1297 break; /* no reply data */
1298 case NI_QTYPE_SUPTYPES:
1299 replylen += sizeof(u_int32_t);
1302 /* XXX will append an mbuf */
1303 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1305 case NI_QTYPE_NODEADDR:
1306 addrs = ni6_addrs(ni6, m, &ifp, subj);
1307 if ((replylen += addrs * (sizeof(struct in6_addr) +
1308 sizeof(u_int32_t))) > MCLBYTES)
1309 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1313 * XXX: We must return a reply with the ICMP6 code
1314 * `unknown Qtype' in this case. However we regard the case
1315 * as an FQDN query for backward compatibility.
1316 * Older versions set a random value to this field,
1317 * so it rarely varies in the defined qtypes.
1318 * But the mechanism is not reliable...
1319 * maybe we should obsolete older versions.
1321 qtype = NI_QTYPE_FQDN;
1322 /* XXX will append an mbuf */
1323 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1328 /* allocate an mbuf to reply. */
1329 if (replylen > MCLBYTES) {
1331 * XXX: should we try to allocate more? But MCLBYTES
1332 * is probably much larger than IPV6_MMTU...
1336 n = m_getb(replylen, MB_DONTWAIT, m->m_type, M_PKTHDR);
1341 M_MOVE_PKTHDR(n, m); /* just for recvif */
1342 n->m_pkthdr.len = n->m_len = replylen;
1344 /* copy mbuf header and IPv6 + Node Information base headers */
1345 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1346 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1347 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1349 /* qtype dependent procedure */
1352 nni6->ni_code = ICMP6_NI_SUCCESS;
1355 case NI_QTYPE_SUPTYPES:
1358 nni6->ni_code = ICMP6_NI_SUCCESS;
1359 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1360 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1361 v = (u_int32_t)htonl(0x0000000f);
1362 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1366 nni6->ni_code = ICMP6_NI_SUCCESS;
1367 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1368 sizeof(struct ip6_hdr) +
1369 sizeof(struct icmp6_nodeinfo));
1370 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1371 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1373 * XXX do we really have FQDN in variable "hostname"?
1375 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1376 if (n->m_next == NULL)
1378 /* XXX we assume that n->m_next is not a chain */
1379 if (n->m_next->m_next != NULL)
1381 n->m_pkthdr.len += n->m_next->m_len;
1383 case NI_QTYPE_NODEADDR:
1387 nni6->ni_code = ICMP6_NI_SUCCESS;
1388 n->m_pkthdr.len = n->m_len =
1389 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1390 lenlim = M_TRAILINGSPACE(n);
1391 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1392 /* XXX: reset mbuf length */
1393 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1394 sizeof(struct icmp6_nodeinfo) + copied;
1398 break; /* XXX impossible! */
1401 nni6->ni_type = ICMP6_NI_REPLY;
1414 * make a mbuf with DNS-encoded string. no compression support.
1416 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1417 * treated as truncated name (two \0 at the end). this is a wild guess.
1419 static struct mbuf *
1420 ni6_nametodns(const char *name, int namelen,
1421 int old) /* return pascal string if non-zero */
1433 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1434 m = m_getb(len, MB_DONTWAIT, MT_DATA, 0);
1440 *mtod(m, char *) = namelen;
1441 bcopy(name, mtod(m, char *) + 1, namelen);
1445 cp = mtod(m, char *);
1446 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1448 /* if not certain about my name, return empty buffer */
1453 * guess if it looks like shortened hostname, or FQDN.
1454 * shortened hostname needs two trailing "\0".
1457 for (p = name; p < name + namelen; p++) {
1458 if (*p && *p == '.')
1467 while (cp < ep && p < name + namelen) {
1469 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1471 /* result does not fit into mbuf */
1472 if (cp + i + 1 >= ep)
1475 * DNS label length restriction, RFC1035 page 8.
1476 * "i == 0" case is included here to avoid returning
1477 * 0-length label on "foo..bar".
1479 if (i <= 0 || i >= 64)
1485 if (p < name + namelen && *p == '.')
1489 if (cp + nterm >= ep)
1493 m->m_len = cp - mtod(m, char *);
1497 panic("should not reach here");
1507 * check if two DNS-encoded string matches. takes care of truncated
1508 * form (with \0\0 at the end). no compression support.
1509 * XXX upper/lowercase match (see RFC2065)
1512 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1514 const char *a0, *b0;
1517 /* simplest case - need validation? */
1518 if (alen == blen && bcmp(a, b, alen) == 0)
1524 /* termination is mandatory */
1525 if (alen < 2 || blen < 2)
1527 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1532 while (a - a0 < alen && b - b0 < blen) {
1533 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1536 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1538 /* we don't support compression yet */
1539 if (a[0] >= 64 || b[0] >= 64)
1542 /* truncated case */
1543 if (a[0] == 0 && a - a0 == alen - 1)
1545 if (b[0] == 0 && b - b0 == blen - 1)
1547 if (a[0] == 0 || b[0] == 0)
1553 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1555 if (bcmp(a + 1, b + 1, l) != 0)
1562 if (a - a0 == alen && b - b0 == blen)
1569 * calculate the number of addresses to be returned in the node info reply.
1572 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1576 struct in6_ifaddr *ifa6;
1577 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1578 int addrs = 0, addrsofif, iffound = 0;
1579 int niflags = ni6->ni_flags;
1581 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1582 switch (ni6->ni_code) {
1583 case ICMP6_NI_SUBJ_IPV6:
1584 if (subj == NULL) /* must be impossible... */
1586 subj_ip6 = (struct sockaddr_in6 *)subj;
1590 * XXX: we only support IPv6 subject address for
1597 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1599 struct ifaddr_container *ifac;
1602 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link)
1604 struct ifaddr *ifa = ifac->ifa;
1606 if (ifa->ifa_addr->sa_family != AF_INET6)
1608 ifa6 = (struct in6_ifaddr *)ifa;
1610 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1611 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1612 &ifa6->ia_addr.sin6_addr))
1616 * IPv4-mapped addresses can only be returned by a
1617 * Node Information proxy, since they represent
1618 * addresses of IPv4-only nodes, which perforce do
1619 * not implement this protocol.
1620 * [icmp-name-lookups-07, Section 5.4]
1621 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1622 * this function at this moment.
1625 /* What do we have to do about ::1? */
1626 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1627 case IPV6_ADDR_SCOPE_LINKLOCAL:
1628 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1631 case IPV6_ADDR_SCOPE_SITELOCAL:
1632 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1635 case IPV6_ADDR_SCOPE_GLOBAL:
1636 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1644 * check if anycast is okay.
1645 * XXX: just experimental. not in the spec.
1647 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) &&
1648 !(niflags & NI_NODEADDR_FLAG_ANYCAST))
1649 continue; /* we need only unicast addresses */
1650 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) &&
1651 (icmp6_nodeinfo & 4) == 0) {
1654 addrsofif++; /* count the address */
1668 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1669 struct ifnet *ifp0, int resid)
1671 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1672 struct in6_ifaddr *ifa6;
1673 struct ifnet *ifp_dep = NULL;
1674 int copied = 0, allow_deprecated = 0;
1675 u_char *cp = (u_char *)(nni6 + 1);
1676 int niflags = ni6->ni_flags;
1679 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1680 return (0); /* needless to copy */
1684 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1686 struct ifaddr_container *ifac;
1688 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1689 struct ifaddr *ifa = ifac->ifa;
1691 if (ifa->ifa_addr->sa_family != AF_INET6)
1693 ifa6 = (struct in6_ifaddr *)ifa;
1695 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) &&
1696 allow_deprecated == 0) {
1698 * prefererred address should be put before
1699 * deprecated addresses.
1702 /* record the interface for later search */
1703 if (ifp_dep == NULL)
1708 else if (!(ifa6->ia6_flags & IN6_IFF_DEPRECATED) &&
1709 allow_deprecated != 0)
1710 continue; /* we now collect deprecated addrs */
1712 /* What do we have to do about ::1? */
1713 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1714 case IPV6_ADDR_SCOPE_LINKLOCAL:
1715 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1718 case IPV6_ADDR_SCOPE_SITELOCAL:
1719 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1722 case IPV6_ADDR_SCOPE_GLOBAL:
1723 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1731 * check if anycast is okay.
1732 * XXX: just experimental. not in the spec.
1734 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) &&
1735 !(niflags & NI_NODEADDR_FLAG_ANYCAST))
1737 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) &&
1738 (icmp6_nodeinfo & 4) == 0) {
1742 /* now we can copy the address */
1743 if (resid < sizeof(struct in6_addr) +
1744 sizeof(u_int32_t)) {
1746 * We give up much more copy.
1747 * Set the truncate flag and return.
1750 NI_NODEADDR_FLAG_TRUNCATE;
1755 * Set the TTL of the address.
1756 * The TTL value should be one of the following
1757 * according to the specification:
1759 * 1. The remaining lifetime of a DHCP lease on the
1761 * 2. The remaining Valid Lifetime of a prefix from
1762 * which the address was derived through Stateless
1763 * Autoconfiguration.
1765 * Note that we currently do not support stateful
1766 * address configuration by DHCPv6, so the former
1767 * case can't happen.
1769 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1770 ltime = ND6_INFINITE_LIFETIME;
1772 if (ifa6->ia6_lifetime.ia6t_expire >
1774 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1779 bcopy(<ime, cp, sizeof(u_int32_t));
1780 cp += sizeof(u_int32_t);
1782 /* copy the address itself */
1783 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1784 sizeof(struct in6_addr));
1785 /* XXX: KAME link-local hack; remove ifindex */
1786 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1787 ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1788 cp += sizeof(struct in6_addr);
1790 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1791 copied += (sizeof(struct in6_addr) +
1794 if (ifp0) /* we need search only on the specified IF */
1798 if (allow_deprecated == 0 && ifp_dep != NULL) {
1800 allow_deprecated = 1;
1809 * XXX almost dup'ed code with rip6_input.
1812 icmp6_rip6_input(struct mbuf **mp, int off)
1814 struct mbuf *m = *mp;
1815 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1816 struct in6pcb *in6p;
1817 struct in6pcb *last = NULL;
1818 struct sockaddr_in6 rip6src;
1819 struct icmp6_hdr *icmp6;
1820 struct mbuf *opts = NULL;
1822 #ifndef PULLDOWN_TEST
1823 /* this is assumed to be safe. */
1824 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1826 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1827 if (icmp6 == NULL) {
1828 /* m is already reclaimed */
1829 return IPPROTO_DONE;
1833 bzero(&rip6src, sizeof(rip6src));
1834 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1835 rip6src.sin6_family = AF_INET6;
1836 /* KAME hack: recover scopeid */
1837 in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1839 LIST_FOREACH(in6p, &ripcbinfo.pcblisthead, inp_list)
1841 if (in6p->inp_flags & INP_PLACEMARKER)
1843 if ((in6p->inp_vflag & INP_IPV6) == 0)
1845 #ifdef HAVE_NRL_INPCB
1846 if (!(in6p->in6p_flags & INP_IPV6))
1849 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1851 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1852 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1854 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1855 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1857 if (in6p->in6p_icmp6filt
1858 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1859 in6p->in6p_icmp6filt))
1863 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1864 if (last->in6p_flags & IN6P_CONTROLOPTS)
1865 ip6_savecontrol(last, &opts, ip6, n);
1866 /* strip intermediate headers */
1868 if (ssb_appendaddr(&last->in6p_socket->so_rcv,
1869 (struct sockaddr *)&rip6src,
1871 /* should notify about lost packet */
1877 sorwakeup(last->in6p_socket);
1884 if (last->in6p_flags & IN6P_CONTROLOPTS)
1885 ip6_savecontrol(last, &opts, ip6, m);
1886 /* strip intermediate headers */
1888 if (ssb_appendaddr(&last->in6p_socket->so_rcv,
1889 (struct sockaddr *)&rip6src, m, opts) == 0) {
1894 sorwakeup(last->in6p_socket);
1897 ip6stat.ip6s_delivered--;
1899 return IPPROTO_DONE;
1903 * Reflect the ip6 packet back to the source.
1904 * OFF points to the icmp6 header, counted from the top of the mbuf.
1907 icmp6_reflect(struct mbuf *m, size_t off)
1909 struct ip6_hdr *ip6;
1910 struct icmp6_hdr *icmp6;
1911 struct in6_ifaddr *ia;
1912 struct in6_addr t, *src = 0;
1915 struct ifnet *outif = NULL;
1916 struct sockaddr_in6 sa6_src, sa6_dst;
1917 #ifdef COMPAT_RFC1885
1918 int mtu = IPV6_MMTU;
1919 struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
1922 /* too short to reflect */
1923 if (off < sizeof(struct ip6_hdr)) {
1925 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1926 (u_long)off, (u_long)sizeof(struct ip6_hdr),
1927 __FILE__, __LINE__));
1932 * If there are extra headers between IPv6 and ICMPv6, strip
1933 * off that header first.
1936 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
1937 panic("assumption failed in icmp6_reflect");
1939 if (off > sizeof(struct ip6_hdr)) {
1941 struct ip6_hdr nip6;
1943 l = off - sizeof(struct ip6_hdr);
1944 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
1946 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1948 if ((m = m_pullup(m, l)) == NULL)
1951 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
1952 } else /* off == sizeof(struct ip6_hdr) */ {
1954 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1956 if ((m = m_pullup(m, l)) == NULL)
1960 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
1961 ip6 = mtod(m, struct ip6_hdr *);
1962 ip6->ip6_nxt = IPPROTO_ICMPV6;
1963 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
1964 type = icmp6->icmp6_type; /* keep type for statistics */
1965 code = icmp6->icmp6_code; /* ditto. */
1969 * ip6_input() drops a packet if its src is multicast.
1970 * So, the src is never multicast.
1972 ip6->ip6_dst = ip6->ip6_src;
1975 * XXX: make sure to embed scope zone information, using
1976 * already embedded IDs or the received interface (if any).
1977 * Note that rcvif may be NULL.
1978 * TODO: scoped routing case (XXX).
1980 bzero(&sa6_src, sizeof(sa6_src));
1981 sa6_src.sin6_family = AF_INET6;
1982 sa6_src.sin6_len = sizeof(sa6_src);
1983 sa6_src.sin6_addr = ip6->ip6_dst;
1984 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
1985 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
1986 bzero(&sa6_dst, sizeof(sa6_dst));
1987 sa6_dst.sin6_family = AF_INET6;
1988 sa6_dst.sin6_len = sizeof(sa6_dst);
1989 sa6_dst.sin6_addr = t;
1990 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
1991 in6_embedscope(&t, &sa6_dst, NULL, NULL);
1993 #ifdef COMPAT_RFC1885
1996 * RFC 1885 requires that echo reply should be truncated if it
1997 * does not fit in with (return) path MTU, but the description was
1998 * removed in the new spec.
2000 if (icmp6_reflect_rt.ro_rt == 0 ||
2001 ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
2002 if (icmp6_reflect_rt.ro_rt) {
2003 RTFREE(icmp6_reflect_rt.ro_rt);
2004 icmp6_reflect_rt.ro_rt = 0;
2006 bzero(sin6, sizeof(*sin6));
2007 sin6->sin6_family = PF_INET6;
2008 sin6->sin6_len = sizeof(struct sockaddr_in6);
2009 sin6->sin6_addr = ip6->ip6_dst;
2011 rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
2015 if (icmp6_reflect_rt.ro_rt == 0)
2018 if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
2019 && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
2020 mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
2022 if (mtu < m->m_pkthdr.len) {
2023 plen -= (m->m_pkthdr.len - mtu);
2024 m_adj(m, mtu - m->m_pkthdr.len);
2028 * If the incoming packet was addressed directly to us(i.e. unicast),
2029 * use dst as the src for the reply.
2030 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2031 * (for example) when we encounter an error while forwarding procedure
2032 * destined to a duplicated address of ours.
2034 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2035 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2036 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2040 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2042 * This is the case if the dst is our link-local address
2043 * and the sender is also ourselves.
2050 struct route_in6 ro;
2053 * This case matches to multicasts, our anycast, or unicasts
2054 * that we do not own. Select a source address based on the
2055 * source address of the erroneous packet.
2057 bzero(&ro, sizeof(ro));
2058 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e, NULL);
2060 RTFREE(ro.ro_rt); /* XXX: we could use this */
2063 "icmp6_reflect: source can't be determined: "
2064 "dst=%s, error=%d\n",
2065 ip6_sprintf(&sa6_src.sin6_addr), e));
2070 ip6->ip6_src = *src;
2073 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2074 ip6->ip6_vfc |= IPV6_VERSION;
2075 ip6->ip6_nxt = IPPROTO_ICMPV6;
2076 if (m->m_pkthdr.rcvif) {
2077 /* XXX: This may not be the outgoing interface */
2078 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2080 ip6->ip6_hlim = ip6_defhlim;
2082 icmp6->icmp6_cksum = 0;
2083 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2084 sizeof(struct ip6_hdr), plen);
2087 * XXX option handling
2090 m->m_flags &= ~(M_BCAST|M_MCAST);
2092 #ifdef COMPAT_RFC1885
2093 ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
2095 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2098 icmp6_ifoutstat_inc(outif, type, code);
2108 icmp6_fasttimo(void)
2115 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2116 struct in6_addr *tgt6)
2118 static char buf[1024];
2119 ksnprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2120 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2125 icmp6_redirect_input(struct mbuf *m, int off)
2127 struct ifnet *ifp = m->m_pkthdr.rcvif;
2128 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2129 struct nd_redirect *nd_rd;
2130 int icmp6len = ntohs(ip6->ip6_plen);
2131 char *lladdr = NULL;
2133 u_char *redirhdr = NULL;
2134 int redirhdrlen = 0;
2135 struct rtentry *rt = NULL;
2138 struct in6_addr src6 = ip6->ip6_src;
2139 struct in6_addr redtgt6;
2140 struct in6_addr reddst6;
2141 union nd_opts ndopts;
2146 /* XXX if we are router, we don't update route by icmp6 redirect */
2149 if (!icmp6_rediraccept)
2152 #ifndef PULLDOWN_TEST
2153 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2154 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2156 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2157 if (nd_rd == NULL) {
2158 icmp6stat.icp6s_tooshort++;
2162 redtgt6 = nd_rd->nd_rd_target;
2163 reddst6 = nd_rd->nd_rd_dst;
2165 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2166 redtgt6.s6_addr16[1] = htons(ifp->if_index);
2167 if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2168 reddst6.s6_addr16[1] = htons(ifp->if_index);
2171 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2173 "ICMP6 redirect sent from %s rejected; "
2174 "must be from linklocal\n", ip6_sprintf(&src6)));
2177 if (ip6->ip6_hlim != 255) {
2179 "ICMP6 redirect sent from %s rejected; "
2180 "hlim=%d (must be 255)\n",
2181 ip6_sprintf(&src6), ip6->ip6_hlim));
2185 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2186 struct sockaddr_in6 sin6;
2187 struct in6_addr *gw6;
2189 bzero(&sin6, sizeof(sin6));
2190 sin6.sin6_family = AF_INET6;
2191 sin6.sin6_len = sizeof(struct sockaddr_in6);
2192 bcopy(&reddst6, &sin6.sin6_addr, sizeof reddst6);
2193 rt = rtpurelookup((struct sockaddr *)&sin6);
2195 if (rt->rt_gateway == NULL ||
2196 rt->rt_gateway->sa_family != AF_INET6) {
2198 "ICMP6 redirect rejected; no route "
2199 "with inet6 gateway found for redirect dst: %s\n",
2200 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2205 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2206 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2208 "ICMP6 redirect rejected; "
2209 "not equal to gw-for-src=%s (must be same): "
2212 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2218 "ICMP6 redirect rejected; "
2219 "no route found for redirect dst: %s\n",
2220 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2226 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2228 "ICMP6 redirect rejected; "
2229 "redirect dst must be unicast: %s\n",
2230 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2234 is_router = is_onlink = 0;
2235 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2236 is_router = 1; /* router case */
2237 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2238 is_onlink = 1; /* on-link destination case */
2239 if (!is_router && !is_onlink) {
2241 "ICMP6 redirect rejected; "
2242 "neither router case nor onlink case: %s\n",
2243 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2246 /* validation passed */
2248 icmp6len -= sizeof(*nd_rd);
2249 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2250 if (nd6_options(&ndopts) < 0) {
2251 nd6log((LOG_INFO, "icmp6_redirect_input: "
2252 "invalid ND option, rejected: %s\n",
2253 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2254 /* nd6_options have incremented stats */
2258 if (ndopts.nd_opts_tgt_lladdr) {
2259 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2260 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2263 if (ndopts.nd_opts_rh) {
2264 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2265 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2268 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2270 "icmp6_redirect_input: lladdrlen mismatch for %s "
2271 "(if %d, icmp6 packet %d): %s\n",
2272 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2273 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2278 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2279 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2281 if (!is_onlink) { /* better router case. perform rtredirect. */
2282 /* perform rtredirect */
2283 struct sockaddr_in6 sdst;
2284 struct sockaddr_in6 sgw;
2285 struct sockaddr_in6 ssrc;
2287 bzero(&sdst, sizeof(sdst));
2288 bzero(&sgw, sizeof(sgw));
2289 bzero(&ssrc, sizeof(ssrc));
2290 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2291 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2292 sizeof(struct sockaddr_in6);
2293 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2294 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2295 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2296 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2297 NULL, RTF_GATEWAY | RTF_HOST,
2298 (struct sockaddr *)&ssrc);
2300 /* finally update cached route in each socket via kpfctlinput */
2302 struct sockaddr_in6 sdst;
2304 bzero(&sdst, sizeof(sdst));
2305 sdst.sin6_family = AF_INET6;
2306 sdst.sin6_len = sizeof(struct sockaddr_in6);
2307 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2308 kpfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2310 key_sa_routechange((struct sockaddr *)&sdst);
2319 icmp6stat.icp6s_badredirect++;
2324 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2326 struct ifnet *ifp; /* my outgoing interface */
2327 struct in6_addr *ifp_ll6;
2328 struct in6_addr *router_ll6;
2329 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2330 struct mbuf *m = NULL; /* newly allocated one */
2331 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2332 struct nd_redirect *nd_rd;
2335 struct ifnet *outif = NULL;
2336 struct sockaddr_in6 src_sa;
2338 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2340 /* if we are not router, we don't send icmp6 redirect */
2341 if (!ip6_forwarding || ip6_accept_rtadv)
2345 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2350 * the source address must identify a neighbor, and
2351 * the destination address must not be a multicast address
2352 * [RFC 2461, sec 8.2]
2354 sip6 = mtod(m0, struct ip6_hdr *);
2355 bzero(&src_sa, sizeof(src_sa));
2356 src_sa.sin6_family = AF_INET6;
2357 src_sa.sin6_len = sizeof(src_sa);
2358 src_sa.sin6_addr = sip6->ip6_src;
2359 /* we don't currently use sin6_scope_id, but eventually use it */
2360 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2361 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2363 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2364 goto fail; /* what should we do here? */
2367 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2371 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2372 * we almost always ask for an mbuf cluster for simplicity.
2373 * (MHLEN < IPV6_MMTU is almost always true)
2375 #if IPV6_MMTU >= MCLBYTES
2376 # error assumption failed about IPV6_MMTU and MCLBYTES
2378 m = m_getb(IPV6_MMTU, MB_DONTWAIT, MT_HEADER, M_PKTHDR);
2382 maxlen = M_TRAILINGSPACE(m);
2383 maxlen = min(IPV6_MMTU, maxlen);
2384 /* just for safety */
2385 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2386 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2391 /* get ip6 linklocal address for ifp(my outgoing interface). */
2392 struct in6_ifaddr *ia;
2393 if ((ia = in6ifa_ifpforlinklocal(ifp,
2395 IN6_IFF_ANYCAST)) == NULL)
2397 ifp_ll6 = &ia->ia_addr.sin6_addr;
2400 /* get ip6 linklocal address for the router. */
2401 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2402 struct sockaddr_in6 *sin6;
2403 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2404 router_ll6 = &sin6->sin6_addr;
2405 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2411 ip6 = mtod(m, struct ip6_hdr *);
2413 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2414 ip6->ip6_vfc |= IPV6_VERSION;
2415 /* ip6->ip6_plen will be set later */
2416 ip6->ip6_nxt = IPPROTO_ICMPV6;
2417 ip6->ip6_hlim = 255;
2418 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2419 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2420 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2423 nd_rd = (struct nd_redirect *)(ip6 + 1);
2424 nd_rd->nd_rd_type = ND_REDIRECT;
2425 nd_rd->nd_rd_code = 0;
2426 nd_rd->nd_rd_reserved = 0;
2427 if (rt->rt_flags & RTF_GATEWAY) {
2429 * nd_rd->nd_rd_target must be a link-local address in
2430 * better router cases.
2434 bcopy(router_ll6, &nd_rd->nd_rd_target,
2435 sizeof(nd_rd->nd_rd_target));
2436 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2437 sizeof(nd_rd->nd_rd_dst));
2439 /* make sure redtgt == reddst */
2440 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2441 sizeof(nd_rd->nd_rd_target));
2442 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2443 sizeof(nd_rd->nd_rd_dst));
2446 p = (u_char *)(nd_rd + 1);
2452 /* target lladdr option */
2453 struct rtentry *rt_router = NULL;
2455 struct sockaddr_dl *sdl;
2456 struct nd_opt_hdr *nd_opt;
2459 rt_router = nd6_lookup(router_ll6, 0, ifp);
2462 len = sizeof(*nd_opt) + ifp->if_addrlen;
2463 len = (len + 7) & ~7; /* round by 8 */
2465 if (len + (p - (u_char *)ip6) > maxlen)
2467 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2468 (rt_router->rt_flags & RTF_LLINFO) &&
2469 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2470 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2472 nd_opt = (struct nd_opt_hdr *)p;
2473 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2474 nd_opt->nd_opt_len = len >> 3;
2475 lladdr = (char *)(nd_opt + 1);
2476 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2482 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2484 /* just to be safe */
2485 #ifdef M_DECRYPTED /*not openbsd*/
2486 if (m0->m_flags & M_DECRYPTED)
2489 if (p - (u_char *)ip6 > maxlen)
2493 /* redirected header option */
2495 struct nd_opt_rd_hdr *nd_opt_rh;
2498 * compute the maximum size for icmp6 redirect header option.
2499 * XXX room for auth header?
2501 len = maxlen - (p - (u_char *)ip6);
2504 /* This is just for simplicity. */
2505 if (m0->m_pkthdr.len != m0->m_len) {
2507 m_freem(m0->m_next);
2510 m0->m_pkthdr.len = m0->m_len;
2514 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2515 * about padding/truncate rule for the original IP packet.
2516 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2517 * - "attach as much as possible" is the goal
2518 * - pad if not aligned (original size can be guessed by original
2520 * Following code adds the padding if it is simple enough,
2521 * and truncates if not.
2523 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2524 panic("assumption failed in %s:%d", __FILE__, __LINE__);
2526 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2527 /* not enough room, truncate */
2528 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2530 /* enough room, pad or truncate */
2533 extra = m0->m_pkthdr.len % 8;
2535 /* pad if easy enough, truncate if not */
2536 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2538 m0->m_len += (8 - extra);
2539 m0->m_pkthdr.len += (8 - extra);
2542 m0->m_pkthdr.len -= extra;
2546 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2547 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2550 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2551 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2552 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2553 nd_opt_rh->nd_opt_rh_len = len >> 3;
2554 p += sizeof(*nd_opt_rh);
2555 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2557 /* connect m0 to m */
2559 m->m_pkthdr.len = m->m_len + m0->m_len;
2563 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2564 sip6->ip6_src.s6_addr16[1] = 0;
2565 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2566 sip6->ip6_dst.s6_addr16[1] = 0;
2568 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2569 ip6->ip6_src.s6_addr16[1] = 0;
2570 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2571 ip6->ip6_dst.s6_addr16[1] = 0;
2573 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2574 nd_rd->nd_rd_target.s6_addr16[1] = 0;
2575 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2576 nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2578 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2580 nd_rd->nd_rd_cksum = 0;
2582 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2584 /* send the packet to outside... */
2585 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2587 icmp6_ifstat_inc(outif, ifs6_out_msg);
2588 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2590 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2601 #ifdef HAVE_NRL_INPCB
2602 #define in6pcb inpcb
2603 #define in6p_icmp6filt inp_icmp6filt
2606 * ICMPv6 socket option processing.
2609 icmp6_ctloutput(netmsg_t msg)
2611 struct socket *so = msg->ctloutput.base.nm_so;
2612 struct sockopt *sopt = msg->ctloutput.nm_sopt;
2615 struct inpcb *inp = so->so_pcb;
2616 int level, op, optname;
2619 level = sopt->sopt_level;
2620 op = sopt->sopt_dir;
2621 optname = sopt->sopt_name;
2622 optlen = sopt->sopt_valsize;
2624 level = op = optname = optlen = 0;
2626 if (level != IPPROTO_ICMPV6) {
2636 struct icmp6_filter *p;
2638 if (optlen != sizeof(*p)) {
2642 if (inp->in6p_icmp6filt == NULL) {
2646 error = soopt_to_kbuf(sopt, inp->in6p_icmp6filt, optlen,
2652 error = ENOPROTOOPT;
2661 if (inp->in6p_icmp6filt == NULL) {
2665 soopt_from_kbuf(sopt, inp->in6p_icmp6filt,
2666 sizeof(struct icmp6_filter));
2671 error = ENOPROTOOPT;
2677 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
2679 #ifdef HAVE_NRL_INPCB
2681 #undef in6p_icmp6filt
2684 #ifndef HAVE_PPSRATECHECK
2686 #define timersub(tvp, uvp, vvp) \
2688 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2689 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2690 if ((vvp)->tv_usec < 0) { \
2692 (vvp)->tv_usec += 1000000; \
2698 * ppsratecheck(): packets (or events) per second limitation.
2701 ppsratecheck(struct timeval *lasttime, int *curpps,
2702 int maxpps) /* maximum pps allowed */
2704 struct timeval tv, delta;
2709 timersub(&tv, lasttime, &delta);
2712 * Check for 0,0 so that the message will be seen at least once.
2713 * If more than one second has passed since the last update of
2714 * lasttime, reset the counter.
2716 * We do increment *curpps even in *curpps < maxpps case, as some may
2717 * try to use *curpps for stat purposes as well.
2719 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
2720 delta.tv_sec >= 1) {
2724 } else if (maxpps < 0)
2726 else if (*curpps < maxpps)
2731 #if 1 /* DIAGNOSTIC? */
2732 /* be careful about wrap-around */
2733 if (*curpps + 1 > *curpps)
2734 *curpps = *curpps + 1;
2737 * assume that there's not too many calls to this function.
2738 * not sure if the assumption holds, as it depends on *caller's*
2739 * behavior, not the behavior of this function.
2740 * IMHO it is wrong to make assumption on the caller's behavior,
2741 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
2743 *curpps = *curpps + 1;
2751 * Perform rate limit check.
2752 * Returns 0 if it is okay to send the icmp6 packet.
2753 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2756 * XXX per-destination/type check necessary?
2759 icmp6_ratelimit(const struct in6_addr *dst, /* not used at this moment */
2760 const int type, /* not used at this moment */
2761 const int code) /* not used at this moment */
2765 ret = 0; /* okay to send */
2768 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2770 /* The packet is subject to rate limit */