2 * Copyright (c) 2001 Daniel Hartmeier
3 * Copyright (c) 2002,2003 Henning Brauer
4 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 * Effort sponsored in part by the Defense Advanced Research Projects
32 * Agency (DARPA) and Air Force Research Laboratory, Air Force
33 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
35 * $OpenBSD: pf_ioctl.c,v 1.213 2009/02/15 21:46:12 mbalmer Exp $
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 #include "opt_inet6.h"
46 #include <sys/param.h>
49 #include <sys/endian.h>
50 #include <sys/fcntl.h>
51 #include <sys/filio.h>
52 #include <sys/interrupt.h>
54 #include <sys/kernel.h>
55 #include <sys/kthread.h>
57 #include <sys/module.h>
60 #include <sys/socket.h>
61 #include <sys/sysctl.h>
63 #include <sys/ucred.h>
66 #include <net/route.h>
68 #include <net/pfvar.h>
69 #include <net/if_pfsync.h>
70 #include <net/if_pflog.h>
72 #include <netinet/in.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_icmp.h>
78 #include <netinet/ip6.h>
82 #include <altq/altq.h>
85 static int pfattach(void);
86 static struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
87 u_int8_t, u_int8_t, u_int8_t);
89 static void pf_mv_pool(struct pf_palist *, struct pf_palist *);
90 static void pf_empty_pool(struct pf_palist *);
91 static int pfioctl(struct cdev *, u_long, caddr_t, int,
94 static int pf_begin_altq(u_int32_t *);
95 static int pf_rollback_altq(u_int32_t);
96 static int pf_commit_altq(u_int32_t);
97 static int pf_enable_altq(struct pf_altq *);
98 static int pf_disable_altq(struct pf_altq *);
99 static u_int32_t pf_qname2qid(char *);
100 static void pf_qid_unref(u_int32_t);
102 static int pf_begin_rules(u_int32_t *, int, const char *);
103 static int pf_rollback_rules(u_int32_t, int, char *);
104 static int pf_setup_pfsync_matching(struct pf_ruleset *);
105 static void pf_hash_rule(MD5_CTX *, struct pf_rule *);
106 static void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
107 static int pf_commit_rules(u_int32_t, int, char *);
108 static int pf_addr_setup(struct pf_ruleset *,
109 struct pf_addr_wrap *, sa_family_t);
110 static void pf_addr_copyout(struct pf_addr_wrap *);
112 VNET_DEFINE(struct pf_rule, pf_default_rule);
115 static VNET_DEFINE(int, pf_altq_running);
116 #define V_pf_altq_running VNET(pf_altq_running)
119 #define TAGID_MAX 50000
121 TAILQ_ENTRY(pf_tagname) entries;
122 char name[PF_TAG_NAME_SIZE];
127 TAILQ_HEAD(pf_tags, pf_tagname);
128 #define V_pf_tags VNET(pf_tags)
129 VNET_DEFINE(struct pf_tags, pf_tags);
130 #define V_pf_qids VNET(pf_qids)
131 VNET_DEFINE(struct pf_tags, pf_qids);
132 static MALLOC_DEFINE(M_PFTAG, "pf_tag", "pf(4) tag names");
133 static MALLOC_DEFINE(M_PFALTQ, "pf_altq", "pf(4) altq configuration db");
134 static MALLOC_DEFINE(M_PFRULE, "pf_rule", "pf(4) rules");
136 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
137 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
140 static u_int16_t tagname2tag(struct pf_tags *, char *);
141 static u_int16_t pf_tagname2tag(char *);
142 static void tag_unref(struct pf_tags *, u_int16_t);
144 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
149 * XXX - These are new and need to be checked when moveing to a new version
151 static void pf_clear_states(void);
152 static int pf_clear_tables(void);
153 static void pf_clear_srcnodes(struct pf_src_node *);
154 static void pf_kill_srcnodes(struct pfioc_src_node_kill *);
155 static void pf_tbladdr_copyout(struct pf_addr_wrap *);
158 * Wrapper functions for pfil(9) hooks
161 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
162 int dir, struct inpcb *inp);
163 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
164 int dir, struct inpcb *inp);
167 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
168 int dir, struct inpcb *inp);
169 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
170 int dir, struct inpcb *inp);
173 static int hook_pf(void);
174 static int dehook_pf(void);
175 static int shutdown_pf(void);
176 static int pf_load(void);
177 static int pf_unload(void);
179 static struct cdevsw pf_cdevsw = {
182 .d_version = D_VERSION,
185 static volatile VNET_DEFINE(int, pf_pfil_hooked);
186 #define V_pf_pfil_hooked VNET(pf_pfil_hooked)
187 VNET_DEFINE(int, pf_end_threads);
189 struct rwlock pf_rules_lock;
192 pfsync_state_import_t *pfsync_state_import_ptr = NULL;
193 pfsync_insert_state_t *pfsync_insert_state_ptr = NULL;
194 pfsync_update_state_t *pfsync_update_state_ptr = NULL;
195 pfsync_delete_state_t *pfsync_delete_state_ptr = NULL;
196 pfsync_clear_states_t *pfsync_clear_states_ptr = NULL;
197 pfsync_defer_t *pfsync_defer_ptr = NULL;
199 pflog_packet_t *pflog_packet_ptr = NULL;
204 u_int32_t *my_timeout = V_pf_default_rule.timeout;
212 V_pf_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
213 V_pf_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
215 RB_INIT(&V_pf_anchors);
216 pf_init_ruleset(&pf_main_ruleset);
218 /* default rule should never be garbage collected */
219 V_pf_default_rule.entries.tqe_prev = &V_pf_default_rule.entries.tqe_next;
220 #ifdef PF_DEFAULT_TO_DROP
221 V_pf_default_rule.action = PF_DROP;
223 V_pf_default_rule.action = PF_PASS;
225 V_pf_default_rule.nr = -1;
226 V_pf_default_rule.rtableid = -1;
228 /* initialize default timeouts */
229 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
230 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
231 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
232 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
233 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
234 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
235 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
236 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
237 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
238 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
239 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
240 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
241 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
242 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
243 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
244 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
245 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
246 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
247 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
248 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
250 bzero(&V_pf_status, sizeof(V_pf_status));
251 V_pf_status.debug = PF_DEBUG_URGENT;
253 V_pf_pfil_hooked = 0;
255 /* XXX do our best to avoid a conflict */
256 V_pf_status.hostid = arc4random();
258 if ((error = kproc_create(pf_purge_thread, curvnet, NULL, 0, 0,
260 /* XXXGL: leaked all above. */
262 if ((error = swi_add(NULL, "pf send", pf_intr, curvnet, SWI_NET,
263 INTR_MPSAFE, &V_pf_swi_cookie)) != 0)
264 /* XXXGL: leaked all above. */
270 static struct pf_pool *
271 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
272 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
273 u_int8_t check_ticket)
275 struct pf_ruleset *ruleset;
276 struct pf_rule *rule;
279 ruleset = pf_find_ruleset(anchor);
282 rs_num = pf_get_ruleset_number(rule_action);
283 if (rs_num >= PF_RULESET_MAX)
286 if (check_ticket && ticket !=
287 ruleset->rules[rs_num].active.ticket)
290 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
293 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
295 if (check_ticket && ticket !=
296 ruleset->rules[rs_num].inactive.ticket)
299 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
302 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
305 while ((rule != NULL) && (rule->nr != rule_number))
306 rule = TAILQ_NEXT(rule, entries);
311 return (&rule->rpool);
315 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
317 struct pf_pooladdr *mv_pool_pa;
319 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
320 TAILQ_REMOVE(poola, mv_pool_pa, entries);
321 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
326 pf_empty_pool(struct pf_palist *poola)
328 struct pf_pooladdr *pa;
330 while ((pa = TAILQ_FIRST(poola)) != NULL) {
331 switch (pa->addr.type) {
332 case PF_ADDR_DYNIFTL:
333 pfi_dynaddr_remove(pa->addr.p.dyn);
336 pfr_detach_table(pa->addr.p.tbl);
340 pfi_kif_unref(pa->kif);
341 TAILQ_REMOVE(poola, pa, entries);
347 pf_unlink_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
352 TAILQ_REMOVE(rulequeue, rule, entries);
354 PF_UNLNKDRULES_LOCK();
355 rule->rule_flag |= PFRULE_REFS;
356 TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries);
357 PF_UNLNKDRULES_UNLOCK();
361 pf_free_rule(struct pf_rule *rule)
367 tag_unref(&V_pf_tags, rule->tag);
369 tag_unref(&V_pf_tags, rule->match_tag);
371 if (rule->pqid != rule->qid)
372 pf_qid_unref(rule->pqid);
373 pf_qid_unref(rule->qid);
375 switch (rule->src.addr.type) {
376 case PF_ADDR_DYNIFTL:
377 pfi_dynaddr_remove(rule->src.addr.p.dyn);
380 pfr_detach_table(rule->src.addr.p.tbl);
383 switch (rule->dst.addr.type) {
384 case PF_ADDR_DYNIFTL:
385 pfi_dynaddr_remove(rule->dst.addr.p.dyn);
388 pfr_detach_table(rule->dst.addr.p.tbl);
391 if (rule->overload_tbl)
392 pfr_detach_table(rule->overload_tbl);
394 pfi_kif_unref(rule->kif);
395 pf_anchor_remove(rule);
396 pf_empty_pool(&rule->rpool.list);
397 free(rule, M_PFRULE);
401 tagname2tag(struct pf_tags *head, char *tagname)
403 struct pf_tagname *tag, *p = NULL;
404 u_int16_t new_tagid = 1;
408 TAILQ_FOREACH(tag, head, entries)
409 if (strcmp(tagname, tag->name) == 0) {
415 * to avoid fragmentation, we do a linear search from the beginning
416 * and take the first free slot we find. if there is none or the list
417 * is empty, append a new entry at the end.
421 if (!TAILQ_EMPTY(head))
422 for (p = TAILQ_FIRST(head); p != NULL &&
423 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
424 new_tagid = p->tag + 1;
426 if (new_tagid > TAGID_MAX)
429 /* allocate and fill new struct pf_tagname */
430 tag = malloc(sizeof(*tag), M_PFTAG, M_NOWAIT|M_ZERO);
433 strlcpy(tag->name, tagname, sizeof(tag->name));
434 tag->tag = new_tagid;
437 if (p != NULL) /* insert new entry before p */
438 TAILQ_INSERT_BEFORE(p, tag, entries);
439 else /* either list empty or no free slot in between */
440 TAILQ_INSERT_TAIL(head, tag, entries);
446 tag_unref(struct pf_tags *head, u_int16_t tag)
448 struct pf_tagname *p, *next;
452 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
453 next = TAILQ_NEXT(p, entries);
456 TAILQ_REMOVE(head, p, entries);
465 pf_tagname2tag(char *tagname)
467 return (tagname2tag(&V_pf_tags, tagname));
472 pf_qname2qid(char *qname)
474 return ((u_int32_t)tagname2tag(&V_pf_qids, qname));
478 pf_qid_unref(u_int32_t qid)
480 tag_unref(&V_pf_qids, (u_int16_t)qid);
484 pf_begin_altq(u_int32_t *ticket)
486 struct pf_altq *altq;
491 /* Purge the old altq list */
492 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
493 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
494 if (altq->qname[0] == 0 &&
495 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
496 /* detach and destroy the discipline */
497 error = altq_remove(altq);
499 pf_qid_unref(altq->qid);
500 free(altq, M_PFALTQ);
504 *ticket = ++V_ticket_altqs_inactive;
505 V_altqs_inactive_open = 1;
510 pf_rollback_altq(u_int32_t ticket)
512 struct pf_altq *altq;
517 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
519 /* Purge the old altq list */
520 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
521 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
522 if (altq->qname[0] == 0 &&
523 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
524 /* detach and destroy the discipline */
525 error = altq_remove(altq);
527 pf_qid_unref(altq->qid);
528 free(altq, M_PFALTQ);
530 V_altqs_inactive_open = 0;
535 pf_commit_altq(u_int32_t ticket)
537 struct pf_altqqueue *old_altqs;
538 struct pf_altq *altq;
543 if (!V_altqs_inactive_open || ticket != V_ticket_altqs_inactive)
546 /* swap altqs, keep the old. */
547 old_altqs = V_pf_altqs_active;
548 V_pf_altqs_active = V_pf_altqs_inactive;
549 V_pf_altqs_inactive = old_altqs;
550 V_ticket_altqs_active = V_ticket_altqs_inactive;
552 /* Attach new disciplines */
553 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
554 if (altq->qname[0] == 0 &&
555 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
556 /* attach the discipline */
557 error = altq_pfattach(altq);
558 if (error == 0 && V_pf_altq_running)
559 error = pf_enable_altq(altq);
565 /* Purge the old altq list */
566 while ((altq = TAILQ_FIRST(V_pf_altqs_inactive)) != NULL) {
567 TAILQ_REMOVE(V_pf_altqs_inactive, altq, entries);
568 if (altq->qname[0] == 0 &&
569 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
570 /* detach and destroy the discipline */
571 if (V_pf_altq_running)
572 error = pf_disable_altq(altq);
573 err = altq_pfdetach(altq);
574 if (err != 0 && error == 0)
576 err = altq_remove(altq);
577 if (err != 0 && error == 0)
580 pf_qid_unref(altq->qid);
581 free(altq, M_PFALTQ);
584 V_altqs_inactive_open = 0;
589 pf_enable_altq(struct pf_altq *altq)
592 struct tb_profile tb;
595 if ((ifp = ifunit(altq->ifname)) == NULL)
598 if (ifp->if_snd.altq_type != ALTQT_NONE)
599 error = altq_enable(&ifp->if_snd);
601 /* set tokenbucket regulator */
602 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
603 tb.rate = altq->ifbandwidth;
604 tb.depth = altq->tbrsize;
605 error = tbr_set(&ifp->if_snd, &tb);
612 pf_disable_altq(struct pf_altq *altq)
615 struct tb_profile tb;
618 if ((ifp = ifunit(altq->ifname)) == NULL)
622 * when the discipline is no longer referenced, it was overridden
623 * by a new one. if so, just return.
625 if (altq->altq_disc != ifp->if_snd.altq_disc)
628 error = altq_disable(&ifp->if_snd);
631 /* clear tokenbucket regulator */
633 error = tbr_set(&ifp->if_snd, &tb);
640 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
643 struct pf_altq *a1, *a2, *a3;
647 /* Interrupt userland queue modifications */
648 if (V_altqs_inactive_open)
649 pf_rollback_altq(V_ticket_altqs_inactive);
651 /* Start new altq ruleset */
652 if (pf_begin_altq(&ticket))
655 /* Copy the current active set */
656 TAILQ_FOREACH(a1, V_pf_altqs_active, entries) {
657 a2 = malloc(sizeof(*a2), M_PFALTQ, M_NOWAIT);
662 bcopy(a1, a2, sizeof(struct pf_altq));
664 if (a2->qname[0] != 0) {
665 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
670 a2->altq_disc = NULL;
671 TAILQ_FOREACH(a3, V_pf_altqs_inactive, entries) {
672 if (strncmp(a3->ifname, a2->ifname,
673 IFNAMSIZ) == 0 && a3->qname[0] == 0) {
674 a2->altq_disc = a3->altq_disc;
679 /* Deactivate the interface in question */
680 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
681 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
682 (remove && ifp1 == ifp)) {
683 a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
685 error = altq_add(a2);
687 if (ticket != V_ticket_altqs_inactive)
696 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, a2, entries);
700 pf_rollback_altq(ticket);
702 pf_commit_altq(ticket);
707 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
709 struct pf_ruleset *rs;
710 struct pf_rule *rule;
714 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
716 rs = pf_find_or_create_ruleset(anchor);
719 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
720 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
721 rs->rules[rs_num].inactive.rcount--;
723 *ticket = ++rs->rules[rs_num].inactive.ticket;
724 rs->rules[rs_num].inactive.open = 1;
729 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
731 struct pf_ruleset *rs;
732 struct pf_rule *rule;
736 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
738 rs = pf_find_ruleset(anchor);
739 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
740 rs->rules[rs_num].inactive.ticket != ticket)
742 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
743 pf_unlink_rule(rs->rules[rs_num].inactive.ptr, rule);
744 rs->rules[rs_num].inactive.rcount--;
746 rs->rules[rs_num].inactive.open = 0;
750 #define PF_MD5_UPD(st, elm) \
751 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
753 #define PF_MD5_UPD_STR(st, elm) \
754 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
756 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
757 (stor) = htonl((st)->elm); \
758 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
761 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
762 (stor) = htons((st)->elm); \
763 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
767 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
769 PF_MD5_UPD(pfr, addr.type);
770 switch (pfr->addr.type) {
771 case PF_ADDR_DYNIFTL:
772 PF_MD5_UPD(pfr, addr.v.ifname);
773 PF_MD5_UPD(pfr, addr.iflags);
776 PF_MD5_UPD(pfr, addr.v.tblname);
778 case PF_ADDR_ADDRMASK:
780 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
781 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
785 PF_MD5_UPD(pfr, port[0]);
786 PF_MD5_UPD(pfr, port[1]);
787 PF_MD5_UPD(pfr, neg);
788 PF_MD5_UPD(pfr, port_op);
792 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
797 pf_hash_rule_addr(ctx, &rule->src);
798 pf_hash_rule_addr(ctx, &rule->dst);
799 PF_MD5_UPD_STR(rule, label);
800 PF_MD5_UPD_STR(rule, ifname);
801 PF_MD5_UPD_STR(rule, match_tagname);
802 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
803 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
804 PF_MD5_UPD_HTONL(rule, prob, y);
805 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
806 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
807 PF_MD5_UPD(rule, uid.op);
808 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
809 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
810 PF_MD5_UPD(rule, gid.op);
811 PF_MD5_UPD_HTONL(rule, rule_flag, y);
812 PF_MD5_UPD(rule, action);
813 PF_MD5_UPD(rule, direction);
814 PF_MD5_UPD(rule, af);
815 PF_MD5_UPD(rule, quick);
816 PF_MD5_UPD(rule, ifnot);
817 PF_MD5_UPD(rule, match_tag_not);
818 PF_MD5_UPD(rule, natpass);
819 PF_MD5_UPD(rule, keep_state);
820 PF_MD5_UPD(rule, proto);
821 PF_MD5_UPD(rule, type);
822 PF_MD5_UPD(rule, code);
823 PF_MD5_UPD(rule, flags);
824 PF_MD5_UPD(rule, flagset);
825 PF_MD5_UPD(rule, allow_opts);
826 PF_MD5_UPD(rule, rt);
827 PF_MD5_UPD(rule, tos);
831 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
833 struct pf_ruleset *rs;
834 struct pf_rule *rule, **old_array;
835 struct pf_rulequeue *old_rules;
837 u_int32_t old_rcount;
841 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
843 rs = pf_find_ruleset(anchor);
844 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
845 ticket != rs->rules[rs_num].inactive.ticket)
848 /* Calculate checksum for the main ruleset */
849 if (rs == &pf_main_ruleset) {
850 error = pf_setup_pfsync_matching(rs);
855 /* Swap rules, keep the old. */
856 old_rules = rs->rules[rs_num].active.ptr;
857 old_rcount = rs->rules[rs_num].active.rcount;
858 old_array = rs->rules[rs_num].active.ptr_array;
860 rs->rules[rs_num].active.ptr =
861 rs->rules[rs_num].inactive.ptr;
862 rs->rules[rs_num].active.ptr_array =
863 rs->rules[rs_num].inactive.ptr_array;
864 rs->rules[rs_num].active.rcount =
865 rs->rules[rs_num].inactive.rcount;
866 rs->rules[rs_num].inactive.ptr = old_rules;
867 rs->rules[rs_num].inactive.ptr_array = old_array;
868 rs->rules[rs_num].inactive.rcount = old_rcount;
870 rs->rules[rs_num].active.ticket =
871 rs->rules[rs_num].inactive.ticket;
872 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
875 /* Purge the old rule list. */
876 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
877 pf_unlink_rule(old_rules, rule);
878 if (rs->rules[rs_num].inactive.ptr_array)
879 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
880 rs->rules[rs_num].inactive.ptr_array = NULL;
881 rs->rules[rs_num].inactive.rcount = 0;
882 rs->rules[rs_num].inactive.open = 0;
883 pf_remove_if_empty_ruleset(rs);
889 pf_setup_pfsync_matching(struct pf_ruleset *rs)
892 struct pf_rule *rule;
894 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
897 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
898 /* XXX PF_RULESET_SCRUB as well? */
899 if (rs_cnt == PF_RULESET_SCRUB)
902 if (rs->rules[rs_cnt].inactive.ptr_array)
903 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
904 rs->rules[rs_cnt].inactive.ptr_array = NULL;
906 if (rs->rules[rs_cnt].inactive.rcount) {
907 rs->rules[rs_cnt].inactive.ptr_array =
908 malloc(sizeof(caddr_t) *
909 rs->rules[rs_cnt].inactive.rcount,
912 if (!rs->rules[rs_cnt].inactive.ptr_array)
916 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
918 pf_hash_rule(&ctx, rule);
919 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
923 MD5Final(digest, &ctx);
924 memcpy(V_pf_status.pf_chksum, digest, sizeof(V_pf_status.pf_chksum));
929 pf_addr_setup(struct pf_ruleset *ruleset, struct pf_addr_wrap *addr,
934 switch (addr->type) {
936 addr->p.tbl = pfr_attach_table(ruleset, addr->v.tblname);
937 if (addr->p.tbl == NULL)
940 case PF_ADDR_DYNIFTL:
941 error = pfi_dynaddr_setup(addr, af);
949 pf_addr_copyout(struct pf_addr_wrap *addr)
952 switch (addr->type) {
953 case PF_ADDR_DYNIFTL:
954 pfi_dynaddr_copyout(addr);
957 pf_tbladdr_copyout(addr);
963 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
967 /* XXX keep in sync with switch() below */
968 if (securelevel_gt(td->td_ucred, 2))
975 case DIOCSETSTATUSIF:
982 case DIOCCLRRULECTRS:
987 case DIOCGETRULESETS:
1001 case DIOCGETSRCNODES:
1002 case DIOCCLRSRCNODES:
1003 case DIOCIGETIFACES:
1008 case DIOCRCLRTABLES:
1009 case DIOCRADDTABLES:
1010 case DIOCRDELTABLES:
1011 case DIOCRSETTFLAGS:
1012 if (((struct pfioc_table *)addr)->pfrio_flags &
1014 break; /* dummy operation ok */
1020 if (!(flags & FWRITE))
1028 case DIOCGETTIMEOUT:
1033 case DIOCGETRULESETS:
1034 case DIOCGETRULESET:
1036 case DIOCRGETTABLES:
1037 case DIOCRGETTSTATS:
1039 case DIOCRGETASTATS:
1042 case DIOCGETSRCNODES:
1043 case DIOCIGETIFACES:
1046 case DIOCRCLRTABLES:
1047 case DIOCRADDTABLES:
1048 case DIOCRDELTABLES:
1049 case DIOCRCLRTSTATS:
1054 case DIOCRSETTFLAGS:
1055 if (((struct pfioc_table *)addr)->pfrio_flags &
1057 flags |= FWRITE; /* need write lock for dummy */
1058 break; /* dummy operation ok */
1062 if (((struct pfioc_rule *)addr)->action ==
1070 CURVNET_SET(TD_TO_VNET(td));
1075 if (V_pf_status.running)
1083 DPFPRINTF(PF_DEBUG_MISC,
1084 ("pf: pfil registration failed\n"));
1088 V_pf_status.running = 1;
1089 V_pf_status.since = time_second;
1092 V_pf_stateid[cpu] = time_second;
1094 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1101 if (!V_pf_status.running)
1104 V_pf_status.running = 0;
1106 error = dehook_pf();
1108 V_pf_status.running = 1;
1109 DPFPRINTF(PF_DEBUG_MISC,
1110 ("pf: pfil unregistration failed\n"));
1113 V_pf_status.since = time_second;
1114 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1120 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1121 struct pf_ruleset *ruleset;
1122 struct pf_rule *rule, *tail;
1123 struct pf_pooladdr *pa;
1124 struct pfi_kif *kif = NULL;
1127 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1132 if (pr->rule.af == AF_INET) {
1133 error = EAFNOSUPPORT;
1138 if (pr->rule.af == AF_INET6) {
1139 error = EAFNOSUPPORT;
1144 rule = malloc(sizeof(*rule), M_PFRULE, M_WAITOK);
1145 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1146 if (rule->ifname[0])
1147 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1148 rule->cuid = td->td_ucred->cr_ruid;
1149 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1150 TAILQ_INIT(&rule->rpool.list);
1152 #define ERROUT(x) { error = (x); goto DIOCADDRULE_error; }
1155 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1156 ruleset = pf_find_ruleset(pr->anchor);
1157 if (ruleset == NULL)
1159 rs_num = pf_get_ruleset_number(pr->rule.action);
1160 if (rs_num >= PF_RULESET_MAX)
1162 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1163 DPFPRINTF(PF_DEBUG_MISC,
1164 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1165 ruleset->rules[rs_num].inactive.ticket));
1168 if (pr->pool_ticket != V_ticket_pabuf) {
1169 DPFPRINTF(PF_DEBUG_MISC,
1170 ("pool_ticket: %d != %d\n", pr->pool_ticket,
1175 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1178 rule->nr = tail->nr + 1;
1181 if (rule->ifname[0]) {
1182 rule->kif = pfi_kif_attach(kif, rule->ifname);
1183 pfi_kif_ref(rule->kif);
1187 if (rule->rtableid > 0 && rule->rtableid >= rt_numfibs)
1192 if (rule->qname[0] != 0) {
1193 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1195 else if (rule->pqname[0] != 0) {
1197 pf_qname2qid(rule->pqname)) == 0)
1200 rule->pqid = rule->qid;
1203 if (rule->tagname[0])
1204 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1206 if (rule->match_tagname[0])
1207 if ((rule->match_tag =
1208 pf_tagname2tag(rule->match_tagname)) == 0)
1210 if (rule->rt && !rule->direction)
1214 if (rule->logif >= PFLOGIFS_MAX)
1216 if (pf_addr_setup(ruleset, &rule->src.addr, rule->af))
1218 if (pf_addr_setup(ruleset, &rule->dst.addr, rule->af))
1220 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1222 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1223 if (pa->addr.type == PF_ADDR_TABLE) {
1224 pa->addr.p.tbl = pfr_attach_table(ruleset,
1225 pa->addr.v.tblname);
1226 if (pa->addr.p.tbl == NULL)
1230 if (rule->overload_tblname[0]) {
1231 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1232 rule->overload_tblname)) == NULL)
1235 rule->overload_tbl->pfrkt_flags |=
1239 pf_mv_pool(&V_pf_pabuf, &rule->rpool.list);
1240 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1241 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1242 (rule->rt > PF_FASTROUTE)) &&
1243 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1252 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1253 rule->evaluations = rule->packets[0] = rule->packets[1] =
1254 rule->bytes[0] = rule->bytes[1] = 0;
1255 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1257 ruleset->rules[rs_num].inactive.rcount++;
1264 free(rule, M_PFRULE);
1266 free(kif, PFI_MTYPE);
1270 case DIOCGETRULES: {
1271 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1272 struct pf_ruleset *ruleset;
1273 struct pf_rule *tail;
1277 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1278 ruleset = pf_find_ruleset(pr->anchor);
1279 if (ruleset == NULL) {
1284 rs_num = pf_get_ruleset_number(pr->rule.action);
1285 if (rs_num >= PF_RULESET_MAX) {
1290 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1293 pr->nr = tail->nr + 1;
1296 pr->ticket = ruleset->rules[rs_num].active.ticket;
1302 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1303 struct pf_ruleset *ruleset;
1304 struct pf_rule *rule;
1308 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1309 ruleset = pf_find_ruleset(pr->anchor);
1310 if (ruleset == NULL) {
1315 rs_num = pf_get_ruleset_number(pr->rule.action);
1316 if (rs_num >= PF_RULESET_MAX) {
1321 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1326 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1327 while ((rule != NULL) && (rule->nr != pr->nr))
1328 rule = TAILQ_NEXT(rule, entries);
1334 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1335 if (pf_anchor_copyout(ruleset, rule, pr)) {
1340 pf_addr_copyout(&pr->rule.src.addr);
1341 pf_addr_copyout(&pr->rule.dst.addr);
1342 for (i = 0; i < PF_SKIP_COUNT; ++i)
1343 if (rule->skip[i].ptr == NULL)
1344 pr->rule.skip[i].nr = -1;
1346 pr->rule.skip[i].nr =
1347 rule->skip[i].ptr->nr;
1349 if (pr->action == PF_GET_CLR_CNTR) {
1350 rule->evaluations = 0;
1351 rule->packets[0] = rule->packets[1] = 0;
1352 rule->bytes[0] = rule->bytes[1] = 0;
1353 rule->states_tot = 0;
1359 case DIOCCHANGERULE: {
1360 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1361 struct pf_ruleset *ruleset;
1362 struct pf_rule *oldrule = NULL, *newrule = NULL;
1363 struct pfi_kif *kif = NULL;
1364 struct pf_pooladdr *pa;
1368 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1369 pcr->action > PF_CHANGE_GET_TICKET) {
1373 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1378 if (pcr->action != PF_CHANGE_REMOVE) {
1380 if (pcr->rule.af == AF_INET) {
1381 error = EAFNOSUPPORT;
1386 if (pcr->rule.af == AF_INET6) {
1387 error = EAFNOSUPPORT;
1391 newrule = malloc(sizeof(*newrule), M_PFRULE, M_WAITOK);
1392 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1393 newrule->cuid = td->td_ucred->cr_ruid;
1394 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1395 TAILQ_INIT(&newrule->rpool.list);
1396 /* Initialize refcounting. */
1397 newrule->states_cur = 0;
1398 newrule->entries.tqe_prev = NULL;
1400 if (newrule->ifname[0])
1401 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
1404 #define ERROUT(x) { error = (x); goto DIOCCHANGERULE_error; }
1407 if (!(pcr->action == PF_CHANGE_REMOVE ||
1408 pcr->action == PF_CHANGE_GET_TICKET) &&
1409 pcr->pool_ticket != V_ticket_pabuf)
1412 ruleset = pf_find_ruleset(pcr->anchor);
1413 if (ruleset == NULL)
1416 rs_num = pf_get_ruleset_number(pcr->rule.action);
1417 if (rs_num >= PF_RULESET_MAX)
1420 if (pcr->action == PF_CHANGE_GET_TICKET) {
1421 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1423 } else if (pcr->ticket !=
1424 ruleset->rules[rs_num].active.ticket)
1427 if (pcr->action != PF_CHANGE_REMOVE) {
1428 if (newrule->ifname[0]) {
1429 newrule->kif = pfi_kif_attach(kif,
1431 pfi_kif_ref(newrule->kif);
1433 newrule->kif = NULL;
1435 if (newrule->rtableid > 0 &&
1436 newrule->rtableid >= rt_numfibs)
1441 if (newrule->qname[0] != 0) {
1443 pf_qname2qid(newrule->qname)) == 0)
1445 else if (newrule->pqname[0] != 0) {
1446 if ((newrule->pqid =
1447 pf_qname2qid(newrule->pqname)) == 0)
1450 newrule->pqid = newrule->qid;
1453 if (newrule->tagname[0])
1455 pf_tagname2tag(newrule->tagname)) == 0)
1457 if (newrule->match_tagname[0])
1458 if ((newrule->match_tag = pf_tagname2tag(
1459 newrule->match_tagname)) == 0)
1461 if (newrule->rt && !newrule->direction)
1465 if (newrule->logif >= PFLOGIFS_MAX)
1467 if (pf_addr_setup(ruleset, &newrule->src.addr, newrule->af))
1469 if (pf_addr_setup(ruleset, &newrule->dst.addr, newrule->af))
1471 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1473 TAILQ_FOREACH(pa, &V_pf_pabuf, entries)
1474 if (pa->addr.type == PF_ADDR_TABLE) {
1476 pfr_attach_table(ruleset,
1477 pa->addr.v.tblname);
1478 if (pa->addr.p.tbl == NULL)
1482 if (newrule->overload_tblname[0]) {
1483 if ((newrule->overload_tbl = pfr_attach_table(
1484 ruleset, newrule->overload_tblname)) ==
1488 newrule->overload_tbl->pfrkt_flags |=
1492 pf_mv_pool(&V_pf_pabuf, &newrule->rpool.list);
1493 if (((((newrule->action == PF_NAT) ||
1494 (newrule->action == PF_RDR) ||
1495 (newrule->action == PF_BINAT) ||
1496 (newrule->rt > PF_FASTROUTE)) &&
1497 !newrule->anchor)) &&
1498 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1502 pf_free_rule(newrule);
1507 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1508 newrule->evaluations = 0;
1509 newrule->packets[0] = newrule->packets[1] = 0;
1510 newrule->bytes[0] = newrule->bytes[1] = 0;
1512 pf_empty_pool(&V_pf_pabuf);
1514 if (pcr->action == PF_CHANGE_ADD_HEAD)
1515 oldrule = TAILQ_FIRST(
1516 ruleset->rules[rs_num].active.ptr);
1517 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1518 oldrule = TAILQ_LAST(
1519 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1521 oldrule = TAILQ_FIRST(
1522 ruleset->rules[rs_num].active.ptr);
1523 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1524 oldrule = TAILQ_NEXT(oldrule, entries);
1525 if (oldrule == NULL) {
1526 if (newrule != NULL)
1527 pf_free_rule(newrule);
1534 if (pcr->action == PF_CHANGE_REMOVE) {
1535 pf_unlink_rule(ruleset->rules[rs_num].active.ptr,
1537 ruleset->rules[rs_num].active.rcount--;
1539 if (oldrule == NULL)
1541 ruleset->rules[rs_num].active.ptr,
1543 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1544 pcr->action == PF_CHANGE_ADD_BEFORE)
1545 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1548 ruleset->rules[rs_num].active.ptr,
1549 oldrule, newrule, entries);
1550 ruleset->rules[rs_num].active.rcount++;
1554 TAILQ_FOREACH(oldrule,
1555 ruleset->rules[rs_num].active.ptr, entries)
1558 ruleset->rules[rs_num].active.ticket++;
1560 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1561 pf_remove_if_empty_ruleset(ruleset);
1567 DIOCCHANGERULE_error:
1569 if (newrule != NULL)
1570 free(newrule, M_PFRULE);
1572 free(kif, PFI_MTYPE);
1576 case DIOCCLRSTATES: {
1578 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1579 u_int i, killed = 0;
1581 for (i = 0; i <= V_pf_hashmask; i++) {
1582 struct pf_idhash *ih = &V_pf_idhash[i];
1584 relock_DIOCCLRSTATES:
1585 PF_HASHROW_LOCK(ih);
1586 LIST_FOREACH(s, &ih->states, entry)
1587 if (!psk->psk_ifname[0] ||
1588 !strcmp(psk->psk_ifname,
1589 s->kif->pfik_name)) {
1591 * Don't send out individual
1594 s->state_flags |= PFSTATE_NOSYNC;
1595 pf_unlink_state(s, PF_ENTER_LOCKED);
1597 goto relock_DIOCCLRSTATES;
1599 PF_HASHROW_UNLOCK(ih);
1601 psk->psk_killed = killed;
1602 if (pfsync_clear_states_ptr != NULL)
1603 pfsync_clear_states_ptr(V_pf_status.hostid, psk->psk_ifname);
1607 case DIOCKILLSTATES: {
1609 struct pf_state_key *sk;
1610 struct pf_addr *srcaddr, *dstaddr;
1611 u_int16_t srcport, dstport;
1612 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1613 u_int i, killed = 0;
1615 if (psk->psk_pfcmp.id) {
1616 if (psk->psk_pfcmp.creatorid == 0)
1617 psk->psk_pfcmp.creatorid = V_pf_status.hostid;
1618 if ((s = pf_find_state_byid(psk->psk_pfcmp.id,
1619 psk->psk_pfcmp.creatorid))) {
1620 pf_unlink_state(s, PF_ENTER_LOCKED);
1621 psk->psk_killed = 1;
1626 for (i = 0; i <= V_pf_hashmask; i++) {
1627 struct pf_idhash *ih = &V_pf_idhash[i];
1629 relock_DIOCKILLSTATES:
1630 PF_HASHROW_LOCK(ih);
1631 LIST_FOREACH(s, &ih->states, entry) {
1632 sk = s->key[PF_SK_WIRE];
1633 if (s->direction == PF_OUT) {
1634 srcaddr = &sk->addr[1];
1635 dstaddr = &sk->addr[0];
1636 srcport = sk->port[0];
1637 dstport = sk->port[0];
1639 srcaddr = &sk->addr[0];
1640 dstaddr = &sk->addr[1];
1641 srcport = sk->port[0];
1642 dstport = sk->port[0];
1645 if ((!psk->psk_af || sk->af == psk->psk_af)
1646 && (!psk->psk_proto || psk->psk_proto ==
1648 PF_MATCHA(psk->psk_src.neg,
1649 &psk->psk_src.addr.v.a.addr,
1650 &psk->psk_src.addr.v.a.mask,
1652 PF_MATCHA(psk->psk_dst.neg,
1653 &psk->psk_dst.addr.v.a.addr,
1654 &psk->psk_dst.addr.v.a.mask,
1656 (psk->psk_src.port_op == 0 ||
1657 pf_match_port(psk->psk_src.port_op,
1658 psk->psk_src.port[0], psk->psk_src.port[1],
1660 (psk->psk_dst.port_op == 0 ||
1661 pf_match_port(psk->psk_dst.port_op,
1662 psk->psk_dst.port[0], psk->psk_dst.port[1],
1664 (!psk->psk_label[0] ||
1665 (s->rule.ptr->label[0] &&
1666 !strcmp(psk->psk_label,
1667 s->rule.ptr->label))) &&
1668 (!psk->psk_ifname[0] ||
1669 !strcmp(psk->psk_ifname,
1670 s->kif->pfik_name))) {
1671 pf_unlink_state(s, PF_ENTER_LOCKED);
1673 goto relock_DIOCKILLSTATES;
1676 PF_HASHROW_UNLOCK(ih);
1678 psk->psk_killed = killed;
1682 case DIOCADDSTATE: {
1683 struct pfioc_state *ps = (struct pfioc_state *)addr;
1684 struct pfsync_state *sp = &ps->state;
1686 if (sp->timeout >= PFTM_MAX) {
1690 if (pfsync_state_import_ptr != NULL) {
1692 error = pfsync_state_import_ptr(sp, PFSYNC_SI_IOCTL);
1699 case DIOCGETSTATE: {
1700 struct pfioc_state *ps = (struct pfioc_state *)addr;
1703 s = pf_find_state_byid(ps->state.id, ps->state.creatorid);
1709 pfsync_state_export(&ps->state, s);
1714 case DIOCGETSTATES: {
1715 struct pfioc_states *ps = (struct pfioc_states *)addr;
1717 struct pfsync_state *pstore, *p;
1720 if (ps->ps_len == 0) {
1721 nr = uma_zone_get_cur(V_pf_state_z);
1722 ps->ps_len = sizeof(struct pfsync_state) * nr;
1726 p = pstore = malloc(ps->ps_len, M_TEMP, M_WAITOK);
1729 for (i = 0; i <= V_pf_hashmask; i++) {
1730 struct pf_idhash *ih = &V_pf_idhash[i];
1732 PF_HASHROW_LOCK(ih);
1733 LIST_FOREACH(s, &ih->states, entry) {
1735 if (s->timeout == PFTM_UNLINKED)
1738 if ((nr+1) * sizeof(*p) > ps->ps_len) {
1739 PF_HASHROW_UNLOCK(ih);
1740 goto DIOCGETSTATES_full;
1742 pfsync_state_export(p, s);
1746 PF_HASHROW_UNLOCK(ih);
1749 error = copyout(pstore, ps->ps_states,
1750 sizeof(struct pfsync_state) * nr);
1752 free(pstore, M_TEMP);
1755 ps->ps_len = sizeof(struct pfsync_state) * nr;
1756 free(pstore, M_TEMP);
1761 case DIOCGETSTATUS: {
1762 struct pf_status *s = (struct pf_status *)addr;
1764 bcopy(&V_pf_status, s, sizeof(struct pf_status));
1765 pfi_update_status(s->ifname, s);
1770 case DIOCSETSTATUSIF: {
1771 struct pfioc_if *pi = (struct pfioc_if *)addr;
1773 if (pi->ifname[0] == 0) {
1774 bzero(V_pf_status.ifname, IFNAMSIZ);
1778 strlcpy(V_pf_status.ifname, pi->ifname, IFNAMSIZ);
1783 case DIOCCLRSTATUS: {
1785 bzero(V_pf_status.counters, sizeof(V_pf_status.counters));
1786 bzero(V_pf_status.fcounters, sizeof(V_pf_status.fcounters));
1787 bzero(V_pf_status.scounters, sizeof(V_pf_status.scounters));
1788 V_pf_status.since = time_second;
1789 if (*V_pf_status.ifname)
1790 pfi_update_status(V_pf_status.ifname, NULL);
1796 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
1797 struct pf_state_key *sk;
1798 struct pf_state *state;
1799 struct pf_state_key_cmp key;
1800 int m = 0, direction = pnl->direction;
1803 /* NATLOOK src and dst are reversed, so reverse sidx/didx */
1804 sidx = (direction == PF_IN) ? 1 : 0;
1805 didx = (direction == PF_IN) ? 0 : 1;
1808 PF_AZERO(&pnl->saddr, pnl->af) ||
1809 PF_AZERO(&pnl->daddr, pnl->af) ||
1810 ((pnl->proto == IPPROTO_TCP ||
1811 pnl->proto == IPPROTO_UDP) &&
1812 (!pnl->dport || !pnl->sport)))
1815 bzero(&key, sizeof(key));
1817 key.proto = pnl->proto;
1818 PF_ACPY(&key.addr[sidx], &pnl->saddr, pnl->af);
1819 key.port[sidx] = pnl->sport;
1820 PF_ACPY(&key.addr[didx], &pnl->daddr, pnl->af);
1821 key.port[didx] = pnl->dport;
1823 state = pf_find_state_all(&key, direction, &m);
1826 error = E2BIG; /* more than one state */
1827 else if (state != NULL) {
1828 /* XXXGL: not locked read */
1829 sk = state->key[sidx];
1830 PF_ACPY(&pnl->rsaddr, &sk->addr[sidx], sk->af);
1831 pnl->rsport = sk->port[sidx];
1832 PF_ACPY(&pnl->rdaddr, &sk->addr[didx], sk->af);
1833 pnl->rdport = sk->port[didx];
1840 case DIOCSETTIMEOUT: {
1841 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1844 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
1850 old = V_pf_default_rule.timeout[pt->timeout];
1851 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
1853 V_pf_default_rule.timeout[pt->timeout] = pt->seconds;
1854 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
1855 wakeup(pf_purge_thread);
1861 case DIOCGETTIMEOUT: {
1862 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
1864 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
1869 pt->seconds = V_pf_default_rule.timeout[pt->timeout];
1874 case DIOCGETLIMIT: {
1875 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1877 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
1882 pl->limit = V_pf_limits[pl->index].limit;
1887 case DIOCSETLIMIT: {
1888 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
1892 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
1893 V_pf_limits[pl->index].zone == NULL) {
1898 uma_zone_set_max(V_pf_limits[pl->index].zone, pl->limit);
1899 old_limit = V_pf_limits[pl->index].limit;
1900 V_pf_limits[pl->index].limit = pl->limit;
1901 pl->limit = old_limit;
1906 case DIOCSETDEBUG: {
1907 u_int32_t *level = (u_int32_t *)addr;
1910 V_pf_status.debug = *level;
1915 case DIOCCLRRULECTRS: {
1916 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
1917 struct pf_ruleset *ruleset = &pf_main_ruleset;
1918 struct pf_rule *rule;
1922 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
1923 rule->evaluations = 0;
1924 rule->packets[0] = rule->packets[1] = 0;
1925 rule->bytes[0] = rule->bytes[1] = 0;
1931 case DIOCGIFSPEED: {
1932 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
1933 struct pf_ifspeed ps;
1936 if (psp->ifname[0] != 0) {
1937 /* Can we completely trust user-land? */
1938 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
1939 ifp = ifunit(ps.ifname);
1941 psp->baudrate = ifp->if_baudrate;
1950 case DIOCSTARTALTQ: {
1951 struct pf_altq *altq;
1954 /* enable all altq interfaces on active list */
1955 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
1956 if (altq->qname[0] == 0 && (altq->local_flags &
1957 PFALTQ_FLAG_IF_REMOVED) == 0) {
1958 error = pf_enable_altq(altq);
1964 V_pf_altq_running = 1;
1966 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
1970 case DIOCSTOPALTQ: {
1971 struct pf_altq *altq;
1974 /* disable all altq interfaces on active list */
1975 TAILQ_FOREACH(altq, V_pf_altqs_active, entries) {
1976 if (altq->qname[0] == 0 && (altq->local_flags &
1977 PFALTQ_FLAG_IF_REMOVED) == 0) {
1978 error = pf_disable_altq(altq);
1984 V_pf_altq_running = 0;
1986 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
1991 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
1992 struct pf_altq *altq, *a;
1995 altq = malloc(sizeof(*altq), M_PFALTQ, M_WAITOK);
1996 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
1997 altq->local_flags = 0;
2000 if (pa->ticket != V_ticket_altqs_inactive) {
2002 free(altq, M_PFALTQ);
2008 * if this is for a queue, find the discipline and
2009 * copy the necessary fields
2011 if (altq->qname[0] != 0) {
2012 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2015 free(altq, M_PFALTQ);
2018 altq->altq_disc = NULL;
2019 TAILQ_FOREACH(a, V_pf_altqs_inactive, entries) {
2020 if (strncmp(a->ifname, altq->ifname,
2021 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2022 altq->altq_disc = a->altq_disc;
2028 if ((ifp = ifunit(altq->ifname)) == NULL)
2029 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2031 error = altq_add(altq);
2035 free(altq, M_PFALTQ);
2039 TAILQ_INSERT_TAIL(V_pf_altqs_inactive, altq, entries);
2040 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2045 case DIOCGETALTQS: {
2046 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2047 struct pf_altq *altq;
2051 TAILQ_FOREACH(altq, V_pf_altqs_active, entries)
2053 pa->ticket = V_ticket_altqs_active;
2059 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2060 struct pf_altq *altq;
2064 if (pa->ticket != V_ticket_altqs_active) {
2070 altq = TAILQ_FIRST(V_pf_altqs_active);
2071 while ((altq != NULL) && (nr < pa->nr)) {
2072 altq = TAILQ_NEXT(altq, entries);
2080 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2085 case DIOCCHANGEALTQ:
2086 /* CHANGEALTQ not supported yet! */
2090 case DIOCGETQSTATS: {
2091 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2092 struct pf_altq *altq;
2097 if (pq->ticket != V_ticket_altqs_active) {
2102 nbytes = pq->nbytes;
2104 altq = TAILQ_FIRST(V_pf_altqs_active);
2105 while ((altq != NULL) && (nr < pq->nr)) {
2106 altq = TAILQ_NEXT(altq, entries);
2115 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2121 error = altq_getqstats(altq, pq->buf, &nbytes);
2123 pq->scheduler = altq->scheduler;
2124 pq->nbytes = nbytes;
2130 case DIOCBEGINADDRS: {
2131 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2134 pf_empty_pool(&V_pf_pabuf);
2135 pp->ticket = ++V_ticket_pabuf;
2141 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2142 struct pf_pooladdr *pa;
2143 struct pfi_kif *kif = NULL;
2146 if (pp->af == AF_INET) {
2147 error = EAFNOSUPPORT;
2152 if (pp->af == AF_INET6) {
2153 error = EAFNOSUPPORT;
2157 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2158 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2159 pp->addr.addr.type != PF_ADDR_TABLE) {
2163 pa = malloc(sizeof(*pa), M_PFRULE, M_WAITOK);
2164 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2166 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2168 if (pp->ticket != V_ticket_pabuf) {
2171 free(kif, PFI_MTYPE);
2176 if (pa->ifname[0]) {
2177 pa->kif = pfi_kif_attach(kif, pa->ifname);
2178 pfi_kif_ref(pa->kif);
2181 if (pa->addr.type == PF_ADDR_DYNIFTL && ((error =
2182 pfi_dynaddr_setup(&pa->addr, pp->af)) != 0)) {
2184 pfi_kif_unref(pa->kif);
2189 TAILQ_INSERT_TAIL(&V_pf_pabuf, pa, entries);
2194 case DIOCGETADDRS: {
2195 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2196 struct pf_pool *pool;
2197 struct pf_pooladdr *pa;
2201 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2202 pp->r_num, 0, 1, 0);
2208 TAILQ_FOREACH(pa, &pool->list, entries)
2215 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2216 struct pf_pool *pool;
2217 struct pf_pooladdr *pa;
2221 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2222 pp->r_num, 0, 1, 1);
2228 pa = TAILQ_FIRST(&pool->list);
2229 while ((pa != NULL) && (nr < pp->nr)) {
2230 pa = TAILQ_NEXT(pa, entries);
2238 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2239 pf_addr_copyout(&pp->addr.addr);
2244 case DIOCCHANGEADDR: {
2245 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2246 struct pf_pool *pool;
2247 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2248 struct pf_ruleset *ruleset;
2249 struct pfi_kif *kif = NULL;
2251 if (pca->action < PF_CHANGE_ADD_HEAD ||
2252 pca->action > PF_CHANGE_REMOVE) {
2256 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2257 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2258 pca->addr.addr.type != PF_ADDR_TABLE) {
2263 if (pca->action != PF_CHANGE_REMOVE) {
2265 if (pca->af == AF_INET) {
2266 error = EAFNOSUPPORT;
2271 if (pca->af == AF_INET6) {
2272 error = EAFNOSUPPORT;
2276 newpa = malloc(sizeof(*newpa), M_PFRULE, M_WAITOK);
2277 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2278 if (newpa->ifname[0])
2279 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
2283 #define ERROUT(x) { error = (x); goto DIOCCHANGEADDR_error; }
2285 ruleset = pf_find_ruleset(pca->anchor);
2286 if (ruleset == NULL)
2289 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2290 pca->r_num, pca->r_last, 1, 1);
2294 if (pca->action != PF_CHANGE_REMOVE) {
2295 if (newpa->ifname[0]) {
2296 newpa->kif = pfi_kif_attach(kif, newpa->ifname);
2297 pfi_kif_ref(newpa->kif);
2301 switch (newpa->addr.type) {
2302 case PF_ADDR_DYNIFTL:
2303 error = pfi_dynaddr_setup(&newpa->addr,
2307 newpa->addr.p.tbl = pfr_attach_table(ruleset,
2308 newpa->addr.v.tblname);
2309 if (newpa->addr.p.tbl == NULL)
2314 goto DIOCCHANGEADDR_error;
2317 switch (pca->action) {
2318 case PF_CHANGE_ADD_HEAD:
2319 oldpa = TAILQ_FIRST(&pool->list);
2321 case PF_CHANGE_ADD_TAIL:
2322 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2325 oldpa = TAILQ_FIRST(&pool->list);
2326 for (int i = 0; oldpa && i < pca->nr; i++)
2327 oldpa = TAILQ_NEXT(oldpa, entries);
2333 if (pca->action == PF_CHANGE_REMOVE) {
2334 TAILQ_REMOVE(&pool->list, oldpa, entries);
2335 switch (oldpa->addr.type) {
2336 case PF_ADDR_DYNIFTL:
2337 pfi_dynaddr_remove(oldpa->addr.p.dyn);
2340 pfr_detach_table(oldpa->addr.p.tbl);
2344 pfi_kif_unref(oldpa->kif);
2345 free(oldpa, M_PFRULE);
2348 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2349 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2350 pca->action == PF_CHANGE_ADD_BEFORE)
2351 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2353 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2357 pool->cur = TAILQ_FIRST(&pool->list);
2358 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr, pca->af);
2363 DIOCCHANGEADDR_error:
2365 pfi_kif_unref(newpa->kif);
2368 free(newpa, M_PFRULE);
2370 free(kif, PFI_MTYPE);
2374 case DIOCGETRULESETS: {
2375 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2376 struct pf_ruleset *ruleset;
2377 struct pf_anchor *anchor;
2380 pr->path[sizeof(pr->path) - 1] = 0;
2381 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2387 if (ruleset->anchor == NULL) {
2388 /* XXX kludge for pf_main_ruleset */
2389 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2390 if (anchor->parent == NULL)
2393 RB_FOREACH(anchor, pf_anchor_node,
2394 &ruleset->anchor->children)
2401 case DIOCGETRULESET: {
2402 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2403 struct pf_ruleset *ruleset;
2404 struct pf_anchor *anchor;
2408 pr->path[sizeof(pr->path) - 1] = 0;
2409 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2415 if (ruleset->anchor == NULL) {
2416 /* XXX kludge for pf_main_ruleset */
2417 RB_FOREACH(anchor, pf_anchor_global, &V_pf_anchors)
2418 if (anchor->parent == NULL && nr++ == pr->nr) {
2419 strlcpy(pr->name, anchor->name,
2424 RB_FOREACH(anchor, pf_anchor_node,
2425 &ruleset->anchor->children)
2426 if (nr++ == pr->nr) {
2427 strlcpy(pr->name, anchor->name,
2438 case DIOCRCLRTABLES: {
2439 struct pfioc_table *io = (struct pfioc_table *)addr;
2441 if (io->pfrio_esize != 0) {
2446 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2447 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2452 case DIOCRADDTABLES: {
2453 struct pfioc_table *io = (struct pfioc_table *)addr;
2454 struct pfr_table *pfrts;
2457 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2461 totlen = io->pfrio_size * sizeof(struct pfr_table);
2462 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2463 error = copyin(io->pfrio_buffer, pfrts, totlen);
2465 free(pfrts, M_TEMP);
2469 error = pfr_add_tables(pfrts, io->pfrio_size,
2470 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2472 free(pfrts, M_TEMP);
2476 case DIOCRDELTABLES: {
2477 struct pfioc_table *io = (struct pfioc_table *)addr;
2478 struct pfr_table *pfrts;
2481 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2485 totlen = io->pfrio_size * sizeof(struct pfr_table);
2486 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2487 error = copyin(io->pfrio_buffer, pfrts, totlen);
2489 free(pfrts, M_TEMP);
2493 error = pfr_del_tables(pfrts, io->pfrio_size,
2494 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2496 free(pfrts, M_TEMP);
2500 case DIOCRGETTABLES: {
2501 struct pfioc_table *io = (struct pfioc_table *)addr;
2502 struct pfr_table *pfrts;
2505 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2509 totlen = io->pfrio_size * sizeof(struct pfr_table);
2510 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2512 error = pfr_get_tables(&io->pfrio_table, pfrts,
2513 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2516 error = copyout(pfrts, io->pfrio_buffer, totlen);
2517 free(pfrts, M_TEMP);
2521 case DIOCRGETTSTATS: {
2522 struct pfioc_table *io = (struct pfioc_table *)addr;
2523 struct pfr_tstats *pfrtstats;
2526 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2530 totlen = io->pfrio_size * sizeof(struct pfr_tstats);
2531 pfrtstats = malloc(totlen, M_TEMP, M_WAITOK);
2533 error = pfr_get_tstats(&io->pfrio_table, pfrtstats,
2534 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2537 error = copyout(pfrtstats, io->pfrio_buffer, totlen);
2538 free(pfrtstats, M_TEMP);
2542 case DIOCRCLRTSTATS: {
2543 struct pfioc_table *io = (struct pfioc_table *)addr;
2544 struct pfr_table *pfrts;
2547 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2551 totlen = io->pfrio_size * sizeof(struct pfr_table);
2552 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2553 error = copyin(io->pfrio_buffer, pfrts, totlen);
2555 free(pfrts, M_TEMP);
2559 error = pfr_clr_tstats(pfrts, io->pfrio_size,
2560 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2562 free(pfrts, M_TEMP);
2566 case DIOCRSETTFLAGS: {
2567 struct pfioc_table *io = (struct pfioc_table *)addr;
2568 struct pfr_table *pfrts;
2571 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2575 totlen = io->pfrio_size * sizeof(struct pfr_table);
2576 pfrts = malloc(totlen, M_TEMP, M_WAITOK);
2577 error = copyin(io->pfrio_buffer, pfrts, totlen);
2579 free(pfrts, M_TEMP);
2583 error = pfr_set_tflags(pfrts, io->pfrio_size,
2584 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2585 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2587 free(pfrts, M_TEMP);
2591 case DIOCRCLRADDRS: {
2592 struct pfioc_table *io = (struct pfioc_table *)addr;
2594 if (io->pfrio_esize != 0) {
2599 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2600 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2605 case DIOCRADDADDRS: {
2606 struct pfioc_table *io = (struct pfioc_table *)addr;
2607 struct pfr_addr *pfras;
2610 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2614 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2615 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2616 error = copyin(io->pfrio_buffer, pfras, totlen);
2618 free(pfras, M_TEMP);
2622 error = pfr_add_addrs(&io->pfrio_table, pfras,
2623 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2624 PFR_FLAG_USERIOCTL);
2626 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2627 error = copyout(pfras, io->pfrio_buffer, totlen);
2628 free(pfras, M_TEMP);
2632 case DIOCRDELADDRS: {
2633 struct pfioc_table *io = (struct pfioc_table *)addr;
2634 struct pfr_addr *pfras;
2637 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2641 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2642 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2643 error = copyin(io->pfrio_buffer, pfras, totlen);
2645 free(pfras, M_TEMP);
2649 error = pfr_del_addrs(&io->pfrio_table, pfras,
2650 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2651 PFR_FLAG_USERIOCTL);
2653 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2654 error = copyout(pfras, io->pfrio_buffer, totlen);
2655 free(pfras, M_TEMP);
2659 case DIOCRSETADDRS: {
2660 struct pfioc_table *io = (struct pfioc_table *)addr;
2661 struct pfr_addr *pfras;
2664 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2668 totlen = (io->pfrio_size + io->pfrio_size2) *
2669 sizeof(struct pfr_addr);
2670 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2671 error = copyin(io->pfrio_buffer, pfras, totlen);
2673 free(pfras, M_TEMP);
2677 error = pfr_set_addrs(&io->pfrio_table, pfras,
2678 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2679 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2680 PFR_FLAG_USERIOCTL, 0);
2682 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2683 error = copyout(pfras, io->pfrio_buffer, totlen);
2684 free(pfras, M_TEMP);
2688 case DIOCRGETADDRS: {
2689 struct pfioc_table *io = (struct pfioc_table *)addr;
2690 struct pfr_addr *pfras;
2693 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2697 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2698 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2700 error = pfr_get_addrs(&io->pfrio_table, pfras,
2701 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2704 error = copyout(pfras, io->pfrio_buffer, totlen);
2705 free(pfras, M_TEMP);
2709 case DIOCRGETASTATS: {
2710 struct pfioc_table *io = (struct pfioc_table *)addr;
2711 struct pfr_astats *pfrastats;
2714 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2718 totlen = io->pfrio_size * sizeof(struct pfr_astats);
2719 pfrastats = malloc(totlen, M_TEMP, M_WAITOK);
2721 error = pfr_get_astats(&io->pfrio_table, pfrastats,
2722 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2725 error = copyout(pfrastats, io->pfrio_buffer, totlen);
2726 free(pfrastats, M_TEMP);
2730 case DIOCRCLRASTATS: {
2731 struct pfioc_table *io = (struct pfioc_table *)addr;
2732 struct pfr_addr *pfras;
2735 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2739 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2740 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2741 error = copyin(io->pfrio_buffer, pfras, totlen);
2743 free(pfras, M_TEMP);
2747 error = pfr_clr_astats(&io->pfrio_table, pfras,
2748 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2749 PFR_FLAG_USERIOCTL);
2751 if (error == 0 && io->pfrio_flags & PFR_FLAG_FEEDBACK)
2752 error = copyout(pfras, io->pfrio_buffer, totlen);
2753 free(pfras, M_TEMP);
2757 case DIOCRTSTADDRS: {
2758 struct pfioc_table *io = (struct pfioc_table *)addr;
2759 struct pfr_addr *pfras;
2762 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2766 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2767 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2768 error = copyin(io->pfrio_buffer, pfras, totlen);
2770 free(pfras, M_TEMP);
2774 error = pfr_tst_addrs(&io->pfrio_table, pfras,
2775 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2776 PFR_FLAG_USERIOCTL);
2779 error = copyout(pfras, io->pfrio_buffer, totlen);
2780 free(pfras, M_TEMP);
2784 case DIOCRINADEFINE: {
2785 struct pfioc_table *io = (struct pfioc_table *)addr;
2786 struct pfr_addr *pfras;
2789 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2793 totlen = io->pfrio_size * sizeof(struct pfr_addr);
2794 pfras = malloc(totlen, M_TEMP, M_WAITOK);
2795 error = copyin(io->pfrio_buffer, pfras, totlen);
2797 free(pfras, M_TEMP);
2801 error = pfr_ina_define(&io->pfrio_table, pfras,
2802 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2803 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2805 free(pfras, M_TEMP);
2810 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2812 error = pf_osfp_add(io);
2818 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
2820 error = pf_osfp_get(io);
2826 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2827 struct pfioc_trans_e *ioes, *ioe;
2831 if (io->esize != sizeof(*ioe)) {
2835 totlen = sizeof(struct pfioc_trans_e) * io->size;
2836 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2837 error = copyin(io->array, ioes, totlen);
2843 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2844 switch (ioe->rs_num) {
2846 case PF_RULESET_ALTQ:
2847 if (ioe->anchor[0]) {
2853 if ((error = pf_begin_altq(&ioe->ticket))) {
2860 case PF_RULESET_TABLE:
2862 struct pfr_table table;
2864 bzero(&table, sizeof(table));
2865 strlcpy(table.pfrt_anchor, ioe->anchor,
2866 sizeof(table.pfrt_anchor));
2867 if ((error = pfr_ina_begin(&table,
2868 &ioe->ticket, NULL, 0))) {
2876 if ((error = pf_begin_rules(&ioe->ticket,
2877 ioe->rs_num, ioe->anchor))) {
2886 error = copyout(ioes, io->array, totlen);
2891 case DIOCXROLLBACK: {
2892 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2893 struct pfioc_trans_e *ioe, *ioes;
2897 if (io->esize != sizeof(*ioe)) {
2901 totlen = sizeof(struct pfioc_trans_e) * io->size;
2902 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2903 error = copyin(io->array, ioes, totlen);
2909 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2910 switch (ioe->rs_num) {
2912 case PF_RULESET_ALTQ:
2913 if (ioe->anchor[0]) {
2919 if ((error = pf_rollback_altq(ioe->ticket))) {
2922 goto fail; /* really bad */
2926 case PF_RULESET_TABLE:
2928 struct pfr_table table;
2930 bzero(&table, sizeof(table));
2931 strlcpy(table.pfrt_anchor, ioe->anchor,
2932 sizeof(table.pfrt_anchor));
2933 if ((error = pfr_ina_rollback(&table,
2934 ioe->ticket, NULL, 0))) {
2937 goto fail; /* really bad */
2942 if ((error = pf_rollback_rules(ioe->ticket,
2943 ioe->rs_num, ioe->anchor))) {
2946 goto fail; /* really bad */
2957 struct pfioc_trans *io = (struct pfioc_trans *)addr;
2958 struct pfioc_trans_e *ioe, *ioes;
2959 struct pf_ruleset *rs;
2963 if (io->esize != sizeof(*ioe)) {
2967 totlen = sizeof(struct pfioc_trans_e) * io->size;
2968 ioes = malloc(totlen, M_TEMP, M_WAITOK);
2969 error = copyin(io->array, ioes, totlen);
2975 /* First makes sure everything will succeed. */
2976 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
2977 switch (ioe->rs_num) {
2979 case PF_RULESET_ALTQ:
2980 if (ioe->anchor[0]) {
2986 if (!V_altqs_inactive_open || ioe->ticket !=
2987 V_ticket_altqs_inactive) {
2995 case PF_RULESET_TABLE:
2996 rs = pf_find_ruleset(ioe->anchor);
2997 if (rs == NULL || !rs->topen || ioe->ticket !=
3006 if (ioe->rs_num < 0 || ioe->rs_num >=
3013 rs = pf_find_ruleset(ioe->anchor);
3015 !rs->rules[ioe->rs_num].inactive.open ||
3016 rs->rules[ioe->rs_num].inactive.ticket !=
3026 /* Now do the commit - no errors should happen here. */
3027 for (i = 0, ioe = ioes; i < io->size; i++, ioe++) {
3028 switch (ioe->rs_num) {
3030 case PF_RULESET_ALTQ:
3031 if ((error = pf_commit_altq(ioe->ticket))) {
3034 goto fail; /* really bad */
3038 case PF_RULESET_TABLE:
3040 struct pfr_table table;
3042 bzero(&table, sizeof(table));
3043 strlcpy(table.pfrt_anchor, ioe->anchor,
3044 sizeof(table.pfrt_anchor));
3045 if ((error = pfr_ina_commit(&table,
3046 ioe->ticket, NULL, NULL, 0))) {
3049 goto fail; /* really bad */
3054 if ((error = pf_commit_rules(ioe->ticket,
3055 ioe->rs_num, ioe->anchor))) {
3058 goto fail; /* really bad */
3068 case DIOCGETSRCNODES: {
3069 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3070 struct pf_srchash *sh;
3071 struct pf_src_node *n, *p, *pstore;
3074 if (psn->psn_len == 0) {
3075 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3077 PF_HASHROW_LOCK(sh);
3078 LIST_FOREACH(n, &sh->nodes, entry)
3080 PF_HASHROW_UNLOCK(sh);
3082 psn->psn_len = sizeof(struct pf_src_node) * nr;
3086 p = pstore = malloc(psn->psn_len, M_TEMP, M_WAITOK);
3087 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3089 PF_HASHROW_LOCK(sh);
3090 LIST_FOREACH(n, &sh->nodes, entry) {
3091 int secs = time_uptime, diff;
3093 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3096 bcopy(n, p, sizeof(struct pf_src_node));
3097 if (n->rule.ptr != NULL)
3098 p->rule.nr = n->rule.ptr->nr;
3099 p->creation = secs - p->creation;
3100 if (p->expire > secs)
3105 /* Adjust the connection rate estimate. */
3106 diff = secs - n->conn_rate.last;
3107 if (diff >= n->conn_rate.seconds)
3108 p->conn_rate.count = 0;
3110 p->conn_rate.count -=
3111 n->conn_rate.count * diff /
3112 n->conn_rate.seconds;
3116 PF_HASHROW_UNLOCK(sh);
3118 error = copyout(pstore, psn->psn_src_nodes,
3119 sizeof(struct pf_src_node) * nr);
3121 free(pstore, M_TEMP);
3124 psn->psn_len = sizeof(struct pf_src_node) * nr;
3125 free(pstore, M_TEMP);
3129 case DIOCCLRSRCNODES: {
3131 pf_clear_srcnodes(NULL);
3132 pf_purge_expired_src_nodes();
3133 V_pf_status.src_nodes = 0;
3137 case DIOCKILLSRCNODES:
3138 pf_kill_srcnodes((struct pfioc_src_node_kill *)addr);
3141 case DIOCSETHOSTID: {
3142 u_int32_t *hostid = (u_int32_t *)addr;
3146 V_pf_status.hostid = arc4random();
3148 V_pf_status.hostid = *hostid;
3159 case DIOCIGETIFACES: {
3160 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3161 struct pfi_kif *ifstore;
3164 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3169 bufsiz = io->pfiio_size * sizeof(struct pfi_kif);
3170 ifstore = malloc(bufsiz, M_TEMP, M_WAITOK);
3172 pfi_get_ifaces(io->pfiio_name, ifstore, &io->pfiio_size);
3174 error = copyout(ifstore, io->pfiio_buffer, bufsiz);
3175 free(ifstore, M_TEMP);
3179 case DIOCSETIFFLAG: {
3180 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3183 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3188 case DIOCCLRIFFLAG: {
3189 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3192 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3208 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
3210 bzero(sp, sizeof(struct pfsync_state));
3212 /* copy from state key */
3213 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
3214 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
3215 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
3216 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
3217 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
3218 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
3219 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
3220 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
3221 sp->proto = st->key[PF_SK_WIRE]->proto;
3222 sp->af = st->key[PF_SK_WIRE]->af;
3224 /* copy from state */
3225 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
3226 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
3227 sp->creation = htonl(time_uptime - st->creation);
3228 sp->expire = pf_state_expires(st);
3229 if (sp->expire <= time_uptime)
3230 sp->expire = htonl(0);
3232 sp->expire = htonl(sp->expire - time_uptime);
3234 sp->direction = st->direction;
3236 sp->timeout = st->timeout;
3237 sp->state_flags = st->state_flags;
3239 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
3240 if (st->nat_src_node)
3241 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
3244 sp->creatorid = st->creatorid;
3245 pf_state_peer_hton(&st->src, &sp->src);
3246 pf_state_peer_hton(&st->dst, &sp->dst);
3248 if (st->rule.ptr == NULL)
3249 sp->rule = htonl(-1);
3251 sp->rule = htonl(st->rule.ptr->nr);
3252 if (st->anchor.ptr == NULL)
3253 sp->anchor = htonl(-1);
3255 sp->anchor = htonl(st->anchor.ptr->nr);
3256 if (st->nat_rule.ptr == NULL)
3257 sp->nat_rule = htonl(-1);
3259 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
3261 pf_state_counter_hton(st->packets[0], sp->packets[0]);
3262 pf_state_counter_hton(st->packets[1], sp->packets[1]);
3263 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
3264 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
3269 pf_tbladdr_copyout(struct pf_addr_wrap *aw)
3271 struct pfr_ktable *kt;
3273 KASSERT(aw->type == PF_ADDR_TABLE, ("%s: type %u", __func__, aw->type));
3276 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
3277 kt = kt->pfrkt_root;
3279 aw->p.tblcnt = (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) ?
3284 * XXX - Check for version missmatch!!!
3287 pf_clear_states(void)
3292 for (i = 0; i <= V_pf_hashmask; i++) {
3293 struct pf_idhash *ih = &V_pf_idhash[i];
3295 PF_HASHROW_LOCK(ih);
3296 LIST_FOREACH(s, &ih->states, entry) {
3297 s->timeout = PFTM_PURGE;
3298 /* Don't send out individual delete messages. */
3299 s->sync_state = PFSTATE_NOSYNC;
3300 pf_unlink_state(s, PF_ENTER_LOCKED);
3303 PF_HASHROW_UNLOCK(ih);
3308 pf_clear_tables(void)
3310 struct pfioc_table io;
3313 bzero(&io, sizeof(io));
3315 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3322 pf_clear_srcnodes(struct pf_src_node *n)
3327 for (i = 0; i <= V_pf_hashmask; i++) {
3328 struct pf_idhash *ih = &V_pf_idhash[i];
3330 PF_HASHROW_LOCK(ih);
3331 LIST_FOREACH(s, &ih->states, entry) {
3332 if (n == NULL || n == s->src_node)
3334 if (n == NULL || n == s->nat_src_node)
3335 s->nat_src_node = NULL;
3337 PF_HASHROW_UNLOCK(ih);
3341 struct pf_srchash *sh;
3343 for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask;
3345 PF_HASHROW_LOCK(sh);
3346 LIST_FOREACH(n, &sh->nodes, entry) {
3350 PF_HASHROW_UNLOCK(sh);
3353 /* XXX: hash slot should already be locked here. */
3360 pf_kill_srcnodes(struct pfioc_src_node_kill *psnk)
3362 struct pf_src_node_list kill;
3365 for (int i = 0; i <= V_pf_srchashmask; i++) {
3366 struct pf_srchash *sh = &V_pf_srchash[i];
3367 struct pf_src_node *sn, *tmp;
3369 PF_HASHROW_LOCK(sh);
3370 LIST_FOREACH_SAFE(sn, &sh->nodes, entry, tmp)
3371 if (PF_MATCHA(psnk->psnk_src.neg,
3372 &psnk->psnk_src.addr.v.a.addr,
3373 &psnk->psnk_src.addr.v.a.mask,
3374 &sn->addr, sn->af) &&
3375 PF_MATCHA(psnk->psnk_dst.neg,
3376 &psnk->psnk_dst.addr.v.a.addr,
3377 &psnk->psnk_dst.addr.v.a.mask,
3378 &sn->raddr, sn->af)) {
3379 pf_unlink_src_node_locked(sn);
3380 LIST_INSERT_HEAD(&kill, sn, entry);
3383 PF_HASHROW_UNLOCK(sh);
3386 for (int i = 0; i <= V_pf_hashmask; i++) {
3387 struct pf_idhash *ih = &V_pf_idhash[i];
3390 PF_HASHROW_LOCK(ih);
3391 LIST_FOREACH(s, &ih->states, entry) {
3392 if (s->src_node && s->src_node->expire == 1) {
3394 s->src_node->states--;
3398 if (s->nat_src_node && s->nat_src_node->expire == 1) {
3400 s->nat_src_node->states--;
3402 s->nat_src_node = NULL;
3405 PF_HASHROW_UNLOCK(ih);
3408 psnk->psnk_killed = pf_free_src_nodes(&kill);
3412 * XXX - Check for version missmatch!!!
3416 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3425 V_pf_status.running = 0;
3427 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3429 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3432 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3434 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3435 break; /* XXX: rollback? */
3437 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3439 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3440 break; /* XXX: rollback? */
3442 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3444 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3445 break; /* XXX: rollback? */
3447 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3449 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3450 break; /* XXX: rollback? */
3453 /* XXX: these should always succeed here */
3454 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3455 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3456 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3457 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3458 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3460 if ((error = pf_clear_tables()) != 0)
3464 if ((error = pf_begin_altq(&t[0])) != 0) {
3465 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3468 pf_commit_altq(t[0]);
3473 pf_clear_srcnodes(NULL);
3475 /* status does not use malloced mem so no need to cleanup */
3476 /* fingerprints and interfaces have thier own cleanup code */
3484 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3489 chk = pf_test(PF_IN, ifp, m, inp);
3499 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3504 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3505 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3506 in_delayed_cksum(*m);
3507 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3510 chk = pf_test(PF_OUT, ifp, m, inp);
3522 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3528 * In case of loopback traffic IPv6 uses the real interface in
3529 * order to support scoped addresses. In order to support stateful
3530 * filtering we have change this to lo0 as it is the case in IPv4.
3532 CURVNET_SET(ifp->if_vnet);
3533 chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? V_loif : ifp, m, inp);
3543 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3548 /* We need a proper CSUM before we start (s. OpenBSD ip_output) */
3549 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3551 /* XXX-BZ copy&paste error from r126261? */
3552 in_delayed_cksum(*m);
3554 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3556 CURVNET_SET(ifp->if_vnet);
3557 chk = pf_test6(PF_OUT, ifp, m, inp);
3571 struct pfil_head *pfh_inet;
3574 struct pfil_head *pfh_inet6;
3577 if (V_pf_pfil_hooked)
3581 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3582 if (pfh_inet == NULL)
3583 return (ESRCH); /* XXX */
3584 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3585 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3588 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3589 if (pfh_inet6 == NULL) {
3591 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3593 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3596 return (ESRCH); /* XXX */
3598 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3599 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3602 V_pf_pfil_hooked = 1;
3610 struct pfil_head *pfh_inet;
3613 struct pfil_head *pfh_inet6;
3616 if (V_pf_pfil_hooked == 0)
3620 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3621 if (pfh_inet == NULL)
3622 return (ESRCH); /* XXX */
3623 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3625 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3629 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3630 if (pfh_inet6 == NULL)
3631 return (ESRCH); /* XXX */
3632 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3634 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3638 V_pf_pfil_hooked = 0;
3647 VNET_ITERATOR_DECL(vnet_iter);
3650 VNET_FOREACH(vnet_iter) {
3651 CURVNET_SET(vnet_iter);
3652 V_pf_pfil_hooked = 0;
3653 V_pf_end_threads = 0;
3654 TAILQ_INIT(&V_pf_tags);
3655 TAILQ_INIT(&V_pf_qids);
3658 VNET_LIST_RUNLOCK();
3660 rw_init(&pf_rules_lock, "pf rulesets");
3662 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3663 if ((error = pfattach()) != 0)
3675 V_pf_status.running = 0;
3677 swi_remove(V_pf_swi_cookie);
3678 error = dehook_pf();
3681 * Should not happen!
3682 * XXX Due to error code ESRCH, kldunload will show
3683 * a message like 'No such process'.
3685 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3690 V_pf_end_threads = 1;
3691 while (V_pf_end_threads < 2) {
3692 wakeup_one(pf_purge_thread);
3693 rw_sleep(pf_purge_thread, &pf_rules_lock, 0, "pftmo", 0);
3695 pf_normalize_cleanup();
3701 destroy_dev(pf_dev);
3702 rw_destroy(&pf_rules_lock);
3708 pf_modevent(module_t mod, int type, void *data)
3718 * Module should not be unloaded due to race conditions.
3723 error = pf_unload();
3733 static moduledata_t pf_mod = {
3739 DECLARE_MODULE(pf, pf_mod, SI_SUB_PSEUDO, SI_ORDER_FIRST);
3740 MODULE_VERSION(pf, PF_MODVER);