1 /* 2 * Copyright (c) 2014 - 2018 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Bill Yuan <bycn82@dragonflybsd.org> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include "opt_ipfw.h" 36 #include "opt_inet.h" 37 #ifndef INET 38 #error IPFIREWALL3 requires INET. 39 #endif /* INET */ 40 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/socketvar.h> 45 #include <sys/sysctl.h> 46 #include <sys/systimer.h> 47 #include <sys/param.h> 48 #include <sys/ucred.h> 49 50 #include <netinet/in_var.h> 51 #include <netinet/ip_var.h> 52 #include <netinet/in.h> 53 #include <netinet/in_systm.h> 54 #include <netinet/in_var.h> 55 #include <netinet/in_pcb.h> 56 #include <netinet/ip.h> 57 #include <netinet/ip_var.h> 58 #include <netinet/ip_icmp.h> 59 #include <netinet/tcp.h> 60 #include <netinet/tcp_timer.h> 61 #include <netinet/tcp_var.h> 62 #include <netinet/tcpip.h> 63 #include <netinet/udp.h> 64 #include <netinet/udp_var.h> 65 #include <netinet/if_ether.h> 66 67 #include <net/bpf.h> 68 #include <net/ethernet.h> 69 #include <net/netmsg2.h> 70 #include <net/netisr2.h> 71 #include <net/route.h> 72 73 #include <net/ipfw3/ip_fw.h> 74 75 #include "ip_fw3_layer4.h" 76 77 void 78 check_tcpflag(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 79 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len); 80 void 81 check_uid(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 82 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len); 83 void 84 check_gid(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 85 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len); 86 void 87 check_established(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 88 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len); 89 void 90 check_bpf(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 91 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len); 92 93 /* 94 * ip_fw3_match_guid can match the gui and uid 95 */ 96 static int 97 ip_fw3_match_guid(const struct ipfw_flow_id *fid, struct ifnet *oif, 98 int opcode, uid_t uid) 99 { 100 struct in_addr src_ip, dst_ip; 101 struct inpcbinfo *pi; 102 boolean_t wildcard; 103 struct inpcb *pcb; 104 105 if (fid->proto == IPPROTO_TCP) { 106 wildcard = FALSE; 107 pi = &tcbinfo[mycpuid]; 108 } else if (fid->proto == IPPROTO_UDP) { 109 wildcard = TRUE; 110 pi = &udbinfo[mycpuid]; 111 } else { 112 return 0; 113 } 114 115 /* 116 * Values in 'fid' are in host byte order 117 */ 118 dst_ip.s_addr = htonl(fid->dst_ip); 119 src_ip.s_addr = htonl(fid->src_ip); 120 if (oif) { 121 pcb = in_pcblookup_hash(pi, 122 dst_ip, htons(fid->dst_port), 123 src_ip, htons(fid->src_port), 124 wildcard, oif); 125 } else { 126 pcb = in_pcblookup_hash(pi, 127 src_ip, htons(fid->src_port), 128 dst_ip, htons(fid->dst_port), 129 wildcard, NULL); 130 } 131 if (pcb == NULL || pcb->inp_socket == NULL) { 132 return 0; 133 } 134 135 if (opcode == O_LAYER4_UID) { 136 #define socheckuid(a,b) ((a)->so_cred->cr_uid != (b)) 137 return !socheckuid(pcb->inp_socket, uid); 138 #undef socheckuid 139 } else { 140 return groupmember(uid, pcb->inp_socket->so_cred); 141 } 142 } 143 144 void 145 check_tcpflag(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 146 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len) 147 { 148 /* XXX TODO check tcpflag */ 149 *cmd_val = 0; 150 *cmd_ctl = IP_FW_CTL_NO; 151 } 152 153 void 154 check_uid(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 155 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len) 156 { 157 *cmd_val = ip_fw3_match_guid(&(*args)->f_id, (*args)->oif, cmd->opcode, 158 (uid_t)((ipfw_insn_u32 *)cmd)->d[0]); 159 *cmd_ctl = IP_FW_CTL_NO; 160 } 161 162 void 163 check_gid(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 164 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len) 165 { 166 *cmd_val = ip_fw3_match_guid(&(*args)->f_id, (*args)->oif, cmd->opcode, 167 (gid_t)((ipfw_insn_u32 *)cmd)->d[0]); 168 *cmd_ctl = IP_FW_CTL_NO; 169 } 170 171 /* 172 * match TCP packets which have all tcpflag except SYN. 173 */ 174 void check_established(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 175 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len) 176 { 177 struct ipfw_flow_id *fid; 178 struct mbuf *m = (*args)->m; 179 struct ip *ip = mtod(m, struct ip *); 180 181 *cmd_ctl = IP_FW_CTL_NO; 182 fid = &(*args)->f_id; 183 if (fid->proto == IPPROTO_TCP) { 184 /* offset == 0 && */ 185 if ((L3HDR(struct tcphdr, ip)->th_flags & 186 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN) { 187 *cmd_val = IP_FW_MATCH; 188 return; 189 } 190 } 191 *cmd_val = IP_FW_NOT_MATCH; 192 } 193 194 void 195 check_bpf(int *cmd_ctl, int *cmd_val, struct ip_fw_args **args, 196 struct ip_fw **f, ipfw_insn *cmd, uint16_t ip_len) 197 { 198 u_int slen = 0; 199 struct mbuf *m = (*args)->m; 200 ipfw_insn_bpf *bpf = (ipfw_insn_bpf *)cmd; 201 *cmd_ctl = IP_FW_CTL_NO; 202 slen = bpf_filter(bpf->bf_insn, (u_char *)m, m_lengthm(m, NULL), 0); 203 if (slen != 0) 204 *cmd_val = IP_FW_MATCH; 205 else 206 *cmd_val = IP_FW_NOT_MATCH; 207 } 208 209 210 static int 211 ip_fw3_layer4_init(void) 212 { 213 ip_fw3_register_module(MODULE_LAYER4_ID, MODULE_LAYER4_NAME); 214 ip_fw3_register_filter_funcs(MODULE_LAYER4_ID, O_LAYER4_TCPFLAG, 215 (filter_func)check_tcpflag); 216 ip_fw3_register_filter_funcs(MODULE_LAYER4_ID, O_LAYER4_UID, 217 (filter_func)check_uid); 218 ip_fw3_register_filter_funcs(MODULE_LAYER4_ID, O_LAYER4_GID, 219 (filter_func)check_gid); 220 ip_fw3_register_filter_funcs(MODULE_LAYER4_ID, O_LAYER4_ESTABLISHED, 221 (filter_func)check_established); 222 ip_fw3_register_filter_funcs(MODULE_LAYER4_ID, O_LAYER4_BPF, 223 (filter_func)check_bpf); 224 return 0; 225 } 226 227 static int 228 ip_fw3_layer4_stop(void) 229 { 230 return ip_fw3_unregister_module(MODULE_LAYER4_ID); 231 } 232 233 static int 234 ipfw3_layer4_modevent(module_t mod, int type, void *data) 235 { 236 switch (type) { 237 case MOD_LOAD: 238 return ip_fw3_layer4_init(); 239 case MOD_UNLOAD: 240 return ip_fw3_layer4_stop(); 241 default: 242 break; 243 } 244 return 0; 245 } 246 247 static moduledata_t ipfw3_layer4_mod = { 248 "ipfw3_layer4", 249 ipfw3_layer4_modevent, 250 NULL 251 }; 252 DECLARE_MODULE(ipfw3_layer4, ipfw3_layer4_mod, SI_SUB_PROTO_END, SI_ORDER_ANY); 253 MODULE_DEPEND(ipfw3_layer4, ipfw3_basic, 1, 1, 1); 254 MODULE_VERSION(ipfw3_layer4, 1); 255