xref: /netbsd/sys/net/bpfdesc.h (revision 6dc5e775)
1 /*	$NetBSD: bpfdesc.h,v 1.48 2021/06/09 15:44:15 martin 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 #ifndef _NET_BPFDESC_H_
42 #define _NET_BPFDESC_H_
43 
44 #include <sys/callout.h>
45 #include <sys/selinfo.h>		/* for struct selinfo */
46 #include <net/if.h>			/* for IFNAMSIZ */
47 #include <net/bpfjit.h>			/* for bpfjit_function_t */
48 #ifdef _KERNEL
49 #include <sys/pslist.h>
50 #include <sys/mutex.h>
51 #include <sys/condvar.h>
52 #include <sys/psref.h>
53 #endif
54 
55 struct bpf_filter {
56 	struct bpf_insn *bf_insn; 	/* filter code */
57 	size_t		bf_size;
58 	bpfjit_func_t	bf_jitcode;	/* compiled filter program */
59 };
60 
61 /*
62  * Descriptor associated with each open bpf file.
63  */
64 struct bpf_d {
65 	/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
66 	struct bpf_d	*_bd_next;	/* Linked list of descriptors */
67 	/*
68 	 * Buffer slots: two mbuf clusters buffer the incoming packets.
69 	 *   The model has three slots.  Sbuf is always occupied.
70 	 *   sbuf (store) - Receive interrupt puts packets here.
71 	 *   hbuf (hold) - When sbuf is full, put cluster here and
72 	 *                 wakeup read (replace sbuf with fbuf).
73 	 *   fbuf (free) - When read is done, put cluster here.
74 	 * On receiving, if sbuf is full and fbuf is 0, packet is dropped.
75 	 */
76 	void *		bd_sbuf;	/* store slot */
77 	void *		bd_hbuf;	/* hold slot */
78 	void *		bd_fbuf;	/* free slot */
79 	int 		bd_slen;	/* current length of store buffer */
80 	int 		bd_hlen;	/* current length of hold buffer */
81 
82 	int		bd_bufsize;	/* absolute length of buffers */
83 
84 	struct bpf_if *	bd_bif;		/* interface descriptor */
85 	u_long		bd_rtout;	/* Read timeout in 'ticks' */
86 	/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
87 	struct bpf_insn *_bd_filter; 	/* filter code */
88 	/*
89 	 * XXX we should make the counters per-CPU once we retire kvm(3) users
90 	 * that directly access them.
91 	 */
92 	u_long		bd_rcount;	/* number of packets received */
93 	u_long		bd_dcount;	/* number of packets dropped */
94 	u_long		bd_ccount;	/* number of packets captured */
95 
96 	u_char		bd_promisc;	/* true if listening promiscuously */
97 	u_char		bd_state;	/* idle, waiting, or timed out */
98 	u_char		bd_immediate;	/* true to return on packet arrival */
99 	int		bd_hdrcmplt;	/* false to fill in src lladdr */
100 	u_int		bd_direction;	/* select packet direction */
101 	int 		bd_feedback;	/* true to feed back sent packets */
102 	int		bd_async;	/* non-zero if packet reception should generate signal */
103 	pid_t		bd_pgid;	/* process or group id for signal */
104 #if BSD < 199103
105 	u_char		bd_selcoll;	/* true if selects collide */
106 	int		bd_timedout;
107 	struct proc *	bd_selproc;	/* process that last selected us */
108 #else
109 	u_char		bd_pad;		/* explicit alignment */
110 	struct selinfo	bd_sel;		/* bsd select info */
111 #endif
112 	callout_t	bd_callout;	/* for BPF timeouts with select */
113 	pid_t		bd_pid;		/* corresponding PID */
114 	/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
115 	LIST_ENTRY(bpf_d) _bd_list;	/* list of all BPF's */
116 	/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
117 	void		*bd_sih;	/* soft interrupt handle */
118 	struct timespec bd_atime;	/* access time */
119 	struct timespec bd_mtime;	/* modification time */
120 	struct timespec bd_btime;	/* birth time */
121 #ifdef _LP64
122 	int		bd_compat32;	/* 32-bit stream on LP64 system */
123 #endif
124 	/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
125 	bpfjit_func_t	bd_jitcode;	/* compiled filter program */
126 	struct bpf_filter *bd_rfilter;
127 	struct bpf_filter *bd_wfilter;
128 	int		bd_locked;
129 #ifdef _KERNEL
130 	struct pslist_entry	bd_bif_dlist_entry; /* For bpf_if */
131 	struct pslist_entry	bd_bpf_dlist_entry; /* For the global list */
132 	kmutex_t	*bd_mtx;
133 	kmutex_t	*bd_buf_mtx;
134 	kcondvar_t	bd_cv;
135 #endif
136 };
137 
138 
139 /* Values for bd_state */
140 #define BPF_IDLE	0		/* no select in progress */
141 #define BPF_WAITING	1		/* waiting for read timeout in select */
142 #define BPF_TIMED_OUT	2		/* read timeout has expired in select */
143 
144 /*
145  * Description associated with the external representation of each
146  * open bpf file.
147  */
148 struct bpf_d_ext {
149 	int32_t		bde_bufsize;
150 	uint8_t		bde_promisc;
151 	uint8_t		bde_state;
152 	uint8_t		bde_immediate;
153 	int32_t		bde_hdrcmplt;
154 	uint32_t	bde_direction;
155 	pid_t		bde_pid;
156 	uint64_t	bde_rcount;		/* number of packets received */
157 	uint64_t	bde_dcount;		/* number of packets dropped */
158 	uint64_t	bde_ccount;		/* number of packets captured */
159 	char		bde_ifname[IFNAMSIZ];
160 	int		bde_locked;
161 };
162 
163 
164 /*
165  * Record for each event tracker watching a tap point
166  */
167 struct bpf_event_tracker {
168 	SLIST_ENTRY(bpf_event_tracker) bet_entries;
169 	void (*bet_notify)(struct bpf_if *, struct ifnet *, int, int);
170 };
171 
172 /*
173  * Descriptor associated with each attached hardware interface.
174  */
175 struct bpf_if {
176 	/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
177 	struct bpf_if *_bif_next;	/* list of all interfaces */
178 	struct bpf_d *_bif_dlist;	/* descriptor list */
179 	struct bpf_if **bif_driverp;	/* pointer into softc */
180 	u_int bif_dlt;			/* link layer type */
181 	u_int bif_hdrlen;		/* length of header (with padding) */
182 	struct ifnet *bif_ifp;		/* corresponding interface */
183 	void *bif_si;
184 	struct mbuf *bif_mbuf_head;
185 	struct mbuf *bif_mbuf_tail;
186 #ifdef _KERNEL
187 	struct pslist_entry bif_iflist_entry;
188 	struct pslist_head bif_dlist_head;
189 	struct psref_target bif_psref;
190 	SLIST_HEAD(, bpf_event_tracker) bif_trackers;
191 #endif
192 };
193 
194 #endif /* !_NET_BPFDESC_H_ */
195