xref: /openbsd-src/sys/net/if_var.h (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: if_var.h,v 1.75 2016/09/04 15:46:39 reyk 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 
48 #include <net/ifq.h>
49 
50 /*
51  * Structures defining a network interface, providing a packet
52  * transport mechanism (ala level 0 of the PUP protocols).
53  *
54  * Each interface accepts output datagrams of a specified maximum
55  * length, and provides higher level routines with input datagrams
56  * received from its medium.
57  *
58  * Output occurs when the routine if_output is called, with four parameters:
59  *	(*ifp->if_output)(ifp, m, dst, rt)
60  * Here m is the mbuf chain to be sent and dst is the destination address.
61  * The output routine encapsulates the supplied datagram if necessary,
62  * and then transmits it on its medium.
63  *
64  * On input, each interface unwraps the data received by it, and either
65  * places it on the input queue of an internetwork datagram routine
66  * and posts the associated software interrupt, or passes the datagram to a raw
67  * packet input routine.
68  *
69  * Routines exist for locating interfaces by their addresses
70  * or for locating an interface on a certain network, as well as more general
71  * routing and gateway routines maintaining information used to locate
72  * interfaces.  These routines live in the files if.c and route.c
73  */
74 
75 struct rtentry;
76 struct timeout;
77 struct ifnet;
78 struct task;
79 
80 /*
81  * Structure describing a `cloning' interface.
82  */
83 struct if_clone {
84 	LIST_ENTRY(if_clone)	 ifc_list;	/* on list of cloners */
85 	const char		*ifc_name;	/* name of device, e.g. `gif' */
86 	size_t			 ifc_namelen;	/* length of name */
87 
88 	int			(*ifc_create)(struct if_clone *, int);
89 	int			(*ifc_destroy)(struct ifnet *);
90 };
91 
92 #define	IF_CLONE_INITIALIZER(name, create, destroy)			\
93 	{ { 0 }, name, sizeof(name) - 1, create, destroy }
94 
95 /*
96  * Structure defining a queue for a network interface.
97  *
98  * (Would like to call this struct ``if'', but C isn't PL/1.)
99  */
100 TAILQ_HEAD(ifnet_head, ifnet);		/* the actual queue head */
101 
102 struct ifnet {				/* and the entries */
103 	void	*if_softc;		/* lower-level data for this if */
104 	struct	refcnt if_refcnt;
105 	TAILQ_ENTRY(ifnet) if_list;	/* all struct ifnets are chained */
106 	TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */
107 	TAILQ_HEAD(, ifmaddr) if_maddrlist; /* list of multicast records */
108 	TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */
109 	struct hook_desc_head *if_addrhooks; /* address change callbacks */
110 	struct hook_desc_head *if_linkstatehooks; /* link change callbacks */
111 	struct hook_desc_head *if_detachhooks; /* detach callbacks */
112 					/* check or clean routes (+ or -)'d */
113 	void	(*if_rtrequest)(struct ifnet *, int, struct rtentry *);
114 	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
115 	int	if_pcount;		/* number of promiscuous listeners */
116 	caddr_t	if_bpf;			/* packet filter structure */
117 	caddr_t if_bridgeport;		/* used by bridge ports */
118 	caddr_t if_switchport;		/* used by switch ports */
119 	caddr_t	if_pf_kif;		/* pf interface abstraction */
120 	union {
121 		caddr_t	carp_s;		/* carp structure (used by !carp ifs) */
122 		struct ifnet *carp_d;	/* ptr to carpdev (used by carp ifs) */
123 	} if_carp_ptr;
124 #define if_carp		if_carp_ptr.carp_s
125 #define if_carpdev	if_carp_ptr.carp_d
126 	unsigned int if_index;		/* numeric abbreviation for this if */
127 	short	if_timer;		/* time 'til if_watchdog called */
128 	short	if_flags;		/* up/down, broadcast, etc. */
129 	int	if_xflags;		/* extra softnet flags */
130 	struct	if_data if_data;	/* stats and other data about if */
131 	u_int32_t if_hardmtu;		/* maximum MTU device supports */
132 	char	if_description[IFDESCRSIZE]; /* interface description */
133 	u_short	if_rtlabelid;		/* next route label */
134 	u_int8_t if_priority;
135 	u_int8_t if_llprio;		/* link layer priority */
136 	struct	timeout *if_slowtimo;	/* watchdog timeout */
137 	struct	task *if_watchdogtask;	/* watchdog task */
138 	struct	task *if_linkstatetask; /* task to do route updates */
139 
140 	/* procedure handles */
141 	struct mbuf_queue if_inputqueue;
142 	struct task *if_inputtask;	/* input task */
143 	SRPL_HEAD(, ifih) if_inputs;	/* input routines (dequeue) */
144 
145 					/* output routine (enqueue) */
146 	int	(*if_output)(struct ifnet *, struct mbuf *, struct sockaddr *,
147 		     struct rtentry *);
148 
149 					/* link level output function */
150 	int	(*if_ll_output)(struct ifnet *, struct mbuf *,
151 		    struct sockaddr *, struct rtentry *);
152 					/* initiate output routine */
153 	void	(*if_start)(struct ifnet *);
154 					/* ioctl routine */
155 	int	(*if_ioctl)(struct ifnet *, u_long, caddr_t);
156 					/* timer routine */
157 	void	(*if_watchdog)(struct ifnet *);
158 	int	(*if_wol)(struct ifnet *, int);
159 	struct	ifqueue if_snd;		/* output queue */
160 	struct sockaddr_dl *if_sadl;	/* pointer to our sockaddr_dl */
161 
162 	void	*if_afdata[AF_MAX];
163 };
164 #define	if_mtu		if_data.ifi_mtu
165 #define	if_type		if_data.ifi_type
166 #define	if_addrlen	if_data.ifi_addrlen
167 #define	if_hdrlen	if_data.ifi_hdrlen
168 #define	if_metric	if_data.ifi_metric
169 #define	if_link_state	if_data.ifi_link_state
170 #define	if_baudrate	if_data.ifi_baudrate
171 #define	if_ipackets	if_data.ifi_ipackets
172 #define	if_ierrors	if_data.ifi_ierrors
173 #define	if_opackets	if_data.ifi_opackets
174 #define	if_oerrors	if_data.ifi_oerrors
175 #define	if_collisions	if_data.ifi_collisions
176 #define	if_ibytes	if_data.ifi_ibytes
177 #define	if_obytes	if_data.ifi_obytes
178 #define	if_imcasts	if_data.ifi_imcasts
179 #define	if_omcasts	if_data.ifi_omcasts
180 #define	if_iqdrops	if_data.ifi_iqdrops
181 #define	if_oqdrops	if_data.ifi_oqdrops
182 #define	if_noproto	if_data.ifi_noproto
183 #define	if_lastchange	if_data.ifi_lastchange
184 #define	if_capabilities	if_data.ifi_capabilities
185 #define	if_rdomain	if_data.ifi_rdomain
186 
187 /*
188  * The ifaddr structure contains information about one address
189  * of an interface.  They are maintained by the different address families,
190  * are allocated and attached when an address is set, and are linked
191  * together so all addresses for an interface can be located.
192  */
193 struct ifaddr {
194 	struct	sockaddr *ifa_addr;	/* address of interface */
195 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
196 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
197 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
198 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
199 	TAILQ_ENTRY(ifaddr) ifa_list;	/* list of addresses for interface */
200 	u_int	ifa_flags;		/* interface flags, see below */
201 	u_int	ifa_refcnt;		/* number of `rt_ifa` references */
202 	int	ifa_metric;		/* cost of going out this interface */
203 };
204 
205 #define	IFA_ROUTE		0x01	/* Auto-magically installed route */
206 
207 /*
208  * Interface multicast address.
209  */
210 struct ifmaddr {
211 	struct sockaddr		*ifma_addr;	/* Protocol address */
212 	unsigned int		 ifma_ifidx;	/* Index of the interface */
213 	unsigned int		 ifma_refcnt;	/* Count of references */
214 	TAILQ_ENTRY(ifmaddr)	 ifma_list;	/* Per-interface list */
215 };
216 
217 /*
218  * interface groups
219  */
220 
221 struct ifg_group {
222 	char			 ifg_group[IFNAMSIZ];
223 	u_int			 ifg_refcnt;
224 	caddr_t			 ifg_pf_kif;
225 	int			 ifg_carp_demoted;
226 	TAILQ_HEAD(, ifg_member) ifg_members;
227 	TAILQ_ENTRY(ifg_group)	 ifg_next;
228 };
229 
230 struct ifg_member {
231 	TAILQ_ENTRY(ifg_member)	 ifgm_next;
232 	struct ifnet		*ifgm_ifp;
233 };
234 
235 struct ifg_list {
236 	struct ifg_group	*ifgl_group;
237 	TAILQ_ENTRY(ifg_list)	 ifgl_next;
238 };
239 
240 #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
241 
242 /*
243  * IFQ compat on ifq API
244  */
245 
246 #define	IFQ_ENQUEUE(ifq, m, err)					\
247 do {									\
248 	(err) = ifq_enqueue((ifq), (m));				\
249 } while (/* CONSTCOND */0)
250 
251 #define	IFQ_DEQUEUE(ifq, m)						\
252 do {									\
253 	(m) = ifq_dequeue(ifq);						\
254 } while (/* CONSTCOND */0)
255 
256 #define	IFQ_PURGE(ifq)							\
257 do {									\
258 	(void)ifq_purge(ifq);						\
259 } while (/* CONSTCOND */0)
260 
261 #define	IFQ_LEN(ifq)			ifq_len(ifq)
262 #define	IFQ_IS_EMPTY(ifq)		ifq_empty(ifq)
263 #define	IFQ_SET_MAXLEN(ifq, len)	ifq_set_maxlen(ifq, len)
264 
265 /* default interface priorities */
266 #define IF_WIRED_DEFAULT_PRIORITY	0
267 #define IF_WIRELESS_DEFAULT_PRIORITY	4
268 #define IF_CARP_DEFAULT_PRIORITY	15
269 
270 /*
271  * Network stack input queues.
272  */
273 struct	niqueue {
274 	struct mbuf_queue	ni_q;
275 	u_int			ni_isr;
276 };
277 
278 #define NIQUEUE_INITIALIZER(_len, _isr) \
279     { MBUF_QUEUE_INITIALIZER((_len), IPL_NET), (_isr) }
280 
281 void		niq_init(struct niqueue *, u_int, u_int);
282 int		niq_enqueue(struct niqueue *, struct mbuf *);
283 int		niq_enlist(struct niqueue *, struct mbuf_list *);
284 
285 #define niq_dequeue(_q)			mq_dequeue(&(_q)->ni_q)
286 #define niq_dechain(_q)			mq_dechain(&(_q)->ni_q)
287 #define niq_delist(_q, _ml)		mq_delist(&(_q)->ni_q, (_ml))
288 #define niq_len(_q)			mq_len(&(_q)->ni_q)
289 #define niq_drops(_q)			mq_drops(&(_q)->ni_q)
290 #define sysctl_niq(_n, _l, _op, _olp, _np, _nl, _niq) \
291     sysctl_mq((_n), (_l), (_op), (_olp), (_np), (_nl), &(_niq)->ni_q)
292 
293 extern struct ifnet_head ifnet;
294 extern unsigned int lo0ifidx;
295 extern struct taskq *softnettq;
296 
297 void	if_start(struct ifnet *);
298 int	if_enqueue_try(struct ifnet *, struct mbuf *);
299 int	if_enqueue(struct ifnet *, struct mbuf *);
300 void	if_input(struct ifnet *, struct mbuf_list *);
301 int	if_input_local(struct ifnet *, struct mbuf *, sa_family_t);
302 void	if_rtrequest_dummy(struct ifnet *, int, struct rtentry *);
303 void	p2p_rtrequest(struct ifnet *, int, struct rtentry *);
304 
305 struct	ifaddr *ifa_ifwithaddr(struct sockaddr *, u_int);
306 struct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *, u_int);
307 struct	ifaddr *ifa_ifwithnet(struct sockaddr *, u_int);
308 struct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
309 void	ifafree(struct ifaddr *);
310 
311 int	if_isconnected(const struct ifnet *, unsigned int);
312 
313 void	if_clone_attach(struct if_clone *);
314 void	if_clone_detach(struct if_clone *);
315 
316 int	if_clone_create(const char *, int);
317 int	if_clone_destroy(const char *);
318 
319 struct if_clone *
320 	if_clone_lookup(const char *, int *);
321 
322 int     sysctl_mq(int *, u_int, void *, size_t *, void *, size_t,
323 	    struct mbuf_queue *);
324 
325 void	ifa_add(struct ifnet *, struct ifaddr *);
326 void	ifa_del(struct ifnet *, struct ifaddr *);
327 void	ifa_update_broadaddr(struct ifnet *, struct ifaddr *,
328 	    struct sockaddr *);
329 
330 void	if_ih_insert(struct ifnet *, int (*)(struct ifnet *, struct mbuf *,
331 	    void *), void *);
332 void	if_ih_remove(struct ifnet *, int (*)(struct ifnet *, struct mbuf *,
333 	    void *), void *);
334 
335 void	if_rxr_init(struct if_rxring *, u_int, u_int);
336 u_int	if_rxr_get(struct if_rxring *, u_int);
337 
338 #define if_rxr_put(_r, _c)	do { (_r)->rxr_alive -= (_c); } while (0)
339 #define if_rxr_inuse(_r)	((_r)->rxr_alive)
340 
341 int	if_rxr_info_ioctl(struct if_rxrinfo *, u_int, struct if_rxring_info *);
342 int	if_rxr_ioctl(struct if_rxrinfo *, const char *, u_int,
343 	    struct if_rxring *);
344 
345 #endif /* _KERNEL */
346 
347 #endif /* _NET_IF_VAR_H_ */
348