xref: /titanic_50/usr/src/uts/common/io/bpf/net/bpfdesc.h (revision 3c112a2b34403220c06c3e2fcac403358cfba168)
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  * Access to "layer 2" networking is provided through each such provider
55  * delcaring a set of functions to use in the structure below. It has been
56  * modeled around what's required to use the mac layer. All of the functions
57  * below must be declared, even if only filled by a stub function.
58  */
59 typedef struct bpf_provider_s {
60 	int		bpr_unit;
61 	int		(*bpr_open)(const char *, uintptr_t *, zoneid_t);
62 	void		(*bpr_close)(uintptr_t);
63 	const char 	*(*bpr_name)(uintptr_t);
64 	int		(*bpr_type)(uintptr_t);
65 	void		(*bpr_sdu_get)(uintptr_t, uint_t *);
66 	int		(*bpr_tx)(uintptr_t, mblk_t *);
67 	uintptr_t	(*bpr_promisc_add)(uintptr_t, int, void *, uintptr_t *,
68 			    int);
69 	void		(*bpr_promisc_remove)(uintptr_t);
70 	int		(*bpr_getlinkid)(const char *, datalink_id_t *,
71 			    zoneid_t);
72 	void		(*bpr_client_close)(uintptr_t);
73 	const char 	*(*bpr_client_name)(uintptr_t);
74 	int		(*bpr_client_open)(uintptr_t, uintptr_t *);
75 	int		(*bpr_getzone)(uintptr_t, zoneid_t *);
76 	int		(*bpr_getdlt)(uintptr_t, uint_t *);
77 } bpf_provider_t;
78 
79 typedef struct bpf_provider_list {
80 	LIST_ENTRY(bpf_provider_list)	bpl_next;
81 	bpf_provider_t			*bpl_what;
82 } bpf_provider_list_t;
83 
84 /*
85  * The bpr_field from bpf_provider_t expects an integer that comes from
86  * the list of defines below.
87  */
88 #define	BPR_MAC		1
89 #define	BPR_IPNET	2
90 
91 #define	MBPF_OPEN(_m, _n, _p, _z)	(_m)->bpr_open(_n, (uintptr_t *)_p, _z)
92 #define	MBPF_CLOSE(_m, _h)		(_m)->bpr_close(_h)
93 #define	MBPF_NAME(_m, _h)		(_m)->bpr_name(_h)
94 #define	MBPF_TYPE(_m, _h)		(_m)->bpr_type(_h)
95 #define	MBPF_SDU_GET(_m, _h, _p)	(_m)->bpr_sdu_get(_h, _p)
96 #define	MBPF_TX(_m, _h, _pkt)		(_m)->bpr_tx(_h, _pkt)
97 #define	MBPF_PROMISC_ADD(_m, _h, _o, _d, _p, _f) \
98 				(_m)->bpr_promisc_add(_h, _o, _d, _p, _f)
99 #define	MBPF_PROMISC_REMOVE(_m, _h)	(_m)->bpr_promisc_remove(_h)
100 #define	MBPF_GET_LINKID(_m, _n, _ip, _z) \
101 					(_m)->bpr_getlinkid(_n, _ip, _z)
102 #define	MBPF_CLIENT_CLOSE(_m, _h)	(_m)->bpr_client_close(_h)
103 #define	MBPF_CLIENT_NAME(_m, _h)	(_m)->bpr_client_name(_h)
104 #define	MBPF_CLIENT_OPEN(_m, _h, _p)	(_m)->bpr_client_open((uintptr_t)_h, \
105 					    (uintptr_t *)_p)
106 #define	MBPF_GET_ZONE(_m, _h, _zp)	(_m)->bpr_getzone(_h, _zp)
107 #define	MBPF_GET_DLT(_m, _h, _dp)	(_m)->bpr_getdlt(_h, _dp);
108 #define	MBPF_GET_HDRLEN(_m, _h, _dp)	(_m)->bpr_gethdrlen(_h, _dp);
109 
110 
111 /*
112  * Descriptor associated with each open bpf file.
113  */
114 struct bpf_d {
115 	LIST_ENTRY(bpf_d) bd_list;	/* List of bpf_d */
116 	LIST_ENTRY(bpf_d) bd_next;	/* List attaced to bif_if */
117 	/*
118 	 * Buffer slots: two mbuf clusters buffer the incoming packets.
119 	 *   The model has three slots.  Sbuf is always occupied.
120 	 *   sbuf (store) - Receive interrupt puts packets here.
121 	 *   hbuf (hold) - When sbuf is full, put cluster here and
122 	 *		   wakeup read (replace sbuf with fbuf).
123 	 *   fbuf (free) - When read is done, put cluster here.
124 	 * On receiving, if sbuf is full and fbuf is 0, packet is dropped.
125 	 */
126 	void *		bd_sbuf;	/* store slot */
127 	void *		bd_hbuf;	/* hold slot */
128 	void *		bd_fbuf;	/* free slot */
129 	int 		bd_slen;	/* current length of store buffer */
130 	int 		bd_hlen;	/* current length of hold buffer */
131 
132 	int		bd_bufsize;	/* absolute length of buffers */
133 
134 	uintptr_t 	bd_bif;		/* interface pointer */
135 	ulong_t		bd_rtout;	/* Read timeout in 'ticks' */
136 	struct bpf_insn *bd_filter; 	/* filter code */
137 	size_t		bd_filter_size;
138 	ulong_t		bd_rcount;	/* number of packets received */
139 	ulong_t		bd_dcount;	/* number of packets dropped */
140 	ulong_t		bd_ccount;	/* number of packets captured */
141 
142 	uchar_t		bd_promisc;	/* true if listening promiscuously */
143 	uchar_t		bd_state;	/* idle, waiting, or timed out */
144 	uchar_t		bd_immediate;	/* true to return on packet arrival */
145 	int		bd_hdrcmplt;	/* false to fill in src lladdr */
146 	int		bd_seesent;	/* true if bpf should see sent pkts */
147 	int		bd_async;	/* non-zero if packet reception .. */
148 					/* .. should generate signal */
149 	int		bd_nonblock;	/* non-zero for non-blocking read */
150 	pid_t		bd_pgid;	/* process or group id for signal */
151 	int		bd_timedout;
152 	struct pollhead	bd_poll;
153 	timeout_id_t	bd_callout;	/* for BPF timeouts with select */
154 	pid_t		bd_pid;		/* corresponding PID */
155 	void		*bd_sih;	/* soft interrupt handle */
156 	/*
157 	 * Solaris specific bits after this.
158 	 */
159 	kmutex_t	bd_lock;
160 	kcondvar_t	bd_wait;
161 	uintptr_t	bd_mh;		/* where mac_handle gets put */
162 	uintptr_t	bd_mcip;	/* Where mac_client_handle_t gets put */
163 	uintptr_t	bd_promisc_handle;
164 	minor_t		bd_dev;		/* device number for this handle */
165 	int		bd_fmode;	/* flags from bpfopen */
166 	zoneid_t	bd_zone;	/* zoneid of the opening process */
167 	int		bd_inuse;
168 	int		bd_waiting;
169 	char		bd_ifname[LIFNAMSIZ];
170 	int		bd_dlt;
171 	int		bd_hdrlen;
172 	bpf_provider_t	bd_mac;
173 	datalink_id_t	bd_linkid;
174 	/*
175 	 * bd_promisc_flags is used to store the promiscuous state of the
176 	 * the interface in BPF so that the correct mode of operation can
177 	 * be kept across changing DLT or network interface.
178 	 */
179 	int		bd_promisc_flags;
180 };
181 
182 
183 /* Values for bd_state */
184 #define	BPF_IDLE	0		/* no select in progress */
185 #define	BPF_WAITING	1		/* waiting for read timeout in select */
186 #define	BPF_TIMED_OUT	2		/* read timeout has expired in select */
187 
188 /*
189  * Description associated with the external representation of each
190  * open bpf file.
191  */
192 struct bpf_d_ext {
193 	int32_t		bde_bufsize;
194 	uint8_t		bde_promisc;
195 	uint8_t		bde_state;
196 	uint8_t		bde_immediate;
197 	int32_t		bde_hdrcmplt;
198 	int32_t		bde_seesent;
199 	pid_t		bde_pid;
200 	uint64_t	bde_rcount;		/* number of packets received */
201 	uint64_t	bde_dcount;		/* number of packets dropped */
202 	uint64_t	bde_ccount;		/* number of packets captured */
203 	char		bde_ifname[IFNAMSIZ];
204 };
205 
206 #ifdef _KERNEL
207 typedef struct bpf_kstats_s {
208 	kstat_named_t	kp_read_wait;
209 	kstat_named_t	kp_write_ok;
210 	kstat_named_t	kp_write_error;
211 	kstat_named_t	kp_receive;
212 	kstat_named_t	kp_capture;
213 	kstat_named_t	kp_dropped;
214 } bpf_kstats_t;
215 
216 int	 bpf_setf(struct bpf_d *, struct bpf_program *);
217 #endif
218 
219 typedef void	(*bpf_attach_fn_t)(uintptr_t, int, zoneid_t, int);
220 typedef void	(*bpf_detach_fn_t)(uintptr_t);
221 typedef int	(*bpf_provider_reg_fn_t)(bpf_provider_t *);
222 typedef	LIST_HEAD(, bpf_provider_list) bpf_provider_head_t;
223 
224 extern bpf_provider_t	*bpf_find_provider_by_id(int);
225 extern int		bpf_provider_tickle(char *, zoneid_t);
226 extern bpf_provider_head_t bpf_providers;
227 
228 #endif /* !_NET_BPFDESC_H_ */
229