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