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