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