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