1 /* $FreeBSD: src/sys/netinet6/icmp6.c,v 1.6.2.6 2001/07/10 09:44:16 ume Exp $ */
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 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
79 #include <sys/domain.h>
82 #include <net/route.h>
83 #include <net/if_dl.h>
84 #include <net/if_types.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet/icmp6.h>
91 #include <netinet6/mld6_var.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet6/in6_pcb.h>
94 #include <netinet6/nd6.h>
95 #include <netinet6/in6_ifattach.h>
96 #include <netinet6/ip6protosw.h>
99 #include <netinet6/ipsec.h>
100 #include <netkey/key.h>
102 extern int ipsec_bypass
;
106 #if defined(NFAITH) && 0 < NFAITH
107 #include <net/if_faith.h>
110 #include <net/net_osdep.h>
115 #define in6p_laddr inp_laddr6
116 #define in6p_faddr inp_faddr6
117 #define in6p_icmp6filt inp_icmp6filt
118 #define in6p_route inp_route
119 #define in6p_socket inp_socket
120 #define in6p_flags inp_flags
121 #define in6p_moptions inp_moptions6
122 #define in6p_outputopts inp_outputopts6
123 #define in6p_ip6 inp_ipv6
124 #define in6p_flowinfo inp_flowinfo
125 #define in6p_sp inp_sp
126 #define in6p_next inp_next
127 #define in6p_prev inp_prev
129 #define sotoin6pcb sotoinpcb
131 #define in6_pcbdetach in_pcbdetach
132 #define in6_rtchange in_rtchange
135 * for KAME src sync over BSD*'s. XXX: FreeBSD (>=3) are VERY different from
138 #define in6p_ip6_nxt inp_ipv6.ip6_nxt
141 extern struct domain inet6domain
;
142 extern struct ip6protosw inet6sw
[];
143 extern struct ip6protosw
*ip6_protox
[];
145 struct icmp6stat icmp6stat
;
147 extern struct inpcbhead ripcb
;
148 extern int icmp6errppslim
;
149 static int icmp6errpps_count
= 0;
150 static struct timeval icmp6errppslim_last
;
151 extern int icmp6_nodeinfo
;
153 static void icmp6_errcount
__P((struct icmp6errstat
*, int, int));
154 static int icmp6_rip6_input
__P((struct mbuf
**, int));
155 static int icmp6_ratelimit
__P((const struct in6_addr
*, const int, const int));
156 static const char *icmp6_redirect_diag
__P((struct in6_addr
*,
157 struct in6_addr
*, struct in6_addr
*));
158 #ifndef HAVE_PPSRATECHECK
159 static int ppsratecheck
__P((struct timeval
*, int *, int));
161 static struct mbuf
*ni6_input
__P((struct mbuf
*, int));
162 static struct mbuf
*ni6_nametodns
__P((const char *, int, int));
163 static int ni6_dnsmatch
__P((const char *, int, const char *, int));
164 static int ni6_addrs
__P((struct icmp6_nodeinfo
*, struct mbuf
*,
165 struct ifnet
**, char *));
166 static int ni6_store_addrs
__P((struct icmp6_nodeinfo
*, struct icmp6_nodeinfo
*,
167 struct ifnet
*, int));
168 static int icmp6_notify_error
__P((struct mbuf
*, int, int, int));
170 #ifdef COMPAT_RFC1885
171 static struct route_in6 icmp6_reflect_rt
;
182 icmp6_errcount(stat
, type
, code
)
183 struct icmp6errstat
*stat
;
187 case ICMP6_DST_UNREACH
:
189 case ICMP6_DST_UNREACH_NOROUTE
:
190 stat
->icp6errs_dst_unreach_noroute
++;
192 case ICMP6_DST_UNREACH_ADMIN
:
193 stat
->icp6errs_dst_unreach_admin
++;
195 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
196 stat
->icp6errs_dst_unreach_beyondscope
++;
198 case ICMP6_DST_UNREACH_ADDR
:
199 stat
->icp6errs_dst_unreach_addr
++;
201 case ICMP6_DST_UNREACH_NOPORT
:
202 stat
->icp6errs_dst_unreach_noport
++;
206 case ICMP6_PACKET_TOO_BIG
:
207 stat
->icp6errs_packet_too_big
++;
209 case ICMP6_TIME_EXCEEDED
:
211 case ICMP6_TIME_EXCEED_TRANSIT
:
212 stat
->icp6errs_time_exceed_transit
++;
214 case ICMP6_TIME_EXCEED_REASSEMBLY
:
215 stat
->icp6errs_time_exceed_reassembly
++;
219 case ICMP6_PARAM_PROB
:
221 case ICMP6_PARAMPROB_HEADER
:
222 stat
->icp6errs_paramprob_header
++;
224 case ICMP6_PARAMPROB_NEXTHEADER
:
225 stat
->icp6errs_paramprob_nextheader
++;
227 case ICMP6_PARAMPROB_OPTION
:
228 stat
->icp6errs_paramprob_option
++;
233 stat
->icp6errs_redirect
++;
236 stat
->icp6errs_unknown
++;
240 * Generate an error packet of type error in response to bad IP6 packet.
243 icmp6_error(m
, type
, code
, param
)
245 int type
, code
, param
;
247 struct ip6_hdr
*oip6
, *nip6
;
248 struct icmp6_hdr
*icmp6
;
253 icmp6stat
.icp6s_error
++;
255 /* count per-type-code statistics */
256 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, type
, code
);
258 #ifdef M_DECRYPTED /*not openbsd*/
259 if (m
->m_flags
& M_DECRYPTED
) {
260 icmp6stat
.icp6s_canterror
++;
265 #ifndef PULLDOWN_TEST
266 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), );
268 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
269 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
274 oip6
= mtod(m
, struct ip6_hdr
*);
277 * Multicast destination check. For unrecognized option errors,
278 * this check has already done in ip6_unknown_opt(), so we can
279 * check only for other errors.
281 if ((m
->m_flags
& (M_BCAST
|M_MCAST
) ||
282 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_dst
)) &&
283 (type
!= ICMP6_PACKET_TOO_BIG
&&
284 (type
!= ICMP6_PARAM_PROB
||
285 code
!= ICMP6_PARAMPROB_OPTION
)))
288 /* Source address check. XXX: the case of anycast source? */
289 if (IN6_IS_ADDR_UNSPECIFIED(&oip6
->ip6_src
) ||
290 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_src
))
294 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
298 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
299 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
300 struct icmp6_hdr
*icp
;
302 #ifndef PULLDOWN_TEST
303 IP6_EXTHDR_CHECK(m
, 0, off
+ sizeof(struct icmp6_hdr
), );
304 icp
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
306 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
309 icmp6stat
.icp6s_tooshort
++;
313 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
314 icp
->icmp6_type
== ND_REDIRECT
) {
317 * Special case: for redirect (which is
318 * informational) we must not send icmp6 error.
320 icmp6stat
.icp6s_canterror
++;
323 /* ICMPv6 informational - send the error */
326 /* non-ICMPv6 - send the error */
329 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
331 /* Finally, do rate limitation check. */
332 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
333 icmp6stat
.icp6s_toofreq
++;
338 * OK, ICMP6 can be generated.
341 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
)
342 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
344 preplen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
345 M_PREPEND(m
, preplen
, M_DONTWAIT
);
346 if (m
&& m
->m_len
< preplen
)
347 m
= m_pullup(m
, preplen
);
349 nd6log((LOG_DEBUG
, "ENOBUFS in icmp6_error %d\n", __LINE__
));
353 nip6
= mtod(m
, struct ip6_hdr
*);
354 nip6
->ip6_src
= oip6
->ip6_src
;
355 nip6
->ip6_dst
= oip6
->ip6_dst
;
357 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
358 oip6
->ip6_src
.s6_addr16
[1] = 0;
359 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
360 oip6
->ip6_dst
.s6_addr16
[1] = 0;
362 icmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
363 icmp6
->icmp6_type
= type
;
364 icmp6
->icmp6_code
= code
;
365 icmp6
->icmp6_pptr
= htonl((u_int32_t
)param
);
368 * icmp6_reflect() is designed to be in the input path.
369 * icmp6_error() can be called from both input and outut path,
370 * and if we are in output path rcvif could contain bogus value.
371 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
372 * information in ip header (nip6).
374 m
->m_pkthdr
.rcvif
= NULL
;
376 icmp6stat
.icp6s_outhist
[type
]++;
377 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /*header order: IPv6 - ICMPv6*/
383 * If we can't tell wheter or not we can generate ICMP6, free it.
389 * Process a received ICMP6 message.
392 icmp6_input(mp
, offp
, proto
)
396 struct mbuf
*m
= *mp
, *n
;
397 struct ip6_hdr
*ip6
, *nip6
;
398 struct icmp6_hdr
*icmp6
, *nicmp6
;
400 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
403 #ifndef PULLDOWN_TEST
404 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_hdr
), IPPROTO_DONE
);
405 /* m might change if M_LOOP. So, call mtod after this */
409 * Locate icmp6 structure in mbuf, and check
410 * that not corrupted and of at least minimum length
413 ip6
= mtod(m
, struct ip6_hdr
*);
414 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
415 icmp6stat
.icp6s_tooshort
++;
420 * calculate the checksum
422 #ifndef PULLDOWN_TEST
423 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
425 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
427 icmp6stat
.icp6s_tooshort
++;
431 code
= icmp6
->icmp6_code
;
433 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
435 "ICMP6 checksum error(%d|%x) %s\n",
436 icmp6
->icmp6_type
, sum
, ip6_sprintf(&ip6
->ip6_src
)));
437 icmp6stat
.icp6s_checksum
++;
441 #if defined(NFAITH) && 0 < NFAITH
442 if (faithprefix(&ip6
->ip6_dst
)) {
444 * Deliver very specific ICMP6 type only.
445 * This is important to deilver TOOBIG. Otherwise PMTUD
448 switch (icmp6
->icmp6_type
) {
449 case ICMP6_DST_UNREACH
:
450 case ICMP6_PACKET_TOO_BIG
:
451 case ICMP6_TIME_EXCEEDED
:
459 icmp6stat
.icp6s_inhist
[icmp6
->icmp6_type
]++;
460 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_msg
);
461 if (icmp6
->icmp6_type
< ICMP6_INFOMSG_MASK
)
462 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_error
);
464 switch (icmp6
->icmp6_type
) {
465 case ICMP6_DST_UNREACH
:
466 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
468 case ICMP6_DST_UNREACH_NOROUTE
:
469 code
= PRC_UNREACH_NET
;
471 case ICMP6_DST_UNREACH_ADMIN
:
472 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_adminprohib
);
473 code
= PRC_UNREACH_PROTOCOL
; /* is this a good code? */
475 case ICMP6_DST_UNREACH_ADDR
:
478 #ifdef COMPAT_RFC1885
479 case ICMP6_DST_UNREACH_NOTNEIGHBOR
:
480 code
= PRC_UNREACH_SRCFAIL
;
483 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
484 /* I mean "source address was incorrect." */
485 code
= PRC_PARAMPROB
;
488 case ICMP6_DST_UNREACH_NOPORT
:
489 code
= PRC_UNREACH_PORT
;
497 case ICMP6_PACKET_TOO_BIG
:
498 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
505 * Updating the path MTU will be done after examining
506 * intermediate extension headers.
511 case ICMP6_TIME_EXCEEDED
:
512 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
514 case ICMP6_TIME_EXCEED_TRANSIT
:
515 case ICMP6_TIME_EXCEED_REASSEMBLY
:
516 code
+= PRC_TIMXCEED_INTRANS
;
524 case ICMP6_PARAM_PROB
:
525 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
527 case ICMP6_PARAMPROB_NEXTHEADER
:
528 code
= PRC_UNREACH_PROTOCOL
;
530 case ICMP6_PARAMPROB_HEADER
:
531 case ICMP6_PARAMPROB_OPTION
:
532 code
= PRC_PARAMPROB
;
540 case ICMP6_ECHO_REQUEST
:
541 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
544 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
548 if ((n
->m_flags
& M_EXT
) != 0
549 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
551 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
554 * Prepare an internal mbuf. m_pullup() doesn't
555 * always copy the length we specified.
557 if (maxlen
>= MCLBYTES
) {
562 MGETHDR(n
, M_DONTWAIT
, n0
->m_type
);
563 if (n
&& maxlen
>= MHLEN
) {
564 MCLGET(n
, M_DONTWAIT
);
565 if ((n
->m_flags
& M_EXT
) == 0) {
575 M_COPY_PKTHDR(n
, n0
);
577 * Copy IPv6 and ICMPv6 only.
579 nip6
= mtod(n
, struct ip6_hdr
*);
580 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
581 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
582 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
583 noff
= sizeof(struct ip6_hdr
);
584 n
->m_pkthdr
.len
= n
->m_len
=
585 noff
+ sizeof(struct icmp6_hdr
);
587 * Adjust mbuf. ip6_plen will be adjusted in
590 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
591 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
593 n0
->m_flags
&= ~M_PKTHDR
;
595 nip6
= mtod(n
, struct ip6_hdr
*);
596 nicmp6
= (struct icmp6_hdr
*)((caddr_t
)nip6
+ off
);
599 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
600 nicmp6
->icmp6_code
= 0;
602 icmp6stat
.icp6s_reflect
++;
603 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
604 icmp6_reflect(n
, noff
);
608 case ICMP6_ECHO_REPLY
:
609 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
614 case MLD6_LISTENER_QUERY
:
615 case MLD6_LISTENER_REPORT
:
616 if (icmp6len
< sizeof(struct mld6_hdr
))
618 if (icmp6
->icmp6_type
== MLD6_LISTENER_QUERY
) /* XXX: ugly... */
619 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
621 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
622 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
632 case MLD6_LISTENER_DONE
:
633 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
634 if (icmp6len
< sizeof(struct mld6_hdr
)) /* necessary? */
636 break; /* nothing to be done in kernel */
638 case MLD6_MTRACE_RESP
:
640 /* XXX: these two are experimental. not officially defind. */
641 /* XXX: per-interface statistics? */
642 break; /* just pass it to applications */
644 case ICMP6_WRUREQUEST
: /* ICMP6_FQDN_QUERY */
646 enum { WRU
, FQDN
} mode
;
651 if (icmp6len
== sizeof(struct icmp6_hdr
) + 4)
653 else if (icmp6len
>= sizeof(struct icmp6_nodeinfo
))
658 #define hostnamelen strlen(hostname)
660 #ifndef PULLDOWN_TEST
661 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
664 n
= m_copy(m
, 0, M_COPYALL
);
666 n
= ni6_input(n
, off
);
667 /* XXX meaningless if n == NULL */
668 noff
= sizeof(struct ip6_hdr
);
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) {
695 n
->m_pkthdr
.rcvif
= NULL
;
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 M_COPY_PKTHDR(n
, m
); /* just for recvif */
712 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
713 sizeof(struct icmp6_hdr
) + 4 + maxhlen
;
714 nicmp6
->icmp6_type
= ICMP6_WRUREPLY
;
715 nicmp6
->icmp6_code
= 0;
719 icmp6stat
.icp6s_reflect
++;
720 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
721 icmp6_reflect(n
, noff
);
731 case ND_ROUTER_SOLICIT
:
732 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
735 if (icmp6len
< sizeof(struct nd_router_solicit
))
737 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
739 nd6_rs_input(m
, off
, icmp6len
);
743 nd6_rs_input(n
, off
, icmp6len
);
747 case ND_ROUTER_ADVERT
:
748 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
751 if (icmp6len
< sizeof(struct nd_router_advert
))
753 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
755 nd6_ra_input(m
, off
, icmp6len
);
759 nd6_ra_input(n
, off
, icmp6len
);
763 case ND_NEIGHBOR_SOLICIT
:
764 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
767 if (icmp6len
< sizeof(struct nd_neighbor_solicit
))
769 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
771 nd6_ns_input(m
, off
, icmp6len
);
775 nd6_ns_input(n
, off
, icmp6len
);
779 case ND_NEIGHBOR_ADVERT
:
780 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
783 if (icmp6len
< sizeof(struct nd_neighbor_advert
))
785 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
787 nd6_na_input(m
, off
, icmp6len
);
791 nd6_na_input(n
, off
, icmp6len
);
796 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
799 if (icmp6len
< sizeof(struct nd_redirect
))
801 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
803 icmp6_redirect_input(m
, off
);
807 icmp6_redirect_input(n
, off
);
811 case ICMP6_ROUTER_RENUMBERING
:
812 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
813 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
)
815 if (icmp6len
< sizeof(struct icmp6_router_renum
))
821 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
822 icmp6
->icmp6_type
, ip6_sprintf(&ip6
->ip6_src
),
823 ip6_sprintf(&ip6
->ip6_dst
),
824 m
->m_pkthdr
.rcvif
? m
->m_pkthdr
.rcvif
->if_index
: 0));
825 if (icmp6
->icmp6_type
< ICMP6_ECHO_REQUEST
) {
826 /* ICMPv6 error: MUST deliver it by spec... */
830 /* ICMPv6 informational: MUST not deliver */
834 if (icmp6_notify_error(m
, off
, icmp6len
, code
)) {
835 /* In this case, m should've been freed. */
836 return(IPPROTO_DONE
);
841 icmp6stat
.icp6s_badcode
++;
845 icmp6stat
.icp6s_badlen
++;
849 /* deliver the packet to appropriate sockets */
850 icmp6_rip6_input(&m
, *offp
);
860 icmp6_notify_error(m
, off
, icmp6len
, code
)
864 struct icmp6_hdr
*icmp6
;
865 struct ip6_hdr
*eip6
;
867 struct sockaddr_in6 icmp6src
, icmp6dst
;
869 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
870 icmp6stat
.icp6s_tooshort
++;
873 #ifndef PULLDOWN_TEST
874 IP6_EXTHDR_CHECK(m
, off
,
875 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
877 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
879 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
880 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
882 icmp6stat
.icp6s_tooshort
++;
886 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
888 /* Detect the upper level protocol */
890 void (*ctlfunc
) __P((int, struct sockaddr
*, void *));
891 u_int8_t nxt
= eip6
->ip6_nxt
;
892 int eoff
= off
+ sizeof(struct icmp6_hdr
) +
893 sizeof(struct ip6_hdr
);
894 struct ip6ctlparam ip6cp
;
895 struct in6_addr
*finaldst
= NULL
;
896 int icmp6type
= icmp6
->icmp6_type
;
898 struct ip6_rthdr
*rth
;
899 struct ip6_rthdr0
*rth0
;
902 while (1) { /* XXX: should avoid inf. loop explicitly? */
906 case IPPROTO_HOPOPTS
:
907 case IPPROTO_DSTOPTS
:
909 #ifndef PULLDOWN_TEST
910 IP6_EXTHDR_CHECK(m
, 0, eoff
+
911 sizeof(struct ip6_ext
),
913 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
916 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
919 icmp6stat
.icp6s_tooshort
++;
924 if (nxt
== IPPROTO_AH
)
925 eoff
+= (eh
->ip6e_len
+ 2) << 2;
927 eoff
+= (eh
->ip6e_len
+ 1) << 3;
930 case IPPROTO_ROUTING
:
932 * When the erroneous packet contains a
933 * routing header, we should examine the
934 * header to determine the final destination.
935 * Otherwise, we can't properly update
936 * information that depends on the final
937 * destination (e.g. path MTU).
939 #ifndef PULLDOWN_TEST
940 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
942 rth
= (struct ip6_rthdr
*)(mtod(m
, caddr_t
)
945 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
948 icmp6stat
.icp6s_tooshort
++;
952 rthlen
= (rth
->ip6r_len
+ 1) << 3;
954 * XXX: currently there is no
955 * officially defined type other
957 * Note that if the segment left field
958 * is 0, all intermediate hops must
961 if (rth
->ip6r_segleft
&&
962 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
965 #ifndef PULLDOWN_TEST
966 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
968 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
971 struct ip6_rthdr0
*, m
,
974 icmp6stat
.icp6s_tooshort
++;
978 /* just ignore a bogus header */
979 if ((rth0
->ip6r0_len
% 2) == 0 &&
980 (hops
= rth0
->ip6r0_len
/2))
981 finaldst
= (struct in6_addr
*)(rth0
+ 1) + (hops
- 1);
986 case IPPROTO_FRAGMENT
:
987 #ifndef PULLDOWN_TEST
988 IP6_EXTHDR_CHECK(m
, 0, eoff
+
989 sizeof(struct ip6_frag
),
991 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
994 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
997 icmp6stat
.icp6s_tooshort
++;
1002 * Data after a fragment header is meaningless
1003 * unless it is the first fragment, but
1004 * we'll go to the notify label for path MTU
1007 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
)
1010 eoff
+= sizeof(struct ip6_frag
);
1015 * This case includes ESP and the No Next
1016 * Header. In such cases going to the notify
1017 * label does not have any meaning
1018 * (i.e. ctlfunc will be NULL), but we go
1019 * anyway since we might have to update
1020 * path MTU information.
1026 #ifndef PULLDOWN_TEST
1027 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
1029 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
1030 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
1031 if (icmp6
== NULL
) {
1032 icmp6stat
.icp6s_tooshort
++;
1037 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1038 bzero(&icmp6dst
, sizeof(icmp6dst
));
1039 icmp6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
1040 icmp6dst
.sin6_family
= AF_INET6
;
1041 if (finaldst
== NULL
)
1042 icmp6dst
.sin6_addr
= eip6
->ip6_dst
;
1044 icmp6dst
.sin6_addr
= *finaldst
;
1045 icmp6dst
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1046 &icmp6dst
.sin6_addr
);
1047 #ifndef SCOPEDROUTING
1048 if (in6_embedscope(&icmp6dst
.sin6_addr
, &icmp6dst
,
1050 /* should be impossbile */
1052 "icmp6_notify_error: in6_embedscope failed\n"));
1058 * retrieve parameters from the inner IPv6 header, and convert
1059 * them into sockaddr structures.
1061 bzero(&icmp6src
, sizeof(icmp6src
));
1062 icmp6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1063 icmp6src
.sin6_family
= AF_INET6
;
1064 icmp6src
.sin6_addr
= eip6
->ip6_src
;
1065 icmp6src
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1066 &icmp6src
.sin6_addr
);
1067 #ifndef SCOPEDROUTING
1068 if (in6_embedscope(&icmp6src
.sin6_addr
, &icmp6src
,
1070 /* should be impossbile */
1072 "icmp6_notify_error: in6_embedscope failed\n"));
1076 icmp6src
.sin6_flowinfo
=
1077 (eip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
);
1079 if (finaldst
== NULL
)
1080 finaldst
= &eip6
->ip6_dst
;
1082 ip6cp
.ip6c_icmp6
= icmp6
;
1083 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1084 ip6cp
.ip6c_off
= eoff
;
1085 ip6cp
.ip6c_finaldst
= finaldst
;
1086 ip6cp
.ip6c_src
= &icmp6src
;
1087 ip6cp
.ip6c_nxt
= nxt
;
1089 if (icmp6type
== ICMP6_PACKET_TOO_BIG
) {
1090 notifymtu
= ntohl(icmp6
->icmp6_mtu
);
1091 ip6cp
.ip6c_cmdarg
= (void *)¬ifymtu
;
1092 icmp6_mtudisc_update(&ip6cp
, 1); /*XXX*/
1095 ctlfunc
= (void (*) __P((int, struct sockaddr
*, void *)))
1096 (ip6_protox
[nxt
]->pr_ctlinput
);
1098 (void) (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6dst
,
1110 icmp6_mtudisc_update(ip6cp
, validated
)
1111 struct ip6ctlparam
*ip6cp
;
1114 struct in6_addr
*dst
= ip6cp
->ip6c_finaldst
;
1115 struct icmp6_hdr
*icmp6
= ip6cp
->ip6c_icmp6
;
1116 struct mbuf
*m
= ip6cp
->ip6c_m
; /* will be necessary for scope issue */
1117 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1118 struct rtentry
*rt
= NULL
;
1119 struct sockaddr_in6 sin6
;
1124 bzero(&sin6
, sizeof(sin6
));
1125 sin6
.sin6_family
= PF_INET6
;
1126 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1127 sin6
.sin6_addr
= *dst
;
1128 /* XXX normally, this won't happen */
1129 if (IN6_IS_ADDR_LINKLOCAL(dst
)) {
1130 sin6
.sin6_addr
.s6_addr16
[1] =
1131 htons(m
->m_pkthdr
.rcvif
->if_index
);
1133 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1134 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0,
1135 RTF_CLONING
| RTF_PRCLONING
);
1137 if (rt
&& (rt
->rt_flags
& RTF_HOST
)
1138 && !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1139 if (mtu
< IPV6_MMTU
) {
1141 rt
->rt_rmx
.rmx_locks
|= RTV_MTU
;
1142 } else if (mtu
< rt
->rt_ifp
->if_mtu
&&
1143 rt
->rt_rmx
.rmx_mtu
> mtu
) {
1144 icmp6stat
.icp6s_pmtuchg
++;
1145 rt
->rt_rmx
.rmx_mtu
= mtu
;
1153 * Process a Node Information Query packet, based on
1154 * draft-ietf-ipngwg-icmp-name-lookups-07.
1156 * Spec incompatibilities:
1157 * - IPv6 Subject address handling
1158 * - IPv4 Subject address handling support missing
1159 * - Proxy reply (answer even if it's not for me)
1160 * - joins NI group address at in6_ifattach() time only, does not cope
1161 * with hostname changes by sethostname(3)
1163 #define hostnamelen strlen(hostname)
1164 static struct mbuf
*
1169 struct icmp6_nodeinfo
*ni6
, *nni6
;
1170 struct mbuf
*n
= NULL
;
1173 int replylen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1174 struct ni_reply_fqdn
*fqdn
;
1175 int addrs
; /* for NI_QTYPE_NODEADDR */
1176 struct ifnet
*ifp
= NULL
; /* for NI_QTYPE_NODEADDR */
1177 struct sockaddr_in6 sin6
; /* double meaning; ip6_dst and subjectaddr */
1178 struct sockaddr_in6 sin6_d
; /* XXX: we should retrieve this from m_aux */
1179 struct ip6_hdr
*ip6
;
1180 int oldfqdn
= 0; /* if 1, return pascal string (03 draft) */
1182 struct in6_ifaddr
*ia6
= NULL
;
1184 ip6
= mtod(m
, struct ip6_hdr
*);
1185 #ifndef PULLDOWN_TEST
1186 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1188 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1190 /* m is already reclaimed */
1196 * Validate IPv6 destination address.
1198 * The Responder must discard the Query without further processing
1199 * unless it is one of the Responder's unicast or anycast addresses, or
1200 * a link-local scope multicast address which the Responder has joined.
1201 * [icmp-name-lookups-07, Section 4.]
1203 bzero(&sin6
, sizeof(sin6
));
1204 sin6
.sin6_family
= AF_INET6
;
1205 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1206 bcopy(&ip6
->ip6_dst
, &sin6
.sin6_addr
, sizeof(sin6
.sin6_addr
));
1208 if ((ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr((struct sockaddr
*)&sin6
)) != NULL
) {
1209 /* unicast/anycast, fine */
1210 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1211 (icmp6_nodeinfo
& 4) == 0) {
1212 nd6log((LOG_DEBUG
, "ni6_input: ignore node info to "
1213 "a temporary address in %s:%d",
1214 __FILE__
, __LINE__
));
1217 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
))
1218 ; /* link-local multicast, fine */
1222 /* validate query Subject field. */
1223 qtype
= ntohs(ni6
->ni_qtype
);
1224 subjlen
= m
->m_pkthdr
.len
- off
- sizeof(struct icmp6_nodeinfo
);
1227 case NI_QTYPE_SUPTYPES
:
1229 if (ni6
->ni_code
== ICMP6_NI_SUBJ_FQDN
&& subjlen
== 0)
1233 case NI_QTYPE_NODEADDR
:
1234 switch (ni6
->ni_code
) {
1235 case ICMP6_NI_SUBJ_IPV6
:
1236 #if ICMP6_NI_SUBJ_IPV6 != 0
1240 * backward compatibility - try to accept 03 draft
1241 * format, where no Subject is present.
1243 if (qtype
== NI_QTYPE_FQDN
&& ni6
->ni_code
== 0 &&
1248 #if ICMP6_NI_SUBJ_IPV6 != 0
1249 if (ni6
->ni_code
!= ICMP6_NI_SUBJ_IPV6
)
1253 if (subjlen
!= sizeof(sin6
.sin6_addr
))
1257 * Validate Subject address.
1259 * Not sure what exactly "address belongs to the node"
1260 * means in the spec, is it just unicast, or what?
1262 * At this moment we consider Subject address as
1263 * "belong to the node" if the Subject address equals
1264 * to the IPv6 destination address; validation for
1265 * IPv6 destination address should have done enough
1268 * We do not do proxy at this moment.
1270 /* m_pulldown instead of copy? */
1271 m_copydata(m
, off
+ sizeof(struct icmp6_nodeinfo
),
1272 subjlen
, (caddr_t
)&sin6
.sin6_addr
);
1273 sin6
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1275 #ifndef SCOPEDROUTING
1276 in6_embedscope(&sin6
.sin6_addr
, &sin6
, NULL
, NULL
);
1278 bzero(&sin6_d
, sizeof(sin6_d
));
1279 sin6_d
.sin6_family
= AF_INET6
; /* not used, actually */
1280 sin6_d
.sin6_len
= sizeof(sin6_d
); /* ditto */
1281 sin6_d
.sin6_addr
= ip6
->ip6_dst
;
1282 sin6_d
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1284 #ifndef SCOPEDROUTING
1285 in6_embedscope(&sin6_d
.sin6_addr
, &sin6_d
, NULL
, NULL
);
1287 subj
= (char *)&sin6
;
1288 if (SA6_ARE_ADDR_EQUAL(&sin6
, &sin6_d
))
1292 * XXX if we are to allow other cases, we should really
1293 * be careful about scope here.
1294 * basically, we should disallow queries toward IPv6
1295 * destination X with subject Y, if scope(X) > scope(Y).
1296 * if we allow scope(X) > scope(Y), it will result in
1297 * information leakage across scope boundary.
1301 case ICMP6_NI_SUBJ_FQDN
:
1303 * Validate Subject name with gethostname(3).
1305 * The behavior may need some debate, since:
1306 * - we are not sure if the node has FQDN as
1307 * hostname (returned by gethostname(3)).
1308 * - the code does wildcard match for truncated names.
1309 * however, we are not sure if we want to perform
1310 * wildcard match, if gethostname(3) side has
1311 * truncated hostname.
1313 n
= ni6_nametodns(hostname
, hostnamelen
, 0);
1314 if (!n
|| n
->m_next
|| n
->m_len
== 0)
1316 IP6_EXTHDR_GET(subj
, char *, m
,
1317 off
+ sizeof(struct icmp6_nodeinfo
), subjlen
);
1320 if (!ni6_dnsmatch(subj
, subjlen
, mtod(n
, const char *),
1328 case ICMP6_NI_SUBJ_IPV4
: /* XXX: to be implemented? */
1335 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1338 if ((icmp6_nodeinfo
& 1) == 0)
1341 case NI_QTYPE_NODEADDR
:
1342 if ((icmp6_nodeinfo
& 2) == 0)
1347 /* guess reply length */
1350 break; /* no reply data */
1351 case NI_QTYPE_SUPTYPES
:
1352 replylen
+= sizeof(u_int32_t
);
1355 /* XXX will append an mbuf */
1356 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1358 case NI_QTYPE_NODEADDR
:
1359 addrs
= ni6_addrs(ni6
, m
, &ifp
, subj
);
1360 if ((replylen
+= addrs
* (sizeof(struct in6_addr
) +
1361 sizeof(u_int32_t
))) > MCLBYTES
)
1362 replylen
= MCLBYTES
; /* XXX: will truncate pkt later */
1366 * XXX: We must return a reply with the ICMP6 code
1367 * `unknown Qtype' in this case. However we regard the case
1368 * as an FQDN query for backward compatibility.
1369 * Older versions set a random value to this field,
1370 * so it rarely varies in the defined qtypes.
1371 * But the mechanism is not reliable...
1372 * maybe we should obsolete older versions.
1374 qtype
= NI_QTYPE_FQDN
;
1375 /* XXX will append an mbuf */
1376 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1381 /* allocate an mbuf to reply. */
1382 MGETHDR(n
, M_DONTWAIT
, m
->m_type
);
1387 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1388 if (replylen
> MHLEN
) {
1389 if (replylen
> MCLBYTES
) {
1391 * XXX: should we try to allocate more? But MCLBYTES
1392 * is probably much larger than IPV6_MMTU...
1396 MCLGET(n
, M_DONTWAIT
);
1397 if ((n
->m_flags
& M_EXT
) == 0) {
1401 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
1403 /* copy mbuf header and IPv6 + Node Information base headers */
1404 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), sizeof(struct ip6_hdr
));
1405 nni6
= (struct icmp6_nodeinfo
*)(mtod(n
, struct ip6_hdr
*) + 1);
1406 bcopy((caddr_t
)ni6
, (caddr_t
)nni6
, sizeof(struct icmp6_nodeinfo
));
1408 /* qtype dependent procedure */
1411 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1414 case NI_QTYPE_SUPTYPES
:
1417 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1418 nni6
->ni_flags
= htons(0x0000); /* raw bitmap */
1419 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1420 v
= (u_int32_t
)htonl(0x0000000f);
1421 bcopy(&v
, nni6
+ 1, sizeof(u_int32_t
));
1425 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1426 fqdn
= (struct ni_reply_fqdn
*)(mtod(n
, caddr_t
) +
1427 sizeof(struct ip6_hdr
) +
1428 sizeof(struct icmp6_nodeinfo
));
1429 nni6
->ni_flags
= 0; /* XXX: meaningless TTL */
1430 fqdn
->ni_fqdn_ttl
= 0; /* ditto. */
1432 * XXX do we really have FQDN in variable "hostname"?
1434 n
->m_next
= ni6_nametodns(hostname
, hostnamelen
, oldfqdn
);
1435 if (n
->m_next
== NULL
)
1437 /* XXX we assume that n->m_next is not a chain */
1438 if (n
->m_next
->m_next
!= NULL
)
1440 n
->m_pkthdr
.len
+= n
->m_next
->m_len
;
1442 case NI_QTYPE_NODEADDR
:
1446 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1447 n
->m_pkthdr
.len
= n
->m_len
=
1448 sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1449 lenlim
= M_TRAILINGSPACE(n
);
1450 copied
= ni6_store_addrs(ni6
, nni6
, ifp
, lenlim
);
1451 /* XXX: reset mbuf length */
1452 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
1453 sizeof(struct icmp6_nodeinfo
) + copied
;
1457 break; /* XXX impossible! */
1460 nni6
->ni_type
= ICMP6_NI_REPLY
;
1473 * make a mbuf with DNS-encoded string. no compression support.
1475 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1476 * treated as truncated name (two \0 at the end). this is a wild guess.
1478 static struct mbuf
*
1479 ni6_nametodns(name
, namelen
, old
)
1482 int old
; /* return pascal string if non-zero */
1494 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1495 MGET(m
, M_DONTWAIT
, MT_DATA
);
1496 if (m
&& len
> MLEN
) {
1497 MCLGET(m
, M_DONTWAIT
);
1498 if ((m
->m_flags
& M_EXT
) == 0)
1507 *mtod(m
, char *) = namelen
;
1508 bcopy(name
, mtod(m
, char *) + 1, namelen
);
1512 cp
= mtod(m
, char *);
1513 ep
= mtod(m
, char *) + M_TRAILINGSPACE(m
);
1515 /* if not certain about my name, return empty buffer */
1520 * guess if it looks like shortened hostname, or FQDN.
1521 * shortened hostname needs two trailing "\0".
1524 for (p
= name
; p
< name
+ namelen
; p
++) {
1525 if (*p
&& *p
== '.')
1534 while (cp
< ep
&& p
< name
+ namelen
) {
1536 for (q
= p
; q
< name
+ namelen
&& *q
&& *q
!= '.'; q
++)
1538 /* result does not fit into mbuf */
1539 if (cp
+ i
+ 1 >= ep
)
1542 * DNS label length restriction, RFC1035 page 8.
1543 * "i == 0" case is included here to avoid returning
1544 * 0-length label on "foo..bar".
1546 if (i
<= 0 || i
>= 64)
1552 if (p
< name
+ namelen
&& *p
== '.')
1556 if (cp
+ nterm
>= ep
)
1560 m
->m_len
= cp
- mtod(m
, char *);
1564 panic("should not reach here");
1574 * check if two DNS-encoded string matches. takes care of truncated
1575 * form (with \0\0 at the end). no compression support.
1576 * XXX upper/lowercase match (see RFC2065)
1579 ni6_dnsmatch(a
, alen
, b
, blen
)
1585 const char *a0
, *b0
;
1588 /* simplest case - need validation? */
1589 if (alen
== blen
&& bcmp(a
, b
, alen
) == 0)
1595 /* termination is mandatory */
1596 if (alen
< 2 || blen
< 2)
1598 if (a0
[alen
- 1] != '\0' || b0
[blen
- 1] != '\0')
1603 while (a
- a0
< alen
&& b
- b0
< blen
) {
1604 if (a
- a0
+ 1 > alen
|| b
- b0
+ 1 > blen
)
1607 if ((signed char)a
[0] < 0 || (signed char)b
[0] < 0)
1609 /* we don't support compression yet */
1610 if (a
[0] >= 64 || b
[0] >= 64)
1613 /* truncated case */
1614 if (a
[0] == 0 && a
- a0
== alen
- 1)
1616 if (b
[0] == 0 && b
- b0
== blen
- 1)
1618 if (a
[0] == 0 || b
[0] == 0)
1624 if (a
- a0
+ 1 + l
> alen
|| b
- b0
+ 1 + l
> blen
)
1626 if (bcmp(a
+ 1, b
+ 1, l
) != 0)
1633 if (a
- a0
== alen
&& b
- b0
== blen
)
1640 * calculate the number of addresses to be returned in the node info reply.
1643 ni6_addrs(ni6
, m
, ifpp
, subj
)
1644 struct icmp6_nodeinfo
*ni6
;
1646 struct ifnet
**ifpp
;
1650 struct in6_ifaddr
*ifa6
;
1652 struct sockaddr_in6
*subj_ip6
= NULL
; /* XXX pedant */
1653 int addrs
= 0, addrsofif
, iffound
= 0;
1654 int niflags
= ni6
->ni_flags
;
1656 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0) {
1657 switch (ni6
->ni_code
) {
1658 case ICMP6_NI_SUBJ_IPV6
:
1659 if (subj
== NULL
) /* must be impossible... */
1661 subj_ip6
= (struct sockaddr_in6
*)subj
;
1665 * XXX: we only support IPv6 subject address for
1672 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
1675 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1677 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1679 ifa6
= (struct in6_ifaddr
*)ifa
;
1681 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0 &&
1682 IN6_ARE_ADDR_EQUAL(&subj_ip6
->sin6_addr
,
1683 &ifa6
->ia_addr
.sin6_addr
))
1687 * IPv4-mapped addresses can only be returned by a
1688 * Node Information proxy, since they represent
1689 * addresses of IPv4-only nodes, which perforce do
1690 * not implement this protocol.
1691 * [icmp-name-lookups-07, Section 5.4]
1692 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1693 * this function at this moment.
1696 /* What do we have to do about ::1? */
1697 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1698 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1699 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1702 case IPV6_ADDR_SCOPE_SITELOCAL
:
1703 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1706 case IPV6_ADDR_SCOPE_GLOBAL
:
1707 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1715 * check if anycast is okay.
1716 * XXX: just experimental. not in the spec.
1718 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1719 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1720 continue; /* we need only unicast addresses */
1721 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1722 (icmp6_nodeinfo
& 4) == 0) {
1725 addrsofif
++; /* count the address */
1739 ni6_store_addrs(ni6
, nni6
, ifp0
, resid
)
1740 struct icmp6_nodeinfo
*ni6
, *nni6
;
1744 struct ifnet
*ifp
= ifp0
? ifp0
: TAILQ_FIRST(&ifnet
);
1745 struct in6_ifaddr
*ifa6
;
1747 struct ifnet
*ifp_dep
= NULL
;
1748 int copied
= 0, allow_deprecated
= 0;
1749 u_char
*cp
= (u_char
*)(nni6
+ 1);
1750 int niflags
= ni6
->ni_flags
;
1753 if (ifp0
== NULL
&& !(niflags
& NI_NODEADDR_FLAG_ALL
))
1754 return(0); /* needless to copy */
1758 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
1760 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1761 ifa
= ifa
->ifa_list
.tqe_next
)
1763 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1765 ifa6
= (struct in6_ifaddr
*)ifa
;
1767 if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0 &&
1768 allow_deprecated
== 0) {
1770 * prefererred address should be put before
1771 * deprecated addresses.
1774 /* record the interface for later search */
1775 if (ifp_dep
== NULL
)
1780 else if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) == 0 &&
1781 allow_deprecated
!= 0)
1782 continue; /* we now collect deprecated addrs */
1784 /* What do we have to do about ::1? */
1785 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1786 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1787 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1790 case IPV6_ADDR_SCOPE_SITELOCAL
:
1791 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1794 case IPV6_ADDR_SCOPE_GLOBAL
:
1795 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1803 * check if anycast is okay.
1804 * XXX: just experimental. not in the spec.
1806 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1807 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1809 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1810 (icmp6_nodeinfo
& 4) == 0) {
1814 /* now we can copy the address */
1815 if (resid
< sizeof(struct in6_addr
) +
1816 sizeof(u_int32_t
)) {
1818 * We give up much more copy.
1819 * Set the truncate flag and return.
1822 NI_NODEADDR_FLAG_TRUNCATE
;
1827 * Set the TTL of the address.
1828 * The TTL value should be one of the following
1829 * according to the specification:
1831 * 1. The remaining lifetime of a DHCP lease on the
1833 * 2. The remaining Valid Lifetime of a prefix from
1834 * which the address was derived through Stateless
1835 * Autoconfiguration.
1837 * Note that we currently do not support stateful
1838 * address configuration by DHCPv6, so the former
1839 * case can't happen.
1841 if (ifa6
->ia6_lifetime
.ia6t_expire
== 0)
1842 ltime
= ND6_INFINITE_LIFETIME
;
1844 if (ifa6
->ia6_lifetime
.ia6t_expire
>
1846 ltime
= htonl(ifa6
->ia6_lifetime
.ia6t_expire
- time_second
);
1851 bcopy(<ime
, cp
, sizeof(u_int32_t
));
1852 cp
+= sizeof(u_int32_t
);
1854 /* copy the address itself */
1855 bcopy(&ifa6
->ia_addr
.sin6_addr
, cp
,
1856 sizeof(struct in6_addr
));
1857 /* XXX: KAME link-local hack; remove ifindex */
1858 if (IN6_IS_ADDR_LINKLOCAL(&ifa6
->ia_addr
.sin6_addr
))
1859 ((struct in6_addr
*)cp
)->s6_addr16
[1] = 0;
1860 cp
+= sizeof(struct in6_addr
);
1862 resid
-= (sizeof(struct in6_addr
) + sizeof(u_int32_t
));
1863 copied
+= (sizeof(struct in6_addr
) +
1866 if (ifp0
) /* we need search only on the specified IF */
1870 if (allow_deprecated
== 0 && ifp_dep
!= NULL
) {
1872 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 rip6src
;
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
;
1907 bzero(&rip6src
, sizeof(rip6src
));
1908 rip6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1909 rip6src
.sin6_family
= AF_INET6
;
1910 /* KAME hack: recover scopeid */
1911 (void)in6_recoverscope(&rip6src
, &ip6
->ip6_src
, m
->m_pkthdr
.rcvif
);
1913 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
1915 if ((in6p
->inp_vflag
& INP_IPV6
) == 0)
1918 if (!(in6p
->in6p_flags
& INP_IPV6
))
1921 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
)
1923 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
1924 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
))
1926 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
1927 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
))
1929 if (in6p
->in6p_icmp6filt
1930 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
1931 in6p
->in6p_icmp6filt
))
1935 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
1936 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1937 ip6_savecontrol(last
, &opts
, ip6
, n
);
1938 /* strip intermediate headers */
1940 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1941 (struct sockaddr
*)&rip6src
,
1943 /* should notify about lost packet */
1949 sorwakeup(last
->in6p_socket
);
1956 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1957 ip6_savecontrol(last
, &opts
, ip6
, m
);
1958 /* strip intermediate headers */
1960 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1961 (struct sockaddr
*)&rip6src
, m
, opts
) == 0) {
1966 sorwakeup(last
->in6p_socket
);
1969 ip6stat
.ip6s_delivered
--;
1971 return IPPROTO_DONE
;
1975 * Reflect the ip6 packet back to the source.
1976 * OFF points to the icmp6 header, counted from the top of the mbuf.
1979 icmp6_reflect(m
, off
)
1983 struct ip6_hdr
*ip6
;
1984 struct icmp6_hdr
*icmp6
;
1985 struct in6_ifaddr
*ia
;
1986 struct in6_addr t
, *src
= 0;
1989 struct ifnet
*outif
= NULL
;
1990 struct sockaddr_in6 sa6_src
, sa6_dst
;
1991 #ifdef COMPAT_RFC1885
1992 int mtu
= IPV6_MMTU
;
1993 struct sockaddr_in6
*sin6
= &icmp6_reflect_rt
.ro_dst
;
1996 /* too short to reflect */
1997 if (off
< sizeof(struct ip6_hdr
)) {
1999 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2000 (u_long
)off
, (u_long
)sizeof(struct ip6_hdr
),
2001 __FILE__
, __LINE__
));
2006 * If there are extra headers between IPv6 and ICMPv6, strip
2007 * off that header first.
2010 if (sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) > MHLEN
)
2011 panic("assumption failed in icmp6_reflect");
2013 if (off
> sizeof(struct ip6_hdr
)) {
2015 struct ip6_hdr nip6
;
2017 l
= off
- sizeof(struct ip6_hdr
);
2018 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
2020 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2022 if ((m
= m_pullup(m
, l
)) == NULL
)
2025 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
2026 } else /* off == sizeof(struct ip6_hdr) */ {
2028 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2030 if ((m
= m_pullup(m
, l
)) == NULL
)
2034 plen
= m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2035 ip6
= mtod(m
, struct ip6_hdr
*);
2036 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2037 icmp6
= (struct icmp6_hdr
*)(ip6
+ 1);
2038 type
= icmp6
->icmp6_type
; /* keep type for statistics */
2039 code
= icmp6
->icmp6_code
; /* ditto. */
2043 * ip6_input() drops a packet if its src is multicast.
2044 * So, the src is never multicast.
2046 ip6
->ip6_dst
= ip6
->ip6_src
;
2049 * XXX: make sure to embed scope zone information, using
2050 * already embedded IDs or the received interface (if any).
2051 * Note that rcvif may be NULL.
2052 * TODO: scoped routing case (XXX).
2054 bzero(&sa6_src
, sizeof(sa6_src
));
2055 sa6_src
.sin6_family
= AF_INET6
;
2056 sa6_src
.sin6_len
= sizeof(sa6_src
);
2057 sa6_src
.sin6_addr
= ip6
->ip6_dst
;
2058 in6_recoverscope(&sa6_src
, &ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
);
2059 in6_embedscope(&ip6
->ip6_dst
, &sa6_src
, NULL
, NULL
);
2060 bzero(&sa6_dst
, sizeof(sa6_dst
));
2061 sa6_dst
.sin6_family
= AF_INET6
;
2062 sa6_dst
.sin6_len
= sizeof(sa6_dst
);
2063 sa6_dst
.sin6_addr
= t
;
2064 in6_recoverscope(&sa6_dst
, &t
, m
->m_pkthdr
.rcvif
);
2065 in6_embedscope(&t
, &sa6_dst
, NULL
, NULL
);
2067 #ifdef COMPAT_RFC1885
2070 * RFC 1885 requires that echo reply should be truncated if it
2071 * does not fit in with (return) path MTU, but the description was
2072 * removed in the new spec.
2074 if (icmp6_reflect_rt
.ro_rt
== 0 ||
2075 ! (IN6_ARE_ADDR_EQUAL(&sin6
->sin6_addr
, &ip6
->ip6_dst
))) {
2076 if (icmp6_reflect_rt
.ro_rt
) {
2077 rtfree(icmp6_reflect_rt
.ro_rt
);
2078 icmp6_reflect_rt
.ro_rt
= 0;
2080 bzero(sin6
, sizeof(*sin6
));
2081 sin6
->sin6_family
= PF_INET6
;
2082 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2083 sin6
->sin6_addr
= ip6
->ip6_dst
;
2085 rtalloc_ign((struct route
*)&icmp6_reflect_rt
.ro_rt
,
2089 if (icmp6_reflect_rt
.ro_rt
== 0)
2092 if ((icmp6_reflect_rt
.ro_rt
->rt_flags
& RTF_HOST
)
2093 && mtu
< icmp6_reflect_rt
.ro_rt
->rt_ifp
->if_mtu
)
2094 mtu
= icmp6_reflect_rt
.ro_rt
->rt_rmx
.rmx_mtu
;
2096 if (mtu
< m
->m_pkthdr
.len
) {
2097 plen
-= (m
->m_pkthdr
.len
- mtu
);
2098 m_adj(m
, mtu
- m
->m_pkthdr
.len
);
2102 * If the incoming packet was addressed directly to us(i.e. unicast),
2103 * use dst as the src for the reply.
2104 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2105 * (for example) when we encounter an error while forwarding procedure
2106 * destined to a duplicated address of ours.
2108 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
)
2109 if (IN6_ARE_ADDR_EQUAL(&t
, &ia
->ia_addr
.sin6_addr
) &&
2110 (ia
->ia6_flags
& (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
)) == 0) {
2114 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) && (m
->m_flags
& M_LOOP
)) {
2116 * This is the case if the dst is our link-local address
2117 * and the sender is also ourseleves.
2124 struct route_in6 ro
;
2127 * This case matches to multicasts, our anycast, or unicasts
2128 * that we do not own. Select a source address based on the
2129 * source address of the erroneous packet.
2131 bzero(&ro
, sizeof(ro
));
2132 src
= in6_selectsrc(&sa6_src
, NULL
, NULL
, &ro
, NULL
, &e
);
2134 rtfree(ro
.ro_rt
); /* XXX: we could use this */
2137 "icmp6_reflect: source can't be determined: "
2138 "dst=%s, error=%d\n",
2139 ip6_sprintf(&sa6_src
.sin6_addr
), e
));
2144 ip6
->ip6_src
= *src
;
2147 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2148 ip6
->ip6_vfc
|= IPV6_VERSION
;
2149 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2150 if (m
->m_pkthdr
.rcvif
) {
2151 /* XXX: This may not be the outgoing interface */
2152 ip6
->ip6_hlim
= nd_ifinfo
[m
->m_pkthdr
.rcvif
->if_index
].chlim
;
2154 ip6
->ip6_hlim
= ip6_defhlim
;
2156 icmp6
->icmp6_cksum
= 0;
2157 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
2158 sizeof(struct ip6_hdr
), plen
);
2161 * XXX option handling
2164 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
2166 /* Don't lookup socket */
2167 if (ipsec_bypass
== 0)
2168 (void)ipsec_setsocket(m
, NULL
);
2171 #ifdef COMPAT_RFC1885
2172 ip6_output(m
, NULL
, &icmp6_reflect_rt
, 0, NULL
, &outif
);
2174 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
);
2177 icmp6_ifoutstat_inc(outif
, type
, code
);
2194 icmp6_redirect_diag(src6
, dst6
, tgt6
)
2195 struct in6_addr
*src6
;
2196 struct in6_addr
*dst6
;
2197 struct in6_addr
*tgt6
;
2199 static char buf
[1024];
2200 snprintf(buf
, sizeof(buf
), "(src=%s dst=%s tgt=%s)",
2201 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
2206 icmp6_redirect_input(m
, off
)
2210 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2211 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2212 struct nd_redirect
*nd_rd
;
2213 int icmp6len
= ntohs(ip6
->ip6_plen
);
2214 char *lladdr
= NULL
;
2216 u_char
*redirhdr
= NULL
;
2217 int redirhdrlen
= 0;
2218 struct rtentry
*rt
= NULL
;
2221 struct in6_addr src6
= ip6
->ip6_src
;
2222 struct in6_addr redtgt6
;
2223 struct in6_addr reddst6
;
2224 union nd_opts ndopts
;
2229 /* XXX if we are router, we don't update route by icmp6 redirect */
2232 if (!icmp6_rediraccept
)
2235 #ifndef PULLDOWN_TEST
2236 IP6_EXTHDR_CHECK(m
, off
, icmp6len
,);
2237 nd_rd
= (struct nd_redirect
*)((caddr_t
)ip6
+ off
);
2239 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
2240 if (nd_rd
== NULL
) {
2241 icmp6stat
.icp6s_tooshort
++;
2245 redtgt6
= nd_rd
->nd_rd_target
;
2246 reddst6
= nd_rd
->nd_rd_dst
;
2248 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2249 redtgt6
.s6_addr16
[1] = htons(ifp
->if_index
);
2250 if (IN6_IS_ADDR_LINKLOCAL(&reddst6
))
2251 reddst6
.s6_addr16
[1] = htons(ifp
->if_index
);
2254 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
2256 "ICMP6 redirect sent from %s rejected; "
2257 "must be from linklocal\n", ip6_sprintf(&src6
)));
2260 if (ip6
->ip6_hlim
!= 255) {
2262 "ICMP6 redirect sent from %s rejected; "
2263 "hlim=%d (must be 255)\n",
2264 ip6_sprintf(&src6
), ip6
->ip6_hlim
));
2268 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2269 struct sockaddr_in6 sin6
;
2270 struct in6_addr
*gw6
;
2272 bzero(&sin6
, sizeof(sin6
));
2273 sin6
.sin6_family
= AF_INET6
;
2274 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2275 bcopy(&reddst6
, &sin6
.sin6_addr
, sizeof(reddst6
));
2276 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0UL);
2278 if (rt
->rt_gateway
== NULL
||
2279 rt
->rt_gateway
->sa_family
!= AF_INET6
) {
2281 "ICMP6 redirect rejected; no route "
2282 "with inet6 gateway found for redirect dst: %s\n",
2283 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2288 gw6
= &(((struct sockaddr_in6
*)rt
->rt_gateway
)->sin6_addr
);
2289 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
2291 "ICMP6 redirect rejected; "
2292 "not equal to gw-for-src=%s (must be same): "
2295 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2301 "ICMP6 redirect rejected; "
2302 "no route found for redirect dst: %s\n",
2303 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2309 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
2311 "ICMP6 redirect rejected; "
2312 "redirect dst must be unicast: %s\n",
2313 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2317 is_router
= is_onlink
= 0;
2318 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2319 is_router
= 1; /* router case */
2320 if (bcmp(&redtgt6
, &reddst6
, sizeof(redtgt6
)) == 0)
2321 is_onlink
= 1; /* on-link destination case */
2322 if (!is_router
&& !is_onlink
) {
2324 "ICMP6 redirect rejected; "
2325 "neither router case nor onlink case: %s\n",
2326 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2329 /* validation passed */
2331 icmp6len
-= sizeof(*nd_rd
);
2332 nd6_option_init(nd_rd
+ 1, icmp6len
, &ndopts
);
2333 if (nd6_options(&ndopts
) < 0) {
2334 nd6log((LOG_INFO
, "icmp6_redirect_input: "
2335 "invalid ND option, rejected: %s\n",
2336 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2337 /* nd6_options have incremented stats */
2341 if (ndopts
.nd_opts_tgt_lladdr
) {
2342 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
2343 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
2346 if (ndopts
.nd_opts_rh
) {
2347 redirhdrlen
= ndopts
.nd_opts_rh
->nd_opt_rh_len
;
2348 redirhdr
= (u_char
*)(ndopts
.nd_opts_rh
+ 1); /* xxx */
2351 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
2353 "icmp6_redirect_input: lladdrlen mismatch for %s "
2354 "(if %d, icmp6 packet %d): %s\n",
2355 ip6_sprintf(&redtgt6
), ifp
->if_addrlen
, lladdrlen
- 2,
2356 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2361 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
2362 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
2364 if (!is_onlink
) { /* better router case. perform rtredirect. */
2365 /* perform rtredirect */
2366 struct sockaddr_in6 sdst
;
2367 struct sockaddr_in6 sgw
;
2368 struct sockaddr_in6 ssrc
;
2370 bzero(&sdst
, sizeof(sdst
));
2371 bzero(&sgw
, sizeof(sgw
));
2372 bzero(&ssrc
, sizeof(ssrc
));
2373 sdst
.sin6_family
= sgw
.sin6_family
= ssrc
.sin6_family
= AF_INET6
;
2374 sdst
.sin6_len
= sgw
.sin6_len
= ssrc
.sin6_len
=
2375 sizeof(struct sockaddr_in6
);
2376 bcopy(&redtgt6
, &sgw
.sin6_addr
, sizeof(struct in6_addr
));
2377 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2378 bcopy(&src6
, &ssrc
.sin6_addr
, sizeof(struct in6_addr
));
2379 rtredirect((struct sockaddr
*)&sdst
, (struct sockaddr
*)&sgw
,
2380 (struct sockaddr
*)NULL
, RTF_GATEWAY
| RTF_HOST
,
2381 (struct sockaddr
*)&ssrc
,
2382 (struct rtentry
**)NULL
);
2384 /* finally update cached route in each socket via pfctlinput */
2386 struct sockaddr_in6 sdst
;
2388 bzero(&sdst
, sizeof(sdst
));
2389 sdst
.sin6_family
= AF_INET6
;
2390 sdst
.sin6_len
= sizeof(struct sockaddr_in6
);
2391 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2392 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
2394 key_sa_routechange((struct sockaddr
*)&sdst
);
2403 icmp6stat
.icp6s_badredirect
++;
2408 icmp6_redirect_output(m0
, rt
)
2412 struct ifnet
*ifp
; /* my outgoing interface */
2413 struct in6_addr
*ifp_ll6
;
2414 struct in6_addr
*router_ll6
;
2415 struct ip6_hdr
*sip6
; /* m0 as struct ip6_hdr */
2416 struct mbuf
*m
= NULL
; /* newly allocated one */
2417 struct ip6_hdr
*ip6
; /* m as struct ip6_hdr */
2418 struct nd_redirect
*nd_rd
;
2421 struct ifnet
*outif
= NULL
;
2422 struct sockaddr_in6 src_sa
;
2424 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, ND_REDIRECT
, 0);
2426 /* if we are not router, we don't send icmp6 redirect */
2427 if (!ip6_forwarding
|| ip6_accept_rtadv
)
2431 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
))
2436 * the source address must identify a neighbor, and
2437 * the destination address must not be a multicast address
2438 * [RFC 2461, sec 8.2]
2440 sip6
= mtod(m0
, struct ip6_hdr
*);
2441 bzero(&src_sa
, sizeof(src_sa
));
2442 src_sa
.sin6_family
= AF_INET6
;
2443 src_sa
.sin6_len
= sizeof(src_sa
);
2444 src_sa
.sin6_addr
= sip6
->ip6_src
;
2445 /* we don't currently use sin6_scope_id, but eventually use it */
2446 src_sa
.sin6_scope_id
= in6_addr2scopeid(ifp
, &sip6
->ip6_src
);
2447 if (nd6_is_addr_neighbor(&src_sa
, ifp
) == 0)
2449 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
))
2450 goto fail
; /* what should we do here? */
2453 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0))
2457 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2458 * we almost always ask for an mbuf cluster for simplicity.
2459 * (MHLEN < IPV6_MMTU is almost always true)
2461 #if IPV6_MMTU >= MCLBYTES
2462 # error assumption failed about IPV6_MMTU and MCLBYTES
2464 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
2465 if (m
&& IPV6_MMTU
>= MHLEN
)
2466 MCLGET(m
, M_DONTWAIT
);
2469 m
->m_pkthdr
.rcvif
= NULL
;
2471 maxlen
= M_TRAILINGSPACE(m
);
2472 maxlen
= min(IPV6_MMTU
, maxlen
);
2473 /* just for safety */
2474 if (maxlen
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) +
2475 ((sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7)) {
2480 /* get ip6 linklocal address for ifp(my outgoing interface). */
2481 struct in6_ifaddr
*ia
;
2482 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
2484 IN6_IFF_ANYCAST
)) == NULL
)
2486 ifp_ll6
= &ia
->ia_addr
.sin6_addr
;
2489 /* get ip6 linklocal address for the router. */
2490 if (rt
->rt_gateway
&& (rt
->rt_flags
& RTF_GATEWAY
)) {
2491 struct sockaddr_in6
*sin6
;
2492 sin6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
2493 router_ll6
= &sin6
->sin6_addr
;
2494 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6
))
2495 router_ll6
= (struct in6_addr
*)NULL
;
2497 router_ll6
= (struct in6_addr
*)NULL
;
2500 ip6
= mtod(m
, struct ip6_hdr
*);
2502 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2503 ip6
->ip6_vfc
|= IPV6_VERSION
;
2504 /* ip6->ip6_plen will be set later */
2505 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2506 ip6
->ip6_hlim
= 255;
2507 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2508 bcopy(ifp_ll6
, &ip6
->ip6_src
, sizeof(struct in6_addr
));
2509 bcopy(&sip6
->ip6_src
, &ip6
->ip6_dst
, sizeof(struct in6_addr
));
2512 nd_rd
= (struct nd_redirect
*)(ip6
+ 1);
2513 nd_rd
->nd_rd_type
= ND_REDIRECT
;
2514 nd_rd
->nd_rd_code
= 0;
2515 nd_rd
->nd_rd_reserved
= 0;
2516 if (rt
->rt_flags
& RTF_GATEWAY
) {
2518 * nd_rd->nd_rd_target must be a link-local address in
2519 * better router cases.
2523 bcopy(router_ll6
, &nd_rd
->nd_rd_target
,
2524 sizeof(nd_rd
->nd_rd_target
));
2525 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2526 sizeof(nd_rd
->nd_rd_dst
));
2528 /* make sure redtgt == reddst */
2529 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_target
,
2530 sizeof(nd_rd
->nd_rd_target
));
2531 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2532 sizeof(nd_rd
->nd_rd_dst
));
2535 p
= (u_char
*)(nd_rd
+ 1);
2541 /* target lladdr option */
2542 struct rtentry
*rt_router
= NULL
;
2544 struct sockaddr_dl
*sdl
;
2545 struct nd_opt_hdr
*nd_opt
;
2548 rt_router
= nd6_lookup(router_ll6
, 0, ifp
);
2551 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2552 len
= (len
+ 7) & ~7; /*round by 8*/
2554 if (len
+ (p
- (u_char
*)ip6
) > maxlen
)
2556 if (!(rt_router
->rt_flags
& RTF_GATEWAY
) &&
2557 (rt_router
->rt_flags
& RTF_LLINFO
) &&
2558 (rt_router
->rt_gateway
->sa_family
== AF_LINK
) &&
2559 (sdl
= (struct sockaddr_dl
*)rt_router
->rt_gateway
) &&
2561 nd_opt
= (struct nd_opt_hdr
*)p
;
2562 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
2563 nd_opt
->nd_opt_len
= len
>> 3;
2564 lladdr
= (char *)(nd_opt
+ 1);
2565 bcopy(LLADDR(sdl
), lladdr
, ifp
->if_addrlen
);
2571 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2573 /* just to be safe */
2574 #ifdef M_DECRYPTED /*not openbsd*/
2575 if (m0
->m_flags
& M_DECRYPTED
)
2578 if (p
- (u_char
*)ip6
> maxlen
)
2582 /* redirected header option */
2584 struct nd_opt_rd_hdr
*nd_opt_rh
;
2587 * compute the maximum size for icmp6 redirect header option.
2588 * XXX room for auth header?
2590 len
= maxlen
- (p
- (u_char
*)ip6
);
2593 /* This is just for simplicity. */
2594 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2596 m_freem(m0
->m_next
);
2599 m0
->m_pkthdr
.len
= m0
->m_len
;
2603 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2604 * about padding/truncate rule for the original IP packet.
2605 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2606 * - "attach as much as possible" is the goal
2607 * - pad if not aligned (original size can be guessed by original
2609 * Following code adds the padding if it is simple enough,
2610 * and truncates if not.
2612 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
)
2613 panic("assumption failed in %s:%d\n", __FILE__
, __LINE__
);
2615 if (len
- sizeof(*nd_opt_rh
) < m0
->m_pkthdr
.len
) {
2616 /* not enough room, truncate */
2617 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2619 /* enough room, pad or truncate */
2622 extra
= m0
->m_pkthdr
.len
% 8;
2624 /* pad if easy enough, truncate if not */
2625 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2627 m0
->m_len
+= (8 - extra
);
2628 m0
->m_pkthdr
.len
+= (8 - extra
);
2631 m0
->m_pkthdr
.len
-= extra
;
2635 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2636 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2639 nd_opt_rh
= (struct nd_opt_rd_hdr
*)p
;
2640 bzero(nd_opt_rh
, sizeof(*nd_opt_rh
));
2641 nd_opt_rh
->nd_opt_rh_type
= ND_OPT_REDIRECTED_HEADER
;
2642 nd_opt_rh
->nd_opt_rh_len
= len
>> 3;
2643 p
+= sizeof(*nd_opt_rh
);
2644 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2646 /* connect m0 to m */
2648 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
2652 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_src
))
2653 sip6
->ip6_src
.s6_addr16
[1] = 0;
2654 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_dst
))
2655 sip6
->ip6_dst
.s6_addr16
[1] = 0;
2657 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_src
))
2658 ip6
->ip6_src
.s6_addr16
[1] = 0;
2659 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_dst
))
2660 ip6
->ip6_dst
.s6_addr16
[1] = 0;
2662 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_target
))
2663 nd_rd
->nd_rd_target
.s6_addr16
[1] = 0;
2664 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_dst
))
2665 nd_rd
->nd_rd_dst
.s6_addr16
[1] = 0;
2667 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
2669 nd_rd
->nd_rd_cksum
= 0;
2671 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
2673 /* send the packet to outside... */
2675 /* Don't lookup socket */
2676 if (ipsec_bypass
== 0)
2677 (void)ipsec_setsocket(m
, NULL
);
2679 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
);
2681 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
2682 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
2684 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
2696 #define sotoin6pcb sotoinpcb
2697 #define in6pcb inpcb
2698 #define in6p_icmp6filt inp_icmp6filt
2701 * ICMPv6 socket option processing.
2704 icmp6_ctloutput(so
, sopt
)
2706 struct sockopt
*sopt
;
2710 struct inpcb
*inp
= sotoinpcb(so
);
2711 int level
, op
, optname
;
2714 level
= sopt
->sopt_level
;
2715 op
= sopt
->sopt_dir
;
2716 optname
= sopt
->sopt_name
;
2717 optlen
= sopt
->sopt_valsize
;
2719 level
= op
= optname
= optlen
= 0;
2721 if (level
!= IPPROTO_ICMPV6
) {
2730 struct icmp6_filter
*p
;
2732 if (optlen
!= sizeof(*p
)) {
2736 if (inp
->in6p_icmp6filt
== NULL
) {
2740 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
2746 error
= ENOPROTOOPT
;
2755 if (inp
->in6p_icmp6filt
== NULL
) {
2759 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
2760 sizeof(struct icmp6_filter
));
2765 error
= ENOPROTOOPT
;
2776 #undef in6p_icmp6filt
2779 #ifndef HAVE_PPSRATECHECK
2781 #define timersub(tvp, uvp, vvp) \
2783 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2784 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2785 if ((vvp)->tv_usec < 0) { \
2787 (vvp)->tv_usec += 1000000; \
2793 * ppsratecheck(): packets (or events) per second limitation.
2796 ppsratecheck(lasttime
, curpps
, maxpps
)
2797 struct timeval
*lasttime
;
2799 int maxpps
; /* maximum pps allowed */
2801 struct timeval tv
, delta
;
2808 timersub(&tv
, lasttime
, &delta
);
2811 * check for 0,0 is so that the message will be seen at least once.
2812 * if more than one second have passed since the last update of
2813 * lasttime, reset the counter.
2815 * we do increment *curpps even in *curpps < maxpps case, as some may
2816 * try to use *curpps for stat purposes as well.
2818 if ((lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0) ||
2819 delta
.tv_sec
>= 1) {
2823 } else if (maxpps
< 0)
2825 else if (*curpps
< maxpps
)
2830 #if 1 /*DIAGNOSTIC?*/
2831 /* be careful about wrap-around */
2832 if (*curpps
+ 1 > *curpps
)
2833 *curpps
= *curpps
+ 1;
2836 * assume that there's not too many calls to this function.
2837 * not sure if the assumption holds, as it depends on *caller's*
2838 * behavior, not the behavior of this function.
2839 * IMHO it is wrong to make assumption on the caller's behavior,
2840 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
2842 *curpps
= *curpps
+ 1;
2850 * Perform rate limit check.
2851 * Returns 0 if it is okay to send the icmp6 packet.
2852 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2855 * XXX per-destination/type check necessary?
2858 icmp6_ratelimit(dst
, type
, code
)
2859 const struct in6_addr
*dst
; /* not used at this moment */
2860 const int type
; /* not used at this moment */
2861 const int code
; /* not used at this moment */
2865 ret
= 0; /*okay to send*/
2868 if (!ppsratecheck(&icmp6errppslim_last
, &icmp6errpps_count
,
2870 /* The packet is subject to rate limit */