Remove ipw(4) manpage
[dragonfly.git] / sys / net / bpf.c
1 /*
2  * Copyright (c) 1990, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from the Stanford/CMU enet packet filter,
6  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8  * Berkeley Laboratory.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)bpf.c       8.2 (Berkeley) 3/28/94
39  *
40  * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.12 2002/04/14 21:41:48 luigi Exp $
41  * $DragonFly: src/sys/net/bpf.c,v 1.44 2008/03/14 09:52:10 matthias Exp $
42  */
43
44 #include "use_bpf.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/conf.h>
49 #include <sys/device.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/time.h>
53 #include <sys/proc.h>
54 #include <sys/signalvar.h>
55 #include <sys/filio.h>
56 #include <sys/sockio.h>
57 #include <sys/ttycom.h>
58 #include <sys/filedesc.h>
59
60 #include <sys/poll.h>
61
62 #include <sys/socket.h>
63 #include <sys/vnode.h>
64
65 #include <sys/thread2.h>
66
67 #include <net/if.h>
68 #include <net/bpf.h>
69 #include <net/bpfdesc.h>
70
71 #include <netinet/in.h>
72 #include <netinet/if_ether.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75
76 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
77
78 #if NBPF > 0
79
80 /*
81  * The default read buffer size is patchable.
82  */
83 static int bpf_bufsize = BPF_DEFAULTBUFSIZE;
84 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
85            &bpf_bufsize, 0, "");
86 int bpf_maxbufsize = BPF_MAXBUFSIZE;
87 SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
88            &bpf_maxbufsize, 0, "");
89
90 /*
91  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
92  */
93 static struct bpf_if    *bpf_iflist;
94
95 static int      bpf_allocbufs(struct bpf_d *);
96 static void     bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
97 static void     bpf_detachd(struct bpf_d *d);
98 static void     bpf_resetd(struct bpf_d *);
99 static void     bpf_freed(struct bpf_d *);
100 static void     bpf_mcopy(const void *, void *, size_t);
101 static int      bpf_movein(struct uio *, int, struct mbuf **,
102                            struct sockaddr *, int *, struct bpf_insn *);
103 static int      bpf_setif(struct bpf_d *, struct ifreq *);
104 static void     bpf_timed_out(void *);
105 static void     bpf_wakeup(struct bpf_d *);
106 static void     catchpacket(struct bpf_d *, u_char *, u_int, u_int,
107                             void (*)(const void *, void *, size_t),
108                             const struct timeval *);
109 static int      bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
110 static int      bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
111 static int      bpf_setdlt(struct bpf_d *, u_int);
112 static void     bpf_drvinit(void *unused);
113
114 static d_open_t         bpfopen;
115 static d_close_t        bpfclose;
116 static d_read_t         bpfread;
117 static d_write_t        bpfwrite;
118 static d_ioctl_t        bpfioctl;
119 static d_poll_t         bpfpoll;
120
121 #define CDEV_MAJOR 23
122 static struct dev_ops bpf_ops = {
123         { "bpf", CDEV_MAJOR, 0 },
124         .d_open =       bpfopen,
125         .d_close =      bpfclose,
126         .d_read =       bpfread,
127         .d_write =      bpfwrite,
128         .d_ioctl =      bpfioctl,
129         .d_poll =       bpfpoll,
130 };
131
132
133 static int
134 bpf_movein(struct uio *uio, int linktype, struct mbuf **mp,
135            struct sockaddr *sockp, int *datlen, struct bpf_insn *wfilter)
136 {
137         struct mbuf *m;
138         int error;
139         int len;
140         int hlen;
141         int slen;
142
143         *datlen = 0;
144         *mp = NULL;
145
146         /*
147          * Build a sockaddr based on the data link layer type.
148          * We do this at this level because the ethernet header
149          * is copied directly into the data field of the sockaddr.
150          * In the case of SLIP, there is no header and the packet
151          * is forwarded as is.
152          * Also, we are careful to leave room at the front of the mbuf
153          * for the link level header.
154          */
155         switch (linktype) {
156         case DLT_SLIP:
157                 sockp->sa_family = AF_INET;
158                 hlen = 0;
159                 break;
160
161         case DLT_EN10MB:
162                 sockp->sa_family = AF_UNSPEC;
163                 /* XXX Would MAXLINKHDR be better? */
164                 hlen = sizeof(struct ether_header);
165                 break;
166
167         case DLT_RAW:
168         case DLT_NULL:
169                 sockp->sa_family = AF_UNSPEC;
170                 hlen = 0;
171                 break;
172
173         case DLT_ATM_RFC1483:
174                 /*
175                  * en atm driver requires 4-byte atm pseudo header.
176                  * though it isn't standard, vpi:vci needs to be
177                  * specified anyway.
178                  */
179                 sockp->sa_family = AF_UNSPEC;
180                 hlen = 12;      /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
181                 break;
182
183         case DLT_PPP:
184                 sockp->sa_family = AF_UNSPEC;
185                 hlen = 4;       /* This should match PPP_HDRLEN */
186                 break;
187
188         default:
189                 return(EIO);
190         }
191
192         len = uio->uio_resid;
193         *datlen = len - hlen;
194         if ((unsigned)len > MCLBYTES)
195                 return(EIO);
196
197         m = m_getl(len, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
198         if (m == NULL)
199                 return(ENOBUFS);
200         m->m_pkthdr.len = m->m_len = len;
201         m->m_pkthdr.rcvif = NULL;
202         *mp = m;
203
204         if (m->m_len < hlen) {
205                 error = EPERM;
206                 goto bad;
207         }
208
209         error = uiomove(mtod(m, u_char *), len, uio);
210         if (error)
211                 goto bad;
212
213         slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
214         if (slen == 0) {
215                 error = EPERM;
216                 goto bad;
217         }
218
219         /*
220          * Make room for link header, and copy it to sockaddr.
221          */
222         if (hlen != 0) {
223                 bcopy(m->m_data, sockp->sa_data, hlen);
224                 m->m_pkthdr.len -= hlen;
225                 m->m_len -= hlen;
226                 m->m_data += hlen; /* XXX */
227         }
228         return (0);
229 bad:
230         m_freem(m);
231         return(error);
232 }
233
234 /*
235  * Attach file to the bpf interface, i.e. make d listen on bp.
236  * Must be called at splimp.
237  */
238 static void
239 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
240 {
241         /*
242          * Point d at bp, and add d to the interface's list of listeners.
243          * Finally, point the driver's bpf cookie at the interface so
244          * it will divert packets to bpf.
245          */
246         d->bd_bif = bp;
247         SLIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
248         *bp->bif_driverp = bp;
249 }
250
251 /*
252  * Detach a file from its interface.
253  */
254 static void
255 bpf_detachd(struct bpf_d *d)
256 {
257         int error;
258         struct bpf_if *bp;
259         struct ifnet *ifp;
260
261         bp = d->bd_bif;
262         ifp = bp->bif_ifp;
263
264         /* Remove d from the interface's descriptor list. */
265         SLIST_REMOVE(&bp->bif_dlist, d, bpf_d, bd_next);
266
267         if (SLIST_EMPTY(&bp->bif_dlist)) {
268                 /*
269                  * Let the driver know that there are no more listeners.
270                  */
271                 *bp->bif_driverp = NULL;
272         }
273         d->bd_bif = NULL;
274         /*
275          * Check if this descriptor had requested promiscuous mode.
276          * If so, turn it off.
277          */
278         if (d->bd_promisc) {
279                 d->bd_promisc = 0;
280                 error = ifpromisc(ifp, 0);
281                 if (error != 0 && error != ENXIO) {
282                         /*
283                          * ENXIO can happen if a pccard is unplugged,
284                          * Something is really wrong if we were able to put
285                          * the driver into promiscuous mode, but can't
286                          * take it out.
287                          */
288                         if_printf(ifp, "bpf_detach: ifpromisc failed(%d)\n",
289                                   error);
290                 }
291         }
292 }
293
294 /*
295  * Open ethernet device.  Returns ENXIO for illegal minor device number,
296  * EBUSY if file is open by another process.
297  */
298 /* ARGSUSED */
299 static int
300 bpfopen(struct dev_open_args *ap)
301 {
302         cdev_t dev = ap->a_head.a_dev;
303         struct bpf_d *d;
304
305         if (ap->a_cred->cr_prison)
306                 return(EPERM);
307
308         d = dev->si_drv1;
309         /*
310          * Each minor can be opened by only one process.  If the requested
311          * minor is in use, return EBUSY.
312          */
313         if (d != NULL)
314                 return(EBUSY);
315         make_dev(&bpf_ops, minor(dev), 0, 0, 0600, "bpf%d", lminor(dev));
316         MALLOC(d, struct bpf_d *, sizeof *d, M_BPF, M_WAITOK | M_ZERO);
317         dev->si_drv1 = d;
318         d->bd_bufsize = bpf_bufsize;
319         d->bd_sig = SIGIO;
320         d->bd_seesent = 1;
321         callout_init(&d->bd_callout);
322         return(0);
323 }
324
325 /*
326  * Close the descriptor by detaching it from its interface,
327  * deallocating its buffers, and marking it free.
328  */
329 /* ARGSUSED */
330 static int
331 bpfclose(struct dev_close_args *ap)
332 {
333         cdev_t dev = ap->a_head.a_dev;
334         struct bpf_d *d = dev->si_drv1;
335
336         funsetown(d->bd_sigio);
337         crit_enter();
338         if (d->bd_state == BPF_WAITING)
339                 callout_stop(&d->bd_callout);
340         d->bd_state = BPF_IDLE;
341         if (d->bd_bif != NULL)
342                 bpf_detachd(d);
343         crit_exit();
344         bpf_freed(d);
345         dev->si_drv1 = NULL;
346         kfree(d, M_BPF);
347
348         return(0);
349 }
350
351 /*
352  * Rotate the packet buffers in descriptor d.  Move the store buffer
353  * into the hold slot, and the free buffer into the store slot.
354  * Zero the length of the new store buffer.
355  */
356 #define ROTATE_BUFFERS(d) \
357         (d)->bd_hbuf = (d)->bd_sbuf; \
358         (d)->bd_hlen = (d)->bd_slen; \
359         (d)->bd_sbuf = (d)->bd_fbuf; \
360         (d)->bd_slen = 0; \
361         (d)->bd_fbuf = NULL;
362 /*
363  *  bpfread - read next chunk of packets from buffers
364  */
365 static int
366 bpfread(struct dev_read_args *ap)
367 {
368         cdev_t dev = ap->a_head.a_dev;
369         struct bpf_d *d = dev->si_drv1;
370         int timed_out;
371         int error;
372
373         /*
374          * Restrict application to use a buffer the same size as
375          * as kernel buffers.
376          */
377         if (ap->a_uio->uio_resid != d->bd_bufsize)
378                 return(EINVAL);
379
380         crit_enter();
381         if (d->bd_state == BPF_WAITING)
382                 callout_stop(&d->bd_callout);
383         timed_out = (d->bd_state == BPF_TIMED_OUT);
384         d->bd_state = BPF_IDLE;
385         /*
386          * If the hold buffer is empty, then do a timed sleep, which
387          * ends when the timeout expires or when enough packets
388          * have arrived to fill the store buffer.
389          */
390         while (d->bd_hbuf == NULL) {
391                 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
392                         /*
393                          * A packet(s) either arrived since the previous
394                          * read or arrived while we were asleep.
395                          * Rotate the buffers and return what's here.
396                          */
397                         ROTATE_BUFFERS(d);
398                         break;
399                 }
400
401                 /*
402                  * No data is available, check to see if the bpf device
403                  * is still pointed at a real interface.  If not, return
404                  * ENXIO so that the userland process knows to rebind
405                  * it before using it again.
406                  */
407                 if (d->bd_bif == NULL) {
408                         crit_exit();
409                         return(ENXIO);
410                 }
411
412                 if (ap->a_ioflag & IO_NDELAY) {
413                         crit_exit();
414                         return(EWOULDBLOCK);
415                 }
416                 error = tsleep(d, PCATCH, "bpf", d->bd_rtout);
417                 if (error == EINTR || error == ERESTART) {
418                         crit_exit();
419                         return(error);
420                 }
421                 if (error == EWOULDBLOCK) {
422                         /*
423                          * On a timeout, return what's in the buffer,
424                          * which may be nothing.  If there is something
425                          * in the store buffer, we can rotate the buffers.
426                          */
427                         if (d->bd_hbuf)
428                                 /*
429                                  * We filled up the buffer in between
430                                  * getting the timeout and arriving
431                                  * here, so we don't need to rotate.
432                                  */
433                                 break;
434
435                         if (d->bd_slen == 0) {
436                                 crit_exit();
437                                 return(0);
438                         }
439                         ROTATE_BUFFERS(d);
440                         break;
441                 }
442         }
443         /*
444          * At this point, we know we have something in the hold slot.
445          */
446         crit_exit();
447
448         /*
449          * Move data from hold buffer into user space.
450          * We know the entire buffer is transferred since
451          * we checked above that the read buffer is bpf_bufsize bytes.
452          */
453         error = uiomove(d->bd_hbuf, d->bd_hlen, ap->a_uio);
454
455         crit_enter();
456         d->bd_fbuf = d->bd_hbuf;
457         d->bd_hbuf = NULL;
458         d->bd_hlen = 0;
459         crit_exit();
460
461         return(error);
462 }
463
464
465 /*
466  * If there are processes sleeping on this descriptor, wake them up.
467  */
468 static void
469 bpf_wakeup(struct bpf_d *d)
470 {
471         if (d->bd_state == BPF_WAITING) {
472                 callout_stop(&d->bd_callout);
473                 d->bd_state = BPF_IDLE;
474         }
475         wakeup(d);
476         if (d->bd_async && d->bd_sig && d->bd_sigio)
477                 pgsigio(d->bd_sigio, d->bd_sig, 0);
478
479         get_mplock();
480         selwakeup(&d->bd_sel);
481         rel_mplock();
482         /* XXX */
483         d->bd_sel.si_pid = 0;
484 }
485
486 static void
487 bpf_timed_out(void *arg)
488 {
489         struct bpf_d *d = (struct bpf_d *)arg;
490
491         crit_enter();
492         if (d->bd_state == BPF_WAITING) {
493                 d->bd_state = BPF_TIMED_OUT;
494                 if (d->bd_slen != 0)
495                         bpf_wakeup(d);
496         }
497         crit_exit();
498 }
499
500 static  int
501 bpfwrite(struct dev_write_args *ap)
502 {
503         cdev_t dev = ap->a_head.a_dev;
504         struct bpf_d *d = dev->si_drv1;
505         struct ifnet *ifp;
506         struct mbuf *m;
507         int error;
508         static struct sockaddr dst;
509         int datlen;
510
511         if (d->bd_bif == NULL)
512                 return(ENXIO);
513
514         ifp = d->bd_bif->bif_ifp;
515
516         if (ap->a_uio->uio_resid == 0)
517                 return(0);
518
519         error = bpf_movein(ap->a_uio, (int)d->bd_bif->bif_dlt, &m,
520                            &dst, &datlen, d->bd_wfilter);
521         if (error)
522                 return(error);
523
524         if (datlen > ifp->if_mtu) {
525                 m_freem(m);
526                 return(EMSGSIZE);
527         }
528
529         if (d->bd_hdrcmplt)
530                 dst.sa_family = pseudo_AF_HDRCMPLT;
531
532         crit_enter();
533         lwkt_serialize_enter(ifp->if_serializer);
534         error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)NULL);
535         lwkt_serialize_exit(ifp->if_serializer);
536         crit_exit();
537         /*
538          * The driver frees the mbuf.
539          */
540         return(error);
541 }
542
543 /*
544  * Reset a descriptor by flushing its packet buffer and clearing the
545  * receive and drop counts.  Should be called at splimp.
546  */
547 static void
548 bpf_resetd(struct bpf_d *d)
549 {
550         if (d->bd_hbuf) {
551                 /* Free the hold buffer. */
552                 d->bd_fbuf = d->bd_hbuf;
553                 d->bd_hbuf = NULL;
554         }
555         d->bd_slen = 0;
556         d->bd_hlen = 0;
557         d->bd_rcount = 0;
558         d->bd_dcount = 0;
559 }
560
561 /*
562  *  FIONREAD            Check for read packet available.
563  *  SIOCGIFADDR         Get interface address - convenient hook to driver.
564  *  BIOCGBLEN           Get buffer len [for read()].
565  *  BIOCSETF            Set ethernet read filter.
566  *  BIOCSETWF           Set ethernet write filter.
567  *  BIOCFLUSH           Flush read packet buffer.
568  *  BIOCPROMISC         Put interface into promiscuous mode.
569  *  BIOCGDLT            Get link layer type.
570  *  BIOCGETIF           Get interface name.
571  *  BIOCSETIF           Set interface.
572  *  BIOCSRTIMEOUT       Set read timeout.
573  *  BIOCGRTIMEOUT       Get read timeout.
574  *  BIOCGSTATS          Get packet stats.
575  *  BIOCIMMEDIATE       Set immediate mode.
576  *  BIOCVERSION         Get filter language version.
577  *  BIOCGHDRCMPLT       Get "header already complete" flag
578  *  BIOCSHDRCMPLT       Set "header already complete" flag
579  *  BIOCGSEESENT        Get "see packets sent" flag
580  *  BIOCSSEESENT        Set "see packets sent" flag
581  *  BIOCLOCK            Set "locked" flag
582  */
583 /* ARGSUSED */
584 static int
585 bpfioctl(struct dev_ioctl_args *ap)
586 {
587         cdev_t dev = ap->a_head.a_dev;
588         struct bpf_d *d = dev->si_drv1;
589         int error = 0;
590
591         crit_enter();
592         if (d->bd_state == BPF_WAITING)
593                 callout_stop(&d->bd_callout);
594         d->bd_state = BPF_IDLE;
595         crit_exit();
596
597         if (d->bd_locked == 1) {
598                 switch (ap->a_cmd) {
599                 case BIOCGBLEN:
600                 case BIOCFLUSH:
601                 case BIOCGDLT:
602                 case BIOCGDLTLIST: 
603                 case BIOCGETIF:
604                 case BIOCGRTIMEOUT:
605                 case BIOCGSTATS:
606                 case BIOCVERSION:
607                 case BIOCGRSIG:
608                 case BIOCGHDRCMPLT:
609                 case FIONREAD:
610                 case BIOCLOCK:
611                 case BIOCSRTIMEOUT:
612                 case BIOCIMMEDIATE:
613                 case TIOCGPGRP:
614                         break;
615                 default:
616                         return (EPERM);
617                 }
618         }
619         switch (ap->a_cmd) {
620         default:
621                 error = EINVAL;
622                 break;
623
624         /*
625          * Check for read packet available.
626          */
627         case FIONREAD:
628                 {
629                         int n;
630
631                         crit_enter();
632                         n = d->bd_slen;
633                         if (d->bd_hbuf)
634                                 n += d->bd_hlen;
635                         crit_exit();
636
637                         *(int *)ap->a_data = n;
638                         break;
639                 }
640
641         case SIOCGIFADDR:
642                 {
643                         struct ifnet *ifp;
644
645                         if (d->bd_bif == NULL) {
646                                 error = EINVAL;
647                         } else {
648                                 ifp = d->bd_bif->bif_ifp;
649                                 lwkt_serialize_enter(ifp->if_serializer);
650                                 error = ifp->if_ioctl(ifp, ap->a_cmd,
651                                                       ap->a_data, ap->a_cred);
652                                 lwkt_serialize_exit(ifp->if_serializer);
653                         }
654                         break;
655                 }
656
657         /*
658          * Get buffer len [for read()].
659          */
660         case BIOCGBLEN:
661                 *(u_int *)ap->a_data = d->bd_bufsize;
662                 break;
663
664         /*
665          * Set buffer length.
666          */
667         case BIOCSBLEN:
668                 if (d->bd_bif != NULL) {
669                         error = EINVAL;
670                 } else {
671                         u_int size = *(u_int *)ap->a_data;
672
673                         if (size > bpf_maxbufsize)
674                                 *(u_int *)ap->a_data = size = bpf_maxbufsize;
675                         else if (size < BPF_MINBUFSIZE)
676                                 *(u_int *)ap->a_data = size = BPF_MINBUFSIZE;
677                         d->bd_bufsize = size;
678                 }
679                 break;
680
681         /*
682          * Set link layer read filter.
683          */
684         case BIOCSETF:
685         case BIOCSETWF:
686                 error = bpf_setf(d, (struct bpf_program *)ap->a_data, 
687                         ap->a_cmd);
688                 break;
689
690         /*
691          * Flush read packet buffer.
692          */
693         case BIOCFLUSH:
694                 crit_enter();
695                 bpf_resetd(d);
696                 crit_exit();
697                 break;
698
699         /*
700          * Put interface into promiscuous mode.
701          */
702         case BIOCPROMISC:
703                 if (d->bd_bif == NULL) {
704                         /*
705                          * No interface attached yet.
706                          */
707                         error = EINVAL;
708                         break;
709                 }
710                 crit_enter();
711                 if (d->bd_promisc == 0) {
712                         error = ifpromisc(d->bd_bif->bif_ifp, 1);
713                         if (error == 0)
714                                 d->bd_promisc = 1;
715                 }
716                 crit_exit();
717                 break;
718
719         /*
720          * Get device parameters.
721          */
722         case BIOCGDLT:
723                 if (d->bd_bif == NULL)
724                         error = EINVAL;
725                 else
726                         *(u_int *)ap->a_data = d->bd_bif->bif_dlt;
727                 break;
728
729         /*
730          * Get a list of supported data link types.
731          */
732         case BIOCGDLTLIST:
733                 if (d->bd_bif == NULL) {
734                         error = EINVAL;
735                 } else {
736                         error = bpf_getdltlist(d,
737                                 (struct bpf_dltlist *)ap->a_data);
738                 }
739                 break;
740
741         /*
742          * Set data link type.
743          */
744         case BIOCSDLT:
745                 if (d->bd_bif == NULL)
746                         error = EINVAL;
747                 else
748                         error = bpf_setdlt(d, *(u_int *)ap->a_data);
749                 break;
750
751         /*
752          * Get interface name.
753          */
754         case BIOCGETIF:
755                 if (d->bd_bif == NULL) {
756                         error = EINVAL;
757                 } else {
758                         struct ifnet *const ifp = d->bd_bif->bif_ifp;
759                         struct ifreq *const ifr = (struct ifreq *)ap->a_data;
760
761                         strlcpy(ifr->ifr_name, ifp->if_xname,
762                                 sizeof ifr->ifr_name);
763                 }
764                 break;
765
766         /*
767          * Set interface.
768          */
769         case BIOCSETIF:
770                 error = bpf_setif(d, (struct ifreq *)ap->a_data);
771                 break;
772
773         /*
774          * Set read timeout.
775          */
776         case BIOCSRTIMEOUT:
777                 {
778                         struct timeval *tv = (struct timeval *)ap->a_data;
779
780                         /*
781                          * Subtract 1 tick from tvtohz() since this isn't
782                          * a one-shot timer.
783                          */
784                         if ((error = itimerfix(tv)) == 0)
785                                 d->bd_rtout = tvtohz_low(tv);
786                         break;
787                 }
788
789         /*
790          * Get read timeout.
791          */
792         case BIOCGRTIMEOUT:
793                 {
794                         struct timeval *tv = (struct timeval *)ap->a_data;
795
796                         tv->tv_sec = d->bd_rtout / hz;
797                         tv->tv_usec = (d->bd_rtout % hz) * tick;
798                         break;
799                 }
800
801         /*
802          * Get packet stats.
803          */
804         case BIOCGSTATS:
805                 {
806                         struct bpf_stat *bs = (struct bpf_stat *)ap->a_data;
807
808                         bs->bs_recv = d->bd_rcount;
809                         bs->bs_drop = d->bd_dcount;
810                         break;
811                 }
812
813         /*
814          * Set immediate mode.
815          */
816         case BIOCIMMEDIATE:
817                 d->bd_immediate = *(u_int *)ap->a_data;
818                 break;
819
820         case BIOCVERSION:
821                 {
822                         struct bpf_version *bv = (struct bpf_version *)ap->a_data;
823
824                         bv->bv_major = BPF_MAJOR_VERSION;
825                         bv->bv_minor = BPF_MINOR_VERSION;
826                         break;
827                 }
828
829         /*
830          * Get "header already complete" flag
831          */
832         case BIOCGHDRCMPLT:
833                 *(u_int *)ap->a_data = d->bd_hdrcmplt;
834                 break;
835
836         /*
837          * Set "header already complete" flag
838          */
839         case BIOCSHDRCMPLT:
840                 d->bd_hdrcmplt = *(u_int *)ap->a_data ? 1 : 0;
841                 break;
842
843         /*
844          * Get "see sent packets" flag
845          */
846         case BIOCGSEESENT:
847                 *(u_int *)ap->a_data = d->bd_seesent;
848                 break;
849
850         /*
851          * Set "see sent packets" flag
852          */
853         case BIOCSSEESENT:
854                 d->bd_seesent = *(u_int *)ap->a_data;
855                 break;
856
857         case FIOASYNC:          /* Send signal on receive packets */
858                 d->bd_async = *(int *)ap->a_data;
859                 break;
860
861         case FIOSETOWN:
862                 error = fsetown(*(int *)ap->a_data, &d->bd_sigio);
863                 break;
864
865         case FIOGETOWN:
866                 *(int *)ap->a_data = fgetown(d->bd_sigio);
867                 break;
868
869         /* This is deprecated, FIOSETOWN should be used instead. */
870         case TIOCSPGRP:
871                 error = fsetown(-(*(int *)ap->a_data), &d->bd_sigio);
872                 break;
873
874         /* This is deprecated, FIOGETOWN should be used instead. */
875         case TIOCGPGRP:
876                 *(int *)ap->a_data = -fgetown(d->bd_sigio);
877                 break;
878
879         case BIOCSRSIG:         /* Set receive signal */
880                 {
881                         u_int sig;
882
883                         sig = *(u_int *)ap->a_data;
884
885                         if (sig >= NSIG)
886                                 error = EINVAL;
887                         else
888                                 d->bd_sig = sig;
889                         break;
890                 }
891         case BIOCGRSIG:
892                 *(u_int *)ap->a_data = d->bd_sig;
893                 break;
894         case BIOCLOCK:
895                 d->bd_locked = 1;
896                 break;
897         }
898         return(error);
899 }
900
901 /*
902  * Set d's packet filter program to fp.  If this file already has a filter,
903  * free it and replace it.  Returns EINVAL for bogus requests.
904  */
905 static int
906 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
907 {
908         struct bpf_insn *fcode, *old;
909         u_int wfilter, flen, size;
910
911         if (cmd == BIOCSETWF) {
912                 old = d->bd_wfilter;
913                 wfilter = 1;
914         } else {
915                 wfilter = 0;
916                 old = d->bd_rfilter;
917         }
918         if (fp->bf_insns == NULL) {
919                 if (fp->bf_len != 0)
920                         return(EINVAL);
921                 crit_enter();
922                 if (wfilter)
923                         d->bd_wfilter = NULL;
924                 else
925                         d->bd_rfilter = NULL;
926                 bpf_resetd(d);
927                 crit_exit();
928                 if (old != NULL)
929                         kfree(old, M_BPF);
930                 return(0);
931         }
932         flen = fp->bf_len;
933         if (flen > BPF_MAXINSNS)
934                 return(EINVAL);
935
936         size = flen * sizeof *fp->bf_insns;
937         fcode = (struct bpf_insn *)kmalloc(size, M_BPF, M_WAITOK);
938         if (copyin(fp->bf_insns, fcode, size) == 0 &&
939             bpf_validate(fcode, (int)flen)) {
940                 crit_enter();
941                 if (wfilter)
942                         d->bd_wfilter = fcode;
943                 else
944                         d->bd_rfilter = fcode;
945                 bpf_resetd(d);
946                 crit_exit();
947                 if (old != NULL)
948                         kfree(old, M_BPF);
949
950                 return(0);
951         }
952         kfree(fcode, M_BPF);
953         return(EINVAL);
954 }
955
956 /*
957  * Detach a file from its current interface (if attached at all) and attach
958  * to the interface indicated by the name stored in ifr.
959  * Return an errno or 0.
960  */
961 static int
962 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
963 {
964         struct bpf_if *bp;
965         int error;
966         struct ifnet *theywant;
967
968         theywant = ifunit(ifr->ifr_name);
969         if (theywant == NULL)
970                 return(ENXIO);
971
972         /*
973          * Look through attached interfaces for the named one.
974          */
975         for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
976                 struct ifnet *ifp = bp->bif_ifp;
977
978                 if (ifp == NULL || ifp != theywant)
979                         continue;
980                 /* skip additional entry */
981                 if (bp->bif_driverp != &ifp->if_bpf)
982                         continue;
983                 /*
984                  * We found the requested interface.
985                  * If it's not up, return an error.
986                  * Allocate the packet buffers if we need to.
987                  * If we're already attached to requested interface,
988                  * just flush the buffer.
989                  */
990                 if (!(ifp->if_flags & IFF_UP))
991                         return(ENETDOWN);
992
993                 if (d->bd_sbuf == NULL) {
994                         error = bpf_allocbufs(d);
995                         if (error != 0)
996                                 return(error);
997                 }
998                 crit_enter();
999                 if (bp != d->bd_bif) {
1000                         if (d->bd_bif != NULL) {
1001                                 /*
1002                                  * Detach if attached to something else.
1003                                  */
1004                                 bpf_detachd(d);
1005                         }
1006
1007                         bpf_attachd(d, bp);
1008                 }
1009                 bpf_resetd(d);
1010                 crit_exit();
1011                 return(0);
1012         }
1013
1014         /* Not found. */
1015         return(ENXIO);
1016 }
1017
1018 /*
1019  * Support for select() and poll() system calls
1020  *
1021  * Return true iff the specific operation will not block indefinitely.
1022  * Otherwise, return false but make a note that a selwakeup() must be done.
1023  */
1024 int
1025 bpfpoll(struct dev_poll_args *ap)
1026 {
1027         cdev_t dev = ap->a_head.a_dev;
1028         struct bpf_d *d;
1029         int revents;
1030
1031         d = dev->si_drv1;
1032         if (d->bd_bif == NULL)
1033                 return(ENXIO);
1034
1035         revents = ap->a_events & (POLLOUT | POLLWRNORM);
1036         crit_enter();
1037         if (ap->a_events & (POLLIN | POLLRDNORM)) {
1038                 /*
1039                  * An imitation of the FIONREAD ioctl code.
1040                  * XXX not quite.  An exact imitation:
1041                  *      if (d->b_slen != 0 ||
1042                  *          (d->bd_hbuf != NULL && d->bd_hlen != 0)
1043                  */
1044                 if (d->bd_hlen != 0 ||
1045                     ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1046                     d->bd_slen != 0)) {
1047                         revents |= ap->a_events & (POLLIN | POLLRDNORM);
1048                 } else {
1049                         selrecord(curthread, &d->bd_sel);
1050                         /* Start the read timeout if necessary. */
1051                         if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1052                                 callout_reset(&d->bd_callout, d->bd_rtout,
1053                                     bpf_timed_out, d);
1054                                 d->bd_state = BPF_WAITING;
1055                         }
1056                 }
1057         }
1058         crit_exit();
1059         ap->a_events = revents;
1060         return(0);
1061 }
1062
1063 /*
1064  * Process the packet pkt of length pktlen.  The packet is parsed
1065  * by each listener's filter, and if accepted, stashed into the
1066  * corresponding buffer.
1067  */
1068 void
1069 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1070 {
1071         struct bpf_d *d;
1072         struct timeval tv;
1073         int gottime = 0;
1074         u_int slen;
1075
1076         /*
1077          * Note that the ipl does not have to be raised at this point.
1078          * The only problem that could arise here is that if two different
1079          * interfaces shared any data.  This is not the case.
1080          */
1081         SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1082                 ++d->bd_rcount;
1083                 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1084                 if (slen != 0) {
1085                         if (!gottime) {
1086                                 microtime(&tv);
1087                                 gottime = 1;
1088                         }
1089                         catchpacket(d, pkt, pktlen, slen, ovbcopy, &tv);
1090                 }
1091         }
1092 }
1093
1094 /*
1095  * Copy data from an mbuf chain into a buffer.  This code is derived
1096  * from m_copydata in sys/uipc_mbuf.c.
1097  */
1098 static void
1099 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1100 {
1101         const struct mbuf *m;
1102         u_int count;
1103         u_char *dst;
1104
1105         m = src_arg;
1106         dst = dst_arg;
1107         while (len > 0) {
1108                 if (m == NULL)
1109                         panic("bpf_mcopy");
1110                 count = min(m->m_len, len);
1111                 bcopy(mtod(m, void *), dst, count);
1112                 m = m->m_next;
1113                 dst += count;
1114                 len -= count;
1115         }
1116 }
1117
1118 /*
1119  * Process the packet in the mbuf chain m.  The packet is parsed by each
1120  * listener's filter, and if accepted, stashed into the corresponding
1121  * buffer.
1122  */
1123 void
1124 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1125 {
1126         struct bpf_d *d;
1127         u_int pktlen, slen;
1128         struct timeval tv;
1129         int gottime = 0;
1130
1131         /* Don't compute pktlen, if no descriptor is attached. */
1132         if (SLIST_EMPTY(&bp->bif_dlist))
1133                 return;
1134
1135         pktlen = m_lengthm(m, NULL);
1136
1137         SLIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1138                 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1139                         continue;
1140                 ++d->bd_rcount;
1141                 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1142                 if (slen != 0) {
1143                         if (!gottime) {
1144                                 microtime(&tv);
1145                                 gottime = 1;
1146                         }
1147                         catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy,
1148                                     &tv);
1149                 }
1150         }
1151 }
1152
1153 void
1154 bpf_mtap_family(struct bpf_if *bp, struct mbuf *m, sa_family_t family)
1155 {
1156         u_int family4;
1157
1158         KKASSERT(family != AF_UNSPEC);
1159
1160         family4 = (u_int)family;
1161         bpf_ptap(bp, m, &family4, sizeof(family4));
1162 }
1163
1164 /*
1165  * Process the packet in the mbuf chain m with the header in m prepended.
1166  * The packet is parsed by each listener's filter, and if accepted,
1167  * stashed into the corresponding buffer.
1168  */
1169 void
1170 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen)
1171 {
1172         struct mbuf mb;
1173
1174         /*
1175          * Craft on-stack mbuf suitable for passing to bpf_mtap.
1176          * Note that we cut corners here; we only setup what's
1177          * absolutely needed--this mbuf should never go anywhere else.
1178          */
1179         mb.m_next = m;
1180         mb.m_data = __DECONST(void *, data); /* LINTED */
1181         mb.m_len = dlen;
1182         mb.m_pkthdr.rcvif = m->m_pkthdr.rcvif;
1183
1184         bpf_mtap(bp, &mb);
1185 }
1186
1187 /*
1188  * Move the packet data from interface memory (pkt) into the
1189  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
1190  * otherwise 0.  "copy" is the routine called to do the actual data
1191  * transfer.  bcopy is passed in to copy contiguous chunks, while
1192  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1193  * pkt is really an mbuf.
1194  */
1195 static void
1196 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1197             void (*cpfn)(const void *, void *, size_t),
1198             const struct timeval *tv)
1199 {
1200         struct bpf_hdr *hp;
1201         int totlen, curlen;
1202         int hdrlen = d->bd_bif->bif_hdrlen;
1203         /*
1204          * Figure out how many bytes to move.  If the packet is
1205          * greater or equal to the snapshot length, transfer that
1206          * much.  Otherwise, transfer the whole packet (unless
1207          * we hit the buffer size limit).
1208          */
1209         totlen = hdrlen + min(snaplen, pktlen);
1210         if (totlen > d->bd_bufsize)
1211                 totlen = d->bd_bufsize;
1212
1213         /*
1214          * Round up the end of the previous packet to the next longword.
1215          */
1216         curlen = BPF_WORDALIGN(d->bd_slen);
1217         if (curlen + totlen > d->bd_bufsize) {
1218                 /*
1219                  * This packet will overflow the storage buffer.
1220                  * Rotate the buffers if we can, then wakeup any
1221                  * pending reads.
1222                  */
1223                 if (d->bd_fbuf == NULL) {
1224                         /*
1225                          * We haven't completed the previous read yet,
1226                          * so drop the packet.
1227                          */
1228                         ++d->bd_dcount;
1229                         return;
1230                 }
1231                 ROTATE_BUFFERS(d);
1232                 bpf_wakeup(d);
1233                 curlen = 0;
1234         } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) {
1235                 /*
1236                  * Immediate mode is set, or the read timeout has
1237                  * already expired during a select call.  A packet
1238                  * arrived, so the reader should be woken up.
1239                  */
1240                 bpf_wakeup(d);
1241         }
1242
1243         /*
1244          * Append the bpf header.
1245          */
1246         hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1247         hp->bh_tstamp = *tv;
1248         hp->bh_datalen = pktlen;
1249         hp->bh_hdrlen = hdrlen;
1250         /*
1251          * Copy the packet data into the store buffer and update its length.
1252          */
1253         (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1254         d->bd_slen = curlen + totlen;
1255 }
1256
1257 /*
1258  * Initialize all nonzero fields of a descriptor.
1259  */
1260 static int
1261 bpf_allocbufs(struct bpf_d *d)
1262 {
1263         d->bd_fbuf = kmalloc(d->bd_bufsize, M_BPF, M_WAITOK);
1264         d->bd_sbuf = kmalloc(d->bd_bufsize, M_BPF, M_WAITOK);
1265         d->bd_slen = 0;
1266         d->bd_hlen = 0;
1267         return(0);
1268 }
1269
1270 /*
1271  * Free buffers and packet filter program currently in use by a descriptor.
1272  * Called on close.
1273  */
1274 static void
1275 bpf_freed(struct bpf_d *d)
1276 {
1277         /*
1278          * We don't need to lock out interrupts since this descriptor has
1279          * been detached from its interface and it yet hasn't been marked
1280          * free.
1281          */
1282         if (d->bd_sbuf != NULL) {
1283                 kfree(d->bd_sbuf, M_BPF);
1284                 if (d->bd_hbuf != NULL)
1285                         kfree(d->bd_hbuf, M_BPF);
1286                 if (d->bd_fbuf != NULL)
1287                         kfree(d->bd_fbuf, M_BPF);
1288         }
1289         if (d->bd_rfilter)
1290                 kfree(d->bd_rfilter, M_BPF);
1291         if (d->bd_wfilter)
1292                 kfree(d->bd_wfilter, M_BPF);
1293 }
1294
1295 /*
1296  * Attach an interface to bpf.  ifp is a pointer to the structure
1297  * defining the interface to be attached, dlt is the link layer type,
1298  * and hdrlen is the fixed size of the link header (variable length
1299  * headers are not yet supported).
1300  */
1301 void
1302 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1303 {
1304         bpfattach_dlt(ifp, dlt, hdrlen, &ifp->if_bpf);
1305 }
1306
1307 void
1308 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1309 {
1310         struct bpf_if *bp;
1311
1312         bp = kmalloc(sizeof *bp, M_BPF, M_WAITOK | M_ZERO);
1313
1314         SLIST_INIT(&bp->bif_dlist);
1315         bp->bif_ifp = ifp;
1316         bp->bif_dlt = dlt;
1317         bp->bif_driverp = driverp;
1318         *bp->bif_driverp = NULL;
1319
1320         bp->bif_next = bpf_iflist;
1321         bpf_iflist = bp;
1322
1323         /*
1324          * Compute the length of the bpf header.  This is not necessarily
1325          * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1326          * that the network layer header begins on a longword boundary (for
1327          * performance reasons and to alleviate alignment restrictions).
1328          */
1329         bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1330
1331         if (bootverbose)
1332                 if_printf(ifp, "bpf attached\n");
1333 }
1334
1335 /*
1336  * Detach bpf from an interface.  This involves detaching each descriptor
1337  * associated with the interface, and leaving bd_bif NULL.  Notify each
1338  * descriptor as it's detached so that any sleepers wake up and get
1339  * ENXIO.
1340  */
1341 void
1342 bpfdetach(struct ifnet *ifp)
1343 {
1344         struct bpf_if *bp, *bp_prev;
1345         struct bpf_d *d;
1346
1347         crit_enter();
1348
1349         /* Locate BPF interface information */
1350         bp_prev = NULL;
1351         for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1352                 if (ifp == bp->bif_ifp)
1353                         break;
1354                 bp_prev = bp;
1355         }
1356
1357         /* Interface wasn't attached */
1358         if (bp->bif_ifp == NULL) {
1359                 crit_exit();
1360                 kprintf("bpfdetach: %s was not attached\n", ifp->if_xname);
1361                 return;
1362         }
1363
1364         while ((d = SLIST_FIRST(&bp->bif_dlist)) != NULL) {
1365                 bpf_detachd(d);
1366                 bpf_wakeup(d);
1367         }
1368
1369         if (bp_prev != NULL)
1370                 bp_prev->bif_next = bp->bif_next;
1371         else
1372                 bpf_iflist = bp->bif_next;
1373
1374         kfree(bp, M_BPF);
1375
1376         crit_exit();
1377 }
1378
1379 /*
1380  * Get a list of available data link type of the interface.
1381  */
1382 static int
1383 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1384 {
1385         int n, error;
1386         struct ifnet *ifp;
1387         struct bpf_if *bp;
1388
1389         ifp = d->bd_bif->bif_ifp;
1390         n = 0;
1391         error = 0;
1392         for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1393                 if (bp->bif_ifp != ifp)
1394                         continue;
1395                 if (bfl->bfl_list != NULL) {
1396                         if (n >= bfl->bfl_len) {
1397                                 return (ENOMEM);
1398                         }
1399                         error = copyout(&bp->bif_dlt,
1400                             bfl->bfl_list + n, sizeof(u_int));
1401                 }
1402                 n++;
1403         }
1404         bfl->bfl_len = n;
1405         return(error);
1406 }
1407
1408 /*
1409  * Set the data link type of a BPF instance.
1410  */
1411 static int
1412 bpf_setdlt(struct bpf_d *d, u_int dlt)
1413 {
1414         int error, opromisc;
1415         struct ifnet *ifp;
1416         struct bpf_if *bp;
1417
1418         if (d->bd_bif->bif_dlt == dlt)
1419                 return (0);
1420         ifp = d->bd_bif->bif_ifp;
1421         for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1422                 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1423                         break;
1424         }
1425         if (bp != NULL) {
1426                 opromisc = d->bd_promisc;
1427                 crit_enter();
1428                 bpf_detachd(d);
1429                 bpf_attachd(d, bp);
1430                 bpf_resetd(d);
1431                 if (opromisc) {
1432                         error = ifpromisc(bp->bif_ifp, 1);
1433                         if (error) {
1434                                 if_printf(bp->bif_ifp,
1435                                         "bpf_setdlt: ifpromisc failed (%d)\n",
1436                                         error);
1437                         } else {
1438                                 d->bd_promisc = 1;
1439                         }
1440                 }
1441                 crit_exit();
1442         }
1443         return(bp == NULL ? EINVAL : 0);
1444 }
1445
1446 static void
1447 bpf_drvinit(void *unused)
1448 {
1449         dev_ops_add(&bpf_ops, 0, 0);
1450 }
1451
1452 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1453
1454 #else /* !BPF */
1455 /*
1456  * NOP stubs to allow bpf-using drivers to load and function.
1457  *
1458  * A 'better' implementation would allow the core bpf functionality
1459  * to be loaded at runtime.
1460  */
1461
1462 void
1463 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1464 {
1465 }
1466
1467 void
1468 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1469 {
1470 }
1471
1472 void
1473 bpf_ptap(struct bpf_if *bp, struct mbuf *m, const void *data, u_int dlen)
1474 {
1475 }
1476
1477 void
1478 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1479 {
1480 }
1481
1482 void
1483 bpfattach_dlt(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1484 {
1485 }
1486
1487 void
1488 bpfdetach(struct ifnet *ifp)
1489 {
1490 }
1491
1492 u_int
1493 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1494 {
1495         return -1;      /* "no filter" behaviour */
1496 }
1497
1498 #endif /* !BPF */