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