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;
212 V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
213 V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
215 RB_INIT(&V_pf_anchors);
216 pf_init_ruleset(&pf_main_ruleset);
218 /* default rule should never be garbage collected */
219 V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next;
220 #ifdef PF_DEFAULT_TO_DROP
221 V_pf_default_rule.action = PF_DROP;
223 V_pf_default_rule.action = PF_PASS;
225 V_pf_default_rule.nr = -1;
226 V_pf_default_rule.rtableid = -1;
228 V_pf_default_rule.states_cur = counter_u64_alloc(M_WAITOK);
229 V_pf_default_rule.states_tot = counter_u64_alloc(M_WAITOK);
230 V_pf_default_rule.src_nodes = counter_u64_alloc(M_WAITOK);
232 /* initialize default timeouts */
233 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
234 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
235 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
236 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
237 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
238 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
239 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
240 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
241 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
242 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
243 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
244 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
245 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
246 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
247 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
248 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
249 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
250 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
251 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
252 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
254 bzero(&V_pf_status, sizeof(V_pf_status));
255 V_pf_status.debug = PF_DEBUG_URGENT;
257 V_pf_pfil_hooked = 0;
259 /* XXX do our best to avoid a conflict */
260 V_pf_status.hostid = arc4random();
262 if ((error = kproc_create(pf_purge_thread, curvnet, NULL, 0, 0,
264 /* XXXGL: leaked all above. */
266 if ((error = swi_add(NULL, "pf send", pf_intr, curvnet, SWI_NET,
267 INTR_MPSAFE, &V_pf_swi_cookie)) != 0)
268 /* XXXGL: leaked all above. */
274 static struct pf_pool *
275 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
276 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
277 u_int8_t check_ticket)
279 struct pf_ruleset *ruleset;
280 struct pf_rule *rule;
283 ruleset = pf_find_ruleset(anchor);
286 rs_num = pf_get_ruleset_number(rule_action);
287 if (rs_num >= PF_RULESET_MAX)
290 if (check_ticket && ticket !=
291 ruleset->rules[rs_num].active.ticket)
294 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
297 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
299 if (check_ticket && ticket !=
300 ruleset->rules[rs_num].inactive.ticket)
303 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
306 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
309 while ((rule != NULL) && (rule->nr != rule_number))
310 rule = TAILQ_NEXT(rule, entries);
315 return (&rule->rpool);
319 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
321 struct pf_pooladdr *mv_pool_pa;
323 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
324 TAILQ_REMOVE(poola, mv_pool_pa, entries);
325 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
330 pf_empty_pool(struct pf_palist *poola)
332 struct pf_pooladdr *pa;
334 while ((pa = TAILQ_FIRST(poola)) != NULL) {
335 switch (pa->addr.type) {
336 case PF_ADDR_DYNIFTL:
337 pfi_dynaddr_remove(pa->addr.p.dyn);
340 pfr_detach_table(pa->addr.p.tbl);
344 pfi_kif_unref(pa->kif);
345 TAILQ_REMOVE(poola, pa, entries);
351 pf_unlink_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
356 TAILQ_REMOVE(rulequeue, rule, entries);
358 PF_UNLNKDRULES_LOCK();
359 rule->rule_flag |= PFRULE_REFS;
360 TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries);
361 PF_UNLNKDRULES_UNLOCK();
365 pf_free_rule(struct pf_rule *rule)
371 tag_unref(&V_pf_tags, rule->tag);
373 tag_unref(&V_pf_tags, rule->match_tag);
375 if (rule->pqid != rule->qid)
376 pf_qid_unref(rule->pqid);
377 pf_qid_unref(rule->qid);
379 switch (rule->src.addr.type) {
380 case PF_ADDR_DYNIFTL:
381 pfi_dynaddr_remove(rule->src.addr.p.dyn);
384 pfr_detach_table(rule->src.addr.p.tbl);
387 switch (rule->dst.addr.type) {
388 case PF_ADDR_DYNIFTL:
389 pfi_dynaddr_remove(rule->dst.addr.p.dyn);
392 pfr_detach_table(rule->dst.addr.p.tbl);
395 if (rule->overload_tbl)
396 pfr_detach_table(rule->overload_tbl);
398 pfi_kif_unref(rule->kif);
399 pf_anchor_remove(rule);
400 pf_empty_pool(&rule->rpool.list);
401 counter_u64_free(rule->states_cur);
402 counter_u64_free(rule->states_tot);
403 counter_u64_free(rule->src_nodes);
404 free(rule, M_PFRULE);
408 tagname2tag(struct pf_tags *head, char *tagname)
410 struct pf_tagname *tag, *p = NULL;
411 u_int16_t new_tagid = 1;
415 TAILQ_FOREACH(tag, head, entries)
416 if (strcmp(tagname, tag->name) == 0) {
422 * to avoid fragmentation, we do a linear search from the beginning
423 * and take the first free slot we find. if there is none or the list
424 * is empty, append a new entry at the end.
428 if (!TAILQ_EMPTY(head))
429 for (p = TAILQ_FIRST(head); p != NULL &&
430 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
431 new_tagid = p->tag + 1;
433 if (new_tagid > TAGID_MAX)
436 /* allocate and fill new struct pf_tagname */
437 tag = malloc(sizeof(*tag), M_PFTAG, M_NOWAIT|M_ZERO);
440 strlcpy(tag->name, tagname, sizeof(tag->name));
441 tag->tag = new_tagid;
444 if (p != NULL) /* insert new entry before p */
445 TAILQ_INSERT_BEFORE(p, tag, entries);
446 else /* either list empty or no free slot in between */
447 TAILQ_INSERT_TAIL(head, tag, entries);
453 tag_unref(struct pf_tags *head, u_int16_t tag)
455 struct pf_tagname *p, *next;
459 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
460 next = TAILQ_NEXT(p, entries);
463 TAILQ_REMOVE(head, p, entries);
472 pf_tagname2tag(char *tagname)
474 return (tagname2tag(&V_pf_tags, tagname));
479 pf_qname2qid(char *qname)
481 return ((u_int32_t)tagname2tag(&V_pf_qids, qname));
485 pf_qid_unref(u_int32_t qid)
487 tag_unref(&V_pf_qids, (u_int16_t)qid);
491 pf_begin_altq(u_int32_t *ticket)
493 struct pf_altq *altq;
498 /* Purge the old altq list */
499 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
500 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
501 if (altq->qname[0] == 0 &&
502 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
503 /* detach and destroy the discipline */
504 error = altq_remove(altq);
506 pf_qid_unref(altq->qid);
507 free(altq, M_PFALTQ);
511 *ticket = ++V_ticket_altqs_inactive;
512 V_altqs_inactive_open = 1;
517 pf_rollback_altq(u_int32_t ticket)
519 struct pf_altq *altq;
524 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
526 /* Purge the old altq list */
527 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
528 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
529 if (altq->qname[0] == 0 &&
530 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
531 /* detach and destroy the discipline */
532 error = altq_remove(altq);
534 pf_qid_unref(altq->qid);
535 free(altq, M_PFALTQ);
537 V_altqs_inactive_open = 0;
542 pf_commit_altq(u_int32_t ticket)
544 struct pf_altqqueue *old_altqs;
545 struct pf_altq *altq;
550 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
553 /* swap altqs, keep the old. */
554 old_altqs = V_pf_altqs_active;
555 V_pf_altqs_active = V_pf_altqs_inactive;
556 V_pf_altqs_inactive = old_altqs;
557 V_ticket_altqs_active = V_ticket_altqs_inactive;
559 /* Attach new disciplines */
560 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
561 if (altq->qname[0] == 0 &&
562 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
563 /* attach the discipline */
564 error = altq_pfattach(altq);
565 if (error == 0 && V_pf_altq_running)
566 error = pf_enable_altq(altq);
572 /* Purge the old altq list */
573 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
574 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
575 if (altq->qname[0] == 0 &&
576 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
577 /* detach and destroy the discipline */
578 if (V_pf_altq_running)
579 error = pf_disable_altq(altq);
580 err = altq_pfdetach(altq);
581 if (err != 0 && error == 0)
583 err = altq_remove(altq);
584 if (err != 0 && error == 0)
587 pf_qid_unref(altq->qid);
588 free(altq, M_PFALTQ);
591 V_altqs_inactive_open = 0;
596 pf_enable_altq(struct pf_altq *altq)
599 struct tb_profile tb;
602 if ((ifp = ifunit(altq->ifname)) == NULL)
605 if (ifp->if_snd.altq_type != ALTQT_NONE)
606 error = altq_enable(&ifp->if_snd);
608 /* set tokenbucket regulator */
609 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
610 tb.rate = altq->ifbandwidth;
611 tb.depth = altq->tbrsize;
612 error = tbr_set(&ifp->if_snd, &tb);
619 pf_disable_altq(struct pf_altq *altq)
622 struct tb_profile tb;
625 if ((ifp = ifunit(altq->ifname)) == NULL)
629 * when the discipline is no longer referenced, it was overridden
630 * by a new one. if so, just return.
632 if (altq->altq_disc != ifp->if_snd.altq_disc)
635 error = altq_disable(&ifp->if_snd);
638 /* clear tokenbucket regulator */
640 error = tbr_set(&ifp->if_snd, &tb);
647 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
650 struct pf_altq *a1, *a2, *a3;
654 /* Interrupt userland queue modifications */
655 if (V_altqs_inactive_open)
656 pf_rollback_altq(V_ticket_altqs_inactive);
658 /* Start new altq ruleset */
659 if (pf_begin_altq(&ticket))
662 /* Copy the current active set */
663 TAILQ_FOREACH(a1, V_pf_altqs_active, entries) {
664 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
669 bcopy(a1, a2, sizeof(struct pf_altq));
671 if (a2->qname[0] != 0) {
672 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
677 a2->altq_disc = NULL;
678 TAILQ_FOREACH(a3, V_pf_altqs_inactive, entries) {
679 if (strncmp(a3->ifname, a2->ifname,
680 IFNAMSIZ) == 0 && a3->qname[0] == 0) {
681 a2->altq_disc = a3->altq_disc;
686 /* Deactivate the interface in question */
687 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
688 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
689 (remove && ifp1 == ifp)) {
690 a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
692 error = altq_add(a2);
694 if (ticket != V_ticket_altqs_inactive)
703 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries);
707 pf_rollback_altq(ticket);
709 pf_commit_altq(ticket);
714 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
716 struct pf_ruleset *rs;
717 struct pf_rule *rule;
721 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
723 rs = pf_find_or_create_ruleset(anchor);
726 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
727 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
728 rs->rules[rs_num].inactive.rcount--;
730 *ticket = ++rs->rules[rs_num].inactive.ticket;
731 rs->rules[rs_num].inactive.open = 1;
736 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
738 struct pf_ruleset *rs;
739 struct pf_rule *rule;
743 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
745 rs = pf_find_ruleset(anchor);
746 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
747 rs->rules[rs_num].inactive.ticket != ticket)
749 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
750 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
751 rs->rules[rs_num].inactive.rcount--;
753 rs->rules[rs_num].inactive.open = 0;
757 #define PF_MD5_UPD(st, elm) \
758 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
760 #define PF_MD5_UPD_STR(st, elm) \
761 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
763 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
764 (stor) = htonl((st)->elm); \
765 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
768 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
769 (stor) = htons((st)->elm); \
770 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
774 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
776 PF_MD5_UPD(pfr, addr.type);
777 switch (pfr->addr.type) {
778 case PF_ADDR_DYNIFTL:
779 PF_MD5_UPD(pfr, addr.v.ifname);
780 PF_MD5_UPD(pfr, addr.iflags);
783 PF_MD5_UPD(pfr, addr.v.tblname);
785 case PF_ADDR_ADDRMASK:
787 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
788 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
792 PF_MD5_UPD(pfr, port[0]);
793 PF_MD5_UPD(pfr, port[1]);
794 PF_MD5_UPD(pfr, neg);
795 PF_MD5_UPD(pfr, port_op);
799 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
804 pf_hash_rule_addr(ctx, &rule->src);
805 pf_hash_rule_addr(ctx, &rule->dst);
806 PF_MD5_UPD_STR(rule, label);
807 PF_MD5_UPD_STR(rule, ifname);
808 PF_MD5_UPD_STR(rule, match_tagname);
809 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
810 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
811 PF_MD5_UPD_HTONL(rule, prob, y);
812 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
813 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
814 PF_MD5_UPD(rule, uid.op);
815 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
816 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
817 PF_MD5_UPD(rule, gid.op);
818 PF_MD5_UPD_HTONL(rule, rule_flag, y);
819 PF_MD5_UPD(rule, action);
820 PF_MD5_UPD(rule, direction);
821 PF_MD5_UPD(rule, af);
822 PF_MD5_UPD(rule, quick);
823 PF_MD5_UPD(rule, ifnot);
824 PF_MD5_UPD(rule, match_tag_not);
825 PF_MD5_UPD(rule, natpass);
826 PF_MD5_UPD(rule, keep_state);
827 PF_MD5_UPD(rule, proto);
828 PF_MD5_UPD(rule, type);
829 PF_MD5_UPD(rule, code);
830 PF_MD5_UPD(rule, flags);
831 PF_MD5_UPD(rule, flagset);
832 PF_MD5_UPD(rule, allow_opts);
833 PF_MD5_UPD(rule, rt);
834 PF_MD5_UPD(rule, tos);
838 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
840 struct pf_ruleset *rs;
841 struct pf_rule *rule, **old_array;
842 struct pf_rulequeue *old_rules;
844 u_int32_t old_rcount;
848 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
850 rs = pf_find_ruleset(anchor);
851 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
852 ticket != rs->rules[rs_num].inactive.ticket)
855 /* Calculate checksum for the main ruleset */
856 if (rs == &pf_main_ruleset) {
857 error = pf_setup_pfsync_matching(rs);
862 /* Swap rules, keep the old. */
863 old_rules = rs->rules[rs_num].active.ptr;
864 old_rcount = rs->rules[rs_num].active.rcount;
865 old_array = rs->rules[rs_num].active.ptr_array;
867 rs->rules[rs_num].active.ptr =
868 rs->rules[rs_num].inactive.ptr;
869 rs->rules[rs_num].active.ptr_array =
870 rs->rules[rs_num].inactive.ptr_array;
871 rs->rules[rs_num].active.rcount =
872 rs->rules[rs_num].inactive.rcount;
873 rs->rules[rs_num].inactive.ptr = old_rules;
874 rs->rules[rs_num].inactive.ptr_array = old_array;
875 rs->rules[rs_num].inactive.rcount = old_rcount;
877 rs->rules[rs_num].active.ticket =
878 rs->rules[rs_num].inactive.ticket;
879 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
882 /* Purge the old rule list. */
883 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
884 pf_unlink_rule(old_rules, rule);
885 if (rs->rules[rs_num].inactive.ptr_array)
886 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
887 rs->rules[rs_num].inactive.ptr_array = NULL;
888 rs->rules[rs_num].inactive.rcount = 0;
889 rs->rules[rs_num].inactive.open = 0;
890 pf_remove_if_empty_ruleset(rs);
896 pf_setup_pfsync_matching(struct pf_ruleset *rs)
899 struct pf_rule *rule;
901 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
904 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
905 /* XXX PF_RULESET_SCRUB as well? */
906 if (rs_cnt == PF_RULESET_SCRUB)
909 if (rs->rules[rs_cnt].inactive.ptr_array)
910 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
911 rs->rules[rs_cnt].inactive.ptr_array = NULL;
913 if (rs->rules[rs_cnt].inactive.rcount) {
914 rs->rules[rs_cnt].inactive.ptr_array =
915 malloc(sizeof(caddr_t) *
916 rs->rules[rs_cnt].inactive.rcount,
919 if (!rs->rules[rs_cnt].inactive.ptr_array)
923 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
925 pf_hash_rule(&ctx, rule);
926 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
930 MD5Final(digest, &ctx);
931 memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum));
936 pf_addr_setup(struct pf_ruleset *ruleset, struct pf_addr_wrap *addr,
941 switch (addr->type) {
943 addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname);
944 if (addr->p.tbl == NULL)
947 case PF_ADDR_DYNIFTL:
948 error = pfi_dynaddr_setup(addr, af);
956 pf_addr_copyout(struct pf_addr_wrap *addr)
959 switch (addr->type) {
960 case PF_ADDR_DYNIFTL:
961 pfi_dynaddr_copyout(addr);
964 pf_tbladdr_copyout(addr);
970 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
974 /* XXX keep in sync with switch() below */
975 if (securelevel_gt(td->td_ucred, 2))
982 case DIOCSETSTATUSIF:
989 case DIOCCLRRULECTRS:
994 case DIOCGETRULESETS:
1004 case DIOCRGETASTATS:
1005 case DIOCRCLRASTATS:
1008 case DIOCGETSRCNODES:
1009 case DIOCCLRSRCNODES:
1010 case DIOCIGETIFACES:
1015 case DIOCRCLRTABLES:
1016 case DIOCRADDTABLES:
1017 case DIOCRDELTABLES:
1018 case DIOCRSETTFLAGS:
1019 if (((struct pfioc_table *)addr)->pfrio_flags &
1021 break; /* dummy operation ok */
1027 if (!(flags & FWRITE))
1035 case DIOCGETTIMEOUT:
1040 case DIOCGETRULESETS:
1041 case DIOCGETRULESET:
1043 case DIOCRGETTABLES:
1044 case DIOCRGETTSTATS:
1046 case DIOCRGETASTATS:
1049 case DIOCGETSRCNODES:
1050 case DIOCIGETIFACES:
1053 case DIOCRCLRTABLES:
1054 case DIOCRADDTABLES:
1055 case DIOCRDELTABLES:
1056 case DIOCRCLRTSTATS:
1061 case DIOCRSETTFLAGS:
1062 if (((struct pfioc_table *)addr)->pfrio_flags &
1064 flags |= FWRITE; /* need write lock for dummy */
1065 break; /* dummy operation ok */
1069 if (((struct pfioc_rule *)addr)->action ==
1077 CURVNET_SET(TD_TO_VNET(td));
1082 if (V_pf_status.running)
1090 DPFPRINTF(PF_DEBUG_MISC,
1091 ("pf: pfil registration failed\n"));
1095 V_pf_status.running = 1;
1096 V_pf_status.since = time_second;
1099 V_pf_stateid[cpu] = time_second;
1101 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1108 if (!V_pf_status.running)
1111 V_pf_status.running = 0;
1113 error = dehook_pf();
1115 V_pf_status.running = 1;
1116 DPFPRINTF(PF_DEBUG_MISC,
1117 ("pf: pfil unregistration failed\n"));
1120 V_pf_status.since = time_second;
1121 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1127 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1128 struct pf_ruleset *ruleset;
1129 struct pf_rule *rule, *tail;
1130 struct pf_pooladdr *pa;
1131 struct pfi_kif *kif = NULL;
1134 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1139 if (pr->rule.af == AF_INET) {
1140 error = EAFNOSUPPORT;
1145 if (pr->rule.af == AF_INET6) {
1146 error = EAFNOSUPPORT;
1151 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK);
1152 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1153 if (rule->ifname[0])
1154 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1155 rule->states_cur = counter_u64_alloc(M_WAITOK);
1156 rule->states_tot = counter_u64_alloc(M_WAITOK);
1157 rule->src_nodes = counter_u64_alloc(M_WAITOK);
1158 rule->cuid = td->td_ucred->cr_ruid;
1159 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1160 TAILQ_INIT(&rule->rpool.list);
1162 #define ERROUT(x) { error = (x); goto DIOCADDRULE_error; }
1165 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1166 ruleset = pf_find_ruleset(pr->anchor);
1167 if (ruleset == NULL)
1169 rs_num = pf_get_ruleset_number(pr->rule.action);
1170 if (rs_num >= PF_RULESET_MAX)
1172 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1173 DPFPRINTF(PF_DEBUG_MISC,
1174 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1175 ruleset->rules[rs_num].inactive.ticket));
1178 if (pr->pool_ticket != V_ticket_pabuf) {
1179 DPFPRINTF(PF_DEBUG_MISC,
1180 ("pool_ticket: %d != %d\n", pr->pool_ticket,
1185 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1188 rule->nr = tail->nr + 1;
1191 if (rule->ifname[0]) {
1192 rule->kif = pfi_kif_attach(kif, rule->ifname);
1193 pfi_kif_ref(rule->kif);
1197 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs)
1202 if (rule->qname[0] != 0) {
1203 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1205 else if (rule->pqname[0] != 0) {
1207 pf_qname2qid(rule->pqname)) == 0)
1210 rule->pqid = rule->qid;
1213 if (rule->tagname[0])
1214 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1216 if (rule->match_tagname[0])
1217 if ((rule->match_tag =
1218 pf_tagname2tag(rule->match_tagname)) == 0)
1220 if (rule->rt && !rule->direction)
1224 if (rule->logif >= PFLOGIFS_MAX)
1226 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
1228 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
1230 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1232 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1233 if (pa->addr.type == PF_ADDR_TABLE) {
1234 pa->addr.p.tbl = pfr_attach_table(ruleset,
1235 pa->addr.v.tblname);
1236 if (pa->addr.p.tbl == NULL)
1240 if (rule->overload_tblname[0]) {
1241 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1242 rule->overload_tblname)) == NULL)
1245 rule->overload_tbl->pfrkt_flags |=
1249 pf_mv_pool(&V_pf_pabuf, &rule->rpool.list);
1250 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1251 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1252 (rule->rt > PF_FASTROUTE)) &&
1253 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1262 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1263 rule->evaluations = rule->packets[0] = rule->packets[1] =
1264 rule->bytes[0] = rule->bytes[1] = 0;
1265 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1267 ruleset->rules[rs_num].inactive.rcount++;
1274 counter_u64_free(rule->states_cur);
1275 counter_u64_free(rule->states_tot);
1276 counter_u64_free(rule->src_nodes);
1277 free(rule, M_PFRULE);
1279 free(kif, PFI_MTYPE);
1283 case DIOCGETRULES: {
1284 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1285 struct pf_ruleset *ruleset;
1286 struct pf_rule *tail;
1290 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1291 ruleset = pf_find_ruleset(pr->anchor);
1292 if (ruleset == NULL) {
1297 rs_num = pf_get_ruleset_number(pr->rule.action);
1298 if (rs_num >= PF_RULESET_MAX) {
1303 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1306 pr->nr = tail->nr + 1;
1309 pr->ticket = ruleset->rules[rs_num].active.ticket;
1315 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1316 struct pf_ruleset *ruleset;
1317 struct pf_rule *rule;
1321 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1322 ruleset = pf_find_ruleset(pr->anchor);
1323 if (ruleset == NULL) {
1328 rs_num = pf_get_ruleset_number(pr->rule.action);
1329 if (rs_num >= PF_RULESET_MAX) {
1334 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1339 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1340 while ((rule != NULL) && (rule->nr != pr->nr))
1341 rule = TAILQ_NEXT(rule, entries);
1347 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1348 pr->rule.u_states_cur = counter_u64_fetch(rule->states_cur);
1349 pr->rule.u_states_tot = counter_u64_fetch(rule->states_tot);
1350 pr->rule.u_src_nodes = counter_u64_fetch(rule->src_nodes);
1351 if (pf_anchor_copyout(ruleset, rule, pr)) {
1356 pf_addr_copyout(&pr->rule.src.addr);
1357 pf_addr_copyout(&pr->rule.dst.addr);
1358 for (i = 0; i < PF_SKIP_COUNT; ++i)
1359 if (rule->skip[i].ptr == NULL)
1360 pr->rule.skip[i].nr = -1;
1362 pr->rule.skip[i].nr =
1363 rule->skip[i].ptr->nr;
1365 if (pr->action == PF_GET_CLR_CNTR) {
1366 rule->evaluations = 0;
1367 rule->packets[0] = rule->packets[1] = 0;
1368 rule->bytes[0] = rule->bytes[1] = 0;
1369 counter_u64_zero(rule->states_tot);
1375 case DIOCCHANGERULE: {
1376 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1377 struct pf_ruleset *ruleset;
1378 struct pf_rule *oldrule = NULL, *newrule = NULL;
1379 struct pfi_kif *kif = NULL;
1380 struct pf_pooladdr *pa;
1384 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1385 pcr->action > PF_CHANGE_GET_TICKET) {
1389 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1394 if (pcr->action != PF_CHANGE_REMOVE) {
1396 if (pcr->rule.af == AF_INET) {
1397 error = EAFNOSUPPORT;
1402 if (pcr->rule.af == AF_INET6) {
1403 error = EAFNOSUPPORT;
1407 newrule = malloc(sizeof(*newrule), M_PFRULE, M_WAITOK);
1408 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1409 if (newrule->ifname[0])
1410 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1411 newrule->states_cur = counter_u64_alloc(M_WAITOK);
1412 newrule->states_tot = counter_u64_alloc(M_WAITOK);
1413 newrule->src_nodes = counter_u64_alloc(M_WAITOK);
1414 newrule->cuid = td->td_ucred->cr_ruid;
1415 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1416 TAILQ_INIT(&newrule->rpool.list);
1419 #define ERROUT(x) { error = (x); goto DIOCCHANGERULE_error; }
1422 if (!(pcr->action == PF_CHANGE_REMOVE ||
1423 pcr->action == PF_CHANGE_GET_TICKET) &&
1424 pcr->pool_ticket != V_ticket_pabuf)
1427 ruleset = pf_find_ruleset(pcr->anchor);
1428 if (ruleset == NULL)
1431 rs_num = pf_get_ruleset_number(pcr->rule.action);
1432 if (rs_num >= PF_RULESET_MAX)
1435 if (pcr->action == PF_CHANGE_GET_TICKET) {
1436 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1438 } else if (pcr->ticket !=
1439 ruleset->rules[rs_num].active.ticket)
1442 if (pcr->action != PF_CHANGE_REMOVE) {
1443 if (newrule->ifname[0]) {
1444 newrule->kif = pfi_kif_attach(kif,
1446 pfi_kif_ref(newrule->kif);
1448 newrule->kif = NULL;
1450 if (newrule->rtableid > 0 &&
1451 newrule->rtableid >= rt_numfibs)
1456 if (newrule->qname[0] != 0) {
1458 pf_qname2qid(newrule->qname)) == 0)
1460 else if (newrule->pqname[0] != 0) {
1461 if ((newrule->pqid =
1462 pf_qname2qid(newrule->pqname)) == 0)
1465 newrule->pqid = newrule->qid;
1468 if (newrule->tagname[0])
1470 pf_tagname2tag(newrule->tagname)) == 0)
1472 if (newrule->match_tagname[0])
1473 if ((newrule->match_tag = pf_tagname2tag(
1474 newrule->match_tagname)) == 0)
1476 if (newrule->rt && !newrule->direction)
1480 if (newrule->logif >= PFLOGIFS_MAX)
1482 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
1484 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
1486 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1488 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1489 if (pa->addr.type == PF_ADDR_TABLE) {
1491 pfr_attach_table(ruleset,
1492 pa->addr.v.tblname);
1493 if (pa->addr.p.tbl == NULL)
1497 if (newrule->overload_tblname[0]) {
1498 if ((newrule->overload_tbl = pfr_attach_table(
1499 ruleset, newrule->overload_tblname)) ==
1503 newrule->overload_tbl->pfrkt_flags |=
1507 pf_mv_pool(&V_pf_pabuf, &newrule->rpool.list);
1508 if (((((newrule->action == PF_NAT) ||
1509 (newrule->action == PF_RDR) ||
1510 (newrule->action == PF_BINAT) ||
1511 (newrule->rt > PF_FASTROUTE)) &&
1512 !newrule->anchor)) &&
1513 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1517 pf_free_rule(newrule);
1522 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1523 newrule->evaluations = 0;
1524 newrule->packets[0] = newrule->packets[1] = 0;
1525 newrule->bytes[0] = newrule->bytes[1] = 0;
1527 pf_empty_pool(&V_pf_pabuf);
1529 if (pcr->action == PF_CHANGE_ADD_HEAD)
1530 oldrule = TAILQ_FIRST(
1531 ruleset->rules[rs_num].active.ptr);
1532 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1533 oldrule = TAILQ_LAST(
1534 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1536 oldrule = TAILQ_FIRST(
1537 ruleset->rules[rs_num].active.ptr);
1538 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1539 oldrule = TAILQ_NEXT(oldrule, entries);
1540 if (oldrule == NULL) {
1541 if (newrule != NULL)
1542 pf_free_rule(newrule);
1549 if (pcr->action == PF_CHANGE_REMOVE) {
1550 pf_unlink_rule(ruleset->rules[rs_num].active.ptr,
1552 ruleset->rules[rs_num].active.rcount--;
1554 if (oldrule == NULL)
1556 ruleset->rules[rs_num].active.ptr,
1558 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1559 pcr->action == PF_CHANGE_ADD_BEFORE)
1560 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1563 ruleset->rules[rs_num].active.ptr,
1564 oldrule, newrule, entries);
1565 ruleset->rules[rs_num].active.rcount++;
1569 TAILQ_FOREACH(oldrule,
1570 ruleset->rules[rs_num].active.ptr, entries)
1573 ruleset->rules[rs_num].active.ticket++;
1575 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1576 pf_remove_if_empty_ruleset(ruleset);
1582 DIOCCHANGERULE_error:
1584 if (newrule != NULL) {
1585 counter_u64_free(newrule->states_cur);
1586 counter_u64_free(newrule->states_tot);
1587 counter_u64_free(newrule->src_nodes);
1588 free(newrule, M_PFRULE);
1591 free(kif, PFI_MTYPE);
1595 case DIOCCLRSTATES: {
1597 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1598 u_int i, killed = 0;
1600 for (i = 0; i <= V_pf_hashmask; i++) {
1601 struct pf_idhash *ih = &V_pf_idhash[i];
1603 relock_DIOCCLRSTATES:
1604 PF_HASHROW_LOCK(ih);
1605 LIST_FOREACH(s, &ih->states, entry)
1606 if (!psk->psk_ifname[0] ||
1607 !strcmp(psk->psk_ifname,
1608 s->kif->pfik_name)) {
1610 * Don't send out individual
1613 s->state_flags |= PFSTATE_NOSYNC;
1614 pf_unlink_state(s, PF_ENTER_LOCKED);
1616 goto relock_DIOCCLRSTATES;
1618 PF_HASHROW_UNLOCK(ih);
1620 psk->psk_killed = killed;
1621 if (pfsync_clear_states_ptr != NULL)
1622 pfsync_clear_states_ptr(V_pf_status.hostid, psk->psk_ifname);
1626 case DIOCKILLSTATES: {
1628 struct pf_state_key *sk;
1629 struct pf_addr *srcaddr, *dstaddr;
1630 u_int16_t srcport, dstport;
1631 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1632 u_int i, killed = 0;
1634 if (psk->psk_pfcmp.id) {
1635 if (psk->psk_pfcmp.creatorid == 0)
1636 psk->psk_pfcmp.creatorid = V_pf_status.hostid;
1637 if ((s = pf_find_state_byid(psk->psk_pfcmp.id,
1638 psk->psk_pfcmp.creatorid))) {
1639 pf_unlink_state(s, PF_ENTER_LOCKED);
1640 psk->psk_killed = 1;
1645 for (i = 0; i <= V_pf_hashmask; i++) {
1646 struct pf_idhash *ih = &V_pf_idhash[i];
1648 relock_DIOCKILLSTATES:
1649 PF_HASHROW_LOCK(ih);
1650 LIST_FOREACH(s, &ih->states, entry) {
1651 sk = s->key[PF_SK_WIRE];
1652 if (s->direction == PF_OUT) {
1653 srcaddr = &sk->addr[1];
1654 dstaddr = &sk->addr[0];
1655 srcport = sk->port[0];
1656 dstport = sk->port[0];
1658 srcaddr = &sk->addr[0];
1659 dstaddr = &sk->addr[1];
1660 srcport = sk->port[0];
1661 dstport = sk->port[0];
1664 if ((!psk->psk_af || sk->af == psk->psk_af)
1665 && (!psk->psk_proto || psk->psk_proto ==
1667 PF_MATCHA(psk->psk_src.neg,
1668 &psk->psk_src.addr.v.a.addr,
1669 &psk->psk_src.addr.v.a.mask,
1671 PF_MATCHA(psk->psk_dst.neg,
1672 &psk->psk_dst.addr.v.a.addr,
1673 &psk->psk_dst.addr.v.a.mask,
1675 (psk->psk_src.port_op == 0 ||
1676 pf_match_port(psk->psk_src.port_op,
1677 psk->psk_src.port[0], psk->psk_src.port[1],
1679 (psk->psk_dst.port_op == 0 ||
1680 pf_match_port(psk->psk_dst.port_op,
1681 psk->psk_dst.port[0], psk->psk_dst.port[1],
1683 (!psk->psk_label[0] ||
1684 (s->rule.ptr->label[0] &&
1685 !strcmp(psk->psk_label,
1686 s->rule.ptr->label))) &&
1687 (!psk->psk_ifname[0] ||
1688 !strcmp(psk->psk_ifname,
1689 s->kif->pfik_name))) {
1690 pf_unlink_state(s, PF_ENTER_LOCKED);
1692 goto relock_DIOCKILLSTATES;
1695 PF_HASHROW_UNLOCK(ih);
1697 psk->psk_killed = killed;
1701 case DIOCADDSTATE: {
1702 struct pfioc_state *ps = (struct pfioc_state *)addr;
1703 struct pfsync_state *sp = &ps->state;
1705 if (sp->timeout >= PFTM_MAX) {
1709 if (pfsync_state_import_ptr != NULL) {
1711 error = pfsync_state_import_ptr(sp, PFSYNC_SI_IOCTL);
1718 case DIOCGETSTATE: {
1719 struct pfioc_state *ps = (struct pfioc_state *)addr;
1722 s = pf_find_state_byid(ps->state.id, ps->state.creatorid);
1728 pfsync_state_export(&ps->state, s);
1733 case DIOCGETSTATES: {
1734 struct pfioc_states *ps = (struct pfioc_states *)addr;
1736 struct pfsync_state *pstore, *p;
1739 if (ps->ps_len == 0) {
1740 nr = uma_zone_get_cur(V_pf_state_z);
1741 ps->ps_len = sizeof(struct pfsync_state) * nr;
1745 p = pstore = malloc(ps->ps_len, M_TEMP, M_WAITOK);
1748 for (i = 0; i <= V_pf_hashmask; i++) {
1749 struct pf_idhash *ih = &V_pf_idhash[i];
1751 PF_HASHROW_LOCK(ih);
1752 LIST_FOREACH(s, &ih->states, entry) {
1754 if (s->timeout == PFTM_UNLINKED)
1757 if ((nr+1) * sizeof(*p) > ps->ps_len) {
1758 PF_HASHROW_UNLOCK(ih);
1759 goto DIOCGETSTATES_full;
1761 pfsync_state_export(p, s);
1765 PF_HASHROW_UNLOCK(ih);
1768 error = copyout(pstore, ps->ps_states,
1769 sizeof(struct pfsync_state) * nr);
1771 free(pstore, M_TEMP);
1774 ps->ps_len = sizeof(struct pfsync_state) * nr;
1775 free(pstore, M_TEMP);
1780 case DIOCGETSTATUS: {
1781 struct pf_status *s = (struct pf_status *)addr;
1783 bcopy(&V_pf_status, s, sizeof(struct pf_status));
1784 pfi_update_status(s->ifname, s);
1789 case DIOCSETSTATUSIF: {
1790 struct pfioc_if *pi = (struct pfioc_if *)addr;
1792 if (pi->ifname[0] == 0) {
1793 bzero(V_pf_status.ifname, IFNAMSIZ);
1797 strlcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ);
1802 case DIOCCLRSTATUS: {
1804 bzero(V_pf_status.counters, sizeof(V_pf_status.counters));
1805 bzero(V_pf_status.fcounters, sizeof(V_pf_status.fcounters));
1806 bzero(V_pf_status.scounters, sizeof(V_pf_status.scounters));
1807 V_pf_status.since = time_second;
1808 if (*V_pf_status.ifname)
1809 pfi_update_status(V_pf_status.ifname, NULL);
1815 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1816 struct pf_state_key *sk;
1817 struct pf_state *state;
1818 struct pf_state_key_cmp key;
1819 int m = 0, direction = pnl->direction;
1822 /* NATLOOK src and dst are reversed, so reverse sidx/didx */
1823 sidx = (direction == PF_IN) ? 1 : 0;
1824 didx = (direction == PF_IN) ? 0 : 1;
1827 PF_AZERO(&pnl->saddr, pnl->af) ||
1828 PF_AZERO(&pnl->daddr, pnl->af) ||
1829 ((pnl->proto == IPPROTO_TCP ||
1830 pnl->proto == IPPROTO_UDP) &&
1831 (!pnl->dport || !pnl->sport)))
1834 bzero(&key, sizeof(key));
1836 key.proto = pnl->proto;
1837 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
1838 key.port[sidx] = pnl->sport;
1839 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
1840 key.port[didx] = pnl->dport;
1842 state = pf_find_state_all(&key, direction, &m);
1845 error = E2BIG; /* more than one state */
1846 else if (state != NULL) {
1847 /* XXXGL: not locked read */
1848 sk = state->key[sidx];
1849 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
1850 pnl->rsport = sk->port[sidx];
1851 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
1852 pnl->rdport = sk->port[didx];
1859 case DIOCSETTIMEOUT: {
1860 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1863 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1869 old = V_pf_default_rule.timeout[pt->timeout];
1870 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
1872 V_pf_default_rule.timeout[pt->timeout] = pt->seconds;
1873 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
1874 wakeup(pf_purge_thread);
1880 case DIOCGETTIMEOUT: {
1881 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1883 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1888 pt->seconds = V_pf_default_rule.timeout[pt->timeout];
1893 case DIOCGETLIMIT: {
1894 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1896 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1901 pl->limit = V_pf_limits[pl->index].limit;
1906 case DIOCSETLIMIT: {
1907 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1911 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1912 V_pf_limits[pl->index].zone == NULL) {
1917 uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit);
1918 old_limit = V_pf_limits[pl->index].limit;
1919 V_pf_limits[pl->index].limit = pl->limit;
1920 pl->limit = old_limit;
1925 case DIOCSETDEBUG: {
1926 u_int32_t *level = (u_int32_t *)addr;
1929 V_pf_status.debug = *level;
1934 case DIOCCLRRULECTRS: {
1935 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
1936 struct pf_ruleset *ruleset = &pf_main_ruleset;
1937 struct pf_rule *rule;
1941 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
1942 rule->evaluations = 0;
1943 rule->packets[0] = rule->packets[1] = 0;
1944 rule->bytes[0] = rule->bytes[1] = 0;
1950 case DIOCGIFSPEED: {
1951 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
1952 struct pf_ifspeed ps;
1955 if (psp->ifname[0] != 0) {
1956 /* Can we completely trust user-land? */
1957 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
1958 ifp = ifunit(ps.ifname);
1960 psp->baudrate = ifp->if_baudrate;
1969 case DIOCSTARTALTQ: {
1970 struct pf_altq *altq;
1973 /* enable all altq interfaces on active list */
1974 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
1975 if (altq->qname[0] == 0 && (altq->local_flags &
1976 PFALTQ_FLAG_IF_REMOVED) == 0) {
1977 error = pf_enable_altq(altq);
1983 V_pf_altq_running = 1;
1985 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
1989 case DIOCSTOPALTQ: {
1990 struct pf_altq *altq;
1993 /* disable all altq interfaces on active list */
1994 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
1995 if (altq->qname[0] == 0 && (altq->local_flags &
1996 PFALTQ_FLAG_IF_REMOVED) == 0) {
1997 error = pf_disable_altq(altq);
2003 V_pf_altq_running = 0;
2005 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
2010 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2011 struct pf_altq *altq, *a;
2014 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK);
2015 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
2016 altq->local_flags = 0;
2019 if (pa->ticket != V_ticket_altqs_inactive) {
2021 free(altq, M_PFALTQ);
2027 * if this is for a queue, find the discipline and
2028 * copy the necessary fields
2030 if (altq->qname[0] != 0) {
2031 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2034 free(altq, M_PFALTQ);
2037 altq->altq_disc = NULL;
2038 TAILQ_FOREACH(a, V_pf_altqs_inactive, entries) {
2039 if (strncmp(a->ifname, altq->ifname,
2040 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2041 altq->altq_disc = a->altq_disc;
2047 if ((ifp = ifunit(altq->ifname)) == NULL)
2048 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2050 error = altq_add(altq);
2054 free(altq, M_PFALTQ);
2058 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries);
2059 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2064 case DIOCGETALTQS: {
2065 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2066 struct pf_altq *altq;
2070 TAILQ_FOREACH(altq, V_pf_altqs_active, entries)
2072 pa->ticket = V_ticket_altqs_active;
2078 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2079 struct pf_altq *altq;
2083 if (pa->ticket != V_ticket_altqs_active) {
2089 altq = TAILQ_FIRST(V_pf_altqs_active);
2090 while ((altq != NULL) && (nr < pa->nr)) {
2091 altq = TAILQ_NEXT(altq, entries);
2099 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2104 case DIOCCHANGEALTQ:
2105 /* CHANGEALTQ not supported yet! */
2109 case DIOCGETQSTATS: {
2110 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2111 struct pf_altq *altq;
2116 if (pq->ticket != V_ticket_altqs_active) {
2121 nbytes = pq->nbytes;
2123 altq = TAILQ_FIRST(V_pf_altqs_active);
2124 while ((altq != NULL) && (nr < pq->nr)) {
2125 altq = TAILQ_NEXT(altq, entries);
2134 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2140 error = altq_getqstats(altq, pq->buf, &nbytes);
2142 pq->scheduler = altq->scheduler;
2143 pq->nbytes = nbytes;
2149 case DIOCBEGINADDRS: {
2150 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2153 pf_empty_pool(&V_pf_pabuf);
2154 pp->ticket = ++V_ticket_pabuf;
2160 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2161 struct pf_pooladdr *pa;
2162 struct pfi_kif *kif = NULL;
2165 if (pp->af == AF_INET) {
2166 error = EAFNOSUPPORT;
2171 if (pp->af == AF_INET6) {
2172 error = EAFNOSUPPORT;
2176 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2177 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2178 pp->addr.addr.type != PF_ADDR_TABLE) {
2182 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK);
2183 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2185 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2187 if (pp->ticket != V_ticket_pabuf) {
2190 free(kif, PFI_MTYPE);
2195 if (pa->ifname[0]) {
2196 pa->kif = pfi_kif_attach(kif, pa->ifname);
2197 pfi_kif_ref(pa->kif);
2200 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error =
2201 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) {
2203 pfi_kif_unref(pa->kif);
2208 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries);
2213 case DIOCGETADDRS: {
2214 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2215 struct pf_pool *pool;
2216 struct pf_pooladdr *pa;
2220 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2221 pp->r_num, 0, 1, 0);
2227 TAILQ_FOREACH(pa, &pool->list, entries)
2234 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2235 struct pf_pool *pool;
2236 struct pf_pooladdr *pa;
2240 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2241 pp->r_num, 0, 1, 1);
2247 pa = TAILQ_FIRST(&pool->list);
2248 while ((pa != NULL) && (nr < pp->nr)) {
2249 pa = TAILQ_NEXT(pa, entries);
2257 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2258 pf_addr_copyout(&pp->addr.addr);
2263 case DIOCCHANGEADDR: {
2264 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2265 struct pf_pool *pool;
2266 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2267 struct pf_ruleset *ruleset;
2268 struct pfi_kif *kif = NULL;
2270 if (pca->action < PF_CHANGE_ADD_HEAD ||
2271 pca->action > PF_CHANGE_REMOVE) {
2275 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2276 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2277 pca->addr.addr.type != PF_ADDR_TABLE) {
2282 if (pca->action != PF_CHANGE_REMOVE) {
2284 if (pca->af == AF_INET) {
2285 error = EAFNOSUPPORT;
2290 if (pca->af == AF_INET6) {
2291 error = EAFNOSUPPORT;
2295 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK);
2296 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2297 if (newpa->ifname[0])
2298 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2302 #define ERROUT(x) { error = (x); goto DIOCCHANGEADDR_error; }
2304 ruleset = pf_find_ruleset(pca->anchor);
2305 if (ruleset == NULL)
2308 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2309 pca->r_num, pca->r_last, 1, 1);
2313 if (pca->action != PF_CHANGE_REMOVE) {
2314 if (newpa->ifname[0]) {
2315 newpa->kif = pfi_kif_attach(kif, newpa->ifname);
2316 pfi_kif_ref(newpa->kif);
2320 switch (newpa->addr.type) {
2321 case PF_ADDR_DYNIFTL:
2322 error = pfi_dynaddr_setup(&newpa->addr,
2326 newpa->addr.p.tbl = pfr_attach_table(ruleset,
2327 newpa->addr.v.tblname);
2328 if (newpa->addr.p.tbl == NULL)
2333 goto DIOCCHANGEADDR_error;
2336 switch (pca->action) {
2337 case PF_CHANGE_ADD_HEAD:
2338 oldpa = TAILQ_FIRST(&pool->list);
2340 case PF_CHANGE_ADD_TAIL:
2341 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2344 oldpa = TAILQ_FIRST(&pool->list);
2345 for (int i = 0; oldpa && i < pca->nr; i++)
2346 oldpa = TAILQ_NEXT(oldpa, entries);
2352 if (pca->action == PF_CHANGE_REMOVE) {
2353 TAILQ_REMOVE(&pool->list, oldpa, entries);
2354 switch (oldpa->addr.type) {
2355 case PF_ADDR_DYNIFTL:
2356 pfi_dynaddr_remove(oldpa->addr.p.dyn);
2359 pfr_detach_table(oldpa->addr.p.tbl);
2363 pfi_kif_unref(oldpa->kif);
2364 free(oldpa, M_PFRULE);
2367 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2368 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2369 pca->action == PF_CHANGE_ADD_BEFORE)
2370 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2372 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2376 pool->cur = TAILQ_FIRST(&pool->list);
2377 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af);
2382 DIOCCHANGEADDR_error:
2384 pfi_kif_unref(newpa->kif);
2387 free(newpa, M_PFRULE);
2389 free(kif, PFI_MTYPE);
2393 case DIOCGETRULESETS: {
2394 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2395 struct pf_ruleset *ruleset;
2396 struct pf_anchor *anchor;
2399 pr->path[sizeof(pr->path) - 1] = 0;
2400 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2406 if (ruleset->anchor == NULL) {
2407 /* XXX kludge for pf_main_ruleset */
2408 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2409 if (anchor->parent == NULL)
2412 RB_FOREACH(anchor, pf_anchor_node,
2413 &ruleset->anchor->children)
2420 case DIOCGETRULESET: {
2421 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2422 struct pf_ruleset *ruleset;
2423 struct pf_anchor *anchor;
2427 pr->path[sizeof(pr->path) - 1] = 0;
2428 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2434 if (ruleset->anchor == NULL) {
2435 /* XXX kludge for pf_main_ruleset */
2436 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2437 if (anchor->parent == NULL && nr++ == pr->nr) {
2438 strlcpy(pr->name, anchor->name,
2443 RB_FOREACH(anchor, pf_anchor_node,
2444 &ruleset->anchor->children)
2445 if (nr++ == pr->nr) {
2446 strlcpy(pr->name, anchor->name,
2457 case DIOCRCLRTABLES: {
2458 struct pfioc_table *io = (struct pfioc_table *)addr;
2460 if (io->pfrio_esize != 0) {
2465 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2466 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2471 case DIOCRADDTABLES: {
2472 struct pfioc_table *io = (struct pfioc_table *)addr;
2473 struct pfr_table *pfrts;
2476 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2480 totlen = io->pfrio_size * sizeof(struct pfr_table);
2481 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2482 error = copyin(io->pfrio_buffer, pfrts, totlen);
2484 free(pfrts, M_TEMP);
2488 error = pfr_add_tables(pfrts, io->pfrio_size,
2489 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2491 free(pfrts, M_TEMP);
2495 case DIOCRDELTABLES: {
2496 struct pfioc_table *io = (struct pfioc_table *)addr;
2497 struct pfr_table *pfrts;
2500 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2504 totlen = io->pfrio_size * sizeof(struct pfr_table);
2505 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2506 error = copyin(io->pfrio_buffer, pfrts, totlen);
2508 free(pfrts, M_TEMP);
2512 error = pfr_del_tables(pfrts, io->pfrio_size,
2513 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2515 free(pfrts, M_TEMP);
2519 case DIOCRGETTABLES: {
2520 struct pfioc_table *io = (struct pfioc_table *)addr;
2521 struct pfr_table *pfrts;
2524 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2528 totlen = io->pfrio_size * sizeof(struct pfr_table);
2529 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2531 error = pfr_get_tables(&io->pfrio_table, pfrts,
2532 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2535 error = copyout(pfrts, io->pfrio_buffer, totlen);
2536 free(pfrts, M_TEMP);
2540 case DIOCRGETTSTATS: {
2541 struct pfioc_table *io = (struct pfioc_table *)addr;
2542 struct pfr_tstats *pfrtstats;
2545 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2549 totlen = io->pfrio_size * sizeof(struct pfr_tstats);
2550 pfrtstats = malloc(totlen, M_TEMP, M_WAITOK);
2552 error = pfr_get_tstats(&io->pfrio_table, pfrtstats,
2553 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2556 error = copyout(pfrtstats, io->pfrio_buffer, totlen);
2557 free(pfrtstats, M_TEMP);
2561 case DIOCRCLRTSTATS: {
2562 struct pfioc_table *io = (struct pfioc_table *)addr;
2563 struct pfr_table *pfrts;
2566 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2570 totlen = io->pfrio_size * sizeof(struct pfr_table);
2571 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2572 error = copyin(io->pfrio_buffer, pfrts, totlen);
2574 free(pfrts, M_TEMP);
2578 error = pfr_clr_tstats(pfrts, io->pfrio_size,
2579 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2581 free(pfrts, M_TEMP);
2585 case DIOCRSETTFLAGS: {
2586 struct pfioc_table *io = (struct pfioc_table *)addr;
2587 struct pfr_table *pfrts;
2590 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2594 totlen = io->pfrio_size * sizeof(struct pfr_table);
2595 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2596 error = copyin(io->pfrio_buffer, pfrts, totlen);
2598 free(pfrts, M_TEMP);
2602 error = pfr_set_tflags(pfrts, io->pfrio_size,
2603 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2604 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2606 free(pfrts, M_TEMP);
2610 case DIOCRCLRADDRS: {
2611 struct pfioc_table *io = (struct pfioc_table *)addr;
2613 if (io->pfrio_esize != 0) {
2618 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2619 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2624 case DIOCRADDADDRS: {
2625 struct pfioc_table *io = (struct pfioc_table *)addr;
2626 struct pfr_addr *pfras;
2629 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2633 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2634 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2635 error = copyin(io->pfrio_buffer, pfras, totlen);
2637 free(pfras, M_TEMP);
2641 error = pfr_add_addrs(&io->pfrio_table, pfras,
2642 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2643 PFR_FLAG_USERIOCTL);
2645 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2646 error = copyout(pfras, io->pfrio_buffer, totlen);
2647 free(pfras, M_TEMP);
2651 case DIOCRDELADDRS: {
2652 struct pfioc_table *io = (struct pfioc_table *)addr;
2653 struct pfr_addr *pfras;
2656 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2660 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2661 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2662 error = copyin(io->pfrio_buffer, pfras, totlen);
2664 free(pfras, M_TEMP);
2668 error = pfr_del_addrs(&io->pfrio_table, pfras,
2669 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2670 PFR_FLAG_USERIOCTL);
2672 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2673 error = copyout(pfras, io->pfrio_buffer, totlen);
2674 free(pfras, M_TEMP);
2678 case DIOCRSETADDRS: {
2679 struct pfioc_table *io = (struct pfioc_table *)addr;
2680 struct pfr_addr *pfras;
2683 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2687 totlen = (io->pfrio_size + io->pfrio_size2) *
2688 sizeof(struct pfr_addr);
2689 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2690 error = copyin(io->pfrio_buffer, pfras, totlen);
2692 free(pfras, M_TEMP);
2696 error = pfr_set_addrs(&io->pfrio_table, pfras,
2697 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2698 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2699 PFR_FLAG_USERIOCTL, 0);
2701 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2702 error = copyout(pfras, io->pfrio_buffer, totlen);
2703 free(pfras, M_TEMP);
2707 case DIOCRGETADDRS: {
2708 struct pfioc_table *io = (struct pfioc_table *)addr;
2709 struct pfr_addr *pfras;
2712 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2716 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2717 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2719 error = pfr_get_addrs(&io->pfrio_table, pfras,
2720 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2723 error = copyout(pfras, io->pfrio_buffer, totlen);
2724 free(pfras, M_TEMP);
2728 case DIOCRGETASTATS: {
2729 struct pfioc_table *io = (struct pfioc_table *)addr;
2730 struct pfr_astats *pfrastats;
2733 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2737 totlen = io->pfrio_size * sizeof(struct pfr_astats);
2738 pfrastats = malloc(totlen, M_TEMP, M_WAITOK);
2740 error = pfr_get_astats(&io->pfrio_table, pfrastats,
2741 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2744 error = copyout(pfrastats, io->pfrio_buffer, totlen);
2745 free(pfrastats, M_TEMP);
2749 case DIOCRCLRASTATS: {
2750 struct pfioc_table *io = (struct pfioc_table *)addr;
2751 struct pfr_addr *pfras;
2754 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2758 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2759 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2760 error = copyin(io->pfrio_buffer, pfras, totlen);
2762 free(pfras, M_TEMP);
2766 error = pfr_clr_astats(&io->pfrio_table, pfras,
2767 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2768 PFR_FLAG_USERIOCTL);
2770 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2771 error = copyout(pfras, io->pfrio_buffer, totlen);
2772 free(pfras, M_TEMP);
2776 case DIOCRTSTADDRS: {
2777 struct pfioc_table *io = (struct pfioc_table *)addr;
2778 struct pfr_addr *pfras;
2781 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2785 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2786 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2787 error = copyin(io->pfrio_buffer, pfras, totlen);
2789 free(pfras, M_TEMP);
2793 error = pfr_tst_addrs(&io->pfrio_table, pfras,
2794 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2795 PFR_FLAG_USERIOCTL);
2798 error = copyout(pfras, io->pfrio_buffer, totlen);
2799 free(pfras, M_TEMP);
2803 case DIOCRINADEFINE: {
2804 struct pfioc_table *io = (struct pfioc_table *)addr;
2805 struct pfr_addr *pfras;
2808 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2812 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2813 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2814 error = copyin(io->pfrio_buffer, pfras, totlen);
2816 free(pfras, M_TEMP);
2820 error = pfr_ina_define(&io->pfrio_table, pfras,
2821 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2822 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2824 free(pfras, M_TEMP);
2829 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2831 error = pf_osfp_add(io);
2837 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2839 error = pf_osfp_get(io);
2845 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2846 struct pfioc_trans_e *ioes, *ioe;
2850 if (io->esize != sizeof(*ioe)) {
2854 totlen = sizeof(struct pfioc_trans_e) * io->size;
2855 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2856 error = copyin(io->array, ioes, totlen);
2862 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2863 switch (ioe->rs_num) {
2865 case PF_RULESET_ALTQ:
2866 if (ioe->anchor[0]) {
2872 if ((error = pf_begin_altq(&ioe->ticket))) {
2879 case PF_RULESET_TABLE:
2881 struct pfr_table table;
2883 bzero(&table, sizeof(table));
2884 strlcpy(table.pfrt_anchor, ioe->anchor,
2885 sizeof(table.pfrt_anchor));
2886 if ((error = pfr_ina_begin(&table,
2887 &ioe->ticket, NULL, 0))) {
2895 if ((error = pf_begin_rules(&ioe->ticket,
2896 ioe->rs_num, ioe->anchor))) {
2905 error = copyout(ioes, io->array, totlen);
2910 case DIOCXROLLBACK: {
2911 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2912 struct pfioc_trans_e *ioe, *ioes;
2916 if (io->esize != sizeof(*ioe)) {
2920 totlen = sizeof(struct pfioc_trans_e) * io->size;
2921 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2922 error = copyin(io->array, ioes, totlen);
2928 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2929 switch (ioe->rs_num) {
2931 case PF_RULESET_ALTQ:
2932 if (ioe->anchor[0]) {
2938 if ((error = pf_rollback_altq(ioe->ticket))) {
2941 goto fail; /* really bad */
2945 case PF_RULESET_TABLE:
2947 struct pfr_table table;
2949 bzero(&table, sizeof(table));
2950 strlcpy(table.pfrt_anchor, ioe->anchor,
2951 sizeof(table.pfrt_anchor));
2952 if ((error = pfr_ina_rollback(&table,
2953 ioe->ticket, NULL, 0))) {
2956 goto fail; /* really bad */
2961 if ((error = pf_rollback_rules(ioe->ticket,
2962 ioe->rs_num, ioe->anchor))) {
2965 goto fail; /* really bad */
2976 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2977 struct pfioc_trans_e *ioe, *ioes;
2978 struct pf_ruleset *rs;
2982 if (io->esize != sizeof(*ioe)) {
2986 totlen = sizeof(struct pfioc_trans_e) * io->size;
2987 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2988 error = copyin(io->array, ioes, totlen);
2994 /* First makes sure everything will succeed. */
2995 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2996 switch (ioe->rs_num) {
2998 case PF_RULESET_ALTQ:
2999 if (ioe->anchor[0]) {
3005 if (!V_altqs_inactive_open || ioe->ticket !=
3006 V_ticket_altqs_inactive) {
3014 case PF_RULESET_TABLE:
3015 rs = pf_find_ruleset(ioe->anchor);
3016 if (rs == NULL || !rs->topen || ioe->ticket !=
3025 if (ioe->rs_num < 0 || ioe->rs_num >=
3032 rs = pf_find_ruleset(ioe->anchor);
3034 !rs->rules[ioe->rs_num].inactive.open ||
3035 rs->rules[ioe->rs_num].inactive.ticket !=
3045 /* Now do the commit - no errors should happen here. */
3046 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3047 switch (ioe->rs_num) {
3049 case PF_RULESET_ALTQ:
3050 if ((error = pf_commit_altq(ioe->ticket))) {
3053 goto fail; /* really bad */
3057 case PF_RULESET_TABLE:
3059 struct pfr_table table;
3061 bzero(&table, sizeof(table));
3062 strlcpy(table.pfrt_anchor, ioe->anchor,
3063 sizeof(table.pfrt_anchor));
3064 if ((error = pfr_ina_commit(&table,
3065 ioe->ticket, NULL, NULL, 0))) {
3068 goto fail; /* really bad */
3073 if ((error = pf_commit_rules(ioe->ticket,
3074 ioe->rs_num, ioe->anchor))) {
3077 goto fail; /* really bad */
3087 case DIOCGETSRCNODES: {
3088 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3089 struct pf_srchash *sh;
3090 struct pf_src_node *n, *p, *pstore;
3093 if (psn->psn_len == 0) {
3094 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3096 PF_HASHROW_LOCK(sh);
3097 LIST_FOREACH(n, &sh->nodes, entry)
3099 PF_HASHROW_UNLOCK(sh);
3101 psn->psn_len = sizeof(struct pf_src_node) * nr;
3105 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK);
3106 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3108 PF_HASHROW_LOCK(sh);
3109 LIST_FOREACH(n, &sh->nodes, entry) {
3110 int secs = time_uptime, diff;
3112 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3115 bcopy(n, p, sizeof(struct pf_src_node));
3116 if (n->rule.ptr != NULL)
3117 p->rule.nr = n->rule.ptr->nr;
3118 p->creation = secs - p->creation;
3119 if (p->expire > secs)
3124 /* Adjust the connection rate estimate. */
3125 diff = secs - n->conn_rate.last;
3126 if (diff >= n->conn_rate.seconds)
3127 p->conn_rate.count = 0;
3129 p->conn_rate.count -=
3130 n->conn_rate.count * diff /
3131 n->conn_rate.seconds;
3135 PF_HASHROW_UNLOCK(sh);
3137 error = copyout(pstore, psn->psn_src_nodes,
3138 sizeof(struct pf_src_node) * nr);
3140 free(pstore, M_TEMP);
3143 psn->psn_len = sizeof(struct pf_src_node) * nr;
3144 free(pstore, M_TEMP);
3148 case DIOCCLRSRCNODES: {
3150 pf_clear_srcnodes(NULL);
3151 pf_purge_expired_src_nodes();
3152 V_pf_status.src_nodes = 0;
3156 case DIOCKILLSRCNODES:
3157 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr);
3160 case DIOCSETHOSTID: {
3161 u_int32_t *hostid = (u_int32_t *)addr;
3165 V_pf_status.hostid = arc4random();
3167 V_pf_status.hostid = *hostid;
3178 case DIOCIGETIFACES: {
3179 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3180 struct pfi_kif *ifstore;
3183 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3188 bufsiz = io->pfiio_size * sizeof(struct pfi_kif);
3189 ifstore = malloc(bufsiz, M_TEMP, M_WAITOK);
3191 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size);
3193 error = copyout(ifstore, io->pfiio_buffer, bufsiz);
3194 free(ifstore, M_TEMP);
3198 case DIOCSETIFFLAG: {
3199 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3202 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3207 case DIOCCLRIFFLAG: {
3208 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3211 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3227 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
3229 bzero(sp, sizeof(struct pfsync_state));
3231 /* copy from state key */
3232 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
3233 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
3234 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
3235 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
3236 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
3237 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
3238 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
3239 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
3240 sp->proto = st->key[PF_SK_WIRE]->proto;
3241 sp->af = st->key[PF_SK_WIRE]->af;
3243 /* copy from state */
3244 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
3245 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
3246 sp->creation = htonl(time_uptime - st->creation);
3247 sp->expire = pf_state_expires(st);
3248 if (sp->expire <= time_uptime)
3249 sp->expire = htonl(0);
3251 sp->expire = htonl(sp->expire - time_uptime);
3253 sp->direction = st->direction;
3255 sp->timeout = st->timeout;
3256 sp->state_flags = st->state_flags;
3258 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
3259 if (st->nat_src_node)
3260 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
3263 sp->creatorid = st->creatorid;
3264 pf_state_peer_hton(&st->src, &sp->src);
3265 pf_state_peer_hton(&st->dst, &sp->dst);
3267 if (st->rule.ptr == NULL)
3268 sp->rule = htonl(-1);
3270 sp->rule = htonl(st->rule.ptr->nr);
3271 if (st->anchor.ptr == NULL)
3272 sp->anchor = htonl(-1);
3274 sp->anchor = htonl(st->anchor.ptr->nr);
3275 if (st->nat_rule.ptr == NULL)
3276 sp->nat_rule = htonl(-1);
3278 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
3280 pf_state_counter_hton(st->packets[0], sp->packets[0]);
3281 pf_state_counter_hton(st->packets[1], sp->packets[1]);
3282 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
3283 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
3288 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
3290 struct pfr_ktable *kt;
3292 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type));
3295 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
3296 kt = kt->pfrkt_root;
3298 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
3303 * XXX - Check for version missmatch!!!
3306 pf_clear_states(void)
3311 for (i = 0; i <= V_pf_hashmask; i++) {
3312 struct pf_idhash *ih = &V_pf_idhash[i];
3314 PF_HASHROW_LOCK(ih);
3315 LIST_FOREACH(s, &ih->states, entry) {
3316 s->timeout = PFTM_PURGE;
3317 /* Don't send out individual delete messages. */
3318 s->sync_state = PFSTATE_NOSYNC;
3319 pf_unlink_state(s, PF_ENTER_LOCKED);
3322 PF_HASHROW_UNLOCK(ih);
3327 pf_clear_tables(void)
3329 struct pfioc_table io;
3332 bzero(&io, sizeof(io));
3334 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3341 pf_clear_srcnodes(struct pf_src_node *n)
3346 for (i = 0; i <= V_pf_hashmask; i++) {
3347 struct pf_idhash *ih = &V_pf_idhash[i];
3349 PF_HASHROW_LOCK(ih);
3350 LIST_FOREACH(s, &ih->states, entry) {
3351 if (n == NULL || n == s->src_node)
3353 if (n == NULL || n == s->nat_src_node)
3354 s->nat_src_node = NULL;
3356 PF_HASHROW_UNLOCK(ih);
3360 struct pf_srchash *sh;
3362 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3364 PF_HASHROW_LOCK(sh);
3365 LIST_FOREACH(n, &sh->nodes, entry) {
3369 PF_HASHROW_UNLOCK(sh);
3372 /* XXX: hash slot should already be locked here. */
3379 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk)
3381 struct pf_src_node_list kill;
3384 for (int i = 0; i <= V_pf_srchashmask; i++) {
3385 struct pf_srchash *sh = &V_pf_srchash[i];
3386 struct pf_src_node *sn, *tmp;
3388 PF_HASHROW_LOCK(sh);
3389 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp)
3390 if (PF_MATCHA(psnk->psnk_src.neg,
3391 &psnk->psnk_src.addr.v.a.addr,
3392 &psnk->psnk_src.addr.v.a.mask,
3393 &sn->addr, sn->af) &&
3394 PF_MATCHA(psnk->psnk_dst.neg,
3395 &psnk->psnk_dst.addr.v.a.addr,
3396 &psnk->psnk_dst.addr.v.a.mask,
3397 &sn->raddr, sn->af)) {
3398 pf_unlink_src_node_locked(sn);
3399 LIST_INSERT_HEAD(&kill, sn, entry);
3402 PF_HASHROW_UNLOCK(sh);
3405 for (int i = 0; i <= V_pf_hashmask; i++) {
3406 struct pf_idhash *ih = &V_pf_idhash[i];
3409 PF_HASHROW_LOCK(ih);
3410 LIST_FOREACH(s, &ih->states, entry) {
3411 if (s->src_node && s->src_node->expire == 1) {
3413 s->src_node->states--;
3417 if (s->nat_src_node && s->nat_src_node->expire == 1) {
3419 s->nat_src_node->states--;
3421 s->nat_src_node = NULL;
3424 PF_HASHROW_UNLOCK(ih);
3427 psnk->psnk_killed = pf_free_src_nodes(&kill);
3431 * XXX - Check for version missmatch!!!
3435 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3444 V_pf_status.running = 0;
3446 counter_u64_free(V_pf_default_rule.states_cur);
3447 counter_u64_free(V_pf_default_rule.states_tot);
3448 counter_u64_free(V_pf_default_rule.src_nodes);
3451 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3453 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3456 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3458 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3459 break; /* XXX: rollback? */
3461 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3463 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3464 break; /* XXX: rollback? */
3466 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3468 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3469 break; /* XXX: rollback? */
3471 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3473 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3474 break; /* XXX: rollback? */
3477 /* XXX: these should always succeed here */
3478 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3479 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3480 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3481 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3482 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3484 if ((error = pf_clear_tables()) != 0)
3488 if ((error = pf_begin_altq(&t[0])) != 0) {
3489 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3492 pf_commit_altq(t[0]);
3497 pf_clear_srcnodes(NULL);
3499 /* status does not use malloced mem so no need to cleanup */
3500 /* fingerprints and interfaces have thier own cleanup code */
3508 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3513 chk = pf_test(PF_IN, ifp, m, inp);
3523 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3528 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3529 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3530 in_delayed_cksum(*m);
3531 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3534 chk = pf_test(PF_OUT, ifp, m, inp);
3546 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3552 * In case of loopback traffic IPv6 uses the real interface in
3553 * order to support scoped addresses. In order to support stateful
3554 * filtering we have change this to lo0 as it is the case in IPv4.
3556 CURVNET_SET(ifp->if_vnet);
3557 chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? V_loif : ifp, m, inp);
3567 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3572 /* We need a proper CSUM before we start (s. OpenBSD ip_output) */
3573 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3575 /* XXX-BZ copy&paste error from r126261? */
3576 in_delayed_cksum(*m);
3578 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3580 CURVNET_SET(ifp->if_vnet);
3581 chk = pf_test6(PF_OUT, ifp, m, inp);
3595 struct pfil_head *pfh_inet;
3598 struct pfil_head *pfh_inet6;
3601 if (V_pf_pfil_hooked)
3605 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3606 if (pfh_inet == NULL)
3607 return (ESRCH); /* XXX */
3608 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3609 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3612 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3613 if (pfh_inet6 == NULL) {
3615 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3617 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3620 return (ESRCH); /* XXX */
3622 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3623 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3626 V_pf_pfil_hooked = 1;
3634 struct pfil_head *pfh_inet;
3637 struct pfil_head *pfh_inet6;
3640 if (V_pf_pfil_hooked == 0)
3644 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3645 if (pfh_inet == NULL)
3646 return (ESRCH); /* XXX */
3647 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3649 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3653 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3654 if (pfh_inet6 == NULL)
3655 return (ESRCH); /* XXX */
3656 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3658 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3662 V_pf_pfil_hooked = 0;
3671 VNET_ITERATOR_DECL(vnet_iter);
3674 VNET_FOREACH(vnet_iter) {
3675 CURVNET_SET(vnet_iter);
3676 V_pf_pfil_hooked = 0;
3677 V_pf_end_threads = 0;
3678 TAILQ_INIT(&V_pf_tags);
3679 TAILQ_INIT(&V_pf_qids);
3682 VNET_LIST_RUNLOCK();
3684 rw_init(&pf_rules_lock, "pf rulesets");
3686 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3687 if ((error = pfattach()) != 0)
3699 V_pf_status.running = 0;
3701 swi_remove(V_pf_swi_cookie);
3702 error = dehook_pf();
3705 * Should not happen!
3706 * XXX Due to error code ESRCH, kldunload will show
3707 * a message like 'No such process'.
3709 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3714 V_pf_end_threads = 1;
3715 while (V_pf_end_threads < 2) {
3716 wakeup_one(pf_purge_thread);
3717 rw_sleep(pf_purge_thread, &pf_rules_lock, 0, "pftmo", 0);
3719 pf_normalize_cleanup();
3725 destroy_dev(pf_dev);
3726 rw_destroy(&pf_rules_lock);
3732 pf_modevent(module_t mod, int type, void *data)
3742 * Module should not be unloaded due to race conditions.
3747 error = pf_unload();
3757 static moduledata_t pf_mod = {
3763 DECLARE_MODULE(pf, pf_mod, SI_SUB_PSEUDO, SI_ORDER_FIRST);
3764 MODULE_VERSION(pf, PF_MODVER);