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/param.h> 28 #include <sys/systm.h> 29 #include <sys/mbuf.h> 30 #include <sys/kernel.h> 31 #include <sys/lock.h> 32 #include <sys/socket.h> 33 #include <net/ethernet.h> 34 #include <net/if.h> 35 #include <net/if_pflog.h> 36 #include <net/if_var.h> 37 #include <net/if_clone.h> 38 #include <net/if_private.h> 39 #include <net/if_types.h> 40 #include <net/vnet.h> 41 #include <net/bpf.h> 42 43 #include <netinet/in.h> 44 #include <netinet/ip_fw.h> 45 #include <netinet/ip_var.h> 46 #include <netpfil/ipfw/ip_fw_private.h> 47 48 VNET_DEFINE_STATIC(struct ifnet *, log_if); 49 VNET_DEFINE_STATIC(struct ifnet *, pflog_if); 50 VNET_DEFINE_STATIC(struct if_clone *, ipfw_cloner); 51 VNET_DEFINE_STATIC(struct if_clone *, ipfwlog_cloner); 52 #define V_ipfw_cloner VNET(ipfw_cloner) 53 #define V_ipfwlog_cloner VNET(ipfwlog_cloner) 54 #define V_log_if VNET(log_if) 55 #define V_pflog_if VNET(pflog_if) 56 57 static const char ipfwname[] = "ipfw"; 58 static const char ipfwlogname[] = "ipfwlog"; 59 60 static int 61 ipfw_bpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr) 62 { 63 64 return (EINVAL); 65 } 66 67 static int 68 ipfw_bpf_output(struct ifnet *ifp, struct mbuf *m, 69 const struct sockaddr *dst, struct route *ro) 70 { 71 72 if (m != NULL) 73 FREE_PKT(m); 74 return (0); 75 } 76 77 static void 78 ipfw_clone_destroy(struct ifnet *ifp) 79 { 80 81 if (ifp->if_hdrlen == ETHER_HDR_LEN) 82 V_log_if = NULL; 83 else 84 V_pflog_if = NULL; 85 86 NET_EPOCH_WAIT(); 87 bpfdetach(ifp); 88 if_detach(ifp); 89 if_free(ifp); 90 } 91 92 static int 93 ipfw_clone_create(struct if_clone *ifc, int unit, caddr_t params) 94 { 95 struct ifnet *ifp; 96 97 ifp = if_alloc(IFT_PFLOG); 98 if (ifp == NULL) 99 return (ENOSPC); 100 if_initname(ifp, ipfwname, unit); 101 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST; 102 ifp->if_mtu = 65536; 103 ifp->if_ioctl = ipfw_bpf_ioctl; 104 ifp->if_output = ipfw_bpf_output; 105 ifp->if_hdrlen = ETHER_HDR_LEN; 106 if_attach(ifp); 107 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 108 if (V_log_if != NULL) { 109 bpfdetach(ifp); 110 if_detach(ifp); 111 if_free(ifp); 112 return (EEXIST); 113 } 114 V_log_if = ifp; 115 return (0); 116 } 117 118 static int 119 ipfwlog_clone_create(struct if_clone *ifc, int unit, caddr_t params) 120 { 121 struct ifnet *ifp; 122 123 ifp = if_alloc(IFT_PFLOG); 124 if (ifp == NULL) 125 return (ENOSPC); 126 if_initname(ifp, ipfwlogname, unit); 127 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST; 128 ifp->if_mtu = 65536; 129 ifp->if_ioctl = ipfw_bpf_ioctl; 130 ifp->if_output = ipfw_bpf_output; 131 ifp->if_hdrlen = PFLOG_HDRLEN; 132 if_attach(ifp); 133 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN); 134 if (V_pflog_if != NULL) { 135 bpfdetach(ifp); 136 if_detach(ifp); 137 if_free(ifp); 138 return (EEXIST); 139 } 140 V_pflog_if = ifp; 141 return (0); 142 } 143 144 void 145 ipfw_bpf_tap(u_char *pkt, u_int pktlen) 146 { 147 struct ifnet *ifp = V_log_if; 148 149 NET_EPOCH_ASSERT(); 150 if (ifp != NULL) 151 BPF_TAP(ifp, pkt, pktlen); 152 } 153 154 void 155 ipfw_bpf_mtap(struct mbuf *m) 156 { 157 struct ifnet *ifp = V_log_if; 158 159 NET_EPOCH_ASSERT(); 160 if (ifp != NULL) 161 BPF_MTAP(ifp, m); 162 } 163 164 void 165 ipfw_bpf_mtap2(void *data, u_int dlen, struct mbuf *m) 166 { 167 struct ifnet *logif; 168 169 NET_EPOCH_ASSERT(); 170 switch (dlen) { 171 case (ETHER_HDR_LEN): 172 logif = V_log_if; 173 break; 174 case (PFLOG_HDRLEN): 175 logif = V_pflog_if; 176 break; 177 default: 178 #ifdef INVARIANTS 179 panic("%s: unsupported len %d", __func__, dlen); 180 #endif 181 logif = NULL; 182 } 183 184 if (logif != NULL) 185 BPF_MTAP2(logif, data, dlen, m); 186 } 187 188 void 189 ipfw_bpf_init(int first __unused) 190 { 191 192 V_log_if = NULL; 193 V_pflog_if = NULL; 194 V_ipfw_cloner = if_clone_simple(ipfwname, ipfw_clone_create, 195 ipfw_clone_destroy, 0); 196 V_ipfwlog_cloner = if_clone_simple(ipfwlogname, ipfwlog_clone_create, 197 ipfw_clone_destroy, 0); 198 } 199 200 void 201 ipfw_bpf_uninit(int last __unused) 202 { 203 204 if_clone_detach(V_ipfw_cloner); 205 if_clone_detach(V_ipfwlog_cloner); 206 } 207