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