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.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 #include "opt_inet6.h"
48 #define NBPFILTER DEV_BPF
54 #define NPFLOG DEV_PFLOG
60 #define NPFSYNC DEV_PFSYNC
71 #include <sys/param.h>
72 #include <sys/systm.h>
74 #include <sys/filio.h>
75 #include <sys/fcntl.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/kernel.h>
80 #include <sys/malloc.h>
82 #include <sys/module.h>
85 #include <sys/sysctl.h>
86 #include <sys/vimage.h>
88 #include <sys/timeout.h>
92 #include <sys/malloc.h>
93 #include <sys/kthread.h>
95 #include <sys/rwlock.h>
96 #include <uvm/uvm_extern.h>
100 #include <net/if_types.h>
101 #include <net/route.h>
103 #include <net/vnet.h>
106 #include <netinet/in.h>
107 #include <netinet/in_var.h>
108 #include <netinet/in_systm.h>
109 #include <netinet/ip.h>
110 #include <netinet/ip_var.h>
111 #include <netinet/ip_icmp.h>
116 #include <dev/rndvar.h>
117 #include <crypto/md5.h>
119 #include <net/pfvar.h>
122 #include <net/if_pfsync.h>
123 #endif /* NPFSYNC > 0 */
125 #include <net/if_pflog.h>
128 #include <netinet/ip6.h>
129 #include <netinet/in_pcb.h>
133 #include <altq/altq.h>
137 #include <sys/limits.h>
138 #include <sys/lock.h>
139 #include <sys/mutex.h>
140 #include <net/pfil.h>
141 #endif /* __FreeBSD__ */
144 void init_zone_var(void);
145 void cleanup_pf_zone(void);
149 void pf_thread_create(void *);
150 int pfopen(dev_t, int, int, struct proc *);
151 int pfclose(dev_t, int, int, struct proc *);
153 struct pf_pool *pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
154 u_int8_t, u_int8_t, u_int8_t);
156 void pf_mv_pool(struct pf_palist *, struct pf_palist *);
157 void pf_empty_pool(struct pf_palist *);
159 int pfioctl(struct cdev *, u_long, caddr_t, int, struct thread *);
161 int pfioctl(struct cdev *, u_long, caddr_t, int, struct proc *);
164 int pf_begin_altq(u_int32_t *);
165 int pf_rollback_altq(u_int32_t);
166 int pf_commit_altq(u_int32_t);
167 int pf_enable_altq(struct pf_altq *);
168 int pf_disable_altq(struct pf_altq *);
170 int pf_begin_rules(u_int32_t *, int, const char *);
171 int pf_rollback_rules(u_int32_t, int, char *);
172 int pf_setup_pfsync_matching(struct pf_ruleset *);
173 void pf_hash_rule(MD5_CTX *, struct pf_rule *);
174 void pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
175 int pf_commit_rules(u_int32_t, int, char *);
177 struct pf_rule pf_default_rule;
179 struct sx pf_consistency_lock;
180 SX_SYSINIT(pf_consistency_lock, &pf_consistency_lock, "pf_statetbl_lock");
182 struct rwlock pf_consistency_lock = RWLOCK_INITIALIZER;
185 static int pf_altq_running;
188 #define TAGID_MAX 50000
189 TAILQ_HEAD(pf_tags, pf_tagname) pf_tags = TAILQ_HEAD_INITIALIZER(pf_tags),
190 pf_qids = TAILQ_HEAD_INITIALIZER(pf_qids);
192 #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
193 #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
195 u_int16_t tagname2tag(struct pf_tags *, char *);
196 void tag2tagname(struct pf_tags *, u_int16_t, char *);
197 void tag_unref(struct pf_tags *, u_int16_t);
198 int pf_rtlabel_add(struct pf_addr_wrap *);
199 void pf_rtlabel_remove(struct pf_addr_wrap *);
200 void pf_rtlabel_copyout(struct pf_addr_wrap *);
202 #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
206 static struct cdev *pf_dev;
209 * XXX - These are new and need to be checked when moveing to a new version
211 static void pf_clear_states(void);
212 static int pf_clear_tables(void);
213 static void pf_clear_srcnodes(void);
215 * XXX - These are new and need to be checked when moveing to a new version
219 * Wrapper functions for pfil(9) hooks
221 static int pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp,
222 int dir, struct inpcb *inp);
223 static int pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp,
224 int dir, struct inpcb *inp);
226 static int pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp,
227 int dir, struct inpcb *inp);
228 static int pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp,
229 int dir, struct inpcb *inp);
232 static int hook_pf(void);
233 static int dehook_pf(void);
234 static int shutdown_pf(void);
235 static int pf_load(void);
236 static int pf_unload(void);
238 static struct cdevsw pf_cdevsw = {
241 .d_version = D_VERSION,
244 static volatile int pf_pfil_hooked = 0;
245 int pf_end_threads = 0;
246 struct mtx pf_task_mtx;
247 pflog_packet_t *pflog_packet_ptr = NULL;
249 int debug_pfugidhack = 0;
250 SYSCTL_INT(_debug, OID_AUTO, pfugidhack, CTLFLAG_RW, &debug_pfugidhack, 0,
251 "Enable/disable pf user/group rules mpsafe hack");
256 mtx_init(&pf_task_mtx, "pf task mtx", NULL, MTX_DEF);
260 destroy_pf_mutex(void)
262 mtx_destroy(&pf_task_mtx);
268 pf_src_tree_pl = pf_rule_pl = NULL;
269 pf_state_pl = pf_altq_pl = pf_pooladdr_pl = NULL;
270 pf_frent_pl = pf_frag_pl = pf_cache_pl = pf_cent_pl = NULL;
271 pf_state_scrub_pl = NULL;
272 pfr_ktable_pl = pfr_kentry_pl = NULL;
276 cleanup_pf_zone(void)
278 UMA_DESTROY(pf_src_tree_pl);
279 UMA_DESTROY(pf_rule_pl);
280 UMA_DESTROY(pf_state_pl);
281 UMA_DESTROY(pf_altq_pl);
282 UMA_DESTROY(pf_pooladdr_pl);
283 UMA_DESTROY(pf_frent_pl);
284 UMA_DESTROY(pf_frag_pl);
285 UMA_DESTROY(pf_cache_pl);
286 UMA_DESTROY(pf_cent_pl);
287 UMA_DESTROY(pfr_ktable_pl);
288 UMA_DESTROY(pfr_kentry_pl2);
289 UMA_DESTROY(pfr_kentry_pl);
290 UMA_DESTROY(pf_state_scrub_pl);
291 UMA_DESTROY(pfi_addr_pl);
297 u_int32_t *my_timeout = pf_default_rule.timeout;
301 UMA_CREATE(pf_src_tree_pl,struct pf_src_node, "pfsrctrpl");
302 UMA_CREATE(pf_rule_pl, struct pf_rule, "pfrulepl");
303 UMA_CREATE(pf_state_pl, struct pf_state, "pfstatepl");
304 UMA_CREATE(pf_altq_pl, struct pf_altq, "pfaltqpl");
305 UMA_CREATE(pf_pooladdr_pl, struct pf_pooladdr, "pfpooladdrpl");
306 UMA_CREATE(pfr_ktable_pl, struct pfr_ktable, "pfrktable");
307 UMA_CREATE(pfr_kentry_pl, struct pfr_kentry, "pfrkentry");
308 UMA_CREATE(pfr_kentry_pl2, struct pfr_kentry, "pfrkentry2");
309 UMA_CREATE(pf_frent_pl, struct pf_frent, "pffrent");
310 UMA_CREATE(pf_frag_pl, struct pf_fragment, "pffrag");
311 UMA_CREATE(pf_cache_pl, struct pf_fragment, "pffrcache");
312 UMA_CREATE(pf_cent_pl, struct pf_frcache, "pffrcent");
313 UMA_CREATE(pf_state_scrub_pl, struct pf_state_scrub,
315 UMA_CREATE(pfi_addr_pl, struct pfi_dynaddr, "pfiaddrpl");
324 if ( (error = pf_osfp_initialize()) ) {
330 pf_pool_limits[PF_LIMIT_STATES].pp = pf_state_pl;
331 pf_pool_limits[PF_LIMIT_STATES].limit = PFSTATE_HIWAT;
332 pf_pool_limits[PF_LIMIT_SRC_NODES].pp = pf_src_tree_pl;
333 pf_pool_limits[PF_LIMIT_SRC_NODES].limit = PFSNODE_HIWAT;
334 pf_pool_limits[PF_LIMIT_FRAGS].pp = pf_frent_pl;
335 pf_pool_limits[PF_LIMIT_FRAGS].limit = PFFRAG_FRENT_HIWAT;
336 pf_pool_limits[PF_LIMIT_TABLES].pp = pfr_ktable_pl;
337 pf_pool_limits[PF_LIMIT_TABLES].limit = PFR_KTABLE_HIWAT;
338 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].pp = pfr_kentry_pl;
339 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit = PFR_KENTRY_HIWAT;
340 uma_zone_set_max(pf_pool_limits[PF_LIMIT_STATES].pp,
341 pf_pool_limits[PF_LIMIT_STATES].limit);
343 RB_INIT(&tree_src_tracking);
344 RB_INIT(&pf_anchors);
345 pf_init_ruleset(&pf_main_ruleset);
346 TAILQ_INIT(&pf_altqs[0]);
347 TAILQ_INIT(&pf_altqs[1]);
348 TAILQ_INIT(&pf_pabuf);
349 pf_altqs_active = &pf_altqs[0];
350 pf_altqs_inactive = &pf_altqs[1];
351 TAILQ_INIT(&state_list);
353 /* default rule should never be garbage collected */
354 pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
355 pf_default_rule.action = PF_PASS;
356 pf_default_rule.nr = -1;
357 pf_default_rule.rtableid = -1;
359 /* initialize default timeouts */
360 my_timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
361 my_timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
362 my_timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
363 my_timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
364 my_timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
365 my_timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
366 my_timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
367 my_timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
368 my_timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
369 my_timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
370 my_timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
371 my_timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
372 my_timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
373 my_timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
374 my_timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
375 my_timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
376 my_timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
377 my_timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
378 my_timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
379 my_timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
382 bzero(&pf_status, sizeof(pf_status));
383 pf_status.debug = PF_DEBUG_URGENT;
387 /* XXX do our best to avoid a conflict */
388 pf_status.hostid = arc4random();
390 if (kproc_create(pf_purge_thread, NULL, NULL, 0, 0, "pfpurge"))
395 #else /* !__FreeBSD__ */
399 u_int32_t *timeout = pf_default_rule.timeout;
401 pool_init(&pf_rule_pl, sizeof(struct pf_rule), 0, 0, 0, "pfrulepl",
402 &pool_allocator_nointr);
403 pool_init(&pf_src_tree_pl, sizeof(struct pf_src_node), 0, 0, 0,
405 pool_init(&pf_state_pl, sizeof(struct pf_state), 0, 0, 0, "pfstatepl",
407 pool_init(&pf_altq_pl, sizeof(struct pf_altq), 0, 0, 0, "pfaltqpl",
408 &pool_allocator_nointr);
409 pool_init(&pf_pooladdr_pl, sizeof(struct pf_pooladdr), 0, 0, 0,
410 "pfpooladdrpl", &pool_allocator_nointr);
413 pf_osfp_initialize();
415 pool_sethardlimit(pf_pool_limits[PF_LIMIT_STATES].pp,
416 pf_pool_limits[PF_LIMIT_STATES].limit, NULL, 0);
418 if (ctob(physmem) <= 100*1024*1024)
419 pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit =
420 PFR_KENTRY_HIWAT_SMALL;
422 RB_INIT(&tree_src_tracking);
423 RB_INIT(&pf_anchors);
424 pf_init_ruleset(&pf_main_ruleset);
425 TAILQ_INIT(&pf_altqs[0]);
426 TAILQ_INIT(&pf_altqs[1]);
427 TAILQ_INIT(&pf_pabuf);
428 pf_altqs_active = &pf_altqs[0];
429 pf_altqs_inactive = &pf_altqs[1];
430 TAILQ_INIT(&state_list);
432 /* default rule should never be garbage collected */
433 pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
434 pf_default_rule.action = PF_PASS;
435 pf_default_rule.nr = -1;
436 pf_default_rule.rtableid = -1;
438 /* initialize default timeouts */
439 timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
440 timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
441 timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
442 timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
443 timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
444 timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
445 timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
446 timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
447 timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
448 timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
449 timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
450 timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
451 timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
452 timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
453 timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
454 timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
455 timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
456 timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
457 timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
458 timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
461 bzero(&pf_status, sizeof(pf_status));
462 pf_status.debug = PF_DEBUG_URGENT;
464 /* XXX do our best to avoid a conflict */
465 pf_status.hostid = arc4random();
467 /* require process context to purge states, so perform in a thread */
468 kproc_create_deferred(pf_thread_create, NULL);
472 pf_thread_create(void *v)
474 if (kproc_create(pf_purge_thread, NULL, NULL, "pfpurge"))
475 panic("pfpurge thread");
479 pfopen(struct cdev *dev, int flags, int fmt, struct proc *p)
481 if (dev2unit(dev) >= 1)
487 pfclose(struct cdev *dev, int flags, int fmt, struct proc *p)
489 if (dev2unit(dev) >= 1)
493 #endif /* __FreeBSD__ */
496 pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
497 u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
498 u_int8_t check_ticket)
500 struct pf_ruleset *ruleset;
501 struct pf_rule *rule;
504 ruleset = pf_find_ruleset(anchor);
507 rs_num = pf_get_ruleset_number(rule_action);
508 if (rs_num >= PF_RULESET_MAX)
511 if (check_ticket && ticket !=
512 ruleset->rules[rs_num].active.ticket)
515 rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
518 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
520 if (check_ticket && ticket !=
521 ruleset->rules[rs_num].inactive.ticket)
524 rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
527 rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
530 while ((rule != NULL) && (rule->nr != rule_number))
531 rule = TAILQ_NEXT(rule, entries);
536 return (&rule->rpool);
540 pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
542 struct pf_pooladdr *mv_pool_pa;
544 while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
545 TAILQ_REMOVE(poola, mv_pool_pa, entries);
546 TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
551 pf_empty_pool(struct pf_palist *poola)
553 struct pf_pooladdr *empty_pool_pa;
555 while ((empty_pool_pa = TAILQ_FIRST(poola)) != NULL) {
556 pfi_dynaddr_remove(&empty_pool_pa->addr);
557 pf_tbladdr_remove(&empty_pool_pa->addr);
558 pfi_kif_unref(empty_pool_pa->kif, PFI_KIF_REF_RULE);
559 TAILQ_REMOVE(poola, empty_pool_pa, entries);
560 pool_put(&pf_pooladdr_pl, empty_pool_pa);
565 pf_rm_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
567 if (rulequeue != NULL) {
568 if (rule->states <= 0) {
570 * XXX - we need to remove the table *before* detaching
571 * the rule to make sure the table code does not delete
572 * the anchor under our feet.
574 pf_tbladdr_remove(&rule->src.addr);
575 pf_tbladdr_remove(&rule->dst.addr);
576 if (rule->overload_tbl)
577 pfr_detach_table(rule->overload_tbl);
579 TAILQ_REMOVE(rulequeue, rule, entries);
580 rule->entries.tqe_prev = NULL;
584 if (rule->states > 0 || rule->src_nodes > 0 ||
585 rule->entries.tqe_prev != NULL)
587 pf_tag_unref(rule->tag);
588 pf_tag_unref(rule->match_tag);
590 if (rule->pqid != rule->qid)
591 pf_qid_unref(rule->pqid);
592 pf_qid_unref(rule->qid);
594 pf_rtlabel_remove(&rule->src.addr);
595 pf_rtlabel_remove(&rule->dst.addr);
596 pfi_dynaddr_remove(&rule->src.addr);
597 pfi_dynaddr_remove(&rule->dst.addr);
598 if (rulequeue == NULL) {
599 pf_tbladdr_remove(&rule->src.addr);
600 pf_tbladdr_remove(&rule->dst.addr);
601 if (rule->overload_tbl)
602 pfr_detach_table(rule->overload_tbl);
604 pfi_kif_unref(rule->kif, PFI_KIF_REF_RULE);
605 pf_anchor_remove(rule);
606 pf_empty_pool(&rule->rpool.list);
607 pool_put(&pf_rule_pl, rule);
611 tagname2tag(struct pf_tags *head, char *tagname)
613 struct pf_tagname *tag, *p = NULL;
614 u_int16_t new_tagid = 1;
616 TAILQ_FOREACH(tag, head, entries)
617 if (strcmp(tagname, tag->name) == 0) {
623 * to avoid fragmentation, we do a linear search from the beginning
624 * and take the first free slot we find. if there is none or the list
625 * is empty, append a new entry at the end.
629 if (!TAILQ_EMPTY(head))
630 for (p = TAILQ_FIRST(head); p != NULL &&
631 p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
632 new_tagid = p->tag + 1;
634 if (new_tagid > TAGID_MAX)
637 /* allocate and fill new struct pf_tagname */
638 tag = (struct pf_tagname *)malloc(sizeof(struct pf_tagname),
642 bzero(tag, sizeof(struct pf_tagname));
643 strlcpy(tag->name, tagname, sizeof(tag->name));
644 tag->tag = new_tagid;
647 if (p != NULL) /* insert new entry before p */
648 TAILQ_INSERT_BEFORE(p, tag, entries);
649 else /* either list empty or no free slot in between */
650 TAILQ_INSERT_TAIL(head, tag, entries);
656 tag2tagname(struct pf_tags *head, u_int16_t tagid, char *p)
658 struct pf_tagname *tag;
660 TAILQ_FOREACH(tag, head, entries)
661 if (tag->tag == tagid) {
662 strlcpy(p, tag->name, PF_TAG_NAME_SIZE);
668 tag_unref(struct pf_tags *head, u_int16_t tag)
670 struct pf_tagname *p, *next;
675 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
676 next = TAILQ_NEXT(p, entries);
679 TAILQ_REMOVE(head, p, entries);
688 pf_tagname2tag(char *tagname)
690 return (tagname2tag(&pf_tags, tagname));
694 pf_tag2tagname(u_int16_t tagid, char *p)
696 tag2tagname(&pf_tags, tagid, p);
700 pf_tag_ref(u_int16_t tag)
702 struct pf_tagname *t;
704 TAILQ_FOREACH(t, &pf_tags, entries)
712 pf_tag_unref(u_int16_t tag)
714 tag_unref(&pf_tags, tag);
718 pf_rtlabel_add(struct pf_addr_wrap *a)
721 /* XXX_IMPORT: later */
724 if (a->type == PF_ADDR_RTLABEL &&
725 (a->v.rtlabel = rtlabel_name2id(a->v.rtlabelname)) == 0)
732 pf_rtlabel_remove(struct pf_addr_wrap *a)
735 /* XXX_IMPORT: later */
737 if (a->type == PF_ADDR_RTLABEL)
738 rtlabel_unref(a->v.rtlabel);
743 pf_rtlabel_copyout(struct pf_addr_wrap *a)
746 /* XXX_IMPORT: later */
747 if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel)
748 strlcpy(a->v.rtlabelname, "?", sizeof(a->v.rtlabelname));
752 if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel) {
753 if ((name = rtlabel_id2name(a->v.rtlabel)) == NULL)
754 strlcpy(a->v.rtlabelname, "?",
755 sizeof(a->v.rtlabelname));
757 strlcpy(a->v.rtlabelname, name,
758 sizeof(a->v.rtlabelname));
765 pf_qname2qid(char *qname)
767 return ((u_int32_t)tagname2tag(&pf_qids, qname));
771 pf_qid2qname(u_int32_t qid, char *p)
773 tag2tagname(&pf_qids, (u_int16_t)qid, p);
777 pf_qid_unref(u_int32_t qid)
779 tag_unref(&pf_qids, (u_int16_t)qid);
783 pf_begin_altq(u_int32_t *ticket)
785 struct pf_altq *altq;
788 /* Purge the old altq list */
789 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
790 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
792 if (altq->qname[0] == 0 &&
793 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
795 if (altq->qname[0] == 0) {
797 /* detach and destroy the discipline */
798 error = altq_remove(altq);
800 pf_qid_unref(altq->qid);
801 pool_put(&pf_altq_pl, altq);
805 *ticket = ++ticket_altqs_inactive;
806 altqs_inactive_open = 1;
811 pf_rollback_altq(u_int32_t ticket)
813 struct pf_altq *altq;
816 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
818 /* Purge the old altq list */
819 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
820 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
822 if (altq->qname[0] == 0 &&
823 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
825 if (altq->qname[0] == 0) {
827 /* detach and destroy the discipline */
828 error = altq_remove(altq);
830 pf_qid_unref(altq->qid);
831 pool_put(&pf_altq_pl, altq);
833 altqs_inactive_open = 0;
838 pf_commit_altq(u_int32_t ticket)
840 struct pf_altqqueue *old_altqs;
841 struct pf_altq *altq;
842 int s, err, error = 0;
844 if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
847 /* swap altqs, keep the old. */
849 old_altqs = pf_altqs_active;
850 pf_altqs_active = pf_altqs_inactive;
851 pf_altqs_inactive = old_altqs;
852 ticket_altqs_active = ticket_altqs_inactive;
854 /* Attach new disciplines */
855 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
857 if (altq->qname[0] == 0 &&
858 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
860 if (altq->qname[0] == 0) {
862 /* attach the discipline */
863 error = altq_pfattach(altq);
864 if (error == 0 && pf_altq_running)
865 error = pf_enable_altq(altq);
873 /* Purge the old altq list */
874 while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
875 TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
877 if (altq->qname[0] == 0 &&
878 (altq->local_flags & PFALTQ_FLAG_IF_REMOVED) == 0) {
880 if (altq->qname[0] == 0) {
882 /* detach and destroy the discipline */
884 error = pf_disable_altq(altq);
885 err = altq_pfdetach(altq);
886 if (err != 0 && error == 0)
888 err = altq_remove(altq);
889 if (err != 0 && error == 0)
892 pf_qid_unref(altq->qid);
893 pool_put(&pf_altq_pl, altq);
897 altqs_inactive_open = 0;
902 pf_enable_altq(struct pf_altq *altq)
905 struct tb_profile tb;
908 if ((ifp = ifunit(altq->ifname)) == NULL)
911 if (ifp->if_snd.altq_type != ALTQT_NONE)
912 error = altq_enable(&ifp->if_snd);
914 /* set tokenbucket regulator */
915 if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
916 tb.rate = altq->ifbandwidth;
917 tb.depth = altq->tbrsize;
922 error = tbr_set(&ifp->if_snd, &tb);
933 pf_disable_altq(struct pf_altq *altq)
936 struct tb_profile tb;
939 if ((ifp = ifunit(altq->ifname)) == NULL)
943 * when the discipline is no longer referenced, it was overridden
944 * by a new one. if so, just return.
946 if (altq->altq_disc != ifp->if_snd.altq_disc)
949 error = altq_disable(&ifp->if_snd);
952 /* clear tokenbucket regulator */
958 error = tbr_set(&ifp->if_snd, &tb);
970 pf_altq_ifnet_event(struct ifnet *ifp, int remove)
973 struct pf_altq *a1, *a2, *a3;
977 /* Interrupt userland queue modifications */
978 if (altqs_inactive_open)
979 pf_rollback_altq(ticket_altqs_inactive);
981 /* Start new altq ruleset */
982 if (pf_begin_altq(&ticket))
985 /* Copy the current active set */
986 TAILQ_FOREACH(a1, pf_altqs_active, entries) {
987 a2 = pool_get(&pf_altq_pl, PR_NOWAIT);
992 bcopy(a1, a2, sizeof(struct pf_altq));
994 if (a2->qname[0] != 0) {
995 if ((a2->qid = pf_qname2qid(a2->qname)) == 0) {
997 pool_put(&pf_altq_pl, a2);
1000 a2->altq_disc = NULL;
1001 TAILQ_FOREACH(a3, pf_altqs_inactive, entries) {
1002 if (strncmp(a3->ifname, a2->ifname,
1003 IFNAMSIZ) == 0 && a3->qname[0] == 0) {
1004 a2->altq_disc = a3->altq_disc;
1009 /* Deactivate the interface in question */
1010 a2->local_flags &= ~PFALTQ_FLAG_IF_REMOVED;
1011 if ((ifp1 = ifunit(a2->ifname)) == NULL ||
1012 (remove && ifp1 == ifp)) {
1013 a2->local_flags |= PFALTQ_FLAG_IF_REMOVED;
1016 error = altq_add(a2);
1019 if (ticket != ticket_altqs_inactive)
1023 pool_put(&pf_altq_pl, a2);
1028 TAILQ_INSERT_TAIL(pf_altqs_inactive, a2, entries);
1032 pf_rollback_altq(ticket);
1034 pf_commit_altq(ticket);
1040 pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
1042 struct pf_ruleset *rs;
1043 struct pf_rule *rule;
1045 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1047 rs = pf_find_or_create_ruleset(anchor);
1050 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1051 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
1052 rs->rules[rs_num].inactive.rcount--;
1054 *ticket = ++rs->rules[rs_num].inactive.ticket;
1055 rs->rules[rs_num].inactive.open = 1;
1060 pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
1062 struct pf_ruleset *rs;
1063 struct pf_rule *rule;
1065 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1067 rs = pf_find_ruleset(anchor);
1068 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1069 rs->rules[rs_num].inactive.ticket != ticket)
1071 while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
1072 pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
1073 rs->rules[rs_num].inactive.rcount--;
1075 rs->rules[rs_num].inactive.open = 0;
1079 #define PF_MD5_UPD(st, elm) \
1080 MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
1082 #define PF_MD5_UPD_STR(st, elm) \
1083 MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
1085 #define PF_MD5_UPD_HTONL(st, elm, stor) do { \
1086 (stor) = htonl((st)->elm); \
1087 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
1090 #define PF_MD5_UPD_HTONS(st, elm, stor) do { \
1091 (stor) = htons((st)->elm); \
1092 MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
1096 pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
1098 PF_MD5_UPD(pfr, addr.type);
1099 switch (pfr->addr.type) {
1100 case PF_ADDR_DYNIFTL:
1101 PF_MD5_UPD(pfr, addr.v.ifname);
1102 PF_MD5_UPD(pfr, addr.iflags);
1105 PF_MD5_UPD(pfr, addr.v.tblname);
1107 case PF_ADDR_ADDRMASK:
1108 /* XXX ignore af? */
1109 PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
1110 PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
1112 case PF_ADDR_RTLABEL:
1113 PF_MD5_UPD(pfr, addr.v.rtlabelname);
1117 PF_MD5_UPD(pfr, port[0]);
1118 PF_MD5_UPD(pfr, port[1]);
1119 PF_MD5_UPD(pfr, neg);
1120 PF_MD5_UPD(pfr, port_op);
1124 pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
1129 pf_hash_rule_addr(ctx, &rule->src);
1130 pf_hash_rule_addr(ctx, &rule->dst);
1131 PF_MD5_UPD_STR(rule, label);
1132 PF_MD5_UPD_STR(rule, ifname);
1133 PF_MD5_UPD_STR(rule, match_tagname);
1134 PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
1135 PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
1136 PF_MD5_UPD_HTONL(rule, prob, y);
1137 PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
1138 PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
1139 PF_MD5_UPD(rule, uid.op);
1140 PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
1141 PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
1142 PF_MD5_UPD(rule, gid.op);
1143 PF_MD5_UPD_HTONL(rule, rule_flag, y);
1144 PF_MD5_UPD(rule, action);
1145 PF_MD5_UPD(rule, direction);
1146 PF_MD5_UPD(rule, af);
1147 PF_MD5_UPD(rule, quick);
1148 PF_MD5_UPD(rule, ifnot);
1149 PF_MD5_UPD(rule, match_tag_not);
1150 PF_MD5_UPD(rule, natpass);
1151 PF_MD5_UPD(rule, keep_state);
1152 PF_MD5_UPD(rule, proto);
1153 PF_MD5_UPD(rule, type);
1154 PF_MD5_UPD(rule, code);
1155 PF_MD5_UPD(rule, flags);
1156 PF_MD5_UPD(rule, flagset);
1157 PF_MD5_UPD(rule, allow_opts);
1158 PF_MD5_UPD(rule, rt);
1159 PF_MD5_UPD(rule, tos);
1163 pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
1165 struct pf_ruleset *rs;
1166 struct pf_rule *rule, **old_array;
1167 struct pf_rulequeue *old_rules;
1169 u_int32_t old_rcount;
1171 if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
1173 rs = pf_find_ruleset(anchor);
1174 if (rs == NULL || !rs->rules[rs_num].inactive.open ||
1175 ticket != rs->rules[rs_num].inactive.ticket)
1178 /* Calculate checksum for the main ruleset */
1179 if (rs == &pf_main_ruleset) {
1180 error = pf_setup_pfsync_matching(rs);
1185 /* Swap rules, keep the old. */
1187 old_rules = rs->rules[rs_num].active.ptr;
1188 old_rcount = rs->rules[rs_num].active.rcount;
1189 old_array = rs->rules[rs_num].active.ptr_array;
1191 rs->rules[rs_num].active.ptr =
1192 rs->rules[rs_num].inactive.ptr;
1193 rs->rules[rs_num].active.ptr_array =
1194 rs->rules[rs_num].inactive.ptr_array;
1195 rs->rules[rs_num].active.rcount =
1196 rs->rules[rs_num].inactive.rcount;
1197 rs->rules[rs_num].inactive.ptr = old_rules;
1198 rs->rules[rs_num].inactive.ptr_array = old_array;
1199 rs->rules[rs_num].inactive.rcount = old_rcount;
1201 rs->rules[rs_num].active.ticket =
1202 rs->rules[rs_num].inactive.ticket;
1203 pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
1206 /* Purge the old rule list. */
1207 while ((rule = TAILQ_FIRST(old_rules)) != NULL)
1208 pf_rm_rule(old_rules, rule);
1209 if (rs->rules[rs_num].inactive.ptr_array)
1210 free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
1211 rs->rules[rs_num].inactive.ptr_array = NULL;
1212 rs->rules[rs_num].inactive.rcount = 0;
1213 rs->rules[rs_num].inactive.open = 0;
1214 pf_remove_if_empty_ruleset(rs);
1220 pf_setup_pfsync_matching(struct pf_ruleset *rs)
1223 struct pf_rule *rule;
1225 u_int8_t digest[PF_MD5_DIGEST_LENGTH];
1228 for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
1229 /* XXX PF_RULESET_SCRUB as well? */
1230 if (rs_cnt == PF_RULESET_SCRUB)
1233 if (rs->rules[rs_cnt].inactive.ptr_array)
1234 free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
1235 rs->rules[rs_cnt].inactive.ptr_array = NULL;
1237 if (rs->rules[rs_cnt].inactive.rcount) {
1238 rs->rules[rs_cnt].inactive.ptr_array =
1239 malloc(sizeof(caddr_t) *
1240 rs->rules[rs_cnt].inactive.rcount,
1243 if (!rs->rules[rs_cnt].inactive.ptr_array)
1247 TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
1249 pf_hash_rule(&ctx, rule);
1250 (rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
1254 MD5Final(digest, &ctx);
1255 memcpy(pf_status.pf_chksum, digest, sizeof(pf_status.pf_chksum));
1261 pfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1263 pfioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
1266 struct pf_pooladdr *pa = NULL;
1267 struct pf_pool *pool = NULL;
1273 /* XXX keep in sync with switch() below */
1275 if (securelevel_gt(td->td_ucred, 2))
1277 if (securelevel > 1)
1285 case DIOCSETSTATUSIF:
1291 case DIOCGETTIMEOUT:
1292 case DIOCCLRRULECTRS:
1297 case DIOCGETRULESETS:
1298 case DIOCGETRULESET:
1299 case DIOCRGETTABLES:
1300 case DIOCRGETTSTATS:
1301 case DIOCRCLRTSTATS:
1307 case DIOCRGETASTATS:
1308 case DIOCRCLRASTATS:
1311 case DIOCGETSRCNODES:
1312 case DIOCCLRSRCNODES:
1313 case DIOCIGETIFACES:
1320 case DIOCRCLRTABLES:
1321 case DIOCRADDTABLES:
1322 case DIOCRDELTABLES:
1323 case DIOCRSETTFLAGS:
1324 if (((struct pfioc_table *)addr)->pfrio_flags &
1326 break; /* dummy operation ok */
1332 if (!(flags & FWRITE))
1340 case DIOCGETTIMEOUT:
1345 case DIOCGETRULESETS:
1346 case DIOCGETRULESET:
1348 case DIOCRGETTABLES:
1349 case DIOCRGETTSTATS:
1351 case DIOCRGETASTATS:
1354 case DIOCGETSRCNODES:
1355 case DIOCIGETIFACES:
1360 case DIOCRCLRTABLES:
1361 case DIOCRADDTABLES:
1362 case DIOCRDELTABLES:
1363 case DIOCRCLRTSTATS:
1368 case DIOCRSETTFLAGS:
1369 if (((struct pfioc_table *)addr)->pfrio_flags &
1371 flags |= FWRITE; /* need write lock for dummy */
1372 break; /* dummy operation ok */
1376 if (((struct pfioc_rule *)addr)->action == PF_GET_CLR_CNTR)
1385 sx_xlock(&pf_consistency_lock);
1387 sx_slock(&pf_consistency_lock);
1389 rw_enter_write(&pf_consistency_lock);
1391 rw_enter_read(&pf_consistency_lock);
1402 if (pf_status.running)
1410 DPFPRINTF(PF_DEBUG_MISC,
1411 ("pf: pfil registeration fail\n"));
1415 pf_status.running = 1;
1416 pf_status.since = time_second;
1417 if (pf_status.stateid == 0) {
1418 pf_status.stateid = time_second;
1419 pf_status.stateid = pf_status.stateid << 32;
1421 DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
1426 if (!pf_status.running)
1429 pf_status.running = 0;
1432 error = dehook_pf();
1435 pf_status.running = 1;
1436 DPFPRINTF(PF_DEBUG_MISC,
1437 ("pf: pfil unregisteration failed\n"));
1440 pf_status.since = time_second;
1441 DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
1446 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1447 struct pf_ruleset *ruleset;
1448 struct pf_rule *rule, *tail;
1449 struct pf_pooladdr *pa;
1452 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1453 ruleset = pf_find_ruleset(pr->anchor);
1454 if (ruleset == NULL) {
1458 rs_num = pf_get_ruleset_number(pr->rule.action);
1459 if (rs_num >= PF_RULESET_MAX) {
1463 if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1467 if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
1469 DPFPRINTF(PF_DEBUG_MISC,
1470 ("ticket: %d != [%d]%d\n", pr->ticket, rs_num,
1471 ruleset->rules[rs_num].inactive.ticket));
1476 if (pr->pool_ticket != ticket_pabuf) {
1478 DPFPRINTF(PF_DEBUG_MISC,
1479 ("pool_ticket: %d != %d\n", pr->pool_ticket,
1485 rule = pool_get(&pf_rule_pl, PR_NOWAIT);
1490 bcopy(&pr->rule, rule, sizeof(struct pf_rule));
1492 rule->cuid = td->td_ucred->cr_ruid;
1493 rule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1495 rule->cuid = p->p_cred->p_ruid;
1496 rule->cpid = p->p_pid;
1498 rule->anchor = NULL;
1500 TAILQ_INIT(&rule->rpool.list);
1501 /* initialize refcounting */
1503 rule->src_nodes = 0;
1504 rule->entries.tqe_prev = NULL;
1506 if (rule->af == AF_INET) {
1507 pool_put(&pf_rule_pl, rule);
1508 error = EAFNOSUPPORT;
1513 if (rule->af == AF_INET6) {
1514 pool_put(&pf_rule_pl, rule);
1515 error = EAFNOSUPPORT;
1519 tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
1522 rule->nr = tail->nr + 1;
1525 if (rule->ifname[0]) {
1526 rule->kif = pfi_kif_get(rule->ifname);
1527 if (rule->kif == NULL) {
1528 pool_put(&pf_rule_pl, rule);
1532 pfi_kif_ref(rule->kif, PFI_KIF_REF_RULE);
1535 #ifdef __FreeBSD__ /* ROUTING */
1536 if (rule->rtableid > 0 && rule->rtableid > rt_numfibs)
1538 if (rule->rtableid > 0 && !rtable_exists(rule->rtableid))
1544 if (rule->qname[0] != 0) {
1545 if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
1547 else if (rule->pqname[0] != 0) {
1549 pf_qname2qid(rule->pqname)) == 0)
1552 rule->pqid = rule->qid;
1555 if (rule->tagname[0])
1556 if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
1558 if (rule->match_tagname[0])
1559 if ((rule->match_tag =
1560 pf_tagname2tag(rule->match_tagname)) == 0)
1562 if (rule->rt && !rule->direction)
1569 if (rule->logif >= PFLOGIFS_MAX)
1572 if (pf_rtlabel_add(&rule->src.addr) ||
1573 pf_rtlabel_add(&rule->dst.addr))
1575 if (pfi_dynaddr_setup(&rule->src.addr, rule->af))
1577 if (pfi_dynaddr_setup(&rule->dst.addr, rule->af))
1579 if (pf_tbladdr_setup(ruleset, &rule->src.addr))
1581 if (pf_tbladdr_setup(ruleset, &rule->dst.addr))
1583 if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
1585 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1586 if (pf_tbladdr_setup(ruleset, &pa->addr))
1589 if (rule->overload_tblname[0]) {
1590 if ((rule->overload_tbl = pfr_attach_table(ruleset,
1591 rule->overload_tblname)) == NULL)
1594 rule->overload_tbl->pfrkt_flags |=
1598 pf_mv_pool(&pf_pabuf, &rule->rpool.list);
1599 if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
1600 (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
1601 (rule->rt > PF_FASTROUTE)) &&
1602 (TAILQ_FIRST(&rule->rpool.list) == NULL))
1606 pf_rm_rule(NULL, rule);
1611 if (!debug_pfugidhack && (rule->uid.op || rule->gid.op ||
1612 rule->log & PF_LOG_SOCKET_LOOKUP)) {
1613 DPFPRINTF(PF_DEBUG_MISC,
1614 ("pf: debug.pfugidhack enabled\n"));
1615 debug_pfugidhack = 1;
1619 rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
1620 rule->evaluations = rule->packets[0] = rule->packets[1] =
1621 rule->bytes[0] = rule->bytes[1] = 0;
1622 TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
1624 ruleset->rules[rs_num].inactive.rcount++;
1628 case DIOCGETRULES: {
1629 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1630 struct pf_ruleset *ruleset;
1631 struct pf_rule *tail;
1634 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1635 ruleset = pf_find_ruleset(pr->anchor);
1636 if (ruleset == NULL) {
1640 rs_num = pf_get_ruleset_number(pr->rule.action);
1641 if (rs_num >= PF_RULESET_MAX) {
1645 tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
1648 pr->nr = tail->nr + 1;
1651 pr->ticket = ruleset->rules[rs_num].active.ticket;
1656 struct pfioc_rule *pr = (struct pfioc_rule *)addr;
1657 struct pf_ruleset *ruleset;
1658 struct pf_rule *rule;
1661 pr->anchor[sizeof(pr->anchor) - 1] = 0;
1662 ruleset = pf_find_ruleset(pr->anchor);
1663 if (ruleset == NULL) {
1667 rs_num = pf_get_ruleset_number(pr->rule.action);
1668 if (rs_num >= PF_RULESET_MAX) {
1672 if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
1676 rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
1677 while ((rule != NULL) && (rule->nr != pr->nr))
1678 rule = TAILQ_NEXT(rule, entries);
1683 bcopy(rule, &pr->rule, sizeof(struct pf_rule));
1684 if (pf_anchor_copyout(ruleset, rule, pr)) {
1688 pfi_dynaddr_copyout(&pr->rule.src.addr);
1689 pfi_dynaddr_copyout(&pr->rule.dst.addr);
1690 pf_tbladdr_copyout(&pr->rule.src.addr);
1691 pf_tbladdr_copyout(&pr->rule.dst.addr);
1692 pf_rtlabel_copyout(&pr->rule.src.addr);
1693 pf_rtlabel_copyout(&pr->rule.dst.addr);
1694 for (i = 0; i < PF_SKIP_COUNT; ++i)
1695 if (rule->skip[i].ptr == NULL)
1696 pr->rule.skip[i].nr = -1;
1698 pr->rule.skip[i].nr =
1699 rule->skip[i].ptr->nr;
1701 if (pr->action == PF_GET_CLR_CNTR) {
1702 rule->evaluations = 0;
1703 rule->packets[0] = rule->packets[1] = 0;
1704 rule->bytes[0] = rule->bytes[1] = 0;
1709 case DIOCCHANGERULE: {
1710 struct pfioc_rule *pcr = (struct pfioc_rule *)addr;
1711 struct pf_ruleset *ruleset;
1712 struct pf_rule *oldrule = NULL, *newrule = NULL;
1716 if (!(pcr->action == PF_CHANGE_REMOVE ||
1717 pcr->action == PF_CHANGE_GET_TICKET) &&
1718 pcr->pool_ticket != ticket_pabuf) {
1723 if (pcr->action < PF_CHANGE_ADD_HEAD ||
1724 pcr->action > PF_CHANGE_GET_TICKET) {
1728 ruleset = pf_find_ruleset(pcr->anchor);
1729 if (ruleset == NULL) {
1733 rs_num = pf_get_ruleset_number(pcr->rule.action);
1734 if (rs_num >= PF_RULESET_MAX) {
1739 if (pcr->action == PF_CHANGE_GET_TICKET) {
1740 pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
1744 ruleset->rules[rs_num].active.ticket) {
1748 if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
1754 if (pcr->action != PF_CHANGE_REMOVE) {
1755 newrule = pool_get(&pf_rule_pl, PR_NOWAIT);
1756 if (newrule == NULL) {
1760 bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
1762 newrule->cuid = td->td_ucred->cr_ruid;
1763 newrule->cpid = td->td_proc ? td->td_proc->p_pid : 0;
1765 newrule->cuid = p->p_cred->p_ruid;
1766 newrule->cpid = p->p_pid;
1768 TAILQ_INIT(&newrule->rpool.list);
1769 /* initialize refcounting */
1770 newrule->states = 0;
1771 newrule->entries.tqe_prev = NULL;
1773 if (newrule->af == AF_INET) {
1774 pool_put(&pf_rule_pl, newrule);
1775 error = EAFNOSUPPORT;
1780 if (newrule->af == AF_INET6) {
1781 pool_put(&pf_rule_pl, newrule);
1782 error = EAFNOSUPPORT;
1786 if (newrule->ifname[0]) {
1787 newrule->kif = pfi_kif_get(newrule->ifname);
1788 if (newrule->kif == NULL) {
1789 pool_put(&pf_rule_pl, newrule);
1793 pfi_kif_ref(newrule->kif, PFI_KIF_REF_RULE);
1795 newrule->kif = NULL;
1797 if (newrule->rtableid > 0 &&
1798 #ifdef __FreeBSD__ /* ROUTING */
1799 newrule->rtableid > rt_numfibs)
1801 !rtable_exists(newrule->rtableid))
1807 if (newrule->qname[0] != 0) {
1809 pf_qname2qid(newrule->qname)) == 0)
1811 else if (newrule->pqname[0] != 0) {
1812 if ((newrule->pqid =
1813 pf_qname2qid(newrule->pqname)) == 0)
1816 newrule->pqid = newrule->qid;
1819 if (newrule->tagname[0])
1821 pf_tagname2tag(newrule->tagname)) == 0)
1823 if (newrule->match_tagname[0])
1824 if ((newrule->match_tag = pf_tagname2tag(
1825 newrule->match_tagname)) == 0)
1827 if (newrule->rt && !newrule->direction)
1833 if (newrule->logif >= PFLOGIFS_MAX)
1837 if (pf_rtlabel_add(&newrule->src.addr) ||
1838 pf_rtlabel_add(&newrule->dst.addr))
1840 if (pfi_dynaddr_setup(&newrule->src.addr, newrule->af))
1842 if (pfi_dynaddr_setup(&newrule->dst.addr, newrule->af))
1844 if (pf_tbladdr_setup(ruleset, &newrule->src.addr))
1846 if (pf_tbladdr_setup(ruleset, &newrule->dst.addr))
1848 if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
1850 TAILQ_FOREACH(pa, &pf_pabuf, entries)
1851 if (pf_tbladdr_setup(ruleset, &pa->addr))
1854 if (newrule->overload_tblname[0]) {
1855 if ((newrule->overload_tbl = pfr_attach_table(
1856 ruleset, newrule->overload_tblname)) ==
1860 newrule->overload_tbl->pfrkt_flags |=
1864 pf_mv_pool(&pf_pabuf, &newrule->rpool.list);
1865 if (((((newrule->action == PF_NAT) ||
1866 (newrule->action == PF_RDR) ||
1867 (newrule->action == PF_BINAT) ||
1868 (newrule->rt > PF_FASTROUTE)) &&
1869 !newrule->anchor)) &&
1870 (TAILQ_FIRST(&newrule->rpool.list) == NULL))
1874 pf_rm_rule(NULL, newrule);
1879 if (!debug_pfugidhack && (newrule->uid.op ||
1881 newrule->log & PF_LOG_SOCKET_LOOKUP)) {
1882 DPFPRINTF(PF_DEBUG_MISC,
1883 ("pf: debug.pfugidhack enabled\n"));
1884 debug_pfugidhack = 1;
1888 newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
1889 newrule->evaluations = 0;
1890 newrule->packets[0] = newrule->packets[1] = 0;
1891 newrule->bytes[0] = newrule->bytes[1] = 0;
1893 pf_empty_pool(&pf_pabuf);
1895 if (pcr->action == PF_CHANGE_ADD_HEAD)
1896 oldrule = TAILQ_FIRST(
1897 ruleset->rules[rs_num].active.ptr);
1898 else if (pcr->action == PF_CHANGE_ADD_TAIL)
1899 oldrule = TAILQ_LAST(
1900 ruleset->rules[rs_num].active.ptr, pf_rulequeue);
1902 oldrule = TAILQ_FIRST(
1903 ruleset->rules[rs_num].active.ptr);
1904 while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
1905 oldrule = TAILQ_NEXT(oldrule, entries);
1906 if (oldrule == NULL) {
1907 if (newrule != NULL)
1908 pf_rm_rule(NULL, newrule);
1914 if (pcr->action == PF_CHANGE_REMOVE) {
1915 pf_rm_rule(ruleset->rules[rs_num].active.ptr, oldrule);
1916 ruleset->rules[rs_num].active.rcount--;
1918 if (oldrule == NULL)
1920 ruleset->rules[rs_num].active.ptr,
1922 else if (pcr->action == PF_CHANGE_ADD_HEAD ||
1923 pcr->action == PF_CHANGE_ADD_BEFORE)
1924 TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
1927 ruleset->rules[rs_num].active.ptr,
1928 oldrule, newrule, entries);
1929 ruleset->rules[rs_num].active.rcount++;
1933 TAILQ_FOREACH(oldrule,
1934 ruleset->rules[rs_num].active.ptr, entries)
1937 ruleset->rules[rs_num].active.ticket++;
1939 pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
1940 pf_remove_if_empty_ruleset(ruleset);
1945 case DIOCCLRSTATES: {
1946 struct pf_state *state, *nexts;
1947 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1950 for (state = RB_MIN(pf_state_tree_id, &tree_id); state;
1952 nexts = RB_NEXT(pf_state_tree_id, &tree_id, state);
1954 if (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
1955 state->u.s.kif->pfik_name)) {
1957 /* don't send out individual delete messages */
1958 state->sync_flags = PFSTATE_NOSYNC;
1960 pf_unlink_state(state);
1964 psk->psk_af = killed;
1966 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
1971 case DIOCKILLSTATES: {
1972 struct pf_state *state, *nexts;
1973 struct pf_state_host *src, *dst;
1974 struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
1977 for (state = RB_MIN(pf_state_tree_id, &tree_id); state;
1979 nexts = RB_NEXT(pf_state_tree_id, &tree_id, state);
1981 if (state->direction == PF_OUT) {
1988 if ((!psk->psk_af || state->af == psk->psk_af)
1989 && (!psk->psk_proto || psk->psk_proto ==
1991 PF_MATCHA(psk->psk_src.neg,
1992 &psk->psk_src.addr.v.a.addr,
1993 &psk->psk_src.addr.v.a.mask,
1994 &src->addr, state->af) &&
1995 PF_MATCHA(psk->psk_dst.neg,
1996 &psk->psk_dst.addr.v.a.addr,
1997 &psk->psk_dst.addr.v.a.mask,
1998 &dst->addr, state->af) &&
1999 (psk->psk_src.port_op == 0 ||
2000 pf_match_port(psk->psk_src.port_op,
2001 psk->psk_src.port[0], psk->psk_src.port[1],
2003 (psk->psk_dst.port_op == 0 ||
2004 pf_match_port(psk->psk_dst.port_op,
2005 psk->psk_dst.port[0], psk->psk_dst.port[1],
2007 (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
2008 state->u.s.kif->pfik_name))) {
2010 /* send immediate delete of state */
2011 pfsync_delete_state(state);
2012 state->sync_flags |= PFSTATE_NOSYNC;
2014 pf_unlink_state(state);
2018 psk->psk_af = killed;
2022 case DIOCADDSTATE: {
2023 struct pfioc_state *ps = (struct pfioc_state *)addr;
2024 struct pf_state *state;
2025 struct pfi_kif *kif;
2027 if (ps->state.timeout >= PFTM_MAX &&
2028 ps->state.timeout != PFTM_UNTIL_PACKET) {
2032 state = pool_get(&pf_state_pl, PR_NOWAIT);
2033 if (state == NULL) {
2037 kif = pfi_kif_get(ps->state.u.ifname);
2039 pool_put(&pf_state_pl, state);
2043 bcopy(&ps->state, state, sizeof(struct pf_state));
2044 bzero(&state->u, sizeof(state->u));
2045 state->rule.ptr = &pf_default_rule;
2046 state->nat_rule.ptr = NULL;
2047 state->anchor.ptr = NULL;
2048 state->rt_kif = NULL;
2049 state->creation = time_second;
2050 state->pfsync_time = 0;
2051 state->packets[0] = state->packets[1] = 0;
2052 state->bytes[0] = state->bytes[1] = 0;
2054 if (pf_insert_state(kif, state)) {
2055 pfi_kif_unref(kif, PFI_KIF_REF_NONE);
2056 pool_put(&pf_state_pl, state);
2062 case DIOCGETSTATE: {
2063 struct pfioc_state *ps = (struct pfioc_state *)addr;
2064 struct pf_state *state;
2069 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
2074 if (state == NULL) {
2079 bcopy(state, &ps->state, sizeof(ps->state));
2080 strlcpy(ps->state.u.ifname, state->u.s.kif->pfik_name,
2081 sizeof(ps->state.u.ifname));
2082 ps->state.rule.nr = state->rule.ptr->nr;
2083 ps->state.nat_rule.nr = (state->nat_rule.ptr == NULL) ?
2084 -1 : state->nat_rule.ptr->nr;
2085 ps->state.anchor.nr = (state->anchor.ptr == NULL) ?
2086 -1 : state->anchor.ptr->nr;
2087 ps->state.creation = secs - ps->state.creation;
2088 ps->state.expire = pf_state_expires(state);
2089 if (ps->state.expire > secs)
2090 ps->state.expire -= secs;
2092 ps->state.expire = 0;
2096 case DIOCGETSTATES: {
2097 struct pfioc_states *ps = (struct pfioc_states *)addr;
2098 struct pf_state *state;
2099 struct pf_state *p, *pstore;
2101 int space = ps->ps_len;
2104 nr = pf_status.states;
2105 ps->ps_len = sizeof(struct pf_state) * nr;
2112 pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
2119 state = TAILQ_FIRST(&state_list);
2121 if (state->timeout != PFTM_UNLINKED) {
2122 int secs = time_second;
2124 if ((nr+1) * sizeof(*p) > (unsigned)ps->ps_len)
2127 bcopy(state, pstore, sizeof(*pstore));
2128 strlcpy(pstore->u.ifname,
2129 state->u.s.kif->pfik_name,
2130 sizeof(pstore->u.ifname));
2131 pstore->rule.nr = state->rule.ptr->nr;
2132 pstore->nat_rule.nr = (state->nat_rule.ptr ==
2133 NULL) ? -1 : state->nat_rule.ptr->nr;
2134 pstore->anchor.nr = (state->anchor.ptr ==
2135 NULL) ? -1 : state->anchor.ptr->nr;
2136 pstore->creation = secs - pstore->creation;
2137 pstore->expire = pf_state_expires(state);
2138 if (pstore->expire > secs)
2139 pstore->expire -= secs;
2143 PF_COPYOUT(pstore, p, sizeof(*p), error);
2145 error = copyout(pstore, p, sizeof(*p));
2148 free(pstore, M_TEMP);
2154 state = TAILQ_NEXT(state, u.s.entry_list);
2157 ps->ps_len = sizeof(struct pf_state) * nr;
2159 free(pstore, M_TEMP);
2163 case DIOCGETSTATUS: {
2164 struct pf_status *s = (struct pf_status *)addr;
2165 bcopy(&pf_status, s, sizeof(struct pf_status));
2166 pfi_fill_oldstatus(s);
2170 case DIOCSETSTATUSIF: {
2171 struct pfioc_if *pi = (struct pfioc_if *)addr;
2173 if (pi->ifname[0] == 0) {
2174 bzero(pf_status.ifname, IFNAMSIZ);
2177 if (ifunit(pi->ifname) == NULL) {
2181 strlcpy(pf_status.ifname, pi->ifname, IFNAMSIZ);
2185 case DIOCCLRSTATUS: {
2186 bzero(pf_status.counters, sizeof(pf_status.counters));
2187 bzero(pf_status.fcounters, sizeof(pf_status.fcounters));
2188 bzero(pf_status.scounters, sizeof(pf_status.scounters));
2189 pf_status.since = time_second;
2190 if (*pf_status.ifname)
2191 pfi_clr_istats(pf_status.ifname);
2196 struct pfioc_natlook *pnl = (struct pfioc_natlook *)addr;
2197 struct pf_state *state;
2198 struct pf_state_cmp key;
2199 int m = 0, direction = pnl->direction;
2202 key.proto = pnl->proto;
2205 PF_AZERO(&pnl->saddr, pnl->af) ||
2206 PF_AZERO(&pnl->daddr, pnl->af) ||
2207 ((pnl->proto == IPPROTO_TCP ||
2208 pnl->proto == IPPROTO_UDP) &&
2209 (!pnl->dport || !pnl->sport)))
2213 * userland gives us source and dest of connection,
2214 * reverse the lookup so we ask for what happens with
2215 * the return traffic, enabling us to find it in the
2218 if (direction == PF_IN) {
2219 PF_ACPY(&key.ext.addr, &pnl->daddr, pnl->af);
2220 key.ext.port = pnl->dport;
2221 PF_ACPY(&key.gwy.addr, &pnl->saddr, pnl->af);
2222 key.gwy.port = pnl->sport;
2223 state = pf_find_state_all(&key, PF_EXT_GWY, &m);
2225 PF_ACPY(&key.lan.addr, &pnl->daddr, pnl->af);
2226 key.lan.port = pnl->dport;
2227 PF_ACPY(&key.ext.addr, &pnl->saddr, pnl->af);
2228 key.ext.port = pnl->sport;
2229 state = pf_find_state_all(&key, PF_LAN_EXT, &m);
2232 error = E2BIG; /* more than one state */
2233 else if (state != NULL) {
2234 if (direction == PF_IN) {
2235 PF_ACPY(&pnl->rsaddr, &state->lan.addr,
2237 pnl->rsport = state->lan.port;
2238 PF_ACPY(&pnl->rdaddr, &pnl->daddr,
2240 pnl->rdport = pnl->dport;
2242 PF_ACPY(&pnl->rdaddr, &state->gwy.addr,
2244 pnl->rdport = state->gwy.port;
2245 PF_ACPY(&pnl->rsaddr, &pnl->saddr,
2247 pnl->rsport = pnl->sport;
2255 case DIOCSETTIMEOUT: {
2256 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
2259 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
2264 old = pf_default_rule.timeout[pt->timeout];
2265 if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
2267 pf_default_rule.timeout[pt->timeout] = pt->seconds;
2268 if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
2269 wakeup(pf_purge_thread);
2274 case DIOCGETTIMEOUT: {
2275 struct pfioc_tm *pt = (struct pfioc_tm *)addr;
2277 if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
2281 pt->seconds = pf_default_rule.timeout[pt->timeout];
2285 case DIOCGETLIMIT: {
2286 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
2288 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
2292 pl->limit = pf_pool_limits[pl->index].limit;
2296 case DIOCSETLIMIT: {
2297 struct pfioc_limit *pl = (struct pfioc_limit *)addr;
2300 if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
2301 pf_pool_limits[pl->index].pp == NULL) {
2306 uma_zone_set_max(pf_pool_limits[pl->index].pp, pl->limit);
2308 if (pool_sethardlimit(pf_pool_limits[pl->index].pp,
2309 pl->limit, NULL, 0) != 0) {
2314 old_limit = pf_pool_limits[pl->index].limit;
2315 pf_pool_limits[pl->index].limit = pl->limit;
2316 pl->limit = old_limit;
2320 case DIOCSETDEBUG: {
2321 u_int32_t *level = (u_int32_t *)addr;
2323 pf_status.debug = *level;
2327 case DIOCCLRRULECTRS: {
2328 /* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
2329 struct pf_ruleset *ruleset = &pf_main_ruleset;
2330 struct pf_rule *rule;
2333 ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
2334 rule->evaluations = 0;
2335 rule->packets[0] = rule->packets[1] = 0;
2336 rule->bytes[0] = rule->bytes[1] = 0;
2342 case DIOCGIFSPEED: {
2343 struct pf_ifspeed *psp = (struct pf_ifspeed *)addr;
2344 struct pf_ifspeed ps;
2347 if (psp->ifname[0] != 0) {
2348 /* Can we completely trust user-land? */
2349 strlcpy(ps.ifname, psp->ifname, IFNAMSIZ);
2350 ifp = ifunit(ps.ifname);
2352 psp->baudrate = ifp->if_baudrate;
2359 #endif /* __FreeBSD__ */
2362 case DIOCSTARTALTQ: {
2363 struct pf_altq *altq;
2365 /* enable all altq interfaces on active list */
2366 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2368 if (altq->qname[0] == 0 && (altq->local_flags &
2369 PFALTQ_FLAG_IF_REMOVED) == 0) {
2371 if (altq->qname[0] == 0) {
2373 error = pf_enable_altq(altq);
2379 pf_altq_running = 1;
2380 DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
2384 case DIOCSTOPALTQ: {
2385 struct pf_altq *altq;
2387 /* disable all altq interfaces on active list */
2388 TAILQ_FOREACH(altq, pf_altqs_active, entries) {
2390 if (altq->qname[0] == 0 && (altq->local_flags &
2391 PFALTQ_FLAG_IF_REMOVED) == 0) {
2393 if (altq->qname[0] == 0) {
2395 error = pf_disable_altq(altq);
2401 pf_altq_running = 0;
2402 DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
2407 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2408 struct pf_altq *altq, *a;
2410 if (pa->ticket != ticket_altqs_inactive) {
2414 altq = pool_get(&pf_altq_pl, PR_NOWAIT);
2419 bcopy(&pa->altq, altq, sizeof(struct pf_altq));
2421 altq->local_flags = 0;
2425 * if this is for a queue, find the discipline and
2426 * copy the necessary fields
2428 if (altq->qname[0] != 0) {
2429 if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
2431 pool_put(&pf_altq_pl, altq);
2434 altq->altq_disc = NULL;
2435 TAILQ_FOREACH(a, pf_altqs_inactive, entries) {
2436 if (strncmp(a->ifname, altq->ifname,
2437 IFNAMSIZ) == 0 && a->qname[0] == 0) {
2438 altq->altq_disc = a->altq_disc;
2447 if ((ifp = ifunit(altq->ifname)) == NULL) {
2448 altq->local_flags |= PFALTQ_FLAG_IF_REMOVED;
2452 error = altq_add(altq);
2458 pool_put(&pf_altq_pl, altq);
2462 TAILQ_INSERT_TAIL(pf_altqs_inactive, altq, entries);
2463 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2467 case DIOCGETALTQS: {
2468 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2469 struct pf_altq *altq;
2472 TAILQ_FOREACH(altq, pf_altqs_active, entries)
2474 pa->ticket = ticket_altqs_active;
2479 struct pfioc_altq *pa = (struct pfioc_altq *)addr;
2480 struct pf_altq *altq;
2483 if (pa->ticket != ticket_altqs_active) {
2488 altq = TAILQ_FIRST(pf_altqs_active);
2489 while ((altq != NULL) && (nr < pa->nr)) {
2490 altq = TAILQ_NEXT(altq, entries);
2497 bcopy(altq, &pa->altq, sizeof(struct pf_altq));
2501 case DIOCCHANGEALTQ:
2502 /* CHANGEALTQ not supported yet! */
2506 case DIOCGETQSTATS: {
2507 struct pfioc_qstats *pq = (struct pfioc_qstats *)addr;
2508 struct pf_altq *altq;
2512 if (pq->ticket != ticket_altqs_active) {
2516 nbytes = pq->nbytes;
2518 altq = TAILQ_FIRST(pf_altqs_active);
2519 while ((altq != NULL) && (nr < pq->nr)) {
2520 altq = TAILQ_NEXT(altq, entries);
2528 if ((altq->local_flags & PFALTQ_FLAG_IF_REMOVED) != 0) {
2534 error = altq_getqstats(altq, pq->buf, &nbytes);
2539 pq->scheduler = altq->scheduler;
2540 pq->nbytes = nbytes;
2546 case DIOCBEGINADDRS: {
2547 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2549 pf_empty_pool(&pf_pabuf);
2550 pp->ticket = ++ticket_pabuf;
2555 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2557 if (pp->ticket != ticket_pabuf) {
2562 if (pp->af == AF_INET) {
2563 error = EAFNOSUPPORT;
2568 if (pp->af == AF_INET6) {
2569 error = EAFNOSUPPORT;
2573 if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
2574 pp->addr.addr.type != PF_ADDR_DYNIFTL &&
2575 pp->addr.addr.type != PF_ADDR_TABLE) {
2579 pa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2584 bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
2585 if (pa->ifname[0]) {
2586 pa->kif = pfi_kif_get(pa->ifname);
2587 if (pa->kif == NULL) {
2588 pool_put(&pf_pooladdr_pl, pa);
2592 pfi_kif_ref(pa->kif, PFI_KIF_REF_RULE);
2594 if (pfi_dynaddr_setup(&pa->addr, pp->af)) {
2595 pfi_dynaddr_remove(&pa->addr);
2596 pfi_kif_unref(pa->kif, PFI_KIF_REF_RULE);
2597 pool_put(&pf_pooladdr_pl, pa);
2601 TAILQ_INSERT_TAIL(&pf_pabuf, pa, entries);
2605 case DIOCGETADDRS: {
2606 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2609 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2610 pp->r_num, 0, 1, 0);
2615 TAILQ_FOREACH(pa, &pool->list, entries)
2621 struct pfioc_pooladdr *pp = (struct pfioc_pooladdr *)addr;
2624 pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
2625 pp->r_num, 0, 1, 1);
2630 pa = TAILQ_FIRST(&pool->list);
2631 while ((pa != NULL) && (nr < pp->nr)) {
2632 pa = TAILQ_NEXT(pa, entries);
2639 bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
2640 pfi_dynaddr_copyout(&pp->addr.addr);
2641 pf_tbladdr_copyout(&pp->addr.addr);
2642 pf_rtlabel_copyout(&pp->addr.addr);
2646 case DIOCCHANGEADDR: {
2647 struct pfioc_pooladdr *pca = (struct pfioc_pooladdr *)addr;
2648 struct pf_pooladdr *oldpa = NULL, *newpa = NULL;
2649 struct pf_ruleset *ruleset;
2651 if (pca->action < PF_CHANGE_ADD_HEAD ||
2652 pca->action > PF_CHANGE_REMOVE) {
2656 if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
2657 pca->addr.addr.type != PF_ADDR_DYNIFTL &&
2658 pca->addr.addr.type != PF_ADDR_TABLE) {
2663 ruleset = pf_find_ruleset(pca->anchor);
2664 if (ruleset == NULL) {
2668 pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
2669 pca->r_num, pca->r_last, 1, 1);
2674 if (pca->action != PF_CHANGE_REMOVE) {
2675 newpa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
2676 if (newpa == NULL) {
2680 bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
2682 if (pca->af == AF_INET) {
2683 pool_put(&pf_pooladdr_pl, newpa);
2684 error = EAFNOSUPPORT;
2689 if (pca->af == AF_INET6) {
2690 pool_put(&pf_pooladdr_pl, newpa);
2691 error = EAFNOSUPPORT;
2695 if (newpa->ifname[0]) {
2696 newpa->kif = pfi_kif_get(newpa->ifname);
2697 if (newpa->kif == NULL) {
2698 pool_put(&pf_pooladdr_pl, newpa);
2702 pfi_kif_ref(newpa->kif, PFI_KIF_REF_RULE);
2705 if (pfi_dynaddr_setup(&newpa->addr, pca->af) ||
2706 pf_tbladdr_setup(ruleset, &newpa->addr)) {
2707 pfi_dynaddr_remove(&newpa->addr);
2708 pfi_kif_unref(newpa->kif, PFI_KIF_REF_RULE);
2709 pool_put(&pf_pooladdr_pl, newpa);
2715 if (pca->action == PF_CHANGE_ADD_HEAD)
2716 oldpa = TAILQ_FIRST(&pool->list);
2717 else if (pca->action == PF_CHANGE_ADD_TAIL)
2718 oldpa = TAILQ_LAST(&pool->list, pf_palist);
2722 oldpa = TAILQ_FIRST(&pool->list);
2723 while ((oldpa != NULL) && (i < pca->nr)) {
2724 oldpa = TAILQ_NEXT(oldpa, entries);
2727 if (oldpa == NULL) {
2733 if (pca->action == PF_CHANGE_REMOVE) {
2734 TAILQ_REMOVE(&pool->list, oldpa, entries);
2735 pfi_dynaddr_remove(&oldpa->addr);
2736 pf_tbladdr_remove(&oldpa->addr);
2737 pfi_kif_unref(oldpa->kif, PFI_KIF_REF_RULE);
2738 pool_put(&pf_pooladdr_pl, oldpa);
2741 TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
2742 else if (pca->action == PF_CHANGE_ADD_HEAD ||
2743 pca->action == PF_CHANGE_ADD_BEFORE)
2744 TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
2746 TAILQ_INSERT_AFTER(&pool->list, oldpa,
2750 pool->cur = TAILQ_FIRST(&pool->list);
2751 PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
2756 case DIOCGETRULESETS: {
2757 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2758 struct pf_ruleset *ruleset;
2759 struct pf_anchor *anchor;
2761 pr->path[sizeof(pr->path) - 1] = 0;
2762 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2767 if (ruleset->anchor == NULL) {
2768 /* XXX kludge for pf_main_ruleset */
2769 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2770 if (anchor->parent == NULL)
2773 RB_FOREACH(anchor, pf_anchor_node,
2774 &ruleset->anchor->children)
2780 case DIOCGETRULESET: {
2781 struct pfioc_ruleset *pr = (struct pfioc_ruleset *)addr;
2782 struct pf_ruleset *ruleset;
2783 struct pf_anchor *anchor;
2786 pr->path[sizeof(pr->path) - 1] = 0;
2787 if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
2792 if (ruleset->anchor == NULL) {
2793 /* XXX kludge for pf_main_ruleset */
2794 RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
2795 if (anchor->parent == NULL && nr++ == pr->nr) {
2796 strlcpy(pr->name, anchor->name,
2801 RB_FOREACH(anchor, pf_anchor_node,
2802 &ruleset->anchor->children)
2803 if (nr++ == pr->nr) {
2804 strlcpy(pr->name, anchor->name,
2814 case DIOCRCLRTABLES: {
2815 struct pfioc_table *io = (struct pfioc_table *)addr;
2817 if (io->pfrio_esize != 0) {
2821 error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
2822 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2826 case DIOCRADDTABLES: {
2827 struct pfioc_table *io = (struct pfioc_table *)addr;
2829 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2833 error = pfr_add_tables(io->pfrio_buffer, io->pfrio_size,
2834 &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2838 case DIOCRDELTABLES: {
2839 struct pfioc_table *io = (struct pfioc_table *)addr;
2841 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2845 error = pfr_del_tables(io->pfrio_buffer, io->pfrio_size,
2846 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2850 case DIOCRGETTABLES: {
2851 struct pfioc_table *io = (struct pfioc_table *)addr;
2853 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2857 error = pfr_get_tables(&io->pfrio_table, io->pfrio_buffer,
2858 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2862 case DIOCRGETTSTATS: {
2863 struct pfioc_table *io = (struct pfioc_table *)addr;
2865 if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
2869 error = pfr_get_tstats(&io->pfrio_table, io->pfrio_buffer,
2870 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2874 case DIOCRCLRTSTATS: {
2875 struct pfioc_table *io = (struct pfioc_table *)addr;
2877 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2881 error = pfr_clr_tstats(io->pfrio_buffer, io->pfrio_size,
2882 &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2886 case DIOCRSETTFLAGS: {
2887 struct pfioc_table *io = (struct pfioc_table *)addr;
2889 if (io->pfrio_esize != sizeof(struct pfr_table)) {
2893 error = pfr_set_tflags(io->pfrio_buffer, io->pfrio_size,
2894 io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
2895 &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2899 case DIOCRCLRADDRS: {
2900 struct pfioc_table *io = (struct pfioc_table *)addr;
2902 if (io->pfrio_esize != 0) {
2906 error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
2907 io->pfrio_flags | PFR_FLAG_USERIOCTL);
2911 case DIOCRADDADDRS: {
2912 struct pfioc_table *io = (struct pfioc_table *)addr;
2914 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2918 error = pfr_add_addrs(&io->pfrio_table, io->pfrio_buffer,
2919 io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
2920 PFR_FLAG_USERIOCTL);
2924 case DIOCRDELADDRS: {
2925 struct pfioc_table *io = (struct pfioc_table *)addr;
2927 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2931 error = pfr_del_addrs(&io->pfrio_table, io->pfrio_buffer,
2932 io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
2933 PFR_FLAG_USERIOCTL);
2937 case DIOCRSETADDRS: {
2938 struct pfioc_table *io = (struct pfioc_table *)addr;
2940 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2944 error = pfr_set_addrs(&io->pfrio_table, io->pfrio_buffer,
2945 io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
2946 &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
2947 PFR_FLAG_USERIOCTL, 0);
2951 case DIOCRGETADDRS: {
2952 struct pfioc_table *io = (struct pfioc_table *)addr;
2954 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2958 error = pfr_get_addrs(&io->pfrio_table, io->pfrio_buffer,
2959 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2963 case DIOCRGETASTATS: {
2964 struct pfioc_table *io = (struct pfioc_table *)addr;
2966 if (io->pfrio_esize != sizeof(struct pfr_astats)) {
2970 error = pfr_get_astats(&io->pfrio_table, io->pfrio_buffer,
2971 &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
2975 case DIOCRCLRASTATS: {
2976 struct pfioc_table *io = (struct pfioc_table *)addr;
2978 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2982 error = pfr_clr_astats(&io->pfrio_table, io->pfrio_buffer,
2983 io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
2984 PFR_FLAG_USERIOCTL);
2988 case DIOCRTSTADDRS: {
2989 struct pfioc_table *io = (struct pfioc_table *)addr;
2991 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
2995 error = pfr_tst_addrs(&io->pfrio_table, io->pfrio_buffer,
2996 io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
2997 PFR_FLAG_USERIOCTL);
3001 case DIOCRINADEFINE: {
3002 struct pfioc_table *io = (struct pfioc_table *)addr;
3004 if (io->pfrio_esize != sizeof(struct pfr_addr)) {
3008 error = pfr_ina_define(&io->pfrio_table, io->pfrio_buffer,
3009 io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
3010 io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
3015 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
3016 error = pf_osfp_add(io);
3021 struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
3022 error = pf_osfp_get(io);
3027 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3028 struct pfioc_trans_e *ioe;
3029 struct pfr_table *table;
3032 if (io->esize != sizeof(*ioe)) {
3039 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3041 table = (struct pfr_table *)malloc(sizeof(*table),
3046 for (i = 0; i < io->size; i++) {
3048 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3051 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3053 free(table, M_TEMP);
3058 switch (ioe->rs_num) {
3060 case PF_RULESET_ALTQ:
3061 if (ioe->anchor[0]) {
3062 free(table, M_TEMP);
3067 if ((error = pf_begin_altq(&ioe->ticket))) {
3068 free(table, M_TEMP);
3074 case PF_RULESET_TABLE:
3075 bzero(table, sizeof(*table));
3076 strlcpy(table->pfrt_anchor, ioe->anchor,
3077 sizeof(table->pfrt_anchor));
3078 if ((error = pfr_ina_begin(table,
3079 &ioe->ticket, NULL, 0))) {
3080 free(table, M_TEMP);
3086 if ((error = pf_begin_rules(&ioe->ticket,
3087 ioe->rs_num, ioe->anchor))) {
3088 free(table, M_TEMP);
3095 PF_COPYOUT(ioe, io->array+i, sizeof(io->array[i]),
3099 if (copyout(ioe, io->array+i, sizeof(io->array[i]))) {
3101 free(table, M_TEMP);
3107 free(table, M_TEMP);
3112 case DIOCXROLLBACK: {
3113 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3114 struct pfioc_trans_e *ioe;
3115 struct pfr_table *table;
3118 if (io->esize != sizeof(*ioe)) {
3125 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3127 table = (struct pfr_table *)malloc(sizeof(*table),
3132 for (i = 0; i < io->size; i++) {
3134 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3137 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3139 free(table, M_TEMP);
3144 switch (ioe->rs_num) {
3146 case PF_RULESET_ALTQ:
3147 if (ioe->anchor[0]) {
3148 free(table, M_TEMP);
3153 if ((error = pf_rollback_altq(ioe->ticket))) {
3154 free(table, M_TEMP);
3156 goto fail; /* really bad */
3160 case PF_RULESET_TABLE:
3161 bzero(table, sizeof(*table));
3162 strlcpy(table->pfrt_anchor, ioe->anchor,
3163 sizeof(table->pfrt_anchor));
3164 if ((error = pfr_ina_rollback(table,
3165 ioe->ticket, NULL, 0))) {
3166 free(table, M_TEMP);
3168 goto fail; /* really bad */
3172 if ((error = pf_rollback_rules(ioe->ticket,
3173 ioe->rs_num, ioe->anchor))) {
3174 free(table, M_TEMP);
3176 goto fail; /* really bad */
3181 free(table, M_TEMP);
3187 struct pfioc_trans *io = (struct pfioc_trans *)addr;
3188 struct pfioc_trans_e *ioe;
3189 struct pfr_table *table;
3190 struct pf_ruleset *rs;
3193 if (io->esize != sizeof(*ioe)) {
3200 ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
3202 table = (struct pfr_table *)malloc(sizeof(*table),
3207 /* first makes sure everything will succeed */
3208 for (i = 0; i < io->size; i++) {
3210 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3213 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3215 free(table, M_TEMP);
3220 switch (ioe->rs_num) {
3222 case PF_RULESET_ALTQ:
3223 if (ioe->anchor[0]) {
3224 free(table, M_TEMP);
3229 if (!altqs_inactive_open || ioe->ticket !=
3230 ticket_altqs_inactive) {
3231 free(table, M_TEMP);
3238 case PF_RULESET_TABLE:
3239 rs = pf_find_ruleset(ioe->anchor);
3240 if (rs == NULL || !rs->topen || ioe->ticket !=
3242 free(table, M_TEMP);
3249 if (ioe->rs_num < 0 || ioe->rs_num >=
3251 free(table, M_TEMP);
3256 rs = pf_find_ruleset(ioe->anchor);
3258 !rs->rules[ioe->rs_num].inactive.open ||
3259 rs->rules[ioe->rs_num].inactive.ticket !=
3261 free(table, M_TEMP);
3269 /* now do the commit - no errors should happen here */
3270 for (i = 0; i < io->size; i++) {
3272 PF_COPYIN(io->array+i, ioe, sizeof(*ioe), error);
3275 if (copyin(io->array+i, ioe, sizeof(*ioe))) {
3277 free(table, M_TEMP);
3282 switch (ioe->rs_num) {
3284 case PF_RULESET_ALTQ:
3285 if ((error = pf_commit_altq(ioe->ticket))) {
3286 free(table, M_TEMP);
3288 goto fail; /* really bad */
3292 case PF_RULESET_TABLE:
3293 bzero(table, sizeof(*table));
3294 strlcpy(table->pfrt_anchor, ioe->anchor,
3295 sizeof(table->pfrt_anchor));
3296 if ((error = pfr_ina_commit(table, ioe->ticket,
3298 free(table, M_TEMP);
3300 goto fail; /* really bad */
3304 if ((error = pf_commit_rules(ioe->ticket,
3305 ioe->rs_num, ioe->anchor))) {
3306 free(table, M_TEMP);
3308 goto fail; /* really bad */
3313 free(table, M_TEMP);
3318 case DIOCGETSRCNODES: {
3319 struct pfioc_src_nodes *psn = (struct pfioc_src_nodes *)addr;
3320 struct pf_src_node *n, *p, *pstore;
3322 int space = psn->psn_len;
3325 RB_FOREACH(n, pf_src_tree, &tree_src_tracking)
3327 psn->psn_len = sizeof(struct pf_src_node) * nr;
3334 pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
3339 p = psn->psn_src_nodes;
3340 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3341 int secs = time_second, diff;
3343 if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
3346 bcopy(n, pstore, sizeof(*pstore));
3347 if (n->rule.ptr != NULL)
3348 pstore->rule.nr = n->rule.ptr->nr;
3349 pstore->creation = secs - pstore->creation;
3350 if (pstore->expire > secs)
3351 pstore->expire -= secs;
3355 /* adjust the connection rate estimate */
3356 diff = secs - n->conn_rate.last;
3357 if (diff >= n->conn_rate.seconds)
3358 pstore->conn_rate.count = 0;
3360 pstore->conn_rate.count -=
3361 n->conn_rate.count * diff /
3362 n->conn_rate.seconds;
3365 PF_COPYOUT(pstore, p, sizeof(*p), error);
3367 error = copyout(pstore, p, sizeof(*p));
3370 free(pstore, M_TEMP);
3376 psn->psn_len = sizeof(struct pf_src_node) * nr;
3378 free(pstore, M_TEMP);
3382 case DIOCCLRSRCNODES: {
3383 struct pf_src_node *n;
3384 struct pf_state *state;
3386 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3387 state->src_node = NULL;
3388 state->nat_src_node = NULL;
3390 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3394 pf_purge_expired_src_nodes(1);
3395 pf_status.src_nodes = 0;
3399 case DIOCKILLSRCNODES: {
3400 struct pf_src_node *sn;
3402 struct pfioc_src_node_kill *psnk = \
3403 (struct pfioc_src_node_kill *) addr;
3406 RB_FOREACH(sn, pf_src_tree, &tree_src_tracking) {
3407 if (PF_MATCHA(psnk->psnk_src.neg, \
3408 &psnk->psnk_src.addr.v.a.addr, \
3409 &psnk->psnk_src.addr.v.a.mask, \
3410 &sn->addr, sn->af) &&
3411 PF_MATCHA(psnk->psnk_dst.neg, \
3412 &psnk->psnk_dst.addr.v.a.addr, \
3413 &psnk->psnk_dst.addr.v.a.mask, \
3414 &sn->raddr, sn->af)) {
3415 /* Handle state to src_node linkage */
3416 if (sn->states != 0) {
3417 RB_FOREACH(s, pf_state_tree_id,
3419 if (s->src_node == sn)
3421 if (s->nat_src_node == sn)
3422 s->nat_src_node = NULL;
3432 pf_purge_expired_src_nodes(1);
3434 psnk->psnk_af = killed;
3438 case DIOCSETHOSTID: {
3439 u_int32_t *hostid = (u_int32_t *)addr;
3442 pf_status.hostid = arc4random();
3444 pf_status.hostid = *hostid;
3452 case DIOCIGETIFACES: {
3453 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3455 if (io->pfiio_esize != sizeof(struct pfi_kif)) {
3459 error = pfi_get_ifaces(io->pfiio_name, io->pfiio_buffer,
3464 case DIOCSETIFFLAG: {
3465 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3467 error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
3471 case DIOCCLRIFFLAG: {
3472 struct pfioc_iface *io = (struct pfioc_iface *)addr;
3474 error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
3487 sx_xunlock(&pf_consistency_lock);
3489 sx_sunlock(&pf_consistency_lock);
3492 /* XXX: Lock order? */
3494 rw_exit_write(&pf_consistency_lock);
3496 rw_exit_read(&pf_consistency_lock);
3503 * XXX - Check for version missmatch!!!
3506 pf_clear_states(void)
3508 struct pf_state *state;
3510 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3511 state->timeout = PFTM_PURGE;
3513 /* don't send out individual delete messages */
3514 state->sync_flags = PFSTATE_NOSYNC;
3516 pf_unlink_state(state);
3521 * XXX This is called on module unload, we do not want to sync that over? */
3523 pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
3528 pf_clear_tables(void)
3530 struct pfioc_table io;
3533 bzero(&io, sizeof(io));
3535 error = pfr_clr_tables(&io.pfrio_table, &io.pfrio_ndel,
3542 pf_clear_srcnodes(void)
3544 struct pf_src_node *n;
3545 struct pf_state *state;
3547 RB_FOREACH(state, pf_state_tree_id, &tree_id) {
3548 state->src_node = NULL;
3549 state->nat_src_node = NULL;
3551 RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
3557 * XXX - Check for version missmatch!!!
3561 * Duplicate pfctl -Fa operation to get rid of as much as we can.
3570 pf_status.running = 0;
3572 if ((error = pf_begin_rules(&t[0], PF_RULESET_SCRUB, &nn))
3574 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: SCRUB\n"));
3577 if ((error = pf_begin_rules(&t[1], PF_RULESET_FILTER, &nn))
3579 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: FILTER\n"));
3580 break; /* XXX: rollback? */
3582 if ((error = pf_begin_rules(&t[2], PF_RULESET_NAT, &nn))
3584 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: NAT\n"));
3585 break; /* XXX: rollback? */
3587 if ((error = pf_begin_rules(&t[3], PF_RULESET_BINAT, &nn))
3589 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: BINAT\n"));
3590 break; /* XXX: rollback? */
3592 if ((error = pf_begin_rules(&t[4], PF_RULESET_RDR, &nn))
3594 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: RDR\n"));
3595 break; /* XXX: rollback? */
3598 /* XXX: these should always succeed here */
3599 pf_commit_rules(t[0], PF_RULESET_SCRUB, &nn);
3600 pf_commit_rules(t[1], PF_RULESET_FILTER, &nn);
3601 pf_commit_rules(t[2], PF_RULESET_NAT, &nn);
3602 pf_commit_rules(t[3], PF_RULESET_BINAT, &nn);
3603 pf_commit_rules(t[4], PF_RULESET_RDR, &nn);
3605 if ((error = pf_clear_tables()) != 0)
3609 if ((error = pf_begin_altq(&t[0])) != 0) {
3610 DPFPRINTF(PF_DEBUG_MISC, ("shutdown_pf: ALTQ\n"));
3613 pf_commit_altq(t[0]);
3618 pf_clear_srcnodes();
3620 /* status does not use malloced mem so no need to cleanup */
3621 /* fingerprints and interfaces have thier own cleanup code */
3628 pf_check_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3632 * XXX Wed Jul 9 22:03:16 2003 UTC
3633 * OpenBSD has changed its byte ordering convention on ip_len/ip_off
3634 * in network stack. OpenBSD's network stack have converted
3635 * ip_len/ip_off to host byte order frist as FreeBSD.
3636 * Now this is not true anymore , so we should convert back to network
3639 struct ip *h = NULL;
3642 if ((*m)->m_pkthdr.len >= (int)sizeof(struct ip)) {
3643 /* if m_pkthdr.len is less than ip header, pf will handle. */
3644 h = mtod(*m, struct ip *);
3648 chk = pf_test(PF_IN, ifp, m, NULL, inp);
3654 /* pf_test can change ip header location */
3655 h = mtod(*m, struct ip *);
3663 pf_check_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3667 * XXX Wed Jul 9 22:03:16 2003 UTC
3668 * OpenBSD has changed its byte ordering convention on ip_len/ip_off
3669 * in network stack. OpenBSD's network stack have converted
3670 * ip_len/ip_off to host byte order frist as FreeBSD.
3671 * Now this is not true anymore , so we should convert back to network
3674 struct ip *h = NULL;
3677 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3678 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3679 in_delayed_cksum(*m);
3680 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3682 if ((*m)->m_pkthdr.len >= (int)sizeof(*h)) {
3683 /* if m_pkthdr.len is less than ip header, pf will handle. */
3684 h = mtod(*m, struct ip *);
3688 chk = pf_test(PF_OUT, ifp, m, NULL, inp);
3694 /* pf_test can change ip header location */
3695 h = mtod(*m, struct ip *);
3704 pf_check6_in(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3707 INIT_VNET_NET(curvnet);
3710 * IPv6 is not affected by ip_len/ip_off byte order changes.
3715 * In case of loopback traffic IPv6 uses the real interface in
3716 * order to support scoped addresses. In order to support stateful
3717 * filtering we have change this to lo0 as it is the case in IPv4.
3719 chk = pf_test6(PF_IN, (*m)->m_flags & M_LOOP ? V_loif : ifp, m,
3729 pf_check6_out(void *arg, struct mbuf **m, struct ifnet *ifp, int dir,
3733 * IPv6 does not affected ip_len/ip_off byte order changes.
3737 /* We need a proper CSUM befor we start (s. OpenBSD ip_output) */
3738 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
3739 in_delayed_cksum(*m);
3740 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
3742 chk = pf_test6(PF_OUT, ifp, m, NULL, inp);
3754 struct pfil_head *pfh_inet;
3756 struct pfil_head *pfh_inet6;
3759 PF_ASSERT(MA_NOTOWNED);
3764 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3765 if (pfh_inet == NULL)
3766 return (ESRCH); /* XXX */
3767 pfil_add_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
3768 pfil_add_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
3770 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3771 if (pfh_inet6 == NULL) {
3772 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3774 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3776 return (ESRCH); /* XXX */
3778 pfil_add_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
3779 pfil_add_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
3789 struct pfil_head *pfh_inet;
3791 struct pfil_head *pfh_inet6;
3794 PF_ASSERT(MA_NOTOWNED);
3796 if (pf_pfil_hooked == 0)
3799 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
3800 if (pfh_inet == NULL)
3801 return (ESRCH); /* XXX */
3802 pfil_remove_hook(pf_check_in, NULL, PFIL_IN | PFIL_WAITOK,
3804 pfil_remove_hook(pf_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
3807 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
3808 if (pfh_inet6 == NULL)
3809 return (ESRCH); /* XXX */
3810 pfil_remove_hook(pf_check6_in, NULL, PFIL_IN | PFIL_WAITOK,
3812 pfil_remove_hook(pf_check6_out, NULL, PFIL_OUT | PFIL_WAITOK,
3825 pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
3826 if (pfattach() < 0) {
3827 destroy_dev(pf_dev);
3840 pf_status.running = 0;
3842 error = dehook_pf();
3845 * Should not happen!
3846 * XXX Due to error code ESRCH, kldunload will show
3847 * a message like 'No such process'.
3849 printf("%s : pfil unregisteration fail\n", __FUNCTION__);
3855 while (pf_end_threads < 2) {
3856 wakeup_one(pf_purge_thread);
3857 msleep(pf_purge_thread, &pf_task_mtx, 0, "pftmo", hz);
3864 destroy_dev(pf_dev);
3870 pf_modevent(module_t mod, int type, void *data)
3880 error = pf_unload();
3889 static moduledata_t pf_mod = {
3895 DECLARE_MODULE(pf, pf_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST);
3896 MODULE_VERSION(pf, PF_MODVER);
3897 #endif /* __FreeBSD__ */