ifq.h revision 147256
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 147256 2005-06-10 16:49:24Z brooks $
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;
7221259Swollman#endif
7321259Swollman
7421259Swollman#include <sys/queue.h>		/* get TAILQ macros */
7521259Swollman
7669224Sjlemon#ifdef _KERNEL
7769152Sjlemon#include <sys/mbuf.h>
78126264Smlaier#include <sys/eventhandler.h>
7969224Sjlemon#endif /* _KERNEL */
8074914Sjhb#include <sys/lock.h>		/* XXX */
8174914Sjhb#include <sys/mutex.h>		/* XXX */
8283130Sjlemon#include <sys/event.h>		/* XXX */
83132712Srwatson#include <sys/_task.h>
8469152Sjlemon
85121816Sbrooks#define	IF_DUNIT_NONE	-1
86121816Sbrooks
87130416Smlaier#include <altq/if_altq.h>
88130416Smlaier
8960938SjakeTAILQ_HEAD(ifnethead, ifnet);	/* we use TAILQs so that the order of */
9060938SjakeTAILQ_HEAD(ifaddrhead, ifaddr);	/* instantiation is preserved in the list */
9160938SjakeTAILQ_HEAD(ifprefixhead, ifprefix);
9272084SphkTAILQ_HEAD(ifmultihead, ifmultiaddr);
9321259Swollman
9421259Swollman/*
9521259Swollman * Structure defining a queue for a network interface.
9621259Swollman */
9721259Swollmanstruct	ifqueue {
9821259Swollman	struct	mbuf *ifq_head;
9921259Swollman	struct	mbuf *ifq_tail;
10021259Swollman	int	ifq_len;
10121259Swollman	int	ifq_maxlen;
10221259Swollman	int	ifq_drops;
10369152Sjlemon	struct	mtx ifq_mtx;
10421259Swollman};
10521259Swollman
10621259Swollman/*
10721259Swollman * Structure defining a network interface.
10821259Swollman *
10921259Swollman * (Would like to call this struct ``if'', but C isn't PL/1.)
11021259Swollman */
11184380Smjacob
11221259Swollmanstruct ifnet {
11321259Swollman	void	*if_softc;		/* pointer to driver state */
114147256Sbrooks	void	*if_l2com;		/* pointer to protocol bits */
11560938Sjake	TAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained */
116121816Sbrooks	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
117121816Sbrooks	const char *if_dname;		/* driver name */
118121816Sbrooks	int	if_dunit;		/* unit or IF_DUNIT_NONE */
11921259Swollman	struct	ifaddrhead if_addrhead;	/* linked list of addresses per if */
120128291Sluigi		/*
121128291Sluigi		 * if_addrhead is the list of all addresses associated to
122128315Sluigi		 * an interface.
123128315Sluigi		 * Some code in the kernel assumes that first element
124128315Sluigi		 * of the list has type AF_LINK, and contains sockaddr_dl
125128315Sluigi		 * addresses which store the link-level address and the name
126128291Sluigi		 * of the interface.
127128315Sluigi		 * However, access to the AF_LINK address through this
128128315Sluigi		 * field is deprecated. Use ifaddr_byindex() instead.
129128291Sluigi		 */
130133741Sjmg	struct	knlist if_klist;	/* events attached to this if */
13183130Sjlemon	int	if_pcount;		/* number of promiscuous listeners */
132142901Sglebius	struct	carp_if *if_carp;	/* carp interface structure */
13321259Swollman	struct	bpf_if *if_bpf;		/* packet filter structure */
13421259Swollman	u_short	if_index;		/* numeric abbreviation for this if  */
13521259Swollman	short	if_timer;		/* time 'til if_watchdog called */
136106931Ssam	u_short	if_nvlans;		/* number of active vlans */
137102052Ssobomax	int	if_flags;		/* up/down, broadcast, etc. */
13883624Sjlemon	int	if_capabilities;	/* interface capabilities */
13983624Sjlemon	int	if_capenable;		/* enabled features */
14021259Swollman	void	*if_linkmib;		/* link-type-specific MIB data */
14121259Swollman	size_t	if_linkmiblen;		/* length of above data */
14221259Swollman	struct	if_data if_data;
14321404Swollman	struct	ifmultihead if_multiaddrs; /* multicast addresses configured */
14421404Swollman	int	if_amcount;		/* number of all-multicast requests */
14521259Swollman/* procedure handles */
14621259Swollman	int	(*if_output)		/* output routine (enqueue) */
14792725Salfred		(struct ifnet *, struct mbuf *, struct sockaddr *,
14892725Salfred		     struct rtentry *);
149106931Ssam	void	(*if_input)		/* input routine (from h/w driver) */
150106931Ssam		(struct ifnet *, struct mbuf *);
15121259Swollman	void	(*if_start)		/* initiate output routine */
15292725Salfred		(struct ifnet *);
15321259Swollman	int	(*if_ioctl)		/* ioctl routine */
15492725Salfred		(struct ifnet *, u_long, caddr_t);
15521259Swollman	void	(*if_watchdog)		/* timer routine */
15692725Salfred		(struct ifnet *);
15721259Swollman	void	(*if_init)		/* Init routine */
15892725Salfred		(void *);
15921404Swollman	int	(*if_resolvemulti)	/* validate/resolve multicast */
16092725Salfred		(struct ifnet *, struct sockaddr **, struct sockaddr *);
161137062Srwatson	void	*if_spare1;		/* spare pointer 1 */
162137062Srwatson	void	*if_spare2;		/* spare pointer 2 */
163137062Srwatson	void	*if_spare3;		/* spare pointer 3 */
164137062Srwatson	u_int	if_spare_flags1;	/* spare flags 1 */
165137062Srwatson	u_int	if_spare_flags2;	/* spare flags 2 */
166130416Smlaier	struct  ifaltq if_snd;		/* output queue (includes altq) */
167123220Simp	const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */
168127828Sluigi
169146986Sthompsa	void	*if_bridge;		/* bridge glue */
170146986Sthompsa
171127828Sluigi	struct	lltable *lltables;	/* list of L3-L2 resolution tables */
172127828Sluigi
173122524Srwatson	struct	label *if_label;	/* interface MAC label */
174121161Sume
175127828Sluigi	/* these are only used by IPv6 */
176127828Sluigi	struct	ifprefixhead if_prefixhead; /* list of prefixes per if */
177121161Sume	void	*if_afdata[AF_MAX];
178121470Sume	int	if_afdata_initialized;
179121470Sume	struct	mtx if_afdata_mtx;
180132712Srwatson	struct	task if_starttask;	/* task for IFF_NEEDSGIANT */
181145320Sglebius	struct	task if_linktask;	/* task for link change events */
18221259Swollman};
18369152Sjlemon
18492725Salfredtypedef void if_init_f_t(void *);
18521259Swollman
186128376Sluigi/*
187128376Sluigi * XXX These aliases are terribly dangerous because they could apply
188128376Sluigi * to anything.
189128376Sluigi */
19021259Swollman#define	if_mtu		if_data.ifi_mtu
19121259Swollman#define	if_type		if_data.ifi_type
19221259Swollman#define if_physical	if_data.ifi_physical
19321259Swollman#define	if_addrlen	if_data.ifi_addrlen
19421259Swollman#define	if_hdrlen	if_data.ifi_hdrlen
19521259Swollman#define	if_metric	if_data.ifi_metric
196128871Sandre#define	if_link_state	if_data.ifi_link_state
19721259Swollman#define	if_baudrate	if_data.ifi_baudrate
19858698Sjlemon#define	if_hwassist	if_data.ifi_hwassist
19921259Swollman#define	if_ipackets	if_data.ifi_ipackets
20021259Swollman#define	if_ierrors	if_data.ifi_ierrors
20121259Swollman#define	if_opackets	if_data.ifi_opackets
20221259Swollman#define	if_oerrors	if_data.ifi_oerrors
20321259Swollman#define	if_collisions	if_data.ifi_collisions
20421259Swollman#define	if_ibytes	if_data.ifi_ibytes
20521259Swollman#define	if_obytes	if_data.ifi_obytes
20621259Swollman#define	if_imcasts	if_data.ifi_imcasts
20721259Swollman#define	if_omcasts	if_data.ifi_omcasts
20821259Swollman#define	if_iqdrops	if_data.ifi_iqdrops
20921259Swollman#define	if_noproto	if_data.ifi_noproto
21021259Swollman#define	if_lastchange	if_data.ifi_lastchange
21121259Swollman#define if_recvquota	if_data.ifi_recvquota
21221259Swollman#define	if_xmitquota	if_data.ifi_xmitquota
213136950Sjmg#define if_rawoutput(if, m, sa) if_output(if, m, sa, (struct rtentry *)NULL)
21421259Swollman
21553541Sshin/* for compatibility with other BSDs */
21653541Sshin#define	if_addrlist	if_addrhead
21753541Sshin#define	if_list		if_link
21853541Sshin
21921259Swollman/*
22021259Swollman * Output queues (ifp->if_snd) and slow device input queues (*ifp->if_slowq)
22121259Swollman * are queues of messages stored on ifqueue structures
22221259Swollman * (defined above).  Entries are added to and deleted from these structures
22321259Swollman * by these macros, which should be called with ipl raised to splimp().
22421259Swollman */
22572200Sbmilekic#define IF_LOCK(ifq)		mtx_lock(&(ifq)->ifq_mtx)
22672200Sbmilekic#define IF_UNLOCK(ifq)		mtx_unlock(&(ifq)->ifq_mtx)
227130416Smlaier#define	IF_LOCK_ASSERT(ifq)	mtx_assert(&(ifq)->ifq_mtx, MA_OWNED)
22869152Sjlemon#define	_IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
22969152Sjlemon#define	_IF_DROP(ifq)		((ifq)->ifq_drops++)
23069152Sjlemon#define	_IF_QLEN(ifq)		((ifq)->ifq_len)
23121259Swollman
23269152Sjlemon#define	_IF_ENQUEUE(ifq, m) do { 				\
23369152Sjlemon	(m)->m_nextpkt = NULL;					\
23469152Sjlemon	if ((ifq)->ifq_tail == NULL) 				\
23569152Sjlemon		(ifq)->ifq_head = m; 				\
23669152Sjlemon	else 							\
23769152Sjlemon		(ifq)->ifq_tail->m_nextpkt = m; 		\
23869152Sjlemon	(ifq)->ifq_tail = m; 					\
23969152Sjlemon	(ifq)->ifq_len++; 					\
24069152Sjlemon} while (0)
24169152Sjlemon
24269152Sjlemon#define IF_ENQUEUE(ifq, m) do {					\
24369152Sjlemon	IF_LOCK(ifq); 						\
24469152Sjlemon	_IF_ENQUEUE(ifq, m); 					\
24569152Sjlemon	IF_UNLOCK(ifq); 					\
24669152Sjlemon} while (0)
24769152Sjlemon
24869152Sjlemon#define	_IF_PREPEND(ifq, m) do {				\
24969152Sjlemon	(m)->m_nextpkt = (ifq)->ifq_head; 			\
25069152Sjlemon	if ((ifq)->ifq_tail == NULL) 				\
25169152Sjlemon		(ifq)->ifq_tail = (m); 				\
25269152Sjlemon	(ifq)->ifq_head = (m); 					\
25369152Sjlemon	(ifq)->ifq_len++; 					\
25469152Sjlemon} while (0)
25569152Sjlemon
25669152Sjlemon#define IF_PREPEND(ifq, m) do {		 			\
25769152Sjlemon	IF_LOCK(ifq); 						\
25869152Sjlemon	_IF_PREPEND(ifq, m); 					\
25969152Sjlemon	IF_UNLOCK(ifq); 					\
26069152Sjlemon} while (0)
26169152Sjlemon
26269152Sjlemon#define	_IF_DEQUEUE(ifq, m) do { 				\
26369152Sjlemon	(m) = (ifq)->ifq_head; 					\
26469152Sjlemon	if (m) { 						\
265136950Sjmg		if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL)	\
26669152Sjlemon			(ifq)->ifq_tail = NULL; 		\
26769152Sjlemon		(m)->m_nextpkt = NULL; 				\
26869152Sjlemon		(ifq)->ifq_len--; 				\
26969152Sjlemon	} 							\
27069152Sjlemon} while (0)
27169152Sjlemon
27269152Sjlemon#define IF_DEQUEUE(ifq, m) do { 				\
27369152Sjlemon	IF_LOCK(ifq); 						\
27469152Sjlemon	_IF_DEQUEUE(ifq, m); 					\
27569152Sjlemon	IF_UNLOCK(ifq); 					\
27669152Sjlemon} while (0)
27769152Sjlemon
278130416Smlaier#define	_IF_POLL(ifq, m)	((m) = (ifq)->ifq_head)
279130416Smlaier#define	IF_POLL(ifq, m)		_IF_POLL(ifq, m)
280130416Smlaier
281130416Smlaier#define _IF_DRAIN(ifq) do { 					\
28269152Sjlemon	struct mbuf *m; 					\
28369152Sjlemon	for (;;) { 						\
28469152Sjlemon		_IF_DEQUEUE(ifq, m); 				\
28569152Sjlemon		if (m == NULL) 					\
28669152Sjlemon			break; 					\
28769152Sjlemon		m_freem(m); 					\
28869152Sjlemon	} 							\
28969152Sjlemon} while (0)
29069152Sjlemon
291130416Smlaier#define IF_DRAIN(ifq) do {					\
292130416Smlaier	IF_LOCK(ifq);						\
293130416Smlaier	_IF_DRAIN(ifq);						\
294130416Smlaier	IF_UNLOCK(ifq);						\
295130416Smlaier} while(0)
296130416Smlaier
29755205Speter#ifdef _KERNEL
298126264Smlaier/* interface address change event */
299126264Smlaiertypedef void (*ifaddr_event_handler_t)(void *, struct ifnet *);
300126264SmlaierEVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
301126264Smlaier/* new interface arrival event */
302126264Smlaiertypedef void (*ifnet_arrival_event_handler_t)(void *, struct ifnet *);
303126264SmlaierEVENTHANDLER_DECLARE(ifnet_arrival_event, ifnet_arrival_event_handler_t);
304126264Smlaier/* interface departure event */
305126264Smlaiertypedef void (*ifnet_departure_event_handler_t)(void *, struct ifnet *);
306126264SmlaierEVENTHANDLER_DECLARE(ifnet_departure_event, ifnet_departure_event_handler_t);
307126264Smlaier
308121470Sume#define	IF_AFDATA_LOCK_INIT(ifp)	\
309121470Sume    mtx_init(&(ifp)->if_afdata_mtx, "if_afdata", NULL, MTX_DEF)
310121470Sume#define	IF_AFDATA_LOCK(ifp)	mtx_lock(&(ifp)->if_afdata_mtx)
311121470Sume#define	IF_AFDATA_TRYLOCK(ifp)	mtx_trylock(&(ifp)->if_afdata_mtx)
312121470Sume#define	IF_AFDATA_UNLOCK(ifp)	mtx_unlock(&(ifp)->if_afdata_mtx)
313121470Sume#define	IF_AFDATA_DESTROY(ifp)	mtx_destroy(&(ifp)->if_afdata_mtx)
314121470Sume
315136704Srwatson#define	IFF_LOCKGIANT(ifp) do {						\
316136704Srwatson	if ((ifp)->if_flags & IFF_NEEDSGIANT)				\
317136704Srwatson		mtx_lock(&Giant);					\
318136704Srwatson} while (0)
319136704Srwatson
320136704Srwatson#define	IFF_UNLOCKGIANT(ifp) do {					\
321136704Srwatson	if ((ifp)->if_flags & IFF_NEEDSGIANT)				\
322136704Srwatson		mtx_unlock(&Giant);					\
323136704Srwatson} while (0)
324136704Srwatson
325137065Srwatsonint	if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp,
326137065Srwatson	    int adjust);
327130416Smlaier#define	IF_HANDOFF(ifq, m, ifp)			\
328130416Smlaier	if_handoff((struct ifqueue *)ifq, m, ifp, 0)
329130416Smlaier#define	IF_HANDOFF_ADJ(ifq, m, ifp, adj)	\
330130416Smlaier	if_handoff((struct ifqueue *)ifq, m, ifp, adj)
33121259Swollman
332132712Srwatsonvoid	if_start(struct ifnet *);
333132712Srwatson
334130416Smlaier#define	IFQ_ENQUEUE(ifq, m, err)					\
335130416Smlaierdo {									\
336130416Smlaier	IF_LOCK(ifq);							\
337130416Smlaier	if (ALTQ_IS_ENABLED(ifq))					\
338130416Smlaier		ALTQ_ENQUEUE(ifq, m, NULL, err);			\
339130416Smlaier	else {								\
340130416Smlaier		if (_IF_QFULL(ifq)) {					\
341130416Smlaier			m_freem(m);					\
342130416Smlaier			(err) = ENOBUFS;				\
343130416Smlaier		} else {						\
344130416Smlaier			_IF_ENQUEUE(ifq, m);				\
345130416Smlaier			(err) = 0;					\
346130416Smlaier		}							\
347130416Smlaier	}								\
348130416Smlaier	if (err)							\
349130416Smlaier		(ifq)->ifq_drops++;					\
350130416Smlaier	IF_UNLOCK(ifq);							\
351130416Smlaier} while (0)
35221259Swollman
353130416Smlaier#define	IFQ_DEQUEUE_NOLOCK(ifq, m)					\
354130416Smlaierdo {									\
355130416Smlaier	if (TBR_IS_ENABLED(ifq))					\
356130508Smlaier		(m) = tbr_dequeue_ptr(ifq, ALTDQ_REMOVE);		\
357130416Smlaier	else if (ALTQ_IS_ENABLED(ifq))					\
358130416Smlaier		ALTQ_DEQUEUE(ifq, m);					\
359130416Smlaier	else								\
360130416Smlaier		_IF_DEQUEUE(ifq, m);					\
361130416Smlaier} while (0)
362130416Smlaier
363130416Smlaier#define	IFQ_DEQUEUE(ifq, m)						\
364130416Smlaierdo {									\
365130416Smlaier	IF_LOCK(ifq);							\
366130416Smlaier	IFQ_DEQUEUE_NOLOCK(ifq, m);					\
367130416Smlaier	IF_UNLOCK(ifq);							\
368130416Smlaier} while (0)
369130416Smlaier
370130416Smlaier#define	IFQ_POLL_NOLOCK(ifq, m)						\
371130416Smlaierdo {									\
372130416Smlaier	if (TBR_IS_ENABLED(ifq))					\
373130508Smlaier		(m) = tbr_dequeue_ptr(ifq, ALTDQ_POLL);			\
374130416Smlaier	else if (ALTQ_IS_ENABLED(ifq))					\
375130416Smlaier		ALTQ_POLL(ifq, m);					\
376130416Smlaier	else								\
377130416Smlaier		_IF_POLL(ifq, m);					\
378130416Smlaier} while (0)
379130416Smlaier
380130416Smlaier#define	IFQ_POLL(ifq, m)						\
381130416Smlaierdo {									\
382130416Smlaier	IF_LOCK(ifq);							\
383130416Smlaier	IFQ_POLL_NOLOCK(ifq, m);					\
384130416Smlaier	IF_UNLOCK(ifq);							\
385130416Smlaier} while (0)
386130416Smlaier
387130416Smlaier#define	IFQ_PURGE_NOLOCK(ifq)						\
388130416Smlaierdo {									\
389130416Smlaier	if (ALTQ_IS_ENABLED(ifq)) {					\
390130416Smlaier		ALTQ_PURGE(ifq);					\
391130416Smlaier	} else								\
392130416Smlaier		_IF_DRAIN(ifq);						\
393130416Smlaier} while (0)
394130416Smlaier
395130416Smlaier#define	IFQ_PURGE(ifq)							\
396130416Smlaierdo {									\
397130416Smlaier	IF_LOCK(ifq);							\
398130416Smlaier	IFQ_PURGE_NOLOCK(ifq);						\
399130416Smlaier	IF_UNLOCK(ifq);							\
400130416Smlaier} while (0)
401130416Smlaier
402130416Smlaier#define	IFQ_SET_READY(ifq)						\
403130416Smlaier	do { ((ifq)->altq_flags |= ALTQF_READY); } while (0)
404130416Smlaier
405130416Smlaier#define	IFQ_LOCK(ifq)			IF_LOCK(ifq)
406130416Smlaier#define	IFQ_UNLOCK(ifq)			IF_UNLOCK(ifq)
407130416Smlaier#define	IFQ_LOCK_ASSERT(ifq)		IF_LOCK_ASSERT(ifq)
408130416Smlaier#define	IFQ_IS_EMPTY(ifq)		((ifq)->ifq_len == 0)
409130416Smlaier#define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
410130416Smlaier#define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
411130416Smlaier#define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
412130416Smlaier#define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
413130416Smlaier
414130416Smlaier#define	IFQ_HANDOFF_ADJ(ifp, m, adj, err)				\
415130416Smlaierdo {									\
416130416Smlaier	int len;							\
417130416Smlaier	short mflags;							\
418130416Smlaier									\
419130416Smlaier	len = (m)->m_pkthdr.len;					\
420130416Smlaier	mflags = (m)->m_flags;						\
421130416Smlaier	IFQ_ENQUEUE(&(ifp)->if_snd, m, err);				\
422130416Smlaier	if ((err) == 0) {						\
423130416Smlaier		(ifp)->if_obytes += len + (adj);			\
424130416Smlaier		if (mflags & M_MCAST)					\
425130416Smlaier			(ifp)->if_omcasts++;				\
426130416Smlaier		if (((ifp)->if_flags & IFF_OACTIVE) == 0)		\
427132712Srwatson			if_start(ifp);					\
428130416Smlaier	}								\
429130416Smlaier} while (0)
430130416Smlaier
431130416Smlaier#define	IFQ_HANDOFF(ifp, m, err)					\
432130512Smlaier	IFQ_HANDOFF_ADJ(ifp, m, 0, err)
433130416Smlaier
434130416Smlaier#define	IFQ_DRV_DEQUEUE(ifq, m)						\
435130416Smlaierdo {									\
436130416Smlaier	(m) = (ifq)->ifq_drv_head;					\
437130416Smlaier	if (m) {							\
438130416Smlaier		if (((ifq)->ifq_drv_head = (m)->m_nextpkt) == NULL)	\
439130416Smlaier			(ifq)->ifq_drv_tail = NULL;			\
440130416Smlaier		(m)->m_nextpkt = NULL;					\
441130416Smlaier		(ifq)->ifq_drv_len--;					\
442130416Smlaier	} else {							\
443130416Smlaier		IFQ_LOCK(ifq);						\
444130416Smlaier		IFQ_DEQUEUE_NOLOCK(ifq, m);				\
445130416Smlaier		while ((ifq)->ifq_drv_len < (ifq)->ifq_drv_maxlen) {	\
446130416Smlaier			struct mbuf *m0;				\
447130416Smlaier			IFQ_DEQUEUE_NOLOCK(ifq, m0);			\
448130416Smlaier			if (m0 == NULL)					\
449130416Smlaier				break;					\
450130416Smlaier			m0->m_nextpkt = NULL;				\
451130416Smlaier			if ((ifq)->ifq_drv_tail == NULL)		\
452130416Smlaier				(ifq)->ifq_drv_head = m0;		\
453130416Smlaier			else						\
454130416Smlaier				(ifq)->ifq_drv_tail->m_nextpkt = m0;	\
455130416Smlaier			(ifq)->ifq_drv_tail = m0;			\
456130416Smlaier			(ifq)->ifq_drv_len++;				\
457130416Smlaier		}							\
458130416Smlaier		IFQ_UNLOCK(ifq);					\
459130416Smlaier	}								\
460130416Smlaier} while (0)
461130416Smlaier
462130416Smlaier#define	IFQ_DRV_PREPEND(ifq, m)						\
463130416Smlaierdo {									\
464130416Smlaier	(m)->m_nextpkt = (ifq)->ifq_drv_head;				\
465132152Smlaier	if ((ifq)->ifq_drv_tail == NULL)				\
466132152Smlaier		(ifq)->ifq_drv_tail = (m);				\
467130416Smlaier	(ifq)->ifq_drv_head = (m);					\
468130416Smlaier	(ifq)->ifq_drv_len++;						\
469130416Smlaier} while (0)
470130416Smlaier
471130416Smlaier#define	IFQ_DRV_IS_EMPTY(ifq)						\
472130416Smlaier	(((ifq)->ifq_drv_len == 0) && ((ifq)->ifq_len == 0))
473130416Smlaier
474130416Smlaier#define	IFQ_DRV_PURGE(ifq)						\
475130416Smlaierdo {									\
476132152Smlaier	struct mbuf *m, *n = (ifq)->ifq_drv_head;			\
477132152Smlaier	while((m = n) != NULL) {					\
478132152Smlaier		n = m->m_nextpkt;					\
479130416Smlaier		m_freem(m);						\
480130416Smlaier	}								\
481130416Smlaier	(ifq)->ifq_drv_head = (ifq)->ifq_drv_tail = NULL;		\
482130416Smlaier	(ifq)->ifq_drv_len = 0;						\
483130416Smlaier	IFQ_PURGE(ifq);							\
484130416Smlaier} while (0)
485130416Smlaier
48649459Sbrian/*
48749459Sbrian * 72 was chosen below because it is the size of a TCP/IP
48849459Sbrian * header (40) + the minimum mss (32).
48949459Sbrian */
49049459Sbrian#define	IF_MINMTU	72
49149459Sbrian#define	IF_MAXMTU	65535
49249459Sbrian
49355205Speter#endif /* _KERNEL */
49421259Swollman
49521259Swollman/*
49621259Swollman * The ifaddr structure contains information about one address
49721259Swollman * of an interface.  They are maintained by the different address families,
49821259Swollman * are allocated and attached when an address is set, and are linked
49921259Swollman * together so all addresses for an interface can be located.
500128291Sluigi *
501128291Sluigi * NOTE: a 'struct ifaddr' is always at the beginning of a larger
502128291Sluigi * chunk of malloc'ed memory, where we store the three addresses
503128291Sluigi * (ifa_addr, ifa_dstaddr and ifa_netmask) referenced here.
50421259Swollman */
50521259Swollmanstruct ifaddr {
50621259Swollman	struct	sockaddr *ifa_addr;	/* address of interface */
50721259Swollman	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
50821259Swollman#define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
50921259Swollman	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
51067334Sjoe	struct	if_data if_data;	/* not all members are meaningful */
51121259Swollman	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
51260938Sjake	TAILQ_ENTRY(ifaddr) ifa_link;	/* queue macro glue */
51321259Swollman	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
51492725Salfred		(int, struct rtentry *, struct rt_addrinfo *);
51521259Swollman	u_short	ifa_flags;		/* mostly rt_flags for cloning */
51647254Spb	u_int	ifa_refcnt;		/* references to this structure */
51721259Swollman	int	ifa_metric;		/* cost of going out this interface */
51828845Sjulian	int (*ifa_claim_addr)		/* check if an addr goes to this if */
51992725Salfred		(struct ifaddr *, struct sockaddr *);
520108033Shsu	struct mtx ifa_mtx;
52121259Swollman};
52221259Swollman#define	IFA_ROUTE	RTF_UP		/* route installed */
52321259Swollman
52453541Sshin/* for compatibility with other BSDs */
52553541Sshin#define	ifa_list	ifa_link
52653541Sshin
527108033Shsu#define	IFA_LOCK_INIT(ifa)	\
528108033Shsu    mtx_init(&(ifa)->ifa_mtx, "ifaddr", NULL, MTX_DEF)
529108033Shsu#define	IFA_LOCK(ifa)		mtx_lock(&(ifa)->ifa_mtx)
530108033Shsu#define	IFA_UNLOCK(ifa)		mtx_unlock(&(ifa)->ifa_mtx)
531108033Shsu#define	IFA_DESTROY(ifa)	mtx_destroy(&(ifa)->ifa_mtx)
532108033Shsu
53321404Swollman/*
53452904Sshin * The prefix structure contains information about one prefix
53552904Sshin * of an interface.  They are maintained by the different address families,
536108470Sschweikh * are allocated and attached when a prefix or an address is set,
53753541Sshin * and are linked together so all prefixes for an interface can be located.
53852904Sshin */
53952904Sshinstruct ifprefix {
54052904Sshin	struct	sockaddr *ifpr_prefix;	/* prefix of interface */
54152904Sshin	struct	ifnet *ifpr_ifp;	/* back-pointer to interface */
54260938Sjake	TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */
54352904Sshin	u_char	ifpr_plen;		/* prefix length in bits */
54452904Sshin	u_char	ifpr_type;		/* protocol dependent prefix type */
54552904Sshin};
54652904Sshin
54752904Sshin/*
54821404Swollman * Multicast address structure.  This is analogous to the ifaddr
54921404Swollman * structure except that it keeps track of multicast addresses.
55021404Swollman * Also, the reference count here is a count of requests for this
55121404Swollman * address, not a count of pointers to this structure.
55221404Swollman */
55321404Swollmanstruct ifmultiaddr {
55472084Sphk	TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
55521434Swollman	struct	sockaddr *ifma_addr; 	/* address this membership is for */
55621434Swollman	struct	sockaddr *ifma_lladdr;	/* link-layer translation, if any */
55721434Swollman	struct	ifnet *ifma_ifp;	/* back-pointer to interface */
55821434Swollman	u_int	ifma_refcount;		/* reference count */
55921434Swollman	void	*ifma_protospec;	/* protocol-specific state, if any */
56021404Swollman};
56121404Swollman
56255205Speter#ifdef _KERNEL
563108036Shsu#define	IFAFREE(ifa)					\
564108036Shsu	do {						\
565108036Shsu		IFA_LOCK(ifa);				\
566108036Shsu		KASSERT((ifa)->ifa_refcnt > 0,		\
567108036Shsu		    ("ifa %p !(ifa_refcnt > 0)", ifa));	\
568108036Shsu		if (--(ifa)->ifa_refcnt == 0) {		\
569108036Shsu			IFA_DESTROY(ifa);		\
570108036Shsu			free(ifa, M_IFADDR);		\
571108036Shsu		} else 					\
572108036Shsu			IFA_UNLOCK(ifa);		\
57346568Speter	} while (0)
57421259Swollman
575108036Shsu#define IFAREF(ifa)					\
576108036Shsu	do {						\
577108036Shsu		IFA_LOCK(ifa);				\
578108036Shsu		++(ifa)->ifa_refcnt;			\
579108036Shsu		IFA_UNLOCK(ifa);			\
580108033Shsu	} while (0)
581108033Shsu
582108172Shsuextern	struct mtx ifnet_lock;
583108298Shsu#define	IFNET_LOCK_INIT() \
584108298Shsu    mtx_init(&ifnet_lock, "ifnet", NULL, MTX_DEF | MTX_RECURSE)
585108172Shsu#define	IFNET_WLOCK()		mtx_lock(&ifnet_lock)
586108172Shsu#define	IFNET_WUNLOCK()		mtx_unlock(&ifnet_lock)
587108172Shsu#define	IFNET_RLOCK()		IFNET_WLOCK()
588108172Shsu#define	IFNET_RUNLOCK()		IFNET_WUNLOCK()
589108172Shsu
59083130Sjlemonstruct ifindex_entry {
59187914Sjlemon	struct	ifnet *ife_ifnet;
59283130Sjlemon	struct	ifaddr *ife_ifnet_addr;
593130585Sphk	struct cdev *ife_dev;
59483130Sjlemon};
59583130Sjlemon
59683130Sjlemon#define ifnet_byindex(idx)	ifindex_table[(idx)].ife_ifnet
597128315Sluigi/*
598128315Sluigi * Given the index, ifaddr_byindex() returns the one and only
599128315Sluigi * link-level ifaddr for the interface. You are not supposed to use
600128315Sluigi * it to traverse the list of addresses associated to the interface.
601128315Sluigi */
60283130Sjlemon#define ifaddr_byindex(idx)	ifindex_table[(idx)].ife_ifnet_addr
60383130Sjlemon#define ifdev_byindex(idx)	ifindex_table[(idx)].ife_dev
60483130Sjlemon
60521259Swollmanextern	struct ifnethead ifnet;
60683130Sjlemonextern	struct ifindex_entry *ifindex_table;
60721259Swollmanextern	int ifqmaxlen;
60871791Speterextern	struct ifnet *loif;	/* first loopback interface */
60921259Swollmanextern	int if_index;
61021259Swollman
61192725Salfredint	if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
61292725Salfredint	if_allmulti(struct ifnet *, int);
613147256Sbrooksstruct	ifnet* if_alloc(u_char);
61492725Salfredvoid	if_attach(struct ifnet *);
61592725Salfredint	if_delmulti(struct ifnet *, struct sockaddr *);
61692725Salfredvoid	if_detach(struct ifnet *);
617146620Speadarvoid	if_purgeaddrs(struct ifnet *);
61892725Salfredvoid	if_down(struct ifnet *);
619147256Sbrooksvoid	if_free(struct ifnet *);
620147256Sbrooksvoid	if_free_type(struct ifnet *, u_char);
621121816Sbrooksvoid	if_initname(struct ifnet *, const char *, int);
622138542Ssamvoid	if_link_state_change(struct ifnet *, int);
623103900Sbrooksint	if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
62492725Salfredint	if_setlladdr(struct ifnet *, const u_char *, int);
62592725Salfredvoid	if_up(struct ifnet *);
62692725Salfred/*void	ifinit(void);*/ /* declared in systm.h for main() */
62792725Salfredint	ifioctl(struct socket *, u_long, caddr_t, struct thread *);
62892725Salfredint	ifpromisc(struct ifnet *, int);
62992725Salfredstruct	ifnet *ifunit(const char *);
63021259Swollman
63192725Salfredstruct	ifaddr *ifa_ifwithaddr(struct sockaddr *);
63292725Salfredstruct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
63392725Salfredstruct	ifaddr *ifa_ifwithnet(struct sockaddr *);
63492725Salfredstruct	ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *);
63592725Salfredstruct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
63621259Swollman
63792725Salfredstruct	ifmultiaddr *ifmaof_ifpforaddr(struct sockaddr *, struct ifnet *);
63892725Salfredint	if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);
63921434Swollman
640147256Sbrookstypedef	void *if_com_alloc_t(u_char type, struct ifnet *ifp);
641147256Sbrookstypedef	void if_com_free_t(void *com, u_char type);
642147256Sbrooksvoid	if_register_com_alloc(u_char type, if_com_alloc_t *a, if_com_free_t *f);
643147256Sbrooksvoid	if_deregister_com_alloc(u_char type);
644147256Sbrooks
64584931Sfjoe#define IF_LLADDR(ifp)							\
64684931Sfjoe    LLADDR((struct sockaddr_dl *) ifaddr_byindex((ifp)->if_index)->ifa_addr)
64784931Sfjoe
64887902Sluigi#ifdef DEVICE_POLLING
64987902Sluigienum poll_cmd {	POLL_ONLY, POLL_AND_CHECK_STATUS, POLL_DEREGISTER };
65087902Sluigi
65192725Salfredtypedef	void poll_handler_t(struct ifnet *ifp, enum poll_cmd cmd, int count);
65292725Salfredint    ether_poll_register(poll_handler_t *h, struct ifnet *ifp);
65392725Salfredint    ether_poll_deregister(struct ifnet *ifp);
65487902Sluigi#endif /* DEVICE_POLLING */
65587902Sluigi
65655205Speter#endif /* _KERNEL */
65721259Swollman
65821259Swollman#endif /* !_NET_IF_VAR_H_ */
659