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