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