1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2007 Seccuris Inc. |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * This software was developed by Robert N. M. Watson under contract to |
---|
8 | * Seccuris Inc. |
---|
9 | * |
---|
10 | * Redistribution and use in source and binary forms, with or without |
---|
11 | * modification, are permitted provided that the following conditions |
---|
12 | * are met: |
---|
13 | * 1. Redistributions of source code must retain the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer. |
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer in the |
---|
17 | * documentation and/or other materials provided with the distribution. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | * |
---|
31 | * Copyright (c) 1990, 1991, 1993 |
---|
32 | * The Regents of the University of California. All rights reserved. |
---|
33 | * |
---|
34 | * This code is derived from the Stanford/CMU enet packet filter, |
---|
35 | * (net/enet.c) distributed as part of 4.3BSD, and code contributed |
---|
36 | * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence |
---|
37 | * Berkeley Laboratory. |
---|
38 | * |
---|
39 | * Redistribution and use in source and binary forms, with or without |
---|
40 | * modification, are permitted provided that the following conditions |
---|
41 | * are met: |
---|
42 | * 1. Redistributions of source code must retain the above copyright |
---|
43 | * notice, this list of conditions and the following disclaimer. |
---|
44 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
45 | * notice, this list of conditions and the following disclaimer in the |
---|
46 | * documentation and/or other materials provided with the distribution. |
---|
47 | * 3. Neither the name of the University nor the names of its contributors |
---|
48 | * may be used to endorse or promote products derived from this software |
---|
49 | * without specific prior written permission. |
---|
50 | * |
---|
51 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
52 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
53 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
54 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
55 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
56 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
57 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
58 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
59 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
60 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
61 | * SUCH DAMAGE. |
---|
62 | * |
---|
63 | * @(#)bpf.c 8.4 (Berkeley) 1/9/95 |
---|
64 | */ |
---|
65 | |
---|
66 | #include <sys/cdefs.h> |
---|
67 | __FBSDID("$FreeBSD$"); |
---|
68 | |
---|
69 | #include <rtems/bsd/local/opt_bpf.h> |
---|
70 | |
---|
71 | #include <sys/param.h> |
---|
72 | #include <sys/malloc.h> |
---|
73 | #include <sys/mbuf.h> |
---|
74 | #include <sys/socket.h> |
---|
75 | #include <sys/uio.h> |
---|
76 | #include <sys/kernel.h> |
---|
77 | #include <sys/sysctl.h> |
---|
78 | |
---|
79 | #include <net/if.h> |
---|
80 | #include <net/bpf.h> |
---|
81 | #include <net/bpf_buffer.h> |
---|
82 | #include <net/bpfdesc.h> |
---|
83 | |
---|
84 | /* |
---|
85 | * Implement historical kernel memory buffering model for BPF: two malloc(9) |
---|
86 | * kernel buffers are hung off of the descriptor. The size is fixed prior to |
---|
87 | * attaching to an ifnet, ad cannot be changed after that. read(2) simply |
---|
88 | * copies the data to user space using uiomove(9). |
---|
89 | */ |
---|
90 | |
---|
91 | static int bpf_bufsize = 4096; |
---|
92 | SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW, |
---|
93 | &bpf_bufsize, 0, "Default capture buffer size in bytes"); |
---|
94 | static int bpf_maxbufsize = BPF_MAXBUFSIZE; |
---|
95 | SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW, |
---|
96 | &bpf_maxbufsize, 0, "Maximum capture buffer in bytes"); |
---|
97 | |
---|
98 | /* |
---|
99 | * Simple data copy to the current kernel buffer. |
---|
100 | */ |
---|
101 | void |
---|
102 | bpf_buffer_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, |
---|
103 | void *src, u_int len) |
---|
104 | { |
---|
105 | u_char *src_bytes; |
---|
106 | |
---|
107 | src_bytes = (u_char *)src; |
---|
108 | bcopy(src_bytes, buf + offset, len); |
---|
109 | } |
---|
110 | |
---|
111 | /* |
---|
112 | * Scatter-gather data copy from an mbuf chain to the current kernel buffer. |
---|
113 | */ |
---|
114 | void |
---|
115 | bpf_buffer_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src, |
---|
116 | u_int len) |
---|
117 | { |
---|
118 | const struct mbuf *m; |
---|
119 | u_char *dst; |
---|
120 | u_int count; |
---|
121 | |
---|
122 | m = (struct mbuf *)src; |
---|
123 | dst = (u_char *)buf + offset; |
---|
124 | while (len > 0) { |
---|
125 | if (m == NULL) |
---|
126 | panic("bpf_mcopy"); |
---|
127 | count = min(m->m_len, len); |
---|
128 | bcopy(mtod(m, void *), dst, count); |
---|
129 | m = m->m_next; |
---|
130 | dst += count; |
---|
131 | len -= count; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | /* |
---|
136 | * Free BPF kernel buffers on device close. |
---|
137 | */ |
---|
138 | void |
---|
139 | bpf_buffer_free(struct bpf_d *d) |
---|
140 | { |
---|
141 | |
---|
142 | if (d->bd_sbuf != NULL) |
---|
143 | free(d->bd_sbuf, M_BPF); |
---|
144 | if (d->bd_hbuf != NULL) |
---|
145 | free(d->bd_hbuf, M_BPF); |
---|
146 | if (d->bd_fbuf != NULL) |
---|
147 | free(d->bd_fbuf, M_BPF); |
---|
148 | |
---|
149 | #ifdef INVARIANTS |
---|
150 | d->bd_sbuf = d->bd_hbuf = d->bd_fbuf = (caddr_t)~0; |
---|
151 | #endif |
---|
152 | } |
---|
153 | |
---|
154 | /* |
---|
155 | * This is a historical initialization that occurs when the BPF descriptor is |
---|
156 | * first opened. It does not imply selection of a buffer mode, so we don't |
---|
157 | * allocate buffers here. |
---|
158 | */ |
---|
159 | void |
---|
160 | bpf_buffer_init(struct bpf_d *d) |
---|
161 | { |
---|
162 | |
---|
163 | d->bd_bufsize = bpf_bufsize; |
---|
164 | } |
---|
165 | |
---|
166 | /* |
---|
167 | * Allocate or resize buffers. |
---|
168 | */ |
---|
169 | int |
---|
170 | bpf_buffer_ioctl_sblen(struct bpf_d *d, u_int *i) |
---|
171 | { |
---|
172 | u_int size; |
---|
173 | caddr_t fbuf, sbuf; |
---|
174 | |
---|
175 | size = *i; |
---|
176 | if (size > bpf_maxbufsize) |
---|
177 | *i = size = bpf_maxbufsize; |
---|
178 | else if (size < BPF_MINBUFSIZE) |
---|
179 | *i = size = BPF_MINBUFSIZE; |
---|
180 | |
---|
181 | /* Allocate buffers immediately */ |
---|
182 | fbuf = (caddr_t)malloc(size, M_BPF, M_WAITOK); |
---|
183 | sbuf = (caddr_t)malloc(size, M_BPF, M_WAITOK); |
---|
184 | |
---|
185 | BPFD_LOCK(d); |
---|
186 | if (d->bd_bif != NULL) { |
---|
187 | /* Interface already attached, unable to change buffers */ |
---|
188 | BPFD_UNLOCK(d); |
---|
189 | free(fbuf, M_BPF); |
---|
190 | free(sbuf, M_BPF); |
---|
191 | return (EINVAL); |
---|
192 | } |
---|
193 | |
---|
194 | /* Free old buffers if set */ |
---|
195 | if (d->bd_fbuf != NULL) |
---|
196 | free(d->bd_fbuf, M_BPF); |
---|
197 | if (d->bd_sbuf != NULL) |
---|
198 | free(d->bd_sbuf, M_BPF); |
---|
199 | |
---|
200 | /* Fill in new data */ |
---|
201 | d->bd_bufsize = size; |
---|
202 | d->bd_fbuf = fbuf; |
---|
203 | d->bd_sbuf = sbuf; |
---|
204 | |
---|
205 | d->bd_hbuf = NULL; |
---|
206 | d->bd_slen = 0; |
---|
207 | d->bd_hlen = 0; |
---|
208 | |
---|
209 | BPFD_UNLOCK(d); |
---|
210 | return (0); |
---|
211 | } |
---|
212 | |
---|
213 | /* |
---|
214 | * Copy buffer storage to user space in read(). |
---|
215 | */ |
---|
216 | int |
---|
217 | bpf_buffer_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio) |
---|
218 | { |
---|
219 | |
---|
220 | return (uiomove(buf, len, uio)); |
---|
221 | } |
---|