1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * SPDX-License-Identifier: BSD-3-Clause |
---|
5 | * |
---|
6 | * Copyright (c) 1990, 1991, 1993 |
---|
7 | * The Regents of the University of California. All rights reserved. |
---|
8 | * |
---|
9 | * This code is derived from the Stanford/CMU enet packet filter, |
---|
10 | * (net/enet.c) distributed as part of 4.3BSD, and code contributed |
---|
11 | * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence |
---|
12 | * Berkeley Laboratory. |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * 3. Neither the name of the University nor the names of its contributors |
---|
23 | * may be used to endorse or promote products derived from this software |
---|
24 | * without specific prior written permission. |
---|
25 | * |
---|
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
36 | * SUCH DAMAGE. |
---|
37 | * |
---|
38 | * @(#)bpf.c 8.4 (Berkeley) 1/9/95 |
---|
39 | */ |
---|
40 | |
---|
41 | #include <sys/cdefs.h> |
---|
42 | __FBSDID("$FreeBSD$"); |
---|
43 | |
---|
44 | #include <rtems/bsd/local/opt_bpf.h> |
---|
45 | #include <rtems/bsd/local/opt_ddb.h> |
---|
46 | #include <rtems/bsd/local/opt_netgraph.h> |
---|
47 | |
---|
48 | #include <sys/types.h> |
---|
49 | #include <sys/param.h> |
---|
50 | #include <sys/lock.h> |
---|
51 | #include <sys/rwlock.h> |
---|
52 | #include <sys/systm.h> |
---|
53 | #include <sys/conf.h> |
---|
54 | #include <sys/fcntl.h> |
---|
55 | #include <sys/jail.h> |
---|
56 | #include <sys/malloc.h> |
---|
57 | #include <sys/mbuf.h> |
---|
58 | #include <sys/time.h> |
---|
59 | #include <sys/priv.h> |
---|
60 | #include <sys/proc.h> |
---|
61 | #include <sys/signalvar.h> |
---|
62 | #include <sys/filio.h> |
---|
63 | #include <sys/sockio.h> |
---|
64 | #include <sys/ttycom.h> |
---|
65 | #include <sys/uio.h> |
---|
66 | #include <sys/sysent.h> |
---|
67 | |
---|
68 | #include <sys/event.h> |
---|
69 | #include <sys/file.h> |
---|
70 | #include <sys/poll.h> |
---|
71 | #include <sys/proc.h> |
---|
72 | |
---|
73 | #include <sys/socket.h> |
---|
74 | |
---|
75 | #ifdef DDB |
---|
76 | #include <ddb/ddb.h> |
---|
77 | #endif |
---|
78 | |
---|
79 | #include <net/if.h> |
---|
80 | #include <net/if_var.h> |
---|
81 | #include <net/if_dl.h> |
---|
82 | #include <net/bpf.h> |
---|
83 | #include <net/bpf_buffer.h> |
---|
84 | #ifdef BPF_JITTER |
---|
85 | #include <net/bpf_jitter.h> |
---|
86 | #endif |
---|
87 | #include <net/bpf_zerocopy.h> |
---|
88 | #include <net/bpfdesc.h> |
---|
89 | #include <net/route.h> |
---|
90 | #include <net/vnet.h> |
---|
91 | |
---|
92 | #include <netinet/in.h> |
---|
93 | #include <netinet/if_ether.h> |
---|
94 | #include <sys/kernel.h> |
---|
95 | #include <sys/sysctl.h> |
---|
96 | |
---|
97 | #include <net80211/ieee80211_freebsd.h> |
---|
98 | |
---|
99 | #include <security/mac/mac_framework.h> |
---|
100 | #ifdef __rtems__ |
---|
101 | #include <rtems/imfs.h> |
---|
102 | #define devfs_get_cdevpriv(x) 0 |
---|
103 | #define devtoname(x) "bpf" |
---|
104 | #endif /* __rtems__ */ |
---|
105 | |
---|
106 | MALLOC_DEFINE(M_BPF, "BPF", "BPF data"); |
---|
107 | |
---|
108 | static struct bpf_if_ext dead_bpf_if = { |
---|
109 | .bif_dlist = LIST_HEAD_INITIALIZER() |
---|
110 | }; |
---|
111 | |
---|
112 | struct bpf_if { |
---|
113 | #define bif_next bif_ext.bif_next |
---|
114 | #define bif_dlist bif_ext.bif_dlist |
---|
115 | struct bpf_if_ext bif_ext; /* public members */ |
---|
116 | u_int bif_dlt; /* link layer type */ |
---|
117 | u_int bif_hdrlen; /* length of link header */ |
---|
118 | struct ifnet *bif_ifp; /* corresponding interface */ |
---|
119 | struct rwlock bif_lock; /* interface lock */ |
---|
120 | LIST_HEAD(, bpf_d) bif_wlist; /* writer-only list */ |
---|
121 | int bif_flags; /* Interface flags */ |
---|
122 | struct bpf_if **bif_bpf; /* Pointer to pointer to us */ |
---|
123 | }; |
---|
124 | |
---|
125 | CTASSERT(offsetof(struct bpf_if, bif_ext) == 0); |
---|
126 | |
---|
127 | #define BPFIF_RLOCK(bif) rw_rlock(&(bif)->bif_lock) |
---|
128 | #define BPFIF_RUNLOCK(bif) rw_runlock(&(bif)->bif_lock) |
---|
129 | #define BPFIF_WLOCK(bif) rw_wlock(&(bif)->bif_lock) |
---|
130 | #define BPFIF_WUNLOCK(bif) rw_wunlock(&(bif)->bif_lock) |
---|
131 | |
---|
132 | #if defined(DEV_BPF) || defined(NETGRAPH_BPF) |
---|
133 | |
---|
134 | #define PRINET 26 /* interruptible */ |
---|
135 | |
---|
136 | #define SIZEOF_BPF_HDR(type) \ |
---|
137 | (offsetof(type, bh_hdrlen) + sizeof(((type *)0)->bh_hdrlen)) |
---|
138 | |
---|
139 | #ifdef COMPAT_FREEBSD32 |
---|
140 | #include <sys/mount.h> |
---|
141 | #include <compat/freebsd32/freebsd32.h> |
---|
142 | #define BPF_ALIGNMENT32 sizeof(int32_t) |
---|
143 | #define BPF_WORDALIGN32(x) roundup2(x, BPF_ALIGNMENT32) |
---|
144 | |
---|
145 | #ifdef __rtems__ |
---|
146 | /* |
---|
147 | * This FreeBSD kernel option is broken in general, but here it is useful to |
---|
148 | * get rid of some legacy support we do not need in libbsd. |
---|
149 | */ |
---|
150 | #define BURN_BRIDGES |
---|
151 | #endif /* __rtems__ */ |
---|
152 | #ifndef BURN_BRIDGES |
---|
153 | /* |
---|
154 | * 32-bit version of structure prepended to each packet. We use this header |
---|
155 | * instead of the standard one for 32-bit streams. We mark the a stream as |
---|
156 | * 32-bit the first time we see a 32-bit compat ioctl request. |
---|
157 | */ |
---|
158 | struct bpf_hdr32 { |
---|
159 | struct timeval32 bh_tstamp; /* time stamp */ |
---|
160 | uint32_t bh_caplen; /* length of captured portion */ |
---|
161 | uint32_t bh_datalen; /* original length of packet */ |
---|
162 | uint16_t bh_hdrlen; /* length of bpf header (this struct |
---|
163 | plus alignment padding) */ |
---|
164 | }; |
---|
165 | #endif |
---|
166 | |
---|
167 | struct bpf_program32 { |
---|
168 | u_int bf_len; |
---|
169 | uint32_t bf_insns; |
---|
170 | }; |
---|
171 | |
---|
172 | struct bpf_dltlist32 { |
---|
173 | u_int bfl_len; |
---|
174 | u_int bfl_list; |
---|
175 | }; |
---|
176 | |
---|
177 | #define BIOCSETF32 _IOW('B', 103, struct bpf_program32) |
---|
178 | #define BIOCSRTIMEOUT32 _IOW('B', 109, struct timeval32) |
---|
179 | #define BIOCGRTIMEOUT32 _IOR('B', 110, struct timeval32) |
---|
180 | #define BIOCGDLTLIST32 _IOWR('B', 121, struct bpf_dltlist32) |
---|
181 | #define BIOCSETWF32 _IOW('B', 123, struct bpf_program32) |
---|
182 | #define BIOCSETFNR32 _IOW('B', 130, struct bpf_program32) |
---|
183 | #endif |
---|
184 | |
---|
185 | #define BPF_LOCK() sx_xlock(&bpf_sx) |
---|
186 | #define BPF_UNLOCK() sx_xunlock(&bpf_sx) |
---|
187 | #define BPF_LOCK_ASSERT() sx_assert(&bpf_sx, SA_XLOCKED) |
---|
188 | /* |
---|
189 | * bpf_iflist is a list of BPF interface structures, each corresponding to a |
---|
190 | * specific DLT. The same network interface might have several BPF interface |
---|
191 | * structures registered by different layers in the stack (i.e., 802.11 |
---|
192 | * frames, ethernet frames, etc). |
---|
193 | */ |
---|
194 | static LIST_HEAD(, bpf_if) bpf_iflist, bpf_freelist; |
---|
195 | static struct sx bpf_sx; /* bpf global lock */ |
---|
196 | static int bpf_bpfd_cnt; |
---|
197 | |
---|
198 | static void bpf_attachd(struct bpf_d *, struct bpf_if *); |
---|
199 | static void bpf_detachd(struct bpf_d *); |
---|
200 | static void bpf_detachd_locked(struct bpf_d *); |
---|
201 | static void bpf_freed(struct bpf_d *); |
---|
202 | static int bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **, |
---|
203 | struct sockaddr *, int *, struct bpf_d *); |
---|
204 | static int bpf_setif(struct bpf_d *, struct ifreq *); |
---|
205 | static void bpf_timed_out(void *); |
---|
206 | static __inline void |
---|
207 | bpf_wakeup(struct bpf_d *); |
---|
208 | static void catchpacket(struct bpf_d *, u_char *, u_int, u_int, |
---|
209 | void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int), |
---|
210 | struct bintime *); |
---|
211 | static void reset_d(struct bpf_d *); |
---|
212 | static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd); |
---|
213 | static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *); |
---|
214 | static int bpf_setdlt(struct bpf_d *, u_int); |
---|
215 | static void filt_bpfdetach(struct knote *); |
---|
216 | static int filt_bpfread(struct knote *, long); |
---|
217 | static void bpf_drvinit(void *); |
---|
218 | static int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS); |
---|
219 | |
---|
220 | SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl"); |
---|
221 | int bpf_maxinsns = BPF_MAXINSNS; |
---|
222 | SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW, |
---|
223 | &bpf_maxinsns, 0, "Maximum bpf program instructions"); |
---|
224 | #ifndef __rtems__ |
---|
225 | static int bpf_zerocopy_enable = 0; |
---|
226 | SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW, |
---|
227 | &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions"); |
---|
228 | #endif /* __rtems__ */ |
---|
229 | static SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW, |
---|
230 | bpf_stats_sysctl, "bpf statistics portal"); |
---|
231 | |
---|
232 | VNET_DEFINE_STATIC(int, bpf_optimize_writers) = 0; |
---|
233 | #define V_bpf_optimize_writers VNET(bpf_optimize_writers) |
---|
234 | SYSCTL_INT(_net_bpf, OID_AUTO, optimize_writers, CTLFLAG_VNET | CTLFLAG_RW, |
---|
235 | &VNET_NAME(bpf_optimize_writers), 0, |
---|
236 | "Do not send packets until BPF program is set"); |
---|
237 | |
---|
238 | #ifndef __rtems__ |
---|
239 | static d_open_t bpfopen; |
---|
240 | static d_read_t bpfread; |
---|
241 | static d_write_t bpfwrite; |
---|
242 | static d_ioctl_t bpfioctl; |
---|
243 | static d_poll_t bpfpoll; |
---|
244 | static d_kqfilter_t bpfkqfilter; |
---|
245 | |
---|
246 | static struct cdevsw bpf_cdevsw = { |
---|
247 | .d_version = D_VERSION, |
---|
248 | .d_open = bpfopen, |
---|
249 | .d_read = bpfread, |
---|
250 | .d_write = bpfwrite, |
---|
251 | .d_ioctl = bpfioctl, |
---|
252 | .d_poll = bpfpoll, |
---|
253 | .d_name = "bpf", |
---|
254 | .d_kqfilter = bpfkqfilter, |
---|
255 | }; |
---|
256 | #endif /* __rtems__ */ |
---|
257 | |
---|
258 | static struct filterops bpfread_filtops = { |
---|
259 | .f_isfd = 1, |
---|
260 | .f_detach = filt_bpfdetach, |
---|
261 | .f_event = filt_bpfread, |
---|
262 | }; |
---|
263 | |
---|
264 | eventhandler_tag bpf_ifdetach_cookie = NULL; |
---|
265 | |
---|
266 | /* |
---|
267 | * LOCKING MODEL USED BY BPF: |
---|
268 | * Locks: |
---|
269 | * 1) global lock (BPF_LOCK). Mutex, used to protect interface addition/removal, |
---|
270 | * some global counters and every bpf_if reference. |
---|
271 | * 2) Interface lock. Rwlock, used to protect list of BPF descriptors and their filters. |
---|
272 | * 3) Descriptor lock. Mutex, used to protect BPF buffers and various structure fields |
---|
273 | * used by bpf_mtap code. |
---|
274 | * |
---|
275 | * Lock order: |
---|
276 | * |
---|
277 | * Global lock, interface lock, descriptor lock |
---|
278 | * |
---|
279 | * We have to acquire interface lock before descriptor main lock due to BPF_MTAP[2] |
---|
280 | * working model. In many places (like bpf_detachd) we start with BPF descriptor |
---|
281 | * (and we need to at least rlock it to get reliable interface pointer). This |
---|
282 | * gives us potential LOR. As a result, we use global lock to protect from bpf_if |
---|
283 | * change in every such place. |
---|
284 | * |
---|
285 | * Changing d->bd_bif is protected by 1) global lock, 2) interface lock and |
---|
286 | * 3) descriptor main wlock. |
---|
287 | * Reading bd_bif can be protected by any of these locks, typically global lock. |
---|
288 | * |
---|
289 | * Changing read/write BPF filter is protected by the same three locks, |
---|
290 | * the same applies for reading. |
---|
291 | * |
---|
292 | * Sleeping in global lock is not allowed due to bpfdetach() using it. |
---|
293 | */ |
---|
294 | |
---|
295 | /* |
---|
296 | * Wrapper functions for various buffering methods. If the set of buffer |
---|
297 | * modes expands, we will probably want to introduce a switch data structure |
---|
298 | * similar to protosw, et. |
---|
299 | */ |
---|
300 | static void |
---|
301 | bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src, |
---|
302 | u_int len) |
---|
303 | { |
---|
304 | |
---|
305 | BPFD_LOCK_ASSERT(d); |
---|
306 | |
---|
307 | switch (d->bd_bufmode) { |
---|
308 | case BPF_BUFMODE_BUFFER: |
---|
309 | return (bpf_buffer_append_bytes(d, buf, offset, src, len)); |
---|
310 | |
---|
311 | #ifndef __rtems__ |
---|
312 | case BPF_BUFMODE_ZBUF: |
---|
313 | counter_u64_add(d->bd_zcopy, 1); |
---|
314 | return (bpf_zerocopy_append_bytes(d, buf, offset, src, len)); |
---|
315 | #endif /* __rtems__ */ |
---|
316 | |
---|
317 | default: |
---|
318 | panic("bpf_buf_append_bytes"); |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | static void |
---|
323 | bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src, |
---|
324 | u_int len) |
---|
325 | { |
---|
326 | |
---|
327 | BPFD_LOCK_ASSERT(d); |
---|
328 | |
---|
329 | switch (d->bd_bufmode) { |
---|
330 | case BPF_BUFMODE_BUFFER: |
---|
331 | return (bpf_buffer_append_mbuf(d, buf, offset, src, len)); |
---|
332 | |
---|
333 | #ifndef __rtems__ |
---|
334 | case BPF_BUFMODE_ZBUF: |
---|
335 | counter_u64_add(d->bd_zcopy, 1); |
---|
336 | return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len)); |
---|
337 | #endif /* __rtems__ */ |
---|
338 | |
---|
339 | default: |
---|
340 | panic("bpf_buf_append_mbuf"); |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | /* |
---|
345 | * This function gets called when the free buffer is re-assigned. |
---|
346 | */ |
---|
347 | static void |
---|
348 | bpf_buf_reclaimed(struct bpf_d *d) |
---|
349 | { |
---|
350 | |
---|
351 | BPFD_LOCK_ASSERT(d); |
---|
352 | |
---|
353 | switch (d->bd_bufmode) { |
---|
354 | case BPF_BUFMODE_BUFFER: |
---|
355 | return; |
---|
356 | |
---|
357 | #ifndef __rtems__ |
---|
358 | case BPF_BUFMODE_ZBUF: |
---|
359 | bpf_zerocopy_buf_reclaimed(d); |
---|
360 | return; |
---|
361 | #endif /* __rtems__ */ |
---|
362 | |
---|
363 | default: |
---|
364 | panic("bpf_buf_reclaimed"); |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | /* |
---|
369 | * If the buffer mechanism has a way to decide that a held buffer can be made |
---|
370 | * free, then it is exposed via the bpf_canfreebuf() interface. (1) is |
---|
371 | * returned if the buffer can be discarded, (0) is returned if it cannot. |
---|
372 | */ |
---|
373 | static int |
---|
374 | bpf_canfreebuf(struct bpf_d *d) |
---|
375 | { |
---|
376 | |
---|
377 | BPFD_LOCK_ASSERT(d); |
---|
378 | |
---|
379 | #ifndef __rtems__ |
---|
380 | switch (d->bd_bufmode) { |
---|
381 | case BPF_BUFMODE_ZBUF: |
---|
382 | return (bpf_zerocopy_canfreebuf(d)); |
---|
383 | } |
---|
384 | #endif /* __rtems__ */ |
---|
385 | return (0); |
---|
386 | } |
---|
387 | |
---|
388 | /* |
---|
389 | * Allow the buffer model to indicate that the current store buffer is |
---|
390 | * immutable, regardless of the appearance of space. Return (1) if the |
---|
391 | * buffer is writable, and (0) if not. |
---|
392 | */ |
---|
393 | static int |
---|
394 | bpf_canwritebuf(struct bpf_d *d) |
---|
395 | { |
---|
396 | BPFD_LOCK_ASSERT(d); |
---|
397 | |
---|
398 | #ifndef __rtems__ |
---|
399 | switch (d->bd_bufmode) { |
---|
400 | case BPF_BUFMODE_ZBUF: |
---|
401 | return (bpf_zerocopy_canwritebuf(d)); |
---|
402 | } |
---|
403 | #endif /* __rtems__ */ |
---|
404 | return (1); |
---|
405 | } |
---|
406 | |
---|
407 | /* |
---|
408 | * Notify buffer model that an attempt to write to the store buffer has |
---|
409 | * resulted in a dropped packet, in which case the buffer may be considered |
---|
410 | * full. |
---|
411 | */ |
---|
412 | static void |
---|
413 | bpf_buffull(struct bpf_d *d) |
---|
414 | { |
---|
415 | |
---|
416 | BPFD_LOCK_ASSERT(d); |
---|
417 | |
---|
418 | #ifndef __rtems__ |
---|
419 | switch (d->bd_bufmode) { |
---|
420 | case BPF_BUFMODE_ZBUF: |
---|
421 | bpf_zerocopy_buffull(d); |
---|
422 | break; |
---|
423 | } |
---|
424 | #endif /* __rtems__ */ |
---|
425 | } |
---|
426 | |
---|
427 | /* |
---|
428 | * Notify the buffer model that a buffer has moved into the hold position. |
---|
429 | */ |
---|
430 | void |
---|
431 | bpf_bufheld(struct bpf_d *d) |
---|
432 | { |
---|
433 | |
---|
434 | BPFD_LOCK_ASSERT(d); |
---|
435 | |
---|
436 | #ifndef __rtems__ |
---|
437 | switch (d->bd_bufmode) { |
---|
438 | case BPF_BUFMODE_ZBUF: |
---|
439 | bpf_zerocopy_bufheld(d); |
---|
440 | break; |
---|
441 | } |
---|
442 | #endif /* __rtems__ */ |
---|
443 | } |
---|
444 | |
---|
445 | static void |
---|
446 | bpf_free(struct bpf_d *d) |
---|
447 | { |
---|
448 | |
---|
449 | switch (d->bd_bufmode) { |
---|
450 | case BPF_BUFMODE_BUFFER: |
---|
451 | return (bpf_buffer_free(d)); |
---|
452 | |
---|
453 | #ifndef __rtems__ |
---|
454 | case BPF_BUFMODE_ZBUF: |
---|
455 | return (bpf_zerocopy_free(d)); |
---|
456 | #endif /* __rtems__ */ |
---|
457 | |
---|
458 | default: |
---|
459 | panic("bpf_buf_free"); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | static int |
---|
464 | bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio) |
---|
465 | { |
---|
466 | |
---|
467 | if (d->bd_bufmode != BPF_BUFMODE_BUFFER) |
---|
468 | return (EOPNOTSUPP); |
---|
469 | return (bpf_buffer_uiomove(d, buf, len, uio)); |
---|
470 | } |
---|
471 | |
---|
472 | static int |
---|
473 | bpf_ioctl_sblen(struct bpf_d *d, u_int *i) |
---|
474 | { |
---|
475 | |
---|
476 | if (d->bd_bufmode != BPF_BUFMODE_BUFFER) |
---|
477 | return (EOPNOTSUPP); |
---|
478 | return (bpf_buffer_ioctl_sblen(d, i)); |
---|
479 | } |
---|
480 | |
---|
481 | static int |
---|
482 | bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i) |
---|
483 | { |
---|
484 | |
---|
485 | #ifndef __rtems__ |
---|
486 | if (d->bd_bufmode != BPF_BUFMODE_ZBUF) |
---|
487 | return (EOPNOTSUPP); |
---|
488 | return (bpf_zerocopy_ioctl_getzmax(td, d, i)); |
---|
489 | #else /* __rtems__ */ |
---|
490 | return (EOPNOTSUPP); |
---|
491 | #endif /* __rtems__ */ |
---|
492 | } |
---|
493 | |
---|
494 | static int |
---|
495 | bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz) |
---|
496 | { |
---|
497 | |
---|
498 | #ifndef __rtems__ |
---|
499 | if (d->bd_bufmode != BPF_BUFMODE_ZBUF) |
---|
500 | return (EOPNOTSUPP); |
---|
501 | return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz)); |
---|
502 | #else /* __rtems__ */ |
---|
503 | return (EOPNOTSUPP); |
---|
504 | #endif /* __rtems__ */ |
---|
505 | } |
---|
506 | |
---|
507 | static int |
---|
508 | bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz) |
---|
509 | { |
---|
510 | |
---|
511 | #ifndef __rtems__ |
---|
512 | if (d->bd_bufmode != BPF_BUFMODE_ZBUF) |
---|
513 | return (EOPNOTSUPP); |
---|
514 | return (bpf_zerocopy_ioctl_setzbuf(td, d, bz)); |
---|
515 | #else /* __rtems__ */ |
---|
516 | return (EOPNOTSUPP); |
---|
517 | #endif /* __rtems__ */ |
---|
518 | } |
---|
519 | |
---|
520 | /* |
---|
521 | * General BPF functions. |
---|
522 | */ |
---|
523 | static int |
---|
524 | bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp, |
---|
525 | struct sockaddr *sockp, int *hdrlen, struct bpf_d *d) |
---|
526 | { |
---|
527 | const struct ieee80211_bpf_params *p; |
---|
528 | struct ether_header *eh; |
---|
529 | struct mbuf *m; |
---|
530 | int error; |
---|
531 | int len; |
---|
532 | int hlen; |
---|
533 | int slen; |
---|
534 | |
---|
535 | /* |
---|
536 | * Build a sockaddr based on the data link layer type. |
---|
537 | * We do this at this level because the ethernet header |
---|
538 | * is copied directly into the data field of the sockaddr. |
---|
539 | * In the case of SLIP, there is no header and the packet |
---|
540 | * is forwarded as is. |
---|
541 | * Also, we are careful to leave room at the front of the mbuf |
---|
542 | * for the link level header. |
---|
543 | */ |
---|
544 | switch (linktype) { |
---|
545 | |
---|
546 | case DLT_SLIP: |
---|
547 | sockp->sa_family = AF_INET; |
---|
548 | hlen = 0; |
---|
549 | break; |
---|
550 | |
---|
551 | case DLT_EN10MB: |
---|
552 | sockp->sa_family = AF_UNSPEC; |
---|
553 | /* XXX Would MAXLINKHDR be better? */ |
---|
554 | hlen = ETHER_HDR_LEN; |
---|
555 | break; |
---|
556 | |
---|
557 | case DLT_FDDI: |
---|
558 | sockp->sa_family = AF_IMPLINK; |
---|
559 | hlen = 0; |
---|
560 | break; |
---|
561 | |
---|
562 | case DLT_RAW: |
---|
563 | sockp->sa_family = AF_UNSPEC; |
---|
564 | hlen = 0; |
---|
565 | break; |
---|
566 | |
---|
567 | case DLT_NULL: |
---|
568 | /* |
---|
569 | * null interface types require a 4 byte pseudo header which |
---|
570 | * corresponds to the address family of the packet. |
---|
571 | */ |
---|
572 | sockp->sa_family = AF_UNSPEC; |
---|
573 | hlen = 4; |
---|
574 | break; |
---|
575 | |
---|
576 | case DLT_ATM_RFC1483: |
---|
577 | /* |
---|
578 | * en atm driver requires 4-byte atm pseudo header. |
---|
579 | * though it isn't standard, vpi:vci needs to be |
---|
580 | * specified anyway. |
---|
581 | */ |
---|
582 | sockp->sa_family = AF_UNSPEC; |
---|
583 | hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */ |
---|
584 | break; |
---|
585 | |
---|
586 | case DLT_PPP: |
---|
587 | sockp->sa_family = AF_UNSPEC; |
---|
588 | hlen = 4; /* This should match PPP_HDRLEN */ |
---|
589 | break; |
---|
590 | |
---|
591 | case DLT_IEEE802_11: /* IEEE 802.11 wireless */ |
---|
592 | sockp->sa_family = AF_IEEE80211; |
---|
593 | hlen = 0; |
---|
594 | break; |
---|
595 | |
---|
596 | case DLT_IEEE802_11_RADIO: /* IEEE 802.11 wireless w/ phy params */ |
---|
597 | sockp->sa_family = AF_IEEE80211; |
---|
598 | sockp->sa_len = 12; /* XXX != 0 */ |
---|
599 | hlen = sizeof(struct ieee80211_bpf_params); |
---|
600 | break; |
---|
601 | |
---|
602 | default: |
---|
603 | return (EIO); |
---|
604 | } |
---|
605 | |
---|
606 | len = uio->uio_resid; |
---|
607 | if (len < hlen || len - hlen > ifp->if_mtu) |
---|
608 | return (EMSGSIZE); |
---|
609 | |
---|
610 | m = m_get2(len, M_WAITOK, MT_DATA, M_PKTHDR); |
---|
611 | if (m == NULL) |
---|
612 | return (EIO); |
---|
613 | m->m_pkthdr.len = m->m_len = len; |
---|
614 | *mp = m; |
---|
615 | |
---|
616 | error = uiomove(mtod(m, u_char *), len, uio); |
---|
617 | if (error) |
---|
618 | goto bad; |
---|
619 | |
---|
620 | slen = bpf_filter(d->bd_wfilter, mtod(m, u_char *), len, len); |
---|
621 | if (slen == 0) { |
---|
622 | error = EPERM; |
---|
623 | goto bad; |
---|
624 | } |
---|
625 | |
---|
626 | /* Check for multicast destination */ |
---|
627 | switch (linktype) { |
---|
628 | case DLT_EN10MB: |
---|
629 | eh = mtod(m, struct ether_header *); |
---|
630 | if (ETHER_IS_MULTICAST(eh->ether_dhost)) { |
---|
631 | if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost, |
---|
632 | ETHER_ADDR_LEN) == 0) |
---|
633 | m->m_flags |= M_BCAST; |
---|
634 | else |
---|
635 | m->m_flags |= M_MCAST; |
---|
636 | } |
---|
637 | if (d->bd_hdrcmplt == 0) { |
---|
638 | memcpy(eh->ether_shost, IF_LLADDR(ifp), |
---|
639 | sizeof(eh->ether_shost)); |
---|
640 | } |
---|
641 | break; |
---|
642 | } |
---|
643 | |
---|
644 | /* |
---|
645 | * Make room for link header, and copy it to sockaddr |
---|
646 | */ |
---|
647 | if (hlen != 0) { |
---|
648 | if (sockp->sa_family == AF_IEEE80211) { |
---|
649 | /* |
---|
650 | * Collect true length from the parameter header |
---|
651 | * NB: sockp is known to be zero'd so if we do a |
---|
652 | * short copy unspecified parameters will be |
---|
653 | * zero. |
---|
654 | * NB: packet may not be aligned after stripping |
---|
655 | * bpf params |
---|
656 | * XXX check ibp_vers |
---|
657 | */ |
---|
658 | p = mtod(m, const struct ieee80211_bpf_params *); |
---|
659 | hlen = p->ibp_len; |
---|
660 | if (hlen > sizeof(sockp->sa_data)) { |
---|
661 | error = EINVAL; |
---|
662 | goto bad; |
---|
663 | } |
---|
664 | } |
---|
665 | bcopy(mtod(m, const void *), sockp->sa_data, hlen); |
---|
666 | } |
---|
667 | *hdrlen = hlen; |
---|
668 | |
---|
669 | return (0); |
---|
670 | bad: |
---|
671 | m_freem(m); |
---|
672 | return (error); |
---|
673 | } |
---|
674 | |
---|
675 | /* |
---|
676 | * Attach file to the bpf interface, i.e. make d listen on bp. |
---|
677 | */ |
---|
678 | static void |
---|
679 | bpf_attachd(struct bpf_d *d, struct bpf_if *bp) |
---|
680 | { |
---|
681 | int op_w; |
---|
682 | |
---|
683 | BPF_LOCK_ASSERT(); |
---|
684 | |
---|
685 | /* |
---|
686 | * Save sysctl value to protect from sysctl change |
---|
687 | * between reads |
---|
688 | */ |
---|
689 | op_w = V_bpf_optimize_writers || d->bd_writer; |
---|
690 | |
---|
691 | if (d->bd_bif != NULL) |
---|
692 | bpf_detachd_locked(d); |
---|
693 | /* |
---|
694 | * Point d at bp, and add d to the interface's list. |
---|
695 | * Since there are many applications using BPF for |
---|
696 | * sending raw packets only (dhcpd, cdpd are good examples) |
---|
697 | * we can delay adding d to the list of active listeners until |
---|
698 | * some filter is configured. |
---|
699 | */ |
---|
700 | |
---|
701 | BPFIF_WLOCK(bp); |
---|
702 | BPFD_LOCK(d); |
---|
703 | |
---|
704 | d->bd_bif = bp; |
---|
705 | |
---|
706 | if (op_w != 0) { |
---|
707 | /* Add to writers-only list */ |
---|
708 | LIST_INSERT_HEAD(&bp->bif_wlist, d, bd_next); |
---|
709 | /* |
---|
710 | * We decrement bd_writer on every filter set operation. |
---|
711 | * First BIOCSETF is done by pcap_open_live() to set up |
---|
712 | * snap length. After that appliation usually sets its own filter |
---|
713 | */ |
---|
714 | d->bd_writer = 2; |
---|
715 | } else |
---|
716 | LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next); |
---|
717 | |
---|
718 | BPFD_UNLOCK(d); |
---|
719 | BPFIF_WUNLOCK(bp); |
---|
720 | |
---|
721 | bpf_bpfd_cnt++; |
---|
722 | |
---|
723 | CTR3(KTR_NET, "%s: bpf_attach called by pid %d, adding to %s list", |
---|
724 | __func__, d->bd_pid, d->bd_writer ? "writer" : "active"); |
---|
725 | |
---|
726 | if (op_w == 0) |
---|
727 | EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1); |
---|
728 | } |
---|
729 | |
---|
730 | /* |
---|
731 | * Check if we need to upgrade our descriptor @d from write-only mode. |
---|
732 | */ |
---|
733 | static int |
---|
734 | bpf_check_upgrade(u_long cmd, struct bpf_d *d, struct bpf_insn *fcode, int flen) |
---|
735 | { |
---|
736 | int is_snap, need_upgrade; |
---|
737 | |
---|
738 | /* |
---|
739 | * Check if we've already upgraded or new filter is empty. |
---|
740 | */ |
---|
741 | if (d->bd_writer == 0 || fcode == NULL) |
---|
742 | return (0); |
---|
743 | |
---|
744 | need_upgrade = 0; |
---|
745 | |
---|
746 | /* |
---|
747 | * Check if cmd looks like snaplen setting from |
---|
748 | * pcap_bpf.c:pcap_open_live(). |
---|
749 | * Note we're not checking .k value here: |
---|
750 | * while pcap_open_live() definitely sets to non-zero value, |
---|
751 | * we'd prefer to treat k=0 (deny ALL) case the same way: e.g. |
---|
752 | * do not consider upgrading immediately |
---|
753 | */ |
---|
754 | if (cmd == BIOCSETF && flen == 1 && fcode[0].code == (BPF_RET | BPF_K)) |
---|
755 | is_snap = 1; |
---|
756 | else |
---|
757 | is_snap = 0; |
---|
758 | |
---|
759 | if (is_snap == 0) { |
---|
760 | /* |
---|
761 | * We're setting first filter and it doesn't look like |
---|
762 | * setting snaplen. We're probably using bpf directly. |
---|
763 | * Upgrade immediately. |
---|
764 | */ |
---|
765 | need_upgrade = 1; |
---|
766 | } else { |
---|
767 | /* |
---|
768 | * Do not require upgrade by first BIOCSETF |
---|
769 | * (used to set snaplen) by pcap_open_live(). |
---|
770 | */ |
---|
771 | |
---|
772 | if (--d->bd_writer == 0) { |
---|
773 | /* |
---|
774 | * First snaplen filter has already |
---|
775 | * been set. This is probably catch-all |
---|
776 | * filter |
---|
777 | */ |
---|
778 | need_upgrade = 1; |
---|
779 | } |
---|
780 | } |
---|
781 | |
---|
782 | CTR5(KTR_NET, |
---|
783 | "%s: filter function set by pid %d, " |
---|
784 | "bd_writer counter %d, snap %d upgrade %d", |
---|
785 | __func__, d->bd_pid, d->bd_writer, |
---|
786 | is_snap, need_upgrade); |
---|
787 | |
---|
788 | return (need_upgrade); |
---|
789 | } |
---|
790 | |
---|
791 | /* |
---|
792 | * Add d to the list of active bp filters. |
---|
793 | * Requires bpf_attachd() to be called before. |
---|
794 | */ |
---|
795 | static void |
---|
796 | bpf_upgraded(struct bpf_d *d) |
---|
797 | { |
---|
798 | struct bpf_if *bp; |
---|
799 | |
---|
800 | BPF_LOCK_ASSERT(); |
---|
801 | |
---|
802 | bp = d->bd_bif; |
---|
803 | |
---|
804 | /* |
---|
805 | * Filter can be set several times without specifying interface. |
---|
806 | * Mark d as reader and exit. |
---|
807 | */ |
---|
808 | if (bp == NULL) { |
---|
809 | BPFD_LOCK(d); |
---|
810 | d->bd_writer = 0; |
---|
811 | BPFD_UNLOCK(d); |
---|
812 | return; |
---|
813 | } |
---|
814 | |
---|
815 | BPFIF_WLOCK(bp); |
---|
816 | BPFD_LOCK(d); |
---|
817 | |
---|
818 | /* Remove from writers-only list */ |
---|
819 | LIST_REMOVE(d, bd_next); |
---|
820 | LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next); |
---|
821 | /* Mark d as reader */ |
---|
822 | d->bd_writer = 0; |
---|
823 | |
---|
824 | BPFD_UNLOCK(d); |
---|
825 | BPFIF_WUNLOCK(bp); |
---|
826 | |
---|
827 | CTR2(KTR_NET, "%s: upgrade required by pid %d", __func__, d->bd_pid); |
---|
828 | |
---|
829 | EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1); |
---|
830 | } |
---|
831 | |
---|
832 | /* |
---|
833 | * Detach a file from its interface. |
---|
834 | */ |
---|
835 | static void |
---|
836 | bpf_detachd(struct bpf_d *d) |
---|
837 | { |
---|
838 | BPF_LOCK(); |
---|
839 | bpf_detachd_locked(d); |
---|
840 | BPF_UNLOCK(); |
---|
841 | } |
---|
842 | |
---|
843 | static void |
---|
844 | bpf_detachd_locked(struct bpf_d *d) |
---|
845 | { |
---|
846 | int error; |
---|
847 | struct bpf_if *bp; |
---|
848 | struct ifnet *ifp; |
---|
849 | |
---|
850 | CTR2(KTR_NET, "%s: detach required by pid %d", __func__, d->bd_pid); |
---|
851 | |
---|
852 | BPF_LOCK_ASSERT(); |
---|
853 | |
---|
854 | /* Check if descriptor is attached */ |
---|
855 | if ((bp = d->bd_bif) == NULL) |
---|
856 | return; |
---|
857 | |
---|
858 | BPFIF_WLOCK(bp); |
---|
859 | BPFD_LOCK(d); |
---|
860 | |
---|
861 | /* Save bd_writer value */ |
---|
862 | error = d->bd_writer; |
---|
863 | |
---|
864 | /* |
---|
865 | * Remove d from the interface's descriptor list. |
---|
866 | */ |
---|
867 | LIST_REMOVE(d, bd_next); |
---|
868 | |
---|
869 | ifp = bp->bif_ifp; |
---|
870 | d->bd_bif = NULL; |
---|
871 | BPFD_UNLOCK(d); |
---|
872 | BPFIF_WUNLOCK(bp); |
---|
873 | |
---|
874 | bpf_bpfd_cnt--; |
---|
875 | |
---|
876 | /* Call event handler iff d is attached */ |
---|
877 | if (error == 0) |
---|
878 | EVENTHANDLER_INVOKE(bpf_track, ifp, bp->bif_dlt, 0); |
---|
879 | |
---|
880 | /* |
---|
881 | * Check if this descriptor had requested promiscuous mode. |
---|
882 | * If so, turn it off. |
---|
883 | */ |
---|
884 | if (d->bd_promisc) { |
---|
885 | d->bd_promisc = 0; |
---|
886 | CURVNET_SET(ifp->if_vnet); |
---|
887 | error = ifpromisc(ifp, 0); |
---|
888 | CURVNET_RESTORE(); |
---|
889 | if (error != 0 && error != ENXIO) { |
---|
890 | /* |
---|
891 | * ENXIO can happen if a pccard is unplugged |
---|
892 | * Something is really wrong if we were able to put |
---|
893 | * the driver into promiscuous mode, but can't |
---|
894 | * take it out. |
---|
895 | */ |
---|
896 | if_printf(bp->bif_ifp, |
---|
897 | "bpf_detach: ifpromisc failed (%d)\n", error); |
---|
898 | } |
---|
899 | } |
---|
900 | } |
---|
901 | |
---|
902 | /* |
---|
903 | * Close the descriptor by detaching it from its interface, |
---|
904 | * deallocating its buffers, and marking it free. |
---|
905 | */ |
---|
906 | static void |
---|
907 | bpf_dtor(void *data) |
---|
908 | { |
---|
909 | struct bpf_d *d = data; |
---|
910 | |
---|
911 | BPFD_LOCK(d); |
---|
912 | if (d->bd_state == BPF_WAITING) |
---|
913 | callout_stop(&d->bd_callout); |
---|
914 | d->bd_state = BPF_IDLE; |
---|
915 | BPFD_UNLOCK(d); |
---|
916 | funsetown(&d->bd_sigio); |
---|
917 | bpf_detachd(d); |
---|
918 | #ifdef MAC |
---|
919 | mac_bpfdesc_destroy(d); |
---|
920 | #endif /* MAC */ |
---|
921 | seldrain(&d->bd_sel); |
---|
922 | knlist_destroy(&d->bd_sel.si_note); |
---|
923 | callout_drain(&d->bd_callout); |
---|
924 | bpf_freed(d); |
---|
925 | free(d, M_BPF); |
---|
926 | } |
---|
927 | |
---|
928 | /* |
---|
929 | * Open ethernet device. Returns ENXIO for illegal minor device number, |
---|
930 | * EBUSY if file is open by another process. |
---|
931 | */ |
---|
932 | /* ARGSUSED */ |
---|
933 | #ifndef __rtems__ |
---|
934 | static int |
---|
935 | #else /* __rtems__ */ |
---|
936 | static struct bpf_d * |
---|
937 | #endif /* __rtems__ */ |
---|
938 | bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td) |
---|
939 | { |
---|
940 | struct bpf_d *d; |
---|
941 | #ifndef __rtems__ |
---|
942 | int error; |
---|
943 | |
---|
944 | d = malloc(sizeof(*d), M_BPF, M_WAITOK | M_ZERO); |
---|
945 | error = devfs_set_cdevpriv(d, bpf_dtor); |
---|
946 | if (error != 0) { |
---|
947 | free(d, M_BPF); |
---|
948 | return (error); |
---|
949 | } |
---|
950 | #else /* __rtems__ */ |
---|
951 | d = malloc(sizeof(*d), M_BPF, M_NOWAIT | M_ZERO); |
---|
952 | if (d == NULL) { |
---|
953 | return (d); |
---|
954 | } |
---|
955 | #endif /* __rtems__ */ |
---|
956 | |
---|
957 | /* Setup counters */ |
---|
958 | d->bd_rcount = counter_u64_alloc(M_WAITOK); |
---|
959 | d->bd_dcount = counter_u64_alloc(M_WAITOK); |
---|
960 | d->bd_fcount = counter_u64_alloc(M_WAITOK); |
---|
961 | d->bd_wcount = counter_u64_alloc(M_WAITOK); |
---|
962 | d->bd_wfcount = counter_u64_alloc(M_WAITOK); |
---|
963 | d->bd_wdcount = counter_u64_alloc(M_WAITOK); |
---|
964 | d->bd_zcopy = counter_u64_alloc(M_WAITOK); |
---|
965 | |
---|
966 | /* |
---|
967 | * For historical reasons, perform a one-time initialization call to |
---|
968 | * the buffer routines, even though we're not yet committed to a |
---|
969 | * particular buffer method. |
---|
970 | */ |
---|
971 | bpf_buffer_init(d); |
---|
972 | if ((flags & FREAD) == 0) |
---|
973 | d->bd_writer = 2; |
---|
974 | d->bd_hbuf_in_use = 0; |
---|
975 | d->bd_bufmode = BPF_BUFMODE_BUFFER; |
---|
976 | d->bd_sig = SIGIO; |
---|
977 | d->bd_direction = BPF_D_INOUT; |
---|
978 | BPF_PID_REFRESH(d, td); |
---|
979 | #ifdef MAC |
---|
980 | mac_bpfdesc_init(d); |
---|
981 | mac_bpfdesc_create(td->td_ucred, d); |
---|
982 | #endif |
---|
983 | mtx_init(&d->bd_lock, devtoname(dev), "bpf cdev lock", MTX_DEF); |
---|
984 | callout_init_mtx(&d->bd_callout, &d->bd_lock, 0); |
---|
985 | knlist_init_mtx(&d->bd_sel.si_note, &d->bd_lock); |
---|
986 | |
---|
987 | #ifndef __rtems__ |
---|
988 | return (0); |
---|
989 | #else /* __rtems__ */ |
---|
990 | return (d); |
---|
991 | #endif /* __rtems__ */ |
---|
992 | } |
---|
993 | |
---|
994 | /* |
---|
995 | * bpfread - read next chunk of packets from buffers |
---|
996 | */ |
---|
997 | static int |
---|
998 | #ifndef __rtems__ |
---|
999 | bpfread(struct cdev *dev, struct uio *uio, int ioflag) |
---|
1000 | #else /* __rtems__ */ |
---|
1001 | bpfread(struct bpf_d *d, struct uio *uio, int ioflag) |
---|
1002 | #endif /* __rtems__ */ |
---|
1003 | { |
---|
1004 | #ifndef __rtems__ |
---|
1005 | struct bpf_d *d; |
---|
1006 | #endif /* __rtems__ */ |
---|
1007 | int error; |
---|
1008 | int non_block; |
---|
1009 | int timed_out; |
---|
1010 | |
---|
1011 | error = devfs_get_cdevpriv((void **)&d); |
---|
1012 | if (error != 0) |
---|
1013 | return (error); |
---|
1014 | |
---|
1015 | /* |
---|
1016 | * Restrict application to use a buffer the same size as |
---|
1017 | * as kernel buffers. |
---|
1018 | */ |
---|
1019 | if (uio->uio_resid != d->bd_bufsize) |
---|
1020 | return (EINVAL); |
---|
1021 | |
---|
1022 | non_block = ((ioflag & O_NONBLOCK) != 0); |
---|
1023 | |
---|
1024 | BPFD_LOCK(d); |
---|
1025 | BPF_PID_REFRESH_CUR(d); |
---|
1026 | if (d->bd_bufmode != BPF_BUFMODE_BUFFER) { |
---|
1027 | BPFD_UNLOCK(d); |
---|
1028 | return (EOPNOTSUPP); |
---|
1029 | } |
---|
1030 | if (d->bd_state == BPF_WAITING) |
---|
1031 | callout_stop(&d->bd_callout); |
---|
1032 | timed_out = (d->bd_state == BPF_TIMED_OUT); |
---|
1033 | d->bd_state = BPF_IDLE; |
---|
1034 | while (d->bd_hbuf_in_use) { |
---|
1035 | error = mtx_sleep(&d->bd_hbuf_in_use, &d->bd_lock, |
---|
1036 | PRINET|PCATCH, "bd_hbuf", 0); |
---|
1037 | if (error != 0) { |
---|
1038 | BPFD_UNLOCK(d); |
---|
1039 | return (error); |
---|
1040 | } |
---|
1041 | } |
---|
1042 | /* |
---|
1043 | * If the hold buffer is empty, then do a timed sleep, which |
---|
1044 | * ends when the timeout expires or when enough packets |
---|
1045 | * have arrived to fill the store buffer. |
---|
1046 | */ |
---|
1047 | while (d->bd_hbuf == NULL) { |
---|
1048 | if (d->bd_slen != 0) { |
---|
1049 | /* |
---|
1050 | * A packet(s) either arrived since the previous |
---|
1051 | * read or arrived while we were asleep. |
---|
1052 | */ |
---|
1053 | if (d->bd_immediate || non_block || timed_out) { |
---|
1054 | /* |
---|
1055 | * Rotate the buffers and return what's here |
---|
1056 | * if we are in immediate mode, non-blocking |
---|
1057 | * flag is set, or this descriptor timed out. |
---|
1058 | */ |
---|
1059 | ROTATE_BUFFERS(d); |
---|
1060 | break; |
---|
1061 | } |
---|
1062 | } |
---|
1063 | |
---|
1064 | /* |
---|
1065 | * No data is available, check to see if the bpf device |
---|
1066 | * is still pointed at a real interface. If not, return |
---|
1067 | * ENXIO so that the userland process knows to rebind |
---|
1068 | * it before using it again. |
---|
1069 | */ |
---|
1070 | if (d->bd_bif == NULL) { |
---|
1071 | BPFD_UNLOCK(d); |
---|
1072 | return (ENXIO); |
---|
1073 | } |
---|
1074 | |
---|
1075 | if (non_block) { |
---|
1076 | BPFD_UNLOCK(d); |
---|
1077 | return (EWOULDBLOCK); |
---|
1078 | } |
---|
1079 | error = msleep(d, &d->bd_lock, PRINET|PCATCH, |
---|
1080 | "bpf", d->bd_rtout); |
---|
1081 | if (error == EINTR || error == ERESTART) { |
---|
1082 | BPFD_UNLOCK(d); |
---|
1083 | return (error); |
---|
1084 | } |
---|
1085 | if (error == EWOULDBLOCK) { |
---|
1086 | /* |
---|
1087 | * On a timeout, return what's in the buffer, |
---|
1088 | * which may be nothing. If there is something |
---|
1089 | * in the store buffer, we can rotate the buffers. |
---|
1090 | */ |
---|
1091 | if (d->bd_hbuf) |
---|
1092 | /* |
---|
1093 | * We filled up the buffer in between |
---|
1094 | * getting the timeout and arriving |
---|
1095 | * here, so we don't need to rotate. |
---|
1096 | */ |
---|
1097 | break; |
---|
1098 | |
---|
1099 | if (d->bd_slen == 0) { |
---|
1100 | BPFD_UNLOCK(d); |
---|
1101 | return (0); |
---|
1102 | } |
---|
1103 | ROTATE_BUFFERS(d); |
---|
1104 | break; |
---|
1105 | } |
---|
1106 | } |
---|
1107 | /* |
---|
1108 | * At this point, we know we have something in the hold slot. |
---|
1109 | */ |
---|
1110 | d->bd_hbuf_in_use = 1; |
---|
1111 | BPFD_UNLOCK(d); |
---|
1112 | |
---|
1113 | /* |
---|
1114 | * Move data from hold buffer into user space. |
---|
1115 | * We know the entire buffer is transferred since |
---|
1116 | * we checked above that the read buffer is bpf_bufsize bytes. |
---|
1117 | * |
---|
1118 | * We do not have to worry about simultaneous reads because |
---|
1119 | * we waited for sole access to the hold buffer above. |
---|
1120 | */ |
---|
1121 | error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio); |
---|
1122 | |
---|
1123 | BPFD_LOCK(d); |
---|
1124 | KASSERT(d->bd_hbuf != NULL, ("bpfread: lost bd_hbuf")); |
---|
1125 | d->bd_fbuf = d->bd_hbuf; |
---|
1126 | d->bd_hbuf = NULL; |
---|
1127 | d->bd_hlen = 0; |
---|
1128 | bpf_buf_reclaimed(d); |
---|
1129 | d->bd_hbuf_in_use = 0; |
---|
1130 | wakeup(&d->bd_hbuf_in_use); |
---|
1131 | BPFD_UNLOCK(d); |
---|
1132 | |
---|
1133 | return (error); |
---|
1134 | } |
---|
1135 | |
---|
1136 | /* |
---|
1137 | * If there are processes sleeping on this descriptor, wake them up. |
---|
1138 | */ |
---|
1139 | static __inline void |
---|
1140 | bpf_wakeup(struct bpf_d *d) |
---|
1141 | { |
---|
1142 | |
---|
1143 | BPFD_LOCK_ASSERT(d); |
---|
1144 | if (d->bd_state == BPF_WAITING) { |
---|
1145 | callout_stop(&d->bd_callout); |
---|
1146 | d->bd_state = BPF_IDLE; |
---|
1147 | } |
---|
1148 | wakeup(d); |
---|
1149 | #ifndef __rtems__ |
---|
1150 | if (d->bd_async && d->bd_sig && d->bd_sigio) |
---|
1151 | pgsigio(&d->bd_sigio, d->bd_sig, 0); |
---|
1152 | #endif /* __rtems__ */ |
---|
1153 | |
---|
1154 | selwakeuppri(&d->bd_sel, PRINET); |
---|
1155 | KNOTE_LOCKED(&d->bd_sel.si_note, 0); |
---|
1156 | } |
---|
1157 | |
---|
1158 | static void |
---|
1159 | bpf_timed_out(void *arg) |
---|
1160 | { |
---|
1161 | struct bpf_d *d = (struct bpf_d *)arg; |
---|
1162 | |
---|
1163 | BPFD_LOCK_ASSERT(d); |
---|
1164 | |
---|
1165 | if (callout_pending(&d->bd_callout) || !callout_active(&d->bd_callout)) |
---|
1166 | return; |
---|
1167 | if (d->bd_state == BPF_WAITING) { |
---|
1168 | d->bd_state = BPF_TIMED_OUT; |
---|
1169 | if (d->bd_slen != 0) |
---|
1170 | bpf_wakeup(d); |
---|
1171 | } |
---|
1172 | } |
---|
1173 | |
---|
1174 | static int |
---|
1175 | bpf_ready(struct bpf_d *d) |
---|
1176 | { |
---|
1177 | |
---|
1178 | BPFD_LOCK_ASSERT(d); |
---|
1179 | |
---|
1180 | if (!bpf_canfreebuf(d) && d->bd_hlen != 0) |
---|
1181 | return (1); |
---|
1182 | if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) && |
---|
1183 | d->bd_slen != 0) |
---|
1184 | return (1); |
---|
1185 | return (0); |
---|
1186 | } |
---|
1187 | |
---|
1188 | static int |
---|
1189 | #ifndef __rtems__ |
---|
1190 | bpfwrite(struct cdev *dev, struct uio *uio, int ioflag) |
---|
1191 | #else /* __rtems__ */ |
---|
1192 | bpfwrite(struct bpf_d *d, struct uio *uio, int ioflag) |
---|
1193 | #endif /* __rtems__ */ |
---|
1194 | { |
---|
1195 | #ifndef __rtems__ |
---|
1196 | struct bpf_d *d; |
---|
1197 | #endif /* __rtems__ */ |
---|
1198 | struct ifnet *ifp; |
---|
1199 | struct mbuf *m, *mc; |
---|
1200 | struct sockaddr dst; |
---|
1201 | struct route ro; |
---|
1202 | int error, hlen; |
---|
1203 | |
---|
1204 | error = devfs_get_cdevpriv((void **)&d); |
---|
1205 | if (error != 0) |
---|
1206 | return (error); |
---|
1207 | |
---|
1208 | BPF_PID_REFRESH_CUR(d); |
---|
1209 | counter_u64_add(d->bd_wcount, 1); |
---|
1210 | /* XXX: locking required */ |
---|
1211 | if (d->bd_bif == NULL) { |
---|
1212 | counter_u64_add(d->bd_wdcount, 1); |
---|
1213 | return (ENXIO); |
---|
1214 | } |
---|
1215 | |
---|
1216 | ifp = d->bd_bif->bif_ifp; |
---|
1217 | |
---|
1218 | if ((ifp->if_flags & IFF_UP) == 0) { |
---|
1219 | counter_u64_add(d->bd_wdcount, 1); |
---|
1220 | return (ENETDOWN); |
---|
1221 | } |
---|
1222 | |
---|
1223 | if (uio->uio_resid == 0) { |
---|
1224 | counter_u64_add(d->bd_wdcount, 1); |
---|
1225 | return (0); |
---|
1226 | } |
---|
1227 | |
---|
1228 | bzero(&dst, sizeof(dst)); |
---|
1229 | m = NULL; |
---|
1230 | hlen = 0; |
---|
1231 | /* XXX: bpf_movein() can sleep */ |
---|
1232 | error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp, |
---|
1233 | &m, &dst, &hlen, d); |
---|
1234 | if (error) { |
---|
1235 | counter_u64_add(d->bd_wdcount, 1); |
---|
1236 | return (error); |
---|
1237 | } |
---|
1238 | counter_u64_add(d->bd_wfcount, 1); |
---|
1239 | if (d->bd_hdrcmplt) |
---|
1240 | dst.sa_family = pseudo_AF_HDRCMPLT; |
---|
1241 | |
---|
1242 | if (d->bd_feedback) { |
---|
1243 | mc = m_dup(m, M_NOWAIT); |
---|
1244 | if (mc != NULL) |
---|
1245 | mc->m_pkthdr.rcvif = ifp; |
---|
1246 | /* Set M_PROMISC for outgoing packets to be discarded. */ |
---|
1247 | if (d->bd_direction == BPF_D_INOUT) |
---|
1248 | m->m_flags |= M_PROMISC; |
---|
1249 | } else |
---|
1250 | mc = NULL; |
---|
1251 | |
---|
1252 | m->m_pkthdr.len -= hlen; |
---|
1253 | m->m_len -= hlen; |
---|
1254 | m->m_data += hlen; /* XXX */ |
---|
1255 | |
---|
1256 | CURVNET_SET(ifp->if_vnet); |
---|
1257 | #ifdef MAC |
---|
1258 | BPFD_LOCK(d); |
---|
1259 | mac_bpfdesc_create_mbuf(d, m); |
---|
1260 | if (mc != NULL) |
---|
1261 | mac_bpfdesc_create_mbuf(d, mc); |
---|
1262 | BPFD_UNLOCK(d); |
---|
1263 | #endif |
---|
1264 | |
---|
1265 | bzero(&ro, sizeof(ro)); |
---|
1266 | if (hlen != 0) { |
---|
1267 | ro.ro_prepend = (u_char *)&dst.sa_data; |
---|
1268 | ro.ro_plen = hlen; |
---|
1269 | ro.ro_flags = RT_HAS_HEADER; |
---|
1270 | } |
---|
1271 | |
---|
1272 | error = (*ifp->if_output)(ifp, m, &dst, &ro); |
---|
1273 | if (error) |
---|
1274 | counter_u64_add(d->bd_wdcount, 1); |
---|
1275 | |
---|
1276 | if (mc != NULL) { |
---|
1277 | if (error == 0) |
---|
1278 | (*ifp->if_input)(ifp, mc); |
---|
1279 | else |
---|
1280 | m_freem(mc); |
---|
1281 | } |
---|
1282 | CURVNET_RESTORE(); |
---|
1283 | |
---|
1284 | return (error); |
---|
1285 | } |
---|
1286 | |
---|
1287 | /* |
---|
1288 | * Reset a descriptor by flushing its packet buffer and clearing the receive |
---|
1289 | * and drop counts. This is doable for kernel-only buffers, but with |
---|
1290 | * zero-copy buffers, we can't write to (or rotate) buffers that are |
---|
1291 | * currently owned by userspace. It would be nice if we could encapsulate |
---|
1292 | * this logic in the buffer code rather than here. |
---|
1293 | */ |
---|
1294 | static void |
---|
1295 | reset_d(struct bpf_d *d) |
---|
1296 | { |
---|
1297 | |
---|
1298 | BPFD_LOCK_ASSERT(d); |
---|
1299 | |
---|
1300 | while (d->bd_hbuf_in_use) |
---|
1301 | mtx_sleep(&d->bd_hbuf_in_use, &d->bd_lock, PRINET, |
---|
1302 | "bd_hbuf", 0); |
---|
1303 | if ((d->bd_hbuf != NULL) && |
---|
1304 | (d->bd_bufmode != BPF_BUFMODE_ZBUF || bpf_canfreebuf(d))) { |
---|
1305 | /* Free the hold buffer. */ |
---|
1306 | d->bd_fbuf = d->bd_hbuf; |
---|
1307 | d->bd_hbuf = NULL; |
---|
1308 | d->bd_hlen = 0; |
---|
1309 | bpf_buf_reclaimed(d); |
---|
1310 | } |
---|
1311 | if (bpf_canwritebuf(d)) |
---|
1312 | d->bd_slen = 0; |
---|
1313 | counter_u64_zero(d->bd_rcount); |
---|
1314 | counter_u64_zero(d->bd_dcount); |
---|
1315 | counter_u64_zero(d->bd_fcount); |
---|
1316 | counter_u64_zero(d->bd_wcount); |
---|
1317 | counter_u64_zero(d->bd_wfcount); |
---|
1318 | counter_u64_zero(d->bd_wdcount); |
---|
1319 | counter_u64_zero(d->bd_zcopy); |
---|
1320 | } |
---|
1321 | |
---|
1322 | /* |
---|
1323 | * FIONREAD Check for read packet available. |
---|
1324 | * BIOCGBLEN Get buffer len [for read()]. |
---|
1325 | * BIOCSETF Set read filter. |
---|
1326 | * BIOCSETFNR Set read filter without resetting descriptor. |
---|
1327 | * BIOCSETWF Set write filter. |
---|
1328 | * BIOCFLUSH Flush read packet buffer. |
---|
1329 | * BIOCPROMISC Put interface into promiscuous mode. |
---|
1330 | * BIOCGDLT Get link layer type. |
---|
1331 | * BIOCGETIF Get interface name. |
---|
1332 | * BIOCSETIF Set interface. |
---|
1333 | * BIOCSRTIMEOUT Set read timeout. |
---|
1334 | * BIOCGRTIMEOUT Get read timeout. |
---|
1335 | * BIOCGSTATS Get packet stats. |
---|
1336 | * BIOCIMMEDIATE Set immediate mode. |
---|
1337 | * BIOCVERSION Get filter language version. |
---|
1338 | * BIOCGHDRCMPLT Get "header already complete" flag |
---|
1339 | * BIOCSHDRCMPLT Set "header already complete" flag |
---|
1340 | * BIOCGDIRECTION Get packet direction flag |
---|
1341 | * BIOCSDIRECTION Set packet direction flag |
---|
1342 | * BIOCGTSTAMP Get time stamp format and resolution. |
---|
1343 | * BIOCSTSTAMP Set time stamp format and resolution. |
---|
1344 | * BIOCLOCK Set "locked" flag |
---|
1345 | * BIOCFEEDBACK Set packet feedback mode. |
---|
1346 | * BIOCSETZBUF Set current zero-copy buffer locations. |
---|
1347 | * BIOCGETZMAX Get maximum zero-copy buffer size. |
---|
1348 | * BIOCROTZBUF Force rotation of zero-copy buffer |
---|
1349 | * BIOCSETBUFMODE Set buffer mode. |
---|
1350 | * BIOCGETBUFMODE Get current buffer mode. |
---|
1351 | */ |
---|
1352 | /* ARGSUSED */ |
---|
1353 | static int |
---|
1354 | #ifndef __rtems__ |
---|
1355 | bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, |
---|
1356 | struct thread *td) |
---|
1357 | #else /* __rtems__ */ |
---|
1358 | bpfioctl(struct bpf_d *d, u_long cmd, caddr_t addr, int flags, |
---|
1359 | struct thread *td) |
---|
1360 | #endif /* __rtems__ */ |
---|
1361 | { |
---|
1362 | #ifndef __rtems__ |
---|
1363 | struct bpf_d *d; |
---|
1364 | #endif /* __rtems__ */ |
---|
1365 | int error; |
---|
1366 | |
---|
1367 | error = devfs_get_cdevpriv((void **)&d); |
---|
1368 | if (error != 0) |
---|
1369 | return (error); |
---|
1370 | |
---|
1371 | /* |
---|
1372 | * Refresh PID associated with this descriptor. |
---|
1373 | */ |
---|
1374 | BPFD_LOCK(d); |
---|
1375 | BPF_PID_REFRESH(d, td); |
---|
1376 | if (d->bd_state == BPF_WAITING) |
---|
1377 | callout_stop(&d->bd_callout); |
---|
1378 | d->bd_state = BPF_IDLE; |
---|
1379 | BPFD_UNLOCK(d); |
---|
1380 | |
---|
1381 | if (d->bd_locked == 1) { |
---|
1382 | switch (cmd) { |
---|
1383 | case BIOCGBLEN: |
---|
1384 | case BIOCFLUSH: |
---|
1385 | case BIOCGDLT: |
---|
1386 | case BIOCGDLTLIST: |
---|
1387 | #ifdef COMPAT_FREEBSD32 |
---|
1388 | case BIOCGDLTLIST32: |
---|
1389 | #endif |
---|
1390 | case BIOCGETIF: |
---|
1391 | case BIOCGRTIMEOUT: |
---|
1392 | #if defined(COMPAT_FREEBSD32) && defined(__amd64__) |
---|
1393 | case BIOCGRTIMEOUT32: |
---|
1394 | #endif |
---|
1395 | case BIOCGSTATS: |
---|
1396 | case BIOCVERSION: |
---|
1397 | case BIOCGRSIG: |
---|
1398 | case BIOCGHDRCMPLT: |
---|
1399 | case BIOCSTSTAMP: |
---|
1400 | case BIOCFEEDBACK: |
---|
1401 | case FIONREAD: |
---|
1402 | case BIOCLOCK: |
---|
1403 | case BIOCSRTIMEOUT: |
---|
1404 | #if defined(COMPAT_FREEBSD32) && defined(__amd64__) |
---|
1405 | case BIOCSRTIMEOUT32: |
---|
1406 | #endif |
---|
1407 | case BIOCIMMEDIATE: |
---|
1408 | case TIOCGPGRP: |
---|
1409 | case BIOCROTZBUF: |
---|
1410 | break; |
---|
1411 | default: |
---|
1412 | return (EPERM); |
---|
1413 | } |
---|
1414 | } |
---|
1415 | #ifdef COMPAT_FREEBSD32 |
---|
1416 | /* |
---|
1417 | * If we see a 32-bit compat ioctl, mark the stream as 32-bit so |
---|
1418 | * that it will get 32-bit packet headers. |
---|
1419 | */ |
---|
1420 | switch (cmd) { |
---|
1421 | case BIOCSETF32: |
---|
1422 | case BIOCSETFNR32: |
---|
1423 | case BIOCSETWF32: |
---|
1424 | case BIOCGDLTLIST32: |
---|
1425 | case BIOCGRTIMEOUT32: |
---|
1426 | case BIOCSRTIMEOUT32: |
---|
1427 | if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { |
---|
1428 | BPFD_LOCK(d); |
---|
1429 | d->bd_compat32 = 1; |
---|
1430 | BPFD_UNLOCK(d); |
---|
1431 | } |
---|
1432 | } |
---|
1433 | #endif |
---|
1434 | |
---|
1435 | CURVNET_SET(TD_TO_VNET(td)); |
---|
1436 | switch (cmd) { |
---|
1437 | |
---|
1438 | default: |
---|
1439 | error = EINVAL; |
---|
1440 | break; |
---|
1441 | |
---|
1442 | /* |
---|
1443 | * Check for read packet available. |
---|
1444 | */ |
---|
1445 | case FIONREAD: |
---|
1446 | { |
---|
1447 | int n; |
---|
1448 | |
---|
1449 | BPFD_LOCK(d); |
---|
1450 | n = d->bd_slen; |
---|
1451 | while (d->bd_hbuf_in_use) |
---|
1452 | mtx_sleep(&d->bd_hbuf_in_use, &d->bd_lock, |
---|
1453 | PRINET, "bd_hbuf", 0); |
---|
1454 | if (d->bd_hbuf) |
---|
1455 | n += d->bd_hlen; |
---|
1456 | BPFD_UNLOCK(d); |
---|
1457 | |
---|
1458 | *(int *)addr = n; |
---|
1459 | break; |
---|
1460 | } |
---|
1461 | |
---|
1462 | /* |
---|
1463 | * Get buffer len [for read()]. |
---|
1464 | */ |
---|
1465 | case BIOCGBLEN: |
---|
1466 | BPFD_LOCK(d); |
---|
1467 | *(u_int *)addr = d->bd_bufsize; |
---|
1468 | BPFD_UNLOCK(d); |
---|
1469 | break; |
---|
1470 | |
---|
1471 | /* |
---|
1472 | * Set buffer length. |
---|
1473 | */ |
---|
1474 | case BIOCSBLEN: |
---|
1475 | error = bpf_ioctl_sblen(d, (u_int *)addr); |
---|
1476 | break; |
---|
1477 | |
---|
1478 | /* |
---|
1479 | * Set link layer read filter. |
---|
1480 | */ |
---|
1481 | case BIOCSETF: |
---|
1482 | case BIOCSETFNR: |
---|
1483 | case BIOCSETWF: |
---|
1484 | #ifdef COMPAT_FREEBSD32 |
---|
1485 | case BIOCSETF32: |
---|
1486 | case BIOCSETFNR32: |
---|
1487 | case BIOCSETWF32: |
---|
1488 | #endif |
---|
1489 | error = bpf_setf(d, (struct bpf_program *)addr, cmd); |
---|
1490 | break; |
---|
1491 | |
---|
1492 | /* |
---|
1493 | * Flush read packet buffer. |
---|
1494 | */ |
---|
1495 | case BIOCFLUSH: |
---|
1496 | BPFD_LOCK(d); |
---|
1497 | reset_d(d); |
---|
1498 | BPFD_UNLOCK(d); |
---|
1499 | break; |
---|
1500 | |
---|
1501 | /* |
---|
1502 | * Put interface into promiscuous mode. |
---|
1503 | */ |
---|
1504 | case BIOCPROMISC: |
---|
1505 | if (d->bd_bif == NULL) { |
---|
1506 | /* |
---|
1507 | * No interface attached yet. |
---|
1508 | */ |
---|
1509 | error = EINVAL; |
---|
1510 | break; |
---|
1511 | } |
---|
1512 | if (d->bd_promisc == 0) { |
---|
1513 | error = ifpromisc(d->bd_bif->bif_ifp, 1); |
---|
1514 | if (error == 0) |
---|
1515 | d->bd_promisc = 1; |
---|
1516 | } |
---|
1517 | break; |
---|
1518 | |
---|
1519 | /* |
---|
1520 | * Get current data link type. |
---|
1521 | */ |
---|
1522 | case BIOCGDLT: |
---|
1523 | BPF_LOCK(); |
---|
1524 | if (d->bd_bif == NULL) |
---|
1525 | error = EINVAL; |
---|
1526 | else |
---|
1527 | *(u_int *)addr = d->bd_bif->bif_dlt; |
---|
1528 | BPF_UNLOCK(); |
---|
1529 | break; |
---|
1530 | |
---|
1531 | /* |
---|
1532 | * Get a list of supported data link types. |
---|
1533 | */ |
---|
1534 | #ifdef COMPAT_FREEBSD32 |
---|
1535 | case BIOCGDLTLIST32: |
---|
1536 | { |
---|
1537 | struct bpf_dltlist32 *list32; |
---|
1538 | struct bpf_dltlist dltlist; |
---|
1539 | |
---|
1540 | list32 = (struct bpf_dltlist32 *)addr; |
---|
1541 | dltlist.bfl_len = list32->bfl_len; |
---|
1542 | dltlist.bfl_list = PTRIN(list32->bfl_list); |
---|
1543 | BPF_LOCK(); |
---|
1544 | if (d->bd_bif == NULL) |
---|
1545 | error = EINVAL; |
---|
1546 | else { |
---|
1547 | error = bpf_getdltlist(d, &dltlist); |
---|
1548 | if (error == 0) |
---|
1549 | list32->bfl_len = dltlist.bfl_len; |
---|
1550 | } |
---|
1551 | BPF_UNLOCK(); |
---|
1552 | break; |
---|
1553 | } |
---|
1554 | #endif |
---|
1555 | |
---|
1556 | case BIOCGDLTLIST: |
---|
1557 | BPF_LOCK(); |
---|
1558 | if (d->bd_bif == NULL) |
---|
1559 | error = EINVAL; |
---|
1560 | else |
---|
1561 | error = bpf_getdltlist(d, (struct bpf_dltlist *)addr); |
---|
1562 | BPF_UNLOCK(); |
---|
1563 | break; |
---|
1564 | |
---|
1565 | /* |
---|
1566 | * Set data link type. |
---|
1567 | */ |
---|
1568 | case BIOCSDLT: |
---|
1569 | BPF_LOCK(); |
---|
1570 | if (d->bd_bif == NULL) |
---|
1571 | error = EINVAL; |
---|
1572 | else |
---|
1573 | error = bpf_setdlt(d, *(u_int *)addr); |
---|
1574 | BPF_UNLOCK(); |
---|
1575 | break; |
---|
1576 | |
---|
1577 | /* |
---|
1578 | * Get interface name. |
---|
1579 | */ |
---|
1580 | case BIOCGETIF: |
---|
1581 | BPF_LOCK(); |
---|
1582 | if (d->bd_bif == NULL) |
---|
1583 | error = EINVAL; |
---|
1584 | else { |
---|
1585 | struct ifnet *const ifp = d->bd_bif->bif_ifp; |
---|
1586 | struct ifreq *const ifr = (struct ifreq *)addr; |
---|
1587 | |
---|
1588 | strlcpy(ifr->ifr_name, ifp->if_xname, |
---|
1589 | sizeof(ifr->ifr_name)); |
---|
1590 | } |
---|
1591 | BPF_UNLOCK(); |
---|
1592 | break; |
---|
1593 | |
---|
1594 | /* |
---|
1595 | * Set interface. |
---|
1596 | */ |
---|
1597 | case BIOCSETIF: |
---|
1598 | { |
---|
1599 | int alloc_buf, size; |
---|
1600 | |
---|
1601 | /* |
---|
1602 | * Behavior here depends on the buffering model. If |
---|
1603 | * we're using kernel memory buffers, then we can |
---|
1604 | * allocate them here. If we're using zero-copy, |
---|
1605 | * then the user process must have registered buffers |
---|
1606 | * by the time we get here. |
---|
1607 | */ |
---|
1608 | alloc_buf = 0; |
---|
1609 | BPFD_LOCK(d); |
---|
1610 | if (d->bd_bufmode == BPF_BUFMODE_BUFFER && |
---|
1611 | d->bd_sbuf == NULL) |
---|
1612 | alloc_buf = 1; |
---|
1613 | BPFD_UNLOCK(d); |
---|
1614 | if (alloc_buf) { |
---|
1615 | size = d->bd_bufsize; |
---|
1616 | error = bpf_buffer_ioctl_sblen(d, &size); |
---|
1617 | if (error != 0) |
---|
1618 | break; |
---|
1619 | } |
---|
1620 | BPF_LOCK(); |
---|
1621 | error = bpf_setif(d, (struct ifreq *)addr); |
---|
1622 | BPF_UNLOCK(); |
---|
1623 | break; |
---|
1624 | } |
---|
1625 | |
---|
1626 | /* |
---|
1627 | * Set read timeout. |
---|
1628 | */ |
---|
1629 | case BIOCSRTIMEOUT: |
---|
1630 | #if defined(COMPAT_FREEBSD32) && defined(__amd64__) |
---|
1631 | case BIOCSRTIMEOUT32: |
---|
1632 | #endif |
---|
1633 | { |
---|
1634 | struct timeval *tv = (struct timeval *)addr; |
---|
1635 | #if defined(COMPAT_FREEBSD32) && !defined(__mips__) |
---|
1636 | struct timeval32 *tv32; |
---|
1637 | struct timeval tv64; |
---|
1638 | |
---|
1639 | if (cmd == BIOCSRTIMEOUT32) { |
---|
1640 | tv32 = (struct timeval32 *)addr; |
---|
1641 | tv = &tv64; |
---|
1642 | tv->tv_sec = tv32->tv_sec; |
---|
1643 | tv->tv_usec = tv32->tv_usec; |
---|
1644 | } else |
---|
1645 | #endif |
---|
1646 | tv = (struct timeval *)addr; |
---|
1647 | |
---|
1648 | /* |
---|
1649 | * Subtract 1 tick from tvtohz() since this isn't |
---|
1650 | * a one-shot timer. |
---|
1651 | */ |
---|
1652 | if ((error = itimerfix(tv)) == 0) |
---|
1653 | d->bd_rtout = tvtohz(tv) - 1; |
---|
1654 | break; |
---|
1655 | } |
---|
1656 | |
---|
1657 | /* |
---|
1658 | * Get read timeout. |
---|
1659 | */ |
---|
1660 | case BIOCGRTIMEOUT: |
---|
1661 | #if defined(COMPAT_FREEBSD32) && defined(__amd64__) |
---|
1662 | case BIOCGRTIMEOUT32: |
---|
1663 | #endif |
---|
1664 | { |
---|
1665 | struct timeval *tv; |
---|
1666 | #if defined(COMPAT_FREEBSD32) && defined(__amd64__) |
---|
1667 | struct timeval32 *tv32; |
---|
1668 | struct timeval tv64; |
---|
1669 | |
---|
1670 | if (cmd == BIOCGRTIMEOUT32) |
---|
1671 | tv = &tv64; |
---|
1672 | else |
---|
1673 | #endif |
---|
1674 | tv = (struct timeval *)addr; |
---|
1675 | |
---|
1676 | tv->tv_sec = d->bd_rtout / hz; |
---|
1677 | tv->tv_usec = (d->bd_rtout % hz) * tick; |
---|
1678 | #if defined(COMPAT_FREEBSD32) && defined(__amd64__) |
---|
1679 | if (cmd == BIOCGRTIMEOUT32) { |
---|
1680 | tv32 = (struct timeval32 *)addr; |
---|
1681 | tv32->tv_sec = tv->tv_sec; |
---|
1682 | tv32->tv_usec = tv->tv_usec; |
---|
1683 | } |
---|
1684 | #endif |
---|
1685 | |
---|
1686 | break; |
---|
1687 | } |
---|
1688 | |
---|
1689 | /* |
---|
1690 | * Get packet stats. |
---|
1691 | */ |
---|
1692 | case BIOCGSTATS: |
---|
1693 | { |
---|
1694 | struct bpf_stat *bs = (struct bpf_stat *)addr; |
---|
1695 | |
---|
1696 | /* XXXCSJP overflow */ |
---|
1697 | bs->bs_recv = (u_int)counter_u64_fetch(d->bd_rcount); |
---|
1698 | bs->bs_drop = (u_int)counter_u64_fetch(d->bd_dcount); |
---|
1699 | break; |
---|
1700 | } |
---|
1701 | |
---|
1702 | /* |
---|
1703 | * Set immediate mode. |
---|
1704 | */ |
---|
1705 | case BIOCIMMEDIATE: |
---|
1706 | BPFD_LOCK(d); |
---|
1707 | d->bd_immediate = *(u_int *)addr; |
---|
1708 | BPFD_UNLOCK(d); |
---|
1709 | break; |
---|
1710 | |
---|
1711 | case BIOCVERSION: |
---|
1712 | { |
---|
1713 | struct bpf_version *bv = (struct bpf_version *)addr; |
---|
1714 | |
---|
1715 | bv->bv_major = BPF_MAJOR_VERSION; |
---|
1716 | bv->bv_minor = BPF_MINOR_VERSION; |
---|
1717 | break; |
---|
1718 | } |
---|
1719 | |
---|
1720 | /* |
---|
1721 | * Get "header already complete" flag |
---|
1722 | */ |
---|
1723 | case BIOCGHDRCMPLT: |
---|
1724 | BPFD_LOCK(d); |
---|
1725 | *(u_int *)addr = d->bd_hdrcmplt; |
---|
1726 | BPFD_UNLOCK(d); |
---|
1727 | break; |
---|
1728 | |
---|
1729 | /* |
---|
1730 | * Set "header already complete" flag |
---|
1731 | */ |
---|
1732 | case BIOCSHDRCMPLT: |
---|
1733 | BPFD_LOCK(d); |
---|
1734 | d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0; |
---|
1735 | BPFD_UNLOCK(d); |
---|
1736 | break; |
---|
1737 | |
---|
1738 | /* |
---|
1739 | * Get packet direction flag |
---|
1740 | */ |
---|
1741 | case BIOCGDIRECTION: |
---|
1742 | BPFD_LOCK(d); |
---|
1743 | *(u_int *)addr = d->bd_direction; |
---|
1744 | BPFD_UNLOCK(d); |
---|
1745 | break; |
---|
1746 | |
---|
1747 | /* |
---|
1748 | * Set packet direction flag |
---|
1749 | */ |
---|
1750 | case BIOCSDIRECTION: |
---|
1751 | { |
---|
1752 | u_int direction; |
---|
1753 | |
---|
1754 | direction = *(u_int *)addr; |
---|
1755 | switch (direction) { |
---|
1756 | case BPF_D_IN: |
---|
1757 | case BPF_D_INOUT: |
---|
1758 | case BPF_D_OUT: |
---|
1759 | BPFD_LOCK(d); |
---|
1760 | d->bd_direction = direction; |
---|
1761 | BPFD_UNLOCK(d); |
---|
1762 | break; |
---|
1763 | default: |
---|
1764 | error = EINVAL; |
---|
1765 | } |
---|
1766 | } |
---|
1767 | break; |
---|
1768 | |
---|
1769 | /* |
---|
1770 | * Get packet timestamp format and resolution. |
---|
1771 | */ |
---|
1772 | case BIOCGTSTAMP: |
---|
1773 | BPFD_LOCK(d); |
---|
1774 | *(u_int *)addr = d->bd_tstamp; |
---|
1775 | BPFD_UNLOCK(d); |
---|
1776 | break; |
---|
1777 | |
---|
1778 | /* |
---|
1779 | * Set packet timestamp format and resolution. |
---|
1780 | */ |
---|
1781 | case BIOCSTSTAMP: |
---|
1782 | { |
---|
1783 | u_int func; |
---|
1784 | |
---|
1785 | func = *(u_int *)addr; |
---|
1786 | if (BPF_T_VALID(func)) |
---|
1787 | d->bd_tstamp = func; |
---|
1788 | else |
---|
1789 | error = EINVAL; |
---|
1790 | } |
---|
1791 | break; |
---|
1792 | |
---|
1793 | case BIOCFEEDBACK: |
---|
1794 | BPFD_LOCK(d); |
---|
1795 | d->bd_feedback = *(u_int *)addr; |
---|
1796 | BPFD_UNLOCK(d); |
---|
1797 | break; |
---|
1798 | |
---|
1799 | case BIOCLOCK: |
---|
1800 | BPFD_LOCK(d); |
---|
1801 | d->bd_locked = 1; |
---|
1802 | BPFD_UNLOCK(d); |
---|
1803 | break; |
---|
1804 | |
---|
1805 | case FIONBIO: /* Non-blocking I/O */ |
---|
1806 | break; |
---|
1807 | |
---|
1808 | #ifndef __rtems__ |
---|
1809 | case FIOASYNC: /* Send signal on receive packets */ |
---|
1810 | BPFD_LOCK(d); |
---|
1811 | d->bd_async = *(int *)addr; |
---|
1812 | BPFD_UNLOCK(d); |
---|
1813 | break; |
---|
1814 | #endif /* __rtems__ */ |
---|
1815 | |
---|
1816 | case FIOSETOWN: |
---|
1817 | /* |
---|
1818 | * XXX: Add some sort of locking here? |
---|
1819 | * fsetown() can sleep. |
---|
1820 | */ |
---|
1821 | error = fsetown(*(int *)addr, &d->bd_sigio); |
---|
1822 | break; |
---|
1823 | |
---|
1824 | case FIOGETOWN: |
---|
1825 | BPFD_LOCK(d); |
---|
1826 | *(int *)addr = fgetown(&d->bd_sigio); |
---|
1827 | BPFD_UNLOCK(d); |
---|
1828 | break; |
---|
1829 | |
---|
1830 | /* This is deprecated, FIOSETOWN should be used instead. */ |
---|
1831 | case TIOCSPGRP: |
---|
1832 | error = fsetown(-(*(int *)addr), &d->bd_sigio); |
---|
1833 | break; |
---|
1834 | |
---|
1835 | /* This is deprecated, FIOGETOWN should be used instead. */ |
---|
1836 | case TIOCGPGRP: |
---|
1837 | *(int *)addr = -fgetown(&d->bd_sigio); |
---|
1838 | break; |
---|
1839 | |
---|
1840 | case BIOCSRSIG: /* Set receive signal */ |
---|
1841 | { |
---|
1842 | u_int sig; |
---|
1843 | |
---|
1844 | sig = *(u_int *)addr; |
---|
1845 | |
---|
1846 | if (sig >= NSIG) |
---|
1847 | error = EINVAL; |
---|
1848 | else { |
---|
1849 | BPFD_LOCK(d); |
---|
1850 | d->bd_sig = sig; |
---|
1851 | BPFD_UNLOCK(d); |
---|
1852 | } |
---|
1853 | break; |
---|
1854 | } |
---|
1855 | case BIOCGRSIG: |
---|
1856 | BPFD_LOCK(d); |
---|
1857 | *(u_int *)addr = d->bd_sig; |
---|
1858 | BPFD_UNLOCK(d); |
---|
1859 | break; |
---|
1860 | |
---|
1861 | case BIOCGETBUFMODE: |
---|
1862 | BPFD_LOCK(d); |
---|
1863 | *(u_int *)addr = d->bd_bufmode; |
---|
1864 | BPFD_UNLOCK(d); |
---|
1865 | break; |
---|
1866 | |
---|
1867 | case BIOCSETBUFMODE: |
---|
1868 | /* |
---|
1869 | * Allow the buffering mode to be changed as long as we |
---|
1870 | * haven't yet committed to a particular mode. Our |
---|
1871 | * definition of commitment, for now, is whether or not a |
---|
1872 | * buffer has been allocated or an interface attached, since |
---|
1873 | * that's the point where things get tricky. |
---|
1874 | */ |
---|
1875 | switch (*(u_int *)addr) { |
---|
1876 | case BPF_BUFMODE_BUFFER: |
---|
1877 | break; |
---|
1878 | |
---|
1879 | #ifndef __rtems__ |
---|
1880 | case BPF_BUFMODE_ZBUF: |
---|
1881 | if (bpf_zerocopy_enable) |
---|
1882 | break; |
---|
1883 | /* FALLSTHROUGH */ |
---|
1884 | #endif /* __rtems__ */ |
---|
1885 | |
---|
1886 | default: |
---|
1887 | CURVNET_RESTORE(); |
---|
1888 | return (EINVAL); |
---|
1889 | } |
---|
1890 | |
---|
1891 | BPFD_LOCK(d); |
---|
1892 | if (d->bd_sbuf != NULL || d->bd_hbuf != NULL || |
---|
1893 | d->bd_fbuf != NULL || d->bd_bif != NULL) { |
---|
1894 | BPFD_UNLOCK(d); |
---|
1895 | CURVNET_RESTORE(); |
---|
1896 | return (EBUSY); |
---|
1897 | } |
---|
1898 | d->bd_bufmode = *(u_int *)addr; |
---|
1899 | BPFD_UNLOCK(d); |
---|
1900 | break; |
---|
1901 | |
---|
1902 | case BIOCGETZMAX: |
---|
1903 | error = bpf_ioctl_getzmax(td, d, (size_t *)addr); |
---|
1904 | break; |
---|
1905 | |
---|
1906 | case BIOCSETZBUF: |
---|
1907 | error = bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr); |
---|
1908 | break; |
---|
1909 | |
---|
1910 | case BIOCROTZBUF: |
---|
1911 | error = bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr); |
---|
1912 | break; |
---|
1913 | } |
---|
1914 | CURVNET_RESTORE(); |
---|
1915 | return (error); |
---|
1916 | } |
---|
1917 | |
---|
1918 | /* |
---|
1919 | * Set d's packet filter program to fp. If this file already has a filter, |
---|
1920 | * free it and replace it. Returns EINVAL for bogus requests. |
---|
1921 | * |
---|
1922 | * Note we need global lock here to serialize bpf_setf() and bpf_setif() calls |
---|
1923 | * since reading d->bd_bif can't be protected by d or interface lock due to |
---|
1924 | * lock order. |
---|
1925 | * |
---|
1926 | * Additionally, we have to acquire interface write lock due to bpf_mtap() uses |
---|
1927 | * interface read lock to read all filers. |
---|
1928 | * |
---|
1929 | */ |
---|
1930 | static int |
---|
1931 | bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd) |
---|
1932 | { |
---|
1933 | #ifdef COMPAT_FREEBSD32 |
---|
1934 | struct bpf_program fp_swab; |
---|
1935 | struct bpf_program32 *fp32; |
---|
1936 | #endif |
---|
1937 | struct bpf_insn *fcode, *old; |
---|
1938 | #ifdef BPF_JITTER |
---|
1939 | bpf_jit_filter *jfunc, *ofunc; |
---|
1940 | #endif |
---|
1941 | size_t size; |
---|
1942 | u_int flen; |
---|
1943 | int need_upgrade; |
---|
1944 | |
---|
1945 | #ifdef COMPAT_FREEBSD32 |
---|
1946 | switch (cmd) { |
---|
1947 | case BIOCSETF32: |
---|
1948 | case BIOCSETWF32: |
---|
1949 | case BIOCSETFNR32: |
---|
1950 | fp32 = (struct bpf_program32 *)fp; |
---|
1951 | fp_swab.bf_len = fp32->bf_len; |
---|
1952 | fp_swab.bf_insns = (struct bpf_insn *)(uintptr_t)fp32->bf_insns; |
---|
1953 | fp = &fp_swab; |
---|
1954 | switch (cmd) { |
---|
1955 | case BIOCSETF32: |
---|
1956 | cmd = BIOCSETF; |
---|
1957 | break; |
---|
1958 | case BIOCSETWF32: |
---|
1959 | cmd = BIOCSETWF; |
---|
1960 | break; |
---|
1961 | } |
---|
1962 | break; |
---|
1963 | } |
---|
1964 | #endif |
---|
1965 | |
---|
1966 | fcode = NULL; |
---|
1967 | #ifdef BPF_JITTER |
---|
1968 | jfunc = ofunc = NULL; |
---|
1969 | #endif |
---|
1970 | need_upgrade = 0; |
---|
1971 | |
---|
1972 | /* |
---|
1973 | * Check new filter validness before acquiring any locks. |
---|
1974 | * Allocate memory for new filter, if needed. |
---|
1975 | */ |
---|
1976 | flen = fp->bf_len; |
---|
1977 | if (flen > bpf_maxinsns || (fp->bf_insns == NULL && flen != 0)) |
---|
1978 | return (EINVAL); |
---|
1979 | size = flen * sizeof(*fp->bf_insns); |
---|
1980 | if (size > 0) { |
---|
1981 | /* We're setting up new filter. Copy and check actual data. */ |
---|
1982 | fcode = malloc(size, M_BPF, M_WAITOK); |
---|
1983 | if (copyin(fp->bf_insns, fcode, size) != 0 || |
---|
1984 | !bpf_validate(fcode, flen)) { |
---|
1985 | free(fcode, M_BPF); |
---|
1986 | return (EINVAL); |
---|
1987 | } |
---|
1988 | #ifdef BPF_JITTER |
---|
1989 | if (cmd != BIOCSETWF) { |
---|
1990 | /* |
---|
1991 | * Filter is copied inside fcode and is |
---|
1992 | * perfectly valid. |
---|
1993 | */ |
---|
1994 | jfunc = bpf_jitter(fcode, flen); |
---|
1995 | } |
---|
1996 | #endif |
---|
1997 | } |
---|
1998 | |
---|
1999 | BPF_LOCK(); |
---|
2000 | |
---|
2001 | /* |
---|
2002 | * Set up new filter. |
---|
2003 | * Protect filter change by interface lock. |
---|
2004 | * Additionally, we are protected by global lock here. |
---|
2005 | */ |
---|
2006 | if (d->bd_bif != NULL) |
---|
2007 | BPFIF_WLOCK(d->bd_bif); |
---|
2008 | BPFD_LOCK(d); |
---|
2009 | if (cmd == BIOCSETWF) { |
---|
2010 | old = d->bd_wfilter; |
---|
2011 | d->bd_wfilter = fcode; |
---|
2012 | } else { |
---|
2013 | old = d->bd_rfilter; |
---|
2014 | d->bd_rfilter = fcode; |
---|
2015 | #ifdef BPF_JITTER |
---|
2016 | ofunc = d->bd_bfilter; |
---|
2017 | d->bd_bfilter = jfunc; |
---|
2018 | #endif |
---|
2019 | if (cmd == BIOCSETF) |
---|
2020 | reset_d(d); |
---|
2021 | |
---|
2022 | need_upgrade = bpf_check_upgrade(cmd, d, fcode, flen); |
---|
2023 | } |
---|
2024 | BPFD_UNLOCK(d); |
---|
2025 | if (d->bd_bif != NULL) |
---|
2026 | BPFIF_WUNLOCK(d->bd_bif); |
---|
2027 | if (old != NULL) |
---|
2028 | free(old, M_BPF); |
---|
2029 | #ifdef BPF_JITTER |
---|
2030 | if (ofunc != NULL) |
---|
2031 | bpf_destroy_jit_filter(ofunc); |
---|
2032 | #endif |
---|
2033 | |
---|
2034 | /* Move d to active readers list. */ |
---|
2035 | if (need_upgrade != 0) |
---|
2036 | bpf_upgraded(d); |
---|
2037 | |
---|
2038 | BPF_UNLOCK(); |
---|
2039 | return (0); |
---|
2040 | } |
---|
2041 | |
---|
2042 | /* |
---|
2043 | * Detach a file from its current interface (if attached at all) and attach |
---|
2044 | * to the interface indicated by the name stored in ifr. |
---|
2045 | * Return an errno or 0. |
---|
2046 | */ |
---|
2047 | static int |
---|
2048 | bpf_setif(struct bpf_d *d, struct ifreq *ifr) |
---|
2049 | { |
---|
2050 | struct bpf_if *bp; |
---|
2051 | struct ifnet *theywant; |
---|
2052 | |
---|
2053 | BPF_LOCK_ASSERT(); |
---|
2054 | |
---|
2055 | theywant = ifunit(ifr->ifr_name); |
---|
2056 | if (theywant == NULL || theywant->if_bpf == NULL) |
---|
2057 | return (ENXIO); |
---|
2058 | |
---|
2059 | bp = theywant->if_bpf; |
---|
2060 | |
---|
2061 | /* Check if interface is not being detached from BPF */ |
---|
2062 | BPFIF_RLOCK(bp); |
---|
2063 | if (bp->bif_flags & BPFIF_FLAG_DYING) { |
---|
2064 | BPFIF_RUNLOCK(bp); |
---|
2065 | return (ENXIO); |
---|
2066 | } |
---|
2067 | BPFIF_RUNLOCK(bp); |
---|
2068 | |
---|
2069 | /* |
---|
2070 | * At this point, we expect the buffer is already allocated. If not, |
---|
2071 | * return an error. |
---|
2072 | */ |
---|
2073 | switch (d->bd_bufmode) { |
---|
2074 | case BPF_BUFMODE_BUFFER: |
---|
2075 | #ifndef __rtems__ |
---|
2076 | case BPF_BUFMODE_ZBUF: |
---|
2077 | #endif /* __rtems__ */ |
---|
2078 | if (d->bd_sbuf == NULL) |
---|
2079 | return (EINVAL); |
---|
2080 | break; |
---|
2081 | |
---|
2082 | default: |
---|
2083 | panic("bpf_setif: bufmode %d", d->bd_bufmode); |
---|
2084 | } |
---|
2085 | if (bp != d->bd_bif) |
---|
2086 | bpf_attachd(d, bp); |
---|
2087 | BPFD_LOCK(d); |
---|
2088 | reset_d(d); |
---|
2089 | BPFD_UNLOCK(d); |
---|
2090 | return (0); |
---|
2091 | } |
---|
2092 | |
---|
2093 | /* |
---|
2094 | * Support for select() and poll() system calls |
---|
2095 | * |
---|
2096 | * Return true iff the specific operation will not block indefinitely. |
---|
2097 | * Otherwise, return false but make a note that a selwakeup() must be done. |
---|
2098 | */ |
---|
2099 | static int |
---|
2100 | #ifndef __rtems__ |
---|
2101 | bpfpoll(struct cdev *dev, int events, struct thread *td) |
---|
2102 | #else /* __rtems__ */ |
---|
2103 | bpfpoll(struct bpf_d *d, int events, struct thread *td) |
---|
2104 | #endif /* __rtems__ */ |
---|
2105 | { |
---|
2106 | #ifndef __rtems__ |
---|
2107 | struct bpf_d *d; |
---|
2108 | #endif /* __rtems__ */ |
---|
2109 | int revents; |
---|
2110 | |
---|
2111 | if (devfs_get_cdevpriv((void **)&d) != 0 || d->bd_bif == NULL) |
---|
2112 | return (events & |
---|
2113 | (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); |
---|
2114 | |
---|
2115 | /* |
---|
2116 | * Refresh PID associated with this descriptor. |
---|
2117 | */ |
---|
2118 | revents = events & (POLLOUT | POLLWRNORM); |
---|
2119 | BPFD_LOCK(d); |
---|
2120 | BPF_PID_REFRESH(d, td); |
---|
2121 | if (events & (POLLIN | POLLRDNORM)) { |
---|
2122 | if (bpf_ready(d)) |
---|
2123 | revents |= events & (POLLIN | POLLRDNORM); |
---|
2124 | else { |
---|
2125 | selrecord(td, &d->bd_sel); |
---|
2126 | /* Start the read timeout if necessary. */ |
---|
2127 | if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { |
---|
2128 | callout_reset(&d->bd_callout, d->bd_rtout, |
---|
2129 | bpf_timed_out, d); |
---|
2130 | d->bd_state = BPF_WAITING; |
---|
2131 | } |
---|
2132 | } |
---|
2133 | } |
---|
2134 | BPFD_UNLOCK(d); |
---|
2135 | return (revents); |
---|
2136 | } |
---|
2137 | |
---|
2138 | /* |
---|
2139 | * Support for kevent() system call. Register EVFILT_READ filters and |
---|
2140 | * reject all others. |
---|
2141 | */ |
---|
2142 | #ifdef __rtems__ |
---|
2143 | static |
---|
2144 | #endif /* __rtems__ */ |
---|
2145 | int |
---|
2146 | #ifndef __rtems__ |
---|
2147 | bpfkqfilter(struct cdev *dev, struct knote *kn) |
---|
2148 | #else /* __rtems__ */ |
---|
2149 | bpfkqfilter(struct bpf_d *d, struct knote *kn) |
---|
2150 | #endif /* __rtems__ */ |
---|
2151 | { |
---|
2152 | #ifndef __rtems__ |
---|
2153 | struct bpf_d *d; |
---|
2154 | #endif /* __rtems__ */ |
---|
2155 | |
---|
2156 | if (devfs_get_cdevpriv((void **)&d) != 0 || |
---|
2157 | kn->kn_filter != EVFILT_READ) |
---|
2158 | return (1); |
---|
2159 | |
---|
2160 | /* |
---|
2161 | * Refresh PID associated with this descriptor. |
---|
2162 | */ |
---|
2163 | BPFD_LOCK(d); |
---|
2164 | BPF_PID_REFRESH_CUR(d); |
---|
2165 | kn->kn_fop = &bpfread_filtops; |
---|
2166 | kn->kn_hook = d; |
---|
2167 | knlist_add(&d->bd_sel.si_note, kn, 1); |
---|
2168 | BPFD_UNLOCK(d); |
---|
2169 | |
---|
2170 | return (0); |
---|
2171 | } |
---|
2172 | |
---|
2173 | static void |
---|
2174 | filt_bpfdetach(struct knote *kn) |
---|
2175 | { |
---|
2176 | struct bpf_d *d = (struct bpf_d *)kn->kn_hook; |
---|
2177 | |
---|
2178 | knlist_remove(&d->bd_sel.si_note, kn, 0); |
---|
2179 | } |
---|
2180 | |
---|
2181 | static int |
---|
2182 | filt_bpfread(struct knote *kn, long hint) |
---|
2183 | { |
---|
2184 | struct bpf_d *d = (struct bpf_d *)kn->kn_hook; |
---|
2185 | int ready; |
---|
2186 | |
---|
2187 | BPFD_LOCK_ASSERT(d); |
---|
2188 | ready = bpf_ready(d); |
---|
2189 | if (ready) { |
---|
2190 | kn->kn_data = d->bd_slen; |
---|
2191 | /* |
---|
2192 | * Ignore the hold buffer if it is being copied to user space. |
---|
2193 | */ |
---|
2194 | if (!d->bd_hbuf_in_use && d->bd_hbuf) |
---|
2195 | kn->kn_data += d->bd_hlen; |
---|
2196 | } else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { |
---|
2197 | callout_reset(&d->bd_callout, d->bd_rtout, |
---|
2198 | bpf_timed_out, d); |
---|
2199 | d->bd_state = BPF_WAITING; |
---|
2200 | } |
---|
2201 | |
---|
2202 | return (ready); |
---|
2203 | } |
---|
2204 | |
---|
2205 | #define BPF_TSTAMP_NONE 0 |
---|
2206 | #define BPF_TSTAMP_FAST 1 |
---|
2207 | #define BPF_TSTAMP_NORMAL 2 |
---|
2208 | #define BPF_TSTAMP_EXTERN 3 |
---|
2209 | |
---|
2210 | static int |
---|
2211 | bpf_ts_quality(int tstype) |
---|
2212 | { |
---|
2213 | |
---|
2214 | if (tstype == BPF_T_NONE) |
---|
2215 | return (BPF_TSTAMP_NONE); |
---|
2216 | if ((tstype & BPF_T_FAST) != 0) |
---|
2217 | return (BPF_TSTAMP_FAST); |
---|
2218 | |
---|
2219 | return (BPF_TSTAMP_NORMAL); |
---|
2220 | } |
---|
2221 | |
---|
2222 | static int |
---|
2223 | bpf_gettime(struct bintime *bt, int tstype, struct mbuf *m) |
---|
2224 | { |
---|
2225 | struct m_tag *tag; |
---|
2226 | int quality; |
---|
2227 | |
---|
2228 | quality = bpf_ts_quality(tstype); |
---|
2229 | if (quality == BPF_TSTAMP_NONE) |
---|
2230 | return (quality); |
---|
2231 | |
---|
2232 | if (m != NULL) { |
---|
2233 | tag = m_tag_locate(m, MTAG_BPF, MTAG_BPF_TIMESTAMP, NULL); |
---|
2234 | if (tag != NULL) { |
---|
2235 | *bt = *(struct bintime *)(tag + 1); |
---|
2236 | return (BPF_TSTAMP_EXTERN); |
---|
2237 | } |
---|
2238 | } |
---|
2239 | if (quality == BPF_TSTAMP_NORMAL) |
---|
2240 | binuptime(bt); |
---|
2241 | else |
---|
2242 | getbinuptime(bt); |
---|
2243 | |
---|
2244 | return (quality); |
---|
2245 | } |
---|
2246 | |
---|
2247 | /* |
---|
2248 | * Incoming linkage from device drivers. Process the packet pkt, of length |
---|
2249 | * pktlen, which is stored in a contiguous buffer. The packet is parsed |
---|
2250 | * by each process' filter, and if accepted, stashed into the corresponding |
---|
2251 | * buffer. |
---|
2252 | */ |
---|
2253 | void |
---|
2254 | bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) |
---|
2255 | { |
---|
2256 | struct bintime bt; |
---|
2257 | struct bpf_d *d; |
---|
2258 | #ifdef BPF_JITTER |
---|
2259 | bpf_jit_filter *bf; |
---|
2260 | #endif |
---|
2261 | u_int slen; |
---|
2262 | int gottime; |
---|
2263 | |
---|
2264 | gottime = BPF_TSTAMP_NONE; |
---|
2265 | |
---|
2266 | BPFIF_RLOCK(bp); |
---|
2267 | |
---|
2268 | LIST_FOREACH(d, &bp->bif_dlist, bd_next) { |
---|
2269 | /* |
---|
2270 | * We are not using any locks for d here because: |
---|
2271 | * 1) any filter change is protected by interface |
---|
2272 | * write lock |
---|
2273 | * 2) destroying/detaching d is protected by interface |
---|
2274 | * write lock, too |
---|
2275 | */ |
---|
2276 | |
---|
2277 | counter_u64_add(d->bd_rcount, 1); |
---|
2278 | /* |
---|
2279 | * NB: We dont call BPF_CHECK_DIRECTION() here since there is no |
---|
2280 | * way for the caller to indiciate to us whether this packet |
---|
2281 | * is inbound or outbound. In the bpf_mtap() routines, we use |
---|
2282 | * the interface pointers on the mbuf to figure it out. |
---|
2283 | */ |
---|
2284 | #ifdef BPF_JITTER |
---|
2285 | bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL; |
---|
2286 | if (bf != NULL) |
---|
2287 | slen = (*(bf->func))(pkt, pktlen, pktlen); |
---|
2288 | else |
---|
2289 | #endif |
---|
2290 | slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen); |
---|
2291 | if (slen != 0) { |
---|
2292 | /* |
---|
2293 | * Filter matches. Let's to acquire write lock. |
---|
2294 | */ |
---|
2295 | BPFD_LOCK(d); |
---|
2296 | |
---|
2297 | counter_u64_add(d->bd_fcount, 1); |
---|
2298 | if (gottime < bpf_ts_quality(d->bd_tstamp)) |
---|
2299 | gottime = bpf_gettime(&bt, d->bd_tstamp, NULL); |
---|
2300 | #ifdef MAC |
---|
2301 | if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0) |
---|
2302 | #endif |
---|
2303 | catchpacket(d, pkt, pktlen, slen, |
---|
2304 | bpf_append_bytes, &bt); |
---|
2305 | BPFD_UNLOCK(d); |
---|
2306 | } |
---|
2307 | } |
---|
2308 | BPFIF_RUNLOCK(bp); |
---|
2309 | } |
---|
2310 | |
---|
2311 | #define BPF_CHECK_DIRECTION(d, r, i) \ |
---|
2312 | (((d)->bd_direction == BPF_D_IN && (r) != (i)) || \ |
---|
2313 | ((d)->bd_direction == BPF_D_OUT && (r) == (i))) |
---|
2314 | |
---|
2315 | /* |
---|
2316 | * Incoming linkage from device drivers, when packet is in an mbuf chain. |
---|
2317 | * Locking model is explained in bpf_tap(). |
---|
2318 | */ |
---|
2319 | void |
---|
2320 | bpf_mtap(struct bpf_if *bp, struct mbuf *m) |
---|
2321 | { |
---|
2322 | struct bintime bt; |
---|
2323 | struct bpf_d *d; |
---|
2324 | #ifdef BPF_JITTER |
---|
2325 | bpf_jit_filter *bf; |
---|
2326 | #endif |
---|
2327 | u_int pktlen, slen; |
---|
2328 | int gottime; |
---|
2329 | |
---|
2330 | /* Skip outgoing duplicate packets. */ |
---|
2331 | if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) { |
---|
2332 | m->m_flags &= ~M_PROMISC; |
---|
2333 | return; |
---|
2334 | } |
---|
2335 | |
---|
2336 | pktlen = m_length(m, NULL); |
---|
2337 | gottime = BPF_TSTAMP_NONE; |
---|
2338 | |
---|
2339 | BPFIF_RLOCK(bp); |
---|
2340 | |
---|
2341 | LIST_FOREACH(d, &bp->bif_dlist, bd_next) { |
---|
2342 | if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp)) |
---|
2343 | continue; |
---|
2344 | counter_u64_add(d->bd_rcount, 1); |
---|
2345 | #ifdef BPF_JITTER |
---|
2346 | bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL; |
---|
2347 | /* XXX We cannot handle multiple mbufs. */ |
---|
2348 | if (bf != NULL && m->m_next == NULL) |
---|
2349 | slen = (*(bf->func))(mtod(m, u_char *), pktlen, pktlen); |
---|
2350 | else |
---|
2351 | #endif |
---|
2352 | slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0); |
---|
2353 | if (slen != 0) { |
---|
2354 | BPFD_LOCK(d); |
---|
2355 | |
---|
2356 | counter_u64_add(d->bd_fcount, 1); |
---|
2357 | if (gottime < bpf_ts_quality(d->bd_tstamp)) |
---|
2358 | gottime = bpf_gettime(&bt, d->bd_tstamp, m); |
---|
2359 | #ifdef MAC |
---|
2360 | if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0) |
---|
2361 | #endif |
---|
2362 | catchpacket(d, (u_char *)m, pktlen, slen, |
---|
2363 | bpf_append_mbuf, &bt); |
---|
2364 | BPFD_UNLOCK(d); |
---|
2365 | } |
---|
2366 | } |
---|
2367 | BPFIF_RUNLOCK(bp); |
---|
2368 | } |
---|
2369 | |
---|
2370 | /* |
---|
2371 | * Incoming linkage from device drivers, when packet is in |
---|
2372 | * an mbuf chain and to be prepended by a contiguous header. |
---|
2373 | */ |
---|
2374 | void |
---|
2375 | bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m) |
---|
2376 | { |
---|
2377 | struct bintime bt; |
---|
2378 | struct mbuf mb; |
---|
2379 | struct bpf_d *d; |
---|
2380 | u_int pktlen, slen; |
---|
2381 | int gottime; |
---|
2382 | |
---|
2383 | /* Skip outgoing duplicate packets. */ |
---|
2384 | if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) { |
---|
2385 | m->m_flags &= ~M_PROMISC; |
---|
2386 | return; |
---|
2387 | } |
---|
2388 | |
---|
2389 | pktlen = m_length(m, NULL); |
---|
2390 | /* |
---|
2391 | * Craft on-stack mbuf suitable for passing to bpf_filter. |
---|
2392 | * Note that we cut corners here; we only setup what's |
---|
2393 | * absolutely needed--this mbuf should never go anywhere else. |
---|
2394 | */ |
---|
2395 | mb.m_next = m; |
---|
2396 | mb.m_data = data; |
---|
2397 | mb.m_len = dlen; |
---|
2398 | pktlen += dlen; |
---|
2399 | |
---|
2400 | gottime = BPF_TSTAMP_NONE; |
---|
2401 | |
---|
2402 | BPFIF_RLOCK(bp); |
---|
2403 | |
---|
2404 | LIST_FOREACH(d, &bp->bif_dlist, bd_next) { |
---|
2405 | if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp)) |
---|
2406 | continue; |
---|
2407 | counter_u64_add(d->bd_rcount, 1); |
---|
2408 | slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0); |
---|
2409 | if (slen != 0) { |
---|
2410 | BPFD_LOCK(d); |
---|
2411 | |
---|
2412 | counter_u64_add(d->bd_fcount, 1); |
---|
2413 | if (gottime < bpf_ts_quality(d->bd_tstamp)) |
---|
2414 | gottime = bpf_gettime(&bt, d->bd_tstamp, m); |
---|
2415 | #ifdef MAC |
---|
2416 | if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0) |
---|
2417 | #endif |
---|
2418 | catchpacket(d, (u_char *)&mb, pktlen, slen, |
---|
2419 | bpf_append_mbuf, &bt); |
---|
2420 | BPFD_UNLOCK(d); |
---|
2421 | } |
---|
2422 | } |
---|
2423 | BPFIF_RUNLOCK(bp); |
---|
2424 | } |
---|
2425 | |
---|
2426 | #undef BPF_CHECK_DIRECTION |
---|
2427 | |
---|
2428 | #undef BPF_TSTAMP_NONE |
---|
2429 | #undef BPF_TSTAMP_FAST |
---|
2430 | #undef BPF_TSTAMP_NORMAL |
---|
2431 | #undef BPF_TSTAMP_EXTERN |
---|
2432 | |
---|
2433 | static int |
---|
2434 | bpf_hdrlen(struct bpf_d *d) |
---|
2435 | { |
---|
2436 | int hdrlen; |
---|
2437 | |
---|
2438 | hdrlen = d->bd_bif->bif_hdrlen; |
---|
2439 | #ifndef BURN_BRIDGES |
---|
2440 | if (d->bd_tstamp == BPF_T_NONE || |
---|
2441 | BPF_T_FORMAT(d->bd_tstamp) == BPF_T_MICROTIME) |
---|
2442 | #ifdef COMPAT_FREEBSD32 |
---|
2443 | if (d->bd_compat32) |
---|
2444 | hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr32); |
---|
2445 | else |
---|
2446 | #endif |
---|
2447 | hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr); |
---|
2448 | else |
---|
2449 | #endif |
---|
2450 | hdrlen += SIZEOF_BPF_HDR(struct bpf_xhdr); |
---|
2451 | #ifdef COMPAT_FREEBSD32 |
---|
2452 | if (d->bd_compat32) |
---|
2453 | hdrlen = BPF_WORDALIGN32(hdrlen); |
---|
2454 | else |
---|
2455 | #endif |
---|
2456 | hdrlen = BPF_WORDALIGN(hdrlen); |
---|
2457 | |
---|
2458 | return (hdrlen - d->bd_bif->bif_hdrlen); |
---|
2459 | } |
---|
2460 | |
---|
2461 | static void |
---|
2462 | bpf_bintime2ts(struct bintime *bt, struct bpf_ts *ts, int tstype) |
---|
2463 | { |
---|
2464 | struct bintime bt2, boottimebin; |
---|
2465 | struct timeval tsm; |
---|
2466 | struct timespec tsn; |
---|
2467 | |
---|
2468 | if ((tstype & BPF_T_MONOTONIC) == 0) { |
---|
2469 | bt2 = *bt; |
---|
2470 | getboottimebin(&boottimebin); |
---|
2471 | bintime_add(&bt2, &boottimebin); |
---|
2472 | bt = &bt2; |
---|
2473 | } |
---|
2474 | switch (BPF_T_FORMAT(tstype)) { |
---|
2475 | case BPF_T_MICROTIME: |
---|
2476 | bintime2timeval(bt, &tsm); |
---|
2477 | ts->bt_sec = tsm.tv_sec; |
---|
2478 | ts->bt_frac = tsm.tv_usec; |
---|
2479 | break; |
---|
2480 | case BPF_T_NANOTIME: |
---|
2481 | bintime2timespec(bt, &tsn); |
---|
2482 | ts->bt_sec = tsn.tv_sec; |
---|
2483 | ts->bt_frac = tsn.tv_nsec; |
---|
2484 | break; |
---|
2485 | case BPF_T_BINTIME: |
---|
2486 | ts->bt_sec = bt->sec; |
---|
2487 | ts->bt_frac = bt->frac; |
---|
2488 | break; |
---|
2489 | } |
---|
2490 | } |
---|
2491 | |
---|
2492 | /* |
---|
2493 | * Move the packet data from interface memory (pkt) into the |
---|
2494 | * store buffer. "cpfn" is the routine called to do the actual data |
---|
2495 | * transfer. bcopy is passed in to copy contiguous chunks, while |
---|
2496 | * bpf_append_mbuf is passed in to copy mbuf chains. In the latter case, |
---|
2497 | * pkt is really an mbuf. |
---|
2498 | */ |
---|
2499 | static void |
---|
2500 | catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen, |
---|
2501 | void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int), |
---|
2502 | struct bintime *bt) |
---|
2503 | { |
---|
2504 | struct bpf_xhdr hdr; |
---|
2505 | #ifndef BURN_BRIDGES |
---|
2506 | struct bpf_hdr hdr_old; |
---|
2507 | #ifdef COMPAT_FREEBSD32 |
---|
2508 | struct bpf_hdr32 hdr32_old; |
---|
2509 | #endif |
---|
2510 | #endif |
---|
2511 | int caplen, curlen, hdrlen, totlen; |
---|
2512 | int do_wakeup = 0; |
---|
2513 | int do_timestamp; |
---|
2514 | int tstype; |
---|
2515 | |
---|
2516 | BPFD_LOCK_ASSERT(d); |
---|
2517 | |
---|
2518 | /* |
---|
2519 | * Detect whether user space has released a buffer back to us, and if |
---|
2520 | * so, move it from being a hold buffer to a free buffer. This may |
---|
2521 | * not be the best place to do it (for example, we might only want to |
---|
2522 | * run this check if we need the space), but for now it's a reliable |
---|
2523 | * spot to do it. |
---|
2524 | */ |
---|
2525 | if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) { |
---|
2526 | d->bd_fbuf = d->bd_hbuf; |
---|
2527 | d->bd_hbuf = NULL; |
---|
2528 | d->bd_hlen = 0; |
---|
2529 | bpf_buf_reclaimed(d); |
---|
2530 | } |
---|
2531 | |
---|
2532 | /* |
---|
2533 | * Figure out how many bytes to move. If the packet is |
---|
2534 | * greater or equal to the snapshot length, transfer that |
---|
2535 | * much. Otherwise, transfer the whole packet (unless |
---|
2536 | * we hit the buffer size limit). |
---|
2537 | */ |
---|
2538 | hdrlen = bpf_hdrlen(d); |
---|
2539 | totlen = hdrlen + min(snaplen, pktlen); |
---|
2540 | if (totlen > d->bd_bufsize) |
---|
2541 | totlen = d->bd_bufsize; |
---|
2542 | |
---|
2543 | /* |
---|
2544 | * Round up the end of the previous packet to the next longword. |
---|
2545 | * |
---|
2546 | * Drop the packet if there's no room and no hope of room |
---|
2547 | * If the packet would overflow the storage buffer or the storage |
---|
2548 | * buffer is considered immutable by the buffer model, try to rotate |
---|
2549 | * the buffer and wakeup pending processes. |
---|
2550 | */ |
---|
2551 | #ifdef COMPAT_FREEBSD32 |
---|
2552 | if (d->bd_compat32) |
---|
2553 | curlen = BPF_WORDALIGN32(d->bd_slen); |
---|
2554 | else |
---|
2555 | #endif |
---|
2556 | curlen = BPF_WORDALIGN(d->bd_slen); |
---|
2557 | if (curlen + totlen > d->bd_bufsize || !bpf_canwritebuf(d)) { |
---|
2558 | if (d->bd_fbuf == NULL) { |
---|
2559 | /* |
---|
2560 | * There's no room in the store buffer, and no |
---|
2561 | * prospect of room, so drop the packet. Notify the |
---|
2562 | * buffer model. |
---|
2563 | */ |
---|
2564 | bpf_buffull(d); |
---|
2565 | counter_u64_add(d->bd_dcount, 1); |
---|
2566 | return; |
---|
2567 | } |
---|
2568 | KASSERT(!d->bd_hbuf_in_use, ("hold buffer is in use")); |
---|
2569 | ROTATE_BUFFERS(d); |
---|
2570 | do_wakeup = 1; |
---|
2571 | curlen = 0; |
---|
2572 | } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) |
---|
2573 | /* |
---|
2574 | * Immediate mode is set, or the read timeout has already |
---|
2575 | * expired during a select call. A packet arrived, so the |
---|
2576 | * reader should be woken up. |
---|
2577 | */ |
---|
2578 | do_wakeup = 1; |
---|
2579 | caplen = totlen - hdrlen; |
---|
2580 | tstype = d->bd_tstamp; |
---|
2581 | do_timestamp = tstype != BPF_T_NONE; |
---|
2582 | #ifndef BURN_BRIDGES |
---|
2583 | if (tstype == BPF_T_NONE || BPF_T_FORMAT(tstype) == BPF_T_MICROTIME) { |
---|
2584 | struct bpf_ts ts; |
---|
2585 | if (do_timestamp) |
---|
2586 | bpf_bintime2ts(bt, &ts, tstype); |
---|
2587 | #ifdef COMPAT_FREEBSD32 |
---|
2588 | if (d->bd_compat32) { |
---|
2589 | bzero(&hdr32_old, sizeof(hdr32_old)); |
---|
2590 | if (do_timestamp) { |
---|
2591 | hdr32_old.bh_tstamp.tv_sec = ts.bt_sec; |
---|
2592 | hdr32_old.bh_tstamp.tv_usec = ts.bt_frac; |
---|
2593 | } |
---|
2594 | hdr32_old.bh_datalen = pktlen; |
---|
2595 | hdr32_old.bh_hdrlen = hdrlen; |
---|
2596 | hdr32_old.bh_caplen = caplen; |
---|
2597 | bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr32_old, |
---|
2598 | sizeof(hdr32_old)); |
---|
2599 | goto copy; |
---|
2600 | } |
---|
2601 | #endif |
---|
2602 | bzero(&hdr_old, sizeof(hdr_old)); |
---|
2603 | if (do_timestamp) { |
---|
2604 | hdr_old.bh_tstamp.tv_sec = ts.bt_sec; |
---|
2605 | hdr_old.bh_tstamp.tv_usec = ts.bt_frac; |
---|
2606 | } |
---|
2607 | hdr_old.bh_datalen = pktlen; |
---|
2608 | hdr_old.bh_hdrlen = hdrlen; |
---|
2609 | hdr_old.bh_caplen = caplen; |
---|
2610 | bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr_old, |
---|
2611 | sizeof(hdr_old)); |
---|
2612 | goto copy; |
---|
2613 | } |
---|
2614 | #endif |
---|
2615 | |
---|
2616 | /* |
---|
2617 | * Append the bpf header. Note we append the actual header size, but |
---|
2618 | * move forward the length of the header plus padding. |
---|
2619 | */ |
---|
2620 | bzero(&hdr, sizeof(hdr)); |
---|
2621 | if (do_timestamp) |
---|
2622 | bpf_bintime2ts(bt, &hdr.bh_tstamp, tstype); |
---|
2623 | hdr.bh_datalen = pktlen; |
---|
2624 | hdr.bh_hdrlen = hdrlen; |
---|
2625 | hdr.bh_caplen = caplen; |
---|
2626 | bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr)); |
---|
2627 | |
---|
2628 | /* |
---|
2629 | * Copy the packet data into the store buffer and update its length. |
---|
2630 | */ |
---|
2631 | #ifndef BURN_BRIDGES |
---|
2632 | copy: |
---|
2633 | #endif |
---|
2634 | (*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, caplen); |
---|
2635 | d->bd_slen = curlen + totlen; |
---|
2636 | |
---|
2637 | if (do_wakeup) |
---|
2638 | bpf_wakeup(d); |
---|
2639 | } |
---|
2640 | |
---|
2641 | /* |
---|
2642 | * Free buffers currently in use by a descriptor. |
---|
2643 | * Called on close. |
---|
2644 | */ |
---|
2645 | static void |
---|
2646 | bpf_freed(struct bpf_d *d) |
---|
2647 | { |
---|
2648 | |
---|
2649 | /* |
---|
2650 | * We don't need to lock out interrupts since this descriptor has |
---|
2651 | * been detached from its interface and it yet hasn't been marked |
---|
2652 | * free. |
---|
2653 | */ |
---|
2654 | bpf_free(d); |
---|
2655 | if (d->bd_rfilter != NULL) { |
---|
2656 | free((caddr_t)d->bd_rfilter, M_BPF); |
---|
2657 | #ifdef BPF_JITTER |
---|
2658 | if (d->bd_bfilter != NULL) |
---|
2659 | bpf_destroy_jit_filter(d->bd_bfilter); |
---|
2660 | #endif |
---|
2661 | } |
---|
2662 | if (d->bd_wfilter != NULL) |
---|
2663 | free((caddr_t)d->bd_wfilter, M_BPF); |
---|
2664 | mtx_destroy(&d->bd_lock); |
---|
2665 | |
---|
2666 | counter_u64_free(d->bd_rcount); |
---|
2667 | counter_u64_free(d->bd_dcount); |
---|
2668 | counter_u64_free(d->bd_fcount); |
---|
2669 | counter_u64_free(d->bd_wcount); |
---|
2670 | counter_u64_free(d->bd_wfcount); |
---|
2671 | counter_u64_free(d->bd_wdcount); |
---|
2672 | counter_u64_free(d->bd_zcopy); |
---|
2673 | |
---|
2674 | } |
---|
2675 | |
---|
2676 | /* |
---|
2677 | * Attach an interface to bpf. dlt is the link layer type; hdrlen is the |
---|
2678 | * fixed size of the link header (variable length headers not yet supported). |
---|
2679 | */ |
---|
2680 | void |
---|
2681 | bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) |
---|
2682 | { |
---|
2683 | |
---|
2684 | bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf); |
---|
2685 | } |
---|
2686 | |
---|
2687 | /* |
---|
2688 | * Attach an interface to bpf. ifp is a pointer to the structure |
---|
2689 | * defining the interface to be attached, dlt is the link layer type, |
---|
2690 | * and hdrlen is the fixed size of the link header (variable length |
---|
2691 | * headers are not yet supporrted). |
---|
2692 | */ |
---|
2693 | void |
---|
2694 | bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) |
---|
2695 | { |
---|
2696 | struct bpf_if *bp; |
---|
2697 | |
---|
2698 | KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized")); |
---|
2699 | |
---|
2700 | bp = malloc(sizeof(*bp), M_BPF, M_WAITOK | M_ZERO); |
---|
2701 | |
---|
2702 | rw_init(&bp->bif_lock, "bpf interface lock"); |
---|
2703 | LIST_INIT(&bp->bif_dlist); |
---|
2704 | LIST_INIT(&bp->bif_wlist); |
---|
2705 | bp->bif_ifp = ifp; |
---|
2706 | bp->bif_dlt = dlt; |
---|
2707 | bp->bif_hdrlen = hdrlen; |
---|
2708 | bp->bif_bpf = driverp; |
---|
2709 | *driverp = bp; |
---|
2710 | |
---|
2711 | BPF_LOCK(); |
---|
2712 | LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next); |
---|
2713 | BPF_UNLOCK(); |
---|
2714 | |
---|
2715 | if (bootverbose && IS_DEFAULT_VNET(curvnet)) |
---|
2716 | if_printf(ifp, "bpf attached\n"); |
---|
2717 | } |
---|
2718 | |
---|
2719 | #ifdef VIMAGE |
---|
2720 | /* |
---|
2721 | * When moving interfaces between vnet instances we need a way to |
---|
2722 | * query the dlt and hdrlen before detach so we can re-attch the if_bpf |
---|
2723 | * after the vmove. We unfortunately have no device driver infrastructure |
---|
2724 | * to query the interface for these values after creation/attach, thus |
---|
2725 | * add this as a workaround. |
---|
2726 | */ |
---|
2727 | int |
---|
2728 | bpf_get_bp_params(struct bpf_if *bp, u_int *bif_dlt, u_int *bif_hdrlen) |
---|
2729 | { |
---|
2730 | |
---|
2731 | if (bp == NULL) |
---|
2732 | return (ENXIO); |
---|
2733 | if (bif_dlt == NULL && bif_hdrlen == NULL) |
---|
2734 | return (0); |
---|
2735 | |
---|
2736 | if (bif_dlt != NULL) |
---|
2737 | *bif_dlt = bp->bif_dlt; |
---|
2738 | if (bif_hdrlen != NULL) |
---|
2739 | *bif_hdrlen = bp->bif_hdrlen; |
---|
2740 | |
---|
2741 | return (0); |
---|
2742 | } |
---|
2743 | #endif |
---|
2744 | |
---|
2745 | /* |
---|
2746 | * Detach bpf from an interface. This involves detaching each descriptor |
---|
2747 | * associated with the interface. Notify each descriptor as it's detached |
---|
2748 | * so that any sleepers wake up and get ENXIO. |
---|
2749 | */ |
---|
2750 | void |
---|
2751 | bpfdetach(struct ifnet *ifp) |
---|
2752 | { |
---|
2753 | struct bpf_if *bp, *bp_temp; |
---|
2754 | struct bpf_d *d; |
---|
2755 | int ndetached; |
---|
2756 | |
---|
2757 | ndetached = 0; |
---|
2758 | |
---|
2759 | BPF_LOCK(); |
---|
2760 | /* Find all bpf_if struct's which reference ifp and detach them. */ |
---|
2761 | LIST_FOREACH_SAFE(bp, &bpf_iflist, bif_next, bp_temp) { |
---|
2762 | if (ifp != bp->bif_ifp) |
---|
2763 | continue; |
---|
2764 | |
---|
2765 | LIST_REMOVE(bp, bif_next); |
---|
2766 | /* Add to to-be-freed list */ |
---|
2767 | LIST_INSERT_HEAD(&bpf_freelist, bp, bif_next); |
---|
2768 | |
---|
2769 | ndetached++; |
---|
2770 | /* |
---|
2771 | * Delay freeing bp till interface is detached |
---|
2772 | * and all routes through this interface are removed. |
---|
2773 | * Mark bp as detached to restrict new consumers. |
---|
2774 | */ |
---|
2775 | BPFIF_WLOCK(bp); |
---|
2776 | bp->bif_flags |= BPFIF_FLAG_DYING; |
---|
2777 | *bp->bif_bpf = (struct bpf_if *)&dead_bpf_if; |
---|
2778 | BPFIF_WUNLOCK(bp); |
---|
2779 | |
---|
2780 | CTR4(KTR_NET, "%s: sheduling free for encap %d (%p) for if %p", |
---|
2781 | __func__, bp->bif_dlt, bp, ifp); |
---|
2782 | |
---|
2783 | /* Free common descriptors */ |
---|
2784 | while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) { |
---|
2785 | bpf_detachd_locked(d); |
---|
2786 | BPFD_LOCK(d); |
---|
2787 | bpf_wakeup(d); |
---|
2788 | BPFD_UNLOCK(d); |
---|
2789 | } |
---|
2790 | |
---|
2791 | /* Free writer-only descriptors */ |
---|
2792 | while ((d = LIST_FIRST(&bp->bif_wlist)) != NULL) { |
---|
2793 | bpf_detachd_locked(d); |
---|
2794 | BPFD_LOCK(d); |
---|
2795 | bpf_wakeup(d); |
---|
2796 | BPFD_UNLOCK(d); |
---|
2797 | } |
---|
2798 | } |
---|
2799 | BPF_UNLOCK(); |
---|
2800 | |
---|
2801 | #ifdef INVARIANTS |
---|
2802 | if (ndetached == 0) |
---|
2803 | printf("bpfdetach: %s was not attached\n", ifp->if_xname); |
---|
2804 | #endif |
---|
2805 | } |
---|
2806 | |
---|
2807 | /* |
---|
2808 | * Interface departure handler. |
---|
2809 | * Note departure event does not guarantee interface is going down. |
---|
2810 | * Interface renaming is currently done via departure/arrival event set. |
---|
2811 | * |
---|
2812 | * Departure handled is called after all routes pointing to |
---|
2813 | * given interface are removed and interface is in down state |
---|
2814 | * restricting any packets to be sent/received. We assume it is now safe |
---|
2815 | * to free data allocated by BPF. |
---|
2816 | */ |
---|
2817 | static void |
---|
2818 | bpf_ifdetach(void *arg __unused, struct ifnet *ifp) |
---|
2819 | { |
---|
2820 | struct bpf_if *bp, *bp_temp; |
---|
2821 | int nmatched = 0; |
---|
2822 | |
---|
2823 | /* Ignore ifnet renaming. */ |
---|
2824 | if (ifp->if_flags & IFF_RENAMING) |
---|
2825 | return; |
---|
2826 | |
---|
2827 | BPF_LOCK(); |
---|
2828 | /* |
---|
2829 | * Find matching entries in free list. |
---|
2830 | * Nothing should be found if bpfdetach() was not called. |
---|
2831 | */ |
---|
2832 | LIST_FOREACH_SAFE(bp, &bpf_freelist, bif_next, bp_temp) { |
---|
2833 | if (ifp != bp->bif_ifp) |
---|
2834 | continue; |
---|
2835 | |
---|
2836 | CTR3(KTR_NET, "%s: freeing BPF instance %p for interface %p", |
---|
2837 | __func__, bp, ifp); |
---|
2838 | |
---|
2839 | LIST_REMOVE(bp, bif_next); |
---|
2840 | |
---|
2841 | rw_destroy(&bp->bif_lock); |
---|
2842 | free(bp, M_BPF); |
---|
2843 | |
---|
2844 | nmatched++; |
---|
2845 | } |
---|
2846 | BPF_UNLOCK(); |
---|
2847 | } |
---|
2848 | |
---|
2849 | /* |
---|
2850 | * Get a list of available data link type of the interface. |
---|
2851 | */ |
---|
2852 | static int |
---|
2853 | bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl) |
---|
2854 | { |
---|
2855 | struct ifnet *ifp; |
---|
2856 | struct bpf_if *bp; |
---|
2857 | u_int *lst; |
---|
2858 | int error, n, n1; |
---|
2859 | |
---|
2860 | BPF_LOCK_ASSERT(); |
---|
2861 | |
---|
2862 | ifp = d->bd_bif->bif_ifp; |
---|
2863 | again: |
---|
2864 | n1 = 0; |
---|
2865 | LIST_FOREACH(bp, &bpf_iflist, bif_next) { |
---|
2866 | if (bp->bif_ifp == ifp) |
---|
2867 | n1++; |
---|
2868 | } |
---|
2869 | if (bfl->bfl_list == NULL) { |
---|
2870 | bfl->bfl_len = n1; |
---|
2871 | return (0); |
---|
2872 | } |
---|
2873 | if (n1 > bfl->bfl_len) |
---|
2874 | return (ENOMEM); |
---|
2875 | BPF_UNLOCK(); |
---|
2876 | lst = malloc(n1 * sizeof(u_int), M_TEMP, M_WAITOK); |
---|
2877 | n = 0; |
---|
2878 | BPF_LOCK(); |
---|
2879 | LIST_FOREACH(bp, &bpf_iflist, bif_next) { |
---|
2880 | if (bp->bif_ifp != ifp) |
---|
2881 | continue; |
---|
2882 | if (n >= n1) { |
---|
2883 | free(lst, M_TEMP); |
---|
2884 | goto again; |
---|
2885 | } |
---|
2886 | lst[n] = bp->bif_dlt; |
---|
2887 | n++; |
---|
2888 | } |
---|
2889 | BPF_UNLOCK(); |
---|
2890 | error = copyout(lst, bfl->bfl_list, sizeof(u_int) * n); |
---|
2891 | free(lst, M_TEMP); |
---|
2892 | BPF_LOCK(); |
---|
2893 | bfl->bfl_len = n; |
---|
2894 | return (error); |
---|
2895 | } |
---|
2896 | |
---|
2897 | /* |
---|
2898 | * Set the data link type of a BPF instance. |
---|
2899 | */ |
---|
2900 | static int |
---|
2901 | bpf_setdlt(struct bpf_d *d, u_int dlt) |
---|
2902 | { |
---|
2903 | int error, opromisc; |
---|
2904 | struct ifnet *ifp; |
---|
2905 | struct bpf_if *bp; |
---|
2906 | |
---|
2907 | BPF_LOCK_ASSERT(); |
---|
2908 | |
---|
2909 | if (d->bd_bif->bif_dlt == dlt) |
---|
2910 | return (0); |
---|
2911 | ifp = d->bd_bif->bif_ifp; |
---|
2912 | |
---|
2913 | LIST_FOREACH(bp, &bpf_iflist, bif_next) { |
---|
2914 | if (bp->bif_ifp == ifp && bp->bif_dlt == dlt) |
---|
2915 | break; |
---|
2916 | } |
---|
2917 | |
---|
2918 | if (bp != NULL) { |
---|
2919 | opromisc = d->bd_promisc; |
---|
2920 | bpf_attachd(d, bp); |
---|
2921 | BPFD_LOCK(d); |
---|
2922 | reset_d(d); |
---|
2923 | BPFD_UNLOCK(d); |
---|
2924 | if (opromisc) { |
---|
2925 | error = ifpromisc(bp->bif_ifp, 1); |
---|
2926 | if (error) |
---|
2927 | if_printf(bp->bif_ifp, |
---|
2928 | "bpf_setdlt: ifpromisc failed (%d)\n", |
---|
2929 | error); |
---|
2930 | else |
---|
2931 | d->bd_promisc = 1; |
---|
2932 | } |
---|
2933 | } |
---|
2934 | return (bp == NULL ? EINVAL : 0); |
---|
2935 | } |
---|
2936 | #ifdef __rtems__ |
---|
2937 | static struct bpf_d * |
---|
2938 | bpf_imfs_get_context_by_iop(const rtems_libio_t *iop) |
---|
2939 | { |
---|
2940 | return iop->data1; |
---|
2941 | } |
---|
2942 | |
---|
2943 | static int |
---|
2944 | bpf_imfs_open(rtems_libio_t *iop, const char *path, int oflag, mode_t mode) |
---|
2945 | { |
---|
2946 | struct bpf_d *d; |
---|
2947 | |
---|
2948 | d = bpfopen(NULL, oflag + 1, 0, NULL); |
---|
2949 | iop->data1 = d; |
---|
2950 | |
---|
2951 | if (d != NULL) { |
---|
2952 | return (0); |
---|
2953 | } else { |
---|
2954 | rtems_set_errno_and_return_minus_one(ENOMEM); |
---|
2955 | } |
---|
2956 | } |
---|
2957 | |
---|
2958 | static int |
---|
2959 | bpf_imfs_close(rtems_libio_t *iop) |
---|
2960 | { |
---|
2961 | struct bpf_d *d = bpf_imfs_get_context_by_iop(iop); |
---|
2962 | |
---|
2963 | bpf_dtor(d); |
---|
2964 | |
---|
2965 | return (0); |
---|
2966 | } |
---|
2967 | |
---|
2968 | static ssize_t |
---|
2969 | bpf_imfs_readv(rtems_libio_t *iop, const struct iovec *iov, int iovcnt, ssize_t total) |
---|
2970 | { |
---|
2971 | struct bpf_d *d = bpf_imfs_get_context_by_iop(iop); |
---|
2972 | struct thread *td = rtems_bsd_get_curthread_or_null(); |
---|
2973 | struct uio uio = { |
---|
2974 | .uio_iov = iov, |
---|
2975 | .uio_iovcnt = iovcnt, |
---|
2976 | .uio_offset = 0, |
---|
2977 | .uio_resid = total, |
---|
2978 | .uio_segflg = UIO_USERSPACE, |
---|
2979 | .uio_rw = UIO_READ, |
---|
2980 | .uio_td = td |
---|
2981 | }; |
---|
2982 | int error; |
---|
2983 | |
---|
2984 | if (td != NULL) { |
---|
2985 | error = bpfread(d, &uio, |
---|
2986 | rtems_libio_to_fcntl_flags(iop->flags)); |
---|
2987 | } else { |
---|
2988 | error = ENOMEM; |
---|
2989 | } |
---|
2990 | |
---|
2991 | if (error == 0) { |
---|
2992 | return (total - uio.uio_resid); |
---|
2993 | } else { |
---|
2994 | rtems_set_errno_and_return_minus_one(error); |
---|
2995 | } |
---|
2996 | } |
---|
2997 | |
---|
2998 | static ssize_t |
---|
2999 | bpf_imfs_read(rtems_libio_t *iop, void *buffer, size_t count) |
---|
3000 | { |
---|
3001 | struct iovec iov = { |
---|
3002 | .iov_base = buffer, |
---|
3003 | .iov_len = count |
---|
3004 | }; |
---|
3005 | |
---|
3006 | return bpf_imfs_readv(iop, &iov, 1, count); |
---|
3007 | } |
---|
3008 | |
---|
3009 | static ssize_t |
---|
3010 | bpf_imfs_writev(rtems_libio_t *iop, const struct iovec *iov, int iovcnt, ssize_t total) |
---|
3011 | { |
---|
3012 | struct bpf_d *d = bpf_imfs_get_context_by_iop(iop); |
---|
3013 | struct thread *td = rtems_bsd_get_curthread_or_null(); |
---|
3014 | struct uio uio = { |
---|
3015 | .uio_iov = iov, |
---|
3016 | .uio_iovcnt = iovcnt, |
---|
3017 | .uio_offset = 0, |
---|
3018 | .uio_resid = total, |
---|
3019 | .uio_segflg = UIO_USERSPACE, |
---|
3020 | .uio_rw = UIO_WRITE, |
---|
3021 | .uio_td = td |
---|
3022 | }; |
---|
3023 | int error; |
---|
3024 | |
---|
3025 | if (td != NULL) { |
---|
3026 | error = bpfwrite(d, &uio, |
---|
3027 | rtems_libio_to_fcntl_flags(iop->flags)); |
---|
3028 | } else { |
---|
3029 | error = ENOMEM; |
---|
3030 | } |
---|
3031 | |
---|
3032 | if (error == 0) { |
---|
3033 | return (total - uio.uio_resid); |
---|
3034 | } else { |
---|
3035 | rtems_set_errno_and_return_minus_one(error); |
---|
3036 | } |
---|
3037 | } |
---|
3038 | |
---|
3039 | static ssize_t |
---|
3040 | bpf_imfs_write(rtems_libio_t *iop, const void *buffer, size_t count) |
---|
3041 | { |
---|
3042 | struct iovec iov = { |
---|
3043 | .iov_base = buffer, |
---|
3044 | .iov_len = count |
---|
3045 | }; |
---|
3046 | |
---|
3047 | return bpf_imfs_writev(iop, &iov, 1, count); |
---|
3048 | } |
---|
3049 | |
---|
3050 | static int |
---|
3051 | bpf_imfs_ioctl(rtems_libio_t *iop, ioctl_command_t request, void *buffer) |
---|
3052 | { |
---|
3053 | struct bpf_d *d = bpf_imfs_get_context_by_iop(iop); |
---|
3054 | struct thread *td = rtems_bsd_get_curthread_or_null(); |
---|
3055 | int error; |
---|
3056 | |
---|
3057 | if (td != 0) { |
---|
3058 | error = bpfioctl(d, request, buffer, 0, td); |
---|
3059 | } else { |
---|
3060 | error = ENOMEM; |
---|
3061 | } |
---|
3062 | |
---|
3063 | return rtems_bsd_error_to_status_and_errno(error); |
---|
3064 | } |
---|
3065 | |
---|
3066 | static int |
---|
3067 | bpf_imfs_poll(rtems_libio_t *iop, int events) |
---|
3068 | { |
---|
3069 | struct bpf_d *d = bpf_imfs_get_context_by_iop(iop); |
---|
3070 | |
---|
3071 | return (bpfpoll(d, events, rtems_bsd_get_curthread_or_wait_forever())); |
---|
3072 | } |
---|
3073 | |
---|
3074 | static int |
---|
3075 | bpf_imfs_kqfilter(rtems_libio_t *iop, struct knote *kn) |
---|
3076 | { |
---|
3077 | struct bpf_d *d = bpf_imfs_get_context_by_iop(iop); |
---|
3078 | |
---|
3079 | return (bpfkqfilter(d, kn)); |
---|
3080 | } |
---|
3081 | |
---|
3082 | static const rtems_filesystem_file_handlers_r bpf_imfs_handlers = { |
---|
3083 | .open_h = bpf_imfs_open, |
---|
3084 | .close_h = bpf_imfs_close, |
---|
3085 | .read_h = bpf_imfs_read, |
---|
3086 | .write_h = bpf_imfs_write, |
---|
3087 | .ioctl_h = bpf_imfs_ioctl, |
---|
3088 | .lseek_h = rtems_filesystem_default_lseek_file, |
---|
3089 | .fstat_h = rtems_filesystem_default_fstat, |
---|
3090 | .ftruncate_h = rtems_filesystem_default_ftruncate, |
---|
3091 | .fsync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
3092 | .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
3093 | .fcntl_h = rtems_filesystem_default_fcntl, |
---|
3094 | .poll_h = bpf_imfs_poll, |
---|
3095 | .kqfilter_h = bpf_imfs_kqfilter, |
---|
3096 | .readv_h = bpf_imfs_readv, |
---|
3097 | .writev_h = bpf_imfs_writev |
---|
3098 | }; |
---|
3099 | |
---|
3100 | static const IMFS_node_control bpf_imfs_control = IMFS_GENERIC_INITIALIZER( |
---|
3101 | &bpf_imfs_handlers, IMFS_node_initialize_generic, |
---|
3102 | IMFS_node_destroy_default); |
---|
3103 | #endif /* __rtems__ */ |
---|
3104 | |
---|
3105 | static void |
---|
3106 | bpf_drvinit(void *unused) |
---|
3107 | { |
---|
3108 | #ifndef __rtems__ |
---|
3109 | struct cdev *dev; |
---|
3110 | #else /* __rtems__ */ |
---|
3111 | mode_t mode = S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO; |
---|
3112 | int rv; |
---|
3113 | #endif /* __rtems__ */ |
---|
3114 | |
---|
3115 | sx_init(&bpf_sx, "bpf global lock"); |
---|
3116 | LIST_INIT(&bpf_iflist); |
---|
3117 | LIST_INIT(&bpf_freelist); |
---|
3118 | |
---|
3119 | #ifndef __rtems__ |
---|
3120 | dev = make_dev(&bpf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "bpf"); |
---|
3121 | /* For compatibility */ |
---|
3122 | make_dev_alias(dev, "bpf0"); |
---|
3123 | #else /* __rtems__ */ |
---|
3124 | rv = IMFS_make_generic_node("/dev/bpf", mode, &bpf_imfs_control, NULL); |
---|
3125 | BSD_ASSERT(rv == 0); |
---|
3126 | rv = symlink("/dev/bpf", "/dev/bpf0"); |
---|
3127 | BSD_ASSERT(rv == 0); |
---|
3128 | #endif /* __rtems__ */ |
---|
3129 | |
---|
3130 | /* Register interface departure handler */ |
---|
3131 | bpf_ifdetach_cookie = EVENTHANDLER_REGISTER( |
---|
3132 | ifnet_departure_event, bpf_ifdetach, NULL, |
---|
3133 | EVENTHANDLER_PRI_ANY); |
---|
3134 | } |
---|
3135 | |
---|
3136 | /* |
---|
3137 | * Zero out the various packet counters associated with all of the bpf |
---|
3138 | * descriptors. At some point, we will probably want to get a bit more |
---|
3139 | * granular and allow the user to specify descriptors to be zeroed. |
---|
3140 | */ |
---|
3141 | static void |
---|
3142 | bpf_zero_counters(void) |
---|
3143 | { |
---|
3144 | struct bpf_if *bp; |
---|
3145 | struct bpf_d *bd; |
---|
3146 | |
---|
3147 | BPF_LOCK(); |
---|
3148 | LIST_FOREACH(bp, &bpf_iflist, bif_next) { |
---|
3149 | BPFIF_RLOCK(bp); |
---|
3150 | LIST_FOREACH(bd, &bp->bif_dlist, bd_next) { |
---|
3151 | BPFD_LOCK(bd); |
---|
3152 | counter_u64_zero(bd->bd_rcount); |
---|
3153 | counter_u64_zero(bd->bd_dcount); |
---|
3154 | counter_u64_zero(bd->bd_fcount); |
---|
3155 | counter_u64_zero(bd->bd_wcount); |
---|
3156 | counter_u64_zero(bd->bd_wfcount); |
---|
3157 | counter_u64_zero(bd->bd_zcopy); |
---|
3158 | BPFD_UNLOCK(bd); |
---|
3159 | } |
---|
3160 | BPFIF_RUNLOCK(bp); |
---|
3161 | } |
---|
3162 | BPF_UNLOCK(); |
---|
3163 | } |
---|
3164 | |
---|
3165 | /* |
---|
3166 | * Fill filter statistics |
---|
3167 | */ |
---|
3168 | static void |
---|
3169 | bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd) |
---|
3170 | { |
---|
3171 | |
---|
3172 | bzero(d, sizeof(*d)); |
---|
3173 | BPFD_LOCK_ASSERT(bd); |
---|
3174 | d->bd_structsize = sizeof(*d); |
---|
3175 | /* XXX: reading should be protected by global lock */ |
---|
3176 | d->bd_immediate = bd->bd_immediate; |
---|
3177 | d->bd_promisc = bd->bd_promisc; |
---|
3178 | d->bd_hdrcmplt = bd->bd_hdrcmplt; |
---|
3179 | d->bd_direction = bd->bd_direction; |
---|
3180 | d->bd_feedback = bd->bd_feedback; |
---|
3181 | #ifndef __rtems__ |
---|
3182 | d->bd_async = bd->bd_async; |
---|
3183 | #endif /* __rtems__ */ |
---|
3184 | d->bd_rcount = counter_u64_fetch(bd->bd_rcount); |
---|
3185 | d->bd_dcount = counter_u64_fetch(bd->bd_dcount); |
---|
3186 | d->bd_fcount = counter_u64_fetch(bd->bd_fcount); |
---|
3187 | d->bd_sig = bd->bd_sig; |
---|
3188 | d->bd_slen = bd->bd_slen; |
---|
3189 | d->bd_hlen = bd->bd_hlen; |
---|
3190 | d->bd_bufsize = bd->bd_bufsize; |
---|
3191 | d->bd_pid = bd->bd_pid; |
---|
3192 | strlcpy(d->bd_ifname, |
---|
3193 | bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ); |
---|
3194 | d->bd_locked = bd->bd_locked; |
---|
3195 | d->bd_wcount = counter_u64_fetch(bd->bd_wcount); |
---|
3196 | d->bd_wdcount = counter_u64_fetch(bd->bd_wdcount); |
---|
3197 | d->bd_wfcount = counter_u64_fetch(bd->bd_wfcount); |
---|
3198 | d->bd_zcopy = counter_u64_fetch(bd->bd_zcopy); |
---|
3199 | d->bd_bufmode = bd->bd_bufmode; |
---|
3200 | } |
---|
3201 | |
---|
3202 | /* |
---|
3203 | * Handle `netstat -B' stats request |
---|
3204 | */ |
---|
3205 | static int |
---|
3206 | bpf_stats_sysctl(SYSCTL_HANDLER_ARGS) |
---|
3207 | { |
---|
3208 | static const struct xbpf_d zerostats; |
---|
3209 | struct xbpf_d *xbdbuf, *xbd, tempstats; |
---|
3210 | int index, error; |
---|
3211 | struct bpf_if *bp; |
---|
3212 | struct bpf_d *bd; |
---|
3213 | |
---|
3214 | /* |
---|
3215 | * XXX This is not technically correct. It is possible for non |
---|
3216 | * privileged users to open bpf devices. It would make sense |
---|
3217 | * if the users who opened the devices were able to retrieve |
---|
3218 | * the statistics for them, too. |
---|
3219 | */ |
---|
3220 | error = priv_check(req->td, PRIV_NET_BPF); |
---|
3221 | if (error) |
---|
3222 | return (error); |
---|
3223 | /* |
---|
3224 | * Check to see if the user is requesting that the counters be |
---|
3225 | * zeroed out. Explicitly check that the supplied data is zeroed, |
---|
3226 | * as we aren't allowing the user to set the counters currently. |
---|
3227 | */ |
---|
3228 | if (req->newptr != NULL) { |
---|
3229 | if (req->newlen != sizeof(tempstats)) |
---|
3230 | return (EINVAL); |
---|
3231 | memset(&tempstats, 0, sizeof(tempstats)); |
---|
3232 | error = SYSCTL_IN(req, &tempstats, sizeof(tempstats)); |
---|
3233 | if (error) |
---|
3234 | return (error); |
---|
3235 | if (bcmp(&tempstats, &zerostats, sizeof(tempstats)) != 0) |
---|
3236 | return (EINVAL); |
---|
3237 | bpf_zero_counters(); |
---|
3238 | return (0); |
---|
3239 | } |
---|
3240 | if (req->oldptr == NULL) |
---|
3241 | return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd))); |
---|
3242 | if (bpf_bpfd_cnt == 0) |
---|
3243 | return (SYSCTL_OUT(req, 0, 0)); |
---|
3244 | xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK); |
---|
3245 | BPF_LOCK(); |
---|
3246 | if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) { |
---|
3247 | BPF_UNLOCK(); |
---|
3248 | free(xbdbuf, M_BPF); |
---|
3249 | return (ENOMEM); |
---|
3250 | } |
---|
3251 | index = 0; |
---|
3252 | LIST_FOREACH(bp, &bpf_iflist, bif_next) { |
---|
3253 | BPFIF_RLOCK(bp); |
---|
3254 | /* Send writers-only first */ |
---|
3255 | LIST_FOREACH(bd, &bp->bif_wlist, bd_next) { |
---|
3256 | xbd = &xbdbuf[index++]; |
---|
3257 | BPFD_LOCK(bd); |
---|
3258 | bpfstats_fill_xbpf(xbd, bd); |
---|
3259 | BPFD_UNLOCK(bd); |
---|
3260 | } |
---|
3261 | LIST_FOREACH(bd, &bp->bif_dlist, bd_next) { |
---|
3262 | xbd = &xbdbuf[index++]; |
---|
3263 | BPFD_LOCK(bd); |
---|
3264 | bpfstats_fill_xbpf(xbd, bd); |
---|
3265 | BPFD_UNLOCK(bd); |
---|
3266 | } |
---|
3267 | BPFIF_RUNLOCK(bp); |
---|
3268 | } |
---|
3269 | BPF_UNLOCK(); |
---|
3270 | error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd)); |
---|
3271 | free(xbdbuf, M_BPF); |
---|
3272 | return (error); |
---|
3273 | } |
---|
3274 | |
---|
3275 | SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL); |
---|
3276 | |
---|
3277 | #else /* !DEV_BPF && !NETGRAPH_BPF */ |
---|
3278 | |
---|
3279 | /* |
---|
3280 | * NOP stubs to allow bpf-using drivers to load and function. |
---|
3281 | * |
---|
3282 | * A 'better' implementation would allow the core bpf functionality |
---|
3283 | * to be loaded at runtime. |
---|
3284 | */ |
---|
3285 | |
---|
3286 | void |
---|
3287 | bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen) |
---|
3288 | { |
---|
3289 | } |
---|
3290 | |
---|
3291 | void |
---|
3292 | bpf_mtap(struct bpf_if *bp, struct mbuf *m) |
---|
3293 | { |
---|
3294 | } |
---|
3295 | |
---|
3296 | void |
---|
3297 | bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m) |
---|
3298 | { |
---|
3299 | } |
---|
3300 | |
---|
3301 | void |
---|
3302 | bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen) |
---|
3303 | { |
---|
3304 | |
---|
3305 | bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf); |
---|
3306 | } |
---|
3307 | |
---|
3308 | void |
---|
3309 | bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) |
---|
3310 | { |
---|
3311 | |
---|
3312 | *driverp = (struct bpf_if *)&dead_bpf_if; |
---|
3313 | } |
---|
3314 | |
---|
3315 | void |
---|
3316 | bpfdetach(struct ifnet *ifp) |
---|
3317 | { |
---|
3318 | } |
---|
3319 | |
---|
3320 | u_int |
---|
3321 | bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen) |
---|
3322 | { |
---|
3323 | return -1; /* "no filter" behaviour */ |
---|
3324 | } |
---|
3325 | |
---|
3326 | int |
---|
3327 | bpf_validate(const struct bpf_insn *f, int len) |
---|
3328 | { |
---|
3329 | return 0; /* false */ |
---|
3330 | } |
---|
3331 | |
---|
3332 | #endif /* !DEV_BPF && !NETGRAPH_BPF */ |
---|
3333 | |
---|
3334 | #ifdef DDB |
---|
3335 | static void |
---|
3336 | bpf_show_bpf_if(struct bpf_if *bpf_if) |
---|
3337 | { |
---|
3338 | |
---|
3339 | if (bpf_if == NULL) |
---|
3340 | return; |
---|
3341 | db_printf("%p:\n", bpf_if); |
---|
3342 | #define BPF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, bpf_if->e); |
---|
3343 | /* bif_ext.bif_next */ |
---|
3344 | /* bif_ext.bif_dlist */ |
---|
3345 | BPF_DB_PRINTF("%#x", bif_dlt); |
---|
3346 | BPF_DB_PRINTF("%u", bif_hdrlen); |
---|
3347 | BPF_DB_PRINTF("%p", bif_ifp); |
---|
3348 | /* bif_lock */ |
---|
3349 | /* bif_wlist */ |
---|
3350 | BPF_DB_PRINTF("%#x", bif_flags); |
---|
3351 | } |
---|
3352 | |
---|
3353 | DB_SHOW_COMMAND(bpf_if, db_show_bpf_if) |
---|
3354 | { |
---|
3355 | |
---|
3356 | if (!have_addr) { |
---|
3357 | db_printf("usage: show bpf_if <struct bpf_if *>\n"); |
---|
3358 | return; |
---|
3359 | } |
---|
3360 | |
---|
3361 | bpf_show_bpf_if((struct bpf_if *)addr); |
---|
3362 | } |
---|
3363 | #endif |
---|