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