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