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