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