Home | History | Annotate | Line # | Download | only in npf
npf_impl.h revision 1.74
      1 /*-
      2  * Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This material is based upon work partially supported by The
      6  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Private NPF structures and interfaces.
     32  * For internal use within NPF core only.
     33  */
     34 
     35 #ifndef _NPF_IMPL_H_
     36 #define _NPF_IMPL_H_
     37 
     38 #if !defined(_KERNEL) && !defined(_NPF_STANDALONE)
     39 #error "Kernel-level header only"
     40 #endif
     41 
     42 #ifdef _KERNEL_OPT
     43 /* For INET/INET6 definitions. */
     44 #include "opt_inet.h"
     45 #include "opt_inet6.h"
     46 #endif
     47 
     48 #ifdef _KERNEL
     49 #include <sys/types.h>
     50 #include <sys/queue.h>
     51 
     52 #include <net/bpf.h>
     53 #include <net/bpfjit.h>
     54 #include <net/if.h>
     55 #endif
     56 #include <dnv.h>
     57 #include <nv.h>
     58 
     59 #include "npf.h"
     60 #include "npfkern.h"
     61 
     62 #ifdef _NPF_DEBUG
     63 #define	NPF_PRINTF(x)	printf x
     64 #else
     65 #define	NPF_PRINTF(x)
     66 #endif
     67 
     68 /*
     69  * STRUCTURE DECLARATIONS.
     70  */
     71 
     72 struct npf_ruleset;
     73 struct npf_rule;
     74 struct npf_rprocset;
     75 struct npf_nat;
     76 struct npf_conn;
     77 struct npf_config;
     78 
     79 typedef struct npf_ruleset	npf_ruleset_t;
     80 typedef struct npf_rule		npf_rule_t;
     81 typedef struct npf_nat		npf_nat_t;
     82 typedef struct npf_rprocset	npf_rprocset_t;
     83 typedef struct npf_alg		npf_alg_t;
     84 typedef struct npf_natpolicy	npf_natpolicy_t;
     85 typedef struct npf_conn		npf_conn_t;
     86 typedef struct npf_config	npf_config_t;
     87 
     88 struct npf_conndb;
     89 struct npf_table;
     90 struct npf_tableset;
     91 struct npf_algset;
     92 struct npf_ifmap;
     93 
     94 typedef struct npf_conndb	npf_conndb_t;
     95 typedef struct npf_table	npf_table_t;
     96 typedef struct npf_tableset	npf_tableset_t;
     97 typedef struct npf_algset	npf_algset_t;
     98 
     99 /*
    100  * DEFINITIONS.
    101  */
    102 
    103 typedef void (*npf_workfunc_t)(npf_t *);
    104 
    105 typedef struct {
    106 	uint64_t	mi_rid;
    107 	unsigned	mi_retfl;
    108 	unsigned	mi_di;
    109 } npf_match_info_t;
    110 
    111 /*
    112  * Some artificial limits.
    113  * Note: very unlikely to have many ALGs.
    114  */
    115 #define	NPF_MAX_RULES		(1024 * 1024)
    116 #define	NPF_MAX_TABLES		128
    117 #define	NPF_MAX_RPROCS		128
    118 #define	NPF_MAX_IFMAP		64
    119 #define	NPF_MAX_ALGS		4
    120 #define	NPF_MAX_WORKS		4
    121 
    122 /*
    123  * CONNECTION STATE STRUCTURES
    124  */
    125 
    126 #define	NPF_FLOW_FORW		0
    127 #define	NPF_FLOW_BACK		1
    128 
    129 typedef struct {
    130 	uint32_t	nst_end;
    131 	uint32_t	nst_maxend;
    132 	uint32_t	nst_maxwin;
    133 	int		nst_wscale;
    134 } npf_tcpstate_t;
    135 
    136 typedef struct {
    137 	u_int		nst_state;
    138 	npf_tcpstate_t	nst_tcpst[2];
    139 } npf_state_t;
    140 
    141 /*
    142  * ALG FUNCTIONS.
    143  */
    144 
    145 typedef struct {
    146 	bool		(*match)(npf_cache_t *, npf_nat_t *, int);
    147 	bool		(*translate)(npf_cache_t *, npf_nat_t *, bool);
    148 	npf_conn_t *	(*inspect)(npf_cache_t *, int);
    149 } npfa_funcs_t;
    150 
    151 /*
    152  * NBUF STRUCTURE.
    153  */
    154 
    155 struct nbuf {
    156 	struct mbuf *	nb_mbuf0;
    157 	struct mbuf *	nb_mbuf;
    158 	void *		nb_nptr;
    159 	const ifnet_t *	nb_ifp;
    160 	unsigned	nb_ifid;
    161 	int		nb_flags;
    162 	const npf_mbufops_t *nb_mops;
    163 };
    164 
    165 /*
    166  * NPF INSTANCE (CONTEXT) STRUCTURE AND AUXILIARY OPERATIONS.
    167  */
    168 
    169 struct npf {
    170 	/* Active NPF configuration. */
    171 	kmutex_t		config_lock;
    172 	pserialize_t		qsbr;
    173 	npf_config_t *		config;
    174 
    175 	/* BPF byte-code context. */
    176 	bpf_ctx_t *		bpfctx;
    177 	const npf_mbufops_t *	mbufops;
    178 
    179 	/*
    180 	 * Connection tracking state: disabled (off) or enabled (on).
    181 	 * Connection tracking database, connection cache and the lock.
    182 	 */
    183 	volatile int		conn_tracking;
    184 	kmutex_t		conn_lock;
    185 	npf_conndb_t *		conn_db;
    186 	pool_cache_t		conn_cache;
    187 
    188 	/* ALGs. */
    189 	npf_algset_t *		algset;
    190 
    191 	/* Interface mapping. */
    192 	const npf_ifops_t *	ifops;
    193 	struct npf_ifmap *	ifmap;
    194 	unsigned		ifmap_cnt;
    195 
    196 	/* Associated worker thread. */
    197 	unsigned		worker_id;
    198 	void *			worker_entry;
    199 	bool			sync_registered;
    200 
    201 	/* List of extensions and its lock. */
    202 	LIST_HEAD(, npf_ext)	ext_list;
    203 	kmutex_t		ext_lock;
    204 
    205 	/* Statistics. */
    206 	percpu_t *		stats_percpu;
    207 };
    208 
    209 
    210 /*
    211  * NPF extensions and rule procedure interface.
    212  */
    213 
    214 struct npf_rproc;
    215 typedef struct npf_rproc npf_rproc_t;
    216 
    217 typedef struct {
    218 	u_int	version;
    219 	void *	ctx;
    220 	int	(*ctor)(npf_rproc_t *, const nvlist_t *);
    221 	void	(*dtor)(npf_rproc_t *, void *);
    222 	bool	(*proc)(npf_cache_t *, void *, const npf_match_info_t *, int *);
    223 } npf_ext_ops_t;
    224 
    225 void *		npf_ext_register(npf_t *, const char *, const npf_ext_ops_t *);
    226 int		npf_ext_unregister(npf_t *, void *);
    227 void		npf_rproc_assign(npf_rproc_t *, void *);
    228 
    229 /*
    230  * INTERFACES.
    231  */
    232 
    233 /* NPF config, statistics, etc. */
    234 void		npf_config_init(npf_t *);
    235 void		npf_config_fini(npf_t *);
    236 
    237 void		npf_config_enter(npf_t *);
    238 void		npf_config_exit(npf_t *);
    239 void		npf_config_sync(npf_t *);
    240 bool		npf_config_locked_p(npf_t *);
    241 int		npf_config_read_enter(void);
    242 void		npf_config_read_exit(int s);
    243 
    244 void		npf_config_load(npf_t *, npf_ruleset_t *, npf_tableset_t *,
    245 		    npf_ruleset_t *, npf_rprocset_t *, npf_conndb_t *, bool);
    246 npf_ruleset_t *	npf_config_ruleset(npf_t *npf);
    247 npf_ruleset_t *	npf_config_natset(npf_t *npf);
    248 npf_tableset_t *npf_config_tableset(npf_t *npf);
    249 npf_rprocset_t *npf_config_rprocs(npf_t *);
    250 bool		npf_default_pass(npf_t *);
    251 
    252 int		npf_worker_sysinit(unsigned);
    253 void		npf_worker_sysfini(void);
    254 void		npf_worker_signal(npf_t *);
    255 void		npf_worker_register(npf_t *, npf_workfunc_t);
    256 void		npf_worker_unregister(npf_t *, npf_workfunc_t);
    257 
    258 int		npfctl_switch(void *);
    259 int		npfctl_reload(u_long, void *);
    260 int		npfctl_save(npf_t *, u_long, void *);
    261 int		npfctl_load(npf_t *, u_long, void *);
    262 int		npfctl_rule(npf_t *, u_long, void *);
    263 int		npfctl_conn_lookup(npf_t *, u_long, void *);
    264 int		npfctl_table(npf_t *, void *);
    265 
    266 void		npf_stats_inc(npf_t *, npf_stats_t);
    267 void		npf_stats_dec(npf_t *, npf_stats_t);
    268 
    269 void		npf_ifmap_init(npf_t *, const npf_ifops_t *);
    270 void		npf_ifmap_fini(npf_t *);
    271 u_int		npf_ifmap_register(npf_t *, const char *);
    272 void		npf_ifmap_flush(npf_t *);
    273 u_int		npf_ifmap_getid(npf_t *, const ifnet_t *);
    274 const char *	npf_ifmap_getname(npf_t *, const u_int);
    275 void		npf_ifmap_copyname(npf_t *, u_int, char *, size_t);
    276 
    277 void		npf_ifaddr_sync(npf_t *, ifnet_t *);
    278 void		npf_ifaddr_flush(npf_t *, ifnet_t *);
    279 void		npf_ifaddr_syncall(npf_t *);
    280 
    281 /* Packet filter hooks. */
    282 int		npf_pfil_register(bool);
    283 void		npf_pfil_unregister(bool);
    284 bool		npf_pfil_registered_p(void);
    285 
    286 /* Protocol helpers. */
    287 int		npf_cache_all(npf_cache_t *);
    288 void		npf_recache(npf_cache_t *);
    289 
    290 bool		npf_rwrip(const npf_cache_t *, u_int, const npf_addr_t *);
    291 bool		npf_rwrport(const npf_cache_t *, u_int, const in_port_t);
    292 bool		npf_rwrcksum(const npf_cache_t *, u_int,
    293 		    const npf_addr_t *, const in_port_t);
    294 int		npf_napt_rwr(const npf_cache_t *, u_int, const npf_addr_t *,
    295 		    const in_addr_t);
    296 int		npf_npt66_rwr(const npf_cache_t *, u_int, const npf_addr_t *,
    297 		    npf_netmask_t, uint16_t);
    298 
    299 uint16_t	npf_fixup16_cksum(uint16_t, uint16_t, uint16_t);
    300 uint16_t	npf_fixup32_cksum(uint16_t, uint32_t, uint32_t);
    301 uint16_t	npf_addr_cksum(uint16_t, int, const npf_addr_t *,
    302 		    const npf_addr_t *);
    303 uint32_t	npf_addr_mix(const int, const npf_addr_t *, const npf_addr_t *);
    304 int		npf_addr_cmp(const npf_addr_t *, const npf_netmask_t,
    305 		    const npf_addr_t *, const npf_netmask_t, const int);
    306 void		npf_addr_mask(const npf_addr_t *, const npf_netmask_t,
    307 		    const int, npf_addr_t *);
    308 void		npf_addr_bitor(const npf_addr_t *, const npf_netmask_t,
    309 		    const int, npf_addr_t *);
    310 
    311 int		npf_tcpsaw(const npf_cache_t *, tcp_seq *, tcp_seq *,
    312 		    uint32_t *);
    313 bool		npf_fetch_tcpopts(npf_cache_t *, uint16_t *, int *);
    314 bool		npf_set_mss(npf_cache_t *, uint16_t, uint16_t *, uint16_t *,
    315 		    bool *);
    316 bool		npf_return_block(npf_cache_t *, const int);
    317 
    318 /* BPF interface. */
    319 void		npf_bpf_sysinit(void);
    320 void		npf_bpf_sysfini(void);
    321 void		npf_bpf_prepare(npf_cache_t *, bpf_args_t *, uint32_t *);
    322 int		npf_bpf_filter(bpf_args_t *, const void *, bpfjit_func_t);
    323 void *		npf_bpf_compile(void *, size_t);
    324 bool		npf_bpf_validate(const void *, size_t);
    325 
    326 /* Tableset interface. */
    327 void		npf_tableset_sysinit(void);
    328 void		npf_tableset_sysfini(void);
    329 
    330 npf_tableset_t *npf_tableset_create(u_int);
    331 void		npf_tableset_destroy(npf_tableset_t *);
    332 int		npf_tableset_insert(npf_tableset_t *, npf_table_t *);
    333 npf_table_t *	npf_tableset_getbyname(npf_tableset_t *, const char *);
    334 npf_table_t *	npf_tableset_getbyid(npf_tableset_t *, u_int);
    335 npf_table_t *	npf_tableset_swap(npf_tableset_t *, npf_table_t *);
    336 void		npf_tableset_reload(npf_t *, npf_tableset_t *, npf_tableset_t *);
    337 int		npf_tableset_export(npf_t *, const npf_tableset_t *, nvlist_t *);
    338 
    339 npf_table_t *	npf_table_create(const char *, u_int, int, const void *, size_t);
    340 void		npf_table_destroy(npf_table_t *);
    341 
    342 u_int		npf_table_getid(npf_table_t *);
    343 int		npf_table_check(npf_tableset_t *, const char *, uint64_t, uint64_t);
    344 int		npf_table_insert(npf_table_t *, const int,
    345 		    const npf_addr_t *, const npf_netmask_t);
    346 int		npf_table_remove(npf_table_t *, const int,
    347 		    const npf_addr_t *, const npf_netmask_t);
    348 int		npf_table_lookup(npf_table_t *, const int, const npf_addr_t *);
    349 npf_addr_t *	npf_table_getsome(npf_table_t *, const int, unsigned);
    350 int		npf_table_list(npf_table_t *, void *, size_t);
    351 int		npf_table_flush(npf_table_t *);
    352 void		npf_table_gc(npf_t *, npf_table_t *);
    353 
    354 /* Ruleset interface. */
    355 npf_ruleset_t *	npf_ruleset_create(size_t);
    356 void		npf_ruleset_destroy(npf_ruleset_t *);
    357 void		npf_ruleset_insert(npf_ruleset_t *, npf_rule_t *);
    358 void		npf_ruleset_reload(npf_t *, npf_ruleset_t *,
    359 		    npf_ruleset_t *, bool);
    360 npf_rule_t *	npf_ruleset_sharepm(npf_ruleset_t *, npf_natpolicy_t *);
    361 npf_natpolicy_t *npf_ruleset_findnat(npf_ruleset_t *, uint64_t);
    362 void		npf_ruleset_freealg(npf_ruleset_t *, npf_alg_t *);
    363 int		npf_ruleset_export(npf_t *, const npf_ruleset_t *,
    364 		    const char *, nvlist_t *);
    365 
    366 npf_rule_t *	npf_ruleset_lookup(npf_ruleset_t *, const char *);
    367 int		npf_ruleset_add(npf_ruleset_t *, const char *, npf_rule_t *);
    368 int		npf_ruleset_remove(npf_ruleset_t *, const char *, uint64_t);
    369 int		npf_ruleset_remkey(npf_ruleset_t *, const char *,
    370 		    const void *, size_t);
    371 nvlist_t *	npf_ruleset_list(npf_t *, npf_ruleset_t *, const char *);
    372 int		npf_ruleset_flush(npf_ruleset_t *, const char *);
    373 void		npf_ruleset_gc(npf_ruleset_t *);
    374 
    375 npf_rule_t *	npf_ruleset_inspect(npf_cache_t *, const npf_ruleset_t *,
    376 		    const int, const int);
    377 int		npf_rule_conclude(const npf_rule_t *, npf_match_info_t *);
    378 
    379 /* Rule interface. */
    380 npf_rule_t *	npf_rule_alloc(npf_t *, const nvlist_t *);
    381 void		npf_rule_setcode(npf_rule_t *, int, void *, size_t);
    382 void		npf_rule_setrproc(npf_rule_t *, npf_rproc_t *);
    383 void		npf_rule_free(npf_rule_t *);
    384 uint64_t	npf_rule_getid(const npf_rule_t *);
    385 npf_natpolicy_t *npf_rule_getnat(const npf_rule_t *);
    386 void		npf_rule_setnat(npf_rule_t *, npf_natpolicy_t *);
    387 npf_rproc_t *	npf_rule_getrproc(const npf_rule_t *);
    388 
    389 void		npf_ext_init(npf_t *);
    390 void		npf_ext_fini(npf_t *);
    391 int		npf_ext_construct(npf_t *, const char *,
    392 		    npf_rproc_t *, const nvlist_t *);
    393 
    394 npf_rprocset_t *npf_rprocset_create(void);
    395 void		npf_rprocset_destroy(npf_rprocset_t *);
    396 npf_rproc_t *	npf_rprocset_lookup(npf_rprocset_t *, const char *);
    397 void		npf_rprocset_insert(npf_rprocset_t *, npf_rproc_t *);
    398 int		npf_rprocset_export(const npf_rprocset_t *, nvlist_t *);
    399 
    400 npf_rproc_t *	npf_rproc_create(const nvlist_t *);
    401 void		npf_rproc_acquire(npf_rproc_t *);
    402 void		npf_rproc_release(npf_rproc_t *);
    403 const char *	npf_rproc_getname(const npf_rproc_t *);
    404 bool		npf_rproc_run(npf_cache_t *, npf_rproc_t *,
    405 		    const npf_match_info_t *, int *);
    406 
    407 /* State handling. */
    408 bool		npf_state_init(npf_cache_t *, npf_state_t *);
    409 bool		npf_state_inspect(npf_cache_t *, npf_state_t *, const bool);
    410 int		npf_state_etime(const npf_state_t *, const int);
    411 void		npf_state_destroy(npf_state_t *);
    412 
    413 bool		npf_state_tcp(npf_cache_t *, npf_state_t *, int);
    414 int		npf_state_tcp_timeout(const npf_state_t *);
    415 
    416 /* NAT. */
    417 void		npf_nat_sysinit(void);
    418 void		npf_nat_sysfini(void);
    419 npf_natpolicy_t *npf_nat_newpolicy(npf_t *, const nvlist_t *, npf_ruleset_t *);
    420 int		npf_nat_policyexport(const npf_natpolicy_t *, nvlist_t *);
    421 void		npf_nat_freepolicy(npf_natpolicy_t *);
    422 bool		npf_nat_cmppolicy(npf_natpolicy_t *, npf_natpolicy_t *);
    423 bool		npf_nat_sharepm(npf_natpolicy_t *, npf_natpolicy_t *);
    424 void		npf_nat_setid(npf_natpolicy_t *, uint64_t);
    425 uint64_t	npf_nat_getid(const npf_natpolicy_t *);
    426 void		npf_nat_freealg(npf_natpolicy_t *, npf_alg_t *);
    427 
    428 int		npf_do_nat(npf_cache_t *, npf_conn_t *, const int);
    429 void		npf_nat_destroy(npf_nat_t *);
    430 void		npf_nat_getorig(npf_nat_t *, npf_addr_t **, in_port_t *);
    431 void		npf_nat_gettrans(npf_nat_t *, npf_addr_t **, in_port_t *);
    432 void		npf_nat_setalg(npf_nat_t *, npf_alg_t *, uintptr_t);
    433 
    434 void		npf_nat_export(nvlist_t *, npf_nat_t *);
    435 npf_nat_t *	npf_nat_import(npf_t *, const nvlist_t *, npf_ruleset_t *,
    436 		    npf_conn_t *);
    437 
    438 /* ALG interface. */
    439 void		npf_alg_sysinit(void);
    440 void		npf_alg_sysfini(void);
    441 void		npf_alg_init(npf_t *);
    442 void		npf_alg_fini(npf_t *);
    443 npf_alg_t *	npf_alg_register(npf_t *, const char *, const npfa_funcs_t *);
    444 int		npf_alg_unregister(npf_t *, npf_alg_t *);
    445 npf_alg_t *	npf_alg_construct(npf_t *, const char *);
    446 bool		npf_alg_match(npf_cache_t *, npf_nat_t *, int);
    447 void		npf_alg_exec(npf_cache_t *, npf_nat_t *, bool);
    448 npf_conn_t *	npf_alg_conn(npf_cache_t *, int);
    449 int		npf_alg_export(npf_t *, nvlist_t *);
    450 
    451 /* Debugging routines. */
    452 const char *	npf_addr_dump(const npf_addr_t *, int);
    453 void		npf_state_dump(const npf_state_t *);
    454 void		npf_nat_dump(const npf_nat_t *);
    455 void		npf_ruleset_dump(npf_t *, const char *);
    456 void		npf_state_setsampler(void (*)(npf_state_t *, bool));
    457 
    458 /* In-kernel routines. */
    459 void		npf_setkernctx(npf_t *);
    460 npf_t *		npf_getkernctx(void);
    461 
    462 #ifdef __NetBSD__
    463 #define	pserialize_register(x)
    464 #define	pserialize_checkpoint(x)
    465 #define	pserialize_unregister(x)
    466 #endif
    467 
    468 #endif	/* _NPF_IMPL_H_ */
    469