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 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <net/ethernet.h>
36 #include <net/if_pflog.h>
37 #include <net/if_var.h>
38 #include <net/if_clone.h>
39 #include <net/if_private.h>
40 #include <net/if_types.h>
44 #include <netinet/in.h>
45 #include <netinet/ip_fw.h>
46 #include <netinet/ip_var.h>
47 #include <netpfil/ipfw/ip_fw_private.h>
49 VNET_DEFINE_STATIC(struct ifnet *, log_if);
50 VNET_DEFINE_STATIC(struct ifnet *, pflog_if);
51 VNET_DEFINE_STATIC(struct if_clone *, ipfw_cloner);
52 VNET_DEFINE_STATIC(struct if_clone *, ipfwlog_cloner);
53 #define V_ipfw_cloner VNET(ipfw_cloner)
54 #define V_ipfwlog_cloner VNET(ipfwlog_cloner)
55 #define V_log_if VNET(log_if)
56 #define V_pflog_if VNET(pflog_if)
58 static const char ipfwname[] = "ipfw";
59 static const char ipfwlogname[] = "ipfwlog";
62 ipfw_bpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
69 ipfw_bpf_output(struct ifnet *ifp, struct mbuf *m,
70 const struct sockaddr *dst, struct route *ro)
79 ipfw_clone_destroy(struct ifnet *ifp)
82 if (ifp->if_hdrlen == ETHER_HDR_LEN)
94 ipfw_clone_create(struct if_clone *ifc, int unit, caddr_t params)
98 ifp = if_alloc(IFT_PFLOG);
101 if_initname(ifp, ipfwname, unit);
102 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
104 ifp->if_ioctl = ipfw_bpf_ioctl;
105 ifp->if_output = ipfw_bpf_output;
106 ifp->if_hdrlen = ETHER_HDR_LEN;
108 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
109 if (V_log_if != NULL) {
120 ipfwlog_clone_create(struct if_clone *ifc, int unit, caddr_t params)
124 ifp = if_alloc(IFT_PFLOG);
127 if_initname(ifp, ipfwlogname, unit);
128 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
130 ifp->if_ioctl = ipfw_bpf_ioctl;
131 ifp->if_output = ipfw_bpf_output;
132 ifp->if_hdrlen = PFLOG_HDRLEN;
134 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
135 if (V_pflog_if != NULL) {
146 ipfw_bpf_tap(u_char *pkt, u_int pktlen)
148 struct ifnet *ifp = V_log_if;
152 BPF_TAP(ifp, pkt, pktlen);
156 ipfw_bpf_mtap(struct mbuf *m)
158 struct ifnet *ifp = V_log_if;
166 ipfw_bpf_mtap2(void *data, u_int dlen, struct mbuf *m)
172 case (ETHER_HDR_LEN):
180 panic("%s: unsupported len %d", __func__, dlen);
186 BPF_MTAP2(logif, data, dlen, m);
190 ipfw_bpf_init(int first __unused)
195 V_ipfw_cloner = if_clone_simple(ipfwname, ipfw_clone_create,
196 ipfw_clone_destroy, 0);
197 V_ipfwlog_cloner = if_clone_simple(ipfwlogname, ipfwlog_clone_create,
198 ipfw_clone_destroy, 0);
202 ipfw_bpf_uninit(int last __unused)
205 if_clone_detach(V_ipfw_cloner);
206 if_clone_detach(V_ipfwlog_cloner);