icmp6.c revision 1.92 1 /* $NetBSD: icmp6.c,v 1.92 2003/06/06 08:13:43 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.92 2003/06/06 08:13:43 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 long time_second = time.tv_sec;
1764
1765 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1766 return (0); /* needless to copy */
1767
1768 again:
1769
1770 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1771 {
1772 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1773 ifa = ifa->ifa_list.tqe_next)
1774 {
1775 if (ifa->ifa_addr->sa_family != AF_INET6)
1776 continue;
1777 ifa6 = (struct in6_ifaddr *)ifa;
1778
1779 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1780 allow_deprecated == 0) {
1781 /*
1782 * prefererred address should be put before
1783 * deprecated addresses.
1784 */
1785
1786 /* record the interface for later search */
1787 if (ifp_dep == NULL)
1788 ifp_dep = ifp;
1789
1790 continue;
1791 }
1792 else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1793 allow_deprecated != 0)
1794 continue; /* we now collect deprecated addrs */
1795
1796 /* What do we have to do about ::1? */
1797 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1798 case IPV6_ADDR_SCOPE_LINKLOCAL:
1799 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1800 continue;
1801 break;
1802 case IPV6_ADDR_SCOPE_SITELOCAL:
1803 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1804 continue;
1805 break;
1806 case IPV6_ADDR_SCOPE_GLOBAL:
1807 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1808 continue;
1809 break;
1810 default:
1811 continue;
1812 }
1813
1814 /*
1815 * check if anycast is okay.
1816 * XXX: just experimental. not in the spec.
1817 */
1818 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1819 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1820 continue;
1821
1822 /* now we can copy the address */
1823 if (resid < sizeof(struct in6_addr) +
1824 sizeof(u_int32_t)) {
1825 /*
1826 * We give up much more copy.
1827 * Set the truncate flag and return.
1828 */
1829 nni6->ni_flags |=
1830 NI_NODEADDR_FLAG_TRUNCATE;
1831 return (copied);
1832 }
1833
1834 /*
1835 * Set the TTL of the address.
1836 * The TTL value should be one of the following
1837 * according to the specification:
1838 *
1839 * 1. The remaining lifetime of a DHCP lease on the
1840 * address, or
1841 * 2. The remaining Valid Lifetime of a prefix from
1842 * which the address was derived through Stateless
1843 * Autoconfiguration.
1844 *
1845 * Note that we currently do not support stateful
1846 * address configuration by DHCPv6, so the former
1847 * case can't happen.
1848 *
1849 * TTL must be 2^31 > TTL >= 0.
1850 */
1851 if (ifa6->ia6_lifetime.ia6t_expire == 0)
1852 ltime = ND6_INFINITE_LIFETIME;
1853 else {
1854 if (ifa6->ia6_lifetime.ia6t_expire >
1855 time_second)
1856 ltime = ifa6->ia6_lifetime.ia6t_expire - time_second;
1857 else
1858 ltime = 0;
1859 }
1860 if (ltime > 0x7fffffff)
1861 ltime = 0x7fffffff;
1862 ltime = htonl(ltime);
1863
1864 bcopy(<ime, cp, sizeof(u_int32_t));
1865 cp += sizeof(u_int32_t);
1866
1867 /* copy the address itself */
1868 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1869 sizeof(struct in6_addr));
1870 /* XXX: KAME link-local hack; remove ifindex */
1871 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1872 ((struct in6_addr *)cp)->s6_addr16[1] = 0;
1873 cp += sizeof(struct in6_addr);
1874
1875 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1876 copied += (sizeof(struct in6_addr) +
1877 sizeof(u_int32_t));
1878 }
1879 if (ifp0) /* we need search only on the specified IF */
1880 break;
1881 }
1882
1883 if (allow_deprecated == 0 && ifp_dep != NULL) {
1884 ifp = ifp_dep;
1885 allow_deprecated = 1;
1886
1887 goto again;
1888 }
1889
1890 return (copied);
1891 }
1892
1893 /*
1894 * XXX almost dup'ed code with rip6_input.
1895 */
1896 static int
1897 icmp6_rip6_input(mp, off)
1898 struct mbuf **mp;
1899 int off;
1900 {
1901 struct mbuf *m = *mp;
1902 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1903 struct in6pcb *in6p;
1904 struct in6pcb *last = NULL;
1905 struct sockaddr_in6 rip6src;
1906 struct icmp6_hdr *icmp6;
1907 struct mbuf *opts = NULL;
1908
1909 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1910 if (icmp6 == NULL) {
1911 /* m is already reclaimed */
1912 return IPPROTO_DONE;
1913 }
1914
1915 bzero(&rip6src, sizeof(rip6src));
1916 rip6src.sin6_len = sizeof(struct sockaddr_in6);
1917 rip6src.sin6_family = AF_INET6;
1918 /* KAME hack: recover scopeid */
1919 (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1920
1921 for (in6p = rawin6pcb.in6p_next;
1922 in6p != &rawin6pcb; in6p = in6p->in6p_next)
1923 {
1924 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1925 continue;
1926 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1927 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1928 continue;
1929 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1930 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1931 continue;
1932 if (in6p->in6p_icmp6filt
1933 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1934 in6p->in6p_icmp6filt))
1935 continue;
1936 if (last) {
1937 struct mbuf *n;
1938 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1939 if (last->in6p_flags & IN6P_CONTROLOPTS)
1940 ip6_savecontrol(last, &opts, ip6, n);
1941 /* strip intermediate headers */
1942 m_adj(n, off);
1943 if (sbappendaddr(&last->in6p_socket->so_rcv,
1944 (struct sockaddr *)&rip6src,
1945 n, opts) == 0) {
1946 /* should notify about lost packet */
1947 m_freem(n);
1948 if (opts)
1949 m_freem(opts);
1950 } else
1951 sorwakeup(last->in6p_socket);
1952 opts = NULL;
1953 }
1954 }
1955 last = in6p;
1956 }
1957 if (last) {
1958 if (last->in6p_flags & IN6P_CONTROLOPTS)
1959 ip6_savecontrol(last, &opts, ip6, m);
1960 /* strip intermediate headers */
1961 m_adj(m, off);
1962 if (sbappendaddr(&last->in6p_socket->so_rcv,
1963 (struct sockaddr *)&rip6src, m, opts) == 0) {
1964 m_freem(m);
1965 if (opts)
1966 m_freem(opts);
1967 } else
1968 sorwakeup(last->in6p_socket);
1969 } else {
1970 m_freem(m);
1971 ip6stat.ip6s_delivered--;
1972 }
1973 return IPPROTO_DONE;
1974 }
1975
1976 /*
1977 * Reflect the ip6 packet back to the source.
1978 * OFF points to the icmp6 header, counted from the top of the mbuf.
1979 *
1980 * Note: RFC 1885 required that an echo reply should be truncated if it
1981 * did not fit in with (return) path MTU, and KAME code supported the
1982 * behavior. However, as a clarification after the RFC, this limitation
1983 * was removed in a revised version of the spec, RFC 2463. We had kept the
1984 * old behavior, with a (non-default) ifdef block, while the new version of
1985 * the spec was an internet-draft status, and even after the new RFC was
1986 * published. But it would rather make sense to clean the obsoleted part
1987 * up, and to make the code simpler at this stage.
1988 */
1989 void
1990 icmp6_reflect(m, off)
1991 struct mbuf *m;
1992 size_t off;
1993 {
1994 struct ip6_hdr *ip6;
1995 struct icmp6_hdr *icmp6;
1996 struct in6_ifaddr *ia;
1997 struct in6_addr t, *src = 0;
1998 int plen;
1999 int type, code;
2000 struct ifnet *outif = NULL;
2001 struct sockaddr_in6 sa6_src, sa6_dst;
2002
2003 /* too short to reflect */
2004 if (off < sizeof(struct ip6_hdr)) {
2005 nd6log((LOG_DEBUG,
2006 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2007 (u_long)off, (u_long)sizeof(struct ip6_hdr),
2008 __FILE__, __LINE__));
2009 goto bad;
2010 }
2011
2012 /*
2013 * If there are extra headers between IPv6 and ICMPv6, strip
2014 * off that header first.
2015 */
2016 #ifdef DIAGNOSTIC
2017 if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2018 panic("assumption failed in icmp6_reflect");
2019 #endif
2020 if (off > sizeof(struct ip6_hdr)) {
2021 size_t l;
2022 struct ip6_hdr nip6;
2023
2024 l = off - sizeof(struct ip6_hdr);
2025 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2026 m_adj(m, l);
2027 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2028 if (m->m_len < l) {
2029 if ((m = m_pullup(m, l)) == NULL)
2030 return;
2031 }
2032 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2033 } else /* off == sizeof(struct ip6_hdr) */ {
2034 size_t l;
2035 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2036 if (m->m_len < l) {
2037 if ((m = m_pullup(m, l)) == NULL)
2038 return;
2039 }
2040 }
2041 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2042 ip6 = mtod(m, struct ip6_hdr *);
2043 ip6->ip6_nxt = IPPROTO_ICMPV6;
2044 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2045 type = icmp6->icmp6_type; /* keep type for statistics */
2046 code = icmp6->icmp6_code; /* ditto. */
2047
2048 t = ip6->ip6_dst;
2049 /*
2050 * ip6_input() drops a packet if its src is multicast.
2051 * So, the src is never multicast.
2052 */
2053 ip6->ip6_dst = ip6->ip6_src;
2054
2055 /*
2056 * XXX: make sure to embed scope zone information, using
2057 * already embedded IDs or the received interface (if any).
2058 * Note that rcvif may be NULL.
2059 * TODO: scoped routing case (XXX).
2060 */
2061 bzero(&sa6_src, sizeof(sa6_src));
2062 sa6_src.sin6_family = AF_INET6;
2063 sa6_src.sin6_len = sizeof(sa6_src);
2064 sa6_src.sin6_addr = ip6->ip6_dst;
2065 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2066 in6_embedscope(&sa6_src.sin6_addr, &sa6_src, NULL, NULL);
2067 ip6->ip6_dst = sa6_src.sin6_addr;
2068
2069 bzero(&sa6_dst, sizeof(sa6_dst));
2070 sa6_dst.sin6_family = AF_INET6;
2071 sa6_dst.sin6_len = sizeof(sa6_dst);
2072 sa6_dst.sin6_addr = t;
2073 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2074 in6_embedscope(&t, &sa6_dst, NULL, NULL);
2075
2076 /*
2077 * If the incoming packet was addressed directly to us (i.e. unicast),
2078 * use dst as the src for the reply.
2079 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2080 * (for example) when we encounter an error while forwarding procedure
2081 * destined to a duplicated address of ours.
2082 */
2083 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2084 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2085 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2086 src = &t;
2087 break;
2088 }
2089 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2090 /*
2091 * This is the case if the dst is our link-local address
2092 * and the sender is also ourselves.
2093 */
2094 src = &t;
2095 }
2096
2097 if (src == 0) {
2098 int e;
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(&ro, sizeof(ro));
2107 src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e);
2108 if (ro.ro_rt) { /* XXX: see comments in icmp6_mtudisc_update */
2109 RTFREE(ro.ro_rt); /* XXX: we could use this */
2110 }
2111 if (src == NULL) {
2112 nd6log((LOG_DEBUG,
2113 "icmp6_reflect: source can't be determined: "
2114 "dst=%s, error=%d\n",
2115 ip6_sprintf(&sa6_src.sin6_addr), e));
2116 goto bad;
2117 }
2118 }
2119
2120 ip6->ip6_src = *src;
2121
2122 ip6->ip6_flow = 0;
2123 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2124 ip6->ip6_vfc |= IPV6_VERSION;
2125 ip6->ip6_nxt = IPPROTO_ICMPV6;
2126 if (m->m_pkthdr.rcvif) {
2127 /* XXX: This may not be the outgoing interface */
2128 ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2129 } else
2130 ip6->ip6_hlim = ip6_defhlim;
2131
2132 icmp6->icmp6_cksum = 0;
2133 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2134 sizeof(struct ip6_hdr), plen);
2135
2136 /*
2137 * XXX option handling
2138 */
2139
2140 m->m_flags &= ~(M_BCAST|M_MCAST);
2141 #ifdef IPSEC
2142 /* Don't lookup socket */
2143 (void)ipsec_setsocket(m, NULL);
2144 #endif /* IPSEC */
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, NULL, &outif) != 0 && outif)
2153 icmp6_ifstat_inc(outif, ifs6_out_error);
2154
2155 if (outif)
2156 icmp6_ifoutstat_inc(outif, type, code);
2157
2158 return;
2159
2160 bad:
2161 m_freem(m);
2162 return;
2163 }
2164
2165 void
2166 icmp6_fasttimo()
2167 {
2168
2169 mld6_fasttimeo();
2170 }
2171
2172 static const char *
2173 icmp6_redirect_diag(src6, dst6, tgt6)
2174 struct in6_addr *src6;
2175 struct in6_addr *dst6;
2176 struct in6_addr *tgt6;
2177 {
2178 static char buf[1024];
2179 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2180 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2181 return buf;
2182 }
2183
2184 void
2185 icmp6_redirect_input(m, off)
2186 struct mbuf *m;
2187 int off;
2188 {
2189 struct ifnet *ifp = m->m_pkthdr.rcvif;
2190 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2191 struct nd_redirect *nd_rd;
2192 int icmp6len = ntohs(ip6->ip6_plen);
2193 char *lladdr = NULL;
2194 int lladdrlen = 0;
2195 u_char *redirhdr = NULL;
2196 int redirhdrlen = 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_IS_ADDR_LINKLOCAL(&redtgt6))
2223 redtgt6.s6_addr16[1] = htons(ifp->if_index);
2224 if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2225 reddst6.s6_addr16[1] = htons(ifp->if_index);
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 (ndopts.nd_opts_rh) {
2321 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2322 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2323 }
2324
2325 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2326 nd6log((LOG_INFO,
2327 "icmp6_redirect_input: lladdrlen mismatch for %s "
2328 "(if %d, icmp6 packet %d): %s\n",
2329 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2330 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2331 goto bad;
2332 }
2333
2334 /* RFC 2461 8.3 */
2335 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2336 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2337
2338 if (!is_onlink) { /* better router case. perform rtredirect. */
2339 /* perform rtredirect */
2340 struct sockaddr_in6 sdst;
2341 struct sockaddr_in6 sgw;
2342 struct sockaddr_in6 ssrc;
2343 unsigned long rtcount;
2344 struct rtentry *newrt = NULL;
2345
2346 /*
2347 * do not install redirect route, if the number of entries
2348 * is too much (> hiwat). note that, the node (= host) will
2349 * work just fine even if we do not install redirect route
2350 * (there will be additional hops, though).
2351 */
2352 rtcount = rt_timer_count(icmp6_redirect_timeout_q);
2353 if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat)
2354 return;
2355 else if (0 <= icmp6_redirect_lowat &&
2356 rtcount > icmp6_redirect_lowat) {
2357 /*
2358 * XXX nuke a victim, install the new one.
2359 */
2360 }
2361
2362 bzero(&sdst, sizeof(sdst));
2363 bzero(&sgw, sizeof(sgw));
2364 bzero(&ssrc, sizeof(ssrc));
2365 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2366 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2367 sizeof(struct sockaddr_in6);
2368 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2369 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2370 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2371 rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2372 (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2373 (struct sockaddr *)&ssrc,
2374 &newrt);
2375
2376 if (newrt) {
2377 (void)rt_timer_add(newrt, icmp6_redirect_timeout,
2378 icmp6_redirect_timeout_q);
2379 rtfree(newrt);
2380 }
2381 }
2382 /* finally update cached route in each socket via pfctlinput */
2383 {
2384 struct sockaddr_in6 sdst;
2385
2386 bzero(&sdst, sizeof(sdst));
2387 sdst.sin6_family = AF_INET6;
2388 sdst.sin6_len = sizeof(struct sockaddr_in6);
2389 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2390 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2391 #ifdef IPSEC
2392 key_sa_routechange((struct sockaddr *)&sdst);
2393 #endif
2394 }
2395
2396 freeit:
2397 m_freem(m);
2398 return;
2399
2400 bad:
2401 icmp6stat.icp6s_badredirect++;
2402 m_freem(m);
2403 }
2404
2405 void
2406 icmp6_redirect_output(m0, rt)
2407 struct mbuf *m0;
2408 struct rtentry *rt;
2409 {
2410 struct ifnet *ifp; /* my outgoing interface */
2411 struct in6_addr *ifp_ll6;
2412 struct in6_addr *nexthop;
2413 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2414 struct mbuf *m = NULL; /* newly allocated one */
2415 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2416 struct nd_redirect *nd_rd;
2417 size_t maxlen;
2418 u_char *p;
2419 struct sockaddr_in6 src_sa;
2420
2421 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2422
2423 /* if we are not router, we don't send icmp6 redirect */
2424 if (!ip6_forwarding || ip6_accept_rtadv)
2425 goto fail;
2426
2427 /* sanity check */
2428 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2429 goto fail;
2430
2431 /*
2432 * Address check:
2433 * the source address must identify a neighbor, and
2434 * the destination address must not be a multicast address
2435 * [RFC 2461, sec 8.2]
2436 */
2437 sip6 = mtod(m0, struct ip6_hdr *);
2438 bzero(&src_sa, sizeof(src_sa));
2439 src_sa.sin6_family = AF_INET6;
2440 src_sa.sin6_len = sizeof(src_sa);
2441 src_sa.sin6_addr = sip6->ip6_src;
2442 /* we don't currently use sin6_scope_id, but eventually use it */
2443 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2444 if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2445 goto fail;
2446 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2447 goto fail; /* what should we do here? */
2448
2449 /* rate limit */
2450 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2451 goto fail;
2452
2453 /*
2454 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2455 * we almost always ask for an mbuf cluster for simplicity.
2456 * (MHLEN < IPV6_MMTU is almost always true)
2457 */
2458 #if IPV6_MMTU >= MCLBYTES
2459 # error assumption failed about IPV6_MMTU and MCLBYTES
2460 #endif
2461 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2462 if (m && IPV6_MMTU >= MHLEN)
2463 MCLGET(m, M_DONTWAIT);
2464 if (!m)
2465 goto fail;
2466 m->m_pkthdr.rcvif = NULL;
2467 m->m_len = 0;
2468 maxlen = M_TRAILINGSPACE(m);
2469 maxlen = min(IPV6_MMTU, maxlen);
2470 /* just for safety */
2471 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2472 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2473 goto fail;
2474 }
2475
2476 {
2477 /* get ip6 linklocal address for ifp(my outgoing interface). */
2478 struct in6_ifaddr *ia;
2479 if ((ia = in6ifa_ifpforlinklocal(ifp,
2480 IN6_IFF_NOTREADY|
2481 IN6_IFF_ANYCAST)) == NULL)
2482 goto fail;
2483 ifp_ll6 = &ia->ia_addr.sin6_addr;
2484 }
2485
2486 /* get ip6 linklocal address for the router. */
2487 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2488 struct sockaddr_in6 *sin6;
2489 sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2490 nexthop = &sin6->sin6_addr;
2491 if (!IN6_IS_ADDR_LINKLOCAL(nexthop))
2492 nexthop = NULL;
2493 } else
2494 nexthop = NULL;
2495
2496 /* ip6 */
2497 ip6 = mtod(m, struct ip6_hdr *);
2498 ip6->ip6_flow = 0;
2499 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2500 ip6->ip6_vfc |= IPV6_VERSION;
2501 /* ip6->ip6_plen will be set later */
2502 ip6->ip6_nxt = IPPROTO_ICMPV6;
2503 ip6->ip6_hlim = 255;
2504 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2505 bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2506 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2507
2508 /* ND Redirect */
2509 nd_rd = (struct nd_redirect *)(ip6 + 1);
2510 nd_rd->nd_rd_type = ND_REDIRECT;
2511 nd_rd->nd_rd_code = 0;
2512 nd_rd->nd_rd_reserved = 0;
2513 if (rt->rt_flags & RTF_GATEWAY) {
2514 /*
2515 * nd_rd->nd_rd_target must be a link-local address in
2516 * better router cases.
2517 */
2518 if (!nexthop)
2519 goto fail;
2520 bcopy(nexthop, &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 } else {
2525 /* make sure redtgt == reddst */
2526 nexthop = &sip6->ip6_dst;
2527 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2528 sizeof(nd_rd->nd_rd_target));
2529 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2530 sizeof(nd_rd->nd_rd_dst));
2531 }
2532
2533 p = (u_char *)(nd_rd + 1);
2534
2535 {
2536 /* target lladdr option */
2537 struct rtentry *rt_nexthop = NULL;
2538 int len;
2539 struct sockaddr_dl *sdl;
2540 struct nd_opt_hdr *nd_opt;
2541 char *lladdr;
2542
2543 rt_nexthop = nd6_lookup(nexthop, 0, ifp);
2544 if (!rt_nexthop)
2545 goto nolladdropt;
2546 len = sizeof(*nd_opt) + ifp->if_addrlen;
2547 len = (len + 7) & ~7; /* round by 8 */
2548 /* safety check */
2549 if (len + (p - (u_char *)ip6) > maxlen)
2550 goto nolladdropt;
2551 if (!(rt_nexthop->rt_flags & RTF_GATEWAY) &&
2552 (rt_nexthop->rt_flags & RTF_LLINFO) &&
2553 (rt_nexthop->rt_gateway->sa_family == AF_LINK) &&
2554 (sdl = (struct sockaddr_dl *)rt_nexthop->rt_gateway) &&
2555 sdl->sdl_alen) {
2556 nd_opt = (struct nd_opt_hdr *)p;
2557 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2558 nd_opt->nd_opt_len = len >> 3;
2559 lladdr = (char *)(nd_opt + 1);
2560 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2561 p += len;
2562 }
2563 }
2564 nolladdropt:;
2565
2566 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2567
2568 /* just to be safe */
2569 if (m0->m_flags & M_DECRYPTED)
2570 goto noredhdropt;
2571 if (p - (u_char *)ip6 > maxlen)
2572 goto noredhdropt;
2573
2574 {
2575 /* redirected header option */
2576 int len;
2577 struct nd_opt_rd_hdr *nd_opt_rh;
2578
2579 /*
2580 * compute the maximum size for icmp6 redirect header option.
2581 * XXX room for auth header?
2582 */
2583 len = maxlen - (p - (u_char *)ip6);
2584 len &= ~7;
2585
2586 /*
2587 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2588 * about padding/truncate rule for the original IP packet.
2589 * From the discussion on IPv6imp in Feb 1999,
2590 * the consensus was:
2591 * - "attach as much as possible" is the goal
2592 * - pad if not aligned (original size can be guessed by
2593 * original ip6 header)
2594 * Following code adds the padding if it is simple enough,
2595 * and truncates if not.
2596 */
2597 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2598 /* not enough room, truncate */
2599 m_adj(m0, (len - sizeof(*nd_opt_rh)) -
2600 m0->m_pkthdr.len);
2601 } else {
2602 /*
2603 * enough room, truncate if not aligned.
2604 * we don't pad here for simplicity.
2605 */
2606 size_t extra;
2607
2608 extra = m0->m_pkthdr.len % 8;
2609 if (extra) {
2610 /* truncate */
2611 m_adj(m0, -extra);
2612 }
2613 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2614 }
2615
2616 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2617 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2618 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2619 nd_opt_rh->nd_opt_rh_len = len >> 3;
2620 p += sizeof(*nd_opt_rh);
2621 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2622
2623 /* connect m0 to m */
2624 m_cat(m, m0);
2625 m->m_pkthdr.len += m0->m_pkthdr.len;
2626 m0 = NULL;
2627 }
2628 noredhdropt:
2629 if (m0) {
2630 m_freem(m0);
2631 m0 = NULL;
2632 }
2633
2634 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2635 sip6->ip6_src.s6_addr16[1] = 0;
2636 if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2637 sip6->ip6_dst.s6_addr16[1] = 0;
2638 #if 0
2639 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2640 ip6->ip6_src.s6_addr16[1] = 0;
2641 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2642 ip6->ip6_dst.s6_addr16[1] = 0;
2643 #endif
2644 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2645 nd_rd->nd_rd_target.s6_addr16[1] = 0;
2646 if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2647 nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2648
2649 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2650
2651 nd_rd->nd_rd_cksum = 0;
2652 nd_rd->nd_rd_cksum
2653 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2654
2655 /* send the packet to outside... */
2656 #ifdef IPSEC
2657 /* Don't lookup socket */
2658 (void)ipsec_setsocket(m, NULL);
2659 #endif /* IPSEC */
2660 if (ip6_output(m, NULL, NULL, 0, NULL, NULL) != 0)
2661 icmp6_ifstat_inc(ifp, ifs6_out_error);
2662
2663 icmp6_ifstat_inc(ifp, ifs6_out_msg);
2664 icmp6_ifstat_inc(ifp, ifs6_out_redirect);
2665 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2666
2667 return;
2668
2669 fail:
2670 if (m)
2671 m_freem(m);
2672 if (m0)
2673 m_freem(m0);
2674 }
2675
2676 /*
2677 * ICMPv6 socket option processing.
2678 */
2679 int
2680 icmp6_ctloutput(op, so, level, optname, mp)
2681 int op;
2682 struct socket *so;
2683 int level, optname;
2684 struct mbuf **mp;
2685 {
2686 int error = 0;
2687 int optlen;
2688 struct in6pcb *in6p = sotoin6pcb(so);
2689 struct mbuf *m = *mp;
2690
2691 optlen = m ? m->m_len : 0;
2692
2693 if (level != IPPROTO_ICMPV6) {
2694 if (op == PRCO_SETOPT && m)
2695 (void)m_free(m);
2696 return EINVAL;
2697 }
2698
2699 switch (op) {
2700 case PRCO_SETOPT:
2701 switch (optname) {
2702 case ICMP6_FILTER:
2703 {
2704 struct icmp6_filter *p;
2705
2706 if (optlen != sizeof(*p)) {
2707 error = EMSGSIZE;
2708 break;
2709 }
2710 p = mtod(m, struct icmp6_filter *);
2711 if (!p || !in6p->in6p_icmp6filt) {
2712 error = EINVAL;
2713 break;
2714 }
2715 bcopy(p, in6p->in6p_icmp6filt,
2716 sizeof(struct icmp6_filter));
2717 error = 0;
2718 break;
2719 }
2720
2721 default:
2722 error = ENOPROTOOPT;
2723 break;
2724 }
2725 if (m)
2726 (void)m_freem(m);
2727 break;
2728
2729 case PRCO_GETOPT:
2730 switch (optname) {
2731 case ICMP6_FILTER:
2732 {
2733 struct icmp6_filter *p;
2734
2735 if (!in6p->in6p_icmp6filt) {
2736 error = EINVAL;
2737 break;
2738 }
2739 *mp = m = m_get(M_WAIT, MT_SOOPTS);
2740 m->m_len = sizeof(struct icmp6_filter);
2741 p = mtod(m, struct icmp6_filter *);
2742 bcopy(in6p->in6p_icmp6filt, p,
2743 sizeof(struct icmp6_filter));
2744 error = 0;
2745 break;
2746 }
2747
2748 default:
2749 error = ENOPROTOOPT;
2750 break;
2751 }
2752 break;
2753 }
2754
2755 return (error);
2756 }
2757
2758 /*
2759 * Perform rate limit check.
2760 * Returns 0 if it is okay to send the icmp6 packet.
2761 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2762 * limitation.
2763 *
2764 * XXX per-destination/type check necessary?
2765 */
2766 static int
2767 icmp6_ratelimit(dst, type, code)
2768 const struct in6_addr *dst; /* not used at this moment */
2769 const int type; /* not used at this moment */
2770 const int code; /* not used at this moment */
2771 {
2772 int ret;
2773
2774 ret = 0; /* okay to send */
2775
2776 /* PPS limit */
2777 if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2778 icmp6errppslim)) {
2779 /* The packet is subject to rate limit */
2780 ret++;
2781 }
2782
2783 return ret;
2784 }
2785
2786 static struct rtentry *
2787 icmp6_mtudisc_clone(dst)
2788 struct sockaddr *dst;
2789 {
2790 struct rtentry *rt;
2791 int error;
2792
2793 rt = rtalloc1(dst, 1);
2794 if (rt == 0)
2795 return NULL;
2796
2797 /* If we didn't get a host route, allocate one */
2798 if ((rt->rt_flags & RTF_HOST) == 0) {
2799 struct rtentry *nrt;
2800
2801 error = rtrequest((int) RTM_ADD, dst,
2802 (struct sockaddr *) rt->rt_gateway,
2803 (struct sockaddr *) 0,
2804 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
2805 if (error) {
2806 rtfree(rt);
2807 return NULL;
2808 }
2809 nrt->rt_rmx = rt->rt_rmx;
2810 rtfree(rt);
2811 rt = nrt;
2812 }
2813 error = rt_timer_add(rt, icmp6_mtudisc_timeout,
2814 icmp6_mtudisc_timeout_q);
2815 if (error) {
2816 rtfree(rt);
2817 return NULL;
2818 }
2819
2820 return rt; /* caller need to call rtfree() */
2821 }
2822
2823 static void
2824 icmp6_mtudisc_timeout(rt, r)
2825 struct rtentry *rt;
2826 struct rttimer *r;
2827 {
2828 if (rt == NULL)
2829 panic("icmp6_mtudisc_timeout: bad route to timeout");
2830 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
2831 (RTF_DYNAMIC | RTF_HOST)) {
2832 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2833 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2834 } else {
2835 if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
2836 rt->rt_rmx.rmx_mtu = 0;
2837 }
2838 }
2839
2840 static void
2841 icmp6_redirect_timeout(rt, r)
2842 struct rtentry *rt;
2843 struct rttimer *r;
2844 {
2845 if (rt == NULL)
2846 panic("icmp6_redirect_timeout: bad route to timeout");
2847 if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
2848 (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
2849 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2850 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2851 }
2852 }
2853
2854 int
2855 icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
2856 int *name;
2857 u_int namelen;
2858 void *oldp;
2859 size_t *oldlenp;
2860 void *newp;
2861 size_t newlen;
2862 {
2863
2864 /* All sysctl names at this level are terminal. */
2865 if (namelen != 1)
2866 return ENOTDIR;
2867
2868 switch (name[0]) {
2869
2870 case ICMPV6CTL_REDIRACCEPT:
2871 return sysctl_int(oldp, oldlenp, newp, newlen,
2872 &icmp6_rediraccept);
2873 case ICMPV6CTL_REDIRTIMEOUT:
2874 return sysctl_int(oldp, oldlenp, newp, newlen,
2875 &icmp6_redirtimeout);
2876 case ICMPV6CTL_STATS:
2877 return sysctl_rdstruct(oldp, oldlenp, newp,
2878 &icmp6stat, sizeof(icmp6stat));
2879 case ICMPV6CTL_ND6_PRUNE:
2880 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_prune);
2881 case ICMPV6CTL_ND6_DELAY:
2882 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_delay);
2883 case ICMPV6CTL_ND6_UMAXTRIES:
2884 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_umaxtries);
2885 case ICMPV6CTL_ND6_MMAXTRIES:
2886 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_mmaxtries);
2887 case ICMPV6CTL_ND6_USELOOPBACK:
2888 return sysctl_int(oldp, oldlenp, newp, newlen,
2889 &nd6_useloopback);
2890 case ICMPV6CTL_NODEINFO:
2891 return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6_nodeinfo);
2892 case ICMPV6CTL_ERRPPSLIMIT:
2893 return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6errppslim);
2894 case ICMPV6CTL_ND6_MAXNUDHINT:
2895 return sysctl_int(oldp, oldlenp, newp, newlen,
2896 &nd6_maxnudhint);
2897 case ICMPV6CTL_MTUDISC_HIWAT:
2898 return sysctl_int(oldp, oldlenp, newp, newlen,
2899 &icmp6_mtudisc_hiwat);
2900 case ICMPV6CTL_MTUDISC_LOWAT:
2901 return sysctl_int(oldp, oldlenp, newp, newlen,
2902 &icmp6_mtudisc_lowat);
2903 case ICMPV6CTL_ND6_DEBUG:
2904 return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_debug);
2905 case ICMPV6CTL_ND6_DRLIST:
2906 case ICMPV6CTL_ND6_PRLIST:
2907 return nd6_sysctl(name[0], oldp, oldlenp, newp, newlen);
2908 default:
2909 return ENOPROTOOPT;
2910 }
2911 /* NOTREACHED */
2912 }
2913