2 * Copyright (c) 2001 Daniel Hartmeier
3 * Copyright (c) 2002,2003 Henning Brauer
4 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 * Effort sponsored in part by the Defense Advanced Research Projects
32 * Agency (DARPA) and Air Force Research Laboratory, Air Force
33 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
35 * $OpenBSD: pf_ioctl.c,v 1.213 2009/02/15 21:46:12 mbalmer Exp $
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 #include "opt_inet6.h"
46 #include <sys/param.h>
49 #include <sys/endian.h>
50 #include <sys/fcntl.h>
51 #include <sys/filio.h>
52 #include <sys/interrupt.h>
54 #include <sys/kernel.h>
55 #include <sys/kthread.h>
58 #include <sys/module.h>
60 #include <sys/rwlock.h>
62 #include <sys/socket.h>
63 #include <sys/sysctl.h>
65 #include <sys/ucred.h>
68 #include <net/if_var.h>
70 #include <net/route.h>
72 #include <net/pfvar.h>
73 #include <net/if_pfsync.h>
74 #include <net/if_pflog.h>
76 #include <netinet/in.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_var.h>
79 #include <netinet6/ip6_var.h>
80 #include <netinet/ip_icmp.h>
83 #include <netinet/ip6.h>
87 #include <net/altq/altq.h>
90 static int pfattach(void);
91 static struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
92 u_int8_t, u_int8_t, u_int8_t);
94 static void pf_mv_pool(struct pf_palist *, struct pf_palist *);
95 static void pf_empty_pool(struct pf_palist *);
96 static int pfioctl(struct cdev *, u_long, caddr_t, int,
99 static int pf_begin_altq(u_int32_t *);
100 static int pf_rollback_altq(u_int32_t);
101 static int pf_commit_altq(u_int32_t);
102 static int pf_enable_altq(struct pf_altq *);
103 static int pf_disable_altq(struct pf_altq *);
104 static u_int32_t pf_qname2qid(char *);
105 static void pf_qid_unref(u_int32_t);
107 static int pf_begin_rules(u_int32_t *, int, const char *);
108 static int pf_rollback_rules(u_int32_t, int, char *);
109 static int pf_setup_pfsync_matching(struct pf_ruleset *);
110 static void pf_hash_rule(MD5_CTX *, struct pf_rule *);
111 static void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
112 static int pf_commit_rules(u_int32_t, int, char *);
113 static int pf_addr_setup(struct pf_ruleset *,
114 struct pf_addr_wrap *, sa_family_t);
115 static void pf_addr_copyout(struct pf_addr_wrap *);
117 VNET_DEFINE(struct pf_rule, pf_default_rule);
120 static VNET_DEFINE(int, pf_altq_running);
121 #define V_pf_altq_running VNET(pf_altq_running)
124 #define TAGID_MAX 50000
126 TAILQ_ENTRY(pf_tagname) entries;
127 char name[PF_TAG_NAME_SIZE];
132 TAILQ_HEAD(pf_tags, pf_tagname);
133 #define V_pf_tags VNET(pf_tags)
134 VNET_DEFINE(struct pf_tags, pf_tags);
135 #define V_pf_qids VNET(pf_qids)
136 VNET_DEFINE(struct pf_tags, pf_qids);
137 static MALLOC_DEFINE(M_PFTAG, "pf_tag", "pf(4) tag names");
138 static MALLOC_DEFINE(M_PFALTQ, "pf_altq", "pf(4) altq configuration db");
139 static MALLOC_DEFINE(M_PFRULE, "pf_rule", "pf(4) rules");
141 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
142 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
145 static u_int16_t tagname2tag(struct pf_tags *, char *);
146 static u_int16_t pf_tagname2tag(char *);
147 static void tag_unref(struct pf_tags *, u_int16_t);
149 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
154 * XXX - These are new and need to be checked when moveing to a new version
156 static void pf_clear_states(void);
157 static int pf_clear_tables(void);
158 static void pf_clear_srcnodes(struct pf_src_node *);
159 static void pf_kill_srcnodes(struct pfioc_src_node_kill *);
160 static void pf_tbladdr_copyout(struct pf_addr_wrap *);
163 * Wrapper functions for pfil(9) hooks
166 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
167 int dir, struct inpcb *inp);
168 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
169 int dir, struct inpcb *inp);
172 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
173 int dir, struct inpcb *inp);
174 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
175 int dir, struct inpcb *inp);
178 static int hook_pf(void);
179 static int dehook_pf(void);
180 static int shutdown_pf(void);
181 static int pf_load(void);
182 static int pf_unload(void);
184 static struct cdevsw pf_cdevsw = {
187 .d_version = D_VERSION,
190 static volatile VNET_DEFINE(int, pf_pfil_hooked);
191 #define V_pf_pfil_hooked VNET(pf_pfil_hooked)
192 VNET_DEFINE(int, pf_end_threads);
194 struct rwlock pf_rules_lock;
195 struct sx pf_ioctl_lock;
198 pfsync_state_import_t *pfsync_state_import_ptr = NULL;
199 pfsync_insert_state_t *pfsync_insert_state_ptr = NULL;
200 pfsync_update_state_t *pfsync_update_state_ptr = NULL;
201 pfsync_delete_state_t *pfsync_delete_state_ptr = NULL;
202 pfsync_clear_states_t *pfsync_clear_states_ptr = NULL;
203 pfsync_defer_t *pfsync_defer_ptr = NULL;
205 pflog_packet_t *pflog_packet_ptr = NULL;
210 u_int32_t *my_timeout = V_pf_default_rule.timeout;
213 if (IS_DEFAULT_VNET(curvnet))
214 pf_mtag_initialize();
220 V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
221 V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
223 RB_INIT(&V_pf_anchors);
224 pf_init_ruleset(&pf_main_ruleset);
226 /* default rule should never be garbage collected */
227 V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next;
228 #ifdef PF_DEFAULT_TO_DROP
229 V_pf_default_rule.action = PF_DROP;
231 V_pf_default_rule.action = PF_PASS;
233 V_pf_default_rule.nr = -1;
234 V_pf_default_rule.rtableid = -1;
236 V_pf_default_rule.states_cur = counter_u64_alloc(M_WAITOK);
237 V_pf_default_rule.states_tot = counter_u64_alloc(M_WAITOK);
238 V_pf_default_rule.src_nodes = counter_u64_alloc(M_WAITOK);
240 /* initialize default timeouts */
241 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
242 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
243 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
244 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
245 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
246 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
247 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
248 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
249 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
250 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
251 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
252 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
253 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
254 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
255 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
256 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
257 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
258 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
259 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
260 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
262 bzero(&V_pf_status, sizeof(V_pf_status));
263 V_pf_status.debug = PF_DEBUG_URGENT;
265 V_pf_pfil_hooked = 0;
267 /* XXX do our best to avoid a conflict */
268 V_pf_status.hostid = arc4random();
270 for (int i = 0; i < PFRES_MAX; i++)
271 V_pf_status.counters[i] = counter_u64_alloc(M_WAITOK);
272 for (int i = 0; i < LCNT_MAX; i++)
273 V_pf_status.lcounters[i] = counter_u64_alloc(M_WAITOK);
274 for (int i = 0; i < FCNT_MAX; i++)
275 V_pf_status.fcounters[i] = counter_u64_alloc(M_WAITOK);
276 for (int i = 0; i < SCNT_MAX; i++)
277 V_pf_status.scounters[i] = counter_u64_alloc(M_WAITOK);
279 if ((error = kproc_create(pf_purge_thread, curvnet, NULL, 0, 0,
281 /* XXXGL: leaked all above. */
283 if ((error = swi_add(NULL, "pf send", pf_intr, curvnet, SWI_NET,
284 INTR_MPSAFE, &V_pf_swi_cookie)) != 0)
285 /* XXXGL: leaked all above. */
291 static struct pf_pool *
292 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
293 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
294 u_int8_t check_ticket)
296 struct pf_ruleset *ruleset;
297 struct pf_rule *rule;
300 ruleset = pf_find_ruleset(anchor);
303 rs_num = pf_get_ruleset_number(rule_action);
304 if (rs_num >= PF_RULESET_MAX)
307 if (check_ticket && ticket !=
308 ruleset->rules[rs_num].active.ticket)
311 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
314 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
316 if (check_ticket && ticket !=
317 ruleset->rules[rs_num].inactive.ticket)
320 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
323 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
326 while ((rule != NULL) && (rule->nr != rule_number))
327 rule = TAILQ_NEXT(rule, entries);
332 return (&rule->rpool);
336 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
338 struct pf_pooladdr *mv_pool_pa;
340 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
341 TAILQ_REMOVE(poola, mv_pool_pa, entries);
342 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
347 pf_empty_pool(struct pf_palist *poola)
349 struct pf_pooladdr *pa;
351 while ((pa = TAILQ_FIRST(poola)) != NULL) {
352 switch (pa->addr.type) {
353 case PF_ADDR_DYNIFTL:
354 pfi_dynaddr_remove(pa->addr.p.dyn);
357 /* XXX: this could be unfinished pooladdr on pabuf */
358 if (pa->addr.p.tbl != NULL)
359 pfr_detach_table(pa->addr.p.tbl);
363 pfi_kif_unref(pa->kif);
364 TAILQ_REMOVE(poola, pa, entries);
370 pf_unlink_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
375 TAILQ_REMOVE(rulequeue, rule, entries);
377 PF_UNLNKDRULES_LOCK();
378 rule->rule_flag |= PFRULE_REFS;
379 TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries);
380 PF_UNLNKDRULES_UNLOCK();
384 pf_free_rule(struct pf_rule *rule)
390 tag_unref(&V_pf_tags, rule->tag);
392 tag_unref(&V_pf_tags, rule->match_tag);
394 if (rule->pqid != rule->qid)
395 pf_qid_unref(rule->pqid);
396 pf_qid_unref(rule->qid);
398 switch (rule->src.addr.type) {
399 case PF_ADDR_DYNIFTL:
400 pfi_dynaddr_remove(rule->src.addr.p.dyn);
403 pfr_detach_table(rule->src.addr.p.tbl);
406 switch (rule->dst.addr.type) {
407 case PF_ADDR_DYNIFTL:
408 pfi_dynaddr_remove(rule->dst.addr.p.dyn);
411 pfr_detach_table(rule->dst.addr.p.tbl);
414 if (rule->overload_tbl)
415 pfr_detach_table(rule->overload_tbl);
417 pfi_kif_unref(rule->kif);
418 pf_anchor_remove(rule);
419 pf_empty_pool(&rule->rpool.list);
420 counter_u64_free(rule->states_cur);
421 counter_u64_free(rule->states_tot);
422 counter_u64_free(rule->src_nodes);
423 free(rule, M_PFRULE);
427 tagname2tag(struct pf_tags *head, char *tagname)
429 struct pf_tagname *tag, *p = NULL;
430 u_int16_t new_tagid = 1;
434 TAILQ_FOREACH(tag, head, entries)
435 if (strcmp(tagname, tag->name) == 0) {
441 * to avoid fragmentation, we do a linear search from the beginning
442 * and take the first free slot we find. if there is none or the list
443 * is empty, append a new entry at the end.
447 if (!TAILQ_EMPTY(head))
448 for (p = TAILQ_FIRST(head); p != NULL &&
449 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
450 new_tagid = p->tag + 1;
452 if (new_tagid > TAGID_MAX)
455 /* allocate and fill new struct pf_tagname */
456 tag = malloc(sizeof(*tag), M_PFTAG, M_NOWAIT|M_ZERO);
459 strlcpy(tag->name, tagname, sizeof(tag->name));
460 tag->tag = new_tagid;
463 if (p != NULL) /* insert new entry before p */
464 TAILQ_INSERT_BEFORE(p, tag, entries);
465 else /* either list empty or no free slot in between */
466 TAILQ_INSERT_TAIL(head, tag, entries);
472 tag_unref(struct pf_tags *head, u_int16_t tag)
474 struct pf_tagname *p, *next;
478 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
479 next = TAILQ_NEXT(p, entries);
482 TAILQ_REMOVE(head, p, entries);
491 pf_tagname2tag(char *tagname)
493 return (tagname2tag(&V_pf_tags, tagname));
498 pf_qname2qid(char *qname)
500 return ((u_int32_t)tagname2tag(&V_pf_qids, qname));
504 pf_qid_unref(u_int32_t qid)
506 tag_unref(&V_pf_qids, (u_int16_t)qid);
510 pf_begin_altq(u_int32_t *ticket)
512 struct pf_altq *altq;
517 /* Purge the old altq list */
518 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
519 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
520 if (altq->qname[0] == 0 &&
521 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
522 /* detach and destroy the discipline */
523 error = altq_remove(altq);
525 pf_qid_unref(altq->qid);
526 free(altq, M_PFALTQ);
530 *ticket = ++V_ticket_altqs_inactive;
531 V_altqs_inactive_open = 1;
536 pf_rollback_altq(u_int32_t ticket)
538 struct pf_altq *altq;
543 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
545 /* Purge the old altq list */
546 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
547 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
548 if (altq->qname[0] == 0 &&
549 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
550 /* detach and destroy the discipline */
551 error = altq_remove(altq);
553 pf_qid_unref(altq->qid);
554 free(altq, M_PFALTQ);
556 V_altqs_inactive_open = 0;
561 pf_commit_altq(u_int32_t ticket)
563 struct pf_altqqueue *old_altqs;
564 struct pf_altq *altq;
569 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
572 /* swap altqs, keep the old. */
573 old_altqs = V_pf_altqs_active;
574 V_pf_altqs_active = V_pf_altqs_inactive;
575 V_pf_altqs_inactive = old_altqs;
576 V_ticket_altqs_active = V_ticket_altqs_inactive;
578 /* Attach new disciplines */
579 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
580 if (altq->qname[0] == 0 &&
581 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
582 /* attach the discipline */
583 error = altq_pfattach(altq);
584 if (error == 0 && V_pf_altq_running)
585 error = pf_enable_altq(altq);
591 /* Purge the old altq list */
592 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
593 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
594 if (altq->qname[0] == 0 &&
595 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
596 /* detach and destroy the discipline */
597 if (V_pf_altq_running)
598 error = pf_disable_altq(altq);
599 err = altq_pfdetach(altq);
600 if (err != 0 && error == 0)
602 err = altq_remove(altq);
603 if (err != 0 && error == 0)
606 pf_qid_unref(altq->qid);
607 free(altq, M_PFALTQ);
610 V_altqs_inactive_open = 0;
615 pf_enable_altq(struct pf_altq *altq)
618 struct tb_profile tb;
621 if ((ifp = ifunit(altq->ifname)) == NULL)
624 if (ifp->if_snd.altq_type != ALTQT_NONE)
625 error = altq_enable(&ifp->if_snd);
627 /* set tokenbucket regulator */
628 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
629 tb.rate = altq->ifbandwidth;
630 tb.depth = altq->tbrsize;
631 error = tbr_set(&ifp->if_snd, &tb);
638 pf_disable_altq(struct pf_altq *altq)
641 struct tb_profile tb;
644 if ((ifp = ifunit(altq->ifname)) == NULL)
648 * when the discipline is no longer referenced, it was overridden
649 * by a new one. if so, just return.
651 if (altq->altq_disc != ifp->if_snd.altq_disc)
654 error = altq_disable(&ifp->if_snd);
657 /* clear tokenbucket regulator */
659 error = tbr_set(&ifp->if_snd, &tb);
666 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
669 struct pf_altq *a1, *a2, *a3;
673 /* Interrupt userland queue modifications */
674 if (V_altqs_inactive_open)
675 pf_rollback_altq(V_ticket_altqs_inactive);
677 /* Start new altq ruleset */
678 if (pf_begin_altq(&ticket))
681 /* Copy the current active set */
682 TAILQ_FOREACH(a1, V_pf_altqs_active, entries) {
683 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
688 bcopy(a1, a2, sizeof(struct pf_altq));
690 if (a2->qname[0] != 0) {
691 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
696 a2->altq_disc = NULL;
697 TAILQ_FOREACH(a3, V_pf_altqs_inactive, entries) {
698 if (strncmp(a3->ifname, a2->ifname,
699 IFNAMSIZ) == 0 && a3->qname[0] == 0) {
700 a2->altq_disc = a3->altq_disc;
705 /* Deactivate the interface in question */
706 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
707 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
708 (remove && ifp1 == ifp)) {
709 a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
711 error = altq_add(a2);
713 if (ticket != V_ticket_altqs_inactive)
722 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries);
726 pf_rollback_altq(ticket);
728 pf_commit_altq(ticket);
733 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
735 struct pf_ruleset *rs;
736 struct pf_rule *rule;
740 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
742 rs = pf_find_or_create_ruleset(anchor);
745 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
746 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
747 rs->rules[rs_num].inactive.rcount--;
749 *ticket = ++rs->rules[rs_num].inactive.ticket;
750 rs->rules[rs_num].inactive.open = 1;
755 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
757 struct pf_ruleset *rs;
758 struct pf_rule *rule;
762 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
764 rs = pf_find_ruleset(anchor);
765 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
766 rs->rules[rs_num].inactive.ticket != ticket)
768 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
769 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
770 rs->rules[rs_num].inactive.rcount--;
772 rs->rules[rs_num].inactive.open = 0;
776 #define PF_MD5_UPD(st, elm) \
777 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
779 #define PF_MD5_UPD_STR(st, elm) \
780 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
782 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
783 (stor) = htonl((st)->elm); \
784 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
787 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
788 (stor) = htons((st)->elm); \
789 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
793 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
795 PF_MD5_UPD(pfr, addr.type);
796 switch (pfr->addr.type) {
797 case PF_ADDR_DYNIFTL:
798 PF_MD5_UPD(pfr, addr.v.ifname);
799 PF_MD5_UPD(pfr, addr.iflags);
802 PF_MD5_UPD(pfr, addr.v.tblname);
804 case PF_ADDR_ADDRMASK:
806 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
807 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
811 PF_MD5_UPD(pfr, port[0]);
812 PF_MD5_UPD(pfr, port[1]);
813 PF_MD5_UPD(pfr, neg);
814 PF_MD5_UPD(pfr, port_op);
818 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
823 pf_hash_rule_addr(ctx, &rule->src);
824 pf_hash_rule_addr(ctx, &rule->dst);
825 PF_MD5_UPD_STR(rule, label);
826 PF_MD5_UPD_STR(rule, ifname);
827 PF_MD5_UPD_STR(rule, match_tagname);
828 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
829 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
830 PF_MD5_UPD_HTONL(rule, prob, y);
831 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
832 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
833 PF_MD5_UPD(rule, uid.op);
834 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
835 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
836 PF_MD5_UPD(rule, gid.op);
837 PF_MD5_UPD_HTONL(rule, rule_flag, y);
838 PF_MD5_UPD(rule, action);
839 PF_MD5_UPD(rule, direction);
840 PF_MD5_UPD(rule, af);
841 PF_MD5_UPD(rule, quick);
842 PF_MD5_UPD(rule, ifnot);
843 PF_MD5_UPD(rule, match_tag_not);
844 PF_MD5_UPD(rule, natpass);
845 PF_MD5_UPD(rule, keep_state);
846 PF_MD5_UPD(rule, proto);
847 PF_MD5_UPD(rule, type);
848 PF_MD5_UPD(rule, code);
849 PF_MD5_UPD(rule, flags);
850 PF_MD5_UPD(rule, flagset);
851 PF_MD5_UPD(rule, allow_opts);
852 PF_MD5_UPD(rule, rt);
853 PF_MD5_UPD(rule, tos);
857 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
859 struct pf_ruleset *rs;
860 struct pf_rule *rule, **old_array;
861 struct pf_rulequeue *old_rules;
863 u_int32_t old_rcount;
867 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
869 rs = pf_find_ruleset(anchor);
870 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
871 ticket != rs->rules[rs_num].inactive.ticket)
874 /* Calculate checksum for the main ruleset */
875 if (rs == &pf_main_ruleset) {
876 error = pf_setup_pfsync_matching(rs);
881 /* Swap rules, keep the old. */
882 old_rules = rs->rules[rs_num].active.ptr;
883 old_rcount = rs->rules[rs_num].active.rcount;
884 old_array = rs->rules[rs_num].active.ptr_array;
886 rs->rules[rs_num].active.ptr =
887 rs->rules[rs_num].inactive.ptr;
888 rs->rules[rs_num].active.ptr_array =
889 rs->rules[rs_num].inactive.ptr_array;
890 rs->rules[rs_num].active.rcount =
891 rs->rules[rs_num].inactive.rcount;
892 rs->rules[rs_num].inactive.ptr = old_rules;
893 rs->rules[rs_num].inactive.ptr_array = old_array;
894 rs->rules[rs_num].inactive.rcount = old_rcount;
896 rs->rules[rs_num].active.ticket =
897 rs->rules[rs_num].inactive.ticket;
898 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
901 /* Purge the old rule list. */
902 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
903 pf_unlink_rule(old_rules, rule);
904 if (rs->rules[rs_num].inactive.ptr_array)
905 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
906 rs->rules[rs_num].inactive.ptr_array = NULL;
907 rs->rules[rs_num].inactive.rcount = 0;
908 rs->rules[rs_num].inactive.open = 0;
909 pf_remove_if_empty_ruleset(rs);
915 pf_setup_pfsync_matching(struct pf_ruleset *rs)
918 struct pf_rule *rule;
920 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
923 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
924 /* XXX PF_RULESET_SCRUB as well? */
925 if (rs_cnt == PF_RULESET_SCRUB)
928 if (rs->rules[rs_cnt].inactive.ptr_array)
929 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
930 rs->rules[rs_cnt].inactive.ptr_array = NULL;
932 if (rs->rules[rs_cnt].inactive.rcount) {
933 rs->rules[rs_cnt].inactive.ptr_array =
934 malloc(sizeof(caddr_t) *
935 rs->rules[rs_cnt].inactive.rcount,
938 if (!rs->rules[rs_cnt].inactive.ptr_array)
942 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
944 pf_hash_rule(&ctx, rule);
945 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
949 MD5Final(digest, &ctx);
950 memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum));
955 pf_addr_setup(struct pf_ruleset *ruleset, struct pf_addr_wrap *addr,
960 switch (addr->type) {
962 addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname);
963 if (addr->p.tbl == NULL)
966 case PF_ADDR_DYNIFTL:
967 error = pfi_dynaddr_setup(addr, af);
975 pf_addr_copyout(struct pf_addr_wrap *addr)
978 switch (addr->type) {
979 case PF_ADDR_DYNIFTL:
980 pfi_dynaddr_copyout(addr);
983 pf_tbladdr_copyout(addr);
989 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
993 /* XXX keep in sync with switch() below */
994 if (securelevel_gt(td->td_ucred, 2))
1001 case DIOCSETSTATUSIF:
1007 case DIOCGETTIMEOUT:
1008 case DIOCCLRRULECTRS:
1013 case DIOCGETRULESETS:
1014 case DIOCGETRULESET:
1015 case DIOCRGETTABLES:
1016 case DIOCRGETTSTATS:
1017 case DIOCRCLRTSTATS:
1023 case DIOCRGETASTATS:
1024 case DIOCRCLRASTATS:
1027 case DIOCGETSRCNODES:
1028 case DIOCCLRSRCNODES:
1029 case DIOCIGETIFACES:
1034 case DIOCRCLRTABLES:
1035 case DIOCRADDTABLES:
1036 case DIOCRDELTABLES:
1037 case DIOCRSETTFLAGS:
1038 if (((struct pfioc_table *)addr)->pfrio_flags &
1040 break; /* dummy operation ok */
1046 if (!(flags & FWRITE))
1054 case DIOCGETTIMEOUT:
1059 case DIOCGETRULESETS:
1060 case DIOCGETRULESET:
1062 case DIOCRGETTABLES:
1063 case DIOCRGETTSTATS:
1065 case DIOCRGETASTATS:
1068 case DIOCGETSRCNODES:
1069 case DIOCIGETIFACES:
1072 case DIOCRCLRTABLES:
1073 case DIOCRADDTABLES:
1074 case DIOCRDELTABLES:
1075 case DIOCRCLRTSTATS:
1080 case DIOCRSETTFLAGS:
1081 if (((struct pfioc_table *)addr)->pfrio_flags &
1083 flags |= FWRITE; /* need write lock for dummy */
1084 break; /* dummy operation ok */
1088 if (((struct pfioc_rule *)addr)->action ==
1096 CURVNET_SET(TD_TO_VNET(td));
1100 sx_xlock(&pf_ioctl_lock);
1101 if (V_pf_status.running)
1108 DPFPRINTF(PF_DEBUG_MISC,
1109 ("pf: pfil registration failed\n"));
1112 V_pf_status.running = 1;
1113 V_pf_status.since = time_second;
1116 V_pf_stateid[cpu] = time_second;
1118 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1123 sx_xlock(&pf_ioctl_lock);
1124 if (!V_pf_status.running)
1127 V_pf_status.running = 0;
1128 error = dehook_pf();
1130 V_pf_status.running = 1;
1131 DPFPRINTF(PF_DEBUG_MISC,
1132 ("pf: pfil unregistration failed\n"));
1134 V_pf_status.since = time_second;
1135 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1140 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1141 struct pf_ruleset *ruleset;
1142 struct pf_rule *rule, *tail;
1143 struct pf_pooladdr *pa;
1144 struct pfi_kif *kif = NULL;
1147 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1152 if (pr->rule.af == AF_INET) {
1153 error = EAFNOSUPPORT;
1158 if (pr->rule.af == AF_INET6) {
1159 error = EAFNOSUPPORT;
1164 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK);
1165 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1166 if (rule->ifname[0])
1167 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1168 rule->states_cur = counter_u64_alloc(M_WAITOK);
1169 rule->states_tot = counter_u64_alloc(M_WAITOK);
1170 rule->src_nodes = counter_u64_alloc(M_WAITOK);
1171 rule->cuid = td->td_ucred->cr_ruid;
1172 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1173 TAILQ_INIT(&rule->rpool.list);
1175 #define ERROUT(x) { error = (x); goto DIOCADDRULE_error; }
1178 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1179 ruleset = pf_find_ruleset(pr->anchor);
1180 if (ruleset == NULL)
1182 rs_num = pf_get_ruleset_number(pr->rule.action);
1183 if (rs_num >= PF_RULESET_MAX)
1185 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1186 DPFPRINTF(PF_DEBUG_MISC,
1187 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1188 ruleset->rules[rs_num].inactive.ticket));
1191 if (pr->pool_ticket != V_ticket_pabuf) {
1192 DPFPRINTF(PF_DEBUG_MISC,
1193 ("pool_ticket: %d != %d\n", pr->pool_ticket,
1198 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1201 rule->nr = tail->nr + 1;
1204 if (rule->ifname[0]) {
1205 rule->kif = pfi_kif_attach(kif, rule->ifname);
1206 pfi_kif_ref(rule->kif);
1210 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs)
1215 if (rule->qname[0] != 0) {
1216 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1218 else if (rule->pqname[0] != 0) {
1220 pf_qname2qid(rule->pqname)) == 0)
1223 rule->pqid = rule->qid;
1226 if (rule->tagname[0])
1227 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1229 if (rule->match_tagname[0])
1230 if ((rule->match_tag =
1231 pf_tagname2tag(rule->match_tagname)) == 0)
1233 if (rule->rt && !rule->direction)
1237 if (rule->logif >= PFLOGIFS_MAX)
1239 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
1241 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
1243 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1245 if (rule->scrub_flags & PFSTATE_SETPRIO &&
1246 (rule->set_prio[0] > PF_PRIO_MAX ||
1247 rule->set_prio[1] > PF_PRIO_MAX))
1249 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1250 if (pa->addr.type == PF_ADDR_TABLE) {
1251 pa->addr.p.tbl = pfr_attach_table(ruleset,
1252 pa->addr.v.tblname);
1253 if (pa->addr.p.tbl == NULL)
1257 if (rule->overload_tblname[0]) {
1258 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1259 rule->overload_tblname)) == NULL)
1262 rule->overload_tbl->pfrkt_flags |=
1266 pf_mv_pool(&V_pf_pabuf, &rule->rpool.list);
1267 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1268 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1269 (rule->rt > PF_FASTROUTE)) &&
1270 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1279 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1280 rule->evaluations = rule->packets[0] = rule->packets[1] =
1281 rule->bytes[0] = rule->bytes[1] = 0;
1282 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1284 ruleset->rules[rs_num].inactive.rcount++;
1291 counter_u64_free(rule->states_cur);
1292 counter_u64_free(rule->states_tot);
1293 counter_u64_free(rule->src_nodes);
1294 free(rule, M_PFRULE);
1296 free(kif, PFI_MTYPE);
1300 case DIOCGETRULES: {
1301 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1302 struct pf_ruleset *ruleset;
1303 struct pf_rule *tail;
1307 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1308 ruleset = pf_find_ruleset(pr->anchor);
1309 if (ruleset == NULL) {
1314 rs_num = pf_get_ruleset_number(pr->rule.action);
1315 if (rs_num >= PF_RULESET_MAX) {
1320 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1323 pr->nr = tail->nr + 1;
1326 pr->ticket = ruleset->rules[rs_num].active.ticket;
1332 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1333 struct pf_ruleset *ruleset;
1334 struct pf_rule *rule;
1338 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1339 ruleset = pf_find_ruleset(pr->anchor);
1340 if (ruleset == NULL) {
1345 rs_num = pf_get_ruleset_number(pr->rule.action);
1346 if (rs_num >= PF_RULESET_MAX) {
1351 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1356 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1357 while ((rule != NULL) && (rule->nr != pr->nr))
1358 rule = TAILQ_NEXT(rule, entries);
1364 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1365 pr->rule.u_states_cur = counter_u64_fetch(rule->states_cur);
1366 pr->rule.u_states_tot = counter_u64_fetch(rule->states_tot);
1367 pr->rule.u_src_nodes = counter_u64_fetch(rule->src_nodes);
1368 if (pf_anchor_copyout(ruleset, rule, pr)) {
1373 pf_addr_copyout(&pr->rule.src.addr);
1374 pf_addr_copyout(&pr->rule.dst.addr);
1375 for (i = 0; i < PF_SKIP_COUNT; ++i)
1376 if (rule->skip[i].ptr == NULL)
1377 pr->rule.skip[i].nr = -1;
1379 pr->rule.skip[i].nr =
1380 rule->skip[i].ptr->nr;
1382 if (pr->action == PF_GET_CLR_CNTR) {
1383 rule->evaluations = 0;
1384 rule->packets[0] = rule->packets[1] = 0;
1385 rule->bytes[0] = rule->bytes[1] = 0;
1386 counter_u64_zero(rule->states_tot);
1392 case DIOCCHANGERULE: {
1393 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1394 struct pf_ruleset *ruleset;
1395 struct pf_rule *oldrule = NULL, *newrule = NULL;
1396 struct pfi_kif *kif = NULL;
1397 struct pf_pooladdr *pa;
1401 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1402 pcr->action > PF_CHANGE_GET_TICKET) {
1406 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1411 if (pcr->action != PF_CHANGE_REMOVE) {
1413 if (pcr->rule.af == AF_INET) {
1414 error = EAFNOSUPPORT;
1419 if (pcr->rule.af == AF_INET6) {
1420 error = EAFNOSUPPORT;
1424 newrule = malloc(sizeof(*newrule), M_PFRULE, M_WAITOK);
1425 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1426 if (newrule->ifname[0])
1427 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1428 newrule->states_cur = counter_u64_alloc(M_WAITOK);
1429 newrule->states_tot = counter_u64_alloc(M_WAITOK);
1430 newrule->src_nodes = counter_u64_alloc(M_WAITOK);
1431 newrule->cuid = td->td_ucred->cr_ruid;
1432 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1433 TAILQ_INIT(&newrule->rpool.list);
1436 #define ERROUT(x) { error = (x); goto DIOCCHANGERULE_error; }
1439 if (!(pcr->action == PF_CHANGE_REMOVE ||
1440 pcr->action == PF_CHANGE_GET_TICKET) &&
1441 pcr->pool_ticket != V_ticket_pabuf)
1444 ruleset = pf_find_ruleset(pcr->anchor);
1445 if (ruleset == NULL)
1448 rs_num = pf_get_ruleset_number(pcr->rule.action);
1449 if (rs_num >= PF_RULESET_MAX)
1452 if (pcr->action == PF_CHANGE_GET_TICKET) {
1453 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1455 } else if (pcr->ticket !=
1456 ruleset->rules[rs_num].active.ticket)
1459 if (pcr->action != PF_CHANGE_REMOVE) {
1460 if (newrule->ifname[0]) {
1461 newrule->kif = pfi_kif_attach(kif,
1463 pfi_kif_ref(newrule->kif);
1465 newrule->kif = NULL;
1467 if (newrule->rtableid > 0 &&
1468 newrule->rtableid >= rt_numfibs)
1473 if (newrule->qname[0] != 0) {
1475 pf_qname2qid(newrule->qname)) == 0)
1477 else if (newrule->pqname[0] != 0) {
1478 if ((newrule->pqid =
1479 pf_qname2qid(newrule->pqname)) == 0)
1482 newrule->pqid = newrule->qid;
1485 if (newrule->tagname[0])
1487 pf_tagname2tag(newrule->tagname)) == 0)
1489 if (newrule->match_tagname[0])
1490 if ((newrule->match_tag = pf_tagname2tag(
1491 newrule->match_tagname)) == 0)
1493 if (newrule->rt && !newrule->direction)
1497 if (newrule->logif >= PFLOGIFS_MAX)
1499 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
1501 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
1503 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1505 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1506 if (pa->addr.type == PF_ADDR_TABLE) {
1508 pfr_attach_table(ruleset,
1509 pa->addr.v.tblname);
1510 if (pa->addr.p.tbl == NULL)
1514 if (newrule->overload_tblname[0]) {
1515 if ((newrule->overload_tbl = pfr_attach_table(
1516 ruleset, newrule->overload_tblname)) ==
1520 newrule->overload_tbl->pfrkt_flags |=
1524 pf_mv_pool(&V_pf_pabuf, &newrule->rpool.list);
1525 if (((((newrule->action == PF_NAT) ||
1526 (newrule->action == PF_RDR) ||
1527 (newrule->action == PF_BINAT) ||
1528 (newrule->rt > PF_FASTROUTE)) &&
1529 !newrule->anchor)) &&
1530 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1534 pf_free_rule(newrule);
1539 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1540 newrule->evaluations = 0;
1541 newrule->packets[0] = newrule->packets[1] = 0;
1542 newrule->bytes[0] = newrule->bytes[1] = 0;
1544 pf_empty_pool(&V_pf_pabuf);
1546 if (pcr->action == PF_CHANGE_ADD_HEAD)
1547 oldrule = TAILQ_FIRST(
1548 ruleset->rules[rs_num].active.ptr);
1549 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1550 oldrule = TAILQ_LAST(
1551 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1553 oldrule = TAILQ_FIRST(
1554 ruleset->rules[rs_num].active.ptr);
1555 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1556 oldrule = TAILQ_NEXT(oldrule, entries);
1557 if (oldrule == NULL) {
1558 if (newrule != NULL)
1559 pf_free_rule(newrule);
1566 if (pcr->action == PF_CHANGE_REMOVE) {
1567 pf_unlink_rule(ruleset->rules[rs_num].active.ptr,
1569 ruleset->rules[rs_num].active.rcount--;
1571 if (oldrule == NULL)
1573 ruleset->rules[rs_num].active.ptr,
1575 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1576 pcr->action == PF_CHANGE_ADD_BEFORE)
1577 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1580 ruleset->rules[rs_num].active.ptr,
1581 oldrule, newrule, entries);
1582 ruleset->rules[rs_num].active.rcount++;
1586 TAILQ_FOREACH(oldrule,
1587 ruleset->rules[rs_num].active.ptr, entries)
1590 ruleset->rules[rs_num].active.ticket++;
1592 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1593 pf_remove_if_empty_ruleset(ruleset);
1599 DIOCCHANGERULE_error:
1601 if (newrule != NULL) {
1602 counter_u64_free(newrule->states_cur);
1603 counter_u64_free(newrule->states_tot);
1604 counter_u64_free(newrule->src_nodes);
1605 free(newrule, M_PFRULE);
1608 free(kif, PFI_MTYPE);
1612 case DIOCCLRSTATES: {
1614 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1615 u_int i, killed = 0;
1617 for (i = 0; i <= pf_hashmask; i++) {
1618 struct pf_idhash *ih = &V_pf_idhash[i];
1620 relock_DIOCCLRSTATES:
1621 PF_HASHROW_LOCK(ih);
1622 LIST_FOREACH(s, &ih->states, entry)
1623 if (!psk->psk_ifname[0] ||
1624 !strcmp(psk->psk_ifname,
1625 s->kif->pfik_name)) {
1627 * Don't send out individual
1630 s->state_flags |= PFSTATE_NOSYNC;
1631 pf_unlink_state(s, PF_ENTER_LOCKED);
1633 goto relock_DIOCCLRSTATES;
1635 PF_HASHROW_UNLOCK(ih);
1637 psk->psk_killed = killed;
1638 if (pfsync_clear_states_ptr != NULL)
1639 pfsync_clear_states_ptr(V_pf_status.hostid, psk->psk_ifname);
1643 case DIOCKILLSTATES: {
1645 struct pf_state_key *sk;
1646 struct pf_addr *srcaddr, *dstaddr;
1647 u_int16_t srcport, dstport;
1648 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1649 u_int i, killed = 0;
1651 if (psk->psk_pfcmp.id) {
1652 if (psk->psk_pfcmp.creatorid == 0)
1653 psk->psk_pfcmp.creatorid = V_pf_status.hostid;
1654 if ((s = pf_find_state_byid(psk->psk_pfcmp.id,
1655 psk->psk_pfcmp.creatorid))) {
1656 pf_unlink_state(s, PF_ENTER_LOCKED);
1657 psk->psk_killed = 1;
1662 for (i = 0; i <= pf_hashmask; i++) {
1663 struct pf_idhash *ih = &V_pf_idhash[i];
1665 relock_DIOCKILLSTATES:
1666 PF_HASHROW_LOCK(ih);
1667 LIST_FOREACH(s, &ih->states, entry) {
1668 sk = s->key[PF_SK_WIRE];
1669 if (s->direction == PF_OUT) {
1670 srcaddr = &sk->addr[1];
1671 dstaddr = &sk->addr[0];
1672 srcport = sk->port[1];
1673 dstport = sk->port[0];
1675 srcaddr = &sk->addr[0];
1676 dstaddr = &sk->addr[1];
1677 srcport = sk->port[0];
1678 dstport = sk->port[1];
1681 if ((!psk->psk_af || sk->af == psk->psk_af)
1682 && (!psk->psk_proto || psk->psk_proto ==
1684 PF_MATCHA(psk->psk_src.neg,
1685 &psk->psk_src.addr.v.a.addr,
1686 &psk->psk_src.addr.v.a.mask,
1688 PF_MATCHA(psk->psk_dst.neg,
1689 &psk->psk_dst.addr.v.a.addr,
1690 &psk->psk_dst.addr.v.a.mask,
1692 (psk->psk_src.port_op == 0 ||
1693 pf_match_port(psk->psk_src.port_op,
1694 psk->psk_src.port[0], psk->psk_src.port[1],
1696 (psk->psk_dst.port_op == 0 ||
1697 pf_match_port(psk->psk_dst.port_op,
1698 psk->psk_dst.port[0], psk->psk_dst.port[1],
1700 (!psk->psk_label[0] ||
1701 (s->rule.ptr->label[0] &&
1702 !strcmp(psk->psk_label,
1703 s->rule.ptr->label))) &&
1704 (!psk->psk_ifname[0] ||
1705 !strcmp(psk->psk_ifname,
1706 s->kif->pfik_name))) {
1707 pf_unlink_state(s, PF_ENTER_LOCKED);
1709 goto relock_DIOCKILLSTATES;
1712 PF_HASHROW_UNLOCK(ih);
1714 psk->psk_killed = killed;
1718 case DIOCADDSTATE: {
1719 struct pfioc_state *ps = (struct pfioc_state *)addr;
1720 struct pfsync_state *sp = &ps->state;
1722 if (sp->timeout >= PFTM_MAX) {
1726 if (pfsync_state_import_ptr != NULL) {
1728 error = pfsync_state_import_ptr(sp, PFSYNC_SI_IOCTL);
1735 case DIOCGETSTATE: {
1736 struct pfioc_state *ps = (struct pfioc_state *)addr;
1739 s = pf_find_state_byid(ps->state.id, ps->state.creatorid);
1745 pfsync_state_export(&ps->state, s);
1750 case DIOCGETSTATES: {
1751 struct pfioc_states *ps = (struct pfioc_states *)addr;
1753 struct pfsync_state *pstore, *p;
1756 if (ps->ps_len == 0) {
1757 nr = uma_zone_get_cur(V_pf_state_z);
1758 ps->ps_len = sizeof(struct pfsync_state) * nr;
1762 p = pstore = malloc(ps->ps_len, M_TEMP, M_WAITOK);
1765 for (i = 0; i <= pf_hashmask; i++) {
1766 struct pf_idhash *ih = &V_pf_idhash[i];
1768 PF_HASHROW_LOCK(ih);
1769 LIST_FOREACH(s, &ih->states, entry) {
1771 if (s->timeout == PFTM_UNLINKED)
1774 if ((nr+1) * sizeof(*p) > ps->ps_len) {
1775 PF_HASHROW_UNLOCK(ih);
1776 goto DIOCGETSTATES_full;
1778 pfsync_state_export(p, s);
1782 PF_HASHROW_UNLOCK(ih);
1785 error = copyout(pstore, ps->ps_states,
1786 sizeof(struct pfsync_state) * nr);
1788 free(pstore, M_TEMP);
1791 ps->ps_len = sizeof(struct pfsync_state) * nr;
1792 free(pstore, M_TEMP);
1797 case DIOCGETSTATUS: {
1798 struct pf_status *s = (struct pf_status *)addr;
1801 s->running = V_pf_status.running;
1802 s->since = V_pf_status.since;
1803 s->debug = V_pf_status.debug;
1804 s->hostid = V_pf_status.hostid;
1805 s->states = V_pf_status.states;
1806 s->src_nodes = V_pf_status.src_nodes;
1808 for (int i = 0; i < PFRES_MAX; i++)
1810 counter_u64_fetch(V_pf_status.counters[i]);
1811 for (int i = 0; i < LCNT_MAX; i++)
1813 counter_u64_fetch(V_pf_status.lcounters[i]);
1814 for (int i = 0; i < FCNT_MAX; i++)
1816 counter_u64_fetch(V_pf_status.fcounters[i]);
1817 for (int i = 0; i < SCNT_MAX; i++)
1819 counter_u64_fetch(V_pf_status.scounters[i]);
1821 bcopy(V_pf_status.ifname, s->ifname, IFNAMSIZ);
1822 bcopy(V_pf_status.pf_chksum, s->pf_chksum,
1823 PF_MD5_DIGEST_LENGTH);
1825 pfi_update_status(s->ifname, s);
1830 case DIOCSETSTATUSIF: {
1831 struct pfioc_if *pi = (struct pfioc_if *)addr;
1833 if (pi->ifname[0] == 0) {
1834 bzero(V_pf_status.ifname, IFNAMSIZ);
1838 strlcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ);
1843 case DIOCCLRSTATUS: {
1845 for (int i = 0; i < PFRES_MAX; i++)
1846 counter_u64_zero(V_pf_status.counters[i]);
1847 for (int i = 0; i < FCNT_MAX; i++)
1848 counter_u64_zero(V_pf_status.fcounters[i]);
1849 for (int i = 0; i < SCNT_MAX; i++)
1850 counter_u64_zero(V_pf_status.scounters[i]);
1851 V_pf_status.since = time_second;
1852 if (*V_pf_status.ifname)
1853 pfi_update_status(V_pf_status.ifname, NULL);
1859 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1860 struct pf_state_key *sk;
1861 struct pf_state *state;
1862 struct pf_state_key_cmp key;
1863 int m = 0, direction = pnl->direction;
1866 /* NATLOOK src and dst are reversed, so reverse sidx/didx */
1867 sidx = (direction == PF_IN) ? 1 : 0;
1868 didx = (direction == PF_IN) ? 0 : 1;
1871 PF_AZERO(&pnl->saddr, pnl->af) ||
1872 PF_AZERO(&pnl->daddr, pnl->af) ||
1873 ((pnl->proto == IPPROTO_TCP ||
1874 pnl->proto == IPPROTO_UDP) &&
1875 (!pnl->dport || !pnl->sport)))
1878 bzero(&key, sizeof(key));
1880 key.proto = pnl->proto;
1881 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
1882 key.port[sidx] = pnl->sport;
1883 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
1884 key.port[didx] = pnl->dport;
1886 state = pf_find_state_all(&key, direction, &m);
1889 error = E2BIG; /* more than one state */
1890 else if (state != NULL) {
1891 /* XXXGL: not locked read */
1892 sk = state->key[sidx];
1893 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
1894 pnl->rsport = sk->port[sidx];
1895 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
1896 pnl->rdport = sk->port[didx];
1903 case DIOCSETTIMEOUT: {
1904 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1907 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1913 old = V_pf_default_rule.timeout[pt->timeout];
1914 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
1916 V_pf_default_rule.timeout[pt->timeout] = pt->seconds;
1917 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
1918 wakeup(pf_purge_thread);
1924 case DIOCGETTIMEOUT: {
1925 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1927 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1932 pt->seconds = V_pf_default_rule.timeout[pt->timeout];
1937 case DIOCGETLIMIT: {
1938 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1940 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1945 pl->limit = V_pf_limits[pl->index].limit;
1950 case DIOCSETLIMIT: {
1951 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1955 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1956 V_pf_limits[pl->index].zone == NULL) {
1961 uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit);
1962 old_limit = V_pf_limits[pl->index].limit;
1963 V_pf_limits[pl->index].limit = pl->limit;
1964 pl->limit = old_limit;
1969 case DIOCSETDEBUG: {
1970 u_int32_t *level = (u_int32_t *)addr;
1973 V_pf_status.debug = *level;
1978 case DIOCCLRRULECTRS: {
1979 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
1980 struct pf_ruleset *ruleset = &pf_main_ruleset;
1981 struct pf_rule *rule;
1985 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
1986 rule->evaluations = 0;
1987 rule->packets[0] = rule->packets[1] = 0;
1988 rule->bytes[0] = rule->bytes[1] = 0;
1994 case DIOCGIFSPEED: {
1995 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
1996 struct pf_ifspeed ps;
1999 if (psp->ifname[0] != 0) {
2000 /* Can we completely trust user-land? */
2001 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
2002 ifp = ifunit(ps.ifname);
2004 psp->baudrate = ifp->if_baudrate;
2013 case DIOCSTARTALTQ: {
2014 struct pf_altq *altq;
2017 /* enable all altq interfaces on active list */
2018 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
2019 if (altq->qname[0] == 0 && (altq->local_flags &
2020 PFALTQ_FLAG_IF_REMOVED) == 0) {
2021 error = pf_enable_altq(altq);
2027 V_pf_altq_running = 1;
2029 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
2033 case DIOCSTOPALTQ: {
2034 struct pf_altq *altq;
2037 /* disable all altq interfaces on active list */
2038 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
2039 if (altq->qname[0] == 0 && (altq->local_flags &
2040 PFALTQ_FLAG_IF_REMOVED) == 0) {
2041 error = pf_disable_altq(altq);
2047 V_pf_altq_running = 0;
2049 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
2054 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2055 struct pf_altq *altq, *a;
2058 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK);
2059 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
2060 altq->local_flags = 0;
2063 if (pa->ticket != V_ticket_altqs_inactive) {
2065 free(altq, M_PFALTQ);
2071 * if this is for a queue, find the discipline and
2072 * copy the necessary fields
2074 if (altq->qname[0] != 0) {
2075 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2078 free(altq, M_PFALTQ);
2081 altq->altq_disc = NULL;
2082 TAILQ_FOREACH(a, V_pf_altqs_inactive, entries) {
2083 if (strncmp(a->ifname, altq->ifname,
2084 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2085 altq->altq_disc = a->altq_disc;
2091 if ((ifp = ifunit(altq->ifname)) == NULL)
2092 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2094 error = altq_add(altq);
2098 free(altq, M_PFALTQ);
2102 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries);
2103 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2108 case DIOCGETALTQS: {
2109 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2110 struct pf_altq *altq;
2114 TAILQ_FOREACH(altq, V_pf_altqs_active, entries)
2116 pa->ticket = V_ticket_altqs_active;
2122 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2123 struct pf_altq *altq;
2127 if (pa->ticket != V_ticket_altqs_active) {
2133 altq = TAILQ_FIRST(V_pf_altqs_active);
2134 while ((altq != NULL) && (nr < pa->nr)) {
2135 altq = TAILQ_NEXT(altq, entries);
2143 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2148 case DIOCCHANGEALTQ:
2149 /* CHANGEALTQ not supported yet! */
2153 case DIOCGETQSTATS: {
2154 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2155 struct pf_altq *altq;
2160 if (pq->ticket != V_ticket_altqs_active) {
2165 nbytes = pq->nbytes;
2167 altq = TAILQ_FIRST(V_pf_altqs_active);
2168 while ((altq != NULL) && (nr < pq->nr)) {
2169 altq = TAILQ_NEXT(altq, entries);
2178 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2184 error = altq_getqstats(altq, pq->buf, &nbytes);
2186 pq->scheduler = altq->scheduler;
2187 pq->nbytes = nbytes;
2193 case DIOCBEGINADDRS: {
2194 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2197 pf_empty_pool(&V_pf_pabuf);
2198 pp->ticket = ++V_ticket_pabuf;
2204 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2205 struct pf_pooladdr *pa;
2206 struct pfi_kif *kif = NULL;
2209 if (pp->af == AF_INET) {
2210 error = EAFNOSUPPORT;
2215 if (pp->af == AF_INET6) {
2216 error = EAFNOSUPPORT;
2220 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2221 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2222 pp->addr.addr.type != PF_ADDR_TABLE) {
2226 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK);
2227 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2229 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2231 if (pp->ticket != V_ticket_pabuf) {
2234 free(kif, PFI_MTYPE);
2239 if (pa->ifname[0]) {
2240 pa->kif = pfi_kif_attach(kif, pa->ifname);
2241 pfi_kif_ref(pa->kif);
2244 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error =
2245 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) {
2247 pfi_kif_unref(pa->kif);
2252 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries);
2257 case DIOCGETADDRS: {
2258 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2259 struct pf_pool *pool;
2260 struct pf_pooladdr *pa;
2264 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2265 pp->r_num, 0, 1, 0);
2271 TAILQ_FOREACH(pa, &pool->list, entries)
2278 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2279 struct pf_pool *pool;
2280 struct pf_pooladdr *pa;
2284 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2285 pp->r_num, 0, 1, 1);
2291 pa = TAILQ_FIRST(&pool->list);
2292 while ((pa != NULL) && (nr < pp->nr)) {
2293 pa = TAILQ_NEXT(pa, entries);
2301 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2302 pf_addr_copyout(&pp->addr.addr);
2307 case DIOCCHANGEADDR: {
2308 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2309 struct pf_pool *pool;
2310 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2311 struct pf_ruleset *ruleset;
2312 struct pfi_kif *kif = NULL;
2314 if (pca->action < PF_CHANGE_ADD_HEAD ||
2315 pca->action > PF_CHANGE_REMOVE) {
2319 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2320 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2321 pca->addr.addr.type != PF_ADDR_TABLE) {
2326 if (pca->action != PF_CHANGE_REMOVE) {
2328 if (pca->af == AF_INET) {
2329 error = EAFNOSUPPORT;
2334 if (pca->af == AF_INET6) {
2335 error = EAFNOSUPPORT;
2339 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK);
2340 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2341 if (newpa->ifname[0])
2342 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2346 #define ERROUT(x) { error = (x); goto DIOCCHANGEADDR_error; }
2348 ruleset = pf_find_ruleset(pca->anchor);
2349 if (ruleset == NULL)
2352 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2353 pca->r_num, pca->r_last, 1, 1);
2357 if (pca->action != PF_CHANGE_REMOVE) {
2358 if (newpa->ifname[0]) {
2359 newpa->kif = pfi_kif_attach(kif, newpa->ifname);
2360 pfi_kif_ref(newpa->kif);
2364 switch (newpa->addr.type) {
2365 case PF_ADDR_DYNIFTL:
2366 error = pfi_dynaddr_setup(&newpa->addr,
2370 newpa->addr.p.tbl = pfr_attach_table(ruleset,
2371 newpa->addr.v.tblname);
2372 if (newpa->addr.p.tbl == NULL)
2377 goto DIOCCHANGEADDR_error;
2380 switch (pca->action) {
2381 case PF_CHANGE_ADD_HEAD:
2382 oldpa = TAILQ_FIRST(&pool->list);
2384 case PF_CHANGE_ADD_TAIL:
2385 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2388 oldpa = TAILQ_FIRST(&pool->list);
2389 for (int i = 0; oldpa && i < pca->nr; i++)
2390 oldpa = TAILQ_NEXT(oldpa, entries);
2396 if (pca->action == PF_CHANGE_REMOVE) {
2397 TAILQ_REMOVE(&pool->list, oldpa, entries);
2398 switch (oldpa->addr.type) {
2399 case PF_ADDR_DYNIFTL:
2400 pfi_dynaddr_remove(oldpa->addr.p.dyn);
2403 pfr_detach_table(oldpa->addr.p.tbl);
2407 pfi_kif_unref(oldpa->kif);
2408 free(oldpa, M_PFRULE);
2411 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2412 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2413 pca->action == PF_CHANGE_ADD_BEFORE)
2414 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2416 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2420 pool->cur = TAILQ_FIRST(&pool->list);
2421 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af);
2426 DIOCCHANGEADDR_error:
2428 pfi_kif_unref(newpa->kif);
2431 free(newpa, M_PFRULE);
2433 free(kif, PFI_MTYPE);
2437 case DIOCGETRULESETS: {
2438 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2439 struct pf_ruleset *ruleset;
2440 struct pf_anchor *anchor;
2443 pr->path[sizeof(pr->path) - 1] = 0;
2444 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2450 if (ruleset->anchor == NULL) {
2451 /* XXX kludge for pf_main_ruleset */
2452 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2453 if (anchor->parent == NULL)
2456 RB_FOREACH(anchor, pf_anchor_node,
2457 &ruleset->anchor->children)
2464 case DIOCGETRULESET: {
2465 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2466 struct pf_ruleset *ruleset;
2467 struct pf_anchor *anchor;
2471 pr->path[sizeof(pr->path) - 1] = 0;
2472 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2478 if (ruleset->anchor == NULL) {
2479 /* XXX kludge for pf_main_ruleset */
2480 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2481 if (anchor->parent == NULL && nr++ == pr->nr) {
2482 strlcpy(pr->name, anchor->name,
2487 RB_FOREACH(anchor, pf_anchor_node,
2488 &ruleset->anchor->children)
2489 if (nr++ == pr->nr) {
2490 strlcpy(pr->name, anchor->name,
2501 case DIOCRCLRTABLES: {
2502 struct pfioc_table *io = (struct pfioc_table *)addr;
2504 if (io->pfrio_esize != 0) {
2509 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2510 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2515 case DIOCRADDTABLES: {
2516 struct pfioc_table *io = (struct pfioc_table *)addr;
2517 struct pfr_table *pfrts;
2520 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2524 totlen = io->pfrio_size * sizeof(struct pfr_table);
2525 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2526 error = copyin(io->pfrio_buffer, pfrts, totlen);
2528 free(pfrts, M_TEMP);
2532 error = pfr_add_tables(pfrts, io->pfrio_size,
2533 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2535 free(pfrts, M_TEMP);
2539 case DIOCRDELTABLES: {
2540 struct pfioc_table *io = (struct pfioc_table *)addr;
2541 struct pfr_table *pfrts;
2544 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2548 totlen = io->pfrio_size * sizeof(struct pfr_table);
2549 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2550 error = copyin(io->pfrio_buffer, pfrts, totlen);
2552 free(pfrts, M_TEMP);
2556 error = pfr_del_tables(pfrts, io->pfrio_size,
2557 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2559 free(pfrts, M_TEMP);
2563 case DIOCRGETTABLES: {
2564 struct pfioc_table *io = (struct pfioc_table *)addr;
2565 struct pfr_table *pfrts;
2568 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2572 totlen = io->pfrio_size * sizeof(struct pfr_table);
2573 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2575 error = pfr_get_tables(&io->pfrio_table, pfrts,
2576 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2579 error = copyout(pfrts, io->pfrio_buffer, totlen);
2580 free(pfrts, M_TEMP);
2584 case DIOCRGETTSTATS: {
2585 struct pfioc_table *io = (struct pfioc_table *)addr;
2586 struct pfr_tstats *pfrtstats;
2589 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2593 totlen = io->pfrio_size * sizeof(struct pfr_tstats);
2594 pfrtstats = malloc(totlen, M_TEMP, M_WAITOK);
2596 error = pfr_get_tstats(&io->pfrio_table, pfrtstats,
2597 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2600 error = copyout(pfrtstats, io->pfrio_buffer, totlen);
2601 free(pfrtstats, M_TEMP);
2605 case DIOCRCLRTSTATS: {
2606 struct pfioc_table *io = (struct pfioc_table *)addr;
2607 struct pfr_table *pfrts;
2610 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2614 totlen = io->pfrio_size * sizeof(struct pfr_table);
2615 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2616 error = copyin(io->pfrio_buffer, pfrts, totlen);
2618 free(pfrts, M_TEMP);
2622 error = pfr_clr_tstats(pfrts, io->pfrio_size,
2623 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2625 free(pfrts, M_TEMP);
2629 case DIOCRSETTFLAGS: {
2630 struct pfioc_table *io = (struct pfioc_table *)addr;
2631 struct pfr_table *pfrts;
2634 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2638 totlen = io->pfrio_size * sizeof(struct pfr_table);
2639 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2640 error = copyin(io->pfrio_buffer, pfrts, totlen);
2642 free(pfrts, M_TEMP);
2646 error = pfr_set_tflags(pfrts, io->pfrio_size,
2647 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2648 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2650 free(pfrts, M_TEMP);
2654 case DIOCRCLRADDRS: {
2655 struct pfioc_table *io = (struct pfioc_table *)addr;
2657 if (io->pfrio_esize != 0) {
2662 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2663 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2668 case DIOCRADDADDRS: {
2669 struct pfioc_table *io = (struct pfioc_table *)addr;
2670 struct pfr_addr *pfras;
2673 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2677 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2678 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2679 error = copyin(io->pfrio_buffer, pfras, totlen);
2681 free(pfras, M_TEMP);
2685 error = pfr_add_addrs(&io->pfrio_table, pfras,
2686 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2687 PFR_FLAG_USERIOCTL);
2689 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2690 error = copyout(pfras, io->pfrio_buffer, totlen);
2691 free(pfras, M_TEMP);
2695 case DIOCRDELADDRS: {
2696 struct pfioc_table *io = (struct pfioc_table *)addr;
2697 struct pfr_addr *pfras;
2700 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2704 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2705 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2706 error = copyin(io->pfrio_buffer, pfras, totlen);
2708 free(pfras, M_TEMP);
2712 error = pfr_del_addrs(&io->pfrio_table, pfras,
2713 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2714 PFR_FLAG_USERIOCTL);
2716 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2717 error = copyout(pfras, io->pfrio_buffer, totlen);
2718 free(pfras, M_TEMP);
2722 case DIOCRSETADDRS: {
2723 struct pfioc_table *io = (struct pfioc_table *)addr;
2724 struct pfr_addr *pfras;
2725 size_t totlen, count;
2727 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2731 count = max(io->pfrio_size, io->pfrio_size2);
2732 totlen = count * sizeof(struct pfr_addr);
2733 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2734 error = copyin(io->pfrio_buffer, pfras, totlen);
2736 free(pfras, M_TEMP);
2740 error = pfr_set_addrs(&io->pfrio_table, pfras,
2741 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2742 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2743 PFR_FLAG_USERIOCTL, 0);
2745 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2746 error = copyout(pfras, io->pfrio_buffer, totlen);
2747 free(pfras, M_TEMP);
2751 case DIOCRGETADDRS: {
2752 struct pfioc_table *io = (struct pfioc_table *)addr;
2753 struct pfr_addr *pfras;
2756 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2760 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2761 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2763 error = pfr_get_addrs(&io->pfrio_table, pfras,
2764 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2767 error = copyout(pfras, io->pfrio_buffer, totlen);
2768 free(pfras, M_TEMP);
2772 case DIOCRGETASTATS: {
2773 struct pfioc_table *io = (struct pfioc_table *)addr;
2774 struct pfr_astats *pfrastats;
2777 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2781 totlen = io->pfrio_size * sizeof(struct pfr_astats);
2782 pfrastats = malloc(totlen, M_TEMP, M_WAITOK);
2784 error = pfr_get_astats(&io->pfrio_table, pfrastats,
2785 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2788 error = copyout(pfrastats, io->pfrio_buffer, totlen);
2789 free(pfrastats, M_TEMP);
2793 case DIOCRCLRASTATS: {
2794 struct pfioc_table *io = (struct pfioc_table *)addr;
2795 struct pfr_addr *pfras;
2798 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2802 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2803 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2804 error = copyin(io->pfrio_buffer, pfras, totlen);
2806 free(pfras, M_TEMP);
2810 error = pfr_clr_astats(&io->pfrio_table, pfras,
2811 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2812 PFR_FLAG_USERIOCTL);
2814 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2815 error = copyout(pfras, io->pfrio_buffer, totlen);
2816 free(pfras, M_TEMP);
2820 case DIOCRTSTADDRS: {
2821 struct pfioc_table *io = (struct pfioc_table *)addr;
2822 struct pfr_addr *pfras;
2825 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2829 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2830 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2831 error = copyin(io->pfrio_buffer, pfras, totlen);
2833 free(pfras, M_TEMP);
2837 error = pfr_tst_addrs(&io->pfrio_table, pfras,
2838 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2839 PFR_FLAG_USERIOCTL);
2842 error = copyout(pfras, io->pfrio_buffer, totlen);
2843 free(pfras, M_TEMP);
2847 case DIOCRINADEFINE: {
2848 struct pfioc_table *io = (struct pfioc_table *)addr;
2849 struct pfr_addr *pfras;
2852 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2856 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2857 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2858 error = copyin(io->pfrio_buffer, pfras, totlen);
2860 free(pfras, M_TEMP);
2864 error = pfr_ina_define(&io->pfrio_table, pfras,
2865 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2866 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2868 free(pfras, M_TEMP);
2873 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2875 error = pf_osfp_add(io);
2881 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2883 error = pf_osfp_get(io);
2889 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2890 struct pfioc_trans_e *ioes, *ioe;
2894 if (io->esize != sizeof(*ioe)) {
2898 totlen = sizeof(struct pfioc_trans_e) * io->size;
2899 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2900 error = copyin(io->array, ioes, totlen);
2906 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2907 switch (ioe->rs_num) {
2909 case PF_RULESET_ALTQ:
2910 if (ioe->anchor[0]) {
2916 if ((error = pf_begin_altq(&ioe->ticket))) {
2923 case PF_RULESET_TABLE:
2925 struct pfr_table table;
2927 bzero(&table, sizeof(table));
2928 strlcpy(table.pfrt_anchor, ioe->anchor,
2929 sizeof(table.pfrt_anchor));
2930 if ((error = pfr_ina_begin(&table,
2931 &ioe->ticket, NULL, 0))) {
2939 if ((error = pf_begin_rules(&ioe->ticket,
2940 ioe->rs_num, ioe->anchor))) {
2949 error = copyout(ioes, io->array, totlen);
2954 case DIOCXROLLBACK: {
2955 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2956 struct pfioc_trans_e *ioe, *ioes;
2960 if (io->esize != sizeof(*ioe)) {
2964 totlen = sizeof(struct pfioc_trans_e) * io->size;
2965 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2966 error = copyin(io->array, ioes, totlen);
2972 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2973 switch (ioe->rs_num) {
2975 case PF_RULESET_ALTQ:
2976 if (ioe->anchor[0]) {
2982 if ((error = pf_rollback_altq(ioe->ticket))) {
2985 goto fail; /* really bad */
2989 case PF_RULESET_TABLE:
2991 struct pfr_table table;
2993 bzero(&table, sizeof(table));
2994 strlcpy(table.pfrt_anchor, ioe->anchor,
2995 sizeof(table.pfrt_anchor));
2996 if ((error = pfr_ina_rollback(&table,
2997 ioe->ticket, NULL, 0))) {
3000 goto fail; /* really bad */
3005 if ((error = pf_rollback_rules(ioe->ticket,
3006 ioe->rs_num, ioe->anchor))) {
3009 goto fail; /* really bad */
3020 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3021 struct pfioc_trans_e *ioe, *ioes;
3022 struct pf_ruleset *rs;
3026 if (io->esize != sizeof(*ioe)) {
3030 totlen = sizeof(struct pfioc_trans_e) * io->size;
3031 ioes = malloc(totlen, M_TEMP, M_WAITOK);
3032 error = copyin(io->array, ioes, totlen);
3038 /* First makes sure everything will succeed. */
3039 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3040 switch (ioe->rs_num) {
3042 case PF_RULESET_ALTQ:
3043 if (ioe->anchor[0]) {
3049 if (!V_altqs_inactive_open || ioe->ticket !=
3050 V_ticket_altqs_inactive) {
3058 case PF_RULESET_TABLE:
3059 rs = pf_find_ruleset(ioe->anchor);
3060 if (rs == NULL || !rs->topen || ioe->ticket !=
3069 if (ioe->rs_num < 0 || ioe->rs_num >=
3076 rs = pf_find_ruleset(ioe->anchor);
3078 !rs->rules[ioe->rs_num].inactive.open ||
3079 rs->rules[ioe->rs_num].inactive.ticket !=
3089 /* Now do the commit - no errors should happen here. */
3090 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3091 switch (ioe->rs_num) {
3093 case PF_RULESET_ALTQ:
3094 if ((error = pf_commit_altq(ioe->ticket))) {
3097 goto fail; /* really bad */
3101 case PF_RULESET_TABLE:
3103 struct pfr_table table;
3105 bzero(&table, sizeof(table));
3106 strlcpy(table.pfrt_anchor, ioe->anchor,
3107 sizeof(table.pfrt_anchor));
3108 if ((error = pfr_ina_commit(&table,
3109 ioe->ticket, NULL, NULL, 0))) {
3112 goto fail; /* really bad */
3117 if ((error = pf_commit_rules(ioe->ticket,
3118 ioe->rs_num, ioe->anchor))) {
3121 goto fail; /* really bad */
3131 case DIOCGETSRCNODES: {
3132 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3133 struct pf_srchash *sh;
3134 struct pf_src_node *n, *p, *pstore;
3137 if (psn->psn_len == 0) {
3138 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
3140 PF_HASHROW_LOCK(sh);
3141 LIST_FOREACH(n, &sh->nodes, entry)
3143 PF_HASHROW_UNLOCK(sh);
3145 psn->psn_len = sizeof(struct pf_src_node) * nr;
3149 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK);
3150 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
3152 PF_HASHROW_LOCK(sh);
3153 LIST_FOREACH(n, &sh->nodes, entry) {
3154 int secs = time_uptime, diff;
3156 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3159 bcopy(n, p, sizeof(struct pf_src_node));
3160 if (n->rule.ptr != NULL)
3161 p->rule.nr = n->rule.ptr->nr;
3162 p->creation = secs - p->creation;
3163 if (p->expire > secs)
3168 /* Adjust the connection rate estimate. */
3169 diff = secs - n->conn_rate.last;
3170 if (diff >= n->conn_rate.seconds)
3171 p->conn_rate.count = 0;
3173 p->conn_rate.count -=
3174 n->conn_rate.count * diff /
3175 n->conn_rate.seconds;
3179 PF_HASHROW_UNLOCK(sh);
3181 error = copyout(pstore, psn->psn_src_nodes,
3182 sizeof(struct pf_src_node) * nr);
3184 free(pstore, M_TEMP);
3187 psn->psn_len = sizeof(struct pf_src_node) * nr;
3188 free(pstore, M_TEMP);
3192 case DIOCCLRSRCNODES: {
3194 pf_clear_srcnodes(NULL);
3195 pf_purge_expired_src_nodes();
3199 case DIOCKILLSRCNODES:
3200 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr);
3203 case DIOCSETHOSTID: {
3204 u_int32_t *hostid = (u_int32_t *)addr;
3208 V_pf_status.hostid = arc4random();
3210 V_pf_status.hostid = *hostid;
3221 case DIOCIGETIFACES: {
3222 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3223 struct pfi_kif *ifstore;
3226 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3231 bufsiz = io->pfiio_size * sizeof(struct pfi_kif);
3232 ifstore = malloc(bufsiz, M_TEMP, M_WAITOK);
3234 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size);
3236 error = copyout(ifstore, io->pfiio_buffer, bufsiz);
3237 free(ifstore, M_TEMP);
3241 case DIOCSETIFFLAG: {
3242 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3245 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3250 case DIOCCLRIFFLAG: {
3251 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3254 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3264 if (sx_xlocked(&pf_ioctl_lock))
3265 sx_xunlock(&pf_ioctl_lock);
3272 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
3274 bzero(sp, sizeof(struct pfsync_state));
3276 /* copy from state key */
3277 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
3278 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
3279 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
3280 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
3281 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
3282 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
3283 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
3284 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
3285 sp->proto = st->key[PF_SK_WIRE]->proto;
3286 sp->af = st->key[PF_SK_WIRE]->af;
3288 /* copy from state */
3289 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
3290 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
3291 sp->creation = htonl(time_uptime - st->creation);
3292 sp->expire = pf_state_expires(st);
3293 if (sp->expire <= time_uptime)
3294 sp->expire = htonl(0);
3296 sp->expire = htonl(sp->expire - time_uptime);
3298 sp->direction = st->direction;
3300 sp->timeout = st->timeout;
3301 sp->state_flags = st->state_flags;
3303 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
3304 if (st->nat_src_node)
3305 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
3308 sp->creatorid = st->creatorid;
3309 pf_state_peer_hton(&st->src, &sp->src);
3310 pf_state_peer_hton(&st->dst, &sp->dst);
3312 if (st->rule.ptr == NULL)
3313 sp->rule = htonl(-1);
3315 sp->rule = htonl(st->rule.ptr->nr);
3316 if (st->anchor.ptr == NULL)
3317 sp->anchor = htonl(-1);
3319 sp->anchor = htonl(st->anchor.ptr->nr);
3320 if (st->nat_rule.ptr == NULL)
3321 sp->nat_rule = htonl(-1);
3323 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
3325 pf_state_counter_hton(st->packets[0], sp->packets[0]);
3326 pf_state_counter_hton(st->packets[1], sp->packets[1]);
3327 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
3328 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
3333 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
3335 struct pfr_ktable *kt;
3337 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type));
3340 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
3341 kt = kt->pfrkt_root;
3343 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
3348 * XXX - Check for version missmatch!!!
3351 pf_clear_states(void)
3356 for (i = 0; i <= pf_hashmask; i++) {
3357 struct pf_idhash *ih = &V_pf_idhash[i];
3359 PF_HASHROW_LOCK(ih);
3360 LIST_FOREACH(s, &ih->states, entry) {
3361 s->timeout = PFTM_PURGE;
3362 /* Don't send out individual delete messages. */
3363 s->sync_state = PFSTATE_NOSYNC;
3364 pf_unlink_state(s, PF_ENTER_LOCKED);
3367 PF_HASHROW_UNLOCK(ih);
3372 pf_clear_tables(void)
3374 struct pfioc_table io;
3377 bzero(&io, sizeof(io));
3379 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3386 pf_clear_srcnodes(struct pf_src_node *n)
3391 for (i = 0; i <= pf_hashmask; i++) {
3392 struct pf_idhash *ih = &V_pf_idhash[i];
3394 PF_HASHROW_LOCK(ih);
3395 LIST_FOREACH(s, &ih->states, entry) {
3396 if (n == NULL || n == s->src_node)
3398 if (n == NULL || n == s->nat_src_node)
3399 s->nat_src_node = NULL;
3401 PF_HASHROW_UNLOCK(ih);
3405 struct pf_srchash *sh;
3407 for (i = 0, sh = V_pf_srchash; i <= pf_srchashmask;
3409 PF_HASHROW_LOCK(sh);
3410 LIST_FOREACH(n, &sh->nodes, entry) {
3414 PF_HASHROW_UNLOCK(sh);
3417 /* XXX: hash slot should already be locked here. */
3424 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk)
3426 struct pf_src_node_list kill;
3429 for (int i = 0; i <= pf_srchashmask; i++) {
3430 struct pf_srchash *sh = &V_pf_srchash[i];
3431 struct pf_src_node *sn, *tmp;
3433 PF_HASHROW_LOCK(sh);
3434 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp)
3435 if (PF_MATCHA(psnk->psnk_src.neg,
3436 &psnk->psnk_src.addr.v.a.addr,
3437 &psnk->psnk_src.addr.v.a.mask,
3438 &sn->addr, sn->af) &&
3439 PF_MATCHA(psnk->psnk_dst.neg,
3440 &psnk->psnk_dst.addr.v.a.addr,
3441 &psnk->psnk_dst.addr.v.a.mask,
3442 &sn->raddr, sn->af)) {
3443 pf_unlink_src_node(sn);
3444 LIST_INSERT_HEAD(&kill, sn, entry);
3447 PF_HASHROW_UNLOCK(sh);
3450 for (int i = 0; i <= pf_hashmask; i++) {
3451 struct pf_idhash *ih = &V_pf_idhash[i];
3454 PF_HASHROW_LOCK(ih);
3455 LIST_FOREACH(s, &ih->states, entry) {
3456 if (s->src_node && s->src_node->expire == 1)
3458 if (s->nat_src_node && s->nat_src_node->expire == 1)
3459 s->nat_src_node = NULL;
3461 PF_HASHROW_UNLOCK(ih);
3464 psnk->psnk_killed = pf_free_src_nodes(&kill);
3468 * XXX - Check for version missmatch!!!
3472 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3481 V_pf_status.running = 0;
3483 counter_u64_free(V_pf_default_rule.states_cur);
3484 counter_u64_free(V_pf_default_rule.states_tot);
3485 counter_u64_free(V_pf_default_rule.src_nodes);
3487 for (int i = 0; i < PFRES_MAX; i++)
3488 counter_u64_free(V_pf_status.counters[i]);
3489 for (int i = 0; i < LCNT_MAX; i++)
3490 counter_u64_free(V_pf_status.lcounters[i]);
3491 for (int i = 0; i < FCNT_MAX; i++)
3492 counter_u64_free(V_pf_status.fcounters[i]);
3493 for (int i = 0; i < SCNT_MAX; i++)
3494 counter_u64_free(V_pf_status.scounters[i]);
3497 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3499 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3502 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3504 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3505 break; /* XXX: rollback? */
3507 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3509 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3510 break; /* XXX: rollback? */
3512 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3514 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3515 break; /* XXX: rollback? */
3517 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3519 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3520 break; /* XXX: rollback? */
3523 /* XXX: these should always succeed here */
3524 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3525 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3526 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3527 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3528 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3530 if ((error = pf_clear_tables()) != 0)
3534 if ((error = pf_begin_altq(&t[0])) != 0) {
3535 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3538 pf_commit_altq(t[0]);
3543 pf_clear_srcnodes(NULL);
3545 /* status does not use malloced mem so no need to cleanup */
3546 /* fingerprints and interfaces have their own cleanup code */
3554 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3559 chk = pf_test(PF_IN, ifp, m, inp);
3569 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3574 chk = pf_test(PF_OUT, ifp, m, inp);
3586 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3592 * In case of loopback traffic IPv6 uses the real interface in
3593 * order to support scoped addresses. In order to support stateful
3594 * filtering we have change this to lo0 as it is the case in IPv4.
3596 CURVNET_SET(ifp->if_vnet);
3597 chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? V_loif : ifp, m, inp);
3607 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3612 CURVNET_SET(ifp->if_vnet);
3613 chk = pf_test6(PF_OUT, ifp, m, inp);
3627 struct pfil_head *pfh_inet;
3630 struct pfil_head *pfh_inet6;
3633 if (V_pf_pfil_hooked)
3637 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3638 if (pfh_inet == NULL)
3639 return (ESRCH); /* XXX */
3640 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3641 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3644 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3645 if (pfh_inet6 == NULL) {
3647 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3649 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3652 return (ESRCH); /* XXX */
3654 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3655 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3658 V_pf_pfil_hooked = 1;
3666 struct pfil_head *pfh_inet;
3669 struct pfil_head *pfh_inet6;
3672 if (V_pf_pfil_hooked == 0)
3676 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3677 if (pfh_inet == NULL)
3678 return (ESRCH); /* XXX */
3679 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3681 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3685 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3686 if (pfh_inet6 == NULL)
3687 return (ESRCH); /* XXX */
3688 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3690 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3694 V_pf_pfil_hooked = 0;
3703 VNET_ITERATOR_DECL(vnet_iter);
3706 VNET_FOREACH(vnet_iter) {
3707 CURVNET_SET(vnet_iter);
3708 V_pf_pfil_hooked = 0;
3709 V_pf_end_threads = 0;
3710 TAILQ_INIT(&V_pf_tags);
3711 TAILQ_INIT(&V_pf_qids);
3714 VNET_LIST_RUNLOCK();
3716 rw_init(&pf_rules_lock, "pf rulesets");
3717 sx_init(&pf_ioctl_lock, "pf ioctl");
3719 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3720 if ((error = pfattach()) != 0)
3731 V_pf_status.running = 0;
3732 swi_remove(V_pf_swi_cookie);
3733 error = dehook_pf();
3736 * Should not happen!
3737 * XXX Due to error code ESRCH, kldunload will show
3738 * a message like 'No such process'.
3740 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3745 V_pf_end_threads = 1;
3746 while (V_pf_end_threads < 2) {
3747 wakeup_one(pf_purge_thread);
3748 rw_sleep(pf_purge_thread, &pf_rules_lock, 0, "pftmo", 0);
3751 pf_normalize_cleanup();
3756 if (IS_DEFAULT_VNET(curvnet))
3758 destroy_dev(pf_dev);
3759 rw_destroy(&pf_rules_lock);
3760 sx_destroy(&pf_ioctl_lock);
3766 pf_modevent(module_t mod, int type, void *data)
3776 * Module should not be unloaded due to race conditions.
3781 error = pf_unload();
3791 static moduledata_t pf_mod = {
3797 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_FIRST);
3798 MODULE_VERSION(pf, PF_MODVER);