xref: /illumos-gate/usr/src/uts/common/io/bpf/net/bpfdesc.h (revision 46b592853d0f4f11781b6b0a7533f267c6aee132)
1 /*	$NetBSD: bpfdesc.h,v 1.29 2009/03/14 14:46:10 dsl Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from the Stanford/CMU enet packet filter,
8  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10  * Berkeley Laboratory.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)bpfdesc.h	8.1 (Berkeley) 6/10/93
37  *
38  * @(#) Header: bpfdesc.h,v 1.14 96/06/16 22:28:07 leres Exp  (LBL)
39  */
40 /*
41  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
42  * Use is subject to license terms.
43  */
44 
45 #ifndef _NET_BPFDESC_H_
46 #define	_NET_BPFDESC_H_
47 
48 #include <net/if.h>			/* for IFNAMSIZ */
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/queue.h>
52 
53 /*
54  * Descriptor associated with each open bpf file.
55  */
56 struct bpf_d {
57 	LIST_ENTRY(bpf_d) bd_list;	/* List of bpf_d */
58 	LIST_ENTRY(bpf_d) bd_next;	/* List attaced to bif_if */
59 	/*
60 	 * Buffer slots: two mbuf clusters buffer the incoming packets.
61 	 *   The model has three slots.  Sbuf is always occupied.
62 	 *   sbuf (store) - Receive interrupt puts packets here.
63 	 *   hbuf (hold) - When sbuf is full, put cluster here and
64 	 *		   wakeup read (replace sbuf with fbuf).
65 	 *   fbuf (free) - When read is done, put cluster here.
66 	 * On receiving, if sbuf is full and fbuf is 0, packet is dropped.
67 	 */
68 	void *		bd_sbuf;	/* store slot */
69 	void *		bd_hbuf;	/* hold slot */
70 	void *		bd_fbuf;	/* free slot */
71 	int 		bd_slen;	/* current length of store buffer */
72 	int 		bd_hlen;	/* current length of hold buffer */
73 
74 	int		bd_bufsize;	/* absolute length of buffers */
75 
76 	struct bpf_if 	*bd_bif;	/* interface descriptor */
77 	ulong_t		bd_rtout;	/* Read timeout in 'ticks' */
78 	struct bpf_insn *bd_filter; 	/* filter code */
79 	size_t		bd_filter_size;
80 	ulong_t		bd_rcount;	/* number of packets received */
81 	ulong_t		bd_dcount;	/* number of packets dropped */
82 	ulong_t		bd_ccount;	/* number of packets captured */
83 
84 	uchar_t		bd_promisc;	/* true if listening promiscuously */
85 	uchar_t		bd_state;	/* idle, waiting, or timed out */
86 	uchar_t		bd_immediate;	/* true to return on packet arrival */
87 	int		bd_hdrcmplt;	/* false to fill in src lladdr */
88 	int		bd_seesent;	/* true if bpf should see sent pkts */
89 	int		bd_async;	/* non-zero if packet reception .. */
90 					/* .. should generate signal */
91 	int		bd_nonblock;	/* non-zero for non-blocking read */
92 	pid_t		bd_pgid;	/* process or group id for signal */
93 	int		bd_timedout;
94 	struct pollhead	bd_poll;
95 	timeout_id_t	bd_callout;	/* for BPF timeouts with select */
96 	pid_t		bd_pid;		/* corresponding PID */
97 	void		*bd_sih;	/* soft interrupt handle */
98 	/*
99 	 * Solaris specific bits after this.
100 	 */
101 	kmutex_t	bd_lock;
102 	kcondvar_t	bd_wait;
103 	uintptr_t	bd_mcip;	/* Where mac_client_handle_t gets put */
104 	uintptr_t	bd_promisc_handle;
105 	minor_t		bd_dev;		/* device number for this handle */
106 	int		bd_fmode;	/* flags from bpfopen */
107 	zoneid_t	bd_zone;	/* zoneid of the opening process */
108 	int		bd_inuse;
109 	int		bd_waiting;
110 	/*
111 	 * bd_promisc_flags is used to store the promiscuous state of the
112 	 * the interface in BPF so that the correct mode of operation can
113 	 * be kept across changing DLT or network interface.
114 	 */
115 	int		bd_promisc_flags;
116 };
117 
118 
119 /* Values for bd_state */
120 #define	BPF_IDLE	0		/* no select in progress */
121 #define	BPF_WAITING	1		/* waiting for read timeout in select */
122 #define	BPF_TIMED_OUT	2		/* read timeout has expired in select */
123 
124 /*
125  * Description associated with the external representation of each
126  * open bpf file.
127  */
128 struct bpf_d_ext {
129 	int32_t		bde_bufsize;
130 	uint8_t		bde_promisc;
131 	uint8_t		bde_state;
132 	uint8_t		bde_immediate;
133 	int32_t		bde_hdrcmplt;
134 	int32_t		bde_seesent;
135 	pid_t		bde_pid;
136 	uint64_t	bde_rcount;		/* number of packets received */
137 	uint64_t	bde_dcount;		/* number of packets dropped */
138 	uint64_t	bde_ccount;		/* number of packets captured */
139 	char		bde_ifname[IFNAMSIZ];
140 };
141 
142 /*
143  * Access to "layer 2" networking is provided through each such provider
144  * delcaring a set of functions to use in the structure below. It has been
145  * modeled around what's required to use the mac layer. All of the functions
146  * below must be declared, even if only filled by a stub function.
147  */
148 typedef struct bpf_provider_s {
149 	int		bpr_unit;
150 	int		(*bpr_open)(const char *, uintptr_t *, zoneid_t);
151 	void		(*bpr_close)(uintptr_t);
152 	const char 	*(*bpr_name)(uintptr_t);
153 	int		(*bpr_type)(uintptr_t);
154 	void		(*bpr_sdu_get)(uintptr_t, uint_t *);
155 	int		(*bpr_tx)(uintptr_t, mblk_t *);
156 	uintptr_t	(*bpr_promisc_add)(uintptr_t, int, void *, uintptr_t *,
157 			    int);
158 	void		(*bpr_promisc_remove)(uintptr_t);
159 	int		(*bpr_getlinkid)(const char *, datalink_id_t *,
160 			    zoneid_t);
161 	void		(*bpr_client_close)(uintptr_t);
162 	const char 	*(*bpr_client_name)(uintptr_t);
163 	int		(*bpr_client_open)(uintptr_t, uintptr_t *);
164 } bpf_provider_t;
165 
166 typedef struct bpf_provider_list {
167 	LIST_ENTRY(bpf_provider_list)	bpl_next;
168 	bpf_provider_t			*bpl_what;
169 } bpf_provider_list_t;
170 
171 /*
172  * The bpr_field from bpf_provider_t expects an integer that comes from
173  * the list of defines below.
174  */
175 #define	BPR_MAC		1
176 #define	BPR_IPNET	2
177 
178 #define	MBPF_OPEN(_m, _n, _p, _z)	(_m)->bpr_open(_n, (uintptr_t *)_p, _z)
179 #define	MBPF_CLOSE(_m, _h)		(_m)->bpr_close(_h)
180 #define	MBPF_NAME(_m, _h)		(_m)->bpr_name(_h)
181 #define	MBPF_TYPE(_m, _h)		(_m)->bpr_type(_h)
182 #define	MBPF_SDU_GET(_m, _h, _p)	(_m)->bpr_sdu_get(_h, _p)
183 #define	MBPF_TX(_m, _h, _pkt)		(_m)->bpr_tx(_h, _pkt)
184 #define	MBPF_PROMISC_ADD(_m, _h, _o, _d, _p, _f) \
185 				(_m)->bpr_promisc_add(_h, _o, _d, _p, _f)
186 #define	MBPF_PROMISC_REMOVE(_m, _h)	(_m)->bpr_promisc_remove(_h)
187 #define	MBPF_GET_LINKID(_m, _n, _ip, _z) \
188 					(_m)->bpr_getlinkid(_n, _ip, _z)
189 #define	MBPF_CLIENT_CLOSE(_m, _h)	(_m)->bpr_client_close(_h)
190 #define	MBPF_CLIENT_NAME(_m, _h)	(_m)->bpr_client_name(_h)
191 #define	MBPF_CLIENT_OPEN(_m, _h, _p)	(_m)->bpr_client_open((uintptr_t)_h, \
192 					    (uintptr_t *)_p)
193 
194 /*
195  * Descriptor associated with each attached hardware interface.
196  */
197 struct bpf_if {
198 	TAILQ_ENTRY(bpf_if) bif_next;	/* list of all interfaces */
199 	LIST_HEAD(, bpf_d) bif_dlist;	/* list of all descriptors att'd */
200 	uint_t		bif_dlt;	/* link layer type */
201 	uint_t		bif_hdrlen;	/* length of header (with padding) */
202 	/*
203 	 * Solaris specific bits after this.
204 	 */
205 	uintptr_t	bif_ifp;	/* correspoding interface */
206 	datalink_id_t	bif_linkid;
207 	kmutex_t	bif_lock;
208 	zoneid_t	bif_zoneid;	/* zone that the interface is in */
209 	int		bif_inuse;
210 	bpf_provider_t	bif_mac;
211 	char		bif_ifname[LIFNAMSIZ+1];
212 };
213 
214 #ifdef _KERNEL
215 typedef struct bpf_kstats_s {
216 	kstat_named_t	kp_read_wait;
217 	kstat_named_t	kp_write_ok;
218 	kstat_named_t	kp_write_error;
219 	kstat_named_t	kp_receive;
220 	kstat_named_t	kp_capture;
221 	kstat_named_t	kp_dropped;
222 } bpf_kstats_t;
223 
224 int	 bpf_setf(struct bpf_d *, struct bpf_program *);
225 #endif
226 
227 typedef void	(*bpf_attach_fn_t)(uintptr_t, int, zoneid_t, int);
228 typedef void	(*bpf_detach_fn_t)(uintptr_t);
229 typedef int	(*bpf_provider_reg_fn_t)(bpf_provider_t *);
230 
231 extern bpf_provider_t	*bpf_find_provider_by_id(int);
232 extern void		bpf_open_zone(const zoneid_t);
233 extern int		bpf_provider_tickle(char *, zoneid_t);
234 
235 #endif /* !_NET_BPFDESC_H_ */
236