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