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