2 /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
65 #include "opt_inet6.h"
66 #include "opt_ipsec.h"
68 #include <sys/param.h>
69 #include <sys/domain.h>
70 #include <sys/kernel.h>
72 #include <sys/malloc.h>
74 #include <sys/protosw.h>
75 #include <sys/signalvar.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
79 #include <sys/syslog.h>
80 #include <sys/systm.h>
84 #include <net/if_dl.h>
85 #include <net/if_types.h>
86 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet/tcp_var.h>
94 #include <netinet6/in6_ifattach.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/ip6protosw.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/scope6_var.h>
99 #include <netinet6/mld6_var.h>
100 #include <netinet6/nd6.h>
103 #include <netinet6/ipsec.h>
104 #include <netkey/key.h>
108 #include <netipsec/ipsec.h>
109 #include <netipsec/key.h>
112 extern struct domain inet6domain;
114 struct icmp6stat icmp6stat;
116 extern struct inpcbinfo ripcbinfo;
117 extern struct inpcbhead ripcb;
118 extern int icmp6errppslim;
119 static int icmp6errpps_count = 0;
120 static struct timeval icmp6errppslim_last;
121 extern int icmp6_nodeinfo;
123 static void icmp6_errcount __P((struct icmp6errstat *, int, int));
124 static int icmp6_rip6_input __P((struct mbuf **, int));
125 static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
126 static const char *icmp6_redirect_diag __P((struct in6_addr *,
127 struct in6_addr *, struct in6_addr *));
128 static struct mbuf *ni6_input __P((struct mbuf *, int));
129 static struct mbuf *ni6_nametodns __P((const char *, int, int));
130 static int ni6_dnsmatch __P((const char *, int, const char *, int));
131 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
132 struct ifnet **, struct in6_addr *));
133 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
134 struct ifnet *, int));
135 static int icmp6_notify_error __P((struct mbuf **, int, int, int));
145 icmp6_errcount(stat, type, code)
146 struct icmp6errstat *stat;
150 case ICMP6_DST_UNREACH:
152 case ICMP6_DST_UNREACH_NOROUTE:
153 stat->icp6errs_dst_unreach_noroute++;
155 case ICMP6_DST_UNREACH_ADMIN:
156 stat->icp6errs_dst_unreach_admin++;
158 case ICMP6_DST_UNREACH_BEYONDSCOPE:
159 stat->icp6errs_dst_unreach_beyondscope++;
161 case ICMP6_DST_UNREACH_ADDR:
162 stat->icp6errs_dst_unreach_addr++;
164 case ICMP6_DST_UNREACH_NOPORT:
165 stat->icp6errs_dst_unreach_noport++;
169 case ICMP6_PACKET_TOO_BIG:
170 stat->icp6errs_packet_too_big++;
172 case ICMP6_TIME_EXCEEDED:
174 case ICMP6_TIME_EXCEED_TRANSIT:
175 stat->icp6errs_time_exceed_transit++;
177 case ICMP6_TIME_EXCEED_REASSEMBLY:
178 stat->icp6errs_time_exceed_reassembly++;
182 case ICMP6_PARAM_PROB:
184 case ICMP6_PARAMPROB_HEADER:
185 stat->icp6errs_paramprob_header++;
187 case ICMP6_PARAMPROB_NEXTHEADER:
188 stat->icp6errs_paramprob_nextheader++;
190 case ICMP6_PARAMPROB_OPTION:
191 stat->icp6errs_paramprob_option++;
196 stat->icp6errs_redirect++;
199 stat->icp6errs_unknown++;
203 * A wrapper function for icmp6_error() necessary when the erroneous packet
204 * may not contain enough scope zone information.
207 icmp6_error2(m, type, code, param, ifp)
209 int type, code, param;
217 #ifndef PULLDOWN_TEST
218 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
220 if (m->m_len < sizeof(struct ip6_hdr)) {
221 m = m_pullup(m, sizeof(struct ip6_hdr));
227 ip6 = mtod(m, struct ip6_hdr *);
229 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
231 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
234 icmp6_error(m, type, code, param);
238 * Generate an error packet of type error in response to bad IP6 packet.
241 icmp6_error(m, type, code, param)
243 int type, code, param;
245 struct ip6_hdr *oip6, *nip6;
246 struct icmp6_hdr *icmp6;
251 icmp6stat.icp6s_error++;
253 /* count per-type-code statistics */
254 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
256 #ifdef M_DECRYPTED /*not openbsd*/
257 if (m->m_flags & M_DECRYPTED) {
258 icmp6stat.icp6s_canterror++;
263 #ifndef PULLDOWN_TEST
264 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
266 if (m->m_len < sizeof(struct ip6_hdr)) {
267 m = m_pullup(m, sizeof(struct ip6_hdr));
272 oip6 = mtod(m, struct ip6_hdr *);
275 * If the destination address of the erroneous packet is a multicast
276 * address, or the packet was sent using link-layer multicast,
277 * we should basically suppress sending an error (RFC 2463, Section
279 * We have two exceptions (the item e.2 in that section):
280 * - the Pakcet Too Big message can be sent for path MTU discovery.
281 * - the Parameter Problem Message that can be allowed an icmp6 error
282 * in the option type field. This check has been done in
283 * ip6_unknown_opt(), so we can just check the type and code.
285 if ((m->m_flags & (M_BCAST|M_MCAST) ||
286 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
287 (type != ICMP6_PACKET_TOO_BIG &&
288 (type != ICMP6_PARAM_PROB ||
289 code != ICMP6_PARAMPROB_OPTION)))
293 * RFC 2463, 2.4 (e.5): source address check.
294 * XXX: the case of anycast source?
296 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
297 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
301 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
305 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
306 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
307 struct icmp6_hdr *icp;
309 #ifndef PULLDOWN_TEST
310 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
311 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
313 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
316 icmp6stat.icp6s_tooshort++;
320 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
321 icp->icmp6_type == ND_REDIRECT) {
324 * Special case: for redirect (which is
325 * informational) we must not send icmp6 error.
327 icmp6stat.icp6s_canterror++;
330 /* ICMPv6 informational - send the error */
333 /* non-ICMPv6 - send the error */
336 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
338 /* Finally, do rate limitation check. */
339 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
340 icmp6stat.icp6s_toofreq++;
345 * OK, ICMP6 can be generated.
348 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
349 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
351 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
352 M_PREPEND(m, preplen, M_DONTWAIT);
353 if (m && m->m_len < preplen)
354 m = m_pullup(m, preplen);
356 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
360 nip6 = mtod(m, struct ip6_hdr *);
361 nip6->ip6_src = oip6->ip6_src;
362 nip6->ip6_dst = oip6->ip6_dst;
364 in6_clearscope(&oip6->ip6_src);
365 in6_clearscope(&oip6->ip6_dst);
367 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
368 icmp6->icmp6_type = type;
369 icmp6->icmp6_code = code;
370 icmp6->icmp6_pptr = htonl((u_int32_t)param);
373 * icmp6_reflect() is designed to be in the input path.
374 * icmp6_error() can be called from both input and output path,
375 * and if we are in output path rcvif could contain bogus value.
376 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
377 * information in ip header (nip6).
379 m->m_pkthdr.rcvif = NULL;
381 icmp6stat.icp6s_outhist[type]++;
382 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
388 * If we can't tell whether or not we can generate ICMP6, free it.
394 * Process a received ICMP6 message.
397 icmp6_input(mp, offp, proto)
401 struct mbuf *m = *mp, *n;
402 struct ip6_hdr *ip6, *nip6;
403 struct icmp6_hdr *icmp6, *nicmp6;
405 int icmp6len = m->m_pkthdr.len - *offp;
407 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
409 #ifndef PULLDOWN_TEST
410 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
411 /* m might change if M_LOOP. So, call mtod after this */
415 * Locate icmp6 structure in mbuf, and check
416 * that not corrupted and of at least minimum length
419 ip6 = mtod(m, struct ip6_hdr *);
420 if (icmp6len < sizeof(struct icmp6_hdr)) {
421 icmp6stat.icp6s_tooshort++;
426 * calculate the checksum
428 #ifndef PULLDOWN_TEST
429 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
431 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
433 icmp6stat.icp6s_tooshort++;
437 code = icmp6->icmp6_code;
439 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
441 "ICMP6 checksum error(%d|%x) %s\n",
442 icmp6->icmp6_type, sum,
443 ip6_sprintf(ip6bufs, &ip6->ip6_src)));
444 icmp6stat.icp6s_checksum++;
448 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
450 * Deliver very specific ICMP6 type only.
451 * This is important to deliver TOOBIG. Otherwise PMTUD
454 switch (icmp6->icmp6_type) {
455 case ICMP6_DST_UNREACH:
456 case ICMP6_PACKET_TOO_BIG:
457 case ICMP6_TIME_EXCEEDED:
464 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
465 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
466 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
467 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
469 switch (icmp6->icmp6_type) {
470 case ICMP6_DST_UNREACH:
471 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
473 case ICMP6_DST_UNREACH_NOROUTE:
474 code = PRC_UNREACH_NET;
476 case ICMP6_DST_UNREACH_ADMIN:
477 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
478 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
480 case ICMP6_DST_UNREACH_ADDR:
483 case ICMP6_DST_UNREACH_BEYONDSCOPE:
484 /* I mean "source address was incorrect." */
485 code = PRC_PARAMPROB;
487 case ICMP6_DST_UNREACH_NOPORT:
488 code = PRC_UNREACH_PORT;
496 case ICMP6_PACKET_TOO_BIG:
497 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
499 /* validation is made in icmp6_mtudisc_update */
504 * Updating the path MTU will be done after examining
505 * intermediate extension headers.
510 case ICMP6_TIME_EXCEEDED:
511 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
513 case ICMP6_TIME_EXCEED_TRANSIT:
514 code = PRC_TIMXCEED_INTRANS;
516 case ICMP6_TIME_EXCEED_REASSEMBLY:
517 code = PRC_TIMXCEED_REASS;
525 case ICMP6_PARAM_PROB:
526 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
528 case ICMP6_PARAMPROB_NEXTHEADER:
529 code = PRC_UNREACH_PROTOCOL;
531 case ICMP6_PARAMPROB_HEADER:
532 case ICMP6_PARAMPROB_OPTION:
533 code = PRC_PARAMPROB;
541 case ICMP6_ECHO_REQUEST:
542 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
545 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
549 if ((n->m_flags & M_EXT) != 0
550 || n->m_len < off + sizeof(struct icmp6_hdr)) {
552 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
555 MGETHDR(n, M_DONTWAIT, n0->m_type);
556 n0len = n0->m_pkthdr.len; /* save for use below */
558 M_MOVE_PKTHDR(n, n0);
559 if (n && maxlen >= MHLEN) {
560 MCLGET(n, M_DONTWAIT);
561 if ((n->m_flags & M_EXT) == 0) {
572 * Copy IPv6 and ICMPv6 only.
574 nip6 = mtod(n, struct ip6_hdr *);
575 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
576 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
577 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
578 noff = sizeof(struct ip6_hdr);
579 /* new mbuf contains only ipv6+icmpv6 headers */
580 n->m_len = noff + sizeof(struct icmp6_hdr);
582 * Adjust mbuf. ip6_plen will be adjusted in
585 m_adj(n0, off + sizeof(struct icmp6_hdr));
586 /* recalculate complete packet size */
587 n->m_pkthdr.len = n0len + (noff - off);
590 nip6 = mtod(n, struct ip6_hdr *);
591 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
595 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
596 nicmp6->icmp6_code = 0;
598 icmp6stat.icp6s_reflect++;
599 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
600 icmp6_reflect(n, noff);
604 case ICMP6_ECHO_REPLY:
605 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
610 case MLD_LISTENER_QUERY:
611 case MLD_LISTENER_REPORT:
612 if (icmp6len < sizeof(struct mld_hdr))
614 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
615 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
617 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
618 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
628 case MLD_LISTENER_DONE:
629 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
630 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
632 break; /* nothing to be done in kernel */
634 case MLD_MTRACE_RESP:
636 /* XXX: these two are experimental. not officially defined. */
637 /* XXX: per-interface statistics? */
638 break; /* just pass it to applications */
640 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
642 enum { WRU, FQDN } mode;
647 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
649 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
654 #define hostnamelen strlen(hostname)
656 #ifndef PULLDOWN_TEST
657 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
660 n = m_copy(m, 0, M_COPYALL);
662 n = ni6_input(n, off);
663 /* XXX meaningless if n == NULL */
664 noff = sizeof(struct ip6_hdr);
670 * XXX: this combination of flags is pointless,
671 * but should we keep this for compatibility?
673 if ((icmp6_nodeinfo & 5) != 5)
678 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
679 if (maxlen >= MCLBYTES) {
683 MGETHDR(n, M_DONTWAIT, m->m_type);
684 if (n && maxlen > MHLEN) {
685 MCLGET(n, M_DONTWAIT);
686 if ((n->m_flags & M_EXT) == 0) {
691 if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) {
693 * Previous code did a blind M_COPY_PKTHDR
694 * and said "just for rcvif". If true, then
695 * we could tolerate the dup failing (due to
696 * the deep copy of the tag chain). For now
697 * be conservative and just fail.
706 n->m_pkthdr.rcvif = NULL;
708 maxhlen = M_TRAILINGSPACE(n) - maxlen;
709 if (maxhlen > hostnamelen)
710 maxhlen = hostnamelen;
712 * Copy IPv6 and ICMPv6 only.
714 nip6 = mtod(n, struct ip6_hdr *);
715 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
716 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
717 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
718 p = (u_char *)(nicmp6 + 1);
720 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
721 noff = sizeof(struct ip6_hdr);
722 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
723 sizeof(struct icmp6_hdr) + 4 + maxhlen;
724 nicmp6->icmp6_type = ICMP6_WRUREPLY;
725 nicmp6->icmp6_code = 0;
729 icmp6stat.icp6s_reflect++;
730 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
731 icmp6_reflect(n, noff);
741 case ND_ROUTER_SOLICIT:
742 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
745 if (icmp6len < sizeof(struct nd_router_solicit))
747 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
749 nd6_rs_input(m, off, icmp6len);
753 nd6_rs_input(n, off, icmp6len);
757 case ND_ROUTER_ADVERT:
758 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
761 if (icmp6len < sizeof(struct nd_router_advert))
763 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
765 nd6_ra_input(m, off, icmp6len);
769 nd6_ra_input(n, off, icmp6len);
773 case ND_NEIGHBOR_SOLICIT:
774 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
777 if (icmp6len < sizeof(struct nd_neighbor_solicit))
779 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
781 nd6_ns_input(m, off, icmp6len);
785 nd6_ns_input(n, off, icmp6len);
789 case ND_NEIGHBOR_ADVERT:
790 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
793 if (icmp6len < sizeof(struct nd_neighbor_advert))
795 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
797 nd6_na_input(m, off, icmp6len);
801 nd6_na_input(n, off, icmp6len);
806 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
809 if (icmp6len < sizeof(struct nd_redirect))
811 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
813 icmp6_redirect_input(m, off);
817 icmp6_redirect_input(n, off);
821 case ICMP6_ROUTER_RENUMBERING:
822 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
823 code != ICMP6_ROUTER_RENUMBERING_RESULT)
825 if (icmp6len < sizeof(struct icmp6_router_renum))
831 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
832 icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
833 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
834 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
835 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
836 /* ICMPv6 error: MUST deliver it by spec... */
840 /* ICMPv6 informational: MUST not deliver */
844 if (icmp6_notify_error(&m, off, icmp6len, code)) {
845 /* In this case, m should've been freed. */
846 return (IPPROTO_DONE);
851 icmp6stat.icp6s_badcode++;
855 icmp6stat.icp6s_badlen++;
859 /* deliver the packet to appropriate sockets */
860 icmp6_rip6_input(&m, *offp);
870 icmp6_notify_error(mp, off, icmp6len, code)
872 int off, icmp6len, code;
874 struct mbuf *m = *mp;
875 struct icmp6_hdr *icmp6;
876 struct ip6_hdr *eip6;
878 struct sockaddr_in6 icmp6src, icmp6dst;
880 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
881 icmp6stat.icp6s_tooshort++;
884 #ifndef PULLDOWN_TEST
885 IP6_EXTHDR_CHECK(m, off,
886 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
887 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
889 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
890 sizeof(*icmp6) + sizeof(struct ip6_hdr));
892 icmp6stat.icp6s_tooshort++;
896 eip6 = (struct ip6_hdr *)(icmp6 + 1);
898 /* Detect the upper level protocol */
900 void (*ctlfunc) __P((int, struct sockaddr *, void *));
901 u_int8_t nxt = eip6->ip6_nxt;
902 int eoff = off + sizeof(struct icmp6_hdr) +
903 sizeof(struct ip6_hdr);
904 struct ip6ctlparam ip6cp;
905 struct in6_addr *finaldst = NULL;
906 int icmp6type = icmp6->icmp6_type;
908 struct ip6_rthdr *rth;
909 struct ip6_rthdr0 *rth0;
912 while (1) { /* XXX: should avoid infinite loop explicitly? */
916 case IPPROTO_HOPOPTS:
917 case IPPROTO_DSTOPTS:
919 #ifndef PULLDOWN_TEST
920 IP6_EXTHDR_CHECK(m, 0,
921 eoff + sizeof(struct ip6_ext), -1);
922 eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
924 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
927 icmp6stat.icp6s_tooshort++;
932 if (nxt == IPPROTO_AH)
933 eoff += (eh->ip6e_len + 2) << 2;
935 eoff += (eh->ip6e_len + 1) << 3;
938 case IPPROTO_ROUTING:
940 * When the erroneous packet contains a
941 * routing header, we should examine the
942 * header to determine the final destination.
943 * Otherwise, we can't properly update
944 * information that depends on the final
945 * destination (e.g. path MTU).
947 #ifndef PULLDOWN_TEST
948 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
949 rth = (struct ip6_rthdr *)
950 (mtod(m, caddr_t) + eoff);
952 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
955 icmp6stat.icp6s_tooshort++;
959 rthlen = (rth->ip6r_len + 1) << 3;
961 * XXX: currently there is no
962 * officially defined type other
964 * Note that if the segment left field
965 * is 0, all intermediate hops must
968 if (rth->ip6r_segleft &&
969 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
972 #ifndef PULLDOWN_TEST
973 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
974 rth0 = (struct ip6_rthdr0 *)
975 (mtod(m, caddr_t) + eoff);
978 struct ip6_rthdr0 *, m,
981 icmp6stat.icp6s_tooshort++;
985 /* just ignore a bogus header */
986 if ((rth0->ip6r0_len % 2) == 0 &&
987 (hops = rth0->ip6r0_len/2))
988 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
993 case IPPROTO_FRAGMENT:
994 #ifndef PULLDOWN_TEST
995 IP6_EXTHDR_CHECK(m, 0, eoff +
996 sizeof(struct ip6_frag), -1);
997 fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1000 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1003 icmp6stat.icp6s_tooshort++;
1008 * Data after a fragment header is meaningless
1009 * unless it is the first fragment, but
1010 * we'll go to the notify label for path MTU
1013 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1016 eoff += sizeof(struct ip6_frag);
1021 * This case includes ESP and the No Next
1022 * Header. In such cases going to the notify
1023 * label does not have any meaning
1024 * (i.e. ctlfunc will be NULL), but we go
1025 * anyway since we might have to update
1026 * path MTU information.
1032 #ifndef PULLDOWN_TEST
1033 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1035 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1036 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1037 if (icmp6 == NULL) {
1038 icmp6stat.icp6s_tooshort++;
1044 * retrieve parameters from the inner IPv6 header, and convert
1045 * them into sockaddr structures.
1046 * XXX: there is no guarantee that the source or destination
1047 * addresses of the inner packet are in the same scope as
1048 * the addresses of the icmp packet. But there is no other
1049 * way to determine the zone.
1051 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1053 bzero(&icmp6dst, sizeof(icmp6dst));
1054 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1055 icmp6dst.sin6_family = AF_INET6;
1056 if (finaldst == NULL)
1057 icmp6dst.sin6_addr = eip6->ip6_dst;
1059 icmp6dst.sin6_addr = *finaldst;
1060 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1062 bzero(&icmp6src, sizeof(icmp6src));
1063 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1064 icmp6src.sin6_family = AF_INET6;
1065 icmp6src.sin6_addr = eip6->ip6_src;
1066 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1068 icmp6src.sin6_flowinfo =
1069 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1071 if (finaldst == NULL)
1072 finaldst = &eip6->ip6_dst;
1074 ip6cp.ip6c_icmp6 = icmp6;
1075 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1076 ip6cp.ip6c_off = eoff;
1077 ip6cp.ip6c_finaldst = finaldst;
1078 ip6cp.ip6c_src = &icmp6src;
1079 ip6cp.ip6c_nxt = nxt;
1081 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1082 notifymtu = ntohl(icmp6->icmp6_mtu);
1083 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1084 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1087 ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
1088 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1090 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1103 icmp6_mtudisc_update(ip6cp, validated)
1104 struct ip6ctlparam *ip6cp;
1107 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1108 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1109 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1110 u_int mtu = ntohl(icmp6->icmp6_mtu);
1111 struct in_conninfo inc;
1115 * RFC2460 section 5, last paragraph.
1116 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1117 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1118 * due to packet translator in the middle.
1119 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1122 if (mtu < IPV6_MMTU)
1127 * we reject ICMPv6 too big with abnormally small value.
1128 * XXX what is the good definition of "abnormally small"?
1130 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1136 bzero(&inc, sizeof(inc));
1137 inc.inc_flags = 1; /* IPv6 */
1138 inc.inc6_faddr = *dst;
1139 if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1142 if (mtu < tcp_maxmtu6(&inc, NULL)) {
1143 tcp_hc_updatemtu(&inc, mtu);
1144 icmp6stat.icp6s_pmtuchg++;
1149 * Process a Node Information Query packet, based on
1150 * draft-ietf-ipngwg-icmp-name-lookups-07.
1152 * Spec incompatibilities:
1153 * - IPv6 Subject address handling
1154 * - IPv4 Subject address handling support missing
1155 * - Proxy reply (answer even if it's not for me)
1156 * - joins NI group address at in6_ifattach() time only, does not cope
1157 * with hostname changes by sethostname(3)
1159 #define hostnamelen strlen(hostname)
1160 static struct mbuf *
1165 struct icmp6_nodeinfo *ni6, *nni6;
1166 struct mbuf *n = NULL;
1169 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1170 struct ni_reply_fqdn *fqdn;
1171 int addrs; /* for NI_QTYPE_NODEADDR */
1172 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1173 struct in6_addr in6_subj; /* subject address */
1174 struct ip6_hdr *ip6;
1175 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1177 struct in6_ifaddr *ia6 = NULL;
1179 ip6 = mtod(m, struct ip6_hdr *);
1180 #ifndef PULLDOWN_TEST
1181 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1183 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1185 /* m is already reclaimed */
1191 * Validate IPv6 source address.
1192 * The default configuration MUST be to refuse answering queries from
1193 * global-scope addresses according to RFC4602.
1195 * - it's not very clear what "refuse" means; this implementation
1197 * - it's not very easy to identify global-scope (unicast) addresses
1198 * since there are many prefixes for them. It should be safer
1199 * and in practice sufficient to check "all" but loopback and
1200 * link-local (note that site-local unicast was deprecated and
1201 * ULA is defined as global scope-wise)
1203 if ((icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1204 !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1205 !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1209 * Validate IPv6 destination address.
1211 * The Responder must discard the Query without further processing
1212 * unless it is one of the Responder's unicast or anycast addresses, or
1213 * a link-local scope multicast address which the Responder has joined.
1214 * [RFC4602, Section 5.]
1216 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1217 if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1219 /* else it's a link-local multicast, fine */
1220 } else { /* unicast or anycast */
1221 if ((ia6 = ip6_getdstifaddr(m)) == NULL)
1222 goto bad; /* XXX impossible */
1224 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1225 !(icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1226 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1227 "a temporary address in %s:%d",
1228 __FILE__, __LINE__));
1233 /* validate query Subject field. */
1234 qtype = ntohs(ni6->ni_qtype);
1235 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1238 case NI_QTYPE_SUPTYPES:
1240 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1244 case NI_QTYPE_NODEADDR:
1245 case NI_QTYPE_IPV4ADDR:
1246 switch (ni6->ni_code) {
1247 case ICMP6_NI_SUBJ_IPV6:
1248 #if ICMP6_NI_SUBJ_IPV6 != 0
1252 * backward compatibility - try to accept 03 draft
1253 * format, where no Subject is present.
1255 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1260 #if ICMP6_NI_SUBJ_IPV6 != 0
1261 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1265 if (subjlen != sizeof(struct in6_addr))
1269 * Validate Subject address.
1271 * Not sure what exactly "address belongs to the node"
1272 * means in the spec, is it just unicast, or what?
1274 * At this moment we consider Subject address as
1275 * "belong to the node" if the Subject address equals
1276 * to the IPv6 destination address; validation for
1277 * IPv6 destination address should have done enough
1280 * We do not do proxy at this moment.
1282 /* m_pulldown instead of copy? */
1283 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1284 subjlen, (caddr_t)&in6_subj);
1285 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1288 subj = (char *)&in6_subj;
1289 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1293 * XXX if we are to allow other cases, we should really
1294 * be careful about scope here.
1295 * basically, we should disallow queries toward IPv6
1296 * destination X with subject Y,
1297 * if scope(X) > scope(Y).
1298 * if we allow scope(X) > scope(Y), it will result in
1299 * information leakage across scope boundary.
1303 case ICMP6_NI_SUBJ_FQDN:
1305 * Validate Subject name with gethostname(3).
1307 * The behavior may need some debate, since:
1308 * - we are not sure if the node has FQDN as
1309 * hostname (returned by gethostname(3)).
1310 * - the code does wildcard match for truncated names.
1311 * however, we are not sure if we want to perform
1312 * wildcard match, if gethostname(3) side has
1313 * truncated hostname.
1315 n = ni6_nametodns(hostname, hostnamelen, 0);
1316 if (!n || n->m_next || n->m_len == 0)
1318 IP6_EXTHDR_GET(subj, char *, m,
1319 off + sizeof(struct icmp6_nodeinfo), subjlen);
1322 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1330 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1337 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1340 if ((icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1343 case NI_QTYPE_NODEADDR:
1344 case NI_QTYPE_IPV4ADDR:
1345 if ((icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1350 /* guess reply length */
1353 break; /* no reply data */
1354 case NI_QTYPE_SUPTYPES:
1355 replylen += sizeof(u_int32_t);
1358 /* XXX will append an mbuf */
1359 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1361 case NI_QTYPE_NODEADDR:
1362 addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1363 if ((replylen += addrs * (sizeof(struct in6_addr) +
1364 sizeof(u_int32_t))) > MCLBYTES)
1365 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1367 case NI_QTYPE_IPV4ADDR:
1368 /* unsupported - should respond with unknown Qtype? */
1372 * XXX: We must return a reply with the ICMP6 code
1373 * `unknown Qtype' in this case. However we regard the case
1374 * as an FQDN query for backward compatibility.
1375 * Older versions set a random value to this field,
1376 * so it rarely varies in the defined qtypes.
1377 * But the mechanism is not reliable...
1378 * maybe we should obsolete older versions.
1380 qtype = NI_QTYPE_FQDN;
1381 /* XXX will append an mbuf */
1382 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1387 /* allocate an mbuf to reply. */
1388 MGETHDR(n, M_DONTWAIT, m->m_type);
1393 M_MOVE_PKTHDR(n, m); /* just for recvif */
1394 if (replylen > MHLEN) {
1395 if (replylen > MCLBYTES) {
1397 * XXX: should we try to allocate more? But MCLBYTES
1398 * is probably much larger than IPV6_MMTU...
1402 MCLGET(n, M_DONTWAIT);
1403 if ((n->m_flags & M_EXT) == 0) {
1407 n->m_pkthdr.len = n->m_len = replylen;
1409 /* copy mbuf header and IPv6 + Node Information base headers */
1410 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1411 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1412 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1414 /* qtype dependent procedure */
1417 nni6->ni_code = ICMP6_NI_SUCCESS;
1420 case NI_QTYPE_SUPTYPES:
1423 nni6->ni_code = ICMP6_NI_SUCCESS;
1424 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1425 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1426 v = (u_int32_t)htonl(0x0000000f);
1427 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1431 nni6->ni_code = ICMP6_NI_SUCCESS;
1432 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1433 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1434 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1435 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1437 * XXX do we really have FQDN in variable "hostname"?
1439 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1440 if (n->m_next == NULL)
1442 /* XXX we assume that n->m_next is not a chain */
1443 if (n->m_next->m_next != NULL)
1445 n->m_pkthdr.len += n->m_next->m_len;
1447 case NI_QTYPE_NODEADDR:
1451 nni6->ni_code = ICMP6_NI_SUCCESS;
1452 n->m_pkthdr.len = n->m_len =
1453 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1454 lenlim = M_TRAILINGSPACE(n);
1455 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1456 /* XXX: reset mbuf length */
1457 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1458 sizeof(struct icmp6_nodeinfo) + copied;
1462 break; /* XXX impossible! */
1465 nni6->ni_type = ICMP6_NI_REPLY;
1478 * make a mbuf with DNS-encoded string. no compression support.
1480 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1481 * treated as truncated name (two \0 at the end). this is a wild guess.
1483 static struct mbuf *
1484 ni6_nametodns(name, namelen, old)
1487 int old; /* return pascal string if non-zero */
1499 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1500 MGET(m, M_DONTWAIT, MT_DATA);
1501 if (m && len > MLEN) {
1502 MCLGET(m, M_DONTWAIT);
1503 if ((m->m_flags & M_EXT) == 0)
1512 *mtod(m, char *) = namelen;
1513 bcopy(name, mtod(m, char *) + 1, namelen);
1517 cp = mtod(m, char *);
1518 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1520 /* if not certain about my name, return empty buffer */
1525 * guess if it looks like shortened hostname, or FQDN.
1526 * shortened hostname needs two trailing "\0".
1529 for (p = name; p < name + namelen; p++) {
1530 if (*p && *p == '.')
1539 while (cp < ep && p < name + namelen) {
1541 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1543 /* result does not fit into mbuf */
1544 if (cp + i + 1 >= ep)
1547 * DNS label length restriction, RFC1035 page 8.
1548 * "i == 0" case is included here to avoid returning
1549 * 0-length label on "foo..bar".
1551 if (i <= 0 || i >= 64)
1557 if (p < name + namelen && *p == '.')
1561 if (cp + nterm >= ep)
1565 m->m_len = cp - mtod(m, char *);
1569 panic("should not reach here");
1579 * check if two DNS-encoded string matches. takes care of truncated
1580 * form (with \0\0 at the end). no compression support.
1581 * XXX upper/lowercase match (see RFC2065)
1584 ni6_dnsmatch(a, alen, b, blen)
1590 const char *a0, *b0;
1593 /* simplest case - need validation? */
1594 if (alen == blen && bcmp(a, b, alen) == 0)
1600 /* termination is mandatory */
1601 if (alen < 2 || blen < 2)
1603 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1608 while (a - a0 < alen && b - b0 < blen) {
1609 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1612 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1614 /* we don't support compression yet */
1615 if (a[0] >= 64 || b[0] >= 64)
1618 /* truncated case */
1619 if (a[0] == 0 && a - a0 == alen - 1)
1621 if (b[0] == 0 && b - b0 == blen - 1)
1623 if (a[0] == 0 || b[0] == 0)
1629 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1631 if (bcmp(a + 1, b + 1, l) != 0)
1638 if (a - a0 == alen && b - b0 == blen)
1645 * calculate the number of addresses to be returned in the node info reply.
1648 ni6_addrs(ni6, m, ifpp, subj)
1649 struct icmp6_nodeinfo *ni6;
1651 struct ifnet **ifpp;
1652 struct in6_addr *subj;
1655 struct in6_ifaddr *ifa6;
1657 int addrs = 0, addrsofif, iffound = 0;
1658 int niflags = ni6->ni_flags;
1660 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1661 switch (ni6->ni_code) {
1662 case ICMP6_NI_SUBJ_IPV6:
1663 if (subj == NULL) /* must be impossible... */
1668 * XXX: we only support IPv6 subject address for
1676 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1678 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1679 if (ifa->ifa_addr->sa_family != AF_INET6)
1681 ifa6 = (struct in6_ifaddr *)ifa;
1683 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1684 IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1688 * IPv4-mapped addresses can only be returned by a
1689 * Node Information proxy, since they represent
1690 * addresses of IPv4-only nodes, which perforce do
1691 * not implement this protocol.
1692 * [icmp-name-lookups-07, Section 5.4]
1693 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1694 * this function at this moment.
1697 /* What do we have to do about ::1? */
1698 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1699 case IPV6_ADDR_SCOPE_LINKLOCAL:
1700 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1703 case IPV6_ADDR_SCOPE_SITELOCAL:
1704 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1707 case IPV6_ADDR_SCOPE_GLOBAL:
1708 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1716 * check if anycast is okay.
1717 * XXX: just experimental. not in the spec.
1719 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1720 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1721 continue; /* we need only unicast addresses */
1722 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1723 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1726 addrsofif++; /* count the address */
1742 ni6_store_addrs(ni6, nni6, ifp0, resid)
1743 struct icmp6_nodeinfo *ni6, *nni6;
1747 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1748 struct in6_ifaddr *ifa6;
1750 struct ifnet *ifp_dep = NULL;
1751 int copied = 0, allow_deprecated = 0;
1752 u_char *cp = (u_char *)(nni6 + 1);
1753 int niflags = ni6->ni_flags;
1756 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1757 return (0); /* needless to copy */
1762 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1763 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1764 ifa = ifa->ifa_list.tqe_next) {
1765 if (ifa->ifa_addr->sa_family != AF_INET6)
1767 ifa6 = (struct in6_ifaddr *)ifa;
1769 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1770 allow_deprecated == 0) {
1772 * prefererred address should be put before
1773 * deprecated addresses.
1776 /* record the interface for later search */
1777 if (ifp_dep == NULL)
1781 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1782 allow_deprecated != 0)
1783 continue; /* we now collect deprecated addrs */
1785 /* What do we have to do about ::1? */
1786 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1787 case IPV6_ADDR_SCOPE_LINKLOCAL:
1788 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1791 case IPV6_ADDR_SCOPE_SITELOCAL:
1792 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1795 case IPV6_ADDR_SCOPE_GLOBAL:
1796 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1804 * check if anycast is okay.
1805 * XXX: just experimental. not in the spec.
1807 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1808 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1810 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1811 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1815 /* now we can copy the address */
1816 if (resid < sizeof(struct in6_addr) +
1817 sizeof(u_int32_t)) {
1819 * We give up much more copy.
1820 * Set the truncate flag and return.
1822 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1828 * Set the TTL of the address.
1829 * The TTL value should be one of the following
1830 * according to the specification:
1832 * 1. The remaining lifetime of a DHCP lease on the
1834 * 2. The remaining Valid Lifetime of a prefix from
1835 * which the address was derived through Stateless
1836 * Autoconfiguration.
1838 * Note that we currently do not support stateful
1839 * address configuration by DHCPv6, so the former
1840 * case can't happen.
1842 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1843 ltime = ND6_INFINITE_LIFETIME;
1845 if (ifa6->ia6_lifetime.ia6t_expire >
1847 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1852 bcopy(<ime, cp, sizeof(u_int32_t));
1853 cp += sizeof(u_int32_t);
1855 /* copy the address itself */
1856 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1857 sizeof(struct in6_addr));
1858 in6_clearscope((struct in6_addr *)cp); /* XXX */
1859 cp += sizeof(struct in6_addr);
1861 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1862 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1864 if (ifp0) /* we need search only on the specified IF */
1868 if (allow_deprecated == 0 && ifp_dep != NULL) {
1870 allow_deprecated = 1;
1881 * XXX almost dup'ed code with rip6_input.
1884 icmp6_rip6_input(mp, off)
1888 struct mbuf *m = *mp;
1889 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1890 struct in6pcb *in6p;
1891 struct in6pcb *last = NULL;
1892 struct sockaddr_in6 fromsa;
1893 struct icmp6_hdr *icmp6;
1894 struct mbuf *opts = NULL;
1896 #ifndef PULLDOWN_TEST
1897 /* this is assumed to be safe. */
1898 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1900 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1901 if (icmp6 == NULL) {
1902 /* m is already reclaimed */
1903 return (IPPROTO_DONE);
1908 * XXX: the address may have embedded scope zone ID, which should be
1909 * hidden from applications.
1911 bzero(&fromsa, sizeof(fromsa));
1912 fromsa.sin6_family = AF_INET6;
1913 fromsa.sin6_len = sizeof(struct sockaddr_in6);
1914 fromsa.sin6_addr = ip6->ip6_src;
1915 if (sa6_recoverscope(&fromsa)) {
1917 return (IPPROTO_DONE);
1920 INP_INFO_RLOCK(&ripcbinfo);
1921 LIST_FOREACH(in6p, &ripcb, inp_list) {
1923 if ((in6p->inp_vflag & INP_IPV6) == 0) {
1928 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1930 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1931 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1933 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1934 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1936 if (in6p->in6p_icmp6filt
1937 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1938 in6p->in6p_icmp6filt))
1941 struct mbuf *n = NULL;
1944 * Recent network drivers tend to allocate a single
1945 * mbuf cluster, rather than to make a couple of
1946 * mbufs without clusters. Also, since the IPv6 code
1947 * path tries to avoid m_pullup(), it is highly
1948 * probable that we still have an mbuf cluster here
1949 * even though the necessary length can be stored in an
1950 * mbuf's internal buffer.
1951 * Meanwhile, the default size of the receive socket
1952 * buffer for raw sockets is not so large. This means
1953 * the possibility of packet loss is relatively higher
1954 * than before. To avoid this scenario, we copy the
1955 * received data to a separate mbuf that does not use
1956 * a cluster, if possible.
1957 * XXX: it is better to copy the data after stripping
1958 * intermediate headers.
1960 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1961 m->m_len <= MHLEN) {
1962 MGET(n, M_DONTWAIT, m->m_type);
1964 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1965 bcopy(m->m_data, n->m_data,
1967 n->m_len = m->m_len;
1975 (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1976 if (last->in6p_flags & IN6P_CONTROLOPTS)
1977 ip6_savecontrol(last, n, &opts);
1978 /* strip intermediate headers */
1980 SOCKBUF_LOCK(&last->in6p_socket->so_rcv);
1981 if (sbappendaddr_locked(
1982 &last->in6p_socket->so_rcv,
1983 (struct sockaddr *)&fromsa, n, opts)
1985 /* should notify about lost packet */
1991 &last->in6p_socket->so_rcv);
1993 sorwakeup_locked(last->in6p_socket);
2001 if (last->in6p_flags & IN6P_CONTROLOPTS)
2002 ip6_savecontrol(last, m, &opts);
2003 /* strip intermediate headers */
2006 /* avoid using mbuf clusters if possible (see above) */
2007 if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2008 m->m_len <= MHLEN) {
2011 MGET(n, M_DONTWAIT, m->m_type);
2013 if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2014 bcopy(m->m_data, n->m_data, m->m_len);
2015 n->m_len = m->m_len;
2025 SOCKBUF_LOCK(&last->in6p_socket->so_rcv);
2026 if (sbappendaddr_locked(&last->in6p_socket->so_rcv,
2027 (struct sockaddr *)&fromsa, m, opts) == 0) {
2031 SOCKBUF_UNLOCK(&last->in6p_socket->so_rcv);
2033 sorwakeup_locked(last->in6p_socket);
2037 ip6stat.ip6s_delivered--;
2039 INP_INFO_RUNLOCK(&ripcbinfo);
2040 return IPPROTO_DONE;
2044 * Reflect the ip6 packet back to the source.
2045 * OFF points to the icmp6 header, counted from the top of the mbuf.
2048 icmp6_reflect(m, off)
2052 struct ip6_hdr *ip6;
2053 struct icmp6_hdr *icmp6;
2054 struct in6_ifaddr *ia;
2057 struct ifnet *outif = NULL;
2058 struct in6_addr origdst, *src = NULL;
2060 /* too short to reflect */
2061 if (off < sizeof(struct ip6_hdr)) {
2063 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2064 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2065 __FILE__, __LINE__));
2070 * If there are extra headers between IPv6 and ICMPv6, strip
2071 * off that header first.
2074 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2075 panic("assumption failed in icmp6_reflect");
2077 if (off > sizeof(struct ip6_hdr)) {
2079 struct ip6_hdr nip6;
2081 l = off - sizeof(struct ip6_hdr);
2082 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2084 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2086 if ((m = m_pullup(m, l)) == NULL)
2089 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2090 } else /* off == sizeof(struct ip6_hdr) */ {
2092 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2094 if ((m = m_pullup(m, l)) == NULL)
2098 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2099 ip6 = mtod(m, struct ip6_hdr *);
2100 ip6->ip6_nxt = IPPROTO_ICMPV6;
2101 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2102 type = icmp6->icmp6_type; /* keep type for statistics */
2103 code = icmp6->icmp6_code; /* ditto. */
2105 origdst = ip6->ip6_dst;
2107 * ip6_input() drops a packet if its src is multicast.
2108 * So, the src is never multicast.
2110 ip6->ip6_dst = ip6->ip6_src;
2113 * If the incoming packet was addressed directly to us (i.e. unicast),
2114 * use dst as the src for the reply.
2115 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2116 * (for example) when we encounter an error while forwarding procedure
2117 * destined to a duplicated address of ours.
2118 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2119 * procedure of an outgoing packet of our own, in which case we need
2120 * to search in the ifaddr list.
2122 if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
2123 if ((ia = ip6_getdstifaddr(m))) {
2124 if (!(ia->ia6_flags &
2125 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2126 src = &ia->ia_addr.sin6_addr;
2128 struct sockaddr_in6 d;
2130 bzero(&d, sizeof(d));
2131 d.sin6_family = AF_INET6;
2132 d.sin6_len = sizeof(d);
2133 d.sin6_addr = origdst;
2134 ia = (struct in6_ifaddr *)
2135 ifa_ifwithaddr((struct sockaddr *)&d);
2138 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2139 src = &ia->ia_addr.sin6_addr;
2146 struct sockaddr_in6 sin6;
2147 struct route_in6 ro;
2150 * This case matches to multicasts, our anycast, or unicasts
2151 * that we do not own. Select a source address based on the
2152 * source address of the erroneous packet.
2154 bzero(&sin6, sizeof(sin6));
2155 sin6.sin6_family = AF_INET6;
2156 sin6.sin6_len = sizeof(sin6);
2157 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2159 bzero(&ro, sizeof(ro));
2160 src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e);
2162 RTFREE(ro.ro_rt); /* XXX: we could use this */
2164 char ip6buf[INET6_ADDRSTRLEN];
2166 "icmp6_reflect: source can't be determined: "
2167 "dst=%s, error=%d\n",
2168 ip6_sprintf(ip6buf, &sin6.sin6_addr), e));
2173 ip6->ip6_src = *src;
2175 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2176 ip6->ip6_vfc |= IPV6_VERSION;
2177 ip6->ip6_nxt = IPPROTO_ICMPV6;
2179 ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2180 else if (m->m_pkthdr.rcvif) {
2181 /* XXX: This may not be the outgoing interface */
2182 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2184 ip6->ip6_hlim = ip6_defhlim;
2186 icmp6->icmp6_cksum = 0;
2187 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2188 sizeof(struct ip6_hdr), plen);
2191 * XXX option handling
2194 m->m_flags &= ~(M_BCAST|M_MCAST);
2196 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2198 icmp6_ifoutstat_inc(outif, type, code);
2215 icmp6_redirect_diag(src6, dst6, tgt6)
2216 struct in6_addr *src6;
2217 struct in6_addr *dst6;
2218 struct in6_addr *tgt6;
2220 static char buf[1024];
2221 char ip6bufs[INET6_ADDRSTRLEN];
2222 char ip6bufd[INET6_ADDRSTRLEN];
2223 char ip6buft[INET6_ADDRSTRLEN];
2224 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2225 ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2226 ip6_sprintf(ip6buft, tgt6));
2231 icmp6_redirect_input(m, off)
2235 struct ifnet *ifp = m->m_pkthdr.rcvif;
2236 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2237 struct nd_redirect *nd_rd;
2238 int icmp6len = ntohs(ip6->ip6_plen);
2239 char *lladdr = NULL;
2241 u_char *redirhdr = NULL;
2242 int redirhdrlen = 0;
2243 struct rtentry *rt = NULL;
2246 struct in6_addr src6 = ip6->ip6_src;
2247 struct in6_addr redtgt6;
2248 struct in6_addr reddst6;
2249 union nd_opts ndopts;
2250 char ip6buf[INET6_ADDRSTRLEN];
2255 /* XXX if we are router, we don't update route by icmp6 redirect */
2258 if (!icmp6_rediraccept)
2261 #ifndef PULLDOWN_TEST
2262 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2263 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2265 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2266 if (nd_rd == NULL) {
2267 icmp6stat.icp6s_tooshort++;
2271 redtgt6 = nd_rd->nd_rd_target;
2272 reddst6 = nd_rd->nd_rd_dst;
2274 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2275 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2280 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2282 "ICMP6 redirect sent from %s rejected; "
2283 "must be from linklocal\n",
2284 ip6_sprintf(ip6buf, &src6)));
2287 if (ip6->ip6_hlim != 255) {
2289 "ICMP6 redirect sent from %s rejected; "
2290 "hlim=%d (must be 255)\n",
2291 ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
2295 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2296 struct sockaddr_in6 sin6;
2297 struct in6_addr *gw6;
2299 bzero(&sin6, sizeof(sin6));
2300 sin6.sin6_family = AF_INET6;
2301 sin6.sin6_len = sizeof(struct sockaddr_in6);
2302 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2303 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
2305 if (rt->rt_gateway == NULL ||
2306 rt->rt_gateway->sa_family != AF_INET6) {
2308 "ICMP6 redirect rejected; no route "
2309 "with inet6 gateway found for redirect dst: %s\n",
2310 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2315 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2316 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2318 "ICMP6 redirect rejected; "
2319 "not equal to gw-for-src=%s (must be same): "
2321 ip6_sprintf(ip6buf, gw6),
2322 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2328 "ICMP6 redirect rejected; "
2329 "no route found for redirect dst: %s\n",
2330 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2336 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2338 "ICMP6 redirect rejected; "
2339 "redirect dst must be unicast: %s\n",
2340 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2344 is_router = is_onlink = 0;
2345 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2346 is_router = 1; /* router case */
2347 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2348 is_onlink = 1; /* on-link destination case */
2349 if (!is_router && !is_onlink) {
2351 "ICMP6 redirect rejected; "
2352 "neither router case nor onlink case: %s\n",
2353 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2356 /* validation passed */
2358 icmp6len -= sizeof(*nd_rd);
2359 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2360 if (nd6_options(&ndopts) < 0) {
2361 nd6log((LOG_INFO, "icmp6_redirect_input: "
2362 "invalid ND option, rejected: %s\n",
2363 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2364 /* nd6_options have incremented stats */
2368 if (ndopts.nd_opts_tgt_lladdr) {
2369 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2370 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2373 if (ndopts.nd_opts_rh) {
2374 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2375 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2378 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2380 "icmp6_redirect_input: lladdrlen mismatch for %s "
2381 "(if %d, icmp6 packet %d): %s\n",
2382 ip6_sprintf(ip6buf, &redtgt6),
2383 ifp->if_addrlen, lladdrlen - 2,
2384 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2389 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2390 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2392 if (!is_onlink) { /* better router case. perform rtredirect. */
2393 /* perform rtredirect */
2394 struct sockaddr_in6 sdst;
2395 struct sockaddr_in6 sgw;
2396 struct sockaddr_in6 ssrc;
2398 bzero(&sdst, sizeof(sdst));
2399 bzero(&sgw, sizeof(sgw));
2400 bzero(&ssrc, sizeof(ssrc));
2401 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2402 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2403 sizeof(struct sockaddr_in6);
2404 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2405 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2406 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2407 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2408 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2409 (struct sockaddr *)&ssrc);
2411 /* finally update cached route in each socket via pfctlinput */
2413 struct sockaddr_in6 sdst;
2415 bzero(&sdst, sizeof(sdst));
2416 sdst.sin6_family = AF_INET6;
2417 sdst.sin6_len = sizeof(struct sockaddr_in6);
2418 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2419 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2420 #if defined(IPSEC) || defined(FAST_IPSEC)
2421 key_sa_routechange((struct sockaddr *)&sdst);
2430 icmp6stat.icp6s_badredirect++;
2435 icmp6_redirect_output(m0, rt)
2439 struct ifnet *ifp; /* my outgoing interface */
2440 struct in6_addr *ifp_ll6;
2441 struct in6_addr *router_ll6;
2442 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2443 struct mbuf *m = NULL; /* newly allocated one */
2444 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2445 struct nd_redirect *nd_rd;
2448 struct ifnet *outif = NULL;
2449 struct sockaddr_in6 src_sa;
2451 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2453 /* if we are not router, we don't send icmp6 redirect */
2454 if (!ip6_forwarding)
2458 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2463 * the source address must identify a neighbor, and
2464 * the destination address must not be a multicast address
2465 * [RFC 2461, sec 8.2]
2467 sip6 = mtod(m0, struct ip6_hdr *);
2468 bzero(&src_sa, sizeof(src_sa));
2469 src_sa.sin6_family = AF_INET6;
2470 src_sa.sin6_len = sizeof(src_sa);
2471 src_sa.sin6_addr = sip6->ip6_src;
2472 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2474 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2475 goto fail; /* what should we do here? */
2478 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2482 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2483 * we almost always ask for an mbuf cluster for simplicity.
2484 * (MHLEN < IPV6_MMTU is almost always true)
2486 #if IPV6_MMTU >= MCLBYTES
2487 # error assumption failed about IPV6_MMTU and MCLBYTES
2489 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2490 if (m && IPV6_MMTU >= MHLEN)
2491 MCLGET(m, M_DONTWAIT);
2494 m->m_pkthdr.rcvif = NULL;
2496 maxlen = M_TRAILINGSPACE(m);
2497 maxlen = min(IPV6_MMTU, maxlen);
2498 /* just for safety */
2499 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2500 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2505 /* get ip6 linklocal address for ifp(my outgoing interface). */
2506 struct in6_ifaddr *ia;
2507 if ((ia = in6ifa_ifpforlinklocal(ifp,
2509 IN6_IFF_ANYCAST)) == NULL)
2511 ifp_ll6 = &ia->ia_addr.sin6_addr;
2514 /* get ip6 linklocal address for the router. */
2515 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2516 struct sockaddr_in6 *sin6;
2517 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2518 router_ll6 = &sin6->sin6_addr;
2519 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2520 router_ll6 = (struct in6_addr *)NULL;
2522 router_ll6 = (struct in6_addr *)NULL;
2525 ip6 = mtod(m, struct ip6_hdr *);
2527 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2528 ip6->ip6_vfc |= IPV6_VERSION;
2529 /* ip6->ip6_plen will be set later */
2530 ip6->ip6_nxt = IPPROTO_ICMPV6;
2531 ip6->ip6_hlim = 255;
2532 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2533 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2534 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2537 nd_rd = (struct nd_redirect *)(ip6 + 1);
2538 nd_rd->nd_rd_type = ND_REDIRECT;
2539 nd_rd->nd_rd_code = 0;
2540 nd_rd->nd_rd_reserved = 0;
2541 if (rt->rt_flags & RTF_GATEWAY) {
2543 * nd_rd->nd_rd_target must be a link-local address in
2544 * better router cases.
2548 bcopy(router_ll6, &nd_rd->nd_rd_target,
2549 sizeof(nd_rd->nd_rd_target));
2550 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2551 sizeof(nd_rd->nd_rd_dst));
2553 /* make sure redtgt == reddst */
2554 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2555 sizeof(nd_rd->nd_rd_target));
2556 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2557 sizeof(nd_rd->nd_rd_dst));
2560 p = (u_char *)(nd_rd + 1);
2566 /* target lladdr option */
2567 struct rtentry *rt_router = NULL;
2569 struct sockaddr_dl *sdl;
2570 struct nd_opt_hdr *nd_opt;
2573 rt_router = nd6_lookup(router_ll6, 0, ifp);
2576 len = sizeof(*nd_opt) + ifp->if_addrlen;
2577 len = (len + 7) & ~7; /* round by 8 */
2579 if (len + (p - (u_char *)ip6) > maxlen)
2581 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2582 (rt_router->rt_flags & RTF_LLINFO) &&
2583 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2584 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2586 nd_opt = (struct nd_opt_hdr *)p;
2587 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2588 nd_opt->nd_opt_len = len >> 3;
2589 lladdr = (char *)(nd_opt + 1);
2590 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2596 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2598 /* just to be safe */
2599 #ifdef M_DECRYPTED /*not openbsd*/
2600 if (m0->m_flags & M_DECRYPTED)
2603 if (p - (u_char *)ip6 > maxlen)
2607 /* redirected header option */
2609 struct nd_opt_rd_hdr *nd_opt_rh;
2612 * compute the maximum size for icmp6 redirect header option.
2613 * XXX room for auth header?
2615 len = maxlen - (p - (u_char *)ip6);
2618 /* This is just for simplicity. */
2619 if (m0->m_pkthdr.len != m0->m_len) {
2621 m_freem(m0->m_next);
2624 m0->m_pkthdr.len = m0->m_len;
2628 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2629 * about padding/truncate rule for the original IP packet.
2630 * From the discussion on IPv6imp in Feb 1999,
2631 * the consensus was:
2632 * - "attach as much as possible" is the goal
2633 * - pad if not aligned (original size can be guessed by
2634 * original ip6 header)
2635 * Following code adds the padding if it is simple enough,
2636 * and truncates if not.
2638 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2639 panic("assumption failed in %s:%d", __FILE__,
2642 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2643 /* not enough room, truncate */
2644 m0->m_pkthdr.len = m0->m_len = len -
2647 /* enough room, pad or truncate */
2650 extra = m0->m_pkthdr.len % 8;
2652 /* pad if easy enough, truncate if not */
2653 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2655 m0->m_len += (8 - extra);
2656 m0->m_pkthdr.len += (8 - extra);
2659 m0->m_pkthdr.len -= extra;
2663 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2664 m0->m_pkthdr.len = m0->m_len = len -
2668 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2669 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2670 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2671 nd_opt_rh->nd_opt_rh_len = len >> 3;
2672 p += sizeof(*nd_opt_rh);
2673 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2675 /* connect m0 to m */
2676 m_tag_delete_chain(m0, NULL);
2677 m0->m_flags &= ~M_PKTHDR;
2679 m->m_pkthdr.len = m->m_len + m0->m_len;
2688 /* XXX: clear embedded link IDs in the inner header */
2689 in6_clearscope(&sip6->ip6_src);
2690 in6_clearscope(&sip6->ip6_dst);
2691 in6_clearscope(&nd_rd->nd_rd_target);
2692 in6_clearscope(&nd_rd->nd_rd_dst);
2694 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2696 nd_rd->nd_rd_cksum = 0;
2697 nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2698 sizeof(*ip6), ntohs(ip6->ip6_plen));
2700 /* send the packet to outside... */
2701 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2703 icmp6_ifstat_inc(outif, ifs6_out_msg);
2704 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2706 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2718 * ICMPv6 socket option processing.
2721 icmp6_ctloutput(so, sopt)
2723 struct sockopt *sopt;
2727 struct inpcb *inp = sotoinpcb(so);
2728 int level, op, optname;
2731 level = sopt->sopt_level;
2732 op = sopt->sopt_dir;
2733 optname = sopt->sopt_name;
2734 optlen = sopt->sopt_valsize;
2736 level = op = optname = optlen = 0;
2738 if (level != IPPROTO_ICMPV6) {
2747 struct icmp6_filter *p;
2749 if (optlen != sizeof(*p)) {
2753 if (inp->in6p_icmp6filt == NULL) {
2757 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2763 error = ENOPROTOOPT;
2772 if (inp->in6p_icmp6filt == NULL) {
2776 error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2777 sizeof(struct icmp6_filter));
2782 error = ENOPROTOOPT;
2792 * Perform rate limit check.
2793 * Returns 0 if it is okay to send the icmp6 packet.
2794 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2797 * XXX per-destination/type check necessary?
2800 icmp6_ratelimit(dst, type, code)
2801 const struct in6_addr *dst; /* not used at this moment */
2802 const int type; /* not used at this moment */
2803 const int code; /* not used at this moment */
2807 ret = 0; /* okay to send */
2810 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2812 /* The packet is subject to rate limit */