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