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