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