source: rtems-libbsd/freebsd/sys/netpfil/ipfw/ip_fw_bpf.c @ 3c967ca

55-freebsd-126-freebsd-12
Last change on this file since 3c967ca was 3c967ca, checked in by Sebastian Huber <sebastian.huber@…>, on 06/08/17 at 11:15:12

Use <sys/lock.h> provided by Newlib

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