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_tbladdr_copyout(struct pf_addr_wrap *);
157 * Wrapper functions for pfil(9) hooks
160 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
161 int dir, struct inpcb *inp);
162 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
163 int dir, struct inpcb *inp);
166 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
167 int dir, struct inpcb *inp);
168 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
169 int dir, struct inpcb *inp);
172 static int hook_pf(void);
173 static int dehook_pf(void);
174 static int shutdown_pf(void);
175 static int pf_load(void);
176 static int pf_unload(void);
178 static struct cdevsw pf_cdevsw = {
181 .d_version = D_VERSION,
184 static volatile VNET_DEFINE(int, pf_pfil_hooked);
185 #define V_pf_pfil_hooked VNET(pf_pfil_hooked)
186 VNET_DEFINE(int, pf_end_threads);
188 struct rwlock pf_rules_lock;
191 pfsync_state_import_t *pfsync_state_import_ptr = NULL;
192 pfsync_insert_state_t *pfsync_insert_state_ptr = NULL;
193 pfsync_update_state_t *pfsync_update_state_ptr = NULL;
194 pfsync_delete_state_t *pfsync_delete_state_ptr = NULL;
195 pfsync_clear_states_t *pfsync_clear_states_ptr = NULL;
196 pfsync_defer_t *pfsync_defer_ptr = NULL;
198 pflog_packet_t *pflog_packet_ptr = NULL;
203 u_int32_t *my_timeout = V_pf_default_rule.timeout;
211 V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
212 V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
214 RB_INIT(&V_pf_anchors);
215 pf_init_ruleset(&pf_main_ruleset);
217 /* default rule should never be garbage collected */
218 V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next;
219 #ifdef PF_DEFAULT_TO_DROP
220 V_pf_default_rule.action = PF_DROP;
222 V_pf_default_rule.action = PF_PASS;
224 V_pf_default_rule.nr = -1;
225 V_pf_default_rule.rtableid = -1;
227 /* initialize default timeouts */
228 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
229 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
230 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
231 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
232 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
233 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
234 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
235 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
236 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
237 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
238 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
239 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
240 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
241 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
242 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
243 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
244 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
245 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
246 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
247 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
249 bzero(&V_pf_status, sizeof(V_pf_status));
250 V_pf_status.debug = PF_DEBUG_URGENT;
252 V_pf_pfil_hooked = 0;
254 /* XXX do our best to avoid a conflict */
255 V_pf_status.hostid = arc4random();
257 if ((error = kproc_create(pf_purge_thread, curvnet, NULL, 0, 0,
259 /* XXXGL: leaked all above. */
261 if ((error = swi_add(NULL, "pf send", pf_intr, curvnet, SWI_NET,
262 INTR_MPSAFE, &V_pf_swi_cookie)) != 0)
263 /* XXXGL: leaked all above. */
269 static struct pf_pool *
270 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
271 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
272 u_int8_t check_ticket)
274 struct pf_ruleset *ruleset;
275 struct pf_rule *rule;
278 ruleset = pf_find_ruleset(anchor);
281 rs_num = pf_get_ruleset_number(rule_action);
282 if (rs_num >= PF_RULESET_MAX)
285 if (check_ticket && ticket !=
286 ruleset->rules[rs_num].active.ticket)
289 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
292 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
294 if (check_ticket && ticket !=
295 ruleset->rules[rs_num].inactive.ticket)
298 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
301 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
304 while ((rule != NULL) && (rule->nr != rule_number))
305 rule = TAILQ_NEXT(rule, entries);
310 return (&rule->rpool);
314 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
316 struct pf_pooladdr *mv_pool_pa;
318 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
319 TAILQ_REMOVE(poola, mv_pool_pa, entries);
320 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
325 pf_empty_pool(struct pf_palist *poola)
327 struct pf_pooladdr *pa;
329 while ((pa = TAILQ_FIRST(poola)) != NULL) {
330 switch (pa->addr.type) {
331 case PF_ADDR_DYNIFTL:
332 pfi_dynaddr_remove(pa->addr.p.dyn);
335 pfr_detach_table(pa->addr.p.tbl);
339 pfi_kif_unref(pa->kif);
340 TAILQ_REMOVE(poola, pa, entries);
346 pf_unlink_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
351 TAILQ_REMOVE(rulequeue, rule, entries);
353 PF_UNLNKDRULES_LOCK();
354 rule->rule_flag |= PFRULE_REFS;
355 TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries);
356 PF_UNLNKDRULES_UNLOCK();
360 pf_free_rule(struct pf_rule *rule)
366 tag_unref(&V_pf_tags, rule->tag);
368 tag_unref(&V_pf_tags, rule->match_tag);
370 if (rule->pqid != rule->qid)
371 pf_qid_unref(rule->pqid);
372 pf_qid_unref(rule->qid);
374 switch (rule->src.addr.type) {
375 case PF_ADDR_DYNIFTL:
376 pfi_dynaddr_remove(rule->src.addr.p.dyn);
379 pfr_detach_table(rule->src.addr.p.tbl);
382 switch (rule->dst.addr.type) {
383 case PF_ADDR_DYNIFTL:
384 pfi_dynaddr_remove(rule->dst.addr.p.dyn);
387 pfr_detach_table(rule->dst.addr.p.tbl);
390 if (rule->overload_tbl)
391 pfr_detach_table(rule->overload_tbl);
393 pfi_kif_unref(rule->kif);
394 pf_anchor_remove(rule);
395 pf_empty_pool(&rule->rpool.list);
396 free(rule, M_PFRULE);
400 tagname2tag(struct pf_tags *head, char *tagname)
402 struct pf_tagname *tag, *p = NULL;
403 u_int16_t new_tagid = 1;
407 TAILQ_FOREACH(tag, head, entries)
408 if (strcmp(tagname, tag->name) == 0) {
414 * to avoid fragmentation, we do a linear search from the beginning
415 * and take the first free slot we find. if there is none or the list
416 * is empty, append a new entry at the end.
420 if (!TAILQ_EMPTY(head))
421 for (p = TAILQ_FIRST(head); p != NULL &&
422 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
423 new_tagid = p->tag + 1;
425 if (new_tagid > TAGID_MAX)
428 /* allocate and fill new struct pf_tagname */
429 tag = malloc(sizeof(*tag), M_PFTAG, M_NOWAIT|M_ZERO);
432 strlcpy(tag->name, tagname, sizeof(tag->name));
433 tag->tag = new_tagid;
436 if (p != NULL) /* insert new entry before p */
437 TAILQ_INSERT_BEFORE(p, tag, entries);
438 else /* either list empty or no free slot in between */
439 TAILQ_INSERT_TAIL(head, tag, entries);
445 tag_unref(struct pf_tags *head, u_int16_t tag)
447 struct pf_tagname *p, *next;
451 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
452 next = TAILQ_NEXT(p, entries);
455 TAILQ_REMOVE(head, p, entries);
464 pf_tagname2tag(char *tagname)
466 return (tagname2tag(&V_pf_tags, tagname));
471 pf_qname2qid(char *qname)
473 return ((u_int32_t)tagname2tag(&V_pf_qids, qname));
477 pf_qid_unref(u_int32_t qid)
479 tag_unref(&V_pf_qids, (u_int16_t)qid);
483 pf_begin_altq(u_int32_t *ticket)
485 struct pf_altq *altq;
490 /* Purge the old altq list */
491 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
492 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
493 if (altq->qname[0] == 0 &&
494 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
495 /* detach and destroy the discipline */
496 error = altq_remove(altq);
498 pf_qid_unref(altq->qid);
499 free(altq, M_PFALTQ);
503 *ticket = ++V_ticket_altqs_inactive;
504 V_altqs_inactive_open = 1;
509 pf_rollback_altq(u_int32_t ticket)
511 struct pf_altq *altq;
516 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
518 /* Purge the old altq list */
519 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
520 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
521 if (altq->qname[0] == 0 &&
522 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
523 /* detach and destroy the discipline */
524 error = altq_remove(altq);
526 pf_qid_unref(altq->qid);
527 free(altq, M_PFALTQ);
529 V_altqs_inactive_open = 0;
534 pf_commit_altq(u_int32_t ticket)
536 struct pf_altqqueue *old_altqs;
537 struct pf_altq *altq;
542 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
545 /* swap altqs, keep the old. */
546 old_altqs = V_pf_altqs_active;
547 V_pf_altqs_active = V_pf_altqs_inactive;
548 V_pf_altqs_inactive = old_altqs;
549 V_ticket_altqs_active = V_ticket_altqs_inactive;
551 /* Attach new disciplines */
552 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
553 if (altq->qname[0] == 0 &&
554 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
555 /* attach the discipline */
556 error = altq_pfattach(altq);
557 if (error == 0 && V_pf_altq_running)
558 error = pf_enable_altq(altq);
564 /* Purge the old altq list */
565 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
566 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
567 if (altq->qname[0] == 0 &&
568 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
569 /* detach and destroy the discipline */
570 if (V_pf_altq_running)
571 error = pf_disable_altq(altq);
572 err = altq_pfdetach(altq);
573 if (err != 0 && error == 0)
575 err = altq_remove(altq);
576 if (err != 0 && error == 0)
579 pf_qid_unref(altq->qid);
580 free(altq, M_PFALTQ);
583 V_altqs_inactive_open = 0;
588 pf_enable_altq(struct pf_altq *altq)
591 struct tb_profile tb;
594 if ((ifp = ifunit(altq->ifname)) == NULL)
597 if (ifp->if_snd.altq_type != ALTQT_NONE)
598 error = altq_enable(&ifp->if_snd);
600 /* set tokenbucket regulator */
601 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
602 tb.rate = altq->ifbandwidth;
603 tb.depth = altq->tbrsize;
604 error = tbr_set(&ifp->if_snd, &tb);
611 pf_disable_altq(struct pf_altq *altq)
614 struct tb_profile tb;
617 if ((ifp = ifunit(altq->ifname)) == NULL)
621 * when the discipline is no longer referenced, it was overridden
622 * by a new one. if so, just return.
624 if (altq->altq_disc != ifp->if_snd.altq_disc)
627 error = altq_disable(&ifp->if_snd);
630 /* clear tokenbucket regulator */
632 error = tbr_set(&ifp->if_snd, &tb);
639 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
642 struct pf_altq *a1, *a2, *a3;
646 /* Interrupt userland queue modifications */
647 if (V_altqs_inactive_open)
648 pf_rollback_altq(V_ticket_altqs_inactive);
650 /* Start new altq ruleset */
651 if (pf_begin_altq(&ticket))
654 /* Copy the current active set */
655 TAILQ_FOREACH(a1, V_pf_altqs_active, entries) {
656 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
661 bcopy(a1, a2, sizeof(struct pf_altq));
663 if (a2->qname[0] != 0) {
664 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
669 a2->altq_disc = NULL;
670 TAILQ_FOREACH(a3, V_pf_altqs_inactive, entries) {
671 if (strncmp(a3->ifname, a2->ifname,
672 IFNAMSIZ) == 0 && a3->qname[0] == 0) {
673 a2->altq_disc = a3->altq_disc;
678 /* Deactivate the interface in question */
679 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
680 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
681 (remove && ifp1 == ifp)) {
682 a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
684 error = altq_add(a2);
686 if (ticket != V_ticket_altqs_inactive)
695 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries);
699 pf_rollback_altq(ticket);
701 pf_commit_altq(ticket);
706 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
708 struct pf_ruleset *rs;
709 struct pf_rule *rule;
713 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
715 rs = pf_find_or_create_ruleset(anchor);
718 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
719 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
720 rs->rules[rs_num].inactive.rcount--;
722 *ticket = ++rs->rules[rs_num].inactive.ticket;
723 rs->rules[rs_num].inactive.open = 1;
728 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
730 struct pf_ruleset *rs;
731 struct pf_rule *rule;
735 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
737 rs = pf_find_ruleset(anchor);
738 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
739 rs->rules[rs_num].inactive.ticket != ticket)
741 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
742 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
743 rs->rules[rs_num].inactive.rcount--;
745 rs->rules[rs_num].inactive.open = 0;
749 #define PF_MD5_UPD(st, elm) \
750 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
752 #define PF_MD5_UPD_STR(st, elm) \
753 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
755 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
756 (stor) = htonl((st)->elm); \
757 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
760 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
761 (stor) = htons((st)->elm); \
762 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
766 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
768 PF_MD5_UPD(pfr, addr.type);
769 switch (pfr->addr.type) {
770 case PF_ADDR_DYNIFTL:
771 PF_MD5_UPD(pfr, addr.v.ifname);
772 PF_MD5_UPD(pfr, addr.iflags);
775 PF_MD5_UPD(pfr, addr.v.tblname);
777 case PF_ADDR_ADDRMASK:
779 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
780 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
784 PF_MD5_UPD(pfr, port[0]);
785 PF_MD5_UPD(pfr, port[1]);
786 PF_MD5_UPD(pfr, neg);
787 PF_MD5_UPD(pfr, port_op);
791 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
796 pf_hash_rule_addr(ctx, &rule->src);
797 pf_hash_rule_addr(ctx, &rule->dst);
798 PF_MD5_UPD_STR(rule, label);
799 PF_MD5_UPD_STR(rule, ifname);
800 PF_MD5_UPD_STR(rule, match_tagname);
801 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
802 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
803 PF_MD5_UPD_HTONL(rule, prob, y);
804 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
805 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
806 PF_MD5_UPD(rule, uid.op);
807 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
808 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
809 PF_MD5_UPD(rule, gid.op);
810 PF_MD5_UPD_HTONL(rule, rule_flag, y);
811 PF_MD5_UPD(rule, action);
812 PF_MD5_UPD(rule, direction);
813 PF_MD5_UPD(rule, af);
814 PF_MD5_UPD(rule, quick);
815 PF_MD5_UPD(rule, ifnot);
816 PF_MD5_UPD(rule, match_tag_not);
817 PF_MD5_UPD(rule, natpass);
818 PF_MD5_UPD(rule, keep_state);
819 PF_MD5_UPD(rule, proto);
820 PF_MD5_UPD(rule, type);
821 PF_MD5_UPD(rule, code);
822 PF_MD5_UPD(rule, flags);
823 PF_MD5_UPD(rule, flagset);
824 PF_MD5_UPD(rule, allow_opts);
825 PF_MD5_UPD(rule, rt);
826 PF_MD5_UPD(rule, tos);
830 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
832 struct pf_ruleset *rs;
833 struct pf_rule *rule, **old_array;
834 struct pf_rulequeue *old_rules;
836 u_int32_t old_rcount;
840 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
842 rs = pf_find_ruleset(anchor);
843 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
844 ticket != rs->rules[rs_num].inactive.ticket)
847 /* Calculate checksum for the main ruleset */
848 if (rs == &pf_main_ruleset) {
849 error = pf_setup_pfsync_matching(rs);
854 /* Swap rules, keep the old. */
855 old_rules = rs->rules[rs_num].active.ptr;
856 old_rcount = rs->rules[rs_num].active.rcount;
857 old_array = rs->rules[rs_num].active.ptr_array;
859 rs->rules[rs_num].active.ptr =
860 rs->rules[rs_num].inactive.ptr;
861 rs->rules[rs_num].active.ptr_array =
862 rs->rules[rs_num].inactive.ptr_array;
863 rs->rules[rs_num].active.rcount =
864 rs->rules[rs_num].inactive.rcount;
865 rs->rules[rs_num].inactive.ptr = old_rules;
866 rs->rules[rs_num].inactive.ptr_array = old_array;
867 rs->rules[rs_num].inactive.rcount = old_rcount;
869 rs->rules[rs_num].active.ticket =
870 rs->rules[rs_num].inactive.ticket;
871 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
874 /* Purge the old rule list. */
875 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
876 pf_unlink_rule(old_rules, rule);
877 if (rs->rules[rs_num].inactive.ptr_array)
878 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
879 rs->rules[rs_num].inactive.ptr_array = NULL;
880 rs->rules[rs_num].inactive.rcount = 0;
881 rs->rules[rs_num].inactive.open = 0;
882 pf_remove_if_empty_ruleset(rs);
888 pf_setup_pfsync_matching(struct pf_ruleset *rs)
891 struct pf_rule *rule;
893 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
896 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
897 /* XXX PF_RULESET_SCRUB as well? */
898 if (rs_cnt == PF_RULESET_SCRUB)
901 if (rs->rules[rs_cnt].inactive.ptr_array)
902 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
903 rs->rules[rs_cnt].inactive.ptr_array = NULL;
905 if (rs->rules[rs_cnt].inactive.rcount) {
906 rs->rules[rs_cnt].inactive.ptr_array =
907 malloc(sizeof(caddr_t) *
908 rs->rules[rs_cnt].inactive.rcount,
911 if (!rs->rules[rs_cnt].inactive.ptr_array)
915 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
917 pf_hash_rule(&ctx, rule);
918 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
922 MD5Final(digest, &ctx);
923 memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum));
928 pf_addr_setup(struct pf_ruleset *ruleset, struct pf_addr_wrap *addr,
933 switch (addr->type) {
935 addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname);
936 if (addr->p.tbl == NULL)
939 case PF_ADDR_DYNIFTL:
940 error = pfi_dynaddr_setup(addr, af);
948 pf_addr_copyout(struct pf_addr_wrap *addr)
951 switch (addr->type) {
952 case PF_ADDR_DYNIFTL:
953 pfi_dynaddr_copyout(addr);
956 pf_tbladdr_copyout(addr);
962 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
966 /* XXX keep in sync with switch() below */
967 if (securelevel_gt(td->td_ucred, 2))
974 case DIOCSETSTATUSIF:
981 case DIOCCLRRULECTRS:
986 case DIOCGETRULESETS:
1000 case DIOCGETSRCNODES:
1001 case DIOCCLRSRCNODES:
1002 case DIOCIGETIFACES:
1007 case DIOCRCLRTABLES:
1008 case DIOCRADDTABLES:
1009 case DIOCRDELTABLES:
1010 case DIOCRSETTFLAGS:
1011 if (((struct pfioc_table *)addr)->pfrio_flags &
1013 break; /* dummy operation ok */
1019 if (!(flags & FWRITE))
1027 case DIOCGETTIMEOUT:
1032 case DIOCGETRULESETS:
1033 case DIOCGETRULESET:
1035 case DIOCRGETTABLES:
1036 case DIOCRGETTSTATS:
1038 case DIOCRGETASTATS:
1041 case DIOCGETSRCNODES:
1042 case DIOCIGETIFACES:
1045 case DIOCRCLRTABLES:
1046 case DIOCRADDTABLES:
1047 case DIOCRDELTABLES:
1048 case DIOCRCLRTSTATS:
1053 case DIOCRSETTFLAGS:
1054 if (((struct pfioc_table *)addr)->pfrio_flags &
1056 flags |= FWRITE; /* need write lock for dummy */
1057 break; /* dummy operation ok */
1061 if (((struct pfioc_rule *)addr)->action ==
1069 CURVNET_SET(TD_TO_VNET(td));
1074 if (V_pf_status.running)
1082 DPFPRINTF(PF_DEBUG_MISC,
1083 ("pf: pfil registration failed\n"));
1087 V_pf_status.running = 1;
1088 V_pf_status.since = time_second;
1091 V_pf_stateid[cpu] = time_second;
1093 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1100 if (!V_pf_status.running)
1103 V_pf_status.running = 0;
1105 error = dehook_pf();
1107 V_pf_status.running = 1;
1108 DPFPRINTF(PF_DEBUG_MISC,
1109 ("pf: pfil unregistration failed\n"));
1112 V_pf_status.since = time_second;
1113 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1119 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1120 struct pf_ruleset *ruleset;
1121 struct pf_rule *rule, *tail;
1122 struct pf_pooladdr *pa;
1123 struct pfi_kif *kif = NULL;
1126 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1131 if (pr->rule.af == AF_INET) {
1132 error = EAFNOSUPPORT;
1137 if (pr->rule.af == AF_INET6) {
1138 error = EAFNOSUPPORT;
1143 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK);
1144 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1145 if (rule->ifname[0])
1146 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1147 rule->cuid = td->td_ucred->cr_ruid;
1148 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1149 TAILQ_INIT(&rule->rpool.list);
1151 #define ERROUT(x) { error = (x); goto DIOCADDRULE_error; }
1154 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1155 ruleset = pf_find_ruleset(pr->anchor);
1156 if (ruleset == NULL)
1158 rs_num = pf_get_ruleset_number(pr->rule.action);
1159 if (rs_num >= PF_RULESET_MAX)
1161 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1162 DPFPRINTF(PF_DEBUG_MISC,
1163 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1164 ruleset->rules[rs_num].inactive.ticket));
1167 if (pr->pool_ticket != V_ticket_pabuf) {
1168 DPFPRINTF(PF_DEBUG_MISC,
1169 ("pool_ticket: %d != %d\n", pr->pool_ticket,
1174 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1177 rule->nr = tail->nr + 1;
1180 if (rule->ifname[0]) {
1181 rule->kif = pfi_kif_attach(kif, rule->ifname);
1182 pfi_kif_ref(rule->kif);
1186 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs)
1191 if (rule->qname[0] != 0) {
1192 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1194 else if (rule->pqname[0] != 0) {
1196 pf_qname2qid(rule->pqname)) == 0)
1199 rule->pqid = rule->qid;
1202 if (rule->tagname[0])
1203 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1205 if (rule->match_tagname[0])
1206 if ((rule->match_tag =
1207 pf_tagname2tag(rule->match_tagname)) == 0)
1209 if (rule->rt && !rule->direction)
1213 if (rule->logif >= PFLOGIFS_MAX)
1215 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
1217 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
1219 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1221 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1222 if (pa->addr.type == PF_ADDR_TABLE) {
1223 pa->addr.p.tbl = pfr_attach_table(ruleset,
1224 pa->addr.v.tblname);
1225 if (pa->addr.p.tbl == NULL)
1229 if (rule->overload_tblname[0]) {
1230 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1231 rule->overload_tblname)) == NULL)
1234 rule->overload_tbl->pfrkt_flags |=
1238 pf_mv_pool(&V_pf_pabuf, &rule->rpool.list);
1239 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1240 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1241 (rule->rt > PF_FASTROUTE)) &&
1242 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1251 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1252 rule->evaluations = rule->packets[0] = rule->packets[1] =
1253 rule->bytes[0] = rule->bytes[1] = 0;
1254 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1256 ruleset->rules[rs_num].inactive.rcount++;
1263 free(rule, M_PFRULE);
1265 free(kif, PFI_MTYPE);
1269 case DIOCGETRULES: {
1270 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1271 struct pf_ruleset *ruleset;
1272 struct pf_rule *tail;
1276 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1277 ruleset = pf_find_ruleset(pr->anchor);
1278 if (ruleset == NULL) {
1283 rs_num = pf_get_ruleset_number(pr->rule.action);
1284 if (rs_num >= PF_RULESET_MAX) {
1289 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1292 pr->nr = tail->nr + 1;
1295 pr->ticket = ruleset->rules[rs_num].active.ticket;
1301 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1302 struct pf_ruleset *ruleset;
1303 struct pf_rule *rule;
1307 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1308 ruleset = pf_find_ruleset(pr->anchor);
1309 if (ruleset == NULL) {
1314 rs_num = pf_get_ruleset_number(pr->rule.action);
1315 if (rs_num >= PF_RULESET_MAX) {
1320 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1325 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1326 while ((rule != NULL) && (rule->nr != pr->nr))
1327 rule = TAILQ_NEXT(rule, entries);
1333 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1334 if (pf_anchor_copyout(ruleset, rule, pr)) {
1339 pf_addr_copyout(&pr->rule.src.addr);
1340 pf_addr_copyout(&pr->rule.dst.addr);
1341 for (i = 0; i < PF_SKIP_COUNT; ++i)
1342 if (rule->skip[i].ptr == NULL)
1343 pr->rule.skip[i].nr = -1;
1345 pr->rule.skip[i].nr =
1346 rule->skip[i].ptr->nr;
1348 if (pr->action == PF_GET_CLR_CNTR) {
1349 rule->evaluations = 0;
1350 rule->packets[0] = rule->packets[1] = 0;
1351 rule->bytes[0] = rule->bytes[1] = 0;
1352 rule->states_tot = 0;
1358 case DIOCCHANGERULE: {
1359 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1360 struct pf_ruleset *ruleset;
1361 struct pf_rule *oldrule = NULL, *newrule = NULL;
1362 struct pfi_kif *kif = NULL;
1363 struct pf_pooladdr *pa;
1367 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1368 pcr->action > PF_CHANGE_GET_TICKET) {
1372 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1377 if (pcr->action != PF_CHANGE_REMOVE) {
1379 if (pcr->rule.af == AF_INET) {
1380 error = EAFNOSUPPORT;
1385 if (pcr->rule.af == AF_INET6) {
1386 error = EAFNOSUPPORT;
1390 newrule = malloc(sizeof(*newrule), M_PFRULE, M_WAITOK);
1391 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1392 newrule->cuid = td->td_ucred->cr_ruid;
1393 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1394 TAILQ_INIT(&newrule->rpool.list);
1395 /* Initialize refcounting. */
1396 newrule->states_cur = 0;
1397 newrule->entries.tqe_prev = NULL;
1399 if (newrule->ifname[0])
1400 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1403 #define ERROUT(x) { error = (x); goto DIOCCHANGERULE_error; }
1406 if (!(pcr->action == PF_CHANGE_REMOVE ||
1407 pcr->action == PF_CHANGE_GET_TICKET) &&
1408 pcr->pool_ticket != V_ticket_pabuf)
1411 ruleset = pf_find_ruleset(pcr->anchor);
1412 if (ruleset == NULL)
1415 rs_num = pf_get_ruleset_number(pcr->rule.action);
1416 if (rs_num >= PF_RULESET_MAX)
1419 if (pcr->action == PF_CHANGE_GET_TICKET) {
1420 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1422 } else if (pcr->ticket !=
1423 ruleset->rules[rs_num].active.ticket)
1426 if (pcr->action != PF_CHANGE_REMOVE) {
1427 if (newrule->ifname[0]) {
1428 newrule->kif = pfi_kif_attach(kif,
1430 pfi_kif_ref(newrule->kif);
1432 newrule->kif = NULL;
1434 if (newrule->rtableid > 0 &&
1435 newrule->rtableid >= rt_numfibs)
1440 if (newrule->qname[0] != 0) {
1442 pf_qname2qid(newrule->qname)) == 0)
1444 else if (newrule->pqname[0] != 0) {
1445 if ((newrule->pqid =
1446 pf_qname2qid(newrule->pqname)) == 0)
1449 newrule->pqid = newrule->qid;
1452 if (newrule->tagname[0])
1454 pf_tagname2tag(newrule->tagname)) == 0)
1456 if (newrule->match_tagname[0])
1457 if ((newrule->match_tag = pf_tagname2tag(
1458 newrule->match_tagname)) == 0)
1460 if (newrule->rt && !newrule->direction)
1464 if (newrule->logif >= PFLOGIFS_MAX)
1466 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
1468 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
1470 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1472 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1473 if (pa->addr.type == PF_ADDR_TABLE) {
1475 pfr_attach_table(ruleset,
1476 pa->addr.v.tblname);
1477 if (pa->addr.p.tbl == NULL)
1481 if (newrule->overload_tblname[0]) {
1482 if ((newrule->overload_tbl = pfr_attach_table(
1483 ruleset, newrule->overload_tblname)) ==
1487 newrule->overload_tbl->pfrkt_flags |=
1491 pf_mv_pool(&V_pf_pabuf, &newrule->rpool.list);
1492 if (((((newrule->action == PF_NAT) ||
1493 (newrule->action == PF_RDR) ||
1494 (newrule->action == PF_BINAT) ||
1495 (newrule->rt > PF_FASTROUTE)) &&
1496 !newrule->anchor)) &&
1497 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1501 pf_free_rule(newrule);
1506 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1507 newrule->evaluations = 0;
1508 newrule->packets[0] = newrule->packets[1] = 0;
1509 newrule->bytes[0] = newrule->bytes[1] = 0;
1511 pf_empty_pool(&V_pf_pabuf);
1513 if (pcr->action == PF_CHANGE_ADD_HEAD)
1514 oldrule = TAILQ_FIRST(
1515 ruleset->rules[rs_num].active.ptr);
1516 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1517 oldrule = TAILQ_LAST(
1518 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1520 oldrule = TAILQ_FIRST(
1521 ruleset->rules[rs_num].active.ptr);
1522 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1523 oldrule = TAILQ_NEXT(oldrule, entries);
1524 if (oldrule == NULL) {
1525 if (newrule != NULL)
1526 pf_free_rule(newrule);
1533 if (pcr->action == PF_CHANGE_REMOVE) {
1534 pf_unlink_rule(ruleset->rules[rs_num].active.ptr,
1536 ruleset->rules[rs_num].active.rcount--;
1538 if (oldrule == NULL)
1540 ruleset->rules[rs_num].active.ptr,
1542 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1543 pcr->action == PF_CHANGE_ADD_BEFORE)
1544 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1547 ruleset->rules[rs_num].active.ptr,
1548 oldrule, newrule, entries);
1549 ruleset->rules[rs_num].active.rcount++;
1553 TAILQ_FOREACH(oldrule,
1554 ruleset->rules[rs_num].active.ptr, entries)
1557 ruleset->rules[rs_num].active.ticket++;
1559 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1560 pf_remove_if_empty_ruleset(ruleset);
1566 DIOCCHANGERULE_error:
1568 if (newrule != NULL)
1569 free(newrule, M_PFRULE);
1571 free(kif, PFI_MTYPE);
1575 case DIOCCLRSTATES: {
1577 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1578 u_int i, killed = 0;
1580 for (i = 0; i <= V_pf_hashmask; i++) {
1581 struct pf_idhash *ih = &V_pf_idhash[i];
1583 relock_DIOCCLRSTATES:
1584 PF_HASHROW_LOCK(ih);
1585 LIST_FOREACH(s, &ih->states, entry)
1586 if (!psk->psk_ifname[0] ||
1587 !strcmp(psk->psk_ifname,
1588 s->kif->pfik_name)) {
1590 * Don't send out individual
1593 s->state_flags |= PFSTATE_NOSYNC;
1594 pf_unlink_state(s, PF_ENTER_LOCKED);
1596 goto relock_DIOCCLRSTATES;
1598 PF_HASHROW_UNLOCK(ih);
1600 psk->psk_killed = killed;
1601 if (pfsync_clear_states_ptr != NULL)
1602 pfsync_clear_states_ptr(V_pf_status.hostid, psk->psk_ifname);
1606 case DIOCKILLSTATES: {
1608 struct pf_state_key *sk;
1609 struct pf_addr *srcaddr, *dstaddr;
1610 u_int16_t srcport, dstport;
1611 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1612 u_int i, killed = 0;
1614 if (psk->psk_pfcmp.id) {
1615 if (psk->psk_pfcmp.creatorid == 0)
1616 psk->psk_pfcmp.creatorid = V_pf_status.hostid;
1617 if ((s = pf_find_state_byid(psk->psk_pfcmp.id,
1618 psk->psk_pfcmp.creatorid))) {
1619 pf_unlink_state(s, PF_ENTER_LOCKED);
1620 psk->psk_killed = 1;
1625 for (i = 0; i <= V_pf_hashmask; i++) {
1626 struct pf_idhash *ih = &V_pf_idhash[i];
1628 relock_DIOCKILLSTATES:
1629 PF_HASHROW_LOCK(ih);
1630 LIST_FOREACH(s, &ih->states, entry) {
1631 sk = s->key[PF_SK_WIRE];
1632 if (s->direction == PF_OUT) {
1633 srcaddr = &sk->addr[1];
1634 dstaddr = &sk->addr[0];
1635 srcport = sk->port[0];
1636 dstport = sk->port[0];
1638 srcaddr = &sk->addr[0];
1639 dstaddr = &sk->addr[1];
1640 srcport = sk->port[0];
1641 dstport = sk->port[0];
1644 if ((!psk->psk_af || sk->af == psk->psk_af)
1645 && (!psk->psk_proto || psk->psk_proto ==
1647 PF_MATCHA(psk->psk_src.neg,
1648 &psk->psk_src.addr.v.a.addr,
1649 &psk->psk_src.addr.v.a.mask,
1651 PF_MATCHA(psk->psk_dst.neg,
1652 &psk->psk_dst.addr.v.a.addr,
1653 &psk->psk_dst.addr.v.a.mask,
1655 (psk->psk_src.port_op == 0 ||
1656 pf_match_port(psk->psk_src.port_op,
1657 psk->psk_src.port[0], psk->psk_src.port[1],
1659 (psk->psk_dst.port_op == 0 ||
1660 pf_match_port(psk->psk_dst.port_op,
1661 psk->psk_dst.port[0], psk->psk_dst.port[1],
1663 (!psk->psk_label[0] ||
1664 (s->rule.ptr->label[0] &&
1665 !strcmp(psk->psk_label,
1666 s->rule.ptr->label))) &&
1667 (!psk->psk_ifname[0] ||
1668 !strcmp(psk->psk_ifname,
1669 s->kif->pfik_name))) {
1670 pf_unlink_state(s, PF_ENTER_LOCKED);
1672 goto relock_DIOCKILLSTATES;
1675 PF_HASHROW_UNLOCK(ih);
1677 psk->psk_killed = killed;
1681 case DIOCADDSTATE: {
1682 struct pfioc_state *ps = (struct pfioc_state *)addr;
1683 struct pfsync_state *sp = &ps->state;
1685 if (sp->timeout >= PFTM_MAX) {
1689 if (pfsync_state_import_ptr != NULL) {
1691 error = pfsync_state_import_ptr(sp, PFSYNC_SI_IOCTL);
1698 case DIOCGETSTATE: {
1699 struct pfioc_state *ps = (struct pfioc_state *)addr;
1702 s = pf_find_state_byid(ps->state.id, ps->state.creatorid);
1708 pfsync_state_export(&ps->state, s);
1713 case DIOCGETSTATES: {
1714 struct pfioc_states *ps = (struct pfioc_states *)addr;
1716 struct pfsync_state *pstore, *p;
1719 if (ps->ps_len == 0) {
1720 nr = uma_zone_get_cur(V_pf_state_z);
1721 ps->ps_len = sizeof(struct pfsync_state) * nr;
1725 p = pstore = malloc(ps->ps_len, M_TEMP, M_WAITOK);
1728 for (i = 0; i <= V_pf_hashmask; i++) {
1729 struct pf_idhash *ih = &V_pf_idhash[i];
1731 PF_HASHROW_LOCK(ih);
1732 LIST_FOREACH(s, &ih->states, entry) {
1734 if (s->timeout == PFTM_UNLINKED)
1737 if ((nr+1) * sizeof(*p) > ps->ps_len) {
1738 PF_HASHROW_UNLOCK(ih);
1739 goto DIOCGETSTATES_full;
1741 pfsync_state_export(p, s);
1745 PF_HASHROW_UNLOCK(ih);
1748 error = copyout(pstore, ps->ps_states,
1749 sizeof(struct pfsync_state) * nr);
1751 free(pstore, M_TEMP);
1754 ps->ps_len = sizeof(struct pfsync_state) * nr;
1755 free(pstore, M_TEMP);
1760 case DIOCGETSTATUS: {
1761 struct pf_status *s = (struct pf_status *)addr;
1763 bcopy(&V_pf_status, s, sizeof(struct pf_status));
1764 pfi_update_status(s->ifname, s);
1769 case DIOCSETSTATUSIF: {
1770 struct pfioc_if *pi = (struct pfioc_if *)addr;
1772 if (pi->ifname[0] == 0) {
1773 bzero(V_pf_status.ifname, IFNAMSIZ);
1777 strlcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ);
1782 case DIOCCLRSTATUS: {
1784 bzero(V_pf_status.counters, sizeof(V_pf_status.counters));
1785 bzero(V_pf_status.fcounters, sizeof(V_pf_status.fcounters));
1786 bzero(V_pf_status.scounters, sizeof(V_pf_status.scounters));
1787 V_pf_status.since = time_second;
1788 if (*V_pf_status.ifname)
1789 pfi_update_status(V_pf_status.ifname, NULL);
1795 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1796 struct pf_state_key *sk;
1797 struct pf_state *state;
1798 struct pf_state_key_cmp key;
1799 int m = 0, direction = pnl->direction;
1802 /* NATLOOK src and dst are reversed, so reverse sidx/didx */
1803 sidx = (direction == PF_IN) ? 1 : 0;
1804 didx = (direction == PF_IN) ? 0 : 1;
1807 PF_AZERO(&pnl->saddr, pnl->af) ||
1808 PF_AZERO(&pnl->daddr, pnl->af) ||
1809 ((pnl->proto == IPPROTO_TCP ||
1810 pnl->proto == IPPROTO_UDP) &&
1811 (!pnl->dport || !pnl->sport)))
1814 bzero(&key, sizeof(key));
1816 key.proto = pnl->proto;
1817 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
1818 key.port[sidx] = pnl->sport;
1819 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
1820 key.port[didx] = pnl->dport;
1822 state = pf_find_state_all(&key, direction, &m);
1825 error = E2BIG; /* more than one state */
1826 else if (state != NULL) {
1827 /* XXXGL: not locked read */
1828 sk = state->key[sidx];
1829 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
1830 pnl->rsport = sk->port[sidx];
1831 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
1832 pnl->rdport = sk->port[didx];
1839 case DIOCSETTIMEOUT: {
1840 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1843 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1849 old = V_pf_default_rule.timeout[pt->timeout];
1850 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
1852 V_pf_default_rule.timeout[pt->timeout] = pt->seconds;
1853 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
1854 wakeup(pf_purge_thread);
1860 case DIOCGETTIMEOUT: {
1861 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1863 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1868 pt->seconds = V_pf_default_rule.timeout[pt->timeout];
1873 case DIOCGETLIMIT: {
1874 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1876 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1881 pl->limit = V_pf_limits[pl->index].limit;
1886 case DIOCSETLIMIT: {
1887 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1891 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1892 V_pf_limits[pl->index].zone == NULL) {
1897 uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit);
1898 old_limit = V_pf_limits[pl->index].limit;
1899 V_pf_limits[pl->index].limit = pl->limit;
1900 pl->limit = old_limit;
1905 case DIOCSETDEBUG: {
1906 u_int32_t *level = (u_int32_t *)addr;
1909 V_pf_status.debug = *level;
1914 case DIOCCLRRULECTRS: {
1915 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
1916 struct pf_ruleset *ruleset = &pf_main_ruleset;
1917 struct pf_rule *rule;
1921 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
1922 rule->evaluations = 0;
1923 rule->packets[0] = rule->packets[1] = 0;
1924 rule->bytes[0] = rule->bytes[1] = 0;
1930 case DIOCGIFSPEED: {
1931 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
1932 struct pf_ifspeed ps;
1935 if (psp->ifname[0] != 0) {
1936 /* Can we completely trust user-land? */
1937 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
1938 ifp = ifunit(ps.ifname);
1940 psp->baudrate = ifp->if_baudrate;
1949 case DIOCSTARTALTQ: {
1950 struct pf_altq *altq;
1953 /* enable all altq interfaces on active list */
1954 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
1955 if (altq->qname[0] == 0 && (altq->local_flags &
1956 PFALTQ_FLAG_IF_REMOVED) == 0) {
1957 error = pf_enable_altq(altq);
1963 V_pf_altq_running = 1;
1965 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
1969 case DIOCSTOPALTQ: {
1970 struct pf_altq *altq;
1973 /* disable 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_disable_altq(altq);
1983 V_pf_altq_running = 0;
1985 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
1990 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
1991 struct pf_altq *altq, *a;
1994 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK);
1995 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
1996 altq->local_flags = 0;
1999 if (pa->ticket != V_ticket_altqs_inactive) {
2001 free(altq, M_PFALTQ);
2007 * if this is for a queue, find the discipline and
2008 * copy the necessary fields
2010 if (altq->qname[0] != 0) {
2011 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2014 free(altq, M_PFALTQ);
2017 altq->altq_disc = NULL;
2018 TAILQ_FOREACH(a, V_pf_altqs_inactive, entries) {
2019 if (strncmp(a->ifname, altq->ifname,
2020 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2021 altq->altq_disc = a->altq_disc;
2027 if ((ifp = ifunit(altq->ifname)) == NULL)
2028 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2030 error = altq_add(altq);
2034 free(altq, M_PFALTQ);
2038 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries);
2039 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2044 case DIOCGETALTQS: {
2045 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2046 struct pf_altq *altq;
2050 TAILQ_FOREACH(altq, V_pf_altqs_active, entries)
2052 pa->ticket = V_ticket_altqs_active;
2058 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2059 struct pf_altq *altq;
2063 if (pa->ticket != V_ticket_altqs_active) {
2069 altq = TAILQ_FIRST(V_pf_altqs_active);
2070 while ((altq != NULL) && (nr < pa->nr)) {
2071 altq = TAILQ_NEXT(altq, entries);
2079 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2084 case DIOCCHANGEALTQ:
2085 /* CHANGEALTQ not supported yet! */
2089 case DIOCGETQSTATS: {
2090 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2091 struct pf_altq *altq;
2096 if (pq->ticket != V_ticket_altqs_active) {
2101 nbytes = pq->nbytes;
2103 altq = TAILQ_FIRST(V_pf_altqs_active);
2104 while ((altq != NULL) && (nr < pq->nr)) {
2105 altq = TAILQ_NEXT(altq, entries);
2114 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2120 error = altq_getqstats(altq, pq->buf, &nbytes);
2122 pq->scheduler = altq->scheduler;
2123 pq->nbytes = nbytes;
2129 case DIOCBEGINADDRS: {
2130 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2133 pf_empty_pool(&V_pf_pabuf);
2134 pp->ticket = ++V_ticket_pabuf;
2140 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2141 struct pf_pooladdr *pa;
2142 struct pfi_kif *kif = NULL;
2145 if (pp->af == AF_INET) {
2146 error = EAFNOSUPPORT;
2151 if (pp->af == AF_INET6) {
2152 error = EAFNOSUPPORT;
2156 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2157 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2158 pp->addr.addr.type != PF_ADDR_TABLE) {
2162 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK);
2163 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2165 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2167 if (pp->ticket != V_ticket_pabuf) {
2170 free(kif, PFI_MTYPE);
2175 if (pa->ifname[0]) {
2176 pa->kif = pfi_kif_attach(kif, pa->ifname);
2177 pfi_kif_ref(pa->kif);
2180 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error =
2181 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) {
2183 pfi_kif_unref(pa->kif);
2188 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries);
2193 case DIOCGETADDRS: {
2194 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2195 struct pf_pool *pool;
2196 struct pf_pooladdr *pa;
2200 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2201 pp->r_num, 0, 1, 0);
2207 TAILQ_FOREACH(pa, &pool->list, entries)
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, 1);
2227 pa = TAILQ_FIRST(&pool->list);
2228 while ((pa != NULL) && (nr < pp->nr)) {
2229 pa = TAILQ_NEXT(pa, entries);
2237 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2238 pf_addr_copyout(&pp->addr.addr);
2243 case DIOCCHANGEADDR: {
2244 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2245 struct pf_pool *pool;
2246 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2247 struct pf_ruleset *ruleset;
2248 struct pfi_kif *kif = NULL;
2250 if (pca->action < PF_CHANGE_ADD_HEAD ||
2251 pca->action > PF_CHANGE_REMOVE) {
2255 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2256 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2257 pca->addr.addr.type != PF_ADDR_TABLE) {
2262 if (pca->action != PF_CHANGE_REMOVE) {
2264 if (pca->af == AF_INET) {
2265 error = EAFNOSUPPORT;
2270 if (pca->af == AF_INET6) {
2271 error = EAFNOSUPPORT;
2275 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK);
2276 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2277 if (newpa->ifname[0])
2278 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2281 #define ERROUT(x) { error = (x); goto DIOCCHANGEADDR_error; }
2283 ruleset = pf_find_ruleset(pca->anchor);
2284 if (ruleset == NULL)
2287 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2288 pca->r_num, pca->r_last, 1, 1);
2292 if (pca->action != PF_CHANGE_REMOVE) {
2293 if (newpa->ifname[0]) {
2294 newpa->kif = pfi_kif_attach(kif, newpa->ifname);
2295 pfi_kif_ref(newpa->kif);
2299 switch (newpa->addr.type) {
2300 case PF_ADDR_DYNIFTL:
2301 error = pfi_dynaddr_setup(&newpa->addr,
2305 newpa->addr.p.tbl = pfr_attach_table(ruleset,
2306 newpa->addr.v.tblname);
2307 if (newpa->addr.p.tbl == NULL)
2313 pfi_kif_unref(newpa->kif);
2315 free(newpa, M_PFRULE);
2320 if (pca->action == PF_CHANGE_ADD_HEAD)
2321 oldpa = TAILQ_FIRST(&pool->list);
2322 else if (pca->action == PF_CHANGE_ADD_TAIL)
2323 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2327 oldpa = TAILQ_FIRST(&pool->list);
2328 while ((oldpa != NULL) && (i < pca->nr)) {
2329 oldpa = TAILQ_NEXT(oldpa, entries);
2332 if (oldpa == NULL) {
2339 if (pca->action == PF_CHANGE_REMOVE) {
2340 TAILQ_REMOVE(&pool->list, oldpa, entries);
2341 switch (oldpa->addr.type) {
2342 case PF_ADDR_DYNIFTL:
2343 pfi_dynaddr_remove(oldpa->addr.p.dyn);
2346 pfr_detach_table(oldpa->addr.p.tbl);
2350 pfi_kif_unref(oldpa->kif);
2351 free(oldpa, M_PFRULE);
2354 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2355 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2356 pca->action == PF_CHANGE_ADD_BEFORE)
2357 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2359 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2363 pool->cur = TAILQ_FIRST(&pool->list);
2364 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
2370 DIOCCHANGEADDR_error:
2373 free(newpa, M_PFRULE);
2375 free(kif, PFI_MTYPE);
2379 case DIOCGETRULESETS: {
2380 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2381 struct pf_ruleset *ruleset;
2382 struct pf_anchor *anchor;
2385 pr->path[sizeof(pr->path) - 1] = 0;
2386 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2392 if (ruleset->anchor == NULL) {
2393 /* XXX kludge for pf_main_ruleset */
2394 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2395 if (anchor->parent == NULL)
2398 RB_FOREACH(anchor, pf_anchor_node,
2399 &ruleset->anchor->children)
2406 case DIOCGETRULESET: {
2407 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2408 struct pf_ruleset *ruleset;
2409 struct pf_anchor *anchor;
2413 pr->path[sizeof(pr->path) - 1] = 0;
2414 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2420 if (ruleset->anchor == NULL) {
2421 /* XXX kludge for pf_main_ruleset */
2422 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2423 if (anchor->parent == NULL && nr++ == pr->nr) {
2424 strlcpy(pr->name, anchor->name,
2429 RB_FOREACH(anchor, pf_anchor_node,
2430 &ruleset->anchor->children)
2431 if (nr++ == pr->nr) {
2432 strlcpy(pr->name, anchor->name,
2443 case DIOCRCLRTABLES: {
2444 struct pfioc_table *io = (struct pfioc_table *)addr;
2446 if (io->pfrio_esize != 0) {
2451 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2452 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2457 case DIOCRADDTABLES: {
2458 struct pfioc_table *io = (struct pfioc_table *)addr;
2459 struct pfr_table *pfrts;
2462 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2466 totlen = io->pfrio_size * sizeof(struct pfr_table);
2467 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2468 error = copyin(io->pfrio_buffer, pfrts, totlen);
2470 free(pfrts, M_TEMP);
2474 error = pfr_add_tables(pfrts, io->pfrio_size,
2475 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2477 free(pfrts, M_TEMP);
2481 case DIOCRDELTABLES: {
2482 struct pfioc_table *io = (struct pfioc_table *)addr;
2483 struct pfr_table *pfrts;
2486 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2490 totlen = io->pfrio_size * sizeof(struct pfr_table);
2491 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2492 error = copyin(io->pfrio_buffer, pfrts, totlen);
2494 free(pfrts, M_TEMP);
2498 error = pfr_del_tables(pfrts, io->pfrio_size,
2499 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2501 free(pfrts, M_TEMP);
2505 case DIOCRGETTABLES: {
2506 struct pfioc_table *io = (struct pfioc_table *)addr;
2507 struct pfr_table *pfrts;
2510 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2514 totlen = io->pfrio_size * sizeof(struct pfr_table);
2515 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2517 error = pfr_get_tables(&io->pfrio_table, pfrts,
2518 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2521 error = copyout(pfrts, io->pfrio_buffer, totlen);
2522 free(pfrts, M_TEMP);
2526 case DIOCRGETTSTATS: {
2527 struct pfioc_table *io = (struct pfioc_table *)addr;
2528 struct pfr_tstats *pfrtstats;
2531 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2535 totlen = io->pfrio_size * sizeof(struct pfr_tstats);
2536 pfrtstats = malloc(totlen, M_TEMP, M_WAITOK);
2538 error = pfr_get_tstats(&io->pfrio_table, pfrtstats,
2539 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2542 error = copyout(pfrtstats, io->pfrio_buffer, totlen);
2543 free(pfrtstats, M_TEMP);
2547 case DIOCRCLRTSTATS: {
2548 struct pfioc_table *io = (struct pfioc_table *)addr;
2549 struct pfr_table *pfrts;
2552 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2556 totlen = io->pfrio_size * sizeof(struct pfr_table);
2557 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2558 error = copyin(io->pfrio_buffer, pfrts, totlen);
2560 free(pfrts, M_TEMP);
2564 error = pfr_clr_tstats(pfrts, io->pfrio_size,
2565 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2567 free(pfrts, M_TEMP);
2571 case DIOCRSETTFLAGS: {
2572 struct pfioc_table *io = (struct pfioc_table *)addr;
2573 struct pfr_table *pfrts;
2576 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2580 totlen = io->pfrio_size * sizeof(struct pfr_table);
2581 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2582 error = copyin(io->pfrio_buffer, pfrts, totlen);
2584 free(pfrts, M_TEMP);
2588 error = pfr_set_tflags(pfrts, io->pfrio_size,
2589 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2590 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2592 free(pfrts, M_TEMP);
2596 case DIOCRCLRADDRS: {
2597 struct pfioc_table *io = (struct pfioc_table *)addr;
2599 if (io->pfrio_esize != 0) {
2604 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2605 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2610 case DIOCRADDADDRS: {
2611 struct pfioc_table *io = (struct pfioc_table *)addr;
2612 struct pfr_addr *pfras;
2615 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2619 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2620 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2621 error = copyin(io->pfrio_buffer, pfras, totlen);
2623 free(pfras, M_TEMP);
2627 error = pfr_add_addrs(&io->pfrio_table, pfras,
2628 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2629 PFR_FLAG_USERIOCTL);
2631 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2632 error = copyout(pfras, io->pfrio_buffer, totlen);
2633 free(pfras, M_TEMP);
2637 case DIOCRDELADDRS: {
2638 struct pfioc_table *io = (struct pfioc_table *)addr;
2639 struct pfr_addr *pfras;
2642 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2646 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2647 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2648 error = copyin(io->pfrio_buffer, pfras, totlen);
2650 free(pfras, M_TEMP);
2654 error = pfr_del_addrs(&io->pfrio_table, pfras,
2655 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2656 PFR_FLAG_USERIOCTL);
2658 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2659 error = copyout(pfras, io->pfrio_buffer, totlen);
2660 free(pfras, M_TEMP);
2664 case DIOCRSETADDRS: {
2665 struct pfioc_table *io = (struct pfioc_table *)addr;
2666 struct pfr_addr *pfras;
2669 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2673 totlen = (io->pfrio_size + io->pfrio_size2) *
2674 sizeof(struct pfr_addr);
2675 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2676 error = copyin(io->pfrio_buffer, pfras, totlen);
2678 free(pfras, M_TEMP);
2682 error = pfr_set_addrs(&io->pfrio_table, pfras,
2683 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2684 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2685 PFR_FLAG_USERIOCTL, 0);
2687 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2688 error = copyout(pfras, io->pfrio_buffer, totlen);
2689 free(pfras, M_TEMP);
2693 case DIOCRGETADDRS: {
2694 struct pfioc_table *io = (struct pfioc_table *)addr;
2695 struct pfr_addr *pfras;
2698 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2702 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2703 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2705 error = pfr_get_addrs(&io->pfrio_table, pfras,
2706 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2709 error = copyout(pfras, io->pfrio_buffer, totlen);
2710 free(pfras, M_TEMP);
2714 case DIOCRGETASTATS: {
2715 struct pfioc_table *io = (struct pfioc_table *)addr;
2716 struct pfr_astats *pfrastats;
2719 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2723 totlen = io->pfrio_size * sizeof(struct pfr_astats);
2724 pfrastats = malloc(totlen, M_TEMP, M_WAITOK);
2726 error = pfr_get_astats(&io->pfrio_table, pfrastats,
2727 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2730 error = copyout(pfrastats, io->pfrio_buffer, totlen);
2731 free(pfrastats, M_TEMP);
2735 case DIOCRCLRASTATS: {
2736 struct pfioc_table *io = (struct pfioc_table *)addr;
2737 struct pfr_addr *pfras;
2740 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2744 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2745 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2746 error = copyin(io->pfrio_buffer, pfras, totlen);
2748 free(pfras, M_TEMP);
2752 error = pfr_clr_astats(&io->pfrio_table, pfras,
2753 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2754 PFR_FLAG_USERIOCTL);
2756 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2757 error = copyout(pfras, io->pfrio_buffer, totlen);
2758 free(pfras, M_TEMP);
2762 case DIOCRTSTADDRS: {
2763 struct pfioc_table *io = (struct pfioc_table *)addr;
2764 struct pfr_addr *pfras;
2767 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2771 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2772 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2773 error = copyin(io->pfrio_buffer, pfras, totlen);
2775 free(pfras, M_TEMP);
2779 error = pfr_tst_addrs(&io->pfrio_table, pfras,
2780 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2781 PFR_FLAG_USERIOCTL);
2784 error = copyout(pfras, io->pfrio_buffer, totlen);
2785 free(pfras, M_TEMP);
2789 case DIOCRINADEFINE: {
2790 struct pfioc_table *io = (struct pfioc_table *)addr;
2791 struct pfr_addr *pfras;
2794 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2798 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2799 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2800 error = copyin(io->pfrio_buffer, pfras, totlen);
2802 free(pfras, M_TEMP);
2806 error = pfr_ina_define(&io->pfrio_table, pfras,
2807 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2808 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2810 free(pfras, M_TEMP);
2815 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2817 error = pf_osfp_add(io);
2823 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2825 error = pf_osfp_get(io);
2831 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2832 struct pfioc_trans_e *ioes, *ioe;
2836 if (io->esize != sizeof(*ioe)) {
2840 totlen = sizeof(struct pfioc_trans_e) * io->size;
2841 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2842 error = copyin(io->array, ioes, totlen);
2848 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2849 switch (ioe->rs_num) {
2851 case PF_RULESET_ALTQ:
2852 if (ioe->anchor[0]) {
2858 if ((error = pf_begin_altq(&ioe->ticket))) {
2865 case PF_RULESET_TABLE:
2867 struct pfr_table table;
2869 bzero(&table, sizeof(table));
2870 strlcpy(table.pfrt_anchor, ioe->anchor,
2871 sizeof(table.pfrt_anchor));
2872 if ((error = pfr_ina_begin(&table,
2873 &ioe->ticket, NULL, 0))) {
2881 if ((error = pf_begin_rules(&ioe->ticket,
2882 ioe->rs_num, ioe->anchor))) {
2891 error = copyout(ioes, io->array, totlen);
2896 case DIOCXROLLBACK: {
2897 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2898 struct pfioc_trans_e *ioe, *ioes;
2902 if (io->esize != sizeof(*ioe)) {
2906 totlen = sizeof(struct pfioc_trans_e) * io->size;
2907 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2908 error = copyin(io->array, ioes, totlen);
2914 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2915 switch (ioe->rs_num) {
2917 case PF_RULESET_ALTQ:
2918 if (ioe->anchor[0]) {
2924 if ((error = pf_rollback_altq(ioe->ticket))) {
2927 goto fail; /* really bad */
2931 case PF_RULESET_TABLE:
2933 struct pfr_table table;
2935 bzero(&table, sizeof(table));
2936 strlcpy(table.pfrt_anchor, ioe->anchor,
2937 sizeof(table.pfrt_anchor));
2938 if ((error = pfr_ina_rollback(&table,
2939 ioe->ticket, NULL, 0))) {
2942 goto fail; /* really bad */
2947 if ((error = pf_rollback_rules(ioe->ticket,
2948 ioe->rs_num, ioe->anchor))) {
2951 goto fail; /* really bad */
2962 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2963 struct pfioc_trans_e *ioe, *ioes;
2964 struct pf_ruleset *rs;
2968 if (io->esize != sizeof(*ioe)) {
2972 totlen = sizeof(struct pfioc_trans_e) * io->size;
2973 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2974 error = copyin(io->array, ioes, totlen);
2980 /* First makes sure everything will succeed. */
2981 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2982 switch (ioe->rs_num) {
2984 case PF_RULESET_ALTQ:
2985 if (ioe->anchor[0]) {
2991 if (!V_altqs_inactive_open || ioe->ticket !=
2992 V_ticket_altqs_inactive) {
3000 case PF_RULESET_TABLE:
3001 rs = pf_find_ruleset(ioe->anchor);
3002 if (rs == NULL || !rs->topen || ioe->ticket !=
3011 if (ioe->rs_num < 0 || ioe->rs_num >=
3018 rs = pf_find_ruleset(ioe->anchor);
3020 !rs->rules[ioe->rs_num].inactive.open ||
3021 rs->rules[ioe->rs_num].inactive.ticket !=
3031 /* Now do the commit - no errors should happen here. */
3032 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3033 switch (ioe->rs_num) {
3035 case PF_RULESET_ALTQ:
3036 if ((error = pf_commit_altq(ioe->ticket))) {
3039 goto fail; /* really bad */
3043 case PF_RULESET_TABLE:
3045 struct pfr_table table;
3047 bzero(&table, sizeof(table));
3048 strlcpy(table.pfrt_anchor, ioe->anchor,
3049 sizeof(table.pfrt_anchor));
3050 if ((error = pfr_ina_commit(&table,
3051 ioe->ticket, NULL, NULL, 0))) {
3054 goto fail; /* really bad */
3059 if ((error = pf_commit_rules(ioe->ticket,
3060 ioe->rs_num, ioe->anchor))) {
3063 goto fail; /* really bad */
3073 case DIOCGETSRCNODES: {
3074 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3075 struct pf_srchash *sh;
3076 struct pf_src_node *n, *p, *pstore;
3079 if (psn->psn_len == 0) {
3080 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3082 PF_HASHROW_LOCK(sh);
3083 LIST_FOREACH(n, &sh->nodes, entry)
3085 PF_HASHROW_UNLOCK(sh);
3087 psn->psn_len = sizeof(struct pf_src_node) * nr;
3091 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK);
3092 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3094 PF_HASHROW_LOCK(sh);
3095 LIST_FOREACH(n, &sh->nodes, entry) {
3096 int secs = time_uptime, diff;
3098 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3101 bcopy(n, p, sizeof(struct pf_src_node));
3102 if (n->rule.ptr != NULL)
3103 p->rule.nr = n->rule.ptr->nr;
3104 p->creation = secs - p->creation;
3105 if (p->expire > secs)
3110 /* Adjust the connection rate estimate. */
3111 diff = secs - n->conn_rate.last;
3112 if (diff >= n->conn_rate.seconds)
3113 p->conn_rate.count = 0;
3115 p->conn_rate.count -=
3116 n->conn_rate.count * diff /
3117 n->conn_rate.seconds;
3121 PF_HASHROW_UNLOCK(sh);
3123 error = copyout(pstore, psn->psn_src_nodes,
3124 sizeof(struct pf_src_node) * nr);
3126 free(pstore, M_TEMP);
3129 psn->psn_len = sizeof(struct pf_src_node) * nr;
3130 free(pstore, M_TEMP);
3134 case DIOCCLRSRCNODES: {
3136 pf_clear_srcnodes(NULL);
3137 pf_purge_expired_src_nodes();
3138 V_pf_status.src_nodes = 0;
3142 case DIOCKILLSRCNODES: {
3143 struct pfioc_src_node_kill *psnk =
3144 (struct pfioc_src_node_kill *)addr;
3145 struct pf_srchash *sh;
3146 struct pf_src_node *sn;
3147 u_int i, killed = 0;
3149 for (i = 0, sh = V_pf_srchash; i < V_pf_srchashmask;
3152 * XXXGL: we don't ever acquire sources hash lock
3153 * but if we ever do, the below call to pf_clear_srcnodes()
3154 * would lead to a LOR.
3156 PF_HASHROW_LOCK(sh);
3157 LIST_FOREACH(sn, &sh->nodes, entry)
3158 if (PF_MATCHA(psnk->psnk_src.neg,
3159 &psnk->psnk_src.addr.v.a.addr,
3160 &psnk->psnk_src.addr.v.a.mask,
3161 &sn->addr, sn->af) &&
3162 PF_MATCHA(psnk->psnk_dst.neg,
3163 &psnk->psnk_dst.addr.v.a.addr,
3164 &psnk->psnk_dst.addr.v.a.mask,
3165 &sn->raddr, sn->af)) {
3166 /* Handle state to src_node linkage */
3167 if (sn->states != 0)
3168 pf_clear_srcnodes(sn);
3172 PF_HASHROW_UNLOCK(sh);
3176 pf_purge_expired_src_nodes();
3178 psnk->psnk_killed = killed;
3182 case DIOCSETHOSTID: {
3183 u_int32_t *hostid = (u_int32_t *)addr;
3187 V_pf_status.hostid = arc4random();
3189 V_pf_status.hostid = *hostid;
3200 case DIOCIGETIFACES: {
3201 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3202 struct pfi_kif *ifstore;
3205 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3210 bufsiz = io->pfiio_size * sizeof(struct pfi_kif);
3211 ifstore = malloc(bufsiz, M_TEMP, M_WAITOK);
3213 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size);
3215 error = copyout(ifstore, io->pfiio_buffer, bufsiz);
3216 free(ifstore, M_TEMP);
3220 case DIOCSETIFFLAG: {
3221 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3224 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3229 case DIOCCLRIFFLAG: {
3230 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3233 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3249 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
3251 bzero(sp, sizeof(struct pfsync_state));
3253 /* copy from state key */
3254 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
3255 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
3256 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
3257 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
3258 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
3259 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
3260 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
3261 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
3262 sp->proto = st->key[PF_SK_WIRE]->proto;
3263 sp->af = st->key[PF_SK_WIRE]->af;
3265 /* copy from state */
3266 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
3267 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
3268 sp->creation = htonl(time_uptime - st->creation);
3269 sp->expire = pf_state_expires(st);
3270 if (sp->expire <= time_uptime)
3271 sp->expire = htonl(0);
3273 sp->expire = htonl(sp->expire - time_uptime);
3275 sp->direction = st->direction;
3277 sp->timeout = st->timeout;
3278 sp->state_flags = st->state_flags;
3280 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
3281 if (st->nat_src_node)
3282 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
3285 sp->creatorid = st->creatorid;
3286 pf_state_peer_hton(&st->src, &sp->src);
3287 pf_state_peer_hton(&st->dst, &sp->dst);
3289 if (st->rule.ptr == NULL)
3290 sp->rule = htonl(-1);
3292 sp->rule = htonl(st->rule.ptr->nr);
3293 if (st->anchor.ptr == NULL)
3294 sp->anchor = htonl(-1);
3296 sp->anchor = htonl(st->anchor.ptr->nr);
3297 if (st->nat_rule.ptr == NULL)
3298 sp->nat_rule = htonl(-1);
3300 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
3302 pf_state_counter_hton(st->packets[0], sp->packets[0]);
3303 pf_state_counter_hton(st->packets[1], sp->packets[1]);
3304 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
3305 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
3310 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
3312 struct pfr_ktable *kt;
3314 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type));
3317 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
3318 kt = kt->pfrkt_root;
3320 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
3325 * XXX - Check for version missmatch!!!
3328 pf_clear_states(void)
3333 for (i = 0; i <= V_pf_hashmask; i++) {
3334 struct pf_idhash *ih = &V_pf_idhash[i];
3336 PF_HASHROW_LOCK(ih);
3337 LIST_FOREACH(s, &ih->states, entry) {
3338 s->timeout = PFTM_PURGE;
3339 /* Don't send out individual delete messages. */
3340 s->sync_state = PFSTATE_NOSYNC;
3341 pf_unlink_state(s, PF_ENTER_LOCKED);
3344 PF_HASHROW_UNLOCK(ih);
3349 pf_clear_tables(void)
3351 struct pfioc_table io;
3354 bzero(&io, sizeof(io));
3356 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3363 pf_clear_srcnodes(struct pf_src_node *n)
3368 for (i = 0; i <= V_pf_hashmask; i++) {
3369 struct pf_idhash *ih = &V_pf_idhash[i];
3371 PF_HASHROW_LOCK(ih);
3372 LIST_FOREACH(s, &ih->states, entry) {
3373 if (n == NULL || n == s->src_node)
3375 if (n == NULL || n == s->nat_src_node)
3376 s->nat_src_node = NULL;
3378 PF_HASHROW_UNLOCK(ih);
3382 struct pf_srchash *sh;
3384 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3386 PF_HASHROW_LOCK(sh);
3387 LIST_FOREACH(n, &sh->nodes, entry) {
3391 PF_HASHROW_UNLOCK(sh);
3394 /* XXX: hash slot should already be locked here. */
3400 * XXX - Check for version missmatch!!!
3404 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3413 V_pf_status.running = 0;
3415 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3417 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3420 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3422 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3423 break; /* XXX: rollback? */
3425 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3427 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3428 break; /* XXX: rollback? */
3430 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3432 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3433 break; /* XXX: rollback? */
3435 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3437 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3438 break; /* XXX: rollback? */
3441 /* XXX: these should always succeed here */
3442 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3443 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3444 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3445 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3446 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3448 if ((error = pf_clear_tables()) != 0)
3452 if ((error = pf_begin_altq(&t[0])) != 0) {
3453 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3456 pf_commit_altq(t[0]);
3461 pf_clear_srcnodes(NULL);
3463 /* status does not use malloced mem so no need to cleanup */
3464 /* fingerprints and interfaces have thier own cleanup code */
3472 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3477 chk = pf_test(PF_IN, ifp, m, inp);
3487 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3492 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3493 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3494 in_delayed_cksum(*m);
3495 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3498 chk = pf_test(PF_OUT, ifp, m, inp);
3510 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3516 * In case of loopback traffic IPv6 uses the real interface in
3517 * order to support scoped addresses. In order to support stateful
3518 * filtering we have change this to lo0 as it is the case in IPv4.
3520 CURVNET_SET(ifp->if_vnet);
3521 chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? V_loif : ifp, m, inp);
3531 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3536 /* We need a proper CSUM before we start (s. OpenBSD ip_output) */
3537 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3539 /* XXX-BZ copy&paste error from r126261? */
3540 in_delayed_cksum(*m);
3542 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3544 CURVNET_SET(ifp->if_vnet);
3545 chk = pf_test6(PF_OUT, ifp, m, inp);
3559 struct pfil_head *pfh_inet;
3562 struct pfil_head *pfh_inet6;
3565 if (V_pf_pfil_hooked)
3569 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3570 if (pfh_inet == NULL)
3571 return (ESRCH); /* XXX */
3572 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3573 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3576 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3577 if (pfh_inet6 == NULL) {
3579 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3581 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3584 return (ESRCH); /* XXX */
3586 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3587 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3590 V_pf_pfil_hooked = 1;
3598 struct pfil_head *pfh_inet;
3601 struct pfil_head *pfh_inet6;
3604 if (V_pf_pfil_hooked == 0)
3608 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3609 if (pfh_inet == NULL)
3610 return (ESRCH); /* XXX */
3611 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3613 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3617 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3618 if (pfh_inet6 == NULL)
3619 return (ESRCH); /* XXX */
3620 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3622 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3626 V_pf_pfil_hooked = 0;
3635 VNET_ITERATOR_DECL(vnet_iter);
3638 VNET_FOREACH(vnet_iter) {
3639 CURVNET_SET(vnet_iter);
3640 V_pf_pfil_hooked = 0;
3641 V_pf_end_threads = 0;
3642 TAILQ_INIT(&V_pf_tags);
3643 TAILQ_INIT(&V_pf_qids);
3646 VNET_LIST_RUNLOCK();
3648 rw_init(&pf_rules_lock, "pf rulesets");
3650 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3651 if ((error = pfattach()) != 0)
3663 V_pf_status.running = 0;
3665 swi_remove(V_pf_swi_cookie);
3666 error = dehook_pf();
3669 * Should not happen!
3670 * XXX Due to error code ESRCH, kldunload will show
3671 * a message like 'No such process'.
3673 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3678 V_pf_end_threads = 1;
3679 while (V_pf_end_threads < 2) {
3680 wakeup_one(pf_purge_thread);
3681 rw_sleep(pf_purge_thread, &pf_rules_lock, 0, "pftmo", 0);
3683 pf_normalize_cleanup();
3689 destroy_dev(pf_dev);
3690 rw_destroy(&pf_rules_lock);
3696 pf_modevent(module_t mod, int type, void *data)
3706 * Module should not be unloaded due to race conditions.
3711 error = pf_unload();
3721 static moduledata_t pf_mod = {
3727 DECLARE_MODULE(pf, pf_mod, SI_SUB_PSEUDO, SI_ORDER_FIRST);
3728 MODULE_VERSION(pf, PF_MODVER);