]>
Commit | Line | Data |
---|---|---|
91447636 | 1 | /* |
eb6b6ca3 | 2 | * Copyright (c) 2004-2020 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5ba3f43e | 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. | |
5ba3f43e | 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. | |
5ba3f43e | 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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
5ba3f43e | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
91447636 A |
27 | */ |
28 | ||
0a7de745 | 29 | #include <sys/param.h> /* for definition of NULL */ |
91447636 A |
30 | #include <sys/errno.h> |
31 | #include <sys/malloc.h> | |
32 | #include <sys/socket.h> | |
33 | #include <sys/mbuf.h> | |
34 | #include <sys/systm.h> | |
b0d623f7 A |
35 | #include <libkern/OSAtomic.h> |
36 | ||
37 | #include <machine/endian.h> | |
91447636 | 38 | |
0a7de745 | 39 | #define _IP_VHL |
91447636 A |
40 | #include <net/if_var.h> |
41 | #include <net/route.h> | |
42 | #include <net/kpi_protocol.h> | |
5ba3f43e | 43 | #include <net/net_api_stats.h> |
91447636 A |
44 | |
45 | #include <netinet/in_systm.h> | |
46 | #include <netinet/in.h> | |
47 | #include <netinet/in_var.h> | |
48 | #include <netinet6/in6_var.h> | |
49 | #include <netinet/ip.h> | |
50 | #include <netinet/ip6.h> | |
51 | #include <netinet/ip_var.h> | |
52 | #include <netinet6/ip6_var.h> | |
53 | #include <netinet/kpi_ipfilter_var.h> | |
54 | ||
5ba3f43e | 55 | #include <stdbool.h> |
b0d623f7 | 56 | |
91447636 A |
57 | /* |
58 | * kipf_lock and kipf_ref protect the linkage of the list of IP filters | |
59 | * An IP filter can be removed only when kipf_ref is zero | |
5ba3f43e A |
60 | * If an IP filter cannot be removed because kipf_ref is not null, then |
61 | * the IP filter is marjed and kipf_delayed_remove is set so that when | |
91447636 A |
62 | * kipf_ref eventually goes down to zero, the IP filter is removed |
63 | */ | |
316670eb A |
64 | decl_lck_mtx_data(static, kipf_lock_data); |
65 | static lck_mtx_t *kipf_lock = &kipf_lock_data; | |
b0d623f7 A |
66 | static u_int32_t kipf_ref = 0; |
67 | static u_int32_t kipf_delayed_remove = 0; | |
68 | u_int32_t kipf_count = 0; | |
91447636 | 69 | |
0a7de745 A |
70 | __private_extern__ struct ipfilter_list ipv4_filters = TAILQ_HEAD_INITIALIZER(ipv4_filters); |
71 | __private_extern__ struct ipfilter_list ipv6_filters = TAILQ_HEAD_INITIALIZER(ipv6_filters); | |
72 | __private_extern__ struct ipfilter_list tbr_filters = TAILQ_HEAD_INITIALIZER(tbr_filters); | |
91447636 | 73 | |
5ba3f43e A |
74 | #undef ipf_addv4 |
75 | #undef ipf_addv6 | |
76 | extern errno_t ipf_addv4(const struct ipf_filter *filter, | |
77 | ipfilter_t *filter_ref); | |
78 | extern errno_t ipf_addv6(const struct ipf_filter *filter, | |
79 | ipfilter_t *filter_ref); | |
80 | ||
81 | static errno_t ipf_add(const struct ipf_filter *filter, | |
82 | ipfilter_t *filter_ref, struct ipfilter_list *head, bool is_internal); | |
83 | ||
91447636 A |
84 | __private_extern__ void |
85 | ipf_ref(void) | |
86 | { | |
87 | lck_mtx_lock(kipf_lock); | |
cb323159 A |
88 | if (os_inc_overflow(&kipf_ref)) { |
89 | panic("kipf_ref overflow"); | |
90 | } | |
91447636 A |
91 | lck_mtx_unlock(kipf_lock); |
92 | } | |
93 | ||
94 | __private_extern__ void | |
95 | ipf_unref(void) | |
96 | { | |
97 | lck_mtx_lock(kipf_lock); | |
98 | ||
cb323159 A |
99 | if (os_dec_overflow(&kipf_ref)) { |
100 | panic("kipf_ref underflow"); | |
0a7de745 | 101 | } |
5ba3f43e | 102 | |
5ba3f43e A |
103 | if (kipf_ref == 0 && kipf_delayed_remove != 0) { |
104 | struct ipfilter *filter; | |
91447636 A |
105 | |
106 | while ((filter = TAILQ_FIRST(&tbr_filters))) { | |
5ba3f43e A |
107 | VERIFY(OSDecrementAtomic64(&net_api_stats.nas_ipf_add_count) > 0); |
108 | ||
91447636 A |
109 | ipf_detach_func ipf_detach = filter->ipf_filter.ipf_detach; |
110 | void* cookie = filter->ipf_filter.cookie; | |
5ba3f43e | 111 | |
91447636 A |
112 | TAILQ_REMOVE(filter->ipf_head, filter, ipf_link); |
113 | TAILQ_REMOVE(&tbr_filters, filter, ipf_tbr); | |
114 | kipf_delayed_remove--; | |
115 | ||
116 | if (ipf_detach) { | |
117 | lck_mtx_unlock(kipf_lock); | |
118 | ipf_detach(cookie); | |
119 | lck_mtx_lock(kipf_lock); | |
120 | /* In case some filter got to run while we released the lock */ | |
0a7de745 | 121 | if (kipf_ref != 0) { |
91447636 | 122 | break; |
0a7de745 | 123 | } |
5ba3f43e | 124 | } |
91447636 | 125 | } |
5ba3f43e | 126 | } |
91447636 A |
127 | lck_mtx_unlock(kipf_lock); |
128 | } | |
129 | ||
130 | static errno_t | |
131 | ipf_add( | |
5ba3f43e | 132 | const struct ipf_filter *filter, |
91447636 | 133 | ipfilter_t *filter_ref, |
5ba3f43e A |
134 | struct ipfilter_list *head, |
135 | bool is_internal) | |
91447636 | 136 | { |
0a7de745 A |
137 | struct ipfilter *new_filter; |
138 | if (filter->name == NULL || (filter->ipf_input == NULL && filter->ipf_output == NULL)) { | |
139 | return EINVAL; | |
140 | } | |
5ba3f43e A |
141 | |
142 | MALLOC(new_filter, struct ipfilter *, sizeof(*new_filter), M_IFADDR, M_WAITOK); | |
0a7de745 A |
143 | if (new_filter == NULL) { |
144 | return ENOMEM; | |
145 | } | |
5ba3f43e | 146 | |
91447636 A |
147 | lck_mtx_lock(kipf_lock); |
148 | new_filter->ipf_filter = *filter; | |
149 | new_filter->ipf_head = head; | |
5ba3f43e | 150 | |
b0d623f7 | 151 | TAILQ_INSERT_HEAD(head, new_filter, ipf_link); |
5ba3f43e A |
152 | |
153 | OSIncrementAtomic64(&net_api_stats.nas_ipf_add_count); | |
154 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_ipf_add_total); | |
155 | if (is_internal) { | |
156 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_ipf_add_os_total); | |
157 | } | |
158 | ||
91447636 | 159 | lck_mtx_unlock(kipf_lock); |
5ba3f43e | 160 | |
91447636 | 161 | *filter_ref = (ipfilter_t)new_filter; |
b0d623f7 A |
162 | |
163 | /* This will force TCP to re-evaluate its use of TSO */ | |
164 | OSAddAtomic(1, &kipf_count); | |
39236c6e | 165 | routegenid_update(); |
b0d623f7 | 166 | |
0a7de745 | 167 | return 0; |
5ba3f43e A |
168 | } |
169 | ||
170 | errno_t | |
171 | ipf_addv4_internal( | |
172 | const struct ipf_filter *filter, | |
173 | ipfilter_t *filter_ref) | |
174 | { | |
0a7de745 | 175 | return ipf_add(filter, filter_ref, &ipv4_filters, true); |
91447636 A |
176 | } |
177 | ||
178 | errno_t | |
179 | ipf_addv4( | |
5ba3f43e A |
180 | const struct ipf_filter *filter, |
181 | ipfilter_t *filter_ref) | |
182 | { | |
0a7de745 | 183 | return ipf_add(filter, filter_ref, &ipv4_filters, false); |
5ba3f43e A |
184 | } |
185 | ||
186 | errno_t | |
187 | ipf_addv6_internal( | |
188 | const struct ipf_filter *filter, | |
91447636 A |
189 | ipfilter_t *filter_ref) |
190 | { | |
0a7de745 | 191 | return ipf_add(filter, filter_ref, &ipv6_filters, true); |
91447636 A |
192 | } |
193 | ||
194 | errno_t | |
195 | ipf_addv6( | |
5ba3f43e | 196 | const struct ipf_filter *filter, |
91447636 A |
197 | ipfilter_t *filter_ref) |
198 | { | |
0a7de745 | 199 | return ipf_add(filter, filter_ref, &ipv6_filters, false); |
91447636 A |
200 | } |
201 | ||
fe8ab488 A |
202 | static errno_t |
203 | ipf_input_detached(void *cookie, mbuf_t *data, int offset, u_int8_t protocol) | |
204 | { | |
205 | #pragma unused(cookie, data, offset, protocol) | |
206 | ||
207 | #if DEBUG | |
208 | printf("ipf_input_detached\n"); | |
209 | #endif /* DEBUG */ | |
210 | ||
0a7de745 | 211 | return 0; |
fe8ab488 A |
212 | } |
213 | ||
214 | static errno_t | |
215 | ipf_output_detached(void *cookie, mbuf_t *data, ipf_pktopts_t options) | |
216 | { | |
217 | #pragma unused(cookie, data, options) | |
218 | ||
219 | #if DEBUG | |
220 | printf("ipf_output_detached\n"); | |
221 | #endif /* DEBUG */ | |
222 | ||
0a7de745 | 223 | return 0; |
fe8ab488 A |
224 | } |
225 | ||
91447636 A |
226 | errno_t |
227 | ipf_remove( | |
228 | ipfilter_t filter_ref) | |
229 | { | |
0a7de745 | 230 | struct ipfilter *match = (struct ipfilter *)filter_ref; |
91447636 | 231 | struct ipfilter_list *head; |
5ba3f43e | 232 | |
0a7de745 A |
233 | if (match == 0 || (match->ipf_head != &ipv4_filters && match->ipf_head != &ipv6_filters)) { |
234 | return EINVAL; | |
235 | } | |
5ba3f43e | 236 | |
91447636 | 237 | head = match->ipf_head; |
5ba3f43e | 238 | |
91447636 A |
239 | lck_mtx_lock(kipf_lock); |
240 | TAILQ_FOREACH(match, head, ipf_link) { | |
5ba3f43e | 241 | if (match == (struct ipfilter *)filter_ref) { |
91447636 A |
242 | ipf_detach_func ipf_detach = match->ipf_filter.ipf_detach; |
243 | void* cookie = match->ipf_filter.cookie; | |
5ba3f43e | 244 | |
91447636 A |
245 | /* |
246 | * Cannot detach when they are filters running | |
247 | */ | |
248 | if (kipf_ref) { | |
249 | kipf_delayed_remove++; | |
250 | TAILQ_INSERT_TAIL(&tbr_filters, match, ipf_tbr); | |
fe8ab488 A |
251 | match->ipf_filter.ipf_input = ipf_input_detached; |
252 | match->ipf_filter.ipf_output = ipf_output_detached; | |
91447636 A |
253 | lck_mtx_unlock(kipf_lock); |
254 | } else { | |
5ba3f43e A |
255 | VERIFY(OSDecrementAtomic64(&net_api_stats.nas_ipf_add_count) > 0); |
256 | ||
91447636 A |
257 | TAILQ_REMOVE(head, match, ipf_link); |
258 | lck_mtx_unlock(kipf_lock); | |
5ba3f43e | 259 | |
0a7de745 | 260 | if (ipf_detach) { |
91447636 | 261 | ipf_detach(cookie); |
0a7de745 | 262 | } |
91447636 | 263 | FREE(match, M_IFADDR); |
b0d623f7 A |
264 | |
265 | /* This will force TCP to re-evaluate its use of TSO */ | |
266 | OSAddAtomic(-1, &kipf_count); | |
39236c6e | 267 | routegenid_update(); |
91447636 | 268 | } |
0a7de745 | 269 | return 0; |
91447636 A |
270 | } |
271 | } | |
272 | lck_mtx_unlock(kipf_lock); | |
5ba3f43e | 273 | |
0a7de745 | 274 | return ENOENT; |
91447636 A |
275 | } |
276 | ||
277 | int log_for_en1 = 0; | |
278 | ||
279 | errno_t | |
280 | ipf_inject_input( | |
281 | mbuf_t data, | |
282 | ipfilter_t filter_ref) | |
283 | { | |
5ba3f43e | 284 | struct mbuf *m = (struct mbuf *)data; |
91447636 A |
285 | struct m_tag *mtag = 0; |
286 | struct ip *ip = mtod(m, struct ip *); | |
d9a64523 | 287 | struct ip6_hdr *ip6; |
0a7de745 | 288 | u_int8_t vers; |
91447636 A |
289 | int hlen; |
290 | errno_t error = 0; | |
291 | protocol_family_t proto; | |
d9a64523 A |
292 | struct in_ifaddr *ia = NULL; |
293 | struct in_addr *pkt_dst = NULL; | |
294 | struct in6_ifaddr *ia6 = NULL; | |
295 | struct sockaddr_in6 pkt_dst6; | |
91447636 A |
296 | |
297 | vers = IP_VHL_V(ip->ip_vhl); | |
5ba3f43e | 298 | |
91447636 | 299 | switch (vers) { |
0a7de745 A |
300 | case 4: |
301 | proto = PF_INET; | |
302 | break; | |
303 | case 6: | |
304 | proto = PF_INET6; | |
305 | break; | |
306 | default: | |
307 | error = ENOTSUP; | |
308 | goto done; | |
91447636 | 309 | } |
5ba3f43e | 310 | |
91447636 | 311 | if (filter_ref == 0 && m->m_pkthdr.rcvif == 0) { |
d9a64523 A |
312 | /* |
313 | * Search for interface with the local address | |
314 | */ | |
315 | switch (proto) { | |
0a7de745 A |
316 | case PF_INET: |
317 | pkt_dst = &ip->ip_dst; | |
318 | lck_rw_lock_shared(in_ifaddr_rwlock); | |
319 | TAILQ_FOREACH(ia, INADDR_HASH(pkt_dst->s_addr), ia_hash) { | |
320 | if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst->s_addr) { | |
321 | m->m_pkthdr.rcvif = ia->ia_ifp; | |
322 | break; | |
d9a64523 | 323 | } |
0a7de745 A |
324 | } |
325 | lck_rw_done(in_ifaddr_rwlock); | |
326 | break; | |
327 | ||
328 | case PF_INET6: | |
329 | ip6 = mtod(m, struct ip6_hdr *); | |
330 | pkt_dst6.sin6_addr = ip6->ip6_dst; | |
331 | lck_rw_lock_shared(&in6_ifaddr_rwlock); | |
eb6b6ca3 | 332 | TAILQ_FOREACH(ia6, IN6ADDR_HASH(&pkt_dst6.sin6_addr), ia6_hash) { |
0a7de745 A |
333 | if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &pkt_dst6.sin6_addr)) { |
334 | m->m_pkthdr.rcvif = ia6->ia_ifp; | |
335 | break; | |
d9a64523 | 336 | } |
0a7de745 A |
337 | } |
338 | lck_rw_done(&in6_ifaddr_rwlock); | |
339 | break; | |
d9a64523 | 340 | |
0a7de745 A |
341 | default: |
342 | break; | |
d9a64523 A |
343 | } |
344 | ||
345 | /* | |
346 | * If none found, fallback to loopback | |
347 | */ | |
348 | if (m->m_pkthdr.rcvif == NULL) { | |
349 | m->m_pkthdr.rcvif = lo_ifp; | |
350 | } | |
351 | ||
91447636 A |
352 | m->m_pkthdr.csum_data = 0; |
353 | m->m_pkthdr.csum_flags = 0; | |
354 | if (vers == 4) { | |
355 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
356 | ip->ip_sum = 0; | |
357 | ip->ip_sum = in_cksum(m, hlen); | |
358 | } | |
359 | } | |
360 | if (filter_ref != 0) { | |
6d2010ae | 361 | mtag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_IPFILT, |
0a7de745 | 362 | sizeof(ipfilter_t), M_NOWAIT, m); |
91447636 A |
363 | if (mtag == NULL) { |
364 | error = ENOMEM; | |
365 | goto done; | |
5ba3f43e | 366 | } |
0a7de745 | 367 | *(ipfilter_t *)(mtag + 1) = filter_ref; |
91447636 A |
368 | m_tag_prepend(m, mtag); |
369 | } | |
5ba3f43e | 370 | |
91447636 A |
371 | error = proto_inject(proto, data); |
372 | ||
373 | done: | |
0a7de745 | 374 | return error; |
91447636 A |
375 | } |
376 | ||
377 | static errno_t | |
6d2010ae | 378 | ipf_injectv4_out(mbuf_t data, ipfilter_t filter_ref, ipf_pktopts_t options) |
91447636 A |
379 | { |
380 | struct route ro; | |
5ba3f43e A |
381 | struct ip *ip; |
382 | struct mbuf *m = (struct mbuf *)data; | |
91447636 | 383 | errno_t error = 0; |
6d2010ae A |
384 | struct m_tag *mtag = NULL; |
385 | struct ip_moptions *imo = NULL; | |
a39ff7e2 A |
386 | struct ip_out_args ipoa; |
387 | ||
388 | bzero(&ipoa, sizeof(ipoa)); | |
389 | ipoa.ipoa_boundif = IFSCOPE_NONE; | |
390 | ipoa.ipoa_sotc = SO_TC_UNSPEC; | |
391 | ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC; | |
6d2010ae | 392 | |
91447636 | 393 | /* Make the IP header contiguous in the mbuf */ |
0a7de745 A |
394 | if ((size_t)m->m_len < sizeof(struct ip)) { |
395 | m = m_pullup(m, sizeof(struct ip)); | |
396 | if (m == NULL) { | |
397 | return ENOMEM; | |
398 | } | |
91447636 | 399 | } |
6d2010ae A |
400 | ip = (struct ip *)m_mtod(m); |
401 | ||
91447636 | 402 | if (filter_ref != 0) { |
6d2010ae | 403 | mtag = m_tag_create(KERNEL_MODULE_TAG_ID, |
0a7de745 | 404 | KERNEL_TAG_TYPE_IPFILT, sizeof(ipfilter_t), M_NOWAIT, m); |
91447636 A |
405 | if (mtag == NULL) { |
406 | m_freem(m); | |
0a7de745 | 407 | return ENOMEM; |
91447636 | 408 | } |
6d2010ae | 409 | *(ipfilter_t *)(mtag + 1) = filter_ref; |
91447636 A |
410 | m_tag_prepend(m, mtag); |
411 | } | |
6d2010ae A |
412 | |
413 | if (options != NULL && (options->ippo_flags & IPPOF_MCAST_OPTS) && | |
414 | (imo = ip_allocmoptions(M_DONTWAIT)) != NULL) { | |
91447636 A |
415 | imo->imo_multicast_ifp = options->ippo_mcast_ifnet; |
416 | imo->imo_multicast_ttl = options->ippo_mcast_ttl; | |
417 | imo->imo_multicast_loop = options->ippo_mcast_loop; | |
418 | } | |
6d2010ae | 419 | |
316670eb | 420 | if (options != NULL) { |
0a7de745 | 421 | if (options->ippo_flags & IPPOF_SELECT_SRCIF) { |
316670eb | 422 | ipoa.ipoa_flags |= IPOAF_SELECT_SRCIF; |
0a7de745 | 423 | } |
6d2010ae | 424 | if (options->ippo_flags & IPPOF_BOUND_IF) { |
316670eb | 425 | ipoa.ipoa_flags |= IPOAF_BOUND_IF; |
6d2010ae A |
426 | ipoa.ipoa_boundif = options->ippo_flags >> |
427 | IPPOF_SHIFT_IFSCOPE; | |
428 | } | |
0a7de745 | 429 | if (options->ippo_flags & IPPOF_NO_IFT_CELLULAR) { |
316670eb | 430 | ipoa.ipoa_flags |= IPOAF_NO_CELLULAR; |
0a7de745 A |
431 | } |
432 | if (options->ippo_flags & IPPOF_BOUND_SRCADDR) { | |
316670eb | 433 | ipoa.ipoa_flags |= IPOAF_BOUND_SRCADDR; |
0a7de745 A |
434 | } |
435 | if (options->ippo_flags & IPPOF_NO_IFF_EXPENSIVE) { | |
fe8ab488 | 436 | ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE; |
0a7de745 | 437 | } |
cb323159 A |
438 | if (options->ippo_flags & IPPOF_NO_IFF_CONSTRAINED) { |
439 | ipoa.ipoa_flags |= IPOAF_NO_CONSTRAINED; | |
440 | } | |
91447636 | 441 | } |
6d2010ae A |
442 | |
443 | bzero(&ro, sizeof(struct route)); | |
444 | ||
0c530ab8 | 445 | /* Put ip_len and ip_off in host byte order, ip_output expects that */ |
b0d623f7 A |
446 | |
447 | #if BYTE_ORDER != BIG_ENDIAN | |
0c530ab8 A |
448 | NTOHS(ip->ip_len); |
449 | NTOHS(ip->ip_off); | |
b0d623f7 A |
450 | #endif |
451 | ||
6d2010ae A |
452 | /* Send; enforce source interface selection via IP_OUTARGS flag */ |
453 | error = ip_output(m, NULL, &ro, | |
454 | IP_ALLOWBROADCAST | IP_RAWOUTPUT | IP_OUTARGS, imo, &ipoa); | |
455 | ||
91447636 | 456 | /* Release the route */ |
39236c6e | 457 | ROUTE_RELEASE(&ro); |
6d2010ae | 458 | |
0a7de745 | 459 | if (imo != NULL) { |
6d2010ae | 460 | IMO_REMREF(imo); |
0a7de745 | 461 | } |
6d2010ae | 462 | |
0a7de745 | 463 | return error; |
91447636 A |
464 | } |
465 | ||
2d21ac55 | 466 | #if INET6 |
91447636 | 467 | static errno_t |
6d2010ae | 468 | ipf_injectv6_out(mbuf_t data, ipfilter_t filter_ref, ipf_pktopts_t options) |
91447636 A |
469 | { |
470 | struct route_in6 ro; | |
5ba3f43e A |
471 | struct ip6_hdr *ip6; |
472 | struct mbuf *m = (struct mbuf *)data; | |
91447636 | 473 | errno_t error = 0; |
6d2010ae A |
474 | struct m_tag *mtag = NULL; |
475 | struct ip6_moptions *im6o = NULL; | |
a39ff7e2 A |
476 | struct ip6_out_args ip6oa; |
477 | ||
478 | bzero(&ip6oa, sizeof(ip6oa)); | |
479 | ip6oa.ip6oa_boundif = IFSCOPE_NONE; | |
480 | ip6oa.ip6oa_sotc = SO_TC_UNSPEC; | |
481 | ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC; | |
6d2010ae | 482 | |
91447636 A |
483 | /* Make the IP header contiguous in the mbuf */ |
484 | if ((size_t)m->m_len < sizeof(struct ip6_hdr)) { | |
485 | m = m_pullup(m, sizeof(struct ip6_hdr)); | |
0a7de745 A |
486 | if (m == NULL) { |
487 | return ENOMEM; | |
488 | } | |
91447636 | 489 | } |
5ba3f43e | 490 | ip6 = (struct ip6_hdr *)m_mtod(m); |
91447636 A |
491 | |
492 | if (filter_ref != 0) { | |
6d2010ae | 493 | mtag = m_tag_create(KERNEL_MODULE_TAG_ID, |
0a7de745 | 494 | KERNEL_TAG_TYPE_IPFILT, sizeof(ipfilter_t), M_NOWAIT, m); |
91447636 A |
495 | if (mtag == NULL) { |
496 | m_freem(m); | |
0a7de745 | 497 | return ENOMEM; |
91447636 | 498 | } |
6d2010ae | 499 | *(ipfilter_t *)(mtag + 1) = filter_ref; |
91447636 A |
500 | m_tag_prepend(m, mtag); |
501 | } | |
6d2010ae A |
502 | |
503 | if (options != NULL && (options->ippo_flags & IPPOF_MCAST_OPTS) && | |
504 | (im6o = ip6_allocmoptions(M_DONTWAIT)) != NULL) { | |
91447636 A |
505 | im6o->im6o_multicast_ifp = options->ippo_mcast_ifnet; |
506 | im6o->im6o_multicast_hlim = options->ippo_mcast_ttl; | |
507 | im6o->im6o_multicast_loop = options->ippo_mcast_loop; | |
508 | } | |
6d2010ae | 509 | |
316670eb | 510 | if (options != NULL) { |
0a7de745 | 511 | if (options->ippo_flags & IPPOF_SELECT_SRCIF) { |
316670eb | 512 | ip6oa.ip6oa_flags |= IP6OAF_SELECT_SRCIF; |
0a7de745 | 513 | } |
6d2010ae | 514 | if (options->ippo_flags & IPPOF_BOUND_IF) { |
316670eb | 515 | ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF; |
6d2010ae A |
516 | ip6oa.ip6oa_boundif = options->ippo_flags >> |
517 | IPPOF_SHIFT_IFSCOPE; | |
518 | } | |
0a7de745 | 519 | if (options->ippo_flags & IPPOF_NO_IFT_CELLULAR) { |
316670eb | 520 | ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR; |
0a7de745 A |
521 | } |
522 | if (options->ippo_flags & IPPOF_BOUND_SRCADDR) { | |
316670eb | 523 | ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR; |
0a7de745 A |
524 | } |
525 | if (options->ippo_flags & IPPOF_NO_IFF_EXPENSIVE) { | |
fe8ab488 | 526 | ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE; |
0a7de745 | 527 | } |
cb323159 A |
528 | if (options->ippo_flags & IPPOF_NO_IFF_CONSTRAINED) { |
529 | ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED; | |
530 | } | |
91447636 | 531 | } |
6d2010ae A |
532 | |
533 | bzero(&ro, sizeof(struct route_in6)); | |
534 | ||
535 | /* | |
536 | * Send mbuf and ifscope information. Check for correctness | |
5ba3f43e | 537 | * of ifscope information is done while searching for a route in |
6d2010ae A |
538 | * ip6_output. |
539 | */ | |
540 | error = ip6_output(m, NULL, &ro, IPV6_OUTARGS, im6o, NULL, &ip6oa); | |
541 | ||
91447636 | 542 | /* Release the route */ |
39236c6e | 543 | ROUTE_RELEASE(&ro); |
6d2010ae | 544 | |
0a7de745 | 545 | if (im6o != NULL) { |
6d2010ae | 546 | IM6O_REMREF(im6o); |
0a7de745 | 547 | } |
6d2010ae | 548 | |
0a7de745 | 549 | return error; |
91447636 | 550 | } |
2d21ac55 | 551 | #endif /* INET6 */ |
91447636 A |
552 | |
553 | errno_t | |
554 | ipf_inject_output( | |
555 | mbuf_t data, | |
556 | ipfilter_t filter_ref, | |
557 | ipf_pktopts_t options) | |
558 | { | |
0a7de745 A |
559 | struct mbuf *m = (struct mbuf *)data; |
560 | u_int8_t vers; | |
561 | errno_t error = 0; | |
91447636 A |
562 | |
563 | /* Make one byte of the header contiguous in the mbuf */ | |
564 | if (m->m_len < 1) { | |
565 | m = m_pullup(m, 1); | |
0a7de745 | 566 | if (m == NULL) { |
91447636 | 567 | goto done; |
0a7de745 | 568 | } |
91447636 | 569 | } |
5ba3f43e A |
570 | |
571 | vers = (*(u_int8_t *)m_mtod(m)) >> 4; | |
572 | switch (vers) { | |
0a7de745 A |
573 | case 4: |
574 | error = ipf_injectv4_out(data, filter_ref, options); | |
575 | break; | |
2d21ac55 | 576 | #if INET6 |
0a7de745 A |
577 | case 6: |
578 | error = ipf_injectv6_out(data, filter_ref, options); | |
579 | break; | |
2d21ac55 | 580 | #endif |
0a7de745 A |
581 | default: |
582 | m_freem(m); | |
583 | error = ENOTSUP; | |
584 | break; | |
91447636 A |
585 | } |
586 | ||
5ba3f43e | 587 | done: |
0a7de745 | 588 | return error; |
91447636 A |
589 | } |
590 | ||
591 | __private_extern__ ipfilter_t | |
592 | ipf_get_inject_filter(struct mbuf *m) | |
593 | { | |
594 | ipfilter_t filter_ref = 0; | |
595 | struct m_tag *mtag; | |
5ba3f43e | 596 | |
91447636 A |
597 | mtag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_IPFILT, NULL); |
598 | if (mtag) { | |
0a7de745 | 599 | filter_ref = *(ipfilter_t *)(mtag + 1); |
5ba3f43e | 600 | |
91447636 A |
601 | m_tag_delete(m, mtag); |
602 | } | |
0a7de745 | 603 | return filter_ref; |
91447636 A |
604 | } |
605 | ||
606 | __private_extern__ int | |
607 | ipf_init(void) | |
608 | { | |
609 | int error = 0; | |
610 | lck_grp_attr_t *grp_attributes = 0; | |
611 | lck_attr_t *lck_attributes = 0; | |
612 | lck_grp_t *lck_grp = 0; | |
5ba3f43e | 613 | |
91447636 A |
614 | grp_attributes = lck_grp_attr_alloc_init(); |
615 | if (grp_attributes == 0) { | |
616 | printf("ipf_init: lck_grp_attr_alloc_init failed\n"); | |
617 | error = ENOMEM; | |
618 | goto done; | |
619 | } | |
5ba3f43e | 620 | |
91447636 A |
621 | lck_grp = lck_grp_alloc_init("IP Filter", grp_attributes); |
622 | if (lck_grp == 0) { | |
623 | printf("ipf_init: lck_grp_alloc_init failed\n"); | |
624 | error = ENOMEM; | |
625 | goto done; | |
626 | } | |
5ba3f43e | 627 | |
91447636 A |
628 | lck_attributes = lck_attr_alloc_init(); |
629 | if (lck_attributes == 0) { | |
630 | printf("ipf_init: lck_attr_alloc_init failed\n"); | |
631 | error = ENOMEM; | |
632 | goto done; | |
633 | } | |
5ba3f43e | 634 | |
316670eb A |
635 | lck_mtx_init(kipf_lock, lck_grp, lck_attributes); |
636 | ||
0a7de745 | 637 | done: |
91447636 A |
638 | if (lck_grp) { |
639 | lck_grp_free(lck_grp); | |
640 | lck_grp = 0; | |
641 | } | |
642 | if (grp_attributes) { | |
643 | lck_grp_attr_free(grp_attributes); | |
644 | grp_attributes = 0; | |
645 | } | |
646 | if (lck_attributes) { | |
647 | lck_attr_free(lck_attributes); | |
648 | lck_attributes = 0; | |
649 | } | |
5ba3f43e | 650 | |
0a7de745 | 651 | return error; |
91447636 | 652 | } |