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