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