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