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