1 /* $OpenBSD: pf_ioctl.c,v 1.182 2007/06/24 11:17:13 mcbride Exp $ */
4 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
6 * Copyright (c) 2001 Daniel Hartmeier
7 * Copyright (c) 2002,2003 Henning Brauer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Effort sponsored in part by the Defense Advanced Research Projects
35 * Agency (DARPA) and Air Force Research Laboratory, Air Force
36 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
41 #include "opt_inet6.h"
42 #include "use_pfsync.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/device.h>
49 #include <sys/filio.h>
50 #include <sys/fcntl.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/kernel.h>
54 #include <sys/kthread.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
59 #include <vm/vm_zone.h>
62 #include <sys/thread2.h>
65 #include <net/if_types.h>
66 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/ip_icmp.h>
75 #include <net/pf/pfvar.h>
77 #include <net/pf/pfvar.h>
80 #include <net/pf/if_pfsync.h>
81 #endif /* NPFSYNC > 0 */
84 #include <net/if_pflog.h>
85 #endif /* NPFLOG > 0 */
88 #include <netinet/ip6.h>
89 #include <netinet/in_pcb.h>
93 #include <net/altq/altq.h>
96 #include <machine/limits.h>
98 #include <sys/mutex.h>
100 u_int rt_numfibs = RT_NUMFIBS;
102 void init_zone_var(void);
103 void cleanup_pf_zone(void);
105 struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
106 u_int8_t, u_int8_t, u_int8_t);
108 void pf_mv_pool(struct pf_palist *, struct pf_palist *);
109 void pf_empty_pool(struct pf_palist *);
111 int pf_begin_altq(u_int32_t *);
112 int pf_rollback_altq(u_int32_t);
113 int pf_commit_altq(u_int32_t);
114 int pf_enable_altq(struct pf_altq *);
115 int pf_disable_altq(struct pf_altq *);
117 int pf_begin_rules(u_int32_t *, int, const char *);
118 int pf_rollback_rules(u_int32_t, int, char *);
119 int pf_setup_pfsync_matching(struct pf_ruleset *);
120 void pf_hash_rule(MD5_CTX *, struct pf_rule *);
121 void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
122 int pf_commit_rules(u_int32_t, int, char *);
123 void pf_state_export(struct pfsync_state *,
124 struct pf_state_key *, struct pf_state *);
125 void pf_state_import(struct pfsync_state *,
126 struct pf_state_key *, struct pf_state *);
128 struct pf_rule pf_default_rule;
129 struct lock pf_consistency_lock;
131 static int pf_altq_running;
134 #define TAGID_MAX 50000
135 TAILQ_HEAD(pf_tags, pf_tagname) pf_tags = TAILQ_HEAD_INITIALIZER(pf_tags),
136 pf_qids = TAILQ_HEAD_INITIALIZER(pf_qids);
138 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
139 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
141 u_int16_t tagname2tag(struct pf_tags *, char *);
142 void tag2tagname(struct pf_tags *, u_int16_t, char *);
143 void tag_unref(struct pf_tags *, u_int16_t);
144 int pf_rtlabel_add(struct pf_addr_wrap *);
145 void pf_rtlabel_remove(struct pf_addr_wrap *);
146 void pf_rtlabel_copyout(struct pf_addr_wrap *);
148 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) kprintf x
150 static cdev_t pf_dev;
153 * XXX - These are new and need to be checked when moveing to a new version
155 static void pf_clear_states(void);
156 static int pf_clear_tables(void);
157 static void pf_clear_srcnodes(void);
159 * XXX - These are new and need to be checked when moveing to a new version
163 * Wrapper functions for pfil(9) hooks
165 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
167 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
170 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
172 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
176 static int hook_pf(void);
177 static int dehook_pf(void);
178 static int shutdown_pf(void);
179 static int pf_load(void);
180 static int pf_unload(void);
186 static struct dev_ops pf_ops = { /* XXX convert to port model */
193 static volatile int pf_pfil_hooked = 0;
194 int pf_end_threads = 0;
195 struct lock pf_mod_lck;
197 int debug_pfugidhack = 0;
198 SYSCTL_INT(_debug, OID_AUTO, pfugidhack, CTLFLAG_RW, &debug_pfugidhack, 0,
199 "Enable/disable pf user/group rules mpsafe hack");
204 pf_src_tree_pl = pf_rule_pl = NULL;
205 pf_state_pl = pf_altq_pl = pf_pooladdr_pl = NULL;
206 pf_frent_pl = pf_frag_pl = pf_cache_pl = pf_cent_pl = NULL;
207 pf_state_scrub_pl = NULL;
208 pfr_ktable_pl = pfr_kentry_pl = NULL;
212 cleanup_pf_zone(void)
214 ZONE_DESTROY(pf_src_tree_pl);
215 ZONE_DESTROY(pf_rule_pl);
216 ZONE_DESTROY(pf_state_pl);
217 ZONE_DESTROY(pf_altq_pl);
218 ZONE_DESTROY(pf_pooladdr_pl);
219 ZONE_DESTROY(pf_frent_pl);
220 ZONE_DESTROY(pf_frag_pl);
221 ZONE_DESTROY(pf_cache_pl);
222 ZONE_DESTROY(pf_cent_pl);
223 ZONE_DESTROY(pfr_ktable_pl);
224 ZONE_DESTROY(pfr_kentry_pl);
225 ZONE_DESTROY(pfr_kentry_pl2);
226 ZONE_DESTROY(pf_state_scrub_pl);
227 ZONE_DESTROY(pfi_addr_pl);
233 u_int32_t *my_timeout = pf_default_rule.timeout;
237 ZONE_CREATE(pf_src_tree_pl,struct pf_src_node, "pfsrctrpl");
238 ZONE_CREATE(pf_rule_pl, struct pf_rule, "pfrulepl");
239 ZONE_CREATE(pf_state_pl, struct pf_state, "pfstatepl");
240 ZONE_CREATE(pf_state_key_pl, struct pf_state_key, "pfstatekeypl");
241 ZONE_CREATE(pf_altq_pl, struct pf_altq, "pfaltqpl");
242 ZONE_CREATE(pf_pooladdr_pl,struct pf_pooladdr, "pfpooladdrpl");
243 ZONE_CREATE(pfr_ktable_pl, struct pfr_ktable, "pfrktable");
244 ZONE_CREATE(pfr_kentry_pl, struct pfr_kentry, "pfrkentry");
245 ZONE_CREATE(pfr_kentry_pl2, struct pfr_kentry, "pfrkentry2");
246 ZONE_CREATE(pf_frent_pl, struct pf_frent, "pffrent");
247 ZONE_CREATE(pf_frag_pl, struct pf_fragment, "pffrag");
248 ZONE_CREATE(pf_cache_pl, struct pf_fragment, "pffrcache");
249 ZONE_CREATE(pf_cent_pl, struct pf_frcache, "pffrcent");
250 ZONE_CREATE(pf_state_scrub_pl, struct pf_state_scrub,
252 ZONE_CREATE(pfi_addr_pl, struct pfi_dynaddr, "pfiaddrpl");
261 error = pf_osfp_initialize();
268 pf_pool_limits[PF_LIMIT_STATES].pp = pf_state_pl;
269 pf_pool_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
270 pf_pool_limits[PF_LIMIT_FRAGS].pp = pf_frent_pl;
271 pf_pool_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
272 /* XXX uma_zone_set_max(pf_pool_limits[PF_LIMIT_STATES].pp,
273 pf_pool_limits[PF_LIMIT_STATES].limit);
275 if (ctob(physmem) <= 100*1024*1024)
276 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit =
277 PFR_KENTRY_HIWAT_SMALL;
278 RB_INIT(&tree_src_tracking);
279 RB_INIT(&pf_anchors);
280 pf_init_ruleset(&pf_main_ruleset);
281 TAILQ_INIT(&pf_altqs[0]);
282 TAILQ_INIT(&pf_altqs[1]);
283 TAILQ_INIT(&pf_pabuf);
284 pf_altqs_active = &pf_altqs[0];
285 pf_altqs_inactive = &pf_altqs[1];
286 TAILQ_INIT(&state_list);
288 /* default rule should never be garbage collected */
289 pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
290 pf_default_rule.action = PF_PASS;
291 pf_default_rule.nr = (uint32_t)(-1);
292 pf_default_rule.rtableid = -1;
294 /* initialize default timeouts */
295 my_timeout[PFTM_TCP_FIRST_PACKET] = 120; /* First TCP packet */
296 my_timeout[PFTM_TCP_OPENING] = 30; /* No response yet */
297 my_timeout[PFTM_TCP_ESTABLISHED] = 24*60*60; /* Established */
298 my_timeout[PFTM_TCP_CLOSING] = 15 * 60; /* Half closed */
299 my_timeout[PFTM_TCP_FIN_WAIT] = 45; /* Got both FINs */
300 my_timeout[PFTM_TCP_CLOSED] = 90; /* Got a RST */
301 my_timeout[PFTM_UDP_FIRST_PACKET] = 60; /* First UDP packet */
302 my_timeout[PFTM_UDP_SINGLE] = 30; /* Unidirectional */
303 my_timeout[PFTM_UDP_MULTIPLE] = 60; /* Bidirectional */
304 my_timeout[PFTM_ICMP_FIRST_PACKET] = 20; /* First ICMP packet */
305 my_timeout[PFTM_ICMP_ERROR_REPLY] = 10; /* Got error response */
306 my_timeout[PFTM_OTHER_FIRST_PACKET] = 60; /* First packet */
307 my_timeout[PFTM_OTHER_SINGLE] = 30; /* Unidirectional */
308 my_timeout[PFTM_OTHER_MULTIPLE] = 60; /* Bidirectional */
309 my_timeout[PFTM_FRAG] = 30; /* Fragment expire */
310 my_timeout[PFTM_INTERVAL] = 10; /* Expire interval */
311 my_timeout[PFTM_SRC_NODE] = 0; /* Source Tracking */
312 my_timeout[PFTM_TS_DIFF] = 30; /* Allowed TS diff */
313 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
314 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
317 bzero(&pf_status, sizeof(pf_status));
318 pf_status.debug = PF_DEBUG_URGENT;
320 /* XXX do our best to avoid a conflict */
321 pf_status.hostid = karc4random();
323 if (kthread_create(pf_purge_thread, NULL, NULL, "pfpurge"))
324 panic("pfpurge thread");
330 pfopen(struct dev_open_args *ap)
332 lwkt_gettoken(&pf_token);
333 cdev_t dev = ap->a_head.a_dev;
334 if (minor(dev) >= 1) {
335 lwkt_reltoken(&pf_token);
338 lwkt_reltoken(&pf_token);
343 pfclose(struct dev_close_args *ap)
345 lwkt_gettoken(&pf_token);
346 cdev_t dev = ap->a_head.a_dev;
347 if (minor(dev) >= 1) {
348 lwkt_reltoken(&pf_token);
351 lwkt_reltoken(&pf_token);
356 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
357 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
358 u_int8_t check_ticket)
360 struct pf_ruleset *ruleset;
361 struct pf_rule *rule;
364 ruleset = pf_find_ruleset(anchor);
367 rs_num = pf_get_ruleset_number(rule_action);
368 if (rs_num >= PF_RULESET_MAX)
371 if (check_ticket && ticket !=
372 ruleset->rules[rs_num].active.ticket)
375 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
378 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
380 if (check_ticket && ticket !=
381 ruleset->rules[rs_num].inactive.ticket)
384 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
387 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
390 while ((rule != NULL) && (rule->nr != rule_number))
391 rule = TAILQ_NEXT(rule, entries);
396 return (&rule->rpool);
400 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
402 struct pf_pooladdr *mv_pool_pa;
404 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
405 TAILQ_REMOVE(poola, mv_pool_pa, entries);
406 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
411 pf_empty_pool(struct pf_palist *poola)
413 struct pf_pooladdr *empty_pool_pa;
415 while ((empty_pool_pa = TAILQ_FIRST(poola)) != NULL) {
416 pfi_dynaddr_remove(&empty_pool_pa->addr);
417 pf_tbladdr_remove(&empty_pool_pa->addr);
418 pfi_kif_unref(empty_pool_pa->kif, PFI_KIF_REF_RULE);
419 TAILQ_REMOVE(poola, empty_pool_pa, entries);
420 pool_put(&pf_pooladdr_pl, empty_pool_pa);
425 pf_rm_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
427 if (rulequeue != NULL) {
428 if (rule->states <= 0) {
430 * XXX - we need to remove the table *before* detaching
431 * the rule to make sure the table code does not delete
432 * the anchor under our feet.
434 pf_tbladdr_remove(&rule->src.addr);
435 pf_tbladdr_remove(&rule->dst.addr);
436 if (rule->overload_tbl)
437 pfr_detach_table(rule->overload_tbl);
439 TAILQ_REMOVE(rulequeue, rule, entries);
440 rule->entries.tqe_prev = NULL;
444 if (rule->states > 0 || rule->src_nodes > 0 ||
445 rule->entries.tqe_prev != NULL)
447 pf_tag_unref(rule->tag);
448 pf_tag_unref(rule->match_tag);
450 if (rule->pqid != rule->qid)
451 pf_qid_unref(rule->pqid);
452 pf_qid_unref(rule->qid);
454 pf_rtlabel_remove(&rule->src.addr);
455 pf_rtlabel_remove(&rule->dst.addr);
456 pfi_dynaddr_remove(&rule->src.addr);
457 pfi_dynaddr_remove(&rule->dst.addr);
458 if (rulequeue == NULL) {
459 pf_tbladdr_remove(&rule->src.addr);
460 pf_tbladdr_remove(&rule->dst.addr);
461 if (rule->overload_tbl)
462 pfr_detach_table(rule->overload_tbl);
464 pfi_kif_unref(rule->kif, PFI_KIF_REF_RULE);
465 pf_anchor_remove(rule);
466 pf_empty_pool(&rule->rpool.list);
467 pool_put(&pf_rule_pl, rule);
471 tagname2tag(struct pf_tags *head, char *tagname)
473 struct pf_tagname *tag, *p = NULL;
474 u_int16_t new_tagid = 1;
476 TAILQ_FOREACH(tag, head, entries)
477 if (strcmp(tagname, tag->name) == 0) {
483 * to avoid fragmentation, we do a linear search from the beginning
484 * and take the first free slot we find. if there is none or the list
485 * is empty, append a new entry at the end.
489 if (!TAILQ_EMPTY(head))
490 for (p = TAILQ_FIRST(head); p != NULL &&
491 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
492 new_tagid = p->tag + 1;
494 if (new_tagid > TAGID_MAX)
497 /* allocate and fill new struct pf_tagname */
498 tag = kmalloc(sizeof(struct pf_tagname), M_TEMP, M_WAITOK);
501 bzero(tag, sizeof(struct pf_tagname));
502 strlcpy(tag->name, tagname, sizeof(tag->name));
503 tag->tag = new_tagid;
506 if (p != NULL) /* insert new entry before p */
507 TAILQ_INSERT_BEFORE(p, tag, entries);
508 else /* either list empty or no free slot in between */
509 TAILQ_INSERT_TAIL(head, tag, entries);
515 tag2tagname(struct pf_tags *head, u_int16_t tagid, char *p)
517 struct pf_tagname *tag;
519 TAILQ_FOREACH(tag, head, entries)
520 if (tag->tag == tagid) {
521 strlcpy(p, tag->name, PF_TAG_NAME_SIZE);
527 tag_unref(struct pf_tags *head, u_int16_t tag)
529 struct pf_tagname *p, *next;
534 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
535 next = TAILQ_NEXT(p, entries);
538 TAILQ_REMOVE(head, p, entries);
547 pf_tagname2tag(char *tagname)
549 return (tagname2tag(&pf_tags, tagname));
553 pf_tag2tagname(u_int16_t tagid, char *p)
555 tag2tagname(&pf_tags, tagid, p);
559 pf_tag_ref(u_int16_t tag)
561 struct pf_tagname *t;
563 TAILQ_FOREACH(t, &pf_tags, entries)
571 pf_tag_unref(u_int16_t tag)
573 tag_unref(&pf_tags, tag);
577 pf_rtlabel_add(struct pf_addr_wrap *a)
583 pf_rtlabel_remove(struct pf_addr_wrap *a)
588 pf_rtlabel_copyout(struct pf_addr_wrap *a)
590 if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel)
591 strlcpy(a->v.rtlabelname, "?", sizeof(a->v.rtlabelname));
596 pf_qname2qid(char *qname)
598 return ((u_int32_t)tagname2tag(&pf_qids, qname));
602 pf_qid2qname(u_int32_t qid, char *p)
604 tag2tagname(&pf_qids, (u_int16_t)qid, p);
608 pf_qid_unref(u_int32_t qid)
610 tag_unref(&pf_qids, (u_int16_t)qid);
614 pf_begin_altq(u_int32_t *ticket)
616 struct pf_altq *altq;
619 /* Purge the old altq list */
620 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
621 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
622 if (altq->qname[0] == 0) {
623 /* detach and destroy the discipline */
624 error = altq_remove(altq);
626 pf_qid_unref(altq->qid);
627 pool_put(&pf_altq_pl, altq);
631 *ticket = ++ticket_altqs_inactive;
632 altqs_inactive_open = 1;
637 pf_rollback_altq(u_int32_t ticket)
639 struct pf_altq *altq;
642 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
644 /* Purge the old altq list */
645 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
646 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
647 if (altq->qname[0] == 0) {
648 /* detach and destroy the discipline */
649 error = altq_remove(altq);
651 pf_qid_unref(altq->qid);
652 pool_put(&pf_altq_pl, altq);
654 altqs_inactive_open = 0;
659 pf_commit_altq(u_int32_t ticket)
661 struct pf_altqqueue *old_altqs;
662 struct pf_altq *altq;
665 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
668 /* swap altqs, keep the old. */
670 old_altqs = pf_altqs_active;
671 pf_altqs_active = pf_altqs_inactive;
672 pf_altqs_inactive = old_altqs;
673 ticket_altqs_active = ticket_altqs_inactive;
675 /* Attach new disciplines */
676 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
677 if (altq->qname[0] == 0) {
678 /* attach the discipline */
679 error = altq_pfattach(altq);
687 /* Purge the old altq list */
688 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
689 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
690 if (altq->qname[0] == 0) {
691 /* detach and destroy the discipline */
693 error = pf_disable_altq(altq);
694 err = altq_pfdetach(altq);
695 if (err != 0 && error == 0)
697 err = altq_remove(altq);
698 if (err != 0 && error == 0)
701 pf_qid_unref(altq->qid);
702 pool_put(&pf_altq_pl, altq);
706 altqs_inactive_open = 0;
711 pf_enable_altq(struct pf_altq *altq)
714 struct tb_profile tb;
717 if ((ifp = ifunit(altq->ifname)) == NULL)
720 if (ifp->if_snd.altq_type != ALTQT_NONE)
721 error = altq_enable(&ifp->if_snd);
723 /* set tokenbucket regulator */
724 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
725 tb.rate = altq->ifbandwidth;
726 tb.depth = altq->tbrsize;
728 error = tbr_set(&ifp->if_snd, &tb);
736 pf_disable_altq(struct pf_altq *altq)
739 struct tb_profile tb;
742 if ((ifp = ifunit(altq->ifname)) == NULL)
746 * when the discipline is no longer referenced, it was overridden
747 * by a new one. if so, just return.
749 if (altq->altq_disc != ifp->if_snd.altq_disc)
752 error = altq_disable(&ifp->if_snd);
755 /* clear tokenbucket regulator */
758 error = tbr_set(&ifp->if_snd, &tb);
767 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
769 struct pf_ruleset *rs;
770 struct pf_rule *rule;
772 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
774 rs = pf_find_or_create_ruleset(anchor);
777 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
778 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
779 rs->rules[rs_num].inactive.rcount--;
781 *ticket = ++rs->rules[rs_num].inactive.ticket;
782 rs->rules[rs_num].inactive.open = 1;
787 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
789 struct pf_ruleset *rs;
790 struct pf_rule *rule;
792 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
794 rs = pf_find_ruleset(anchor);
795 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
796 rs->rules[rs_num].inactive.ticket != ticket)
798 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
799 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
800 rs->rules[rs_num].inactive.rcount--;
802 rs->rules[rs_num].inactive.open = 0;
806 #define PF_MD5_UPD(st, elm) \
807 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
809 #define PF_MD5_UPD_STR(st, elm) \
810 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
812 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
813 (stor) = htonl((st)->elm); \
814 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
817 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
818 (stor) = htons((st)->elm); \
819 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
823 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
825 PF_MD5_UPD(pfr, addr.type);
826 switch (pfr->addr.type) {
827 case PF_ADDR_DYNIFTL:
828 PF_MD5_UPD(pfr, addr.v.ifname);
829 PF_MD5_UPD(pfr, addr.iflags);
832 PF_MD5_UPD(pfr, addr.v.tblname);
834 case PF_ADDR_ADDRMASK:
836 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
837 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
839 case PF_ADDR_RTLABEL:
840 PF_MD5_UPD(pfr, addr.v.rtlabelname);
844 PF_MD5_UPD(pfr, port[0]);
845 PF_MD5_UPD(pfr, port[1]);
846 PF_MD5_UPD(pfr, neg);
847 PF_MD5_UPD(pfr, port_op);
851 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
856 pf_hash_rule_addr(ctx, &rule->src);
857 pf_hash_rule_addr(ctx, &rule->dst);
858 PF_MD5_UPD_STR(rule, label);
859 PF_MD5_UPD_STR(rule, ifname);
860 PF_MD5_UPD_STR(rule, match_tagname);
861 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
862 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
863 PF_MD5_UPD_HTONL(rule, prob, y);
864 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
865 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
866 PF_MD5_UPD(rule, uid.op);
867 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
868 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
869 PF_MD5_UPD(rule, gid.op);
870 PF_MD5_UPD_HTONL(rule, rule_flag, y);
871 PF_MD5_UPD(rule, action);
872 PF_MD5_UPD(rule, direction);
873 PF_MD5_UPD(rule, af);
874 PF_MD5_UPD(rule, quick);
875 PF_MD5_UPD(rule, ifnot);
876 PF_MD5_UPD(rule, match_tag_not);
877 PF_MD5_UPD(rule, natpass);
878 PF_MD5_UPD(rule, keep_state);
879 PF_MD5_UPD(rule, proto);
880 PF_MD5_UPD(rule, type);
881 PF_MD5_UPD(rule, code);
882 PF_MD5_UPD(rule, flags);
883 PF_MD5_UPD(rule, flagset);
884 PF_MD5_UPD(rule, allow_opts);
885 PF_MD5_UPD(rule, rt);
886 PF_MD5_UPD(rule, tos);
890 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
892 struct pf_ruleset *rs;
893 struct pf_rule *rule, **old_array;
894 struct pf_rulequeue *old_rules;
896 u_int32_t old_rcount;
898 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
900 rs = pf_find_ruleset(anchor);
901 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
902 ticket != rs->rules[rs_num].inactive.ticket)
905 /* Calculate checksum for the main ruleset */
906 if (rs == &pf_main_ruleset) {
907 error = pf_setup_pfsync_matching(rs);
912 /* Swap rules, keep the old. */
914 old_rules = rs->rules[rs_num].active.ptr;
915 old_rcount = rs->rules[rs_num].active.rcount;
916 old_array = rs->rules[rs_num].active.ptr_array;
918 rs->rules[rs_num].active.ptr =
919 rs->rules[rs_num].inactive.ptr;
920 rs->rules[rs_num].active.ptr_array =
921 rs->rules[rs_num].inactive.ptr_array;
922 rs->rules[rs_num].active.rcount =
923 rs->rules[rs_num].inactive.rcount;
924 rs->rules[rs_num].inactive.ptr = old_rules;
925 rs->rules[rs_num].inactive.ptr_array = old_array;
926 rs->rules[rs_num].inactive.rcount = old_rcount;
928 rs->rules[rs_num].active.ticket =
929 rs->rules[rs_num].inactive.ticket;
930 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
933 /* Purge the old rule list. */
934 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
935 pf_rm_rule(old_rules, rule);
936 if (rs->rules[rs_num].inactive.ptr_array)
937 kfree(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
938 rs->rules[rs_num].inactive.ptr_array = NULL;
939 rs->rules[rs_num].inactive.rcount = 0;
940 rs->rules[rs_num].inactive.open = 0;
941 pf_remove_if_empty_ruleset(rs);
947 pf_state_export(struct pfsync_state *sp, struct pf_state_key *sk,
950 int secs = time_second;
951 bzero(sp, sizeof(struct pfsync_state));
953 /* copy from state key */
954 sp->lan.addr = sk->lan.addr;
955 sp->lan.port = sk->lan.port;
956 sp->gwy.addr = sk->gwy.addr;
957 sp->gwy.port = sk->gwy.port;
958 sp->ext.addr = sk->ext.addr;
959 sp->ext.port = sk->ext.port;
960 sp->proto = sk->proto;
962 sp->direction = sk->direction;
964 /* copy from state */
965 memcpy(&sp->id, &s->id, sizeof(sp->id));
966 sp->creatorid = s->creatorid;
967 strlcpy(sp->ifname, s->kif->pfik_name, sizeof(sp->ifname));
968 pf_state_peer_to_pfsync(&s->src, &sp->src);
969 pf_state_peer_to_pfsync(&s->dst, &sp->dst);
971 sp->rule = s->rule.ptr->nr;
972 sp->nat_rule = (s->nat_rule.ptr == NULL) ? -1 : s->nat_rule.ptr->nr;
973 sp->anchor = (s->anchor.ptr == NULL) ? -1 : s->anchor.ptr->nr;
975 pf_state_counter_to_pfsync(s->bytes[0], sp->bytes[0]);
976 pf_state_counter_to_pfsync(s->bytes[1], sp->bytes[1]);
977 pf_state_counter_to_pfsync(s->packets[0], sp->packets[0]);
978 pf_state_counter_to_pfsync(s->packets[1], sp->packets[1]);
979 sp->creation = secs - s->creation;
980 sp->expire = pf_state_expires(s);
982 sp->allow_opts = s->allow_opts;
983 sp->timeout = s->timeout;
986 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
988 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
990 if (sp->expire > secs)
998 pf_state_import(struct pfsync_state *sp, struct pf_state_key *sk,
1001 /* copy to state key */
1002 sk->lan.addr = sp->lan.addr;
1003 sk->lan.port = sp->lan.port;
1004 sk->gwy.addr = sp->gwy.addr;
1005 sk->gwy.port = sp->gwy.port;
1006 sk->ext.addr = sp->ext.addr;
1007 sk->ext.port = sp->ext.port;
1008 sk->proto = sp->proto;
1010 sk->direction = sp->direction;
1013 memcpy(&s->id, &sp->id, sizeof(sp->id));
1014 s->creatorid = sp->creatorid;
1015 strlcpy(sp->ifname, s->kif->pfik_name, sizeof(sp->ifname));
1016 pf_state_peer_from_pfsync(&sp->src, &s->src);
1017 pf_state_peer_from_pfsync(&sp->dst, &s->dst);
1019 s->rule.ptr = &pf_default_rule;
1020 s->nat_rule.ptr = NULL;
1021 s->anchor.ptr = NULL;
1023 s->creation = time_second;
1025 s->packets[0] = s->packets[1] = 0;
1026 s->bytes[0] = s->bytes[1] = 0;
1030 pf_setup_pfsync_matching(struct pf_ruleset *rs)
1033 struct pf_rule *rule;
1035 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
1038 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
1039 /* XXX PF_RULESET_SCRUB as well? */
1040 if (rs_cnt == PF_RULESET_SCRUB)
1043 if (rs->rules[rs_cnt].inactive.ptr_array)
1044 kfree(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
1045 rs->rules[rs_cnt].inactive.ptr_array = NULL;
1047 if (rs->rules[rs_cnt].inactive.rcount) {
1048 rs->rules[rs_cnt].inactive.ptr_array =
1049 kmalloc(sizeof(caddr_t) *
1050 rs->rules[rs_cnt].inactive.rcount,
1053 if (!rs->rules[rs_cnt].inactive.ptr_array)
1057 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
1059 pf_hash_rule(&ctx, rule);
1060 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
1064 MD5Final(digest, &ctx);
1065 memcpy(pf_status.pf_chksum, digest, sizeof(pf_status.pf_chksum));
1070 pfioctl(struct dev_ioctl_args *ap)
1072 u_long cmd = ap->a_cmd;
1073 caddr_t addr = ap->a_data;
1074 struct pf_pooladdr *pa = NULL;
1075 struct pf_pool *pool = NULL;
1078 lwkt_gettoken(&pf_token);
1080 /* XXX keep in sync with switch() below */
1081 if (securelevel > 1)
1088 case DIOCSETSTATUSIF:
1094 case DIOCGETTIMEOUT:
1095 case DIOCCLRRULECTRS:
1100 case DIOCGETRULESETS:
1101 case DIOCGETRULESET:
1102 case DIOCRGETTABLES:
1103 case DIOCRGETTSTATS:
1104 case DIOCRCLRTSTATS:
1110 case DIOCRGETASTATS:
1111 case DIOCRCLRASTATS:
1114 case DIOCGETSRCNODES:
1115 case DIOCCLRSRCNODES:
1116 case DIOCIGETIFACES:
1121 case DIOCRCLRTABLES:
1122 case DIOCRADDTABLES:
1123 case DIOCRDELTABLES:
1124 case DIOCRSETTFLAGS:
1125 if (((struct pfioc_table *)addr)->pfrio_flags &
1127 break; /* dummy operation ok */
1128 lwkt_reltoken(&pf_token);
1131 lwkt_reltoken(&pf_token);
1135 if (!(ap->a_fflag & FWRITE))
1143 case DIOCGETTIMEOUT:
1148 case DIOCGETRULESETS:
1149 case DIOCGETRULESET:
1151 case DIOCRGETTABLES:
1152 case DIOCRGETTSTATS:
1154 case DIOCRGETASTATS:
1157 case DIOCGETSRCNODES:
1158 case DIOCIGETIFACES:
1161 case DIOCRCLRTABLES:
1162 case DIOCRADDTABLES:
1163 case DIOCRDELTABLES:
1164 case DIOCRCLRTSTATS:
1169 case DIOCRSETTFLAGS:
1170 if (((struct pfioc_table *)addr)->pfrio_flags &
1172 break; /* dummy operation ok */
1173 lwkt_reltoken(&pf_token);
1176 if (((struct pfioc_rule *)addr)->action == PF_GET_CLR_CNTR) {
1177 lwkt_reltoken(&pf_token);
1182 lwkt_reltoken(&pf_token);
1189 if (pf_status.running)
1194 DPFPRINTF(PF_DEBUG_MISC,
1195 ("pf: pfil registration fail\n"));
1198 pf_status.running = 1;
1199 pf_status.since = time_second;
1200 if (pf_status.stateid == 0) {
1201 pf_status.stateid = time_second;
1202 pf_status.stateid = pf_status.stateid << 32;
1204 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1209 if (!pf_status.running)
1212 pf_status.running = 0;
1213 error = dehook_pf();
1215 pf_status.running = 1;
1216 DPFPRINTF(PF_DEBUG_MISC,
1217 ("pf: pfil unregistration failed\n"));
1219 pf_status.since = time_second;
1220 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1225 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1226 struct pf_ruleset *ruleset;
1227 struct pf_rule *rule, *tail;
1228 struct pf_pooladdr *pa;
1231 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1232 ruleset = pf_find_ruleset(pr->anchor);
1233 if (ruleset == NULL) {
1237 rs_num = pf_get_ruleset_number(pr->rule.action);
1238 if (rs_num >= PF_RULESET_MAX) {
1242 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1246 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1250 if (pr->pool_ticket != ticket_pabuf) {
1254 rule = pool_get(&pf_rule_pl, PR_NOWAIT);
1259 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1260 rule->cuid = ap->a_cred->cr_ruid;
1261 rule->cpid = (int)NULL;
1262 rule->anchor = NULL;
1264 TAILQ_INIT(&rule->rpool.list);
1265 /* initialize refcounting */
1267 rule->src_nodes = 0;
1268 rule->entries.tqe_prev = NULL;
1270 if (rule->af == AF_INET) {
1271 pool_put(&pf_rule_pl, rule);
1272 error = EAFNOSUPPORT;
1277 if (rule->af == AF_INET6) {
1278 pool_put(&pf_rule_pl, rule);
1279 error = EAFNOSUPPORT;
1283 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1286 rule->nr = tail->nr + 1;
1289 if (rule->ifname[0]) {
1290 rule->kif = pfi_kif_get(rule->ifname);
1291 if (rule->kif == NULL) {
1292 pool_put(&pf_rule_pl, rule);
1296 pfi_kif_ref(rule->kif, PFI_KIF_REF_RULE);
1299 if (rule->rtableid > 0 && rule->rtableid > rt_numfibs)
1304 if (rule->qname[0] != 0) {
1305 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1307 else if (rule->pqname[0] != 0) {
1309 pf_qname2qid(rule->pqname)) == 0)
1312 rule->pqid = rule->qid;
1315 if (rule->tagname[0])
1316 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1318 if (rule->match_tagname[0])
1319 if ((rule->match_tag =
1320 pf_tagname2tag(rule->match_tagname)) == 0)
1322 if (rule->rt && !rule->direction)
1327 if (rule->logif >= PFLOGIFS_MAX)
1330 if (pf_rtlabel_add(&rule->src.addr) ||
1331 pf_rtlabel_add(&rule->dst.addr))
1333 if (pfi_dynaddr_setup(&rule->src.addr, rule->af))
1335 if (pfi_dynaddr_setup(&rule->dst.addr, rule->af))
1337 if (pf_tbladdr_setup(ruleset, &rule->src.addr))
1339 if (pf_tbladdr_setup(ruleset, &rule->dst.addr))
1341 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1343 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1344 if (pf_tbladdr_setup(ruleset, &pa->addr))
1347 if (rule->overload_tblname[0]) {
1348 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1349 rule->overload_tblname)) == NULL)
1352 rule->overload_tbl->pfrkt_flags |=
1356 pf_mv_pool(&pf_pabuf, &rule->rpool.list);
1357 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1358 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1359 (rule->rt > PF_FASTROUTE)) &&
1360 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1364 pf_rm_rule(NULL, rule);
1367 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1368 rule->evaluations = rule->packets[0] = rule->packets[1] =
1369 rule->bytes[0] = rule->bytes[1] = 0;
1370 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1372 ruleset->rules[rs_num].inactive.rcount++;
1376 case DIOCGETRULES: {
1377 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1378 struct pf_ruleset *ruleset;
1379 struct pf_rule *tail;
1382 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1383 ruleset = pf_find_ruleset(pr->anchor);
1384 if (ruleset == NULL) {
1388 rs_num = pf_get_ruleset_number(pr->rule.action);
1389 if (rs_num >= PF_RULESET_MAX) {
1393 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1396 pr->nr = tail->nr + 1;
1399 pr->ticket = ruleset->rules[rs_num].active.ticket;
1404 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1405 struct pf_ruleset *ruleset;
1406 struct pf_rule *rule;
1409 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1410 ruleset = pf_find_ruleset(pr->anchor);
1411 if (ruleset == NULL) {
1415 rs_num = pf_get_ruleset_number(pr->rule.action);
1416 if (rs_num >= PF_RULESET_MAX) {
1420 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1424 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1425 while ((rule != NULL) && (rule->nr != pr->nr))
1426 rule = TAILQ_NEXT(rule, entries);
1431 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1432 if (pf_anchor_copyout(ruleset, rule, pr)) {
1436 pfi_dynaddr_copyout(&pr->rule.src.addr);
1437 pfi_dynaddr_copyout(&pr->rule.dst.addr);
1438 pf_tbladdr_copyout(&pr->rule.src.addr);
1439 pf_tbladdr_copyout(&pr->rule.dst.addr);
1440 pf_rtlabel_copyout(&pr->rule.src.addr);
1441 pf_rtlabel_copyout(&pr->rule.dst.addr);
1442 for (i = 0; i < PF_SKIP_COUNT; ++i)
1443 if (rule->skip[i].ptr == NULL)
1444 pr->rule.skip[i].nr = (uint32_t)(-1);
1446 pr->rule.skip[i].nr =
1447 rule->skip[i].ptr->nr;
1449 if (pr->action == PF_GET_CLR_CNTR) {
1450 rule->evaluations = 0;
1451 rule->packets[0] = rule->packets[1] = 0;
1452 rule->bytes[0] = rule->bytes[1] = 0;
1457 case DIOCCHANGERULE: {
1458 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1459 struct pf_ruleset *ruleset;
1460 struct pf_rule *oldrule = NULL, *newrule = NULL;
1464 if (!(pcr->action == PF_CHANGE_REMOVE ||
1465 pcr->action == PF_CHANGE_GET_TICKET) &&
1466 pcr->pool_ticket != ticket_pabuf) {
1471 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1472 pcr->action > PF_CHANGE_GET_TICKET) {
1476 ruleset = pf_find_ruleset(pcr->anchor);
1477 if (ruleset == NULL) {
1481 rs_num = pf_get_ruleset_number(pcr->rule.action);
1482 if (rs_num >= PF_RULESET_MAX) {
1487 if (pcr->action == PF_CHANGE_GET_TICKET) {
1488 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1492 ruleset->rules[rs_num].active.ticket) {
1496 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1502 if (pcr->action != PF_CHANGE_REMOVE) {
1503 newrule = pool_get(&pf_rule_pl, PR_NOWAIT);
1504 if (newrule == NULL) {
1508 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1509 newrule->cuid = ap->a_cred->cr_ruid;
1510 newrule->cpid = (int)NULL;
1511 TAILQ_INIT(&newrule->rpool.list);
1512 /* initialize refcounting */
1513 newrule->states = 0;
1514 newrule->entries.tqe_prev = NULL;
1516 if (newrule->af == AF_INET) {
1517 pool_put(&pf_rule_pl, newrule);
1518 error = EAFNOSUPPORT;
1523 if (newrule->af == AF_INET6) {
1524 pool_put(&pf_rule_pl, newrule);
1525 error = EAFNOSUPPORT;
1529 if (newrule->ifname[0]) {
1530 newrule->kif = pfi_kif_get(newrule->ifname);
1531 if (newrule->kif == NULL) {
1532 pool_put(&pf_rule_pl, newrule);
1536 pfi_kif_ref(newrule->kif, PFI_KIF_REF_RULE);
1538 newrule->kif = NULL;
1540 if (newrule->rtableid > 0 &&
1541 newrule->rtableid > rt_numfibs)
1546 if (newrule->qname[0] != 0) {
1548 pf_qname2qid(newrule->qname)) == 0)
1550 else if (newrule->pqname[0] != 0) {
1551 if ((newrule->pqid =
1552 pf_qname2qid(newrule->pqname)) == 0)
1555 newrule->pqid = newrule->qid;
1558 if (newrule->tagname[0])
1560 pf_tagname2tag(newrule->tagname)) == 0)
1562 if (newrule->match_tagname[0])
1563 if ((newrule->match_tag = pf_tagname2tag(
1564 newrule->match_tagname)) == 0)
1566 if (newrule->rt && !newrule->direction)
1571 if (newrule->logif >= PFLOGIFS_MAX)
1574 if (pf_rtlabel_add(&newrule->src.addr) ||
1575 pf_rtlabel_add(&newrule->dst.addr))
1577 if (pfi_dynaddr_setup(&newrule->src.addr, newrule->af))
1579 if (pfi_dynaddr_setup(&newrule->dst.addr, newrule->af))
1581 if (pf_tbladdr_setup(ruleset, &newrule->src.addr))
1583 if (pf_tbladdr_setup(ruleset, &newrule->dst.addr))
1585 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1587 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1588 if (pf_tbladdr_setup(ruleset, &pa->addr))
1591 if (newrule->overload_tblname[0]) {
1592 if ((newrule->overload_tbl = pfr_attach_table(
1593 ruleset, newrule->overload_tblname)) ==
1597 newrule->overload_tbl->pfrkt_flags |=
1601 pf_mv_pool(&pf_pabuf, &newrule->rpool.list);
1602 if (((((newrule->action == PF_NAT) ||
1603 (newrule->action == PF_RDR) ||
1604 (newrule->action == PF_BINAT) ||
1605 (newrule->rt > PF_FASTROUTE)) &&
1606 !newrule->anchor)) &&
1607 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1611 pf_rm_rule(NULL, newrule);
1614 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1615 newrule->evaluations = 0;
1616 newrule->packets[0] = newrule->packets[1] = 0;
1617 newrule->bytes[0] = newrule->bytes[1] = 0;
1619 pf_empty_pool(&pf_pabuf);
1621 if (pcr->action == PF_CHANGE_ADD_HEAD)
1622 oldrule = TAILQ_FIRST(
1623 ruleset->rules[rs_num].active.ptr);
1624 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1625 oldrule = TAILQ_LAST(
1626 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1628 oldrule = TAILQ_FIRST(
1629 ruleset->rules[rs_num].active.ptr);
1630 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1631 oldrule = TAILQ_NEXT(oldrule, entries);
1632 if (oldrule == NULL) {
1633 if (newrule != NULL)
1634 pf_rm_rule(NULL, newrule);
1640 if (pcr->action == PF_CHANGE_REMOVE) {
1641 pf_rm_rule(ruleset->rules[rs_num].active.ptr, oldrule);
1642 ruleset->rules[rs_num].active.rcount--;
1644 if (oldrule == NULL)
1646 ruleset->rules[rs_num].active.ptr,
1648 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1649 pcr->action == PF_CHANGE_ADD_BEFORE)
1650 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1653 ruleset->rules[rs_num].active.ptr,
1654 oldrule, newrule, entries);
1655 ruleset->rules[rs_num].active.rcount++;
1659 TAILQ_FOREACH(oldrule,
1660 ruleset->rules[rs_num].active.ptr, entries)
1663 ruleset->rules[rs_num].active.ticket++;
1665 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1666 pf_remove_if_empty_ruleset(ruleset);
1671 case DIOCCLRSTATES: {
1672 struct pf_state *s, *nexts;
1673 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1676 for (s = RB_MIN(pf_state_tree_id, &tree_id); s; s = nexts) {
1677 nexts = RB_NEXT(pf_state_tree_id, &tree_id, s);
1679 if (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1680 s->kif->pfik_name)) {
1682 /* don't send out individual delete messages */
1683 s->sync_flags = PFSTATE_NOSYNC;
1689 psk->psk_af = killed;
1691 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
1696 case DIOCKILLSTATES: {
1697 struct pf_state *s, *nexts;
1698 struct pf_state_key *sk;
1699 struct pf_state_host *src, *dst;
1700 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1703 for (s = RB_MIN(pf_state_tree_id, &tree_id); s;
1705 nexts = RB_NEXT(pf_state_tree_id, &tree_id, s);
1708 if (sk->direction == PF_OUT) {
1715 if ((!psk->psk_af || sk->af == psk->psk_af)
1716 && (!psk->psk_proto || psk->psk_proto ==
1718 PF_MATCHA(psk->psk_src.neg,
1719 &psk->psk_src.addr.v.a.addr,
1720 &psk->psk_src.addr.v.a.mask,
1721 &src->addr, sk->af) &&
1722 PF_MATCHA(psk->psk_dst.neg,
1723 &psk->psk_dst.addr.v.a.addr,
1724 &psk->psk_dst.addr.v.a.mask,
1725 &dst->addr, sk->af) &&
1726 (psk->psk_src.port_op == 0 ||
1727 pf_match_port(psk->psk_src.port_op,
1728 psk->psk_src.port[0], psk->psk_src.port[1],
1730 (psk->psk_dst.port_op == 0 ||
1731 pf_match_port(psk->psk_dst.port_op,
1732 psk->psk_dst.port[0], psk->psk_dst.port[1],
1734 (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1735 s->kif->pfik_name))) {
1737 /* send immediate delete of state */
1738 pfsync_delete_state(s);
1739 s->sync_flags |= PFSTATE_NOSYNC;
1745 psk->psk_af = killed;
1749 case DIOCADDSTATE: {
1750 struct pfioc_state *ps = (struct pfioc_state *)addr;
1751 struct pfsync_state *sp = (struct pfsync_state *)ps->state;
1753 struct pf_state_key *sk;
1754 struct pfi_kif *kif;
1756 if (sp->timeout >= PFTM_MAX &&
1757 sp->timeout != PFTM_UNTIL_PACKET) {
1761 s = pool_get(&pf_state_pl, PR_NOWAIT);
1766 bzero(s, sizeof(struct pf_state));
1767 if ((sk = pf_alloc_state_key(s)) == NULL) {
1771 pf_state_import(sp, sk, s);
1772 kif = pfi_kif_get(sp->ifname);
1774 pool_put(&pf_state_pl, s);
1775 pool_put(&pf_state_key_pl, sk);
1779 if (pf_insert_state(kif, s)) {
1780 pfi_kif_unref(kif, PFI_KIF_REF_NONE);
1781 pool_put(&pf_state_pl, s);
1782 pool_put(&pf_state_key_pl, sk);
1788 case DIOCGETSTATE: {
1789 struct pfioc_state *ps = (struct pfioc_state *)addr;
1794 RB_FOREACH(s, pf_state_tree_id, &tree_id) {
1804 pf_state_export((struct pfsync_state *)&ps->state,
1809 case DIOCGETSTATES: {
1810 struct pfioc_states *ps = (struct pfioc_states *)addr;
1811 struct pf_state *state;
1812 struct pfsync_state *p, *pstore;
1815 if (ps->ps_len == 0) {
1816 nr = pf_status.states;
1817 ps->ps_len = sizeof(struct pfsync_state) * nr;
1821 pstore = kmalloc(sizeof(*pstore), M_TEMP, M_WAITOK);
1825 state = TAILQ_FIRST(&state_list);
1827 if (state->timeout != PFTM_UNLINKED) {
1828 if ((nr+1) * sizeof(*p) > (unsigned)ps->ps_len)
1831 pf_state_export(pstore,
1832 state->state_key, state);
1833 error = copyout(pstore, p, sizeof(*p));
1835 kfree(pstore, M_TEMP);
1841 state = TAILQ_NEXT(state, entry_list);
1844 ps->ps_len = sizeof(struct pfsync_state) * nr;
1846 kfree(pstore, M_TEMP);
1850 case DIOCGETSTATUS: {
1851 struct pf_status *s = (struct pf_status *)addr;
1852 bcopy(&pf_status, s, sizeof(struct pf_status));
1853 pfi_fill_oldstatus(s);
1857 case DIOCSETSTATUSIF: {
1858 struct pfioc_if *pi = (struct pfioc_if *)addr;
1860 if (pi->ifname[0] == 0) {
1861 bzero(pf_status.ifname, IFNAMSIZ);
1864 if (ifunit(pi->ifname) == NULL) {
1868 strlcpy(pf_status.ifname, pi->ifname, IFNAMSIZ);
1872 case DIOCCLRSTATUS: {
1873 bzero(pf_status.counters, sizeof(pf_status.counters));
1874 bzero(pf_status.fcounters, sizeof(pf_status.fcounters));
1875 bzero(pf_status.scounters, sizeof(pf_status.scounters));
1876 pf_status.since = time_second;
1877 if (*pf_status.ifname)
1878 pfi_clr_istats(pf_status.ifname);
1883 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1884 struct pf_state_key *sk;
1885 struct pf_state *state;
1886 struct pf_state_key_cmp key;
1887 int m = 0, direction = pnl->direction;
1890 key.proto = pnl->proto;
1893 PF_AZERO(&pnl->saddr, pnl->af) ||
1894 PF_AZERO(&pnl->daddr, pnl->af) ||
1895 ((pnl->proto == IPPROTO_TCP ||
1896 pnl->proto == IPPROTO_UDP) &&
1897 (!pnl->dport || !pnl->sport)))
1901 * userland gives us source and dest of connection,
1902 * reverse the lookup so we ask for what happens with
1903 * the return traffic, enabling us to find it in the
1906 if (direction == PF_IN) {
1907 PF_ACPY(&key.ext.addr, &pnl->daddr, pnl->af);
1908 key.ext.port = pnl->dport;
1909 PF_ACPY(&key.gwy.addr, &pnl->saddr, pnl->af);
1910 key.gwy.port = pnl->sport;
1911 state = pf_find_state_all(&key, PF_EXT_GWY, &m);
1913 PF_ACPY(&key.lan.addr, &pnl->daddr, pnl->af);
1914 key.lan.port = pnl->dport;
1915 PF_ACPY(&key.ext.addr, &pnl->saddr, pnl->af);
1916 key.ext.port = pnl->sport;
1917 state = pf_find_state_all(&key, PF_LAN_EXT, &m);
1920 error = E2BIG; /* more than one state */
1921 else if (state != NULL) {
1922 sk = state->state_key;
1923 if (direction == PF_IN) {
1924 PF_ACPY(&pnl->rsaddr, &sk->lan.addr,
1926 pnl->rsport = sk->lan.port;
1927 PF_ACPY(&pnl->rdaddr, &pnl->daddr,
1929 pnl->rdport = pnl->dport;
1931 PF_ACPY(&pnl->rdaddr, &sk->gwy.addr,
1933 pnl->rdport = sk->gwy.port;
1934 PF_ACPY(&pnl->rsaddr, &pnl->saddr,
1936 pnl->rsport = pnl->sport;
1944 case DIOCSETTIMEOUT: {
1945 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1948 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1953 old = pf_default_rule.timeout[pt->timeout];
1954 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
1956 pf_default_rule.timeout[pt->timeout] = pt->seconds;
1957 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
1958 wakeup(pf_purge_thread);
1963 case DIOCGETTIMEOUT: {
1964 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1966 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1970 pt->seconds = pf_default_rule.timeout[pt->timeout];
1974 case DIOCGETLIMIT: {
1975 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1977 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1981 pl->limit = pf_pool_limits[pl->index].limit;
1985 case DIOCSETLIMIT: {
1986 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1989 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1990 pf_pool_limits[pl->index].pp == NULL) {
1995 /* XXX Get an API to set limits on the zone/pool */
1996 old_limit = pf_pool_limits[pl->index].limit;
1997 pf_pool_limits[pl->index].limit = pl->limit;
1998 pl->limit = old_limit;
2002 case DIOCSETDEBUG: {
2003 u_int32_t *level = (u_int32_t *)addr;
2005 pf_status.debug = *level;
2009 case DIOCCLRRULECTRS: {
2010 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
2011 struct pf_ruleset *ruleset = &pf_main_ruleset;
2012 struct pf_rule *rule;
2015 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
2016 rule->evaluations = 0;
2017 rule->packets[0] = rule->packets[1] = 0;
2018 rule->bytes[0] = rule->bytes[1] = 0;
2023 case DIOCGIFSPEED: {
2024 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
2025 struct pf_ifspeed ps;
2028 if (psp->ifname[0] != 0) {
2029 /* Can we completely trust user-land? */
2030 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
2031 ifp = ifunit(ps.ifname);
2033 psp->baudrate = ifp->if_baudrate;
2041 case DIOCSTARTALTQ: {
2042 struct pf_altq *altq;
2044 /* enable all altq interfaces on active list */
2045 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2046 if (altq->qname[0] == 0) {
2047 error = pf_enable_altq(altq);
2053 pf_altq_running = 1;
2054 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
2058 case DIOCSTOPALTQ: {
2059 struct pf_altq *altq;
2061 /* disable all altq interfaces on active list */
2062 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2063 if (altq->qname[0] == 0) {
2064 error = pf_disable_altq(altq);
2070 pf_altq_running = 0;
2071 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
2076 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2077 struct pf_altq *altq, *a;
2079 if (pa->ticket != ticket_altqs_inactive) {
2083 altq = pool_get(&pf_altq_pl, PR_NOWAIT);
2088 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
2091 * if this is for a queue, find the discipline and
2092 * copy the necessary fields
2094 if (altq->qname[0] != 0) {
2095 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2097 pool_put(&pf_altq_pl, altq);
2100 TAILQ_FOREACH(a, pf_altqs_inactive, entries) {
2101 if (strncmp(a->ifname, altq->ifname,
2102 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2103 altq->altq_disc = a->altq_disc;
2109 error = altq_add(altq);
2111 pool_put(&pf_altq_pl, altq);
2115 TAILQ_INSERT_TAIL(pf_altqs_inactive, altq, entries);
2116 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2120 case DIOCGETALTQS: {
2121 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2122 struct pf_altq *altq;
2125 TAILQ_FOREACH(altq, pf_altqs_active, entries)
2127 pa->ticket = ticket_altqs_active;
2132 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2133 struct pf_altq *altq;
2136 if (pa->ticket != ticket_altqs_active) {
2141 altq = TAILQ_FIRST(pf_altqs_active);
2142 while ((altq != NULL) && (nr < pa->nr)) {
2143 altq = TAILQ_NEXT(altq, entries);
2150 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2154 case DIOCCHANGEALTQ:
2155 /* CHANGEALTQ not supported yet! */
2159 case DIOCGETQSTATS: {
2160 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2161 struct pf_altq *altq;
2165 if (pq->ticket != ticket_altqs_active) {
2169 nbytes = pq->nbytes;
2171 altq = TAILQ_FIRST(pf_altqs_active);
2172 while ((altq != NULL) && (nr < pq->nr)) {
2173 altq = TAILQ_NEXT(altq, entries);
2180 error = altq_getqstats(altq, pq->buf, &nbytes);
2182 pq->scheduler = altq->scheduler;
2183 pq->nbytes = nbytes;
2189 case DIOCBEGINADDRS: {
2190 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2192 pf_empty_pool(&pf_pabuf);
2193 pp->ticket = ++ticket_pabuf;
2198 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2200 if (pp->ticket != ticket_pabuf) {
2205 if (pp->af == AF_INET) {
2206 error = EAFNOSUPPORT;
2211 if (pp->af == AF_INET6) {
2212 error = EAFNOSUPPORT;
2216 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2217 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2218 pp->addr.addr.type != PF_ADDR_TABLE) {
2222 pa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2227 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2228 if (pa->ifname[0]) {
2229 pa->kif = pfi_kif_get(pa->ifname);
2230 if (pa->kif == NULL) {
2231 pool_put(&pf_pooladdr_pl, pa);
2235 pfi_kif_ref(pa->kif, PFI_KIF_REF_RULE);
2237 if (pfi_dynaddr_setup(&pa->addr, pp->af)) {
2238 pfi_dynaddr_remove(&pa->addr);
2239 pfi_kif_unref(pa->kif, PFI_KIF_REF_RULE);
2240 pool_put(&pf_pooladdr_pl, pa);
2244 TAILQ_INSERT_TAIL(&pf_pabuf, pa, entries);
2248 case DIOCGETADDRS: {
2249 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2252 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2253 pp->r_num, 0, 1, 0);
2258 TAILQ_FOREACH(pa, &pool->list, entries)
2264 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2267 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2268 pp->r_num, 0, 1, 1);
2273 pa = TAILQ_FIRST(&pool->list);
2274 while ((pa != NULL) && (nr < pp->nr)) {
2275 pa = TAILQ_NEXT(pa, entries);
2282 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2283 pfi_dynaddr_copyout(&pp->addr.addr);
2284 pf_tbladdr_copyout(&pp->addr.addr);
2285 pf_rtlabel_copyout(&pp->addr.addr);
2289 case DIOCCHANGEADDR: {
2290 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2291 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2292 struct pf_ruleset *ruleset;
2294 if (pca->action < PF_CHANGE_ADD_HEAD ||
2295 pca->action > PF_CHANGE_REMOVE) {
2299 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2300 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2301 pca->addr.addr.type != PF_ADDR_TABLE) {
2306 ruleset = pf_find_ruleset(pca->anchor);
2307 if (ruleset == NULL) {
2311 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2312 pca->r_num, pca->r_last, 1, 1);
2317 if (pca->action != PF_CHANGE_REMOVE) {
2318 newpa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2319 if (newpa == NULL) {
2323 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2325 if (pca->af == AF_INET) {
2326 pool_put(&pf_pooladdr_pl, newpa);
2327 error = EAFNOSUPPORT;
2332 if (pca->af == AF_INET6) {
2333 pool_put(&pf_pooladdr_pl, newpa);
2334 error = EAFNOSUPPORT;
2338 if (newpa->ifname[0]) {
2339 newpa->kif = pfi_kif_get(newpa->ifname);
2340 if (newpa->kif == NULL) {
2341 pool_put(&pf_pooladdr_pl, newpa);
2345 pfi_kif_ref(newpa->kif, PFI_KIF_REF_RULE);
2348 if (pfi_dynaddr_setup(&newpa->addr, pca->af) ||
2349 pf_tbladdr_setup(ruleset, &newpa->addr)) {
2350 pfi_dynaddr_remove(&newpa->addr);
2351 pfi_kif_unref(newpa->kif, PFI_KIF_REF_RULE);
2352 pool_put(&pf_pooladdr_pl, newpa);
2358 if (pca->action == PF_CHANGE_ADD_HEAD)
2359 oldpa = TAILQ_FIRST(&pool->list);
2360 else if (pca->action == PF_CHANGE_ADD_TAIL)
2361 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2365 oldpa = TAILQ_FIRST(&pool->list);
2366 while ((oldpa != NULL) && (i < pca->nr)) {
2367 oldpa = TAILQ_NEXT(oldpa, entries);
2370 if (oldpa == NULL) {
2376 if (pca->action == PF_CHANGE_REMOVE) {
2377 TAILQ_REMOVE(&pool->list, oldpa, entries);
2378 pfi_dynaddr_remove(&oldpa->addr);
2379 pf_tbladdr_remove(&oldpa->addr);
2380 pfi_kif_unref(oldpa->kif, PFI_KIF_REF_RULE);
2381 pool_put(&pf_pooladdr_pl, oldpa);
2384 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2385 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2386 pca->action == PF_CHANGE_ADD_BEFORE)
2387 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2389 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2393 pool->cur = TAILQ_FIRST(&pool->list);
2394 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
2399 case DIOCGETRULESETS: {
2400 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2401 struct pf_ruleset *ruleset;
2402 struct pf_anchor *anchor;
2404 pr->path[sizeof(pr->path) - 1] = 0;
2405 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2410 if (ruleset->anchor == NULL) {
2411 /* XXX kludge for pf_main_ruleset */
2412 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2413 if (anchor->parent == NULL)
2416 RB_FOREACH(anchor, pf_anchor_node,
2417 &ruleset->anchor->children)
2423 case DIOCGETRULESET: {
2424 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2425 struct pf_ruleset *ruleset;
2426 struct pf_anchor *anchor;
2429 pr->path[sizeof(pr->path) - 1] = 0;
2430 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2435 if (ruleset->anchor == NULL) {
2436 /* XXX kludge for pf_main_ruleset */
2437 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2438 if (anchor->parent == NULL && nr++ == pr->nr) {
2439 strlcpy(pr->name, anchor->name,
2444 RB_FOREACH(anchor, pf_anchor_node,
2445 &ruleset->anchor->children)
2446 if (nr++ == pr->nr) {
2447 strlcpy(pr->name, anchor->name,
2457 case DIOCRCLRTABLES: {
2458 struct pfioc_table *io = (struct pfioc_table *)addr;
2460 if (io->pfrio_esize != 0) {
2464 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2465 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2469 case DIOCRADDTABLES: {
2470 struct pfioc_table *io = (struct pfioc_table *)addr;
2472 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2476 error = pfr_add_tables(io->pfrio_buffer, io->pfrio_size,
2477 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2481 case DIOCRDELTABLES: {
2482 struct pfioc_table *io = (struct pfioc_table *)addr;
2484 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2488 error = pfr_del_tables(io->pfrio_buffer, io->pfrio_size,
2489 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2493 case DIOCRGETTABLES: {
2494 struct pfioc_table *io = (struct pfioc_table *)addr;
2496 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2500 error = pfr_get_tables(&io->pfrio_table, io->pfrio_buffer,
2501 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2505 case DIOCRGETTSTATS: {
2506 struct pfioc_table *io = (struct pfioc_table *)addr;
2508 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2512 error = pfr_get_tstats(&io->pfrio_table, io->pfrio_buffer,
2513 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2517 case DIOCRCLRTSTATS: {
2518 struct pfioc_table *io = (struct pfioc_table *)addr;
2520 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2524 error = pfr_clr_tstats(io->pfrio_buffer, io->pfrio_size,
2525 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2529 case DIOCRSETTFLAGS: {
2530 struct pfioc_table *io = (struct pfioc_table *)addr;
2532 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2536 error = pfr_set_tflags(io->pfrio_buffer, io->pfrio_size,
2537 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2538 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2542 case DIOCRCLRADDRS: {
2543 struct pfioc_table *io = (struct pfioc_table *)addr;
2545 if (io->pfrio_esize != 0) {
2549 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2550 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2554 case DIOCRADDADDRS: {
2555 struct pfioc_table *io = (struct pfioc_table *)addr;
2557 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2561 error = pfr_add_addrs(&io->pfrio_table, io->pfrio_buffer,
2562 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2563 PFR_FLAG_USERIOCTL);
2567 case DIOCRDELADDRS: {
2568 struct pfioc_table *io = (struct pfioc_table *)addr;
2570 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2574 error = pfr_del_addrs(&io->pfrio_table, io->pfrio_buffer,
2575 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2576 PFR_FLAG_USERIOCTL);
2580 case DIOCRSETADDRS: {
2581 struct pfioc_table *io = (struct pfioc_table *)addr;
2583 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2587 error = pfr_set_addrs(&io->pfrio_table, io->pfrio_buffer,
2588 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2589 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2590 PFR_FLAG_USERIOCTL, 0);
2594 case DIOCRGETADDRS: {
2595 struct pfioc_table *io = (struct pfioc_table *)addr;
2597 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2601 error = pfr_get_addrs(&io->pfrio_table, io->pfrio_buffer,
2602 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2606 case DIOCRGETASTATS: {
2607 struct pfioc_table *io = (struct pfioc_table *)addr;
2609 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2613 error = pfr_get_astats(&io->pfrio_table, io->pfrio_buffer,
2614 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2618 case DIOCRCLRASTATS: {
2619 struct pfioc_table *io = (struct pfioc_table *)addr;
2621 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2625 error = pfr_clr_astats(&io->pfrio_table, io->pfrio_buffer,
2626 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2627 PFR_FLAG_USERIOCTL);
2631 case DIOCRTSTADDRS: {
2632 struct pfioc_table *io = (struct pfioc_table *)addr;
2634 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2638 error = pfr_tst_addrs(&io->pfrio_table, io->pfrio_buffer,
2639 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2640 PFR_FLAG_USERIOCTL);
2644 case DIOCRINADEFINE: {
2645 struct pfioc_table *io = (struct pfioc_table *)addr;
2647 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2651 error = pfr_ina_define(&io->pfrio_table, io->pfrio_buffer,
2652 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2653 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2658 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2659 error = pf_osfp_add(io);
2664 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2665 error = pf_osfp_get(io);
2670 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2671 struct pfioc_trans_e *ioe;
2672 struct pfr_table *table;
2675 if (io->esize != sizeof(*ioe)) {
2679 ioe = (struct pfioc_trans_e *)kmalloc(sizeof(*ioe),
2681 table = (struct pfr_table *)kmalloc(sizeof(*table),
2683 for (i = 0; i < io->size; i++) {
2684 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2685 kfree(table, M_TEMP);
2690 switch (ioe->rs_num) {
2692 case PF_RULESET_ALTQ:
2693 if (ioe->anchor[0]) {
2694 kfree(table, M_TEMP);
2699 if ((error = pf_begin_altq(&ioe->ticket))) {
2700 kfree(table, M_TEMP);
2706 case PF_RULESET_TABLE:
2707 bzero(table, sizeof(*table));
2708 strlcpy(table->pfrt_anchor, ioe->anchor,
2709 sizeof(table->pfrt_anchor));
2710 if ((error = pfr_ina_begin(table,
2711 &ioe->ticket, NULL, 0))) {
2712 kfree(table, M_TEMP);
2718 if ((error = pf_begin_rules(&ioe->ticket,
2719 ioe->rs_num, ioe->anchor))) {
2720 kfree(table, M_TEMP);
2726 if (copyout(ioe, io->array+i, sizeof(io->array[i]))) {
2727 kfree(table, M_TEMP);
2733 kfree(table, M_TEMP);
2738 case DIOCXROLLBACK: {
2739 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2740 struct pfioc_trans_e *ioe;
2741 struct pfr_table *table;
2744 if (io->esize != sizeof(*ioe)) {
2748 ioe = (struct pfioc_trans_e *)kmalloc(sizeof(*ioe),
2750 table = (struct pfr_table *)kmalloc(sizeof(*table),
2752 for (i = 0; i < io->size; i++) {
2753 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2754 kfree(table, M_TEMP);
2759 switch (ioe->rs_num) {
2761 case PF_RULESET_ALTQ:
2762 if (ioe->anchor[0]) {
2763 kfree(table, M_TEMP);
2768 if ((error = pf_rollback_altq(ioe->ticket))) {
2769 kfree(table, M_TEMP);
2771 goto fail; /* really bad */
2775 case PF_RULESET_TABLE:
2776 bzero(table, sizeof(*table));
2777 strlcpy(table->pfrt_anchor, ioe->anchor,
2778 sizeof(table->pfrt_anchor));
2779 if ((error = pfr_ina_rollback(table,
2780 ioe->ticket, NULL, 0))) {
2781 kfree(table, M_TEMP);
2783 goto fail; /* really bad */
2787 if ((error = pf_rollback_rules(ioe->ticket,
2788 ioe->rs_num, ioe->anchor))) {
2789 kfree(table, M_TEMP);
2791 goto fail; /* really bad */
2796 kfree(table, M_TEMP);
2802 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2803 struct pfioc_trans_e *ioe;
2804 struct pfr_table *table;
2805 struct pf_ruleset *rs;
2808 if (io->esize != sizeof(*ioe)) {
2812 ioe = (struct pfioc_trans_e *)kmalloc(sizeof(*ioe),
2814 table = (struct pfr_table *)kmalloc(sizeof(*table),
2816 /* first makes sure everything will succeed */
2817 for (i = 0; i < io->size; i++) {
2818 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2819 kfree(table, M_TEMP);
2824 switch (ioe->rs_num) {
2826 case PF_RULESET_ALTQ:
2827 if (ioe->anchor[0]) {
2828 kfree(table, M_TEMP);
2833 if (!altqs_inactive_open || ioe->ticket !=
2834 ticket_altqs_inactive) {
2835 kfree(table, M_TEMP);
2842 case PF_RULESET_TABLE:
2843 rs = pf_find_ruleset(ioe->anchor);
2844 if (rs == NULL || !rs->topen || ioe->ticket !=
2846 kfree(table, M_TEMP);
2853 if (ioe->rs_num < 0 || ioe->rs_num >=
2855 kfree(table, M_TEMP);
2860 rs = pf_find_ruleset(ioe->anchor);
2862 !rs->rules[ioe->rs_num].inactive.open ||
2863 rs->rules[ioe->rs_num].inactive.ticket !=
2865 kfree(table, M_TEMP);
2873 /* now do the commit - no errors should happen here */
2874 for (i = 0; i < io->size; i++) {
2875 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2876 kfree(table, M_TEMP);
2881 switch (ioe->rs_num) {
2883 case PF_RULESET_ALTQ:
2884 if ((error = pf_commit_altq(ioe->ticket))) {
2885 kfree(table, M_TEMP);
2887 goto fail; /* really bad */
2891 case PF_RULESET_TABLE:
2892 bzero(table, sizeof(*table));
2893 strlcpy(table->pfrt_anchor, ioe->anchor,
2894 sizeof(table->pfrt_anchor));
2895 if ((error = pfr_ina_commit(table, ioe->ticket,
2897 kfree(table, M_TEMP);
2899 goto fail; /* really bad */
2903 if ((error = pf_commit_rules(ioe->ticket,
2904 ioe->rs_num, ioe->anchor))) {
2905 kfree(table, M_TEMP);
2907 goto fail; /* really bad */
2912 kfree(table, M_TEMP);
2917 case DIOCGETSRCNODES: {
2918 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
2919 struct pf_src_node *n, *p, *pstore;
2921 int space = psn->psn_len;
2924 RB_FOREACH(n, pf_src_tree, &tree_src_tracking)
2926 psn->psn_len = sizeof(struct pf_src_node) * nr;
2930 pstore = kmalloc(sizeof(*pstore), M_TEMP, M_WAITOK);
2932 p = psn->psn_src_nodes;
2933 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
2934 int secs = time_second, diff;
2936 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
2939 bcopy(n, pstore, sizeof(*pstore));
2940 if (n->rule.ptr != NULL)
2941 pstore->rule.nr = n->rule.ptr->nr;
2942 pstore->creation = secs - pstore->creation;
2943 if (pstore->expire > secs)
2944 pstore->expire -= secs;
2948 /* adjust the connection rate estimate */
2949 diff = secs - n->conn_rate.last;
2950 if (diff >= n->conn_rate.seconds)
2951 pstore->conn_rate.count = 0;
2953 pstore->conn_rate.count -=
2954 n->conn_rate.count * diff /
2955 n->conn_rate.seconds;
2957 error = copyout(pstore, p, sizeof(*p));
2959 kfree(pstore, M_TEMP);
2965 psn->psn_len = sizeof(struct pf_src_node) * nr;
2967 kfree(pstore, M_TEMP);
2971 case DIOCCLRSRCNODES: {
2972 struct pf_src_node *n;
2973 struct pf_state *state;
2975 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2976 state->src_node = NULL;
2977 state->nat_src_node = NULL;
2979 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
2983 pf_purge_expired_src_nodes(1);
2984 pf_status.src_nodes = 0;
2988 case DIOCKILLSRCNODES: {
2989 struct pf_src_node *sn;
2991 struct pfioc_src_node_kill *psnk = \
2992 (struct pfioc_src_node_kill *) addr;
2995 RB_FOREACH(sn, pf_src_tree, &tree_src_tracking) {
2996 if (PF_MATCHA(psnk->psnk_src.neg, \
2997 &psnk->psnk_src.addr.v.a.addr, \
2998 &psnk->psnk_src.addr.v.a.mask, \
2999 &sn->addr, sn->af) &&
3000 PF_MATCHA(psnk->psnk_dst.neg, \
3001 &psnk->psnk_dst.addr.v.a.addr, \
3002 &psnk->psnk_dst.addr.v.a.mask, \
3003 &sn->raddr, sn->af)) {
3004 /* Handle state to src_node linkage */
3005 if (sn->states != 0) {
3006 RB_FOREACH(s, pf_state_tree_id,
3008 if (s->src_node == sn)
3010 if (s->nat_src_node == sn)
3011 s->nat_src_node = NULL;
3021 pf_purge_expired_src_nodes(1);
3023 psnk->psnk_af = killed;
3027 case DIOCSETHOSTID: {
3028 u_int32_t *hostid = (u_int32_t *)addr;
3031 pf_status.hostid = karc4random();
3033 pf_status.hostid = *hostid;
3043 case DIOCIGETIFACES: {
3044 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3046 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3050 error = pfi_get_ifaces(io->pfiio_name, io->pfiio_buffer,
3055 case DIOCSETIFFLAG: {
3056 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3058 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3062 case DIOCCLRIFFLAG: {
3063 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3065 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3074 lwkt_reltoken(&pf_token);
3079 * XXX - Check for version missmatch!!!
3082 pf_clear_states(void)
3084 struct pf_state *state;
3086 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3087 state->timeout = PFTM_PURGE;
3089 /* don't send out individual delete messages */
3090 state->sync_flags = PFSTATE_NOSYNC;
3092 pf_unlink_state(state);
3094 pf_status.states = 0;
3097 * XXX This is called on module unload, we do not want to sync that over? */
3099 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
3104 pf_clear_tables(void)
3106 struct pfioc_table io;
3109 bzero(&io, sizeof(io));
3111 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3118 pf_clear_srcnodes(void)
3120 struct pf_src_node *n;
3121 struct pf_state *state;
3123 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3124 state->src_node = NULL;
3125 state->nat_src_node = NULL;
3127 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3131 pf_purge_expired_src_nodes(0);
3132 pf_status.src_nodes = 0;
3135 * XXX - Check for version missmatch!!!
3139 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3148 pf_status.running = 0;
3150 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn)) != 0) {
3151 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3154 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn)) != 0) {
3155 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3156 break; /* XXX: rollback? */
3158 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn)) != 0) {
3159 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3160 break; /* XXX: rollback? */
3162 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3164 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3165 break; /* XXX: rollback? */
3167 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3169 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3170 break; /* XXX: rollback? */
3173 /* XXX: these should always succeed here */
3174 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3175 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3176 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3177 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3178 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3180 if ((error = pf_clear_tables()) != 0)
3184 if ((error = pf_begin_altq(&t[0])) != 0) {
3185 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3188 pf_commit_altq(t[0]);
3193 pf_clear_srcnodes();
3195 /* status does not use malloced mem so no need to cleanup */
3196 /* fingerprints and interfaces have their own cleanup code */
3203 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3206 * DragonFly's version of pf uses FreeBSD's native host byte ordering
3207 * for ip_len/ip_off. This is why we don't have to change byte order
3208 * like the FreeBSD-5 version does.
3212 lwkt_gettoken(&pf_token);
3214 chk = pf_test(PF_IN, ifp, m, NULL, NULL);
3219 lwkt_reltoken(&pf_token);
3224 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3227 * DragonFly's version of pf uses FreeBSD's native host byte ordering
3228 * for ip_len/ip_off. This is why we don't have to change byte order
3229 * like the FreeBSD-5 version does.
3233 lwkt_gettoken(&pf_token);
3235 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3236 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3237 in_delayed_cksum(*m);
3238 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3240 chk = pf_test(PF_OUT, ifp, m, NULL, NULL);
3245 lwkt_reltoken(&pf_token);
3251 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3254 * IPv6 is not affected by ip_len/ip_off byte order changes.
3258 lwkt_gettoken(&pf_token);
3260 chk = pf_test6(PF_IN, ifp, m, NULL, NULL);
3265 lwkt_reltoken(&pf_token);
3270 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3273 * IPv6 is not affected by ip_len/ip_off byte order changes.
3277 lwkt_gettoken(&pf_token);
3279 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3280 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3281 in_delayed_cksum(*m);
3282 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3284 chk = pf_test6(PF_OUT, ifp, m, NULL, NULL);
3289 lwkt_reltoken(&pf_token);
3297 struct pfil_head *pfh_inet;
3299 struct pfil_head *pfh_inet6;
3302 lwkt_gettoken(&pf_token);
3304 if (pf_pfil_hooked) {
3305 lwkt_reltoken(&pf_token);
3309 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3310 if (pfh_inet == NULL) {
3311 lwkt_reltoken(&pf_token);
3314 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_MPSAFE, pfh_inet);
3315 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_MPSAFE, pfh_inet);
3317 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3318 if (pfh_inet6 == NULL) {
3319 pfil_remove_hook(pf_check_in, NULL, PFIL_IN, pfh_inet);
3320 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT, pfh_inet);
3321 lwkt_reltoken(&pf_token);
3324 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_MPSAFE, pfh_inet6);
3325 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_MPSAFE, pfh_inet6);
3329 lwkt_reltoken(&pf_token);
3336 struct pfil_head *pfh_inet;
3338 struct pfil_head *pfh_inet6;
3341 lwkt_gettoken(&pf_token);
3343 if (pf_pfil_hooked == 0) {
3344 lwkt_reltoken(&pf_token);
3348 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3349 if (pfh_inet == NULL) {
3350 lwkt_reltoken(&pf_token);
3353 pfil_remove_hook(pf_check_in, NULL, PFIL_IN, pfh_inet);
3354 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT, pfh_inet);
3356 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3357 if (pfh_inet6 == NULL) {
3358 lwkt_reltoken(&pf_token);
3361 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN, pfh_inet6);
3362 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT, pfh_inet6);
3366 lwkt_reltoken(&pf_token);
3375 lwkt_gettoken(&pf_token);
3378 lockinit(&pf_mod_lck, "pf task lck", 0, LK_CANRECURSE);
3379 pf_dev = make_dev(&pf_ops, 0, 0, 0, 0600, PF_NAME);
3382 dev_ops_remove_all(&pf_ops);
3383 lockuninit(&pf_mod_lck);
3384 lwkt_reltoken(&pf_token);
3387 lockinit(&pf_consistency_lock, "pfconslck", 0, LK_CANRECURSE);
3388 lwkt_reltoken(&pf_token);
3396 pf_status.running = 0;
3398 lwkt_gettoken(&pf_token);
3400 error = dehook_pf();
3403 * Should not happen!
3404 * XXX Due to error code ESRCH, kldunload will show
3405 * a message like 'No such process'.
3407 kprintf("pfil unregistration fail\n");
3408 lwkt_reltoken(&pf_token);
3413 while (pf_end_threads < 2) {
3414 wakeup_one(pf_purge_thread);
3415 lksleep(pf_purge_thread, &pf_mod_lck, 0, "pftmo", hz);
3422 dev_ops_remove_all(&pf_ops);
3423 lockuninit(&pf_consistency_lock);
3424 lockuninit(&pf_mod_lck);
3425 lwkt_reltoken(&pf_token);
3430 pf_modevent(module_t mod, int type, void *data)
3434 lwkt_gettoken(&pf_token);
3442 error = pf_unload();
3448 lwkt_reltoken(&pf_token);
3452 static moduledata_t pf_mod = {
3457 DECLARE_MODULE(pf, pf_mod, SI_SUB_PSEUDO, SI_ORDER_FIRST);
3458 MODULE_VERSION(pf, PF_MODVER);