bpf.c revision 1.99 1 /* $NetBSD: bpf.c,v 1.99 2004/05/29 08:56:19 darrenr 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)bpf.c 8.4 (Berkeley) 1/9/95
37 * static char rcsid[] =
38 * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp ";
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.99 2004/05/29 08:56:19 darrenr Exp $");
43
44 #include "bpfilter.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/buf.h>
50 #include <sys/time.h>
51 #include <sys/proc.h>
52 #include <sys/user.h>
53 #include <sys/ioctl.h>
54 #include <sys/conf.h>
55 #include <sys/vnode.h>
56
57 #include <sys/file.h>
58 #include <sys/tty.h>
59 #include <sys/uio.h>
60
61 #include <sys/protosw.h>
62 #include <sys/socket.h>
63 #include <sys/errno.h>
64 #include <sys/kernel.h>
65 #include <sys/poll.h>
66 #include <sys/sysctl.h>
67
68 #include <net/if.h>
69
70 #include <net/bpf.h>
71 #include <net/bpfdesc.h>
72
73 #include <net/if_arc.h>
74 #include <net/if_ether.h>
75
76 #include <netinet/in.h>
77 #include <netinet/if_inarp.h>
78
79 #if defined(_KERNEL_OPT)
80 #include "opt_bpf.h"
81 #endif
82
83 #ifndef BPF_BUFSIZE
84 /*
85 * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet
86 * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k).
87 */
88 # define BPF_BUFSIZE 32768
89 #endif
90
91 #define PRINET 26 /* interruptible */
92
93 /*
94 * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able.
95 * XXX the default values should be computed dynamically based
96 * on available memory size and available mbuf clusters.
97 */
98 int bpf_bufsize = BPF_BUFSIZE;
99 int bpf_maxbufsize = BPF_DFLTBUFSIZE; /* XXX set dynamically, see above */
100
101 /*
102 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
103 * bpf_dtab holds the descriptors, indexed by minor device #
104 */
105 struct bpf_if *bpf_iflist;
106 struct bpf_d bpf_dtab[NBPFILTER];
107
108 static int bpf_allocbufs __P((struct bpf_d *));
109 static void bpf_freed __P((struct bpf_d *));
110 static void bpf_ifname __P((struct ifnet *, struct ifreq *));
111 static void *bpf_mcpy __P((void *, const void *, size_t));
112 static int bpf_movein __P((struct uio *, int, int,
113 struct mbuf **, struct sockaddr *));
114 static void bpf_attachd __P((struct bpf_d *, struct bpf_if *));
115 static void bpf_detachd __P((struct bpf_d *));
116 static int bpf_setif __P((struct bpf_d *, struct ifreq *));
117 static void bpf_timed_out __P((void *));
118 static __inline void
119 bpf_wakeup __P((struct bpf_d *));
120 static void catchpacket __P((struct bpf_d *, u_char *, u_int, u_int,
121 void *(*)(void *, const void *, size_t)));
122 static void reset_d __P((struct bpf_d *));
123 static int bpf_getdltlist __P((struct bpf_d *, struct bpf_dltlist *));
124 static int bpf_setdlt __P((struct bpf_d *, u_int));
125 static int bpf_mmapinfo __P((struct bpf_d *, struct bpf_mmapinfo *));
126 static int bpf_waitfordata __P((struct bpf_d *));
127
128 dev_type_open(bpfopen);
129 dev_type_close(bpfclose);
130 dev_type_read(bpfread);
131 dev_type_write(bpfwrite);
132 dev_type_ioctl(bpfioctl);
133 dev_type_poll(bpfpoll);
134 dev_type_kqfilter(bpfkqfilter);
135 dev_type_mmap(bpfmmap);
136
137 const struct cdevsw bpf_cdevsw = {
138 bpfopen, bpfclose, bpfread, bpfwrite, bpfioctl,
139 nostop, notty, bpfpoll, bpfmmap, bpfkqfilter,
140 };
141
142 static int
143 bpf_movein(uio, linktype, mtu, mp, sockp)
144 struct uio *uio;
145 int linktype;
146 int mtu;
147 struct mbuf **mp;
148 struct sockaddr *sockp;
149 {
150 struct mbuf *m;
151 int error;
152 int len;
153 int hlen;
154 int align;
155
156 /*
157 * Build a sockaddr based on the data link layer type.
158 * We do this at this level because the ethernet header
159 * is copied directly into the data field of the sockaddr.
160 * In the case of SLIP, there is no header and the packet
161 * is forwarded as is.
162 * Also, we are careful to leave room at the front of the mbuf
163 * for the link level header.
164 */
165 switch (linktype) {
166
167 case DLT_SLIP:
168 sockp->sa_family = AF_INET;
169 hlen = 0;
170 align = 0;
171 break;
172
173 case DLT_PPP:
174 sockp->sa_family = AF_UNSPEC;
175 hlen = 0;
176 align = 0;
177 break;
178
179 case DLT_EN10MB:
180 sockp->sa_family = AF_UNSPEC;
181 /* XXX Would MAXLINKHDR be better? */
182 /* 6(dst)+6(src)+2(type) */
183 hlen = sizeof(struct ether_header);
184 align = 2;
185 break;
186
187 case DLT_ARCNET:
188 sockp->sa_family = AF_UNSPEC;
189 hlen = ARC_HDRLEN;
190 align = 5;
191 break;
192
193 case DLT_FDDI:
194 sockp->sa_family = AF_LINK;
195 /* XXX 4(FORMAC)+6(dst)+6(src) */
196 hlen = 16;
197 align = 0;
198 break;
199
200 case DLT_ECONET:
201 sockp->sa_family = AF_UNSPEC;
202 hlen = 6;
203 align = 2;
204 break;
205
206 case DLT_NULL:
207 sockp->sa_family = AF_UNSPEC;
208 hlen = 0;
209 align = 0;
210 break;
211
212 default:
213 return (EIO);
214 }
215
216 len = uio->uio_resid;
217 /*
218 * If there aren't enough bytes for a link level header or the
219 * packet length exceeds the interface mtu, return an error.
220 */
221 if (len < hlen || len - hlen > mtu)
222 return (EMSGSIZE);
223
224 /*
225 * XXX Avoid complicated buffer chaining ---
226 * bail if it won't fit in a single mbuf.
227 * (Take into account possible alignment bytes)
228 */
229 if ((unsigned)len > MCLBYTES - align)
230 return (EIO);
231
232 m = m_gethdr(M_WAIT, MT_DATA);
233 m->m_pkthdr.rcvif = 0;
234 m->m_pkthdr.len = len - hlen;
235 if (len > MHLEN - align) {
236 m_clget(m, M_WAIT);
237 if ((m->m_flags & M_EXT) == 0) {
238 error = ENOBUFS;
239 goto bad;
240 }
241 }
242
243 /* Insure the data is properly aligned */
244 if (align > 0) {
245 m->m_data += align;
246 m->m_len -= align;
247 }
248
249 error = uiomove(mtod(m, caddr_t), len, uio);
250 if (error)
251 goto bad;
252 if (hlen != 0) {
253 memcpy(sockp->sa_data, mtod(m, caddr_t), hlen);
254 m->m_data += hlen; /* XXX */
255 len -= hlen;
256 }
257 m->m_len = len;
258 *mp = m;
259 return (0);
260
261 bad:
262 m_freem(m);
263 return (error);
264 }
265
266 /*
267 * Attach file to the bpf interface, i.e. make d listen on bp.
268 * Must be called at splnet.
269 */
270 static void
271 bpf_attachd(d, bp)
272 struct bpf_d *d;
273 struct bpf_if *bp;
274 {
275 /*
276 * Point d at bp, and add d to the interface's list of listeners.
277 * Finally, point the driver's bpf cookie at the interface so
278 * it will divert packets to bpf.
279 */
280 d->bd_bif = bp;
281 d->bd_next = bp->bif_dlist;
282 bp->bif_dlist = d;
283
284 *bp->bif_driverp = bp;
285 }
286
287 /*
288 * Detach a file from its interface.
289 */
290 static void
291 bpf_detachd(d)
292 struct bpf_d *d;
293 {
294 struct bpf_d **p;
295 struct bpf_if *bp;
296
297 bp = d->bd_bif;
298 /*
299 * Check if this descriptor had requested promiscuous mode.
300 * If so, turn it off.
301 */
302 if (d->bd_promisc) {
303 int error;
304
305 d->bd_promisc = 0;
306 /*
307 * Take device out of promiscuous mode. Since we were
308 * able to enter promiscuous mode, we should be able
309 * to turn it off. But we can get an error if
310 * the interface was configured down, so only panic
311 * if we don't get an unexpected error.
312 */
313 error = ifpromisc(bp->bif_ifp, 0);
314 if (error && error != EINVAL)
315 panic("bpf: ifpromisc failed");
316 }
317 /* Remove d from the interface's descriptor list. */
318 p = &bp->bif_dlist;
319 while (*p != d) {
320 p = &(*p)->bd_next;
321 if (*p == 0)
322 panic("bpf_detachd: descriptor not in list");
323 }
324 *p = (*p)->bd_next;
325 if (bp->bif_dlist == 0)
326 /*
327 * Let the driver know that there are no more listeners.
328 */
329 *d->bd_bif->bif_driverp = 0;
330 d->bd_bif = 0;
331 }
332
333
334 /*
335 * Mark a descriptor free by making it point to itself.
336 * This is probably cheaper than marking with a constant since
337 * the address should be in a register anyway.
338 */
339 #define D_ISFREE(d) ((d) == (d)->bd_next)
340 #define D_MARKFREE(d) ((d)->bd_next = (d))
341 #define D_MARKUSED(d) ((d)->bd_next = 0)
342
343 /*
344 * bpfilterattach() is called at boot time.
345 */
346 /* ARGSUSED */
347 void
348 bpfilterattach(n)
349 int n;
350 {
351 int i;
352 /*
353 * Mark all the descriptors free.
354 */
355 for (i = 0; i < NBPFILTER; ++i)
356 D_MARKFREE(&bpf_dtab[i]);
357
358 }
359
360 /*
361 * Open ethernet device. Returns ENXIO for illegal minor device number,
362 * EBUSY if file is open by another process.
363 */
364 /* ARGSUSED */
365 int
366 bpfopen(dev, flag, mode, p)
367 dev_t dev;
368 int flag;
369 int mode;
370 struct proc *p;
371 {
372 struct bpf_d *d;
373
374 if (minor(dev) >= NBPFILTER)
375 return (ENXIO);
376 /*
377 * Each minor can be opened by only one process. If the requested
378 * minor is in use, return EBUSY.
379 */
380 d = &bpf_dtab[minor(dev)];
381 if (!D_ISFREE(d))
382 return (EBUSY);
383
384 /* Mark "free" and do most initialization. */
385 memset((char *)d, 0, sizeof(*d));
386 d->bd_bufsize = bpf_bufsize;
387 d->bd_seesent = 1;
388 callout_init(&d->bd_callout);
389
390 return (0);
391 }
392
393 /*
394 * Close the descriptor by detaching it from its interface,
395 * deallocating its buffers, and marking it free.
396 */
397 /* ARGSUSED */
398 int
399 bpfclose(dev, flag, mode, p)
400 dev_t dev;
401 int flag;
402 int mode;
403 struct proc *p;
404 {
405 struct bpf_d *d = &bpf_dtab[minor(dev)];
406 int s;
407
408 s = splnet();
409 if (d->bd_state == BPF_WAITING)
410 callout_stop(&d->bd_callout);
411 d->bd_state = BPF_IDLE;
412 if (d->bd_bif)
413 bpf_detachd(d);
414 splx(s);
415 bpf_freed(d);
416
417 return (0);
418 }
419
420 /*
421 * Rotate the packet buffers in descriptor d. Move the store buffer
422 * into the hold slot, and the free buffer into the store slot.
423 * Zero the length of the new store buffer.
424 */
425 #define ROTATE_BUFFERS(d) \
426 do { \
427 (d)->bd_hbuf = (d)->bd_sbuf; \
428 (d)->bd_hlen = (d)->bd_slen; \
429 (d)->bd_sbuf = (d)->bd_fbuf; \
430 (d)->bd_slen = 0; \
431 (d)->bd_fbuf = 0; \
432 } while (0)
433
434 /*
435 * bpfread - read next chunk of packets from buffers
436 */
437 int
438 bpfread(dev, uio, ioflag)
439 dev_t dev;
440 struct uio *uio;
441 int ioflag;
442 {
443 struct bpf_d *d = &bpf_dtab[minor(dev)];
444 int timed_out;
445 int error;
446 int s;
447
448 /*
449 * Restrict application to use a buffer the same size as
450 * as kernel buffers.
451 */
452 if (uio->uio_resid != d->bd_bufsize)
453 return (EINVAL);
454
455 s = splnet();
456 if (d->bd_state == BPF_WAITING)
457 callout_stop(&d->bd_callout);
458 timed_out = (d->bd_state == BPF_TIMED_OUT);
459 d->bd_state = BPF_IDLE;
460 error = bpf_waitfordata(d);
461 if (error != 0)
462 goto done;
463 /*
464 * At this point, we know we have something in the hold slot.
465 */
466 splx(s);
467
468 /*
469 * Move data from hold buffer into user space.
470 * We know the entire buffer is transferred since
471 * we checked above that the read buffer is bpf_bufsize bytes.
472 */
473 error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
474
475 s = splnet();
476 d->bd_fbuf = d->bd_hbuf;
477 d->bd_hbuf = 0;
478 d->bd_hlen = 0;
479 done:
480 splx(s);
481 if (error == -1)
482 error = 0;
483 return (error);
484 }
485
486
487 /*
488 * NOTE: splnet() is assumed to be held when calling this function.
489 * It is left to the caller to drop the spl.
490 */
491 static int
492 bpf_waitfordata(d)
493 struct bpf_d *d;
494 {
495 int error;
496
497 /*
498 * If the hold buffer is empty, then do a timed sleep, which
499 * ends when the timeout expires or when enough packets
500 * have arrived to fill the store buffer.
501 */
502 while (d->bd_hbuf == 0) {
503 if (ioflag & IO_NDELAY) {
504 if (d->bd_slen == 0) {
505 return (EWOULDBLOCK);
506 }
507 ROTATE_BUFFERS(d);
508 break;
509 }
510
511 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
512 /*
513 * A packet(s) either arrived since the previous
514 * read or arrived while we were asleep.
515 * Rotate the buffers and return what's here.
516 */
517 ROTATE_BUFFERS(d);
518 break;
519 }
520 error = tsleep((caddr_t)d, PRINET|PCATCH, "bpf",
521 d->bd_rtout);
522 if (error == EINTR || error == ERESTART) {
523 splx(s);
524 return (error);
525 }
526 if (error == EWOULDBLOCK) {
527 /*
528 * On a timeout, return what's in the buffer,
529 * which may be nothing. If there is something
530 * in the store buffer, we can rotate the buffers.
531 */
532 if (d->bd_hbuf)
533 /*
534 * We filled up the buffer in between
535 * getting the timeout and arriving
536 * here, so we don't need to rotate.
537 */
538 break;
539
540 if (d->bd_slen == 0)
541 return -1;
542
543 if (d->bd_mapbuf == -1) {
544 ROTATE_BUFFERS(d);
545 break;
546 }
547 }
548 if (error != 0)
549 return error;
550 }
551
552 return 0;
553 }
554
555
556 /*
557 * If there are processes sleeping on this descriptor, wake them up.
558 */
559 static __inline void
560 bpf_wakeup(d)
561 struct bpf_d *d;
562 {
563 wakeup((caddr_t)d);
564 if (d->bd_async)
565 fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL);
566
567 selnotify(&d->bd_sel, 0);
568 /* XXX */
569 d->bd_sel.sel_pid = 0;
570 }
571
572
573 static void
574 bpf_timed_out(arg)
575 void *arg;
576 {
577 struct bpf_d *d = (struct bpf_d *)arg;
578 int s;
579
580 s = splnet();
581 if (d->bd_state == BPF_WAITING) {
582 d->bd_state = BPF_TIMED_OUT;
583 if (d->bd_slen != 0)
584 bpf_wakeup(d);
585 }
586 splx(s);
587 }
588
589
590 int
591 bpfwrite(dev, uio, ioflag)
592 dev_t dev;
593 struct uio *uio;
594 int ioflag;
595 {
596 struct bpf_d *d = &bpf_dtab[minor(dev)];
597 struct ifnet *ifp;
598 struct mbuf *m;
599 int error, s;
600 static struct sockaddr_storage dst;
601
602 if (d->bd_bif == 0)
603 return (ENXIO);
604
605 ifp = d->bd_bif->bif_ifp;
606
607 if (uio->uio_resid == 0)
608 return (0);
609
610 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m,
611 (struct sockaddr *) &dst);
612 if (error)
613 return (error);
614
615 if (m->m_pkthdr.len > ifp->if_mtu)
616 return (EMSGSIZE);
617
618 if (d->bd_hdrcmplt)
619 dst.ss_family = pseudo_AF_HDRCMPLT;
620
621 s = splsoftnet();
622 error = (*ifp->if_output)(ifp, m, (struct sockaddr *) &dst, NULL);
623 splx(s);
624 /*
625 * The driver frees the mbuf.
626 */
627 return (error);
628 }
629
630 /*
631 * Reset a descriptor by flushing its packet buffer and clearing the
632 * receive and drop counts. Should be called at splnet.
633 */
634 static void
635 reset_d(d)
636 struct bpf_d *d;
637 {
638 if (d->bd_hbuf) {
639 /* Free the hold buffer. */
640 d->bd_fbuf = d->bd_hbuf;
641 d->bd_hbuf = 0;
642 }
643 d->bd_slen = 0;
644 d->bd_hlen = 0;
645 d->bd_rcount = 0;
646 d->bd_dcount = 0;
647 d->bd_ccount = 0;
648 }
649
650 #ifdef BPF_KERN_FILTER
651 extern struct bpf_insn *bpf_tcp_filter;
652 extern struct bpf_insn *bpf_udp_filter;
653 #endif
654
655 /*
656 * FIONREAD Check for read packet available.
657 * BIOCGBLEN Get buffer len [for read()].
658 * BIOCSETF Set ethernet read filter.
659 * BIOCFLUSH Flush read packet buffer.
660 * BIOCPROMISC Put interface into promiscuous mode.
661 * BIOCGDLT Get link layer type.
662 * BIOCGETIF Get interface name.
663 * BIOCSETIF Set interface.
664 * BIOCSRTIMEOUT Set read timeout.
665 * BIOCGRTIMEOUT Get read timeout.
666 * BIOCGSTATS Get packet stats.
667 * BIOCIMMEDIATE Set immediate mode.
668 * BIOCVERSION Get filter language version.
669 * BIOGHDRCMPLT Get "header already complete" flag.
670 * BIOSHDRCMPLT Set "header already complete" flag.
671 */
672 /* ARGSUSED */
673 int
674 bpfioctl(dev, cmd, addr, flag, p)
675 dev_t dev;
676 u_long cmd;
677 caddr_t addr;
678 int flag;
679 struct proc *p;
680 {
681 struct bpf_d *d = &bpf_dtab[minor(dev)];
682 int s, error = 0;
683 #ifdef BPF_KERN_FILTER
684 struct bpf_insn **p;
685 #endif
686
687 s = splnet();
688 if (d->bd_state == BPF_WAITING)
689 callout_stop(&d->bd_callout);
690 d->bd_state = BPF_IDLE;
691 splx(s);
692
693 switch (cmd) {
694
695 default:
696 error = EINVAL;
697 break;
698
699 /*
700 * Check for read packet available.
701 */
702 case FIONREAD:
703 {
704 int n;
705
706 s = splnet();
707 n = d->bd_slen;
708 if (d->bd_hbuf)
709 n += d->bd_hlen;
710 splx(s);
711
712 *(int *)addr = n;
713 break;
714 }
715
716 /*
717 * Get buffer len [for read()].
718 */
719 case BIOCGBLEN:
720 *(u_int *)addr = d->bd_bufsize;
721 break;
722
723 /*
724 * Set buffer length.
725 */
726 case BIOCSBLEN:
727 if (d->bd_bif != 0)
728 error = EINVAL;
729 else {
730 u_int size = *(u_int *)addr;
731
732 if (size > bpf_maxbufsize)
733 *(u_int *)addr = size = bpf_maxbufsize;
734 else if (size < BPF_MINBUFSIZE)
735 *(u_int *)addr = size = BPF_MINBUFSIZE;
736 d->bd_bufsize = size;
737 }
738 break;
739
740 /*
741 * Set link layer read filter.
742 */
743 case BIOCSETF:
744 error = bpf_setf(d, (struct bpf_program *)addr);
745 break;
746
747 #ifdef BPF_KERN_FILTER
748 /*
749 * Set TCP or UDP reject filter.
750 */
751 case BIOCSTCPF:
752 case BIOCSUDPF:
753 if (!suser()) {
754 error = EPERM;
755 break;
756 }
757
758 /* Validate and store filter */
759 error = bpf_setf(d, (struct bpf_program *)addr);
760
761 /* Free possible old filter */
762 if (cmd == BIOCSTCPF)
763 p = &bpf_tcp_filter;
764 else
765 p = &bpf_udp_filter;
766 if (*p != NULL)
767 free((caddr_t)*p, M_DEVBUF);
768
769 /* Steal new filter (noop if error) */
770 s = splnet();
771 *p = d->bd_filter;
772 d->bd_filter = NULL;
773 splx(s);
774 break;
775 #endif
776
777 /*
778 * Flush read packet buffer.
779 */
780 case BIOCFLUSH:
781 s = splnet();
782 reset_d(d);
783 splx(s);
784 break;
785
786 /*
787 * Put interface into promiscuous mode.
788 */
789 case BIOCPROMISC:
790 if (d->bd_bif == 0) {
791 /*
792 * No interface attached yet.
793 */
794 error = EINVAL;
795 break;
796 }
797 s = splnet();
798 if (d->bd_promisc == 0) {
799 error = ifpromisc(d->bd_bif->bif_ifp, 1);
800 if (error == 0)
801 d->bd_promisc = 1;
802 }
803 splx(s);
804 break;
805
806 /*
807 * Get device parameters.
808 */
809 case BIOCGDLT:
810 if (d->bd_bif == 0)
811 error = EINVAL;
812 else
813 *(u_int *)addr = d->bd_bif->bif_dlt;
814 break;
815
816 /*
817 * Get a list of supported device parameters.
818 */
819 case BIOCGDLTLIST:
820 if (d->bd_bif == 0)
821 error = EINVAL;
822 else
823 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
824 break;
825
826 /*
827 * Set device parameters.
828 */
829 case BIOCSDLT:
830 if (d->bd_bif == 0)
831 error = EINVAL;
832 else
833 error = bpf_setdlt(d, *(u_int *)addr);
834 break;
835
836 /*
837 * Set interface name.
838 */
839 case BIOCGETIF:
840 if (d->bd_bif == 0)
841 error = EINVAL;
842 else
843 bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
844 break;
845
846 /*
847 * Set interface.
848 */
849 case BIOCSETIF:
850 error = bpf_setif(d, (struct ifreq *)addr);
851 break;
852
853 /*
854 * Set read timeout.
855 */
856 case BIOCSRTIMEOUT:
857 {
858 struct timeval *tv = (struct timeval *)addr;
859
860 /* Compute number of ticks. */
861 d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
862 if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
863 d->bd_rtout = 1;
864 break;
865 }
866
867 /*
868 * Get read timeout.
869 */
870 case BIOCGRTIMEOUT:
871 {
872 struct timeval *tv = (struct timeval *)addr;
873
874 tv->tv_sec = d->bd_rtout / hz;
875 tv->tv_usec = (d->bd_rtout % hz) * tick;
876 break;
877 }
878
879 /*
880 * Get packet stats.
881 */
882 case BIOCGSTATS:
883 {
884 struct bpf_stat *bs = (struct bpf_stat *)addr;
885
886 bs->bs_recv = d->bd_rcount;
887 bs->bs_drop = d->bd_dcount;
888 bs->bs_capt = d->bd_ccount;
889 break;
890 }
891
892 case BIOCGSTATSOLD:
893 {
894 struct bpf_stat_old *bs = (struct bpf_stat_old *)addr;
895
896 bs->bs_recv = d->bd_rcount;
897 bs->bs_drop = d->bd_dcount;
898 break;
899 }
900
901 /*
902 * Set immediate mode.
903 */
904 case BIOCIMMEDIATE:
905 d->bd_immediate = *(u_int *)addr;
906 break;
907
908 case BIOCVERSION:
909 {
910 struct bpf_version *bv = (struct bpf_version *)addr;
911
912 bv->bv_major = BPF_MAJOR_VERSION;
913 bv->bv_minor = BPF_MINOR_VERSION;
914 break;
915 }
916
917 case BIOCGHDRCMPLT: /* get "header already complete" flag */
918 *(u_int *)addr = d->bd_hdrcmplt;
919 break;
920
921 case BIOCSHDRCMPLT: /* set "header already complete" flag */
922 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
923 break;
924
925 /*
926 * Get "see sent packets" flag
927 */
928 case BIOCGSEESENT:
929 *(u_int *)addr = d->bd_seesent;
930 break;
931
932 /*
933 * Set "see sent" packets flag
934 */
935 case BIOCSSEESENT:
936 d->bd_seesent = *(u_int *)addr;
937 break;
938
939 case BIOCMMAPINFO:
940 error = bpf_mmapinfo(d, (struct bpf_mmapinfo *)addr);
941 break;
942
943 case FIONBIO: /* Non-blocking I/O */
944 /*
945 * No need to do anything special as we use IO_NDELAY in
946 * bpfread() as an indication of whether or not to block
947 * the read.
948 */
949 break;
950
951 case FIOASYNC: /* Send signal on receive packets */
952 d->bd_async = *(int *)addr;
953 break;
954
955 case TIOCSPGRP: /* Process or group to send signals to */
956 case FIOSETOWN:
957 error = fsetown(p, &d->bd_pgid, cmd, addr);
958 break;
959
960 case TIOCGPGRP:
961 case FIOGETOWN:
962 error = fgetown(p, d->bd_pgid, cmd, addr);
963 break;
964 }
965 return (error);
966 }
967
968 /*
969 * Set d's packet filter program to fp. If this file already has a filter,
970 * free it and replace it. Returns EINVAL for bogus requests.
971 */
972 int
973 bpf_setf(d, fp)
974 struct bpf_d *d;
975 struct bpf_program *fp;
976 {
977 struct bpf_insn *fcode, *old;
978 u_int flen, size;
979 int s;
980
981 old = d->bd_filter;
982 if (fp->bf_insns == 0) {
983 if (fp->bf_len != 0)
984 return (EINVAL);
985 s = splnet();
986 d->bd_filter = 0;
987 reset_d(d);
988 splx(s);
989 if (old != 0)
990 free((caddr_t)old, M_DEVBUF);
991 return (0);
992 }
993 flen = fp->bf_len;
994 if (flen > BPF_MAXINSNS)
995 return (EINVAL);
996
997 size = flen * sizeof(*fp->bf_insns);
998 fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
999 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1000 bpf_validate(fcode, (int)flen)) {
1001 s = splnet();
1002 d->bd_filter = fcode;
1003 reset_d(d);
1004 splx(s);
1005 if (old != 0)
1006 free((caddr_t)old, M_DEVBUF);
1007
1008 return (0);
1009 }
1010 free((caddr_t)fcode, M_DEVBUF);
1011 return (EINVAL);
1012 }
1013
1014 /*
1015 * Detach a file from its current interface (if attached at all) and attach
1016 * to the interface indicated by the name stored in ifr.
1017 * Return an errno or 0.
1018 */
1019 static int
1020 bpf_setif(d, ifr)
1021 struct bpf_d *d;
1022 struct ifreq *ifr;
1023 {
1024 struct bpf_if *bp;
1025 char *cp;
1026 int unit_seen, i, s, error;
1027
1028 /*
1029 * Make sure the provided name has a unit number, and default
1030 * it to '0' if not specified.
1031 * XXX This is ugly ... do this differently?
1032 */
1033 unit_seen = 0;
1034 cp = ifr->ifr_name;
1035 cp[sizeof(ifr->ifr_name) - 1] = '\0'; /* sanity */
1036 while (*cp++)
1037 if (*cp >= '0' && *cp <= '9')
1038 unit_seen = 1;
1039 if (!unit_seen) {
1040 /* Make sure to leave room for the '\0'. */
1041 for (i = 0; i < (IFNAMSIZ - 1); ++i) {
1042 if ((ifr->ifr_name[i] >= 'a' &&
1043 ifr->ifr_name[i] <= 'z') ||
1044 (ifr->ifr_name[i] >= 'A' &&
1045 ifr->ifr_name[i] <= 'Z'))
1046 continue;
1047 ifr->ifr_name[i] = '0';
1048 }
1049 }
1050
1051 /*
1052 * Look through attached interfaces for the named one.
1053 */
1054 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
1055 struct ifnet *ifp = bp->bif_ifp;
1056
1057 if (ifp == 0 ||
1058 strcmp(ifp->if_xname, ifr->ifr_name) != 0)
1059 continue;
1060 /* skip additional entry */
1061 if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
1062 continue;
1063 /*
1064 * We found the requested interface.
1065 * If it's not up, return an error.
1066 * Allocate the packet buffers if we need to.
1067 * If we're already attached to requested interface,
1068 * just flush the buffer.
1069 */
1070 if ((ifp->if_flags & IFF_UP) == 0)
1071 return (ENETDOWN);
1072
1073 if (d->bd_sbuf == 0) {
1074 error = bpf_allocbufs(d);
1075 if (error != 0)
1076 return (error);
1077 }
1078 s = splnet();
1079 if (bp != d->bd_bif) {
1080 if (d->bd_bif)
1081 /*
1082 * Detach if attached to something else.
1083 */
1084 bpf_detachd(d);
1085
1086 bpf_attachd(d, bp);
1087 }
1088 reset_d(d);
1089 splx(s);
1090 return (0);
1091 }
1092 /* Not found. */
1093 return (ENXIO);
1094 }
1095
1096 /*
1097 * Copy the interface name to the ifreq.
1098 */
1099 static void
1100 bpf_ifname(ifp, ifr)
1101 struct ifnet *ifp;
1102 struct ifreq *ifr;
1103 {
1104
1105 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
1106 }
1107
1108 /*
1109 * Support for poll() system call
1110 *
1111 * Return true iff the specific operation will not block indefinitely - with
1112 * the assumption that it is safe to positively acknowledge a request for the
1113 * ability to write to the BPF device.
1114 * Otherwise, return false but make a note that a selwakeup() must be done.
1115 */
1116 int
1117 bpfpoll(dev, events, p)
1118 dev_t dev;
1119 int events;
1120 struct proc *p;
1121 {
1122 struct bpf_d *d = &bpf_dtab[minor(dev)];
1123 int s = splnet();
1124 int revents;
1125
1126 revents = events & (POLLOUT | POLLWRNORM);
1127 if (events & (POLLIN | POLLRDNORM)) {
1128 /*
1129 * An imitation of the FIONREAD ioctl code.
1130 */
1131 if ((d->bd_hlen != 0) ||
1132 (d->bd_immediate && d->bd_slen != 0)) {
1133 revents |= events & (POLLIN | POLLRDNORM);
1134 } else if (d->bd_state == BPF_TIMED_OUT) {
1135 if (d->bd_slen != 0)
1136 revents |= events & (POLLIN | POLLRDNORM);
1137 else
1138 revents |= events & POLLIN;
1139 } else {
1140 selrecord(p, &d->bd_sel);
1141 /* Start the read timeout if necessary */
1142 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1143 callout_reset(&d->bd_callout, d->bd_rtout,
1144 bpf_timed_out, d);
1145 d->bd_state = BPF_WAITING;
1146 }
1147 }
1148 }
1149
1150 splx(s);
1151 return (revents);
1152 }
1153
1154 static void
1155 filt_bpfrdetach(struct knote *kn)
1156 {
1157 struct bpf_d *d = kn->kn_hook;
1158 int s;
1159
1160 s = splnet();
1161 SLIST_REMOVE(&d->bd_sel.sel_klist, kn, knote, kn_selnext);
1162 splx(s);
1163 }
1164
1165 static int
1166 filt_bpfread(struct knote *kn, long hint)
1167 {
1168 struct bpf_d *d = kn->kn_hook;
1169
1170 kn->kn_data = d->bd_hlen;
1171 if (d->bd_immediate)
1172 kn->kn_data += d->bd_slen;
1173 return (kn->kn_data > 0);
1174 }
1175
1176 static const struct filterops bpfread_filtops =
1177 { 1, NULL, filt_bpfrdetach, filt_bpfread };
1178
1179 int
1180 bpfkqfilter(dev, kn)
1181 dev_t dev;
1182 struct knote *kn;
1183 {
1184 struct bpf_d *d = &bpf_dtab[minor(dev)];
1185 struct klist *klist;
1186 int s;
1187
1188 switch (kn->kn_filter) {
1189 case EVFILT_READ:
1190 klist = &d->bd_sel.sel_klist;
1191 kn->kn_fop = &bpfread_filtops;
1192 break;
1193
1194 default:
1195 return (1);
1196 }
1197
1198 kn->kn_hook = d;
1199
1200 s = splnet();
1201 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1202 splx(s);
1203
1204 return (0);
1205 }
1206
1207 /*
1208 * Incoming linkage from device drivers. Process the packet pkt, of length
1209 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1210 * by each process' filter, and if accepted, stashed into the corresponding
1211 * buffer.
1212 */
1213 void
1214 bpf_tap(arg, pkt, pktlen)
1215 caddr_t arg;
1216 u_char *pkt;
1217 u_int pktlen;
1218 {
1219 struct bpf_if *bp;
1220 struct bpf_d *d;
1221 u_int slen;
1222 /*
1223 * Note that the ipl does not have to be raised at this point.
1224 * The only problem that could arise here is that if two different
1225 * interfaces shared any data. This is not the case.
1226 */
1227 bp = (struct bpf_if *)arg;
1228 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1229 ++d->bd_rcount;
1230 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1231 if (slen != 0)
1232 catchpacket(d, pkt, pktlen, slen, memcpy);
1233 }
1234 }
1235
1236 /*
1237 * Copy data from an mbuf chain into a buffer. This code is derived
1238 * from m_copydata in sys/uipc_mbuf.c.
1239 */
1240 static void *
1241 bpf_mcpy(dst_arg, src_arg, len)
1242 void *dst_arg;
1243 const void *src_arg;
1244 size_t len;
1245 {
1246 const struct mbuf *m;
1247 u_int count;
1248 u_char *dst;
1249
1250 m = src_arg;
1251 dst = dst_arg;
1252 while (len > 0) {
1253 if (m == 0)
1254 panic("bpf_mcpy");
1255 count = min(m->m_len, len);
1256 memcpy((caddr_t)dst, mtod(m, caddr_t), count);
1257 m = m->m_next;
1258 dst += count;
1259 len -= count;
1260 }
1261 return (dst_arg);
1262 }
1263
1264 /*
1265 * Dispatch a packet to all the listeners on interface bp.
1266 *
1267 * marg pointer to the packet, either a data buffer or an mbuf chain
1268 * buflen buffer length, if marg is a data buffer
1269 * cpfn a function that can copy marg into the listener's buffer
1270 * pktlen length of the packet
1271 * rcvif either NULL or the interface the packet came in on.
1272 */
1273 static __inline void
1274 bpf_deliver(struct bpf_if *bp, void *(*cpfn)(void *, const void *, size_t),
1275 void *marg, u_int pktlen, u_int buflen, struct ifnet *rcvif)
1276 {
1277 u_int slen;
1278 struct bpf_d *d;
1279
1280 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1281 if (!d->bd_seesent && (rcvif == NULL))
1282 continue;
1283 ++d->bd_rcount;
1284 slen = bpf_filter(d->bd_filter, marg, pktlen, buflen);
1285 if (slen != 0)
1286 catchpacket(d, marg, pktlen, slen, cpfn);
1287 }
1288 }
1289
1290 static __inline u_int
1291 bpf_measure(struct mbuf *m)
1292 {
1293 struct mbuf *m0;
1294 u_int pktlen;
1295
1296 pktlen = 0;
1297 for (m0 = m; m0 != 0; m0 = m0->m_next)
1298 pktlen += m0->m_len;
1299 return pktlen;
1300 }
1301
1302 /*
1303 * Incoming linkage from device drivers, when the head of the packet is in
1304 * a buffer, and the tail is in an mbuf chain.
1305 */
1306 void
1307 bpf_mtap2(arg, data, dlen, m)
1308 caddr_t arg;
1309 void *data;
1310 u_int dlen;
1311 struct mbuf *m;
1312 {
1313 struct bpf_if *bp = (struct bpf_if *)arg;
1314 u_int pktlen;
1315 struct mbuf mb;
1316
1317 pktlen = bpf_measure(m) + dlen;
1318
1319 /*
1320 * Craft on-stack mbuf suitable for passing to bpf_filter.
1321 * Note that we cut corners here; we only setup what's
1322 * absolutely needed--this mbuf should never go anywhere else.
1323 */
1324 mb.m_next = m;
1325 mb.m_data = data;
1326 mb.m_len = dlen;
1327
1328 bpf_deliver(bp, bpf_mcpy, &mb, pktlen, 0, m->m_pkthdr.rcvif);
1329 }
1330
1331 /*
1332 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1333 */
1334 void
1335 bpf_mtap(arg, m)
1336 caddr_t arg;
1337 struct mbuf *m;
1338 {
1339 void *(*cpfn) __P((void *, const void *, size_t));
1340 struct bpf_if *bp = (struct bpf_if *)arg;
1341 u_int pktlen, buflen;
1342 void *marg;
1343
1344 pktlen = bpf_measure(m);
1345
1346 if (pktlen == m->m_len) {
1347 cpfn = memcpy;
1348 marg = mtod(m, void *);
1349 buflen = pktlen;
1350 } else {
1351 cpfn = bpf_mcpy;
1352 marg = m;
1353 buflen = 0;
1354 }
1355
1356 bpf_deliver(bp, cpfn, marg, pktlen, buflen, m->m_pkthdr.rcvif);
1357 }
1358
1359 /*
1360 * Move the packet data from interface memory (pkt) into the
1361 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1362 * otherwise 0. "copy" is the routine called to do the actual data
1363 * transfer. memcpy is passed in to copy contiguous chunks, while
1364 * bpf_mcpy is passed in to copy mbuf chains. In the latter case,
1365 * pkt is really an mbuf.
1366 */
1367 static void
1368 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1369 struct bpf_d *d;
1370 u_char *pkt;
1371 u_int pktlen, snaplen;
1372 void *(*cpfn) __P((void *, const void *, size_t));
1373 {
1374 struct bpf_hdr *hp;
1375 int totlen, curlen;
1376 int hdrlen = d->bd_bif->bif_hdrlen;
1377
1378 ++d->bd_ccount;
1379 /*
1380 * Figure out how many bytes to move. If the packet is
1381 * greater or equal to the snapshot length, transfer that
1382 * much. Otherwise, transfer the whole packet (unless
1383 * we hit the buffer size limit).
1384 */
1385 totlen = hdrlen + min(snaplen, pktlen);
1386 if (totlen > d->bd_bufsize)
1387 totlen = d->bd_bufsize;
1388
1389 /*
1390 * Round up the end of the previous packet to the next longword.
1391 */
1392 curlen = BPF_WORDALIGN(d->bd_slen);
1393 if (curlen + totlen > d->bd_bufsize) {
1394 /*
1395 * This packet will overflow the storage buffer.
1396 * Rotate the buffers if we can, then wakeup any
1397 * pending reads.
1398 */
1399 if (d->bd_fbuf == 0 || d->bd_mapbuf != -1) {
1400 /*
1401 * We haven't completed the previous read yet,
1402 * so drop the packet.
1403 */
1404 ++d->bd_dcount;
1405 return;
1406 }
1407 ROTATE_BUFFERS(d);
1408 bpf_wakeup(d);
1409 curlen = 0;
1410 }
1411
1412 /*
1413 * Append the bpf header.
1414 */
1415 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1416 microtime(&hp->bh_tstamp);
1417 hp->bh_datalen = pktlen;
1418 hp->bh_hdrlen = hdrlen;
1419 /*
1420 * Copy the packet data into the store buffer and update its length.
1421 */
1422 (*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
1423 d->bd_slen = curlen + totlen;
1424
1425 /*
1426 * Call bpf_wakeup after bd_slen has been updated so that kevent(2)
1427 * will cause filt_bpfread() to be called with it adjusted.
1428 */
1429 if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1430 /*
1431 * Immediate mode is set, or the read timeout has
1432 * already expired during a select call. A packet
1433 * arrived, so the reader should be woken up.
1434 */
1435 bpf_wakeup(d);
1436 }
1437
1438 /*
1439 * Initialize all nonzero fields of a descriptor.
1440 */
1441 static int
1442 bpf_allocbufs(d)
1443 struct bpf_d *d;
1444 {
1445
1446 d->bd_bufs[1] = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
1447 if (d->bd_bufs[1] == NULL)
1448 return (ENOBUFS);
1449 d->bd_fbuf = d->bd_bufs[1];
1450 d->bd_bufs[0] = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
1451 if (d->bd_bufs[0] == NULL) {
1452 free(d->bd_bufs[1], M_DEVBUF);
1453 d->bd_bufs[1] = NULL;
1454 d->bd_fbuf = NULL;
1455 return (ENOBUFS);
1456 }
1457 d->bd_sbuf = d->bd_bufs[0];
1458 d->bd_fbuf = d->bd_bufs[1];
1459 d->bd_slen = 0;
1460 d->bd_hlen = 0;
1461 d->bd_mapbuf = -1;
1462 return (0);
1463 }
1464
1465 /*
1466 * Free buffers currently in use by a descriptor.
1467 * Called on close.
1468 */
1469 static void
1470 bpf_freed(d)
1471 struct bpf_d *d;
1472 {
1473 /*
1474 * We don't need to lock out interrupts since this descriptor has
1475 * been detached from its interface and it yet hasn't been marked
1476 * free.
1477 */
1478 if (d->bd_bufs[0] != 0)
1479 free(d->bd_bufs[0], M_DEVBUF);
1480 if (d->bd_bufs[1] != 0)
1481 free(d->bd_bufs[1], M_DEVBUF);
1482 if (d->bd_filter)
1483 free((caddr_t)d->bd_filter, M_DEVBUF);
1484
1485 D_MARKFREE(d);
1486 }
1487
1488 /*
1489 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the
1490 * fixed size of the link header (variable length headers not yet supported).
1491 */
1492 void
1493 bpfattach(ifp, dlt, hdrlen)
1494 struct ifnet *ifp;
1495 u_int dlt, hdrlen;
1496 {
1497
1498 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1499 }
1500
1501 /*
1502 * Attach additional dlt for a interface to bpf. dlt is the link layer type;
1503 * hdrlen is the fixed size of the link header for the specified dlt
1504 * (variable length headers not yet supported).
1505 */
1506 void
1507 bpfattach2(ifp, dlt, hdrlen, driverp)
1508 struct ifnet *ifp;
1509 u_int dlt, hdrlen;
1510 caddr_t *driverp;
1511 {
1512 struct bpf_if *bp;
1513 bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
1514 if (bp == 0)
1515 panic("bpfattach");
1516
1517 bp->bif_dlist = 0;
1518 bp->bif_driverp = (struct bpf_if **)driverp;
1519 bp->bif_ifp = ifp;
1520 bp->bif_dlt = dlt;
1521
1522 bp->bif_next = bpf_iflist;
1523 bpf_iflist = bp;
1524
1525 *bp->bif_driverp = 0;
1526
1527 /*
1528 * Compute the length of the bpf header. This is not necessarily
1529 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1530 * that the network layer header begins on a longword boundary (for
1531 * performance reasons and to alleviate alignment restrictions).
1532 */
1533 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1534
1535 #if 0
1536 printf("bpf: %s attached\n", ifp->if_xname);
1537 #endif
1538 }
1539
1540 /*
1541 * Remove an interface from bpf.
1542 */
1543 void
1544 bpfdetach(ifp)
1545 struct ifnet *ifp;
1546 {
1547 struct bpf_if *bp, **pbp;
1548 struct bpf_d *d;
1549 int i, s, cmaj;
1550
1551 /* locate the major number */
1552 cmaj = cdevsw_lookup_major(&bpf_cdevsw);
1553
1554 /* Nuke the vnodes for any open instances */
1555 for (i = 0; i < NBPFILTER; ++i) {
1556 d = &bpf_dtab[i];
1557 if (!D_ISFREE(d) && d->bd_bif != NULL &&
1558 d->bd_bif->bif_ifp == ifp) {
1559 /*
1560 * Detach the descriptor from an interface now.
1561 * It will be free'ed later by close routine.
1562 */
1563 s = splnet();
1564 d->bd_promisc = 0; /* we can't touch device. */
1565 bpf_detachd(d);
1566 splx(s);
1567 vdevgone(cmaj, i, i, VCHR);
1568 }
1569 }
1570
1571 again:
1572 for (bp = bpf_iflist, pbp = &bpf_iflist;
1573 bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
1574 if (bp->bif_ifp == ifp) {
1575 *pbp = bp->bif_next;
1576 free(bp, M_DEVBUF);
1577 goto again;
1578 }
1579 }
1580 }
1581
1582 /*
1583 * Change the data link type of a interface.
1584 */
1585 void
1586 bpf_change_type(ifp, dlt, hdrlen)
1587 struct ifnet *ifp;
1588 u_int dlt, hdrlen;
1589 {
1590 struct bpf_if *bp;
1591
1592 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1593 if (bp->bif_driverp == (struct bpf_if **)&ifp->if_bpf)
1594 break;
1595 }
1596 if (bp == NULL)
1597 panic("bpf_change_type");
1598
1599 bp->bif_dlt = dlt;
1600
1601 /*
1602 * Compute the length of the bpf header. This is not necessarily
1603 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1604 * that the network layer header begins on a longword boundary (for
1605 * performance reasons and to alleviate alignment restrictions).
1606 */
1607 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1608 }
1609
1610 /*
1611 * Get a list of available data link type of the interface.
1612 */
1613 static int
1614 bpf_getdltlist(d, bfl)
1615 struct bpf_d *d;
1616 struct bpf_dltlist *bfl;
1617 {
1618 int n, error;
1619 struct ifnet *ifp;
1620 struct bpf_if *bp;
1621
1622 ifp = d->bd_bif->bif_ifp;
1623 n = 0;
1624 error = 0;
1625 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1626 if (bp->bif_ifp != ifp)
1627 continue;
1628 if (bfl->bfl_list != NULL) {
1629 if (n >= bfl->bfl_len)
1630 return ENOMEM;
1631 error = copyout(&bp->bif_dlt,
1632 bfl->bfl_list + n, sizeof(u_int));
1633 }
1634 n++;
1635 }
1636 bfl->bfl_len = n;
1637 return error;
1638 }
1639
1640 /*
1641 * Set the data link type of a BPF instance.
1642 */
1643 static int
1644 bpf_setdlt(d, dlt)
1645 struct bpf_d *d;
1646 u_int dlt;
1647 {
1648 int s, error, opromisc;
1649 struct ifnet *ifp;
1650 struct bpf_if *bp;
1651
1652 if (d->bd_bif->bif_dlt == dlt)
1653 return 0;
1654 ifp = d->bd_bif->bif_ifp;
1655 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1656 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1657 break;
1658 }
1659 if (bp == NULL)
1660 return EINVAL;
1661
1662 /*
1663 * Provide a mmap(2) interface to the BPF buffers.
1664 * Read-only mapping (PROT_READ) is enforced by this driver - an application
1665 * using this should never write to this buffer and especially not with copy
1666 * on write as the real buffer contents will then disappear from the process
1667 * view.
1668 *
1669 * An application should create two maps: one for each buffer that bpf has
1670 * internally and use the information returned from BIOCMMAPINFO to determine
1671 * which one has valid data in it and how much data is valid.
1672 */
1673 paddr_t
1674 bpfmmap(dev_t dev, off_t off, int prot)
1675 {
1676 struct bpf_d *d;
1677 u_int uoff;
1678
1679 if (prot != VM_PROT_READ)
1680 return -1;
1681
1682 if (off & PAGE_MASK)
1683 panic("bpfmmap");
1684
1685 d = &bpf_dtab[minor(dev)];
1686 uoff = (u_int)off;
1687
1688 if (uoff >= 0 && uoff < d->bd_bufsize)
1689 return (atop(d->bd_bufs[0] + uoff));
1690
1691 if (uoff >= d->bd_bufsize && uoff < (d->bd_bufsize * 2))
1692 return (atop(d->bd_bufs[1] + (uoff - d->bd_bufsize)));
1693
1694 /* Page not found. */
1695 return (-1);
1696 }
1697
1698 static int
1699 bpf_mmapinfo(d, info)
1700 struct bpf_d *d;
1701 struct bpf_mmapinfo *info;
1702 {
1703 int which, s, error;
1704
1705 s = splnet();
1706
1707 if (info->bpm_op == BPM_RELEASE) { /* only want to unlock */
1708 d->bd_mapbuf = -1;
1709 splx(s);
1710 return 0;
1711 }
1712
1713 /*
1714 * Currently only two operations are supported, release and acquire.
1715 * If it's not one of these then return an error.
1716 */
1717 if (info->bpm_op != BPM_ACQUIRE) {
1718 splx(s);
1719 return EINVAL;
1720 }
1721
1722 /*
1723 * An incoming call must give up the current buffer locked for use
1724 * with mmap, if it has one, so that bpf has somewhere to write new
1725 * data when this call returns.
1726 */
1727 if (d->bd_mapbuf != -1) {
1728 d->bd_fbuf = d->bd_hbuf;
1729 d->bd_hbuf = NULL;
1730 d->bd_hlen = 0;
1731 d->bd_mapbuf = -1;
1732 }
1733
1734 error = bpf_waitfordata(d);
1735 if (error == 0) {
1736 if (d->bd_hbuf == d->bd_bufs[0])
1737 which = 0;
1738 else if (d->bd_hbuf == d->bd_bufs[1])
1739 which = 1;
1740 else
1741 which = -1;
1742 d->bd_mapbuf = which;
1743 info->bpm_len = d->bd_hlen;
1744 info->bpm_which = which;
1745 }
1746 splx(s);
1747 return 0;
1748 }
1749 s = splnet();
1750 opromisc = d->bd_promisc;
1751 bpf_detachd(d);
1752 bpf_attachd(d, bp);
1753 reset_d(d);
1754 if (opromisc) {
1755 error = ifpromisc(bp->bif_ifp, 1);
1756 if (error)
1757 printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
1758 bp->bif_ifp->if_xname, error);
1759 else
1760 d->bd_promisc = 1;
1761 }
1762 splx(s);
1763 return 0;
1764 }
1765
1766 static int
1767 sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS)
1768 {
1769 int newsize, error;
1770 struct sysctlnode node;
1771
1772 node = *rnode;
1773 node.sysctl_data = &newsize;
1774 newsize = bpf_maxbufsize;
1775 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1776 if (error || newp == NULL)
1777 return (error);
1778
1779 if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE)
1780 return (EINVAL);
1781
1782 bpf_maxbufsize = newsize;
1783
1784 return (0);
1785 }
1786
1787 SYSCTL_SETUP(sysctl_net_bfp_setup, "sysctl net.bpf subtree setup")
1788 {
1789 struct sysctlnode *node;
1790
1791 sysctl_createv(clog, 0, NULL, NULL,
1792 CTLFLAG_PERMANENT,
1793 CTLTYPE_NODE, "net", NULL,
1794 NULL, 0, NULL, 0,
1795 CTL_NET, CTL_EOL);
1796
1797 node = NULL;
1798 sysctl_createv(clog, 0, NULL, &node,
1799 CTLFLAG_PERMANENT,
1800 CTLTYPE_NODE, "bpf",
1801 SYSCTL_DESCR("BPF options"),
1802 NULL, 0, NULL, 0,
1803 CTL_NET, CTL_CREATE, CTL_EOL);
1804 if (node != NULL)
1805 sysctl_createv(clog, 0, NULL, NULL,
1806 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1807 CTLTYPE_INT, "maxbufsize",
1808 SYSCTL_DESCR("Maximum size for data capture buffer"),
1809 sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0,
1810 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1811 }
1812
1813
1814