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