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