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