icmp6.c revision 1.76 1 /* $NetBSD: icmp6.c,v 1.76 2002/05/24 09:13:59 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.76 2002/05/24 09:13:59 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
104 #ifdef IPSEC
105 #include <netinet6/ipsec.h>
106 #include <netkey/key.h>
107 #endif
108
109 #include "faith.h"
110 #if defined(NFAITH) && 0 < NFAITH
111 #include <net/if_faith.h>
112 #endif
113
114 #include <net/net_osdep.h>
115
116 extern struct domain inet6domain;
117
118 struct icmp6stat icmp6stat;
119
120 extern struct in6pcb rawin6pcb;
121 extern int icmp6errppslim;
122 static int icmp6errpps_count = 0;
123 static struct timeval icmp6errppslim_last;
124 extern int icmp6_nodeinfo;
125
126 /*
127 * List of callbacks to notify when Path MTU changes are made.
128 */
129 struct icmp6_mtudisc_callback {
130 LIST_ENTRY(icmp6_mtudisc_callback) mc_list;
131 void (*mc_func) __P((struct in6_addr *));
132 };
133
134 LIST_HEAD(, icmp6_mtudisc_callback) icmp6_mtudisc_callbacks =
135 LIST_HEAD_INITIALIZER(&icmp6_mtudisc_callbacks);
136
137 static struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL;
138 extern int pmtu_expire;
139
140 /* XXX do these values make any sense? */
141 static int icmp6_mtudisc_hiwat = 1280;
142 static int icmp6_mtudisc_lowat = 256;
143
144 /*
145 * keep track of # of redirect routes.
146 */
147 static struct rttimer_queue *icmp6_redirect_timeout_q = NULL;
148
149 /* XXX experimental, turned off */
150 static int icmp6_redirect_hiwat = -1;
151 static int icmp6_redirect_lowat = -1;
152
153 static void icmp6_errcount __P((struct icmp6errstat *, int, int));
154 static int icmp6_rip6_input __P((struct mbuf **, int));
155 static int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
156 static const char *icmp6_redirect_diag __P((struct in6_addr *,
157 struct in6_addr *, struct in6_addr *));
158 static struct mbuf *ni6_input __P((struct mbuf *, int));
159 static struct mbuf *ni6_nametodns __P((const char *, int, int));
160 static int ni6_dnsmatch __P((const char *, int, const char *, int));
161 static int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
162 struct ifnet **, char *));
163 static int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
164 struct ifnet *, int));
165 static int icmp6_notify_error __P((struct mbuf *, int, int, int));
166 static struct rtentry *icmp6_mtudisc_clone __P((struct sockaddr *));
167 static void icmp6_mtudisc_timeout __P((struct rtentry *, struct rttimer *));
168 static void icmp6_redirect_timeout __P((struct rtentry *, struct rttimer *));
169
170 void
171 icmp6_init()
172 {
173 mld6_init();
174 icmp6_mtudisc_timeout_q = rt_timer_queue_create(pmtu_expire);
175 icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout);
176 }
177
178 static void
179 icmp6_errcount(stat, type, code)
180 struct icmp6errstat *stat;
181 int type, code;
182 {
183 switch (type) {
184 case ICMP6_DST_UNREACH:
185 switch (code) {
186 case ICMP6_DST_UNREACH_NOROUTE:
187 stat->icp6errs_dst_unreach_noroute++;
188 return;
189 case ICMP6_DST_UNREACH_ADMIN:
190 stat->icp6errs_dst_unreach_admin++;
191 return;
192 case ICMP6_DST_UNREACH_BEYONDSCOPE:
193 stat->icp6errs_dst_unreach_beyondscope++;
194 return;
195 case ICMP6_DST_UNREACH_ADDR:
196 stat->icp6errs_dst_unreach_addr++;
197 return;
198 case ICMP6_DST_UNREACH_NOPORT:
199 stat->icp6errs_dst_unreach_noport++;
200 return;
201 }
202 break;
203 case ICMP6_PACKET_TOO_BIG:
204 stat->icp6errs_packet_too_big++;
205 return;
206 case ICMP6_TIME_EXCEEDED:
207 switch (code) {
208 case ICMP6_TIME_EXCEED_TRANSIT:
209 stat->icp6errs_time_exceed_transit++;
210 return;
211 case ICMP6_TIME_EXCEED_REASSEMBLY:
212 stat->icp6errs_time_exceed_reassembly++;
213 return;
214 }
215 break;
216 case ICMP6_PARAM_PROB:
217 switch (code) {
218 case ICMP6_PARAMPROB_HEADER:
219 stat->icp6errs_paramprob_header++;
220 return;
221 case ICMP6_PARAMPROB_NEXTHEADER:
222 stat->icp6errs_paramprob_nextheader++;
223 return;
224 case ICMP6_PARAMPROB_OPTION:
225 stat->icp6errs_paramprob_option++;
226 return;
227 }
228 break;
229 case ND_REDIRECT:
230 stat->icp6errs_redirect++;
231 return;
232 }
233 stat->icp6errs_unknown++;
234 }
235
236 /*
237 * Register a Path MTU Discovery callback.
238 */
239 void
240 icmp6_mtudisc_callback_register(func)
241 void (*func) __P((struct in6_addr *));
242 {
243 struct icmp6_mtudisc_callback *mc;
244
245 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
246 mc = LIST_NEXT(mc, mc_list)) {
247 if (mc->mc_func == func)
248 return;
249 }
250
251 mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
252 if (mc == NULL)
253 panic("icmp6_mtudisc_callback_register");
254
255 mc->mc_func = func;
256 LIST_INSERT_HEAD(&icmp6_mtudisc_callbacks, mc, mc_list);
257 }
258
259 /*
260 * Generate an error packet of type error in response to bad IP6 packet.
261 */
262 void
263 icmp6_error(m, type, code, param)
264 struct mbuf *m;
265 int type, code, param;
266 {
267 struct ip6_hdr *oip6, *nip6;
268 struct icmp6_hdr *icmp6;
269 u_int preplen;
270 int off;
271 int nxt;
272
273 icmp6stat.icp6s_error++;
274
275 /* count per-type-code statistics */
276 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
277
278 if (m->m_flags & M_DECRYPTED) {
279 icmp6stat.icp6s_canterror++;
280 goto freeit;
281 }
282
283 #ifndef PULLDOWN_TEST
284 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
285 #else
286 if (m->m_len < sizeof(struct ip6_hdr)) {
287 m = m_pullup(m, sizeof(struct ip6_hdr));
288 if (m == NULL)
289 return;
290 }
291 #endif
292 oip6 = mtod(m, struct ip6_hdr *);
293
294 /*
295 * If the destination address of the erroneous packet is a multicast
296 * address, or the packet was sent using link-layer multicast,
297 * we should basically suppress sending an error (RFC 2463, Section
298 * 2.4).
299 * We have two exceptions (the item e.2 in that section):
300 * - the Pakcet Too Big message can be sent for path MTU discovery.
301 * - the Parameter Problem Message that can be allowed an icmp6 error
302 * in the option type field. This check has been done in
303 * ip6_unknown_opt(), so we can just check the type and code.
304 */
305 if ((m->m_flags & (M_BCAST|M_MCAST) ||
306 IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
307 (type != ICMP6_PACKET_TOO_BIG &&
308 (type != ICMP6_PARAM_PROB ||
309 code != ICMP6_PARAMPROB_OPTION)))
310 goto freeit;
311
312 /*
313 * RFC 2463, 2.4 (e.5): source address check.
314 * XXX: the case of anycast source?
315 */
316 if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
317 IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
318 goto freeit;
319
320 /*
321 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
322 * don't do it.
323 */
324 nxt = -1;
325 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
326 if (off >= 0 && nxt == IPPROTO_ICMPV6) {
327 struct icmp6_hdr *icp;
328
329 #ifndef PULLDOWN_TEST
330 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
331 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
332 #else
333 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
334 sizeof(*icp));
335 if (icp == NULL) {
336 icmp6stat.icp6s_tooshort++;
337 return;
338 }
339 #endif
340 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
341 icp->icmp6_type == ND_REDIRECT) {
342 /*
343 * ICMPv6 error
344 * Special case: for redirect (which is
345 * informational) we must not send icmp6 error.
346 */
347 icmp6stat.icp6s_canterror++;
348 goto freeit;
349 } else {
350 /* ICMPv6 informational - send the error */
351 }
352 }
353 #if 0 /* controversial */
354 else if (off >= 0 && nxt == IPPROTO_ESP) {
355 /*
356 * It could be ICMPv6 error inside ESP. Take a safer side,
357 * don't respond.
358 */
359 icmp6stat.icp6s_canterror++;
360 goto freeit;
361 }
362 #endif
363 else {
364 /* non-ICMPv6 - send the error */
365 }
366
367 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
368
369 /* Finally, do rate limitation check. */
370 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
371 icmp6stat.icp6s_toofreq++;
372 goto freeit;
373 }
374
375 /*
376 * OK, ICMP6 can be generated.
377 */
378
379 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
380 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
381
382 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
383 M_PREPEND(m, preplen, M_DONTWAIT);
384 if (m && m->m_len < preplen)
385 m = m_pullup(m, preplen);
386 if (m == NULL) {
387 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
388 return;
389 }
390
391 nip6 = mtod(m, struct ip6_hdr *);
392 nip6->ip6_src = oip6->ip6_src;
393 nip6->ip6_dst = oip6->ip6_dst;
394
395 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
396 oip6->ip6_src.s6_addr16[1] = 0;
397 if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
398 oip6->ip6_dst.s6_addr16[1] = 0;
399
400 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
401 icmp6->icmp6_type = type;
402 icmp6->icmp6_code = code;
403 icmp6->icmp6_pptr = htonl((u_int32_t)param);
404
405 /*
406 * icmp6_reflect() is designed to be in the input path.
407 * icmp6_error() can be called from both input and outut path,
408 * and if we are in output path rcvif could contain bogus value.
409 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
410 * information in ip header (nip6).
411 */
412 m->m_pkthdr.rcvif = NULL;
413
414 icmp6stat.icp6s_outhist[type]++;
415 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
416
417 return;
418
419 freeit:
420 /*
421 * If we can't tell wheter or not we can generate ICMP6, free it.
422 */
423 m_freem(m);
424 }
425
426 /*
427 * Process a received ICMP6 message.
428 */
429 int
430 icmp6_input(mp, offp, proto)
431 struct mbuf **mp;
432 int *offp, proto;
433 {
434 struct mbuf *m = *mp, *n;
435 struct ip6_hdr *ip6, *nip6;
436 struct icmp6_hdr *icmp6, *nicmp6;
437 int off = *offp;
438 int icmp6len = m->m_pkthdr.len - *offp;
439 int code, sum, noff;
440
441 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
442
443 #ifndef PULLDOWN_TEST
444 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
445 /* m might change if M_LOOP. So, call mtod after this */
446 #endif
447
448 /*
449 * Locate icmp6 structure in mbuf, and check
450 * that not corrupted and of at least minimum length
451 */
452
453 ip6 = mtod(m, struct ip6_hdr *);
454 if (icmp6len < sizeof(struct icmp6_hdr)) {
455 icmp6stat.icp6s_tooshort++;
456 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
457 goto freeit;
458 }
459
460 /*
461 * calculate the checksum
462 */
463 #ifndef PULLDOWN_TEST
464 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
465 #else
466 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
467 if (icmp6 == NULL) {
468 icmp6stat.icp6s_tooshort++;
469 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
470 return IPPROTO_DONE;
471 }
472 #endif
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 if (mtu < IPV6_MMTU) {
1215 /* xxx */
1216 rt->rt_rmx.rmx_locks |= RTV_MTU;
1217 } else if (mtu < rt->rt_ifp->if_mtu &&
1218 rt->rt_rmx.rmx_mtu > mtu) {
1219 icmp6stat.icp6s_pmtuchg++;
1220 rt->rt_rmx.rmx_mtu = mtu;
1221 }
1222 }
1223 if (rt) { /* XXX: need braces to avoid conflict with else in RTFREE. */
1224 RTFREE(rt);
1225 }
1226
1227 /*
1228 * Notify protocols that the MTU for this destination
1229 * has changed.
1230 */
1231 for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
1232 mc = LIST_NEXT(mc, mc_list))
1233 (*mc->mc_func)(&sin6.sin6_addr);
1234 }
1235
1236 /*
1237 * Process a Node Information Query packet, based on
1238 * draft-ietf-ipngwg-icmp-name-lookups-07.
1239 *
1240 * Spec incompatibilities:
1241 * - IPv6 Subject address handling
1242 * - IPv4 Subject address handling support missing
1243 * - Proxy reply (answer even if it's not for me)
1244 * - joins NI group address at in6_ifattach() time only, does not cope
1245 * with hostname changes by sethostname(3)
1246 */
1247 #ifndef offsetof /* XXX */
1248 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
1249 #endif
1250 static struct mbuf *
1251 ni6_input(m, off)
1252 struct mbuf *m;
1253 int off;
1254 {
1255 struct icmp6_nodeinfo *ni6, *nni6;
1256 struct mbuf *n = NULL;
1257 u_int16_t qtype;
1258 int subjlen;
1259 int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1260 struct ni_reply_fqdn *fqdn;
1261 int addrs; /* for NI_QTYPE_NODEADDR */
1262 struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1263 struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1264 struct ip6_hdr *ip6;
1265 int oldfqdn = 0; /* if 1, return pascal string (03 draft) */
1266 char *subj = NULL;
1267
1268 ip6 = mtod(m, struct ip6_hdr *);
1269 #ifndef PULLDOWN_TEST
1270 ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1271 #else
1272 IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1273 if (ni6 == NULL) {
1274 /* m is already reclaimed */
1275 return NULL;
1276 }
1277 #endif
1278
1279 /*
1280 * Validate IPv6 destination address.
1281 *
1282 * The Responder must discard the Query without further processing
1283 * unless it is one of the Responder's unicast or anycast addresses, or
1284 * a link-local scope multicast address which the Responder has joined.
1285 * [icmp-name-lookups-07, Section 4.]
1286 */
1287 bzero(&sin6, sizeof(sin6));
1288 sin6.sin6_family = AF_INET6;
1289 sin6.sin6_len = sizeof(struct sockaddr_in6);
1290 bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1291 /* XXX scopeid */
1292 if (ifa_ifwithaddr((struct sockaddr *)&sin6))
1293 ; /* unicast/anycast, fine */
1294 else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1295 ; /* link-local multicast, fine */
1296 else
1297 goto bad;
1298
1299 /* validate query Subject field. */
1300 qtype = ntohs(ni6->ni_qtype);
1301 subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1302 switch (qtype) {
1303 case NI_QTYPE_NOOP:
1304 case NI_QTYPE_SUPTYPES:
1305 /* 07 draft */
1306 if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1307 break;
1308 /* FALLTHROUGH */
1309 case NI_QTYPE_FQDN:
1310 case NI_QTYPE_NODEADDR:
1311 switch (ni6->ni_code) {
1312 case ICMP6_NI_SUBJ_IPV6:
1313 #if ICMP6_NI_SUBJ_IPV6 != 0
1314 case 0:
1315 #endif
1316 /*
1317 * backward compatibility - try to accept 03 draft
1318 * format, where no Subject is present.
1319 */
1320 if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1321 subjlen == 0) {
1322 oldfqdn++;
1323 break;
1324 }
1325 #if ICMP6_NI_SUBJ_IPV6 != 0
1326 if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1327 goto bad;
1328 #endif
1329
1330 if (subjlen != sizeof(sin6.sin6_addr))
1331 goto bad;
1332
1333 /*
1334 * Validate Subject address.
1335 *
1336 * Not sure what exactly "address belongs to the node"
1337 * means in the spec, is it just unicast, or what?
1338 *
1339 * At this moment we consider Subject address as
1340 * "belong to the node" if the Subject address equals
1341 * to the IPv6 destination address; validation for
1342 * IPv6 destination address should have done enough
1343 * check for us.
1344 *
1345 * We do not do proxy at this moment.
1346 */
1347 /* m_pulldown instead of copy? */
1348 m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1349 subjlen, (caddr_t)&sin6.sin6_addr);
1350 /* XXX kame scope hack */
1351 if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
1352 if ((m->m_flags & M_PKTHDR) != 0 &&
1353 m->m_pkthdr.rcvif) {
1354 sin6.sin6_addr.s6_addr16[1] =
1355 htons(m->m_pkthdr.rcvif->if_index);
1356 }
1357 }
1358 subj = (char *)&sin6;
1359 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &sin6.sin6_addr))
1360 break;
1361
1362 /*
1363 * XXX if we are to allow other cases, we should really
1364 * be careful about scope here.
1365 * basically, we should disallow queries toward IPv6
1366 * destination X with subject Y, if scope(X) > scope(Y).
1367 * if we allow scope(X) > scope(Y), it will result in
1368 * information leakage across scope boundary.
1369 */
1370 goto bad;
1371
1372 case ICMP6_NI_SUBJ_FQDN:
1373 /*
1374 * Validate Subject name with gethostname(3).
1375 *
1376 * The behavior may need some debate, since:
1377 * - we are not sure if the node has FQDN as
1378 * hostname (returned by gethostname(3)).
1379 * - the code does wildcard match for truncated names.
1380 * however, we are not sure if we want to perform
1381 * wildcard match, if gethostname(3) side has
1382 * truncated hostname.
1383 */
1384 n = ni6_nametodns(hostname, hostnamelen, 0);
1385 if (!n || n->m_next || n->m_len == 0)
1386 goto bad;
1387 IP6_EXTHDR_GET(subj, char *, m,
1388 off + sizeof(struct icmp6_nodeinfo), subjlen);
1389 if (subj == NULL)
1390 goto bad;
1391 if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1392 n->m_len)) {
1393 goto bad;
1394 }
1395 m_freem(n);
1396 n = NULL;
1397 break;
1398
1399 case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */
1400 default:
1401 goto bad;
1402 }
1403 break;
1404 }
1405
1406 /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */
1407 switch (qtype) {
1408 case NI_QTYPE_FQDN:
1409 if ((icmp6_nodeinfo & 1) == 0)
1410 goto bad;
1411 break;
1412 case NI_QTYPE_NODEADDR:
1413 if ((icmp6_nodeinfo & 2) == 0)
1414 goto bad;
1415 break;
1416 }
1417
1418 /* guess reply length */
1419 switch (qtype) {
1420 case NI_QTYPE_NOOP:
1421 break; /* no reply data */
1422 case NI_QTYPE_SUPTYPES:
1423 replylen += sizeof(u_int32_t);
1424 break;
1425 case NI_QTYPE_FQDN:
1426 /* XXX will append an mbuf */
1427 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1428 break;
1429 case NI_QTYPE_NODEADDR:
1430 addrs = ni6_addrs(ni6, m, &ifp, subj);
1431 if ((replylen += addrs * (sizeof(struct in6_addr) +
1432 sizeof(u_int32_t))) > MCLBYTES)
1433 replylen = MCLBYTES; /* XXX: will truncate pkt later */
1434 break;
1435 default:
1436 /*
1437 * XXX: We must return a reply with the ICMP6 code
1438 * `unknown Qtype' in this case. However we regard the case
1439 * as an FQDN query for backward compatibility.
1440 * Older versions set a random value to this field,
1441 * so it rarely varies in the defined qtypes.
1442 * But the mechanism is not reliable...
1443 * maybe we should obsolete older versions.
1444 */
1445 qtype = NI_QTYPE_FQDN;
1446 /* XXX will append an mbuf */
1447 replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1448 oldfqdn++;
1449 break;
1450 }
1451
1452 /* allocate an mbuf to reply. */
1453 MGETHDR(n, M_DONTWAIT, m->m_type);
1454 if (n == NULL) {
1455 m_freem(m);
1456 return(NULL);
1457 }
1458 M_COPY_PKTHDR(n, m); /* just for rcvif */
1459 if (replylen > MHLEN) {
1460 if (replylen > MCLBYTES) {
1461 /*
1462 * XXX: should we try to allocate more? But MCLBYTES
1463 * is probably much larger than IPV6_MMTU...
1464 */
1465 goto bad;
1466 }
1467 MCLGET(n, M_DONTWAIT);
1468 if ((n->m_flags & M_EXT) == 0) {
1469 goto bad;
1470 }
1471 }
1472 n->m_pkthdr.len = n->m_len = replylen;
1473
1474 /* copy mbuf header and IPv6 + Node Information base headers */
1475 bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1476 nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1477 bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1478
1479 /* qtype dependent procedure */
1480 switch (qtype) {
1481 case NI_QTYPE_NOOP:
1482 nni6->ni_code = ICMP6_NI_SUCCESS;
1483 nni6->ni_flags = 0;
1484 break;
1485 case NI_QTYPE_SUPTYPES:
1486 {
1487 u_int32_t v;
1488 nni6->ni_code = ICMP6_NI_SUCCESS;
1489 nni6->ni_flags = htons(0x0000); /* raw bitmap */
1490 /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1491 v = (u_int32_t)htonl(0x0000000f);
1492 bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1493 break;
1494 }
1495 case NI_QTYPE_FQDN:
1496 nni6->ni_code = ICMP6_NI_SUCCESS;
1497 fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1498 sizeof(struct ip6_hdr) +
1499 sizeof(struct icmp6_nodeinfo));
1500 nni6->ni_flags = 0; /* XXX: meaningless TTL */
1501 fqdn->ni_fqdn_ttl = 0; /* ditto. */
1502 /*
1503 * XXX do we really have FQDN in variable "hostname"?
1504 */
1505 n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1506 if (n->m_next == NULL)
1507 goto bad;
1508 /* XXX we assume that n->m_next is not a chain */
1509 if (n->m_next->m_next != NULL)
1510 goto bad;
1511 n->m_pkthdr.len += n->m_next->m_len;
1512 break;
1513 case NI_QTYPE_NODEADDR:
1514 {
1515 int lenlim, copied;
1516
1517 nni6->ni_code = ICMP6_NI_SUCCESS;
1518 n->m_pkthdr.len = n->m_len =
1519 sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1520 lenlim = M_TRAILINGSPACE(n);
1521 copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1522 /* XXX: reset mbuf length */
1523 n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1524 sizeof(struct icmp6_nodeinfo) + copied;
1525 break;
1526 }
1527 default:
1528 break; /* XXX impossible! */
1529 }
1530
1531 nni6->ni_type = ICMP6_NI_REPLY;
1532 m_freem(m);
1533 return(n);
1534
1535 bad:
1536 m_freem(m);
1537 if (n)
1538 m_freem(n);
1539 return(NULL);
1540 }
1541 #undef hostnamelen
1542
1543 #define isupper(x) ('A' <= (x) && (x) <= 'Z')
1544 #define isalpha(x) (('A' <= (x) && (x) <= 'Z') || ('a' <= (x) && (x) <= 'z'))
1545 #define isalnum(x) (isalpha(x) || ('0' <= (x) && (x) <= '9'))
1546 #define tolower(x) (isupper(x) ? (x) + 'a' - 'A' : (x))
1547
1548 /*
1549 * make a mbuf with DNS-encoded string. no compression support.
1550 *
1551 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1552 * treated as truncated name (two \0 at the end). this is a wild guess.
1553 */
1554 static struct mbuf *
1555 ni6_nametodns(name, namelen, old)
1556 const char *name;
1557 int namelen;
1558 int old; /* return pascal string if non-zero */
1559 {
1560 struct mbuf *m;
1561 char *cp, *ep;
1562 const char *p, *q;
1563 int i, len, nterm;
1564
1565 if (old)
1566 len = namelen + 1;
1567 else
1568 len = MCLBYTES;
1569
1570 /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1571 MGET(m, M_DONTWAIT, MT_DATA);
1572 if (m && len > MLEN) {
1573 MCLGET(m, M_DONTWAIT);
1574 if ((m->m_flags & M_EXT) == 0)
1575 goto fail;
1576 }
1577 if (!m)
1578 goto fail;
1579 m->m_next = NULL;
1580
1581 if (old) {
1582 m->m_len = len;
1583 *mtod(m, char *) = namelen;
1584 bcopy(name, mtod(m, char *) + 1, namelen);
1585 return m;
1586 } else {
1587 m->m_len = 0;
1588 cp = mtod(m, char *);
1589 ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1590
1591 /* if not certain about my name, return empty buffer */
1592 if (namelen == 0)
1593 return m;
1594
1595 /*
1596 * guess if it looks like shortened hostname, or FQDN.
1597 * shortened hostname needs two trailing "\0".
1598 */
1599 i = 0;
1600 for (p = name; p < name + namelen; p++) {
1601 if (*p && *p == '.')
1602 i++;
1603 }
1604 if (i < 2)
1605 nterm = 2;
1606 else
1607 nterm = 1;
1608
1609 p = name;
1610 while (cp < ep && p < name + namelen) {
1611 i = 0;
1612 for (q = p; q < name + namelen && *q && *q != '.'; q++)
1613 i++;
1614 /* result does not fit into mbuf */
1615 if (cp + i + 1 >= ep)
1616 goto fail;
1617 /*
1618 * DNS label length restriction, RFC1035 page 8.
1619 * "i == 0" case is included here to avoid returning
1620 * 0-length label on "foo..bar".
1621 */
1622 if (i <= 0 || i >= 64)
1623 goto fail;
1624 *cp++ = i;
1625 if (!isalpha(p[0]) || !isalnum(p[i - 1]))
1626 goto fail;
1627 while (i > 0) {
1628 if (!isalnum(*p) && *p != '-')
1629 goto fail;
1630 if (isupper(*p))
1631 *cp++ = tolower(*p++);
1632 else
1633 *cp++ = *p++;
1634 i--;
1635 }
1636 p = q;
1637 if (p < name + namelen && *p == '.')
1638 p++;
1639 }
1640 /* termination */
1641 if (cp + nterm >= ep)
1642 goto fail;
1643 while (nterm-- > 0)
1644 *cp++ = '\0';
1645 m->m_len = cp - mtod(m, char *);
1646 return m;
1647 }
1648
1649 panic("should not reach here");
1650 /* NOTREACHED */
1651
1652 fail:
1653 if (m)
1654 m_freem(m);
1655 return NULL;
1656 }
1657
1658 /*
1659 * check if two DNS-encoded string matches. takes care of truncated
1660 * form (with \0\0 at the end). no compression support.
1661 * XXX upper/lowercase match (see RFC2065)
1662 */
1663 static int
1664 ni6_dnsmatch(a, alen, b, blen)
1665 const char *a;
1666 int alen;
1667 const char *b;
1668 int blen;
1669 {
1670 const char *a0, *b0;
1671 int l;
1672
1673 /* simplest case - need validation? */
1674 if (alen == blen && bcmp(a, b, alen) == 0)
1675 return 1;
1676
1677 a0 = a;
1678 b0 = b;
1679
1680 /* termination is mandatory */
1681 if (alen < 2 || blen < 2)
1682 return 0;
1683 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1684 return 0;
1685 alen--;
1686 blen--;
1687
1688 while (a - a0 < alen && b - b0 < blen) {
1689 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1690 return 0;
1691
1692 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1693 return 0;
1694 /* we don't support compression yet */
1695 if (a[0] >= 64 || b[0] >= 64)
1696 return 0;
1697
1698 /* truncated case */
1699 if (a[0] == 0 && a - a0 == alen - 1)
1700 return 1;
1701 if (b[0] == 0 && b - b0 == blen - 1)
1702 return 1;
1703 if (a[0] == 0 || b[0] == 0)
1704 return 0;
1705
1706 if (a[0] != b[0])
1707 return 0;
1708 l = a[0];
1709 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1710 return 0;
1711 if (bcmp(a + 1, b + 1, l) != 0)
1712 return 0;
1713
1714 a += 1 + l;
1715 b += 1 + l;
1716 }
1717
1718 if (a - a0 == alen && b - b0 == blen)
1719 return 1;
1720 else
1721 return 0;
1722 }
1723
1724 /*
1725 * calculate the number of addresses to be returned in the node info reply.
1726 */
1727 static int
1728 ni6_addrs(ni6, m, ifpp, subj)
1729 struct icmp6_nodeinfo *ni6;
1730 struct mbuf *m;
1731 struct ifnet **ifpp;
1732 char *subj;
1733 {
1734 struct ifnet *ifp;
1735 struct in6_ifaddr *ifa6;
1736 struct ifaddr *ifa;
1737 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1738 int addrs = 0, addrsofif, iffound = 0;
1739 int niflags = ni6->ni_flags;
1740
1741 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1742 switch (ni6->ni_code) {
1743 case ICMP6_NI_SUBJ_IPV6:
1744 if (subj == NULL) /* must be impossible... */
1745 return(0);
1746 subj_ip6 = (struct sockaddr_in6 *)subj;
1747 break;
1748 default:
1749 /*
1750 * XXX: we only support IPv6 subject address for
1751 * this Qtype.
1752 */
1753 return(0);
1754 }
1755 }
1756
1757 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1758 {
1759 addrsofif = 0;
1760 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1761 ifa = ifa->ifa_list.tqe_next)
1762 {
1763 if (ifa->ifa_addr->sa_family != AF_INET6)
1764 continue;
1765 ifa6 = (struct in6_ifaddr *)ifa;
1766
1767 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1768 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1769 &ifa6->ia_addr.sin6_addr))
1770 iffound = 1;
1771
1772 /*
1773 * IPv4-mapped addresses can only be returned by a
1774 * Node Information proxy, since they represent
1775 * addresses of IPv4-only nodes, which perforce do
1776 * not implement this protocol.
1777 * [icmp-name-lookups-07, Section 5.4]
1778 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1779 * this function at this moment.
1780 */
1781
1782 /* What do we have to do about ::1? */
1783 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1784 case IPV6_ADDR_SCOPE_LINKLOCAL:
1785 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1786 continue;
1787 break;
1788 case IPV6_ADDR_SCOPE_SITELOCAL:
1789 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1790 continue;
1791 break;
1792 case IPV6_ADDR_SCOPE_GLOBAL:
1793 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1794 continue;
1795 break;
1796 default:
1797 continue;
1798 }
1799
1800 /*
1801 * check if anycast is okay.
1802 * XXX: just experimental. not in the spec.
1803 */
1804 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1805 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1806 continue; /* we need only unicast addresses */
1807
1808 addrsofif++; /* count the address */
1809 }
1810 if (iffound) {
1811 *ifpp = ifp;
1812 return(addrsofif);
1813 }
1814
1815 addrs += addrsofif;
1816 }
1817
1818 return(addrs);
1819 }
1820
1821 static int
1822 ni6_store_addrs(ni6, nni6, ifp0, resid)
1823 struct icmp6_nodeinfo *ni6, *nni6;
1824 struct ifnet *ifp0;
1825 int resid;
1826 {
1827 struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1828 struct in6_ifaddr *ifa6;
1829 struct ifaddr *ifa;
1830 struct ifnet *ifp_dep = NULL;
1831 int copied = 0, allow_deprecated = 0;
1832 u_char *cp = (u_char *)(nni6 + 1);
1833 int niflags = ni6->ni_flags;
1834 u_int32_t ltime;
1835 long time_second = time.tv_sec;
1836
1837 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1838 return(0); /* needless to copy */
1839
1840 again:
1841
1842 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1843 {
1844 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1845 ifa = ifa->ifa_list.tqe_next)
1846 {
1847 if (ifa->ifa_addr->sa_family != AF_INET6)
1848 continue;
1849 ifa6 = (struct in6_ifaddr *)ifa;
1850
1851 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1852 allow_deprecated == 0) {
1853 /*
1854 * prefererred address should be put before
1855 * deprecated addresses.
1856 */
1857
1858 /* record the interface for later search */
1859 if (ifp_dep == NULL)
1860 ifp_dep = ifp;
1861
1862 continue;
1863 }
1864 else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1865 allow_deprecated != 0)
1866 continue; /* we now collect deprecated addrs */
1867
1868 /* What do we have to do about ::1? */
1869 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1870 case IPV6_ADDR_SCOPE_LINKLOCAL:
1871 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1872 continue;
1873 break;
1874 case IPV6_ADDR_SCOPE_SITELOCAL:
1875 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1876 continue;
1877 break;
1878 case IPV6_ADDR_SCOPE_GLOBAL:
1879 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1880 continue;
1881 break;
1882 default:
1883 continue;
1884 }
1885
1886 /*
1887 * check if anycast is okay.
1888 * XXX: just experimental. not in the spec.
1889 */
1890 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1891 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1892 continue;
1893
1894 /* now we can copy the address */
1895 if (resid < sizeof(struct in6_addr) +
1896 sizeof(u_int32_t)) {
1897 /*
1898 * We give up much more copy.
1899 * Set the truncate flag and return.
1900 */
1901 nni6->ni_flags |=
1902 NI_NODEADDR_FLAG_TRUNCATE;
1903 return(copied);
1904 }
1905
1906 /*
1907 * Set the TTL of the address.
1908 * The TTL value should be one of the following
1909 * according to the specification:
1910 *
1911 * 1. The remaining lifetime of a DHCP lease on the
1912 * address, or
1913 * 2. The remaining Valid Lifetime of a prefix from
1914 * which the address was derived through Stateless
1915 * Autoconfiguration.
1916 *
1917 * Note that we currently do not support stateful
1918 * address configuration by DHCPv6, so the former
1919 * case can't happen.
1920 *
1921 * TTL must be 2^31 > TTL >= 0.
1922 */
1923 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1924 ltime = ND6_INFINITE_LIFETIME;
1925 else {
1926 if (ifa6->ia6_lifetime.ia6t_expire >
1927 time_second)
1928 ltime = ifa6->ia6_lifetime.ia6t_expire - time_second;
1929 else
1930 ltime = 0;
1931 }
1932 if (ltime > 0x7fffffff)
1933 ltime = 0x7fffffff;
1934 ltime = htonl(ltime);
1935
1936 bcopy(<ime, cp, sizeof(u_int32_t));
1937 cp += sizeof(u_int32_t);
1938
1939 /* copy the address itself */
1940 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1941 sizeof(struct in6_addr));
1942 /* XXX: KAME link-local hack; remove ifindex */
1943 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1944 ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1945 cp += sizeof(struct in6_addr);
1946
1947 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1948 copied += (sizeof(struct in6_addr) +
1949 sizeof(u_int32_t));
1950 }
1951 if (ifp0) /* we need search only on the specified IF */
1952 break;
1953 }
1954
1955 if (allow_deprecated == 0 && ifp_dep != NULL) {
1956 ifp = ifp_dep;
1957 allow_deprecated = 1;
1958
1959 goto again;
1960 }
1961
1962 return(copied);
1963 }
1964
1965 /*
1966 * XXX almost dup'ed code with rip6_input.
1967 */
1968 static int
1969 icmp6_rip6_input(mp, off)
1970 struct mbuf **mp;
1971 int off;
1972 {
1973 struct mbuf *m = *mp;
1974 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1975 struct in6pcb *in6p;
1976 struct in6pcb *last = NULL;
1977 struct sockaddr_in6 rip6src;
1978 struct icmp6_hdr *icmp6;
1979 struct mbuf *opts = NULL;
1980
1981 #ifndef PULLDOWN_TEST
1982 /* this is assumed to be safe. */
1983 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1984 #else
1985 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1986 if (icmp6 == NULL) {
1987 /* m is already reclaimed */
1988 return IPPROTO_DONE;
1989 }
1990 #endif
1991
1992 bzero(&rip6src, sizeof(rip6src));
1993 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1994 rip6src.sin6_family = AF_INET6;
1995 /* KAME hack: recover scopeid */
1996 (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1997
1998 for (in6p = rawin6pcb.in6p_next;
1999 in6p != &rawin6pcb; in6p = in6p->in6p_next)
2000 {
2001 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
2002 continue;
2003 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
2004 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
2005 continue;
2006 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
2007 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
2008 continue;
2009 if (in6p->in6p_icmp6filt
2010 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
2011 in6p->in6p_icmp6filt))
2012 continue;
2013 if (last) {
2014 struct mbuf *n;
2015 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
2016 if (last->in6p_flags & IN6P_CONTROLOPTS)
2017 ip6_savecontrol(last, &opts, ip6, n);
2018 /* strip intermediate headers */
2019 m_adj(n, off);
2020 if (sbappendaddr(&last->in6p_socket->so_rcv,
2021 (struct sockaddr *)&rip6src,
2022 n, opts) == 0) {
2023 /* should notify about lost packet */
2024 m_freem(n);
2025 if (opts)
2026 m_freem(opts);
2027 } else
2028 sorwakeup(last->in6p_socket);
2029 opts = NULL;
2030 }
2031 }
2032 last = in6p;
2033 }
2034 if (last) {
2035 if (last->in6p_flags & IN6P_CONTROLOPTS)
2036 ip6_savecontrol(last, &opts, ip6, m);
2037 /* strip intermediate headers */
2038 m_adj(m, off);
2039 if (sbappendaddr(&last->in6p_socket->so_rcv,
2040 (struct sockaddr *)&rip6src, m, opts) == 0) {
2041 m_freem(m);
2042 if (opts)
2043 m_freem(opts);
2044 } else
2045 sorwakeup(last->in6p_socket);
2046 } else {
2047 m_freem(m);
2048 ip6stat.ip6s_delivered--;
2049 }
2050 return IPPROTO_DONE;
2051 }
2052
2053 /*
2054 * Reflect the ip6 packet back to the source.
2055 * OFF points to the icmp6 header, counted from the top of the mbuf.
2056 *
2057 * Note: RFC 1885 required that an echo reply should be truncated if it
2058 * did not fit in with (return) path MTU, and KAME code supported the
2059 * behavior. However, as a clarification after the RFC, this limitation
2060 * was removed in a revised version of the spec, RFC 2463. We had kept the
2061 * old behavior, with a (non-default) ifdef block, while the new version of
2062 * the spec was an internet-draft status, and even after the new RFC was
2063 * published. But it would rather make sense to clean the obsoleted part
2064 * up, and to make the code simpler at this stage.
2065 */
2066 void
2067 icmp6_reflect(m, off)
2068 struct mbuf *m;
2069 size_t off;
2070 {
2071 struct ip6_hdr *ip6;
2072 struct icmp6_hdr *icmp6;
2073 struct in6_ifaddr *ia;
2074 struct in6_addr t, *src = 0;
2075 int plen;
2076 int type, code;
2077 struct ifnet *outif = NULL;
2078 struct sockaddr_in6 sa6_src, sa6_dst;
2079
2080 /* too short to reflect */
2081 if (off < sizeof(struct ip6_hdr)) {
2082 nd6log((LOG_DEBUG,
2083 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2084 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2085 __FILE__, __LINE__));
2086 goto bad;
2087 }
2088
2089 /*
2090 * If there are extra headers between IPv6 and ICMPv6, strip
2091 * off that header first.
2092 */
2093 #ifdef DIAGNOSTIC
2094 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2095 panic("assumption failed in icmp6_reflect");
2096 #endif
2097 if (off > sizeof(struct ip6_hdr)) {
2098 size_t l;
2099 struct ip6_hdr nip6;
2100
2101 l = off - sizeof(struct ip6_hdr);
2102 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2103 m_adj(m, l);
2104 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2105 if (m->m_len < l) {
2106 if ((m = m_pullup(m, l)) == NULL)
2107 return;
2108 }
2109 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2110 } else /* off == sizeof(struct ip6_hdr) */ {
2111 size_t l;
2112 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2113 if (m->m_len < l) {
2114 if ((m = m_pullup(m, l)) == NULL)
2115 return;
2116 }
2117 }
2118 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2119 ip6 = mtod(m, struct ip6_hdr *);
2120 ip6->ip6_nxt = IPPROTO_ICMPV6;
2121 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2122 type = icmp6->icmp6_type; /* keep type for statistics */
2123 code = icmp6->icmp6_code; /* ditto. */
2124
2125 t = ip6->ip6_dst;
2126 /*
2127 * ip6_input() drops a packet if its src is multicast.
2128 * So, the src is never multicast.
2129 */
2130 ip6->ip6_dst = ip6->ip6_src;
2131
2132 /*
2133 * XXX: make sure to embed scope zone information, using
2134 * already embedded IDs or the received interface (if any).
2135 * Note that rcvif may be NULL.
2136 * TODO: scoped routing case (XXX).
2137 */
2138 bzero(&sa6_src, sizeof(sa6_src));
2139 sa6_src.sin6_family = AF_INET6;
2140 sa6_src.sin6_len = sizeof(sa6_src);
2141 sa6_src.sin6_addr = ip6->ip6_dst;
2142 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2143 in6_embedscope(&sa6_src.sin6_addr, &sa6_src, NULL, NULL);
2144 ip6->ip6_dst = sa6_src.sin6_addr;
2145
2146 bzero(&sa6_dst, sizeof(sa6_dst));
2147 sa6_dst.sin6_family = AF_INET6;
2148 sa6_dst.sin6_len = sizeof(sa6_dst);
2149 sa6_dst.sin6_addr = t;
2150 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2151 in6_embedscope(&t, &sa6_dst, NULL, NULL);
2152
2153 /*
2154 * If the incoming packet was addressed directly to us (i.e. unicast),
2155 * use dst as the src for the reply.
2156 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2157 * (for example) when we encounter an error while forwarding procedure
2158 * destined to a duplicated address of ours.
2159 */
2160 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2161 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2162 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2163 src = &t;
2164 break;
2165 }
2166 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2167 /*
2168 * This is the case if the dst is our link-local address
2169 * and the sender is also ourselves.
2170 */
2171 src = &t;
2172 }
2173
2174 if (src == 0) {
2175 int e;
2176 struct route_in6 ro;
2177
2178 /*
2179 * This case matches to multicasts, our anycast, or unicasts
2180 * that we do not own. Select a source address based on the
2181 * source address of the erroneous packet.
2182 */
2183 bzero(&ro, sizeof(ro));
2184 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e);
2185 if (ro.ro_rt) { /* XXX: see comments in icmp6_mtudisc_update */
2186 RTFREE(ro.ro_rt); /* XXX: we could use this */
2187 }
2188 if (src == NULL) {
2189 nd6log((LOG_DEBUG,
2190 "icmp6_reflect: source can't be determined: "
2191 "dst=%s, error=%d\n",
2192 ip6_sprintf(&sa6_src.sin6_addr), e));
2193 goto bad;
2194 }
2195 }
2196
2197 ip6->ip6_src = *src;
2198
2199 ip6->ip6_flow = 0;
2200 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2201 ip6->ip6_vfc |= IPV6_VERSION;
2202 ip6->ip6_nxt = IPPROTO_ICMPV6;
2203 if (m->m_pkthdr.rcvif) {
2204 /* XXX: This may not be the outgoing interface */
2205 ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim;
2206 } else
2207 ip6->ip6_hlim = ip6_defhlim;
2208
2209 icmp6->icmp6_cksum = 0;
2210 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2211 sizeof(struct ip6_hdr), plen);
2212
2213 /*
2214 * XXX option handling
2215 */
2216
2217 m->m_flags &= ~(M_BCAST|M_MCAST);
2218 #ifdef IPSEC
2219 /* Don't lookup socket */
2220 (void)ipsec_setsocket(m, NULL);
2221 #endif /* IPSEC */
2222
2223 /*
2224 * To avoid a "too big" situation at an intermediate router
2225 * and the path MTU discovery process, specify the IPV6_MINMTU flag.
2226 * Note that only echo and node information replies are affected,
2227 * since the length of ICMP6 errors is limited to the minimum MTU.
2228 */
2229 if (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, &outif) != 0 && outif)
2230 icmp6_ifstat_inc(outif, ifs6_out_error);
2231
2232 if (outif)
2233 icmp6_ifoutstat_inc(outif, type, code);
2234
2235 return;
2236
2237 bad:
2238 m_freem(m);
2239 return;
2240 }
2241
2242 void
2243 icmp6_fasttimo()
2244 {
2245
2246 mld6_fasttimeo();
2247 }
2248
2249 static const char *
2250 icmp6_redirect_diag(src6, dst6, tgt6)
2251 struct in6_addr *src6;
2252 struct in6_addr *dst6;
2253 struct in6_addr *tgt6;
2254 {
2255 static char buf[1024];
2256 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2257 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2258 return buf;
2259 }
2260
2261 void
2262 icmp6_redirect_input(m, off)
2263 struct mbuf *m;
2264 int off;
2265 {
2266 struct ifnet *ifp = m->m_pkthdr.rcvif;
2267 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2268 struct nd_redirect *nd_rd;
2269 int icmp6len = ntohs(ip6->ip6_plen);
2270 char *lladdr = NULL;
2271 int lladdrlen = 0;
2272 u_char *redirhdr = NULL;
2273 int redirhdrlen = 0;
2274 struct rtentry *rt = NULL;
2275 int is_router;
2276 int is_onlink;
2277 struct in6_addr src6 = ip6->ip6_src;
2278 struct in6_addr redtgt6;
2279 struct in6_addr reddst6;
2280 union nd_opts ndopts;
2281
2282 if (!m || !ifp)
2283 return;
2284
2285 /* XXX if we are router, we don't update route by icmp6 redirect */
2286 if (ip6_forwarding)
2287 goto freeit;
2288 if (!icmp6_rediraccept)
2289 goto freeit;
2290
2291 #ifndef PULLDOWN_TEST
2292 IP6_EXTHDR_CHECK(m, off, icmp6len,);
2293 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2294 #else
2295 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2296 if (nd_rd == NULL) {
2297 icmp6stat.icp6s_tooshort++;
2298 return;
2299 }
2300 #endif
2301 redtgt6 = nd_rd->nd_rd_target;
2302 reddst6 = nd_rd->nd_rd_dst;
2303
2304 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2305 redtgt6.s6_addr16[1] = htons(ifp->if_index);
2306 if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2307 reddst6.s6_addr16[1] = htons(ifp->if_index);
2308
2309 /* validation */
2310 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2311 nd6log((LOG_ERR,
2312 "ICMP6 redirect sent from %s rejected; "
2313 "must be from linklocal\n", ip6_sprintf(&src6)));
2314 goto bad;
2315 }
2316 if (ip6->ip6_hlim != 255) {
2317 nd6log((LOG_ERR,
2318 "ICMP6 redirect sent from %s rejected; "
2319 "hlim=%d (must be 255)\n",
2320 ip6_sprintf(&src6), ip6->ip6_hlim));
2321 goto bad;
2322 }
2323 {
2324 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2325 struct sockaddr_in6 sin6;
2326 struct in6_addr *gw6;
2327
2328 bzero(&sin6, sizeof(sin6));
2329 sin6.sin6_family = AF_INET6;
2330 sin6.sin6_len = sizeof(struct sockaddr_in6);
2331 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2332 rt = rtalloc1((struct sockaddr *)&sin6, 0);
2333 if (rt) {
2334 if (rt->rt_gateway == NULL ||
2335 rt->rt_gateway->sa_family != AF_INET6) {
2336 nd6log((LOG_ERR,
2337 "ICMP6 redirect rejected; no route "
2338 "with inet6 gateway found for redirect dst: %s\n",
2339 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2340 RTFREE(rt);
2341 goto bad;
2342 }
2343
2344 gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2345 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2346 nd6log((LOG_ERR,
2347 "ICMP6 redirect rejected; "
2348 "not equal to gw-for-src=%s (must be same): "
2349 "%s\n",
2350 ip6_sprintf(gw6),
2351 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2352 RTFREE(rt);
2353 goto bad;
2354 }
2355 } else {
2356 nd6log((LOG_ERR,
2357 "ICMP6 redirect rejected; "
2358 "no route found for redirect dst: %s\n",
2359 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2360 goto bad;
2361 }
2362 RTFREE(rt);
2363 rt = NULL;
2364 }
2365 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2366 nd6log((LOG_ERR,
2367 "ICMP6 redirect rejected; "
2368 "redirect dst must be unicast: %s\n",
2369 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2370 goto bad;
2371 }
2372
2373 is_router = is_onlink = 0;
2374 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2375 is_router = 1; /* router case */
2376 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2377 is_onlink = 1; /* on-link destination case */
2378 if (!is_router && !is_onlink) {
2379 nd6log((LOG_ERR,
2380 "ICMP6 redirect rejected; "
2381 "neither router case nor onlink case: %s\n",
2382 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2383 goto bad;
2384 }
2385 /* validation passed */
2386
2387 icmp6len -= sizeof(*nd_rd);
2388 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2389 if (nd6_options(&ndopts) < 0) {
2390 nd6log((LOG_INFO, "icmp6_redirect_input: "
2391 "invalid ND option, rejected: %s\n",
2392 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2393 /* nd6_options have incremented stats */
2394 goto freeit;
2395 }
2396
2397 if (ndopts.nd_opts_tgt_lladdr) {
2398 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2399 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2400 }
2401
2402 if (ndopts.nd_opts_rh) {
2403 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2404 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2405 }
2406
2407 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2408 nd6log((LOG_INFO,
2409 "icmp6_redirect_input: lladdrlen mismatch for %s "
2410 "(if %d, icmp6 packet %d): %s\n",
2411 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2412 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2413 goto bad;
2414 }
2415
2416 /* RFC 2461 8.3 */
2417 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2418 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2419
2420 if (!is_onlink) { /* better router case. perform rtredirect. */
2421 /* perform rtredirect */
2422 struct sockaddr_in6 sdst;
2423 struct sockaddr_in6 sgw;
2424 struct sockaddr_in6 ssrc;
2425 unsigned long rtcount;
2426 struct rtentry *newrt = NULL;
2427
2428 /*
2429 * do not install redirect route, if the number of entries
2430 * is too much (> hiwat). note that, the node (= host) will
2431 * work just fine even if we do not install redirect route
2432 * (there will be additional hops, though).
2433 */
2434 rtcount = rt_timer_count(icmp6_redirect_timeout_q);
2435 if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat)
2436 return;
2437 else if (0 <= icmp6_redirect_lowat &&
2438 rtcount > icmp6_redirect_lowat) {
2439 /*
2440 * XXX nuke a victim, install the new one.
2441 */
2442 }
2443
2444 bzero(&sdst, sizeof(sdst));
2445 bzero(&sgw, sizeof(sgw));
2446 bzero(&ssrc, sizeof(ssrc));
2447 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2448 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2449 sizeof(struct sockaddr_in6);
2450 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2451 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2452 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2453 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2454 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2455 (struct sockaddr *)&ssrc,
2456 &newrt);
2457
2458 if (newrt) {
2459 (void)rt_timer_add(newrt, icmp6_redirect_timeout,
2460 icmp6_redirect_timeout_q);
2461 rtfree(newrt);
2462 }
2463 }
2464 /* finally update cached route in each socket via pfctlinput */
2465 {
2466 struct sockaddr_in6 sdst;
2467
2468 bzero(&sdst, sizeof(sdst));
2469 sdst.sin6_family = AF_INET6;
2470 sdst.sin6_len = sizeof(struct sockaddr_in6);
2471 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2472 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2473 #ifdef IPSEC
2474 key_sa_routechange((struct sockaddr *)&sdst);
2475 #endif
2476 }
2477
2478 freeit:
2479 m_freem(m);
2480 return;
2481
2482 bad:
2483 icmp6stat.icp6s_badredirect++;
2484 m_freem(m);
2485 }
2486
2487 void
2488 icmp6_redirect_output(m0, rt)
2489 struct mbuf *m0;
2490 struct rtentry *rt;
2491 {
2492 struct ifnet *ifp; /* my outgoing interface */
2493 struct in6_addr *ifp_ll6;
2494 struct in6_addr *nexthop;
2495 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2496 struct mbuf *m = NULL; /* newly allocated one */
2497 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2498 struct nd_redirect *nd_rd;
2499 size_t maxlen;
2500 u_char *p;
2501 struct sockaddr_in6 src_sa;
2502
2503 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2504
2505 /* if we are not router, we don't send icmp6 redirect */
2506 if (!ip6_forwarding || ip6_accept_rtadv)
2507 goto fail;
2508
2509 /* sanity check */
2510 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2511 goto fail;
2512
2513 /*
2514 * Address check:
2515 * the source address must identify a neighbor, and
2516 * the destination address must not be a multicast address
2517 * [RFC 2461, sec 8.2]
2518 */
2519 sip6 = mtod(m0, struct ip6_hdr *);
2520 bzero(&src_sa, sizeof(src_sa));
2521 src_sa.sin6_family = AF_INET6;
2522 src_sa.sin6_len = sizeof(src_sa);
2523 src_sa.sin6_addr = sip6->ip6_src;
2524 /* we don't currently use sin6_scope_id, but eventually use it */
2525 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2526 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2527 goto fail;
2528 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2529 goto fail; /* what should we do here? */
2530
2531 /* rate limit */
2532 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2533 goto fail;
2534
2535 /*
2536 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2537 * we almost always ask for an mbuf cluster for simplicity.
2538 * (MHLEN < IPV6_MMTU is almost always true)
2539 */
2540 #if IPV6_MMTU >= MCLBYTES
2541 # error assumption failed about IPV6_MMTU and MCLBYTES
2542 #endif
2543 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2544 if (m && IPV6_MMTU >= MHLEN)
2545 MCLGET(m, M_DONTWAIT);
2546 if (!m)
2547 goto fail;
2548 m->m_pkthdr.rcvif = NULL;
2549 m->m_len = 0;
2550 maxlen = M_TRAILINGSPACE(m);
2551 maxlen = min(IPV6_MMTU, maxlen);
2552 /* just for safety */
2553 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2554 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2555 goto fail;
2556 }
2557
2558 {
2559 /* get ip6 linklocal address for ifp(my outgoing interface). */
2560 struct in6_ifaddr *ia;
2561 if ((ia = in6ifa_ifpforlinklocal(ifp,
2562 IN6_IFF_NOTREADY|
2563 IN6_IFF_ANYCAST)) == NULL)
2564 goto fail;
2565 ifp_ll6 = &ia->ia_addr.sin6_addr;
2566 }
2567
2568 /* get ip6 linklocal address for the router. */
2569 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2570 struct sockaddr_in6 *sin6;
2571 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2572 nexthop = &sin6->sin6_addr;
2573 if (!IN6_IS_ADDR_LINKLOCAL(nexthop))
2574 nexthop = NULL;
2575 } else
2576 nexthop = NULL;
2577
2578 /* ip6 */
2579 ip6 = mtod(m, struct ip6_hdr *);
2580 ip6->ip6_flow = 0;
2581 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2582 ip6->ip6_vfc |= IPV6_VERSION;
2583 /* ip6->ip6_plen will be set later */
2584 ip6->ip6_nxt = IPPROTO_ICMPV6;
2585 ip6->ip6_hlim = 255;
2586 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2587 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2588 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2589
2590 /* ND Redirect */
2591 nd_rd = (struct nd_redirect *)(ip6 + 1);
2592 nd_rd->nd_rd_type = ND_REDIRECT;
2593 nd_rd->nd_rd_code = 0;
2594 nd_rd->nd_rd_reserved = 0;
2595 if (rt->rt_flags & RTF_GATEWAY) {
2596 /*
2597 * nd_rd->nd_rd_target must be a link-local address in
2598 * better router cases.
2599 */
2600 if (!nexthop)
2601 goto fail;
2602 bcopy(nexthop, &nd_rd->nd_rd_target,
2603 sizeof(nd_rd->nd_rd_target));
2604 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2605 sizeof(nd_rd->nd_rd_dst));
2606 } else {
2607 /* make sure redtgt == reddst */
2608 nexthop = &sip6->ip6_dst;
2609 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2610 sizeof(nd_rd->nd_rd_target));
2611 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2612 sizeof(nd_rd->nd_rd_dst));
2613 }
2614
2615 p = (u_char *)(nd_rd + 1);
2616
2617 {
2618 /* target lladdr option */
2619 struct rtentry *rt_nexthop = NULL;
2620 int len;
2621 struct sockaddr_dl *sdl;
2622 struct nd_opt_hdr *nd_opt;
2623 char *lladdr;
2624
2625 rt_nexthop = nd6_lookup(nexthop, 0, ifp);
2626 if (!rt_nexthop)
2627 goto nolladdropt;
2628 len = sizeof(*nd_opt) + ifp->if_addrlen;
2629 len = (len + 7) & ~7; /* round by 8 */
2630 /* safety check */
2631 if (len + (p - (u_char *)ip6) > maxlen)
2632 goto nolladdropt;
2633 if (!(rt_nexthop->rt_flags & RTF_GATEWAY) &&
2634 (rt_nexthop->rt_flags & RTF_LLINFO) &&
2635 (rt_nexthop->rt_gateway->sa_family == AF_LINK) &&
2636 (sdl = (struct sockaddr_dl *)rt_nexthop->rt_gateway) &&
2637 sdl->sdl_alen) {
2638 nd_opt = (struct nd_opt_hdr *)p;
2639 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2640 nd_opt->nd_opt_len = len >> 3;
2641 lladdr = (char *)(nd_opt + 1);
2642 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2643 p += len;
2644 }
2645 }
2646 nolladdropt:;
2647
2648 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2649
2650 /* just to be safe */
2651 if (m0->m_flags & M_DECRYPTED)
2652 goto noredhdropt;
2653 if (p - (u_char *)ip6 > maxlen)
2654 goto noredhdropt;
2655
2656 {
2657 /* redirected header option */
2658 int len;
2659 struct nd_opt_rd_hdr *nd_opt_rh;
2660
2661 /*
2662 * compute the maximum size for icmp6 redirect header option.
2663 * XXX room for auth header?
2664 */
2665 len = maxlen - (p - (u_char *)ip6);
2666 len &= ~7;
2667
2668 /* This is just for simplicity. */
2669 if (m0->m_pkthdr.len != m0->m_len) {
2670 if (m0->m_next) {
2671 m_freem(m0->m_next);
2672 m0->m_next = NULL;
2673 }
2674 m0->m_pkthdr.len = m0->m_len;
2675 }
2676
2677 /*
2678 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2679 * about padding/truncate rule for the original IP packet.
2680 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2681 * - "attach as much as possible" is the goal
2682 * - pad if not aligned (original size can be guessed by original
2683 * ip6 header)
2684 * Following code adds the padding if it is simple enough,
2685 * and truncates if not.
2686 */
2687 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2688 panic("assumption failed in %s:%d\n", __FILE__, __LINE__);
2689
2690 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2691 /* not enough room, truncate */
2692 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2693 } else {
2694 /* enough room, pad or truncate */
2695 size_t extra;
2696
2697 extra = m0->m_pkthdr.len % 8;
2698 if (extra) {
2699 /* pad if easy enough, truncate if not */
2700 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2701 /* pad */
2702 m0->m_len += (8 - extra);
2703 m0->m_pkthdr.len += (8 - extra);
2704 } else {
2705 /* truncate */
2706 m0->m_pkthdr.len -= extra;
2707 m0->m_len -= extra;
2708 }
2709 }
2710 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2711 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2712 }
2713
2714 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2715 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2716 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2717 nd_opt_rh->nd_opt_rh_len = len >> 3;
2718 p += sizeof(*nd_opt_rh);
2719 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2720
2721 /* connect m0 to m */
2722 m->m_next = m0;
2723 m->m_pkthdr.len = m->m_len + m0->m_len;
2724 }
2725 noredhdropt:;
2726
2727 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2728 sip6->ip6_src.s6_addr16[1] = 0;
2729 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2730 sip6->ip6_dst.s6_addr16[1] = 0;
2731 #if 0
2732 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2733 ip6->ip6_src.s6_addr16[1] = 0;
2734 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2735 ip6->ip6_dst.s6_addr16[1] = 0;
2736 #endif
2737 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2738 nd_rd->nd_rd_target.s6_addr16[1] = 0;
2739 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2740 nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2741
2742 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2743
2744 nd_rd->nd_rd_cksum = 0;
2745 nd_rd->nd_rd_cksum
2746 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2747
2748 /* send the packet to outside... */
2749 #ifdef IPSEC
2750 /* Don't lookup socket */
2751 (void)ipsec_setsocket(m, NULL);
2752 #endif /* IPSEC */
2753 if (ip6_output(m, NULL, NULL, 0, NULL, NULL) != 0)
2754 icmp6_ifstat_inc(ifp, ifs6_out_error);
2755
2756 icmp6_ifstat_inc(ifp, ifs6_out_msg);
2757 icmp6_ifstat_inc(ifp, ifs6_out_redirect);
2758 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2759
2760 return;
2761
2762 fail:
2763 if (m)
2764 m_freem(m);
2765 if (m0)
2766 m_freem(m0);
2767 }
2768
2769 /*
2770 * ICMPv6 socket option processing.
2771 */
2772 int
2773 icmp6_ctloutput(op, so, level, optname, mp)
2774 int op;
2775 struct socket *so;
2776 int level, optname;
2777 struct mbuf **mp;
2778 {
2779 int error = 0;
2780 int optlen;
2781 struct in6pcb *in6p = sotoin6pcb(so);
2782 struct mbuf *m = *mp;
2783
2784 optlen = m ? m->m_len : 0;
2785
2786 if (level != IPPROTO_ICMPV6) {
2787 if (op == PRCO_SETOPT && m)
2788 (void)m_free(m);
2789 return EINVAL;
2790 }
2791
2792 switch (op) {
2793 case PRCO_SETOPT:
2794 switch (optname) {
2795 case ICMP6_FILTER:
2796 {
2797 struct icmp6_filter *p;
2798
2799 if (optlen != sizeof(*p)) {
2800 error = EMSGSIZE;
2801 break;
2802 }
2803 p = mtod(m, struct icmp6_filter *);
2804 if (!p || !in6p->in6p_icmp6filt) {
2805 error = EINVAL;
2806 break;
2807 }
2808 bcopy(p, in6p->in6p_icmp6filt,
2809 sizeof(struct icmp6_filter));
2810 error = 0;
2811 break;
2812 }
2813
2814 default:
2815 error = ENOPROTOOPT;
2816 break;
2817 }
2818 if (m)
2819 (void)m_freem(m);
2820 break;
2821
2822 case PRCO_GETOPT:
2823 switch (optname) {
2824 case ICMP6_FILTER:
2825 {
2826 struct icmp6_filter *p;
2827
2828 if (!in6p->in6p_icmp6filt) {
2829 error = EINVAL;
2830 break;
2831 }
2832 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2833 m->m_len = sizeof(struct icmp6_filter);
2834 p = mtod(m, struct icmp6_filter *);
2835 bcopy(in6p->in6p_icmp6filt, p,
2836 sizeof(struct icmp6_filter));
2837 error = 0;
2838 break;
2839 }
2840
2841 default:
2842 error = ENOPROTOOPT;
2843 break;
2844 }
2845 break;
2846 }
2847
2848 return(error);
2849 }
2850
2851 /*
2852 * Perform rate limit check.
2853 * Returns 0 if it is okay to send the icmp6 packet.
2854 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2855 * limitation.
2856 *
2857 * XXX per-destination/type check necessary?
2858 */
2859 static int
2860 icmp6_ratelimit(dst, type, code)
2861 const struct in6_addr *dst; /* not used at this moment */
2862 const int type; /* not used at this moment */
2863 const int code; /* not used at this moment */
2864 {
2865 int ret;
2866
2867 ret = 0; /* okay to send */
2868
2869 /* PPS limit */
2870 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2871 icmp6errppslim)) {
2872 /* The packet is subject to rate limit */
2873 ret++;
2874 }
2875
2876 return ret;
2877 }
2878
2879 static struct rtentry *
2880 icmp6_mtudisc_clone(dst)
2881 struct sockaddr *dst;
2882 {
2883 struct rtentry *rt;
2884 int error;
2885
2886 rt = rtalloc1(dst, 1);
2887 if (rt == 0)
2888 return NULL;
2889
2890 /* If we didn't get a host route, allocate one */
2891 if ((rt->rt_flags & RTF_HOST) == 0) {
2892 struct rtentry *nrt;
2893
2894 error = rtrequest((int) RTM_ADD, dst,
2895 (struct sockaddr *) rt->rt_gateway,
2896 (struct sockaddr *) 0,
2897 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
2898 if (error) {
2899 rtfree(rt);
2900 return NULL;
2901 }
2902 nrt->rt_rmx = rt->rt_rmx;
2903 rtfree(rt);
2904 rt = nrt;
2905 }
2906 error = rt_timer_add(rt, icmp6_mtudisc_timeout,
2907 icmp6_mtudisc_timeout_q);
2908 if (error) {
2909 rtfree(rt);
2910 return NULL;
2911 }
2912
2913 return rt; /* caller need to call rtfree() */
2914 }
2915
2916 static void
2917 icmp6_mtudisc_timeout(rt, r)
2918 struct rtentry *rt;
2919 struct rttimer *r;
2920 {
2921 if (rt == NULL)
2922 panic("icmp6_mtudisc_timeout: bad route to timeout");
2923 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
2924 (RTF_DYNAMIC | RTF_HOST)) {
2925 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2926 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2927 } else {
2928 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
2929 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
2930 }
2931 }
2932
2933 static void
2934 icmp6_redirect_timeout(rt, r)
2935 struct rtentry *rt;
2936 struct rttimer *r;
2937 {
2938 if (rt == NULL)
2939 panic("icmp6_redirect_timeout: bad route to timeout");
2940 if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
2941 (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
2942 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2943 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2944 }
2945 }
2946
2947 int
2948 icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
2949 int *name;
2950 u_int namelen;
2951 void *oldp;
2952 size_t *oldlenp;
2953 void *newp;
2954 size_t newlen;
2955 {
2956
2957 /* All sysctl names at this level are terminal. */
2958 if (namelen != 1)
2959 return ENOTDIR;
2960
2961 switch (name[0]) {
2962
2963 case ICMPV6CTL_REDIRACCEPT:
2964 return sysctl_int(oldp, oldlenp, newp, newlen,
2965 &icmp6_rediraccept);
2966 case ICMPV6CTL_REDIRTIMEOUT:
2967 return sysctl_int(oldp, oldlenp, newp, newlen,
2968 &icmp6_redirtimeout);
2969 case ICMPV6CTL_STATS:
2970 return sysctl_rdstruct(oldp, oldlenp, newp,
2971 &icmp6stat, sizeof(icmp6stat));
2972 case ICMPV6CTL_ND6_PRUNE:
2973 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_prune);
2974 case ICMPV6CTL_ND6_DELAY:
2975 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_delay);
2976 case ICMPV6CTL_ND6_UMAXTRIES:
2977 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_umaxtries);
2978 case ICMPV6CTL_ND6_MMAXTRIES:
2979 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_mmaxtries);
2980 case ICMPV6CTL_ND6_USELOOPBACK:
2981 return sysctl_int(oldp, oldlenp, newp, newlen,
2982 &nd6_useloopback);
2983 case ICMPV6CTL_NODEINFO:
2984 return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6_nodeinfo);
2985 case ICMPV6CTL_ERRPPSLIMIT:
2986 return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6errppslim);
2987 case ICMPV6CTL_ND6_MAXNUDHINT:
2988 return sysctl_int(oldp, oldlenp, newp, newlen,
2989 &nd6_maxnudhint);
2990 case ICMPV6CTL_MTUDISC_HIWAT:
2991 return sysctl_int(oldp, oldlenp, newp, newlen,
2992 &icmp6_mtudisc_hiwat);
2993 case ICMPV6CTL_MTUDISC_LOWAT:
2994 return sysctl_int(oldp, oldlenp, newp, newlen,
2995 &icmp6_mtudisc_lowat);
2996 case ICMPV6CTL_ND6_DEBUG:
2997 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_debug);
2998 default:
2999 return ENOPROTOOPT;
3000 }
3001 /* NOTREACHED */
3002 }
3003