1 /*- 2 * Copyright (c) 2016 Yandex LLC 3 * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/mbuf.h> 33 #include <sys/kernel.h> 34 #include <sys/lock.h> 35 #include <sys/socket.h> 36 #include <net/ethernet.h> 37 #include <net/if.h> 38 #include <net/if_pflog.h> 39 #include <net/if_var.h> 40 #include <net/if_clone.h> 41 #include <net/if_private.h> 42 #include <net/if_types.h> 43 #include <net/vnet.h> 44 #include <net/bpf.h> 45 46 #include <netinet/in.h> 47 #include <netinet/ip_fw.h> 48 #include <netinet/ip_var.h> 49 #include <netpfil/ipfw/ip_fw_private.h> 50 51 VNET_DEFINE_STATIC(struct ifnet *, log_if); 52 VNET_DEFINE_STATIC(struct ifnet *, pflog_if); 53 VNET_DEFINE_STATIC(struct if_clone *, ipfw_cloner); 54 VNET_DEFINE_STATIC(struct if_clone *, ipfwlog_cloner); 55 #define V_ipfw_cloner VNET(ipfw_cloner) 56 #define V_ipfwlog_cloner VNET(ipfwlog_cloner) 57 #define V_log_if VNET(log_if) 58 #define V_pflog_if VNET(pflog_if) 59 60 static const char ipfwname[] = "ipfw"; 61 static const char ipfwlogname[] = "ipfwlog"; 62 63 static int 64 ipfw_bpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr) 65 { 66 67 return (EINVAL); 68 } 69 70 static int 71 ipfw_bpf_output(struct ifnet *ifp, struct mbuf *m, 72 const struct sockaddr *dst, struct route *ro) 73 { 74 75 if (m != NULL) 76 FREE_PKT(m); 77 return (0); 78 } 79 80 static void 81 ipfw_clone_destroy(struct ifnet *ifp) 82 { 83 84 if (ifp->if_hdrlen == ETHER_HDR_LEN) 85 V_log_if = NULL; 86 else 87 V_pflog_if = NULL; 88 89 NET_EPOCH_WAIT(); 90 bpfdetach(ifp); 91 if_detach(ifp); 92 if_free(ifp); 93 } 94 95 static int 96 ipfw_clone_create(struct if_clone *ifc, int unit, caddr_t params) 97 { 98 struct ifnet *ifp; 99 100 ifp = if_alloc(IFT_PFLOG); 101 if (ifp == NULL) 102 return (ENOSPC); 103 if_initname(ifp, ipfwname, unit); 104 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST; 105 ifp->if_mtu = 65536; 106 ifp->if_ioctl = ipfw_bpf_ioctl; 107 ifp->if_output = ipfw_bpf_output; 108 ifp->if_hdrlen = ETHER_HDR_LEN; 109 if_attach(ifp); 110 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 111 if (V_log_if != NULL) { 112 bpfdetach(ifp); 113 if_detach(ifp); 114 if_free(ifp); 115 return (EEXIST); 116 } 117 V_log_if = ifp; 118 return (0); 119 } 120 121 static int 122 ipfwlog_clone_create(struct if_clone *ifc, int unit, caddr_t params) 123 { 124 struct ifnet *ifp; 125 126 ifp = if_alloc(IFT_PFLOG); 127 if (ifp == NULL) 128 return (ENOSPC); 129 if_initname(ifp, ipfwlogname, unit); 130 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST; 131 ifp->if_mtu = 65536; 132 ifp->if_ioctl = ipfw_bpf_ioctl; 133 ifp->if_output = ipfw_bpf_output; 134 ifp->if_hdrlen = PFLOG_HDRLEN; 135 if_attach(ifp); 136 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN); 137 if (V_pflog_if != NULL) { 138 bpfdetach(ifp); 139 if_detach(ifp); 140 if_free(ifp); 141 return (EEXIST); 142 } 143 V_pflog_if = ifp; 144 return (0); 145 } 146 147 void 148 ipfw_bpf_tap(u_char *pkt, u_int pktlen) 149 { 150 struct ifnet *ifp = V_log_if; 151 152 NET_EPOCH_ASSERT(); 153 if (ifp != NULL) 154 BPF_TAP(ifp, pkt, pktlen); 155 } 156 157 void 158 ipfw_bpf_mtap(struct mbuf *m) 159 { 160 struct ifnet *ifp = V_log_if; 161 162 NET_EPOCH_ASSERT(); 163 if (ifp != NULL) 164 BPF_MTAP(ifp, m); 165 } 166 167 void 168 ipfw_bpf_mtap2(void *data, u_int dlen, struct mbuf *m) 169 { 170 struct ifnet *logif; 171 172 NET_EPOCH_ASSERT(); 173 switch (dlen) { 174 case (ETHER_HDR_LEN): 175 logif = V_log_if; 176 break; 177 case (PFLOG_HDRLEN): 178 logif = V_pflog_if; 179 break; 180 default: 181 #ifdef INVARIANTS 182 panic("%s: unsupported len %d", __func__, dlen); 183 #endif 184 logif = NULL; 185 } 186 187 if (logif != NULL) 188 BPF_MTAP2(logif, data, dlen, m); 189 } 190 191 void 192 ipfw_bpf_init(int first __unused) 193 { 194 195 V_log_if = NULL; 196 V_pflog_if = NULL; 197 V_ipfw_cloner = if_clone_simple(ipfwname, ipfw_clone_create, 198 ipfw_clone_destroy, 0); 199 V_ipfwlog_cloner = if_clone_simple(ipfwlogname, ipfwlog_clone_create, 200 ipfw_clone_destroy, 0); 201 } 202 203 void 204 ipfw_bpf_uninit(int last __unused) 205 { 206 207 if_clone_detach(V_ipfw_cloner); 208 if_clone_detach(V_ipfwlog_cloner); 209 } 210