1 /* $OpenBSD: pf_ioctl.c,v 1.175 2007/02/26 22:47:43 deraadt Exp $ */
4 * Copyright (c) 2001 Daniel Hartmeier
5 * Copyright (c) 2002,2003 Henning Brauer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * Effort sponsored in part by the Defense Advanced Research Projects
33 * Agency (DARPA) and Air Force Research Laboratory, Air Force
34 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
40 #include "opt_inet6.h"
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
51 #define NBPFILTER DEV_BPF
57 #define NPFLOG DEV_PFLOG
63 #define NPFSYNC DEV_PFSYNC
74 #include <sys/param.h>
75 #include <sys/systm.h>
77 #include <sys/filio.h>
78 #include <sys/fcntl.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/kernel.h>
83 #include <sys/malloc.h>
85 #include <sys/module.h>
88 #include <sys/sysctl.h>
89 #include <sys/vimage.h>
91 #include <sys/timeout.h>
95 #include <sys/malloc.h>
96 #include <sys/kthread.h>
98 #include <sys/rwlock.h>
99 #include <uvm/uvm_extern.h>
103 #include <net/if_types.h>
104 #include <net/route.h>
106 #include <net/vnet.h>
109 #include <netinet/in.h>
110 #include <netinet/in_var.h>
111 #include <netinet/in_systm.h>
112 #include <netinet/ip.h>
113 #include <netinet/ip_var.h>
114 #include <netinet/ip_icmp.h>
119 #include <dev/rndvar.h>
120 #include <crypto/md5.h>
122 #include <net/pfvar.h>
125 #include <net/if_pfsync.h>
126 #endif /* NPFSYNC > 0 */
128 #include <net/if_pflog.h>
131 #include <netinet/ip6.h>
132 #include <netinet/in_pcb.h>
136 #include <altq/altq.h>
140 #include <sys/limits.h>
141 #include <sys/lock.h>
142 #include <sys/mutex.h>
143 #include <net/pfil.h>
144 #endif /* __FreeBSD__ */
147 void init_zone_var(void);
148 void cleanup_pf_zone(void);
152 void pf_thread_create(void *);
153 int pfopen(dev_t, int, int, struct proc *);
154 int pfclose(dev_t, int, int, struct proc *);
156 struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
157 u_int8_t, u_int8_t, u_int8_t);
159 void pf_mv_pool(struct pf_palist *, struct pf_palist *);
160 void pf_empty_pool(struct pf_palist *);
162 int pfioctl(struct cdev *, u_long, caddr_t, int, struct thread *);
164 int pfioctl(struct cdev *, u_long, caddr_t, int, struct proc *);
167 int pf_begin_altq(u_int32_t *);
168 int pf_rollback_altq(u_int32_t);
169 int pf_commit_altq(u_int32_t);
170 int pf_enable_altq(struct pf_altq *);
171 int pf_disable_altq(struct pf_altq *);
173 int pf_begin_rules(u_int32_t *, int, const char *);
174 int pf_rollback_rules(u_int32_t, int, char *);
175 int pf_setup_pfsync_matching(struct pf_ruleset *);
176 void pf_hash_rule(MD5_CTX *, struct pf_rule *);
177 void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
178 int pf_commit_rules(u_int32_t, int, char *);
180 struct pf_rule pf_default_rule;
182 struct sx pf_consistency_lock;
183 SX_SYSINIT(pf_consistency_lock, &pf_consistency_lock, "pf_statetbl_lock");
185 struct rwlock pf_consistency_lock = RWLOCK_INITIALIZER;
188 static int pf_altq_running;
191 #define TAGID_MAX 50000
192 TAILQ_HEAD(pf_tags, pf_tagname) pf_tags = TAILQ_HEAD_INITIALIZER(pf_tags),
193 pf_qids = TAILQ_HEAD_INITIALIZER(pf_qids);
195 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
196 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
198 u_int16_t tagname2tag(struct pf_tags *, char *);
199 void tag2tagname(struct pf_tags *, u_int16_t, char *);
200 void tag_unref(struct pf_tags *, u_int16_t);
201 int pf_rtlabel_add(struct pf_addr_wrap *);
202 void pf_rtlabel_remove(struct pf_addr_wrap *);
203 void pf_rtlabel_copyout(struct pf_addr_wrap *);
205 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
209 static struct cdev *pf_dev;
212 * XXX - These are new and need to be checked when moveing to a new version
214 static void pf_clear_states(void);
215 static int pf_clear_tables(void);
216 static void pf_clear_srcnodes(void);
218 * XXX - These are new and need to be checked when moveing to a new version
222 * Wrapper functions for pfil(9) hooks
224 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
225 int dir, struct inpcb *inp);
226 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
227 int dir, struct inpcb *inp);
229 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
230 int dir, struct inpcb *inp);
231 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
232 int dir, struct inpcb *inp);
235 static int hook_pf(void);
236 static int dehook_pf(void);
237 static int shutdown_pf(void);
238 static int pf_load(void);
239 static int pf_unload(void);
241 static struct cdevsw pf_cdevsw = {
244 .d_version = D_VERSION,
247 static volatile int pf_pfil_hooked = 0;
248 int pf_end_threads = 0;
249 struct mtx pf_task_mtx;
250 pflog_packet_t *pflog_packet_ptr = NULL;
252 int debug_pfugidhack = 0;
253 SYSCTL_INT(_debug, OID_AUTO, pfugidhack, CTLFLAG_RW, &debug_pfugidhack, 0,
254 "Enable/disable pf user/group rules mpsafe hack");
259 mtx_init(&pf_task_mtx, "pf task mtx", NULL, MTX_DEF);
263 destroy_pf_mutex(void)
265 mtx_destroy(&pf_task_mtx);
271 pf_src_tree_pl = pf_rule_pl = NULL;
272 pf_state_pl = pf_altq_pl = pf_pooladdr_pl = NULL;
273 pf_frent_pl = pf_frag_pl = pf_cache_pl = pf_cent_pl = NULL;
274 pf_state_scrub_pl = NULL;
275 pfr_ktable_pl = pfr_kentry_pl = NULL;
279 cleanup_pf_zone(void)
281 UMA_DESTROY(pf_src_tree_pl);
282 UMA_DESTROY(pf_rule_pl);
283 UMA_DESTROY(pf_state_pl);
284 UMA_DESTROY(pf_altq_pl);
285 UMA_DESTROY(pf_pooladdr_pl);
286 UMA_DESTROY(pf_frent_pl);
287 UMA_DESTROY(pf_frag_pl);
288 UMA_DESTROY(pf_cache_pl);
289 UMA_DESTROY(pf_cent_pl);
290 UMA_DESTROY(pfr_ktable_pl);
291 UMA_DESTROY(pfr_kentry_pl2);
292 UMA_DESTROY(pfr_kentry_pl);
293 UMA_DESTROY(pf_state_scrub_pl);
294 UMA_DESTROY(pfi_addr_pl);
300 u_int32_t *my_timeout = pf_default_rule.timeout;
304 UMA_CREATE(pf_src_tree_pl,struct pf_src_node, "pfsrctrpl");
305 UMA_CREATE(pf_rule_pl, struct pf_rule, "pfrulepl");
306 UMA_CREATE(pf_state_pl, struct pf_state, "pfstatepl");
307 UMA_CREATE(pf_altq_pl, struct pf_altq, "pfaltqpl");
308 UMA_CREATE(pf_pooladdr_pl, struct pf_pooladdr, "pfpooladdrpl");
309 UMA_CREATE(pfr_ktable_pl, struct pfr_ktable, "pfrktable");
310 UMA_CREATE(pfr_kentry_pl, struct pfr_kentry, "pfrkentry");
311 UMA_CREATE(pfr_kentry_pl2, struct pfr_kentry, "pfrkentry2");
312 UMA_CREATE(pf_frent_pl, struct pf_frent, "pffrent");
313 UMA_CREATE(pf_frag_pl, struct pf_fragment, "pffrag");
314 UMA_CREATE(pf_cache_pl, struct pf_fragment, "pffrcache");
315 UMA_CREATE(pf_cent_pl, struct pf_frcache, "pffrcent");
316 UMA_CREATE(pf_state_scrub_pl, struct pf_state_scrub,
318 UMA_CREATE(pfi_addr_pl, struct pfi_dynaddr, "pfiaddrpl");
327 if ( (error = pf_osfp_initialize()) ) {
333 pf_pool_limits[PF_LIMIT_STATES].pp = pf_state_pl;
334 pf_pool_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
335 pf_pool_limits[PF_LIMIT_SRC_NODES].pp = pf_src_tree_pl;
336 pf_pool_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
337 pf_pool_limits[PF_LIMIT_FRAGS].pp = pf_frent_pl;
338 pf_pool_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
339 pf_pool_limits[PF_LIMIT_TABLES].pp = pfr_ktable_pl;
340 pf_pool_limits[PF_LIMIT_TABLES].limit = PFR_KTABLE_HIWAT;
341 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].pp = pfr_kentry_pl;
342 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit = PFR_KENTRY_HIWAT;
343 uma_zone_set_max(pf_pool_limits[PF_LIMIT_STATES].pp,
344 pf_pool_limits[PF_LIMIT_STATES].limit);
346 RB_INIT(&tree_src_tracking);
347 RB_INIT(&pf_anchors);
348 pf_init_ruleset(&pf_main_ruleset);
349 TAILQ_INIT(&pf_altqs[0]);
350 TAILQ_INIT(&pf_altqs[1]);
351 TAILQ_INIT(&pf_pabuf);
352 pf_altqs_active = &pf_altqs[0];
353 pf_altqs_inactive = &pf_altqs[1];
354 TAILQ_INIT(&state_list);
356 /* default rule should never be garbage collected */
357 pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
358 pf_default_rule.action = PF_PASS;
359 pf_default_rule.nr = -1;
360 pf_default_rule.rtableid = -1;
362 /* initialize default timeouts */
363 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
364 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
365 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
366 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
367 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
368 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
369 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
370 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
371 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
372 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
373 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
374 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
375 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
376 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
377 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
378 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
379 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
380 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
381 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
382 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
385 bzero(&pf_status, sizeof(pf_status));
386 pf_status.debug = PF_DEBUG_URGENT;
390 /* XXX do our best to avoid a conflict */
391 pf_status.hostid = arc4random();
393 if (kproc_create(pf_purge_thread, NULL, NULL, 0, 0, "pfpurge"))
398 #else /* !__FreeBSD__ */
402 u_int32_t *timeout = pf_default_rule.timeout;
404 pool_init(&pf_rule_pl, sizeof(struct pf_rule), 0, 0, 0, "pfrulepl",
405 &pool_allocator_nointr);
406 pool_init(&pf_src_tree_pl, sizeof(struct pf_src_node), 0, 0, 0,
408 pool_init(&pf_state_pl, sizeof(struct pf_state), 0, 0, 0, "pfstatepl",
410 pool_init(&pf_altq_pl, sizeof(struct pf_altq), 0, 0, 0, "pfaltqpl",
411 &pool_allocator_nointr);
412 pool_init(&pf_pooladdr_pl, sizeof(struct pf_pooladdr), 0, 0, 0,
413 "pfpooladdrpl", &pool_allocator_nointr);
416 pf_osfp_initialize();
418 pool_sethardlimit(pf_pool_limits[PF_LIMIT_STATES].pp,
419 pf_pool_limits[PF_LIMIT_STATES].limit, NULL, 0);
421 if (ctob(physmem) <= 100*1024*1024)
422 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit =
423 PFR_KENTRY_HIWAT_SMALL;
425 RB_INIT(&tree_src_tracking);
426 RB_INIT(&pf_anchors);
427 pf_init_ruleset(&pf_main_ruleset);
428 TAILQ_INIT(&pf_altqs[0]);
429 TAILQ_INIT(&pf_altqs[1]);
430 TAILQ_INIT(&pf_pabuf);
431 pf_altqs_active = &pf_altqs[0];
432 pf_altqs_inactive = &pf_altqs[1];
433 TAILQ_INIT(&state_list);
435 /* default rule should never be garbage collected */
436 pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
437 pf_default_rule.action = PF_PASS;
438 pf_default_rule.nr = -1;
439 pf_default_rule.rtableid = -1;
441 /* initialize default timeouts */
442 timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
443 timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
444 timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
445 timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
446 timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
447 timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
448 timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
449 timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
450 timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
451 timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
452 timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
453 timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
454 timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
455 timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
456 timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
457 timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
458 timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
459 timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
460 timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
461 timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
464 bzero(&pf_status, sizeof(pf_status));
465 pf_status.debug = PF_DEBUG_URGENT;
467 /* XXX do our best to avoid a conflict */
468 pf_status.hostid = arc4random();
470 /* require process context to purge states, so perform in a thread */
471 kproc_create_deferred(pf_thread_create, NULL);
475 pf_thread_create(void *v)
477 if (kproc_create(pf_purge_thread, NULL, NULL, "pfpurge"))
478 panic("pfpurge thread");
480 #endif /* __FreeBSD__ */
483 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
484 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
485 u_int8_t check_ticket)
487 struct pf_ruleset *ruleset;
488 struct pf_rule *rule;
491 ruleset = pf_find_ruleset(anchor);
494 rs_num = pf_get_ruleset_number(rule_action);
495 if (rs_num >= PF_RULESET_MAX)
498 if (check_ticket && ticket !=
499 ruleset->rules[rs_num].active.ticket)
502 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
505 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
507 if (check_ticket && ticket !=
508 ruleset->rules[rs_num].inactive.ticket)
511 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
514 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
517 while ((rule != NULL) && (rule->nr != rule_number))
518 rule = TAILQ_NEXT(rule, entries);
523 return (&rule->rpool);
527 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
529 struct pf_pooladdr *mv_pool_pa;
531 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
532 TAILQ_REMOVE(poola, mv_pool_pa, entries);
533 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
538 pf_empty_pool(struct pf_palist *poola)
540 struct pf_pooladdr *empty_pool_pa;
542 while ((empty_pool_pa = TAILQ_FIRST(poola)) != NULL) {
543 pfi_dynaddr_remove(&empty_pool_pa->addr);
544 pf_tbladdr_remove(&empty_pool_pa->addr);
545 pfi_kif_unref(empty_pool_pa->kif, PFI_KIF_REF_RULE);
546 TAILQ_REMOVE(poola, empty_pool_pa, entries);
547 pool_put(&pf_pooladdr_pl, empty_pool_pa);
552 pf_rm_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
554 if (rulequeue != NULL) {
555 if (rule->states <= 0) {
557 * XXX - we need to remove the table *before* detaching
558 * the rule to make sure the table code does not delete
559 * the anchor under our feet.
561 pf_tbladdr_remove(&rule->src.addr);
562 pf_tbladdr_remove(&rule->dst.addr);
563 if (rule->overload_tbl)
564 pfr_detach_table(rule->overload_tbl);
566 TAILQ_REMOVE(rulequeue, rule, entries);
567 rule->entries.tqe_prev = NULL;
571 if (rule->states > 0 || rule->src_nodes > 0 ||
572 rule->entries.tqe_prev != NULL)
574 pf_tag_unref(rule->tag);
575 pf_tag_unref(rule->match_tag);
577 if (rule->pqid != rule->qid)
578 pf_qid_unref(rule->pqid);
579 pf_qid_unref(rule->qid);
581 pf_rtlabel_remove(&rule->src.addr);
582 pf_rtlabel_remove(&rule->dst.addr);
583 pfi_dynaddr_remove(&rule->src.addr);
584 pfi_dynaddr_remove(&rule->dst.addr);
585 if (rulequeue == NULL) {
586 pf_tbladdr_remove(&rule->src.addr);
587 pf_tbladdr_remove(&rule->dst.addr);
588 if (rule->overload_tbl)
589 pfr_detach_table(rule->overload_tbl);
591 pfi_kif_unref(rule->kif, PFI_KIF_REF_RULE);
592 pf_anchor_remove(rule);
593 pf_empty_pool(&rule->rpool.list);
594 pool_put(&pf_rule_pl, rule);
598 tagname2tag(struct pf_tags *head, char *tagname)
600 struct pf_tagname *tag, *p = NULL;
601 u_int16_t new_tagid = 1;
603 TAILQ_FOREACH(tag, head, entries)
604 if (strcmp(tagname, tag->name) == 0) {
610 * to avoid fragmentation, we do a linear search from the beginning
611 * and take the first free slot we find. if there is none or the list
612 * is empty, append a new entry at the end.
616 if (!TAILQ_EMPTY(head))
617 for (p = TAILQ_FIRST(head); p != NULL &&
618 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
619 new_tagid = p->tag + 1;
621 if (new_tagid > TAGID_MAX)
624 /* allocate and fill new struct pf_tagname */
625 tag = (struct pf_tagname *)malloc(sizeof(struct pf_tagname),
629 bzero(tag, sizeof(struct pf_tagname));
630 strlcpy(tag->name, tagname, sizeof(tag->name));
631 tag->tag = new_tagid;
634 if (p != NULL) /* insert new entry before p */
635 TAILQ_INSERT_BEFORE(p, tag, entries);
636 else /* either list empty or no free slot in between */
637 TAILQ_INSERT_TAIL(head, tag, entries);
643 tag2tagname(struct pf_tags *head, u_int16_t tagid, char *p)
645 struct pf_tagname *tag;
647 TAILQ_FOREACH(tag, head, entries)
648 if (tag->tag == tagid) {
649 strlcpy(p, tag->name, PF_TAG_NAME_SIZE);
655 tag_unref(struct pf_tags *head, u_int16_t tag)
657 struct pf_tagname *p, *next;
662 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
663 next = TAILQ_NEXT(p, entries);
666 TAILQ_REMOVE(head, p, entries);
675 pf_tagname2tag(char *tagname)
677 return (tagname2tag(&pf_tags, tagname));
681 pf_tag2tagname(u_int16_t tagid, char *p)
683 tag2tagname(&pf_tags, tagid, p);
687 pf_tag_ref(u_int16_t tag)
689 struct pf_tagname *t;
691 TAILQ_FOREACH(t, &pf_tags, entries)
699 pf_tag_unref(u_int16_t tag)
701 tag_unref(&pf_tags, tag);
705 pf_rtlabel_add(struct pf_addr_wrap *a)
708 /* XXX_IMPORT: later */
711 if (a->type == PF_ADDR_RTLABEL &&
712 (a->v.rtlabel = rtlabel_name2id(a->v.rtlabelname)) == 0)
719 pf_rtlabel_remove(struct pf_addr_wrap *a)
722 /* XXX_IMPORT: later */
724 if (a->type == PF_ADDR_RTLABEL)
725 rtlabel_unref(a->v.rtlabel);
730 pf_rtlabel_copyout(struct pf_addr_wrap *a)
733 /* XXX_IMPORT: later */
734 if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel)
735 strlcpy(a->v.rtlabelname, "?", sizeof(a->v.rtlabelname));
739 if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel) {
740 if ((name = rtlabel_id2name(a->v.rtlabel)) == NULL)
741 strlcpy(a->v.rtlabelname, "?",
742 sizeof(a->v.rtlabelname));
744 strlcpy(a->v.rtlabelname, name,
745 sizeof(a->v.rtlabelname));
752 pf_qname2qid(char *qname)
754 return ((u_int32_t)tagname2tag(&pf_qids, qname));
758 pf_qid2qname(u_int32_t qid, char *p)
760 tag2tagname(&pf_qids, (u_int16_t)qid, p);
764 pf_qid_unref(u_int32_t qid)
766 tag_unref(&pf_qids, (u_int16_t)qid);
770 pf_begin_altq(u_int32_t *ticket)
772 struct pf_altq *altq;
775 /* Purge the old altq list */
776 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
777 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
779 if (altq->qname[0] == 0 &&
780 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
782 if (altq->qname[0] == 0) {
784 /* detach and destroy the discipline */
785 error = altq_remove(altq);
787 pf_qid_unref(altq->qid);
788 pool_put(&pf_altq_pl, altq);
792 *ticket = ++ticket_altqs_inactive;
793 altqs_inactive_open = 1;
798 pf_rollback_altq(u_int32_t ticket)
800 struct pf_altq *altq;
803 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
805 /* Purge the old altq list */
806 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
807 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
809 if (altq->qname[0] == 0 &&
810 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
812 if (altq->qname[0] == 0) {
814 /* detach and destroy the discipline */
815 error = altq_remove(altq);
817 pf_qid_unref(altq->qid);
818 pool_put(&pf_altq_pl, altq);
820 altqs_inactive_open = 0;
825 pf_commit_altq(u_int32_t ticket)
827 struct pf_altqqueue *old_altqs;
828 struct pf_altq *altq;
829 int s, err, error = 0;
831 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
834 /* swap altqs, keep the old. */
836 old_altqs = pf_altqs_active;
837 pf_altqs_active = pf_altqs_inactive;
838 pf_altqs_inactive = old_altqs;
839 ticket_altqs_active = ticket_altqs_inactive;
841 /* Attach new disciplines */
842 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
844 if (altq->qname[0] == 0 &&
845 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
847 if (altq->qname[0] == 0) {
849 /* attach the discipline */
850 error = altq_pfattach(altq);
851 if (error == 0 && pf_altq_running)
852 error = pf_enable_altq(altq);
860 /* Purge the old altq list */
861 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
862 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
864 if (altq->qname[0] == 0 &&
865 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
867 if (altq->qname[0] == 0) {
869 /* detach and destroy the discipline */
871 error = pf_disable_altq(altq);
872 err = altq_pfdetach(altq);
873 if (err != 0 && error == 0)
875 err = altq_remove(altq);
876 if (err != 0 && error == 0)
879 pf_qid_unref(altq->qid);
880 pool_put(&pf_altq_pl, altq);
884 altqs_inactive_open = 0;
889 pf_enable_altq(struct pf_altq *altq)
892 struct tb_profile tb;
895 if ((ifp = ifunit(altq->ifname)) == NULL)
898 if (ifp->if_snd.altq_type != ALTQT_NONE)
899 error = altq_enable(&ifp->if_snd);
901 /* set tokenbucket regulator */
902 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
903 tb.rate = altq->ifbandwidth;
904 tb.depth = altq->tbrsize;
909 error = tbr_set(&ifp->if_snd, &tb);
920 pf_disable_altq(struct pf_altq *altq)
923 struct tb_profile tb;
926 if ((ifp = ifunit(altq->ifname)) == NULL)
930 * when the discipline is no longer referenced, it was overridden
931 * by a new one. if so, just return.
933 if (altq->altq_disc != ifp->if_snd.altq_disc)
936 error = altq_disable(&ifp->if_snd);
939 /* clear tokenbucket regulator */
945 error = tbr_set(&ifp->if_snd, &tb);
957 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
960 struct pf_altq *a1, *a2, *a3;
964 /* Interrupt userland queue modifications */
965 if (altqs_inactive_open)
966 pf_rollback_altq(ticket_altqs_inactive);
968 /* Start new altq ruleset */
969 if (pf_begin_altq(&ticket))
972 /* Copy the current active set */
973 TAILQ_FOREACH(a1, pf_altqs_active, entries) {
974 a2 = pool_get(&pf_altq_pl, PR_NOWAIT);
979 bcopy(a1, a2, sizeof(struct pf_altq));
981 if (a2->qname[0] != 0) {
982 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
984 pool_put(&pf_altq_pl, a2);
987 a2->altq_disc = NULL;
988 TAILQ_FOREACH(a3, pf_altqs_inactive, entries) {
989 if (strncmp(a3->ifname, a2->ifname,
990 IFNAMSIZ) == 0 && a3->qname[0] == 0) {
991 a2->altq_disc = a3->altq_disc;
996 /* Deactivate the interface in question */
997 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
998 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
999 (remove && ifp1 == ifp)) {
1000 a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
1003 error = altq_add(a2);
1006 if (ticket != ticket_altqs_inactive)
1010 pool_put(&pf_altq_pl, a2);
1015 TAILQ_INSERT_TAIL(pf_altqs_inactive, a2, entries);
1019 pf_rollback_altq(ticket);
1021 pf_commit_altq(ticket);
1027 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
1029 struct pf_ruleset *rs;
1030 struct pf_rule *rule;
1032 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1034 rs = pf_find_or_create_ruleset(anchor);
1037 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1038 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
1039 rs->rules[rs_num].inactive.rcount--;
1041 *ticket = ++rs->rules[rs_num].inactive.ticket;
1042 rs->rules[rs_num].inactive.open = 1;
1047 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
1049 struct pf_ruleset *rs;
1050 struct pf_rule *rule;
1052 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1054 rs = pf_find_ruleset(anchor);
1055 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1056 rs->rules[rs_num].inactive.ticket != ticket)
1058 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1059 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
1060 rs->rules[rs_num].inactive.rcount--;
1062 rs->rules[rs_num].inactive.open = 0;
1066 #define PF_MD5_UPD(st, elm) \
1067 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
1069 #define PF_MD5_UPD_STR(st, elm) \
1070 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
1072 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
1073 (stor) = htonl((st)->elm); \
1074 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
1077 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
1078 (stor) = htons((st)->elm); \
1079 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
1083 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
1085 PF_MD5_UPD(pfr, addr.type);
1086 switch (pfr->addr.type) {
1087 case PF_ADDR_DYNIFTL:
1088 PF_MD5_UPD(pfr, addr.v.ifname);
1089 PF_MD5_UPD(pfr, addr.iflags);
1092 PF_MD5_UPD(pfr, addr.v.tblname);
1094 case PF_ADDR_ADDRMASK:
1095 /* XXX ignore af? */
1096 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
1097 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
1099 case PF_ADDR_RTLABEL:
1100 PF_MD5_UPD(pfr, addr.v.rtlabelname);
1104 PF_MD5_UPD(pfr, port[0]);
1105 PF_MD5_UPD(pfr, port[1]);
1106 PF_MD5_UPD(pfr, neg);
1107 PF_MD5_UPD(pfr, port_op);
1111 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
1116 pf_hash_rule_addr(ctx, &rule->src);
1117 pf_hash_rule_addr(ctx, &rule->dst);
1118 PF_MD5_UPD_STR(rule, label);
1119 PF_MD5_UPD_STR(rule, ifname);
1120 PF_MD5_UPD_STR(rule, match_tagname);
1121 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
1122 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
1123 PF_MD5_UPD_HTONL(rule, prob, y);
1124 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
1125 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
1126 PF_MD5_UPD(rule, uid.op);
1127 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
1128 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
1129 PF_MD5_UPD(rule, gid.op);
1130 PF_MD5_UPD_HTONL(rule, rule_flag, y);
1131 PF_MD5_UPD(rule, action);
1132 PF_MD5_UPD(rule, direction);
1133 PF_MD5_UPD(rule, af);
1134 PF_MD5_UPD(rule, quick);
1135 PF_MD5_UPD(rule, ifnot);
1136 PF_MD5_UPD(rule, match_tag_not);
1137 PF_MD5_UPD(rule, natpass);
1138 PF_MD5_UPD(rule, keep_state);
1139 PF_MD5_UPD(rule, proto);
1140 PF_MD5_UPD(rule, type);
1141 PF_MD5_UPD(rule, code);
1142 PF_MD5_UPD(rule, flags);
1143 PF_MD5_UPD(rule, flagset);
1144 PF_MD5_UPD(rule, allow_opts);
1145 PF_MD5_UPD(rule, rt);
1146 PF_MD5_UPD(rule, tos);
1150 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
1152 struct pf_ruleset *rs;
1153 struct pf_rule *rule, **old_array;
1154 struct pf_rulequeue *old_rules;
1156 u_int32_t old_rcount;
1158 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1160 rs = pf_find_ruleset(anchor);
1161 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1162 ticket != rs->rules[rs_num].inactive.ticket)
1165 /* Calculate checksum for the main ruleset */
1166 if (rs == &pf_main_ruleset) {
1167 error = pf_setup_pfsync_matching(rs);
1172 /* Swap rules, keep the old. */
1174 old_rules = rs->rules[rs_num].active.ptr;
1175 old_rcount = rs->rules[rs_num].active.rcount;
1176 old_array = rs->rules[rs_num].active.ptr_array;
1178 rs->rules[rs_num].active.ptr =
1179 rs->rules[rs_num].inactive.ptr;
1180 rs->rules[rs_num].active.ptr_array =
1181 rs->rules[rs_num].inactive.ptr_array;
1182 rs->rules[rs_num].active.rcount =
1183 rs->rules[rs_num].inactive.rcount;
1184 rs->rules[rs_num].inactive.ptr = old_rules;
1185 rs->rules[rs_num].inactive.ptr_array = old_array;
1186 rs->rules[rs_num].inactive.rcount = old_rcount;
1188 rs->rules[rs_num].active.ticket =
1189 rs->rules[rs_num].inactive.ticket;
1190 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
1193 /* Purge the old rule list. */
1194 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
1195 pf_rm_rule(old_rules, rule);
1196 if (rs->rules[rs_num].inactive.ptr_array)
1197 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
1198 rs->rules[rs_num].inactive.ptr_array = NULL;
1199 rs->rules[rs_num].inactive.rcount = 0;
1200 rs->rules[rs_num].inactive.open = 0;
1201 pf_remove_if_empty_ruleset(rs);
1207 pf_setup_pfsync_matching(struct pf_ruleset *rs)
1210 struct pf_rule *rule;
1212 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
1215 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
1216 /* XXX PF_RULESET_SCRUB as well? */
1217 if (rs_cnt == PF_RULESET_SCRUB)
1220 if (rs->rules[rs_cnt].inactive.ptr_array)
1221 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
1222 rs->rules[rs_cnt].inactive.ptr_array = NULL;
1224 if (rs->rules[rs_cnt].inactive.rcount) {
1225 rs->rules[rs_cnt].inactive.ptr_array =
1226 malloc(sizeof(caddr_t) *
1227 rs->rules[rs_cnt].inactive.rcount,
1230 if (!rs->rules[rs_cnt].inactive.ptr_array)
1234 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
1236 pf_hash_rule(&ctx, rule);
1237 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
1241 MD5Final(digest, &ctx);
1242 memcpy(pf_status.pf_chksum, digest, sizeof(pf_status.pf_chksum));
1248 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1250 pfioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
1253 struct pf_pooladdr *pa = NULL;
1254 struct pf_pool *pool = NULL;
1260 /* XXX keep in sync with switch() below */
1262 if (securelevel_gt(td->td_ucred, 2))
1264 if (securelevel > 1)
1272 case DIOCSETSTATUSIF:
1278 case DIOCGETTIMEOUT:
1279 case DIOCCLRRULECTRS:
1284 case DIOCGETRULESETS:
1285 case DIOCGETRULESET:
1286 case DIOCRGETTABLES:
1287 case DIOCRGETTSTATS:
1288 case DIOCRCLRTSTATS:
1294 case DIOCRGETASTATS:
1295 case DIOCRCLRASTATS:
1298 case DIOCGETSRCNODES:
1299 case DIOCCLRSRCNODES:
1300 case DIOCIGETIFACES:
1307 case DIOCRCLRTABLES:
1308 case DIOCRADDTABLES:
1309 case DIOCRDELTABLES:
1310 case DIOCRSETTFLAGS:
1311 if (((struct pfioc_table *)addr)->pfrio_flags &
1313 break; /* dummy operation ok */
1319 if (!(flags & FWRITE))
1327 case DIOCGETTIMEOUT:
1332 case DIOCGETRULESETS:
1333 case DIOCGETRULESET:
1335 case DIOCRGETTABLES:
1336 case DIOCRGETTSTATS:
1338 case DIOCRGETASTATS:
1341 case DIOCGETSRCNODES:
1342 case DIOCIGETIFACES:
1347 case DIOCRCLRTABLES:
1348 case DIOCRADDTABLES:
1349 case DIOCRDELTABLES:
1350 case DIOCRCLRTSTATS:
1355 case DIOCRSETTFLAGS:
1356 if (((struct pfioc_table *)addr)->pfrio_flags &
1358 flags |= FWRITE; /* need write lock for dummy */
1359 break; /* dummy operation ok */
1363 if (((struct pfioc_rule *)addr)->action == PF_GET_CLR_CNTR)
1372 sx_xlock(&pf_consistency_lock);
1374 sx_slock(&pf_consistency_lock);
1376 rw_enter_write(&pf_consistency_lock);
1378 rw_enter_read(&pf_consistency_lock);
1389 if (pf_status.running)
1397 DPFPRINTF(PF_DEBUG_MISC,
1398 ("pf: pfil registeration fail\n"));
1402 pf_status.running = 1;
1403 pf_status.since = time_second;
1404 if (pf_status.stateid == 0) {
1405 pf_status.stateid = time_second;
1406 pf_status.stateid = pf_status.stateid << 32;
1408 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1413 if (!pf_status.running)
1416 pf_status.running = 0;
1419 error = dehook_pf();
1422 pf_status.running = 1;
1423 DPFPRINTF(PF_DEBUG_MISC,
1424 ("pf: pfil unregisteration failed\n"));
1427 pf_status.since = time_second;
1428 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1433 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1434 struct pf_ruleset *ruleset;
1435 struct pf_rule *rule, *tail;
1436 struct pf_pooladdr *pa;
1439 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1440 ruleset = pf_find_ruleset(pr->anchor);
1441 if (ruleset == NULL) {
1445 rs_num = pf_get_ruleset_number(pr->rule.action);
1446 if (rs_num >= PF_RULESET_MAX) {
1450 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1454 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1456 DPFPRINTF(PF_DEBUG_MISC,
1457 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1458 ruleset->rules[rs_num].inactive.ticket));
1463 if (pr->pool_ticket != ticket_pabuf) {
1465 DPFPRINTF(PF_DEBUG_MISC,
1466 ("pool_ticket: %d != %d\n", pr->pool_ticket,
1472 rule = pool_get(&pf_rule_pl, PR_NOWAIT);
1477 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1479 rule->cuid = td->td_ucred->cr_ruid;
1480 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1482 rule->cuid = p->p_cred->p_ruid;
1483 rule->cpid = p->p_pid;
1485 rule->anchor = NULL;
1487 TAILQ_INIT(&rule->rpool.list);
1488 /* initialize refcounting */
1490 rule->src_nodes = 0;
1491 rule->entries.tqe_prev = NULL;
1493 if (rule->af == AF_INET) {
1494 pool_put(&pf_rule_pl, rule);
1495 error = EAFNOSUPPORT;
1500 if (rule->af == AF_INET6) {
1501 pool_put(&pf_rule_pl, rule);
1502 error = EAFNOSUPPORT;
1506 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1509 rule->nr = tail->nr + 1;
1512 if (rule->ifname[0]) {
1513 rule->kif = pfi_kif_get(rule->ifname);
1514 if (rule->kif == NULL) {
1515 pool_put(&pf_rule_pl, rule);
1519 pfi_kif_ref(rule->kif, PFI_KIF_REF_RULE);
1522 #ifdef __FreeBSD__ /* ROUTING */
1523 if (rule->rtableid > 0 && rule->rtableid > rt_numfibs)
1525 if (rule->rtableid > 0 && !rtable_exists(rule->rtableid))
1531 if (rule->qname[0] != 0) {
1532 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1534 else if (rule->pqname[0] != 0) {
1536 pf_qname2qid(rule->pqname)) == 0)
1539 rule->pqid = rule->qid;
1542 if (rule->tagname[0])
1543 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1545 if (rule->match_tagname[0])
1546 if ((rule->match_tag =
1547 pf_tagname2tag(rule->match_tagname)) == 0)
1549 if (rule->rt && !rule->direction)
1556 if (rule->logif >= PFLOGIFS_MAX)
1559 if (pf_rtlabel_add(&rule->src.addr) ||
1560 pf_rtlabel_add(&rule->dst.addr))
1562 if (pfi_dynaddr_setup(&rule->src.addr, rule->af))
1564 if (pfi_dynaddr_setup(&rule->dst.addr, rule->af))
1566 if (pf_tbladdr_setup(ruleset, &rule->src.addr))
1568 if (pf_tbladdr_setup(ruleset, &rule->dst.addr))
1570 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1572 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1573 if (pf_tbladdr_setup(ruleset, &pa->addr))
1576 if (rule->overload_tblname[0]) {
1577 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1578 rule->overload_tblname)) == NULL)
1581 rule->overload_tbl->pfrkt_flags |=
1585 pf_mv_pool(&pf_pabuf, &rule->rpool.list);
1586 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1587 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1588 (rule->rt > PF_FASTROUTE)) &&
1589 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1593 pf_rm_rule(NULL, rule);
1598 if (!debug_pfugidhack && (rule->uid.op || rule->gid.op ||
1599 rule->log & PF_LOG_SOCKET_LOOKUP)) {
1600 DPFPRINTF(PF_DEBUG_MISC,
1601 ("pf: debug.pfugidhack enabled\n"));
1602 debug_pfugidhack = 1;
1606 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1607 rule->evaluations = rule->packets[0] = rule->packets[1] =
1608 rule->bytes[0] = rule->bytes[1] = 0;
1609 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1611 ruleset->rules[rs_num].inactive.rcount++;
1615 case DIOCGETRULES: {
1616 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1617 struct pf_ruleset *ruleset;
1618 struct pf_rule *tail;
1621 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1622 ruleset = pf_find_ruleset(pr->anchor);
1623 if (ruleset == NULL) {
1627 rs_num = pf_get_ruleset_number(pr->rule.action);
1628 if (rs_num >= PF_RULESET_MAX) {
1632 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1635 pr->nr = tail->nr + 1;
1638 pr->ticket = ruleset->rules[rs_num].active.ticket;
1643 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1644 struct pf_ruleset *ruleset;
1645 struct pf_rule *rule;
1648 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1649 ruleset = pf_find_ruleset(pr->anchor);
1650 if (ruleset == NULL) {
1654 rs_num = pf_get_ruleset_number(pr->rule.action);
1655 if (rs_num >= PF_RULESET_MAX) {
1659 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1663 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1664 while ((rule != NULL) && (rule->nr != pr->nr))
1665 rule = TAILQ_NEXT(rule, entries);
1670 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1671 if (pf_anchor_copyout(ruleset, rule, pr)) {
1675 pfi_dynaddr_copyout(&pr->rule.src.addr);
1676 pfi_dynaddr_copyout(&pr->rule.dst.addr);
1677 pf_tbladdr_copyout(&pr->rule.src.addr);
1678 pf_tbladdr_copyout(&pr->rule.dst.addr);
1679 pf_rtlabel_copyout(&pr->rule.src.addr);
1680 pf_rtlabel_copyout(&pr->rule.dst.addr);
1681 for (i = 0; i < PF_SKIP_COUNT; ++i)
1682 if (rule->skip[i].ptr == NULL)
1683 pr->rule.skip[i].nr = -1;
1685 pr->rule.skip[i].nr =
1686 rule->skip[i].ptr->nr;
1688 if (pr->action == PF_GET_CLR_CNTR) {
1689 rule->evaluations = 0;
1690 rule->packets[0] = rule->packets[1] = 0;
1691 rule->bytes[0] = rule->bytes[1] = 0;
1696 case DIOCCHANGERULE: {
1697 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1698 struct pf_ruleset *ruleset;
1699 struct pf_rule *oldrule = NULL, *newrule = NULL;
1703 if (!(pcr->action == PF_CHANGE_REMOVE ||
1704 pcr->action == PF_CHANGE_GET_TICKET) &&
1705 pcr->pool_ticket != ticket_pabuf) {
1710 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1711 pcr->action > PF_CHANGE_GET_TICKET) {
1715 ruleset = pf_find_ruleset(pcr->anchor);
1716 if (ruleset == NULL) {
1720 rs_num = pf_get_ruleset_number(pcr->rule.action);
1721 if (rs_num >= PF_RULESET_MAX) {
1726 if (pcr->action == PF_CHANGE_GET_TICKET) {
1727 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1731 ruleset->rules[rs_num].active.ticket) {
1735 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1741 if (pcr->action != PF_CHANGE_REMOVE) {
1742 newrule = pool_get(&pf_rule_pl, PR_NOWAIT);
1743 if (newrule == NULL) {
1747 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1749 newrule->cuid = td->td_ucred->cr_ruid;
1750 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1752 newrule->cuid = p->p_cred->p_ruid;
1753 newrule->cpid = p->p_pid;
1755 TAILQ_INIT(&newrule->rpool.list);
1756 /* initialize refcounting */
1757 newrule->states = 0;
1758 newrule->entries.tqe_prev = NULL;
1760 if (newrule->af == AF_INET) {
1761 pool_put(&pf_rule_pl, newrule);
1762 error = EAFNOSUPPORT;
1767 if (newrule->af == AF_INET6) {
1768 pool_put(&pf_rule_pl, newrule);
1769 error = EAFNOSUPPORT;
1773 if (newrule->ifname[0]) {
1774 newrule->kif = pfi_kif_get(newrule->ifname);
1775 if (newrule->kif == NULL) {
1776 pool_put(&pf_rule_pl, newrule);
1780 pfi_kif_ref(newrule->kif, PFI_KIF_REF_RULE);
1782 newrule->kif = NULL;
1784 if (newrule->rtableid > 0 &&
1785 #ifdef __FreeBSD__ /* ROUTING */
1786 newrule->rtableid > rt_numfibs)
1788 !rtable_exists(newrule->rtableid))
1794 if (newrule->qname[0] != 0) {
1796 pf_qname2qid(newrule->qname)) == 0)
1798 else if (newrule->pqname[0] != 0) {
1799 if ((newrule->pqid =
1800 pf_qname2qid(newrule->pqname)) == 0)
1803 newrule->pqid = newrule->qid;
1806 if (newrule->tagname[0])
1808 pf_tagname2tag(newrule->tagname)) == 0)
1810 if (newrule->match_tagname[0])
1811 if ((newrule->match_tag = pf_tagname2tag(
1812 newrule->match_tagname)) == 0)
1814 if (newrule->rt && !newrule->direction)
1820 if (newrule->logif >= PFLOGIFS_MAX)
1824 if (pf_rtlabel_add(&newrule->src.addr) ||
1825 pf_rtlabel_add(&newrule->dst.addr))
1827 if (pfi_dynaddr_setup(&newrule->src.addr, newrule->af))
1829 if (pfi_dynaddr_setup(&newrule->dst.addr, newrule->af))
1831 if (pf_tbladdr_setup(ruleset, &newrule->src.addr))
1833 if (pf_tbladdr_setup(ruleset, &newrule->dst.addr))
1835 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1837 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1838 if (pf_tbladdr_setup(ruleset, &pa->addr))
1841 if (newrule->overload_tblname[0]) {
1842 if ((newrule->overload_tbl = pfr_attach_table(
1843 ruleset, newrule->overload_tblname)) ==
1847 newrule->overload_tbl->pfrkt_flags |=
1851 pf_mv_pool(&pf_pabuf, &newrule->rpool.list);
1852 if (((((newrule->action == PF_NAT) ||
1853 (newrule->action == PF_RDR) ||
1854 (newrule->action == PF_BINAT) ||
1855 (newrule->rt > PF_FASTROUTE)) &&
1856 !newrule->anchor)) &&
1857 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1861 pf_rm_rule(NULL, newrule);
1866 if (!debug_pfugidhack && (newrule->uid.op ||
1868 newrule->log & PF_LOG_SOCKET_LOOKUP)) {
1869 DPFPRINTF(PF_DEBUG_MISC,
1870 ("pf: debug.pfugidhack enabled\n"));
1871 debug_pfugidhack = 1;
1875 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1876 newrule->evaluations = 0;
1877 newrule->packets[0] = newrule->packets[1] = 0;
1878 newrule->bytes[0] = newrule->bytes[1] = 0;
1880 pf_empty_pool(&pf_pabuf);
1882 if (pcr->action == PF_CHANGE_ADD_HEAD)
1883 oldrule = TAILQ_FIRST(
1884 ruleset->rules[rs_num].active.ptr);
1885 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1886 oldrule = TAILQ_LAST(
1887 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1889 oldrule = TAILQ_FIRST(
1890 ruleset->rules[rs_num].active.ptr);
1891 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1892 oldrule = TAILQ_NEXT(oldrule, entries);
1893 if (oldrule == NULL) {
1894 if (newrule != NULL)
1895 pf_rm_rule(NULL, newrule);
1901 if (pcr->action == PF_CHANGE_REMOVE) {
1902 pf_rm_rule(ruleset->rules[rs_num].active.ptr, oldrule);
1903 ruleset->rules[rs_num].active.rcount--;
1905 if (oldrule == NULL)
1907 ruleset->rules[rs_num].active.ptr,
1909 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1910 pcr->action == PF_CHANGE_ADD_BEFORE)
1911 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1914 ruleset->rules[rs_num].active.ptr,
1915 oldrule, newrule, entries);
1916 ruleset->rules[rs_num].active.rcount++;
1920 TAILQ_FOREACH(oldrule,
1921 ruleset->rules[rs_num].active.ptr, entries)
1924 ruleset->rules[rs_num].active.ticket++;
1926 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1927 pf_remove_if_empty_ruleset(ruleset);
1932 case DIOCCLRSTATES: {
1933 struct pf_state *state, *nexts;
1934 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1937 for (state = RB_MIN(pf_state_tree_id, &tree_id); state;
1939 nexts = RB_NEXT(pf_state_tree_id, &tree_id, state);
1941 if (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1942 state->u.s.kif->pfik_name)) {
1944 /* don't send out individual delete messages */
1945 state->sync_flags = PFSTATE_NOSYNC;
1947 pf_unlink_state(state);
1951 psk->psk_af = killed;
1953 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
1958 case DIOCKILLSTATES: {
1959 struct pf_state *state, *nexts;
1960 struct pf_state_host *src, *dst;
1961 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1964 for (state = RB_MIN(pf_state_tree_id, &tree_id); state;
1966 nexts = RB_NEXT(pf_state_tree_id, &tree_id, state);
1968 if (state->direction == PF_OUT) {
1975 if ((!psk->psk_af || state->af == psk->psk_af)
1976 && (!psk->psk_proto || psk->psk_proto ==
1978 PF_MATCHA(psk->psk_src.neg,
1979 &psk->psk_src.addr.v.a.addr,
1980 &psk->psk_src.addr.v.a.mask,
1981 &src->addr, state->af) &&
1982 PF_MATCHA(psk->psk_dst.neg,
1983 &psk->psk_dst.addr.v.a.addr,
1984 &psk->psk_dst.addr.v.a.mask,
1985 &dst->addr, state->af) &&
1986 (psk->psk_src.port_op == 0 ||
1987 pf_match_port(psk->psk_src.port_op,
1988 psk->psk_src.port[0], psk->psk_src.port[1],
1990 (psk->psk_dst.port_op == 0 ||
1991 pf_match_port(psk->psk_dst.port_op,
1992 psk->psk_dst.port[0], psk->psk_dst.port[1],
1994 (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1995 state->u.s.kif->pfik_name))) {
1997 /* send immediate delete of state */
1998 pfsync_delete_state(state);
1999 state->sync_flags |= PFSTATE_NOSYNC;
2001 pf_unlink_state(state);
2005 psk->psk_af = killed;
2009 case DIOCADDSTATE: {
2010 struct pfioc_state *ps = (struct pfioc_state *)addr;
2011 struct pf_state *state;
2012 struct pfi_kif *kif;
2014 if (ps->state.timeout >= PFTM_MAX &&
2015 ps->state.timeout != PFTM_UNTIL_PACKET) {
2019 state = pool_get(&pf_state_pl, PR_NOWAIT);
2020 if (state == NULL) {
2024 kif = pfi_kif_get(ps->state.u.ifname);
2026 pool_put(&pf_state_pl, state);
2030 bcopy(&ps->state, state, sizeof(struct pf_state));
2031 bzero(&state->u, sizeof(state->u));
2032 state->rule.ptr = &pf_default_rule;
2033 state->nat_rule.ptr = NULL;
2034 state->anchor.ptr = NULL;
2035 state->rt_kif = NULL;
2036 state->creation = time_second;
2037 state->pfsync_time = 0;
2038 state->packets[0] = state->packets[1] = 0;
2039 state->bytes[0] = state->bytes[1] = 0;
2041 if (pf_insert_state(kif, state)) {
2042 pfi_kif_unref(kif, PFI_KIF_REF_NONE);
2043 pool_put(&pf_state_pl, state);
2049 case DIOCGETSTATE: {
2050 struct pfioc_state *ps = (struct pfioc_state *)addr;
2051 struct pf_state *state;
2056 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2061 if (state == NULL) {
2066 bcopy(state, &ps->state, sizeof(ps->state));
2067 strlcpy(ps->state.u.ifname, state->u.s.kif->pfik_name,
2068 sizeof(ps->state.u.ifname));
2069 ps->state.rule.nr = state->rule.ptr->nr;
2070 ps->state.nat_rule.nr = (state->nat_rule.ptr == NULL) ?
2071 -1 : state->nat_rule.ptr->nr;
2072 ps->state.anchor.nr = (state->anchor.ptr == NULL) ?
2073 -1 : state->anchor.ptr->nr;
2074 ps->state.creation = secs - ps->state.creation;
2075 ps->state.expire = pf_state_expires(state);
2076 if (ps->state.expire > secs)
2077 ps->state.expire -= secs;
2079 ps->state.expire = 0;
2083 case DIOCGETSTATES: {
2084 struct pfioc_states *ps = (struct pfioc_states *)addr;
2085 struct pf_state *state;
2086 struct pf_state *p, *pstore;
2088 int space = ps->ps_len;
2091 nr = pf_status.states;
2092 ps->ps_len = sizeof(struct pf_state) * nr;
2099 pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
2106 state = TAILQ_FIRST(&state_list);
2108 if (state->timeout != PFTM_UNLINKED) {
2109 int secs = time_second;
2111 if ((nr+1) * sizeof(*p) > (unsigned)ps->ps_len)
2114 bcopy(state, pstore, sizeof(*pstore));
2115 strlcpy(pstore->u.ifname,
2116 state->u.s.kif->pfik_name,
2117 sizeof(pstore->u.ifname));
2118 pstore->rule.nr = state->rule.ptr->nr;
2119 pstore->nat_rule.nr = (state->nat_rule.ptr ==
2120 NULL) ? -1 : state->nat_rule.ptr->nr;
2121 pstore->anchor.nr = (state->anchor.ptr ==
2122 NULL) ? -1 : state->anchor.ptr->nr;
2123 pstore->creation = secs - pstore->creation;
2124 pstore->expire = pf_state_expires(state);
2125 if (pstore->expire > secs)
2126 pstore->expire -= secs;
2130 PF_COPYOUT(pstore, p, sizeof(*p), error);
2132 error = copyout(pstore, p, sizeof(*p));
2135 free(pstore, M_TEMP);
2141 state = TAILQ_NEXT(state, u.s.entry_list);
2144 ps->ps_len = sizeof(struct pf_state) * nr;
2146 free(pstore, M_TEMP);
2150 case DIOCGETSTATUS: {
2151 struct pf_status *s = (struct pf_status *)addr;
2152 bcopy(&pf_status, s, sizeof(struct pf_status));
2153 pfi_fill_oldstatus(s);
2157 case DIOCSETSTATUSIF: {
2158 struct pfioc_if *pi = (struct pfioc_if *)addr;
2160 if (pi->ifname[0] == 0) {
2161 bzero(pf_status.ifname, IFNAMSIZ);
2164 if (ifunit(pi->ifname) == NULL) {
2168 strlcpy(pf_status.ifname, pi->ifname, IFNAMSIZ);
2172 case DIOCCLRSTATUS: {
2173 bzero(pf_status.counters, sizeof(pf_status.counters));
2174 bzero(pf_status.fcounters, sizeof(pf_status.fcounters));
2175 bzero(pf_status.scounters, sizeof(pf_status.scounters));
2176 pf_status.since = time_second;
2177 if (*pf_status.ifname)
2178 pfi_clr_istats(pf_status.ifname);
2183 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
2184 struct pf_state *state;
2185 struct pf_state_cmp key;
2186 int m = 0, direction = pnl->direction;
2189 key.proto = pnl->proto;
2192 PF_AZERO(&pnl->saddr, pnl->af) ||
2193 PF_AZERO(&pnl->daddr, pnl->af) ||
2194 ((pnl->proto == IPPROTO_TCP ||
2195 pnl->proto == IPPROTO_UDP) &&
2196 (!pnl->dport || !pnl->sport)))
2200 * userland gives us source and dest of connection,
2201 * reverse the lookup so we ask for what happens with
2202 * the return traffic, enabling us to find it in the
2205 if (direction == PF_IN) {
2206 PF_ACPY(&key.ext.addr, &pnl->daddr, pnl->af);
2207 key.ext.port = pnl->dport;
2208 PF_ACPY(&key.gwy.addr, &pnl->saddr, pnl->af);
2209 key.gwy.port = pnl->sport;
2210 state = pf_find_state_all(&key, PF_EXT_GWY, &m);
2212 PF_ACPY(&key.lan.addr, &pnl->daddr, pnl->af);
2213 key.lan.port = pnl->dport;
2214 PF_ACPY(&key.ext.addr, &pnl->saddr, pnl->af);
2215 key.ext.port = pnl->sport;
2216 state = pf_find_state_all(&key, PF_LAN_EXT, &m);
2219 error = E2BIG; /* more than one state */
2220 else if (state != NULL) {
2221 if (direction == PF_IN) {
2222 PF_ACPY(&pnl->rsaddr, &state->lan.addr,
2224 pnl->rsport = state->lan.port;
2225 PF_ACPY(&pnl->rdaddr, &pnl->daddr,
2227 pnl->rdport = pnl->dport;
2229 PF_ACPY(&pnl->rdaddr, &state->gwy.addr,
2231 pnl->rdport = state->gwy.port;
2232 PF_ACPY(&pnl->rsaddr, &pnl->saddr,
2234 pnl->rsport = pnl->sport;
2242 case DIOCSETTIMEOUT: {
2243 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
2246 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
2251 old = pf_default_rule.timeout[pt->timeout];
2252 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
2254 pf_default_rule.timeout[pt->timeout] = pt->seconds;
2255 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
2256 wakeup(pf_purge_thread);
2261 case DIOCGETTIMEOUT: {
2262 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
2264 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
2268 pt->seconds = pf_default_rule.timeout[pt->timeout];
2272 case DIOCGETLIMIT: {
2273 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
2275 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
2279 pl->limit = pf_pool_limits[pl->index].limit;
2283 case DIOCSETLIMIT: {
2284 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
2287 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
2288 pf_pool_limits[pl->index].pp == NULL) {
2293 uma_zone_set_max(pf_pool_limits[pl->index].pp, pl->limit);
2295 if (pool_sethardlimit(pf_pool_limits[pl->index].pp,
2296 pl->limit, NULL, 0) != 0) {
2301 old_limit = pf_pool_limits[pl->index].limit;
2302 pf_pool_limits[pl->index].limit = pl->limit;
2303 pl->limit = old_limit;
2307 case DIOCSETDEBUG: {
2308 u_int32_t *level = (u_int32_t *)addr;
2310 pf_status.debug = *level;
2314 case DIOCCLRRULECTRS: {
2315 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
2316 struct pf_ruleset *ruleset = &pf_main_ruleset;
2317 struct pf_rule *rule;
2320 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
2321 rule->evaluations = 0;
2322 rule->packets[0] = rule->packets[1] = 0;
2323 rule->bytes[0] = rule->bytes[1] = 0;
2329 case DIOCGIFSPEED: {
2330 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
2331 struct pf_ifspeed ps;
2334 if (psp->ifname[0] != 0) {
2335 /* Can we completely trust user-land? */
2336 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
2337 ifp = ifunit(ps.ifname);
2339 psp->baudrate = ifp->if_baudrate;
2346 #endif /* __FreeBSD__ */
2349 case DIOCSTARTALTQ: {
2350 struct pf_altq *altq;
2352 /* enable all altq interfaces on active list */
2353 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2355 if (altq->qname[0] == 0 && (altq->local_flags &
2356 PFALTQ_FLAG_IF_REMOVED) == 0) {
2358 if (altq->qname[0] == 0) {
2360 error = pf_enable_altq(altq);
2366 pf_altq_running = 1;
2367 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
2371 case DIOCSTOPALTQ: {
2372 struct pf_altq *altq;
2374 /* disable all altq interfaces on active list */
2375 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2377 if (altq->qname[0] == 0 && (altq->local_flags &
2378 PFALTQ_FLAG_IF_REMOVED) == 0) {
2380 if (altq->qname[0] == 0) {
2382 error = pf_disable_altq(altq);
2388 pf_altq_running = 0;
2389 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
2394 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2395 struct pf_altq *altq, *a;
2397 if (pa->ticket != ticket_altqs_inactive) {
2401 altq = pool_get(&pf_altq_pl, PR_NOWAIT);
2406 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
2408 altq->local_flags = 0;
2412 * if this is for a queue, find the discipline and
2413 * copy the necessary fields
2415 if (altq->qname[0] != 0) {
2416 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2418 pool_put(&pf_altq_pl, altq);
2421 altq->altq_disc = NULL;
2422 TAILQ_FOREACH(a, pf_altqs_inactive, entries) {
2423 if (strncmp(a->ifname, altq->ifname,
2424 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2425 altq->altq_disc = a->altq_disc;
2434 if ((ifp = ifunit(altq->ifname)) == NULL) {
2435 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2439 error = altq_add(altq);
2445 pool_put(&pf_altq_pl, altq);
2449 TAILQ_INSERT_TAIL(pf_altqs_inactive, altq, entries);
2450 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2454 case DIOCGETALTQS: {
2455 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2456 struct pf_altq *altq;
2459 TAILQ_FOREACH(altq, pf_altqs_active, entries)
2461 pa->ticket = ticket_altqs_active;
2466 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2467 struct pf_altq *altq;
2470 if (pa->ticket != ticket_altqs_active) {
2475 altq = TAILQ_FIRST(pf_altqs_active);
2476 while ((altq != NULL) && (nr < pa->nr)) {
2477 altq = TAILQ_NEXT(altq, entries);
2484 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2488 case DIOCCHANGEALTQ:
2489 /* CHANGEALTQ not supported yet! */
2493 case DIOCGETQSTATS: {
2494 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2495 struct pf_altq *altq;
2499 if (pq->ticket != ticket_altqs_active) {
2503 nbytes = pq->nbytes;
2505 altq = TAILQ_FIRST(pf_altqs_active);
2506 while ((altq != NULL) && (nr < pq->nr)) {
2507 altq = TAILQ_NEXT(altq, entries);
2515 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2521 error = altq_getqstats(altq, pq->buf, &nbytes);
2526 pq->scheduler = altq->scheduler;
2527 pq->nbytes = nbytes;
2533 case DIOCBEGINADDRS: {
2534 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2536 pf_empty_pool(&pf_pabuf);
2537 pp->ticket = ++ticket_pabuf;
2542 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2544 if (pp->ticket != ticket_pabuf) {
2549 if (pp->af == AF_INET) {
2550 error = EAFNOSUPPORT;
2555 if (pp->af == AF_INET6) {
2556 error = EAFNOSUPPORT;
2560 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2561 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2562 pp->addr.addr.type != PF_ADDR_TABLE) {
2566 pa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2571 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2572 if (pa->ifname[0]) {
2573 pa->kif = pfi_kif_get(pa->ifname);
2574 if (pa->kif == NULL) {
2575 pool_put(&pf_pooladdr_pl, pa);
2579 pfi_kif_ref(pa->kif, PFI_KIF_REF_RULE);
2581 if (pfi_dynaddr_setup(&pa->addr, pp->af)) {
2582 pfi_dynaddr_remove(&pa->addr);
2583 pfi_kif_unref(pa->kif, PFI_KIF_REF_RULE);
2584 pool_put(&pf_pooladdr_pl, pa);
2588 TAILQ_INSERT_TAIL(&pf_pabuf, pa, entries);
2592 case DIOCGETADDRS: {
2593 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2596 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2597 pp->r_num, 0, 1, 0);
2602 TAILQ_FOREACH(pa, &pool->list, entries)
2608 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2611 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2612 pp->r_num, 0, 1, 1);
2617 pa = TAILQ_FIRST(&pool->list);
2618 while ((pa != NULL) && (nr < pp->nr)) {
2619 pa = TAILQ_NEXT(pa, entries);
2626 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2627 pfi_dynaddr_copyout(&pp->addr.addr);
2628 pf_tbladdr_copyout(&pp->addr.addr);
2629 pf_rtlabel_copyout(&pp->addr.addr);
2633 case DIOCCHANGEADDR: {
2634 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2635 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2636 struct pf_ruleset *ruleset;
2638 if (pca->action < PF_CHANGE_ADD_HEAD ||
2639 pca->action > PF_CHANGE_REMOVE) {
2643 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2644 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2645 pca->addr.addr.type != PF_ADDR_TABLE) {
2650 ruleset = pf_find_ruleset(pca->anchor);
2651 if (ruleset == NULL) {
2655 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2656 pca->r_num, pca->r_last, 1, 1);
2661 if (pca->action != PF_CHANGE_REMOVE) {
2662 newpa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2663 if (newpa == NULL) {
2667 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2669 if (pca->af == AF_INET) {
2670 pool_put(&pf_pooladdr_pl, newpa);
2671 error = EAFNOSUPPORT;
2676 if (pca->af == AF_INET6) {
2677 pool_put(&pf_pooladdr_pl, newpa);
2678 error = EAFNOSUPPORT;
2682 if (newpa->ifname[0]) {
2683 newpa->kif = pfi_kif_get(newpa->ifname);
2684 if (newpa->kif == NULL) {
2685 pool_put(&pf_pooladdr_pl, newpa);
2689 pfi_kif_ref(newpa->kif, PFI_KIF_REF_RULE);
2692 if (pfi_dynaddr_setup(&newpa->addr, pca->af) ||
2693 pf_tbladdr_setup(ruleset, &newpa->addr)) {
2694 pfi_dynaddr_remove(&newpa->addr);
2695 pfi_kif_unref(newpa->kif, PFI_KIF_REF_RULE);
2696 pool_put(&pf_pooladdr_pl, newpa);
2702 if (pca->action == PF_CHANGE_ADD_HEAD)
2703 oldpa = TAILQ_FIRST(&pool->list);
2704 else if (pca->action == PF_CHANGE_ADD_TAIL)
2705 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2709 oldpa = TAILQ_FIRST(&pool->list);
2710 while ((oldpa != NULL) && (i < pca->nr)) {
2711 oldpa = TAILQ_NEXT(oldpa, entries);
2714 if (oldpa == NULL) {
2720 if (pca->action == PF_CHANGE_REMOVE) {
2721 TAILQ_REMOVE(&pool->list, oldpa, entries);
2722 pfi_dynaddr_remove(&oldpa->addr);
2723 pf_tbladdr_remove(&oldpa->addr);
2724 pfi_kif_unref(oldpa->kif, PFI_KIF_REF_RULE);
2725 pool_put(&pf_pooladdr_pl, oldpa);
2728 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2729 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2730 pca->action == PF_CHANGE_ADD_BEFORE)
2731 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2733 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2737 pool->cur = TAILQ_FIRST(&pool->list);
2738 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
2743 case DIOCGETRULESETS: {
2744 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2745 struct pf_ruleset *ruleset;
2746 struct pf_anchor *anchor;
2748 pr->path[sizeof(pr->path) - 1] = 0;
2749 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2754 if (ruleset->anchor == NULL) {
2755 /* XXX kludge for pf_main_ruleset */
2756 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2757 if (anchor->parent == NULL)
2760 RB_FOREACH(anchor, pf_anchor_node,
2761 &ruleset->anchor->children)
2767 case DIOCGETRULESET: {
2768 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2769 struct pf_ruleset *ruleset;
2770 struct pf_anchor *anchor;
2773 pr->path[sizeof(pr->path) - 1] = 0;
2774 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2779 if (ruleset->anchor == NULL) {
2780 /* XXX kludge for pf_main_ruleset */
2781 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2782 if (anchor->parent == NULL && nr++ == pr->nr) {
2783 strlcpy(pr->name, anchor->name,
2788 RB_FOREACH(anchor, pf_anchor_node,
2789 &ruleset->anchor->children)
2790 if (nr++ == pr->nr) {
2791 strlcpy(pr->name, anchor->name,
2801 case DIOCRCLRTABLES: {
2802 struct pfioc_table *io = (struct pfioc_table *)addr;
2804 if (io->pfrio_esize != 0) {
2808 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2809 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2813 case DIOCRADDTABLES: {
2814 struct pfioc_table *io = (struct pfioc_table *)addr;
2816 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2820 error = pfr_add_tables(io->pfrio_buffer, io->pfrio_size,
2821 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2825 case DIOCRDELTABLES: {
2826 struct pfioc_table *io = (struct pfioc_table *)addr;
2828 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2832 error = pfr_del_tables(io->pfrio_buffer, io->pfrio_size,
2833 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2837 case DIOCRGETTABLES: {
2838 struct pfioc_table *io = (struct pfioc_table *)addr;
2840 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2844 error = pfr_get_tables(&io->pfrio_table, io->pfrio_buffer,
2845 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2849 case DIOCRGETTSTATS: {
2850 struct pfioc_table *io = (struct pfioc_table *)addr;
2852 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2856 error = pfr_get_tstats(&io->pfrio_table, io->pfrio_buffer,
2857 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2861 case DIOCRCLRTSTATS: {
2862 struct pfioc_table *io = (struct pfioc_table *)addr;
2864 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2868 error = pfr_clr_tstats(io->pfrio_buffer, io->pfrio_size,
2869 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2873 case DIOCRSETTFLAGS: {
2874 struct pfioc_table *io = (struct pfioc_table *)addr;
2876 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2880 error = pfr_set_tflags(io->pfrio_buffer, io->pfrio_size,
2881 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2882 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2886 case DIOCRCLRADDRS: {
2887 struct pfioc_table *io = (struct pfioc_table *)addr;
2889 if (io->pfrio_esize != 0) {
2893 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2894 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2898 case DIOCRADDADDRS: {
2899 struct pfioc_table *io = (struct pfioc_table *)addr;
2901 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2905 error = pfr_add_addrs(&io->pfrio_table, io->pfrio_buffer,
2906 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2907 PFR_FLAG_USERIOCTL);
2911 case DIOCRDELADDRS: {
2912 struct pfioc_table *io = (struct pfioc_table *)addr;
2914 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2918 error = pfr_del_addrs(&io->pfrio_table, io->pfrio_buffer,
2919 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2920 PFR_FLAG_USERIOCTL);
2924 case DIOCRSETADDRS: {
2925 struct pfioc_table *io = (struct pfioc_table *)addr;
2927 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2931 error = pfr_set_addrs(&io->pfrio_table, io->pfrio_buffer,
2932 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2933 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2934 PFR_FLAG_USERIOCTL, 0);
2938 case DIOCRGETADDRS: {
2939 struct pfioc_table *io = (struct pfioc_table *)addr;
2941 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2945 error = pfr_get_addrs(&io->pfrio_table, io->pfrio_buffer,
2946 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2950 case DIOCRGETASTATS: {
2951 struct pfioc_table *io = (struct pfioc_table *)addr;
2953 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2957 error = pfr_get_astats(&io->pfrio_table, io->pfrio_buffer,
2958 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2962 case DIOCRCLRASTATS: {
2963 struct pfioc_table *io = (struct pfioc_table *)addr;
2965 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2969 error = pfr_clr_astats(&io->pfrio_table, io->pfrio_buffer,
2970 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2971 PFR_FLAG_USERIOCTL);
2975 case DIOCRTSTADDRS: {
2976 struct pfioc_table *io = (struct pfioc_table *)addr;
2978 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2982 error = pfr_tst_addrs(&io->pfrio_table, io->pfrio_buffer,
2983 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2984 PFR_FLAG_USERIOCTL);
2988 case DIOCRINADEFINE: {
2989 struct pfioc_table *io = (struct pfioc_table *)addr;
2991 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2995 error = pfr_ina_define(&io->pfrio_table, io->pfrio_buffer,
2996 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
2997 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
3002 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
3003 error = pf_osfp_add(io);
3008 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
3009 error = pf_osfp_get(io);
3014 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3015 struct pfioc_trans_e *ioe;
3016 struct pfr_table *table;
3019 if (io->esize != sizeof(*ioe)) {
3026 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3028 table = (struct pfr_table *)malloc(sizeof(*table),
3033 for (i = 0; i < io->size; i++) {
3035 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3038 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3040 free(table, M_TEMP);
3045 switch (ioe->rs_num) {
3047 case PF_RULESET_ALTQ:
3048 if (ioe->anchor[0]) {
3049 free(table, M_TEMP);
3054 if ((error = pf_begin_altq(&ioe->ticket))) {
3055 free(table, M_TEMP);
3061 case PF_RULESET_TABLE:
3062 bzero(table, sizeof(*table));
3063 strlcpy(table->pfrt_anchor, ioe->anchor,
3064 sizeof(table->pfrt_anchor));
3065 if ((error = pfr_ina_begin(table,
3066 &ioe->ticket, NULL, 0))) {
3067 free(table, M_TEMP);
3073 if ((error = pf_begin_rules(&ioe->ticket,
3074 ioe->rs_num, ioe->anchor))) {
3075 free(table, M_TEMP);
3082 PF_COPYOUT(ioe, io->array+i, sizeof(io->array[i]),
3086 if (copyout(ioe, io->array+i, sizeof(io->array[i]))) {
3088 free(table, M_TEMP);
3094 free(table, M_TEMP);
3099 case DIOCXROLLBACK: {
3100 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3101 struct pfioc_trans_e *ioe;
3102 struct pfr_table *table;
3105 if (io->esize != sizeof(*ioe)) {
3112 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3114 table = (struct pfr_table *)malloc(sizeof(*table),
3119 for (i = 0; i < io->size; i++) {
3121 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3124 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3126 free(table, M_TEMP);
3131 switch (ioe->rs_num) {
3133 case PF_RULESET_ALTQ:
3134 if (ioe->anchor[0]) {
3135 free(table, M_TEMP);
3140 if ((error = pf_rollback_altq(ioe->ticket))) {
3141 free(table, M_TEMP);
3143 goto fail; /* really bad */
3147 case PF_RULESET_TABLE:
3148 bzero(table, sizeof(*table));
3149 strlcpy(table->pfrt_anchor, ioe->anchor,
3150 sizeof(table->pfrt_anchor));
3151 if ((error = pfr_ina_rollback(table,
3152 ioe->ticket, NULL, 0))) {
3153 free(table, M_TEMP);
3155 goto fail; /* really bad */
3159 if ((error = pf_rollback_rules(ioe->ticket,
3160 ioe->rs_num, ioe->anchor))) {
3161 free(table, M_TEMP);
3163 goto fail; /* really bad */
3168 free(table, M_TEMP);
3174 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3175 struct pfioc_trans_e *ioe;
3176 struct pfr_table *table;
3177 struct pf_ruleset *rs;
3180 if (io->esize != sizeof(*ioe)) {
3187 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3189 table = (struct pfr_table *)malloc(sizeof(*table),
3194 /* first makes sure everything will succeed */
3195 for (i = 0; i < io->size; i++) {
3197 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3200 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3202 free(table, M_TEMP);
3207 switch (ioe->rs_num) {
3209 case PF_RULESET_ALTQ:
3210 if (ioe->anchor[0]) {
3211 free(table, M_TEMP);
3216 if (!altqs_inactive_open || ioe->ticket !=
3217 ticket_altqs_inactive) {
3218 free(table, M_TEMP);
3225 case PF_RULESET_TABLE:
3226 rs = pf_find_ruleset(ioe->anchor);
3227 if (rs == NULL || !rs->topen || ioe->ticket !=
3229 free(table, M_TEMP);
3236 if (ioe->rs_num < 0 || ioe->rs_num >=
3238 free(table, M_TEMP);
3243 rs = pf_find_ruleset(ioe->anchor);
3245 !rs->rules[ioe->rs_num].inactive.open ||
3246 rs->rules[ioe->rs_num].inactive.ticket !=
3248 free(table, M_TEMP);
3256 /* now do the commit - no errors should happen here */
3257 for (i = 0; i < io->size; i++) {
3259 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3262 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3264 free(table, M_TEMP);
3269 switch (ioe->rs_num) {
3271 case PF_RULESET_ALTQ:
3272 if ((error = pf_commit_altq(ioe->ticket))) {
3273 free(table, M_TEMP);
3275 goto fail; /* really bad */
3279 case PF_RULESET_TABLE:
3280 bzero(table, sizeof(*table));
3281 strlcpy(table->pfrt_anchor, ioe->anchor,
3282 sizeof(table->pfrt_anchor));
3283 if ((error = pfr_ina_commit(table, ioe->ticket,
3285 free(table, M_TEMP);
3287 goto fail; /* really bad */
3291 if ((error = pf_commit_rules(ioe->ticket,
3292 ioe->rs_num, ioe->anchor))) {
3293 free(table, M_TEMP);
3295 goto fail; /* really bad */
3300 free(table, M_TEMP);
3305 case DIOCGETSRCNODES: {
3306 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3307 struct pf_src_node *n, *p, *pstore;
3309 int space = psn->psn_len;
3312 RB_FOREACH(n, pf_src_tree, &tree_src_tracking)
3314 psn->psn_len = sizeof(struct pf_src_node) * nr;
3321 pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
3326 p = psn->psn_src_nodes;
3327 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3328 int secs = time_second, diff;
3330 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3333 bcopy(n, pstore, sizeof(*pstore));
3334 if (n->rule.ptr != NULL)
3335 pstore->rule.nr = n->rule.ptr->nr;
3336 pstore->creation = secs - pstore->creation;
3337 if (pstore->expire > secs)
3338 pstore->expire -= secs;
3342 /* adjust the connection rate estimate */
3343 diff = secs - n->conn_rate.last;
3344 if (diff >= n->conn_rate.seconds)
3345 pstore->conn_rate.count = 0;
3347 pstore->conn_rate.count -=
3348 n->conn_rate.count * diff /
3349 n->conn_rate.seconds;
3352 PF_COPYOUT(pstore, p, sizeof(*p), error);
3354 error = copyout(pstore, p, sizeof(*p));
3357 free(pstore, M_TEMP);
3363 psn->psn_len = sizeof(struct pf_src_node) * nr;
3365 free(pstore, M_TEMP);
3369 case DIOCCLRSRCNODES: {
3370 struct pf_src_node *n;
3371 struct pf_state *state;
3373 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3374 state->src_node = NULL;
3375 state->nat_src_node = NULL;
3377 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3381 pf_purge_expired_src_nodes(1);
3382 pf_status.src_nodes = 0;
3386 case DIOCKILLSRCNODES: {
3387 struct pf_src_node *sn;
3389 struct pfioc_src_node_kill *psnk = \
3390 (struct pfioc_src_node_kill *) addr;
3393 RB_FOREACH(sn, pf_src_tree, &tree_src_tracking) {
3394 if (PF_MATCHA(psnk->psnk_src.neg, \
3395 &psnk->psnk_src.addr.v.a.addr, \
3396 &psnk->psnk_src.addr.v.a.mask, \
3397 &sn->addr, sn->af) &&
3398 PF_MATCHA(psnk->psnk_dst.neg, \
3399 &psnk->psnk_dst.addr.v.a.addr, \
3400 &psnk->psnk_dst.addr.v.a.mask, \
3401 &sn->raddr, sn->af)) {
3402 /* Handle state to src_node linkage */
3403 if (sn->states != 0) {
3404 RB_FOREACH(s, pf_state_tree_id,
3406 if (s->src_node == sn)
3408 if (s->nat_src_node == sn)
3409 s->nat_src_node = NULL;
3419 pf_purge_expired_src_nodes(1);
3421 psnk->psnk_af = killed;
3425 case DIOCSETHOSTID: {
3426 u_int32_t *hostid = (u_int32_t *)addr;
3429 pf_status.hostid = arc4random();
3431 pf_status.hostid = *hostid;
3439 case DIOCIGETIFACES: {
3440 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3442 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3446 error = pfi_get_ifaces(io->pfiio_name, io->pfiio_buffer,
3451 case DIOCSETIFFLAG: {
3452 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3454 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3458 case DIOCCLRIFFLAG: {
3459 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3461 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3474 sx_xunlock(&pf_consistency_lock);
3476 sx_sunlock(&pf_consistency_lock);
3479 /* XXX: Lock order? */
3481 rw_exit_write(&pf_consistency_lock);
3483 rw_exit_read(&pf_consistency_lock);
3490 * XXX - Check for version missmatch!!!
3493 pf_clear_states(void)
3495 struct pf_state *state;
3497 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3498 state->timeout = PFTM_PURGE;
3500 /* don't send out individual delete messages */
3501 state->sync_flags = PFSTATE_NOSYNC;
3503 pf_unlink_state(state);
3508 * XXX This is called on module unload, we do not want to sync that over? */
3510 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
3515 pf_clear_tables(void)
3517 struct pfioc_table io;
3520 bzero(&io, sizeof(io));
3522 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3529 pf_clear_srcnodes(void)
3531 struct pf_src_node *n;
3532 struct pf_state *state;
3534 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3535 state->src_node = NULL;
3536 state->nat_src_node = NULL;
3538 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3544 * XXX - Check for version missmatch!!!
3548 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3557 pf_status.running = 0;
3559 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3561 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3564 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3566 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3567 break; /* XXX: rollback? */
3569 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3571 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3572 break; /* XXX: rollback? */
3574 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3576 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3577 break; /* XXX: rollback? */
3579 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3581 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3582 break; /* XXX: rollback? */
3585 /* XXX: these should always succeed here */
3586 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3587 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3588 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3589 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3590 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3592 if ((error = pf_clear_tables()) != 0)
3596 if ((error = pf_begin_altq(&t[0])) != 0) {
3597 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3600 pf_commit_altq(t[0]);
3605 pf_clear_srcnodes();
3607 /* status does not use malloced mem so no need to cleanup */
3608 /* fingerprints and interfaces have thier own cleanup code */
3615 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3619 * XXX Wed Jul 9 22:03:16 2003 UTC
3620 * OpenBSD has changed its byte ordering convention on ip_len/ip_off
3621 * in network stack. OpenBSD's network stack have converted
3622 * ip_len/ip_off to host byte order frist as FreeBSD.
3623 * Now this is not true anymore , so we should convert back to network
3626 struct ip *h = NULL;
3629 if ((*m)->m_pkthdr.len >= (int)sizeof(struct ip)) {
3630 /* if m_pkthdr.len is less than ip header, pf will handle. */
3631 h = mtod(*m, struct ip *);
3635 chk = pf_test(PF_IN, ifp, m, NULL, inp);
3641 /* pf_test can change ip header location */
3642 h = mtod(*m, struct ip *);
3650 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3654 * XXX Wed Jul 9 22:03:16 2003 UTC
3655 * OpenBSD has changed its byte ordering convention on ip_len/ip_off
3656 * in network stack. OpenBSD's network stack have converted
3657 * ip_len/ip_off to host byte order frist as FreeBSD.
3658 * Now this is not true anymore , so we should convert back to network
3661 struct ip *h = NULL;
3664 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3665 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3666 in_delayed_cksum(*m);
3667 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3669 if ((*m)->m_pkthdr.len >= (int)sizeof(*h)) {
3670 /* if m_pkthdr.len is less than ip header, pf will handle. */
3671 h = mtod(*m, struct ip *);
3675 chk = pf_test(PF_OUT, ifp, m, NULL, inp);
3681 /* pf_test can change ip header location */
3682 h = mtod(*m, struct ip *);
3691 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3694 INIT_VNET_NET(curvnet);
3697 * IPv6 is not affected by ip_len/ip_off byte order changes.
3702 * In case of loopback traffic IPv6 uses the real interface in
3703 * order to support scoped addresses. In order to support stateful
3704 * filtering we have change this to lo0 as it is the case in IPv4.
3706 chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? &V_loif[0] : ifp, m,
3716 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3720 * IPv6 does not affected ip_len/ip_off byte order changes.
3724 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3725 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3726 in_delayed_cksum(*m);
3727 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3729 chk = pf_test6(PF_OUT, ifp, m, NULL, inp);
3741 struct pfil_head *pfh_inet;
3743 struct pfil_head *pfh_inet6;
3746 PF_ASSERT(MA_NOTOWNED);
3751 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3752 if (pfh_inet == NULL)
3753 return (ESRCH); /* XXX */
3754 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3755 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3757 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3758 if (pfh_inet6 == NULL) {
3759 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3761 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3763 return (ESRCH); /* XXX */
3765 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3766 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3776 struct pfil_head *pfh_inet;
3778 struct pfil_head *pfh_inet6;
3781 PF_ASSERT(MA_NOTOWNED);
3783 if (pf_pfil_hooked == 0)
3786 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3787 if (pfh_inet == NULL)
3788 return (ESRCH); /* XXX */
3789 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3791 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3794 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3795 if (pfh_inet6 == NULL)
3796 return (ESRCH); /* XXX */
3797 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3799 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3812 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3813 if (pfattach() < 0) {
3814 destroy_dev(pf_dev);
3827 pf_status.running = 0;
3829 error = dehook_pf();
3832 * Should not happen!
3833 * XXX Due to error code ESRCH, kldunload will show
3834 * a message like 'No such process'.
3836 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3842 while (pf_end_threads < 2) {
3843 wakeup_one(pf_purge_thread);
3844 msleep(pf_purge_thread, &pf_task_mtx, 0, "pftmo", hz);
3851 destroy_dev(pf_dev);
3857 pf_modevent(module_t mod, int type, void *data)
3867 error = pf_unload();
3876 static moduledata_t pf_mod = {
3882 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST);
3883 MODULE_VERSION(pf, PF_MODVER);
3884 #endif /* __FreeBSD__ */