npf_conn.h revision 1.16       1   1.1     rmind /*-
      2  1.16     rmind  * Copyright (c) 2009-2019 The NetBSD Foundation, Inc.
      3   1.1     rmind  * All rights reserved.
      4   1.1     rmind  *
      5   1.1     rmind  * This material is based upon work partially supported by The
      6   1.1     rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      7   1.1     rmind  *
      8   1.1     rmind  * Redistribution and use in source and binary forms, with or without
      9   1.1     rmind  * modification, are permitted provided that the following conditions
     10   1.1     rmind  * are met:
     11   1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     12   1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     13   1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     15   1.1     rmind  *    documentation and/or other materials provided with the distribution.
     16   1.1     rmind  *
     17   1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18   1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19   1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20   1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21   1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22   1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23   1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24   1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25   1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26   1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27   1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     28   1.1     rmind  */
     29   1.1     rmind 
     30   1.1     rmind #ifndef _NPF_CONN_H_
     31   1.1     rmind #define _NPF_CONN_H_
     32   1.1     rmind 
     33  1.11  christos #if !defined(_KERNEL) && !defined(_NPF_STANDALONE)
     34   1.1     rmind #error "kernel-level header only"
     35   1.1     rmind #endif
     36   1.1     rmind 
     37   1.1     rmind #include <sys/types.h>
     38   1.1     rmind 
     39   1.1     rmind #include "npf_impl.h"
     40   1.1     rmind 
     41   1.1     rmind typedef struct npf_connkey npf_connkey_t;
     42   1.1     rmind 
     43   1.1     rmind #if defined(__NPF_CONN_PRIVATE)
     44   1.1     rmind 
     45   1.3     rmind /*
     46   1.1     rmind  * The main connection tracking structure.
     47   1.1     rmind  */
     48   1.1     rmind struct npf_conn {
     49   1.1     rmind 	/*
     50  1.16     rmind 	 * Protocol, address length, the interface ID (if zero,
     51  1.16     rmind 	 * then the state is global) and connection flags.
     52   1.1     rmind 	 */
     53  1.16     rmind 	unsigned		c_proto;
     54  1.16     rmind 	unsigned		c_ifid;
     55  1.16     rmind 	unsigned		c_flags;
     56  1.16     rmind 
     57  1.16     rmind 	/* Matching rule flags and ID. */
     58  1.16     rmind 	unsigned		c_retfl;
     59  1.16     rmind 	uint64_t		c_rid;
     60   1.1     rmind 
     61  1.15     rmind 	/*
     62  1.15     rmind 	 * Entry in the connection database/list.  The entry is
     63  1.15     rmind 	 * protected by npf_t::conn_lock.
     64  1.15     rmind 	 */
     65  1.15     rmind 	union {
     66  1.15     rmind 		npf_conn_t *		c_next;
     67  1.15     rmind 		LIST_ENTRY(npf_conn)	c_entry;
     68  1.15     rmind 	};
     69   1.1     rmind 
     70   1.1     rmind 	/* Associated rule procedure or NAT (if any). */
     71   1.1     rmind 	npf_rproc_t *		c_rproc;
     72   1.1     rmind 	npf_nat_t *		c_nat;
     73   1.1     rmind 
     74   1.1     rmind 	/*
     75  1.16     rmind 	 * The Reference count and the last activity time (used to
     76  1.16     rmind 	 * calculate expiration time).  Note: *unsigned* 32-bit integer
     77  1.16     rmind 	 * as a timestamp is sufficient for us.
     78   1.1     rmind 	 */
     79  1.16     rmind 	unsigned		c_refcnt;
     80  1.16     rmind 	uint32_t		c_atime;
     81  1.16     rmind 
     82  1.16     rmind 	/* The protocol state and lock. */
     83   1.1     rmind 	kmutex_t		c_lock;
     84   1.1     rmind 	npf_state_t		c_state;
     85  1.13     rmind 
     86  1.13     rmind 	/*
     87  1.16     rmind 	 * Connection "forwards" and "backwards" keys.  They are accessed
     88  1.16     rmind 	 * as npf_connkey_t, see below and npf_conn_getkey().
     89  1.13     rmind 	 */
     90  1.16     rmind 	uint32_t		c_keys[];
     91  1.16     rmind };
     92  1.16     rmind 
     93  1.16     rmind /*
     94  1.16     rmind  * Connection key interface.
     95  1.16     rmind  *
     96  1.16     rmind  * See the key layout description in the npf_connkey.c source file.
     97  1.16     rmind  */
     98  1.16     rmind 
     99  1.16     rmind #define	NPF_CONNKEY_V4WORDS	(2 + ((sizeof(struct in_addr) * 2) >> 2))
    100  1.16     rmind #define	NPF_CONNKEY_V6WORDS	(2 + ((sizeof(struct in6_addr) * 2) >> 2))
    101  1.16     rmind #define	NPF_CONNKEY_MAXWORDS	(NPF_CONNKEY_V6WORDS)
    102  1.16     rmind 
    103  1.16     rmind #define	NPF_CONNKEY_ALEN(key)	((key)->ck_key[0] & 0xffff)
    104  1.16     rmind #define	NPF_CONNKEY_LEN(key)	(8 + (NPF_CONNKEY_ALEN(key) * 2))
    105  1.16     rmind 
    106  1.16     rmind struct npf_connkey {
    107  1.16     rmind 	/* Warning: ck_key has a variable length -- see above. */
    108  1.16     rmind 	uint32_t		ck_key[NPF_CONNKEY_MAXWORDS];
    109   1.1     rmind };
    110   1.1     rmind 
    111  1.16     rmind unsigned	npf_conn_conkey(const npf_cache_t *, npf_connkey_t *, bool);
    112  1.16     rmind npf_connkey_t *	npf_conn_getforwkey(npf_conn_t *);
    113  1.16     rmind npf_connkey_t *	npf_conn_getbackkey(npf_conn_t *, unsigned);
    114  1.16     rmind void		npf_conn_adjkey(npf_connkey_t *, const npf_addr_t *,
    115  1.16     rmind 		    const uint16_t, const int);
    116  1.16     rmind 
    117  1.16     rmind unsigned	npf_connkey_import(const nvlist_t *, npf_connkey_t *);
    118  1.16     rmind nvlist_t *	npf_connkey_export(const npf_connkey_t *);
    119  1.16     rmind void		npf_connkey_print(const npf_connkey_t *);
    120  1.16     rmind 
    121   1.1     rmind #endif
    122   1.1     rmind 
    123   1.1     rmind /*
    124   1.1     rmind  * Connection tracking interface.
    125   1.1     rmind  */
    126  1.11  christos void		npf_conn_init(npf_t *, int);
    127  1.11  christos void		npf_conn_fini(npf_t *);
    128  1.11  christos void		npf_conn_tracking(npf_t *, bool);
    129  1.11  christos void		npf_conn_load(npf_t *, npf_conndb_t *, bool);
    130   1.1     rmind 
    131   1.2     rmind npf_conn_t *	npf_conn_lookup(const npf_cache_t *, const int, bool *);
    132   1.2     rmind npf_conn_t *	npf_conn_inspect(npf_cache_t *, const int, int *);
    133   1.2     rmind npf_conn_t *	npf_conn_establish(npf_cache_t *, int, bool);
    134   1.1     rmind void		npf_conn_release(npf_conn_t *);
    135  1.15     rmind void		npf_conn_destroy(npf_t *, npf_conn_t *);
    136   1.1     rmind void		npf_conn_expire(npf_conn_t *);
    137  1.12  christos bool		npf_conn_pass(const npf_conn_t *, npf_match_info_t *,
    138  1.12  christos 		    npf_rproc_t **);
    139  1.12  christos void		npf_conn_setpass(npf_conn_t *, const npf_match_info_t *,
    140  1.12  christos 		    npf_rproc_t *);
    141   1.1     rmind int		npf_conn_setnat(const npf_cache_t *, npf_conn_t *,
    142   1.1     rmind 		    npf_nat_t *, u_int);
    143   1.7     rmind npf_nat_t *	npf_conn_getnat(npf_conn_t *, const int, bool *);
    144  1.16     rmind bool		npf_conn_expired(npf_t *, const npf_conn_t *, uint64_t);
    145  1.15     rmind void		npf_conn_remove(npf_conndb_t *, npf_conn_t *);
    146  1.11  christos void		npf_conn_worker(npf_t *);
    147  1.14     rmind int		npf_conn_import(npf_t *, npf_conndb_t *, const nvlist_t *,
    148   1.3     rmind 		    npf_ruleset_t *);
    149  1.14     rmind int		npf_conn_find(npf_t *, const nvlist_t *, nvlist_t **);
    150  1.16     rmind void		npf_conn_print(npf_conn_t *);
    151   1.1     rmind 
    152   1.1     rmind /*
    153   1.1     rmind  * Connection database (aka state table) interface.
    154   1.1     rmind  */
    155  1.16     rmind void		npf_conndb_sysinit(npf_t *);
    156  1.16     rmind void		npf_conndb_sysfini(npf_t *);
    157  1.16     rmind 
    158   1.1     rmind npf_conndb_t *	npf_conndb_create(void);
    159   1.1     rmind void		npf_conndb_destroy(npf_conndb_t *);
    160   1.1     rmind 
    161  1.15     rmind npf_conn_t *	npf_conndb_lookup(npf_conndb_t *, const npf_connkey_t *, bool *);
    162  1.16     rmind bool		npf_conndb_insert(npf_conndb_t *, const npf_connkey_t *,
    163  1.16     rmind 		    npf_conn_t *, bool);
    164  1.11  christos npf_conn_t *	npf_conndb_remove(npf_conndb_t *, npf_connkey_t *);
    165   1.1     rmind 
    166   1.1     rmind void		npf_conndb_enqueue(npf_conndb_t *, npf_conn_t *);
    167   1.1     rmind npf_conn_t *	npf_conndb_getlist(npf_conndb_t *);
    168  1.15     rmind npf_conn_t *	npf_conndb_getnext(npf_conndb_t *, npf_conn_t *);
    169  1.14     rmind int		npf_conndb_export(npf_t *, nvlist_t *);
    170  1.15     rmind void		npf_conndb_gc(npf_t *, npf_conndb_t *, bool, bool);
    171   1.1     rmind 
    172   1.1     rmind #endif	/* _NPF_CONN_H_ */
    173