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