ifq.h revision 186119
1139823Simp/*-
221259Swollman * Copyright (c) 1982, 1986, 1989, 1993
321259Swollman *	The Regents of the University of California.  All rights reserved.
421259Swollman *
521259Swollman * Redistribution and use in source and binary forms, with or without
621259Swollman * modification, are permitted provided that the following conditions
721259Swollman * are met:
821259Swollman * 1. Redistributions of source code must retain the above copyright
921259Swollman *    notice, this list of conditions and the following disclaimer.
1021259Swollman * 2. Redistributions in binary form must reproduce the above copyright
1121259Swollman *    notice, this list of conditions and the following disclaimer in the
1221259Swollman *    documentation and/or other materials provided with the distribution.
1321259Swollman * 4. Neither the name of the University nor the names of its contributors
1421259Swollman *    may be used to endorse or promote products derived from this software
1521259Swollman *    without specific prior written permission.
1621259Swollman *
1721259Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1821259Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1921259Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2021259Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2121259Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2221259Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2321259Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2421259Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2521259Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2621259Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2721259Swollman * SUCH DAMAGE.
2821259Swollman *
2921259Swollman *	From: @(#)if.h	8.1 (Berkeley) 6/10/93
3050477Speter * $FreeBSD: head/sys/net/if_var.h 186119 2008-12-15 06:10:57Z qingli $
3121259Swollman */
3221259Swollman
3321259Swollman#ifndef	_NET_IF_VAR_H_
3421259Swollman#define	_NET_IF_VAR_H_
3521259Swollman
3621259Swollman/*
3721259Swollman * Structures defining a network interface, providing a packet
3821259Swollman * transport mechanism (ala level 0 of the PUP protocols).
3921259Swollman *
4021259Swollman * Each interface accepts output datagrams of a specified maximum
4121259Swollman * length, and provides higher level routines with input datagrams
4221259Swollman * received from its medium.
4321259Swollman *
4421259Swollman * Output occurs when the routine if_output is called, with three parameters:
4521259Swollman *	(*ifp->if_output)(ifp, m, dst, rt)
4621259Swollman * Here m is the mbuf chain to be sent and dst is the destination address.
4721259Swollman * The output routine encapsulates the supplied datagram if necessary,
4821259Swollman * and then transmits it on its medium.
4921259Swollman *
5021259Swollman * On input, each interface unwraps the data received by it, and either
51108533Sschweikh * places it on the input queue of an internetwork datagram routine
5221259Swollman * and posts the associated software interrupt, or passes the datagram to a raw
5321259Swollman * packet input routine.
5421259Swollman *
5521259Swollman * Routines exist for locating interfaces by their addresses
56108533Sschweikh * or for locating an interface on a certain network, as well as more general
5721259Swollman * routing and gateway routines maintaining information used to locate
5821259Swollman * interfaces.  These routines live in the files if.c and route.c
5921259Swollman */
6021259Swollman
6121259Swollman#ifdef __STDC__
6221259Swollman/*
6321259Swollman * Forward structure declarations for function prototypes [sic].
6421259Swollman */
6521259Swollmanstruct	mbuf;
6683366Sjulianstruct	thread;
6721259Swollmanstruct	rtentry;
6885074Srustruct	rt_addrinfo;
6921259Swollmanstruct	socket;
7021259Swollmanstruct	ether_header;
71142215Sglebiusstruct	carp_if;
72155051Sglebiusstruct  ifvlantrunk;
7321259Swollman#endif
7421259Swollman
7521259Swollman#include <sys/queue.h>		/* get TAILQ macros */
7621259Swollman
7769224Sjlemon#ifdef _KERNEL
7869152Sjlemon#include <sys/mbuf.h>
79126264Smlaier#include <sys/eventhandler.h>
8069224Sjlemon#endif /* _KERNEL */
8174914Sjhb#include <sys/lock.h>		/* XXX */
8274914Sjhb#include <sys/mutex.h>		/* XXX */
8383130Sjlemon#include <sys/event.h>		/* XXX */
84132712Srwatson#include <sys/_task.h>
8569152Sjlemon
86121816Sbrooks#define	IF_DUNIT_NONE	-1
87121816Sbrooks
88130416Smlaier#include <altq/if_altq.h>
89130416Smlaier
9060938SjakeTAILQ_HEAD(ifnethead, ifnet);	/* we use TAILQs so that the order of */
9160938SjakeTAILQ_HEAD(ifaddrhead, ifaddr);	/* instantiation is preserved in the list */
9260938SjakeTAILQ_HEAD(ifprefixhead, ifprefix);
9372084SphkTAILQ_HEAD(ifmultihead, ifmultiaddr);
94159781SmlaierTAILQ_HEAD(ifgrouphead, ifg_group);
9521259Swollman
9621259Swollman/*
9721259Swollman * Structure defining a queue for a network interface.
9821259Swollman */
9921259Swollmanstruct	ifqueue {
10021259Swollman	struct	mbuf *ifq_head;
10121259Swollman	struct	mbuf *ifq_tail;
10221259Swollman	int	ifq_len;
10321259Swollman	int	ifq_maxlen;
10421259Swollman	int	ifq_drops;
10569152Sjlemon	struct	mtx ifq_mtx;
10621259Swollman};
10721259Swollman
10821259Swollman/*
10921259Swollman * Structure defining a network interface.
11021259Swollman *
11121259Swollman * (Would like to call this struct ``if'', but C isn't PL/1.)
11221259Swollman */
11384380Smjacob
11421259Swollmanstruct ifnet {
11521259Swollman	void	*if_softc;		/* pointer to driver state */
116147256Sbrooks	void	*if_l2com;		/* pointer to protocol bits */
11760938Sjake	TAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained */
118121816Sbrooks	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
119121816Sbrooks	const char *if_dname;		/* driver name */
120121816Sbrooks	int	if_dunit;		/* unit or IF_DUNIT_NONE */
12121259Swollman	struct	ifaddrhead if_addrhead;	/* linked list of addresses per if */
122128291Sluigi		/*
123128291Sluigi		 * if_addrhead is the list of all addresses associated to
124128315Sluigi		 * an interface.
125128315Sluigi		 * Some code in the kernel assumes that first element
126128315Sluigi		 * of the list has type AF_LINK, and contains sockaddr_dl
127128315Sluigi		 * addresses which store the link-level address and the name
128128291Sluigi		 * of the interface.
129128315Sluigi		 * However, access to the AF_LINK address through this
130152315Sru		 * field is deprecated. Use if_addr or ifaddr_byindex() instead.
131128291Sluigi		 */
132133741Sjmg	struct	knlist if_klist;	/* events attached to this if */
13383130Sjlemon	int	if_pcount;		/* number of promiscuous listeners */
134142901Sglebius	struct	carp_if *if_carp;	/* carp interface structure */
13521259Swollman	struct	bpf_if *if_bpf;		/* packet filter structure */
13621259Swollman	u_short	if_index;		/* numeric abbreviation for this if  */
13721259Swollman	short	if_timer;		/* time 'til if_watchdog called */
138155051Sglebius	struct  ifvlantrunk *if_vlantrunk; /* pointer to 802.1q data */
139102052Ssobomax	int	if_flags;		/* up/down, broadcast, etc. */
140162070Sandre	int	if_capabilities;	/* interface features & capabilities */
141162070Sandre	int	if_capenable;		/* enabled features & capabilities */
14221259Swollman	void	*if_linkmib;		/* link-type-specific MIB data */
14321259Swollman	size_t	if_linkmiblen;		/* length of above data */
14421259Swollman	struct	if_data if_data;
14521404Swollman	struct	ifmultihead if_multiaddrs; /* multicast addresses configured */
14621404Swollman	int	if_amcount;		/* number of all-multicast requests */
14721259Swollman/* procedure handles */
14821259Swollman	int	(*if_output)		/* output routine (enqueue) */
14992725Salfred		(struct ifnet *, struct mbuf *, struct sockaddr *,
15092725Salfred		     struct rtentry *);
151106931Ssam	void	(*if_input)		/* input routine (from h/w driver) */
152106931Ssam		(struct ifnet *, struct mbuf *);
15321259Swollman	void	(*if_start)		/* initiate output routine */
15492725Salfred		(struct ifnet *);
15521259Swollman	int	(*if_ioctl)		/* ioctl routine */
15692725Salfred		(struct ifnet *, u_long, caddr_t);
15721259Swollman	void	(*if_watchdog)		/* timer routine */
15892725Salfred		(struct ifnet *);
15921259Swollman	void	(*if_init)		/* Init routine */
16092725Salfred		(void *);
16121404Swollman	int	(*if_resolvemulti)	/* validate/resolve multicast */
16292725Salfred		(struct ifnet *, struct sockaddr **, struct sockaddr *);
163152315Sru	struct	ifaddr	*if_addr;	/* pointer to link-level address */
164174388Skmacy	void	*if_llsoftc;		/* link layer softc */
165148265Srwatson	int	if_drv_flags;		/* driver-managed status flags */
166137062Srwatson	u_int	if_spare_flags2;	/* spare flags 2 */
167130416Smlaier	struct  ifaltq if_snd;		/* output queue (includes altq) */
168123220Simp	const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */
169127828Sluigi
170146986Sthompsa	void	*if_bridge;		/* bridge glue */
171146986Sthompsa
172122524Srwatson	struct	label *if_label;	/* interface MAC label */
173121161Sume
174127828Sluigi	/* these are only used by IPv6 */
175127828Sluigi	struct	ifprefixhead if_prefixhead; /* list of prefixes per if */
176121161Sume	void	*if_afdata[AF_MAX];
177121470Sume	int	if_afdata_initialized;
178121470Sume	struct	mtx if_afdata_mtx;
179132712Srwatson	struct	task if_starttask;	/* task for IFF_NEEDSGIANT */
180145320Sglebius	struct	task if_linktask;	/* task for link change events */
181148640Srwatson	struct	mtx if_addr_mtx;	/* mutex to protect address lists */
182186119Sqingli
183152209Sthompsa	LIST_ENTRY(ifnet) if_clones;	/* interfaces of a cloner */
184159781Smlaier	TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */
185159781Smlaier					/* protected by if_addr_mtx */
186159781Smlaier	void	*if_pf_kif;
187168793Sthompsa	void	*if_lagg;		/* lagg glue */
188185162Skmacy	void	*if_pspare[8];		/* multiq/TOE 3; vimage 3; general use 4 */
189185162Skmacy	void	(*if_qflush)	/* flush any queues */
190185162Skmacy		(struct ifnet *);
191185162Skmacy	int	(*if_transmit)	/* initiate output routine */
192185162Skmacy		(struct ifnet *, struct mbuf *);
193174388Skmacy	int	if_ispare[2];		/* general use 2 */
19421259Swollman};
19569152Sjlemon
19692725Salfredtypedef void if_init_f_t(void *);
19721259Swollman
198128376Sluigi/*
199128376Sluigi * XXX These aliases are terribly dangerous because they could apply
200128376Sluigi * to anything.
201128376Sluigi */
20221259Swollman#define	if_mtu		if_data.ifi_mtu
20321259Swollman#define	if_type		if_data.ifi_type
20421259Swollman#define if_physical	if_data.ifi_physical
20521259Swollman#define	if_addrlen	if_data.ifi_addrlen
20621259Swollman#define	if_hdrlen	if_data.ifi_hdrlen
20721259Swollman#define	if_metric	if_data.ifi_metric
208128871Sandre#define	if_link_state	if_data.ifi_link_state
20921259Swollman#define	if_baudrate	if_data.ifi_baudrate
21058698Sjlemon#define	if_hwassist	if_data.ifi_hwassist
21121259Swollman#define	if_ipackets	if_data.ifi_ipackets
21221259Swollman#define	if_ierrors	if_data.ifi_ierrors
21321259Swollman#define	if_opackets	if_data.ifi_opackets
21421259Swollman#define	if_oerrors	if_data.ifi_oerrors
21521259Swollman#define	if_collisions	if_data.ifi_collisions
21621259Swollman#define	if_ibytes	if_data.ifi_ibytes
21721259Swollman#define	if_obytes	if_data.ifi_obytes
21821259Swollman#define	if_imcasts	if_data.ifi_imcasts
21921259Swollman#define	if_omcasts	if_data.ifi_omcasts
22021259Swollman#define	if_iqdrops	if_data.ifi_iqdrops
22121259Swollman#define	if_noproto	if_data.ifi_noproto
22221259Swollman#define	if_lastchange	if_data.ifi_lastchange
223136950Sjmg#define if_rawoutput(if, m, sa) if_output(if, m, sa, (struct rtentry *)NULL)
22421259Swollman
22553541Sshin/* for compatibility with other BSDs */
22653541Sshin#define	if_addrlist	if_addrhead
22753541Sshin#define	if_list		if_link
228160981Sbrooks#define	if_name(ifp)	((ifp)->if_xname)
22953541Sshin
23021259Swollman/*
231148640Srwatson * Locks for address lists on the network interface.
232148640Srwatson */
233148640Srwatson#define	IF_ADDR_LOCK_INIT(if)	mtx_init(&(if)->if_addr_mtx,		\
234148640Srwatson				    "if_addr_mtx", NULL, MTX_DEF)
235148640Srwatson#define	IF_ADDR_LOCK_DESTROY(if)	mtx_destroy(&(if)->if_addr_mtx)
236148640Srwatson#define	IF_ADDR_LOCK(if)	mtx_lock(&(if)->if_addr_mtx)
237148640Srwatson#define	IF_ADDR_UNLOCK(if)	mtx_unlock(&(if)->if_addr_mtx)
238148640Srwatson#define	IF_ADDR_LOCK_ASSERT(if)	mtx_assert(&(if)->if_addr_mtx, MA_OWNED)
239148640Srwatson
240148640Srwatson/*
24121259Swollman * Output queues (ifp->if_snd) and slow device input queues (*ifp->if_slowq)
24221259Swollman * are queues of messages stored on ifqueue structures
24321259Swollman * (defined above).  Entries are added to and deleted from these structures
24421259Swollman * by these macros, which should be called with ipl raised to splimp().
24521259Swollman */
24672200Sbmilekic#define IF_LOCK(ifq)		mtx_lock(&(ifq)->ifq_mtx)
24772200Sbmilekic#define IF_UNLOCK(ifq)		mtx_unlock(&(ifq)->ifq_mtx)
248130416Smlaier#define	IF_LOCK_ASSERT(ifq)	mtx_assert(&(ifq)->ifq_mtx, MA_OWNED)
24969152Sjlemon#define	_IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
25069152Sjlemon#define	_IF_DROP(ifq)		((ifq)->ifq_drops++)
25169152Sjlemon#define	_IF_QLEN(ifq)		((ifq)->ifq_len)
25221259Swollman
25369152Sjlemon#define	_IF_ENQUEUE(ifq, m) do { 				\
25469152Sjlemon	(m)->m_nextpkt = NULL;					\
25569152Sjlemon	if ((ifq)->ifq_tail == NULL) 				\
25669152Sjlemon		(ifq)->ifq_head = m; 				\
25769152Sjlemon	else 							\
25869152Sjlemon		(ifq)->ifq_tail->m_nextpkt = m; 		\
25969152Sjlemon	(ifq)->ifq_tail = m; 					\
26069152Sjlemon	(ifq)->ifq_len++; 					\
26169152Sjlemon} while (0)
26269152Sjlemon
26369152Sjlemon#define IF_ENQUEUE(ifq, m) do {					\
26469152Sjlemon	IF_LOCK(ifq); 						\
26569152Sjlemon	_IF_ENQUEUE(ifq, m); 					\
26669152Sjlemon	IF_UNLOCK(ifq); 					\
26769152Sjlemon} while (0)
26869152Sjlemon
26969152Sjlemon#define	_IF_PREPEND(ifq, m) do {				\
27069152Sjlemon	(m)->m_nextpkt = (ifq)->ifq_head; 			\
27169152Sjlemon	if ((ifq)->ifq_tail == NULL) 				\
27269152Sjlemon		(ifq)->ifq_tail = (m); 				\
27369152Sjlemon	(ifq)->ifq_head = (m); 					\
27469152Sjlemon	(ifq)->ifq_len++; 					\
27569152Sjlemon} while (0)
27669152Sjlemon
27769152Sjlemon#define IF_PREPEND(ifq, m) do {		 			\
27869152Sjlemon	IF_LOCK(ifq); 						\
27969152Sjlemon	_IF_PREPEND(ifq, m); 					\
28069152Sjlemon	IF_UNLOCK(ifq); 					\
28169152Sjlemon} while (0)
28269152Sjlemon
28369152Sjlemon#define	_IF_DEQUEUE(ifq, m) do { 				\
28469152Sjlemon	(m) = (ifq)->ifq_head; 					\
28569152Sjlemon	if (m) { 						\
286136950Sjmg		if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL)	\
28769152Sjlemon			(ifq)->ifq_tail = NULL; 		\
28869152Sjlemon		(m)->m_nextpkt = NULL; 				\
28969152Sjlemon		(ifq)->ifq_len--; 				\
29069152Sjlemon	} 							\
29169152Sjlemon} while (0)
29269152Sjlemon
29369152Sjlemon#define IF_DEQUEUE(ifq, m) do { 				\
29469152Sjlemon	IF_LOCK(ifq); 						\
29569152Sjlemon	_IF_DEQUEUE(ifq, m); 					\
29669152Sjlemon	IF_UNLOCK(ifq); 					\
29769152Sjlemon} while (0)
29869152Sjlemon
299130416Smlaier#define	_IF_POLL(ifq, m)	((m) = (ifq)->ifq_head)
300130416Smlaier#define	IF_POLL(ifq, m)		_IF_POLL(ifq, m)
301130416Smlaier
302130416Smlaier#define _IF_DRAIN(ifq) do { 					\
30369152Sjlemon	struct mbuf *m; 					\
30469152Sjlemon	for (;;) { 						\
30569152Sjlemon		_IF_DEQUEUE(ifq, m); 				\
30669152Sjlemon		if (m == NULL) 					\
30769152Sjlemon			break; 					\
30869152Sjlemon		m_freem(m); 					\
30969152Sjlemon	} 							\
31069152Sjlemon} while (0)
31169152Sjlemon
312130416Smlaier#define IF_DRAIN(ifq) do {					\
313130416Smlaier	IF_LOCK(ifq);						\
314130416Smlaier	_IF_DRAIN(ifq);						\
315130416Smlaier	IF_UNLOCK(ifq);						\
316130416Smlaier} while(0)
317130416Smlaier
31855205Speter#ifdef _KERNEL
319126264Smlaier/* interface address change event */
320126264Smlaiertypedef void (*ifaddr_event_handler_t)(void *, struct ifnet *);
321126264SmlaierEVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
322126264Smlaier/* new interface arrival event */
323126264Smlaiertypedef void (*ifnet_arrival_event_handler_t)(void *, struct ifnet *);
324126264SmlaierEVENTHANDLER_DECLARE(ifnet_arrival_event, ifnet_arrival_event_handler_t);
325126264Smlaier/* interface departure event */
326126264Smlaiertypedef void (*ifnet_departure_event_handler_t)(void *, struct ifnet *);
327126264SmlaierEVENTHANDLER_DECLARE(ifnet_departure_event, ifnet_departure_event_handler_t);
328126264Smlaier
329159781Smlaier/*
330159781Smlaier * interface groups
331159781Smlaier */
332159781Smlaierstruct ifg_group {
333159781Smlaier	char				 ifg_group[IFNAMSIZ];
334159781Smlaier	u_int				 ifg_refcnt;
335159781Smlaier	void				*ifg_pf_kif;
336159781Smlaier	TAILQ_HEAD(, ifg_member)	 ifg_members;
337159781Smlaier	TAILQ_ENTRY(ifg_group)		 ifg_next;
338159781Smlaier};
339159781Smlaier
340159781Smlaierstruct ifg_member {
341159781Smlaier	TAILQ_ENTRY(ifg_member)	 ifgm_next;
342159781Smlaier	struct ifnet		*ifgm_ifp;
343159781Smlaier};
344159781Smlaier
345159781Smlaierstruct ifg_list {
346159781Smlaier	struct ifg_group	*ifgl_group;
347159781Smlaier	TAILQ_ENTRY(ifg_list)	 ifgl_next;
348159781Smlaier};
349159781Smlaier
350159781Smlaier/* group attach event */
351159781Smlaiertypedef void (*group_attach_event_handler_t)(void *, struct ifg_group *);
352159781SmlaierEVENTHANDLER_DECLARE(group_attach_event, group_attach_event_handler_t);
353159781Smlaier/* group detach event */
354159781Smlaiertypedef void (*group_detach_event_handler_t)(void *, struct ifg_group *);
355159781SmlaierEVENTHANDLER_DECLARE(group_detach_event, group_detach_event_handler_t);
356159781Smlaier/* group change event */
357159781Smlaiertypedef void (*group_change_event_handler_t)(void *, const char *);
358159781SmlaierEVENTHANDLER_DECLARE(group_change_event, group_change_event_handler_t);
359159781Smlaier
360121470Sume#define	IF_AFDATA_LOCK_INIT(ifp)	\
361121470Sume    mtx_init(&(ifp)->if_afdata_mtx, "if_afdata", NULL, MTX_DEF)
362121470Sume#define	IF_AFDATA_LOCK(ifp)	mtx_lock(&(ifp)->if_afdata_mtx)
363121470Sume#define	IF_AFDATA_TRYLOCK(ifp)	mtx_trylock(&(ifp)->if_afdata_mtx)
364121470Sume#define	IF_AFDATA_UNLOCK(ifp)	mtx_unlock(&(ifp)->if_afdata_mtx)
365121470Sume#define	IF_AFDATA_DESTROY(ifp)	mtx_destroy(&(ifp)->if_afdata_mtx)
366121470Sume
367186119Sqingli#define	IF_AFDATA_LOCK_ASSERT(ifp)	mtx_assert(&(ifp)->if_afdata_mtx, MA_OWNED)
368186119Sqingli#define	IF_AFDATA_UNLOCK_ASSERT(ifp)	mtx_assert(&(ifp)->if_afdata_mtx, MA_NOTOWNED)
369186119Sqingli
370136704Srwatson#define	IFF_LOCKGIANT(ifp) do {						\
371136704Srwatson	if ((ifp)->if_flags & IFF_NEEDSGIANT)				\
372136704Srwatson		mtx_lock(&Giant);					\
373136704Srwatson} while (0)
374136704Srwatson
375136704Srwatson#define	IFF_UNLOCKGIANT(ifp) do {					\
376136704Srwatson	if ((ifp)->if_flags & IFF_NEEDSGIANT)				\
377136704Srwatson		mtx_unlock(&Giant);					\
378136704Srwatson} while (0)
379136704Srwatson
380137065Srwatsonint	if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp,
381137065Srwatson	    int adjust);
382130416Smlaier#define	IF_HANDOFF(ifq, m, ifp)			\
383130416Smlaier	if_handoff((struct ifqueue *)ifq, m, ifp, 0)
384130416Smlaier#define	IF_HANDOFF_ADJ(ifq, m, ifp, adj)	\
385130416Smlaier	if_handoff((struct ifqueue *)ifq, m, ifp, adj)
38621259Swollman
387132712Srwatsonvoid	if_start(struct ifnet *);
388132712Srwatson
389130416Smlaier#define	IFQ_ENQUEUE(ifq, m, err)					\
390130416Smlaierdo {									\
391130416Smlaier	IF_LOCK(ifq);							\
392130416Smlaier	if (ALTQ_IS_ENABLED(ifq))					\
393130416Smlaier		ALTQ_ENQUEUE(ifq, m, NULL, err);			\
394130416Smlaier	else {								\
395130416Smlaier		if (_IF_QFULL(ifq)) {					\
396130416Smlaier			m_freem(m);					\
397130416Smlaier			(err) = ENOBUFS;				\
398130416Smlaier		} else {						\
399130416Smlaier			_IF_ENQUEUE(ifq, m);				\
400130416Smlaier			(err) = 0;					\
401130416Smlaier		}							\
402130416Smlaier	}								\
403130416Smlaier	if (err)							\
404130416Smlaier		(ifq)->ifq_drops++;					\
405130416Smlaier	IF_UNLOCK(ifq);							\
406130416Smlaier} while (0)
40721259Swollman
408130416Smlaier#define	IFQ_DEQUEUE_NOLOCK(ifq, m)					\
409130416Smlaierdo {									\
410130416Smlaier	if (TBR_IS_ENABLED(ifq))					\
411130508Smlaier		(m) = tbr_dequeue_ptr(ifq, ALTDQ_REMOVE);		\
412130416Smlaier	else if (ALTQ_IS_ENABLED(ifq))					\
413130416Smlaier		ALTQ_DEQUEUE(ifq, m);					\
414130416Smlaier	else								\
415130416Smlaier		_IF_DEQUEUE(ifq, m);					\
416130416Smlaier} while (0)
417130416Smlaier
418130416Smlaier#define	IFQ_DEQUEUE(ifq, m)						\
419130416Smlaierdo {									\
420130416Smlaier	IF_LOCK(ifq);							\
421130416Smlaier	IFQ_DEQUEUE_NOLOCK(ifq, m);					\
422130416Smlaier	IF_UNLOCK(ifq);							\
423130416Smlaier} while (0)
424130416Smlaier
425130416Smlaier#define	IFQ_POLL_NOLOCK(ifq, m)						\
426130416Smlaierdo {									\
427130416Smlaier	if (TBR_IS_ENABLED(ifq))					\
428130508Smlaier		(m) = tbr_dequeue_ptr(ifq, ALTDQ_POLL);			\
429130416Smlaier	else if (ALTQ_IS_ENABLED(ifq))					\
430130416Smlaier		ALTQ_POLL(ifq, m);					\
431130416Smlaier	else								\
432130416Smlaier		_IF_POLL(ifq, m);					\
433130416Smlaier} while (0)
434130416Smlaier
435130416Smlaier#define	IFQ_POLL(ifq, m)						\
436130416Smlaierdo {									\
437130416Smlaier	IF_LOCK(ifq);							\
438130416Smlaier	IFQ_POLL_NOLOCK(ifq, m);					\
439130416Smlaier	IF_UNLOCK(ifq);							\
440130416Smlaier} while (0)
441130416Smlaier
442130416Smlaier#define	IFQ_PURGE_NOLOCK(ifq)						\
443130416Smlaierdo {									\
444130416Smlaier	if (ALTQ_IS_ENABLED(ifq)) {					\
445130416Smlaier		ALTQ_PURGE(ifq);					\
446130416Smlaier	} else								\
447130416Smlaier		_IF_DRAIN(ifq);						\
448130416Smlaier} while (0)
449130416Smlaier
450130416Smlaier#define	IFQ_PURGE(ifq)							\
451130416Smlaierdo {									\
452130416Smlaier	IF_LOCK(ifq);							\
453130416Smlaier	IFQ_PURGE_NOLOCK(ifq);						\
454130416Smlaier	IF_UNLOCK(ifq);							\
455130416Smlaier} while (0)
456130416Smlaier
457130416Smlaier#define	IFQ_SET_READY(ifq)						\
458130416Smlaier	do { ((ifq)->altq_flags |= ALTQF_READY); } while (0)
459130416Smlaier
460130416Smlaier#define	IFQ_LOCK(ifq)			IF_LOCK(ifq)
461130416Smlaier#define	IFQ_UNLOCK(ifq)			IF_UNLOCK(ifq)
462130416Smlaier#define	IFQ_LOCK_ASSERT(ifq)		IF_LOCK_ASSERT(ifq)
463130416Smlaier#define	IFQ_IS_EMPTY(ifq)		((ifq)->ifq_len == 0)
464130416Smlaier#define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
465130416Smlaier#define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
466130416Smlaier#define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
467130416Smlaier#define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
468130416Smlaier
469148886Srwatson/*
470148886Srwatson * The IFF_DRV_OACTIVE test should really occur in the device driver, not in
471148886Srwatson * the handoff logic, as that flag is locked by the device driver.
472148886Srwatson */
473130416Smlaier#define	IFQ_HANDOFF_ADJ(ifp, m, adj, err)				\
474130416Smlaierdo {									\
475130416Smlaier	int len;							\
476130416Smlaier	short mflags;							\
477130416Smlaier									\
478130416Smlaier	len = (m)->m_pkthdr.len;					\
479130416Smlaier	mflags = (m)->m_flags;						\
480130416Smlaier	IFQ_ENQUEUE(&(ifp)->if_snd, m, err);				\
481130416Smlaier	if ((err) == 0) {						\
482130416Smlaier		(ifp)->if_obytes += len + (adj);			\
483130416Smlaier		if (mflags & M_MCAST)					\
484130416Smlaier			(ifp)->if_omcasts++;				\
485148886Srwatson		if (((ifp)->if_drv_flags & IFF_DRV_OACTIVE) == 0)	\
486132712Srwatson			if_start(ifp);					\
487130416Smlaier	}								\
488130416Smlaier} while (0)
489130416Smlaier
490130416Smlaier#define	IFQ_HANDOFF(ifp, m, err)					\
491130512Smlaier	IFQ_HANDOFF_ADJ(ifp, m, 0, err)
492130416Smlaier
493130416Smlaier#define	IFQ_DRV_DEQUEUE(ifq, m)						\
494130416Smlaierdo {									\
495130416Smlaier	(m) = (ifq)->ifq_drv_head;					\
496130416Smlaier	if (m) {							\
497130416Smlaier		if (((ifq)->ifq_drv_head = (m)->m_nextpkt) == NULL)	\
498130416Smlaier			(ifq)->ifq_drv_tail = NULL;			\
499130416Smlaier		(m)->m_nextpkt = NULL;					\
500130416Smlaier		(ifq)->ifq_drv_len--;					\
501130416Smlaier	} else {							\
502130416Smlaier		IFQ_LOCK(ifq);						\
503130416Smlaier		IFQ_DEQUEUE_NOLOCK(ifq, m);				\
504130416Smlaier		while ((ifq)->ifq_drv_len < (ifq)->ifq_drv_maxlen) {	\
505130416Smlaier			struct mbuf *m0;				\
506130416Smlaier			IFQ_DEQUEUE_NOLOCK(ifq, m0);			\
507130416Smlaier			if (m0 == NULL)					\
508130416Smlaier				break;					\
509130416Smlaier			m0->m_nextpkt = NULL;				\
510130416Smlaier			if ((ifq)->ifq_drv_tail == NULL)		\
511130416Smlaier				(ifq)->ifq_drv_head = m0;		\
512130416Smlaier			else						\
513130416Smlaier				(ifq)->ifq_drv_tail->m_nextpkt = m0;	\
514130416Smlaier			(ifq)->ifq_drv_tail = m0;			\
515130416Smlaier			(ifq)->ifq_drv_len++;				\
516130416Smlaier		}							\
517130416Smlaier		IFQ_UNLOCK(ifq);					\
518130416Smlaier	}								\
519130416Smlaier} while (0)
520130416Smlaier
521130416Smlaier#define	IFQ_DRV_PREPEND(ifq, m)						\
522130416Smlaierdo {									\
523130416Smlaier	(m)->m_nextpkt = (ifq)->ifq_drv_head;				\
524132152Smlaier	if ((ifq)->ifq_drv_tail == NULL)				\
525132152Smlaier		(ifq)->ifq_drv_tail = (m);				\
526130416Smlaier	(ifq)->ifq_drv_head = (m);					\
527130416Smlaier	(ifq)->ifq_drv_len++;						\
528130416Smlaier} while (0)
529130416Smlaier
530130416Smlaier#define	IFQ_DRV_IS_EMPTY(ifq)						\
531130416Smlaier	(((ifq)->ifq_drv_len == 0) && ((ifq)->ifq_len == 0))
532130416Smlaier
533130416Smlaier#define	IFQ_DRV_PURGE(ifq)						\
534130416Smlaierdo {									\
535132152Smlaier	struct mbuf *m, *n = (ifq)->ifq_drv_head;			\
536132152Smlaier	while((m = n) != NULL) {					\
537132152Smlaier		n = m->m_nextpkt;					\
538130416Smlaier		m_freem(m);						\
539130416Smlaier	}								\
540130416Smlaier	(ifq)->ifq_drv_head = (ifq)->ifq_drv_tail = NULL;		\
541130416Smlaier	(ifq)->ifq_drv_len = 0;						\
542130416Smlaier	IFQ_PURGE(ifq);							\
543130416Smlaier} while (0)
544130416Smlaier
54549459Sbrian/*
54649459Sbrian * 72 was chosen below because it is the size of a TCP/IP
54749459Sbrian * header (40) + the minimum mss (32).
54849459Sbrian */
54949459Sbrian#define	IF_MINMTU	72
55049459Sbrian#define	IF_MAXMTU	65535
55149459Sbrian
55255205Speter#endif /* _KERNEL */
55321259Swollman
55421259Swollman/*
55521259Swollman * The ifaddr structure contains information about one address
55621259Swollman * of an interface.  They are maintained by the different address families,
55721259Swollman * are allocated and attached when an address is set, and are linked
55821259Swollman * together so all addresses for an interface can be located.
559128291Sluigi *
560128291Sluigi * NOTE: a 'struct ifaddr' is always at the beginning of a larger
561128291Sluigi * chunk of malloc'ed memory, where we store the three addresses
562128291Sluigi * (ifa_addr, ifa_dstaddr and ifa_netmask) referenced here.
56321259Swollman */
56421259Swollmanstruct ifaddr {
56521259Swollman	struct	sockaddr *ifa_addr;	/* address of interface */
56621259Swollman	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
56721259Swollman#define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
56821259Swollman	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
56967334Sjoe	struct	if_data if_data;	/* not all members are meaningful */
57021259Swollman	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
57160938Sjake	TAILQ_ENTRY(ifaddr) ifa_link;	/* queue macro glue */
57221259Swollman	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
57392725Salfred		(int, struct rtentry *, struct rt_addrinfo *);
57421259Swollman	u_short	ifa_flags;		/* mostly rt_flags for cloning */
57547254Spb	u_int	ifa_refcnt;		/* references to this structure */
57621259Swollman	int	ifa_metric;		/* cost of going out this interface */
57728845Sjulian	int (*ifa_claim_addr)		/* check if an addr goes to this if */
57892725Salfred		(struct ifaddr *, struct sockaddr *);
579108033Shsu	struct mtx ifa_mtx;
58021259Swollman};
58121259Swollman#define	IFA_ROUTE	RTF_UP		/* route installed */
58221259Swollman
58353541Sshin/* for compatibility with other BSDs */
58453541Sshin#define	ifa_list	ifa_link
58553541Sshin
586108033Shsu#define	IFA_LOCK_INIT(ifa)	\
587108033Shsu    mtx_init(&(ifa)->ifa_mtx, "ifaddr", NULL, MTX_DEF)
588108033Shsu#define	IFA_LOCK(ifa)		mtx_lock(&(ifa)->ifa_mtx)
589108033Shsu#define	IFA_UNLOCK(ifa)		mtx_unlock(&(ifa)->ifa_mtx)
590108033Shsu#define	IFA_DESTROY(ifa)	mtx_destroy(&(ifa)->ifa_mtx)
591108033Shsu
59221404Swollman/*
59352904Sshin * The prefix structure contains information about one prefix
59452904Sshin * of an interface.  They are maintained by the different address families,
595108470Sschweikh * are allocated and attached when a prefix or an address is set,
59653541Sshin * and are linked together so all prefixes for an interface can be located.
59752904Sshin */
59852904Sshinstruct ifprefix {
59952904Sshin	struct	sockaddr *ifpr_prefix;	/* prefix of interface */
60052904Sshin	struct	ifnet *ifpr_ifp;	/* back-pointer to interface */
60160938Sjake	TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */
60252904Sshin	u_char	ifpr_plen;		/* prefix length in bits */
60352904Sshin	u_char	ifpr_type;		/* protocol dependent prefix type */
60452904Sshin};
60552904Sshin
60652904Sshin/*
60721404Swollman * Multicast address structure.  This is analogous to the ifaddr
60821404Swollman * structure except that it keeps track of multicast addresses.
60921404Swollman */
61021404Swollmanstruct ifmultiaddr {
61172084Sphk	TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
61221434Swollman	struct	sockaddr *ifma_addr; 	/* address this membership is for */
61321434Swollman	struct	sockaddr *ifma_lladdr;	/* link-layer translation, if any */
61421434Swollman	struct	ifnet *ifma_ifp;	/* back-pointer to interface */
61521434Swollman	u_int	ifma_refcount;		/* reference count */
61621434Swollman	void	*ifma_protospec;	/* protocol-specific state, if any */
617167729Sbms	struct	ifmultiaddr *ifma_llifma; /* pointer to ifma for ifma_lladdr */
61821404Swollman};
61921404Swollman
62055205Speter#ifdef _KERNEL
621108036Shsu#define	IFAFREE(ifa)					\
622108036Shsu	do {						\
623108036Shsu		IFA_LOCK(ifa);				\
624108036Shsu		KASSERT((ifa)->ifa_refcnt > 0,		\
625108036Shsu		    ("ifa %p !(ifa_refcnt > 0)", ifa));	\
626108036Shsu		if (--(ifa)->ifa_refcnt == 0) {		\
627108036Shsu			IFA_DESTROY(ifa);		\
628108036Shsu			free(ifa, M_IFADDR);		\
629108036Shsu		} else 					\
630108036Shsu			IFA_UNLOCK(ifa);		\
63146568Speter	} while (0)
63221259Swollman
633108036Shsu#define IFAREF(ifa)					\
634108036Shsu	do {						\
635108036Shsu		IFA_LOCK(ifa);				\
636108036Shsu		++(ifa)->ifa_refcnt;			\
637108036Shsu		IFA_UNLOCK(ifa);			\
638108033Shsu	} while (0)
639108033Shsu
640108172Shsuextern	struct mtx ifnet_lock;
641108298Shsu#define	IFNET_LOCK_INIT() \
642108298Shsu    mtx_init(&ifnet_lock, "ifnet", NULL, MTX_DEF | MTX_RECURSE)
643108172Shsu#define	IFNET_WLOCK()		mtx_lock(&ifnet_lock)
644108172Shsu#define	IFNET_WUNLOCK()		mtx_unlock(&ifnet_lock)
645180042Srwatson#define	IFNET_WLOCK_ASSERT()	mtx_assert(&ifnet_lock, MA_OWNED)
646108172Shsu#define	IFNET_RLOCK()		IFNET_WLOCK()
647108172Shsu#define	IFNET_RUNLOCK()		IFNET_WUNLOCK()
648108172Shsu
64983130Sjlemonstruct ifindex_entry {
65087914Sjlemon	struct	ifnet *ife_ifnet;
651130585Sphk	struct cdev *ife_dev;
65283130Sjlemon};
65383130Sjlemon
654180042Srwatsonstruct ifnet	*ifnet_byindex(u_short idx);
655181892Skmacy
656128315Sluigi/*
657128315Sluigi * Given the index, ifaddr_byindex() returns the one and only
658128315Sluigi * link-level ifaddr for the interface. You are not supposed to use
659128315Sluigi * it to traverse the list of addresses associated to the interface.
660128315Sluigi */
661180042Srwatsonstruct ifaddr	*ifaddr_byindex(u_short idx);
662180042Srwatsonstruct cdev	*ifdev_byindex(u_short idx);
66383130Sjlemon
664186048Sbz#ifdef VIMAGE_GLOBALS
66521259Swollmanextern	struct ifnethead ifnet;
66671791Speterextern	struct ifnet *loif;	/* first loopback interface */
66721259Swollmanextern	int if_index;
668186048Sbz#endif
669186048Sbzextern	int ifqmaxlen;
67021259Swollman
671159781Smlaierint	if_addgroup(struct ifnet *, const char *);
672159781Smlaierint	if_delgroup(struct ifnet *, const char *);
67392725Salfredint	if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
67492725Salfredint	if_allmulti(struct ifnet *, int);
675147256Sbrooksstruct	ifnet* if_alloc(u_char);
67692725Salfredvoid	if_attach(struct ifnet *);
67792725Salfredint	if_delmulti(struct ifnet *, struct sockaddr *);
678167729Sbmsvoid	if_delmulti_ifma(struct ifmultiaddr *);
67992725Salfredvoid	if_detach(struct ifnet *);
680146620Speadarvoid	if_purgeaddrs(struct ifnet *);
681177617Ssamvoid	if_purgemaddrs(struct ifnet *);
68292725Salfredvoid	if_down(struct ifnet *);
683167732Sbmsstruct ifmultiaddr *
684167732Sbms	if_findmulti(struct ifnet *, struct sockaddr *);
685147256Sbrooksvoid	if_free(struct ifnet *);
686147256Sbrooksvoid	if_free_type(struct ifnet *, u_char);
687121816Sbrooksvoid	if_initname(struct ifnet *, const char *, int);
688138542Ssamvoid	if_link_state_change(struct ifnet *, int);
689103900Sbrooksint	if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
69092725Salfredint	if_setlladdr(struct ifnet *, const u_char *, int);
69192725Salfredvoid	if_up(struct ifnet *);
69292725Salfred/*void	ifinit(void);*/ /* declared in systm.h for main() */
69392725Salfredint	ifioctl(struct socket *, u_long, caddr_t, struct thread *);
69492725Salfredint	ifpromisc(struct ifnet *, int);
69592725Salfredstruct	ifnet *ifunit(const char *);
69621259Swollman
697185162Skmacyvoid	ifq_attach(struct ifaltq *, struct ifnet *ifp);
698185162Skmacyvoid	ifq_detach(struct ifaltq *);
699185162Skmacy
70092725Salfredstruct	ifaddr *ifa_ifwithaddr(struct sockaddr *);
701162068Sandrestruct	ifaddr *ifa_ifwithbroadaddr(struct sockaddr *);
70292725Salfredstruct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
70392725Salfredstruct	ifaddr *ifa_ifwithnet(struct sockaddr *);
70492725Salfredstruct	ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *);
705178888Sjulianstruct	ifaddr *ifa_ifwithroute_fib(int, struct sockaddr *, struct sockaddr *, u_int);
706178888Sjulian
70792725Salfredstruct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
70821259Swollman
70992725Salfredint	if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);
71021434Swollman
711147256Sbrookstypedef	void *if_com_alloc_t(u_char type, struct ifnet *ifp);
712147256Sbrookstypedef	void if_com_free_t(void *com, u_char type);
713147256Sbrooksvoid	if_register_com_alloc(u_char type, if_com_alloc_t *a, if_com_free_t *f);
714147256Sbrooksvoid	if_deregister_com_alloc(u_char type);
715147256Sbrooks
71684931Sfjoe#define IF_LLADDR(ifp)							\
717152315Sru    LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr))
71884931Sfjoe
71987902Sluigi#ifdef DEVICE_POLLING
720150789Sglebiusenum poll_cmd {	POLL_ONLY, POLL_AND_CHECK_STATUS };
72187902Sluigi
72292725Salfredtypedef	void poll_handler_t(struct ifnet *ifp, enum poll_cmd cmd, int count);
72392725Salfredint    ether_poll_register(poll_handler_t *h, struct ifnet *ifp);
72492725Salfredint    ether_poll_deregister(struct ifnet *ifp);
72587902Sluigi#endif /* DEVICE_POLLING */
72687902Sluigi
72755205Speter#endif /* _KERNEL */
72821259Swollman
72921259Swollman#endif /* !_NET_IF_VAR_H_ */
730