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