Deleted Added
sdiff udiff text old ( 48645 ) new ( 49827 )
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.

--- 23 unchanged lines hidden (view full) ---

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 * $Id: bpf.c,v 1.52 1999/07/06 19:23:10 des Exp $
41 */
42
43#include "bpf.h"
44
45#ifndef __GNUC__
46#define inline
47#else
48#define inline __inline

--- 24 unchanged lines hidden (view full) ---

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();

--- 11 unchanged lines hidden (view full) ---

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,

--- 207 unchanged lines hidden (view full) ---

327 if (bp->bif_dlist == 0)
328 /*
329 * Let the driver know that there are no more listeners.
330 */
331 d->bd_bif->bif_ifp->if_bpf = 0;
332 d->bd_bif = 0;
333}
334
335/*
336 * Open ethernet device. Returns ENXIO for illegal minor device number,
337 * EBUSY if file is open by another process.
338 */
339/* ARGSUSED */
340static int
341bpfopen(dev, flags, fmt, p)
342 dev_t dev;
343 int flags;
344 int fmt;
345 struct proc *p;
346{
347 register struct bpf_d *d;
348
349 if (p->p_prison)
350 return (EPERM);
351
352 d = dev->si_drv1;
353 /*
354 * Each minor can be opened by only one process. If the requested
355 * minor is in use, return EBUSY.
356 */
357 if (d)
358 return (EBUSY);
359 make_dev(&bpf_cdevsw, minor(dev), 0, 0, 0600, "bpf%d", lminor(dev));
360 MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK);
361 bzero(d, sizeof(*d));
362 dev->si_drv1 = d;
363 d->bd_bufsize = bpf_bufsize;
364 d->bd_sig = SIGIO;
365
366 return (0);
367}
368
369/*
370 * Close the descriptor by detaching it from its interface,
371 * deallocating its buffers, and marking it free.
372 */
373/* ARGSUSED */
374static int
375bpfclose(dev, flags, fmt, p)
376 dev_t dev;
377 int flags;
378 int fmt;
379 struct proc *p;
380{
381 register struct bpf_d *d = dev->si_drv1;
382 register int s;
383
384 funsetown(d->bd_sigio);
385 s = splimp();
386 if (d->bd_bif)
387 bpf_detachd(d);
388 splx(s);
389 bpf_freed(d);
390 dev->si_drv1 = 0;
391 FREE(d, M_BPF);
392
393 return (0);
394}
395
396/*
397 * Support for SunOS, which does not have tsleep.
398 */
399#if BSD < 199103

--- 47 unchanged lines hidden (view full) ---

447 * bpfread - read next chunk of packets from buffers
448 */
449static int
450bpfread(dev, uio, ioflag)
451 dev_t dev;
452 register struct uio *uio;
453 int ioflag;
454{
455 register struct bpf_d *d = dev->si_drv1;
456 int error;
457 int s;
458
459 /*
460 * Restrict application to use a buffer the same size as
461 * as kernel buffers.
462 */
463 if (uio->uio_resid != d->bd_bufsize)

--- 93 unchanged lines hidden (view full) ---

557}
558
559static int
560bpfwrite(dev, uio, ioflag)
561 dev_t dev;
562 struct uio *uio;
563 int ioflag;
564{
565 register struct bpf_d *d = dev->si_drv1;
566 struct ifnet *ifp;
567 struct mbuf *m;
568 int error, s;
569 static struct sockaddr dst;
570 int datlen;
571
572 if (d->bd_bif == 0)
573 return (ENXIO);

--- 62 unchanged lines hidden (view full) ---

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

--- 235 unchanged lines hidden (view full) ---

888 if (fp->bf_insns == 0) {
889 if (fp->bf_len != 0)
890 return (EINVAL);
891 s = splimp();
892 d->bd_filter = 0;
893 reset_d(d);
894 splx(s);
895 if (old != 0)
896 free((caddr_t)old, M_BPF);
897 return (0);
898 }
899 flen = fp->bf_len;
900 if (flen > BPF_MAXINSNS)
901 return (EINVAL);
902
903 size = flen * sizeof(*fp->bf_insns);
904 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
905 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
906 bpf_validate(fcode, (int)flen)) {
907 s = splimp();
908 d->bd_filter = fcode;
909 reset_d(d);
910 splx(s);
911 if (old != 0)
912 free((caddr_t)old, M_BPF);
913
914 return (0);
915 }
916 free((caddr_t)fcode, M_BPF);
917 return (EINVAL);
918}
919
920/*
921 * Detach a file from its current interface (if attached at all) and attach
922 * to the interface indicated by the name stored in ifr.
923 * Return an errno or 0.
924 */

--- 85 unchanged lines hidden (view full) ---

1010{
1011 register struct bpf_d *d;
1012 register int s;
1013 int revents = 0;
1014
1015 /*
1016 * An imitation of the FIONREAD ioctl code.
1017 */
1018 d = dev->si_drv1;
1019
1020 s = splimp();
1021 if (events & (POLLIN | POLLRDNORM)) {
1022 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1023 revents |= events & (POLLIN | POLLRDNORM);
1024 else
1025 selrecord(p, &d->bd_sel);
1026 }

--- 161 unchanged lines hidden (view full) ---

1188
1189/*
1190 * Initialize all nonzero fields of a descriptor.
1191 */
1192static int
1193bpf_allocbufs(d)
1194 register struct bpf_d *d;
1195{
1196 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1197 if (d->bd_fbuf == 0)
1198 return (ENOBUFS);
1199
1200 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1201 if (d->bd_sbuf == 0) {
1202 free(d->bd_fbuf, M_BPF);
1203 return (ENOBUFS);
1204 }
1205 d->bd_slen = 0;
1206 d->bd_hlen = 0;
1207 return (0);
1208}
1209
1210/*

--- 5 unchanged lines hidden (view full) ---

1216 register struct bpf_d *d;
1217{
1218 /*
1219 * We don't need to lock out interrupts since this descriptor has
1220 * been detached from its interface and it yet hasn't been marked
1221 * free.
1222 */
1223 if (d->bd_sbuf != 0) {
1224 free(d->bd_sbuf, M_BPF);
1225 if (d->bd_hbuf != 0)
1226 free(d->bd_hbuf, M_BPF);
1227 if (d->bd_fbuf != 0)
1228 free(d->bd_fbuf, M_BPF);
1229 }
1230 if (d->bd_filter)
1231 free((caddr_t)d->bd_filter, M_BPF);
1232}
1233
1234/*
1235 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1236 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1237 * size of the link header (variable length headers not yet supported).
1238 */
1239void
1240bpfattach(ifp, dlt, hdrlen)
1241 struct ifnet *ifp;
1242 u_int dlt, hdrlen;
1243{
1244 struct bpf_if *bp;
1245 bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_DONTWAIT);
1246 if (bp == 0)
1247 panic("bpfattach");
1248
1249 bp->bif_dlist = 0;
1250 bp->bif_ifp = ifp;
1251 bp->bif_dlt = dlt;
1252
1253 bp->bif_next = bpf_iflist;

--- 4 unchanged lines hidden (view full) ---

1258 /*
1259 * Compute the length of the bpf header. This is not necessarily
1260 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1261 * that the network layer header begins on a longword boundary (for
1262 * performance reasons and to alleviate alignment restrictions).
1263 */
1264 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1265
1266 if (bootverbose)
1267 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1268}
1269
1270static void bpf_drvinit __P((void *unused));
1271
1272static void
1273bpf_drvinit(unused)
1274 void *unused;
1275{
1276
1277 cdevsw_add(&bpf_cdevsw);
1278}
1279
1280SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1281
1282#else /* !BPF */
1283/*
1284 * NOP stubs to allow bpf-using drivers to load and function.
1285 *

--- 28 unchanged lines hidden (view full) ---

1314 register struct bpf_insn *pc;
1315 register u_char *p;
1316 u_int wirelen;
1317 register u_int buflen;
1318{
1319 return -1; /* "no filter" behaviour */
1320}
1321
1322#endif /* !BPF */