ifq.h revision 189230
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 189230 2009-03-01 12:42:54Z rwatson $
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>
80186207Skmacy#include <sys/buf_ring.h>
8169224Sjlemon#endif /* _KERNEL */
8274914Sjhb#include <sys/lock.h>		/* XXX */
8374914Sjhb#include <sys/mutex.h>		/* XXX */
84186199Skmacy#include <sys/rwlock.h>		/* XXX */
8583130Sjlemon#include <sys/event.h>		/* XXX */
86132712Srwatson#include <sys/_task.h>
8769152Sjlemon
88121816Sbrooks#define	IF_DUNIT_NONE	-1
89121816Sbrooks
90130416Smlaier#include <altq/if_altq.h>
91130416Smlaier
9260938SjakeTAILQ_HEAD(ifnethead, ifnet);	/* we use TAILQs so that the order of */
9360938SjakeTAILQ_HEAD(ifaddrhead, ifaddr);	/* instantiation is preserved in the list */
9460938SjakeTAILQ_HEAD(ifprefixhead, ifprefix);
9572084SphkTAILQ_HEAD(ifmultihead, ifmultiaddr);
96159781SmlaierTAILQ_HEAD(ifgrouphead, ifg_group);
9721259Swollman
9821259Swollman/*
9921259Swollman * Structure defining a queue for a network interface.
10021259Swollman */
10121259Swollmanstruct	ifqueue {
10221259Swollman	struct	mbuf *ifq_head;
10321259Swollman	struct	mbuf *ifq_tail;
10421259Swollman	int	ifq_len;
10521259Swollman	int	ifq_maxlen;
10621259Swollman	int	ifq_drops;
10769152Sjlemon	struct	mtx ifq_mtx;
10821259Swollman};
10921259Swollman
11021259Swollman/*
11121259Swollman * Structure defining a network interface.
11221259Swollman *
11321259Swollman * (Would like to call this struct ``if'', but C isn't PL/1.)
11421259Swollman */
11584380Smjacob
11621259Swollmanstruct ifnet {
11721259Swollman	void	*if_softc;		/* pointer to driver state */
118147256Sbrooks	void	*if_l2com;		/* pointer to protocol bits */
11960938Sjake	TAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained */
120121816Sbrooks	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
121121816Sbrooks	const char *if_dname;		/* driver name */
122121816Sbrooks	int	if_dunit;		/* unit or IF_DUNIT_NONE */
12321259Swollman	struct	ifaddrhead if_addrhead;	/* linked list of addresses per if */
124128291Sluigi		/*
125128291Sluigi		 * if_addrhead is the list of all addresses associated to
126128315Sluigi		 * an interface.
127128315Sluigi		 * Some code in the kernel assumes that first element
128128315Sluigi		 * of the list has type AF_LINK, and contains sockaddr_dl
129128315Sluigi		 * addresses which store the link-level address and the name
130128291Sluigi		 * of the interface.
131128315Sluigi		 * However, access to the AF_LINK address through this
132152315Sru		 * field is deprecated. Use if_addr or ifaddr_byindex() instead.
133128291Sluigi		 */
134133741Sjmg	struct	knlist if_klist;	/* events attached to this if */
13583130Sjlemon	int	if_pcount;		/* number of promiscuous listeners */
136142901Sglebius	struct	carp_if *if_carp;	/* carp interface structure */
13721259Swollman	struct	bpf_if *if_bpf;		/* packet filter structure */
13821259Swollman	u_short	if_index;		/* numeric abbreviation for this if  */
13921259Swollman	short	if_timer;		/* time 'til if_watchdog called */
140155051Sglebius	struct  ifvlantrunk *if_vlantrunk; /* pointer to 802.1q data */
141102052Ssobomax	int	if_flags;		/* up/down, broadcast, etc. */
142162070Sandre	int	if_capabilities;	/* interface features & capabilities */
143162070Sandre	int	if_capenable;		/* enabled features & capabilities */
14421259Swollman	void	*if_linkmib;		/* link-type-specific MIB data */
14521259Swollman	size_t	if_linkmiblen;		/* length of above data */
14621259Swollman	struct	if_data if_data;
14721404Swollman	struct	ifmultihead if_multiaddrs; /* multicast addresses configured */
14821404Swollman	int	if_amcount;		/* number of all-multicast requests */
14921259Swollman/* procedure handles */
15021259Swollman	int	(*if_output)		/* output routine (enqueue) */
15192725Salfred		(struct ifnet *, struct mbuf *, struct sockaddr *,
15292725Salfred		     struct rtentry *);
153106931Ssam	void	(*if_input)		/* input routine (from h/w driver) */
154106931Ssam		(struct ifnet *, struct mbuf *);
15521259Swollman	void	(*if_start)		/* initiate output routine */
15692725Salfred		(struct ifnet *);
15721259Swollman	int	(*if_ioctl)		/* ioctl routine */
15892725Salfred		(struct ifnet *, u_long, caddr_t);
15921259Swollman	void	(*if_watchdog)		/* timer routine */
16092725Salfred		(struct ifnet *);
16121259Swollman	void	(*if_init)		/* Init routine */
16292725Salfred		(void *);
16321404Swollman	int	(*if_resolvemulti)	/* validate/resolve multicast */
16492725Salfred		(struct ifnet *, struct sockaddr **, struct sockaddr *);
165189230Srwatson	void	(*if_qflush)		/* flush any queues */
166189230Srwatson		(struct ifnet *);
167189230Srwatson	int	(*if_transmit)		/* initiate output routine */
168189230Srwatson		(struct ifnet *, struct mbuf *);
169152315Sru	struct	ifaddr	*if_addr;	/* pointer to link-level address */
170174388Skmacy	void	*if_llsoftc;		/* link layer softc */
171148265Srwatson	int	if_drv_flags;		/* driver-managed status flags */
172130416Smlaier	struct  ifaltq if_snd;		/* output queue (includes altq) */
173123220Simp	const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */
174127828Sluigi
175146986Sthompsa	void	*if_bridge;		/* bridge glue */
176146986Sthompsa
177122524Srwatson	struct	label *if_label;	/* interface MAC label */
178121161Sume
179127828Sluigi	/* these are only used by IPv6 */
180127828Sluigi	struct	ifprefixhead if_prefixhead; /* list of prefixes per if */
181121161Sume	void	*if_afdata[AF_MAX];
182121470Sume	int	if_afdata_initialized;
183186199Skmacy	struct	rwlock if_afdata_lock;
184132712Srwatson	struct	task if_starttask;	/* task for IFF_NEEDSGIANT */
185145320Sglebius	struct	task if_linktask;	/* task for link change events */
186148640Srwatson	struct	mtx if_addr_mtx;	/* mutex to protect address lists */
187186119Sqingli
188152209Sthompsa	LIST_ENTRY(ifnet) if_clones;	/* interfaces of a cloner */
189159781Smlaier	TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */
190159781Smlaier					/* protected by if_addr_mtx */
191159781Smlaier	void	*if_pf_kif;
192168793Sthompsa	void	*if_lagg;		/* lagg glue */
193189230Srwatson
194189230Srwatson	/*
195189230Srwatson	 * Spare fields are added so that we can modify sensitive data
196189230Srwatson	 * structures without changing the kernel binary interface, and must
197189230Srwatson	 * be used with care where binary compatibility is required.
198189230Srwatson	 */
199189230Srwatson	void	*if_pspare[8];
200189230Srwatson	int	if_ispare[4];
20121259Swollman};
20269152Sjlemon
20392725Salfredtypedef void if_init_f_t(void *);
20421259Swollman
205128376Sluigi/*
206128376Sluigi * XXX These aliases are terribly dangerous because they could apply
207128376Sluigi * to anything.
208128376Sluigi */
20921259Swollman#define	if_mtu		if_data.ifi_mtu
21021259Swollman#define	if_type		if_data.ifi_type
21121259Swollman#define if_physical	if_data.ifi_physical
21221259Swollman#define	if_addrlen	if_data.ifi_addrlen
21321259Swollman#define	if_hdrlen	if_data.ifi_hdrlen
21421259Swollman#define	if_metric	if_data.ifi_metric
215128871Sandre#define	if_link_state	if_data.ifi_link_state
21621259Swollman#define	if_baudrate	if_data.ifi_baudrate
21758698Sjlemon#define	if_hwassist	if_data.ifi_hwassist
21821259Swollman#define	if_ipackets	if_data.ifi_ipackets
21921259Swollman#define	if_ierrors	if_data.ifi_ierrors
22021259Swollman#define	if_opackets	if_data.ifi_opackets
22121259Swollman#define	if_oerrors	if_data.ifi_oerrors
22221259Swollman#define	if_collisions	if_data.ifi_collisions
22321259Swollman#define	if_ibytes	if_data.ifi_ibytes
22421259Swollman#define	if_obytes	if_data.ifi_obytes
22521259Swollman#define	if_imcasts	if_data.ifi_imcasts
22621259Swollman#define	if_omcasts	if_data.ifi_omcasts
22721259Swollman#define	if_iqdrops	if_data.ifi_iqdrops
22821259Swollman#define	if_noproto	if_data.ifi_noproto
22921259Swollman#define	if_lastchange	if_data.ifi_lastchange
230136950Sjmg#define if_rawoutput(if, m, sa) if_output(if, m, sa, (struct rtentry *)NULL)
23121259Swollman
23253541Sshin/* for compatibility with other BSDs */
23353541Sshin#define	if_addrlist	if_addrhead
23453541Sshin#define	if_list		if_link
235160981Sbrooks#define	if_name(ifp)	((ifp)->if_xname)
23653541Sshin
23721259Swollman/*
238148640Srwatson * Locks for address lists on the network interface.
239148640Srwatson */
240148640Srwatson#define	IF_ADDR_LOCK_INIT(if)	mtx_init(&(if)->if_addr_mtx,		\
241148640Srwatson				    "if_addr_mtx", NULL, MTX_DEF)
242148640Srwatson#define	IF_ADDR_LOCK_DESTROY(if)	mtx_destroy(&(if)->if_addr_mtx)
243148640Srwatson#define	IF_ADDR_LOCK(if)	mtx_lock(&(if)->if_addr_mtx)
244148640Srwatson#define	IF_ADDR_UNLOCK(if)	mtx_unlock(&(if)->if_addr_mtx)
245148640Srwatson#define	IF_ADDR_LOCK_ASSERT(if)	mtx_assert(&(if)->if_addr_mtx, MA_OWNED)
246148640Srwatson
247148640Srwatson/*
24821259Swollman * Output queues (ifp->if_snd) and slow device input queues (*ifp->if_slowq)
24921259Swollman * are queues of messages stored on ifqueue structures
25021259Swollman * (defined above).  Entries are added to and deleted from these structures
25121259Swollman * by these macros, which should be called with ipl raised to splimp().
25221259Swollman */
25372200Sbmilekic#define IF_LOCK(ifq)		mtx_lock(&(ifq)->ifq_mtx)
25472200Sbmilekic#define IF_UNLOCK(ifq)		mtx_unlock(&(ifq)->ifq_mtx)
255130416Smlaier#define	IF_LOCK_ASSERT(ifq)	mtx_assert(&(ifq)->ifq_mtx, MA_OWNED)
25669152Sjlemon#define	_IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
25769152Sjlemon#define	_IF_DROP(ifq)		((ifq)->ifq_drops++)
25869152Sjlemon#define	_IF_QLEN(ifq)		((ifq)->ifq_len)
25921259Swollman
26069152Sjlemon#define	_IF_ENQUEUE(ifq, m) do { 				\
26169152Sjlemon	(m)->m_nextpkt = NULL;					\
26269152Sjlemon	if ((ifq)->ifq_tail == NULL) 				\
26369152Sjlemon		(ifq)->ifq_head = m; 				\
26469152Sjlemon	else 							\
26569152Sjlemon		(ifq)->ifq_tail->m_nextpkt = m; 		\
26669152Sjlemon	(ifq)->ifq_tail = m; 					\
26769152Sjlemon	(ifq)->ifq_len++; 					\
26869152Sjlemon} while (0)
26969152Sjlemon
27069152Sjlemon#define IF_ENQUEUE(ifq, m) do {					\
27169152Sjlemon	IF_LOCK(ifq); 						\
27269152Sjlemon	_IF_ENQUEUE(ifq, m); 					\
27369152Sjlemon	IF_UNLOCK(ifq); 					\
27469152Sjlemon} while (0)
27569152Sjlemon
27669152Sjlemon#define	_IF_PREPEND(ifq, m) do {				\
27769152Sjlemon	(m)->m_nextpkt = (ifq)->ifq_head; 			\
27869152Sjlemon	if ((ifq)->ifq_tail == NULL) 				\
27969152Sjlemon		(ifq)->ifq_tail = (m); 				\
28069152Sjlemon	(ifq)->ifq_head = (m); 					\
28169152Sjlemon	(ifq)->ifq_len++; 					\
28269152Sjlemon} while (0)
28369152Sjlemon
28469152Sjlemon#define IF_PREPEND(ifq, m) do {		 			\
28569152Sjlemon	IF_LOCK(ifq); 						\
28669152Sjlemon	_IF_PREPEND(ifq, m); 					\
28769152Sjlemon	IF_UNLOCK(ifq); 					\
28869152Sjlemon} while (0)
28969152Sjlemon
29069152Sjlemon#define	_IF_DEQUEUE(ifq, m) do { 				\
29169152Sjlemon	(m) = (ifq)->ifq_head; 					\
29269152Sjlemon	if (m) { 						\
293136950Sjmg		if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL)	\
29469152Sjlemon			(ifq)->ifq_tail = NULL; 		\
29569152Sjlemon		(m)->m_nextpkt = NULL; 				\
29669152Sjlemon		(ifq)->ifq_len--; 				\
29769152Sjlemon	} 							\
29869152Sjlemon} while (0)
29969152Sjlemon
30069152Sjlemon#define IF_DEQUEUE(ifq, m) do { 				\
30169152Sjlemon	IF_LOCK(ifq); 						\
30269152Sjlemon	_IF_DEQUEUE(ifq, m); 					\
30369152Sjlemon	IF_UNLOCK(ifq); 					\
30469152Sjlemon} while (0)
30569152Sjlemon
306130416Smlaier#define	_IF_POLL(ifq, m)	((m) = (ifq)->ifq_head)
307130416Smlaier#define	IF_POLL(ifq, m)		_IF_POLL(ifq, m)
308130416Smlaier
309130416Smlaier#define _IF_DRAIN(ifq) do { 					\
31069152Sjlemon	struct mbuf *m; 					\
31169152Sjlemon	for (;;) { 						\
31269152Sjlemon		_IF_DEQUEUE(ifq, m); 				\
31369152Sjlemon		if (m == NULL) 					\
31469152Sjlemon			break; 					\
31569152Sjlemon		m_freem(m); 					\
31669152Sjlemon	} 							\
31769152Sjlemon} while (0)
31869152Sjlemon
319130416Smlaier#define IF_DRAIN(ifq) do {					\
320130416Smlaier	IF_LOCK(ifq);						\
321130416Smlaier	_IF_DRAIN(ifq);						\
322130416Smlaier	IF_UNLOCK(ifq);						\
323130416Smlaier} while(0)
324130416Smlaier
32555205Speter#ifdef _KERNEL
326126264Smlaier/* interface address change event */
327126264Smlaiertypedef void (*ifaddr_event_handler_t)(void *, struct ifnet *);
328126264SmlaierEVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
329126264Smlaier/* new interface arrival event */
330126264Smlaiertypedef void (*ifnet_arrival_event_handler_t)(void *, struct ifnet *);
331126264SmlaierEVENTHANDLER_DECLARE(ifnet_arrival_event, ifnet_arrival_event_handler_t);
332126264Smlaier/* interface departure event */
333126264Smlaiertypedef void (*ifnet_departure_event_handler_t)(void *, struct ifnet *);
334126264SmlaierEVENTHANDLER_DECLARE(ifnet_departure_event, ifnet_departure_event_handler_t);
335126264Smlaier
336159781Smlaier/*
337159781Smlaier * interface groups
338159781Smlaier */
339159781Smlaierstruct ifg_group {
340159781Smlaier	char				 ifg_group[IFNAMSIZ];
341159781Smlaier	u_int				 ifg_refcnt;
342159781Smlaier	void				*ifg_pf_kif;
343159781Smlaier	TAILQ_HEAD(, ifg_member)	 ifg_members;
344159781Smlaier	TAILQ_ENTRY(ifg_group)		 ifg_next;
345159781Smlaier};
346159781Smlaier
347159781Smlaierstruct ifg_member {
348159781Smlaier	TAILQ_ENTRY(ifg_member)	 ifgm_next;
349159781Smlaier	struct ifnet		*ifgm_ifp;
350159781Smlaier};
351159781Smlaier
352159781Smlaierstruct ifg_list {
353159781Smlaier	struct ifg_group	*ifgl_group;
354159781Smlaier	TAILQ_ENTRY(ifg_list)	 ifgl_next;
355159781Smlaier};
356159781Smlaier
357159781Smlaier/* group attach event */
358159781Smlaiertypedef void (*group_attach_event_handler_t)(void *, struct ifg_group *);
359159781SmlaierEVENTHANDLER_DECLARE(group_attach_event, group_attach_event_handler_t);
360159781Smlaier/* group detach event */
361159781Smlaiertypedef void (*group_detach_event_handler_t)(void *, struct ifg_group *);
362159781SmlaierEVENTHANDLER_DECLARE(group_detach_event, group_detach_event_handler_t);
363159781Smlaier/* group change event */
364159781Smlaiertypedef void (*group_change_event_handler_t)(void *, const char *);
365159781SmlaierEVENTHANDLER_DECLARE(group_change_event, group_change_event_handler_t);
366159781Smlaier
367121470Sume#define	IF_AFDATA_LOCK_INIT(ifp)	\
368186199Skmacy	rw_init(&(ifp)->if_afdata_lock, "if_afdata")
369121470Sume
370186199Skmacy#define	IF_AFDATA_WLOCK(ifp)	rw_wlock(&(ifp)->if_afdata_lock)
371186199Skmacy#define	IF_AFDATA_RLOCK(ifp)	rw_rlock(&(ifp)->if_afdata_lock)
372186199Skmacy#define	IF_AFDATA_WUNLOCK(ifp)	rw_wunlock(&(ifp)->if_afdata_lock)
373186199Skmacy#define	IF_AFDATA_RUNLOCK(ifp)	rw_runlock(&(ifp)->if_afdata_lock)
374186199Skmacy#define	IF_AFDATA_LOCK(ifp)	IF_AFDATA_WLOCK(ifp)
375186199Skmacy#define	IF_AFDATA_UNLOCK(ifp)	IF_AFDATA_WUNLOCK(ifp)
376186199Skmacy#define	IF_AFDATA_TRYLOCK(ifp)	rw_try_wlock(&(ifp)->if_afdata_lock)
377186199Skmacy#define	IF_AFDATA_DESTROY(ifp)	rw_destroy(&(ifp)->if_afdata_lock)
378186119Sqingli
379186199Skmacy#define	IF_AFDATA_LOCK_ASSERT(ifp)	rw_assert(&(ifp)->if_afdata_lock, RA_LOCKED)
380186199Skmacy#define	IF_AFDATA_UNLOCK_ASSERT(ifp)	rw_assert(&(ifp)->if_afdata_lock, RA_UNLOCKED)
381186199Skmacy
382136704Srwatson#define	IFF_LOCKGIANT(ifp) do {						\
383136704Srwatson	if ((ifp)->if_flags & IFF_NEEDSGIANT)				\
384136704Srwatson		mtx_lock(&Giant);					\
385136704Srwatson} while (0)
386136704Srwatson
387136704Srwatson#define	IFF_UNLOCKGIANT(ifp) do {					\
388136704Srwatson	if ((ifp)->if_flags & IFF_NEEDSGIANT)				\
389136704Srwatson		mtx_unlock(&Giant);					\
390136704Srwatson} while (0)
391136704Srwatson
392137065Srwatsonint	if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp,
393137065Srwatson	    int adjust);
394130416Smlaier#define	IF_HANDOFF(ifq, m, ifp)			\
395130416Smlaier	if_handoff((struct ifqueue *)ifq, m, ifp, 0)
396130416Smlaier#define	IF_HANDOFF_ADJ(ifq, m, ifp, adj)	\
397130416Smlaier	if_handoff((struct ifqueue *)ifq, m, ifp, adj)
39821259Swollman
399132712Srwatsonvoid	if_start(struct ifnet *);
400132712Srwatson
401130416Smlaier#define	IFQ_ENQUEUE(ifq, m, err)					\
402130416Smlaierdo {									\
403130416Smlaier	IF_LOCK(ifq);							\
404130416Smlaier	if (ALTQ_IS_ENABLED(ifq))					\
405130416Smlaier		ALTQ_ENQUEUE(ifq, m, NULL, err);			\
406130416Smlaier	else {								\
407130416Smlaier		if (_IF_QFULL(ifq)) {					\
408130416Smlaier			m_freem(m);					\
409130416Smlaier			(err) = ENOBUFS;				\
410130416Smlaier		} else {						\
411130416Smlaier			_IF_ENQUEUE(ifq, m);				\
412130416Smlaier			(err) = 0;					\
413130416Smlaier		}							\
414130416Smlaier	}								\
415130416Smlaier	if (err)							\
416130416Smlaier		(ifq)->ifq_drops++;					\
417130416Smlaier	IF_UNLOCK(ifq);							\
418130416Smlaier} while (0)
41921259Swollman
420130416Smlaier#define	IFQ_DEQUEUE_NOLOCK(ifq, m)					\
421130416Smlaierdo {									\
422130416Smlaier	if (TBR_IS_ENABLED(ifq))					\
423130508Smlaier		(m) = tbr_dequeue_ptr(ifq, ALTDQ_REMOVE);		\
424130416Smlaier	else if (ALTQ_IS_ENABLED(ifq))					\
425130416Smlaier		ALTQ_DEQUEUE(ifq, m);					\
426130416Smlaier	else								\
427130416Smlaier		_IF_DEQUEUE(ifq, m);					\
428130416Smlaier} while (0)
429130416Smlaier
430130416Smlaier#define	IFQ_DEQUEUE(ifq, m)						\
431130416Smlaierdo {									\
432130416Smlaier	IF_LOCK(ifq);							\
433130416Smlaier	IFQ_DEQUEUE_NOLOCK(ifq, m);					\
434130416Smlaier	IF_UNLOCK(ifq);							\
435130416Smlaier} while (0)
436130416Smlaier
437130416Smlaier#define	IFQ_POLL_NOLOCK(ifq, m)						\
438130416Smlaierdo {									\
439130416Smlaier	if (TBR_IS_ENABLED(ifq))					\
440130508Smlaier		(m) = tbr_dequeue_ptr(ifq, ALTDQ_POLL);			\
441130416Smlaier	else if (ALTQ_IS_ENABLED(ifq))					\
442130416Smlaier		ALTQ_POLL(ifq, m);					\
443130416Smlaier	else								\
444130416Smlaier		_IF_POLL(ifq, m);					\
445130416Smlaier} while (0)
446130416Smlaier
447130416Smlaier#define	IFQ_POLL(ifq, m)						\
448130416Smlaierdo {									\
449130416Smlaier	IF_LOCK(ifq);							\
450130416Smlaier	IFQ_POLL_NOLOCK(ifq, m);					\
451130416Smlaier	IF_UNLOCK(ifq);							\
452130416Smlaier} while (0)
453130416Smlaier
454130416Smlaier#define	IFQ_PURGE_NOLOCK(ifq)						\
455130416Smlaierdo {									\
456130416Smlaier	if (ALTQ_IS_ENABLED(ifq)) {					\
457130416Smlaier		ALTQ_PURGE(ifq);					\
458130416Smlaier	} else								\
459130416Smlaier		_IF_DRAIN(ifq);						\
460130416Smlaier} while (0)
461130416Smlaier
462130416Smlaier#define	IFQ_PURGE(ifq)							\
463130416Smlaierdo {									\
464130416Smlaier	IF_LOCK(ifq);							\
465130416Smlaier	IFQ_PURGE_NOLOCK(ifq);						\
466130416Smlaier	IF_UNLOCK(ifq);							\
467130416Smlaier} while (0)
468130416Smlaier
469130416Smlaier#define	IFQ_SET_READY(ifq)						\
470130416Smlaier	do { ((ifq)->altq_flags |= ALTQF_READY); } while (0)
471130416Smlaier
472130416Smlaier#define	IFQ_LOCK(ifq)			IF_LOCK(ifq)
473130416Smlaier#define	IFQ_UNLOCK(ifq)			IF_UNLOCK(ifq)
474130416Smlaier#define	IFQ_LOCK_ASSERT(ifq)		IF_LOCK_ASSERT(ifq)
475130416Smlaier#define	IFQ_IS_EMPTY(ifq)		((ifq)->ifq_len == 0)
476130416Smlaier#define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
477130416Smlaier#define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
478130416Smlaier#define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
479130416Smlaier#define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
480130416Smlaier
481148886Srwatson/*
482148886Srwatson * The IFF_DRV_OACTIVE test should really occur in the device driver, not in
483148886Srwatson * the handoff logic, as that flag is locked by the device driver.
484148886Srwatson */
485130416Smlaier#define	IFQ_HANDOFF_ADJ(ifp, m, adj, err)				\
486130416Smlaierdo {									\
487130416Smlaier	int len;							\
488130416Smlaier	short mflags;							\
489130416Smlaier									\
490130416Smlaier	len = (m)->m_pkthdr.len;					\
491130416Smlaier	mflags = (m)->m_flags;						\
492130416Smlaier	IFQ_ENQUEUE(&(ifp)->if_snd, m, err);				\
493130416Smlaier	if ((err) == 0) {						\
494130416Smlaier		(ifp)->if_obytes += len + (adj);			\
495130416Smlaier		if (mflags & M_MCAST)					\
496130416Smlaier			(ifp)->if_omcasts++;				\
497148886Srwatson		if (((ifp)->if_drv_flags & IFF_DRV_OACTIVE) == 0)	\
498132712Srwatson			if_start(ifp);					\
499130416Smlaier	}								\
500130416Smlaier} while (0)
501130416Smlaier
502130416Smlaier#define	IFQ_HANDOFF(ifp, m, err)					\
503130512Smlaier	IFQ_HANDOFF_ADJ(ifp, m, 0, err)
504130416Smlaier
505130416Smlaier#define	IFQ_DRV_DEQUEUE(ifq, m)						\
506130416Smlaierdo {									\
507130416Smlaier	(m) = (ifq)->ifq_drv_head;					\
508130416Smlaier	if (m) {							\
509130416Smlaier		if (((ifq)->ifq_drv_head = (m)->m_nextpkt) == NULL)	\
510130416Smlaier			(ifq)->ifq_drv_tail = NULL;			\
511130416Smlaier		(m)->m_nextpkt = NULL;					\
512130416Smlaier		(ifq)->ifq_drv_len--;					\
513130416Smlaier	} else {							\
514130416Smlaier		IFQ_LOCK(ifq);						\
515130416Smlaier		IFQ_DEQUEUE_NOLOCK(ifq, m);				\
516130416Smlaier		while ((ifq)->ifq_drv_len < (ifq)->ifq_drv_maxlen) {	\
517130416Smlaier			struct mbuf *m0;				\
518130416Smlaier			IFQ_DEQUEUE_NOLOCK(ifq, m0);			\
519130416Smlaier			if (m0 == NULL)					\
520130416Smlaier				break;					\
521130416Smlaier			m0->m_nextpkt = NULL;				\
522130416Smlaier			if ((ifq)->ifq_drv_tail == NULL)		\
523130416Smlaier				(ifq)->ifq_drv_head = m0;		\
524130416Smlaier			else						\
525130416Smlaier				(ifq)->ifq_drv_tail->m_nextpkt = m0;	\
526130416Smlaier			(ifq)->ifq_drv_tail = m0;			\
527130416Smlaier			(ifq)->ifq_drv_len++;				\
528130416Smlaier		}							\
529130416Smlaier		IFQ_UNLOCK(ifq);					\
530130416Smlaier	}								\
531130416Smlaier} while (0)
532130416Smlaier
533130416Smlaier#define	IFQ_DRV_PREPEND(ifq, m)						\
534130416Smlaierdo {									\
535130416Smlaier	(m)->m_nextpkt = (ifq)->ifq_drv_head;				\
536132152Smlaier	if ((ifq)->ifq_drv_tail == NULL)				\
537132152Smlaier		(ifq)->ifq_drv_tail = (m);				\
538130416Smlaier	(ifq)->ifq_drv_head = (m);					\
539130416Smlaier	(ifq)->ifq_drv_len++;						\
540130416Smlaier} while (0)
541130416Smlaier
542130416Smlaier#define	IFQ_DRV_IS_EMPTY(ifq)						\
543130416Smlaier	(((ifq)->ifq_drv_len == 0) && ((ifq)->ifq_len == 0))
544130416Smlaier
545130416Smlaier#define	IFQ_DRV_PURGE(ifq)						\
546130416Smlaierdo {									\
547132152Smlaier	struct mbuf *m, *n = (ifq)->ifq_drv_head;			\
548132152Smlaier	while((m = n) != NULL) {					\
549132152Smlaier		n = m->m_nextpkt;					\
550130416Smlaier		m_freem(m);						\
551130416Smlaier	}								\
552130416Smlaier	(ifq)->ifq_drv_head = (ifq)->ifq_drv_tail = NULL;		\
553130416Smlaier	(ifq)->ifq_drv_len = 0;						\
554130416Smlaier	IFQ_PURGE(ifq);							\
555130416Smlaier} while (0)
556130416Smlaier
557186207Skmacy#ifdef _KERNEL
558186213Skmacystatic __inline void
559186213Skmacydrbr_stats_update(struct ifnet *ifp, int len, int mflags)
560186213Skmacy{
561186213Skmacy
562186213Skmacy	ifp->if_obytes += len;
563186213Skmacy	if (mflags & M_MCAST)
564186213Skmacy		ifp->if_omcasts++;
565186213Skmacy}
566186213Skmacy
567186207Skmacystatic __inline int
568186213Skmacydrbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
569186207Skmacy{
570186207Skmacy	int error = 0;
571186213Skmacy	int len = m->m_pkthdr.len;
572186213Skmacy	int mflags = m->m_flags;
573186207Skmacy
574186207Skmacy	if ((error = buf_ring_enqueue(br, m)) == ENOBUFS) {
575186207Skmacy		br->br_drops++;
576186213Skmacy		_IF_DROP(&ifp->if_snd);
577186207Skmacy		m_freem(m);
578186213Skmacy	} else
579186213Skmacy		drbr_stats_update(ifp, len, mflags);
580186213Skmacy
581186207Skmacy	return (error);
582186207Skmacy}
583186207Skmacy
584186207Skmacystatic __inline void
585186207Skmacydrbr_free(struct buf_ring *br, struct malloc_type *type)
586186207Skmacy{
587186207Skmacy	struct mbuf *m;
588186207Skmacy
589186207Skmacy	while ((m = buf_ring_dequeue_sc(br)) != NULL)
590186207Skmacy		m_freem(m);
591186207Skmacy
592186207Skmacy	buf_ring_free(br, type);
593186207Skmacy}
594186207Skmacy#endif
595186207Skmacy
59649459Sbrian/*
59749459Sbrian * 72 was chosen below because it is the size of a TCP/IP
59849459Sbrian * header (40) + the minimum mss (32).
59949459Sbrian */
60049459Sbrian#define	IF_MINMTU	72
60149459Sbrian#define	IF_MAXMTU	65535
60249459Sbrian
60355205Speter#endif /* _KERNEL */
60421259Swollman
60521259Swollman/*
60621259Swollman * The ifaddr structure contains information about one address
60721259Swollman * of an interface.  They are maintained by the different address families,
60821259Swollman * are allocated and attached when an address is set, and are linked
60921259Swollman * together so all addresses for an interface can be located.
610128291Sluigi *
611128291Sluigi * NOTE: a 'struct ifaddr' is always at the beginning of a larger
612128291Sluigi * chunk of malloc'ed memory, where we store the three addresses
613128291Sluigi * (ifa_addr, ifa_dstaddr and ifa_netmask) referenced here.
61421259Swollman */
61521259Swollmanstruct ifaddr {
61621259Swollman	struct	sockaddr *ifa_addr;	/* address of interface */
61721259Swollman	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
61821259Swollman#define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
61921259Swollman	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
62067334Sjoe	struct	if_data if_data;	/* not all members are meaningful */
62121259Swollman	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
62260938Sjake	TAILQ_ENTRY(ifaddr) ifa_link;	/* queue macro glue */
62321259Swollman	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
62492725Salfred		(int, struct rtentry *, struct rt_addrinfo *);
62521259Swollman	u_short	ifa_flags;		/* mostly rt_flags for cloning */
62647254Spb	u_int	ifa_refcnt;		/* references to this structure */
62721259Swollman	int	ifa_metric;		/* cost of going out this interface */
62828845Sjulian	int (*ifa_claim_addr)		/* check if an addr goes to this if */
62992725Salfred		(struct ifaddr *, struct sockaddr *);
630108033Shsu	struct mtx ifa_mtx;
63121259Swollman};
63221259Swollman#define	IFA_ROUTE	RTF_UP		/* route installed */
63321259Swollman
63453541Sshin/* for compatibility with other BSDs */
63553541Sshin#define	ifa_list	ifa_link
63653541Sshin
637108033Shsu#define	IFA_LOCK_INIT(ifa)	\
638108033Shsu    mtx_init(&(ifa)->ifa_mtx, "ifaddr", NULL, MTX_DEF)
639108033Shsu#define	IFA_LOCK(ifa)		mtx_lock(&(ifa)->ifa_mtx)
640108033Shsu#define	IFA_UNLOCK(ifa)		mtx_unlock(&(ifa)->ifa_mtx)
641108033Shsu#define	IFA_DESTROY(ifa)	mtx_destroy(&(ifa)->ifa_mtx)
642108033Shsu
64321404Swollman/*
64452904Sshin * The prefix structure contains information about one prefix
64552904Sshin * of an interface.  They are maintained by the different address families,
646108470Sschweikh * are allocated and attached when a prefix or an address is set,
64753541Sshin * and are linked together so all prefixes for an interface can be located.
64852904Sshin */
64952904Sshinstruct ifprefix {
65052904Sshin	struct	sockaddr *ifpr_prefix;	/* prefix of interface */
65152904Sshin	struct	ifnet *ifpr_ifp;	/* back-pointer to interface */
65260938Sjake	TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */
65352904Sshin	u_char	ifpr_plen;		/* prefix length in bits */
65452904Sshin	u_char	ifpr_type;		/* protocol dependent prefix type */
65552904Sshin};
65652904Sshin
65752904Sshin/*
65821404Swollman * Multicast address structure.  This is analogous to the ifaddr
65921404Swollman * structure except that it keeps track of multicast addresses.
66021404Swollman */
66121404Swollmanstruct ifmultiaddr {
66272084Sphk	TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
66321434Swollman	struct	sockaddr *ifma_addr; 	/* address this membership is for */
66421434Swollman	struct	sockaddr *ifma_lladdr;	/* link-layer translation, if any */
66521434Swollman	struct	ifnet *ifma_ifp;	/* back-pointer to interface */
66621434Swollman	u_int	ifma_refcount;		/* reference count */
66721434Swollman	void	*ifma_protospec;	/* protocol-specific state, if any */
668167729Sbms	struct	ifmultiaddr *ifma_llifma; /* pointer to ifma for ifma_lladdr */
66921404Swollman};
67021404Swollman
67155205Speter#ifdef _KERNEL
672108036Shsu#define	IFAFREE(ifa)					\
673108036Shsu	do {						\
674108036Shsu		IFA_LOCK(ifa);				\
675108036Shsu		KASSERT((ifa)->ifa_refcnt > 0,		\
676108036Shsu		    ("ifa %p !(ifa_refcnt > 0)", ifa));	\
677108036Shsu		if (--(ifa)->ifa_refcnt == 0) {		\
678108036Shsu			IFA_DESTROY(ifa);		\
679108036Shsu			free(ifa, M_IFADDR);		\
680108036Shsu		} else 					\
681108036Shsu			IFA_UNLOCK(ifa);		\
68246568Speter	} while (0)
68321259Swollman
684108036Shsu#define IFAREF(ifa)					\
685108036Shsu	do {						\
686108036Shsu		IFA_LOCK(ifa);				\
687108036Shsu		++(ifa)->ifa_refcnt;			\
688108036Shsu		IFA_UNLOCK(ifa);			\
689108033Shsu	} while (0)
690108033Shsu
691186199Skmacyextern	struct rwlock ifnet_lock;
692108298Shsu#define	IFNET_LOCK_INIT() \
693186199Skmacy   rw_init_flags(&ifnet_lock, "ifnet",  RW_RECURSE)
694186199Skmacy#define	IFNET_WLOCK()		rw_wlock(&ifnet_lock)
695186199Skmacy#define	IFNET_WUNLOCK()		rw_wunlock(&ifnet_lock)
696186199Skmacy#define	IFNET_WLOCK_ASSERT()	rw_assert(&ifnet_lock, RA_LOCKED)
697186199Skmacy#define	IFNET_RLOCK()		rw_rlock(&ifnet_lock)
698186199Skmacy#define	IFNET_RUNLOCK()		rw_runlock(&ifnet_lock)
699108172Shsu
70083130Sjlemonstruct ifindex_entry {
70187914Sjlemon	struct	ifnet *ife_ifnet;
702130585Sphk	struct cdev *ife_dev;
70383130Sjlemon};
70483130Sjlemon
705180042Srwatsonstruct ifnet	*ifnet_byindex(u_short idx);
706181892Skmacy
707128315Sluigi/*
708128315Sluigi * Given the index, ifaddr_byindex() returns the one and only
709128315Sluigi * link-level ifaddr for the interface. You are not supposed to use
710128315Sluigi * it to traverse the list of addresses associated to the interface.
711128315Sluigi */
712180042Srwatsonstruct ifaddr	*ifaddr_byindex(u_short idx);
713180042Srwatsonstruct cdev	*ifdev_byindex(u_short idx);
71483130Sjlemon
715186048Sbz#ifdef VIMAGE_GLOBALS
71621259Swollmanextern	struct ifnethead ifnet;
71771791Speterextern	struct ifnet *loif;	/* first loopback interface */
71821259Swollmanextern	int if_index;
719186048Sbz#endif
720186048Sbzextern	int ifqmaxlen;
72121259Swollman
722159781Smlaierint	if_addgroup(struct ifnet *, const char *);
723159781Smlaierint	if_delgroup(struct ifnet *, const char *);
72492725Salfredint	if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
72592725Salfredint	if_allmulti(struct ifnet *, int);
726147256Sbrooksstruct	ifnet* if_alloc(u_char);
72792725Salfredvoid	if_attach(struct ifnet *);
72892725Salfredint	if_delmulti(struct ifnet *, struct sockaddr *);
729167729Sbmsvoid	if_delmulti_ifma(struct ifmultiaddr *);
73092725Salfredvoid	if_detach(struct ifnet *);
731146620Speadarvoid	if_purgeaddrs(struct ifnet *);
732177617Ssamvoid	if_purgemaddrs(struct ifnet *);
73392725Salfredvoid	if_down(struct ifnet *);
734167732Sbmsstruct ifmultiaddr *
735167732Sbms	if_findmulti(struct ifnet *, struct sockaddr *);
736147256Sbrooksvoid	if_free(struct ifnet *);
737147256Sbrooksvoid	if_free_type(struct ifnet *, u_char);
738121816Sbrooksvoid	if_initname(struct ifnet *, const char *, int);
739138542Ssamvoid	if_link_state_change(struct ifnet *, int);
740103900Sbrooksint	if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
74192725Salfredint	if_setlladdr(struct ifnet *, const u_char *, int);
74292725Salfredvoid	if_up(struct ifnet *);
74392725Salfred/*void	ifinit(void);*/ /* declared in systm.h for main() */
74492725Salfredint	ifioctl(struct socket *, u_long, caddr_t, struct thread *);
74592725Salfredint	ifpromisc(struct ifnet *, int);
74692725Salfredstruct	ifnet *ifunit(const char *);
74721259Swollman
748185162Skmacyvoid	ifq_attach(struct ifaltq *, struct ifnet *ifp);
749185162Skmacyvoid	ifq_detach(struct ifaltq *);
750185162Skmacy
75192725Salfredstruct	ifaddr *ifa_ifwithaddr(struct sockaddr *);
752162068Sandrestruct	ifaddr *ifa_ifwithbroadaddr(struct sockaddr *);
75392725Salfredstruct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
75492725Salfredstruct	ifaddr *ifa_ifwithnet(struct sockaddr *);
75592725Salfredstruct	ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *);
756178888Sjulianstruct	ifaddr *ifa_ifwithroute_fib(int, struct sockaddr *, struct sockaddr *, u_int);
757178888Sjulian
75892725Salfredstruct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
75921259Swollman
76092725Salfredint	if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);
76121434Swollman
762147256Sbrookstypedef	void *if_com_alloc_t(u_char type, struct ifnet *ifp);
763147256Sbrookstypedef	void if_com_free_t(void *com, u_char type);
764147256Sbrooksvoid	if_register_com_alloc(u_char type, if_com_alloc_t *a, if_com_free_t *f);
765147256Sbrooksvoid	if_deregister_com_alloc(u_char type);
766147256Sbrooks
76784931Sfjoe#define IF_LLADDR(ifp)							\
768152315Sru    LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr))
76984931Sfjoe
77087902Sluigi#ifdef DEVICE_POLLING
771150789Sglebiusenum poll_cmd {	POLL_ONLY, POLL_AND_CHECK_STATUS };
77287902Sluigi
77392725Salfredtypedef	void poll_handler_t(struct ifnet *ifp, enum poll_cmd cmd, int count);
77492725Salfredint    ether_poll_register(poll_handler_t *h, struct ifnet *ifp);
77592725Salfredint    ether_poll_deregister(struct ifnet *ifp);
77687902Sluigi#endif /* DEVICE_POLLING */
77787902Sluigi
77855205Speter#endif /* _KERNEL */
77921259Swollman
78021259Swollman#endif /* !_NET_IF_VAR_H_ */
781