Home | History | Annotate | Line # | Download | only in npf
npf_conn.h revision 1.17
      1 /*-
      2  * Copyright (c) 2009-2019 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 #ifndef _NPF_CONN_H_
     31 #define _NPF_CONN_H_
     32 
     33 #if !defined(_KERNEL) && !defined(_NPF_STANDALONE)
     34 #error "kernel-level header only"
     35 #endif
     36 
     37 #include <sys/types.h>
     38 
     39 #include "npf_impl.h"
     40 
     41 typedef struct npf_connkey npf_connkey_t;
     42 
     43 #if defined(__NPF_CONN_PRIVATE)
     44 
     45 /*
     46  * The main connection tracking structure.
     47  */
     48 struct npf_conn {
     49 	/*
     50 	 * Protocol, address length, the interface ID (if zero,
     51 	 * then the state is global) and connection flags.
     52 	 */
     53 	uint16_t		c_proto;
     54 	uint16_t		c_alen;
     55 	unsigned		c_ifid;
     56 	unsigned		c_flags;
     57 
     58 	/* Matching rule flags and ID. */
     59 	unsigned		c_retfl;
     60 	uint64_t		c_rid;
     61 
     62 	/*
     63 	 * Entry in the connection database/list.  The entry is
     64 	 * protected by npf_t::conn_lock.
     65 	 */
     66 	union {
     67 		npf_conn_t *		c_next;
     68 		LIST_ENTRY(npf_conn)	c_entry;
     69 	};
     70 
     71 	/* Associated rule procedure or NAT (if any). */
     72 	npf_rproc_t *		c_rproc;
     73 	npf_nat_t *		c_nat;
     74 
     75 	/*
     76 	 * The Reference count and the last activity time (used to
     77 	 * calculate expiration time).  Note: *unsigned* 32-bit integer
     78 	 * as a timestamp is sufficient for us.
     79 	 */
     80 	unsigned		c_refcnt;
     81 	uint32_t		c_atime;
     82 
     83 	/* The protocol state and lock. */
     84 	kmutex_t		c_lock;
     85 	npf_state_t		c_state;
     86 
     87 	/*
     88 	 * Connection "forwards" and "backwards" keys.  They are accessed
     89 	 * as npf_connkey_t, see below and npf_conn_getkey().
     90 	 */
     91 	uint32_t		c_keys[];
     92 };
     93 
     94 /*
     95  * Connection key interface.
     96  *
     97  * See the key layout description in the npf_connkey.c source file.
     98  */
     99 
    100 #define	NPF_CONNKEY_V4WORDS	(2 + ((sizeof(struct in_addr) * 2) >> 2))
    101 #define	NPF_CONNKEY_V6WORDS	(2 + ((sizeof(struct in6_addr) * 2) >> 2))
    102 #define	NPF_CONNKEY_MAXWORDS	(NPF_CONNKEY_V6WORDS)
    103 
    104 #define	NPF_CONNKEY_ALEN(key)	((key)->ck_key[0] & 0xffff)
    105 #define	NPF_CONNKEY_LEN(key)	(8 + (NPF_CONNKEY_ALEN(key) * 2))
    106 
    107 struct npf_connkey {
    108 	/* Warning: ck_key has a variable length -- see above. */
    109 	uint32_t		ck_key[NPF_CONNKEY_MAXWORDS];
    110 };
    111 
    112 unsigned	npf_conn_conkey(const npf_cache_t *, npf_connkey_t *, bool);
    113 npf_connkey_t *	npf_conn_getforwkey(npf_conn_t *);
    114 npf_connkey_t *	npf_conn_getbackkey(npf_conn_t *, unsigned);
    115 void		npf_conn_adjkey(npf_connkey_t *, const npf_addr_t *,
    116 		    const uint16_t, const int);
    117 
    118 unsigned	npf_connkey_import(const nvlist_t *, npf_connkey_t *);
    119 nvlist_t *	npf_connkey_export(const npf_connkey_t *);
    120 void		npf_connkey_print(const npf_connkey_t *);
    121 
    122 #endif
    123 
    124 /*
    125  * Connection tracking interface.
    126  */
    127 void		npf_conn_init(npf_t *);
    128 void		npf_conn_fini(npf_t *);
    129 void		npf_conn_tracking(npf_t *, bool);
    130 void		npf_conn_load(npf_t *, npf_conndb_t *, bool);
    131 
    132 npf_conn_t *	npf_conn_lookup(const npf_cache_t *, const int, bool *);
    133 npf_conn_t *	npf_conn_inspect(npf_cache_t *, const int, int *);
    134 npf_conn_t *	npf_conn_establish(npf_cache_t *, int, bool);
    135 void		npf_conn_release(npf_conn_t *);
    136 void		npf_conn_destroy(npf_t *, npf_conn_t *);
    137 void		npf_conn_expire(npf_conn_t *);
    138 bool		npf_conn_pass(const npf_conn_t *, npf_match_info_t *,
    139 		    npf_rproc_t **);
    140 void		npf_conn_setpass(npf_conn_t *, const npf_match_info_t *,
    141 		    npf_rproc_t *);
    142 int		npf_conn_setnat(const npf_cache_t *, npf_conn_t *,
    143 		    npf_nat_t *, u_int);
    144 npf_nat_t *	npf_conn_getnat(npf_conn_t *, const int, bool *);
    145 bool		npf_conn_expired(npf_t *, const npf_conn_t *, uint64_t);
    146 void		npf_conn_remove(npf_conndb_t *, npf_conn_t *);
    147 void		npf_conn_worker(npf_t *);
    148 int		npf_conn_import(npf_t *, npf_conndb_t *, const nvlist_t *,
    149 		    npf_ruleset_t *);
    150 int		npf_conn_find(npf_t *, const nvlist_t *, nvlist_t **);
    151 void		npf_conn_print(npf_conn_t *);
    152 
    153 /*
    154  * Connection database (aka state table) interface.
    155  */
    156 void		npf_conndb_sysinit(npf_t *);
    157 void		npf_conndb_sysfini(npf_t *);
    158 
    159 npf_conndb_t *	npf_conndb_create(void);
    160 void		npf_conndb_destroy(npf_conndb_t *);
    161 
    162 npf_conn_t *	npf_conndb_lookup(npf_conndb_t *, const npf_connkey_t *, bool *);
    163 bool		npf_conndb_insert(npf_conndb_t *, const npf_connkey_t *,
    164 		    npf_conn_t *, bool);
    165 npf_conn_t *	npf_conndb_remove(npf_conndb_t *, npf_connkey_t *);
    166 
    167 void		npf_conndb_enqueue(npf_conndb_t *, npf_conn_t *);
    168 npf_conn_t *	npf_conndb_getlist(npf_conndb_t *);
    169 npf_conn_t *	npf_conndb_getnext(npf_conndb_t *, npf_conn_t *);
    170 int		npf_conndb_export(npf_t *, nvlist_t *);
    171 void		npf_conndb_gc(npf_t *, npf_conndb_t *, bool, bool);
    172 
    173 #endif	/* _NPF_CONN_H_ */
    174