2  * Copyright (c) 2000-2007 Apple Inc. All rights reserved. 
   4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 
   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. 
  15  * Please obtain a copy of the License at 
  16  * http://www.opensource.apple.com/apsl/ and read it before using this file. 
  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. 
  26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 
  29  * Copyright (c) 1990, 1991, 1993 
  30  *      The Regents of the University of California.  All rights reserved. 
  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. 
  37  * Redistribution and use in source and binary forms, with or without 
  38  * modification, are permitted provided that the following conditions 
  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. 
  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 
  65  *      @(#)bpf.c       8.2 (Berkeley) 3/28/94 
  67  * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.5 2001/01/05 04:49:09 jdp Exp $ 
  70  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce 
  71  * support for mandatory and extensible security protections.  This notice 
  72  * is included in support of clause 2.2 (b) of the Apple Public License, 
  81 #define inline __inline 
  84 #include <sys/param.h> 
  85 #include <sys/systm.h> 
  87 #include <sys/malloc.h> 
  91 #include <sys/signalvar.h> 
  92 #include <sys/filio.h> 
  93 #include <sys/sockio.h> 
  94 #include <sys/ttycom.h> 
  95 #include <sys/filedesc.h> 
  96 #include <sys/uio_internal.h> 
  98 #if defined(sparc) && BSD < 199103 
  99 #include <sys/stream.h> 
 101 #include <sys/poll.h> 
 103 #include <sys/socket.h> 
 104 #include <sys/vnode.h> 
 108 #include <net/bpfdesc.h> 
 110 #include <netinet/in.h> 
 111 #include <netinet/if_ether.h> 
 112 #include <sys/kernel.h> 
 113 #include <sys/sysctl.h> 
 114 #include <net/firewire.h> 
 116 #include <miscfs/devfs/devfs.h> 
 117 #include <net/dlil.h> 
 119 #include <kern/locks.h> 
 122 #include <security/mac_framework.h> 
 125 extern int tvtohz(struct timeval 
*); 
 128  * Older BSDs don't have kernel malloc. 
 132 static caddr_t 
bpf_alloc(); 
 133 #include <net/bpf_compat.h> 
 134 #define BPF_BUFSIZE (MCLBYTES-8) 
 135 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio) 
 137 #define BPF_BUFSIZE 4096 
 138 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio) 
 142 #define PRINET  26                      /* interruptible */ 
 145  * The default read buffer size is patchable. 
 147 static unsigned int bpf_bufsize 
= BPF_BUFSIZE
; 
 148 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
,  
 149         &bpf_bufsize
, 0, ""); 
 150 static unsigned int bpf_maxbufsize 
= BPF_MAXBUFSIZE
; 
 151 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
,  
 152         &bpf_maxbufsize
, 0, ""); 
 153 static unsigned int bpf_maxdevices 
= 256; 
 154 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
,  
 155         &bpf_maxdevices
, 0, ""); 
 158  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet 
 159  *  bpf_dtab holds pointer to the descriptors, indexed by minor device # 
 161 static struct bpf_if    
*bpf_iflist
; 
 164  * BSD now stores the bpf_d in the dev_t which is a struct 
 165  * on their system. Our dev_t is an int, so we still store 
 166  * the bpf_d in a separate table indexed by minor device #. 
 168  * The value stored in bpf_dtab[n] represent three states: 
 169  *  0: device not opened 
 170  *  1: device opening or closing 
 171  *  other: device <n> opened with pointer to storage 
 173 static struct bpf_d     
**bpf_dtab 
= NULL
; 
 174 static unsigned int bpf_dtab_size 
= 0; 
 175 static unsigned int     nbpfilter 
= 0; 
 177 static lck_mtx_t                
*bpf_mlock
; 
 178 static lck_grp_t                
*bpf_mlock_grp
; 
 179 static lck_grp_attr_t   
*bpf_mlock_grp_attr
; 
 180 static lck_attr_t               
*bpf_mlock_attr
; 
 183  * Mark a descriptor free by making it point to itself. 
 184  * This is probably cheaper than marking with a constant since 
 185  * the address should be in a register anyway. 
 187 #endif /* __APPLE__ */ 
 189 static int      bpf_allocbufs(struct bpf_d 
*); 
 190 static errno_t  
bpf_attachd(struct bpf_d 
*d
, struct bpf_if 
*bp
); 
 191 static void     bpf_detachd(struct bpf_d 
*d
); 
 192 static void     bpf_freed(struct bpf_d 
*); 
 193 static void     bpf_mcopy(const void *, void *, size_t); 
 194 static int      bpf_movein(struct uio 
*, int, 
 195                     struct mbuf 
**, struct sockaddr 
*, int *); 
 196 static int      bpf_setif(struct bpf_d 
*, ifnet_t ifp
, u_int32_t dlt
); 
 197 static void bpf_wakeup(struct bpf_d 
*); 
 198 static void     catchpacket(struct bpf_d 
*, u_char 
*, u_int
, 
 199                     u_int
, void (*)(const void *, void *, size_t)); 
 200 static void     reset_d(struct bpf_d 
*); 
 201 static int bpf_setf(struct bpf_d 
*, u_int bf_len
, user_addr_t bf_insns
); 
 202 static int      bpf_getdltlist(struct bpf_d 
*, struct bpf_dltlist 
*); 
 203 static int      bpf_setdlt(struct bpf_d 
*, u_int
); 
 205 /*static  void *bpf_devfs_token[MAXBPFILTER];*/ 
 207 static  int bpf_devsw_installed
; 
 209 void bpf_init(void *unused
); 
 210 static int bpf_tap_callback(struct ifnet 
*ifp
, struct mbuf 
*m
); 
 213  * Darwin differs from BSD here, the following are static 
 214  * on BSD and not static on Darwin. 
 220          ioctl_fcn_t    bpfioctl
; 
 221         select_fcn_t    bpfpoll
; 
 224 /* Darwin's cdevsw struct differs slightly from BSDs */ 
 225 #define CDEV_MAJOR 23 
 226 static struct cdevsw bpf_cdevsw 
= { 
 228         /* close */     bpfclose
, 
 230         /* write */     bpfwrite
, 
 231         /* ioctl */     bpfioctl
, 
 233         /* reset */             eno_reset
, 
 235         /* select */    bpfpoll
, 
 237         /* strategy*/   eno_strat
, 
 243 #define SOCKADDR_HDR_LEN           offsetof(struct sockaddr, sa_data) 
 246 bpf_movein(struct uio 
*uio
, int linktype
, struct mbuf 
**mp
, struct sockaddr 
*sockp
, int *datlen
) 
 264                 sa_family 
= AF_UNSPEC
; 
 265                 /* XXX Would MAXLINKHDR be better? */ 
 266                 hlen 
= sizeof(struct ether_header
); 
 271         #if defined(__FreeBSD__) || defined(__bsdi__) 
 272                 sa_family 
= AF_IMPLINK
; 
 275                 sa_family 
= AF_UNSPEC
; 
 276                 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */ 
 284                 sa_family 
= AF_UNSPEC
; 
 289         case DLT_ATM_RFC1483
: 
 291                  * en atm driver requires 4-byte atm pseudo header. 
 292                  * though it isn't standard, vpi:vci needs to be 
 295                 sa_family 
= AF_UNSPEC
; 
 296                 hlen 
= 12;      /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */ 
 301                 sa_family 
= AF_UNSPEC
; 
 302                 hlen 
= 4;       /* This should match PPP_HDRLEN */ 
 305         case DLT_APPLE_IP_OVER_IEEE1394
: 
 306                 sa_family 
= AF_UNSPEC
; 
 307                 hlen 
= sizeof(struct firewire_header
); 
 314         // LP64todo - fix this! 
 315         len 
= uio_resid(uio
); 
 316         *datlen 
= len 
- hlen
; 
 317         if ((unsigned)len 
> MCLBYTES
) 
 322                  * Build a sockaddr based on the data link layer type. 
 323                  * We do this at this level because the ethernet header 
 324                  * is copied directly into the data field of the sockaddr. 
 325                  * In the case of SLIP, there is no header and the packet 
 326                  * is forwarded as is. 
 327                  * Also, we are careful to leave room at the front of the mbuf 
 328                  * for the link level header. 
 330                 if ((hlen 
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) { 
 333                 sockp
->sa_family 
= sa_family
; 
 336                  * We're directly sending the packet data supplied by 
 337                  * the user; we don't need to make room for the link 
 338                  * header, and don't need the header length value any 
 339                  * more, so set it to 0. 
 344         MGETHDR(m
, M_WAIT
, MT_DATA
); 
 347         if ((unsigned)len 
> MHLEN
) { 
 350                 if ((m
->m_flags 
& M_EXT
) == 0) { 
 353                 if (m
->m_len 
!= MCLBYTES
) { 
 359         m
->m_pkthdr
.len 
= m
->m_len 
= len
; 
 360         m
->m_pkthdr
.rcvif 
= NULL
; 
 363          * Make room for link header. 
 366                 m
->m_pkthdr
.len 
-= hlen
; 
 369                 m
->m_data 
+= hlen
; /* XXX */ 
 373                 error 
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
); 
 377         error 
= UIOMOVE(mtod(m
, caddr_t
), len 
- hlen
, UIO_WRITE
, uio
); 
 388  * The dynamic addition of a new device node must block all processes that are opening  
 389  * the last device so that no process will get an unexpected ENOENT  
 392 bpf_make_dev_t(int maj
) 
 394         static int              bpf_growing 
= 0; 
 395         unsigned int    cur_size 
= nbpfilter
, i
; 
 397         if (nbpfilter 
>= bpf_maxdevices
) 
 400         while (bpf_growing
) { 
 401                 /* Wait until new device has been created */ 
 402                 (void)tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0); 
 404         if (nbpfilter 
> cur_size
) { 
 405                 /* other thread grew it already */ 
 410         /* need to grow bpf_dtab first */ 
 411         if (nbpfilter 
== bpf_dtab_size
) { 
 413                 struct bpf_d 
**new_dtab 
= NULL
; 
 414                 struct bpf_d 
**old_dtab 
= NULL
; 
 416                 new_dtab_size 
= bpf_dtab_size 
+ NBPFILTER
;       
 417                 new_dtab 
= (struct bpf_d 
**)_MALLOC(sizeof(struct bpf_d 
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
); 
 419                         printf("bpf_make_dev_t: malloc bpf_dtab failed\n"); 
 423                         bcopy(bpf_dtab
, new_dtab
,  
 424                                   sizeof(struct bpf_d 
*) * bpf_dtab_size
); 
 426                 bzero(new_dtab 
+ bpf_dtab_size
,  
 427                           sizeof(struct bpf_d 
*) * NBPFILTER
); 
 430                 bpf_dtab_size 
= new_dtab_size
; 
 431                 if (old_dtab 
!= NULL
) 
 432                         _FREE(old_dtab
, M_DEVBUF
); 
 435         (void) devfs_make_node(makedev(maj
, i
), 
 436                                 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600, 
 440         wakeup((caddr_t
)&bpf_growing
); 
 446  * Attach file to the bpf interface, i.e. make d listen on bp. 
 449 bpf_attachd(struct bpf_d 
*d
, struct bpf_if 
*bp
) 
 451         int first 
= bp
->bif_dlist 
== NULL
; 
 455          * Point d at bp, and add d to the interface's list of listeners. 
 456          * Finally, point the driver's bpf cookie at the interface so 
 457          * it will divert packets to bpf. 
 460         d
->bd_next 
= bp
->bif_dlist
; 
 464                 /* Find the default bpf entry for this ifp */ 
 465                 if (bp
->bif_ifp
->if_bpf 
== NULL
) { 
 466                         struct bpf_if   
*primary
; 
 468                         for (primary 
= bpf_iflist
; primary 
&& primary
->bif_ifp 
!= bp
->bif_ifp
; 
 469                                  primary 
= primary
->bif_next
) 
 472                         bp
->bif_ifp
->if_bpf 
= primary
; 
 475                 /* Only call dlil_set_bpf_tap for primary dlt */ 
 476                 if (bp
->bif_ifp
->if_bpf 
== bp
) 
 477                         dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
); 
 480                         error 
= bp
->bif_tap(bp
->bif_ifp
, bp
->bif_dlt
, BPF_TAP_INPUT_OUTPUT
); 
 487  * Detach a file from its interface. 
 490 bpf_detachd(struct bpf_d 
*d
) 
 496         ifp 
= d
->bd_bif
->bif_ifp
; 
 499         /* Remove d from the interface's descriptor list. */ 
 504                         panic("bpf_detachd: descriptor not in list"); 
 507         if (bp
->bif_dlist 
== 0) { 
 509                  * Let the driver know that there are no more listeners. 
 511                 /* Only call dlil_set_bpf_tap for primary dlt */ 
 512                 if (bp
->bif_ifp
->if_bpf 
== bp
) 
 513                         dlil_set_bpf_tap(ifp
, BPF_TAP_DISABLE
, NULL
); 
 515                         bp
->bif_tap(ifp
, bp
->bif_dlt
, BPF_TAP_DISABLE
); 
 517                 for (bp 
= bpf_iflist
; bp
; bp 
= bp
->bif_next
) 
 518                         if (bp
->bif_ifp 
== ifp 
&& bp
->bif_dlist 
!= 0) 
 525          * Check if this descriptor had requested promiscuous mode. 
 526          * If so, turn it off. 
 530                 lck_mtx_unlock(bpf_mlock
); 
 531                 if (ifnet_set_promiscuous(ifp
, 0)) { 
 533                          * Something is really wrong if we were able to put 
 534                          * the driver into promiscuous mode, but can't 
 536                          * Most likely the network interface is gone. 
 538                         printf("bpf: ifnet_set_promiscuous failed"); 
 540                 lck_mtx_lock(bpf_mlock
); 
 546  * Open ethernet device.  Returns ENXIO for illegal minor device number, 
 547  * EBUSY if file is open by another process. 
 551 bpfopen(dev_t dev
, __unused 
int flags
, __unused 
int fmt
, 
 552         __unused 
struct proc 
*p
) 
 556         lck_mtx_lock(bpf_mlock
); 
 557         if ((unsigned int) minor(dev
) >= nbpfilter
) { 
 558                 lck_mtx_unlock(bpf_mlock
); 
 562          * New device nodes are created on demand when opening the last one.  
 563          * The programming model is for processes to loop on the minor starting at 0  
 564          * as long as EBUSY is returned. The loop stops when either the open succeeds or  
 565          * an error other that EBUSY is returned. That means that bpf_make_dev_t() must  
 566          * block all processes that are opening the last  node. If not all  
 567          * processes are blocked, they could unexpectedly get ENOENT and abort their  
 570         if ((unsigned int) minor(dev
) == (nbpfilter 
- 1)) 
 571                 bpf_make_dev_t(major(dev
)); 
 574          * Each minor can be opened by only one process.  If the requested  
 575          * minor is in use, return EBUSY. 
 577          * Important: bpfopen() and bpfclose() have to check and set the status of a device 
 578          * in the same lockin context otherwise the device may be leaked because the vnode use count  
 579          * will be unpextectly greater than 1 when close() is called. 
 581         if (bpf_dtab
[minor(dev
)] == 0) { 
 582                 bpf_dtab
[minor(dev
)] = (void *)1;       /* Mark opening */ 
 584                 lck_mtx_unlock(bpf_mlock
); 
 587         d 
= (struct bpf_d 
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
, M_WAIT
); 
 589                 /* this really is a catastrophic failure */ 
 590                 printf("bpfopen: malloc bpf_d failed\n"); 
 591                 bpf_dtab
[minor(dev
)] = NULL
; 
 592                 lck_mtx_unlock(bpf_mlock
); 
 595         bzero(d
, sizeof(struct bpf_d
)); 
 598          * It is not necessary to take the BPF lock here because no other  
 599          * thread can access the device until it is marked opened... 
 602         /* Mark "in use" and do most initialization. */ 
 603         d
->bd_bufsize 
= bpf_bufsize
; 
 607         mac_bpfdesc_label_init(d
); 
 608         mac_bpfdesc_label_associate(kauth_cred_get(), d
); 
 610         bpf_dtab
[minor(dev
)] = d
;                               /* Mark opened */ 
 611         lck_mtx_unlock(bpf_mlock
); 
 617  * Close the descriptor by detaching it from its interface, 
 618  * deallocating its buffers, and marking it free. 
 622 bpfclose(dev_t dev
, __unused 
int flags
, __unused 
int fmt
, 
 623          __unused 
struct proc 
*p
) 
 627         /* Take BPF lock to ensure no other thread is using the device */ 
 628         lck_mtx_lock(bpf_mlock
); 
 630         d 
= bpf_dtab
[minor(dev
)]; 
 631         if (d 
== 0 || d 
== (void *)1) { 
 632                 lck_mtx_unlock(bpf_mlock
); 
 635         bpf_dtab
[minor(dev
)] = (void *)1;               /* Mark closing */ 
 639         selthreadclear(&d
->bd_sel
); 
 641         mac_bpfdesc_label_destroy(d
); 
 645         /* Mark free in same context as bpfopen comes to check */ 
 646         bpf_dtab
[minor(dev
)] = NULL
;                    /* Mark closed */ 
 647         lck_mtx_unlock(bpf_mlock
); 
 655 #define BPF_SLEEP bpf_sleep 
 658 bpf_sleep(struct bpf_d 
*d
, int pri
, const char *wmesg
, int timo
) 
 662         lck_mtx_unlock(bpf_mlock
); 
 664         st 
= tsleep((caddr_t
)d
, pri
, wmesg
, timo
); 
 666         lck_mtx_lock(bpf_mlock
); 
 672  * Rotate the packet buffers in descriptor d.  Move the store buffer 
 673  * into the hold slot, and the free buffer into the store slot. 
 674  * Zero the length of the new store buffer. 
 676 #define ROTATE_BUFFERS(d) \ 
 677         (d)->bd_hbuf = (d)->bd_sbuf; \ 
 678         (d)->bd_hlen = (d)->bd_slen; \ 
 679         (d)->bd_sbuf = (d)->bd_fbuf; \ 
 683  *  bpfread - read next chunk of packets from buffers 
 686 bpfread(dev_t dev
, struct uio 
*uio
, int ioflag
) 
 691         lck_mtx_lock(bpf_mlock
); 
 693         d 
= bpf_dtab
[minor(dev
)]; 
 694         if (d 
== 0 || d 
== (void *)1) { 
 695                 lck_mtx_unlock(bpf_mlock
); 
 701          * Restrict application to use a buffer the same size as 
 704         // LP64todo - fix this 
 705         if (uio
->uio_resid 
!= d
->bd_bufsize
) { 
 706                 lck_mtx_unlock(bpf_mlock
); 
 711          * If the hold buffer is empty, then do a timed sleep, which 
 712          * ends when the timeout expires or when enough packets 
 713          * have arrived to fill the store buffer. 
 715         while (d
->bd_hbuf 
== 0) { 
 716                 if (d
->bd_immediate 
&& d
->bd_slen 
!= 0) { 
 718                          * A packet(s) either arrived since the previous 
 719                          * read or arrived while we were asleep. 
 720                          * Rotate the buffers and return what's here. 
 727                  * No data is available, check to see if the bpf device 
 728                  * is still pointed at a real interface.  If not, return 
 729                  * ENXIO so that the userland process knows to rebind 
 730                  * it before using it again. 
 732                 if (d
->bd_bif 
== NULL
) { 
 733                         lck_mtx_unlock(bpf_mlock
); 
 737                 if (ioflag 
& IO_NDELAY
) 
 740                         error 
= BPF_SLEEP(d
, PRINET
|PCATCH
, "bpf", 
 743                  * Make sure device is still opened 
 745                 d 
= bpf_dtab
[minor(dev
)]; 
 746                 if (d 
== 0 || d 
== (void *)1) { 
 747                         lck_mtx_unlock(bpf_mlock
); 
 750                 if (error 
== EINTR 
|| error 
== ERESTART
) { 
 751                         lck_mtx_unlock(bpf_mlock
); 
 754                 if (error 
== EWOULDBLOCK
) { 
 756                          * On a timeout, return what's in the buffer, 
 757                          * which may be nothing.  If there is something 
 758                          * in the store buffer, we can rotate the buffers. 
 762                                  * We filled up the buffer in between 
 763                                  * getting the timeout and arriving 
 764                                  * here, so we don't need to rotate. 
 768                         if (d
->bd_slen 
== 0) { 
 769                                 lck_mtx_unlock(bpf_mlock
); 
 777          * At this point, we know we have something in the hold slot. 
 781          * Move data from hold buffer into user space. 
 782          * We know the entire buffer is transferred since 
 783          * we checked above that the read buffer is bpf_bufsize bytes. 
 785         error 
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
); 
 787         d
->bd_fbuf 
= d
->bd_hbuf
; 
 790         lck_mtx_unlock(bpf_mlock
); 
 796  * If there are processes sleeping on this descriptor, wake them up. 
 799 bpf_wakeup(struct bpf_d 
*d
) 
 802         if (d
->bd_async 
&& d
->bd_sig 
&& d
->bd_sigio
) 
 803                 pgsigio(d
->bd_sigio
, d
->bd_sig
); 
 806         selwakeup(&d
->bd_sel
); 
 809         d
->bd_sel
.si_pid 
= 0; 
 813                 selwakeup(d
->bd_selproc
, (int)d
->bd_selcoll
); 
 820 /* keep in sync with bpf_movein above: */ 
 821 #define MAX_DATALINK_HDR_LEN    (sizeof(struct firewire_header)) 
 824 bpfwrite(dev_t dev
, struct uio 
*uio
, __unused 
int ioflag
) 
 828         struct mbuf 
*m 
= NULL
; 
 830         char              dst_buf
[SOCKADDR_HDR_LEN 
+ MAX_DATALINK_HDR_LEN
]; 
 833         lck_mtx_lock(bpf_mlock
); 
 835         d 
= bpf_dtab
[minor(dev
)]; 
 836         if (d 
== 0 || d 
== (void *)1) { 
 837                 lck_mtx_unlock(bpf_mlock
); 
 840         if (d
->bd_bif 
== 0) { 
 841                 lck_mtx_unlock(bpf_mlock
); 
 845         ifp 
= d
->bd_bif
->bif_ifp
; 
 847         if (uio
->uio_resid 
== 0) { 
 848                 lck_mtx_unlock(bpf_mlock
); 
 851         ((struct sockaddr 
*)dst_buf
)->sa_len 
= sizeof(dst_buf
); 
 852         error 
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, &m
,  
 853                            d
->bd_hdrcmplt 
? NULL 
: (struct sockaddr 
*)dst_buf
, 
 856                 lck_mtx_unlock(bpf_mlock
); 
 860         if ((unsigned)datlen 
> ifp
->if_mtu
) { 
 861                 lck_mtx_unlock(bpf_mlock
); 
 866         if ((error 
= ifp_use(ifp
, kIfNetUseCount_MustNotBeZero
)) != 0) { 
 867                 lck_mtx_unlock(bpf_mlock
); 
 873         mac_mbuf_label_associate_bpfdesc(d
, m
); 
 875         lck_mtx_unlock(bpf_mlock
); 
 877         if (d
->bd_hdrcmplt
) { 
 878                 if (d
->bd_bif
->bif_send
) 
 879                         error 
= d
->bd_bif
->bif_send(ifp
, d
->bd_bif
->bif_dlt
, m
); 
 881                         error 
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1); 
 884                 error 
= dlil_output(ifp
, PF_INET
, m
, NULL
, (struct sockaddr 
*)dst_buf
, 0); 
 887         if (ifp_unuse(ifp
) != 0) 
 888                 ifp_use_reached_zero(ifp
); 
 891          * The driver frees the mbuf. 
 897  * Reset a descriptor by flushing its packet buffer and clearing the 
 898  * receive and drop counts. 
 901 reset_d(struct bpf_d 
*d
) 
 904                 /* Free the hold buffer. */ 
 905                 d
->bd_fbuf 
= d
->bd_hbuf
; 
 915  *  FIONREAD            Check for read packet available. 
 916  *  SIOCGIFADDR         Get interface address - convenient hook to driver. 
 917  *  BIOCGBLEN           Get buffer len [for read()]. 
 918  *  BIOCSETF            Set ethernet read filter. 
 919  *  BIOCFLUSH           Flush read packet buffer. 
 920  *  BIOCPROMISC         Put interface into promiscuous mode. 
 921  *  BIOCGDLT            Get link layer type. 
 922  *  BIOCGETIF           Get interface name. 
 923  *  BIOCSETIF           Set interface. 
 924  *  BIOCSRTIMEOUT       Set read timeout. 
 925  *  BIOCGRTIMEOUT       Get read timeout. 
 926  *  BIOCGSTATS          Get packet stats. 
 927  *  BIOCIMMEDIATE       Set immediate mode. 
 928  *  BIOCVERSION         Get filter language version. 
 929  *  BIOCGHDRCMPLT       Get "header already complete" flag 
 930  *  BIOCSHDRCMPLT       Set "header already complete" flag 
 931  *  BIOCGSEESENT        Get "see packets sent" flag 
 932  *  BIOCSSEESENT        Set "see packets sent" flag 
 936 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused 
int flags
, 
 937                  __unused 
struct proc 
*p
) 
 942         lck_mtx_lock(bpf_mlock
); 
 944         d 
= bpf_dtab
[minor(dev
)]; 
 945         if (d 
== 0 || d 
== (void *)1) { 
 946                 lck_mtx_unlock(bpf_mlock
); 
 957          * Check for read packet available. 
 978                                 ifp 
= d
->bd_bif
->bif_ifp
; 
 979                                 error 
= ifnet_ioctl(ifp
, 0, cmd
, addr
); 
 985          * Get buffer len [for read()]. 
 988                 *(u_int 
*)addr 
= d
->bd_bufsize
; 
1001                         u_int size 
= *(u_int 
*)addr
; 
1003                         if (size 
> bpf_maxbufsize
) 
1004                                 *(u_int 
*)addr 
= size 
= bpf_maxbufsize
; 
1005                         else if (size 
< BPF_MINBUFSIZE
) 
1006                                 *(u_int 
*)addr 
= size 
= BPF_MINBUFSIZE
; 
1007                         d
->bd_bufsize 
= size
; 
1013          * Set link layer read filter. 
1017                 if (proc_is64bit(current_proc())) { 
1018                     struct bpf_program64 
*      prg64
; 
1020                     prg64 
= (struct bpf_program64 
*)addr
; 
1021                     error 
= bpf_setf(d
, prg64
->bf_len
, 
1025                     struct bpf_program 
*        prg
; 
1027                     prg 
= (struct bpf_program 
*)addr
; 
1028                     error 
= bpf_setf(d
, prg
->bf_len
, 
1029                                      CAST_USER_ADDR_T(prg
->bf_insns
)); 
1034          * Flush read packet buffer. 
1041          * Put interface into promiscuous mode. 
1044                 if (d
->bd_bif 
== 0) { 
1046                          * No interface attached yet. 
1051                 if (d
->bd_promisc 
== 0) { 
1052                         lck_mtx_unlock(bpf_mlock
); 
1053                         error 
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1); 
1054                         lck_mtx_lock(bpf_mlock
); 
1061          * Get device parameters. 
1067                         *(u_int 
*)addr 
= d
->bd_bif
->bif_dlt
; 
1071          * Get a list of supported data link types. 
1074                         if (d
->bd_bif 
== NULL
) 
1077                                         error 
= bpf_getdltlist(d
, (struct bpf_dltlist 
*)addr
); 
1081          * Set data link type. 
1084                         if (d
->bd_bif 
== NULL
) 
1087                                         error 
= bpf_setdlt(d
, *(u_int 
*)addr
); 
1091          * Get interface name. 
1097                         struct ifnet 
*const ifp 
= d
->bd_bif
->bif_ifp
; 
1098                         struct ifreq 
*const ifr 
= (struct ifreq 
*)addr
; 
1100                         snprintf(ifr
->ifr_name
, sizeof(ifr
->ifr_name
), 
1101                             "%s%d", ifp
->if_name
, ifp
->if_unit
); 
1110                 ifp 
= ifunit(((struct ifreq 
*)addr
)->ifr_name
); 
1114                         error 
= bpf_setif(d
, ifp
, 0); 
1123                         struct timeval 
*tv 
= (struct timeval 
*)addr
; 
1126                          * Subtract 1 tick from tvtohz() since this isn't 
1129                         if ((error 
= itimerfix(tv
)) == 0) 
1130                                 d
->bd_rtout 
= tvtohz(tv
) - 1; 
1139                         struct timeval 
*tv 
= (struct timeval 
*)addr
; 
1141                         tv
->tv_sec 
= d
->bd_rtout 
/ hz
; 
1142                         tv
->tv_usec 
= (d
->bd_rtout 
% hz
) * tick
; 
1151                         struct bpf_stat 
*bs 
= (struct bpf_stat 
*)addr
; 
1153                         bs
->bs_recv 
= d
->bd_rcount
; 
1154                         bs
->bs_drop 
= d
->bd_dcount
; 
1159          * Set immediate mode. 
1162                 d
->bd_immediate 
= *(u_int 
*)addr
; 
1167                         struct bpf_version 
*bv 
= (struct bpf_version 
*)addr
; 
1169                         bv
->bv_major 
= BPF_MAJOR_VERSION
; 
1170                         bv
->bv_minor 
= BPF_MINOR_VERSION
; 
1175          * Get "header already complete" flag 
1178                 *(u_int 
*)addr 
= d
->bd_hdrcmplt
; 
1182          * Set "header already complete" flag 
1185                 d
->bd_hdrcmplt 
= *(u_int 
*)addr 
? 1 : 0; 
1189          * Get "see sent packets" flag 
1192                 *(u_int 
*)addr 
= d
->bd_seesent
; 
1196          * Set "see sent packets" flag 
1199                 d
->bd_seesent 
= *(u_int 
*)addr
; 
1202         case FIONBIO
:           /* Non-blocking I/O */ 
1205         case FIOASYNC
:          /* Send signal on receive packets */ 
1206                 d
->bd_async 
= *(int *)addr
; 
1210                 error 
= fsetown(*(int *)addr
, &d
->bd_sigio
); 
1214                 *(int *)addr 
= fgetown(d
->bd_sigio
); 
1217         /* This is deprecated, FIOSETOWN should be used instead. */ 
1219                 error 
= fsetown(-(*(int *)addr
), &d
->bd_sigio
); 
1222         /* This is deprecated, FIOGETOWN should be used instead. */ 
1224                 *(int *)addr 
= -fgetown(d
->bd_sigio
); 
1227         case BIOCSRSIG
:         /* Set receive signal */ 
1231                         sig 
= *(u_int 
*)addr
; 
1240                 *(u_int 
*)addr 
= d
->bd_sig
; 
1244         lck_mtx_unlock(bpf_mlock
); 
1250  * Set d's packet filter program to fp.  If this file already has a filter, 
1251  * free it and replace it.  Returns EINVAL for bogus requests. 
1254 bpf_setf(struct bpf_d 
*d
, u_int bf_len
, user_addr_t bf_insns
) 
1256         struct bpf_insn 
*fcode
, *old
; 
1260         if (bf_insns 
== USER_ADDR_NULL
) { 
1263                 d
->bd_filter 
= NULL
; 
1266                         FREE((caddr_t
)old
, M_DEVBUF
); 
1270         if (flen 
> BPF_MAXINSNS
) 
1273         size 
= flen 
* sizeof(struct bpf_insn
); 
1274         fcode 
= (struct bpf_insn 
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
); 
1279         if (copyin(bf_insns
, (caddr_t
)fcode
, size
) == 0 && 
1280             bpf_validate(fcode
, (int)flen
)) { 
1281                 d
->bd_filter 
= fcode
; 
1284                         FREE((caddr_t
)old
, M_DEVBUF
); 
1288         FREE((caddr_t
)fcode
, M_DEVBUF
); 
1293  * Detach a file from its current interface (if attached at all) and attach 
1294  * to the interface indicated by the name stored in ifr. 
1295  * Return an errno or 0. 
1298 bpf_setif(struct bpf_d 
*d
, ifnet_t theywant
, u_int32_t dlt
) 
1304          * Look through attached interfaces for the named one. 
1306         for (bp 
= bpf_iflist
; bp 
!= 0; bp 
= bp
->bif_next
) { 
1307                 struct ifnet 
*ifp 
= bp
->bif_ifp
; 
1309                 if (ifp 
== 0 || ifp 
!= theywant 
|| (dlt 
!= 0 && dlt 
!= bp
->bif_dlt
)) 
1312                  * We found the requested interface. 
1313                  * If it's not up, return an error. 
1314                  * Allocate the packet buffers if we need to. 
1315                  * If we're already attached to requested interface, 
1316                  * just flush the buffer. 
1318                 if ((ifp
->if_flags 
& IFF_UP
) == 0) 
1321                 if (d
->bd_sbuf 
== 0) { 
1322                         error 
= bpf_allocbufs(d
); 
1326                 if (bp 
!= d
->bd_bif
) { 
1329                                  * Detach if attached to something else. 
1333                         if (bpf_attachd(d
, bp
) != 0) { 
1347  * Get a list of available data link type of the interface. 
1352         struct bpf_dltlist 
*bfl
) 
1360         if (IS_64BIT_PROCESS(current_proc())) { 
1361                 dlist 
= CAST_USER_ADDR_T(bfl
->bfl_u
.bflu_pad
); 
1364                 dlist 
= CAST_USER_ADDR_T(bfl
->bfl_u
.bflu_list
); 
1367         ifp 
= d
->bd_bif
->bif_ifp
; 
1370         for (bp 
= bpf_iflist
; bp
; bp 
= bp
->bif_next
) { 
1371                 if (bp
->bif_ifp 
!= ifp
) 
1374                         if (n 
>= bfl
->bfl_len
) { 
1377                         error 
= copyout(&bp
->bif_dlt
, dlist
, sizeof(bp
->bif_dlt
)); 
1378                         dlist 
+= sizeof(bp
->bif_dlt
); 
1387  * Set the data link type of a BPF instance. 
1390 bpf_setdlt(struct bpf_d 
*d
, uint32_t dlt
) 
1394         int error
, opromisc
; 
1398         if (d
->bd_bif
->bif_dlt 
== dlt
) 
1400         ifp 
= d
->bd_bif
->bif_ifp
; 
1401         for (bp 
= bpf_iflist
; bp
; bp 
= bp
->bif_next
) { 
1402                 if (bp
->bif_ifp 
== ifp 
&& bp
->bif_dlt 
== dlt
) 
1406                 opromisc 
= d
->bd_promisc
; 
1408                 error 
= bpf_attachd(d
, bp
); 
1410                         printf("bpf_setdlt: bpf_attachd %s%d failed (%d)\n", 
1411                                 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
); 
1416                         lck_mtx_unlock(bpf_mlock
); 
1417                         error 
= ifnet_set_promiscuous(bp
->bif_ifp
, 1); 
1418                         lck_mtx_lock(bpf_mlock
); 
1420                                 printf("bpf_setdlt: ifpromisc %s%d failed (%d)\n", 
1421                                            ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
); 
1426         return (bp 
== NULL 
? EINVAL 
: 0); 
1430  * Support for select() and poll() system calls 
1432  * Return true iff the specific operation will not block indefinitely. 
1433  * Otherwise, return false but make a note that a selwakeup() must be done. 
1436 bpfpoll(dev_t dev
, int events
, void * wql
, struct proc 
*p
) 
1441         lck_mtx_lock(bpf_mlock
); 
1443         d 
= bpf_dtab
[minor(dev
)]; 
1444         if (d 
== 0 || d 
== (void *)1) { 
1445                 lck_mtx_unlock(bpf_mlock
); 
1450          * An imitation of the FIONREAD ioctl code. 
1452         if (d
->bd_bif 
== NULL
) { 
1453                 lck_mtx_unlock(bpf_mlock
); 
1457         if (events 
& (POLLIN 
| POLLRDNORM
)) { 
1458                 if (d
->bd_hlen 
!= 0 || (d
->bd_immediate 
&& d
->bd_slen 
!= 0)) 
1459                         revents 
|= events 
& (POLLIN 
| POLLRDNORM
); 
1461                         selrecord(p
, &d
->bd_sel
, wql
); 
1464         lck_mtx_unlock(bpf_mlock
); 
1469 _cast_non_const(const void * ptr
) { 
1480  * Copy data from an mbuf chain into a buffer.  This code is derived 
1481  * from m_copydata in sys/uipc_mbuf.c. 
1484 bpf_mcopy(const void *src_arg
, void *dst_arg
, size_t len
) 
1486         struct mbuf 
*m 
= _cast_non_const(src_arg
); 
1494                 count 
= min(m
->m_len
, len
); 
1495                 bcopy(mbuf_data(m
), dst
, count
); 
1514          * It's possible that we get here after the bpf descriptor has been 
1515          * detached from the interface; in such a case we simply return. 
1516          * Lock ordering is important since we can be called asynchronously 
1517          * (from the IOKit) to process an inbound packet; when that happens 
1518          * we would have been holding its "gateLock" and will be acquiring 
1519          * "bpf_mlock" upon entering this routine.  Due to that, we release 
1520          * "bpf_mlock" prior to calling ifnet_set_promiscuous (which will 
1521          * acquire "gateLock" in the IOKit), in order to avoid a deadlock 
1522          * when a ifnet_set_promiscuous request simultaneously collides with 
1523          * an inbound packet being passed into the tap callback. 
1525         lck_mtx_lock(bpf_mlock
); 
1526         if (ifp
->if_bpf 
== NULL
) { 
1527                 lck_mtx_unlock(bpf_mlock
); 
1531         for (bp 
= ifp
->if_bpf
; bp 
&& bp
->bif_ifp 
== ifp 
&& 
1532                  (dlt 
!= 0 && bp
->bif_dlt 
!= dlt
); bp 
= bp
->bif_next
) 
1534         if (bp 
&& bp
->bif_ifp 
== ifp 
&& bp
->bif_dlist 
!= NULL
) { 
1536                 struct m_hdr    hack_hdr
; 
1543                          * This is gross. We mock up an mbuf that points to the 
1544                          * header buffer. This means we don't have to copy the 
1545                          * header. A number of interfaces prepended headers just 
1546                          * for bpf by allocating an mbuf on the stack. We want to 
1547                          * give developers an easy way to prepend a header for bpf. 
1548                          * Since a developer allocating an mbuf on the stack is bad, 
1549                          * we do even worse here, allocating only a header to point 
1550                          * to a buffer the developer supplied. This makes assumptions 
1551                          * that bpf_filter and catchpacket will not look at anything 
1552                          * in the mbuf other than the header. This was true at the 
1553                          * time this code was written. 
1555                         hack_hdr
.mh_next 
= m
; 
1556                         hack_hdr
.mh_nextpkt 
= NULL
; 
1557                         hack_hdr
.mh_len 
= hlen
; 
1558                         hack_hdr
.mh_data 
= hdr
; 
1559                         hack_hdr
.mh_type 
= m
->m_type
; 
1560                         hack_hdr
.mh_flags 
= 0; 
1562                         m 
= (mbuf_t
)&hack_hdr
; 
1565                 for (m0 
= m
; m0 
!= 0; m0 
= m0
->m_next
) 
1566                         pktlen 
+= m0
->m_len
; 
1568                 for (d 
= bp
->bif_dlist
; d
; d 
= d
->bd_next
) { 
1569                         if (outbound 
&& !d
->bd_seesent
) 
1572                         slen 
= bpf_filter(d
->bd_filter
, (u_char 
*)m
, pktlen
, 0); 
1575                                 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) != 0) 
1578                                 catchpacket(d
, (u_char 
*)m
, pktlen
, slen
, bpf_mcopy
); 
1582         lck_mtx_unlock(bpf_mlock
); 
1593         bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 1); 
1604         bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 0); 
1607 /* Callback registered with Ethernet driver. */ 
1608 static int bpf_tap_callback(struct ifnet 
*ifp
, struct mbuf 
*m
) 
1610         bpf_tap_imp(ifp
, 0, m
, NULL
, 0, mbuf_pkthdr_rcvif(m
) == NULL
); 
1616  * Move the packet data from interface memory (pkt) into the 
1617  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full), 
1618  * otherwise 0.  "copy" is the routine called to do the actual data 
1619  * transfer.  bcopy is passed in to copy contiguous chunks, while 
1620  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case, 
1621  * pkt is really an mbuf. 
1624 catchpacket(struct bpf_d 
*d
, u_char 
*pkt
, u_int pktlen
, u_int snaplen
,  
1625         void (*cpfn
)(const void *, void *, size_t)) 
1629         int hdrlen 
= d
->bd_bif
->bif_hdrlen
; 
1631          * Figure out how many bytes to move.  If the packet is 
1632          * greater or equal to the snapshot length, transfer that 
1633          * much.  Otherwise, transfer the whole packet (unless 
1634          * we hit the buffer size limit). 
1636         totlen 
= hdrlen 
+ min(snaplen
, pktlen
); 
1637         if (totlen 
> d
->bd_bufsize
) 
1638                 totlen 
= d
->bd_bufsize
; 
1641          * Round up the end of the previous packet to the next longword. 
1643         curlen 
= BPF_WORDALIGN(d
->bd_slen
); 
1644         if (curlen 
+ totlen 
> d
->bd_bufsize
) { 
1646                  * This packet will overflow the storage buffer. 
1647                  * Rotate the buffers if we can, then wakeup any 
1650                 if (d
->bd_fbuf 
== 0) { 
1652                          * We haven't completed the previous read yet, 
1653                          * so drop the packet. 
1662         else if (d
->bd_immediate
) 
1664                  * Immediate mode is set.  A packet arrived so any 
1665                  * reads should be woken up. 
1670          * Append the bpf header. 
1672         hp 
= (struct bpf_hdr 
*)(d
->bd_sbuf 
+ curlen
); 
1673         microtime(&hp
->bh_tstamp
); 
1674         hp
->bh_datalen 
= pktlen
; 
1675         hp
->bh_hdrlen 
= hdrlen
; 
1677          * Copy the packet data into the store buffer and update its length. 
1679         (*cpfn
)(pkt
, (u_char 
*)hp 
+ hdrlen
, (hp
->bh_caplen 
= totlen 
- hdrlen
)); 
1680         d
->bd_slen 
= curlen 
+ totlen
; 
1684  * Initialize all nonzero fields of a descriptor. 
1687 bpf_allocbufs(struct bpf_d 
*d
) 
1689         d
->bd_fbuf 
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
); 
1690         if (d
->bd_fbuf 
== 0) 
1693         d
->bd_sbuf 
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
); 
1694         if (d
->bd_sbuf 
== 0) { 
1695                 FREE(d
->bd_fbuf
, M_DEVBUF
); 
1704  * Free buffers currently in use by a descriptor. 
1708 bpf_freed(struct bpf_d 
*d
) 
1711          * We don't need to lock out interrupts since this descriptor has 
1712          * been detached from its interface and it yet hasn't been marked 
1715         if (d
->bd_sbuf 
!= 0) { 
1716                 FREE(d
->bd_sbuf
, M_DEVBUF
); 
1717                 if (d
->bd_hbuf 
!= 0) 
1718                         FREE(d
->bd_hbuf
, M_DEVBUF
); 
1719                 if (d
->bd_fbuf 
!= 0) 
1720                         FREE(d
->bd_fbuf
, M_DEVBUF
); 
1723                 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
); 
1727  * Attach an interface to bpf.  driverp is a pointer to a (struct bpf_if *) 
1728  * in the driver's softc; dlt is the link layer type; hdrlen is the fixed 
1729  * size of the link header (variable length headers not yet supported). 
1732 bpfattach(struct ifnet 
*ifp
, u_int dlt
, u_int hdrlen
) 
1734         bpf_attach(ifp
, dlt
, hdrlen
, NULL
, NULL
); 
1745         struct bpf_if 
*bp_new
; 
1746         struct bpf_if 
*bp_temp
; 
1747         struct bpf_if 
*bp_first 
= NULL
; 
1749         bp_new 
= (struct bpf_if 
*) _MALLOC(sizeof(*bp_new
), M_DEVBUF
, M_WAIT
); 
1753         lck_mtx_lock(bpf_mlock
); 
1756          * Check if this interface/dlt is already attached, record first 
1757          * attachment for this interface. 
1759         for (bp_temp 
= bpf_iflist
; bp_temp 
&& (bp_temp
->bif_ifp 
!= ifp 
|| 
1760                  bp_temp
->bif_dlt 
!= dlt
); bp_temp 
= bp_temp
->bif_next
) { 
1761                  if (bp_temp
->bif_ifp 
== ifp 
&& bp_first 
== NULL
) 
1765         if (bp_temp 
!= NULL
) { 
1766                 printf("bpfattach - %s%d with dlt %d is already attached\n", 
1767                         ifp
->if_name
, ifp
->if_unit
, dlt
); 
1768                 FREE(bp_new
, M_DEVBUF
); 
1769                 lck_mtx_unlock(bpf_mlock
); 
1773         bzero(bp_new
, sizeof(*bp_new
)); 
1774         bp_new
->bif_ifp 
= ifp
; 
1775         bp_new
->bif_dlt 
= dlt
; 
1776         bp_new
->bif_send 
= send
; 
1777         bp_new
->bif_tap 
= tap
; 
1779         if (bp_first 
== NULL
) { 
1780                 /* No other entries for this ifp */ 
1781                 bp_new
->bif_next 
= bpf_iflist
; 
1782                 bpf_iflist 
= bp_new
; 
1785                 /* Add this after the first entry for this interface */ 
1786                 bp_new
->bif_next 
= bp_first
->bif_next
; 
1787                 bp_first
->bif_next 
= bp_new
; 
1791          * Compute the length of the bpf header.  This is not necessarily 
1792          * equal to SIZEOF_BPF_HDR because we want to insert spacing such 
1793          * that the network layer header begins on a longword boundary (for 
1794          * performance reasons and to alleviate alignment restrictions). 
1796         bp_new
->bif_hdrlen 
= BPF_WORDALIGN(hdrlen 
+ SIZEOF_BPF_HDR
) - hdrlen
; 
1798         /* Take a reference on the interface */ 
1799         ifnet_reference(ifp
); 
1801         lck_mtx_unlock(bpf_mlock
); 
1805                 printf("bpf: %s%d attached\n", ifp
->if_name
, ifp
->if_unit
); 
1812  * Detach bpf from an interface.  This involves detaching each descriptor 
1813  * associated with the interface, and leaving bd_bif NULL.  Notify each 
1814  * descriptor as it's detached so that any sleepers wake up and get 
1818 bpfdetach(struct ifnet 
*ifp
) 
1820         struct bpf_if   
*bp
, *bp_prev
, *bp_next
; 
1821         struct bpf_if   
*bp_free 
= NULL
; 
1825         lck_mtx_lock(bpf_mlock
); 
1827         /* Locate BPF interface information */ 
1829         for (bp 
= bpf_iflist
; bp 
!= NULL
; bp 
= bp_next
) { 
1830                 bp_next 
= bp
->bif_next
; 
1831                 if (ifp 
!= bp
->bif_ifp
) { 
1836                 while ((d 
= bp
->bif_dlist
) != NULL
) { 
1842                         bp_prev
->bif_next 
= bp
->bif_next
; 
1844                         bpf_iflist 
= bp
->bif_next
; 
1847                 bp
->bif_next 
= bp_free
; 
1853         lck_mtx_unlock(bpf_mlock
); 
1860 bpf_init(__unused 
void *unused
) 
1866         if (bpf_devsw_installed 
== 0) { 
1867                 bpf_devsw_installed 
= 1; 
1869         bpf_mlock_grp_attr 
= lck_grp_attr_alloc_init(); 
1871         bpf_mlock_grp 
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
); 
1873         bpf_mlock_attr 
= lck_attr_alloc_init(); 
1875         bpf_mlock 
= lck_mtx_alloc_init(bpf_mlock_grp
, bpf_mlock_attr
); 
1877                 if (bpf_mlock 
== 0) { 
1878                         printf("bpf_init: failed to allocate bpf_mlock\n"); 
1879                         bpf_devsw_installed 
= 0; 
1883                 maj 
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
); 
1886                                 lck_mtx_free(bpf_mlock
, bpf_mlock_grp
); 
1888                                 lck_attr_free(bpf_mlock_attr
); 
1890                                 lck_grp_free(bpf_mlock_grp
); 
1891                         if (bpf_mlock_grp_attr
) 
1892                                 lck_grp_attr_free(bpf_mlock_grp_attr
); 
1895                         bpf_mlock_attr 
= NULL
; 
1896                         bpf_mlock_grp 
= NULL
; 
1897                         bpf_mlock_grp_attr 
= NULL
; 
1898                         bpf_devsw_installed 
= 0; 
1899                         printf("bpf_init: failed to allocate a major number!\n"); 
1903                 for (i 
= 0 ; i 
< NBPFILTER
; i
++) 
1904                         bpf_make_dev_t(maj
); 
1907         cdevsw_add(&bpf_cdevsw
); 
1912 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
) 
1917 mac_bpfdesc_label_get(struct bpf_d 
*d
) 
1920         return (d
->bd_label
); 
1924 mac_bpfdesc_label_set(struct bpf_d 
*d
, struct label 
*label
) 
1927         d
->bd_label 
= label
;