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