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