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