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