icmp6.c revision 1.123.2.2 1 /* $NetBSD: icmp6.c,v 1.123.2.2 2007/05/24 19:13:14 pavel 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.123.2.2 2007/05/24 19:13:14 pavel Exp $");
66
67 #include "opt_inet.h"
68 #include "opt_ipsec.h"
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/time.h>
78 #include <sys/kernel.h>
79 #include <sys/syslog.h>
80 #include <sys/domain.h>
81 #include <sys/sysctl.h>
82
83 #include <net/if.h>
84 #include <net/route.h>
85 #include <net/if_dl.h>
86 #include <net/if_types.h>
87
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/mld6_var.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/nd6.h>
96 #include <netinet6/in6_ifattach.h>
97 #include <netinet6/ip6protosw.h>
98 #include <netinet6/scope6_var.h>
99
100 #ifdef IPSEC
101 #include <netinet6/ipsec.h>
102 #include <netkey/key.h>
103 #endif
104
105 #ifdef FAST_IPSEC
106 #include <netipsec/ipsec.h>
107 #include <netipsec/key.h>
108 #endif
109
110
111 #include "faith.h"
112 #if defined(NFAITH) && 0 < NFAITH
113 #include <net/if_faith.h>
114 #endif
115
116 #include <net/net_osdep.h>
117
118 extern struct domain inet6domain;
119
120 struct icmp6stat icmp6stat;
121
122 extern struct inpcbtable raw6cbtable;
123 extern int icmp6errppslim;
124 static int icmp6errpps_count = 0;
125 static struct timeval icmp6errppslim_last;
126 extern int icmp6_nodeinfo;
127
128 /*
129 * List of callbacks to notify when Path MTU changes are made.
130 */
131 struct icmp6_mtudisc_callback {
132 LIST_ENTRY(icmp6_mtudisc_callback) mc_list;
133 void (*mc_func) __P((struct in6_addr *));
134 };
135
136 LIST_HEAD(, icmp6_mtudisc_callback) icmp6_mtudisc_callbacks =
137 LIST_HEAD_INITIALIZER(&icmp6_mtudisc_callbacks);
138
139 static struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL;
140 extern int pmtu_expire;
141
142 /* XXX do these values make any sense? */
143 static int icmp6_mtudisc_hiwat = 1280;
144 static int icmp6_mtudisc_lowat = 256;
145
146 /*
147 * keep track of # of redirect routes.
148 */
149 static struct rttimer_queue *icmp6_redirect_timeout_q = NULL;
150
151 /* XXX experimental, turned off */
152 static int icmp6_redirect_hiwat = -1;
153 static int icmp6_redirect_lowat = -1;
154
155 static void icmp6_errcount __P((struct icmp6errstat *, int, int));
156 static int icmp6_rip6_input __P((struct mbuf **, int));
157 static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
158 static const char *icmp6_redirect_diag __P((struct in6_addr *,
159 struct in6_addr *, struct in6_addr *));
160 static struct mbuf *ni6_input __P((struct mbuf *, int));
161 static struct mbuf *ni6_nametodns __P((const char *, int, int));
162 static int ni6_dnsmatch __P((const char *, int, const char *, int));
163 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
164 struct ifnet **, char *));
165 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
166 struct ifnet *, int));
167 static int icmp6_notify_error __P((struct mbuf *, int, int, int));
168 static struct rtentry *icmp6_mtudisc_clone __P((struct sockaddr *));
169 static void icmp6_mtudisc_timeout __P((struct rtentry *, struct rttimer *));
170 static void icmp6_redirect_timeout __P((struct rtentry *, struct rttimer *));
171
172
173 void
174 icmp6_init()
175 {
176 mld_init();
177 icmp6_mtudisc_timeout_q = rt_timer_queue_create(pmtu_expire);
178 icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout);
179 }
180
181 static void
182 icmp6_errcount(stat, type, code)
183 struct icmp6errstat *stat;
184 int type, code;
185 {
186 switch (type) {
187 case ICMP6_DST_UNREACH:
188 switch (code) {
189 case ICMP6_DST_UNREACH_NOROUTE:
190 stat->icp6errs_dst_unreach_noroute++;
191 return;
192 case ICMP6_DST_UNREACH_ADMIN:
193 stat->icp6errs_dst_unreach_admin++;
194 return;
195 case ICMP6_DST_UNREACH_BEYONDSCOPE:
196 stat->icp6errs_dst_unreach_beyondscope++;
197 return;
198 case ICMP6_DST_UNREACH_ADDR:
199 stat->icp6errs_dst_unreach_addr++;
200 return;
201 case ICMP6_DST_UNREACH_NOPORT:
202 stat->icp6errs_dst_unreach_noport++;
203 return;
204 }
205 break;
206 case ICMP6_PACKET_TOO_BIG:
207 stat->icp6errs_packet_too_big++;
208 return;
209 case ICMP6_TIME_EXCEEDED:
210 switch (code) {
211 case ICMP6_TIME_EXCEED_TRANSIT:
212 stat->icp6errs_time_exceed_transit++;
213 return;
214 case ICMP6_TIME_EXCEED_REASSEMBLY:
215 stat->icp6errs_time_exceed_reassembly++;
216 return;
217 }
218 break;
219 case ICMP6_PARAM_PROB:
220 switch (code) {
221 case ICMP6_PARAMPROB_HEADER:
222 stat->icp6errs_paramprob_header++;
223 return;
224 case ICMP6_PARAMPROB_NEXTHEADER:
225 stat->icp6errs_paramprob_nextheader++;
226 return;
227 case ICMP6_PARAMPROB_OPTION:
228 stat->icp6errs_paramprob_option++;
229 return;
230 }
231 break;
232 case ND_REDIRECT:
233 stat->icp6errs_redirect++;
234 return;
235 }
236 stat->icp6errs_unknown++;
237 }
238
239 /*
240 * Register a Path MTU Discovery callback.
241 */
242 void
243 icmp6_mtudisc_callback_register(func)
244 void (*func) __P((struct in6_addr *));
245 {
246 struct icmp6_mtudisc_callback *mc;
247
248 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
249 mc = LIST_NEXT(mc, mc_list)) {
250 if (mc->mc_func == func)
251 return;
252 }
253
254 mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
255 if (mc == NULL)
256 panic("icmp6_mtudisc_callback_register");
257
258 mc->mc_func = func;
259 LIST_INSERT_HEAD(&icmp6_mtudisc_callbacks, mc, mc_list);
260 }
261
262 /*
263 * A wrapper function for icmp6_error() necessary when the erroneous packet
264 * may not contain enough scope zone information.
265 */
266 void
267 icmp6_error2(m, type, code, param, ifp)
268 struct mbuf *m;
269 int type, code, param;
270 struct ifnet *ifp;
271 {
272 struct ip6_hdr *ip6;
273
274 if (ifp == NULL)
275 return;
276
277 if (m->m_len < sizeof(struct ip6_hdr)) {
278 m = m_pullup(m, sizeof(struct ip6_hdr));
279 if (m == NULL)
280 return;
281 }
282
283 ip6 = mtod(m, struct ip6_hdr *);
284
285 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
286 return;
287 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
288 return;
289
290 icmp6_error(m, type, code, param);
291 }
292
293 /*
294 * Generate an error packet of type error in response to bad IP6 packet.
295 */
296 void
297 icmp6_error(m, type, code, param)
298 struct mbuf *m;
299 int type, code, param;
300 {
301 struct ip6_hdr *oip6, *nip6;
302 struct icmp6_hdr *icmp6;
303 u_int preplen;
304 int off;
305 int nxt;
306
307 icmp6stat.icp6s_error++;
308
309 /* count per-type-code statistics */
310 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
311
312 if (m->m_flags & M_DECRYPTED) {
313 icmp6stat.icp6s_canterror++;
314 goto freeit;
315 }
316
317 if (m->m_len < sizeof(struct ip6_hdr)) {
318 m = m_pullup(m, sizeof(struct ip6_hdr));
319 if (m == NULL)
320 return;
321 }
322 oip6 = mtod(m, struct ip6_hdr *);
323
324 /*
325 * If the destination address of the erroneous packet is a multicast
326 * address, or the packet was sent using link-layer multicast,
327 * we should basically suppress sending an error (RFC 2463, Section
328 * 2.4).
329 * We have two exceptions (the item e.2 in that section):
330 * - the Pakcet Too Big message can be sent for path MTU discovery.
331 * - the Parameter Problem Message that can be allowed an icmp6 error
332 * in the option type field. This check has been done in
333 * ip6_unknown_opt(), so we can just check the type and code.
334 */
335 if ((m->m_flags & (M_BCAST|M_MCAST) ||
336 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
337 (type != ICMP6_PACKET_TOO_BIG &&
338 (type != ICMP6_PARAM_PROB ||
339 code != ICMP6_PARAMPROB_OPTION)))
340 goto freeit;
341
342 /*
343 * RFC 2463, 2.4 (e.5): source address check.
344 * XXX: the case of anycast source?
345 */
346 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
347 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
348 goto freeit;
349
350 /*
351 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
352 * don't do it.
353 */
354 nxt = -1;
355 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
356 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
357 struct icmp6_hdr *icp;
358
359 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
360 sizeof(*icp));
361 if (icp == NULL) {
362 icmp6stat.icp6s_tooshort++;
363 return;
364 }
365 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
366 icp->icmp6_type == ND_REDIRECT) {
367 /*
368 * ICMPv6 error
369 * Special case: for redirect (which is
370 * informational) we must not send icmp6 error.
371 */
372 icmp6stat.icp6s_canterror++;
373 goto freeit;
374 } else {
375 /* ICMPv6 informational - send the error */
376 }
377 }
378 #if 0 /* controversial */
379 else if (off >= 0 && nxt == IPPROTO_ESP) {
380 /*
381 * It could be ICMPv6 error inside ESP. Take a safer side,
382 * don't respond.
383 */
384 icmp6stat.icp6s_canterror++;
385 goto freeit;
386 }
387 #endif
388 else {
389 /* non-ICMPv6 - send the error */
390 }
391
392 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
393
394 /* Finally, do rate limitation check. */
395 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
396 icmp6stat.icp6s_toofreq++;
397 goto freeit;
398 }
399
400 /*
401 * OK, ICMP6 can be generated.
402 */
403
404 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
405 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
406
407 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
408 M_PREPEND(m, preplen, M_DONTWAIT);
409 if (m && m->m_len < preplen)
410 m = m_pullup(m, preplen);
411 if (m == NULL) {
412 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
413 return;
414 }
415
416 nip6 = mtod(m, struct ip6_hdr *);
417 nip6->ip6_src = oip6->ip6_src;
418 nip6->ip6_dst = oip6->ip6_dst;
419
420 in6_clearscope(&oip6->ip6_src);
421 in6_clearscope(&oip6->ip6_dst);
422
423 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
424 icmp6->icmp6_type = type;
425 icmp6->icmp6_code = code;
426 icmp6->icmp6_pptr = htonl((u_int32_t)param);
427
428 /*
429 * icmp6_reflect() is designed to be in the input path.
430 * icmp6_error() can be called from both input and output path,
431 * and if we are in output path rcvif could contain bogus value.
432 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
433 * information in ip header (nip6).
434 */
435 m->m_pkthdr.rcvif = NULL;
436
437 icmp6stat.icp6s_outhist[type]++;
438 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
439
440 return;
441
442 freeit:
443 /*
444 * If we can't tell whether or not we can generate ICMP6, free it.
445 */
446 m_freem(m);
447 }
448
449 /*
450 * Process a received ICMP6 message.
451 */
452 int
453 icmp6_input(struct mbuf **mp, int *offp, int proto)
454 {
455 struct mbuf *m = *mp, *n;
456 struct ip6_hdr *ip6, *nip6;
457 struct icmp6_hdr *icmp6, *nicmp6;
458 int off = *offp;
459 int icmp6len = m->m_pkthdr.len - *offp;
460 int code, sum, noff;
461
462 #define ICMP6_MAXLEN (sizeof(*nip6) + sizeof(*nicmp6) + 4)
463 KASSERT(ICMP6_MAXLEN < MCLBYTES);
464 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
465
466 /*
467 * Locate icmp6 structure in mbuf, and check
468 * that not corrupted and of at least minimum length
469 */
470
471 ip6 = mtod(m, struct ip6_hdr *);
472 if (icmp6len < sizeof(struct icmp6_hdr)) {
473 icmp6stat.icp6s_tooshort++;
474 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
475 goto freeit;
476 }
477
478 /*
479 * calculate the checksum
480 */
481 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
482 if (icmp6 == NULL) {
483 icmp6stat.icp6s_tooshort++;
484 /* m is invalid */
485 /*icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);*/
486 return IPPROTO_DONE;
487 }
488 KASSERT(IP6_HDR_ALIGNED_P(icmp6));
489 code = icmp6->icmp6_code;
490
491 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
492 nd6log((LOG_ERR,
493 "ICMP6 checksum error(%d|%x) %s\n",
494 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
495 icmp6stat.icp6s_checksum++;
496 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
497 goto freeit;
498 }
499
500 #if defined(NFAITH) && 0 < NFAITH
501 if (faithprefix(&ip6->ip6_dst)) {
502 /*
503 * Deliver very specific ICMP6 type only.
504 * This is important to deliver TOOBIG. Otherwise PMTUD
505 * will not work.
506 */
507 switch (icmp6->icmp6_type) {
508 case ICMP6_DST_UNREACH:
509 case ICMP6_PACKET_TOO_BIG:
510 case ICMP6_TIME_EXCEEDED:
511 break;
512 default:
513 goto freeit;
514 }
515 }
516 #endif
517
518 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
519
520 switch (icmp6->icmp6_type) {
521 case ICMP6_DST_UNREACH:
522 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
523 switch (code) {
524 case ICMP6_DST_UNREACH_NOROUTE:
525 code = PRC_UNREACH_NET;
526 break;
527 case ICMP6_DST_UNREACH_ADMIN:
528 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
529 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
530 break;
531 case ICMP6_DST_UNREACH_ADDR:
532 code = PRC_HOSTDEAD;
533 break;
534 #ifdef COMPAT_RFC1885
535 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
536 code = PRC_UNREACH_SRCFAIL;
537 break;
538 #else
539 case ICMP6_DST_UNREACH_BEYONDSCOPE:
540 /* I mean "source address was incorrect." */
541 code = PRC_UNREACH_NET;
542 break;
543 #endif
544 case ICMP6_DST_UNREACH_NOPORT:
545 code = PRC_UNREACH_PORT;
546 break;
547 default:
548 goto badcode;
549 }
550 goto deliver;
551
552 case ICMP6_PACKET_TOO_BIG:
553 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
554
555 code = PRC_MSGSIZE;
556
557 /*
558 * Updating the path MTU will be done after examining
559 * intermediate extension headers.
560 */
561 goto deliver;
562
563 case ICMP6_TIME_EXCEEDED:
564 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
565 switch (code) {
566 case ICMP6_TIME_EXCEED_TRANSIT:
567 code = PRC_TIMXCEED_INTRANS;
568 break;
569 case ICMP6_TIME_EXCEED_REASSEMBLY:
570 code = PRC_TIMXCEED_REASS;
571 break;
572 default:
573 goto badcode;
574 }
575 goto deliver;
576
577 case ICMP6_PARAM_PROB:
578 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
579 switch (code) {
580 case ICMP6_PARAMPROB_NEXTHEADER:
581 code = PRC_UNREACH_PROTOCOL;
582 break;
583 case ICMP6_PARAMPROB_HEADER:
584 case ICMP6_PARAMPROB_OPTION:
585 code = PRC_PARAMPROB;
586 break;
587 default:
588 goto badcode;
589 }
590 goto deliver;
591
592 case ICMP6_ECHO_REQUEST:
593 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
594 if (code != 0)
595 goto badcode;
596 /*
597 * Copy mbuf to send to two data paths: userland socket(s),
598 * and to the querier (echo reply).
599 * m: a copy for socket, n: a copy for querier
600 *
601 * If the first mbuf is shared, or the first mbuf is too short,
602 * copy the first part of the data into a fresh mbuf.
603 * Otherwise, we will wrongly overwrite both copies.
604 */
605 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
606 /* Give up local */
607 n = m;
608 m = NULL;
609 } else if (M_READONLY(n) ||
610 n->m_len < off + sizeof(struct icmp6_hdr)) {
611 struct mbuf *n0 = n;
612
613 /*
614 * Prepare an internal mbuf. m_pullup() doesn't
615 * always copy the length we specified.
616 */
617 if ((n = m_dup(n0, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
618 /* Give up local */
619 n = m;
620 m = NULL;
621 }
622 m_freem(n0);
623 }
624 IP6_EXTHDR_GET(nicmp6,struct icmp6_hdr *, n, off, sizeof(*nicmp6));
625 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
626 nicmp6->icmp6_code = 0;
627 if (n) {
628 icmp6stat.icp6s_reflect++;
629 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
630 icmp6_reflect(n, off);
631 }
632 if (!m)
633 goto freeit;
634 break;
635
636 case ICMP6_ECHO_REPLY:
637 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
638 if (code != 0)
639 goto badcode;
640 break;
641
642 case MLD_LISTENER_QUERY:
643 case MLD_LISTENER_REPORT:
644 if (icmp6len < sizeof(struct mld_hdr))
645 goto badlen;
646 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
647 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
648 else
649 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
650 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
651 /* give up local */
652 mld_input(m, off);
653 m = NULL;
654 goto freeit;
655 }
656 mld_input(n, off);
657 /* m stays. */
658 break;
659
660 case MLD_LISTENER_DONE:
661 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
662 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
663 goto badlen;
664 break; /* nothing to be done in kernel */
665
666 case MLD_MTRACE_RESP:
667 case MLD_MTRACE:
668 /* XXX: these two are experimental. not officially defined. */
669 /* XXX: per-interface statistics? */
670 break; /* just pass it to applications */
671
672 case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
673 {
674 enum { WRU, FQDN } mode;
675
676 if (!icmp6_nodeinfo)
677 break;
678
679 if (icmp6len == sizeof(struct icmp6_hdr) + 4)
680 mode = WRU;
681 else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
682 mode = FQDN;
683 else
684 goto badlen;
685
686 if (mode == FQDN) {
687 n = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
688 if (n)
689 n = ni6_input(n, off);
690 /* XXX meaningless if n == NULL */
691 noff = sizeof(struct ip6_hdr);
692 } else {
693 u_char *p;
694 int maxhlen;
695
696 if ((icmp6_nodeinfo & 5) != 5)
697 break;
698
699 if (code != 0)
700 goto badcode;
701 MGETHDR(n, M_DONTWAIT, m->m_type);
702 if (n && ICMP6_MAXLEN > MHLEN) {
703 MCLGET(n, M_DONTWAIT);
704 if ((n->m_flags & M_EXT) == 0) {
705 m_free(n);
706 n = NULL;
707 }
708 }
709 if (n == NULL) {
710 /* Give up remote */
711 break;
712 }
713 n->m_pkthdr.rcvif = NULL;
714 n->m_len = 0;
715 maxhlen = M_TRAILINGSPACE(n) - ICMP6_MAXLEN;
716 if (maxhlen > hostnamelen)
717 maxhlen = hostnamelen;
718 /*
719 * Copy IPv6 and ICMPv6 only.
720 */
721 nip6 = mtod(n, struct ip6_hdr *);
722 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
723 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
724 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
725 p = (u_char *)(nicmp6 + 1);
726 bzero(p, 4);
727 bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
728 noff = sizeof(struct ip6_hdr);
729 M_COPY_PKTHDR(n, m); /* just for rcvif */
730 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
731 sizeof(struct icmp6_hdr) + 4 + maxhlen;
732 nicmp6->icmp6_type = ICMP6_WRUREPLY;
733 nicmp6->icmp6_code = 0;
734 }
735 #undef hostnamelen
736 if (n) {
737 icmp6stat.icp6s_reflect++;
738 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
739 icmp6_reflect(n, noff);
740 }
741 break;
742 }
743
744 case ICMP6_WRUREPLY:
745 if (code != 0)
746 goto badcode;
747 break;
748
749 case ND_ROUTER_SOLICIT:
750 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
751 if (code != 0)
752 goto badcode;
753 if (icmp6len < sizeof(struct nd_router_solicit))
754 goto badlen;
755 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
756 /* give up local */
757 nd6_rs_input(m, off, icmp6len);
758 m = NULL;
759 goto freeit;
760 }
761 nd6_rs_input(n, off, icmp6len);
762 /* m stays. */
763 break;
764
765 case ND_ROUTER_ADVERT:
766 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
767 if (code != 0)
768 goto badcode;
769 if (icmp6len < sizeof(struct nd_router_advert))
770 goto badlen;
771 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
772 /* give up local */
773 nd6_ra_input(m, off, icmp6len);
774 m = NULL;
775 goto freeit;
776 }
777 nd6_ra_input(n, off, icmp6len);
778 /* m stays. */
779 break;
780
781 case ND_NEIGHBOR_SOLICIT:
782 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
783 if (code != 0)
784 goto badcode;
785 if (icmp6len < sizeof(struct nd_neighbor_solicit))
786 goto badlen;
787 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
788 /* give up local */
789 nd6_ns_input(m, off, icmp6len);
790 m = NULL;
791 goto freeit;
792 }
793 nd6_ns_input(n, off, icmp6len);
794 /* m stays. */
795 break;
796
797 case ND_NEIGHBOR_ADVERT:
798 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
799 if (code != 0)
800 goto badcode;
801 if (icmp6len < sizeof(struct nd_neighbor_advert))
802 goto badlen;
803 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
804 /* give up local */
805 nd6_na_input(m, off, icmp6len);
806 m = NULL;
807 goto freeit;
808 }
809 nd6_na_input(n, off, icmp6len);
810 /* m stays. */
811 break;
812
813 case ND_REDIRECT:
814 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
815 if (code != 0)
816 goto badcode;
817 if (icmp6len < sizeof(struct nd_redirect))
818 goto badlen;
819 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
820 /* give up local */
821 icmp6_redirect_input(m, off);
822 m = NULL;
823 goto freeit;
824 }
825 icmp6_redirect_input(n, off);
826 /* m stays. */
827 break;
828
829 case ICMP6_ROUTER_RENUMBERING:
830 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
831 code != ICMP6_ROUTER_RENUMBERING_RESULT)
832 goto badcode;
833 if (icmp6len < sizeof(struct icmp6_router_renum))
834 goto badlen;
835 break;
836
837 default:
838 nd6log((LOG_DEBUG,
839 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
840 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
841 ip6_sprintf(&ip6->ip6_dst),
842 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
843 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
844 /* ICMPv6 error: MUST deliver it by spec... */
845 code = PRC_NCMDS;
846 /* deliver */
847 } else {
848 /* ICMPv6 informational: MUST not deliver */
849 break;
850 }
851 deliver:
852 if (icmp6_notify_error(m, off, icmp6len, code)) {
853 /* In this case, m should've been freed. */
854 return (IPPROTO_DONE);
855 }
856 break;
857
858 badcode:
859 icmp6stat.icp6s_badcode++;
860 break;
861
862 badlen:
863 icmp6stat.icp6s_badlen++;
864 break;
865 }
866
867 /* deliver the packet to appropriate sockets */
868 icmp6_rip6_input(&m, *offp);
869
870 return IPPROTO_DONE;
871
872 freeit:
873 m_freem(m);
874 return IPPROTO_DONE;
875 }
876
877 static int
878 icmp6_notify_error(m, off, icmp6len, code)
879 struct mbuf *m;
880 int off, icmp6len, code;
881 {
882 struct icmp6_hdr *icmp6;
883 struct ip6_hdr *eip6;
884 u_int32_t notifymtu;
885 struct sockaddr_in6 icmp6src, icmp6dst;
886
887 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
888 icmp6stat.icp6s_tooshort++;
889 goto freeit;
890 }
891 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
892 sizeof(*icmp6) + sizeof(struct ip6_hdr));
893 if (icmp6 == NULL) {
894 icmp6stat.icp6s_tooshort++;
895 return (-1);
896 }
897 eip6 = (struct ip6_hdr *)(icmp6 + 1);
898
899 /* Detect the upper level protocol */
900 {
901 void (*ctlfunc) __P((int, struct sockaddr *, void *));
902 u_int8_t nxt = eip6->ip6_nxt;
903 int eoff = off + sizeof(struct icmp6_hdr) +
904 sizeof(struct ip6_hdr);
905 struct ip6ctlparam ip6cp;
906 struct in6_addr *finaldst = NULL;
907 int icmp6type = icmp6->icmp6_type;
908 struct ip6_frag *fh;
909 struct ip6_rthdr *rth;
910 struct ip6_rthdr0 *rth0;
911 int rthlen;
912
913 while (1) { /* XXX: should avoid infinite loop explicitly? */
914 struct ip6_ext *eh;
915
916 switch (nxt) {
917 case IPPROTO_HOPOPTS:
918 case IPPROTO_DSTOPTS:
919 case IPPROTO_AH:
920 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
921 eoff, sizeof(*eh));
922 if (eh == NULL) {
923 icmp6stat.icp6s_tooshort++;
924 return (-1);
925 }
926
927 if (nxt == IPPROTO_AH)
928 eoff += (eh->ip6e_len + 2) << 2;
929 else
930 eoff += (eh->ip6e_len + 1) << 3;
931 nxt = eh->ip6e_nxt;
932 break;
933 case IPPROTO_ROUTING:
934 /*
935 * When the erroneous packet contains a
936 * routing header, we should examine the
937 * header to determine the final destination.
938 * Otherwise, we can't properly update
939 * information that depends on the final
940 * destination (e.g. path MTU).
941 */
942 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
943 eoff, sizeof(*rth));
944 if (rth == NULL) {
945 icmp6stat.icp6s_tooshort++;
946 return (-1);
947 }
948 rthlen = (rth->ip6r_len + 1) << 3;
949 /*
950 * XXX: currently there is no
951 * officially defined type other
952 * than type-0.
953 * Note that if the segment left field
954 * is 0, all intermediate hops must
955 * have been passed.
956 */
957 if (rth->ip6r_segleft &&
958 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
959 int hops;
960
961 IP6_EXTHDR_GET(rth0,
962 struct ip6_rthdr0 *, m,
963 eoff, rthlen);
964 if (rth0 == NULL) {
965 icmp6stat.icp6s_tooshort++;
966 return (-1);
967 }
968 /* just ignore a bogus header */
969 if ((rth0->ip6r0_len % 2) == 0 &&
970 (hops = rth0->ip6r0_len/2))
971 finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
972 }
973 eoff += rthlen;
974 nxt = rth->ip6r_nxt;
975 break;
976 case IPPROTO_FRAGMENT:
977 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
978 eoff, sizeof(*fh));
979 if (fh == NULL) {
980 icmp6stat.icp6s_tooshort++;
981 return (-1);
982 }
983 /*
984 * Data after a fragment header is meaningless
985 * unless it is the first fragment, but
986 * we'll go to the notify label for path MTU
987 * discovery.
988 */
989 if (fh->ip6f_offlg & IP6F_OFF_MASK)
990 goto notify;
991
992 eoff += sizeof(struct ip6_frag);
993 nxt = fh->ip6f_nxt;
994 break;
995 default:
996 /*
997 * This case includes ESP and the No Next
998 * Header. In such cases going to the notify
999 * label does not have any meaning
1000 * (i.e. ctlfunc will be NULL), but we go
1001 * anyway since we might have to update
1002 * path MTU information.
1003 */
1004 goto notify;
1005 }
1006 }
1007 notify:
1008 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1009 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1010 if (icmp6 == NULL) {
1011 icmp6stat.icp6s_tooshort++;
1012 return (-1);
1013 }
1014
1015 /*
1016 * retrieve parameters from the inner IPv6 header, and convert
1017 * them into sockaddr structures.
1018 * XXX: there is no guarantee that the source or destination
1019 * addresses of the inner packet are in the same scope zone as
1020 * the addresses of the icmp packet. But there is no other
1021 * way to determine the zone.
1022 */
1023 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1024
1025 bzero(&icmp6dst, sizeof(icmp6dst));
1026 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1027 icmp6dst.sin6_family = AF_INET6;
1028 if (finaldst == NULL)
1029 icmp6dst.sin6_addr = eip6->ip6_dst;
1030 else
1031 icmp6dst.sin6_addr = *finaldst;
1032 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1033 goto freeit;
1034 bzero(&icmp6src, sizeof(icmp6src));
1035 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1036 icmp6src.sin6_family = AF_INET6;
1037 icmp6src.sin6_addr = eip6->ip6_src;
1038 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1039 goto freeit;
1040 icmp6src.sin6_flowinfo =
1041 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1042
1043 if (finaldst == NULL)
1044 finaldst = &eip6->ip6_dst;
1045 ip6cp.ip6c_m = m;
1046 ip6cp.ip6c_icmp6 = icmp6;
1047 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1048 ip6cp.ip6c_off = eoff;
1049 ip6cp.ip6c_finaldst = finaldst;
1050 ip6cp.ip6c_src = &icmp6src;
1051 ip6cp.ip6c_nxt = nxt;
1052
1053 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1054 notifymtu = ntohl(icmp6->icmp6_mtu);
1055 ip6cp.ip6c_cmdarg = (void *)¬ifymtu;
1056 }
1057
1058 ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
1059 (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1060 if (ctlfunc) {
1061 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1062 &ip6cp);
1063 }
1064 }
1065 return (0);
1066
1067 freeit:
1068 m_freem(m);
1069 return (-1);
1070 }
1071
1072 void
1073 icmp6_mtudisc_update(ip6cp, validated)
1074 struct ip6ctlparam *ip6cp;
1075 int validated;
1076 {
1077 unsigned long rtcount;
1078 struct icmp6_mtudisc_callback *mc;
1079 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1080 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1081 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1082 u_int mtu = ntohl(icmp6->icmp6_mtu);
1083 struct rtentry *rt = NULL;
1084 struct sockaddr_in6 sin6;
1085
1086 /*
1087 * allow non-validated cases if memory is plenty, to make traffic
1088 * from non-connected pcb happy.
1089 */
1090 rtcount = rt_timer_count(icmp6_mtudisc_timeout_q);
1091 if (validated) {
1092 if (0 <= icmp6_mtudisc_hiwat && rtcount > icmp6_mtudisc_hiwat)
1093 return;
1094 else if (0 <= icmp6_mtudisc_lowat &&
1095 rtcount > icmp6_mtudisc_lowat) {
1096 /*
1097 * XXX nuke a victim, install the new one.
1098 */
1099 }
1100 } else {
1101 if (0 <= icmp6_mtudisc_lowat && rtcount > icmp6_mtudisc_lowat)
1102 return;
1103 }
1104
1105 bzero(&sin6, sizeof(sin6));
1106 sin6.sin6_family = PF_INET6;
1107 sin6.sin6_len = sizeof(struct sockaddr_in6);
1108 sin6.sin6_addr = *dst;
1109 if (in6_setscope(&sin6.sin6_addr, m->m_pkthdr.rcvif, NULL))
1110 return;
1111
1112 rt = icmp6_mtudisc_clone((struct sockaddr *)&sin6);
1113
1114 if (rt && (rt->rt_flags & RTF_HOST) &&
1115 !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
1116 (rt->rt_rmx.rmx_mtu > mtu || rt->rt_rmx.rmx_mtu == 0)) {
1117 if (mtu < IN6_LINKMTU(rt->rt_ifp)) {
1118 icmp6stat.icp6s_pmtuchg++;
1119 rt->rt_rmx.rmx_mtu = mtu;
1120 }
1121 }
1122 if (rt) { /* XXX: need braces to avoid conflict with else in RTFREE. */
1123 RTFREE(rt);
1124 }
1125
1126 /*
1127 * Notify protocols that the MTU for this destination
1128 * has changed.
1129 */
1130 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
1131 mc = LIST_NEXT(mc, mc_list))
1132 (*mc->mc_func)(&sin6.sin6_addr);
1133 }
1134
1135 /*
1136 * Process a Node Information Query packet, based on
1137 * draft-ietf-ipngwg-icmp-name-lookups-07.
1138 *
1139 * Spec incompatibilities:
1140 * - IPv6 Subject address handling
1141 * - IPv4 Subject address handling support missing
1142 * - Proxy reply (answer even if it's not for me)
1143 * - joins NI group address at in6_ifattach() time only, does not cope
1144 * with hostname changes by sethostname(3)
1145 */
1146 #ifndef offsetof /* XXX */
1147 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
1148 #endif
1149 static struct mbuf *
1150 ni6_input(m, off)
1151 struct mbuf *m;
1152 int off;
1153 {
1154 struct icmp6_nodeinfo *ni6, *nni6;
1155 struct mbuf *n = NULL;
1156 u_int16_t qtype;
1157 int subjlen;
1158 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1159 struct ni_reply_fqdn *fqdn;
1160 int addrs; /* for NI_QTYPE_NODEADDR */
1161 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1162 struct sockaddr_in6 sin6; /* ip6_dst */
1163 struct in6_addr in6_subj; /* subject address */
1164 struct ip6_hdr *ip6;
1165 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1166 char *subj = NULL;
1167
1168 ip6 = mtod(m, struct ip6_hdr *);
1169 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1170 if (ni6 == NULL) {
1171 /* m is already reclaimed */
1172 return NULL;
1173 }
1174
1175 /*
1176 * Validate IPv6 destination address.
1177 *
1178 * The Responder must discard the Query without further processing
1179 * unless it is one of the Responder's unicast or anycast addresses, or
1180 * a link-local scope multicast address which the Responder has joined.
1181 * [icmp-name-lookups-07, Section 4.]
1182 */
1183 bzero(&sin6, sizeof(sin6));
1184 sin6.sin6_family = AF_INET6;
1185 sin6.sin6_len = sizeof(struct sockaddr_in6);
1186 bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1187 /* XXX scopeid */
1188 if (ifa_ifwithaddr((struct sockaddr *)&sin6))
1189 ; /* unicast/anycast, fine */
1190 else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1191 ; /* link-local multicast, fine */
1192 else
1193 goto bad;
1194
1195 /* validate query Subject field. */
1196 qtype = ntohs(ni6->ni_qtype);
1197 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1198 switch (qtype) {
1199 case NI_QTYPE_NOOP:
1200 case NI_QTYPE_SUPTYPES:
1201 /* 07 draft */
1202 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1203 break;
1204 /* FALLTHROUGH */
1205 case NI_QTYPE_FQDN:
1206 case NI_QTYPE_NODEADDR:
1207 case NI_QTYPE_IPV4ADDR:
1208 switch (ni6->ni_code) {
1209 case ICMP6_NI_SUBJ_IPV6:
1210 #if ICMP6_NI_SUBJ_IPV6 != 0
1211 case 0:
1212 #endif
1213 /*
1214 * backward compatibility - try to accept 03 draft
1215 * format, where no Subject is present.
1216 */
1217 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1218 subjlen == 0) {
1219 oldfqdn++;
1220 break;
1221 }
1222 #if ICMP6_NI_SUBJ_IPV6 != 0
1223 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1224 goto bad;
1225 #endif
1226
1227 if (subjlen != sizeof(sin6.sin6_addr))
1228 goto bad;
1229
1230 /*
1231 * Validate Subject address.
1232 *
1233 * Not sure what exactly "address belongs to the node"
1234 * means in the spec, is it just unicast, or what?
1235 *
1236 * At this moment we consider Subject address as
1237 * "belong to the node" if the Subject address equals
1238 * to the IPv6 destination address; validation for
1239 * IPv6 destination address should have done enough
1240 * check for us.
1241 *
1242 * We do not do proxy at this moment.
1243 */
1244 /* m_pulldown instead of copy? */
1245 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1246 subjlen, (caddr_t)&in6_subj);
1247 if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1248 goto bad;
1249
1250 subj = (char *)&in6_subj;
1251 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1252 break;
1253
1254 /*
1255 * XXX if we are to allow other cases, we should really
1256 * be careful about scope here.
1257 * basically, we should disallow queries toward IPv6
1258 * destination X with subject Y, if scope(X) > scope(Y).
1259 * if we allow scope(X) > scope(Y), it will result in
1260 * information leakage across scope boundary.
1261 */
1262 goto bad;
1263
1264 case ICMP6_NI_SUBJ_FQDN:
1265 /*
1266 * Validate Subject name with gethostname(3).
1267 *
1268 * The behavior may need some debate, since:
1269 * - we are not sure if the node has FQDN as
1270 * hostname (returned by gethostname(3)).
1271 * - the code does wildcard match for truncated names.
1272 * however, we are not sure if we want to perform
1273 * wildcard match, if gethostname(3) side has
1274 * truncated hostname.
1275 */
1276 n = ni6_nametodns(hostname, hostnamelen, 0);
1277 if (!n || n->m_next || n->m_len == 0)
1278 goto bad;
1279 IP6_EXTHDR_GET(subj, char *, m,
1280 off + sizeof(struct icmp6_nodeinfo), subjlen);
1281 if (subj == NULL)
1282 goto bad;
1283 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1284 n->m_len)) {
1285 goto bad;
1286 }
1287 m_freem(n);
1288 n = NULL;
1289 break;
1290
1291 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1292 default:
1293 goto bad;
1294 }
1295 break;
1296 }
1297
1298 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1299 switch (qtype) {
1300 case NI_QTYPE_FQDN:
1301 if ((icmp6_nodeinfo & 1) == 0)
1302 goto bad;
1303 break;
1304 case NI_QTYPE_NODEADDR:
1305 case NI_QTYPE_IPV4ADDR:
1306 if ((icmp6_nodeinfo & 2) == 0)
1307 goto bad;
1308 break;
1309 }
1310
1311 /* guess reply length */
1312 switch (qtype) {
1313 case NI_QTYPE_NOOP:
1314 break; /* no reply data */
1315 case NI_QTYPE_SUPTYPES:
1316 replylen += sizeof(u_int32_t);
1317 break;
1318 case NI_QTYPE_FQDN:
1319 /* XXX will append an mbuf */
1320 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1321 break;
1322 case NI_QTYPE_NODEADDR:
1323 addrs = ni6_addrs(ni6, m, &ifp, subj);
1324 if ((replylen += addrs * (sizeof(struct in6_addr) +
1325 sizeof(u_int32_t))) > MCLBYTES)
1326 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1327 break;
1328 case NI_QTYPE_IPV4ADDR:
1329 /* unsupported - should respond with unknown Qtype? */
1330 goto bad;
1331 default:
1332 /*
1333 * XXX: We must return a reply with the ICMP6 code
1334 * `unknown Qtype' in this case. However we regard the case
1335 * as an FQDN query for backward compatibility.
1336 * Older versions set a random value to this field,
1337 * so it rarely varies in the defined qtypes.
1338 * But the mechanism is not reliable...
1339 * maybe we should obsolete older versions.
1340 */
1341 qtype = NI_QTYPE_FQDN;
1342 /* XXX will append an mbuf */
1343 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1344 oldfqdn++;
1345 break;
1346 }
1347
1348 /* allocate an mbuf to reply. */
1349 MGETHDR(n, M_DONTWAIT, m->m_type);
1350 if (n == NULL) {
1351 m_freem(m);
1352 return (NULL);
1353 }
1354 M_MOVE_PKTHDR(n, m); /* just for rcvif */
1355 if (replylen > MHLEN) {
1356 if (replylen > MCLBYTES) {
1357 /*
1358 * XXX: should we try to allocate more? But MCLBYTES
1359 * is probably much larger than IPV6_MMTU...
1360 */
1361 goto bad;
1362 }
1363 MCLGET(n, M_DONTWAIT);
1364 if ((n->m_flags & M_EXT) == 0) {
1365 goto bad;
1366 }
1367 }
1368 n->m_pkthdr.len = n->m_len = replylen;
1369
1370 /* copy mbuf header and IPv6 + Node Information base headers */
1371 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1372 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1373 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1374
1375 /* qtype dependent procedure */
1376 switch (qtype) {
1377 case NI_QTYPE_NOOP:
1378 nni6->ni_code = ICMP6_NI_SUCCESS;
1379 nni6->ni_flags = 0;
1380 break;
1381 case NI_QTYPE_SUPTYPES:
1382 {
1383 u_int32_t v;
1384 nni6->ni_code = ICMP6_NI_SUCCESS;
1385 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1386 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1387 v = (u_int32_t)htonl(0x0000000f);
1388 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1389 break;
1390 }
1391 case NI_QTYPE_FQDN:
1392 nni6->ni_code = ICMP6_NI_SUCCESS;
1393 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1394 sizeof(struct ip6_hdr) +
1395 sizeof(struct icmp6_nodeinfo));
1396 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1397 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1398 /*
1399 * XXX do we really have FQDN in variable "hostname"?
1400 */
1401 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1402 if (n->m_next == NULL)
1403 goto bad;
1404 /* XXX we assume that n->m_next is not a chain */
1405 if (n->m_next->m_next != NULL)
1406 goto bad;
1407 n->m_pkthdr.len += n->m_next->m_len;
1408 break;
1409 case NI_QTYPE_NODEADDR:
1410 {
1411 int lenlim, copied;
1412
1413 nni6->ni_code = ICMP6_NI_SUCCESS;
1414 n->m_pkthdr.len = n->m_len =
1415 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1416 lenlim = M_TRAILINGSPACE(n);
1417 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1418 /* XXX: reset mbuf length */
1419 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1420 sizeof(struct icmp6_nodeinfo) + copied;
1421 break;
1422 }
1423 default:
1424 break; /* XXX impossible! */
1425 }
1426
1427 nni6->ni_type = ICMP6_NI_REPLY;
1428 m_freem(m);
1429 return (n);
1430
1431 bad:
1432 m_freem(m);
1433 if (n)
1434 m_freem(n);
1435 return (NULL);
1436 }
1437 #undef hostnamelen
1438
1439 #define isupper(x) ('A' <= (x) && (x) <= 'Z')
1440 #define isalpha(x) (('A' <= (x) && (x) <= 'Z') || ('a' <= (x) && (x) <= 'z'))
1441 #define isalnum(x) (isalpha(x) || ('0' <= (x) && (x) <= '9'))
1442 #define tolower(x) (isupper(x) ? (x) + 'a' - 'A' : (x))
1443
1444 /*
1445 * make a mbuf with DNS-encoded string. no compression support.
1446 *
1447 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1448 * treated as truncated name (two \0 at the end). this is a wild guess.
1449 */
1450 static struct mbuf *
1451 ni6_nametodns(name, namelen, old)
1452 const char *name;
1453 int namelen;
1454 int old; /* return pascal string if non-zero */
1455 {
1456 struct mbuf *m;
1457 char *cp, *ep;
1458 const char *p, *q;
1459 int i, len, nterm;
1460
1461 if (old)
1462 len = namelen + 1;
1463 else
1464 len = MCLBYTES;
1465
1466 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1467 MGET(m, M_DONTWAIT, MT_DATA);
1468 if (m && len > MLEN) {
1469 MCLGET(m, M_DONTWAIT);
1470 if ((m->m_flags & M_EXT) == 0)
1471 goto fail;
1472 }
1473 if (!m)
1474 goto fail;
1475 m->m_next = NULL;
1476
1477 if (old) {
1478 m->m_len = len;
1479 *mtod(m, char *) = namelen;
1480 bcopy(name, mtod(m, char *) + 1, namelen);
1481 return m;
1482 } else {
1483 m->m_len = 0;
1484 cp = mtod(m, char *);
1485 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1486
1487 /* if not certain about my name, return empty buffer */
1488 if (namelen == 0)
1489 return m;
1490
1491 /*
1492 * guess if it looks like shortened hostname, or FQDN.
1493 * shortened hostname needs two trailing "\0".
1494 */
1495 i = 0;
1496 for (p = name; p < name + namelen; p++) {
1497 if (*p && *p == '.')
1498 i++;
1499 }
1500 if (i < 2)
1501 nterm = 2;
1502 else
1503 nterm = 1;
1504
1505 p = name;
1506 while (cp < ep && p < name + namelen) {
1507 i = 0;
1508 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1509 i++;
1510 /* result does not fit into mbuf */
1511 if (cp + i + 1 >= ep)
1512 goto fail;
1513 /*
1514 * DNS label length restriction, RFC1035 page 8.
1515 * "i == 0" case is included here to avoid returning
1516 * 0-length label on "foo..bar".
1517 */
1518 if (i <= 0 || i >= 64)
1519 goto fail;
1520 *cp++ = i;
1521 if (!isalpha(p[0]) || !isalnum(p[i - 1]))
1522 goto fail;
1523 while (i > 0) {
1524 if (!isalnum(*p) && *p != '-')
1525 goto fail;
1526 if (isupper(*p)) {
1527 *cp++ = tolower(*p);
1528 p++;
1529 } else
1530 *cp++ = *p++;
1531 i--;
1532 }
1533 p = q;
1534 if (p < name + namelen && *p == '.')
1535 p++;
1536 }
1537 /* termination */
1538 if (cp + nterm >= ep)
1539 goto fail;
1540 while (nterm-- > 0)
1541 *cp++ = '\0';
1542 m->m_len = cp - mtod(m, char *);
1543 return m;
1544 }
1545
1546 panic("should not reach here");
1547 /* NOTREACHED */
1548
1549 fail:
1550 if (m)
1551 m_freem(m);
1552 return NULL;
1553 }
1554
1555 /*
1556 * check if two DNS-encoded string matches. takes care of truncated
1557 * form (with \0\0 at the end). no compression support.
1558 * XXX upper/lowercase match (see RFC2065)
1559 */
1560 static int
1561 ni6_dnsmatch(a, alen, b, blen)
1562 const char *a;
1563 int alen;
1564 const char *b;
1565 int blen;
1566 {
1567 const char *a0, *b0;
1568 int l;
1569
1570 /* simplest case - need validation? */
1571 if (alen == blen && bcmp(a, b, alen) == 0)
1572 return 1;
1573
1574 a0 = a;
1575 b0 = b;
1576
1577 /* termination is mandatory */
1578 if (alen < 2 || blen < 2)
1579 return 0;
1580 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1581 return 0;
1582 alen--;
1583 blen--;
1584
1585 while (a - a0 < alen && b - b0 < blen) {
1586 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1587 return 0;
1588
1589 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1590 return 0;
1591 /* we don't support compression yet */
1592 if (a[0] >= 64 || b[0] >= 64)
1593 return 0;
1594
1595 /* truncated case */
1596 if (a[0] == 0 && a - a0 == alen - 1)
1597 return 1;
1598 if (b[0] == 0 && b - b0 == blen - 1)
1599 return 1;
1600 if (a[0] == 0 || b[0] == 0)
1601 return 0;
1602
1603 if (a[0] != b[0])
1604 return 0;
1605 l = a[0];
1606 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1607 return 0;
1608 if (bcmp(a + 1, b + 1, l) != 0)
1609 return 0;
1610
1611 a += 1 + l;
1612 b += 1 + l;
1613 }
1614
1615 if (a - a0 == alen && b - b0 == blen)
1616 return 1;
1617 else
1618 return 0;
1619 }
1620
1621 /*
1622 * calculate the number of addresses to be returned in the node info reply.
1623 */
1624 static int
1625 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m,
1626 struct ifnet **ifpp, 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 #if defined(IPSEC) || defined(FAST_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(
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(struct rtentry *rt, struct rttimer *r)
2760 {
2761 if (rt == NULL)
2762 panic("icmp6_mtudisc_timeout: bad route to timeout");
2763 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
2764 (RTF_DYNAMIC | RTF_HOST)) {
2765 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2766 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2767 } else {
2768 if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
2769 rt->rt_rmx.rmx_mtu = 0;
2770 }
2771 }
2772
2773 static void
2774 icmp6_redirect_timeout(struct rtentry *rt, struct rttimer *r)
2775 {
2776 if (rt == NULL)
2777 panic("icmp6_redirect_timeout: bad route to timeout");
2778 if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
2779 (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
2780 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2781 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2782 }
2783 }
2784
2785 /*
2786 * sysctl helper routine for the net.inet6.icmp6.nd6 nodes. silly?
2787 */
2788 static int
2789 sysctl_net_inet6_icmp6_nd6(SYSCTLFN_ARGS)
2790 {
2791 (void)&name;
2792 (void)&l;
2793 (void)&oname;
2794
2795 if (namelen != 0)
2796 return (EINVAL);
2797
2798 return (nd6_sysctl(rnode->sysctl_num, oldp, oldlenp,
2799 /*XXXUNCONST*/
2800 __UNCONST(newp), newlen));
2801 }
2802
2803 SYSCTL_SETUP(sysctl_net_inet6_icmp6_setup,
2804 "sysctl net.inet6.icmp6 subtree setup")
2805 {
2806 extern int nd6_maxqueuelen; /* defined in nd6.c */
2807
2808 sysctl_createv(clog, 0, NULL, NULL,
2809 CTLFLAG_PERMANENT,
2810 CTLTYPE_NODE, "net", NULL,
2811 NULL, 0, NULL, 0,
2812 CTL_NET, CTL_EOL);
2813 sysctl_createv(clog, 0, NULL, NULL,
2814 CTLFLAG_PERMANENT,
2815 CTLTYPE_NODE, "inet6", NULL,
2816 NULL, 0, NULL, 0,
2817 CTL_NET, PF_INET6, CTL_EOL);
2818 sysctl_createv(clog, 0, NULL, NULL,
2819 CTLFLAG_PERMANENT,
2820 CTLTYPE_NODE, "icmp6",
2821 SYSCTL_DESCR("ICMPv6 related settings"),
2822 NULL, 0, NULL, 0,
2823 CTL_NET, PF_INET6, IPPROTO_ICMPV6, CTL_EOL);
2824
2825 sysctl_createv(clog, 0, NULL, NULL,
2826 CTLFLAG_PERMANENT,
2827 CTLTYPE_STRUCT, "stats",
2828 SYSCTL_DESCR("ICMPv6 transmission statistics"),
2829 NULL, 0, &icmp6stat, sizeof(icmp6stat),
2830 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2831 ICMPV6CTL_STATS, CTL_EOL);
2832 sysctl_createv(clog, 0, NULL, NULL,
2833 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2834 CTLTYPE_INT, "rediraccept",
2835 SYSCTL_DESCR("Accept and process redirect messages"),
2836 NULL, 0, &icmp6_rediraccept, 0,
2837 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2838 ICMPV6CTL_REDIRACCEPT, CTL_EOL);
2839 sysctl_createv(clog, 0, NULL, NULL,
2840 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2841 CTLTYPE_INT, "redirtimeout",
2842 SYSCTL_DESCR("Redirect generated route lifetime"),
2843 NULL, 0, &icmp6_redirtimeout, 0,
2844 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2845 ICMPV6CTL_REDIRTIMEOUT, CTL_EOL);
2846 #if 0 /* obsoleted */
2847 sysctl_createv(clog, 0, NULL, NULL,
2848 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2849 CTLTYPE_INT, "errratelimit", NULL,
2850 NULL, 0, &icmp6_errratelimit, 0,
2851 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2852 ICMPV6CTL_ERRRATELIMIT, CTL_EOL);
2853 #endif
2854 sysctl_createv(clog, 0, NULL, NULL,
2855 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2856 CTLTYPE_INT, "nd6_prune",
2857 SYSCTL_DESCR("Neighbor discovery prune interval"),
2858 NULL, 0, &nd6_prune, 0,
2859 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2860 ICMPV6CTL_ND6_PRUNE, CTL_EOL);
2861 sysctl_createv(clog, 0, NULL, NULL,
2862 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2863 CTLTYPE_INT, "nd6_delay",
2864 SYSCTL_DESCR("First probe delay time"),
2865 NULL, 0, &nd6_delay, 0,
2866 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2867 ICMPV6CTL_ND6_DELAY, CTL_EOL);
2868 sysctl_createv(clog, 0, NULL, NULL,
2869 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2870 CTLTYPE_INT, "nd6_umaxtries",
2871 SYSCTL_DESCR("Number of unicast discovery attempts"),
2872 NULL, 0, &nd6_umaxtries, 0,
2873 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2874 ICMPV6CTL_ND6_UMAXTRIES, CTL_EOL);
2875 sysctl_createv(clog, 0, NULL, NULL,
2876 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2877 CTLTYPE_INT, "nd6_mmaxtries",
2878 SYSCTL_DESCR("Number of multicast discovery attempts"),
2879 NULL, 0, &nd6_mmaxtries, 0,
2880 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2881 ICMPV6CTL_ND6_MMAXTRIES, CTL_EOL);
2882 sysctl_createv(clog, 0, NULL, NULL,
2883 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2884 CTLTYPE_INT, "nd6_useloopback",
2885 SYSCTL_DESCR("Use loopback interface for local traffic"),
2886 NULL, 0, &nd6_useloopback, 0,
2887 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2888 ICMPV6CTL_ND6_USELOOPBACK, CTL_EOL);
2889 #if 0 /* obsoleted */
2890 sysctl_createv(clog, 0, NULL, NULL,
2891 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2892 CTLTYPE_INT, "nd6_proxyall", NULL,
2893 NULL, 0, &nd6_proxyall, 0,
2894 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2895 ICMPV6CTL_ND6_PROXYALL, CTL_EOL);
2896 #endif
2897 sysctl_createv(clog, 0, NULL, NULL,
2898 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2899 CTLTYPE_INT, "nodeinfo",
2900 SYSCTL_DESCR("Respond to node information requests"),
2901 NULL, 0, &icmp6_nodeinfo, 0,
2902 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2903 ICMPV6CTL_NODEINFO, CTL_EOL);
2904 sysctl_createv(clog, 0, NULL, NULL,
2905 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2906 CTLTYPE_INT, "errppslimit",
2907 SYSCTL_DESCR("Maximum ICMP errors sent per second"),
2908 NULL, 0, &icmp6errppslim, 0,
2909 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2910 ICMPV6CTL_ERRPPSLIMIT, CTL_EOL);
2911 sysctl_createv(clog, 0, NULL, NULL,
2912 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2913 CTLTYPE_INT, "nd6_maxnudhint",
2914 SYSCTL_DESCR("Maximum neighbor unreachable hint count"),
2915 NULL, 0, &nd6_maxnudhint, 0,
2916 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2917 ICMPV6CTL_ND6_MAXNUDHINT, CTL_EOL);
2918 sysctl_createv(clog, 0, NULL, NULL,
2919 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2920 CTLTYPE_INT, "mtudisc_hiwat",
2921 SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
2922 NULL, 0, &icmp6_mtudisc_hiwat, 0,
2923 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2924 ICMPV6CTL_MTUDISC_HIWAT, CTL_EOL);
2925 sysctl_createv(clog, 0, NULL, NULL,
2926 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2927 CTLTYPE_INT, "mtudisc_lowat",
2928 SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
2929 NULL, 0, &icmp6_mtudisc_lowat, 0,
2930 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2931 ICMPV6CTL_MTUDISC_LOWAT, CTL_EOL);
2932 sysctl_createv(clog, 0, NULL, NULL,
2933 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2934 CTLTYPE_INT, "nd6_debug",
2935 SYSCTL_DESCR("Enable neighbor discovery debug output"),
2936 NULL, 0, &nd6_debug, 0,
2937 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2938 ICMPV6CTL_ND6_DEBUG, CTL_EOL);
2939 sysctl_createv(clog, 0, NULL, NULL,
2940 CTLFLAG_PERMANENT,
2941 CTLTYPE_STRUCT, "nd6_drlist",
2942 SYSCTL_DESCR("Default router list"),
2943 sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
2944 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2945 ICMPV6CTL_ND6_DRLIST, CTL_EOL);
2946 sysctl_createv(clog, 0, NULL, NULL,
2947 CTLFLAG_PERMANENT,
2948 CTLTYPE_STRUCT, "nd6_prlist",
2949 SYSCTL_DESCR("Prefix list"),
2950 sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
2951 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2952 ICMPV6CTL_ND6_PRLIST, CTL_EOL);
2953 sysctl_createv(clog, 0, NULL, NULL,
2954 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2955 CTLTYPE_INT, "maxqueuelen",
2956 SYSCTL_DESCR("max packet queue len for a unresolved ND"),
2957 NULL, 1, &nd6_maxqueuelen, 0,
2958 CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2959 ICMPV6CTL_ND6_MAXQLEN, CTL_EOL);
2960 }
2961