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