Home | History | Annotate | Line # | Download | only in net
pf_ioctl.c revision 1.1.1.4
      1  1.1.1.4  martti /*	$NetBSD: pf_ioctl.c,v 1.1.1.4 2009/12/01 07:03:13 martti Exp $	*/
      2  1.1.1.4  martti /*	$OpenBSD: pf_ioctl.c,v 1.182 2007/06/24 11:17:13 mcbride Exp $ */
      3      1.1  itojun 
      4      1.1  itojun /*
      5      1.1  itojun  * Copyright (c) 2001 Daniel Hartmeier
      6      1.1  itojun  * Copyright (c) 2002,2003 Henning Brauer
      7      1.1  itojun  * All rights reserved.
      8      1.1  itojun  *
      9      1.1  itojun  * Redistribution and use in source and binary forms, with or without
     10      1.1  itojun  * modification, are permitted provided that the following conditions
     11      1.1  itojun  * are met:
     12      1.1  itojun  *
     13      1.1  itojun  *    - Redistributions of source code must retain the above copyright
     14      1.1  itojun  *      notice, this list of conditions and the following disclaimer.
     15      1.1  itojun  *    - Redistributions in binary form must reproduce the above
     16      1.1  itojun  *      copyright notice, this list of conditions and the following
     17      1.1  itojun  *      disclaimer in the documentation and/or other materials provided
     18      1.1  itojun  *      with the distribution.
     19      1.1  itojun  *
     20      1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21      1.1  itojun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22      1.1  itojun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23      1.1  itojun  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24      1.1  itojun  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1  itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26      1.1  itojun  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27      1.1  itojun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     28      1.1  itojun  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     30      1.1  itojun  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31      1.1  itojun  * POSSIBILITY OF SUCH DAMAGE.
     32      1.1  itojun  *
     33      1.1  itojun  * Effort sponsored in part by the Defense Advanced Research Projects
     34      1.1  itojun  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     35      1.1  itojun  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     36      1.1  itojun  *
     37      1.1  itojun  */
     38      1.1  itojun 
     39      1.1  itojun #include "pfsync.h"
     40      1.1  itojun 
     41      1.1  itojun #include <sys/param.h>
     42      1.1  itojun #include <sys/systm.h>
     43      1.1  itojun #include <sys/mbuf.h>
     44      1.1  itojun #include <sys/filio.h>
     45      1.1  itojun #include <sys/fcntl.h>
     46      1.1  itojun #include <sys/socket.h>
     47      1.1  itojun #include <sys/socketvar.h>
     48      1.1  itojun #include <sys/kernel.h>
     49      1.1  itojun #include <sys/time.h>
     50      1.1  itojun #include <sys/timeout.h>
     51      1.1  itojun #include <sys/pool.h>
     52  1.1.1.4  martti #include <sys/proc.h>
     53      1.1  itojun #include <sys/malloc.h>
     54  1.1.1.4  martti #include <sys/kthread.h>
     55  1.1.1.4  martti #include <sys/rwlock.h>
     56  1.1.1.4  martti #include <uvm/uvm_extern.h>
     57      1.1  itojun 
     58      1.1  itojun #include <net/if.h>
     59      1.1  itojun #include <net/if_types.h>
     60      1.1  itojun #include <net/route.h>
     61      1.1  itojun 
     62      1.1  itojun #include <netinet/in.h>
     63      1.1  itojun #include <netinet/in_var.h>
     64      1.1  itojun #include <netinet/in_systm.h>
     65      1.1  itojun #include <netinet/ip.h>
     66      1.1  itojun #include <netinet/ip_var.h>
     67      1.1  itojun #include <netinet/ip_icmp.h>
     68      1.1  itojun 
     69      1.1  itojun #include <dev/rndvar.h>
     70  1.1.1.4  martti #include <crypto/md5.h>
     71      1.1  itojun #include <net/pfvar.h>
     72      1.1  itojun 
     73      1.1  itojun #if NPFSYNC > 0
     74      1.1  itojun #include <net/if_pfsync.h>
     75      1.1  itojun #endif /* NPFSYNC > 0 */
     76      1.1  itojun 
     77  1.1.1.4  martti #if NPFLOG > 0
     78  1.1.1.4  martti #include <net/if_pflog.h>
     79  1.1.1.4  martti #endif /* NPFLOG > 0 */
     80  1.1.1.4  martti 
     81      1.1  itojun #ifdef INET6
     82      1.1  itojun #include <netinet/ip6.h>
     83      1.1  itojun #include <netinet/in_pcb.h>
     84      1.1  itojun #endif /* INET6 */
     85      1.1  itojun 
     86      1.1  itojun #ifdef ALTQ
     87      1.1  itojun #include <altq/altq.h>
     88      1.1  itojun #endif
     89      1.1  itojun 
     90      1.1  itojun void			 pfattach(int);
     91  1.1.1.4  martti void			 pf_thread_create(void *);
     92      1.1  itojun int			 pfopen(dev_t, int, int, struct proc *);
     93      1.1  itojun int			 pfclose(dev_t, int, int, struct proc *);
     94  1.1.1.2    yamt struct pf_pool		*pf_get_pool(char *, u_int32_t, u_int8_t, u_int32_t,
     95  1.1.1.2    yamt 			    u_int8_t, u_int8_t, u_int8_t);
     96  1.1.1.2    yamt 
     97      1.1  itojun void			 pf_mv_pool(struct pf_palist *, struct pf_palist *);
     98      1.1  itojun void			 pf_empty_pool(struct pf_palist *);
     99      1.1  itojun int			 pfioctl(dev_t, u_long, caddr_t, int, struct proc *);
    100      1.1  itojun #ifdef ALTQ
    101      1.1  itojun int			 pf_begin_altq(u_int32_t *);
    102      1.1  itojun int			 pf_rollback_altq(u_int32_t);
    103      1.1  itojun int			 pf_commit_altq(u_int32_t);
    104  1.1.1.2    yamt int			 pf_enable_altq(struct pf_altq *);
    105  1.1.1.2    yamt int			 pf_disable_altq(struct pf_altq *);
    106      1.1  itojun #endif /* ALTQ */
    107  1.1.1.2    yamt int			 pf_begin_rules(u_int32_t *, int, const char *);
    108  1.1.1.2    yamt int			 pf_rollback_rules(u_int32_t, int, char *);
    109  1.1.1.4  martti int			 pf_setup_pfsync_matching(struct pf_ruleset *);
    110  1.1.1.4  martti void			 pf_hash_rule(MD5_CTX *, struct pf_rule *);
    111  1.1.1.4  martti void			 pf_hash_rule_addr(MD5_CTX *, struct pf_rule_addr *);
    112  1.1.1.2    yamt int			 pf_commit_rules(u_int32_t, int, char *);
    113  1.1.1.4  martti void			 pf_state_export(struct pfsync_state *,
    114  1.1.1.4  martti 			    struct pf_state_key *, struct pf_state *);
    115  1.1.1.4  martti void			 pf_state_import(struct pfsync_state *,
    116  1.1.1.4  martti 			    struct pf_state_key *, struct pf_state *);
    117      1.1  itojun 
    118      1.1  itojun struct pf_rule		 pf_default_rule;
    119  1.1.1.4  martti struct rwlock		 pf_consistency_lock = RWLOCK_INITIALIZER("pfcnslk");
    120  1.1.1.2    yamt #ifdef ALTQ
    121  1.1.1.2    yamt static int		 pf_altq_running;
    122  1.1.1.2    yamt #endif
    123      1.1  itojun 
    124      1.1  itojun #define	TAGID_MAX	 50000
    125      1.1  itojun TAILQ_HEAD(pf_tags, pf_tagname)	pf_tags = TAILQ_HEAD_INITIALIZER(pf_tags),
    126      1.1  itojun 				pf_qids = TAILQ_HEAD_INITIALIZER(pf_qids);
    127      1.1  itojun 
    128      1.1  itojun #if (PF_QNAME_SIZE != PF_TAG_NAME_SIZE)
    129      1.1  itojun #error PF_QNAME_SIZE must be equal to PF_TAG_NAME_SIZE
    130      1.1  itojun #endif
    131  1.1.1.4  martti u_int16_t		 tagname2tag(struct pf_tags *, char *);
    132  1.1.1.4  martti void			 tag2tagname(struct pf_tags *, u_int16_t, char *);
    133  1.1.1.4  martti void			 tag_unref(struct pf_tags *, u_int16_t);
    134  1.1.1.3   peter int			 pf_rtlabel_add(struct pf_addr_wrap *);
    135  1.1.1.3   peter void			 pf_rtlabel_remove(struct pf_addr_wrap *);
    136  1.1.1.3   peter void			 pf_rtlabel_copyout(struct pf_addr_wrap *);
    137      1.1  itojun 
    138      1.1  itojun #define DPFPRINTF(n, x) if (pf_status.debug >= (n)) printf x
    139      1.1  itojun 
    140      1.1  itojun void
    141      1.1  itojun pfattach(int num)
    142      1.1  itojun {
    143      1.1  itojun 	u_int32_t *timeout = pf_default_rule.timeout;
    144      1.1  itojun 
    145      1.1  itojun 	pool_init(&pf_rule_pl, sizeof(struct pf_rule), 0, 0, 0, "pfrulepl",
    146      1.1  itojun 	    &pool_allocator_nointr);
    147      1.1  itojun 	pool_init(&pf_src_tree_pl, sizeof(struct pf_src_node), 0, 0, 0,
    148      1.1  itojun 	    "pfsrctrpl", NULL);
    149      1.1  itojun 	pool_init(&pf_state_pl, sizeof(struct pf_state), 0, 0, 0, "pfstatepl",
    150      1.1  itojun 	    NULL);
    151  1.1.1.4  martti 	pool_init(&pf_state_key_pl, sizeof(struct pf_state_key), 0, 0, 0,
    152  1.1.1.4  martti 	    "pfstatekeypl", NULL);
    153      1.1  itojun 	pool_init(&pf_altq_pl, sizeof(struct pf_altq), 0, 0, 0, "pfaltqpl",
    154  1.1.1.2    yamt 	    &pool_allocator_nointr);
    155      1.1  itojun 	pool_init(&pf_pooladdr_pl, sizeof(struct pf_pooladdr), 0, 0, 0,
    156  1.1.1.2    yamt 	    "pfpooladdrpl", &pool_allocator_nointr);
    157      1.1  itojun 	pfr_initialize();
    158      1.1  itojun 	pfi_initialize();
    159      1.1  itojun 	pf_osfp_initialize();
    160      1.1  itojun 
    161      1.1  itojun 	pool_sethardlimit(pf_pool_limits[PF_LIMIT_STATES].pp,
    162      1.1  itojun 	    pf_pool_limits[PF_LIMIT_STATES].limit, NULL, 0);
    163      1.1  itojun 
    164  1.1.1.4  martti 	if (ctob(physmem) <= 100*1024*1024)
    165  1.1.1.4  martti 		pf_pool_limits[PF_LIMIT_TABLE_ENTRIES].limit =
    166  1.1.1.4  martti 		    PFR_KENTRY_HIWAT_SMALL;
    167  1.1.1.4  martti 
    168      1.1  itojun 	RB_INIT(&tree_src_tracking);
    169  1.1.1.2    yamt 	RB_INIT(&pf_anchors);
    170      1.1  itojun 	pf_init_ruleset(&pf_main_ruleset);
    171      1.1  itojun 	TAILQ_INIT(&pf_altqs[0]);
    172      1.1  itojun 	TAILQ_INIT(&pf_altqs[1]);
    173      1.1  itojun 	TAILQ_INIT(&pf_pabuf);
    174      1.1  itojun 	pf_altqs_active = &pf_altqs[0];
    175      1.1  itojun 	pf_altqs_inactive = &pf_altqs[1];
    176  1.1.1.4  martti 	TAILQ_INIT(&state_list);
    177      1.1  itojun 
    178      1.1  itojun 	/* default rule should never be garbage collected */
    179      1.1  itojun 	pf_default_rule.entries.tqe_prev = &pf_default_rule.entries.tqe_next;
    180      1.1  itojun 	pf_default_rule.action = PF_PASS;
    181      1.1  itojun 	pf_default_rule.nr = -1;
    182  1.1.1.4  martti 	pf_default_rule.rtableid = -1;
    183      1.1  itojun 
    184      1.1  itojun 	/* initialize default timeouts */
    185  1.1.1.3   peter 	timeout[PFTM_TCP_FIRST_PACKET] = PFTM_TCP_FIRST_PACKET_VAL;
    186  1.1.1.3   peter 	timeout[PFTM_TCP_OPENING] = PFTM_TCP_OPENING_VAL;
    187  1.1.1.3   peter 	timeout[PFTM_TCP_ESTABLISHED] = PFTM_TCP_ESTABLISHED_VAL;
    188  1.1.1.3   peter 	timeout[PFTM_TCP_CLOSING] = PFTM_TCP_CLOSING_VAL;
    189  1.1.1.3   peter 	timeout[PFTM_TCP_FIN_WAIT] = PFTM_TCP_FIN_WAIT_VAL;
    190  1.1.1.3   peter 	timeout[PFTM_TCP_CLOSED] = PFTM_TCP_CLOSED_VAL;
    191  1.1.1.3   peter 	timeout[PFTM_UDP_FIRST_PACKET] = PFTM_UDP_FIRST_PACKET_VAL;
    192  1.1.1.3   peter 	timeout[PFTM_UDP_SINGLE] = PFTM_UDP_SINGLE_VAL;
    193  1.1.1.3   peter 	timeout[PFTM_UDP_MULTIPLE] = PFTM_UDP_MULTIPLE_VAL;
    194  1.1.1.3   peter 	timeout[PFTM_ICMP_FIRST_PACKET] = PFTM_ICMP_FIRST_PACKET_VAL;
    195  1.1.1.3   peter 	timeout[PFTM_ICMP_ERROR_REPLY] = PFTM_ICMP_ERROR_REPLY_VAL;
    196  1.1.1.3   peter 	timeout[PFTM_OTHER_FIRST_PACKET] = PFTM_OTHER_FIRST_PACKET_VAL;
    197  1.1.1.3   peter 	timeout[PFTM_OTHER_SINGLE] = PFTM_OTHER_SINGLE_VAL;
    198  1.1.1.3   peter 	timeout[PFTM_OTHER_MULTIPLE] = PFTM_OTHER_MULTIPLE_VAL;
    199  1.1.1.3   peter 	timeout[PFTM_FRAG] = PFTM_FRAG_VAL;
    200  1.1.1.3   peter 	timeout[PFTM_INTERVAL] = PFTM_INTERVAL_VAL;
    201  1.1.1.3   peter 	timeout[PFTM_SRC_NODE] = PFTM_SRC_NODE_VAL;
    202  1.1.1.3   peter 	timeout[PFTM_TS_DIFF] = PFTM_TS_DIFF_VAL;
    203  1.1.1.4  martti 	timeout[PFTM_ADAPTIVE_START] = PFSTATE_ADAPT_START;
    204  1.1.1.4  martti 	timeout[PFTM_ADAPTIVE_END] = PFSTATE_ADAPT_END;
    205      1.1  itojun 
    206      1.1  itojun 	pf_normalize_init();
    207      1.1  itojun 	bzero(&pf_status, sizeof(pf_status));
    208      1.1  itojun 	pf_status.debug = PF_DEBUG_URGENT;
    209      1.1  itojun 
    210      1.1  itojun 	/* XXX do our best to avoid a conflict */
    211      1.1  itojun 	pf_status.hostid = arc4random();
    212  1.1.1.4  martti 
    213  1.1.1.4  martti 	/* require process context to purge states, so perform in a thread */
    214  1.1.1.4  martti 	kthread_create_deferred(pf_thread_create, NULL);
    215  1.1.1.4  martti }
    216  1.1.1.4  martti 
    217  1.1.1.4  martti void
    218  1.1.1.4  martti pf_thread_create(void *v)
    219  1.1.1.4  martti {
    220  1.1.1.4  martti 	if (kthread_create(pf_purge_thread, NULL, NULL, "pfpurge"))
    221  1.1.1.4  martti 		panic("pfpurge thread");
    222      1.1  itojun }
    223      1.1  itojun 
    224      1.1  itojun int
    225      1.1  itojun pfopen(dev_t dev, int flags, int fmt, struct proc *p)
    226      1.1  itojun {
    227      1.1  itojun 	if (minor(dev) >= 1)
    228      1.1  itojun 		return (ENXIO);
    229      1.1  itojun 	return (0);
    230      1.1  itojun }
    231      1.1  itojun 
    232      1.1  itojun int
    233      1.1  itojun pfclose(dev_t dev, int flags, int fmt, struct proc *p)
    234      1.1  itojun {
    235      1.1  itojun 	if (minor(dev) >= 1)
    236      1.1  itojun 		return (ENXIO);
    237      1.1  itojun 	return (0);
    238      1.1  itojun }
    239      1.1  itojun 
    240      1.1  itojun struct pf_pool *
    241  1.1.1.2    yamt pf_get_pool(char *anchor, u_int32_t ticket, u_int8_t rule_action,
    242  1.1.1.2    yamt     u_int32_t rule_number, u_int8_t r_last, u_int8_t active,
    243  1.1.1.2    yamt     u_int8_t check_ticket)
    244      1.1  itojun {
    245      1.1  itojun 	struct pf_ruleset	*ruleset;
    246      1.1  itojun 	struct pf_rule		*rule;
    247      1.1  itojun 	int			 rs_num;
    248      1.1  itojun 
    249  1.1.1.2    yamt 	ruleset = pf_find_ruleset(anchor);
    250      1.1  itojun 	if (ruleset == NULL)
    251      1.1  itojun 		return (NULL);
    252      1.1  itojun 	rs_num = pf_get_ruleset_number(rule_action);
    253      1.1  itojun 	if (rs_num >= PF_RULESET_MAX)
    254      1.1  itojun 		return (NULL);
    255      1.1  itojun 	if (active) {
    256      1.1  itojun 		if (check_ticket && ticket !=
    257      1.1  itojun 		    ruleset->rules[rs_num].active.ticket)
    258      1.1  itojun 			return (NULL);
    259      1.1  itojun 		if (r_last)
    260      1.1  itojun 			rule = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
    261      1.1  itojun 			    pf_rulequeue);
    262      1.1  itojun 		else
    263      1.1  itojun 			rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
    264      1.1  itojun 	} else {
    265      1.1  itojun 		if (check_ticket && ticket !=
    266      1.1  itojun 		    ruleset->rules[rs_num].inactive.ticket)
    267      1.1  itojun 			return (NULL);
    268      1.1  itojun 		if (r_last)
    269      1.1  itojun 			rule = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
    270      1.1  itojun 			    pf_rulequeue);
    271      1.1  itojun 		else
    272      1.1  itojun 			rule = TAILQ_FIRST(ruleset->rules[rs_num].inactive.ptr);
    273      1.1  itojun 	}
    274      1.1  itojun 	if (!r_last) {
    275      1.1  itojun 		while ((rule != NULL) && (rule->nr != rule_number))
    276      1.1  itojun 			rule = TAILQ_NEXT(rule, entries);
    277      1.1  itojun 	}
    278      1.1  itojun 	if (rule == NULL)
    279      1.1  itojun 		return (NULL);
    280      1.1  itojun 
    281      1.1  itojun 	return (&rule->rpool);
    282      1.1  itojun }
    283      1.1  itojun 
    284      1.1  itojun void
    285      1.1  itojun pf_mv_pool(struct pf_palist *poola, struct pf_palist *poolb)
    286      1.1  itojun {
    287      1.1  itojun 	struct pf_pooladdr	*mv_pool_pa;
    288      1.1  itojun 
    289      1.1  itojun 	while ((mv_pool_pa = TAILQ_FIRST(poola)) != NULL) {
    290      1.1  itojun 		TAILQ_REMOVE(poola, mv_pool_pa, entries);
    291      1.1  itojun 		TAILQ_INSERT_TAIL(poolb, mv_pool_pa, entries);
    292      1.1  itojun 	}
    293      1.1  itojun }
    294      1.1  itojun 
    295      1.1  itojun void
    296      1.1  itojun pf_empty_pool(struct pf_palist *poola)
    297      1.1  itojun {
    298      1.1  itojun 	struct pf_pooladdr	*empty_pool_pa;
    299      1.1  itojun 
    300      1.1  itojun 	while ((empty_pool_pa = TAILQ_FIRST(poola)) != NULL) {
    301      1.1  itojun 		pfi_dynaddr_remove(&empty_pool_pa->addr);
    302      1.1  itojun 		pf_tbladdr_remove(&empty_pool_pa->addr);
    303  1.1.1.4  martti 		pfi_kif_unref(empty_pool_pa->kif, PFI_KIF_REF_RULE);
    304      1.1  itojun 		TAILQ_REMOVE(poola, empty_pool_pa, entries);
    305      1.1  itojun 		pool_put(&pf_pooladdr_pl, empty_pool_pa);
    306      1.1  itojun 	}
    307      1.1  itojun }
    308      1.1  itojun 
    309      1.1  itojun void
    310      1.1  itojun pf_rm_rule(struct pf_rulequeue *rulequeue, struct pf_rule *rule)
    311      1.1  itojun {
    312      1.1  itojun 	if (rulequeue != NULL) {
    313      1.1  itojun 		if (rule->states <= 0) {
    314      1.1  itojun 			/*
    315      1.1  itojun 			 * XXX - we need to remove the table *before* detaching
    316      1.1  itojun 			 * the rule to make sure the table code does not delete
    317      1.1  itojun 			 * the anchor under our feet.
    318      1.1  itojun 			 */
    319      1.1  itojun 			pf_tbladdr_remove(&rule->src.addr);
    320      1.1  itojun 			pf_tbladdr_remove(&rule->dst.addr);
    321  1.1.1.3   peter 			if (rule->overload_tbl)
    322  1.1.1.3   peter 				pfr_detach_table(rule->overload_tbl);
    323      1.1  itojun 		}
    324      1.1  itojun 		TAILQ_REMOVE(rulequeue, rule, entries);
    325      1.1  itojun 		rule->entries.tqe_prev = NULL;
    326      1.1  itojun 		rule->nr = -1;
    327      1.1  itojun 	}
    328      1.1  itojun 
    329      1.1  itojun 	if (rule->states > 0 || rule->src_nodes > 0 ||
    330      1.1  itojun 	    rule->entries.tqe_prev != NULL)
    331      1.1  itojun 		return;
    332      1.1  itojun 	pf_tag_unref(rule->tag);
    333      1.1  itojun 	pf_tag_unref(rule->match_tag);
    334      1.1  itojun #ifdef ALTQ
    335      1.1  itojun 	if (rule->pqid != rule->qid)
    336      1.1  itojun 		pf_qid_unref(rule->pqid);
    337      1.1  itojun 	pf_qid_unref(rule->qid);
    338      1.1  itojun #endif
    339  1.1.1.3   peter 	pf_rtlabel_remove(&rule->src.addr);
    340  1.1.1.3   peter 	pf_rtlabel_remove(&rule->dst.addr);
    341      1.1  itojun 	pfi_dynaddr_remove(&rule->src.addr);
    342      1.1  itojun 	pfi_dynaddr_remove(&rule->dst.addr);
    343      1.1  itojun 	if (rulequeue == NULL) {
    344      1.1  itojun 		pf_tbladdr_remove(&rule->src.addr);
    345      1.1  itojun 		pf_tbladdr_remove(&rule->dst.addr);
    346  1.1.1.3   peter 		if (rule->overload_tbl)
    347  1.1.1.3   peter 			pfr_detach_table(rule->overload_tbl);
    348      1.1  itojun 	}
    349  1.1.1.4  martti 	pfi_kif_unref(rule->kif, PFI_KIF_REF_RULE);
    350  1.1.1.2    yamt 	pf_anchor_remove(rule);
    351      1.1  itojun 	pf_empty_pool(&rule->rpool.list);
    352      1.1  itojun 	pool_put(&pf_rule_pl, rule);
    353      1.1  itojun }
    354      1.1  itojun 
    355  1.1.1.4  martti u_int16_t
    356      1.1  itojun tagname2tag(struct pf_tags *head, char *tagname)
    357      1.1  itojun {
    358      1.1  itojun 	struct pf_tagname	*tag, *p = NULL;
    359      1.1  itojun 	u_int16_t		 new_tagid = 1;
    360      1.1  itojun 
    361      1.1  itojun 	TAILQ_FOREACH(tag, head, entries)
    362      1.1  itojun 		if (strcmp(tagname, tag->name) == 0) {
    363      1.1  itojun 			tag->ref++;
    364      1.1  itojun 			return (tag->tag);
    365      1.1  itojun 		}
    366      1.1  itojun 
    367      1.1  itojun 	/*
    368      1.1  itojun 	 * to avoid fragmentation, we do a linear search from the beginning
    369      1.1  itojun 	 * and take the first free slot we find. if there is none or the list
    370      1.1  itojun 	 * is empty, append a new entry at the end.
    371      1.1  itojun 	 */
    372      1.1  itojun 
    373      1.1  itojun 	/* new entry */
    374      1.1  itojun 	if (!TAILQ_EMPTY(head))
    375      1.1  itojun 		for (p = TAILQ_FIRST(head); p != NULL &&
    376      1.1  itojun 		    p->tag == new_tagid; p = TAILQ_NEXT(p, entries))
    377      1.1  itojun 			new_tagid = p->tag + 1;
    378      1.1  itojun 
    379      1.1  itojun 	if (new_tagid > TAGID_MAX)
    380      1.1  itojun 		return (0);
    381      1.1  itojun 
    382      1.1  itojun 	/* allocate and fill new struct pf_tagname */
    383      1.1  itojun 	tag = (struct pf_tagname *)malloc(sizeof(struct pf_tagname),
    384      1.1  itojun 	    M_TEMP, M_NOWAIT);
    385      1.1  itojun 	if (tag == NULL)
    386      1.1  itojun 		return (0);
    387      1.1  itojun 	bzero(tag, sizeof(struct pf_tagname));
    388      1.1  itojun 	strlcpy(tag->name, tagname, sizeof(tag->name));
    389      1.1  itojun 	tag->tag = new_tagid;
    390      1.1  itojun 	tag->ref++;
    391      1.1  itojun 
    392      1.1  itojun 	if (p != NULL)	/* insert new entry before p */
    393      1.1  itojun 		TAILQ_INSERT_BEFORE(p, tag, entries);
    394      1.1  itojun 	else	/* either list empty or no free slot in between */
    395      1.1  itojun 		TAILQ_INSERT_TAIL(head, tag, entries);
    396      1.1  itojun 
    397      1.1  itojun 	return (tag->tag);
    398      1.1  itojun }
    399      1.1  itojun 
    400  1.1.1.4  martti void
    401      1.1  itojun tag2tagname(struct pf_tags *head, u_int16_t tagid, char *p)
    402      1.1  itojun {
    403      1.1  itojun 	struct pf_tagname	*tag;
    404      1.1  itojun 
    405      1.1  itojun 	TAILQ_FOREACH(tag, head, entries)
    406      1.1  itojun 		if (tag->tag == tagid) {
    407      1.1  itojun 			strlcpy(p, tag->name, PF_TAG_NAME_SIZE);
    408      1.1  itojun 			return;
    409      1.1  itojun 		}
    410      1.1  itojun }
    411      1.1  itojun 
    412  1.1.1.4  martti void
    413      1.1  itojun tag_unref(struct pf_tags *head, u_int16_t tag)
    414      1.1  itojun {
    415      1.1  itojun 	struct pf_tagname	*p, *next;
    416      1.1  itojun 
    417      1.1  itojun 	if (tag == 0)
    418      1.1  itojun 		return;
    419      1.1  itojun 
    420      1.1  itojun 	for (p = TAILQ_FIRST(head); p != NULL; p = next) {
    421      1.1  itojun 		next = TAILQ_NEXT(p, entries);
    422      1.1  itojun 		if (tag == p->tag) {
    423      1.1  itojun 			if (--p->ref == 0) {
    424      1.1  itojun 				TAILQ_REMOVE(head, p, entries);
    425      1.1  itojun 				free(p, M_TEMP);
    426      1.1  itojun 			}
    427      1.1  itojun 			break;
    428      1.1  itojun 		}
    429      1.1  itojun 	}
    430      1.1  itojun }
    431      1.1  itojun 
    432      1.1  itojun u_int16_t
    433      1.1  itojun pf_tagname2tag(char *tagname)
    434      1.1  itojun {
    435      1.1  itojun 	return (tagname2tag(&pf_tags, tagname));
    436      1.1  itojun }
    437      1.1  itojun 
    438      1.1  itojun void
    439      1.1  itojun pf_tag2tagname(u_int16_t tagid, char *p)
    440      1.1  itojun {
    441  1.1.1.4  martti 	tag2tagname(&pf_tags, tagid, p);
    442      1.1  itojun }
    443      1.1  itojun 
    444      1.1  itojun void
    445  1.1.1.3   peter pf_tag_ref(u_int16_t tag)
    446  1.1.1.3   peter {
    447  1.1.1.3   peter 	struct pf_tagname *t;
    448  1.1.1.3   peter 
    449  1.1.1.3   peter 	TAILQ_FOREACH(t, &pf_tags, entries)
    450  1.1.1.3   peter 		if (t->tag == tag)
    451  1.1.1.3   peter 			break;
    452  1.1.1.3   peter 	if (t != NULL)
    453  1.1.1.3   peter 		t->ref++;
    454  1.1.1.3   peter }
    455  1.1.1.3   peter 
    456  1.1.1.3   peter void
    457      1.1  itojun pf_tag_unref(u_int16_t tag)
    458      1.1  itojun {
    459  1.1.1.4  martti 	tag_unref(&pf_tags, tag);
    460      1.1  itojun }
    461      1.1  itojun 
    462  1.1.1.3   peter int
    463  1.1.1.3   peter pf_rtlabel_add(struct pf_addr_wrap *a)
    464  1.1.1.3   peter {
    465  1.1.1.3   peter 	if (a->type == PF_ADDR_RTLABEL &&
    466  1.1.1.3   peter 	    (a->v.rtlabel = rtlabel_name2id(a->v.rtlabelname)) == 0)
    467  1.1.1.3   peter 		return (-1);
    468  1.1.1.3   peter 	return (0);
    469  1.1.1.3   peter }
    470  1.1.1.3   peter 
    471  1.1.1.3   peter void
    472  1.1.1.3   peter pf_rtlabel_remove(struct pf_addr_wrap *a)
    473  1.1.1.3   peter {
    474  1.1.1.3   peter 	if (a->type == PF_ADDR_RTLABEL)
    475  1.1.1.3   peter 		rtlabel_unref(a->v.rtlabel);
    476  1.1.1.3   peter }
    477  1.1.1.3   peter 
    478  1.1.1.3   peter void
    479  1.1.1.3   peter pf_rtlabel_copyout(struct pf_addr_wrap *a)
    480  1.1.1.3   peter {
    481  1.1.1.3   peter 	const char	*name;
    482  1.1.1.3   peter 
    483  1.1.1.3   peter 	if (a->type == PF_ADDR_RTLABEL && a->v.rtlabel) {
    484  1.1.1.3   peter 		if ((name = rtlabel_id2name(a->v.rtlabel)) == NULL)
    485  1.1.1.3   peter 			strlcpy(a->v.rtlabelname, "?",
    486  1.1.1.3   peter 			    sizeof(a->v.rtlabelname));
    487  1.1.1.3   peter 		else
    488  1.1.1.3   peter 			strlcpy(a->v.rtlabelname, name,
    489  1.1.1.3   peter 			    sizeof(a->v.rtlabelname));
    490  1.1.1.3   peter 	}
    491  1.1.1.3   peter }
    492  1.1.1.3   peter 
    493      1.1  itojun #ifdef ALTQ
    494      1.1  itojun u_int32_t
    495      1.1  itojun pf_qname2qid(char *qname)
    496      1.1  itojun {
    497      1.1  itojun 	return ((u_int32_t)tagname2tag(&pf_qids, qname));
    498      1.1  itojun }
    499      1.1  itojun 
    500      1.1  itojun void
    501      1.1  itojun pf_qid2qname(u_int32_t qid, char *p)
    502      1.1  itojun {
    503  1.1.1.4  martti 	tag2tagname(&pf_qids, (u_int16_t)qid, p);
    504      1.1  itojun }
    505      1.1  itojun 
    506      1.1  itojun void
    507      1.1  itojun pf_qid_unref(u_int32_t qid)
    508      1.1  itojun {
    509  1.1.1.4  martti 	tag_unref(&pf_qids, (u_int16_t)qid);
    510      1.1  itojun }
    511      1.1  itojun 
    512      1.1  itojun int
    513      1.1  itojun pf_begin_altq(u_int32_t *ticket)
    514      1.1  itojun {
    515      1.1  itojun 	struct pf_altq	*altq;
    516      1.1  itojun 	int		 error = 0;
    517      1.1  itojun 
    518      1.1  itojun 	/* Purge the old altq list */
    519      1.1  itojun 	while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
    520      1.1  itojun 		TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
    521      1.1  itojun 		if (altq->qname[0] == 0) {
    522      1.1  itojun 			/* detach and destroy the discipline */
    523      1.1  itojun 			error = altq_remove(altq);
    524      1.1  itojun 		} else
    525      1.1  itojun 			pf_qid_unref(altq->qid);
    526      1.1  itojun 		pool_put(&pf_altq_pl, altq);
    527      1.1  itojun 	}
    528      1.1  itojun 	if (error)
    529      1.1  itojun 		return (error);
    530      1.1  itojun 	*ticket = ++ticket_altqs_inactive;
    531      1.1  itojun 	altqs_inactive_open = 1;
    532      1.1  itojun 	return (0);
    533      1.1  itojun }
    534      1.1  itojun 
    535      1.1  itojun int
    536      1.1  itojun pf_rollback_altq(u_int32_t ticket)
    537      1.1  itojun {
    538      1.1  itojun 	struct pf_altq	*altq;
    539      1.1  itojun 	int		 error = 0;
    540      1.1  itojun 
    541      1.1  itojun 	if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
    542      1.1  itojun 		return (0);
    543      1.1  itojun 	/* Purge the old altq list */
    544      1.1  itojun 	while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
    545      1.1  itojun 		TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
    546      1.1  itojun 		if (altq->qname[0] == 0) {
    547      1.1  itojun 			/* detach and destroy the discipline */
    548      1.1  itojun 			error = altq_remove(altq);
    549      1.1  itojun 		} else
    550      1.1  itojun 			pf_qid_unref(altq->qid);
    551      1.1  itojun 		pool_put(&pf_altq_pl, altq);
    552      1.1  itojun 	}
    553      1.1  itojun 	altqs_inactive_open = 0;
    554      1.1  itojun 	return (error);
    555      1.1  itojun }
    556      1.1  itojun 
    557      1.1  itojun int
    558      1.1  itojun pf_commit_altq(u_int32_t ticket)
    559      1.1  itojun {
    560      1.1  itojun 	struct pf_altqqueue	*old_altqs;
    561      1.1  itojun 	struct pf_altq		*altq;
    562      1.1  itojun 	int			 s, err, error = 0;
    563      1.1  itojun 
    564      1.1  itojun 	if (!altqs_inactive_open || ticket != ticket_altqs_inactive)
    565      1.1  itojun 		return (EBUSY);
    566      1.1  itojun 
    567      1.1  itojun 	/* swap altqs, keep the old. */
    568      1.1  itojun 	s = splsoftnet();
    569      1.1  itojun 	old_altqs = pf_altqs_active;
    570      1.1  itojun 	pf_altqs_active = pf_altqs_inactive;
    571      1.1  itojun 	pf_altqs_inactive = old_altqs;
    572      1.1  itojun 	ticket_altqs_active = ticket_altqs_inactive;
    573      1.1  itojun 
    574      1.1  itojun 	/* Attach new disciplines */
    575      1.1  itojun 	TAILQ_FOREACH(altq, pf_altqs_active, entries) {
    576      1.1  itojun 		if (altq->qname[0] == 0) {
    577      1.1  itojun 			/* attach the discipline */
    578      1.1  itojun 			error = altq_pfattach(altq);
    579  1.1.1.2    yamt 			if (error == 0 && pf_altq_running)
    580  1.1.1.2    yamt 				error = pf_enable_altq(altq);
    581  1.1.1.2    yamt 			if (error != 0) {
    582      1.1  itojun 				splx(s);
    583      1.1  itojun 				return (error);
    584      1.1  itojun 			}
    585      1.1  itojun 		}
    586      1.1  itojun 	}
    587      1.1  itojun 
    588      1.1  itojun 	/* Purge the old altq list */
    589      1.1  itojun 	while ((altq = TAILQ_FIRST(pf_altqs_inactive)) != NULL) {
    590      1.1  itojun 		TAILQ_REMOVE(pf_altqs_inactive, altq, entries);
    591      1.1  itojun 		if (altq->qname[0] == 0) {
    592      1.1  itojun 			/* detach and destroy the discipline */
    593  1.1.1.2    yamt 			if (pf_altq_running)
    594  1.1.1.2    yamt 				error = pf_disable_altq(altq);
    595      1.1  itojun 			err = altq_pfdetach(altq);
    596      1.1  itojun 			if (err != 0 && error == 0)
    597      1.1  itojun 				error = err;
    598      1.1  itojun 			err = altq_remove(altq);
    599      1.1  itojun 			if (err != 0 && error == 0)
    600      1.1  itojun 				error = err;
    601      1.1  itojun 		} else
    602      1.1  itojun 			pf_qid_unref(altq->qid);
    603      1.1  itojun 		pool_put(&pf_altq_pl, altq);
    604      1.1  itojun 	}
    605      1.1  itojun 	splx(s);
    606      1.1  itojun 
    607      1.1  itojun 	altqs_inactive_open = 0;
    608      1.1  itojun 	return (error);
    609      1.1  itojun }
    610  1.1.1.2    yamt 
    611  1.1.1.2    yamt int
    612  1.1.1.2    yamt pf_enable_altq(struct pf_altq *altq)
    613  1.1.1.2    yamt {
    614  1.1.1.2    yamt 	struct ifnet		*ifp;
    615  1.1.1.2    yamt 	struct tb_profile	 tb;
    616  1.1.1.2    yamt 	int			 s, error = 0;
    617  1.1.1.2    yamt 
    618  1.1.1.2    yamt 	if ((ifp = ifunit(altq->ifname)) == NULL)
    619  1.1.1.2    yamt 		return (EINVAL);
    620  1.1.1.2    yamt 
    621  1.1.1.2    yamt 	if (ifp->if_snd.altq_type != ALTQT_NONE)
    622  1.1.1.2    yamt 		error = altq_enable(&ifp->if_snd);
    623  1.1.1.2    yamt 
    624  1.1.1.2    yamt 	/* set tokenbucket regulator */
    625  1.1.1.2    yamt 	if (error == 0 && ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
    626  1.1.1.2    yamt 		tb.rate = altq->ifbandwidth;
    627  1.1.1.2    yamt 		tb.depth = altq->tbrsize;
    628  1.1.1.4  martti 		s = splnet();
    629  1.1.1.2    yamt 		error = tbr_set(&ifp->if_snd, &tb);
    630  1.1.1.2    yamt 		splx(s);
    631  1.1.1.2    yamt 	}
    632  1.1.1.2    yamt 
    633  1.1.1.2    yamt 	return (error);
    634  1.1.1.2    yamt }
    635  1.1.1.2    yamt 
    636  1.1.1.2    yamt int
    637  1.1.1.2    yamt pf_disable_altq(struct pf_altq *altq)
    638  1.1.1.2    yamt {
    639  1.1.1.2    yamt 	struct ifnet		*ifp;
    640  1.1.1.2    yamt 	struct tb_profile	 tb;
    641  1.1.1.2    yamt 	int			 s, error;
    642  1.1.1.2    yamt 
    643  1.1.1.2    yamt 	if ((ifp = ifunit(altq->ifname)) == NULL)
    644  1.1.1.2    yamt 		return (EINVAL);
    645  1.1.1.2    yamt 
    646  1.1.1.2    yamt 	/*
    647  1.1.1.2    yamt 	 * when the discipline is no longer referenced, it was overridden
    648  1.1.1.2    yamt 	 * by a new one.  if so, just return.
    649  1.1.1.2    yamt 	 */
    650  1.1.1.2    yamt 	if (altq->altq_disc != ifp->if_snd.altq_disc)
    651  1.1.1.2    yamt 		return (0);
    652  1.1.1.2    yamt 
    653  1.1.1.2    yamt 	error = altq_disable(&ifp->if_snd);
    654  1.1.1.2    yamt 
    655  1.1.1.2    yamt 	if (error == 0) {
    656  1.1.1.2    yamt 		/* clear tokenbucket regulator */
    657  1.1.1.2    yamt 		tb.rate = 0;
    658  1.1.1.4  martti 		s = splnet();
    659  1.1.1.2    yamt 		error = tbr_set(&ifp->if_snd, &tb);
    660  1.1.1.2    yamt 		splx(s);
    661  1.1.1.2    yamt 	}
    662  1.1.1.2    yamt 
    663  1.1.1.2    yamt 	return (error);
    664  1.1.1.2    yamt }
    665      1.1  itojun #endif /* ALTQ */
    666      1.1  itojun 
    667      1.1  itojun int
    668  1.1.1.2    yamt pf_begin_rules(u_int32_t *ticket, int rs_num, const char *anchor)
    669      1.1  itojun {
    670      1.1  itojun 	struct pf_ruleset	*rs;
    671      1.1  itojun 	struct pf_rule		*rule;
    672      1.1  itojun 
    673      1.1  itojun 	if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
    674      1.1  itojun 		return (EINVAL);
    675  1.1.1.2    yamt 	rs = pf_find_or_create_ruleset(anchor);
    676      1.1  itojun 	if (rs == NULL)
    677      1.1  itojun 		return (EINVAL);
    678  1.1.1.4  martti 	while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
    679      1.1  itojun 		pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
    680  1.1.1.4  martti 		rs->rules[rs_num].inactive.rcount--;
    681  1.1.1.4  martti 	}
    682      1.1  itojun 	*ticket = ++rs->rules[rs_num].inactive.ticket;
    683      1.1  itojun 	rs->rules[rs_num].inactive.open = 1;
    684      1.1  itojun 	return (0);
    685      1.1  itojun }
    686      1.1  itojun 
    687      1.1  itojun int
    688  1.1.1.2    yamt pf_rollback_rules(u_int32_t ticket, int rs_num, char *anchor)
    689      1.1  itojun {
    690      1.1  itojun 	struct pf_ruleset	*rs;
    691      1.1  itojun 	struct pf_rule		*rule;
    692      1.1  itojun 
    693      1.1  itojun 	if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
    694      1.1  itojun 		return (EINVAL);
    695  1.1.1.2    yamt 	rs = pf_find_ruleset(anchor);
    696      1.1  itojun 	if (rs == NULL || !rs->rules[rs_num].inactive.open ||
    697      1.1  itojun 	    rs->rules[rs_num].inactive.ticket != ticket)
    698      1.1  itojun 		return (0);
    699  1.1.1.4  martti 	while ((rule = TAILQ_FIRST(rs->rules[rs_num].inactive.ptr)) != NULL) {
    700      1.1  itojun 		pf_rm_rule(rs->rules[rs_num].inactive.ptr, rule);
    701  1.1.1.4  martti 		rs->rules[rs_num].inactive.rcount--;
    702  1.1.1.4  martti 	}
    703      1.1  itojun 	rs->rules[rs_num].inactive.open = 0;
    704      1.1  itojun 	return (0);
    705      1.1  itojun }
    706      1.1  itojun 
    707  1.1.1.4  martti #define PF_MD5_UPD(st, elm)						\
    708  1.1.1.4  martti 		MD5Update(ctx, (u_int8_t *) &(st)->elm, sizeof((st)->elm))
    709  1.1.1.4  martti 
    710  1.1.1.4  martti #define PF_MD5_UPD_STR(st, elm)						\
    711  1.1.1.4  martti 		MD5Update(ctx, (u_int8_t *) (st)->elm, strlen((st)->elm))
    712  1.1.1.4  martti 
    713  1.1.1.4  martti #define PF_MD5_UPD_HTONL(st, elm, stor) do {				\
    714  1.1.1.4  martti 		(stor) = htonl((st)->elm);				\
    715  1.1.1.4  martti 		MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int32_t));\
    716  1.1.1.4  martti } while (0)
    717  1.1.1.4  martti 
    718  1.1.1.4  martti #define PF_MD5_UPD_HTONS(st, elm, stor) do {				\
    719  1.1.1.4  martti 		(stor) = htons((st)->elm);				\
    720  1.1.1.4  martti 		MD5Update(ctx, (u_int8_t *) &(stor), sizeof(u_int16_t));\
    721  1.1.1.4  martti } while (0)
    722  1.1.1.4  martti 
    723  1.1.1.4  martti void
    724  1.1.1.4  martti pf_hash_rule_addr(MD5_CTX *ctx, struct pf_rule_addr *pfr)
    725  1.1.1.4  martti {
    726  1.1.1.4  martti 	PF_MD5_UPD(pfr, addr.type);
    727  1.1.1.4  martti 	switch (pfr->addr.type) {
    728  1.1.1.4  martti 		case PF_ADDR_DYNIFTL:
    729  1.1.1.4  martti 			PF_MD5_UPD(pfr, addr.v.ifname);
    730  1.1.1.4  martti 			PF_MD5_UPD(pfr, addr.iflags);
    731  1.1.1.4  martti 			break;
    732  1.1.1.4  martti 		case PF_ADDR_TABLE:
    733  1.1.1.4  martti 			PF_MD5_UPD(pfr, addr.v.tblname);
    734  1.1.1.4  martti 			break;
    735  1.1.1.4  martti 		case PF_ADDR_ADDRMASK:
    736  1.1.1.4  martti 			/* XXX ignore af? */
    737  1.1.1.4  martti 			PF_MD5_UPD(pfr, addr.v.a.addr.addr32);
    738  1.1.1.4  martti 			PF_MD5_UPD(pfr, addr.v.a.mask.addr32);
    739  1.1.1.4  martti 			break;
    740  1.1.1.4  martti 		case PF_ADDR_RTLABEL:
    741  1.1.1.4  martti 			PF_MD5_UPD(pfr, addr.v.rtlabelname);
    742  1.1.1.4  martti 			break;
    743  1.1.1.4  martti 	}
    744  1.1.1.4  martti 
    745  1.1.1.4  martti 	PF_MD5_UPD(pfr, port[0]);
    746  1.1.1.4  martti 	PF_MD5_UPD(pfr, port[1]);
    747  1.1.1.4  martti 	PF_MD5_UPD(pfr, neg);
    748  1.1.1.4  martti 	PF_MD5_UPD(pfr, port_op);
    749  1.1.1.4  martti }
    750  1.1.1.4  martti 
    751  1.1.1.4  martti void
    752  1.1.1.4  martti pf_hash_rule(MD5_CTX *ctx, struct pf_rule *rule)
    753  1.1.1.4  martti {
    754  1.1.1.4  martti 	u_int16_t x;
    755  1.1.1.4  martti 	u_int32_t y;
    756  1.1.1.4  martti 
    757  1.1.1.4  martti 	pf_hash_rule_addr(ctx, &rule->src);
    758  1.1.1.4  martti 	pf_hash_rule_addr(ctx, &rule->dst);
    759  1.1.1.4  martti 	PF_MD5_UPD_STR(rule, label);
    760  1.1.1.4  martti 	PF_MD5_UPD_STR(rule, ifname);
    761  1.1.1.4  martti 	PF_MD5_UPD_STR(rule, match_tagname);
    762  1.1.1.4  martti 	PF_MD5_UPD_HTONS(rule, match_tag, x); /* dup? */
    763  1.1.1.4  martti 	PF_MD5_UPD_HTONL(rule, os_fingerprint, y);
    764  1.1.1.4  martti 	PF_MD5_UPD_HTONL(rule, prob, y);
    765  1.1.1.4  martti 	PF_MD5_UPD_HTONL(rule, uid.uid[0], y);
    766  1.1.1.4  martti 	PF_MD5_UPD_HTONL(rule, uid.uid[1], y);
    767  1.1.1.4  martti 	PF_MD5_UPD(rule, uid.op);
    768  1.1.1.4  martti 	PF_MD5_UPD_HTONL(rule, gid.gid[0], y);
    769  1.1.1.4  martti 	PF_MD5_UPD_HTONL(rule, gid.gid[1], y);
    770  1.1.1.4  martti 	PF_MD5_UPD(rule, gid.op);
    771  1.1.1.4  martti 	PF_MD5_UPD_HTONL(rule, rule_flag, y);
    772  1.1.1.4  martti 	PF_MD5_UPD(rule, action);
    773  1.1.1.4  martti 	PF_MD5_UPD(rule, direction);
    774  1.1.1.4  martti 	PF_MD5_UPD(rule, af);
    775  1.1.1.4  martti 	PF_MD5_UPD(rule, quick);
    776  1.1.1.4  martti 	PF_MD5_UPD(rule, ifnot);
    777  1.1.1.4  martti 	PF_MD5_UPD(rule, match_tag_not);
    778  1.1.1.4  martti 	PF_MD5_UPD(rule, natpass);
    779  1.1.1.4  martti 	PF_MD5_UPD(rule, keep_state);
    780  1.1.1.4  martti 	PF_MD5_UPD(rule, proto);
    781  1.1.1.4  martti 	PF_MD5_UPD(rule, type);
    782  1.1.1.4  martti 	PF_MD5_UPD(rule, code);
    783  1.1.1.4  martti 	PF_MD5_UPD(rule, flags);
    784  1.1.1.4  martti 	PF_MD5_UPD(rule, flagset);
    785  1.1.1.4  martti 	PF_MD5_UPD(rule, allow_opts);
    786  1.1.1.4  martti 	PF_MD5_UPD(rule, rt);
    787  1.1.1.4  martti 	PF_MD5_UPD(rule, tos);
    788  1.1.1.4  martti }
    789  1.1.1.4  martti 
    790      1.1  itojun int
    791  1.1.1.2    yamt pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
    792      1.1  itojun {
    793      1.1  itojun 	struct pf_ruleset	*rs;
    794  1.1.1.4  martti 	struct pf_rule		*rule, **old_array;
    795      1.1  itojun 	struct pf_rulequeue	*old_rules;
    796  1.1.1.4  martti 	int			 s, error;
    797  1.1.1.4  martti 	u_int32_t		 old_rcount;
    798      1.1  itojun 
    799      1.1  itojun 	if (rs_num < 0 || rs_num >= PF_RULESET_MAX)
    800      1.1  itojun 		return (EINVAL);
    801  1.1.1.2    yamt 	rs = pf_find_ruleset(anchor);
    802      1.1  itojun 	if (rs == NULL || !rs->rules[rs_num].inactive.open ||
    803      1.1  itojun 	    ticket != rs->rules[rs_num].inactive.ticket)
    804      1.1  itojun 		return (EBUSY);
    805      1.1  itojun 
    806  1.1.1.4  martti 	/* Calculate checksum for the main ruleset */
    807  1.1.1.4  martti 	if (rs == &pf_main_ruleset) {
    808  1.1.1.4  martti 		error = pf_setup_pfsync_matching(rs);
    809  1.1.1.4  martti 		if (error != 0)
    810  1.1.1.4  martti 			return (error);
    811  1.1.1.4  martti 	}
    812  1.1.1.4  martti 
    813      1.1  itojun 	/* Swap rules, keep the old. */
    814      1.1  itojun 	s = splsoftnet();
    815      1.1  itojun 	old_rules = rs->rules[rs_num].active.ptr;
    816  1.1.1.4  martti 	old_rcount = rs->rules[rs_num].active.rcount;
    817  1.1.1.4  martti 	old_array = rs->rules[rs_num].active.ptr_array;
    818  1.1.1.4  martti 
    819      1.1  itojun 	rs->rules[rs_num].active.ptr =
    820      1.1  itojun 	    rs->rules[rs_num].inactive.ptr;
    821  1.1.1.4  martti 	rs->rules[rs_num].active.ptr_array =
    822  1.1.1.4  martti 	    rs->rules[rs_num].inactive.ptr_array;
    823  1.1.1.4  martti 	rs->rules[rs_num].active.rcount =
    824  1.1.1.4  martti 	    rs->rules[rs_num].inactive.rcount;
    825      1.1  itojun 	rs->rules[rs_num].inactive.ptr = old_rules;
    826  1.1.1.4  martti 	rs->rules[rs_num].inactive.ptr_array = old_array;
    827  1.1.1.4  martti 	rs->rules[rs_num].inactive.rcount = old_rcount;
    828  1.1.1.4  martti 
    829      1.1  itojun 	rs->rules[rs_num].active.ticket =
    830      1.1  itojun 	    rs->rules[rs_num].inactive.ticket;
    831      1.1  itojun 	pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
    832      1.1  itojun 
    833  1.1.1.4  martti 
    834      1.1  itojun 	/* Purge the old rule list. */
    835      1.1  itojun 	while ((rule = TAILQ_FIRST(old_rules)) != NULL)
    836      1.1  itojun 		pf_rm_rule(old_rules, rule);
    837  1.1.1.4  martti 	if (rs->rules[rs_num].inactive.ptr_array)
    838  1.1.1.4  martti 		free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
    839  1.1.1.4  martti 	rs->rules[rs_num].inactive.ptr_array = NULL;
    840  1.1.1.4  martti 	rs->rules[rs_num].inactive.rcount = 0;
    841      1.1  itojun 	rs->rules[rs_num].inactive.open = 0;
    842      1.1  itojun 	pf_remove_if_empty_ruleset(rs);
    843      1.1  itojun 	splx(s);
    844      1.1  itojun 	return (0);
    845      1.1  itojun }
    846      1.1  itojun 
    847  1.1.1.4  martti void
    848  1.1.1.4  martti pf_state_export(struct pfsync_state *sp, struct pf_state_key *sk,
    849  1.1.1.4  martti    struct pf_state *s)
    850  1.1.1.4  martti {
    851  1.1.1.4  martti 	int secs = time_second;
    852  1.1.1.4  martti 	bzero(sp, sizeof(struct pfsync_state));
    853  1.1.1.4  martti 
    854  1.1.1.4  martti 	/* copy from state key */
    855  1.1.1.4  martti 	sp->lan.addr = sk->lan.addr;
    856  1.1.1.4  martti 	sp->lan.port = sk->lan.port;
    857  1.1.1.4  martti 	sp->gwy.addr = sk->gwy.addr;
    858  1.1.1.4  martti 	sp->gwy.port = sk->gwy.port;
    859  1.1.1.4  martti 	sp->ext.addr = sk->ext.addr;
    860  1.1.1.4  martti 	sp->ext.port = sk->ext.port;
    861  1.1.1.4  martti 	sp->proto = sk->proto;
    862  1.1.1.4  martti 	sp->af = sk->af;
    863  1.1.1.4  martti 	sp->direction = sk->direction;
    864  1.1.1.4  martti 
    865  1.1.1.4  martti 	/* copy from state */
    866  1.1.1.4  martti 	memcpy(&sp->id, &s->id, sizeof(sp->id));
    867  1.1.1.4  martti 	sp->creatorid = s->creatorid;
    868  1.1.1.4  martti 	strlcpy(sp->ifname, s->kif->pfik_name, sizeof(sp->ifname));
    869  1.1.1.4  martti 	pf_state_peer_to_pfsync(&s->src, &sp->src);
    870  1.1.1.4  martti 	pf_state_peer_to_pfsync(&s->dst, &sp->dst);
    871  1.1.1.4  martti 
    872  1.1.1.4  martti 	sp->rule = s->rule.ptr->nr;
    873  1.1.1.4  martti 	sp->nat_rule = (s->nat_rule.ptr == NULL) ?  -1 : s->nat_rule.ptr->nr;
    874  1.1.1.4  martti 	sp->anchor = (s->anchor.ptr == NULL) ?  -1 : s->anchor.ptr->nr;
    875  1.1.1.4  martti 
    876  1.1.1.4  martti 	pf_state_counter_to_pfsync(s->bytes[0], sp->bytes[0]);
    877  1.1.1.4  martti 	pf_state_counter_to_pfsync(s->bytes[1], sp->bytes[1]);
    878  1.1.1.4  martti 	pf_state_counter_to_pfsync(s->packets[0], sp->packets[0]);
    879  1.1.1.4  martti 	pf_state_counter_to_pfsync(s->packets[1], sp->packets[1]);
    880  1.1.1.4  martti 	sp->creation = secs - s->creation;
    881  1.1.1.4  martti 	sp->expire = pf_state_expires(s);
    882  1.1.1.4  martti 	sp->log = s->log;
    883  1.1.1.4  martti 	sp->allow_opts = s->allow_opts;
    884  1.1.1.4  martti 	sp->timeout = s->timeout;
    885  1.1.1.4  martti 
    886  1.1.1.4  martti 	if (s->src_node)
    887  1.1.1.4  martti 		sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
    888  1.1.1.4  martti 	if (s->nat_src_node)
    889  1.1.1.4  martti 		sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
    890  1.1.1.4  martti 
    891  1.1.1.4  martti 	if (sp->expire > secs)
    892  1.1.1.4  martti 		sp->expire -= secs;
    893  1.1.1.4  martti 	else
    894  1.1.1.4  martti 		sp->expire = 0;
    895  1.1.1.4  martti 
    896  1.1.1.4  martti }
    897  1.1.1.4  martti 
    898  1.1.1.4  martti void
    899  1.1.1.4  martti pf_state_import(struct pfsync_state *sp, struct pf_state_key *sk,
    900  1.1.1.4  martti    struct pf_state *s)
    901  1.1.1.4  martti {
    902  1.1.1.4  martti 	/* copy to state key */
    903  1.1.1.4  martti 	sk->lan.addr = sp->lan.addr;
    904  1.1.1.4  martti 	sk->lan.port = sp->lan.port;
    905  1.1.1.4  martti 	sk->gwy.addr = sp->gwy.addr;
    906  1.1.1.4  martti 	sk->gwy.port = sp->gwy.port;
    907  1.1.1.4  martti 	sk->ext.addr = sp->ext.addr;
    908  1.1.1.4  martti 	sk->ext.port = sp->ext.port;
    909  1.1.1.4  martti 	sk->proto = sp->proto;
    910  1.1.1.4  martti 	sk->af = sp->af;
    911  1.1.1.4  martti 	sk->direction = sp->direction;
    912  1.1.1.4  martti 
    913  1.1.1.4  martti 	/* copy to state */
    914  1.1.1.4  martti 	memcpy(&s->id, &sp->id, sizeof(sp->id));
    915  1.1.1.4  martti 	s->creatorid = sp->creatorid;
    916  1.1.1.4  martti 	strlcpy(sp->ifname, s->kif->pfik_name, sizeof(sp->ifname));
    917  1.1.1.4  martti 	pf_state_peer_from_pfsync(&sp->src, &s->src);
    918  1.1.1.4  martti 	pf_state_peer_from_pfsync(&sp->dst, &s->dst);
    919  1.1.1.4  martti 
    920  1.1.1.4  martti 	s->rule.ptr = &pf_default_rule;
    921  1.1.1.4  martti 	s->nat_rule.ptr = NULL;
    922  1.1.1.4  martti 	s->anchor.ptr = NULL;
    923  1.1.1.4  martti 	s->rt_kif = NULL;
    924  1.1.1.4  martti 	s->creation = time_second;
    925  1.1.1.4  martti 	s->pfsync_time = 0;
    926  1.1.1.4  martti 	s->packets[0] = s->packets[1] = 0;
    927  1.1.1.4  martti 	s->bytes[0] = s->bytes[1] = 0;
    928  1.1.1.4  martti }
    929  1.1.1.4  martti 
    930  1.1.1.4  martti int
    931  1.1.1.4  martti pf_setup_pfsync_matching(struct pf_ruleset *rs)
    932  1.1.1.4  martti {
    933  1.1.1.4  martti 	MD5_CTX			 ctx;
    934  1.1.1.4  martti 	struct pf_rule		*rule;
    935  1.1.1.4  martti 	int			 rs_cnt;
    936  1.1.1.4  martti 	u_int8_t		 digest[PF_MD5_DIGEST_LENGTH];
    937  1.1.1.4  martti 
    938  1.1.1.4  martti 	MD5Init(&ctx);
    939  1.1.1.4  martti 	for (rs_cnt = 0; rs_cnt < PF_RULESET_MAX; rs_cnt++) {
    940  1.1.1.4  martti 		/* XXX PF_RULESET_SCRUB as well? */
    941  1.1.1.4  martti 		if (rs_cnt == PF_RULESET_SCRUB)
    942  1.1.1.4  martti 			continue;
    943  1.1.1.4  martti 
    944  1.1.1.4  martti 		if (rs->rules[rs_cnt].inactive.ptr_array)
    945  1.1.1.4  martti 			free(rs->rules[rs_cnt].inactive.ptr_array, M_TEMP);
    946  1.1.1.4  martti 		rs->rules[rs_cnt].inactive.ptr_array = NULL;
    947  1.1.1.4  martti 
    948  1.1.1.4  martti 		if (rs->rules[rs_cnt].inactive.rcount) {
    949  1.1.1.4  martti 			rs->rules[rs_cnt].inactive.ptr_array =
    950  1.1.1.4  martti 			    malloc(sizeof(caddr_t) *
    951  1.1.1.4  martti 			    rs->rules[rs_cnt].inactive.rcount,
    952  1.1.1.4  martti 			    M_TEMP, M_NOWAIT);
    953  1.1.1.4  martti 
    954  1.1.1.4  martti 			if (!rs->rules[rs_cnt].inactive.ptr_array)
    955  1.1.1.4  martti 				return (ENOMEM);
    956  1.1.1.4  martti 		}
    957  1.1.1.4  martti 
    958  1.1.1.4  martti 		TAILQ_FOREACH(rule, rs->rules[rs_cnt].inactive.ptr,
    959  1.1.1.4  martti 		    entries) {
    960  1.1.1.4  martti 			pf_hash_rule(&ctx, rule);
    961  1.1.1.4  martti 			(rs->rules[rs_cnt].inactive.ptr_array)[rule->nr] = rule;
    962  1.1.1.4  martti 		}
    963  1.1.1.4  martti 	}
    964  1.1.1.4  martti 
    965  1.1.1.4  martti 	MD5Final(digest, &ctx);
    966  1.1.1.4  martti 	memcpy(pf_status.pf_chksum, digest, sizeof(pf_status.pf_chksum));
    967  1.1.1.4  martti 	return (0);
    968  1.1.1.4  martti }
    969  1.1.1.4  martti 
    970      1.1  itojun int
    971      1.1  itojun pfioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
    972      1.1  itojun {
    973      1.1  itojun 	struct pf_pooladdr	*pa = NULL;
    974      1.1  itojun 	struct pf_pool		*pool = NULL;
    975      1.1  itojun 	int			 s;
    976      1.1  itojun 	int			 error = 0;
    977      1.1  itojun 
    978      1.1  itojun 	/* XXX keep in sync with switch() below */
    979      1.1  itojun 	if (securelevel > 1)
    980      1.1  itojun 		switch (cmd) {
    981      1.1  itojun 		case DIOCGETRULES:
    982      1.1  itojun 		case DIOCGETRULE:
    983      1.1  itojun 		case DIOCGETADDRS:
    984      1.1  itojun 		case DIOCGETADDR:
    985      1.1  itojun 		case DIOCGETSTATE:
    986      1.1  itojun 		case DIOCSETSTATUSIF:
    987      1.1  itojun 		case DIOCGETSTATUS:
    988      1.1  itojun 		case DIOCCLRSTATUS:
    989      1.1  itojun 		case DIOCNATLOOK:
    990      1.1  itojun 		case DIOCSETDEBUG:
    991      1.1  itojun 		case DIOCGETSTATES:
    992      1.1  itojun 		case DIOCGETTIMEOUT:
    993      1.1  itojun 		case DIOCCLRRULECTRS:
    994      1.1  itojun 		case DIOCGETLIMIT:
    995      1.1  itojun 		case DIOCGETALTQS:
    996      1.1  itojun 		case DIOCGETALTQ:
    997      1.1  itojun 		case DIOCGETQSTATS:
    998      1.1  itojun 		case DIOCGETRULESETS:
    999      1.1  itojun 		case DIOCGETRULESET:
   1000      1.1  itojun 		case DIOCRGETTABLES:
   1001      1.1  itojun 		case DIOCRGETTSTATS:
   1002      1.1  itojun 		case DIOCRCLRTSTATS:
   1003      1.1  itojun 		case DIOCRCLRADDRS:
   1004      1.1  itojun 		case DIOCRADDADDRS:
   1005      1.1  itojun 		case DIOCRDELADDRS:
   1006      1.1  itojun 		case DIOCRSETADDRS:
   1007      1.1  itojun 		case DIOCRGETADDRS:
   1008      1.1  itojun 		case DIOCRGETASTATS:
   1009      1.1  itojun 		case DIOCRCLRASTATS:
   1010      1.1  itojun 		case DIOCRTSTADDRS:
   1011      1.1  itojun 		case DIOCOSFPGET:
   1012      1.1  itojun 		case DIOCGETSRCNODES:
   1013      1.1  itojun 		case DIOCCLRSRCNODES:
   1014      1.1  itojun 		case DIOCIGETIFACES:
   1015  1.1.1.3   peter 		case DIOCSETIFFLAG:
   1016  1.1.1.3   peter 		case DIOCCLRIFFLAG:
   1017      1.1  itojun 			break;
   1018      1.1  itojun 		case DIOCRCLRTABLES:
   1019      1.1  itojun 		case DIOCRADDTABLES:
   1020      1.1  itojun 		case DIOCRDELTABLES:
   1021      1.1  itojun 		case DIOCRSETTFLAGS:
   1022      1.1  itojun 			if (((struct pfioc_table *)addr)->pfrio_flags &
   1023      1.1  itojun 			    PFR_FLAG_DUMMY)
   1024      1.1  itojun 				break; /* dummy operation ok */
   1025      1.1  itojun 			return (EPERM);
   1026      1.1  itojun 		default:
   1027      1.1  itojun 			return (EPERM);
   1028      1.1  itojun 		}
   1029      1.1  itojun 
   1030      1.1  itojun 	if (!(flags & FWRITE))
   1031      1.1  itojun 		switch (cmd) {
   1032      1.1  itojun 		case DIOCGETRULES:
   1033      1.1  itojun 		case DIOCGETADDRS:
   1034      1.1  itojun 		case DIOCGETADDR:
   1035      1.1  itojun 		case DIOCGETSTATE:
   1036      1.1  itojun 		case DIOCGETSTATUS:
   1037      1.1  itojun 		case DIOCGETSTATES:
   1038      1.1  itojun 		case DIOCGETTIMEOUT:
   1039      1.1  itojun 		case DIOCGETLIMIT:
   1040      1.1  itojun 		case DIOCGETALTQS:
   1041      1.1  itojun 		case DIOCGETALTQ:
   1042      1.1  itojun 		case DIOCGETQSTATS:
   1043      1.1  itojun 		case DIOCGETRULESETS:
   1044      1.1  itojun 		case DIOCGETRULESET:
   1045  1.1.1.4  martti 		case DIOCNATLOOK:
   1046      1.1  itojun 		case DIOCRGETTABLES:
   1047      1.1  itojun 		case DIOCRGETTSTATS:
   1048      1.1  itojun 		case DIOCRGETADDRS:
   1049      1.1  itojun 		case DIOCRGETASTATS:
   1050      1.1  itojun 		case DIOCRTSTADDRS:
   1051      1.1  itojun 		case DIOCOSFPGET:
   1052      1.1  itojun 		case DIOCGETSRCNODES:
   1053      1.1  itojun 		case DIOCIGETIFACES:
   1054      1.1  itojun 			break;
   1055      1.1  itojun 		case DIOCRCLRTABLES:
   1056      1.1  itojun 		case DIOCRADDTABLES:
   1057      1.1  itojun 		case DIOCRDELTABLES:
   1058      1.1  itojun 		case DIOCRCLRTSTATS:
   1059      1.1  itojun 		case DIOCRCLRADDRS:
   1060      1.1  itojun 		case DIOCRADDADDRS:
   1061      1.1  itojun 		case DIOCRDELADDRS:
   1062      1.1  itojun 		case DIOCRSETADDRS:
   1063      1.1  itojun 		case DIOCRSETTFLAGS:
   1064      1.1  itojun 			if (((struct pfioc_table *)addr)->pfrio_flags &
   1065  1.1.1.4  martti 			    PFR_FLAG_DUMMY) {
   1066  1.1.1.4  martti 				flags |= FWRITE; /* need write lock for dummy */
   1067      1.1  itojun 				break; /* dummy operation ok */
   1068  1.1.1.4  martti 			}
   1069      1.1  itojun 			return (EACCES);
   1070  1.1.1.4  martti 		case DIOCGETRULE:
   1071  1.1.1.4  martti 			if (((struct pfioc_rule *)addr)->action == PF_GET_CLR_CNTR)
   1072  1.1.1.4  martti 				return (EACCES);
   1073  1.1.1.4  martti 			break;
   1074      1.1  itojun 		default:
   1075      1.1  itojun 			return (EACCES);
   1076      1.1  itojun 		}
   1077      1.1  itojun 
   1078  1.1.1.4  martti 	if (flags & FWRITE)
   1079  1.1.1.4  martti 		rw_enter_write(&pf_consistency_lock);
   1080  1.1.1.4  martti 	else
   1081  1.1.1.4  martti 		rw_enter_read(&pf_consistency_lock);
   1082  1.1.1.4  martti 
   1083  1.1.1.3   peter 	s = splsoftnet();
   1084      1.1  itojun 	switch (cmd) {
   1085      1.1  itojun 
   1086      1.1  itojun 	case DIOCSTART:
   1087      1.1  itojun 		if (pf_status.running)
   1088      1.1  itojun 			error = EEXIST;
   1089      1.1  itojun 		else {
   1090      1.1  itojun 			pf_status.running = 1;
   1091  1.1.1.2    yamt 			pf_status.since = time_second;
   1092      1.1  itojun 			if (pf_status.stateid == 0) {
   1093  1.1.1.2    yamt 				pf_status.stateid = time_second;
   1094      1.1  itojun 				pf_status.stateid = pf_status.stateid << 32;
   1095      1.1  itojun 			}
   1096      1.1  itojun 			DPFPRINTF(PF_DEBUG_MISC, ("pf: started\n"));
   1097      1.1  itojun 		}
   1098      1.1  itojun 		break;
   1099      1.1  itojun 
   1100      1.1  itojun 	case DIOCSTOP:
   1101      1.1  itojun 		if (!pf_status.running)
   1102      1.1  itojun 			error = ENOENT;
   1103      1.1  itojun 		else {
   1104      1.1  itojun 			pf_status.running = 0;
   1105  1.1.1.2    yamt 			pf_status.since = time_second;
   1106      1.1  itojun 			DPFPRINTF(PF_DEBUG_MISC, ("pf: stopped\n"));
   1107      1.1  itojun 		}
   1108      1.1  itojun 		break;
   1109      1.1  itojun 
   1110      1.1  itojun 	case DIOCADDRULE: {
   1111      1.1  itojun 		struct pfioc_rule	*pr = (struct pfioc_rule *)addr;
   1112      1.1  itojun 		struct pf_ruleset	*ruleset;
   1113      1.1  itojun 		struct pf_rule		*rule, *tail;
   1114      1.1  itojun 		struct pf_pooladdr	*pa;
   1115      1.1  itojun 		int			 rs_num;
   1116      1.1  itojun 
   1117  1.1.1.2    yamt 		pr->anchor[sizeof(pr->anchor) - 1] = 0;
   1118  1.1.1.2    yamt 		ruleset = pf_find_ruleset(pr->anchor);
   1119      1.1  itojun 		if (ruleset == NULL) {
   1120      1.1  itojun 			error = EINVAL;
   1121      1.1  itojun 			break;
   1122      1.1  itojun 		}
   1123      1.1  itojun 		rs_num = pf_get_ruleset_number(pr->rule.action);
   1124      1.1  itojun 		if (rs_num >= PF_RULESET_MAX) {
   1125      1.1  itojun 			error = EINVAL;
   1126      1.1  itojun 			break;
   1127      1.1  itojun 		}
   1128      1.1  itojun 		if (pr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
   1129      1.1  itojun 			error = EINVAL;
   1130      1.1  itojun 			break;
   1131      1.1  itojun 		}
   1132      1.1  itojun 		if (pr->ticket != ruleset->rules[rs_num].inactive.ticket) {
   1133      1.1  itojun 			error = EBUSY;
   1134      1.1  itojun 			break;
   1135      1.1  itojun 		}
   1136      1.1  itojun 		if (pr->pool_ticket != ticket_pabuf) {
   1137      1.1  itojun 			error = EBUSY;
   1138      1.1  itojun 			break;
   1139      1.1  itojun 		}
   1140      1.1  itojun 		rule = pool_get(&pf_rule_pl, PR_NOWAIT);
   1141      1.1  itojun 		if (rule == NULL) {
   1142      1.1  itojun 			error = ENOMEM;
   1143      1.1  itojun 			break;
   1144      1.1  itojun 		}
   1145      1.1  itojun 		bcopy(&pr->rule, rule, sizeof(struct pf_rule));
   1146  1.1.1.4  martti 		rule->cuid = p->p_cred->p_ruid;
   1147  1.1.1.4  martti 		rule->cpid = p->p_pid;
   1148      1.1  itojun 		rule->anchor = NULL;
   1149      1.1  itojun 		rule->kif = NULL;
   1150      1.1  itojun 		TAILQ_INIT(&rule->rpool.list);
   1151      1.1  itojun 		/* initialize refcounting */
   1152      1.1  itojun 		rule->states = 0;
   1153      1.1  itojun 		rule->src_nodes = 0;
   1154      1.1  itojun 		rule->entries.tqe_prev = NULL;
   1155      1.1  itojun #ifndef INET
   1156      1.1  itojun 		if (rule->af == AF_INET) {
   1157      1.1  itojun 			pool_put(&pf_rule_pl, rule);
   1158      1.1  itojun 			error = EAFNOSUPPORT;
   1159      1.1  itojun 			break;
   1160      1.1  itojun 		}
   1161      1.1  itojun #endif /* INET */
   1162      1.1  itojun #ifndef INET6
   1163      1.1  itojun 		if (rule->af == AF_INET6) {
   1164      1.1  itojun 			pool_put(&pf_rule_pl, rule);
   1165      1.1  itojun 			error = EAFNOSUPPORT;
   1166      1.1  itojun 			break;
   1167      1.1  itojun 		}
   1168      1.1  itojun #endif /* INET6 */
   1169      1.1  itojun 		tail = TAILQ_LAST(ruleset->rules[rs_num].inactive.ptr,
   1170      1.1  itojun 		    pf_rulequeue);
   1171      1.1  itojun 		if (tail)
   1172      1.1  itojun 			rule->nr = tail->nr + 1;
   1173      1.1  itojun 		else
   1174      1.1  itojun 			rule->nr = 0;
   1175      1.1  itojun 		if (rule->ifname[0]) {
   1176  1.1.1.4  martti 			rule->kif = pfi_kif_get(rule->ifname);
   1177      1.1  itojun 			if (rule->kif == NULL) {
   1178      1.1  itojun 				pool_put(&pf_rule_pl, rule);
   1179      1.1  itojun 				error = EINVAL;
   1180      1.1  itojun 				break;
   1181      1.1  itojun 			}
   1182  1.1.1.4  martti 			pfi_kif_ref(rule->kif, PFI_KIF_REF_RULE);
   1183      1.1  itojun 		}
   1184      1.1  itojun 
   1185  1.1.1.4  martti 		if (rule->rtableid > 0 && !rtable_exists(rule->rtableid))
   1186  1.1.1.4  martti 			error = EBUSY;
   1187  1.1.1.4  martti 
   1188      1.1  itojun #ifdef ALTQ
   1189      1.1  itojun 		/* set queue IDs */
   1190      1.1  itojun 		if (rule->qname[0] != 0) {
   1191      1.1  itojun 			if ((rule->qid = pf_qname2qid(rule->qname)) == 0)
   1192      1.1  itojun 				error = EBUSY;
   1193      1.1  itojun 			else if (rule->pqname[0] != 0) {
   1194      1.1  itojun 				if ((rule->pqid =
   1195      1.1  itojun 				    pf_qname2qid(rule->pqname)) == 0)
   1196      1.1  itojun 					error = EBUSY;
   1197      1.1  itojun 			} else
   1198      1.1  itojun 				rule->pqid = rule->qid;
   1199      1.1  itojun 		}
   1200      1.1  itojun #endif
   1201      1.1  itojun 		if (rule->tagname[0])
   1202      1.1  itojun 			if ((rule->tag = pf_tagname2tag(rule->tagname)) == 0)
   1203      1.1  itojun 				error = EBUSY;
   1204      1.1  itojun 		if (rule->match_tagname[0])
   1205      1.1  itojun 			if ((rule->match_tag =
   1206      1.1  itojun 			    pf_tagname2tag(rule->match_tagname)) == 0)
   1207      1.1  itojun 				error = EBUSY;
   1208      1.1  itojun 		if (rule->rt && !rule->direction)
   1209      1.1  itojun 			error = EINVAL;
   1210  1.1.1.4  martti #if NPFLOG > 0
   1211  1.1.1.4  martti 		if (!rule->log)
   1212  1.1.1.4  martti 			rule->logif = 0;
   1213  1.1.1.4  martti 		if (rule->logif >= PFLOGIFS_MAX)
   1214  1.1.1.4  martti 			error = EINVAL;
   1215  1.1.1.4  martti #endif
   1216  1.1.1.3   peter 		if (pf_rtlabel_add(&rule->src.addr) ||
   1217  1.1.1.3   peter 		    pf_rtlabel_add(&rule->dst.addr))
   1218  1.1.1.3   peter 			error = EBUSY;
   1219      1.1  itojun 		if (pfi_dynaddr_setup(&rule->src.addr, rule->af))
   1220      1.1  itojun 			error = EINVAL;
   1221      1.1  itojun 		if (pfi_dynaddr_setup(&rule->dst.addr, rule->af))
   1222      1.1  itojun 			error = EINVAL;
   1223      1.1  itojun 		if (pf_tbladdr_setup(ruleset, &rule->src.addr))
   1224      1.1  itojun 			error = EINVAL;
   1225      1.1  itojun 		if (pf_tbladdr_setup(ruleset, &rule->dst.addr))
   1226      1.1  itojun 			error = EINVAL;
   1227  1.1.1.2    yamt 		if (pf_anchor_setup(rule, ruleset, pr->anchor_call))
   1228  1.1.1.2    yamt 			error = EINVAL;
   1229      1.1  itojun 		TAILQ_FOREACH(pa, &pf_pabuf, entries)
   1230      1.1  itojun 			if (pf_tbladdr_setup(ruleset, &pa->addr))
   1231      1.1  itojun 				error = EINVAL;
   1232      1.1  itojun 
   1233  1.1.1.3   peter 		if (rule->overload_tblname[0]) {
   1234  1.1.1.3   peter 			if ((rule->overload_tbl = pfr_attach_table(ruleset,
   1235  1.1.1.3   peter 			    rule->overload_tblname)) == NULL)
   1236  1.1.1.3   peter 				error = EINVAL;
   1237  1.1.1.3   peter 			else
   1238  1.1.1.3   peter 				rule->overload_tbl->pfrkt_flags |=
   1239  1.1.1.3   peter 				    PFR_TFLAG_ACTIVE;
   1240  1.1.1.3   peter 		}
   1241  1.1.1.3   peter 
   1242      1.1  itojun 		pf_mv_pool(&pf_pabuf, &rule->rpool.list);
   1243      1.1  itojun 		if (((((rule->action == PF_NAT) || (rule->action == PF_RDR) ||
   1244  1.1.1.2    yamt 		    (rule->action == PF_BINAT)) && rule->anchor == NULL) ||
   1245      1.1  itojun 		    (rule->rt > PF_FASTROUTE)) &&
   1246      1.1  itojun 		    (TAILQ_FIRST(&rule->rpool.list) == NULL))
   1247      1.1  itojun 			error = EINVAL;
   1248      1.1  itojun 
   1249      1.1  itojun 		if (error) {
   1250      1.1  itojun 			pf_rm_rule(NULL, rule);
   1251      1.1  itojun 			break;
   1252      1.1  itojun 		}
   1253      1.1  itojun 		rule->rpool.cur = TAILQ_FIRST(&rule->rpool.list);
   1254  1.1.1.4  martti 		rule->evaluations = rule->packets[0] = rule->packets[1] =
   1255  1.1.1.4  martti 		    rule->bytes[0] = rule->bytes[1] = 0;
   1256      1.1  itojun 		TAILQ_INSERT_TAIL(ruleset->rules[rs_num].inactive.ptr,
   1257      1.1  itojun 		    rule, entries);
   1258  1.1.1.4  martti 		ruleset->rules[rs_num].inactive.rcount++;
   1259      1.1  itojun 		break;
   1260      1.1  itojun 	}
   1261      1.1  itojun 
   1262      1.1  itojun 	case DIOCGETRULES: {
   1263      1.1  itojun 		struct pfioc_rule	*pr = (struct pfioc_rule *)addr;
   1264      1.1  itojun 		struct pf_ruleset	*ruleset;
   1265      1.1  itojun 		struct pf_rule		*tail;
   1266      1.1  itojun 		int			 rs_num;
   1267      1.1  itojun 
   1268  1.1.1.2    yamt 		pr->anchor[sizeof(pr->anchor) - 1] = 0;
   1269  1.1.1.2    yamt 		ruleset = pf_find_ruleset(pr->anchor);
   1270      1.1  itojun 		if (ruleset == NULL) {
   1271      1.1  itojun 			error = EINVAL;
   1272      1.1  itojun 			break;
   1273      1.1  itojun 		}
   1274      1.1  itojun 		rs_num = pf_get_ruleset_number(pr->rule.action);
   1275      1.1  itojun 		if (rs_num >= PF_RULESET_MAX) {
   1276      1.1  itojun 			error = EINVAL;
   1277      1.1  itojun 			break;
   1278      1.1  itojun 		}
   1279      1.1  itojun 		tail = TAILQ_LAST(ruleset->rules[rs_num].active.ptr,
   1280      1.1  itojun 		    pf_rulequeue);
   1281      1.1  itojun 		if (tail)
   1282      1.1  itojun 			pr->nr = tail->nr + 1;
   1283      1.1  itojun 		else
   1284      1.1  itojun 			pr->nr = 0;
   1285      1.1  itojun 		pr->ticket = ruleset->rules[rs_num].active.ticket;
   1286      1.1  itojun 		break;
   1287      1.1  itojun 	}
   1288      1.1  itojun 
   1289      1.1  itojun 	case DIOCGETRULE: {
   1290      1.1  itojun 		struct pfioc_rule	*pr = (struct pfioc_rule *)addr;
   1291      1.1  itojun 		struct pf_ruleset	*ruleset;
   1292      1.1  itojun 		struct pf_rule		*rule;
   1293      1.1  itojun 		int			 rs_num, i;
   1294      1.1  itojun 
   1295  1.1.1.2    yamt 		pr->anchor[sizeof(pr->anchor) - 1] = 0;
   1296  1.1.1.2    yamt 		ruleset = pf_find_ruleset(pr->anchor);
   1297      1.1  itojun 		if (ruleset == NULL) {
   1298      1.1  itojun 			error = EINVAL;
   1299      1.1  itojun 			break;
   1300      1.1  itojun 		}
   1301      1.1  itojun 		rs_num = pf_get_ruleset_number(pr->rule.action);
   1302      1.1  itojun 		if (rs_num >= PF_RULESET_MAX) {
   1303      1.1  itojun 			error = EINVAL;
   1304      1.1  itojun 			break;
   1305      1.1  itojun 		}
   1306      1.1  itojun 		if (pr->ticket != ruleset->rules[rs_num].active.ticket) {
   1307      1.1  itojun 			error = EBUSY;
   1308      1.1  itojun 			break;
   1309      1.1  itojun 		}
   1310      1.1  itojun 		rule = TAILQ_FIRST(ruleset->rules[rs_num].active.ptr);
   1311      1.1  itojun 		while ((rule != NULL) && (rule->nr != pr->nr))
   1312      1.1  itojun 			rule = TAILQ_NEXT(rule, entries);
   1313      1.1  itojun 		if (rule == NULL) {
   1314      1.1  itojun 			error = EBUSY;
   1315      1.1  itojun 			break;
   1316      1.1  itojun 		}
   1317      1.1  itojun 		bcopy(rule, &pr->rule, sizeof(struct pf_rule));
   1318  1.1.1.2    yamt 		if (pf_anchor_copyout(ruleset, rule, pr)) {
   1319  1.1.1.2    yamt 			error = EBUSY;
   1320  1.1.1.2    yamt 			break;
   1321  1.1.1.2    yamt 		}
   1322      1.1  itojun 		pfi_dynaddr_copyout(&pr->rule.src.addr);
   1323      1.1  itojun 		pfi_dynaddr_copyout(&pr->rule.dst.addr);
   1324      1.1  itojun 		pf_tbladdr_copyout(&pr->rule.src.addr);
   1325      1.1  itojun 		pf_tbladdr_copyout(&pr->rule.dst.addr);
   1326  1.1.1.3   peter 		pf_rtlabel_copyout(&pr->rule.src.addr);
   1327  1.1.1.3   peter 		pf_rtlabel_copyout(&pr->rule.dst.addr);
   1328      1.1  itojun 		for (i = 0; i < PF_SKIP_COUNT; ++i)
   1329      1.1  itojun 			if (rule->skip[i].ptr == NULL)
   1330      1.1  itojun 				pr->rule.skip[i].nr = -1;
   1331      1.1  itojun 			else
   1332      1.1  itojun 				pr->rule.skip[i].nr =
   1333      1.1  itojun 				    rule->skip[i].ptr->nr;
   1334  1.1.1.4  martti 
   1335  1.1.1.4  martti 		if (pr->action == PF_GET_CLR_CNTR) {
   1336  1.1.1.4  martti 			rule->evaluations = 0;
   1337  1.1.1.4  martti 			rule->packets[0] = rule->packets[1] = 0;
   1338  1.1.1.4  martti 			rule->bytes[0] = rule->bytes[1] = 0;
   1339  1.1.1.4  martti 		}
   1340      1.1  itojun 		break;
   1341      1.1  itojun 	}
   1342      1.1  itojun 
   1343      1.1  itojun 	case DIOCCHANGERULE: {
   1344      1.1  itojun 		struct pfioc_rule	*pcr = (struct pfioc_rule *)addr;
   1345      1.1  itojun 		struct pf_ruleset	*ruleset;
   1346      1.1  itojun 		struct pf_rule		*oldrule = NULL, *newrule = NULL;
   1347      1.1  itojun 		u_int32_t		 nr = 0;
   1348      1.1  itojun 		int			 rs_num;
   1349      1.1  itojun 
   1350      1.1  itojun 		if (!(pcr->action == PF_CHANGE_REMOVE ||
   1351      1.1  itojun 		    pcr->action == PF_CHANGE_GET_TICKET) &&
   1352      1.1  itojun 		    pcr->pool_ticket != ticket_pabuf) {
   1353      1.1  itojun 			error = EBUSY;
   1354      1.1  itojun 			break;
   1355      1.1  itojun 		}
   1356      1.1  itojun 
   1357      1.1  itojun 		if (pcr->action < PF_CHANGE_ADD_HEAD ||
   1358      1.1  itojun 		    pcr->action > PF_CHANGE_GET_TICKET) {
   1359      1.1  itojun 			error = EINVAL;
   1360      1.1  itojun 			break;
   1361      1.1  itojun 		}
   1362  1.1.1.2    yamt 		ruleset = pf_find_ruleset(pcr->anchor);
   1363      1.1  itojun 		if (ruleset == NULL) {
   1364      1.1  itojun 			error = EINVAL;
   1365      1.1  itojun 			break;
   1366      1.1  itojun 		}
   1367      1.1  itojun 		rs_num = pf_get_ruleset_number(pcr->rule.action);
   1368      1.1  itojun 		if (rs_num >= PF_RULESET_MAX) {
   1369      1.1  itojun 			error = EINVAL;
   1370      1.1  itojun 			break;
   1371      1.1  itojun 		}
   1372      1.1  itojun 
   1373      1.1  itojun 		if (pcr->action == PF_CHANGE_GET_TICKET) {
   1374      1.1  itojun 			pcr->ticket = ++ruleset->rules[rs_num].active.ticket;
   1375      1.1  itojun 			break;
   1376      1.1  itojun 		} else {
   1377      1.1  itojun 			if (pcr->ticket !=
   1378      1.1  itojun 			    ruleset->rules[rs_num].active.ticket) {
   1379      1.1  itojun 				error = EINVAL;
   1380      1.1  itojun 				break;
   1381      1.1  itojun 			}
   1382      1.1  itojun 			if (pcr->rule.return_icmp >> 8 > ICMP_MAXTYPE) {
   1383      1.1  itojun 				error = EINVAL;
   1384      1.1  itojun 				break;
   1385      1.1  itojun 			}
   1386      1.1  itojun 		}
   1387      1.1  itojun 
   1388      1.1  itojun 		if (pcr->action != PF_CHANGE_REMOVE) {
   1389      1.1  itojun 			newrule = pool_get(&pf_rule_pl, PR_NOWAIT);
   1390      1.1  itojun 			if (newrule == NULL) {
   1391      1.1  itojun 				error = ENOMEM;
   1392      1.1  itojun 				break;
   1393      1.1  itojun 			}
   1394      1.1  itojun 			bcopy(&pcr->rule, newrule, sizeof(struct pf_rule));
   1395  1.1.1.4  martti 			newrule->cuid = p->p_cred->p_ruid;
   1396  1.1.1.4  martti 			newrule->cpid = p->p_pid;
   1397      1.1  itojun 			TAILQ_INIT(&newrule->rpool.list);
   1398      1.1  itojun 			/* initialize refcounting */
   1399      1.1  itojun 			newrule->states = 0;
   1400      1.1  itojun 			newrule->entries.tqe_prev = NULL;
   1401      1.1  itojun #ifndef INET
   1402      1.1  itojun 			if (newrule->af == AF_INET) {
   1403      1.1  itojun 				pool_put(&pf_rule_pl, newrule);
   1404      1.1  itojun 				error = EAFNOSUPPORT;
   1405      1.1  itojun 				break;
   1406      1.1  itojun 			}
   1407      1.1  itojun #endif /* INET */
   1408      1.1  itojun #ifndef INET6
   1409      1.1  itojun 			if (newrule->af == AF_INET6) {
   1410      1.1  itojun 				pool_put(&pf_rule_pl, newrule);
   1411      1.1  itojun 				error = EAFNOSUPPORT;
   1412      1.1  itojun 				break;
   1413      1.1  itojun 			}
   1414      1.1  itojun #endif /* INET6 */
   1415      1.1  itojun 			if (newrule->ifname[0]) {
   1416  1.1.1.4  martti 				newrule->kif = pfi_kif_get(newrule->ifname);
   1417      1.1  itojun 				if (newrule->kif == NULL) {
   1418      1.1  itojun 					pool_put(&pf_rule_pl, newrule);
   1419      1.1  itojun 					error = EINVAL;
   1420      1.1  itojun 					break;
   1421      1.1  itojun 				}
   1422  1.1.1.4  martti 				pfi_kif_ref(newrule->kif, PFI_KIF_REF_RULE);
   1423      1.1  itojun 			} else
   1424      1.1  itojun 				newrule->kif = NULL;
   1425      1.1  itojun 
   1426  1.1.1.4  martti 			if (newrule->rtableid > 0 &&
   1427  1.1.1.4  martti 			    !rtable_exists(newrule->rtableid))
   1428  1.1.1.4  martti 				error = EBUSY;
   1429  1.1.1.4  martti 
   1430      1.1  itojun #ifdef ALTQ
   1431      1.1  itojun 			/* set queue IDs */
   1432      1.1  itojun 			if (newrule->qname[0] != 0) {
   1433      1.1  itojun 				if ((newrule->qid =
   1434      1.1  itojun 				    pf_qname2qid(newrule->qname)) == 0)
   1435      1.1  itojun 					error = EBUSY;
   1436      1.1  itojun 				else if (newrule->pqname[0] != 0) {
   1437      1.1  itojun 					if ((newrule->pqid =
   1438      1.1  itojun 					    pf_qname2qid(newrule->pqname)) == 0)
   1439      1.1  itojun 						error = EBUSY;
   1440      1.1  itojun 				} else
   1441      1.1  itojun 					newrule->pqid = newrule->qid;
   1442      1.1  itojun 			}
   1443  1.1.1.2    yamt #endif /* ALTQ */
   1444      1.1  itojun 			if (newrule->tagname[0])
   1445      1.1  itojun 				if ((newrule->tag =
   1446      1.1  itojun 				    pf_tagname2tag(newrule->tagname)) == 0)
   1447      1.1  itojun 					error = EBUSY;
   1448      1.1  itojun 			if (newrule->match_tagname[0])
   1449      1.1  itojun 				if ((newrule->match_tag = pf_tagname2tag(
   1450      1.1  itojun 				    newrule->match_tagname)) == 0)
   1451      1.1  itojun 					error = EBUSY;
   1452      1.1  itojun 			if (newrule->rt && !newrule->direction)
   1453      1.1  itojun 				error = EINVAL;
   1454  1.1.1.4  martti #if NPFLOG > 0
   1455  1.1.1.4  martti 			if (!newrule->log)
   1456  1.1.1.4  martti 				newrule->logif = 0;
   1457  1.1.1.4  martti 			if (newrule->logif >= PFLOGIFS_MAX)
   1458  1.1.1.4  martti 				error = EINVAL;
   1459  1.1.1.4  martti #endif
   1460  1.1.1.3   peter 			if (pf_rtlabel_add(&newrule->src.addr) ||
   1461  1.1.1.3   peter 			    pf_rtlabel_add(&newrule->dst.addr))
   1462  1.1.1.3   peter 				error = EBUSY;
   1463      1.1  itojun 			if (pfi_dynaddr_setup(&newrule->src.addr, newrule->af))
   1464      1.1  itojun 				error = EINVAL;
   1465      1.1  itojun 			if (pfi_dynaddr_setup(&newrule->dst.addr, newrule->af))
   1466      1.1  itojun 				error = EINVAL;
   1467      1.1  itojun 			if (pf_tbladdr_setup(ruleset, &newrule->src.addr))
   1468      1.1  itojun 				error = EINVAL;
   1469      1.1  itojun 			if (pf_tbladdr_setup(ruleset, &newrule->dst.addr))
   1470      1.1  itojun 				error = EINVAL;
   1471  1.1.1.2    yamt 			if (pf_anchor_setup(newrule, ruleset, pcr->anchor_call))
   1472  1.1.1.2    yamt 				error = EINVAL;
   1473  1.1.1.4  martti 			TAILQ_FOREACH(pa, &pf_pabuf, entries)
   1474  1.1.1.4  martti 				if (pf_tbladdr_setup(ruleset, &pa->addr))
   1475  1.1.1.4  martti 					error = EINVAL;
   1476      1.1  itojun 
   1477  1.1.1.3   peter 			if (newrule->overload_tblname[0]) {
   1478  1.1.1.3   peter 				if ((newrule->overload_tbl = pfr_attach_table(
   1479  1.1.1.3   peter 				    ruleset, newrule->overload_tblname)) ==
   1480  1.1.1.3   peter 				    NULL)
   1481  1.1.1.3   peter 					error = EINVAL;
   1482  1.1.1.3   peter 				else
   1483  1.1.1.3   peter 					newrule->overload_tbl->pfrkt_flags |=
   1484  1.1.1.3   peter 					    PFR_TFLAG_ACTIVE;
   1485  1.1.1.3   peter 			}
   1486  1.1.1.3   peter 
   1487      1.1  itojun 			pf_mv_pool(&pf_pabuf, &newrule->rpool.list);
   1488      1.1  itojun 			if (((((newrule->action == PF_NAT) ||
   1489      1.1  itojun 			    (newrule->action == PF_RDR) ||
   1490      1.1  itojun 			    (newrule->action == PF_BINAT) ||
   1491      1.1  itojun 			    (newrule->rt > PF_FASTROUTE)) &&
   1492  1.1.1.4  martti 			    !newrule->anchor)) &&
   1493      1.1  itojun 			    (TAILQ_FIRST(&newrule->rpool.list) == NULL))
   1494      1.1  itojun 				error = EINVAL;
   1495      1.1  itojun 
   1496      1.1  itojun 			if (error) {
   1497      1.1  itojun 				pf_rm_rule(NULL, newrule);
   1498      1.1  itojun 				break;
   1499      1.1  itojun 			}
   1500      1.1  itojun 			newrule->rpool.cur = TAILQ_FIRST(&newrule->rpool.list);
   1501  1.1.1.4  martti 			newrule->evaluations = 0;
   1502  1.1.1.4  martti 			newrule->packets[0] = newrule->packets[1] = 0;
   1503  1.1.1.4  martti 			newrule->bytes[0] = newrule->bytes[1] = 0;
   1504      1.1  itojun 		}
   1505      1.1  itojun 		pf_empty_pool(&pf_pabuf);
   1506      1.1  itojun 
   1507      1.1  itojun 		if (pcr->action == PF_CHANGE_ADD_HEAD)
   1508      1.1  itojun 			oldrule = TAILQ_FIRST(
   1509      1.1  itojun 			    ruleset->rules[rs_num].active.ptr);
   1510      1.1  itojun 		else if (pcr->action == PF_CHANGE_ADD_TAIL)
   1511      1.1  itojun 			oldrule = TAILQ_LAST(
   1512      1.1  itojun 			    ruleset->rules[rs_num].active.ptr, pf_rulequeue);
   1513      1.1  itojun 		else {
   1514      1.1  itojun 			oldrule = TAILQ_FIRST(
   1515      1.1  itojun 			    ruleset->rules[rs_num].active.ptr);
   1516      1.1  itojun 			while ((oldrule != NULL) && (oldrule->nr != pcr->nr))
   1517      1.1  itojun 				oldrule = TAILQ_NEXT(oldrule, entries);
   1518      1.1  itojun 			if (oldrule == NULL) {
   1519  1.1.1.2    yamt 				if (newrule != NULL)
   1520  1.1.1.2    yamt 					pf_rm_rule(NULL, newrule);
   1521      1.1  itojun 				error = EINVAL;
   1522      1.1  itojun 				break;
   1523      1.1  itojun 			}
   1524      1.1  itojun 		}
   1525      1.1  itojun 
   1526  1.1.1.4  martti 		if (pcr->action == PF_CHANGE_REMOVE) {
   1527      1.1  itojun 			pf_rm_rule(ruleset->rules[rs_num].active.ptr, oldrule);
   1528  1.1.1.4  martti 			ruleset->rules[rs_num].active.rcount--;
   1529  1.1.1.4  martti 		} else {
   1530      1.1  itojun 			if (oldrule == NULL)
   1531      1.1  itojun 				TAILQ_INSERT_TAIL(
   1532      1.1  itojun 				    ruleset->rules[rs_num].active.ptr,
   1533      1.1  itojun 				    newrule, entries);
   1534      1.1  itojun 			else if (pcr->action == PF_CHANGE_ADD_HEAD ||
   1535      1.1  itojun 			    pcr->action == PF_CHANGE_ADD_BEFORE)
   1536      1.1  itojun 				TAILQ_INSERT_BEFORE(oldrule, newrule, entries);
   1537      1.1  itojun 			else
   1538      1.1  itojun 				TAILQ_INSERT_AFTER(
   1539      1.1  itojun 				    ruleset->rules[rs_num].active.ptr,
   1540      1.1  itojun 				    oldrule, newrule, entries);
   1541  1.1.1.4  martti 			ruleset->rules[rs_num].active.rcount++;
   1542      1.1  itojun 		}
   1543      1.1  itojun 
   1544      1.1  itojun 		nr = 0;
   1545      1.1  itojun 		TAILQ_FOREACH(oldrule,
   1546      1.1  itojun 		    ruleset->rules[rs_num].active.ptr, entries)
   1547      1.1  itojun 			oldrule->nr = nr++;
   1548      1.1  itojun 
   1549  1.1.1.2    yamt 		ruleset->rules[rs_num].active.ticket++;
   1550  1.1.1.2    yamt 
   1551      1.1  itojun 		pf_calc_skip_steps(ruleset->rules[rs_num].active.ptr);
   1552      1.1  itojun 		pf_remove_if_empty_ruleset(ruleset);
   1553      1.1  itojun 
   1554      1.1  itojun 		break;
   1555      1.1  itojun 	}
   1556      1.1  itojun 
   1557      1.1  itojun 	case DIOCCLRSTATES: {
   1558  1.1.1.4  martti 		struct pf_state		*s, *nexts;
   1559      1.1  itojun 		struct pfioc_state_kill *psk = (struct pfioc_state_kill *)addr;
   1560      1.1  itojun 		int			 killed = 0;
   1561      1.1  itojun 
   1562  1.1.1.4  martti 		for (s = RB_MIN(pf_state_tree_id, &tree_id); s; s = nexts) {
   1563  1.1.1.4  martti 			nexts = RB_NEXT(pf_state_tree_id, &tree_id, s);
   1564  1.1.1.4  martti 
   1565      1.1  itojun 			if (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
   1566  1.1.1.4  martti 			    s->kif->pfik_name)) {
   1567      1.1  itojun #if NPFSYNC
   1568      1.1  itojun 				/* don't send out individual delete messages */
   1569  1.1.1.4  martti 				s->sync_flags = PFSTATE_NOSYNC;
   1570      1.1  itojun #endif
   1571  1.1.1.4  martti 				pf_unlink_state(s);
   1572      1.1  itojun 				killed++;
   1573      1.1  itojun 			}
   1574      1.1  itojun 		}
   1575      1.1  itojun 		psk->psk_af = killed;
   1576      1.1  itojun #if NPFSYNC
   1577      1.1  itojun 		pfsync_clear_states(pf_status.hostid, psk->psk_ifname);
   1578      1.1  itojun #endif
   1579      1.1  itojun 		break;
   1580      1.1  itojun 	}
   1581      1.1  itojun 
   1582      1.1  itojun 	case DIOCKILLSTATES: {
   1583  1.1.1.4  martti 		struct pf_state		*s, *nexts;
   1584  1.1.1.4  martti 		struct pf_state_key	*sk;
   1585  1.1.1.4  martti 		struct pf_state_host	*src, *dst;
   1586      1.1  itojun 		struct pfioc_state_kill	*psk = (struct pfioc_state_kill *)addr;
   1587      1.1  itojun 		int			 killed = 0;
   1588      1.1  itojun 
   1589  1.1.1.4  martti 		for (s = RB_MIN(pf_state_tree_id, &tree_id); s;
   1590  1.1.1.4  martti 		    s = nexts) {
   1591  1.1.1.4  martti 			nexts = RB_NEXT(pf_state_tree_id, &tree_id, s);
   1592  1.1.1.4  martti 			sk = s->state_key;
   1593  1.1.1.4  martti 
   1594  1.1.1.4  martti 			if (sk->direction == PF_OUT) {
   1595  1.1.1.4  martti 				src = &sk->lan;
   1596  1.1.1.4  martti 				dst = &sk->ext;
   1597  1.1.1.4  martti 			} else {
   1598  1.1.1.4  martti 				src = &sk->ext;
   1599  1.1.1.4  martti 				dst = &sk->lan;
   1600  1.1.1.4  martti 			}
   1601  1.1.1.4  martti 			if ((!psk->psk_af || sk->af == psk->psk_af)
   1602      1.1  itojun 			    && (!psk->psk_proto || psk->psk_proto ==
   1603  1.1.1.4  martti 			    sk->proto) &&
   1604  1.1.1.2    yamt 			    PF_MATCHA(psk->psk_src.neg,
   1605      1.1  itojun 			    &psk->psk_src.addr.v.a.addr,
   1606      1.1  itojun 			    &psk->psk_src.addr.v.a.mask,
   1607  1.1.1.4  martti 			    &src->addr, sk->af) &&
   1608  1.1.1.2    yamt 			    PF_MATCHA(psk->psk_dst.neg,
   1609      1.1  itojun 			    &psk->psk_dst.addr.v.a.addr,
   1610      1.1  itojun 			    &psk->psk_dst.addr.v.a.mask,
   1611  1.1.1.4  martti 			    &dst->addr, sk->af) &&
   1612      1.1  itojun 			    (psk->psk_src.port_op == 0 ||
   1613      1.1  itojun 			    pf_match_port(psk->psk_src.port_op,
   1614      1.1  itojun 			    psk->psk_src.port[0], psk->psk_src.port[1],
   1615  1.1.1.4  martti 			    src->port)) &&
   1616      1.1  itojun 			    (psk->psk_dst.port_op == 0 ||
   1617      1.1  itojun 			    pf_match_port(psk->psk_dst.port_op,
   1618      1.1  itojun 			    psk->psk_dst.port[0], psk->psk_dst.port[1],
   1619  1.1.1.4  martti 			    dst->port)) &&
   1620      1.1  itojun 			    (!psk->psk_ifname[0] || !strcmp(psk->psk_ifname,
   1621  1.1.1.4  martti 			    s->kif->pfik_name))) {
   1622  1.1.1.4  martti #if NPFSYNC > 0
   1623  1.1.1.4  martti 				/* send immediate delete of state */
   1624  1.1.1.4  martti 				pfsync_delete_state(s);
   1625  1.1.1.4  martti 				s->sync_flags |= PFSTATE_NOSYNC;
   1626  1.1.1.4  martti #endif
   1627  1.1.1.4  martti 				pf_unlink_state(s);
   1628      1.1  itojun 				killed++;
   1629      1.1  itojun 			}
   1630      1.1  itojun 		}
   1631      1.1  itojun 		psk->psk_af = killed;
   1632      1.1  itojun 		break;
   1633      1.1  itojun 	}
   1634      1.1  itojun 
   1635      1.1  itojun 	case DIOCADDSTATE: {
   1636      1.1  itojun 		struct pfioc_state	*ps = (struct pfioc_state *)addr;
   1637  1.1.1.4  martti 		struct pfsync_state 	*sp = (struct pfsync_state *)ps->state;
   1638  1.1.1.4  martti 		struct pf_state		*s;
   1639  1.1.1.4  martti 		struct pf_state_key	*sk;
   1640      1.1  itojun 		struct pfi_kif		*kif;
   1641      1.1  itojun 
   1642  1.1.1.4  martti 		if (sp->timeout >= PFTM_MAX &&
   1643  1.1.1.4  martti 		    sp->timeout != PFTM_UNTIL_PACKET) {
   1644      1.1  itojun 			error = EINVAL;
   1645      1.1  itojun 			break;
   1646      1.1  itojun 		}
   1647  1.1.1.4  martti 		s = pool_get(&pf_state_pl, PR_NOWAIT);
   1648  1.1.1.4  martti 		if (s == NULL) {
   1649  1.1.1.4  martti 			error = ENOMEM;
   1650  1.1.1.4  martti 			break;
   1651  1.1.1.4  martti 		}
   1652  1.1.1.4  martti 		bzero(s, sizeof(struct pf_state));
   1653  1.1.1.4  martti 		if ((sk = pf_alloc_state_key(s)) == NULL) {
   1654      1.1  itojun 			error = ENOMEM;
   1655      1.1  itojun 			break;
   1656      1.1  itojun 		}
   1657  1.1.1.4  martti 		pf_state_import(sp, sk, s);
   1658  1.1.1.4  martti 		kif = pfi_kif_get(sp->ifname);
   1659      1.1  itojun 		if (kif == NULL) {
   1660  1.1.1.4  martti 			pool_put(&pf_state_pl, s);
   1661  1.1.1.4  martti 			pool_put(&pf_state_key_pl, sk);
   1662      1.1  itojun 			error = ENOENT;
   1663      1.1  itojun 			break;
   1664      1.1  itojun 		}
   1665  1.1.1.4  martti 		if (pf_insert_state(kif, s)) {
   1666  1.1.1.4  martti 			pfi_kif_unref(kif, PFI_KIF_REF_NONE);
   1667  1.1.1.4  martti 			pool_put(&pf_state_pl, s);
   1668  1.1.1.4  martti 			pool_put(&pf_state_key_pl, sk);
   1669      1.1  itojun 			error = ENOMEM;
   1670      1.1  itojun 		}
   1671      1.1  itojun 		break;
   1672      1.1  itojun 	}
   1673      1.1  itojun 
   1674      1.1  itojun 	case DIOCGETSTATE: {
   1675      1.1  itojun 		struct pfioc_state	*ps = (struct pfioc_state *)addr;
   1676  1.1.1.4  martti 		struct pf_state		*s;
   1677      1.1  itojun 		u_int32_t		 nr;
   1678      1.1  itojun 
   1679      1.1  itojun 		nr = 0;
   1680  1.1.1.4  martti 		RB_FOREACH(s, pf_state_tree_id, &tree_id) {
   1681      1.1  itojun 			if (nr >= ps->nr)
   1682      1.1  itojun 				break;
   1683      1.1  itojun 			nr++;
   1684      1.1  itojun 		}
   1685  1.1.1.4  martti 		if (s == NULL) {
   1686      1.1  itojun 			error = EBUSY;
   1687      1.1  itojun 			break;
   1688      1.1  itojun 		}
   1689  1.1.1.4  martti 
   1690  1.1.1.4  martti 		pf_state_export((struct pfsync_state *)&ps->state,
   1691  1.1.1.4  martti 		    s->state_key, s);
   1692      1.1  itojun 		break;
   1693      1.1  itojun 	}
   1694      1.1  itojun 
   1695      1.1  itojun 	case DIOCGETSTATES: {
   1696      1.1  itojun 		struct pfioc_states	*ps = (struct pfioc_states *)addr;
   1697      1.1  itojun 		struct pf_state		*state;
   1698  1.1.1.4  martti 		struct pfsync_state	*p, *pstore;
   1699      1.1  itojun 		u_int32_t		 nr = 0;
   1700      1.1  itojun 
   1701  1.1.1.4  martti 		if (ps->ps_len == 0) {
   1702  1.1.1.4  martti 			nr = pf_status.states;
   1703  1.1.1.4  martti 			ps->ps_len = sizeof(struct pfsync_state) * nr;
   1704  1.1.1.3   peter 			break;
   1705      1.1  itojun 		}
   1706      1.1  itojun 
   1707  1.1.1.4  martti 		pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
   1708  1.1.1.4  martti 
   1709      1.1  itojun 		p = ps->ps_states;
   1710      1.1  itojun 
   1711  1.1.1.4  martti 		state = TAILQ_FIRST(&state_list);
   1712  1.1.1.4  martti 		while (state) {
   1713  1.1.1.4  martti 			if (state->timeout != PFTM_UNLINKED) {
   1714      1.1  itojun 				if ((nr+1) * sizeof(*p) > (unsigned)ps->ps_len)
   1715      1.1  itojun 					break;
   1716      1.1  itojun 
   1717  1.1.1.4  martti 				pf_state_export(pstore,
   1718  1.1.1.4  martti 				    state->state_key, state);
   1719  1.1.1.4  martti 				error = copyout(pstore, p, sizeof(*p));
   1720  1.1.1.4  martti 				if (error) {
   1721  1.1.1.4  martti 					free(pstore, M_TEMP);
   1722      1.1  itojun 					goto fail;
   1723  1.1.1.4  martti 				}
   1724      1.1  itojun 				p++;
   1725      1.1  itojun 				nr++;
   1726      1.1  itojun 			}
   1727  1.1.1.4  martti 			state = TAILQ_NEXT(state, entry_list);
   1728  1.1.1.4  martti 		}
   1729  1.1.1.4  martti 
   1730  1.1.1.4  martti 		ps->ps_len = sizeof(struct pfsync_state) * nr;
   1731  1.1.1.4  martti 
   1732  1.1.1.4  martti 		free(pstore, M_TEMP);
   1733      1.1  itojun 		break;
   1734      1.1  itojun 	}
   1735      1.1  itojun 
   1736      1.1  itojun 	case DIOCGETSTATUS: {
   1737      1.1  itojun 		struct pf_status *s = (struct pf_status *)addr;
   1738      1.1  itojun 		bcopy(&pf_status, s, sizeof(struct pf_status));
   1739      1.1  itojun 		pfi_fill_oldstatus(s);
   1740      1.1  itojun 		break;
   1741      1.1  itojun 	}
   1742      1.1  itojun 
   1743      1.1  itojun 	case DIOCSETSTATUSIF: {
   1744      1.1  itojun 		struct pfioc_if	*pi = (struct pfioc_if *)addr;
   1745      1.1  itojun 
   1746      1.1  itojun 		if (pi->ifname[0] == 0) {
   1747      1.1  itojun 			bzero(pf_status.ifname, IFNAMSIZ);
   1748      1.1  itojun 			break;
   1749      1.1  itojun 		}
   1750      1.1  itojun 		if (ifunit(pi->ifname) == NULL) {
   1751      1.1  itojun 			error = EINVAL;
   1752      1.1  itojun 			break;
   1753      1.1  itojun 		}
   1754      1.1  itojun 		strlcpy(pf_status.ifname, pi->ifname, IFNAMSIZ);
   1755      1.1  itojun 		break;
   1756      1.1  itojun 	}
   1757      1.1  itojun 
   1758      1.1  itojun 	case DIOCCLRSTATUS: {
   1759      1.1  itojun 		bzero(pf_status.counters, sizeof(pf_status.counters));
   1760      1.1  itojun 		bzero(pf_status.fcounters, sizeof(pf_status.fcounters));
   1761      1.1  itojun 		bzero(pf_status.scounters, sizeof(pf_status.scounters));
   1762  1.1.1.4  martti 		pf_status.since = time_second;
   1763      1.1  itojun 		if (*pf_status.ifname)
   1764  1.1.1.4  martti 			pfi_clr_istats(pf_status.ifname);
   1765      1.1  itojun 		break;
   1766      1.1  itojun 	}
   1767      1.1  itojun 
   1768      1.1  itojun 	case DIOCNATLOOK: {
   1769      1.1  itojun 		struct pfioc_natlook	*pnl = (struct pfioc_natlook *)addr;
   1770  1.1.1.4  martti 		struct pf_state_key	*sk;
   1771      1.1  itojun 		struct pf_state		*state;
   1772  1.1.1.4  martti 		struct pf_state_key_cmp	 key;
   1773      1.1  itojun 		int			 m = 0, direction = pnl->direction;
   1774      1.1  itojun 
   1775      1.1  itojun 		key.af = pnl->af;
   1776      1.1  itojun 		key.proto = pnl->proto;
   1777      1.1  itojun 
   1778      1.1  itojun 		if (!pnl->proto ||
   1779      1.1  itojun 		    PF_AZERO(&pnl->saddr, pnl->af) ||
   1780      1.1  itojun 		    PF_AZERO(&pnl->daddr, pnl->af) ||
   1781  1.1.1.4  martti 		    ((pnl->proto == IPPROTO_TCP ||
   1782  1.1.1.4  martti 		    pnl->proto == IPPROTO_UDP) &&
   1783  1.1.1.4  martti 		    (!pnl->dport || !pnl->sport)))
   1784      1.1  itojun 			error = EINVAL;
   1785      1.1  itojun 		else {
   1786      1.1  itojun 			/*
   1787      1.1  itojun 			 * userland gives us source and dest of connection,
   1788      1.1  itojun 			 * reverse the lookup so we ask for what happens with
   1789      1.1  itojun 			 * the return traffic, enabling us to find it in the
   1790      1.1  itojun 			 * state tree.
   1791      1.1  itojun 			 */
   1792      1.1  itojun 			if (direction == PF_IN) {
   1793      1.1  itojun 				PF_ACPY(&key.ext.addr, &pnl->daddr, pnl->af);
   1794      1.1  itojun 				key.ext.port = pnl->dport;
   1795      1.1  itojun 				PF_ACPY(&key.gwy.addr, &pnl->saddr, pnl->af);
   1796      1.1  itojun 				key.gwy.port = pnl->sport;
   1797      1.1  itojun 				state = pf_find_state_all(&key, PF_EXT_GWY, &m);
   1798      1.1  itojun 			} else {
   1799      1.1  itojun 				PF_ACPY(&key.lan.addr, &pnl->daddr, pnl->af);
   1800      1.1  itojun 				key.lan.port = pnl->dport;
   1801      1.1  itojun 				PF_ACPY(&key.ext.addr, &pnl->saddr, pnl->af);
   1802      1.1  itojun 				key.ext.port = pnl->sport;
   1803      1.1  itojun 				state = pf_find_state_all(&key, PF_LAN_EXT, &m);
   1804      1.1  itojun 			}
   1805      1.1  itojun 			if (m > 1)
   1806      1.1  itojun 				error = E2BIG;	/* more than one state */
   1807      1.1  itojun 			else if (state != NULL) {
   1808  1.1.1.4  martti 				sk = state->state_key;
   1809      1.1  itojun 				if (direction == PF_IN) {
   1810  1.1.1.4  martti 					PF_ACPY(&pnl->rsaddr, &sk->lan.addr,
   1811  1.1.1.4  martti 					    sk->af);
   1812  1.1.1.4  martti 					pnl->rsport = sk->lan.port;
   1813      1.1  itojun 					PF_ACPY(&pnl->rdaddr, &pnl->daddr,
   1814      1.1  itojun 					    pnl->af);
   1815      1.1  itojun 					pnl->rdport = pnl->dport;
   1816      1.1  itojun 				} else {
   1817  1.1.1.4  martti 					PF_ACPY(&pnl->rdaddr, &sk->gwy.addr,
   1818  1.1.1.4  martti 					    sk->af);
   1819  1.1.1.4  martti 					pnl->rdport = sk->gwy.port;
   1820      1.1  itojun 					PF_ACPY(&pnl->rsaddr, &pnl->saddr,
   1821      1.1  itojun 					    pnl->af);
   1822      1.1  itojun 					pnl->rsport = pnl->sport;
   1823      1.1  itojun 				}
   1824      1.1  itojun 			} else
   1825      1.1  itojun 				error = ENOENT;
   1826      1.1  itojun 		}
   1827      1.1  itojun 		break;
   1828      1.1  itojun 	}
   1829      1.1  itojun 
   1830      1.1  itojun 	case DIOCSETTIMEOUT: {
   1831      1.1  itojun 		struct pfioc_tm	*pt = (struct pfioc_tm *)addr;
   1832      1.1  itojun 		int		 old;
   1833      1.1  itojun 
   1834      1.1  itojun 		if (pt->timeout < 0 || pt->timeout >= PFTM_MAX ||
   1835      1.1  itojun 		    pt->seconds < 0) {
   1836      1.1  itojun 			error = EINVAL;
   1837      1.1  itojun 			goto fail;
   1838      1.1  itojun 		}
   1839      1.1  itojun 		old = pf_default_rule.timeout[pt->timeout];
   1840  1.1.1.4  martti 		if (pt->timeout == PFTM_INTERVAL && pt->seconds == 0)
   1841  1.1.1.4  martti 			pt->seconds = 1;
   1842      1.1  itojun 		pf_default_rule.timeout[pt->timeout] = pt->seconds;
   1843  1.1.1.4  martti 		if (pt->timeout == PFTM_INTERVAL && pt->seconds < old)
   1844  1.1.1.4  martti 			wakeup(pf_purge_thread);
   1845      1.1  itojun 		pt->seconds = old;
   1846      1.1  itojun 		break;
   1847      1.1  itojun 	}
   1848      1.1  itojun 
   1849      1.1  itojun 	case DIOCGETTIMEOUT: {
   1850      1.1  itojun 		struct pfioc_tm	*pt = (struct pfioc_tm *)addr;
   1851      1.1  itojun 
   1852      1.1  itojun 		if (pt->timeout < 0 || pt->timeout >= PFTM_MAX) {
   1853      1.1  itojun 			error = EINVAL;
   1854      1.1  itojun 			goto fail;
   1855      1.1  itojun 		}
   1856      1.1  itojun 		pt->seconds = pf_default_rule.timeout[pt->timeout];
   1857      1.1  itojun 		break;
   1858      1.1  itojun 	}
   1859      1.1  itojun 
   1860      1.1  itojun 	case DIOCGETLIMIT: {
   1861      1.1  itojun 		struct pfioc_limit	*pl = (struct pfioc_limit *)addr;
   1862      1.1  itojun 
   1863      1.1  itojun 		if (pl->index < 0 || pl->index >= PF_LIMIT_MAX) {
   1864      1.1  itojun 			error = EINVAL;
   1865      1.1  itojun 			goto fail;
   1866      1.1  itojun 		}
   1867      1.1  itojun 		pl->limit = pf_pool_limits[pl->index].limit;
   1868      1.1  itojun 		break;
   1869      1.1  itojun 	}
   1870      1.1  itojun 
   1871      1.1  itojun 	case DIOCSETLIMIT: {
   1872      1.1  itojun 		struct pfioc_limit	*pl = (struct pfioc_limit *)addr;
   1873      1.1  itojun 		int			 old_limit;
   1874      1.1  itojun 
   1875      1.1  itojun 		if (pl->index < 0 || pl->index >= PF_LIMIT_MAX ||
   1876      1.1  itojun 		    pf_pool_limits[pl->index].pp == NULL) {
   1877      1.1  itojun 			error = EINVAL;
   1878      1.1  itojun 			goto fail;
   1879      1.1  itojun 		}
   1880      1.1  itojun 		if (pool_sethardlimit(pf_pool_limits[pl->index].pp,
   1881      1.1  itojun 		    pl->limit, NULL, 0) != 0) {
   1882      1.1  itojun 			error = EBUSY;
   1883      1.1  itojun 			goto fail;
   1884      1.1  itojun 		}
   1885      1.1  itojun 		old_limit = pf_pool_limits[pl->index].limit;
   1886      1.1  itojun 		pf_pool_limits[pl->index].limit = pl->limit;
   1887      1.1  itojun 		pl->limit = old_limit;
   1888      1.1  itojun 		break;
   1889      1.1  itojun 	}
   1890      1.1  itojun 
   1891      1.1  itojun 	case DIOCSETDEBUG: {
   1892      1.1  itojun 		u_int32_t	*level = (u_int32_t *)addr;
   1893      1.1  itojun 
   1894      1.1  itojun 		pf_status.debug = *level;
   1895      1.1  itojun 		break;
   1896      1.1  itojun 	}
   1897      1.1  itojun 
   1898      1.1  itojun 	case DIOCCLRRULECTRS: {
   1899  1.1.1.4  martti 		/* obsoleted by DIOCGETRULE with action=PF_GET_CLR_CNTR */
   1900      1.1  itojun 		struct pf_ruleset	*ruleset = &pf_main_ruleset;
   1901      1.1  itojun 		struct pf_rule		*rule;
   1902      1.1  itojun 
   1903      1.1  itojun 		TAILQ_FOREACH(rule,
   1904  1.1.1.4  martti 		    ruleset->rules[PF_RULESET_FILTER].active.ptr, entries) {
   1905  1.1.1.4  martti 			rule->evaluations = 0;
   1906  1.1.1.4  martti 			rule->packets[0] = rule->packets[1] = 0;
   1907  1.1.1.4  martti 			rule->bytes[0] = rule->bytes[1] = 0;
   1908  1.1.1.4  martti 		}
   1909      1.1  itojun 		break;
   1910      1.1  itojun 	}
   1911      1.1  itojun 
   1912      1.1  itojun #ifdef ALTQ
   1913      1.1  itojun 	case DIOCSTARTALTQ: {
   1914      1.1  itojun 		struct pf_altq		*altq;
   1915      1.1  itojun 
   1916      1.1  itojun 		/* enable all altq interfaces on active list */
   1917      1.1  itojun 		TAILQ_FOREACH(altq, pf_altqs_active, entries) {
   1918      1.1  itojun 			if (altq->qname[0] == 0) {
   1919  1.1.1.2    yamt 				error = pf_enable_altq(altq);
   1920      1.1  itojun 				if (error != 0)
   1921      1.1  itojun 					break;
   1922      1.1  itojun 			}
   1923      1.1  itojun 		}
   1924      1.1  itojun 		if (error == 0)
   1925  1.1.1.2    yamt 			pf_altq_running = 1;
   1926      1.1  itojun 		DPFPRINTF(PF_DEBUG_MISC, ("altq: started\n"));
   1927      1.1  itojun 		break;
   1928      1.1  itojun 	}
   1929      1.1  itojun 
   1930      1.1  itojun 	case DIOCSTOPALTQ: {
   1931      1.1  itojun 		struct pf_altq		*altq;
   1932      1.1  itojun 
   1933      1.1  itojun 		/* disable all altq interfaces on active list */
   1934      1.1  itojun 		TAILQ_FOREACH(altq, pf_altqs_active, entries) {
   1935      1.1  itojun 			if (altq->qname[0] == 0) {
   1936  1.1.1.2    yamt 				error = pf_disable_altq(altq);
   1937  1.1.1.2    yamt 				if (error != 0)
   1938      1.1  itojun 					break;
   1939      1.1  itojun 			}
   1940      1.1  itojun 		}
   1941      1.1  itojun 		if (error == 0)
   1942  1.1.1.2    yamt 			pf_altq_running = 0;
   1943      1.1  itojun 		DPFPRINTF(PF_DEBUG_MISC, ("altq: stopped\n"));
   1944      1.1  itojun 		break;
   1945      1.1  itojun 	}
   1946      1.1  itojun 
   1947      1.1  itojun 	case DIOCADDALTQ: {
   1948      1.1  itojun 		struct pfioc_altq	*pa = (struct pfioc_altq *)addr;
   1949      1.1  itojun 		struct pf_altq		*altq, *a;
   1950      1.1  itojun 
   1951      1.1  itojun 		if (pa->ticket != ticket_altqs_inactive) {
   1952      1.1  itojun 			error = EBUSY;
   1953      1.1  itojun 			break;
   1954      1.1  itojun 		}
   1955      1.1  itojun 		altq = pool_get(&pf_altq_pl, PR_NOWAIT);
   1956      1.1  itojun 		if (altq == NULL) {
   1957      1.1  itojun 			error = ENOMEM;
   1958      1.1  itojun 			break;
   1959      1.1  itojun 		}
   1960      1.1  itojun 		bcopy(&pa->altq, altq, sizeof(struct pf_altq));
   1961      1.1  itojun 
   1962      1.1  itojun 		/*
   1963      1.1  itojun 		 * if this is for a queue, find the discipline and
   1964      1.1  itojun 		 * copy the necessary fields
   1965      1.1  itojun 		 */
   1966      1.1  itojun 		if (altq->qname[0] != 0) {
   1967      1.1  itojun 			if ((altq->qid = pf_qname2qid(altq->qname)) == 0) {
   1968      1.1  itojun 				error = EBUSY;
   1969      1.1  itojun 				pool_put(&pf_altq_pl, altq);
   1970      1.1  itojun 				break;
   1971      1.1  itojun 			}
   1972      1.1  itojun 			TAILQ_FOREACH(a, pf_altqs_inactive, entries) {
   1973      1.1  itojun 				if (strncmp(a->ifname, altq->ifname,
   1974      1.1  itojun 				    IFNAMSIZ) == 0 && a->qname[0] == 0) {
   1975      1.1  itojun 					altq->altq_disc = a->altq_disc;
   1976      1.1  itojun 					break;
   1977      1.1  itojun 				}
   1978      1.1  itojun 			}
   1979      1.1  itojun 		}
   1980      1.1  itojun 
   1981      1.1  itojun 		error = altq_add(altq);
   1982      1.1  itojun 		if (error) {
   1983      1.1  itojun 			pool_put(&pf_altq_pl, altq);
   1984      1.1  itojun 			break;
   1985      1.1  itojun 		}
   1986      1.1  itojun 
   1987      1.1  itojun 		TAILQ_INSERT_TAIL(pf_altqs_inactive, altq, entries);
   1988      1.1  itojun 		bcopy(altq, &pa->altq, sizeof(struct pf_altq));
   1989      1.1  itojun 		break;
   1990      1.1  itojun 	}
   1991      1.1  itojun 
   1992      1.1  itojun 	case DIOCGETALTQS: {
   1993      1.1  itojun 		struct pfioc_altq	*pa = (struct pfioc_altq *)addr;
   1994      1.1  itojun 		struct pf_altq		*altq;
   1995      1.1  itojun 
   1996      1.1  itojun 		pa->nr = 0;
   1997      1.1  itojun 		TAILQ_FOREACH(altq, pf_altqs_active, entries)
   1998      1.1  itojun 			pa->nr++;
   1999      1.1  itojun 		pa->ticket = ticket_altqs_active;
   2000      1.1  itojun 		break;
   2001      1.1  itojun 	}
   2002      1.1  itojun 
   2003      1.1  itojun 	case DIOCGETALTQ: {
   2004      1.1  itojun 		struct pfioc_altq	*pa = (struct pfioc_altq *)addr;
   2005      1.1  itojun 		struct pf_altq		*altq;
   2006      1.1  itojun 		u_int32_t		 nr;
   2007      1.1  itojun 
   2008      1.1  itojun 		if (pa->ticket != ticket_altqs_active) {
   2009      1.1  itojun 			error = EBUSY;
   2010      1.1  itojun 			break;
   2011      1.1  itojun 		}
   2012      1.1  itojun 		nr = 0;
   2013      1.1  itojun 		altq = TAILQ_FIRST(pf_altqs_active);
   2014      1.1  itojun 		while ((altq != NULL) && (nr < pa->nr)) {
   2015      1.1  itojun 			altq = TAILQ_NEXT(altq, entries);
   2016      1.1  itojun 			nr++;
   2017      1.1  itojun 		}
   2018      1.1  itojun 		if (altq == NULL) {
   2019      1.1  itojun 			error = EBUSY;
   2020      1.1  itojun 			break;
   2021      1.1  itojun 		}
   2022      1.1  itojun 		bcopy(altq, &pa->altq, sizeof(struct pf_altq));
   2023      1.1  itojun 		break;
   2024      1.1  itojun 	}
   2025      1.1  itojun 
   2026      1.1  itojun 	case DIOCCHANGEALTQ:
   2027      1.1  itojun 		/* CHANGEALTQ not supported yet! */
   2028      1.1  itojun 		error = ENODEV;
   2029      1.1  itojun 		break;
   2030      1.1  itojun 
   2031      1.1  itojun 	case DIOCGETQSTATS: {
   2032      1.1  itojun 		struct pfioc_qstats	*pq = (struct pfioc_qstats *)addr;
   2033      1.1  itojun 		struct pf_altq		*altq;
   2034      1.1  itojun 		u_int32_t		 nr;
   2035      1.1  itojun 		int			 nbytes;
   2036      1.1  itojun 
   2037      1.1  itojun 		if (pq->ticket != ticket_altqs_active) {
   2038      1.1  itojun 			error = EBUSY;
   2039      1.1  itojun 			break;
   2040      1.1  itojun 		}
   2041      1.1  itojun 		nbytes = pq->nbytes;
   2042      1.1  itojun 		nr = 0;
   2043      1.1  itojun 		altq = TAILQ_FIRST(pf_altqs_active);
   2044      1.1  itojun 		while ((altq != NULL) && (nr < pq->nr)) {
   2045      1.1  itojun 			altq = TAILQ_NEXT(altq, entries);
   2046      1.1  itojun 			nr++;
   2047      1.1  itojun 		}
   2048      1.1  itojun 		if (altq == NULL) {
   2049      1.1  itojun 			error = EBUSY;
   2050      1.1  itojun 			break;
   2051      1.1  itojun 		}
   2052      1.1  itojun 		error = altq_getqstats(altq, pq->buf, &nbytes);
   2053      1.1  itojun 		if (error == 0) {
   2054      1.1  itojun 			pq->scheduler = altq->scheduler;
   2055      1.1  itojun 			pq->nbytes = nbytes;
   2056      1.1  itojun 		}
   2057      1.1  itojun 		break;
   2058      1.1  itojun 	}
   2059      1.1  itojun #endif /* ALTQ */
   2060      1.1  itojun 
   2061      1.1  itojun 	case DIOCBEGINADDRS: {
   2062      1.1  itojun 		struct pfioc_pooladdr	*pp = (struct pfioc_pooladdr *)addr;
   2063      1.1  itojun 
   2064      1.1  itojun 		pf_empty_pool(&pf_pabuf);
   2065      1.1  itojun 		pp->ticket = ++ticket_pabuf;
   2066      1.1  itojun 		break;
   2067      1.1  itojun 	}
   2068      1.1  itojun 
   2069      1.1  itojun 	case DIOCADDADDR: {
   2070      1.1  itojun 		struct pfioc_pooladdr	*pp = (struct pfioc_pooladdr *)addr;
   2071      1.1  itojun 
   2072  1.1.1.4  martti 		if (pp->ticket != ticket_pabuf) {
   2073  1.1.1.4  martti 			error = EBUSY;
   2074  1.1.1.4  martti 			break;
   2075  1.1.1.4  martti 		}
   2076      1.1  itojun #ifndef INET
   2077      1.1  itojun 		if (pp->af == AF_INET) {
   2078      1.1  itojun 			error = EAFNOSUPPORT;
   2079      1.1  itojun 			break;
   2080      1.1  itojun 		}
   2081      1.1  itojun #endif /* INET */
   2082      1.1  itojun #ifndef INET6
   2083      1.1  itojun 		if (pp->af == AF_INET6) {
   2084      1.1  itojun 			error = EAFNOSUPPORT;
   2085      1.1  itojun 			break;
   2086      1.1  itojun 		}
   2087      1.1  itojun #endif /* INET6 */
   2088      1.1  itojun 		if (pp->addr.addr.type != PF_ADDR_ADDRMASK &&
   2089      1.1  itojun 		    pp->addr.addr.type != PF_ADDR_DYNIFTL &&
   2090      1.1  itojun 		    pp->addr.addr.type != PF_ADDR_TABLE) {
   2091      1.1  itojun 			error = EINVAL;
   2092      1.1  itojun 			break;
   2093      1.1  itojun 		}
   2094      1.1  itojun 		pa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
   2095      1.1  itojun 		if (pa == NULL) {
   2096      1.1  itojun 			error = ENOMEM;
   2097      1.1  itojun 			break;
   2098      1.1  itojun 		}
   2099      1.1  itojun 		bcopy(&pp->addr, pa, sizeof(struct pf_pooladdr));
   2100      1.1  itojun 		if (pa->ifname[0]) {
   2101  1.1.1.4  martti 			pa->kif = pfi_kif_get(pa->ifname);
   2102      1.1  itojun 			if (pa->kif == NULL) {
   2103      1.1  itojun 				pool_put(&pf_pooladdr_pl, pa);
   2104      1.1  itojun 				error = EINVAL;
   2105      1.1  itojun 				break;
   2106      1.1  itojun 			}
   2107  1.1.1.4  martti 			pfi_kif_ref(pa->kif, PFI_KIF_REF_RULE);
   2108      1.1  itojun 		}
   2109      1.1  itojun 		if (pfi_dynaddr_setup(&pa->addr, pp->af)) {
   2110      1.1  itojun 			pfi_dynaddr_remove(&pa->addr);
   2111  1.1.1.4  martti 			pfi_kif_unref(pa->kif, PFI_KIF_REF_RULE);
   2112      1.1  itojun 			pool_put(&pf_pooladdr_pl, pa);
   2113      1.1  itojun 			error = EINVAL;
   2114      1.1  itojun 			break;
   2115      1.1  itojun 		}
   2116      1.1  itojun 		TAILQ_INSERT_TAIL(&pf_pabuf, pa, entries);
   2117      1.1  itojun 		break;
   2118      1.1  itojun 	}
   2119      1.1  itojun 
   2120      1.1  itojun 	case DIOCGETADDRS: {
   2121      1.1  itojun 		struct pfioc_pooladdr	*pp = (struct pfioc_pooladdr *)addr;
   2122      1.1  itojun 
   2123      1.1  itojun 		pp->nr = 0;
   2124  1.1.1.2    yamt 		pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
   2125  1.1.1.2    yamt 		    pp->r_num, 0, 1, 0);
   2126      1.1  itojun 		if (pool == NULL) {
   2127      1.1  itojun 			error = EBUSY;
   2128      1.1  itojun 			break;
   2129      1.1  itojun 		}
   2130      1.1  itojun 		TAILQ_FOREACH(pa, &pool->list, entries)
   2131      1.1  itojun 			pp->nr++;
   2132      1.1  itojun 		break;
   2133      1.1  itojun 	}
   2134      1.1  itojun 
   2135      1.1  itojun 	case DIOCGETADDR: {
   2136      1.1  itojun 		struct pfioc_pooladdr	*pp = (struct pfioc_pooladdr *)addr;
   2137      1.1  itojun 		u_int32_t		 nr = 0;
   2138      1.1  itojun 
   2139  1.1.1.2    yamt 		pool = pf_get_pool(pp->anchor, pp->ticket, pp->r_action,
   2140  1.1.1.2    yamt 		    pp->r_num, 0, 1, 1);
   2141      1.1  itojun 		if (pool == NULL) {
   2142      1.1  itojun 			error = EBUSY;
   2143      1.1  itojun 			break;
   2144      1.1  itojun 		}
   2145      1.1  itojun 		pa = TAILQ_FIRST(&pool->list);
   2146      1.1  itojun 		while ((pa != NULL) && (nr < pp->nr)) {
   2147      1.1  itojun 			pa = TAILQ_NEXT(pa, entries);
   2148      1.1  itojun 			nr++;
   2149      1.1  itojun 		}
   2150      1.1  itojun 		if (pa == NULL) {
   2151      1.1  itojun 			error = EBUSY;
   2152      1.1  itojun 			break;
   2153      1.1  itojun 		}
   2154      1.1  itojun 		bcopy(pa, &pp->addr, sizeof(struct pf_pooladdr));
   2155      1.1  itojun 		pfi_dynaddr_copyout(&pp->addr.addr);
   2156      1.1  itojun 		pf_tbladdr_copyout(&pp->addr.addr);
   2157  1.1.1.3   peter 		pf_rtlabel_copyout(&pp->addr.addr);
   2158      1.1  itojun 		break;
   2159      1.1  itojun 	}
   2160      1.1  itojun 
   2161      1.1  itojun 	case DIOCCHANGEADDR: {
   2162      1.1  itojun 		struct pfioc_pooladdr	*pca = (struct pfioc_pooladdr *)addr;
   2163      1.1  itojun 		struct pf_pooladdr	*oldpa = NULL, *newpa = NULL;
   2164      1.1  itojun 		struct pf_ruleset	*ruleset;
   2165      1.1  itojun 
   2166      1.1  itojun 		if (pca->action < PF_CHANGE_ADD_HEAD ||
   2167      1.1  itojun 		    pca->action > PF_CHANGE_REMOVE) {
   2168      1.1  itojun 			error = EINVAL;
   2169      1.1  itojun 			break;
   2170      1.1  itojun 		}
   2171      1.1  itojun 		if (pca->addr.addr.type != PF_ADDR_ADDRMASK &&
   2172      1.1  itojun 		    pca->addr.addr.type != PF_ADDR_DYNIFTL &&
   2173      1.1  itojun 		    pca->addr.addr.type != PF_ADDR_TABLE) {
   2174      1.1  itojun 			error = EINVAL;
   2175      1.1  itojun 			break;
   2176      1.1  itojun 		}
   2177      1.1  itojun 
   2178  1.1.1.2    yamt 		ruleset = pf_find_ruleset(pca->anchor);
   2179      1.1  itojun 		if (ruleset == NULL) {
   2180      1.1  itojun 			error = EBUSY;
   2181      1.1  itojun 			break;
   2182      1.1  itojun 		}
   2183  1.1.1.2    yamt 		pool = pf_get_pool(pca->anchor, pca->ticket, pca->r_action,
   2184  1.1.1.2    yamt 		    pca->r_num, pca->r_last, 1, 1);
   2185      1.1  itojun 		if (pool == NULL) {
   2186      1.1  itojun 			error = EBUSY;
   2187      1.1  itojun 			break;
   2188      1.1  itojun 		}
   2189      1.1  itojun 		if (pca->action != PF_CHANGE_REMOVE) {
   2190      1.1  itojun 			newpa = pool_get(&pf_pooladdr_pl, PR_NOWAIT);
   2191      1.1  itojun 			if (newpa == NULL) {
   2192      1.1  itojun 				error = ENOMEM;
   2193      1.1  itojun 				break;
   2194      1.1  itojun 			}
   2195      1.1  itojun 			bcopy(&pca->addr, newpa, sizeof(struct pf_pooladdr));
   2196      1.1  itojun #ifndef INET
   2197      1.1  itojun 			if (pca->af == AF_INET) {
   2198      1.1  itojun 				pool_put(&pf_pooladdr_pl, newpa);
   2199      1.1  itojun 				error = EAFNOSUPPORT;
   2200      1.1  itojun 				break;
   2201      1.1  itojun 			}
   2202      1.1  itojun #endif /* INET */
   2203      1.1  itojun #ifndef INET6
   2204      1.1  itojun 			if (pca->af == AF_INET6) {
   2205      1.1  itojun 				pool_put(&pf_pooladdr_pl, newpa);
   2206      1.1  itojun 				error = EAFNOSUPPORT;
   2207      1.1  itojun 				break;
   2208      1.1  itojun 			}
   2209      1.1  itojun #endif /* INET6 */
   2210      1.1  itojun 			if (newpa->ifname[0]) {
   2211  1.1.1.4  martti 				newpa->kif = pfi_kif_get(newpa->ifname);
   2212      1.1  itojun 				if (newpa->kif == NULL) {
   2213      1.1  itojun 					pool_put(&pf_pooladdr_pl, newpa);
   2214      1.1  itojun 					error = EINVAL;
   2215      1.1  itojun 					break;
   2216      1.1  itojun 				}
   2217  1.1.1.4  martti 				pfi_kif_ref(newpa->kif, PFI_KIF_REF_RULE);
   2218      1.1  itojun 			} else
   2219      1.1  itojun 				newpa->kif = NULL;
   2220      1.1  itojun 			if (pfi_dynaddr_setup(&newpa->addr, pca->af) ||
   2221      1.1  itojun 			    pf_tbladdr_setup(ruleset, &newpa->addr)) {
   2222      1.1  itojun 				pfi_dynaddr_remove(&newpa->addr);
   2223  1.1.1.4  martti 				pfi_kif_unref(newpa->kif, PFI_KIF_REF_RULE);
   2224      1.1  itojun 				pool_put(&pf_pooladdr_pl, newpa);
   2225      1.1  itojun 				error = EINVAL;
   2226      1.1  itojun 				break;
   2227      1.1  itojun 			}
   2228      1.1  itojun 		}
   2229      1.1  itojun 
   2230      1.1  itojun 		if (pca->action == PF_CHANGE_ADD_HEAD)
   2231      1.1  itojun 			oldpa = TAILQ_FIRST(&pool->list);
   2232      1.1  itojun 		else if (pca->action == PF_CHANGE_ADD_TAIL)
   2233      1.1  itojun 			oldpa = TAILQ_LAST(&pool->list, pf_palist);
   2234      1.1  itojun 		else {
   2235      1.1  itojun 			int	i = 0;
   2236      1.1  itojun 
   2237      1.1  itojun 			oldpa = TAILQ_FIRST(&pool->list);
   2238      1.1  itojun 			while ((oldpa != NULL) && (i < pca->nr)) {
   2239      1.1  itojun 				oldpa = TAILQ_NEXT(oldpa, entries);
   2240      1.1  itojun 				i++;
   2241      1.1  itojun 			}
   2242      1.1  itojun 			if (oldpa == NULL) {
   2243      1.1  itojun 				error = EINVAL;
   2244      1.1  itojun 				break;
   2245      1.1  itojun 			}
   2246      1.1  itojun 		}
   2247      1.1  itojun 
   2248      1.1  itojun 		if (pca->action == PF_CHANGE_REMOVE) {
   2249      1.1  itojun 			TAILQ_REMOVE(&pool->list, oldpa, entries);
   2250      1.1  itojun 			pfi_dynaddr_remove(&oldpa->addr);
   2251      1.1  itojun 			pf_tbladdr_remove(&oldpa->addr);
   2252  1.1.1.4  martti 			pfi_kif_unref(oldpa->kif, PFI_KIF_REF_RULE);
   2253      1.1  itojun 			pool_put(&pf_pooladdr_pl, oldpa);
   2254      1.1  itojun 		} else {
   2255      1.1  itojun 			if (oldpa == NULL)
   2256      1.1  itojun 				TAILQ_INSERT_TAIL(&pool->list, newpa, entries);
   2257      1.1  itojun 			else if (pca->action == PF_CHANGE_ADD_HEAD ||
   2258      1.1  itojun 			    pca->action == PF_CHANGE_ADD_BEFORE)
   2259      1.1  itojun 				TAILQ_INSERT_BEFORE(oldpa, newpa, entries);
   2260      1.1  itojun 			else
   2261      1.1  itojun 				TAILQ_INSERT_AFTER(&pool->list, oldpa,
   2262      1.1  itojun 				    newpa, entries);
   2263      1.1  itojun 		}
   2264      1.1  itojun 
   2265      1.1  itojun 		pool->cur = TAILQ_FIRST(&pool->list);
   2266      1.1  itojun 		PF_ACPY(&pool->counter, &pool->cur->addr.v.a.addr,
   2267      1.1  itojun 		    pca->af);
   2268      1.1  itojun 		break;
   2269      1.1  itojun 	}
   2270      1.1  itojun 
   2271      1.1  itojun 	case DIOCGETRULESETS: {
   2272      1.1  itojun 		struct pfioc_ruleset	*pr = (struct pfioc_ruleset *)addr;
   2273      1.1  itojun 		struct pf_ruleset	*ruleset;
   2274  1.1.1.2    yamt 		struct pf_anchor	*anchor;
   2275      1.1  itojun 
   2276  1.1.1.2    yamt 		pr->path[sizeof(pr->path) - 1] = 0;
   2277  1.1.1.2    yamt 		if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
   2278      1.1  itojun 			error = EINVAL;
   2279      1.1  itojun 			break;
   2280      1.1  itojun 		}
   2281      1.1  itojun 		pr->nr = 0;
   2282  1.1.1.2    yamt 		if (ruleset->anchor == NULL) {
   2283  1.1.1.2    yamt 			/* XXX kludge for pf_main_ruleset */
   2284  1.1.1.2    yamt 			RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
   2285  1.1.1.2    yamt 				if (anchor->parent == NULL)
   2286  1.1.1.2    yamt 					pr->nr++;
   2287  1.1.1.2    yamt 		} else {
   2288  1.1.1.2    yamt 			RB_FOREACH(anchor, pf_anchor_node,
   2289  1.1.1.2    yamt 			    &ruleset->anchor->children)
   2290  1.1.1.2    yamt 				pr->nr++;
   2291  1.1.1.2    yamt 		}
   2292      1.1  itojun 		break;
   2293      1.1  itojun 	}
   2294      1.1  itojun 
   2295      1.1  itojun 	case DIOCGETRULESET: {
   2296      1.1  itojun 		struct pfioc_ruleset	*pr = (struct pfioc_ruleset *)addr;
   2297      1.1  itojun 		struct pf_ruleset	*ruleset;
   2298  1.1.1.2    yamt 		struct pf_anchor	*anchor;
   2299      1.1  itojun 		u_int32_t		 nr = 0;
   2300      1.1  itojun 
   2301  1.1.1.2    yamt 		pr->path[sizeof(pr->path) - 1] = 0;
   2302  1.1.1.2    yamt 		if ((ruleset = pf_find_ruleset(pr->path)) == NULL) {
   2303      1.1  itojun 			error = EINVAL;
   2304      1.1  itojun 			break;
   2305      1.1  itojun 		}
   2306  1.1.1.2    yamt 		pr->name[0] = 0;
   2307  1.1.1.2    yamt 		if (ruleset->anchor == NULL) {
   2308  1.1.1.2    yamt 			/* XXX kludge for pf_main_ruleset */
   2309  1.1.1.2    yamt 			RB_FOREACH(anchor, pf_anchor_global, &pf_anchors)
   2310  1.1.1.2    yamt 				if (anchor->parent == NULL && nr++ == pr->nr) {
   2311  1.1.1.2    yamt 					strlcpy(pr->name, anchor->name,
   2312  1.1.1.2    yamt 					    sizeof(pr->name));
   2313  1.1.1.2    yamt 					break;
   2314  1.1.1.2    yamt 				}
   2315  1.1.1.2    yamt 		} else {
   2316  1.1.1.2    yamt 			RB_FOREACH(anchor, pf_anchor_node,
   2317  1.1.1.2    yamt 			    &ruleset->anchor->children)
   2318  1.1.1.2    yamt 				if (nr++ == pr->nr) {
   2319  1.1.1.2    yamt 					strlcpy(pr->name, anchor->name,
   2320  1.1.1.2    yamt 					    sizeof(pr->name));
   2321  1.1.1.2    yamt 					break;
   2322  1.1.1.2    yamt 				}
   2323      1.1  itojun 		}
   2324  1.1.1.2    yamt 		if (!pr->name[0])
   2325      1.1  itojun 			error = EBUSY;
   2326      1.1  itojun 		break;
   2327      1.1  itojun 	}
   2328      1.1  itojun 
   2329      1.1  itojun 	case DIOCRCLRTABLES: {
   2330      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2331      1.1  itojun 
   2332      1.1  itojun 		if (io->pfrio_esize != 0) {
   2333      1.1  itojun 			error = ENODEV;
   2334      1.1  itojun 			break;
   2335      1.1  itojun 		}
   2336      1.1  itojun 		error = pfr_clr_tables(&io->pfrio_table, &io->pfrio_ndel,
   2337      1.1  itojun 		    io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2338      1.1  itojun 		break;
   2339      1.1  itojun 	}
   2340      1.1  itojun 
   2341      1.1  itojun 	case DIOCRADDTABLES: {
   2342      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2343      1.1  itojun 
   2344      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
   2345      1.1  itojun 			error = ENODEV;
   2346      1.1  itojun 			break;
   2347      1.1  itojun 		}
   2348      1.1  itojun 		error = pfr_add_tables(io->pfrio_buffer, io->pfrio_size,
   2349      1.1  itojun 		    &io->pfrio_nadd, io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2350      1.1  itojun 		break;
   2351      1.1  itojun 	}
   2352      1.1  itojun 
   2353      1.1  itojun 	case DIOCRDELTABLES: {
   2354      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2355      1.1  itojun 
   2356      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
   2357      1.1  itojun 			error = ENODEV;
   2358      1.1  itojun 			break;
   2359      1.1  itojun 		}
   2360      1.1  itojun 		error = pfr_del_tables(io->pfrio_buffer, io->pfrio_size,
   2361      1.1  itojun 		    &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2362      1.1  itojun 		break;
   2363      1.1  itojun 	}
   2364      1.1  itojun 
   2365      1.1  itojun 	case DIOCRGETTABLES: {
   2366      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2367      1.1  itojun 
   2368      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
   2369      1.1  itojun 			error = ENODEV;
   2370      1.1  itojun 			break;
   2371      1.1  itojun 		}
   2372      1.1  itojun 		error = pfr_get_tables(&io->pfrio_table, io->pfrio_buffer,
   2373      1.1  itojun 		    &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2374      1.1  itojun 		break;
   2375      1.1  itojun 	}
   2376      1.1  itojun 
   2377      1.1  itojun 	case DIOCRGETTSTATS: {
   2378      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2379      1.1  itojun 
   2380      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_tstats)) {
   2381      1.1  itojun 			error = ENODEV;
   2382      1.1  itojun 			break;
   2383      1.1  itojun 		}
   2384      1.1  itojun 		error = pfr_get_tstats(&io->pfrio_table, io->pfrio_buffer,
   2385      1.1  itojun 		    &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2386      1.1  itojun 		break;
   2387      1.1  itojun 	}
   2388      1.1  itojun 
   2389      1.1  itojun 	case DIOCRCLRTSTATS: {
   2390      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2391      1.1  itojun 
   2392      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
   2393      1.1  itojun 			error = ENODEV;
   2394      1.1  itojun 			break;
   2395      1.1  itojun 		}
   2396      1.1  itojun 		error = pfr_clr_tstats(io->pfrio_buffer, io->pfrio_size,
   2397      1.1  itojun 		    &io->pfrio_nzero, io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2398      1.1  itojun 		break;
   2399      1.1  itojun 	}
   2400      1.1  itojun 
   2401      1.1  itojun 	case DIOCRSETTFLAGS: {
   2402      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2403      1.1  itojun 
   2404      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_table)) {
   2405      1.1  itojun 			error = ENODEV;
   2406      1.1  itojun 			break;
   2407      1.1  itojun 		}
   2408      1.1  itojun 		error = pfr_set_tflags(io->pfrio_buffer, io->pfrio_size,
   2409      1.1  itojun 		    io->pfrio_setflag, io->pfrio_clrflag, &io->pfrio_nchange,
   2410      1.1  itojun 		    &io->pfrio_ndel, io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2411      1.1  itojun 		break;
   2412      1.1  itojun 	}
   2413      1.1  itojun 
   2414      1.1  itojun 	case DIOCRCLRADDRS: {
   2415      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2416      1.1  itojun 
   2417      1.1  itojun 		if (io->pfrio_esize != 0) {
   2418      1.1  itojun 			error = ENODEV;
   2419      1.1  itojun 			break;
   2420      1.1  itojun 		}
   2421      1.1  itojun 		error = pfr_clr_addrs(&io->pfrio_table, &io->pfrio_ndel,
   2422      1.1  itojun 		    io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2423      1.1  itojun 		break;
   2424      1.1  itojun 	}
   2425      1.1  itojun 
   2426      1.1  itojun 	case DIOCRADDADDRS: {
   2427      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2428      1.1  itojun 
   2429      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
   2430      1.1  itojun 			error = ENODEV;
   2431      1.1  itojun 			break;
   2432      1.1  itojun 		}
   2433      1.1  itojun 		error = pfr_add_addrs(&io->pfrio_table, io->pfrio_buffer,
   2434      1.1  itojun 		    io->pfrio_size, &io->pfrio_nadd, io->pfrio_flags |
   2435      1.1  itojun 		    PFR_FLAG_USERIOCTL);
   2436      1.1  itojun 		break;
   2437      1.1  itojun 	}
   2438      1.1  itojun 
   2439      1.1  itojun 	case DIOCRDELADDRS: {
   2440      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2441      1.1  itojun 
   2442      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
   2443      1.1  itojun 			error = ENODEV;
   2444      1.1  itojun 			break;
   2445      1.1  itojun 		}
   2446      1.1  itojun 		error = pfr_del_addrs(&io->pfrio_table, io->pfrio_buffer,
   2447      1.1  itojun 		    io->pfrio_size, &io->pfrio_ndel, io->pfrio_flags |
   2448      1.1  itojun 		    PFR_FLAG_USERIOCTL);
   2449      1.1  itojun 		break;
   2450      1.1  itojun 	}
   2451      1.1  itojun 
   2452      1.1  itojun 	case DIOCRSETADDRS: {
   2453      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2454      1.1  itojun 
   2455      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
   2456      1.1  itojun 			error = ENODEV;
   2457      1.1  itojun 			break;
   2458      1.1  itojun 		}
   2459      1.1  itojun 		error = pfr_set_addrs(&io->pfrio_table, io->pfrio_buffer,
   2460      1.1  itojun 		    io->pfrio_size, &io->pfrio_size2, &io->pfrio_nadd,
   2461      1.1  itojun 		    &io->pfrio_ndel, &io->pfrio_nchange, io->pfrio_flags |
   2462  1.1.1.4  martti 		    PFR_FLAG_USERIOCTL, 0);
   2463      1.1  itojun 		break;
   2464      1.1  itojun 	}
   2465      1.1  itojun 
   2466      1.1  itojun 	case DIOCRGETADDRS: {
   2467      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2468      1.1  itojun 
   2469      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
   2470      1.1  itojun 			error = ENODEV;
   2471      1.1  itojun 			break;
   2472      1.1  itojun 		}
   2473      1.1  itojun 		error = pfr_get_addrs(&io->pfrio_table, io->pfrio_buffer,
   2474      1.1  itojun 		    &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2475      1.1  itojun 		break;
   2476      1.1  itojun 	}
   2477      1.1  itojun 
   2478      1.1  itojun 	case DIOCRGETASTATS: {
   2479      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2480      1.1  itojun 
   2481      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_astats)) {
   2482      1.1  itojun 			error = ENODEV;
   2483      1.1  itojun 			break;
   2484      1.1  itojun 		}
   2485      1.1  itojun 		error = pfr_get_astats(&io->pfrio_table, io->pfrio_buffer,
   2486      1.1  itojun 		    &io->pfrio_size, io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2487      1.1  itojun 		break;
   2488      1.1  itojun 	}
   2489      1.1  itojun 
   2490      1.1  itojun 	case DIOCRCLRASTATS: {
   2491      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2492      1.1  itojun 
   2493      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
   2494      1.1  itojun 			error = ENODEV;
   2495      1.1  itojun 			break;
   2496      1.1  itojun 		}
   2497      1.1  itojun 		error = pfr_clr_astats(&io->pfrio_table, io->pfrio_buffer,
   2498      1.1  itojun 		    io->pfrio_size, &io->pfrio_nzero, io->pfrio_flags |
   2499      1.1  itojun 		    PFR_FLAG_USERIOCTL);
   2500      1.1  itojun 		break;
   2501      1.1  itojun 	}
   2502      1.1  itojun 
   2503      1.1  itojun 	case DIOCRTSTADDRS: {
   2504      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2505      1.1  itojun 
   2506      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
   2507      1.1  itojun 			error = ENODEV;
   2508      1.1  itojun 			break;
   2509      1.1  itojun 		}
   2510      1.1  itojun 		error = pfr_tst_addrs(&io->pfrio_table, io->pfrio_buffer,
   2511      1.1  itojun 		    io->pfrio_size, &io->pfrio_nmatch, io->pfrio_flags |
   2512      1.1  itojun 		    PFR_FLAG_USERIOCTL);
   2513      1.1  itojun 		break;
   2514      1.1  itojun 	}
   2515      1.1  itojun 
   2516      1.1  itojun 	case DIOCRINADEFINE: {
   2517      1.1  itojun 		struct pfioc_table *io = (struct pfioc_table *)addr;
   2518      1.1  itojun 
   2519      1.1  itojun 		if (io->pfrio_esize != sizeof(struct pfr_addr)) {
   2520      1.1  itojun 			error = ENODEV;
   2521      1.1  itojun 			break;
   2522      1.1  itojun 		}
   2523      1.1  itojun 		error = pfr_ina_define(&io->pfrio_table, io->pfrio_buffer,
   2524      1.1  itojun 		    io->pfrio_size, &io->pfrio_nadd, &io->pfrio_naddr,
   2525      1.1  itojun 		    io->pfrio_ticket, io->pfrio_flags | PFR_FLAG_USERIOCTL);
   2526      1.1  itojun 		break;
   2527      1.1  itojun 	}
   2528      1.1  itojun 
   2529      1.1  itojun 	case DIOCOSFPADD: {
   2530      1.1  itojun 		struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
   2531      1.1  itojun 		error = pf_osfp_add(io);
   2532      1.1  itojun 		break;
   2533      1.1  itojun 	}
   2534      1.1  itojun 
   2535      1.1  itojun 	case DIOCOSFPGET: {
   2536      1.1  itojun 		struct pf_osfp_ioctl *io = (struct pf_osfp_ioctl *)addr;
   2537      1.1  itojun 		error = pf_osfp_get(io);
   2538      1.1  itojun 		break;
   2539      1.1  itojun 	}
   2540      1.1  itojun 
   2541      1.1  itojun 	case DIOCXBEGIN: {
   2542  1.1.1.4  martti 		struct pfioc_trans	*io = (struct pfioc_trans *)addr;
   2543  1.1.1.4  martti 		struct pfioc_trans_e	*ioe;
   2544  1.1.1.4  martti 		struct pfr_table	*table;
   2545  1.1.1.4  martti 		int			 i;
   2546      1.1  itojun 
   2547  1.1.1.4  martti 		if (io->esize != sizeof(*ioe)) {
   2548      1.1  itojun 			error = ENODEV;
   2549      1.1  itojun 			goto fail;
   2550      1.1  itojun 		}
   2551  1.1.1.4  martti 		ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
   2552  1.1.1.4  martti 		    M_TEMP, M_WAITOK);
   2553  1.1.1.4  martti 		table = (struct pfr_table *)malloc(sizeof(*table),
   2554  1.1.1.4  martti 		    M_TEMP, M_WAITOK);
   2555      1.1  itojun 		for (i = 0; i < io->size; i++) {
   2556  1.1.1.4  martti 			if (copyin(io->array+i, ioe, sizeof(*ioe))) {
   2557  1.1.1.4  martti 				free(table, M_TEMP);
   2558  1.1.1.4  martti 				free(ioe, M_TEMP);
   2559      1.1  itojun 				error = EFAULT;
   2560      1.1  itojun 				goto fail;
   2561      1.1  itojun 			}
   2562  1.1.1.4  martti 			switch (ioe->rs_num) {
   2563      1.1  itojun #ifdef ALTQ
   2564      1.1  itojun 			case PF_RULESET_ALTQ:
   2565  1.1.1.4  martti 				if (ioe->anchor[0]) {
   2566  1.1.1.4  martti 					free(table, M_TEMP);
   2567  1.1.1.4  martti 					free(ioe, M_TEMP);
   2568      1.1  itojun 					error = EINVAL;
   2569      1.1  itojun 					goto fail;
   2570      1.1  itojun 				}
   2571  1.1.1.4  martti 				if ((error = pf_begin_altq(&ioe->ticket))) {
   2572  1.1.1.4  martti 					free(table, M_TEMP);
   2573  1.1.1.4  martti 					free(ioe, M_TEMP);
   2574      1.1  itojun 					goto fail;
   2575  1.1.1.4  martti 				}
   2576      1.1  itojun 				break;
   2577      1.1  itojun #endif /* ALTQ */
   2578      1.1  itojun 			case PF_RULESET_TABLE:
   2579  1.1.1.4  martti 				bzero(table, sizeof(*table));
   2580  1.1.1.4  martti 				strlcpy(table->pfrt_anchor, ioe->anchor,
   2581  1.1.1.4  martti 				    sizeof(table->pfrt_anchor));
   2582  1.1.1.4  martti 				if ((error = pfr_ina_begin(table,
   2583  1.1.1.4  martti 				    &ioe->ticket, NULL, 0))) {
   2584  1.1.1.4  martti 					free(table, M_TEMP);
   2585  1.1.1.4  martti 					free(ioe, M_TEMP);
   2586      1.1  itojun 					goto fail;
   2587  1.1.1.4  martti 				}
   2588      1.1  itojun 				break;
   2589      1.1  itojun 			default:
   2590  1.1.1.4  martti 				if ((error = pf_begin_rules(&ioe->ticket,
   2591  1.1.1.4  martti 				    ioe->rs_num, ioe->anchor))) {
   2592  1.1.1.4  martti 					free(table, M_TEMP);
   2593  1.1.1.4  martti 					free(ioe, M_TEMP);
   2594      1.1  itojun 					goto fail;
   2595  1.1.1.4  martti 				}
   2596      1.1  itojun 				break;
   2597      1.1  itojun 			}
   2598  1.1.1.4  martti 			if (copyout(ioe, io->array+i, sizeof(io->array[i]))) {
   2599  1.1.1.4  martti 				free(table, M_TEMP);
   2600  1.1.1.4  martti 				free(ioe, M_TEMP);
   2601      1.1  itojun 				error = EFAULT;
   2602      1.1  itojun 				goto fail;
   2603      1.1  itojun 			}
   2604      1.1  itojun 		}
   2605  1.1.1.4  martti 		free(table, M_TEMP);
   2606  1.1.1.4  martti 		free(ioe, M_TEMP);
   2607      1.1  itojun 		break;
   2608      1.1  itojun 	}
   2609      1.1  itojun 
   2610      1.1  itojun 	case DIOCXROLLBACK: {
   2611  1.1.1.4  martti 		struct pfioc_trans	*io = (struct pfioc_trans *)addr;
   2612  1.1.1.4  martti 		struct pfioc_trans_e	*ioe;
   2613  1.1.1.4  martti 		struct pfr_table	*table;
   2614  1.1.1.4  martti 		int			 i;
   2615      1.1  itojun 
   2616  1.1.1.4  martti 		if (io->esize != sizeof(*ioe)) {
   2617      1.1  itojun 			error = ENODEV;
   2618      1.1  itojun 			goto fail;
   2619      1.1  itojun 		}
   2620  1.1.1.4  martti 		ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
   2621  1.1.1.4  martti 		    M_TEMP, M_WAITOK);
   2622  1.1.1.4  martti 		table = (struct pfr_table *)malloc(sizeof(*table),
   2623  1.1.1.4  martti 		    M_TEMP, M_WAITOK);
   2624      1.1  itojun 		for (i = 0; i < io->size; i++) {
   2625  1.1.1.4  martti 			if (copyin(io->array+i, ioe, sizeof(*ioe))) {
   2626  1.1.1.4  martti 				free(table, M_TEMP);
   2627  1.1.1.4  martti 				free(ioe, M_TEMP);
   2628      1.1  itojun 				error = EFAULT;
   2629      1.1  itojun 				goto fail;
   2630      1.1  itojun 			}
   2631  1.1.1.4  martti 			switch (ioe->rs_num) {
   2632      1.1  itojun #ifdef ALTQ
   2633      1.1  itojun 			case PF_RULESET_ALTQ:
   2634  1.1.1.4  martti 				if (ioe->anchor[0]) {
   2635  1.1.1.4  martti 					free(table, M_TEMP);
   2636  1.1.1.4  martti 					free(ioe, M_TEMP);
   2637      1.1  itojun 					error = EINVAL;
   2638      1.1  itojun 					goto fail;
   2639      1.1  itojun 				}
   2640  1.1.1.4  martti 				if ((error = pf_rollback_altq(ioe->ticket))) {
   2641  1.1.1.4  martti 					free(table, M_TEMP);
   2642  1.1.1.4  martti 					free(ioe, M_TEMP);
   2643      1.1  itojun 					goto fail; /* really bad */
   2644  1.1.1.4  martti 				}
   2645      1.1  itojun 				break;
   2646      1.1  itojun #endif /* ALTQ */
   2647      1.1  itojun 			case PF_RULESET_TABLE:
   2648  1.1.1.4  martti 				bzero(table, sizeof(*table));
   2649  1.1.1.4  martti 				strlcpy(table->pfrt_anchor, ioe->anchor,
   2650  1.1.1.4  martti 				    sizeof(table->pfrt_anchor));
   2651  1.1.1.4  martti 				if ((error = pfr_ina_rollback(table,
   2652  1.1.1.4  martti 				    ioe->ticket, NULL, 0))) {
   2653  1.1.1.4  martti 					free(table, M_TEMP);
   2654  1.1.1.4  martti 					free(ioe, M_TEMP);
   2655      1.1  itojun 					goto fail; /* really bad */
   2656  1.1.1.4  martti 				}
   2657      1.1  itojun 				break;
   2658      1.1  itojun 			default:
   2659  1.1.1.4  martti 				if ((error = pf_rollback_rules(ioe->ticket,
   2660  1.1.1.4  martti 				    ioe->rs_num, ioe->anchor))) {
   2661  1.1.1.4  martti 					free(table, M_TEMP);
   2662  1.1.1.4  martti 					free(ioe, M_TEMP);
   2663      1.1  itojun 					goto fail; /* really bad */
   2664  1.1.1.4  martti 				}
   2665      1.1  itojun 				break;
   2666      1.1  itojun 			}
   2667      1.1  itojun 		}
   2668  1.1.1.4  martti 		free(table, M_TEMP);
   2669  1.1.1.4  martti 		free(ioe, M_TEMP);
   2670      1.1  itojun 		break;
   2671      1.1  itojun 	}
   2672      1.1  itojun 
   2673      1.1  itojun 	case DIOCXCOMMIT: {
   2674  1.1.1.4  martti 		struct pfioc_trans	*io = (struct pfioc_trans *)addr;
   2675  1.1.1.4  martti 		struct pfioc_trans_e	*ioe;
   2676  1.1.1.4  martti 		struct pfr_table	*table;
   2677  1.1.1.4  martti 		struct pf_ruleset	*rs;
   2678  1.1.1.4  martti 		int			 i;
   2679      1.1  itojun 
   2680  1.1.1.4  martti 		if (io->esize != sizeof(*ioe)) {
   2681      1.1  itojun 			error = ENODEV;
   2682      1.1  itojun 			goto fail;
   2683      1.1  itojun 		}
   2684  1.1.1.4  martti 		ioe = (struct pfioc_trans_e *)malloc(sizeof(*ioe),
   2685  1.1.1.4  martti 		    M_TEMP, M_WAITOK);
   2686  1.1.1.4  martti 		table = (struct pfr_table *)malloc(sizeof(*table),
   2687  1.1.1.4  martti 		    M_TEMP, M_WAITOK);
   2688      1.1  itojun 		/* first makes sure everything will succeed */
   2689      1.1  itojun 		for (i = 0; i < io->size; i++) {
   2690  1.1.1.4  martti 			if (copyin(io->array+i, ioe, sizeof(*ioe))) {
   2691  1.1.1.4  martti 				free(table, M_TEMP);
   2692  1.1.1.4  martti 				free(ioe, M_TEMP);
   2693      1.1  itojun 				error = EFAULT;
   2694      1.1  itojun 				goto fail;
   2695      1.1  itojun 			}
   2696  1.1.1.4  martti 			switch (ioe->rs_num) {
   2697      1.1  itojun #ifdef ALTQ
   2698      1.1  itojun 			case PF_RULESET_ALTQ:
   2699  1.1.1.4  martti 				if (ioe->anchor[0]) {
   2700  1.1.1.4  martti 					free(table, M_TEMP);
   2701  1.1.1.4  martti 					free(ioe, M_TEMP);
   2702      1.1  itojun 					error = EINVAL;
   2703      1.1  itojun 					goto fail;
   2704      1.1  itojun 				}
   2705  1.1.1.4  martti 				if (!altqs_inactive_open || ioe->ticket !=
   2706      1.1  itojun 				    ticket_altqs_inactive) {
   2707  1.1.1.4  martti 					free(table, M_TEMP);
   2708  1.1.1.4  martti 					free(ioe, M_TEMP);
   2709      1.1  itojun 					error = EBUSY;
   2710      1.1  itojun 					goto fail;
   2711      1.1  itojun 				}
   2712      1.1  itojun 				break;
   2713      1.1  itojun #endif /* ALTQ */
   2714      1.1  itojun 			case PF_RULESET_TABLE:
   2715  1.1.1.4  martti 				rs = pf_find_ruleset(ioe->anchor);
   2716  1.1.1.4  martti 				if (rs == NULL || !rs->topen || ioe->ticket !=
   2717      1.1  itojun 				     rs->tticket) {
   2718  1.1.1.4  martti 					free(table, M_TEMP);
   2719  1.1.1.4  martti 					free(ioe, M_TEMP);
   2720      1.1  itojun 					error = EBUSY;
   2721      1.1  itojun 					goto fail;
   2722      1.1  itojun 				}
   2723      1.1  itojun 				break;
   2724      1.1  itojun 			default:
   2725  1.1.1.4  martti 				if (ioe->rs_num < 0 || ioe->rs_num >=
   2726      1.1  itojun 				    PF_RULESET_MAX) {
   2727  1.1.1.4  martti 					free(table, M_TEMP);
   2728  1.1.1.4  martti 					free(ioe, M_TEMP);
   2729      1.1  itojun 					error = EINVAL;
   2730      1.1  itojun 					goto fail;
   2731      1.1  itojun 				}
   2732  1.1.1.4  martti 				rs = pf_find_ruleset(ioe->anchor);
   2733      1.1  itojun 				if (rs == NULL ||
   2734  1.1.1.4  martti 				    !rs->rules[ioe->rs_num].inactive.open ||
   2735  1.1.1.4  martti 				    rs->rules[ioe->rs_num].inactive.ticket !=
   2736  1.1.1.4  martti 				    ioe->ticket) {
   2737  1.1.1.4  martti 					free(table, M_TEMP);
   2738  1.1.1.4  martti 					free(ioe, M_TEMP);
   2739      1.1  itojun 					error = EBUSY;
   2740      1.1  itojun 					goto fail;
   2741      1.1  itojun 				}
   2742      1.1  itojun 				break;
   2743      1.1  itojun 			}
   2744      1.1  itojun 		}
   2745      1.1  itojun 		/* now do the commit - no errors should happen here */
   2746      1.1  itojun 		for (i = 0; i < io->size; i++) {
   2747  1.1.1.4  martti 			if (copyin(io->array+i, ioe, sizeof(*ioe))) {
   2748  1.1.1.4  martti 				free(table, M_TEMP);
   2749  1.1.1.4  martti 				free(ioe, M_TEMP);
   2750      1.1  itojun 				error = EFAULT;
   2751      1.1  itojun 				goto fail;
   2752      1.1  itojun 			}
   2753  1.1.1.4  martti 			switch (ioe->rs_num) {
   2754      1.1  itojun #ifdef ALTQ
   2755      1.1  itojun 			case PF_RULESET_ALTQ:
   2756  1.1.1.4  martti 				if ((error = pf_commit_altq(ioe->ticket))) {
   2757  1.1.1.4  martti 					free(table, M_TEMP);
   2758  1.1.1.4  martti 					free(ioe, M_TEMP);
   2759      1.1  itojun 					goto fail; /* really bad */
   2760  1.1.1.4  martti 				}
   2761      1.1  itojun 				break;
   2762      1.1  itojun #endif /* ALTQ */
   2763      1.1  itojun 			case PF_RULESET_TABLE:
   2764  1.1.1.4  martti 				bzero(table, sizeof(*table));
   2765  1.1.1.4  martti 				strlcpy(table->pfrt_anchor, ioe->anchor,
   2766  1.1.1.4  martti 				    sizeof(table->pfrt_anchor));
   2767  1.1.1.4  martti 				if ((error = pfr_ina_commit(table, ioe->ticket,
   2768  1.1.1.4  martti 				    NULL, NULL, 0))) {
   2769  1.1.1.4  martti 					free(table, M_TEMP);
   2770  1.1.1.4  martti 					free(ioe, M_TEMP);
   2771      1.1  itojun 					goto fail; /* really bad */
   2772  1.1.1.4  martti 				}
   2773      1.1  itojun 				break;
   2774      1.1  itojun 			default:
   2775  1.1.1.4  martti 				if ((error = pf_commit_rules(ioe->ticket,
   2776  1.1.1.4  martti 				    ioe->rs_num, ioe->anchor))) {
   2777  1.1.1.4  martti 					free(table, M_TEMP);
   2778  1.1.1.4  martti 					free(ioe, M_TEMP);
   2779      1.1  itojun 					goto fail; /* really bad */
   2780  1.1.1.4  martti 				}
   2781      1.1  itojun 				break;
   2782      1.1  itojun 			}
   2783      1.1  itojun 		}
   2784  1.1.1.4  martti 		free(table, M_TEMP);
   2785  1.1.1.4  martti 		free(ioe, M_TEMP);
   2786      1.1  itojun 		break;
   2787      1.1  itojun 	}
   2788      1.1  itojun 
   2789      1.1  itojun 	case DIOCGETSRCNODES: {
   2790      1.1  itojun 		struct pfioc_src_nodes	*psn = (struct pfioc_src_nodes *)addr;
   2791  1.1.1.4  martti 		struct pf_src_node	*n, *p, *pstore;
   2792      1.1  itojun 		u_int32_t		 nr = 0;
   2793      1.1  itojun 		int			 space = psn->psn_len;
   2794      1.1  itojun 
   2795      1.1  itojun 		if (space == 0) {
   2796      1.1  itojun 			RB_FOREACH(n, pf_src_tree, &tree_src_tracking)
   2797      1.1  itojun 				nr++;
   2798      1.1  itojun 			psn->psn_len = sizeof(struct pf_src_node) * nr;
   2799  1.1.1.3   peter 			break;
   2800      1.1  itojun 		}
   2801      1.1  itojun 
   2802  1.1.1.4  martti 		pstore = malloc(sizeof(*pstore), M_TEMP, M_WAITOK);
   2803  1.1.1.4  martti 
   2804      1.1  itojun 		p = psn->psn_src_nodes;
   2805      1.1  itojun 		RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
   2806  1.1.1.3   peter 			int	secs = time_second, diff;
   2807      1.1  itojun 
   2808      1.1  itojun 			if ((nr + 1) * sizeof(*p) > (unsigned)psn->psn_len)
   2809      1.1  itojun 				break;
   2810      1.1  itojun 
   2811  1.1.1.4  martti 			bcopy(n, pstore, sizeof(*pstore));
   2812      1.1  itojun 			if (n->rule.ptr != NULL)
   2813  1.1.1.4  martti 				pstore->rule.nr = n->rule.ptr->nr;
   2814  1.1.1.4  martti 			pstore->creation = secs - pstore->creation;
   2815  1.1.1.4  martti 			if (pstore->expire > secs)
   2816  1.1.1.4  martti 				pstore->expire -= secs;
   2817      1.1  itojun 			else
   2818  1.1.1.4  martti 				pstore->expire = 0;
   2819  1.1.1.3   peter 
   2820  1.1.1.3   peter 			/* adjust the connection rate estimate */
   2821  1.1.1.3   peter 			diff = secs - n->conn_rate.last;
   2822  1.1.1.3   peter 			if (diff >= n->conn_rate.seconds)
   2823  1.1.1.4  martti 				pstore->conn_rate.count = 0;
   2824  1.1.1.3   peter 			else
   2825  1.1.1.4  martti 				pstore->conn_rate.count -=
   2826  1.1.1.3   peter 				    n->conn_rate.count * diff /
   2827  1.1.1.3   peter 				    n->conn_rate.seconds;
   2828  1.1.1.3   peter 
   2829  1.1.1.4  martti 			error = copyout(pstore, p, sizeof(*p));
   2830  1.1.1.4  martti 			if (error) {
   2831  1.1.1.4  martti 				free(pstore, M_TEMP);
   2832      1.1  itojun 				goto fail;
   2833  1.1.1.4  martti 			}
   2834      1.1  itojun 			p++;
   2835      1.1  itojun 			nr++;
   2836      1.1  itojun 		}
   2837      1.1  itojun 		psn->psn_len = sizeof(struct pf_src_node) * nr;
   2838  1.1.1.4  martti 
   2839  1.1.1.4  martti 		free(pstore, M_TEMP);
   2840      1.1  itojun 		break;
   2841      1.1  itojun 	}
   2842      1.1  itojun 
   2843      1.1  itojun 	case DIOCCLRSRCNODES: {
   2844      1.1  itojun 		struct pf_src_node	*n;
   2845      1.1  itojun 		struct pf_state		*state;
   2846      1.1  itojun 
   2847      1.1  itojun 		RB_FOREACH(state, pf_state_tree_id, &tree_id) {
   2848      1.1  itojun 			state->src_node = NULL;
   2849      1.1  itojun 			state->nat_src_node = NULL;
   2850      1.1  itojun 		}
   2851      1.1  itojun 		RB_FOREACH(n, pf_src_tree, &tree_src_tracking) {
   2852      1.1  itojun 			n->expire = 1;
   2853      1.1  itojun 			n->states = 0;
   2854      1.1  itojun 		}
   2855  1.1.1.4  martti 		pf_purge_expired_src_nodes(1);
   2856      1.1  itojun 		pf_status.src_nodes = 0;
   2857      1.1  itojun 		break;
   2858      1.1  itojun 	}
   2859      1.1  itojun 
   2860  1.1.1.4  martti 	case DIOCKILLSRCNODES: {
   2861  1.1.1.4  martti 		struct pf_src_node	*sn;
   2862  1.1.1.4  martti 		struct pf_state		*s;
   2863  1.1.1.4  martti 		struct pfioc_src_node_kill *psnk = \
   2864  1.1.1.4  martti 			(struct pfioc_src_node_kill *) addr;
   2865  1.1.1.4  martti 		int			killed = 0;
   2866  1.1.1.4  martti 
   2867  1.1.1.4  martti 		RB_FOREACH(sn, pf_src_tree, &tree_src_tracking) {
   2868  1.1.1.4  martti         		if (PF_MATCHA(psnk->psnk_src.neg, \
   2869  1.1.1.4  martti 				      &psnk->psnk_src.addr.v.a.addr, \
   2870  1.1.1.4  martti 				      &psnk->psnk_src.addr.v.a.mask, \
   2871  1.1.1.4  martti 				      &sn->addr, sn->af) &&
   2872  1.1.1.4  martti 			    PF_MATCHA(psnk->psnk_dst.neg, \
   2873  1.1.1.4  martti 				      &psnk->psnk_dst.addr.v.a.addr, \
   2874  1.1.1.4  martti 				      &psnk->psnk_dst.addr.v.a.mask, \
   2875  1.1.1.4  martti 				      &sn->raddr, sn->af)) {
   2876  1.1.1.4  martti 				/* Handle state to src_node linkage */
   2877  1.1.1.4  martti 				if (sn->states != 0) {
   2878  1.1.1.4  martti 					RB_FOREACH(s, pf_state_tree_id,
   2879  1.1.1.4  martti 					    &tree_id) {
   2880  1.1.1.4  martti 						if (s->src_node == sn)
   2881  1.1.1.4  martti 							s->src_node = NULL;
   2882  1.1.1.4  martti 						if (s->nat_src_node == sn)
   2883  1.1.1.4  martti 							s->nat_src_node = NULL;
   2884  1.1.1.4  martti 					}
   2885  1.1.1.4  martti 					sn->states = 0;
   2886  1.1.1.4  martti 				}
   2887  1.1.1.4  martti 				sn->expire = 1;
   2888  1.1.1.4  martti 				killed++;
   2889  1.1.1.4  martti 			}
   2890  1.1.1.4  martti 		}
   2891  1.1.1.4  martti 
   2892  1.1.1.4  martti 		if (killed > 0)
   2893  1.1.1.4  martti 			pf_purge_expired_src_nodes(1);
   2894  1.1.1.4  martti 
   2895  1.1.1.4  martti 		psnk->psnk_af = killed;
   2896  1.1.1.4  martti 		break;
   2897  1.1.1.4  martti 	}
   2898  1.1.1.4  martti 
   2899      1.1  itojun 	case DIOCSETHOSTID: {
   2900      1.1  itojun 		u_int32_t	*hostid = (u_int32_t *)addr;
   2901      1.1  itojun 
   2902  1.1.1.3   peter 		if (*hostid == 0)
   2903  1.1.1.3   peter 			pf_status.hostid = arc4random();
   2904  1.1.1.3   peter 		else
   2905  1.1.1.3   peter 			pf_status.hostid = *hostid;
   2906      1.1  itojun 		break;
   2907      1.1  itojun 	}
   2908      1.1  itojun 
   2909      1.1  itojun 	case DIOCOSFPFLUSH:
   2910      1.1  itojun 		pf_osfp_flush();
   2911      1.1  itojun 		break;
   2912      1.1  itojun 
   2913      1.1  itojun 	case DIOCIGETIFACES: {
   2914      1.1  itojun 		struct pfioc_iface *io = (struct pfioc_iface *)addr;
   2915      1.1  itojun 
   2916  1.1.1.4  martti 		if (io->pfiio_esize != sizeof(struct pfi_kif)) {
   2917      1.1  itojun 			error = ENODEV;
   2918      1.1  itojun 			break;
   2919      1.1  itojun 		}
   2920      1.1  itojun 		error = pfi_get_ifaces(io->pfiio_name, io->pfiio_buffer,
   2921  1.1.1.4  martti 		    &io->pfiio_size);
   2922      1.1  itojun 		break;
   2923      1.1  itojun 	}
   2924      1.1  itojun 
   2925  1.1.1.3   peter 	case DIOCSETIFFLAG: {
   2926  1.1.1.3   peter 		struct pfioc_iface *io = (struct pfioc_iface *)addr;
   2927  1.1.1.3   peter 
   2928  1.1.1.3   peter 		error = pfi_set_flags(io->pfiio_name, io->pfiio_flags);
   2929  1.1.1.3   peter 		break;
   2930  1.1.1.3   peter 	}
   2931  1.1.1.3   peter 
   2932  1.1.1.3   peter 	case DIOCCLRIFFLAG: {
   2933  1.1.1.3   peter 		struct pfioc_iface *io = (struct pfioc_iface *)addr;
   2934  1.1.1.3   peter 
   2935  1.1.1.3   peter 		error = pfi_clear_flags(io->pfiio_name, io->pfiio_flags);
   2936  1.1.1.3   peter 		break;
   2937  1.1.1.3   peter 	}
   2938  1.1.1.3   peter 
   2939      1.1  itojun 	default:
   2940      1.1  itojun 		error = ENODEV;
   2941      1.1  itojun 		break;
   2942      1.1  itojun 	}
   2943      1.1  itojun fail:
   2944  1.1.1.3   peter 	splx(s);
   2945  1.1.1.4  martti 	if (flags & FWRITE)
   2946  1.1.1.4  martti 		rw_exit_write(&pf_consistency_lock);
   2947  1.1.1.4  martti 	else
   2948  1.1.1.4  martti 		rw_exit_read(&pf_consistency_lock);
   2949      1.1  itojun 	return (error);
   2950      1.1  itojun }
   2951