]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/bpf.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / net / bpf.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
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@
1c79356b
A
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 *
9bccf70c 69 * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.5 2001/01/05 04:49:09 jdp Exp $
1c79356b
A
70 */
71
9bccf70c 72#include "bpf.h"
1c79356b
A
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>
1c79356b
A
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>
91447636 92#include <sys/uio_internal.h>
1c79356b 93
9bccf70c
A
94#if defined(sparc) && BSD < 199103
95#include <sys/stream.h>
96#endif
97#include <sys/poll.h>
98
1c79356b
A
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>
55e303ae 110#include <net/firewire.h>
1c79356b 111
91447636 112#include <machine/spl.h>
1c79356b
A
113#include <miscfs/devfs/devfs.h>
114#include <net/dlil.h>
115
91447636
A
116#include <kern/locks.h>
117
118extern int tvtohz(struct timeval *);
119
9bccf70c
A
120#if NBPFILTER > 0
121
1c79356b
A
122/*
123 * Older BSDs don't have kernel malloc.
124 */
125#if BSD < 199103
126extern bcopy();
127static caddr_t bpf_alloc();
9bccf70c 128#include <net/bpf_compat.h>
1c79356b
A
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
55e303ae 136
1c79356b
A
137#define PRINET 26 /* interruptible */
138
139/*
140 * The default read buffer size is patchable.
141 */
91447636 142static unsigned int bpf_bufsize = BPF_BUFSIZE;
1c79356b
A
143SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
144 &bpf_bufsize, 0, "");
91447636 145static unsigned int bpf_maxbufsize = BPF_MAXBUFSIZE;
9bccf70c
A
146SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
147 &bpf_maxbufsize, 0, "");
91447636
A
148static unsigned int bpf_maxdevices = 256;
149SYSCTL_UINT(_debug, OID_AUTO, bpf_maxdevices, CTLFLAG_RW,
150 &bpf_maxdevices, 0, "");
1c79356b
A
151
152/*
153 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
55e303ae 154 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
1c79356b
A
155 */
156static struct bpf_if *bpf_iflist;
9bccf70c
A
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 #.
91447636
A
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
9bccf70c 167 */
55e303ae 168static struct bpf_d **bpf_dtab = NULL;
91447636
A
169static unsigned int bpf_dtab_size = 0;
170static unsigned int nbpfilter = 0;
171
172static lck_mtx_t *bpf_mlock;
173static lck_grp_t *bpf_mlock_grp;
174static lck_grp_attr_t *bpf_mlock_grp_attr;
175static lck_attr_t *bpf_mlock_attr;
55e303ae
A
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 */
55e303ae 182#endif /* __APPLE__ */
1c79356b 183
91447636
A
184static int bpf_allocbufs(struct bpf_d *);
185static void bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
186static void bpf_detachd(struct bpf_d *d);
187static void bpf_freed(struct bpf_d *);
188static void bpf_mcopy(const void *, void *, size_t);
189static int bpf_movein(struct uio *, int,
190 struct mbuf **, struct sockaddr *, int *);
191static int bpf_setif(struct bpf_d *, struct ifreq *);
192static void bpf_wakeup(struct bpf_d *);
193static void catchpacket(struct bpf_d *, u_char *, u_int,
194 u_int, void (*)(const void *, void *, size_t));
195static void reset_d(struct bpf_d *);
196static int bpf_setf(struct bpf_d *, struct user_bpf_program *);
1c79356b 197
55e303ae
A
198/*static void *bpf_devfs_token[MAXBPFILTER];*/
199
200static int bpf_devsw_installed;
201
91447636
A
202void bpf_init(void *unused);
203int bpf_tap_callback(struct ifnet *ifp, struct mbuf *m);
55e303ae 204
9bccf70c
A
205/*
206 * Darwin differs from BSD here, the following are static
207 * on BSD and not static on Darwin.
208 */
1c79356b
A
209 d_open_t bpfopen;
210 d_close_t bpfclose;
211 d_read_t bpfread;
212 d_write_t bpfwrite;
91447636 213 ioctl_fcn_t bpfioctl;
9bccf70c 214 select_fcn_t bpfpoll;
1c79356b 215
1c79356b 216
9bccf70c
A
217/* Darwin's cdevsw struct differs slightly from BSDs */
218#define CDEV_MAJOR 23
1c79356b 219static struct cdevsw bpf_cdevsw = {
9bccf70c
A
220 /* open */ bpfopen,
221 /* close */ bpfclose,
222 /* read */ bpfread,
223 /* write */ bpfwrite,
224 /* ioctl */ bpfioctl,
91447636
A
225 /* stop */ eno_stop,
226 /* reset */ eno_reset,
227 /* tty */ NULL,
9bccf70c 228 /* select */ bpfpoll,
91447636 229 /* mmap */ eno_mmap,
9bccf70c 230 /* strategy*/ eno_strat,
91447636
A
231 /* getc */ eno_getc,
232 /* putc */ eno_putc,
233 /* type */ 0
1c79356b
A
234};
235
55e303ae 236#define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
9bccf70c 237
1c79356b 238static int
91447636 239bpf_movein(struct uio *uio, int linktype, struct mbuf **mp, struct sockaddr *sockp, int *datlen)
1c79356b
A
240{
241 struct mbuf *m;
242 int error;
243 int len;
244 int hlen;
245
91447636 246 if (sockp) {
1c79356b 247 /*
91447636
A
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.
1c79356b 255 */
91447636
A
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 }
1c79356b 313 }
91447636
A
314 else {
315 hlen = 0;
55e303ae 316 }
91447636
A
317
318 // LP64todo - fix this!
319 len = uio_resid(uio);
1c79356b
A
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);
91447636 327 if ((unsigned)len > MHLEN) {
1c79356b
A
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
9bccf70c
A
365#ifdef __APPLE__
366/* Callback registered with Ethernet driver. */
1c79356b
A
367int bpf_tap_callback(struct ifnet *ifp, struct mbuf *m)
368{
1c79356b
A
369 /*
370 * Do nothing if the BPF tap has been turned off.
371 * This is to protect from a potential race where this
91447636 372 * call blocks on the lock. And in the meantime
1c79356b
A
373 * BPF is turned off, which will clear if_bpf.
374 */
375 if (ifp->if_bpf)
376 bpf_mtap(ifp, m);
1c79356b
A
377 return 0;
378}
55e303ae
A
379
380/*
91447636
A
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
55e303ae 383 */
91447636
A
384static void
385bpf_make_dev_t(int maj)
55e303ae 386{
91447636
A
387 static int bpf_growing = 0;
388 unsigned int cur_size = nbpfilter, i;
55e303ae 389
91447636
A
390 if (nbpfilter >= bpf_maxdevices)
391 return;
55e303ae 392
91447636
A
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;
55e303ae 402
91447636
A
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);
55e303ae 426 }
91447636
A
427 i = nbpfilter++;
428 (void) devfs_make_node(makedev(maj, i),
429 DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0600,
430 "bpf%d", i);
431done:
432 bpf_growing = 0;
433 wakeup((caddr_t)&bpf_growing);
55e303ae
A
434}
435
9bccf70c 436#endif
1c79356b
A
437
438/*
439 * Attach file to the bpf interface, i.e. make d listen on bp.
440 * Must be called at splimp.
441 */
442static void
91447636 443bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
1c79356b 444{
1c79356b
A
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;
1c79356b 455
9bccf70c 456#ifdef __APPLE__
91447636 457 dlil_set_bpf_tap(bp->bif_ifp, BPF_TAP_INPUT_OUTPUT, bpf_tap_callback);
9bccf70c 458#endif
1c79356b
A
459}
460
461/*
462 * Detach a file from its interface.
463 */
464static void
91447636 465bpf_detachd(struct bpf_d *d)
1c79356b
A
466{
467 struct bpf_d **p;
468 struct bpf_if *bp;
9bccf70c 469#ifdef __APPLE__
1c79356b
A
470 struct ifnet *ifp;
471
472 ifp = d->bd_bif->bif_ifp;
9bccf70c
A
473
474#endif
1c79356b
A
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;
91447636 483 if (ifnet_set_promiscuous(bp->bif_ifp, 0))
1c79356b
A
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.
9bccf70c 488 * Most likely the network interface is gone.
1c79356b 489 */
91447636 490 printf("bpf: ifnet_set_promiscuous failed");
1c79356b
A
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;
9bccf70c 500 if (bp->bif_dlist == 0) {
1c79356b
A
501 /*
502 * Let the driver know that there are no more listeners.
503 */
9bccf70c
A
504 if (ifp->if_set_bpf_tap)
505 (*ifp->if_set_bpf_tap)(ifp, BPF_TAP_DISABLE, 0);
1c79356b 506 d->bd_bif->bif_ifp->if_bpf = 0;
9bccf70c 507 }
1c79356b
A
508 d->bd_bif = 0;
509}
510
511
1c79356b
A
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
91447636 518bpfopen(dev_t dev, __unused int flags, __unused int fmt, __unused struct proc *p)
1c79356b
A
519{
520 register struct bpf_d *d;
521
91447636 522 if ((unsigned int) minor(dev) >= nbpfilter)
1c79356b 523 return (ENXIO);
91447636
A
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));
9bccf70c 536
1c79356b 537 /*
9bccf70c 538 * Each minor can be opened by only one process. If the requested
1c79356b 539 * minor is in use, return EBUSY.
91447636
A
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.
1c79356b 544 */
91447636
A
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;
1c79356b 556 }
91447636 557 bzero(d, sizeof(struct bpf_d));
9bccf70c 558
91447636
A
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. */
1c79356b
A
565 d->bd_bufsize = bpf_bufsize;
566 d->bd_sig = SIGIO;
9bccf70c 567 d->bd_seesent = 1;
91447636 568 bpf_dtab[minor(dev)] = d; /* Mark opened */
55e303ae 569
1c79356b
A
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
91447636 579bpfclose(dev_t dev, __unused int flags, __unused int fmt, __unused struct proc *p)
1c79356b
A
580{
581 register struct bpf_d *d;
1c79356b 582
55e303ae 583 d = bpf_dtab[minor(dev)];
91447636
A
584 if (d == 0 || d == (void *)1)
585 return (ENXIO);
586
587 bpf_dtab[minor(dev)] = (void *)1; /* Mark closing */
55e303ae 588
91447636
A
589 /* Take BPF lock to ensure no other thread is using the device */
590 lck_mtx_lock(bpf_mlock);
55e303ae 591
1c79356b
A
592 if (d->bd_bif)
593 bpf_detachd(d);
0b4e3aa0 594 selthreadclear(&d->bd_sel);
1c79356b 595 bpf_freed(d);
91447636
A
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
1c79356b
A
603 return (0);
604}
605
1c79356b 606
91447636 607#define BPF_SLEEP bpf_sleep
1c79356b 608
91447636
A
609static int
610bpf_sleep(struct bpf_d *d, int pri, const char *wmesg, int timo)
1c79356b 611{
1c79356b
A
612 register int st;
613
91447636
A
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;
1c79356b 621}
1c79356b
A
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
91447636 638bpfread(dev_t dev, struct uio *uio, int ioflag)
1c79356b
A
639{
640 register struct bpf_d *d;
641 int error;
642 int s;
643
55e303ae 644 d = bpf_dtab[minor(dev)];
91447636
A
645 if (d == 0 || d == (void *)1)
646 return (ENXIO);
647
648 lck_mtx_lock(bpf_mlock);
55e303ae 649
1c79356b
A
650
651 /*
652 * Restrict application to use a buffer the same size as
653 * as kernel buffers.
654 */
91447636 655 // LP64todo - fix this
1c79356b 656 if (uio->uio_resid != d->bd_bufsize) {
91447636 657 lck_mtx_unlock(bpf_mlock);
1c79356b
A
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 }
9bccf70c
A
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);
91447636 686 lck_mtx_unlock(bpf_mlock);
9bccf70c
A
687 return (ENXIO);
688 }
689
1c79356b
A
690 if (ioflag & IO_NDELAY)
691 error = EWOULDBLOCK;
692 else
91447636 693 error = BPF_SLEEP(d, PRINET|PCATCH, "bpf",
1c79356b
A
694 d->bd_rtout);
695 if (error == EINTR || error == ERESTART) {
696 splx(s);
91447636 697 lck_mtx_unlock(bpf_mlock);
1c79356b
A
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);
91447636 716 lck_mtx_unlock(bpf_mlock);
1c79356b
A
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);
91447636 740 lck_mtx_unlock(bpf_mlock);
1c79356b
A
741 return (error);
742}
743
744
745/*
746 * If there are processes sleeping on this descriptor, wake them up.
747 */
91447636
A
748static void
749bpf_wakeup(struct bpf_d *d)
1c79356b
A
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
1c79356b 756 selwakeup(&d->bd_sel);
9bccf70c
A
757#ifndef __APPLE__
758 /* XXX */
759 d->bd_sel.si_pid = 0;
760#endif
1c79356b
A
761#else
762 if (d->bd_selproc) {
1c79356b 763 selwakeup(d->bd_selproc, (int)d->bd_selcoll);
1c79356b
A
764 d->bd_selcoll = 0;
765 d->bd_selproc = 0;
766 }
767#endif
768}
769
55e303ae
A
770/* keep in sync with bpf_movein above: */
771#define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
772
1c79356b 773 int
91447636 774bpfwrite(dev_t dev, struct uio *uio, __unused int ioflag)
1c79356b
A
775{
776 register struct bpf_d *d;
1c79356b
A
777 struct ifnet *ifp;
778 struct mbuf *m;
91447636 779 int error;
55e303ae 780 char dst_buf[SOCKADDR_HDR_LEN + MAX_DATALINK_HDR_LEN];
1c79356b
A
781 int datlen;
782
55e303ae 783 d = bpf_dtab[minor(dev)];
91447636
A
784 if (d == 0 || d == (void *)1)
785 return (ENXIO);
786
787 lck_mtx_lock(bpf_mlock);
9bccf70c 788
1c79356b 789 if (d->bd_bif == 0) {
91447636 790 lck_mtx_unlock(bpf_mlock);
1c79356b
A
791 return (ENXIO);
792 }
793
794 ifp = d->bd_bif->bif_ifp;
795
796 if (uio->uio_resid == 0) {
91447636 797 lck_mtx_unlock(bpf_mlock);
1c79356b
A
798 return (0);
799 }
55e303ae
A
800 ((struct sockaddr *)dst_buf)->sa_len = sizeof(dst_buf);
801 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m,
91447636 802 d->bd_hdrcmplt ? 0 : (struct sockaddr *)dst_buf, &datlen);
1c79356b 803 if (error) {
91447636 804 lck_mtx_unlock(bpf_mlock);
1c79356b
A
805 return (error);
806 }
807
91447636
A
808 if ((unsigned)datlen > ifp->if_mtu) {
809 lck_mtx_unlock(bpf_mlock);
1c79356b
A
810 return (EMSGSIZE);
811 }
812
91447636
A
813 lck_mtx_unlock(bpf_mlock);
814
55e303ae 815 if (d->bd_hdrcmplt) {
91447636 816 error = dlil_output(ifp, 0, m, NULL, NULL, 1);
55e303ae 817 }
91447636
A
818 else {
819 error = dlil_output(ifp, PF_INET, m, NULL, (struct sockaddr *)dst_buf, 0);
820 }
821
1c79356b
A
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 */
832static void
91447636 833reset_d(struct bpf_d *d)
1c79356b
A
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.
9bccf70c
A
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
1c79356b
A
865 */
866/* ARGSUSED */
9bccf70c 867int
91447636 868bpfioctl(dev_t dev, u_long cmd, caddr_t addr, __unused int flags, struct proc *p)
1c79356b
A
869{
870 register struct bpf_d *d;
871 int s, error = 0;
872
55e303ae 873 d = bpf_dtab[minor(dev)];
91447636
A
874 if (d == 0 || d == (void *)1)
875 return (ENXIO);
1c79356b 876
91447636 877 lck_mtx_lock(bpf_mlock);
1c79356b
A
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;
91447636 910 error = dlil_ioctl(0, ifp, cmd, addr);
1c79356b
A
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
9bccf70c
A
934 if (size > bpf_maxbufsize)
935 *(u_int *)addr = size = bpf_maxbufsize;
1c79356b
A
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:
91447636
A
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 }
1c79356b
A
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) {
91447636 983 error = ifnet_set_promiscuous(d->bd_bif->bif_ifp, 1);
1c79356b
A
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 /*
9bccf70c 1001 * Get interface name.
1c79356b
A
1002 */
1003 case BIOCGETIF:
1004 if (d->bd_bif == 0)
1005 error = EINVAL;
9bccf70c
A
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 }
1c79356b
A
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
9bccf70c
A
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
1c79356b
A
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;
9bccf70c 1112#ifndef __APPLE__
1c79356b
A
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 }
91447636
A
1147
1148 lck_mtx_unlock(bpf_mlock);
1149
1c79356b
A
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 */
1157static int
91447636 1158bpf_setf(struct bpf_d *d, struct user_bpf_program *fp)
1c79356b
A
1159{
1160 struct bpf_insn *fcode, *old;
1161 u_int flen, size;
1162 int s;
1163
1164 old = d->bd_filter;
91447636 1165 if (fp->bf_insns == USER_ADDR_NULL) {
1c79356b
A
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
91447636 1180 size = flen * sizeof(struct bpf_insn);
1c79356b 1181 fcode = (struct bpf_insn *) _MALLOC(size, M_DEVBUF, M_WAIT);
9bccf70c 1182#ifdef __APPLE__
0b4e3aa0
A
1183 if (fcode == NULL)
1184 return (ENOBUFS);
9bccf70c 1185#endif
91447636 1186 if (copyin(fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1c79356b
A
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 */
1206static int
91447636 1207bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1c79356b
A
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
1c79356b
A
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 */
1264int
91447636 1265bpfpoll(dev_t dev, int events, void * wql, struct proc *p)
1c79356b
A
1266{
1267 register struct bpf_d *d;
1268 register int s;
1269 int revents = 0;
1270
55e303ae 1271 d = bpf_dtab[minor(dev)];
91447636
A
1272 if (d == 0 || d == (void *)1)
1273 return (ENXIO);
1274
1275 lck_mtx_lock(bpf_mlock);
55e303ae 1276
1c79356b
A
1277 /*
1278 * An imitation of the FIONREAD ioctl code.
1279 */
9bccf70c 1280 if (d->bd_bif == NULL) {
91447636 1281 lck_mtx_unlock(bpf_mlock);
9bccf70c
A
1282 return (ENXIO);
1283 }
1284
1c79356b 1285 s = splimp();
9bccf70c 1286 if (events & (POLLIN | POLLRDNORM)) {
1c79356b
A
1287 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1288 revents |= events & (POLLIN | POLLRDNORM);
1289 else
0b4e3aa0 1290 selrecord(p, &d->bd_sel, wql);
9bccf70c 1291 }
1c79356b 1292 splx(s);
91447636
A
1293
1294 lck_mtx_unlock(bpf_mlock);
1c79356b
A
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 */
1304void
91447636 1305bpf_tap(struct ifnet *ifp, u_char *pkt, u_int pktlen)
1c79356b
A
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 */
91447636
A
1315 lck_mtx_lock(bpf_mlock);
1316
9bccf70c
A
1317 bp = ifp->if_bpf;
1318#ifdef __APPLE__
1319 if (bp) {
1320#endif
91447636
A
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 }
9bccf70c 1327#ifdef __APPLE__
1c79356b 1328 }
91447636 1329 lck_mtx_unlock(bpf_mlock);
9bccf70c 1330#endif
1c79356b
A
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 */
1337static void
91447636 1338bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1c79356b 1339{
91447636
A
1340 const struct mbuf *m;
1341 u_int count;
1c79356b
A
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);
91447636 1350 bcopy(mtod(m, const void *), dst, count);
1c79356b
A
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 */
1360void
91447636 1361bpf_mtap(struct ifnet *ifp, struct mbuf *m)
1c79356b 1362{
91447636 1363 struct bpf_if *bp;
1c79356b
A
1364 struct bpf_d *d;
1365 u_int pktlen, slen;
1366 struct mbuf *m0;
1367
91447636
A
1368 lck_mtx_lock(bpf_mlock);
1369
1370 bp = ifp->if_bpf;
1371 if (bp) {
1c79356b
A
1372 pktlen = 0;
1373 for (m0 = m; m0 != 0; m0 = m0->m_next)
1374 pktlen += m0->m_len;
91447636
A
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 }
1c79356b 1384 }
91447636
A
1385
1386 lck_mtx_unlock(bpf_mlock);
1c79356b
A
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 */
1397static void
91447636
A
1398catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1399 void (*cpfn)(const void *, void *, size_t))
1c79356b
A
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 */
1466static int
91447636 1467bpf_allocbufs(struct bpf_d *d)
1c79356b
A
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 */
1487static void
91447636 1488bpf_freed(struct bpf_d *d)
1c79356b
A
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);
1c79356b
A
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 */
1511void
91447636 1512bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1c79356b
A
1513{
1514 struct bpf_if *bp;
0b4e3aa0 1515 bp = (struct bpf_if *) _MALLOC(sizeof(*bp), M_DEVBUF, M_WAIT);
1c79356b
A
1516 if (bp == 0)
1517 panic("bpfattach");
1518
91447636
A
1519 lck_mtx_lock(bpf_mlock);
1520
1c79356b
A
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;
91447636
A
1537
1538 /* Take a reference on the interface */
1539 ifp_reference(ifp);
1540
1541 lck_mtx_unlock(bpf_mlock);
1c79356b 1542
55e303ae 1543#ifndef __APPLE__
1c79356b
A
1544 if (bootverbose)
1545 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1546#endif
1547}
1548
9bccf70c
A
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 */
1555void
91447636 1556bpfdetach(struct ifnet *ifp)
9bccf70c
A
1557{
1558 struct bpf_if *bp, *bp_prev;
1559 struct bpf_d *d;
1560 int s;
1561
1562 s = splimp();
91447636
A
1563
1564 lck_mtx_lock(bpf_mlock);
9bccf70c
A
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 }
91447636
A
1601
1602 ifp_release(ifp);
1603
1604 lck_mtx_unlock(bpf_mlock);
9bccf70c
A
1605
1606 FREE(bp, M_DEVBUF);
1607
1608 splx(s);
1609}
1610
1c79356b 1611void
91447636 1612bpf_init(__unused void *unused)
1c79356b 1613{
9bccf70c 1614#ifdef __APPLE__
1c79356b 1615 int i;
9bccf70c 1616 int maj;
1c79356b 1617
91447636 1618 if (bpf_devsw_installed == 0) {
9bccf70c 1619 bpf_devsw_installed = 1;
91447636
A
1620
1621 bpf_mlock_grp_attr = lck_grp_attr_alloc_init();
91447636
A
1622
1623 bpf_mlock_grp = lck_grp_alloc_init("bpf", bpf_mlock_grp_attr);
1624
1625 bpf_mlock_attr = lck_attr_alloc_init();
91447636
A
1626
1627 bpf_mlock = lck_mtx_alloc_init(bpf_mlock_grp, bpf_mlock_attr);
1628
1629 if (bpf_mlock == 0) {
1630 printf("bpf_init: failed to allocate bpf_mlock\n");
1631 bpf_devsw_installed = 0;
1632 return;
1633 }
1634
9bccf70c
A
1635 maj = cdevsw_add(CDEV_MAJOR, &bpf_cdevsw);
1636 if (maj == -1) {
91447636
A
1637 if (bpf_mlock)
1638 lck_mtx_free(bpf_mlock, bpf_mlock_grp);
1639 if (bpf_mlock_attr)
1640 lck_attr_free(bpf_mlock_attr);
1641 if (bpf_mlock_grp)
1642 lck_grp_free(bpf_mlock_grp);
1643 if (bpf_mlock_grp_attr)
1644 lck_grp_attr_free(bpf_mlock_grp_attr);
1645
1646 bpf_mlock = 0;
1647 bpf_mlock_attr = 0;
1648 bpf_mlock_grp = 0;
1649 bpf_mlock_grp_attr = 0;
1650 bpf_devsw_installed = 0;
9bccf70c 1651 printf("bpf_init: failed to allocate a major number!\n");
55e303ae 1652 return;
9bccf70c 1653 }
91447636 1654
55e303ae
A
1655 for (i = 0 ; i < NBPFILTER; i++)
1656 bpf_make_dev_t(maj);
9bccf70c
A
1657 }
1658#else
1659 cdevsw_add(&bpf_cdevsw);
1660#endif
1c79356b
A
1661}
1662
9bccf70c 1663#ifndef __APPLE__
1c79356b 1664SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1c79356b 1665#endif
9bccf70c
A
1666
1667#else /* !BPF */
1668#ifndef __APPLE__
1669/*
1670 * NOP stubs to allow bpf-using drivers to load and function.
1671 *
1672 * A 'better' implementation would allow the core bpf functionality
1673 * to be loaded at runtime.
1674 */
1675
1676void
1677bpf_tap(ifp, pkt, pktlen)
1678 struct ifnet *ifp;
1679 register u_char *pkt;
1680 register u_int pktlen;
1681{
1682}
1683
1684void
1685bpf_mtap(ifp, m)
1686 struct ifnet *ifp;
1687 struct mbuf *m;
1688{
1689}
1690
1691void
1692bpfattach(ifp, dlt, hdrlen)
1693 struct ifnet *ifp;
1694 u_int dlt, hdrlen;
1695{
1696}
1697
1698void
1699bpfdetach(ifp)
1700 struct ifnet *ifp;
1701{
1702}
1703
1704u_int
1705bpf_filter(pc, p, wirelen, buflen)
1706 register const struct bpf_insn *pc;
1707 register u_char *p;
1708 u_int wirelen;
1709 register u_int buflen;
1710{
1711 return -1; /* "no filter" behaviour */
1712}
1713#endif /* !defined(__APPLE__) */
1714#endif /* NBPFILTER > 0 */