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