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