xref: /netbsd-src/sys/net/bpfdesc.h (revision 9ffb2c7b3665bd3cbb077169866941027e12a84c)
1 /*	$NetBSD: bpfdesc.h,v 1.50 2024/08/19 07:47:16 ozaki-r 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 	void		*bd_sih;	/* soft interrupt handle */
117 	struct timespec bd_atime;	/* access time */
118 	struct timespec bd_mtime;	/* modification time */
119 	struct timespec bd_btime;	/* birth time */
120 #ifdef _LP64
121 	int		bd_compat32;	/* 32-bit stream on LP64 system */
122 #endif
123 	/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
124 	bpfjit_func_t	bd_jitcode;	/* compiled filter program */
125 	struct bpf_filter *bd_rfilter;
126 	struct bpf_filter *bd_wfilter;
127 	int		bd_locked;
128 #ifdef _KERNEL
129 	struct pslist_entry	bd_bif_dlist_entry; /* For bpf_if */
130 	struct pslist_entry	bd_bpf_dlist_entry; /* For the global list */
131 	kmutex_t	*bd_mtx;
132 	kmutex_t	*bd_buf_mtx;	/* For buffers, bd_state, bd_sel and bd_cv */
133 	kcondvar_t	bd_cv;
134 #endif
135 };
136 
137 
138 /* Values for bd_state */
139 #define BPF_IDLE	0		/* no select in progress */
140 #define BPF_WAITING	1		/* waiting for read timeout in select */
141 #define BPF_TIMED_OUT	2		/* read timeout has expired in select */
142 
143 /*
144  * Description associated with the external representation of each
145  * open bpf file.
146  */
147 struct bpf_d_ext {
148 	int32_t		bde_bufsize;
149 	uint8_t		bde_promisc;
150 	uint8_t		bde_state;
151 	uint8_t		bde_immediate;
152 	int32_t		bde_hdrcmplt;
153 	uint32_t	bde_direction;
154 	pid_t		bde_pid;
155 	uint64_t	bde_rcount;		/* number of packets received */
156 	uint64_t	bde_dcount;		/* number of packets dropped */
157 	uint64_t	bde_ccount;		/* number of packets captured */
158 	char		bde_ifname[IFNAMSIZ];
159 	int		bde_locked;
160 };
161 
162 
163 /*
164  * Record for each event tracker watching a tap point
165  */
166 struct bpf_event_tracker {
167 	SLIST_ENTRY(bpf_event_tracker) bet_entries;
168 	void (*bet_notify)(struct bpf_if *, struct ifnet *, int, int);
169 };
170 
171 /*
172  * Descriptor associated with each attached hardware interface.
173  */
174 struct bpf_if {
175 	/* DEPRECATED. Keep it to avoid breaking kvm(3) users */
176 	struct bpf_if *_bif_next;	/* list of all interfaces */
177 	struct bpf_d *_bif_dlist;	/* descriptor list */
178 	struct bpf_if **bif_driverp;	/* pointer into softc */
179 	u_int bif_dlt;			/* link layer type */
180 	u_int bif_hdrlen;		/* length of header (with padding) */
181 	struct ifnet *bif_ifp;		/* corresponding interface */
182 	void *bif_si;
183 	struct mbuf *bif_mbuf_head;
184 	struct mbuf *bif_mbuf_tail;
185 #ifdef _KERNEL
186 	struct pslist_entry bif_iflist_entry;
187 	struct pslist_head bif_dlist_head;
188 	struct psref_target bif_psref;
189 	SLIST_HEAD(, bpf_event_tracker) bif_trackers;
190 #endif
191 };
192 
193 #endif /* !_NET_BPFDESC_H_ */
194