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