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