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