]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/bpf.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / bsd / net / bpf.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1990, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * This code is derived from the Stanford/CMU enet packet filter,
33 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
34 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
35 * Berkeley Laboratory.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)bpf.c 8.2 (Berkeley) 3/28/94
66 *
67 * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.5 2001/01/05 04:49:09 jdp Exp $
68 */
69
70 #include "bpf.h"
71
72 #ifndef __GNUC__
73 #define inline
74 #else
75 #define inline __inline
76 #endif
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/conf.h>
81 #include <sys/malloc.h>
82 #include <sys/mbuf.h>
83 #include <sys/time.h>
84 #include <sys/proc.h>
85 #include <sys/signalvar.h>
86 #include <sys/filio.h>
87 #include <sys/sockio.h>
88 #include <sys/ttycom.h>
89 #include <sys/filedesc.h>
90 #include <sys/uio_internal.h>
91
92 #if defined(sparc) && BSD < 199103
93 #include <sys/stream.h>
94 #endif
95 #include <sys/poll.h>
96
97 #include <sys/socket.h>
98 #include <sys/vnode.h>
99
100 #include <net/if.h>
101 #include <net/bpf.h>
102 #include <net/bpfdesc.h>
103
104 #include <netinet/in.h>
105 #include <netinet/if_ether.h>
106 #include <sys/kernel.h>
107 #include <sys/sysctl.h>
108 #include <net/firewire.h>
109
110 #include <machine/spl.h>
111 #include <miscfs/devfs/devfs.h>
112 #include <net/dlil.h>
113
114 #include <kern/locks.h>
115
116 extern int tvtohz(struct timeval *);
117
118 #if NBPFILTER > 0
119
120 /*
121 * Older BSDs don't have kernel malloc.
122 */
123 #if BSD < 199103
124 extern bcopy();
125 static caddr_t bpf_alloc();
126 #include <net/bpf_compat.h>
127 #define BPF_BUFSIZE (MCLBYTES-8)
128 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
129 #else
130 #define BPF_BUFSIZE 4096
131 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
132 #endif
133
134
135 #define PRINET 26 /* interruptible */
136
137 /*
138 * The default read buffer size is patchable.
139 */
140 static unsigned int bpf_bufsize = BPF_BUFSIZE;
141 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
142 &bpf_bufsize, 0, "");
143 static unsigned int bpf_maxbufsize = BPF_MAXBUFSIZE;
144 SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
145 &bpf_maxbufsize, 0, "");
146 static unsigned int bpf_maxdevices = 256;
147 SYSCTL_UINT(_debug, OID_AUTO, bpf_maxdevices, CTLFLAG_RW,
148 &bpf_maxdevices, 0, "");
149
150 /*
151 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
152 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
153 */
154 static struct bpf_if *bpf_iflist;
155 #ifdef __APPLE__
156 /*
157 * BSD now stores the bpf_d in the dev_t which is a struct
158 * on their system. Our dev_t is an int, so we still store
159 * the bpf_d in a separate table indexed by minor device #.
160 *
161 * The value stored in bpf_dtab[n] represent three states:
162 * 0: device not opened
163 * 1: device opening or closing
164 * other: device <n> opened with pointer to storage
165 */
166 static struct bpf_d **bpf_dtab = NULL;
167 static unsigned int bpf_dtab_size = 0;
168 static unsigned int nbpfilter = 0;
169
170 static lck_mtx_t *bpf_mlock;
171 static lck_grp_t *bpf_mlock_grp;
172 static lck_grp_attr_t *bpf_mlock_grp_attr;
173 static lck_attr_t *bpf_mlock_attr;
174
175 /*
176 * Mark a descriptor free by making it point to itself.
177 * This is probably cheaper than marking with a constant since
178 * the address should be in a register anyway.
179 */
180 #endif /* __APPLE__ */
181
182 static int bpf_allocbufs(struct bpf_d *);
183 static void bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
184 static void bpf_detachd(struct bpf_d *d);
185 static void bpf_freed(struct bpf_d *);
186 static void bpf_mcopy(const void *, void *, size_t);
187 static int bpf_movein(struct uio *, int,
188 struct mbuf **, struct sockaddr *, int *);
189 static int bpf_setif(struct bpf_d *, struct ifreq *);
190 static void bpf_wakeup(struct bpf_d *);
191 static void catchpacket(struct bpf_d *, u_char *, u_int,
192 u_int, void (*)(const void *, void *, size_t));
193 static void reset_d(struct bpf_d *);
194 static int bpf_setf(struct bpf_d *, struct user_bpf_program *);
195
196 /*static void *bpf_devfs_token[MAXBPFILTER];*/
197
198 static int bpf_devsw_installed;
199
200 void bpf_init(void *unused);
201 int bpf_tap_callback(struct ifnet *ifp, struct mbuf *m);
202
203 /*
204 * Darwin differs from BSD here, the following are static
205 * on BSD and not static on Darwin.
206 */
207 d_open_t bpfopen;
208 d_close_t bpfclose;
209 d_read_t bpfread;
210 d_write_t bpfwrite;
211 ioctl_fcn_t bpfioctl;
212 select_fcn_t bpfpoll;
213
214
215 /* Darwin's cdevsw struct differs slightly from BSDs */
216 #define CDEV_MAJOR 23
217 static struct cdevsw bpf_cdevsw = {
218 /* open */ bpfopen,
219 /* close */ bpfclose,
220 /* read */ bpfread,
221 /* write */ bpfwrite,
222 /* ioctl */ bpfioctl,
223 /* stop */ eno_stop,
224 /* reset */ eno_reset,
225 /* tty */ NULL,
226 /* select */ bpfpoll,
227 /* mmap */ eno_mmap,
228 /* strategy*/ eno_strat,
229 /* getc */ eno_getc,
230 /* putc */ eno_putc,
231 /* type */ 0
232 };
233
234 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
235
236 static int
237 bpf_movein(struct uio *uio, int linktype, struct mbuf **mp, struct sockaddr *sockp, int *datlen)
238 {
239 struct mbuf *m;
240 int error;
241 int len;
242 int hlen;
243
244 if (sockp) {
245 /*
246 * Build a sockaddr based on the data link layer type.
247 * We do this at this level because the ethernet header
248 * is copied directly into the data field of the sockaddr.
249 * In the case of SLIP, there is no header and the packet
250 * is forwarded as is.
251 * Also, we are careful to leave room at the front of the mbuf
252 * for the link level header.
253 */
254 switch (linktype) {
255
256 case DLT_SLIP:
257 sockp->sa_family = AF_INET;
258 hlen = 0;
259 break;
260
261 case DLT_EN10MB:
262 sockp->sa_family = AF_UNSPEC;
263 /* XXX Would MAXLINKHDR be better? */
264 hlen = sizeof(struct ether_header);
265 break;
266
267 case DLT_FDDI:
268 #if defined(__FreeBSD__) || defined(__bsdi__)
269 sockp->sa_family = AF_IMPLINK;
270 hlen = 0;
271 #else
272 sockp->sa_family = AF_UNSPEC;
273 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
274 hlen = 24;
275 #endif
276 break;
277
278 case DLT_RAW:
279 case DLT_NULL:
280 sockp->sa_family = AF_UNSPEC;
281 hlen = 0;
282 break;
283
284 #ifdef __FreeBSD__
285 case DLT_ATM_RFC1483:
286 /*
287 * en atm driver requires 4-byte atm pseudo header.
288 * though it isn't standard, vpi:vci needs to be
289 * specified anyway.
290 */
291 sockp->sa_family = AF_UNSPEC;
292 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
293 break;
294 #endif
295 case DLT_PPP:
296 sockp->sa_family = AF_UNSPEC;
297 hlen = 4; /* This should match PPP_HDRLEN */
298 break;
299
300 case DLT_APPLE_IP_OVER_IEEE1394:
301 sockp->sa_family = AF_UNSPEC;
302 hlen = sizeof(struct firewire_header);
303 break;
304
305 default:
306 return (EIO);
307 }
308 if ((hlen + SOCKADDR_HDR_LEN) > sockp->sa_len) {
309 return (EIO);
310 }
311 }
312 else {
313 hlen = 0;
314 }
315
316 // LP64todo - fix this!
317 len = uio_resid(uio);
318 *datlen = len - hlen;
319 if ((unsigned)len > MCLBYTES)
320 return (EIO);
321
322 MGETHDR(m, M_WAIT, MT_DATA);
323 if (m == 0)
324 return (ENOBUFS);
325 if ((unsigned)len > MHLEN) {
326 #if BSD >= 199103
327 MCLGET(m, M_WAIT);
328 if ((m->m_flags & M_EXT) == 0) {
329 #else
330 MCLGET(m);
331 if (m->m_len != MCLBYTES) {
332 #endif
333 error = ENOBUFS;
334 goto bad;
335 }
336 }
337 m->m_pkthdr.len = m->m_len = len;
338 m->m_pkthdr.rcvif = NULL;
339 *mp = m;
340 /*
341 * Make room for link header.
342 */
343 if (hlen != 0) {
344 m->m_pkthdr.len -= hlen;
345 m->m_len -= hlen;
346 #if BSD >= 199103
347 m->m_data += hlen; /* XXX */
348 #else
349 m->m_off += hlen;
350 #endif
351 error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio);
352 if (error)
353 goto bad;
354 }
355 error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio);
356 if (!error)
357 return (0);
358 bad:
359 m_freem(m);
360 return (error);
361 }
362
363 #ifdef __APPLE__
364 /* Callback registered with Ethernet driver. */
365 int bpf_tap_callback(struct ifnet *ifp, struct mbuf *m)
366 {
367 /*
368 * Do nothing if the BPF tap has been turned off.
369 * This is to protect from a potential race where this
370 * call blocks on the lock. And in the meantime
371 * BPF is turned off, which will clear if_bpf.
372 */
373 if (ifp->if_bpf)
374 bpf_mtap(ifp, m);
375 return 0;
376 }
377
378 /*
379 * The dynamic addition of a new device node must block all processes that are opening
380 * the last device so that no process will get an unexpected ENOENT
381 */
382 static void
383 bpf_make_dev_t(int maj)
384 {
385 static int bpf_growing = 0;
386 unsigned int cur_size = nbpfilter, i;
387
388 if (nbpfilter >= bpf_maxdevices)
389 return;
390
391 while (bpf_growing) {
392 /* Wait until new device has been created */
393 (void)tsleep((caddr_t)&bpf_growing, PZERO, "bpf_growing", 0);
394 }
395 if (nbpfilter > cur_size) {
396 /* other thread grew it already */
397 return;
398 }
399 bpf_growing = 1;
400
401 /* need to grow bpf_dtab first */
402 if (nbpfilter == bpf_dtab_size) {
403 int new_dtab_size;
404 struct bpf_d **new_dtab = NULL;
405 struct bpf_d **old_dtab = NULL;
406
407 new_dtab_size = bpf_dtab_size + NBPFILTER;
408 new_dtab = (struct bpf_d **)_MALLOC(sizeof(struct bpf_d *) * new_dtab_size, M_DEVBUF, M_WAIT);
409 if (new_dtab == 0) {
410 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
411 goto done;
412 }
413 if (bpf_dtab) {
414 bcopy(bpf_dtab, new_dtab,
415 sizeof(struct bpf_d *) * bpf_dtab_size);
416 }
417 bzero(new_dtab + bpf_dtab_size,
418 sizeof(struct bpf_d *) * NBPFILTER);
419 old_dtab = bpf_dtab;
420 bpf_dtab = new_dtab;
421 bpf_dtab_size = new_dtab_size;
422 if (old_dtab != NULL)
423 _FREE(old_dtab, M_DEVBUF);
424 }
425 i = nbpfilter++;
426 (void) devfs_make_node(makedev(maj, i),
427 DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0600,
428 "bpf%d", i);
429 done:
430 bpf_growing = 0;
431 wakeup((caddr_t)&bpf_growing);
432 }
433
434 #endif
435
436 /*
437 * Attach file to the bpf interface, i.e. make d listen on bp.
438 * Must be called at splimp.
439 */
440 static void
441 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
442 {
443 /*
444 * Point d at bp, and add d to the interface's list of listeners.
445 * Finally, point the driver's bpf cookie at the interface so
446 * it will divert packets to bpf.
447 */
448 d->bd_bif = bp;
449 d->bd_next = bp->bif_dlist;
450 bp->bif_dlist = d;
451
452 bp->bif_ifp->if_bpf = bp;
453
454 #ifdef __APPLE__
455 dlil_set_bpf_tap(bp->bif_ifp, BPF_TAP_INPUT_OUTPUT, bpf_tap_callback);
456 #endif
457 }
458
459 /*
460 * Detach a file from its interface.
461 */
462 static void
463 bpf_detachd(struct bpf_d *d)
464 {
465 struct bpf_d **p;
466 struct bpf_if *bp;
467 #ifdef __APPLE__
468 struct ifnet *ifp;
469
470 ifp = d->bd_bif->bif_ifp;
471
472 #endif
473
474 bp = d->bd_bif;
475 /*
476 * Check if this descriptor had requested promiscuous mode.
477 * If so, turn it off.
478 */
479 if (d->bd_promisc) {
480 d->bd_promisc = 0;
481 if (ifnet_set_promiscuous(bp->bif_ifp, 0))
482 /*
483 * Something is really wrong if we were able to put
484 * the driver into promiscuous mode, but can't
485 * take it out.
486 * Most likely the network interface is gone.
487 */
488 printf("bpf: ifnet_set_promiscuous failed");
489 }
490 /* Remove d from the interface's descriptor list. */
491 p = &bp->bif_dlist;
492 while (*p != d) {
493 p = &(*p)->bd_next;
494 if (*p == 0)
495 panic("bpf_detachd: descriptor not in list");
496 }
497 *p = (*p)->bd_next;
498 if (bp->bif_dlist == 0) {
499 /*
500 * Let the driver know that there are no more listeners.
501 */
502 if (ifp->if_set_bpf_tap)
503 (*ifp->if_set_bpf_tap)(ifp, BPF_TAP_DISABLE, 0);
504 d->bd_bif->bif_ifp->if_bpf = 0;
505 }
506 d->bd_bif = 0;
507 }
508
509
510 /*
511 * Open ethernet device. Returns ENXIO for illegal minor device number,
512 * EBUSY if file is open by another process.
513 */
514 /* ARGSUSED */
515 int
516 bpfopen(dev_t dev, __unused int flags, __unused int fmt, __unused struct proc *p)
517 {
518 register struct bpf_d *d;
519
520 if ((unsigned int) minor(dev) >= nbpfilter)
521 return (ENXIO);
522
523 /*
524 * New device nodes are created on demand when opening the last one.
525 * The programming model is for processes to loop on the minor starting at 0
526 * as long as EBUSY is returned. The loop stops when either the open succeeds or
527 * an error other that EBUSY is returned. That means that bpf_make_dev_t() must
528 * block all processes that are opening the last node. If not all
529 * processes are blocked, they could unexpectedly get ENOENT and abort their
530 * opening loop.
531 */
532 if ((unsigned int) minor(dev) == (nbpfilter - 1))
533 bpf_make_dev_t(major(dev));
534
535 /*
536 * Each minor can be opened by only one process. If the requested
537 * minor is in use, return EBUSY.
538 *
539 * Important: bpfopen() and bpfclose() have to check and set the status of a device
540 * in the same lockin context otherwise the device may be leaked because the vnode use count
541 * will be unpextectly greater than 1 when close() is called.
542 */
543 if (bpf_dtab[minor(dev)] == 0)
544 bpf_dtab[minor(dev)] = (void *)1; /* Mark opening */
545 else
546 return (EBUSY);
547
548 d = (struct bpf_d *)_MALLOC(sizeof(struct bpf_d), M_DEVBUF, M_WAIT);
549 if (d == NULL) {
550 /* this really is a catastrophic failure */
551 printf("bpfopen: malloc bpf_d failed\n");
552 bpf_dtab[minor(dev)] = 0;
553 return ENOMEM;
554 }
555 bzero(d, sizeof(struct bpf_d));
556
557 /*
558 * It is not necessary to take the BPF lock here because no other
559 * thread can access the device until it is marked opened...
560 */
561
562 /* Mark "in use" and do most initialization. */
563 d->bd_bufsize = bpf_bufsize;
564 d->bd_sig = SIGIO;
565 d->bd_seesent = 1;
566 bpf_dtab[minor(dev)] = d; /* Mark opened */
567
568 return (0);
569 }
570
571 /*
572 * Close the descriptor by detaching it from its interface,
573 * deallocating its buffers, and marking it free.
574 */
575 /* ARGSUSED */
576 int
577 bpfclose(dev_t dev, __unused int flags, __unused int fmt, __unused struct proc *p)
578 {
579 register struct bpf_d *d;
580
581 d = bpf_dtab[minor(dev)];
582 if (d == 0 || d == (void *)1)
583 return (ENXIO);
584
585 bpf_dtab[minor(dev)] = (void *)1; /* Mark closing */
586
587 /* Take BPF lock to ensure no other thread is using the device */
588 lck_mtx_lock(bpf_mlock);
589
590 if (d->bd_bif)
591 bpf_detachd(d);
592 selthreadclear(&d->bd_sel);
593 bpf_freed(d);
594
595 lck_mtx_unlock(bpf_mlock);
596
597 /* Mark free in same context as bpfopen comes to check */
598 bpf_dtab[minor(dev)] = 0; /* Mark closed */
599 _FREE(d, M_DEVBUF);
600
601 return (0);
602 }
603
604
605 #define BPF_SLEEP bpf_sleep
606
607 static int
608 bpf_sleep(struct bpf_d *d, int pri, const char *wmesg, int timo)
609 {
610 register int st;
611
612 lck_mtx_unlock(bpf_mlock);
613
614 st = tsleep((caddr_t)d, pri, wmesg, timo);
615
616 lck_mtx_lock(bpf_mlock);
617
618 return st;
619 }
620
621 /*
622 * Rotate the packet buffers in descriptor d. Move the store buffer
623 * into the hold slot, and the free buffer into the store slot.
624 * Zero the length of the new store buffer.
625 */
626 #define ROTATE_BUFFERS(d) \
627 (d)->bd_hbuf = (d)->bd_sbuf; \
628 (d)->bd_hlen = (d)->bd_slen; \
629 (d)->bd_sbuf = (d)->bd_fbuf; \
630 (d)->bd_slen = 0; \
631 (d)->bd_fbuf = 0;
632 /*
633 * bpfread - read next chunk of packets from buffers
634 */
635 int
636 bpfread(dev_t dev, struct uio *uio, int ioflag)
637 {
638 register struct bpf_d *d;
639 int error;
640 int s;
641
642 d = bpf_dtab[minor(dev)];
643 if (d == 0 || d == (void *)1)
644 return (ENXIO);
645
646 lck_mtx_lock(bpf_mlock);
647
648
649 /*
650 * Restrict application to use a buffer the same size as
651 * as kernel buffers.
652 */
653 // LP64todo - fix this
654 if (uio->uio_resid != d->bd_bufsize) {
655 lck_mtx_unlock(bpf_mlock);
656 return (EINVAL);
657 }
658
659 s = splimp();
660 /*
661 * If the hold buffer is empty, then do a timed sleep, which
662 * ends when the timeout expires or when enough packets
663 * have arrived to fill the store buffer.
664 */
665 while (d->bd_hbuf == 0) {
666 if (d->bd_immediate && d->bd_slen != 0) {
667 /*
668 * A packet(s) either arrived since the previous
669 * read or arrived while we were asleep.
670 * Rotate the buffers and return what's here.
671 */
672 ROTATE_BUFFERS(d);
673 break;
674 }
675
676 /*
677 * No data is available, check to see if the bpf device
678 * is still pointed at a real interface. If not, return
679 * ENXIO so that the userland process knows to rebind
680 * it before using it again.
681 */
682 if (d->bd_bif == NULL) {
683 splx(s);
684 lck_mtx_unlock(bpf_mlock);
685 return (ENXIO);
686 }
687
688 if (ioflag & IO_NDELAY)
689 error = EWOULDBLOCK;
690 else
691 error = BPF_SLEEP(d, PRINET|PCATCH, "bpf",
692 d->bd_rtout);
693 if (error == EINTR || error == ERESTART) {
694 splx(s);
695 lck_mtx_unlock(bpf_mlock);
696 return (error);
697 }
698 if (error == EWOULDBLOCK) {
699 /*
700 * On a timeout, return what's in the buffer,
701 * which may be nothing. If there is something
702 * in the store buffer, we can rotate the buffers.
703 */
704 if (d->bd_hbuf)
705 /*
706 * We filled up the buffer in between
707 * getting the timeout and arriving
708 * here, so we don't need to rotate.
709 */
710 break;
711
712 if (d->bd_slen == 0) {
713 splx(s);
714 lck_mtx_unlock(bpf_mlock);
715 return (0);
716 }
717 ROTATE_BUFFERS(d);
718 break;
719 }
720 }
721 /*
722 * At this point, we know we have something in the hold slot.
723 */
724 splx(s);
725
726 /*
727 * Move data from hold buffer into user space.
728 * We know the entire buffer is transferred since
729 * we checked above that the read buffer is bpf_bufsize bytes.
730 */
731 error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
732
733 s = splimp();
734 d->bd_fbuf = d->bd_hbuf;
735 d->bd_hbuf = 0;
736 d->bd_hlen = 0;
737 splx(s);
738 lck_mtx_unlock(bpf_mlock);
739 return (error);
740 }
741
742
743 /*
744 * If there are processes sleeping on this descriptor, wake them up.
745 */
746 static void
747 bpf_wakeup(struct bpf_d *d)
748 {
749 wakeup((caddr_t)d);
750 if (d->bd_async && d->bd_sig && d->bd_sigio)
751 pgsigio(d->bd_sigio, d->bd_sig, 0);
752
753 #if BSD >= 199103
754 selwakeup(&d->bd_sel);
755 #ifndef __APPLE__
756 /* XXX */
757 d->bd_sel.si_pid = 0;
758 #endif
759 #else
760 if (d->bd_selproc) {
761 selwakeup(d->bd_selproc, (int)d->bd_selcoll);
762 d->bd_selcoll = 0;
763 d->bd_selproc = 0;
764 }
765 #endif
766 }
767
768 /* keep in sync with bpf_movein above: */
769 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
770
771 int
772 bpfwrite(dev_t dev, struct uio *uio, __unused int ioflag)
773 {
774 register struct bpf_d *d;
775 struct ifnet *ifp;
776 struct mbuf *m;
777 int error;
778 char dst_buf[SOCKADDR_HDR_LEN + MAX_DATALINK_HDR_LEN];
779 int datlen;
780
781 d = bpf_dtab[minor(dev)];
782 if (d == 0 || d == (void *)1)
783 return (ENXIO);
784
785 lck_mtx_lock(bpf_mlock);
786
787 if (d->bd_bif == 0) {
788 lck_mtx_unlock(bpf_mlock);
789 return (ENXIO);
790 }
791
792 ifp = d->bd_bif->bif_ifp;
793
794 if (uio->uio_resid == 0) {
795 lck_mtx_unlock(bpf_mlock);
796 return (0);
797 }
798 ((struct sockaddr *)dst_buf)->sa_len = sizeof(dst_buf);
799 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m,
800 d->bd_hdrcmplt ? 0 : (struct sockaddr *)dst_buf, &datlen);
801 if (error) {
802 lck_mtx_unlock(bpf_mlock);
803 return (error);
804 }
805
806 if ((unsigned)datlen > ifp->if_mtu) {
807 lck_mtx_unlock(bpf_mlock);
808 return (EMSGSIZE);
809 }
810
811 lck_mtx_unlock(bpf_mlock);
812
813 if (d->bd_hdrcmplt) {
814 error = dlil_output(ifp, 0, m, NULL, NULL, 1);
815 }
816 else {
817 error = dlil_output(ifp, PF_INET, m, NULL, (struct sockaddr *)dst_buf, 0);
818 }
819
820 /*
821 * The driver frees the mbuf.
822 */
823 return (error);
824 }
825
826 /*
827 * Reset a descriptor by flushing its packet buffer and clearing the
828 * receive and drop counts. Should be called at splimp.
829 */
830 static void
831 reset_d(struct bpf_d *d)
832 {
833 if (d->bd_hbuf) {
834 /* Free the hold buffer. */
835 d->bd_fbuf = d->bd_hbuf;
836 d->bd_hbuf = 0;
837 }
838 d->bd_slen = 0;
839 d->bd_hlen = 0;
840 d->bd_rcount = 0;
841 d->bd_dcount = 0;
842 }
843
844 /*
845 * FIONREAD Check for read packet available.
846 * SIOCGIFADDR Get interface address - convenient hook to driver.
847 * BIOCGBLEN Get buffer len [for read()].
848 * BIOCSETF Set ethernet read filter.
849 * BIOCFLUSH Flush read packet buffer.
850 * BIOCPROMISC Put interface into promiscuous mode.
851 * BIOCGDLT Get link layer type.
852 * BIOCGETIF Get interface name.
853 * BIOCSETIF Set interface.
854 * BIOCSRTIMEOUT Set read timeout.
855 * BIOCGRTIMEOUT Get read timeout.
856 * BIOCGSTATS Get packet stats.
857 * BIOCIMMEDIATE Set immediate mode.
858 * BIOCVERSION Get filter language version.
859 * BIOCGHDRCMPLT Get "header already complete" flag
860 * BIOCSHDRCMPLT Set "header already complete" flag
861 * BIOCGSEESENT Get "see packets sent" flag
862 * BIOCSSEESENT Set "see packets sent" flag
863 */
864 /* ARGSUSED */
865 int
866 bpfioctl(dev_t dev, u_long cmd, caddr_t addr, __unused int flags, struct proc *p)
867 {
868 register struct bpf_d *d;
869 int s, error = 0;
870
871 d = bpf_dtab[minor(dev)];
872 if (d == 0 || d == (void *)1)
873 return (ENXIO);
874
875 lck_mtx_lock(bpf_mlock);
876
877 switch (cmd) {
878
879 default:
880 error = EINVAL;
881 break;
882
883 /*
884 * Check for read packet available.
885 */
886 case FIONREAD:
887 {
888 int n;
889
890 s = splimp();
891 n = d->bd_slen;
892 if (d->bd_hbuf)
893 n += d->bd_hlen;
894 splx(s);
895
896 *(int *)addr = n;
897 break;
898 }
899
900 case SIOCGIFADDR:
901 {
902 struct ifnet *ifp;
903
904 if (d->bd_bif == 0)
905 error = EINVAL;
906 else {
907 ifp = d->bd_bif->bif_ifp;
908 error = dlil_ioctl(0, ifp, cmd, addr);
909 }
910 break;
911 }
912
913 /*
914 * Get buffer len [for read()].
915 */
916 case BIOCGBLEN:
917 *(u_int *)addr = d->bd_bufsize;
918 break;
919
920 /*
921 * Set buffer length.
922 */
923 case BIOCSBLEN:
924 #if BSD < 199103
925 error = EINVAL;
926 #else
927 if (d->bd_bif != 0)
928 error = EINVAL;
929 else {
930 register u_int size = *(u_int *)addr;
931
932 if (size > bpf_maxbufsize)
933 *(u_int *)addr = size = bpf_maxbufsize;
934 else if (size < BPF_MINBUFSIZE)
935 *(u_int *)addr = size = BPF_MINBUFSIZE;
936 d->bd_bufsize = size;
937 }
938 #endif
939 break;
940
941 /*
942 * Set link layer read filter.
943 */
944 case BIOCSETF:
945 if (proc_is64bit(p)) {
946 error = bpf_setf(d, (struct user_bpf_program *)addr);
947 }
948 else {
949 struct bpf_program * tmpp;
950 struct user_bpf_program tmp;
951
952 tmpp = (struct bpf_program *)addr;
953 tmp.bf_len = tmpp->bf_len;
954 tmp.bf_insns = CAST_USER_ADDR_T(tmpp->bf_insns);
955 error = bpf_setf(d, &tmp);
956 }
957 break;
958
959 /*
960 * Flush read packet buffer.
961 */
962 case BIOCFLUSH:
963 s = splimp();
964 reset_d(d);
965 splx(s);
966 break;
967
968 /*
969 * Put interface into promiscuous mode.
970 */
971 case BIOCPROMISC:
972 if (d->bd_bif == 0) {
973 /*
974 * No interface attached yet.
975 */
976 error = EINVAL;
977 break;
978 }
979 s = splimp();
980 if (d->bd_promisc == 0) {
981 error = ifnet_set_promiscuous(d->bd_bif->bif_ifp, 1);
982 if (error == 0)
983 d->bd_promisc = 1;
984 }
985 splx(s);
986 break;
987
988 /*
989 * Get device parameters.
990 */
991 case BIOCGDLT:
992 if (d->bd_bif == 0)
993 error = EINVAL;
994 else
995 *(u_int *)addr = d->bd_bif->bif_dlt;
996 break;
997
998 /*
999 * Get interface name.
1000 */
1001 case BIOCGETIF:
1002 if (d->bd_bif == 0)
1003 error = EINVAL;
1004 else {
1005 struct ifnet *const ifp = d->bd_bif->bif_ifp;
1006 struct ifreq *const ifr = (struct ifreq *)addr;
1007
1008 snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
1009 "%s%d", ifp->if_name, ifp->if_unit);
1010 }
1011 break;
1012
1013 /*
1014 * Set interface.
1015 */
1016 case BIOCSETIF:
1017 error = bpf_setif(d, (struct ifreq *)addr);
1018 break;
1019
1020 /*
1021 * Set read timeout.
1022 */
1023 case BIOCSRTIMEOUT:
1024 {
1025 struct timeval *tv = (struct timeval *)addr;
1026
1027 /*
1028 * Subtract 1 tick from tvtohz() since this isn't
1029 * a one-shot timer.
1030 */
1031 if ((error = itimerfix(tv)) == 0)
1032 d->bd_rtout = tvtohz(tv) - 1;
1033 break;
1034 }
1035
1036 /*
1037 * Get read timeout.
1038 */
1039 case BIOCGRTIMEOUT:
1040 {
1041 struct timeval *tv = (struct timeval *)addr;
1042
1043 tv->tv_sec = d->bd_rtout / hz;
1044 tv->tv_usec = (d->bd_rtout % hz) * tick;
1045 break;
1046 }
1047
1048 /*
1049 * Get packet stats.
1050 */
1051 case BIOCGSTATS:
1052 {
1053 struct bpf_stat *bs = (struct bpf_stat *)addr;
1054
1055 bs->bs_recv = d->bd_rcount;
1056 bs->bs_drop = d->bd_dcount;
1057 break;
1058 }
1059
1060 /*
1061 * Set immediate mode.
1062 */
1063 case BIOCIMMEDIATE:
1064 d->bd_immediate = *(u_int *)addr;
1065 break;
1066
1067 case BIOCVERSION:
1068 {
1069 struct bpf_version *bv = (struct bpf_version *)addr;
1070
1071 bv->bv_major = BPF_MAJOR_VERSION;
1072 bv->bv_minor = BPF_MINOR_VERSION;
1073 break;
1074 }
1075
1076 /*
1077 * Get "header already complete" flag
1078 */
1079 case BIOCGHDRCMPLT:
1080 *(u_int *)addr = d->bd_hdrcmplt;
1081 break;
1082
1083 /*
1084 * Set "header already complete" flag
1085 */
1086 case BIOCSHDRCMPLT:
1087 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1088 break;
1089
1090 /*
1091 * Get "see sent packets" flag
1092 */
1093 case BIOCGSEESENT:
1094 *(u_int *)addr = d->bd_seesent;
1095 break;
1096
1097 /*
1098 * Set "see sent packets" flag
1099 */
1100 case BIOCSSEESENT:
1101 d->bd_seesent = *(u_int *)addr;
1102 break;
1103
1104 case FIONBIO: /* Non-blocking I/O */
1105 break;
1106
1107 case FIOASYNC: /* Send signal on receive packets */
1108 d->bd_async = *(int *)addr;
1109 break;
1110 #ifndef __APPLE__
1111 case FIOSETOWN:
1112 error = fsetown(*(int *)addr, &d->bd_sigio);
1113 break;
1114
1115 case FIOGETOWN:
1116 *(int *)addr = fgetown(d->bd_sigio);
1117 break;
1118
1119 /* This is deprecated, FIOSETOWN should be used instead. */
1120 case TIOCSPGRP:
1121 error = fsetown(-(*(int *)addr), &d->bd_sigio);
1122 break;
1123
1124 /* This is deprecated, FIOGETOWN should be used instead. */
1125 case TIOCGPGRP:
1126 *(int *)addr = -fgetown(d->bd_sigio);
1127 break;
1128 #endif
1129 case BIOCSRSIG: /* Set receive signal */
1130 {
1131 u_int sig;
1132
1133 sig = *(u_int *)addr;
1134
1135 if (sig >= NSIG)
1136 error = EINVAL;
1137 else
1138 d->bd_sig = sig;
1139 break;
1140 }
1141 case BIOCGRSIG:
1142 *(u_int *)addr = d->bd_sig;
1143 break;
1144 }
1145
1146 lck_mtx_unlock(bpf_mlock);
1147
1148 return (error);
1149 }
1150
1151 /*
1152 * Set d's packet filter program to fp. If this file already has a filter,
1153 * free it and replace it. Returns EINVAL for bogus requests.
1154 */
1155 static int
1156 bpf_setf(struct bpf_d *d, struct user_bpf_program *fp)
1157 {
1158 struct bpf_insn *fcode, *old;
1159 u_int flen, size;
1160 int s;
1161
1162 old = d->bd_filter;
1163 if (fp->bf_insns == USER_ADDR_NULL) {
1164 if (fp->bf_len != 0)
1165 return (EINVAL);
1166 s = splimp();
1167 d->bd_filter = 0;
1168 reset_d(d);
1169 splx(s);
1170 if (old != 0)
1171 FREE((caddr_t)old, M_DEVBUF);
1172 return (0);
1173 }
1174 flen = fp->bf_len;
1175 if (flen > BPF_MAXINSNS)
1176 return (EINVAL);
1177
1178 size = flen * sizeof(struct bpf_insn);
1179 fcode = (struct bpf_insn *) _MALLOC(size, M_DEVBUF, M_WAIT);
1180 #ifdef __APPLE__
1181 if (fcode == NULL)
1182 return (ENOBUFS);
1183 #endif
1184 if (copyin(fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1185 bpf_validate(fcode, (int)flen)) {
1186 s = splimp();
1187 d->bd_filter = fcode;
1188 reset_d(d);
1189 splx(s);
1190 if (old != 0)
1191 FREE((caddr_t)old, M_DEVBUF);
1192
1193 return (0);
1194 }
1195 FREE((caddr_t)fcode, M_DEVBUF);
1196 return (EINVAL);
1197 }
1198
1199 /*
1200 * Detach a file from its current interface (if attached at all) and attach
1201 * to the interface indicated by the name stored in ifr.
1202 * Return an errno or 0.
1203 */
1204 static int
1205 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1206 {
1207 struct bpf_if *bp;
1208 int s, error;
1209 struct ifnet *theywant;
1210
1211 theywant = ifunit(ifr->ifr_name);
1212 if (theywant == 0)
1213 return ENXIO;
1214
1215 /*
1216 * Look through attached interfaces for the named one.
1217 */
1218 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
1219 struct ifnet *ifp = bp->bif_ifp;
1220
1221 if (ifp == 0 || ifp != theywant)
1222 continue;
1223 /*
1224 * We found the requested interface.
1225 * If it's not up, return an error.
1226 * Allocate the packet buffers if we need to.
1227 * If we're already attached to requested interface,
1228 * just flush the buffer.
1229 */
1230 if ((ifp->if_flags & IFF_UP) == 0)
1231 return (ENETDOWN);
1232
1233 if (d->bd_sbuf == 0) {
1234 error = bpf_allocbufs(d);
1235 if (error != 0)
1236 return (error);
1237 }
1238 s = splimp();
1239 if (bp != d->bd_bif) {
1240 if (d->bd_bif)
1241 /*
1242 * Detach if attached to something else.
1243 */
1244 bpf_detachd(d);
1245
1246 bpf_attachd(d, bp);
1247 }
1248 reset_d(d);
1249 splx(s);
1250 return (0);
1251 }
1252 /* Not found. */
1253 return (ENXIO);
1254 }
1255
1256 /*
1257 * Support for select() and poll() system calls
1258 *
1259 * Return true iff the specific operation will not block indefinitely.
1260 * Otherwise, return false but make a note that a selwakeup() must be done.
1261 */
1262 int
1263 bpfpoll(dev_t dev, int events, void * wql, struct proc *p)
1264 {
1265 register struct bpf_d *d;
1266 register int s;
1267 int revents = 0;
1268
1269 d = bpf_dtab[minor(dev)];
1270 if (d == 0 || d == (void *)1)
1271 return (ENXIO);
1272
1273 lck_mtx_lock(bpf_mlock);
1274
1275 /*
1276 * An imitation of the FIONREAD ioctl code.
1277 */
1278 if (d->bd_bif == NULL) {
1279 lck_mtx_unlock(bpf_mlock);
1280 return (ENXIO);
1281 }
1282
1283 s = splimp();
1284 if (events & (POLLIN | POLLRDNORM)) {
1285 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1286 revents |= events & (POLLIN | POLLRDNORM);
1287 else
1288 selrecord(p, &d->bd_sel, wql);
1289 }
1290 splx(s);
1291
1292 lck_mtx_unlock(bpf_mlock);
1293 return (revents);
1294 }
1295
1296 /*
1297 * Incoming linkage from device drivers. Process the packet pkt, of length
1298 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1299 * by each process' filter, and if accepted, stashed into the corresponding
1300 * buffer.
1301 */
1302 void
1303 bpf_tap(struct ifnet *ifp, u_char *pkt, u_int pktlen)
1304 {
1305 struct bpf_if *bp;
1306 register struct bpf_d *d;
1307 register u_int slen;
1308 /*
1309 * Note that the ipl does not have to be raised at this point.
1310 * The only problem that could arise here is that if two different
1311 * interfaces shared any data. This is not the case.
1312 */
1313 lck_mtx_lock(bpf_mlock);
1314
1315 bp = ifp->if_bpf;
1316 #ifdef __APPLE__
1317 if (bp) {
1318 #endif
1319 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1320 ++d->bd_rcount;
1321 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1322 if (slen != 0)
1323 catchpacket(d, pkt, pktlen, slen, bcopy);
1324 }
1325 #ifdef __APPLE__
1326 }
1327 lck_mtx_unlock(bpf_mlock);
1328 #endif
1329 }
1330
1331 /*
1332 * Copy data from an mbuf chain into a buffer. This code is derived
1333 * from m_copydata in sys/uipc_mbuf.c.
1334 */
1335 static void
1336 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1337 {
1338 const struct mbuf *m;
1339 u_int count;
1340 u_char *dst;
1341
1342 m = src_arg;
1343 dst = dst_arg;
1344 while (len > 0) {
1345 if (m == 0)
1346 panic("bpf_mcopy");
1347 count = min(m->m_len, len);
1348 bcopy(mtod(m, const void *), dst, count);
1349 m = m->m_next;
1350 dst += count;
1351 len -= count;
1352 }
1353 }
1354
1355 /*
1356 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1357 */
1358 void
1359 bpf_mtap(struct ifnet *ifp, struct mbuf *m)
1360 {
1361 struct bpf_if *bp;
1362 struct bpf_d *d;
1363 u_int pktlen, slen;
1364 struct mbuf *m0;
1365
1366 lck_mtx_lock(bpf_mlock);
1367
1368 bp = ifp->if_bpf;
1369 if (bp) {
1370 pktlen = 0;
1371 for (m0 = m; m0 != 0; m0 = m0->m_next)
1372 pktlen += m0->m_len;
1373
1374 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1375 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1376 continue;
1377 ++d->bd_rcount;
1378 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1379 if (slen != 0)
1380 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1381 }
1382 }
1383
1384 lck_mtx_unlock(bpf_mlock);
1385 }
1386
1387 /*
1388 * Move the packet data from interface memory (pkt) into the
1389 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1390 * otherwise 0. "copy" is the routine called to do the actual data
1391 * transfer. bcopy is passed in to copy contiguous chunks, while
1392 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1393 * pkt is really an mbuf.
1394 */
1395 static void
1396 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1397 void (*cpfn)(const void *, void *, size_t))
1398 {
1399 register struct bpf_hdr *hp;
1400 register int totlen, curlen;
1401 register int hdrlen = d->bd_bif->bif_hdrlen;
1402 /*
1403 * Figure out how many bytes to move. If the packet is
1404 * greater or equal to the snapshot length, transfer that
1405 * much. Otherwise, transfer the whole packet (unless
1406 * we hit the buffer size limit).
1407 */
1408 totlen = hdrlen + min(snaplen, pktlen);
1409 if (totlen > d->bd_bufsize)
1410 totlen = d->bd_bufsize;
1411
1412 /*
1413 * Round up the end of the previous packet to the next longword.
1414 */
1415 curlen = BPF_WORDALIGN(d->bd_slen);
1416 if (curlen + totlen > d->bd_bufsize) {
1417 /*
1418 * This packet will overflow the storage buffer.
1419 * Rotate the buffers if we can, then wakeup any
1420 * pending reads.
1421 */
1422 if (d->bd_fbuf == 0) {
1423 /*
1424 * We haven't completed the previous read yet,
1425 * so drop the packet.
1426 */
1427 ++d->bd_dcount;
1428 return;
1429 }
1430 ROTATE_BUFFERS(d);
1431 bpf_wakeup(d);
1432 curlen = 0;
1433 }
1434 else if (d->bd_immediate)
1435 /*
1436 * Immediate mode is set. A packet arrived so any
1437 * reads should be woken up.
1438 */
1439 bpf_wakeup(d);
1440
1441 /*
1442 * Append the bpf header.
1443 */
1444 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1445 #if BSD >= 199103
1446 microtime(&hp->bh_tstamp);
1447 #elif defined(sun)
1448 uniqtime(&hp->bh_tstamp);
1449 #else
1450 hp->bh_tstamp = time;
1451 #endif
1452 hp->bh_datalen = pktlen;
1453 hp->bh_hdrlen = hdrlen;
1454 /*
1455 * Copy the packet data into the store buffer and update its length.
1456 */
1457 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1458 d->bd_slen = curlen + totlen;
1459 }
1460
1461 /*
1462 * Initialize all nonzero fields of a descriptor.
1463 */
1464 static int
1465 bpf_allocbufs(struct bpf_d *d)
1466 {
1467 d->bd_fbuf = (caddr_t) _MALLOC(d->bd_bufsize, M_DEVBUF, M_WAIT);
1468 if (d->bd_fbuf == 0)
1469 return (ENOBUFS);
1470
1471 d->bd_sbuf = (caddr_t) _MALLOC(d->bd_bufsize, M_DEVBUF, M_WAIT);
1472 if (d->bd_sbuf == 0) {
1473 FREE(d->bd_fbuf, M_DEVBUF);
1474 return (ENOBUFS);
1475 }
1476 d->bd_slen = 0;
1477 d->bd_hlen = 0;
1478 return (0);
1479 }
1480
1481 /*
1482 * Free buffers currently in use by a descriptor.
1483 * Called on close.
1484 */
1485 static void
1486 bpf_freed(struct bpf_d *d)
1487 {
1488 /*
1489 * We don't need to lock out interrupts since this descriptor has
1490 * been detached from its interface and it yet hasn't been marked
1491 * free.
1492 */
1493 if (d->bd_sbuf != 0) {
1494 FREE(d->bd_sbuf, M_DEVBUF);
1495 if (d->bd_hbuf != 0)
1496 FREE(d->bd_hbuf, M_DEVBUF);
1497 if (d->bd_fbuf != 0)
1498 FREE(d->bd_fbuf, M_DEVBUF);
1499 }
1500 if (d->bd_filter)
1501 FREE((caddr_t)d->bd_filter, M_DEVBUF);
1502 }
1503
1504 /*
1505 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1506 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1507 * size of the link header (variable length headers not yet supported).
1508 */
1509 void
1510 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1511 {
1512 struct bpf_if *bp;
1513 bp = (struct bpf_if *) _MALLOC(sizeof(*bp), M_DEVBUF, M_WAIT);
1514 if (bp == 0)
1515 panic("bpfattach");
1516
1517 lck_mtx_lock(bpf_mlock);
1518
1519 bp->bif_dlist = 0;
1520 bp->bif_ifp = ifp;
1521 bp->bif_dlt = dlt;
1522
1523 bp->bif_next = bpf_iflist;
1524 bpf_iflist = bp;
1525
1526 bp->bif_ifp->if_bpf = 0;
1527
1528 /*
1529 * Compute the length of the bpf header. This is not necessarily
1530 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1531 * that the network layer header begins on a longword boundary (for
1532 * performance reasons and to alleviate alignment restrictions).
1533 */
1534 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1535
1536 /* Take a reference on the interface */
1537 ifp_reference(ifp);
1538
1539 lck_mtx_unlock(bpf_mlock);
1540
1541 #ifndef __APPLE__
1542 if (bootverbose)
1543 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1544 #endif
1545 }
1546
1547 /*
1548 * Detach bpf from an interface. This involves detaching each descriptor
1549 * associated with the interface, and leaving bd_bif NULL. Notify each
1550 * descriptor as it's detached so that any sleepers wake up and get
1551 * ENXIO.
1552 */
1553 void
1554 bpfdetach(struct ifnet *ifp)
1555 {
1556 struct bpf_if *bp, *bp_prev;
1557 struct bpf_d *d;
1558 int s;
1559
1560 s = splimp();
1561
1562 lck_mtx_lock(bpf_mlock);
1563
1564 /* Locate BPF interface information */
1565 bp_prev = NULL;
1566 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1567 if (ifp == bp->bif_ifp)
1568 break;
1569 bp_prev = bp;
1570 }
1571
1572 #ifdef __APPLE__
1573 /* Check for no BPF interface information */
1574 if (bp == NULL) {
1575 return;
1576 }
1577 #endif
1578
1579 /* Interface wasn't attached */
1580 if (bp->bif_ifp == NULL) {
1581 splx(s);
1582 #ifndef __APPLE__
1583 printf("bpfdetach: %s%d was not attached\n", ifp->if_name,
1584 ifp->if_unit);
1585 #endif
1586 return;
1587 }
1588
1589 while ((d = bp->bif_dlist) != NULL) {
1590 bpf_detachd(d);
1591 bpf_wakeup(d);
1592 }
1593
1594 if (bp_prev) {
1595 bp_prev->bif_next = bp->bif_next;
1596 } else {
1597 bpf_iflist = bp->bif_next;
1598 }
1599
1600 ifp_release(ifp);
1601
1602 lck_mtx_unlock(bpf_mlock);
1603
1604 FREE(bp, M_DEVBUF);
1605
1606 splx(s);
1607 }
1608
1609 void
1610 bpf_init(__unused void *unused)
1611 {
1612 #ifdef __APPLE__
1613 int i;
1614 int maj;
1615
1616 if (bpf_devsw_installed == 0) {
1617 bpf_devsw_installed = 1;
1618
1619 bpf_mlock_grp_attr = lck_grp_attr_alloc_init();
1620 lck_grp_attr_setdefault(bpf_mlock_grp_attr);
1621
1622 bpf_mlock_grp = lck_grp_alloc_init("bpf", bpf_mlock_grp_attr);
1623
1624 bpf_mlock_attr = lck_attr_alloc_init();
1625 lck_attr_setdefault(bpf_mlock_attr);
1626
1627 bpf_mlock = lck_mtx_alloc_init(bpf_mlock_grp, bpf_mlock_attr);
1628
1629 if (bpf_mlock == 0) {
1630 printf("bpf_init: failed to allocate bpf_mlock\n");
1631 bpf_devsw_installed = 0;
1632 return;
1633 }
1634
1635 maj = cdevsw_add(CDEV_MAJOR, &bpf_cdevsw);
1636 if (maj == -1) {
1637 if (bpf_mlock)
1638 lck_mtx_free(bpf_mlock, bpf_mlock_grp);
1639 if (bpf_mlock_attr)
1640 lck_attr_free(bpf_mlock_attr);
1641 if (bpf_mlock_grp)
1642 lck_grp_free(bpf_mlock_grp);
1643 if (bpf_mlock_grp_attr)
1644 lck_grp_attr_free(bpf_mlock_grp_attr);
1645
1646 bpf_mlock = 0;
1647 bpf_mlock_attr = 0;
1648 bpf_mlock_grp = 0;
1649 bpf_mlock_grp_attr = 0;
1650 bpf_devsw_installed = 0;
1651 printf("bpf_init: failed to allocate a major number!\n");
1652 return;
1653 }
1654
1655 for (i = 0 ; i < NBPFILTER; i++)
1656 bpf_make_dev_t(maj);
1657 }
1658 #else
1659 cdevsw_add(&bpf_cdevsw);
1660 #endif
1661 }
1662
1663 #ifndef __APPLE__
1664 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1665 #endif
1666
1667 #else /* !BPF */
1668 #ifndef __APPLE__
1669 /*
1670 * NOP stubs to allow bpf-using drivers to load and function.
1671 *
1672 * A 'better' implementation would allow the core bpf functionality
1673 * to be loaded at runtime.
1674 */
1675
1676 void
1677 bpf_tap(ifp, pkt, pktlen)
1678 struct ifnet *ifp;
1679 register u_char *pkt;
1680 register u_int pktlen;
1681 {
1682 }
1683
1684 void
1685 bpf_mtap(ifp, m)
1686 struct ifnet *ifp;
1687 struct mbuf *m;
1688 {
1689 }
1690
1691 void
1692 bpfattach(ifp, dlt, hdrlen)
1693 struct ifnet *ifp;
1694 u_int dlt, hdrlen;
1695 {
1696 }
1697
1698 void
1699 bpfdetach(ifp)
1700 struct ifnet *ifp;
1701 {
1702 }
1703
1704 u_int
1705 bpf_filter(pc, p, wirelen, buflen)
1706 register const struct bpf_insn *pc;
1707 register u_char *p;
1708 u_int wirelen;
1709 register u_int buflen;
1710 {
1711 return -1; /* "no filter" behaviour */
1712 }
1713 #endif /* !defined(__APPLE__) */
1714 #endif /* NBPFILTER > 0 */