1 /* $OpenBSD: pf_ioctl.c,v 1.209 2008/06/29 08:42:15 mcbride Exp $ */
2 /*add $OpenBSD: pf_ioctl.c,v 1.212 2009/02/15 20:42:33 mbalmer Exp $ */
5 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier
8 * Copyright (c) 2002,2003 Henning Brauer
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * - Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 * Effort sponsored in part by the Defense Advanced Research Projects
36 * Agency (DARPA) and Air Force Research Laboratory, Air Force
37 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
42 #include "opt_inet6.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>
79 #include <net/pf/if_pfsync.h>
82 #include <net/if_pflog.h>
83 #endif /* NPFLOG > 0 */
86 #include <netinet/ip6.h>
87 #include <netinet/in_pcb.h>
91 #include <net/altq/altq.h>
94 #include <machine/limits.h>
96 #include <sys/mutex.h>
98 u_int rt_numfibs = RT_NUMFIBS;
100 void init_zone_var(void);
101 void cleanup_pf_zone(void);
103 struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
104 u_int8_t, u_int8_t, u_int8_t);
106 void pf_mv_pool(struct pf_palist *, struct pf_palist *);
107 void pf_empty_pool(struct pf_palist *);
109 int pf_begin_altq(u_int32_t *);
110 int pf_rollback_altq(u_int32_t);
111 int pf_commit_altq(u_int32_t);
112 int pf_enable_altq(struct pf_altq *);
113 int pf_disable_altq(struct pf_altq *);
115 int pf_begin_rules(u_int32_t *, int, const char *);
116 int pf_rollback_rules(u_int32_t, int, char *);
117 int pf_setup_pfsync_matching(struct pf_ruleset *);
118 void pf_hash_rule(MD5_CTX *, struct pf_rule *);
119 void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
120 int pf_commit_rules(u_int32_t, int, char *);
121 int pf_addr_setup(struct pf_ruleset *,
122 struct pf_addr_wrap *, sa_family_t);
123 void pf_addr_copyout(struct pf_addr_wrap *);
125 struct pf_rule pf_default_rule;
126 struct lock pf_consistency_lock;
128 static int pf_altq_running;
131 #define TAGID_MAX 50000
132 TAILQ_HEAD(pf_tags, pf_tagname) pf_tags = TAILQ_HEAD_INITIALIZER(pf_tags),
133 pf_qids = TAILQ_HEAD_INITIALIZER(pf_qids);
135 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
136 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
138 u_int16_t tagname2tag(struct pf_tags *, char *);
139 void tag2tagname(struct pf_tags *, u_int16_t, char *);
140 void tag_unref(struct pf_tags *, u_int16_t);
141 int pf_rtlabel_add(struct pf_addr_wrap *);
142 void pf_rtlabel_remove(struct pf_addr_wrap *);
143 void pf_rtlabel_copyout(struct pf_addr_wrap *);
145 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) kprintf x
147 static cdev_t pf_dev;
150 * XXX - These are new and need to be checked when moveing to a new version
152 static void pf_clear_states(void);
153 static int pf_clear_tables(void);
154 static void pf_clear_srcnodes(void);
156 * XXX - These are new and need to be checked when moveing to a new version
160 * Wrapper functions for pfil(9) hooks
162 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
164 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
167 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
169 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
173 static int hook_pf(void);
174 static int dehook_pf(void);
175 static int shutdown_pf(void);
176 static int pf_load(void);
177 static int pf_unload(void);
183 static struct dev_ops pf_ops = { /* XXX convert to port model */
190 static volatile int pf_pfil_hooked = 0;
191 int pf_end_threads = 0;
192 struct lock pf_mod_lck;
194 int debug_pfugidhack = 0;
195 SYSCTL_INT(_debug, OID_AUTO, pfugidhack, CTLFLAG_RW, &debug_pfugidhack, 0,
196 "Enable/disable pf user/group rules mpsafe hack");
201 pf_src_tree_pl = pf_rule_pl = NULL;
202 pf_state_pl = pf_altq_pl = pf_pooladdr_pl = NULL;
203 pf_frent_pl = pf_frag_pl = pf_cache_pl = pf_cent_pl = NULL;
204 pf_state_scrub_pl = NULL;
205 pfr_ktable_pl = pfr_kentry_pl = NULL;
209 cleanup_pf_zone(void)
211 ZONE_DESTROY(pf_src_tree_pl);
212 ZONE_DESTROY(pf_rule_pl);
213 ZONE_DESTROY(pf_state_pl);
214 ZONE_DESTROY(pf_altq_pl);
215 ZONE_DESTROY(pf_pooladdr_pl);
216 ZONE_DESTROY(pf_frent_pl);
217 ZONE_DESTROY(pf_frag_pl);
218 ZONE_DESTROY(pf_cache_pl);
219 ZONE_DESTROY(pf_cent_pl);
220 ZONE_DESTROY(pfr_ktable_pl);
221 ZONE_DESTROY(pfr_kentry_pl);
222 ZONE_DESTROY(pfr_kentry_pl2);
223 ZONE_DESTROY(pf_state_scrub_pl);
224 ZONE_DESTROY(pfi_addr_pl);
230 u_int32_t *my_timeout = pf_default_rule.timeout;
234 ZONE_CREATE(pf_src_tree_pl,struct pf_src_node, "pfsrctrpl");
235 ZONE_CREATE(pf_rule_pl, struct pf_rule, "pfrulepl");
236 ZONE_CREATE(pf_state_pl, struct pf_state, "pfstatepl");
237 ZONE_CREATE(pf_state_key_pl, struct pf_state_key, "pfstatekeypl");
238 ZONE_CREATE(pf_state_item_pl, struct pf_state_item, "pfstateitempl");
239 ZONE_CREATE(pf_altq_pl, struct pf_altq, "pfaltqpl");
240 ZONE_CREATE(pf_pooladdr_pl,struct pf_pooladdr, "pfpooladdrpl");
241 ZONE_CREATE(pfr_ktable_pl, struct pfr_ktable, "pfrktable");
242 ZONE_CREATE(pfr_kentry_pl, struct pfr_kentry, "pfrkentry");
243 ZONE_CREATE(pfr_kentry_pl2, struct pfr_kentry, "pfrkentry2");
244 ZONE_CREATE(pf_frent_pl, struct pf_frent, "pffrent");
245 ZONE_CREATE(pf_frag_pl, struct pf_fragment, "pffrag");
246 ZONE_CREATE(pf_cache_pl, struct pf_fragment, "pffrcache");
247 ZONE_CREATE(pf_cent_pl, struct pf_frcache, "pffrcent");
248 ZONE_CREATE(pf_state_scrub_pl, struct pf_state_scrub,
250 ZONE_CREATE(pfi_addr_pl, struct pfi_dynaddr, "pfiaddrpl");
259 error = pf_osfp_initialize();
266 pf_pool_limits[PF_LIMIT_STATES].pp = pf_state_pl;
267 pf_pool_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
268 pf_pool_limits[PF_LIMIT_FRAGS].pp = pf_frent_pl;
269 pf_pool_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
270 /* XXX uma_zone_set_max(pf_pool_limits[PF_LIMIT_STATES].pp,
271 pf_pool_limits[PF_LIMIT_STATES].limit);
273 if (ctob(physmem) <= 100*1024*1024)
274 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit =
275 PFR_KENTRY_HIWAT_SMALL;
277 RB_INIT(&tree_src_tracking);
278 RB_INIT(&pf_anchors);
279 pf_init_ruleset(&pf_main_ruleset);
280 TAILQ_INIT(&pf_altqs[0]);
281 TAILQ_INIT(&pf_altqs[1]);
282 TAILQ_INIT(&pf_pabuf);
283 pf_altqs_active = &pf_altqs[0];
284 pf_altqs_inactive = &pf_altqs[1];
285 TAILQ_INIT(&state_list);
287 /* default rule should never be garbage collected */
288 pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
289 pf_default_rule.action = PF_PASS;
290 pf_default_rule.nr = (uint32_t)(-1);
291 pf_default_rule.rtableid = -1;
293 /* initialize default timeouts */
294 my_timeout[PFTM_TCP_FIRST_PACKET] = 120; /* First TCP packet */
295 my_timeout[PFTM_TCP_OPENING] = 30; /* No response yet */
296 my_timeout[PFTM_TCP_ESTABLISHED] = 24*60*60; /* Established */
297 my_timeout[PFTM_TCP_CLOSING] = 15 * 60; /* Half closed */
298 my_timeout[PFTM_TCP_FIN_WAIT] = 45; /* Got both FINs */
299 my_timeout[PFTM_TCP_CLOSED] = 90; /* Got a RST */
300 my_timeout[PFTM_UDP_FIRST_PACKET] = 60; /* First UDP packet */
301 my_timeout[PFTM_UDP_SINGLE] = 30; /* Unidirectional */
302 my_timeout[PFTM_UDP_MULTIPLE] = 60; /* Bidirectional */
303 my_timeout[PFTM_ICMP_FIRST_PACKET] = 20; /* First ICMP packet */
304 my_timeout[PFTM_ICMP_ERROR_REPLY] = 10; /* Got error response */
305 my_timeout[PFTM_OTHER_FIRST_PACKET] = 60; /* First packet */
306 my_timeout[PFTM_OTHER_SINGLE] = 30; /* Unidirectional */
307 my_timeout[PFTM_OTHER_MULTIPLE] = 60; /* Bidirectional */
308 my_timeout[PFTM_FRAG] = 30; /* Fragment expire */
309 my_timeout[PFTM_INTERVAL] = 10; /* Expire interval */
310 my_timeout[PFTM_SRC_NODE] = 0; /* Source Tracking */
311 my_timeout[PFTM_TS_DIFF] = 30; /* Allowed TS diff */
312 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
313 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
316 bzero(&pf_status, sizeof(pf_status));
317 pf_status.debug = PF_DEBUG_URGENT;
319 /* XXX do our best to avoid a conflict */
320 pf_status.hostid = karc4random();
322 if (kthread_create(pf_purge_thread, NULL, NULL, "pfpurge"))
323 panic("pfpurge thread");
329 pfopen(struct dev_open_args *ap)
331 lwkt_gettoken(&pf_token);
332 cdev_t dev = ap->a_head.a_dev;
333 if (minor(dev) >= 1) {
334 lwkt_reltoken(&pf_token);
337 lwkt_reltoken(&pf_token);
342 pfclose(struct dev_close_args *ap)
344 lwkt_gettoken(&pf_token);
345 cdev_t dev = ap->a_head.a_dev;
346 if (minor(dev) >= 1) {
347 lwkt_reltoken(&pf_token);
350 lwkt_reltoken(&pf_token);
355 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
356 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
357 u_int8_t check_ticket)
359 struct pf_ruleset *ruleset;
360 struct pf_rule *rule;
363 ruleset = pf_find_ruleset(anchor);
366 rs_num = pf_get_ruleset_number(rule_action);
367 if (rs_num >= PF_RULESET_MAX)
370 if (check_ticket && ticket !=
371 ruleset->rules[rs_num].active.ticket)
374 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
377 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
379 if (check_ticket && ticket !=
380 ruleset->rules[rs_num].inactive.ticket)
383 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
386 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
389 while ((rule != NULL) && (rule->nr != rule_number))
390 rule = TAILQ_NEXT(rule, entries);
395 return (&rule->rpool);
399 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
401 struct pf_pooladdr *mv_pool_pa;
403 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
404 TAILQ_REMOVE(poola, mv_pool_pa, entries);
405 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
410 pf_empty_pool(struct pf_palist *poola)
412 struct pf_pooladdr *empty_pool_pa;
414 while ((empty_pool_pa = TAILQ_FIRST(poola)) != NULL) {
415 pfi_dynaddr_remove(&empty_pool_pa->addr);
416 pf_tbladdr_remove(&empty_pool_pa->addr);
417 pfi_kif_unref(empty_pool_pa->kif, PFI_KIF_REF_RULE);
418 TAILQ_REMOVE(poola, empty_pool_pa, entries);
419 pool_put(&pf_pooladdr_pl, empty_pool_pa);
424 pf_rm_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
426 if (rulequeue != NULL) {
427 if (rule->states_cur <= 0) {
429 * XXX - we need to remove the table *before* detaching
430 * the rule to make sure the table code does not delete
431 * the anchor under our feet.
433 pf_tbladdr_remove(&rule->src.addr);
434 pf_tbladdr_remove(&rule->dst.addr);
435 if (rule->overload_tbl)
436 pfr_detach_table(rule->overload_tbl);
438 TAILQ_REMOVE(rulequeue, rule, entries);
439 rule->entries.tqe_prev = NULL;
443 if (rule->states_cur > 0 || rule->src_nodes > 0 ||
444 rule->entries.tqe_prev != NULL)
446 pf_tag_unref(rule->tag);
447 pf_tag_unref(rule->match_tag);
449 if (rule->pqid != rule->qid)
450 pf_qid_unref(rule->pqid);
451 pf_qid_unref(rule->qid);
453 pf_rtlabel_remove(&rule->src.addr);
454 pf_rtlabel_remove(&rule->dst.addr);
455 pfi_dynaddr_remove(&rule->src.addr);
456 pfi_dynaddr_remove(&rule->dst.addr);
457 if (rulequeue == NULL) {
458 pf_tbladdr_remove(&rule->src.addr);
459 pf_tbladdr_remove(&rule->dst.addr);
460 if (rule->overload_tbl)
461 pfr_detach_table(rule->overload_tbl);
463 pfi_kif_unref(rule->kif, PFI_KIF_REF_RULE);
464 pf_anchor_remove(rule);
465 pf_empty_pool(&rule->rpool.list);
466 pool_put(&pf_rule_pl, rule);
470 tagname2tag(struct pf_tags *head, char *tagname)
472 struct pf_tagname *tag, *p = NULL;
473 u_int16_t new_tagid = 1;
475 TAILQ_FOREACH(tag, head, entries)
476 if (strcmp(tagname, tag->name) == 0) {
482 * to avoid fragmentation, we do a linear search from the beginning
483 * and take the first free slot we find. if there is none or the list
484 * is empty, append a new entry at the end.
488 if (!TAILQ_EMPTY(head))
489 for (p = TAILQ_FIRST(head); p != NULL &&
490 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
491 new_tagid = p->tag + 1;
493 if (new_tagid > TAGID_MAX)
496 /* allocate and fill new struct pf_tagname */
497 tag = kmalloc(sizeof(*tag), M_TEMP, M_WAITOK);
500 strlcpy(tag->name, tagname, sizeof(tag->name));
501 tag->tag = new_tagid;
504 if (p != NULL) /* insert new entry before p */
505 TAILQ_INSERT_BEFORE(p, tag, entries);
506 else /* either list empty or no free slot in between */
507 TAILQ_INSERT_TAIL(head, tag, entries);
513 tag2tagname(struct pf_tags *head, u_int16_t tagid, char *p)
515 struct pf_tagname *tag;
517 TAILQ_FOREACH(tag, head, entries)
518 if (tag->tag == tagid) {
519 strlcpy(p, tag->name, PF_TAG_NAME_SIZE);
525 tag_unref(struct pf_tags *head, u_int16_t tag)
527 struct pf_tagname *p, *next;
532 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
533 next = TAILQ_NEXT(p, entries);
536 TAILQ_REMOVE(head, p, entries);
545 pf_tagname2tag(char *tagname)
547 return (tagname2tag(&pf_tags, tagname));
551 pf_tag2tagname(u_int16_t tagid, char *p)
553 tag2tagname(&pf_tags, tagid, p);
557 pf_tag_ref(u_int16_t tag)
559 struct pf_tagname *t;
561 TAILQ_FOREACH(t, &pf_tags, entries)
569 pf_tag_unref(u_int16_t tag)
571 tag_unref(&pf_tags, tag);
575 pf_rtlabel_add(struct pf_addr_wrap *a)
581 pf_rtlabel_remove(struct pf_addr_wrap *a)
586 pf_rtlabel_copyout(struct pf_addr_wrap *a)
588 if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel)
589 strlcpy(a->v.rtlabelname, "?", sizeof(a->v.rtlabelname));
594 pf_qname2qid(char *qname)
596 return ((u_int32_t)tagname2tag(&pf_qids, qname));
600 pf_qid2qname(u_int32_t qid, char *p)
602 tag2tagname(&pf_qids, (u_int16_t)qid, p);
606 pf_qid_unref(u_int32_t qid)
608 tag_unref(&pf_qids, (u_int16_t)qid);
612 pf_begin_altq(u_int32_t *ticket)
614 struct pf_altq *altq;
617 /* Purge the old altq list */
618 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
619 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
620 if (altq->qname[0] == 0) {
621 /* detach and destroy the discipline */
622 error = altq_remove(altq);
624 pf_qid_unref(altq->qid);
625 pool_put(&pf_altq_pl, altq);
629 *ticket = ++ticket_altqs_inactive;
630 altqs_inactive_open = 1;
635 pf_rollback_altq(u_int32_t ticket)
637 struct pf_altq *altq;
640 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
642 /* Purge the old altq list */
643 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
644 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
645 if (altq->qname[0] == 0) {
646 /* detach and destroy the discipline */
647 error = altq_remove(altq);
649 pf_qid_unref(altq->qid);
650 pool_put(&pf_altq_pl, altq);
652 altqs_inactive_open = 0;
657 pf_commit_altq(u_int32_t ticket)
659 struct pf_altqqueue *old_altqs;
660 struct pf_altq *altq;
663 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
666 /* swap altqs, keep the old. */
668 old_altqs = pf_altqs_active;
669 pf_altqs_active = pf_altqs_inactive;
670 pf_altqs_inactive = old_altqs;
671 ticket_altqs_active = ticket_altqs_inactive;
673 /* Attach new disciplines */
674 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
675 if (altq->qname[0] == 0) {
676 /* attach the discipline */
677 error = altq_pfattach(altq);
685 /* Purge the old altq list */
686 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
687 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
688 if (altq->qname[0] == 0) {
689 /* detach and destroy the discipline */
691 error = pf_disable_altq(altq);
692 err = altq_pfdetach(altq);
693 if (err != 0 && error == 0)
695 err = altq_remove(altq);
696 if (err != 0 && error == 0)
699 pf_qid_unref(altq->qid);
700 pool_put(&pf_altq_pl, altq);
704 altqs_inactive_open = 0;
709 pf_enable_altq(struct pf_altq *altq)
712 struct tb_profile tb;
715 if ((ifp = ifunit(altq->ifname)) == NULL)
718 if (ifp->if_snd.altq_type != ALTQT_NONE)
719 error = altq_enable(&ifp->if_snd);
721 /* set tokenbucket regulator */
722 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
723 tb.rate = altq->ifbandwidth;
724 tb.depth = altq->tbrsize;
726 error = tbr_set(&ifp->if_snd, &tb);
734 pf_disable_altq(struct pf_altq *altq)
737 struct tb_profile tb;
740 if ((ifp = ifunit(altq->ifname)) == NULL)
744 * when the discipline is no longer referenced, it was overridden
745 * by a new one. if so, just return.
747 if (altq->altq_disc != ifp->if_snd.altq_disc)
750 error = altq_disable(&ifp->if_snd);
753 /* clear tokenbucket regulator */
756 error = tbr_set(&ifp->if_snd, &tb);
765 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
767 struct pf_ruleset *rs;
768 struct pf_rule *rule;
770 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
772 rs = pf_find_or_create_ruleset(anchor);
775 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
776 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
777 rs->rules[rs_num].inactive.rcount--;
779 *ticket = ++rs->rules[rs_num].inactive.ticket;
780 rs->rules[rs_num].inactive.open = 1;
785 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
787 struct pf_ruleset *rs;
788 struct pf_rule *rule;
790 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
792 rs = pf_find_ruleset(anchor);
793 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
794 rs->rules[rs_num].inactive.ticket != ticket)
796 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
797 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
798 rs->rules[rs_num].inactive.rcount--;
800 rs->rules[rs_num].inactive.open = 0;
804 #define PF_MD5_UPD(st, elm) \
805 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
807 #define PF_MD5_UPD_STR(st, elm) \
808 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
810 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
811 (stor) = htonl((st)->elm); \
812 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
815 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
816 (stor) = htons((st)->elm); \
817 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
821 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
823 PF_MD5_UPD(pfr, addr.type);
824 switch (pfr->addr.type) {
825 case PF_ADDR_DYNIFTL:
826 PF_MD5_UPD(pfr, addr.v.ifname);
827 PF_MD5_UPD(pfr, addr.iflags);
830 PF_MD5_UPD(pfr, addr.v.tblname);
832 case PF_ADDR_ADDRMASK:
834 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
835 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
837 case PF_ADDR_RTLABEL:
838 PF_MD5_UPD(pfr, addr.v.rtlabelname);
842 PF_MD5_UPD(pfr, port[0]);
843 PF_MD5_UPD(pfr, port[1]);
844 PF_MD5_UPD(pfr, neg);
845 PF_MD5_UPD(pfr, port_op);
849 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
854 pf_hash_rule_addr(ctx, &rule->src);
855 pf_hash_rule_addr(ctx, &rule->dst);
856 PF_MD5_UPD_STR(rule, label);
857 PF_MD5_UPD_STR(rule, ifname);
858 PF_MD5_UPD_STR(rule, match_tagname);
859 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
860 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
861 PF_MD5_UPD_HTONL(rule, prob, y);
862 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
863 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
864 PF_MD5_UPD(rule, uid.op);
865 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
866 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
867 PF_MD5_UPD(rule, gid.op);
868 PF_MD5_UPD_HTONL(rule, rule_flag, y);
869 PF_MD5_UPD(rule, action);
870 PF_MD5_UPD(rule, direction);
871 PF_MD5_UPD(rule, af);
872 PF_MD5_UPD(rule, quick);
873 PF_MD5_UPD(rule, ifnot);
874 PF_MD5_UPD(rule, match_tag_not);
875 PF_MD5_UPD(rule, natpass);
876 PF_MD5_UPD(rule, keep_state);
877 PF_MD5_UPD(rule, proto);
878 PF_MD5_UPD(rule, type);
879 PF_MD5_UPD(rule, code);
880 PF_MD5_UPD(rule, flags);
881 PF_MD5_UPD(rule, flagset);
882 PF_MD5_UPD(rule, allow_opts);
883 PF_MD5_UPD(rule, rt);
884 PF_MD5_UPD(rule, tos);
888 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
890 struct pf_ruleset *rs;
891 struct pf_rule *rule, **old_array;
892 struct pf_rulequeue *old_rules;
894 u_int32_t old_rcount;
896 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
898 rs = pf_find_ruleset(anchor);
899 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
900 ticket != rs->rules[rs_num].inactive.ticket)
903 /* Calculate checksum for the main ruleset */
904 if (rs == &pf_main_ruleset) {
905 error = pf_setup_pfsync_matching(rs);
910 /* Swap rules, keep the old. */
912 old_rules = rs->rules[rs_num].active.ptr;
913 old_rcount = rs->rules[rs_num].active.rcount;
914 old_array = rs->rules[rs_num].active.ptr_array;
916 rs->rules[rs_num].active.ptr =
917 rs->rules[rs_num].inactive.ptr;
918 rs->rules[rs_num].active.ptr_array =
919 rs->rules[rs_num].inactive.ptr_array;
920 rs->rules[rs_num].active.rcount =
921 rs->rules[rs_num].inactive.rcount;
922 rs->rules[rs_num].inactive.ptr = old_rules;
923 rs->rules[rs_num].inactive.ptr_array = old_array;
924 rs->rules[rs_num].inactive.rcount = old_rcount;
926 rs->rules[rs_num].active.ticket =
927 rs->rules[rs_num].inactive.ticket;
928 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
931 /* Purge the old rule list. */
932 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
933 pf_rm_rule(old_rules, rule);
934 if (rs->rules[rs_num].inactive.ptr_array)
935 kfree(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
936 rs->rules[rs_num].inactive.ptr_array = NULL;
937 rs->rules[rs_num].inactive.rcount = 0;
938 rs->rules[rs_num].inactive.open = 0;
939 pf_remove_if_empty_ruleset(rs);
945 pf_setup_pfsync_matching(struct pf_ruleset *rs)
948 struct pf_rule *rule;
950 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
953 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
954 /* XXX PF_RULESET_SCRUB as well? */
955 if (rs_cnt == PF_RULESET_SCRUB)
958 if (rs->rules[rs_cnt].inactive.ptr_array)
959 kfree(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
960 rs->rules[rs_cnt].inactive.ptr_array = NULL;
962 if (rs->rules[rs_cnt].inactive.rcount) {
963 rs->rules[rs_cnt].inactive.ptr_array =
964 kmalloc(sizeof(caddr_t) *
965 rs->rules[rs_cnt].inactive.rcount,
968 if (!rs->rules[rs_cnt].inactive.ptr_array)
972 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
974 pf_hash_rule(&ctx, rule);
975 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
979 MD5Final(digest, &ctx);
980 memcpy(pf_status.pf_chksum, digest, sizeof(pf_status.pf_chksum));
985 pf_addr_setup(struct pf_ruleset *ruleset, struct pf_addr_wrap *addr,
988 if (pfi_dynaddr_setup(addr, af) ||
989 pf_tbladdr_setup(ruleset, addr))
996 pf_addr_copyout(struct pf_addr_wrap *addr)
998 pfi_dynaddr_copyout(addr);
999 pf_tbladdr_copyout(addr);
1000 pf_rtlabel_copyout(addr);
1004 pfioctl(struct dev_ioctl_args *ap)
1006 u_long cmd = ap->a_cmd;
1007 caddr_t addr = ap->a_data;
1008 struct pf_pooladdr *pa = NULL;
1009 struct pf_pool *pool = NULL;
1012 lwkt_gettoken(&pf_token);
1014 /* XXX keep in sync with switch() below */
1015 if (securelevel > 1)
1022 case DIOCSETSTATUSIF:
1028 case DIOCGETTIMEOUT:
1029 case DIOCCLRRULECTRS:
1034 case DIOCGETRULESETS:
1035 case DIOCGETRULESET:
1036 case DIOCRGETTABLES:
1037 case DIOCRGETTSTATS:
1038 case DIOCRCLRTSTATS:
1044 case DIOCRGETASTATS:
1045 case DIOCRCLRASTATS:
1048 case DIOCGETSRCNODES:
1049 case DIOCCLRSRCNODES:
1050 case DIOCIGETIFACES:
1055 case DIOCRCLRTABLES:
1056 case DIOCRADDTABLES:
1057 case DIOCRDELTABLES:
1058 case DIOCRSETTFLAGS:
1059 if (((struct pfioc_table *)addr)->pfrio_flags &
1061 break; /* dummy operation ok */
1062 lwkt_reltoken(&pf_token);
1065 lwkt_reltoken(&pf_token);
1069 if (!(ap->a_fflag & FWRITE))
1077 case DIOCGETTIMEOUT:
1082 case DIOCGETRULESETS:
1083 case DIOCGETRULESET:
1085 case DIOCRGETTABLES:
1086 case DIOCRGETTSTATS:
1088 case DIOCRGETASTATS:
1091 case DIOCGETSRCNODES:
1092 case DIOCIGETIFACES:
1095 case DIOCRCLRTABLES:
1096 case DIOCRADDTABLES:
1097 case DIOCRDELTABLES:
1098 case DIOCRCLRTSTATS:
1103 case DIOCRSETTFLAGS:
1104 if (((struct pfioc_table *)addr)->pfrio_flags &
1106 break; /* dummy operation ok */
1107 lwkt_reltoken(&pf_token);
1110 if (((struct pfioc_rule *)addr)->action ==
1112 lwkt_reltoken(&pf_token);
1117 lwkt_reltoken(&pf_token);
1124 if (pf_status.running)
1129 DPFPRINTF(PF_DEBUG_MISC,
1130 ("pf: pfil registration fail\n"));
1133 pf_status.running = 1;
1134 pf_status.since = time_second;
1135 if (pf_status.stateid == 0) {
1136 pf_status.stateid = time_second;
1137 pf_status.stateid = pf_status.stateid << 32;
1139 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1144 if (!pf_status.running)
1147 pf_status.running = 0;
1148 error = dehook_pf();
1150 pf_status.running = 1;
1151 DPFPRINTF(PF_DEBUG_MISC,
1152 ("pf: pfil unregistration failed\n"));
1154 pf_status.since = time_second;
1155 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1160 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1161 struct pf_ruleset *ruleset;
1162 struct pf_rule *rule, *tail;
1163 struct pf_pooladdr *pa;
1166 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1167 ruleset = pf_find_ruleset(pr->anchor);
1168 if (ruleset == NULL) {
1172 rs_num = pf_get_ruleset_number(pr->rule.action);
1173 if (rs_num >= PF_RULESET_MAX) {
1177 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1181 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1185 if (pr->pool_ticket != ticket_pabuf) {
1189 rule = pool_get(&pf_rule_pl, PR_WAITOK|PR_LIMITFAIL);
1194 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1195 rule->cuid = ap->a_cred->cr_ruid;
1196 rule->cpid = (int)NULL;
1197 rule->anchor = NULL;
1199 TAILQ_INIT(&rule->rpool.list);
1200 /* initialize refcounting */
1201 rule->states_cur = 0;
1202 rule->src_nodes = 0;
1203 rule->entries.tqe_prev = NULL;
1205 if (rule->af == AF_INET) {
1206 pool_put(&pf_rule_pl, rule);
1207 error = EAFNOSUPPORT;
1212 if (rule->af == AF_INET6) {
1213 pool_put(&pf_rule_pl, rule);
1214 error = EAFNOSUPPORT;
1218 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1221 rule->nr = tail->nr + 1;
1224 if (rule->ifname[0]) {
1225 rule->kif = pfi_kif_get(rule->ifname);
1226 if (rule->kif == NULL) {
1227 pool_put(&pf_rule_pl, rule);
1231 pfi_kif_ref(rule->kif, PFI_KIF_REF_RULE);
1234 if (rule->rtableid > 0 && rule->rtableid > rt_numfibs)
1239 if (rule->qname[0] != 0) {
1240 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1242 else if (rule->pqname[0] != 0) {
1244 pf_qname2qid(rule->pqname)) == 0)
1247 rule->pqid = rule->qid;
1250 if (rule->tagname[0])
1251 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1253 if (rule->match_tagname[0])
1254 if ((rule->match_tag =
1255 pf_tagname2tag(rule->match_tagname)) == 0)
1257 if (rule->rt && !rule->direction)
1262 if (rule->logif >= PFLOGIFS_MAX)
1265 if (pf_rtlabel_add(&rule->src.addr) ||
1266 pf_rtlabel_add(&rule->dst.addr))
1268 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
1270 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
1272 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1274 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1275 if (pf_tbladdr_setup(ruleset, &pa->addr))
1278 if (rule->overload_tblname[0]) {
1279 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1280 rule->overload_tblname)) == NULL)
1283 rule->overload_tbl->pfrkt_flags |=
1287 pf_mv_pool(&pf_pabuf, &rule->rpool.list);
1288 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1289 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1290 (rule->rt > PF_FASTROUTE)) &&
1291 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1295 pf_rm_rule(NULL, rule);
1298 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1299 rule->evaluations = rule->packets[0] = rule->packets[1] =
1300 rule->bytes[0] = rule->bytes[1] = 0;
1301 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1303 ruleset->rules[rs_num].inactive.rcount++;
1307 case DIOCGETRULES: {
1308 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1309 struct pf_ruleset *ruleset;
1310 struct pf_rule *tail;
1313 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1314 ruleset = pf_find_ruleset(pr->anchor);
1315 if (ruleset == NULL) {
1319 rs_num = pf_get_ruleset_number(pr->rule.action);
1320 if (rs_num >= PF_RULESET_MAX) {
1324 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1327 pr->nr = tail->nr + 1;
1330 pr->ticket = ruleset->rules[rs_num].active.ticket;
1335 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1336 struct pf_ruleset *ruleset;
1337 struct pf_rule *rule;
1340 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1341 ruleset = pf_find_ruleset(pr->anchor);
1342 if (ruleset == NULL) {
1346 rs_num = pf_get_ruleset_number(pr->rule.action);
1347 if (rs_num >= PF_RULESET_MAX) {
1351 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1355 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1356 while ((rule != NULL) && (rule->nr != pr->nr))
1357 rule = TAILQ_NEXT(rule, entries);
1362 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1363 if (pf_anchor_copyout(ruleset, rule, pr)) {
1367 pf_addr_copyout(&pr->rule.src.addr);
1368 pf_addr_copyout(&pr->rule.dst.addr);
1369 for (i = 0; i < PF_SKIP_COUNT; ++i)
1370 if (rule->skip[i].ptr == NULL)
1371 pr->rule.skip[i].nr = (uint32_t)(-1);
1373 pr->rule.skip[i].nr =
1374 rule->skip[i].ptr->nr;
1376 if (pr->action == PF_GET_CLR_CNTR) {
1377 rule->evaluations = 0;
1378 rule->packets[0] = rule->packets[1] = 0;
1379 rule->bytes[0] = rule->bytes[1] = 0;
1380 rule->states_tot = 0;
1385 case DIOCCHANGERULE: {
1386 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1387 struct pf_ruleset *ruleset;
1388 struct pf_rule *oldrule = NULL, *newrule = NULL;
1392 if (!(pcr->action == PF_CHANGE_REMOVE ||
1393 pcr->action == PF_CHANGE_GET_TICKET) &&
1394 pcr->pool_ticket != ticket_pabuf) {
1399 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1400 pcr->action > PF_CHANGE_GET_TICKET) {
1404 ruleset = pf_find_ruleset(pcr->anchor);
1405 if (ruleset == NULL) {
1409 rs_num = pf_get_ruleset_number(pcr->rule.action);
1410 if (rs_num >= PF_RULESET_MAX) {
1415 if (pcr->action == PF_CHANGE_GET_TICKET) {
1416 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1420 ruleset->rules[rs_num].active.ticket) {
1424 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1430 if (pcr->action != PF_CHANGE_REMOVE) {
1431 newrule = pool_get(&pf_rule_pl, PR_WAITOK|PR_LIMITFAIL);
1432 if (newrule == NULL) {
1436 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1437 newrule->cuid = ap->a_cred->cr_ruid;
1438 newrule->cpid = (int)NULL;
1439 TAILQ_INIT(&newrule->rpool.list);
1440 /* initialize refcounting */
1441 newrule->states_cur = 0;
1442 newrule->entries.tqe_prev = NULL;
1444 if (newrule->af == AF_INET) {
1445 pool_put(&pf_rule_pl, newrule);
1446 error = EAFNOSUPPORT;
1451 if (newrule->af == AF_INET6) {
1452 pool_put(&pf_rule_pl, newrule);
1453 error = EAFNOSUPPORT;
1457 if (newrule->ifname[0]) {
1458 newrule->kif = pfi_kif_get(newrule->ifname);
1459 if (newrule->kif == NULL) {
1460 pool_put(&pf_rule_pl, newrule);
1464 pfi_kif_ref(newrule->kif, PFI_KIF_REF_RULE);
1466 newrule->kif = NULL;
1468 if (newrule->rtableid > 0 &&
1469 newrule->rtableid > rt_numfibs)
1474 if (newrule->qname[0] != 0) {
1476 pf_qname2qid(newrule->qname)) == 0)
1478 else if (newrule->pqname[0] != 0) {
1479 if ((newrule->pqid =
1480 pf_qname2qid(newrule->pqname)) == 0)
1483 newrule->pqid = newrule->qid;
1486 if (newrule->tagname[0])
1488 pf_tagname2tag(newrule->tagname)) == 0)
1490 if (newrule->match_tagname[0])
1491 if ((newrule->match_tag = pf_tagname2tag(
1492 newrule->match_tagname)) == 0)
1494 if (newrule->rt && !newrule->direction)
1499 if (newrule->logif >= PFLOGIFS_MAX)
1502 if (pf_rtlabel_add(&newrule->src.addr) ||
1503 pf_rtlabel_add(&newrule->dst.addr))
1505 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
1507 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
1509 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1511 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1512 if (pf_tbladdr_setup(ruleset, &pa->addr))
1515 if (newrule->overload_tblname[0]) {
1516 if ((newrule->overload_tbl = pfr_attach_table(
1517 ruleset, newrule->overload_tblname)) ==
1521 newrule->overload_tbl->pfrkt_flags |=
1525 pf_mv_pool(&pf_pabuf, &newrule->rpool.list);
1526 if (((((newrule->action == PF_NAT) ||
1527 (newrule->action == PF_RDR) ||
1528 (newrule->action == PF_BINAT) ||
1529 (newrule->rt > PF_FASTROUTE)) &&
1530 !newrule->anchor)) &&
1531 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1535 pf_rm_rule(NULL, newrule);
1538 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1539 newrule->evaluations = 0;
1540 newrule->packets[0] = newrule->packets[1] = 0;
1541 newrule->bytes[0] = newrule->bytes[1] = 0;
1543 pf_empty_pool(&pf_pabuf);
1545 if (pcr->action == PF_CHANGE_ADD_HEAD)
1546 oldrule = TAILQ_FIRST(
1547 ruleset->rules[rs_num].active.ptr);
1548 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1549 oldrule = TAILQ_LAST(
1550 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1552 oldrule = TAILQ_FIRST(
1553 ruleset->rules[rs_num].active.ptr);
1554 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1555 oldrule = TAILQ_NEXT(oldrule, entries);
1556 if (oldrule == NULL) {
1557 if (newrule != NULL)
1558 pf_rm_rule(NULL, newrule);
1564 if (pcr->action == PF_CHANGE_REMOVE) {
1565 pf_rm_rule(ruleset->rules[rs_num].active.ptr, oldrule);
1566 ruleset->rules[rs_num].active.rcount--;
1568 if (oldrule == NULL)
1570 ruleset->rules[rs_num].active.ptr,
1572 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1573 pcr->action == PF_CHANGE_ADD_BEFORE)
1574 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1577 ruleset->rules[rs_num].active.ptr,
1578 oldrule, newrule, entries);
1579 ruleset->rules[rs_num].active.rcount++;
1583 TAILQ_FOREACH(oldrule,
1584 ruleset->rules[rs_num].active.ptr, entries)
1587 ruleset->rules[rs_num].active.ticket++;
1589 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1590 pf_remove_if_empty_ruleset(ruleset);
1595 case DIOCCLRSTATES: {
1596 struct pf_state *s, *nexts;
1597 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1600 for (s = RB_MIN(pf_state_tree_id, &tree_id); s; s = nexts) {
1601 nexts = RB_NEXT(pf_state_tree_id, &tree_id, s);
1603 if (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1604 s->kif->pfik_name)) {
1605 /* don't send out individual delete messages */
1606 s->sync_flags = PFSTATE_NOSYNC;
1611 psk->psk_killed = killed;
1612 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
1616 case DIOCKILLSTATES: {
1617 struct pf_state *s, *nexts;
1618 struct pf_state_key *sk;
1619 struct pf_addr *srcaddr, *dstaddr;
1620 u_int16_t srcport, dstport;
1621 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1624 if (psk->psk_pfcmp.id) {
1625 if (psk->psk_pfcmp.creatorid == 0)
1626 psk->psk_pfcmp.creatorid = pf_status.hostid;
1627 if ((s = pf_find_state_byid(&psk->psk_pfcmp))) {
1628 /* send immediate delete of state */
1629 pfsync_delete_state(s);
1630 s->sync_flags |= PFSTATE_NOSYNC;
1632 psk->psk_killed = 1;
1637 for (s = RB_MIN(pf_state_tree_id, &tree_id); s;
1639 nexts = RB_NEXT(pf_state_tree_id, &tree_id, s);
1640 sk = s->key[PF_SK_WIRE];
1642 if (s->direction == PF_OUT) {
1643 srcaddr = &sk->addr[1];
1644 dstaddr = &sk->addr[0];
1645 srcport = sk->port[0];
1646 dstport = sk->port[0];
1648 srcaddr = &sk->addr[0];
1649 dstaddr = &sk->addr[1];
1650 srcport = sk->port[0];
1651 dstport = sk->port[0];
1653 if ((!psk->psk_af || sk->af == psk->psk_af)
1654 && (!psk->psk_proto || psk->psk_proto ==
1656 PF_MATCHA(psk->psk_src.neg,
1657 &psk->psk_src.addr.v.a.addr,
1658 &psk->psk_src.addr.v.a.mask,
1660 PF_MATCHA(psk->psk_dst.neg,
1661 &psk->psk_dst.addr.v.a.addr,
1662 &psk->psk_dst.addr.v.a.mask,
1664 (psk->psk_src.port_op == 0 ||
1665 pf_match_port(psk->psk_src.port_op,
1666 psk->psk_src.port[0], psk->psk_src.port[1],
1668 (psk->psk_dst.port_op == 0 ||
1669 pf_match_port(psk->psk_dst.port_op,
1670 psk->psk_dst.port[0], psk->psk_dst.port[1],
1672 (!psk->psk_label[0] || (s->rule.ptr->label[0] &&
1673 !strcmp(psk->psk_label, s->rule.ptr->label))) &&
1674 (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1675 s->kif->pfik_name))) {
1676 /* send immediate delete of state */
1677 pfsync_delete_state(s);
1678 s->sync_flags |= PFSTATE_NOSYNC;
1683 psk->psk_killed = killed;
1687 case DIOCADDSTATE: {
1688 struct pfioc_state *ps = (struct pfioc_state *)addr;
1689 struct pfsync_state *sp = &ps->state;
1691 if (sp->timeout >= PFTM_MAX &&
1692 sp->timeout != PFTM_UNTIL_PACKET) {
1696 error = pfsync_state_import(sp, PFSYNC_SI_IOCTL);
1700 case DIOCGETSTATE: {
1701 struct pfioc_state *ps = (struct pfioc_state *)addr;
1703 struct pf_state_cmp id_key;
1705 bcopy(ps->state.id, &id_key.id, sizeof(id_key.id));
1706 id_key.creatorid = ps->state.creatorid;
1708 s = pf_find_state_byid(&id_key);
1713 pfsync_state_export(&ps->state, s);
1717 case DIOCGETSTATES: {
1718 struct pfioc_states *ps = (struct pfioc_states *)addr;
1719 struct pf_state *state;
1720 struct pfsync_state *p, *pstore;
1723 if (ps->ps_len == 0) {
1724 nr = pf_status.states;
1725 ps->ps_len = sizeof(struct pfsync_state) * nr;
1729 pstore = kmalloc(sizeof(*pstore), M_TEMP, M_WAITOK);
1733 state = TAILQ_FIRST(&state_list);
1735 if (state->timeout != PFTM_UNLINKED) {
1736 if ((nr+1) * sizeof(*p) > (unsigned)ps->ps_len)
1738 pfsync_state_export(pstore, state);
1739 error = copyout(pstore, p, sizeof(*p));
1741 kfree(pstore, M_TEMP);
1747 state = TAILQ_NEXT(state, entry_list);
1750 ps->ps_len = sizeof(struct pfsync_state) * nr;
1752 kfree(pstore, M_TEMP);
1756 case DIOCGETSTATUS: {
1757 struct pf_status *s = (struct pf_status *)addr;
1758 bcopy(&pf_status, s, sizeof(struct pf_status));
1759 pfi_update_status(s->ifname, s);
1763 case DIOCSETSTATUSIF: {
1764 struct pfioc_if *pi = (struct pfioc_if *)addr;
1766 if (pi->ifname[0] == 0) {
1767 bzero(pf_status.ifname, IFNAMSIZ);
1770 strlcpy(pf_status.ifname, pi->ifname, IFNAMSIZ);
1774 case DIOCCLRSTATUS: {
1775 bzero(pf_status.counters, sizeof(pf_status.counters));
1776 bzero(pf_status.fcounters, sizeof(pf_status.fcounters));
1777 bzero(pf_status.scounters, sizeof(pf_status.scounters));
1778 pf_status.since = time_second;
1779 if (*pf_status.ifname)
1780 pfi_update_status(pf_status.ifname, NULL);
1785 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1786 struct pf_state_key *sk;
1787 struct pf_state *state;
1788 struct pf_state_key_cmp key;
1789 int m = 0, direction = pnl->direction;
1792 /* NATLOOK src and dst are reversed, so reverse sidx/didx */
1793 sidx = (direction == PF_IN) ? 1 : 0;
1794 didx = (direction == PF_IN) ? 0 : 1;
1797 PF_AZERO(&pnl->saddr, pnl->af) ||
1798 PF_AZERO(&pnl->daddr, pnl->af) ||
1799 ((pnl->proto == IPPROTO_TCP ||
1800 pnl->proto == IPPROTO_UDP) &&
1801 (!pnl->dport || !pnl->sport)))
1805 key.proto = pnl->proto;
1806 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
1807 key.port[sidx] = pnl->sport;
1808 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
1809 key.port[didx] = pnl->dport;
1811 state = pf_find_state_all(&key, direction, &m);
1814 error = E2BIG; /* more than one state */
1815 else if (state != NULL) {
1816 sk = state->key[sidx];
1817 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
1818 pnl->rsport = sk->port[sidx];
1819 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
1820 pnl->rdport = sk->port[didx];
1827 case DIOCSETTIMEOUT: {
1828 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1831 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1836 old = pf_default_rule.timeout[pt->timeout];
1837 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
1839 pf_default_rule.timeout[pt->timeout] = pt->seconds;
1840 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
1841 wakeup(pf_purge_thread);
1846 case DIOCGETTIMEOUT: {
1847 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1849 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1853 pt->seconds = pf_default_rule.timeout[pt->timeout];
1857 case DIOCGETLIMIT: {
1858 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1860 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1864 pl->limit = pf_pool_limits[pl->index].limit;
1868 case DIOCSETLIMIT: {
1869 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1872 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1873 pf_pool_limits[pl->index].pp == NULL) {
1878 /* XXX Get an API to set limits on the zone/pool */
1879 old_limit = pf_pool_limits[pl->index].limit;
1880 pf_pool_limits[pl->index].limit = pl->limit;
1881 pl->limit = old_limit;
1885 case DIOCSETDEBUG: {
1886 u_int32_t *level = (u_int32_t *)addr;
1888 pf_status.debug = *level;
1892 case DIOCCLRRULECTRS: {
1893 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
1894 struct pf_ruleset *ruleset = &pf_main_ruleset;
1895 struct pf_rule *rule;
1898 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
1899 rule->evaluations = 0;
1900 rule->packets[0] = rule->packets[1] = 0;
1901 rule->bytes[0] = rule->bytes[1] = 0;
1906 case DIOCGIFSPEED: {
1907 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
1908 struct pf_ifspeed ps;
1911 if (psp->ifname[0] != 0) {
1912 /* Can we completely trust user-land? */
1913 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
1914 ifp = ifunit(ps.ifname);
1916 psp->baudrate = ifp->if_baudrate;
1924 case DIOCSTARTALTQ: {
1925 struct pf_altq *altq;
1927 /* enable all altq interfaces on active list */
1928 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
1929 if (altq->qname[0] == 0) {
1930 error = pf_enable_altq(altq);
1936 pf_altq_running = 1;
1937 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
1941 case DIOCSTOPALTQ: {
1942 struct pf_altq *altq;
1944 /* disable all altq interfaces on active list */
1945 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
1946 if (altq->qname[0] == 0) {
1947 error = pf_disable_altq(altq);
1953 pf_altq_running = 0;
1954 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
1959 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
1960 struct pf_altq *altq, *a;
1962 if (pa->ticket != ticket_altqs_inactive) {
1966 altq = pool_get(&pf_altq_pl, PR_WAITOK|PR_LIMITFAIL);
1971 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
1974 * if this is for a queue, find the discipline and
1975 * copy the necessary fields
1977 if (altq->qname[0] != 0) {
1978 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
1980 pool_put(&pf_altq_pl, altq);
1983 altq->altq_disc = NULL;
1984 TAILQ_FOREACH(a, pf_altqs_inactive, entries) {
1985 if (strncmp(a->ifname, altq->ifname,
1986 IFNAMSIZ) == 0 && a->qname[0] == 0) {
1987 altq->altq_disc = a->altq_disc;
1993 error = altq_add(altq);
1995 pool_put(&pf_altq_pl, altq);
1999 TAILQ_INSERT_TAIL(pf_altqs_inactive, altq, entries);
2000 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2004 case DIOCGETALTQS: {
2005 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2006 struct pf_altq *altq;
2009 TAILQ_FOREACH(altq, pf_altqs_active, entries)
2011 pa->ticket = ticket_altqs_active;
2016 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2017 struct pf_altq *altq;
2020 if (pa->ticket != ticket_altqs_active) {
2025 altq = TAILQ_FIRST(pf_altqs_active);
2026 while ((altq != NULL) && (nr < pa->nr)) {
2027 altq = TAILQ_NEXT(altq, entries);
2034 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2038 case DIOCCHANGEALTQ:
2039 /* CHANGEALTQ not supported yet! */
2043 case DIOCGETQSTATS: {
2044 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2045 struct pf_altq *altq;
2049 if (pq->ticket != ticket_altqs_active) {
2053 nbytes = pq->nbytes;
2055 altq = TAILQ_FIRST(pf_altqs_active);
2056 while ((altq != NULL) && (nr < pq->nr)) {
2057 altq = TAILQ_NEXT(altq, entries);
2064 error = altq_getqstats(altq, pq->buf, &nbytes);
2066 pq->scheduler = altq->scheduler;
2067 pq->nbytes = nbytes;
2073 case DIOCBEGINADDRS: {
2074 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2076 pf_empty_pool(&pf_pabuf);
2077 pp->ticket = ++ticket_pabuf;
2082 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2084 if (pp->ticket != ticket_pabuf) {
2089 if (pp->af == AF_INET) {
2090 error = EAFNOSUPPORT;
2095 if (pp->af == AF_INET6) {
2096 error = EAFNOSUPPORT;
2100 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2101 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2102 pp->addr.addr.type != PF_ADDR_TABLE) {
2106 pa = pool_get(&pf_pooladdr_pl, PR_WAITOK|PR_LIMITFAIL);
2111 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2112 if (pa->ifname[0]) {
2113 pa->kif = pfi_kif_get(pa->ifname);
2114 if (pa->kif == NULL) {
2115 pool_put(&pf_pooladdr_pl, pa);
2119 pfi_kif_ref(pa->kif, PFI_KIF_REF_RULE);
2121 if (pfi_dynaddr_setup(&pa->addr, pp->af)) {
2122 pfi_dynaddr_remove(&pa->addr);
2123 pfi_kif_unref(pa->kif, PFI_KIF_REF_RULE);
2124 pool_put(&pf_pooladdr_pl, pa);
2128 TAILQ_INSERT_TAIL(&pf_pabuf, pa, entries);
2132 case DIOCGETADDRS: {
2133 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2136 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2137 pp->r_num, 0, 1, 0);
2142 TAILQ_FOREACH(pa, &pool->list, entries)
2148 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2151 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2152 pp->r_num, 0, 1, 1);
2157 pa = TAILQ_FIRST(&pool->list);
2158 while ((pa != NULL) && (nr < pp->nr)) {
2159 pa = TAILQ_NEXT(pa, entries);
2166 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2167 pf_addr_copyout(&pp->addr.addr);
2171 case DIOCCHANGEADDR: {
2172 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2173 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2174 struct pf_ruleset *ruleset;
2176 if (pca->action < PF_CHANGE_ADD_HEAD ||
2177 pca->action > PF_CHANGE_REMOVE) {
2181 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2182 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2183 pca->addr.addr.type != PF_ADDR_TABLE) {
2188 ruleset = pf_find_ruleset(pca->anchor);
2189 if (ruleset == NULL) {
2193 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2194 pca->r_num, pca->r_last, 1, 1);
2199 if (pca->action != PF_CHANGE_REMOVE) {
2200 newpa = pool_get(&pf_pooladdr_pl,
2201 PR_WAITOK|PR_LIMITFAIL);
2202 if (newpa == NULL) {
2206 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2208 if (pca->af == AF_INET) {
2209 pool_put(&pf_pooladdr_pl, newpa);
2210 error = EAFNOSUPPORT;
2215 if (pca->af == AF_INET6) {
2216 pool_put(&pf_pooladdr_pl, newpa);
2217 error = EAFNOSUPPORT;
2221 if (newpa->ifname[0]) {
2222 newpa->kif = pfi_kif_get(newpa->ifname);
2223 if (newpa->kif == NULL) {
2224 pool_put(&pf_pooladdr_pl, newpa);
2228 pfi_kif_ref(newpa->kif, PFI_KIF_REF_RULE);
2231 if (pfi_dynaddr_setup(&newpa->addr, pca->af) ||
2232 pf_tbladdr_setup(ruleset, &newpa->addr)) {
2233 pfi_dynaddr_remove(&newpa->addr);
2234 pfi_kif_unref(newpa->kif, PFI_KIF_REF_RULE);
2235 pool_put(&pf_pooladdr_pl, newpa);
2241 if (pca->action == PF_CHANGE_ADD_HEAD)
2242 oldpa = TAILQ_FIRST(&pool->list);
2243 else if (pca->action == PF_CHANGE_ADD_TAIL)
2244 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2248 oldpa = TAILQ_FIRST(&pool->list);
2249 while ((oldpa != NULL) && (i < pca->nr)) {
2250 oldpa = TAILQ_NEXT(oldpa, entries);
2253 if (oldpa == NULL) {
2259 if (pca->action == PF_CHANGE_REMOVE) {
2260 TAILQ_REMOVE(&pool->list, oldpa, entries);
2261 pfi_dynaddr_remove(&oldpa->addr);
2262 pf_tbladdr_remove(&oldpa->addr);
2263 pfi_kif_unref(oldpa->kif, PFI_KIF_REF_RULE);
2264 pool_put(&pf_pooladdr_pl, oldpa);
2267 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2268 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2269 pca->action == PF_CHANGE_ADD_BEFORE)
2270 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2272 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2276 pool->cur = TAILQ_FIRST(&pool->list);
2277 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
2282 case DIOCGETRULESETS: {
2283 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2284 struct pf_ruleset *ruleset;
2285 struct pf_anchor *anchor;
2287 pr->path[sizeof(pr->path) - 1] = 0;
2288 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2293 if (ruleset->anchor == NULL) {
2294 /* XXX kludge for pf_main_ruleset */
2295 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2296 if (anchor->parent == NULL)
2299 RB_FOREACH(anchor, pf_anchor_node,
2300 &ruleset->anchor->children)
2306 case DIOCGETRULESET: {
2307 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2308 struct pf_ruleset *ruleset;
2309 struct pf_anchor *anchor;
2312 pr->path[sizeof(pr->path) - 1] = 0;
2313 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2318 if (ruleset->anchor == NULL) {
2319 /* XXX kludge for pf_main_ruleset */
2320 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2321 if (anchor->parent == NULL && nr++ == pr->nr) {
2322 strlcpy(pr->name, anchor->name,
2327 RB_FOREACH(anchor, pf_anchor_node,
2328 &ruleset->anchor->children)
2329 if (nr++ == pr->nr) {
2330 strlcpy(pr->name, anchor->name,
2340 case DIOCRCLRTABLES: {
2341 struct pfioc_table *io = (struct pfioc_table *)addr;
2343 if (io->pfrio_esize != 0) {
2347 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2348 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2352 case DIOCRADDTABLES: {
2353 struct pfioc_table *io = (struct pfioc_table *)addr;
2355 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2359 error = pfr_add_tables(io->pfrio_buffer, io->pfrio_size,
2360 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2364 case DIOCRDELTABLES: {
2365 struct pfioc_table *io = (struct pfioc_table *)addr;
2367 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2371 error = pfr_del_tables(io->pfrio_buffer, io->pfrio_size,
2372 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2376 case DIOCRGETTABLES: {
2377 struct pfioc_table *io = (struct pfioc_table *)addr;
2379 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2383 error = pfr_get_tables(&io->pfrio_table, io->pfrio_buffer,
2384 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2388 case DIOCRGETTSTATS: {
2389 struct pfioc_table *io = (struct pfioc_table *)addr;
2391 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2395 error = pfr_get_tstats(&io->pfrio_table, io->pfrio_buffer,
2396 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2400 case DIOCRCLRTSTATS: {
2401 struct pfioc_table *io = (struct pfioc_table *)addr;
2403 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2407 error = pfr_clr_tstats(io->pfrio_buffer, io->pfrio_size,
2408 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2412 case DIOCRSETTFLAGS: {
2413 struct pfioc_table *io = (struct pfioc_table *)addr;
2415 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2419 error = pfr_set_tflags(io->pfrio_buffer, io->pfrio_size,
2420 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2421 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2425 case DIOCRCLRADDRS: {
2426 struct pfioc_table *io = (struct pfioc_table *)addr;
2428 if (io->pfrio_esize != 0) {
2432 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2433 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2437 case DIOCRADDADDRS: {
2438 struct pfioc_table *io = (struct pfioc_table *)addr;
2440 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2444 error = pfr_add_addrs(&io->pfrio_table, io->pfrio_buffer,
2445 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2446 PFR_FLAG_USERIOCTL);
2450 case DIOCRDELADDRS: {
2451 struct pfioc_table *io = (struct pfioc_table *)addr;
2453 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2457 error = pfr_del_addrs(&io->pfrio_table, io->pfrio_buffer,
2458 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2459 PFR_FLAG_USERIOCTL);
2463 case DIOCRSETADDRS: {
2464 struct pfioc_table *io = (struct pfioc_table *)addr;
2466 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2470 error = pfr_set_addrs(&io->pfrio_table, io->pfrio_buffer,
2471 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2472 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2473 PFR_FLAG_USERIOCTL, 0);
2477 case DIOCRGETADDRS: {
2478 struct pfioc_table *io = (struct pfioc_table *)addr;
2480 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2484 error = pfr_get_addrs(&io->pfrio_table, io->pfrio_buffer,
2485 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2489 case DIOCRGETASTATS: {
2490 struct pfioc_table *io = (struct pfioc_table *)addr;
2492 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2496 error = pfr_get_astats(&io->pfrio_table, io->pfrio_buffer,
2497 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2501 case DIOCRCLRASTATS: {
2502 struct pfioc_table *io = (struct pfioc_table *)addr;
2504 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2508 error = pfr_clr_astats(&io->pfrio_table, io->pfrio_buffer,
2509 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2510 PFR_FLAG_USERIOCTL);
2514 case DIOCRTSTADDRS: {
2515 struct pfioc_table *io = (struct pfioc_table *)addr;
2517 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2521 error = pfr_tst_addrs(&io->pfrio_table, io->pfrio_buffer,
2522 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2523 PFR_FLAG_USERIOCTL);
2527 case DIOCRINADEFINE: {
2528 struct pfioc_table *io = (struct pfioc_table *)addr;
2530 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2534 error = pfr_ina_define(&io->pfrio_table, io->pfrio_buffer,
2535 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2536 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2541 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2542 error = pf_osfp_add(io);
2547 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2548 error = pf_osfp_get(io);
2553 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2554 struct pfioc_trans_e *ioe;
2555 struct pfr_table *table;
2558 if (io->esize != sizeof(*ioe)) {
2562 ioe = kmalloc(sizeof(*ioe), M_TEMP, M_WAITOK);
2563 table = kmalloc(sizeof(*table), M_TEMP, M_WAITOK);
2564 for (i = 0; i < io->size; i++) {
2565 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2566 kfree(table, M_TEMP);
2571 switch (ioe->rs_num) {
2573 case PF_RULESET_ALTQ:
2574 if (ioe->anchor[0]) {
2575 kfree(table, M_TEMP);
2580 if ((error = pf_begin_altq(&ioe->ticket))) {
2581 kfree(table, M_TEMP);
2587 case PF_RULESET_TABLE:
2588 bzero(table, sizeof(*table));
2589 strlcpy(table->pfrt_anchor, ioe->anchor,
2590 sizeof(table->pfrt_anchor));
2591 if ((error = pfr_ina_begin(table,
2592 &ioe->ticket, NULL, 0))) {
2593 kfree(table, M_TEMP);
2599 if ((error = pf_begin_rules(&ioe->ticket,
2600 ioe->rs_num, ioe->anchor))) {
2601 kfree(table, M_TEMP);
2607 if (copyout(ioe, io->array+i, sizeof(io->array[i]))) {
2608 kfree(table, M_TEMP);
2614 kfree(table, M_TEMP);
2619 case DIOCXROLLBACK: {
2620 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2621 struct pfioc_trans_e *ioe;
2622 struct pfr_table *table;
2625 if (io->esize != sizeof(*ioe)) {
2629 ioe = kmalloc(sizeof(*ioe), M_TEMP, M_WAITOK);
2630 table = kmalloc(sizeof(*table), M_TEMP, M_WAITOK);
2631 for (i = 0; i < io->size; i++) {
2632 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2633 kfree(table, M_TEMP);
2638 switch (ioe->rs_num) {
2640 case PF_RULESET_ALTQ:
2641 if (ioe->anchor[0]) {
2642 kfree(table, M_TEMP);
2647 if ((error = pf_rollback_altq(ioe->ticket))) {
2648 kfree(table, M_TEMP);
2650 goto fail; /* really bad */
2654 case PF_RULESET_TABLE:
2655 bzero(table, sizeof(*table));
2656 strlcpy(table->pfrt_anchor, ioe->anchor,
2657 sizeof(table->pfrt_anchor));
2658 if ((error = pfr_ina_rollback(table,
2659 ioe->ticket, NULL, 0))) {
2660 kfree(table, M_TEMP);
2662 goto fail; /* really bad */
2666 if ((error = pf_rollback_rules(ioe->ticket,
2667 ioe->rs_num, ioe->anchor))) {
2668 kfree(table, M_TEMP);
2670 goto fail; /* really bad */
2675 kfree(table, M_TEMP);
2681 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2682 struct pfioc_trans_e *ioe;
2683 struct pfr_table *table;
2684 struct pf_ruleset *rs;
2687 if (io->esize != sizeof(*ioe)) {
2691 ioe = kmalloc(sizeof(*ioe), M_TEMP, M_WAITOK);
2692 table = kmalloc(sizeof(*table), M_TEMP, M_WAITOK);
2693 /* first makes sure everything will succeed */
2694 for (i = 0; i < io->size; i++) {
2695 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2696 kfree(table, M_TEMP);
2701 switch (ioe->rs_num) {
2703 case PF_RULESET_ALTQ:
2704 if (ioe->anchor[0]) {
2705 kfree(table, M_TEMP);
2710 if (!altqs_inactive_open || ioe->ticket !=
2711 ticket_altqs_inactive) {
2712 kfree(table, M_TEMP);
2719 case PF_RULESET_TABLE:
2720 rs = pf_find_ruleset(ioe->anchor);
2721 if (rs == NULL || !rs->topen || ioe->ticket !=
2723 kfree(table, M_TEMP);
2730 if (ioe->rs_num < 0 || ioe->rs_num >=
2732 kfree(table, M_TEMP);
2737 rs = pf_find_ruleset(ioe->anchor);
2739 !rs->rules[ioe->rs_num].inactive.open ||
2740 rs->rules[ioe->rs_num].inactive.ticket !=
2742 kfree(table, M_TEMP);
2750 /* now do the commit - no errors should happen here */
2751 for (i = 0; i < io->size; i++) {
2752 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2753 kfree(table, M_TEMP);
2758 switch (ioe->rs_num) {
2760 case PF_RULESET_ALTQ:
2761 if ((error = pf_commit_altq(ioe->ticket))) {
2762 kfree(table, M_TEMP);
2764 goto fail; /* really bad */
2768 case PF_RULESET_TABLE:
2769 bzero(table, sizeof(*table));
2770 strlcpy(table->pfrt_anchor, ioe->anchor,
2771 sizeof(table->pfrt_anchor));
2772 if ((error = pfr_ina_commit(table, ioe->ticket,
2774 kfree(table, M_TEMP);
2776 goto fail; /* really bad */
2780 if ((error = pf_commit_rules(ioe->ticket,
2781 ioe->rs_num, ioe->anchor))) {
2782 kfree(table, M_TEMP);
2784 goto fail; /* really bad */
2789 kfree(table, M_TEMP);
2794 case DIOCGETSRCNODES: {
2795 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
2796 struct pf_src_node *n, *p, *pstore;
2798 int space = psn->psn_len;
2801 RB_FOREACH(n, pf_src_tree, &tree_src_tracking)
2803 psn->psn_len = sizeof(struct pf_src_node) * nr;
2807 pstore = kmalloc(sizeof(*pstore), M_TEMP, M_WAITOK);
2809 p = psn->psn_src_nodes;
2810 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
2811 int secs = time_second, diff;
2813 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
2816 bcopy(n, pstore, sizeof(*pstore));
2817 if (n->rule.ptr != NULL)
2818 pstore->rule.nr = n->rule.ptr->nr;
2819 pstore->creation = secs - pstore->creation;
2820 if (pstore->expire > secs)
2821 pstore->expire -= secs;
2825 /* adjust the connection rate estimate */
2826 diff = secs - n->conn_rate.last;
2827 if (diff >= n->conn_rate.seconds)
2828 pstore->conn_rate.count = 0;
2830 pstore->conn_rate.count -=
2831 n->conn_rate.count * diff /
2832 n->conn_rate.seconds;
2834 error = copyout(pstore, p, sizeof(*p));
2836 kfree(pstore, M_TEMP);
2842 psn->psn_len = sizeof(struct pf_src_node) * nr;
2844 kfree(pstore, M_TEMP);
2848 case DIOCCLRSRCNODES: {
2849 struct pf_src_node *n;
2850 struct pf_state *state;
2852 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2853 state->src_node = NULL;
2854 state->nat_src_node = NULL;
2856 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
2860 pf_purge_expired_src_nodes(1);
2861 pf_status.src_nodes = 0;
2865 case DIOCKILLSRCNODES: {
2866 struct pf_src_node *sn;
2868 struct pfioc_src_node_kill *psnk =
2869 (struct pfioc_src_node_kill *)addr;
2872 RB_FOREACH(sn, pf_src_tree, &tree_src_tracking) {
2873 if (PF_MATCHA(psnk->psnk_src.neg,
2874 &psnk->psnk_src.addr.v.a.addr,
2875 &psnk->psnk_src.addr.v.a.mask,
2876 &sn->addr, sn->af) &&
2877 PF_MATCHA(psnk->psnk_dst.neg,
2878 &psnk->psnk_dst.addr.v.a.addr,
2879 &psnk->psnk_dst.addr.v.a.mask,
2880 &sn->raddr, sn->af)) {
2881 /* Handle state to src_node linkage */
2882 if (sn->states != 0) {
2883 RB_FOREACH(s, pf_state_tree_id,
2885 if (s->src_node == sn)
2887 if (s->nat_src_node == sn)
2888 s->nat_src_node = NULL;
2898 pf_purge_expired_src_nodes(1);
2900 psnk->psnk_killed = killed;
2904 case DIOCSETHOSTID: {
2905 u_int32_t *hostid = (u_int32_t *)addr;
2908 pf_status.hostid = karc4random();
2910 pf_status.hostid = *hostid;
2920 case DIOCIGETIFACES: {
2921 struct pfioc_iface *io = (struct pfioc_iface *)addr;
2923 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
2927 error = pfi_get_ifaces(io->pfiio_name, io->pfiio_buffer,
2932 case DIOCSETIFFLAG: {
2933 struct pfioc_iface *io = (struct pfioc_iface *)addr;
2935 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
2939 case DIOCCLRIFFLAG: {
2940 struct pfioc_iface *io = (struct pfioc_iface *)addr;
2942 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
2951 lwkt_reltoken(&pf_token);
2956 * XXX - Check for version missmatch!!!
2959 pf_clear_states(void)
2961 struct pf_state *state;
2963 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2964 state->timeout = PFTM_PURGE;
2965 /* don't send out individual delete messages */
2966 state->sync_flags = PFSTATE_NOSYNC;
2967 pf_unlink_state(state);
2969 pf_status.states = 0;
2972 * XXX This is called on module unload, we do not want to sync that over? */
2974 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
2979 pf_clear_tables(void)
2981 struct pfioc_table io;
2984 bzero(&io, sizeof(io));
2986 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
2993 pf_clear_srcnodes(void)
2995 struct pf_src_node *n;
2996 struct pf_state *state;
2998 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2999 state->src_node = NULL;
3000 state->nat_src_node = NULL;
3002 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3006 pf_purge_expired_src_nodes(0);
3007 pf_status.src_nodes = 0;
3010 * XXX - Check for version missmatch!!!
3014 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3024 pf_status.running = 0;
3026 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn)) != 0) {
3027 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3030 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn)) != 0) {
3031 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3032 break; /* XXX: rollback? */
3034 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn)) != 0) {
3035 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3036 break; /* XXX: rollback? */
3038 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3040 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3041 break; /* XXX: rollback? */
3043 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3045 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3046 break; /* XXX: rollback? */
3049 /* XXX: these should always succeed here */
3050 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3051 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3052 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3053 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3054 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3056 if ((error = pf_clear_tables()) != 0)
3059 if ((error = pf_begin_altq(&t[0])) != 0) {
3060 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3063 pf_commit_altq(t[0]);
3067 pf_clear_srcnodes();
3069 /* status does not use malloced mem so no need to cleanup */
3070 /* fingerprints and interfaces have their own cleanup code */
3077 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3080 * DragonFly's version of pf uses FreeBSD's native host byte ordering
3081 * for ip_len/ip_off. This is why we don't have to change byte order
3082 * like the FreeBSD-5 version does.
3086 lwkt_gettoken(&pf_token);
3088 chk = pf_test(PF_IN, ifp, m, NULL, NULL);
3093 lwkt_reltoken(&pf_token);
3098 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3101 * DragonFly's version of pf uses FreeBSD's native host byte ordering
3102 * for ip_len/ip_off. This is why we don't have to change byte order
3103 * like the FreeBSD-5 version does.
3107 lwkt_gettoken(&pf_token);
3109 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3110 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3111 in_delayed_cksum(*m);
3112 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3114 chk = pf_test(PF_OUT, ifp, m, NULL, NULL);
3119 lwkt_reltoken(&pf_token);
3125 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3128 * IPv6 is not affected by ip_len/ip_off byte order changes.
3132 lwkt_gettoken(&pf_token);
3134 chk = pf_test6(PF_IN, ifp, m, NULL, NULL);
3139 lwkt_reltoken(&pf_token);
3144 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3147 * IPv6 is not affected by ip_len/ip_off byte order changes.
3151 lwkt_gettoken(&pf_token);
3153 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3154 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3155 in_delayed_cksum(*m);
3156 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3158 chk = pf_test6(PF_OUT, ifp, m, NULL, NULL);
3163 lwkt_reltoken(&pf_token);
3171 struct pfil_head *pfh_inet;
3173 struct pfil_head *pfh_inet6;
3176 lwkt_gettoken(&pf_token);
3178 if (pf_pfil_hooked) {
3179 lwkt_reltoken(&pf_token);
3183 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3184 if (pfh_inet == NULL) {
3185 lwkt_reltoken(&pf_token);
3188 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_MPSAFE, pfh_inet);
3189 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_MPSAFE, pfh_inet);
3191 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3192 if (pfh_inet6 == NULL) {
3193 pfil_remove_hook(pf_check_in, NULL, PFIL_IN, pfh_inet);
3194 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT, pfh_inet);
3195 lwkt_reltoken(&pf_token);
3198 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_MPSAFE, pfh_inet6);
3199 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_MPSAFE, pfh_inet6);
3203 lwkt_reltoken(&pf_token);
3210 struct pfil_head *pfh_inet;
3212 struct pfil_head *pfh_inet6;
3215 lwkt_gettoken(&pf_token);
3217 if (pf_pfil_hooked == 0) {
3218 lwkt_reltoken(&pf_token);
3222 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3223 if (pfh_inet == NULL) {
3224 lwkt_reltoken(&pf_token);
3227 pfil_remove_hook(pf_check_in, NULL, PFIL_IN, pfh_inet);
3228 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT, pfh_inet);
3230 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3231 if (pfh_inet6 == NULL) {
3232 lwkt_reltoken(&pf_token);
3235 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN, pfh_inet6);
3236 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT, pfh_inet6);
3240 lwkt_reltoken(&pf_token);
3249 lwkt_gettoken(&pf_token);
3252 lockinit(&pf_mod_lck, "pf task lck", 0, LK_CANRECURSE);
3253 pf_dev = make_dev(&pf_ops, 0, 0, 0, 0600, PF_NAME);
3256 dev_ops_remove_all(&pf_ops);
3257 lockuninit(&pf_mod_lck);
3258 lwkt_reltoken(&pf_token);
3261 lockinit(&pf_consistency_lock, "pfconslck", 0, LK_CANRECURSE);
3262 lwkt_reltoken(&pf_token);
3270 pf_status.running = 0;
3272 lwkt_gettoken(&pf_token);
3274 error = dehook_pf();
3277 * Should not happen!
3278 * XXX Due to error code ESRCH, kldunload will show
3279 * a message like 'No such process'.
3281 kprintf("pfil unregistration fail\n");
3282 lwkt_reltoken(&pf_token);
3287 while (pf_end_threads < 2) {
3288 wakeup_one(pf_purge_thread);
3289 lksleep(pf_purge_thread, &pf_mod_lck, 0, "pftmo", hz);
3296 dev_ops_remove_all(&pf_ops);
3297 lockuninit(&pf_consistency_lock);
3298 lockuninit(&pf_mod_lck);
3299 lwkt_reltoken(&pf_token);
3304 pf_modevent(module_t mod, int type, void *data)
3308 lwkt_gettoken(&pf_token);
3316 error = pf_unload();
3322 lwkt_reltoken(&pf_token);
3326 static moduledata_t pf_mod = {
3331 DECLARE_MODULE(pf, pf_mod, SI_SUB_PSEUDO, SI_ORDER_FIRST);
3332 MODULE_VERSION(pf, PF_MODVER);