xref: /openbsd/sys/net/if_var.h (revision 6f40fd34)
1 /*	$OpenBSD: if_var.h,v 1.81 2017/05/08 08:46:39 rzalamena 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_mcast;		/* used by multicast code */
120 	caddr_t if_mcast6;		/* used by IPv6 multicast code */
121 	caddr_t	if_pf_kif;		/* pf interface abstraction */
122 	union {
123 		caddr_t	carp_s;		/* carp structure (used by !carp ifs) */
124 		struct ifnet *carp_d;	/* ptr to carpdev (used by carp ifs) */
125 	} if_carp_ptr;
126 #define if_carp		if_carp_ptr.carp_s
127 #define if_carpdev	if_carp_ptr.carp_d
128 	unsigned int if_index;		/* numeric abbreviation for this if */
129 	short	if_timer;		/* time 'til if_watchdog called */
130 	unsigned short if_flags;	/* up/down, broadcast, etc. */
131 	int	if_xflags;		/* extra softnet flags */
132 	struct	if_data if_data;	/* stats and other data about if */
133 	u_int32_t if_hardmtu;		/* maximum MTU device supports */
134 	char	if_description[IFDESCRSIZE]; /* interface description */
135 	u_short	if_rtlabelid;		/* next route label */
136 	u_int8_t if_priority;
137 	u_int8_t if_llprio;		/* link layer priority */
138 	struct	timeout *if_slowtimo;	/* watchdog timeout */
139 	struct	task *if_watchdogtask;	/* watchdog task */
140 	struct	task *if_linkstatetask; /* task to do route updates */
141 
142 	/* procedure handles */
143 	struct mbuf_queue if_inputqueue;
144 	struct task *if_inputtask;	/* input task */
145 	SRPL_HEAD(, ifih) if_inputs;	/* input routines (dequeue) */
146 
147 					/* output routine (enqueue) */
148 	int	(*if_output)(struct ifnet *, struct mbuf *, struct sockaddr *,
149 		     struct rtentry *);
150 
151 					/* link level output function */
152 	int	(*if_ll_output)(struct ifnet *, struct mbuf *,
153 		    struct sockaddr *, struct rtentry *);
154 					/* initiate output routine */
155 	void	(*if_start)(struct ifnet *);
156 					/* ioctl routine */
157 	int	(*if_ioctl)(struct ifnet *, u_long, caddr_t);
158 					/* timer routine */
159 	void	(*if_watchdog)(struct ifnet *);
160 	int	(*if_wol)(struct ifnet *, int);
161 
162 	struct	ifqueue if_snd;		/* transmit queue */
163 	struct	ifqueue **if_ifqs;	/* pointer to an array of sndqs */
164 	void	(*if_qstart)(struct ifqueue *);
165 	unsigned int if_nifqs;
166 
167 	struct sockaddr_dl *if_sadl;	/* pointer to our sockaddr_dl */
168 
169 	void	*if_afdata[AF_MAX];
170 };
171 #define	if_mtu		if_data.ifi_mtu
172 #define	if_type		if_data.ifi_type
173 #define	if_addrlen	if_data.ifi_addrlen
174 #define	if_hdrlen	if_data.ifi_hdrlen
175 #define	if_metric	if_data.ifi_metric
176 #define	if_link_state	if_data.ifi_link_state
177 #define	if_baudrate	if_data.ifi_baudrate
178 #define	if_ipackets	if_data.ifi_ipackets
179 #define	if_ierrors	if_data.ifi_ierrors
180 #define	if_opackets	if_data.ifi_opackets
181 #define	if_oerrors	if_data.ifi_oerrors
182 #define	if_collisions	if_data.ifi_collisions
183 #define	if_ibytes	if_data.ifi_ibytes
184 #define	if_obytes	if_data.ifi_obytes
185 #define	if_imcasts	if_data.ifi_imcasts
186 #define	if_omcasts	if_data.ifi_omcasts
187 #define	if_iqdrops	if_data.ifi_iqdrops
188 #define	if_oqdrops	if_data.ifi_oqdrops
189 #define	if_noproto	if_data.ifi_noproto
190 #define	if_lastchange	if_data.ifi_lastchange
191 #define	if_capabilities	if_data.ifi_capabilities
192 #define	if_rdomain	if_data.ifi_rdomain
193 
194 /*
195  * The ifaddr structure contains information about one address
196  * of an interface.  They are maintained by the different address families,
197  * are allocated and attached when an address is set, and are linked
198  * together so all addresses for an interface can be located.
199  */
200 struct ifaddr {
201 	struct	sockaddr *ifa_addr;	/* address of interface */
202 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
203 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
204 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
205 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
206 	TAILQ_ENTRY(ifaddr) ifa_list;	/* list of addresses for interface */
207 	u_int	ifa_flags;		/* interface flags, see below */
208 	u_int	ifa_refcnt;		/* number of `rt_ifa` references */
209 	int	ifa_metric;		/* cost of going out this interface */
210 };
211 
212 #define	IFA_ROUTE		0x01	/* Auto-magically installed route */
213 
214 /*
215  * Interface multicast address.
216  */
217 struct ifmaddr {
218 	struct sockaddr		*ifma_addr;	/* Protocol address */
219 	unsigned int		 ifma_ifidx;	/* Index of the interface */
220 	unsigned int		 ifma_refcnt;	/* Count of references */
221 	TAILQ_ENTRY(ifmaddr)	 ifma_list;	/* Per-interface list */
222 };
223 
224 /*
225  * interface groups
226  */
227 
228 struct ifg_group {
229 	char			 ifg_group[IFNAMSIZ];
230 	u_int			 ifg_refcnt;
231 	caddr_t			 ifg_pf_kif;
232 	int			 ifg_carp_demoted;
233 	TAILQ_HEAD(, ifg_member) ifg_members;
234 	TAILQ_ENTRY(ifg_group)	 ifg_next;
235 };
236 
237 struct ifg_member {
238 	TAILQ_ENTRY(ifg_member)	 ifgm_next;
239 	struct ifnet		*ifgm_ifp;
240 };
241 
242 struct ifg_list {
243 	struct ifg_group	*ifgl_group;
244 	TAILQ_ENTRY(ifg_list)	 ifgl_next;
245 };
246 
247 #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
248 
249 /*
250  * IFQ compat on ifq API
251  */
252 
253 #define	IFQ_ENQUEUE(ifq, m, err)					\
254 do {									\
255 	(err) = ifq_enqueue((ifq), (m));				\
256 } while (/* CONSTCOND */0)
257 
258 #define	IFQ_DEQUEUE(ifq, m)						\
259 do {									\
260 	(m) = ifq_dequeue(ifq);						\
261 } while (/* CONSTCOND */0)
262 
263 #define	IFQ_PURGE(ifq)							\
264 do {									\
265 	(void)ifq_purge(ifq);						\
266 } while (/* CONSTCOND */0)
267 
268 #define	IFQ_LEN(ifq)			ifq_len(ifq)
269 #define	IFQ_IS_EMPTY(ifq)		ifq_empty(ifq)
270 #define	IFQ_SET_MAXLEN(ifq, len)	ifq_set_maxlen(ifq, len)
271 
272 /* default interface priorities */
273 #define IF_WIRED_DEFAULT_PRIORITY	0
274 #define IF_WIRELESS_DEFAULT_PRIORITY	4
275 #define IF_CARP_DEFAULT_PRIORITY	15
276 
277 /*
278  * Network stack input queues.
279  */
280 struct	niqueue {
281 	struct mbuf_queue	ni_q;
282 	u_int			ni_isr;
283 };
284 
285 #define NIQUEUE_INITIALIZER(_len, _isr) \
286     { MBUF_QUEUE_INITIALIZER((_len), IPL_NET), (_isr) }
287 
288 void		niq_init(struct niqueue *, u_int, u_int);
289 int		niq_enqueue(struct niqueue *, struct mbuf *);
290 int		niq_enlist(struct niqueue *, struct mbuf_list *);
291 
292 #define niq_dequeue(_q)			mq_dequeue(&(_q)->ni_q)
293 #define niq_dechain(_q)			mq_dechain(&(_q)->ni_q)
294 #define niq_delist(_q, _ml)		mq_delist(&(_q)->ni_q, (_ml))
295 #define niq_len(_q)			mq_len(&(_q)->ni_q)
296 #define niq_drops(_q)			mq_drops(&(_q)->ni_q)
297 #define sysctl_niq(_n, _l, _op, _olp, _np, _nl, _niq) \
298     sysctl_mq((_n), (_l), (_op), (_olp), (_np), (_nl), &(_niq)->ni_q)
299 
300 extern struct ifnet_head ifnet;
301 extern struct taskq *softnettq;
302 
303 void	if_start(struct ifnet *);
304 int	if_enqueue_try(struct ifnet *, struct mbuf *);
305 int	if_enqueue(struct ifnet *, struct mbuf *);
306 void	if_input(struct ifnet *, struct mbuf_list *);
307 int	if_input_local(struct ifnet *, struct mbuf *, sa_family_t);
308 void	if_rtrequest_dummy(struct ifnet *, int, struct rtentry *);
309 void	p2p_rtrequest(struct ifnet *, int, struct rtentry *);
310 
311 struct	ifaddr *ifa_ifwithaddr(struct sockaddr *, u_int);
312 struct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *, u_int);
313 struct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
314 void	ifafree(struct ifaddr *);
315 
316 int	if_isconnected(const struct ifnet *, unsigned int);
317 
318 void	if_clone_attach(struct if_clone *);
319 void	if_clone_detach(struct if_clone *);
320 
321 int	if_clone_create(const char *, int);
322 int	if_clone_destroy(const char *);
323 
324 struct if_clone *
325 	if_clone_lookup(const char *, int *);
326 
327 int     sysctl_mq(int *, u_int, void *, size_t *, void *, size_t,
328 	    struct mbuf_queue *);
329 
330 void	ifa_add(struct ifnet *, struct ifaddr *);
331 void	ifa_del(struct ifnet *, struct ifaddr *);
332 void	ifa_update_broadaddr(struct ifnet *, struct ifaddr *,
333 	    struct sockaddr *);
334 
335 void	if_ih_insert(struct ifnet *, int (*)(struct ifnet *, struct mbuf *,
336 	    void *), void *);
337 void	if_ih_remove(struct ifnet *, int (*)(struct ifnet *, struct mbuf *,
338 	    void *), void *);
339 
340 void	if_rxr_init(struct if_rxring *, u_int, u_int);
341 u_int	if_rxr_get(struct if_rxring *, u_int);
342 
343 #define if_rxr_put(_r, _c)	do { (_r)->rxr_alive -= (_c); } while (0)
344 #define if_rxr_inuse(_r)	((_r)->rxr_alive)
345 
346 int	if_rxr_info_ioctl(struct if_rxrinfo *, u_int, struct if_rxring_info *);
347 int	if_rxr_ioctl(struct if_rxrinfo *, const char *, u_int,
348 	    struct if_rxring *);
349 
350 #endif /* _KERNEL */
351 
352 #endif /* _NET_IF_VAR_H_ */
353