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