2 * Copyright (c) 2016 Yandex LLC
3 * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <net/ethernet.h>
38 #include <net/if_pflog.h>
39 #include <net/if_var.h>
40 #include <net/if_clone.h>
41 #include <net/if_types.h>
45 #include <netinet/in.h>
46 #include <netinet/ip_fw.h>
47 #include <netinet/ip_var.h>
48 #include <netpfil/ipfw/ip_fw_private.h>
50 VNET_DEFINE_STATIC(struct ifnet *, log_if);
51 VNET_DEFINE_STATIC(struct ifnet *, pflog_if);
52 VNET_DEFINE_STATIC(struct if_clone *, ipfw_cloner);
53 VNET_DEFINE_STATIC(struct if_clone *, ipfwlog_cloner);
54 #define V_ipfw_cloner VNET(ipfw_cloner)
55 #define V_ipfwlog_cloner VNET(ipfwlog_cloner)
56 #define V_log_if VNET(log_if)
57 #define V_pflog_if VNET(pflog_if)
59 static const char ipfwname[] = "ipfw";
60 static const char ipfwlogname[] = "ipfwlog";
63 ipfw_bpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
70 ipfw_bpf_output(struct ifnet *ifp, struct mbuf *m,
71 const struct sockaddr *dst, struct route *ro)
80 ipfw_clone_destroy(struct ifnet *ifp)
83 if (ifp->if_hdrlen == ETHER_HDR_LEN)
95 ipfw_clone_create(struct if_clone *ifc, int unit, caddr_t params)
99 ifp = if_alloc(IFT_PFLOG);
102 if_initname(ifp, ipfwname, unit);
103 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
105 ifp->if_ioctl = ipfw_bpf_ioctl;
106 ifp->if_output = ipfw_bpf_output;
107 ifp->if_hdrlen = ETHER_HDR_LEN;
109 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
110 if (V_log_if != NULL) {
121 ipfwlog_clone_create(struct if_clone *ifc, int unit, caddr_t params)
125 ifp = if_alloc(IFT_PFLOG);
128 if_initname(ifp, ipfwlogname, unit);
129 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
131 ifp->if_ioctl = ipfw_bpf_ioctl;
132 ifp->if_output = ipfw_bpf_output;
133 ifp->if_hdrlen = PFLOG_HDRLEN;
135 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
136 if (V_pflog_if != NULL) {
147 ipfw_bpf_tap(u_char *pkt, u_int pktlen)
149 struct ifnet *ifp = V_log_if;
153 BPF_TAP(ifp, pkt, pktlen);
157 ipfw_bpf_mtap(struct mbuf *m)
159 struct ifnet *ifp = V_log_if;
167 ipfw_bpf_mtap2(void *data, u_int dlen, struct mbuf *m)
173 case (ETHER_HDR_LEN):
181 panic("%s: unsupported len %d", __func__, dlen);
187 BPF_MTAP2(logif, data, dlen, m);
191 ipfw_bpf_init(int first __unused)
196 V_ipfw_cloner = if_clone_simple(ipfwname, ipfw_clone_create,
197 ipfw_clone_destroy, 0);
198 V_ipfwlog_cloner = if_clone_simple(ipfwlogname, ipfwlog_clone_create,
199 ipfw_clone_destroy, 0);
203 ipfw_bpf_uninit(int last __unused)
206 if_clone_detach(V_ipfw_cloner);
207 if_clone_detach(V_ipfwlog_cloner);