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