FreeBSD/Linux Kernel Cross Reference
sys/net/bpf.c
1 /*-
2 * Copyright (c) 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from the Stanford/CMU enet packet filter,
6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8 * Berkeley Laboratory.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)bpf.c 8.4 (Berkeley) 1/9/95
35 *
36 * $FreeBSD$
37 */
38
39 #include "opt_bpf.h"
40 #include "opt_mac.h"
41 #include "opt_netgraph.h"
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/fcntl.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/time.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/signalvar.h>
54 #include <sys/filio.h>
55 #include <sys/sockio.h>
56 #include <sys/ttycom.h>
57 #include <sys/uio.h>
58
59 #include <sys/event.h>
60 #include <sys/file.h>
61 #include <sys/poll.h>
62 #include <sys/proc.h>
63
64 #include <sys/socket.h>
65
66 #include <net/if.h>
67 #include <net/bpf.h>
68 #ifdef BPF_JITTER
69 #include <net/bpf_jitter.h>
70 #endif
71 #include <net/bpfdesc.h>
72
73 #include <netinet/in.h>
74 #include <netinet/if_ether.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77
78 #include <net80211/ieee80211_freebsd.h>
79
80 #include <security/mac/mac_framework.h>
81
82 static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
83
84 #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
85
86 #define PRINET 26 /* interruptible */
87
88 /*
89 * bpf_iflist is a list of BPF interface structures, each corresponding to a
90 * specific DLT. The same network interface might have several BPF interface
91 * structures registered by different layers in the stack (i.e., 802.11
92 * frames, ethernet frames, etc).
93 */
94 static LIST_HEAD(, bpf_if) bpf_iflist;
95 static struct mtx bpf_mtx; /* bpf global lock */
96 static int bpf_bpfd_cnt;
97
98 static void bpf_allocbufs(struct bpf_d *);
99 static void bpf_attachd(struct bpf_d *, struct bpf_if *);
100 static void bpf_detachd(struct bpf_d *);
101 static void bpf_freed(struct bpf_d *);
102 static void bpf_mcopy(const void *, void *, size_t);
103 static int bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
104 struct sockaddr *, int *, struct bpf_insn *);
105 static int bpf_setif(struct bpf_d *, struct ifreq *);
106 static void bpf_timed_out(void *);
107 static __inline void
108 bpf_wakeup(struct bpf_d *);
109 static void catchpacket(struct bpf_d *, u_char *, u_int,
110 u_int, void (*)(const void *, void *, size_t),
111 struct timeval *);
112 static void reset_d(struct bpf_d *);
113 static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
114 static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
115 static int bpf_setdlt(struct bpf_d *, u_int);
116 static void filt_bpfdetach(struct knote *);
117 static int filt_bpfread(struct knote *, long);
118 static void bpf_drvinit(void *);
119 static void bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
120 static int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
121
122 SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
123 static int bpf_bufsize = 4096;
124 SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
125 &bpf_bufsize, 0, "Default bpf buffer size");
126 static int bpf_maxbufsize = BPF_MAXBUFSIZE;
127 SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
128 &bpf_maxbufsize, 0, "Maximum bpf buffer size");
129 static int bpf_maxinsns = BPF_MAXINSNS;
130 SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
131 &bpf_maxinsns, 0, "Maximum bpf program instructions");
132 SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW,
133 bpf_stats_sysctl, "bpf statistics portal");
134
135 static d_open_t bpfopen;
136 static d_close_t bpfclose;
137 static d_read_t bpfread;
138 static d_write_t bpfwrite;
139 static d_ioctl_t bpfioctl;
140 static d_poll_t bpfpoll;
141 static d_kqfilter_t bpfkqfilter;
142
143 static struct cdevsw bpf_cdevsw = {
144 .d_version = D_VERSION,
145 .d_flags = D_TRACKCLOSE,
146 .d_open = bpfopen,
147 .d_close = bpfclose,
148 .d_read = bpfread,
149 .d_write = bpfwrite,
150 .d_ioctl = bpfioctl,
151 .d_poll = bpfpoll,
152 .d_name = "bpf",
153 .d_kqfilter = bpfkqfilter,
154 };
155
156 static struct filterops bpfread_filtops =
157 { 1, NULL, filt_bpfdetach, filt_bpfread };
158
159 static int
160 bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
161 struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
162 {
163 const struct ieee80211_bpf_params *p;
164 struct ether_header *eh;
165 struct mbuf *m;
166 int error;
167 int len;
168 int hlen;
169 int slen;
170
171 /*
172 * Build a sockaddr based on the data link layer type.
173 * We do this at this level because the ethernet header
174 * is copied directly into the data field of the sockaddr.
175 * In the case of SLIP, there is no header and the packet
176 * is forwarded as is.
177 * Also, we are careful to leave room at the front of the mbuf
178 * for the link level header.
179 */
180 switch (linktype) {
181
182 case DLT_SLIP:
183 sockp->sa_family = AF_INET;
184 hlen = 0;
185 break;
186
187 case DLT_EN10MB:
188 sockp->sa_family = AF_UNSPEC;
189 /* XXX Would MAXLINKHDR be better? */
190 hlen = ETHER_HDR_LEN;
191 break;
192
193 case DLT_FDDI:
194 sockp->sa_family = AF_IMPLINK;
195 hlen = 0;
196 break;
197
198 case DLT_RAW:
199 sockp->sa_family = AF_UNSPEC;
200 hlen = 0;
201 break;
202
203 case DLT_NULL:
204 /*
205 * null interface types require a 4 byte pseudo header which
206 * corresponds to the address family of the packet.
207 */
208 sockp->sa_family = AF_UNSPEC;
209 hlen = 4;
210 break;
211
212 case DLT_ATM_RFC1483:
213 /*
214 * en atm driver requires 4-byte atm pseudo header.
215 * though it isn't standard, vpi:vci needs to be
216 * specified anyway.
217 */
218 sockp->sa_family = AF_UNSPEC;
219 hlen = 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
220 break;
221
222 case DLT_PPP:
223 sockp->sa_family = AF_UNSPEC;
224 hlen = 4; /* This should match PPP_HDRLEN */
225 break;
226
227 case DLT_IEEE802_11: /* IEEE 802.11 wireless */
228 sockp->sa_family = AF_IEEE80211;
229 hlen = 0;
230 break;
231
232 case DLT_IEEE802_11_RADIO: /* IEEE 802.11 wireless w/ phy params */
233 sockp->sa_family = AF_IEEE80211;
234 sockp->sa_len = 12; /* XXX != 0 */
235 hlen = sizeof(struct ieee80211_bpf_params);
236 break;
237
238 default:
239 return (EIO);
240 }
241
242 len = uio->uio_resid;
243
244 if (len - hlen > ifp->if_mtu)
245 return (EMSGSIZE);
246
247 if ((unsigned)len > MJUM16BYTES)
248 return (EIO);
249
250 if (len <= MHLEN)
251 MGETHDR(m, M_TRYWAIT, MT_DATA);
252 else if (len <= MCLBYTES)
253 m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
254 else
255 m = m_getjcl(M_TRYWAIT, MT_DATA, M_PKTHDR,
256 #if (MJUMPAGESIZE > MCLBYTES)
257 len <= MJUMPAGESIZE ? MJUMPAGESIZE :
258 #endif
259 (len <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES));
260 if (m == NULL)
261 return (ENOBUFS);
262 m->m_pkthdr.len = m->m_len = len;
263 m->m_pkthdr.rcvif = NULL;
264 *mp = m;
265
266 if (m->m_len < hlen) {
267 error = EPERM;
268 goto bad;
269 }
270
271 error = uiomove(mtod(m, u_char *), len, uio);
272 if (error)
273 goto bad;
274
275 slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
276 if (slen == 0) {
277 error = EPERM;
278 goto bad;
279 }
280
281 /* Check for multicast destination */
282 switch (linktype) {
283 case DLT_EN10MB:
284 eh = mtod(m, struct ether_header *);
285 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
286 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
287 ETHER_ADDR_LEN) == 0)
288 m->m_flags |= M_BCAST;
289 else
290 m->m_flags |= M_MCAST;
291 }
292 break;
293 }
294
295 /*
296 * Make room for link header, and copy it to sockaddr
297 */
298 if (hlen != 0) {
299 if (sockp->sa_family == AF_IEEE80211) {
300 /*
301 * Collect true length from the parameter header
302 * NB: sockp is known to be zero'd so if we do a
303 * short copy unspecified parameters will be
304 * zero.
305 * NB: packet may not be aligned after stripping
306 * bpf params
307 * XXX check ibp_vers
308 */
309 p = mtod(m, const struct ieee80211_bpf_params *);
310 hlen = p->ibp_len;
311 if (hlen > sizeof(sockp->sa_data)) {
312 error = EINVAL;
313 goto bad;
314 }
315 }
316 bcopy(m->m_data, sockp->sa_data, hlen);
317 }
318 *hdrlen = hlen;
319
320 return (0);
321 bad:
322 m_freem(m);
323 return (error);
324 }
325
326 /*
327 * Attach file to the bpf interface, i.e. make d listen on bp.
328 */
329 static void
330 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
331 {
332 /*
333 * Point d at bp, and add d to the interface's list of listeners.
334 * Finally, point the driver's bpf cookie at the interface so
335 * it will divert packets to bpf.
336 */
337 BPFIF_LOCK(bp);
338 d->bd_bif = bp;
339 LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
340
341 bpf_bpfd_cnt++;
342 BPFIF_UNLOCK(bp);
343 }
344
345 /*
346 * Detach a file from its interface.
347 */
348 static void
349 bpf_detachd(struct bpf_d *d)
350 {
351 int error;
352 struct bpf_if *bp;
353 struct ifnet *ifp;
354
355 bp = d->bd_bif;
356 BPFIF_LOCK(bp);
357 BPFD_LOCK(d);
358 ifp = d->bd_bif->bif_ifp;
359
360 /*
361 * Remove d from the interface's descriptor list.
362 */
363 LIST_REMOVE(d, bd_next);
364
365 bpf_bpfd_cnt--;
366 d->bd_bif = NULL;
367 BPFD_UNLOCK(d);
368 BPFIF_UNLOCK(bp);
369
370 /*
371 * Check if this descriptor had requested promiscuous mode.
372 * If so, turn it off.
373 */
374 if (d->bd_promisc) {
375 d->bd_promisc = 0;
376 error = ifpromisc(ifp, 0);
377 if (error != 0 && error != ENXIO) {
378 /*
379 * ENXIO can happen if a pccard is unplugged
380 * Something is really wrong if we were able to put
381 * the driver into promiscuous mode, but can't
382 * take it out.
383 */
384 if_printf(bp->bif_ifp,
385 "bpf_detach: ifpromisc failed (%d)\n", error);
386 }
387 }
388 }
389
390 /*
391 * Open ethernet device. Returns ENXIO for illegal minor device number,
392 * EBUSY if file is open by another process.
393 */
394 /* ARGSUSED */
395 static int
396 bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
397 {
398 struct bpf_d *d;
399
400 mtx_lock(&bpf_mtx);
401 d = dev->si_drv1;
402 /*
403 * Each minor can be opened by only one process. If the requested
404 * minor is in use, return EBUSY.
405 */
406 if (d != NULL) {
407 mtx_unlock(&bpf_mtx);
408 return (EBUSY);
409 }
410 dev->si_drv1 = (struct bpf_d *)~0; /* mark device in use */
411 mtx_unlock(&bpf_mtx);
412
413 if ((dev->si_flags & SI_NAMED) == 0)
414 make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
415 "bpf%d", dev2unit(dev));
416 MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
417 dev->si_drv1 = d;
418 d->bd_bufsize = bpf_bufsize;
419 d->bd_sig = SIGIO;
420 d->bd_direction = BPF_D_INOUT;
421 d->bd_pid = td->td_proc->p_pid;
422 #ifdef MAC
423 mac_init_bpfdesc(d);
424 mac_create_bpfdesc(td->td_ucred, d);
425 #endif
426 mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
427 callout_init_mtx(&d->bd_callout, &d->bd_mtx, 0);
428 knlist_init_mtx(&d->bd_sel.si_note, &d->bd_mtx);
429
430 return (0);
431 }
432
433 /*
434 * Close the descriptor by detaching it from its interface,
435 * deallocating its buffers, and marking it free.
436 */
437 /* ARGSUSED */
438 static int
439 bpfclose(struct cdev *dev, int flags, int fmt, struct thread *td)
440 {
441 struct bpf_d *d = dev->si_drv1;
442
443 BPFD_LOCK(d);
444 if (d->bd_state == BPF_WAITING)
445 callout_stop(&d->bd_callout);
446 d->bd_state = BPF_IDLE;
447 BPFD_UNLOCK(d);
448 funsetown(&d->bd_sigio);
449 mtx_lock(&bpf_mtx);
450 if (d->bd_bif)
451 bpf_detachd(d);
452 mtx_unlock(&bpf_mtx);
453 selwakeuppri(&d->bd_sel, PRINET);
454 #ifdef MAC
455 mac_destroy_bpfdesc(d);
456 #endif /* MAC */
457 knlist_destroy(&d->bd_sel.si_note);
458 callout_drain(&d->bd_callout);
459 bpf_freed(d);
460 dev->si_drv1 = NULL;
461 free(d, M_BPF);
462
463 return (0);
464 }
465
466
467 /*
468 * Rotate the packet buffers in descriptor d. Move the store buffer
469 * into the hold slot, and the free buffer into the store slot.
470 * Zero the length of the new store buffer.
471 */
472 #define ROTATE_BUFFERS(d) \
473 (d)->bd_hbuf = (d)->bd_sbuf; \
474 (d)->bd_hlen = (d)->bd_slen; \
475 (d)->bd_sbuf = (d)->bd_fbuf; \
476 (d)->bd_slen = 0; \
477 (d)->bd_fbuf = NULL;
478 /*
479 * bpfread - read next chunk of packets from buffers
480 */
481 static int
482 bpfread(struct cdev *dev, struct uio *uio, int ioflag)
483 {
484 struct bpf_d *d = dev->si_drv1;
485 int error;
486 int non_block;
487 int timed_out;
488
489 /*
490 * Restrict application to use a buffer the same size as
491 * as kernel buffers.
492 */
493 if (uio->uio_resid != d->bd_bufsize)
494 return (EINVAL);
495
496 non_block = ((ioflag & O_NONBLOCK) != 0);
497
498 BPFD_LOCK(d);
499 d->bd_pid = curthread->td_proc->p_pid;
500 if (d->bd_state == BPF_WAITING)
501 callout_stop(&d->bd_callout);
502 timed_out = (d->bd_state == BPF_TIMED_OUT);
503 d->bd_state = BPF_IDLE;
504 /*
505 * If the hold buffer is empty, then do a timed sleep, which
506 * ends when the timeout expires or when enough packets
507 * have arrived to fill the store buffer.
508 */
509 while (d->bd_hbuf == NULL) {
510 if (d->bd_slen != 0) {
511 /*
512 * A packet(s) either arrived since the previous
513 * read or arrived while we were asleep.
514 */
515 if (d->bd_immediate || non_block || timed_out) {
516 /*
517 * Rotate the buffers and return what's here
518 * if we are in immediate mode, non-blocking
519 * flag is set, or this descriptor timed out.
520 */
521 ROTATE_BUFFERS(d);
522 break;
523 }
524 }
525
526 /*
527 * No data is available, check to see if the bpf device
528 * is still pointed at a real interface. If not, return
529 * ENXIO so that the userland process knows to rebind
530 * it before using it again.
531 */
532 if (d->bd_bif == NULL) {
533 BPFD_UNLOCK(d);
534 return (ENXIO);
535 }
536
537 if (non_block) {
538 BPFD_UNLOCK(d);
539 return (EWOULDBLOCK);
540 }
541 error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
542 "bpf", d->bd_rtout);
543 if (error == EINTR || error == ERESTART) {
544 BPFD_UNLOCK(d);
545 return (error);
546 }
547 if (error == EWOULDBLOCK) {
548 /*
549 * On a timeout, return what's in the buffer,
550 * which may be nothing. If there is something
551 * in the store buffer, we can rotate the buffers.
552 */
553 if (d->bd_hbuf)
554 /*
555 * We filled up the buffer in between
556 * getting the timeout and arriving
557 * here, so we don't need to rotate.
558 */
559 break;
560
561 if (d->bd_slen == 0) {
562 BPFD_UNLOCK(d);
563 return (0);
564 }
565 ROTATE_BUFFERS(d);
566 break;
567 }
568 }
569 /*
570 * At this point, we know we have something in the hold slot.
571 */
572 BPFD_UNLOCK(d);
573
574 /*
575 * Move data from hold buffer into user space.
576 * We know the entire buffer is transferred since
577 * we checked above that the read buffer is bpf_bufsize bytes.
578 *
579 * XXXRW: More synchronization needed here: what if a second thread
580 * issues a read on the same fd at the same time? Don't want this
581 * getting invalidated.
582 */
583 error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
584
585 BPFD_LOCK(d);
586 d->bd_fbuf = d->bd_hbuf;
587 d->bd_hbuf = NULL;
588 d->bd_hlen = 0;
589 BPFD_UNLOCK(d);
590
591 return (error);
592 }
593
594 /*
595 * If there are processes sleeping on this descriptor, wake them up.
596 */
597 static __inline void
598 bpf_wakeup(struct bpf_d *d)
599 {
600
601 BPFD_LOCK_ASSERT(d);
602 if (d->bd_state == BPF_WAITING) {
603 callout_stop(&d->bd_callout);
604 d->bd_state = BPF_IDLE;
605 }
606 wakeup(d);
607 if (d->bd_async && d->bd_sig && d->bd_sigio)
608 pgsigio(&d->bd_sigio, d->bd_sig, 0);
609
610 selwakeuppri(&d->bd_sel, PRINET);
611 KNOTE_LOCKED(&d->bd_sel.si_note, 0);
612 }
613
614 static void
615 bpf_timed_out(void *arg)
616 {
617 struct bpf_d *d = (struct bpf_d *)arg;
618
619 BPFD_LOCK_ASSERT(d);
620
621 if (callout_pending(&d->bd_callout) || !callout_active(&d->bd_callout))
622 return;
623 if (d->bd_state == BPF_WAITING) {
624 d->bd_state = BPF_TIMED_OUT;
625 if (d->bd_slen != 0)
626 bpf_wakeup(d);
627 }
628 }
629
630 static int
631 bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
632 {
633 struct bpf_d *d = dev->si_drv1;
634 struct ifnet *ifp;
635 struct mbuf *m, *mc;
636 struct sockaddr dst;
637 int error, hlen;
638
639 d->bd_pid = curthread->td_proc->p_pid;
640 if (d->bd_bif == NULL)
641 return (ENXIO);
642
643 ifp = d->bd_bif->bif_ifp;
644
645 if ((ifp->if_flags & IFF_UP) == 0)
646 return (ENETDOWN);
647
648 if (uio->uio_resid == 0)
649 return (0);
650
651 bzero(&dst, sizeof(dst));
652 m = NULL;
653 hlen = 0;
654 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp,
655 &m, &dst, &hlen, d->bd_wfilter);
656 if (error)
657 return (error);
658
659 if (d->bd_hdrcmplt)
660 dst.sa_family = pseudo_AF_HDRCMPLT;
661
662 if (d->bd_feedback) {
663 mc = m_dup(m, M_DONTWAIT);
664 if (mc != NULL)
665 mc->m_pkthdr.rcvif = ifp;
666 /* Set M_PROMISC for outgoing packets to be discarded. */
667 if (d->bd_direction == BPF_D_INOUT)
668 m->m_flags |= M_PROMISC;
669 } else
670 mc = NULL;
671
672 m->m_pkthdr.len -= hlen;
673 m->m_len -= hlen;
674 m->m_data += hlen; /* XXX */
675
676 #ifdef MAC
677 BPFD_LOCK(d);
678 mac_create_mbuf_from_bpfdesc(d, m);
679 if (mc != NULL)
680 mac_create_mbuf_from_bpfdesc(d, mc);
681 BPFD_UNLOCK(d);
682 #endif
683
684 error = (*ifp->if_output)(ifp, m, &dst, NULL);
685
686 if (mc != NULL) {
687 if (error == 0)
688 (*ifp->if_input)(ifp, mc);
689 else
690 m_freem(mc);
691 }
692
693 return (error);
694 }
695
696 /*
697 * Reset a descriptor by flushing its packet buffer and clearing the
698 * receive and drop counts.
699 */
700 static void
701 reset_d(struct bpf_d *d)
702 {
703
704 mtx_assert(&d->bd_mtx, MA_OWNED);
705 if (d->bd_hbuf) {
706 /* Free the hold buffer. */
707 d->bd_fbuf = d->bd_hbuf;
708 d->bd_hbuf = NULL;
709 }
710 d->bd_slen = 0;
711 d->bd_hlen = 0;
712 d->bd_rcount = 0;
713 d->bd_dcount = 0;
714 d->bd_fcount = 0;
715 }
716
717 /*
718 * FIONREAD Check for read packet available.
719 * SIOCGIFADDR Get interface address - convenient hook to driver.
720 * BIOCGBLEN Get buffer len [for read()].
721 * BIOCSETF Set read filter.
722 * BIOCSETFNR Set read filter without resetting descriptor.
723 * BIOCSETWF Set write filter.
724 * BIOCFLUSH Flush read packet buffer.
725 * BIOCPROMISC Put interface into promiscuous mode.
726 * BIOCGDLT Get link layer type.
727 * BIOCGETIF Get interface name.
728 * BIOCSETIF Set interface.
729 * BIOCSRTIMEOUT Set read timeout.
730 * BIOCGRTIMEOUT Get read timeout.
731 * BIOCGSTATS Get packet stats.
732 * BIOCIMMEDIATE Set immediate mode.
733 * BIOCVERSION Get filter language version.
734 * BIOCGHDRCMPLT Get "header already complete" flag
735 * BIOCSHDRCMPLT Set "header already complete" flag
736 * BIOCGDIRECTION Get packet direction flag
737 * BIOCSDIRECTION Set packet direction flag
738 * BIOCLOCK Set "locked" flag
739 * BIOCFEEDBACK Set packet feedback mode.
740 */
741 /* ARGSUSED */
742 static int
743 bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
744 struct thread *td)
745 {
746 struct bpf_d *d = dev->si_drv1;
747 int error = 0;
748
749 /*
750 * Refresh PID associated with this descriptor.
751 */
752 BPFD_LOCK(d);
753 d->bd_pid = td->td_proc->p_pid;
754 if (d->bd_state == BPF_WAITING)
755 callout_stop(&d->bd_callout);
756 d->bd_state = BPF_IDLE;
757 BPFD_UNLOCK(d);
758
759 if (d->bd_locked == 1) {
760 switch (cmd) {
761 case BIOCGBLEN:
762 case BIOCFLUSH:
763 case BIOCGDLT:
764 case BIOCGDLTLIST:
765 case BIOCGETIF:
766 case BIOCGRTIMEOUT:
767 case BIOCGSTATS:
768 case BIOCVERSION:
769 case BIOCGRSIG:
770 case BIOCGHDRCMPLT:
771 case BIOCFEEDBACK:
772 case FIONREAD:
773 case BIOCLOCK:
774 case BIOCSRTIMEOUT:
775 case BIOCIMMEDIATE:
776 case TIOCGPGRP:
777 break;
778 default:
779 return (EPERM);
780 }
781 }
782 switch (cmd) {
783
784 default:
785 error = EINVAL;
786 break;
787
788 /*
789 * Check for read packet available.
790 */
791 case FIONREAD:
792 {
793 int n;
794
795 BPFD_LOCK(d);
796 n = d->bd_slen;
797 if (d->bd_hbuf)
798 n += d->bd_hlen;
799 BPFD_UNLOCK(d);
800
801 *(int *)addr = n;
802 break;
803 }
804
805 case SIOCGIFADDR:
806 {
807 struct ifnet *ifp;
808
809 if (d->bd_bif == NULL)
810 error = EINVAL;
811 else {
812 ifp = d->bd_bif->bif_ifp;
813 error = (*ifp->if_ioctl)(ifp, cmd, addr);
814 }
815 break;
816 }
817
818 /*
819 * Get buffer len [for read()].
820 */
821 case BIOCGBLEN:
822 *(u_int *)addr = d->bd_bufsize;
823 break;
824
825 /*
826 * Set buffer length.
827 */
828 case BIOCSBLEN:
829 if (d->bd_bif != NULL)
830 error = EINVAL;
831 else {
832 u_int size = *(u_int *)addr;
833
834 if (size > bpf_maxbufsize)
835 *(u_int *)addr = size = bpf_maxbufsize;
836 else if (size < BPF_MINBUFSIZE)
837 *(u_int *)addr = size = BPF_MINBUFSIZE;
838 d->bd_bufsize = size;
839 }
840 break;
841
842 /*
843 * Set link layer read filter.
844 */
845 case BIOCSETF:
846 case BIOCSETFNR:
847 case BIOCSETWF:
848 error = bpf_setf(d, (struct bpf_program *)addr, cmd);
849 break;
850
851 /*
852 * Flush read packet buffer.
853 */
854 case BIOCFLUSH:
855 BPFD_LOCK(d);
856 reset_d(d);
857 BPFD_UNLOCK(d);
858 break;
859
860 /*
861 * Put interface into promiscuous mode.
862 */
863 case BIOCPROMISC:
864 if (d->bd_bif == NULL) {
865 /*
866 * No interface attached yet.
867 */
868 error = EINVAL;
869 break;
870 }
871 if (d->bd_promisc == 0) {
872 error = ifpromisc(d->bd_bif->bif_ifp, 1);
873 if (error == 0)
874 d->bd_promisc = 1;
875 }
876 break;
877
878 /*
879 * Get current data link type.
880 */
881 case BIOCGDLT:
882 if (d->bd_bif == NULL)
883 error = EINVAL;
884 else
885 *(u_int *)addr = d->bd_bif->bif_dlt;
886 break;
887
888 /*
889 * Get a list of supported data link types.
890 */
891 case BIOCGDLTLIST:
892 if (d->bd_bif == NULL)
893 error = EINVAL;
894 else
895 error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
896 break;
897
898 /*
899 * Set data link type.
900 */
901 case BIOCSDLT:
902 if (d->bd_bif == NULL)
903 error = EINVAL;
904 else
905 error = bpf_setdlt(d, *(u_int *)addr);
906 break;
907
908 /*
909 * Get interface name.
910 */
911 case BIOCGETIF:
912 if (d->bd_bif == NULL)
913 error = EINVAL;
914 else {
915 struct ifnet *const ifp = d->bd_bif->bif_ifp;
916 struct ifreq *const ifr = (struct ifreq *)addr;
917
918 strlcpy(ifr->ifr_name, ifp->if_xname,
919 sizeof(ifr->ifr_name));
920 }
921 break;
922
923 /*
924 * Set interface.
925 */
926 case BIOCSETIF:
927 error = bpf_setif(d, (struct ifreq *)addr);
928 break;
929
930 /*
931 * Set read timeout.
932 */
933 case BIOCSRTIMEOUT:
934 {
935 struct timeval *tv = (struct timeval *)addr;
936
937 /*
938 * Subtract 1 tick from tvtohz() since this isn't
939 * a one-shot timer.
940 */
941 if ((error = itimerfix(tv)) == 0)
942 d->bd_rtout = tvtohz(tv) - 1;
943 break;
944 }
945
946 /*
947 * Get read timeout.
948 */
949 case BIOCGRTIMEOUT:
950 {
951 struct timeval *tv = (struct timeval *)addr;
952
953 tv->tv_sec = d->bd_rtout / hz;
954 tv->tv_usec = (d->bd_rtout % hz) * tick;
955 break;
956 }
957
958 /*
959 * Get packet stats.
960 */
961 case BIOCGSTATS:
962 {
963 struct bpf_stat *bs = (struct bpf_stat *)addr;
964
965 bs->bs_recv = d->bd_rcount;
966 bs->bs_drop = d->bd_dcount;
967 break;
968 }
969
970 /*
971 * Set immediate mode.
972 */
973 case BIOCIMMEDIATE:
974 d->bd_immediate = *(u_int *)addr;
975 break;
976
977 case BIOCVERSION:
978 {
979 struct bpf_version *bv = (struct bpf_version *)addr;
980
981 bv->bv_major = BPF_MAJOR_VERSION;
982 bv->bv_minor = BPF_MINOR_VERSION;
983 break;
984 }
985
986 /*
987 * Get "header already complete" flag
988 */
989 case BIOCGHDRCMPLT:
990 *(u_int *)addr = d->bd_hdrcmplt;
991 break;
992
993 /*
994 * Set "header already complete" flag
995 */
996 case BIOCSHDRCMPLT:
997 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
998 break;
999
1000 /*
1001 * Get packet direction flag
1002 */
1003 case BIOCGDIRECTION:
1004 *(u_int *)addr = d->bd_direction;
1005 break;
1006
1007 /*
1008 * Set packet direction flag
1009 */
1010 case BIOCSDIRECTION:
1011 {
1012 u_int direction;
1013
1014 direction = *(u_int *)addr;
1015 switch (direction) {
1016 case BPF_D_IN:
1017 case BPF_D_INOUT:
1018 case BPF_D_OUT:
1019 d->bd_direction = direction;
1020 break;
1021 default:
1022 error = EINVAL;
1023 }
1024 }
1025 break;
1026
1027 case BIOCFEEDBACK:
1028 d->bd_feedback = *(u_int *)addr;
1029 break;
1030
1031 case BIOCLOCK:
1032 d->bd_locked = 1;
1033 break;
1034
1035 case FIONBIO: /* Non-blocking I/O */
1036 break;
1037
1038 case FIOASYNC: /* Send signal on receive packets */
1039 d->bd_async = *(int *)addr;
1040 break;
1041
1042 case FIOSETOWN:
1043 error = fsetown(*(int *)addr, &d->bd_sigio);
1044 break;
1045
1046 case FIOGETOWN:
1047 *(int *)addr = fgetown(&d->bd_sigio);
1048 break;
1049
1050 /* This is deprecated, FIOSETOWN should be used instead. */
1051 case TIOCSPGRP:
1052 error = fsetown(-(*(int *)addr), &d->bd_sigio);
1053 break;
1054
1055 /* This is deprecated, FIOGETOWN should be used instead. */
1056 case TIOCGPGRP:
1057 *(int *)addr = -fgetown(&d->bd_sigio);
1058 break;
1059
1060 case BIOCSRSIG: /* Set receive signal */
1061 {
1062 u_int sig;
1063
1064 sig = *(u_int *)addr;
1065
1066 if (sig >= NSIG)
1067 error = EINVAL;
1068 else
1069 d->bd_sig = sig;
1070 break;
1071 }
1072 case BIOCGRSIG:
1073 *(u_int *)addr = d->bd_sig;
1074 break;
1075 }
1076 return (error);
1077 }
1078
1079 /*
1080 * Set d's packet filter program to fp. If this file already has a filter,
1081 * free it and replace it. Returns EINVAL for bogus requests.
1082 */
1083 static int
1084 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1085 {
1086 struct bpf_insn *fcode, *old;
1087 u_int wfilter, flen, size;
1088 #ifdef BPF_JITTER
1089 bpf_jit_filter *ofunc;
1090 #endif
1091
1092 if (cmd == BIOCSETWF) {
1093 old = d->bd_wfilter;
1094 wfilter = 1;
1095 #ifdef BPF_JITTER
1096 ofunc = NULL;
1097 #endif
1098 } else {
1099 wfilter = 0;
1100 old = d->bd_rfilter;
1101 #ifdef BPF_JITTER
1102 ofunc = d->bd_bfilter;
1103 #endif
1104 }
1105 if (fp->bf_insns == NULL) {
1106 if (fp->bf_len != 0)
1107 return (EINVAL);
1108 BPFD_LOCK(d);
1109 if (wfilter)
1110 d->bd_wfilter = NULL;
1111 else {
1112 d->bd_rfilter = NULL;
1113 #ifdef BPF_JITTER
1114 d->bd_bfilter = NULL;
1115 #endif
1116 if (cmd == BIOCSETF)
1117 reset_d(d);
1118 }
1119 BPFD_UNLOCK(d);
1120 if (old != NULL)
1121 free((caddr_t)old, M_BPF);
1122 #ifdef BPF_JITTER
1123 if (ofunc != NULL)
1124 bpf_destroy_jit_filter(ofunc);
1125 #endif
1126 return (0);
1127 }
1128 flen = fp->bf_len;
1129 if (flen > bpf_maxinsns)
1130 return (EINVAL);
1131
1132 size = flen * sizeof(*fp->bf_insns);
1133 fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1134 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1135 bpf_validate(fcode, (int)flen)) {
1136 BPFD_LOCK(d);
1137 if (wfilter)
1138 d->bd_wfilter = fcode;
1139 else {
1140 d->bd_rfilter = fcode;
1141 #ifdef BPF_JITTER
1142 d->bd_bfilter = bpf_jitter(fcode, flen);
1143 #endif
1144 if (cmd == BIOCSETF)
1145 reset_d(d);
1146 }
1147 BPFD_UNLOCK(d);
1148 if (old != NULL)
1149 free((caddr_t)old, M_BPF);
1150 #ifdef BPF_JITTER
1151 if (ofunc != NULL)
1152 bpf_destroy_jit_filter(ofunc);
1153 #endif
1154
1155 return (0);
1156 }
1157 free((caddr_t)fcode, M_BPF);
1158 return (EINVAL);
1159 }
1160
1161 /*
1162 * Detach a file from its current interface (if attached at all) and attach
1163 * to the interface indicated by the name stored in ifr.
1164 * Return an errno or 0.
1165 */
1166 static int
1167 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1168 {
1169 struct bpf_if *bp;
1170 struct ifnet *theywant;
1171
1172 theywant = ifunit(ifr->ifr_name);
1173 if (theywant == NULL || theywant->if_bpf == NULL)
1174 return (ENXIO);
1175
1176 bp = theywant->if_bpf;
1177 /*
1178 * Allocate the packet buffers if we need to.
1179 * If we're already attached to requested interface,
1180 * just flush the buffer.
1181 */
1182 if (d->bd_sbuf == NULL)
1183 bpf_allocbufs(d);
1184 if (bp != d->bd_bif) {
1185 if (d->bd_bif)
1186 /*
1187 * Detach if attached to something else.
1188 */
1189 bpf_detachd(d);
1190
1191 bpf_attachd(d, bp);
1192 }
1193 BPFD_LOCK(d);
1194 reset_d(d);
1195 BPFD_UNLOCK(d);
1196 return (0);
1197 }
1198
1199 /*
1200 * Support for select() and poll() system calls
1201 *
1202 * Return true iff the specific operation will not block indefinitely.
1203 * Otherwise, return false but make a note that a selwakeup() must be done.
1204 */
1205 static int
1206 bpfpoll(struct cdev *dev, int events, struct thread *td)
1207 {
1208 struct bpf_d *d;
1209 int revents;
1210
1211 d = dev->si_drv1;
1212 if (d->bd_bif == NULL)
1213 return (ENXIO);
1214
1215 /*
1216 * Refresh PID associated with this descriptor.
1217 */
1218 revents = events & (POLLOUT | POLLWRNORM);
1219 BPFD_LOCK(d);
1220 d->bd_pid = td->td_proc->p_pid;
1221 if (events & (POLLIN | POLLRDNORM)) {
1222 if (bpf_ready(d))
1223 revents |= events & (POLLIN | POLLRDNORM);
1224 else {
1225 selrecord(td, &d->bd_sel);
1226 /* Start the read timeout if necessary. */
1227 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1228 callout_reset(&d->bd_callout, d->bd_rtout,
1229 bpf_timed_out, d);
1230 d->bd_state = BPF_WAITING;
1231 }
1232 }
1233 }
1234 BPFD_UNLOCK(d);
1235 return (revents);
1236 }
1237
1238 /*
1239 * Support for kevent() system call. Register EVFILT_READ filters and
1240 * reject all others.
1241 */
1242 int
1243 bpfkqfilter(struct cdev *dev, struct knote *kn)
1244 {
1245 struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1246
1247 if (kn->kn_filter != EVFILT_READ)
1248 return (1);
1249
1250 /*
1251 * Refresh PID associated with this descriptor.
1252 */
1253 BPFD_LOCK(d);
1254 d->bd_pid = curthread->td_proc->p_pid;
1255 kn->kn_fop = &bpfread_filtops;
1256 kn->kn_hook = d;
1257 knlist_add(&d->bd_sel.si_note, kn, 1);
1258 BPFD_UNLOCK(d);
1259
1260 return (0);
1261 }
1262
1263 static void
1264 filt_bpfdetach(struct knote *kn)
1265 {
1266 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1267
1268 knlist_remove(&d->bd_sel.si_note, kn, 0);
1269 }
1270
1271 static int
1272 filt_bpfread(struct knote *kn, long hint)
1273 {
1274 struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1275 int ready;
1276
1277 BPFD_LOCK_ASSERT(d);
1278 ready = bpf_ready(d);
1279 if (ready) {
1280 kn->kn_data = d->bd_slen;
1281 if (d->bd_hbuf)
1282 kn->kn_data += d->bd_hlen;
1283 }
1284 else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1285 callout_reset(&d->bd_callout, d->bd_rtout,
1286 bpf_timed_out, d);
1287 d->bd_state = BPF_WAITING;
1288 }
1289
1290 return (ready);
1291 }
1292
1293 /*
1294 * Incoming linkage from device drivers. Process the packet pkt, of length
1295 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1296 * by each process' filter, and if accepted, stashed into the corresponding
1297 * buffer.
1298 */
1299 void
1300 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1301 {
1302 struct bpf_d *d;
1303 #ifdef BPF_JITTER
1304 bpf_jit_filter *bf;
1305 #endif
1306 u_int slen;
1307 int gottime;
1308 struct timeval tv;
1309
1310 gottime = 0;
1311 BPFIF_LOCK(bp);
1312 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1313 BPFD_LOCK(d);
1314 ++d->bd_rcount;
1315 #ifdef BPF_JITTER
1316 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
1317 if (bf != NULL)
1318 slen = (*(bf->func))(pkt, pktlen, pktlen);
1319 else
1320 #endif
1321 slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1322 if (slen != 0) {
1323 d->bd_fcount++;
1324 if (!gottime) {
1325 microtime(&tv);
1326 gottime = 1;
1327 }
1328 #ifdef MAC
1329 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1330 #endif
1331 catchpacket(d, pkt, pktlen, slen, bcopy, &tv);
1332 }
1333 BPFD_UNLOCK(d);
1334 }
1335 BPFIF_UNLOCK(bp);
1336 }
1337
1338 /*
1339 * Copy data from an mbuf chain into a buffer. This code is derived
1340 * from m_copydata in sys/uipc_mbuf.c.
1341 */
1342 static void
1343 bpf_mcopy(const void *src_arg, void *dst_arg, size_t len)
1344 {
1345 const struct mbuf *m;
1346 u_int count;
1347 u_char *dst;
1348
1349 m = src_arg;
1350 dst = dst_arg;
1351 while (len > 0) {
1352 if (m == NULL)
1353 panic("bpf_mcopy");
1354 count = min(m->m_len, len);
1355 bcopy(mtod(m, void *), dst, count);
1356 m = m->m_next;
1357 dst += count;
1358 len -= count;
1359 }
1360 }
1361
1362 #define BPF_CHECK_DIRECTION(d, r, i) \
1363 (((d)->bd_direction == BPF_D_IN && (r) != (i)) || \
1364 ((d)->bd_direction == BPF_D_OUT && (r) == (i)))
1365
1366 /*
1367 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1368 */
1369 void
1370 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1371 {
1372 struct bpf_d *d;
1373 #ifdef BPF_JITTER
1374 bpf_jit_filter *bf;
1375 #endif
1376 u_int pktlen, slen;
1377 int gottime;
1378 struct timeval tv;
1379
1380 /* Skip outgoing duplicate packets. */
1381 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
1382 m->m_flags &= ~M_PROMISC;
1383 return;
1384 }
1385
1386 gottime = 0;
1387
1388 pktlen = m_length(m, NULL);
1389
1390 BPFIF_LOCK(bp);
1391 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1392 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1393 continue;
1394 BPFD_LOCK(d);
1395 ++d->bd_rcount;
1396 #ifdef BPF_JITTER
1397 bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
1398 /* XXX We cannot handle multiple mbufs. */
1399 if (bf != NULL && m->m_next == NULL)
1400 slen = (*(bf->func))(mtod(m, u_char *), pktlen, pktlen);
1401 else
1402 #endif
1403 slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1404 if (slen != 0) {
1405 d->bd_fcount++;
1406 if (!gottime) {
1407 microtime(&tv);
1408 gottime = 1;
1409 }
1410 #ifdef MAC
1411 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1412 #endif
1413 catchpacket(d, (u_char *)m, pktlen, slen,
1414 bpf_mcopy, &tv);
1415 }
1416 BPFD_UNLOCK(d);
1417 }
1418 BPFIF_UNLOCK(bp);
1419 }
1420
1421 /*
1422 * Incoming linkage from device drivers, when packet is in
1423 * an mbuf chain and to be prepended by a contiguous header.
1424 */
1425 void
1426 bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1427 {
1428 struct mbuf mb;
1429 struct bpf_d *d;
1430 u_int pktlen, slen;
1431 int gottime;
1432 struct timeval tv;
1433
1434 /* Skip outgoing duplicate packets. */
1435 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
1436 m->m_flags &= ~M_PROMISC;
1437 return;
1438 }
1439
1440 gottime = 0;
1441
1442 pktlen = m_length(m, NULL);
1443 /*
1444 * Craft on-stack mbuf suitable for passing to bpf_filter.
1445 * Note that we cut corners here; we only setup what's
1446 * absolutely needed--this mbuf should never go anywhere else.
1447 */
1448 mb.m_next = m;
1449 mb.m_data = data;
1450 mb.m_len = dlen;
1451 pktlen += dlen;
1452
1453 BPFIF_LOCK(bp);
1454 LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1455 if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1456 continue;
1457 BPFD_LOCK(d);
1458 ++d->bd_rcount;
1459 slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
1460 if (slen != 0) {
1461 d->bd_fcount++;
1462 if (!gottime) {
1463 microtime(&tv);
1464 gottime = 1;
1465 }
1466 #ifdef MAC
1467 if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1468 #endif
1469 catchpacket(d, (u_char *)&mb, pktlen, slen,
1470 bpf_mcopy, &tv);
1471 }
1472 BPFD_UNLOCK(d);
1473 }
1474 BPFIF_UNLOCK(bp);
1475 }
1476
1477 #undef BPF_CHECK_DIRECTION
1478
1479 /*
1480 * Move the packet data from interface memory (pkt) into the
1481 * store buffer. "cpfn" is the routine called to do the actual data
1482 * transfer. bcopy is passed in to copy contiguous chunks, while
1483 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1484 * pkt is really an mbuf.
1485 */
1486 static void
1487 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1488 void (*cpfn)(const void *, void *, size_t), struct timeval *tv)
1489 {
1490 struct bpf_hdr *hp;
1491 int totlen, curlen;
1492 int hdrlen = d->bd_bif->bif_hdrlen;
1493 int do_wakeup = 0;
1494
1495 BPFD_LOCK_ASSERT(d);
1496 /*
1497 * Figure out how many bytes to move. If the packet is
1498 * greater or equal to the snapshot length, transfer that
1499 * much. Otherwise, transfer the whole packet (unless
1500 * we hit the buffer size limit).
1501 */
1502 totlen = hdrlen + min(snaplen, pktlen);
1503 if (totlen > d->bd_bufsize)
1504 totlen = d->bd_bufsize;
1505
1506 /*
1507 * Round up the end of the previous packet to the next longword.
1508 */
1509 curlen = BPF_WORDALIGN(d->bd_slen);
1510 if (curlen + totlen > d->bd_bufsize) {
1511 /*
1512 * This packet will overflow the storage buffer.
1513 * Rotate the buffers if we can, then wakeup any
1514 * pending reads.
1515 */
1516 if (d->bd_fbuf == NULL) {
1517 /*
1518 * We haven't completed the previous read yet,
1519 * so drop the packet.
1520 */
1521 ++d->bd_dcount;
1522 return;
1523 }
1524 ROTATE_BUFFERS(d);
1525 do_wakeup = 1;
1526 curlen = 0;
1527 }
1528 else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1529 /*
1530 * Immediate mode is set, or the read timeout has
1531 * already expired during a select call. A packet
1532 * arrived, so the reader should be woken up.
1533 */
1534 do_wakeup = 1;
1535
1536 /*
1537 * Append the bpf header.
1538 */
1539 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1540 hp->bh_tstamp = *tv;
1541 hp->bh_datalen = pktlen;
1542 hp->bh_hdrlen = hdrlen;
1543 /*
1544 * Copy the packet data into the store buffer and update its length.
1545 */
1546 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1547 d->bd_slen = curlen + totlen;
1548
1549 if (do_wakeup)
1550 bpf_wakeup(d);
1551 }
1552
1553 /*
1554 * Initialize all nonzero fields of a descriptor.
1555 */
1556 static void
1557 bpf_allocbufs(struct bpf_d *d)
1558 {
1559
1560 KASSERT(d->bd_fbuf == NULL, ("bpf_allocbufs: bd_fbuf != NULL"));
1561 KASSERT(d->bd_sbuf == NULL, ("bpf_allocbufs: bd_sbuf != NULL"));
1562 KASSERT(d->bd_hbuf == NULL, ("bpf_allocbufs: bd_hbuf != NULL"));
1563
1564 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1565 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1566 d->bd_slen = 0;
1567 d->bd_hlen = 0;
1568 }
1569
1570 /*
1571 * Free buffers currently in use by a descriptor.
1572 * Called on close.
1573 */
1574 static void
1575 bpf_freed(struct bpf_d *d)
1576 {
1577 /*
1578 * We don't need to lock out interrupts since this descriptor has
1579 * been detached from its interface and it yet hasn't been marked
1580 * free.
1581 */
1582 if (d->bd_sbuf != NULL) {
1583 free(d->bd_sbuf, M_BPF);
1584 if (d->bd_hbuf != NULL)
1585 free(d->bd_hbuf, M_BPF);
1586 if (d->bd_fbuf != NULL)
1587 free(d->bd_fbuf, M_BPF);
1588 }
1589 if (d->bd_rfilter != NULL) {
1590 free((caddr_t)d->bd_rfilter, M_BPF);
1591 #ifdef BPF_JITTER
1592 if (d->bd_bfilter != NULL)
1593 bpf_destroy_jit_filter(d->bd_bfilter);
1594 #endif
1595 }
1596 if (d->bd_wfilter != NULL)
1597 free((caddr_t)d->bd_wfilter, M_BPF);
1598 mtx_destroy(&d->bd_mtx);
1599 }
1600
1601 /*
1602 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the
1603 * fixed size of the link header (variable length headers not yet supported).
1604 */
1605 void
1606 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1607 {
1608
1609 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1610 }
1611
1612 /*
1613 * Attach an interface to bpf. ifp is a pointer to the structure
1614 * defining the interface to be attached, dlt is the link layer type,
1615 * and hdrlen is the fixed size of the link header (variable length
1616 * headers are not yet supporrted).
1617 */
1618 void
1619 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1620 {
1621 struct bpf_if *bp;
1622
1623 bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1624 if (bp == NULL)
1625 panic("bpfattach");
1626
1627 LIST_INIT(&bp->bif_dlist);
1628 bp->bif_ifp = ifp;
1629 bp->bif_dlt = dlt;
1630 mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1631 KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
1632 *driverp = bp;
1633
1634 mtx_lock(&bpf_mtx);
1635 LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1636 mtx_unlock(&bpf_mtx);
1637
1638 /*
1639 * Compute the length of the bpf header. This is not necessarily
1640 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1641 * that the network layer header begins on a longword boundary (for
1642 * performance reasons and to alleviate alignment restrictions).
1643 */
1644 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1645
1646 if (bootverbose)
1647 if_printf(ifp, "bpf attached\n");
1648 }
1649
1650 /*
1651 * Detach bpf from an interface. This involves detaching each descriptor
1652 * associated with the interface, and leaving bd_bif NULL. Notify each
1653 * descriptor as it's detached so that any sleepers wake up and get
1654 * ENXIO.
1655 */
1656 void
1657 bpfdetach(struct ifnet *ifp)
1658 {
1659 struct bpf_if *bp;
1660 struct bpf_d *d;
1661 #ifdef INVARIANTS
1662 int ndetached;
1663
1664 ndetached = 0;
1665 #endif
1666
1667 /* Find all bpf_if struct's which reference ifp and detach them. */
1668 do {
1669 mtx_lock(&bpf_mtx);
1670 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1671 if (ifp == bp->bif_ifp)
1672 break;
1673 }
1674 if (bp != NULL)
1675 LIST_REMOVE(bp, bif_next);
1676 mtx_unlock(&bpf_mtx);
1677
1678 if (bp != NULL) {
1679 #ifdef INVARIANTS
1680 ndetached++;
1681 #endif
1682 while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1683 bpf_detachd(d);
1684 BPFD_LOCK(d);
1685 bpf_wakeup(d);
1686 BPFD_UNLOCK(d);
1687 }
1688 mtx_destroy(&bp->bif_mtx);
1689 free(bp, M_BPF);
1690 }
1691 } while (bp != NULL);
1692
1693 #ifdef INVARIANTS
1694 if (ndetached == 0)
1695 printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1696 #endif
1697 }
1698
1699 /*
1700 * Get a list of available data link type of the interface.
1701 */
1702 static int
1703 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1704 {
1705 int n, error;
1706 struct ifnet *ifp;
1707 struct bpf_if *bp;
1708
1709 ifp = d->bd_bif->bif_ifp;
1710 n = 0;
1711 error = 0;
1712 mtx_lock(&bpf_mtx);
1713 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1714 if (bp->bif_ifp != ifp)
1715 continue;
1716 if (bfl->bfl_list != NULL) {
1717 if (n >= bfl->bfl_len) {
1718 mtx_unlock(&bpf_mtx);
1719 return (ENOMEM);
1720 }
1721 error = copyout(&bp->bif_dlt,
1722 bfl->bfl_list + n, sizeof(u_int));
1723 }
1724 n++;
1725 }
1726 mtx_unlock(&bpf_mtx);
1727 bfl->bfl_len = n;
1728 return (error);
1729 }
1730
1731 /*
1732 * Set the data link type of a BPF instance.
1733 */
1734 static int
1735 bpf_setdlt(struct bpf_d *d, u_int dlt)
1736 {
1737 int error, opromisc;
1738 struct ifnet *ifp;
1739 struct bpf_if *bp;
1740
1741 if (d->bd_bif->bif_dlt == dlt)
1742 return (0);
1743 ifp = d->bd_bif->bif_ifp;
1744 mtx_lock(&bpf_mtx);
1745 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1746 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1747 break;
1748 }
1749 mtx_unlock(&bpf_mtx);
1750 if (bp != NULL) {
1751 opromisc = d->bd_promisc;
1752 bpf_detachd(d);
1753 bpf_attachd(d, bp);
1754 BPFD_LOCK(d);
1755 reset_d(d);
1756 BPFD_UNLOCK(d);
1757 if (opromisc) {
1758 error = ifpromisc(bp->bif_ifp, 1);
1759 if (error)
1760 if_printf(bp->bif_ifp,
1761 "bpf_setdlt: ifpromisc failed (%d)\n",
1762 error);
1763 else
1764 d->bd_promisc = 1;
1765 }
1766 }
1767 return (bp == NULL ? EINVAL : 0);
1768 }
1769
1770 static void
1771 bpf_clone(void *arg, struct ucred *cred, char *name, int namelen,
1772 struct cdev **dev)
1773 {
1774 int u;
1775
1776 if (*dev != NULL)
1777 return;
1778 if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1779 return;
1780 *dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1781 "bpf%d", u);
1782 dev_ref(*dev);
1783 (*dev)->si_flags |= SI_CHEAPCLONE;
1784 return;
1785 }
1786
1787 static void
1788 bpf_drvinit(void *unused)
1789 {
1790
1791 mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1792 LIST_INIT(&bpf_iflist);
1793 EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1794 }
1795
1796 static void
1797 bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
1798 {
1799
1800 bzero(d, sizeof(*d));
1801 BPFD_LOCK_ASSERT(bd);
1802 d->bd_immediate = bd->bd_immediate;
1803 d->bd_promisc = bd->bd_promisc;
1804 d->bd_hdrcmplt = bd->bd_hdrcmplt;
1805 d->bd_direction = bd->bd_direction;
1806 d->bd_feedback = bd->bd_feedback;
1807 d->bd_async = bd->bd_async;
1808 d->bd_rcount = bd->bd_rcount;
1809 d->bd_dcount = bd->bd_dcount;
1810 d->bd_fcount = bd->bd_fcount;
1811 d->bd_sig = bd->bd_sig;
1812 d->bd_slen = bd->bd_slen;
1813 d->bd_hlen = bd->bd_hlen;
1814 d->bd_bufsize = bd->bd_bufsize;
1815 d->bd_pid = bd->bd_pid;
1816 strlcpy(d->bd_ifname,
1817 bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
1818 d->bd_locked = bd->bd_locked;
1819 }
1820
1821 static int
1822 bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
1823 {
1824 struct xbpf_d *xbdbuf, *xbd;
1825 int index, error;
1826 struct bpf_if *bp;
1827 struct bpf_d *bd;
1828
1829 /*
1830 * XXX This is not technically correct. It is possible for non
1831 * privileged users to open bpf devices. It would make sense
1832 * if the users who opened the devices were able to retrieve
1833 * the statistics for them, too.
1834 */
1835 error = priv_check(req->td, PRIV_NET_BPF);
1836 if (error)
1837 return (error);
1838 if (req->oldptr == NULL)
1839 return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
1840 if (bpf_bpfd_cnt == 0)
1841 return (SYSCTL_OUT(req, 0, 0));
1842 xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
1843 mtx_lock(&bpf_mtx);
1844 if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
1845 mtx_unlock(&bpf_mtx);
1846 free(xbdbuf, M_BPF);
1847 return (ENOMEM);
1848 }
1849 index = 0;
1850 LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1851 BPFIF_LOCK(bp);
1852 LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
1853 xbd = &xbdbuf[index++];
1854 BPFD_LOCK(bd);
1855 bpfstats_fill_xbpf(xbd, bd);
1856 BPFD_UNLOCK(bd);
1857 }
1858 BPFIF_UNLOCK(bp);
1859 }
1860 mtx_unlock(&bpf_mtx);
1861 error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
1862 free(xbdbuf, M_BPF);
1863 return (error);
1864 }
1865
1866 SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL);
1867
1868 #else /* !DEV_BPF && !NETGRAPH_BPF */
1869 /*
1870 * NOP stubs to allow bpf-using drivers to load and function.
1871 *
1872 * A 'better' implementation would allow the core bpf functionality
1873 * to be loaded at runtime.
1874 */
1875 static struct bpf_if bp_null;
1876
1877 void
1878 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1879 {
1880 }
1881
1882 void
1883 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1884 {
1885 }
1886
1887 void
1888 bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
1889 {
1890 }
1891
1892 void
1893 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1894 {
1895
1896 bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1897 }
1898
1899 void
1900 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1901 {
1902
1903 *driverp = &bp_null;
1904 }
1905
1906 void
1907 bpfdetach(struct ifnet *ifp)
1908 {
1909 }
1910
1911 u_int
1912 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
1913 {
1914 return -1; /* "no filter" behaviour */
1915 }
1916
1917 int
1918 bpf_validate(const struct bpf_insn *f, int len)
1919 {
1920 return 0; /* false */
1921 }
1922
1923 #endif /* !DEV_BPF && !NETGRAPH_BPF */
Cache object: 7f41fa0edb93bc84e80d67f12205f256
|