xref: /openbsd/sys/net/if_var.h (revision 938ff1ae)
1 /*	$OpenBSD: if_var.h,v 1.132 2023/12/23 10:52:54 bluhm Exp $	*/
2 /*	$NetBSD: if.h,v 1.23 1996/05/07 02:40:27 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 2012-2013 Henning Brauer <henning@openbsd.org>
6  * Copyright (c) 1982, 1986, 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if.h	8.1 (Berkeley) 6/10/93
34  */
35 
36 #ifndef _NET_IF_VAR_H_
37 #define _NET_IF_VAR_H_
38 
39 #ifdef _KERNEL
40 
41 #include <sys/queue.h>
42 #include <sys/mbuf.h>
43 #include <sys/srp.h>
44 #include <sys/refcnt.h>
45 #include <sys/task.h>
46 #include <sys/time.h>
47 #include <sys/timeout.h>
48 
49 #include <net/ifq.h>
50 
51 /*
52  * Structures defining a network interface, providing a packet
53  * transport mechanism (ala level 0 of the PUP protocols).
54  *
55  * Each interface accepts output datagrams of a specified maximum
56  * length, and provides higher level routines with input datagrams
57  * received from its medium.
58  *
59  * Output occurs when the routine if_output is called, with four parameters:
60  *	(*ifp->if_output)(ifp, m, dst, rt)
61  * Here m is the mbuf chain to be sent and dst is the destination address.
62  * The output routine encapsulates the supplied datagram if necessary,
63  * and then transmits it on its medium.
64  *
65  * On input, each interface unwraps the data received by it, and either
66  * places it on the input queue of an internetwork datagram routine
67  * and posts the associated software interrupt, or passes the datagram to a raw
68  * packet input routine.
69  *
70  * Routines exist for locating interfaces by their addresses
71  * or for locating an interface on a certain network, as well as more general
72  * routing and gateway routines maintaining information used to locate
73  * interfaces.  These routines live in the files if.c and route.c
74  */
75 
76 /*
77  *  Locks used to protect struct members in this file:
78  *	I	immutable after creation
79  *	d	protection left to the driver
80  *	c	only used in ioctl or routing socket contexts (kernel lock)
81  *	K	kernel lock
82  *	N	net lock
83  *
84  *  For SRP related structures that allow lock-free reads, the write lock
85  *  is indicated below.
86  */
87 
88 struct rtentry;
89 struct ifnet;
90 struct task;
91 struct cpumem;
92 
93 /*
94  * Structure describing a `cloning' interface.
95  */
96 struct if_clone {
97 	LIST_ENTRY(if_clone)	 ifc_list;	/* [I] on list of cloners */
98 	const char		*ifc_name;	/* name of device, e.g. `gif' */
99 	size_t			 ifc_namelen;	/* length of name */
100 
101 	int			(*ifc_create)(struct if_clone *, int);
102 	int			(*ifc_destroy)(struct ifnet *);
103 };
104 
105 #define	IF_CLONE_INITIALIZER(name, create, destroy)			\
106 {									\
107   .ifc_list	= { NULL, NULL },					\
108   .ifc_name	= name,							\
109   .ifc_namelen	= sizeof(name) - 1,					\
110   .ifc_create	= create,						\
111   .ifc_destroy	= destroy,						\
112 }
113 
114 /*
115  * Structure defining a queue for a network interface.
116  *
117  * (Would like to call this struct ``if'', but C isn't PL/1.)
118  */
119 TAILQ_HEAD(ifnet_head, ifnet);		/* the actual queue head */
120 
121 struct ifnet {				/* and the entries */
122 	void	*if_softc;		/* [I] lower-level data for this if */
123 	struct	refcnt if_refcnt;
124 	TAILQ_ENTRY(ifnet) if_list;	/* [NK] all struct ifnets are chained */
125 	TAILQ_HEAD(, ifaddr) if_addrlist; /* [N] list of addresses per if */
126 	TAILQ_HEAD(, ifmaddr) if_maddrlist; /* [N] list of multicast records */
127 	TAILQ_HEAD(, ifg_list) if_groups; /* [N] list of groups per if */
128 	struct task_list if_addrhooks;	/* [I] address change callbacks */
129 	struct task_list if_linkstatehooks; /* [I] link change callbacks*/
130 	struct task_list if_detachhooks; /* [I] detach callbacks */
131 				/* [I] check or clean routes (+ or -)'d */
132 	void	(*if_rtrequest)(struct ifnet *, int, struct rtentry *);
133 	char	if_xname[IFNAMSIZ];	/* [I] external name (name + unit) */
134 	int	if_pcount;		/* [N] # of promiscuous listeners */
135 	unsigned int if_bridgeidx;	/* [K] used by bridge ports */
136 	caddr_t	if_bpf;			/* packet filter structure */
137 	caddr_t if_mcast;		/* used by multicast code */
138 	caddr_t if_mcast6;		/* used by IPv6 multicast code */
139 	caddr_t	if_pf_kif;		/* pf interface abstraction */
140 	union {
141 		struct srpl carp_s;	/* carp if list (used by !carp ifs) */
142 		unsigned int carp_idx;	/* index of carpdev (used by carp
143 						ifs) */
144 	} if_carp_ptr;
145 #define if_carp		if_carp_ptr.carp_s
146 #define if_carpdevidx	if_carp_ptr.carp_idx
147 	unsigned int if_index;		/* [I] unique index for this if */
148 	short	if_timer;		/* time 'til if_watchdog called */
149 	unsigned short if_flags;	/* [N] up/down, broadcast, etc. */
150 	int	if_xflags;		/* [N] extra softnet flags */
151 	struct	if_data if_data;	/* stats and other data about if */
152 	struct	cpumem *if_counters;	/* per cpu stats */
153 	uint32_t if_hardmtu;		/* [d] maximum MTU device supports */
154 	char	if_description[IFDESCRSIZE]; /* [c] interface description */
155 	u_short	if_rtlabelid;		/* [c] next route label */
156 	uint8_t if_priority;		/* [c] route priority offset */
157 	uint8_t if_llprio;		/* [N] link layer priority */
158 	struct	timeout if_slowtimo;	/* [I] watchdog timeout */
159 	struct	task if_watchdogtask;	/* [I] watchdog task */
160 	struct	task if_linkstatetask;	/* [I] task to do route updates */
161 
162 	/* procedure handles */
163 	void	(*if_input)(struct ifnet *, struct mbuf *);
164 	int	(*if_bpf_mtap)(caddr_t, const struct mbuf *, u_int);
165 	int	(*if_output)(struct ifnet *, struct mbuf *, struct sockaddr *,
166 		     struct rtentry *);	/* output routine (enqueue) */
167 					/* link level output function */
168 	int	(*if_ll_output)(struct ifnet *, struct mbuf *,
169 		    struct sockaddr *, struct rtentry *);
170 	int	(*if_enqueue)(struct ifnet *, struct mbuf *);
171 	void	(*if_start)(struct ifnet *);	/* initiate output */
172 	int	(*if_ioctl)(struct ifnet *, u_long, caddr_t); /* ioctl hook */
173 	void	(*if_watchdog)(struct ifnet *);	/* timer routine */
174 	int	(*if_wol)(struct ifnet *, int);	/* WoL routine **/
175 
176 	/* queues */
177 	struct	ifqueue if_snd;		/* transmit queue */
178 	struct	ifqueue **if_ifqs;	/* [I] pointer to an array of sndqs */
179 	void	(*if_qstart)(struct ifqueue *);
180 	unsigned int if_nifqs;		/* [I] number of output queues */
181 	unsigned int if_txmit;		/* [c] txmitigation amount */
182 
183 	struct	ifiqueue if_rcv;	/* rx/input queue */
184 	struct	ifiqueue **if_iqs;	/* [I] pointer to the array of iqs */
185 	unsigned int if_niqs;		/* [I] number of input queues */
186 
187 	struct sockaddr_dl *if_sadl;	/* [N] pointer to our sockaddr_dl */
188 
189 	struct	nd_ifinfo *if_nd;	/* [I] IPv6 Neighbor Discovery info */
190 };
191 #define	if_mtu		if_data.ifi_mtu
192 #define	if_type		if_data.ifi_type
193 #define	if_addrlen	if_data.ifi_addrlen
194 #define	if_hdrlen	if_data.ifi_hdrlen
195 #define	if_metric	if_data.ifi_metric
196 #define	if_link_state	if_data.ifi_link_state
197 #define	if_baudrate	if_data.ifi_baudrate
198 #define	if_ipackets	if_data.ifi_ipackets
199 #define	if_ierrors	if_data.ifi_ierrors
200 #define	if_opackets	if_data.ifi_opackets
201 #define	if_oerrors	if_data.ifi_oerrors
202 #define	if_collisions	if_data.ifi_collisions
203 #define	if_ibytes	if_data.ifi_ibytes
204 #define	if_obytes	if_data.ifi_obytes
205 #define	if_imcasts	if_data.ifi_imcasts
206 #define	if_omcasts	if_data.ifi_omcasts
207 #define	if_iqdrops	if_data.ifi_iqdrops
208 #define	if_oqdrops	if_data.ifi_oqdrops
209 #define	if_noproto	if_data.ifi_noproto
210 #define	if_lastchange	if_data.ifi_lastchange	/* [c] last op. state change */
211 #define	if_capabilities	if_data.ifi_capabilities
212 #define	if_rdomain	if_data.ifi_rdomain
213 
214 enum if_counters {
215 	ifc_ipackets,		/* packets received on interface */
216 	ifc_ierrors,		/* input errors on interface */
217 	ifc_opackets,		/* packets sent on interface */
218 	ifc_oerrors,		/* output errors on interface */
219 	ifc_collisions,		/* collisions on csma interfaces */
220 	ifc_ibytes,		/* total number of octets received */
221 	ifc_obytes,		/* total number of octets sent */
222 	ifc_imcasts,		/* packets received via multicast */
223 	ifc_omcasts,		/* packets sent via multicast */
224 	ifc_iqdrops,		/* dropped on input, this interface */
225 	ifc_oqdrops,		/* dropped on output, this interface */
226 	ifc_noproto,		/* destined for unsupported protocol */
227 
228 	ifc_ncounters
229 };
230 
231 /*
232  * The ifaddr structure contains information about one address
233  * of an interface.  They are maintained by the different address families,
234  * are allocated and attached when an address is set, and are linked
235  * together so all addresses for an interface can be located.
236  */
237 struct ifaddr {
238 	struct	sockaddr *ifa_addr;	/* address of interface */
239 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
240 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
241 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
242 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
243 	TAILQ_ENTRY(ifaddr) ifa_list;	/* [N] list of addresses for
244 					    interface */
245 	u_int	ifa_flags;		/* interface flags, see below */
246 	struct	refcnt ifa_refcnt;	/* number of `rt_ifa` references */
247 	int	ifa_metric;		/* cost of going out this interface */
248 };
249 
250 #define	IFA_ROUTE		0x01	/* Auto-magically installed route */
251 
252 /*
253  * Interface multicast address.
254  */
255 struct ifmaddr {
256 	struct sockaddr		*ifma_addr;	/* Protocol address */
257 	unsigned int		 ifma_ifidx;	/* Index of the interface */
258 	struct refcnt		 ifma_refcnt;	/* Count of references */
259 	TAILQ_ENTRY(ifmaddr)	 ifma_list;	/* Per-interface list */
260 };
261 
262 /*
263  * interface groups
264  */
265 
266 struct ifg_group {
267 	char			 ifg_group[IFNAMSIZ]; /* [I] group name */
268 	u_int			 ifg_refcnt;  /* [N] group reference count */
269 	caddr_t			 ifg_pf_kif;  /* [I] pf interface group */
270 	int			 ifg_carp_demoted; /* [K] carp demotion counter */
271 	TAILQ_HEAD(, ifg_member) ifg_members; /* [N] list of members per group */
272 	TAILQ_ENTRY(ifg_group)	 ifg_next;    /* [N] all groups are chained */
273 };
274 
275 struct ifg_member {
276 	TAILQ_ENTRY(ifg_member)	 ifgm_next; /* [N] all members are chained */
277 	struct ifnet		*ifgm_ifp;  /* [I] member interface */
278 };
279 
280 struct ifg_list {
281 	struct ifg_group	*ifgl_group; /* [I] interface group */
282 	TAILQ_ENTRY(ifg_list)	 ifgl_next;  /* [N] all groups are chained */
283 };
284 
285 #define	IFNET_SLOWTIMO	1		/* granularity is 1 second */
286 
287 #define IF_TXMIT_MIN			1
288 #define IF_TXMIT_DEFAULT		16
289 
290 /* default interface priorities */
291 #define IF_WIRED_DEFAULT_PRIORITY	0
292 #define IF_WIRELESS_DEFAULT_PRIORITY	4
293 #define IF_WWAN_DEFAULT_PRIORITY	6
294 #define IF_CARP_DEFAULT_PRIORITY	15
295 
296 /*
297  * Network stack input queues.
298  */
299 struct	niqueue {
300 	struct mbuf_queue	ni_q;
301 	u_int			ni_isr;
302 };
303 
304 #define NIQUEUE_INITIALIZER(_len, _isr) \
305     { MBUF_QUEUE_INITIALIZER((_len), IPL_NET), (_isr) }
306 
307 void		niq_init(struct niqueue *, u_int, u_int);
308 int		niq_enqueue(struct niqueue *, struct mbuf *);
309 int		niq_enlist(struct niqueue *, struct mbuf_list *);
310 
311 #define niq_dequeue(_q)			mq_dequeue(&(_q)->ni_q)
312 #define niq_dechain(_q)			mq_dechain(&(_q)->ni_q)
313 #define niq_delist(_q, _ml)		mq_delist(&(_q)->ni_q, (_ml))
314 #define niq_len(_q)			mq_len(&(_q)->ni_q)
315 #define niq_drops(_q)			mq_drops(&(_q)->ni_q)
316 #define sysctl_niq(_n, _l, _op, _olp, _np, _nl, _niq) \
317     sysctl_mq((_n), (_l), (_op), (_olp), (_np), (_nl), &(_niq)->ni_q)
318 
319 extern struct ifnet_head ifnetlist;
320 
321 void	if_start(struct ifnet *);
322 int	if_enqueue(struct ifnet *, struct mbuf *);
323 int	if_enqueue_ifq(struct ifnet *, struct mbuf *);
324 void	if_input(struct ifnet *, struct mbuf_list *);
325 void	if_vinput(struct ifnet *, struct mbuf *);
326 void	if_input_process(struct ifnet *, struct mbuf_list *);
327 int	if_input_local(struct ifnet *, struct mbuf *, sa_family_t);
328 int	if_output_ml(struct ifnet *, struct mbuf_list *,
329 	    struct sockaddr *, struct rtentry *);
330 int	if_output_mq(struct ifnet *, struct mbuf_queue *, unsigned int *,
331 	    struct sockaddr *, struct rtentry *);
332 int	if_output_tso(struct ifnet *, struct mbuf **, struct sockaddr *,
333 	    struct rtentry *, u_int);
334 int	if_output_local(struct ifnet *, struct mbuf *, sa_family_t);
335 void	if_rtrequest_dummy(struct ifnet *, int, struct rtentry *);
336 void	p2p_rtrequest(struct ifnet *, int, struct rtentry *);
337 void	p2p_input(struct ifnet *, struct mbuf *);
338 int	p2p_bpf_mtap(caddr_t, const struct mbuf *, u_int);
339 
340 struct	ifaddr *ifa_ifwithaddr(const struct sockaddr *, u_int);
341 struct	ifaddr *ifa_ifwithdstaddr(const struct sockaddr *, u_int);
342 struct	ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *);
343 struct	ifaddr *ifaref(struct ifaddr *);
344 void	ifafree(struct ifaddr *);
345 
346 int	if_isconnected(const struct ifnet *, unsigned int);
347 
348 void	if_clone_attach(struct if_clone *);
349 
350 int	if_clone_create(const char *, int);
351 int	if_clone_destroy(const char *);
352 
353 struct if_clone *
354 	if_clone_lookup(const char *, int *);
355 
356 void	ifa_add(struct ifnet *, struct ifaddr *);
357 void	ifa_del(struct ifnet *, struct ifaddr *);
358 void	ifa_update_broadaddr(struct ifnet *, struct ifaddr *,
359 	    struct sockaddr *);
360 
361 void	if_addrhook_add(struct ifnet *, struct task *);
362 void	if_addrhook_del(struct ifnet *, struct task *);
363 void	if_addrhooks_run(struct ifnet *);
364 void	if_linkstatehook_add(struct ifnet *, struct task *);
365 void	if_linkstatehook_del(struct ifnet *, struct task *);
366 void	if_detachhook_add(struct ifnet *, struct task *);
367 void	if_detachhook_del(struct ifnet *, struct task *);
368 
369 void	if_rxr_livelocked(struct if_rxring *);
370 void	if_rxr_init(struct if_rxring *, u_int, u_int);
371 u_int	if_rxr_get(struct if_rxring *, u_int);
372 
373 #define if_rxr_put(_r, _c)	do { (_r)->rxr_alive -= (_c); } while (0)
374 #define if_rxr_needrefill(_r)	((_r)->rxr_alive < (_r)->rxr_lwm)
375 #define if_rxr_inuse(_r)	((_r)->rxr_alive)
376 #define if_rxr_cwm(_r)		((_r)->rxr_cwm)
377 
378 int	if_rxr_info_ioctl(struct if_rxrinfo *, u_int, struct if_rxring_info *);
379 int	if_rxr_ioctl(struct if_rxrinfo *, const char *, u_int,
380 	    struct if_rxring *);
381 
382 void	if_counters_alloc(struct ifnet *);
383 void	if_counters_free(struct ifnet *);
384 
385 int	if_txhprio_l2_check(int);
386 int	if_txhprio_l3_check(int);
387 int	if_rxhprio_l2_check(int);
388 int	if_rxhprio_l3_check(int);
389 
390 #endif /* _KERNEL */
391 
392 #endif /* _NET_IF_VAR_H_ */
393