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