bpf.c revision 1.36 1 /* $NetBSD: bpf.c,v 1.36 1997/10/09 18:17:19 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)bpf.c 8.2 (Berkeley) 3/28/94
41 * static char rcsid[] =
42 * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp ";
43 */
44
45 #include "bpfilter.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/buf.h>
51 #include <sys/time.h>
52 #include <sys/proc.h>
53 #include <sys/user.h>
54 #include <sys/ioctl.h>
55 #include <sys/map.h>
56 #include <sys/conf.h>
57
58 #include <sys/file.h>
59 #if defined(sparc) && BSD < 199103
60 #include <sys/stream.h>
61 #endif
62 #include <sys/tty.h>
63 #include <sys/uio.h>
64
65 #include <sys/protosw.h>
66 #include <sys/socket.h>
67 #include <sys/errno.h>
68 #include <sys/kernel.h>
69 #include <sys/poll.h>
70
71 #include <net/if.h>
72
73 #include <net/bpf.h>
74 #include <net/bpfdesc.h>
75
76 #include <net/if_arc.h>
77 #include <net/if_ether.h>
78
79 #include <netinet/in.h>
80 #include <netinet/if_inarp.h>
81
82 /*
83 * Older BSDs don't have kernel malloc.
84 */
85 #if BSD < 199103
86 extern bcopy();
87 static caddr_t bpf_alloc();
88 #include <net/bpf_compat.h>
89 #define BPF_BUFSIZE (MCLBYTES-8)
90 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
91 #else
92 #define BPF_BUFSIZE 4096
93 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
94 #endif
95
96 #define PRINET 26 /* interruptible */
97
98 /*
99 * The default read buffer size is patchable.
100 */
101 int bpf_bufsize = BPF_BUFSIZE;
102
103 /*
104 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
105 * bpf_dtab holds the descriptors, indexed by minor device #
106 */
107 struct bpf_if *bpf_iflist;
108 struct bpf_d bpf_dtab[NBPFILTER];
109
110 #if BSD >= 199207 || NetBSD0_9 >= 2
111 /*
112 * bpfilterattach() is called at boot time in new systems. We do
113 * nothing here since old systems will not call this.
114 */
115 /* ARGSUSED */
116 void
117 bpfilterattach(n)
118 int n;
119 {
120 }
121 #endif
122
123 static int bpf_allocbufs __P((struct bpf_d *));
124 static int bpf_allocbufs __P((struct bpf_d *));
125 static void bpf_freed __P((struct bpf_d *));
126 static void bpf_freed __P((struct bpf_d *));
127 static void bpf_ifname __P((struct ifnet *, struct ifreq *));
128 static void bpf_ifname __P((struct ifnet *, struct ifreq *));
129 static void bpf_mcopy __P((const void *, void *, size_t));
130 static int bpf_movein __P((struct uio *, int, int,
131 struct mbuf **, struct sockaddr *));
132 static void bpf_attachd __P((struct bpf_d *, struct bpf_if *));
133 static void bpf_detachd __P((struct bpf_d *));
134 static int bpf_setif __P((struct bpf_d *, struct ifreq *));
135 int bpfpoll __P((dev_t, int, struct proc *));
136 static __inline void
137 bpf_wakeup __P((struct bpf_d *));
138 static void catchpacket __P((struct bpf_d *, u_char *, u_int, u_int,
139 void (*)(const void *, void *, size_t)));
140 static void reset_d __P((struct bpf_d *));
141
142 static int
143 bpf_movein(uio, linktype, mtu, mp, sockp)
144 register struct uio *uio;
145 int linktype;
146 int mtu;
147 register struct mbuf **mp;
148 register struct sockaddr *sockp;
149 {
150 struct mbuf *m;
151 int error;
152 int len;
153 int hlen;
154 int align;
155
156 /*
157 * Build a sockaddr based on the data link layer type.
158 * We do this at this level because the ethernet header
159 * is copied directly into the data field of the sockaddr.
160 * In the case of SLIP, there is no header and the packet
161 * is forwarded as is.
162 * Also, we are careful to leave room at the front of the mbuf
163 * for the link level header.
164 */
165 switch (linktype) {
166
167 case DLT_SLIP:
168 sockp->sa_family = AF_INET;
169 hlen = 0;
170 align = 0;
171 break;
172
173 case DLT_PPP:
174 sockp->sa_family = AF_UNSPEC;
175 hlen = 0;
176 align = 0;
177 break;
178
179 case DLT_EN10MB:
180 sockp->sa_family = AF_UNSPEC;
181 /* XXX Would MAXLINKHDR be better? */
182 /* 6(dst)+6(src)+2(type) */
183 hlen = sizeof(struct ether_header);
184 align = 2;
185 break;
186
187 case DLT_ARCNET:
188 sockp->sa_family = AF_UNSPEC;
189 hlen = ARC_HDRLEN;
190 align = 5;
191 break;
192
193 case DLT_FDDI:
194 sockp->sa_family = AF_UNSPEC;
195 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
196 hlen = 24;
197 align = 0;
198 break;
199
200 case DLT_NULL:
201 sockp->sa_family = AF_UNSPEC;
202 hlen = 0;
203 align = 0;
204 break;
205
206 default:
207 return (EIO);
208 }
209
210 len = uio->uio_resid;
211 /*
212 * If there aren't enough bytes for a link level header or the
213 * packet length exceeds the interface mtu, return an error.
214 */
215 if (len < hlen || len - hlen > mtu)
216 return (EMSGSIZE);
217
218 /*
219 * XXX Avoid complicated buffer chaining ---
220 * bail if it won't fit in a single mbuf.
221 * (Take into account possible alignment bytes)
222 */
223 if ((unsigned)len > MCLBYTES - align)
224 return (EIO);
225
226 MGETHDR(m, M_WAIT, MT_DATA);
227 if (m == 0)
228 return (ENOBUFS);
229 m->m_pkthdr.rcvif = 0;
230 m->m_pkthdr.len = len - hlen;
231 if (len > MHLEN - align) {
232 #if BSD >= 199103
233 MCLGET(m, M_WAIT);
234 if ((m->m_flags & M_EXT) == 0) {
235 #else
236 MCLGET(m);
237 if (m->m_len != MCLBYTES) {
238 #endif
239 error = ENOBUFS;
240 goto bad;
241 }
242 }
243
244 /* Insure the data is properly aligned */
245 if (align > 0) {
246 #if BSD >= 199103
247 m->m_data += align;
248 #else
249 m->m_off += align;
250 #endif
251 m->m_len -= align;
252 }
253
254 error = UIOMOVE(mtod(m, caddr_t), len, UIO_WRITE, uio);
255 if (error)
256 goto bad;
257 if (hlen != 0) {
258 bcopy(mtod(m, caddr_t), sockp->sa_data, hlen);
259 #if BSD >= 199103
260 m->m_data += hlen; /* XXX */
261 #else
262 m->m_off += hlen;
263 #endif
264 len -= hlen;
265 }
266 m->m_len = len;
267 *mp = m;
268 bad:
269 m_freem(m);
270 return (error);
271 }
272
273 /*
274 * Attach file to the bpf interface, i.e. make d listen on bp.
275 * Must be called at splimp.
276 */
277 static void
278 bpf_attachd(d, bp)
279 struct bpf_d *d;
280 struct bpf_if *bp;
281 {
282 /*
283 * Point d at bp, and add d to the interface's list of listeners.
284 * Finally, point the driver's bpf cookie at the interface so
285 * it will divert packets to bpf.
286 */
287 d->bd_bif = bp;
288 d->bd_next = bp->bif_dlist;
289 bp->bif_dlist = d;
290
291 *bp->bif_driverp = bp;
292 }
293
294 /*
295 * Detach a file from its interface.
296 */
297 static void
298 bpf_detachd(d)
299 struct bpf_d *d;
300 {
301 struct bpf_d **p;
302 struct bpf_if *bp;
303
304 bp = d->bd_bif;
305 /*
306 * Check if this descriptor had requested promiscuous mode.
307 * If so, turn it off.
308 */
309 if (d->bd_promisc) {
310 int error;
311
312 d->bd_promisc = 0;
313 /*
314 * Take device out of promiscuous mode. Since we were
315 * able to enter promiscuous mode, we should be able
316 * to turn it off. But we can get an error if
317 * the interface was configured down, so only panic
318 * if we don't get an unexpected error.
319 */
320 error = ifpromisc(bp->bif_ifp, 0);
321 if (error && error != EINVAL)
322 panic("bpf: ifpromisc failed");
323 }
324 /* Remove d from the interface's descriptor list. */
325 p = &bp->bif_dlist;
326 while (*p != d) {
327 p = &(*p)->bd_next;
328 if (*p == 0)
329 panic("bpf_detachd: descriptor not in list");
330 }
331 *p = (*p)->bd_next;
332 if (bp->bif_dlist == 0)
333 /*
334 * Let the driver know that there are no more listeners.
335 */
336 *d->bd_bif->bif_driverp = 0;
337 d->bd_bif = 0;
338 }
339
340
341 /*
342 * Mark a descriptor free by making it point to itself.
343 * This is probably cheaper than marking with a constant since
344 * the address should be in a register anyway.
345 */
346 #define D_ISFREE(d) ((d) == (d)->bd_next)
347 #define D_MARKFREE(d) ((d)->bd_next = (d))
348 #define D_MARKUSED(d) ((d)->bd_next = 0)
349
350 /*
351 * Open ethernet device. Returns ENXIO for illegal minor device number,
352 * EBUSY if file is open by another process.
353 */
354 /* ARGSUSED */
355 int
356 bpfopen(dev, flag, mode, p)
357 dev_t dev;
358 int flag;
359 int mode;
360 struct proc *p;
361 {
362 register struct bpf_d *d;
363
364 if (minor(dev) >= NBPFILTER)
365 return (ENXIO);
366 /*
367 * Each minor can be opened by only one process. If the requested
368 * minor is in use, return EBUSY.
369 */
370 d = &bpf_dtab[minor(dev)];
371 if (!D_ISFREE(d))
372 return (EBUSY);
373
374 /* Mark "free" and do most initialization. */
375 bzero((char *)d, sizeof(*d));
376 d->bd_bufsize = bpf_bufsize;
377 d->bd_sig = SIGIO;
378
379 return (0);
380 }
381
382 /*
383 * Close the descriptor by detaching it from its interface,
384 * deallocating its buffers, and marking it free.
385 */
386 /* ARGSUSED */
387 int
388 bpfclose(dev, flag, mode, p)
389 dev_t dev;
390 int flag;
391 int mode;
392 struct proc *p;
393 {
394 register struct bpf_d *d = &bpf_dtab[minor(dev)];
395 register int s;
396
397 s = splimp();
398 if (d->bd_bif)
399 bpf_detachd(d);
400 splx(s);
401 bpf_freed(d);
402
403 return (0);
404 }
405
406 /*
407 * Support for SunOS, which does not have tsleep.
408 */
409 #if BSD < 199103
410 static
411 bpf_timeout(arg)
412 caddr_t arg;
413 {
414 struct bpf_d *d = (struct bpf_d *)arg;
415 d->bd_timedout = 1;
416 wakeup(arg);
417 }
418
419 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
420
421 int
422 bpf_sleep(d)
423 register struct bpf_d *d;
424 {
425 register int rto = d->bd_rtout;
426 register int st;
427
428 if (rto != 0) {
429 d->bd_timedout = 0;
430 timeout(bpf_timeout, (caddr_t)d, rto);
431 }
432 st = sleep((caddr_t)d, PRINET|PCATCH);
433 if (rto != 0) {
434 if (d->bd_timedout == 0)
435 untimeout(bpf_timeout, (caddr_t)d);
436 else if (st == 0)
437 return EWOULDBLOCK;
438 }
439 return (st != 0) ? EINTR : 0;
440 }
441 #else
442 #define BPF_SLEEP tsleep
443 #endif
444
445 /*
446 * Rotate the packet buffers in descriptor d. Move the store buffer
447 * into the hold slot, and the free buffer into the store slot.
448 * Zero the length of the new store buffer.
449 */
450 #define ROTATE_BUFFERS(d) \
451 (d)->bd_hbuf = (d)->bd_sbuf; \
452 (d)->bd_hlen = (d)->bd_slen; \
453 (d)->bd_sbuf = (d)->bd_fbuf; \
454 (d)->bd_slen = 0; \
455 (d)->bd_fbuf = 0;
456 /*
457 * bpfread - read next chunk of packets from buffers
458 */
459 int
460 bpfread(dev, uio, ioflag)
461 dev_t dev;
462 register struct uio *uio;
463 int ioflag;
464 {
465 register struct bpf_d *d = &bpf_dtab[minor(dev)];
466 int error;
467 int s;
468
469 /*
470 * Restrict application to use a buffer the same size as
471 * as kernel buffers.
472 */
473 if (uio->uio_resid != d->bd_bufsize)
474 return (EINVAL);
475
476 s = splimp();
477 /*
478 * If the hold buffer is empty, then do a timed sleep, which
479 * ends when the timeout expires or when enough packets
480 * have arrived to fill the store buffer.
481 */
482 while (d->bd_hbuf == 0) {
483 if (d->bd_immediate) {
484 if (d->bd_slen == 0) {
485 splx(s);
486 return (EWOULDBLOCK);
487 }
488 /*
489 * A packet(s) either arrived since the previous
490 * read or arrived while we were asleep.
491 * Rotate the buffers and return what's here.
492 */
493 ROTATE_BUFFERS(d);
494 break;
495 }
496 if (d->bd_rtout != -1)
497 error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf",
498 d->bd_rtout);
499 else
500 error = EWOULDBLOCK; /* User requested non-blocking I/O */
501 if (error == EINTR || error == ERESTART) {
502 splx(s);
503 return (error);
504 }
505 if (error == EWOULDBLOCK) {
506 /*
507 * On a timeout, return what's in the buffer,
508 * which may be nothing. If there is something
509 * in the store buffer, we can rotate the buffers.
510 */
511 if (d->bd_hbuf)
512 /*
513 * We filled up the buffer in between
514 * getting the timeout and arriving
515 * here, so we don't need to rotate.
516 */
517 break;
518
519 if (d->bd_slen == 0) {
520 splx(s);
521 return (0);
522 }
523 ROTATE_BUFFERS(d);
524 break;
525 }
526 if (error != 0)
527 goto done;
528 }
529 /*
530 * At this point, we know we have something in the hold slot.
531 */
532 splx(s);
533
534 /*
535 * Move data from hold buffer into user space.
536 * We know the entire buffer is transferred since
537 * we checked above that the read buffer is bpf_bufsize bytes.
538 */
539 error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
540
541 s = splimp();
542 d->bd_fbuf = d->bd_hbuf;
543 d->bd_hbuf = 0;
544 d->bd_hlen = 0;
545 done:
546 splx(s);
547 return (error);
548 }
549
550
551 /*
552 * If there are processes sleeping on this descriptor, wake them up.
553 */
554 static __inline void
555 bpf_wakeup(d)
556 register struct bpf_d *d;
557 {
558 struct proc *p;
559
560 wakeup((caddr_t)d);
561 if (d->bd_async && d->bd_sig)
562 if (d->bd_pgid > 0)
563 gsignal (d->bd_pgid, d->bd_sig);
564 else if ((p = pfind (-d->bd_pgid)) != NULL)
565 psignal (p, d->bd_sig);
566
567 #if BSD >= 199103
568 selwakeup(&d->bd_sel);
569 /* XXX */
570 d->bd_sel.si_pid = 0;
571 #else
572 if (d->bd_selproc) {
573 selwakeup(d->bd_selproc, (int)d->bd_selcoll);
574 d->bd_selcoll = 0;
575 d->bd_selproc = 0;
576 }
577 #endif
578 }
579
580 int
581 bpfwrite(dev, uio, ioflag)
582 dev_t dev;
583 struct uio *uio;
584 int ioflag;
585 {
586 register struct bpf_d *d = &bpf_dtab[minor(dev)];
587 struct ifnet *ifp;
588 struct mbuf *m;
589 int error, s;
590 static struct sockaddr dst;
591
592 if (d->bd_bif == 0)
593 return (ENXIO);
594
595 ifp = d->bd_bif->bif_ifp;
596
597 if (uio->uio_resid == 0)
598 return (0);
599
600 error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m, &dst);
601 if (error)
602 return (error);
603
604 if (m->m_pkthdr.len > ifp->if_mtu)
605 return (EMSGSIZE);
606
607 s = splsoftnet();
608 #if BSD >= 199103
609 error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
610 #else
611 error = (*ifp->if_output)(ifp, m, &dst);
612 #endif
613 splx(s);
614 /*
615 * The driver frees the mbuf.
616 */
617 return (error);
618 }
619
620 /*
621 * Reset a descriptor by flushing its packet buffer and clearing the
622 * receive and drop counts. Should be called at splimp.
623 */
624 static void
625 reset_d(d)
626 struct bpf_d *d;
627 {
628 if (d->bd_hbuf) {
629 /* Free the hold buffer. */
630 d->bd_fbuf = d->bd_hbuf;
631 d->bd_hbuf = 0;
632 }
633 d->bd_slen = 0;
634 d->bd_hlen = 0;
635 d->bd_rcount = 0;
636 d->bd_dcount = 0;
637 }
638
639 #ifdef BPF_KERN_FILTER
640 extern struct bpf_insn *bpf_tcp_filter;
641 extern struct bpf_insn *bpf_udp_filter;
642 #endif
643
644 /*
645 * FIONREAD Check for read packet available.
646 * BIOCGBLEN Get buffer len [for read()].
647 * BIOCSETF Set ethernet read filter.
648 * BIOCFLUSH Flush read packet buffer.
649 * BIOCPROMISC Put interface into promiscuous mode.
650 * BIOCGDLT Get link layer type.
651 * BIOCGETIF Get interface name.
652 * BIOCSETIF Set interface.
653 * BIOCSRTIMEOUT Set read timeout.
654 * BIOCGRTIMEOUT Get read timeout.
655 * BIOCGSTATS Get packet stats.
656 * BIOCIMMEDIATE Set immediate mode.
657 * BIOCVERSION Get filter language version.
658 */
659 /* ARGSUSED */
660 int
661 bpfioctl(dev, cmd, addr, flag, p)
662 dev_t dev;
663 u_long cmd;
664 caddr_t addr;
665 int flag;
666 struct proc *p;
667 {
668 register struct bpf_d *d = &bpf_dtab[minor(dev)];
669 int s, error = 0;
670 #ifdef BPF_KERN_FILTER
671 register struct bpf_insn **p;
672 #endif
673
674 switch (cmd) {
675
676 default:
677 error = EINVAL;
678 break;
679
680 /*
681 * Check for read packet available.
682 */
683 case FIONREAD:
684 {
685 int n;
686
687 s = splimp();
688 n = d->bd_slen;
689 if (d->bd_hbuf)
690 n += d->bd_hlen;
691 splx(s);
692
693 *(int *)addr = n;
694 break;
695 }
696
697 /*
698 * Get buffer len [for read()].
699 */
700 case BIOCGBLEN:
701 *(u_int *)addr = d->bd_bufsize;
702 break;
703
704 /*
705 * Set buffer length.
706 */
707 case BIOCSBLEN:
708 #if BSD < 199103
709 error = EINVAL;
710 #else
711 if (d->bd_bif != 0)
712 error = EINVAL;
713 else {
714 register u_int size = *(u_int *)addr;
715
716 if (size > BPF_MAXBUFSIZE)
717 *(u_int *)addr = size = BPF_MAXBUFSIZE;
718 else if (size < BPF_MINBUFSIZE)
719 *(u_int *)addr = size = BPF_MINBUFSIZE;
720 d->bd_bufsize = size;
721 }
722 #endif
723 break;
724
725 /*
726 * Set link layer read filter.
727 */
728 case BIOCSETF:
729 error = bpf_setf(d, (struct bpf_program *)addr);
730 break;
731
732 #ifdef BPF_KERN_FILTER
733 /*
734 * Set TCP or UDP reject filter.
735 */
736 case BIOCSTCPF:
737 case BIOCSUDPF:
738 if (!suser()) {
739 error = EPERM;
740 break;
741 }
742
743 /* Validate and store filter */
744 error = bpf_setf(d, (struct bpf_program *)addr);
745
746 /* Free possible old filter */
747 if (cmd == BIOCSTCPF)
748 p = &bpf_tcp_filter;
749 else
750 p = &bpf_udp_filter;
751 if (*p != NULL)
752 free((caddr_t)*p, M_DEVBUF);
753
754 /* Steal new filter (noop if error) */
755 s = splimp();
756 *p = d->bd_filter;
757 d->bd_filter = NULL;
758 splx(s);
759 break;
760 #endif
761
762 /*
763 * Flush read packet buffer.
764 */
765 case BIOCFLUSH:
766 s = splimp();
767 reset_d(d);
768 splx(s);
769 break;
770
771 /*
772 * Put interface into promiscuous mode.
773 */
774 case BIOCPROMISC:
775 if (d->bd_bif == 0) {
776 /*
777 * No interface attached yet.
778 */
779 error = EINVAL;
780 break;
781 }
782 s = splimp();
783 if (d->bd_promisc == 0) {
784 error = ifpromisc(d->bd_bif->bif_ifp, 1);
785 if (error == 0)
786 d->bd_promisc = 1;
787 }
788 splx(s);
789 break;
790
791 /*
792 * Get device parameters.
793 */
794 case BIOCGDLT:
795 if (d->bd_bif == 0)
796 error = EINVAL;
797 else
798 *(u_int *)addr = d->bd_bif->bif_dlt;
799 break;
800
801 /*
802 * Set interface name.
803 */
804 case BIOCGETIF:
805 if (d->bd_bif == 0)
806 error = EINVAL;
807 else
808 bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
809 break;
810
811 /*
812 * Set interface.
813 */
814 case BIOCSETIF:
815 error = bpf_setif(d, (struct ifreq *)addr);
816 break;
817
818 /*
819 * Set read timeout.
820 */
821 case BIOCSRTIMEOUT:
822 {
823 struct timeval *tv = (struct timeval *)addr;
824
825 /* Compute number of ticks. */
826 d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
827 if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
828 d->bd_rtout = 1;
829 break;
830 }
831
832 /*
833 * Get read timeout.
834 */
835 case BIOCGRTIMEOUT:
836 {
837 struct timeval *tv = (struct timeval *)addr;
838
839 tv->tv_sec = d->bd_rtout / hz;
840 tv->tv_usec = (d->bd_rtout % hz) * tick;
841 break;
842 }
843
844 /*
845 * Get packet stats.
846 */
847 case BIOCGSTATS:
848 {
849 struct bpf_stat *bs = (struct bpf_stat *)addr;
850
851 bs->bs_recv = d->bd_rcount;
852 bs->bs_drop = d->bd_dcount;
853 break;
854 }
855
856 /*
857 * Set immediate mode.
858 */
859 case BIOCIMMEDIATE:
860 d->bd_immediate = *(u_int *)addr;
861 break;
862
863 case BIOCVERSION:
864 {
865 struct bpf_version *bv = (struct bpf_version *)addr;
866
867 bv->bv_major = BPF_MAJOR_VERSION;
868 bv->bv_minor = BPF_MINOR_VERSION;
869 break;
870 }
871
872
873 case FIONBIO: /* Non-blocking I/O */
874 if (*(int *)addr)
875 d->bd_rtout = -1;
876 else
877 d->bd_rtout = 0;
878 break;
879
880 case FIOASYNC: /* Send signal on receive packets */
881 d->bd_async = *(int *)addr;
882 break;
883
884 /*
885 * N.B. ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing
886 * the equivalent of a TIOCSPGRP and hence end up here. *However*
887 * TIOCSPGRP's arg is a process group if it's positive and a process
888 * id if it's negative. This is exactly the opposite of what the
889 * other two functions want! Therefore there is code in ioctl and
890 * fcntl to negate the arg before calling here.
891 */
892 case TIOCSPGRP: /* Process or group to send signals to */
893 d->bd_pgid = *(int *)addr;
894 break;
895
896 case TIOCGPGRP:
897 *(int *)addr = d->bd_pgid;
898 break;
899 }
900 return (error);
901 }
902
903 /*
904 * Set d's packet filter program to fp. If this file already has a filter,
905 * free it and replace it. Returns EINVAL for bogus requests.
906 */
907 int
908 bpf_setf(d, fp)
909 struct bpf_d *d;
910 struct bpf_program *fp;
911 {
912 struct bpf_insn *fcode, *old;
913 u_int flen, size;
914 int s;
915
916 old = d->bd_filter;
917 if (fp->bf_insns == 0) {
918 if (fp->bf_len != 0)
919 return (EINVAL);
920 s = splimp();
921 d->bd_filter = 0;
922 reset_d(d);
923 splx(s);
924 if (old != 0)
925 free((caddr_t)old, M_DEVBUF);
926 return (0);
927 }
928 flen = fp->bf_len;
929 if (flen > BPF_MAXINSNS)
930 return (EINVAL);
931
932 size = flen * sizeof(*fp->bf_insns);
933 fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
934 if (fcode == 0)
935 return (ENOMEM);
936 if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
937 bpf_validate(fcode, (int)flen)) {
938 s = splimp();
939 d->bd_filter = fcode;
940 reset_d(d);
941 splx(s);
942 if (old != 0)
943 free((caddr_t)old, M_DEVBUF);
944
945 return (0);
946 }
947 free((caddr_t)fcode, M_DEVBUF);
948 return (EINVAL);
949 }
950
951 /*
952 * Detach a file from its current interface (if attached at all) and attach
953 * to the interface indicated by the name stored in ifr.
954 * Return an errno or 0.
955 */
956 static int
957 bpf_setif(d, ifr)
958 struct bpf_d *d;
959 struct ifreq *ifr;
960 {
961 struct bpf_if *bp;
962 char *cp;
963 int unit_seen, i, s, error;
964
965 /*
966 * Make sure the provided name has a unit number, and default
967 * it to '0' if not specified.
968 * XXX This is ugly ... do this differently?
969 */
970 unit_seen = 0;
971 cp = ifr->ifr_name;
972 cp[sizeof(ifr->ifr_name) - 1] = '\0'; /* sanity */
973 while (*cp++)
974 if (*cp >= '0' && *cp <= '9')
975 unit_seen = 1;
976 if (!unit_seen) {
977 /* Make sure to leave room for the '\0'. */
978 for (i = 0; i < (IFNAMSIZ - 1); ++i) {
979 if ((ifr->ifr_name[i] >= 'a' &&
980 ifr->ifr_name[i] <= 'z') ||
981 (ifr->ifr_name[i] >= 'A' &&
982 ifr->ifr_name[i] <= 'Z'))
983 continue;
984 ifr->ifr_name[i] = '0';
985 }
986 }
987
988 /*
989 * Look through attached interfaces for the named one.
990 */
991 for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
992 struct ifnet *ifp = bp->bif_ifp;
993
994 if (ifp == 0 ||
995 strcmp(ifp->if_xname, ifr->ifr_name) != 0)
996 continue;
997 /*
998 * We found the requested interface.
999 * If it's not up, return an error.
1000 * Allocate the packet buffers if we need to.
1001 * If we're already attached to requested interface,
1002 * just flush the buffer.
1003 */
1004 if ((ifp->if_flags & IFF_UP) == 0)
1005 return (ENETDOWN);
1006
1007 if (d->bd_sbuf == 0) {
1008 error = bpf_allocbufs(d);
1009 if (error != 0)
1010 return (error);
1011 }
1012 s = splimp();
1013 if (bp != d->bd_bif) {
1014 if (d->bd_bif)
1015 /*
1016 * Detach if attached to something else.
1017 */
1018 bpf_detachd(d);
1019
1020 bpf_attachd(d, bp);
1021 }
1022 reset_d(d);
1023 splx(s);
1024 return (0);
1025 }
1026 /* Not found. */
1027 return (ENXIO);
1028 }
1029
1030 /*
1031 * Copy the interface name to the ifreq.
1032 */
1033 static void
1034 bpf_ifname(ifp, ifr)
1035 struct ifnet *ifp;
1036 struct ifreq *ifr;
1037 {
1038
1039 bcopy(ifp->if_xname, ifr->ifr_name, IFNAMSIZ);
1040 }
1041
1042 /*
1043 * Support for poll() system call
1044 *
1045 * Return true iff the specific operation will not block indefinitely.
1046 * Otherwise, return false but make a note that a selwakeup() must be done.
1047 */
1048 int
1049 bpfpoll(dev, events, p)
1050 register dev_t dev;
1051 int events;
1052 struct proc *p;
1053 {
1054 register struct bpf_d *d = &bpf_dtab[minor(dev)];
1055 int revents = 0;
1056 register int s = splimp();
1057
1058 /*
1059 * An imitation of the FIONREAD ioctl code.
1060 */
1061 if (events & (POLLIN | POLLRDNORM))
1062 if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
1063 revents |= events & (POLLIN | POLLRDNORM);
1064 else
1065 selrecord(p, &d->bd_sel);
1066
1067 splx(s);
1068 return (revents);
1069 }
1070
1071 /*
1072 * Incoming linkage from device drivers. Process the packet pkt, of length
1073 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1074 * by each process' filter, and if accepted, stashed into the corresponding
1075 * buffer.
1076 */
1077 void
1078 bpf_tap(arg, pkt, pktlen)
1079 caddr_t arg;
1080 register u_char *pkt;
1081 register u_int pktlen;
1082 {
1083 struct bpf_if *bp;
1084 register struct bpf_d *d;
1085 register u_int slen;
1086 /*
1087 * Note that the ipl does not have to be raised at this point.
1088 * The only problem that could arise here is that if two different
1089 * interfaces shared any data. This is not the case.
1090 */
1091 bp = (struct bpf_if *)arg;
1092 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1093 ++d->bd_rcount;
1094 slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1095 if (slen != 0)
1096 catchpacket(d, pkt, pktlen, slen, bcopy);
1097 }
1098 }
1099
1100 /*
1101 * Copy data from an mbuf chain into a buffer. This code is derived
1102 * from m_copydata in sys/uipc_mbuf.c.
1103 */
1104 static void
1105 bpf_mcopy(src_arg, dst_arg, len)
1106 const void *src_arg;
1107 void *dst_arg;
1108 register size_t len;
1109 {
1110 register const struct mbuf *m;
1111 register u_int count;
1112 u_char *dst;
1113
1114 m = src_arg;
1115 dst = dst_arg;
1116 while (len > 0) {
1117 if (m == 0)
1118 panic("bpf_mcopy");
1119 count = min(m->m_len, len);
1120 bcopy(mtod(m, caddr_t), (caddr_t)dst, count);
1121 m = m->m_next;
1122 dst += count;
1123 len -= count;
1124 }
1125 }
1126
1127 /*
1128 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1129 */
1130 void
1131 bpf_mtap(arg, m)
1132 caddr_t arg;
1133 struct mbuf *m;
1134 {
1135 struct bpf_if *bp = (struct bpf_if *)arg;
1136 struct bpf_d *d;
1137 u_int pktlen, slen;
1138 struct mbuf *m0;
1139
1140 pktlen = 0;
1141 for (m0 = m; m0 != 0; m0 = m0->m_next)
1142 pktlen += m0->m_len;
1143
1144 for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1145 ++d->bd_rcount;
1146 slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1147 if (slen != 0)
1148 catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1149 }
1150 }
1151
1152 /*
1153 * Move the packet data from interface memory (pkt) into the
1154 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1155 * otherwise 0. "copy" is the routine called to do the actual data
1156 * transfer. bcopy is passed in to copy contiguous chunks, while
1157 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1158 * pkt is really an mbuf.
1159 */
1160 static void
1161 catchpacket(d, pkt, pktlen, snaplen, cpfn)
1162 register struct bpf_d *d;
1163 register u_char *pkt;
1164 register u_int pktlen, snaplen;
1165 register void (*cpfn) __P((const void *, void *, size_t));
1166 {
1167 register struct bpf_hdr *hp;
1168 register int totlen, curlen;
1169 register int hdrlen = d->bd_bif->bif_hdrlen;
1170 /*
1171 * Figure out how many bytes to move. If the packet is
1172 * greater or equal to the snapshot length, transfer that
1173 * much. Otherwise, transfer the whole packet (unless
1174 * we hit the buffer size limit).
1175 */
1176 totlen = hdrlen + min(snaplen, pktlen);
1177 if (totlen > d->bd_bufsize)
1178 totlen = d->bd_bufsize;
1179
1180 /*
1181 * Round up the end of the previous packet to the next longword.
1182 */
1183 curlen = BPF_WORDALIGN(d->bd_slen);
1184 if (curlen + totlen > d->bd_bufsize) {
1185 /*
1186 * This packet will overflow the storage buffer.
1187 * Rotate the buffers if we can, then wakeup any
1188 * pending reads.
1189 */
1190 if (d->bd_fbuf == 0) {
1191 /*
1192 * We haven't completed the previous read yet,
1193 * so drop the packet.
1194 */
1195 ++d->bd_dcount;
1196 return;
1197 }
1198 ROTATE_BUFFERS(d);
1199 bpf_wakeup(d);
1200 curlen = 0;
1201 }
1202 else if (d->bd_immediate)
1203 /*
1204 * Immediate mode is set. A packet arrived so any
1205 * reads should be woken up.
1206 */
1207 bpf_wakeup(d);
1208
1209 /*
1210 * Append the bpf header.
1211 */
1212 hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1213 #if BSD >= 199103
1214 microtime(&hp->bh_tstamp);
1215 #elif defined(sun)
1216 uniqtime(&hp->bh_tstamp);
1217 #else
1218 hp->bh_tstamp = time;
1219 #endif
1220 hp->bh_datalen = pktlen;
1221 hp->bh_hdrlen = hdrlen;
1222 /*
1223 * Copy the packet data into the store buffer and update its length.
1224 */
1225 (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1226 d->bd_slen = curlen + totlen;
1227 }
1228
1229 /*
1230 * Initialize all nonzero fields of a descriptor.
1231 */
1232 static int
1233 bpf_allocbufs(d)
1234 register struct bpf_d *d;
1235 {
1236 d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1237 if (d->bd_fbuf == 0)
1238 return (ENOBUFS);
1239
1240 d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
1241 if (d->bd_sbuf == 0) {
1242 free(d->bd_fbuf, M_DEVBUF);
1243 return (ENOBUFS);
1244 }
1245 d->bd_slen = 0;
1246 d->bd_hlen = 0;
1247 return (0);
1248 }
1249
1250 /*
1251 * Free buffers currently in use by a descriptor.
1252 * Called on close.
1253 */
1254 static void
1255 bpf_freed(d)
1256 register struct bpf_d *d;
1257 {
1258 /*
1259 * We don't need to lock out interrupts since this descriptor has
1260 * been detached from its interface and it yet hasn't been marked
1261 * free.
1262 */
1263 if (d->bd_sbuf != 0) {
1264 free(d->bd_sbuf, M_DEVBUF);
1265 if (d->bd_hbuf != 0)
1266 free(d->bd_hbuf, M_DEVBUF);
1267 if (d->bd_fbuf != 0)
1268 free(d->bd_fbuf, M_DEVBUF);
1269 }
1270 if (d->bd_filter)
1271 free((caddr_t)d->bd_filter, M_DEVBUF);
1272
1273 D_MARKFREE(d);
1274 }
1275
1276 /*
1277 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1278 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1279 * size of the link header (variable length headers not yet supported).
1280 */
1281 void
1282 bpfattach(driverp, ifp, dlt, hdrlen)
1283 caddr_t *driverp;
1284 struct ifnet *ifp;
1285 u_int dlt, hdrlen;
1286 {
1287 struct bpf_if *bp;
1288 int i;
1289 #if BSD < 199103
1290 static struct bpf_if bpf_ifs[NBPFILTER];
1291 static int bpfifno;
1292
1293 bp = (bpfifno < NBPFILTER) ? &bpf_ifs[bpfifno++] : 0;
1294 #else
1295 bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
1296 #endif
1297 if (bp == 0)
1298 panic("bpfattach");
1299
1300 bp->bif_dlist = 0;
1301 bp->bif_driverp = (struct bpf_if **)driverp;
1302 bp->bif_ifp = ifp;
1303 bp->bif_dlt = dlt;
1304
1305 bp->bif_next = bpf_iflist;
1306 bpf_iflist = bp;
1307
1308 *bp->bif_driverp = 0;
1309
1310 /*
1311 * Compute the length of the bpf header. This is not necessarily
1312 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1313 * that the network layer header begins on a longword boundary (for
1314 * performance reasons and to alleviate alignment restrictions).
1315 */
1316 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1317
1318 /*
1319 * Mark all the descriptors free if this hasn't been done.
1320 */
1321 if (!D_ISFREE(&bpf_dtab[0]))
1322 for (i = 0; i < NBPFILTER; ++i)
1323 D_MARKFREE(&bpf_dtab[i]);
1324
1325 #if 0
1326 printf("bpf: %s attached\n", ifp->if_xname);
1327 #endif
1328 }
1329
1330 #if BSD >= 199103
1331 /* XXX This routine belongs in net/if.c. */
1332 /*
1333 * Set/clear promiscuous mode on interface ifp based on the truth value
1334 * of pswitch. The calls are reference counted so that only the first
1335 * "on" request actually has an effect, as does the final "off" request.
1336 * Results are undefined if the "off" and "on" requests are not matched.
1337 */
1338 int
1339 ifpromisc(ifp, pswitch)
1340 register struct ifnet *ifp;
1341 register int pswitch;
1342 {
1343 register int pcount, ret;
1344 register short flags;
1345 struct ifreq ifr;
1346
1347 pcount = ifp->if_pcount;
1348 flags = ifp->if_flags;
1349 if (pswitch) {
1350 /*
1351 * If the device is not configured up, we cannot put it in
1352 * promiscuous mode.
1353 */
1354 if ((ifp->if_flags & IFF_UP) == 0)
1355 return (ENETDOWN);
1356 if (ifp->if_pcount++ != 0)
1357 return (0);
1358 ifp->if_flags |= IFF_PROMISC;
1359 } else {
1360 if (--ifp->if_pcount > 0)
1361 return (0);
1362 ifp->if_flags &= ~IFF_PROMISC;
1363 /*
1364 * If the device is not configured up, we should not need to
1365 * turn off promiscuous mode (device should have turned it
1366 * off when interface went down; and will look at IFF_PROMISC
1367 * again next time interface comes up).
1368 */
1369 if ((ifp->if_flags & IFF_UP) == 0)
1370 return (0);
1371 }
1372 bzero((caddr_t)&ifr, sizeof(ifr));
1373 ifr.ifr_flags = ifp->if_flags;
1374 ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1375 /* Restore interface state if not successful */
1376 if (ret != 0) {
1377 ifp->if_pcount = pcount;
1378 ifp->if_flags = flags;
1379 }
1380 return (ret);
1381 }
1382 #endif
1383
1384 #if BSD < 199103
1385 /*
1386 * Allocate some memory for bpf. This is temporary SunOS support, and
1387 * is admittedly a hack.
1388 * If resources unavailable, return 0.
1389 */
1390 static caddr_t
1391 bpf_alloc(size, canwait)
1392 register int size;
1393 register int canwait;
1394 {
1395 register struct mbuf *m;
1396
1397 if ((unsigned)size > (MCLBYTES-8))
1398 return 0;
1399
1400 MGET(m, canwait, MT_DATA);
1401 if (m == 0)
1402 return 0;
1403 if ((unsigned)size > (MLEN-8)) {
1404 MCLGET(m);
1405 if (m->m_len != MCLBYTES) {
1406 m_freem(m);
1407 return 0;
1408 }
1409 }
1410 *mtod(m, struct mbuf **) = m;
1411 return mtod(m, caddr_t) + 8;
1412 }
1413 #endif
1414