xref: /freebsd/sys/net/if_var.h (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	From: @(#)if.h	8.1 (Berkeley) 6/10/93
30  * $FreeBSD$
31  */
32 
33 #ifndef	_NET_IF_VAR_H_
34 #define	_NET_IF_VAR_H_
35 
36 /*
37  * Structures defining a network interface, providing a packet
38  * transport mechanism (ala level 0 of the PUP protocols).
39  *
40  * Each interface accepts output datagrams of a specified maximum
41  * length, and provides higher level routines with input datagrams
42  * received from its medium.
43  *
44  * Output occurs when the routine if_output is called, with three parameters:
45  *	(*ifp->if_output)(ifp, m, dst, rt)
46  * Here m is the mbuf chain to be sent and dst is the destination address.
47  * The output routine encapsulates the supplied datagram if necessary,
48  * and then transmits it on its medium.
49  *
50  * On input, each interface unwraps the data received by it, and either
51  * places it on the input queue of an internetwork datagram routine
52  * and posts the associated software interrupt, or passes the datagram to a raw
53  * packet input routine.
54  *
55  * Routines exist for locating interfaces by their addresses
56  * or for locating an interface on a certain network, as well as more general
57  * routing and gateway routines maintaining information used to locate
58  * interfaces.  These routines live in the files if.c and route.c
59  */
60 
61 #ifdef __STDC__
62 /*
63  * Forward structure declarations for function prototypes [sic].
64  */
65 struct	mbuf;
66 struct	thread;
67 struct	rtentry;
68 struct	rt_addrinfo;
69 struct	socket;
70 struct	ether_header;
71 struct	carp_if;
72 struct  ifvlantrunk;
73 struct	route;
74 struct	vnet;
75 #endif
76 
77 #include <sys/queue.h>		/* get TAILQ macros */
78 
79 #ifdef _KERNEL
80 #include <sys/mbuf.h>
81 #include <sys/eventhandler.h>
82 #include <sys/buf_ring.h>
83 #include <net/vnet.h>
84 #endif /* _KERNEL */
85 #include <sys/lock.h>		/* XXX */
86 #include <sys/mutex.h>		/* XXX */
87 #include <sys/rwlock.h>		/* XXX */
88 #include <sys/sx.h>		/* XXX */
89 #include <sys/event.h>		/* XXX */
90 #include <sys/_task.h>
91 
92 #define	IF_DUNIT_NONE	-1
93 
94 #include <altq/if_altq.h>
95 
96 TAILQ_HEAD(ifnethead, ifnet);	/* we use TAILQs so that the order of */
97 TAILQ_HEAD(ifaddrhead, ifaddr);	/* instantiation is preserved in the list */
98 TAILQ_HEAD(ifprefixhead, ifprefix);
99 TAILQ_HEAD(ifmultihead, ifmultiaddr);
100 TAILQ_HEAD(ifgrouphead, ifg_group);
101 
102 /*
103  * Structure defining a queue for a network interface.
104  */
105 struct	ifqueue {
106 	struct	mbuf *ifq_head;
107 	struct	mbuf *ifq_tail;
108 	int	ifq_len;
109 	int	ifq_maxlen;
110 	int	ifq_drops;
111 	struct	mtx ifq_mtx;
112 };
113 
114 /*
115  * Structure defining a network interface.
116  *
117  * (Would like to call this struct ``if'', but C isn't PL/1.)
118  */
119 
120 struct ifnet {
121 	void	*if_softc;		/* pointer to driver state */
122 	void	*if_l2com;		/* pointer to protocol bits */
123 	struct vnet *if_vnet;		/* pointer to network stack instance */
124 	TAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained */
125 	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
126 	const char *if_dname;		/* driver name */
127 	int	if_dunit;		/* unit or IF_DUNIT_NONE */
128 	u_int	if_refcount;		/* reference count */
129 	struct	ifaddrhead if_addrhead;	/* linked list of addresses per if */
130 		/*
131 		 * if_addrhead is the list of all addresses associated to
132 		 * an interface.
133 		 * Some code in the kernel assumes that first element
134 		 * of the list has type AF_LINK, and contains sockaddr_dl
135 		 * addresses which store the link-level address and the name
136 		 * of the interface.
137 		 * However, access to the AF_LINK address through this
138 		 * field is deprecated. Use if_addr or ifaddr_byindex() instead.
139 		 */
140 	int	if_pcount;		/* number of promiscuous listeners */
141 	struct	carp_if *if_carp;	/* carp interface structure */
142 	struct	bpf_if *if_bpf;		/* packet filter structure */
143 	u_short	if_index;		/* numeric abbreviation for this if  */
144 	short	if_index_reserved;	/* spare space to grow if_index */
145 	struct  ifvlantrunk *if_vlantrunk; /* pointer to 802.1q data */
146 	int	if_flags;		/* up/down, broadcast, etc. */
147 	int	if_capabilities;	/* interface features & capabilities */
148 	int	if_capenable;		/* enabled features & capabilities */
149 	void	*if_linkmib;		/* link-type-specific MIB data */
150 	size_t	if_linkmiblen;		/* length of above data */
151 	struct	if_data if_data;
152 	struct	ifmultihead if_multiaddrs; /* multicast addresses configured */
153 	int	if_amcount;		/* number of all-multicast requests */
154 /* procedure handles */
155 	int	(*if_output)		/* output routine (enqueue) */
156 		(struct ifnet *, struct mbuf *, struct sockaddr *,
157 		     struct route *);
158 	void	(*if_input)		/* input routine (from h/w driver) */
159 		(struct ifnet *, struct mbuf *);
160 	void	(*if_start)		/* initiate output routine */
161 		(struct ifnet *);
162 	int	(*if_ioctl)		/* ioctl routine */
163 		(struct ifnet *, u_long, caddr_t);
164 	void	(*if_init)		/* Init routine */
165 		(void *);
166 	int	(*if_resolvemulti)	/* validate/resolve multicast */
167 		(struct ifnet *, struct sockaddr **, struct sockaddr *);
168 	void	(*if_qflush)		/* flush any queues */
169 		(struct ifnet *);
170 	int	(*if_transmit)		/* initiate output routine */
171 		(struct ifnet *, struct mbuf *);
172 	void	(*if_reassign)		/* reassign to vnet routine */
173 		(struct ifnet *, struct vnet *, char *);
174 	struct	vnet *if_home_vnet;	/* where this ifnet originates from */
175 	struct	ifaddr	*if_addr;	/* pointer to link-level address */
176 	void	*if_llsoftc;		/* link layer softc */
177 	int	if_drv_flags;		/* driver-managed status flags */
178 	struct  ifaltq if_snd;		/* output queue (includes altq) */
179 	const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */
180 
181 	void	*if_bridge;		/* bridge glue */
182 
183 	struct	label *if_label;	/* interface MAC label */
184 
185 	/* these are only used by IPv6 */
186 	struct	ifprefixhead if_prefixhead; /* list of prefixes per if */
187 	void	*if_afdata[AF_MAX];
188 	int	if_afdata_initialized;
189 	struct	rwlock if_afdata_lock;
190 	struct	task if_linktask;	/* task for link change events */
191 	struct	mtx if_addr_mtx;	/* mutex to protect address lists */
192 
193 	LIST_ENTRY(ifnet) if_clones;	/* interfaces of a cloner */
194 	TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */
195 					/* protected by if_addr_mtx */
196 	void	*if_pf_kif;
197 	void	*if_lagg;		/* lagg glue */
198 	char	*if_description;	/* interface description */
199 	u_int	if_fib;			/* interface FIB */
200 	u_char	if_alloctype;		/* if_type at time of allocation */
201 
202 	/*
203 	 * Spare fields are added so that we can modify sensitive data
204 	 * structures without changing the kernel binary interface, and must
205 	 * be used with care where binary compatibility is required.
206 	 */
207 	char	if_cspare[3];
208 	int	if_ispare[4];
209 	void	*if_pspare[8];		/* 1 netmap, 7 TDB */
210 };
211 
212 typedef void if_init_f_t(void *);
213 
214 /*
215  * XXX These aliases are terribly dangerous because they could apply
216  * to anything.
217  */
218 #define	if_mtu		if_data.ifi_mtu
219 #define	if_type		if_data.ifi_type
220 #define if_physical	if_data.ifi_physical
221 #define	if_addrlen	if_data.ifi_addrlen
222 #define	if_hdrlen	if_data.ifi_hdrlen
223 #define	if_metric	if_data.ifi_metric
224 #define	if_link_state	if_data.ifi_link_state
225 #define	if_baudrate	if_data.ifi_baudrate
226 #define	if_hwassist	if_data.ifi_hwassist
227 #define	if_ipackets	if_data.ifi_ipackets
228 #define	if_ierrors	if_data.ifi_ierrors
229 #define	if_opackets	if_data.ifi_opackets
230 #define	if_oerrors	if_data.ifi_oerrors
231 #define	if_collisions	if_data.ifi_collisions
232 #define	if_ibytes	if_data.ifi_ibytes
233 #define	if_obytes	if_data.ifi_obytes
234 #define	if_imcasts	if_data.ifi_imcasts
235 #define	if_omcasts	if_data.ifi_omcasts
236 #define	if_iqdrops	if_data.ifi_iqdrops
237 #define	if_noproto	if_data.ifi_noproto
238 #define	if_lastchange	if_data.ifi_lastchange
239 
240 /* for compatibility with other BSDs */
241 #define	if_addrlist	if_addrhead
242 #define	if_list		if_link
243 #define	if_name(ifp)	((ifp)->if_xname)
244 
245 /*
246  * Locks for address lists on the network interface.
247  */
248 #define	IF_ADDR_LOCK_INIT(if)	mtx_init(&(if)->if_addr_mtx,		\
249 				    "if_addr_mtx", NULL, MTX_DEF)
250 #define	IF_ADDR_LOCK_DESTROY(if)	mtx_destroy(&(if)->if_addr_mtx)
251 #define	IF_ADDR_LOCK(if)	mtx_lock(&(if)->if_addr_mtx)
252 #define	IF_ADDR_UNLOCK(if)	mtx_unlock(&(if)->if_addr_mtx)
253 #define	IF_ADDR_LOCK_ASSERT(if)	mtx_assert(&(if)->if_addr_mtx, MA_OWNED)
254 
255 /*
256  * Function variations on locking macros intended to be used by loadable
257  * kernel modules in order to divorce them from the internals of address list
258  * locking.
259  */
260 void	if_addr_rlock(struct ifnet *ifp);	/* if_addrhead */
261 void	if_addr_runlock(struct ifnet *ifp);	/* if_addrhead */
262 void	if_maddr_rlock(struct ifnet *ifp);	/* if_multiaddrs */
263 void	if_maddr_runlock(struct ifnet *ifp);	/* if_multiaddrs */
264 
265 /*
266  * Output queues (ifp->if_snd) and slow device input queues (*ifp->if_slowq)
267  * are queues of messages stored on ifqueue structures
268  * (defined above).  Entries are added to and deleted from these structures
269  * by these macros, which should be called with ipl raised to splimp().
270  */
271 #define IF_LOCK(ifq)		mtx_lock(&(ifq)->ifq_mtx)
272 #define IF_UNLOCK(ifq)		mtx_unlock(&(ifq)->ifq_mtx)
273 #define	IF_LOCK_ASSERT(ifq)	mtx_assert(&(ifq)->ifq_mtx, MA_OWNED)
274 #define	_IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
275 #define	_IF_DROP(ifq)		((ifq)->ifq_drops++)
276 #define	_IF_QLEN(ifq)		((ifq)->ifq_len)
277 
278 #define	_IF_ENQUEUE(ifq, m) do { 				\
279 	(m)->m_nextpkt = NULL;					\
280 	if ((ifq)->ifq_tail == NULL) 				\
281 		(ifq)->ifq_head = m; 				\
282 	else 							\
283 		(ifq)->ifq_tail->m_nextpkt = m; 		\
284 	(ifq)->ifq_tail = m; 					\
285 	(ifq)->ifq_len++; 					\
286 } while (0)
287 
288 #define IF_ENQUEUE(ifq, m) do {					\
289 	IF_LOCK(ifq); 						\
290 	_IF_ENQUEUE(ifq, m); 					\
291 	IF_UNLOCK(ifq); 					\
292 } while (0)
293 
294 #define	_IF_PREPEND(ifq, m) do {				\
295 	(m)->m_nextpkt = (ifq)->ifq_head; 			\
296 	if ((ifq)->ifq_tail == NULL) 				\
297 		(ifq)->ifq_tail = (m); 				\
298 	(ifq)->ifq_head = (m); 					\
299 	(ifq)->ifq_len++; 					\
300 } while (0)
301 
302 #define IF_PREPEND(ifq, m) do {		 			\
303 	IF_LOCK(ifq); 						\
304 	_IF_PREPEND(ifq, m); 					\
305 	IF_UNLOCK(ifq); 					\
306 } while (0)
307 
308 #define	_IF_DEQUEUE(ifq, m) do { 				\
309 	(m) = (ifq)->ifq_head; 					\
310 	if (m) { 						\
311 		if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL)	\
312 			(ifq)->ifq_tail = NULL; 		\
313 		(m)->m_nextpkt = NULL; 				\
314 		(ifq)->ifq_len--; 				\
315 	} 							\
316 } while (0)
317 
318 #define IF_DEQUEUE(ifq, m) do { 				\
319 	IF_LOCK(ifq); 						\
320 	_IF_DEQUEUE(ifq, m); 					\
321 	IF_UNLOCK(ifq); 					\
322 } while (0)
323 
324 #define	_IF_DEQUEUE_ALL(ifq, m) do {				\
325 	(m) = (ifq)->ifq_head;					\
326 	(ifq)->ifq_head = (ifq)->ifq_tail = NULL;		\
327 	(ifq)->ifq_len = 0;					\
328 } while (0)
329 
330 #define	IF_DEQUEUE_ALL(ifq, m) do {				\
331 	IF_LOCK(ifq); 						\
332 	_IF_DEQUEUE_ALL(ifq, m);				\
333 	IF_UNLOCK(ifq); 					\
334 } while (0)
335 
336 #define	_IF_POLL(ifq, m)	((m) = (ifq)->ifq_head)
337 #define	IF_POLL(ifq, m)		_IF_POLL(ifq, m)
338 
339 #define _IF_DRAIN(ifq) do { 					\
340 	struct mbuf *m; 					\
341 	for (;;) { 						\
342 		_IF_DEQUEUE(ifq, m); 				\
343 		if (m == NULL) 					\
344 			break; 					\
345 		m_freem(m); 					\
346 	} 							\
347 } while (0)
348 
349 #define IF_DRAIN(ifq) do {					\
350 	IF_LOCK(ifq);						\
351 	_IF_DRAIN(ifq);						\
352 	IF_UNLOCK(ifq);						\
353 } while(0)
354 
355 #ifdef _KERNEL
356 /* interface link layer address change event */
357 typedef void (*iflladdr_event_handler_t)(void *, struct ifnet *);
358 EVENTHANDLER_DECLARE(iflladdr_event, iflladdr_event_handler_t);
359 /* interface address change event */
360 typedef void (*ifaddr_event_handler_t)(void *, struct ifnet *);
361 EVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
362 /* new interface arrival event */
363 typedef void (*ifnet_arrival_event_handler_t)(void *, struct ifnet *);
364 EVENTHANDLER_DECLARE(ifnet_arrival_event, ifnet_arrival_event_handler_t);
365 /* interface departure event */
366 typedef void (*ifnet_departure_event_handler_t)(void *, struct ifnet *);
367 EVENTHANDLER_DECLARE(ifnet_departure_event, ifnet_departure_event_handler_t);
368 /* Interface link state change event */
369 typedef void (*ifnet_link_event_handler_t)(void *, struct ifnet *, int);
370 EVENTHANDLER_DECLARE(ifnet_link_event, ifnet_link_event_handler_t);
371 
372 /*
373  * interface groups
374  */
375 struct ifg_group {
376 	char				 ifg_group[IFNAMSIZ];
377 	u_int				 ifg_refcnt;
378 	void				*ifg_pf_kif;
379 	TAILQ_HEAD(, ifg_member)	 ifg_members;
380 	TAILQ_ENTRY(ifg_group)		 ifg_next;
381 };
382 
383 struct ifg_member {
384 	TAILQ_ENTRY(ifg_member)	 ifgm_next;
385 	struct ifnet		*ifgm_ifp;
386 };
387 
388 struct ifg_list {
389 	struct ifg_group	*ifgl_group;
390 	TAILQ_ENTRY(ifg_list)	 ifgl_next;
391 };
392 
393 /* group attach event */
394 typedef void (*group_attach_event_handler_t)(void *, struct ifg_group *);
395 EVENTHANDLER_DECLARE(group_attach_event, group_attach_event_handler_t);
396 /* group detach event */
397 typedef void (*group_detach_event_handler_t)(void *, struct ifg_group *);
398 EVENTHANDLER_DECLARE(group_detach_event, group_detach_event_handler_t);
399 /* group change event */
400 typedef void (*group_change_event_handler_t)(void *, const char *);
401 EVENTHANDLER_DECLARE(group_change_event, group_change_event_handler_t);
402 
403 #define	IF_AFDATA_LOCK_INIT(ifp)	\
404 	rw_init(&(ifp)->if_afdata_lock, "if_afdata")
405 
406 #define	IF_AFDATA_WLOCK(ifp)	rw_wlock(&(ifp)->if_afdata_lock)
407 #define	IF_AFDATA_RLOCK(ifp)	rw_rlock(&(ifp)->if_afdata_lock)
408 #define	IF_AFDATA_WUNLOCK(ifp)	rw_wunlock(&(ifp)->if_afdata_lock)
409 #define	IF_AFDATA_RUNLOCK(ifp)	rw_runlock(&(ifp)->if_afdata_lock)
410 #define	IF_AFDATA_LOCK(ifp)	IF_AFDATA_WLOCK(ifp)
411 #define	IF_AFDATA_UNLOCK(ifp)	IF_AFDATA_WUNLOCK(ifp)
412 #define	IF_AFDATA_TRYLOCK(ifp)	rw_try_wlock(&(ifp)->if_afdata_lock)
413 #define	IF_AFDATA_DESTROY(ifp)	rw_destroy(&(ifp)->if_afdata_lock)
414 
415 #define	IF_AFDATA_LOCK_ASSERT(ifp)	rw_assert(&(ifp)->if_afdata_lock, RA_LOCKED)
416 #define	IF_AFDATA_UNLOCK_ASSERT(ifp)	rw_assert(&(ifp)->if_afdata_lock, RA_UNLOCKED)
417 
418 int	if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp,
419 	    int adjust);
420 #define	IF_HANDOFF(ifq, m, ifp)			\
421 	if_handoff((struct ifqueue *)ifq, m, ifp, 0)
422 #define	IF_HANDOFF_ADJ(ifq, m, ifp, adj)	\
423 	if_handoff((struct ifqueue *)ifq, m, ifp, adj)
424 
425 void	if_start(struct ifnet *);
426 
427 #define	IFQ_ENQUEUE(ifq, m, err)					\
428 do {									\
429 	IF_LOCK(ifq);							\
430 	if (ALTQ_IS_ENABLED(ifq))					\
431 		ALTQ_ENQUEUE(ifq, m, NULL, err);			\
432 	else {								\
433 		if (_IF_QFULL(ifq)) {					\
434 			m_freem(m);					\
435 			(err) = ENOBUFS;				\
436 		} else {						\
437 			_IF_ENQUEUE(ifq, m);				\
438 			(err) = 0;					\
439 		}							\
440 	}								\
441 	if (err)							\
442 		(ifq)->ifq_drops++;					\
443 	IF_UNLOCK(ifq);							\
444 } while (0)
445 
446 #define	IFQ_DEQUEUE_NOLOCK(ifq, m)					\
447 do {									\
448 	if (TBR_IS_ENABLED(ifq))					\
449 		(m) = tbr_dequeue_ptr(ifq, ALTDQ_REMOVE);		\
450 	else if (ALTQ_IS_ENABLED(ifq))					\
451 		ALTQ_DEQUEUE(ifq, m);					\
452 	else								\
453 		_IF_DEQUEUE(ifq, m);					\
454 } while (0)
455 
456 #define	IFQ_DEQUEUE(ifq, m)						\
457 do {									\
458 	IF_LOCK(ifq);							\
459 	IFQ_DEQUEUE_NOLOCK(ifq, m);					\
460 	IF_UNLOCK(ifq);							\
461 } while (0)
462 
463 #define	IFQ_POLL_NOLOCK(ifq, m)						\
464 do {									\
465 	if (TBR_IS_ENABLED(ifq))					\
466 		(m) = tbr_dequeue_ptr(ifq, ALTDQ_POLL);			\
467 	else if (ALTQ_IS_ENABLED(ifq))					\
468 		ALTQ_POLL(ifq, m);					\
469 	else								\
470 		_IF_POLL(ifq, m);					\
471 } while (0)
472 
473 #define	IFQ_POLL(ifq, m)						\
474 do {									\
475 	IF_LOCK(ifq);							\
476 	IFQ_POLL_NOLOCK(ifq, m);					\
477 	IF_UNLOCK(ifq);							\
478 } while (0)
479 
480 #define	IFQ_PURGE_NOLOCK(ifq)						\
481 do {									\
482 	if (ALTQ_IS_ENABLED(ifq)) {					\
483 		ALTQ_PURGE(ifq);					\
484 	} else								\
485 		_IF_DRAIN(ifq);						\
486 } while (0)
487 
488 #define	IFQ_PURGE(ifq)							\
489 do {									\
490 	IF_LOCK(ifq);							\
491 	IFQ_PURGE_NOLOCK(ifq);						\
492 	IF_UNLOCK(ifq);							\
493 } while (0)
494 
495 #define	IFQ_SET_READY(ifq)						\
496 	do { ((ifq)->altq_flags |= ALTQF_READY); } while (0)
497 
498 #define	IFQ_LOCK(ifq)			IF_LOCK(ifq)
499 #define	IFQ_UNLOCK(ifq)			IF_UNLOCK(ifq)
500 #define	IFQ_LOCK_ASSERT(ifq)		IF_LOCK_ASSERT(ifq)
501 #define	IFQ_IS_EMPTY(ifq)		((ifq)->ifq_len == 0)
502 #define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
503 #define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
504 #define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
505 #define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
506 
507 /*
508  * The IFF_DRV_OACTIVE test should really occur in the device driver, not in
509  * the handoff logic, as that flag is locked by the device driver.
510  */
511 #define	IFQ_HANDOFF_ADJ(ifp, m, adj, err)				\
512 do {									\
513 	int len;							\
514 	short mflags;							\
515 									\
516 	len = (m)->m_pkthdr.len;					\
517 	mflags = (m)->m_flags;						\
518 	IFQ_ENQUEUE(&(ifp)->if_snd, m, err);				\
519 	if ((err) == 0) {						\
520 		(ifp)->if_obytes += len + (adj);			\
521 		if (mflags & M_MCAST)					\
522 			(ifp)->if_omcasts++;				\
523 		if (((ifp)->if_drv_flags & IFF_DRV_OACTIVE) == 0)	\
524 			if_start(ifp);					\
525 	}								\
526 } while (0)
527 
528 #define	IFQ_HANDOFF(ifp, m, err)					\
529 	IFQ_HANDOFF_ADJ(ifp, m, 0, err)
530 
531 #define	IFQ_DRV_DEQUEUE(ifq, m)						\
532 do {									\
533 	(m) = (ifq)->ifq_drv_head;					\
534 	if (m) {							\
535 		if (((ifq)->ifq_drv_head = (m)->m_nextpkt) == NULL)	\
536 			(ifq)->ifq_drv_tail = NULL;			\
537 		(m)->m_nextpkt = NULL;					\
538 		(ifq)->ifq_drv_len--;					\
539 	} else {							\
540 		IFQ_LOCK(ifq);						\
541 		IFQ_DEQUEUE_NOLOCK(ifq, m);				\
542 		while ((ifq)->ifq_drv_len < (ifq)->ifq_drv_maxlen) {	\
543 			struct mbuf *m0;				\
544 			IFQ_DEQUEUE_NOLOCK(ifq, m0);			\
545 			if (m0 == NULL)					\
546 				break;					\
547 			m0->m_nextpkt = NULL;				\
548 			if ((ifq)->ifq_drv_tail == NULL)		\
549 				(ifq)->ifq_drv_head = m0;		\
550 			else						\
551 				(ifq)->ifq_drv_tail->m_nextpkt = m0;	\
552 			(ifq)->ifq_drv_tail = m0;			\
553 			(ifq)->ifq_drv_len++;				\
554 		}							\
555 		IFQ_UNLOCK(ifq);					\
556 	}								\
557 } while (0)
558 
559 #define	IFQ_DRV_PREPEND(ifq, m)						\
560 do {									\
561 	(m)->m_nextpkt = (ifq)->ifq_drv_head;				\
562 	if ((ifq)->ifq_drv_tail == NULL)				\
563 		(ifq)->ifq_drv_tail = (m);				\
564 	(ifq)->ifq_drv_head = (m);					\
565 	(ifq)->ifq_drv_len++;						\
566 } while (0)
567 
568 #define	IFQ_DRV_IS_EMPTY(ifq)						\
569 	(((ifq)->ifq_drv_len == 0) && ((ifq)->ifq_len == 0))
570 
571 #define	IFQ_DRV_PURGE(ifq)						\
572 do {									\
573 	struct mbuf *m, *n = (ifq)->ifq_drv_head;			\
574 	while((m = n) != NULL) {					\
575 		n = m->m_nextpkt;					\
576 		m_freem(m);						\
577 	}								\
578 	(ifq)->ifq_drv_head = (ifq)->ifq_drv_tail = NULL;		\
579 	(ifq)->ifq_drv_len = 0;						\
580 	IFQ_PURGE(ifq);							\
581 } while (0)
582 
583 #ifdef _KERNEL
584 static __inline void
585 drbr_stats_update(struct ifnet *ifp, int len, int mflags)
586 {
587 #ifndef NO_SLOW_STATS
588 	ifp->if_obytes += len;
589 	if (mflags & M_MCAST)
590 		ifp->if_omcasts++;
591 #endif
592 }
593 
594 static __inline int
595 drbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
596 {
597 	int error = 0;
598 	int len = m->m_pkthdr.len;
599 	int mflags = m->m_flags;
600 
601 #ifdef ALTQ
602 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
603 		IFQ_ENQUEUE(&ifp->if_snd, m, error);
604 		return (error);
605 	}
606 #endif
607 	if ((error = buf_ring_enqueue_bytes(br, m, len)) == ENOBUFS) {
608 		br->br_drops++;
609 		m_freem(m);
610 	} else
611 		drbr_stats_update(ifp, len, mflags);
612 
613 	return (error);
614 }
615 
616 static __inline void
617 drbr_flush(struct ifnet *ifp, struct buf_ring *br)
618 {
619 	struct mbuf *m;
620 
621 #ifdef ALTQ
622 	if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
623 		IFQ_PURGE(&ifp->if_snd);
624 #endif
625 	while ((m = buf_ring_dequeue_sc(br)) != NULL)
626 		m_freem(m);
627 }
628 
629 static __inline void
630 drbr_free(struct buf_ring *br, struct malloc_type *type)
631 {
632 
633 	drbr_flush(NULL, br);
634 	buf_ring_free(br, type);
635 }
636 
637 static __inline struct mbuf *
638 drbr_dequeue(struct ifnet *ifp, struct buf_ring *br)
639 {
640 #ifdef ALTQ
641 	struct mbuf *m;
642 
643 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
644 		IFQ_DEQUEUE(&ifp->if_snd, m);
645 		return (m);
646 	}
647 #endif
648 	return (buf_ring_dequeue_sc(br));
649 }
650 
651 static __inline struct mbuf *
652 drbr_dequeue_cond(struct ifnet *ifp, struct buf_ring *br,
653     int (*func) (struct mbuf *, void *), void *arg)
654 {
655 	struct mbuf *m;
656 #ifdef ALTQ
657 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
658 		IFQ_LOCK(&ifp->if_snd);
659 		IFQ_POLL_NOLOCK(&ifp->if_snd, m);
660 		if (m != NULL && func(m, arg) == 0) {
661 			IFQ_UNLOCK(&ifp->if_snd);
662 			return (NULL);
663 		}
664 		IFQ_DEQUEUE_NOLOCK(&ifp->if_snd, m);
665 		IFQ_UNLOCK(&ifp->if_snd);
666 		return (m);
667 	}
668 #endif
669 	m = buf_ring_peek(br);
670 	if (m == NULL || func(m, arg) == 0)
671 		return (NULL);
672 
673 	return (buf_ring_dequeue_sc(br));
674 }
675 
676 static __inline int
677 drbr_empty(struct ifnet *ifp, struct buf_ring *br)
678 {
679 #ifdef ALTQ
680 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
681 		return (IFQ_IS_EMPTY(&ifp->if_snd));
682 #endif
683 	return (buf_ring_empty(br));
684 }
685 
686 static __inline int
687 drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
688 {
689 #ifdef ALTQ
690 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
691 		return (1);
692 #endif
693 	return (!buf_ring_empty(br));
694 }
695 
696 static __inline int
697 drbr_inuse(struct ifnet *ifp, struct buf_ring *br)
698 {
699 #ifdef ALTQ
700 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
701 		return (ifp->if_snd.ifq_len);
702 #endif
703 	return (buf_ring_count(br));
704 }
705 #endif
706 /*
707  * 72 was chosen below because it is the size of a TCP/IP
708  * header (40) + the minimum mss (32).
709  */
710 #define	IF_MINMTU	72
711 #define	IF_MAXMTU	65535
712 
713 #endif /* _KERNEL */
714 
715 /*
716  * The ifaddr structure contains information about one address
717  * of an interface.  They are maintained by the different address families,
718  * are allocated and attached when an address is set, and are linked
719  * together so all addresses for an interface can be located.
720  *
721  * NOTE: a 'struct ifaddr' is always at the beginning of a larger
722  * chunk of malloc'ed memory, where we store the three addresses
723  * (ifa_addr, ifa_dstaddr and ifa_netmask) referenced here.
724  */
725 struct ifaddr {
726 	struct	sockaddr *ifa_addr;	/* address of interface */
727 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
728 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
729 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
730 	struct	if_data if_data;	/* not all members are meaningful */
731 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
732 	TAILQ_ENTRY(ifaddr) ifa_link;	/* queue macro glue */
733 	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
734 		(int, struct rtentry *, struct rt_addrinfo *);
735 	u_short	ifa_flags;		/* mostly rt_flags for cloning */
736 	u_int	ifa_refcnt;		/* references to this structure */
737 	int	ifa_metric;		/* cost of going out this interface */
738 	int (*ifa_claim_addr)		/* check if an addr goes to this if */
739 		(struct ifaddr *, struct sockaddr *);
740 	struct mtx ifa_mtx;
741 };
742 #define	IFA_ROUTE	RTF_UP		/* route installed */
743 #define IFA_RTSELF	RTF_HOST	/* loopback route to self installed */
744 
745 /* for compatibility with other BSDs */
746 #define	ifa_list	ifa_link
747 
748 #ifdef _KERNEL
749 #define	IFA_LOCK(ifa)		mtx_lock(&(ifa)->ifa_mtx)
750 #define	IFA_UNLOCK(ifa)		mtx_unlock(&(ifa)->ifa_mtx)
751 
752 void	ifa_free(struct ifaddr *ifa);
753 void	ifa_init(struct ifaddr *ifa);
754 void	ifa_ref(struct ifaddr *ifa);
755 #endif
756 
757 /*
758  * The prefix structure contains information about one prefix
759  * of an interface.  They are maintained by the different address families,
760  * are allocated and attached when a prefix or an address is set,
761  * and are linked together so all prefixes for an interface can be located.
762  */
763 struct ifprefix {
764 	struct	sockaddr *ifpr_prefix;	/* prefix of interface */
765 	struct	ifnet *ifpr_ifp;	/* back-pointer to interface */
766 	TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */
767 	u_char	ifpr_plen;		/* prefix length in bits */
768 	u_char	ifpr_type;		/* protocol dependent prefix type */
769 };
770 
771 /*
772  * Multicast address structure.  This is analogous to the ifaddr
773  * structure except that it keeps track of multicast addresses.
774  */
775 struct ifmultiaddr {
776 	TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
777 	struct	sockaddr *ifma_addr; 	/* address this membership is for */
778 	struct	sockaddr *ifma_lladdr;	/* link-layer translation, if any */
779 	struct	ifnet *ifma_ifp;	/* back-pointer to interface */
780 	u_int	ifma_refcount;		/* reference count */
781 	void	*ifma_protospec;	/* protocol-specific state, if any */
782 	struct	ifmultiaddr *ifma_llifma; /* pointer to ifma for ifma_lladdr */
783 };
784 
785 #ifdef _KERNEL
786 
787 extern	struct rwlock ifnet_rwlock;
788 extern	struct sx ifnet_sxlock;
789 
790 #define	IFNET_LOCK_INIT() do {						\
791 	rw_init_flags(&ifnet_rwlock, "ifnet_rw",  RW_RECURSE);		\
792 	sx_init_flags(&ifnet_sxlock, "ifnet_sx",  SX_RECURSE);		\
793 } while(0)
794 
795 #define	IFNET_WLOCK() do {						\
796 	sx_xlock(&ifnet_sxlock);					\
797 	rw_wlock(&ifnet_rwlock);					\
798 } while (0)
799 
800 #define	IFNET_WUNLOCK() do {						\
801 	rw_wunlock(&ifnet_rwlock);					\
802 	sx_xunlock(&ifnet_sxlock);					\
803 } while (0)
804 
805 /*
806  * To assert the ifnet lock, you must know not only whether it's for read or
807  * write, but also whether it was acquired with sleep support or not.
808  */
809 #define	IFNET_RLOCK_ASSERT()		sx_assert(&ifnet_sxlock, SA_SLOCKED)
810 #define	IFNET_RLOCK_NOSLEEP_ASSERT()	rw_assert(&ifnet_rwlock, RA_RLOCKED)
811 #define	IFNET_WLOCK_ASSERT() do {					\
812 	sx_assert(&ifnet_sxlock, SA_XLOCKED);				\
813 	rw_assert(&ifnet_rwlock, RA_WLOCKED);				\
814 } while (0)
815 
816 #define	IFNET_RLOCK()		sx_slock(&ifnet_sxlock)
817 #define	IFNET_RLOCK_NOSLEEP()	rw_rlock(&ifnet_rwlock)
818 #define	IFNET_RUNLOCK()		sx_sunlock(&ifnet_sxlock)
819 #define	IFNET_RUNLOCK_NOSLEEP()	rw_runlock(&ifnet_rwlock)
820 
821 /*
822  * Look up an ifnet given its index; the _ref variant also acquires a
823  * reference that must be freed using if_rele().  It is almost always a bug
824  * to call ifnet_byindex() instead if ifnet_byindex_ref().
825  */
826 struct ifnet	*ifnet_byindex(u_short idx);
827 struct ifnet	*ifnet_byindex_locked(u_short idx);
828 struct ifnet	*ifnet_byindex_ref(u_short idx);
829 
830 /*
831  * Given the index, ifaddr_byindex() returns the one and only
832  * link-level ifaddr for the interface. You are not supposed to use
833  * it to traverse the list of addresses associated to the interface.
834  */
835 struct ifaddr	*ifaddr_byindex(u_short idx);
836 
837 VNET_DECLARE(struct ifnethead, ifnet);
838 VNET_DECLARE(struct ifgrouphead, ifg_head);
839 VNET_DECLARE(int, if_index);
840 VNET_DECLARE(struct ifnet *, loif);	/* first loopback interface */
841 VNET_DECLARE(int, useloopback);
842 
843 #define	V_ifnet		VNET(ifnet)
844 #define	V_ifg_head	VNET(ifg_head)
845 #define	V_if_index	VNET(if_index)
846 #define	V_loif		VNET(loif)
847 #define	V_useloopback	VNET(useloopback)
848 
849 extern	int ifqmaxlen;
850 
851 int	if_addgroup(struct ifnet *, const char *);
852 int	if_delgroup(struct ifnet *, const char *);
853 int	if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
854 int	if_allmulti(struct ifnet *, int);
855 struct	ifnet* if_alloc(u_char);
856 void	if_attach(struct ifnet *);
857 void	if_dead(struct ifnet *);
858 int	if_delmulti(struct ifnet *, struct sockaddr *);
859 void	if_delmulti_ifma(struct ifmultiaddr *);
860 void	if_detach(struct ifnet *);
861 void	if_vmove(struct ifnet *, struct vnet *);
862 void	if_purgeaddrs(struct ifnet *);
863 void	if_delallmulti(struct ifnet *);
864 void	if_down(struct ifnet *);
865 struct ifmultiaddr *
866 	if_findmulti(struct ifnet *, struct sockaddr *);
867 void	if_free(struct ifnet *);
868 void	if_initname(struct ifnet *, const char *, int);
869 void	if_link_state_change(struct ifnet *, int);
870 int	if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
871 void	if_qflush(struct ifnet *);
872 void	if_ref(struct ifnet *);
873 void	if_rele(struct ifnet *);
874 int	if_setlladdr(struct ifnet *, const u_char *, int);
875 void	if_up(struct ifnet *);
876 int	ifioctl(struct socket *, u_long, caddr_t, struct thread *);
877 int	ifpromisc(struct ifnet *, int);
878 struct	ifnet *ifunit(const char *);
879 struct	ifnet *ifunit_ref(const char *);
880 
881 void	ifq_init(struct ifaltq *, struct ifnet *ifp);
882 void	ifq_delete(struct ifaltq *);
883 
884 int	ifa_add_loopback_route(struct ifaddr *, struct sockaddr *);
885 int	ifa_del_loopback_route(struct ifaddr *, struct sockaddr *);
886 
887 struct	ifaddr *ifa_ifwithaddr(struct sockaddr *);
888 int		ifa_ifwithaddr_check(struct sockaddr *);
889 struct	ifaddr *ifa_ifwithbroadaddr(struct sockaddr *);
890 struct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
891 struct	ifaddr *ifa_ifwithnet(struct sockaddr *, int);
892 struct	ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *);
893 struct	ifaddr *ifa_ifwithroute_fib(int, struct sockaddr *, struct sockaddr *, u_int);
894 
895 struct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
896 
897 int	if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);
898 
899 typedef	void *if_com_alloc_t(u_char type, struct ifnet *ifp);
900 typedef	void if_com_free_t(void *com, u_char type);
901 void	if_register_com_alloc(u_char type, if_com_alloc_t *a, if_com_free_t *f);
902 void	if_deregister_com_alloc(u_char type);
903 
904 #define IF_LLADDR(ifp)							\
905     LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr))
906 
907 #ifdef DEVICE_POLLING
908 enum poll_cmd {	POLL_ONLY, POLL_AND_CHECK_STATUS };
909 
910 typedef	int poll_handler_t(struct ifnet *ifp, enum poll_cmd cmd, int count);
911 int    ether_poll_register(poll_handler_t *h, struct ifnet *ifp);
912 int    ether_poll_deregister(struct ifnet *ifp);
913 #endif /* DEVICE_POLLING */
914 
915 #endif /* _KERNEL */
916 
917 #endif /* !_NET_IF_VAR_H_ */
918