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>
61 #include <sys/thread2.h>
64 #include <net/if_types.h>
65 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/ip_icmp.h>
74 #include <net/pf/pfvar.h>
76 #include <net/pf/pfvar.h>
78 #include <net/pf/if_pfsync.h>
81 #include <net/if_pflog.h>
82 #endif /* NPFLOG > 0 */
85 #include <netinet/ip6.h>
86 #include <netinet/in_pcb.h>
90 #include <net/altq/altq.h>
93 #include <machine/limits.h>
95 #include <sys/mutex.h>
97 u_int rt_numfibs = RT_NUMFIBS;
100 struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
101 u_int8_t, u_int8_t, u_int8_t);
103 void pf_mv_pool(struct pf_palist *, struct pf_palist *);
104 void pf_empty_pool(struct pf_palist *);
106 int pf_begin_altq(u_int32_t *);
107 int pf_rollback_altq(u_int32_t);
108 int pf_commit_altq(u_int32_t);
109 int pf_enable_altq(struct pf_altq *);
110 int pf_disable_altq(struct pf_altq *);
112 int pf_begin_rules(u_int32_t *, int, const char *);
113 int pf_rollback_rules(u_int32_t, int, char *);
114 int pf_setup_pfsync_matching(struct pf_ruleset *);
115 void pf_hash_rule(MD5_CTX *, struct pf_rule *);
116 void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
117 int pf_commit_rules(u_int32_t, int, char *);
118 int pf_addr_setup(struct pf_ruleset *,
119 struct pf_addr_wrap *, sa_family_t);
120 void pf_addr_copyout(struct pf_addr_wrap *);
122 struct pf_rule pf_default_rule;
123 struct lock pf_consistency_lock;
125 static int pf_altq_running;
128 #define TAGID_MAX 50000
129 TAILQ_HEAD(pf_tags, pf_tagname) pf_tags = TAILQ_HEAD_INITIALIZER(pf_tags),
130 pf_qids = TAILQ_HEAD_INITIALIZER(pf_qids);
132 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
133 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
135 u_int16_t tagname2tag(struct pf_tags *, char *);
136 void tag2tagname(struct pf_tags *, u_int16_t, char *);
137 void tag_unref(struct pf_tags *, u_int16_t);
138 int pf_rtlabel_add(struct pf_addr_wrap *);
139 void pf_rtlabel_remove(struct pf_addr_wrap *);
140 void pf_rtlabel_copyout(struct pf_addr_wrap *);
142 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) kprintf x
144 static cdev_t pf_dev;
146 static MALLOC_DEFINE(M_PFRULEPL, "pfrulepl", "pf rule pool list");
147 static MALLOC_DEFINE(M_PFALTQPL, "pfaltqpl", "pf altq pool list");
148 static MALLOC_DEFINE(M_PFPOOLADDRPL, "pfpooladdrpl", "pf pool address pool list");
149 static MALLOC_DEFINE(M_PFFRENTPL, "pffrent", "pf frent pool list");
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;
196 int debug_pfugidhack = 0;
197 SYSCTL_INT(_debug, OID_AUTO, pfugidhack, CTLFLAG_RW, &debug_pfugidhack, 0,
198 "Enable/disable pf user/group rules mpsafe hack");
203 u_int32_t *my_timeout = pf_default_rule.timeout;
206 if (!rn_inithead((void **)&pf_maskhead, NULL, 0)) {
207 kprintf("pf mask radix tree create failed\n");
210 kmalloc_create(&pf_state_pl, "pf state pool list");
211 kmalloc_raise_limit(pf_state_pl, 0);
212 kmalloc_create(&pf_frent_pl, "pf fragment pool list");
213 kmalloc_raise_limit(pf_frent_pl, 0);
214 kmalloc_create(&pf_cent_pl, "pf cent pool list");
215 kmalloc_raise_limit(pf_cent_pl, 0);
219 pf_osfp_initialize();
221 pf_pool_limits[PF_LIMIT_STATES].pp = pf_state_pl;
222 pf_pool_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
223 pf_pool_limits[PF_LIMIT_FRAGS].pp = pf_frent_pl;
224 pf_pool_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
225 if (ctob(physmem) <= 100*1024*1024)
226 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit =
227 PFR_KENTRY_HIWAT_SMALL;
229 RB_INIT(&tree_src_tracking);
230 RB_INIT(&pf_anchors);
231 pf_init_ruleset(&pf_main_ruleset);
232 TAILQ_INIT(&pf_altqs[0]);
233 TAILQ_INIT(&pf_altqs[1]);
234 TAILQ_INIT(&pf_pabuf);
235 pf_altqs_active = &pf_altqs[0];
236 pf_altqs_inactive = &pf_altqs[1];
237 TAILQ_INIT(&state_list);
239 /* default rule should never be garbage collected */
240 pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
241 pf_default_rule.action = PF_PASS;
242 pf_default_rule.nr = (uint32_t)(-1);
243 pf_default_rule.rtableid = -1;
245 /* initialize default timeouts */
246 my_timeout[PFTM_TCP_FIRST_PACKET] = 120; /* First TCP packet */
247 my_timeout[PFTM_TCP_OPENING] = 30; /* No response yet */
248 my_timeout[PFTM_TCP_ESTABLISHED] = 24*60*60; /* Established */
249 my_timeout[PFTM_TCP_CLOSING] = 15 * 60; /* Half closed */
250 my_timeout[PFTM_TCP_FIN_WAIT] = 45; /* Got both FINs */
251 my_timeout[PFTM_TCP_CLOSED] = 90; /* Got a RST */
252 my_timeout[PFTM_UDP_FIRST_PACKET] = 60; /* First UDP packet */
253 my_timeout[PFTM_UDP_SINGLE] = 30; /* Unidirectional */
254 my_timeout[PFTM_UDP_MULTIPLE] = 60; /* Bidirectional */
255 my_timeout[PFTM_ICMP_FIRST_PACKET] = 20; /* First ICMP packet */
256 my_timeout[PFTM_ICMP_ERROR_REPLY] = 10; /* Got error response */
257 my_timeout[PFTM_OTHER_FIRST_PACKET] = 60; /* First packet */
258 my_timeout[PFTM_OTHER_SINGLE] = 30; /* Unidirectional */
259 my_timeout[PFTM_OTHER_MULTIPLE] = 60; /* Bidirectional */
260 my_timeout[PFTM_FRAG] = 30; /* Fragment expire */
261 my_timeout[PFTM_INTERVAL] = 10; /* Expire interval */
262 my_timeout[PFTM_SRC_NODE] = 0; /* Source Tracking */
263 my_timeout[PFTM_TS_DIFF] = 30; /* Allowed TS diff */
264 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
265 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
268 bzero(&pf_status, sizeof(pf_status));
269 pf_status.debug = PF_DEBUG_URGENT;
270 /* XXX do our best to avoid a conflict */
271 pf_status.hostid = karc4random();
273 if (kthread_create(pf_purge_thread, NULL, NULL, "pfpurge"))
274 panic("pfpurge thread");
278 pfopen(struct dev_open_args *ap)
280 lwkt_gettoken(&pf_token);
281 cdev_t dev = ap->a_head.a_dev;
282 if (minor(dev) >= 1) {
283 lwkt_reltoken(&pf_token);
286 lwkt_reltoken(&pf_token);
291 pfclose(struct dev_close_args *ap)
293 lwkt_gettoken(&pf_token);
294 cdev_t dev = ap->a_head.a_dev;
295 if (minor(dev) >= 1) {
296 lwkt_reltoken(&pf_token);
299 lwkt_reltoken(&pf_token);
304 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
305 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
306 u_int8_t check_ticket)
308 struct pf_ruleset *ruleset;
309 struct pf_rule *rule;
312 ruleset = pf_find_ruleset(anchor);
315 rs_num = pf_get_ruleset_number(rule_action);
316 if (rs_num >= PF_RULESET_MAX)
319 if (check_ticket && ticket !=
320 ruleset->rules[rs_num].active.ticket)
323 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
326 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
328 if (check_ticket && ticket !=
329 ruleset->rules[rs_num].inactive.ticket)
332 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
335 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
338 while ((rule != NULL) && (rule->nr != rule_number))
339 rule = TAILQ_NEXT(rule, entries);
344 return (&rule->rpool);
348 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
350 struct pf_pooladdr *mv_pool_pa;
352 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
353 TAILQ_REMOVE(poola, mv_pool_pa, entries);
354 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
359 pf_empty_pool(struct pf_palist *poola)
361 struct pf_pooladdr *empty_pool_pa;
363 while ((empty_pool_pa = TAILQ_FIRST(poola)) != NULL) {
364 pfi_dynaddr_remove(&empty_pool_pa->addr);
365 pf_tbladdr_remove(&empty_pool_pa->addr);
366 pfi_kif_unref(empty_pool_pa->kif, PFI_KIF_REF_RULE);
367 TAILQ_REMOVE(poola, empty_pool_pa, entries);
368 kfree(empty_pool_pa, M_PFPOOLADDRPL);
373 pf_rm_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
375 if (rulequeue != NULL) {
376 if (rule->states_cur <= 0) {
378 * XXX - we need to remove the table *before* detaching
379 * the rule to make sure the table code does not delete
380 * the anchor under our feet.
382 pf_tbladdr_remove(&rule->src.addr);
383 pf_tbladdr_remove(&rule->dst.addr);
384 if (rule->overload_tbl)
385 pfr_detach_table(rule->overload_tbl);
387 TAILQ_REMOVE(rulequeue, rule, entries);
388 rule->entries.tqe_prev = NULL;
392 if (rule->states_cur > 0 || rule->src_nodes > 0 ||
393 rule->entries.tqe_prev != NULL)
395 pf_tag_unref(rule->tag);
396 pf_tag_unref(rule->match_tag);
398 if (rule->pqid != rule->qid)
399 pf_qid_unref(rule->pqid);
400 pf_qid_unref(rule->qid);
402 pf_rtlabel_remove(&rule->src.addr);
403 pf_rtlabel_remove(&rule->dst.addr);
404 pfi_dynaddr_remove(&rule->src.addr);
405 pfi_dynaddr_remove(&rule->dst.addr);
406 if (rulequeue == NULL) {
407 pf_tbladdr_remove(&rule->src.addr);
408 pf_tbladdr_remove(&rule->dst.addr);
409 if (rule->overload_tbl)
410 pfr_detach_table(rule->overload_tbl);
412 pfi_kif_unref(rule->kif, PFI_KIF_REF_RULE);
413 pf_anchor_remove(rule);
414 pf_empty_pool(&rule->rpool.list);
415 kfree(rule, M_PFRULEPL);
419 tagname2tag(struct pf_tags *head, char *tagname)
421 struct pf_tagname *tag, *p = NULL;
422 u_int16_t new_tagid = 1;
424 TAILQ_FOREACH(tag, head, entries)
425 if (strcmp(tagname, tag->name) == 0) {
431 * to avoid fragmentation, we do a linear search from the beginning
432 * and take the first free slot we find. if there is none or the list
433 * is empty, append a new entry at the end.
437 if (!TAILQ_EMPTY(head))
438 for (p = TAILQ_FIRST(head); p != NULL &&
439 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
440 new_tagid = p->tag + 1;
442 if (new_tagid > TAGID_MAX)
445 /* allocate and fill new struct pf_tagname */
446 tag = kmalloc(sizeof(*tag), M_TEMP, M_WAITOK);
449 strlcpy(tag->name, tagname, sizeof(tag->name));
450 tag->tag = new_tagid;
453 if (p != NULL) /* insert new entry before p */
454 TAILQ_INSERT_BEFORE(p, tag, entries);
455 else /* either list empty or no free slot in between */
456 TAILQ_INSERT_TAIL(head, tag, entries);
462 tag2tagname(struct pf_tags *head, u_int16_t tagid, char *p)
464 struct pf_tagname *tag;
466 TAILQ_FOREACH(tag, head, entries)
467 if (tag->tag == tagid) {
468 strlcpy(p, tag->name, PF_TAG_NAME_SIZE);
474 tag_unref(struct pf_tags *head, u_int16_t tag)
476 struct pf_tagname *p, *next;
481 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
482 next = TAILQ_NEXT(p, entries);
485 TAILQ_REMOVE(head, p, entries);
494 pf_tagname2tag(char *tagname)
496 return (tagname2tag(&pf_tags, tagname));
500 pf_tag2tagname(u_int16_t tagid, char *p)
502 tag2tagname(&pf_tags, tagid, p);
506 pf_tag_ref(u_int16_t tag)
508 struct pf_tagname *t;
510 TAILQ_FOREACH(t, &pf_tags, entries)
518 pf_tag_unref(u_int16_t tag)
520 tag_unref(&pf_tags, tag);
524 pf_rtlabel_add(struct pf_addr_wrap *a)
530 pf_rtlabel_remove(struct pf_addr_wrap *a)
535 pf_rtlabel_copyout(struct pf_addr_wrap *a)
537 if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel)
538 strlcpy(a->v.rtlabelname, "?", sizeof(a->v.rtlabelname));
543 pf_qname2qid(char *qname)
545 return ((u_int32_t)tagname2tag(&pf_qids, qname));
549 pf_qid2qname(u_int32_t qid, char *p)
551 tag2tagname(&pf_qids, (u_int16_t)qid, p);
555 pf_qid_unref(u_int32_t qid)
557 tag_unref(&pf_qids, (u_int16_t)qid);
561 pf_begin_altq(u_int32_t *ticket)
563 struct pf_altq *altq;
566 /* Purge the old altq list */
567 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
568 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
569 if (altq->qname[0] == 0) {
570 /* detach and destroy the discipline */
571 error = altq_remove(altq);
573 pf_qid_unref(altq->qid);
574 kfree(altq, M_PFALTQPL);
578 *ticket = ++ticket_altqs_inactive;
579 altqs_inactive_open = 1;
584 pf_rollback_altq(u_int32_t ticket)
586 struct pf_altq *altq;
589 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
591 /* Purge the old altq list */
592 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
593 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
594 if (altq->qname[0] == 0) {
595 /* detach and destroy the discipline */
596 error = altq_remove(altq);
598 pf_qid_unref(altq->qid);
599 kfree(altq, M_PFALTQPL);
601 altqs_inactive_open = 0;
606 pf_commit_altq(u_int32_t ticket)
608 struct pf_altqqueue *old_altqs;
609 struct pf_altq *altq;
612 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
615 /* swap altqs, keep the old. */
617 old_altqs = pf_altqs_active;
618 pf_altqs_active = pf_altqs_inactive;
619 pf_altqs_inactive = old_altqs;
620 ticket_altqs_active = ticket_altqs_inactive;
622 /* Attach new disciplines */
623 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
624 if (altq->qname[0] == 0) {
625 /* attach the discipline */
626 error = altq_pfattach(altq);
634 /* Purge the old altq list */
635 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
636 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
637 if (altq->qname[0] == 0) {
638 /* detach and destroy the discipline */
640 error = pf_disable_altq(altq);
641 err = altq_pfdetach(altq);
642 if (err != 0 && error == 0)
644 err = altq_remove(altq);
645 if (err != 0 && error == 0)
648 pf_qid_unref(altq->qid);
649 kfree(altq, M_PFALTQPL);
653 altqs_inactive_open = 0;
658 pf_enable_altq(struct pf_altq *altq)
661 struct tb_profile tb;
664 if ((ifp = ifunit(altq->ifname)) == NULL)
667 if (ifp->if_snd.altq_type != ALTQT_NONE)
668 error = altq_enable(&ifp->if_snd);
670 /* set tokenbucket regulator */
671 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
672 tb.rate = altq->ifbandwidth;
673 tb.depth = altq->tbrsize;
675 error = tbr_set(&ifp->if_snd, &tb);
683 pf_disable_altq(struct pf_altq *altq)
686 struct tb_profile tb;
689 if ((ifp = ifunit(altq->ifname)) == NULL)
693 * when the discipline is no longer referenced, it was overridden
694 * by a new one. if so, just return.
696 if (altq->altq_disc != ifp->if_snd.altq_disc)
699 error = altq_disable(&ifp->if_snd);
702 /* clear tokenbucket regulator */
705 error = tbr_set(&ifp->if_snd, &tb);
714 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
716 struct pf_ruleset *rs;
717 struct pf_rule *rule;
719 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
721 rs = pf_find_or_create_ruleset(anchor);
724 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
725 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
726 rs->rules[rs_num].inactive.rcount--;
728 *ticket = ++rs->rules[rs_num].inactive.ticket;
729 rs->rules[rs_num].inactive.open = 1;
734 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
736 struct pf_ruleset *rs;
737 struct pf_rule *rule;
739 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
741 rs = pf_find_ruleset(anchor);
742 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
743 rs->rules[rs_num].inactive.ticket != ticket)
745 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
746 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
747 rs->rules[rs_num].inactive.rcount--;
749 rs->rules[rs_num].inactive.open = 0;
753 #define PF_MD5_UPD(st, elm) \
754 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
756 #define PF_MD5_UPD_STR(st, elm) \
757 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
759 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
760 (stor) = htonl((st)->elm); \
761 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
764 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
765 (stor) = htons((st)->elm); \
766 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
770 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
772 PF_MD5_UPD(pfr, addr.type);
773 switch (pfr->addr.type) {
774 case PF_ADDR_DYNIFTL:
775 PF_MD5_UPD(pfr, addr.v.ifname);
776 PF_MD5_UPD(pfr, addr.iflags);
779 PF_MD5_UPD(pfr, addr.v.tblname);
781 case PF_ADDR_ADDRMASK:
783 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
784 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
786 case PF_ADDR_RTLABEL:
787 PF_MD5_UPD(pfr, addr.v.rtlabelname);
791 PF_MD5_UPD(pfr, port[0]);
792 PF_MD5_UPD(pfr, port[1]);
793 PF_MD5_UPD(pfr, neg);
794 PF_MD5_UPD(pfr, port_op);
798 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
803 pf_hash_rule_addr(ctx, &rule->src);
804 pf_hash_rule_addr(ctx, &rule->dst);
805 PF_MD5_UPD_STR(rule, label);
806 PF_MD5_UPD_STR(rule, ifname);
807 PF_MD5_UPD_STR(rule, match_tagname);
808 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
809 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
810 PF_MD5_UPD_HTONL(rule, prob, y);
811 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
812 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
813 PF_MD5_UPD(rule, uid.op);
814 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
815 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
816 PF_MD5_UPD(rule, gid.op);
817 PF_MD5_UPD_HTONL(rule, rule_flag, y);
818 PF_MD5_UPD(rule, action);
819 PF_MD5_UPD(rule, direction);
820 PF_MD5_UPD(rule, af);
821 PF_MD5_UPD(rule, quick);
822 PF_MD5_UPD(rule, ifnot);
823 PF_MD5_UPD(rule, match_tag_not);
824 PF_MD5_UPD(rule, natpass);
825 PF_MD5_UPD(rule, keep_state);
826 PF_MD5_UPD(rule, proto);
827 PF_MD5_UPD(rule, type);
828 PF_MD5_UPD(rule, code);
829 PF_MD5_UPD(rule, flags);
830 PF_MD5_UPD(rule, flagset);
831 PF_MD5_UPD(rule, allow_opts);
832 PF_MD5_UPD(rule, rt);
833 PF_MD5_UPD(rule, tos);
837 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
839 struct pf_ruleset *rs;
840 struct pf_rule *rule, **old_array;
841 struct pf_rulequeue *old_rules;
843 u_int32_t old_rcount;
845 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
847 rs = pf_find_ruleset(anchor);
848 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
849 ticket != rs->rules[rs_num].inactive.ticket)
852 /* Calculate checksum for the main ruleset */
853 if (rs == &pf_main_ruleset) {
854 error = pf_setup_pfsync_matching(rs);
859 /* Swap rules, keep the old. */
861 old_rules = rs->rules[rs_num].active.ptr;
862 old_rcount = rs->rules[rs_num].active.rcount;
863 old_array = rs->rules[rs_num].active.ptr_array;
865 rs->rules[rs_num].active.ptr =
866 rs->rules[rs_num].inactive.ptr;
867 rs->rules[rs_num].active.ptr_array =
868 rs->rules[rs_num].inactive.ptr_array;
869 rs->rules[rs_num].active.rcount =
870 rs->rules[rs_num].inactive.rcount;
871 rs->rules[rs_num].inactive.ptr = old_rules;
872 rs->rules[rs_num].inactive.ptr_array = old_array;
873 rs->rules[rs_num].inactive.rcount = old_rcount;
875 rs->rules[rs_num].active.ticket =
876 rs->rules[rs_num].inactive.ticket;
877 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
880 /* Purge the old rule list. */
881 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
882 pf_rm_rule(old_rules, rule);
883 if (rs->rules[rs_num].inactive.ptr_array)
884 kfree(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
885 rs->rules[rs_num].inactive.ptr_array = NULL;
886 rs->rules[rs_num].inactive.rcount = 0;
887 rs->rules[rs_num].inactive.open = 0;
888 pf_remove_if_empty_ruleset(rs);
894 pf_setup_pfsync_matching(struct pf_ruleset *rs)
897 struct pf_rule *rule;
899 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
902 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
903 /* XXX PF_RULESET_SCRUB as well? */
904 if (rs_cnt == PF_RULESET_SCRUB)
907 if (rs->rules[rs_cnt].inactive.ptr_array)
908 kfree(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
909 rs->rules[rs_cnt].inactive.ptr_array = NULL;
911 if (rs->rules[rs_cnt].inactive.rcount) {
912 rs->rules[rs_cnt].inactive.ptr_array =
913 kmalloc(sizeof(caddr_t) *
914 rs->rules[rs_cnt].inactive.rcount,
917 if (!rs->rules[rs_cnt].inactive.ptr_array)
921 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
923 pf_hash_rule(&ctx, rule);
924 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
928 MD5Final(digest, &ctx);
929 memcpy(pf_status.pf_chksum, digest, sizeof(pf_status.pf_chksum));
934 pf_addr_setup(struct pf_ruleset *ruleset, struct pf_addr_wrap *addr,
937 if (pfi_dynaddr_setup(addr, af) ||
938 pf_tbladdr_setup(ruleset, addr))
945 pf_addr_copyout(struct pf_addr_wrap *addr)
947 pfi_dynaddr_copyout(addr);
948 pf_tbladdr_copyout(addr);
949 pf_rtlabel_copyout(addr);
953 pfioctl(struct dev_ioctl_args *ap)
955 u_long cmd = ap->a_cmd;
956 caddr_t addr = ap->a_data;
957 struct pf_pooladdr *pa = NULL;
958 struct pf_pool *pool = NULL;
961 lwkt_gettoken(&pf_token);
963 /* XXX keep in sync with switch() below */
971 case DIOCSETSTATUSIF:
978 case DIOCCLRRULECTRS:
983 case DIOCGETRULESETS:
997 case DIOCGETSRCNODES:
998 case DIOCCLRSRCNODES:
1004 case DIOCRCLRTABLES:
1005 case DIOCRADDTABLES:
1006 case DIOCRDELTABLES:
1007 case DIOCRSETTFLAGS:
1008 if (((struct pfioc_table *)addr)->pfrio_flags &
1010 break; /* dummy operation ok */
1011 lwkt_reltoken(&pf_token);
1014 lwkt_reltoken(&pf_token);
1018 if (!(ap->a_fflag & FWRITE))
1026 case DIOCGETTIMEOUT:
1031 case DIOCGETRULESETS:
1032 case DIOCGETRULESET:
1034 case DIOCRGETTABLES:
1035 case DIOCRGETTSTATS:
1037 case DIOCRGETASTATS:
1040 case DIOCGETSRCNODES:
1041 case DIOCIGETIFACES:
1044 case DIOCRCLRTABLES:
1045 case DIOCRADDTABLES:
1046 case DIOCRDELTABLES:
1047 case DIOCRCLRTSTATS:
1052 case DIOCRSETTFLAGS:
1053 if (((struct pfioc_table *)addr)->pfrio_flags &
1055 break; /* dummy operation ok */
1056 lwkt_reltoken(&pf_token);
1059 if (((struct pfioc_rule *)addr)->action ==
1061 lwkt_reltoken(&pf_token);
1066 lwkt_reltoken(&pf_token);
1073 if (pf_status.running)
1078 DPFPRINTF(PF_DEBUG_MISC,
1079 ("pf: pfil registration fail\n"));
1082 pf_status.running = 1;
1083 pf_status.since = time_second;
1084 if (pf_status.stateid == 0) {
1085 pf_status.stateid = time_second;
1086 pf_status.stateid = pf_status.stateid << 32;
1088 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1093 if (!pf_status.running)
1096 pf_status.running = 0;
1097 error = dehook_pf();
1099 pf_status.running = 1;
1100 DPFPRINTF(PF_DEBUG_MISC,
1101 ("pf: pfil unregistration failed\n"));
1103 pf_status.since = time_second;
1104 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1109 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1110 struct pf_ruleset *ruleset;
1111 struct pf_rule *rule, *tail;
1112 struct pf_pooladdr *pa;
1115 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1116 ruleset = pf_find_ruleset(pr->anchor);
1117 if (ruleset == NULL) {
1121 rs_num = pf_get_ruleset_number(pr->rule.action);
1122 if (rs_num >= PF_RULESET_MAX) {
1126 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1130 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1134 if (pr->pool_ticket != ticket_pabuf) {
1138 rule = kmalloc(sizeof(struct pf_rule), M_PFRULEPL,M_WAITOK);
1143 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1144 rule->cuid = ap->a_cred->cr_ruid;
1146 rule->anchor = NULL;
1148 TAILQ_INIT(&rule->rpool.list);
1149 /* initialize refcounting */
1150 rule->states_cur = 0;
1151 rule->src_nodes = 0;
1152 rule->entries.tqe_prev = NULL;
1154 if (rule->af == AF_INET) {
1155 kfree(rule, M_PFRULEPL);
1156 error = EAFNOSUPPORT;
1161 if (rule->af == AF_INET6) {
1162 kfree(rule, M_PFRULEPL);
1163 error = EAFNOSUPPORT;
1167 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1170 rule->nr = tail->nr + 1;
1173 if (rule->ifname[0]) {
1174 rule->kif = pfi_kif_get(rule->ifname);
1175 if (rule->kif == NULL) {
1176 kfree(rule, M_PFRULEPL);
1180 pfi_kif_ref(rule->kif, PFI_KIF_REF_RULE);
1183 if (rule->rtableid > 0 && rule->rtableid > rt_numfibs)
1188 if (rule->qname[0] != 0) {
1189 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1191 else if (rule->pqname[0] != 0) {
1193 pf_qname2qid(rule->pqname)) == 0)
1196 rule->pqid = rule->qid;
1199 if (rule->tagname[0])
1200 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1202 if (rule->match_tagname[0])
1203 if ((rule->match_tag =
1204 pf_tagname2tag(rule->match_tagname)) == 0)
1206 if (rule->rt && !rule->direction)
1211 if (rule->logif >= PFLOGIFS_MAX)
1214 if (pf_rtlabel_add(&rule->src.addr) ||
1215 pf_rtlabel_add(&rule->dst.addr))
1217 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
1219 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
1221 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1223 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1224 if (pf_tbladdr_setup(ruleset, &pa->addr))
1227 if (rule->overload_tblname[0]) {
1228 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1229 rule->overload_tblname)) == NULL)
1232 rule->overload_tbl->pfrkt_flags |=
1236 pf_mv_pool(&pf_pabuf, &rule->rpool.list);
1237 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1238 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1239 (rule->rt > PF_FASTROUTE)) &&
1240 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1244 pf_rm_rule(NULL, rule);
1247 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1248 rule->evaluations = rule->packets[0] = rule->packets[1] =
1249 rule->bytes[0] = rule->bytes[1] = 0;
1250 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1252 ruleset->rules[rs_num].inactive.rcount++;
1256 case DIOCGETRULES: {
1257 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1258 struct pf_ruleset *ruleset;
1259 struct pf_rule *tail;
1262 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1263 ruleset = pf_find_ruleset(pr->anchor);
1264 if (ruleset == NULL) {
1268 rs_num = pf_get_ruleset_number(pr->rule.action);
1269 if (rs_num >= PF_RULESET_MAX) {
1273 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1276 pr->nr = tail->nr + 1;
1279 pr->ticket = ruleset->rules[rs_num].active.ticket;
1284 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1285 struct pf_ruleset *ruleset;
1286 struct pf_rule *rule;
1289 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1290 ruleset = pf_find_ruleset(pr->anchor);
1291 if (ruleset == NULL) {
1295 rs_num = pf_get_ruleset_number(pr->rule.action);
1296 if (rs_num >= PF_RULESET_MAX) {
1300 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1304 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1305 while ((rule != NULL) && (rule->nr != pr->nr))
1306 rule = TAILQ_NEXT(rule, entries);
1311 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1312 if (pf_anchor_copyout(ruleset, rule, pr)) {
1316 pf_addr_copyout(&pr->rule.src.addr);
1317 pf_addr_copyout(&pr->rule.dst.addr);
1318 for (i = 0; i < PF_SKIP_COUNT; ++i)
1319 if (rule->skip[i].ptr == NULL)
1320 pr->rule.skip[i].nr = (uint32_t)(-1);
1322 pr->rule.skip[i].nr =
1323 rule->skip[i].ptr->nr;
1325 if (pr->action == PF_GET_CLR_CNTR) {
1326 rule->evaluations = 0;
1327 rule->packets[0] = rule->packets[1] = 0;
1328 rule->bytes[0] = rule->bytes[1] = 0;
1329 rule->states_tot = 0;
1334 case DIOCCHANGERULE: {
1335 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1336 struct pf_ruleset *ruleset;
1337 struct pf_rule *oldrule = NULL, *newrule = NULL;
1341 if (!(pcr->action == PF_CHANGE_REMOVE ||
1342 pcr->action == PF_CHANGE_GET_TICKET) &&
1343 pcr->pool_ticket != ticket_pabuf) {
1348 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1349 pcr->action > PF_CHANGE_GET_TICKET) {
1353 ruleset = pf_find_ruleset(pcr->anchor);
1354 if (ruleset == NULL) {
1358 rs_num = pf_get_ruleset_number(pcr->rule.action);
1359 if (rs_num >= PF_RULESET_MAX) {
1364 if (pcr->action == PF_CHANGE_GET_TICKET) {
1365 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1369 ruleset->rules[rs_num].active.ticket) {
1373 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1379 if (pcr->action != PF_CHANGE_REMOVE) {
1380 newrule = kmalloc(sizeof(struct pf_rule), M_PFRULEPL, M_WAITOK|M_NULLOK);
1381 if (newrule == NULL) {
1385 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1386 newrule->cuid = ap->a_cred->cr_ruid;
1388 TAILQ_INIT(&newrule->rpool.list);
1389 /* initialize refcounting */
1390 newrule->states_cur = 0;
1391 newrule->entries.tqe_prev = NULL;
1393 if (newrule->af == AF_INET) {
1394 kfree(newrule, M_PFRULEPL);
1395 error = EAFNOSUPPORT;
1400 if (newrule->af == AF_INET6) {
1401 kfree(newrule, M_PFRULEPL);
1402 error = EAFNOSUPPORT;
1406 if (newrule->ifname[0]) {
1407 newrule->kif = pfi_kif_get(newrule->ifname);
1408 if (newrule->kif == NULL) {
1409 kfree(newrule, M_PFRULEPL);
1413 pfi_kif_ref(newrule->kif, PFI_KIF_REF_RULE);
1415 newrule->kif = NULL;
1417 if (newrule->rtableid > 0 &&
1418 newrule->rtableid > rt_numfibs)
1423 if (newrule->qname[0] != 0) {
1425 pf_qname2qid(newrule->qname)) == 0)
1427 else if (newrule->pqname[0] != 0) {
1428 if ((newrule->pqid =
1429 pf_qname2qid(newrule->pqname)) == 0)
1432 newrule->pqid = newrule->qid;
1435 if (newrule->tagname[0])
1437 pf_tagname2tag(newrule->tagname)) == 0)
1439 if (newrule->match_tagname[0])
1440 if ((newrule->match_tag = pf_tagname2tag(
1441 newrule->match_tagname)) == 0)
1443 if (newrule->rt && !newrule->direction)
1448 if (newrule->logif >= PFLOGIFS_MAX)
1451 if (pf_rtlabel_add(&newrule->src.addr) ||
1452 pf_rtlabel_add(&newrule->dst.addr))
1454 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
1456 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
1458 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1460 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1461 if (pf_tbladdr_setup(ruleset, &pa->addr))
1464 if (newrule->overload_tblname[0]) {
1465 if ((newrule->overload_tbl = pfr_attach_table(
1466 ruleset, newrule->overload_tblname)) ==
1470 newrule->overload_tbl->pfrkt_flags |=
1474 pf_mv_pool(&pf_pabuf, &newrule->rpool.list);
1475 if (((((newrule->action == PF_NAT) ||
1476 (newrule->action == PF_RDR) ||
1477 (newrule->action == PF_BINAT) ||
1478 (newrule->rt > PF_FASTROUTE)) &&
1479 !newrule->anchor)) &&
1480 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1484 pf_rm_rule(NULL, newrule);
1487 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1488 newrule->evaluations = 0;
1489 newrule->packets[0] = newrule->packets[1] = 0;
1490 newrule->bytes[0] = newrule->bytes[1] = 0;
1492 pf_empty_pool(&pf_pabuf);
1494 if (pcr->action == PF_CHANGE_ADD_HEAD)
1495 oldrule = TAILQ_FIRST(
1496 ruleset->rules[rs_num].active.ptr);
1497 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1498 oldrule = TAILQ_LAST(
1499 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1501 oldrule = TAILQ_FIRST(
1502 ruleset->rules[rs_num].active.ptr);
1503 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1504 oldrule = TAILQ_NEXT(oldrule, entries);
1505 if (oldrule == NULL) {
1506 if (newrule != NULL)
1507 pf_rm_rule(NULL, newrule);
1513 if (pcr->action == PF_CHANGE_REMOVE) {
1514 pf_rm_rule(ruleset->rules[rs_num].active.ptr, oldrule);
1515 ruleset->rules[rs_num].active.rcount--;
1517 if (oldrule == NULL)
1519 ruleset->rules[rs_num].active.ptr,
1521 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1522 pcr->action == PF_CHANGE_ADD_BEFORE)
1523 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1526 ruleset->rules[rs_num].active.ptr,
1527 oldrule, newrule, entries);
1528 ruleset->rules[rs_num].active.rcount++;
1532 TAILQ_FOREACH(oldrule,
1533 ruleset->rules[rs_num].active.ptr, entries)
1536 ruleset->rules[rs_num].active.ticket++;
1538 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1539 pf_remove_if_empty_ruleset(ruleset);
1544 case DIOCCLRSTATES: {
1545 struct pf_state *s, *nexts;
1546 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1549 for (s = RB_MIN(pf_state_tree_id, &tree_id); s; s = nexts) {
1550 nexts = RB_NEXT(pf_state_tree_id, &tree_id, s);
1552 if (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1553 s->kif->pfik_name)) {
1554 /* don't send out individual delete messages */
1555 s->sync_flags = PFSTATE_NOSYNC;
1560 psk->psk_killed = killed;
1561 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
1565 case DIOCKILLSTATES: {
1566 struct pf_state *s, *nexts;
1567 struct pf_state_key *sk;
1568 struct pf_addr *srcaddr, *dstaddr;
1569 u_int16_t srcport, dstport;
1570 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1573 if (psk->psk_pfcmp.id) {
1574 if (psk->psk_pfcmp.creatorid == 0)
1575 psk->psk_pfcmp.creatorid = pf_status.hostid;
1576 if ((s = pf_find_state_byid(&psk->psk_pfcmp))) {
1577 /* send immediate delete of state */
1578 pfsync_delete_state(s);
1579 s->sync_flags |= PFSTATE_NOSYNC;
1581 psk->psk_killed = 1;
1586 for (s = RB_MIN(pf_state_tree_id, &tree_id); s;
1588 nexts = RB_NEXT(pf_state_tree_id, &tree_id, s);
1589 sk = s->key[PF_SK_WIRE];
1591 if (s->direction == PF_OUT) {
1592 srcaddr = &sk->addr[1];
1593 dstaddr = &sk->addr[0];
1594 srcport = sk->port[0];
1595 dstport = sk->port[0];
1597 srcaddr = &sk->addr[0];
1598 dstaddr = &sk->addr[1];
1599 srcport = sk->port[0];
1600 dstport = sk->port[0];
1602 if ((!psk->psk_af || sk->af == psk->psk_af)
1603 && (!psk->psk_proto || psk->psk_proto ==
1605 PF_MATCHA(psk->psk_src.neg,
1606 &psk->psk_src.addr.v.a.addr,
1607 &psk->psk_src.addr.v.a.mask,
1609 PF_MATCHA(psk->psk_dst.neg,
1610 &psk->psk_dst.addr.v.a.addr,
1611 &psk->psk_dst.addr.v.a.mask,
1613 (psk->psk_src.port_op == 0 ||
1614 pf_match_port(psk->psk_src.port_op,
1615 psk->psk_src.port[0], psk->psk_src.port[1],
1617 (psk->psk_dst.port_op == 0 ||
1618 pf_match_port(psk->psk_dst.port_op,
1619 psk->psk_dst.port[0], psk->psk_dst.port[1],
1621 (!psk->psk_label[0] || (s->rule.ptr->label[0] &&
1622 !strcmp(psk->psk_label, s->rule.ptr->label))) &&
1623 (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1624 s->kif->pfik_name))) {
1625 /* send immediate delete of state */
1626 pfsync_delete_state(s);
1627 s->sync_flags |= PFSTATE_NOSYNC;
1632 psk->psk_killed = killed;
1636 case DIOCADDSTATE: {
1637 struct pfioc_state *ps = (struct pfioc_state *)addr;
1638 struct pfsync_state *sp = &ps->state;
1640 if (sp->timeout >= PFTM_MAX &&
1641 sp->timeout != PFTM_UNTIL_PACKET) {
1645 error = pfsync_state_import(sp, PFSYNC_SI_IOCTL);
1649 case DIOCGETSTATE: {
1650 struct pfioc_state *ps = (struct pfioc_state *)addr;
1652 struct pf_state_cmp id_key;
1654 bcopy(ps->state.id, &id_key.id, sizeof(id_key.id));
1655 id_key.creatorid = ps->state.creatorid;
1657 s = pf_find_state_byid(&id_key);
1662 pfsync_state_export(&ps->state, s);
1666 case DIOCGETSTATES: {
1667 struct pfioc_states *ps = (struct pfioc_states *)addr;
1668 struct pf_state *state;
1669 struct pfsync_state *p, *pstore;
1672 if (ps->ps_len == 0) {
1673 nr = pf_status.states;
1674 ps->ps_len = sizeof(struct pfsync_state) * nr;
1678 pstore = kmalloc(sizeof(*pstore), M_TEMP, M_WAITOK);
1682 state = TAILQ_FIRST(&state_list);
1684 if (state->timeout != PFTM_UNLINKED) {
1685 if ((nr+1) * sizeof(*p) > (unsigned)ps->ps_len)
1687 pfsync_state_export(pstore, state);
1688 error = copyout(pstore, p, sizeof(*p));
1690 kfree(pstore, M_TEMP);
1696 state = TAILQ_NEXT(state, entry_list);
1699 ps->ps_len = sizeof(struct pfsync_state) * nr;
1701 kfree(pstore, M_TEMP);
1705 case DIOCGETSTATUS: {
1706 struct pf_status *s = (struct pf_status *)addr;
1707 bcopy(&pf_status, s, sizeof(struct pf_status));
1708 pfi_update_status(s->ifname, s);
1712 case DIOCSETSTATUSIF: {
1713 struct pfioc_if *pi = (struct pfioc_if *)addr;
1715 if (pi->ifname[0] == 0) {
1716 bzero(pf_status.ifname, IFNAMSIZ);
1719 strlcpy(pf_status.ifname, pi->ifname, IFNAMSIZ);
1723 case DIOCCLRSTATUS: {
1724 bzero(pf_status.counters, sizeof(pf_status.counters));
1725 bzero(pf_status.fcounters, sizeof(pf_status.fcounters));
1726 bzero(pf_status.scounters, sizeof(pf_status.scounters));
1727 pf_status.since = time_second;
1728 if (*pf_status.ifname)
1729 pfi_update_status(pf_status.ifname, NULL);
1734 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1735 struct pf_state_key *sk;
1736 struct pf_state *state;
1737 struct pf_state_key_cmp key;
1738 int m = 0, direction = pnl->direction;
1741 /* NATLOOK src and dst are reversed, so reverse sidx/didx */
1742 sidx = (direction == PF_IN) ? 1 : 0;
1743 didx = (direction == PF_IN) ? 0 : 1;
1746 PF_AZERO(&pnl->saddr, pnl->af) ||
1747 PF_AZERO(&pnl->daddr, pnl->af) ||
1748 ((pnl->proto == IPPROTO_TCP ||
1749 pnl->proto == IPPROTO_UDP) &&
1750 (!pnl->dport || !pnl->sport)))
1754 key.proto = pnl->proto;
1755 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
1756 key.port[sidx] = pnl->sport;
1757 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
1758 key.port[didx] = pnl->dport;
1760 state = pf_find_state_all(&key, direction, &m);
1763 error = E2BIG; /* more than one state */
1764 else if (state != NULL) {
1765 sk = state->key[sidx];
1766 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
1767 pnl->rsport = sk->port[sidx];
1768 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
1769 pnl->rdport = sk->port[didx];
1776 case DIOCSETTIMEOUT: {
1777 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1780 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1785 old = pf_default_rule.timeout[pt->timeout];
1786 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
1788 pf_default_rule.timeout[pt->timeout] = pt->seconds;
1789 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
1790 wakeup(pf_purge_thread);
1795 case DIOCGETTIMEOUT: {
1796 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1798 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1802 pt->seconds = pf_default_rule.timeout[pt->timeout];
1806 case DIOCGETLIMIT: {
1807 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1809 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1813 pl->limit = pf_pool_limits[pl->index].limit;
1817 case DIOCSETLIMIT: {
1818 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1821 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1822 pf_pool_limits[pl->index].pp == NULL) {
1827 /* XXX Get an API to set limits on the zone/pool */
1828 old_limit = pf_pool_limits[pl->index].limit;
1829 pf_pool_limits[pl->index].limit = pl->limit;
1830 pl->limit = old_limit;
1834 case DIOCSETDEBUG: {
1835 u_int32_t *level = (u_int32_t *)addr;
1837 pf_status.debug = *level;
1841 case DIOCCLRRULECTRS: {
1842 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
1843 struct pf_ruleset *ruleset = &pf_main_ruleset;
1844 struct pf_rule *rule;
1847 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
1848 rule->evaluations = 0;
1849 rule->packets[0] = rule->packets[1] = 0;
1850 rule->bytes[0] = rule->bytes[1] = 0;
1855 case DIOCGIFSPEED: {
1856 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
1857 struct pf_ifspeed ps;
1860 if (psp->ifname[0] != 0) {
1861 /* Can we completely trust user-land? */
1862 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
1863 ifp = ifunit(ps.ifname);
1865 psp->baudrate = ifp->if_baudrate;
1873 case DIOCSTARTALTQ: {
1874 struct pf_altq *altq;
1876 /* enable all altq interfaces on active list */
1877 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
1878 if (altq->qname[0] == 0) {
1879 error = pf_enable_altq(altq);
1885 pf_altq_running = 1;
1886 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
1890 case DIOCSTOPALTQ: {
1891 struct pf_altq *altq;
1893 /* disable all altq interfaces on active list */
1894 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
1895 if (altq->qname[0] == 0) {
1896 error = pf_disable_altq(altq);
1902 pf_altq_running = 0;
1903 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
1908 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
1909 struct pf_altq *altq, *a;
1911 if (pa->ticket != ticket_altqs_inactive) {
1915 altq = kmalloc(sizeof(struct pf_altq), M_PFALTQPL, M_WAITOK|M_NULLOK);
1920 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
1923 * if this is for a queue, find the discipline and
1924 * copy the necessary fields
1926 if (altq->qname[0] != 0) {
1927 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
1929 kfree(altq, M_PFALTQPL);
1932 altq->altq_disc = NULL;
1933 TAILQ_FOREACH(a, pf_altqs_inactive, entries) {
1934 if (strncmp(a->ifname, altq->ifname,
1935 IFNAMSIZ) == 0 && a->qname[0] == 0) {
1936 altq->altq_disc = a->altq_disc;
1942 error = altq_add(altq);
1944 kfree(altq, M_PFALTQPL);
1948 TAILQ_INSERT_TAIL(pf_altqs_inactive, altq, entries);
1949 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
1953 case DIOCGETALTQS: {
1954 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
1955 struct pf_altq *altq;
1958 TAILQ_FOREACH(altq, pf_altqs_active, entries)
1960 pa->ticket = ticket_altqs_active;
1965 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
1966 struct pf_altq *altq;
1969 if (pa->ticket != ticket_altqs_active) {
1974 altq = TAILQ_FIRST(pf_altqs_active);
1975 while ((altq != NULL) && (nr < pa->nr)) {
1976 altq = TAILQ_NEXT(altq, entries);
1983 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
1987 case DIOCCHANGEALTQ:
1988 /* CHANGEALTQ not supported yet! */
1992 case DIOCGETQSTATS: {
1993 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
1994 struct pf_altq *altq;
1998 if (pq->ticket != ticket_altqs_active) {
2002 nbytes = pq->nbytes;
2004 altq = TAILQ_FIRST(pf_altqs_active);
2005 while ((altq != NULL) && (nr < pq->nr)) {
2006 altq = TAILQ_NEXT(altq, entries);
2013 error = altq_getqstats(altq, pq->buf, &nbytes);
2015 pq->scheduler = altq->scheduler;
2016 pq->nbytes = nbytes;
2022 case DIOCBEGINADDRS: {
2023 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2025 pf_empty_pool(&pf_pabuf);
2026 pp->ticket = ++ticket_pabuf;
2031 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2033 if (pp->ticket != ticket_pabuf) {
2038 if (pp->af == AF_INET) {
2039 error = EAFNOSUPPORT;
2044 if (pp->af == AF_INET6) {
2045 error = EAFNOSUPPORT;
2049 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2050 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2051 pp->addr.addr.type != PF_ADDR_TABLE) {
2055 pa = kmalloc(sizeof(struct pf_altq), M_PFPOOLADDRPL, M_WAITOK|M_NULLOK);
2060 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2061 if (pa->ifname[0]) {
2062 pa->kif = pfi_kif_get(pa->ifname);
2063 if (pa->kif == NULL) {
2064 kfree(ap, M_PFPOOLADDRPL);
2068 pfi_kif_ref(pa->kif, PFI_KIF_REF_RULE);
2070 if (pfi_dynaddr_setup(&pa->addr, pp->af)) {
2071 pfi_dynaddr_remove(&pa->addr);
2072 pfi_kif_unref(pa->kif, PFI_KIF_REF_RULE);
2073 kfree(pa, M_PFPOOLADDRPL);
2077 TAILQ_INSERT_TAIL(&pf_pabuf, pa, entries);
2081 case DIOCGETADDRS: {
2082 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2085 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2086 pp->r_num, 0, 1, 0);
2091 TAILQ_FOREACH(pa, &pool->list, entries)
2097 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2100 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2101 pp->r_num, 0, 1, 1);
2106 pa = TAILQ_FIRST(&pool->list);
2107 while ((pa != NULL) && (nr < pp->nr)) {
2108 pa = TAILQ_NEXT(pa, entries);
2115 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2116 pf_addr_copyout(&pp->addr.addr);
2120 case DIOCCHANGEADDR: {
2121 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2122 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2123 struct pf_ruleset *ruleset;
2125 if (pca->action < PF_CHANGE_ADD_HEAD ||
2126 pca->action > PF_CHANGE_REMOVE) {
2130 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2131 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2132 pca->addr.addr.type != PF_ADDR_TABLE) {
2137 ruleset = pf_find_ruleset(pca->anchor);
2138 if (ruleset == NULL) {
2142 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2143 pca->r_num, pca->r_last, 1, 1);
2148 if (pca->action != PF_CHANGE_REMOVE) {
2149 newpa = kmalloc(sizeof(struct pf_pooladdr),
2150 M_PFPOOLADDRPL, M_WAITOK|M_NULLOK);
2151 if (newpa == NULL) {
2155 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2157 if (pca->af == AF_INET) {
2158 kfree(newpa, M_PFPOOLADDRPL);
2159 error = EAFNOSUPPORT;
2164 if (pca->af == AF_INET6) {
2165 kfree(newpa, M_PFPOOLADDRPL);
2166 error = EAFNOSUPPORT;
2170 if (newpa->ifname[0]) {
2171 newpa->kif = pfi_kif_get(newpa->ifname);
2172 if (newpa->kif == NULL) {
2173 kfree(newpa, M_PFPOOLADDRPL);
2177 pfi_kif_ref(newpa->kif, PFI_KIF_REF_RULE);
2180 if (pfi_dynaddr_setup(&newpa->addr, pca->af) ||
2181 pf_tbladdr_setup(ruleset, &newpa->addr)) {
2182 pfi_dynaddr_remove(&newpa->addr);
2183 pfi_kif_unref(newpa->kif, PFI_KIF_REF_RULE);
2184 kfree(newpa, M_PFPOOLADDRPL);
2190 if (pca->action == PF_CHANGE_ADD_HEAD)
2191 oldpa = TAILQ_FIRST(&pool->list);
2192 else if (pca->action == PF_CHANGE_ADD_TAIL)
2193 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2197 oldpa = TAILQ_FIRST(&pool->list);
2198 while ((oldpa != NULL) && (i < pca->nr)) {
2199 oldpa = TAILQ_NEXT(oldpa, entries);
2202 if (oldpa == NULL) {
2208 if (pca->action == PF_CHANGE_REMOVE) {
2209 TAILQ_REMOVE(&pool->list, oldpa, entries);
2210 pfi_dynaddr_remove(&oldpa->addr);
2211 pf_tbladdr_remove(&oldpa->addr);
2212 pfi_kif_unref(oldpa->kif, PFI_KIF_REF_RULE);
2213 kfree(oldpa, M_PFPOOLADDRPL);
2216 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2217 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2218 pca->action == PF_CHANGE_ADD_BEFORE)
2219 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2221 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2225 pool->cur = TAILQ_FIRST(&pool->list);
2226 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
2231 case DIOCGETRULESETS: {
2232 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2233 struct pf_ruleset *ruleset;
2234 struct pf_anchor *anchor;
2236 pr->path[sizeof(pr->path) - 1] = 0;
2237 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2242 if (ruleset->anchor == NULL) {
2243 /* XXX kludge for pf_main_ruleset */
2244 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2245 if (anchor->parent == NULL)
2248 RB_FOREACH(anchor, pf_anchor_node,
2249 &ruleset->anchor->children)
2255 case DIOCGETRULESET: {
2256 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2257 struct pf_ruleset *ruleset;
2258 struct pf_anchor *anchor;
2261 pr->path[sizeof(pr->path) - 1] = 0;
2262 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2267 if (ruleset->anchor == NULL) {
2268 /* XXX kludge for pf_main_ruleset */
2269 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2270 if (anchor->parent == NULL && nr++ == pr->nr) {
2271 strlcpy(pr->name, anchor->name,
2276 RB_FOREACH(anchor, pf_anchor_node,
2277 &ruleset->anchor->children)
2278 if (nr++ == pr->nr) {
2279 strlcpy(pr->name, anchor->name,
2289 case DIOCRCLRTABLES: {
2290 struct pfioc_table *io = (struct pfioc_table *)addr;
2292 if (io->pfrio_esize != 0) {
2296 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2297 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2301 case DIOCRADDTABLES: {
2302 struct pfioc_table *io = (struct pfioc_table *)addr;
2304 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2308 error = pfr_add_tables(io->pfrio_buffer, io->pfrio_size,
2309 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2313 case DIOCRDELTABLES: {
2314 struct pfioc_table *io = (struct pfioc_table *)addr;
2316 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2320 error = pfr_del_tables(io->pfrio_buffer, io->pfrio_size,
2321 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2325 case DIOCRGETTABLES: {
2326 struct pfioc_table *io = (struct pfioc_table *)addr;
2328 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2332 error = pfr_get_tables(&io->pfrio_table, io->pfrio_buffer,
2333 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2337 case DIOCRGETTSTATS: {
2338 struct pfioc_table *io = (struct pfioc_table *)addr;
2340 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2344 error = pfr_get_tstats(&io->pfrio_table, io->pfrio_buffer,
2345 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2349 case DIOCRCLRTSTATS: {
2350 struct pfioc_table *io = (struct pfioc_table *)addr;
2352 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2356 error = pfr_clr_tstats(io->pfrio_buffer, io->pfrio_size,
2357 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2361 case DIOCRSETTFLAGS: {
2362 struct pfioc_table *io = (struct pfioc_table *)addr;
2364 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2368 error = pfr_set_tflags(io->pfrio_buffer, io->pfrio_size,
2369 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2370 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2374 case DIOCRCLRADDRS: {
2375 struct pfioc_table *io = (struct pfioc_table *)addr;
2377 if (io->pfrio_esize != 0) {
2381 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2382 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2386 case DIOCRADDADDRS: {
2387 struct pfioc_table *io = (struct pfioc_table *)addr;
2389 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2393 error = pfr_add_addrs(&io->pfrio_table, io->pfrio_buffer,
2394 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2395 PFR_FLAG_USERIOCTL);
2399 case DIOCRDELADDRS: {
2400 struct pfioc_table *io = (struct pfioc_table *)addr;
2402 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2406 error = pfr_del_addrs(&io->pfrio_table, io->pfrio_buffer,
2407 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2408 PFR_FLAG_USERIOCTL);
2412 case DIOCRSETADDRS: {
2413 struct pfioc_table *io = (struct pfioc_table *)addr;
2415 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2419 error = pfr_set_addrs(&io->pfrio_table, io->pfrio_buffer,
2420 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2421 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2422 PFR_FLAG_USERIOCTL, 0);
2426 case DIOCRGETADDRS: {
2427 struct pfioc_table *io = (struct pfioc_table *)addr;
2429 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2433 error = pfr_get_addrs(&io->pfrio_table, io->pfrio_buffer,
2434 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2438 case DIOCRGETASTATS: {
2439 struct pfioc_table *io = (struct pfioc_table *)addr;
2441 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2445 error = pfr_get_astats(&io->pfrio_table, io->pfrio_buffer,
2446 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2450 case DIOCRCLRASTATS: {
2451 struct pfioc_table *io = (struct pfioc_table *)addr;
2453 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2457 error = pfr_clr_astats(&io->pfrio_table, io->pfrio_buffer,
2458 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2459 PFR_FLAG_USERIOCTL);
2463 case DIOCRTSTADDRS: {
2464 struct pfioc_table *io = (struct pfioc_table *)addr;
2466 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2470 error = pfr_tst_addrs(&io->pfrio_table, io->pfrio_buffer,
2471 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2472 PFR_FLAG_USERIOCTL);
2476 case DIOCRINADEFINE: {
2477 struct pfioc_table *io = (struct pfioc_table *)addr;
2479 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2483 error = pfr_ina_define(&io->pfrio_table, io->pfrio_buffer,
2484 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2485 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2490 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2491 error = pf_osfp_add(io);
2496 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2497 error = pf_osfp_get(io);
2502 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2503 struct pfioc_trans_e *ioe;
2504 struct pfr_table *table;
2507 if (io->esize != sizeof(*ioe)) {
2511 ioe = kmalloc(sizeof(*ioe), M_TEMP, M_WAITOK);
2512 table = kmalloc(sizeof(*table), M_TEMP, M_WAITOK);
2513 for (i = 0; i < io->size; i++) {
2514 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2515 kfree(table, M_TEMP);
2520 switch (ioe->rs_num) {
2522 case PF_RULESET_ALTQ:
2523 if (ioe->anchor[0]) {
2524 kfree(table, M_TEMP);
2529 if ((error = pf_begin_altq(&ioe->ticket))) {
2530 kfree(table, M_TEMP);
2536 case PF_RULESET_TABLE:
2537 bzero(table, sizeof(*table));
2538 strlcpy(table->pfrt_anchor, ioe->anchor,
2539 sizeof(table->pfrt_anchor));
2540 if ((error = pfr_ina_begin(table,
2541 &ioe->ticket, NULL, 0))) {
2542 kfree(table, M_TEMP);
2548 if ((error = pf_begin_rules(&ioe->ticket,
2549 ioe->rs_num, ioe->anchor))) {
2550 kfree(table, M_TEMP);
2556 if (copyout(ioe, io->array+i, sizeof(io->array[i]))) {
2557 kfree(table, M_TEMP);
2563 kfree(table, M_TEMP);
2568 case DIOCXROLLBACK: {
2569 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2570 struct pfioc_trans_e *ioe;
2571 struct pfr_table *table;
2574 if (io->esize != sizeof(*ioe)) {
2578 ioe = kmalloc(sizeof(*ioe), M_TEMP, M_WAITOK);
2579 table = kmalloc(sizeof(*table), M_TEMP, M_WAITOK);
2580 for (i = 0; i < io->size; i++) {
2581 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2582 kfree(table, M_TEMP);
2587 switch (ioe->rs_num) {
2589 case PF_RULESET_ALTQ:
2590 if (ioe->anchor[0]) {
2591 kfree(table, M_TEMP);
2596 if ((error = pf_rollback_altq(ioe->ticket))) {
2597 kfree(table, M_TEMP);
2599 goto fail; /* really bad */
2603 case PF_RULESET_TABLE:
2604 bzero(table, sizeof(*table));
2605 strlcpy(table->pfrt_anchor, ioe->anchor,
2606 sizeof(table->pfrt_anchor));
2607 if ((error = pfr_ina_rollback(table,
2608 ioe->ticket, NULL, 0))) {
2609 kfree(table, M_TEMP);
2611 goto fail; /* really bad */
2615 if ((error = pf_rollback_rules(ioe->ticket,
2616 ioe->rs_num, ioe->anchor))) {
2617 kfree(table, M_TEMP);
2619 goto fail; /* really bad */
2624 kfree(table, M_TEMP);
2630 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2631 struct pfioc_trans_e *ioe;
2632 struct pfr_table *table;
2633 struct pf_ruleset *rs;
2636 if (io->esize != sizeof(*ioe)) {
2640 ioe = kmalloc(sizeof(*ioe), M_TEMP, M_WAITOK);
2641 table = kmalloc(sizeof(*table), M_TEMP, M_WAITOK);
2642 /* first makes sure everything will succeed */
2643 for (i = 0; i < io->size; i++) {
2644 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2645 kfree(table, M_TEMP);
2650 switch (ioe->rs_num) {
2652 case PF_RULESET_ALTQ:
2653 if (ioe->anchor[0]) {
2654 kfree(table, M_TEMP);
2659 if (!altqs_inactive_open || ioe->ticket !=
2660 ticket_altqs_inactive) {
2661 kfree(table, M_TEMP);
2668 case PF_RULESET_TABLE:
2669 rs = pf_find_ruleset(ioe->anchor);
2670 if (rs == NULL || !rs->topen || ioe->ticket !=
2672 kfree(table, M_TEMP);
2679 if (ioe->rs_num < 0 || ioe->rs_num >=
2681 kfree(table, M_TEMP);
2686 rs = pf_find_ruleset(ioe->anchor);
2688 !rs->rules[ioe->rs_num].inactive.open ||
2689 rs->rules[ioe->rs_num].inactive.ticket !=
2691 kfree(table, M_TEMP);
2699 /* now do the commit - no errors should happen here */
2700 for (i = 0; i < io->size; i++) {
2701 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
2702 kfree(table, M_TEMP);
2707 switch (ioe->rs_num) {
2709 case PF_RULESET_ALTQ:
2710 if ((error = pf_commit_altq(ioe->ticket))) {
2711 kfree(table, M_TEMP);
2713 goto fail; /* really bad */
2717 case PF_RULESET_TABLE:
2718 bzero(table, sizeof(*table));
2719 strlcpy(table->pfrt_anchor, ioe->anchor,
2720 sizeof(table->pfrt_anchor));
2721 if ((error = pfr_ina_commit(table, ioe->ticket,
2723 kfree(table, M_TEMP);
2725 goto fail; /* really bad */
2729 if ((error = pf_commit_rules(ioe->ticket,
2730 ioe->rs_num, ioe->anchor))) {
2731 kfree(table, M_TEMP);
2733 goto fail; /* really bad */
2738 kfree(table, M_TEMP);
2743 case DIOCGETSRCNODES: {
2744 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
2745 struct pf_src_node *n, *p, *pstore;
2747 int space = psn->psn_len;
2750 RB_FOREACH(n, pf_src_tree, &tree_src_tracking)
2752 psn->psn_len = sizeof(struct pf_src_node) * nr;
2756 pstore = kmalloc(sizeof(*pstore), M_TEMP, M_WAITOK);
2758 p = psn->psn_src_nodes;
2759 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
2760 int secs = time_second, diff;
2762 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
2765 bcopy(n, pstore, sizeof(*pstore));
2766 if (n->rule.ptr != NULL)
2767 pstore->rule.nr = n->rule.ptr->nr;
2768 pstore->creation = secs - pstore->creation;
2769 if (pstore->expire > secs)
2770 pstore->expire -= secs;
2774 /* adjust the connection rate estimate */
2775 diff = secs - n->conn_rate.last;
2776 if (diff >= n->conn_rate.seconds)
2777 pstore->conn_rate.count = 0;
2779 pstore->conn_rate.count -=
2780 n->conn_rate.count * diff /
2781 n->conn_rate.seconds;
2783 error = copyout(pstore, p, sizeof(*p));
2785 kfree(pstore, M_TEMP);
2791 psn->psn_len = sizeof(struct pf_src_node) * nr;
2793 kfree(pstore, M_TEMP);
2797 case DIOCCLRSRCNODES: {
2798 struct pf_src_node *n;
2799 struct pf_state *state;
2801 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2802 state->src_node = NULL;
2803 state->nat_src_node = NULL;
2805 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
2809 pf_purge_expired_src_nodes(1);
2810 pf_status.src_nodes = 0;
2814 case DIOCKILLSRCNODES: {
2815 struct pf_src_node *sn;
2817 struct pfioc_src_node_kill *psnk =
2818 (struct pfioc_src_node_kill *)addr;
2821 RB_FOREACH(sn, pf_src_tree, &tree_src_tracking) {
2822 if (PF_MATCHA(psnk->psnk_src.neg,
2823 &psnk->psnk_src.addr.v.a.addr,
2824 &psnk->psnk_src.addr.v.a.mask,
2825 &sn->addr, sn->af) &&
2826 PF_MATCHA(psnk->psnk_dst.neg,
2827 &psnk->psnk_dst.addr.v.a.addr,
2828 &psnk->psnk_dst.addr.v.a.mask,
2829 &sn->raddr, sn->af)) {
2830 /* Handle state to src_node linkage */
2831 if (sn->states != 0) {
2832 RB_FOREACH(s, pf_state_tree_id,
2834 if (s->src_node == sn)
2836 if (s->nat_src_node == sn)
2837 s->nat_src_node = NULL;
2847 pf_purge_expired_src_nodes(1);
2849 psnk->psnk_killed = killed;
2853 case DIOCSETHOSTID: {
2854 u_int32_t *hostid = (u_int32_t *)addr;
2857 pf_status.hostid = karc4random();
2859 pf_status.hostid = *hostid;
2869 case DIOCIGETIFACES: {
2870 struct pfioc_iface *io = (struct pfioc_iface *)addr;
2872 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
2876 error = pfi_get_ifaces(io->pfiio_name, io->pfiio_buffer,
2881 case DIOCSETIFFLAG: {
2882 struct pfioc_iface *io = (struct pfioc_iface *)addr;
2884 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
2888 case DIOCCLRIFFLAG: {
2889 struct pfioc_iface *io = (struct pfioc_iface *)addr;
2891 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
2900 lwkt_reltoken(&pf_token);
2905 * XXX - Check for version missmatch!!!
2908 pf_clear_states(void)
2910 struct pf_state *s, *nexts;
2913 for (s = RB_MIN(pf_state_tree_id, &tree_id); s; s = nexts) {
2914 nexts = RB_NEXT(pf_state_tree_id, &tree_id, s);
2916 /* don't send out individual delete messages */
2917 s->sync_flags = PFSTATE_NOSYNC;
2925 * XXX This is called on module unload, we do not want to sync that over? */
2927 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
2932 pf_clear_tables(void)
2934 struct pfioc_table io;
2937 bzero(&io, sizeof(io));
2939 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
2946 pf_clear_srcnodes(void)
2948 struct pf_src_node *n;
2949 struct pf_state *state;
2951 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2952 state->src_node = NULL;
2953 state->nat_src_node = NULL;
2955 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
2959 pf_purge_expired_src_nodes(0);
2960 pf_status.src_nodes = 0;
2964 * XXX - Check for version missmatch!!!
2968 * Duplicate pfctl -Fa operation to get rid of as much as we can.
2978 pf_status.running = 0;
2979 error = dehook_pf();
2981 pf_status.running = 1;
2982 DPFPRINTF(PF_DEBUG_MISC,
2983 ("pf: pfil unregistration failed\n"));
2987 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn)) != 0) {
2988 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
2991 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn)) != 0) {
2992 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
2993 break; /* XXX: rollback? */
2995 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn)) != 0) {
2996 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
2997 break; /* XXX: rollback? */
2999 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3001 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3002 break; /* XXX: rollback? */
3004 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3006 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3007 break; /* XXX: rollback? */
3010 /* XXX: these should always succeed here */
3011 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3012 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3013 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3014 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3015 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3017 if ((error = pf_clear_tables()) != 0)
3020 if ((error = pf_begin_altq(&t[0])) != 0) {
3021 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3024 pf_commit_altq(t[0]);
3027 pf_clear_srcnodes();
3029 /* status does not use malloced mem so no need to cleanup */
3030 /* fingerprints and interfaces have their own cleanup code */
3036 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3039 * DragonFly's version of pf uses FreeBSD's native host byte ordering
3040 * for ip_len/ip_off. This is why we don't have to change byte order
3041 * like the FreeBSD-5 version does.
3045 lwkt_gettoken(&pf_token);
3047 chk = pf_test(PF_IN, ifp, m, NULL, NULL);
3052 lwkt_reltoken(&pf_token);
3057 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3060 * DragonFly's version of pf uses FreeBSD's native host byte ordering
3061 * for ip_len/ip_off. This is why we don't have to change byte order
3062 * like the FreeBSD-5 version does.
3066 lwkt_gettoken(&pf_token);
3068 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3069 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3070 in_delayed_cksum(*m);
3071 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3073 chk = pf_test(PF_OUT, ifp, m, NULL, NULL);
3078 lwkt_reltoken(&pf_token);
3084 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3087 * IPv6 is not affected by ip_len/ip_off byte order changes.
3091 lwkt_gettoken(&pf_token);
3093 chk = pf_test6(PF_IN, ifp, m, NULL, NULL);
3098 lwkt_reltoken(&pf_token);
3103 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir)
3106 * IPv6 is not affected by ip_len/ip_off byte order changes.
3110 lwkt_gettoken(&pf_token);
3112 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3113 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3114 in_delayed_cksum(*m);
3115 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3117 chk = pf_test6(PF_OUT, ifp, m, NULL, NULL);
3122 lwkt_reltoken(&pf_token);
3130 struct pfil_head *pfh_inet;
3132 struct pfil_head *pfh_inet6;
3135 lwkt_gettoken(&pf_token);
3137 if (pf_pfil_hooked) {
3138 lwkt_reltoken(&pf_token);
3142 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3143 if (pfh_inet == NULL) {
3144 lwkt_reltoken(&pf_token);
3147 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_MPSAFE, pfh_inet);
3148 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_MPSAFE, pfh_inet);
3150 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3151 if (pfh_inet6 == NULL) {
3152 pfil_remove_hook(pf_check_in, NULL, PFIL_IN, pfh_inet);
3153 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT, pfh_inet);
3154 lwkt_reltoken(&pf_token);
3157 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_MPSAFE, pfh_inet6);
3158 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_MPSAFE, pfh_inet6);
3162 lwkt_reltoken(&pf_token);
3169 struct pfil_head *pfh_inet;
3171 struct pfil_head *pfh_inet6;
3174 lwkt_gettoken(&pf_token);
3176 if (pf_pfil_hooked == 0) {
3177 lwkt_reltoken(&pf_token);
3181 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3182 if (pfh_inet == NULL) {
3183 lwkt_reltoken(&pf_token);
3186 pfil_remove_hook(pf_check_in, NULL, PFIL_IN, pfh_inet);
3187 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT, pfh_inet);
3189 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3190 if (pfh_inet6 == NULL) {
3191 lwkt_reltoken(&pf_token);
3194 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN, pfh_inet6);
3195 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT, pfh_inet6);
3199 lwkt_reltoken(&pf_token);
3206 lwkt_gettoken(&pf_token);
3208 pf_dev = make_dev(&pf_ops, 0, 0, 0, 0600, PF_NAME);
3210 lockinit(&pf_consistency_lock, "pfconslck", 0, LK_CANRECURSE);
3211 lwkt_reltoken(&pf_token);
3216 pf_mask_del(struct radix_node *rn, void *arg)
3218 struct radix_node_head *rnh = arg;
3220 rnh->rnh_deladdr(rn->rn_key, rn->rn_mask, rnh);
3229 pf_status.running = 0;
3231 lwkt_gettoken(&pf_token);
3233 error = dehook_pf();
3236 * Should not happen!
3237 * XXX Due to error code ESRCH, kldunload will show
3238 * a message like 'No such process'.
3240 kprintf("pfil unregistration fail\n");
3241 lwkt_reltoken(&pf_token);
3246 while (pf_end_threads < 2) {
3247 wakeup_one(pf_purge_thread);
3248 tsleep(pf_purge_thread, 0, "pftmo", hz);
3252 dev_ops_remove_all(&pf_ops);
3253 lockuninit(&pf_consistency_lock);
3254 lwkt_reltoken(&pf_token);
3256 if (pf_maskhead != NULL) {
3257 pf_maskhead->rnh_walktree(pf_maskhead,
3258 pf_mask_del, pf_maskhead);
3262 kmalloc_destroy(&pf_state_pl);
3263 kmalloc_destroy(&pf_frent_pl);
3264 kmalloc_destroy(&pf_cent_pl);
3269 pf_modevent(module_t mod, int type, void *data)
3273 lwkt_gettoken(&pf_token);
3281 error = pf_unload();
3287 lwkt_reltoken(&pf_token);
3291 static moduledata_t pf_mod = {
3296 DECLARE_MODULE(pf, pf_mod, SI_SUB_PSEUDO, SI_ORDER_FIRST);
3297 MODULE_VERSION(pf, PF_MODVER);