bpf.c revision 1.62 1 /* $NetBSD: bpf.c,v 1.62 2001/09/10 23:11:06 bjh21 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 selwakeup(&d->bd_sel);
518 /* XXX */
519 d->bd_sel.si_pid = 0;
520 }
521
522 int
523 bpfwrite(dev, uio, ioflag)
524 dev_t dev;
525 struct uio *uio;
526 int ioflag;
527 {
528 struct bpf_d *d = &bpf_dtab[minor(dev)];
529 struct ifnet *ifp;
530 struct mbuf *m;
531 int error, s;
532 static struct sockaddr_storage dst;
533
534 if (d->bd_bif == 0)
535 return (ENXIO);
536
537 ifp = d->bd_bif->bif_ifp;
538
539 if (uio->uio_resid == 0)
540 return (0);
541
542 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m,
543 (struct sockaddr *) &dst);
544 if (error)
545 return (error);
546
547 if (m->m_pkthdr.len > ifp->if_mtu)
548 return (EMSGSIZE);
549
550 if (d->bd_hdrcmplt)
551 dst.ss_family = pseudo_AF_HDRCMPLT;
552
553 s = splsoftnet();
554 error = (*ifp->if_output)(ifp, m, (struct sockaddr *) &dst, NULL);
555 splx(s);
556 /*
557 * The driver frees the mbuf.
558 */
559 return (error);
560 }
561
562 /*
563 * Reset a descriptor by flushing its packet buffer and clearing the
564 * receive and drop counts. Should be called at splnet.
565 */
566 static void
567 reset_d(d)
568 struct bpf_d *d;
569 {
570 if (d->bd_hbuf) {
571 /* Free the hold buffer. */
572 d->bd_fbuf = d->bd_hbuf;
573 d->bd_hbuf = 0;
574 }
575 d->bd_slen = 0;
576 d->bd_hlen = 0;
577 d->bd_rcount = 0;
578 d->bd_dcount = 0;
579 }
580
581 #ifdef BPF_KERN_FILTER
582 extern struct bpf_insn *bpf_tcp_filter;
583 extern struct bpf_insn *bpf_udp_filter;
584 #endif
585
586 /*
587 * FIONREAD Check for read packet available.
588 * BIOCGBLEN Get buffer len [for read()].
589 * BIOCSETF Set ethernet read filter.
590 * BIOCFLUSH Flush read packet buffer.
591 * BIOCPROMISC Put interface into promiscuous mode.
592 * BIOCGDLT Get link layer type.
593 * BIOCGETIF Get interface name.
594 * BIOCSETIF Set interface.
595 * BIOCSRTIMEOUT Set read timeout.
596 * BIOCGRTIMEOUT Get read timeout.
597 * BIOCGSTATS Get packet stats.
598 * BIOCIMMEDIATE Set immediate mode.
599 * BIOCVERSION Get filter language version.
600 * BIOGHDRCMPLT Get "header already complete" flag.
601 * BIOSHDRCMPLT Set "header already complete" flag.
602 */
603 /* ARGSUSED */
604 int
605 bpfioctl(dev, cmd, addr, flag, p)
606 dev_t dev;
607 u_long cmd;
608 caddr_t addr;
609 int flag;
610 struct proc *p;
611 {
612 struct bpf_d *d = &bpf_dtab[minor(dev)];
613 int s, error = 0;
614 #ifdef BPF_KERN_FILTER
615 struct bpf_insn **p;
616 #endif
617
618 switch (cmd) {
619
620 default:
621 error = EINVAL;
622 break;
623
624 /*
625 * Check for read packet available.
626 */
627 case FIONREAD:
628 {
629 int n;
630
631 s = splnet();
632 n = d->bd_slen;
633 if (d->bd_hbuf)
634 n += d->bd_hlen;
635 splx(s);
636
637 *(int *)addr = n;
638 break;
639 }
640
641 /*
642 * Get buffer len [for read()].
643 */
644 case BIOCGBLEN:
645 *(u_int *)addr = d->bd_bufsize;
646 break;
647
648 /*
649 * Set buffer length.
650 */
651 case BIOCSBLEN:
652 if (d->bd_bif != 0)
653 error = EINVAL;
654 else {
655 u_int size = *(u_int *)addr;
656
657 if (size > BPF_MAXBUFSIZE)
658 *(u_int *)addr = size = BPF_MAXBUFSIZE;
659 else if (size < BPF_MINBUFSIZE)
660 *(u_int *)addr = size = BPF_MINBUFSIZE;
661 d->bd_bufsize = size;
662 }
663 break;
664
665 /*
666 * Set link layer read filter.
667 */
668 case BIOCSETF:
669 error = bpf_setf(d, (struct bpf_program *)addr);
670 break;
671
672 #ifdef BPF_KERN_FILTER
673 /*
674 * Set TCP or UDP reject filter.
675 */
676 case BIOCSTCPF:
677 case BIOCSUDPF:
678 if (!suser()) {
679 error = EPERM;
680 break;
681 }
682
683 /* Validate and store filter */
684 error = bpf_setf(d, (struct bpf_program *)addr);
685
686 /* Free possible old filter */
687 if (cmd == BIOCSTCPF)
688 p = &bpf_tcp_filter;
689 else
690 p = &bpf_udp_filter;
691 if (*p != NULL)
692 free((caddr_t)*p, M_DEVBUF);
693
694 /* Steal new filter (noop if error) */
695 s = splnet();
696 *p = d->bd_filter;
697 d->bd_filter = NULL;
698 splx(s);
699 break;
700 #endif
701
702 /*
703 * Flush read packet buffer.
704 */
705 case BIOCFLUSH:
706 s = splnet();
707 reset_d(d);
708 splx(s);
709 break;
710
711 /*
712 * Put interface into promiscuous mode.
713 */
714 case BIOCPROMISC:
715 if (d->bd_bif == 0) {
716 /*
717 * No interface attached yet.
718 */
719 error = EINVAL;
720 break;
721 }
722 s = splnet();
723 if (d->bd_promisc == 0) {
724 error = ifpromisc(d->bd_bif->bif_ifp, 1);
725 if (error == 0)
726 d->bd_promisc = 1;
727 }
728 splx(s);
729 break;
730
731 /*
732 * Get device parameters.
733 */
734 case BIOCGDLT:
735 if (d->bd_bif == 0)
736 error = EINVAL;
737 else
738 *(u_int *)addr = d->bd_bif->bif_dlt;
739 break;
740
741 /*
742 * Set interface name.
743 */
744 case BIOCGETIF:
745 if (d->bd_bif == 0)
746 error = EINVAL;
747 else
748 bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
749 break;
750
751 /*
752 * Set interface.
753 */
754 case BIOCSETIF:
755 error = bpf_setif(d, (struct ifreq *)addr);
756 break;
757
758 /*
759 * Set read timeout.
760 */
761 case BIOCSRTIMEOUT:
762 {
763 struct timeval *tv = (struct timeval *)addr;
764
765 /* Compute number of ticks. */
766 d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
767 if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
768 d->bd_rtout = 1;
769 break;
770 }
771
772 /*
773 * Get read timeout.
774 */
775 case BIOCGRTIMEOUT:
776 {
777 struct timeval *tv = (struct timeval *)addr;
778
779 tv->tv_sec = d->bd_rtout / hz;
780 tv->tv_usec = (d->bd_rtout % hz) * tick;
781 break;
782 }
783
784 /*
785 * Get packet stats.
786 */
787 case BIOCGSTATS:
788 {
789 struct bpf_stat *bs = (struct bpf_stat *)addr;
790
791 bs->bs_recv = d->bd_rcount;
792 bs->bs_drop = d->bd_dcount;
793 break;
794 }
795
796 /*
797 * Set immediate mode.
798 */
799 case BIOCIMMEDIATE:
800 d->bd_immediate = *(u_int *)addr;
801 break;
802
803 case BIOCVERSION:
804 {
805 struct bpf_version *bv = (struct bpf_version *)addr;
806
807 bv->bv_major = BPF_MAJOR_VERSION;
808 bv->bv_minor = BPF_MINOR_VERSION;
809 break;
810 }
811
812 case BIOCGHDRCMPLT: /* get "header already complete" flag */
813 *(u_int *)addr = d->bd_hdrcmplt;
814 break;
815
816 case BIOCSHDRCMPLT: /* set "header already complete" flag */
817 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
818 break;
819
820 case FIONBIO: /* Non-blocking I/O */
821 if (*(int *)addr)
822 d->bd_rtout = -1;
823 else
824 d->bd_rtout = 0;
825 break;
826
827 case FIOASYNC: /* Send signal on receive packets */
828 d->bd_async = *(int *)addr;
829 break;
830
831 /*
832 * N.B. ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing
833 * the equivalent of a TIOCSPGRP and hence end up here. *However*
834 * TIOCSPGRP's arg is a process group if it's positive and a process
835 * id if it's negative. This is exactly the opposite of what the
836 * other two functions want! Therefore there is code in ioctl and
837 * fcntl to negate the arg before calling here.
838 */
839 case TIOCSPGRP: /* Process or group to send signals to */
840 d->bd_pgid = *(int *)addr;
841 break;
842
843 case TIOCGPGRP:
844 *(int *)addr = d->bd_pgid;
845 break;
846 }
847 return (error);
848 }
849
850 /*
851 * Set d's packet filter program to fp. If this file already has a filter,
852 * free it and replace it. Returns EINVAL for bogus requests.
853 */
854 int
855 bpf_setf(d, fp)
856 struct bpf_d *d;
857 struct bpf_program *fp;
858 {
859 struct bpf_insn *fcode, *old;
860 u_int flen, size;
861 int s;
862
863 old = d->bd_filter;
864 if (fp->bf_insns == 0) {
865 if (fp->bf_len != 0)
866 return (EINVAL);
867 s = splnet();
868 d->bd_filter = 0;
869 reset_d(d);
870 splx(s);
871 if (old != 0)
872 free((caddr_t)old, M_DEVBUF);
873 return (0);
874 }
875 flen = fp->bf_len;
876 if (flen > BPF_MAXINSNS)
877 return (EINVAL);
878
879 size = flen * sizeof(*fp->bf_insns);
880 fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
881 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
882 bpf_validate(fcode, (int)flen)) {
883 s = splnet();
884 d->bd_filter = fcode;
885 reset_d(d);
886 splx(s);
887 if (old != 0)
888 free((caddr_t)old, M_DEVBUF);
889
890 return (0);
891 }
892 free((caddr_t)fcode, M_DEVBUF);
893 return (EINVAL);
894 }
895
896 /*
897 * Detach a file from its current interface (if attached at all) and attach
898 * to the interface indicated by the name stored in ifr.
899 * Return an errno or 0.
900 */
901 static int
902 bpf_setif(d, ifr)
903 struct bpf_d *d;
904 struct ifreq *ifr;
905 {
906 struct bpf_if *bp;
907 char *cp;
908 int unit_seen, i, s, error;
909
910 /*
911 * Make sure the provided name has a unit number, and default
912 * it to '0' if not specified.
913 * XXX This is ugly ... do this differently?
914 */
915 unit_seen = 0;
916 cp = ifr->ifr_name;
917 cp[sizeof(ifr->ifr_name) - 1] = '\0'; /* sanity */
918 while (*cp++)
919 if (*cp >= '0' && *cp <= '9')
920 unit_seen = 1;
921 if (!unit_seen) {
922 /* Make sure to leave room for the '\0'. */
923 for (i = 0; i < (IFNAMSIZ - 1); ++i) {
924 if ((ifr->ifr_name[i] >= 'a' &&
925 ifr->ifr_name[i] <= 'z') ||
926 (ifr->ifr_name[i] >= 'A' &&
927 ifr->ifr_name[i] <= 'Z'))
928 continue;
929 ifr->ifr_name[i] = '0';
930 }
931 }
932
933 /*
934 * Look through attached interfaces for the named one.
935 */
936 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
937 struct ifnet *ifp = bp->bif_ifp;
938
939 if (ifp == 0 ||
940 strcmp(ifp->if_xname, ifr->ifr_name) != 0)
941 continue;
942 /*
943 * We found the requested interface.
944 * If it's not up, return an error.
945 * Allocate the packet buffers if we need to.
946 * If we're already attached to requested interface,
947 * just flush the buffer.
948 */
949 if ((ifp->if_flags & IFF_UP) == 0)
950 return (ENETDOWN);
951
952 if (d->bd_sbuf == 0) {
953 error = bpf_allocbufs(d);
954 if (error != 0)
955 return (error);
956 }
957 s = splnet();
958 if (bp != d->bd_bif) {
959 if (d->bd_bif)
960 /*
961 * Detach if attached to something else.
962 */
963 bpf_detachd(d);
964
965 bpf_attachd(d, bp);
966 }
967 reset_d(d);
968 splx(s);
969 return (0);
970 }
971 /* Not found. */
972 return (ENXIO);
973 }
974
975 /*
976 * Copy the interface name to the ifreq.
977 */
978 static void
979 bpf_ifname(ifp, ifr)
980 struct ifnet *ifp;
981 struct ifreq *ifr;
982 {
983
984 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
985 }
986
987 /*
988 * Support for poll() system call
989 *
990 * Return true iff the specific operation will not block indefinitely.
991 * Otherwise, return false but make a note that a selwakeup() must be done.
992 */
993 int
994 bpfpoll(dev, events, p)
995 dev_t dev;
996 int events;
997 struct proc *p;
998 {
999 struct bpf_d *d = &bpf_dtab[minor(dev)];
1000 int revents = 0;
1001 int s = splnet();
1002
1003 /*
1004 * An imitation of the FIONREAD ioctl code.
1005 */
1006 if (events & (POLLIN | POLLRDNORM)) {
1007 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1008 revents |= events & (POLLIN | POLLRDNORM);
1009 else
1010 selrecord(p, &d->bd_sel);
1011 }
1012
1013 splx(s);
1014 return (revents);
1015 }
1016
1017 /*
1018 * Incoming linkage from device drivers. Process the packet pkt, of length
1019 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1020 * by each process' filter, and if accepted, stashed into the corresponding
1021 * buffer.
1022 */
1023 void
1024 bpf_tap(arg, pkt, pktlen)
1025 caddr_t arg;
1026 u_char *pkt;
1027 u_int pktlen;
1028 {
1029 struct bpf_if *bp;
1030 struct bpf_d *d;
1031 u_int slen;
1032 /*
1033 * Note that the ipl does not have to be raised at this point.
1034 * The only problem that could arise here is that if two different
1035 * interfaces shared any data. This is not the case.
1036 */
1037 bp = (struct bpf_if *)arg;
1038 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1039 ++d->bd_rcount;
1040 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1041 if (slen != 0)
1042 catchpacket(d, pkt, pktlen, slen, memcpy);
1043 }
1044 }
1045
1046 /*
1047 * Copy data from an mbuf chain into a buffer. This code is derived
1048 * from m_copydata in sys/uipc_mbuf.c.
1049 */
1050 static void *
1051 bpf_mcpy(dst_arg, src_arg, len)
1052 void *dst_arg;
1053 const void *src_arg;
1054 size_t len;
1055 {
1056 const struct mbuf *m;
1057 u_int count;
1058 u_char *dst;
1059
1060 m = src_arg;
1061 dst = dst_arg;
1062 while (len > 0) {
1063 if (m == 0)
1064 panic("bpf_mcpy");
1065 count = min(m->m_len, len);
1066 memcpy((caddr_t)dst, mtod(m, caddr_t), count);
1067 m = m->m_next;
1068 dst += count;
1069 len -= count;
1070 }
1071 return(dst_arg);
1072 }
1073
1074 /*
1075 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1076 */
1077 void
1078 bpf_mtap(arg, m)
1079 caddr_t arg;
1080 struct mbuf *m;
1081 {
1082 struct bpf_if *bp = (struct bpf_if *)arg;
1083 struct bpf_d *d;
1084 u_int pktlen, slen;
1085 struct mbuf *m0;
1086
1087 pktlen = 0;
1088 for (m0 = m; m0 != 0; m0 = m0->m_next)
1089 pktlen += m0->m_len;
1090
1091 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1092 ++d->bd_rcount;
1093 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1094 if (slen != 0)
1095 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcpy);
1096 }
1097 }
1098
1099 /*
1100 * Move the packet data from interface memory (pkt) into the
1101 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1102 * otherwise 0. "copy" is the routine called to do the actual data
1103 * transfer. memcpy is passed in to copy contiguous chunks, while
1104 * bpf_mcpy is passed in to copy mbuf chains. In the latter case,
1105 * pkt is really an mbuf.
1106 */
1107 static void
1108 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1109 struct bpf_d *d;
1110 u_char *pkt;
1111 u_int pktlen, snaplen;
1112 void *(*cpfn) __P((void *, const void *, size_t));
1113 {
1114 struct bpf_hdr *hp;
1115 int totlen, curlen;
1116 int hdrlen = d->bd_bif->bif_hdrlen;
1117 /*
1118 * Figure out how many bytes to move. If the packet is
1119 * greater or equal to the snapshot length, transfer that
1120 * much. Otherwise, transfer the whole packet (unless
1121 * we hit the buffer size limit).
1122 */
1123 totlen = hdrlen + min(snaplen, pktlen);
1124 if (totlen > d->bd_bufsize)
1125 totlen = d->bd_bufsize;
1126
1127 /*
1128 * Round up the end of the previous packet to the next longword.
1129 */
1130 curlen = BPF_WORDALIGN(d->bd_slen);
1131 if (curlen + totlen > d->bd_bufsize) {
1132 /*
1133 * This packet will overflow the storage buffer.
1134 * Rotate the buffers if we can, then wakeup any
1135 * pending reads.
1136 */
1137 if (d->bd_fbuf == 0) {
1138 /*
1139 * We haven't completed the previous read yet,
1140 * so drop the packet.
1141 */
1142 ++d->bd_dcount;
1143 return;
1144 }
1145 ROTATE_BUFFERS(d);
1146 bpf_wakeup(d);
1147 curlen = 0;
1148 }
1149 else if (d->bd_immediate)
1150 /*
1151 * Immediate mode is set. A packet arrived so any
1152 * reads should be woken up.
1153 */
1154 bpf_wakeup(d);
1155
1156 /*
1157 * Append the bpf header.
1158 */
1159 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1160 microtime(&hp->bh_tstamp);
1161 hp->bh_datalen = pktlen;
1162 hp->bh_hdrlen = hdrlen;
1163 /*
1164 * Copy the packet data into the store buffer and update its length.
1165 */
1166 (*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
1167 d->bd_slen = curlen + totlen;
1168 }
1169
1170 /*
1171 * Initialize all nonzero fields of a descriptor.
1172 */
1173 static int
1174 bpf_allocbufs(d)
1175 struct bpf_d *d;
1176 {
1177
1178 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1179 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1180 d->bd_slen = 0;
1181 d->bd_hlen = 0;
1182 return (0);
1183 }
1184
1185 /*
1186 * Free buffers currently in use by a descriptor.
1187 * Called on close.
1188 */
1189 static void
1190 bpf_freed(d)
1191 struct bpf_d *d;
1192 {
1193 /*
1194 * We don't need to lock out interrupts since this descriptor has
1195 * been detached from its interface and it yet hasn't been marked
1196 * free.
1197 */
1198 if (d->bd_sbuf != 0) {
1199 free(d->bd_sbuf, M_DEVBUF);
1200 if (d->bd_hbuf != 0)
1201 free(d->bd_hbuf, M_DEVBUF);
1202 if (d->bd_fbuf != 0)
1203 free(d->bd_fbuf, M_DEVBUF);
1204 }
1205 if (d->bd_filter)
1206 free((caddr_t)d->bd_filter, M_DEVBUF);
1207
1208 D_MARKFREE(d);
1209 }
1210
1211 /*
1212 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the
1213 * fixed size of the link header (variable length headers not yet supported).
1214 */
1215 void
1216 bpfattach(ifp, dlt, hdrlen)
1217 struct ifnet *ifp;
1218 u_int dlt, hdrlen;
1219 {
1220 struct bpf_if *bp;
1221 bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
1222 if (bp == 0)
1223 panic("bpfattach");
1224
1225 bp->bif_dlist = 0;
1226 bp->bif_driverp = (struct bpf_if **)&ifp->if_bpf;
1227 bp->bif_ifp = ifp;
1228 bp->bif_dlt = dlt;
1229
1230 bp->bif_next = bpf_iflist;
1231 bpf_iflist = bp;
1232
1233 *bp->bif_driverp = 0;
1234
1235 /*
1236 * Compute the length of the bpf header. This is not necessarily
1237 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1238 * that the network layer header begins on a longword boundary (for
1239 * performance reasons and to alleviate alignment restrictions).
1240 */
1241 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1242
1243 #if 0
1244 printf("bpf: %s attached\n", ifp->if_xname);
1245 #endif
1246 }
1247
1248 /*
1249 * Remove an interface from bpf.
1250 */
1251 void
1252 bpfdetach(ifp)
1253 struct ifnet *ifp;
1254 {
1255 struct bpf_if *bp, **pbp;
1256 struct bpf_d *d;
1257 int i, s, cmaj;
1258
1259 /* locate the major number */
1260 for (cmaj = 0; cmaj <= nchrdev; cmaj++)
1261 if (cdevsw[cmaj].d_open == bpfopen)
1262 break;
1263
1264 /* Nuke the vnodes for any open instances */
1265 for (i = 0; i < NBPFILTER; ++i) {
1266 d = &bpf_dtab[i];
1267 if (!D_ISFREE(d) && d->bd_bif != NULL &&
1268 d->bd_bif->bif_ifp == ifp) {
1269 /*
1270 * Detach the descriptor from an interface now.
1271 * It will be free'ed later by close routine.
1272 */
1273 s = splnet();
1274 d->bd_promisc = 0; /* we can't touch device. */
1275 bpf_detachd(d);
1276 splx(s);
1277 vdevgone(cmaj, i, i, VCHR);
1278 }
1279 }
1280
1281 for (bp = bpf_iflist, pbp = &bpf_iflist;
1282 bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
1283 if (bp->bif_ifp == ifp) {
1284 *pbp = bp->bif_next;
1285 free(bp, M_DEVBUF);
1286 break;
1287 }
1288 }
1289 }
1290
1291 /*
1292 * Change the data link type of a BPF instance.
1293 */
1294 void
1295 bpf_change_type(ifp, dlt, hdrlen)
1296 struct ifnet *ifp;
1297 u_int dlt, hdrlen;
1298 {
1299 struct bpf_if *bp;
1300
1301 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1302 if (bp->bif_driverp == (struct bpf_if **)&ifp->if_bpf)
1303 break;
1304 }
1305 if (bp == NULL)
1306 panic("bpf_change_type");
1307
1308 bp->bif_dlt = dlt;
1309
1310 /*
1311 * Compute the length of the bpf header. This is not necessarily
1312 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1313 * that the network layer header begins on a longword boundary (for
1314 * performance reasons and to alleviate alignment restrictions).
1315 */
1316 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1317 }
1318