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