2 * Copyright (c) 2007-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $apfw: git commit b6bf13f8321283cd7ee82b1795e86506084b1b95 $ */
30 /* $OpenBSD: pf_ioctl.c,v 1.175 2007/02/26 22:47:43 deraadt Exp $ */
33 * Copyright (c) 2001 Daniel Hartmeier
34 * Copyright (c) 2002,2003 Henning Brauer
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * - Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * - Redistributions in binary form must reproduce the above
44 * copyright notice, this list of conditions and the following
45 * disclaimer in the documentation and/or other materials provided
46 * with the distribution.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
61 * Effort sponsored in part by the Defense Advanced Research Projects
62 * Agency (DARPA) and Air Force Research Laboratory, Air Force
63 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
67 #include <machine/endian.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/filio.h>
72 #include <sys/fcntl.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/kernel.h>
77 #include <sys/proc_internal.h>
78 #include <sys/malloc.h>
79 #include <sys/kauth.h>
81 #include <sys/mcache.h>
82 #include <sys/queue.h>
85 #include <mach/vm_param.h>
89 #include <net/if_types.h>
90 #include <net/net_api_stats.h>
91 #include <net/route.h>
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/ip_icmp.h>
99 #include <netinet/if_ether.h>
102 #include <netinet/ip_dummynet.h>
105 #endif /* DUMMYNET */
107 #include <libkern/crypto/md5.h>
109 #include <machine/machine_routines.h>
111 #include <miscfs/devfs/devfs.h>
113 #include <net/pfvar.h>
116 #include <net/if_pfsync.h>
120 #include <net/if_pflog.h>
124 #include <netinet/ip6.h>
125 #include <netinet/in_pcb.h>
128 #include <dev/random/randomdev.h>
131 static void pfdetach(void);
133 static int pfopen(dev_t
, int, int, struct proc
*);
134 static int pfclose(dev_t
, int, int, struct proc
*);
135 static int pfioctl(dev_t
, u_long
, caddr_t
, int, struct proc
*);
136 static int pfioctl_ioc_table(u_long
, struct pfioc_table_32
*,
137 struct pfioc_table_64
*, struct proc
*);
138 static int pfioctl_ioc_tokens(u_long
, struct pfioc_tokens_32
*,
139 struct pfioc_tokens_64
*, struct proc
*);
140 static int pfioctl_ioc_rule(u_long
, int, struct pfioc_rule
*, struct proc
*);
141 static int pfioctl_ioc_state_kill(u_long
, struct pfioc_state_kill
*,
143 static int pfioctl_ioc_state(u_long
, struct pfioc_state
*, struct proc
*);
144 static int pfioctl_ioc_states(u_long
, struct pfioc_states_32
*,
145 struct pfioc_states_64
*, struct proc
*);
146 static int pfioctl_ioc_natlook(u_long
, struct pfioc_natlook
*, struct proc
*);
147 static int pfioctl_ioc_tm(u_long
, struct pfioc_tm
*, struct proc
*);
148 static int pfioctl_ioc_limit(u_long
, struct pfioc_limit
*, struct proc
*);
149 static int pfioctl_ioc_pooladdr(u_long
, struct pfioc_pooladdr
*, struct proc
*);
150 static int pfioctl_ioc_ruleset(u_long
, struct pfioc_ruleset
*, struct proc
*);
151 static int pfioctl_ioc_trans(u_long
, struct pfioc_trans_32
*,
152 struct pfioc_trans_64
*, struct proc
*);
153 static int pfioctl_ioc_src_nodes(u_long
, struct pfioc_src_nodes_32
*,
154 struct pfioc_src_nodes_64
*, struct proc
*);
155 static int pfioctl_ioc_src_node_kill(u_long
, struct pfioc_src_node_kill
*,
157 static int pfioctl_ioc_iface(u_long
, struct pfioc_iface_32
*,
158 struct pfioc_iface_64
*, struct proc
*);
159 static struct pf_pool
*pf_get_pool(char *, u_int32_t
, u_int8_t
, u_int32_t
,
160 u_int8_t
, u_int8_t
, u_int8_t
);
161 static void pf_mv_pool(struct pf_palist
*, struct pf_palist
*);
162 static void pf_empty_pool(struct pf_palist
*);
163 static int pf_begin_rules(u_int32_t
*, int, const char *);
164 static int pf_rollback_rules(u_int32_t
, int, char *);
165 static int pf_setup_pfsync_matching(struct pf_ruleset
*);
166 static void pf_hash_rule(MD5_CTX
*, struct pf_rule
*);
167 static void pf_hash_rule_addr(MD5_CTX
*, struct pf_rule_addr
*, u_int8_t
);
168 static int pf_commit_rules(u_int32_t
, int, char *);
169 static void pf_rule_copyin(struct pf_rule
*, struct pf_rule
*, struct proc
*,
171 static void pf_rule_copyout(struct pf_rule
*, struct pf_rule
*);
172 static void pf_state_export(struct pfsync_state
*, struct pf_state_key
*,
174 static void pf_state_import(struct pfsync_state
*, struct pf_state_key
*,
176 static void pf_pooladdr_copyin(struct pf_pooladdr
*, struct pf_pooladdr
*);
177 static void pf_pooladdr_copyout(struct pf_pooladdr
*, struct pf_pooladdr
*);
178 static void pf_expire_states_and_src_nodes(struct pf_rule
*);
179 static void pf_delete_rule_from_ruleset(struct pf_ruleset
*,
180 int, struct pf_rule
*);
181 static void pf_addrwrap_setup(struct pf_addr_wrap
*);
182 static int pf_rule_setup(struct pfioc_rule
*, struct pf_rule
*,
183 struct pf_ruleset
*);
184 static void pf_delete_rule_by_owner(char *, u_int32_t
);
185 static int pf_delete_rule_by_ticket(struct pfioc_rule
*, u_int32_t
);
186 static void pf_ruleset_cleanup(struct pf_ruleset
*, int);
187 static void pf_deleterule_anchor_step_out(struct pf_ruleset
**,
188 int, struct pf_rule
**);
190 #define PF_CDEV_MAJOR (-1)
192 static struct cdevsw pf_cdevsw
= {
196 .d_write
= eno_rdwrt
,
199 .d_reset
= eno_reset
,
201 .d_select
= eno_select
,
203 .d_strategy
= eno_strat
,
204 .d_reserved_1
= eno_getc
,
205 .d_reserved_2
= eno_putc
,
209 static void pf_attach_hooks(void);
211 /* currently unused along with pfdetach() */
212 static void pf_detach_hooks(void);
216 * This is set during DIOCSTART/DIOCSTOP with pf_perim_lock held as writer,
217 * and used in pf_af_hook() for performance optimization, such that packets
218 * will enter pf_test() or pf_test6() only when PF is running.
220 int pf_is_enabled
= 0;
222 u_int32_t pf_hash_seed
;
223 int16_t pf_nat64_configured
= 0;
226 * These are the pf enabled reference counting variables
228 #define NR_TOKENS_LIMIT (INT_MAX / sizeof(struct pfioc_token))
230 static u_int64_t pf_enabled_ref_count
;
231 static u_int32_t nr_tokens
= 0;
232 static u_int64_t pffwrules
;
233 static u_int32_t pfdevcnt
;
235 SLIST_HEAD(list_head
, pfioc_kernel_token
);
236 static struct list_head token_list_head
;
238 struct pf_rule pf_default_rule
;
240 #define TAGID_MAX 50000
241 static TAILQ_HEAD(pf_tags
, pf_tagname
) pf_tags
=
242 TAILQ_HEAD_INITIALIZER(pf_tags
);
244 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
245 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
247 static u_int16_t
tagname2tag(struct pf_tags
*, char *);
248 static void tag2tagname(struct pf_tags
*, u_int16_t
, char *);
249 static void tag_unref(struct pf_tags
*, u_int16_t
);
250 static int pf_rtlabel_add(struct pf_addr_wrap
*);
251 static void pf_rtlabel_remove(struct pf_addr_wrap
*);
252 static void pf_rtlabel_copyout(struct pf_addr_wrap
*);
255 static int pf_inet_hook(struct ifnet
*, struct mbuf
**, int,
256 struct ip_fw_args
*);
259 static int pf_inet6_hook(struct ifnet
*, struct mbuf
**, int,
260 struct ip_fw_args
*);
263 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
266 * Helper macros for ioctl structures which vary in size (32-bit vs. 64-bit)
268 #define PFIOCX_STRUCT_DECL(s) \
271 struct s##_32 _s##_32; \
272 struct s##_64 _s##_64; \
276 #define PFIOCX_STRUCT_BEGIN(a, s, _action) { \
277 VERIFY(s##_un == NULL); \
278 s##_un = _MALLOC(sizeof (*s##_un), M_TEMP, M_WAITOK|M_ZERO); \
279 if (s##_un == NULL) { \
283 bcopy(a, &s##_un->_u._s##_64, \
284 sizeof (struct s##_64)); \
286 bcopy(a, &s##_un->_u._s##_32, \
287 sizeof (struct s##_32)); \
291 #define PFIOCX_STRUCT_END(s, a) { \
292 VERIFY(s##_un != NULL); \
294 bcopy(&s##_un->_u._s##_64, a, sizeof (struct s##_64)); \
296 bcopy(&s##_un->_u._s##_32, a, sizeof (struct s##_32)); \
297 _FREE(s##_un, M_TEMP); \
301 #define PFIOCX_STRUCT_ADDR32(s) (&s##_un->_u._s##_32)
302 #define PFIOCX_STRUCT_ADDR64(s) (&s##_un->_u._s##_64)
305 * Helper macros for regular ioctl structures.
307 #define PFIOC_STRUCT_BEGIN(a, v, _action) { \
308 VERIFY((v) == NULL); \
309 (v) = _MALLOC(sizeof (*(v)), M_TEMP, M_WAITOK|M_ZERO); \
313 bcopy(a, v, sizeof (*(v))); \
317 #define PFIOC_STRUCT_END(v, a) { \
318 VERIFY((v) != NULL); \
319 bcopy(v, a, sizeof (*(v))); \
324 #define PFIOC_STRUCT_ADDR32(s) (&s##_un->_u._s##_32)
325 #define PFIOC_STRUCT_ADDR64(s) (&s##_un->_u._s##_64)
327 static lck_attr_t
*pf_perim_lock_attr
;
328 static lck_grp_t
*pf_perim_lock_grp
;
329 static lck_grp_attr_t
*pf_perim_lock_grp_attr
;
331 static lck_attr_t
*pf_lock_attr
;
332 static lck_grp_t
*pf_lock_grp
;
333 static lck_grp_attr_t
*pf_lock_grp_attr
;
335 struct thread
*pf_purge_thread
;
337 extern void pfi_kifaddr_update(void *);
339 /* pf enable ref-counting helper functions */
340 static u_int64_t
generate_token(struct proc
*);
341 static int remove_token(struct pfioc_remove_token
*);
342 static void invalidate_all_tokens(void);
345 generate_token(struct proc
*p
)
347 u_int64_t token_value
;
348 struct pfioc_kernel_token
*new_token
;
350 if (nr_tokens
+ 1 > NR_TOKENS_LIMIT
) {
351 os_log_error(OS_LOG_DEFAULT
, "%s: NR_TOKENS_LIMIT reached", __func__
);
355 new_token
= _MALLOC(sizeof(struct pfioc_kernel_token
), M_TEMP
,
358 LCK_MTX_ASSERT(pf_lock
, LCK_MTX_ASSERT_OWNED
);
360 if (new_token
== NULL
) {
361 /* malloc failed! bail! */
362 os_log_error(OS_LOG_DEFAULT
, "%s: unable to allocate pf token structure!", __func__
);
366 token_value
= VM_KERNEL_ADDRPERM((u_int64_t
)(uintptr_t)new_token
);
368 new_token
->token
.token_value
= token_value
;
369 new_token
->token
.pid
= proc_pid(p
);
370 proc_name(new_token
->token
.pid
, new_token
->token
.proc_name
,
371 sizeof(new_token
->token
.proc_name
));
372 new_token
->token
.timestamp
= pf_calendar_time_second();
374 SLIST_INSERT_HEAD(&token_list_head
, new_token
, next
);
381 remove_token(struct pfioc_remove_token
*tok
)
383 struct pfioc_kernel_token
*entry
, *tmp
;
385 LCK_MTX_ASSERT(pf_lock
, LCK_MTX_ASSERT_OWNED
);
387 SLIST_FOREACH_SAFE(entry
, &token_list_head
, next
, tmp
) {
388 if (tok
->token_value
== entry
->token
.token_value
) {
389 SLIST_REMOVE(&token_list_head
, entry
,
390 pfioc_kernel_token
, next
);
391 _FREE(entry
, M_TEMP
);
393 return 0; /* success */
397 printf("pf : remove failure\n");
398 return ESRCH
; /* failure */
402 invalidate_all_tokens(void)
404 struct pfioc_kernel_token
*entry
, *tmp
;
406 LCK_MTX_ASSERT(pf_lock
, LCK_MTX_ASSERT_OWNED
);
408 SLIST_FOREACH_SAFE(entry
, &token_list_head
, next
, tmp
) {
409 SLIST_REMOVE(&token_list_head
, entry
, pfioc_kernel_token
, next
);
410 _FREE(entry
, M_TEMP
);
419 u_int32_t
*t
= pf_default_rule
.timeout
;
422 pf_perim_lock_grp_attr
= lck_grp_attr_alloc_init();
423 pf_perim_lock_grp
= lck_grp_alloc_init("pf_perim",
424 pf_perim_lock_grp_attr
);
425 pf_perim_lock_attr
= lck_attr_alloc_init();
426 lck_rw_init(pf_perim_lock
, pf_perim_lock_grp
, pf_perim_lock_attr
);
428 pf_lock_grp_attr
= lck_grp_attr_alloc_init();
429 pf_lock_grp
= lck_grp_alloc_init("pf", pf_lock_grp_attr
);
430 pf_lock_attr
= lck_attr_alloc_init();
431 lck_mtx_init(pf_lock
, pf_lock_grp
, pf_lock_attr
);
433 pool_init(&pf_rule_pl
, sizeof(struct pf_rule
), 0, 0, 0, "pfrulepl",
435 pool_init(&pf_src_tree_pl
, sizeof(struct pf_src_node
), 0, 0, 0,
437 pool_init(&pf_state_pl
, sizeof(struct pf_state
), 0, 0, 0, "pfstatepl",
439 pool_init(&pf_state_key_pl
, sizeof(struct pf_state_key
), 0, 0, 0,
440 "pfstatekeypl", NULL
);
441 pool_init(&pf_app_state_pl
, sizeof(struct pf_app_state
), 0, 0, 0,
442 "pfappstatepl", NULL
);
443 pool_init(&pf_pooladdr_pl
, sizeof(struct pf_pooladdr
), 0, 0, 0,
444 "pfpooladdrpl", NULL
);
447 pf_osfp_initialize();
449 pool_sethardlimit(pf_pool_limits
[PF_LIMIT_STATES
].pp
,
450 pf_pool_limits
[PF_LIMIT_STATES
].limit
, NULL
, 0);
452 if (max_mem
<= 256 * 1024 * 1024) {
453 pf_pool_limits
[PF_LIMIT_TABLE_ENTRIES
].limit
=
454 PFR_KENTRY_HIWAT_SMALL
;
457 RB_INIT(&tree_src_tracking
);
458 RB_INIT(&pf_anchors
);
459 pf_init_ruleset(&pf_main_ruleset
);
460 TAILQ_INIT(&pf_pabuf
);
461 TAILQ_INIT(&state_list
);
463 _CASSERT((SC_BE
& SCIDX_MASK
) == SCIDX_BE
);
464 _CASSERT((SC_BK_SYS
& SCIDX_MASK
) == SCIDX_BK_SYS
);
465 _CASSERT((SC_BK
& SCIDX_MASK
) == SCIDX_BK
);
466 _CASSERT((SC_RD
& SCIDX_MASK
) == SCIDX_RD
);
467 _CASSERT((SC_OAM
& SCIDX_MASK
) == SCIDX_OAM
);
468 _CASSERT((SC_AV
& SCIDX_MASK
) == SCIDX_AV
);
469 _CASSERT((SC_RV
& SCIDX_MASK
) == SCIDX_RV
);
470 _CASSERT((SC_VI
& SCIDX_MASK
) == SCIDX_VI
);
471 _CASSERT((SC_SIG
& SCIDX_MASK
) == SCIDX_SIG
);
472 _CASSERT((SC_VO
& SCIDX_MASK
) == SCIDX_VO
);
473 _CASSERT((SC_CTL
& SCIDX_MASK
) == SCIDX_CTL
);
475 /* default rule should never be garbage collected */
476 pf_default_rule
.entries
.tqe_prev
= &pf_default_rule
.entries
.tqe_next
;
477 pf_default_rule
.action
= PF_PASS
;
478 pf_default_rule
.nr
= -1;
479 pf_default_rule
.rtableid
= IFSCOPE_NONE
;
481 /* initialize default timeouts */
482 t
[PFTM_TCP_FIRST_PACKET
] = PFTM_TCP_FIRST_PACKET_VAL
;
483 t
[PFTM_TCP_OPENING
] = PFTM_TCP_OPENING_VAL
;
484 t
[PFTM_TCP_ESTABLISHED
] = PFTM_TCP_ESTABLISHED_VAL
;
485 t
[PFTM_TCP_CLOSING
] = PFTM_TCP_CLOSING_VAL
;
486 t
[PFTM_TCP_FIN_WAIT
] = PFTM_TCP_FIN_WAIT_VAL
;
487 t
[PFTM_TCP_CLOSED
] = PFTM_TCP_CLOSED_VAL
;
488 t
[PFTM_UDP_FIRST_PACKET
] = PFTM_UDP_FIRST_PACKET_VAL
;
489 t
[PFTM_UDP_SINGLE
] = PFTM_UDP_SINGLE_VAL
;
490 t
[PFTM_UDP_MULTIPLE
] = PFTM_UDP_MULTIPLE_VAL
;
491 t
[PFTM_ICMP_FIRST_PACKET
] = PFTM_ICMP_FIRST_PACKET_VAL
;
492 t
[PFTM_ICMP_ERROR_REPLY
] = PFTM_ICMP_ERROR_REPLY_VAL
;
493 t
[PFTM_GREv1_FIRST_PACKET
] = PFTM_GREv1_FIRST_PACKET_VAL
;
494 t
[PFTM_GREv1_INITIATING
] = PFTM_GREv1_INITIATING_VAL
;
495 t
[PFTM_GREv1_ESTABLISHED
] = PFTM_GREv1_ESTABLISHED_VAL
;
496 t
[PFTM_ESP_FIRST_PACKET
] = PFTM_ESP_FIRST_PACKET_VAL
;
497 t
[PFTM_ESP_INITIATING
] = PFTM_ESP_INITIATING_VAL
;
498 t
[PFTM_ESP_ESTABLISHED
] = PFTM_ESP_ESTABLISHED_VAL
;
499 t
[PFTM_OTHER_FIRST_PACKET
] = PFTM_OTHER_FIRST_PACKET_VAL
;
500 t
[PFTM_OTHER_SINGLE
] = PFTM_OTHER_SINGLE_VAL
;
501 t
[PFTM_OTHER_MULTIPLE
] = PFTM_OTHER_MULTIPLE_VAL
;
502 t
[PFTM_FRAG
] = PFTM_FRAG_VAL
;
503 t
[PFTM_INTERVAL
] = PFTM_INTERVAL_VAL
;
504 t
[PFTM_SRC_NODE
] = PFTM_SRC_NODE_VAL
;
505 t
[PFTM_TS_DIFF
] = PFTM_TS_DIFF_VAL
;
506 t
[PFTM_ADAPTIVE_START
] = PFSTATE_ADAPT_START
;
507 t
[PFTM_ADAPTIVE_END
] = PFSTATE_ADAPT_END
;
510 bzero(&pf_status
, sizeof(pf_status
));
511 pf_status
.debug
= PF_DEBUG_URGENT
;
512 pf_hash_seed
= RandomULong();
514 /* XXX do our best to avoid a conflict */
515 pf_status
.hostid
= random();
517 if (kernel_thread_start(pf_purge_thread_fn
, NULL
,
518 &pf_purge_thread
) != 0) {
519 printf("%s: unable to start purge thread!", __func__
);
523 maj
= cdevsw_add(PF_CDEV_MAJOR
, &pf_cdevsw
);
525 printf("%s: failed to allocate major number!\n", __func__
);
528 (void) devfs_make_node(makedev(maj
, PFDEV_PF
), DEVFS_CHAR
,
529 UID_ROOT
, GID_WHEEL
, 0600, "pf", 0);
531 (void) devfs_make_node(makedev(maj
, PFDEV_PFM
), DEVFS_CHAR
,
532 UID_ROOT
, GID_WHEEL
, 0600, "pfm", 0);
544 struct pf_anchor
*anchor
;
545 struct pf_state
*state
;
546 struct pf_src_node
*node
;
547 struct pfioc_table pt
;
554 pf_status
.running
= 0;
555 wakeup(pf_purge_thread_fn
);
557 /* clear the rulesets */
558 for (i
= 0; i
< PF_RULESET_MAX
; i
++) {
559 if (pf_begin_rules(&ticket
, i
, &r
) == 0) {
560 pf_commit_rules(ticket
, i
, &r
);
565 RB_FOREACH(state
, pf_state_tree_id
, &tree_id
) {
566 state
->timeout
= PFTM_PURGE
;
568 state
->sync_flags
= PFSTATE_NOSYNC
;
571 pf_purge_expired_states(pf_status
.states
);
574 pfsync_clear_states(pf_status
.hostid
, NULL
);
577 /* clear source nodes */
578 RB_FOREACH(state
, pf_state_tree_id
, &tree_id
) {
579 state
->src_node
= NULL
;
580 state
->nat_src_node
= NULL
;
582 RB_FOREACH(node
, pf_src_tree
, &tree_src_tracking
) {
586 pf_purge_expired_src_nodes();
589 memset(&pt
, '\0', sizeof(pt
));
590 pfr_clr_tables(&pt
.pfrio_table
, &pt
.pfrio_ndel
, pt
.pfrio_flags
);
592 /* destroy anchors */
593 while ((anchor
= RB_MIN(pf_anchor_global
, &pf_anchors
)) != NULL
) {
594 for (i
= 0; i
< PF_RULESET_MAX
; i
++) {
595 if (pf_begin_rules(&ticket
, i
, anchor
->name
) == 0) {
596 pf_commit_rules(ticket
, i
, anchor
->name
);
601 /* destroy main ruleset */
602 pf_remove_if_empty_ruleset(&pf_main_ruleset
);
604 /* destroy the pools */
605 pool_destroy(&pf_pooladdr_pl
);
606 pool_destroy(&pf_state_pl
);
607 pool_destroy(&pf_rule_pl
);
608 pool_destroy(&pf_src_tree_pl
);
610 /* destroy subsystems */
611 pf_normalize_destroy();
619 pfopen(dev_t dev
, int flags
, int fmt
, struct proc
*p
)
621 #pragma unused(flags, fmt, p)
622 if (minor(dev
) >= PFDEV_MAX
) {
626 if (minor(dev
) == PFDEV_PFM
) {
627 lck_mtx_lock(pf_lock
);
629 lck_mtx_unlock(pf_lock
);
633 lck_mtx_unlock(pf_lock
);
639 pfclose(dev_t dev
, int flags
, int fmt
, struct proc
*p
)
641 #pragma unused(flags, fmt, p)
642 if (minor(dev
) >= PFDEV_MAX
) {
646 if (minor(dev
) == PFDEV_PFM
) {
647 lck_mtx_lock(pf_lock
);
648 VERIFY(pfdevcnt
> 0);
650 lck_mtx_unlock(pf_lock
);
655 static struct pf_pool
*
656 pf_get_pool(char *anchor
, u_int32_t ticket
, u_int8_t rule_action
,
657 u_int32_t rule_number
, u_int8_t r_last
, u_int8_t active
,
658 u_int8_t check_ticket
)
660 struct pf_ruleset
*ruleset
;
661 struct pf_rule
*rule
;
664 ruleset
= pf_find_ruleset(anchor
);
665 if (ruleset
== NULL
) {
668 rs_num
= pf_get_ruleset_number(rule_action
);
669 if (rs_num
>= PF_RULESET_MAX
) {
673 if (check_ticket
&& ticket
!=
674 ruleset
->rules
[rs_num
].active
.ticket
) {
678 rule
= TAILQ_LAST(ruleset
->rules
[rs_num
].active
.ptr
,
681 rule
= TAILQ_FIRST(ruleset
->rules
[rs_num
].active
.ptr
);
684 if (check_ticket
&& ticket
!=
685 ruleset
->rules
[rs_num
].inactive
.ticket
) {
689 rule
= TAILQ_LAST(ruleset
->rules
[rs_num
].inactive
.ptr
,
692 rule
= TAILQ_FIRST(ruleset
->rules
[rs_num
].inactive
.ptr
);
696 while ((rule
!= NULL
) && (rule
->nr
!= rule_number
)) {
697 rule
= TAILQ_NEXT(rule
, entries
);
708 pf_mv_pool(struct pf_palist
*poola
, struct pf_palist
*poolb
)
710 struct pf_pooladdr
*mv_pool_pa
;
712 while ((mv_pool_pa
= TAILQ_FIRST(poola
)) != NULL
) {
713 TAILQ_REMOVE(poola
, mv_pool_pa
, entries
);
714 TAILQ_INSERT_TAIL(poolb
, mv_pool_pa
, entries
);
719 pf_empty_pool(struct pf_palist
*poola
)
721 struct pf_pooladdr
*empty_pool_pa
;
723 while ((empty_pool_pa
= TAILQ_FIRST(poola
)) != NULL
) {
724 pfi_dynaddr_remove(&empty_pool_pa
->addr
);
725 pf_tbladdr_remove(&empty_pool_pa
->addr
);
726 pfi_kif_unref(empty_pool_pa
->kif
, PFI_KIF_REF_RULE
);
727 TAILQ_REMOVE(poola
, empty_pool_pa
, entries
);
728 pool_put(&pf_pooladdr_pl
, empty_pool_pa
);
733 pf_rm_rule(struct pf_rulequeue
*rulequeue
, struct pf_rule
*rule
)
735 if (rulequeue
!= NULL
) {
736 if (rule
->states
<= 0) {
738 * XXX - we need to remove the table *before* detaching
739 * the rule to make sure the table code does not delete
740 * the anchor under our feet.
742 pf_tbladdr_remove(&rule
->src
.addr
);
743 pf_tbladdr_remove(&rule
->dst
.addr
);
744 if (rule
->overload_tbl
) {
745 pfr_detach_table(rule
->overload_tbl
);
748 TAILQ_REMOVE(rulequeue
, rule
, entries
);
749 rule
->entries
.tqe_prev
= NULL
;
753 if (rule
->states
> 0 || rule
->src_nodes
> 0 ||
754 rule
->entries
.tqe_prev
!= NULL
) {
757 pf_tag_unref(rule
->tag
);
758 pf_tag_unref(rule
->match_tag
);
759 pf_rtlabel_remove(&rule
->src
.addr
);
760 pf_rtlabel_remove(&rule
->dst
.addr
);
761 pfi_dynaddr_remove(&rule
->src
.addr
);
762 pfi_dynaddr_remove(&rule
->dst
.addr
);
763 if (rulequeue
== NULL
) {
764 pf_tbladdr_remove(&rule
->src
.addr
);
765 pf_tbladdr_remove(&rule
->dst
.addr
);
766 if (rule
->overload_tbl
) {
767 pfr_detach_table(rule
->overload_tbl
);
770 pfi_kif_unref(rule
->kif
, PFI_KIF_REF_RULE
);
771 pf_anchor_remove(rule
);
772 pf_empty_pool(&rule
->rpool
.list
);
773 pool_put(&pf_rule_pl
, rule
);
777 tagname2tag(struct pf_tags
*head
, char *tagname
)
779 struct pf_tagname
*tag
, *p
= NULL
;
780 u_int16_t new_tagid
= 1;
782 TAILQ_FOREACH(tag
, head
, entries
)
783 if (strcmp(tagname
, tag
->name
) == 0) {
789 * to avoid fragmentation, we do a linear search from the beginning
790 * and take the first free slot we find. if there is none or the list
791 * is empty, append a new entry at the end.
795 if (!TAILQ_EMPTY(head
)) {
796 for (p
= TAILQ_FIRST(head
); p
!= NULL
&&
797 p
->tag
== new_tagid
; p
= TAILQ_NEXT(p
, entries
)) {
798 new_tagid
= p
->tag
+ 1;
802 if (new_tagid
> TAGID_MAX
) {
806 /* allocate and fill new struct pf_tagname */
807 tag
= _MALLOC(sizeof(*tag
), M_TEMP
, M_WAITOK
| M_ZERO
);
811 strlcpy(tag
->name
, tagname
, sizeof(tag
->name
));
812 tag
->tag
= new_tagid
;
815 if (p
!= NULL
) { /* insert new entry before p */
816 TAILQ_INSERT_BEFORE(p
, tag
, entries
);
817 } else { /* either list empty or no free slot in between */
818 TAILQ_INSERT_TAIL(head
, tag
, entries
);
825 tag2tagname(struct pf_tags
*head
, u_int16_t tagid
, char *p
)
827 struct pf_tagname
*tag
;
829 TAILQ_FOREACH(tag
, head
, entries
)
830 if (tag
->tag
== tagid
) {
831 strlcpy(p
, tag
->name
, PF_TAG_NAME_SIZE
);
837 tag_unref(struct pf_tags
*head
, u_int16_t tag
)
839 struct pf_tagname
*p
, *next
;
845 for (p
= TAILQ_FIRST(head
); p
!= NULL
; p
= next
) {
846 next
= TAILQ_NEXT(p
, entries
);
849 TAILQ_REMOVE(head
, p
, entries
);
858 pf_tagname2tag(char *tagname
)
860 return tagname2tag(&pf_tags
, tagname
);
864 pf_tag2tagname(u_int16_t tagid
, char *p
)
866 tag2tagname(&pf_tags
, tagid
, p
);
870 pf_tag_ref(u_int16_t tag
)
872 struct pf_tagname
*t
;
874 TAILQ_FOREACH(t
, &pf_tags
, entries
)
884 pf_tag_unref(u_int16_t tag
)
886 tag_unref(&pf_tags
, tag
);
890 pf_rtlabel_add(struct pf_addr_wrap
*a
)
897 pf_rtlabel_remove(struct pf_addr_wrap
*a
)
903 pf_rtlabel_copyout(struct pf_addr_wrap
*a
)
909 pf_begin_rules(u_int32_t
*ticket
, int rs_num
, const char *anchor
)
911 struct pf_ruleset
*rs
;
912 struct pf_rule
*rule
;
914 if (rs_num
< 0 || rs_num
>= PF_RULESET_MAX
) {
917 rs
= pf_find_or_create_ruleset(anchor
);
921 while ((rule
= TAILQ_FIRST(rs
->rules
[rs_num
].inactive
.ptr
)) != NULL
) {
922 pf_rm_rule(rs
->rules
[rs_num
].inactive
.ptr
, rule
);
923 rs
->rules
[rs_num
].inactive
.rcount
--;
925 *ticket
= ++rs
->rules
[rs_num
].inactive
.ticket
;
926 rs
->rules
[rs_num
].inactive
.open
= 1;
931 pf_rollback_rules(u_int32_t ticket
, int rs_num
, char *anchor
)
933 struct pf_ruleset
*rs
;
934 struct pf_rule
*rule
;
936 if (rs_num
< 0 || rs_num
>= PF_RULESET_MAX
) {
939 rs
= pf_find_ruleset(anchor
);
940 if (rs
== NULL
|| !rs
->rules
[rs_num
].inactive
.open
||
941 rs
->rules
[rs_num
].inactive
.ticket
!= ticket
) {
944 while ((rule
= TAILQ_FIRST(rs
->rules
[rs_num
].inactive
.ptr
)) != NULL
) {
945 pf_rm_rule(rs
->rules
[rs_num
].inactive
.ptr
, rule
);
946 rs
->rules
[rs_num
].inactive
.rcount
--;
948 rs
->rules
[rs_num
].inactive
.open
= 0;
952 #define PF_MD5_UPD(st, elm) \
953 MD5Update(ctx, (u_int8_t *)&(st)->elm, sizeof ((st)->elm))
955 #define PF_MD5_UPD_STR(st, elm) \
956 MD5Update(ctx, (u_int8_t *)(st)->elm, strlen((st)->elm))
958 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
959 (stor) = htonl((st)->elm); \
960 MD5Update(ctx, (u_int8_t *)&(stor), sizeof (u_int32_t)); \
963 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
964 (stor) = htons((st)->elm); \
965 MD5Update(ctx, (u_int8_t *)&(stor), sizeof (u_int16_t)); \
969 pf_hash_rule_addr(MD5_CTX
*ctx
, struct pf_rule_addr
*pfr
, u_int8_t proto
)
971 PF_MD5_UPD(pfr
, addr
.type
);
972 switch (pfr
->addr
.type
) {
973 case PF_ADDR_DYNIFTL
:
974 PF_MD5_UPD(pfr
, addr
.v
.ifname
);
975 PF_MD5_UPD(pfr
, addr
.iflags
);
978 PF_MD5_UPD(pfr
, addr
.v
.tblname
);
980 case PF_ADDR_ADDRMASK
:
982 PF_MD5_UPD(pfr
, addr
.v
.a
.addr
.addr32
);
983 PF_MD5_UPD(pfr
, addr
.v
.a
.mask
.addr32
);
985 case PF_ADDR_RTLABEL
:
986 PF_MD5_UPD(pfr
, addr
.v
.rtlabelname
);
993 PF_MD5_UPD(pfr
, xport
.range
.port
[0]);
994 PF_MD5_UPD(pfr
, xport
.range
.port
[1]);
995 PF_MD5_UPD(pfr
, xport
.range
.op
);
1002 PF_MD5_UPD(pfr
, neg
);
1006 pf_hash_rule(MD5_CTX
*ctx
, struct pf_rule
*rule
)
1011 pf_hash_rule_addr(ctx
, &rule
->src
, rule
->proto
);
1012 pf_hash_rule_addr(ctx
, &rule
->dst
, rule
->proto
);
1013 PF_MD5_UPD_STR(rule
, label
);
1014 PF_MD5_UPD_STR(rule
, ifname
);
1015 PF_MD5_UPD_STR(rule
, match_tagname
);
1016 PF_MD5_UPD_HTONS(rule
, match_tag
, x
); /* dup? */
1017 PF_MD5_UPD_HTONL(rule
, os_fingerprint
, y
);
1018 PF_MD5_UPD_HTONL(rule
, prob
, y
);
1019 PF_MD5_UPD_HTONL(rule
, uid
.uid
[0], y
);
1020 PF_MD5_UPD_HTONL(rule
, uid
.uid
[1], y
);
1021 PF_MD5_UPD(rule
, uid
.op
);
1022 PF_MD5_UPD_HTONL(rule
, gid
.gid
[0], y
);
1023 PF_MD5_UPD_HTONL(rule
, gid
.gid
[1], y
);
1024 PF_MD5_UPD(rule
, gid
.op
);
1025 PF_MD5_UPD_HTONL(rule
, rule_flag
, y
);
1026 PF_MD5_UPD(rule
, action
);
1027 PF_MD5_UPD(rule
, direction
);
1028 PF_MD5_UPD(rule
, af
);
1029 PF_MD5_UPD(rule
, quick
);
1030 PF_MD5_UPD(rule
, ifnot
);
1031 PF_MD5_UPD(rule
, match_tag_not
);
1032 PF_MD5_UPD(rule
, natpass
);
1033 PF_MD5_UPD(rule
, keep_state
);
1034 PF_MD5_UPD(rule
, proto
);
1035 PF_MD5_UPD(rule
, type
);
1036 PF_MD5_UPD(rule
, code
);
1037 PF_MD5_UPD(rule
, flags
);
1038 PF_MD5_UPD(rule
, flagset
);
1039 PF_MD5_UPD(rule
, allow_opts
);
1040 PF_MD5_UPD(rule
, rt
);
1041 PF_MD5_UPD(rule
, tos
);
1045 pf_commit_rules(u_int32_t ticket
, int rs_num
, char *anchor
)
1047 struct pf_ruleset
*rs
;
1048 struct pf_rule
*rule
, **old_array
, *r
;
1049 struct pf_rulequeue
*old_rules
;
1051 u_int32_t old_rcount
;
1053 LCK_MTX_ASSERT(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1055 if (rs_num
< 0 || rs_num
>= PF_RULESET_MAX
) {
1058 rs
= pf_find_ruleset(anchor
);
1059 if (rs
== NULL
|| !rs
->rules
[rs_num
].inactive
.open
||
1060 ticket
!= rs
->rules
[rs_num
].inactive
.ticket
) {
1064 /* Calculate checksum for the main ruleset */
1065 if (rs
== &pf_main_ruleset
) {
1066 error
= pf_setup_pfsync_matching(rs
);
1072 /* Swap rules, keep the old. */
1073 old_rules
= rs
->rules
[rs_num
].active
.ptr
;
1074 old_rcount
= rs
->rules
[rs_num
].active
.rcount
;
1075 old_array
= rs
->rules
[rs_num
].active
.ptr_array
;
1077 if (old_rcount
!= 0) {
1078 r
= TAILQ_FIRST(rs
->rules
[rs_num
].active
.ptr
);
1080 if (r
->rule_flag
& PFRULE_PFM
) {
1083 r
= TAILQ_NEXT(r
, entries
);
1088 rs
->rules
[rs_num
].active
.ptr
=
1089 rs
->rules
[rs_num
].inactive
.ptr
;
1090 rs
->rules
[rs_num
].active
.ptr_array
=
1091 rs
->rules
[rs_num
].inactive
.ptr_array
;
1092 rs
->rules
[rs_num
].active
.rcount
=
1093 rs
->rules
[rs_num
].inactive
.rcount
;
1094 rs
->rules
[rs_num
].inactive
.ptr
= old_rules
;
1095 rs
->rules
[rs_num
].inactive
.ptr_array
= old_array
;
1096 rs
->rules
[rs_num
].inactive
.rcount
= old_rcount
;
1098 rs
->rules
[rs_num
].active
.ticket
=
1099 rs
->rules
[rs_num
].inactive
.ticket
;
1100 pf_calc_skip_steps(rs
->rules
[rs_num
].active
.ptr
);
1103 /* Purge the old rule list. */
1104 while ((rule
= TAILQ_FIRST(old_rules
)) != NULL
) {
1105 pf_rm_rule(old_rules
, rule
);
1107 if (rs
->rules
[rs_num
].inactive
.ptr_array
) {
1108 _FREE(rs
->rules
[rs_num
].inactive
.ptr_array
, M_TEMP
);
1110 rs
->rules
[rs_num
].inactive
.ptr_array
= NULL
;
1111 rs
->rules
[rs_num
].inactive
.rcount
= 0;
1112 rs
->rules
[rs_num
].inactive
.open
= 0;
1113 pf_remove_if_empty_ruleset(rs
);
1118 pf_rule_copyin(struct pf_rule
*src
, struct pf_rule
*dst
, struct proc
*p
,
1121 bcopy(src
, dst
, sizeof(struct pf_rule
));
1123 dst
->label
[sizeof(dst
->label
) - 1] = '\0';
1124 dst
->ifname
[sizeof(dst
->ifname
) - 1] = '\0';
1125 dst
->qname
[sizeof(dst
->qname
) - 1] = '\0';
1126 dst
->pqname
[sizeof(dst
->pqname
) - 1] = '\0';
1127 dst
->tagname
[sizeof(dst
->tagname
) - 1] = '\0';
1128 dst
->match_tagname
[sizeof(dst
->match_tagname
) - 1] = '\0';
1129 dst
->overload_tblname
[sizeof(dst
->overload_tblname
) - 1] = '\0';
1131 dst
->cuid
= kauth_cred_getuid(p
->p_ucred
);
1132 dst
->cpid
= p
->p_pid
;
1136 dst
->overload_tbl
= NULL
;
1138 TAILQ_INIT(&dst
->rpool
.list
);
1139 dst
->rpool
.cur
= NULL
;
1141 /* initialize refcounting */
1145 dst
->entries
.tqe_prev
= NULL
;
1146 dst
->entries
.tqe_next
= NULL
;
1147 if ((uint8_t)minordev
== PFDEV_PFM
) {
1148 dst
->rule_flag
|= PFRULE_PFM
;
1153 pf_rule_copyout(struct pf_rule
*src
, struct pf_rule
*dst
)
1155 bcopy(src
, dst
, sizeof(struct pf_rule
));
1159 dst
->overload_tbl
= NULL
;
1161 TAILQ_INIT(&dst
->rpool
.list
);
1162 dst
->rpool
.cur
= NULL
;
1164 dst
->entries
.tqe_prev
= NULL
;
1165 dst
->entries
.tqe_next
= NULL
;
1169 pf_state_export(struct pfsync_state
*sp
, struct pf_state_key
*sk
,
1172 uint64_t secs
= pf_time_second();
1173 bzero(sp
, sizeof(struct pfsync_state
));
1175 /* copy from state key */
1176 sp
->lan
.addr
= sk
->lan
.addr
;
1177 sp
->lan
.xport
= sk
->lan
.xport
;
1178 sp
->gwy
.addr
= sk
->gwy
.addr
;
1179 sp
->gwy
.xport
= sk
->gwy
.xport
;
1180 sp
->ext_lan
.addr
= sk
->ext_lan
.addr
;
1181 sp
->ext_lan
.xport
= sk
->ext_lan
.xport
;
1182 sp
->ext_gwy
.addr
= sk
->ext_gwy
.addr
;
1183 sp
->ext_gwy
.xport
= sk
->ext_gwy
.xport
;
1184 sp
->proto_variant
= sk
->proto_variant
;
1186 sp
->proto
= sk
->proto
;
1187 sp
->af_lan
= sk
->af_lan
;
1188 sp
->af_gwy
= sk
->af_gwy
;
1189 sp
->direction
= sk
->direction
;
1190 sp
->flowhash
= sk
->flowhash
;
1192 /* copy from state */
1193 memcpy(&sp
->id
, &s
->id
, sizeof(sp
->id
));
1194 sp
->creatorid
= s
->creatorid
;
1195 strlcpy(sp
->ifname
, s
->kif
->pfik_name
, sizeof(sp
->ifname
));
1196 pf_state_peer_to_pfsync(&s
->src
, &sp
->src
);
1197 pf_state_peer_to_pfsync(&s
->dst
, &sp
->dst
);
1199 sp
->rule
= s
->rule
.ptr
->nr
;
1200 sp
->nat_rule
= (s
->nat_rule
.ptr
== NULL
) ?
1201 (unsigned)-1 : s
->nat_rule
.ptr
->nr
;
1202 sp
->anchor
= (s
->anchor
.ptr
== NULL
) ?
1203 (unsigned)-1 : s
->anchor
.ptr
->nr
;
1205 pf_state_counter_to_pfsync(s
->bytes
[0], sp
->bytes
[0]);
1206 pf_state_counter_to_pfsync(s
->bytes
[1], sp
->bytes
[1]);
1207 pf_state_counter_to_pfsync(s
->packets
[0], sp
->packets
[0]);
1208 pf_state_counter_to_pfsync(s
->packets
[1], sp
->packets
[1]);
1209 sp
->creation
= secs
- s
->creation
;
1210 sp
->expire
= pf_state_expires(s
);
1212 sp
->allow_opts
= s
->allow_opts
;
1213 sp
->timeout
= s
->timeout
;
1216 sp
->sync_flags
|= PFSYNC_FLAG_SRCNODE
;
1218 if (s
->nat_src_node
) {
1219 sp
->sync_flags
|= PFSYNC_FLAG_NATSRCNODE
;
1222 if (sp
->expire
> secs
) {
1230 pf_state_import(struct pfsync_state
*sp
, struct pf_state_key
*sk
,
1233 /* copy to state key */
1234 sk
->lan
.addr
= sp
->lan
.addr
;
1235 sk
->lan
.xport
= sp
->lan
.xport
;
1236 sk
->gwy
.addr
= sp
->gwy
.addr
;
1237 sk
->gwy
.xport
= sp
->gwy
.xport
;
1238 sk
->ext_lan
.addr
= sp
->ext_lan
.addr
;
1239 sk
->ext_lan
.xport
= sp
->ext_lan
.xport
;
1240 sk
->ext_gwy
.addr
= sp
->ext_gwy
.addr
;
1241 sk
->ext_gwy
.xport
= sp
->ext_gwy
.xport
;
1242 sk
->proto_variant
= sp
->proto_variant
;
1244 sk
->proto
= sp
->proto
;
1245 sk
->af_lan
= sp
->af_lan
;
1246 sk
->af_gwy
= sp
->af_gwy
;
1247 sk
->direction
= sp
->direction
;
1248 sk
->flowhash
= pf_calc_state_key_flowhash(sk
);
1251 memcpy(&s
->id
, &sp
->id
, sizeof(sp
->id
));
1252 s
->creatorid
= sp
->creatorid
;
1253 pf_state_peer_from_pfsync(&sp
->src
, &s
->src
);
1254 pf_state_peer_from_pfsync(&sp
->dst
, &s
->dst
);
1256 s
->rule
.ptr
= &pf_default_rule
;
1257 s
->nat_rule
.ptr
= NULL
;
1258 s
->anchor
.ptr
= NULL
;
1260 s
->creation
= pf_time_second();
1261 s
->expire
= pf_time_second();
1262 if (sp
->expire
> 0) {
1263 s
->expire
-= pf_default_rule
.timeout
[sp
->timeout
] - sp
->expire
;
1266 s
->packets
[0] = s
->packets
[1] = 0;
1267 s
->bytes
[0] = s
->bytes
[1] = 0;
1271 pf_pooladdr_copyin(struct pf_pooladdr
*src
, struct pf_pooladdr
*dst
)
1273 bcopy(src
, dst
, sizeof(struct pf_pooladdr
));
1275 dst
->entries
.tqe_prev
= NULL
;
1276 dst
->entries
.tqe_next
= NULL
;
1277 dst
->ifname
[sizeof(dst
->ifname
) - 1] = '\0';
1282 pf_pooladdr_copyout(struct pf_pooladdr
*src
, struct pf_pooladdr
*dst
)
1284 bcopy(src
, dst
, sizeof(struct pf_pooladdr
));
1286 dst
->entries
.tqe_prev
= NULL
;
1287 dst
->entries
.tqe_next
= NULL
;
1292 pf_setup_pfsync_matching(struct pf_ruleset
*rs
)
1295 struct pf_rule
*rule
;
1297 u_int8_t digest
[PF_MD5_DIGEST_LENGTH
];
1300 for (rs_cnt
= 0; rs_cnt
< PF_RULESET_MAX
; rs_cnt
++) {
1301 /* XXX PF_RULESET_SCRUB as well? */
1302 if (rs_cnt
== PF_RULESET_SCRUB
) {
1306 if (rs
->rules
[rs_cnt
].inactive
.ptr_array
) {
1307 _FREE(rs
->rules
[rs_cnt
].inactive
.ptr_array
, M_TEMP
);
1309 rs
->rules
[rs_cnt
].inactive
.ptr_array
= NULL
;
1311 if (rs
->rules
[rs_cnt
].inactive
.rcount
) {
1312 rs
->rules
[rs_cnt
].inactive
.ptr_array
=
1313 _MALLOC(sizeof(caddr_t
) *
1314 rs
->rules
[rs_cnt
].inactive
.rcount
,
1317 if (!rs
->rules
[rs_cnt
].inactive
.ptr_array
) {
1322 TAILQ_FOREACH(rule
, rs
->rules
[rs_cnt
].inactive
.ptr
,
1324 pf_hash_rule(&ctx
, rule
);
1325 (rs
->rules
[rs_cnt
].inactive
.ptr_array
)[rule
->nr
] = rule
;
1329 MD5Final(digest
, &ctx
);
1330 memcpy(pf_status
.pf_chksum
, digest
, sizeof(pf_status
.pf_chksum
));
1337 LCK_MTX_ASSERT(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1339 VERIFY(pf_is_enabled
== 0);
1342 pf_status
.running
= 1;
1343 pf_status
.since
= pf_calendar_time_second();
1344 if (pf_status
.stateid
== 0) {
1345 pf_status
.stateid
= pf_time_second();
1346 pf_status
.stateid
= pf_status
.stateid
<< 32;
1348 wakeup(pf_purge_thread_fn
);
1349 DPFPRINTF(PF_DEBUG_MISC
, ("pf: started\n"));
1355 LCK_MTX_ASSERT(pf_lock
, LCK_MTX_ASSERT_OWNED
);
1357 VERIFY(pf_is_enabled
);
1359 pf_status
.running
= 0;
1361 pf_status
.since
= pf_calendar_time_second();
1362 wakeup(pf_purge_thread_fn
);
1363 DPFPRINTF(PF_DEBUG_MISC
, ("pf: stopped\n"));
1367 pfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, int flags
, struct proc
*p
)
1370 int p64
= proc_is64bit(p
);
1372 int minordev
= minor(dev
);
1374 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
1378 /* XXX keep in sync with switch() below */
1379 if (securelevel
> 1) {
1386 case DIOCSETSTATUSIF
:
1392 case DIOCINSERTRULE
:
1393 case DIOCDELETERULE
:
1394 case DIOCGETTIMEOUT
:
1395 case DIOCCLRRULECTRS
:
1400 case DIOCGETRULESETS
:
1401 case DIOCGETRULESET
:
1402 case DIOCRGETTABLES
:
1403 case DIOCRGETTSTATS
:
1404 case DIOCRCLRTSTATS
:
1410 case DIOCRGETASTATS
:
1411 case DIOCRCLRASTATS
:
1414 case DIOCGETSRCNODES
:
1415 case DIOCCLRSRCNODES
:
1416 case DIOCIGETIFACES
:
1421 case DIOCRCLRTABLES
:
1422 case DIOCRADDTABLES
:
1423 case DIOCRDELTABLES
:
1424 case DIOCRSETTFLAGS
: {
1427 bcopy(&((struct pfioc_table
*)(void *)addr
)->
1428 pfrio_flags
, &pfrio_flags
, sizeof(pfrio_flags
));
1430 if (pfrio_flags
& PFR_FLAG_DUMMY
) {
1431 break; /* dummy operation ok */
1440 if (!(flags
& FWRITE
)) {
1446 case DIOCGETSTARTERS
:
1453 case DIOCINSERTRULE
:
1454 case DIOCDELETERULE
:
1455 case DIOCGETTIMEOUT
:
1460 case DIOCGETRULESETS
:
1461 case DIOCGETRULESET
:
1463 case DIOCRGETTABLES
:
1464 case DIOCRGETTSTATS
:
1466 case DIOCRGETASTATS
:
1469 case DIOCGETSRCNODES
:
1470 case DIOCIGETIFACES
:
1473 case DIOCRCLRTABLES
:
1474 case DIOCRADDTABLES
:
1475 case DIOCRDELTABLES
:
1476 case DIOCRCLRTSTATS
:
1481 case DIOCRSETTFLAGS
: {
1484 bcopy(&((struct pfioc_table
*)(void *)addr
)->
1485 pfrio_flags
, &pfrio_flags
, sizeof(pfrio_flags
));
1487 if (pfrio_flags
& PFR_FLAG_DUMMY
) {
1488 flags
|= FWRITE
; /* need write lock for dummy */
1489 break; /* dummy operation ok */
1496 bcopy(&((struct pfioc_rule
*)(void *)addr
)->action
,
1497 &action
, sizeof(action
));
1499 if (action
== PF_GET_CLR_CNTR
) {
1509 if (flags
& FWRITE
) {
1510 lck_rw_lock_exclusive(pf_perim_lock
);
1512 lck_rw_lock_shared(pf_perim_lock
);
1515 lck_mtx_lock(pf_lock
);
1519 if (pf_status
.running
) {
1521 * Increment the reference for a simple -e enable, so
1522 * that even if other processes drop their references,
1523 * pf will still be available to processes that turned
1524 * it on without taking a reference
1526 if (nr_tokens
== pf_enabled_ref_count
) {
1527 pf_enabled_ref_count
++;
1528 VERIFY(pf_enabled_ref_count
!= 0);
1531 } else if (pf_purge_thread
== NULL
) {
1535 pf_enabled_ref_count
++;
1536 VERIFY(pf_enabled_ref_count
!= 0);
1540 case DIOCSTARTREF
: /* u_int64_t */
1541 if (pf_purge_thread
== NULL
) {
1546 /* small enough to be on stack */
1547 if ((token
= generate_token(p
)) != 0) {
1548 if (pf_is_enabled
== 0) {
1551 pf_enabled_ref_count
++;
1552 VERIFY(pf_enabled_ref_count
!= 0);
1555 DPFPRINTF(PF_DEBUG_URGENT
,
1556 ("pf: unable to generate token\n"));
1558 bcopy(&token
, addr
, sizeof(token
));
1563 if (!pf_status
.running
) {
1567 pf_enabled_ref_count
= 0;
1568 invalidate_all_tokens();
1572 case DIOCSTOPREF
: /* struct pfioc_remove_token */
1573 if (!pf_status
.running
) {
1576 struct pfioc_remove_token pfrt
;
1578 /* small enough to be on stack */
1579 bcopy(addr
, &pfrt
, sizeof(pfrt
));
1580 if ((error
= remove_token(&pfrt
)) == 0) {
1581 VERIFY(pf_enabled_ref_count
!= 0);
1582 pf_enabled_ref_count
--;
1583 /* return currently held references */
1584 pfrt
.refcount
= pf_enabled_ref_count
;
1585 DPFPRINTF(PF_DEBUG_MISC
,
1586 ("pf: enabled refcount decremented\n"));
1589 DPFPRINTF(PF_DEBUG_URGENT
,
1590 ("pf: token mismatch\n"));
1592 bcopy(&pfrt
, addr
, sizeof(pfrt
));
1594 if (error
== 0 && pf_enabled_ref_count
== 0) {
1600 case DIOCGETSTARTERS
: { /* struct pfioc_tokens */
1601 PFIOCX_STRUCT_DECL(pfioc_tokens
);
1603 PFIOCX_STRUCT_BEGIN(addr
, pfioc_tokens
, error
= ENOMEM
; break; );
1604 error
= pfioctl_ioc_tokens(cmd
,
1605 PFIOCX_STRUCT_ADDR32(pfioc_tokens
),
1606 PFIOCX_STRUCT_ADDR64(pfioc_tokens
), p
);
1607 PFIOCX_STRUCT_END(pfioc_tokens
, addr
);
1611 case DIOCADDRULE
: /* struct pfioc_rule */
1612 case DIOCGETRULES
: /* struct pfioc_rule */
1613 case DIOCGETRULE
: /* struct pfioc_rule */
1614 case DIOCCHANGERULE
: /* struct pfioc_rule */
1615 case DIOCINSERTRULE
: /* struct pfioc_rule */
1616 case DIOCDELETERULE
: { /* struct pfioc_rule */
1617 struct pfioc_rule
*pr
= NULL
;
1619 PFIOC_STRUCT_BEGIN(addr
, pr
, error
= ENOMEM
; break; );
1620 error
= pfioctl_ioc_rule(cmd
, minordev
, pr
, p
);
1621 PFIOC_STRUCT_END(pr
, addr
);
1625 case DIOCCLRSTATES
: /* struct pfioc_state_kill */
1626 case DIOCKILLSTATES
: { /* struct pfioc_state_kill */
1627 struct pfioc_state_kill
*psk
= NULL
;
1629 PFIOC_STRUCT_BEGIN(addr
, psk
, error
= ENOMEM
; break; );
1630 error
= pfioctl_ioc_state_kill(cmd
, psk
, p
);
1631 PFIOC_STRUCT_END(psk
, addr
);
1635 case DIOCADDSTATE
: /* struct pfioc_state */
1636 case DIOCGETSTATE
: { /* struct pfioc_state */
1637 struct pfioc_state
*ps
= NULL
;
1639 PFIOC_STRUCT_BEGIN(addr
, ps
, error
= ENOMEM
; break; );
1640 error
= pfioctl_ioc_state(cmd
, ps
, p
);
1641 PFIOC_STRUCT_END(ps
, addr
);
1645 case DIOCGETSTATES
: { /* struct pfioc_states */
1646 PFIOCX_STRUCT_DECL(pfioc_states
);
1648 PFIOCX_STRUCT_BEGIN(addr
, pfioc_states
, error
= ENOMEM
; break; );
1649 error
= pfioctl_ioc_states(cmd
,
1650 PFIOCX_STRUCT_ADDR32(pfioc_states
),
1651 PFIOCX_STRUCT_ADDR64(pfioc_states
), p
);
1652 PFIOCX_STRUCT_END(pfioc_states
, addr
);
1656 case DIOCGETSTATUS
: { /* struct pf_status */
1657 struct pf_status
*s
= NULL
;
1659 PFIOC_STRUCT_BEGIN(&pf_status
, s
, error
= ENOMEM
; break; );
1660 pfi_update_status(s
->ifname
, s
);
1661 PFIOC_STRUCT_END(s
, addr
);
1665 case DIOCSETSTATUSIF
: { /* struct pfioc_if */
1666 struct pfioc_if
*pi
= (struct pfioc_if
*)(void *)addr
;
1668 /* OK for unaligned accesses */
1669 if (pi
->ifname
[0] == 0) {
1670 bzero(pf_status
.ifname
, IFNAMSIZ
);
1673 strlcpy(pf_status
.ifname
, pi
->ifname
, IFNAMSIZ
);
1677 case DIOCCLRSTATUS
: {
1678 bzero(pf_status
.counters
, sizeof(pf_status
.counters
));
1679 bzero(pf_status
.fcounters
, sizeof(pf_status
.fcounters
));
1680 bzero(pf_status
.scounters
, sizeof(pf_status
.scounters
));
1681 pf_status
.since
= pf_calendar_time_second();
1682 if (*pf_status
.ifname
) {
1683 pfi_update_status(pf_status
.ifname
, NULL
);
1688 case DIOCNATLOOK
: { /* struct pfioc_natlook */
1689 struct pfioc_natlook
*pnl
= NULL
;
1691 PFIOC_STRUCT_BEGIN(addr
, pnl
, error
= ENOMEM
; break; );
1692 error
= pfioctl_ioc_natlook(cmd
, pnl
, p
);
1693 PFIOC_STRUCT_END(pnl
, addr
);
1697 case DIOCSETTIMEOUT
: /* struct pfioc_tm */
1698 case DIOCGETTIMEOUT
: { /* struct pfioc_tm */
1701 /* small enough to be on stack */
1702 bcopy(addr
, &pt
, sizeof(pt
));
1703 error
= pfioctl_ioc_tm(cmd
, &pt
, p
);
1704 bcopy(&pt
, addr
, sizeof(pt
));
1708 case DIOCGETLIMIT
: /* struct pfioc_limit */
1709 case DIOCSETLIMIT
: { /* struct pfioc_limit */
1710 struct pfioc_limit pl
;
1712 /* small enough to be on stack */
1713 bcopy(addr
, &pl
, sizeof(pl
));
1714 error
= pfioctl_ioc_limit(cmd
, &pl
, p
);
1715 bcopy(&pl
, addr
, sizeof(pl
));
1719 case DIOCSETDEBUG
: { /* u_int32_t */
1720 bcopy(addr
, &pf_status
.debug
, sizeof(u_int32_t
));
1724 case DIOCCLRRULECTRS
: {
1725 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
1726 struct pf_ruleset
*ruleset
= &pf_main_ruleset
;
1727 struct pf_rule
*rule
;
1730 ruleset
->rules
[PF_RULESET_FILTER
].active
.ptr
, entries
) {
1731 rule
->evaluations
= 0;
1732 rule
->packets
[0] = rule
->packets
[1] = 0;
1733 rule
->bytes
[0] = rule
->bytes
[1] = 0;
1738 case DIOCGIFSPEED
: {
1739 struct pf_ifspeed
*psp
= (struct pf_ifspeed
*)(void *)addr
;
1740 struct pf_ifspeed ps
;
1744 if (psp
->ifname
[0] != '\0') {
1745 /* Can we completely trust user-land? */
1746 strlcpy(ps
.ifname
, psp
->ifname
, IFNAMSIZ
);
1747 ps
.ifname
[IFNAMSIZ
- 1] = '\0';
1748 ifp
= ifunit(ps
.ifname
);
1750 baudrate
= ifp
->if_output_bw
.max_bw
;
1751 bcopy(&baudrate
, &psp
->baudrate
,
1762 case DIOCBEGINADDRS
: /* struct pfioc_pooladdr */
1763 case DIOCADDADDR
: /* struct pfioc_pooladdr */
1764 case DIOCGETADDRS
: /* struct pfioc_pooladdr */
1765 case DIOCGETADDR
: /* struct pfioc_pooladdr */
1766 case DIOCCHANGEADDR
: { /* struct pfioc_pooladdr */
1767 struct pfioc_pooladdr
*pp
= NULL
;
1769 PFIOC_STRUCT_BEGIN(addr
, pp
, error
= ENOMEM
; break; )
1770 error
= pfioctl_ioc_pooladdr(cmd
, pp
, p
);
1771 PFIOC_STRUCT_END(pp
, addr
);
1775 case DIOCGETRULESETS
: /* struct pfioc_ruleset */
1776 case DIOCGETRULESET
: { /* struct pfioc_ruleset */
1777 struct pfioc_ruleset
*pr
= NULL
;
1779 PFIOC_STRUCT_BEGIN(addr
, pr
, error
= ENOMEM
; break; );
1780 error
= pfioctl_ioc_ruleset(cmd
, pr
, p
);
1781 PFIOC_STRUCT_END(pr
, addr
);
1785 case DIOCRCLRTABLES
: /* struct pfioc_table */
1786 case DIOCRADDTABLES
: /* struct pfioc_table */
1787 case DIOCRDELTABLES
: /* struct pfioc_table */
1788 case DIOCRGETTABLES
: /* struct pfioc_table */
1789 case DIOCRGETTSTATS
: /* struct pfioc_table */
1790 case DIOCRCLRTSTATS
: /* struct pfioc_table */
1791 case DIOCRSETTFLAGS
: /* struct pfioc_table */
1792 case DIOCRCLRADDRS
: /* struct pfioc_table */
1793 case DIOCRADDADDRS
: /* struct pfioc_table */
1794 case DIOCRDELADDRS
: /* struct pfioc_table */
1795 case DIOCRSETADDRS
: /* struct pfioc_table */
1796 case DIOCRGETADDRS
: /* struct pfioc_table */
1797 case DIOCRGETASTATS
: /* struct pfioc_table */
1798 case DIOCRCLRASTATS
: /* struct pfioc_table */
1799 case DIOCRTSTADDRS
: /* struct pfioc_table */
1800 case DIOCRINADEFINE
: { /* struct pfioc_table */
1801 PFIOCX_STRUCT_DECL(pfioc_table
);
1803 PFIOCX_STRUCT_BEGIN(addr
, pfioc_table
, error
= ENOMEM
; break; );
1804 error
= pfioctl_ioc_table(cmd
,
1805 PFIOCX_STRUCT_ADDR32(pfioc_table
),
1806 PFIOCX_STRUCT_ADDR64(pfioc_table
), p
);
1807 PFIOCX_STRUCT_END(pfioc_table
, addr
);
1811 case DIOCOSFPADD
: /* struct pf_osfp_ioctl */
1812 case DIOCOSFPGET
: { /* struct pf_osfp_ioctl */
1813 struct pf_osfp_ioctl
*io
= NULL
;
1815 PFIOC_STRUCT_BEGIN(addr
, io
, error
= ENOMEM
; break; );
1816 if (cmd
== DIOCOSFPADD
) {
1817 error
= pf_osfp_add(io
);
1819 VERIFY(cmd
== DIOCOSFPGET
);
1820 error
= pf_osfp_get(io
);
1822 PFIOC_STRUCT_END(io
, addr
);
1826 case DIOCXBEGIN
: /* struct pfioc_trans */
1827 case DIOCXROLLBACK
: /* struct pfioc_trans */
1828 case DIOCXCOMMIT
: { /* struct pfioc_trans */
1829 PFIOCX_STRUCT_DECL(pfioc_trans
);
1831 PFIOCX_STRUCT_BEGIN(addr
, pfioc_trans
, error
= ENOMEM
; break; );
1832 error
= pfioctl_ioc_trans(cmd
,
1833 PFIOCX_STRUCT_ADDR32(pfioc_trans
),
1834 PFIOCX_STRUCT_ADDR64(pfioc_trans
), p
);
1835 PFIOCX_STRUCT_END(pfioc_trans
, addr
);
1839 case DIOCGETSRCNODES
: { /* struct pfioc_src_nodes */
1840 PFIOCX_STRUCT_DECL(pfioc_src_nodes
);
1842 PFIOCX_STRUCT_BEGIN(addr
, pfioc_src_nodes
,
1843 error
= ENOMEM
; break; );
1844 error
= pfioctl_ioc_src_nodes(cmd
,
1845 PFIOCX_STRUCT_ADDR32(pfioc_src_nodes
),
1846 PFIOCX_STRUCT_ADDR64(pfioc_src_nodes
), p
);
1847 PFIOCX_STRUCT_END(pfioc_src_nodes
, addr
);
1851 case DIOCCLRSRCNODES
: {
1852 struct pf_src_node
*n
;
1853 struct pf_state
*state
;
1855 RB_FOREACH(state
, pf_state_tree_id
, &tree_id
) {
1856 state
->src_node
= NULL
;
1857 state
->nat_src_node
= NULL
;
1859 RB_FOREACH(n
, pf_src_tree
, &tree_src_tracking
) {
1863 pf_purge_expired_src_nodes();
1864 pf_status
.src_nodes
= 0;
1868 case DIOCKILLSRCNODES
: { /* struct pfioc_src_node_kill */
1869 struct pfioc_src_node_kill
*psnk
= NULL
;
1871 PFIOC_STRUCT_BEGIN(addr
, psnk
, error
= ENOMEM
; break; );
1872 error
= pfioctl_ioc_src_node_kill(cmd
, psnk
, p
);
1873 PFIOC_STRUCT_END(psnk
, addr
);
1877 case DIOCSETHOSTID
: { /* u_int32_t */
1880 /* small enough to be on stack */
1881 bcopy(addr
, &hid
, sizeof(hid
));
1883 pf_status
.hostid
= random();
1885 pf_status
.hostid
= hid
;
1894 case DIOCIGETIFACES
: /* struct pfioc_iface */
1895 case DIOCSETIFFLAG
: /* struct pfioc_iface */
1896 case DIOCCLRIFFLAG
: { /* struct pfioc_iface */
1897 PFIOCX_STRUCT_DECL(pfioc_iface
);
1899 PFIOCX_STRUCT_BEGIN(addr
, pfioc_iface
, error
= ENOMEM
; break; );
1900 error
= pfioctl_ioc_iface(cmd
,
1901 PFIOCX_STRUCT_ADDR32(pfioc_iface
),
1902 PFIOCX_STRUCT_ADDR64(pfioc_iface
), p
);
1903 PFIOCX_STRUCT_END(pfioc_iface
, addr
);
1912 lck_mtx_unlock(pf_lock
);
1913 lck_rw_done(pf_perim_lock
);
1919 pfioctl_ioc_table(u_long cmd
, struct pfioc_table_32
*io32
,
1920 struct pfioc_table_64
*io64
, struct proc
*p
)
1922 int p64
= proc_is64bit(p
);
1930 * 64-bit structure processing
1933 case DIOCRCLRTABLES
:
1934 if (io64
->pfrio_esize
!= 0) {
1938 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
1939 error
= pfr_clr_tables(&io64
->pfrio_table
, &io64
->pfrio_ndel
,
1940 io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
1943 case DIOCRADDTABLES
:
1944 if (io64
->pfrio_esize
!= sizeof(struct pfr_table
)) {
1948 error
= pfr_add_tables(io64
->pfrio_buffer
, io64
->pfrio_size
,
1949 &io64
->pfrio_nadd
, io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
1952 case DIOCRDELTABLES
:
1953 if (io64
->pfrio_esize
!= sizeof(struct pfr_table
)) {
1957 error
= pfr_del_tables(io64
->pfrio_buffer
, io64
->pfrio_size
,
1958 &io64
->pfrio_ndel
, io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
1961 case DIOCRGETTABLES
:
1962 if (io64
->pfrio_esize
!= sizeof(struct pfr_table
)) {
1966 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
1967 error
= pfr_get_tables(&io64
->pfrio_table
, io64
->pfrio_buffer
,
1968 &io64
->pfrio_size
, io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
1971 case DIOCRGETTSTATS
:
1972 if (io64
->pfrio_esize
!= sizeof(struct pfr_tstats
)) {
1976 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
1977 error
= pfr_get_tstats(&io64
->pfrio_table
, io64
->pfrio_buffer
,
1978 &io64
->pfrio_size
, io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
1981 case DIOCRCLRTSTATS
:
1982 if (io64
->pfrio_esize
!= sizeof(struct pfr_table
)) {
1986 error
= pfr_clr_tstats(io64
->pfrio_buffer
, io64
->pfrio_size
,
1987 &io64
->pfrio_nzero
, io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
1990 case DIOCRSETTFLAGS
:
1991 if (io64
->pfrio_esize
!= sizeof(struct pfr_table
)) {
1995 error
= pfr_set_tflags(io64
->pfrio_buffer
, io64
->pfrio_size
,
1996 io64
->pfrio_setflag
, io64
->pfrio_clrflag
,
1997 &io64
->pfrio_nchange
, &io64
->pfrio_ndel
,
1998 io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2002 if (io64
->pfrio_esize
!= 0) {
2006 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
2007 error
= pfr_clr_addrs(&io64
->pfrio_table
, &io64
->pfrio_ndel
,
2008 io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2012 if (io64
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2016 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
2017 error
= pfr_add_addrs(&io64
->pfrio_table
, io64
->pfrio_buffer
,
2018 io64
->pfrio_size
, &io64
->pfrio_nadd
, io64
->pfrio_flags
|
2019 PFR_FLAG_USERIOCTL
);
2023 if (io64
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2027 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
2028 error
= pfr_del_addrs(&io64
->pfrio_table
, io64
->pfrio_buffer
,
2029 io64
->pfrio_size
, &io64
->pfrio_ndel
, io64
->pfrio_flags
|
2030 PFR_FLAG_USERIOCTL
);
2034 if (io64
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2038 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
2039 error
= pfr_set_addrs(&io64
->pfrio_table
, io64
->pfrio_buffer
,
2040 io64
->pfrio_size
, &io64
->pfrio_size2
, &io64
->pfrio_nadd
,
2041 &io64
->pfrio_ndel
, &io64
->pfrio_nchange
, io64
->pfrio_flags
|
2042 PFR_FLAG_USERIOCTL
, 0);
2046 if (io64
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2050 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
2051 error
= pfr_get_addrs(&io64
->pfrio_table
, io64
->pfrio_buffer
,
2052 &io64
->pfrio_size
, io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2055 case DIOCRGETASTATS
:
2056 if (io64
->pfrio_esize
!= sizeof(struct pfr_astats
)) {
2060 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
2061 error
= pfr_get_astats(&io64
->pfrio_table
, io64
->pfrio_buffer
,
2062 &io64
->pfrio_size
, io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2065 case DIOCRCLRASTATS
:
2066 if (io64
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2070 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
2071 error
= pfr_clr_astats(&io64
->pfrio_table
, io64
->pfrio_buffer
,
2072 io64
->pfrio_size
, &io64
->pfrio_nzero
, io64
->pfrio_flags
|
2073 PFR_FLAG_USERIOCTL
);
2077 if (io64
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2081 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
2082 error
= pfr_tst_addrs(&io64
->pfrio_table
, io64
->pfrio_buffer
,
2083 io64
->pfrio_size
, &io64
->pfrio_nmatch
, io64
->pfrio_flags
|
2084 PFR_FLAG_USERIOCTL
);
2087 case DIOCRINADEFINE
:
2088 if (io64
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2092 pfr_table_copyin_cleanup(&io64
->pfrio_table
);
2093 error
= pfr_ina_define(&io64
->pfrio_table
, io64
->pfrio_buffer
,
2094 io64
->pfrio_size
, &io64
->pfrio_nadd
, &io64
->pfrio_naddr
,
2095 io64
->pfrio_ticket
, io64
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2106 * 32-bit structure processing
2109 case DIOCRCLRTABLES
:
2110 if (io32
->pfrio_esize
!= 0) {
2114 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2115 error
= pfr_clr_tables(&io32
->pfrio_table
, &io32
->pfrio_ndel
,
2116 io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2119 case DIOCRADDTABLES
:
2120 if (io32
->pfrio_esize
!= sizeof(struct pfr_table
)) {
2124 error
= pfr_add_tables(io32
->pfrio_buffer
, io32
->pfrio_size
,
2125 &io32
->pfrio_nadd
, io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2128 case DIOCRDELTABLES
:
2129 if (io32
->pfrio_esize
!= sizeof(struct pfr_table
)) {
2133 error
= pfr_del_tables(io32
->pfrio_buffer
, io32
->pfrio_size
,
2134 &io32
->pfrio_ndel
, io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2137 case DIOCRGETTABLES
:
2138 if (io32
->pfrio_esize
!= sizeof(struct pfr_table
)) {
2142 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2143 error
= pfr_get_tables(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2144 &io32
->pfrio_size
, io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2147 case DIOCRGETTSTATS
:
2148 if (io32
->pfrio_esize
!= sizeof(struct pfr_tstats
)) {
2152 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2153 error
= pfr_get_tstats(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2154 &io32
->pfrio_size
, io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2157 case DIOCRCLRTSTATS
:
2158 if (io32
->pfrio_esize
!= sizeof(struct pfr_table
)) {
2162 error
= pfr_clr_tstats(io32
->pfrio_buffer
, io32
->pfrio_size
,
2163 &io32
->pfrio_nzero
, io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2166 case DIOCRSETTFLAGS
:
2167 if (io32
->pfrio_esize
!= sizeof(struct pfr_table
)) {
2171 error
= pfr_set_tflags(io32
->pfrio_buffer
, io32
->pfrio_size
,
2172 io32
->pfrio_setflag
, io32
->pfrio_clrflag
,
2173 &io32
->pfrio_nchange
, &io32
->pfrio_ndel
,
2174 io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2178 if (io32
->pfrio_esize
!= 0) {
2182 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2183 error
= pfr_clr_addrs(&io32
->pfrio_table
, &io32
->pfrio_ndel
,
2184 io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2188 if (io32
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2192 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2193 error
= pfr_add_addrs(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2194 io32
->pfrio_size
, &io32
->pfrio_nadd
, io32
->pfrio_flags
|
2195 PFR_FLAG_USERIOCTL
);
2199 if (io32
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2203 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2204 error
= pfr_del_addrs(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2205 io32
->pfrio_size
, &io32
->pfrio_ndel
, io32
->pfrio_flags
|
2206 PFR_FLAG_USERIOCTL
);
2210 if (io32
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2214 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2215 error
= pfr_set_addrs(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2216 io32
->pfrio_size
, &io32
->pfrio_size2
, &io32
->pfrio_nadd
,
2217 &io32
->pfrio_ndel
, &io32
->pfrio_nchange
, io32
->pfrio_flags
|
2218 PFR_FLAG_USERIOCTL
, 0);
2222 if (io32
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2226 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2227 error
= pfr_get_addrs(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2228 &io32
->pfrio_size
, io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2231 case DIOCRGETASTATS
:
2232 if (io32
->pfrio_esize
!= sizeof(struct pfr_astats
)) {
2236 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2237 error
= pfr_get_astats(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2238 &io32
->pfrio_size
, io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2241 case DIOCRCLRASTATS
:
2242 if (io32
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2246 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2247 error
= pfr_clr_astats(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2248 io32
->pfrio_size
, &io32
->pfrio_nzero
, io32
->pfrio_flags
|
2249 PFR_FLAG_USERIOCTL
);
2253 if (io32
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2257 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2258 error
= pfr_tst_addrs(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2259 io32
->pfrio_size
, &io32
->pfrio_nmatch
, io32
->pfrio_flags
|
2260 PFR_FLAG_USERIOCTL
);
2263 case DIOCRINADEFINE
:
2264 if (io32
->pfrio_esize
!= sizeof(struct pfr_addr
)) {
2268 pfr_table_copyin_cleanup(&io32
->pfrio_table
);
2269 error
= pfr_ina_define(&io32
->pfrio_table
, io32
->pfrio_buffer
,
2270 io32
->pfrio_size
, &io32
->pfrio_nadd
, &io32
->pfrio_naddr
,
2271 io32
->pfrio_ticket
, io32
->pfrio_flags
| PFR_FLAG_USERIOCTL
);
2284 pfioctl_ioc_tokens(u_long cmd
, struct pfioc_tokens_32
*tok32
,
2285 struct pfioc_tokens_64
*tok64
, struct proc
*p
)
2287 struct pfioc_token
*tokens
;
2288 struct pfioc_kernel_token
*entry
, *tmp
;
2289 user_addr_t token_buf
;
2290 int ocnt
, cnt
, error
= 0, p64
= proc_is64bit(p
);
2294 case DIOCGETSTARTERS
: {
2297 if (nr_tokens
== 0) {
2302 size
= sizeof(struct pfioc_token
) * nr_tokens
;
2303 if (size
/ nr_tokens
!= sizeof(struct pfioc_token
)) {
2304 os_log_error(OS_LOG_DEFAULT
, "%s: size overflows", __func__
);
2308 ocnt
= cnt
= (p64
? tok64
->size
: tok32
->size
);
2318 token_buf
= (p64
? tok64
->pgt_buf
: tok32
->pgt_buf
);
2319 tokens
= _MALLOC(size
, M_TEMP
, M_WAITOK
| M_ZERO
);
2320 if (tokens
== NULL
) {
2325 ptr
= (void *)tokens
;
2326 SLIST_FOREACH_SAFE(entry
, &token_list_head
, next
, tmp
) {
2327 struct pfioc_token
*t
;
2329 if ((unsigned)cnt
< sizeof(*tokens
)) {
2330 break; /* no more buffer space left */
2332 t
= (struct pfioc_token
*)(void *)ptr
;
2333 t
->token_value
= entry
->token
.token_value
;
2334 t
->timestamp
= entry
->token
.timestamp
;
2335 t
->pid
= entry
->token
.pid
;
2336 bcopy(entry
->token
.proc_name
, t
->proc_name
,
2337 PFTOK_PROCNAME_LEN
);
2338 ptr
+= sizeof(struct pfioc_token
);
2340 cnt
-= sizeof(struct pfioc_token
);
2344 error
= copyout(tokens
, token_buf
, ocnt
- cnt
);
2348 tok64
->size
= ocnt
- cnt
;
2350 tok32
->size
= ocnt
- cnt
;
2353 _FREE(tokens
, M_TEMP
);
2366 pf_expire_states_and_src_nodes(struct pf_rule
*rule
)
2368 struct pf_state
*state
;
2369 struct pf_src_node
*sn
;
2372 /* expire the states */
2373 state
= TAILQ_FIRST(&state_list
);
2375 if (state
->rule
.ptr
== rule
) {
2376 state
->timeout
= PFTM_PURGE
;
2378 state
= TAILQ_NEXT(state
, entry_list
);
2380 pf_purge_expired_states(pf_status
.states
);
2382 /* expire the src_nodes */
2383 RB_FOREACH(sn
, pf_src_tree
, &tree_src_tracking
) {
2384 if (sn
->rule
.ptr
!= rule
) {
2387 if (sn
->states
!= 0) {
2388 RB_FOREACH(state
, pf_state_tree_id
,
2390 if (state
->src_node
== sn
) {
2391 state
->src_node
= NULL
;
2393 if (state
->nat_src_node
== sn
) {
2394 state
->nat_src_node
= NULL
;
2403 pf_purge_expired_src_nodes();
2408 pf_delete_rule_from_ruleset(struct pf_ruleset
*ruleset
, int rs_num
,
2409 struct pf_rule
*rule
)
2414 pf_expire_states_and_src_nodes(rule
);
2416 pf_rm_rule(ruleset
->rules
[rs_num
].active
.ptr
, rule
);
2417 if (ruleset
->rules
[rs_num
].active
.rcount
-- == 0) {
2418 panic("%s: rcount value broken!", __func__
);
2420 r
= TAILQ_FIRST(ruleset
->rules
[rs_num
].active
.ptr
);
2424 r
= TAILQ_NEXT(r
, entries
);
2430 pf_ruleset_cleanup(struct pf_ruleset
*ruleset
, int rs
)
2432 pf_calc_skip_steps(ruleset
->rules
[rs
].active
.ptr
);
2433 ruleset
->rules
[rs
].active
.ticket
=
2434 ++ruleset
->rules
[rs
].inactive
.ticket
;
2438 * req_dev encodes the PF interface. Currently, possible values are
2442 pf_delete_rule_by_ticket(struct pfioc_rule
*pr
, u_int32_t req_dev
)
2444 struct pf_ruleset
*ruleset
;
2445 struct pf_rule
*rule
= NULL
;
2450 is_anchor
= (pr
->anchor_call
[0] != '\0');
2451 if ((ruleset
= pf_find_ruleset_with_owner(pr
->anchor
,
2452 pr
->rule
.owner
, is_anchor
, &error
)) == NULL
) {
2456 for (i
= 0; i
< PF_RULESET_MAX
&& rule
== NULL
; i
++) {
2457 rule
= TAILQ_FIRST(ruleset
->rules
[i
].active
.ptr
);
2458 while (rule
&& (rule
->ticket
!= pr
->rule
.ticket
)) {
2459 rule
= TAILQ_NEXT(rule
, entries
);
2468 if (strcmp(rule
->owner
, pr
->rule
.owner
)) {
2473 if (rule
->anchor
&& (ruleset
!= &pf_main_ruleset
) &&
2474 ((strcmp(ruleset
->anchor
->owner
, "")) == 0) &&
2475 ((ruleset
->rules
[i
].active
.rcount
- 1) == 0)) {
2476 /* set rule & ruleset to parent and repeat */
2477 struct pf_rule
*delete_rule
= rule
;
2478 struct pf_ruleset
*delete_ruleset
= ruleset
;
2480 #define parent_ruleset ruleset->anchor->parent->ruleset
2481 if (ruleset
->anchor
->parent
== NULL
) {
2482 ruleset
= &pf_main_ruleset
;
2484 ruleset
= &parent_ruleset
;
2487 rule
= TAILQ_FIRST(ruleset
->rules
[i
].active
.ptr
);
2489 (rule
->anchor
!= delete_ruleset
->anchor
)) {
2490 rule
= TAILQ_NEXT(rule
, entries
);
2493 panic("%s: rule not found!", __func__
);
2497 * if reqest device != rule's device, bail :
2498 * with error if ticket matches;
2499 * without error if ticket doesn't match (i.e. its just cleanup)
2501 if ((rule
->rule_flag
& PFRULE_PFM
) ^ req_dev
) {
2502 if (rule
->ticket
!= pr
->rule
.ticket
) {
2509 if (delete_rule
->rule_flag
& PFRULE_PFM
) {
2513 pf_delete_rule_from_ruleset(delete_ruleset
,
2515 delete_ruleset
->rules
[i
].active
.ticket
=
2516 ++delete_ruleset
->rules
[i
].inactive
.ticket
;
2520 * process deleting rule only if device that added the
2521 * rule matches device that issued the request
2523 if ((rule
->rule_flag
& PFRULE_PFM
) ^ req_dev
) {
2526 if (rule
->rule_flag
& PFRULE_PFM
) {
2529 pf_delete_rule_from_ruleset(ruleset
, i
,
2531 pf_ruleset_cleanup(ruleset
, i
);
2538 * req_dev encodes the PF interface. Currently, possible values are
2542 pf_delete_rule_by_owner(char *owner
, u_int32_t req_dev
)
2544 struct pf_ruleset
*ruleset
;
2545 struct pf_rule
*rule
, *next
;
2548 for (int rs
= 0; rs
< PF_RULESET_MAX
; rs
++) {
2549 rule
= TAILQ_FIRST(pf_main_ruleset
.rules
[rs
].active
.ptr
);
2550 ruleset
= &pf_main_ruleset
;
2552 next
= TAILQ_NEXT(rule
, entries
);
2554 * process deleting rule only if device that added the
2555 * rule matches device that issued the request
2557 if ((rule
->rule_flag
& PFRULE_PFM
) ^ req_dev
) {
2562 if (((strcmp(rule
->owner
, owner
)) == 0) ||
2563 ((strcmp(rule
->owner
, "")) == 0)) {
2564 if (rule
->anchor
->ruleset
.rules
[rs
].active
.rcount
> 0) {
2566 pf_ruleset_cleanup(ruleset
, rs
);
2569 /* step into anchor */
2571 &rule
->anchor
->ruleset
;
2572 rule
= TAILQ_FIRST(ruleset
->rules
[rs
].active
.ptr
);
2575 if (rule
->rule_flag
&
2579 pf_delete_rule_from_ruleset(ruleset
, rs
, rule
);
2587 if (((strcmp(rule
->owner
, owner
)) == 0)) {
2589 if (rule
->rule_flag
& PFRULE_PFM
) {
2592 pf_delete_rule_from_ruleset(ruleset
,
2600 pf_ruleset_cleanup(ruleset
, rs
);
2603 if (ruleset
!= &pf_main_ruleset
) {
2604 pf_deleterule_anchor_step_out(&ruleset
,
2613 pf_deleterule_anchor_step_out(struct pf_ruleset
**ruleset_ptr
,
2614 int rs
, struct pf_rule
**rule_ptr
)
2616 struct pf_ruleset
*ruleset
= *ruleset_ptr
;
2617 struct pf_rule
*rule
= *rule_ptr
;
2619 /* step out of anchor */
2620 struct pf_ruleset
*rs_copy
= ruleset
;
2621 ruleset
= ruleset
->anchor
->parent
?
2622 &ruleset
->anchor
->parent
->ruleset
:&pf_main_ruleset
;
2624 rule
= TAILQ_FIRST(ruleset
->rules
[rs
].active
.ptr
);
2625 while (rule
&& (rule
->anchor
!= rs_copy
->anchor
)) {
2626 rule
= TAILQ_NEXT(rule
, entries
);
2629 panic("%s: parent rule of anchor not found!", __func__
);
2631 if (rule
->anchor
->ruleset
.rules
[rs
].active
.rcount
> 0) {
2632 rule
= TAILQ_NEXT(rule
, entries
);
2635 *ruleset_ptr
= ruleset
;
2640 pf_addrwrap_setup(struct pf_addr_wrap
*aw
)
2643 bzero(&aw
->p
, sizeof aw
->p
);
2647 pf_rule_setup(struct pfioc_rule
*pr
, struct pf_rule
*rule
,
2648 struct pf_ruleset
*ruleset
)
2650 struct pf_pooladdr
*apa
;
2653 if (rule
->ifname
[0]) {
2654 rule
->kif
= pfi_kif_get(rule
->ifname
);
2655 if (rule
->kif
== NULL
) {
2656 pool_put(&pf_rule_pl
, rule
);
2659 pfi_kif_ref(rule
->kif
, PFI_KIF_REF_RULE
);
2661 if (rule
->tagname
[0]) {
2662 if ((rule
->tag
= pf_tagname2tag(rule
->tagname
)) == 0) {
2666 if (rule
->match_tagname
[0]) {
2667 if ((rule
->match_tag
=
2668 pf_tagname2tag(rule
->match_tagname
)) == 0) {
2672 if (rule
->rt
&& !rule
->direction
) {
2679 if (rule
->logif
>= PFLOGIFS_MAX
) {
2683 pf_addrwrap_setup(&rule
->src
.addr
);
2684 pf_addrwrap_setup(&rule
->dst
.addr
);
2685 if (pf_rtlabel_add(&rule
->src
.addr
) ||
2686 pf_rtlabel_add(&rule
->dst
.addr
)) {
2689 if (pfi_dynaddr_setup(&rule
->src
.addr
, rule
->af
)) {
2692 if (pfi_dynaddr_setup(&rule
->dst
.addr
, rule
->af
)) {
2695 if (pf_tbladdr_setup(ruleset
, &rule
->src
.addr
)) {
2698 if (pf_tbladdr_setup(ruleset
, &rule
->dst
.addr
)) {
2701 if (pf_anchor_setup(rule
, ruleset
, pr
->anchor_call
)) {
2704 TAILQ_FOREACH(apa
, &pf_pabuf
, entries
)
2705 if (pf_tbladdr_setup(ruleset
, &apa
->addr
)) {
2709 if (rule
->overload_tblname
[0]) {
2710 if ((rule
->overload_tbl
= pfr_attach_table(ruleset
,
2711 rule
->overload_tblname
)) == NULL
) {
2714 rule
->overload_tbl
->pfrkt_flags
|=
2719 pf_mv_pool(&pf_pabuf
, &rule
->rpool
.list
);
2721 if (((((rule
->action
== PF_NAT
) || (rule
->action
== PF_RDR
) ||
2722 (rule
->action
== PF_BINAT
) || (rule
->action
== PF_NAT64
)) &&
2723 rule
->anchor
== NULL
) ||
2724 (rule
->rt
> PF_FASTROUTE
)) &&
2725 (TAILQ_FIRST(&rule
->rpool
.list
) == NULL
)) {
2730 pf_rm_rule(NULL
, rule
);
2733 /* For a NAT64 rule the rule's address family is AF_INET6 whereas
2734 * the address pool's family will be AF_INET
2736 rule
->rpool
.af
= (rule
->action
== PF_NAT64
) ? AF_INET
: rule
->af
;
2737 rule
->rpool
.cur
= TAILQ_FIRST(&rule
->rpool
.list
);
2738 rule
->evaluations
= rule
->packets
[0] = rule
->packets
[1] =
2739 rule
->bytes
[0] = rule
->bytes
[1] = 0;
2745 pfioctl_ioc_rule(u_long cmd
, int minordev
, struct pfioc_rule
*pr
, struct proc
*p
)
2748 u_int32_t req_dev
= 0;
2752 struct pf_ruleset
*ruleset
;
2753 struct pf_rule
*rule
, *tail
;
2756 pr
->anchor
[sizeof(pr
->anchor
) - 1] = '\0';
2757 pr
->anchor_call
[sizeof(pr
->anchor_call
) - 1] = '\0';
2758 ruleset
= pf_find_ruleset(pr
->anchor
);
2759 if (ruleset
== NULL
) {
2763 rs_num
= pf_get_ruleset_number(pr
->rule
.action
);
2764 if (rs_num
>= PF_RULESET_MAX
) {
2768 if (pr
->rule
.return_icmp
>> 8 > ICMP_MAXTYPE
) {
2772 if (pr
->ticket
!= ruleset
->rules
[rs_num
].inactive
.ticket
) {
2776 if (pr
->pool_ticket
!= ticket_pabuf
) {
2780 rule
= pool_get(&pf_rule_pl
, PR_WAITOK
);
2785 pf_rule_copyin(&pr
->rule
, rule
, p
, minordev
);
2787 if (rule
->af
== AF_INET
) {
2788 pool_put(&pf_rule_pl
, rule
);
2789 error
= EAFNOSUPPORT
;
2794 if (rule
->af
== AF_INET6
) {
2795 pool_put(&pf_rule_pl
, rule
);
2796 error
= EAFNOSUPPORT
;
2800 tail
= TAILQ_LAST(ruleset
->rules
[rs_num
].inactive
.ptr
,
2803 rule
->nr
= tail
->nr
+ 1;
2808 if ((error
= pf_rule_setup(pr
, rule
, ruleset
))) {
2812 TAILQ_INSERT_TAIL(ruleset
->rules
[rs_num
].inactive
.ptr
,
2814 ruleset
->rules
[rs_num
].inactive
.rcount
++;
2815 if (rule
->rule_flag
& PFRULE_PFM
) {
2819 if (rule
->action
== PF_NAT64
) {
2820 atomic_add_16(&pf_nat64_configured
, 1);
2823 if (pr
->anchor_call
[0] == '\0') {
2824 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_pf_addrule_total
);
2825 if (rule
->rule_flag
& PFRULE_PFM
) {
2826 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_pf_addrule_os
);
2831 if (rule
->action
== PF_DUMMYNET
) {
2832 struct dummynet_event dn_event
;
2833 uint32_t direction
= DN_INOUT
;;
2834 bzero(&dn_event
, sizeof(dn_event
));
2836 dn_event
.dn_event_code
= DUMMYNET_RULE_CONFIG
;
2838 if (rule
->direction
== PF_IN
) {
2840 } else if (rule
->direction
== PF_OUT
) {
2844 dn_event
.dn_event_rule_config
.dir
= direction
;
2845 dn_event
.dn_event_rule_config
.af
= rule
->af
;
2846 dn_event
.dn_event_rule_config
.proto
= rule
->proto
;
2847 dn_event
.dn_event_rule_config
.src_port
= rule
->src
.xport
.range
.port
[0];
2848 dn_event
.dn_event_rule_config
.dst_port
= rule
->dst
.xport
.range
.port
[0];
2849 strlcpy(dn_event
.dn_event_rule_config
.ifname
, rule
->ifname
,
2850 sizeof(dn_event
.dn_event_rule_config
.ifname
));
2852 dummynet_event_enqueue_nwk_wq_entry(&dn_event
);
2858 case DIOCGETRULES
: {
2859 struct pf_ruleset
*ruleset
;
2860 struct pf_rule
*tail
;
2863 pr
->anchor
[sizeof(pr
->anchor
) - 1] = '\0';
2864 pr
->anchor_call
[sizeof(pr
->anchor_call
) - 1] = '\0';
2865 ruleset
= pf_find_ruleset(pr
->anchor
);
2866 if (ruleset
== NULL
) {
2870 rs_num
= pf_get_ruleset_number(pr
->rule
.action
);
2871 if (rs_num
>= PF_RULESET_MAX
) {
2875 tail
= TAILQ_LAST(ruleset
->rules
[rs_num
].active
.ptr
,
2878 pr
->nr
= tail
->nr
+ 1;
2882 pr
->ticket
= ruleset
->rules
[rs_num
].active
.ticket
;
2887 struct pf_ruleset
*ruleset
;
2888 struct pf_rule
*rule
;
2891 pr
->anchor
[sizeof(pr
->anchor
) - 1] = '\0';
2892 pr
->anchor_call
[sizeof(pr
->anchor_call
) - 1] = '\0';
2893 ruleset
= pf_find_ruleset(pr
->anchor
);
2894 if (ruleset
== NULL
) {
2898 rs_num
= pf_get_ruleset_number(pr
->rule
.action
);
2899 if (rs_num
>= PF_RULESET_MAX
) {
2903 if (pr
->ticket
!= ruleset
->rules
[rs_num
].active
.ticket
) {
2907 rule
= TAILQ_FIRST(ruleset
->rules
[rs_num
].active
.ptr
);
2908 while ((rule
!= NULL
) && (rule
->nr
!= pr
->nr
)) {
2909 rule
= TAILQ_NEXT(rule
, entries
);
2915 pf_rule_copyout(rule
, &pr
->rule
);
2916 if (pf_anchor_copyout(ruleset
, rule
, pr
)) {
2920 pfi_dynaddr_copyout(&pr
->rule
.src
.addr
);
2921 pfi_dynaddr_copyout(&pr
->rule
.dst
.addr
);
2922 pf_tbladdr_copyout(&pr
->rule
.src
.addr
);
2923 pf_tbladdr_copyout(&pr
->rule
.dst
.addr
);
2924 pf_rtlabel_copyout(&pr
->rule
.src
.addr
);
2925 pf_rtlabel_copyout(&pr
->rule
.dst
.addr
);
2926 for (i
= 0; i
< PF_SKIP_COUNT
; ++i
) {
2927 if (rule
->skip
[i
].ptr
== NULL
) {
2928 pr
->rule
.skip
[i
].nr
= -1;
2930 pr
->rule
.skip
[i
].nr
=
2931 rule
->skip
[i
].ptr
->nr
;
2935 if (pr
->action
== PF_GET_CLR_CNTR
) {
2936 rule
->evaluations
= 0;
2937 rule
->packets
[0] = rule
->packets
[1] = 0;
2938 rule
->bytes
[0] = rule
->bytes
[1] = 0;
2943 case DIOCCHANGERULE
: {
2944 struct pfioc_rule
*pcr
= pr
;
2945 struct pf_ruleset
*ruleset
;
2946 struct pf_rule
*oldrule
= NULL
, *newrule
= NULL
;
2947 struct pf_pooladdr
*pa
;
2951 if (!(pcr
->action
== PF_CHANGE_REMOVE
||
2952 pcr
->action
== PF_CHANGE_GET_TICKET
) &&
2953 pcr
->pool_ticket
!= ticket_pabuf
) {
2958 if (pcr
->action
< PF_CHANGE_ADD_HEAD
||
2959 pcr
->action
> PF_CHANGE_GET_TICKET
) {
2963 pcr
->anchor
[sizeof(pcr
->anchor
) - 1] = '\0';
2964 pcr
->anchor_call
[sizeof(pcr
->anchor_call
) - 1] = '\0';
2965 ruleset
= pf_find_ruleset(pcr
->anchor
);
2966 if (ruleset
== NULL
) {
2970 rs_num
= pf_get_ruleset_number(pcr
->rule
.action
);
2971 if (rs_num
>= PF_RULESET_MAX
) {
2976 if (pcr
->action
== PF_CHANGE_GET_TICKET
) {
2977 pcr
->ticket
= ++ruleset
->rules
[rs_num
].active
.ticket
;
2981 ruleset
->rules
[rs_num
].active
.ticket
) {
2985 if (pcr
->rule
.return_icmp
>> 8 > ICMP_MAXTYPE
) {
2991 if (pcr
->action
!= PF_CHANGE_REMOVE
) {
2992 newrule
= pool_get(&pf_rule_pl
, PR_WAITOK
);
2993 if (newrule
== NULL
) {
2997 pf_rule_copyin(&pcr
->rule
, newrule
, p
, minordev
);
2999 if (newrule
->af
== AF_INET
) {
3000 pool_put(&pf_rule_pl
, newrule
);
3001 error
= EAFNOSUPPORT
;
3006 if (newrule
->af
== AF_INET6
) {
3007 pool_put(&pf_rule_pl
, newrule
);
3008 error
= EAFNOSUPPORT
;
3012 if (newrule
->ifname
[0]) {
3013 newrule
->kif
= pfi_kif_get(newrule
->ifname
);
3014 if (newrule
->kif
== NULL
) {
3015 pool_put(&pf_rule_pl
, newrule
);
3019 pfi_kif_ref(newrule
->kif
, PFI_KIF_REF_RULE
);
3021 newrule
->kif
= NULL
;
3024 if (newrule
->tagname
[0]) {
3026 pf_tagname2tag(newrule
->tagname
)) == 0) {
3030 if (newrule
->match_tagname
[0]) {
3031 if ((newrule
->match_tag
= pf_tagname2tag(
3032 newrule
->match_tagname
)) == 0) {
3036 if (newrule
->rt
&& !newrule
->direction
) {
3040 if (!newrule
->log
) {
3043 if (newrule
->logif
>= PFLOGIFS_MAX
) {
3047 pf_addrwrap_setup(&newrule
->src
.addr
);
3048 pf_addrwrap_setup(&newrule
->dst
.addr
);
3049 if (pf_rtlabel_add(&newrule
->src
.addr
) ||
3050 pf_rtlabel_add(&newrule
->dst
.addr
)) {
3053 if (pfi_dynaddr_setup(&newrule
->src
.addr
, newrule
->af
)) {
3056 if (pfi_dynaddr_setup(&newrule
->dst
.addr
, newrule
->af
)) {
3059 if (pf_tbladdr_setup(ruleset
, &newrule
->src
.addr
)) {
3062 if (pf_tbladdr_setup(ruleset
, &newrule
->dst
.addr
)) {
3065 if (pf_anchor_setup(newrule
, ruleset
, pcr
->anchor_call
)) {
3068 TAILQ_FOREACH(pa
, &pf_pabuf
, entries
)
3069 if (pf_tbladdr_setup(ruleset
, &pa
->addr
)) {
3073 if (newrule
->overload_tblname
[0]) {
3074 if ((newrule
->overload_tbl
= pfr_attach_table(
3075 ruleset
, newrule
->overload_tblname
)) ==
3079 newrule
->overload_tbl
->pfrkt_flags
|=
3084 pf_mv_pool(&pf_pabuf
, &newrule
->rpool
.list
);
3085 if (((((newrule
->action
== PF_NAT
) ||
3086 (newrule
->action
== PF_RDR
) ||
3087 (newrule
->action
== PF_BINAT
) ||
3088 (newrule
->rt
> PF_FASTROUTE
)) &&
3089 !newrule
->anchor
)) &&
3090 (TAILQ_FIRST(&newrule
->rpool
.list
) == NULL
)) {
3095 pf_rm_rule(NULL
, newrule
);
3098 newrule
->rpool
.cur
= TAILQ_FIRST(&newrule
->rpool
.list
);
3099 newrule
->evaluations
= 0;
3100 newrule
->packets
[0] = newrule
->packets
[1] = 0;
3101 newrule
->bytes
[0] = newrule
->bytes
[1] = 0;
3103 pf_empty_pool(&pf_pabuf
);
3105 if (pcr
->action
== PF_CHANGE_ADD_HEAD
) {
3106 oldrule
= TAILQ_FIRST(
3107 ruleset
->rules
[rs_num
].active
.ptr
);
3108 } else if (pcr
->action
== PF_CHANGE_ADD_TAIL
) {
3109 oldrule
= TAILQ_LAST(
3110 ruleset
->rules
[rs_num
].active
.ptr
, pf_rulequeue
);
3112 oldrule
= TAILQ_FIRST(
3113 ruleset
->rules
[rs_num
].active
.ptr
);
3114 while ((oldrule
!= NULL
) && (oldrule
->nr
!= pcr
->nr
)) {
3115 oldrule
= TAILQ_NEXT(oldrule
, entries
);
3117 if (oldrule
== NULL
) {
3118 if (newrule
!= NULL
) {
3119 pf_rm_rule(NULL
, newrule
);
3126 if (pcr
->action
== PF_CHANGE_REMOVE
) {
3127 pf_rm_rule(ruleset
->rules
[rs_num
].active
.ptr
, oldrule
);
3128 ruleset
->rules
[rs_num
].active
.rcount
--;
3130 if (oldrule
== NULL
) {
3132 ruleset
->rules
[rs_num
].active
.ptr
,
3134 } else if (pcr
->action
== PF_CHANGE_ADD_HEAD
||
3135 pcr
->action
== PF_CHANGE_ADD_BEFORE
) {
3136 TAILQ_INSERT_BEFORE(oldrule
, newrule
, entries
);
3139 ruleset
->rules
[rs_num
].active
.ptr
,
3140 oldrule
, newrule
, entries
);
3142 ruleset
->rules
[rs_num
].active
.rcount
++;
3146 TAILQ_FOREACH(oldrule
,
3147 ruleset
->rules
[rs_num
].active
.ptr
, entries
)
3150 ruleset
->rules
[rs_num
].active
.ticket
++;
3152 pf_calc_skip_steps(ruleset
->rules
[rs_num
].active
.ptr
);
3153 pf_remove_if_empty_ruleset(ruleset
);
3158 case DIOCINSERTRULE
: {
3159 struct pf_ruleset
*ruleset
;
3160 struct pf_rule
*rule
, *tail
, *r
;
3164 pr
->anchor
[sizeof(pr
->anchor
) - 1] = '\0';
3165 pr
->anchor_call
[sizeof(pr
->anchor_call
) - 1] = '\0';
3166 is_anchor
= (pr
->anchor_call
[0] != '\0');
3168 if ((ruleset
= pf_find_ruleset_with_owner(pr
->anchor
,
3169 pr
->rule
.owner
, is_anchor
, &error
)) == NULL
) {
3173 rs_num
= pf_get_ruleset_number(pr
->rule
.action
);
3174 if (rs_num
>= PF_RULESET_MAX
) {
3178 if (pr
->rule
.return_icmp
>> 8 > ICMP_MAXTYPE
) {
3183 /* make sure this anchor rule doesn't exist already */
3185 r
= TAILQ_FIRST(ruleset
->rules
[rs_num
].active
.ptr
);
3188 ((strcmp(r
->anchor
->name
,
3189 pr
->anchor_call
)) == 0)) {
3190 if (((strcmp(pr
->rule
.owner
,
3192 ((strcmp(r
->owner
, "")) == 0)) {
3199 r
= TAILQ_NEXT(r
, entries
);
3206 rule
= pool_get(&pf_rule_pl
, PR_WAITOK
);
3211 pf_rule_copyin(&pr
->rule
, rule
, p
, minordev
);
3213 if (rule
->af
== AF_INET
) {
3214 pool_put(&pf_rule_pl
, rule
);
3215 error
= EAFNOSUPPORT
;
3220 if (rule
->af
== AF_INET6
) {
3221 pool_put(&pf_rule_pl
, rule
);
3222 error
= EAFNOSUPPORT
;
3227 r
= TAILQ_FIRST(ruleset
->rules
[rs_num
].active
.ptr
);
3228 while ((r
!= NULL
) && (rule
->priority
>= (unsigned)r
->priority
)) {
3229 r
= TAILQ_NEXT(r
, entries
);
3233 TAILQ_LAST(ruleset
->rules
[rs_num
].active
.ptr
,
3234 pf_rulequeue
)) != NULL
) {
3235 rule
->nr
= tail
->nr
+ 1;
3243 if ((error
= pf_rule_setup(pr
, rule
, ruleset
))) {
3247 if (rule
->anchor
!= NULL
) {
3248 strlcpy(rule
->anchor
->owner
, rule
->owner
,
3249 PF_OWNER_NAME_SIZE
);
3253 TAILQ_INSERT_BEFORE(r
, rule
, entries
);
3254 while (r
&& ++r
->nr
) {
3255 r
= TAILQ_NEXT(r
, entries
);
3258 TAILQ_INSERT_TAIL(ruleset
->rules
[rs_num
].active
.ptr
,
3261 ruleset
->rules
[rs_num
].active
.rcount
++;
3263 /* Calculate checksum for the main ruleset */
3264 if (ruleset
== &pf_main_ruleset
) {
3265 error
= pf_setup_pfsync_matching(ruleset
);
3268 pf_ruleset_cleanup(ruleset
, rs_num
);
3269 rule
->ticket
= VM_KERNEL_ADDRPERM((u_int64_t
)(uintptr_t)rule
);
3271 pr
->rule
.ticket
= rule
->ticket
;
3272 pf_rule_copyout(rule
, &pr
->rule
);
3273 if (rule
->rule_flag
& PFRULE_PFM
) {
3276 if (rule
->action
== PF_NAT64
) {
3277 atomic_add_16(&pf_nat64_configured
, 1);
3280 if (pr
->anchor_call
[0] == '\0') {
3281 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_pf_addrule_total
);
3282 if (rule
->rule_flag
& PFRULE_PFM
) {
3283 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_pf_addrule_os
);
3289 case DIOCDELETERULE
: {
3290 pr
->anchor
[sizeof(pr
->anchor
) - 1] = '\0';
3291 pr
->anchor_call
[sizeof(pr
->anchor_call
) - 1] = '\0';
3293 if (pr
->rule
.return_icmp
>> 8 > ICMP_MAXTYPE
) {
3298 /* get device through which request is made */
3299 if ((uint8_t)minordev
== PFDEV_PFM
) {
3300 req_dev
|= PFRULE_PFM
;
3303 if (pr
->rule
.ticket
) {
3304 if ((error
= pf_delete_rule_by_ticket(pr
, req_dev
))) {
3308 pf_delete_rule_by_owner(pr
->rule
.owner
, req_dev
);
3311 if (pr
->rule
.action
== PF_NAT64
) {
3312 atomic_add_16(&pf_nat64_configured
, -1);
3326 pfioctl_ioc_state_kill(u_long cmd
, struct pfioc_state_kill
*psk
, struct proc
*p
)
3331 psk
->psk_ifname
[sizeof(psk
->psk_ifname
) - 1] = '\0';
3332 psk
->psk_ownername
[sizeof(psk
->psk_ownername
) - 1] = '\0';
3334 bool ifname_matched
= true;
3335 bool owner_matched
= true;
3338 case DIOCCLRSTATES
: {
3339 struct pf_state
*s
, *nexts
;
3342 for (s
= RB_MIN(pf_state_tree_id
, &tree_id
); s
; s
= nexts
) {
3343 nexts
= RB_NEXT(pf_state_tree_id
, &tree_id
, s
);
3345 * Purge all states only when neither ifname
3346 * or owner is provided. If any of these are provided
3347 * we purge only the states with meta data that match
3349 bool unlink_state
= false;
3350 ifname_matched
= true;
3351 owner_matched
= true;
3353 if (psk
->psk_ifname
[0] &&
3354 strcmp(psk
->psk_ifname
, s
->kif
->pfik_name
)) {
3355 ifname_matched
= false;
3358 if (psk
->psk_ownername
[0] &&
3359 ((NULL
== s
->rule
.ptr
) ||
3360 strcmp(psk
->psk_ownername
, s
->rule
.ptr
->owner
))) {
3361 owner_matched
= false;
3364 unlink_state
= ifname_matched
&& owner_matched
;
3368 /* don't send out individual delete messages */
3369 s
->sync_flags
= PFSTATE_NOSYNC
;
3375 psk
->psk_af
= killed
;
3377 pfsync_clear_states(pf_status
.hostid
, psk
->psk_ifname
);
3382 case DIOCKILLSTATES
: {
3383 struct pf_state
*s
, *nexts
;
3384 struct pf_state_key
*sk
;
3385 struct pf_state_host
*src
, *dst
;
3388 for (s
= RB_MIN(pf_state_tree_id
, &tree_id
); s
;
3390 nexts
= RB_NEXT(pf_state_tree_id
, &tree_id
, s
);
3392 ifname_matched
= true;
3393 owner_matched
= true;
3395 if (psk
->psk_ifname
[0] &&
3396 strcmp(psk
->psk_ifname
, s
->kif
->pfik_name
)) {
3397 ifname_matched
= false;
3400 if (psk
->psk_ownername
[0] &&
3401 ((NULL
== s
->rule
.ptr
) ||
3402 strcmp(psk
->psk_ownername
, s
->rule
.ptr
->owner
))) {
3403 owner_matched
= false;
3406 if (sk
->direction
== PF_OUT
) {
3413 if ((!psk
->psk_af
|| sk
->af_lan
== psk
->psk_af
) &&
3414 (!psk
->psk_proto
|| psk
->psk_proto
== sk
->proto
) &&
3415 PF_MATCHA(psk
->psk_src
.neg
,
3416 &psk
->psk_src
.addr
.v
.a
.addr
,
3417 &psk
->psk_src
.addr
.v
.a
.mask
,
3418 &src
->addr
, sk
->af_lan
) &&
3419 PF_MATCHA(psk
->psk_dst
.neg
,
3420 &psk
->psk_dst
.addr
.v
.a
.addr
,
3421 &psk
->psk_dst
.addr
.v
.a
.mask
,
3422 &dst
->addr
, sk
->af_lan
) &&
3423 (pf_match_xport(psk
->psk_proto
,
3424 psk
->psk_proto_variant
, &psk
->psk_src
.xport
,
3426 (pf_match_xport(psk
->psk_proto
,
3427 psk
->psk_proto_variant
, &psk
->psk_dst
.xport
,
3432 /* send immediate delete of state */
3433 pfsync_delete_state(s
);
3434 s
->sync_flags
|= PFSTATE_NOSYNC
;
3440 psk
->psk_af
= killed
;
3453 pfioctl_ioc_state(u_long cmd
, struct pfioc_state
*ps
, struct proc
*p
)
3459 case DIOCADDSTATE
: {
3460 struct pfsync_state
*sp
= &ps
->state
;
3462 struct pf_state_key
*sk
;
3463 struct pfi_kif
*kif
;
3465 if (sp
->timeout
>= PFTM_MAX
) {
3469 s
= pool_get(&pf_state_pl
, PR_WAITOK
);
3474 bzero(s
, sizeof(struct pf_state
));
3475 if ((sk
= pf_alloc_state_key(s
, NULL
)) == NULL
) {
3476 pool_put(&pf_state_pl
, s
);
3480 pf_state_import(sp
, sk
, s
);
3481 kif
= pfi_kif_get(sp
->ifname
);
3483 pool_put(&pf_state_pl
, s
);
3484 pool_put(&pf_state_key_pl
, sk
);
3488 TAILQ_INIT(&s
->unlink_hooks
);
3489 s
->state_key
->app_state
= 0;
3490 if (pf_insert_state(kif
, s
)) {
3491 pfi_kif_unref(kif
, PFI_KIF_REF_NONE
);
3492 pool_put(&pf_state_pl
, s
);
3496 pf_default_rule
.states
++;
3497 VERIFY(pf_default_rule
.states
!= 0);
3501 case DIOCGETSTATE
: {
3503 struct pf_state_cmp id_key
;
3505 bcopy(ps
->state
.id
, &id_key
.id
, sizeof(id_key
.id
));
3506 id_key
.creatorid
= ps
->state
.creatorid
;
3508 s
= pf_find_state_byid(&id_key
);
3514 pf_state_export(&ps
->state
, s
->state_key
, s
);
3527 pfioctl_ioc_states(u_long cmd
, struct pfioc_states_32
*ps32
,
3528 struct pfioc_states_64
*ps64
, struct proc
*p
)
3530 int p64
= proc_is64bit(p
);
3534 case DIOCGETSTATES
: { /* struct pfioc_states */
3535 struct pf_state
*state
;
3536 struct pfsync_state
*pstore
;
3541 len
= (p64
? ps64
->ps_len
: ps32
->ps_len
);
3543 size
= sizeof(struct pfsync_state
) * pf_status
.states
;
3545 ps64
->ps_len
= size
;
3547 ps32
->ps_len
= size
;
3552 pstore
= _MALLOC(sizeof(*pstore
), M_TEMP
, M_WAITOK
| M_ZERO
);
3553 if (pstore
== NULL
) {
3557 buf
= (p64
? ps64
->ps_buf
: ps32
->ps_buf
);
3559 state
= TAILQ_FIRST(&state_list
);
3561 if (state
->timeout
!= PFTM_UNLINKED
) {
3562 if ((nr
+ 1) * sizeof(*pstore
) > (unsigned)len
) {
3566 pf_state_export(pstore
,
3567 state
->state_key
, state
);
3568 error
= copyout(pstore
, buf
, sizeof(*pstore
));
3570 _FREE(pstore
, M_TEMP
);
3573 buf
+= sizeof(*pstore
);
3576 state
= TAILQ_NEXT(state
, entry_list
);
3579 size
= sizeof(struct pfsync_state
) * nr
;
3581 ps64
->ps_len
= size
;
3583 ps32
->ps_len
= size
;
3586 _FREE(pstore
, M_TEMP
);
3599 pfioctl_ioc_natlook(u_long cmd
, struct pfioc_natlook
*pnl
, struct proc
*p
)
3606 struct pf_state_key
*sk
;
3607 struct pf_state
*state
;
3608 struct pf_state_key_cmp key
;
3609 int m
= 0, direction
= pnl
->direction
;
3611 key
.proto
= pnl
->proto
;
3612 key
.proto_variant
= pnl
->proto_variant
;
3615 PF_AZERO(&pnl
->saddr
, pnl
->af
) ||
3616 PF_AZERO(&pnl
->daddr
, pnl
->af
) ||
3617 ((pnl
->proto
== IPPROTO_TCP
||
3618 pnl
->proto
== IPPROTO_UDP
) &&
3619 (!pnl
->dxport
.port
|| !pnl
->sxport
.port
))) {
3623 * userland gives us source and dest of connection,
3624 * reverse the lookup so we ask for what happens with
3625 * the return traffic, enabling us to find it in the
3628 if (direction
== PF_IN
) {
3629 key
.af_gwy
= pnl
->af
;
3630 PF_ACPY(&key
.ext_gwy
.addr
, &pnl
->daddr
,
3632 memcpy(&key
.ext_gwy
.xport
, &pnl
->dxport
,
3633 sizeof(key
.ext_gwy
.xport
));
3634 PF_ACPY(&key
.gwy
.addr
, &pnl
->saddr
, pnl
->af
);
3635 memcpy(&key
.gwy
.xport
, &pnl
->sxport
,
3636 sizeof(key
.gwy
.xport
));
3637 state
= pf_find_state_all(&key
, PF_IN
, &m
);
3639 key
.af_lan
= pnl
->af
;
3640 PF_ACPY(&key
.lan
.addr
, &pnl
->daddr
, pnl
->af
);
3641 memcpy(&key
.lan
.xport
, &pnl
->dxport
,
3642 sizeof(key
.lan
.xport
));
3643 PF_ACPY(&key
.ext_lan
.addr
, &pnl
->saddr
,
3645 memcpy(&key
.ext_lan
.xport
, &pnl
->sxport
,
3646 sizeof(key
.ext_lan
.xport
));
3647 state
= pf_find_state_all(&key
, PF_OUT
, &m
);
3650 error
= E2BIG
; /* more than one state */
3651 } else if (state
!= NULL
) {
3652 sk
= state
->state_key
;
3653 if (direction
== PF_IN
) {
3654 PF_ACPY(&pnl
->rsaddr
, &sk
->lan
.addr
,
3656 memcpy(&pnl
->rsxport
, &sk
->lan
.xport
,
3657 sizeof(pnl
->rsxport
));
3658 PF_ACPY(&pnl
->rdaddr
, &pnl
->daddr
,
3660 memcpy(&pnl
->rdxport
, &pnl
->dxport
,
3661 sizeof(pnl
->rdxport
));
3663 PF_ACPY(&pnl
->rdaddr
, &sk
->gwy
.addr
,
3665 memcpy(&pnl
->rdxport
, &sk
->gwy
.xport
,
3666 sizeof(pnl
->rdxport
));
3667 PF_ACPY(&pnl
->rsaddr
, &pnl
->saddr
,
3669 memcpy(&pnl
->rsxport
, &pnl
->sxport
,
3670 sizeof(pnl
->rsxport
));
3688 pfioctl_ioc_tm(u_long cmd
, struct pfioc_tm
*pt
, struct proc
*p
)
3694 case DIOCSETTIMEOUT
: {
3697 if (pt
->timeout
< 0 || pt
->timeout
>= PFTM_MAX
||
3702 old
= pf_default_rule
.timeout
[pt
->timeout
];
3703 if (pt
->timeout
== PFTM_INTERVAL
&& pt
->seconds
== 0) {
3706 pf_default_rule
.timeout
[pt
->timeout
] = pt
->seconds
;
3707 if (pt
->timeout
== PFTM_INTERVAL
&& pt
->seconds
< old
) {
3708 wakeup(pf_purge_thread_fn
);
3714 case DIOCGETTIMEOUT
: {
3715 if (pt
->timeout
< 0 || pt
->timeout
>= PFTM_MAX
) {
3719 pt
->seconds
= pf_default_rule
.timeout
[pt
->timeout
];
3732 pfioctl_ioc_limit(u_long cmd
, struct pfioc_limit
*pl
, struct proc
*p
)
3738 case DIOCGETLIMIT
: {
3739 if (pl
->index
< 0 || pl
->index
>= PF_LIMIT_MAX
) {
3743 pl
->limit
= pf_pool_limits
[pl
->index
].limit
;
3747 case DIOCSETLIMIT
: {
3750 if (pl
->index
< 0 || pl
->index
>= PF_LIMIT_MAX
||
3751 pf_pool_limits
[pl
->index
].pp
== NULL
) {
3755 pool_sethardlimit(pf_pool_limits
[pl
->index
].pp
,
3756 pl
->limit
, NULL
, 0);
3757 old_limit
= pf_pool_limits
[pl
->index
].limit
;
3758 pf_pool_limits
[pl
->index
].limit
= pl
->limit
;
3759 pl
->limit
= old_limit
;
3772 pfioctl_ioc_pooladdr(u_long cmd
, struct pfioc_pooladdr
*pp
, struct proc
*p
)
3775 struct pf_pooladdr
*pa
= NULL
;
3776 struct pf_pool
*pool
= NULL
;
3780 case DIOCBEGINADDRS
: {
3781 pf_empty_pool(&pf_pabuf
);
3782 pp
->ticket
= ++ticket_pabuf
;
3787 pp
->anchor
[sizeof(pp
->anchor
) - 1] = '\0';
3788 if (pp
->ticket
!= ticket_pabuf
) {
3793 if (pp
->af
== AF_INET
) {
3794 error
= EAFNOSUPPORT
;
3799 if (pp
->af
== AF_INET6
) {
3800 error
= EAFNOSUPPORT
;
3804 if (pp
->addr
.addr
.type
!= PF_ADDR_ADDRMASK
&&
3805 pp
->addr
.addr
.type
!= PF_ADDR_DYNIFTL
&&
3806 pp
->addr
.addr
.type
!= PF_ADDR_TABLE
) {
3810 pa
= pool_get(&pf_pooladdr_pl
, PR_WAITOK
);
3815 pf_pooladdr_copyin(&pp
->addr
, pa
);
3816 if (pa
->ifname
[0]) {
3817 pa
->kif
= pfi_kif_get(pa
->ifname
);
3818 if (pa
->kif
== NULL
) {
3819 pool_put(&pf_pooladdr_pl
, pa
);
3823 pfi_kif_ref(pa
->kif
, PFI_KIF_REF_RULE
);
3825 pf_addrwrap_setup(&pa
->addr
);
3826 if (pfi_dynaddr_setup(&pa
->addr
, pp
->af
)) {
3827 pfi_dynaddr_remove(&pa
->addr
);
3828 pfi_kif_unref(pa
->kif
, PFI_KIF_REF_RULE
);
3829 pool_put(&pf_pooladdr_pl
, pa
);
3833 TAILQ_INSERT_TAIL(&pf_pabuf
, pa
, entries
);
3837 case DIOCGETADDRS
: {
3839 pp
->anchor
[sizeof(pp
->anchor
) - 1] = '\0';
3840 pool
= pf_get_pool(pp
->anchor
, pp
->ticket
, pp
->r_action
,
3841 pp
->r_num
, 0, 1, 0);
3846 TAILQ_FOREACH(pa
, &pool
->list
, entries
)
3854 pp
->anchor
[sizeof(pp
->anchor
) - 1] = '\0';
3855 pool
= pf_get_pool(pp
->anchor
, pp
->ticket
, pp
->r_action
,
3856 pp
->r_num
, 0, 1, 1);
3861 pa
= TAILQ_FIRST(&pool
->list
);
3862 while ((pa
!= NULL
) && (nr
< pp
->nr
)) {
3863 pa
= TAILQ_NEXT(pa
, entries
);
3870 pf_pooladdr_copyout(pa
, &pp
->addr
);
3871 pfi_dynaddr_copyout(&pp
->addr
.addr
);
3872 pf_tbladdr_copyout(&pp
->addr
.addr
);
3873 pf_rtlabel_copyout(&pp
->addr
.addr
);
3877 case DIOCCHANGEADDR
: {
3878 struct pfioc_pooladdr
*pca
= pp
;
3879 struct pf_pooladdr
*oldpa
= NULL
, *newpa
= NULL
;
3880 struct pf_ruleset
*ruleset
;
3882 if (pca
->action
< PF_CHANGE_ADD_HEAD
||
3883 pca
->action
> PF_CHANGE_REMOVE
) {
3887 if (pca
->addr
.addr
.type
!= PF_ADDR_ADDRMASK
&&
3888 pca
->addr
.addr
.type
!= PF_ADDR_DYNIFTL
&&
3889 pca
->addr
.addr
.type
!= PF_ADDR_TABLE
) {
3894 pca
->anchor
[sizeof(pca
->anchor
) - 1] = '\0';
3895 ruleset
= pf_find_ruleset(pca
->anchor
);
3896 if (ruleset
== NULL
) {
3900 pool
= pf_get_pool(pca
->anchor
, pca
->ticket
, pca
->r_action
,
3901 pca
->r_num
, pca
->r_last
, 1, 1);
3906 if (pca
->action
!= PF_CHANGE_REMOVE
) {
3907 newpa
= pool_get(&pf_pooladdr_pl
, PR_WAITOK
);
3908 if (newpa
== NULL
) {
3912 pf_pooladdr_copyin(&pca
->addr
, newpa
);
3914 if (pca
->af
== AF_INET
) {
3915 pool_put(&pf_pooladdr_pl
, newpa
);
3916 error
= EAFNOSUPPORT
;
3921 if (pca
->af
== AF_INET6
) {
3922 pool_put(&pf_pooladdr_pl
, newpa
);
3923 error
= EAFNOSUPPORT
;
3927 if (newpa
->ifname
[0]) {
3928 newpa
->kif
= pfi_kif_get(newpa
->ifname
);
3929 if (newpa
->kif
== NULL
) {
3930 pool_put(&pf_pooladdr_pl
, newpa
);
3934 pfi_kif_ref(newpa
->kif
, PFI_KIF_REF_RULE
);
3938 pf_addrwrap_setup(&newpa
->addr
);
3939 if (pfi_dynaddr_setup(&newpa
->addr
, pca
->af
) ||
3940 pf_tbladdr_setup(ruleset
, &newpa
->addr
)) {
3941 pfi_dynaddr_remove(&newpa
->addr
);
3942 pfi_kif_unref(newpa
->kif
, PFI_KIF_REF_RULE
);
3943 pool_put(&pf_pooladdr_pl
, newpa
);
3949 if (pca
->action
== PF_CHANGE_ADD_HEAD
) {
3950 oldpa
= TAILQ_FIRST(&pool
->list
);
3951 } else if (pca
->action
== PF_CHANGE_ADD_TAIL
) {
3952 oldpa
= TAILQ_LAST(&pool
->list
, pf_palist
);
3956 oldpa
= TAILQ_FIRST(&pool
->list
);
3957 while ((oldpa
!= NULL
) && (i
< (int)pca
->nr
)) {
3958 oldpa
= TAILQ_NEXT(oldpa
, entries
);
3961 if (oldpa
== NULL
) {
3967 if (pca
->action
== PF_CHANGE_REMOVE
) {
3968 TAILQ_REMOVE(&pool
->list
, oldpa
, entries
);
3969 pfi_dynaddr_remove(&oldpa
->addr
);
3970 pf_tbladdr_remove(&oldpa
->addr
);
3971 pfi_kif_unref(oldpa
->kif
, PFI_KIF_REF_RULE
);
3972 pool_put(&pf_pooladdr_pl
, oldpa
);
3974 if (oldpa
== NULL
) {
3975 TAILQ_INSERT_TAIL(&pool
->list
, newpa
, entries
);
3976 } else if (pca
->action
== PF_CHANGE_ADD_HEAD
||
3977 pca
->action
== PF_CHANGE_ADD_BEFORE
) {
3978 TAILQ_INSERT_BEFORE(oldpa
, newpa
, entries
);
3980 TAILQ_INSERT_AFTER(&pool
->list
, oldpa
,
3985 pool
->cur
= TAILQ_FIRST(&pool
->list
);
3986 PF_ACPY(&pool
->counter
, &pool
->cur
->addr
.v
.a
.addr
,
4000 pfioctl_ioc_ruleset(u_long cmd
, struct pfioc_ruleset
*pr
, struct proc
*p
)
4006 case DIOCGETRULESETS
: {
4007 struct pf_ruleset
*ruleset
;
4008 struct pf_anchor
*anchor
;
4010 pr
->path
[sizeof(pr
->path
) - 1] = '\0';
4011 pr
->name
[sizeof(pr
->name
) - 1] = '\0';
4012 if ((ruleset
= pf_find_ruleset(pr
->path
)) == NULL
) {
4017 if (ruleset
->anchor
== NULL
) {
4018 /* XXX kludge for pf_main_ruleset */
4019 RB_FOREACH(anchor
, pf_anchor_global
, &pf_anchors
)
4020 if (anchor
->parent
== NULL
) {
4024 RB_FOREACH(anchor
, pf_anchor_node
,
4025 &ruleset
->anchor
->children
)
4031 case DIOCGETRULESET
: {
4032 struct pf_ruleset
*ruleset
;
4033 struct pf_anchor
*anchor
;
4036 pr
->path
[sizeof(pr
->path
) - 1] = '\0';
4037 if ((ruleset
= pf_find_ruleset(pr
->path
)) == NULL
) {
4042 if (ruleset
->anchor
== NULL
) {
4043 /* XXX kludge for pf_main_ruleset */
4044 RB_FOREACH(anchor
, pf_anchor_global
, &pf_anchors
)
4045 if (anchor
->parent
== NULL
&& nr
++ == pr
->nr
) {
4046 strlcpy(pr
->name
, anchor
->name
,
4051 RB_FOREACH(anchor
, pf_anchor_node
,
4052 &ruleset
->anchor
->children
)
4053 if (nr
++ == pr
->nr
) {
4054 strlcpy(pr
->name
, anchor
->name
,
4074 pfioctl_ioc_trans(u_long cmd
, struct pfioc_trans_32
*io32
,
4075 struct pfioc_trans_64
*io64
, struct proc
*p
)
4077 int p64
= proc_is64bit(p
);
4078 int error
= 0, esize
, size
;
4081 esize
= (p64
? io64
->esize
: io32
->esize
);
4082 size
= (p64
? io64
->size
: io32
->size
);
4083 buf
= (p64
? io64
->array
: io32
->array
);
4087 struct pfioc_trans_e
*ioe
;
4088 struct pfr_table
*table
;
4091 if (esize
!= sizeof(*ioe
)) {
4095 ioe
= _MALLOC(sizeof(*ioe
), M_TEMP
, M_WAITOK
);
4096 table
= _MALLOC(sizeof(*table
), M_TEMP
, M_WAITOK
);
4097 for (i
= 0; i
< size
; i
++, buf
+= sizeof(*ioe
)) {
4098 if (copyin(buf
, ioe
, sizeof(*ioe
))) {
4099 _FREE(table
, M_TEMP
);
4104 ioe
->anchor
[sizeof(ioe
->anchor
) - 1] = '\0';
4105 switch (ioe
->rs_num
) {
4106 case PF_RULESET_ALTQ
:
4108 case PF_RULESET_TABLE
:
4109 bzero(table
, sizeof(*table
));
4110 strlcpy(table
->pfrt_anchor
, ioe
->anchor
,
4111 sizeof(table
->pfrt_anchor
));
4112 if ((error
= pfr_ina_begin(table
,
4113 &ioe
->ticket
, NULL
, 0))) {
4114 _FREE(table
, M_TEMP
);
4120 if ((error
= pf_begin_rules(&ioe
->ticket
,
4121 ioe
->rs_num
, ioe
->anchor
))) {
4122 _FREE(table
, M_TEMP
);
4128 if (copyout(ioe
, buf
, sizeof(*ioe
))) {
4129 _FREE(table
, M_TEMP
);
4135 _FREE(table
, M_TEMP
);
4140 case DIOCXROLLBACK
: {
4141 struct pfioc_trans_e
*ioe
;
4142 struct pfr_table
*table
;
4145 if (esize
!= sizeof(*ioe
)) {
4149 ioe
= _MALLOC(sizeof(*ioe
), M_TEMP
, M_WAITOK
);
4150 table
= _MALLOC(sizeof(*table
), M_TEMP
, M_WAITOK
);
4151 for (i
= 0; i
< size
; i
++, buf
+= sizeof(*ioe
)) {
4152 if (copyin(buf
, ioe
, sizeof(*ioe
))) {
4153 _FREE(table
, M_TEMP
);
4158 ioe
->anchor
[sizeof(ioe
->anchor
) - 1] = '\0';
4159 switch (ioe
->rs_num
) {
4160 case PF_RULESET_ALTQ
:
4162 case PF_RULESET_TABLE
:
4163 bzero(table
, sizeof(*table
));
4164 strlcpy(table
->pfrt_anchor
, ioe
->anchor
,
4165 sizeof(table
->pfrt_anchor
));
4166 if ((error
= pfr_ina_rollback(table
,
4167 ioe
->ticket
, NULL
, 0))) {
4168 _FREE(table
, M_TEMP
);
4170 goto fail
; /* really bad */
4174 if ((error
= pf_rollback_rules(ioe
->ticket
,
4175 ioe
->rs_num
, ioe
->anchor
))) {
4176 _FREE(table
, M_TEMP
);
4178 goto fail
; /* really bad */
4183 _FREE(table
, M_TEMP
);
4189 struct pfioc_trans_e
*ioe
;
4190 struct pfr_table
*table
;
4191 struct pf_ruleset
*rs
;
4192 user_addr_t _buf
= buf
;
4195 if (esize
!= sizeof(*ioe
)) {
4199 ioe
= _MALLOC(sizeof(*ioe
), M_TEMP
, M_WAITOK
);
4200 table
= _MALLOC(sizeof(*table
), M_TEMP
, M_WAITOK
);
4201 /* first makes sure everything will succeed */
4202 for (i
= 0; i
< size
; i
++, buf
+= sizeof(*ioe
)) {
4203 if (copyin(buf
, ioe
, sizeof(*ioe
))) {
4204 _FREE(table
, M_TEMP
);
4209 ioe
->anchor
[sizeof(ioe
->anchor
) - 1] = '\0';
4210 switch (ioe
->rs_num
) {
4211 case PF_RULESET_ALTQ
:
4213 case PF_RULESET_TABLE
:
4214 rs
= pf_find_ruleset(ioe
->anchor
);
4215 if (rs
== NULL
|| !rs
->topen
|| ioe
->ticket
!=
4217 _FREE(table
, M_TEMP
);
4224 if (ioe
->rs_num
< 0 || ioe
->rs_num
>=
4226 _FREE(table
, M_TEMP
);
4231 rs
= pf_find_ruleset(ioe
->anchor
);
4233 !rs
->rules
[ioe
->rs_num
].inactive
.open
||
4234 rs
->rules
[ioe
->rs_num
].inactive
.ticket
!=
4236 _FREE(table
, M_TEMP
);
4245 /* now do the commit - no errors should happen here */
4246 for (i
= 0; i
< size
; i
++, buf
+= sizeof(*ioe
)) {
4247 if (copyin(buf
, ioe
, sizeof(*ioe
))) {
4248 _FREE(table
, M_TEMP
);
4253 ioe
->anchor
[sizeof(ioe
->anchor
) - 1] = '\0';
4254 switch (ioe
->rs_num
) {
4255 case PF_RULESET_ALTQ
:
4257 case PF_RULESET_TABLE
:
4258 bzero(table
, sizeof(*table
));
4259 strlcpy(table
->pfrt_anchor
, ioe
->anchor
,
4260 sizeof(table
->pfrt_anchor
));
4261 if ((error
= pfr_ina_commit(table
, ioe
->ticket
,
4263 _FREE(table
, M_TEMP
);
4265 goto fail
; /* really bad */
4269 if ((error
= pf_commit_rules(ioe
->ticket
,
4270 ioe
->rs_num
, ioe
->anchor
))) {
4271 _FREE(table
, M_TEMP
);
4273 goto fail
; /* really bad */
4278 _FREE(table
, M_TEMP
);
4292 pfioctl_ioc_src_nodes(u_long cmd
, struct pfioc_src_nodes_32
*psn32
,
4293 struct pfioc_src_nodes_64
*psn64
, struct proc
*p
)
4295 int p64
= proc_is64bit(p
);
4299 case DIOCGETSRCNODES
: {
4300 struct pf_src_node
*n
, *pstore
;
4305 space
= (p64
? psn64
->psn_len
: psn32
->psn_len
);
4307 RB_FOREACH(n
, pf_src_tree
, &tree_src_tracking
)
4310 size
= sizeof(struct pf_src_node
) * nr
;
4312 psn64
->psn_len
= size
;
4314 psn32
->psn_len
= size
;
4319 pstore
= _MALLOC(sizeof(*pstore
), M_TEMP
, M_WAITOK
);
4320 if (pstore
== NULL
) {
4324 buf
= (p64
? psn64
->psn_buf
: psn32
->psn_buf
);
4326 RB_FOREACH(n
, pf_src_tree
, &tree_src_tracking
) {
4327 uint64_t secs
= pf_time_second(), diff
;
4329 if ((nr
+ 1) * sizeof(*pstore
) > (unsigned)space
) {
4333 bcopy(n
, pstore
, sizeof(*pstore
));
4334 if (n
->rule
.ptr
!= NULL
) {
4335 pstore
->rule
.nr
= n
->rule
.ptr
->nr
;
4337 pstore
->creation
= secs
- pstore
->creation
;
4338 if (pstore
->expire
> secs
) {
4339 pstore
->expire
-= secs
;
4344 /* adjust the connection rate estimate */
4345 diff
= secs
- n
->conn_rate
.last
;
4346 if (diff
>= n
->conn_rate
.seconds
) {
4347 pstore
->conn_rate
.count
= 0;
4349 pstore
->conn_rate
.count
-=
4350 n
->conn_rate
.count
* diff
/
4351 n
->conn_rate
.seconds
;
4354 _RB_PARENT(pstore
, entry
) = NULL
;
4355 RB_LEFT(pstore
, entry
) = RB_RIGHT(pstore
, entry
) = NULL
;
4358 error
= copyout(pstore
, buf
, sizeof(*pstore
));
4360 _FREE(pstore
, M_TEMP
);
4363 buf
+= sizeof(*pstore
);
4367 size
= sizeof(struct pf_src_node
) * nr
;
4369 psn64
->psn_len
= size
;
4371 psn32
->psn_len
= size
;
4374 _FREE(pstore
, M_TEMP
);
4387 pfioctl_ioc_src_node_kill(u_long cmd
, struct pfioc_src_node_kill
*psnk
,
4394 case DIOCKILLSRCNODES
: {
4395 struct pf_src_node
*sn
;
4399 RB_FOREACH(sn
, pf_src_tree
, &tree_src_tracking
) {
4400 if (PF_MATCHA(psnk
->psnk_src
.neg
,
4401 &psnk
->psnk_src
.addr
.v
.a
.addr
,
4402 &psnk
->psnk_src
.addr
.v
.a
.mask
,
4403 &sn
->addr
, sn
->af
) &&
4404 PF_MATCHA(psnk
->psnk_dst
.neg
,
4405 &psnk
->psnk_dst
.addr
.v
.a
.addr
,
4406 &psnk
->psnk_dst
.addr
.v
.a
.mask
,
4407 &sn
->raddr
, sn
->af
)) {
4408 /* Handle state to src_node linkage */
4409 if (sn
->states
!= 0) {
4410 RB_FOREACH(s
, pf_state_tree_id
,
4412 if (s
->src_node
== sn
) {
4415 if (s
->nat_src_node
== sn
) {
4416 s
->nat_src_node
= NULL
;
4427 pf_purge_expired_src_nodes();
4430 psnk
->psnk_af
= killed
;
4443 pfioctl_ioc_iface(u_long cmd
, struct pfioc_iface_32
*io32
,
4444 struct pfioc_iface_64
*io64
, struct proc
*p
)
4446 int p64
= proc_is64bit(p
);
4450 case DIOCIGETIFACES
: {
4454 buf
= (p64
? io64
->pfiio_buffer
: io32
->pfiio_buffer
);
4455 esize
= (p64
? io64
->pfiio_esize
: io32
->pfiio_esize
);
4457 /* esize must be that of the user space version of pfi_kif */
4458 if (esize
!= sizeof(struct pfi_uif
)) {
4463 io64
->pfiio_name
[sizeof(io64
->pfiio_name
) - 1] = '\0';
4465 io32
->pfiio_name
[sizeof(io32
->pfiio_name
) - 1] = '\0';
4467 error
= pfi_get_ifaces(
4468 p64
? io64
->pfiio_name
: io32
->pfiio_name
, buf
,
4469 p64
? &io64
->pfiio_size
: &io32
->pfiio_size
);
4473 case DIOCSETIFFLAG
: {
4475 io64
->pfiio_name
[sizeof(io64
->pfiio_name
) - 1] = '\0';
4477 io32
->pfiio_name
[sizeof(io32
->pfiio_name
) - 1] = '\0';
4480 error
= pfi_set_flags(
4481 p64
? io64
->pfiio_name
: io32
->pfiio_name
,
4482 p64
? io64
->pfiio_flags
: io32
->pfiio_flags
);
4486 case DIOCCLRIFFLAG
: {
4488 io64
->pfiio_name
[sizeof(io64
->pfiio_name
) - 1] = '\0';
4490 io32
->pfiio_name
[sizeof(io32
->pfiio_name
) - 1] = '\0';
4493 error
= pfi_clear_flags(
4494 p64
? io64
->pfiio_name
: io32
->pfiio_name
,
4495 p64
? io64
->pfiio_flags
: io32
->pfiio_flags
);
4508 pf_af_hook(struct ifnet
*ifp
, struct mbuf
**mppn
, struct mbuf
**mp
,
4509 unsigned int af
, int input
, struct ip_fw_args
*fwa
)
4512 struct mbuf
*nextpkt
;
4513 net_thread_marks_t marks
;
4514 struct ifnet
* pf_ifp
= ifp
;
4516 /* Always allow traffic on co-processor interfaces. */
4517 if (!intcoproc_unrestricted
&& ifp
&& IFNET_IS_INTCOPROC(ifp
)) {
4521 marks
= net_thread_marks_push(NET_THREAD_HELD_PF
);
4523 if (marks
!= net_thread_marks_none
) {
4524 lck_rw_lock_shared(pf_perim_lock
);
4525 if (!pf_is_enabled
) {
4528 lck_mtx_lock(pf_lock
);
4531 if (mppn
!= NULL
&& *mppn
!= NULL
) {
4532 VERIFY(*mppn
== *mp
);
4534 if ((nextpkt
= (*mp
)->m_nextpkt
) != NULL
) {
4535 (*mp
)->m_nextpkt
= NULL
;
4539 * For packets destined to locally hosted IP address
4540 * ip_output_list sets Mbuf's pkt header's rcvif to
4541 * the interface hosting the IP address.
4542 * While on the output path ifp passed to pf_af_hook
4543 * to such local communication is the loopback interface,
4544 * the input path derives ifp from mbuf packet header's
4546 * This asymmetry caues issues with PF.
4547 * To handle that case, we have a limited change here to
4548 * pass interface as loopback if packets are looped in.
4550 if (input
&& ((*mp
)->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
4557 error
= pf_inet_hook(pf_ifp
, mp
, input
, fwa
);
4563 error
= pf_inet6_hook(pf_ifp
, mp
, input
, fwa
);
4570 /* When packet valid, link to the next packet */
4571 if (*mp
!= NULL
&& nextpkt
!= NULL
) {
4572 struct mbuf
*m
= *mp
;
4573 while (m
->m_nextpkt
!= NULL
) {
4576 m
->m_nextpkt
= nextpkt
;
4578 /* Fix up linkage of previous packet in the chain */
4587 if (marks
!= net_thread_marks_none
) {
4588 lck_mtx_unlock(pf_lock
);
4592 if (marks
!= net_thread_marks_none
) {
4593 lck_rw_done(pf_perim_lock
);
4596 net_thread_marks_pop(marks
);
4603 pf_inet_hook(struct ifnet
*ifp
, struct mbuf
**mp
, int input
,
4604 struct ip_fw_args
*fwa
)
4606 struct mbuf
*m
= *mp
;
4607 #if BYTE_ORDER != BIG_ENDIAN
4608 struct ip
*ip
= mtod(m
, struct ip
*);
4613 * If the packet is outbound, is originated locally, is flagged for
4614 * delayed UDP/TCP checksum calculation, and is about to be processed
4615 * for an interface that doesn't support the appropriate checksum
4616 * offloading, then calculated the checksum here so that PF can adjust
4619 if (!input
&& m
->m_pkthdr
.rcvif
== NULL
) {
4620 static const int mask
= CSUM_DELAY_DATA
;
4621 const int flags
= m
->m_pkthdr
.csum_flags
&
4622 ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
4625 in_delayed_cksum(m
);
4626 m
->m_pkthdr
.csum_flags
&= ~mask
;
4630 #if BYTE_ORDER != BIG_ENDIAN
4634 if (pf_test_mbuf(input
? PF_IN
: PF_OUT
, ifp
, mp
, NULL
, fwa
) != PF_PASS
) {
4638 error
= EHOSTUNREACH
;
4643 #if BYTE_ORDER != BIG_ENDIAN
4646 ip
= mtod(*mp
, struct ip
*);
4658 pf_inet6_hook(struct ifnet
*ifp
, struct mbuf
**mp
, int input
,
4659 struct ip_fw_args
*fwa
)
4664 * If the packet is outbound, is originated locally, is flagged for
4665 * delayed UDP/TCP checksum calculation, and is about to be processed
4666 * for an interface that doesn't support the appropriate checksum
4667 * offloading, then calculated the checksum here so that PF can adjust
4670 if (!input
&& (*mp
)->m_pkthdr
.rcvif
== NULL
) {
4671 static const int mask
= CSUM_DELAY_IPV6_DATA
;
4672 const int flags
= (*mp
)->m_pkthdr
.csum_flags
&
4673 ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
4677 * Checksum offload should not have been enabled
4678 * when extension headers exist, thus 0 for optlen.
4680 in6_delayed_cksum(*mp
);
4681 (*mp
)->m_pkthdr
.csum_flags
&= ~mask
;
4685 if (pf_test6_mbuf(input
? PF_IN
: PF_OUT
, ifp
, mp
, NULL
, fwa
) != PF_PASS
) {
4689 error
= EHOSTUNREACH
;
4699 pf_ifaddr_hook(struct ifnet
*ifp
)
4701 struct pfi_kif
*kif
= ifp
->if_pf_kif
;
4704 lck_rw_lock_shared(pf_perim_lock
);
4705 lck_mtx_lock(pf_lock
);
4707 pfi_kifaddr_update(kif
);
4709 lck_mtx_unlock(pf_lock
);
4710 lck_rw_done(pf_perim_lock
);
4716 * Caller acquires dlil lock as writer (exclusive)
4719 pf_ifnet_hook(struct ifnet
*ifp
, int attach
)
4721 lck_rw_lock_shared(pf_perim_lock
);
4722 lck_mtx_lock(pf_lock
);
4724 pfi_attach_ifnet(ifp
);
4726 pfi_detach_ifnet(ifp
);
4728 lck_mtx_unlock(pf_lock
);
4729 lck_rw_done(pf_perim_lock
);
4733 pf_attach_hooks(void)
4735 ifnet_head_lock_shared();
4737 * Check against ifnet_addrs[] before proceeding, in case this
4738 * is called very early on, e.g. during dlil_init() before any
4739 * network interface is attached.
4741 if (ifnet_addrs
!= NULL
) {
4744 for (i
= 0; i
<= if_index
; i
++) {
4745 struct ifnet
*ifp
= ifindex2ifnet
[i
];
4747 pfi_attach_ifnet(ifp
);
4755 /* currently unused along with pfdetach() */
4757 pf_detach_hooks(void)
4759 ifnet_head_lock_shared();
4760 if (ifnet_addrs
!= NULL
) {
4761 for (i
= 0; i
<= if_index
; i
++) {
4764 struct ifnet
*ifp
= ifindex2ifnet
[i
];
4765 if (ifp
!= NULL
&& ifp
->if_pf_kif
!= NULL
) {
4766 pfi_detach_ifnet(ifp
);
4777 * The switch statement below does nothing at runtime, as it serves as a
4778 * compile time check to ensure that all of the socket 'D' ioctls (those
4779 * in the 'D' group going thru soo_ioctl) that are made available by the
4780 * networking stack is unique. This works as long as this routine gets
4781 * updated each time a new interface ioctl gets added.
4783 * Any failures at compile time indicates duplicated ioctl values.
4785 static __attribute__((unused
)) void
4786 pfioctl_cassert(void)
4789 * This is equivalent to _CASSERT() and the compiler wouldn't
4790 * generate any instructions, thus for compile time only.
4792 switch ((u_long
)0) {
4795 /* bsd/net/pfvar.h */
4799 case DIOCGETSTARTERS
:
4806 case DIOCSETSTATUSIF
:
4812 case DIOCCHANGERULE
:
4813 case DIOCINSERTRULE
:
4814 case DIOCDELETERULE
:
4815 case DIOCSETTIMEOUT
:
4816 case DIOCGETTIMEOUT
:
4818 case DIOCCLRRULECTRS
:
4821 case DIOCKILLSTATES
:
4827 case DIOCCHANGEALTQ
:
4829 case DIOCBEGINADDRS
:
4833 case DIOCCHANGEADDR
:
4834 case DIOCGETRULESETS
:
4835 case DIOCGETRULESET
:
4836 case DIOCRCLRTABLES
:
4837 case DIOCRADDTABLES
:
4838 case DIOCRDELTABLES
:
4839 case DIOCRGETTABLES
:
4840 case DIOCRGETTSTATS
:
4841 case DIOCRCLRTSTATS
:
4847 case DIOCRGETASTATS
:
4848 case DIOCRCLRASTATS
:
4850 case DIOCRSETTFLAGS
:
4851 case DIOCRINADEFINE
:
4858 case DIOCGETSRCNODES
:
4859 case DIOCCLRSRCNODES
:
4861 case DIOCIGETIFACES
:
4864 case DIOCKILLSRCNODES
: