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