]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/ip6_input.c
xnu-2050.9.2.tar.gz
[apple/xnu.git] / bsd / netinet6 / ip6_input.c
CommitLineData
2d21ac55 1/*
316670eb 2 * Copyright (c) 2003-2012 Apple Inc. All rights reserved.
2d21ac55
A
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 */
9bccf70c
A
28/* $FreeBSD: src/sys/netinet6/ip6_input.c,v 1.11.2.10 2001/07/24 19:10:18 brooks Exp $ */
29/* $KAME: ip6_input.c,v 1.194 2001/05/27 13:28:35 itojun Exp $ */
1c79356b
A
30
31/*
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60/*
61 * Copyright (c) 1982, 1986, 1988, 1993
62 * The Regents of the University of California. All rights reserved.
63 *
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
66 * are met:
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
72 * 3. All advertising materials mentioning features or use of this software
73 * must display the following acknowledgement:
74 * This product includes software developed by the University of
75 * California, Berkeley and its contributors.
76 * 4. Neither the name of the University nor the names of its contributors
77 * may be used to endorse or promote products derived from this software
78 * without specific prior written permission.
79 *
80 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
81 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 * SUCH DAMAGE.
91 *
92 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
93 */
9bccf70c 94
1c79356b
A
95
96#include <sys/param.h>
97#include <sys/systm.h>
98#include <sys/malloc.h>
99#include <sys/mbuf.h>
100#include <sys/domain.h>
101#include <sys/protosw.h>
102#include <sys/socket.h>
103#include <sys/socketvar.h>
104#include <sys/errno.h>
105#include <sys/time.h>
106#include <sys/kernel.h>
107#include <sys/syslog.h>
6d2010ae 108#include <sys/sysctl.h>
1c79356b 109#include <sys/proc.h>
91447636 110#include <sys/kauth.h>
6d2010ae
A
111#include <sys/mcache.h>
112#include <mach/mach_time.h>
113
114#include <pexpert/pexpert.h>
1c79356b
A
115
116#include <net/if.h>
9bccf70c 117#include <net/if_var.h>
1c79356b
A
118#include <net/if_types.h>
119#include <net/if_dl.h>
120#include <net/route.h>
2d21ac55 121#include <net/kpi_protocol.h>
6d2010ae 122#include <net/ntstat.h>
316670eb 123#include <net/init.h>
1c79356b
A
124
125#include <netinet/in.h>
126#include <netinet/in_systm.h>
127#if INET
128#include <netinet/ip.h>
129#include <netinet/ip_icmp.h>
130#endif /*INET*/
131#include <netinet/ip6.h>
132#include <netinet6/in6_var.h>
133#include <netinet6/ip6_var.h>
1c79356b 134#include <netinet/in_pcb.h>
1c79356b
A
135#include <netinet/icmp6.h>
136#include <netinet6/in6_ifattach.h>
137#include <netinet6/nd6.h>
6d2010ae
A
138#include <netinet6/scope6_var.h>
139#include <mach/sdt.h>
1c79356b 140
9bccf70c
A
141#if IPSEC
142#include <netinet6/ipsec.h>
143#if INET6
144#include <netinet6/ipsec6.h>
145#endif
146extern int ipsec_bypass;
1c79356b
A
147#endif
148
1c79356b 149#include <netinet6/ip6_fw.h>
1c79356b 150
316670eb
A
151#if DUMMYNET
152#include <netinet/ip_fw.h>
153#include <netinet/ip_dummynet.h>
154#endif /* DUMMYNET */
155
91447636
A
156#include <netinet/kpi_ipfilter_var.h>
157
1c79356b
A
158#include <netinet6/ip6protosw.h>
159
160/* we need it for NLOOP. */
1c79356b 161#include "loop.h"
1c79356b
A
162
163#include <net/net_osdep.h>
164
b0d623f7
A
165#if PF
166#include <net/pfvar.h>
167#endif /* PF */
168
1c79356b
A
169extern struct domain inet6domain;
170extern struct ip6protosw inet6sw[];
1c79356b
A
171
172struct ip6protosw * ip6_protox[IPPROTO_MAX];
173static int ip6qmaxlen = IFQ_MAXLEN;
6d2010ae
A
174
175static lck_grp_attr_t *in6_ifaddr_rwlock_grp_attr;
176static lck_grp_t *in6_ifaddr_rwlock_grp;
177static lck_attr_t *in6_ifaddr_rwlock_attr;
178decl_lck_rw_data(, in6_ifaddr_rwlock);
179
180/* Protected by in6_ifaddr_rwlock */
181struct in6_ifaddr *in6_ifaddrs = NULL;
9bccf70c 182
1c79356b
A
183int ip6_forward_srcrt; /* XXX */
184int ip6_sourcecheck; /* XXX */
185int ip6_sourcecheck_interval; /* XXX */
1c79356b 186const int int6intrq_present = 1;
1c79356b 187
9bccf70c 188int ip6_ours_check_algorithm;
9bccf70c 189
6d2010ae
A
190#define IN6_IFSTAT_REQUIRE_ALIGNED_64(f) \
191 _CASSERT(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t)))
316670eb 192
6d2010ae
A
193#define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f) \
194 _CASSERT(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t)))
9bccf70c 195
7e4a7d39 196#if IPFW2
1c79356b
A
197/* firewall hooks */
198ip6_fw_chk_t *ip6_fw_chk_ptr;
199ip6_fw_ctl_t *ip6_fw_ctl_ptr;
9bccf70c 200int ip6_fw_enable = 1;
7e4a7d39 201#endif
1c79356b
A
202
203struct ip6stat ip6stat;
204
9bccf70c
A
205#ifdef __APPLE__
206struct ifqueue ip6intrq;
6d2010ae 207decl_lck_mtx_data(, ip6_init_mutex);
316670eb
A
208decl_lck_mtx_data(, proxy6_lock);
209decl_lck_mtx_data(, dad6_mutex_data);
210decl_lck_mtx_data(, nd6_mutex_data);
211decl_lck_mtx_data(, prefix6_mutex_data);
212decl_lck_mtx_data(, scope6_mutex_data);
213lck_mtx_t *dad6_mutex = &dad6_mutex_data;
214lck_mtx_t *nd6_mutex = &nd6_mutex_data;
215lck_mtx_t *prefix6_mutex = &prefix6_mutex_data;
216lck_mtx_t *scope6_mutex = &scope6_mutex_data;
6d2010ae 217#ifdef ENABLE_ADDRSEL
316670eb
A
218decl_lck_mtx_data(, addrsel_mutex_data);
219lck_mtx_t *addrsel_mutex = &addrsel_mutex_data;
6d2010ae
A
220#endif
221decl_lck_rw_data(, in6_ifs_rwlock);
222decl_lck_rw_data(, icmp6_ifs_rwlock);
91447636 223lck_attr_t *ip6_mutex_attr;
b0d623f7
A
224lck_grp_t *ip6_mutex_grp;
225lck_grp_attr_t *ip6_mutex_grp_attr;
91447636 226extern lck_mtx_t *inet6_domain_mutex;
9bccf70c 227#endif
91447636 228extern int loopattach_done;
6d2010ae 229extern void addrsel_policy_init(void);
9bccf70c 230
316670eb 231static void ip6_init_delayed(void);
2d21ac55 232static struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *);
1c79356b 233
91447636 234static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
1c79356b 235#if PULLDOWN_TEST
91447636 236static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
1c79356b
A
237#endif
238
9bccf70c 239#ifdef __APPLE__
91447636 240void gifattach(void);
91447636 241void stfattach(void);
1c79356b
A
242#endif
243
6d2010ae
A
244SYSCTL_DECL(_net_inet6_ip6);
245
246int ip6_doscopedroute = 1;
247SYSCTL_INT(_net_inet6_ip6, OID_AUTO, scopedroute, CTLFLAG_RD | CTLFLAG_LOCKED,
248 &ip6_doscopedroute, 0, "Enable IPv6 scoped routing");
2d21ac55 249
316670eb
A
250int ip6_restrictrecvif = 1;
251SYSCTL_INT(_net_inet6_ip6, OID_AUTO, restrictrecvif,
252 CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_restrictrecvif, 0,
253 "Enable inbound interface restrictions");
254
255/*
256 * On platforms which require strict alignment (currently for anything but
257 * i386 or x86_64), check if the IP header pointer is 32-bit aligned; if not,
258 * copy the contents of the mbuf chain into a new chain, and free the original
259 * one. Create some head room in the first mbuf of the new chain, in case
260 * it's needed later on.
261 *
262 * RFC 2460 says that IPv6 headers are 64-bit aligned, but network interfaces
263 * mostly align to 32-bit boundaries. Care should be taken never to use 64-bit
264 * load/store operations on the fields in IPv6 headers.
265 */
266#if defined(__i386__) || defined(__x86_64__)
267#define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
268#else /* !__i386__ && !__x86_64__ */
269#define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { \
270 if (!IP6_HDR_ALIGNED_P(mtod(_m, caddr_t))) { \
271 struct mbuf *_n; \
272 struct ifnet *__ifp = (_ifp); \
273 atomic_add_64(&(__ifp)->if_alignerrs, 1); \
274 if (((_m)->m_flags & M_PKTHDR) && \
275 (_m)->m_pkthdr.header != NULL) \
276 (_m)->m_pkthdr.header = NULL; \
277 _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT); \
278 if (_n == NULL) { \
279 ip6stat.ip6s_toosmall++; \
280 m_freem(_m); \
281 (_m) = NULL; \
282 _action \
283 } else { \
284 VERIFY(_n != (_m)); \
285 (_m) = _n; \
286 } \
287 } \
288} while (0)
289#endif /* !__i386__ && !__x86_64__ */
290
91447636
A
291static void
292ip6_proto_input(
2d21ac55 293 __unused protocol_family_t protocol,
91447636
A
294 mbuf_t packet)
295{
296 ip6_input(packet);
297}
298
1c79356b
A
299/*
300 * IP6 initialization: fill in IP6 protocol switch table.
301 * All protocols not implemented in kernel go to raw IP6 protocol handler.
302 */
303void
304ip6_init()
305{
9bccf70c
A
306 struct ip6protosw *pr;
307 int i;
1c79356b
A
308 struct timeval tv;
309
6d2010ae
A
310 PE_parse_boot_argn("net.inet6.ip6.scopedroute", &ip6_doscopedroute,
311 sizeof (ip6_doscopedroute));
312
9bccf70c
A
313#if DIAGNOSTIC
314 if (sizeof(struct protosw) != sizeof(struct ip6protosw))
315 panic("sizeof(protosw) != sizeof(ip6protosw)");
316#endif
91447636 317 pr = (struct ip6protosw *)pffindproto_locked(PF_INET6, IPPROTO_RAW, SOCK_RAW);
1c79356b
A
318 if (pr == 0)
319 panic("ip6_init");
320 for (i = 0; i < IPPROTO_MAX; i++)
321 ip6_protox[i] = pr;
9bccf70c 322 for (pr = (struct ip6protosw*)inet6domain.dom_protosw; pr; pr = pr->pr_next) {
b0d623f7 323 if(!(pr->pr_domain)) continue; /* If uninitialized, skip */
1c79356b
A
324 if (pr->pr_domain->dom_family == PF_INET6 &&
325 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
326 ip6_protox[pr->pr_protocol] = pr;
327 }
328 }
329
91447636 330 ip6_mutex_grp_attr = lck_grp_attr_alloc_init();
91447636
A
331
332 ip6_mutex_grp = lck_grp_alloc_init("ip6", ip6_mutex_grp_attr);
333 ip6_mutex_attr = lck_attr_alloc_init();
91447636 334
316670eb
A
335 lck_mtx_init(dad6_mutex, ip6_mutex_grp, ip6_mutex_attr);
336 lck_mtx_init(nd6_mutex, ip6_mutex_grp, ip6_mutex_attr);
337 lck_mtx_init(prefix6_mutex, ip6_mutex_grp, ip6_mutex_attr);
338 lck_mtx_init(scope6_mutex, ip6_mutex_grp, ip6_mutex_attr);
91447636 339
6d2010ae 340#ifdef ENABLE_ADDRSEL
316670eb 341 lck_mtx_init(addrsel_mutex, ip6_mutex_grp, ip6_mutex_attr);
6d2010ae
A
342#endif
343
316670eb
A
344 lck_mtx_init(&proxy6_lock, ip6_mutex_grp, ip6_mutex_attr);
345 lck_mtx_init(&ip6_init_mutex, ip6_mutex_grp, ip6_mutex_attr);
346
6d2010ae
A
347 lck_rw_init(&in6_ifs_rwlock, ip6_mutex_grp, ip6_mutex_attr);
348 lck_rw_init(&icmp6_ifs_rwlock, ip6_mutex_grp, ip6_mutex_attr);
b0d623f7 349
91447636
A
350 inet6domain.dom_flags = DOM_REENTRANT;
351
1c79356b 352 ip6intrq.ifq_maxlen = ip6qmaxlen;
6d2010ae
A
353
354 in6_ifaddr_rwlock_grp_attr = lck_grp_attr_alloc_init();
355 in6_ifaddr_rwlock_grp = lck_grp_alloc_init("in6_ifaddr_rwlock",
356 in6_ifaddr_rwlock_grp_attr);
357 in6_ifaddr_rwlock_attr = lck_attr_alloc_init();
358 lck_rw_init(&in6_ifaddr_rwlock, in6_ifaddr_rwlock_grp,
359 in6_ifaddr_rwlock_attr);
360
361 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_receive);
362 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_hdrerr);
363 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_toobig);
364 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_noroute);
365 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_addrerr);
366 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_protounknown);
367 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_truncated);
368 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_discard);
369 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_deliver);
370 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_forward);
371 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_request);
372 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_discard);
373 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragok);
374 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragfail);
375 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragcreat);
376 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_reqd);
377 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_ok);
378 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_fail);
379 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mcast);
380 IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast);
381
382 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_msg);
383 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_error);
384 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_dstunreach);
385 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_adminprohib);
386 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_timeexceed);
387 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_paramprob);
388 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_pkttoobig);
389 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echo);
390 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echoreply);
391 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routersolicit);
392 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routeradvert);
393 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighborsolicit);
394 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighboradvert);
395 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_redirect);
396 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldquery);
397 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldreport);
398 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mlddone);
399
400 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_msg);
401 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_error);
402 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_dstunreach);
403 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_adminprohib);
404 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_timeexceed);
405 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_paramprob);
406 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_pkttoobig);
407 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echo);
408 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echoreply);
409 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routersolicit);
410 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routeradvert);
411 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighborsolicit);
412 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighboradvert);
413 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_redirect);
414 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldquery);
415 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldreport);
416 ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mlddone);
417
b0d623f7 418 in6_ifaddr_init();
6d2010ae 419 ip6_moptions_init();
1c79356b
A
420 nd6_init();
421 frag6_init();
55e303ae 422 icmp6_init();
6d2010ae 423 addrsel_policy_init();
1c79356b
A
424 /*
425 * in many cases, random() here does NOT return random number
426 * as initialization during bootstrap time occur in fixed order.
427 */
428 microtime(&tv);
429 ip6_flow_seq = random() ^ tv.tv_usec;
9bccf70c
A
430 microtime(&tv);
431 ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR;
91447636 432
316670eb
A
433 /*
434 * P2P interfaces often route the local address to the loopback
435 * interface. At this point, lo0 hasn't been initialized yet, which
436 * means that we need to delay the IPv6 configuration of lo0.
437 */
438 net_init_add(ip6_init_delayed);
439
440 domain_proto_mtx_unlock(TRUE);
2d21ac55 441 proto_register_input(PF_INET6, ip6_proto_input, NULL, 0);
316670eb 442 domain_proto_mtx_lock();
1c79356b
A
443}
444
445static void
316670eb 446ip6_init_delayed(void)
1c79356b 447{
b0d623f7 448 (void) in6_ifattach(lo_ifp, NULL, NULL);
1c79356b 449
9bccf70c
A
450#ifdef __APPLE__
451 /* nd6_timer_init */
91447636 452 timeout(nd6_timer, (caddr_t)0, hz);
1c79356b 453
9bccf70c 454 /* timer for regeneranation of temporary addresses randomize ID */
91447636 455 timeout(in6_tmpaddrtimer, (caddr_t)0,
9bccf70c
A
456 (ip6_temp_preferred_lifetime - ip6_desync_factor -
457 ip6_temp_regen_advance) * hz);
458
459#if NGIF
460 gifattach();
1c79356b 461#endif
9bccf70c
A
462#if NSTF
463 stfattach();
464#endif
316670eb 465#endif /* __APPLE__ */
6d2010ae 466}
1c79356b
A
467
468void
6d2010ae 469ip6_input(struct mbuf *m)
1c79356b
A
470{
471 struct ip6_hdr *ip6;
472 int off = sizeof(struct ip6_hdr), nest;
473 u_int32_t plen;
474 u_int32_t rtalert = ~0;
475 int nxt = 0, ours = 0;
476 struct ifnet *deliverifp = NULL;
91447636
A
477 ipfilter_t inject_ipfref = 0;
478 int seen;
6d2010ae
A
479 struct in6_ifaddr *ia6 = NULL;
480 struct route_in6 ip6_forward_rt;
481 struct sockaddr_in6 *dst6;
316670eb
A
482#if DUMMYNET
483 struct m_tag *tag;
484 struct ip_fw_args args;
485
486 bzero(&args, sizeof(struct ip_fw_args));
487#endif /* DUMMYNET */
6d2010ae
A
488
489 bzero(&ip6_forward_rt, sizeof(ip6_forward_rt));
490
491 /* Check if the packet we received is valid after interface filter
492 * processing
493 */
494 MBUF_INPUT_CHECK(m, m->m_pkthdr.rcvif);
1c79356b 495
316670eb
A
496 /* Perform IP header alignment fixup, if needed */
497 IP6_HDR_ALIGNMENT_FIXUP(m, m->m_pkthdr.rcvif, return;);
498
499#if DUMMYNET
500 if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
501 KERNEL_TAG_TYPE_DUMMYNET, NULL)) != NULL) {
502 struct dn_pkt_tag *dn_tag;
503
504 dn_tag = (struct dn_pkt_tag *)(tag+1);
505
506 args.fwa_pf_rule = dn_tag->dn_pf_rule;
507
508 m_tag_delete(m, tag);
509 }
510
511 if (args.fwa_pf_rule) {
512 ip6 = mtod(m, struct ip6_hdr *); /* In case PF got disabled */
513
514 goto check_with_pf;
515 }
516#endif /* DUMMYNET */
517
91447636
A
518 /*
519 * No need to proccess packet twice if we've
520 * already seen it
521 */
522 inject_ipfref = ipf_get_inject_filter(m);
523 if (inject_ipfref != 0) {
524 ip6 = mtod(m, struct ip6_hdr *);
525 nxt = ip6->ip6_nxt;
526 seen = 0;
527 goto injectit;
528 } else
529 seen = 1;
316670eb 530
1c79356b
A
531#if IPSEC
532 /*
533 * should the inner packet be considered authentic?
534 * see comment in ah4_input().
535 */
536 if (m) {
537 m->m_flags &= ~M_AUTHIPHDR;
538 m->m_flags &= ~M_AUTHIPDGM;
539 }
540#endif
541
9bccf70c
A
542 /*
543 * make sure we don't have onion peering information into m_aux.
544 */
545 ip6_delaux(m);
546
1c79356b 547 /*
55e303ae 548 * mbuf statistics
1c79356b
A
549 */
550 if (m->m_flags & M_EXT) {
551 if (m->m_next)
552 ip6stat.ip6s_mext2m++;
553 else
554 ip6stat.ip6s_mext1++;
555 } else {
9bccf70c 556#define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
1c79356b 557 if (m->m_next) {
9bccf70c 558 if (m->m_flags & M_LOOP) {
2d21ac55 559 ip6stat.ip6s_m2m[ifnet_index(lo_ifp)]++; /* XXX */
9bccf70c 560 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
1c79356b
A
561 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
562 else
563 ip6stat.ip6s_m2m[0]++;
564 } else
565 ip6stat.ip6s_m1++;
9bccf70c 566#undef M2MMAX
1c79356b
A
567 }
568
316670eb
A
569 /*
570 * Drop the packet if IPv6 operation is disabled on the IF;
571 * accessing the flag is done without acquiring nd_ifinfo lock
572 * for performance reasons.
573 */
6d2010ae
A
574 lck_rw_lock_shared(nd_if_rwlock);
575 if (m->m_pkthdr.rcvif->if_index < nd_ifinfo_indexlim &&
576 (nd_ifinfo[m->m_pkthdr.rcvif->if_index].flags & ND6_IFF_IFDISABLED)) {
577 lck_rw_done(nd_if_rwlock);
578 goto bad;
579 }
580 lck_rw_done(nd_if_rwlock);
581
1c79356b
A
582 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
583 ip6stat.ip6s_total++;
584
585#ifndef PULLDOWN_TEST
9bccf70c
A
586 /*
587 * L2 bridge code and some other code can return mbuf chain
588 * that does not conform to KAME requirement. too bad.
589 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram?
590 */
591 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
592 struct mbuf *n;
593
2d21ac55 594 MGETHDR(n, M_DONTWAIT, MT_HEADER); /* MAC-OK */
9bccf70c
A
595 if (n)
596 M_COPY_PKTHDR(n, m);
597 if (n && m->m_pkthdr.len > MHLEN) {
598 MCLGET(n, M_DONTWAIT);
599 if ((n->m_flags & M_EXT) == 0) {
600 m_freem(n);
601 n = NULL;
602 }
603 }
6d2010ae
A
604 if (n == NULL)
605 goto bad;
9bccf70c
A
606
607 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
608 n->m_len = m->m_pkthdr.len;
609 m_freem(m);
610 m = n;
611 }
91447636 612 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr),
6d2010ae 613 {goto done;});
1c79356b
A
614#endif
615
616 if (m->m_len < sizeof(struct ip6_hdr)) {
617 struct ifnet *inifp;
618 inifp = m->m_pkthdr.rcvif;
619 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
620 ip6stat.ip6s_toosmall++;
621 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
6d2010ae 622 goto done;
1c79356b
A
623 }
624 }
625
626 ip6 = mtod(m, struct ip6_hdr *);
627
628 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
629 ip6stat.ip6s_badvers++;
630 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
631 goto bad;
632 }
633
634 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
635
7e4a7d39 636#if IPFW2
1c79356b
A
637 /*
638 * Check with the firewall...
639 */
9bccf70c 640 if (ip6_fw_enable && ip6_fw_chk_ptr) {
1c79356b
A
641 u_short port = 0;
642 /* If ipfw says divert, we have to just drop packet */
643 /* use port as a dummy argument */
644 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
645 m_freem(m);
646 m = NULL;
647 }
6d2010ae
A
648 if (!m)
649 goto done;
1c79356b 650 }
7e4a7d39 651#endif
1c79356b
A
652
653 /*
9bccf70c 654 * Check against address spoofing/corruption.
1c79356b
A
655 */
656 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
657 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
9bccf70c
A
658 /*
659 * XXX: "badscope" is not very suitable for a multicast source.
660 */
661 ip6stat.ip6s_badscope++;
662 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
663 goto bad;
664 }
6d2010ae
A
665 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
666 !(m->m_flags & M_LOOP)) {
667 /*
668 * In this case, the packet should come from the loopback
669 * interface. However, we cannot just check the if_flags,
670 * because ip6_mloopback() passes the "actual" interface
671 * as the outgoing/incoming interface.
672 */
1c79356b
A
673 ip6stat.ip6s_badscope++;
674 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
675 goto bad;
676 }
55e303ae 677
1c79356b 678 /*
9bccf70c
A
679 * The following check is not documented in specs. A malicious
680 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
681 * and bypass security checks (act as if it was from 127.0.0.1 by using
6d2010ae 682 * IPv6 src ::ffff:127.0.0.1). Be cautious.
9bccf70c
A
683 *
684 * This check chokes if we are in an SIIT cloud. As none of BSDs
685 * support IPv4-less kernel compilation, we cannot support SIIT
686 * environment at all. So, it makes more sense for us to reject any
687 * malicious packets for non-SIIT environment, than try to do a
6d2010ae 688 * partial support for SIIT environment.
1c79356b 689 */
9bccf70c
A
690 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
691 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
692 ip6stat.ip6s_badscope++;
693 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
694 goto bad;
695 }
1c79356b
A
696#if 0
697 /*
698 * Reject packets with IPv4 compatible addresses (auto tunnel).
699 *
700 * The code forbids auto tunnel relay case in RFC1933 (the check is
701 * stronger than RFC1933). We may want to re-enable it if mech-xx
702 * is revised to forbid relaying case.
703 */
704 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
705 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
706 ip6stat.ip6s_badscope++;
707 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
708 goto bad;
709 }
710#endif
9bccf70c 711
6d2010ae
A
712 /*
713 * Naively assume we can attribute inbound data to the route we would
714 * use to send to this destination. Asymetric routing breaks this
715 * assumption, but it still allows us to account for traffic from
716 * a remote node in the routing table.
717 * this has a very significant performance impact so we bypass
718 * if nstat_collect is disabled. We may also bypass if the
719 * protocol is tcp in the future because tcp will have a route that
720 * we can use to attribute the data to. That does mean we would not
721 * account for forwarded tcp traffic.
722 */
723 if (nstat_collect) {
724 struct rtentry *rte =
725 ifnet_cached_rtlookup_inet6(m->m_pkthdr.rcvif,
726 &ip6->ip6_src);
727 if (rte != NULL) {
728 nstat_route_rx(rte, 1, m->m_pkthdr.len, 0);
729 rtfree(rte);
730 }
731 }
732
316670eb
A
733#if DUMMYNET
734check_with_pf:
735#endif
b0d623f7
A
736#if PF
737 /* Invoke inbound packet filter */
6d2010ae
A
738 if (PF_IS_ENABLED) {
739 int error;
316670eb
A
740#if DUMMYNET
741 error = pf_af_hook(m->m_pkthdr.rcvif, NULL, &m, AF_INET6, TRUE, &args);
742#else
743 error = pf_af_hook(m->m_pkthdr.rcvif, NULL, &m, AF_INET6, TRUE, NULL);
744#endif
745 if (error != 0 || m == NULL) {
6d2010ae
A
746 if (m != NULL) {
747 panic("%s: unexpected packet %p\n", __func__, m);
748 /* NOTREACHED */
749 }
750 /* Already freed by callee */
751 goto done;
b0d623f7 752 }
6d2010ae 753 ip6 = mtod(m, struct ip6_hdr *);
b0d623f7 754 }
b0d623f7
A
755#endif /* PF */
756
9bccf70c
A
757 /* drop packets if interface ID portion is already filled */
758 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
759 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
760 ip6->ip6_src.s6_addr16[1]) {
761 ip6stat.ip6s_badscope++;
762 goto bad;
763 }
764 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
765 ip6->ip6_dst.s6_addr16[1]) {
1c79356b 766 ip6stat.ip6s_badscope++;
1c79356b
A
767 goto bad;
768 }
769 }
770
9bccf70c
A
771 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
772 ip6->ip6_src.s6_addr16[1]
773 = htons(m->m_pkthdr.rcvif->if_index);
774 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
775 ip6->ip6_dst.s6_addr16[1]
776 = htons(m->m_pkthdr.rcvif->if_index);
777
1c79356b
A
778 /*
779 * Multicast check
780 */
781 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
6d2010ae 782 struct in6_multi *in6m = NULL;
b0d623f7 783 struct ifnet *ifp = m->m_pkthdr.rcvif;
1c79356b 784
b0d623f7 785 in6_ifstat_inc(ifp, ifs6_in_mcast);
1c79356b
A
786 /*
787 * See if we belong to the destination multicast group on the
788 * arrival interface.
789 */
6d2010ae
A
790 in6_multihead_lock_shared();
791 IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp, in6m);
792 in6_multihead_lock_done();
793 if (in6m != NULL) {
794 IN6M_REMREF(in6m);
1c79356b 795 ours = 1;
316670eb 796 } else if (!nd6_prproxy
b7266188 797#if MROUTING
316670eb 798 && !ip6_mrouter
b7266188 799#endif
316670eb 800 ) {
1c79356b
A
801 ip6stat.ip6s_notmember++;
802 ip6stat.ip6s_cantforward++;
b0d623f7 803 in6_ifstat_inc(ifp, ifs6_in_discard);
1c79356b
A
804 goto bad;
805 }
b0d623f7 806 deliverifp = ifp;
1c79356b
A
807 goto hbhcheck;
808 }
809
810 /*
811 * Unicast check
812 */
6d2010ae
A
813 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
814 dst6->sin6_len = sizeof(struct sockaddr_in6);
815 dst6->sin6_family = AF_INET6;
816 dst6->sin6_addr = ip6->ip6_dst;
1c79356b 817
6d2010ae
A
818 rtalloc_scoped_ign((struct route *)&ip6_forward_rt,
819 RTF_PRCLONING, IFSCOPE_NONE);
820 if (ip6_forward_rt.ro_rt != NULL)
821 RT_LOCK(ip6_forward_rt.ro_rt);
1c79356b
A
822
823#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
824
825 /*
826 * Accept the packet if the forwarding interface to the destination
827 * according to the routing table is the loopback interface,
828 * unless the associated route has a gateway.
829 * Note that this approach causes to accept a packet if there is a
830 * route to the loopback interface for the destination of the packet.
831 * But we think it's even useful in some situations, e.g. when using
832 * a special daemon which wants to intercept the packet.
9bccf70c
A
833 *
834 * XXX: some OSes automatically make a cloned route for the destination
835 * of an outgoing packet. If the outgoing interface of the packet
836 * is a loopback one, the kernel would consider the packet to be
837 * accepted, even if we have no such address assinged on the interface.
838 * We check the cloned flag of the route entry to reject such cases,
839 * assuming that route entries for our own addresses are not made by
840 * cloning (it should be true because in6_addloop explicitly installs
841 * the host route). However, we might have to do an explicit check
842 * while it would be less efficient. Or, should we rather install a
843 * reject route for such a case?
1c79356b 844 */
b0d623f7 845 if (ip6_forward_rt.ro_rt != NULL &&
1c79356b
A
846 (ip6_forward_rt.ro_rt->rt_flags &
847 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
9bccf70c
A
848#if RTF_WASCLONED
849 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
850#endif
1c79356b
A
851#if 0
852 /*
853 * The check below is redundant since the comparison of
854 * the destination and the key of the rtentry has
855 * already done through looking up the routing table.
856 */
857 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
9bccf70c 858 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
1c79356b
A
859#endif
860 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
6d2010ae 861 ia6 = (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
9bccf70c
A
862
863 /*
864 * record address information into m_aux.
865 */
866 (void)ip6_setdstifaddr(m, ia6);
867
868 /*
869 * packets to a tentative, duplicated, or somehow invalid
870 * address must not be accepted.
871 */
6d2010ae
A
872 RT_CONVERT_LOCK(ip6_forward_rt.ro_rt); /* just in case */
873 IFA_LOCK_SPIN(&ia6->ia_ifa);
1c79356b 874 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
6d2010ae 875 IFA_UNLOCK(&ia6->ia_ifa);
9bccf70c 876 /* this address is ready */
1c79356b
A
877 ours = 1;
878 deliverifp = ia6->ia_ifp; /* correct? */
9bccf70c 879 /* Count the packet in the ip address stats */
9bccf70c 880
b0d623f7 881 RT_UNLOCK(ip6_forward_rt.ro_rt);
6d2010ae 882 ia6 = NULL;
1c79356b 883 goto hbhcheck;
1c79356b 884 }
6d2010ae
A
885 IFA_UNLOCK(&ia6->ia_ifa);
886 RT_UNLOCK(ip6_forward_rt.ro_rt);
887 /* address is not ready, so discard the packet. */
888 nd6log((LOG_INFO,
889 "ip6_input: packet to an unready address %s->%s\n",
890 ip6_sprintf(&ip6->ip6_src),
891 ip6_sprintf(&ip6->ip6_dst)));
892 ia6 = NULL;
893 goto bad;
1c79356b
A
894 }
895
b0d623f7
A
896 if (ip6_forward_rt.ro_rt != NULL)
897 RT_UNLOCK(ip6_forward_rt.ro_rt);
1c79356b 898
1c79356b
A
899 /*
900 * Now there is no reason to process the packet if it's not our own
901 * and we're not a router.
902 */
903 if (!ip6_forwarding) {
904 ip6stat.ip6s_cantforward++;
905 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
906 goto bad;
907 }
908
909 hbhcheck:
9bccf70c
A
910 /*
911 * record address information into m_aux, if we don't have one yet.
912 * note that we are unable to record it, if the address is not listed
316670eb 913 * as our interface address (e.g. multicast addresses, etc.)
9bccf70c 914 */
6d2010ae 915 if (deliverifp && (ia6 = ip6_getdstifaddr(m)) == NULL) {
9bccf70c
A
916 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
917 if (ia6) {
918 if (!ip6_setdstifaddr(m, ia6)) {
919 /*
920 * XXX maybe we should drop the packet here,
921 * as we could not provide enough information
922 * to the upper layers.
923 */
924 }
6d2010ae
A
925 IFA_REMREF(&ia6->ia_ifa);
926 ia6 = NULL;
9bccf70c
A
927 }
928 }
929
6d2010ae
A
930 if (ia6 != NULL) {
931 IFA_REMREF(&ia6->ia_ifa);
932 ia6 = NULL;
933 }
934
1c79356b
A
935 /*
936 * Process Hop-by-Hop options header if it's contained.
937 * m may be modified in ip6_hopopts_input().
938 * If a JumboPayload option is included, plen will also be modified.
939 */
940 plen = (u_int32_t)ntohs(ip6->ip6_plen);
941 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
942 struct ip6_hbh *hbh;
943
944 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
945#if 0 /*touches NULL pointer*/
946 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
947#endif
6d2010ae 948 goto done; /* m have already been freed */
1c79356b 949 }
9bccf70c 950
1c79356b
A
951 /* adjust pointer */
952 ip6 = mtod(m, struct ip6_hdr *);
9bccf70c
A
953
954 /*
55e303ae 955 * if the payload length field is 0 and the next header field
9bccf70c
A
956 * indicates Hop-by-Hop Options header, then a Jumbo Payload
957 * option MUST be included.
958 */
959 if (ip6->ip6_plen == 0 && plen == 0) {
960 /*
961 * Note that if a valid jumbo payload option is
6d2010ae
A
962 * contained, ip6_hopopts_input() must set a valid
963 * (non-zero) payload length to the variable plen.
9bccf70c
A
964 */
965 ip6stat.ip6s_badoptions++;
966 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
967 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
968 icmp6_error(m, ICMP6_PARAM_PROB,
969 ICMP6_PARAMPROB_HEADER,
970 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
6d2010ae 971 goto done;
9bccf70c 972 }
1c79356b
A
973#ifndef PULLDOWN_TEST
974 /* ip6_hopopts_input() ensures that mbuf is contiguous */
975 hbh = (struct ip6_hbh *)(ip6 + 1);
976#else
977 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
978 sizeof(struct ip6_hbh));
979 if (hbh == NULL) {
980 ip6stat.ip6s_tooshort++;
6d2010ae 981 goto done;
1c79356b
A
982 }
983#endif
984 nxt = hbh->ip6h_nxt;
985
986 /*
6d2010ae
A
987 * If we are acting as a router and the packet contains a
988 * router alert option, see if we know the option value.
989 * Currently, we only support the option value for MLD, in which
990 * case we should pass the packet to the multicast routing
991 * daemon.
1c79356b 992 */
6d2010ae
A
993 if (rtalert != ~0 && ip6_forwarding) {
994 switch (rtalert) {
995 case IP6OPT_RTALERT_MLD:
996 ours = 1;
997 break;
998 default:
999 /*
1000 * RFC2711 requires unrecognized values must be
1001 * silently ignored.
1002 */
1003 break;
1004 }
1005 }
1c79356b
A
1006 } else
1007 nxt = ip6->ip6_nxt;
1008
1009 /*
1010 * Check that the amount of data in the buffers
1011 * is as at least much as the IPv6 header would have us expect.
1012 * Trim mbufs if longer than we expect.
1013 * Drop packet if shorter than we expect.
1014 */
1015 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
1016 ip6stat.ip6s_tooshort++;
1017 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1018 goto bad;
1019 }
1020 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
1021 if (m->m_len == m->m_pkthdr.len) {
1022 m->m_len = sizeof(struct ip6_hdr) + plen;
1023 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
1024 } else
1025 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
1026 }
1027
1028 /*
1029 * Forward if desirable.
1030 */
1031 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1032 /*
1033 * If we are acting as a multicast router, all
1034 * incoming multicast packets are passed to the
1035 * kernel-level multicast forwarding function.
1036 * The packet is returned (relatively) intact; if
1037 * ip6_mforward() returns a non-zero value, the packet
1038 * must be discarded, else it may be accepted below.
1039 */
b7266188 1040#if MROUTING
1c79356b
A
1041 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
1042 ip6stat.ip6s_cantforward++;
6d2010ae 1043 goto bad;
1c79356b 1044 }
b7266188 1045#endif
316670eb
A
1046 if (!ours && nd6_prproxy) {
1047 /*
1048 * If this isn't for us, this might be a Neighbor
1049 * Solicitation (dst is solicited-node multicast)
1050 * against an address in one of the proxied prefixes;
1051 * if so, claim the packet and let icmp6_input()
1052 * handle the rest.
1053 */
1054 ours = nd6_prproxy_isours(m, ip6, NULL, IFSCOPE_NONE);
1055 VERIFY(!ours ||
1056 (m->m_pkthdr.aux_flags & MAUXF_PROXY_DST));
1057 }
6d2010ae
A
1058 if (!ours)
1059 goto bad;
1c79356b 1060 } else if (!ours) {
316670eb
A
1061 /*
1062 * The unicast forwarding function might return the packet
1063 * if we are proxying prefix(es), and if the packet is an
1064 * ICMPv6 packet that has failed the zone checks, but is
1065 * targetted towards a proxied address (this is optimized by
1066 * way of RTF_PROXY test.) If so, claim the packet as ours
1067 * and let icmp6_input() handle the rest. The packet's hop
1068 * limit value is kept intact (it's not decremented). This
1069 * is for supporting Neighbor Unreachability Detection between
1070 * proxied nodes on different links (src is link-local, dst
1071 * is target address.)
1072 */
1073 if ((m = ip6_forward(m, &ip6_forward_rt, 0)) == NULL)
1074 goto done;
1075 VERIFY(ip6_forward_rt.ro_rt != NULL);
1076 VERIFY(m->m_pkthdr.aux_flags & MAUXF_PROXY_DST);
1077 deliverifp = ip6_forward_rt.ro_rt->rt_ifp;
1078 ours = 1;
1079 }
1c79356b
A
1080
1081 ip6 = mtod(m, struct ip6_hdr *);
1082
1083 /*
1084 * Malicious party may be able to use IPv4 mapped addr to confuse
1085 * tcp/udp stack and bypass security checks (act as if it was from
1086 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
1087 *
1088 * For SIIT end node behavior, you may want to disable the check.
1089 * However, you will become vulnerable to attacks using IPv4 mapped
1090 * source.
1091 */
1092 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
1093 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
1094 ip6stat.ip6s_badscope++;
1095 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1096 goto bad;
1097 }
1098
1099 /*
1100 * Tell launch routine the next header
1101 */
1c79356b
A
1102 ip6stat.ip6s_delivered++;
1103 in6_ifstat_inc(deliverifp, ifs6_in_deliver);
91447636 1104
91447636 1105injectit:
1c79356b
A
1106 nest = 0;
1107
316670eb
A
1108 /*
1109 * Perform IP header alignment fixup again, if needed. Note that
1110 * we do it once for the outermost protocol, and we assume each
1111 * protocol handler wouldn't mess with the alignment afterwards.
1112 */
1113 IP6_HDR_ALIGNMENT_FIXUP(m, m->m_pkthdr.rcvif, return;);
1114
1c79356b 1115 while (nxt != IPPROTO_DONE) {
91447636 1116 struct ipfilter *filter;
6d2010ae 1117 int (*pr_input)(struct mbuf **, int *, int);
0b4c1975 1118
1c79356b
A
1119 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
1120 ip6stat.ip6s_toomanyhdr++;
6d2010ae 1121 goto bad;
1c79356b
A
1122 }
1123
1124 /*
1125 * protection against faulty packet - there should be
1126 * more sanity checks in header chain processing.
1127 */
9bccf70c 1128 if (m->m_pkthdr.len < off) {
1c79356b
A
1129 ip6stat.ip6s_tooshort++;
1130 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
9bccf70c
A
1131 goto bad;
1132 }
6d2010ae 1133
9bccf70c
A
1134
1135#if IPSEC
1136 /*
1137 * enforce IPsec policy checking if we are seeing last header.
1138 * note that we do not visit this with protocols with pcb layer
1139 * code - like udp/tcp/raw ip.
1140 */
316670eb
A
1141 if ((ipsec_bypass == 0) &&
1142 (ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0) {
91447636 1143 if (ipsec6_in_reject(m, NULL)) {
2d21ac55 1144 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
6d2010ae 1145 goto bad;
2d21ac55 1146 }
1c79356b
A
1147 }
1148#endif
9bccf70c 1149
91447636 1150 /*
2d21ac55 1151 * Call IP filter
91447636 1152 */
2d21ac55 1153 if (!TAILQ_EMPTY(&ipv6_filters)) {
91447636
A
1154 ipf_ref();
1155 TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) {
1156 if (seen == 0) {
316670eb
A
1157 if ((struct ipfilter *)inject_ipfref ==
1158 filter)
91447636
A
1159 seen = 1;
1160 } else if (filter->ipf_filter.ipf_input) {
1161 errno_t result;
316670eb 1162
91447636 1163 result = filter->ipf_filter.ipf_input(
316670eb
A
1164 filter->ipf_filter.cookie,
1165 (mbuf_t *)&m, off, nxt);
91447636
A
1166 if (result == EJUSTRETURN) {
1167 ipf_unref();
6d2010ae 1168 goto done;
91447636
A
1169 }
1170 if (result != 0) {
1171 ipf_unref();
6d2010ae 1172 goto bad;
91447636
A
1173 }
1174 }
1175 }
1176 ipf_unref();
1177 }
0b4c1975 1178
6d2010ae
A
1179 DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL,
1180 struct ip6_hdr *, ip6, struct ifnet *, m->m_pkthdr.rcvif,
1181 struct ip *, NULL, struct ip6_hdr *, ip6);
1182
0b4c1975
A
1183 if ((pr_input = ip6_protox[nxt]->pr_input) == NULL) {
1184 m_freem(m);
1185 m = NULL;
1186 nxt = IPPROTO_DONE;
1187 } else if (!(ip6_protox[nxt]->pr_flags & PR_PROTOLOCK)) {
91447636 1188 lck_mtx_lock(inet6_domain_mutex);
6d2010ae 1189 nxt = pr_input(&m, &off, nxt);
91447636 1190 lck_mtx_unlock(inet6_domain_mutex);
0b4c1975 1191 } else {
6d2010ae 1192 nxt = pr_input(&m, &off, nxt);
91447636 1193 }
1c79356b 1194 }
6d2010ae
A
1195done:
1196 if (ip6_forward_rt.ro_rt != NULL)
1197 rtfree(ip6_forward_rt.ro_rt);
1c79356b
A
1198 return;
1199 bad:
1200 m_freem(m);
6d2010ae 1201 goto done;
1c79356b
A
1202}
1203
9bccf70c
A
1204/*
1205 * set/grab in6_ifaddr correspond to IPv6 destination address.
1206 * XXX backward compatibility wrapper
1207 */
2d21ac55
A
1208static struct ip6aux *
1209ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6)
9bccf70c 1210{
2d21ac55 1211 struct ip6aux *n;
9bccf70c
A
1212
1213 n = ip6_addaux(m);
6d2010ae
A
1214 if (n != NULL) {
1215 if (ia6 != NULL)
1216 IFA_ADDREF(&ia6->ia_ifa);
1217 if (n->ip6a_dstia6 != NULL)
1218 IFA_REMREF(&n->ip6a_dstia6->ia_ifa);
2d21ac55 1219 n->ip6a_dstia6 = ia6;
6d2010ae 1220 }
2d21ac55 1221 return (struct ip6aux *)n; /* NULL if failed to set */
9bccf70c
A
1222}
1223
1224struct in6_ifaddr *
1225ip6_getdstifaddr(m)
1226 struct mbuf *m;
1227{
2d21ac55 1228 struct ip6aux *n;
9bccf70c
A
1229
1230 n = ip6_findaux(m);
6d2010ae
A
1231 if (n != NULL) {
1232 if (n->ip6a_dstia6 != NULL)
1233 IFA_ADDREF(&n->ip6a_dstia6->ia_ifa);
1234 return (n->ip6a_dstia6);
1235 }
1236 return (NULL);
9bccf70c
A
1237}
1238
1c79356b
A
1239/*
1240 * Hop-by-Hop options header processing. If a valid jumbo payload option is
1241 * included, the real payload length will be stored in plenp.
1242 */
1243static int
6d2010ae
A
1244ip6_hopopts_input(uint32_t *plenp, uint32_t *rtalertp, struct mbuf **mp,
1245 int *offp)
1c79356b 1246{
9bccf70c 1247 struct mbuf *m = *mp;
1c79356b
A
1248 int off = *offp, hbhlen;
1249 struct ip6_hbh *hbh;
1250 u_int8_t *opt;
1251
1252 /* validation of the length of the header */
1253#ifndef PULLDOWN_TEST
91447636 1254 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), return -1);
1c79356b
A
1255 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1256 hbhlen = (hbh->ip6h_len + 1) << 3;
1257
91447636 1258 IP6_EXTHDR_CHECK(m, off, hbhlen, return -1);
1c79356b
A
1259 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1260#else
1261 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1262 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
1263 if (hbh == NULL) {
1264 ip6stat.ip6s_tooshort++;
1265 return -1;
1266 }
1267 hbhlen = (hbh->ip6h_len + 1) << 3;
1268 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
1269 hbhlen);
1270 if (hbh == NULL) {
1271 ip6stat.ip6s_tooshort++;
1272 return -1;
1273 }
1274#endif
1275 off += hbhlen;
1276 hbhlen -= sizeof(struct ip6_hbh);
1277 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
1278
1279 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
1280 hbhlen, rtalertp, plenp) < 0)
6d2010ae 1281 return (-1);
1c79356b
A
1282
1283 *offp = off;
1284 *mp = m;
6d2010ae 1285 return (0);
1c79356b
A
1286}
1287
1288/*
1289 * Search header for all Hop-by-hop options and process each option.
1290 * This function is separate from ip6_hopopts_input() in order to
1291 * handle a case where the sending node itself process its hop-by-hop
1292 * options header. In such a case, the function is called from ip6_output().
9bccf70c
A
1293 *
1294 * The function assumes that hbh header is located right after the IPv6 header
1295 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1296 * opthead + hbhlen is located in continuous memory region.
1c79356b
A
1297 */
1298int
1299ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
1300 struct mbuf *m;
1301 u_int8_t *opthead;
1302 int hbhlen;
1303 u_int32_t *rtalertp;
1304 u_int32_t *plenp;
1305{
1306 struct ip6_hdr *ip6;
1307 int optlen = 0;
1308 u_int8_t *opt = opthead;
1309 u_int16_t rtalert_val;
9bccf70c
A
1310 u_int32_t jumboplen;
1311 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1c79356b
A
1312
1313 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
9bccf70c
A
1314 switch (*opt) {
1315 case IP6OPT_PAD1:
1316 optlen = 1;
1317 break;
1318 case IP6OPT_PADN:
1319 if (hbhlen < IP6OPT_MINLEN) {
1320 ip6stat.ip6s_toosmall++;
1321 goto bad;
1322 }
1323 optlen = *(opt + 1) + 2;
1324 break;
6d2010ae 1325 case IP6OPT_ROUTER_ALERT:
9bccf70c
A
1326 /* XXX may need check for alignment */
1327 if (hbhlen < IP6OPT_RTALERT_LEN) {
1328 ip6stat.ip6s_toosmall++;
1329 goto bad;
1330 }
1331 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1332 /* XXX stat */
1333 icmp6_error(m, ICMP6_PARAM_PROB,
1334 ICMP6_PARAMPROB_HEADER,
1335 erroff + opt + 1 - opthead);
1336 return(-1);
1337 }
1338 optlen = IP6OPT_RTALERT_LEN;
1339 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1340 *rtalertp = ntohs(rtalert_val);
1341 break;
1342 case IP6OPT_JUMBO:
1343 /* XXX may need check for alignment */
1344 if (hbhlen < IP6OPT_JUMBO_LEN) {
1345 ip6stat.ip6s_toosmall++;
1346 goto bad;
1347 }
1348 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1349 /* XXX stat */
1350 icmp6_error(m, ICMP6_PARAM_PROB,
1351 ICMP6_PARAMPROB_HEADER,
1352 erroff + opt + 1 - opthead);
1353 return(-1);
1354 }
1355 optlen = IP6OPT_JUMBO_LEN;
1356
1357 /*
1358 * IPv6 packets that have non 0 payload length
1359 * must not contain a jumbo payload option.
1360 */
1361 ip6 = mtod(m, struct ip6_hdr *);
1362 if (ip6->ip6_plen) {
1363 ip6stat.ip6s_badoptions++;
1364 icmp6_error(m, ICMP6_PARAM_PROB,
1365 ICMP6_PARAMPROB_HEADER,
1366 erroff + opt - opthead);
1367 return(-1);
1368 }
1369
1370 /*
1371 * We may see jumbolen in unaligned location, so
1372 * we'd need to perform bcopy().
1373 */
1374 bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1375 jumboplen = (u_int32_t)htonl(jumboplen);
1376
1377#if 1
1378 /*
1379 * if there are multiple jumbo payload options,
1380 * *plenp will be non-zero and the packet will be
1381 * rejected.
1382 * the behavior may need some debate in ipngwg -
1383 * multiple options does not make sense, however,
1384 * there's no explicit mention in specification.
1385 */
1386 if (*plenp != 0) {
1387 ip6stat.ip6s_badoptions++;
1388 icmp6_error(m, ICMP6_PARAM_PROB,
1389 ICMP6_PARAMPROB_HEADER,
1390 erroff + opt + 2 - opthead);
1391 return(-1);
1392 }
1c79356b 1393#endif
9bccf70c
A
1394
1395 /*
1396 * jumbo payload length must be larger than 65535.
1397 */
1398 if (jumboplen <= IPV6_MAXPACKET) {
1399 ip6stat.ip6s_badoptions++;
1400 icmp6_error(m, ICMP6_PARAM_PROB,
1401 ICMP6_PARAMPROB_HEADER,
1402 erroff + opt + 2 - opthead);
1403 return(-1);
1404 }
1405 *plenp = jumboplen;
1406
1407 break;
1408 default: /* unknown option */
1409 if (hbhlen < IP6OPT_MINLEN) {
1410 ip6stat.ip6s_toosmall++;
1411 goto bad;
1412 }
1413 optlen = ip6_unknown_opt(opt, m,
6d2010ae 1414 erroff + opt - opthead);
91447636 1415 if (optlen == -1) {
9bccf70c 1416 return(-1);
91447636 1417 }
9bccf70c
A
1418 optlen += 2;
1419 break;
1c79356b
A
1420 }
1421 }
1422
1423 return(0);
1424
91447636 1425 bad:
1c79356b
A
1426 m_freem(m);
1427 return(-1);
1428}
1429
1430/*
1431 * Unknown option processing.
1432 * The third argument `off' is the offset from the IPv6 header to the option,
1433 * which is necessary if the IPv6 header the and option header and IPv6 header
1434 * is not continuous in order to return an ICMPv6 error.
1435 */
1436int
6d2010ae 1437ip6_unknown_opt(uint8_t *optp, struct mbuf *m, int off)
1c79356b
A
1438{
1439 struct ip6_hdr *ip6;
1440
9bccf70c
A
1441 switch (IP6OPT_TYPE(*optp)) {
1442 case IP6OPT_TYPE_SKIP: /* ignore the option */
1443 return((int)*(optp + 1));
1444 case IP6OPT_TYPE_DISCARD: /* silently discard */
1445 m_freem(m);
1446 return(-1);
1447 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1448 ip6stat.ip6s_badoptions++;
1449 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1450 return(-1);
1451 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1452 ip6stat.ip6s_badoptions++;
1453 ip6 = mtod(m, struct ip6_hdr *);
1454 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1455 (m->m_flags & (M_BCAST|M_MCAST)))
1456 m_freem(m);
6d2010ae 1457 else
9bccf70c
A
1458 icmp6_error(m, ICMP6_PARAM_PROB,
1459 ICMP6_PARAMPROB_OPTION, off);
1460 return(-1);
1c79356b
A
1461 }
1462
1463 m_freem(m); /* XXX: NOTREACHED */
6d2010ae 1464 return (-1);
1c79356b
A
1465}
1466
1467/*
1468 * Create the "control" list for this pcb.
6d2010ae 1469 * These functions will not modify mbuf chain at all.
1c79356b 1470 *
6d2010ae 1471 * With KAME mbuf chain restriction:
1c79356b
A
1472 * The routine will be called from upper layer handlers like tcp6_input().
1473 * Thus the routine assumes that the caller (tcp6_input) have already
1474 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1475 * very first mbuf on the mbuf chain.
6d2010ae
A
1476 *
1477 * ip6_savecontrol_v4 will handle those options that are possible to be
1478 * set on a v4-mapped socket.
1479 * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1480 * options and handle the v6-only ones itself.
1c79356b 1481 */
6d2010ae
A
1482struct mbuf **
1483ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1484 int *v4only)
1c79356b 1485{
6d2010ae 1486 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1c79356b 1487
6d2010ae 1488 if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1c79356b
A
1489 struct timeval tv;
1490
1491 microtime(&tv);
6d2010ae
A
1492 mp = sbcreatecontrol_mbuf((caddr_t) &tv, sizeof(tv),
1493 SCM_TIMESTAMP, SOL_SOCKET, mp);
1494 if (*mp == NULL)
1495 return NULL;
1c79356b 1496 }
6d2010ae
A
1497 if ((inp->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
1498 uint64_t time;
1c79356b 1499
6d2010ae
A
1500 time = mach_absolute_time();
1501 mp = sbcreatecontrol_mbuf((caddr_t) &time, sizeof(time),
1502 SCM_TIMESTAMP_MONOTONIC, SOL_SOCKET, mp);
2d21ac55 1503
6d2010ae
A
1504 if (*mp == NULL)
1505 return NULL;
1506 }
1507 if ((inp->inp_socket->so_flags & SOF_RECV_TRAFFIC_CLASS) != 0) {
316670eb
A
1508 int tc = m_get_traffic_class(m);
1509
6d2010ae
A
1510 mp = sbcreatecontrol_mbuf((caddr_t) &tc, sizeof(tc),
1511 SO_TRAFFIC_CLASS, SOL_SOCKET, mp);
316670eb
A
1512 if (*mp == NULL)
1513 return (NULL);
6d2010ae
A
1514 }
1515
1516 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1517 if (v4only != NULL)
1518 *v4only = 1;
1519 return (mp);
1520 }
1521
1522#define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1c79356b 1523 /* RFC 2292 sec. 5 */
6d2010ae 1524 if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
9bccf70c 1525 struct in6_pktinfo pi6;
6d2010ae 1526
1c79356b 1527 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
6d2010ae
A
1528 in6_clearscope(&pi6.ipi6_addr); /* XXX */
1529 pi6.ipi6_ifindex =
1530 (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1531
1532 mp = sbcreatecontrol_mbuf((caddr_t) &pi6,
1533 sizeof(struct in6_pktinfo),
1534 IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6, mp);
1535 if (*mp == NULL)
1536 return NULL;
1c79356b
A
1537 }
1538
6d2010ae 1539 if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
9bccf70c 1540 int hlim = ip6->ip6_hlim & 0xff;
6d2010ae
A
1541
1542 mp = sbcreatecontrol_mbuf((caddr_t) &hlim, sizeof(int),
1543 IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1544 IPPROTO_IPV6, mp);
1545 if (*mp == NULL)
1546 return NULL;
1c79356b
A
1547 }
1548
6d2010ae
A
1549 if (v4only != NULL)
1550 *v4only = 0;
1551 return (mp);
1552}
1553
1554int
1555ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1556{
1557 struct mbuf **np;
1558 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1559 int v4only = 0;
1560
1561 *mp = NULL;
1562 np = ip6_savecontrol_v4(in6p, m, mp, &v4only);
1563 if (np == NULL)
1564 goto no_mbufs;
b0d623f7 1565
6d2010ae
A
1566 mp = np;
1567 if (v4only)
1568 return(0);
b0d623f7 1569
6d2010ae
A
1570 if ((in6p->inp_flags & IN6P_TCLASS) != 0) {
1571 u_int32_t flowinfo;
1572 int tclass;
1573
1574 flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1575 flowinfo >>= 20;
1576
1577 tclass = flowinfo & 0xff;
1578 mp = sbcreatecontrol_mbuf((caddr_t) &tclass, sizeof(tclass),
1579 IPV6_TCLASS, IPPROTO_IPV6, mp);
1580 if (*mp == NULL)
1581 goto no_mbufs;
1582 }
b0d623f7 1583
1c79356b 1584 /*
91447636
A
1585 * IPV6_HOPOPTS socket option. Recall that we required super-user
1586 * privilege for the option (see ip6_ctloutput), but it might be too
1587 * strict, since there might be some hop-by-hop options which can be
1588 * returned to normal user.
6d2010ae 1589 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1c79356b 1590 */
6d2010ae 1591 if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
1c79356b
A
1592 /*
1593 * Check if a hop-by-hop options header is contatined in the
1594 * received packet, and if so, store the options as ancillary
1595 * data. Note that a hop-by-hop options header must be
6d2010ae
A
1596 * just after the IPv6 header, which is assured through the
1597 * IPv6 input processing.
1c79356b 1598 */
91447636 1599 ip6 = mtod(m, struct ip6_hdr *);
1c79356b 1600 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
9bccf70c
A
1601 struct ip6_hbh *hbh;
1602 int hbhlen = 0;
1603#if PULLDOWN_TEST
1c79356b
A
1604 struct mbuf *ext;
1605#endif
1606
1607#ifndef PULLDOWN_TEST
1608 hbh = (struct ip6_hbh *)(ip6 + 1);
1609 hbhlen = (hbh->ip6h_len + 1) << 3;
1610#else
1611 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1612 ip6->ip6_nxt);
1613 if (ext == NULL) {
1614 ip6stat.ip6s_tooshort++;
6d2010ae 1615 return(0);
1c79356b
A
1616 }
1617 hbh = mtod(ext, struct ip6_hbh *);
1618 hbhlen = (hbh->ip6h_len + 1) << 3;
1619 if (hbhlen != ext->m_len) {
1620 m_freem(ext);
1621 ip6stat.ip6s_tooshort++;
6d2010ae 1622 return(0);
1c79356b
A
1623 }
1624#endif
1625
1c79356b 1626 /*
6d2010ae
A
1627 * XXX: We copy the whole header even if a
1628 * jumbo payload option is included, the option which
1629 * is to be removed before returning according to
1630 * RFC2292.
1631 * Note: this constraint is removed in RFC3542
1c79356b 1632 */
6d2010ae
A
1633 mp = sbcreatecontrol_mbuf((caddr_t)hbh, hbhlen,
1634 IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1635 IPPROTO_IPV6, mp);
1636
9bccf70c 1637#if PULLDOWN_TEST
1c79356b
A
1638 m_freem(ext);
1639#endif
6d2010ae
A
1640 if (*mp == NULL) {
1641 goto no_mbufs;
9bccf70c 1642 }
9bccf70c
A
1643 }
1644 }
1645
6d2010ae 1646 if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1c79356b 1647 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1c79356b
A
1648
1649 /*
1650 * Search for destination options headers or routing
1651 * header(s) through the header chain, and stores each
1652 * header as ancillary data.
1653 * Note that the order of the headers remains in
1654 * the chain of ancillary data.
1655 */
1656 while (1) { /* is explicit loop prevention necessary? */
1657 struct ip6_ext *ip6e = NULL;
1658 int elen;
9bccf70c 1659#if PULLDOWN_TEST
1c79356b
A
1660 struct mbuf *ext = NULL;
1661#endif
1662
1663 /*
1664 * if it is not an extension header, don't try to
1665 * pull it from the chain.
1666 */
1667 switch (nxt) {
1668 case IPPROTO_DSTOPTS:
1669 case IPPROTO_ROUTING:
1670 case IPPROTO_HOPOPTS:
1671 case IPPROTO_AH: /* is it possible? */
1672 break;
1673 default:
1674 goto loopend;
1675 }
1676
1677#ifndef PULLDOWN_TEST
1678 if (off + sizeof(*ip6e) > m->m_len)
1679 goto loopend;
1680 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1681 if (nxt == IPPROTO_AH)
1682 elen = (ip6e->ip6e_len + 2) << 2;
1683 else
1684 elen = (ip6e->ip6e_len + 1) << 3;
1685 if (off + elen > m->m_len)
1686 goto loopend;
1687#else
1688 ext = ip6_pullexthdr(m, off, nxt);
1689 if (ext == NULL) {
1690 ip6stat.ip6s_tooshort++;
6d2010ae 1691 return(0);
1c79356b
A
1692 }
1693 ip6e = mtod(ext, struct ip6_ext *);
1694 if (nxt == IPPROTO_AH)
1695 elen = (ip6e->ip6e_len + 2) << 2;
1696 else
1697 elen = (ip6e->ip6e_len + 1) << 3;
1698 if (elen != ext->m_len) {
1699 m_freem(ext);
1700 ip6stat.ip6s_tooshort++;
6d2010ae 1701 return(0);
1c79356b
A
1702 }
1703#endif
1704
1705 switch (nxt) {
1706 case IPPROTO_DSTOPTS:
6d2010ae 1707 if (!(in6p->inp_flags & IN6P_DSTOPTS))
1c79356b
A
1708 break;
1709
6d2010ae
A
1710 mp = sbcreatecontrol_mbuf((caddr_t)ip6e, elen,
1711 IS2292(in6p,
1712 IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1713 IPPROTO_IPV6, mp);
1714 if (*mp == NULL) {
1715#if PULLDOWN_TEST
1716 m_freem(ext);
1717#endif
1718 goto no_mbufs;
1719 }
1c79356b 1720 break;
1c79356b 1721 case IPPROTO_ROUTING:
6d2010ae 1722 if (!in6p->inp_flags & IN6P_RTHDR)
1c79356b
A
1723 break;
1724
6d2010ae
A
1725 mp = sbcreatecontrol_mbuf((caddr_t)ip6e, elen,
1726 IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
1727 IPPROTO_IPV6, mp);
1728 if (*mp == NULL) {
1729#if PULLDOWN_TEST
1730 m_freem(ext);
1731#endif
1732 goto no_mbufs;
1733 }
1c79356b 1734 break;
1c79356b
A
1735 case IPPROTO_HOPOPTS:
1736 case IPPROTO_AH: /* is it possible? */
1737 break;
1738
1739 default:
1740 /*
6d2010ae 1741 * other cases have been filtered in the above.
1c79356b
A
1742 * none will visit this case. here we supply
1743 * the code just in case (nxt overwritten or
1744 * other cases).
1745 */
9bccf70c 1746#if PULLDOWN_TEST
1c79356b
A
1747 m_freem(ext);
1748#endif
1749 goto loopend;
1750
1751 }
1752
1753 /* proceed with the next header. */
1754 off += elen;
1755 nxt = ip6e->ip6e_nxt;
1756 ip6e = NULL;
9bccf70c 1757#if PULLDOWN_TEST
1c79356b
A
1758 m_freem(ext);
1759 ext = NULL;
1760#endif
1761 }
1762 loopend:
9bccf70c 1763 ;
1c79356b 1764 }
6d2010ae
A
1765 return(0);
1766no_mbufs:
1767 ip6stat.ip6s_pktdropcntrl++;
1768 /* XXX increment a stat to show the failure */
1769 return(ENOBUFS);
1770}
1771#undef IS2292
1772
1773void
1774ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu)
1775{
1776 struct socket *so;
1777 struct mbuf *m_mtu;
1778 struct ip6_mtuinfo mtuctl;
1779
1780 so = in6p->inp_socket;
1c79356b 1781
6d2010ae
A
1782 if (mtu == NULL)
1783 return;
1784
1785#ifdef DIAGNOSTIC
1786 if (so == NULL) /* I believe this is impossible */
1787 panic("ip6_notify_pmtu: socket is NULL");
1788#endif
1789
1790 bzero(&mtuctl, sizeof(mtuctl)); /* zero-clear for safety */
1791 mtuctl.ip6m_mtu = *mtu;
1792 mtuctl.ip6m_addr = *dst;
316670eb 1793 if (sa6_recoverscope(&mtuctl.ip6m_addr, TRUE))
6d2010ae
A
1794 return;
1795
1796 if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1797 IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1798 return;
1799
1800 if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu, NULL)
1801 == 0) {
1802 m_freem(m_mtu);
1803 /* XXX: should count statistics */
1804 } else
1805 sorwakeup(so);
1806
1807 return;
1c79356b
A
1808}
1809
9bccf70c 1810#if PULLDOWN_TEST
1c79356b
A
1811/*
1812 * pull single extension header from mbuf chain. returns single mbuf that
1813 * contains the result, or NULL on error.
1814 */
1815static struct mbuf *
1816ip6_pullexthdr(m, off, nxt)
1817 struct mbuf *m;
1818 size_t off;
1819 int nxt;
1820{
1821 struct ip6_ext ip6e;
1822 size_t elen;
1823 struct mbuf *n;
1824
9bccf70c 1825#if DIAGNOSTIC
1c79356b
A
1826 switch (nxt) {
1827 case IPPROTO_DSTOPTS:
1828 case IPPROTO_ROUTING:
1829 case IPPROTO_HOPOPTS:
1830 case IPPROTO_AH: /* is it possible? */
1831 break;
1832 default:
1833 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1834 }
1835#endif
1836
1837 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1838 if (nxt == IPPROTO_AH)
1839 elen = (ip6e.ip6e_len + 2) << 2;
1840 else
1841 elen = (ip6e.ip6e_len + 1) << 3;
1842
1843 MGET(n, M_DONTWAIT, MT_DATA);
1844 if (n && elen >= MLEN) {
1845 MCLGET(n, M_DONTWAIT);
1846 if ((n->m_flags & M_EXT) == 0) {
1847 m_free(n);
1848 n = NULL;
1849 }
1850 }
1851 if (!n)
1852 return NULL;
1853
1854 n->m_len = 0;
1855 if (elen >= M_TRAILINGSPACE(n)) {
1856 m_free(n);
1857 return NULL;
1858 }
1859
1860 m_copydata(m, off, elen, mtod(n, caddr_t));
1861 n->m_len = elen;
1862 return n;
1863}
1864#endif
1865
1c79356b
A
1866/*
1867 * Get pointer to the previous header followed by the header
1868 * currently processed.
1869 * XXX: This function supposes that
1870 * M includes all headers,
1871 * the next header field and the header length field of each header
1872 * are valid, and
1873 * the sum of each header length equals to OFF.
1874 * Because of these assumptions, this function must be called very
1875 * carefully. Moreover, it will not be used in the near future when
1876 * we develop `neater' mechanism to process extension headers.
1877 */
1878char *
1879ip6_get_prevhdr(m, off)
1880 struct mbuf *m;
1881 int off;
1882{
1883 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1884
1885 if (off == sizeof(struct ip6_hdr))
2d21ac55 1886 return((char *) &ip6->ip6_nxt);
1c79356b
A
1887 else {
1888 int len, nxt;
1889 struct ip6_ext *ip6e = NULL;
1890
1891 nxt = ip6->ip6_nxt;
1892 len = sizeof(struct ip6_hdr);
1893 while (len < off) {
1894 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1895
9bccf70c 1896 switch (nxt) {
1c79356b
A
1897 case IPPROTO_FRAGMENT:
1898 len += sizeof(struct ip6_frag);
1899 break;
1900 case IPPROTO_AH:
1901 len += (ip6e->ip6e_len + 2) << 2;
1902 break;
1903 default:
1904 len += (ip6e->ip6e_len + 1) << 3;
1905 break;
1906 }
1907 nxt = ip6e->ip6e_nxt;
1908 }
1909 if (ip6e)
2d21ac55 1910 return((char *) &ip6e->ip6e_nxt);
1c79356b
A
1911 else
1912 return NULL;
1913 }
1914}
1915
1916/*
1917 * get next header offset. m will be retained.
1918 */
1919int
1920ip6_nexthdr(m, off, proto, nxtp)
1921 struct mbuf *m;
1922 int off;
1923 int proto;
1924 int *nxtp;
1925{
1926 struct ip6_hdr ip6;
1927 struct ip6_ext ip6e;
1928 struct ip6_frag fh;
1929
1930 /* just in case */
1931 if (m == NULL)
1932 panic("ip6_nexthdr: m == NULL");
1933 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1934 return -1;
1935
1936 switch (proto) {
1937 case IPPROTO_IPV6:
1938 if (m->m_pkthdr.len < off + sizeof(ip6))
1939 return -1;
1940 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1941 if (nxtp)
1942 *nxtp = ip6.ip6_nxt;
1943 off += sizeof(ip6);
1944 return off;
1945
1946 case IPPROTO_FRAGMENT:
1947 /*
1948 * terminate parsing if it is not the first fragment,
1949 * it does not make sense to parse through it.
1950 */
1951 if (m->m_pkthdr.len < off + sizeof(fh))
1952 return -1;
1953 m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
91447636
A
1954 /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1955 if (fh.ip6f_offlg & IP6F_OFF_MASK)
1c79356b
A
1956 return -1;
1957 if (nxtp)
1958 *nxtp = fh.ip6f_nxt;
1959 off += sizeof(struct ip6_frag);
1960 return off;
1961
1962 case IPPROTO_AH:
1963 if (m->m_pkthdr.len < off + sizeof(ip6e))
1964 return -1;
1965 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1966 if (nxtp)
1967 *nxtp = ip6e.ip6e_nxt;
1968 off += (ip6e.ip6e_len + 2) << 2;
1969 return off;
1970
1971 case IPPROTO_HOPOPTS:
1972 case IPPROTO_ROUTING:
1973 case IPPROTO_DSTOPTS:
1974 if (m->m_pkthdr.len < off + sizeof(ip6e))
1975 return -1;
1976 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1977 if (nxtp)
1978 *nxtp = ip6e.ip6e_nxt;
1979 off += (ip6e.ip6e_len + 1) << 3;
1980 return off;
1981
1982 case IPPROTO_NONE:
1983 case IPPROTO_ESP:
1984 case IPPROTO_IPCOMP:
1985 /* give up */
1986 return -1;
1987
1988 default:
1989 return -1;
1990 }
1991
1992 return -1;
1993}
1994
1995/*
1996 * get offset for the last header in the chain. m will be kept untainted.
1997 */
1998int
1999ip6_lasthdr(m, off, proto, nxtp)
2000 struct mbuf *m;
2001 int off;
2002 int proto;
2003 int *nxtp;
2004{
2005 int newoff;
2006 int nxt;
2007
2008 if (!nxtp) {
2009 nxt = -1;
2010 nxtp = &nxt;
2011 }
2012 while (1) {
2013 newoff = ip6_nexthdr(m, off, proto, nxtp);
2014 if (newoff < 0)
2015 return off;
2016 else if (newoff < off)
2017 return -1; /* invalid */
2018 else if (newoff == off)
2019 return newoff;
2020
2021 off = newoff;
2022 proto = *nxtp;
2023 }
2024}
2025
2d21ac55 2026struct ip6aux *
316670eb 2027ip6_addaux(struct mbuf *m)
9bccf70c 2028{
2d21ac55 2029 struct m_tag *tag;
316670eb 2030
2d21ac55 2031 /* Check if one is already allocated */
316670eb
A
2032 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
2033 KERNEL_TAG_TYPE_INET6, NULL);
2d21ac55
A
2034 if (tag == NULL) {
2035 /* Allocate a tag */
6d2010ae
A
2036 tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_INET6,
2037 sizeof (struct ip6aux), M_DONTWAIT, m);
b0d623f7 2038
2d21ac55
A
2039 /* Attach it to the mbuf */
2040 if (tag) {
2041 m_tag_prepend(m, tag);
55e303ae 2042 }
9bccf70c 2043 }
316670eb
A
2044
2045 return (tag ? (struct ip6aux *)(tag + 1) : NULL);
9bccf70c
A
2046}
2047
2d21ac55 2048struct ip6aux *
316670eb 2049ip6_findaux(struct mbuf *m)
9bccf70c 2050{
2d21ac55 2051 struct m_tag *tag;
316670eb
A
2052
2053 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
2054 KERNEL_TAG_TYPE_INET6, NULL);
2055
2056 return (tag ? (struct ip6aux *)(tag + 1) : NULL);
9bccf70c
A
2057}
2058
2059void
316670eb 2060ip6_delaux(struct mbuf *m)
9bccf70c 2061{
2d21ac55 2062 struct m_tag *tag;
9bccf70c 2063
316670eb
A
2064 tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
2065 KERNEL_TAG_TYPE_INET6, NULL);
2d21ac55
A
2066 if (tag) {
2067 m_tag_delete(m, tag);
2068 }
9bccf70c
A
2069}
2070
6d2010ae
A
2071/*
2072 * Called by m_tag_free().
2073 */
2074void
2075ip6_destroyaux(struct ip6aux *n)
2076{
2077 if (n->ip6a_dstia6 != NULL) {
2078 IFA_REMREF(&n->ip6a_dstia6->ia_ifa);
2079 n->ip6a_dstia6 = NULL;
2080 }
2081}
2082
2083/*
2084 * Called by m_tag_copy()
2085 */
2086void
2087ip6_copyaux(struct ip6aux *src, struct ip6aux *dst)
2088{
2089 bcopy(src, dst, sizeof (*dst));
2090 if (dst->ip6a_dstia6 != NULL)
2091 IFA_ADDREF(&dst->ip6a_dstia6->ia_ifa);
2092}
2093
1c79356b
A
2094/*
2095 * System control for IP6
2096 */
2097
2098u_char inet6ctlerrmap[PRC_NCMDS] = {
2099 0, 0, 0, 0,
2100 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
2101 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
2102 EMSGSIZE, EHOSTUNREACH, 0, 0,
2103 0, 0, 0, 0,
2104 ENOPROTOOPT
2105};