FreeBSD/Linux Kernel Cross Reference
sys/net/bpf.c
1 /*
2 * Copyright (c) 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from the Stanford/CMU enet packet filter,
6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8 * Berkeley Laboratory.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)bpf.c 8.2 (Berkeley) 3/28/94
39 *
40 * $FreeBSD: src/sys/net/bpf.c,v 1.26.2.3 1999/09/05 08:17:25 peter Exp $
41 */
42
43 #include "bpfilter.h"
44
45 #if NBPFILTER > 0
46
47 #ifndef __GNUC__
48 #define inline
49 #else
50 #define inline __inline
51 #endif
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/conf.h>
56 #include <sys/mbuf.h>
57 #include <sys/buf.h>
58 #include <sys/time.h>
59 #include <sys/proc.h>
60 #include <sys/signalvar.h>
61 #include <sys/ioctl.h>
62
63 #include <sys/file.h>
64 #if defined(sparc) && BSD < 199103
65 #include <sys/stream.h>
66 #endif
67 #include <sys/uio.h>
68
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/protosw.h>
72 #include <net/if.h>
73
74 #include <net/bpf.h>
75 #include <net/bpfdesc.h>
76
77 #include <sys/errno.h>
78
79 #include <netinet/in.h>
80 #include <netinet/if_ether.h>
81 #include <sys/kernel.h>
82 #include <sys/sysctl.h>
83 #include <sys/conf.h>
84 #ifdef DEVFS
85 #include <sys/devfsext.h>
86 #endif /*DEVFS*/
87
88
89 /*
90 * Older BSDs don't have kernel malloc.
91 */
92 #if BSD < 199103
93 extern bcopy();
94 static caddr_t bpf_alloc();
95 #include <net/bpf_compat.h>
96 #define BPF_BUFSIZE (MCLBYTES-8)
97 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
98 #else
99 #define BPF_BUFSIZE 4096
100 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
101 #endif
102
103 #define PRINET 26 /* interruptible */
104
105 /*
106 * The default read buffer size is patchable.
107 */
108 static int bpf_bufsize = BPF_BUFSIZE;
109 SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
110 &bpf_bufsize, 0, "");
111
112 /*
113 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
114 * bpf_dtab holds the descriptors, indexed by minor device #
115 */
116 static struct bpf_if *bpf_iflist;
117 static struct bpf_d bpf_dtab[NBPFILTER];
118 static int bpf_dtab_init;
119
120 static int bpf_allocbufs __P((struct bpf_d *));
121 static void bpf_attachd __P((struct bpf_d *d, struct bpf_if *bp));
122 static void bpf_detachd __P((struct bpf_d *d));
123 static void bpf_freed __P((struct bpf_d *));
124 static void bpf_ifname __P((struct ifnet *, struct ifreq *));
125 static void bpf_mcopy __P((const void *, void *, u_int));
126 static int bpf_movein __P((struct uio *, int,
127 struct mbuf **, struct sockaddr *, int *));
128 static int bpf_setif __P((struct bpf_d *, struct ifreq *));
129 static inline void
130 bpf_wakeup __P((struct bpf_d *));
131 static void catchpacket __P((struct bpf_d *, u_char *, u_int,
132 u_int, void (*)(const void *, void *, u_int)));
133 static void reset_d __P((struct bpf_d *));
134 static int bpf_setf __P((struct bpf_d *, struct bpf_program *));
135
136 static d_open_t bpfopen;
137 static d_close_t bpfclose;
138 static d_read_t bpfread;
139 static d_write_t bpfwrite;
140 static d_ioctl_t bpfioctl;
141 static d_select_t bpfselect;
142
143 #define CDEV_MAJOR 23
144 static struct cdevsw bpf_cdevsw =
145 { bpfopen, bpfclose, bpfread, bpfwrite, /*23*/
146 bpfioctl, nostop, nullreset, nodevtotty,/* bpf */
147 bpfselect, nommap, NULL, "bpf", NULL, -1 };
148
149
150 static int
151 bpf_movein(uio, linktype, mp, sockp, datlen)
152 register struct uio *uio;
153 int linktype, *datlen;
154 register struct mbuf **mp;
155 register struct sockaddr *sockp;
156 {
157 struct mbuf *m;
158 int error;
159 int len;
160 int hlen;
161
162 /*
163 * Build a sockaddr based on the data link layer type.
164 * We do this at this level because the ethernet header
165 * is copied directly into the data field of the sockaddr.
166 * In the case of SLIP, there is no header and the packet
167 * is forwarded as is.
168 * Also, we are careful to leave room at the front of the mbuf
169 * for the link level header.
170 */
171 switch (linktype) {
172
173 case DLT_SLIP:
174 sockp->sa_family = AF_INET;
175 hlen = 0;
176 break;
177
178 case DLT_EN10MB:
179 sockp->sa_family = AF_UNSPEC;
180 /* XXX Would MAXLINKHDR be better? */
181 hlen = sizeof(struct ether_header);
182 break;
183
184 case DLT_FDDI:
185 #if defined(__FreeBSD__) || defined(__bsdi__)
186 sockp->sa_family = AF_IMPLINK;
187 hlen = 0;
188 #else
189 sockp->sa_family = AF_UNSPEC;
190 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
191 hlen = 24;
192 #endif
193 break;
194
195 case DLT_NULL:
196 sockp->sa_family = AF_UNSPEC;
197 hlen = 0;
198 break;
199
200 default:
201 return (EIO);
202 }
203
204 len = uio->uio_resid;
205 *datlen = len - hlen;
206 if ((unsigned)len > MCLBYTES)
207 return (EIO);
208
209 MGETHDR(m, M_WAIT, MT_DATA);
210 if (m == 0)
211 return (ENOBUFS);
212 if (len > MHLEN) {
213 #if BSD >= 199103
214 MCLGET(m, M_WAIT);
215 if ((m->m_flags & M_EXT) == 0) {
216 #else
217 MCLGET(m);
218 if (m->m_len != MCLBYTES) {
219 #endif
220 error = ENOBUFS;
221 goto bad;
222 }
223 }
224 m->m_pkthdr.len = m->m_len = len;
225 m->m_pkthdr.rcvif = NULL;
226 *mp = m;
227 /*
228 * Make room for link header.
229 */
230 if (hlen != 0) {
231 m->m_pkthdr.len -= hlen;
232 m->m_len -= hlen;
233 #if BSD >= 199103
234 m->m_data += hlen; /* XXX */
235 #else
236 m->m_off += hlen;
237 #endif
238 error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio);
239 if (error)
240 goto bad;
241 }
242 error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio);
243 if (!error)
244 return (0);
245 bad:
246 m_freem(m);
247 return (error);
248 }
249
250 /*
251 * Attach file to the bpf interface, i.e. make d listen on bp.
252 * Must be called at splimp.
253 */
254 static void
255 bpf_attachd(d, bp)
256 struct bpf_d *d;
257 struct bpf_if *bp;
258 {
259 /*
260 * Point d at bp, and add d to the interface's list of listeners.
261 * Finally, point the driver's bpf cookie at the interface so
262 * it will divert packets to bpf.
263 */
264 d->bd_bif = bp;
265 d->bd_next = bp->bif_dlist;
266 bp->bif_dlist = d;
267
268 bp->bif_ifp->if_bpf = bp;
269 }
270
271 /*
272 * Detach a file from its interface.
273 */
274 static void
275 bpf_detachd(d)
276 struct bpf_d *d;
277 {
278 struct bpf_d **p;
279 struct bpf_if *bp;
280
281 bp = d->bd_bif;
282 /*
283 * Check if this descriptor had requested promiscuous mode.
284 * If so, turn it off.
285 */
286 if (d->bd_promisc) {
287 d->bd_promisc = 0;
288 if (ifpromisc(bp->bif_ifp, 0))
289 /*
290 * Something is really wrong if we were able to put
291 * the driver into promiscuous mode, but can't
292 * take it out.
293 */
294 panic("bpf: ifpromisc failed");
295 }
296 /* Remove d from the interface's descriptor list. */
297 p = &bp->bif_dlist;
298 while (*p != d) {
299 p = &(*p)->bd_next;
300 if (*p == 0)
301 panic("bpf_detachd: descriptor not in list");
302 }
303 *p = (*p)->bd_next;
304 if (bp->bif_dlist == 0)
305 /*
306 * Let the driver know that there are no more listeners.
307 */
308 d->bd_bif->bif_ifp->if_bpf = 0;
309 d->bd_bif = 0;
310 }
311
312
313 /*
314 * Mark a descriptor free by making it point to itself.
315 * This is probably cheaper than marking with a constant since
316 * the address should be in a register anyway.
317 */
318 #define D_ISFREE(d) ((d) == (d)->bd_next)
319 #define D_MARKFREE(d) ((d)->bd_next = (d))
320 #define D_MARKUSED(d) ((d)->bd_next = 0)
321
322 /*
323 * Open ethernet device. Returns ENXIO for illegal minor device number,
324 * EBUSY if file is open by another process.
325 */
326 /* ARGSUSED */
327 static int
328 bpfopen(dev, flags, fmt, p)
329 dev_t dev;
330 int flags;
331 int fmt;
332 struct proc *p;
333 {
334 register struct bpf_d *d;
335
336 if (minor(dev) >= NBPFILTER)
337 return (ENXIO);
338 /*
339 * Each minor can be opened by only one process. If the requested
340 * minor is in use, return EBUSY.
341 */
342 d = &bpf_dtab[minor(dev)];
343 if (!D_ISFREE(d))
344 return (EBUSY);
345
346 /* Mark "free" and do most initialization. */
347 bzero((char *)d, sizeof(*d));
348 d->bd_bufsize = bpf_bufsize;
349 d->bd_sig = SIGIO;
350
351 return (0);
352 }
353
354 /*
355 * Close the descriptor by detaching it from its interface,
356 * deallocating its buffers, and marking it free.
357 */
358 /* ARGSUSED */
359 static int
360 bpfclose(dev, flags, fmt, p)
361 dev_t dev;
362 int flags;
363 int fmt;
364 struct proc *p;
365 {
366 register struct bpf_d *d = &bpf_dtab[minor(dev)];
367 register int s;
368
369 s = splimp();
370 if (d->bd_bif)
371 bpf_detachd(d);
372 splx(s);
373 bpf_freed(d);
374
375 return (0);
376 }
377
378 /*
379 * Support for SunOS, which does not have tsleep.
380 */
381 #if BSD < 199103
382 static
383 bpf_timeout(arg)
384 caddr_t arg;
385 {
386 struct bpf_d *d = (struct bpf_d *)arg;
387 d->bd_timedout = 1;
388 wakeup(arg);
389 }
390
391 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
392
393 int
394 bpf_sleep(d)
395 register struct bpf_d *d;
396 {
397 register int rto = d->bd_rtout;
398 register int st;
399
400 if (rto != 0) {
401 d->bd_timedout = 0;
402 timeout(bpf_timeout, (caddr_t)d, rto);
403 }
404 st = sleep((caddr_t)d, PRINET|PCATCH);
405 if (rto != 0) {
406 if (d->bd_timedout == 0)
407 untimeout(bpf_timeout, (caddr_t)d);
408 else if (st == 0)
409 return EWOULDBLOCK;
410 }
411 return (st != 0) ? EINTR : 0;
412 }
413 #else
414 #define BPF_SLEEP tsleep
415 #endif
416
417 /*
418 * Rotate the packet buffers in descriptor d. Move the store buffer
419 * into the hold slot, and the free buffer into the store slot.
420 * Zero the length of the new store buffer.
421 */
422 #define ROTATE_BUFFERS(d) \
423 (d)->bd_hbuf = (d)->bd_sbuf; \
424 (d)->bd_hlen = (d)->bd_slen; \
425 (d)->bd_sbuf = (d)->bd_fbuf; \
426 (d)->bd_slen = 0; \
427 (d)->bd_fbuf = 0;
428 /*
429 * bpfread - read next chunk of packets from buffers
430 */
431 static int
432 bpfread(dev, uio, ioflag)
433 dev_t dev;
434 register struct uio *uio;
435 int ioflag;
436 {
437 register struct bpf_d *d = &bpf_dtab[minor(dev)];
438 int error;
439 int s;
440
441 /*
442 * Restrict application to use a buffer the same size as
443 * as kernel buffers.
444 */
445 if (uio->uio_resid != d->bd_bufsize)
446 return (EINVAL);
447
448 s = splimp();
449 /*
450 * If the hold buffer is empty, then do a timed sleep, which
451 * ends when the timeout expires or when enough packets
452 * have arrived to fill the store buffer.
453 */
454 while (d->bd_hbuf == 0) {
455 if (d->bd_immediate && d->bd_slen != 0) {
456 /*
457 * A packet(s) either arrived since the previous
458 * read or arrived while we were asleep.
459 * Rotate the buffers and return what's here.
460 */
461 ROTATE_BUFFERS(d);
462 break;
463 }
464 if (d->bd_rtout != -1)
465 error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf",
466 d->bd_rtout);
467 else
468 error = EWOULDBLOCK; /* User requested non-blocking I/O */
469 if (error == EINTR || error == ERESTART) {
470 splx(s);
471 return (error);
472 }
473 if (error == EWOULDBLOCK) {
474 /*
475 * On a timeout, return what's in the buffer,
476 * which may be nothing. If there is something
477 * in the store buffer, we can rotate the buffers.
478 */
479 if (d->bd_hbuf)
480 /*
481 * We filled up the buffer in between
482 * getting the timeout and arriving
483 * here, so we don't need to rotate.
484 */
485 break;
486
487 if (d->bd_slen == 0) {
488 splx(s);
489 return (0);
490 }
491 ROTATE_BUFFERS(d);
492 break;
493 }
494 }
495 /*
496 * At this point, we know we have something in the hold slot.
497 */
498 splx(s);
499
500 /*
501 * Move data from hold buffer into user space.
502 * We know the entire buffer is transferred since
503 * we checked above that the read buffer is bpf_bufsize bytes.
504 */
505 error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
506
507 s = splimp();
508 d->bd_fbuf = d->bd_hbuf;
509 d->bd_hbuf = 0;
510 d->bd_hlen = 0;
511 splx(s);
512
513 return (error);
514 }
515
516
517 /*
518 * If there are processes sleeping on this descriptor, wake them up.
519 */
520 static inline void
521 bpf_wakeup(d)
522 register struct bpf_d *d;
523 {
524 struct proc *p;
525
526 wakeup((caddr_t)d);
527 if (d->bd_async && d->bd_sig)
528 if (d->bd_pgid > 0)
529 gsignal (d->bd_pgid, d->bd_sig);
530 else if (p = pfind (-d->bd_pgid))
531 psignal (p, d->bd_sig);
532
533 #if BSD >= 199103
534 selwakeup(&d->bd_sel);
535 /* XXX */
536 d->bd_sel.si_pid = 0;
537 #else
538 if (d->bd_selproc) {
539 selwakeup(d->bd_selproc, (int)d->bd_selcoll);
540 d->bd_selcoll = 0;
541 d->bd_selproc = 0;
542 }
543 #endif
544 }
545
546 static int
547 bpfwrite(dev, uio, ioflag)
548 dev_t dev;
549 struct uio *uio;
550 int ioflag;
551 {
552 register struct bpf_d *d = &bpf_dtab[minor(dev)];
553 struct ifnet *ifp;
554 struct mbuf *m;
555 int error, s;
556 static struct sockaddr dst;
557 int datlen;
558
559 if (d->bd_bif == 0)
560 return (ENXIO);
561
562 ifp = d->bd_bif->bif_ifp;
563
564 if (uio->uio_resid == 0)
565 return (0);
566
567 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
568 if (error)
569 return (error);
570
571 if (datlen > ifp->if_mtu)
572 return (EMSGSIZE);
573
574 s = splnet();
575 #if BSD >= 199103
576 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
577 #else
578 error = (*ifp->if_output)(ifp, m, &dst);
579 #endif
580 splx(s);
581 /*
582 * The driver frees the mbuf.
583 */
584 return (error);
585 }
586
587 /*
588 * Reset a descriptor by flushing its packet buffer and clearing the
589 * receive and drop counts. Should be called at splimp.
590 */
591 static void
592 reset_d(d)
593 struct bpf_d *d;
594 {
595 if (d->bd_hbuf) {
596 /* Free the hold buffer. */
597 d->bd_fbuf = d->bd_hbuf;
598 d->bd_hbuf = 0;
599 }
600 d->bd_slen = 0;
601 d->bd_hlen = 0;
602 d->bd_rcount = 0;
603 d->bd_dcount = 0;
604 }
605
606 /*
607 * FIONREAD Check for read packet available.
608 * SIOCGIFADDR Get interface address - convenient hook to driver.
609 * BIOCGBLEN Get buffer len [for read()].
610 * BIOCSETF Set ethernet read filter.
611 * BIOCFLUSH Flush read packet buffer.
612 * BIOCPROMISC Put interface into promiscuous mode.
613 * BIOCGDLT Get link layer type.
614 * BIOCGETIF Get interface name.
615 * BIOCSETIF Set interface.
616 * BIOCSRTIMEOUT Set read timeout.
617 * BIOCGRTIMEOUT Get read timeout.
618 * BIOCGSTATS Get packet stats.
619 * BIOCIMMEDIATE Set immediate mode.
620 * BIOCVERSION Get filter language version.
621 */
622 /* ARGSUSED */
623 static int
624 bpfioctl(dev, cmd, addr, flags, p)
625 dev_t dev;
626 int cmd;
627 caddr_t addr;
628 int flags;
629 struct proc *p;
630 {
631 register struct bpf_d *d = &bpf_dtab[minor(dev)];
632 int s, error = 0;
633
634 switch (cmd) {
635
636 default:
637 error = EINVAL;
638 break;
639
640 /*
641 * Check for read packet available.
642 */
643 case FIONREAD:
644 {
645 int n;
646
647 s = splimp();
648 n = d->bd_slen;
649 if (d->bd_hbuf)
650 n += d->bd_hlen;
651 splx(s);
652
653 *(int *)addr = n;
654 break;
655 }
656
657 case SIOCGIFADDR:
658 {
659 struct ifnet *ifp;
660
661 if (d->bd_bif == 0)
662 error = EINVAL;
663 else {
664 ifp = d->bd_bif->bif_ifp;
665 error = (*ifp->if_ioctl)(ifp, cmd, addr);
666 }
667 break;
668 }
669
670 /*
671 * Get buffer len [for read()].
672 */
673 case BIOCGBLEN:
674 *(u_int *)addr = d->bd_bufsize;
675 break;
676
677 /*
678 * Set buffer length.
679 */
680 case BIOCSBLEN:
681 #if BSD < 199103
682 error = EINVAL;
683 #else
684 if (d->bd_bif != 0)
685 error = EINVAL;
686 else {
687 register u_int size = *(u_int *)addr;
688
689 if (size > BPF_MAXBUFSIZE)
690 *(u_int *)addr = size = BPF_MAXBUFSIZE;
691 else if (size < BPF_MINBUFSIZE)
692 *(u_int *)addr = size = BPF_MINBUFSIZE;
693 d->bd_bufsize = size;
694 }
695 #endif
696 break;
697
698 /*
699 * Set link layer read filter.
700 */
701 case BIOCSETF:
702 error = bpf_setf(d, (struct bpf_program *)addr);
703 break;
704
705 /*
706 * Flush read packet buffer.
707 */
708 case BIOCFLUSH:
709 s = splimp();
710 reset_d(d);
711 splx(s);
712 break;
713
714 /*
715 * Put interface into promiscuous mode.
716 */
717 case BIOCPROMISC:
718 if (d->bd_bif == 0) {
719 /*
720 * No interface attached yet.
721 */
722 error = EINVAL;
723 break;
724 }
725 s = splimp();
726 if (d->bd_promisc == 0) {
727 error = ifpromisc(d->bd_bif->bif_ifp, 1);
728 if (error == 0)
729 d->bd_promisc = 1;
730 }
731 splx(s);
732 break;
733
734 /*
735 * Get device parameters.
736 */
737 case BIOCGDLT:
738 if (d->bd_bif == 0)
739 error = EINVAL;
740 else
741 *(u_int *)addr = d->bd_bif->bif_dlt;
742 break;
743
744 /*
745 * Set interface name.
746 */
747 case BIOCGETIF:
748 if (d->bd_bif == 0)
749 error = EINVAL;
750 else
751 bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
752 break;
753
754 /*
755 * Set interface.
756 */
757 case BIOCSETIF:
758 error = bpf_setif(d, (struct ifreq *)addr);
759 break;
760
761 /*
762 * Set read timeout.
763 */
764 case BIOCSRTIMEOUT:
765 {
766 struct timeval *tv = (struct timeval *)addr;
767 u_long msec;
768
769 /* Compute number of milliseconds. */
770 msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
771 /* Scale milliseconds to ticks. Assume hard
772 clock has millisecond or greater resolution
773 (i.e. tick >= 1000). For 10ms hardclock,
774 tick/1000 = 10, so rtout<-msec/10. */
775 d->bd_rtout = msec / (tick / 1000);
776 break;
777 }
778
779 /*
780 * Get read timeout.
781 */
782 case BIOCGRTIMEOUT:
783 {
784 struct timeval *tv = (struct timeval *)addr;
785 u_long msec = d->bd_rtout;
786
787 msec *= tick / 1000;
788 tv->tv_sec = msec / 1000;
789 tv->tv_usec = msec % 1000;
790 break;
791 }
792
793 /*
794 * Get packet stats.
795 */
796 case BIOCGSTATS:
797 {
798 struct bpf_stat *bs = (struct bpf_stat *)addr;
799
800 bs->bs_recv = d->bd_rcount;
801 bs->bs_drop = d->bd_dcount;
802 break;
803 }
804
805 /*
806 * Set immediate mode.
807 */
808 case BIOCIMMEDIATE:
809 d->bd_immediate = *(u_int *)addr;
810 break;
811
812 case BIOCVERSION:
813 {
814 struct bpf_version *bv = (struct bpf_version *)addr;
815
816 bv->bv_major = BPF_MAJOR_VERSION;
817 bv->bv_minor = BPF_MINOR_VERSION;
818 break;
819 }
820
821
822 case FIONBIO: /* Non-blocking I/O */
823 if (*(int *)addr)
824 d->bd_rtout = -1;
825 else
826 d->bd_rtout = 0;
827 break;
828
829 case FIOASYNC: /* Send signal on receive packets */
830 d->bd_async = *(int *)addr;
831 break;
832
833 /* N.B. ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing the
834 equivalent of a TIOCSPGRP and hence end up here. *However* TIOCSPGRP's arg
835 is a process group if it's positive and a process id if it's negative. This
836 is exactly the opposite of what the other two functions want! Therefore
837 there is code in ioctl and fcntl to negate the arg before calling here. */
838
839 case TIOCSPGRP: /* Process or group to send signals to */
840 d->bd_pgid = *(int *)addr;
841 break;
842
843 case TIOCGPGRP:
844 *(int *)addr = d->bd_pgid;
845 break;
846
847 case BIOCSRSIG: /* Set receive signal */
848 {
849 u_int sig;
850
851 sig = *(u_int *)addr;
852
853 if (sig >= NSIG)
854 error = EINVAL;
855 else
856 d->bd_sig = sig;
857 break;
858 }
859 case BIOCGRSIG:
860 *(u_int *)addr = d->bd_sig;
861 break;
862 }
863 return (error);
864 }
865
866 /*
867 * Set d's packet filter program to fp. If this file already has a filter,
868 * free it and replace it. Returns EINVAL for bogus requests.
869 */
870 static int
871 bpf_setf(d, fp)
872 struct bpf_d *d;
873 struct bpf_program *fp;
874 {
875 struct bpf_insn *fcode, *old;
876 u_int flen, size;
877 int s;
878
879 old = d->bd_filter;
880 if (fp->bf_insns == 0) {
881 if (fp->bf_len != 0)
882 return (EINVAL);
883 s = splimp();
884 d->bd_filter = 0;
885 reset_d(d);
886 splx(s);
887 if (old != 0)
888 free((caddr_t)old, M_DEVBUF);
889 return (0);
890 }
891 flen = fp->bf_len;
892 if (flen > BPF_MAXINSNS)
893 return (EINVAL);
894
895 size = flen * sizeof(*fp->bf_insns);
896 fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
897 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
898 bpf_validate(fcode, (int)flen)) {
899 s = splimp();
900 d->bd_filter = fcode;
901 reset_d(d);
902 splx(s);
903 if (old != 0)
904 free((caddr_t)old, M_DEVBUF);
905
906 return (0);
907 }
908 free((caddr_t)fcode, M_DEVBUF);
909 return (EINVAL);
910 }
911
912 /*
913 * Detach a file from its current interface (if attached at all) and attach
914 * to the interface indicated by the name stored in ifr.
915 * Return an errno or 0.
916 */
917 static int
918 bpf_setif(d, ifr)
919 struct bpf_d *d;
920 struct ifreq *ifr;
921 {
922 struct bpf_if *bp;
923 int s, error;
924 struct ifnet *theywant;
925
926 theywant = ifunit(ifr->ifr_name);
927 if (theywant == 0)
928 return ENXIO;
929
930 /*
931 * Look through attached interfaces for the named one.
932 */
933 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
934 struct ifnet *ifp = bp->bif_ifp;
935
936 if (ifp == 0 || ifp != theywant)
937 continue;
938 /*
939 * We found the requested interface.
940 * If it's not up, return an error.
941 * Allocate the packet buffers if we need to.
942 * If we're already attached to requested interface,
943 * just flush the buffer.
944 */
945 if ((ifp->if_flags & IFF_UP) == 0)
946 return (ENETDOWN);
947
948 if (d->bd_sbuf == 0) {
949 error = bpf_allocbufs(d);
950 if (error != 0)
951 return (error);
952 }
953 s = splimp();
954 if (bp != d->bd_bif) {
955 if (d->bd_bif)
956 /*
957 * Detach if attached to something else.
958 */
959 bpf_detachd(d);
960
961 bpf_attachd(d, bp);
962 }
963 reset_d(d);
964 splx(s);
965 return (0);
966 }
967 /* Not found. */
968 return (ENXIO);
969 }
970
971 /*
972 * Convert an interface name plus unit number of an ifp to a single
973 * name which is returned in the ifr.
974 */
975 static void
976 bpf_ifname(ifp, ifr)
977 struct ifnet *ifp;
978 struct ifreq *ifr;
979 {
980 char *s = ifp->if_name;
981 char *d = ifr->ifr_name;
982
983 while (*d++ = *s++)
984 continue;
985 d--; /* back to the null */
986 /* XXX Assume that unit number is less than 10. */
987 *d++ = ifp->if_unit + '';
988 *d = '\0';
989 }
990
991 /*
992 * The new select interface passes down the proc pointer; the old select
993 * stubs had to grab it out of the user struct. This glue allows either case.
994 */
995 #if BSD >= 199103
996 #define bpf_select bpfselect
997 #else
998 static int
999 bpfselect(dev, rw)
1000 register dev_t dev;
1001 int rw;
1002 {
1003 return (bpf_select(dev, rw, u.u_procp));
1004 }
1005 #endif
1006
1007 /*
1008 * Support for select() system call
1009 *
1010 * Return true iff the specific operation will not block indefinitely.
1011 * Otherwise, return false but make a note that a selwakeup() must be done.
1012 */
1013 int
1014 bpf_select(dev, rw, p)
1015 register dev_t dev;
1016 int rw;
1017 struct proc *p;
1018 {
1019 register struct bpf_d *d;
1020 register int s;
1021
1022 if (rw != FREAD)
1023 return (0);
1024 /*
1025 * An imitation of the FIONREAD ioctl code.
1026 */
1027 d = &bpf_dtab[minor(dev)];
1028
1029 s = splimp();
1030 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0)) {
1031 /*
1032 * There is data waiting.
1033 */
1034 splx(s);
1035 return (1);
1036 }
1037 #if BSD >= 199103
1038 selrecord(p, &d->bd_sel);
1039 #else
1040 /*
1041 * No data ready. If there's already a select() waiting on this
1042 * minor device then this is a collision. This shouldn't happen
1043 * because minors really should not be shared, but if a process
1044 * forks while one of these is open, it is possible that both
1045 * processes could select on the same descriptor.
1046 */
1047 if (d->bd_selproc && d->bd_selproc->p_wchan == (caddr_t)&selwait)
1048 d->bd_selcoll = 1;
1049 else
1050 d->bd_selproc = p;
1051 #endif
1052 splx(s);
1053 return (0);
1054 }
1055
1056 /*
1057 * Incoming linkage from device drivers. Process the packet pkt, of length
1058 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1059 * by each process' filter, and if accepted, stashed into the corresponding
1060 * buffer.
1061 */
1062 void
1063 bpf_tap(ifp, pkt, pktlen)
1064 struct ifnet *ifp;
1065 register u_char *pkt;
1066 register u_int pktlen;
1067 {
1068 struct bpf_if *bp;
1069 register struct bpf_d *d;
1070 register u_int slen;
1071 /*
1072 * Note that the ipl does not have to be raised at this point.
1073 * The only problem that could arise here is that if two different
1074 * interfaces shared any data. This is not the case.
1075 */
1076 bp = ifp->if_bpf;
1077 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1078 ++d->bd_rcount;
1079 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1080 if (slen != 0)
1081 catchpacket(d, pkt, pktlen, slen, bcopy);
1082 }
1083 }
1084
1085 /*
1086 * Copy data from an mbuf chain into a buffer. This code is derived
1087 * from m_copydata in sys/uipc_mbuf.c.
1088 */
1089 static void
1090 bpf_mcopy(src_arg, dst_arg, len)
1091 const void *src_arg;
1092 void *dst_arg;
1093 register u_int len;
1094 {
1095 register const struct mbuf *m;
1096 register u_int count;
1097 u_char *dst;
1098
1099 m = src_arg;
1100 dst = dst_arg;
1101 while (len > 0) {
1102 if (m == 0)
1103 panic("bpf_mcopy");
1104 count = min(m->m_len, len);
1105 bcopy(mtod(m, void *), dst, count);
1106 m = m->m_next;
1107 dst += count;
1108 len -= count;
1109 }
1110 }
1111
1112 /*
1113 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1114 */
1115 void
1116 bpf_mtap(ifp, m)
1117 struct ifnet *ifp;
1118 struct mbuf *m;
1119 {
1120 struct bpf_if *bp = ifp->if_bpf;
1121 struct bpf_d *d;
1122 u_int pktlen, slen;
1123 struct mbuf *m0;
1124
1125 pktlen = 0;
1126 for (m0 = m; m0 != 0; m0 = m0->m_next)
1127 pktlen += m0->m_len;
1128
1129 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1130 ++d->bd_rcount;
1131 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1132 if (slen != 0)
1133 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1134 }
1135 }
1136
1137 /*
1138 * Move the packet data from interface memory (pkt) into the
1139 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1140 * otherwise 0. "copy" is the routine called to do the actual data
1141 * transfer. bcopy is passed in to copy contiguous chunks, while
1142 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1143 * pkt is really an mbuf.
1144 */
1145 static void
1146 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1147 register struct bpf_d *d;
1148 register u_char *pkt;
1149 register u_int pktlen, snaplen;
1150 register void (*cpfn)(const void *, void *, u_int);
1151 {
1152 register struct bpf_hdr *hp;
1153 register int totlen, curlen;
1154 register int hdrlen = d->bd_bif->bif_hdrlen;
1155 /*
1156 * Figure out how many bytes to move. If the packet is
1157 * greater or equal to the snapshot length, transfer that
1158 * much. Otherwise, transfer the whole packet (unless
1159 * we hit the buffer size limit).
1160 */
1161 totlen = hdrlen + min(snaplen, pktlen);
1162 if (totlen > d->bd_bufsize)
1163 totlen = d->bd_bufsize;
1164
1165 /*
1166 * Round up the end of the previous packet to the next longword.
1167 */
1168 curlen = BPF_WORDALIGN(d->bd_slen);
1169 if (curlen + totlen > d->bd_bufsize) {
1170 /*
1171 * This packet will overflow the storage buffer.
1172 * Rotate the buffers if we can, then wakeup any
1173 * pending reads.
1174 */
1175 if (d->bd_fbuf == 0) {
1176 /*
1177 * We haven't completed the previous read yet,
1178 * so drop the packet.
1179 */
1180 ++d->bd_dcount;
1181 return;
1182 }
1183 ROTATE_BUFFERS(d);
1184 bpf_wakeup(d);
1185 curlen = 0;
1186 }
1187 else if (d->bd_immediate)
1188 /*
1189 * Immediate mode is set. A packet arrived so any
1190 * reads should be woken up.
1191 */
1192 bpf_wakeup(d);
1193
1194 /*
1195 * Append the bpf header.
1196 */
1197 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1198 #if BSD >= 199103
1199 microtime(&hp->bh_tstamp);
1200 #elif defined(sun)
1201 uniqtime(&hp->bh_tstamp);
1202 #else
1203 hp->bh_tstamp = time;
1204 #endif
1205 hp->bh_datalen = pktlen;
1206 hp->bh_hdrlen = hdrlen;
1207 /*
1208 * Copy the packet data into the store buffer and update its length.
1209 */
1210 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1211 d->bd_slen = curlen + totlen;
1212 }
1213
1214 /*
1215 * Initialize all nonzero fields of a descriptor.
1216 */
1217 static int
1218 bpf_allocbufs(d)
1219 register struct bpf_d *d;
1220 {
1221 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1222 if (d->bd_fbuf == 0)
1223 return (ENOBUFS);
1224
1225 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1226 if (d->bd_sbuf == 0) {
1227 free(d->bd_fbuf, M_DEVBUF);
1228 return (ENOBUFS);
1229 }
1230 d->bd_slen = 0;
1231 d->bd_hlen = 0;
1232 return (0);
1233 }
1234
1235 /*
1236 * Free buffers currently in use by a descriptor.
1237 * Called on close.
1238 */
1239 static void
1240 bpf_freed(d)
1241 register struct bpf_d *d;
1242 {
1243 /*
1244 * We don't need to lock out interrupts since this descriptor has
1245 * been detached from its interface and it yet hasn't been marked
1246 * free.
1247 */
1248 if (d->bd_sbuf != 0) {
1249 free(d->bd_sbuf, M_DEVBUF);
1250 if (d->bd_hbuf != 0)
1251 free(d->bd_hbuf, M_DEVBUF);
1252 if (d->bd_fbuf != 0)
1253 free(d->bd_fbuf, M_DEVBUF);
1254 }
1255 if (d->bd_filter)
1256 free((caddr_t)d->bd_filter, M_DEVBUF);
1257
1258 D_MARKFREE(d);
1259 }
1260
1261 /*
1262 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1263 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1264 * size of the link header (variable length headers not yet supported).
1265 */
1266 void
1267 bpfattach(ifp, dlt, hdrlen)
1268 struct ifnet *ifp;
1269 u_int dlt, hdrlen;
1270 {
1271 struct bpf_if *bp;
1272 int i;
1273 bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
1274 if (bp == 0)
1275 panic("bpfattach");
1276
1277 bp->bif_dlist = 0;
1278 bp->bif_ifp = ifp;
1279 bp->bif_dlt = dlt;
1280
1281 bp->bif_next = bpf_iflist;
1282 bpf_iflist = bp;
1283
1284 bp->bif_ifp->if_bpf = 0;
1285
1286 /*
1287 * Compute the length of the bpf header. This is not necessarily
1288 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1289 * that the network layer header begins on a longword boundary (for
1290 * performance reasons and to alleviate alignment restrictions).
1291 */
1292 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1293
1294 /*
1295 * Mark all the descriptors free if this hasn't been done.
1296 */
1297 if (!bpf_dtab_init) {
1298 for (i = 0; i < NBPFILTER; ++i)
1299 D_MARKFREE(&bpf_dtab[i]);
1300 bpf_dtab_init = 1;
1301 }
1302
1303 if (bootverbose)
1304 printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1305 }
1306
1307 #ifdef DEVFS
1308 static void *bpf_devfs_token[NBPFILTER];
1309 #endif
1310
1311 static bpf_devsw_installed = 0;
1312
1313 static void bpf_drvinit(void *unused)
1314 {
1315 dev_t dev;
1316 #ifdef DEVFS
1317 int i;
1318 #endif
1319
1320 if( ! bpf_devsw_installed ) {
1321 dev = makedev(CDEV_MAJOR, 0);
1322 cdevsw_add(&dev,&bpf_cdevsw, NULL);
1323 bpf_devsw_installed = 1;
1324 #ifdef DEVFS
1325
1326 for ( i = 0 ; i < NBPFILTER ; i++ ) {
1327 bpf_devfs_token[i] =
1328 devfs_add_devswf(&bpf_cdevsw, i, DV_CHR, 0, 0,
1329 0600, "bpf%d", i);
1330 }
1331 #endif
1332 }
1333 }
1334
1335 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
1336
1337 #endif
Cache object: 0604181ac5fd62eff9b1d55587442375
|