1 /*- 2 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 #ifndef _IPFW2_PRIVATE_H 29 #define _IPFW2_PRIVATE_H 30 31 /* 32 * Internal constants and data structures used by ipfw components 33 * and not meant to be exported outside the kernel. 34 */ 35 36 #ifdef _KERNEL 37 38 /* 39 * For platforms that do not have SYSCTL support, we wrap the 40 * SYSCTL_* into a function (one per file) to collect the values 41 * into an array at module initialization. The wrapping macros, 42 * SYSBEGIN() and SYSEND, are empty in the default case. 43 */ 44 #ifndef SYSBEGIN 45 #define SYSBEGIN(x) 46 #endif 47 #ifndef SYSEND 48 #define SYSEND 49 #endif 50 51 /* Return values from ipfw_chk() */ 52 enum { 53 IP_FW_PASS = 0, 54 IP_FW_DENY, 55 IP_FW_DIVERT, 56 IP_FW_TEE, 57 IP_FW_DUMMYNET, 58 IP_FW_NETGRAPH, 59 IP_FW_NGTEE, 60 IP_FW_NAT, 61 IP_FW_REASS, 62 }; 63 64 /* 65 * Structure for collecting parameters to dummynet for ip6_output forwarding 66 */ 67 struct _ip6dn_args { 68 struct ip6_pktopts *opt_or; 69 struct route_in6 ro_or; 70 int flags_or; 71 struct ip6_moptions *im6o_or; 72 struct ifnet *origifp_or; 73 struct ifnet *ifp_or; 74 struct sockaddr_in6 dst_or; 75 u_long mtu_or; 76 struct route_in6 ro_pmtu_or; 77 }; 78 79 80 /* 81 * Arguments for calling ipfw_chk() and dummynet_io(). We put them 82 * all into a structure because this way it is easier and more 83 * efficient to pass variables around and extend the interface. 84 */ 85 struct ip_fw_args { 86 struct mbuf *m; /* the mbuf chain */ 87 struct ifnet *oif; /* output interface */ 88 struct sockaddr_in *next_hop; /* forward address */ 89 struct sockaddr_in6 *next_hop6; /* ipv6 forward address */ 90 91 /* 92 * On return, it points to the matching rule. 93 * On entry, rule.slot > 0 means the info is valid and 94 * contains the starting rule for an ipfw search. 95 * If chain_id == chain->id && slot >0 then jump to that slot. 96 * Otherwise, we locate the first rule >= rulenum:rule_id 97 */ 98 struct ipfw_rule_ref rule; /* match/restart info */ 99 100 struct ether_header *eh; /* for bridged packets */ 101 102 struct ipfw_flow_id f_id; /* grabbed from IP header */ 103 //uint32_t cookie; /* a cookie depending on rule action */ 104 struct inpcb *inp; 105 106 struct _ip6dn_args dummypar; /* dummynet->ip6_output */ 107 struct sockaddr_in hopstore; /* store here if cannot use a pointer */ 108 }; 109 110 MALLOC_DECLARE(M_IPFW); 111 112 /* 113 * Hooks sometime need to know the direction of the packet 114 * (divert, dummynet, netgraph, ...) 115 * We use a generic definition here, with bit0-1 indicating the 116 * direction, bit 2 indicating layer2 or 3, bit 3-4 indicating the 117 * specific protocol 118 * indicating the protocol (if necessary) 119 */ 120 enum { 121 DIR_MASK = 0x3, 122 DIR_OUT = 0, 123 DIR_IN = 1, 124 DIR_FWD = 2, 125 DIR_DROP = 3, 126 PROTO_LAYER2 = 0x4, /* set for layer 2 */ 127 /* PROTO_DEFAULT = 0, */ 128 PROTO_IPV4 = 0x08, 129 PROTO_IPV6 = 0x10, 130 PROTO_IFB = 0x0c, /* layer2 + ifbridge */ 131 /* PROTO_OLDBDG = 0x14, unused, old bridge */ 132 }; 133 134 /* wrapper for freeing a packet, in case we need to do more work */ 135 #ifndef FREE_PKT 136 #if defined(__linux__) || defined(_WIN32) 137 #define FREE_PKT(m) netisr_dispatch(-1, m) 138 #else 139 #define FREE_PKT(m) m_freem(m) 140 #endif 141 #endif /* !FREE_PKT */ 142 143 /* 144 * Function definitions. 145 */ 146 147 /* attach (arg = 1) or detach (arg = 0) hooks */ 148 int ipfw_attach_hooks(int); 149 #ifdef NOTYET 150 void ipfw_nat_destroy(void); 151 #endif 152 153 /* In ip_fw_log.c */ 154 struct ip; 155 void ipfw_log_bpf(int); 156 void ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args, 157 struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg, 158 struct ip *ip); 159 VNET_DECLARE(u_int64_t, norule_counter); 160 #define V_norule_counter VNET(norule_counter) 161 VNET_DECLARE(int, verbose_limit); 162 #define V_verbose_limit VNET(verbose_limit) 163 164 /* In ip_fw_dynamic.c */ 165 166 enum { /* result for matching dynamic rules */ 167 MATCH_REVERSE = 0, 168 MATCH_FORWARD, 169 MATCH_NONE, 170 MATCH_UNKNOWN, 171 }; 172 173 /* 174 * The lock for dynamic rules is only used once outside the file, 175 * and only to release the result of lookup_dyn_rule(). 176 * Eventually we may implement it with a callback on the function. 177 */ 178 struct ip_fw_chain; 179 void ipfw_expire_dyn_rules(struct ip_fw_chain *, struct ip_fw *, int); 180 void ipfw_dyn_unlock(ipfw_dyn_rule *q); 181 182 struct tcphdr; 183 struct mbuf *ipfw_send_pkt(struct mbuf *, struct ipfw_flow_id *, 184 u_int32_t, u_int32_t, int); 185 int ipfw_install_state(struct ip_fw *rule, ipfw_insn_limit *cmd, 186 struct ip_fw_args *args, uint32_t tablearg); 187 ipfw_dyn_rule *ipfw_lookup_dyn_rule(struct ipfw_flow_id *pkt, 188 int *match_direction, struct tcphdr *tcp); 189 void ipfw_remove_dyn_children(struct ip_fw *rule); 190 void ipfw_get_dynamic(struct ip_fw_chain *chain, char **bp, const char *ep); 191 192 void ipfw_dyn_init(struct ip_fw_chain *); /* per-vnet initialization */ 193 void ipfw_dyn_uninit(int); /* per-vnet deinitialization */ 194 int ipfw_dyn_len(void); 195 196 /* common variables */ 197 VNET_DECLARE(int, fw_one_pass); 198 #define V_fw_one_pass VNET(fw_one_pass) 199 200 VNET_DECLARE(int, fw_verbose); 201 #define V_fw_verbose VNET(fw_verbose) 202 203 VNET_DECLARE(struct ip_fw_chain, layer3_chain); 204 #define V_layer3_chain VNET(layer3_chain) 205 206 VNET_DECLARE(u_int32_t, set_disable); 207 #define V_set_disable VNET(set_disable) 208 209 VNET_DECLARE(int, autoinc_step); 210 #define V_autoinc_step VNET(autoinc_step) 211 212 VNET_DECLARE(unsigned int, fw_tables_max); 213 #define V_fw_tables_max VNET(fw_tables_max) 214 215 struct ip_fw_chain { 216 struct ip_fw *rules; /* list of rules */ 217 struct ip_fw *reap; /* list of rules to reap */ 218 struct ip_fw *default_rule; 219 int n_rules; /* number of static rules */ 220 int static_len; /* total len of static rules */ 221 struct ip_fw **map; /* array of rule ptrs to ease lookup */ 222 LIST_HEAD(nat_list, cfg_nat) nat; /* list of nat entries */ 223 struct radix_node_head **tables; /* IPv4 tables */ 224 struct radix_node_head **xtables; /* extended tables */ 225 uint8_t *tabletype; /* Array of table types */ 226 #if defined( __linux__ ) || defined( _WIN32 ) 227 spinlock_t rwmtx; 228 spinlock_t uh_lock; 229 #else 230 struct rwlock rwmtx; 231 struct rwlock uh_lock; /* lock for upper half */ 232 #endif 233 uint32_t id; /* ruleset id */ 234 uint32_t gencnt; /* generation count */ 235 }; 236 237 struct sockopt; /* used by tcp_var.h */ 238 239 /* Macro for working with various counters */ 240 #define IPFW_INC_RULE_COUNTER(_cntr, _bytes) do { \ 241 (_cntr)->pcnt++; \ 242 (_cntr)->bcnt += _bytes; \ 243 (_cntr)->timestamp = time_uptime; \ 244 } while (0) 245 246 #define IPFW_INC_DYN_COUNTER(_cntr, _bytes) do { \ 247 (_cntr)->pcnt++; \ 248 (_cntr)->bcnt += _bytes; \ 249 } while (0) 250 251 #define IPFW_ZERO_RULE_COUNTER(_cntr) do { \ 252 (_cntr)->pcnt = 0; \ 253 (_cntr)->bcnt = 0; \ 254 (_cntr)->timestamp = 0; \ 255 } while (0) 256 257 #define IPFW_ZERO_DYN_COUNTER(_cntr) do { \ 258 (_cntr)->pcnt = 0; \ 259 (_cntr)->bcnt = 0; \ 260 } while (0) 261 262 #define IP_FW_ARG_TABLEARG(a) (((a) == IP_FW_TABLEARG) ? tablearg : (a)) 263 /* 264 * The lock is heavily used by ip_fw2.c (the main file) and ip_fw_nat.c 265 * so the variable and the macros must be here. 266 */ 267 268 #define IPFW_LOCK_INIT(_chain) do { \ 269 rw_init(&(_chain)->rwmtx, "IPFW static rules"); \ 270 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \ 271 } while (0) 272 273 #define IPFW_LOCK_DESTROY(_chain) do { \ 274 rw_destroy(&(_chain)->rwmtx); \ 275 rw_destroy(&(_chain)->uh_lock); \ 276 } while (0) 277 278 #define IPFW_RLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_RLOCKED) 279 #define IPFW_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_WLOCKED) 280 281 #define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx) 282 #define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx) 283 #define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx) 284 #define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx) 285 #define IPFW_PF_RLOCK(p) IPFW_RLOCK(p) 286 #define IPFW_PF_RUNLOCK(p) IPFW_RUNLOCK(p) 287 288 #define IPFW_UH_RLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_RLOCKED) 289 #define IPFW_UH_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_WLOCKED) 290 291 #define IPFW_UH_RLOCK(p) rw_rlock(&(p)->uh_lock) 292 #define IPFW_UH_RUNLOCK(p) rw_runlock(&(p)->uh_lock) 293 #define IPFW_UH_WLOCK(p) rw_wlock(&(p)->uh_lock) 294 #define IPFW_UH_WUNLOCK(p) rw_wunlock(&(p)->uh_lock) 295 296 /* In ip_fw_sockopt.c */ 297 int ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id); 298 int ipfw_add_rule(struct ip_fw_chain *chain, struct ip_fw *input_rule); 299 int ipfw_ctl(struct sockopt *sopt); 300 int ipfw_chk(struct ip_fw_args *args); 301 void ipfw_reap_rules(struct ip_fw *head); 302 303 /* In ip_fw_table.c */ 304 struct radix_node; 305 int ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr, 306 uint32_t *val); 307 int ipfw_lookup_table_extended(struct ip_fw_chain *ch, uint16_t tbl, void *paddr, 308 uint32_t *val, int type); 309 int ipfw_init_tables(struct ip_fw_chain *ch); 310 void ipfw_destroy_tables(struct ip_fw_chain *ch); 311 int ipfw_flush_table(struct ip_fw_chain *ch, uint16_t tbl); 312 int ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr, 313 uint8_t plen, uint8_t mlen, uint8_t type, uint32_t value); 314 int ipfw_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr, 315 uint8_t plen, uint8_t mlen, uint8_t type); 316 int ipfw_count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt); 317 int ipfw_dump_table_entry(struct radix_node *rn, void *arg); 318 int ipfw_dump_table(struct ip_fw_chain *ch, ipfw_table *tbl); 319 int ipfw_count_xtable(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt); 320 int ipfw_dump_xtable(struct ip_fw_chain *ch, ipfw_xtable *tbl); 321 int ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables); 322 323 /* In ip_fw_nat.c -- XXX to be moved to ip_var.h */ 324 325 extern struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int); 326 327 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *); 328 typedef int ipfw_nat_cfg_t(struct sockopt *); 329 330 VNET_DECLARE(int, ipfw_nat_ready); 331 #define V_ipfw_nat_ready VNET(ipfw_nat_ready) 332 #define IPFW_NAT_LOADED (V_ipfw_nat_ready) 333 334 extern ipfw_nat_t *ipfw_nat_ptr; 335 extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr; 336 extern ipfw_nat_cfg_t *ipfw_nat_del_ptr; 337 extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr; 338 extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr; 339 340 #endif /* _KERNEL */ 341 #endif /* _IPFW2_PRIVATE_H */ 342