linprocfs - Introduce /proc/mounts
[dragonfly.git] / sys / netinet6 / icmp6.c
blob9d454e6b7233a27085e18c1c5d586b287bf80df2
1 /* $FreeBSD: src/sys/netinet6/icmp6.c,v 1.6.2.13 2003/05/06 06:46:58 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/icmp6.c,v 1.31 2008/10/27 02:56:30 sephe Exp $ */
3 /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */
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/socketops.h>
81 #include <sys/time.h>
82 #include <sys/kernel.h>
83 #include <sys/syslog.h>
84 #include <sys/domain.h>
86 #include <net/if.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
89 #include <net/if_types.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet/icmp6.h>
96 #include <netinet6/mld6_var.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet6/nd6.h>
100 #include <netinet6/in6_ifattach.h>
101 #include <netinet6/ip6protosw.h>
103 #ifdef IPSEC
104 #include <netinet6/ipsec.h>
105 #include <netproto/key/key.h>
106 #endif
108 #ifdef FAST_IPSEC
109 #include <netproto/ipsec/ipsec.h>
110 #include <netproto/ipsec/key.h>
111 #define IPSEC
112 #endif
114 #include <net/net_osdep.h>
116 #ifdef HAVE_NRL_INPCB
117 /* inpcb members */
118 #define in6pcb inpcb
119 #define in6p_laddr inp_laddr6
120 #define in6p_faddr inp_faddr6
121 #define in6p_icmp6filt inp_icmp6filt
122 #define in6p_route inp_route
123 #define in6p_socket inp_socket
124 #define in6p_flags inp_flags
125 #define in6p_moptions inp_moptions6
126 #define in6p_outputopts inp_outputopts6
127 #define in6p_ip6 inp_ipv6
128 #define in6p_flowinfo inp_flowinfo
129 #define in6p_sp inp_sp
130 #define in6p_next inp_next
131 #define in6p_prev inp_prev
132 /* function names */
133 #define in6_pcbdetach in_pcbdetach
134 #define in6_rtchange in_rtchange
137 * for KAME src sync over BSD*'s. XXX: FreeBSD (>=3) are VERY different from
138 * others...
140 #define in6p_ip6_nxt inp_ipv6.ip6_nxt
141 #endif
144 * ppratecheck() is available, so define it here.
146 #define HAVE_PPSRATECHECK
148 extern struct domain inet6domain;
149 extern struct ip6protosw inet6sw[];
150 extern u_char ip6_protox[];
152 struct icmp6stat icmp6stat;
154 extern struct inpcbinfo ripcbinfo;
155 extern int icmp6errppslim;
156 static int icmp6errpps_count = 0;
157 static struct timeval icmp6errppslim_last;
158 extern int icmp6_nodeinfo;
160 static void icmp6_errcount (struct icmp6errstat *, int, int);
161 static int icmp6_rip6_input (struct mbuf **, int);
162 static int icmp6_ratelimit (const struct in6_addr *, const int, const int);
163 static const char *icmp6_redirect_diag (struct in6_addr *,
164 struct in6_addr *, struct in6_addr *);
165 #ifndef HAVE_PPSRATECHECK
166 static int ppsratecheck (struct timeval *, int *, int);
167 #endif
168 static struct mbuf *ni6_input (struct mbuf *, int);
169 static struct mbuf *ni6_nametodns (const char *, int, int);
170 static int ni6_dnsmatch (const char *, int, const char *, int);
171 static int ni6_addrs (struct icmp6_nodeinfo *, struct mbuf *,
172 struct ifnet **, char *);
173 static int ni6_store_addrs (struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
174 struct ifnet *, int);
175 static int icmp6_notify_error (struct mbuf *, int, int, int);
177 #ifdef COMPAT_RFC1885
178 static struct route_in6 icmp6_reflect_rt;
179 #endif
182 void
183 icmp6_init(void)
185 mld6_init();
188 static void
189 icmp6_errcount(struct icmp6errstat *stat, int type, int code)
191 switch (type) {
192 case ICMP6_DST_UNREACH:
193 switch (code) {
194 case ICMP6_DST_UNREACH_NOROUTE:
195 stat->icp6errs_dst_unreach_noroute++;
196 return;
197 case ICMP6_DST_UNREACH_ADMIN:
198 stat->icp6errs_dst_unreach_admin++;
199 return;
200 case ICMP6_DST_UNREACH_BEYONDSCOPE:
201 stat->icp6errs_dst_unreach_beyondscope++;
202 return;
203 case ICMP6_DST_UNREACH_ADDR:
204 stat->icp6errs_dst_unreach_addr++;
205 return;
206 case ICMP6_DST_UNREACH_NOPORT:
207 stat->icp6errs_dst_unreach_noport++;
208 return;
210 break;
211 case ICMP6_PACKET_TOO_BIG:
212 stat->icp6errs_packet_too_big++;
213 return;
214 case ICMP6_TIME_EXCEEDED:
215 switch (code) {
216 case ICMP6_TIME_EXCEED_TRANSIT:
217 stat->icp6errs_time_exceed_transit++;
218 return;
219 case ICMP6_TIME_EXCEED_REASSEMBLY:
220 stat->icp6errs_time_exceed_reassembly++;
221 return;
223 break;
224 case ICMP6_PARAM_PROB:
225 switch (code) {
226 case ICMP6_PARAMPROB_HEADER:
227 stat->icp6errs_paramprob_header++;
228 return;
229 case ICMP6_PARAMPROB_NEXTHEADER:
230 stat->icp6errs_paramprob_nextheader++;
231 return;
232 case ICMP6_PARAMPROB_OPTION:
233 stat->icp6errs_paramprob_option++;
234 return;
236 break;
237 case ND_REDIRECT:
238 stat->icp6errs_redirect++;
239 return;
241 stat->icp6errs_unknown++;
245 * Generate an error packet of type error in response to bad IP6 packet.
247 void
248 icmp6_error(struct mbuf *m, int type, int code, int param)
250 struct ip6_hdr *oip6, *nip6;
251 struct icmp6_hdr *icmp6;
252 u_int preplen;
253 int off;
254 int nxt;
256 icmp6stat.icp6s_error++;
258 /* count per-type-code statistics */
259 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
261 #ifdef M_DECRYPTED /*not openbsd*/
262 if (m->m_flags & M_DECRYPTED) {
263 icmp6stat.icp6s_canterror++;
264 goto freeit;
266 #endif
268 #ifndef PULLDOWN_TEST
269 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
270 #else
271 if (m->m_len < sizeof(struct ip6_hdr)) {
272 m = m_pullup(m, sizeof(struct ip6_hdr));
273 if (m == NULL)
274 return;
276 #endif
277 oip6 = mtod(m, struct ip6_hdr *);
280 * Multicast destination check. For unrecognized option errors,
281 * this check has already done in ip6_unknown_opt(), so we can
282 * check only for other errors.
284 if ((m->m_flags & (M_BCAST|M_MCAST) ||
285 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
286 (type != ICMP6_PACKET_TOO_BIG &&
287 (type != ICMP6_PARAM_PROB ||
288 code != ICMP6_PARAMPROB_OPTION)))
289 goto freeit;
291 /* Source address check. XXX: the case of anycast source? */
292 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
293 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
294 goto freeit;
297 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
298 * don't do it.
300 nxt = -1;
301 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
302 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
303 struct icmp6_hdr *icp;
305 #ifndef PULLDOWN_TEST
306 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
307 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
308 #else
309 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
310 sizeof(*icp));
311 if (icp == NULL) {
312 icmp6stat.icp6s_tooshort++;
313 return;
315 #endif
316 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
317 icp->icmp6_type == ND_REDIRECT) {
319 * ICMPv6 error
320 * Special case: for redirect (which is
321 * informational) we must not send icmp6 error.
323 icmp6stat.icp6s_canterror++;
324 goto freeit;
325 } else {
326 /* ICMPv6 informational - send the error */
328 } else {
329 /* non-ICMPv6 - send the error */
332 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
334 /* Finally, do rate limitation check. */
335 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
336 icmp6stat.icp6s_toofreq++;
337 goto freeit;
341 * OK, ICMP6 can be generated.
344 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
345 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
347 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
348 M_PREPEND(m, preplen, MB_DONTWAIT);
349 if (m && m->m_len < preplen)
350 m = m_pullup(m, preplen);
351 if (m == NULL) {
352 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
353 return;
356 nip6 = mtod(m, struct ip6_hdr *);
357 nip6->ip6_src = oip6->ip6_src;
358 nip6->ip6_dst = oip6->ip6_dst;
360 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
361 oip6->ip6_src.s6_addr16[1] = 0;
362 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
363 oip6->ip6_dst.s6_addr16[1] = 0;
365 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
366 icmp6->icmp6_type = type;
367 icmp6->icmp6_code = code;
368 icmp6->icmp6_pptr = htonl((u_int32_t)param);
371 * icmp6_reflect() is designed to be in the input path.
372 * icmp6_error() can be called from both input and outut path,
373 * and if we are in output path rcvif could contain bogus value.
374 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
375 * information in ip header (nip6).
377 m->m_pkthdr.rcvif = NULL;
379 icmp6stat.icp6s_outhist[type]++;
380 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
382 return;
384 freeit:
386 * If we can't tell wheter or not we can generate ICMP6, free it.
388 m_freem(m);
392 * Process a received ICMP6 message.
395 icmp6_input(struct mbuf **mp, int *offp, int proto)
397 struct mbuf *m = *mp, *n;
398 struct ip6_hdr *ip6, *nip6;
399 struct icmp6_hdr *icmp6, *nicmp6;
400 int off = *offp;
401 int icmp6len = m->m_pkthdr.len - *offp;
402 int code, sum, noff;
404 #ifndef PULLDOWN_TEST
405 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
406 /* m might change if M_LOOP. So, call mtod after this */
407 #endif
410 * Locate icmp6 structure in mbuf, and check
411 * that not corrupted and of at least minimum length
414 ip6 = mtod(m, struct ip6_hdr *);
415 if (icmp6len < sizeof(struct icmp6_hdr)) {
416 icmp6stat.icp6s_tooshort++;
417 goto freeit;
421 * calculate the checksum
423 #ifndef PULLDOWN_TEST
424 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
425 #else
426 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
427 if (icmp6 == NULL) {
428 icmp6stat.icp6s_tooshort++;
429 return IPPROTO_DONE;
431 #endif
432 code = icmp6->icmp6_code;
434 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
435 nd6log((LOG_ERR,
436 "ICMP6 checksum error(%d|%x) %s\n",
437 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
438 icmp6stat.icp6s_checksum++;
439 goto freeit;
442 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
444 * Deliver very specific ICMP6 type only.
445 * This is important to deilver TOOBIG. Otherwise PMTUD
446 * will not work.
448 switch (icmp6->icmp6_type) {
449 case ICMP6_DST_UNREACH:
450 case ICMP6_PACKET_TOO_BIG:
451 case ICMP6_TIME_EXCEEDED:
452 break;
453 default:
454 goto freeit;
458 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
459 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
460 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
461 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
463 switch (icmp6->icmp6_type) {
464 case ICMP6_DST_UNREACH:
465 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
466 switch (code) {
467 case ICMP6_DST_UNREACH_NOROUTE:
468 code = PRC_UNREACH_NET;
469 break;
470 case ICMP6_DST_UNREACH_ADMIN:
471 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
472 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
473 break;
474 case ICMP6_DST_UNREACH_ADDR:
475 code = PRC_HOSTDEAD;
476 break;
477 #ifdef COMPAT_RFC1885
478 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
479 code = PRC_UNREACH_SRCFAIL;
480 break;
481 #else
482 case ICMP6_DST_UNREACH_BEYONDSCOPE:
483 /* I mean "source address was incorrect." */
484 code = PRC_PARAMPROB;
485 break;
486 #endif
487 case ICMP6_DST_UNREACH_NOPORT:
488 code = PRC_UNREACH_PORT;
489 break;
490 default:
491 goto badcode;
493 goto deliver;
494 break;
496 case ICMP6_PACKET_TOO_BIG:
497 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
498 if (code != 0)
499 goto badcode;
501 code = PRC_MSGSIZE;
504 * Updating the path MTU will be done after examining
505 * intermediate extension headers.
507 goto deliver;
508 break;
510 case ICMP6_TIME_EXCEEDED:
511 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
512 switch (code) {
513 case ICMP6_TIME_EXCEED_TRANSIT:
514 case ICMP6_TIME_EXCEED_REASSEMBLY:
515 code += PRC_TIMXCEED_INTRANS;
516 break;
517 default:
518 goto badcode;
520 goto deliver;
521 break;
523 case ICMP6_PARAM_PROB:
524 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
525 switch (code) {
526 case ICMP6_PARAMPROB_NEXTHEADER:
527 code = PRC_UNREACH_PROTOCOL;
528 break;
529 case ICMP6_PARAMPROB_HEADER:
530 case ICMP6_PARAMPROB_OPTION:
531 code = PRC_PARAMPROB;
532 break;
533 default:
534 goto badcode;
536 goto deliver;
537 break;
539 case ICMP6_ECHO_REQUEST:
540 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
541 if (code != 0)
542 goto badcode;
543 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
544 /* Give up remote */
545 break;
547 if ((n->m_flags & M_EXT) ||
548 n->m_len < off + sizeof(struct icmp6_hdr)) {
549 struct mbuf *n0 = n;
550 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
551 int n0len;
554 * Prepare an internal mbuf. m_pullup() doesn't
555 * always copy the length we specified.
557 if (maxlen >= MCLBYTES) {
558 /* Give up remote */
559 m_freem(n0);
560 break;
562 n = m_getb(maxlen, MB_DONTWAIT, n0->m_type, M_PKTHDR);
563 if (n == NULL) {
564 /* Give up remote */
565 m_freem(n0);
566 break;
568 n0len = n0->m_pkthdr.len; /* save for use below */
569 M_MOVE_PKTHDR(n, n0);
571 * Copy IPv6 and ICMPv6 only.
573 nip6 = mtod(n, struct ip6_hdr *);
574 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
575 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
576 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
577 noff = sizeof(struct ip6_hdr);
578 /* new mbuf contains only ipv6+icmpv6 headers */
579 n->m_len = noff + sizeof(struct icmp6_hdr);
581 * Adjust mbuf. ip6_plen will be adjusted in
582 * ip6_output().
584 m_adj(n0, off + sizeof(struct icmp6_hdr));
585 /* recalculate complete packet size */
586 n->m_pkthdr.len = n0len + (noff - off);
587 n->m_next = n0;
588 } else {
589 nip6 = mtod(n, struct ip6_hdr *);
590 nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
591 noff = off;
593 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
594 nicmp6->icmp6_code = 0;
595 if (n) {
596 icmp6stat.icp6s_reflect++;
597 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
598 icmp6_reflect(n, noff);
600 break;
602 case ICMP6_ECHO_REPLY:
603 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
604 if (code != 0)
605 goto badcode;
606 break;
608 case MLD_LISTENER_QUERY:
609 case MLD_LISTENER_REPORT:
610 if (icmp6len < sizeof(struct mld_hdr))
611 goto badlen;
612 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
613 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
614 else
615 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
616 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
617 /* give up local */
618 mld6_input(m, off);
619 m = NULL;
620 goto freeit;
622 mld6_input(n, off);
623 /* m stays. */
624 break;
626 case MLD_LISTENER_DONE:
627 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
628 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
629 goto badlen;
630 break; /* nothing to be done in kernel */
632 case MLD_MTRACE_RESP:
633 case MLD_MTRACE:
634 /* XXX: these two are experimental. not officially defind. */
635 /* XXX: per-interface statistics? */
636 break; /* just pass it to applications */
638 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
640 enum { WRU, FQDN } mode;
642 if (!icmp6_nodeinfo)
643 break;
645 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
646 mode = WRU;
647 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
648 mode = FQDN;
649 else
650 goto badlen;
652 #define hostnamelen strlen(hostname)
653 if (mode == FQDN) {
654 #ifndef PULLDOWN_TEST
655 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
656 IPPROTO_DONE);
657 #endif
658 n = m_copy(m, 0, M_COPYALL);
659 if (n)
660 n = ni6_input(n, off);
661 /* XXX meaningless if n == NULL */
662 noff = sizeof(struct ip6_hdr);
663 } else {
664 u_char *p;
665 int maxlen, maxhlen;
667 if ((icmp6_nodeinfo & 5) != 5)
668 break;
670 if (code != 0)
671 goto badcode;
672 maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
673 if (maxlen >= MCLBYTES) {
674 /* Give up remote */
675 break;
677 n = m_getb(maxlen, MB_DONTWAIT, m->m_type, M_PKTHDR);
678 if (n == NULL) {
679 /* Give up remote */
680 break;
682 if (!m_dup_pkthdr(n, m, MB_DONTWAIT)) {
684 * Previous code did a blind M_COPY_PKTHDR
685 * and said "just for rcvif". If true, then
686 * we could tolerate the dup failing (due to
687 * the deep copy of the tag chain). For now
688 * be conservative and just fail.
690 m_free(n);
691 break;
693 n->m_len = 0;
694 maxhlen = M_TRAILINGSPACE(n) - maxlen;
695 if (maxhlen > hostnamelen)
696 maxhlen = hostnamelen;
698 * Copy IPv6 and ICMPv6 only.
700 nip6 = mtod(n, struct ip6_hdr *);
701 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
702 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
703 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
704 p = (u_char *)(nicmp6 + 1);
705 bzero(p, 4);
706 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
707 noff = sizeof(struct ip6_hdr);
708 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
709 sizeof(struct icmp6_hdr) + 4 + maxhlen;
710 nicmp6->icmp6_type = ICMP6_WRUREPLY;
711 nicmp6->icmp6_code = 0;
713 #undef hostnamelen
714 if (n) {
715 icmp6stat.icp6s_reflect++;
716 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
717 icmp6_reflect(n, noff);
719 break;
722 case ICMP6_WRUREPLY:
723 if (code != 0)
724 goto badcode;
725 break;
727 case ND_ROUTER_SOLICIT:
728 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
729 if (code != 0)
730 goto badcode;
731 if (icmp6len < sizeof(struct nd_router_solicit))
732 goto badlen;
733 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
734 /* give up local */
735 nd6_rs_input(m, off, icmp6len);
736 m = NULL;
737 goto freeit;
739 nd6_rs_input(n, off, icmp6len);
740 /* m stays. */
741 break;
743 case ND_ROUTER_ADVERT:
744 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
745 if (code != 0)
746 goto badcode;
747 if (icmp6len < sizeof(struct nd_router_advert))
748 goto badlen;
749 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
750 /* give up local */
751 nd6_ra_input(m, off, icmp6len);
752 m = NULL;
753 goto freeit;
755 nd6_ra_input(n, off, icmp6len);
756 /* m stays. */
757 break;
759 case ND_NEIGHBOR_SOLICIT:
760 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
761 if (code != 0)
762 goto badcode;
763 if (icmp6len < sizeof(struct nd_neighbor_solicit))
764 goto badlen;
765 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
766 /* give up local */
767 nd6_ns_input(m, off, icmp6len);
768 m = NULL;
769 goto freeit;
771 nd6_ns_input(n, off, icmp6len);
772 /* m stays. */
773 break;
775 case ND_NEIGHBOR_ADVERT:
776 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
777 if (code != 0)
778 goto badcode;
779 if (icmp6len < sizeof(struct nd_neighbor_advert))
780 goto badlen;
781 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
782 /* give up local */
783 nd6_na_input(m, off, icmp6len);
784 m = NULL;
785 goto freeit;
787 nd6_na_input(n, off, icmp6len);
788 /* m stays. */
789 break;
791 case ND_REDIRECT:
792 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
793 if (code != 0)
794 goto badcode;
795 if (icmp6len < sizeof(struct nd_redirect))
796 goto badlen;
797 if ((n = m_copym(m, 0, M_COPYALL, MB_DONTWAIT)) == NULL) {
798 /* give up local */
799 icmp6_redirect_input(m, off);
800 m = NULL;
801 goto freeit;
803 icmp6_redirect_input(n, off);
804 /* m stays. */
805 break;
807 case ICMP6_ROUTER_RENUMBERING:
808 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
809 code != ICMP6_ROUTER_RENUMBERING_RESULT)
810 goto badcode;
811 if (icmp6len < sizeof(struct icmp6_router_renum))
812 goto badlen;
813 break;
815 default:
816 nd6log((LOG_DEBUG,
817 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
818 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
819 ip6_sprintf(&ip6->ip6_dst),
820 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
821 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
822 /* ICMPv6 error: MUST deliver it by spec... */
823 code = PRC_NCMDS;
824 /* deliver */
825 } else {
826 /* ICMPv6 informational: MUST not deliver */
827 break;
829 deliver:
830 if (icmp6_notify_error(m, off, icmp6len, code)) {
831 /* In this case, m should've been freed. */
832 return (IPPROTO_DONE);
834 break;
836 badcode:
837 icmp6stat.icp6s_badcode++;
838 break;
840 badlen:
841 icmp6stat.icp6s_badlen++;
842 break;
845 /* deliver the packet to appropriate sockets */
846 icmp6_rip6_input(&m, *offp);
848 return IPPROTO_DONE;
850 freeit:
851 m_freem(m);
852 return IPPROTO_DONE;
855 static int
856 icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
858 struct icmp6_hdr *icmp6;
859 struct ip6_hdr *eip6;
860 u_int32_t notifymtu;
861 struct sockaddr_in6 icmp6src, icmp6dst;
863 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
864 icmp6stat.icp6s_tooshort++;
865 goto freeit;
867 #ifndef PULLDOWN_TEST
868 IP6_EXTHDR_CHECK(m, off,
869 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
870 -1);
871 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
872 #else
873 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
874 sizeof(*icmp6) + sizeof(struct ip6_hdr));
875 if (icmp6 == NULL) {
876 icmp6stat.icp6s_tooshort++;
877 return (-1);
879 #endif
880 eip6 = (struct ip6_hdr *)(icmp6 + 1);
882 /* Detect the upper level protocol */
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 int rthlen;
894 while (1) { /* XXX: should avoid infinite loop explicitly? */
895 struct ip6_ext *eh;
897 switch (nxt) {
898 case IPPROTO_HOPOPTS:
899 case IPPROTO_DSTOPTS:
900 case IPPROTO_AH:
901 #ifndef PULLDOWN_TEST
902 IP6_EXTHDR_CHECK(m, 0, eoff +
903 sizeof(struct ip6_ext),
904 -1);
905 eh = (struct ip6_ext *)(mtod(m, caddr_t)
906 + eoff);
907 #else
908 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
909 eoff, sizeof(*eh));
910 if (eh == NULL) {
911 icmp6stat.icp6s_tooshort++;
912 return (-1);
914 #endif
916 if (nxt == IPPROTO_AH)
917 eoff += (eh->ip6e_len + 2) << 2;
918 else
919 eoff += (eh->ip6e_len + 1) << 3;
920 nxt = eh->ip6e_nxt;
921 break;
922 case IPPROTO_ROUTING:
924 * When the erroneous packet contains a
925 * routing header, we should examine the
926 * header to determine the final destination.
927 * Otherwise, we can't properly update
928 * information that depends on the final
929 * destination (e.g. path MTU).
931 #ifndef PULLDOWN_TEST
932 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
933 -1);
934 rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
935 + eoff);
936 #else
937 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
938 eoff, sizeof(*rth));
939 if (rth == NULL) {
940 icmp6stat.icp6s_tooshort++;
941 return (-1);
943 #endif
944 rthlen = (rth->ip6r_len + 1) << 3;
945 eoff += rthlen;
946 nxt = rth->ip6r_nxt;
947 break;
948 case IPPROTO_FRAGMENT:
949 #ifndef PULLDOWN_TEST
950 IP6_EXTHDR_CHECK(m, 0, eoff +
951 sizeof(struct ip6_frag),
952 -1);
953 fh = (struct ip6_frag *)(mtod(m, caddr_t)
954 + eoff);
955 #else
956 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
957 eoff, sizeof(*fh));
958 if (fh == NULL) {
959 icmp6stat.icp6s_tooshort++;
960 return (-1);
962 #endif
964 * Data after a fragment header is meaningless
965 * unless it is the first fragment, but
966 * we'll go to the notify label for path MTU
967 * discovery.
969 if (fh->ip6f_offlg & IP6F_OFF_MASK)
970 goto notify;
972 eoff += sizeof(struct ip6_frag);
973 nxt = fh->ip6f_nxt;
974 break;
975 default:
977 * This case includes ESP and the No Next
978 * Header. In such cases going to the notify
979 * label does not have any meaning
980 * (i.e. pr_ctlinput will be NULL), but we go
981 * anyway since we might have to update
982 * path MTU information.
984 goto notify;
987 notify:
988 #ifndef PULLDOWN_TEST
989 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
990 #else
991 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
992 sizeof(*icmp6) + sizeof(struct ip6_hdr));
993 if (icmp6 == NULL) {
994 icmp6stat.icp6s_tooshort++;
995 return (-1);
997 #endif
999 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1000 bzero(&icmp6dst, sizeof(icmp6dst));
1001 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1002 icmp6dst.sin6_family = AF_INET6;
1003 if (finaldst == NULL)
1004 icmp6dst.sin6_addr = eip6->ip6_dst;
1005 else
1006 icmp6dst.sin6_addr = *finaldst;
1007 icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1008 &icmp6dst.sin6_addr);
1009 if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
1010 NULL, NULL)) {
1011 /* should be impossbile */
1012 nd6log((LOG_DEBUG,
1013 "icmp6_notify_error: in6_embedscope failed\n"));
1014 goto freeit;
1018 * retrieve parameters from the inner IPv6 header, and convert
1019 * them into sockaddr structures.
1021 bzero(&icmp6src, sizeof(icmp6src));
1022 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1023 icmp6src.sin6_family = AF_INET6;
1024 icmp6src.sin6_addr = eip6->ip6_src;
1025 icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1026 &icmp6src.sin6_addr);
1027 if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
1028 NULL, NULL)) {
1029 /* should be impossbile */
1030 nd6log((LOG_DEBUG,
1031 "icmp6_notify_error: in6_embedscope failed\n"));
1032 goto freeit;
1034 icmp6src.sin6_flowinfo =
1035 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1037 if (finaldst == NULL)
1038 finaldst = &eip6->ip6_dst;
1039 ip6cp.ip6c_m = m;
1040 ip6cp.ip6c_icmp6 = icmp6;
1041 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1042 ip6cp.ip6c_off = eoff;
1043 ip6cp.ip6c_finaldst = finaldst;
1044 ip6cp.ip6c_src = &icmp6src;
1045 ip6cp.ip6c_nxt = nxt;
1047 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1048 notifymtu = ntohl(icmp6->icmp6_mtu);
1049 ip6cp.ip6c_cmdarg = (void *)&notifymtu;
1050 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1053 so_pru_ctlinput(
1054 (struct protosw *)&inet6sw[ip6_protox[nxt]],
1055 code, (struct sockaddr *)&icmp6dst, &ip6cp);
1057 return (0);
1059 freeit:
1060 m_freem(m);
1061 return (-1);
1064 void
1065 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1067 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1068 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1069 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1070 u_int mtu = ntohl(icmp6->icmp6_mtu);
1071 struct rtentry *rt = NULL;
1072 struct sockaddr_in6 sin6;
1074 if (!validated)
1075 return;
1077 bzero(&sin6, sizeof(sin6));
1078 sin6.sin6_family = PF_INET6;
1079 sin6.sin6_len = sizeof(struct sockaddr_in6);
1080 sin6.sin6_addr = *dst;
1081 /* XXX normally, this won't happen */
1082 if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1083 sin6.sin6_addr.s6_addr16[1] =
1084 htons(m->m_pkthdr.rcvif->if_index);
1086 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1087 rt = rtpurelookup((struct sockaddr *)&sin6);
1088 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
1089 !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1090 if (mtu < IPV6_MMTU) { /* XXX */
1091 rt->rt_rmx.rmx_locks |= RTV_MTU;
1092 } else if (mtu < rt->rt_ifp->if_mtu &&
1093 rt->rt_rmx.rmx_mtu > mtu) {
1094 icmp6stat.icp6s_pmtuchg++;
1095 rt->rt_rmx.rmx_mtu = mtu;
1098 if (rt)
1099 --rt->rt_refcnt;
1103 * Process a Node Information Query packet, based on
1104 * draft-ietf-ipngwg-icmp-name-lookups-07.
1106 * Spec incompatibilities:
1107 * - IPv6 Subject address handling
1108 * - IPv4 Subject address handling support missing
1109 * - Proxy reply (answer even if it's not for me)
1110 * - joins NI group address at in6_ifattach() time only, does not cope
1111 * with hostname changes by sethostname(3)
1113 #define hostnamelen strlen(hostname)
1114 static struct mbuf *
1115 ni6_input(struct mbuf *m, int off)
1117 struct icmp6_nodeinfo *ni6, *nni6;
1118 struct mbuf *n = NULL;
1119 u_int16_t qtype;
1120 int subjlen;
1121 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1122 struct ni_reply_fqdn *fqdn;
1123 int addrs; /* for NI_QTYPE_NODEADDR */
1124 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1125 struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1126 struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
1127 struct ip6_hdr *ip6;
1128 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1129 char *subj = NULL;
1130 struct in6_ifaddr *ia6 = NULL;
1132 ip6 = mtod(m, struct ip6_hdr *);
1133 #ifndef PULLDOWN_TEST
1134 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1135 #else
1136 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1137 if (ni6 == NULL) {
1138 /* m is already reclaimed */
1139 return NULL;
1141 #endif
1144 * Validate IPv6 destination address.
1146 * The Responder must discard the Query without further processing
1147 * unless it is one of the Responder's unicast or anycast addresses, or
1148 * a link-local scope multicast address which the Responder has joined.
1149 * [icmp-name-lookups-07, Section 4.]
1151 bzero(&sin6, sizeof(sin6));
1152 sin6.sin6_family = AF_INET6;
1153 sin6.sin6_len = sizeof(struct sockaddr_in6);
1154 bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1155 /* XXX scopeid */
1156 if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
1157 /* unicast/anycast, fine */
1158 if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1159 (icmp6_nodeinfo & 4) == 0) {
1160 nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1161 "a temporary address in %s:%d",
1162 __FILE__, __LINE__));
1163 goto bad;
1165 } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1166 ; /* link-local multicast, fine */
1167 else
1168 goto bad;
1170 /* validate query Subject field. */
1171 qtype = ntohs(ni6->ni_qtype);
1172 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1173 switch (qtype) {
1174 case NI_QTYPE_NOOP:
1175 case NI_QTYPE_SUPTYPES:
1176 /* 07 draft */
1177 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1178 break;
1179 /* FALLTHROUGH */
1180 case NI_QTYPE_FQDN:
1181 case NI_QTYPE_NODEADDR:
1182 switch (ni6->ni_code) {
1183 case ICMP6_NI_SUBJ_IPV6:
1184 #if ICMP6_NI_SUBJ_IPV6 != 0
1185 case 0:
1186 #endif
1188 * backward compatibility - try to accept 03 draft
1189 * format, where no Subject is present.
1191 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1192 subjlen == 0) {
1193 oldfqdn++;
1194 break;
1196 #if ICMP6_NI_SUBJ_IPV6 != 0
1197 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1198 goto bad;
1199 #endif
1201 if (subjlen != sizeof(sin6.sin6_addr))
1202 goto bad;
1205 * Validate Subject address.
1207 * Not sure what exactly "address belongs to the node"
1208 * means in the spec, is it just unicast, or what?
1210 * At this moment we consider Subject address as
1211 * "belong to the node" if the Subject address equals
1212 * to the IPv6 destination address; validation for
1213 * IPv6 destination address should have done enough
1214 * check for us.
1216 * We do not do proxy at this moment.
1218 /* m_pulldown instead of copy? */
1219 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1220 subjlen, (caddr_t)&sin6.sin6_addr);
1221 sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1222 &sin6.sin6_addr);
1223 in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL);
1224 bzero(&sin6_d, sizeof(sin6_d));
1225 sin6_d.sin6_family = AF_INET6; /* not used, actually */
1226 sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
1227 sin6_d.sin6_addr = ip6->ip6_dst;
1228 sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
1229 &ip6->ip6_dst);
1230 in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL);
1231 subj = (char *)&sin6;
1232 if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
1233 break;
1236 * XXX if we are to allow other cases, we should really
1237 * be careful about scope here.
1238 * basically, we should disallow queries toward IPv6
1239 * destination X with subject Y, if scope(X) > scope(Y).
1240 * if we allow scope(X) > scope(Y), it will result in
1241 * information leakage across scope boundary.
1243 goto bad;
1245 case ICMP6_NI_SUBJ_FQDN:
1247 * Validate Subject name with gethostname(3).
1249 * The behavior may need some debate, since:
1250 * - we are not sure if the node has FQDN as
1251 * hostname (returned by gethostname(3)).
1252 * - the code does wildcard match for truncated names.
1253 * however, we are not sure if we want to perform
1254 * wildcard match, if gethostname(3) side has
1255 * truncated hostname.
1257 n = ni6_nametodns(hostname, hostnamelen, 0);
1258 if (!n || n->m_next || n->m_len == 0)
1259 goto bad;
1260 IP6_EXTHDR_GET(subj, char *, m,
1261 off + sizeof(struct icmp6_nodeinfo), subjlen);
1262 if (subj == NULL)
1263 goto bad;
1264 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1265 n->m_len)) {
1266 goto bad;
1268 m_freem(n);
1269 n = NULL;
1270 break;
1272 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1273 default:
1274 goto bad;
1276 break;
1279 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1280 switch (qtype) {
1281 case NI_QTYPE_FQDN:
1282 if ((icmp6_nodeinfo & 1) == 0)
1283 goto bad;
1284 break;
1285 case NI_QTYPE_NODEADDR:
1286 if ((icmp6_nodeinfo & 2) == 0)
1287 goto bad;
1288 break;
1291 /* guess reply length */
1292 switch (qtype) {
1293 case NI_QTYPE_NOOP:
1294 break; /* no reply data */
1295 case NI_QTYPE_SUPTYPES:
1296 replylen += sizeof(u_int32_t);
1297 break;
1298 case NI_QTYPE_FQDN:
1299 /* XXX will append an mbuf */
1300 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1301 break;
1302 case NI_QTYPE_NODEADDR:
1303 addrs = ni6_addrs(ni6, m, &ifp, subj);
1304 if ((replylen += addrs * (sizeof(struct in6_addr) +
1305 sizeof(u_int32_t))) > MCLBYTES)
1306 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1307 break;
1308 default:
1310 * XXX: We must return a reply with the ICMP6 code
1311 * `unknown Qtype' in this case. However we regard the case
1312 * as an FQDN query for backward compatibility.
1313 * Older versions set a random value to this field,
1314 * so it rarely varies in the defined qtypes.
1315 * But the mechanism is not reliable...
1316 * maybe we should obsolete older versions.
1318 qtype = NI_QTYPE_FQDN;
1319 /* XXX will append an mbuf */
1320 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1321 oldfqdn++;
1322 break;
1325 /* allocate an mbuf to reply. */
1326 if (replylen > MCLBYTES) {
1328 * XXX: should we try to allocate more? But MCLBYTES
1329 * is probably much larger than IPV6_MMTU...
1331 goto bad;
1333 n = m_getb(replylen, MB_DONTWAIT, m->m_type, M_PKTHDR);
1334 if (n == NULL) {
1335 m_freem(m);
1336 return (NULL);
1338 M_MOVE_PKTHDR(n, m); /* just for recvif */
1339 n->m_pkthdr.len = n->m_len = replylen;
1341 /* copy mbuf header and IPv6 + Node Information base headers */
1342 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1343 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1344 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1346 /* qtype dependent procedure */
1347 switch (qtype) {
1348 case NI_QTYPE_NOOP:
1349 nni6->ni_code = ICMP6_NI_SUCCESS;
1350 nni6->ni_flags = 0;
1351 break;
1352 case NI_QTYPE_SUPTYPES:
1354 u_int32_t v;
1355 nni6->ni_code = ICMP6_NI_SUCCESS;
1356 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1357 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1358 v = (u_int32_t)htonl(0x0000000f);
1359 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1360 break;
1362 case NI_QTYPE_FQDN:
1363 nni6->ni_code = ICMP6_NI_SUCCESS;
1364 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1365 sizeof(struct ip6_hdr) +
1366 sizeof(struct icmp6_nodeinfo));
1367 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1368 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1370 * XXX do we really have FQDN in variable "hostname"?
1372 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1373 if (n->m_next == NULL)
1374 goto bad;
1375 /* XXX we assume that n->m_next is not a chain */
1376 if (n->m_next->m_next != NULL)
1377 goto bad;
1378 n->m_pkthdr.len += n->m_next->m_len;
1379 break;
1380 case NI_QTYPE_NODEADDR:
1382 int lenlim, copied;
1384 nni6->ni_code = ICMP6_NI_SUCCESS;
1385 n->m_pkthdr.len = n->m_len =
1386 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1387 lenlim = M_TRAILINGSPACE(n);
1388 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1389 /* XXX: reset mbuf length */
1390 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1391 sizeof(struct icmp6_nodeinfo) + copied;
1392 break;
1394 default:
1395 break; /* XXX impossible! */
1398 nni6->ni_type = ICMP6_NI_REPLY;
1399 m_freem(m);
1400 return (n);
1402 bad:
1403 m_freem(m);
1404 if (n)
1405 m_freem(n);
1406 return (NULL);
1408 #undef hostnamelen
1411 * make a mbuf with DNS-encoded string. no compression support.
1413 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1414 * treated as truncated name (two \0 at the end). this is a wild guess.
1416 static struct mbuf *
1417 ni6_nametodns(const char *name, int namelen,
1418 int old) /* return pascal string if non-zero */
1420 struct mbuf *m;
1421 char *cp, *ep;
1422 const char *p, *q;
1423 int i, len, nterm;
1425 if (old)
1426 len = namelen + 1;
1427 else
1428 len = MCLBYTES;
1430 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1431 m = m_getb(len, MB_DONTWAIT, MT_DATA, 0);
1432 if (!m)
1433 goto fail;
1435 if (old) {
1436 m->m_len = len;
1437 *mtod(m, char *) = namelen;
1438 bcopy(name, mtod(m, char *) + 1, namelen);
1439 return m;
1440 } else {
1441 m->m_len = 0;
1442 cp = mtod(m, char *);
1443 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1445 /* if not certain about my name, return empty buffer */
1446 if (namelen == 0)
1447 return m;
1450 * guess if it looks like shortened hostname, or FQDN.
1451 * shortened hostname needs two trailing "\0".
1453 i = 0;
1454 for (p = name; p < name + namelen; p++) {
1455 if (*p && *p == '.')
1456 i++;
1458 if (i < 2)
1459 nterm = 2;
1460 else
1461 nterm = 1;
1463 p = name;
1464 while (cp < ep && p < name + namelen) {
1465 i = 0;
1466 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1467 i++;
1468 /* result does not fit into mbuf */
1469 if (cp + i + 1 >= ep)
1470 goto fail;
1472 * DNS label length restriction, RFC1035 page 8.
1473 * "i == 0" case is included here to avoid returning
1474 * 0-length label on "foo..bar".
1476 if (i <= 0 || i >= 64)
1477 goto fail;
1478 *cp++ = i;
1479 bcopy(p, cp, i);
1480 cp += i;
1481 p = q;
1482 if (p < name + namelen && *p == '.')
1483 p++;
1485 /* termination */
1486 if (cp + nterm >= ep)
1487 goto fail;
1488 while (nterm-- > 0)
1489 *cp++ = '\0';
1490 m->m_len = cp - mtod(m, char *);
1491 return m;
1494 panic("should not reach here");
1495 /* NOTREACHED */
1497 fail:
1498 if (m)
1499 m_freem(m);
1500 return NULL;
1504 * check if two DNS-encoded string matches. takes care of truncated
1505 * form (with \0\0 at the end). no compression support.
1506 * XXX upper/lowercase match (see RFC2065)
1508 static int
1509 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1511 const char *a0, *b0;
1512 int l;
1514 /* simplest case - need validation? */
1515 if (alen == blen && bcmp(a, b, alen) == 0)
1516 return 1;
1518 a0 = a;
1519 b0 = b;
1521 /* termination is mandatory */
1522 if (alen < 2 || blen < 2)
1523 return 0;
1524 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1525 return 0;
1526 alen--;
1527 blen--;
1529 while (a - a0 < alen && b - b0 < blen) {
1530 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1531 return 0;
1533 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1534 return 0;
1535 /* we don't support compression yet */
1536 if (a[0] >= 64 || b[0] >= 64)
1537 return 0;
1539 /* truncated case */
1540 if (a[0] == 0 && a - a0 == alen - 1)
1541 return 1;
1542 if (b[0] == 0 && b - b0 == blen - 1)
1543 return 1;
1544 if (a[0] == 0 || b[0] == 0)
1545 return 0;
1547 if (a[0] != b[0])
1548 return 0;
1549 l = a[0];
1550 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1551 return 0;
1552 if (bcmp(a + 1, b + 1, l) != 0)
1553 return 0;
1555 a += 1 + l;
1556 b += 1 + l;
1559 if (a - a0 == alen && b - b0 == blen)
1560 return 1;
1561 else
1562 return 0;
1566 * calculate the number of addresses to be returned in the node info reply.
1568 static int
1569 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1570 char *subj)
1572 struct ifnet *ifp;
1573 struct in6_ifaddr *ifa6;
1574 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1575 int addrs = 0, addrsofif, iffound = 0;
1576 int niflags = ni6->ni_flags;
1578 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1579 switch (ni6->ni_code) {
1580 case ICMP6_NI_SUBJ_IPV6:
1581 if (subj == NULL) /* must be impossible... */
1582 return (0);
1583 subj_ip6 = (struct sockaddr_in6 *)subj;
1584 break;
1585 default:
1587 * XXX: we only support IPv6 subject address for
1588 * this Qtype.
1590 return (0);
1594 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1596 struct ifaddr_container *ifac;
1598 addrsofif = 0;
1599 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link)
1601 struct ifaddr *ifa = ifac->ifa;
1603 if (ifa->ifa_addr->sa_family != AF_INET6)
1604 continue;
1605 ifa6 = (struct in6_ifaddr *)ifa;
1607 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1608 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1609 &ifa6->ia_addr.sin6_addr))
1610 iffound = 1;
1613 * IPv4-mapped addresses can only be returned by a
1614 * Node Information proxy, since they represent
1615 * addresses of IPv4-only nodes, which perforce do
1616 * not implement this protocol.
1617 * [icmp-name-lookups-07, Section 5.4]
1618 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1619 * this function at this moment.
1622 /* What do we have to do about ::1? */
1623 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1624 case IPV6_ADDR_SCOPE_LINKLOCAL:
1625 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1626 continue;
1627 break;
1628 case IPV6_ADDR_SCOPE_SITELOCAL:
1629 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1630 continue;
1631 break;
1632 case IPV6_ADDR_SCOPE_GLOBAL:
1633 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1634 continue;
1635 break;
1636 default:
1637 continue;
1641 * check if anycast is okay.
1642 * XXX: just experimental. not in the spec.
1644 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) &&
1645 !(niflags & NI_NODEADDR_FLAG_ANYCAST))
1646 continue; /* we need only unicast addresses */
1647 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) &&
1648 (icmp6_nodeinfo & 4) == 0) {
1649 continue;
1651 addrsofif++; /* count the address */
1653 if (iffound) {
1654 *ifpp = ifp;
1655 return (addrsofif);
1658 addrs += addrsofif;
1661 return (addrs);
1664 static int
1665 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1666 struct ifnet *ifp0, int resid)
1668 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1669 struct in6_ifaddr *ifa6;
1670 struct ifnet *ifp_dep = NULL;
1671 int copied = 0, allow_deprecated = 0;
1672 u_char *cp = (u_char *)(nni6 + 1);
1673 int niflags = ni6->ni_flags;
1674 u_int32_t ltime;
1676 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1677 return (0); /* needless to copy */
1679 again:
1681 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1683 struct ifaddr_container *ifac;
1685 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1686 struct ifaddr *ifa = ifac->ifa;
1688 if (ifa->ifa_addr->sa_family != AF_INET6)
1689 continue;
1690 ifa6 = (struct in6_ifaddr *)ifa;
1692 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) &&
1693 allow_deprecated == 0) {
1695 * prefererred address should be put before
1696 * deprecated addresses.
1699 /* record the interface for later search */
1700 if (ifp_dep == NULL)
1701 ifp_dep = ifp;
1703 continue;
1705 else if (!(ifa6->ia6_flags & IN6_IFF_DEPRECATED) &&
1706 allow_deprecated != 0)
1707 continue; /* we now collect deprecated addrs */
1709 /* What do we have to do about ::1? */
1710 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1711 case IPV6_ADDR_SCOPE_LINKLOCAL:
1712 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1713 continue;
1714 break;
1715 case IPV6_ADDR_SCOPE_SITELOCAL:
1716 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1717 continue;
1718 break;
1719 case IPV6_ADDR_SCOPE_GLOBAL:
1720 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1721 continue;
1722 break;
1723 default:
1724 continue;
1728 * check if anycast is okay.
1729 * XXX: just experimental. not in the spec.
1731 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) &&
1732 !(niflags & NI_NODEADDR_FLAG_ANYCAST))
1733 continue;
1734 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) &&
1735 (icmp6_nodeinfo & 4) == 0) {
1736 continue;
1739 /* now we can copy the address */
1740 if (resid < sizeof(struct in6_addr) +
1741 sizeof(u_int32_t)) {
1743 * We give up much more copy.
1744 * Set the truncate flag and return.
1746 nni6->ni_flags |=
1747 NI_NODEADDR_FLAG_TRUNCATE;
1748 return (copied);
1752 * Set the TTL of the address.
1753 * The TTL value should be one of the following
1754 * according to the specification:
1756 * 1. The remaining lifetime of a DHCP lease on the
1757 * address, or
1758 * 2. The remaining Valid Lifetime of a prefix from
1759 * which the address was derived through Stateless
1760 * Autoconfiguration.
1762 * Note that we currently do not support stateful
1763 * address configuration by DHCPv6, so the former
1764 * case can't happen.
1766 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1767 ltime = ND6_INFINITE_LIFETIME;
1768 else {
1769 if (ifa6->ia6_lifetime.ia6t_expire >
1770 time_second)
1771 ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1772 else
1773 ltime = 0;
1776 bcopy(&ltime, cp, sizeof(u_int32_t));
1777 cp += sizeof(u_int32_t);
1779 /* copy the address itself */
1780 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1781 sizeof(struct in6_addr));
1782 /* XXX: KAME link-local hack; remove ifindex */
1783 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1784 ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1785 cp += sizeof(struct in6_addr);
1787 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1788 copied += (sizeof(struct in6_addr) +
1789 sizeof(u_int32_t));
1791 if (ifp0) /* we need search only on the specified IF */
1792 break;
1795 if (allow_deprecated == 0 && ifp_dep != NULL) {
1796 ifp = ifp_dep;
1797 allow_deprecated = 1;
1799 goto again;
1802 return (copied);
1806 * XXX almost dup'ed code with rip6_input.
1808 static int
1809 icmp6_rip6_input(struct mbuf **mp, int off)
1811 struct mbuf *m = *mp;
1812 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1813 struct in6pcb *in6p;
1814 struct in6pcb *last = NULL;
1815 struct sockaddr_in6 rip6src;
1816 struct icmp6_hdr *icmp6;
1817 struct mbuf *opts = NULL;
1819 #ifndef PULLDOWN_TEST
1820 /* this is assumed to be safe. */
1821 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1822 #else
1823 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1824 if (icmp6 == NULL) {
1825 /* m is already reclaimed */
1826 return IPPROTO_DONE;
1828 #endif
1830 bzero(&rip6src, sizeof(rip6src));
1831 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1832 rip6src.sin6_family = AF_INET6;
1833 /* KAME hack: recover scopeid */
1834 in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1836 LIST_FOREACH(in6p, &ripcbinfo.pcblisthead, inp_list)
1838 if (in6p->inp_flags & INP_PLACEMARKER)
1839 continue;
1840 if ((in6p->inp_vflag & INP_IPV6) == 0)
1841 continue;
1842 #ifdef HAVE_NRL_INPCB
1843 if (!(in6p->in6p_flags & INP_IPV6))
1844 continue;
1845 #endif
1846 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1847 continue;
1848 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1849 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1850 continue;
1851 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1852 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1853 continue;
1854 if (in6p->in6p_icmp6filt
1855 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1856 in6p->in6p_icmp6filt))
1857 continue;
1858 if (last) {
1859 struct mbuf *n;
1860 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1861 if (last->in6p_flags & IN6P_CONTROLOPTS)
1862 ip6_savecontrol(last, &opts, ip6, n);
1863 /* strip intermediate headers */
1864 m_adj(n, off);
1865 if (ssb_appendaddr(&last->in6p_socket->so_rcv,
1866 (struct sockaddr *)&rip6src,
1867 n, opts) == 0) {
1868 /* should notify about lost packet */
1869 m_freem(n);
1870 if (opts) {
1871 m_freem(opts);
1873 } else
1874 sorwakeup(last->in6p_socket);
1875 opts = NULL;
1878 last = in6p;
1880 if (last) {
1881 if (last->in6p_flags & IN6P_CONTROLOPTS)
1882 ip6_savecontrol(last, &opts, ip6, m);
1883 /* strip intermediate headers */
1884 m_adj(m, off);
1885 if (ssb_appendaddr(&last->in6p_socket->so_rcv,
1886 (struct sockaddr *)&rip6src, m, opts) == 0) {
1887 m_freem(m);
1888 if (opts)
1889 m_freem(opts);
1890 } else
1891 sorwakeup(last->in6p_socket);
1892 } else {
1893 m_freem(m);
1894 ip6stat.ip6s_delivered--;
1896 return IPPROTO_DONE;
1900 * Reflect the ip6 packet back to the source.
1901 * OFF points to the icmp6 header, counted from the top of the mbuf.
1903 void
1904 icmp6_reflect(struct mbuf *m, size_t off)
1906 struct ip6_hdr *ip6;
1907 struct icmp6_hdr *icmp6;
1908 struct in6_ifaddr *ia;
1909 struct in6_addr t, *src = 0;
1910 int plen;
1911 int type, code;
1912 struct ifnet *outif = NULL;
1913 struct sockaddr_in6 sa6_src, sa6_dst;
1914 #ifdef COMPAT_RFC1885
1915 int mtu = IPV6_MMTU;
1916 struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
1917 #endif
1919 /* too short to reflect */
1920 if (off < sizeof(struct ip6_hdr)) {
1921 nd6log((LOG_DEBUG,
1922 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1923 (u_long)off, (u_long)sizeof(struct ip6_hdr),
1924 __FILE__, __LINE__));
1925 goto bad;
1929 * If there are extra headers between IPv6 and ICMPv6, strip
1930 * off that header first.
1932 #ifdef DIAGNOSTIC
1933 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
1934 panic("assumption failed in icmp6_reflect");
1935 #endif
1936 if (off > sizeof(struct ip6_hdr)) {
1937 size_t l;
1938 struct ip6_hdr nip6;
1940 l = off - sizeof(struct ip6_hdr);
1941 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
1942 m_adj(m, l);
1943 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1944 if (m->m_len < l) {
1945 if ((m = m_pullup(m, l)) == NULL)
1946 return;
1948 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
1949 } else /* off == sizeof(struct ip6_hdr) */ {
1950 size_t l;
1951 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1952 if (m->m_len < l) {
1953 if ((m = m_pullup(m, l)) == NULL)
1954 return;
1957 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
1958 ip6 = mtod(m, struct ip6_hdr *);
1959 ip6->ip6_nxt = IPPROTO_ICMPV6;
1960 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
1961 type = icmp6->icmp6_type; /* keep type for statistics */
1962 code = icmp6->icmp6_code; /* ditto. */
1964 t = ip6->ip6_dst;
1966 * ip6_input() drops a packet if its src is multicast.
1967 * So, the src is never multicast.
1969 ip6->ip6_dst = ip6->ip6_src;
1972 * XXX: make sure to embed scope zone information, using
1973 * already embedded IDs or the received interface (if any).
1974 * Note that rcvif may be NULL.
1975 * TODO: scoped routing case (XXX).
1977 bzero(&sa6_src, sizeof(sa6_src));
1978 sa6_src.sin6_family = AF_INET6;
1979 sa6_src.sin6_len = sizeof(sa6_src);
1980 sa6_src.sin6_addr = ip6->ip6_dst;
1981 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
1982 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
1983 bzero(&sa6_dst, sizeof(sa6_dst));
1984 sa6_dst.sin6_family = AF_INET6;
1985 sa6_dst.sin6_len = sizeof(sa6_dst);
1986 sa6_dst.sin6_addr = t;
1987 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
1988 in6_embedscope(&t, &sa6_dst, NULL, NULL);
1990 #ifdef COMPAT_RFC1885
1992 * xxx guess MTU
1993 * RFC 1885 requires that echo reply should be truncated if it
1994 * does not fit in with (return) path MTU, but the description was
1995 * removed in the new spec.
1997 if (icmp6_reflect_rt.ro_rt == 0 ||
1998 ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
1999 if (icmp6_reflect_rt.ro_rt) {
2000 RTFREE(icmp6_reflect_rt.ro_rt);
2001 icmp6_reflect_rt.ro_rt = 0;
2003 bzero(sin6, sizeof(*sin6));
2004 sin6->sin6_family = PF_INET6;
2005 sin6->sin6_len = sizeof(struct sockaddr_in6);
2006 sin6->sin6_addr = ip6->ip6_dst;
2008 rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
2009 RTF_PRCLONING);
2012 if (icmp6_reflect_rt.ro_rt == 0)
2013 goto bad;
2015 if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
2016 && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
2017 mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
2019 if (mtu < m->m_pkthdr.len) {
2020 plen -= (m->m_pkthdr.len - mtu);
2021 m_adj(m, mtu - m->m_pkthdr.len);
2023 #endif
2025 * If the incoming packet was addressed directly to us(i.e. unicast),
2026 * use dst as the src for the reply.
2027 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2028 * (for example) when we encounter an error while forwarding procedure
2029 * destined to a duplicated address of ours.
2031 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2032 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2033 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2034 src = &t;
2035 break;
2037 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2039 * This is the case if the dst is our link-local address
2040 * and the sender is also ourselves.
2042 src = &t;
2045 if (src == 0) {
2046 int e;
2047 struct route_in6 ro;
2050 * This case matches to multicasts, our anycast, or unicasts
2051 * that we do not own. Select a source address based on the
2052 * source address of the erroneous packet.
2054 bzero(&ro, sizeof(ro));
2055 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e, NULL);
2056 if (ro.ro_rt)
2057 RTFREE(ro.ro_rt); /* XXX: we could use this */
2058 if (src == NULL) {
2059 nd6log((LOG_DEBUG,
2060 "icmp6_reflect: source can't be determined: "
2061 "dst=%s, error=%d\n",
2062 ip6_sprintf(&sa6_src.sin6_addr), e));
2063 goto bad;
2067 ip6->ip6_src = *src;
2069 ip6->ip6_flow = 0;
2070 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2071 ip6->ip6_vfc |= IPV6_VERSION;
2072 ip6->ip6_nxt = IPPROTO_ICMPV6;
2073 if (m->m_pkthdr.rcvif) {
2074 /* XXX: This may not be the outgoing interface */
2075 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2076 } else
2077 ip6->ip6_hlim = ip6_defhlim;
2079 icmp6->icmp6_cksum = 0;
2080 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2081 sizeof(struct ip6_hdr), plen);
2084 * XXX option handling
2087 m->m_flags &= ~(M_BCAST|M_MCAST);
2089 #ifdef COMPAT_RFC1885
2090 ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
2091 #else
2092 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2093 #endif
2094 if (outif)
2095 icmp6_ifoutstat_inc(outif, type, code);
2097 return;
2099 bad:
2100 m_freem(m);
2101 return;
2104 void
2105 icmp6_fasttimo(void)
2108 mld6_fasttimeo();
2111 static const char *
2112 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2113 struct in6_addr *tgt6)
2115 static char buf[1024];
2116 ksnprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2117 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2118 return buf;
2121 void
2122 icmp6_redirect_input(struct mbuf *m, int off)
2124 struct ifnet *ifp = m->m_pkthdr.rcvif;
2125 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2126 struct nd_redirect *nd_rd;
2127 int icmp6len = ntohs(ip6->ip6_plen);
2128 char *lladdr = NULL;
2129 int lladdrlen = 0;
2130 u_char *redirhdr = NULL;
2131 int redirhdrlen = 0;
2132 struct rtentry *rt = NULL;
2133 int is_router;
2134 int is_onlink;
2135 struct in6_addr src6 = ip6->ip6_src;
2136 struct in6_addr redtgt6;
2137 struct in6_addr reddst6;
2138 union nd_opts ndopts;
2140 if (!m || !ifp)
2141 return;
2143 /* XXX if we are router, we don't update route by icmp6 redirect */
2144 if (ip6_forwarding)
2145 goto freeit;
2146 if (!icmp6_rediraccept)
2147 goto freeit;
2149 #ifndef PULLDOWN_TEST
2150 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2151 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2152 #else
2153 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2154 if (nd_rd == NULL) {
2155 icmp6stat.icp6s_tooshort++;
2156 return;
2158 #endif
2159 redtgt6 = nd_rd->nd_rd_target;
2160 reddst6 = nd_rd->nd_rd_dst;
2162 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2163 redtgt6.s6_addr16[1] = htons(ifp->if_index);
2164 if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2165 reddst6.s6_addr16[1] = htons(ifp->if_index);
2167 /* validation */
2168 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2169 nd6log((LOG_ERR,
2170 "ICMP6 redirect sent from %s rejected; "
2171 "must be from linklocal\n", ip6_sprintf(&src6)));
2172 goto bad;
2174 if (ip6->ip6_hlim != 255) {
2175 nd6log((LOG_ERR,
2176 "ICMP6 redirect sent from %s rejected; "
2177 "hlim=%d (must be 255)\n",
2178 ip6_sprintf(&src6), ip6->ip6_hlim));
2179 goto bad;
2182 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2183 struct sockaddr_in6 sin6;
2184 struct in6_addr *gw6;
2186 bzero(&sin6, sizeof(sin6));
2187 sin6.sin6_family = AF_INET6;
2188 sin6.sin6_len = sizeof(struct sockaddr_in6);
2189 bcopy(&reddst6, &sin6.sin6_addr, sizeof reddst6);
2190 rt = rtpurelookup((struct sockaddr *)&sin6);
2191 if (rt != NULL) {
2192 if (rt->rt_gateway == NULL ||
2193 rt->rt_gateway->sa_family != AF_INET6) {
2194 nd6log((LOG_ERR,
2195 "ICMP6 redirect rejected; no route "
2196 "with inet6 gateway found for redirect dst: %s\n",
2197 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2198 --rt->rt_refcnt;
2199 goto bad;
2202 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2203 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2204 nd6log((LOG_ERR,
2205 "ICMP6 redirect rejected; "
2206 "not equal to gw-for-src=%s (must be same): "
2207 "%s\n",
2208 ip6_sprintf(gw6),
2209 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2210 --rt->rt_refcnt;
2211 goto bad;
2213 } else {
2214 nd6log((LOG_ERR,
2215 "ICMP6 redirect rejected; "
2216 "no route found for redirect dst: %s\n",
2217 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2218 goto bad;
2220 --rt->rt_refcnt;
2221 rt = NULL;
2223 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2224 nd6log((LOG_ERR,
2225 "ICMP6 redirect rejected; "
2226 "redirect dst must be unicast: %s\n",
2227 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2228 goto bad;
2231 is_router = is_onlink = 0;
2232 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2233 is_router = 1; /* router case */
2234 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2235 is_onlink = 1; /* on-link destination case */
2236 if (!is_router && !is_onlink) {
2237 nd6log((LOG_ERR,
2238 "ICMP6 redirect rejected; "
2239 "neither router case nor onlink case: %s\n",
2240 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2241 goto bad;
2243 /* validation passed */
2245 icmp6len -= sizeof(*nd_rd);
2246 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2247 if (nd6_options(&ndopts) < 0) {
2248 nd6log((LOG_INFO, "icmp6_redirect_input: "
2249 "invalid ND option, rejected: %s\n",
2250 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2251 /* nd6_options have incremented stats */
2252 goto freeit;
2255 if (ndopts.nd_opts_tgt_lladdr) {
2256 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2257 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2260 if (ndopts.nd_opts_rh) {
2261 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2262 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2265 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2266 nd6log((LOG_INFO,
2267 "icmp6_redirect_input: lladdrlen mismatch for %s "
2268 "(if %d, icmp6 packet %d): %s\n",
2269 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2270 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2271 goto bad;
2274 /* RFC 2461 8.3 */
2275 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2276 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2278 if (!is_onlink) { /* better router case. perform rtredirect. */
2279 /* perform rtredirect */
2280 struct sockaddr_in6 sdst;
2281 struct sockaddr_in6 sgw;
2282 struct sockaddr_in6 ssrc;
2284 bzero(&sdst, sizeof(sdst));
2285 bzero(&sgw, sizeof(sgw));
2286 bzero(&ssrc, sizeof(ssrc));
2287 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2288 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2289 sizeof(struct sockaddr_in6);
2290 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2291 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2292 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2293 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2294 NULL, RTF_GATEWAY | RTF_HOST,
2295 (struct sockaddr *)&ssrc);
2297 /* finally update cached route in each socket via kpfctlinput */
2299 struct sockaddr_in6 sdst;
2301 bzero(&sdst, sizeof(sdst));
2302 sdst.sin6_family = AF_INET6;
2303 sdst.sin6_len = sizeof(struct sockaddr_in6);
2304 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2305 kpfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2306 #ifdef IPSEC
2307 key_sa_routechange((struct sockaddr *)&sdst);
2308 #endif
2311 freeit:
2312 m_freem(m);
2313 return;
2315 bad:
2316 icmp6stat.icp6s_badredirect++;
2317 m_freem(m);
2320 void
2321 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2323 struct ifnet *ifp; /* my outgoing interface */
2324 struct in6_addr *ifp_ll6;
2325 struct in6_addr *router_ll6;
2326 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2327 struct mbuf *m = NULL; /* newly allocated one */
2328 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2329 struct nd_redirect *nd_rd;
2330 size_t maxlen;
2331 u_char *p;
2332 struct ifnet *outif = NULL;
2333 struct sockaddr_in6 src_sa;
2335 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2337 /* if we are not router, we don't send icmp6 redirect */
2338 if (!ip6_forwarding || ip6_accept_rtadv)
2339 goto fail;
2341 /* sanity check */
2342 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2343 goto fail;
2346 * Address check:
2347 * the source address must identify a neighbor, and
2348 * the destination address must not be a multicast address
2349 * [RFC 2461, sec 8.2]
2351 sip6 = mtod(m0, struct ip6_hdr *);
2352 bzero(&src_sa, sizeof(src_sa));
2353 src_sa.sin6_family = AF_INET6;
2354 src_sa.sin6_len = sizeof(src_sa);
2355 src_sa.sin6_addr = sip6->ip6_src;
2356 /* we don't currently use sin6_scope_id, but eventually use it */
2357 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2358 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2359 goto fail;
2360 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2361 goto fail; /* what should we do here? */
2363 /* rate limit */
2364 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2365 goto fail;
2368 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2369 * we almost always ask for an mbuf cluster for simplicity.
2370 * (MHLEN < IPV6_MMTU is almost always true)
2372 #if IPV6_MMTU >= MCLBYTES
2373 # error assumption failed about IPV6_MMTU and MCLBYTES
2374 #endif
2375 m = m_getb(IPV6_MMTU, MB_DONTWAIT, MT_HEADER, M_PKTHDR);
2376 if (!m)
2377 goto fail;
2378 m->m_len = 0;
2379 maxlen = M_TRAILINGSPACE(m);
2380 maxlen = min(IPV6_MMTU, maxlen);
2381 /* just for safety */
2382 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2383 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2384 goto fail;
2388 /* get ip6 linklocal address for ifp(my outgoing interface). */
2389 struct in6_ifaddr *ia;
2390 if ((ia = in6ifa_ifpforlinklocal(ifp,
2391 IN6_IFF_NOTREADY|
2392 IN6_IFF_ANYCAST)) == NULL)
2393 goto fail;
2394 ifp_ll6 = &ia->ia_addr.sin6_addr;
2397 /* get ip6 linklocal address for the router. */
2398 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2399 struct sockaddr_in6 *sin6;
2400 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2401 router_ll6 = &sin6->sin6_addr;
2402 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2403 router_ll6 = NULL;
2404 } else
2405 router_ll6 = NULL;
2407 /* ip6 */
2408 ip6 = mtod(m, struct ip6_hdr *);
2409 ip6->ip6_flow = 0;
2410 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2411 ip6->ip6_vfc |= IPV6_VERSION;
2412 /* ip6->ip6_plen will be set later */
2413 ip6->ip6_nxt = IPPROTO_ICMPV6;
2414 ip6->ip6_hlim = 255;
2415 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2416 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2417 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2419 /* ND Redirect */
2420 nd_rd = (struct nd_redirect *)(ip6 + 1);
2421 nd_rd->nd_rd_type = ND_REDIRECT;
2422 nd_rd->nd_rd_code = 0;
2423 nd_rd->nd_rd_reserved = 0;
2424 if (rt->rt_flags & RTF_GATEWAY) {
2426 * nd_rd->nd_rd_target must be a link-local address in
2427 * better router cases.
2429 if (!router_ll6)
2430 goto fail;
2431 bcopy(router_ll6, &nd_rd->nd_rd_target,
2432 sizeof(nd_rd->nd_rd_target));
2433 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2434 sizeof(nd_rd->nd_rd_dst));
2435 } else {
2436 /* make sure redtgt == reddst */
2437 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2438 sizeof(nd_rd->nd_rd_target));
2439 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2440 sizeof(nd_rd->nd_rd_dst));
2443 p = (u_char *)(nd_rd + 1);
2445 if (!router_ll6)
2446 goto nolladdropt;
2449 /* target lladdr option */
2450 struct rtentry *rt_router = NULL;
2451 int len;
2452 struct sockaddr_dl *sdl;
2453 struct nd_opt_hdr *nd_opt;
2454 char *lladdr;
2456 rt_router = nd6_lookup(router_ll6, 0, ifp);
2457 if (!rt_router)
2458 goto nolladdropt;
2459 len = sizeof(*nd_opt) + ifp->if_addrlen;
2460 len = (len + 7) & ~7; /* round by 8 */
2461 /* safety check */
2462 if (len + (p - (u_char *)ip6) > maxlen)
2463 goto nolladdropt;
2464 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2465 (rt_router->rt_flags & RTF_LLINFO) &&
2466 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2467 (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2468 sdl->sdl_alen) {
2469 nd_opt = (struct nd_opt_hdr *)p;
2470 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2471 nd_opt->nd_opt_len = len >> 3;
2472 lladdr = (char *)(nd_opt + 1);
2473 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2474 p += len;
2477 nolladdropt:;
2479 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2481 /* just to be safe */
2482 #ifdef M_DECRYPTED /*not openbsd*/
2483 if (m0->m_flags & M_DECRYPTED)
2484 goto noredhdropt;
2485 #endif
2486 if (p - (u_char *)ip6 > maxlen)
2487 goto noredhdropt;
2490 /* redirected header option */
2491 int len;
2492 struct nd_opt_rd_hdr *nd_opt_rh;
2495 * compute the maximum size for icmp6 redirect header option.
2496 * XXX room for auth header?
2498 len = maxlen - (p - (u_char *)ip6);
2499 len &= ~7;
2501 /* This is just for simplicity. */
2502 if (m0->m_pkthdr.len != m0->m_len) {
2503 if (m0->m_next) {
2504 m_freem(m0->m_next);
2505 m0->m_next = NULL;
2507 m0->m_pkthdr.len = m0->m_len;
2511 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2512 * about padding/truncate rule for the original IP packet.
2513 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2514 * - "attach as much as possible" is the goal
2515 * - pad if not aligned (original size can be guessed by original
2516 * ip6 header)
2517 * Following code adds the padding if it is simple enough,
2518 * and truncates if not.
2520 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2521 panic("assumption failed in %s:%d", __FILE__, __LINE__);
2523 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2524 /* not enough room, truncate */
2525 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2526 } else {
2527 /* enough room, pad or truncate */
2528 size_t extra;
2530 extra = m0->m_pkthdr.len % 8;
2531 if (extra) {
2532 /* pad if easy enough, truncate if not */
2533 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2534 /* pad */
2535 m0->m_len += (8 - extra);
2536 m0->m_pkthdr.len += (8 - extra);
2537 } else {
2538 /* truncate */
2539 m0->m_pkthdr.len -= extra;
2540 m0->m_len -= extra;
2543 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2544 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2547 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2548 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2549 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2550 nd_opt_rh->nd_opt_rh_len = len >> 3;
2551 p += sizeof(*nd_opt_rh);
2552 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2554 /* connect m0 to m */
2555 m->m_next = m0;
2556 m->m_pkthdr.len = m->m_len + m0->m_len;
2558 noredhdropt:;
2560 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2561 sip6->ip6_src.s6_addr16[1] = 0;
2562 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2563 sip6->ip6_dst.s6_addr16[1] = 0;
2564 #if 0
2565 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2566 ip6->ip6_src.s6_addr16[1] = 0;
2567 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2568 ip6->ip6_dst.s6_addr16[1] = 0;
2569 #endif
2570 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2571 nd_rd->nd_rd_target.s6_addr16[1] = 0;
2572 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2573 nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2575 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2577 nd_rd->nd_rd_cksum = 0;
2578 nd_rd->nd_rd_cksum
2579 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2581 /* send the packet to outside... */
2582 ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2583 if (outif) {
2584 icmp6_ifstat_inc(outif, ifs6_out_msg);
2585 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2587 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2589 return;
2591 fail:
2592 if (m)
2593 m_freem(m);
2594 if (m0)
2595 m_freem(m0);
2598 #ifdef HAVE_NRL_INPCB
2599 #define in6pcb inpcb
2600 #define in6p_icmp6filt inp_icmp6filt
2601 #endif
2603 * ICMPv6 socket option processing.
2606 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2608 int error = 0;
2609 int optlen;
2610 struct inpcb *inp = so->so_pcb;
2611 int level, op, optname;
2613 if (sopt) {
2614 level = sopt->sopt_level;
2615 op = sopt->sopt_dir;
2616 optname = sopt->sopt_name;
2617 optlen = sopt->sopt_valsize;
2618 } else
2619 level = op = optname = optlen = 0;
2621 if (level != IPPROTO_ICMPV6) {
2622 return EINVAL;
2625 switch (op) {
2626 case PRCO_SETOPT:
2627 switch (optname) {
2628 case ICMP6_FILTER:
2630 struct icmp6_filter *p;
2632 if (optlen != sizeof(*p)) {
2633 error = EMSGSIZE;
2634 break;
2636 if (inp->in6p_icmp6filt == NULL) {
2637 error = EINVAL;
2638 break;
2640 error = soopt_to_kbuf(sopt, inp->in6p_icmp6filt, optlen,
2641 optlen);
2642 break;
2645 default:
2646 error = ENOPROTOOPT;
2647 break;
2649 break;
2651 case PRCO_GETOPT:
2652 switch (optname) {
2653 case ICMP6_FILTER:
2655 if (inp->in6p_icmp6filt == NULL) {
2656 error = EINVAL;
2657 break;
2659 soopt_from_kbuf(sopt, inp->in6p_icmp6filt,
2660 sizeof(struct icmp6_filter));
2661 break;
2664 default:
2665 error = ENOPROTOOPT;
2666 break;
2668 break;
2671 return (error);
2673 #ifdef HAVE_NRL_INPCB
2674 #undef in6pcb
2675 #undef in6p_icmp6filt
2676 #endif
2678 #ifndef HAVE_PPSRATECHECK
2679 #ifndef timersub
2680 #define timersub(tvp, uvp, vvp) \
2681 do { \
2682 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2683 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2684 if ((vvp)->tv_usec < 0) { \
2685 (vvp)->tv_sec--; \
2686 (vvp)->tv_usec += 1000000; \
2688 } while (0)
2689 #endif
2692 * ppsratecheck(): packets (or events) per second limitation.
2694 static int
2695 ppsratecheck(struct timeval *lasttime, int *curpps,
2696 int maxpps) /* maximum pps allowed */
2698 struct timeval tv, delta;
2699 int rv;
2701 microtime(&tv);
2703 timersub(&tv, lasttime, &delta);
2706 * Check for 0,0 so that the message will be seen at least once.
2707 * If more than one second has passed since the last update of
2708 * lasttime, reset the counter.
2710 * We do increment *curpps even in *curpps < maxpps case, as some may
2711 * try to use *curpps for stat purposes as well.
2713 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
2714 delta.tv_sec >= 1) {
2715 *lasttime = tv;
2716 *curpps = 0;
2717 rv = 1;
2718 } else if (maxpps < 0)
2719 rv = 1;
2720 else if (*curpps < maxpps)
2721 rv = 1;
2722 else
2723 rv = 0;
2725 #if 1 /* DIAGNOSTIC? */
2726 /* be careful about wrap-around */
2727 if (*curpps + 1 > *curpps)
2728 *curpps = *curpps + 1;
2729 #else
2731 * assume that there's not too many calls to this function.
2732 * not sure if the assumption holds, as it depends on *caller's*
2733 * behavior, not the behavior of this function.
2734 * IMHO it is wrong to make assumption on the caller's behavior,
2735 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
2737 *curpps = *curpps + 1;
2738 #endif
2740 return (rv);
2742 #endif
2745 * Perform rate limit check.
2746 * Returns 0 if it is okay to send the icmp6 packet.
2747 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2748 * limitation.
2750 * XXX per-destination/type check necessary?
2752 static int
2753 icmp6_ratelimit(const struct in6_addr *dst, /* not used at this moment */
2754 const int type, /* not used at this moment */
2755 const int code) /* not used at this moment */
2757 int ret;
2759 ret = 0; /* okay to send */
2761 /* PPS limit */
2762 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2763 icmp6errppslim)) {
2764 /* The packet is subject to rate limit */
2765 ret++;
2768 return ret;