|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.1 (the "License"). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License.
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24:
25: /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
26: /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27: /*
28: * Copyright (c) 1990, 1991, 1993
29: * The Regents of the University of California. All rights reserved.
30: *
31: * This code is derived from the Stanford/CMU enet packet filter,
32: * (net/enet.c) distributed as part of 4.3BSD, and code contributed
33: * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
34: * Berkeley Laboratory.
35: *
36: * Redistribution and use in source and binary forms, with or without
37: * modification, are permitted provided that the following conditions
38: * are met:
39: * 1. Redistributions of source code must retain the above copyright
40: * notice, this list of conditions and the following disclaimer.
41: * 2. Redistributions in binary form must reproduce the above copyright
42: * notice, this list of conditions and the following disclaimer in the
43: * documentation and/or other materials provided with the distribution.
44: * 3. All advertising materials mentioning features or use of this software
45: * must display the following acknowledgement:
46: * This product includes software developed by the University of
47: * California, Berkeley and its contributors.
48: * 4. Neither the name of the University nor the names of its contributors
49: * may be used to endorse or promote products derived from this software
50: * without specific prior written permission.
51: *
52: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62: * SUCH DAMAGE.
63: *
64: * @(#)bpf.c 8.4 (Berkeley) 1/9/95
65: */
66:
67: #include "bpfilter.h"
68:
69: #include <sys/param.h>
70: #include <sys/systm.h>
71: #include <sys/mbuf.h>
72: #include <sys/buf.h>
73: #include <sys/time.h>
74: #include <sys/proc.h>
75: #include <sys/user.h>
76: #include <sys/ioctl.h>
77:
78: #include <sys/file.h>
79: #if defined(sparc) && BSD < 199103
80: #include <sys/stream.h>
81: #endif
82: #include <sys/tty.h>
83: #include <sys/uio.h>
84:
85: #include <sys/protosw.h>
86: #include <sys/socket.h>
87: #include <net/if.h>
88:
89: #include <net/bpf.h>
90: #include <net/bpfdesc.h>
91:
92: #include <sys/errno.h>
93:
94: #include <netinet/in.h>
95: #include <netinet/if_ether.h>
96: #include <sys/kernel.h>
97:
98: /*
99: * Older BSDs don't have kernel malloc.
100: */
101: #if BSD < 199103
102: extern bcopy();
103: static caddr_t bpf_alloc();
104: #include <net/bpf_compat.h>
105: #define BPF_BUFSIZE (MCLBYTES-8)
106: #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
107: #else
108: #include <sys/malloc.h>
109: #define BPF_BUFSIZE 4096
110: #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
111: #endif
112:
113: /*
114: * The default read buffer size is patchable.
115: */
116: int bpf_bufsize = BPF_BUFSIZE;
117:
118: /*
119: * bpf_iflist is the list of interfaces; each corresponds to an ifnet
120: * bpf_dtab holds the descriptors, indexed by minor device #
121: */
122: struct bpf_if *bpf_iflist;
123: struct bpf_d *bpf_dtab;
124: int nbpfilter = -1; /* Mark as uninitialized; BPF will init */
125:
126: /*
127: * The kernel loadable server version of the BPF driver will
128: * intialize the bpf_ops object when it is loaded.
129: */
130: bpfops_t bpfops;
131:
132: /*
133: * Mark a descriptor free by making it point to itself.
134: * This is probably cheaper than marking with a constant since
135: * the address should be in a register anyway.
136: */
137: #define D_ISFREE(d) ((d) == (d)->bd_next)
138: #define D_MARKFREE(d) ((d)->bd_next = (d))
139: #define D_MARKUSED(d) ((d)->bd_next = 0)
140:
141: /*
142: * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
143: * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
144: * size of the link header (variable length headers not yet supported).
145: */
146: void
147: bpfattach(driverp, ifp, dlt, hdrlen)
148: caddr_t *driverp;
149: struct ifnet *ifp;
150: u_int dlt, hdrlen;
151: {
152: struct bpf_if *bp;
153: int i;
154: bp = (struct bpf_if *)_MALLOC(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
155:
156: if (bp == 0)
157: panic("bpfattach");
158:
159: bp->bif_dlist = 0;
160: bp->bif_driverp = (struct bpf_if **)driverp;
161: bp->bif_ifp = ifp;
162: bp->bif_dlt = dlt;
163:
164: bp->bif_next = bpf_iflist;
165: bpf_iflist = bp;
166:
167: *bp->bif_driverp = 0;
168:
169: /*
170: * Compute the length of the bpf header. This is not necessarily
171: * equal to SIZEOF_BPF_HDR because we want to insert spacing such
172: * that the network layer header begins on a longword boundary (for
173: * performance reasons and to alleviate alignment restrictions).
174: */
175: bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
176:
177: #if 0
178: printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
179: #endif
180: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.