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