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