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