2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2001 Daniel Hartmeier
5 * Copyright (c) 2002,2003 Henning Brauer
6 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Effort sponsored in part by the Defense Advanced Research Projects
34 * Agency (DARPA) and Air Force Research Laboratory, Air Force
35 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
37 * $OpenBSD: pf_ioctl.c,v 1.213 2009/02/15 21:46:12 mbalmer Exp $
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 #include "opt_inet6.h"
48 #include <sys/param.h>
51 #include <sys/endian.h>
52 #include <sys/fcntl.h>
53 #include <sys/filio.h>
54 #include <sys/interrupt.h>
56 #include <sys/kernel.h>
57 #include <sys/kthread.h>
60 #include <sys/module.h>
62 #include <sys/rwlock.h>
64 #include <sys/socket.h>
65 #include <sys/sysctl.h>
67 #include <sys/ucred.h>
70 #include <net/if_var.h>
72 #include <net/route.h>
74 #include <net/pfvar.h>
75 #include <net/if_pfsync.h>
76 #include <net/if_pflog.h>
78 #include <netinet/in.h>
79 #include <netinet/ip.h>
80 #include <netinet/ip_var.h>
81 #include <netinet6/ip6_var.h>
82 #include <netinet/ip_icmp.h>
85 #include <netinet/ip6.h>
89 #include <net/altq/altq.h>
92 static struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
93 u_int8_t, u_int8_t, u_int8_t);
95 static void pf_mv_pool(struct pf_palist *, struct pf_palist *);
96 static void pf_empty_pool(struct pf_palist *);
97 static int pfioctl(struct cdev *, u_long, caddr_t, int,
100 static int pf_begin_altq(u_int32_t *);
101 static int pf_rollback_altq(u_int32_t);
102 static int pf_commit_altq(u_int32_t);
103 static int pf_enable_altq(struct pf_altq *);
104 static int pf_disable_altq(struct pf_altq *);
105 static u_int32_t pf_qname2qid(char *);
106 static void pf_qid_unref(u_int32_t);
108 static int pf_begin_rules(u_int32_t *, int, const char *);
109 static int pf_rollback_rules(u_int32_t, int, char *);
110 static int pf_setup_pfsync_matching(struct pf_ruleset *);
111 static void pf_hash_rule(MD5_CTX *, struct pf_rule *);
112 static void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
113 static int pf_commit_rules(u_int32_t, int, char *);
114 static int pf_addr_setup(struct pf_ruleset *,
115 struct pf_addr_wrap *, sa_family_t);
116 static void pf_addr_copyout(struct pf_addr_wrap *);
118 VNET_DEFINE(struct pf_rule, pf_default_rule);
121 static VNET_DEFINE(int, pf_altq_running);
122 #define V_pf_altq_running VNET(pf_altq_running)
125 #define TAGID_MAX 50000
127 TAILQ_ENTRY(pf_tagname) entries;
128 char name[PF_TAG_NAME_SIZE];
133 TAILQ_HEAD(pf_tags, pf_tagname);
134 #define V_pf_tags VNET(pf_tags)
135 VNET_DEFINE(struct pf_tags, pf_tags);
136 #define V_pf_qids VNET(pf_qids)
137 VNET_DEFINE(struct pf_tags, pf_qids);
138 static MALLOC_DEFINE(M_PFTAG, "pf_tag", "pf(4) tag names");
139 static MALLOC_DEFINE(M_PFALTQ, "pf_altq", "pf(4) altq configuration db");
140 static MALLOC_DEFINE(M_PFRULE, "pf_rule", "pf(4) rules");
142 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
143 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
146 static u_int16_t tagname2tag(struct pf_tags *, char *);
147 static u_int16_t pf_tagname2tag(char *);
148 static void tag_unref(struct pf_tags *, u_int16_t);
150 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
155 * XXX - These are new and need to be checked when moveing to a new version
157 static void pf_clear_states(void);
158 static int pf_clear_tables(void);
159 static void pf_clear_srcnodes(struct pf_src_node *);
160 static void pf_kill_srcnodes(struct pfioc_src_node_kill *);
161 static void pf_tbladdr_copyout(struct pf_addr_wrap *);
164 * Wrapper functions for pfil(9) hooks
167 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
168 int dir, struct inpcb *inp);
169 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
170 int dir, struct inpcb *inp);
173 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
174 int dir, struct inpcb *inp);
175 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
176 int dir, struct inpcb *inp);
179 static int hook_pf(void);
180 static int dehook_pf(void);
181 static int shutdown_pf(void);
182 static int pf_load(void);
183 static void pf_unload(void);
185 static struct cdevsw pf_cdevsw = {
188 .d_version = D_VERSION,
191 static volatile VNET_DEFINE(int, pf_pfil_hooked);
192 #define V_pf_pfil_hooked VNET(pf_pfil_hooked)
195 * We need a flag that is neither hooked nor running to know when
196 * the VNET is "valid". We primarily need this to control (global)
197 * external event, e.g., eventhandlers.
199 VNET_DEFINE(int, pf_vnet_active);
200 #define V_pf_vnet_active VNET(pf_vnet_active)
203 struct proc *pf_purge_proc;
205 struct rwlock pf_rules_lock;
206 struct sx pf_ioctl_lock;
207 struct sx pf_end_lock;
210 pfsync_state_import_t *pfsync_state_import_ptr = NULL;
211 pfsync_insert_state_t *pfsync_insert_state_ptr = NULL;
212 pfsync_update_state_t *pfsync_update_state_ptr = NULL;
213 pfsync_delete_state_t *pfsync_delete_state_ptr = NULL;
214 pfsync_clear_states_t *pfsync_clear_states_ptr = NULL;
215 pfsync_defer_t *pfsync_defer_ptr = NULL;
217 pflog_packet_t *pflog_packet_ptr = NULL;
222 u_int32_t *my_timeout = V_pf_default_rule.timeout;
226 pfi_initialize_vnet();
229 V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
230 V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
232 RB_INIT(&V_pf_anchors);
233 pf_init_ruleset(&pf_main_ruleset);
235 /* default rule should never be garbage collected */
236 V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next;
237 #ifdef PF_DEFAULT_TO_DROP
238 V_pf_default_rule.action = PF_DROP;
240 V_pf_default_rule.action = PF_PASS;
242 V_pf_default_rule.nr = -1;
243 V_pf_default_rule.rtableid = -1;
245 V_pf_default_rule.states_cur = counter_u64_alloc(M_WAITOK);
246 V_pf_default_rule.states_tot = counter_u64_alloc(M_WAITOK);
247 V_pf_default_rule.src_nodes = counter_u64_alloc(M_WAITOK);
249 /* initialize default timeouts */
250 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
251 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
252 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
253 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
254 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
255 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
256 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
257 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
258 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
259 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
260 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
261 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
262 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
263 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
264 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
265 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
266 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
267 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
268 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
269 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
271 bzero(&V_pf_status, sizeof(V_pf_status));
272 V_pf_status.debug = PF_DEBUG_URGENT;
274 V_pf_pfil_hooked = 0;
276 /* XXX do our best to avoid a conflict */
277 V_pf_status.hostid = arc4random();
279 for (int i = 0; i < PFRES_MAX; i++)
280 V_pf_status.counters[i] = counter_u64_alloc(M_WAITOK);
281 for (int i = 0; i < LCNT_MAX; i++)
282 V_pf_status.lcounters[i] = counter_u64_alloc(M_WAITOK);
283 for (int i = 0; i < FCNT_MAX; i++)
284 V_pf_status.fcounters[i] = counter_u64_alloc(M_WAITOK);
285 for (int i = 0; i < SCNT_MAX; i++)
286 V_pf_status.scounters[i] = counter_u64_alloc(M_WAITOK);
288 if (swi_add(NULL, "pf send", pf_intr, curvnet, SWI_NET,
289 INTR_MPSAFE, &V_pf_swi_cookie) != 0)
290 /* XXXGL: leaked all above. */
295 static struct pf_pool *
296 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
297 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
298 u_int8_t check_ticket)
300 struct pf_ruleset *ruleset;
301 struct pf_rule *rule;
304 ruleset = pf_find_ruleset(anchor);
307 rs_num = pf_get_ruleset_number(rule_action);
308 if (rs_num >= PF_RULESET_MAX)
311 if (check_ticket && ticket !=
312 ruleset->rules[rs_num].active.ticket)
315 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
318 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
320 if (check_ticket && ticket !=
321 ruleset->rules[rs_num].inactive.ticket)
324 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
327 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
330 while ((rule != NULL) && (rule->nr != rule_number))
331 rule = TAILQ_NEXT(rule, entries);
336 return (&rule->rpool);
340 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
342 struct pf_pooladdr *mv_pool_pa;
344 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
345 TAILQ_REMOVE(poola, mv_pool_pa, entries);
346 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
351 pf_empty_pool(struct pf_palist *poola)
353 struct pf_pooladdr *pa;
355 while ((pa = TAILQ_FIRST(poola)) != NULL) {
356 switch (pa->addr.type) {
357 case PF_ADDR_DYNIFTL:
358 pfi_dynaddr_remove(pa->addr.p.dyn);
361 /* XXX: this could be unfinished pooladdr on pabuf */
362 if (pa->addr.p.tbl != NULL)
363 pfr_detach_table(pa->addr.p.tbl);
367 pfi_kif_unref(pa->kif);
368 TAILQ_REMOVE(poola, pa, entries);
374 pf_unlink_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
379 TAILQ_REMOVE(rulequeue, rule, entries);
381 PF_UNLNKDRULES_LOCK();
382 rule->rule_flag |= PFRULE_REFS;
383 TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries);
384 PF_UNLNKDRULES_UNLOCK();
388 pf_free_rule(struct pf_rule *rule)
394 tag_unref(&V_pf_tags, rule->tag);
396 tag_unref(&V_pf_tags, rule->match_tag);
398 if (rule->pqid != rule->qid)
399 pf_qid_unref(rule->pqid);
400 pf_qid_unref(rule->qid);
402 switch (rule->src.addr.type) {
403 case PF_ADDR_DYNIFTL:
404 pfi_dynaddr_remove(rule->src.addr.p.dyn);
407 pfr_detach_table(rule->src.addr.p.tbl);
410 switch (rule->dst.addr.type) {
411 case PF_ADDR_DYNIFTL:
412 pfi_dynaddr_remove(rule->dst.addr.p.dyn);
415 pfr_detach_table(rule->dst.addr.p.tbl);
418 if (rule->overload_tbl)
419 pfr_detach_table(rule->overload_tbl);
421 pfi_kif_unref(rule->kif);
422 pf_anchor_remove(rule);
423 pf_empty_pool(&rule->rpool.list);
424 counter_u64_free(rule->states_cur);
425 counter_u64_free(rule->states_tot);
426 counter_u64_free(rule->src_nodes);
427 free(rule, M_PFRULE);
431 tagname2tag(struct pf_tags *head, char *tagname)
433 struct pf_tagname *tag, *p = NULL;
434 u_int16_t new_tagid = 1;
438 TAILQ_FOREACH(tag, head, entries)
439 if (strcmp(tagname, tag->name) == 0) {
445 * to avoid fragmentation, we do a linear search from the beginning
446 * and take the first free slot we find. if there is none or the list
447 * is empty, append a new entry at the end.
451 if (!TAILQ_EMPTY(head))
452 for (p = TAILQ_FIRST(head); p != NULL &&
453 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
454 new_tagid = p->tag + 1;
456 if (new_tagid > TAGID_MAX)
459 /* allocate and fill new struct pf_tagname */
460 tag = malloc(sizeof(*tag), M_PFTAG, M_NOWAIT|M_ZERO);
463 strlcpy(tag->name, tagname, sizeof(tag->name));
464 tag->tag = new_tagid;
467 if (p != NULL) /* insert new entry before p */
468 TAILQ_INSERT_BEFORE(p, tag, entries);
469 else /* either list empty or no free slot in between */
470 TAILQ_INSERT_TAIL(head, tag, entries);
476 tag_unref(struct pf_tags *head, u_int16_t tag)
478 struct pf_tagname *p, *next;
482 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
483 next = TAILQ_NEXT(p, entries);
486 TAILQ_REMOVE(head, p, entries);
495 pf_tagname2tag(char *tagname)
497 return (tagname2tag(&V_pf_tags, tagname));
502 pf_qname2qid(char *qname)
504 return ((u_int32_t)tagname2tag(&V_pf_qids, qname));
508 pf_qid_unref(u_int32_t qid)
510 tag_unref(&V_pf_qids, (u_int16_t)qid);
514 pf_begin_altq(u_int32_t *ticket)
516 struct pf_altq *altq;
521 /* Purge the old altq list */
522 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
523 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
524 if (altq->qname[0] == 0 &&
525 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
526 /* detach and destroy the discipline */
527 error = altq_remove(altq);
529 pf_qid_unref(altq->qid);
530 free(altq, M_PFALTQ);
534 *ticket = ++V_ticket_altqs_inactive;
535 V_altqs_inactive_open = 1;
540 pf_rollback_altq(u_int32_t ticket)
542 struct pf_altq *altq;
547 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
549 /* Purge the old altq list */
550 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
551 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
552 if (altq->qname[0] == 0 &&
553 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
554 /* detach and destroy the discipline */
555 error = altq_remove(altq);
557 pf_qid_unref(altq->qid);
558 free(altq, M_PFALTQ);
560 V_altqs_inactive_open = 0;
565 pf_commit_altq(u_int32_t ticket)
567 struct pf_altqqueue *old_altqs;
568 struct pf_altq *altq;
573 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
576 /* swap altqs, keep the old. */
577 old_altqs = V_pf_altqs_active;
578 V_pf_altqs_active = V_pf_altqs_inactive;
579 V_pf_altqs_inactive = old_altqs;
580 V_ticket_altqs_active = V_ticket_altqs_inactive;
582 /* Attach new disciplines */
583 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
584 if (altq->qname[0] == 0 &&
585 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
586 /* attach the discipline */
587 error = altq_pfattach(altq);
588 if (error == 0 && V_pf_altq_running)
589 error = pf_enable_altq(altq);
595 /* Purge the old altq list */
596 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
597 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
598 if (altq->qname[0] == 0 &&
599 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
600 /* detach and destroy the discipline */
601 if (V_pf_altq_running)
602 error = pf_disable_altq(altq);
603 err = altq_pfdetach(altq);
604 if (err != 0 && error == 0)
606 err = altq_remove(altq);
607 if (err != 0 && error == 0)
610 pf_qid_unref(altq->qid);
611 free(altq, M_PFALTQ);
614 V_altqs_inactive_open = 0;
619 pf_enable_altq(struct pf_altq *altq)
622 struct tb_profile tb;
625 if ((ifp = ifunit(altq->ifname)) == NULL)
628 if (ifp->if_snd.altq_type != ALTQT_NONE)
629 error = altq_enable(&ifp->if_snd);
631 /* set tokenbucket regulator */
632 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
633 tb.rate = altq->ifbandwidth;
634 tb.depth = altq->tbrsize;
635 error = tbr_set(&ifp->if_snd, &tb);
642 pf_disable_altq(struct pf_altq *altq)
645 struct tb_profile tb;
648 if ((ifp = ifunit(altq->ifname)) == NULL)
652 * when the discipline is no longer referenced, it was overridden
653 * by a new one. if so, just return.
655 if (altq->altq_disc != ifp->if_snd.altq_disc)
658 error = altq_disable(&ifp->if_snd);
661 /* clear tokenbucket regulator */
663 error = tbr_set(&ifp->if_snd, &tb);
670 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
673 struct pf_altq *a1, *a2, *a3;
677 /* Interrupt userland queue modifications */
678 if (V_altqs_inactive_open)
679 pf_rollback_altq(V_ticket_altqs_inactive);
681 /* Start new altq ruleset */
682 if (pf_begin_altq(&ticket))
685 /* Copy the current active set */
686 TAILQ_FOREACH(a1, V_pf_altqs_active, entries) {
687 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
692 bcopy(a1, a2, sizeof(struct pf_altq));
694 if (a2->qname[0] != 0) {
695 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
700 a2->altq_disc = NULL;
701 TAILQ_FOREACH(a3, V_pf_altqs_inactive, entries) {
702 if (strncmp(a3->ifname, a2->ifname,
703 IFNAMSIZ) == 0 && a3->qname[0] == 0) {
704 a2->altq_disc = a3->altq_disc;
709 /* Deactivate the interface in question */
710 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
711 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
712 (remove && ifp1 == ifp)) {
713 a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
715 error = altq_add(a2);
717 if (ticket != V_ticket_altqs_inactive)
726 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries);
730 pf_rollback_altq(ticket);
732 pf_commit_altq(ticket);
737 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
739 struct pf_ruleset *rs;
740 struct pf_rule *rule;
744 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
746 rs = pf_find_or_create_ruleset(anchor);
749 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
750 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
751 rs->rules[rs_num].inactive.rcount--;
753 *ticket = ++rs->rules[rs_num].inactive.ticket;
754 rs->rules[rs_num].inactive.open = 1;
759 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
761 struct pf_ruleset *rs;
762 struct pf_rule *rule;
766 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
768 rs = pf_find_ruleset(anchor);
769 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
770 rs->rules[rs_num].inactive.ticket != ticket)
772 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
773 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
774 rs->rules[rs_num].inactive.rcount--;
776 rs->rules[rs_num].inactive.open = 0;
780 #define PF_MD5_UPD(st, elm) \
781 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
783 #define PF_MD5_UPD_STR(st, elm) \
784 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
786 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
787 (stor) = htonl((st)->elm); \
788 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
791 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
792 (stor) = htons((st)->elm); \
793 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
797 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
799 PF_MD5_UPD(pfr, addr.type);
800 switch (pfr->addr.type) {
801 case PF_ADDR_DYNIFTL:
802 PF_MD5_UPD(pfr, addr.v.ifname);
803 PF_MD5_UPD(pfr, addr.iflags);
806 PF_MD5_UPD(pfr, addr.v.tblname);
808 case PF_ADDR_ADDRMASK:
810 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
811 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
815 PF_MD5_UPD(pfr, port[0]);
816 PF_MD5_UPD(pfr, port[1]);
817 PF_MD5_UPD(pfr, neg);
818 PF_MD5_UPD(pfr, port_op);
822 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
827 pf_hash_rule_addr(ctx, &rule->src);
828 pf_hash_rule_addr(ctx, &rule->dst);
829 PF_MD5_UPD_STR(rule, label);
830 PF_MD5_UPD_STR(rule, ifname);
831 PF_MD5_UPD_STR(rule, match_tagname);
832 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
833 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
834 PF_MD5_UPD_HTONL(rule, prob, y);
835 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
836 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
837 PF_MD5_UPD(rule, uid.op);
838 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
839 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
840 PF_MD5_UPD(rule, gid.op);
841 PF_MD5_UPD_HTONL(rule, rule_flag, y);
842 PF_MD5_UPD(rule, action);
843 PF_MD5_UPD(rule, direction);
844 PF_MD5_UPD(rule, af);
845 PF_MD5_UPD(rule, quick);
846 PF_MD5_UPD(rule, ifnot);
847 PF_MD5_UPD(rule, match_tag_not);
848 PF_MD5_UPD(rule, natpass);
849 PF_MD5_UPD(rule, keep_state);
850 PF_MD5_UPD(rule, proto);
851 PF_MD5_UPD(rule, type);
852 PF_MD5_UPD(rule, code);
853 PF_MD5_UPD(rule, flags);
854 PF_MD5_UPD(rule, flagset);
855 PF_MD5_UPD(rule, allow_opts);
856 PF_MD5_UPD(rule, rt);
857 PF_MD5_UPD(rule, tos);
861 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
863 struct pf_ruleset *rs;
864 struct pf_rule *rule, **old_array;
865 struct pf_rulequeue *old_rules;
867 u_int32_t old_rcount;
871 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
873 rs = pf_find_ruleset(anchor);
874 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
875 ticket != rs->rules[rs_num].inactive.ticket)
878 /* Calculate checksum for the main ruleset */
879 if (rs == &pf_main_ruleset) {
880 error = pf_setup_pfsync_matching(rs);
885 /* Swap rules, keep the old. */
886 old_rules = rs->rules[rs_num].active.ptr;
887 old_rcount = rs->rules[rs_num].active.rcount;
888 old_array = rs->rules[rs_num].active.ptr_array;
890 rs->rules[rs_num].active.ptr =
891 rs->rules[rs_num].inactive.ptr;
892 rs->rules[rs_num].active.ptr_array =
893 rs->rules[rs_num].inactive.ptr_array;
894 rs->rules[rs_num].active.rcount =
895 rs->rules[rs_num].inactive.rcount;
896 rs->rules[rs_num].inactive.ptr = old_rules;
897 rs->rules[rs_num].inactive.ptr_array = old_array;
898 rs->rules[rs_num].inactive.rcount = old_rcount;
900 rs->rules[rs_num].active.ticket =
901 rs->rules[rs_num].inactive.ticket;
902 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
905 /* Purge the old rule list. */
906 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
907 pf_unlink_rule(old_rules, rule);
908 if (rs->rules[rs_num].inactive.ptr_array)
909 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
910 rs->rules[rs_num].inactive.ptr_array = NULL;
911 rs->rules[rs_num].inactive.rcount = 0;
912 rs->rules[rs_num].inactive.open = 0;
913 pf_remove_if_empty_ruleset(rs);
919 pf_setup_pfsync_matching(struct pf_ruleset *rs)
922 struct pf_rule *rule;
924 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
927 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
928 /* XXX PF_RULESET_SCRUB as well? */
929 if (rs_cnt == PF_RULESET_SCRUB)
932 if (rs->rules[rs_cnt].inactive.ptr_array)
933 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
934 rs->rules[rs_cnt].inactive.ptr_array = NULL;
936 if (rs->rules[rs_cnt].inactive.rcount) {
937 rs->rules[rs_cnt].inactive.ptr_array =
938 malloc(sizeof(caddr_t) *
939 rs->rules[rs_cnt].inactive.rcount,
942 if (!rs->rules[rs_cnt].inactive.ptr_array)
946 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
948 pf_hash_rule(&ctx, rule);
949 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
953 MD5Final(digest, &ctx);
954 memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum));
959 pf_addr_setup(struct pf_ruleset *ruleset, struct pf_addr_wrap *addr,
964 switch (addr->type) {
966 addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname);
967 if (addr->p.tbl == NULL)
970 case PF_ADDR_DYNIFTL:
971 error = pfi_dynaddr_setup(addr, af);
979 pf_addr_copyout(struct pf_addr_wrap *addr)
982 switch (addr->type) {
983 case PF_ADDR_DYNIFTL:
984 pfi_dynaddr_copyout(addr);
987 pf_tbladdr_copyout(addr);
993 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
997 /* XXX keep in sync with switch() below */
998 if (securelevel_gt(td->td_ucred, 2))
1005 case DIOCSETSTATUSIF:
1011 case DIOCGETTIMEOUT:
1012 case DIOCCLRRULECTRS:
1017 case DIOCGETRULESETS:
1018 case DIOCGETRULESET:
1019 case DIOCRGETTABLES:
1020 case DIOCRGETTSTATS:
1021 case DIOCRCLRTSTATS:
1027 case DIOCRGETASTATS:
1028 case DIOCRCLRASTATS:
1031 case DIOCGETSRCNODES:
1032 case DIOCCLRSRCNODES:
1033 case DIOCIGETIFACES:
1038 case DIOCRCLRTABLES:
1039 case DIOCRADDTABLES:
1040 case DIOCRDELTABLES:
1041 case DIOCRSETTFLAGS:
1042 if (((struct pfioc_table *)addr)->pfrio_flags &
1044 break; /* dummy operation ok */
1050 if (!(flags & FWRITE))
1058 case DIOCGETTIMEOUT:
1063 case DIOCGETRULESETS:
1064 case DIOCGETRULESET:
1066 case DIOCRGETTABLES:
1067 case DIOCRGETTSTATS:
1069 case DIOCRGETASTATS:
1072 case DIOCGETSRCNODES:
1073 case DIOCIGETIFACES:
1076 case DIOCRCLRTABLES:
1077 case DIOCRADDTABLES:
1078 case DIOCRDELTABLES:
1079 case DIOCRCLRTSTATS:
1084 case DIOCRSETTFLAGS:
1085 if (((struct pfioc_table *)addr)->pfrio_flags &
1087 flags |= FWRITE; /* need write lock for dummy */
1088 break; /* dummy operation ok */
1092 if (((struct pfioc_rule *)addr)->action ==
1100 CURVNET_SET(TD_TO_VNET(td));
1104 sx_xlock(&pf_ioctl_lock);
1105 if (V_pf_status.running)
1112 DPFPRINTF(PF_DEBUG_MISC,
1113 ("pf: pfil registration failed\n"));
1116 V_pf_status.running = 1;
1117 V_pf_status.since = time_second;
1120 V_pf_stateid[cpu] = time_second;
1122 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1127 sx_xlock(&pf_ioctl_lock);
1128 if (!V_pf_status.running)
1131 V_pf_status.running = 0;
1132 error = dehook_pf();
1134 V_pf_status.running = 1;
1135 DPFPRINTF(PF_DEBUG_MISC,
1136 ("pf: pfil unregistration failed\n"));
1138 V_pf_status.since = time_second;
1139 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1144 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1145 struct pf_ruleset *ruleset;
1146 struct pf_rule *rule, *tail;
1147 struct pf_pooladdr *pa;
1148 struct pfi_kif *kif = NULL;
1151 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1156 if (pr->rule.af == AF_INET) {
1157 error = EAFNOSUPPORT;
1162 if (pr->rule.af == AF_INET6) {
1163 error = EAFNOSUPPORT;
1168 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK);
1169 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1170 if (rule->ifname[0])
1171 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1172 rule->states_cur = counter_u64_alloc(M_WAITOK);
1173 rule->states_tot = counter_u64_alloc(M_WAITOK);
1174 rule->src_nodes = counter_u64_alloc(M_WAITOK);
1175 rule->cuid = td->td_ucred->cr_ruid;
1176 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1177 TAILQ_INIT(&rule->rpool.list);
1179 #define ERROUT(x) { error = (x); goto DIOCADDRULE_error; }
1182 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1183 ruleset = pf_find_ruleset(pr->anchor);
1184 if (ruleset == NULL)
1186 rs_num = pf_get_ruleset_number(pr->rule.action);
1187 if (rs_num >= PF_RULESET_MAX)
1189 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1190 DPFPRINTF(PF_DEBUG_MISC,
1191 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1192 ruleset->rules[rs_num].inactive.ticket));
1195 if (pr->pool_ticket != V_ticket_pabuf) {
1196 DPFPRINTF(PF_DEBUG_MISC,
1197 ("pool_ticket: %d != %d\n", pr->pool_ticket,
1202 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1205 rule->nr = tail->nr + 1;
1208 if (rule->ifname[0]) {
1209 rule->kif = pfi_kif_attach(kif, rule->ifname);
1210 pfi_kif_ref(rule->kif);
1214 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs)
1219 if (rule->qname[0] != 0) {
1220 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1222 else if (rule->pqname[0] != 0) {
1224 pf_qname2qid(rule->pqname)) == 0)
1227 rule->pqid = rule->qid;
1230 if (rule->tagname[0])
1231 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1233 if (rule->match_tagname[0])
1234 if ((rule->match_tag =
1235 pf_tagname2tag(rule->match_tagname)) == 0)
1237 if (rule->rt && !rule->direction)
1241 if (rule->logif >= PFLOGIFS_MAX)
1243 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
1245 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
1247 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1249 if (rule->scrub_flags & PFSTATE_SETPRIO &&
1250 (rule->set_prio[0] > PF_PRIO_MAX ||
1251 rule->set_prio[1] > PF_PRIO_MAX))
1253 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1254 if (pa->addr.type == PF_ADDR_TABLE) {
1255 pa->addr.p.tbl = pfr_attach_table(ruleset,
1256 pa->addr.v.tblname);
1257 if (pa->addr.p.tbl == NULL)
1261 rule->overload_tbl = NULL;
1262 if (rule->overload_tblname[0]) {
1263 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1264 rule->overload_tblname)) == NULL)
1267 rule->overload_tbl->pfrkt_flags |=
1271 pf_mv_pool(&V_pf_pabuf, &rule->rpool.list);
1272 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1273 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1274 (rule->rt > PF_NOPFROUTE)) &&
1275 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1284 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1285 rule->evaluations = rule->packets[0] = rule->packets[1] =
1286 rule->bytes[0] = rule->bytes[1] = 0;
1287 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1289 ruleset->rules[rs_num].inactive.rcount++;
1296 counter_u64_free(rule->states_cur);
1297 counter_u64_free(rule->states_tot);
1298 counter_u64_free(rule->src_nodes);
1299 free(rule, M_PFRULE);
1301 free(kif, PFI_MTYPE);
1305 case DIOCGETRULES: {
1306 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1307 struct pf_ruleset *ruleset;
1308 struct pf_rule *tail;
1312 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1313 ruleset = pf_find_ruleset(pr->anchor);
1314 if (ruleset == NULL) {
1319 rs_num = pf_get_ruleset_number(pr->rule.action);
1320 if (rs_num >= PF_RULESET_MAX) {
1325 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1328 pr->nr = tail->nr + 1;
1331 pr->ticket = ruleset->rules[rs_num].active.ticket;
1337 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1338 struct pf_ruleset *ruleset;
1339 struct pf_rule *rule;
1343 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1344 ruleset = pf_find_ruleset(pr->anchor);
1345 if (ruleset == NULL) {
1350 rs_num = pf_get_ruleset_number(pr->rule.action);
1351 if (rs_num >= PF_RULESET_MAX) {
1356 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1361 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1362 while ((rule != NULL) && (rule->nr != pr->nr))
1363 rule = TAILQ_NEXT(rule, entries);
1369 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1370 pr->rule.u_states_cur = counter_u64_fetch(rule->states_cur);
1371 pr->rule.u_states_tot = counter_u64_fetch(rule->states_tot);
1372 pr->rule.u_src_nodes = counter_u64_fetch(rule->src_nodes);
1373 if (pf_anchor_copyout(ruleset, rule, pr)) {
1378 pf_addr_copyout(&pr->rule.src.addr);
1379 pf_addr_copyout(&pr->rule.dst.addr);
1380 for (i = 0; i < PF_SKIP_COUNT; ++i)
1381 if (rule->skip[i].ptr == NULL)
1382 pr->rule.skip[i].nr = -1;
1384 pr->rule.skip[i].nr =
1385 rule->skip[i].ptr->nr;
1387 if (pr->action == PF_GET_CLR_CNTR) {
1388 rule->evaluations = 0;
1389 rule->packets[0] = rule->packets[1] = 0;
1390 rule->bytes[0] = rule->bytes[1] = 0;
1391 counter_u64_zero(rule->states_tot);
1397 case DIOCCHANGERULE: {
1398 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1399 struct pf_ruleset *ruleset;
1400 struct pf_rule *oldrule = NULL, *newrule = NULL;
1401 struct pfi_kif *kif = NULL;
1402 struct pf_pooladdr *pa;
1406 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1407 pcr->action > PF_CHANGE_GET_TICKET) {
1411 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1416 if (pcr->action != PF_CHANGE_REMOVE) {
1418 if (pcr->rule.af == AF_INET) {
1419 error = EAFNOSUPPORT;
1424 if (pcr->rule.af == AF_INET6) {
1425 error = EAFNOSUPPORT;
1429 newrule = malloc(sizeof(*newrule), M_PFRULE, M_WAITOK);
1430 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1431 if (newrule->ifname[0])
1432 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1433 newrule->states_cur = counter_u64_alloc(M_WAITOK);
1434 newrule->states_tot = counter_u64_alloc(M_WAITOK);
1435 newrule->src_nodes = counter_u64_alloc(M_WAITOK);
1436 newrule->cuid = td->td_ucred->cr_ruid;
1437 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1438 TAILQ_INIT(&newrule->rpool.list);
1441 #define ERROUT(x) { error = (x); goto DIOCCHANGERULE_error; }
1444 if (!(pcr->action == PF_CHANGE_REMOVE ||
1445 pcr->action == PF_CHANGE_GET_TICKET) &&
1446 pcr->pool_ticket != V_ticket_pabuf)
1449 ruleset = pf_find_ruleset(pcr->anchor);
1450 if (ruleset == NULL)
1453 rs_num = pf_get_ruleset_number(pcr->rule.action);
1454 if (rs_num >= PF_RULESET_MAX)
1457 if (pcr->action == PF_CHANGE_GET_TICKET) {
1458 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1460 } else if (pcr->ticket !=
1461 ruleset->rules[rs_num].active.ticket)
1464 if (pcr->action != PF_CHANGE_REMOVE) {
1465 if (newrule->ifname[0]) {
1466 newrule->kif = pfi_kif_attach(kif,
1468 pfi_kif_ref(newrule->kif);
1470 newrule->kif = NULL;
1472 if (newrule->rtableid > 0 &&
1473 newrule->rtableid >= rt_numfibs)
1478 if (newrule->qname[0] != 0) {
1480 pf_qname2qid(newrule->qname)) == 0)
1482 else if (newrule->pqname[0] != 0) {
1483 if ((newrule->pqid =
1484 pf_qname2qid(newrule->pqname)) == 0)
1487 newrule->pqid = newrule->qid;
1490 if (newrule->tagname[0])
1492 pf_tagname2tag(newrule->tagname)) == 0)
1494 if (newrule->match_tagname[0])
1495 if ((newrule->match_tag = pf_tagname2tag(
1496 newrule->match_tagname)) == 0)
1498 if (newrule->rt && !newrule->direction)
1502 if (newrule->logif >= PFLOGIFS_MAX)
1504 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
1506 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
1508 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1510 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1511 if (pa->addr.type == PF_ADDR_TABLE) {
1513 pfr_attach_table(ruleset,
1514 pa->addr.v.tblname);
1515 if (pa->addr.p.tbl == NULL)
1519 newrule->overload_tbl = NULL;
1520 if (newrule->overload_tblname[0]) {
1521 if ((newrule->overload_tbl = pfr_attach_table(
1522 ruleset, newrule->overload_tblname)) ==
1526 newrule->overload_tbl->pfrkt_flags |=
1530 pf_mv_pool(&V_pf_pabuf, &newrule->rpool.list);
1531 if (((((newrule->action == PF_NAT) ||
1532 (newrule->action == PF_RDR) ||
1533 (newrule->action == PF_BINAT) ||
1534 (newrule->rt > PF_NOPFROUTE)) &&
1535 !newrule->anchor)) &&
1536 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1540 pf_free_rule(newrule);
1545 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1546 newrule->evaluations = 0;
1547 newrule->packets[0] = newrule->packets[1] = 0;
1548 newrule->bytes[0] = newrule->bytes[1] = 0;
1550 pf_empty_pool(&V_pf_pabuf);
1552 if (pcr->action == PF_CHANGE_ADD_HEAD)
1553 oldrule = TAILQ_FIRST(
1554 ruleset->rules[rs_num].active.ptr);
1555 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1556 oldrule = TAILQ_LAST(
1557 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1559 oldrule = TAILQ_FIRST(
1560 ruleset->rules[rs_num].active.ptr);
1561 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1562 oldrule = TAILQ_NEXT(oldrule, entries);
1563 if (oldrule == NULL) {
1564 if (newrule != NULL)
1565 pf_free_rule(newrule);
1572 if (pcr->action == PF_CHANGE_REMOVE) {
1573 pf_unlink_rule(ruleset->rules[rs_num].active.ptr,
1575 ruleset->rules[rs_num].active.rcount--;
1577 if (oldrule == NULL)
1579 ruleset->rules[rs_num].active.ptr,
1581 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1582 pcr->action == PF_CHANGE_ADD_BEFORE)
1583 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1586 ruleset->rules[rs_num].active.ptr,
1587 oldrule, newrule, entries);
1588 ruleset->rules[rs_num].active.rcount++;
1592 TAILQ_FOREACH(oldrule,
1593 ruleset->rules[rs_num].active.ptr, entries)
1596 ruleset->rules[rs_num].active.ticket++;
1598 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1599 pf_remove_if_empty_ruleset(ruleset);
1605 DIOCCHANGERULE_error:
1607 if (newrule != NULL) {
1608 counter_u64_free(newrule->states_cur);
1609 counter_u64_free(newrule->states_tot);
1610 counter_u64_free(newrule->src_nodes);
1611 free(newrule, M_PFRULE);
1614 free(kif, PFI_MTYPE);
1618 case DIOCCLRSTATES: {
1620 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1621 u_int i, killed = 0;
1623 for (i = 0; i <= pf_hashmask; i++) {
1624 struct pf_idhash *ih = &V_pf_idhash[i];
1626 relock_DIOCCLRSTATES:
1627 PF_HASHROW_LOCK(ih);
1628 LIST_FOREACH(s, &ih->states, entry)
1629 if (!psk->psk_ifname[0] ||
1630 !strcmp(psk->psk_ifname,
1631 s->kif->pfik_name)) {
1633 * Don't send out individual
1636 s->state_flags |= PFSTATE_NOSYNC;
1637 pf_unlink_state(s, PF_ENTER_LOCKED);
1639 goto relock_DIOCCLRSTATES;
1641 PF_HASHROW_UNLOCK(ih);
1643 psk->psk_killed = killed;
1644 if (pfsync_clear_states_ptr != NULL)
1645 pfsync_clear_states_ptr(V_pf_status.hostid, psk->psk_ifname);
1649 case DIOCKILLSTATES: {
1651 struct pf_state_key *sk;
1652 struct pf_addr *srcaddr, *dstaddr;
1653 u_int16_t srcport, dstport;
1654 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1655 u_int i, killed = 0;
1657 if (psk->psk_pfcmp.id) {
1658 if (psk->psk_pfcmp.creatorid == 0)
1659 psk->psk_pfcmp.creatorid = V_pf_status.hostid;
1660 if ((s = pf_find_state_byid(psk->psk_pfcmp.id,
1661 psk->psk_pfcmp.creatorid))) {
1662 pf_unlink_state(s, PF_ENTER_LOCKED);
1663 psk->psk_killed = 1;
1668 for (i = 0; i <= pf_hashmask; i++) {
1669 struct pf_idhash *ih = &V_pf_idhash[i];
1671 relock_DIOCKILLSTATES:
1672 PF_HASHROW_LOCK(ih);
1673 LIST_FOREACH(s, &ih->states, entry) {
1674 sk = s->key[PF_SK_WIRE];
1675 if (s->direction == PF_OUT) {
1676 srcaddr = &sk->addr[1];
1677 dstaddr = &sk->addr[0];
1678 srcport = sk->port[1];
1679 dstport = sk->port[0];
1681 srcaddr = &sk->addr[0];
1682 dstaddr = &sk->addr[1];
1683 srcport = sk->port[0];
1684 dstport = sk->port[1];
1687 if ((!psk->psk_af || sk->af == psk->psk_af)
1688 && (!psk->psk_proto || psk->psk_proto ==
1690 PF_MATCHA(psk->psk_src.neg,
1691 &psk->psk_src.addr.v.a.addr,
1692 &psk->psk_src.addr.v.a.mask,
1694 PF_MATCHA(psk->psk_dst.neg,
1695 &psk->psk_dst.addr.v.a.addr,
1696 &psk->psk_dst.addr.v.a.mask,
1698 (psk->psk_src.port_op == 0 ||
1699 pf_match_port(psk->psk_src.port_op,
1700 psk->psk_src.port[0], psk->psk_src.port[1],
1702 (psk->psk_dst.port_op == 0 ||
1703 pf_match_port(psk->psk_dst.port_op,
1704 psk->psk_dst.port[0], psk->psk_dst.port[1],
1706 (!psk->psk_label[0] ||
1707 (s->rule.ptr->label[0] &&
1708 !strcmp(psk->psk_label,
1709 s->rule.ptr->label))) &&
1710 (!psk->psk_ifname[0] ||
1711 !strcmp(psk->psk_ifname,
1712 s->kif->pfik_name))) {
1713 pf_unlink_state(s, PF_ENTER_LOCKED);
1715 goto relock_DIOCKILLSTATES;
1718 PF_HASHROW_UNLOCK(ih);
1720 psk->psk_killed = killed;
1724 case DIOCADDSTATE: {
1725 struct pfioc_state *ps = (struct pfioc_state *)addr;
1726 struct pfsync_state *sp = &ps->state;
1728 if (sp->timeout >= PFTM_MAX) {
1732 if (pfsync_state_import_ptr != NULL) {
1734 error = pfsync_state_import_ptr(sp, PFSYNC_SI_IOCTL);
1741 case DIOCGETSTATE: {
1742 struct pfioc_state *ps = (struct pfioc_state *)addr;
1745 s = pf_find_state_byid(ps->state.id, ps->state.creatorid);
1751 pfsync_state_export(&ps->state, s);
1756 case DIOCGETSTATES: {
1757 struct pfioc_states *ps = (struct pfioc_states *)addr;
1759 struct pfsync_state *pstore, *p;
1762 if (ps->ps_len == 0) {
1763 nr = uma_zone_get_cur(V_pf_state_z);
1764 ps->ps_len = sizeof(struct pfsync_state) * nr;
1768 p = pstore = malloc(ps->ps_len, M_TEMP, M_WAITOK);
1771 for (i = 0; i <= pf_hashmask; i++) {
1772 struct pf_idhash *ih = &V_pf_idhash[i];
1774 PF_HASHROW_LOCK(ih);
1775 LIST_FOREACH(s, &ih->states, entry) {
1777 if (s->timeout == PFTM_UNLINKED)
1780 if ((nr+1) * sizeof(*p) > ps->ps_len) {
1781 PF_HASHROW_UNLOCK(ih);
1782 goto DIOCGETSTATES_full;
1784 pfsync_state_export(p, s);
1788 PF_HASHROW_UNLOCK(ih);
1791 error = copyout(pstore, ps->ps_states,
1792 sizeof(struct pfsync_state) * nr);
1794 free(pstore, M_TEMP);
1797 ps->ps_len = sizeof(struct pfsync_state) * nr;
1798 free(pstore, M_TEMP);
1803 case DIOCGETSTATUS: {
1804 struct pf_status *s = (struct pf_status *)addr;
1807 s->running = V_pf_status.running;
1808 s->since = V_pf_status.since;
1809 s->debug = V_pf_status.debug;
1810 s->hostid = V_pf_status.hostid;
1811 s->states = V_pf_status.states;
1812 s->src_nodes = V_pf_status.src_nodes;
1814 for (int i = 0; i < PFRES_MAX; i++)
1816 counter_u64_fetch(V_pf_status.counters[i]);
1817 for (int i = 0; i < LCNT_MAX; i++)
1819 counter_u64_fetch(V_pf_status.lcounters[i]);
1820 for (int i = 0; i < FCNT_MAX; i++)
1822 counter_u64_fetch(V_pf_status.fcounters[i]);
1823 for (int i = 0; i < SCNT_MAX; i++)
1825 counter_u64_fetch(V_pf_status.scounters[i]);
1827 bcopy(V_pf_status.ifname, s->ifname, IFNAMSIZ);
1828 bcopy(V_pf_status.pf_chksum, s->pf_chksum,
1829 PF_MD5_DIGEST_LENGTH);
1831 pfi_update_status(s->ifname, s);
1836 case DIOCSETSTATUSIF: {
1837 struct pfioc_if *pi = (struct pfioc_if *)addr;
1839 if (pi->ifname[0] == 0) {
1840 bzero(V_pf_status.ifname, IFNAMSIZ);
1844 strlcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ);
1849 case DIOCCLRSTATUS: {
1851 for (int i = 0; i < PFRES_MAX; i++)
1852 counter_u64_zero(V_pf_status.counters[i]);
1853 for (int i = 0; i < FCNT_MAX; i++)
1854 counter_u64_zero(V_pf_status.fcounters[i]);
1855 for (int i = 0; i < SCNT_MAX; i++)
1856 counter_u64_zero(V_pf_status.scounters[i]);
1857 for (int i = 0; i < LCNT_MAX; i++)
1858 counter_u64_zero(V_pf_status.lcounters[i]);
1859 V_pf_status.since = time_second;
1860 if (*V_pf_status.ifname)
1861 pfi_update_status(V_pf_status.ifname, NULL);
1867 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1868 struct pf_state_key *sk;
1869 struct pf_state *state;
1870 struct pf_state_key_cmp key;
1871 int m = 0, direction = pnl->direction;
1874 /* NATLOOK src and dst are reversed, so reverse sidx/didx */
1875 sidx = (direction == PF_IN) ? 1 : 0;
1876 didx = (direction == PF_IN) ? 0 : 1;
1879 PF_AZERO(&pnl->saddr, pnl->af) ||
1880 PF_AZERO(&pnl->daddr, pnl->af) ||
1881 ((pnl->proto == IPPROTO_TCP ||
1882 pnl->proto == IPPROTO_UDP) &&
1883 (!pnl->dport || !pnl->sport)))
1886 bzero(&key, sizeof(key));
1888 key.proto = pnl->proto;
1889 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
1890 key.port[sidx] = pnl->sport;
1891 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
1892 key.port[didx] = pnl->dport;
1894 state = pf_find_state_all(&key, direction, &m);
1897 error = E2BIG; /* more than one state */
1898 else if (state != NULL) {
1899 /* XXXGL: not locked read */
1900 sk = state->key[sidx];
1901 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
1902 pnl->rsport = sk->port[sidx];
1903 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
1904 pnl->rdport = sk->port[didx];
1911 case DIOCSETTIMEOUT: {
1912 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1915 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1921 old = V_pf_default_rule.timeout[pt->timeout];
1922 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
1924 V_pf_default_rule.timeout[pt->timeout] = pt->seconds;
1925 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
1926 wakeup(pf_purge_thread);
1932 case DIOCGETTIMEOUT: {
1933 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1935 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1940 pt->seconds = V_pf_default_rule.timeout[pt->timeout];
1945 case DIOCGETLIMIT: {
1946 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1948 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1953 pl->limit = V_pf_limits[pl->index].limit;
1958 case DIOCSETLIMIT: {
1959 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1963 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1964 V_pf_limits[pl->index].zone == NULL) {
1969 uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit);
1970 old_limit = V_pf_limits[pl->index].limit;
1971 V_pf_limits[pl->index].limit = pl->limit;
1972 pl->limit = old_limit;
1977 case DIOCSETDEBUG: {
1978 u_int32_t *level = (u_int32_t *)addr;
1981 V_pf_status.debug = *level;
1986 case DIOCCLRRULECTRS: {
1987 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
1988 struct pf_ruleset *ruleset = &pf_main_ruleset;
1989 struct pf_rule *rule;
1993 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
1994 rule->evaluations = 0;
1995 rule->packets[0] = rule->packets[1] = 0;
1996 rule->bytes[0] = rule->bytes[1] = 0;
2002 case DIOCGIFSPEED: {
2003 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
2004 struct pf_ifspeed ps;
2007 if (psp->ifname[0] != 0) {
2008 /* Can we completely trust user-land? */
2009 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
2010 ifp = ifunit(ps.ifname);
2012 psp->baudrate = ifp->if_baudrate;
2021 case DIOCSTARTALTQ: {
2022 struct pf_altq *altq;
2025 /* enable all altq interfaces on active list */
2026 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
2027 if (altq->qname[0] == 0 && (altq->local_flags &
2028 PFALTQ_FLAG_IF_REMOVED) == 0) {
2029 error = pf_enable_altq(altq);
2035 V_pf_altq_running = 1;
2037 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
2041 case DIOCSTOPALTQ: {
2042 struct pf_altq *altq;
2045 /* disable all altq interfaces on active list */
2046 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
2047 if (altq->qname[0] == 0 && (altq->local_flags &
2048 PFALTQ_FLAG_IF_REMOVED) == 0) {
2049 error = pf_disable_altq(altq);
2055 V_pf_altq_running = 0;
2057 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
2062 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2063 struct pf_altq *altq, *a;
2066 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK);
2067 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
2068 altq->local_flags = 0;
2071 if (pa->ticket != V_ticket_altqs_inactive) {
2073 free(altq, M_PFALTQ);
2079 * if this is for a queue, find the discipline and
2080 * copy the necessary fields
2082 if (altq->qname[0] != 0) {
2083 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2086 free(altq, M_PFALTQ);
2089 altq->altq_disc = NULL;
2090 TAILQ_FOREACH(a, V_pf_altqs_inactive, entries) {
2091 if (strncmp(a->ifname, altq->ifname,
2092 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2093 altq->altq_disc = a->altq_disc;
2099 if ((ifp = ifunit(altq->ifname)) == NULL)
2100 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2102 error = altq_add(altq);
2106 free(altq, M_PFALTQ);
2110 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries);
2111 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2116 case DIOCGETALTQS: {
2117 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2118 struct pf_altq *altq;
2122 TAILQ_FOREACH(altq, V_pf_altqs_active, entries)
2124 pa->ticket = V_ticket_altqs_active;
2130 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2131 struct pf_altq *altq;
2135 if (pa->ticket != V_ticket_altqs_active) {
2141 altq = TAILQ_FIRST(V_pf_altqs_active);
2142 while ((altq != NULL) && (nr < pa->nr)) {
2143 altq = TAILQ_NEXT(altq, entries);
2151 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2156 case DIOCCHANGEALTQ:
2157 /* CHANGEALTQ not supported yet! */
2161 case DIOCGETQSTATS: {
2162 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2163 struct pf_altq *altq;
2168 if (pq->ticket != V_ticket_altqs_active) {
2173 nbytes = pq->nbytes;
2175 altq = TAILQ_FIRST(V_pf_altqs_active);
2176 while ((altq != NULL) && (nr < pq->nr)) {
2177 altq = TAILQ_NEXT(altq, entries);
2186 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2192 error = altq_getqstats(altq, pq->buf, &nbytes);
2194 pq->scheduler = altq->scheduler;
2195 pq->nbytes = nbytes;
2201 case DIOCBEGINADDRS: {
2202 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2205 pf_empty_pool(&V_pf_pabuf);
2206 pp->ticket = ++V_ticket_pabuf;
2212 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2213 struct pf_pooladdr *pa;
2214 struct pfi_kif *kif = NULL;
2217 if (pp->af == AF_INET) {
2218 error = EAFNOSUPPORT;
2223 if (pp->af == AF_INET6) {
2224 error = EAFNOSUPPORT;
2228 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2229 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2230 pp->addr.addr.type != PF_ADDR_TABLE) {
2234 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK);
2235 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2237 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2239 if (pp->ticket != V_ticket_pabuf) {
2242 free(kif, PFI_MTYPE);
2247 if (pa->ifname[0]) {
2248 pa->kif = pfi_kif_attach(kif, pa->ifname);
2249 pfi_kif_ref(pa->kif);
2252 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error =
2253 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) {
2255 pfi_kif_unref(pa->kif);
2260 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries);
2265 case DIOCGETADDRS: {
2266 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2267 struct pf_pool *pool;
2268 struct pf_pooladdr *pa;
2272 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2273 pp->r_num, 0, 1, 0);
2279 TAILQ_FOREACH(pa, &pool->list, entries)
2286 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2287 struct pf_pool *pool;
2288 struct pf_pooladdr *pa;
2292 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2293 pp->r_num, 0, 1, 1);
2299 pa = TAILQ_FIRST(&pool->list);
2300 while ((pa != NULL) && (nr < pp->nr)) {
2301 pa = TAILQ_NEXT(pa, entries);
2309 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2310 pf_addr_copyout(&pp->addr.addr);
2315 case DIOCCHANGEADDR: {
2316 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2317 struct pf_pool *pool;
2318 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2319 struct pf_ruleset *ruleset;
2320 struct pfi_kif *kif = NULL;
2322 if (pca->action < PF_CHANGE_ADD_HEAD ||
2323 pca->action > PF_CHANGE_REMOVE) {
2327 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2328 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2329 pca->addr.addr.type != PF_ADDR_TABLE) {
2334 if (pca->action != PF_CHANGE_REMOVE) {
2336 if (pca->af == AF_INET) {
2337 error = EAFNOSUPPORT;
2342 if (pca->af == AF_INET6) {
2343 error = EAFNOSUPPORT;
2347 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK);
2348 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2349 if (newpa->ifname[0])
2350 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2354 #define ERROUT(x) { error = (x); goto DIOCCHANGEADDR_error; }
2356 ruleset = pf_find_ruleset(pca->anchor);
2357 if (ruleset == NULL)
2360 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2361 pca->r_num, pca->r_last, 1, 1);
2365 if (pca->action != PF_CHANGE_REMOVE) {
2366 if (newpa->ifname[0]) {
2367 newpa->kif = pfi_kif_attach(kif, newpa->ifname);
2368 pfi_kif_ref(newpa->kif);
2372 switch (newpa->addr.type) {
2373 case PF_ADDR_DYNIFTL:
2374 error = pfi_dynaddr_setup(&newpa->addr,
2378 newpa->addr.p.tbl = pfr_attach_table(ruleset,
2379 newpa->addr.v.tblname);
2380 if (newpa->addr.p.tbl == NULL)
2385 goto DIOCCHANGEADDR_error;
2388 switch (pca->action) {
2389 case PF_CHANGE_ADD_HEAD:
2390 oldpa = TAILQ_FIRST(&pool->list);
2392 case PF_CHANGE_ADD_TAIL:
2393 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2396 oldpa = TAILQ_FIRST(&pool->list);
2397 for (int i = 0; oldpa && i < pca->nr; i++)
2398 oldpa = TAILQ_NEXT(oldpa, entries);
2404 if (pca->action == PF_CHANGE_REMOVE) {
2405 TAILQ_REMOVE(&pool->list, oldpa, entries);
2406 switch (oldpa->addr.type) {
2407 case PF_ADDR_DYNIFTL:
2408 pfi_dynaddr_remove(oldpa->addr.p.dyn);
2411 pfr_detach_table(oldpa->addr.p.tbl);
2415 pfi_kif_unref(oldpa->kif);
2416 free(oldpa, M_PFRULE);
2419 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2420 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2421 pca->action == PF_CHANGE_ADD_BEFORE)
2422 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2424 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2428 pool->cur = TAILQ_FIRST(&pool->list);
2429 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af);
2434 DIOCCHANGEADDR_error:
2435 if (newpa != NULL) {
2437 pfi_kif_unref(newpa->kif);
2438 free(newpa, M_PFRULE);
2442 free(kif, PFI_MTYPE);
2446 case DIOCGETRULESETS: {
2447 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2448 struct pf_ruleset *ruleset;
2449 struct pf_anchor *anchor;
2452 pr->path[sizeof(pr->path) - 1] = 0;
2453 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2459 if (ruleset->anchor == NULL) {
2460 /* XXX kludge for pf_main_ruleset */
2461 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2462 if (anchor->parent == NULL)
2465 RB_FOREACH(anchor, pf_anchor_node,
2466 &ruleset->anchor->children)
2473 case DIOCGETRULESET: {
2474 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2475 struct pf_ruleset *ruleset;
2476 struct pf_anchor *anchor;
2480 pr->path[sizeof(pr->path) - 1] = 0;
2481 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2487 if (ruleset->anchor == NULL) {
2488 /* XXX kludge for pf_main_ruleset */
2489 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2490 if (anchor->parent == NULL && nr++ == pr->nr) {
2491 strlcpy(pr->name, anchor->name,
2496 RB_FOREACH(anchor, pf_anchor_node,
2497 &ruleset->anchor->children)
2498 if (nr++ == pr->nr) {
2499 strlcpy(pr->name, anchor->name,
2510 case DIOCRCLRTABLES: {
2511 struct pfioc_table *io = (struct pfioc_table *)addr;
2513 if (io->pfrio_esize != 0) {
2518 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2519 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2524 case DIOCRADDTABLES: {
2525 struct pfioc_table *io = (struct pfioc_table *)addr;
2526 struct pfr_table *pfrts;
2529 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2533 totlen = io->pfrio_size * sizeof(struct pfr_table);
2534 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
2540 error = copyin(io->pfrio_buffer, pfrts, totlen);
2542 free(pfrts, M_TEMP);
2546 error = pfr_add_tables(pfrts, io->pfrio_size,
2547 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2549 free(pfrts, M_TEMP);
2553 case DIOCRDELTABLES: {
2554 struct pfioc_table *io = (struct pfioc_table *)addr;
2555 struct pfr_table *pfrts;
2558 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2562 totlen = io->pfrio_size * sizeof(struct pfr_table);
2563 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
2569 error = copyin(io->pfrio_buffer, pfrts, totlen);
2571 free(pfrts, M_TEMP);
2575 error = pfr_del_tables(pfrts, io->pfrio_size,
2576 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2578 free(pfrts, M_TEMP);
2582 case DIOCRGETTABLES: {
2583 struct pfioc_table *io = (struct pfioc_table *)addr;
2584 struct pfr_table *pfrts;
2587 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2591 totlen = io->pfrio_size * sizeof(struct pfr_table);
2592 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
2599 error = pfr_get_tables(&io->pfrio_table, pfrts,
2600 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2603 error = copyout(pfrts, io->pfrio_buffer, totlen);
2604 free(pfrts, M_TEMP);
2608 case DIOCRGETTSTATS: {
2609 struct pfioc_table *io = (struct pfioc_table *)addr;
2610 struct pfr_tstats *pfrtstats;
2613 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2617 totlen = io->pfrio_size * sizeof(struct pfr_tstats);
2618 pfrtstats = mallocarray(io->pfrio_size,
2619 sizeof(struct pfr_tstats), M_TEMP, M_WAITOK);
2625 error = pfr_get_tstats(&io->pfrio_table, pfrtstats,
2626 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2629 error = copyout(pfrtstats, io->pfrio_buffer, totlen);
2630 free(pfrtstats, M_TEMP);
2634 case DIOCRCLRTSTATS: {
2635 struct pfioc_table *io = (struct pfioc_table *)addr;
2636 struct pfr_table *pfrts;
2639 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2643 totlen = io->pfrio_size * sizeof(struct pfr_table);
2644 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
2650 error = copyin(io->pfrio_buffer, pfrts, totlen);
2652 free(pfrts, M_TEMP);
2656 error = pfr_clr_tstats(pfrts, io->pfrio_size,
2657 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2659 free(pfrts, M_TEMP);
2663 case DIOCRSETTFLAGS: {
2664 struct pfioc_table *io = (struct pfioc_table *)addr;
2665 struct pfr_table *pfrts;
2668 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2672 totlen = io->pfrio_size * sizeof(struct pfr_table);
2673 pfrts = mallocarray(io->pfrio_size, sizeof(struct pfr_table),
2679 error = copyin(io->pfrio_buffer, pfrts, totlen);
2681 free(pfrts, M_TEMP);
2685 error = pfr_set_tflags(pfrts, io->pfrio_size,
2686 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2687 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2689 free(pfrts, M_TEMP);
2693 case DIOCRCLRADDRS: {
2694 struct pfioc_table *io = (struct pfioc_table *)addr;
2696 if (io->pfrio_esize != 0) {
2701 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2702 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2707 case DIOCRADDADDRS: {
2708 struct pfioc_table *io = (struct pfioc_table *)addr;
2709 struct pfr_addr *pfras;
2712 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2716 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2717 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
2723 error = copyin(io->pfrio_buffer, pfras, totlen);
2725 free(pfras, M_TEMP);
2729 error = pfr_add_addrs(&io->pfrio_table, pfras,
2730 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2731 PFR_FLAG_USERIOCTL);
2733 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2734 error = copyout(pfras, io->pfrio_buffer, totlen);
2735 free(pfras, M_TEMP);
2739 case DIOCRDELADDRS: {
2740 struct pfioc_table *io = (struct pfioc_table *)addr;
2741 struct pfr_addr *pfras;
2744 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2748 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2749 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
2755 error = copyin(io->pfrio_buffer, pfras, totlen);
2757 free(pfras, M_TEMP);
2761 error = pfr_del_addrs(&io->pfrio_table, pfras,
2762 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2763 PFR_FLAG_USERIOCTL);
2765 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2766 error = copyout(pfras, io->pfrio_buffer, totlen);
2767 free(pfras, M_TEMP);
2771 case DIOCRSETADDRS: {
2772 struct pfioc_table *io = (struct pfioc_table *)addr;
2773 struct pfr_addr *pfras;
2774 size_t totlen, count;
2776 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2780 count = max(io->pfrio_size, io->pfrio_size2);
2781 totlen = count * sizeof(struct pfr_addr);
2782 pfras = mallocarray(count, sizeof(struct pfr_addr), M_TEMP,
2788 error = copyin(io->pfrio_buffer, pfras, totlen);
2790 free(pfras, M_TEMP);
2794 error = pfr_set_addrs(&io->pfrio_table, pfras,
2795 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2796 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2797 PFR_FLAG_USERIOCTL, 0);
2799 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2800 error = copyout(pfras, io->pfrio_buffer, totlen);
2801 free(pfras, M_TEMP);
2805 case DIOCRGETADDRS: {
2806 struct pfioc_table *io = (struct pfioc_table *)addr;
2807 struct pfr_addr *pfras;
2810 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2814 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2815 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
2822 error = pfr_get_addrs(&io->pfrio_table, pfras,
2823 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2826 error = copyout(pfras, io->pfrio_buffer, totlen);
2827 free(pfras, M_TEMP);
2831 case DIOCRGETASTATS: {
2832 struct pfioc_table *io = (struct pfioc_table *)addr;
2833 struct pfr_astats *pfrastats;
2836 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2840 totlen = io->pfrio_size * sizeof(struct pfr_astats);
2841 pfrastats = mallocarray(io->pfrio_size,
2842 sizeof(struct pfr_astats), M_TEMP, M_WAITOK);
2848 error = pfr_get_astats(&io->pfrio_table, pfrastats,
2849 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2852 error = copyout(pfrastats, io->pfrio_buffer, totlen);
2853 free(pfrastats, M_TEMP);
2857 case DIOCRCLRASTATS: {
2858 struct pfioc_table *io = (struct pfioc_table *)addr;
2859 struct pfr_addr *pfras;
2862 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2866 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2867 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
2873 error = copyin(io->pfrio_buffer, pfras, totlen);
2875 free(pfras, M_TEMP);
2879 error = pfr_clr_astats(&io->pfrio_table, pfras,
2880 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2881 PFR_FLAG_USERIOCTL);
2883 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2884 error = copyout(pfras, io->pfrio_buffer, totlen);
2885 free(pfras, M_TEMP);
2889 case DIOCRTSTADDRS: {
2890 struct pfioc_table *io = (struct pfioc_table *)addr;
2891 struct pfr_addr *pfras;
2894 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2898 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2899 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
2905 error = copyin(io->pfrio_buffer, pfras, totlen);
2907 free(pfras, M_TEMP);
2911 error = pfr_tst_addrs(&io->pfrio_table, pfras,
2912 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2913 PFR_FLAG_USERIOCTL);
2916 error = copyout(pfras, io->pfrio_buffer, totlen);
2917 free(pfras, M_TEMP);
2921 case DIOCRINADEFINE: {
2922 struct pfioc_table *io = (struct pfioc_table *)addr;
2923 struct pfr_addr *pfras;
2926 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2930 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2931 pfras = mallocarray(io->pfrio_size, sizeof(struct pfr_addr),
2937 error = copyin(io->pfrio_buffer, pfras, totlen);
2939 free(pfras, M_TEMP);
2943 error = pfr_ina_define(&io->pfrio_table, pfras,
2944 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2945 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2947 free(pfras, M_TEMP);
2952 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2954 error = pf_osfp_add(io);
2960 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2962 error = pf_osfp_get(io);
2968 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2969 struct pfioc_trans_e *ioes, *ioe;
2973 if (io->esize != sizeof(*ioe)) {
2977 totlen = sizeof(struct pfioc_trans_e) * io->size;
2978 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e),
2984 error = copyin(io->array, ioes, totlen);
2990 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2991 switch (ioe->rs_num) {
2993 case PF_RULESET_ALTQ:
2994 if (ioe->anchor[0]) {
3000 if ((error = pf_begin_altq(&ioe->ticket))) {
3007 case PF_RULESET_TABLE:
3009 struct pfr_table table;
3011 bzero(&table, sizeof(table));
3012 strlcpy(table.pfrt_anchor, ioe->anchor,
3013 sizeof(table.pfrt_anchor));
3014 if ((error = pfr_ina_begin(&table,
3015 &ioe->ticket, NULL, 0))) {
3023 if ((error = pf_begin_rules(&ioe->ticket,
3024 ioe->rs_num, ioe->anchor))) {
3033 error = copyout(ioes, io->array, totlen);
3038 case DIOCXROLLBACK: {
3039 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3040 struct pfioc_trans_e *ioe, *ioes;
3044 if (io->esize != sizeof(*ioe)) {
3048 totlen = sizeof(struct pfioc_trans_e) * io->size;
3049 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e),
3055 error = copyin(io->array, ioes, totlen);
3061 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3062 switch (ioe->rs_num) {
3064 case PF_RULESET_ALTQ:
3065 if (ioe->anchor[0]) {
3071 if ((error = pf_rollback_altq(ioe->ticket))) {
3074 goto fail; /* really bad */
3078 case PF_RULESET_TABLE:
3080 struct pfr_table table;
3082 bzero(&table, sizeof(table));
3083 strlcpy(table.pfrt_anchor, ioe->anchor,
3084 sizeof(table.pfrt_anchor));
3085 if ((error = pfr_ina_rollback(&table,
3086 ioe->ticket, NULL, 0))) {
3089 goto fail; /* really bad */
3094 if ((error = pf_rollback_rules(ioe->ticket,
3095 ioe->rs_num, ioe->anchor))) {
3098 goto fail; /* really bad */
3109 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3110 struct pfioc_trans_e *ioe, *ioes;
3111 struct pf_ruleset *rs;
3115 if (io->esize != sizeof(*ioe)) {
3119 totlen = sizeof(struct pfioc_trans_e) * io->size;
3120 ioes = mallocarray(io->size, sizeof(struct pfioc_trans_e),
3126 error = copyin(io->array, ioes, totlen);
3132 /* First makes sure everything will succeed. */
3133 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3134 switch (ioe->rs_num) {
3136 case PF_RULESET_ALTQ:
3137 if (ioe->anchor[0]) {
3143 if (!V_altqs_inactive_open || ioe->ticket !=
3144 V_ticket_altqs_inactive) {
3152 case PF_RULESET_TABLE:
3153 rs = pf_find_ruleset(ioe->anchor);
3154 if (rs == NULL || !rs->topen || ioe->ticket !=
3163 if (ioe->rs_num < 0 || ioe->rs_num >=
3170 rs = pf_find_ruleset(ioe->anchor);
3172 !rs->rules[ioe->rs_num].inactive.open ||
3173 rs->rules[ioe->rs_num].inactive.ticket !=
3183 /* Now do the commit - no errors should happen here. */
3184 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3185 switch (ioe->rs_num) {
3187 case PF_RULESET_ALTQ:
3188 if ((error = pf_commit_altq(ioe->ticket))) {
3191 goto fail; /* really bad */
3195 case PF_RULESET_TABLE:
3197 struct pfr_table table;
3199 bzero(&table, sizeof(table));
3200 strlcpy(table.pfrt_anchor, ioe->anchor,
3201 sizeof(table.pfrt_anchor));
3202 if ((error = pfr_ina_commit(&table,
3203 ioe->ticket, NULL, NULL, 0))) {
3206 goto fail; /* really bad */
3211 if ((error = pf_commit_rules(ioe->ticket,
3212 ioe->rs_num, ioe->anchor))) {
3215 goto fail; /* really bad */
3225 case DIOCGETSRCNODES: {
3226 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3227 struct pf_srchash *sh;
3228 struct pf_src_node *n, *p, *pstore;
3231 if (psn->psn_len == 0) {
3232 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
3234 PF_HASHROW_LOCK(sh);
3235 LIST_FOREACH(n, &sh->nodes, entry)
3237 PF_HASHROW_UNLOCK(sh);
3239 psn->psn_len = sizeof(struct pf_src_node) * nr;
3243 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK);
3244 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
3246 PF_HASHROW_LOCK(sh);
3247 LIST_FOREACH(n, &sh->nodes, entry) {
3248 int secs = time_uptime, diff;
3250 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3253 bcopy(n, p, sizeof(struct pf_src_node));
3254 if (n->rule.ptr != NULL)
3255 p->rule.nr = n->rule.ptr->nr;
3256 p->creation = secs - p->creation;
3257 if (p->expire > secs)
3262 /* Adjust the connection rate estimate. */
3263 diff = secs - n->conn_rate.last;
3264 if (diff >= n->conn_rate.seconds)
3265 p->conn_rate.count = 0;
3267 p->conn_rate.count -=
3268 n->conn_rate.count * diff /
3269 n->conn_rate.seconds;
3273 PF_HASHROW_UNLOCK(sh);
3275 error = copyout(pstore, psn->psn_src_nodes,
3276 sizeof(struct pf_src_node) * nr);
3278 free(pstore, M_TEMP);
3281 psn->psn_len = sizeof(struct pf_src_node) * nr;
3282 free(pstore, M_TEMP);
3286 case DIOCCLRSRCNODES: {
3288 pf_clear_srcnodes(NULL);
3289 pf_purge_expired_src_nodes();
3293 case DIOCKILLSRCNODES:
3294 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr);
3297 case DIOCSETHOSTID: {
3298 u_int32_t *hostid = (u_int32_t *)addr;
3302 V_pf_status.hostid = arc4random();
3304 V_pf_status.hostid = *hostid;
3315 case DIOCIGETIFACES: {
3316 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3317 struct pfi_kif *ifstore;
3320 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3325 bufsiz = io->pfiio_size * sizeof(struct pfi_kif);
3326 ifstore = mallocarray(io->pfiio_size, sizeof(struct pfi_kif),
3333 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size);
3335 error = copyout(ifstore, io->pfiio_buffer, bufsiz);
3336 free(ifstore, M_TEMP);
3340 case DIOCSETIFFLAG: {
3341 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3344 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3349 case DIOCCLRIFFLAG: {
3350 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3353 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3363 if (sx_xlocked(&pf_ioctl_lock))
3364 sx_xunlock(&pf_ioctl_lock);
3371 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
3373 bzero(sp, sizeof(struct pfsync_state));
3375 /* copy from state key */
3376 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
3377 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
3378 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
3379 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
3380 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
3381 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
3382 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
3383 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
3384 sp->proto = st->key[PF_SK_WIRE]->proto;
3385 sp->af = st->key[PF_SK_WIRE]->af;
3387 /* copy from state */
3388 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
3389 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
3390 sp->creation = htonl(time_uptime - st->creation);
3391 sp->expire = pf_state_expires(st);
3392 if (sp->expire <= time_uptime)
3393 sp->expire = htonl(0);
3395 sp->expire = htonl(sp->expire - time_uptime);
3397 sp->direction = st->direction;
3399 sp->timeout = st->timeout;
3400 sp->state_flags = st->state_flags;
3402 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
3403 if (st->nat_src_node)
3404 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
3407 sp->creatorid = st->creatorid;
3408 pf_state_peer_hton(&st->src, &sp->src);
3409 pf_state_peer_hton(&st->dst, &sp->dst);
3411 if (st->rule.ptr == NULL)
3412 sp->rule = htonl(-1);
3414 sp->rule = htonl(st->rule.ptr->nr);
3415 if (st->anchor.ptr == NULL)
3416 sp->anchor = htonl(-1);
3418 sp->anchor = htonl(st->anchor.ptr->nr);
3419 if (st->nat_rule.ptr == NULL)
3420 sp->nat_rule = htonl(-1);
3422 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
3424 pf_state_counter_hton(st->packets[0], sp->packets[0]);
3425 pf_state_counter_hton(st->packets[1], sp->packets[1]);
3426 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
3427 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
3432 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
3434 struct pfr_ktable *kt;
3436 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type));
3439 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
3440 kt = kt->pfrkt_root;
3442 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
3447 * XXX - Check for version missmatch!!!
3450 pf_clear_states(void)
3455 for (i = 0; i <= pf_hashmask; i++) {
3456 struct pf_idhash *ih = &V_pf_idhash[i];
3458 PF_HASHROW_LOCK(ih);
3459 LIST_FOREACH(s, &ih->states, entry) {
3460 s->timeout = PFTM_PURGE;
3461 /* Don't send out individual delete messages. */
3462 s->state_flags |= PFSTATE_NOSYNC;
3463 pf_unlink_state(s, PF_ENTER_LOCKED);
3466 PF_HASHROW_UNLOCK(ih);
3471 pf_clear_tables(void)
3473 struct pfioc_table io;
3476 bzero(&io, sizeof(io));
3478 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3485 pf_clear_srcnodes(struct pf_src_node *n)
3490 for (i = 0; i <= pf_hashmask; i++) {
3491 struct pf_idhash *ih = &V_pf_idhash[i];
3493 PF_HASHROW_LOCK(ih);
3494 LIST_FOREACH(s, &ih->states, entry) {
3495 if (n == NULL || n == s->src_node)
3497 if (n == NULL || n == s->nat_src_node)
3498 s->nat_src_node = NULL;
3500 PF_HASHROW_UNLOCK(ih);
3504 struct pf_srchash *sh;
3506 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
3508 PF_HASHROW_LOCK(sh);
3509 LIST_FOREACH(n, &sh->nodes, entry) {
3513 PF_HASHROW_UNLOCK(sh);
3516 /* XXX: hash slot should already be locked here. */
3523 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk)
3525 struct pf_src_node_list kill;
3528 for (int i = 0; i <= pf_srchashmask; i++) {
3529 struct pf_srchash *sh = &V_pf_srchash[i];
3530 struct pf_src_node *sn, *tmp;
3532 PF_HASHROW_LOCK(sh);
3533 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp)
3534 if (PF_MATCHA(psnk->psnk_src.neg,
3535 &psnk->psnk_src.addr.v.a.addr,
3536 &psnk->psnk_src.addr.v.a.mask,
3537 &sn->addr, sn->af) &&
3538 PF_MATCHA(psnk->psnk_dst.neg,
3539 &psnk->psnk_dst.addr.v.a.addr,
3540 &psnk->psnk_dst.addr.v.a.mask,
3541 &sn->raddr, sn->af)) {
3542 pf_unlink_src_node(sn);
3543 LIST_INSERT_HEAD(&kill, sn, entry);
3546 PF_HASHROW_UNLOCK(sh);
3549 for (int i = 0; i <= pf_hashmask; i++) {
3550 struct pf_idhash *ih = &V_pf_idhash[i];
3553 PF_HASHROW_LOCK(ih);
3554 LIST_FOREACH(s, &ih->states, entry) {
3555 if (s->src_node && s->src_node->expire == 1)
3557 if (s->nat_src_node && s->nat_src_node->expire == 1)
3558 s->nat_src_node = NULL;
3560 PF_HASHROW_UNLOCK(ih);
3563 psnk->psnk_killed = pf_free_src_nodes(&kill);
3567 * XXX - Check for version missmatch!!!
3571 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3581 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3583 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3586 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3588 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3589 break; /* XXX: rollback? */
3591 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3593 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3594 break; /* XXX: rollback? */
3596 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3598 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3599 break; /* XXX: rollback? */
3601 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3603 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3604 break; /* XXX: rollback? */
3607 /* XXX: these should always succeed here */
3608 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3609 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3610 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3611 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3612 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3614 if ((error = pf_clear_tables()) != 0)
3618 if ((error = pf_begin_altq(&t[0])) != 0) {
3619 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3622 pf_commit_altq(t[0]);
3627 pf_clear_srcnodes(NULL);
3629 /* status does not use malloced mem so no need to cleanup */
3630 /* fingerprints and interfaces have their own cleanup code */
3632 /* Free counters last as we updated them during shutdown. */
3633 counter_u64_free(V_pf_default_rule.states_cur);
3634 counter_u64_free(V_pf_default_rule.states_tot);
3635 counter_u64_free(V_pf_default_rule.src_nodes);
3637 for (int i = 0; i < PFRES_MAX; i++)
3638 counter_u64_free(V_pf_status.counters[i]);
3639 for (int i = 0; i < LCNT_MAX; i++)
3640 counter_u64_free(V_pf_status.lcounters[i]);
3641 for (int i = 0; i < FCNT_MAX; i++)
3642 counter_u64_free(V_pf_status.fcounters[i]);
3643 for (int i = 0; i < SCNT_MAX; i++)
3644 counter_u64_free(V_pf_status.scounters[i]);
3652 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3657 chk = pf_test(PF_IN, ifp, m, inp);
3669 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3674 chk = pf_test(PF_OUT, ifp, m, inp);
3688 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3694 * In case of loopback traffic IPv6 uses the real interface in
3695 * order to support scoped addresses. In order to support stateful
3696 * filtering we have change this to lo0 as it is the case in IPv4.
3698 CURVNET_SET(ifp->if_vnet);
3699 chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? V_loif : ifp, m, inp);
3711 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3716 CURVNET_SET(ifp->if_vnet);
3717 chk = pf_test6(PF_OUT, ifp, m, inp);
3733 struct pfil_head *pfh_inet;
3736 struct pfil_head *pfh_inet6;
3739 if (V_pf_pfil_hooked)
3743 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3744 if (pfh_inet == NULL)
3745 return (ESRCH); /* XXX */
3746 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3747 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3750 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3751 if (pfh_inet6 == NULL) {
3753 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3755 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3758 return (ESRCH); /* XXX */
3760 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3761 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3764 V_pf_pfil_hooked = 1;
3772 struct pfil_head *pfh_inet;
3775 struct pfil_head *pfh_inet6;
3778 if (V_pf_pfil_hooked == 0)
3782 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3783 if (pfh_inet == NULL)
3784 return (ESRCH); /* XXX */
3785 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3787 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3791 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3792 if (pfh_inet6 == NULL)
3793 return (ESRCH); /* XXX */
3794 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3796 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3800 V_pf_pfil_hooked = 0;
3807 TAILQ_INIT(&V_pf_tags);
3808 TAILQ_INIT(&V_pf_qids);
3811 V_pf_vnet_active = 1;
3819 rw_init(&pf_rules_lock, "pf rulesets");
3820 sx_init(&pf_ioctl_lock, "pf ioctl");
3821 sx_init(&pf_end_lock, "pf end thread");
3823 pf_mtag_initialize();
3825 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3830 error = kproc_create(pf_purge_thread, NULL, &pf_purge_proc, 0, 0, "pf purge");
3840 pf_unload_vnet(void)
3844 V_pf_vnet_active = 0;
3845 V_pf_status.running = 0;
3846 swi_remove(V_pf_swi_cookie);
3847 error = dehook_pf();
3850 * Should not happen!
3851 * XXX Due to error code ESRCH, kldunload will show
3852 * a message like 'No such process'.
3854 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3862 pf_unload_vnet_purge();
3864 pf_normalize_cleanup();
3871 if (IS_DEFAULT_VNET(curvnet))
3879 sx_xlock(&pf_end_lock);
3881 while (pf_end_threads < 2) {
3882 wakeup_one(pf_purge_thread);
3883 sx_sleep(pf_purge_proc, &pf_end_lock, 0, "pftmo", 0);
3885 sx_xunlock(&pf_end_lock);
3888 destroy_dev(pf_dev);
3892 rw_destroy(&pf_rules_lock);
3893 sx_destroy(&pf_ioctl_lock);
3894 sx_destroy(&pf_end_lock);
3898 vnet_pf_init(void *unused __unused)
3903 VNET_SYSINIT(vnet_pf_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD,
3904 vnet_pf_init, NULL);
3907 vnet_pf_uninit(const void *unused __unused)
3912 SYSUNINIT(pf_unload, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND, pf_unload, NULL);
3913 VNET_SYSUNINIT(vnet_pf_uninit, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD,
3914 vnet_pf_uninit, NULL);
3918 pf_modevent(module_t mod, int type, void *data)
3927 /* Handled in SYSUNINIT(pf_unload) to ensure it's done after
3928 * the vnet_pf_uninit()s */
3938 static moduledata_t pf_mod = {
3944 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND);
3945 MODULE_VERSION(pf, PF_MODVER);