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 = malloc(totlen, M_TEMP, M_WAITOK);
2535 error = copyin(io->pfrio_buffer, pfrts, totlen);
2537 free(pfrts, M_TEMP);
2541 error = pfr_add_tables(pfrts, io->pfrio_size,
2542 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2544 free(pfrts, M_TEMP);
2548 case DIOCRDELTABLES: {
2549 struct pfioc_table *io = (struct pfioc_table *)addr;
2550 struct pfr_table *pfrts;
2553 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2557 totlen = io->pfrio_size * sizeof(struct pfr_table);
2558 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2559 error = copyin(io->pfrio_buffer, pfrts, totlen);
2561 free(pfrts, M_TEMP);
2565 error = pfr_del_tables(pfrts, io->pfrio_size,
2566 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2568 free(pfrts, M_TEMP);
2572 case DIOCRGETTABLES: {
2573 struct pfioc_table *io = (struct pfioc_table *)addr;
2574 struct pfr_table *pfrts;
2577 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2581 totlen = io->pfrio_size * sizeof(struct pfr_table);
2582 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2584 error = pfr_get_tables(&io->pfrio_table, pfrts,
2585 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2588 error = copyout(pfrts, io->pfrio_buffer, totlen);
2589 free(pfrts, M_TEMP);
2593 case DIOCRGETTSTATS: {
2594 struct pfioc_table *io = (struct pfioc_table *)addr;
2595 struct pfr_tstats *pfrtstats;
2598 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2602 totlen = io->pfrio_size * sizeof(struct pfr_tstats);
2603 pfrtstats = malloc(totlen, M_TEMP, M_WAITOK);
2605 error = pfr_get_tstats(&io->pfrio_table, pfrtstats,
2606 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2609 error = copyout(pfrtstats, io->pfrio_buffer, totlen);
2610 free(pfrtstats, M_TEMP);
2614 case DIOCRCLRTSTATS: {
2615 struct pfioc_table *io = (struct pfioc_table *)addr;
2616 struct pfr_table *pfrts;
2619 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2623 totlen = io->pfrio_size * sizeof(struct pfr_table);
2624 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2625 error = copyin(io->pfrio_buffer, pfrts, totlen);
2627 free(pfrts, M_TEMP);
2631 error = pfr_clr_tstats(pfrts, io->pfrio_size,
2632 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2634 free(pfrts, M_TEMP);
2638 case DIOCRSETTFLAGS: {
2639 struct pfioc_table *io = (struct pfioc_table *)addr;
2640 struct pfr_table *pfrts;
2643 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2647 totlen = io->pfrio_size * sizeof(struct pfr_table);
2648 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2649 error = copyin(io->pfrio_buffer, pfrts, totlen);
2651 free(pfrts, M_TEMP);
2655 error = pfr_set_tflags(pfrts, io->pfrio_size,
2656 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2657 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2659 free(pfrts, M_TEMP);
2663 case DIOCRCLRADDRS: {
2664 struct pfioc_table *io = (struct pfioc_table *)addr;
2666 if (io->pfrio_esize != 0) {
2671 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2672 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2677 case DIOCRADDADDRS: {
2678 struct pfioc_table *io = (struct pfioc_table *)addr;
2679 struct pfr_addr *pfras;
2682 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2686 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2687 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2688 error = copyin(io->pfrio_buffer, pfras, totlen);
2690 free(pfras, M_TEMP);
2694 error = pfr_add_addrs(&io->pfrio_table, pfras,
2695 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2696 PFR_FLAG_USERIOCTL);
2698 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2699 error = copyout(pfras, io->pfrio_buffer, totlen);
2700 free(pfras, M_TEMP);
2704 case DIOCRDELADDRS: {
2705 struct pfioc_table *io = (struct pfioc_table *)addr;
2706 struct pfr_addr *pfras;
2709 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2713 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2714 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2715 error = copyin(io->pfrio_buffer, pfras, totlen);
2717 free(pfras, M_TEMP);
2721 error = pfr_del_addrs(&io->pfrio_table, pfras,
2722 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2723 PFR_FLAG_USERIOCTL);
2725 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2726 error = copyout(pfras, io->pfrio_buffer, totlen);
2727 free(pfras, M_TEMP);
2731 case DIOCRSETADDRS: {
2732 struct pfioc_table *io = (struct pfioc_table *)addr;
2733 struct pfr_addr *pfras;
2734 size_t totlen, count;
2736 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2740 count = max(io->pfrio_size, io->pfrio_size2);
2741 totlen = count * sizeof(struct pfr_addr);
2742 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2743 error = copyin(io->pfrio_buffer, pfras, totlen);
2745 free(pfras, M_TEMP);
2749 error = pfr_set_addrs(&io->pfrio_table, pfras,
2750 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2751 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2752 PFR_FLAG_USERIOCTL, 0);
2754 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2755 error = copyout(pfras, io->pfrio_buffer, totlen);
2756 free(pfras, M_TEMP);
2760 case DIOCRGETADDRS: {
2761 struct pfioc_table *io = (struct pfioc_table *)addr;
2762 struct pfr_addr *pfras;
2765 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2769 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2770 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2772 error = pfr_get_addrs(&io->pfrio_table, pfras,
2773 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2776 error = copyout(pfras, io->pfrio_buffer, totlen);
2777 free(pfras, M_TEMP);
2781 case DIOCRGETASTATS: {
2782 struct pfioc_table *io = (struct pfioc_table *)addr;
2783 struct pfr_astats *pfrastats;
2786 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2790 totlen = io->pfrio_size * sizeof(struct pfr_astats);
2791 pfrastats = malloc(totlen, M_TEMP, M_WAITOK);
2793 error = pfr_get_astats(&io->pfrio_table, pfrastats,
2794 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2797 error = copyout(pfrastats, io->pfrio_buffer, totlen);
2798 free(pfrastats, M_TEMP);
2802 case DIOCRCLRASTATS: {
2803 struct pfioc_table *io = (struct pfioc_table *)addr;
2804 struct pfr_addr *pfras;
2807 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2811 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2812 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2813 error = copyin(io->pfrio_buffer, pfras, totlen);
2815 free(pfras, M_TEMP);
2819 error = pfr_clr_astats(&io->pfrio_table, pfras,
2820 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2821 PFR_FLAG_USERIOCTL);
2823 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2824 error = copyout(pfras, io->pfrio_buffer, totlen);
2825 free(pfras, M_TEMP);
2829 case DIOCRTSTADDRS: {
2830 struct pfioc_table *io = (struct pfioc_table *)addr;
2831 struct pfr_addr *pfras;
2834 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2838 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2839 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2840 error = copyin(io->pfrio_buffer, pfras, totlen);
2842 free(pfras, M_TEMP);
2846 error = pfr_tst_addrs(&io->pfrio_table, pfras,
2847 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2848 PFR_FLAG_USERIOCTL);
2851 error = copyout(pfras, io->pfrio_buffer, totlen);
2852 free(pfras, M_TEMP);
2856 case DIOCRINADEFINE: {
2857 struct pfioc_table *io = (struct pfioc_table *)addr;
2858 struct pfr_addr *pfras;
2861 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2865 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2866 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2867 error = copyin(io->pfrio_buffer, pfras, totlen);
2869 free(pfras, M_TEMP);
2873 error = pfr_ina_define(&io->pfrio_table, pfras,
2874 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2875 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2877 free(pfras, M_TEMP);
2882 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2884 error = pf_osfp_add(io);
2890 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2892 error = pf_osfp_get(io);
2898 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2899 struct pfioc_trans_e *ioes, *ioe;
2903 if (io->esize != sizeof(*ioe)) {
2907 totlen = sizeof(struct pfioc_trans_e) * io->size;
2908 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2909 error = copyin(io->array, ioes, totlen);
2915 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2916 switch (ioe->rs_num) {
2918 case PF_RULESET_ALTQ:
2919 if (ioe->anchor[0]) {
2925 if ((error = pf_begin_altq(&ioe->ticket))) {
2932 case PF_RULESET_TABLE:
2934 struct pfr_table table;
2936 bzero(&table, sizeof(table));
2937 strlcpy(table.pfrt_anchor, ioe->anchor,
2938 sizeof(table.pfrt_anchor));
2939 if ((error = pfr_ina_begin(&table,
2940 &ioe->ticket, NULL, 0))) {
2948 if ((error = pf_begin_rules(&ioe->ticket,
2949 ioe->rs_num, ioe->anchor))) {
2958 error = copyout(ioes, io->array, totlen);
2963 case DIOCXROLLBACK: {
2964 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2965 struct pfioc_trans_e *ioe, *ioes;
2969 if (io->esize != sizeof(*ioe)) {
2973 totlen = sizeof(struct pfioc_trans_e) * io->size;
2974 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2975 error = copyin(io->array, ioes, totlen);
2981 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2982 switch (ioe->rs_num) {
2984 case PF_RULESET_ALTQ:
2985 if (ioe->anchor[0]) {
2991 if ((error = pf_rollback_altq(ioe->ticket))) {
2994 goto fail; /* really bad */
2998 case PF_RULESET_TABLE:
3000 struct pfr_table table;
3002 bzero(&table, sizeof(table));
3003 strlcpy(table.pfrt_anchor, ioe->anchor,
3004 sizeof(table.pfrt_anchor));
3005 if ((error = pfr_ina_rollback(&table,
3006 ioe->ticket, NULL, 0))) {
3009 goto fail; /* really bad */
3014 if ((error = pf_rollback_rules(ioe->ticket,
3015 ioe->rs_num, ioe->anchor))) {
3018 goto fail; /* really bad */
3029 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3030 struct pfioc_trans_e *ioe, *ioes;
3031 struct pf_ruleset *rs;
3035 if (io->esize != sizeof(*ioe)) {
3039 totlen = sizeof(struct pfioc_trans_e) * io->size;
3040 ioes = malloc(totlen, M_TEMP, M_WAITOK);
3041 error = copyin(io->array, ioes, totlen);
3047 /* First makes sure everything will succeed. */
3048 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3049 switch (ioe->rs_num) {
3051 case PF_RULESET_ALTQ:
3052 if (ioe->anchor[0]) {
3058 if (!V_altqs_inactive_open || ioe->ticket !=
3059 V_ticket_altqs_inactive) {
3067 case PF_RULESET_TABLE:
3068 rs = pf_find_ruleset(ioe->anchor);
3069 if (rs == NULL || !rs->topen || ioe->ticket !=
3078 if (ioe->rs_num < 0 || ioe->rs_num >=
3085 rs = pf_find_ruleset(ioe->anchor);
3087 !rs->rules[ioe->rs_num].inactive.open ||
3088 rs->rules[ioe->rs_num].inactive.ticket !=
3098 /* Now do the commit - no errors should happen here. */
3099 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3100 switch (ioe->rs_num) {
3102 case PF_RULESET_ALTQ:
3103 if ((error = pf_commit_altq(ioe->ticket))) {
3106 goto fail; /* really bad */
3110 case PF_RULESET_TABLE:
3112 struct pfr_table table;
3114 bzero(&table, sizeof(table));
3115 strlcpy(table.pfrt_anchor, ioe->anchor,
3116 sizeof(table.pfrt_anchor));
3117 if ((error = pfr_ina_commit(&table,
3118 ioe->ticket, NULL, NULL, 0))) {
3121 goto fail; /* really bad */
3126 if ((error = pf_commit_rules(ioe->ticket,
3127 ioe->rs_num, ioe->anchor))) {
3130 goto fail; /* really bad */
3140 case DIOCGETSRCNODES: {
3141 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3142 struct pf_srchash *sh;
3143 struct pf_src_node *n, *p, *pstore;
3146 if (psn->psn_len == 0) {
3147 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
3149 PF_HASHROW_LOCK(sh);
3150 LIST_FOREACH(n, &sh->nodes, entry)
3152 PF_HASHROW_UNLOCK(sh);
3154 psn->psn_len = sizeof(struct pf_src_node) * nr;
3158 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK);
3159 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
3161 PF_HASHROW_LOCK(sh);
3162 LIST_FOREACH(n, &sh->nodes, entry) {
3163 int secs = time_uptime, diff;
3165 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3168 bcopy(n, p, sizeof(struct pf_src_node));
3169 if (n->rule.ptr != NULL)
3170 p->rule.nr = n->rule.ptr->nr;
3171 p->creation = secs - p->creation;
3172 if (p->expire > secs)
3177 /* Adjust the connection rate estimate. */
3178 diff = secs - n->conn_rate.last;
3179 if (diff >= n->conn_rate.seconds)
3180 p->conn_rate.count = 0;
3182 p->conn_rate.count -=
3183 n->conn_rate.count * diff /
3184 n->conn_rate.seconds;
3188 PF_HASHROW_UNLOCK(sh);
3190 error = copyout(pstore, psn->psn_src_nodes,
3191 sizeof(struct pf_src_node) * nr);
3193 free(pstore, M_TEMP);
3196 psn->psn_len = sizeof(struct pf_src_node) * nr;
3197 free(pstore, M_TEMP);
3201 case DIOCCLRSRCNODES: {
3203 pf_clear_srcnodes(NULL);
3204 pf_purge_expired_src_nodes();
3208 case DIOCKILLSRCNODES:
3209 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr);
3212 case DIOCSETHOSTID: {
3213 u_int32_t *hostid = (u_int32_t *)addr;
3217 V_pf_status.hostid = arc4random();
3219 V_pf_status.hostid = *hostid;
3230 case DIOCIGETIFACES: {
3231 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3232 struct pfi_kif *ifstore;
3235 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3240 bufsiz = io->pfiio_size * sizeof(struct pfi_kif);
3241 ifstore = malloc(bufsiz, M_TEMP, M_WAITOK);
3243 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size);
3245 error = copyout(ifstore, io->pfiio_buffer, bufsiz);
3246 free(ifstore, M_TEMP);
3250 case DIOCSETIFFLAG: {
3251 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3254 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3259 case DIOCCLRIFFLAG: {
3260 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3263 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3273 if (sx_xlocked(&pf_ioctl_lock))
3274 sx_xunlock(&pf_ioctl_lock);
3281 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
3283 bzero(sp, sizeof(struct pfsync_state));
3285 /* copy from state key */
3286 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
3287 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
3288 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
3289 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
3290 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
3291 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
3292 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
3293 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
3294 sp->proto = st->key[PF_SK_WIRE]->proto;
3295 sp->af = st->key[PF_SK_WIRE]->af;
3297 /* copy from state */
3298 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
3299 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
3300 sp->creation = htonl(time_uptime - st->creation);
3301 sp->expire = pf_state_expires(st);
3302 if (sp->expire <= time_uptime)
3303 sp->expire = htonl(0);
3305 sp->expire = htonl(sp->expire - time_uptime);
3307 sp->direction = st->direction;
3309 sp->timeout = st->timeout;
3310 sp->state_flags = st->state_flags;
3312 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
3313 if (st->nat_src_node)
3314 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
3317 sp->creatorid = st->creatorid;
3318 pf_state_peer_hton(&st->src, &sp->src);
3319 pf_state_peer_hton(&st->dst, &sp->dst);
3321 if (st->rule.ptr == NULL)
3322 sp->rule = htonl(-1);
3324 sp->rule = htonl(st->rule.ptr->nr);
3325 if (st->anchor.ptr == NULL)
3326 sp->anchor = htonl(-1);
3328 sp->anchor = htonl(st->anchor.ptr->nr);
3329 if (st->nat_rule.ptr == NULL)
3330 sp->nat_rule = htonl(-1);
3332 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
3334 pf_state_counter_hton(st->packets[0], sp->packets[0]);
3335 pf_state_counter_hton(st->packets[1], sp->packets[1]);
3336 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
3337 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
3342 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
3344 struct pfr_ktable *kt;
3346 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type));
3349 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
3350 kt = kt->pfrkt_root;
3352 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
3357 * XXX - Check for version missmatch!!!
3360 pf_clear_states(void)
3365 for (i = 0; i <= pf_hashmask; i++) {
3366 struct pf_idhash *ih = &V_pf_idhash[i];
3368 PF_HASHROW_LOCK(ih);
3369 LIST_FOREACH(s, &ih->states, entry) {
3370 s->timeout = PFTM_PURGE;
3371 /* Don't send out individual delete messages. */
3372 s->state_flags |= PFSTATE_NOSYNC;
3373 pf_unlink_state(s, PF_ENTER_LOCKED);
3376 PF_HASHROW_UNLOCK(ih);
3381 pf_clear_tables(void)
3383 struct pfioc_table io;
3386 bzero(&io, sizeof(io));
3388 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3395 pf_clear_srcnodes(struct pf_src_node *n)
3400 for (i = 0; i <= pf_hashmask; i++) {
3401 struct pf_idhash *ih = &V_pf_idhash[i];
3403 PF_HASHROW_LOCK(ih);
3404 LIST_FOREACH(s, &ih->states, entry) {
3405 if (n == NULL || n == s->src_node)
3407 if (n == NULL || n == s->nat_src_node)
3408 s->nat_src_node = NULL;
3410 PF_HASHROW_UNLOCK(ih);
3414 struct pf_srchash *sh;
3416 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
3418 PF_HASHROW_LOCK(sh);
3419 LIST_FOREACH(n, &sh->nodes, entry) {
3423 PF_HASHROW_UNLOCK(sh);
3426 /* XXX: hash slot should already be locked here. */
3433 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk)
3435 struct pf_src_node_list kill;
3438 for (int i = 0; i <= pf_srchashmask; i++) {
3439 struct pf_srchash *sh = &V_pf_srchash[i];
3440 struct pf_src_node *sn, *tmp;
3442 PF_HASHROW_LOCK(sh);
3443 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp)
3444 if (PF_MATCHA(psnk->psnk_src.neg,
3445 &psnk->psnk_src.addr.v.a.addr,
3446 &psnk->psnk_src.addr.v.a.mask,
3447 &sn->addr, sn->af) &&
3448 PF_MATCHA(psnk->psnk_dst.neg,
3449 &psnk->psnk_dst.addr.v.a.addr,
3450 &psnk->psnk_dst.addr.v.a.mask,
3451 &sn->raddr, sn->af)) {
3452 pf_unlink_src_node(sn);
3453 LIST_INSERT_HEAD(&kill, sn, entry);
3456 PF_HASHROW_UNLOCK(sh);
3459 for (int i = 0; i <= pf_hashmask; i++) {
3460 struct pf_idhash *ih = &V_pf_idhash[i];
3463 PF_HASHROW_LOCK(ih);
3464 LIST_FOREACH(s, &ih->states, entry) {
3465 if (s->src_node && s->src_node->expire == 1)
3467 if (s->nat_src_node && s->nat_src_node->expire == 1)
3468 s->nat_src_node = NULL;
3470 PF_HASHROW_UNLOCK(ih);
3473 psnk->psnk_killed = pf_free_src_nodes(&kill);
3477 * XXX - Check for version missmatch!!!
3481 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3491 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3493 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3496 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3498 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3499 break; /* XXX: rollback? */
3501 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3503 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3504 break; /* XXX: rollback? */
3506 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3508 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3509 break; /* XXX: rollback? */
3511 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3513 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3514 break; /* XXX: rollback? */
3517 /* XXX: these should always succeed here */
3518 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3519 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3520 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3521 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3522 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3524 if ((error = pf_clear_tables()) != 0)
3528 if ((error = pf_begin_altq(&t[0])) != 0) {
3529 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3532 pf_commit_altq(t[0]);
3537 pf_clear_srcnodes(NULL);
3539 /* status does not use malloced mem so no need to cleanup */
3540 /* fingerprints and interfaces have their own cleanup code */
3542 /* Free counters last as we updated them during shutdown. */
3543 counter_u64_free(V_pf_default_rule.states_cur);
3544 counter_u64_free(V_pf_default_rule.states_tot);
3545 counter_u64_free(V_pf_default_rule.src_nodes);
3547 for (int i = 0; i < PFRES_MAX; i++)
3548 counter_u64_free(V_pf_status.counters[i]);
3549 for (int i = 0; i < LCNT_MAX; i++)
3550 counter_u64_free(V_pf_status.lcounters[i]);
3551 for (int i = 0; i < FCNT_MAX; i++)
3552 counter_u64_free(V_pf_status.fcounters[i]);
3553 for (int i = 0; i < SCNT_MAX; i++)
3554 counter_u64_free(V_pf_status.scounters[i]);
3562 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3567 chk = pf_test(PF_IN, ifp, m, inp);
3579 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3584 chk = pf_test(PF_OUT, ifp, m, inp);
3598 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3604 * In case of loopback traffic IPv6 uses the real interface in
3605 * order to support scoped addresses. In order to support stateful
3606 * filtering we have change this to lo0 as it is the case in IPv4.
3608 CURVNET_SET(ifp->if_vnet);
3609 chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? V_loif : ifp, m, inp);
3621 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3626 CURVNET_SET(ifp->if_vnet);
3627 chk = pf_test6(PF_OUT, ifp, m, inp);
3643 struct pfil_head *pfh_inet;
3646 struct pfil_head *pfh_inet6;
3649 if (V_pf_pfil_hooked)
3653 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3654 if (pfh_inet == NULL)
3655 return (ESRCH); /* XXX */
3656 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3657 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3660 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3661 if (pfh_inet6 == NULL) {
3663 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3665 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3668 return (ESRCH); /* XXX */
3670 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3671 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3674 V_pf_pfil_hooked = 1;
3682 struct pfil_head *pfh_inet;
3685 struct pfil_head *pfh_inet6;
3688 if (V_pf_pfil_hooked == 0)
3692 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3693 if (pfh_inet == NULL)
3694 return (ESRCH); /* XXX */
3695 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3697 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3701 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3702 if (pfh_inet6 == NULL)
3703 return (ESRCH); /* XXX */
3704 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3706 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3710 V_pf_pfil_hooked = 0;
3717 TAILQ_INIT(&V_pf_tags);
3718 TAILQ_INIT(&V_pf_qids);
3721 V_pf_vnet_active = 1;
3729 rw_init(&pf_rules_lock, "pf rulesets");
3730 sx_init(&pf_ioctl_lock, "pf ioctl");
3731 sx_init(&pf_end_lock, "pf end thread");
3733 pf_mtag_initialize();
3735 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3740 error = kproc_create(pf_purge_thread, NULL, &pf_purge_proc, 0, 0, "pf purge");
3750 pf_unload_vnet(void)
3754 V_pf_vnet_active = 0;
3755 V_pf_status.running = 0;
3756 swi_remove(V_pf_swi_cookie);
3757 error = dehook_pf();
3760 * Should not happen!
3761 * XXX Due to error code ESRCH, kldunload will show
3762 * a message like 'No such process'.
3764 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3772 pf_unload_vnet_purge();
3774 pf_normalize_cleanup();
3781 if (IS_DEFAULT_VNET(curvnet))
3789 sx_xlock(&pf_end_lock);
3791 while (pf_end_threads < 2) {
3792 wakeup_one(pf_purge_thread);
3793 sx_sleep(pf_purge_proc, &pf_end_lock, 0, "pftmo", 0);
3795 sx_xunlock(&pf_end_lock);
3798 destroy_dev(pf_dev);
3802 rw_destroy(&pf_rules_lock);
3803 sx_destroy(&pf_ioctl_lock);
3804 sx_destroy(&pf_end_lock);
3808 vnet_pf_init(void *unused __unused)
3813 VNET_SYSINIT(vnet_pf_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD,
3814 vnet_pf_init, NULL);
3817 vnet_pf_uninit(const void *unused __unused)
3822 SYSUNINIT(pf_unload, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND, pf_unload, NULL);
3823 VNET_SYSUNINIT(vnet_pf_uninit, SI_SUB_PROTO_FIREWALL, SI_ORDER_THIRD,
3824 vnet_pf_uninit, NULL);
3828 pf_modevent(module_t mod, int type, void *data)
3838 * Module should not be unloaded due to race conditions.
3843 /* Handled in SYSUNINIT(pf_unload) to ensure it's done after
3844 * the vnet_pf_uninit()s */
3854 static moduledata_t pf_mod = {
3860 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_SECOND);
3861 MODULE_VERSION(pf, PF_MODVER);