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