icmp6.c revision 1.132 1 /* $NetBSD: icmp6.c,v 1.132 2007/05/02 20:40:25 dyoung Exp $ */
2 /* $KAME: icmp6.c,v 1.217 2001/06/20 15:03:29 jinmei Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: icmp6.c,v 1.132 2007/05/02 20:40:25 dyoung Exp $");
66
67 #include "opt_inet.h"
68 #include "opt_ipsec.h"
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/time.h>
78 #include <sys/kernel.h>
79 #include <sys/syslog.h>
80 #include <sys/domain.h>
81 #include <sys/sysctl.h>
82
83 #include <net/if.h>
84 #include <net/route.h>
85 #include <net/if_dl.h>
86 #include <net/if_types.h>
87
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/mld6_var.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/nd6.h>
96 #include <netinet6/in6_ifattach.h>
97 #include <netinet6/ip6protosw.h>
98 #include <netinet6/scope6_var.h>
99
100 #ifdef IPSEC
101 #include <netinet6/ipsec.h>
102 #include <netkey/key.h>
103 #endif
104
105 #ifdef FAST_IPSEC
106 #include <netipsec/ipsec.h>
107 #include <netipsec/key.h>
108 #endif
109
110
111 #include "faith.h"
112 #if defined(NFAITH) && 0 < NFAITH
113 #include <net/if_faith.h>
114 #endif
115
116 #include <net/net_osdep.h>
117
118 extern struct domain inet6domain;
119
120 struct icmp6stat icmp6stat;
121
122 extern struct inpcbtable raw6cbtable;
123 extern int icmp6errppslim;
124 static int icmp6errpps_count = 0;
125 static struct timeval icmp6errppslim_last;
126 extern int icmp6_nodeinfo;
127
128 /*
129 * List of callbacks to notify when Path MTU changes are made.
130 */
131 struct icmp6_mtudisc_callback {
132 LIST_ENTRY(icmp6_mtudisc_callback) mc_list;
133 void (*mc_func) __P((struct in6_addr *));
134 };
135
136 LIST_HEAD(, icmp6_mtudisc_callback) icmp6_mtudisc_callbacks =
137 LIST_HEAD_INITIALIZER(&icmp6_mtudisc_callbacks);
138
139 static struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL;
140 extern int pmtu_expire;
141
142 /* XXX do these values make any sense? */
143 static int icmp6_mtudisc_hiwat = 1280;
144 static int icmp6_mtudisc_lowat = 256;
145
146 /*
147 * keep track of # of redirect routes.
148 */
149 static struct rttimer_queue *icmp6_redirect_timeout_q = NULL;
150
151 /* XXX experimental, turned off */
152 static int icmp6_redirect_hiwat = -1;
153 static int icmp6_redirect_lowat = -1;
154
155 static void icmp6_errcount __P((struct icmp6errstat *, int, int));
156 static int icmp6_rip6_input __P((struct mbuf **, int));
157 static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
158 static const char *icmp6_redirect_diag __P((struct in6_addr *,
159 struct in6_addr *, struct in6_addr *));
160 static struct mbuf *ni6_input __P((struct mbuf *, int));
161 static struct mbuf *ni6_nametodns __P((const char *, int, int));
162 static int ni6_dnsmatch __P((const char *, int, const char *, int));
163 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
164 struct ifnet **, char *));
165 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
166 struct ifnet *, int));
167 static int icmp6_notify_error __P((struct mbuf *, int, int, int));
168 static struct rtentry *icmp6_mtudisc_clone __P((struct sockaddr *));
169 static void icmp6_mtudisc_timeout __P((struct rtentry *, struct rttimer *));
170 static void icmp6_redirect_timeout __P((struct rtentry *, struct rttimer *));
171
172
173 void
174 icmp6_init()
175 {
176 mld_init();
177 icmp6_mtudisc_timeout_q = rt_timer_queue_create(pmtu_expire);
178 icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout);
179 }
180
181 static void
182 icmp6_errcount(stat, type, code)
183 struct icmp6errstat *stat;
184 int type, code;
185 {
186 switch (type) {
187 case ICMP6_DST_UNREACH:
188 switch (code) {
189 case ICMP6_DST_UNREACH_NOROUTE:
190 stat->icp6errs_dst_unreach_noroute++;
191 return;
192 case ICMP6_DST_UNREACH_ADMIN:
193 stat->icp6errs_dst_unreach_admin++;
194 return;
195 case ICMP6_DST_UNREACH_BEYONDSCOPE:
196 stat->icp6errs_dst_unreach_beyondscope++;
197 return;
198 case ICMP6_DST_UNREACH_ADDR:
199 stat->icp6errs_dst_unreach_addr++;
200 return;
201 case ICMP6_DST_UNREACH_NOPORT:
202 stat->icp6errs_dst_unreach_noport++;
203 return;
204 }
205 break;
206 case ICMP6_PACKET_TOO_BIG:
207 stat->icp6errs_packet_too_big++;
208 return;
209 case ICMP6_TIME_EXCEEDED:
210 switch (code) {
211 case ICMP6_TIME_EXCEED_TRANSIT:
212 stat->icp6errs_time_exceed_transit++;
213 return;
214 case ICMP6_TIME_EXCEED_REASSEMBLY:
215 stat->icp6errs_time_exceed_reassembly++;
216 return;
217 }
218 break;
219 case ICMP6_PARAM_PROB:
220 switch (code) {
221 case ICMP6_PARAMPROB_HEADER:
222 stat->icp6errs_paramprob_header++;
223 return;
224 case ICMP6_PARAMPROB_NEXTHEADER:
225 stat->icp6errs_paramprob_nextheader++;
226 return;
227 case ICMP6_PARAMPROB_OPTION:
228 stat->icp6errs_paramprob_option++;
229 return;
230 }
231 break;
232 case ND_REDIRECT:
233 stat->icp6errs_redirect++;
234 return;
235 }
236 stat->icp6errs_unknown++;
237 }
238
239 /*
240 * Register a Path MTU Discovery callback.
241 */
242 void
243 icmp6_mtudisc_callback_register(func)
244 void (*func) __P((struct in6_addr *));
245 {
246 struct icmp6_mtudisc_callback *mc;
247
248 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
249 mc = LIST_NEXT(mc, mc_list)) {
250 if (mc->mc_func == func)
251 return;
252 }
253
254 mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
255 if (mc == NULL)
256 panic("icmp6_mtudisc_callback_register");
257
258 mc->mc_func = func;
259 LIST_INSERT_HEAD(&icmp6_mtudisc_callbacks, mc, mc_list);
260 }
261
262 /*
263 * A wrapper function for icmp6_error() necessary when the erroneous packet
264 * may not contain enough scope zone information.
265 */
266 void
267 icmp6_error2(m, type, code, param, ifp)
268 struct mbuf *m;
269 int type, code, param;
270 struct ifnet *ifp;
271 {
272 struct ip6_hdr *ip6;
273
274 if (ifp == NULL)
275 return;
276
277 if (m->m_len < sizeof(struct ip6_hdr)) {
278 m = m_pullup(m, sizeof(struct ip6_hdr));
279 if (m == NULL)
280 return;
281 }
282
283 ip6 = mtod(m, struct ip6_hdr *);
284
285 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
286 return;
287 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
288 return;
289
290 icmp6_error(m, type, code, param);
291 }
292
293 /*
294 * Generate an error packet of type error in response to bad IP6 packet.
295 */
296 void
297 icmp6_error(m, type, code, param)
298 struct mbuf *m;
299 int type, code, param;
300 {
301 struct ip6_hdr *oip6, *nip6;
302 struct icmp6_hdr *icmp6;
303 u_int preplen;
304 int off;
305 int nxt;
306
307 icmp6stat.icp6s_error++;
308
309 /* count per-type-code statistics */
310 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
311
312 if (m->m_flags & M_DECRYPTED) {
313 icmp6stat.icp6s_canterror++;
314 goto freeit;
315 }
316
317 if (m->m_len < sizeof(struct ip6_hdr)) {
318 m = m_pullup(m, sizeof(struct ip6_hdr));
319 if (m == NULL)
320 return;
321 }
322 oip6 = mtod(m, struct ip6_hdr *);
323
324 /*
325 * If the destination address of the erroneous packet is a multicast
326 * address, or the packet was sent using link-layer multicast,
327 * we should basically suppress sending an error (RFC 2463, Section
328 * 2.4).
329 * We have two exceptions (the item e.2 in that section):
330 * - the Pakcet Too Big message can be sent for path MTU discovery.
331 * - the Parameter Problem Message that can be allowed an icmp6 error
332 * in the option type field. This check has been done in
333 * ip6_unknown_opt(), so we can just check the type and code.
334 */
335 if ((m->m_flags & (M_BCAST|M_MCAST) ||
336 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
337 (type != ICMP6_PACKET_TOO_BIG &&
338 (type != ICMP6_PARAM_PROB ||
339 code != ICMP6_PARAMPROB_OPTION)))
340 goto freeit;
341
342 /*
343 * RFC 2463, 2.4 (e.5): source address check.
344 * XXX: the case of anycast source?
345 */
346 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
347 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
348 goto freeit;
349
350 /*
351 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
352 * don't do it.
353 */
354 nxt = -1;
355 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
356 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
357 struct icmp6_hdr *icp;
358
359 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
360 sizeof(*icp));
361 if (icp == NULL) {
362 icmp6stat.icp6s_tooshort++;
363 return;
364 }
365 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
366 icp->icmp6_type == ND_REDIRECT) {
367 /*
368 * ICMPv6 error
369 * Special case: for redirect (which is
370 * informational) we must not send icmp6 error.
371 */
372 icmp6stat.icp6s_canterror++;
373 goto freeit;
374 } else {
375 /* ICMPv6 informational - send the error */
376 }
377 }
378 #if 0 /* controversial */
379 else if (off >= 0 && nxt == IPPROTO_ESP) {
380 /*
381 * It could be ICMPv6 error inside ESP. Take a safer side,
382 * don't respond.
383 */
384 icmp6stat.icp6s_canterror++;
385 goto freeit;
386 }
387 #endif
388 else {
389 /* non-ICMPv6 - send the error */
390 }
391
392 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
393
394 /* Finally, do rate limitation check. */
395 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
396 icmp6stat.icp6s_toofreq++;
397 goto freeit;
398 }
399
400 /*
401 * OK, ICMP6 can be generated.
402 */
403
404 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
405 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
406
407 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
408 M_PREPEND(m, preplen, M_DONTWAIT);
409 if (m && m->m_len < preplen)
410 m = m_pullup(m, preplen);
411 if (m == NULL) {
412 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
413 return;
414 }
415
416 nip6 = mtod(m, struct ip6_hdr *);
417 nip6->ip6_src = oip6->ip6_src;
418 nip6->ip6_dst = oip6->ip6_dst;
419
420 in6_clearscope(&oip6->ip6_src);
421 in6_clearscope(&oip6->ip6_dst);
422
423 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
424 icmp6->icmp6_type = type;
425 icmp6->icmp6_code = code;
426 icmp6->icmp6_pptr = htonl((u_int32_t)param);
427
428 /*
429 * icmp6_reflect() is designed to be in the input path.
430 * icmp6_error() can be called from both input and output path,
431 * and if we are in output path rcvif could contain bogus value.
432 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
433 * information in ip header (nip6).
434 */
435 m->m_pkthdr.rcvif = NULL;
436
437 icmp6stat.icp6s_outhist[type]++;
438 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
439
440 return;
441
442 freeit:
443 /*
444 * If we can't tell whether or not we can generate ICMP6, free it.
445 */
446 m_freem(m);
447 }
448
449 /*
450 * Process a received ICMP6 message.
451 */
452 int
453 icmp6_input(struct mbuf **mp, int *offp, int proto)
454 {
455 struct mbuf *m = *mp, *n;
456 struct ip6_hdr *ip6, *nip6;
457 struct icmp6_hdr *icmp6, *nicmp6;
458 int off = *offp;
459 int icmp6len = m->m_pkthdr.len - *offp;
460 int code, sum, noff;
461
462 #define ICMP6_MAXLEN (sizeof(*nip6) + sizeof(*nicmp6) + 4)
463 KASSERT(ICMP6_MAXLEN < MCLBYTES);
464 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
465
466 /*
467 * Locate icmp6 structure in mbuf, and check
468 * that not corrupted and of at least minimum length
469 */
470
471 ip6 = mtod(m, struct ip6_hdr *);
472 if (icmp6len < sizeof(struct icmp6_hdr)) {
473 icmp6stat.icp6s_tooshort++;
474 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
475 goto freeit;
476 }
477
478 /*
479 * calculate the checksum
480 */
481 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
482 if (icmp6 == NULL) {
483 icmp6stat.icp6s_tooshort++;
484 /* m is invalid */
485 /*icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);*/
486 return IPPROTO_DONE;
487 }
488 KASSERT(IP6_HDR_ALIGNED_P(icmp6));
489 code = icmp6->icmp6_code;
490
491 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
492 nd6log((LOG_ERR,
493 "ICMP6 checksum error(%d|%x) %s\n",
494 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
495 icmp6stat.icp6s_checksum++;
496 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
497 goto freeit;
498 }
499
500 #if defined(NFAITH) && 0 < NFAITH
501 if (faithprefix(&ip6->ip6_dst)) {
502 /*
503 * Deliver very specific ICMP6 type only.
504 * This is important to deliver TOOBIG. Otherwise PMTUD
505 * will not work.
506 */
507 switch (icmp6->icmp6_type) {
508 case ICMP6_DST_UNREACH:
509 case ICMP6_PACKET_TOO_BIG:
510 case ICMP6_TIME_EXCEEDED:
511 break;
512 default:
513 goto freeit;
514 }
515 }
516 #endif
517
518 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
519
520 switch (icmp6->icmp6_type) {
521 case ICMP6_DST_UNREACH:
522 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
523 switch (code) {
524 case ICMP6_DST_UNREACH_NOROUTE:
525 code = PRC_UNREACH_NET;
526 break;
527 case ICMP6_DST_UNREACH_ADMIN:
528 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
529 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
530 break;
531 case ICMP6_DST_UNREACH_ADDR:
532 code = PRC_HOSTDEAD;
533 break;
534 #ifdef COMPAT_RFC1885
535 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
536 code = PRC_UNREACH_SRCFAIL;
537 break;
538 #else
539 case ICMP6_DST_UNREACH_BEYONDSCOPE:
540 /* I mean "source address was incorrect." */
541 code = PRC_UNREACH_NET;
542 break;
543 #endif
544 case ICMP6_DST_UNREACH_NOPORT:
545 code = PRC_UNREACH_PORT;
546 break;
547 default:
548 goto badcode;
549 }
550 goto deliver;
551
552 case ICMP6_PACKET_TOO_BIG:
553 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
554
555 code = PRC_MSGSIZE;
556
557 /*
558 * Updating the path MTU will be done after examining
559 * intermediate extension headers.
560 */
561 goto deliver;
562
563 case ICMP6_TIME_EXCEEDED:
564 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
565 switch (code) {
566 case ICMP6_TIME_EXCEED_TRANSIT:
567 code = PRC_TIMXCEED_INTRANS;
568 break;
569 case ICMP6_TIME_EXCEED_REASSEMBLY:
570 code = PRC_TIMXCEED_REASS;
571 break;
572 default:
573 goto badcode;
574 }
575 goto deliver;
576
577 case ICMP6_PARAM_PROB:
578 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
579 switch (code) {
580 case ICMP6_PARAMPROB_NEXTHEADER:
581 code = PRC_UNREACH_PROTOCOL;
582 break;
583 case ICMP6_PARAMPROB_HEADER:
584 case ICMP6_PARAMPROB_OPTION:
585 code = PRC_PARAMPROB;
586 break;
587 default:
588 goto badcode;
589 }
590 goto deliver;
591
592 case ICMP6_ECHO_REQUEST:
593 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
594 if (code != 0)
595 goto badcode;
596 /*
597 * Copy mbuf to send to two data paths: userland socket(s),
598 * and to the querier (echo reply).
599 * m: a copy for socket, n: a copy for querier
600 *
601 * If the first mbuf is shared, or the first mbuf is too short,
602 * copy the first part of the data into a fresh mbuf.
603 * Otherwise, we will wrongly overwrite both copies.
604 */
605 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
606 /* Give up local */
607 n = m;
608 m = NULL;
609 } else if (M_READONLY(n) ||
610 n->m_len < off + sizeof(struct icmp6_hdr)) {
611 struct mbuf *n0 = n;
612
613 /*
614 * Prepare an internal mbuf. m_pullup() doesn't
615 * always copy the length we specified.
616 */
617 if ((n = m_dup(n0, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
618 /* Give up local */
619 n = m;
620 m = NULL;
621 }
622 m_freem(n0);
623 }
624 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
625 sizeof(*nicmp6));
626 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
627 nicmp6->icmp6_code = 0;
628 if (n) {
629 icmp6stat.icp6s_reflect++;
630 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
631 icmp6_reflect(n, off);
632 }
633 if (!m)
634 goto freeit;
635 break;
636
637 case ICMP6_ECHO_REPLY:
638 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
639 if (code != 0)
640 goto badcode;
641 break;
642
643 case MLD_LISTENER_QUERY:
644 case MLD_LISTENER_REPORT:
645 if (icmp6len < sizeof(struct mld_hdr))
646 goto badlen;
647 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
648 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
649 else
650 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
651 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
652 /* give up local */
653 mld_input(m, off);
654 m = NULL;
655 goto freeit;
656 }
657 mld_input(n, off);
658 /* m stays. */
659 break;
660
661 case MLD_LISTENER_DONE:
662 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
663 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
664 goto badlen;
665 break; /* nothing to be done in kernel */
666
667 case MLD_MTRACE_RESP:
668 case MLD_MTRACE:
669 /* XXX: these two are experimental. not officially defined. */
670 /* XXX: per-interface statistics? */
671 break; /* just pass it to applications */
672
673 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
674 {
675 enum { WRU, FQDN } mode;
676
677 if (!icmp6_nodeinfo)
678 break;
679
680 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
681 mode = WRU;
682 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
683 mode = FQDN;
684 else
685 goto badlen;
686
687 if (mode == FQDN) {
688 n = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
689 if (n)
690 n = ni6_input(n, off);
691 /* XXX meaningless if n == NULL */
692 noff = sizeof(struct ip6_hdr);
693 } else {
694 u_char *p;
695 int maxhlen;
696
697 if ((icmp6_nodeinfo & 5) != 5)
698 break;
699
700 if (code != 0)
701 goto badcode;
702 MGETHDR(n, M_DONTWAIT, m->m_type);
703 if (n && ICMP6_MAXLEN > MHLEN) {
704 MCLGET(n, M_DONTWAIT);
705 if ((n->m_flags & M_EXT) == 0) {
706 m_free(n);
707 n = NULL;
708 }
709 }
710 if (n == NULL) {
711 /* Give up remote */
712 break;
713 }
714 n->m_pkthdr.rcvif = NULL;
715 n->m_len = 0;
716 maxhlen = M_TRAILINGSPACE(n) - ICMP6_MAXLEN;
717 if (maxhlen > hostnamelen)
718 maxhlen = hostnamelen;
719 /*
720 * Copy IPv6 and ICMPv6 only.
721 */
722 nip6 = mtod(n, struct ip6_hdr *);
723 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
724 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
725 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
726 p = (u_char *)(nicmp6 + 1);
727 bzero(p, 4);
728 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
729 noff = sizeof(struct ip6_hdr);
730 M_COPY_PKTHDR(n, m); /* just for rcvif */
731 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
732 sizeof(struct icmp6_hdr) + 4 + maxhlen;
733 nicmp6->icmp6_type = ICMP6_WRUREPLY;
734 nicmp6->icmp6_code = 0;
735 }
736 #undef hostnamelen
737 if (n) {
738 icmp6stat.icp6s_reflect++;
739 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
740 icmp6_reflect(n, noff);
741 }
742 break;
743 }
744
745 case ICMP6_WRUREPLY:
746 if (code != 0)
747 goto badcode;
748 break;
749
750 case ND_ROUTER_SOLICIT:
751 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
752 if (code != 0)
753 goto badcode;
754 if (icmp6len < sizeof(struct nd_router_solicit))
755 goto badlen;
756 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
757 /* give up local */
758 nd6_rs_input(m, off, icmp6len);
759 m = NULL;
760 goto freeit;
761 }
762 nd6_rs_input(n, off, icmp6len);
763 /* m stays. */
764 break;
765
766 case ND_ROUTER_ADVERT:
767 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
768 if (code != 0)
769 goto badcode;
770 if (icmp6len < sizeof(struct nd_router_advert))
771 goto badlen;
772 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
773 /* give up local */
774 nd6_ra_input(m, off, icmp6len);
775 m = NULL;
776 goto freeit;
777 }
778 nd6_ra_input(n, off, icmp6len);
779 /* m stays. */
780 break;
781
782 case ND_NEIGHBOR_SOLICIT:
783 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
784 if (code != 0)
785 goto badcode;
786 if (icmp6len < sizeof(struct nd_neighbor_solicit))
787 goto badlen;
788 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
789 /* give up local */
790 nd6_ns_input(m, off, icmp6len);
791 m = NULL;
792 goto freeit;
793 }
794 nd6_ns_input(n, off, icmp6len);
795 /* m stays. */
796 break;
797
798 case ND_NEIGHBOR_ADVERT:
799 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
800 if (code != 0)
801 goto badcode;
802 if (icmp6len < sizeof(struct nd_neighbor_advert))
803 goto badlen;
804 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
805 /* give up local */
806 nd6_na_input(m, off, icmp6len);
807 m = NULL;
808 goto freeit;
809 }
810 nd6_na_input(n, off, icmp6len);
811 /* m stays. */
812 break;
813
814 case ND_REDIRECT:
815 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
816 if (code != 0)
817 goto badcode;
818 if (icmp6len < sizeof(struct nd_redirect))
819 goto badlen;
820 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
821 /* give up local */
822 icmp6_redirect_input(m, off);
823 m = NULL;
824 goto freeit;
825 }
826 icmp6_redirect_input(n, off);
827 /* m stays. */
828 break;
829
830 case ICMP6_ROUTER_RENUMBERING:
831 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
832 code != ICMP6_ROUTER_RENUMBERING_RESULT)
833 goto badcode;
834 if (icmp6len < sizeof(struct icmp6_router_renum))
835 goto badlen;
836 break;
837
838 default:
839 nd6log((LOG_DEBUG,
840 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
841 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
842 ip6_sprintf(&ip6->ip6_dst),
843 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
844 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
845 /* ICMPv6 error: MUST deliver it by spec... */
846 code = PRC_NCMDS;
847 /* deliver */
848 } else {
849 /* ICMPv6 informational: MUST not deliver */
850 break;
851 }
852 deliver:
853 if (icmp6_notify_error(m, off, icmp6len, code)) {
854 /* In this case, m should've been freed. */
855 return (IPPROTO_DONE);
856 }
857 break;
858
859 badcode:
860 icmp6stat.icp6s_badcode++;
861 break;
862
863 badlen:
864 icmp6stat.icp6s_badlen++;
865 break;
866 }
867
868 /* deliver the packet to appropriate sockets */
869 icmp6_rip6_input(&m, *offp);
870
871 return IPPROTO_DONE;
872
873 freeit:
874 m_freem(m);
875 return IPPROTO_DONE;
876 }
877
878 static int
879 icmp6_notify_error(m, off, icmp6len, code)
880 struct mbuf *m;
881 int off, icmp6len, code;
882 {
883 struct icmp6_hdr *icmp6;
884 struct ip6_hdr *eip6;
885 u_int32_t notifymtu;
886 struct sockaddr_in6 icmp6src, icmp6dst;
887
888 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
889 icmp6stat.icp6s_tooshort++;
890 goto freeit;
891 }
892 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
893 sizeof(*icmp6) + sizeof(struct ip6_hdr));
894 if (icmp6 == NULL) {
895 icmp6stat.icp6s_tooshort++;
896 return (-1);
897 }
898 eip6 = (struct ip6_hdr *)(icmp6 + 1);
899
900 /* Detect the upper level protocol */
901 {
902 void (*ctlfunc) __P((int, struct sockaddr *, void *));
903 u_int8_t nxt = eip6->ip6_nxt;
904 int eoff = off + sizeof(struct icmp6_hdr) +
905 sizeof(struct ip6_hdr);
906 struct ip6ctlparam ip6cp;
907 struct in6_addr *finaldst = NULL;
908 int icmp6type = icmp6->icmp6_type;
909 struct ip6_frag *fh;
910 struct ip6_rthdr *rth;
911 struct ip6_rthdr0 *rth0;
912 int rthlen;
913
914 while (1) { /* XXX: should avoid infinite loop explicitly? */
915 struct ip6_ext *eh;
916
917 switch (nxt) {
918 case IPPROTO_HOPOPTS:
919 case IPPROTO_DSTOPTS:
920 case IPPROTO_AH:
921 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
922 eoff, sizeof(*eh));
923 if (eh == NULL) {
924 icmp6stat.icp6s_tooshort++;
925 return (-1);
926 }
927
928 if (nxt == IPPROTO_AH)
929 eoff += (eh->ip6e_len + 2) << 2;
930 else
931 eoff += (eh->ip6e_len + 1) << 3;
932 nxt = eh->ip6e_nxt;
933 break;
934 case IPPROTO_ROUTING:
935 /*
936 * When the erroneous packet contains a
937 * routing header, we should examine the
938 * header to determine the final destination.
939 * Otherwise, we can't properly update
940 * information that depends on the final
941 * destination (e.g. path MTU).
942 */
943 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
944 eoff, sizeof(*rth));
945 if (rth == NULL) {
946 icmp6stat.icp6s_tooshort++;
947 return (-1);
948 }
949 rthlen = (rth->ip6r_len + 1) << 3;
950 /*
951 * XXX: currently there is no
952 * officially defined type other
953 * than type-0.
954 * Note that if the segment left field
955 * is 0, all intermediate hops must
956 * have been passed.
957 */
958 if (rth->ip6r_segleft &&
959 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
960 int hops;
961
962 IP6_EXTHDR_GET(rth0,
963 struct ip6_rthdr0 *, m,
964 eoff, rthlen);
965 if (rth0 == NULL) {
966 icmp6stat.icp6s_tooshort++;
967 return (-1);
968 }
969 /* just ignore a bogus header */
970 if ((rth0->ip6r0_len % 2) == 0 &&
971 (hops = rth0->ip6r0_len/2))
972 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
973 }
974 eoff += rthlen;
975 nxt = rth->ip6r_nxt;
976 break;
977 case IPPROTO_FRAGMENT:
978 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
979 eoff, sizeof(*fh));
980 if (fh == NULL) {
981 icmp6stat.icp6s_tooshort++;
982 return (-1);
983 }
984 /*
985 * Data after a fragment header is meaningless
986 * unless it is the first fragment, but
987 * we'll go to the notify label for path MTU
988 * discovery.
989 */
990 if (fh->ip6f_offlg & IP6F_OFF_MASK)
991 goto notify;
992
993 eoff += sizeof(struct ip6_frag);
994 nxt = fh->ip6f_nxt;
995 break;
996 default:
997 /*
998 * This case includes ESP and the No Next
999 * Header. In such cases going to the notify
1000 * label does not have any meaning
1001 * (i.e. ctlfunc will be NULL), but we go
1002 * anyway since we might have to update
1003 * path MTU information.
1004 */
1005 goto notify;
1006 }
1007 }
1008 notify:
1009 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1010 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1011 if (icmp6 == NULL) {
1012 icmp6stat.icp6s_tooshort++;
1013 return (-1);
1014 }
1015
1016 /*
1017 * retrieve parameters from the inner IPv6 header, and convert
1018 * them into sockaddr structures.
1019 * XXX: there is no guarantee that the source or destination
1020 * addresses of the inner packet are in the same scope zone as
1021 * the addresses of the icmp packet. But there is no other
1022 * way to determine the zone.
1023 */
1024 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1025
1026 bzero(&icmp6dst, sizeof(icmp6dst));
1027 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1028 icmp6dst.sin6_family = AF_INET6;
1029 if (finaldst == NULL)
1030 icmp6dst.sin6_addr = eip6->ip6_dst;
1031 else
1032 icmp6dst.sin6_addr = *finaldst;
1033 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1034 goto freeit;
1035 bzero(&icmp6src, sizeof(icmp6src));
1036 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1037 icmp6src.sin6_family = AF_INET6;
1038 icmp6src.sin6_addr = eip6->ip6_src;
1039 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1040 goto freeit;
1041 icmp6src.sin6_flowinfo =
1042 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1043
1044 if (finaldst == NULL)
1045 finaldst = &eip6->ip6_dst;
1046 ip6cp.ip6c_m = m;
1047 ip6cp.ip6c_icmp6 = icmp6;
1048 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1049 ip6cp.ip6c_off = eoff;
1050 ip6cp.ip6c_finaldst = finaldst;
1051 ip6cp.ip6c_src = &icmp6src;
1052 ip6cp.ip6c_nxt = nxt;
1053
1054 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1055 notifymtu = ntohl(icmp6->icmp6_mtu);
1056 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1057 }
1058
1059 ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
1060 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1061 if (ctlfunc) {
1062 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1063 &ip6cp);
1064 }
1065 }
1066 return (0);
1067
1068 freeit:
1069 m_freem(m);
1070 return (-1);
1071 }
1072
1073 void
1074 icmp6_mtudisc_update(ip6cp, validated)
1075 struct ip6ctlparam *ip6cp;
1076 int validated;
1077 {
1078 unsigned long rtcount;
1079 struct icmp6_mtudisc_callback *mc;
1080 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1081 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1082 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1083 u_int mtu = ntohl(icmp6->icmp6_mtu);
1084 struct rtentry *rt = NULL;
1085 struct sockaddr_in6 sin6;
1086
1087 /*
1088 * allow non-validated cases if memory is plenty, to make traffic
1089 * from non-connected pcb happy.
1090 */
1091 rtcount = rt_timer_count(icmp6_mtudisc_timeout_q);
1092 if (validated) {
1093 if (0 <= icmp6_mtudisc_hiwat && rtcount > icmp6_mtudisc_hiwat)
1094 return;
1095 else if (0 <= icmp6_mtudisc_lowat &&
1096 rtcount > icmp6_mtudisc_lowat) {
1097 /*
1098 * XXX nuke a victim, install the new one.
1099 */
1100 }
1101 } else {
1102 if (0 <= icmp6_mtudisc_lowat && rtcount > icmp6_mtudisc_lowat)
1103 return;
1104 }
1105
1106 bzero(&sin6, sizeof(sin6));
1107 sin6.sin6_family = PF_INET6;
1108 sin6.sin6_len = sizeof(struct sockaddr_in6);
1109 sin6.sin6_addr = *dst;
1110 if (in6_setscope(&sin6.sin6_addr, m->m_pkthdr.rcvif, NULL))
1111 return;
1112
1113 rt = icmp6_mtudisc_clone((struct sockaddr *)&sin6);
1114
1115 if (rt && (rt->rt_flags & RTF_HOST) &&
1116 !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
1117 (rt->rt_rmx.rmx_mtu > mtu || rt->rt_rmx.rmx_mtu == 0)) {
1118 if (mtu < IN6_LINKMTU(rt->rt_ifp)) {
1119 icmp6stat.icp6s_pmtuchg++;
1120 rt->rt_rmx.rmx_mtu = mtu;
1121 }
1122 }
1123 if (rt) { /* XXX: need braces to avoid conflict with else in RTFREE. */
1124 RTFREE(rt);
1125 }
1126
1127 /*
1128 * Notify protocols that the MTU for this destination
1129 * has changed.
1130 */
1131 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
1132 mc = LIST_NEXT(mc, mc_list))
1133 (*mc->mc_func)(&sin6.sin6_addr);
1134 }
1135
1136 /*
1137 * Process a Node Information Query packet, based on
1138 * draft-ietf-ipngwg-icmp-name-lookups-07.
1139 *
1140 * Spec incompatibilities:
1141 * - IPv6 Subject address handling
1142 * - IPv4 Subject address handling support missing
1143 * - Proxy reply (answer even if it's not for me)
1144 * - joins NI group address at in6_ifattach() time only, does not cope
1145 * with hostname changes by sethostname(3)
1146 */
1147 #ifndef offsetof /* XXX */
1148 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
1149 #endif
1150 static struct mbuf *
1151 ni6_input(m, off)
1152 struct mbuf *m;
1153 int off;
1154 {
1155 struct icmp6_nodeinfo *ni6, *nni6;
1156 struct mbuf *n = NULL;
1157 u_int16_t qtype;
1158 int subjlen;
1159 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1160 struct ni_reply_fqdn *fqdn;
1161 int addrs; /* for NI_QTYPE_NODEADDR */
1162 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1163 struct sockaddr_in6 sin6; /* ip6_dst */
1164 struct in6_addr in6_subj; /* subject address */
1165 struct ip6_hdr *ip6;
1166 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1167 char *subj = NULL;
1168
1169 ip6 = mtod(m, struct ip6_hdr *);
1170 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1171 if (ni6 == NULL) {
1172 /* m is already reclaimed */
1173 return NULL;
1174 }
1175
1176 /*
1177 * Validate IPv6 destination address.
1178 *
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.]
1183 */
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 (ifa_ifwithaddr((struct sockaddr *)&sin6))
1190 ; /* unicast/anycast, fine */
1191 else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1192 ; /* link-local multicast, fine */
1193 else
1194 goto bad;
1195
1196 /* validate query Subject field. */
1197 qtype = ntohs(ni6->ni_qtype);
1198 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1199 switch (qtype) {
1200 case NI_QTYPE_NOOP:
1201 case NI_QTYPE_SUPTYPES:
1202 /* 07 draft */
1203 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1204 break;
1205 /* FALLTHROUGH */
1206 case NI_QTYPE_FQDN:
1207 case NI_QTYPE_NODEADDR:
1208 case NI_QTYPE_IPV4ADDR:
1209 switch (ni6->ni_code) {
1210 case ICMP6_NI_SUBJ_IPV6:
1211 #if ICMP6_NI_SUBJ_IPV6 != 0
1212 case 0:
1213 #endif
1214 /*
1215 * backward compatibility - try to accept 03 draft
1216 * format, where no Subject is present.
1217 */
1218 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1219 subjlen == 0) {
1220 oldfqdn++;
1221 break;
1222 }
1223 #if ICMP6_NI_SUBJ_IPV6 != 0
1224 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1225 goto bad;
1226 #endif
1227
1228 if (subjlen != sizeof(sin6.sin6_addr))
1229 goto bad;
1230
1231 /*
1232 * Validate Subject address.
1233 *
1234 * Not sure what exactly "address belongs to the node"
1235 * means in the spec, is it just unicast, or what?
1236 *
1237 * At this moment we consider Subject address as
1238 * "belong to the node" if the Subject address equals
1239 * to the IPv6 destination address; validation for
1240 * IPv6 destination address should have done enough
1241 * check for us.
1242 *
1243 * We do not do proxy at this moment.
1244 */
1245 /* m_pulldown instead of copy? */
1246 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1247 subjlen, (void *)&in6_subj);
1248 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1249 goto bad;
1250
1251 subj = (char *)&in6_subj;
1252 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1253 break;
1254
1255 /*
1256 * XXX if we are to allow other cases, we should really
1257 * be careful about scope here.
1258 * basically, we should disallow queries toward IPv6
1259 * destination X with subject Y, if scope(X) > scope(Y).
1260 * if we allow scope(X) > scope(Y), it will result in
1261 * information leakage across scope boundary.
1262 */
1263 goto bad;
1264
1265 case ICMP6_NI_SUBJ_FQDN:
1266 /*
1267 * Validate Subject name with gethostname(3).
1268 *
1269 * The behavior may need some debate, since:
1270 * - we are not sure if the node has FQDN as
1271 * hostname (returned by gethostname(3)).
1272 * - the code does wildcard match for truncated names.
1273 * however, we are not sure if we want to perform
1274 * wildcard match, if gethostname(3) side has
1275 * truncated hostname.
1276 */
1277 n = ni6_nametodns(hostname, hostnamelen, 0);
1278 if (!n || n->m_next || n->m_len == 0)
1279 goto bad;
1280 IP6_EXTHDR_GET(subj, char *, m,
1281 off + sizeof(struct icmp6_nodeinfo), subjlen);
1282 if (subj == NULL)
1283 goto bad;
1284 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1285 n->m_len)) {
1286 goto bad;
1287 }
1288 m_freem(n);
1289 n = NULL;
1290 break;
1291
1292 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1293 default:
1294 goto bad;
1295 }
1296 break;
1297 }
1298
1299 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1300 switch (qtype) {
1301 case NI_QTYPE_FQDN:
1302 if ((icmp6_nodeinfo & 1) == 0)
1303 goto bad;
1304 break;
1305 case NI_QTYPE_NODEADDR:
1306 case NI_QTYPE_IPV4ADDR:
1307 if ((icmp6_nodeinfo & 2) == 0)
1308 goto bad;
1309 break;
1310 }
1311
1312 /* guess reply length */
1313 switch (qtype) {
1314 case NI_QTYPE_NOOP:
1315 break; /* no reply data */
1316 case NI_QTYPE_SUPTYPES:
1317 replylen += sizeof(u_int32_t);
1318 break;
1319 case NI_QTYPE_FQDN:
1320 /* XXX will append an mbuf */
1321 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1322 break;
1323 case NI_QTYPE_NODEADDR:
1324 addrs = ni6_addrs(ni6, m, &ifp, subj);
1325 if ((replylen += addrs * (sizeof(struct in6_addr) +
1326 sizeof(u_int32_t))) > MCLBYTES)
1327 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1328 break;
1329 case NI_QTYPE_IPV4ADDR:
1330 /* unsupported - should respond with unknown Qtype? */
1331 goto bad;
1332 default:
1333 /*
1334 * XXX: We must return a reply with the ICMP6 code
1335 * `unknown Qtype' in this case. However we regard the case
1336 * as an FQDN query for backward compatibility.
1337 * Older versions set a random value to this field,
1338 * so it rarely varies in the defined qtypes.
1339 * But the mechanism is not reliable...
1340 * maybe we should obsolete older versions.
1341 */
1342 qtype = NI_QTYPE_FQDN;
1343 /* XXX will append an mbuf */
1344 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1345 oldfqdn++;
1346 break;
1347 }
1348
1349 /* allocate an mbuf to reply. */
1350 MGETHDR(n, M_DONTWAIT, m->m_type);
1351 if (n == NULL) {
1352 m_freem(m);
1353 return (NULL);
1354 }
1355 M_MOVE_PKTHDR(n, m); /* just for rcvif */
1356 if (replylen > MHLEN) {
1357 if (replylen > MCLBYTES) {
1358 /*
1359 * XXX: should we try to allocate more? But MCLBYTES
1360 * is probably much larger than IPV6_MMTU...
1361 */
1362 goto bad;
1363 }
1364 MCLGET(n, M_DONTWAIT);
1365 if ((n->m_flags & M_EXT) == 0) {
1366 goto bad;
1367 }
1368 }
1369 n->m_pkthdr.len = n->m_len = replylen;
1370
1371 /* copy mbuf header and IPv6 + Node Information base headers */
1372 bcopy(mtod(m, void *), mtod(n, void *), sizeof(struct ip6_hdr));
1373 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1374 bcopy((void *)ni6, (void *)nni6, sizeof(struct icmp6_nodeinfo));
1375
1376 /* qtype dependent procedure */
1377 switch (qtype) {
1378 case NI_QTYPE_NOOP:
1379 nni6->ni_code = ICMP6_NI_SUCCESS;
1380 nni6->ni_flags = 0;
1381 break;
1382 case NI_QTYPE_SUPTYPES:
1383 {
1384 u_int32_t v;
1385 nni6->ni_code = ICMP6_NI_SUCCESS;
1386 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1387 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1388 v = (u_int32_t)htonl(0x0000000f);
1389 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1390 break;
1391 }
1392 case NI_QTYPE_FQDN:
1393 nni6->ni_code = ICMP6_NI_SUCCESS;
1394 fqdn = (struct ni_reply_fqdn *)(mtod(n, char *) +
1395 sizeof(struct ip6_hdr) +
1396 sizeof(struct icmp6_nodeinfo));
1397 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1398 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1399 /*
1400 * XXX do we really have FQDN in variable "hostname"?
1401 */
1402 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1403 if (n->m_next == NULL)
1404 goto bad;
1405 /* XXX we assume that n->m_next is not a chain */
1406 if (n->m_next->m_next != NULL)
1407 goto bad;
1408 n->m_pkthdr.len += n->m_next->m_len;
1409 break;
1410 case NI_QTYPE_NODEADDR:
1411 {
1412 int lenlim, copied;
1413
1414 nni6->ni_code = ICMP6_NI_SUCCESS;
1415 n->m_pkthdr.len = n->m_len =
1416 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1417 lenlim = M_TRAILINGSPACE(n);
1418 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1419 /* XXX: reset mbuf length */
1420 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1421 sizeof(struct icmp6_nodeinfo) + copied;
1422 break;
1423 }
1424 default:
1425 break; /* XXX impossible! */
1426 }
1427
1428 nni6->ni_type = ICMP6_NI_REPLY;
1429 m_freem(m);
1430 return (n);
1431
1432 bad:
1433 m_freem(m);
1434 if (n)
1435 m_freem(n);
1436 return (NULL);
1437 }
1438 #undef hostnamelen
1439
1440 #define isupper(x) ('A' <= (x) && (x) <= 'Z')
1441 #define isalpha(x) (('A' <= (x) && (x) <= 'Z') || ('a' <= (x) && (x) <= 'z'))
1442 #define isalnum(x) (isalpha(x) || ('0' <= (x) && (x) <= '9'))
1443 #define tolower(x) (isupper(x) ? (x) + 'a' - 'A' : (x))
1444
1445 /*
1446 * make a mbuf with DNS-encoded string. no compression support.
1447 *
1448 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1449 * treated as truncated name (two \0 at the end). this is a wild guess.
1450 */
1451 static struct mbuf *
1452 ni6_nametodns(name, namelen, old)
1453 const char *name;
1454 int namelen;
1455 int old; /* return pascal string if non-zero */
1456 {
1457 struct mbuf *m;
1458 char *cp, *ep;
1459 const char *p, *q;
1460 int i, len, nterm;
1461
1462 if (old)
1463 len = namelen + 1;
1464 else
1465 len = MCLBYTES;
1466
1467 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1468 MGET(m, M_DONTWAIT, MT_DATA);
1469 if (m && len > MLEN) {
1470 MCLGET(m, M_DONTWAIT);
1471 if ((m->m_flags & M_EXT) == 0)
1472 goto fail;
1473 }
1474 if (!m)
1475 goto fail;
1476 m->m_next = NULL;
1477
1478 if (old) {
1479 m->m_len = len;
1480 *mtod(m, char *) = namelen;
1481 bcopy(name, mtod(m, char *) + 1, namelen);
1482 return m;
1483 } else {
1484 m->m_len = 0;
1485 cp = mtod(m, char *);
1486 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1487
1488 /* if not certain about my name, return empty buffer */
1489 if (namelen == 0)
1490 return m;
1491
1492 /*
1493 * guess if it looks like shortened hostname, or FQDN.
1494 * shortened hostname needs two trailing "\0".
1495 */
1496 i = 0;
1497 for (p = name; p < name + namelen; p++) {
1498 if (*p && *p == '.')
1499 i++;
1500 }
1501 if (i < 2)
1502 nterm = 2;
1503 else
1504 nterm = 1;
1505
1506 p = name;
1507 while (cp < ep && p < name + namelen) {
1508 i = 0;
1509 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1510 i++;
1511 /* result does not fit into mbuf */
1512 if (cp + i + 1 >= ep)
1513 goto fail;
1514 /*
1515 * DNS label length restriction, RFC1035 page 8.
1516 * "i == 0" case is included here to avoid returning
1517 * 0-length label on "foo..bar".
1518 */
1519 if (i <= 0 || i >= 64)
1520 goto fail;
1521 *cp++ = i;
1522 if (!isalpha(p[0]) || !isalnum(p[i - 1]))
1523 goto fail;
1524 while (i > 0) {
1525 if (!isalnum(*p) && *p != '-')
1526 goto fail;
1527 if (isupper(*p)) {
1528 *cp++ = tolower(*p);
1529 p++;
1530 } else
1531 *cp++ = *p++;
1532 i--;
1533 }
1534 p = q;
1535 if (p < name + namelen && *p == '.')
1536 p++;
1537 }
1538 /* termination */
1539 if (cp + nterm >= ep)
1540 goto fail;
1541 while (nterm-- > 0)
1542 *cp++ = '\0';
1543 m->m_len = cp - mtod(m, char *);
1544 return m;
1545 }
1546
1547 panic("should not reach here");
1548 /* NOTREACHED */
1549
1550 fail:
1551 if (m)
1552 m_freem(m);
1553 return NULL;
1554 }
1555
1556 /*
1557 * check if two DNS-encoded string matches. takes care of truncated
1558 * form (with \0\0 at the end). no compression support.
1559 * XXX upper/lowercase match (see RFC2065)
1560 */
1561 static int
1562 ni6_dnsmatch(a, alen, b, blen)
1563 const char *a;
1564 int alen;
1565 const char *b;
1566 int blen;
1567 {
1568 const char *a0, *b0;
1569 int l;
1570
1571 /* simplest case - need validation? */
1572 if (alen == blen && bcmp(a, b, alen) == 0)
1573 return 1;
1574
1575 a0 = a;
1576 b0 = b;
1577
1578 /* termination is mandatory */
1579 if (alen < 2 || blen < 2)
1580 return 0;
1581 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1582 return 0;
1583 alen--;
1584 blen--;
1585
1586 while (a - a0 < alen && b - b0 < blen) {
1587 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1588 return 0;
1589
1590 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1591 return 0;
1592 /* we don't support compression yet */
1593 if (a[0] >= 64 || b[0] >= 64)
1594 return 0;
1595
1596 /* truncated case */
1597 if (a[0] == 0 && a - a0 == alen - 1)
1598 return 1;
1599 if (b[0] == 0 && b - b0 == blen - 1)
1600 return 1;
1601 if (a[0] == 0 || b[0] == 0)
1602 return 0;
1603
1604 if (a[0] != b[0])
1605 return 0;
1606 l = a[0];
1607 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1608 return 0;
1609 if (bcmp(a + 1, b + 1, l) != 0)
1610 return 0;
1611
1612 a += 1 + l;
1613 b += 1 + l;
1614 }
1615
1616 if (a - a0 == alen && b - b0 == blen)
1617 return 1;
1618 else
1619 return 0;
1620 }
1621
1622 /*
1623 * calculate the number of addresses to be returned in the node info reply.
1624 */
1625 static int
1626 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m,
1627 struct ifnet **ifpp, char *subj)
1628 {
1629 struct ifnet *ifp;
1630 struct in6_ifaddr *ifa6;
1631 struct ifaddr *ifa;
1632 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1633 int addrs = 0, addrsofif, iffound = 0;
1634 int niflags = ni6->ni_flags;
1635
1636 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1637 switch (ni6->ni_code) {
1638 case ICMP6_NI_SUBJ_IPV6:
1639 if (subj == NULL) /* must be impossible... */
1640 return (0);
1641 subj_ip6 = (struct sockaddr_in6 *)subj;
1642 break;
1643 default:
1644 /*
1645 * XXX: we only support IPv6 subject address for
1646 * this Qtype.
1647 */
1648 return (0);
1649 }
1650 }
1651
1652 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1653 {
1654 addrsofif = 0;
1655 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1656 ifa = ifa->ifa_list.tqe_next)
1657 {
1658 if (ifa->ifa_addr->sa_family != AF_INET6)
1659 continue;
1660 ifa6 = (struct in6_ifaddr *)ifa;
1661
1662 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1663 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1664 &ifa6->ia_addr.sin6_addr))
1665 iffound = 1;
1666
1667 /*
1668 * IPv4-mapped addresses can only be returned by a
1669 * Node Information proxy, since they represent
1670 * addresses of IPv4-only nodes, which perforce do
1671 * not implement this protocol.
1672 * [icmp-name-lookups-07, Section 5.4]
1673 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1674 * this function at this moment.
1675 */
1676
1677 /* What do we have to do about ::1? */
1678 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1679 case IPV6_ADDR_SCOPE_LINKLOCAL:
1680 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1681 continue;
1682 break;
1683 case IPV6_ADDR_SCOPE_SITELOCAL:
1684 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1685 continue;
1686 break;
1687 case IPV6_ADDR_SCOPE_GLOBAL:
1688 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1689 continue;
1690 break;
1691 default:
1692 continue;
1693 }
1694
1695 /*
1696 * check if anycast is okay.
1697 * XXX: just experimental. not in the spec.
1698 */
1699 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1700 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1701 continue; /* we need only unicast addresses */
1702
1703 addrsofif++; /* count the address */
1704 }
1705 if (iffound) {
1706 *ifpp = ifp;
1707 return (addrsofif);
1708 }
1709
1710 addrs += addrsofif;
1711 }
1712
1713 return (addrs);
1714 }
1715
1716 static int
1717 ni6_store_addrs(ni6, nni6, ifp0, resid)
1718 struct icmp6_nodeinfo *ni6, *nni6;
1719 struct ifnet *ifp0;
1720 int resid;
1721 {
1722 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1723 struct in6_ifaddr *ifa6;
1724 struct ifaddr *ifa;
1725 struct ifnet *ifp_dep = NULL;
1726 int copied = 0, allow_deprecated = 0;
1727 u_char *cp = (u_char *)(nni6 + 1);
1728 int niflags = ni6->ni_flags;
1729 u_int32_t ltime;
1730
1731 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1732 return (0); /* needless to copy */
1733
1734 again:
1735
1736 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1737 {
1738 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1739 ifa = ifa->ifa_list.tqe_next)
1740 {
1741 if (ifa->ifa_addr->sa_family != AF_INET6)
1742 continue;
1743 ifa6 = (struct in6_ifaddr *)ifa;
1744
1745 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1746 allow_deprecated == 0) {
1747 /*
1748 * prefererred address should be put before
1749 * deprecated addresses.
1750 */
1751
1752 /* record the interface for later search */
1753 if (ifp_dep == NULL)
1754 ifp_dep = ifp;
1755
1756 continue;
1757 }
1758 else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1759 allow_deprecated != 0)
1760 continue; /* we now collect deprecated addrs */
1761
1762 /* What do we have to do about ::1? */
1763 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1764 case IPV6_ADDR_SCOPE_LINKLOCAL:
1765 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1766 continue;
1767 break;
1768 case IPV6_ADDR_SCOPE_SITELOCAL:
1769 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1770 continue;
1771 break;
1772 case IPV6_ADDR_SCOPE_GLOBAL:
1773 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1774 continue;
1775 break;
1776 default:
1777 continue;
1778 }
1779
1780 /*
1781 * check if anycast is okay.
1782 * XXX: just experimental. not in the spec.
1783 */
1784 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1785 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1786 continue;
1787
1788 /* now we can copy the address */
1789 if (resid < sizeof(struct in6_addr) +
1790 sizeof(u_int32_t)) {
1791 /*
1792 * We give up much more copy.
1793 * Set the truncate flag and return.
1794 */
1795 nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1796 return (copied);
1797 }
1798
1799 /*
1800 * Set the TTL of the address.
1801 * The TTL value should be one of the following
1802 * according to the specification:
1803 *
1804 * 1. The remaining lifetime of a DHCP lease on the
1805 * address, or
1806 * 2. The remaining Valid Lifetime of a prefix from
1807 * which the address was derived through Stateless
1808 * Autoconfiguration.
1809 *
1810 * Note that we currently do not support stateful
1811 * address configuration by DHCPv6, so the former
1812 * case can't happen.
1813 *
1814 * TTL must be 2^31 > TTL >= 0.
1815 */
1816 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1817 ltime = ND6_INFINITE_LIFETIME;
1818 else {
1819 if (ifa6->ia6_lifetime.ia6t_expire >
1820 time_second)
1821 ltime = ifa6->ia6_lifetime.ia6t_expire -
1822 time_second;
1823 else
1824 ltime = 0;
1825 }
1826 if (ltime > 0x7fffffff)
1827 ltime = 0x7fffffff;
1828 ltime = htonl(ltime);
1829
1830 bcopy(<ime, cp, sizeof(u_int32_t));
1831 cp += sizeof(u_int32_t);
1832
1833 /* copy the address itself */
1834 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1835 sizeof(struct in6_addr));
1836 in6_clearscope((struct in6_addr *)cp); /* XXX */
1837 cp += sizeof(struct in6_addr);
1838
1839 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1840 copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1841 }
1842 if (ifp0) /* we need search only on the specified IF */
1843 break;
1844 }
1845
1846 if (allow_deprecated == 0 && ifp_dep != NULL) {
1847 ifp = ifp_dep;
1848 allow_deprecated = 1;
1849
1850 goto again;
1851 }
1852
1853 return (copied);
1854 }
1855
1856 /*
1857 * XXX almost dup'ed code with rip6_input.
1858 */
1859 static int
1860 icmp6_rip6_input(mp, off)
1861 struct mbuf **mp;
1862 int off;
1863 {
1864 struct mbuf *m = *mp;
1865 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1866 struct inpcb_hdr *inph;
1867 struct in6pcb *in6p;
1868 struct in6pcb *last = NULL;
1869 struct sockaddr_in6 rip6src;
1870 struct icmp6_hdr *icmp6;
1871 struct mbuf *opts = NULL;
1872
1873 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1874 if (icmp6 == NULL) {
1875 /* m is already reclaimed */
1876 return IPPROTO_DONE;
1877 }
1878
1879 /*
1880 * XXX: the address may have embedded scope zone ID, which should be
1881 * hidden from applications.
1882 */
1883 bzero(&rip6src, sizeof(rip6src));
1884 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1885 rip6src.sin6_family = AF_INET6;
1886 rip6src.sin6_addr = ip6->ip6_src;
1887 if (sa6_recoverscope(&rip6src)) {
1888 m_freem(m);
1889 return (IPPROTO_DONE);
1890 }
1891
1892 CIRCLEQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) {
1893 in6p = (struct in6pcb *)inph;
1894 if (in6p->in6p_af != AF_INET6)
1895 continue;
1896 if (in6p->in6p_ip6.ip6_nxt != IPPROTO_ICMPV6)
1897 continue;
1898 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1899 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1900 continue;
1901 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1902 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1903 continue;
1904 if (in6p->in6p_icmp6filt
1905 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1906 in6p->in6p_icmp6filt))
1907 continue;
1908 if (last) {
1909 struct mbuf *n;
1910 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1911 if (last->in6p_flags & IN6P_CONTROLOPTS)
1912 ip6_savecontrol(last, &opts, ip6, n);
1913 /* strip intermediate headers */
1914 m_adj(n, off);
1915 if (sbappendaddr(&last->in6p_socket->so_rcv,
1916 (struct sockaddr *)&rip6src,
1917 n, opts) == 0) {
1918 /* should notify about lost packet */
1919 m_freem(n);
1920 if (opts)
1921 m_freem(opts);
1922 } else
1923 sorwakeup(last->in6p_socket);
1924 opts = NULL;
1925 }
1926 }
1927 last = in6p;
1928 }
1929 if (last) {
1930 if (last->in6p_flags & IN6P_CONTROLOPTS)
1931 ip6_savecontrol(last, &opts, ip6, m);
1932 /* strip intermediate headers */
1933 m_adj(m, off);
1934 if (sbappendaddr(&last->in6p_socket->so_rcv,
1935 (struct sockaddr *)&rip6src, m, opts) == 0) {
1936 m_freem(m);
1937 if (opts)
1938 m_freem(opts);
1939 } else
1940 sorwakeup(last->in6p_socket);
1941 } else {
1942 m_freem(m);
1943 ip6stat.ip6s_delivered--;
1944 }
1945 return IPPROTO_DONE;
1946 }
1947
1948 /*
1949 * Reflect the ip6 packet back to the source.
1950 * OFF points to the icmp6 header, counted from the top of the mbuf.
1951 *
1952 * Note: RFC 1885 required that an echo reply should be truncated if it
1953 * did not fit in with (return) path MTU, and KAME code supported the
1954 * behavior. However, as a clarification after the RFC, this limitation
1955 * was removed in a revised version of the spec, RFC 2463. We had kept the
1956 * old behavior, with a (non-default) ifdef block, while the new version of
1957 * the spec was an internet-draft status, and even after the new RFC was
1958 * published. But it would rather make sense to clean the obsoleted part
1959 * up, and to make the code simpler at this stage.
1960 */
1961 void
1962 icmp6_reflect(struct mbuf *m, size_t off)
1963 {
1964 struct ip6_hdr *ip6;
1965 struct icmp6_hdr *icmp6;
1966 struct in6_ifaddr *ia;
1967 int plen;
1968 int type, code;
1969 struct ifnet *outif = NULL;
1970 struct in6_addr origdst, *src = NULL;
1971
1972 /* too short to reflect */
1973 if (off < sizeof(struct ip6_hdr)) {
1974 nd6log((LOG_DEBUG,
1975 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1976 (u_long)off, (u_long)sizeof(struct ip6_hdr),
1977 __FILE__, __LINE__));
1978 goto bad;
1979 }
1980
1981 /*
1982 * If there are extra headers between IPv6 and ICMPv6, strip
1983 * off that header first.
1984 */
1985 #ifdef DIAGNOSTIC
1986 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
1987 panic("assumption failed in icmp6_reflect");
1988 #endif
1989 if (off > sizeof(struct ip6_hdr)) {
1990 size_t l;
1991 struct ip6_hdr nip6;
1992
1993 l = off - sizeof(struct ip6_hdr);
1994 m_copydata(m, 0, sizeof(nip6), (void *)&nip6);
1995 m_adj(m, l);
1996 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1997 if (m->m_len < l) {
1998 if ((m = m_pullup(m, l)) == NULL)
1999 return;
2000 }
2001 bcopy((void *)&nip6, mtod(m, void *), sizeof(nip6));
2002 } else /* off == sizeof(struct ip6_hdr) */ {
2003 size_t l;
2004 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2005 if (m->m_len < l) {
2006 if ((m = m_pullup(m, l)) == NULL)
2007 return;
2008 }
2009 }
2010 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2011 ip6 = mtod(m, struct ip6_hdr *);
2012 ip6->ip6_nxt = IPPROTO_ICMPV6;
2013 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2014 type = icmp6->icmp6_type; /* keep type for statistics */
2015 code = icmp6->icmp6_code; /* ditto. */
2016
2017 origdst = ip6->ip6_dst;
2018 /*
2019 * ip6_input() drops a packet if its src is multicast.
2020 * So, the src is never multicast.
2021 */
2022 ip6->ip6_dst = ip6->ip6_src;
2023
2024 /*
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 should 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.
2030 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2031 * procedure of an outgoing packet of our own, in which case we need
2032 * to search in the ifaddr list.
2033 */
2034 if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
2035 if ((ia = ip6_getdstifaddr(m))) {
2036 if (!(ia->ia6_flags &
2037 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2038 src = &ia->ia_addr.sin6_addr;
2039 } else {
2040 struct sockaddr_in6 d;
2041
2042 bzero(&d, sizeof(d));
2043 d.sin6_family = AF_INET6;
2044 d.sin6_len = sizeof(d);
2045 d.sin6_addr = origdst;
2046 ia = (struct in6_ifaddr *)
2047 ifa_ifwithaddr((struct sockaddr *)&d);
2048 if (ia &&
2049 !(ia->ia6_flags &
2050 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2051 src = &ia->ia_addr.sin6_addr;
2052 }
2053 }
2054 }
2055
2056 if (src == NULL) {
2057 int e;
2058 struct sockaddr_in6 sin6;
2059 struct route ro;
2060
2061 /*
2062 * This case matches to multicasts, our anycast, or unicasts
2063 * that we do not own. Select a source address based on the
2064 * source address of the erroneous packet.
2065 */
2066 memset(&sin6, 0, sizeof(sin6));
2067 sin6.sin6_family = AF_INET6;
2068 sin6.sin6_len = sizeof(sin6);
2069 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2070
2071 memset(&ro, 0, sizeof(ro));
2072 src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e);
2073 rtcache_free(&ro);
2074 if (src == NULL) {
2075 nd6log((LOG_DEBUG,
2076 "icmp6_reflect: source can't be determined: "
2077 "dst=%s, error=%d\n",
2078 ip6_sprintf(&sin6.sin6_addr), e));
2079 goto bad;
2080 }
2081 }
2082
2083 ip6->ip6_src = *src;
2084 ip6->ip6_flow = 0;
2085 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2086 ip6->ip6_vfc |= IPV6_VERSION;
2087 ip6->ip6_nxt = IPPROTO_ICMPV6;
2088 if (m->m_pkthdr.rcvif) {
2089 /* XXX: This may not be the outgoing interface */
2090 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2091 } else
2092 ip6->ip6_hlim = ip6_defhlim;
2093
2094 m->m_pkthdr.csum_flags = 0;
2095 icmp6->icmp6_cksum = 0;
2096 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2097 sizeof(struct ip6_hdr), plen);
2098
2099 /*
2100 * XXX option handling
2101 */
2102
2103 m->m_flags &= ~(M_BCAST|M_MCAST);
2104
2105 /*
2106 * To avoid a "too big" situation at an intermediate router
2107 * and the path MTU discovery process, specify the IPV6_MINMTU flag.
2108 * Note that only echo and node information replies are affected,
2109 * since the length of ICMP6 errors is limited to the minimum MTU.
2110 */
2111 if (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, &outif) != 0 &&
2112 outif)
2113 icmp6_ifstat_inc(outif, ifs6_out_error);
2114
2115 if (outif)
2116 icmp6_ifoutstat_inc(outif, type, code);
2117
2118 return;
2119
2120 bad:
2121 m_freem(m);
2122 return;
2123 }
2124
2125 static const char *
2126 icmp6_redirect_diag(src6, dst6, tgt6)
2127 struct in6_addr *src6;
2128 struct in6_addr *dst6;
2129 struct in6_addr *tgt6;
2130 {
2131 static char buf[1024];
2132 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2133 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2134 return buf;
2135 }
2136
2137 void
2138 icmp6_redirect_input(m, off)
2139 struct mbuf *m;
2140 int off;
2141 {
2142 struct ifnet *ifp = m->m_pkthdr.rcvif;
2143 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2144 struct nd_redirect *nd_rd;
2145 int icmp6len = ntohs(ip6->ip6_plen);
2146 char *lladdr = NULL;
2147 int lladdrlen = 0;
2148 struct rtentry *rt = NULL;
2149 int is_router;
2150 int is_onlink;
2151 struct in6_addr src6 = ip6->ip6_src;
2152 struct in6_addr redtgt6;
2153 struct in6_addr reddst6;
2154 union nd_opts ndopts;
2155
2156 if (!ifp)
2157 return;
2158
2159 /* XXX if we are router, we don't update route by icmp6 redirect */
2160 if (ip6_forwarding)
2161 goto freeit;
2162 if (!icmp6_rediraccept)
2163 goto freeit;
2164
2165 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2166 if (nd_rd == NULL) {
2167 icmp6stat.icp6s_tooshort++;
2168 return;
2169 }
2170 redtgt6 = nd_rd->nd_rd_target;
2171 reddst6 = nd_rd->nd_rd_dst;
2172
2173 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2174 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2175 goto freeit;
2176 }
2177
2178 /* validation */
2179 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2180 nd6log((LOG_ERR,
2181 "ICMP6 redirect sent from %s rejected; "
2182 "must be from linklocal\n", ip6_sprintf(&src6)));
2183 goto bad;
2184 }
2185 if (ip6->ip6_hlim != 255) {
2186 nd6log((LOG_ERR,
2187 "ICMP6 redirect sent from %s rejected; "
2188 "hlim=%d (must be 255)\n",
2189 ip6_sprintf(&src6), ip6->ip6_hlim));
2190 goto bad;
2191 }
2192 {
2193 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2194 struct sockaddr_in6 sin6;
2195 struct in6_addr *gw6;
2196
2197 bzero(&sin6, sizeof(sin6));
2198 sin6.sin6_family = AF_INET6;
2199 sin6.sin6_len = sizeof(struct sockaddr_in6);
2200 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2201 rt = rtalloc1((struct sockaddr *)&sin6, 0);
2202 if (rt) {
2203 if (rt->rt_gateway == NULL ||
2204 rt->rt_gateway->sa_family != AF_INET6) {
2205 nd6log((LOG_ERR,
2206 "ICMP6 redirect rejected; no route "
2207 "with inet6 gateway found for redirect dst: %s\n",
2208 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2209 RTFREE(rt);
2210 goto bad;
2211 }
2212
2213 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2214 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2215 nd6log((LOG_ERR,
2216 "ICMP6 redirect rejected; "
2217 "not equal to gw-for-src=%s (must be same): "
2218 "%s\n",
2219 ip6_sprintf(gw6),
2220 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2221 RTFREE(rt);
2222 goto bad;
2223 }
2224 } else {
2225 nd6log((LOG_ERR,
2226 "ICMP6 redirect rejected; "
2227 "no route found for redirect dst: %s\n",
2228 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2229 goto bad;
2230 }
2231 RTFREE(rt);
2232 rt = NULL;
2233 }
2234 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2235 nd6log((LOG_ERR,
2236 "ICMP6 redirect rejected; "
2237 "redirect dst must be unicast: %s\n",
2238 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2239 goto bad;
2240 }
2241
2242 is_router = is_onlink = 0;
2243 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2244 is_router = 1; /* router case */
2245 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2246 is_onlink = 1; /* on-link destination case */
2247 if (!is_router && !is_onlink) {
2248 nd6log((LOG_ERR,
2249 "ICMP6 redirect rejected; "
2250 "neither router case nor onlink case: %s\n",
2251 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2252 goto bad;
2253 }
2254 /* validation passed */
2255
2256 icmp6len -= sizeof(*nd_rd);
2257 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2258 if (nd6_options(&ndopts) < 0) {
2259 nd6log((LOG_INFO, "icmp6_redirect_input: "
2260 "invalid ND option, rejected: %s\n",
2261 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2262 /* nd6_options have incremented stats */
2263 goto freeit;
2264 }
2265
2266 if (ndopts.nd_opts_tgt_lladdr) {
2267 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2268 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2269 }
2270
2271 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2272 nd6log((LOG_INFO,
2273 "icmp6_redirect_input: lladdrlen mismatch for %s "
2274 "(if %d, icmp6 packet %d): %s\n",
2275 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2276 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2277 goto bad;
2278 }
2279
2280 /* RFC 2461 8.3 */
2281 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2282 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2283
2284 if (!is_onlink) { /* better router case. perform rtredirect. */
2285 /* perform rtredirect */
2286 struct sockaddr_in6 sdst;
2287 struct sockaddr_in6 sgw;
2288 struct sockaddr_in6 ssrc;
2289 unsigned long rtcount;
2290 struct rtentry *newrt = NULL;
2291
2292 /*
2293 * do not install redirect route, if the number of entries
2294 * is too much (> hiwat). note that, the node (= host) will
2295 * work just fine even if we do not install redirect route
2296 * (there will be additional hops, though).
2297 */
2298 rtcount = rt_timer_count(icmp6_redirect_timeout_q);
2299 if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat)
2300 return;
2301 else if (0 <= icmp6_redirect_lowat &&
2302 rtcount > icmp6_redirect_lowat) {
2303 /*
2304 * XXX nuke a victim, install the new one.
2305 */
2306 }
2307
2308 bzero(&sdst, sizeof(sdst));
2309 bzero(&sgw, sizeof(sgw));
2310 bzero(&ssrc, sizeof(ssrc));
2311 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2312 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2313 sizeof(struct sockaddr_in6);
2314 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2315 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2316 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2317 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2318 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2319 (struct sockaddr *)&ssrc,
2320 &newrt);
2321
2322 if (newrt) {
2323 (void)rt_timer_add(newrt, icmp6_redirect_timeout,
2324 icmp6_redirect_timeout_q);
2325 rtfree(newrt);
2326 }
2327 }
2328 /* finally update cached route in each socket via pfctlinput */
2329 {
2330 struct sockaddr_in6 sdst;
2331
2332 bzero(&sdst, sizeof(sdst));
2333 sdst.sin6_family = AF_INET6;
2334 sdst.sin6_len = sizeof(struct sockaddr_in6);
2335 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2336 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2337 #if defined(IPSEC) || defined(FAST_IPSEC)
2338 key_sa_routechange((struct sockaddr *)&sdst);
2339 #endif
2340 }
2341
2342 freeit:
2343 m_freem(m);
2344 return;
2345
2346 bad:
2347 icmp6stat.icp6s_badredirect++;
2348 m_freem(m);
2349 }
2350
2351 void
2352 icmp6_redirect_output(m0, rt)
2353 struct mbuf *m0;
2354 struct rtentry *rt;
2355 {
2356 struct ifnet *ifp; /* my outgoing interface */
2357 struct in6_addr *ifp_ll6;
2358 struct in6_addr *nexthop;
2359 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2360 struct mbuf *m = NULL; /* newly allocated one */
2361 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2362 struct nd_redirect *nd_rd;
2363 size_t maxlen;
2364 u_char *p;
2365 struct sockaddr_in6 src_sa;
2366
2367 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2368
2369 /* if we are not router, we don't send icmp6 redirect */
2370 if (!ip6_forwarding)
2371 goto fail;
2372
2373 /* sanity check */
2374 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2375 goto fail;
2376
2377 /*
2378 * Address check:
2379 * the source address must identify a neighbor, and
2380 * the destination address must not be a multicast address
2381 * [RFC 2461, sec 8.2]
2382 */
2383 sip6 = mtod(m0, struct ip6_hdr *);
2384 bzero(&src_sa, sizeof(src_sa));
2385 src_sa.sin6_family = AF_INET6;
2386 src_sa.sin6_len = sizeof(src_sa);
2387 src_sa.sin6_addr = sip6->ip6_src;
2388 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2389 goto fail;
2390 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2391 goto fail; /* what should we do here? */
2392
2393 /* rate limit */
2394 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2395 goto fail;
2396
2397 /*
2398 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2399 * we almost always ask for an mbuf cluster for simplicity.
2400 * (MHLEN < IPV6_MMTU is almost always true)
2401 */
2402 #if IPV6_MMTU >= MCLBYTES
2403 # error assumption failed about IPV6_MMTU and MCLBYTES
2404 #endif
2405 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2406 if (m && IPV6_MMTU >= MHLEN)
2407 MCLGET(m, M_DONTWAIT);
2408 if (!m)
2409 goto fail;
2410 m->m_pkthdr.rcvif = NULL;
2411 m->m_len = 0;
2412 maxlen = M_TRAILINGSPACE(m);
2413 maxlen = min(IPV6_MMTU, maxlen);
2414 /* just for safety */
2415 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2416 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2417 goto fail;
2418 }
2419
2420 {
2421 /* get ip6 linklocal address for ifp(my outgoing interface). */
2422 struct in6_ifaddr *ia;
2423 if ((ia = in6ifa_ifpforlinklocal(ifp,
2424 IN6_IFF_NOTREADY|
2425 IN6_IFF_ANYCAST)) == NULL)
2426 goto fail;
2427 ifp_ll6 = &ia->ia_addr.sin6_addr;
2428 }
2429
2430 /* get ip6 linklocal address for the router. */
2431 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2432 struct sockaddr_in6 *sin6;
2433 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2434 nexthop = &sin6->sin6_addr;
2435 if (!IN6_IS_ADDR_LINKLOCAL(nexthop))
2436 nexthop = NULL;
2437 } else
2438 nexthop = NULL;
2439
2440 /* ip6 */
2441 ip6 = mtod(m, struct ip6_hdr *);
2442 ip6->ip6_flow = 0;
2443 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2444 ip6->ip6_vfc |= IPV6_VERSION;
2445 /* ip6->ip6_plen will be set later */
2446 ip6->ip6_nxt = IPPROTO_ICMPV6;
2447 ip6->ip6_hlim = 255;
2448 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2449 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2450 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2451
2452 /* ND Redirect */
2453 nd_rd = (struct nd_redirect *)(ip6 + 1);
2454 nd_rd->nd_rd_type = ND_REDIRECT;
2455 nd_rd->nd_rd_code = 0;
2456 nd_rd->nd_rd_reserved = 0;
2457 if (rt->rt_flags & RTF_GATEWAY) {
2458 /*
2459 * nd_rd->nd_rd_target must be a link-local address in
2460 * better router cases.
2461 */
2462 if (!nexthop)
2463 goto fail;
2464 bcopy(nexthop, &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));
2468 } else {
2469 /* make sure redtgt == reddst */
2470 nexthop = &sip6->ip6_dst;
2471 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2472 sizeof(nd_rd->nd_rd_target));
2473 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2474 sizeof(nd_rd->nd_rd_dst));
2475 }
2476
2477 p = (u_char *)(nd_rd + 1);
2478
2479 {
2480 /* target lladdr option */
2481 struct rtentry *rt_nexthop = NULL;
2482 int len;
2483 struct sockaddr_dl *sdl;
2484 struct nd_opt_hdr *nd_opt;
2485 char *lladdr;
2486
2487 rt_nexthop = nd6_lookup(nexthop, 0, ifp);
2488 if (!rt_nexthop)
2489 goto nolladdropt;
2490 len = sizeof(*nd_opt) + ifp->if_addrlen;
2491 len = (len + 7) & ~7; /* round by 8 */
2492 /* safety check */
2493 if (len + (p - (u_char *)ip6) > maxlen)
2494 goto nolladdropt;
2495 if (!(rt_nexthop->rt_flags & RTF_GATEWAY) &&
2496 (rt_nexthop->rt_flags & RTF_LLINFO) &&
2497 (rt_nexthop->rt_gateway->sa_family == AF_LINK) &&
2498 (sdl = (struct sockaddr_dl *)rt_nexthop->rt_gateway) &&
2499 sdl->sdl_alen) {
2500 nd_opt = (struct nd_opt_hdr *)p;
2501 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2502 nd_opt->nd_opt_len = len >> 3;
2503 lladdr = (char *)(nd_opt + 1);
2504 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2505 p += len;
2506 }
2507 }
2508 nolladdropt:;
2509
2510 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2511
2512 /* just to be safe */
2513 if (m0->m_flags & M_DECRYPTED)
2514 goto noredhdropt;
2515 if (p - (u_char *)ip6 > maxlen)
2516 goto noredhdropt;
2517
2518 {
2519 /* redirected header option */
2520 int len;
2521 struct nd_opt_rd_hdr *nd_opt_rh;
2522
2523 /*
2524 * compute the maximum size for icmp6 redirect header option.
2525 * XXX room for auth header?
2526 */
2527 len = maxlen - (p - (u_char *)ip6);
2528 len &= ~7;
2529
2530 /*
2531 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2532 * about padding/truncate rule for the original IP packet.
2533 * From the discussion on IPv6imp in Feb 1999,
2534 * the consensus was:
2535 * - "attach as much as possible" is the goal
2536 * - pad if not aligned (original size can be guessed by
2537 * original ip6 header)
2538 * Following code adds the padding if it is simple enough,
2539 * and truncates if not.
2540 */
2541 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2542 /* not enough room, truncate */
2543 m_adj(m0, (len - sizeof(*nd_opt_rh)) -
2544 m0->m_pkthdr.len);
2545 } else {
2546 /*
2547 * enough room, truncate if not aligned.
2548 * we don't pad here for simplicity.
2549 */
2550 size_t extra;
2551
2552 extra = m0->m_pkthdr.len % 8;
2553 if (extra) {
2554 /* truncate */
2555 m_adj(m0, -extra);
2556 }
2557 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2558 }
2559
2560 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2561 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2562 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2563 nd_opt_rh->nd_opt_rh_len = len >> 3;
2564 p += sizeof(*nd_opt_rh);
2565 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2566
2567 /* connect m0 to m */
2568 m->m_pkthdr.len += m0->m_pkthdr.len;
2569 m_cat(m, m0);
2570 m0 = NULL;
2571 }
2572 noredhdropt:
2573 if (m0) {
2574 m_freem(m0);
2575 m0 = NULL;
2576 }
2577
2578 /* XXX: clear embedded link IDs in the inner header */
2579 in6_clearscope(&sip6->ip6_src);
2580 in6_clearscope(&sip6->ip6_dst);
2581 in6_clearscope(&nd_rd->nd_rd_target);
2582 in6_clearscope(&nd_rd->nd_rd_dst);
2583
2584 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2585
2586 nd_rd->nd_rd_cksum = 0;
2587 nd_rd->nd_rd_cksum
2588 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2589
2590 /* send the packet to outside... */
2591 if (ip6_output(m, NULL, NULL, 0,
2592 (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL) != 0)
2593 icmp6_ifstat_inc(ifp, ifs6_out_error);
2594
2595 icmp6_ifstat_inc(ifp, ifs6_out_msg);
2596 icmp6_ifstat_inc(ifp, ifs6_out_redirect);
2597 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2598
2599 return;
2600
2601 fail:
2602 if (m)
2603 m_freem(m);
2604 if (m0)
2605 m_freem(m0);
2606 }
2607
2608 /*
2609 * ICMPv6 socket option processing.
2610 */
2611 int
2612 icmp6_ctloutput(op, so, level, optname, mp)
2613 int op;
2614 struct socket *so;
2615 int level, optname;
2616 struct mbuf **mp;
2617 {
2618 int error = 0;
2619 int optlen;
2620 struct in6pcb *in6p = sotoin6pcb(so);
2621 struct mbuf *m = *mp;
2622
2623 optlen = m ? m->m_len : 0;
2624
2625 if (level != IPPROTO_ICMPV6) {
2626 if (op == PRCO_SETOPT && m)
2627 (void)m_free(m);
2628 return EINVAL;
2629 }
2630
2631 switch (op) {
2632 case PRCO_SETOPT:
2633 switch (optname) {
2634 case ICMP6_FILTER:
2635 {
2636 struct icmp6_filter *p;
2637
2638 if (optlen != sizeof(*p)) {
2639 error = EMSGSIZE;
2640 break;
2641 }
2642 p = mtod(m, struct icmp6_filter *);
2643 if (!p || !in6p->in6p_icmp6filt) {
2644 error = EINVAL;
2645 break;
2646 }
2647 bcopy(p, in6p->in6p_icmp6filt,
2648 sizeof(struct icmp6_filter));
2649 error = 0;
2650 break;
2651 }
2652
2653 default:
2654 error = ENOPROTOOPT;
2655 break;
2656 }
2657 if (m)
2658 (void)m_freem(m);
2659 break;
2660
2661 case PRCO_GETOPT:
2662 switch (optname) {
2663 case ICMP6_FILTER:
2664 {
2665 struct icmp6_filter *p;
2666
2667 if (!in6p->in6p_icmp6filt) {
2668 error = EINVAL;
2669 break;
2670 }
2671 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2672 m->m_len = sizeof(struct icmp6_filter);
2673 p = mtod(m, struct icmp6_filter *);
2674 bcopy(in6p->in6p_icmp6filt, p,
2675 sizeof(struct icmp6_filter));
2676 error = 0;
2677 break;
2678 }
2679
2680 default:
2681 error = ENOPROTOOPT;
2682 break;
2683 }
2684 break;
2685 }
2686
2687 return (error);
2688 }
2689
2690 /*
2691 * Perform rate limit check.
2692 * Returns 0 if it is okay to send the icmp6 packet.
2693 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2694 * limitation.
2695 *
2696 * XXX per-destination/type check necessary?
2697 */
2698 static int
2699 icmp6_ratelimit(
2700 const struct in6_addr *dst, /* not used at this moment */
2701 const int type, /* not used at this moment */
2702 const int code) /* not used at this moment */
2703 {
2704 int ret;
2705
2706 ret = 0; /* okay to send */
2707
2708 /* PPS limit */
2709 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2710 icmp6errppslim)) {
2711 /* The packet is subject to rate limit */
2712 ret++;
2713 }
2714
2715 return ret;
2716 }
2717
2718 static struct rtentry *
2719 icmp6_mtudisc_clone(dst)
2720 struct sockaddr *dst;
2721 {
2722 struct rtentry *rt;
2723 int error;
2724
2725 rt = rtalloc1(dst, 1);
2726 if (rt == 0)
2727 return NULL;
2728
2729 /* If we didn't get a host route, allocate one */
2730 if ((rt->rt_flags & RTF_HOST) == 0) {
2731 struct rtentry *nrt;
2732
2733 error = rtrequest((int) RTM_ADD, dst,
2734 (struct sockaddr *) rt->rt_gateway,
2735 (struct sockaddr *) 0,
2736 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
2737 if (error) {
2738 rtfree(rt);
2739 return NULL;
2740 }
2741 nrt->rt_rmx = rt->rt_rmx;
2742 rtfree(rt);
2743 rt = nrt;
2744 }
2745 error = rt_timer_add(rt, icmp6_mtudisc_timeout,
2746 icmp6_mtudisc_timeout_q);
2747 if (error) {
2748 rtfree(rt);
2749 return NULL;
2750 }
2751
2752 return rt; /* caller need to call rtfree() */
2753 }
2754
2755 static void
2756 icmp6_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
2757 {
2758 if (rt == NULL)
2759 panic("icmp6_mtudisc_timeout: bad route to timeout");
2760 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
2761 (RTF_DYNAMIC | RTF_HOST)) {
2762 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2763 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2764 } else {
2765 if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
2766 rt->rt_rmx.rmx_mtu = 0;
2767 }
2768 }
2769
2770 static void
2771 icmp6_redirect_timeout(struct rtentry *rt, struct rttimer *r)
2772 {
2773 if (rt == NULL)
2774 panic("icmp6_redirect_timeout: bad route to timeout");
2775 if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
2776 (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
2777 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2778 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2779 }
2780 }
2781
2782 /*
2783 * sysctl helper routine for the net.inet6.icmp6.nd6 nodes. silly?
2784 */
2785 static int
2786 sysctl_net_inet6_icmp6_nd6(SYSCTLFN_ARGS)
2787 {
2788 (void)&name;
2789 (void)&l;
2790 (void)&oname;
2791
2792 if (namelen != 0)
2793 return (EINVAL);
2794
2795 return (nd6_sysctl(rnode->sysctl_num, oldp, oldlenp,
2796 /*XXXUNCONST*/
2797 __UNCONST(newp), newlen));
2798 }
2799
2800 SYSCTL_SETUP(sysctl_net_inet6_icmp6_setup,
2801 "sysctl net.inet6.icmp6 subtree setup")
2802 {
2803 extern int nd6_maxqueuelen; /* defined in nd6.c */
2804
2805 sysctl_createv(clog, 0, NULL, NULL,
2806 CTLFLAG_PERMANENT,
2807 CTLTYPE_NODE, "net", NULL,
2808 NULL, 0, NULL, 0,
2809 CTL_NET, CTL_EOL);
2810 sysctl_createv(clog, 0, NULL, NULL,
2811 CTLFLAG_PERMANENT,
2812 CTLTYPE_NODE, "inet6", NULL,
2813 NULL, 0, NULL, 0,
2814 CTL_NET, PF_INET6, CTL_EOL);
2815 sysctl_createv(clog, 0, NULL, NULL,
2816 CTLFLAG_PERMANENT,
2817 CTLTYPE_NODE, "icmp6",
2818 SYSCTL_DESCR("ICMPv6 related settings"),
2819 NULL, 0, NULL, 0,
2820 CTL_NET, PF_INET6, IPPROTO_ICMPV6, CTL_EOL);
2821
2822 sysctl_createv(clog, 0, NULL, NULL,
2823 CTLFLAG_PERMANENT,
2824 CTLTYPE_STRUCT, "stats",
2825 SYSCTL_DESCR("ICMPv6 transmission statistics"),
2826 NULL, 0, &icmp6stat, sizeof(icmp6stat),
2827 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2828 ICMPV6CTL_STATS, CTL_EOL);
2829 sysctl_createv(clog, 0, NULL, NULL,
2830 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2831 CTLTYPE_INT, "rediraccept",
2832 SYSCTL_DESCR("Accept and process redirect messages"),
2833 NULL, 0, &icmp6_rediraccept, 0,
2834 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2835 ICMPV6CTL_REDIRACCEPT, CTL_EOL);
2836 sysctl_createv(clog, 0, NULL, NULL,
2837 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2838 CTLTYPE_INT, "redirtimeout",
2839 SYSCTL_DESCR("Redirect generated route lifetime"),
2840 NULL, 0, &icmp6_redirtimeout, 0,
2841 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2842 ICMPV6CTL_REDIRTIMEOUT, CTL_EOL);
2843 #if 0 /* obsoleted */
2844 sysctl_createv(clog, 0, NULL, NULL,
2845 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2846 CTLTYPE_INT, "errratelimit", NULL,
2847 NULL, 0, &icmp6_errratelimit, 0,
2848 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2849 ICMPV6CTL_ERRRATELIMIT, CTL_EOL);
2850 #endif
2851 sysctl_createv(clog, 0, NULL, NULL,
2852 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2853 CTLTYPE_INT, "nd6_prune",
2854 SYSCTL_DESCR("Neighbor discovery prune interval"),
2855 NULL, 0, &nd6_prune, 0,
2856 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2857 ICMPV6CTL_ND6_PRUNE, CTL_EOL);
2858 sysctl_createv(clog, 0, NULL, NULL,
2859 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2860 CTLTYPE_INT, "nd6_delay",
2861 SYSCTL_DESCR("First probe delay time"),
2862 NULL, 0, &nd6_delay, 0,
2863 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2864 ICMPV6CTL_ND6_DELAY, CTL_EOL);
2865 sysctl_createv(clog, 0, NULL, NULL,
2866 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2867 CTLTYPE_INT, "nd6_umaxtries",
2868 SYSCTL_DESCR("Number of unicast discovery attempts"),
2869 NULL, 0, &nd6_umaxtries, 0,
2870 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2871 ICMPV6CTL_ND6_UMAXTRIES, CTL_EOL);
2872 sysctl_createv(clog, 0, NULL, NULL,
2873 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2874 CTLTYPE_INT, "nd6_mmaxtries",
2875 SYSCTL_DESCR("Number of multicast discovery attempts"),
2876 NULL, 0, &nd6_mmaxtries, 0,
2877 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2878 ICMPV6CTL_ND6_MMAXTRIES, CTL_EOL);
2879 sysctl_createv(clog, 0, NULL, NULL,
2880 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2881 CTLTYPE_INT, "nd6_useloopback",
2882 SYSCTL_DESCR("Use loopback interface for local traffic"),
2883 NULL, 0, &nd6_useloopback, 0,
2884 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2885 ICMPV6CTL_ND6_USELOOPBACK, CTL_EOL);
2886 #if 0 /* obsoleted */
2887 sysctl_createv(clog, 0, NULL, NULL,
2888 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2889 CTLTYPE_INT, "nd6_proxyall", NULL,
2890 NULL, 0, &nd6_proxyall, 0,
2891 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2892 ICMPV6CTL_ND6_PROXYALL, CTL_EOL);
2893 #endif
2894 sysctl_createv(clog, 0, NULL, NULL,
2895 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2896 CTLTYPE_INT, "nodeinfo",
2897 SYSCTL_DESCR("Respond to node information requests"),
2898 NULL, 0, &icmp6_nodeinfo, 0,
2899 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2900 ICMPV6CTL_NODEINFO, CTL_EOL);
2901 sysctl_createv(clog, 0, NULL, NULL,
2902 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2903 CTLTYPE_INT, "errppslimit",
2904 SYSCTL_DESCR("Maximum ICMP errors sent per second"),
2905 NULL, 0, &icmp6errppslim, 0,
2906 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2907 ICMPV6CTL_ERRPPSLIMIT, CTL_EOL);
2908 sysctl_createv(clog, 0, NULL, NULL,
2909 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2910 CTLTYPE_INT, "nd6_maxnudhint",
2911 SYSCTL_DESCR("Maximum neighbor unreachable hint count"),
2912 NULL, 0, &nd6_maxnudhint, 0,
2913 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2914 ICMPV6CTL_ND6_MAXNUDHINT, CTL_EOL);
2915 sysctl_createv(clog, 0, NULL, NULL,
2916 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2917 CTLTYPE_INT, "mtudisc_hiwat",
2918 SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
2919 NULL, 0, &icmp6_mtudisc_hiwat, 0,
2920 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2921 ICMPV6CTL_MTUDISC_HIWAT, CTL_EOL);
2922 sysctl_createv(clog, 0, NULL, NULL,
2923 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2924 CTLTYPE_INT, "mtudisc_lowat",
2925 SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
2926 NULL, 0, &icmp6_mtudisc_lowat, 0,
2927 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2928 ICMPV6CTL_MTUDISC_LOWAT, CTL_EOL);
2929 sysctl_createv(clog, 0, NULL, NULL,
2930 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2931 CTLTYPE_INT, "nd6_debug",
2932 SYSCTL_DESCR("Enable neighbor discovery debug output"),
2933 NULL, 0, &nd6_debug, 0,
2934 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2935 ICMPV6CTL_ND6_DEBUG, CTL_EOL);
2936 sysctl_createv(clog, 0, NULL, NULL,
2937 CTLFLAG_PERMANENT,
2938 CTLTYPE_STRUCT, "nd6_drlist",
2939 SYSCTL_DESCR("Default router list"),
2940 sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
2941 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2942 ICMPV6CTL_ND6_DRLIST, CTL_EOL);
2943 sysctl_createv(clog, 0, NULL, NULL,
2944 CTLFLAG_PERMANENT,
2945 CTLTYPE_STRUCT, "nd6_prlist",
2946 SYSCTL_DESCR("Prefix list"),
2947 sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
2948 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2949 ICMPV6CTL_ND6_PRLIST, CTL_EOL);
2950 sysctl_createv(clog, 0, NULL, NULL,
2951 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2952 CTLTYPE_INT, "maxqueuelen",
2953 SYSCTL_DESCR("max packet queue len for a unresolved ND"),
2954 NULL, 1, &nd6_maxqueuelen, 0,
2955 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2956 ICMPV6CTL_ND6_MAXQLEN, CTL_EOL);
2957 }
2958