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>
90 #include <sys/timeout.h>
94 #include <sys/malloc.h>
95 #include <sys/kthread.h>
97 #include <sys/rwlock.h>
98 #include <uvm/uvm_extern.h>
102 #include <net/if_types.h>
103 #include <net/route.h>
105 #include <netinet/in.h>
106 #include <netinet/in_var.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/ip.h>
109 #include <netinet/ip_var.h>
110 #include <netinet/ip_icmp.h>
115 #include <dev/rndvar.h>
116 #include <crypto/md5.h>
118 #include <net/pfvar.h>
121 #include <net/if_pfsync.h>
122 #endif /* NPFSYNC > 0 */
124 #include <net/if_pflog.h>
127 #include <netinet/ip6.h>
128 #include <netinet/in_pcb.h>
132 #include <altq/altq.h>
136 #include <sys/limits.h>
137 #include <sys/lock.h>
138 #include <sys/mutex.h>
139 #include <net/pfil.h>
140 #endif /* __FreeBSD__ */
143 void init_zone_var(void);
144 void cleanup_pf_zone(void);
148 void pf_thread_create(void *);
149 int pfopen(dev_t, int, int, struct proc *);
150 int pfclose(dev_t, int, int, struct proc *);
152 struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
153 u_int8_t, u_int8_t, u_int8_t);
155 void pf_mv_pool(struct pf_palist *, struct pf_palist *);
156 void pf_empty_pool(struct pf_palist *);
158 int pfioctl(struct cdev *, u_long, caddr_t, int, struct thread *);
160 int pfioctl(struct cdev *, u_long, caddr_t, int, struct proc *);
163 int pf_begin_altq(u_int32_t *);
164 int pf_rollback_altq(u_int32_t);
165 int pf_commit_altq(u_int32_t);
166 int pf_enable_altq(struct pf_altq *);
167 int pf_disable_altq(struct pf_altq *);
169 int pf_begin_rules(u_int32_t *, int, const char *);
170 int pf_rollback_rules(u_int32_t, int, char *);
171 int pf_setup_pfsync_matching(struct pf_ruleset *);
172 void pf_hash_rule(MD5_CTX *, struct pf_rule *);
173 void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
174 int pf_commit_rules(u_int32_t, int, char *);
176 struct pf_rule pf_default_rule;
178 struct sx pf_consistency_lock;
179 SX_SYSINIT(pf_consistency_lock, &pf_consistency_lock, "pf_statetbl_lock");
181 struct rwlock pf_consistency_lock = RWLOCK_INITIALIZER;
184 static int pf_altq_running;
187 #define TAGID_MAX 50000
188 TAILQ_HEAD(pf_tags, pf_tagname) pf_tags = TAILQ_HEAD_INITIALIZER(pf_tags),
189 pf_qids = TAILQ_HEAD_INITIALIZER(pf_qids);
191 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
192 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
194 u_int16_t tagname2tag(struct pf_tags *, char *);
195 void tag2tagname(struct pf_tags *, u_int16_t, char *);
196 void tag_unref(struct pf_tags *, u_int16_t);
197 int pf_rtlabel_add(struct pf_addr_wrap *);
198 void pf_rtlabel_remove(struct pf_addr_wrap *);
199 void pf_rtlabel_copyout(struct pf_addr_wrap *);
201 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
205 static struct cdev *pf_dev;
208 * XXX - These are new and need to be checked when moveing to a new version
210 static void pf_clear_states(void);
211 static int pf_clear_tables(void);
212 static void pf_clear_srcnodes(void);
214 * XXX - These are new and need to be checked when moveing to a new version
218 * Wrapper functions for pfil(9) hooks
220 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
221 int dir, struct inpcb *inp);
222 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
223 int dir, struct inpcb *inp);
225 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
226 int dir, struct inpcb *inp);
227 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
228 int dir, struct inpcb *inp);
231 static int hook_pf(void);
232 static int dehook_pf(void);
233 static int shutdown_pf(void);
234 static int pf_load(void);
235 static int pf_unload(void);
237 static struct cdevsw pf_cdevsw = {
240 .d_version = D_VERSION,
243 static volatile int pf_pfil_hooked = 0;
244 int pf_end_threads = 0;
245 struct mtx pf_task_mtx;
246 pflog_packet_t *pflog_packet_ptr = NULL;
248 int debug_pfugidhack = 0;
249 SYSCTL_INT(_debug, OID_AUTO, pfugidhack, CTLFLAG_RW, &debug_pfugidhack, 0,
250 "Enable/disable pf user/group rules mpsafe hack");
255 mtx_init(&pf_task_mtx, "pf task mtx", NULL, MTX_DEF);
259 destroy_pf_mutex(void)
261 mtx_destroy(&pf_task_mtx);
267 pf_src_tree_pl = pf_rule_pl = NULL;
268 pf_state_pl = pf_altq_pl = pf_pooladdr_pl = NULL;
269 pf_frent_pl = pf_frag_pl = pf_cache_pl = pf_cent_pl = NULL;
270 pf_state_scrub_pl = NULL;
271 pfr_ktable_pl = pfr_kentry_pl = NULL;
275 cleanup_pf_zone(void)
277 UMA_DESTROY(pf_src_tree_pl);
278 UMA_DESTROY(pf_rule_pl);
279 UMA_DESTROY(pf_state_pl);
280 UMA_DESTROY(pf_altq_pl);
281 UMA_DESTROY(pf_pooladdr_pl);
282 UMA_DESTROY(pf_frent_pl);
283 UMA_DESTROY(pf_frag_pl);
284 UMA_DESTROY(pf_cache_pl);
285 UMA_DESTROY(pf_cent_pl);
286 UMA_DESTROY(pfr_ktable_pl);
287 UMA_DESTROY(pfr_kentry_pl2);
288 UMA_DESTROY(pfr_kentry_pl);
289 UMA_DESTROY(pf_state_scrub_pl);
290 UMA_DESTROY(pfi_addr_pl);
296 u_int32_t *my_timeout = pf_default_rule.timeout;
300 UMA_CREATE(pf_src_tree_pl,struct pf_src_node, "pfsrctrpl");
301 UMA_CREATE(pf_rule_pl, struct pf_rule, "pfrulepl");
302 UMA_CREATE(pf_state_pl, struct pf_state, "pfstatepl");
303 UMA_CREATE(pf_altq_pl, struct pf_altq, "pfaltqpl");
304 UMA_CREATE(pf_pooladdr_pl, struct pf_pooladdr, "pfpooladdrpl");
305 UMA_CREATE(pfr_ktable_pl, struct pfr_ktable, "pfrktable");
306 UMA_CREATE(pfr_kentry_pl, struct pfr_kentry, "pfrkentry");
307 UMA_CREATE(pfr_kentry_pl2, struct pfr_kentry, "pfrkentry2");
308 UMA_CREATE(pf_frent_pl, struct pf_frent, "pffrent");
309 UMA_CREATE(pf_frag_pl, struct pf_fragment, "pffrag");
310 UMA_CREATE(pf_cache_pl, struct pf_fragment, "pffrcache");
311 UMA_CREATE(pf_cent_pl, struct pf_frcache, "pffrcent");
312 UMA_CREATE(pf_state_scrub_pl, struct pf_state_scrub,
314 UMA_CREATE(pfi_addr_pl, struct pfi_dynaddr, "pfiaddrpl");
323 if ( (error = pf_osfp_initialize()) ) {
329 pf_pool_limits[PF_LIMIT_STATES].pp = pf_state_pl;
330 pf_pool_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
331 pf_pool_limits[PF_LIMIT_SRC_NODES].pp = pf_src_tree_pl;
332 pf_pool_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
333 pf_pool_limits[PF_LIMIT_FRAGS].pp = pf_frent_pl;
334 pf_pool_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
335 pf_pool_limits[PF_LIMIT_TABLES].pp = pfr_ktable_pl;
336 pf_pool_limits[PF_LIMIT_TABLES].limit = PFR_KTABLE_HIWAT;
337 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].pp = pfr_kentry_pl;
338 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit = PFR_KENTRY_HIWAT;
339 uma_zone_set_max(pf_pool_limits[PF_LIMIT_STATES].pp,
340 pf_pool_limits[PF_LIMIT_STATES].limit);
342 RB_INIT(&tree_src_tracking);
343 RB_INIT(&pf_anchors);
344 pf_init_ruleset(&pf_main_ruleset);
345 TAILQ_INIT(&pf_altqs[0]);
346 TAILQ_INIT(&pf_altqs[1]);
347 TAILQ_INIT(&pf_pabuf);
348 pf_altqs_active = &pf_altqs[0];
349 pf_altqs_inactive = &pf_altqs[1];
350 TAILQ_INIT(&state_list);
352 /* default rule should never be garbage collected */
353 pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
354 pf_default_rule.action = PF_PASS;
355 pf_default_rule.nr = -1;
356 pf_default_rule.rtableid = -1;
358 /* initialize default timeouts */
359 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
360 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
361 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
362 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
363 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
364 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
365 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
366 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
367 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
368 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
369 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
370 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
371 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
372 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
373 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
374 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
375 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
376 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
377 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
378 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
381 bzero(&pf_status, sizeof(pf_status));
382 pf_status.debug = PF_DEBUG_URGENT;
386 /* XXX do our best to avoid a conflict */
387 pf_status.hostid = arc4random();
389 if (kthread_create(pf_purge_thread, NULL, NULL, 0, 0, "pfpurge"))
394 #else /* !__FreeBSD__ */
398 u_int32_t *timeout = pf_default_rule.timeout;
400 pool_init(&pf_rule_pl, sizeof(struct pf_rule), 0, 0, 0, "pfrulepl",
401 &pool_allocator_nointr);
402 pool_init(&pf_src_tree_pl, sizeof(struct pf_src_node), 0, 0, 0,
404 pool_init(&pf_state_pl, sizeof(struct pf_state), 0, 0, 0, "pfstatepl",
406 pool_init(&pf_altq_pl, sizeof(struct pf_altq), 0, 0, 0, "pfaltqpl",
407 &pool_allocator_nointr);
408 pool_init(&pf_pooladdr_pl, sizeof(struct pf_pooladdr), 0, 0, 0,
409 "pfpooladdrpl", &pool_allocator_nointr);
412 pf_osfp_initialize();
414 pool_sethardlimit(pf_pool_limits[PF_LIMIT_STATES].pp,
415 pf_pool_limits[PF_LIMIT_STATES].limit, NULL, 0);
417 if (ctob(physmem) <= 100*1024*1024)
418 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit =
419 PFR_KENTRY_HIWAT_SMALL;
421 RB_INIT(&tree_src_tracking);
422 RB_INIT(&pf_anchors);
423 pf_init_ruleset(&pf_main_ruleset);
424 TAILQ_INIT(&pf_altqs[0]);
425 TAILQ_INIT(&pf_altqs[1]);
426 TAILQ_INIT(&pf_pabuf);
427 pf_altqs_active = &pf_altqs[0];
428 pf_altqs_inactive = &pf_altqs[1];
429 TAILQ_INIT(&state_list);
431 /* default rule should never be garbage collected */
432 pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
433 pf_default_rule.action = PF_PASS;
434 pf_default_rule.nr = -1;
435 pf_default_rule.rtableid = -1;
437 /* initialize default timeouts */
438 timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
439 timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
440 timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
441 timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
442 timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
443 timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
444 timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
445 timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
446 timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
447 timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
448 timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
449 timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
450 timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
451 timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
452 timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
453 timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
454 timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
455 timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
456 timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
457 timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
460 bzero(&pf_status, sizeof(pf_status));
461 pf_status.debug = PF_DEBUG_URGENT;
463 /* XXX do our best to avoid a conflict */
464 pf_status.hostid = arc4random();
466 /* require process context to purge states, so perform in a thread */
467 kthread_create_deferred(pf_thread_create, NULL);
471 pf_thread_create(void *v)
473 if (kthread_create(pf_purge_thread, NULL, NULL, "pfpurge"))
474 panic("pfpurge thread");
478 pfopen(struct cdev *dev, int flags, int fmt, struct proc *p)
486 pfclose(struct cdev *dev, int flags, int fmt, struct proc *p)
492 #endif /* __FreeBSD__ */
495 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
496 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
497 u_int8_t check_ticket)
499 struct pf_ruleset *ruleset;
500 struct pf_rule *rule;
503 ruleset = pf_find_ruleset(anchor);
506 rs_num = pf_get_ruleset_number(rule_action);
507 if (rs_num >= PF_RULESET_MAX)
510 if (check_ticket && ticket !=
511 ruleset->rules[rs_num].active.ticket)
514 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
517 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
519 if (check_ticket && ticket !=
520 ruleset->rules[rs_num].inactive.ticket)
523 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
526 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
529 while ((rule != NULL) && (rule->nr != rule_number))
530 rule = TAILQ_NEXT(rule, entries);
535 return (&rule->rpool);
539 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
541 struct pf_pooladdr *mv_pool_pa;
543 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
544 TAILQ_REMOVE(poola, mv_pool_pa, entries);
545 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
550 pf_empty_pool(struct pf_palist *poola)
552 struct pf_pooladdr *empty_pool_pa;
554 while ((empty_pool_pa = TAILQ_FIRST(poola)) != NULL) {
555 pfi_dynaddr_remove(&empty_pool_pa->addr);
556 pf_tbladdr_remove(&empty_pool_pa->addr);
557 pfi_kif_unref(empty_pool_pa->kif, PFI_KIF_REF_RULE);
558 TAILQ_REMOVE(poola, empty_pool_pa, entries);
559 pool_put(&pf_pooladdr_pl, empty_pool_pa);
564 pf_rm_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
566 if (rulequeue != NULL) {
567 if (rule->states <= 0) {
569 * XXX - we need to remove the table *before* detaching
570 * the rule to make sure the table code does not delete
571 * the anchor under our feet.
573 pf_tbladdr_remove(&rule->src.addr);
574 pf_tbladdr_remove(&rule->dst.addr);
575 if (rule->overload_tbl)
576 pfr_detach_table(rule->overload_tbl);
578 TAILQ_REMOVE(rulequeue, rule, entries);
579 rule->entries.tqe_prev = NULL;
583 if (rule->states > 0 || rule->src_nodes > 0 ||
584 rule->entries.tqe_prev != NULL)
586 pf_tag_unref(rule->tag);
587 pf_tag_unref(rule->match_tag);
589 if (rule->pqid != rule->qid)
590 pf_qid_unref(rule->pqid);
591 pf_qid_unref(rule->qid);
593 pf_rtlabel_remove(&rule->src.addr);
594 pf_rtlabel_remove(&rule->dst.addr);
595 pfi_dynaddr_remove(&rule->src.addr);
596 pfi_dynaddr_remove(&rule->dst.addr);
597 if (rulequeue == NULL) {
598 pf_tbladdr_remove(&rule->src.addr);
599 pf_tbladdr_remove(&rule->dst.addr);
600 if (rule->overload_tbl)
601 pfr_detach_table(rule->overload_tbl);
603 pfi_kif_unref(rule->kif, PFI_KIF_REF_RULE);
604 pf_anchor_remove(rule);
605 pf_empty_pool(&rule->rpool.list);
606 pool_put(&pf_rule_pl, rule);
610 tagname2tag(struct pf_tags *head, char *tagname)
612 struct pf_tagname *tag, *p = NULL;
613 u_int16_t new_tagid = 1;
615 TAILQ_FOREACH(tag, head, entries)
616 if (strcmp(tagname, tag->name) == 0) {
622 * to avoid fragmentation, we do a linear search from the beginning
623 * and take the first free slot we find. if there is none or the list
624 * is empty, append a new entry at the end.
628 if (!TAILQ_EMPTY(head))
629 for (p = TAILQ_FIRST(head); p != NULL &&
630 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
631 new_tagid = p->tag + 1;
633 if (new_tagid > TAGID_MAX)
636 /* allocate and fill new struct pf_tagname */
637 tag = (struct pf_tagname *)malloc(sizeof(struct pf_tagname),
641 bzero(tag, sizeof(struct pf_tagname));
642 strlcpy(tag->name, tagname, sizeof(tag->name));
643 tag->tag = new_tagid;
646 if (p != NULL) /* insert new entry before p */
647 TAILQ_INSERT_BEFORE(p, tag, entries);
648 else /* either list empty or no free slot in between */
649 TAILQ_INSERT_TAIL(head, tag, entries);
655 tag2tagname(struct pf_tags *head, u_int16_t tagid, char *p)
657 struct pf_tagname *tag;
659 TAILQ_FOREACH(tag, head, entries)
660 if (tag->tag == tagid) {
661 strlcpy(p, tag->name, PF_TAG_NAME_SIZE);
667 tag_unref(struct pf_tags *head, u_int16_t tag)
669 struct pf_tagname *p, *next;
674 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
675 next = TAILQ_NEXT(p, entries);
678 TAILQ_REMOVE(head, p, entries);
687 pf_tagname2tag(char *tagname)
689 return (tagname2tag(&pf_tags, tagname));
693 pf_tag2tagname(u_int16_t tagid, char *p)
695 tag2tagname(&pf_tags, tagid, p);
699 pf_tag_ref(u_int16_t tag)
701 struct pf_tagname *t;
703 TAILQ_FOREACH(t, &pf_tags, entries)
711 pf_tag_unref(u_int16_t tag)
713 tag_unref(&pf_tags, tag);
717 pf_rtlabel_add(struct pf_addr_wrap *a)
720 /* XXX_IMPORT: later */
723 if (a->type == PF_ADDR_RTLABEL &&
724 (a->v.rtlabel = rtlabel_name2id(a->v.rtlabelname)) == 0)
731 pf_rtlabel_remove(struct pf_addr_wrap *a)
734 /* XXX_IMPORT: later */
736 if (a->type == PF_ADDR_RTLABEL)
737 rtlabel_unref(a->v.rtlabel);
742 pf_rtlabel_copyout(struct pf_addr_wrap *a)
745 /* XXX_IMPORT: later */
746 if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel)
747 strlcpy(a->v.rtlabelname, "?", sizeof(a->v.rtlabelname));
751 if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel) {
752 if ((name = rtlabel_id2name(a->v.rtlabel)) == NULL)
753 strlcpy(a->v.rtlabelname, "?",
754 sizeof(a->v.rtlabelname));
756 strlcpy(a->v.rtlabelname, name,
757 sizeof(a->v.rtlabelname));
764 pf_qname2qid(char *qname)
766 return ((u_int32_t)tagname2tag(&pf_qids, qname));
770 pf_qid2qname(u_int32_t qid, char *p)
772 tag2tagname(&pf_qids, (u_int16_t)qid, p);
776 pf_qid_unref(u_int32_t qid)
778 tag_unref(&pf_qids, (u_int16_t)qid);
782 pf_begin_altq(u_int32_t *ticket)
784 struct pf_altq *altq;
787 /* Purge the old altq list */
788 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
789 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
791 if (altq->qname[0] == 0 &&
792 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
794 if (altq->qname[0] == 0) {
796 /* detach and destroy the discipline */
797 error = altq_remove(altq);
799 pf_qid_unref(altq->qid);
800 pool_put(&pf_altq_pl, altq);
804 *ticket = ++ticket_altqs_inactive;
805 altqs_inactive_open = 1;
810 pf_rollback_altq(u_int32_t ticket)
812 struct pf_altq *altq;
815 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
817 /* Purge the old altq list */
818 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
819 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
821 if (altq->qname[0] == 0 &&
822 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
824 if (altq->qname[0] == 0) {
826 /* detach and destroy the discipline */
827 error = altq_remove(altq);
829 pf_qid_unref(altq->qid);
830 pool_put(&pf_altq_pl, altq);
832 altqs_inactive_open = 0;
837 pf_commit_altq(u_int32_t ticket)
839 struct pf_altqqueue *old_altqs;
840 struct pf_altq *altq;
841 int s, err, error = 0;
843 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
846 /* swap altqs, keep the old. */
848 old_altqs = pf_altqs_active;
849 pf_altqs_active = pf_altqs_inactive;
850 pf_altqs_inactive = old_altqs;
851 ticket_altqs_active = ticket_altqs_inactive;
853 /* Attach new disciplines */
854 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
856 if (altq->qname[0] == 0 &&
857 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
859 if (altq->qname[0] == 0) {
861 /* attach the discipline */
862 error = altq_pfattach(altq);
863 if (error == 0 && pf_altq_running)
864 error = pf_enable_altq(altq);
872 /* Purge the old altq list */
873 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
874 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
876 if (altq->qname[0] == 0 &&
877 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
879 if (altq->qname[0] == 0) {
881 /* detach and destroy the discipline */
883 error = pf_disable_altq(altq);
884 err = altq_pfdetach(altq);
885 if (err != 0 && error == 0)
887 err = altq_remove(altq);
888 if (err != 0 && error == 0)
891 pf_qid_unref(altq->qid);
892 pool_put(&pf_altq_pl, altq);
896 altqs_inactive_open = 0;
901 pf_enable_altq(struct pf_altq *altq)
904 struct tb_profile tb;
907 if ((ifp = ifunit(altq->ifname)) == NULL)
910 if (ifp->if_snd.altq_type != ALTQT_NONE)
911 error = altq_enable(&ifp->if_snd);
913 /* set tokenbucket regulator */
914 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
915 tb.rate = altq->ifbandwidth;
916 tb.depth = altq->tbrsize;
921 error = tbr_set(&ifp->if_snd, &tb);
932 pf_disable_altq(struct pf_altq *altq)
935 struct tb_profile tb;
938 if ((ifp = ifunit(altq->ifname)) == NULL)
942 * when the discipline is no longer referenced, it was overridden
943 * by a new one. if so, just return.
945 if (altq->altq_disc != ifp->if_snd.altq_disc)
948 error = altq_disable(&ifp->if_snd);
951 /* clear tokenbucket regulator */
957 error = tbr_set(&ifp->if_snd, &tb);
969 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
972 struct pf_altq *a1, *a2, *a3;
976 /* Interrupt userland queue modifications */
977 if (altqs_inactive_open)
978 pf_rollback_altq(ticket_altqs_inactive);
980 /* Start new altq ruleset */
981 if (pf_begin_altq(&ticket))
984 /* Copy the current active set */
985 TAILQ_FOREACH(a1, pf_altqs_active, entries) {
986 a2 = pool_get(&pf_altq_pl, PR_NOWAIT);
991 bcopy(a1, a2, sizeof(struct pf_altq));
993 if (a2->qname[0] != 0) {
994 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
996 pool_put(&pf_altq_pl, a2);
999 a2->altq_disc = NULL;
1000 TAILQ_FOREACH(a3, pf_altqs_inactive, entries) {
1001 if (strncmp(a3->ifname, a2->ifname,
1002 IFNAMSIZ) == 0 && a3->qname[0] == 0) {
1003 a2->altq_disc = a3->altq_disc;
1008 /* Deactivate the interface in question */
1009 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
1010 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
1011 (remove && ifp1 == ifp)) {
1012 a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
1015 error = altq_add(a2);
1018 if (ticket != ticket_altqs_inactive)
1022 pool_put(&pf_altq_pl, a2);
1027 TAILQ_INSERT_TAIL(pf_altqs_inactive, a2, entries);
1031 pf_rollback_altq(ticket);
1033 pf_commit_altq(ticket);
1039 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
1041 struct pf_ruleset *rs;
1042 struct pf_rule *rule;
1044 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1046 rs = pf_find_or_create_ruleset(anchor);
1049 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1050 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
1051 rs->rules[rs_num].inactive.rcount--;
1053 *ticket = ++rs->rules[rs_num].inactive.ticket;
1054 rs->rules[rs_num].inactive.open = 1;
1059 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
1061 struct pf_ruleset *rs;
1062 struct pf_rule *rule;
1064 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1066 rs = pf_find_ruleset(anchor);
1067 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1068 rs->rules[rs_num].inactive.ticket != ticket)
1070 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1071 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
1072 rs->rules[rs_num].inactive.rcount--;
1074 rs->rules[rs_num].inactive.open = 0;
1078 #define PF_MD5_UPD(st, elm) \
1079 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
1081 #define PF_MD5_UPD_STR(st, elm) \
1082 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
1084 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
1085 (stor) = htonl((st)->elm); \
1086 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
1089 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
1090 (stor) = htons((st)->elm); \
1091 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
1095 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
1097 PF_MD5_UPD(pfr, addr.type);
1098 switch (pfr->addr.type) {
1099 case PF_ADDR_DYNIFTL:
1100 PF_MD5_UPD(pfr, addr.v.ifname);
1101 PF_MD5_UPD(pfr, addr.iflags);
1104 PF_MD5_UPD(pfr, addr.v.tblname);
1106 case PF_ADDR_ADDRMASK:
1107 /* XXX ignore af? */
1108 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
1109 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
1111 case PF_ADDR_RTLABEL:
1112 PF_MD5_UPD(pfr, addr.v.rtlabelname);
1116 PF_MD5_UPD(pfr, port[0]);
1117 PF_MD5_UPD(pfr, port[1]);
1118 PF_MD5_UPD(pfr, neg);
1119 PF_MD5_UPD(pfr, port_op);
1123 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
1128 pf_hash_rule_addr(ctx, &rule->src);
1129 pf_hash_rule_addr(ctx, &rule->dst);
1130 PF_MD5_UPD_STR(rule, label);
1131 PF_MD5_UPD_STR(rule, ifname);
1132 PF_MD5_UPD_STR(rule, match_tagname);
1133 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
1134 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
1135 PF_MD5_UPD_HTONL(rule, prob, y);
1136 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
1137 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
1138 PF_MD5_UPD(rule, uid.op);
1139 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
1140 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
1141 PF_MD5_UPD(rule, gid.op);
1142 PF_MD5_UPD_HTONL(rule, rule_flag, y);
1143 PF_MD5_UPD(rule, action);
1144 PF_MD5_UPD(rule, direction);
1145 PF_MD5_UPD(rule, af);
1146 PF_MD5_UPD(rule, quick);
1147 PF_MD5_UPD(rule, ifnot);
1148 PF_MD5_UPD(rule, match_tag_not);
1149 PF_MD5_UPD(rule, natpass);
1150 PF_MD5_UPD(rule, keep_state);
1151 PF_MD5_UPD(rule, proto);
1152 PF_MD5_UPD(rule, type);
1153 PF_MD5_UPD(rule, code);
1154 PF_MD5_UPD(rule, flags);
1155 PF_MD5_UPD(rule, flagset);
1156 PF_MD5_UPD(rule, allow_opts);
1157 PF_MD5_UPD(rule, rt);
1158 PF_MD5_UPD(rule, tos);
1162 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
1164 struct pf_ruleset *rs;
1165 struct pf_rule *rule, **old_array;
1166 struct pf_rulequeue *old_rules;
1168 u_int32_t old_rcount;
1170 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1172 rs = pf_find_ruleset(anchor);
1173 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1174 ticket != rs->rules[rs_num].inactive.ticket)
1177 /* Calculate checksum for the main ruleset */
1178 if (rs == &pf_main_ruleset) {
1179 error = pf_setup_pfsync_matching(rs);
1184 /* Swap rules, keep the old. */
1186 old_rules = rs->rules[rs_num].active.ptr;
1187 old_rcount = rs->rules[rs_num].active.rcount;
1188 old_array = rs->rules[rs_num].active.ptr_array;
1190 rs->rules[rs_num].active.ptr =
1191 rs->rules[rs_num].inactive.ptr;
1192 rs->rules[rs_num].active.ptr_array =
1193 rs->rules[rs_num].inactive.ptr_array;
1194 rs->rules[rs_num].active.rcount =
1195 rs->rules[rs_num].inactive.rcount;
1196 rs->rules[rs_num].inactive.ptr = old_rules;
1197 rs->rules[rs_num].inactive.ptr_array = old_array;
1198 rs->rules[rs_num].inactive.rcount = old_rcount;
1200 rs->rules[rs_num].active.ticket =
1201 rs->rules[rs_num].inactive.ticket;
1202 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
1205 /* Purge the old rule list. */
1206 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
1207 pf_rm_rule(old_rules, rule);
1208 if (rs->rules[rs_num].inactive.ptr_array)
1209 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
1210 rs->rules[rs_num].inactive.ptr_array = NULL;
1211 rs->rules[rs_num].inactive.rcount = 0;
1212 rs->rules[rs_num].inactive.open = 0;
1213 pf_remove_if_empty_ruleset(rs);
1219 pf_setup_pfsync_matching(struct pf_ruleset *rs)
1222 struct pf_rule *rule;
1224 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
1227 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
1228 /* XXX PF_RULESET_SCRUB as well? */
1229 if (rs_cnt == PF_RULESET_SCRUB)
1232 if (rs->rules[rs_cnt].inactive.ptr_array)
1233 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
1234 rs->rules[rs_cnt].inactive.ptr_array = NULL;
1236 if (rs->rules[rs_cnt].inactive.rcount) {
1237 rs->rules[rs_cnt].inactive.ptr_array =
1238 malloc(sizeof(caddr_t) *
1239 rs->rules[rs_cnt].inactive.rcount,
1242 if (!rs->rules[rs_cnt].inactive.ptr_array)
1246 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
1248 pf_hash_rule(&ctx, rule);
1249 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
1253 MD5Final(digest, &ctx);
1254 memcpy(pf_status.pf_chksum, digest, sizeof(pf_status.pf_chksum));
1260 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1262 pfioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
1265 struct pf_pooladdr *pa = NULL;
1266 struct pf_pool *pool = NULL;
1272 /* XXX keep in sync with switch() below */
1274 if (securelevel_gt(td->td_ucred, 2))
1276 if (securelevel > 1)
1284 case DIOCSETSTATUSIF:
1290 case DIOCGETTIMEOUT:
1291 case DIOCCLRRULECTRS:
1296 case DIOCGETRULESETS:
1297 case DIOCGETRULESET:
1298 case DIOCRGETTABLES:
1299 case DIOCRGETTSTATS:
1300 case DIOCRCLRTSTATS:
1306 case DIOCRGETASTATS:
1307 case DIOCRCLRASTATS:
1310 case DIOCGETSRCNODES:
1311 case DIOCCLRSRCNODES:
1312 case DIOCIGETIFACES:
1319 case DIOCRCLRTABLES:
1320 case DIOCRADDTABLES:
1321 case DIOCRDELTABLES:
1322 case DIOCRSETTFLAGS:
1323 if (((struct pfioc_table *)addr)->pfrio_flags &
1325 break; /* dummy operation ok */
1331 if (!(flags & FWRITE))
1339 case DIOCGETTIMEOUT:
1344 case DIOCGETRULESETS:
1345 case DIOCGETRULESET:
1347 case DIOCRGETTABLES:
1348 case DIOCRGETTSTATS:
1350 case DIOCRGETASTATS:
1353 case DIOCGETSRCNODES:
1354 case DIOCIGETIFACES:
1359 case DIOCRCLRTABLES:
1360 case DIOCRADDTABLES:
1361 case DIOCRDELTABLES:
1362 case DIOCRCLRTSTATS:
1367 case DIOCRSETTFLAGS:
1368 if (((struct pfioc_table *)addr)->pfrio_flags &
1370 flags |= FWRITE; /* need write lock for dummy */
1371 break; /* dummy operation ok */
1375 if (((struct pfioc_rule *)addr)->action == PF_GET_CLR_CNTR)
1384 sx_xlock(&pf_consistency_lock);
1386 sx_slock(&pf_consistency_lock);
1388 rw_enter_write(&pf_consistency_lock);
1390 rw_enter_read(&pf_consistency_lock);
1401 if (pf_status.running)
1409 DPFPRINTF(PF_DEBUG_MISC,
1410 ("pf: pfil registeration fail\n"));
1414 pf_status.running = 1;
1415 pf_status.since = time_second;
1416 if (pf_status.stateid == 0) {
1417 pf_status.stateid = time_second;
1418 pf_status.stateid = pf_status.stateid << 32;
1420 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1425 if (!pf_status.running)
1428 pf_status.running = 0;
1431 error = dehook_pf();
1434 pf_status.running = 1;
1435 DPFPRINTF(PF_DEBUG_MISC,
1436 ("pf: pfil unregisteration failed\n"));
1439 pf_status.since = time_second;
1440 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1445 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1446 struct pf_ruleset *ruleset;
1447 struct pf_rule *rule, *tail;
1448 struct pf_pooladdr *pa;
1451 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1452 ruleset = pf_find_ruleset(pr->anchor);
1453 if (ruleset == NULL) {
1457 rs_num = pf_get_ruleset_number(pr->rule.action);
1458 if (rs_num >= PF_RULESET_MAX) {
1462 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1466 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1468 DPFPRINTF(PF_DEBUG_MISC,
1469 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1470 ruleset->rules[rs_num].inactive.ticket));
1475 if (pr->pool_ticket != ticket_pabuf) {
1477 DPFPRINTF(PF_DEBUG_MISC,
1478 ("pool_ticket: %d != %d\n", pr->pool_ticket,
1484 rule = pool_get(&pf_rule_pl, PR_NOWAIT);
1489 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1491 rule->cuid = td->td_ucred->cr_ruid;
1492 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1494 rule->cuid = p->p_cred->p_ruid;
1495 rule->cpid = p->p_pid;
1497 rule->anchor = NULL;
1499 TAILQ_INIT(&rule->rpool.list);
1500 /* initialize refcounting */
1502 rule->src_nodes = 0;
1503 rule->entries.tqe_prev = NULL;
1505 if (rule->af == AF_INET) {
1506 pool_put(&pf_rule_pl, rule);
1507 error = EAFNOSUPPORT;
1512 if (rule->af == AF_INET6) {
1513 pool_put(&pf_rule_pl, rule);
1514 error = EAFNOSUPPORT;
1518 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1521 rule->nr = tail->nr + 1;
1524 if (rule->ifname[0]) {
1525 rule->kif = pfi_kif_get(rule->ifname);
1526 if (rule->kif == NULL) {
1527 pool_put(&pf_rule_pl, rule);
1531 pfi_kif_ref(rule->kif, PFI_KIF_REF_RULE);
1534 #ifdef __FreeBSD__ /* ROUTING */
1535 if (rule->rtableid > 0 && rule->rtableid > rt_numfibs)
1537 if (rule->rtableid > 0 && !rtable_exists(rule->rtableid))
1543 if (rule->qname[0] != 0) {
1544 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1546 else if (rule->pqname[0] != 0) {
1548 pf_qname2qid(rule->pqname)) == 0)
1551 rule->pqid = rule->qid;
1554 if (rule->tagname[0])
1555 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1557 if (rule->match_tagname[0])
1558 if ((rule->match_tag =
1559 pf_tagname2tag(rule->match_tagname)) == 0)
1561 if (rule->rt && !rule->direction)
1568 if (rule->logif >= PFLOGIFS_MAX)
1571 if (pf_rtlabel_add(&rule->src.addr) ||
1572 pf_rtlabel_add(&rule->dst.addr))
1574 if (pfi_dynaddr_setup(&rule->src.addr, rule->af))
1576 if (pfi_dynaddr_setup(&rule->dst.addr, rule->af))
1578 if (pf_tbladdr_setup(ruleset, &rule->src.addr))
1580 if (pf_tbladdr_setup(ruleset, &rule->dst.addr))
1582 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1584 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1585 if (pf_tbladdr_setup(ruleset, &pa->addr))
1588 if (rule->overload_tblname[0]) {
1589 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1590 rule->overload_tblname)) == NULL)
1593 rule->overload_tbl->pfrkt_flags |=
1597 pf_mv_pool(&pf_pabuf, &rule->rpool.list);
1598 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1599 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1600 (rule->rt > PF_FASTROUTE)) &&
1601 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1605 pf_rm_rule(NULL, rule);
1610 if (!debug_pfugidhack && (rule->uid.op || rule->gid.op ||
1611 rule->log & PF_LOG_SOCKET_LOOKUP)) {
1612 DPFPRINTF(PF_DEBUG_MISC,
1613 ("pf: debug.pfugidhack enabled\n"));
1614 debug_pfugidhack = 1;
1618 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1619 rule->evaluations = rule->packets[0] = rule->packets[1] =
1620 rule->bytes[0] = rule->bytes[1] = 0;
1621 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1623 ruleset->rules[rs_num].inactive.rcount++;
1627 case DIOCGETRULES: {
1628 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1629 struct pf_ruleset *ruleset;
1630 struct pf_rule *tail;
1633 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1634 ruleset = pf_find_ruleset(pr->anchor);
1635 if (ruleset == NULL) {
1639 rs_num = pf_get_ruleset_number(pr->rule.action);
1640 if (rs_num >= PF_RULESET_MAX) {
1644 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1647 pr->nr = tail->nr + 1;
1650 pr->ticket = ruleset->rules[rs_num].active.ticket;
1655 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1656 struct pf_ruleset *ruleset;
1657 struct pf_rule *rule;
1660 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1661 ruleset = pf_find_ruleset(pr->anchor);
1662 if (ruleset == NULL) {
1666 rs_num = pf_get_ruleset_number(pr->rule.action);
1667 if (rs_num >= PF_RULESET_MAX) {
1671 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1675 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1676 while ((rule != NULL) && (rule->nr != pr->nr))
1677 rule = TAILQ_NEXT(rule, entries);
1682 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1683 if (pf_anchor_copyout(ruleset, rule, pr)) {
1687 pfi_dynaddr_copyout(&pr->rule.src.addr);
1688 pfi_dynaddr_copyout(&pr->rule.dst.addr);
1689 pf_tbladdr_copyout(&pr->rule.src.addr);
1690 pf_tbladdr_copyout(&pr->rule.dst.addr);
1691 pf_rtlabel_copyout(&pr->rule.src.addr);
1692 pf_rtlabel_copyout(&pr->rule.dst.addr);
1693 for (i = 0; i < PF_SKIP_COUNT; ++i)
1694 if (rule->skip[i].ptr == NULL)
1695 pr->rule.skip[i].nr = -1;
1697 pr->rule.skip[i].nr =
1698 rule->skip[i].ptr->nr;
1700 if (pr->action == PF_GET_CLR_CNTR) {
1701 rule->evaluations = 0;
1702 rule->packets[0] = rule->packets[1] = 0;
1703 rule->bytes[0] = rule->bytes[1] = 0;
1708 case DIOCCHANGERULE: {
1709 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1710 struct pf_ruleset *ruleset;
1711 struct pf_rule *oldrule = NULL, *newrule = NULL;
1715 if (!(pcr->action == PF_CHANGE_REMOVE ||
1716 pcr->action == PF_CHANGE_GET_TICKET) &&
1717 pcr->pool_ticket != ticket_pabuf) {
1722 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1723 pcr->action > PF_CHANGE_GET_TICKET) {
1727 ruleset = pf_find_ruleset(pcr->anchor);
1728 if (ruleset == NULL) {
1732 rs_num = pf_get_ruleset_number(pcr->rule.action);
1733 if (rs_num >= PF_RULESET_MAX) {
1738 if (pcr->action == PF_CHANGE_GET_TICKET) {
1739 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1743 ruleset->rules[rs_num].active.ticket) {
1747 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1753 if (pcr->action != PF_CHANGE_REMOVE) {
1754 newrule = pool_get(&pf_rule_pl, PR_NOWAIT);
1755 if (newrule == NULL) {
1759 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1761 newrule->cuid = td->td_ucred->cr_ruid;
1762 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1764 newrule->cuid = p->p_cred->p_ruid;
1765 newrule->cpid = p->p_pid;
1767 TAILQ_INIT(&newrule->rpool.list);
1768 /* initialize refcounting */
1769 newrule->states = 0;
1770 newrule->entries.tqe_prev = NULL;
1772 if (newrule->af == AF_INET) {
1773 pool_put(&pf_rule_pl, newrule);
1774 error = EAFNOSUPPORT;
1779 if (newrule->af == AF_INET6) {
1780 pool_put(&pf_rule_pl, newrule);
1781 error = EAFNOSUPPORT;
1785 if (newrule->ifname[0]) {
1786 newrule->kif = pfi_kif_get(newrule->ifname);
1787 if (newrule->kif == NULL) {
1788 pool_put(&pf_rule_pl, newrule);
1792 pfi_kif_ref(newrule->kif, PFI_KIF_REF_RULE);
1794 newrule->kif = NULL;
1796 if (newrule->rtableid > 0 &&
1797 #ifdef __FreeBSD__ /* ROUTING */
1798 newrule->rtableid > rt_numfibs)
1800 !rtable_exists(newrule->rtableid))
1806 if (newrule->qname[0] != 0) {
1808 pf_qname2qid(newrule->qname)) == 0)
1810 else if (newrule->pqname[0] != 0) {
1811 if ((newrule->pqid =
1812 pf_qname2qid(newrule->pqname)) == 0)
1815 newrule->pqid = newrule->qid;
1818 if (newrule->tagname[0])
1820 pf_tagname2tag(newrule->tagname)) == 0)
1822 if (newrule->match_tagname[0])
1823 if ((newrule->match_tag = pf_tagname2tag(
1824 newrule->match_tagname)) == 0)
1826 if (newrule->rt && !newrule->direction)
1832 if (newrule->logif >= PFLOGIFS_MAX)
1836 if (pf_rtlabel_add(&newrule->src.addr) ||
1837 pf_rtlabel_add(&newrule->dst.addr))
1839 if (pfi_dynaddr_setup(&newrule->src.addr, newrule->af))
1841 if (pfi_dynaddr_setup(&newrule->dst.addr, newrule->af))
1843 if (pf_tbladdr_setup(ruleset, &newrule->src.addr))
1845 if (pf_tbladdr_setup(ruleset, &newrule->dst.addr))
1847 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1849 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1850 if (pf_tbladdr_setup(ruleset, &pa->addr))
1853 if (newrule->overload_tblname[0]) {
1854 if ((newrule->overload_tbl = pfr_attach_table(
1855 ruleset, newrule->overload_tblname)) ==
1859 newrule->overload_tbl->pfrkt_flags |=
1863 pf_mv_pool(&pf_pabuf, &newrule->rpool.list);
1864 if (((((newrule->action == PF_NAT) ||
1865 (newrule->action == PF_RDR) ||
1866 (newrule->action == PF_BINAT) ||
1867 (newrule->rt > PF_FASTROUTE)) &&
1868 !newrule->anchor)) &&
1869 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1873 pf_rm_rule(NULL, newrule);
1878 if (!debug_pfugidhack && (newrule->uid.op ||
1880 newrule->log & PF_LOG_SOCKET_LOOKUP)) {
1881 DPFPRINTF(PF_DEBUG_MISC,
1882 ("pf: debug.pfugidhack enabled\n"));
1883 debug_pfugidhack = 1;
1887 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1888 newrule->evaluations = 0;
1889 newrule->packets[0] = newrule->packets[1] = 0;
1890 newrule->bytes[0] = newrule->bytes[1] = 0;
1892 pf_empty_pool(&pf_pabuf);
1894 if (pcr->action == PF_CHANGE_ADD_HEAD)
1895 oldrule = TAILQ_FIRST(
1896 ruleset->rules[rs_num].active.ptr);
1897 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1898 oldrule = TAILQ_LAST(
1899 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1901 oldrule = TAILQ_FIRST(
1902 ruleset->rules[rs_num].active.ptr);
1903 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1904 oldrule = TAILQ_NEXT(oldrule, entries);
1905 if (oldrule == NULL) {
1906 if (newrule != NULL)
1907 pf_rm_rule(NULL, newrule);
1913 if (pcr->action == PF_CHANGE_REMOVE) {
1914 pf_rm_rule(ruleset->rules[rs_num].active.ptr, oldrule);
1915 ruleset->rules[rs_num].active.rcount--;
1917 if (oldrule == NULL)
1919 ruleset->rules[rs_num].active.ptr,
1921 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1922 pcr->action == PF_CHANGE_ADD_BEFORE)
1923 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1926 ruleset->rules[rs_num].active.ptr,
1927 oldrule, newrule, entries);
1928 ruleset->rules[rs_num].active.rcount++;
1932 TAILQ_FOREACH(oldrule,
1933 ruleset->rules[rs_num].active.ptr, entries)
1936 ruleset->rules[rs_num].active.ticket++;
1938 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1939 pf_remove_if_empty_ruleset(ruleset);
1944 case DIOCCLRSTATES: {
1945 struct pf_state *state, *nexts;
1946 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1949 for (state = RB_MIN(pf_state_tree_id, &tree_id); state;
1951 nexts = RB_NEXT(pf_state_tree_id, &tree_id, state);
1953 if (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1954 state->u.s.kif->pfik_name)) {
1956 /* don't send out individual delete messages */
1957 state->sync_flags = PFSTATE_NOSYNC;
1959 pf_unlink_state(state);
1963 psk->psk_af = killed;
1965 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
1970 case DIOCKILLSTATES: {
1971 struct pf_state *state, *nexts;
1972 struct pf_state_host *src, *dst;
1973 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1976 for (state = RB_MIN(pf_state_tree_id, &tree_id); state;
1978 nexts = RB_NEXT(pf_state_tree_id, &tree_id, state);
1980 if (state->direction == PF_OUT) {
1987 if ((!psk->psk_af || state->af == psk->psk_af)
1988 && (!psk->psk_proto || psk->psk_proto ==
1990 PF_MATCHA(psk->psk_src.neg,
1991 &psk->psk_src.addr.v.a.addr,
1992 &psk->psk_src.addr.v.a.mask,
1993 &src->addr, state->af) &&
1994 PF_MATCHA(psk->psk_dst.neg,
1995 &psk->psk_dst.addr.v.a.addr,
1996 &psk->psk_dst.addr.v.a.mask,
1997 &dst->addr, state->af) &&
1998 (psk->psk_src.port_op == 0 ||
1999 pf_match_port(psk->psk_src.port_op,
2000 psk->psk_src.port[0], psk->psk_src.port[1],
2002 (psk->psk_dst.port_op == 0 ||
2003 pf_match_port(psk->psk_dst.port_op,
2004 psk->psk_dst.port[0], psk->psk_dst.port[1],
2006 (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
2007 state->u.s.kif->pfik_name))) {
2009 /* send immediate delete of state */
2010 pfsync_delete_state(state);
2011 state->sync_flags |= PFSTATE_NOSYNC;
2013 pf_unlink_state(state);
2017 psk->psk_af = killed;
2021 case DIOCADDSTATE: {
2022 struct pfioc_state *ps = (struct pfioc_state *)addr;
2023 struct pf_state *state;
2024 struct pfi_kif *kif;
2026 if (ps->state.timeout >= PFTM_MAX &&
2027 ps->state.timeout != PFTM_UNTIL_PACKET) {
2031 state = pool_get(&pf_state_pl, PR_NOWAIT);
2032 if (state == NULL) {
2036 kif = pfi_kif_get(ps->state.u.ifname);
2038 pool_put(&pf_state_pl, state);
2042 bcopy(&ps->state, state, sizeof(struct pf_state));
2043 bzero(&state->u, sizeof(state->u));
2044 state->rule.ptr = &pf_default_rule;
2045 state->nat_rule.ptr = NULL;
2046 state->anchor.ptr = NULL;
2047 state->rt_kif = NULL;
2048 state->creation = time_second;
2049 state->pfsync_time = 0;
2050 state->packets[0] = state->packets[1] = 0;
2051 state->bytes[0] = state->bytes[1] = 0;
2053 if (pf_insert_state(kif, state)) {
2054 pfi_kif_unref(kif, PFI_KIF_REF_NONE);
2055 pool_put(&pf_state_pl, state);
2061 case DIOCGETSTATE: {
2062 struct pfioc_state *ps = (struct pfioc_state *)addr;
2063 struct pf_state *state;
2068 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2073 if (state == NULL) {
2078 bcopy(state, &ps->state, sizeof(ps->state));
2079 strlcpy(ps->state.u.ifname, state->u.s.kif->pfik_name,
2080 sizeof(ps->state.u.ifname));
2081 ps->state.rule.nr = state->rule.ptr->nr;
2082 ps->state.nat_rule.nr = (state->nat_rule.ptr == NULL) ?
2083 -1 : state->nat_rule.ptr->nr;
2084 ps->state.anchor.nr = (state->anchor.ptr == NULL) ?
2085 -1 : state->anchor.ptr->nr;
2086 ps->state.creation = secs - ps->state.creation;
2087 ps->state.expire = pf_state_expires(state);
2088 if (ps->state.expire > secs)
2089 ps->state.expire -= secs;
2091 ps->state.expire = 0;
2095 case DIOCGETSTATES: {
2096 struct pfioc_states *ps = (struct pfioc_states *)addr;
2097 struct pf_state *state;
2098 struct pf_state *p, *pstore;
2100 int space = ps->ps_len;
2103 nr = pf_status.states;
2104 ps->ps_len = sizeof(struct pf_state) * nr;
2111 pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
2118 state = TAILQ_FIRST(&state_list);
2120 if (state->timeout != PFTM_UNLINKED) {
2121 int secs = time_second;
2123 if ((nr+1) * sizeof(*p) > (unsigned)ps->ps_len)
2126 bcopy(state, pstore, sizeof(*pstore));
2127 strlcpy(pstore->u.ifname,
2128 state->u.s.kif->pfik_name,
2129 sizeof(pstore->u.ifname));
2130 pstore->rule.nr = state->rule.ptr->nr;
2131 pstore->nat_rule.nr = (state->nat_rule.ptr ==
2132 NULL) ? -1 : state->nat_rule.ptr->nr;
2133 pstore->anchor.nr = (state->anchor.ptr ==
2134 NULL) ? -1 : state->anchor.ptr->nr;
2135 pstore->creation = secs - pstore->creation;
2136 pstore->expire = pf_state_expires(state);
2137 if (pstore->expire > secs)
2138 pstore->expire -= secs;
2142 PF_COPYOUT(pstore, p, sizeof(*p), error);
2144 error = copyout(pstore, p, sizeof(*p));
2147 free(pstore, M_TEMP);
2153 state = TAILQ_NEXT(state, u.s.entry_list);
2156 ps->ps_len = sizeof(struct pf_state) * nr;
2158 free(pstore, M_TEMP);
2162 case DIOCGETSTATUS: {
2163 struct pf_status *s = (struct pf_status *)addr;
2164 bcopy(&pf_status, s, sizeof(struct pf_status));
2165 pfi_fill_oldstatus(s);
2169 case DIOCSETSTATUSIF: {
2170 struct pfioc_if *pi = (struct pfioc_if *)addr;
2172 if (pi->ifname[0] == 0) {
2173 bzero(pf_status.ifname, IFNAMSIZ);
2176 if (ifunit(pi->ifname) == NULL) {
2180 strlcpy(pf_status.ifname, pi->ifname, IFNAMSIZ);
2184 case DIOCCLRSTATUS: {
2185 bzero(pf_status.counters, sizeof(pf_status.counters));
2186 bzero(pf_status.fcounters, sizeof(pf_status.fcounters));
2187 bzero(pf_status.scounters, sizeof(pf_status.scounters));
2188 pf_status.since = time_second;
2189 if (*pf_status.ifname)
2190 pfi_clr_istats(pf_status.ifname);
2195 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
2196 struct pf_state *state;
2197 struct pf_state_cmp key;
2198 int m = 0, direction = pnl->direction;
2201 key.proto = pnl->proto;
2204 PF_AZERO(&pnl->saddr, pnl->af) ||
2205 PF_AZERO(&pnl->daddr, pnl->af) ||
2206 ((pnl->proto == IPPROTO_TCP ||
2207 pnl->proto == IPPROTO_UDP) &&
2208 (!pnl->dport || !pnl->sport)))
2212 * userland gives us source and dest of connection,
2213 * reverse the lookup so we ask for what happens with
2214 * the return traffic, enabling us to find it in the
2217 if (direction == PF_IN) {
2218 PF_ACPY(&key.ext.addr, &pnl->daddr, pnl->af);
2219 key.ext.port = pnl->dport;
2220 PF_ACPY(&key.gwy.addr, &pnl->saddr, pnl->af);
2221 key.gwy.port = pnl->sport;
2222 state = pf_find_state_all(&key, PF_EXT_GWY, &m);
2224 PF_ACPY(&key.lan.addr, &pnl->daddr, pnl->af);
2225 key.lan.port = pnl->dport;
2226 PF_ACPY(&key.ext.addr, &pnl->saddr, pnl->af);
2227 key.ext.port = pnl->sport;
2228 state = pf_find_state_all(&key, PF_LAN_EXT, &m);
2231 error = E2BIG; /* more than one state */
2232 else if (state != NULL) {
2233 if (direction == PF_IN) {
2234 PF_ACPY(&pnl->rsaddr, &state->lan.addr,
2236 pnl->rsport = state->lan.port;
2237 PF_ACPY(&pnl->rdaddr, &pnl->daddr,
2239 pnl->rdport = pnl->dport;
2241 PF_ACPY(&pnl->rdaddr, &state->gwy.addr,
2243 pnl->rdport = state->gwy.port;
2244 PF_ACPY(&pnl->rsaddr, &pnl->saddr,
2246 pnl->rsport = pnl->sport;
2254 case DIOCSETTIMEOUT: {
2255 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
2258 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
2263 old = pf_default_rule.timeout[pt->timeout];
2264 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
2266 pf_default_rule.timeout[pt->timeout] = pt->seconds;
2267 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
2268 wakeup(pf_purge_thread);
2273 case DIOCGETTIMEOUT: {
2274 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
2276 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
2280 pt->seconds = pf_default_rule.timeout[pt->timeout];
2284 case DIOCGETLIMIT: {
2285 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
2287 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
2291 pl->limit = pf_pool_limits[pl->index].limit;
2295 case DIOCSETLIMIT: {
2296 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
2299 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
2300 pf_pool_limits[pl->index].pp == NULL) {
2305 uma_zone_set_max(pf_pool_limits[pl->index].pp, pl->limit);
2307 if (pool_sethardlimit(pf_pool_limits[pl->index].pp,
2308 pl->limit, NULL, 0) != 0) {
2313 old_limit = pf_pool_limits[pl->index].limit;
2314 pf_pool_limits[pl->index].limit = pl->limit;
2315 pl->limit = old_limit;
2319 case DIOCSETDEBUG: {
2320 u_int32_t *level = (u_int32_t *)addr;
2322 pf_status.debug = *level;
2326 case DIOCCLRRULECTRS: {
2327 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
2328 struct pf_ruleset *ruleset = &pf_main_ruleset;
2329 struct pf_rule *rule;
2332 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
2333 rule->evaluations = 0;
2334 rule->packets[0] = rule->packets[1] = 0;
2335 rule->bytes[0] = rule->bytes[1] = 0;
2341 case DIOCGIFSPEED: {
2342 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
2343 struct pf_ifspeed ps;
2346 if (psp->ifname[0] != 0) {
2347 /* Can we completely trust user-land? */
2348 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
2349 ifp = ifunit(ps.ifname);
2351 psp->baudrate = ifp->if_baudrate;
2358 #endif /* __FreeBSD__ */
2361 case DIOCSTARTALTQ: {
2362 struct pf_altq *altq;
2364 /* enable all altq interfaces on active list */
2365 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2367 if (altq->qname[0] == 0 && (altq->local_flags &
2368 PFALTQ_FLAG_IF_REMOVED) == 0) {
2370 if (altq->qname[0] == 0) {
2372 error = pf_enable_altq(altq);
2378 pf_altq_running = 1;
2379 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
2383 case DIOCSTOPALTQ: {
2384 struct pf_altq *altq;
2386 /* disable all altq interfaces on active list */
2387 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2389 if (altq->qname[0] == 0 && (altq->local_flags &
2390 PFALTQ_FLAG_IF_REMOVED) == 0) {
2392 if (altq->qname[0] == 0) {
2394 error = pf_disable_altq(altq);
2400 pf_altq_running = 0;
2401 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
2406 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2407 struct pf_altq *altq, *a;
2409 if (pa->ticket != ticket_altqs_inactive) {
2413 altq = pool_get(&pf_altq_pl, PR_NOWAIT);
2418 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
2420 altq->local_flags = 0;
2424 * if this is for a queue, find the discipline and
2425 * copy the necessary fields
2427 if (altq->qname[0] != 0) {
2428 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2430 pool_put(&pf_altq_pl, altq);
2433 altq->altq_disc = NULL;
2434 TAILQ_FOREACH(a, pf_altqs_inactive, entries) {
2435 if (strncmp(a->ifname, altq->ifname,
2436 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2437 altq->altq_disc = a->altq_disc;
2446 if ((ifp = ifunit(altq->ifname)) == NULL) {
2447 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2451 error = altq_add(altq);
2457 pool_put(&pf_altq_pl, altq);
2461 TAILQ_INSERT_TAIL(pf_altqs_inactive, altq, entries);
2462 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2466 case DIOCGETALTQS: {
2467 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2468 struct pf_altq *altq;
2471 TAILQ_FOREACH(altq, pf_altqs_active, entries)
2473 pa->ticket = ticket_altqs_active;
2478 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2479 struct pf_altq *altq;
2482 if (pa->ticket != ticket_altqs_active) {
2487 altq = TAILQ_FIRST(pf_altqs_active);
2488 while ((altq != NULL) && (nr < pa->nr)) {
2489 altq = TAILQ_NEXT(altq, entries);
2496 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2500 case DIOCCHANGEALTQ:
2501 /* CHANGEALTQ not supported yet! */
2505 case DIOCGETQSTATS: {
2506 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2507 struct pf_altq *altq;
2511 if (pq->ticket != ticket_altqs_active) {
2515 nbytes = pq->nbytes;
2517 altq = TAILQ_FIRST(pf_altqs_active);
2518 while ((altq != NULL) && (nr < pq->nr)) {
2519 altq = TAILQ_NEXT(altq, entries);
2527 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2533 error = altq_getqstats(altq, pq->buf, &nbytes);
2538 pq->scheduler = altq->scheduler;
2539 pq->nbytes = nbytes;
2545 case DIOCBEGINADDRS: {
2546 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2548 pf_empty_pool(&pf_pabuf);
2549 pp->ticket = ++ticket_pabuf;
2554 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2556 if (pp->ticket != ticket_pabuf) {
2561 if (pp->af == AF_INET) {
2562 error = EAFNOSUPPORT;
2567 if (pp->af == AF_INET6) {
2568 error = EAFNOSUPPORT;
2572 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2573 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2574 pp->addr.addr.type != PF_ADDR_TABLE) {
2578 pa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2583 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2584 if (pa->ifname[0]) {
2585 pa->kif = pfi_kif_get(pa->ifname);
2586 if (pa->kif == NULL) {
2587 pool_put(&pf_pooladdr_pl, pa);
2591 pfi_kif_ref(pa->kif, PFI_KIF_REF_RULE);
2593 if (pfi_dynaddr_setup(&pa->addr, pp->af)) {
2594 pfi_dynaddr_remove(&pa->addr);
2595 pfi_kif_unref(pa->kif, PFI_KIF_REF_RULE);
2596 pool_put(&pf_pooladdr_pl, pa);
2600 TAILQ_INSERT_TAIL(&pf_pabuf, pa, entries);
2604 case DIOCGETADDRS: {
2605 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2608 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2609 pp->r_num, 0, 1, 0);
2614 TAILQ_FOREACH(pa, &pool->list, entries)
2620 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2623 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2624 pp->r_num, 0, 1, 1);
2629 pa = TAILQ_FIRST(&pool->list);
2630 while ((pa != NULL) && (nr < pp->nr)) {
2631 pa = TAILQ_NEXT(pa, entries);
2638 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2639 pfi_dynaddr_copyout(&pp->addr.addr);
2640 pf_tbladdr_copyout(&pp->addr.addr);
2641 pf_rtlabel_copyout(&pp->addr.addr);
2645 case DIOCCHANGEADDR: {
2646 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2647 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2648 struct pf_ruleset *ruleset;
2650 if (pca->action < PF_CHANGE_ADD_HEAD ||
2651 pca->action > PF_CHANGE_REMOVE) {
2655 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2656 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2657 pca->addr.addr.type != PF_ADDR_TABLE) {
2662 ruleset = pf_find_ruleset(pca->anchor);
2663 if (ruleset == NULL) {
2667 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2668 pca->r_num, pca->r_last, 1, 1);
2673 if (pca->action != PF_CHANGE_REMOVE) {
2674 newpa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2675 if (newpa == NULL) {
2679 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2681 if (pca->af == AF_INET) {
2682 pool_put(&pf_pooladdr_pl, newpa);
2683 error = EAFNOSUPPORT;
2688 if (pca->af == AF_INET6) {
2689 pool_put(&pf_pooladdr_pl, newpa);
2690 error = EAFNOSUPPORT;
2694 if (newpa->ifname[0]) {
2695 newpa->kif = pfi_kif_get(newpa->ifname);
2696 if (newpa->kif == NULL) {
2697 pool_put(&pf_pooladdr_pl, newpa);
2701 pfi_kif_ref(newpa->kif, PFI_KIF_REF_RULE);
2704 if (pfi_dynaddr_setup(&newpa->addr, pca->af) ||
2705 pf_tbladdr_setup(ruleset, &newpa->addr)) {
2706 pfi_dynaddr_remove(&newpa->addr);
2707 pfi_kif_unref(newpa->kif, PFI_KIF_REF_RULE);
2708 pool_put(&pf_pooladdr_pl, newpa);
2714 if (pca->action == PF_CHANGE_ADD_HEAD)
2715 oldpa = TAILQ_FIRST(&pool->list);
2716 else if (pca->action == PF_CHANGE_ADD_TAIL)
2717 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2721 oldpa = TAILQ_FIRST(&pool->list);
2722 while ((oldpa != NULL) && (i < pca->nr)) {
2723 oldpa = TAILQ_NEXT(oldpa, entries);
2726 if (oldpa == NULL) {
2732 if (pca->action == PF_CHANGE_REMOVE) {
2733 TAILQ_REMOVE(&pool->list, oldpa, entries);
2734 pfi_dynaddr_remove(&oldpa->addr);
2735 pf_tbladdr_remove(&oldpa->addr);
2736 pfi_kif_unref(oldpa->kif, PFI_KIF_REF_RULE);
2737 pool_put(&pf_pooladdr_pl, oldpa);
2740 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2741 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2742 pca->action == PF_CHANGE_ADD_BEFORE)
2743 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2745 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2749 pool->cur = TAILQ_FIRST(&pool->list);
2750 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
2755 case DIOCGETRULESETS: {
2756 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2757 struct pf_ruleset *ruleset;
2758 struct pf_anchor *anchor;
2760 pr->path[sizeof(pr->path) - 1] = 0;
2761 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2766 if (ruleset->anchor == NULL) {
2767 /* XXX kludge for pf_main_ruleset */
2768 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2769 if (anchor->parent == NULL)
2772 RB_FOREACH(anchor, pf_anchor_node,
2773 &ruleset->anchor->children)
2779 case DIOCGETRULESET: {
2780 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2781 struct pf_ruleset *ruleset;
2782 struct pf_anchor *anchor;
2785 pr->path[sizeof(pr->path) - 1] = 0;
2786 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2791 if (ruleset->anchor == NULL) {
2792 /* XXX kludge for pf_main_ruleset */
2793 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2794 if (anchor->parent == NULL && nr++ == pr->nr) {
2795 strlcpy(pr->name, anchor->name,
2800 RB_FOREACH(anchor, pf_anchor_node,
2801 &ruleset->anchor->children)
2802 if (nr++ == pr->nr) {
2803 strlcpy(pr->name, anchor->name,
2813 case DIOCRCLRTABLES: {
2814 struct pfioc_table *io = (struct pfioc_table *)addr;
2816 if (io->pfrio_esize != 0) {
2820 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2821 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2825 case DIOCRADDTABLES: {
2826 struct pfioc_table *io = (struct pfioc_table *)addr;
2828 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2832 error = pfr_add_tables(io->pfrio_buffer, io->pfrio_size,
2833 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2837 case DIOCRDELTABLES: {
2838 struct pfioc_table *io = (struct pfioc_table *)addr;
2840 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2844 error = pfr_del_tables(io->pfrio_buffer, io->pfrio_size,
2845 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2849 case DIOCRGETTABLES: {
2850 struct pfioc_table *io = (struct pfioc_table *)addr;
2852 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2856 error = pfr_get_tables(&io->pfrio_table, io->pfrio_buffer,
2857 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2861 case DIOCRGETTSTATS: {
2862 struct pfioc_table *io = (struct pfioc_table *)addr;
2864 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2868 error = pfr_get_tstats(&io->pfrio_table, io->pfrio_buffer,
2869 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2873 case DIOCRCLRTSTATS: {
2874 struct pfioc_table *io = (struct pfioc_table *)addr;
2876 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2880 error = pfr_clr_tstats(io->pfrio_buffer, io->pfrio_size,
2881 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2885 case DIOCRSETTFLAGS: {
2886 struct pfioc_table *io = (struct pfioc_table *)addr;
2888 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2892 error = pfr_set_tflags(io->pfrio_buffer, io->pfrio_size,
2893 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2894 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2898 case DIOCRCLRADDRS: {
2899 struct pfioc_table *io = (struct pfioc_table *)addr;
2901 if (io->pfrio_esize != 0) {
2905 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2906 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2910 case DIOCRADDADDRS: {
2911 struct pfioc_table *io = (struct pfioc_table *)addr;
2913 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2917 error = pfr_add_addrs(&io->pfrio_table, io->pfrio_buffer,
2918 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2919 PFR_FLAG_USERIOCTL);
2923 case DIOCRDELADDRS: {
2924 struct pfioc_table *io = (struct pfioc_table *)addr;
2926 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2930 error = pfr_del_addrs(&io->pfrio_table, io->pfrio_buffer,
2931 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2932 PFR_FLAG_USERIOCTL);
2936 case DIOCRSETADDRS: {
2937 struct pfioc_table *io = (struct pfioc_table *)addr;
2939 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2943 error = pfr_set_addrs(&io->pfrio_table, io->pfrio_buffer,
2944 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2945 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2946 PFR_FLAG_USERIOCTL, 0);
2950 case DIOCRGETADDRS: {
2951 struct pfioc_table *io = (struct pfioc_table *)addr;
2953 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2957 error = pfr_get_addrs(&io->pfrio_table, io->pfrio_buffer,
2958 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2962 case DIOCRGETASTATS: {
2963 struct pfioc_table *io = (struct pfioc_table *)addr;
2965 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2969 error = pfr_get_astats(&io->pfrio_table, io->pfrio_buffer,
2970 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2974 case DIOCRCLRASTATS: {
2975 struct pfioc_table *io = (struct pfioc_table *)addr;
2977 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2981 error = pfr_clr_astats(&io->pfrio_table, io->pfrio_buffer,
2982 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2983 PFR_FLAG_USERIOCTL);
2987 case DIOCRTSTADDRS: {
2988 struct pfioc_table *io = (struct pfioc_table *)addr;
2990 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2994 error = pfr_tst_addrs(&io->pfrio_table, io->pfrio_buffer,
2995 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2996 PFR_FLAG_USERIOCTL);
3000 case DIOCRINADEFINE: {
3001 struct pfioc_table *io = (struct pfioc_table *)addr;
3003 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
3007 error = pfr_ina_define(&io->pfrio_table, io->pfrio_buffer,
3008 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
3009 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
3014 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
3015 error = pf_osfp_add(io);
3020 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
3021 error = pf_osfp_get(io);
3026 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3027 struct pfioc_trans_e *ioe;
3028 struct pfr_table *table;
3031 if (io->esize != sizeof(*ioe)) {
3038 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3040 table = (struct pfr_table *)malloc(sizeof(*table),
3045 for (i = 0; i < io->size; i++) {
3047 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3050 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3052 free(table, M_TEMP);
3057 switch (ioe->rs_num) {
3059 case PF_RULESET_ALTQ:
3060 if (ioe->anchor[0]) {
3061 free(table, M_TEMP);
3066 if ((error = pf_begin_altq(&ioe->ticket))) {
3067 free(table, M_TEMP);
3073 case PF_RULESET_TABLE:
3074 bzero(table, sizeof(*table));
3075 strlcpy(table->pfrt_anchor, ioe->anchor,
3076 sizeof(table->pfrt_anchor));
3077 if ((error = pfr_ina_begin(table,
3078 &ioe->ticket, NULL, 0))) {
3079 free(table, M_TEMP);
3085 if ((error = pf_begin_rules(&ioe->ticket,
3086 ioe->rs_num, ioe->anchor))) {
3087 free(table, M_TEMP);
3094 PF_COPYOUT(ioe, io->array+i, sizeof(io->array[i]),
3098 if (copyout(ioe, io->array+i, sizeof(io->array[i]))) {
3100 free(table, M_TEMP);
3106 free(table, M_TEMP);
3111 case DIOCXROLLBACK: {
3112 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3113 struct pfioc_trans_e *ioe;
3114 struct pfr_table *table;
3117 if (io->esize != sizeof(*ioe)) {
3124 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3126 table = (struct pfr_table *)malloc(sizeof(*table),
3131 for (i = 0; i < io->size; i++) {
3133 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3136 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3138 free(table, M_TEMP);
3143 switch (ioe->rs_num) {
3145 case PF_RULESET_ALTQ:
3146 if (ioe->anchor[0]) {
3147 free(table, M_TEMP);
3152 if ((error = pf_rollback_altq(ioe->ticket))) {
3153 free(table, M_TEMP);
3155 goto fail; /* really bad */
3159 case PF_RULESET_TABLE:
3160 bzero(table, sizeof(*table));
3161 strlcpy(table->pfrt_anchor, ioe->anchor,
3162 sizeof(table->pfrt_anchor));
3163 if ((error = pfr_ina_rollback(table,
3164 ioe->ticket, NULL, 0))) {
3165 free(table, M_TEMP);
3167 goto fail; /* really bad */
3171 if ((error = pf_rollback_rules(ioe->ticket,
3172 ioe->rs_num, ioe->anchor))) {
3173 free(table, M_TEMP);
3175 goto fail; /* really bad */
3180 free(table, M_TEMP);
3186 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3187 struct pfioc_trans_e *ioe;
3188 struct pfr_table *table;
3189 struct pf_ruleset *rs;
3192 if (io->esize != sizeof(*ioe)) {
3199 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3201 table = (struct pfr_table *)malloc(sizeof(*table),
3206 /* first makes sure everything will succeed */
3207 for (i = 0; i < io->size; i++) {
3209 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3212 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3214 free(table, M_TEMP);
3219 switch (ioe->rs_num) {
3221 case PF_RULESET_ALTQ:
3222 if (ioe->anchor[0]) {
3223 free(table, M_TEMP);
3228 if (!altqs_inactive_open || ioe->ticket !=
3229 ticket_altqs_inactive) {
3230 free(table, M_TEMP);
3237 case PF_RULESET_TABLE:
3238 rs = pf_find_ruleset(ioe->anchor);
3239 if (rs == NULL || !rs->topen || ioe->ticket !=
3241 free(table, M_TEMP);
3248 if (ioe->rs_num < 0 || ioe->rs_num >=
3250 free(table, M_TEMP);
3255 rs = pf_find_ruleset(ioe->anchor);
3257 !rs->rules[ioe->rs_num].inactive.open ||
3258 rs->rules[ioe->rs_num].inactive.ticket !=
3260 free(table, M_TEMP);
3268 /* now do the commit - no errors should happen here */
3269 for (i = 0; i < io->size; i++) {
3271 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3274 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3276 free(table, M_TEMP);
3281 switch (ioe->rs_num) {
3283 case PF_RULESET_ALTQ:
3284 if ((error = pf_commit_altq(ioe->ticket))) {
3285 free(table, M_TEMP);
3287 goto fail; /* really bad */
3291 case PF_RULESET_TABLE:
3292 bzero(table, sizeof(*table));
3293 strlcpy(table->pfrt_anchor, ioe->anchor,
3294 sizeof(table->pfrt_anchor));
3295 if ((error = pfr_ina_commit(table, ioe->ticket,
3297 free(table, M_TEMP);
3299 goto fail; /* really bad */
3303 if ((error = pf_commit_rules(ioe->ticket,
3304 ioe->rs_num, ioe->anchor))) {
3305 free(table, M_TEMP);
3307 goto fail; /* really bad */
3312 free(table, M_TEMP);
3317 case DIOCGETSRCNODES: {
3318 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3319 struct pf_src_node *n, *p, *pstore;
3321 int space = psn->psn_len;
3324 RB_FOREACH(n, pf_src_tree, &tree_src_tracking)
3326 psn->psn_len = sizeof(struct pf_src_node) * nr;
3333 pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
3338 p = psn->psn_src_nodes;
3339 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3340 int secs = time_second, diff;
3342 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3345 bcopy(n, pstore, sizeof(*pstore));
3346 if (n->rule.ptr != NULL)
3347 pstore->rule.nr = n->rule.ptr->nr;
3348 pstore->creation = secs - pstore->creation;
3349 if (pstore->expire > secs)
3350 pstore->expire -= secs;
3354 /* adjust the connection rate estimate */
3355 diff = secs - n->conn_rate.last;
3356 if (diff >= n->conn_rate.seconds)
3357 pstore->conn_rate.count = 0;
3359 pstore->conn_rate.count -=
3360 n->conn_rate.count * diff /
3361 n->conn_rate.seconds;
3364 PF_COPYOUT(pstore, p, sizeof(*p), error);
3366 error = copyout(pstore, p, sizeof(*p));
3369 free(pstore, M_TEMP);
3375 psn->psn_len = sizeof(struct pf_src_node) * nr;
3377 free(pstore, M_TEMP);
3381 case DIOCCLRSRCNODES: {
3382 struct pf_src_node *n;
3383 struct pf_state *state;
3385 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3386 state->src_node = NULL;
3387 state->nat_src_node = NULL;
3389 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3393 pf_purge_expired_src_nodes(1);
3394 pf_status.src_nodes = 0;
3398 case DIOCKILLSRCNODES: {
3399 struct pf_src_node *sn;
3401 struct pfioc_src_node_kill *psnk = \
3402 (struct pfioc_src_node_kill *) addr;
3405 RB_FOREACH(sn, pf_src_tree, &tree_src_tracking) {
3406 if (PF_MATCHA(psnk->psnk_src.neg, \
3407 &psnk->psnk_src.addr.v.a.addr, \
3408 &psnk->psnk_src.addr.v.a.mask, \
3409 &sn->addr, sn->af) &&
3410 PF_MATCHA(psnk->psnk_dst.neg, \
3411 &psnk->psnk_dst.addr.v.a.addr, \
3412 &psnk->psnk_dst.addr.v.a.mask, \
3413 &sn->raddr, sn->af)) {
3414 /* Handle state to src_node linkage */
3415 if (sn->states != 0) {
3416 RB_FOREACH(s, pf_state_tree_id,
3418 if (s->src_node == sn)
3420 if (s->nat_src_node == sn)
3421 s->nat_src_node = NULL;
3431 pf_purge_expired_src_nodes(1);
3433 psnk->psnk_af = killed;
3437 case DIOCSETHOSTID: {
3438 u_int32_t *hostid = (u_int32_t *)addr;
3441 pf_status.hostid = arc4random();
3443 pf_status.hostid = *hostid;
3451 case DIOCIGETIFACES: {
3452 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3454 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3458 error = pfi_get_ifaces(io->pfiio_name, io->pfiio_buffer,
3463 case DIOCSETIFFLAG: {
3464 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3466 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3470 case DIOCCLRIFFLAG: {
3471 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3473 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3486 sx_xunlock(&pf_consistency_lock);
3488 sx_sunlock(&pf_consistency_lock);
3491 /* XXX: Lock order? */
3493 rw_exit_write(&pf_consistency_lock);
3495 rw_exit_read(&pf_consistency_lock);
3502 * XXX - Check for version missmatch!!!
3505 pf_clear_states(void)
3507 struct pf_state *state;
3509 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3510 state->timeout = PFTM_PURGE;
3512 /* don't send out individual delete messages */
3513 state->sync_flags = PFSTATE_NOSYNC;
3515 pf_unlink_state(state);
3520 * XXX This is called on module unload, we do not want to sync that over? */
3522 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
3527 pf_clear_tables(void)
3529 struct pfioc_table io;
3532 bzero(&io, sizeof(io));
3534 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3541 pf_clear_srcnodes(void)
3543 struct pf_src_node *n;
3544 struct pf_state *state;
3546 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3547 state->src_node = NULL;
3548 state->nat_src_node = NULL;
3550 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3556 * XXX - Check for version missmatch!!!
3560 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3569 pf_status.running = 0;
3571 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3573 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3576 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3578 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3579 break; /* XXX: rollback? */
3581 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3583 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3584 break; /* XXX: rollback? */
3586 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3588 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3589 break; /* XXX: rollback? */
3591 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3593 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3594 break; /* XXX: rollback? */
3597 /* XXX: these should always succeed here */
3598 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3599 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3600 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3601 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3602 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3604 if ((error = pf_clear_tables()) != 0)
3608 if ((error = pf_begin_altq(&t[0])) != 0) {
3609 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3612 pf_commit_altq(t[0]);
3617 pf_clear_srcnodes();
3619 /* status does not use malloced mem so no need to cleanup */
3620 /* fingerprints and interfaces have thier own cleanup code */
3627 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3631 * XXX Wed Jul 9 22:03:16 2003 UTC
3632 * OpenBSD has changed its byte ordering convention on ip_len/ip_off
3633 * in network stack. OpenBSD's network stack have converted
3634 * ip_len/ip_off to host byte order frist as FreeBSD.
3635 * Now this is not true anymore , so we should convert back to network
3638 struct ip *h = NULL;
3641 if ((*m)->m_pkthdr.len >= (int)sizeof(struct ip)) {
3642 /* if m_pkthdr.len is less than ip header, pf will handle. */
3643 h = mtod(*m, struct ip *);
3647 chk = pf_test(PF_IN, ifp, m, NULL, inp);
3653 /* pf_test can change ip header location */
3654 h = mtod(*m, struct ip *);
3662 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3666 * XXX Wed Jul 9 22:03:16 2003 UTC
3667 * OpenBSD has changed its byte ordering convention on ip_len/ip_off
3668 * in network stack. OpenBSD's network stack have converted
3669 * ip_len/ip_off to host byte order frist as FreeBSD.
3670 * Now this is not true anymore , so we should convert back to network
3673 struct ip *h = NULL;
3676 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3677 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3678 in_delayed_cksum(*m);
3679 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3681 if ((*m)->m_pkthdr.len >= (int)sizeof(*h)) {
3682 /* if m_pkthdr.len is less than ip header, pf will handle. */
3683 h = mtod(*m, struct ip *);
3687 chk = pf_test(PF_OUT, ifp, m, NULL, inp);
3693 /* pf_test can change ip header location */
3694 h = mtod(*m, struct ip *);
3703 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3707 * IPv6 is not affected by ip_len/ip_off byte order changes.
3712 * In case of loopback traffic IPv6 uses the real interface in
3713 * order to support scoped addresses. In order to support stateful
3714 * filtering we have change this to lo0 as it is the case in IPv4.
3716 chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? &loif[0] : ifp, m,
3726 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3730 * IPv6 does not affected ip_len/ip_off byte order changes.
3734 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3735 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3736 in_delayed_cksum(*m);
3737 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3739 chk = pf_test6(PF_OUT, ifp, m, NULL, inp);
3751 struct pfil_head *pfh_inet;
3753 struct pfil_head *pfh_inet6;
3756 PF_ASSERT(MA_NOTOWNED);
3761 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3762 if (pfh_inet == NULL)
3763 return (ESRCH); /* XXX */
3764 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3765 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3767 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3768 if (pfh_inet6 == NULL) {
3769 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3771 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3773 return (ESRCH); /* XXX */
3775 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3776 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3786 struct pfil_head *pfh_inet;
3788 struct pfil_head *pfh_inet6;
3791 PF_ASSERT(MA_NOTOWNED);
3793 if (pf_pfil_hooked == 0)
3796 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3797 if (pfh_inet == NULL)
3798 return (ESRCH); /* XXX */
3799 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3801 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3804 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3805 if (pfh_inet6 == NULL)
3806 return (ESRCH); /* XXX */
3807 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3809 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3822 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3823 if (pfattach() < 0) {
3824 destroy_dev(pf_dev);
3837 pf_status.running = 0;
3839 error = dehook_pf();
3842 * Should not happen!
3843 * XXX Due to error code ESRCH, kldunload will show
3844 * a message like 'No such process'.
3846 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3852 while (pf_end_threads < 2) {
3853 wakeup_one(pf_purge_thread);
3854 msleep(pf_purge_thread, &pf_task_mtx, 0, "pftmo", hz);
3861 destroy_dev(pf_dev);
3867 pf_modevent(module_t mod, int type, void *data)
3877 error = pf_unload();
3886 static moduledata_t pf_mod = {
3892 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST);
3893 MODULE_VERSION(pf, PF_MODVER);
3894 #endif /* __FreeBSD__ */