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