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>
72 #include <sys/malloc.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
78 #include <sys/kernel.h>
79 #include <sys/syslog.h>
80 #include <sys/domain.h>
83 #include <net/route.h>
84 #include <net/if_dl.h>
85 #include <net/if_types.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/mld6_var.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/nd6.h>
96 #include <netinet6/in6_ifattach.h>
97 #include <netinet6/ip6protosw.h>
100 #include <netinet6/ipsec.h>
101 #include <netkey/key.h>
103 extern int ipsec_bypass
;
107 #if defined(NFAITH) && 0 < NFAITH
108 #include <net/if_faith.h>
111 #include <net/net_osdep.h>
113 extern struct domain inet6domain
;
114 extern struct ip6protosw inet6sw
[];
115 extern struct ip6protosw
*ip6_protox
[];
117 struct icmp6stat icmp6stat
;
119 extern struct inpcbhead ripcb
;
120 extern int icmp6errppslim
;
121 static int icmp6errpps_count
= 0;
122 static struct timeval icmp6errppslim_last
;
123 extern int icmp6_nodeinfo
;
124 extern struct inpcbinfo ripcbinfo
;
125 extern lck_mtx_t
*ip6_mutex
;
126 extern lck_mtx_t
*nd6_mutex
;
128 static void icmp6_errcount(struct icmp6errstat
*, int, int);
129 static int icmp6_rip6_input(struct mbuf
**, int);
130 static int icmp6_ratelimit(const struct in6_addr
*, const int, const int);
131 static const char *icmp6_redirect_diag(struct in6_addr
*,
132 struct in6_addr
*, struct in6_addr
*);
133 #ifndef HAVE_PPSRATECHECK
134 static int ppsratecheck(struct timeval
*, int *, int);
136 static struct mbuf
*ni6_input(struct mbuf
*, int);
137 static struct mbuf
*ni6_nametodns(const char *, int, int);
138 static int ni6_dnsmatch(const char *, int, const char *, int);
139 static int ni6_addrs(struct icmp6_nodeinfo
*,
140 struct ifnet
**, char *);
141 static int ni6_store_addrs(struct icmp6_nodeinfo
*, struct icmp6_nodeinfo
*,
142 struct ifnet
*, int);
143 static int icmp6_notify_error(struct mbuf
*, int, int, int);
145 #ifdef COMPAT_RFC1885
146 static struct route_in6 icmp6_reflect_rt
;
157 icmp6_errcount(stat
, type
, code
)
158 struct icmp6errstat
*stat
;
162 case ICMP6_DST_UNREACH
:
164 case ICMP6_DST_UNREACH_NOROUTE
:
165 stat
->icp6errs_dst_unreach_noroute
++;
167 case ICMP6_DST_UNREACH_ADMIN
:
168 stat
->icp6errs_dst_unreach_admin
++;
170 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
171 stat
->icp6errs_dst_unreach_beyondscope
++;
173 case ICMP6_DST_UNREACH_ADDR
:
174 stat
->icp6errs_dst_unreach_addr
++;
176 case ICMP6_DST_UNREACH_NOPORT
:
177 stat
->icp6errs_dst_unreach_noport
++;
181 case ICMP6_PACKET_TOO_BIG
:
182 stat
->icp6errs_packet_too_big
++;
184 case ICMP6_TIME_EXCEEDED
:
186 case ICMP6_TIME_EXCEED_TRANSIT
:
187 stat
->icp6errs_time_exceed_transit
++;
189 case ICMP6_TIME_EXCEED_REASSEMBLY
:
190 stat
->icp6errs_time_exceed_reassembly
++;
194 case ICMP6_PARAM_PROB
:
196 case ICMP6_PARAMPROB_HEADER
:
197 stat
->icp6errs_paramprob_header
++;
199 case ICMP6_PARAMPROB_NEXTHEADER
:
200 stat
->icp6errs_paramprob_nextheader
++;
202 case ICMP6_PARAMPROB_OPTION
:
203 stat
->icp6errs_paramprob_option
++;
208 stat
->icp6errs_redirect
++;
211 stat
->icp6errs_unknown
++;
215 * Generate an error packet of type error in response to bad IP6 packet.
218 icmp6_error(m
, type
, code
, param
)
220 int type
, code
, param
;
222 struct ip6_hdr
*oip6
, *nip6
;
223 struct icmp6_hdr
*icmp6
;
228 icmp6stat
.icp6s_error
++;
230 lck_mtx_assert(ip6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
231 /* count per-type-code statistics */
232 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, type
, code
);
234 #ifdef M_DECRYPTED /*not openbsd*/
235 if (m
->m_flags
& M_DECRYPTED
) {
236 icmp6stat
.icp6s_canterror
++;
241 #ifndef PULLDOWN_TEST
242 IP6_EXTHDR_CHECK(m
, 0, sizeof(struct ip6_hdr
), return);
244 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
245 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
250 oip6
= mtod(m
, struct ip6_hdr
*);
253 * Multicast destination check. For unrecognized option errors,
254 * this check has already done in ip6_unknown_opt(), so we can
255 * check only for other errors.
257 if ((m
->m_flags
& (M_BCAST
|M_MCAST
) ||
258 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_dst
)) &&
259 (type
!= ICMP6_PACKET_TOO_BIG
&&
260 (type
!= ICMP6_PARAM_PROB
||
261 code
!= ICMP6_PARAMPROB_OPTION
)))
264 /* Source address check. XXX: the case of anycast source? */
265 if (IN6_IS_ADDR_UNSPECIFIED(&oip6
->ip6_src
) ||
266 IN6_IS_ADDR_MULTICAST(&oip6
->ip6_src
))
270 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
274 off
= ip6_lasthdr(m
, 0, IPPROTO_IPV6
, &nxt
);
275 if (off
>= 0 && nxt
== IPPROTO_ICMPV6
) {
276 struct icmp6_hdr
*icp
;
278 #ifndef PULLDOWN_TEST
279 IP6_EXTHDR_CHECK(m
, 0, off
+ sizeof(struct icmp6_hdr
), return);
280 icp
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
282 IP6_EXTHDR_GET(icp
, struct icmp6_hdr
*, m
, off
,
285 icmp6stat
.icp6s_tooshort
++;
289 if (icp
->icmp6_type
< ICMP6_ECHO_REQUEST
||
290 icp
->icmp6_type
== ND_REDIRECT
) {
293 * Special case: for redirect (which is
294 * informational) we must not send icmp6 error.
296 icmp6stat
.icp6s_canterror
++;
299 /* ICMPv6 informational - send the error */
302 /* non-ICMPv6 - send the error */
305 oip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer */
307 /* Finally, do rate limitation check. */
308 if (icmp6_ratelimit(&oip6
->ip6_src
, type
, code
)) {
309 icmp6stat
.icp6s_toofreq
++;
314 * OK, ICMP6 can be generated.
317 if (m
->m_pkthdr
.len
>= ICMPV6_PLD_MAXLEN
)
318 m_adj(m
, ICMPV6_PLD_MAXLEN
- m
->m_pkthdr
.len
);
320 preplen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
321 M_PREPEND(m
, preplen
, M_DONTWAIT
);
322 if (m
&& m
->m_len
< preplen
)
323 m
= m_pullup(m
, preplen
);
325 nd6log((LOG_DEBUG
, "ENOBUFS in icmp6_error %d\n", __LINE__
));
329 nip6
= mtod(m
, struct ip6_hdr
*);
330 nip6
->ip6_src
= oip6
->ip6_src
;
331 nip6
->ip6_dst
= oip6
->ip6_dst
;
333 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_src
))
334 oip6
->ip6_src
.s6_addr16
[1] = 0;
335 if (IN6_IS_SCOPE_LINKLOCAL(&oip6
->ip6_dst
))
336 oip6
->ip6_dst
.s6_addr16
[1] = 0;
338 icmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
339 icmp6
->icmp6_type
= type
;
340 icmp6
->icmp6_code
= code
;
341 icmp6
->icmp6_pptr
= htonl((u_int32_t
)param
);
344 * icmp6_reflect() is designed to be in the input path.
345 * icmp6_error() can be called from both input and outut path,
346 * and if we are in output path rcvif could contain bogus value.
347 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
348 * information in ip header (nip6).
350 m
->m_pkthdr
.rcvif
= NULL
;
352 icmp6stat
.icp6s_outhist
[type
]++;
353 icmp6_reflect(m
, sizeof(struct ip6_hdr
)); /* header order: IPv6 - ICMPv6 */
359 * If we can't tell wheter or not we can generate ICMP6, free it.
365 * Process a received ICMP6 message.
368 icmp6_input(mp
, offp
)
372 struct mbuf
*m
= *mp
, *n
;
373 struct ip6_hdr
*ip6
, *nip6
;
374 struct icmp6_hdr
*icmp6
, *nicmp6
;
376 int icmp6len
= m
->m_pkthdr
.len
- *offp
;
379 #ifndef PULLDOWN_TEST
380 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_hdr
), return IPPROTO_DONE
);
381 /* m might change if M_LOOP. So, call mtod after this */
385 * Locate icmp6 structure in mbuf, and check
386 * that not corrupted and of at least minimum length
389 ip6
= mtod(m
, struct ip6_hdr
*);
390 if (icmp6len
< sizeof(struct icmp6_hdr
)) {
391 icmp6stat
.icp6s_tooshort
++;
396 * calculate the checksum
398 #ifndef PULLDOWN_TEST
399 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
401 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
403 icmp6stat
.icp6s_tooshort
++;
407 code
= icmp6
->icmp6_code
;
409 if ((sum
= in6_cksum(m
, IPPROTO_ICMPV6
, off
, icmp6len
)) != 0) {
411 "ICMP6 checksum error(%d|%x) %s\n",
412 icmp6
->icmp6_type
, sum
, ip6_sprintf(&ip6
->ip6_src
)));
413 icmp6stat
.icp6s_checksum
++;
417 #if defined(NFAITH) && 0 < NFAITH
418 if (faithprefix(&ip6
->ip6_dst
)) {
420 * Deliver very specific ICMP6 type only.
421 * This is important to deilver TOOBIG. Otherwise PMTUD
424 switch (icmp6
->icmp6_type
) {
425 case ICMP6_DST_UNREACH
:
426 case ICMP6_PACKET_TOO_BIG
:
427 case ICMP6_TIME_EXCEEDED
:
435 icmp6stat
.icp6s_inhist
[icmp6
->icmp6_type
]++;
436 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_msg
);
437 if (icmp6
->icmp6_type
< ICMP6_INFOMSG_MASK
)
438 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_error
);
440 switch (icmp6
->icmp6_type
) {
441 case ICMP6_DST_UNREACH
:
442 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_dstunreach
);
444 case ICMP6_DST_UNREACH_NOROUTE
:
445 code
= PRC_UNREACH_NET
;
447 case ICMP6_DST_UNREACH_ADMIN
:
448 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_adminprohib
);
449 code
= PRC_UNREACH_PROTOCOL
; /* is this a good code? */
451 case ICMP6_DST_UNREACH_ADDR
:
454 #ifdef COMPAT_RFC1885
455 case ICMP6_DST_UNREACH_NOTNEIGHBOR
:
456 code
= PRC_UNREACH_SRCFAIL
;
459 case ICMP6_DST_UNREACH_BEYONDSCOPE
:
460 /* I mean "source address was incorrect." */
461 code
= PRC_PARAMPROB
;
464 case ICMP6_DST_UNREACH_NOPORT
:
465 code
= PRC_UNREACH_PORT
;
473 case ICMP6_PACKET_TOO_BIG
:
474 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_pkttoobig
);
481 * Updating the path MTU will be done after examining
482 * intermediate extension headers.
487 case ICMP6_TIME_EXCEEDED
:
488 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_timeexceed
);
490 case ICMP6_TIME_EXCEED_TRANSIT
:
491 case ICMP6_TIME_EXCEED_REASSEMBLY
:
492 code
+= PRC_TIMXCEED_INTRANS
;
500 case ICMP6_PARAM_PROB
:
501 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_paramprob
);
503 case ICMP6_PARAMPROB_NEXTHEADER
:
504 code
= PRC_UNREACH_PROTOCOL
;
506 case ICMP6_PARAMPROB_HEADER
:
507 case ICMP6_PARAMPROB_OPTION
:
508 code
= PRC_PARAMPROB
;
516 case ICMP6_ECHO_REQUEST
:
517 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echo
);
520 if ((n
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
524 if ((n
->m_flags
& M_EXT
) != 0
525 || n
->m_len
< off
+ sizeof(struct icmp6_hdr
)) {
527 const int maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
);
530 * Prepare an internal mbuf. m_pullup() doesn't
531 * always copy the length we specified.
533 if (maxlen
>= MCLBYTES
) {
538 MGETHDR(n
, M_DONTWAIT
, n0
->m_type
);
539 if (n
&& maxlen
>= MHLEN
) {
540 MCLGET(n
, M_DONTWAIT
);
541 if ((n
->m_flags
& M_EXT
) == 0) {
551 M_COPY_PKTHDR(n
, n0
);
553 * Copy IPv6 and ICMPv6 only.
555 nip6
= mtod(n
, struct ip6_hdr
*);
556 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
557 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
558 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
559 noff
= sizeof(struct ip6_hdr
);
560 n
->m_pkthdr
.len
= n
->m_len
=
561 noff
+ sizeof(struct icmp6_hdr
);
563 * Adjust mbuf. ip6_plen will be adjusted in
566 m_adj(n0
, off
+ sizeof(struct icmp6_hdr
));
567 n
->m_pkthdr
.len
+= n0
->m_pkthdr
.len
;
569 n0
->m_flags
&= ~M_PKTHDR
;
571 nip6
= mtod(n
, struct ip6_hdr
*);
572 nicmp6
= (struct icmp6_hdr
*)((caddr_t
)nip6
+ off
);
575 nicmp6
->icmp6_type
= ICMP6_ECHO_REPLY
;
576 nicmp6
->icmp6_code
= 0;
578 icmp6stat
.icp6s_reflect
++;
579 icmp6stat
.icp6s_outhist
[ICMP6_ECHO_REPLY
]++;
580 icmp6_reflect(n
, noff
);
584 case ICMP6_ECHO_REPLY
:
585 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_echoreply
);
590 case MLD6_LISTENER_QUERY
:
591 case MLD6_LISTENER_REPORT
:
592 if (icmp6len
< sizeof(struct mld6_hdr
))
594 if (icmp6
->icmp6_type
== MLD6_LISTENER_QUERY
) /* XXX: ugly... */
595 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldquery
);
597 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mldreport
);
598 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
608 case MLD6_LISTENER_DONE
:
609 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_mlddone
);
610 if (icmp6len
< sizeof(struct mld6_hdr
)) /* necessary? */
612 break; /* nothing to be done in kernel */
614 case MLD6_MTRACE_RESP
:
616 /* XXX: these two are experimental. not officially defind. */
617 /* XXX: per-interface statistics? */
618 break; /* just pass it to applications */
620 case ICMP6_WRUREQUEST
: /* ICMP6_FQDN_QUERY */
622 enum { WRU
, FQDN
} mode
;
627 if (icmp6len
== sizeof(struct icmp6_hdr
) + 4)
629 else if (icmp6len
>= sizeof(struct icmp6_nodeinfo
))
634 #define hostnamelen strlen(hostname)
636 #ifndef PULLDOWN_TEST
637 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct icmp6_nodeinfo
),
638 return IPPROTO_DONE
);
640 n
= m_copy(m
, 0, M_COPYALL
);
642 n
= ni6_input(n
, off
);
643 /* XXX meaningless if n == NULL */
644 noff
= sizeof(struct ip6_hdr
);
649 if ((icmp6_nodeinfo
& 5) != 5)
654 maxlen
= sizeof(*nip6
) + sizeof(*nicmp6
) + 4;
655 if (maxlen
>= MCLBYTES
) {
659 MGETHDR(n
, M_DONTWAIT
, m
->m_type
);
660 if (n
&& maxlen
> MHLEN
) {
661 MCLGET(n
, M_DONTWAIT
);
662 if ((n
->m_flags
& M_EXT
) == 0) {
671 n
->m_pkthdr
.rcvif
= NULL
;
673 maxhlen
= M_TRAILINGSPACE(n
) - maxlen
;
674 if (maxhlen
> hostnamelen
)
675 maxhlen
= hostnamelen
;
677 * Copy IPv6 and ICMPv6 only.
679 nip6
= mtod(n
, struct ip6_hdr
*);
680 bcopy(ip6
, nip6
, sizeof(struct ip6_hdr
));
681 nicmp6
= (struct icmp6_hdr
*)(nip6
+ 1);
682 bcopy(icmp6
, nicmp6
, sizeof(struct icmp6_hdr
));
683 p
= (u_char
*)(nicmp6
+ 1);
685 bcopy(hostname
, p
+ 4, maxhlen
); /* meaningless TTL */
686 noff
= sizeof(struct ip6_hdr
);
687 M_COPY_PKTHDR(n
, m
); /* just for rcvif */
688 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
689 sizeof(struct icmp6_hdr
) + 4 + maxhlen
;
690 nicmp6
->icmp6_type
= ICMP6_WRUREPLY
;
691 nicmp6
->icmp6_code
= 0;
695 icmp6stat
.icp6s_reflect
++;
696 icmp6stat
.icp6s_outhist
[ICMP6_WRUREPLY
]++;
697 icmp6_reflect(n
, noff
);
707 case ND_ROUTER_SOLICIT
:
708 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routersolicit
);
711 if (icmp6len
< sizeof(struct nd_router_solicit
))
713 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
715 nd6_rs_input(m
, off
, icmp6len
);
719 nd6_rs_input(n
, off
, icmp6len
);
723 case ND_ROUTER_ADVERT
:
724 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_routeradvert
);
727 if (icmp6len
< sizeof(struct nd_router_advert
))
729 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
731 nd6_ra_input(m
, off
, icmp6len
);
735 nd6_ra_input(n
, off
, icmp6len
);
739 case ND_NEIGHBOR_SOLICIT
:
740 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighborsolicit
);
743 if (icmp6len
< sizeof(struct nd_neighbor_solicit
))
745 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
747 nd6_ns_input(m
, off
, icmp6len
);
751 nd6_ns_input(n
, off
, icmp6len
);
755 case ND_NEIGHBOR_ADVERT
:
756 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_neighboradvert
);
759 if (icmp6len
< sizeof(struct nd_neighbor_advert
))
761 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
763 nd6_na_input(m
, off
, icmp6len
);
767 nd6_na_input(n
, off
, icmp6len
);
772 icmp6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_redirect
);
775 if (icmp6len
< sizeof(struct nd_redirect
))
777 if ((n
= m_copym(m
, 0, M_COPYALL
, M_DONTWAIT
)) == NULL
) {
779 icmp6_redirect_input(m
, off
);
783 icmp6_redirect_input(n
, off
);
787 case ICMP6_ROUTER_RENUMBERING
:
788 if (code
!= ICMP6_ROUTER_RENUMBERING_COMMAND
&&
789 code
!= ICMP6_ROUTER_RENUMBERING_RESULT
)
791 if (icmp6len
< sizeof(struct icmp6_router_renum
))
797 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
798 icmp6
->icmp6_type
, ip6_sprintf(&ip6
->ip6_src
),
799 ip6_sprintf(&ip6
->ip6_dst
),
800 m
->m_pkthdr
.rcvif
? m
->m_pkthdr
.rcvif
->if_index
: 0));
801 if (icmp6
->icmp6_type
< ICMP6_ECHO_REQUEST
) {
802 /* ICMPv6 error: MUST deliver it by spec... */
806 /* ICMPv6 informational: MUST not deliver */
810 if (icmp6_notify_error(m
, off
, icmp6len
, code
)) {
811 /* In this case, m should've been freed. */
812 return(IPPROTO_DONE
);
817 icmp6stat
.icp6s_badcode
++;
821 icmp6stat
.icp6s_badlen
++;
825 /* deliver the packet to appropriate sockets */
826 icmp6_rip6_input(&m
, *offp
);
836 icmp6_notify_error(m
, off
, icmp6len
, code
)
838 int off
, icmp6len
, code
;
840 struct icmp6_hdr
*icmp6
;
841 struct ip6_hdr
*eip6
;
843 struct sockaddr_in6 icmp6src
, icmp6dst
;
845 if (icmp6len
< sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
)) {
846 icmp6stat
.icp6s_tooshort
++;
849 #ifndef PULLDOWN_TEST
850 IP6_EXTHDR_CHECK(m
, off
,
851 sizeof(struct icmp6_hdr
) + sizeof(struct ip6_hdr
),
853 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
855 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
856 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
858 icmp6stat
.icp6s_tooshort
++;
862 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
864 /* Detect the upper level protocol */
866 void (*ctlfunc
)(int, struct sockaddr
*, void *);
867 u_int8_t nxt
= eip6
->ip6_nxt
;
868 int eoff
= off
+ sizeof(struct icmp6_hdr
) +
869 sizeof(struct ip6_hdr
);
870 struct ip6ctlparam ip6cp
;
871 struct in6_addr
*finaldst
= NULL
;
872 int icmp6type
= icmp6
->icmp6_type
;
874 struct ip6_rthdr
*rth
;
875 struct ip6_rthdr0
*rth0
;
878 while (1) { /* XXX: should avoid infinite loop explicitly? */
882 case IPPROTO_HOPOPTS
:
883 case IPPROTO_DSTOPTS
:
885 #ifndef PULLDOWN_TEST
886 IP6_EXTHDR_CHECK(m
, 0, eoff
+
887 sizeof(struct ip6_ext
),
889 eh
= (struct ip6_ext
*)(mtod(m
, caddr_t
)
892 IP6_EXTHDR_GET(eh
, struct ip6_ext
*, m
,
895 icmp6stat
.icp6s_tooshort
++;
900 if (nxt
== IPPROTO_AH
)
901 eoff
+= (eh
->ip6e_len
+ 2) << 2;
903 eoff
+= (eh
->ip6e_len
+ 1) << 3;
906 case IPPROTO_ROUTING
:
908 * When the erroneous packet contains a
909 * routing header, we should examine the
910 * header to determine the final destination.
911 * Otherwise, we can't properly update
912 * information that depends on the final
913 * destination (e.g. path MTU).
915 #ifndef PULLDOWN_TEST
916 IP6_EXTHDR_CHECK(m
, 0, eoff
+ sizeof(*rth
),
918 rth
= (struct ip6_rthdr
*)(mtod(m
, caddr_t
)
921 IP6_EXTHDR_GET(rth
, struct ip6_rthdr
*, m
,
924 icmp6stat
.icp6s_tooshort
++;
928 rthlen
= (rth
->ip6r_len
+ 1) << 3;
930 * XXX: currently there is no
931 * officially defined type other
933 * Note that if the segment left field
934 * is 0, all intermediate hops must
937 if (rth
->ip6r_segleft
&&
938 rth
->ip6r_type
== IPV6_RTHDR_TYPE_0
) {
941 #ifndef PULLDOWN_TEST
942 IP6_EXTHDR_CHECK(m
, 0, eoff
+ rthlen
,
944 rth0
= (struct ip6_rthdr0
*)(mtod(m
, caddr_t
) + eoff
);
947 struct ip6_rthdr0
*, m
,
950 icmp6stat
.icp6s_tooshort
++;
954 /* just ignore a bogus header */
955 if ((rth0
->ip6r0_len
% 2) == 0 &&
956 (hops
= rth0
->ip6r0_len
/2))
957 finaldst
= (struct in6_addr
*)(rth0
+ 1) + (hops
- 1);
962 case IPPROTO_FRAGMENT
:
963 #ifndef PULLDOWN_TEST
964 IP6_EXTHDR_CHECK(m
, 0, eoff
+
965 sizeof(struct ip6_frag
),
967 fh
= (struct ip6_frag
*)(mtod(m
, caddr_t
)
970 IP6_EXTHDR_GET(fh
, struct ip6_frag
*, m
,
973 icmp6stat
.icp6s_tooshort
++;
978 * Data after a fragment header is meaningless
979 * unless it is the first fragment, but
980 * we'll go to the notify label for path MTU
983 if (fh
->ip6f_offlg
& IP6F_OFF_MASK
)
986 eoff
+= sizeof(struct ip6_frag
);
991 * This case includes ESP and the No Next
992 * Header. In such cases going to the notify
993 * label does not have any meaning
994 * (i.e. ctlfunc will be NULL), but we go
995 * anyway since we might have to update
996 * path MTU information.
1002 #ifndef PULLDOWN_TEST
1003 icmp6
= (struct icmp6_hdr
*)(mtod(m
, caddr_t
) + off
);
1005 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
,
1006 sizeof(*icmp6
) + sizeof(struct ip6_hdr
));
1007 if (icmp6
== NULL
) {
1008 icmp6stat
.icp6s_tooshort
++;
1013 eip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1014 bzero(&icmp6dst
, sizeof(icmp6dst
));
1015 icmp6dst
.sin6_len
= sizeof(struct sockaddr_in6
);
1016 icmp6dst
.sin6_family
= AF_INET6
;
1017 if (finaldst
== NULL
)
1018 icmp6dst
.sin6_addr
= eip6
->ip6_dst
;
1020 icmp6dst
.sin6_addr
= *finaldst
;
1021 icmp6dst
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1022 &icmp6dst
.sin6_addr
);
1023 #ifndef SCOPEDROUTING
1024 if (in6_embedscope(&icmp6dst
.sin6_addr
, &icmp6dst
,
1026 /* should be impossbile */
1028 "icmp6_notify_error: in6_embedscope failed\n"));
1034 * retrieve parameters from the inner IPv6 header, and convert
1035 * them into sockaddr structures.
1037 bzero(&icmp6src
, sizeof(icmp6src
));
1038 icmp6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1039 icmp6src
.sin6_family
= AF_INET6
;
1040 icmp6src
.sin6_addr
= eip6
->ip6_src
;
1041 icmp6src
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1042 &icmp6src
.sin6_addr
);
1043 #ifndef SCOPEDROUTING
1044 if (in6_embedscope(&icmp6src
.sin6_addr
, &icmp6src
,
1046 /* should be impossbile */
1048 "icmp6_notify_error: in6_embedscope failed\n"));
1052 icmp6src
.sin6_flowinfo
=
1053 (eip6
->ip6_flow
& IPV6_FLOWLABEL_MASK
);
1055 if (finaldst
== NULL
)
1056 finaldst
= &eip6
->ip6_dst
;
1058 ip6cp
.ip6c_icmp6
= icmp6
;
1059 ip6cp
.ip6c_ip6
= (struct ip6_hdr
*)(icmp6
+ 1);
1060 ip6cp
.ip6c_off
= eoff
;
1061 ip6cp
.ip6c_finaldst
= finaldst
;
1062 ip6cp
.ip6c_src
= &icmp6src
;
1063 ip6cp
.ip6c_nxt
= nxt
;
1065 if (icmp6type
== ICMP6_PACKET_TOO_BIG
) {
1066 notifymtu
= ntohl(icmp6
->icmp6_mtu
);
1067 ip6cp
.ip6c_cmdarg
= (void *)¬ifymtu
;
1068 icmp6_mtudisc_update(&ip6cp
, 1); /*XXX*/
1071 ctlfunc
= (void (*)(int, struct sockaddr
*, void *))
1072 (ip6_protox
[nxt
]->pr_ctlinput
);
1074 (void) (*ctlfunc
)(code
, (struct sockaddr
*)&icmp6dst
,
1086 icmp6_mtudisc_update(ip6cp
, validated
)
1087 struct ip6ctlparam
*ip6cp
;
1090 struct in6_addr
*dst
= ip6cp
->ip6c_finaldst
;
1091 struct icmp6_hdr
*icmp6
= ip6cp
->ip6c_icmp6
;
1092 struct mbuf
*m
= ip6cp
->ip6c_m
; /* will be necessary for scope issue */
1093 u_int mtu
= ntohl(icmp6
->icmp6_mtu
);
1094 struct rtentry
*rt
= NULL
;
1095 struct sockaddr_in6 sin6
;
1100 bzero(&sin6
, sizeof(sin6
));
1101 sin6
.sin6_family
= PF_INET6
;
1102 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1103 sin6
.sin6_addr
= *dst
;
1104 /* XXX normally, this won't happen */
1105 if (IN6_IS_ADDR_LINKLOCAL(dst
)) {
1106 sin6
.sin6_addr
.s6_addr16
[1] =
1107 htons(m
->m_pkthdr
.rcvif
->if_index
);
1109 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1110 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0,
1111 RTF_CLONING
| RTF_PRCLONING
);
1113 if (rt
&& (rt
->rt_flags
& RTF_HOST
)
1114 && !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
1115 if (mtu
< IPV6_MMTU
) {
1117 rt
->rt_rmx
.rmx_locks
|= RTV_MTU
;
1118 } else if (mtu
< rt
->rt_ifp
->if_mtu
&&
1119 rt
->rt_rmx
.rmx_mtu
> mtu
) {
1120 icmp6stat
.icp6s_pmtuchg
++;
1121 rt
->rt_rmx
.rmx_mtu
= mtu
;
1129 * Process a Node Information Query packet, based on
1130 * draft-ietf-ipngwg-icmp-name-lookups-07.
1132 * Spec incompatibilities:
1133 * - IPv6 Subject address handling
1134 * - IPv4 Subject address handling support missing
1135 * - Proxy reply (answer even if it's not for me)
1136 * - joins NI group address at in6_ifattach() time only, does not cope
1137 * with hostname changes by sethostname(3)
1139 #define hostnamelen strlen(hostname)
1140 static struct mbuf
*
1145 struct icmp6_nodeinfo
*ni6
, *nni6
;
1146 struct mbuf
*n
= NULL
;
1149 int replylen
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1150 struct ni_reply_fqdn
*fqdn
;
1151 int addrs
; /* for NI_QTYPE_NODEADDR */
1152 struct ifnet
*ifp
= NULL
; /* for NI_QTYPE_NODEADDR */
1153 struct sockaddr_in6 sin6
; /* double meaning; ip6_dst and subjectaddr */
1154 struct sockaddr_in6 sin6_d
; /* XXX: we should retrieve this from m_aux */
1155 struct ip6_hdr
*ip6
;
1156 int oldfqdn
= 0; /* if 1, return pascal string (03 draft) */
1158 struct in6_ifaddr
*ia6
= NULL
;
1160 ip6
= mtod(m
, struct ip6_hdr
*);
1161 #ifndef PULLDOWN_TEST
1162 ni6
= (struct icmp6_nodeinfo
*)(mtod(m
, caddr_t
) + off
);
1164 IP6_EXTHDR_GET(ni6
, struct icmp6_nodeinfo
*, m
, off
, sizeof(*ni6
));
1166 /* m is already reclaimed */
1172 * Validate IPv6 destination address.
1174 * The Responder must discard the Query without further processing
1175 * unless it is one of the Responder's unicast or anycast addresses, or
1176 * a link-local scope multicast address which the Responder has joined.
1177 * [icmp-name-lookups-07, Section 4.]
1179 bzero(&sin6
, sizeof(sin6
));
1180 sin6
.sin6_family
= AF_INET6
;
1181 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1182 bcopy(&ip6
->ip6_dst
, &sin6
.sin6_addr
, sizeof(sin6
.sin6_addr
));
1184 if ((ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr((struct sockaddr
*)&sin6
)) != NULL
) {
1185 /* unicast/anycast, fine */
1186 if ((ia6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1187 (icmp6_nodeinfo
& 4) == 0) {
1188 ifafree(&ia6
->ia_ifa
);
1190 nd6log((LOG_DEBUG
, "ni6_input: ignore node info to "
1191 "a temporary address in %s:%d",
1192 __FILE__
, __LINE__
));
1195 ifafree(&ia6
->ia_ifa
);
1197 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
))
1198 ; /* link-local multicast, fine */
1202 /* validate query Subject field. */
1203 qtype
= ntohs(ni6
->ni_qtype
);
1204 subjlen
= m
->m_pkthdr
.len
- off
- sizeof(struct icmp6_nodeinfo
);
1207 case NI_QTYPE_SUPTYPES
:
1209 if (ni6
->ni_code
== ICMP6_NI_SUBJ_FQDN
&& subjlen
== 0)
1213 case NI_QTYPE_NODEADDR
:
1214 switch (ni6
->ni_code
) {
1215 case ICMP6_NI_SUBJ_IPV6
:
1216 #if ICMP6_NI_SUBJ_IPV6 != 0
1220 * backward compatibility - try to accept 03 draft
1221 * format, where no Subject is present.
1223 if (qtype
== NI_QTYPE_FQDN
&& ni6
->ni_code
== 0 &&
1228 #if ICMP6_NI_SUBJ_IPV6 != 0
1229 if (ni6
->ni_code
!= ICMP6_NI_SUBJ_IPV6
)
1233 if (subjlen
!= sizeof(sin6
.sin6_addr
))
1237 * Validate Subject address.
1239 * Not sure what exactly "address belongs to the node"
1240 * means in the spec, is it just unicast, or what?
1242 * At this moment we consider Subject address as
1243 * "belong to the node" if the Subject address equals
1244 * to the IPv6 destination address; validation for
1245 * IPv6 destination address should have done enough
1248 * We do not do proxy at this moment.
1250 /* m_pulldown instead of copy? */
1251 m_copydata(m
, off
+ sizeof(struct icmp6_nodeinfo
),
1252 subjlen
, (caddr_t
)&sin6
.sin6_addr
);
1253 sin6
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1255 #ifndef SCOPEDROUTING
1256 in6_embedscope(&sin6
.sin6_addr
, &sin6
, NULL
, NULL
);
1258 bzero(&sin6_d
, sizeof(sin6_d
));
1259 sin6_d
.sin6_family
= AF_INET6
; /* not used, actually */
1260 sin6_d
.sin6_len
= sizeof(sin6_d
); /* ditto */
1261 sin6_d
.sin6_addr
= ip6
->ip6_dst
;
1262 sin6_d
.sin6_scope_id
= in6_addr2scopeid(m
->m_pkthdr
.rcvif
,
1264 #ifndef SCOPEDROUTING
1265 in6_embedscope(&sin6_d
.sin6_addr
, &sin6_d
, NULL
, NULL
);
1267 subj
= (char *)&sin6
;
1268 if (SA6_ARE_ADDR_EQUAL(&sin6
, &sin6_d
))
1272 * XXX if we are to allow other cases, we should really
1273 * be careful about scope here.
1274 * basically, we should disallow queries toward IPv6
1275 * destination X with subject Y, if scope(X) > scope(Y).
1276 * if we allow scope(X) > scope(Y), it will result in
1277 * information leakage across scope boundary.
1281 case ICMP6_NI_SUBJ_FQDN
:
1283 * Validate Subject name with gethostname(3).
1285 * The behavior may need some debate, since:
1286 * - we are not sure if the node has FQDN as
1287 * hostname (returned by gethostname(3)).
1288 * - the code does wildcard match for truncated names.
1289 * however, we are not sure if we want to perform
1290 * wildcard match, if gethostname(3) side has
1291 * truncated hostname.
1293 n
= ni6_nametodns(hostname
, hostnamelen
, 0);
1294 if (!n
|| n
->m_next
|| n
->m_len
== 0)
1296 IP6_EXTHDR_GET(subj
, char *, m
,
1297 off
+ sizeof(struct icmp6_nodeinfo
), subjlen
);
1300 if (!ni6_dnsmatch(subj
, subjlen
, mtod(n
, const char *),
1308 case ICMP6_NI_SUBJ_IPV4
: /* XXX: to be implemented? */
1315 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1318 if ((icmp6_nodeinfo
& 1) == 0)
1321 case NI_QTYPE_NODEADDR
:
1322 if ((icmp6_nodeinfo
& 2) == 0)
1327 /* guess reply length */
1330 break; /* no reply data */
1331 case NI_QTYPE_SUPTYPES
:
1332 replylen
+= sizeof(u_int32_t
);
1335 /* XXX will append an mbuf */
1336 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1338 case NI_QTYPE_NODEADDR
:
1339 addrs
= ni6_addrs(ni6
, &ifp
, subj
);
1340 if ((replylen
+= addrs
* (sizeof(struct in6_addr
) +
1341 sizeof(u_int32_t
))) > MCLBYTES
)
1342 replylen
= MCLBYTES
; /* XXX: will truncate pkt later */
1346 * XXX: We must return a reply with the ICMP6 code
1347 * `unknown Qtype' in this case. However we regard the case
1348 * as an FQDN query for backward compatibility.
1349 * Older versions set a random value to this field,
1350 * so it rarely varies in the defined qtypes.
1351 * But the mechanism is not reliable...
1352 * maybe we should obsolete older versions.
1354 qtype
= NI_QTYPE_FQDN
;
1355 /* XXX will append an mbuf */
1356 replylen
+= offsetof(struct ni_reply_fqdn
, ni_fqdn_namelen
);
1361 /* allocate an mbuf to reply. */
1362 MGETHDR(n
, M_DONTWAIT
, m
->m_type
);
1367 M_COPY_PKTHDR(n
, m
); /* just for recvif */
1368 if (replylen
> MHLEN
) {
1369 if (replylen
> MCLBYTES
) {
1371 * XXX: should we try to allocate more? But MCLBYTES
1372 * is probably much larger than IPV6_MMTU...
1376 MCLGET(n
, M_DONTWAIT
);
1377 if ((n
->m_flags
& M_EXT
) == 0) {
1381 n
->m_pkthdr
.len
= n
->m_len
= replylen
;
1383 /* copy mbuf header and IPv6 + Node Information base headers */
1384 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), sizeof(struct ip6_hdr
));
1385 nni6
= (struct icmp6_nodeinfo
*)(mtod(n
, struct ip6_hdr
*) + 1);
1386 bcopy((caddr_t
)ni6
, (caddr_t
)nni6
, sizeof(struct icmp6_nodeinfo
));
1388 /* qtype dependent procedure */
1391 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1394 case NI_QTYPE_SUPTYPES
:
1397 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1398 nni6
->ni_flags
= htons(0x0000); /* raw bitmap */
1399 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1400 v
= (u_int32_t
)htonl(0x0000000f);
1401 bcopy(&v
, nni6
+ 1, sizeof(u_int32_t
));
1405 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1406 fqdn
= (struct ni_reply_fqdn
*)(mtod(n
, caddr_t
) +
1407 sizeof(struct ip6_hdr
) +
1408 sizeof(struct icmp6_nodeinfo
));
1409 nni6
->ni_flags
= 0; /* XXX: meaningless TTL */
1410 fqdn
->ni_fqdn_ttl
= 0; /* ditto. */
1412 * XXX do we really have FQDN in variable "hostname"?
1414 n
->m_next
= ni6_nametodns(hostname
, hostnamelen
, oldfqdn
);
1415 if (n
->m_next
== NULL
)
1417 /* XXX we assume that n->m_next is not a chain */
1418 if (n
->m_next
->m_next
!= NULL
)
1420 n
->m_pkthdr
.len
+= n
->m_next
->m_len
;
1422 case NI_QTYPE_NODEADDR
:
1426 nni6
->ni_code
= ICMP6_NI_SUCCESS
;
1427 n
->m_pkthdr
.len
= n
->m_len
=
1428 sizeof(struct ip6_hdr
) + sizeof(struct icmp6_nodeinfo
);
1429 lenlim
= M_TRAILINGSPACE(n
);
1430 copied
= ni6_store_addrs(ni6
, nni6
, ifp
, lenlim
);
1431 /* XXX: reset mbuf length */
1432 n
->m_pkthdr
.len
= n
->m_len
= sizeof(struct ip6_hdr
) +
1433 sizeof(struct icmp6_nodeinfo
) + copied
;
1437 break; /* XXX impossible! */
1440 nni6
->ni_type
= ICMP6_NI_REPLY
;
1453 * make a mbuf with DNS-encoded string. no compression support.
1455 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1456 * treated as truncated name (two \0 at the end). this is a wild guess.
1458 static struct mbuf
*
1459 ni6_nametodns(name
, namelen
, old
)
1462 int old
; /* return pascal string if non-zero */
1474 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1475 MGET(m
, M_DONTWAIT
, MT_DATA
);
1476 if (m
&& len
> MLEN
) {
1477 MCLGET(m
, M_DONTWAIT
);
1478 if ((m
->m_flags
& M_EXT
) == 0)
1487 *mtod(m
, char *) = namelen
;
1488 bcopy(name
, mtod(m
, char *) + 1, namelen
);
1492 cp
= mtod(m
, char *);
1493 ep
= mtod(m
, char *) + M_TRAILINGSPACE(m
);
1495 /* if not certain about my name, return empty buffer */
1500 * guess if it looks like shortened hostname, or FQDN.
1501 * shortened hostname needs two trailing "\0".
1504 for (p
= name
; p
< name
+ namelen
; p
++) {
1505 if (*p
&& *p
== '.')
1514 while (cp
< ep
&& p
< name
+ namelen
) {
1516 for (q
= p
; q
< name
+ namelen
&& *q
&& *q
!= '.'; q
++)
1518 /* result does not fit into mbuf */
1519 if (cp
+ i
+ 1 >= ep
)
1522 * DNS label length restriction, RFC1035 page 8.
1523 * "i == 0" case is included here to avoid returning
1524 * 0-length label on "foo..bar".
1526 if (i
<= 0 || i
>= 64)
1532 if (p
< name
+ namelen
&& *p
== '.')
1536 if (cp
+ nterm
>= ep
)
1540 m
->m_len
= cp
- mtod(m
, char *);
1544 panic("should not reach here");
1554 * check if two DNS-encoded string matches. takes care of truncated
1555 * form (with \0\0 at the end). no compression support.
1556 * XXX upper/lowercase match (see RFC2065)
1559 ni6_dnsmatch(a
, alen
, b
, blen
)
1565 const char *a0
, *b0
;
1568 /* simplest case - need validation? */
1569 if (alen
== blen
&& bcmp(a
, b
, alen
) == 0)
1575 /* termination is mandatory */
1576 if (alen
< 2 || blen
< 2)
1578 if (a0
[alen
- 1] != '\0' || b0
[blen
- 1] != '\0')
1583 while (a
- a0
< alen
&& b
- b0
< blen
) {
1584 if (a
- a0
+ 1 > alen
|| b
- b0
+ 1 > blen
)
1587 if ((signed char)a
[0] < 0 || (signed char)b
[0] < 0)
1589 /* we don't support compression yet */
1590 if (a
[0] >= 64 || b
[0] >= 64)
1593 /* truncated case */
1594 if (a
[0] == 0 && a
- a0
== alen
- 1)
1596 if (b
[0] == 0 && b
- b0
== blen
- 1)
1598 if (a
[0] == 0 || b
[0] == 0)
1604 if (a
- a0
+ 1 + l
> alen
|| b
- b0
+ 1 + l
> blen
)
1606 if (bcmp(a
+ 1, b
+ 1, l
) != 0)
1613 if (a
- a0
== alen
&& b
- b0
== blen
)
1620 * calculate the number of addresses to be returned in the node info reply.
1623 ni6_addrs(ni6
, ifpp
, subj
)
1624 struct icmp6_nodeinfo
*ni6
;
1625 struct ifnet
**ifpp
;
1629 struct in6_ifaddr
*ifa6
;
1631 struct sockaddr_in6
*subj_ip6
= NULL
; /* XXX pedant */
1632 int addrs
= 0, addrsofif
, iffound
= 0;
1633 int niflags
= ni6
->ni_flags
;
1635 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0) {
1636 switch (ni6
->ni_code
) {
1637 case ICMP6_NI_SUBJ_IPV6
:
1638 if (subj
== NULL
) /* must be impossible... */
1640 subj_ip6
= (struct sockaddr_in6
*)subj
;
1644 * XXX: we only support IPv6 subject address for
1651 ifnet_head_lock_shared();
1652 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
1654 ifnet_lock_shared(ifp
);
1655 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1657 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1659 ifa6
= (struct in6_ifaddr
*)ifa
;
1661 if ((niflags
& NI_NODEADDR_FLAG_ALL
) == 0 &&
1662 IN6_ARE_ADDR_EQUAL(&subj_ip6
->sin6_addr
,
1663 &ifa6
->ia_addr
.sin6_addr
))
1667 * IPv4-mapped addresses can only be returned by a
1668 * Node Information proxy, since they represent
1669 * addresses of IPv4-only nodes, which perforce do
1670 * not implement this protocol.
1671 * [icmp-name-lookups-07, Section 5.4]
1672 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1673 * this function at this moment.
1676 /* What do we have to do about ::1? */
1677 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1678 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1679 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1682 case IPV6_ADDR_SCOPE_SITELOCAL
:
1683 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1686 case IPV6_ADDR_SCOPE_GLOBAL
:
1687 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1695 * check if anycast is okay.
1696 * XXX: just experimental. not in the spec.
1698 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1699 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1700 continue; /* we need only unicast addresses */
1701 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1702 (icmp6_nodeinfo
& 4) == 0) {
1705 addrsofif
++; /* count the address */
1707 ifnet_lock_done(ifp
);
1722 ni6_store_addrs(ni6
, nni6
, ifp0
, resid
)
1723 struct icmp6_nodeinfo
*ni6
, *nni6
;
1727 struct ifnet
*ifp
= ifp0
;
1728 struct in6_ifaddr
*ifa6
;
1730 struct ifnet
*ifp_dep
= NULL
;
1731 int copied
= 0, allow_deprecated
= 0;
1732 u_char
*cp
= (u_char
*)(nni6
+ 1);
1733 int niflags
= ni6
->ni_flags
;
1735 struct timeval timenow
;
1737 getmicrotime(&timenow
);
1739 if (ifp0
== NULL
&& !(niflags
& NI_NODEADDR_FLAG_ALL
))
1740 return(0); /* needless to copy */
1744 ifnet_head_lock_shared();
1745 if (ifp
== NULL
) ifp
= TAILQ_FIRST(&ifnet_head
);
1747 for (; ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
)) {
1748 ifnet_lock_shared(ifp
);
1749 for (ifa
= ifp
->if_addrlist
.tqh_first
; ifa
;
1750 ifa
= ifa
->ifa_list
.tqe_next
)
1752 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1754 ifa6
= (struct in6_ifaddr
*)ifa
;
1756 if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0 &&
1757 allow_deprecated
== 0) {
1759 * prefererred address should be put before
1760 * deprecated addresses.
1763 /* record the interface for later search */
1764 if (ifp_dep
== NULL
)
1769 else if ((ifa6
->ia6_flags
& IN6_IFF_DEPRECATED
) == 0 &&
1770 allow_deprecated
!= 0)
1771 continue; /* we now collect deprecated addrs */
1773 /* What do we have to do about ::1? */
1774 switch (in6_addrscope(&ifa6
->ia_addr
.sin6_addr
)) {
1775 case IPV6_ADDR_SCOPE_LINKLOCAL
:
1776 if ((niflags
& NI_NODEADDR_FLAG_LINKLOCAL
) == 0)
1779 case IPV6_ADDR_SCOPE_SITELOCAL
:
1780 if ((niflags
& NI_NODEADDR_FLAG_SITELOCAL
) == 0)
1783 case IPV6_ADDR_SCOPE_GLOBAL
:
1784 if ((niflags
& NI_NODEADDR_FLAG_GLOBAL
) == 0)
1792 * check if anycast is okay.
1793 * XXX: just experimental. not in the spec.
1795 if ((ifa6
->ia6_flags
& IN6_IFF_ANYCAST
) != 0 &&
1796 (niflags
& NI_NODEADDR_FLAG_ANYCAST
) == 0)
1798 if ((ifa6
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0 &&
1799 (icmp6_nodeinfo
& 4) == 0) {
1803 /* now we can copy the address */
1804 if (resid
< sizeof(struct in6_addr
) +
1805 sizeof(u_int32_t
)) {
1807 * We give up much more copy.
1808 * Set the truncate flag and return.
1811 NI_NODEADDR_FLAG_TRUNCATE
;
1812 ifnet_lock_done(ifp
);
1818 * Set the TTL of the address.
1819 * The TTL value should be one of the following
1820 * according to the specification:
1822 * 1. The remaining lifetime of a DHCP lease on the
1824 * 2. The remaining Valid Lifetime of a prefix from
1825 * which the address was derived through Stateless
1826 * Autoconfiguration.
1828 * Note that we currently do not support stateful
1829 * address configuration by DHCPv6, so the former
1830 * case can't happen.
1832 if (ifa6
->ia6_lifetime
.ia6t_expire
== 0)
1833 ltime
= ND6_INFINITE_LIFETIME
;
1835 if (ifa6
->ia6_lifetime
.ia6t_expire
>
1837 ltime
= htonl(ifa6
->ia6_lifetime
.ia6t_expire
- timenow
.tv_sec
);
1842 bcopy(<ime
, cp
, sizeof(u_int32_t
));
1843 cp
+= sizeof(u_int32_t
);
1845 /* copy the address itself */
1846 bcopy(&ifa6
->ia_addr
.sin6_addr
, cp
,
1847 sizeof(struct in6_addr
));
1848 /* XXX: KAME link-local hack; remove ifindex */
1849 if (IN6_IS_ADDR_LINKLOCAL(&ifa6
->ia_addr
.sin6_addr
))
1850 ((struct in6_addr
*)cp
)->s6_addr16
[1] = 0;
1851 cp
+= sizeof(struct in6_addr
);
1853 resid
-= (sizeof(struct in6_addr
) + sizeof(u_int32_t
));
1854 copied
+= (sizeof(struct in6_addr
) +
1857 ifnet_lock_done(ifp
);
1858 if (ifp0
) /* we need search only on the specified IF */
1863 if (allow_deprecated
== 0 && ifp_dep
!= NULL
) {
1865 allow_deprecated
= 1;
1874 * XXX almost dup'ed code with rip6_input.
1877 icmp6_rip6_input(mp
, off
)
1881 struct mbuf
*m
= *mp
;
1882 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
1883 struct in6pcb
*in6p
;
1884 struct in6pcb
*last
= NULL
;
1885 struct sockaddr_in6 rip6src
;
1886 struct icmp6_hdr
*icmp6
;
1887 struct mbuf
*opts
= NULL
;
1889 #ifndef PULLDOWN_TEST
1890 /* this is assumed to be safe. */
1891 icmp6
= (struct icmp6_hdr
*)((caddr_t
)ip6
+ off
);
1893 IP6_EXTHDR_GET(icmp6
, struct icmp6_hdr
*, m
, off
, sizeof(*icmp6
));
1894 if (icmp6
== NULL
) {
1895 /* m is already reclaimed */
1896 return IPPROTO_DONE
;
1900 bzero(&rip6src
, sizeof(rip6src
));
1901 rip6src
.sin6_len
= sizeof(struct sockaddr_in6
);
1902 rip6src
.sin6_family
= AF_INET6
;
1903 /* KAME hack: recover scopeid */
1904 (void)in6_recoverscope(&rip6src
, &ip6
->ip6_src
, m
->m_pkthdr
.rcvif
);
1906 lck_rw_lock_shared(ripcbinfo
.mtx
);
1907 LIST_FOREACH(in6p
, &ripcb
, inp_list
)
1909 if ((in6p
->inp_vflag
& INP_IPV6
) == 0)
1912 if (!(in6p
->in6p_flags
& INP_IPV6
))
1915 if (in6p
->in6p_ip6_nxt
!= IPPROTO_ICMPV6
)
1917 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
) &&
1918 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_laddr
, &ip6
->ip6_dst
))
1920 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
) &&
1921 !IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
, &ip6
->ip6_src
))
1923 if (in6p
->in6p_icmp6filt
1924 && ICMP6_FILTER_WILLBLOCK(icmp6
->icmp6_type
,
1925 in6p
->in6p_icmp6filt
))
1929 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
1930 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1931 ip6_savecontrol(last
, &opts
, ip6
, n
);
1932 /* strip intermediate headers */
1934 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1935 (struct sockaddr
*)&rip6src
,
1936 n
, opts
, NULL
) != 0) {
1937 sorwakeup(last
->in6p_socket
);
1944 lck_rw_done(ripcbinfo
.mtx
);
1946 if (last
->in6p_flags
& IN6P_CONTROLOPTS
)
1947 ip6_savecontrol(last
, &opts
, ip6
, m
);
1948 /* strip intermediate headers */
1950 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
1951 (struct sockaddr
*)&rip6src
, m
, opts
, NULL
) != 0) {
1952 sorwakeup(last
->in6p_socket
);
1956 ip6stat
.ip6s_delivered
--;
1958 return IPPROTO_DONE
;
1962 * Reflect the ip6 packet back to the source.
1963 * OFF points to the icmp6 header, counted from the top of the mbuf.
1966 icmp6_reflect(m
, off
)
1970 struct ip6_hdr
*ip6
;
1971 struct icmp6_hdr
*icmp6
;
1972 struct in6_ifaddr
*ia
;
1973 struct in6_addr t
, src_storage
, *src
= 0;
1976 struct ifnet
*outif
= NULL
;
1977 struct sockaddr_in6 sa6_src
, sa6_dst
;
1978 #ifdef COMPAT_RFC1885
1979 int mtu
= IPV6_MMTU
;
1980 struct sockaddr_in6
*sin6
= &icmp6_reflect_rt
.ro_dst
;
1983 /* too short to reflect */
1984 if (off
< sizeof(struct ip6_hdr
)) {
1986 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1987 (u_long
)off
, (u_long
)sizeof(struct ip6_hdr
),
1988 __FILE__
, __LINE__
));
1993 * If there are extra headers between IPv6 and ICMPv6, strip
1994 * off that header first.
1997 if (sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) > MHLEN
)
1998 panic("assumption failed in icmp6_reflect");
2000 if (off
> sizeof(struct ip6_hdr
)) {
2002 struct ip6_hdr nip6
;
2004 l
= off
- sizeof(struct ip6_hdr
);
2005 m_copydata(m
, 0, sizeof(nip6
), (caddr_t
)&nip6
);
2007 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2009 if ((m
= m_pullup(m
, l
)) == NULL
)
2012 bcopy((caddr_t
)&nip6
, mtod(m
, caddr_t
), sizeof(nip6
));
2013 } else /* off == sizeof(struct ip6_hdr) */ {
2015 l
= sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
);
2017 if ((m
= m_pullup(m
, l
)) == NULL
)
2021 plen
= m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
);
2022 ip6
= mtod(m
, struct ip6_hdr
*);
2023 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2024 icmp6
= (struct icmp6_hdr
*)(ip6
+ 1);
2025 type
= icmp6
->icmp6_type
; /* keep type for statistics */
2026 code
= icmp6
->icmp6_code
; /* ditto. */
2030 * ip6_input() drops a packet if its src is multicast.
2031 * So, the src is never multicast.
2033 ip6
->ip6_dst
= ip6
->ip6_src
;
2036 * XXX: make sure to embed scope zone information, using
2037 * already embedded IDs or the received interface (if any).
2038 * Note that rcvif may be NULL.
2039 * TODO: scoped routing case (XXX).
2041 bzero(&sa6_src
, sizeof(sa6_src
));
2042 sa6_src
.sin6_family
= AF_INET6
;
2043 sa6_src
.sin6_len
= sizeof(sa6_src
);
2044 sa6_src
.sin6_addr
= ip6
->ip6_dst
;
2045 in6_recoverscope(&sa6_src
, &ip6
->ip6_dst
, m
->m_pkthdr
.rcvif
);
2046 in6_embedscope(&ip6
->ip6_dst
, &sa6_src
, NULL
, NULL
);
2047 bzero(&sa6_dst
, sizeof(sa6_dst
));
2048 sa6_dst
.sin6_family
= AF_INET6
;
2049 sa6_dst
.sin6_len
= sizeof(sa6_dst
);
2050 sa6_dst
.sin6_addr
= t
;
2051 in6_recoverscope(&sa6_dst
, &t
, m
->m_pkthdr
.rcvif
);
2052 in6_embedscope(&t
, &sa6_dst
, NULL
, NULL
);
2054 #ifdef COMPAT_RFC1885
2057 * RFC 1885 requires that echo reply should be truncated if it
2058 * does not fit in with (return) path MTU, but the description was
2059 * removed in the new spec.
2061 if (icmp6_reflect_rt
.ro_rt
== 0 ||
2062 ! (IN6_ARE_ADDR_EQUAL(&sin6
->sin6_addr
, &ip6
->ip6_dst
))) {
2063 if (icmp6_reflect_rt
.ro_rt
) {
2064 rtfree(icmp6_reflect_rt
.ro_rt
);
2065 icmp6_reflect_rt
.ro_rt
= 0;
2067 bzero(sin6
, sizeof(*sin6
));
2068 sin6
->sin6_family
= PF_INET6
;
2069 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2070 sin6
->sin6_addr
= ip6
->ip6_dst
;
2072 rtalloc_ign((struct route
*)&icmp6_reflect_rt
.ro_rt
,
2076 if (icmp6_reflect_rt
.ro_rt
== 0)
2079 if ((icmp6_reflect_rt
.ro_rt
->rt_flags
& RTF_HOST
)
2080 && mtu
< icmp6_reflect_rt
.ro_rt
->rt_ifp
->if_mtu
)
2081 mtu
= icmp6_reflect_rt
.ro_rt
->rt_rmx
.rmx_mtu
;
2083 if (mtu
< m
->m_pkthdr
.len
) {
2084 plen
-= (m
->m_pkthdr
.len
- mtu
);
2085 m_adj(m
, mtu
- m
->m_pkthdr
.len
);
2089 * If the incoming packet was addressed directly to us(i.e. unicast),
2090 * use dst as the src for the reply.
2091 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2092 * (for example) when we encounter an error while forwarding procedure
2093 * destined to a duplicated address of ours.
2095 lck_mtx_lock(nd6_mutex
);
2096 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
)
2097 if (IN6_ARE_ADDR_EQUAL(&t
, &ia
->ia_addr
.sin6_addr
) &&
2098 (ia
->ia6_flags
& (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
)) == 0) {
2102 lck_mtx_unlock(nd6_mutex
);
2103 if (ia
== NULL
&& IN6_IS_ADDR_LINKLOCAL(&t
) && (m
->m_flags
& M_LOOP
)) {
2105 * This is the case if the dst is our link-local address
2106 * and the sender is also ourselves.
2113 struct route_in6 ro
;
2116 * This case matches to multicasts, our anycast, or unicasts
2117 * that we do not own. Select a source address based on the
2118 * source address of the erroneous packet.
2120 bzero(&ro
, sizeof(ro
));
2121 src
= in6_selectsrc(&sa6_src
, NULL
, NULL
, &ro
, NULL
, &src_storage
, &e
);
2123 rtfree(ro
.ro_rt
); /* XXX: we could use this */
2126 "icmp6_reflect: source can't be determined: "
2127 "dst=%s, error=%d\n",
2128 ip6_sprintf(&sa6_src
.sin6_addr
), e
));
2133 ip6
->ip6_src
= *src
;
2136 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2137 ip6
->ip6_vfc
|= IPV6_VERSION
;
2138 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2139 if (m
->m_pkthdr
.rcvif
) {
2140 /* XXX: This may not be the outgoing interface */
2141 ip6
->ip6_hlim
= nd_ifinfo
[m
->m_pkthdr
.rcvif
->if_index
].chlim
;
2143 ip6
->ip6_hlim
= ip6_defhlim
;
2145 icmp6
->icmp6_cksum
= 0;
2146 icmp6
->icmp6_cksum
= in6_cksum(m
, IPPROTO_ICMPV6
,
2147 sizeof(struct ip6_hdr
), plen
);
2150 * XXX option handling
2153 m
->m_flags
&= ~(M_BCAST
|M_MCAST
);
2155 /* Don't lookup socket */
2156 if (ipsec_bypass
== 0)
2157 (void)ipsec_setsocket(m
, NULL
);
2160 #ifdef COMPAT_RFC1885
2161 ip6_output(m
, NULL
, &icmp6_reflect_rt
, 0, NULL
, &outif
, 0);
2163 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
, 0);
2166 icmp6_ifoutstat_inc(outif
, type
, code
);
2183 icmp6_redirect_diag(src6
, dst6
, tgt6
)
2184 struct in6_addr
*src6
;
2185 struct in6_addr
*dst6
;
2186 struct in6_addr
*tgt6
;
2188 static char buf
[1024];
2189 snprintf(buf
, sizeof(buf
), "(src=%s dst=%s tgt=%s)",
2190 ip6_sprintf(src6
), ip6_sprintf(dst6
), ip6_sprintf(tgt6
));
2195 icmp6_redirect_input(m
, off
)
2199 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2200 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
2201 struct nd_redirect
*nd_rd
;
2202 int icmp6len
= ntohs(ip6
->ip6_plen
);
2203 char *lladdr
= NULL
;
2205 u_char
*redirhdr
= NULL
;
2206 int redirhdrlen
= 0;
2207 struct rtentry
*rt
= NULL
;
2210 struct in6_addr src6
= ip6
->ip6_src
;
2211 struct in6_addr redtgt6
;
2212 struct in6_addr reddst6
;
2213 union nd_opts ndopts
;
2218 /* XXX if we are router, we don't update route by icmp6 redirect */
2221 if (!icmp6_rediraccept
)
2224 #ifndef PULLDOWN_TEST
2225 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
2226 nd_rd
= (struct nd_redirect
*)((caddr_t
)ip6
+ off
);
2228 IP6_EXTHDR_GET(nd_rd
, struct nd_redirect
*, m
, off
, icmp6len
);
2229 if (nd_rd
== NULL
) {
2230 icmp6stat
.icp6s_tooshort
++;
2234 redtgt6
= nd_rd
->nd_rd_target
;
2235 reddst6
= nd_rd
->nd_rd_dst
;
2237 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2238 redtgt6
.s6_addr16
[1] = htons(ifp
->if_index
);
2239 if (IN6_IS_ADDR_LINKLOCAL(&reddst6
))
2240 reddst6
.s6_addr16
[1] = htons(ifp
->if_index
);
2243 if (!IN6_IS_ADDR_LINKLOCAL(&src6
)) {
2245 "ICMP6 redirect sent from %s rejected; "
2246 "must be from linklocal\n", ip6_sprintf(&src6
)));
2249 if (ip6
->ip6_hlim
!= 255) {
2251 "ICMP6 redirect sent from %s rejected; "
2252 "hlim=%d (must be 255)\n",
2253 ip6_sprintf(&src6
), ip6
->ip6_hlim
));
2257 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2258 struct sockaddr_in6 sin6
;
2259 struct in6_addr
*gw6
;
2261 bzero(&sin6
, sizeof(sin6
));
2262 sin6
.sin6_family
= AF_INET6
;
2263 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2264 bcopy(&reddst6
, &sin6
.sin6_addr
, sizeof(reddst6
));
2265 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0UL);
2267 if (rt
->rt_gateway
== NULL
||
2268 rt
->rt_gateway
->sa_family
!= AF_INET6
) {
2270 "ICMP6 redirect rejected; no route "
2271 "with inet6 gateway found for redirect dst: %s\n",
2272 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2277 gw6
= &(((struct sockaddr_in6
*)rt
->rt_gateway
)->sin6_addr
);
2278 if (bcmp(&src6
, gw6
, sizeof(struct in6_addr
)) != 0) {
2280 "ICMP6 redirect rejected; "
2281 "not equal to gw-for-src=%s (must be same): "
2284 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2290 "ICMP6 redirect rejected; "
2291 "no route found for redirect dst: %s\n",
2292 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2298 if (IN6_IS_ADDR_MULTICAST(&reddst6
)) {
2300 "ICMP6 redirect rejected; "
2301 "redirect dst must be unicast: %s\n",
2302 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2306 is_router
= is_onlink
= 0;
2307 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6
))
2308 is_router
= 1; /* router case */
2309 if (bcmp(&redtgt6
, &reddst6
, sizeof(redtgt6
)) == 0)
2310 is_onlink
= 1; /* on-link destination case */
2311 if (!is_router
&& !is_onlink
) {
2313 "ICMP6 redirect rejected; "
2314 "neither router case nor onlink case: %s\n",
2315 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2318 /* validation passed */
2320 icmp6len
-= sizeof(*nd_rd
);
2321 nd6_option_init(nd_rd
+ 1, icmp6len
, &ndopts
);
2322 if (nd6_options(&ndopts
) < 0) {
2323 nd6log((LOG_INFO
, "icmp6_redirect_input: "
2324 "invalid ND option, rejected: %s\n",
2325 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2326 /* nd6_options have incremented stats */
2330 if (ndopts
.nd_opts_tgt_lladdr
) {
2331 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
2332 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
2335 if (ndopts
.nd_opts_rh
) {
2336 redirhdrlen
= ndopts
.nd_opts_rh
->nd_opt_rh_len
;
2337 redirhdr
= (u_char
*)(ndopts
.nd_opts_rh
+ 1); /* xxx */
2340 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
2342 "icmp6_redirect_input: lladdrlen mismatch for %s "
2343 "(if %d, icmp6 packet %d): %s\n",
2344 ip6_sprintf(&redtgt6
), ifp
->if_addrlen
, lladdrlen
- 2,
2345 icmp6_redirect_diag(&src6
, &reddst6
, &redtgt6
)));
2350 nd6_cache_lladdr(ifp
, &redtgt6
, lladdr
, lladdrlen
, ND_REDIRECT
,
2351 is_onlink
? ND_REDIRECT_ONLINK
: ND_REDIRECT_ROUTER
);
2353 if (!is_onlink
) { /* better router case. perform rtredirect. */
2354 /* perform rtredirect */
2355 struct sockaddr_in6 sdst
;
2356 struct sockaddr_in6 sgw
;
2357 struct sockaddr_in6 ssrc
;
2359 bzero(&sdst
, sizeof(sdst
));
2360 bzero(&sgw
, sizeof(sgw
));
2361 bzero(&ssrc
, sizeof(ssrc
));
2362 sdst
.sin6_family
= sgw
.sin6_family
= ssrc
.sin6_family
= AF_INET6
;
2363 sdst
.sin6_len
= sgw
.sin6_len
= ssrc
.sin6_len
=
2364 sizeof(struct sockaddr_in6
);
2365 bcopy(&redtgt6
, &sgw
.sin6_addr
, sizeof(struct in6_addr
));
2366 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2367 bcopy(&src6
, &ssrc
.sin6_addr
, sizeof(struct in6_addr
));
2368 rtredirect((struct sockaddr
*)&sdst
, (struct sockaddr
*)&sgw
,
2369 (struct sockaddr
*)NULL
, RTF_GATEWAY
| RTF_HOST
,
2370 (struct sockaddr
*)&ssrc
,
2371 (struct rtentry
**)NULL
);
2373 /* finally update cached route in each socket via pfctlinput */
2375 struct sockaddr_in6 sdst
;
2377 bzero(&sdst
, sizeof(sdst
));
2378 sdst
.sin6_family
= AF_INET6
;
2379 sdst
.sin6_len
= sizeof(struct sockaddr_in6
);
2380 bcopy(&reddst6
, &sdst
.sin6_addr
, sizeof(struct in6_addr
));
2381 pfctlinput(PRC_REDIRECT_HOST
, (struct sockaddr
*)&sdst
);
2383 key_sa_routechange((struct sockaddr
*)&sdst
);
2392 icmp6stat
.icp6s_badredirect
++;
2397 icmp6_redirect_output(m0
, rt
)
2401 struct ifnet
*ifp
; /* my outgoing interface */
2402 struct in6_addr ifp_ll6
;
2403 struct in6_addr
*router_ll6
;
2404 struct ip6_hdr
*sip6
; /* m0 as struct ip6_hdr */
2405 struct mbuf
*m
= NULL
; /* newly allocated one */
2406 struct ip6_hdr
*ip6
; /* m as struct ip6_hdr */
2407 struct nd_redirect
*nd_rd
;
2410 struct ifnet
*outif
= NULL
;
2411 struct sockaddr_in6 src_sa
;
2413 icmp6_errcount(&icmp6stat
.icp6s_outerrhist
, ND_REDIRECT
, 0);
2416 if (!m0
|| !rt
|| !(rt
->rt_flags
& RTF_UP
) || !(ifp
= rt
->rt_ifp
))
2419 /* if we are not router, we don't send icmp6 redirect */
2420 if (!ip6_forwarding
|| ip6_accept_rtadv
|| (ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))
2425 * the source address must identify a neighbor, and
2426 * the destination address must not be a multicast address
2427 * [RFC 2461, sec 8.2]
2429 sip6
= mtod(m0
, struct ip6_hdr
*);
2430 bzero(&src_sa
, sizeof(src_sa
));
2431 src_sa
.sin6_family
= AF_INET6
;
2432 src_sa
.sin6_len
= sizeof(src_sa
);
2433 src_sa
.sin6_addr
= sip6
->ip6_src
;
2434 /* we don't currently use sin6_scope_id, but eventually use it */
2435 src_sa
.sin6_scope_id
= in6_addr2scopeid(ifp
, &sip6
->ip6_src
);
2436 if (nd6_is_addr_neighbor(&src_sa
, ifp
, 0) == 0)
2438 if (IN6_IS_ADDR_MULTICAST(&sip6
->ip6_dst
))
2439 goto fail
; /* what should we do here? */
2442 if (icmp6_ratelimit(&sip6
->ip6_src
, ND_REDIRECT
, 0))
2446 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2447 * we almost always ask for an mbuf cluster for simplicity.
2448 * (MHLEN < IPV6_MMTU is almost always true)
2450 #if IPV6_MMTU >= MCLBYTES
2451 # error assumption failed about IPV6_MMTU and MCLBYTES
2453 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
2454 if (m
&& IPV6_MMTU
>= MHLEN
)
2455 MCLGET(m
, M_DONTWAIT
);
2458 m
->m_pkthdr
.rcvif
= NULL
;
2460 maxlen
= M_TRAILINGSPACE(m
);
2461 maxlen
= min(IPV6_MMTU
, maxlen
);
2462 /* just for safety */
2463 if (maxlen
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
) +
2464 ((sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7)) {
2469 /* get ip6 linklocal address for ifp(my outgoing interface). */
2470 struct in6_ifaddr
*ia
;
2471 if ((ia
= in6ifa_ifpforlinklocal(ifp
,
2473 IN6_IFF_ANYCAST
)) == NULL
)
2475 ifp_ll6
= ia
->ia_addr
.sin6_addr
;
2476 ifafree(&ia
->ia_ifa
);
2479 /* get ip6 linklocal address for the router. */
2480 if (rt
->rt_gateway
&& (rt
->rt_flags
& RTF_GATEWAY
)) {
2481 struct sockaddr_in6
*sin6
;
2482 sin6
= (struct sockaddr_in6
*)rt
->rt_gateway
;
2483 router_ll6
= &sin6
->sin6_addr
;
2484 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6
))
2485 router_ll6
= (struct in6_addr
*)NULL
;
2487 router_ll6
= (struct in6_addr
*)NULL
;
2490 ip6
= mtod(m
, struct ip6_hdr
*);
2492 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2493 ip6
->ip6_vfc
|= IPV6_VERSION
;
2494 /* ip6->ip6_plen will be set later */
2495 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2496 ip6
->ip6_hlim
= 255;
2497 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2498 bcopy(&ifp_ll6
, &ip6
->ip6_src
, sizeof(struct in6_addr
));
2499 bcopy(&sip6
->ip6_src
, &ip6
->ip6_dst
, sizeof(struct in6_addr
));
2502 nd_rd
= (struct nd_redirect
*)(ip6
+ 1);
2503 nd_rd
->nd_rd_type
= ND_REDIRECT
;
2504 nd_rd
->nd_rd_code
= 0;
2505 nd_rd
->nd_rd_reserved
= 0;
2506 if (rt
->rt_flags
& RTF_GATEWAY
) {
2508 * nd_rd->nd_rd_target must be a link-local address in
2509 * better router cases.
2513 bcopy(router_ll6
, &nd_rd
->nd_rd_target
,
2514 sizeof(nd_rd
->nd_rd_target
));
2515 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2516 sizeof(nd_rd
->nd_rd_dst
));
2518 /* make sure redtgt == reddst */
2519 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_target
,
2520 sizeof(nd_rd
->nd_rd_target
));
2521 bcopy(&sip6
->ip6_dst
, &nd_rd
->nd_rd_dst
,
2522 sizeof(nd_rd
->nd_rd_dst
));
2525 p
= (u_char
*)(nd_rd
+ 1);
2531 /* target lladdr option */
2532 struct rtentry
*rt_router
= NULL
;
2534 struct sockaddr_dl
*sdl
;
2535 struct nd_opt_hdr
*nd_opt
;
2538 rt_router
= nd6_lookup(router_ll6
, 0, ifp
, 0);
2541 len
= sizeof(*nd_opt
) + ifp
->if_addrlen
;
2542 len
= (len
+ 7) & ~7; /* round by 8 */
2544 if (len
+ (p
- (u_char
*)ip6
) > maxlen
)
2546 if (!(rt_router
->rt_flags
& RTF_GATEWAY
) &&
2547 (rt_router
->rt_flags
& RTF_LLINFO
) &&
2548 (rt_router
->rt_gateway
->sa_family
== AF_LINK
) &&
2549 (sdl
= (struct sockaddr_dl
*)rt_router
->rt_gateway
) &&
2551 nd_opt
= (struct nd_opt_hdr
*)p
;
2552 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
2553 nd_opt
->nd_opt_len
= len
>> 3;
2554 lladdr
= (char *)(nd_opt
+ 1);
2555 bcopy(LLADDR(sdl
), lladdr
, ifp
->if_addrlen
);
2561 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2563 /* just to be safe */
2564 #ifdef M_DECRYPTED /*not openbsd*/
2565 if (m0
->m_flags
& M_DECRYPTED
)
2568 if (p
- (u_char
*)ip6
> maxlen
)
2572 /* redirected header option */
2574 struct nd_opt_rd_hdr
*nd_opt_rh
;
2577 * compute the maximum size for icmp6 redirect header option.
2578 * XXX room for auth header?
2580 len
= maxlen
- (p
- (u_char
*)ip6
);
2583 /* This is just for simplicity. */
2584 if (m0
->m_pkthdr
.len
!= m0
->m_len
) {
2586 m_freem(m0
->m_next
);
2589 m0
->m_pkthdr
.len
= m0
->m_len
;
2593 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2594 * about padding/truncate rule for the original IP packet.
2595 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2596 * - "attach as much as possible" is the goal
2597 * - pad if not aligned (original size can be guessed by original
2599 * Following code adds the padding if it is simple enough,
2600 * and truncates if not.
2602 if (m0
->m_next
|| m0
->m_pkthdr
.len
!= m0
->m_len
)
2603 panic("assumption failed in %s:%d\n", __FILE__
, __LINE__
);
2605 if (len
- sizeof(*nd_opt_rh
) < m0
->m_pkthdr
.len
) {
2606 /* not enough room, truncate */
2607 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2609 /* enough room, pad or truncate */
2612 extra
= m0
->m_pkthdr
.len
% 8;
2614 /* pad if easy enough, truncate if not */
2615 if (8 - extra
<= M_TRAILINGSPACE(m0
)) {
2617 m0
->m_len
+= (8 - extra
);
2618 m0
->m_pkthdr
.len
+= (8 - extra
);
2621 m0
->m_pkthdr
.len
-= extra
;
2625 len
= m0
->m_pkthdr
.len
+ sizeof(*nd_opt_rh
);
2626 m0
->m_pkthdr
.len
= m0
->m_len
= len
- sizeof(*nd_opt_rh
);
2629 nd_opt_rh
= (struct nd_opt_rd_hdr
*)p
;
2630 bzero(nd_opt_rh
, sizeof(*nd_opt_rh
));
2631 nd_opt_rh
->nd_opt_rh_type
= ND_OPT_REDIRECTED_HEADER
;
2632 nd_opt_rh
->nd_opt_rh_len
= len
>> 3;
2633 p
+= sizeof(*nd_opt_rh
);
2634 m
->m_pkthdr
.len
= m
->m_len
= p
- (u_char
*)ip6
;
2636 /* connect m0 to m */
2638 m
->m_pkthdr
.len
= m
->m_len
+ m0
->m_len
;
2642 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_src
))
2643 sip6
->ip6_src
.s6_addr16
[1] = 0;
2644 if (IN6_IS_ADDR_LINKLOCAL(&sip6
->ip6_dst
))
2645 sip6
->ip6_dst
.s6_addr16
[1] = 0;
2647 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_src
))
2648 ip6
->ip6_src
.s6_addr16
[1] = 0;
2649 if (IN6_IS_ADDR_LINKLOCAL(&ip6
->ip6_dst
))
2650 ip6
->ip6_dst
.s6_addr16
[1] = 0;
2652 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_target
))
2653 nd_rd
->nd_rd_target
.s6_addr16
[1] = 0;
2654 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd
->nd_rd_dst
))
2655 nd_rd
->nd_rd_dst
.s6_addr16
[1] = 0;
2657 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
2659 nd_rd
->nd_rd_cksum
= 0;
2661 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), ntohs(ip6
->ip6_plen
));
2663 /* send the packet to outside... */
2665 /* Don't lookup socket */
2666 if (ipsec_bypass
== 0)
2667 (void)ipsec_setsocket(m
, NULL
);
2669 ip6_output(m
, NULL
, NULL
, 0, NULL
, &outif
, 0);
2671 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
2672 icmp6_ifstat_inc(outif
, ifs6_out_redirect
);
2674 icmp6stat
.icp6s_outhist
[ND_REDIRECT
]++;
2686 #define sotoin6pcb sotoinpcb
2687 #define in6pcb inpcb
2688 #define in6p_icmp6filt inp_icmp6filt
2691 * ICMPv6 socket option processing.
2694 icmp6_ctloutput(so
, sopt
)
2696 struct sockopt
*sopt
;
2700 struct inpcb
*inp
= sotoinpcb(so
);
2701 int level
, op
, optname
;
2704 level
= sopt
->sopt_level
;
2705 op
= sopt
->sopt_dir
;
2706 optname
= sopt
->sopt_name
;
2707 optlen
= sopt
->sopt_valsize
;
2709 level
= op
= optname
= optlen
= 0;
2711 if (level
!= IPPROTO_ICMPV6
) {
2720 struct icmp6_filter
*p
;
2722 if (optlen
!= sizeof(*p
)) {
2726 if (inp
->in6p_icmp6filt
== NULL
) {
2730 error
= sooptcopyin(sopt
, inp
->in6p_icmp6filt
, optlen
,
2736 error
= ENOPROTOOPT
;
2745 if (inp
->in6p_icmp6filt
== NULL
) {
2749 error
= sooptcopyout(sopt
, inp
->in6p_icmp6filt
,
2750 sizeof(struct icmp6_filter
));
2755 error
= ENOPROTOOPT
;
2766 #undef in6p_icmp6filt
2769 #ifndef HAVE_PPSRATECHECK
2771 #define timersub(tvp, uvp, vvp) \
2773 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2774 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2775 if ((vvp)->tv_usec < 0) { \
2777 (vvp)->tv_usec += 1000000; \
2783 * ppsratecheck(): packets (or events) per second limitation.
2786 ppsratecheck(lasttime
, curpps
, maxpps
)
2787 struct timeval
*lasttime
;
2789 int maxpps
; /* maximum pps allowed */
2791 struct timeval tv
, delta
;
2798 timersub(&tv
, lasttime
, &delta
);
2801 * Check for 0,0 so that the message will be seen at least once.
2802 * If more than one second has passed since the last update of
2803 * lasttime, reset the counter.
2805 * we do increment *curpps even in *curpps < maxpps case, as some may
2806 * try to use *curpps for stat purposes as well.
2808 if ((lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0) ||
2809 delta
.tv_sec
>= 1) {
2813 } else if (maxpps
< 0)
2815 else if (*curpps
< maxpps
)
2820 #if 1 /* DIAGNOSTIC? */
2821 /* be careful about wrap-around */
2822 if (*curpps
+ 1 > *curpps
)
2823 *curpps
= *curpps
+ 1;
2826 * assume that there's not too many calls to this function.
2827 * not sure if the assumption holds, as it depends on *caller's*
2828 * behavior, not the behavior of this function.
2829 * IMHO it is wrong to make assumption on the caller's behavior,
2830 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
2832 *curpps
= *curpps
+ 1;
2840 * Perform rate limit check.
2841 * Returns 0 if it is okay to send the icmp6 packet.
2842 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2845 * XXX per-destination/type check necessary?
2848 icmp6_ratelimit(dst
, type
, code
)
2849 const struct in6_addr
*dst
; /* not used at this moment */
2850 const int type
; /* not used at this moment */
2851 const int code
; /* not used at this moment */
2855 ret
= 0; /* okay to send */
2858 if (!ppsratecheck(&icmp6errppslim_last
, &icmp6errpps_count
,
2860 /* The packet is subject to rate limit */