bpf.c revision 1.208 1 /* $NetBSD: bpf.c,v 1.208 2017/02/01 08:07:27 ozaki-r 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.208 2017/02/01 08:07:27 ozaki-r Exp $");
43
44 #if defined(_KERNEL_OPT)
45 #include "opt_bpf.h"
46 #include "sl.h"
47 #include "strip.h"
48 #include "opt_net_mpsafe.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/ioctl.h>
58 #include <sys/conf.h>
59 #include <sys/vnode.h>
60 #include <sys/queue.h>
61 #include <sys/stat.h>
62 #include <sys/module.h>
63 #include <sys/atomic.h>
64 #include <sys/cpu.h>
65
66 #include <sys/file.h>
67 #include <sys/filedesc.h>
68 #include <sys/tty.h>
69 #include <sys/uio.h>
70
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/errno.h>
74 #include <sys/kernel.h>
75 #include <sys/poll.h>
76 #include <sys/sysctl.h>
77 #include <sys/kauth.h>
78 #include <sys/syslog.h>
79
80 #include <net/if.h>
81 #include <net/slip.h>
82
83 #include <net/bpf.h>
84 #include <net/bpfdesc.h>
85 #include <net/bpfjit.h>
86
87 #include <net/if_arc.h>
88 #include <net/if_ether.h>
89
90 #include <netinet/in.h>
91 #include <netinet/if_inarp.h>
92
93
94 #include <compat/sys/sockio.h>
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 static int bpf_bufsize = BPF_BUFSIZE;
112 static int bpf_maxbufsize = BPF_DFLTBUFSIZE; /* XXX set dynamically, see above */
113 static bool bpf_jit = false;
114
115 struct bpfjit_ops bpfjit_module_ops = {
116 .bj_generate_code = NULL,
117 .bj_free_code = NULL
118 };
119
120 /*
121 * Global BPF statistics returned by net.bpf.stats sysctl.
122 */
123 static struct bpf_stat bpf_gstats;
124
125 /*
126 * Use a mutex to avoid a race condition between gathering the stats/peers
127 * and opening/closing the device.
128 */
129 static kmutex_t bpf_mtx;
130
131 /*
132 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
133 * bpf_dtab holds the descriptors, indexed by minor device #
134 */
135 static struct bpf_if *bpf_iflist;
136 static LIST_HEAD(, bpf_d) bpf_list;
137
138 static int bpf_allocbufs(struct bpf_d *);
139 static void bpf_deliver(struct bpf_if *,
140 void *(*cpfn)(void *, const void *, size_t),
141 void *, u_int, u_int, const bool);
142 static void bpf_freed(struct bpf_d *);
143 static void bpf_ifname(struct ifnet *, struct ifreq *);
144 static void *bpf_mcpy(void *, const void *, size_t);
145 static int bpf_movein(struct uio *, int, uint64_t,
146 struct mbuf **, struct sockaddr *);
147 static void bpf_attachd(struct bpf_d *, struct bpf_if *);
148 static void bpf_detachd(struct bpf_d *);
149 static int bpf_setif(struct bpf_d *, struct ifreq *);
150 static int bpf_setf(struct bpf_d *, struct bpf_program *);
151 static void bpf_timed_out(void *);
152 static inline void
153 bpf_wakeup(struct bpf_d *);
154 static int bpf_hdrlen(struct bpf_d *);
155 static void catchpacket(struct bpf_d *, u_char *, u_int, u_int,
156 void *(*)(void *, const void *, size_t), struct timespec *);
157 static void reset_d(struct bpf_d *);
158 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
159 static int bpf_setdlt(struct bpf_d *, u_int);
160
161 static int bpf_read(struct file *, off_t *, struct uio *, kauth_cred_t,
162 int);
163 static int bpf_write(struct file *, off_t *, struct uio *, kauth_cred_t,
164 int);
165 static int bpf_ioctl(struct file *, u_long, void *);
166 static int bpf_poll(struct file *, int);
167 static int bpf_stat(struct file *, struct stat *);
168 static int bpf_close(struct file *);
169 static int bpf_kqfilter(struct file *, struct knote *);
170 static void bpf_softintr(void *);
171
172 static const struct fileops bpf_fileops = {
173 .fo_read = bpf_read,
174 .fo_write = bpf_write,
175 .fo_ioctl = bpf_ioctl,
176 .fo_fcntl = fnullop_fcntl,
177 .fo_poll = bpf_poll,
178 .fo_stat = bpf_stat,
179 .fo_close = bpf_close,
180 .fo_kqfilter = bpf_kqfilter,
181 .fo_restart = fnullop_restart,
182 };
183
184 dev_type_open(bpfopen);
185
186 const struct cdevsw bpf_cdevsw = {
187 .d_open = bpfopen,
188 .d_close = noclose,
189 .d_read = noread,
190 .d_write = nowrite,
191 .d_ioctl = noioctl,
192 .d_stop = nostop,
193 .d_tty = notty,
194 .d_poll = nopoll,
195 .d_mmap = nommap,
196 .d_kqfilter = nokqfilter,
197 .d_discard = nodiscard,
198 .d_flag = D_OTHER
199 };
200
201 bpfjit_func_t
202 bpf_jit_generate(bpf_ctx_t *bc, void *code, size_t size)
203 {
204
205 membar_consumer();
206 if (bpfjit_module_ops.bj_generate_code != NULL) {
207 return bpfjit_module_ops.bj_generate_code(bc, code, size);
208 }
209 return NULL;
210 }
211
212 void
213 bpf_jit_freecode(bpfjit_func_t jcode)
214 {
215 KASSERT(bpfjit_module_ops.bj_free_code != NULL);
216 bpfjit_module_ops.bj_free_code(jcode);
217 }
218
219 static int
220 bpf_movein(struct uio *uio, int linktype, uint64_t mtu, struct mbuf **mp,
221 struct sockaddr *sockp)
222 {
223 struct mbuf *m;
224 int error;
225 size_t len;
226 size_t hlen;
227 size_t align;
228
229 /*
230 * Build a sockaddr based on the data link layer type.
231 * We do this at this level because the ethernet header
232 * is copied directly into the data field of the sockaddr.
233 * In the case of SLIP, there is no header and the packet
234 * is forwarded as is.
235 * Also, we are careful to leave room at the front of the mbuf
236 * for the link level header.
237 */
238 switch (linktype) {
239
240 case DLT_SLIP:
241 sockp->sa_family = AF_INET;
242 hlen = 0;
243 align = 0;
244 break;
245
246 case DLT_PPP:
247 sockp->sa_family = AF_UNSPEC;
248 hlen = 0;
249 align = 0;
250 break;
251
252 case DLT_EN10MB:
253 sockp->sa_family = AF_UNSPEC;
254 /* XXX Would MAXLINKHDR be better? */
255 /* 6(dst)+6(src)+2(type) */
256 hlen = sizeof(struct ether_header);
257 align = 2;
258 break;
259
260 case DLT_ARCNET:
261 sockp->sa_family = AF_UNSPEC;
262 hlen = ARC_HDRLEN;
263 align = 5;
264 break;
265
266 case DLT_FDDI:
267 sockp->sa_family = AF_LINK;
268 /* XXX 4(FORMAC)+6(dst)+6(src) */
269 hlen = 16;
270 align = 0;
271 break;
272
273 case DLT_ECONET:
274 sockp->sa_family = AF_UNSPEC;
275 hlen = 6;
276 align = 2;
277 break;
278
279 case DLT_NULL:
280 sockp->sa_family = AF_UNSPEC;
281 hlen = 0;
282 align = 0;
283 break;
284
285 default:
286 return (EIO);
287 }
288
289 len = uio->uio_resid;
290 /*
291 * If there aren't enough bytes for a link level header or the
292 * packet length exceeds the interface mtu, return an error.
293 */
294 if (len - hlen > mtu)
295 return (EMSGSIZE);
296
297 /*
298 * XXX Avoid complicated buffer chaining ---
299 * bail if it won't fit in a single mbuf.
300 * (Take into account possible alignment bytes)
301 */
302 if (len + align > MCLBYTES)
303 return (EIO);
304
305 m = m_gethdr(M_WAIT, MT_DATA);
306 m_reset_rcvif(m);
307 m->m_pkthdr.len = (int)(len - hlen);
308 if (len + align > MHLEN) {
309 m_clget(m, M_WAIT);
310 if ((m->m_flags & M_EXT) == 0) {
311 error = ENOBUFS;
312 goto bad;
313 }
314 }
315
316 /* Insure the data is properly aligned */
317 if (align > 0) {
318 m->m_data += align;
319 m->m_len -= (int)align;
320 }
321
322 error = uiomove(mtod(m, void *), len, uio);
323 if (error)
324 goto bad;
325 if (hlen != 0) {
326 memcpy(sockp->sa_data, mtod(m, void *), hlen);
327 m->m_data += hlen; /* XXX */
328 len -= hlen;
329 }
330 m->m_len = (int)len;
331 *mp = m;
332 return (0);
333
334 bad:
335 m_freem(m);
336 return (error);
337 }
338
339 /*
340 * Attach file to the bpf interface, i.e. make d listen on bp.
341 * Must be called at splnet.
342 */
343 static void
344 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
345 {
346 KASSERT(mutex_owned(&bpf_mtx));
347 /*
348 * Point d at bp, and add d to the interface's list of listeners.
349 * Finally, point the driver's bpf cookie at the interface so
350 * it will divert packets to bpf.
351 */
352 d->bd_bif = bp;
353 d->bd_next = bp->bif_dlist;
354 bp->bif_dlist = d;
355
356 *bp->bif_driverp = bp;
357 }
358
359 /*
360 * Detach a file from its interface.
361 */
362 static void
363 bpf_detachd(struct bpf_d *d)
364 {
365 struct bpf_d **p;
366 struct bpf_if *bp;
367
368 KASSERT(mutex_owned(&bpf_mtx));
369
370 bp = d->bd_bif;
371 /*
372 * Check if this descriptor had requested promiscuous mode.
373 * If so, turn it off.
374 */
375 if (d->bd_promisc) {
376 int error __diagused;
377
378 d->bd_promisc = 0;
379 /*
380 * Take device out of promiscuous mode. Since we were
381 * able to enter promiscuous mode, we should be able
382 * to turn it off. But we can get an error if
383 * the interface was configured down, so only panic
384 * if we don't get an unexpected error.
385 */
386 error = ifpromisc(bp->bif_ifp, 0);
387 #ifdef DIAGNOSTIC
388 if (error)
389 printf("%s: ifpromisc failed: %d", __func__, error);
390 #endif
391 }
392 /* Remove d from the interface's descriptor list. */
393 p = &bp->bif_dlist;
394 while (*p != d) {
395 p = &(*p)->bd_next;
396 if (*p == NULL)
397 panic("%s: descriptor not in list", __func__);
398 }
399 *p = (*p)->bd_next;
400 if (bp->bif_dlist == NULL)
401 /*
402 * Let the driver know that there are no more listeners.
403 */
404 *d->bd_bif->bif_driverp = NULL;
405 d->bd_bif = NULL;
406 }
407
408 static void
409 bpf_init(void)
410 {
411
412 mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE);
413
414 LIST_INIT(&bpf_list);
415
416 bpf_gstats.bs_recv = 0;
417 bpf_gstats.bs_drop = 0;
418 bpf_gstats.bs_capt = 0;
419
420 return;
421 }
422
423 /*
424 * bpfilterattach() is called at boot time. We don't need to do anything
425 * here, since any initialization will happen as part of module init code.
426 */
427 /* ARGSUSED */
428 void
429 bpfilterattach(int n)
430 {
431
432 }
433
434 /*
435 * Open ethernet device. Clones.
436 */
437 /* ARGSUSED */
438 int
439 bpfopen(dev_t dev, int flag, int mode, struct lwp *l)
440 {
441 struct bpf_d *d;
442 struct file *fp;
443 int error, fd;
444
445 /* falloc() will fill in the descriptor for us. */
446 if ((error = fd_allocfile(&fp, &fd)) != 0)
447 return error;
448
449 d = kmem_zalloc(sizeof(*d), KM_SLEEP);
450 d->bd_bufsize = bpf_bufsize;
451 d->bd_seesent = 1;
452 d->bd_feedback = 0;
453 d->bd_pid = l->l_proc->p_pid;
454 #ifdef _LP64
455 if (curproc->p_flag & PK_32)
456 d->bd_compat32 = 1;
457 #endif
458 getnanotime(&d->bd_btime);
459 d->bd_atime = d->bd_mtime = d->bd_btime;
460 callout_init(&d->bd_callout, 0);
461 selinit(&d->bd_sel);
462 d->bd_sih = softint_establish(SOFTINT_CLOCK, bpf_softintr, d);
463 d->bd_jitcode = NULL;
464
465 mutex_enter(&bpf_mtx);
466 LIST_INSERT_HEAD(&bpf_list, d, bd_list);
467 mutex_exit(&bpf_mtx);
468
469 return fd_clone(fp, fd, flag, &bpf_fileops, d);
470 }
471
472 /*
473 * Close the descriptor by detaching it from its interface,
474 * deallocating its buffers, and marking it free.
475 */
476 /* ARGSUSED */
477 static int
478 bpf_close(struct file *fp)
479 {
480 struct bpf_d *d;
481 int s;
482
483 KERNEL_LOCK(1, NULL);
484 mutex_enter(&bpf_mtx);
485
486 if ((d = fp->f_bpf) == NULL) {
487 mutex_exit(&bpf_mtx);
488 KERNEL_UNLOCK_ONE(NULL);
489 return 0;
490 }
491
492 /*
493 * Refresh the PID associated with this bpf file.
494 */
495 d->bd_pid = curproc->p_pid;
496
497 s = splnet();
498 if (d->bd_state == BPF_WAITING)
499 callout_stop(&d->bd_callout);
500 d->bd_state = BPF_IDLE;
501 if (d->bd_bif)
502 bpf_detachd(d);
503 splx(s);
504 bpf_freed(d);
505 LIST_REMOVE(d, bd_list);
506 fp->f_bpf = NULL;
507
508 mutex_exit(&bpf_mtx);
509 KERNEL_UNLOCK_ONE(NULL);
510
511 callout_destroy(&d->bd_callout);
512 seldestroy(&d->bd_sel);
513 softint_disestablish(d->bd_sih);
514 kmem_free(d, sizeof(*d));
515
516 return (0);
517 }
518
519 /*
520 * Rotate the packet buffers in descriptor d. Move the store buffer
521 * into the hold slot, and the free buffer into the store slot.
522 * Zero the length of the new store buffer.
523 */
524 #define ROTATE_BUFFERS(d) \
525 (d)->bd_hbuf = (d)->bd_sbuf; \
526 (d)->bd_hlen = (d)->bd_slen; \
527 (d)->bd_sbuf = (d)->bd_fbuf; \
528 (d)->bd_slen = 0; \
529 (d)->bd_fbuf = NULL;
530 /*
531 * bpfread - read next chunk of packets from buffers
532 */
533 static int
534 bpf_read(struct file *fp, off_t *offp, struct uio *uio,
535 kauth_cred_t cred, int flags)
536 {
537 struct bpf_d *d = fp->f_bpf;
538 int timed_out;
539 int error;
540 int s;
541
542 getnanotime(&d->bd_atime);
543 /*
544 * Restrict application to use a buffer the same size as
545 * the kernel buffers.
546 */
547 if (uio->uio_resid != d->bd_bufsize)
548 return (EINVAL);
549
550 KERNEL_LOCK(1, NULL);
551 s = splnet();
552 if (d->bd_state == BPF_WAITING)
553 callout_stop(&d->bd_callout);
554 timed_out = (d->bd_state == BPF_TIMED_OUT);
555 d->bd_state = BPF_IDLE;
556 /*
557 * If the hold buffer is empty, then do a timed sleep, which
558 * ends when the timeout expires or when enough packets
559 * have arrived to fill the store buffer.
560 */
561 while (d->bd_hbuf == NULL) {
562 if (fp->f_flag & FNONBLOCK) {
563 if (d->bd_slen == 0) {
564 splx(s);
565 KERNEL_UNLOCK_ONE(NULL);
566 return (EWOULDBLOCK);
567 }
568 ROTATE_BUFFERS(d);
569 break;
570 }
571
572 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
573 /*
574 * A packet(s) either arrived since the previous
575 * read or arrived while we were asleep.
576 * Rotate the buffers and return what's here.
577 */
578 ROTATE_BUFFERS(d);
579 break;
580 }
581 error = tsleep(d, PRINET|PCATCH, "bpf",
582 d->bd_rtout);
583 if (error == EINTR || error == ERESTART) {
584 splx(s);
585 KERNEL_UNLOCK_ONE(NULL);
586 return (error);
587 }
588 if (error == EWOULDBLOCK) {
589 /*
590 * On a timeout, return what's in the buffer,
591 * which may be nothing. If there is something
592 * in the store buffer, we can rotate the buffers.
593 */
594 if (d->bd_hbuf)
595 /*
596 * We filled up the buffer in between
597 * getting the timeout and arriving
598 * here, so we don't need to rotate.
599 */
600 break;
601
602 if (d->bd_slen == 0) {
603 splx(s);
604 KERNEL_UNLOCK_ONE(NULL);
605 return (0);
606 }
607 ROTATE_BUFFERS(d);
608 break;
609 }
610 if (error != 0)
611 goto done;
612 }
613 /*
614 * At this point, we know we have something in the hold slot.
615 */
616 splx(s);
617
618 /*
619 * Move data from hold buffer into user space.
620 * We know the entire buffer is transferred since
621 * we checked above that the read buffer is bpf_bufsize bytes.
622 */
623 error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
624
625 s = splnet();
626 d->bd_fbuf = d->bd_hbuf;
627 d->bd_hbuf = NULL;
628 d->bd_hlen = 0;
629 done:
630 splx(s);
631 KERNEL_UNLOCK_ONE(NULL);
632 return (error);
633 }
634
635
636 /*
637 * If there are processes sleeping on this descriptor, wake them up.
638 */
639 static inline void
640 bpf_wakeup(struct bpf_d *d)
641 {
642 wakeup(d);
643 if (d->bd_async)
644 softint_schedule(d->bd_sih);
645 selnotify(&d->bd_sel, 0, 0);
646 }
647
648 static void
649 bpf_softintr(void *cookie)
650 {
651 struct bpf_d *d;
652
653 d = cookie;
654 if (d->bd_async)
655 fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL);
656 }
657
658 static void
659 bpf_timed_out(void *arg)
660 {
661 struct bpf_d *d = arg;
662 int s;
663
664 s = splnet();
665 if (d->bd_state == BPF_WAITING) {
666 d->bd_state = BPF_TIMED_OUT;
667 if (d->bd_slen != 0)
668 bpf_wakeup(d);
669 }
670 splx(s);
671 }
672
673
674 static int
675 bpf_write(struct file *fp, off_t *offp, struct uio *uio,
676 kauth_cred_t cred, int flags)
677 {
678 struct bpf_d *d = fp->f_bpf;
679 struct ifnet *ifp;
680 struct mbuf *m, *mc;
681 int error, s;
682 static struct sockaddr_storage dst;
683
684 m = NULL; /* XXX gcc */
685
686 KERNEL_LOCK(1, NULL);
687
688 if (d->bd_bif == NULL) {
689 KERNEL_UNLOCK_ONE(NULL);
690 return (ENXIO);
691 }
692 getnanotime(&d->bd_mtime);
693
694 ifp = d->bd_bif->bif_ifp;
695
696 if (uio->uio_resid == 0) {
697 KERNEL_UNLOCK_ONE(NULL);
698 return (0);
699 }
700
701 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m,
702 (struct sockaddr *) &dst);
703 if (error) {
704 KERNEL_UNLOCK_ONE(NULL);
705 return (error);
706 }
707
708 if (m->m_pkthdr.len > ifp->if_mtu) {
709 KERNEL_UNLOCK_ONE(NULL);
710 m_freem(m);
711 return (EMSGSIZE);
712 }
713
714 if (d->bd_hdrcmplt)
715 dst.ss_family = pseudo_AF_HDRCMPLT;
716
717 if (d->bd_feedback) {
718 mc = m_dup(m, 0, M_COPYALL, M_NOWAIT);
719 if (mc != NULL)
720 m_set_rcvif(mc, ifp);
721 /* Set M_PROMISC for outgoing packets to be discarded. */
722 if (1 /*d->bd_direction == BPF_D_INOUT*/)
723 m->m_flags |= M_PROMISC;
724 } else
725 mc = NULL;
726
727 s = splsoftnet();
728 error = if_output_lock(ifp, ifp, m, (struct sockaddr *) &dst, NULL);
729
730 if (mc != NULL) {
731 if (error == 0)
732 ifp->_if_input(ifp, mc);
733 else
734 m_freem(mc);
735 }
736 splx(s);
737 KERNEL_UNLOCK_ONE(NULL);
738 /*
739 * The driver frees the mbuf.
740 */
741 return (error);
742 }
743
744 /*
745 * Reset a descriptor by flushing its packet buffer and clearing the
746 * receive and drop counts. Should be called at splnet.
747 */
748 static void
749 reset_d(struct bpf_d *d)
750 {
751 if (d->bd_hbuf) {
752 /* Free the hold buffer. */
753 d->bd_fbuf = d->bd_hbuf;
754 d->bd_hbuf = NULL;
755 }
756 d->bd_slen = 0;
757 d->bd_hlen = 0;
758 d->bd_rcount = 0;
759 d->bd_dcount = 0;
760 d->bd_ccount = 0;
761 }
762
763 /*
764 * FIONREAD Check for read packet available.
765 * BIOCGBLEN Get buffer len [for read()].
766 * BIOCSETF Set ethernet read filter.
767 * BIOCFLUSH Flush read packet buffer.
768 * BIOCPROMISC Put interface into promiscuous mode.
769 * BIOCGDLT Get link layer type.
770 * BIOCGETIF Get interface name.
771 * BIOCSETIF Set interface.
772 * BIOCSRTIMEOUT Set read timeout.
773 * BIOCGRTIMEOUT Get read timeout.
774 * BIOCGSTATS Get packet stats.
775 * BIOCIMMEDIATE Set immediate mode.
776 * BIOCVERSION Get filter language version.
777 * BIOCGHDRCMPLT Get "header already complete" flag.
778 * BIOCSHDRCMPLT Set "header already complete" flag.
779 * BIOCSFEEDBACK Set packet feedback mode.
780 * BIOCGFEEDBACK Get packet feedback mode.
781 * BIOCGSEESENT Get "see sent packets" mode.
782 * BIOCSSEESENT Set "see sent packets" mode.
783 */
784 /* ARGSUSED */
785 static int
786 bpf_ioctl(struct file *fp, u_long cmd, void *addr)
787 {
788 struct bpf_d *d = fp->f_bpf;
789 int s, error = 0;
790
791 /*
792 * Refresh the PID associated with this bpf file.
793 */
794 KERNEL_LOCK(1, NULL);
795 d->bd_pid = curproc->p_pid;
796 #ifdef _LP64
797 if (curproc->p_flag & PK_32)
798 d->bd_compat32 = 1;
799 else
800 d->bd_compat32 = 0;
801 #endif
802
803 s = splnet();
804 if (d->bd_state == BPF_WAITING)
805 callout_stop(&d->bd_callout);
806 d->bd_state = BPF_IDLE;
807 splx(s);
808
809 switch (cmd) {
810
811 default:
812 error = EINVAL;
813 break;
814
815 /*
816 * Check for read packet available.
817 */
818 case FIONREAD:
819 {
820 int n;
821
822 s = splnet();
823 n = d->bd_slen;
824 if (d->bd_hbuf)
825 n += d->bd_hlen;
826 splx(s);
827
828 *(int *)addr = n;
829 break;
830 }
831
832 /*
833 * Get buffer len [for read()].
834 */
835 case BIOCGBLEN:
836 *(u_int *)addr = d->bd_bufsize;
837 break;
838
839 /*
840 * Set buffer length.
841 */
842 case BIOCSBLEN:
843 /*
844 * Forbid to change the buffer length if buffers are already
845 * allocated.
846 */
847 if (d->bd_bif != NULL || d->bd_sbuf != NULL)
848 error = EINVAL;
849 else {
850 u_int size = *(u_int *)addr;
851
852 if (size > bpf_maxbufsize)
853 *(u_int *)addr = size = bpf_maxbufsize;
854 else if (size < BPF_MINBUFSIZE)
855 *(u_int *)addr = size = BPF_MINBUFSIZE;
856 d->bd_bufsize = size;
857 }
858 break;
859
860 /*
861 * Set link layer read filter.
862 */
863 case BIOCSETF:
864 error = bpf_setf(d, addr);
865 break;
866
867 /*
868 * Flush read packet buffer.
869 */
870 case BIOCFLUSH:
871 s = splnet();
872 reset_d(d);
873 splx(s);
874 break;
875
876 /*
877 * Put interface into promiscuous mode.
878 */
879 case BIOCPROMISC:
880 if (d->bd_bif == NULL) {
881 /*
882 * No interface attached yet.
883 */
884 error = EINVAL;
885 break;
886 }
887 s = splnet();
888 if (d->bd_promisc == 0) {
889 error = ifpromisc(d->bd_bif->bif_ifp, 1);
890 if (error == 0)
891 d->bd_promisc = 1;
892 }
893 splx(s);
894 break;
895
896 /*
897 * Get device parameters.
898 */
899 case BIOCGDLT:
900 if (d->bd_bif == NULL)
901 error = EINVAL;
902 else
903 *(u_int *)addr = d->bd_bif->bif_dlt;
904 break;
905
906 /*
907 * Get a list of supported device parameters.
908 */
909 case BIOCGDLTLIST:
910 if (d->bd_bif == NULL)
911 error = EINVAL;
912 else
913 error = bpf_getdltlist(d, addr);
914 break;
915
916 /*
917 * Set device parameters.
918 */
919 case BIOCSDLT:
920 mutex_enter(&bpf_mtx);
921 if (d->bd_bif == NULL)
922 error = EINVAL;
923 else
924 error = bpf_setdlt(d, *(u_int *)addr);
925 mutex_exit(&bpf_mtx);
926 break;
927
928 /*
929 * Set interface name.
930 */
931 #ifdef OBIOCGETIF
932 case OBIOCGETIF:
933 #endif
934 case BIOCGETIF:
935 if (d->bd_bif == NULL)
936 error = EINVAL;
937 else
938 bpf_ifname(d->bd_bif->bif_ifp, addr);
939 break;
940
941 /*
942 * Set interface.
943 */
944 #ifdef OBIOCSETIF
945 case OBIOCSETIF:
946 #endif
947 case BIOCSETIF:
948 mutex_enter(&bpf_mtx);
949 error = bpf_setif(d, addr);
950 mutex_exit(&bpf_mtx);
951 break;
952
953 /*
954 * Set read timeout.
955 */
956 case BIOCSRTIMEOUT:
957 {
958 struct timeval *tv = addr;
959
960 /* Compute number of ticks. */
961 d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
962 if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
963 d->bd_rtout = 1;
964 break;
965 }
966
967 #ifdef BIOCGORTIMEOUT
968 /*
969 * Get read timeout.
970 */
971 case BIOCGORTIMEOUT:
972 {
973 struct timeval50 *tv = addr;
974
975 tv->tv_sec = d->bd_rtout / hz;
976 tv->tv_usec = (d->bd_rtout % hz) * tick;
977 break;
978 }
979 #endif
980
981 #ifdef BIOCSORTIMEOUT
982 /*
983 * Set read timeout.
984 */
985 case BIOCSORTIMEOUT:
986 {
987 struct timeval50 *tv = addr;
988
989 /* Compute number of ticks. */
990 d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
991 if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
992 d->bd_rtout = 1;
993 break;
994 }
995 #endif
996
997 /*
998 * Get read timeout.
999 */
1000 case BIOCGRTIMEOUT:
1001 {
1002 struct timeval *tv = addr;
1003
1004 tv->tv_sec = d->bd_rtout / hz;
1005 tv->tv_usec = (d->bd_rtout % hz) * tick;
1006 break;
1007 }
1008 /*
1009 * Get packet stats.
1010 */
1011 case BIOCGSTATS:
1012 {
1013 struct bpf_stat *bs = addr;
1014
1015 bs->bs_recv = d->bd_rcount;
1016 bs->bs_drop = d->bd_dcount;
1017 bs->bs_capt = d->bd_ccount;
1018 break;
1019 }
1020
1021 case BIOCGSTATSOLD:
1022 {
1023 struct bpf_stat_old *bs = addr;
1024
1025 bs->bs_recv = d->bd_rcount;
1026 bs->bs_drop = d->bd_dcount;
1027 break;
1028 }
1029
1030 /*
1031 * Set immediate mode.
1032 */
1033 case BIOCIMMEDIATE:
1034 d->bd_immediate = *(u_int *)addr;
1035 break;
1036
1037 case BIOCVERSION:
1038 {
1039 struct bpf_version *bv = addr;
1040
1041 bv->bv_major = BPF_MAJOR_VERSION;
1042 bv->bv_minor = BPF_MINOR_VERSION;
1043 break;
1044 }
1045
1046 case BIOCGHDRCMPLT: /* get "header already complete" flag */
1047 *(u_int *)addr = d->bd_hdrcmplt;
1048 break;
1049
1050 case BIOCSHDRCMPLT: /* set "header already complete" flag */
1051 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1052 break;
1053
1054 /*
1055 * Get "see sent packets" flag
1056 */
1057 case BIOCGSEESENT:
1058 *(u_int *)addr = d->bd_seesent;
1059 break;
1060
1061 /*
1062 * Set "see sent" packets flag
1063 */
1064 case BIOCSSEESENT:
1065 d->bd_seesent = *(u_int *)addr;
1066 break;
1067
1068 /*
1069 * Set "feed packets from bpf back to input" mode
1070 */
1071 case BIOCSFEEDBACK:
1072 d->bd_feedback = *(u_int *)addr;
1073 break;
1074
1075 /*
1076 * Get "feed packets from bpf back to input" mode
1077 */
1078 case BIOCGFEEDBACK:
1079 *(u_int *)addr = d->bd_feedback;
1080 break;
1081
1082 case FIONBIO: /* Non-blocking I/O */
1083 /*
1084 * No need to do anything special as we use IO_NDELAY in
1085 * bpfread() as an indication of whether or not to block
1086 * the read.
1087 */
1088 break;
1089
1090 case FIOASYNC: /* Send signal on receive packets */
1091 d->bd_async = *(int *)addr;
1092 break;
1093
1094 case TIOCSPGRP: /* Process or group to send signals to */
1095 case FIOSETOWN:
1096 error = fsetown(&d->bd_pgid, cmd, addr);
1097 break;
1098
1099 case TIOCGPGRP:
1100 case FIOGETOWN:
1101 error = fgetown(d->bd_pgid, cmd, addr);
1102 break;
1103 }
1104 KERNEL_UNLOCK_ONE(NULL);
1105 return (error);
1106 }
1107
1108 /*
1109 * Set d's packet filter program to fp. If this file already has a filter,
1110 * free it and replace it. Returns EINVAL for bogus requests.
1111 */
1112 static int
1113 bpf_setf(struct bpf_d *d, struct bpf_program *fp)
1114 {
1115 struct bpf_insn *fcode, *old;
1116 bpfjit_func_t jcode, oldj;
1117 size_t flen, size = 0, old_size;
1118 int s;
1119
1120 jcode = NULL;
1121 flen = fp->bf_len;
1122
1123 if ((fp->bf_insns == NULL && flen) || flen > BPF_MAXINSNS) {
1124 return EINVAL;
1125 }
1126
1127 if (flen) {
1128 /*
1129 * Allocate the buffer, copy the byte-code from
1130 * userspace and validate it.
1131 */
1132 size = flen * sizeof(*fp->bf_insns);
1133 fcode = kmem_alloc(size, KM_SLEEP);
1134 if (copyin(fp->bf_insns, fcode, size) != 0 ||
1135 !bpf_validate(fcode, (int)flen)) {
1136 kmem_free(fcode, size);
1137 return EINVAL;
1138 }
1139 membar_consumer();
1140 if (bpf_jit)
1141 jcode = bpf_jit_generate(NULL, fcode, flen);
1142 } else {
1143 fcode = NULL;
1144 }
1145
1146 old_size = d->bd_filter_size;
1147
1148 s = splnet();
1149 old = d->bd_filter;
1150 d->bd_filter = fcode;
1151 d->bd_filter_size = size;
1152 oldj = d->bd_jitcode;
1153 d->bd_jitcode = jcode;
1154 reset_d(d);
1155 splx(s);
1156
1157 if (old) {
1158 kmem_free(old, old_size);
1159 }
1160 if (oldj) {
1161 bpf_jit_freecode(oldj);
1162 }
1163
1164 return 0;
1165 }
1166
1167 /*
1168 * Detach a file from its current interface (if attached at all) and attach
1169 * to the interface indicated by the name stored in ifr.
1170 * Return an errno or 0.
1171 */
1172 static int
1173 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1174 {
1175 struct bpf_if *bp;
1176 char *cp;
1177 int unit_seen, i, s, error;
1178
1179 KASSERT(mutex_owned(&bpf_mtx));
1180 /*
1181 * Make sure the provided name has a unit number, and default
1182 * it to '0' if not specified.
1183 * XXX This is ugly ... do this differently?
1184 */
1185 unit_seen = 0;
1186 cp = ifr->ifr_name;
1187 cp[sizeof(ifr->ifr_name) - 1] = '\0'; /* sanity */
1188 while (*cp++)
1189 if (*cp >= '0' && *cp <= '9')
1190 unit_seen = 1;
1191 if (!unit_seen) {
1192 /* Make sure to leave room for the '\0'. */
1193 for (i = 0; i < (IFNAMSIZ - 1); ++i) {
1194 if ((ifr->ifr_name[i] >= 'a' &&
1195 ifr->ifr_name[i] <= 'z') ||
1196 (ifr->ifr_name[i] >= 'A' &&
1197 ifr->ifr_name[i] <= 'Z'))
1198 continue;
1199 ifr->ifr_name[i] = '0';
1200 }
1201 }
1202
1203 /*
1204 * Look through attached interfaces for the named one.
1205 */
1206 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1207 struct ifnet *ifp = bp->bif_ifp;
1208
1209 if (ifp == NULL ||
1210 strcmp(ifp->if_xname, ifr->ifr_name) != 0)
1211 continue;
1212 /* skip additional entry */
1213 if (bp->bif_driverp != &ifp->if_bpf)
1214 continue;
1215 /*
1216 * We found the requested interface.
1217 * Allocate the packet buffers if we need to.
1218 * If we're already attached to requested interface,
1219 * just flush the buffer.
1220 */
1221 if (d->bd_sbuf == NULL) {
1222 error = bpf_allocbufs(d);
1223 if (error != 0)
1224 return (error);
1225 }
1226 s = splnet();
1227 if (bp != d->bd_bif) {
1228 if (d->bd_bif)
1229 /*
1230 * Detach if attached to something else.
1231 */
1232 bpf_detachd(d);
1233
1234 bpf_attachd(d, bp);
1235 }
1236 reset_d(d);
1237 splx(s);
1238 return (0);
1239 }
1240 /* Not found. */
1241 return (ENXIO);
1242 }
1243
1244 /*
1245 * Copy the interface name to the ifreq.
1246 */
1247 static void
1248 bpf_ifname(struct ifnet *ifp, struct ifreq *ifr)
1249 {
1250 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
1251 }
1252
1253 static int
1254 bpf_stat(struct file *fp, struct stat *st)
1255 {
1256 struct bpf_d *d = fp->f_bpf;
1257
1258 (void)memset(st, 0, sizeof(*st));
1259 KERNEL_LOCK(1, NULL);
1260 st->st_dev = makedev(cdevsw_lookup_major(&bpf_cdevsw), d->bd_pid);
1261 st->st_atimespec = d->bd_atime;
1262 st->st_mtimespec = d->bd_mtime;
1263 st->st_ctimespec = st->st_birthtimespec = d->bd_btime;
1264 st->st_uid = kauth_cred_geteuid(fp->f_cred);
1265 st->st_gid = kauth_cred_getegid(fp->f_cred);
1266 st->st_mode = S_IFCHR;
1267 KERNEL_UNLOCK_ONE(NULL);
1268 return 0;
1269 }
1270
1271 /*
1272 * Support for poll() system call
1273 *
1274 * Return true iff the specific operation will not block indefinitely - with
1275 * the assumption that it is safe to positively acknowledge a request for the
1276 * ability to write to the BPF device.
1277 * Otherwise, return false but make a note that a selnotify() must be done.
1278 */
1279 static int
1280 bpf_poll(struct file *fp, int events)
1281 {
1282 struct bpf_d *d = fp->f_bpf;
1283 int s = splnet();
1284 int revents;
1285
1286 /*
1287 * Refresh the PID associated with this bpf file.
1288 */
1289 KERNEL_LOCK(1, NULL);
1290 d->bd_pid = curproc->p_pid;
1291
1292 revents = events & (POLLOUT | POLLWRNORM);
1293 if (events & (POLLIN | POLLRDNORM)) {
1294 /*
1295 * An imitation of the FIONREAD ioctl code.
1296 */
1297 if (d->bd_hlen != 0 ||
1298 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1299 d->bd_slen != 0)) {
1300 revents |= events & (POLLIN | POLLRDNORM);
1301 } else {
1302 selrecord(curlwp, &d->bd_sel);
1303 /* Start the read timeout if necessary */
1304 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1305 callout_reset(&d->bd_callout, d->bd_rtout,
1306 bpf_timed_out, d);
1307 d->bd_state = BPF_WAITING;
1308 }
1309 }
1310 }
1311
1312 KERNEL_UNLOCK_ONE(NULL);
1313 splx(s);
1314 return (revents);
1315 }
1316
1317 static void
1318 filt_bpfrdetach(struct knote *kn)
1319 {
1320 struct bpf_d *d = kn->kn_hook;
1321 int s;
1322
1323 KERNEL_LOCK(1, NULL);
1324 s = splnet();
1325 SLIST_REMOVE(&d->bd_sel.sel_klist, kn, knote, kn_selnext);
1326 splx(s);
1327 KERNEL_UNLOCK_ONE(NULL);
1328 }
1329
1330 static int
1331 filt_bpfread(struct knote *kn, long hint)
1332 {
1333 struct bpf_d *d = kn->kn_hook;
1334 int rv;
1335
1336 KERNEL_LOCK(1, NULL);
1337 kn->kn_data = d->bd_hlen;
1338 if (d->bd_immediate)
1339 kn->kn_data += d->bd_slen;
1340 rv = (kn->kn_data > 0);
1341 KERNEL_UNLOCK_ONE(NULL);
1342 return rv;
1343 }
1344
1345 static const struct filterops bpfread_filtops =
1346 { 1, NULL, filt_bpfrdetach, filt_bpfread };
1347
1348 static int
1349 bpf_kqfilter(struct file *fp, struct knote *kn)
1350 {
1351 struct bpf_d *d = fp->f_bpf;
1352 struct klist *klist;
1353 int s;
1354
1355 KERNEL_LOCK(1, NULL);
1356
1357 switch (kn->kn_filter) {
1358 case EVFILT_READ:
1359 klist = &d->bd_sel.sel_klist;
1360 kn->kn_fop = &bpfread_filtops;
1361 break;
1362
1363 default:
1364 KERNEL_UNLOCK_ONE(NULL);
1365 return (EINVAL);
1366 }
1367
1368 kn->kn_hook = d;
1369
1370 s = splnet();
1371 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1372 splx(s);
1373 KERNEL_UNLOCK_ONE(NULL);
1374
1375 return (0);
1376 }
1377
1378 /*
1379 * Copy data from an mbuf chain into a buffer. This code is derived
1380 * from m_copydata in sys/uipc_mbuf.c.
1381 */
1382 static void *
1383 bpf_mcpy(void *dst_arg, const void *src_arg, size_t len)
1384 {
1385 const struct mbuf *m;
1386 u_int count;
1387 u_char *dst;
1388
1389 m = src_arg;
1390 dst = dst_arg;
1391 while (len > 0) {
1392 if (m == NULL)
1393 panic("bpf_mcpy");
1394 count = min(m->m_len, len);
1395 memcpy(dst, mtod(m, const void *), count);
1396 m = m->m_next;
1397 dst += count;
1398 len -= count;
1399 }
1400 return dst_arg;
1401 }
1402
1403 /*
1404 * Dispatch a packet to all the listeners on interface bp.
1405 *
1406 * pkt pointer to the packet, either a data buffer or an mbuf chain
1407 * buflen buffer length, if pkt is a data buffer
1408 * cpfn a function that can copy pkt into the listener's buffer
1409 * pktlen length of the packet
1410 * rcv true if packet came in
1411 */
1412 static inline void
1413 bpf_deliver(struct bpf_if *bp, void *(*cpfn)(void *, const void *, size_t),
1414 void *pkt, u_int pktlen, u_int buflen, const bool rcv)
1415 {
1416 uint32_t mem[BPF_MEMWORDS];
1417 bpf_args_t args = {
1418 .pkt = (const uint8_t *)pkt,
1419 .wirelen = pktlen,
1420 .buflen = buflen,
1421 .mem = mem,
1422 .arg = NULL
1423 };
1424 bool gottime = false;
1425 struct timespec ts;
1426
1427 /*
1428 * Note that the IPL does not have to be raised at this point.
1429 * The only problem that could arise here is that if two different
1430 * interfaces shared any data. This is not the case.
1431 */
1432 for (struct bpf_d *d = bp->bif_dlist; d != NULL; d = d->bd_next) {
1433 u_int slen;
1434
1435 if (!d->bd_seesent && !rcv) {
1436 continue;
1437 }
1438 d->bd_rcount++;
1439 bpf_gstats.bs_recv++;
1440
1441 if (d->bd_jitcode)
1442 slen = d->bd_jitcode(NULL, &args);
1443 else
1444 slen = bpf_filter_ext(NULL, d->bd_filter, &args);
1445
1446 if (!slen) {
1447 continue;
1448 }
1449 if (!gottime) {
1450 gottime = true;
1451 nanotime(&ts);
1452 }
1453 catchpacket(d, pkt, pktlen, slen, cpfn, &ts);
1454 }
1455 }
1456
1457 /*
1458 * Incoming linkage from device drivers. Process the packet pkt, of length
1459 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1460 * by each process' filter, and if accepted, stashed into the corresponding
1461 * buffer.
1462 */
1463 static void
1464 _bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1465 {
1466
1467 bpf_deliver(bp, memcpy, pkt, pktlen, pktlen, true);
1468 }
1469
1470 /*
1471 * Incoming linkage from device drivers, when the head of the packet is in
1472 * a buffer, and the tail is in an mbuf chain.
1473 */
1474 static void
1475 _bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1476 {
1477 u_int pktlen;
1478 struct mbuf mb;
1479
1480 /* Skip outgoing duplicate packets. */
1481 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif_index == 0) {
1482 m->m_flags &= ~M_PROMISC;
1483 return;
1484 }
1485
1486 pktlen = m_length(m) + dlen;
1487
1488 /*
1489 * Craft on-stack mbuf suitable for passing to bpf_filter.
1490 * Note that we cut corners here; we only setup what's
1491 * absolutely needed--this mbuf should never go anywhere else.
1492 */
1493 (void)memset(&mb, 0, sizeof(mb));
1494 mb.m_next = m;
1495 mb.m_data = data;
1496 mb.m_len = dlen;
1497
1498 bpf_deliver(bp, bpf_mcpy, &mb, pktlen, 0, m->m_pkthdr.rcvif_index != 0);
1499 }
1500
1501 /*
1502 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1503 */
1504 static void
1505 _bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1506 {
1507 void *(*cpfn)(void *, const void *, size_t);
1508 u_int pktlen, buflen;
1509 void *marg;
1510
1511 /* Skip outgoing duplicate packets. */
1512 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif_index == 0) {
1513 m->m_flags &= ~M_PROMISC;
1514 return;
1515 }
1516
1517 pktlen = m_length(m);
1518
1519 if (pktlen == m->m_len) {
1520 cpfn = (void *)memcpy;
1521 marg = mtod(m, void *);
1522 buflen = pktlen;
1523 } else {
1524 cpfn = bpf_mcpy;
1525 marg = m;
1526 buflen = 0;
1527 }
1528
1529 bpf_deliver(bp, cpfn, marg, pktlen, buflen, m->m_pkthdr.rcvif_index != 0);
1530 }
1531
1532 /*
1533 * We need to prepend the address family as
1534 * a four byte field. Cons up a dummy header
1535 * to pacify bpf. This is safe because bpf
1536 * will only read from the mbuf (i.e., it won't
1537 * try to free it or keep a pointer a to it).
1538 */
1539 static void
1540 _bpf_mtap_af(struct bpf_if *bp, uint32_t af, struct mbuf *m)
1541 {
1542 struct mbuf m0;
1543
1544 m0.m_flags = 0;
1545 m0.m_next = m;
1546 m0.m_len = 4;
1547 m0.m_data = (char *)⁡
1548
1549 _bpf_mtap(bp, &m0);
1550 }
1551
1552 /*
1553 * Put the SLIP pseudo-"link header" in place.
1554 * Note this M_PREPEND() should never fail,
1555 * swince we know we always have enough space
1556 * in the input buffer.
1557 */
1558 static void
1559 _bpf_mtap_sl_in(struct bpf_if *bp, u_char *chdr, struct mbuf **m)
1560 {
1561 int s;
1562 u_char *hp;
1563
1564 M_PREPEND(*m, SLIP_HDRLEN, M_DONTWAIT);
1565 if (*m == NULL)
1566 return;
1567
1568 hp = mtod(*m, u_char *);
1569 hp[SLX_DIR] = SLIPDIR_IN;
1570 (void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
1571
1572 s = splnet();
1573 _bpf_mtap(bp, *m);
1574 splx(s);
1575
1576 m_adj(*m, SLIP_HDRLEN);
1577 }
1578
1579 /*
1580 * Put the SLIP pseudo-"link header" in
1581 * place. The compressed header is now
1582 * at the beginning of the mbuf.
1583 */
1584 static void
1585 _bpf_mtap_sl_out(struct bpf_if *bp, u_char *chdr, struct mbuf *m)
1586 {
1587 struct mbuf m0;
1588 u_char *hp;
1589 int s;
1590
1591 m0.m_flags = 0;
1592 m0.m_next = m;
1593 m0.m_data = m0.m_dat;
1594 m0.m_len = SLIP_HDRLEN;
1595
1596 hp = mtod(&m0, u_char *);
1597
1598 hp[SLX_DIR] = SLIPDIR_OUT;
1599 (void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
1600
1601 s = splnet();
1602 _bpf_mtap(bp, &m0);
1603 splx(s);
1604 m_freem(m);
1605 }
1606
1607 static struct mbuf *
1608 bpf_mbuf_enqueue(struct bpf_if *bp, struct mbuf *m)
1609 {
1610 struct mbuf *dup;
1611
1612 dup = m_dup(m, 0, M_COPYALL, M_NOWAIT);
1613 if (dup == NULL)
1614 return NULL;
1615
1616 if (bp->bif_mbuf_tail != NULL) {
1617 bp->bif_mbuf_tail->m_nextpkt = dup;
1618 } else {
1619 bp->bif_mbuf_head = dup;
1620 }
1621 bp->bif_mbuf_tail = dup;
1622 #ifdef BPF_MTAP_SOFTINT_DEBUG
1623 log(LOG_DEBUG, "%s: enqueued mbuf=%p to %s\n",
1624 __func__, dup, bp->bif_ifp->if_xname);
1625 #endif
1626
1627 return dup;
1628 }
1629
1630 static struct mbuf *
1631 bpf_mbuf_dequeue(struct bpf_if *bp)
1632 {
1633 struct mbuf *m;
1634 int s;
1635
1636 s = splnet();
1637 m = bp->bif_mbuf_head;
1638 if (m != NULL) {
1639 bp->bif_mbuf_head = m->m_nextpkt;
1640 m->m_nextpkt = NULL;
1641
1642 if (bp->bif_mbuf_head == NULL)
1643 bp->bif_mbuf_tail = NULL;
1644 #ifdef BPF_MTAP_SOFTINT_DEBUG
1645 log(LOG_DEBUG, "%s: dequeued mbuf=%p from %s\n",
1646 __func__, m, bp->bif_ifp->if_xname);
1647 #endif
1648 }
1649 splx(s);
1650
1651 return m;
1652 }
1653
1654 static void
1655 bpf_mtap_si(void *arg)
1656 {
1657 struct bpf_if *bp = arg;
1658 struct mbuf *m;
1659
1660 while ((m = bpf_mbuf_dequeue(bp)) != NULL) {
1661 #ifdef BPF_MTAP_SOFTINT_DEBUG
1662 log(LOG_DEBUG, "%s: tapping mbuf=%p on %s\n",
1663 __func__, m, bp->bif_ifp->if_xname);
1664 #endif
1665 #ifndef NET_MPSAFE
1666 KERNEL_LOCK(1, NULL);
1667 #endif
1668 bpf_ops->bpf_mtap(bp, m);
1669 #ifndef NET_MPSAFE
1670 KERNEL_UNLOCK_ONE(NULL);
1671 #endif
1672 m_freem(m);
1673 }
1674 }
1675
1676 static void
1677 _bpf_mtap_softint(struct ifnet *ifp, struct mbuf *m)
1678 {
1679 struct bpf_if *bp = ifp->if_bpf;
1680 struct mbuf *dup;
1681
1682 KASSERT(cpu_intr_p());
1683
1684 /* To avoid extra invocations of the softint */
1685 if (bp->bif_dlist == NULL)
1686 return;
1687 KASSERT(bp->bif_si != NULL);
1688
1689 dup = bpf_mbuf_enqueue(bp, m);
1690 if (dup != NULL)
1691 softint_schedule(bp->bif_si);
1692 }
1693
1694 static int
1695 bpf_hdrlen(struct bpf_d *d)
1696 {
1697 int hdrlen = d->bd_bif->bif_hdrlen;
1698 /*
1699 * Compute the length of the bpf header. This is not necessarily
1700 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1701 * that the network layer header begins on a longword boundary (for
1702 * performance reasons and to alleviate alignment restrictions).
1703 */
1704 #ifdef _LP64
1705 if (d->bd_compat32)
1706 return (BPF_WORDALIGN32(hdrlen + SIZEOF_BPF_HDR32) - hdrlen);
1707 else
1708 #endif
1709 return (BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen);
1710 }
1711
1712 /*
1713 * Move the packet data from interface memory (pkt) into the
1714 * store buffer. Call the wakeup functions if it's time to wakeup
1715 * a listener (buffer full), "cpfn" is the routine called to do the
1716 * actual data transfer. memcpy is passed in to copy contiguous chunks,
1717 * while bpf_mcpy is passed in to copy mbuf chains. In the latter case,
1718 * pkt is really an mbuf.
1719 */
1720 static void
1721 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1722 void *(*cpfn)(void *, const void *, size_t), struct timespec *ts)
1723 {
1724 char *h;
1725 int totlen, curlen, caplen;
1726 int hdrlen = bpf_hdrlen(d);
1727 int do_wakeup = 0;
1728
1729 ++d->bd_ccount;
1730 ++bpf_gstats.bs_capt;
1731 /*
1732 * Figure out how many bytes to move. If the packet is
1733 * greater or equal to the snapshot length, transfer that
1734 * much. Otherwise, transfer the whole packet (unless
1735 * we hit the buffer size limit).
1736 */
1737 totlen = hdrlen + min(snaplen, pktlen);
1738 if (totlen > d->bd_bufsize)
1739 totlen = d->bd_bufsize;
1740 /*
1741 * If we adjusted totlen to fit the bufsize, it could be that
1742 * totlen is smaller than hdrlen because of the link layer header.
1743 */
1744 caplen = totlen - hdrlen;
1745 if (caplen < 0)
1746 caplen = 0;
1747
1748 /*
1749 * Round up the end of the previous packet to the next longword.
1750 */
1751 #ifdef _LP64
1752 if (d->bd_compat32)
1753 curlen = BPF_WORDALIGN32(d->bd_slen);
1754 else
1755 #endif
1756 curlen = BPF_WORDALIGN(d->bd_slen);
1757 if (curlen + totlen > d->bd_bufsize) {
1758 /*
1759 * This packet will overflow the storage buffer.
1760 * Rotate the buffers if we can, then wakeup any
1761 * pending reads.
1762 */
1763 if (d->bd_fbuf == NULL) {
1764 /*
1765 * We haven't completed the previous read yet,
1766 * so drop the packet.
1767 */
1768 ++d->bd_dcount;
1769 ++bpf_gstats.bs_drop;
1770 return;
1771 }
1772 ROTATE_BUFFERS(d);
1773 do_wakeup = 1;
1774 curlen = 0;
1775 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) {
1776 /*
1777 * Immediate mode is set, or the read timeout has
1778 * already expired during a select call. A packet
1779 * arrived, so the reader should be woken up.
1780 */
1781 do_wakeup = 1;
1782 }
1783
1784 /*
1785 * Append the bpf header.
1786 */
1787 h = (char *)d->bd_sbuf + curlen;
1788 #ifdef _LP64
1789 if (d->bd_compat32) {
1790 struct bpf_hdr32 *hp32;
1791
1792 hp32 = (struct bpf_hdr32 *)h;
1793 hp32->bh_tstamp.tv_sec = ts->tv_sec;
1794 hp32->bh_tstamp.tv_usec = ts->tv_nsec / 1000;
1795 hp32->bh_datalen = pktlen;
1796 hp32->bh_hdrlen = hdrlen;
1797 hp32->bh_caplen = caplen;
1798 } else
1799 #endif
1800 {
1801 struct bpf_hdr *hp;
1802
1803 hp = (struct bpf_hdr *)h;
1804 hp->bh_tstamp.tv_sec = ts->tv_sec;
1805 hp->bh_tstamp.tv_usec = ts->tv_nsec / 1000;
1806 hp->bh_datalen = pktlen;
1807 hp->bh_hdrlen = hdrlen;
1808 hp->bh_caplen = caplen;
1809 }
1810
1811 /*
1812 * Copy the packet data into the store buffer and update its length.
1813 */
1814 (*cpfn)(h + hdrlen, pkt, caplen);
1815 d->bd_slen = curlen + totlen;
1816
1817 /*
1818 * Call bpf_wakeup after bd_slen has been updated so that kevent(2)
1819 * will cause filt_bpfread() to be called with it adjusted.
1820 */
1821 if (do_wakeup)
1822 bpf_wakeup(d);
1823 }
1824
1825 /*
1826 * Initialize all nonzero fields of a descriptor.
1827 */
1828 static int
1829 bpf_allocbufs(struct bpf_d *d)
1830 {
1831
1832 d->bd_fbuf = kmem_alloc(d->bd_bufsize, KM_NOSLEEP);
1833 if (!d->bd_fbuf)
1834 return (ENOBUFS);
1835 d->bd_sbuf = kmem_alloc(d->bd_bufsize, KM_NOSLEEP);
1836 if (!d->bd_sbuf) {
1837 kmem_free(d->bd_fbuf, d->bd_bufsize);
1838 return (ENOBUFS);
1839 }
1840 d->bd_slen = 0;
1841 d->bd_hlen = 0;
1842 return (0);
1843 }
1844
1845 /*
1846 * Free buffers currently in use by a descriptor.
1847 * Called on close.
1848 */
1849 static void
1850 bpf_freed(struct bpf_d *d)
1851 {
1852 /*
1853 * We don't need to lock out interrupts since this descriptor has
1854 * been detached from its interface and it yet hasn't been marked
1855 * free.
1856 */
1857 if (d->bd_sbuf != NULL) {
1858 kmem_free(d->bd_sbuf, d->bd_bufsize);
1859 if (d->bd_hbuf != NULL)
1860 kmem_free(d->bd_hbuf, d->bd_bufsize);
1861 if (d->bd_fbuf != NULL)
1862 kmem_free(d->bd_fbuf, d->bd_bufsize);
1863 }
1864 if (d->bd_filter)
1865 kmem_free(d->bd_filter, d->bd_filter_size);
1866
1867 if (d->bd_jitcode != NULL) {
1868 bpf_jit_freecode(d->bd_jitcode);
1869 }
1870 }
1871
1872 /*
1873 * Attach an interface to bpf. dlt is the link layer type;
1874 * hdrlen is the fixed size of the link header for the specified dlt
1875 * (variable length headers not yet supported).
1876 */
1877 static void
1878 _bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1879 {
1880 struct bpf_if *bp;
1881 bp = kmem_alloc(sizeof(*bp), KM_NOSLEEP);
1882 if (bp == NULL)
1883 panic("bpfattach");
1884
1885 mutex_enter(&bpf_mtx);
1886 bp->bif_dlist = NULL;
1887 bp->bif_driverp = driverp;
1888 bp->bif_ifp = ifp;
1889 bp->bif_dlt = dlt;
1890 bp->bif_si = NULL;
1891
1892 bp->bif_next = bpf_iflist;
1893 bpf_iflist = bp;
1894
1895 *bp->bif_driverp = NULL;
1896
1897 bp->bif_hdrlen = hdrlen;
1898 mutex_exit(&bpf_mtx);
1899 #if 0
1900 printf("bpf: %s attached\n", ifp->if_xname);
1901 #endif
1902 }
1903
1904 static void
1905 _bpf_mtap_softint_init(struct ifnet *ifp)
1906 {
1907 struct bpf_if *bp;
1908
1909 mutex_enter(&bpf_mtx);
1910 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1911 if (bp->bif_ifp != ifp)
1912 continue;
1913
1914 bp->bif_mbuf_head = NULL;
1915 bp->bif_mbuf_tail = NULL;
1916 bp->bif_si = softint_establish(SOFTINT_NET, bpf_mtap_si, bp);
1917 if (bp->bif_si == NULL)
1918 panic("%s: softint_establish() failed", __func__);
1919 break;
1920 }
1921 mutex_exit(&bpf_mtx);
1922
1923 if (bp == NULL)
1924 panic("%s: no bpf_if found for %s", __func__, ifp->if_xname);
1925 }
1926
1927 /*
1928 * Remove an interface from bpf.
1929 */
1930 static void
1931 _bpfdetach(struct ifnet *ifp)
1932 {
1933 struct bpf_if *bp, **pbp;
1934 struct bpf_d *d;
1935 int s;
1936
1937 mutex_enter(&bpf_mtx);
1938 /* Nuke the vnodes for any open instances */
1939 LIST_FOREACH(d, &bpf_list, bd_list) {
1940 if (d->bd_bif != NULL && d->bd_bif->bif_ifp == ifp) {
1941 /*
1942 * Detach the descriptor from an interface now.
1943 * It will be free'ed later by close routine.
1944 */
1945 s = splnet();
1946 d->bd_promisc = 0; /* we can't touch device. */
1947 bpf_detachd(d);
1948 splx(s);
1949 }
1950 }
1951
1952 again:
1953 for (bp = bpf_iflist, pbp = &bpf_iflist;
1954 bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
1955 if (bp->bif_ifp == ifp) {
1956 *pbp = bp->bif_next;
1957 if (bp->bif_si != NULL) {
1958 s = splnet();
1959 while (bp->bif_mbuf_head != NULL) {
1960 struct mbuf *m = bp->bif_mbuf_head;
1961 bp->bif_mbuf_head = m->m_nextpkt;
1962 m_freem(m);
1963 }
1964 splx(s);
1965 softint_disestablish(bp->bif_si);
1966 }
1967 kmem_free(bp, sizeof(*bp));
1968 goto again;
1969 }
1970 }
1971 mutex_exit(&bpf_mtx);
1972 }
1973
1974 /*
1975 * Change the data link type of a interface.
1976 */
1977 static void
1978 _bpf_change_type(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1979 {
1980 struct bpf_if *bp;
1981
1982 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1983 if (bp->bif_driverp == &ifp->if_bpf)
1984 break;
1985 }
1986 if (bp == NULL)
1987 panic("bpf_change_type");
1988
1989 bp->bif_dlt = dlt;
1990
1991 bp->bif_hdrlen = hdrlen;
1992 }
1993
1994 /*
1995 * Get a list of available data link type of the interface.
1996 */
1997 static int
1998 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1999 {
2000 int n, error;
2001 struct ifnet *ifp;
2002 struct bpf_if *bp;
2003
2004 ifp = d->bd_bif->bif_ifp;
2005 n = 0;
2006 error = 0;
2007 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
2008 if (bp->bif_ifp != ifp)
2009 continue;
2010 if (bfl->bfl_list != NULL) {
2011 if (n >= bfl->bfl_len)
2012 return ENOMEM;
2013 error = copyout(&bp->bif_dlt,
2014 bfl->bfl_list + n, sizeof(u_int));
2015 }
2016 n++;
2017 }
2018 bfl->bfl_len = n;
2019 return error;
2020 }
2021
2022 /*
2023 * Set the data link type of a BPF instance.
2024 */
2025 static int
2026 bpf_setdlt(struct bpf_d *d, u_int dlt)
2027 {
2028 int s, error, opromisc;
2029 struct ifnet *ifp;
2030 struct bpf_if *bp;
2031
2032 KASSERT(mutex_owned(&bpf_mtx));
2033
2034 if (d->bd_bif->bif_dlt == dlt)
2035 return 0;
2036 ifp = d->bd_bif->bif_ifp;
2037 for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
2038 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
2039 break;
2040 }
2041 if (bp == NULL)
2042 return EINVAL;
2043 s = splnet();
2044 opromisc = d->bd_promisc;
2045 bpf_detachd(d);
2046 bpf_attachd(d, bp);
2047 reset_d(d);
2048 if (opromisc) {
2049 error = ifpromisc(bp->bif_ifp, 1);
2050 if (error)
2051 printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
2052 bp->bif_ifp->if_xname, error);
2053 else
2054 d->bd_promisc = 1;
2055 }
2056 splx(s);
2057 return 0;
2058 }
2059
2060 static int
2061 sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS)
2062 {
2063 int newsize, error;
2064 struct sysctlnode node;
2065
2066 node = *rnode;
2067 node.sysctl_data = &newsize;
2068 newsize = bpf_maxbufsize;
2069 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2070 if (error || newp == NULL)
2071 return (error);
2072
2073 if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE)
2074 return (EINVAL);
2075
2076 bpf_maxbufsize = newsize;
2077
2078 return (0);
2079 }
2080
2081 #if defined(MODULAR) || defined(BPFJIT)
2082 static int
2083 sysctl_net_bpf_jit(SYSCTLFN_ARGS)
2084 {
2085 bool newval;
2086 int error;
2087 struct sysctlnode node;
2088
2089 node = *rnode;
2090 node.sysctl_data = &newval;
2091 newval = bpf_jit;
2092 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2093 if (error != 0 || newp == NULL)
2094 return error;
2095
2096 bpf_jit = newval;
2097
2098 /*
2099 * Do a full sync to publish new bpf_jit value and
2100 * update bpfjit_module_ops.bj_generate_code variable.
2101 */
2102 membar_sync();
2103
2104 if (newval && bpfjit_module_ops.bj_generate_code == NULL) {
2105 printf("JIT compilation is postponed "
2106 "until after bpfjit module is loaded\n");
2107 }
2108
2109 return 0;
2110 }
2111 #endif
2112
2113 static int
2114 sysctl_net_bpf_peers(SYSCTLFN_ARGS)
2115 {
2116 int error, elem_count;
2117 struct bpf_d *dp;
2118 struct bpf_d_ext dpe;
2119 size_t len, needed, elem_size, out_size;
2120 char *sp;
2121
2122 if (namelen == 1 && name[0] == CTL_QUERY)
2123 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2124
2125 if (namelen != 2)
2126 return (EINVAL);
2127
2128 /* BPF peers is privileged information. */
2129 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
2130 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, NULL, NULL, NULL);
2131 if (error)
2132 return (EPERM);
2133
2134 len = (oldp != NULL) ? *oldlenp : 0;
2135 sp = oldp;
2136 elem_size = name[0];
2137 elem_count = name[1];
2138 out_size = MIN(sizeof(dpe), elem_size);
2139 needed = 0;
2140
2141 if (elem_size < 1 || elem_count < 0)
2142 return (EINVAL);
2143
2144 mutex_enter(&bpf_mtx);
2145 LIST_FOREACH(dp, &bpf_list, bd_list) {
2146 if (len >= elem_size && elem_count > 0) {
2147 #define BPF_EXT(field) dpe.bde_ ## field = dp->bd_ ## field
2148 BPF_EXT(bufsize);
2149 BPF_EXT(promisc);
2150 BPF_EXT(state);
2151 BPF_EXT(immediate);
2152 BPF_EXT(hdrcmplt);
2153 BPF_EXT(seesent);
2154 BPF_EXT(pid);
2155 BPF_EXT(rcount);
2156 BPF_EXT(dcount);
2157 BPF_EXT(ccount);
2158 #undef BPF_EXT
2159 if (dp->bd_bif)
2160 (void)strlcpy(dpe.bde_ifname,
2161 dp->bd_bif->bif_ifp->if_xname,
2162 IFNAMSIZ - 1);
2163 else
2164 dpe.bde_ifname[0] = '\0';
2165
2166 error = copyout(&dpe, sp, out_size);
2167 if (error)
2168 break;
2169 sp += elem_size;
2170 len -= elem_size;
2171 }
2172 needed += elem_size;
2173 if (elem_count > 0 && elem_count != INT_MAX)
2174 elem_count--;
2175 }
2176 mutex_exit(&bpf_mtx);
2177
2178 *oldlenp = needed;
2179
2180 return (error);
2181 }
2182
2183 static struct sysctllog *bpf_sysctllog;
2184 static void
2185 sysctl_net_bpf_setup(void)
2186 {
2187 const struct sysctlnode *node;
2188
2189 node = NULL;
2190 sysctl_createv(&bpf_sysctllog, 0, NULL, &node,
2191 CTLFLAG_PERMANENT,
2192 CTLTYPE_NODE, "bpf",
2193 SYSCTL_DESCR("BPF options"),
2194 NULL, 0, NULL, 0,
2195 CTL_NET, CTL_CREATE, CTL_EOL);
2196 if (node != NULL) {
2197 #if defined(MODULAR) || defined(BPFJIT)
2198 sysctl_createv(&bpf_sysctllog, 0, NULL, NULL,
2199 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2200 CTLTYPE_BOOL, "jit",
2201 SYSCTL_DESCR("Toggle Just-In-Time compilation"),
2202 sysctl_net_bpf_jit, 0, &bpf_jit, 0,
2203 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2204 #endif
2205 sysctl_createv(&bpf_sysctllog, 0, NULL, NULL,
2206 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2207 CTLTYPE_INT, "maxbufsize",
2208 SYSCTL_DESCR("Maximum size for data capture buffer"),
2209 sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0,
2210 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2211 sysctl_createv(&bpf_sysctllog, 0, NULL, NULL,
2212 CTLFLAG_PERMANENT,
2213 CTLTYPE_STRUCT, "stats",
2214 SYSCTL_DESCR("BPF stats"),
2215 NULL, 0, &bpf_gstats, sizeof(bpf_gstats),
2216 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2217 sysctl_createv(&bpf_sysctllog, 0, NULL, NULL,
2218 CTLFLAG_PERMANENT,
2219 CTLTYPE_STRUCT, "peers",
2220 SYSCTL_DESCR("BPF peers"),
2221 sysctl_net_bpf_peers, 0, NULL, 0,
2222 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2223 }
2224
2225 }
2226
2227 struct bpf_ops bpf_ops_kernel = {
2228 .bpf_attach = _bpfattach,
2229 .bpf_detach = _bpfdetach,
2230 .bpf_change_type = _bpf_change_type,
2231
2232 .bpf_tap = _bpf_tap,
2233 .bpf_mtap = _bpf_mtap,
2234 .bpf_mtap2 = _bpf_mtap2,
2235 .bpf_mtap_af = _bpf_mtap_af,
2236 .bpf_mtap_sl_in = _bpf_mtap_sl_in,
2237 .bpf_mtap_sl_out = _bpf_mtap_sl_out,
2238
2239 .bpf_mtap_softint = _bpf_mtap_softint,
2240 .bpf_mtap_softint_init = _bpf_mtap_softint_init,
2241 };
2242
2243 MODULE(MODULE_CLASS_DRIVER, bpf, "bpf_filter");
2244
2245 static int
2246 bpf_modcmd(modcmd_t cmd, void *arg)
2247 {
2248 #ifdef _MODULE
2249 devmajor_t bmajor, cmajor;
2250 #endif
2251 int error = 0;
2252
2253 switch (cmd) {
2254 case MODULE_CMD_INIT:
2255 bpf_init();
2256 #ifdef _MODULE
2257 bmajor = cmajor = NODEVMAJOR;
2258 error = devsw_attach("bpf", NULL, &bmajor,
2259 &bpf_cdevsw, &cmajor);
2260 if (error)
2261 break;
2262 #endif
2263
2264 bpf_ops_handover_enter(&bpf_ops_kernel);
2265 atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel);
2266 bpf_ops_handover_exit();
2267 sysctl_net_bpf_setup();
2268 break;
2269
2270 case MODULE_CMD_FINI:
2271 /*
2272 * While there is no reference counting for bpf callers,
2273 * unload could at least in theory be done similarly to
2274 * system call disestablishment. This should even be
2275 * a little simpler:
2276 *
2277 * 1) replace op vector with stubs
2278 * 2) post update to all cpus with xc
2279 * 3) check that nobody is in bpf anymore
2280 * (it's doubtful we'd want something like l_sysent,
2281 * but we could do something like *signed* percpu
2282 * counters. if the sum is 0, we're good).
2283 * 4) if fail, unroll changes
2284 *
2285 * NOTE: change won't be atomic to the outside. some
2286 * packets may be not captured even if unload is
2287 * not succesful. I think packet capture not working
2288 * is a perfectly logical consequence of trying to
2289 * disable packet capture.
2290 */
2291 error = EOPNOTSUPP;
2292 /* insert sysctl teardown */
2293 break;
2294
2295 default:
2296 error = ENOTTY;
2297 break;
2298 }
2299
2300 return error;
2301 }
2302