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