]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/icmp6.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / bsd / netinet6 / icmp6.c
1 /*
2 * Copyright (c) 2000-2016 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(struct icmp6errstat *stat, int type, int code)
194 {
195 switch (type) {
196 case ICMP6_DST_UNREACH:
197 switch (code) {
198 case ICMP6_DST_UNREACH_NOROUTE:
199 stat->icp6errs_dst_unreach_noroute++;
200 return;
201 case ICMP6_DST_UNREACH_ADMIN:
202 stat->icp6errs_dst_unreach_admin++;
203 return;
204 case ICMP6_DST_UNREACH_BEYONDSCOPE:
205 stat->icp6errs_dst_unreach_beyondscope++;
206 return;
207 case ICMP6_DST_UNREACH_ADDR:
208 stat->icp6errs_dst_unreach_addr++;
209 return;
210 case ICMP6_DST_UNREACH_NOPORT:
211 stat->icp6errs_dst_unreach_noport++;
212 return;
213 }
214 break;
215 case ICMP6_PACKET_TOO_BIG:
216 stat->icp6errs_packet_too_big++;
217 return;
218 case ICMP6_TIME_EXCEEDED:
219 switch (code) {
220 case ICMP6_TIME_EXCEED_TRANSIT:
221 stat->icp6errs_time_exceed_transit++;
222 return;
223 case ICMP6_TIME_EXCEED_REASSEMBLY:
224 stat->icp6errs_time_exceed_reassembly++;
225 return;
226 }
227 break;
228 case ICMP6_PARAM_PROB:
229 switch (code) {
230 case ICMP6_PARAMPROB_HEADER:
231 stat->icp6errs_paramprob_header++;
232 return;
233 case ICMP6_PARAMPROB_NEXTHEADER:
234 stat->icp6errs_paramprob_nextheader++;
235 return;
236 case ICMP6_PARAMPROB_OPTION:
237 stat->icp6errs_paramprob_option++;
238 return;
239 }
240 break;
241 case ND_REDIRECT:
242 stat->icp6errs_redirect++;
243 return;
244 }
245 stat->icp6errs_unknown++;
246 }
247
248 /*
249 * A wrapper function for icmp6_error() necessary when the erroneous packet
250 * may not contain enough scope zone information.
251 */
252 void
253 icmp6_error2(struct mbuf *m, int type, int code, int param,
254 struct ifnet *ifp)
255 {
256 struct ip6_hdr *ip6;
257
258 if (ifp == NULL)
259 return;
260
261 #ifndef PULLDOWN_TEST
262 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr),return );
263 #else
264 if (m->m_len < sizeof(struct ip6_hdr)) {
265 m = m_pullup(m, sizeof(struct ip6_hdr));
266 if (m == NULL)
267 return;
268 }
269 #endif
270
271 ip6 = mtod(m, struct ip6_hdr *);
272
273 if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
274 return;
275 if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
276 return;
277
278 icmp6_error(m, type, code, param);
279 }
280
281 /*
282 * Generate an error packet of type error in response to bad IP6 packet.
283 */
284 void
285 icmp6_error(struct mbuf *m, int type, int code, int param) {
286 icmp6_error_flag(m, type, code, param, ICMP6_ERROR_RST_MRCVIF);
287 }
288
289 void icmp6_error_flag (struct mbuf *m, int type, int code, int param, int flags)
290 {
291 struct ip6_hdr *oip6, *nip6;
292 struct icmp6_hdr *icmp6;
293 u_int preplen;
294 int off;
295 int nxt;
296
297 icmp6stat.icp6s_error++;
298
299 /* count per-type-code statistics */
300 icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
301
302 #ifdef M_DECRYPTED /*not openbsd*/
303 if (m->m_flags & M_DECRYPTED) {
304 icmp6stat.icp6s_canterror++;
305 goto freeit;
306 }
307 #endif
308
309 #ifndef PULLDOWN_TEST
310 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), return);
311 #else
312 if (m->m_len < sizeof(struct ip6_hdr)) {
313 m = m_pullup(m, sizeof(struct ip6_hdr));
314 if (m == NULL)
315 return;
316 }
317 #endif
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 #ifndef PULLDOWN_TEST
356 IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), return);
357 icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
358 #else
359 IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
360 sizeof(*icp));
361 if (icp == NULL) {
362 icmp6stat.icp6s_tooshort++;
363 return;
364 }
365 #endif
366 if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
367 icp->icmp6_type == ND_REDIRECT) {
368 /*
369 * ICMPv6 error
370 * Special case: for redirect (which is
371 * informational) we must not send icmp6 error.
372 */
373 icmp6stat.icp6s_canterror++;
374 goto freeit;
375 } else {
376 /* ICMPv6 informational - send the error */
377 }
378 } else {
379 /* non-ICMPv6 - send the error */
380 }
381
382 oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
383
384 /* Finally, do rate limitation check. */
385 if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
386 icmp6stat.icp6s_toofreq++;
387 goto freeit;
388 }
389
390 /*
391 * OK, ICMP6 can be generated.
392 */
393
394 if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
395 m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
396
397 preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
398 M_PREPEND(m, preplen, M_DONTWAIT, 1);
399 if (m && m->m_len < preplen)
400 m = m_pullup(m, preplen);
401 if (m == NULL) {
402 nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
403 return;
404 }
405
406 nip6 = mtod(m, struct ip6_hdr *);
407 nip6->ip6_src = oip6->ip6_src;
408 nip6->ip6_dst = oip6->ip6_dst;
409
410 in6_clearscope(&oip6->ip6_src);
411 in6_clearscope(&oip6->ip6_dst);
412
413 icmp6 = (struct icmp6_hdr *)(nip6 + 1);
414 icmp6->icmp6_type = type;
415 icmp6->icmp6_code = code;
416 icmp6->icmp6_pptr = htonl((u_int32_t)param);
417
418 /*
419 * icmp6_reflect() is designed to be in the input path.
420 * icmp6_error() can be called from both input and output path,
421 * and if we are in output path rcvif could contain bogus value.
422 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
423 * information in ip header (nip6).
424 */
425 if (flags & ICMP6_ERROR_RST_MRCVIF) {
426 m->m_pkthdr.rcvif = NULL;
427 }
428
429 icmp6stat.icp6s_outhist[type]++;
430 icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
431
432 return;
433
434 freeit:
435 /*
436 * If we can't tell whether or not we can generate ICMP6, free it.
437 */
438 m_freem(m);
439 }
440
441 /*
442 * Process a received ICMP6 message.
443 */
444 int
445 icmp6_input(struct mbuf **mp, int *offp, int proto)
446 {
447 #pragma unused(proto)
448 struct mbuf *m = *mp, *n;
449 struct ifnet *ifp;
450 struct ip6_hdr *ip6, *nip6;
451 struct icmp6_hdr *icmp6, *nicmp6;
452 int off = *offp;
453 int icmp6len = m->m_pkthdr.len - *offp;
454 int code, sum, noff, proxy = 0;
455
456 ifp = m->m_pkthdr.rcvif;
457
458 #ifndef PULLDOWN_TEST
459 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), return IPPROTO_DONE);
460 /* m might change if M_LOOP. So, call mtod after this */
461 #endif
462
463 /* Expect 32-bit aligned data pointer on strict-align platforms */
464 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
465
466 /*
467 * Locate icmp6 structure in mbuf, and check
468 * that not corrupted and of at least minimum length
469 */
470 ip6 = mtod(m, struct ip6_hdr *);
471 if (icmp6len < sizeof(struct icmp6_hdr)) {
472 icmp6stat.icp6s_tooshort++;
473 goto freeit;
474 }
475
476 #ifndef PULLDOWN_TEST
477 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
478 #else
479 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
480 if (icmp6 == NULL) {
481 icmp6stat.icp6s_tooshort++;
482 return IPPROTO_DONE;
483 }
484 #endif
485 code = icmp6->icmp6_code;
486
487 /*
488 * Early check for RFC 6980
489 * Drop certain NDP packets if they came in fragmented
490 */
491 switch (icmp6->icmp6_type) {
492 case ND_ROUTER_SOLICIT:
493 case ND_ROUTER_ADVERT:
494 case ND_NEIGHBOR_SOLICIT:
495 case ND_NEIGHBOR_ADVERT:
496 case ND_REDIRECT:
497 if (m->m_pkthdr.pkt_flags & PKTF_REASSEMBLED) {
498 icmp6stat.icp6s_rfc6980_drop++;
499 goto freeit;
500 }
501 break;
502 default:
503 break;
504 }
505
506 /* Apply rate limit before checksum validation. */
507 if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) {
508 icmp6stat.icp6s_toofreq++;
509 goto freeit;
510 }
511
512 /*
513 * Check multicast group membership.
514 * Note: SSM filters are not applied for ICMPv6 traffic.
515 */
516 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
517 struct in6_multi *inm;
518
519 in6_multihead_lock_shared();
520 IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp, inm);
521 in6_multihead_lock_done();
522
523 if (inm == NULL) {
524 /*
525 * Don't discard if this is a Neighbor Solicitation
526 * that needs to be proxied (see check down below.)
527 */
528 if (!(m->m_pkthdr.pkt_flags & PKTF_PROXY_DST)) {
529 ip6stat.ip6s_notmember++;
530 in6_ifstat_inc(m->m_pkthdr.rcvif,
531 ifs6_in_discard);
532 goto freeit;
533 }
534 } else {
535 IN6M_REMREF(inm);
536 }
537 }
538
539 /*
540 * calculate the checksum
541 */
542 if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
543 nd6log((LOG_ERR,
544 "ICMP6 checksum error(%d|%x) %s\n",
545 icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
546 icmp6stat.icp6s_checksum++;
547 goto freeit;
548 }
549
550 if (m->m_pkthdr.pkt_flags & PKTF_PROXY_DST) {
551 /*
552 * This is the special case of proxying NS (dst is either
553 * solicited-node multicast or unicast); process it locally
554 * but don't deliver it to sockets. It practically lets us
555 * steer the packet to nd6_prproxy_ns_input, where more
556 * specific tests and actions will be taken.
557 */
558 switch (icmp6->icmp6_type) {
559 case ND_NEIGHBOR_SOLICIT:
560 proxy = 1;
561 break;
562 default:
563 goto freeit;
564 }
565 }
566
567 icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
568 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
569 if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
570 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
571
572 switch (icmp6->icmp6_type) {
573 case ICMP6_DST_UNREACH:
574 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
575 switch (code) {
576 case ICMP6_DST_UNREACH_NOROUTE:
577 code = PRC_UNREACH_NET;
578 break;
579 case ICMP6_DST_UNREACH_ADMIN:
580 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
581 code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
582 break;
583 case ICMP6_DST_UNREACH_ADDR:
584 code = PRC_HOSTDEAD;
585 break;
586 case ICMP6_DST_UNREACH_BEYONDSCOPE:
587 /* I mean "source address was incorrect." */
588 code = PRC_PARAMPROB;
589 break;
590 case ICMP6_DST_UNREACH_NOPORT:
591 code = PRC_UNREACH_PORT;
592 break;
593 default:
594 goto badcode;
595 }
596 goto deliver;
597
598 case ICMP6_PACKET_TOO_BIG:
599 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
600
601 code = PRC_MSGSIZE;
602
603 /*
604 * Updating the path MTU will be done after examining
605 * intermediate extension headers.
606 */
607 goto deliver;
608
609 case ICMP6_TIME_EXCEEDED:
610 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
611 switch (code) {
612 case ICMP6_TIME_EXCEED_TRANSIT:
613 code = PRC_TIMXCEED_INTRANS;
614 break;
615 case ICMP6_TIME_EXCEED_REASSEMBLY:
616 code = PRC_TIMXCEED_REASS;
617 break;
618 default:
619 goto badcode;
620 }
621 goto deliver;
622
623 case ICMP6_PARAM_PROB:
624 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
625 switch (code) {
626 case ICMP6_PARAMPROB_NEXTHEADER:
627 code = PRC_UNREACH_PROTOCOL;
628 break;
629 case ICMP6_PARAMPROB_HEADER:
630 case ICMP6_PARAMPROB_OPTION:
631 code = PRC_PARAMPROB;
632 break;
633 default:
634 goto badcode;
635 }
636 goto deliver;
637
638 case ICMP6_ECHO_REQUEST:
639 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
640 if (code != 0)
641 goto badcode;
642
643 if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
644 /* Give up remote */
645 goto rate_limit_checked;
646 }
647 if ((n->m_flags & M_EXT) != 0
648 || n->m_len < off + sizeof(struct icmp6_hdr)) {
649 struct mbuf *n0 = n;
650 const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
651
652 /*
653 * Prepare an internal mbuf. m_pullup() doesn't
654 * always copy the length we specified.
655 */
656 if (maxlen >= MCLBYTES) {
657 /* Give up remote */
658 m_freem(n0);
659 goto rate_limit_checked;
660 }
661 MGETHDR(n, M_DONTWAIT, n0->m_type); /* MAC-OK */
662 if (n && maxlen >= MHLEN) {
663 MCLGET(n, M_DONTWAIT);
664 if ((n->m_flags & M_EXT) == 0) {
665 m_free(n);
666 n = NULL;
667 }
668 }
669 if (n == NULL) {
670 /* Give up remote */
671 m_freem(n0);
672 goto rate_limit_checked;
673 }
674 M_COPY_PKTHDR(n, n0);
675 /*
676 * Copy IPv6 and ICMPv6 only.
677 */
678 nip6 = mtod(n, struct ip6_hdr *);
679 bcopy(ip6, nip6, sizeof(struct ip6_hdr));
680 nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
681 bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
682 noff = sizeof(struct ip6_hdr);
683 n->m_pkthdr.len = n->m_len =
684 noff + sizeof(struct icmp6_hdr);
685 /*
686 * Adjust mbuf. ip6_plen will be adjusted in
687 * ip6_output().
688 */
689 m_adj(n0, off + sizeof(struct icmp6_hdr));
690 n->m_pkthdr.len += n0->m_pkthdr.len;
691 n->m_next = n0;
692 n0->m_flags &= ~M_PKTHDR;
693 } else {
694 nip6 = mtod(n, struct ip6_hdr *);
695 IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
696 sizeof(*nicmp6));
697 noff = off;
698 }
699 if(nicmp6 == NULL)
700 panic("nicmp6 is NULL in %s, which isn't good!\n", __FUNCTION__);
701 else {
702 nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
703 nicmp6->icmp6_code = 0;
704 }
705 if (n) {
706 icmp6stat.icp6s_reflect++;
707 icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
708 icmp6_reflect(n, noff);
709 }
710 goto rate_limit_checked;
711
712 case ICMP6_ECHO_REPLY:
713 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
714 if (code != 0)
715 goto badcode;
716 break;
717
718 case MLD_LISTENER_QUERY:
719 case MLD_LISTENER_REPORT:
720
721 if (icmp6len < sizeof(struct mld_hdr))
722 goto badlen;
723 if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
724 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
725 else
726 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
727
728 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
729 /* give up local */
730 if (mld_input(m, off, icmp6len) == IPPROTO_DONE)
731 m = NULL;
732 goto freeit;
733 }
734 if (mld_input(n, off, icmp6len) != IPPROTO_DONE)
735 m_freem(n);
736 /* m stays. */
737 goto rate_limit_checked;
738
739 case MLD_LISTENER_DONE:
740 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
741 if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
742 goto badlen;
743 break; /* nothing to be done in kernel */
744
745 case MLD_MTRACE_RESP:
746 case MLD_MTRACE:
747 /* XXX: these two are experimental. not officially defined. */
748 /* XXX: per-interface statistics? */
749 break; /* just pass it to applications */
750
751 case ICMP6_NI_QUERY:
752 if (!icmp6_nodeinfo)
753 break;
754 //### LD 10/20 Check fbsd differences here. Not sure we're more advanced or not.
755 /* By RFC 4620 refuse to answer queries from global scope addresses */
756 if ((icmp6_nodeinfo & 8) != 8 && in6_addrscope(&ip6->ip6_src) == IPV6_ADDR_SCOPE_GLOBAL)
757 break;
758
759 if (icmp6len < sizeof(struct icmp6_nodeinfo))
760 goto badlen;
761
762 #ifndef PULLDOWN_TEST
763 IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
764 return IPPROTO_DONE);
765 #endif
766
767 n = m_copy(m, 0, M_COPYALL);
768 if (n)
769 n = ni6_input(n, off);
770 if (n) {
771 noff = sizeof(struct ip6_hdr);
772 icmp6stat.icp6s_reflect++;
773 icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
774 icmp6_reflect(n, noff);
775 }
776 goto rate_limit_checked;
777
778 case ICMP6_WRUREPLY:
779 if (code != 0)
780 goto badcode;
781 break;
782
783 case ND_ROUTER_SOLICIT:
784 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
785 if (code != 0)
786 goto badcode;
787 if (icmp6len < sizeof(struct nd_router_solicit))
788 goto badlen;
789
790 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
791 /* give up local */
792 nd6_rs_input(m, off, icmp6len);
793 m = NULL;
794 goto freeit;
795 }
796 nd6_rs_input(n, off, icmp6len);
797 /* m stays. */
798 goto rate_limit_checked;
799
800 case ND_ROUTER_ADVERT:
801 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
802 if (code != 0)
803 goto badcode;
804 if (icmp6len < sizeof(struct nd_router_advert))
805 goto badlen;
806
807 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
808 /* give up local */
809 nd6_ra_input(m, off, icmp6len);
810 m = NULL;
811 goto freeit;
812 }
813 nd6_ra_input(n, off, icmp6len);
814 /* m stays. */
815 goto rate_limit_checked;
816
817 case ND_NEIGHBOR_SOLICIT:
818 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
819 if (code != 0)
820 goto badcode;
821 if (icmp6len < sizeof(struct nd_neighbor_solicit))
822 goto badlen;
823
824 if (proxy ||
825 ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL)) {
826 /* give up local */
827 nd6_ns_input(m, off, icmp6len);
828 m = NULL;
829 goto freeit;
830 }
831 nd6_ns_input(n, off, icmp6len);
832 /* m stays. */
833 goto rate_limit_checked;
834
835 case ND_NEIGHBOR_ADVERT:
836 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
837 if (code != 0)
838 goto badcode;
839 if (icmp6len < sizeof(struct nd_neighbor_advert))
840 goto badlen;
841
842 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
843 /* give up local */
844 nd6_na_input(m, off, icmp6len);
845 m = NULL;
846 goto freeit;
847 }
848 nd6_na_input(n, off, icmp6len);
849 /* m stays. */
850 goto rate_limit_checked;
851
852 case ND_REDIRECT:
853 icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
854 if (code != 0)
855 goto badcode;
856 if (icmp6len < sizeof(struct nd_redirect))
857 goto badlen;
858
859 if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
860 /* give up local */
861 icmp6_redirect_input(m, off);
862 m = NULL;
863 goto freeit;
864 }
865 icmp6_redirect_input(n, off);
866 /* m stays. */
867 goto rate_limit_checked;
868
869 case ICMP6_ROUTER_RENUMBERING:
870 if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
871 code != ICMP6_ROUTER_RENUMBERING_RESULT)
872 goto badcode;
873 if (icmp6len < sizeof(struct icmp6_router_renum))
874 goto badlen;
875 break;
876
877 default:
878 nd6log((LOG_DEBUG,
879 "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
880 icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
881 ip6_sprintf(&ip6->ip6_dst),
882 m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
883 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
884 /* ICMPv6 error: MUST deliver it by spec... */
885 code = PRC_NCMDS;
886 /* deliver */
887 } else {
888 /* ICMPv6 informational: MUST not deliver */
889 goto rate_limit_checked;
890 }
891 deliver:
892 if (icmp6_notify_error(m, off, icmp6len, code)) {
893 /* In this case, m should've been freed. */
894 return(IPPROTO_DONE);
895 }
896 break;
897
898 badcode:
899 icmp6stat.icp6s_badcode++;
900 break;
901
902 badlen:
903 icmp6stat.icp6s_badlen++;
904 break;
905 }
906
907 rate_limit_checked:
908 icmp6_rip6_input(&m, *offp);
909 return IPPROTO_DONE;
910
911 freeit:
912 m_freem(m);
913 return IPPROTO_DONE;
914 }
915
916 static int
917 icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
918 {
919 struct icmp6_hdr *icmp6;
920 struct ip6_hdr *eip6;
921 u_int32_t notifymtu;
922 struct sockaddr_in6 icmp6src, icmp6dst;
923
924 if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
925 icmp6stat.icp6s_tooshort++;
926 goto freeit;
927 }
928 #ifndef PULLDOWN_TEST
929 IP6_EXTHDR_CHECK(m, off,
930 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
931 return -1);
932 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
933 #else
934 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
935 sizeof(*icmp6) + sizeof(struct ip6_hdr));
936 if (icmp6 == NULL) {
937 icmp6stat.icp6s_tooshort++;
938 return(-1);
939 }
940 #endif
941 eip6 = (struct ip6_hdr *)(icmp6 + 1);
942
943 /* Detect the upper level protocol */
944 {
945 void (*ctlfunc)(int, struct sockaddr *, void *);
946 u_int8_t nxt = eip6->ip6_nxt;
947 int eoff = off + sizeof(struct icmp6_hdr) +
948 sizeof(struct ip6_hdr);
949 struct ip6ctlparam ip6cp;
950 struct in6_addr *finaldst = NULL;
951 int icmp6type = icmp6->icmp6_type;
952 struct ip6_frag *fh;
953 struct ip6_rthdr *rth;
954 struct ip6_rthdr0 *rth0;
955 int rthlen;
956
957 while (1) { /* XXX: should avoid infinite loop explicitly? */
958 struct ip6_ext *eh;
959
960 switch (nxt) {
961 case IPPROTO_HOPOPTS:
962 case IPPROTO_DSTOPTS:
963 case IPPROTO_AH:
964 #ifndef PULLDOWN_TEST
965 IP6_EXTHDR_CHECK(m, 0, eoff +
966 sizeof(struct ip6_ext),
967 return -1);
968 eh = (struct ip6_ext *)(mtod(m, caddr_t)
969 + eoff);
970 #else
971 IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
972 eoff, sizeof(*eh));
973 if (eh == NULL) {
974 icmp6stat.icp6s_tooshort++;
975 return(-1);
976 }
977 #endif
978
979 if (nxt == IPPROTO_AH)
980 eoff += (eh->ip6e_len + 2) << 2;
981 else
982 eoff += (eh->ip6e_len + 1) << 3;
983 nxt = eh->ip6e_nxt;
984 break;
985 case IPPROTO_ROUTING:
986 /*
987 * When the erroneous packet contains a
988 * routing header, we should examine the
989 * header to determine the final destination.
990 * Otherwise, we can't properly update
991 * information that depends on the final
992 * destination (e.g. path MTU).
993 */
994 #ifndef PULLDOWN_TEST
995 IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
996 return -1);
997 rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
998 + eoff);
999 #else
1000 IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
1001 eoff, sizeof(*rth));
1002 if (rth == NULL) {
1003 icmp6stat.icp6s_tooshort++;
1004 return(-1);
1005 }
1006 #endif
1007 rthlen = (rth->ip6r_len + 1) << 3;
1008 /*
1009 * XXX: currently there is no
1010 * officially defined type other
1011 * than type-0.
1012 * Note that if the segment left field
1013 * is 0, all intermediate hops must
1014 * have been passed.
1015 */
1016 if (rth->ip6r_segleft &&
1017 rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
1018 int hops;
1019
1020 #ifndef PULLDOWN_TEST
1021 IP6_EXTHDR_CHECK(m, 0, eoff + rthlen,
1022 return -1);
1023 rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff);
1024 #else
1025 IP6_EXTHDR_GET(rth0,
1026 struct ip6_rthdr0 *, m,
1027 eoff, rthlen);
1028 if (rth0 == NULL) {
1029 icmp6stat.icp6s_tooshort++;
1030 return(-1);
1031 }
1032 #endif
1033 /* just ignore a bogus header */
1034 if ((rth0->ip6r0_len % 2) == 0 &&
1035 (hops = rth0->ip6r0_len/2))
1036 finaldst = (struct in6_addr *)(void *)(rth0 + 1) + (hops - 1);
1037 }
1038 eoff += rthlen;
1039 nxt = rth->ip6r_nxt;
1040 break;
1041 case IPPROTO_FRAGMENT:
1042 #ifndef PULLDOWN_TEST
1043 IP6_EXTHDR_CHECK(m, 0, eoff +
1044 sizeof(struct ip6_frag),
1045 return -1);
1046 fh = (struct ip6_frag *)(mtod(m, caddr_t)
1047 + eoff);
1048 #else
1049 IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1050 eoff, sizeof(*fh));
1051 if (fh == NULL) {
1052 icmp6stat.icp6s_tooshort++;
1053 return (-1);
1054 }
1055 #endif
1056 /*
1057 * Data after a fragment header is meaningless
1058 * unless it is the first fragment, but
1059 * we'll go to the notify label for path MTU
1060 * discovery.
1061 */
1062 if (fh->ip6f_offlg & IP6F_OFF_MASK)
1063 goto notify;
1064
1065 eoff += sizeof(struct ip6_frag);
1066 nxt = fh->ip6f_nxt;
1067 break;
1068 default:
1069 /*
1070 * This case includes ESP and the No Next
1071 * Header. In such cases going to the notify
1072 * label does not have any meaning
1073 * (i.e. ctlfunc will be NULL), but we go
1074 * anyway since we might have to update
1075 * path MTU information.
1076 */
1077 goto notify;
1078 }
1079 }
1080 notify:
1081 #ifndef PULLDOWN_TEST
1082 icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1083 #else
1084 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1085 sizeof(*icmp6) + sizeof(struct ip6_hdr));
1086 if (icmp6 == NULL) {
1087 icmp6stat.icp6s_tooshort++;
1088 return (-1);
1089 }
1090 #endif
1091
1092 /*
1093 * retrieve parameters from the inner IPv6 header, and convert
1094 * them into sockaddr structures.
1095 * XXX: there is no guarantee that the source or destination
1096 * addresses of the inner packet are in the same scope as
1097 * the addresses of the icmp packet. But there is no other
1098 * way to determine the zone.
1099 */
1100 eip6 = (struct ip6_hdr *)(icmp6 + 1);
1101
1102 bzero(&icmp6dst, sizeof(icmp6dst));
1103 icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1104 icmp6dst.sin6_family = AF_INET6;
1105 if (finaldst == NULL)
1106 icmp6dst.sin6_addr = eip6->ip6_dst;
1107 else
1108 icmp6dst.sin6_addr = *finaldst;
1109 if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1110 goto freeit;
1111 bzero(&icmp6src, sizeof(icmp6src));
1112 icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1113 icmp6src.sin6_family = AF_INET6;
1114 icmp6src.sin6_addr = eip6->ip6_src;
1115 if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1116 goto freeit;
1117 icmp6src.sin6_flowinfo =
1118 (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1119
1120 if (finaldst == NULL)
1121 finaldst = &eip6->ip6_dst;
1122 ip6cp.ip6c_m = m;
1123 ip6cp.ip6c_icmp6 = icmp6;
1124 ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1125 ip6cp.ip6c_off = eoff;
1126 ip6cp.ip6c_finaldst = finaldst;
1127 ip6cp.ip6c_src = &icmp6src;
1128 ip6cp.ip6c_nxt = nxt;
1129
1130 if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1131 notifymtu = ntohl(icmp6->icmp6_mtu);
1132 ip6cp.ip6c_cmdarg = (void *)&notifymtu;
1133 icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/
1134 }
1135
1136 ctlfunc = (void (*)(int, struct sockaddr *, void *))
1137 (ip6_protox[nxt]->pr_ctlinput);
1138 if (ctlfunc) {
1139 (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1140 &ip6cp);
1141 }
1142 }
1143 return(0);
1144
1145 freeit:
1146 m_freem(m);
1147 return(-1);
1148 }
1149
1150 void
1151 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1152 {
1153 struct in6_addr *dst = ip6cp->ip6c_finaldst;
1154 struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1155 struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
1156 u_int mtu = ntohl(icmp6->icmp6_mtu);
1157 struct rtentry *rt = NULL;
1158 struct sockaddr_in6 sin6;
1159 /*
1160 * we reject ICMPv6 too big with abnormally small value.
1161 * XXX what is the good definition of "abnormally small"?
1162 */
1163 if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1164 return;
1165
1166 if (!validated)
1167 return;
1168
1169 /*
1170 * In case the suggested mtu is less than IPV6_MMTU, we
1171 * only need to remember that it was for above mentioned
1172 * "alwaysfrag" case.
1173 * Try to be as close to the spec as possible.
1174 */
1175 if (mtu < IPV6_MMTU)
1176 mtu = IPV6_MMTU - 8;
1177
1178 bzero(&sin6, sizeof(sin6));
1179 sin6.sin6_family = PF_INET6;
1180 sin6.sin6_len = sizeof(struct sockaddr_in6);
1181 sin6.sin6_addr = *dst;
1182 /* XXX normally, this won't happen */
1183 if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1184 sin6.sin6_addr.s6_addr16[1] =
1185 htons(m->m_pkthdr.rcvif->if_index);
1186 }
1187 /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1188 /*
1189 * XXX On a side note, for asymmetric data-path
1190 * the lookup on receive interace is probably not
1191 * what we want to do.
1192 * That requires looking at the cached route for the
1193 * protocol control block.
1194 */
1195 rt = rtalloc1_scoped((struct sockaddr *)&sin6, 0,
1196 RTF_CLONING | RTF_PRCLONING, m->m_pkthdr.rcvif->if_index);
1197 if (rt != NULL) {
1198 RT_LOCK(rt);
1199 if ((rt->rt_flags & RTF_HOST) &&
1200 !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
1201 mtu < IN6_LINKMTU(rt->rt_ifp) &&
1202 rt->rt_rmx.rmx_mtu > mtu) {
1203 icmp6stat.icp6s_pmtuchg++;
1204 rt->rt_rmx.rmx_mtu = mtu;
1205 }
1206 RT_UNLOCK(rt);
1207 rtfree(rt);
1208 }
1209 }
1210
1211 /*
1212 * Process a Node Information Query packet, based on
1213 * draft-ietf-ipngwg-icmp-name-lookups-07.
1214 *
1215 * Spec incompatibilities:
1216 * - IPv6 Subject address handling
1217 * - IPv4 Subject address handling support missing
1218 * - Proxy reply (answer even if it's not for me)
1219 * - joins NI group address at in6_ifattach() time only, does not cope
1220 * with hostname changes by sethostname(3)
1221 */
1222 #define hostnamelen strlen(hostname)
1223 static struct mbuf *
1224 ni6_input(struct mbuf *m, 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(
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(const char *a, int alen, const char *b, int blen)
1664 {
1665 const char *a0, *b0;
1666 int l;
1667
1668 /* simplest case - need validation? */
1669 if (alen == blen && bcmp(a, b, alen) == 0)
1670 return 1;
1671
1672 a0 = a;
1673 b0 = b;
1674
1675 /* termination is mandatory */
1676 if (alen < 2 || blen < 2)
1677 return 0;
1678 if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1679 return 0;
1680 alen--;
1681 blen--;
1682
1683 while (a - a0 < alen && b - b0 < blen) {
1684 if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1685 return 0;
1686
1687 if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1688 return 0;
1689 /* we don't support compression yet */
1690 if (a[0] >= 64 || b[0] >= 64)
1691 return 0;
1692
1693 /* truncated case */
1694 if (a[0] == 0 && a - a0 == alen - 1)
1695 return 1;
1696 if (b[0] == 0 && b - b0 == blen - 1)
1697 return 1;
1698 if (a[0] == 0 || b[0] == 0)
1699 return 0;
1700
1701 if (a[0] != b[0])
1702 return 0;
1703 l = a[0];
1704 if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1705 return 0;
1706 if (bcmp(a + 1, b + 1, l) != 0)
1707 return 0;
1708
1709 a += 1 + l;
1710 b += 1 + l;
1711 }
1712
1713 if (a - a0 == alen && b - b0 == blen)
1714 return 1;
1715 else
1716 return 0;
1717 }
1718
1719 /*
1720 * calculate the number of addresses to be returned in the node info reply.
1721 */
1722 static int
1723 ni6_addrs(struct icmp6_nodeinfo *ni6, struct ifnet **ifpp, char *subj)
1724 {
1725 struct ifnet *ifp;
1726 struct in6_ifaddr *ifa6;
1727 struct ifaddr *ifa;
1728 struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1729 int addrs = 0, addrsofif, iffound = 0;
1730 int niflags = ni6->ni_flags;
1731
1732 if (ifpp != NULL)
1733 *ifpp = NULL;
1734
1735 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1736 switch (ni6->ni_code) {
1737 case ICMP6_NI_SUBJ_IPV6:
1738 if (subj == NULL) /* must be impossible... */
1739 return(0);
1740 subj_ip6 = (struct sockaddr_in6 *)(void *)subj;
1741 break;
1742 default:
1743 /*
1744 * XXX: we only support IPv6 subject address for
1745 * this Qtype.
1746 */
1747 return (0);
1748 }
1749 }
1750
1751 ifnet_head_lock_shared();
1752 TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
1753 addrsofif = 0;
1754 ifnet_lock_shared(ifp);
1755 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1756 {
1757 IFA_LOCK(ifa);
1758 if (ifa->ifa_addr->sa_family != AF_INET6) {
1759 IFA_UNLOCK(ifa);
1760 continue;
1761 }
1762 ifa6 = (struct in6_ifaddr *)ifa;
1763
1764 if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1765 IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1766 &ifa6->ia_addr.sin6_addr))
1767 iffound = 1;
1768
1769 /*
1770 * IPv4-mapped addresses can only be returned by a
1771 * Node Information proxy, since they represent
1772 * addresses of IPv4-only nodes, which perforce do
1773 * not implement this protocol.
1774 * [icmp-name-lookups-07, Section 5.4]
1775 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1776 * this function at this moment.
1777 */
1778
1779 /* What do we have to do about ::1? */
1780 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1781 case IPV6_ADDR_SCOPE_LINKLOCAL:
1782 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) {
1783 IFA_UNLOCK(ifa);
1784 continue;
1785 }
1786 break;
1787 case IPV6_ADDR_SCOPE_SITELOCAL:
1788 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) {
1789 IFA_UNLOCK(ifa);
1790 continue;
1791 }
1792 break;
1793 case IPV6_ADDR_SCOPE_GLOBAL:
1794 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) {
1795 IFA_UNLOCK(ifa);
1796 continue;
1797 }
1798 break;
1799 default:
1800 IFA_UNLOCK(ifa);
1801 continue;
1802 }
1803
1804 /*
1805 * check if anycast is okay.
1806 * XXX: just experimental. not in the spec.
1807 */
1808 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1809 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) {
1810 IFA_UNLOCK(ifa);
1811 continue; /* we need only unicast addresses */
1812 }
1813 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1814 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1815 IFA_UNLOCK(ifa);
1816 continue;
1817 }
1818 addrsofif++; /* count the address */
1819 IFA_UNLOCK(ifa);
1820 }
1821 ifnet_lock_done(ifp);
1822 if (iffound) {
1823 if (ifpp != NULL) {
1824 *ifpp = ifp;
1825 ifnet_reference(ifp);
1826 }
1827 ifnet_head_done();
1828 return(addrsofif);
1829 }
1830
1831 addrs += addrsofif;
1832 }
1833 ifnet_head_done();
1834
1835 return (addrs);
1836 }
1837
1838 static int
1839 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1840 struct ifnet *ifp0, int resid)
1841 {
1842 struct ifnet *ifp = ifp0;
1843 struct in6_ifaddr *ifa6;
1844 struct ifaddr *ifa;
1845 struct ifnet *ifp_dep = NULL;
1846 int copied = 0, allow_deprecated = 0;
1847 u_char *cp = (u_char *)(nni6 + 1);
1848 int niflags = ni6->ni_flags;
1849 u_int32_t ltime;
1850 uint64_t now = net_uptime();
1851
1852 if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1853 return (0); /* needless to copy */
1854
1855 again:
1856
1857 ifnet_head_lock_shared();
1858 if (ifp == NULL)
1859 ifp = TAILQ_FIRST(&ifnet_head);
1860
1861 for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1862 ifnet_lock_shared(ifp);
1863 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1864 ifa = ifa->ifa_list.tqe_next) {
1865 struct in6_addrlifetime_i *lt;
1866
1867 IFA_LOCK(ifa);
1868 if (ifa->ifa_addr->sa_family != AF_INET6) {
1869 IFA_UNLOCK(ifa);
1870 continue;
1871 }
1872 ifa6 = (struct in6_ifaddr *)ifa;
1873
1874 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1875 allow_deprecated == 0) {
1876 /*
1877 * prefererred address should be put before
1878 * deprecated addresses.
1879 */
1880
1881 /* record the interface for later search */
1882 if (ifp_dep == NULL)
1883 ifp_dep = ifp;
1884
1885 IFA_UNLOCK(ifa);
1886 continue;
1887 } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1888 allow_deprecated != 0) {
1889 IFA_UNLOCK(ifa);
1890 continue; /* we now collect deprecated addrs */
1891 }
1892 /* What do we have to do about ::1? */
1893 switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1894 case IPV6_ADDR_SCOPE_LINKLOCAL:
1895 if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) {
1896 IFA_UNLOCK(ifa);
1897 continue;
1898 }
1899 break;
1900 case IPV6_ADDR_SCOPE_SITELOCAL:
1901 if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) {
1902 IFA_UNLOCK(ifa);
1903 continue;
1904 }
1905 break;
1906 case IPV6_ADDR_SCOPE_GLOBAL:
1907 if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) {
1908 IFA_UNLOCK(ifa);
1909 continue;
1910 }
1911 break;
1912 default:
1913 IFA_UNLOCK(ifa);
1914 continue;
1915 }
1916
1917 /*
1918 * check if anycast is okay.
1919 * XXX: just experimental. not in the spec.
1920 */
1921 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1922 (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) {
1923 IFA_UNLOCK(ifa);
1924 continue;
1925 }
1926 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1927 (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1928 IFA_UNLOCK(ifa);
1929 continue;
1930 }
1931
1932 /* now we can copy the address */
1933 if (resid < sizeof(struct in6_addr) +
1934 sizeof(u_int32_t)) {
1935 IFA_UNLOCK(ifa);
1936 /*
1937 * We give up much more copy.
1938 * Set the truncate flag and return.
1939 */
1940 nni6->ni_flags |=
1941 NI_NODEADDR_FLAG_TRUNCATE;
1942 ifnet_lock_done(ifp);
1943 ifnet_head_done();
1944 return(copied);
1945 }
1946
1947 /*
1948 * Set the TTL of the address.
1949 * The TTL value should be one of the following
1950 * according to the specification:
1951 *
1952 * 1. The remaining lifetime of a DHCP lease on the
1953 * address, or
1954 * 2. The remaining Valid Lifetime of a prefix from
1955 * which the address was derived through Stateless
1956 * Autoconfiguration.
1957 *
1958 * Note that we currently do not support stateful
1959 * address configuration by DHCPv6, so the former
1960 * case can't happen.
1961 */
1962 lt = &ifa6->ia6_lifetime;
1963 if (lt->ia6ti_expire == 0) {
1964 ltime = ND6_INFINITE_LIFETIME;
1965 } else {
1966 if (lt->ia6ti_expire > now)
1967 ltime = htonl(lt->ia6ti_expire - now);
1968 else
1969 ltime = 0;
1970 }
1971
1972 bcopy(&ltime, cp, sizeof(u_int32_t));
1973 cp += sizeof(u_int32_t);
1974
1975 /* copy the address itself */
1976 bcopy(&ifa6->ia_addr.sin6_addr, cp,
1977 sizeof(struct in6_addr));
1978 /* XXX: KAME link-local hack; remove ifindex */
1979 if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1980 ((struct in6_addr *)(void *)cp)->s6_addr16[1] = 0;
1981 cp += sizeof(struct in6_addr);
1982
1983 resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1984 copied += (sizeof(struct in6_addr) +
1985 sizeof(u_int32_t));
1986 IFA_UNLOCK(ifa);
1987 }
1988 ifnet_lock_done(ifp);
1989 if (ifp0) /* we need search only on the specified IF */
1990 break;
1991 }
1992 ifnet_head_done();
1993
1994 if (allow_deprecated == 0 && ifp_dep != NULL) {
1995 ifp = ifp_dep;
1996 allow_deprecated = 1;
1997
1998 goto again;
1999 }
2000
2001 return(copied);
2002 }
2003
2004 /*
2005 * XXX almost dup'ed code with rip6_input.
2006 */
2007 static int
2008 icmp6_rip6_input(struct mbuf **mp, int off)
2009 {
2010 struct mbuf *m = *mp;
2011 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2012 struct in6pcb *in6p;
2013 struct in6pcb *last = NULL;
2014 struct sockaddr_in6 rip6src;
2015 struct icmp6_hdr *icmp6;
2016 struct mbuf *opts = NULL;
2017 int ret = 0;
2018 struct ifnet *ifp = m->m_pkthdr.rcvif;
2019
2020 #ifndef PULLDOWN_TEST
2021 /* this is assumed to be safe. */
2022 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
2023 #else
2024 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
2025 if (icmp6 == NULL) {
2026 /* m is already reclaimed */
2027 return IPPROTO_DONE;
2028 }
2029 #endif
2030
2031 /*
2032 * XXX: the address may have embedded scope zone ID, which should be
2033 * hidden from applications.
2034 */
2035 bzero(&rip6src, sizeof(rip6src));
2036 rip6src.sin6_family = AF_INET6;
2037 rip6src.sin6_len = sizeof(struct sockaddr_in6);
2038 rip6src.sin6_addr = ip6->ip6_src;
2039 if (sa6_recoverscope(&rip6src, TRUE))
2040 return (IPPROTO_DONE);
2041
2042 lck_rw_lock_shared(ripcbinfo.ipi_lock);
2043 LIST_FOREACH(in6p, &ripcb, inp_list)
2044 {
2045 if ((in6p->inp_vflag & INP_IPV6) == 0)
2046 continue;
2047 if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
2048 continue;
2049 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
2050 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
2051 continue;
2052 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
2053 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
2054 continue;
2055 if (in6p->in6p_icmp6filt
2056 && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
2057 in6p->in6p_icmp6filt))
2058 continue;
2059
2060 if (inp_restricted_recv(in6p, ifp))
2061 continue;
2062
2063 if (last) {
2064 struct mbuf *n;
2065 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
2066 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
2067 (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
2068 (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
2069 ret = ip6_savecontrol(last, n, &opts);
2070 if (ret != 0) {
2071 m_freem(n);
2072 m_freem(opts);
2073 last = in6p;
2074 continue;
2075 }
2076 }
2077 /* strip intermediate headers */
2078 m_adj(n, off);
2079 so_recv_data_stat(last->in6p_socket, m, 0);
2080 if (sbappendaddr(&last->in6p_socket->so_rcv,
2081 (struct sockaddr *)&rip6src,
2082 n, opts, NULL) != 0) {
2083 sorwakeup(last->in6p_socket);
2084 }
2085 opts = NULL;
2086 }
2087 }
2088 last = in6p;
2089 }
2090 if (last) {
2091 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
2092 (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
2093 (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
2094 ret = ip6_savecontrol(last, m, &opts);
2095 if (ret != 0) {
2096 goto error;
2097 }
2098 }
2099 /* strip intermediate headers */
2100 m_adj(m, off);
2101 so_recv_data_stat(last->in6p_socket, m, 0);
2102 if (sbappendaddr(&last->in6p_socket->so_rcv,
2103 (struct sockaddr *)&rip6src, m, opts, NULL) != 0) {
2104 sorwakeup(last->in6p_socket);
2105 }
2106 } else {
2107 goto error;
2108 }
2109 lck_rw_done(ripcbinfo.ipi_lock);
2110 return IPPROTO_DONE;
2111
2112 error:
2113 lck_rw_done(ripcbinfo.ipi_lock);
2114 m_freem(m);
2115 m_freem(opts);
2116 ip6stat.ip6s_delivered--;
2117 return IPPROTO_DONE;
2118 }
2119
2120 /*
2121 * Reflect the ip6 packet back to the source.
2122 * OFF points to the icmp6 header, counted from the top of the mbuf.
2123 */
2124 void
2125 icmp6_reflect(struct mbuf *m, size_t off)
2126 {
2127 struct mbuf *m_ip6hdr = m;
2128 struct ip6_hdr *ip6;
2129 struct icmp6_hdr *icmp6;
2130 struct in6_ifaddr *ia;
2131 struct in6_addr t, src_storage, *src = 0;
2132 int plen;
2133 int type, code;
2134 struct ifnet *outif = NULL;
2135 struct sockaddr_in6 sa6_src, sa6_dst;
2136 struct nd_ifinfo *ndi = NULL;
2137 u_int32_t oflow;
2138 struct ip6_out_args ip6oa = { IFSCOPE_NONE, { 0 },
2139 IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR |
2140 IP6OAF_INTCOPROC_ALLOWED | IP6OAF_AWDL_UNRESTRICTED, 0,
2141 SO_TC_UNSPEC, _NET_SERVICE_TYPE_UNSPEC };
2142
2143 if (!(m->m_pkthdr.pkt_flags & PKTF_LOOP) && m->m_pkthdr.rcvif != NULL) {
2144 ip6oa.ip6oa_boundif = m->m_pkthdr.rcvif->if_index;
2145 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
2146 }
2147
2148 /* too short to reflect */
2149 if (off < sizeof(struct ip6_hdr)) {
2150 nd6log((LOG_DEBUG,
2151 "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2152 (u_int32_t)off, (u_int32_t)sizeof(struct ip6_hdr),
2153 __FILE__, __LINE__));
2154 goto bad;
2155 }
2156
2157 /*
2158 * If there are extra headers between IPv6 and ICMPv6, strip
2159 * off that header first.
2160 */
2161 if (off > sizeof(struct ip6_hdr)) {
2162 size_t l;
2163 struct ip6_hdr nip6;
2164
2165 l = off - sizeof(struct ip6_hdr);
2166 m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2167 m_adj(m, l);
2168 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2169 if (m->m_len < l) {
2170 if ((m_ip6hdr = m_pulldown(m, 0, l, NULL)) == NULL)
2171 return;
2172 }
2173 bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2174 } else /* off == sizeof(struct ip6_hdr) */ {
2175 size_t l;
2176 l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2177 if (m->m_len < l) {
2178 if ((m_ip6hdr = m_pulldown(m, 0, l, NULL)) == NULL)
2179 return;
2180 }
2181 }
2182 plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2183 ip6 = mtod(m_ip6hdr, struct ip6_hdr *);
2184 ip6->ip6_nxt = IPPROTO_ICMPV6;
2185 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2186 type = icmp6->icmp6_type; /* keep type for statistics */
2187 code = icmp6->icmp6_code; /* ditto. */
2188
2189 t = ip6->ip6_dst;
2190 /*
2191 * ip6_input() drops a packet if its src is multicast.
2192 * So, the src is never multicast.
2193 */
2194 ip6->ip6_dst = ip6->ip6_src;
2195
2196 /*
2197 * XXX: make sure to embed scope zone information, using
2198 * already embedded IDs or the received interface (if any).
2199 * Note that rcvif may be NULL.
2200 */
2201 bzero(&sa6_src, sizeof(sa6_src));
2202 sa6_src.sin6_family = AF_INET6;
2203 sa6_src.sin6_len = sizeof(sa6_src);
2204 sa6_src.sin6_addr = ip6->ip6_dst;
2205 in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2206 in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL, NULL);
2207 bzero(&sa6_dst, sizeof(sa6_dst));
2208 sa6_dst.sin6_family = AF_INET6;
2209 sa6_dst.sin6_len = sizeof(sa6_dst);
2210 sa6_dst.sin6_addr = t;
2211 in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2212 in6_embedscope(&t, &sa6_dst, NULL, NULL, NULL);
2213
2214 /*
2215 * If the incoming packet was addressed directly to us(i.e. unicast),
2216 * use dst as the src for the reply.
2217 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2218 * (for example) when we encounter an error while forwarding procedure
2219 * destined to a duplicated address of ours.
2220 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2221 * procedure of an outgoing packet of our own, in which case we need
2222 * to search in the ifaddr list.
2223 */
2224 lck_rw_lock_shared(&in6_ifaddr_rwlock);
2225 for (ia = in6_ifaddrs; ia; ia = ia->ia_next) {
2226 IFA_LOCK(&ia->ia_ifa);
2227 if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2228 (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2229 IFA_UNLOCK(&ia->ia_ifa);
2230 src = &t;
2231 break;
2232 }
2233 IFA_UNLOCK(&ia->ia_ifa);
2234 }
2235 lck_rw_done(&in6_ifaddr_rwlock);
2236 if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) &&
2237 ((m->m_flags & M_LOOP) || (m->m_pkthdr.pkt_flags & PKTF_LOOP))) {
2238 /*
2239 * This is the case if the dst is our link-local address
2240 * and the sender is also ourselves. Here we test for both
2241 * M_LOOP and PKTF_LOOP, since the former may have been set
2242 * in ip6_output() and that we get here as part of callling
2243 * ip6_process_hopopts(). See comments in <sys/mbuf.h>
2244 */
2245 src = &t;
2246 }
2247
2248 if (src == NULL) {
2249 int e;
2250 struct sockaddr_in6 sin6;
2251 struct route_in6 ro;
2252
2253 /*
2254 * This case matches to multicasts, our anycast, or unicasts
2255 * that we do not own. Select a source address based on the
2256 * source address of the erroneous packet.
2257 */
2258 bzero(&sin6, sizeof(sin6));
2259 sin6.sin6_family = AF_INET6;
2260 sin6.sin6_len = sizeof(sin6);
2261 sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2262
2263 bzero(&ro, sizeof(ro));
2264 /*
2265 * in6_selectsrc() might return outif with its reference held
2266 * even in the error case, so we always need to release it
2267 * if non-NULL.
2268 */
2269 src = in6_selectsrc(&sin6, NULL, NULL, &ro, &outif,
2270 &src_storage, ip6oa.ip6oa_boundif, &e);
2271 ROUTE_RELEASE(&ro);
2272 if (src == NULL) {
2273 nd6log((LOG_DEBUG,
2274 "icmp6_reflect: source can't be determined: "
2275 "dst=%s, error=%d\n",
2276 ip6_sprintf(&sa6_src.sin6_addr), e));
2277 goto bad;
2278 }
2279 }
2280 oflow = ip6->ip6_flow; /* Save for later */
2281 ip6->ip6_src = *src;
2282 ip6->ip6_flow = 0;
2283 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2284 ip6->ip6_vfc |= IPV6_VERSION;
2285 if (icmp6->icmp6_type == ICMP6_ECHO_REPLY && icmp6->icmp6_code == 0) {
2286 ip6->ip6_flow |= (oflow & htonl(0x0ff00000));
2287 }
2288 ip6->ip6_nxt = IPPROTO_ICMPV6;
2289 if (outif != NULL && (ndi = ND_IFINFO(outif)) != NULL &&
2290 ndi->initialized) {
2291 lck_mtx_lock(&ndi->lock);
2292 ip6->ip6_hlim = ndi->chlim;
2293 lck_mtx_unlock(&ndi->lock);
2294 }
2295 if (m->m_pkthdr.rcvif != NULL &&
2296 (ndi = ND_IFINFO(m->m_pkthdr.rcvif)) != NULL &&
2297 ndi->initialized) {
2298 /* XXX: This may not be the outgoing interface */
2299 lck_mtx_lock(&ndi->lock);
2300 ip6->ip6_hlim = ndi->chlim;
2301 lck_mtx_unlock(&ndi->lock);
2302 } else {
2303 ip6->ip6_hlim = ip6_defhlim;
2304 }
2305 /* Use the same traffic class as in the request to match IPv4 */
2306 icmp6->icmp6_cksum = 0;
2307 icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2308 sizeof(struct ip6_hdr), plen);
2309
2310 /*
2311 * XXX option handling
2312 */
2313 m->m_flags &= ~(M_BCAST|M_MCAST);
2314
2315 if (outif != NULL) {
2316 ifnet_release(outif);
2317 outif = NULL;
2318 }
2319
2320 m->m_pkthdr.csum_data = 0;
2321 m->m_pkthdr.csum_flags = 0;
2322 ip6_output(m, NULL, NULL, IPV6_OUTARGS, NULL, &outif, &ip6oa);
2323 if (outif != NULL) {
2324 icmp6_ifoutstat_inc(outif, type, code);
2325 ifnet_release(outif);
2326 }
2327 return;
2328
2329 bad:
2330 m_freem(m);
2331 if (outif != NULL)
2332 ifnet_release(outif);
2333 return;
2334 }
2335
2336 static const char *
2337 icmp6_redirect_diag(struct in6_addr *src6,
2338 struct in6_addr *dst6,
2339 struct in6_addr *tgt6)
2340 {
2341 static char buf[1024];
2342 snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2343 ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2344 return buf;
2345 }
2346
2347 void
2348 icmp6_redirect_input(struct mbuf *m, int off)
2349 {
2350 struct ifnet *ifp = m->m_pkthdr.rcvif;
2351 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2352 struct nd_redirect *nd_rd;
2353 int icmp6len = ntohs(ip6->ip6_plen);
2354 char *lladdr = NULL;
2355 int lladdrlen = 0;
2356 u_char *redirhdr = NULL;
2357 int redirhdrlen = 0;
2358 struct rtentry *rt = NULL;
2359 int is_router;
2360 int is_onlink;
2361 struct in6_addr src6 = ip6->ip6_src;
2362 struct in6_addr redtgt6;
2363 struct in6_addr reddst6;
2364 union nd_opts ndopts;
2365
2366 if (!m || !ifp)
2367 return;
2368
2369 /*
2370 * If we are an advertising router on this interface,
2371 * don't update route by icmp6 redirect.
2372 */
2373 if (ifp->if_eflags & IFEF_IPV6_ROUTER)
2374 goto freeit;
2375 if (!icmp6_rediraccept)
2376 goto freeit;
2377
2378 #ifndef PULLDOWN_TEST
2379 IP6_EXTHDR_CHECK(m, off, icmp6len, return);
2380 nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2381 #else
2382 IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2383 if (nd_rd == NULL) {
2384 icmp6stat.icp6s_tooshort++;
2385 return;
2386 }
2387 #endif
2388 redtgt6 = nd_rd->nd_rd_target;
2389 reddst6 = nd_rd->nd_rd_dst;
2390
2391 if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2392 in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2393 goto freeit;
2394 }
2395
2396 /* validation */
2397 if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2398 nd6log((LOG_ERR,
2399 "ICMP6 redirect sent from %s rejected; "
2400 "must be from linklocal\n", ip6_sprintf(&src6)));
2401 goto bad;
2402 }
2403 if (ip6->ip6_hlim != 255) {
2404 nd6log((LOG_ERR,
2405 "ICMP6 redirect sent from %s rejected; "
2406 "hlim=%d (must be 255)\n",
2407 ip6_sprintf(&src6), ip6->ip6_hlim));
2408 goto bad;
2409 }
2410 {
2411 /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2412 struct sockaddr_in6 sin6;
2413 struct in6_addr *gw6;
2414
2415 bzero(&sin6, sizeof(sin6));
2416 sin6.sin6_family = AF_INET6;
2417 sin6.sin6_len = sizeof(struct sockaddr_in6);
2418 bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2419 rt = rtalloc1_scoped((struct sockaddr *)&sin6, 0, 0, ifp->if_index);
2420 if (rt) {
2421 RT_LOCK(rt);
2422 if (rt->rt_gateway == NULL ||
2423 rt->rt_gateway->sa_family != AF_INET6) {
2424 nd6log((LOG_ERR,
2425 "ICMP6 redirect rejected; no route "
2426 "with inet6 gateway found for redirect dst: %s\n",
2427 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2428 RT_UNLOCK(rt);
2429 rtfree(rt);
2430 goto bad;
2431 }
2432
2433 gw6 = &(((struct sockaddr_in6 *)(void *)
2434 rt->rt_gateway)->sin6_addr);
2435 if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2436 nd6log((LOG_ERR,
2437 "ICMP6 redirect rejected; "
2438 "not equal to gw-for-src=%s (must be same): "
2439 "%s\n",
2440 ip6_sprintf(gw6),
2441 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2442 RT_UNLOCK(rt);
2443 rtfree(rt);
2444 goto bad;
2445 }
2446 } else {
2447 nd6log((LOG_ERR,
2448 "ICMP6 redirect rejected; "
2449 "no route found for redirect dst: %s\n",
2450 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2451 goto bad;
2452 }
2453 RT_UNLOCK(rt);
2454 rtfree(rt);
2455 rt = NULL;
2456 }
2457 if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2458 nd6log((LOG_ERR,
2459 "ICMP6 redirect rejected; "
2460 "redirect dst must be unicast: %s\n",
2461 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2462 goto bad;
2463 }
2464
2465 is_router = is_onlink = 0;
2466 if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2467 is_router = 1; /* router case */
2468 if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2469 is_onlink = 1; /* on-link destination case */
2470 if (!is_router && !is_onlink) {
2471 nd6log((LOG_ERR,
2472 "ICMP6 redirect rejected; "
2473 "neither router case nor onlink case: %s\n",
2474 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2475 goto bad;
2476 }
2477 /* validation passed */
2478
2479 icmp6len -= sizeof(*nd_rd);
2480 nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2481 if (nd6_options(&ndopts) < 0) {
2482 nd6log((LOG_INFO, "icmp6_redirect_input: "
2483 "invalid ND option, rejected: %s\n",
2484 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2485 /* nd6_options have incremented stats */
2486 goto freeit;
2487 }
2488
2489 if (ndopts.nd_opts_tgt_lladdr) {
2490 lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2491 lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2492 }
2493
2494 if (ndopts.nd_opts_rh) {
2495 redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2496 redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2497 }
2498
2499 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2500 nd6log((LOG_INFO,
2501 "icmp6_redirect_input: lladdrlen mismatch for %s "
2502 "(if %d, icmp6 packet %d): %s\n",
2503 ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2504 icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2505 goto bad;
2506 }
2507
2508 /* RFC 2461 8.3 */
2509 nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2510 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2511
2512 if (!is_onlink) { /* better router case. perform rtredirect. */
2513 /* perform rtredirect */
2514 struct sockaddr_in6 sdst;
2515 struct sockaddr_in6 sgw;
2516 struct sockaddr_in6 ssrc;
2517
2518 bzero(&sdst, sizeof(sdst));
2519 bzero(&sgw, sizeof(sgw));
2520 bzero(&ssrc, sizeof(ssrc));
2521 sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2522 sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2523 sizeof(struct sockaddr_in6);
2524 bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2525 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2526 bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2527 rtredirect(ifp, (struct sockaddr *)&sdst,
2528 (struct sockaddr *)&sgw, NULL, RTF_GATEWAY | RTF_HOST,
2529 (struct sockaddr *)&ssrc, NULL);
2530 }
2531 /* finally update cached route in each socket via pfctlinput */
2532 {
2533 struct sockaddr_in6 sdst;
2534
2535 bzero(&sdst, sizeof(sdst));
2536 sdst.sin6_family = AF_INET6;
2537 sdst.sin6_len = sizeof(struct sockaddr_in6);
2538 bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2539
2540 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2541 #if IPSEC
2542 key_sa_routechange((struct sockaddr *)&sdst);
2543 #endif
2544 }
2545
2546 freeit:
2547 m_freem(m);
2548 return;
2549
2550 bad:
2551 icmp6stat.icp6s_badredirect++;
2552 m_freem(m);
2553 }
2554
2555 void
2556 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2557 {
2558 struct ifnet *ifp; /* my outgoing interface */
2559 struct in6_addr ifp_ll6;
2560 struct in6_addr *router_ll6;
2561 struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
2562 struct mbuf *m = NULL; /* newly allocated one */
2563 struct ip6_hdr *ip6; /* m as struct ip6_hdr */
2564 struct nd_redirect *nd_rd;
2565 size_t maxlen;
2566 u_char *p;
2567 struct ifnet *outif = NULL;
2568 struct sockaddr_in6 src_sa;
2569 struct ip6_out_args ip6oa = { IFSCOPE_NONE, { 0 },
2570 IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR, 0,
2571 SO_TC_UNSPEC, _NET_SERVICE_TYPE_UNSPEC };
2572
2573 icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2574
2575 if (rt != NULL)
2576 RT_LOCK(rt);
2577
2578 /* sanity check */
2579 if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2580 goto fail;
2581
2582 /*
2583 * If we are not a router to begin with, or not an advertising
2584 * router on this interface, don't send icmp6 redirect.
2585 */
2586 if (!ip6_forwarding || !(ifp->if_eflags & IFEF_IPV6_ROUTER))
2587 goto fail;
2588
2589 /*
2590 * Address check:
2591 * the source address must identify a neighbor, and
2592 * the destination address must not be a multicast address
2593 * [RFC 2461, sec 8.2]
2594 */
2595 sip6 = mtod(m0, struct ip6_hdr *);
2596 bzero(&src_sa, sizeof(src_sa));
2597 src_sa.sin6_family = AF_INET6;
2598 src_sa.sin6_len = sizeof(src_sa);
2599 src_sa.sin6_addr = sip6->ip6_src;
2600 /* we don't currently use sin6_scope_id, but eventually use it */
2601 src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2602 RT_UNLOCK(rt);
2603 if (nd6_is_addr_neighbor(&src_sa, ifp, 0) == 0) {
2604 /* already unlocked */
2605 rt = NULL;
2606 goto fail;
2607 }
2608 RT_LOCK(rt);
2609 if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2610 goto fail; /* what should we do here? */
2611
2612 /* rate limit */
2613 if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2614 goto fail;
2615
2616 /*
2617 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2618 * we almost always ask for an mbuf cluster for simplicity.
2619 * (MHLEN < IPV6_MMTU is almost always true)
2620 */
2621 #if IPV6_MMTU >= MCLBYTES
2622 # error assumption failed about IPV6_MMTU and MCLBYTES
2623 #endif
2624 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
2625 if (m && IPV6_MMTU >= MHLEN)
2626 MCLGET(m, M_DONTWAIT);
2627 if (!m)
2628 goto fail;
2629 m->m_pkthdr.rcvif = NULL;
2630 m->m_len = 0;
2631 maxlen = M_TRAILINGSPACE(m);
2632 maxlen = min(IPV6_MMTU, maxlen);
2633 /* just for safety */
2634 if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2635 ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2636 goto fail;
2637 }
2638
2639 {
2640 /* get ip6 linklocal address for ifp(my outgoing interface). */
2641 struct in6_ifaddr *ia;
2642 if ((ia = in6ifa_ifpforlinklocal(ifp,
2643 IN6_IFF_NOTREADY|
2644 IN6_IFF_ANYCAST)) == NULL)
2645 goto fail;
2646 IFA_LOCK(&ia->ia_ifa);
2647 ifp_ll6 = ia->ia_addr.sin6_addr;
2648 IFA_UNLOCK(&ia->ia_ifa);
2649 IFA_REMREF(&ia->ia_ifa);
2650 }
2651
2652 /* get ip6 linklocal address for the router. */
2653 if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2654 struct sockaddr_in6 *sin6;
2655 sin6 = (struct sockaddr_in6 *)(void *)rt->rt_gateway;
2656 router_ll6 = &sin6->sin6_addr;
2657 if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2658 router_ll6 = (struct in6_addr *)NULL;
2659 } else
2660 router_ll6 = (struct in6_addr *)NULL;
2661
2662 /* ip6 */
2663 ip6 = mtod(m, struct ip6_hdr *);
2664 ip6->ip6_flow = 0;
2665 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2666 ip6->ip6_vfc |= IPV6_VERSION;
2667 /* ip6->ip6_plen will be set later */
2668 ip6->ip6_nxt = IPPROTO_ICMPV6;
2669 ip6->ip6_hlim = 255;
2670 /* ip6->ip6_src must be linklocal addr for my outgoing if. */
2671 bcopy(&ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2672 bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2673
2674 /* ND Redirect */
2675 nd_rd = (struct nd_redirect *)(ip6 + 1);
2676 nd_rd->nd_rd_type = ND_REDIRECT;
2677 nd_rd->nd_rd_code = 0;
2678 nd_rd->nd_rd_reserved = 0;
2679 if (rt->rt_flags & RTF_GATEWAY) {
2680 /*
2681 * nd_rd->nd_rd_target must be a link-local address in
2682 * better router cases.
2683 */
2684 if (!router_ll6)
2685 goto fail;
2686 bcopy(router_ll6, &nd_rd->nd_rd_target,
2687 sizeof(nd_rd->nd_rd_target));
2688 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2689 sizeof(nd_rd->nd_rd_dst));
2690 } else {
2691 /* make sure redtgt == reddst */
2692 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2693 sizeof(nd_rd->nd_rd_target));
2694 bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2695 sizeof(nd_rd->nd_rd_dst));
2696 }
2697 RT_UNLOCK(rt);
2698 rt = NULL;
2699
2700 p = (u_char *)(nd_rd + 1);
2701
2702 if (!router_ll6)
2703 goto nolladdropt;
2704
2705 {
2706 /* target lladdr option */
2707 struct rtentry *rt_router = NULL;
2708 int len;
2709 struct sockaddr_dl *sdl;
2710 struct nd_opt_hdr *nd_opt;
2711 char *lladdr;
2712
2713 /* Callee returns a locked route upon success */
2714 rt_router = nd6_lookup(router_ll6, 0, ifp, 0);
2715 if (!rt_router)
2716 goto nolladdropt;
2717 RT_LOCK_ASSERT_HELD(rt_router);
2718 len = sizeof(*nd_opt) + ifp->if_addrlen;
2719 len = (len + 7) & ~7; /* round by 8 */
2720 /* safety check */
2721 if (len + (p - (u_char *)ip6) > maxlen) {
2722 RT_REMREF_LOCKED(rt_router);
2723 RT_UNLOCK(rt_router);
2724 goto nolladdropt;
2725 }
2726
2727 if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2728 (rt_router->rt_flags & RTF_LLINFO) &&
2729 (rt_router->rt_gateway->sa_family == AF_LINK) &&
2730 (sdl = (struct sockaddr_dl *)(void *)
2731 rt_router->rt_gateway) && sdl->sdl_alen) {
2732 nd_opt = (struct nd_opt_hdr *)p;
2733 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2734 nd_opt->nd_opt_len = len >> 3;
2735 lladdr = (char *)(nd_opt + 1);
2736 bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2737 p += len;
2738 }
2739 RT_REMREF_LOCKED(rt_router);
2740 RT_UNLOCK(rt_router);
2741 }
2742
2743 nolladdropt:;
2744
2745 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2746
2747 /* just to be safe */
2748 #ifdef M_DECRYPTED /*not openbsd*/
2749 if (m0->m_flags & M_DECRYPTED)
2750 goto noredhdropt;
2751 #endif
2752 if (p - (u_char *)ip6 > maxlen)
2753 goto noredhdropt;
2754
2755 {
2756 /* redirected header option */
2757 int len;
2758 struct nd_opt_rd_hdr *nd_opt_rh;
2759
2760 /*
2761 * compute the maximum size for icmp6 redirect header option.
2762 * XXX room for auth header?
2763 */
2764 len = maxlen - (p - (u_char *)ip6);
2765 len &= ~7;
2766
2767 /* This is just for simplicity. */
2768 if (m0->m_pkthdr.len != m0->m_len) {
2769 if (m0->m_next) {
2770 m_freem(m0->m_next);
2771 m0->m_next = NULL;
2772 }
2773 m0->m_pkthdr.len = m0->m_len;
2774 }
2775
2776 /*
2777 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2778 * about padding/truncate rule for the original IP packet.
2779 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2780 * - "attach as much as possible" is the goal
2781 * - pad if not aligned (original size can be guessed by original
2782 * ip6 header)
2783 * Following code adds the padding if it is simple enough,
2784 * and truncates if not.
2785 */
2786 if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2787 panic("assumption failed in %s:%d\n", __FILE__, __LINE__);
2788
2789 if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2790 /* not enough room, truncate */
2791 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2792 } else {
2793 /* enough room, pad or truncate */
2794 size_t extra;
2795
2796 extra = m0->m_pkthdr.len % 8;
2797 if (extra) {
2798 /* pad if easy enough, truncate if not */
2799 if (8 - extra <= M_TRAILINGSPACE(m0)) {
2800 /* pad */
2801 m0->m_len += (8 - extra);
2802 m0->m_pkthdr.len += (8 - extra);
2803 } else {
2804 /* truncate */
2805 m0->m_pkthdr.len -= extra;
2806 m0->m_len -= extra;
2807 }
2808 }
2809 len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2810 m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2811 }
2812
2813 nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2814 bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2815 nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2816 nd_opt_rh->nd_opt_rh_len = len >> 3;
2817 p += sizeof(*nd_opt_rh);
2818 m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2819
2820 /* connect m0 to m */
2821 m->m_next = m0;
2822 m->m_pkthdr.len = m->m_len + m0->m_len;
2823 }
2824 noredhdropt:;
2825
2826 /* XXX: clear embedded link IDs in the inner header */
2827 in6_clearscope(&sip6->ip6_src);
2828 in6_clearscope(&sip6->ip6_dst);
2829 in6_clearscope(&nd_rd->nd_rd_target);
2830 in6_clearscope(&nd_rd->nd_rd_dst);
2831
2832 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2833
2834 nd_rd->nd_rd_cksum = 0;
2835 nd_rd->nd_rd_cksum
2836 = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2837
2838 /* send the packet to outside... */
2839 ip6oa.ip6oa_boundif = ifp->if_index;
2840 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
2841
2842 ip6_output(m, NULL, NULL, IPV6_OUTARGS, NULL, &outif, &ip6oa);
2843 if (outif) {
2844 icmp6_ifstat_inc(outif, ifs6_out_msg);
2845 icmp6_ifstat_inc(outif, ifs6_out_redirect);
2846 ifnet_release(outif);
2847 }
2848 icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2849
2850 return;
2851
2852 fail:
2853 if (rt != NULL)
2854 RT_UNLOCK(rt);
2855 if (m)
2856 m_freem(m);
2857 if (m0)
2858 m_freem(m0);
2859 }
2860
2861 /*
2862 * ICMPv6 socket option processing.
2863 */
2864 int
2865 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2866 {
2867 int error = 0;
2868 int optlen;
2869 struct inpcb *inp = sotoinpcb(so);
2870 int level, op, optname;
2871
2872 if (sopt) {
2873 level = sopt->sopt_level;
2874 op = sopt->sopt_dir;
2875 optname = sopt->sopt_name;
2876 optlen = sopt->sopt_valsize;
2877 } else
2878 level = op = optname = optlen = 0;
2879
2880 if (level != IPPROTO_ICMPV6) {
2881 return EINVAL;
2882 }
2883
2884 switch (op) {
2885 case PRCO_SETOPT:
2886 switch (optname) {
2887 case ICMP6_FILTER:
2888 {
2889 struct icmp6_filter *p;
2890
2891 if (optlen != 0 && optlen != sizeof(*p)) {
2892 error = EMSGSIZE;
2893 break;
2894 }
2895 if (inp->in6p_icmp6filt == NULL) {
2896 error = EINVAL;
2897 break;
2898 }
2899
2900 if (optlen == 0) {
2901 /* According to RFC 3542, an installed filter can be
2902 * cleared by issuing a setsockopt for ICMP6_FILTER
2903 * with a zero length.
2904 */
2905 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
2906 } else {
2907 error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2908 optlen);
2909 }
2910 break;
2911 }
2912
2913 default:
2914 error = ENOPROTOOPT;
2915 break;
2916 }
2917 break;
2918
2919 case PRCO_GETOPT:
2920 switch (optname) {
2921 case ICMP6_FILTER:
2922 {
2923 if (inp->in6p_icmp6filt == NULL) {
2924 error = EINVAL;
2925 break;
2926 }
2927 error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2928 min(sizeof(struct icmp6_filter), optlen));
2929 break;
2930 }
2931
2932 default:
2933 error = ENOPROTOOPT;
2934 break;
2935 }
2936 break;
2937 }
2938
2939 return(error);
2940 }
2941
2942 /*
2943 * ICMPv6 socket datagram option processing.
2944 */
2945 int
2946 icmp6_dgram_ctloutput(struct socket *so, struct sockopt *sopt)
2947 {
2948 if (kauth_cred_issuser(so->so_cred))
2949 return icmp6_ctloutput(so, sopt);
2950
2951 if (sopt->sopt_level == IPPROTO_ICMPV6) {
2952 switch (sopt->sopt_name) {
2953 case ICMP6_FILTER:
2954 return icmp6_ctloutput(so, sopt);
2955 default:
2956 return EPERM;
2957 }
2958 }
2959
2960 if (sopt->sopt_level != IPPROTO_IPV6)
2961 return EINVAL;
2962
2963 switch (sopt->sopt_name) {
2964 case IPV6_UNICAST_HOPS:
2965 case IPV6_CHECKSUM:
2966 case IPV6_V6ONLY:
2967 case IPV6_USE_MIN_MTU:
2968 case IPV6_RECVRTHDR:
2969 case IPV6_RECVPKTINFO:
2970 case IPV6_RECVHOPLIMIT:
2971 case IPV6_PATHMTU:
2972 case IPV6_PKTINFO:
2973 case IPV6_HOPLIMIT:
2974 case IPV6_HOPOPTS:
2975 case IPV6_DSTOPTS:
2976 case IPV6_MULTICAST_IF:
2977 case IPV6_MULTICAST_HOPS:
2978 case IPV6_MULTICAST_LOOP:
2979 case IPV6_JOIN_GROUP:
2980 case IPV6_LEAVE_GROUP:
2981 case IPV6_PORTRANGE:
2982 case IPV6_IPSEC_POLICY:
2983 case IPV6_RECVTCLASS:
2984 case IPV6_TCLASS:
2985 case IPV6_2292PKTOPTIONS:
2986 case IPV6_2292PKTINFO:
2987 case IPV6_2292HOPLIMIT:
2988 case IPV6_2292HOPOPTS:
2989 case IPV6_2292DSTOPTS:
2990 case IPV6_2292RTHDR:
2991 case IPV6_BOUND_IF:
2992 case IPV6_NO_IFT_CELLULAR:
2993
2994 return ip6_ctloutput(so, sopt);
2995
2996 default:
2997 return EPERM;
2998 }
2999 }
3000
3001 __private_extern__ int
3002 icmp6_dgram_send(struct socket *so, int flags, struct mbuf *m,
3003 struct sockaddr *nam, struct mbuf *control, struct proc *p)
3004 {
3005 #pragma unused(flags, p)
3006 int error = 0;
3007 struct inpcb *inp = sotoinpcb(so);
3008 struct sockaddr_in6 tmp;
3009 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)(void *)nam;
3010 struct icmp6_hdr *icmp6;
3011
3012 if (inp == NULL
3013 #if NECP
3014 || (necp_socket_should_use_flow_divert(inp))
3015 #endif /* NECP */
3016 ) {
3017 error = (inp == NULL ? EINVAL : EPROTOTYPE);
3018 goto bad;
3019 }
3020
3021 if (kauth_cred_issuser(so->so_cred))
3022 return (rip6_output(m, so, SIN6(nam), control, 0));
3023
3024 /* always copy sockaddr to avoid overwrites */
3025 if (so->so_state & SS_ISCONNECTED) {
3026 if (nam != NULL) {
3027 error = EISCONN;
3028 goto bad;
3029 }
3030 /* XXX */
3031 bzero(&tmp, sizeof(tmp));
3032 tmp.sin6_family = AF_INET6;
3033 tmp.sin6_len = sizeof(struct sockaddr_in6);
3034 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
3035 sizeof(struct in6_addr));
3036 dst = &tmp;
3037 } else {
3038 if (nam == NULL) {
3039 error = ENOTCONN;
3040 goto bad;
3041 }
3042 tmp = *(struct sockaddr_in6 *)(void *)nam;
3043 dst = &tmp;
3044 }
3045
3046 /*
3047 * For an ICMPv6 packet, we should know its type and code
3048 */
3049 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
3050 if (m->m_len < sizeof(struct icmp6_hdr) &&
3051 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
3052 error = ENOBUFS;
3053 goto bad;
3054 }
3055 icmp6 = mtod(m, struct icmp6_hdr *);
3056
3057 /*
3058 * Allow only to send echo request and node information request
3059 * See RFC 2463 for Echo Request Message format
3060 */
3061 if ((icmp6->icmp6_type == ICMP6_ECHO_REQUEST &&
3062 icmp6->icmp6_code == 0) ||
3063 (icmp6->icmp6_type == ICMP6_NI_QUERY &&
3064 (icmp6->icmp6_code == ICMP6_NI_SUBJ_IPV6 ||
3065 icmp6->icmp6_code == ICMP6_NI_SUBJ_FQDN))) {
3066 /* Good */
3067 ;
3068 } else {
3069 error = EPERM;
3070 goto bad;
3071 }
3072 }
3073
3074 #if ENABLE_DEFAULT_SCOPE
3075 if (dst->sin6_scope_id == 0) { /* not change if specified */
3076 dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
3077 }
3078 #endif
3079
3080 return (rip6_output(m, so, dst, control, 0));
3081 bad:
3082 VERIFY(error != 0);
3083
3084 if (m != NULL)
3085 m_freem(m);
3086 if (control != NULL)
3087 m_freem(control);
3088
3089 return (error);
3090 }
3091
3092 /* Like rip6_attach but without root privilege enforcement */
3093 __private_extern__ int
3094 icmp6_dgram_attach(struct socket *so, int proto, struct proc *p)
3095 {
3096 struct inpcb *inp;
3097 int error;
3098
3099 inp = sotoinpcb(so);
3100 if (inp)
3101 panic("icmp6_dgram_attach");
3102
3103 if (proto != IPPROTO_ICMPV6)
3104 return EINVAL;
3105
3106 error = soreserve(so, rip_sendspace, rip_recvspace);
3107 if (error)
3108 return error;
3109 error = in_pcballoc(so, &ripcbinfo, p);
3110 if (error)
3111 return error;
3112 inp = (struct inpcb *)so->so_pcb;
3113 inp->inp_vflag |= INP_IPV6;
3114 inp->in6p_ip6_nxt = IPPROTO_ICMPV6;
3115 inp->in6p_hops = -1; /* use kernel default */
3116 inp->in6p_cksum = -1;
3117 MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
3118 sizeof(struct icmp6_filter), M_PCB, M_WAITOK);
3119 if (inp->in6p_icmp6filt == NULL)
3120 return (ENOMEM);
3121 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
3122 return 0;
3123 }
3124
3125
3126 /*
3127 * Perform rate limit check.
3128 * Returns 0 if it is okay to send the icmp6 packet.
3129 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
3130 * limitation.
3131 *
3132 * XXX per-destination check necessary?
3133 */
3134 static int
3135 icmp6_ratelimit(
3136 __unused const struct in6_addr *dst, /* not used at this moment */
3137 const int type,
3138 __unused const int code)
3139 {
3140 int ret;
3141
3142 ret = 0; /* okay to send */
3143
3144 /* PPS limit */
3145 if (type == ND_ROUTER_ADVERT) {
3146 if (!ppsratecheck(&icmp6rappslim_last, &icmp6rapps_count,
3147 icmp6rappslim))
3148 ret++;
3149 } else if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
3150 icmp6errppslim)) {
3151 /* The packet is subject to rate limit */
3152 ret++;
3153 }
3154
3155 return ret;
3156 }