1 /* $NetBSD: npf_impl.h,v 1.1 2010/08/22 18:56:22 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 2009-2010 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This material is based upon work partially supported by The 8 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Private NPF structures and interfaces. 34 * For internal use within NPF core only. 35 */ 36 37 #ifndef _NPF_IMPL_H_ 38 #define _NPF_IMPL_H_ 39 40 #include <sys/rb.h> 41 #include <sys/hash.h> 42 #include <sys/queue.h> 43 #include <sys/types.h> 44 #include <sys/rwlock.h> 45 46 #include "npf.h" 47 #include "npf_ncode.h" 48 49 #ifdef _NPF_TESTING 50 #include "testing.h" 51 #endif 52 53 /* 54 * STRUCTURE DECLARATIONS. 55 * 56 * Note: ruleset interface declarations are public. 57 */ 58 59 struct npf_nat; 60 struct npf_session; 61 62 typedef struct npf_nat npf_nat_t; 63 typedef struct npf_alg npf_alg_t; 64 typedef struct npf_natpolicy npf_natpolicy_t; 65 typedef struct npf_session npf_session_t; 66 67 struct npf_tblent; 68 struct npf_table; 69 70 typedef struct npf_tblent npf_tblent_t; 71 typedef struct npf_table npf_table_t; 72 73 typedef npf_table_t * npf_tableset_t; 74 75 /* 76 * DEFINITIONS. 77 */ 78 79 typedef bool (*npf_algfunc_t)(npf_cache_t *, void *, void *); 80 81 #define NPF_NCODE_LIMIT 1024 82 #define NPF_TABLE_SLOTS 32 83 84 /* 85 * INTERFACES. 86 */ 87 88 /* NPF control. */ 89 int npfctl_switch(void *); 90 int npfctl_reload(u_long, void *); 91 int npfctl_table(void *); 92 93 /* Packet filter hooks. */ 94 int npf_register_pfil(void); 95 void npf_unregister_pfil(void); 96 97 /* Protocol helpers. */ 98 bool npf_ip4_proto(npf_cache_t *, nbuf_t *, void *); 99 bool npf_fetch_ip4addrs(npf_cache_t *, nbuf_t *, void *); 100 bool npf_fetch_ports(npf_cache_t *, nbuf_t *, void *, const int); 101 bool npf_fetch_icmp(npf_cache_t *, nbuf_t *, void *); 102 bool npf_cache_all_ip4(npf_cache_t *, nbuf_t *, const int); 103 104 bool npf_rwrport(npf_cache_t *, nbuf_t *, void *, const int, 105 in_port_t, in_addr_t); 106 bool npf_rwrip(npf_cache_t *, nbuf_t *, void *, const int, in_addr_t); 107 108 uint16_t npf_fixup16_cksum(uint16_t, uint16_t, uint16_t); 109 uint16_t npf_fixup32_cksum(uint16_t, uint32_t, uint32_t); 110 111 /* Complex instructions. */ 112 int npf_match_ether(nbuf_t *, int, int, uint16_t, uint32_t *); 113 int npf_match_ip4table(npf_cache_t *, nbuf_t *, void *, 114 const int, const u_int); 115 int npf_match_ip4mask(npf_cache_t *, nbuf_t *, void *, 116 const int, in_addr_t, in_addr_t); 117 int npf_match_tcp_ports(npf_cache_t *, nbuf_t *, void *, 118 const int, const uint32_t); 119 int npf_match_udp_ports(npf_cache_t *, nbuf_t *, void *, 120 const int, const uint32_t); 121 int npf_match_icmp4(npf_cache_t *, nbuf_t *, void *, 122 const int, const int); 123 124 /* Tableset interface. */ 125 int npf_tableset_sysinit(void); 126 void npf_tableset_sysfini(void); 127 128 npf_tableset_t *npf_tableset_create(void); 129 void npf_tableset_destroy(npf_tableset_t *); 130 int npf_tableset_insert(npf_tableset_t *, npf_table_t *); 131 npf_tableset_t *npf_tableset_reload(npf_tableset_t *); 132 133 npf_table_t * npf_table_create(u_int, int, size_t); 134 void npf_table_destroy(npf_table_t *); 135 void npf_table_ref(npf_table_t *); 136 void npf_table_unref(npf_table_t *); 137 138 npf_table_t * npf_table_get(npf_tableset_t *, u_int); 139 void npf_table_put(npf_table_t *); 140 int npf_table_check(npf_tableset_t *, u_int, int); 141 int npf_table_add_v4cidr(npf_tableset_t *, u_int, 142 in_addr_t, in_addr_t); 143 int npf_table_rem_v4cidr(npf_tableset_t *, u_int, 144 in_addr_t, in_addr_t); 145 int npf_table_match_v4addr(u_int, in_addr_t); 146 147 /* Ruleset interface. */ 148 int npf_ruleset_sysinit(void); 149 void npf_ruleset_sysfini(void); 150 151 npf_ruleset_t * npf_ruleset_create(void); 152 void npf_ruleset_destroy(npf_ruleset_t *); 153 void npf_ruleset_insert(npf_ruleset_t *, npf_rule_t *); 154 void npf_ruleset_reload(npf_ruleset_t *, npf_tableset_t *); 155 156 npf_rule_t * npf_ruleset_match(npf_ruleset_t *, npf_cache_t *, nbuf_t *, 157 struct ifnet *, const int, const int); 158 npf_rule_t * npf_ruleset_inspect(npf_cache_t *, nbuf_t *, 159 struct ifnet *, const int, const int); 160 int npf_rule_apply(const npf_cache_t *, npf_rule_t *, bool *); 161 npf_ruleset_t * npf_rule_subset(npf_rule_t *); 162 163 npf_natpolicy_t *npf_rule_getnat(const npf_rule_t *); 164 void npf_rule_setnat(npf_rule_t *, npf_natpolicy_t *); 165 166 /* State handling interface. */ 167 int npf_session_sysinit(void); 168 void npf_session_sysfini(void); 169 int npf_session_tracking(bool); 170 171 npf_session_t * npf_session_inspect(npf_cache_t *, nbuf_t *, 172 struct ifnet *, const int, const int); 173 npf_session_t * npf_session_establish(const npf_cache_t *, 174 npf_nat_t *, const int); 175 void npf_session_release(npf_session_t *); 176 bool npf_session_pass(const npf_session_t *); 177 178 npf_nat_t * npf_session_retnat(const npf_session_t *); 179 180 void npf_session_link(npf_session_t *, npf_session_t *); 181 npf_nat_t * npf_session_retlinknat(const npf_session_t *); 182 183 /* NAT. */ 184 void npf_nat_sysinit(void); 185 void npf_nat_sysfini(void); 186 npf_natpolicy_t *npf_nat_newpolicy(in_addr_t); 187 void npf_nat_freepolicy(npf_natpolicy_t *); 188 void npf_nat_flush(void); 189 void npf_nat_reload(npf_ruleset_t *); 190 191 int npf_natout(npf_cache_t *, npf_session_t *, nbuf_t *, 192 struct ifnet *, const int); 193 int npf_natin(npf_cache_t *, npf_session_t *, nbuf_t *, const int); 194 195 void npf_nat_expire(npf_nat_t *); 196 void npf_nat_getlocal(npf_nat_t *, in_addr_t *, in_port_t *); 197 void npf_nat_setalg(npf_nat_t *, npf_alg_t *, uintptr_t); 198 199 /* ALG interface. */ 200 void npf_alg_sysinit(void); 201 void npf_alg_sysfini(void); 202 npf_alg_t * npf_alg_register(npf_algfunc_t, npf_algfunc_t, 203 npf_algfunc_t, npf_algfunc_t); 204 int npf_alg_unregister(npf_alg_t *); 205 void npf_alg_match(npf_cache_t *, nbuf_t *, npf_nat_t *); 206 void npf_alg_exec(npf_cache_t *, nbuf_t *, npf_nat_t *, const int ); 207 bool npf_alg_sessionid(npf_cache_t *, nbuf_t *, npf_cache_t *); 208 209 /* Debugging routines. */ 210 void npf_rulenc_dump(npf_rule_t *); 211 void npf_sessions_dump(void); 212 void npf_nat_dump(npf_nat_t *); 213 214 #endif 215