GC old tcsh version.
[dragonfly.git] / sys / netinet6 / icmp6.c
blob1aaaf8d4ce6162e2734acf796ec7efb2dbe3418b
1 /* $FreeBSD: src/sys/netinet6/icmp6.c,v 1.6.2.13 2003/05/06 06:46:58 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/icmp6.c,v 1.26 2007/04/22 01:13:14 dillon Exp $ */
3 /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
66 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/time.h>
81 #include <sys/kernel.h>
82 #include <sys/syslog.h>
83 #include <sys/domain.h>
85 #include <net/if.h>
86 #include <net/route.h>
87 #include <net/if_dl.h>
88 #include <net/if_types.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet/icmp6.h>
95 #include <netinet6/mld6_var.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet6/nd6.h>
99 #include <netinet6/in6_ifattach.h>
100 #include <netinet6/ip6protosw.h>
102 #ifdef IPSEC
103 #include <netinet6/ipsec.h>
104 #include <netproto/key/key.h>
105 #endif
107 #ifdef FAST_IPSEC
108 #include <netproto/ipsec/ipsec.h>
109 #include <netproto/ipsec/key.h>
110 #define IPSEC
111 #endif
113 #include <net/net_osdep.h>
115 #ifdef HAVE_NRL_INPCB
116 /* inpcb members */
117 #define in6pcb inpcb
118 #define in6p_laddr inp_laddr6
119 #define in6p_faddr inp_faddr6
120 #define in6p_icmp6filt inp_icmp6filt
121 #define in6p_route inp_route
122 #define in6p_socket inp_socket
123 #define in6p_flags inp_flags
124 #define in6p_moptions inp_moptions6
125 #define in6p_outputopts inp_outputopts6
126 #define in6p_ip6 inp_ipv6
127 #define in6p_flowinfo inp_flowinfo
128 #define in6p_sp inp_sp
129 #define in6p_next inp_next
130 #define in6p_prev inp_prev
131 /* function names */
132 #define in6_pcbdetach in_pcbdetach
133 #define in6_rtchange in_rtchange
136 * for KAME src sync over BSD*'s. XXX: FreeBSD (>=3) are VERY different from
137 * others...
139 #define in6p_ip6_nxt inp_ipv6.ip6_nxt
140 #endif
143 * ppratecheck() is available, so define it here.
145 #define HAVE_PPSRATECHECK
147 extern struct domain inet6domain;
148 extern struct ip6protosw inet6sw[];
149 extern u_char ip6_protox[];
151 struct icmp6stat icmp6stat;
153 extern struct inpcbinfo ripcbinfo;
154 extern int icmp6errppslim;
155 static int icmp6errpps_count = 0;
156 static struct timeval icmp6errppslim_last;
157 extern int icmp6_nodeinfo;
159 static void icmp6_errcount (struct icmp6errstat *, int, int);
160 static int icmp6_rip6_input (struct mbuf **, int);
161 static int icmp6_ratelimit (const struct in6_addr *, const int, const int);
162 static const char *icmp6_redirect_diag (struct in6_addr *,
163 struct in6_addr *, struct in6_addr *);
164 #ifndef HAVE_PPSRATECHECK
165 static int ppsratecheck (struct timeval *, int *, int);
166 #endif
167 static struct mbuf *ni6_input (struct mbuf *, int);
168 static struct mbuf *ni6_nametodns (const char *, int, int);
169 static int ni6_dnsmatch (const char *, int, const char *, int);
170 static int ni6_addrs (struct icmp6_nodeinfo *, struct mbuf *,
171 struct ifnet **, char *);
172 static int ni6_store_addrs (struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
173 struct ifnet *, int);
174 static int icmp6_notify_error (struct mbuf *, int, int, int);
176 #ifdef COMPAT_RFC1885
177 static struct route_in6 icmp6_reflect_rt;
178 #endif
181 void
182 icmp6_init(void)
184 mld6_init();
187 static void
188 icmp6_errcount(struct icmp6errstat *stat, int type, int code)
190 switch (type) {
191 case ICMP6_DST_UNREACH:
192 switch (code) {
193 case ICMP6_DST_UNREACH_NOROUTE:
194 stat->icp6errs_dst_unreach_noroute++;
195 return;
196 case ICMP6_DST_UNREACH_ADMIN:
197 stat->icp6errs_dst_unreach_admin++;
198 return;
199 case ICMP6_DST_UNREACH_BEYONDSCOPE:
200 stat->icp6errs_dst_unreach_beyondscope++;
201 return;
202 case ICMP6_DST_UNREACH_ADDR:
203 stat->icp6errs_dst_unreach_addr++;
204 return;
205 case ICMP6_DST_UNREACH_NOPORT:
206 stat->icp6errs_dst_unreach_noport++;
207 return;
209 break;
210 case ICMP6_PACKET_TOO_BIG:
211 stat->icp6errs_packet_too_big++;
212 return;
213 case ICMP6_TIME_EXCEEDED:
214 switch (code) {
215 case ICMP6_TIME_EXCEED_TRANSIT:
216 stat->icp6errs_time_exceed_transit++;
217 return;
218 case ICMP6_TIME_EXCEED_REASSEMBLY:
219 stat->icp6errs_time_exceed_reassembly++;
220 return;
222 break;
223 case ICMP6_PARAM_PROB:
224 switch (code) {
225 case ICMP6_PARAMPROB_HEADER:
226 stat->icp6errs_paramprob_header++;
227 return;
228 case ICMP6_PARAMPROB_NEXTHEADER:
229 stat->icp6errs_paramprob_nextheader++;
230 return;
231 case ICMP6_PARAMPROB_OPTION:
232 stat->icp6errs_paramprob_option++;
233 return;
235 break;
236 case ND_REDIRECT:
237 stat->icp6errs_redirect++;
238 return;
240 stat->icp6errs_unknown++;
244 * Generate an error packet of type error in response to bad IP6 packet.
246 void
247 icmp6_error(struct mbuf *m, int type, int code, int param)
249 struct ip6_hdr *oip6, *nip6;
250 struct icmp6_hdr *icmp6;
251 u_int preplen;
252 int off;
253 int nxt;
255 icmp6stat.icp6s_error++;
257 /* count per-type-code statistics */
258 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
260 #ifdef M_DECRYPTED /*not openbsd*/
261 if (m->m_flags & M_DECRYPTED) {
262 icmp6stat.icp6s_canterror++;
263 goto freeit;
265 #endif
267 #ifndef PULLDOWN_TEST
268 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
269 #else
270 if (m->m_len < sizeof(struct ip6_hdr)) {
271 m = m_pullup(m, sizeof(struct ip6_hdr));
272 if (m == NULL)
273 return;
275 #endif
276 oip6 = mtod(m, struct ip6_hdr *);
279 * Multicast destination check. For unrecognized option errors,
280 * this check has already done in ip6_unknown_opt(), so we can
281 * check only for other errors.
283 if ((m->m_flags & (M_BCAST|M_MCAST) ||
284 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
285 (type != ICMP6_PACKET_TOO_BIG &&
286 (type != ICMP6_PARAM_PROB ||
287 code != ICMP6_PARAMPROB_OPTION)))
288 goto freeit;
290 /* Source address check. XXX: the case of anycast source? */
291 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
292 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
293 goto freeit;
296 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
297 * don't do it.
299 nxt = -1;
300 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
301 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
302 struct icmp6_hdr *icp;
304 #ifndef PULLDOWN_TEST
305 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
306 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
307 #else
308 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
309 sizeof(*icp));
310 if (icp == NULL) {
311 icmp6stat.icp6s_tooshort++;
312 return;
314 #endif
315 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
316 icp->icmp6_type == ND_REDIRECT) {
318 * ICMPv6 error
319 * Special case: for redirect (which is
320 * informational) we must not send icmp6 error.
322 icmp6stat.icp6s_canterror++;
323 goto freeit;
324 } else {
325 /* ICMPv6 informational - send the error */
327 } else {
328 /* non-ICMPv6 - send the error */
331 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
333 /* Finally, do rate limitation check. */
334 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
335 icmp6stat.icp6s_toofreq++;
336 goto freeit;
340 * OK, ICMP6 can be generated.
343 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
344 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
346 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
347 M_PREPEND(m, preplen, MB_DONTWAIT);
348 if (m && m->m_len < preplen)
349 m = m_pullup(m, preplen);
350 if (m == NULL) {
351 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
352 return;
355 nip6 = mtod(m, struct ip6_hdr *);
356 nip6->ip6_src = oip6->ip6_src;
357 nip6->ip6_dst = oip6->ip6_dst;
359 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
360 oip6->ip6_src.s6_addr16[1] = 0;
361 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
362 oip6->ip6_dst.s6_addr16[1] = 0;
364 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
365 icmp6->icmp6_type = type;
366 icmp6->icmp6_code = code;
367 icmp6->icmp6_pptr = htonl((u_int32_t)param);
370 * icmp6_reflect() is designed to be in the input path.
371 * icmp6_error() can be called from both input and outut path,
372 * and if we are in output path rcvif could contain bogus value.
373 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
374 * information in ip header (nip6).
376 m->m_pkthdr.rcvif = NULL;
378 icmp6stat.icp6s_outhist[type]++;
379 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
381 return;
383 freeit:
385 * If we can't tell wheter or not we can generate ICMP6, free it.
387 m_freem(m);
391 * Process a received ICMP6 message.
394 icmp6_input(struct mbuf **mp, int *offp, int proto)
396 struct mbuf *m = *mp, *n;
397 struct ip6_hdr *ip6, *nip6;
398 struct icmp6_hdr *icmp6, *nicmp6;
399 int off = *offp;
400 int icmp6len = m->m_pkthdr.len - *offp;
401 int code, sum, noff;
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 */
406 #endif
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++;
416 goto freeit;
420 * calculate the checksum
422 #ifndef PULLDOWN_TEST
423 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
424 #else
425 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
426 if (icmp6 == NULL) {
427 icmp6stat.icp6s_tooshort++;
428 return IPPROTO_DONE;
430 #endif
431 code = icmp6->icmp6_code;
433 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
434 nd6log((LOG_ERR,
435 "ICMP6 checksum error(%d|%x) %s\n",
436 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
437 icmp6stat.icp6s_checksum++;
438 goto freeit;
441 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
443 * Deliver very specific ICMP6 type only.
444 * This is important to deilver TOOBIG. Otherwise PMTUD
445 * will not work.
447 switch (icmp6->icmp6_type) {
448 case ICMP6_DST_UNREACH:
449 case ICMP6_PACKET_TOO_BIG:
450 case ICMP6_TIME_EXCEEDED:
451 break;
452 default:
453 goto freeit;
457 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
458 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
459 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
460 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
462 switch (icmp6->icmp6_type) {
463 case ICMP6_DST_UNREACH:
464 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
465 switch (code) {
466 case ICMP6_DST_UNREACH_NOROUTE:
467 code = PRC_UNREACH_NET;
468 break;
469 case ICMP6_DST_UNREACH_ADMIN:
470 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
471 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
472 break;
473 case ICMP6_DST_UNREACH_ADDR:
474 code = PRC_HOSTDEAD;
475 break;
476 #ifdef COMPAT_RFC1885
477 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
478 code = PRC_UNREACH_SRCFAIL;
479 break;
480 #else
481 case ICMP6_DST_UNREACH_BEYONDSCOPE:
482 /* I mean "source address was incorrect." */
483 code = PRC_PARAMPROB;
484 break;
485 #endif
486 case ICMP6_DST_UNREACH_NOPORT:
487 code = PRC_UNREACH_PORT;
488 break;
489 default:
490 goto badcode;
492 goto deliver;
493 break;
495 case ICMP6_PACKET_TOO_BIG:
496 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
497 if (code != 0)
498 goto badcode;
500 code = PRC_MSGSIZE;
503 * Updating the path MTU will be done after examining
504 * intermediate extension headers.
506 goto deliver;
507 break;
509 case ICMP6_TIME_EXCEEDED:
510 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
511 switch (code) {
512 case ICMP6_TIME_EXCEED_TRANSIT:
513 case ICMP6_TIME_EXCEED_REASSEMBLY:
514 code += PRC_TIMXCEED_INTRANS;
515 break;
516 default:
517 goto badcode;
519 goto deliver;
520 break;
522 case ICMP6_PARAM_PROB:
523 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
524 switch (code) {
525 case ICMP6_PARAMPROB_NEXTHEADER:
526 code = PRC_UNREACH_PROTOCOL;
527 break;
528 case ICMP6_PARAMPROB_HEADER:
529 case ICMP6_PARAMPROB_OPTION:
530 code = PRC_PARAMPROB;
531 break;
532 default:
533 goto badcode;
535 goto deliver;
536 break;
538 case ICMP6_ECHO_REQUEST:
539 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
540 if (code != 0)
541 goto badcode;
542 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
543 /* Give up remote */
544 break;
546 if ((n->m_flags & M_EXT) ||
547 n->m_len < off + sizeof(struct icmp6_hdr)) {
548 struct mbuf *n0 = n;
549 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
550 int n0len;
553 * Prepare an internal mbuf. m_pullup() doesn't
554 * always copy the length we specified.
556 if (maxlen >= MCLBYTES) {
557 /* Give up remote */
558 m_freem(n0);
559 break;
561 n = m_getb(maxlen, MB_DONTWAIT, n0->m_type, M_PKTHDR);
562 if (n == NULL) {
563 /* Give up remote */
564 m_freem(n0);
565 break;
567 n0len = n0->m_pkthdr.len; /* save for use below */
568 M_MOVE_PKTHDR(n, n0);
570 * Copy IPv6 and ICMPv6 only.
572 nip6 = mtod(n, struct ip6_hdr *);
573 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
574 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
575 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
576 noff = sizeof(struct ip6_hdr);
577 /* new mbuf contains only ipv6+icmpv6 headers */
578 n->m_len = noff + sizeof(struct icmp6_hdr);
580 * Adjust mbuf. ip6_plen will be adjusted in
581 * ip6_output().
583 m_adj(n0, off + sizeof(struct icmp6_hdr));
584 /* recalculate complete packet size */
585 n->m_pkthdr.len = n0len + (noff - off);
586 n->m_next = n0;
587 } else {
588 nip6 = mtod(n, struct ip6_hdr *);
589 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
590 noff = off;
592 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
593 nicmp6->icmp6_code = 0;
594 if (n) {
595 icmp6stat.icp6s_reflect++;
596 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
597 icmp6_reflect(n, noff);
599 break;
601 case ICMP6_ECHO_REPLY:
602 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
603 if (code != 0)
604 goto badcode;
605 break;
607 case MLD_LISTENER_QUERY:
608 case MLD_LISTENER_REPORT:
609 if (icmp6len < sizeof(struct mld_hdr))
610 goto badlen;
611 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
612 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
613 else
614 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
615 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
616 /* give up local */
617 mld6_input(m, off);
618 m = NULL;
619 goto freeit;
621 mld6_input(n, off);
622 /* m stays. */
623 break;
625 case MLD_LISTENER_DONE:
626 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
627 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
628 goto badlen;
629 break; /* nothing to be done in kernel */
631 case MLD_MTRACE_RESP:
632 case MLD_MTRACE:
633 /* XXX: these two are experimental. not officially defind. */
634 /* XXX: per-interface statistics? */
635 break; /* just pass it to applications */
637 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
639 enum { WRU, FQDN } mode;
641 if (!icmp6_nodeinfo)
642 break;
644 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
645 mode = WRU;
646 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
647 mode = FQDN;
648 else
649 goto badlen;
651 #define hostnamelen strlen(hostname)
652 if (mode == FQDN) {
653 #ifndef PULLDOWN_TEST
654 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
655 IPPROTO_DONE);
656 #endif
657 n = m_copy(m, 0, M_COPYALL);
658 if (n)
659 n = ni6_input(n, off);
660 /* XXX meaningless if n == NULL */
661 noff = sizeof(struct ip6_hdr);
662 } else {
663 u_char *p;
664 int maxlen, maxhlen;
666 if ((icmp6_nodeinfo & 5) != 5)
667 break;
669 if (code != 0)
670 goto badcode;
671 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
672 if (maxlen >= MCLBYTES) {
673 /* Give up remote */
674 break;
676 n = m_getb(maxlen, MB_DONTWAIT, m->m_type, M_PKTHDR);
677 if (n == NULL) {
678 /* Give up remote */
679 break;
681 if (!m_dup_pkthdr(n, m, MB_DONTWAIT)) {
683 * Previous code did a blind M_COPY_PKTHDR
684 * and said "just for rcvif". If true, then
685 * we could tolerate the dup failing (due to
686 * the deep copy of the tag chain). For now
687 * be conservative and just fail.
689 m_free(n);
690 break;
692 n->m_len = 0;
693 maxhlen = M_TRAILINGSPACE(n) - maxlen;
694 if (maxhlen > hostnamelen)
695 maxhlen = hostnamelen;
697 * Copy IPv6 and ICMPv6 only.
699 nip6 = mtod(n, struct ip6_hdr *);
700 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
701 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
702 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
703 p = (u_char *)(nicmp6 + 1);
704 bzero(p, 4);
705 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
706 noff = sizeof(struct ip6_hdr);
707 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
708 sizeof(struct icmp6_hdr) + 4 + maxhlen;
709 nicmp6->icmp6_type = ICMP6_WRUREPLY;
710 nicmp6->icmp6_code = 0;
712 #undef hostnamelen
713 if (n) {
714 icmp6stat.icp6s_reflect++;
715 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
716 icmp6_reflect(n, noff);
718 break;
721 case ICMP6_WRUREPLY:
722 if (code != 0)
723 goto badcode;
724 break;
726 case ND_ROUTER_SOLICIT:
727 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
728 if (code != 0)
729 goto badcode;
730 if (icmp6len < sizeof(struct nd_router_solicit))
731 goto badlen;
732 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
733 /* give up local */
734 nd6_rs_input(m, off, icmp6len);
735 m = NULL;
736 goto freeit;
738 nd6_rs_input(n, off, icmp6len);
739 /* m stays. */
740 break;
742 case ND_ROUTER_ADVERT:
743 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
744 if (code != 0)
745 goto badcode;
746 if (icmp6len < sizeof(struct nd_router_advert))
747 goto badlen;
748 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
749 /* give up local */
750 nd6_ra_input(m, off, icmp6len);
751 m = NULL;
752 goto freeit;
754 nd6_ra_input(n, off, icmp6len);
755 /* m stays. */
756 break;
758 case ND_NEIGHBOR_SOLICIT:
759 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
760 if (code != 0)
761 goto badcode;
762 if (icmp6len < sizeof(struct nd_neighbor_solicit))
763 goto badlen;
764 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
765 /* give up local */
766 nd6_ns_input(m, off, icmp6len);
767 m = NULL;
768 goto freeit;
770 nd6_ns_input(n, off, icmp6len);
771 /* m stays. */
772 break;
774 case ND_NEIGHBOR_ADVERT:
775 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
776 if (code != 0)
777 goto badcode;
778 if (icmp6len < sizeof(struct nd_neighbor_advert))
779 goto badlen;
780 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
781 /* give up local */
782 nd6_na_input(m, off, icmp6len);
783 m = NULL;
784 goto freeit;
786 nd6_na_input(n, off, icmp6len);
787 /* m stays. */
788 break;
790 case ND_REDIRECT:
791 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
792 if (code != 0)
793 goto badcode;
794 if (icmp6len < sizeof(struct nd_redirect))
795 goto badlen;
796 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
797 /* give up local */
798 icmp6_redirect_input(m, off);
799 m = NULL;
800 goto freeit;
802 icmp6_redirect_input(n, off);
803 /* m stays. */
804 break;
806 case ICMP6_ROUTER_RENUMBERING:
807 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
808 code != ICMP6_ROUTER_RENUMBERING_RESULT)
809 goto badcode;
810 if (icmp6len < sizeof(struct icmp6_router_renum))
811 goto badlen;
812 break;
814 default:
815 nd6log((LOG_DEBUG,
816 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
817 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
818 ip6_sprintf(&ip6->ip6_dst),
819 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
820 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
821 /* ICMPv6 error: MUST deliver it by spec... */
822 code = PRC_NCMDS;
823 /* deliver */
824 } else {
825 /* ICMPv6 informational: MUST not deliver */
826 break;
828 deliver:
829 if (icmp6_notify_error(m, off, icmp6len, code)) {
830 /* In this case, m should've been freed. */
831 return (IPPROTO_DONE);
833 break;
835 badcode:
836 icmp6stat.icp6s_badcode++;
837 break;
839 badlen:
840 icmp6stat.icp6s_badlen++;
841 break;
844 /* deliver the packet to appropriate sockets */
845 icmp6_rip6_input(&m, *offp);
847 return IPPROTO_DONE;
849 freeit:
850 m_freem(m);
851 return IPPROTO_DONE;
854 static int
855 icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
857 struct icmp6_hdr *icmp6;
858 struct ip6_hdr *eip6;
859 u_int32_t notifymtu;
860 struct sockaddr_in6 icmp6src, icmp6dst;
862 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
863 icmp6stat.icp6s_tooshort++;
864 goto freeit;
866 #ifndef PULLDOWN_TEST
867 IP6_EXTHDR_CHECK(m, off,
868 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
869 -1);
870 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
871 #else
872 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
873 sizeof(*icmp6) + sizeof(struct ip6_hdr));
874 if (icmp6 == NULL) {
875 icmp6stat.icp6s_tooshort++;
876 return (-1);
878 #endif
879 eip6 = (struct ip6_hdr *)(icmp6 + 1);
881 /* Detect the upper level protocol */
883 void (*ctlfunc) (int, struct sockaddr *, void *);
884 u_int8_t nxt = eip6->ip6_nxt;
885 int eoff = off + sizeof(struct icmp6_hdr) +
886 sizeof(struct ip6_hdr);
887 struct ip6ctlparam ip6cp;
888 struct in6_addr *finaldst = NULL;
889 int icmp6type = icmp6->icmp6_type;
890 struct ip6_frag *fh;
891 struct ip6_rthdr *rth;
892 struct ip6_rthdr0 *rth0;
893 int rthlen;
895 while (1) { /* XXX: should avoid infinite loop explicitly? */
896 struct ip6_ext *eh;
898 switch (nxt) {
899 case IPPROTO_HOPOPTS:
900 case IPPROTO_DSTOPTS:
901 case IPPROTO_AH:
902 #ifndef PULLDOWN_TEST
903 IP6_EXTHDR_CHECK(m, 0, eoff +
904 sizeof(struct ip6_ext),
905 -1);
906 eh = (struct ip6_ext *)(mtod(m, caddr_t)
907 + eoff);
908 #else
909 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
910 eoff, sizeof(*eh));
911 if (eh == NULL) {
912 icmp6stat.icp6s_tooshort++;
913 return (-1);
915 #endif
917 if (nxt == IPPROTO_AH)
918 eoff += (eh->ip6e_len + 2) << 2;
919 else
920 eoff += (eh->ip6e_len + 1) << 3;
921 nxt = eh->ip6e_nxt;
922 break;
923 case IPPROTO_ROUTING:
925 * When the erroneous packet contains a
926 * routing header, we should examine the
927 * header to determine the final destination.
928 * Otherwise, we can't properly update
929 * information that depends on the final
930 * destination (e.g. path MTU).
932 #ifndef PULLDOWN_TEST
933 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
934 -1);
935 rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
936 + eoff);
937 #else
938 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
939 eoff, sizeof(*rth));
940 if (rth == NULL) {
941 icmp6stat.icp6s_tooshort++;
942 return (-1);
944 #endif
945 rthlen = (rth->ip6r_len + 1) << 3;
947 * XXX: currently there is no
948 * officially defined type other
949 * than type-0.
950 * Note that if the segment left field
951 * is 0, all intermediate hops must
952 * have been passed.
954 if (rth->ip6r_segleft &&
955 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
956 int hops;
958 #ifndef PULLDOWN_TEST
959 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen,
960 -1);
961 rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff);
962 #else
963 IP6_EXTHDR_GET(rth0,
964 struct ip6_rthdr0 *, m,
965 eoff, rthlen);
966 if (rth0 == NULL) {
967 icmp6stat.icp6s_tooshort++;
968 return (-1);
970 #endif
971 /* just ignore a bogus header */
972 if ((rth0->ip6r0_len % 2) == 0 &&
973 (hops = rth0->ip6r0_len/2))
974 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
976 eoff += rthlen;
977 nxt = rth->ip6r_nxt;
978 break;
979 case IPPROTO_FRAGMENT:
980 #ifndef PULLDOWN_TEST
981 IP6_EXTHDR_CHECK(m, 0, eoff +
982 sizeof(struct ip6_frag),
983 -1);
984 fh = (struct ip6_frag *)(mtod(m, caddr_t)
985 + eoff);
986 #else
987 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
988 eoff, sizeof(*fh));
989 if (fh == NULL) {
990 icmp6stat.icp6s_tooshort++;
991 return (-1);
993 #endif
995 * Data after a fragment header is meaningless
996 * unless it is the first fragment, but
997 * we'll go to the notify label for path MTU
998 * discovery.
1000 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1001 goto notify;
1003 eoff += sizeof(struct ip6_frag);
1004 nxt = fh->ip6f_nxt;
1005 break;
1006 default:
1008 * This case includes ESP and the No Next
1009 * Header. In such cases going to the notify
1010 * label does not have any meaning
1011 * (i.e. ctlfunc will be NULL), but we go
1012 * anyway since we might have to update
1013 * path MTU information.
1015 goto notify;
1018 notify:
1019 #ifndef PULLDOWN_TEST
1020 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1021 #else
1022 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1023 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1024 if (icmp6 == NULL) {
1025 icmp6stat.icp6s_tooshort++;
1026 return (-1);
1028 #endif
1030 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1031 bzero(&icmp6dst, sizeof(icmp6dst));
1032 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1033 icmp6dst.sin6_family = AF_INET6;
1034 if (finaldst == NULL)
1035 icmp6dst.sin6_addr = eip6->ip6_dst;
1036 else
1037 icmp6dst.sin6_addr = *finaldst;
1038 icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1039 &icmp6dst.sin6_addr);
1040 if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
1041 NULL, NULL)) {
1042 /* should be impossbile */
1043 nd6log((LOG_DEBUG,
1044 "icmp6_notify_error: in6_embedscope failed\n"));
1045 goto freeit;
1049 * retrieve parameters from the inner IPv6 header, and convert
1050 * them into sockaddr structures.
1052 bzero(&icmp6src, sizeof(icmp6src));
1053 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1054 icmp6src.sin6_family = AF_INET6;
1055 icmp6src.sin6_addr = eip6->ip6_src;
1056 icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1057 &icmp6src.sin6_addr);
1058 if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
1059 NULL, NULL)) {
1060 /* should be impossbile */
1061 nd6log((LOG_DEBUG,
1062 "icmp6_notify_error: in6_embedscope failed\n"));
1063 goto freeit;
1065 icmp6src.sin6_flowinfo =
1066 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1068 if (finaldst == NULL)
1069 finaldst = &eip6->ip6_dst;
1070 ip6cp.ip6c_m = m;
1071 ip6cp.ip6c_icmp6 = icmp6;
1072 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1073 ip6cp.ip6c_off = eoff;
1074 ip6cp.ip6c_finaldst = finaldst;
1075 ip6cp.ip6c_src = &icmp6src;
1076 ip6cp.ip6c_nxt = nxt;
1078 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1079 notifymtu = ntohl(icmp6->icmp6_mtu);
1080 ip6cp.ip6c_cmdarg = (void *)&notifymtu;
1081 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1084 ctlfunc = (void (*) (int, struct sockaddr *, void *))
1085 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1086 if (ctlfunc) {
1087 (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, &ip6cp);
1090 return (0);
1092 freeit:
1093 m_freem(m);
1094 return (-1);
1097 void
1098 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1100 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1101 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1102 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1103 u_int mtu = ntohl(icmp6->icmp6_mtu);
1104 struct rtentry *rt = NULL;
1105 struct sockaddr_in6 sin6;
1107 if (!validated)
1108 return;
1110 bzero(&sin6, sizeof(sin6));
1111 sin6.sin6_family = PF_INET6;
1112 sin6.sin6_len = sizeof(struct sockaddr_in6);
1113 sin6.sin6_addr = *dst;
1114 /* XXX normally, this won't happen */
1115 if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1116 sin6.sin6_addr.s6_addr16[1] =
1117 htons(m->m_pkthdr.rcvif->if_index);
1119 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1120 rt = rtpurelookup((struct sockaddr *)&sin6);
1121 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
1122 !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1123 if (mtu < IPV6_MMTU) { /* XXX */
1124 rt->rt_rmx.rmx_locks |= RTV_MTU;
1125 } else if (mtu < rt->rt_ifp->if_mtu &&
1126 rt->rt_rmx.rmx_mtu > mtu) {
1127 icmp6stat.icp6s_pmtuchg++;
1128 rt->rt_rmx.rmx_mtu = mtu;
1131 if (rt)
1132 --rt->rt_refcnt;
1136 * Process a Node Information Query packet, based on
1137 * draft-ietf-ipngwg-icmp-name-lookups-07.
1139 * Spec incompatibilities:
1140 * - IPv6 Subject address handling
1141 * - IPv4 Subject address handling support missing
1142 * - Proxy reply (answer even if it's not for me)
1143 * - joins NI group address at in6_ifattach() time only, does not cope
1144 * with hostname changes by sethostname(3)
1146 #define hostnamelen strlen(hostname)
1147 static struct mbuf *
1148 ni6_input(struct mbuf *m, int off)
1150 struct icmp6_nodeinfo *ni6, *nni6;
1151 struct mbuf *n = NULL;
1152 u_int16_t qtype;
1153 int subjlen;
1154 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1155 struct ni_reply_fqdn *fqdn;
1156 int addrs; /* for NI_QTYPE_NODEADDR */
1157 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1158 struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1159 struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
1160 struct ip6_hdr *ip6;
1161 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1162 char *subj = NULL;
1163 struct in6_ifaddr *ia6 = NULL;
1165 ip6 = mtod(m, struct ip6_hdr *);
1166 #ifndef PULLDOWN_TEST
1167 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1168 #else
1169 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1170 if (ni6 == NULL) {
1171 /* m is already reclaimed */
1172 return NULL;
1174 #endif
1177 * Validate IPv6 destination address.
1179 * The Responder must discard the Query without further processing
1180 * unless it is one of the Responder's unicast or anycast addresses, or
1181 * a link-local scope multicast address which the Responder has joined.
1182 * [icmp-name-lookups-07, Section 4.]
1184 bzero(&sin6, sizeof(sin6));
1185 sin6.sin6_family = AF_INET6;
1186 sin6.sin6_len = sizeof(struct sockaddr_in6);
1187 bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1188 /* XXX scopeid */
1189 if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
1190 /* unicast/anycast, fine */
1191 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1192 (icmp6_nodeinfo & 4) == 0) {
1193 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1194 "a temporary address in %s:%d",
1195 __FILE__, __LINE__));
1196 goto bad;
1198 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1199 ; /* link-local multicast, fine */
1200 else
1201 goto bad;
1203 /* validate query Subject field. */
1204 qtype = ntohs(ni6->ni_qtype);
1205 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1206 switch (qtype) {
1207 case NI_QTYPE_NOOP:
1208 case NI_QTYPE_SUPTYPES:
1209 /* 07 draft */
1210 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1211 break;
1212 /* FALLTHROUGH */
1213 case NI_QTYPE_FQDN:
1214 case NI_QTYPE_NODEADDR:
1215 switch (ni6->ni_code) {
1216 case ICMP6_NI_SUBJ_IPV6:
1217 #if ICMP6_NI_SUBJ_IPV6 != 0
1218 case 0:
1219 #endif
1221 * backward compatibility - try to accept 03 draft
1222 * format, where no Subject is present.
1224 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1225 subjlen == 0) {
1226 oldfqdn++;
1227 break;
1229 #if ICMP6_NI_SUBJ_IPV6 != 0
1230 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1231 goto bad;
1232 #endif
1234 if (subjlen != sizeof(sin6.sin6_addr))
1235 goto bad;
1238 * Validate Subject address.
1240 * Not sure what exactly "address belongs to the node"
1241 * means in the spec, is it just unicast, or what?
1243 * At this moment we consider Subject address as
1244 * "belong to the node" if the Subject address equals
1245 * to the IPv6 destination address; validation for
1246 * IPv6 destination address should have done enough
1247 * check for us.
1249 * We do not do proxy at this moment.
1251 /* m_pulldown instead of copy? */
1252 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1253 subjlen, (caddr_t)&sin6.sin6_addr);
1254 sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1255 &sin6.sin6_addr);
1256 in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL);
1257 bzero(&sin6_d, sizeof(sin6_d));
1258 sin6_d.sin6_family = AF_INET6; /* not used, actually */
1259 sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
1260 sin6_d.sin6_addr = ip6->ip6_dst;
1261 sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1262 &ip6->ip6_dst);
1263 in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL);
1264 subj = (char *)&sin6;
1265 if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
1266 break;
1269 * XXX if we are to allow other cases, we should really
1270 * be careful about scope here.
1271 * basically, we should disallow queries toward IPv6
1272 * destination X with subject Y, if scope(X) > scope(Y).
1273 * if we allow scope(X) > scope(Y), it will result in
1274 * information leakage across scope boundary.
1276 goto bad;
1278 case ICMP6_NI_SUBJ_FQDN:
1280 * Validate Subject name with gethostname(3).
1282 * The behavior may need some debate, since:
1283 * - we are not sure if the node has FQDN as
1284 * hostname (returned by gethostname(3)).
1285 * - the code does wildcard match for truncated names.
1286 * however, we are not sure if we want to perform
1287 * wildcard match, if gethostname(3) side has
1288 * truncated hostname.
1290 n = ni6_nametodns(hostname, hostnamelen, 0);
1291 if (!n || n->m_next || n->m_len == 0)
1292 goto bad;
1293 IP6_EXTHDR_GET(subj, char *, m,
1294 off + sizeof(struct icmp6_nodeinfo), subjlen);
1295 if (subj == NULL)
1296 goto bad;
1297 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1298 n->m_len)) {
1299 goto bad;
1301 m_freem(n);
1302 n = NULL;
1303 break;
1305 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1306 default:
1307 goto bad;
1309 break;
1312 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1313 switch (qtype) {
1314 case NI_QTYPE_FQDN:
1315 if ((icmp6_nodeinfo & 1) == 0)
1316 goto bad;
1317 break;
1318 case NI_QTYPE_NODEADDR:
1319 if ((icmp6_nodeinfo & 2) == 0)
1320 goto bad;
1321 break;
1324 /* guess reply length */
1325 switch (qtype) {
1326 case NI_QTYPE_NOOP:
1327 break; /* no reply data */
1328 case NI_QTYPE_SUPTYPES:
1329 replylen += sizeof(u_int32_t);
1330 break;
1331 case NI_QTYPE_FQDN:
1332 /* XXX will append an mbuf */
1333 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1334 break;
1335 case NI_QTYPE_NODEADDR:
1336 addrs = ni6_addrs(ni6, m, &ifp, subj);
1337 if ((replylen += addrs * (sizeof(struct in6_addr) +
1338 sizeof(u_int32_t))) > MCLBYTES)
1339 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1340 break;
1341 default:
1343 * XXX: We must return a reply with the ICMP6 code
1344 * `unknown Qtype' in this case. However we regard the case
1345 * as an FQDN query for backward compatibility.
1346 * Older versions set a random value to this field,
1347 * so it rarely varies in the defined qtypes.
1348 * But the mechanism is not reliable...
1349 * maybe we should obsolete older versions.
1351 qtype = NI_QTYPE_FQDN;
1352 /* XXX will append an mbuf */
1353 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1354 oldfqdn++;
1355 break;
1358 /* allocate an mbuf to reply. */
1359 if (replylen > MCLBYTES) {
1361 * XXX: should we try to allocate more? But MCLBYTES
1362 * is probably much larger than IPV6_MMTU...
1364 goto bad;
1366 n = m_getb(replylen, MB_DONTWAIT, m->m_type, M_PKTHDR);
1367 if (n == NULL) {
1368 m_freem(m);
1369 return (NULL);
1371 M_MOVE_PKTHDR(n, m); /* just for recvif */
1372 n->m_pkthdr.len = n->m_len = replylen;
1374 /* copy mbuf header and IPv6 + Node Information base headers */
1375 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1376 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1377 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1379 /* qtype dependent procedure */
1380 switch (qtype) {
1381 case NI_QTYPE_NOOP:
1382 nni6->ni_code = ICMP6_NI_SUCCESS;
1383 nni6->ni_flags = 0;
1384 break;
1385 case NI_QTYPE_SUPTYPES:
1387 u_int32_t v;
1388 nni6->ni_code = ICMP6_NI_SUCCESS;
1389 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1390 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1391 v = (u_int32_t)htonl(0x0000000f);
1392 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1393 break;
1395 case NI_QTYPE_FQDN:
1396 nni6->ni_code = ICMP6_NI_SUCCESS;
1397 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1398 sizeof(struct ip6_hdr) +
1399 sizeof(struct icmp6_nodeinfo));
1400 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1401 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1403 * XXX do we really have FQDN in variable "hostname"?
1405 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1406 if (n->m_next == NULL)
1407 goto bad;
1408 /* XXX we assume that n->m_next is not a chain */
1409 if (n->m_next->m_next != NULL)
1410 goto bad;
1411 n->m_pkthdr.len += n->m_next->m_len;
1412 break;
1413 case NI_QTYPE_NODEADDR:
1415 int lenlim, copied;
1417 nni6->ni_code = ICMP6_NI_SUCCESS;
1418 n->m_pkthdr.len = n->m_len =
1419 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1420 lenlim = M_TRAILINGSPACE(n);
1421 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1422 /* XXX: reset mbuf length */
1423 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1424 sizeof(struct icmp6_nodeinfo) + copied;
1425 break;
1427 default:
1428 break; /* XXX impossible! */
1431 nni6->ni_type = ICMP6_NI_REPLY;
1432 m_freem(m);
1433 return (n);
1435 bad:
1436 m_freem(m);
1437 if (n)
1438 m_freem(n);
1439 return (NULL);
1441 #undef hostnamelen
1444 * make a mbuf with DNS-encoded string. no compression support.
1446 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1447 * treated as truncated name (two \0 at the end). this is a wild guess.
1449 static struct mbuf *
1450 ni6_nametodns(const char *name, int namelen,
1451 int old) /* return pascal string if non-zero */
1453 struct mbuf *m;
1454 char *cp, *ep;
1455 const char *p, *q;
1456 int i, len, nterm;
1458 if (old)
1459 len = namelen + 1;
1460 else
1461 len = MCLBYTES;
1463 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1464 m = m_getb(len, MB_DONTWAIT, MT_DATA, 0);
1465 if (!m)
1466 goto fail;
1468 if (old) {
1469 m->m_len = len;
1470 *mtod(m, char *) = namelen;
1471 bcopy(name, mtod(m, char *) + 1, namelen);
1472 return m;
1473 } else {
1474 m->m_len = 0;
1475 cp = mtod(m, char *);
1476 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1478 /* if not certain about my name, return empty buffer */
1479 if (namelen == 0)
1480 return m;
1483 * guess if it looks like shortened hostname, or FQDN.
1484 * shortened hostname needs two trailing "\0".
1486 i = 0;
1487 for (p = name; p < name + namelen; p++) {
1488 if (*p && *p == '.')
1489 i++;
1491 if (i < 2)
1492 nterm = 2;
1493 else
1494 nterm = 1;
1496 p = name;
1497 while (cp < ep && p < name + namelen) {
1498 i = 0;
1499 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1500 i++;
1501 /* result does not fit into mbuf */
1502 if (cp + i + 1 >= ep)
1503 goto fail;
1505 * DNS label length restriction, RFC1035 page 8.
1506 * "i == 0" case is included here to avoid returning
1507 * 0-length label on "foo..bar".
1509 if (i <= 0 || i >= 64)
1510 goto fail;
1511 *cp++ = i;
1512 bcopy(p, cp, i);
1513 cp += i;
1514 p = q;
1515 if (p < name + namelen && *p == '.')
1516 p++;
1518 /* termination */
1519 if (cp + nterm >= ep)
1520 goto fail;
1521 while (nterm-- > 0)
1522 *cp++ = '\0';
1523 m->m_len = cp - mtod(m, char *);
1524 return m;
1527 panic("should not reach here");
1528 /* NOTREACHED */
1530 fail:
1531 if (m)
1532 m_freem(m);
1533 return NULL;
1537 * check if two DNS-encoded string matches. takes care of truncated
1538 * form (with \0\0 at the end). no compression support.
1539 * XXX upper/lowercase match (see RFC2065)
1541 static int
1542 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1544 const char *a0, *b0;
1545 int l;
1547 /* simplest case - need validation? */
1548 if (alen == blen && bcmp(a, b, alen) == 0)
1549 return 1;
1551 a0 = a;
1552 b0 = b;
1554 /* termination is mandatory */
1555 if (alen < 2 || blen < 2)
1556 return 0;
1557 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1558 return 0;
1559 alen--;
1560 blen--;
1562 while (a - a0 < alen && b - b0 < blen) {
1563 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1564 return 0;
1566 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1567 return 0;
1568 /* we don't support compression yet */
1569 if (a[0] >= 64 || b[0] >= 64)
1570 return 0;
1572 /* truncated case */
1573 if (a[0] == 0 && a - a0 == alen - 1)
1574 return 1;
1575 if (b[0] == 0 && b - b0 == blen - 1)
1576 return 1;
1577 if (a[0] == 0 || b[0] == 0)
1578 return 0;
1580 if (a[0] != b[0])
1581 return 0;
1582 l = a[0];
1583 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1584 return 0;
1585 if (bcmp(a + 1, b + 1, l) != 0)
1586 return 0;
1588 a += 1 + l;
1589 b += 1 + l;
1592 if (a - a0 == alen && b - b0 == blen)
1593 return 1;
1594 else
1595 return 0;
1599 * calculate the number of addresses to be returned in the node info reply.
1601 static int
1602 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1603 char *subj)
1605 struct ifnet *ifp;
1606 struct in6_ifaddr *ifa6;
1607 struct ifaddr *ifa;
1608 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1609 int addrs = 0, addrsofif, iffound = 0;
1610 int niflags = ni6->ni_flags;
1612 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1613 switch (ni6->ni_code) {
1614 case ICMP6_NI_SUBJ_IPV6:
1615 if (subj == NULL) /* must be impossible... */
1616 return (0);
1617 subj_ip6 = (struct sockaddr_in6 *)subj;
1618 break;
1619 default:
1621 * XXX: we only support IPv6 subject address for
1622 * this Qtype.
1624 return (0);
1628 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1630 addrsofif = 0;
1631 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1633 if (ifa->ifa_addr->sa_family != AF_INET6)
1634 continue;
1635 ifa6 = (struct in6_ifaddr *)ifa;
1637 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1638 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1639 &ifa6->ia_addr.sin6_addr))
1640 iffound = 1;
1643 * IPv4-mapped addresses can only be returned by a
1644 * Node Information proxy, since they represent
1645 * addresses of IPv4-only nodes, which perforce do
1646 * not implement this protocol.
1647 * [icmp-name-lookups-07, Section 5.4]
1648 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1649 * this function at this moment.
1652 /* What do we have to do about ::1? */
1653 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1654 case IPV6_ADDR_SCOPE_LINKLOCAL:
1655 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1656 continue;
1657 break;
1658 case IPV6_ADDR_SCOPE_SITELOCAL:
1659 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1660 continue;
1661 break;
1662 case IPV6_ADDR_SCOPE_GLOBAL:
1663 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1664 continue;
1665 break;
1666 default:
1667 continue;
1671 * check if anycast is okay.
1672 * XXX: just experimental. not in the spec.
1674 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) &&
1675 !(niflags & NI_NODEADDR_FLAG_ANYCAST))
1676 continue; /* we need only unicast addresses */
1677 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) &&
1678 (icmp6_nodeinfo & 4) == 0) {
1679 continue;
1681 addrsofif++; /* count the address */
1683 if (iffound) {
1684 *ifpp = ifp;
1685 return (addrsofif);
1688 addrs += addrsofif;
1691 return (addrs);
1694 static int
1695 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1696 struct ifnet *ifp0, int resid)
1698 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1699 struct in6_ifaddr *ifa6;
1700 struct ifaddr *ifa;
1701 struct ifnet *ifp_dep = NULL;
1702 int copied = 0, allow_deprecated = 0;
1703 u_char *cp = (u_char *)(nni6 + 1);
1704 int niflags = ni6->ni_flags;
1705 u_int32_t ltime;
1707 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1708 return (0); /* needless to copy */
1710 again:
1712 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1714 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1715 if (ifa->ifa_addr->sa_family != AF_INET6)
1716 continue;
1717 ifa6 = (struct in6_ifaddr *)ifa;
1719 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) &&
1720 allow_deprecated == 0) {
1722 * prefererred address should be put before
1723 * deprecated addresses.
1726 /* record the interface for later search */
1727 if (ifp_dep == NULL)
1728 ifp_dep = ifp;
1730 continue;
1732 else if (!(ifa6->ia6_flags & IN6_IFF_DEPRECATED) &&
1733 allow_deprecated != 0)
1734 continue; /* we now collect deprecated addrs */
1736 /* What do we have to do about ::1? */
1737 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1738 case IPV6_ADDR_SCOPE_LINKLOCAL:
1739 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1740 continue;
1741 break;
1742 case IPV6_ADDR_SCOPE_SITELOCAL:
1743 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1744 continue;
1745 break;
1746 case IPV6_ADDR_SCOPE_GLOBAL:
1747 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1748 continue;
1749 break;
1750 default:
1751 continue;
1755 * check if anycast is okay.
1756 * XXX: just experimental. not in the spec.
1758 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) &&
1759 !(niflags & NI_NODEADDR_FLAG_ANYCAST))
1760 continue;
1761 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) &&
1762 (icmp6_nodeinfo & 4) == 0) {
1763 continue;
1766 /* now we can copy the address */
1767 if (resid < sizeof(struct in6_addr) +
1768 sizeof(u_int32_t)) {
1770 * We give up much more copy.
1771 * Set the truncate flag and return.
1773 nni6->ni_flags |=
1774 NI_NODEADDR_FLAG_TRUNCATE;
1775 return (copied);
1779 * Set the TTL of the address.
1780 * The TTL value should be one of the following
1781 * according to the specification:
1783 * 1. The remaining lifetime of a DHCP lease on the
1784 * address, or
1785 * 2. The remaining Valid Lifetime of a prefix from
1786 * which the address was derived through Stateless
1787 * Autoconfiguration.
1789 * Note that we currently do not support stateful
1790 * address configuration by DHCPv6, so the former
1791 * case can't happen.
1793 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1794 ltime = ND6_INFINITE_LIFETIME;
1795 else {
1796 if (ifa6->ia6_lifetime.ia6t_expire >
1797 time_second)
1798 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1799 else
1800 ltime = 0;
1803 bcopy(&ltime, cp, sizeof(u_int32_t));
1804 cp += sizeof(u_int32_t);
1806 /* copy the address itself */
1807 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1808 sizeof(struct in6_addr));
1809 /* XXX: KAME link-local hack; remove ifindex */
1810 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1811 ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1812 cp += sizeof(struct in6_addr);
1814 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1815 copied += (sizeof(struct in6_addr) +
1816 sizeof(u_int32_t));
1818 if (ifp0) /* we need search only on the specified IF */
1819 break;
1822 if (allow_deprecated == 0 && ifp_dep != NULL) {
1823 ifp = ifp_dep;
1824 allow_deprecated = 1;
1826 goto again;
1829 return (copied);
1833 * XXX almost dup'ed code with rip6_input.
1835 static int
1836 icmp6_rip6_input(struct mbuf **mp, int off)
1838 struct mbuf *m = *mp;
1839 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1840 struct in6pcb *in6p;
1841 struct in6pcb *last = NULL;
1842 struct sockaddr_in6 rip6src;
1843 struct icmp6_hdr *icmp6;
1844 struct mbuf *opts = NULL;
1846 #ifndef PULLDOWN_TEST
1847 /* this is assumed to be safe. */
1848 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1849 #else
1850 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1851 if (icmp6 == NULL) {
1852 /* m is already reclaimed */
1853 return IPPROTO_DONE;
1855 #endif
1857 bzero(&rip6src, sizeof(rip6src));
1858 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1859 rip6src.sin6_family = AF_INET6;
1860 /* KAME hack: recover scopeid */
1861 in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1863 LIST_FOREACH(in6p, &ripcbinfo.pcblisthead, inp_list)
1865 if (in6p->inp_flags & INP_PLACEMARKER)
1866 continue;
1867 if ((in6p->inp_vflag & INP_IPV6) == 0)
1868 continue;
1869 #ifdef HAVE_NRL_INPCB
1870 if (!(in6p->in6p_flags & INP_IPV6))
1871 continue;
1872 #endif
1873 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1874 continue;
1875 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1876 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1877 continue;
1878 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1879 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1880 continue;
1881 if (in6p->in6p_icmp6filt
1882 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1883 in6p->in6p_icmp6filt))
1884 continue;
1885 if (last) {
1886 struct mbuf *n;
1887 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1888 if (last->in6p_flags & IN6P_CONTROLOPTS)
1889 ip6_savecontrol(last, &opts, ip6, n);
1890 /* strip intermediate headers */
1891 m_adj(n, off);
1892 if (ssb_appendaddr(&last->in6p_socket->so_rcv,
1893 (struct sockaddr *)&rip6src,
1894 n, opts) == 0) {
1895 /* should notify about lost packet */
1896 m_freem(n);
1897 if (opts) {
1898 m_freem(opts);
1900 } else
1901 sorwakeup(last->in6p_socket);
1902 opts = NULL;
1905 last = in6p;
1907 if (last) {
1908 if (last->in6p_flags & IN6P_CONTROLOPTS)
1909 ip6_savecontrol(last, &opts, ip6, m);
1910 /* strip intermediate headers */
1911 m_adj(m, off);
1912 if (ssb_appendaddr(&last->in6p_socket->so_rcv,
1913 (struct sockaddr *)&rip6src, m, opts) == 0) {
1914 m_freem(m);
1915 if (opts)
1916 m_freem(opts);
1917 } else
1918 sorwakeup(last->in6p_socket);
1919 } else {
1920 m_freem(m);
1921 ip6stat.ip6s_delivered--;
1923 return IPPROTO_DONE;
1927 * Reflect the ip6 packet back to the source.
1928 * OFF points to the icmp6 header, counted from the top of the mbuf.
1930 void
1931 icmp6_reflect(struct mbuf *m, size_t off)
1933 struct ip6_hdr *ip6;
1934 struct icmp6_hdr *icmp6;
1935 struct in6_ifaddr *ia;
1936 struct in6_addr t, *src = 0;
1937 int plen;
1938 int type, code;
1939 struct ifnet *outif = NULL;
1940 struct sockaddr_in6 sa6_src, sa6_dst;
1941 #ifdef COMPAT_RFC1885
1942 int mtu = IPV6_MMTU;
1943 struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
1944 #endif
1946 /* too short to reflect */
1947 if (off < sizeof(struct ip6_hdr)) {
1948 nd6log((LOG_DEBUG,
1949 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1950 (u_long)off, (u_long)sizeof(struct ip6_hdr),
1951 __FILE__, __LINE__));
1952 goto bad;
1956 * If there are extra headers between IPv6 and ICMPv6, strip
1957 * off that header first.
1959 #ifdef DIAGNOSTIC
1960 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
1961 panic("assumption failed in icmp6_reflect");
1962 #endif
1963 if (off > sizeof(struct ip6_hdr)) {
1964 size_t l;
1965 struct ip6_hdr nip6;
1967 l = off - sizeof(struct ip6_hdr);
1968 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
1969 m_adj(m, l);
1970 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1971 if (m->m_len < l) {
1972 if ((m = m_pullup(m, l)) == NULL)
1973 return;
1975 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
1976 } else /* off == sizeof(struct ip6_hdr) */ {
1977 size_t l;
1978 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1979 if (m->m_len < l) {
1980 if ((m = m_pullup(m, l)) == NULL)
1981 return;
1984 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
1985 ip6 = mtod(m, struct ip6_hdr *);
1986 ip6->ip6_nxt = IPPROTO_ICMPV6;
1987 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
1988 type = icmp6->icmp6_type; /* keep type for statistics */
1989 code = icmp6->icmp6_code; /* ditto. */
1991 t = ip6->ip6_dst;
1993 * ip6_input() drops a packet if its src is multicast.
1994 * So, the src is never multicast.
1996 ip6->ip6_dst = ip6->ip6_src;
1999 * XXX: make sure to embed scope zone information, using
2000 * already embedded IDs or the received interface (if any).
2001 * Note that rcvif may be NULL.
2002 * TODO: scoped routing case (XXX).
2004 bzero(&sa6_src, sizeof(sa6_src));
2005 sa6_src.sin6_family = AF_INET6;
2006 sa6_src.sin6_len = sizeof(sa6_src);
2007 sa6_src.sin6_addr = ip6->ip6_dst;
2008 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2009 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
2010 bzero(&sa6_dst, sizeof(sa6_dst));
2011 sa6_dst.sin6_family = AF_INET6;
2012 sa6_dst.sin6_len = sizeof(sa6_dst);
2013 sa6_dst.sin6_addr = t;
2014 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2015 in6_embedscope(&t, &sa6_dst, NULL, NULL);
2017 #ifdef COMPAT_RFC1885
2019 * xxx guess MTU
2020 * RFC 1885 requires that echo reply should be truncated if it
2021 * does not fit in with (return) path MTU, but the description was
2022 * removed in the new spec.
2024 if (icmp6_reflect_rt.ro_rt == 0 ||
2025 ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
2026 if (icmp6_reflect_rt.ro_rt) {
2027 RTFREE(icmp6_reflect_rt.ro_rt);
2028 icmp6_reflect_rt.ro_rt = 0;
2030 bzero(sin6, sizeof(*sin6));
2031 sin6->sin6_family = PF_INET6;
2032 sin6->sin6_len = sizeof(struct sockaddr_in6);
2033 sin6->sin6_addr = ip6->ip6_dst;
2035 rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
2036 RTF_PRCLONING);
2039 if (icmp6_reflect_rt.ro_rt == 0)
2040 goto bad;
2042 if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
2043 && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
2044 mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
2046 if (mtu < m->m_pkthdr.len) {
2047 plen -= (m->m_pkthdr.len - mtu);
2048 m_adj(m, mtu - m->m_pkthdr.len);
2050 #endif
2052 * If the incoming packet was addressed directly to us(i.e. unicast),
2053 * use dst as the src for the reply.
2054 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2055 * (for example) when we encounter an error while forwarding procedure
2056 * destined to a duplicated address of ours.
2058 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2059 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2060 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2061 src = &t;
2062 break;
2064 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2066 * This is the case if the dst is our link-local address
2067 * and the sender is also ourselves.
2069 src = &t;
2072 if (src == 0) {
2073 int e;
2074 struct route_in6 ro;
2077 * This case matches to multicasts, our anycast, or unicasts
2078 * that we do not own. Select a source address based on the
2079 * source address of the erroneous packet.
2081 bzero(&ro, sizeof(ro));
2082 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e, NULL);
2083 if (ro.ro_rt)
2084 RTFREE(ro.ro_rt); /* XXX: we could use this */
2085 if (src == NULL) {
2086 nd6log((LOG_DEBUG,
2087 "icmp6_reflect: source can't be determined: "
2088 "dst=%s, error=%d\n",
2089 ip6_sprintf(&sa6_src.sin6_addr), e));
2090 goto bad;
2094 ip6->ip6_src = *src;
2096 ip6->ip6_flow = 0;
2097 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2098 ip6->ip6_vfc |= IPV6_VERSION;
2099 ip6->ip6_nxt = IPPROTO_ICMPV6;
2100 if (m->m_pkthdr.rcvif) {
2101 /* XXX: This may not be the outgoing interface */
2102 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2103 } else
2104 ip6->ip6_hlim = ip6_defhlim;
2106 icmp6->icmp6_cksum = 0;
2107 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2108 sizeof(struct ip6_hdr), plen);
2111 * XXX option handling
2114 m->m_flags &= ~(M_BCAST|M_MCAST);
2116 #ifdef COMPAT_RFC1885
2117 ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
2118 #else
2119 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2120 #endif
2121 if (outif)
2122 icmp6_ifoutstat_inc(outif, type, code);
2124 return;
2126 bad:
2127 m_freem(m);
2128 return;
2131 void
2132 icmp6_fasttimo(void)
2135 mld6_fasttimeo();
2138 static const char *
2139 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2140 struct in6_addr *tgt6)
2142 static char buf[1024];
2143 ksnprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2144 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2145 return buf;
2148 void
2149 icmp6_redirect_input(struct mbuf *m, int off)
2151 struct ifnet *ifp = m->m_pkthdr.rcvif;
2152 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2153 struct nd_redirect *nd_rd;
2154 int icmp6len = ntohs(ip6->ip6_plen);
2155 char *lladdr = NULL;
2156 int lladdrlen = 0;
2157 u_char *redirhdr = NULL;
2158 int redirhdrlen = 0;
2159 struct rtentry *rt = NULL;
2160 int is_router;
2161 int is_onlink;
2162 struct in6_addr src6 = ip6->ip6_src;
2163 struct in6_addr redtgt6;
2164 struct in6_addr reddst6;
2165 union nd_opts ndopts;
2167 if (!m || !ifp)
2168 return;
2170 /* XXX if we are router, we don't update route by icmp6 redirect */
2171 if (ip6_forwarding)
2172 goto freeit;
2173 if (!icmp6_rediraccept)
2174 goto freeit;
2176 #ifndef PULLDOWN_TEST
2177 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2178 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2179 #else
2180 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2181 if (nd_rd == NULL) {
2182 icmp6stat.icp6s_tooshort++;
2183 return;
2185 #endif
2186 redtgt6 = nd_rd->nd_rd_target;
2187 reddst6 = nd_rd->nd_rd_dst;
2189 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2190 redtgt6.s6_addr16[1] = htons(ifp->if_index);
2191 if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2192 reddst6.s6_addr16[1] = htons(ifp->if_index);
2194 /* validation */
2195 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2196 nd6log((LOG_ERR,
2197 "ICMP6 redirect sent from %s rejected; "
2198 "must be from linklocal\n", ip6_sprintf(&src6)));
2199 goto bad;
2201 if (ip6->ip6_hlim != 255) {
2202 nd6log((LOG_ERR,
2203 "ICMP6 redirect sent from %s rejected; "
2204 "hlim=%d (must be 255)\n",
2205 ip6_sprintf(&src6), ip6->ip6_hlim));
2206 goto bad;
2209 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2210 struct sockaddr_in6 sin6;
2211 struct in6_addr *gw6;
2213 bzero(&sin6, sizeof(sin6));
2214 sin6.sin6_family = AF_INET6;
2215 sin6.sin6_len = sizeof(struct sockaddr_in6);
2216 bcopy(&reddst6, &sin6.sin6_addr, sizeof reddst6);
2217 rt = rtpurelookup((struct sockaddr *)&sin6);
2218 if (rt != NULL) {
2219 if (rt->rt_gateway == NULL ||
2220 rt->rt_gateway->sa_family != AF_INET6) {
2221 nd6log((LOG_ERR,
2222 "ICMP6 redirect rejected; no route "
2223 "with inet6 gateway found for redirect dst: %s\n",
2224 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2225 --rt->rt_refcnt;
2226 goto bad;
2229 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2230 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2231 nd6log((LOG_ERR,
2232 "ICMP6 redirect rejected; "
2233 "not equal to gw-for-src=%s (must be same): "
2234 "%s\n",
2235 ip6_sprintf(gw6),
2236 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2237 --rt->rt_refcnt;
2238 goto bad;
2240 } else {
2241 nd6log((LOG_ERR,
2242 "ICMP6 redirect rejected; "
2243 "no route found for redirect dst: %s\n",
2244 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2245 goto bad;
2247 --rt->rt_refcnt;
2248 rt = NULL;
2250 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2251 nd6log((LOG_ERR,
2252 "ICMP6 redirect rejected; "
2253 "redirect dst must be unicast: %s\n",
2254 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2255 goto bad;
2258 is_router = is_onlink = 0;
2259 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2260 is_router = 1; /* router case */
2261 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2262 is_onlink = 1; /* on-link destination case */
2263 if (!is_router && !is_onlink) {
2264 nd6log((LOG_ERR,
2265 "ICMP6 redirect rejected; "
2266 "neither router case nor onlink case: %s\n",
2267 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2268 goto bad;
2270 /* validation passed */
2272 icmp6len -= sizeof(*nd_rd);
2273 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2274 if (nd6_options(&ndopts) < 0) {
2275 nd6log((LOG_INFO, "icmp6_redirect_input: "
2276 "invalid ND option, rejected: %s\n",
2277 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2278 /* nd6_options have incremented stats */
2279 goto freeit;
2282 if (ndopts.nd_opts_tgt_lladdr) {
2283 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2284 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2287 if (ndopts.nd_opts_rh) {
2288 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2289 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2292 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2293 nd6log((LOG_INFO,
2294 "icmp6_redirect_input: lladdrlen mismatch for %s "
2295 "(if %d, icmp6 packet %d): %s\n",
2296 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2297 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2298 goto bad;
2301 /* RFC 2461 8.3 */
2302 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2303 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2305 if (!is_onlink) { /* better router case. perform rtredirect. */
2306 /* perform rtredirect */
2307 struct sockaddr_in6 sdst;
2308 struct sockaddr_in6 sgw;
2309 struct sockaddr_in6 ssrc;
2311 bzero(&sdst, sizeof(sdst));
2312 bzero(&sgw, sizeof(sgw));
2313 bzero(&ssrc, sizeof(ssrc));
2314 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2315 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2316 sizeof(struct sockaddr_in6);
2317 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2318 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2319 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2320 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2321 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2322 (struct sockaddr *)&ssrc);
2324 /* finally update cached route in each socket via pfctlinput */
2326 struct sockaddr_in6 sdst;
2328 bzero(&sdst, sizeof(sdst));
2329 sdst.sin6_family = AF_INET6;
2330 sdst.sin6_len = sizeof(struct sockaddr_in6);
2331 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2332 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2333 #ifdef IPSEC
2334 key_sa_routechange((struct sockaddr *)&sdst);
2335 #endif
2338 freeit:
2339 m_freem(m);
2340 return;
2342 bad:
2343 icmp6stat.icp6s_badredirect++;
2344 m_freem(m);
2347 void
2348 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2350 struct ifnet *ifp; /* my outgoing interface */
2351 struct in6_addr *ifp_ll6;
2352 struct in6_addr *router_ll6;
2353 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2354 struct mbuf *m = NULL; /* newly allocated one */
2355 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2356 struct nd_redirect *nd_rd;
2357 size_t maxlen;
2358 u_char *p;
2359 struct ifnet *outif = NULL;
2360 struct sockaddr_in6 src_sa;
2362 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2364 /* if we are not router, we don't send icmp6 redirect */
2365 if (!ip6_forwarding || ip6_accept_rtadv)
2366 goto fail;
2368 /* sanity check */
2369 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2370 goto fail;
2373 * Address check:
2374 * the source address must identify a neighbor, and
2375 * the destination address must not be a multicast address
2376 * [RFC 2461, sec 8.2]
2378 sip6 = mtod(m0, struct ip6_hdr *);
2379 bzero(&src_sa, sizeof(src_sa));
2380 src_sa.sin6_family = AF_INET6;
2381 src_sa.sin6_len = sizeof(src_sa);
2382 src_sa.sin6_addr = sip6->ip6_src;
2383 /* we don't currently use sin6_scope_id, but eventually use it */
2384 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2385 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2386 goto fail;
2387 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2388 goto fail; /* what should we do here? */
2390 /* rate limit */
2391 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2392 goto fail;
2395 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2396 * we almost always ask for an mbuf cluster for simplicity.
2397 * (MHLEN < IPV6_MMTU is almost always true)
2399 #if IPV6_MMTU >= MCLBYTES
2400 # error assumption failed about IPV6_MMTU and MCLBYTES
2401 #endif
2402 m = m_getb(IPV6_MMTU, MB_DONTWAIT, MT_HEADER, M_PKTHDR);
2403 if (!m)
2404 goto fail;
2405 m->m_len = 0;
2406 maxlen = M_TRAILINGSPACE(m);
2407 maxlen = min(IPV6_MMTU, maxlen);
2408 /* just for safety */
2409 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2410 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2411 goto fail;
2415 /* get ip6 linklocal address for ifp(my outgoing interface). */
2416 struct in6_ifaddr *ia;
2417 if ((ia = in6ifa_ifpforlinklocal(ifp,
2418 IN6_IFF_NOTREADY|
2419 IN6_IFF_ANYCAST)) == NULL)
2420 goto fail;
2421 ifp_ll6 = &ia->ia_addr.sin6_addr;
2424 /* get ip6 linklocal address for the router. */
2425 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2426 struct sockaddr_in6 *sin6;
2427 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2428 router_ll6 = &sin6->sin6_addr;
2429 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2430 router_ll6 = (struct in6_addr *)NULL;
2431 } else
2432 router_ll6 = (struct in6_addr *)NULL;
2434 /* ip6 */
2435 ip6 = mtod(m, struct ip6_hdr *);
2436 ip6->ip6_flow = 0;
2437 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2438 ip6->ip6_vfc |= IPV6_VERSION;
2439 /* ip6->ip6_plen will be set later */
2440 ip6->ip6_nxt = IPPROTO_ICMPV6;
2441 ip6->ip6_hlim = 255;
2442 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2443 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2444 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2446 /* ND Redirect */
2447 nd_rd = (struct nd_redirect *)(ip6 + 1);
2448 nd_rd->nd_rd_type = ND_REDIRECT;
2449 nd_rd->nd_rd_code = 0;
2450 nd_rd->nd_rd_reserved = 0;
2451 if (rt->rt_flags & RTF_GATEWAY) {
2453 * nd_rd->nd_rd_target must be a link-local address in
2454 * better router cases.
2456 if (!router_ll6)
2457 goto fail;
2458 bcopy(router_ll6, &nd_rd->nd_rd_target,
2459 sizeof(nd_rd->nd_rd_target));
2460 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2461 sizeof(nd_rd->nd_rd_dst));
2462 } else {
2463 /* make sure redtgt == reddst */
2464 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2465 sizeof(nd_rd->nd_rd_target));
2466 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2467 sizeof(nd_rd->nd_rd_dst));
2470 p = (u_char *)(nd_rd + 1);
2472 if (!router_ll6)
2473 goto nolladdropt;
2476 /* target lladdr option */
2477 struct rtentry *rt_router = NULL;
2478 int len;
2479 struct sockaddr_dl *sdl;
2480 struct nd_opt_hdr *nd_opt;
2481 char *lladdr;
2483 rt_router = nd6_lookup(router_ll6, 0, ifp);
2484 if (!rt_router)
2485 goto nolladdropt;
2486 len = sizeof(*nd_opt) + ifp->if_addrlen;
2487 len = (len + 7) & ~7; /* round by 8 */
2488 /* safety check */
2489 if (len + (p - (u_char *)ip6) > maxlen)
2490 goto nolladdropt;
2491 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2492 (rt_router->rt_flags & RTF_LLINFO) &&
2493 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2494 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2495 sdl->sdl_alen) {
2496 nd_opt = (struct nd_opt_hdr *)p;
2497 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2498 nd_opt->nd_opt_len = len >> 3;
2499 lladdr = (char *)(nd_opt + 1);
2500 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2501 p += len;
2504 nolladdropt:;
2506 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2508 /* just to be safe */
2509 #ifdef M_DECRYPTED /*not openbsd*/
2510 if (m0->m_flags & M_DECRYPTED)
2511 goto noredhdropt;
2512 #endif
2513 if (p - (u_char *)ip6 > maxlen)
2514 goto noredhdropt;
2517 /* redirected header option */
2518 int len;
2519 struct nd_opt_rd_hdr *nd_opt_rh;
2522 * compute the maximum size for icmp6 redirect header option.
2523 * XXX room for auth header?
2525 len = maxlen - (p - (u_char *)ip6);
2526 len &= ~7;
2528 /* This is just for simplicity. */
2529 if (m0->m_pkthdr.len != m0->m_len) {
2530 if (m0->m_next) {
2531 m_freem(m0->m_next);
2532 m0->m_next = NULL;
2534 m0->m_pkthdr.len = m0->m_len;
2538 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2539 * about padding/truncate rule for the original IP packet.
2540 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2541 * - "attach as much as possible" is the goal
2542 * - pad if not aligned (original size can be guessed by original
2543 * ip6 header)
2544 * Following code adds the padding if it is simple enough,
2545 * and truncates if not.
2547 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2548 panic("assumption failed in %s:%d", __FILE__, __LINE__);
2550 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2551 /* not enough room, truncate */
2552 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2553 } else {
2554 /* enough room, pad or truncate */
2555 size_t extra;
2557 extra = m0->m_pkthdr.len % 8;
2558 if (extra) {
2559 /* pad if easy enough, truncate if not */
2560 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2561 /* pad */
2562 m0->m_len += (8 - extra);
2563 m0->m_pkthdr.len += (8 - extra);
2564 } else {
2565 /* truncate */
2566 m0->m_pkthdr.len -= extra;
2567 m0->m_len -= extra;
2570 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2571 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2574 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2575 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2576 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2577 nd_opt_rh->nd_opt_rh_len = len >> 3;
2578 p += sizeof(*nd_opt_rh);
2579 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2581 /* connect m0 to m */
2582 m->m_next = m0;
2583 m->m_pkthdr.len = m->m_len + m0->m_len;
2585 noredhdropt:;
2587 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2588 sip6->ip6_src.s6_addr16[1] = 0;
2589 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2590 sip6->ip6_dst.s6_addr16[1] = 0;
2591 #if 0
2592 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2593 ip6->ip6_src.s6_addr16[1] = 0;
2594 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2595 ip6->ip6_dst.s6_addr16[1] = 0;
2596 #endif
2597 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2598 nd_rd->nd_rd_target.s6_addr16[1] = 0;
2599 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2600 nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2602 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2604 nd_rd->nd_rd_cksum = 0;
2605 nd_rd->nd_rd_cksum
2606 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2608 /* send the packet to outside... */
2609 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2610 if (outif) {
2611 icmp6_ifstat_inc(outif, ifs6_out_msg);
2612 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2614 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2616 return;
2618 fail:
2619 if (m)
2620 m_freem(m);
2621 if (m0)
2622 m_freem(m0);
2625 #ifdef HAVE_NRL_INPCB
2626 #define in6pcb inpcb
2627 #define in6p_icmp6filt inp_icmp6filt
2628 #endif
2630 * ICMPv6 socket option processing.
2633 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2635 int error = 0;
2636 int optlen;
2637 struct inpcb *inp = so->so_pcb;
2638 int level, op, optname;
2640 if (sopt) {
2641 level = sopt->sopt_level;
2642 op = sopt->sopt_dir;
2643 optname = sopt->sopt_name;
2644 optlen = sopt->sopt_valsize;
2645 } else
2646 level = op = optname = optlen = 0;
2648 if (level != IPPROTO_ICMPV6) {
2649 return EINVAL;
2652 switch (op) {
2653 case PRCO_SETOPT:
2654 switch (optname) {
2655 case ICMP6_FILTER:
2657 struct icmp6_filter *p;
2659 if (optlen != sizeof(*p)) {
2660 error = EMSGSIZE;
2661 break;
2663 if (inp->in6p_icmp6filt == NULL) {
2664 error = EINVAL;
2665 break;
2667 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2668 optlen);
2669 break;
2672 default:
2673 error = ENOPROTOOPT;
2674 break;
2676 break;
2678 case PRCO_GETOPT:
2679 switch (optname) {
2680 case ICMP6_FILTER:
2682 if (inp->in6p_icmp6filt == NULL) {
2683 error = EINVAL;
2684 break;
2686 error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2687 sizeof(struct icmp6_filter));
2688 break;
2691 default:
2692 error = ENOPROTOOPT;
2693 break;
2695 break;
2698 return (error);
2700 #ifdef HAVE_NRL_INPCB
2701 #undef in6pcb
2702 #undef in6p_icmp6filt
2703 #endif
2705 #ifndef HAVE_PPSRATECHECK
2706 #ifndef timersub
2707 #define timersub(tvp, uvp, vvp) \
2708 do { \
2709 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2710 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2711 if ((vvp)->tv_usec < 0) { \
2712 (vvp)->tv_sec--; \
2713 (vvp)->tv_usec += 1000000; \
2715 } while (0)
2716 #endif
2719 * ppsratecheck(): packets (or events) per second limitation.
2721 static int
2722 ppsratecheck(struct timeval *lasttime, int *curpps,
2723 int maxpps) /* maximum pps allowed */
2725 struct timeval tv, delta;
2726 int rv;
2728 microtime(&tv);
2730 timersub(&tv, lasttime, &delta);
2733 * Check for 0,0 so that the message will be seen at least once.
2734 * If more than one second has passed since the last update of
2735 * lasttime, reset the counter.
2737 * We do increment *curpps even in *curpps < maxpps case, as some may
2738 * try to use *curpps for stat purposes as well.
2740 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
2741 delta.tv_sec >= 1) {
2742 *lasttime = tv;
2743 *curpps = 0;
2744 rv = 1;
2745 } else if (maxpps < 0)
2746 rv = 1;
2747 else if (*curpps < maxpps)
2748 rv = 1;
2749 else
2750 rv = 0;
2752 #if 1 /* DIAGNOSTIC? */
2753 /* be careful about wrap-around */
2754 if (*curpps + 1 > *curpps)
2755 *curpps = *curpps + 1;
2756 #else
2758 * assume that there's not too many calls to this function.
2759 * not sure if the assumption holds, as it depends on *caller's*
2760 * behavior, not the behavior of this function.
2761 * IMHO it is wrong to make assumption on the caller's behavior,
2762 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
2764 *curpps = *curpps + 1;
2765 #endif
2767 return (rv);
2769 #endif
2772 * Perform rate limit check.
2773 * Returns 0 if it is okay to send the icmp6 packet.
2774 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2775 * limitation.
2777 * XXX per-destination/type check necessary?
2779 static int
2780 icmp6_ratelimit(const struct in6_addr *dst, /* not used at this moment */
2781 const int type, /* not used at this moment */
2782 const int code) /* not used at this moment */
2784 int ret;
2786 ret = 0; /* okay to send */
2788 /* PPS limit */
2789 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2790 icmp6errppslim)) {
2791 /* The packet is subject to rate limit */
2792 ret++;
2795 return ret;