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