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