usb_pf.h revision 215649
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 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/dev/usb/usb_pf.h 215649 2010-11-22 01:11:28Z weongyo $
35 */
36
37#ifndef _DEV_USB_PF_H
38#define	_DEV_USB_PF_H
39
40#ifdef _KERNEL
41#include <sys/callout.h>
42#include <sys/selinfo.h>
43#include <sys/queue.h>
44#include <sys/conf.h>
45#endif
46
47typedef	int32_t	  usbpf_int32;
48typedef	u_int32_t usbpf_u_int32;
49typedef	int64_t	  usbpf_int64;
50typedef	u_int64_t usbpf_u_int64;
51
52struct usbpf_if;
53
54/*
55 * Alignment macros.  USBPF_WORDALIGN rounds up to the next
56 * even multiple of USBPF_ALIGNMENT.
57 */
58#define	USBPF_ALIGNMENT sizeof(long)
59#define	USBPF_WORDALIGN(x) (((x)+(USBPF_ALIGNMENT-1))&~(USBPF_ALIGNMENT-1))
60
61/*
62 * The instruction encodings.
63 */
64
65/* instruction classes */
66#define	USBPF_CLASS(code) ((code) & 0x07)
67#define		USBPF_LD	0x00
68#define		USBPF_LDX	0x01
69#define		USBPF_ST	0x02
70#define		USBPF_STX	0x03
71#define		USBPF_ALU	0x04
72#define		USBPF_JMP	0x05
73#define		USBPF_RET	0x06
74#define		USBPF_MISC	0x07
75
76/* ld/ldx fields */
77#define	USBPF_SIZE(code)	((code) & 0x18)
78#define		USBPF_W		0x00
79#define		USBPF_H		0x08
80#define		USBPF_B		0x10
81#define	USBPF_MODE(code)	((code) & 0xe0)
82#define		USBPF_IMM 	0x00
83#define		USBPF_ABS	0x20
84#define		USBPF_IND	0x40
85#define		USBPF_MEM	0x60
86#define		USBPF_LEN	0x80
87#define		USBPF_MSH	0xa0
88
89/* alu/jmp fields */
90#define	USBPF_OP(code)	((code) & 0xf0)
91#define		USBPF_ADD	0x00
92#define		USBPF_SUB	0x10
93#define		USBPF_MUL	0x20
94#define		USBPF_DIV	0x30
95#define		USBPF_OR	0x40
96#define		USBPF_AND	0x50
97#define		USBPF_LSH	0x60
98#define		USBPF_RSH	0x70
99#define		USBPF_NEG	0x80
100#define		USBPF_JA	0x00
101#define		USBPF_JEQ	0x10
102#define		USBPF_JGT	0x20
103#define		USBPF_JGE	0x30
104#define		USBPF_JSET	0x40
105#define	USBPF_SRC(code)	((code) & 0x08)
106#define		USBPF_K		0x00
107#define		USBPF_X		0x08
108
109/* ret - USBPF_K and USBPF_X also apply */
110#define	USBPF_RVAL(code)	((code) & 0x18)
111#define		USBPF_A		0x10
112
113/* misc */
114#define	USBPF_MISCOP(code) ((code) & 0xf8)
115#define		USBPF_TAX	0x00
116#define		USBPF_TXA	0x80
117
118/*
119 * The instruction data structure.
120 */
121struct usbpf_insn {
122	u_short		code;
123	u_char		jt;
124	u_char		jf;
125	usbpf_u_int32	k;
126};
127
128#ifdef _KERNEL
129
130/*
131 * Descriptor associated with each open uff file.
132 */
133
134struct usbpf_d {
135	LIST_ENTRY(usbpf_d) ud_next;	/* Linked list of descriptors */
136	/*
137	 * Buffer slots: two memory buffers store the incoming packets.
138	 *   The model has three slots.  Sbuf is always occupied.
139	 *   sbuf (store) - Receive interrupt puts packets here.
140	 *   hbuf (hold) - When sbuf is full, put buffer here and
141	 *                 wakeup read (replace sbuf with fbuf).
142	 *   fbuf (free) - When read is done, put buffer here.
143	 * On receiving, if sbuf is full and fbuf is 0, packet is dropped.
144	 */
145	caddr_t		ud_sbuf;	/* store slot */
146	caddr_t		ud_hbuf;	/* hold slot */
147	caddr_t		ud_fbuf;	/* free slot */
148	int		ud_slen;	/* current length of store buffer */
149	int		ud_hlen;	/* current length of hold buffer */
150
151	int		ud_bufsize;	/* absolute length of buffers */
152
153	struct usbpf_if *ud_bif;	/* interface descriptor */
154	u_long		ud_rtout;	/* Read timeout in 'ticks' */
155	struct usbpf_insn *ud_rfilter;	/* read filter code */
156	struct usbpf_insn *ud_wfilter;	/* write filter code */
157	void		*ud_bfilter;	/* binary filter code */
158	u_int64_t	ud_rcount;	/* number of packets received */
159	u_int64_t	ud_dcount;	/* number of packets dropped */
160
161	u_char		ud_promisc;	/* true if listening promiscuously */
162	u_char		ud_state;	/* idle, waiting, or timed out */
163	u_char		ud_immediate;	/* true to return on packet arrival */
164	int		ud_hdrcmplt;	/* false to fill in src lladdr automatically */
165	int		ud_direction;	/* select packet direction */
166	int		ud_tstamp;	/* select time stamping function */
167	int		ud_feedback;	/* true to feed back sent packets */
168	int		ud_async;	/* non-zero if packet reception should generate signal */
169	int		ud_sig;		/* signal to send upon packet reception */
170	struct sigio *	ud_sigio;	/* information for async I/O */
171	struct selinfo	ud_sel;		/* bsd select info */
172	struct mtx	ud_mtx;		/* mutex for this descriptor */
173	struct callout	ud_callout;	/* for USBPF timeouts with select */
174	struct label	*ud_label;	/* MAC label for descriptor */
175	u_int64_t	ud_fcount;	/* number of packets which matched filter */
176	pid_t		ud_pid;		/* PID which created descriptor */
177	int		ud_locked;	/* true if descriptor is locked */
178	u_int		ud_bufmode;	/* Current buffer mode. */
179	u_int64_t	ud_wcount;	/* number of packets written */
180	u_int64_t	ud_wfcount;	/* number of packets that matched write filter */
181	u_int64_t	ud_wdcount;	/* number of packets dropped during a write */
182	u_int64_t	ud_zcopy;	/* number of zero copy operations */
183	u_char		ud_compat32;	/* 32-bit stream on LP64 system */
184};
185
186#define	USBPFD_LOCK(ud)		mtx_lock(&(ud)->ud_mtx)
187#define	USBPFD_UNLOCK(ud)	mtx_unlock(&(ud)->ud_mtx)
188#define	USBPFD_LOCK_ASSERT(ud)	mtx_assert(&(ud)->ud_mtx, MA_OWNED)
189
190/*
191 * Descriptor associated with each attached hardware interface.
192 */
193struct usbpf_if {
194	LIST_ENTRY(usbpf_if) uif_next; /* list of all interfaces */
195	LIST_HEAD(, usbpf_d) uif_dlist;	/* descriptor list */
196	u_int uif_hdrlen;		/* length of link header */
197	struct usb_bus *uif_ubus;	/* corresponding interface */
198	struct mtx	uif_mtx;	/* mutex for interface */
199};
200
201#define	USBPFIF_LOCK(uif)	mtx_lock(&(uif)->uif_mtx)
202#define	USBPFIF_UNLOCK(uif)	mtx_unlock(&(uif)->uif_mtx)
203
204#endif
205
206/*
207 * Structure prepended to each packet.
208 */
209struct usbpf_ts {
210	usbpf_int64	ut_sec;		/* seconds */
211	usbpf_u_int64	ut_frac;	/* fraction */
212};
213struct usbpf_xhdr {
214	struct usbpf_ts	uh_tstamp;	/* time stamp */
215	usbpf_u_int32	uh_caplen;	/* length of captured portion */
216	usbpf_u_int32	uh_datalen;	/* original length of packet */
217	u_short		uh_hdrlen;	/* length of uff header (this struct
218					   plus alignment padding) */
219};
220
221#define	USBPF_BUFMODE_BUFFER	1	/* Kernel buffers with read(). */
222#define	USBPF_BUFMODE_ZBUF	2	/* Zero-copy buffers. */
223
224struct usbpf_pkthdr {
225	int		up_busunit;	/* Host controller unit number */
226	u_char		up_address;	/* USB device address */
227	u_char		up_endpoint;	/* USB endpoint */
228	u_char		up_type;	/* points SUBMIT / DONE */
229	u_char		up_xfertype;	/* Transfer type */
230	u_int32_t	up_flags;	/* Transfer flags */
231#define	USBPF_FLAG_FORCE_SHORT_XFER	(1 << 0)
232#define	USBPF_FLAG_SHORT_XFER_OK	(1 << 1)
233#define	USBPF_FLAG_SHORT_FRAMES_OK	(1 << 2)
234#define	USBPF_FLAG_PIPE_BOF		(1 << 3)
235#define	USBPF_FLAG_PROXY_BUFFER		(1 << 4)
236#define	USBPF_FLAG_EXT_BUFFER		(1 << 5)
237#define	USBPF_FLAG_MANUAL_STATUS	(1 << 6)
238#define	USBPF_FLAG_NO_PIPE_OK		(1 << 7)
239#define	USBPF_FLAG_STALL_PIPE		(1 << 8)
240	u_int32_t	up_status;	/* Transfer status */
241#define	USBPF_STATUS_OPEN		(1 << 0)
242#define	USBPF_STATUS_TRANSFERRING	(1 << 1)
243#define	USBPF_STATUS_DID_DMA_DELAY	(1 << 2)
244#define	USBPF_STATUS_DID_CLOSE		(1 << 3)
245#define	USBPF_STATUS_DRAINING		(1 << 4)
246#define	USBPF_STATUS_STARTED		(1 << 5)
247#define	USBPF_STATUS_BW_RECLAIMED	(1 << 6)
248#define	USBPF_STATUS_CONTROL_XFR	(1 << 7)
249#define	USBPF_STATUS_CONTROL_HDR	(1 << 8)
250#define	USBPF_STATUS_CONTROL_ACT	(1 << 9)
251#define	USBPF_STATUS_CONTROL_STALL	(1 << 10)
252#define	USBPF_STATUS_SHORT_FRAMES_OK	(1 << 11)
253#define	USBPF_STATUS_SHORT_XFER_OK	(1 << 12)
254#if USB_HAVE_BUSDMA
255#define	USBPF_STATUS_BDMA_ENABLE	(1 << 13)
256#define	USBPF_STATUS_BDMA_NO_POST_SYNC	(1 << 14)
257#define	USBPF_STATUS_BDMA_SETUP		(1 << 15)
258#endif
259#define	USBPF_STATUS_ISOCHRONOUS_XFR	(1 << 16)
260#define	USBPF_STATUS_CURR_DMA_SET	(1 << 17)
261#define	USBPF_STATUS_CAN_CANCEL_IMMED	(1 << 18)
262#define	USBPF_STATUS_DOING_CALLBACK	(1 << 19)
263	u_int32_t	up_length;	/* Total data length (submit/actual) */
264	u_int32_t	up_frames;	/* USB frame number (submit/actual) */
265	u_int32_t	up_error;	/* usb_error_t */
266	u_int32_t	up_interval;	/* for interrupt and isoc */
267	/* sizeof(struct usbpf_pkthdr) == 128 bytes */
268	u_char		up_reserved[96];
269};
270
271struct usbpf_version {
272	u_short		uv_major;
273	u_short		uv_minor;
274};
275#define	USBPF_MAJOR_VERSION	1
276#define	USBPF_MINOR_VERSION	1
277
278#define	USBPF_IFNAMSIZ	32
279struct usbpf_ifreq {
280	/* bus name, e.g. "usbus0" */
281	char		ufr_name[USBPF_IFNAMSIZ];
282};
283
284/*
285 *  Structure for UIOCSETF.
286 */
287struct usbpf_program {
288	u_int			uf_len;
289	struct usbpf_insn	*uf_insns;
290};
291
292/*
293 * Struct returned by UIOCGSTATS.
294 */
295struct usbpf_stat {
296	u_int us_recv;		/* number of packets received */
297	u_int us_drop;		/* number of packets dropped */
298};
299
300#define	UIOCGBLEN	_IOR('U', 102, u_int)
301#define	UIOCSBLEN	_IOWR('U', 102, u_int)
302#define	UIOCSETF	_IOW('U', 103, struct usbpf_program)
303#define	UIOCSETIF	_IOW('U', 108, struct usbpf_ifreq)
304#define	UIOCSRTIMEOUT	_IOW('U', 109, struct timeval)
305#define	UIOCGRTIMEOUT	_IOR('U', 110, struct timeval)
306#define	UIOCGSTATS	_IOR('U', 111, struct usbpf_stat)
307#define	UIOCVERSION	_IOR('U', 113, struct usbpf_version)
308#define	UIOCSETWF	_IOW('U', 123, struct usbpf_program)
309
310#define	USBPF_XFERTAP_SUBMIT	0
311#define	USBPF_XFERTAP_DONE	1
312
313#ifdef _KERNEL
314void	usbpf_attach(struct usb_bus *, struct usbpf_if **);
315void	usbpf_detach(struct usb_bus *);
316void	usbpf_xfertap(struct usb_xfer *, int);
317#endif
318
319#endif
320