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>
57 #include <sys/module.h>
60 #include <sys/socket.h>
61 #include <sys/sysctl.h>
63 #include <sys/ucred.h>
66 #include <net/route.h>
68 #include <net/pfvar.h>
69 #include <net/if_pfsync.h>
70 #include <net/if_pflog.h>
72 #include <netinet/in.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_icmp.h>
78 #include <netinet/ip6.h>
82 #include <altq/altq.h>
85 static int pfattach(void);
86 static struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
87 u_int8_t, u_int8_t, u_int8_t);
89 static void pf_mv_pool(struct pf_palist *, struct pf_palist *);
90 static void pf_empty_pool(struct pf_palist *);
91 static int pfioctl(struct cdev *, u_long, caddr_t, int,
94 static int pf_begin_altq(u_int32_t *);
95 static int pf_rollback_altq(u_int32_t);
96 static int pf_commit_altq(u_int32_t);
97 static int pf_enable_altq(struct pf_altq *);
98 static int pf_disable_altq(struct pf_altq *);
99 static u_int32_t pf_qname2qid(char *);
100 static void pf_qid_unref(u_int32_t);
102 static int pf_begin_rules(u_int32_t *, int, const char *);
103 static int pf_rollback_rules(u_int32_t, int, char *);
104 static int pf_setup_pfsync_matching(struct pf_ruleset *);
105 static void pf_hash_rule(MD5_CTX *, struct pf_rule *);
106 static void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
107 static int pf_commit_rules(u_int32_t, int, char *);
108 static int pf_addr_setup(struct pf_ruleset *,
109 struct pf_addr_wrap *, sa_family_t);
110 static void pf_addr_copyout(struct pf_addr_wrap *);
112 VNET_DEFINE(struct pf_rule, pf_default_rule);
115 static VNET_DEFINE(int, pf_altq_running);
116 #define V_pf_altq_running VNET(pf_altq_running)
119 #define TAGID_MAX 50000
121 TAILQ_ENTRY(pf_tagname) entries;
122 char name[PF_TAG_NAME_SIZE];
127 TAILQ_HEAD(pf_tags, pf_tagname);
128 #define V_pf_tags VNET(pf_tags)
129 VNET_DEFINE(struct pf_tags, pf_tags);
130 #define V_pf_qids VNET(pf_qids)
131 VNET_DEFINE(struct pf_tags, pf_qids);
132 static MALLOC_DEFINE(M_PFTAG, "pf_tag", "pf(4) tag names");
133 static MALLOC_DEFINE(M_PFALTQ, "pf_altq", "pf(4) altq configuration db");
134 static MALLOC_DEFINE(M_PFRULE, "pf_rule", "pf(4) rules");
136 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
137 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
140 static u_int16_t tagname2tag(struct pf_tags *, char *);
141 static u_int16_t pf_tagname2tag(char *);
142 static void tag_unref(struct pf_tags *, u_int16_t);
144 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
149 * XXX - These are new and need to be checked when moveing to a new version
151 static void pf_clear_states(void);
152 static int pf_clear_tables(void);
153 static void pf_clear_srcnodes(struct pf_src_node *);
154 static void pf_kill_srcnodes(struct pfioc_src_node_kill *);
155 static void pf_tbladdr_copyout(struct pf_addr_wrap *);
158 * Wrapper functions for pfil(9) hooks
161 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
162 int dir, struct inpcb *inp);
163 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
164 int dir, struct inpcb *inp);
167 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
168 int dir, struct inpcb *inp);
169 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
170 int dir, struct inpcb *inp);
173 static int hook_pf(void);
174 static int dehook_pf(void);
175 static int shutdown_pf(void);
176 static int pf_load(void);
177 static int pf_unload(void);
179 static struct cdevsw pf_cdevsw = {
182 .d_version = D_VERSION,
185 static volatile VNET_DEFINE(int, pf_pfil_hooked);
186 #define V_pf_pfil_hooked VNET(pf_pfil_hooked)
187 VNET_DEFINE(int, pf_end_threads);
189 struct rwlock pf_rules_lock;
192 pfsync_state_import_t *pfsync_state_import_ptr = NULL;
193 pfsync_insert_state_t *pfsync_insert_state_ptr = NULL;
194 pfsync_update_state_t *pfsync_update_state_ptr = NULL;
195 pfsync_delete_state_t *pfsync_delete_state_ptr = NULL;
196 pfsync_clear_states_t *pfsync_clear_states_ptr = NULL;
197 pfsync_defer_t *pfsync_defer_ptr = NULL;
199 pflog_packet_t *pflog_packet_ptr = NULL;
204 u_int32_t *my_timeout = V_pf_default_rule.timeout;
207 if (IS_DEFAULT_VNET(curvnet))
208 pf_mtag_initialize();
214 V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
215 V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
217 RB_INIT(&V_pf_anchors);
218 pf_init_ruleset(&pf_main_ruleset);
220 /* default rule should never be garbage collected */
221 V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next;
222 #ifdef PF_DEFAULT_TO_DROP
223 V_pf_default_rule.action = PF_DROP;
225 V_pf_default_rule.action = PF_PASS;
227 V_pf_default_rule.nr = -1;
228 V_pf_default_rule.rtableid = -1;
230 V_pf_default_rule.states_cur = counter_u64_alloc(M_WAITOK);
231 V_pf_default_rule.states_tot = counter_u64_alloc(M_WAITOK);
232 V_pf_default_rule.src_nodes = counter_u64_alloc(M_WAITOK);
234 /* initialize default timeouts */
235 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
236 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
237 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
238 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
239 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
240 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
241 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
242 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
243 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
244 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
245 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
246 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
247 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
248 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
249 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
250 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
251 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
252 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
253 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
254 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
256 bzero(&V_pf_status, sizeof(V_pf_status));
257 V_pf_status.debug = PF_DEBUG_URGENT;
259 V_pf_pfil_hooked = 0;
261 /* XXX do our best to avoid a conflict */
262 V_pf_status.hostid = arc4random();
264 for (int i = 0; i < PFRES_MAX; i++)
265 V_pf_status.counters[i] = counter_u64_alloc(M_WAITOK);
266 for (int i = 0; i < LCNT_MAX; i++)
267 V_pf_status.lcounters[i] = counter_u64_alloc(M_WAITOK);
268 for (int i = 0; i < FCNT_MAX; i++)
269 V_pf_status.fcounters[i] = counter_u64_alloc(M_WAITOK);
270 for (int i = 0; i < SCNT_MAX; i++)
271 V_pf_status.scounters[i] = counter_u64_alloc(M_WAITOK);
273 if ((error = kproc_create(pf_purge_thread, curvnet, NULL, 0, 0,
275 /* XXXGL: leaked all above. */
277 if ((error = swi_add(NULL, "pf send", pf_intr, curvnet, SWI_NET,
278 INTR_MPSAFE, &V_pf_swi_cookie)) != 0)
279 /* XXXGL: leaked all above. */
285 static struct pf_pool *
286 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
287 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
288 u_int8_t check_ticket)
290 struct pf_ruleset *ruleset;
291 struct pf_rule *rule;
294 ruleset = pf_find_ruleset(anchor);
297 rs_num = pf_get_ruleset_number(rule_action);
298 if (rs_num >= PF_RULESET_MAX)
301 if (check_ticket && ticket !=
302 ruleset->rules[rs_num].active.ticket)
305 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
308 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
310 if (check_ticket && ticket !=
311 ruleset->rules[rs_num].inactive.ticket)
314 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
317 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
320 while ((rule != NULL) && (rule->nr != rule_number))
321 rule = TAILQ_NEXT(rule, entries);
326 return (&rule->rpool);
330 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
332 struct pf_pooladdr *mv_pool_pa;
334 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
335 TAILQ_REMOVE(poola, mv_pool_pa, entries);
336 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
341 pf_empty_pool(struct pf_palist *poola)
343 struct pf_pooladdr *pa;
345 while ((pa = TAILQ_FIRST(poola)) != NULL) {
346 switch (pa->addr.type) {
347 case PF_ADDR_DYNIFTL:
348 pfi_dynaddr_remove(pa->addr.p.dyn);
351 pfr_detach_table(pa->addr.p.tbl);
355 pfi_kif_unref(pa->kif);
356 TAILQ_REMOVE(poola, pa, entries);
362 pf_unlink_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
367 TAILQ_REMOVE(rulequeue, rule, entries);
369 PF_UNLNKDRULES_LOCK();
370 rule->rule_flag |= PFRULE_REFS;
371 TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries);
372 PF_UNLNKDRULES_UNLOCK();
376 pf_free_rule(struct pf_rule *rule)
382 tag_unref(&V_pf_tags, rule->tag);
384 tag_unref(&V_pf_tags, rule->match_tag);
386 if (rule->pqid != rule->qid)
387 pf_qid_unref(rule->pqid);
388 pf_qid_unref(rule->qid);
390 switch (rule->src.addr.type) {
391 case PF_ADDR_DYNIFTL:
392 pfi_dynaddr_remove(rule->src.addr.p.dyn);
395 pfr_detach_table(rule->src.addr.p.tbl);
398 switch (rule->dst.addr.type) {
399 case PF_ADDR_DYNIFTL:
400 pfi_dynaddr_remove(rule->dst.addr.p.dyn);
403 pfr_detach_table(rule->dst.addr.p.tbl);
406 if (rule->overload_tbl)
407 pfr_detach_table(rule->overload_tbl);
409 pfi_kif_unref(rule->kif);
410 pf_anchor_remove(rule);
411 pf_empty_pool(&rule->rpool.list);
412 counter_u64_free(rule->states_cur);
413 counter_u64_free(rule->states_tot);
414 counter_u64_free(rule->src_nodes);
415 free(rule, M_PFRULE);
419 tagname2tag(struct pf_tags *head, char *tagname)
421 struct pf_tagname *tag, *p = NULL;
422 u_int16_t new_tagid = 1;
426 TAILQ_FOREACH(tag, head, entries)
427 if (strcmp(tagname, tag->name) == 0) {
433 * to avoid fragmentation, we do a linear search from the beginning
434 * and take the first free slot we find. if there is none or the list
435 * is empty, append a new entry at the end.
439 if (!TAILQ_EMPTY(head))
440 for (p = TAILQ_FIRST(head); p != NULL &&
441 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
442 new_tagid = p->tag + 1;
444 if (new_tagid > TAGID_MAX)
447 /* allocate and fill new struct pf_tagname */
448 tag = malloc(sizeof(*tag), M_PFTAG, M_NOWAIT|M_ZERO);
451 strlcpy(tag->name, tagname, sizeof(tag->name));
452 tag->tag = new_tagid;
455 if (p != NULL) /* insert new entry before p */
456 TAILQ_INSERT_BEFORE(p, tag, entries);
457 else /* either list empty or no free slot in between */
458 TAILQ_INSERT_TAIL(head, tag, entries);
464 tag_unref(struct pf_tags *head, u_int16_t tag)
466 struct pf_tagname *p, *next;
470 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
471 next = TAILQ_NEXT(p, entries);
474 TAILQ_REMOVE(head, p, entries);
483 pf_tagname2tag(char *tagname)
485 return (tagname2tag(&V_pf_tags, tagname));
490 pf_qname2qid(char *qname)
492 return ((u_int32_t)tagname2tag(&V_pf_qids, qname));
496 pf_qid_unref(u_int32_t qid)
498 tag_unref(&V_pf_qids, (u_int16_t)qid);
502 pf_begin_altq(u_int32_t *ticket)
504 struct pf_altq *altq;
509 /* Purge the old altq list */
510 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
511 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
512 if (altq->qname[0] == 0 &&
513 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
514 /* detach and destroy the discipline */
515 error = altq_remove(altq);
517 pf_qid_unref(altq->qid);
518 free(altq, M_PFALTQ);
522 *ticket = ++V_ticket_altqs_inactive;
523 V_altqs_inactive_open = 1;
528 pf_rollback_altq(u_int32_t ticket)
530 struct pf_altq *altq;
535 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
537 /* Purge the old altq list */
538 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
539 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
540 if (altq->qname[0] == 0 &&
541 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
542 /* detach and destroy the discipline */
543 error = altq_remove(altq);
545 pf_qid_unref(altq->qid);
546 free(altq, M_PFALTQ);
548 V_altqs_inactive_open = 0;
553 pf_commit_altq(u_int32_t ticket)
555 struct pf_altqqueue *old_altqs;
556 struct pf_altq *altq;
561 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
564 /* swap altqs, keep the old. */
565 old_altqs = V_pf_altqs_active;
566 V_pf_altqs_active = V_pf_altqs_inactive;
567 V_pf_altqs_inactive = old_altqs;
568 V_ticket_altqs_active = V_ticket_altqs_inactive;
570 /* Attach new disciplines */
571 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
572 if (altq->qname[0] == 0 &&
573 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
574 /* attach the discipline */
575 error = altq_pfattach(altq);
576 if (error == 0 && V_pf_altq_running)
577 error = pf_enable_altq(altq);
583 /* Purge the old altq list */
584 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
585 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
586 if (altq->qname[0] == 0 &&
587 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
588 /* detach and destroy the discipline */
589 if (V_pf_altq_running)
590 error = pf_disable_altq(altq);
591 err = altq_pfdetach(altq);
592 if (err != 0 && error == 0)
594 err = altq_remove(altq);
595 if (err != 0 && error == 0)
598 pf_qid_unref(altq->qid);
599 free(altq, M_PFALTQ);
602 V_altqs_inactive_open = 0;
607 pf_enable_altq(struct pf_altq *altq)
610 struct tb_profile tb;
613 if ((ifp = ifunit(altq->ifname)) == NULL)
616 if (ifp->if_snd.altq_type != ALTQT_NONE)
617 error = altq_enable(&ifp->if_snd);
619 /* set tokenbucket regulator */
620 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
621 tb.rate = altq->ifbandwidth;
622 tb.depth = altq->tbrsize;
623 error = tbr_set(&ifp->if_snd, &tb);
630 pf_disable_altq(struct pf_altq *altq)
633 struct tb_profile tb;
636 if ((ifp = ifunit(altq->ifname)) == NULL)
640 * when the discipline is no longer referenced, it was overridden
641 * by a new one. if so, just return.
643 if (altq->altq_disc != ifp->if_snd.altq_disc)
646 error = altq_disable(&ifp->if_snd);
649 /* clear tokenbucket regulator */
651 error = tbr_set(&ifp->if_snd, &tb);
658 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
661 struct pf_altq *a1, *a2, *a3;
665 /* Interrupt userland queue modifications */
666 if (V_altqs_inactive_open)
667 pf_rollback_altq(V_ticket_altqs_inactive);
669 /* Start new altq ruleset */
670 if (pf_begin_altq(&ticket))
673 /* Copy the current active set */
674 TAILQ_FOREACH(a1, V_pf_altqs_active, entries) {
675 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
680 bcopy(a1, a2, sizeof(struct pf_altq));
682 if (a2->qname[0] != 0) {
683 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
688 a2->altq_disc = NULL;
689 TAILQ_FOREACH(a3, V_pf_altqs_inactive, entries) {
690 if (strncmp(a3->ifname, a2->ifname,
691 IFNAMSIZ) == 0 && a3->qname[0] == 0) {
692 a2->altq_disc = a3->altq_disc;
697 /* Deactivate the interface in question */
698 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
699 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
700 (remove && ifp1 == ifp)) {
701 a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
703 error = altq_add(a2);
705 if (ticket != V_ticket_altqs_inactive)
714 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries);
718 pf_rollback_altq(ticket);
720 pf_commit_altq(ticket);
725 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
727 struct pf_ruleset *rs;
728 struct pf_rule *rule;
732 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
734 rs = pf_find_or_create_ruleset(anchor);
737 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
738 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
739 rs->rules[rs_num].inactive.rcount--;
741 *ticket = ++rs->rules[rs_num].inactive.ticket;
742 rs->rules[rs_num].inactive.open = 1;
747 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
749 struct pf_ruleset *rs;
750 struct pf_rule *rule;
754 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
756 rs = pf_find_ruleset(anchor);
757 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
758 rs->rules[rs_num].inactive.ticket != ticket)
760 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
761 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
762 rs->rules[rs_num].inactive.rcount--;
764 rs->rules[rs_num].inactive.open = 0;
768 #define PF_MD5_UPD(st, elm) \
769 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
771 #define PF_MD5_UPD_STR(st, elm) \
772 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
774 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
775 (stor) = htonl((st)->elm); \
776 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
779 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
780 (stor) = htons((st)->elm); \
781 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
785 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
787 PF_MD5_UPD(pfr, addr.type);
788 switch (pfr->addr.type) {
789 case PF_ADDR_DYNIFTL:
790 PF_MD5_UPD(pfr, addr.v.ifname);
791 PF_MD5_UPD(pfr, addr.iflags);
794 PF_MD5_UPD(pfr, addr.v.tblname);
796 case PF_ADDR_ADDRMASK:
798 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
799 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
803 PF_MD5_UPD(pfr, port[0]);
804 PF_MD5_UPD(pfr, port[1]);
805 PF_MD5_UPD(pfr, neg);
806 PF_MD5_UPD(pfr, port_op);
810 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
815 pf_hash_rule_addr(ctx, &rule->src);
816 pf_hash_rule_addr(ctx, &rule->dst);
817 PF_MD5_UPD_STR(rule, label);
818 PF_MD5_UPD_STR(rule, ifname);
819 PF_MD5_UPD_STR(rule, match_tagname);
820 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
821 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
822 PF_MD5_UPD_HTONL(rule, prob, y);
823 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
824 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
825 PF_MD5_UPD(rule, uid.op);
826 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
827 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
828 PF_MD5_UPD(rule, gid.op);
829 PF_MD5_UPD_HTONL(rule, rule_flag, y);
830 PF_MD5_UPD(rule, action);
831 PF_MD5_UPD(rule, direction);
832 PF_MD5_UPD(rule, af);
833 PF_MD5_UPD(rule, quick);
834 PF_MD5_UPD(rule, ifnot);
835 PF_MD5_UPD(rule, match_tag_not);
836 PF_MD5_UPD(rule, natpass);
837 PF_MD5_UPD(rule, keep_state);
838 PF_MD5_UPD(rule, proto);
839 PF_MD5_UPD(rule, type);
840 PF_MD5_UPD(rule, code);
841 PF_MD5_UPD(rule, flags);
842 PF_MD5_UPD(rule, flagset);
843 PF_MD5_UPD(rule, allow_opts);
844 PF_MD5_UPD(rule, rt);
845 PF_MD5_UPD(rule, tos);
849 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
851 struct pf_ruleset *rs;
852 struct pf_rule *rule, **old_array;
853 struct pf_rulequeue *old_rules;
855 u_int32_t old_rcount;
859 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
861 rs = pf_find_ruleset(anchor);
862 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
863 ticket != rs->rules[rs_num].inactive.ticket)
866 /* Calculate checksum for the main ruleset */
867 if (rs == &pf_main_ruleset) {
868 error = pf_setup_pfsync_matching(rs);
873 /* Swap rules, keep the old. */
874 old_rules = rs->rules[rs_num].active.ptr;
875 old_rcount = rs->rules[rs_num].active.rcount;
876 old_array = rs->rules[rs_num].active.ptr_array;
878 rs->rules[rs_num].active.ptr =
879 rs->rules[rs_num].inactive.ptr;
880 rs->rules[rs_num].active.ptr_array =
881 rs->rules[rs_num].inactive.ptr_array;
882 rs->rules[rs_num].active.rcount =
883 rs->rules[rs_num].inactive.rcount;
884 rs->rules[rs_num].inactive.ptr = old_rules;
885 rs->rules[rs_num].inactive.ptr_array = old_array;
886 rs->rules[rs_num].inactive.rcount = old_rcount;
888 rs->rules[rs_num].active.ticket =
889 rs->rules[rs_num].inactive.ticket;
890 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
893 /* Purge the old rule list. */
894 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
895 pf_unlink_rule(old_rules, rule);
896 if (rs->rules[rs_num].inactive.ptr_array)
897 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
898 rs->rules[rs_num].inactive.ptr_array = NULL;
899 rs->rules[rs_num].inactive.rcount = 0;
900 rs->rules[rs_num].inactive.open = 0;
901 pf_remove_if_empty_ruleset(rs);
907 pf_setup_pfsync_matching(struct pf_ruleset *rs)
910 struct pf_rule *rule;
912 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
915 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
916 /* XXX PF_RULESET_SCRUB as well? */
917 if (rs_cnt == PF_RULESET_SCRUB)
920 if (rs->rules[rs_cnt].inactive.ptr_array)
921 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
922 rs->rules[rs_cnt].inactive.ptr_array = NULL;
924 if (rs->rules[rs_cnt].inactive.rcount) {
925 rs->rules[rs_cnt].inactive.ptr_array =
926 malloc(sizeof(caddr_t) *
927 rs->rules[rs_cnt].inactive.rcount,
930 if (!rs->rules[rs_cnt].inactive.ptr_array)
934 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
936 pf_hash_rule(&ctx, rule);
937 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
941 MD5Final(digest, &ctx);
942 memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum));
947 pf_addr_setup(struct pf_ruleset *ruleset, struct pf_addr_wrap *addr,
952 switch (addr->type) {
954 addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname);
955 if (addr->p.tbl == NULL)
958 case PF_ADDR_DYNIFTL:
959 error = pfi_dynaddr_setup(addr, af);
967 pf_addr_copyout(struct pf_addr_wrap *addr)
970 switch (addr->type) {
971 case PF_ADDR_DYNIFTL:
972 pfi_dynaddr_copyout(addr);
975 pf_tbladdr_copyout(addr);
981 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
985 /* XXX keep in sync with switch() below */
986 if (securelevel_gt(td->td_ucred, 2))
993 case DIOCSETSTATUSIF:
1000 case DIOCCLRRULECTRS:
1005 case DIOCGETRULESETS:
1006 case DIOCGETRULESET:
1007 case DIOCRGETTABLES:
1008 case DIOCRGETTSTATS:
1009 case DIOCRCLRTSTATS:
1015 case DIOCRGETASTATS:
1016 case DIOCRCLRASTATS:
1019 case DIOCGETSRCNODES:
1020 case DIOCCLRSRCNODES:
1021 case DIOCIGETIFACES:
1026 case DIOCRCLRTABLES:
1027 case DIOCRADDTABLES:
1028 case DIOCRDELTABLES:
1029 case DIOCRSETTFLAGS:
1030 if (((struct pfioc_table *)addr)->pfrio_flags &
1032 break; /* dummy operation ok */
1038 if (!(flags & FWRITE))
1046 case DIOCGETTIMEOUT:
1051 case DIOCGETRULESETS:
1052 case DIOCGETRULESET:
1054 case DIOCRGETTABLES:
1055 case DIOCRGETTSTATS:
1057 case DIOCRGETASTATS:
1060 case DIOCGETSRCNODES:
1061 case DIOCIGETIFACES:
1064 case DIOCRCLRTABLES:
1065 case DIOCRADDTABLES:
1066 case DIOCRDELTABLES:
1067 case DIOCRCLRTSTATS:
1072 case DIOCRSETTFLAGS:
1073 if (((struct pfioc_table *)addr)->pfrio_flags &
1075 flags |= FWRITE; /* need write lock for dummy */
1076 break; /* dummy operation ok */
1080 if (((struct pfioc_rule *)addr)->action ==
1088 CURVNET_SET(TD_TO_VNET(td));
1093 if (V_pf_status.running)
1101 DPFPRINTF(PF_DEBUG_MISC,
1102 ("pf: pfil registration failed\n"));
1106 V_pf_status.running = 1;
1107 V_pf_status.since = time_second;
1110 V_pf_stateid[cpu] = time_second;
1112 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1119 if (!V_pf_status.running)
1122 V_pf_status.running = 0;
1124 error = dehook_pf();
1126 V_pf_status.running = 1;
1127 DPFPRINTF(PF_DEBUG_MISC,
1128 ("pf: pfil unregistration failed\n"));
1131 V_pf_status.since = time_second;
1132 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1138 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1139 struct pf_ruleset *ruleset;
1140 struct pf_rule *rule, *tail;
1141 struct pf_pooladdr *pa;
1142 struct pfi_kif *kif = NULL;
1145 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1150 if (pr->rule.af == AF_INET) {
1151 error = EAFNOSUPPORT;
1156 if (pr->rule.af == AF_INET6) {
1157 error = EAFNOSUPPORT;
1162 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK);
1163 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1164 if (rule->ifname[0])
1165 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1166 rule->states_cur = counter_u64_alloc(M_WAITOK);
1167 rule->states_tot = counter_u64_alloc(M_WAITOK);
1168 rule->src_nodes = counter_u64_alloc(M_WAITOK);
1169 rule->cuid = td->td_ucred->cr_ruid;
1170 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1171 TAILQ_INIT(&rule->rpool.list);
1173 #define ERROUT(x) { error = (x); goto DIOCADDRULE_error; }
1176 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1177 ruleset = pf_find_ruleset(pr->anchor);
1178 if (ruleset == NULL)
1180 rs_num = pf_get_ruleset_number(pr->rule.action);
1181 if (rs_num >= PF_RULESET_MAX)
1183 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1184 DPFPRINTF(PF_DEBUG_MISC,
1185 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1186 ruleset->rules[rs_num].inactive.ticket));
1189 if (pr->pool_ticket != V_ticket_pabuf) {
1190 DPFPRINTF(PF_DEBUG_MISC,
1191 ("pool_ticket: %d != %d\n", pr->pool_ticket,
1196 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1199 rule->nr = tail->nr + 1;
1202 if (rule->ifname[0]) {
1203 rule->kif = pfi_kif_attach(kif, rule->ifname);
1204 pfi_kif_ref(rule->kif);
1208 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs)
1213 if (rule->qname[0] != 0) {
1214 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1216 else if (rule->pqname[0] != 0) {
1218 pf_qname2qid(rule->pqname)) == 0)
1221 rule->pqid = rule->qid;
1224 if (rule->tagname[0])
1225 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1227 if (rule->match_tagname[0])
1228 if ((rule->match_tag =
1229 pf_tagname2tag(rule->match_tagname)) == 0)
1231 if (rule->rt && !rule->direction)
1235 if (rule->logif >= PFLOGIFS_MAX)
1237 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
1239 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
1241 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1243 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1244 if (pa->addr.type == PF_ADDR_TABLE) {
1245 pa->addr.p.tbl = pfr_attach_table(ruleset,
1246 pa->addr.v.tblname);
1247 if (pa->addr.p.tbl == NULL)
1251 if (rule->overload_tblname[0]) {
1252 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1253 rule->overload_tblname)) == NULL)
1256 rule->overload_tbl->pfrkt_flags |=
1260 pf_mv_pool(&V_pf_pabuf, &rule->rpool.list);
1261 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1262 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1263 (rule->rt > PF_FASTROUTE)) &&
1264 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1273 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1274 rule->evaluations = rule->packets[0] = rule->packets[1] =
1275 rule->bytes[0] = rule->bytes[1] = 0;
1276 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1278 ruleset->rules[rs_num].inactive.rcount++;
1285 counter_u64_free(rule->states_cur);
1286 counter_u64_free(rule->states_tot);
1287 counter_u64_free(rule->src_nodes);
1288 free(rule, M_PFRULE);
1290 free(kif, PFI_MTYPE);
1294 case DIOCGETRULES: {
1295 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1296 struct pf_ruleset *ruleset;
1297 struct pf_rule *tail;
1301 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1302 ruleset = pf_find_ruleset(pr->anchor);
1303 if (ruleset == NULL) {
1308 rs_num = pf_get_ruleset_number(pr->rule.action);
1309 if (rs_num >= PF_RULESET_MAX) {
1314 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1317 pr->nr = tail->nr + 1;
1320 pr->ticket = ruleset->rules[rs_num].active.ticket;
1326 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1327 struct pf_ruleset *ruleset;
1328 struct pf_rule *rule;
1332 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1333 ruleset = pf_find_ruleset(pr->anchor);
1334 if (ruleset == NULL) {
1339 rs_num = pf_get_ruleset_number(pr->rule.action);
1340 if (rs_num >= PF_RULESET_MAX) {
1345 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1350 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1351 while ((rule != NULL) && (rule->nr != pr->nr))
1352 rule = TAILQ_NEXT(rule, entries);
1358 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1359 pr->rule.u_states_cur = counter_u64_fetch(rule->states_cur);
1360 pr->rule.u_states_tot = counter_u64_fetch(rule->states_tot);
1361 pr->rule.u_src_nodes = counter_u64_fetch(rule->src_nodes);
1362 if (pf_anchor_copyout(ruleset, rule, pr)) {
1367 pf_addr_copyout(&pr->rule.src.addr);
1368 pf_addr_copyout(&pr->rule.dst.addr);
1369 for (i = 0; i < PF_SKIP_COUNT; ++i)
1370 if (rule->skip[i].ptr == NULL)
1371 pr->rule.skip[i].nr = -1;
1373 pr->rule.skip[i].nr =
1374 rule->skip[i].ptr->nr;
1376 if (pr->action == PF_GET_CLR_CNTR) {
1377 rule->evaluations = 0;
1378 rule->packets[0] = rule->packets[1] = 0;
1379 rule->bytes[0] = rule->bytes[1] = 0;
1380 counter_u64_zero(rule->states_tot);
1386 case DIOCCHANGERULE: {
1387 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1388 struct pf_ruleset *ruleset;
1389 struct pf_rule *oldrule = NULL, *newrule = NULL;
1390 struct pfi_kif *kif = NULL;
1391 struct pf_pooladdr *pa;
1395 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1396 pcr->action > PF_CHANGE_GET_TICKET) {
1400 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1405 if (pcr->action != PF_CHANGE_REMOVE) {
1407 if (pcr->rule.af == AF_INET) {
1408 error = EAFNOSUPPORT;
1413 if (pcr->rule.af == AF_INET6) {
1414 error = EAFNOSUPPORT;
1418 newrule = malloc(sizeof(*newrule), M_PFRULE, M_WAITOK);
1419 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1420 if (newrule->ifname[0])
1421 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1422 newrule->states_cur = counter_u64_alloc(M_WAITOK);
1423 newrule->states_tot = counter_u64_alloc(M_WAITOK);
1424 newrule->src_nodes = counter_u64_alloc(M_WAITOK);
1425 newrule->cuid = td->td_ucred->cr_ruid;
1426 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1427 TAILQ_INIT(&newrule->rpool.list);
1430 #define ERROUT(x) { error = (x); goto DIOCCHANGERULE_error; }
1433 if (!(pcr->action == PF_CHANGE_REMOVE ||
1434 pcr->action == PF_CHANGE_GET_TICKET) &&
1435 pcr->pool_ticket != V_ticket_pabuf)
1438 ruleset = pf_find_ruleset(pcr->anchor);
1439 if (ruleset == NULL)
1442 rs_num = pf_get_ruleset_number(pcr->rule.action);
1443 if (rs_num >= PF_RULESET_MAX)
1446 if (pcr->action == PF_CHANGE_GET_TICKET) {
1447 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1449 } else if (pcr->ticket !=
1450 ruleset->rules[rs_num].active.ticket)
1453 if (pcr->action != PF_CHANGE_REMOVE) {
1454 if (newrule->ifname[0]) {
1455 newrule->kif = pfi_kif_attach(kif,
1457 pfi_kif_ref(newrule->kif);
1459 newrule->kif = NULL;
1461 if (newrule->rtableid > 0 &&
1462 newrule->rtableid >= rt_numfibs)
1467 if (newrule->qname[0] != 0) {
1469 pf_qname2qid(newrule->qname)) == 0)
1471 else if (newrule->pqname[0] != 0) {
1472 if ((newrule->pqid =
1473 pf_qname2qid(newrule->pqname)) == 0)
1476 newrule->pqid = newrule->qid;
1479 if (newrule->tagname[0])
1481 pf_tagname2tag(newrule->tagname)) == 0)
1483 if (newrule->match_tagname[0])
1484 if ((newrule->match_tag = pf_tagname2tag(
1485 newrule->match_tagname)) == 0)
1487 if (newrule->rt && !newrule->direction)
1491 if (newrule->logif >= PFLOGIFS_MAX)
1493 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
1495 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
1497 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1499 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1500 if (pa->addr.type == PF_ADDR_TABLE) {
1502 pfr_attach_table(ruleset,
1503 pa->addr.v.tblname);
1504 if (pa->addr.p.tbl == NULL)
1508 if (newrule->overload_tblname[0]) {
1509 if ((newrule->overload_tbl = pfr_attach_table(
1510 ruleset, newrule->overload_tblname)) ==
1514 newrule->overload_tbl->pfrkt_flags |=
1518 pf_mv_pool(&V_pf_pabuf, &newrule->rpool.list);
1519 if (((((newrule->action == PF_NAT) ||
1520 (newrule->action == PF_RDR) ||
1521 (newrule->action == PF_BINAT) ||
1522 (newrule->rt > PF_FASTROUTE)) &&
1523 !newrule->anchor)) &&
1524 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1528 pf_free_rule(newrule);
1533 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1534 newrule->evaluations = 0;
1535 newrule->packets[0] = newrule->packets[1] = 0;
1536 newrule->bytes[0] = newrule->bytes[1] = 0;
1538 pf_empty_pool(&V_pf_pabuf);
1540 if (pcr->action == PF_CHANGE_ADD_HEAD)
1541 oldrule = TAILQ_FIRST(
1542 ruleset->rules[rs_num].active.ptr);
1543 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1544 oldrule = TAILQ_LAST(
1545 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1547 oldrule = TAILQ_FIRST(
1548 ruleset->rules[rs_num].active.ptr);
1549 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1550 oldrule = TAILQ_NEXT(oldrule, entries);
1551 if (oldrule == NULL) {
1552 if (newrule != NULL)
1553 pf_free_rule(newrule);
1560 if (pcr->action == PF_CHANGE_REMOVE) {
1561 pf_unlink_rule(ruleset->rules[rs_num].active.ptr,
1563 ruleset->rules[rs_num].active.rcount--;
1565 if (oldrule == NULL)
1567 ruleset->rules[rs_num].active.ptr,
1569 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1570 pcr->action == PF_CHANGE_ADD_BEFORE)
1571 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1574 ruleset->rules[rs_num].active.ptr,
1575 oldrule, newrule, entries);
1576 ruleset->rules[rs_num].active.rcount++;
1580 TAILQ_FOREACH(oldrule,
1581 ruleset->rules[rs_num].active.ptr, entries)
1584 ruleset->rules[rs_num].active.ticket++;
1586 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1587 pf_remove_if_empty_ruleset(ruleset);
1593 DIOCCHANGERULE_error:
1595 if (newrule != NULL) {
1596 counter_u64_free(newrule->states_cur);
1597 counter_u64_free(newrule->states_tot);
1598 counter_u64_free(newrule->src_nodes);
1599 free(newrule, M_PFRULE);
1602 free(kif, PFI_MTYPE);
1606 case DIOCCLRSTATES: {
1608 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1609 u_int i, killed = 0;
1611 for (i = 0; i <= V_pf_hashmask; i++) {
1612 struct pf_idhash *ih = &V_pf_idhash[i];
1614 relock_DIOCCLRSTATES:
1615 PF_HASHROW_LOCK(ih);
1616 LIST_FOREACH(s, &ih->states, entry)
1617 if (!psk->psk_ifname[0] ||
1618 !strcmp(psk->psk_ifname,
1619 s->kif->pfik_name)) {
1621 * Don't send out individual
1624 s->state_flags |= PFSTATE_NOSYNC;
1625 pf_unlink_state(s, PF_ENTER_LOCKED);
1627 goto relock_DIOCCLRSTATES;
1629 PF_HASHROW_UNLOCK(ih);
1631 psk->psk_killed = killed;
1632 if (pfsync_clear_states_ptr != NULL)
1633 pfsync_clear_states_ptr(V_pf_status.hostid, psk->psk_ifname);
1637 case DIOCKILLSTATES: {
1639 struct pf_state_key *sk;
1640 struct pf_addr *srcaddr, *dstaddr;
1641 u_int16_t srcport, dstport;
1642 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1643 u_int i, killed = 0;
1645 if (psk->psk_pfcmp.id) {
1646 if (psk->psk_pfcmp.creatorid == 0)
1647 psk->psk_pfcmp.creatorid = V_pf_status.hostid;
1648 if ((s = pf_find_state_byid(psk->psk_pfcmp.id,
1649 psk->psk_pfcmp.creatorid))) {
1650 pf_unlink_state(s, PF_ENTER_LOCKED);
1651 psk->psk_killed = 1;
1656 for (i = 0; i <= V_pf_hashmask; i++) {
1657 struct pf_idhash *ih = &V_pf_idhash[i];
1659 relock_DIOCKILLSTATES:
1660 PF_HASHROW_LOCK(ih);
1661 LIST_FOREACH(s, &ih->states, entry) {
1662 sk = s->key[PF_SK_WIRE];
1663 if (s->direction == PF_OUT) {
1664 srcaddr = &sk->addr[1];
1665 dstaddr = &sk->addr[0];
1666 srcport = sk->port[0];
1667 dstport = sk->port[0];
1669 srcaddr = &sk->addr[0];
1670 dstaddr = &sk->addr[1];
1671 srcport = sk->port[0];
1672 dstport = sk->port[0];
1675 if ((!psk->psk_af || sk->af == psk->psk_af)
1676 && (!psk->psk_proto || psk->psk_proto ==
1678 PF_MATCHA(psk->psk_src.neg,
1679 &psk->psk_src.addr.v.a.addr,
1680 &psk->psk_src.addr.v.a.mask,
1682 PF_MATCHA(psk->psk_dst.neg,
1683 &psk->psk_dst.addr.v.a.addr,
1684 &psk->psk_dst.addr.v.a.mask,
1686 (psk->psk_src.port_op == 0 ||
1687 pf_match_port(psk->psk_src.port_op,
1688 psk->psk_src.port[0], psk->psk_src.port[1],
1690 (psk->psk_dst.port_op == 0 ||
1691 pf_match_port(psk->psk_dst.port_op,
1692 psk->psk_dst.port[0], psk->psk_dst.port[1],
1694 (!psk->psk_label[0] ||
1695 (s->rule.ptr->label[0] &&
1696 !strcmp(psk->psk_label,
1697 s->rule.ptr->label))) &&
1698 (!psk->psk_ifname[0] ||
1699 !strcmp(psk->psk_ifname,
1700 s->kif->pfik_name))) {
1701 pf_unlink_state(s, PF_ENTER_LOCKED);
1703 goto relock_DIOCKILLSTATES;
1706 PF_HASHROW_UNLOCK(ih);
1708 psk->psk_killed = killed;
1712 case DIOCADDSTATE: {
1713 struct pfioc_state *ps = (struct pfioc_state *)addr;
1714 struct pfsync_state *sp = &ps->state;
1716 if (sp->timeout >= PFTM_MAX) {
1720 if (pfsync_state_import_ptr != NULL) {
1722 error = pfsync_state_import_ptr(sp, PFSYNC_SI_IOCTL);
1729 case DIOCGETSTATE: {
1730 struct pfioc_state *ps = (struct pfioc_state *)addr;
1733 s = pf_find_state_byid(ps->state.id, ps->state.creatorid);
1739 pfsync_state_export(&ps->state, s);
1744 case DIOCGETSTATES: {
1745 struct pfioc_states *ps = (struct pfioc_states *)addr;
1747 struct pfsync_state *pstore, *p;
1750 if (ps->ps_len == 0) {
1751 nr = uma_zone_get_cur(V_pf_state_z);
1752 ps->ps_len = sizeof(struct pfsync_state) * nr;
1756 p = pstore = malloc(ps->ps_len, M_TEMP, M_WAITOK);
1759 for (i = 0; i <= V_pf_hashmask; i++) {
1760 struct pf_idhash *ih = &V_pf_idhash[i];
1762 PF_HASHROW_LOCK(ih);
1763 LIST_FOREACH(s, &ih->states, entry) {
1765 if (s->timeout == PFTM_UNLINKED)
1768 if ((nr+1) * sizeof(*p) > ps->ps_len) {
1769 PF_HASHROW_UNLOCK(ih);
1770 goto DIOCGETSTATES_full;
1772 pfsync_state_export(p, s);
1776 PF_HASHROW_UNLOCK(ih);
1779 error = copyout(pstore, ps->ps_states,
1780 sizeof(struct pfsync_state) * nr);
1782 free(pstore, M_TEMP);
1785 ps->ps_len = sizeof(struct pfsync_state) * nr;
1786 free(pstore, M_TEMP);
1791 case DIOCGETSTATUS: {
1792 struct pf_status *s = (struct pf_status *)addr;
1795 s->running = V_pf_status.running;
1796 s->since = V_pf_status.since;
1797 s->debug = V_pf_status.debug;
1798 s->hostid = V_pf_status.hostid;
1799 s->states = V_pf_status.states;
1800 s->src_nodes = V_pf_status.src_nodes;
1802 for (int i = 0; i < PFRES_MAX; i++)
1804 counter_u64_fetch(V_pf_status.counters[i]);
1805 for (int i = 0; i < LCNT_MAX; i++)
1807 counter_u64_fetch(V_pf_status.lcounters[i]);
1808 for (int i = 0; i < FCNT_MAX; i++)
1810 counter_u64_fetch(V_pf_status.fcounters[i]);
1811 for (int i = 0; i < SCNT_MAX; i++)
1813 counter_u64_fetch(V_pf_status.scounters[i]);
1815 bcopy(V_pf_status.ifname, s->ifname, IFNAMSIZ);
1816 bcopy(V_pf_status.pf_chksum, s->pf_chksum,
1817 PF_MD5_DIGEST_LENGTH);
1819 pfi_update_status(s->ifname, s);
1824 case DIOCSETSTATUSIF: {
1825 struct pfioc_if *pi = (struct pfioc_if *)addr;
1827 if (pi->ifname[0] == 0) {
1828 bzero(V_pf_status.ifname, IFNAMSIZ);
1832 strlcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ);
1837 case DIOCCLRSTATUS: {
1839 for (int i = 0; i < PFRES_MAX; i++)
1840 counter_u64_zero(V_pf_status.counters[i]);
1841 for (int i = 0; i < FCNT_MAX; i++)
1842 counter_u64_zero(V_pf_status.fcounters[i]);
1843 for (int i = 0; i < SCNT_MAX; i++)
1844 counter_u64_zero(V_pf_status.scounters[i]);
1845 V_pf_status.since = time_second;
1846 if (*V_pf_status.ifname)
1847 pfi_update_status(V_pf_status.ifname, NULL);
1853 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1854 struct pf_state_key *sk;
1855 struct pf_state *state;
1856 struct pf_state_key_cmp key;
1857 int m = 0, direction = pnl->direction;
1860 /* NATLOOK src and dst are reversed, so reverse sidx/didx */
1861 sidx = (direction == PF_IN) ? 1 : 0;
1862 didx = (direction == PF_IN) ? 0 : 1;
1865 PF_AZERO(&pnl->saddr, pnl->af) ||
1866 PF_AZERO(&pnl->daddr, pnl->af) ||
1867 ((pnl->proto == IPPROTO_TCP ||
1868 pnl->proto == IPPROTO_UDP) &&
1869 (!pnl->dport || !pnl->sport)))
1872 bzero(&key, sizeof(key));
1874 key.proto = pnl->proto;
1875 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
1876 key.port[sidx] = pnl->sport;
1877 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
1878 key.port[didx] = pnl->dport;
1880 state = pf_find_state_all(&key, direction, &m);
1883 error = E2BIG; /* more than one state */
1884 else if (state != NULL) {
1885 /* XXXGL: not locked read */
1886 sk = state->key[sidx];
1887 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
1888 pnl->rsport = sk->port[sidx];
1889 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
1890 pnl->rdport = sk->port[didx];
1897 case DIOCSETTIMEOUT: {
1898 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1901 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1907 old = V_pf_default_rule.timeout[pt->timeout];
1908 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
1910 V_pf_default_rule.timeout[pt->timeout] = pt->seconds;
1911 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
1912 wakeup(pf_purge_thread);
1918 case DIOCGETTIMEOUT: {
1919 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1921 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1926 pt->seconds = V_pf_default_rule.timeout[pt->timeout];
1931 case DIOCGETLIMIT: {
1932 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1934 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1939 pl->limit = V_pf_limits[pl->index].limit;
1944 case DIOCSETLIMIT: {
1945 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1949 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1950 V_pf_limits[pl->index].zone == NULL) {
1955 uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit);
1956 old_limit = V_pf_limits[pl->index].limit;
1957 V_pf_limits[pl->index].limit = pl->limit;
1958 pl->limit = old_limit;
1963 case DIOCSETDEBUG: {
1964 u_int32_t *level = (u_int32_t *)addr;
1967 V_pf_status.debug = *level;
1972 case DIOCCLRRULECTRS: {
1973 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
1974 struct pf_ruleset *ruleset = &pf_main_ruleset;
1975 struct pf_rule *rule;
1979 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
1980 rule->evaluations = 0;
1981 rule->packets[0] = rule->packets[1] = 0;
1982 rule->bytes[0] = rule->bytes[1] = 0;
1988 case DIOCGIFSPEED: {
1989 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
1990 struct pf_ifspeed ps;
1993 if (psp->ifname[0] != 0) {
1994 /* Can we completely trust user-land? */
1995 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
1996 ifp = ifunit(ps.ifname);
1998 psp->baudrate = ifp->if_baudrate;
2007 case DIOCSTARTALTQ: {
2008 struct pf_altq *altq;
2011 /* enable all altq interfaces on active list */
2012 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
2013 if (altq->qname[0] == 0 && (altq->local_flags &
2014 PFALTQ_FLAG_IF_REMOVED) == 0) {
2015 error = pf_enable_altq(altq);
2021 V_pf_altq_running = 1;
2023 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
2027 case DIOCSTOPALTQ: {
2028 struct pf_altq *altq;
2031 /* disable all altq interfaces on active list */
2032 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
2033 if (altq->qname[0] == 0 && (altq->local_flags &
2034 PFALTQ_FLAG_IF_REMOVED) == 0) {
2035 error = pf_disable_altq(altq);
2041 V_pf_altq_running = 0;
2043 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
2048 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2049 struct pf_altq *altq, *a;
2052 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK);
2053 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
2054 altq->local_flags = 0;
2057 if (pa->ticket != V_ticket_altqs_inactive) {
2059 free(altq, M_PFALTQ);
2065 * if this is for a queue, find the discipline and
2066 * copy the necessary fields
2068 if (altq->qname[0] != 0) {
2069 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2072 free(altq, M_PFALTQ);
2075 altq->altq_disc = NULL;
2076 TAILQ_FOREACH(a, V_pf_altqs_inactive, entries) {
2077 if (strncmp(a->ifname, altq->ifname,
2078 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2079 altq->altq_disc = a->altq_disc;
2085 if ((ifp = ifunit(altq->ifname)) == NULL)
2086 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2088 error = altq_add(altq);
2092 free(altq, M_PFALTQ);
2096 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries);
2097 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2102 case DIOCGETALTQS: {
2103 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2104 struct pf_altq *altq;
2108 TAILQ_FOREACH(altq, V_pf_altqs_active, entries)
2110 pa->ticket = V_ticket_altqs_active;
2116 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2117 struct pf_altq *altq;
2121 if (pa->ticket != V_ticket_altqs_active) {
2127 altq = TAILQ_FIRST(V_pf_altqs_active);
2128 while ((altq != NULL) && (nr < pa->nr)) {
2129 altq = TAILQ_NEXT(altq, entries);
2137 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2142 case DIOCCHANGEALTQ:
2143 /* CHANGEALTQ not supported yet! */
2147 case DIOCGETQSTATS: {
2148 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2149 struct pf_altq *altq;
2154 if (pq->ticket != V_ticket_altqs_active) {
2159 nbytes = pq->nbytes;
2161 altq = TAILQ_FIRST(V_pf_altqs_active);
2162 while ((altq != NULL) && (nr < pq->nr)) {
2163 altq = TAILQ_NEXT(altq, entries);
2172 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2178 error = altq_getqstats(altq, pq->buf, &nbytes);
2180 pq->scheduler = altq->scheduler;
2181 pq->nbytes = nbytes;
2187 case DIOCBEGINADDRS: {
2188 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2191 pf_empty_pool(&V_pf_pabuf);
2192 pp->ticket = ++V_ticket_pabuf;
2198 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2199 struct pf_pooladdr *pa;
2200 struct pfi_kif *kif = NULL;
2203 if (pp->af == AF_INET) {
2204 error = EAFNOSUPPORT;
2209 if (pp->af == AF_INET6) {
2210 error = EAFNOSUPPORT;
2214 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2215 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2216 pp->addr.addr.type != PF_ADDR_TABLE) {
2220 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK);
2221 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2223 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2225 if (pp->ticket != V_ticket_pabuf) {
2228 free(kif, PFI_MTYPE);
2233 if (pa->ifname[0]) {
2234 pa->kif = pfi_kif_attach(kif, pa->ifname);
2235 pfi_kif_ref(pa->kif);
2238 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error =
2239 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) {
2241 pfi_kif_unref(pa->kif);
2246 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries);
2251 case DIOCGETADDRS: {
2252 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2253 struct pf_pool *pool;
2254 struct pf_pooladdr *pa;
2258 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2259 pp->r_num, 0, 1, 0);
2265 TAILQ_FOREACH(pa, &pool->list, entries)
2272 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2273 struct pf_pool *pool;
2274 struct pf_pooladdr *pa;
2278 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2279 pp->r_num, 0, 1, 1);
2285 pa = TAILQ_FIRST(&pool->list);
2286 while ((pa != NULL) && (nr < pp->nr)) {
2287 pa = TAILQ_NEXT(pa, entries);
2295 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2296 pf_addr_copyout(&pp->addr.addr);
2301 case DIOCCHANGEADDR: {
2302 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2303 struct pf_pool *pool;
2304 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2305 struct pf_ruleset *ruleset;
2306 struct pfi_kif *kif = NULL;
2308 if (pca->action < PF_CHANGE_ADD_HEAD ||
2309 pca->action > PF_CHANGE_REMOVE) {
2313 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2314 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2315 pca->addr.addr.type != PF_ADDR_TABLE) {
2320 if (pca->action != PF_CHANGE_REMOVE) {
2322 if (pca->af == AF_INET) {
2323 error = EAFNOSUPPORT;
2328 if (pca->af == AF_INET6) {
2329 error = EAFNOSUPPORT;
2333 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK);
2334 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2335 if (newpa->ifname[0])
2336 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2340 #define ERROUT(x) { error = (x); goto DIOCCHANGEADDR_error; }
2342 ruleset = pf_find_ruleset(pca->anchor);
2343 if (ruleset == NULL)
2346 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2347 pca->r_num, pca->r_last, 1, 1);
2351 if (pca->action != PF_CHANGE_REMOVE) {
2352 if (newpa->ifname[0]) {
2353 newpa->kif = pfi_kif_attach(kif, newpa->ifname);
2354 pfi_kif_ref(newpa->kif);
2358 switch (newpa->addr.type) {
2359 case PF_ADDR_DYNIFTL:
2360 error = pfi_dynaddr_setup(&newpa->addr,
2364 newpa->addr.p.tbl = pfr_attach_table(ruleset,
2365 newpa->addr.v.tblname);
2366 if (newpa->addr.p.tbl == NULL)
2371 goto DIOCCHANGEADDR_error;
2374 switch (pca->action) {
2375 case PF_CHANGE_ADD_HEAD:
2376 oldpa = TAILQ_FIRST(&pool->list);
2378 case PF_CHANGE_ADD_TAIL:
2379 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2382 oldpa = TAILQ_FIRST(&pool->list);
2383 for (int i = 0; oldpa && i < pca->nr; i++)
2384 oldpa = TAILQ_NEXT(oldpa, entries);
2390 if (pca->action == PF_CHANGE_REMOVE) {
2391 TAILQ_REMOVE(&pool->list, oldpa, entries);
2392 switch (oldpa->addr.type) {
2393 case PF_ADDR_DYNIFTL:
2394 pfi_dynaddr_remove(oldpa->addr.p.dyn);
2397 pfr_detach_table(oldpa->addr.p.tbl);
2401 pfi_kif_unref(oldpa->kif);
2402 free(oldpa, M_PFRULE);
2405 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2406 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2407 pca->action == PF_CHANGE_ADD_BEFORE)
2408 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2410 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2414 pool->cur = TAILQ_FIRST(&pool->list);
2415 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af);
2420 DIOCCHANGEADDR_error:
2422 pfi_kif_unref(newpa->kif);
2425 free(newpa, M_PFRULE);
2427 free(kif, PFI_MTYPE);
2431 case DIOCGETRULESETS: {
2432 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2433 struct pf_ruleset *ruleset;
2434 struct pf_anchor *anchor;
2437 pr->path[sizeof(pr->path) - 1] = 0;
2438 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2444 if (ruleset->anchor == NULL) {
2445 /* XXX kludge for pf_main_ruleset */
2446 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2447 if (anchor->parent == NULL)
2450 RB_FOREACH(anchor, pf_anchor_node,
2451 &ruleset->anchor->children)
2458 case DIOCGETRULESET: {
2459 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2460 struct pf_ruleset *ruleset;
2461 struct pf_anchor *anchor;
2465 pr->path[sizeof(pr->path) - 1] = 0;
2466 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2472 if (ruleset->anchor == NULL) {
2473 /* XXX kludge for pf_main_ruleset */
2474 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2475 if (anchor->parent == NULL && nr++ == pr->nr) {
2476 strlcpy(pr->name, anchor->name,
2481 RB_FOREACH(anchor, pf_anchor_node,
2482 &ruleset->anchor->children)
2483 if (nr++ == pr->nr) {
2484 strlcpy(pr->name, anchor->name,
2495 case DIOCRCLRTABLES: {
2496 struct pfioc_table *io = (struct pfioc_table *)addr;
2498 if (io->pfrio_esize != 0) {
2503 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2504 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2509 case DIOCRADDTABLES: {
2510 struct pfioc_table *io = (struct pfioc_table *)addr;
2511 struct pfr_table *pfrts;
2514 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2518 totlen = io->pfrio_size * sizeof(struct pfr_table);
2519 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2520 error = copyin(io->pfrio_buffer, pfrts, totlen);
2522 free(pfrts, M_TEMP);
2526 error = pfr_add_tables(pfrts, io->pfrio_size,
2527 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2529 free(pfrts, M_TEMP);
2533 case DIOCRDELTABLES: {
2534 struct pfioc_table *io = (struct pfioc_table *)addr;
2535 struct pfr_table *pfrts;
2538 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2542 totlen = io->pfrio_size * sizeof(struct pfr_table);
2543 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2544 error = copyin(io->pfrio_buffer, pfrts, totlen);
2546 free(pfrts, M_TEMP);
2550 error = pfr_del_tables(pfrts, io->pfrio_size,
2551 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2553 free(pfrts, M_TEMP);
2557 case DIOCRGETTABLES: {
2558 struct pfioc_table *io = (struct pfioc_table *)addr;
2559 struct pfr_table *pfrts;
2562 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2566 totlen = io->pfrio_size * sizeof(struct pfr_table);
2567 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2569 error = pfr_get_tables(&io->pfrio_table, pfrts,
2570 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2573 error = copyout(pfrts, io->pfrio_buffer, totlen);
2574 free(pfrts, M_TEMP);
2578 case DIOCRGETTSTATS: {
2579 struct pfioc_table *io = (struct pfioc_table *)addr;
2580 struct pfr_tstats *pfrtstats;
2583 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2587 totlen = io->pfrio_size * sizeof(struct pfr_tstats);
2588 pfrtstats = malloc(totlen, M_TEMP, M_WAITOK);
2590 error = pfr_get_tstats(&io->pfrio_table, pfrtstats,
2591 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2594 error = copyout(pfrtstats, io->pfrio_buffer, totlen);
2595 free(pfrtstats, M_TEMP);
2599 case DIOCRCLRTSTATS: {
2600 struct pfioc_table *io = (struct pfioc_table *)addr;
2601 struct pfr_table *pfrts;
2604 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2608 totlen = io->pfrio_size * sizeof(struct pfr_table);
2609 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2610 error = copyin(io->pfrio_buffer, pfrts, totlen);
2612 free(pfrts, M_TEMP);
2616 error = pfr_clr_tstats(pfrts, io->pfrio_size,
2617 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2619 free(pfrts, M_TEMP);
2623 case DIOCRSETTFLAGS: {
2624 struct pfioc_table *io = (struct pfioc_table *)addr;
2625 struct pfr_table *pfrts;
2628 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2632 totlen = io->pfrio_size * sizeof(struct pfr_table);
2633 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2634 error = copyin(io->pfrio_buffer, pfrts, totlen);
2636 free(pfrts, M_TEMP);
2640 error = pfr_set_tflags(pfrts, io->pfrio_size,
2641 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2642 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2644 free(pfrts, M_TEMP);
2648 case DIOCRCLRADDRS: {
2649 struct pfioc_table *io = (struct pfioc_table *)addr;
2651 if (io->pfrio_esize != 0) {
2656 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2657 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2662 case DIOCRADDADDRS: {
2663 struct pfioc_table *io = (struct pfioc_table *)addr;
2664 struct pfr_addr *pfras;
2667 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2671 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2672 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2673 error = copyin(io->pfrio_buffer, pfras, totlen);
2675 free(pfras, M_TEMP);
2679 error = pfr_add_addrs(&io->pfrio_table, pfras,
2680 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2681 PFR_FLAG_USERIOCTL);
2683 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2684 error = copyout(pfras, io->pfrio_buffer, totlen);
2685 free(pfras, M_TEMP);
2689 case DIOCRDELADDRS: {
2690 struct pfioc_table *io = (struct pfioc_table *)addr;
2691 struct pfr_addr *pfras;
2694 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2698 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2699 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2700 error = copyin(io->pfrio_buffer, pfras, totlen);
2702 free(pfras, M_TEMP);
2706 error = pfr_del_addrs(&io->pfrio_table, pfras,
2707 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2708 PFR_FLAG_USERIOCTL);
2710 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2711 error = copyout(pfras, io->pfrio_buffer, totlen);
2712 free(pfras, M_TEMP);
2716 case DIOCRSETADDRS: {
2717 struct pfioc_table *io = (struct pfioc_table *)addr;
2718 struct pfr_addr *pfras;
2721 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2725 totlen = (io->pfrio_size + io->pfrio_size2) *
2726 sizeof(struct pfr_addr);
2727 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2728 error = copyin(io->pfrio_buffer, pfras, totlen);
2730 free(pfras, M_TEMP);
2734 error = pfr_set_addrs(&io->pfrio_table, pfras,
2735 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2736 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2737 PFR_FLAG_USERIOCTL, 0);
2739 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2740 error = copyout(pfras, io->pfrio_buffer, totlen);
2741 free(pfras, M_TEMP);
2745 case DIOCRGETADDRS: {
2746 struct pfioc_table *io = (struct pfioc_table *)addr;
2747 struct pfr_addr *pfras;
2750 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2754 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2755 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2757 error = pfr_get_addrs(&io->pfrio_table, pfras,
2758 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2761 error = copyout(pfras, io->pfrio_buffer, totlen);
2762 free(pfras, M_TEMP);
2766 case DIOCRGETASTATS: {
2767 struct pfioc_table *io = (struct pfioc_table *)addr;
2768 struct pfr_astats *pfrastats;
2771 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2775 totlen = io->pfrio_size * sizeof(struct pfr_astats);
2776 pfrastats = malloc(totlen, M_TEMP, M_WAITOK);
2778 error = pfr_get_astats(&io->pfrio_table, pfrastats,
2779 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2782 error = copyout(pfrastats, io->pfrio_buffer, totlen);
2783 free(pfrastats, M_TEMP);
2787 case DIOCRCLRASTATS: {
2788 struct pfioc_table *io = (struct pfioc_table *)addr;
2789 struct pfr_addr *pfras;
2792 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2796 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2797 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2798 error = copyin(io->pfrio_buffer, pfras, totlen);
2800 free(pfras, M_TEMP);
2804 error = pfr_clr_astats(&io->pfrio_table, pfras,
2805 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2806 PFR_FLAG_USERIOCTL);
2808 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2809 error = copyout(pfras, io->pfrio_buffer, totlen);
2810 free(pfras, M_TEMP);
2814 case DIOCRTSTADDRS: {
2815 struct pfioc_table *io = (struct pfioc_table *)addr;
2816 struct pfr_addr *pfras;
2819 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2823 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2824 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2825 error = copyin(io->pfrio_buffer, pfras, totlen);
2827 free(pfras, M_TEMP);
2831 error = pfr_tst_addrs(&io->pfrio_table, pfras,
2832 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2833 PFR_FLAG_USERIOCTL);
2836 error = copyout(pfras, io->pfrio_buffer, totlen);
2837 free(pfras, M_TEMP);
2841 case DIOCRINADEFINE: {
2842 struct pfioc_table *io = (struct pfioc_table *)addr;
2843 struct pfr_addr *pfras;
2846 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2850 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2851 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2852 error = copyin(io->pfrio_buffer, pfras, totlen);
2854 free(pfras, M_TEMP);
2858 error = pfr_ina_define(&io->pfrio_table, pfras,
2859 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2860 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2862 free(pfras, M_TEMP);
2867 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2869 error = pf_osfp_add(io);
2875 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2877 error = pf_osfp_get(io);
2883 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2884 struct pfioc_trans_e *ioes, *ioe;
2888 if (io->esize != sizeof(*ioe)) {
2892 totlen = sizeof(struct pfioc_trans_e) * io->size;
2893 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2894 error = copyin(io->array, ioes, totlen);
2900 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2901 switch (ioe->rs_num) {
2903 case PF_RULESET_ALTQ:
2904 if (ioe->anchor[0]) {
2910 if ((error = pf_begin_altq(&ioe->ticket))) {
2917 case PF_RULESET_TABLE:
2919 struct pfr_table table;
2921 bzero(&table, sizeof(table));
2922 strlcpy(table.pfrt_anchor, ioe->anchor,
2923 sizeof(table.pfrt_anchor));
2924 if ((error = pfr_ina_begin(&table,
2925 &ioe->ticket, NULL, 0))) {
2933 if ((error = pf_begin_rules(&ioe->ticket,
2934 ioe->rs_num, ioe->anchor))) {
2943 error = copyout(ioes, io->array, totlen);
2948 case DIOCXROLLBACK: {
2949 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2950 struct pfioc_trans_e *ioe, *ioes;
2954 if (io->esize != sizeof(*ioe)) {
2958 totlen = sizeof(struct pfioc_trans_e) * io->size;
2959 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2960 error = copyin(io->array, ioes, totlen);
2966 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2967 switch (ioe->rs_num) {
2969 case PF_RULESET_ALTQ:
2970 if (ioe->anchor[0]) {
2976 if ((error = pf_rollback_altq(ioe->ticket))) {
2979 goto fail; /* really bad */
2983 case PF_RULESET_TABLE:
2985 struct pfr_table table;
2987 bzero(&table, sizeof(table));
2988 strlcpy(table.pfrt_anchor, ioe->anchor,
2989 sizeof(table.pfrt_anchor));
2990 if ((error = pfr_ina_rollback(&table,
2991 ioe->ticket, NULL, 0))) {
2994 goto fail; /* really bad */
2999 if ((error = pf_rollback_rules(ioe->ticket,
3000 ioe->rs_num, ioe->anchor))) {
3003 goto fail; /* really bad */
3014 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3015 struct pfioc_trans_e *ioe, *ioes;
3016 struct pf_ruleset *rs;
3020 if (io->esize != sizeof(*ioe)) {
3024 totlen = sizeof(struct pfioc_trans_e) * io->size;
3025 ioes = malloc(totlen, M_TEMP, M_WAITOK);
3026 error = copyin(io->array, ioes, totlen);
3032 /* First makes sure everything will succeed. */
3033 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3034 switch (ioe->rs_num) {
3036 case PF_RULESET_ALTQ:
3037 if (ioe->anchor[0]) {
3043 if (!V_altqs_inactive_open || ioe->ticket !=
3044 V_ticket_altqs_inactive) {
3052 case PF_RULESET_TABLE:
3053 rs = pf_find_ruleset(ioe->anchor);
3054 if (rs == NULL || !rs->topen || ioe->ticket !=
3063 if (ioe->rs_num < 0 || ioe->rs_num >=
3070 rs = pf_find_ruleset(ioe->anchor);
3072 !rs->rules[ioe->rs_num].inactive.open ||
3073 rs->rules[ioe->rs_num].inactive.ticket !=
3083 /* Now do the commit - no errors should happen here. */
3084 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3085 switch (ioe->rs_num) {
3087 case PF_RULESET_ALTQ:
3088 if ((error = pf_commit_altq(ioe->ticket))) {
3091 goto fail; /* really bad */
3095 case PF_RULESET_TABLE:
3097 struct pfr_table table;
3099 bzero(&table, sizeof(table));
3100 strlcpy(table.pfrt_anchor, ioe->anchor,
3101 sizeof(table.pfrt_anchor));
3102 if ((error = pfr_ina_commit(&table,
3103 ioe->ticket, NULL, NULL, 0))) {
3106 goto fail; /* really bad */
3111 if ((error = pf_commit_rules(ioe->ticket,
3112 ioe->rs_num, ioe->anchor))) {
3115 goto fail; /* really bad */
3125 case DIOCGETSRCNODES: {
3126 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3127 struct pf_srchash *sh;
3128 struct pf_src_node *n, *p, *pstore;
3131 if (psn->psn_len == 0) {
3132 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3134 PF_HASHROW_LOCK(sh);
3135 LIST_FOREACH(n, &sh->nodes, entry)
3137 PF_HASHROW_UNLOCK(sh);
3139 psn->psn_len = sizeof(struct pf_src_node) * nr;
3143 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK);
3144 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3146 PF_HASHROW_LOCK(sh);
3147 LIST_FOREACH(n, &sh->nodes, entry) {
3148 int secs = time_uptime, diff;
3150 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3153 bcopy(n, p, sizeof(struct pf_src_node));
3154 if (n->rule.ptr != NULL)
3155 p->rule.nr = n->rule.ptr->nr;
3156 p->creation = secs - p->creation;
3157 if (p->expire > secs)
3162 /* Adjust the connection rate estimate. */
3163 diff = secs - n->conn_rate.last;
3164 if (diff >= n->conn_rate.seconds)
3165 p->conn_rate.count = 0;
3167 p->conn_rate.count -=
3168 n->conn_rate.count * diff /
3169 n->conn_rate.seconds;
3173 PF_HASHROW_UNLOCK(sh);
3175 error = copyout(pstore, psn->psn_src_nodes,
3176 sizeof(struct pf_src_node) * nr);
3178 free(pstore, M_TEMP);
3181 psn->psn_len = sizeof(struct pf_src_node) * nr;
3182 free(pstore, M_TEMP);
3186 case DIOCCLRSRCNODES: {
3188 pf_clear_srcnodes(NULL);
3189 pf_purge_expired_src_nodes();
3193 case DIOCKILLSRCNODES:
3194 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr);
3197 case DIOCSETHOSTID: {
3198 u_int32_t *hostid = (u_int32_t *)addr;
3202 V_pf_status.hostid = arc4random();
3204 V_pf_status.hostid = *hostid;
3215 case DIOCIGETIFACES: {
3216 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3217 struct pfi_kif *ifstore;
3220 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3225 bufsiz = io->pfiio_size * sizeof(struct pfi_kif);
3226 ifstore = malloc(bufsiz, M_TEMP, M_WAITOK);
3228 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size);
3230 error = copyout(ifstore, io->pfiio_buffer, bufsiz);
3231 free(ifstore, M_TEMP);
3235 case DIOCSETIFFLAG: {
3236 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3239 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3244 case DIOCCLRIFFLAG: {
3245 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3248 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3264 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
3266 bzero(sp, sizeof(struct pfsync_state));
3268 /* copy from state key */
3269 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
3270 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
3271 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
3272 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
3273 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
3274 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
3275 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
3276 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
3277 sp->proto = st->key[PF_SK_WIRE]->proto;
3278 sp->af = st->key[PF_SK_WIRE]->af;
3280 /* copy from state */
3281 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
3282 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
3283 sp->creation = htonl(time_uptime - st->creation);
3284 sp->expire = pf_state_expires(st);
3285 if (sp->expire <= time_uptime)
3286 sp->expire = htonl(0);
3288 sp->expire = htonl(sp->expire - time_uptime);
3290 sp->direction = st->direction;
3292 sp->timeout = st->timeout;
3293 sp->state_flags = st->state_flags;
3295 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
3296 if (st->nat_src_node)
3297 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
3300 sp->creatorid = st->creatorid;
3301 pf_state_peer_hton(&st->src, &sp->src);
3302 pf_state_peer_hton(&st->dst, &sp->dst);
3304 if (st->rule.ptr == NULL)
3305 sp->rule = htonl(-1);
3307 sp->rule = htonl(st->rule.ptr->nr);
3308 if (st->anchor.ptr == NULL)
3309 sp->anchor = htonl(-1);
3311 sp->anchor = htonl(st->anchor.ptr->nr);
3312 if (st->nat_rule.ptr == NULL)
3313 sp->nat_rule = htonl(-1);
3315 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
3317 pf_state_counter_hton(st->packets[0], sp->packets[0]);
3318 pf_state_counter_hton(st->packets[1], sp->packets[1]);
3319 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
3320 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
3325 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
3327 struct pfr_ktable *kt;
3329 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type));
3332 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
3333 kt = kt->pfrkt_root;
3335 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
3340 * XXX - Check for version missmatch!!!
3343 pf_clear_states(void)
3348 for (i = 0; i <= V_pf_hashmask; i++) {
3349 struct pf_idhash *ih = &V_pf_idhash[i];
3351 PF_HASHROW_LOCK(ih);
3352 LIST_FOREACH(s, &ih->states, entry) {
3353 s->timeout = PFTM_PURGE;
3354 /* Don't send out individual delete messages. */
3355 s->sync_state = PFSTATE_NOSYNC;
3356 pf_unlink_state(s, PF_ENTER_LOCKED);
3359 PF_HASHROW_UNLOCK(ih);
3364 pf_clear_tables(void)
3366 struct pfioc_table io;
3369 bzero(&io, sizeof(io));
3371 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3378 pf_clear_srcnodes(struct pf_src_node *n)
3383 for (i = 0; i <= V_pf_hashmask; i++) {
3384 struct pf_idhash *ih = &V_pf_idhash[i];
3386 PF_HASHROW_LOCK(ih);
3387 LIST_FOREACH(s, &ih->states, entry) {
3388 if (n == NULL || n == s->src_node)
3390 if (n == NULL || n == s->nat_src_node)
3391 s->nat_src_node = NULL;
3393 PF_HASHROW_UNLOCK(ih);
3397 struct pf_srchash *sh;
3399 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3401 PF_HASHROW_LOCK(sh);
3402 LIST_FOREACH(n, &sh->nodes, entry) {
3406 PF_HASHROW_UNLOCK(sh);
3409 /* XXX: hash slot should already be locked here. */
3416 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk)
3418 struct pf_src_node_list kill;
3421 for (int i = 0; i <= V_pf_srchashmask; i++) {
3422 struct pf_srchash *sh = &V_pf_srchash[i];
3423 struct pf_src_node *sn, *tmp;
3425 PF_HASHROW_LOCK(sh);
3426 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp)
3427 if (PF_MATCHA(psnk->psnk_src.neg,
3428 &psnk->psnk_src.addr.v.a.addr,
3429 &psnk->psnk_src.addr.v.a.mask,
3430 &sn->addr, sn->af) &&
3431 PF_MATCHA(psnk->psnk_dst.neg,
3432 &psnk->psnk_dst.addr.v.a.addr,
3433 &psnk->psnk_dst.addr.v.a.mask,
3434 &sn->raddr, sn->af)) {
3435 pf_unlink_src_node_locked(sn);
3436 LIST_INSERT_HEAD(&kill, sn, entry);
3439 PF_HASHROW_UNLOCK(sh);
3442 for (int i = 0; i <= V_pf_hashmask; i++) {
3443 struct pf_idhash *ih = &V_pf_idhash[i];
3446 PF_HASHROW_LOCK(ih);
3447 LIST_FOREACH(s, &ih->states, entry) {
3448 if (s->src_node && s->src_node->expire == 1) {
3450 s->src_node->states--;
3454 if (s->nat_src_node && s->nat_src_node->expire == 1) {
3456 s->nat_src_node->states--;
3458 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 thier 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");
3718 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3719 if ((error = pfattach()) != 0)
3731 V_pf_status.running = 0;
3733 swi_remove(V_pf_swi_cookie);
3734 error = dehook_pf();
3737 * Should not happen!
3738 * XXX Due to error code ESRCH, kldunload will show
3739 * a message like 'No such process'.
3741 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3746 V_pf_end_threads = 1;
3747 while (V_pf_end_threads < 2) {
3748 wakeup_one(pf_purge_thread);
3749 rw_sleep(pf_purge_thread, &pf_rules_lock, 0, "pftmo", 0);
3751 pf_normalize_cleanup();
3756 if (IS_DEFAULT_VNET(curvnet))
3759 destroy_dev(pf_dev);
3760 rw_destroy(&pf_rules_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_PSEUDO, SI_ORDER_FIRST);
3798 MODULE_VERSION(pf, PF_MODVER);