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