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