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