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