]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Drivers/network/AppleBPF/bpf.c
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1990, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)bpf.c 8.4 (Berkeley) 1/9/95
61 #include <net/bpfilter.h>
62 #endif /* not BPFDRV */
65 #include <sys/param.h>
66 #include <sys/systm.h>
69 #include <machine/spl.h>
77 #include <sys/ioctl.h>
80 #if defined(sparc) && BSD < 199103
81 #include <sys/stream.h>
86 #include <sys/protosw.h>
87 #include <sys/socket.h>
91 #include <net/bpfdesc.h>
93 #include <sys/errno.h>
95 #include <netinet/in.h>
96 #include <netinet/if_ether.h>
97 #include <sys/kernel.h>
100 * Older BSDs don't have kernel malloc.
104 static caddr_t
bpf_alloc();
105 #include <net/bpf_compat.h>
106 #define BPF_BUFSIZE (MCLBYTES-8)
107 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
109 #include <sys/malloc.h>
110 #define BPF_BUFSIZE 4096
111 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
114 #define PRINET 26 /* interruptible */
117 * The default read buffer size is patchable.
120 int bpf_bufsize
= BPF_BUFSIZE
;
122 extern int bpf_bufsize
;
126 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
127 * bpf_dtab holds the descriptors, indexed by minor device #
130 extern struct bpf_if
*bpf_iflist
;
131 extern struct bpf_d
*bpf_dtab
;
132 extern int nbpfilter
;
134 struct bpf_if
*bpf_iflist
;
135 struct bpf_d bpf_dtab
[NBPFILTER
];
136 int nbpfilter
= NBPFILTER
;
142 * bpfilterattach() is called at boot time in new systems. We do
143 * nothing here since old systems will not call this.
154 static int bpf_allocbufs
__P((struct bpf_d
*));
155 static int bpf_allocbufs
__P((struct bpf_d
*));
156 static void bpf_freed
__P((struct bpf_d
*));
157 static void bpf_freed
__P((struct bpf_d
*));
158 static void bpf_ifname
__P((struct ifnet
*, struct ifreq
*));
159 static void bpf_ifname
__P((struct ifnet
*, struct ifreq
*));
160 static void bpf_mcopy
__P((const void *, void *, u_int
));
161 static int bpf_movein
__P((struct uio
*, int,
162 struct mbuf
**, struct sockaddr
*, int *));
163 static int bpf_setif
__P((struct bpf_d
*, struct ifreq
*));
164 static int bpf_setif
__P((struct bpf_d
*, struct ifreq
*));
166 bpf_wakeup
__P((struct bpf_d
*));
167 static void catchpacket
__P((struct bpf_d
*, u_char
*, u_int
,
168 u_int
, void (*)(const void *, void *, u_int
)));
169 static void reset_d
__P((struct bpf_d
*));
173 bpf_movein(uio
, linktype
, mp
, sockp
, datlen
)
174 register struct uio
*uio
;
175 int linktype
, *datlen
;
176 register struct mbuf
**mp
;
177 register struct sockaddr
*sockp
;
185 * Build a sockaddr based on the data link layer type.
186 * We do this at this level because the ethernet header
187 * is copied directly into the data field of the sockaddr.
188 * In the case of SLIP, there is no header and the packet
189 * is forwarded as is.
190 * Also, we are careful to leave room at the front of the mbuf
191 * for the link level header.
196 sockp
->sa_family
= AF_INET
;
201 sockp
->sa_family
= AF_UNSPEC
;
206 sockp
->sa_family
= AF_UNSPEC
;
207 /* XXX Would MAXLINKHDR be better? */
208 hlen
= sizeof(struct ether_header
);
212 sockp
->sa_family
= AF_UNSPEC
;
213 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
218 sockp
->sa_family
= AF_UNSPEC
;
226 len
= uio
->uio_resid
;
227 *datlen
= len
- hlen
;
228 if ((unsigned)len
> MCLBYTES
)
231 MGETHDR(m
, M_WAIT
, MT_DATA
);
237 if ((m
->m_flags
& M_EXT
) == 0) {
240 if (m
->m_len
!= MCLBYTES
) {
247 m
->m_pkthdr
.len
= len
;
250 * Make room for link header.
254 m
->m_pkthdr
.len
-= hlen
;
256 m
->m_data
+= hlen
; /* XXX */
260 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
264 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
273 * Attach file to the bpf interface, i.e. make d listen on bp.
274 * Must be called at splimp.
282 * Point d at bp, and add d to the interface's list of listeners.
283 * Finally, point the driver's bpf cookie at the interface so
284 * it will divert packets to bpf.
287 d
->bd_next
= bp
->bif_dlist
;
290 /* *bp->bif_driverp = bp; */
294 * Detach a file from its interface.
305 * Check if this descriptor had requested promiscuous mode.
306 * If so, turn it off.
310 if (ifpromisc(bp
->bif_ifp
, 0))
312 * Something is really wrong if we were able to put
313 * the driver into promiscuous mode, but can't
316 panic("bpf: ifpromisc failed");
318 /* Remove d from the interface's descriptor list. */
323 panic("bpf_detachd: descriptor not in list");
326 if (bp
->bif_dlist
== 0)
328 * Let the driver know that there are no more listeners.
330 /* *d->bd_bif->bif_driverp = 0 */;
337 * Open ethernet device. Returns ENXIO for illegal minor device number,
338 * EBUSY if file is open by another process.
346 register struct bpf_d
*d
;
348 if (minor(dev
) >= nbpfilter
)
351 * Each minor can be opened by only one process. If the requested
352 * minor is in use, return EBUSY.
354 d
= &bpf_dtab
[minor(dev
)];
358 /* Mark "free" and do most initialization. */
359 bzero((char *)d
, sizeof(*d
));
360 d
->bd_bufsize
= bpf_bufsize
;
366 * Close the descriptor by detaching it from its interface,
367 * deallocating its buffers, and marking it free.
375 register struct bpf_d
*d
= &bpf_dtab
[minor(dev
)];
388 * Support for SunOS, which does not have tsleep.
395 struct bpf_d
*d
= (struct bpf_d
*)arg
;
400 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
404 register struct bpf_d
*d
;
406 register int rto
= d
->bd_rtout
;
411 timeout(bpf_timeout
, (caddr_t
)d
, rto
);
413 st
= sleep((caddr_t
)d
, PRINET
|PCATCH
);
415 if (d
->bd_timedout
== 0)
416 untimeout(bpf_timeout
, (caddr_t
)d
);
420 return (st
!= 0) ? EINTR
: 0;
423 #define BPF_SLEEP tsleep
427 * Rotate the packet buffers in descriptor d. Move the store buffer
428 * into the hold slot, and the free buffer into the store slot.
429 * Zero the length of the new store buffer.
431 #define ROTATE_BUFFERS(d) \
432 (d)->bd_hbuf = (d)->bd_sbuf; \
433 (d)->bd_hlen = (d)->bd_slen; \
434 (d)->bd_sbuf = (d)->bd_fbuf; \
438 * bpfread - read next chunk of packets from buffers
443 register struct uio
*uio
;
445 register struct bpf_d
*d
= &bpf_dtab
[minor(dev
)];
450 * Restrict application to use a buffer the same size as
453 if (uio
->uio_resid
!= d
->bd_bufsize
)
458 * If the hold buffer is empty, then do a timed sleep, which
459 * ends when the timeout expires or when enough packets
460 * have arrived to fill the store buffer.
462 while (d
->bd_hbuf
== 0) {
463 if (d
->bd_immediate
&& d
->bd_slen
!= 0) {
465 * A packet(s) either arrived since the previous
466 * read or arrived while we were asleep.
467 * Rotate the buffers and return what's here.
472 error
= BPF_SLEEP((caddr_t
)d
, PRINET
|PCATCH
, "bpf",
474 if (error
== EINTR
|| error
== ERESTART
) {
478 if (error
== EWOULDBLOCK
) {
480 * On a timeout, return what's in the buffer,
481 * which may be nothing. If there is something
482 * in the store buffer, we can rotate the buffers.
486 * We filled up the buffer in between
487 * getting the timeout and arriving
488 * here, so we don't need to rotate.
492 if (d
->bd_slen
== 0) {
501 * At this point, we know we have something in the hold slot.
506 * Move data from hold buffer into user space.
507 * We know the entire buffer is transferred since
508 * we checked above that the read buffer is bpf_bufsize bytes.
510 error
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
);
513 d
->bd_fbuf
= d
->bd_hbuf
;
523 * If there are processes sleeping on this descriptor, wake them up.
527 register struct bpf_d
*d
;
530 selwakeup(&d
->bd_sel
);
531 d
->bd_sel
.si_thread
= 0;
539 register struct bpf_d
*d
= &bpf_dtab
[minor(dev
)];
543 static struct sockaddr dst
;
549 ifp
= d
->bd_bif
->bif_ifp
;
551 if (uio
->uio_resid
== 0)
554 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, &m
, &dst
, &datlen
);
558 if (datlen
> ifp
->if_mtu
)
563 error
= (*ifp
->if_output
)(ifp
, m
, &dst
, (struct rtentry
*)0);
565 error
= (*ifp
->if_output
)(ifp
, m
, &dst
);
569 * The driver frees the mbuf.
575 * Reset a descriptor by flushing its packet buffer and clearing the
576 * receive and drop counts. Should be called at splimp.
583 /* Free the hold buffer. */
584 d
->bd_fbuf
= d
->bd_hbuf
;
594 * FIONREAD Check for read packet available.
595 * SIOCGIFADDR Get interface address - convenient hook to driver.
596 * BIOCGBLEN Get buffer len [for read()].
597 * BIOCSETF Set ethernet read filter.
598 * BIOCFLUSH Flush read packet buffer.
599 * BIOCPROMISC Put interface into promiscuous mode.
600 * BIOCGDLT Get link layer type.
601 * BIOCGETIF Get interface name.
602 * BIOCSETIF Set interface.
603 * BIOCSRTIMEOUT Set read timeout.
604 * BIOCGRTIMEOUT Get read timeout.
605 * BIOCGSTATS Get packet stats.
606 * BIOCIMMEDIATE Set immediate mode.
607 * BIOCVERSION Get filter language version.
611 bpfioctl(dev
, cmd
, addr
, flag
)
617 register struct bpf_d
*d
= &bpf_dtab
[minor(dev
)];
627 * Check for read packet available.
650 ifp
= d
->bd_bif
->bif_ifp
;
651 error
= (*ifp
->if_ioctl
)(ifp
, cmd
, addr
);
657 * Get buffer len [for read()].
660 *(u_int
*)addr
= d
->bd_bufsize
;
673 register u_int size
= *(u_int
*)addr
;
675 if (size
> BPF_MAXBUFSIZE
)
676 *(u_int
*)addr
= size
= BPF_MAXBUFSIZE
;
677 else if (size
< BPF_MINBUFSIZE
)
678 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
679 d
->bd_bufsize
= size
;
685 * Set link layer read filter.
688 error
= bpf_setf(d
, (struct bpf_program
*)addr
);
692 * Flush read packet buffer.
701 * Put interface into promiscuous mode.
704 if (d
->bd_bif
== 0) {
706 * No interface attached yet.
712 if (d
->bd_promisc
== 0) {
713 error
= ifpromisc(d
->bd_bif
->bif_ifp
, 1);
721 * Get device parameters.
727 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
731 * Set interface name.
737 bpf_ifname(d
->bd_bif
->bif_ifp
, (struct ifreq
*)addr
);
744 error
= bpf_setif(d
, (struct ifreq
*)addr
);
752 struct timeval
*tv
= (struct timeval
*)addr
;
755 /* Compute number of milliseconds. */
756 msec
= tv
->tv_sec
* 1000 + tv
->tv_usec
/ 1000;
757 /* Scale milliseconds to ticks. Assume hard
758 clock has millisecond or greater resolution
759 (i.e. tick >= 1000). For 10ms hardclock,
760 tick/1000 = 10, so rtout<-msec/10. */
761 d
->bd_rtout
= msec
/ (tick
/ 1000);
770 struct timeval
*tv
= (struct timeval
*)addr
;
771 u_long msec
= d
->bd_rtout
;
774 tv
->tv_sec
= msec
/ 1000;
775 tv
->tv_usec
= msec
% 1000;
784 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
786 bs
->bs_recv
= d
->bd_rcount
;
787 bs
->bs_drop
= d
->bd_dcount
;
792 * Set immediate mode.
795 d
->bd_immediate
= *(u_int
*)addr
;
800 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
802 bv
->bv_major
= BPF_MAJOR_VERSION
;
803 bv
->bv_minor
= BPF_MINOR_VERSION
;
811 * Set d's packet filter program to fp. If this file already has a filter,
812 * free it and replace it. Returns EINVAL for bogus requests.
817 struct bpf_program
*fp
;
819 struct bpf_insn
*fcode
, *old
;
824 if (fp
->bf_insns
== 0) {
832 FREE((caddr_t
)old
, M_DEVBUF
);
836 if (flen
> BPF_MAXINSNS
)
839 size
= flen
* sizeof(*fp
->bf_insns
);
840 MALLOC(fcode
, struct bpf_insn
*, size
, M_DEVBUF
, M_WAITOK
);
841 if (copyin((caddr_t
)fp
->bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
842 bpf_validate(fcode
, (int)flen
)) {
844 d
->bd_filter
= fcode
;
848 FREE((caddr_t
)old
, M_DEVBUF
);
852 FREE((caddr_t
)fcode
, M_DEVBUF
);
857 * Detach a file from its current interface (if attached at all) and attach
858 * to the interface indicated by the name stored in ifr.
859 * Return an errno or 0.
871 * Separate string into name part and unit number. Put a null
872 * byte at the end of the name part, and compute the number.
873 * If the a unit number is unspecified, the default is 0,
874 * as initialized above. XXX This should be common code.
878 cp
[sizeof(ifr
->ifr_name
) - 1] = '\0';
880 if (*cp
>= '0' && *cp
<= '9') {
884 unit
= 10 * unit
+ *cp
++ - '0';
889 * Look through attached interfaces for the named one.
891 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
892 struct ifnet
*ifp
= bp
->bif_ifp
;
894 if (ifp
== 0 || unit
!= ifp
->if_unit
895 || strcmp(ifp
->if_name
, ifr
->ifr_name
) != 0)
898 * We found the requested interface.
899 * If it's not up, return an error.
900 * Allocate the packet buffers if we need to.
901 * If we're already attached to requested interface,
902 * just flush the buffer.
904 if ((ifp
->if_flags
& IFF_UP
) == 0)
907 if (d
->bd_sbuf
== 0) {
908 error
= bpf_allocbufs(d
);
913 if (bp
!= d
->bd_bif
) {
916 * Detach if attached to something else.
931 * Convert an interface name plus unit number of an ifp to a single
932 * name which is returned in the ifr.
939 char *s
= ifp
->if_name
;
940 char *d
= ifr
->ifr_name
;
942 while ((*d
++ = *s
++) != 0)
944 /* XXX Assume that unit number is less than 10. */
945 *d
++ = ifp
->if_unit
+ '0';
950 * The new select interface passes down the proc pointer; the old select
951 * stubs had to grab it out of the user struct. This glue allows either case.
959 return (bpf_select(dev
, rw
, u
.u_procp
));
964 * Support for select() system call
966 * Return true iff the specific operation will not block indefinitely.
967 * Otherwise, return false but make a note that a selwakeup() must be done.
970 bpf_select(dev
, rw
, p
)
975 register struct bpf_d
*d
;
981 * An imitation of the FIONREAD ioctl code.
983 d
= &bpf_dtab
[minor(dev
)];
986 if (d
->bd_hlen
!= 0 || (d
->bd_immediate
&& d
->bd_slen
!= 0)) {
988 * There is data waiting.
994 selrecord(p
, &d
->bd_sel
);
997 * No data ready. If there's already a select() waiting on this
998 * minor device then this is a collision. This shouldn't happen
999 * because minors really should not be shared, but if a process
1000 * forks while one of these is open, it is possible that both
1001 * processes could select on the same descriptor.
1003 if (d
->bd_selproc
&& d
->bd_selproc
->p_wchan
== (caddr_t
)&selwait
)
1014 * Incoming linkage from device drivers. Process the packet pkt, of length
1015 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1016 * by each process' filter, and if accepted, stashed into the corresponding
1021 bpf_tap(arg
, pkt
, pktlen
)
1023 register u_char
*pkt
;
1024 register u_int pktlen
;
1027 register struct bpf_d
*d
;
1028 register u_int slen
;
1030 * Note that the ipl does not have to be raised at this point.
1031 * The only problem that could arise here is that if two different
1032 * interfaces shared any data. This is not the case.
1034 bp
= (struct bpf_if
*)arg
;
1035 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1037 slen
= bpf_filter(d
->bd_filter
, pkt
, pktlen
, pktlen
);
1039 catchpacket(d
, pkt
, pktlen
, slen
, bcopy
);
1044 * Copy data from an mbuf chain into a buffer. This code is derived
1045 * from m_copydata in sys/uipc_mbuf.c.
1048 bpf_mcopy(src_arg
, dst_arg
, len
)
1049 const void *src_arg
;
1053 register const struct mbuf
*m
;
1054 register u_int count
;
1062 count
= min(m
->m_len
, len
);
1063 bcopy(mtod(m
, caddr_t
), (caddr_t
)dst
, count
);
1073 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1080 struct bpf_if
*bp
= (struct bpf_if
*)arg
;
1086 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
1087 pktlen
+= m0
->m_len
;
1089 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1091 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
1093 catchpacket(d
, (u_char
*)m
, pktlen
, slen
, bpf_mcopy
);
1100 * Move the packet data from interface memory (pkt) into the
1101 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1102 * otherwise 0. "copy" is the routine called to do the actual data
1103 * transfer. bcopy is passed in to copy contiguous chunks, while
1104 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1105 * pkt is really an mbuf.
1108 catchpacket(d
, pkt
, pktlen
, snaplen
, cpfn
)
1109 register struct bpf_d
*d
;
1110 register u_char
*pkt
;
1111 register u_int pktlen
, snaplen
;
1112 register void (*cpfn
) __P((const void *, void *, u_int
));
1114 register struct bpf_hdr
*hp
;
1115 register int totlen
, curlen
;
1116 register int hdrlen
= d
->bd_bif
->bif_hdrlen
;
1118 * Figure out how many bytes to move. If the packet is
1119 * greater or equal to the snapshot length, transfer that
1120 * much. Otherwise, transfer the whole packet (unless
1121 * we hit the buffer size limit).
1123 totlen
= hdrlen
+ min(snaplen
, pktlen
);
1124 if (totlen
> d
->bd_bufsize
)
1125 totlen
= d
->bd_bufsize
;
1128 * Round up the end of the previous packet to the next longword.
1130 curlen
= BPF_WORDALIGN(d
->bd_slen
);
1131 if (curlen
+ totlen
> d
->bd_bufsize
) {
1133 * This packet will overflow the storage buffer.
1134 * Rotate the buffers if we can, then wakeup any
1137 if (d
->bd_fbuf
== 0) {
1139 * We haven't completed the previous read yet,
1140 * so drop the packet.
1149 else if (d
->bd_immediate
)
1151 * Immediate mode is set. A packet arrived so any
1152 * reads should be woken up.
1157 * Append the bpf header.
1159 hp
= (struct bpf_hdr
*)(d
->bd_sbuf
+ curlen
);
1161 microtime(&hp
->bh_tstamp
);
1163 uniqtime(&hp
->bh_tstamp
);
1165 hp
->bh_tstamp
= time
;
1167 hp
->bh_datalen
= pktlen
;
1168 hp
->bh_hdrlen
= hdrlen
;
1170 * Copy the packet data into the store buffer and update its length.
1172 (*cpfn
)(pkt
, (u_char
*)hp
+ hdrlen
, (hp
->bh_caplen
= totlen
- hdrlen
));
1173 d
->bd_slen
= curlen
+ totlen
;
1177 * Initialize all nonzero fields of a descriptor.
1181 register struct bpf_d
*d
;
1183 MALLOC(d
->bd_fbuf
, caddr_t
, d
->bd_bufsize
, M_DEVBUF
, M_WAITOK
);
1184 if (d
->bd_fbuf
== 0)
1187 MALLOC(d
->bd_sbuf
, caddr_t
, d
->bd_bufsize
, M_DEVBUF
, M_WAITOK
);
1188 if (d
->bd_sbuf
== 0) {
1189 FREE(d
->bd_fbuf
, M_DEVBUF
);
1198 * Free buffers currently in use by a descriptor.
1203 register struct bpf_d
*d
;
1206 * We don't need to lock out interrupts since this descriptor has
1207 * been detached from its interface and it yet hasn't been marked
1210 if (d
->bd_sbuf
!= 0) {
1211 FREE(d
->bd_sbuf
, M_DEVBUF
);
1212 if (d
->bd_hbuf
!= 0)
1213 FREE(d
->bd_hbuf
, M_DEVBUF
);
1214 if (d
->bd_fbuf
!= 0)
1215 FREE(d
->bd_fbuf
, M_DEVBUF
);
1218 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
1226 /* XXX This routine belongs in net/if.c. */
1228 * Set/clear promiscuous mode on interface ifp based on the truth value
1229 * of pswitch. The calls are reference counted so that only the first
1230 * "on" request actually has an effect, as does the final "off" request.
1231 * Results are undefined if the "off" and "on" requests are not matched.
1234 ifpromisc(ifp
, pswitch
)
1240 * If the device is not configured up, we cannot put it in
1243 if ((ifp
->if_flags
& IFF_UP
) == 0)
1247 if (ifp
->if_pcount
++ != 0)
1249 ifp
->if_flags
|= IFF_PROMISC
;
1251 if (--ifp
->if_pcount
> 0)
1253 ifp
->if_flags
&= ~IFF_PROMISC
;
1255 ifr
.ifr_flags
= ifp
->if_flags
;
1256 return ((*ifp
->if_ioctl
)(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
));
1262 * Allocate some memory for bpf. This is temporary SunOS support, and
1263 * is admittedly a hack.
1264 * If resources unavaiable, return 0.
1267 bpf_alloc(size
, canwait
)
1269 register int canwait
;
1271 register struct mbuf
*m
;
1273 if ((unsigned)size
> (MCLBYTES
-8))
1276 MGET(m
, canwait
, MT_DATA
);
1279 if ((unsigned)size
> (MLEN
-8)) {
1281 if (m
->m_len
!= MCLBYTES
) {
1286 *mtod(m
, struct mbuf
**) = m
;
1287 return mtod(m
, caddr_t
) + 8;