]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
39037602 | 2 | * Copyright (c) 2003-2016 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 | |
316670eb A |
149 | #if DUMMYNET |
150 | #include <netinet/ip_fw.h> | |
151 | #include <netinet/ip_dummynet.h> | |
152 | #endif /* DUMMYNET */ | |
153 | ||
1c79356b | 154 | /* we need it for NLOOP. */ |
1c79356b | 155 | #include "loop.h" |
1c79356b | 156 | |
b0d623f7 A |
157 | #if PF |
158 | #include <net/pfvar.h> | |
159 | #endif /* PF */ | |
160 | ||
39236c6e | 161 | struct ip6protosw *ip6_protox[IPPROTO_MAX]; |
6d2010ae A |
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; | |
9bccf70c | 170 | |
39236c6e | 171 | #define IN6_IFSTAT_REQUIRE_ALIGNED_64(f) \ |
6d2010ae | 172 | _CASSERT(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t))) |
316670eb | 173 | |
39236c6e | 174 | #define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f) \ |
6d2010ae | 175 | _CASSERT(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t))) |
9bccf70c | 176 | |
1c79356b A |
177 | struct ip6stat ip6stat; |
178 | ||
316670eb | 179 | decl_lck_mtx_data(, proxy6_lock); |
39236c6e A |
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); | |
316670eb A |
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; | |
6d2010ae | 186 | #ifdef ENABLE_ADDRSEL |
39236c6e | 187 | decl_lck_mtx_data(static, addrsel_mutex_data); |
316670eb | 188 | lck_mtx_t *addrsel_mutex = &addrsel_mutex_data; |
6d2010ae | 189 | #endif |
39236c6e A |
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 | ||
91447636 | 194 | extern int loopattach_done; |
6d2010ae | 195 | extern void addrsel_policy_init(void); |
9bccf70c | 196 | |
3e170ce0 A |
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; | |
316670eb | 200 | static void ip6_init_delayed(void); |
91447636 | 201 | static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *); |
1c79356b | 202 | |
39236c6e A |
203 | #if NSTF |
204 | extern void stfattach(void); | |
205 | #endif /* NSTF */ | |
1c79356b | 206 | |
6d2010ae A |
207 | SYSCTL_DECL(_net_inet6_ip6); |
208 | ||
39236c6e A |
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"); | |
316670eb | 213 | |
3e170ce0 A |
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 | ||
316670eb A |
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) && \ | |
39236c6e A |
251 | (_m)->m_pkthdr.pkt_hdr != NULL) \ |
252 | (_m)->m_pkthdr.pkt_hdr = NULL; \ | |
316670eb A |
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; \ | |
39236c6e | 258 | _action; \ |
316670eb A |
259 | } else { \ |
260 | VERIFY(_n != (_m)); \ | |
261 | (_m) = _n; \ | |
262 | } \ | |
263 | } \ | |
264 | } while (0) | |
265 | #endif /* !__i386__ && !__x86_64__ */ | |
266 | ||
91447636 | 267 | static void |
39236c6e | 268 | ip6_proto_input(protocol_family_t protocol, mbuf_t packet) |
91447636 | 269 | { |
39236c6e | 270 | #pragma unused(protocol) |
3e170ce0 A |
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 */ | |
91447636 | 276 | ip6_input(packet); |
3e170ce0 A |
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 */ | |
91447636 A |
283 | } |
284 | ||
1c79356b A |
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 | |
39236c6e | 290 | ip6_init(struct ip6protosw *pp, struct domain *dp) |
1c79356b | 291 | { |
39236c6e A |
292 | static int ip6_initialized = 0; |
293 | struct protosw *pr; | |
1c79356b | 294 | struct timeval tv; |
39236c6e A |
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); | |
1c79356b | 303 | |
39236c6e A |
304 | if (ip6_initialized) |
305 | return; | |
306 | ip6_initialized = 1; | |
99c3a104 | 307 | |
39236c6e A |
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. */ | |
1c79356b | 316 | for (i = 0; i < IPPROTO_MAX; i++) |
39236c6e A |
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; | |
1c79356b A |
330 | } |
331 | } | |
332 | ||
91447636 | 333 | ip6_mutex_grp_attr = lck_grp_attr_alloc_init(); |
91447636 A |
334 | |
335 | ip6_mutex_grp = lck_grp_alloc_init("ip6", ip6_mutex_grp_attr); | |
336 | ip6_mutex_attr = lck_attr_alloc_init(); | |
91447636 | 337 | |
316670eb A |
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); | |
39236c6e | 341 | scope6_init(ip6_mutex_grp, ip6_mutex_attr); |
91447636 | 342 | |
6d2010ae | 343 | #ifdef ENABLE_ADDRSEL |
316670eb | 344 | lck_mtx_init(addrsel_mutex, ip6_mutex_grp, ip6_mutex_attr); |
6d2010ae A |
345 | #endif |
346 | ||
316670eb | 347 | lck_mtx_init(&proxy6_lock, ip6_mutex_grp, ip6_mutex_attr); |
6d2010ae A |
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); | |
39236c6e | 375 | IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast); |
6d2010ae A |
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 | ||
39236c6e A |
413 | getmicrotime(&tv); |
414 | ip6_desync_factor = | |
415 | (RandomULong() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR; | |
416 | ||
b0d623f7 | 417 | in6_ifaddr_init(); |
6d2010ae | 418 | ip6_moptions_init(); |
1c79356b A |
419 | nd6_init(); |
420 | frag6_init(); | |
39236c6e | 421 | icmp6_init(NULL, dp); |
6d2010ae | 422 | addrsel_policy_init(); |
91447636 | 423 | |
316670eb A |
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 | ||
39236c6e A |
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); | |
1c79356b A |
439 | } |
440 | ||
441 | static void | |
316670eb | 442 | ip6_init_delayed(void) |
1c79356b | 443 | { |
39236c6e | 444 | (void) in6_ifattach_prelim(lo_ifp); |
1c79356b | 445 | |
9bccf70c | 446 | /* timer for regeneranation of temporary addresses randomize ID */ |
39236c6e A |
447 | timeout(in6_tmpaddrtimer, NULL, |
448 | (ip6_temp_preferred_lifetime - ip6_desync_factor - | |
449 | ip6_temp_regen_advance) * hz); | |
9bccf70c | 450 | |
9bccf70c A |
451 | #if NSTF |
452 | stfattach(); | |
39236c6e | 453 | #endif /* NSTF */ |
6d2010ae | 454 | } |
1c79356b A |
455 | |
456 | void | |
6d2010ae | 457 | ip6_input(struct mbuf *m) |
1c79356b A |
458 | { |
459 | struct ip6_hdr *ip6; | |
39236c6e | 460 | int off = sizeof (struct ip6_hdr), nest; |
1c79356b A |
461 | u_int32_t plen; |
462 | u_int32_t rtalert = ~0; | |
463 | int nxt = 0, ours = 0; | |
39236c6e A |
464 | struct ifnet *inifp, *deliverifp = NULL; |
465 | ipfilter_t inject_ipfref = NULL; | |
2dced7af | 466 | int seen = 1; |
6d2010ae | 467 | struct in6_ifaddr *ia6 = NULL; |
6d2010ae | 468 | struct sockaddr_in6 *dst6; |
316670eb | 469 | #if DUMMYNET |
39236c6e | 470 | struct m_tag *tag; |
316670eb | 471 | #endif /* DUMMYNET */ |
39236c6e A |
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 | |
6d2010ae | 480 | |
39236c6e A |
481 | /* zero out {rin6, args} */ |
482 | bzero(&ip6ibz, sizeof (ip6ibz)); | |
6d2010ae | 483 | |
39236c6e A |
484 | /* |
485 | * Check if the packet we received is valid after interface filter | |
6d2010ae A |
486 | * processing |
487 | */ | |
488 | MBUF_INPUT_CHECK(m, m->m_pkthdr.rcvif); | |
39236c6e A |
489 | inifp = m->m_pkthdr.rcvif; |
490 | VERIFY(inifp != NULL); | |
1c79356b | 491 | |
316670eb | 492 | /* Perform IP header alignment fixup, if needed */ |
39236c6e A |
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); | |
316670eb A |
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); | |
39236c6e | 516 | |
316670eb | 517 | args.fwa_pf_rule = dn_tag->dn_pf_rule; |
39236c6e | 518 | |
316670eb A |
519 | m_tag_delete(m, tag); |
520 | } | |
39236c6e | 521 | |
316670eb A |
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 | ||
91447636 | 529 | /* |
39236c6e | 530 | * No need to proccess packet twice if we've already seen it. |
91447636 A |
531 | */ |
532 | inject_ipfref = ipf_get_inject_filter(m); | |
39236c6e | 533 | if (inject_ipfref != NULL) { |
91447636 A |
534 | ip6 = mtod(m, struct ip6_hdr *); |
535 | nxt = ip6->ip6_nxt; | |
536 | seen = 0; | |
537 | goto injectit; | |
39236c6e | 538 | } else { |
91447636 | 539 | seen = 1; |
1c79356b | 540 | } |
9bccf70c | 541 | |
1c79356b | 542 | /* |
55e303ae | 543 | * mbuf statistics |
1c79356b A |
544 | */ |
545 | if (m->m_flags & M_EXT) { | |
39236c6e | 546 | if (m->m_next != NULL) |
1c79356b A |
547 | ip6stat.ip6s_mext2m++; |
548 | else | |
549 | ip6stat.ip6s_mext1++; | |
550 | } else { | |
39236c6e A |
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 { | |
1c79356b | 559 | ip6stat.ip6s_m2m[0]++; |
39236c6e A |
560 | } |
561 | } else { | |
1c79356b | 562 | ip6stat.ip6s_m1++; |
39236c6e | 563 | } |
9bccf70c | 564 | #undef M2MMAX |
1c79356b A |
565 | } |
566 | ||
316670eb | 567 | /* |
39236c6e | 568 | * Drop the packet if IPv6 operation is disabled on the interface. |
316670eb | 569 | */ |
39236c6e | 570 | if (inifp->if_eflags & IFEF_IPV6_DISABLED) |
6d2010ae | 571 | goto bad; |
6d2010ae | 572 | |
39236c6e | 573 | in6_ifstat_inc_na(inifp, ifs6_in_receive); |
1c79356b A |
574 | ip6stat.ip6s_total++; |
575 | ||
9bccf70c A |
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 | */ | |
39236c6e | 581 | if (m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { |
9bccf70c A |
582 | struct mbuf *n; |
583 | ||
2d21ac55 | 584 | MGETHDR(n, M_DONTWAIT, MT_HEADER); /* MAC-OK */ |
9bccf70c A |
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 | } | |
6d2010ae A |
594 | if (n == NULL) |
595 | goto bad; | |
9bccf70c A |
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 | } | |
39236c6e | 602 | IP6_EXTHDR_CHECK(m, 0, sizeof (struct ip6_hdr), { goto done; }); |
1c79356b | 603 | |
39236c6e A |
604 | if (m->m_len < sizeof (struct ip6_hdr)) { |
605 | if ((m = m_pullup(m, sizeof (struct ip6_hdr))) == 0) { | |
1c79356b A |
606 | ip6stat.ip6s_toosmall++; |
607 | in6_ifstat_inc(inifp, ifs6_in_hdrerr); | |
6d2010ae | 608 | goto done; |
1c79356b A |
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++; | |
39236c6e | 616 | in6_ifstat_inc(inifp, ifs6_in_hdrerr); |
1c79356b A |
617 | goto bad; |
618 | } | |
619 | ||
620 | ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; | |
3e170ce0 | 621 | |
1c79356b | 622 | /* |
9bccf70c | 623 | * Check against address spoofing/corruption. |
1c79356b | 624 | */ |
04b8595b A |
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 | } | |
1c79356b A |
631 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || |
632 | IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { | |
9bccf70c A |
633 | /* |
634 | * XXX: "badscope" is not very suitable for a multicast source. | |
635 | */ | |
636 | ip6stat.ip6s_badscope++; | |
39236c6e | 637 | in6_ifstat_inc(inifp, ifs6_in_addrerr); |
9bccf70c A |
638 | goto bad; |
639 | } | |
6d2010ae | 640 | if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) && |
39236c6e | 641 | !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) { |
6d2010ae A |
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 | */ | |
1c79356b | 648 | ip6stat.ip6s_badscope++; |
39236c6e | 649 | in6_ifstat_inc(inifp, ifs6_in_addrerr); |
1c79356b A |
650 | goto bad; |
651 | } | |
55e303ae | 652 | |
1c79356b | 653 | /* |
9bccf70c A |
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 | |
6d2010ae | 657 | * IPv6 src ::ffff:127.0.0.1). Be cautious. |
9bccf70c A |
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 | |
6d2010ae | 663 | * partial support for SIIT environment. |
1c79356b | 664 | */ |
9bccf70c A |
665 | if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || |
666 | IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { | |
667 | ip6stat.ip6s_badscope++; | |
39236c6e | 668 | in6_ifstat_inc(inifp, ifs6_in_addrerr); |
9bccf70c A |
669 | goto bad; |
670 | } | |
1c79356b A |
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++; | |
39236c6e | 682 | in6_ifstat_inc(inifp, ifs6_in_addrerr); |
1c79356b A |
683 | goto bad; |
684 | } | |
685 | #endif | |
9bccf70c | 686 | |
6d2010ae A |
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 = | |
39236c6e | 700 | ifnet_cached_rtlookup_inet6(inifp, &ip6->ip6_src); |
6d2010ae A |
701 | if (rte != NULL) { |
702 | nstat_route_rx(rte, 1, m->m_pkthdr.len, 0); | |
703 | rtfree(rte); | |
704 | } | |
705 | } | |
706 | ||
39236c6e A |
707 | /* for consistency */ |
708 | m->m_pkthdr.pkt_proto = ip6->ip6_nxt; | |
709 | ||
316670eb A |
710 | #if DUMMYNET |
711 | check_with_pf: | |
39236c6e | 712 | #endif /* DUMMYNET */ |
b0d623f7 A |
713 | #if PF |
714 | /* Invoke inbound packet filter */ | |
6d2010ae A |
715 | if (PF_IS_ENABLED) { |
716 | int error; | |
316670eb | 717 | #if DUMMYNET |
39236c6e A |
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 */ | |
316670eb | 722 | if (error != 0 || m == NULL) { |
6d2010ae | 723 | if (m != NULL) { |
39236c6e A |
724 | panic("%s: unexpected packet %p\n", |
725 | __func__, m); | |
6d2010ae A |
726 | /* NOTREACHED */ |
727 | } | |
728 | /* Already freed by callee */ | |
729 | goto done; | |
b0d623f7 | 730 | } |
6d2010ae | 731 | ip6 = mtod(m, struct ip6_hdr *); |
b0d623f7 | 732 | } |
b0d623f7 A |
733 | #endif /* PF */ |
734 | ||
9bccf70c | 735 | /* drop packets if interface ID portion is already filled */ |
39236c6e A |
736 | if (!(inifp->if_flags & IFF_LOOPBACK) && |
737 | !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) { | |
9bccf70c A |
738 | if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && |
739 | ip6->ip6_src.s6_addr16[1]) { | |
740 | ip6stat.ip6s_badscope++; | |
741 | goto bad; | |
742 | } | |
fe8ab488 | 743 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) && |
9bccf70c | 744 | ip6->ip6_dst.s6_addr16[1]) { |
1c79356b | 745 | ip6stat.ip6s_badscope++; |
1c79356b A |
746 | goto bad; |
747 | } | |
748 | } | |
749 | ||
39236c6e A |
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); | |
fe8ab488 | 754 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) |
39236c6e A |
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); | |
fe8ab488 | 760 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) |
39236c6e A |
761 | ip6->ip6_dst.s6_addr16[1] = htons(inifp->if_index); |
762 | } | |
9bccf70c | 763 | |
1c79356b A |
764 | /* |
765 | * Multicast check | |
766 | */ | |
767 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { | |
6d2010ae | 768 | struct in6_multi *in6m = NULL; |
1c79356b | 769 | |
39236c6e | 770 | in6_ifstat_inc_na(inifp, ifs6_in_mcast); |
1c79356b A |
771 | /* |
772 | * See if we belong to the destination multicast group on the | |
773 | * arrival interface. | |
774 | */ | |
6d2010ae | 775 | in6_multihead_lock_shared(); |
39236c6e | 776 | IN6_LOOKUP_MULTI(&ip6->ip6_dst, inifp, in6m); |
6d2010ae A |
777 | in6_multihead_lock_done(); |
778 | if (in6m != NULL) { | |
779 | IN6M_REMREF(in6m); | |
1c79356b | 780 | ours = 1; |
fe8ab488 | 781 | } else if (!nd6_prproxy) { |
1c79356b A |
782 | ip6stat.ip6s_notmember++; |
783 | ip6stat.ip6s_cantforward++; | |
39236c6e | 784 | in6_ifstat_inc(inifp, ifs6_in_discard); |
1c79356b A |
785 | goto bad; |
786 | } | |
39236c6e A |
787 | deliverifp = inifp; |
788 | VERIFY(ia6 == NULL); | |
1c79356b A |
789 | goto hbhcheck; |
790 | } | |
791 | ||
792 | /* | |
39236c6e A |
793 | * Unicast check |
794 | * | |
795 | * Fast path: see if the target is ourselves. | |
1c79356b | 796 | */ |
39236c6e A |
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); | |
6d2010ae A |
839 | dst6->sin6_family = AF_INET6; |
840 | dst6->sin6_addr = ip6->ip6_dst; | |
1c79356b | 841 | |
39236c6e | 842 | rtalloc_scoped_ign((struct route *)&rin6, |
6d2010ae | 843 | RTF_PRCLONING, IFSCOPE_NONE); |
39236c6e A |
844 | if (rin6.ro_rt != NULL) |
845 | RT_LOCK_SPIN(rin6.ro_rt); | |
1c79356b | 846 | |
39236c6e | 847 | #define rt6_key(r) (SIN6((r)->rt_nodes->rn_key)) |
1c79356b A |
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. | |
9bccf70c A |
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? | |
1c79356b | 868 | */ |
39236c6e A |
869 | if (rin6.ro_rt != NULL && |
870 | (rin6.ro_rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && | |
9bccf70c | 871 | #if RTF_WASCLONED |
39236c6e | 872 | !(rin6.ro_rt->rt_flags & RTF_WASCLONED) && |
1c79356b | 873 | #endif |
39236c6e A |
874 | rin6.ro_rt->rt_ifp->if_type == IFT_LOOP) { |
875 | ia6 = (struct in6_ifaddr *)rin6.ro_rt->rt_ifa; | |
9bccf70c | 876 | /* |
39236c6e | 877 | * Packets to a tentative, duplicated, or somehow invalid |
9bccf70c | 878 | * address must not be accepted. |
39236c6e A |
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.) | |
9bccf70c | 883 | */ |
1c79356b | 884 | if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { |
9bccf70c | 885 | /* this address is ready */ |
1c79356b A |
886 | ours = 1; |
887 | deliverifp = ia6->ia_ifp; /* correct? */ | |
39236c6e A |
888 | /* |
889 | * record dst address information into mbuf. | |
890 | */ | |
891 | (void) ip6_setdstifaddr_info(m, 0, ia6); | |
892 | RT_UNLOCK(rin6.ro_rt); | |
1c79356b | 893 | goto hbhcheck; |
1c79356b | 894 | } |
39236c6e A |
895 | RT_UNLOCK(rin6.ro_rt); |
896 | ia6 = NULL; | |
6d2010ae | 897 | /* address is not ready, so discard the packet. */ |
39236c6e A |
898 | nd6log((LOG_INFO, "%s: packet to an unready address %s->%s\n", |
899 | __func__, ip6_sprintf(&ip6->ip6_src), | |
6d2010ae | 900 | ip6_sprintf(&ip6->ip6_dst))); |
6d2010ae | 901 | goto bad; |
1c79356b A |
902 | } |
903 | ||
39236c6e A |
904 | if (rin6.ro_rt != NULL) |
905 | RT_UNLOCK(rin6.ro_rt); | |
1c79356b | 906 | |
1c79356b A |
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++; | |
39236c6e | 913 | in6_ifstat_inc(inifp, ifs6_in_discard); |
1c79356b A |
914 | goto bad; |
915 | } | |
916 | ||
39236c6e | 917 | hbhcheck: |
9bccf70c | 918 | /* |
39236c6e | 919 | * record dst address information into mbuf, if we don't have one yet. |
9bccf70c | 920 | * note that we are unable to record it, if the address is not listed |
316670eb | 921 | * as our interface address (e.g. multicast addresses, etc.) |
9bccf70c | 922 | */ |
39236c6e | 923 | if (deliverifp != NULL && ia6 == NULL) { |
9bccf70c | 924 | ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); |
39236c6e A |
925 | if (ia6 != NULL) { |
926 | (void) ip6_setdstifaddr_info(m, 0, ia6); | |
6d2010ae | 927 | IFA_REMREF(&ia6->ia_ifa); |
9bccf70c A |
928 | } |
929 | } | |
930 | ||
1c79356b A |
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)) { | |
39236c6e A |
941 | #if 0 /* touches NULL pointer */ |
942 | in6_ifstat_inc(inifp, ifs6_in_discard); | |
1c79356b | 943 | #endif |
6d2010ae | 944 | goto done; /* m have already been freed */ |
1c79356b | 945 | } |
9bccf70c | 946 | |
1c79356b A |
947 | /* adjust pointer */ |
948 | ip6 = mtod(m, struct ip6_hdr *); | |
9bccf70c A |
949 | |
950 | /* | |
55e303ae | 951 | * if the payload length field is 0 and the next header field |
9bccf70c A |
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 | |
6d2010ae A |
958 | * contained, ip6_hopopts_input() must set a valid |
959 | * (non-zero) payload length to the variable plen. | |
9bccf70c A |
960 | */ |
961 | ip6stat.ip6s_badoptions++; | |
39236c6e A |
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); | |
6d2010ae | 966 | goto done; |
9bccf70c | 967 | } |
1c79356b A |
968 | /* ip6_hopopts_input() ensures that mbuf is contiguous */ |
969 | hbh = (struct ip6_hbh *)(ip6 + 1); | |
1c79356b A |
970 | nxt = hbh->ip6h_nxt; |
971 | ||
972 | /* | |
6d2010ae A |
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. | |
1c79356b | 978 | */ |
6d2010ae A |
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 | } | |
1c79356b A |
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 | */ | |
39236c6e | 1001 | if (m->m_pkthdr.len - sizeof (struct ip6_hdr) < plen) { |
1c79356b | 1002 | ip6stat.ip6s_tooshort++; |
39236c6e | 1003 | in6_ifstat_inc(inifp, ifs6_in_truncated); |
1c79356b A |
1004 | goto bad; |
1005 | } | |
39236c6e A |
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++; | |
1c79356b | 1023 | if (m->m_len == m->m_pkthdr.len) { |
39236c6e A |
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 | } | |
1c79356b A |
1030 | } |
1031 | ||
1032 | /* | |
1033 | * Forward if desirable. | |
1034 | */ | |
1035 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { | |
316670eb A |
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 || | |
39236c6e | 1046 | (m->m_pkthdr.pkt_flags & PKTF_PROXY_DST)); |
316670eb | 1047 | } |
6d2010ae A |
1048 | if (!ours) |
1049 | goto bad; | |
1c79356b | 1050 | } else if (!ours) { |
316670eb A |
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 | */ | |
39236c6e | 1063 | if ((m = ip6_forward(m, &rin6, 0)) == NULL) |
316670eb | 1064 | goto done; |
39236c6e A |
1065 | VERIFY(rin6.ro_rt != NULL); |
1066 | VERIFY(m->m_pkthdr.pkt_flags & PKTF_PROXY_DST); | |
1067 | deliverifp = rin6.ro_rt->rt_ifp; | |
316670eb A |
1068 | ours = 1; |
1069 | } | |
1c79356b A |
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++; | |
39236c6e | 1085 | in6_ifstat_inc(inifp, ifs6_in_addrerr); |
1c79356b A |
1086 | goto bad; |
1087 | } | |
1088 | ||
1089 | /* | |
1090 | * Tell launch routine the next header | |
1091 | */ | |
1c79356b | 1092 | ip6stat.ip6s_delivered++; |
39236c6e | 1093 | in6_ifstat_inc_na(deliverifp, ifs6_in_deliver); |
91447636 | 1094 | |
91447636 | 1095 | injectit: |
1c79356b A |
1096 | nest = 0; |
1097 | ||
316670eb A |
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 | */ | |
39236c6e | 1103 | IP6_HDR_ALIGNMENT_FIXUP(m, inifp, return); |
316670eb | 1104 | |
1c79356b | 1105 | while (nxt != IPPROTO_DONE) { |
91447636 | 1106 | struct ipfilter *filter; |
6d2010ae | 1107 | int (*pr_input)(struct mbuf **, int *, int); |
0b4c1975 | 1108 | |
1c79356b A |
1109 | if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { |
1110 | ip6stat.ip6s_toomanyhdr++; | |
6d2010ae | 1111 | goto bad; |
1c79356b A |
1112 | } |
1113 | ||
1114 | /* | |
1115 | * protection against faulty packet - there should be | |
1116 | * more sanity checks in header chain processing. | |
1117 | */ | |
9bccf70c | 1118 | if (m->m_pkthdr.len < off) { |
1c79356b | 1119 | ip6stat.ip6s_tooshort++; |
39236c6e | 1120 | in6_ifstat_inc(inifp, ifs6_in_truncated); |
9bccf70c A |
1121 | goto bad; |
1122 | } | |
6d2010ae | 1123 | |
9bccf70c A |
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 | */ | |
316670eb A |
1131 | if ((ipsec_bypass == 0) && |
1132 | (ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0) { | |
91447636 | 1133 | if (ipsec6_in_reject(m, NULL)) { |
2d21ac55 | 1134 | IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio); |
6d2010ae | 1135 | goto bad; |
39236c6e | 1136 | } |
1c79356b | 1137 | } |
39236c6e | 1138 | #endif /* IPSEC */ |
9bccf70c | 1139 | |
91447636 | 1140 | /* |
2d21ac55 | 1141 | * Call IP filter |
91447636 | 1142 | */ |
743345f9 | 1143 | if (!TAILQ_EMPTY(&ipv6_filters) && !IFNET_IS_INTCOPROC(inifp)) { |
91447636 A |
1144 | ipf_ref(); |
1145 | TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) { | |
1146 | if (seen == 0) { | |
316670eb A |
1147 | if ((struct ipfilter *)inject_ipfref == |
1148 | filter) | |
91447636 A |
1149 | seen = 1; |
1150 | } else if (filter->ipf_filter.ipf_input) { | |
1151 | errno_t result; | |
316670eb | 1152 | |
91447636 | 1153 | result = filter->ipf_filter.ipf_input( |
39236c6e A |
1154 | filter->ipf_filter.cookie, |
1155 | (mbuf_t *)&m, off, nxt); | |
91447636 A |
1156 | if (result == EJUSTRETURN) { |
1157 | ipf_unref(); | |
6d2010ae | 1158 | goto done; |
91447636 A |
1159 | } |
1160 | if (result != 0) { | |
1161 | ipf_unref(); | |
6d2010ae | 1162 | goto bad; |
91447636 A |
1163 | } |
1164 | } | |
1165 | } | |
1166 | ipf_unref(); | |
1167 | } | |
0b4c1975 | 1168 | |
6d2010ae | 1169 | DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL, |
39236c6e A |
1170 | struct ip6_hdr *, ip6, struct ifnet *, inifp, |
1171 | struct ip *, NULL, struct ip6_hdr *, ip6); | |
6d2010ae | 1172 | |
0b4c1975 A |
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)) { | |
91447636 | 1178 | lck_mtx_lock(inet6_domain_mutex); |
6d2010ae | 1179 | nxt = pr_input(&m, &off, nxt); |
91447636 | 1180 | lck_mtx_unlock(inet6_domain_mutex); |
0b4c1975 | 1181 | } else { |
6d2010ae | 1182 | nxt = pr_input(&m, &off, nxt); |
91447636 | 1183 | } |
1c79356b | 1184 | } |
6d2010ae | 1185 | done: |
39236c6e | 1186 | ROUTE_RELEASE(&rin6); |
1c79356b | 1187 | return; |
39236c6e | 1188 | bad: |
1c79356b | 1189 | m_freem(m); |
6d2010ae | 1190 | goto done; |
1c79356b A |
1191 | } |
1192 | ||
39236c6e A |
1193 | void |
1194 | ip6_setsrcifaddr_info(struct mbuf *m, uint32_t src_idx, struct in6_ifaddr *ia6) | |
9bccf70c | 1195 | { |
39236c6e A |
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; | |
6d2010ae | 1214 | } |
9bccf70c A |
1215 | } |
1216 | ||
39236c6e A |
1217 | void |
1218 | ip6_setdstifaddr_info(struct mbuf *m, uint32_t dst_idx, struct in6_ifaddr *ia6) | |
9bccf70c | 1219 | { |
39236c6e | 1220 | VERIFY(m->m_flags & M_PKTHDR); |
9bccf70c | 1221 | |
39236c6e A |
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; | |
6d2010ae | 1238 | } |
39236c6e A |
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); | |
9bccf70c A |
1273 | } |
1274 | ||
1c79356b A |
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 | |
6d2010ae A |
1280 | ip6_hopopts_input(uint32_t *plenp, uint32_t *rtalertp, struct mbuf **mp, |
1281 | int *offp) | |
1c79356b | 1282 | { |
9bccf70c | 1283 | struct mbuf *m = *mp; |
1c79356b A |
1284 | int off = *offp, hbhlen; |
1285 | struct ip6_hbh *hbh; | |
1286 | u_int8_t *opt; | |
1287 | ||
1288 | /* validation of the length of the header */ | |
39236c6e | 1289 | IP6_EXTHDR_CHECK(m, off, sizeof (*hbh), return (-1)); |
1c79356b A |
1290 | hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); |
1291 | hbhlen = (hbh->ip6h_len + 1) << 3; | |
1292 | ||
39236c6e | 1293 | IP6_EXTHDR_CHECK(m, off, hbhlen, return (-1)); |
1c79356b | 1294 | hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); |
1c79356b | 1295 | off += hbhlen; |
39236c6e A |
1296 | hbhlen -= sizeof (struct ip6_hbh); |
1297 | opt = (u_int8_t *)hbh + sizeof (struct ip6_hbh); | |
1c79356b | 1298 | |
39236c6e A |
1299 | if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof (struct ip6_hbh), |
1300 | hbhlen, rtalertp, plenp) < 0) | |
6d2010ae | 1301 | return (-1); |
1c79356b A |
1302 | |
1303 | *offp = off; | |
1304 | *mp = m; | |
6d2010ae | 1305 | return (0); |
1c79356b A |
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(). | |
9bccf70c A |
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. | |
1c79356b A |
1317 | */ |
1318 | int | |
39037602 A |
1319 | ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen, |
1320 | u_int32_t *rtalertp, u_int32_t *plenp) | |
1c79356b A |
1321 | { |
1322 | struct ip6_hdr *ip6; | |
1323 | int optlen = 0; | |
1324 | u_int8_t *opt = opthead; | |
1325 | u_int16_t rtalert_val; | |
9bccf70c | 1326 | u_int32_t jumboplen; |
39236c6e | 1327 | const int erroff = sizeof (struct ip6_hdr) + sizeof (struct ip6_hbh); |
1c79356b A |
1328 | |
1329 | for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { | |
9bccf70c A |
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; | |
6d2010ae | 1341 | case IP6OPT_ROUTER_ALERT: |
9bccf70c A |
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); | |
39236c6e | 1352 | return (-1); |
9bccf70c A |
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); | |
39236c6e | 1369 | return (-1); |
9bccf70c A |
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); | |
39236c6e | 1383 | return (-1); |
9bccf70c A |
1384 | } |
1385 | ||
1386 | /* | |
1387 | * We may see jumbolen in unaligned location, so | |
1388 | * we'd need to perform bcopy(). | |
1389 | */ | |
39236c6e | 1390 | bcopy(opt + 2, &jumboplen, sizeof (jumboplen)); |
9bccf70c A |
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); | |
39236c6e | 1407 | return (-1); |
9bccf70c | 1408 | } |
1c79356b | 1409 | #endif |
9bccf70c A |
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); | |
39236c6e | 1419 | return (-1); |
9bccf70c A |
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, | |
6d2010ae | 1430 | erroff + opt - opthead); |
91447636 | 1431 | if (optlen == -1) { |
39236c6e | 1432 | return (-1); |
91447636 | 1433 | } |
9bccf70c A |
1434 | optlen += 2; |
1435 | break; | |
1c79356b A |
1436 | } |
1437 | } | |
1438 | ||
39236c6e | 1439 | return (0); |
1c79356b | 1440 | |
39236c6e | 1441 | bad: |
1c79356b | 1442 | m_freem(m); |
39236c6e | 1443 | return (-1); |
1c79356b A |
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 | |
6d2010ae | 1453 | ip6_unknown_opt(uint8_t *optp, struct mbuf *m, int off) |
1c79356b A |
1454 | { |
1455 | struct ip6_hdr *ip6; | |
1456 | ||
9bccf70c A |
1457 | switch (IP6OPT_TYPE(*optp)) { |
1458 | case IP6OPT_TYPE_SKIP: /* ignore the option */ | |
39236c6e A |
1459 | return ((int)*(optp + 1)); |
1460 | ||
9bccf70c A |
1461 | case IP6OPT_TYPE_DISCARD: /* silently discard */ |
1462 | m_freem(m); | |
39236c6e A |
1463 | return (-1); |
1464 | ||
9bccf70c A |
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); | |
39236c6e A |
1468 | return (-1); |
1469 | ||
9bccf70c A |
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) || | |
39236c6e | 1474 | (m->m_flags & (M_BCAST|M_MCAST))) { |
9bccf70c | 1475 | m_freem(m); |
39236c6e | 1476 | } else { |
9bccf70c | 1477 | icmp6_error(m, ICMP6_PARAM_PROB, |
39236c6e A |
1478 | ICMP6_PARAMPROB_OPTION, off); |
1479 | } | |
1480 | return (-1); | |
1c79356b A |
1481 | } |
1482 | ||
1483 | m_freem(m); /* XXX: NOTREACHED */ | |
6d2010ae | 1484 | return (-1); |
1c79356b A |
1485 | } |
1486 | ||
1487 | /* | |
1488 | * Create the "control" list for this pcb. | |
6d2010ae | 1489 | * These functions will not modify mbuf chain at all. |
1c79356b | 1490 | * |
6d2010ae | 1491 | * With KAME mbuf chain restriction: |
1c79356b A |
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. | |
6d2010ae A |
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. | |
1c79356b | 1501 | */ |
6d2010ae A |
1502 | struct mbuf ** |
1503 | ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp, | |
1504 | int *v4only) | |
1c79356b | 1505 | { |
6d2010ae | 1506 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); |
1c79356b | 1507 | |
6d2010ae | 1508 | if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) { |
1c79356b A |
1509 | struct timeval tv; |
1510 | ||
39236c6e A |
1511 | getmicrotime(&tv); |
1512 | mp = sbcreatecontrol_mbuf((caddr_t)&tv, sizeof (tv), | |
6d2010ae | 1513 | SCM_TIMESTAMP, SOL_SOCKET, mp); |
39236c6e A |
1514 | if (*mp == NULL) |
1515 | return (NULL); | |
1c79356b | 1516 | } |
39236c6e A |
1517 | if ((inp->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { |
1518 | uint64_t time; | |
1c79356b | 1519 | |
39236c6e A |
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 | } | |
6d2010ae | 1526 | if ((inp->inp_socket->so_flags & SOF_RECV_TRAFFIC_CLASS) != 0) { |
316670eb A |
1527 | int tc = m_get_traffic_class(m); |
1528 | ||
39236c6e A |
1529 | mp = sbcreatecontrol_mbuf((caddr_t)&tc, sizeof (tc), |
1530 | SO_TRAFFIC_CLASS, SOL_SOCKET, mp); | |
316670eb A |
1531 | if (*mp == NULL) |
1532 | return (NULL); | |
6d2010ae A |
1533 | } |
1534 | ||
1535 | if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { | |
1536 | if (v4only != NULL) | |
1537 | *v4only = 1; | |
1538 | return (mp); | |
1539 | } | |
1540 | ||
39236c6e | 1541 | #define IS2292(inp, x, y) (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y)) |
1c79356b | 1542 | /* RFC 2292 sec. 5 */ |
6d2010ae | 1543 | if ((inp->inp_flags & IN6P_PKTINFO) != 0) { |
9bccf70c | 1544 | struct in6_pktinfo pi6; |
6d2010ae | 1545 | |
39236c6e | 1546 | bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof (struct in6_addr)); |
6d2010ae A |
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 | ||
39236c6e A |
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); | |
1c79356b A |
1557 | } |
1558 | ||
6d2010ae | 1559 | if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) { |
9bccf70c | 1560 | int hlim = ip6->ip6_hlim & 0xff; |
6d2010ae | 1561 | |
39236c6e | 1562 | mp = sbcreatecontrol_mbuf((caddr_t)&hlim, sizeof (int), |
6d2010ae A |
1563 | IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), |
1564 | IPPROTO_IPV6, mp); | |
39236c6e A |
1565 | if (*mp == NULL) |
1566 | return (NULL); | |
1c79356b A |
1567 | } |
1568 | ||
6d2010ae A |
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; | |
b0d623f7 | 1585 | |
6d2010ae A |
1586 | mp = np; |
1587 | if (v4only) | |
39236c6e | 1588 | return (0); |
b0d623f7 | 1589 | |
6d2010ae A |
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; | |
39236c6e | 1598 | mp = sbcreatecontrol_mbuf((caddr_t)&tclass, sizeof (tclass), |
6d2010ae | 1599 | IPV6_TCLASS, IPPROTO_IPV6, mp); |
39236c6e | 1600 | if (*mp == NULL) |
6d2010ae A |
1601 | goto no_mbufs; |
1602 | } | |
b0d623f7 | 1603 | |
1c79356b | 1604 | /* |
91447636 A |
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. | |
6d2010ae | 1609 | * See also RFC 2292 section 6 (or RFC 3542 section 8). |
1c79356b | 1610 | */ |
6d2010ae | 1611 | if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) { |
1c79356b A |
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 | |
6d2010ae A |
1616 | * just after the IPv6 header, which is assured through the |
1617 | * IPv6 input processing. | |
1c79356b | 1618 | */ |
91447636 | 1619 | ip6 = mtod(m, struct ip6_hdr *); |
1c79356b | 1620 | if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { |
9bccf70c A |
1621 | struct ip6_hbh *hbh; |
1622 | int hbhlen = 0; | |
1c79356b A |
1623 | hbh = (struct ip6_hbh *)(ip6 + 1); |
1624 | hbhlen = (hbh->ip6h_len + 1) << 3; | |
1c79356b | 1625 | |
1c79356b | 1626 | /* |
6d2010ae A |
1627 | * XXX: We copy the whole header even if a |
1628 | * jumbo payload option is included, the option which | |
1629 | * is to be removed before returning according to | |
1630 | * RFC2292. | |
1631 | * Note: this constraint is removed in RFC3542 | |
1c79356b | 1632 | */ |
6d2010ae A |
1633 | mp = sbcreatecontrol_mbuf((caddr_t)hbh, hbhlen, |
1634 | IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS), | |
1635 | IPPROTO_IPV6, mp); | |
1636 | ||
6d2010ae A |
1637 | if (*mp == NULL) { |
1638 | goto no_mbufs; | |
9bccf70c | 1639 | } |
9bccf70c A |
1640 | } |
1641 | } | |
1642 | ||
6d2010ae | 1643 | if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) { |
39236c6e | 1644 | int nxt = ip6->ip6_nxt, off = sizeof (struct ip6_hdr); |
1c79356b A |
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; | |
1c79356b A |
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 | ||
39236c6e | 1671 | if (off + sizeof (*ip6e) > m->m_len) |
1c79356b A |
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; | |
1c79356b A |
1680 | |
1681 | switch (nxt) { | |
1682 | case IPPROTO_DSTOPTS: | |
6d2010ae | 1683 | if (!(in6p->inp_flags & IN6P_DSTOPTS)) |
1c79356b A |
1684 | break; |
1685 | ||
6d2010ae | 1686 | mp = sbcreatecontrol_mbuf((caddr_t)ip6e, elen, |
39236c6e A |
1687 | IS2292(in6p, IPV6_2292DSTOPTS, |
1688 | IPV6_DSTOPTS), IPPROTO_IPV6, mp); | |
1689 | if (*mp == NULL) { | |
6d2010ae A |
1690 | goto no_mbufs; |
1691 | } | |
1c79356b | 1692 | break; |
1c79356b | 1693 | case IPPROTO_ROUTING: |
3e170ce0 | 1694 | if (!(in6p->inp_flags & IN6P_RTHDR)) |
1c79356b A |
1695 | break; |
1696 | ||
6d2010ae A |
1697 | mp = sbcreatecontrol_mbuf((caddr_t)ip6e, elen, |
1698 | IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR), | |
1699 | IPPROTO_IPV6, mp); | |
1700 | if (*mp == NULL) { | |
6d2010ae A |
1701 | goto no_mbufs; |
1702 | } | |
1c79356b | 1703 | break; |
1c79356b A |
1704 | case IPPROTO_HOPOPTS: |
1705 | case IPPROTO_AH: /* is it possible? */ | |
1706 | break; | |
1707 | ||
1708 | default: | |
1709 | /* | |
6d2010ae | 1710 | * other cases have been filtered in the above. |
1c79356b A |
1711 | * none will visit this case. here we supply |
1712 | * the code just in case (nxt overwritten or | |
1713 | * other cases). | |
1714 | */ | |
1c79356b A |
1715 | goto loopend; |
1716 | ||
1717 | } | |
1718 | ||
1719 | /* proceed with the next header. */ | |
1720 | off += elen; | |
1721 | nxt = ip6e->ip6e_nxt; | |
1722 | ip6e = NULL; | |
1c79356b | 1723 | } |
39236c6e | 1724 | loopend: |
9bccf70c | 1725 | ; |
1c79356b | 1726 | } |
39236c6e | 1727 | return (0); |
6d2010ae A |
1728 | no_mbufs: |
1729 | ip6stat.ip6s_pktdropcntrl++; | |
1730 | /* XXX increment a stat to show the failure */ | |
39236c6e | 1731 | return (ENOBUFS); |
6d2010ae A |
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; | |
1c79356b | 1743 | |
6d2010ae A |
1744 | if (mtu == NULL) |
1745 | return; | |
1746 | ||
1747 | #ifdef DIAGNOSTIC | |
39236c6e | 1748 | if (so == NULL) { /* I believe this is impossible */ |
6d2010ae | 1749 | panic("ip6_notify_pmtu: socket is NULL"); |
39236c6e A |
1750 | /* NOTREACHED */ |
1751 | } | |
6d2010ae A |
1752 | #endif |
1753 | ||
39236c6e | 1754 | bzero(&mtuctl, sizeof (mtuctl)); /* zero-clear for safety */ |
6d2010ae A |
1755 | mtuctl.ip6m_mtu = *mtu; |
1756 | mtuctl.ip6m_addr = *dst; | |
316670eb | 1757 | if (sa6_recoverscope(&mtuctl.ip6m_addr, TRUE)) |
6d2010ae A |
1758 | return; |
1759 | ||
39236c6e | 1760 | if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof (mtuctl), |
6d2010ae A |
1761 | IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) |
1762 | return; | |
1763 | ||
39236c6e | 1764 | if (sbappendaddr(&so->so_rcv, SA(dst), NULL, m_mtu, NULL) == 0) { |
6d2010ae A |
1765 | m_freem(m_mtu); |
1766 | /* XXX: should count statistics */ | |
39236c6e | 1767 | } else { |
6d2010ae | 1768 | sorwakeup(so); |
39236c6e | 1769 | } |
1c79356b A |
1770 | } |
1771 | ||
1c79356b A |
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 * | |
39037602 | 1785 | ip6_get_prevhdr(struct mbuf *m, int off) |
1c79356b A |
1786 | { |
1787 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
1788 | ||
39236c6e A |
1789 | if (off == sizeof (struct ip6_hdr)) { |
1790 | return ((char *)&ip6->ip6_nxt); | |
1791 | } else { | |
1c79356b A |
1792 | int len, nxt; |
1793 | struct ip6_ext *ip6e = NULL; | |
1794 | ||
1795 | nxt = ip6->ip6_nxt; | |
39236c6e | 1796 | len = sizeof (struct ip6_hdr); |
1c79356b A |
1797 | while (len < off) { |
1798 | ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); | |
1799 | ||
9bccf70c | 1800 | switch (nxt) { |
1c79356b | 1801 | case IPPROTO_FRAGMENT: |
39236c6e | 1802 | len += sizeof (struct ip6_frag); |
1c79356b A |
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) | |
39236c6e | 1814 | return ((char *)&ip6e->ip6e_nxt); |
1c79356b | 1815 | else |
39236c6e | 1816 | return (NULL); |
1c79356b A |
1817 | } |
1818 | } | |
1819 | ||
1820 | /* | |
1821 | * get next header offset. m will be retained. | |
1822 | */ | |
1823 | int | |
39236c6e | 1824 | ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp) |
1c79356b A |
1825 | { |
1826 | struct ip6_hdr ip6; | |
1827 | struct ip6_ext ip6e; | |
1828 | struct ip6_frag fh; | |
1829 | ||
1830 | /* just in case */ | |
39236c6e | 1831 | VERIFY(m != NULL); |
1c79356b | 1832 | if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) |
39236c6e | 1833 | return (-1); |
1c79356b A |
1834 | |
1835 | switch (proto) { | |
1836 | case IPPROTO_IPV6: | |
39236c6e A |
1837 | if (m->m_pkthdr.len < off + sizeof (ip6)) |
1838 | return (-1); | |
1839 | m_copydata(m, off, sizeof (ip6), (caddr_t)&ip6); | |
1c79356b A |
1840 | if (nxtp) |
1841 | *nxtp = ip6.ip6_nxt; | |
39236c6e A |
1842 | off += sizeof (ip6); |
1843 | return (off); | |
1c79356b A |
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 | */ | |
39236c6e A |
1850 | if (m->m_pkthdr.len < off + sizeof (fh)) |
1851 | return (-1); | |
1852 | m_copydata(m, off, sizeof (fh), (caddr_t)&fh); | |
91447636 A |
1853 | /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ |
1854 | if (fh.ip6f_offlg & IP6F_OFF_MASK) | |
39236c6e | 1855 | return (-1); |
1c79356b A |
1856 | if (nxtp) |
1857 | *nxtp = fh.ip6f_nxt; | |
39236c6e A |
1858 | off += sizeof (struct ip6_frag); |
1859 | return (off); | |
1c79356b A |
1860 | |
1861 | case IPPROTO_AH: | |
39236c6e A |
1862 | if (m->m_pkthdr.len < off + sizeof (ip6e)) |
1863 | return (-1); | |
1864 | m_copydata(m, off, sizeof (ip6e), (caddr_t)&ip6e); | |
1c79356b A |
1865 | if (nxtp) |
1866 | *nxtp = ip6e.ip6e_nxt; | |
1867 | off += (ip6e.ip6e_len + 2) << 2; | |
39236c6e | 1868 | return (off); |
1c79356b A |
1869 | |
1870 | case IPPROTO_HOPOPTS: | |
1871 | case IPPROTO_ROUTING: | |
1872 | case IPPROTO_DSTOPTS: | |
39236c6e A |
1873 | if (m->m_pkthdr.len < off + sizeof (ip6e)) |
1874 | return (-1); | |
1875 | m_copydata(m, off, sizeof (ip6e), (caddr_t)&ip6e); | |
1c79356b A |
1876 | if (nxtp) |
1877 | *nxtp = ip6e.ip6e_nxt; | |
1878 | off += (ip6e.ip6e_len + 1) << 3; | |
39236c6e | 1879 | return (off); |
1c79356b A |
1880 | |
1881 | case IPPROTO_NONE: | |
1882 | case IPPROTO_ESP: | |
1883 | case IPPROTO_IPCOMP: | |
1884 | /* give up */ | |
39236c6e | 1885 | return (-1); |
1c79356b A |
1886 | |
1887 | default: | |
39236c6e | 1888 | return (-1); |
1c79356b | 1889 | } |
1c79356b A |
1890 | } |
1891 | ||
1892 | /* | |
1893 | * get offset for the last header in the chain. m will be kept untainted. | |
1894 | */ | |
1895 | int | |
39236c6e | 1896 | ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp) |
1c79356b A |
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) | |
39236c6e | 1908 | return (off); |
1c79356b | 1909 | else if (newoff < off) |
39236c6e | 1910 | return (-1); /* invalid */ |
1c79356b | 1911 | else if (newoff == off) |
39236c6e | 1912 | return (newoff); |
1c79356b A |
1913 | |
1914 | off = newoff; | |
1915 | proto = *nxtp; | |
1916 | } | |
1917 | } | |
1918 | ||
2d21ac55 | 1919 | struct ip6aux * |
316670eb | 1920 | ip6_addaux(struct mbuf *m) |
9bccf70c | 1921 | { |
2d21ac55 | 1922 | struct m_tag *tag; |
316670eb | 1923 | |
2d21ac55 | 1924 | /* Check if one is already allocated */ |
316670eb A |
1925 | tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, |
1926 | KERNEL_TAG_TYPE_INET6, NULL); | |
2d21ac55 A |
1927 | if (tag == NULL) { |
1928 | /* Allocate a tag */ | |
6d2010ae A |
1929 | tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_INET6, |
1930 | sizeof (struct ip6aux), M_DONTWAIT, m); | |
b0d623f7 | 1931 | |
2d21ac55 A |
1932 | /* Attach it to the mbuf */ |
1933 | if (tag) { | |
1934 | m_tag_prepend(m, tag); | |
55e303ae | 1935 | } |
9bccf70c | 1936 | } |
316670eb A |
1937 | |
1938 | return (tag ? (struct ip6aux *)(tag + 1) : NULL); | |
9bccf70c A |
1939 | } |
1940 | ||
2d21ac55 | 1941 | struct ip6aux * |
316670eb | 1942 | ip6_findaux(struct mbuf *m) |
9bccf70c | 1943 | { |
2d21ac55 | 1944 | struct m_tag *tag; |
316670eb A |
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); | |
9bccf70c A |
1950 | } |
1951 | ||
1952 | void | |
316670eb | 1953 | ip6_delaux(struct mbuf *m) |
9bccf70c | 1954 | { |
2d21ac55 | 1955 | struct m_tag *tag; |
9bccf70c | 1956 | |
316670eb A |
1957 | tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, |
1958 | KERNEL_TAG_TYPE_INET6, NULL); | |
2d21ac55 A |
1959 | if (tag) { |
1960 | m_tag_delete(m, tag); | |
1961 | } | |
9bccf70c A |
1962 | } |
1963 | ||
6d2010ae | 1964 | /* |
39236c6e | 1965 | * Drain callback |
6d2010ae A |
1966 | */ |
1967 | void | |
39236c6e | 1968 | ip6_drain(void) |
6d2010ae | 1969 | { |
39236c6e A |
1970 | frag6_drain(); /* fragments */ |
1971 | in6_rtqdrain(); /* protocol cloned routes */ | |
1972 | nd6_drain(NULL); /* cloned routes: ND6 */ | |
6d2010ae A |
1973 | } |
1974 | ||
1c79356b A |
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 | }; | |
3e170ce0 A |
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 | } |