source: rtems-libbsd/freebsd/sys/net/bpf.c @ e10d1cd

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e10d1cd was e10d1cd, checked in by Sebastian Huber <sebastian.huber@…>, on 12/13/13 at 09:57:03

BPF(4): Add support

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