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