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