1*4408d548SBill Yuan /* 2*4408d548SBill Yuan * Copyright (c) 2014 - 2018 The DragonFly Project. All rights reserved. 3*4408d548SBill Yuan * 4*4408d548SBill Yuan * This code is derived from software contributed to The DragonFly Project 5*4408d548SBill Yuan * by Bill Yuan <bycn82@dragonflybsd.org> 6*4408d548SBill Yuan * 7*4408d548SBill Yuan * Redistribution and use in source and binary forms, with or without 8*4408d548SBill Yuan * modification, are permitted provided that the following conditions 9*4408d548SBill Yuan * are met: 10*4408d548SBill Yuan * 11*4408d548SBill Yuan * 1. Redistributions of source code must retain the above copyright 12*4408d548SBill Yuan * notice, this list of conditions and the following disclaimer. 13*4408d548SBill Yuan * 2. Redistributions in binary form must reproduce the above copyright 14*4408d548SBill Yuan * notice, this list of conditions and the following disclaimer in 15*4408d548SBill Yuan * the documentation and/or other materials provided with the 16*4408d548SBill Yuan * distribution. 17*4408d548SBill Yuan * 3. Neither the name of The DragonFly Project nor the names of its 18*4408d548SBill Yuan * contributors may be used to endorse or promote products derived 19*4408d548SBill Yuan * from this software without specific, prior written permission. 20*4408d548SBill Yuan * 21*4408d548SBill Yuan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*4408d548SBill Yuan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*4408d548SBill Yuan * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*4408d548SBill Yuan * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*4408d548SBill Yuan * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*4408d548SBill Yuan * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*4408d548SBill Yuan * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28*4408d548SBill Yuan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29*4408d548SBill Yuan * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30*4408d548SBill Yuan * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31*4408d548SBill Yuan * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32*4408d548SBill Yuan * SUCH DAMAGE. 33*4408d548SBill Yuan */ 34*4408d548SBill Yuan #ifndef _IP_FW3_STATE_H 35*4408d548SBill Yuan #define _IP_FW3_STATE_H 36*4408d548SBill Yuan 37*4408d548SBill Yuan struct ipfw3_ioc_state { 38*4408d548SBill Yuan struct in_addr src_addr; 39*4408d548SBill Yuan struct in_addr dst_addr; 40*4408d548SBill Yuan u_short src_port; 41*4408d548SBill Yuan u_short dst_port; 42*4408d548SBill Yuan int rule_id; 43*4408d548SBill Yuan int cpu_id; 44*4408d548SBill Yuan int proto; 45*4408d548SBill Yuan int direction; 46*4408d548SBill Yuan time_t life; 47*4408d548SBill Yuan }; 48*4408d548SBill Yuan 49*4408d548SBill Yuan #define LEN_IOC_FW3_STATE sizeof(struct ipfw3_ioc_state); 50*4408d548SBill Yuan 51*4408d548SBill Yuan 52*4408d548SBill Yuan #ifdef _KERNEL 53*4408d548SBill Yuan 54*4408d548SBill Yuan 55*4408d548SBill Yuan 56*4408d548SBill Yuan struct ipfw3_state { 57*4408d548SBill Yuan RB_ENTRY(ipfw3_state) entries; 58*4408d548SBill Yuan uint32_t src_addr; 59*4408d548SBill Yuan uint32_t dst_addr; 60*4408d548SBill Yuan uint16_t src_port; 61*4408d548SBill Yuan uint16_t dst_port; 62*4408d548SBill Yuan struct ip_fw *stub; 63*4408d548SBill Yuan time_t timestamp; 64*4408d548SBill Yuan }; 65*4408d548SBill Yuan #define LEN_FW3_STATE sizeof(struct ipfw3_state) 66*4408d548SBill Yuan 67*4408d548SBill Yuan int ip_fw3_state_cmp(struct ipfw3_state *s1, struct ipfw3_state *s2); 68*4408d548SBill Yuan 69*4408d548SBill Yuan RB_HEAD(fw3_state_tree, ipfw3_state); 70*4408d548SBill Yuan RB_PROTOTYPE(fw3_state_tree, ipfw3_state, entries, ip_fw3_state_cmp); 71*4408d548SBill Yuan 72*4408d548SBill Yuan /* place to hold the states */ 73*4408d548SBill Yuan struct ipfw3_state_context { 74*4408d548SBill Yuan struct fw3_state_tree rb_tcp_in; 75*4408d548SBill Yuan struct fw3_state_tree rb_tcp_out; 76*4408d548SBill Yuan struct fw3_state_tree rb_udp_in; 77*4408d548SBill Yuan struct fw3_state_tree rb_udp_out; 78*4408d548SBill Yuan struct fw3_state_tree rb_icmp_in; 79*4408d548SBill Yuan struct fw3_state_tree rb_icmp_out; 80*4408d548SBill Yuan 81*4408d548SBill Yuan int count_tcp_in; 82*4408d548SBill Yuan int count_tcp_out; 83*4408d548SBill Yuan int count_udp_in; 84*4408d548SBill Yuan int count_udp_out; 85*4408d548SBill Yuan int count_icmp_in; 86*4408d548SBill Yuan int count_icmp_out; 87*4408d548SBill Yuan }; 88*4408d548SBill Yuan #define LEN_STATE_CTX sizeof(struct ipfw3_state_context) 89*4408d548SBill Yuan 90*4408d548SBill Yuan void check_check_state(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 91*4408d548SBill Yuan struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len); 92*4408d548SBill Yuan void check_keep_state(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 93*4408d548SBill Yuan struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len); 94*4408d548SBill Yuan 95*4408d548SBill Yuan void ip_fw3_state_flush_dispatch(netmsg_t nmsg); 96*4408d548SBill Yuan void ip_fw3_state_flush(struct ip_fw *rule); 97*4408d548SBill Yuan 98*4408d548SBill Yuan void ip_fw3_state_cleanup_dispatch(netmsg_t nmsg); 99*4408d548SBill Yuan void ip_fw3_state_cleanup(void *dummy __unused); 100*4408d548SBill Yuan void ip_fw3_state_append_dispatch(netmsg_t nmsg); 101*4408d548SBill Yuan void ip_fw3_state_delete_dispatch(netmsg_t nmsg); 102*4408d548SBill Yuan int ip_fw3_ctl_state_add(struct sockopt *sopt); 103*4408d548SBill Yuan int ip_fw3_ctl_state_delete(struct sockopt *sopt); 104*4408d548SBill Yuan int ip_fw3_ctl_state_flush(struct sockopt *sopt); 105*4408d548SBill Yuan int ip_fw3_ctl_state_get(struct sockopt *sopt); 106*4408d548SBill Yuan int ip_fw3_ctl_state_sockopt(struct sockopt *sopt); 107*4408d548SBill Yuan void ip_fw3_state_init_dispatch(netmsg_t msg); 108*4408d548SBill Yuan void ip_fw3_state_fini_dispatch(netmsg_t msg); 109*4408d548SBill Yuan void ip_fw3_state_fini(void); 110*4408d548SBill Yuan void ip_fw3_state_init(void); 111*4408d548SBill Yuan void ip_fw3_state_modevent(int type); 112*4408d548SBill Yuan #endif /* _KERNEL */ 113*4408d548SBill Yuan #endif 114