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