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