icmp6.c revision 1.122 1 /* $NetBSD: icmp6.c,v 1.122 2006/10/12 01:32:38 christos 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.122 2006/10/12 01:32:38 christos 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 __unused)
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 __unused,
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 if (ro.ro_rt) { /* XXX: see comments in icmp6_mtudisc_update */
2070 RTFREE(ro.ro_rt); /* XXX: we could use this */
2071 }
2072 if (src == NULL) {
2073 nd6log((LOG_DEBUG,
2074 "icmp6_reflect: source can't be determined: "
2075 "dst=%s, error=%d\n",
2076 ip6_sprintf(&sin6.sin6_addr), e));
2077 goto bad;
2078 }
2079 }
2080
2081 ip6->ip6_src = *src;
2082 ip6->ip6_flow = 0;
2083 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2084 ip6->ip6_vfc |= IPV6_VERSION;
2085 ip6->ip6_nxt = IPPROTO_ICMPV6;
2086 if (m->m_pkthdr.rcvif) {
2087 /* XXX: This may not be the outgoing interface */
2088 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2089 } else
2090 ip6->ip6_hlim = ip6_defhlim;
2091
2092 m->m_pkthdr.csum_flags = 0;
2093 icmp6->icmp6_cksum = 0;
2094 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2095 sizeof(struct ip6_hdr), plen);
2096
2097 /*
2098 * XXX option handling
2099 */
2100
2101 m->m_flags &= ~(M_BCAST|M_MCAST);
2102
2103 /*
2104 * To avoid a "too big" situation at an intermediate router
2105 * and the path MTU discovery process, specify the IPV6_MINMTU flag.
2106 * Note that only echo and node information replies are affected,
2107 * since the length of ICMP6 errors is limited to the minimum MTU.
2108 */
2109 if (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, &outif) != 0 &&
2110 outif)
2111 icmp6_ifstat_inc(outif, ifs6_out_error);
2112
2113 if (outif)
2114 icmp6_ifoutstat_inc(outif, type, code);
2115
2116 return;
2117
2118 bad:
2119 m_freem(m);
2120 return;
2121 }
2122
2123 static const char *
2124 icmp6_redirect_diag(src6, dst6, tgt6)
2125 struct in6_addr *src6;
2126 struct in6_addr *dst6;
2127 struct in6_addr *tgt6;
2128 {
2129 static char buf[1024];
2130 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2131 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2132 return buf;
2133 }
2134
2135 void
2136 icmp6_redirect_input(m, off)
2137 struct mbuf *m;
2138 int off;
2139 {
2140 struct ifnet *ifp = m->m_pkthdr.rcvif;
2141 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2142 struct nd_redirect *nd_rd;
2143 int icmp6len = ntohs(ip6->ip6_plen);
2144 char *lladdr = NULL;
2145 int lladdrlen = 0;
2146 struct rtentry *rt = NULL;
2147 int is_router;
2148 int is_onlink;
2149 struct in6_addr src6 = ip6->ip6_src;
2150 struct in6_addr redtgt6;
2151 struct in6_addr reddst6;
2152 union nd_opts ndopts;
2153
2154 if (!ifp)
2155 return;
2156
2157 /* XXX if we are router, we don't update route by icmp6 redirect */
2158 if (ip6_forwarding)
2159 goto freeit;
2160 if (!icmp6_rediraccept)
2161 goto freeit;
2162
2163 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2164 if (nd_rd == NULL) {
2165 icmp6stat.icp6s_tooshort++;
2166 return;
2167 }
2168 redtgt6 = nd_rd->nd_rd_target;
2169 reddst6 = nd_rd->nd_rd_dst;
2170
2171 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2172 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2173 goto freeit;
2174 }
2175
2176 /* validation */
2177 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2178 nd6log((LOG_ERR,
2179 "ICMP6 redirect sent from %s rejected; "
2180 "must be from linklocal\n", ip6_sprintf(&src6)));
2181 goto bad;
2182 }
2183 if (ip6->ip6_hlim != 255) {
2184 nd6log((LOG_ERR,
2185 "ICMP6 redirect sent from %s rejected; "
2186 "hlim=%d (must be 255)\n",
2187 ip6_sprintf(&src6), ip6->ip6_hlim));
2188 goto bad;
2189 }
2190 {
2191 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2192 struct sockaddr_in6 sin6;
2193 struct in6_addr *gw6;
2194
2195 bzero(&sin6, sizeof(sin6));
2196 sin6.sin6_family = AF_INET6;
2197 sin6.sin6_len = sizeof(struct sockaddr_in6);
2198 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2199 rt = rtalloc1((struct sockaddr *)&sin6, 0);
2200 if (rt) {
2201 if (rt->rt_gateway == NULL ||
2202 rt->rt_gateway->sa_family != AF_INET6) {
2203 nd6log((LOG_ERR,
2204 "ICMP6 redirect rejected; no route "
2205 "with inet6 gateway found for redirect dst: %s\n",
2206 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2207 RTFREE(rt);
2208 goto bad;
2209 }
2210
2211 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2212 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2213 nd6log((LOG_ERR,
2214 "ICMP6 redirect rejected; "
2215 "not equal to gw-for-src=%s (must be same): "
2216 "%s\n",
2217 ip6_sprintf(gw6),
2218 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2219 RTFREE(rt);
2220 goto bad;
2221 }
2222 } else {
2223 nd6log((LOG_ERR,
2224 "ICMP6 redirect rejected; "
2225 "no route found for redirect dst: %s\n",
2226 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2227 goto bad;
2228 }
2229 RTFREE(rt);
2230 rt = NULL;
2231 }
2232 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2233 nd6log((LOG_ERR,
2234 "ICMP6 redirect rejected; "
2235 "redirect dst must be unicast: %s\n",
2236 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2237 goto bad;
2238 }
2239
2240 is_router = is_onlink = 0;
2241 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2242 is_router = 1; /* router case */
2243 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2244 is_onlink = 1; /* on-link destination case */
2245 if (!is_router && !is_onlink) {
2246 nd6log((LOG_ERR,
2247 "ICMP6 redirect rejected; "
2248 "neither router case nor onlink case: %s\n",
2249 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2250 goto bad;
2251 }
2252 /* validation passed */
2253
2254 icmp6len -= sizeof(*nd_rd);
2255 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2256 if (nd6_options(&ndopts) < 0) {
2257 nd6log((LOG_INFO, "icmp6_redirect_input: "
2258 "invalid ND option, rejected: %s\n",
2259 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2260 /* nd6_options have incremented stats */
2261 goto freeit;
2262 }
2263
2264 if (ndopts.nd_opts_tgt_lladdr) {
2265 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2266 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2267 }
2268
2269 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2270 nd6log((LOG_INFO,
2271 "icmp6_redirect_input: lladdrlen mismatch for %s "
2272 "(if %d, icmp6 packet %d): %s\n",
2273 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2274 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2275 goto bad;
2276 }
2277
2278 /* RFC 2461 8.3 */
2279 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2280 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2281
2282 if (!is_onlink) { /* better router case. perform rtredirect. */
2283 /* perform rtredirect */
2284 struct sockaddr_in6 sdst;
2285 struct sockaddr_in6 sgw;
2286 struct sockaddr_in6 ssrc;
2287 unsigned long rtcount;
2288 struct rtentry *newrt = NULL;
2289
2290 /*
2291 * do not install redirect route, if the number of entries
2292 * is too much (> hiwat). note that, the node (= host) will
2293 * work just fine even if we do not install redirect route
2294 * (there will be additional hops, though).
2295 */
2296 rtcount = rt_timer_count(icmp6_redirect_timeout_q);
2297 if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat)
2298 return;
2299 else if (0 <= icmp6_redirect_lowat &&
2300 rtcount > icmp6_redirect_lowat) {
2301 /*
2302 * XXX nuke a victim, install the new one.
2303 */
2304 }
2305
2306 bzero(&sdst, sizeof(sdst));
2307 bzero(&sgw, sizeof(sgw));
2308 bzero(&ssrc, sizeof(ssrc));
2309 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2310 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2311 sizeof(struct sockaddr_in6);
2312 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2313 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2314 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2315 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2316 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2317 (struct sockaddr *)&ssrc,
2318 &newrt);
2319
2320 if (newrt) {
2321 (void)rt_timer_add(newrt, icmp6_redirect_timeout,
2322 icmp6_redirect_timeout_q);
2323 rtfree(newrt);
2324 }
2325 }
2326 /* finally update cached route in each socket via pfctlinput */
2327 {
2328 struct sockaddr_in6 sdst;
2329
2330 bzero(&sdst, sizeof(sdst));
2331 sdst.sin6_family = AF_INET6;
2332 sdst.sin6_len = sizeof(struct sockaddr_in6);
2333 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2334 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2335 #ifdef IPSEC
2336 key_sa_routechange((struct sockaddr *)&sdst);
2337 #endif
2338 }
2339
2340 freeit:
2341 m_freem(m);
2342 return;
2343
2344 bad:
2345 icmp6stat.icp6s_badredirect++;
2346 m_freem(m);
2347 }
2348
2349 void
2350 icmp6_redirect_output(m0, rt)
2351 struct mbuf *m0;
2352 struct rtentry *rt;
2353 {
2354 struct ifnet *ifp; /* my outgoing interface */
2355 struct in6_addr *ifp_ll6;
2356 struct in6_addr *nexthop;
2357 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2358 struct mbuf *m = NULL; /* newly allocated one */
2359 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2360 struct nd_redirect *nd_rd;
2361 size_t maxlen;
2362 u_char *p;
2363 struct sockaddr_in6 src_sa;
2364
2365 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2366
2367 /* if we are not router, we don't send icmp6 redirect */
2368 if (!ip6_forwarding)
2369 goto fail;
2370
2371 /* sanity check */
2372 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2373 goto fail;
2374
2375 /*
2376 * Address check:
2377 * the source address must identify a neighbor, and
2378 * the destination address must not be a multicast address
2379 * [RFC 2461, sec 8.2]
2380 */
2381 sip6 = mtod(m0, struct ip6_hdr *);
2382 bzero(&src_sa, sizeof(src_sa));
2383 src_sa.sin6_family = AF_INET6;
2384 src_sa.sin6_len = sizeof(src_sa);
2385 src_sa.sin6_addr = sip6->ip6_src;
2386 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2387 goto fail;
2388 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2389 goto fail; /* what should we do here? */
2390
2391 /* rate limit */
2392 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2393 goto fail;
2394
2395 /*
2396 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2397 * we almost always ask for an mbuf cluster for simplicity.
2398 * (MHLEN < IPV6_MMTU is almost always true)
2399 */
2400 #if IPV6_MMTU >= MCLBYTES
2401 # error assumption failed about IPV6_MMTU and MCLBYTES
2402 #endif
2403 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2404 if (m && IPV6_MMTU >= MHLEN)
2405 MCLGET(m, M_DONTWAIT);
2406 if (!m)
2407 goto fail;
2408 m->m_pkthdr.rcvif = NULL;
2409 m->m_len = 0;
2410 maxlen = M_TRAILINGSPACE(m);
2411 maxlen = min(IPV6_MMTU, maxlen);
2412 /* just for safety */
2413 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2414 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2415 goto fail;
2416 }
2417
2418 {
2419 /* get ip6 linklocal address for ifp(my outgoing interface). */
2420 struct in6_ifaddr *ia;
2421 if ((ia = in6ifa_ifpforlinklocal(ifp,
2422 IN6_IFF_NOTREADY|
2423 IN6_IFF_ANYCAST)) == NULL)
2424 goto fail;
2425 ifp_ll6 = &ia->ia_addr.sin6_addr;
2426 }
2427
2428 /* get ip6 linklocal address for the router. */
2429 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2430 struct sockaddr_in6 *sin6;
2431 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2432 nexthop = &sin6->sin6_addr;
2433 if (!IN6_IS_ADDR_LINKLOCAL(nexthop))
2434 nexthop = NULL;
2435 } else
2436 nexthop = NULL;
2437
2438 /* ip6 */
2439 ip6 = mtod(m, struct ip6_hdr *);
2440 ip6->ip6_flow = 0;
2441 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2442 ip6->ip6_vfc |= IPV6_VERSION;
2443 /* ip6->ip6_plen will be set later */
2444 ip6->ip6_nxt = IPPROTO_ICMPV6;
2445 ip6->ip6_hlim = 255;
2446 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2447 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2448 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2449
2450 /* ND Redirect */
2451 nd_rd = (struct nd_redirect *)(ip6 + 1);
2452 nd_rd->nd_rd_type = ND_REDIRECT;
2453 nd_rd->nd_rd_code = 0;
2454 nd_rd->nd_rd_reserved = 0;
2455 if (rt->rt_flags & RTF_GATEWAY) {
2456 /*
2457 * nd_rd->nd_rd_target must be a link-local address in
2458 * better router cases.
2459 */
2460 if (!nexthop)
2461 goto fail;
2462 bcopy(nexthop, &nd_rd->nd_rd_target,
2463 sizeof(nd_rd->nd_rd_target));
2464 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2465 sizeof(nd_rd->nd_rd_dst));
2466 } else {
2467 /* make sure redtgt == reddst */
2468 nexthop = &sip6->ip6_dst;
2469 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2470 sizeof(nd_rd->nd_rd_target));
2471 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2472 sizeof(nd_rd->nd_rd_dst));
2473 }
2474
2475 p = (u_char *)(nd_rd + 1);
2476
2477 {
2478 /* target lladdr option */
2479 struct rtentry *rt_nexthop = NULL;
2480 int len;
2481 struct sockaddr_dl *sdl;
2482 struct nd_opt_hdr *nd_opt;
2483 char *lladdr;
2484
2485 rt_nexthop = nd6_lookup(nexthop, 0, ifp);
2486 if (!rt_nexthop)
2487 goto nolladdropt;
2488 len = sizeof(*nd_opt) + ifp->if_addrlen;
2489 len = (len + 7) & ~7; /* round by 8 */
2490 /* safety check */
2491 if (len + (p - (u_char *)ip6) > maxlen)
2492 goto nolladdropt;
2493 if (!(rt_nexthop->rt_flags & RTF_GATEWAY) &&
2494 (rt_nexthop->rt_flags & RTF_LLINFO) &&
2495 (rt_nexthop->rt_gateway->sa_family == AF_LINK) &&
2496 (sdl = (struct sockaddr_dl *)rt_nexthop->rt_gateway) &&
2497 sdl->sdl_alen) {
2498 nd_opt = (struct nd_opt_hdr *)p;
2499 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2500 nd_opt->nd_opt_len = len >> 3;
2501 lladdr = (char *)(nd_opt + 1);
2502 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2503 p += len;
2504 }
2505 }
2506 nolladdropt:;
2507
2508 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2509
2510 /* just to be safe */
2511 if (m0->m_flags & M_DECRYPTED)
2512 goto noredhdropt;
2513 if (p - (u_char *)ip6 > maxlen)
2514 goto noredhdropt;
2515
2516 {
2517 /* redirected header option */
2518 int len;
2519 struct nd_opt_rd_hdr *nd_opt_rh;
2520
2521 /*
2522 * compute the maximum size for icmp6 redirect header option.
2523 * XXX room for auth header?
2524 */
2525 len = maxlen - (p - (u_char *)ip6);
2526 len &= ~7;
2527
2528 /*
2529 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2530 * about padding/truncate rule for the original IP packet.
2531 * From the discussion on IPv6imp in Feb 1999,
2532 * the consensus was:
2533 * - "attach as much as possible" is the goal
2534 * - pad if not aligned (original size can be guessed by
2535 * original ip6 header)
2536 * Following code adds the padding if it is simple enough,
2537 * and truncates if not.
2538 */
2539 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2540 /* not enough room, truncate */
2541 m_adj(m0, (len - sizeof(*nd_opt_rh)) -
2542 m0->m_pkthdr.len);
2543 } else {
2544 /*
2545 * enough room, truncate if not aligned.
2546 * we don't pad here for simplicity.
2547 */
2548 size_t extra;
2549
2550 extra = m0->m_pkthdr.len % 8;
2551 if (extra) {
2552 /* truncate */
2553 m_adj(m0, -extra);
2554 }
2555 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2556 }
2557
2558 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2559 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2560 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2561 nd_opt_rh->nd_opt_rh_len = len >> 3;
2562 p += sizeof(*nd_opt_rh);
2563 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2564
2565 /* connect m0 to m */
2566 m->m_pkthdr.len += m0->m_pkthdr.len;
2567 m_cat(m, m0);
2568 m0 = NULL;
2569 }
2570 noredhdropt:
2571 if (m0) {
2572 m_freem(m0);
2573 m0 = NULL;
2574 }
2575
2576 /* XXX: clear embedded link IDs in the inner header */
2577 in6_clearscope(&sip6->ip6_src);
2578 in6_clearscope(&sip6->ip6_dst);
2579 in6_clearscope(&nd_rd->nd_rd_target);
2580 in6_clearscope(&nd_rd->nd_rd_dst);
2581
2582 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2583
2584 nd_rd->nd_rd_cksum = 0;
2585 nd_rd->nd_rd_cksum
2586 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2587
2588 /* send the packet to outside... */
2589 if (ip6_output(m, NULL, NULL, 0,
2590 (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL) != 0)
2591 icmp6_ifstat_inc(ifp, ifs6_out_error);
2592
2593 icmp6_ifstat_inc(ifp, ifs6_out_msg);
2594 icmp6_ifstat_inc(ifp, ifs6_out_redirect);
2595 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2596
2597 return;
2598
2599 fail:
2600 if (m)
2601 m_freem(m);
2602 if (m0)
2603 m_freem(m0);
2604 }
2605
2606 /*
2607 * ICMPv6 socket option processing.
2608 */
2609 int
2610 icmp6_ctloutput(op, so, level, optname, mp)
2611 int op;
2612 struct socket *so;
2613 int level, optname;
2614 struct mbuf **mp;
2615 {
2616 int error = 0;
2617 int optlen;
2618 struct in6pcb *in6p = sotoin6pcb(so);
2619 struct mbuf *m = *mp;
2620
2621 optlen = m ? m->m_len : 0;
2622
2623 if (level != IPPROTO_ICMPV6) {
2624 if (op == PRCO_SETOPT && m)
2625 (void)m_free(m);
2626 return EINVAL;
2627 }
2628
2629 switch (op) {
2630 case PRCO_SETOPT:
2631 switch (optname) {
2632 case ICMP6_FILTER:
2633 {
2634 struct icmp6_filter *p;
2635
2636 if (optlen != sizeof(*p)) {
2637 error = EMSGSIZE;
2638 break;
2639 }
2640 p = mtod(m, struct icmp6_filter *);
2641 if (!p || !in6p->in6p_icmp6filt) {
2642 error = EINVAL;
2643 break;
2644 }
2645 bcopy(p, in6p->in6p_icmp6filt,
2646 sizeof(struct icmp6_filter));
2647 error = 0;
2648 break;
2649 }
2650
2651 default:
2652 error = ENOPROTOOPT;
2653 break;
2654 }
2655 if (m)
2656 (void)m_freem(m);
2657 break;
2658
2659 case PRCO_GETOPT:
2660 switch (optname) {
2661 case ICMP6_FILTER:
2662 {
2663 struct icmp6_filter *p;
2664
2665 if (!in6p->in6p_icmp6filt) {
2666 error = EINVAL;
2667 break;
2668 }
2669 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2670 m->m_len = sizeof(struct icmp6_filter);
2671 p = mtod(m, struct icmp6_filter *);
2672 bcopy(in6p->in6p_icmp6filt, p,
2673 sizeof(struct icmp6_filter));
2674 error = 0;
2675 break;
2676 }
2677
2678 default:
2679 error = ENOPROTOOPT;
2680 break;
2681 }
2682 break;
2683 }
2684
2685 return (error);
2686 }
2687
2688 /*
2689 * Perform rate limit check.
2690 * Returns 0 if it is okay to send the icmp6 packet.
2691 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2692 * limitation.
2693 *
2694 * XXX per-destination/type check necessary?
2695 */
2696 static int
2697 icmp6_ratelimit(
2698 const struct in6_addr *dst __unused, /* not used at this moment */
2699 const int type __unused, /* not used at this moment */
2700 const int code __unused) /* not used at this moment */
2701 {
2702 int ret;
2703
2704 ret = 0; /* okay to send */
2705
2706 /* PPS limit */
2707 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2708 icmp6errppslim)) {
2709 /* The packet is subject to rate limit */
2710 ret++;
2711 }
2712
2713 return ret;
2714 }
2715
2716 static struct rtentry *
2717 icmp6_mtudisc_clone(dst)
2718 struct sockaddr *dst;
2719 {
2720 struct rtentry *rt;
2721 int error;
2722
2723 rt = rtalloc1(dst, 1);
2724 if (rt == 0)
2725 return NULL;
2726
2727 /* If we didn't get a host route, allocate one */
2728 if ((rt->rt_flags & RTF_HOST) == 0) {
2729 struct rtentry *nrt;
2730
2731 error = rtrequest((int) RTM_ADD, dst,
2732 (struct sockaddr *) rt->rt_gateway,
2733 (struct sockaddr *) 0,
2734 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
2735 if (error) {
2736 rtfree(rt);
2737 return NULL;
2738 }
2739 nrt->rt_rmx = rt->rt_rmx;
2740 rtfree(rt);
2741 rt = nrt;
2742 }
2743 error = rt_timer_add(rt, icmp6_mtudisc_timeout,
2744 icmp6_mtudisc_timeout_q);
2745 if (error) {
2746 rtfree(rt);
2747 return NULL;
2748 }
2749
2750 return rt; /* caller need to call rtfree() */
2751 }
2752
2753 static void
2754 icmp6_mtudisc_timeout(struct rtentry *rt, struct rttimer *r __unused)
2755 {
2756 if (rt == NULL)
2757 panic("icmp6_mtudisc_timeout: bad route to timeout");
2758 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
2759 (RTF_DYNAMIC | RTF_HOST)) {
2760 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2761 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2762 } else {
2763 if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
2764 rt->rt_rmx.rmx_mtu = 0;
2765 }
2766 }
2767
2768 static void
2769 icmp6_redirect_timeout(struct rtentry *rt, struct rttimer *r __unused)
2770 {
2771 if (rt == NULL)
2772 panic("icmp6_redirect_timeout: bad route to timeout");
2773 if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
2774 (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
2775 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2776 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2777 }
2778 }
2779
2780 /*
2781 * sysctl helper routine for the net.inet6.icmp6.nd6 nodes. silly?
2782 */
2783 static int
2784 sysctl_net_inet6_icmp6_nd6(SYSCTLFN_ARGS)
2785 {
2786 (void)&name;
2787 (void)&l;
2788 (void)&oname;
2789
2790 if (namelen != 0)
2791 return (EINVAL);
2792
2793 return (nd6_sysctl(rnode->sysctl_num, oldp, oldlenp,
2794 /*XXXUNCONST*/
2795 __UNCONST(newp), newlen));
2796 }
2797
2798 SYSCTL_SETUP(sysctl_net_inet6_icmp6_setup,
2799 "sysctl net.inet6.icmp6 subtree setup")
2800 {
2801 extern int nd6_maxqueuelen; /* defined in nd6.c */
2802
2803 sysctl_createv(clog, 0, NULL, NULL,
2804 CTLFLAG_PERMANENT,
2805 CTLTYPE_NODE, "net", NULL,
2806 NULL, 0, NULL, 0,
2807 CTL_NET, CTL_EOL);
2808 sysctl_createv(clog, 0, NULL, NULL,
2809 CTLFLAG_PERMANENT,
2810 CTLTYPE_NODE, "inet6", NULL,
2811 NULL, 0, NULL, 0,
2812 CTL_NET, PF_INET6, CTL_EOL);
2813 sysctl_createv(clog, 0, NULL, NULL,
2814 CTLFLAG_PERMANENT,
2815 CTLTYPE_NODE, "icmp6",
2816 SYSCTL_DESCR("ICMPv6 related settings"),
2817 NULL, 0, NULL, 0,
2818 CTL_NET, PF_INET6, IPPROTO_ICMPV6, CTL_EOL);
2819
2820 sysctl_createv(clog, 0, NULL, NULL,
2821 CTLFLAG_PERMANENT,
2822 CTLTYPE_STRUCT, "stats",
2823 SYSCTL_DESCR("ICMPv6 transmission statistics"),
2824 NULL, 0, &icmp6stat, sizeof(icmp6stat),
2825 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2826 ICMPV6CTL_STATS, CTL_EOL);
2827 sysctl_createv(clog, 0, NULL, NULL,
2828 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2829 CTLTYPE_INT, "rediraccept",
2830 SYSCTL_DESCR("Accept and process redirect messages"),
2831 NULL, 0, &icmp6_rediraccept, 0,
2832 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2833 ICMPV6CTL_REDIRACCEPT, CTL_EOL);
2834 sysctl_createv(clog, 0, NULL, NULL,
2835 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2836 CTLTYPE_INT, "redirtimeout",
2837 SYSCTL_DESCR("Redirect generated route lifetime"),
2838 NULL, 0, &icmp6_redirtimeout, 0,
2839 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2840 ICMPV6CTL_REDIRTIMEOUT, CTL_EOL);
2841 #if 0 /* obsoleted */
2842 sysctl_createv(clog, 0, NULL, NULL,
2843 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2844 CTLTYPE_INT, "errratelimit", NULL,
2845 NULL, 0, &icmp6_errratelimit, 0,
2846 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2847 ICMPV6CTL_ERRRATELIMIT, CTL_EOL);
2848 #endif
2849 sysctl_createv(clog, 0, NULL, NULL,
2850 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2851 CTLTYPE_INT, "nd6_prune",
2852 SYSCTL_DESCR("Neighbor discovery prune interval"),
2853 NULL, 0, &nd6_prune, 0,
2854 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2855 ICMPV6CTL_ND6_PRUNE, CTL_EOL);
2856 sysctl_createv(clog, 0, NULL, NULL,
2857 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2858 CTLTYPE_INT, "nd6_delay",
2859 SYSCTL_DESCR("First probe delay time"),
2860 NULL, 0, &nd6_delay, 0,
2861 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2862 ICMPV6CTL_ND6_DELAY, CTL_EOL);
2863 sysctl_createv(clog, 0, NULL, NULL,
2864 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2865 CTLTYPE_INT, "nd6_umaxtries",
2866 SYSCTL_DESCR("Number of unicast discovery attempts"),
2867 NULL, 0, &nd6_umaxtries, 0,
2868 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2869 ICMPV6CTL_ND6_UMAXTRIES, CTL_EOL);
2870 sysctl_createv(clog, 0, NULL, NULL,
2871 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2872 CTLTYPE_INT, "nd6_mmaxtries",
2873 SYSCTL_DESCR("Number of multicast discovery attempts"),
2874 NULL, 0, &nd6_mmaxtries, 0,
2875 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2876 ICMPV6CTL_ND6_MMAXTRIES, CTL_EOL);
2877 sysctl_createv(clog, 0, NULL, NULL,
2878 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2879 CTLTYPE_INT, "nd6_useloopback",
2880 SYSCTL_DESCR("Use loopback interface for local traffic"),
2881 NULL, 0, &nd6_useloopback, 0,
2882 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2883 ICMPV6CTL_ND6_USELOOPBACK, CTL_EOL);
2884 #if 0 /* obsoleted */
2885 sysctl_createv(clog, 0, NULL, NULL,
2886 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2887 CTLTYPE_INT, "nd6_proxyall", NULL,
2888 NULL, 0, &nd6_proxyall, 0,
2889 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2890 ICMPV6CTL_ND6_PROXYALL, CTL_EOL);
2891 #endif
2892 sysctl_createv(clog, 0, NULL, NULL,
2893 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2894 CTLTYPE_INT, "nodeinfo",
2895 SYSCTL_DESCR("Respond to node information requests"),
2896 NULL, 0, &icmp6_nodeinfo, 0,
2897 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2898 ICMPV6CTL_NODEINFO, CTL_EOL);
2899 sysctl_createv(clog, 0, NULL, NULL,
2900 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2901 CTLTYPE_INT, "errppslimit",
2902 SYSCTL_DESCR("Maximum ICMP errors sent per second"),
2903 NULL, 0, &icmp6errppslim, 0,
2904 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2905 ICMPV6CTL_ERRPPSLIMIT, CTL_EOL);
2906 sysctl_createv(clog, 0, NULL, NULL,
2907 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2908 CTLTYPE_INT, "nd6_maxnudhint",
2909 SYSCTL_DESCR("Maximum neighbor unreachable hint count"),
2910 NULL, 0, &nd6_maxnudhint, 0,
2911 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2912 ICMPV6CTL_ND6_MAXNUDHINT, CTL_EOL);
2913 sysctl_createv(clog, 0, NULL, NULL,
2914 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2915 CTLTYPE_INT, "mtudisc_hiwat",
2916 SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
2917 NULL, 0, &icmp6_mtudisc_hiwat, 0,
2918 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2919 ICMPV6CTL_MTUDISC_HIWAT, CTL_EOL);
2920 sysctl_createv(clog, 0, NULL, NULL,
2921 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2922 CTLTYPE_INT, "mtudisc_lowat",
2923 SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
2924 NULL, 0, &icmp6_mtudisc_lowat, 0,
2925 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2926 ICMPV6CTL_MTUDISC_LOWAT, CTL_EOL);
2927 sysctl_createv(clog, 0, NULL, NULL,
2928 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2929 CTLTYPE_INT, "nd6_debug",
2930 SYSCTL_DESCR("Enable neighbor discovery debug output"),
2931 NULL, 0, &nd6_debug, 0,
2932 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2933 ICMPV6CTL_ND6_DEBUG, CTL_EOL);
2934 sysctl_createv(clog, 0, NULL, NULL,
2935 CTLFLAG_PERMANENT,
2936 CTLTYPE_STRUCT, "nd6_drlist",
2937 SYSCTL_DESCR("Default router list"),
2938 sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
2939 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2940 ICMPV6CTL_ND6_DRLIST, CTL_EOL);
2941 sysctl_createv(clog, 0, NULL, NULL,
2942 CTLFLAG_PERMANENT,
2943 CTLTYPE_STRUCT, "nd6_prlist",
2944 SYSCTL_DESCR("Prefix list"),
2945 sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
2946 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2947 ICMPV6CTL_ND6_PRLIST, CTL_EOL);
2948 sysctl_createv(clog, 0, NULL, NULL,
2949 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2950 CTLTYPE_INT, "maxqueuelen",
2951 SYSCTL_DESCR("max packet queue len for a unresolved ND"),
2952 NULL, 1, &nd6_maxqueuelen, 0,
2953 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2954 ICMPV6CTL_ND6_MAXQLEN, CTL_EOL);
2955 }
2956