ifq.h revision 128376
121259Swollman/*
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 128376 2004-04-18 01:15:32Z luigi $
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;
7121259Swollman#endif
7221259Swollman
7321259Swollman#include <sys/queue.h>		/* get TAILQ macros */
7421259Swollman
7569224Sjlemon#ifdef _KERNEL
7669152Sjlemon#include <sys/mbuf.h>
77126264Smlaier#include <sys/eventhandler.h>
7869224Sjlemon#endif /* _KERNEL */
7974914Sjhb#include <sys/lock.h>		/* XXX */
8074914Sjhb#include <sys/mutex.h>		/* XXX */
8183130Sjlemon#include <sys/event.h>		/* XXX */
8269152Sjlemon
83121816Sbrooks#define	IF_DUNIT_NONE	-1
84121816Sbrooks
8560938SjakeTAILQ_HEAD(ifnethead, ifnet);	/* we use TAILQs so that the order of */
8660938SjakeTAILQ_HEAD(ifaddrhead, ifaddr);	/* instantiation is preserved in the list */
8760938SjakeTAILQ_HEAD(ifprefixhead, ifprefix);
8872084SphkTAILQ_HEAD(ifmultihead, ifmultiaddr);
8921259Swollman
9021259Swollman/*
9121259Swollman * Structure defining a queue for a network interface.
9221259Swollman */
9321259Swollmanstruct	ifqueue {
9421259Swollman	struct	mbuf *ifq_head;
9521259Swollman	struct	mbuf *ifq_tail;
9621259Swollman	int	ifq_len;
9721259Swollman	int	ifq_maxlen;
9821259Swollman	int	ifq_drops;
9969152Sjlemon	struct	mtx ifq_mtx;
10021259Swollman};
10121259Swollman
10221259Swollman/*
10321259Swollman * Structure defining a network interface.
10421259Swollman *
10521259Swollman * (Would like to call this struct ``if'', but C isn't PL/1.)
10621259Swollman */
10784380Smjacob
10884380Smjacob/*
10984380Smjacob * NB: For FreeBSD, it is assumed that each NIC driver's softc starts with
11084380Smjacob * one of these structures, typically held within an arpcom structure.
11186797Sluigi *
11286797Sluigi *	struct <foo>_softc {
11386797Sluigi *		struct arpcom {
11486797Sluigi *			struct  ifnet ac_if;
11586797Sluigi *			...
11686797Sluigi *		} <arpcom> ;
11786797Sluigi *		...
11886797Sluigi *	};
11986797Sluigi *
12086797Sluigi * The assumption is used in a number of places, including many
12186797Sluigi * files in sys/net, device drivers, and sys/dev/mii.c:miibus_attach().
12286797Sluigi *
12386797Sluigi * Unfortunately devices' softc are opaque, so we depend on this layout
12486797Sluigi * to locate the struct ifnet from the softc in the generic code.
12586797Sluigi *
12684380Smjacob */
12721259Swollmanstruct ifnet {
12821259Swollman	void	*if_softc;		/* pointer to driver state */
12960938Sjake	TAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained */
130121816Sbrooks	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
131121816Sbrooks	const char *if_dname;		/* driver name */
132121816Sbrooks	int	if_dunit;		/* unit or IF_DUNIT_NONE */
13321259Swollman	struct	ifaddrhead if_addrhead;	/* linked list of addresses per if */
134128291Sluigi		/*
135128291Sluigi		 * if_addrhead is the list of all addresses associated to
136128315Sluigi		 * an interface.
137128315Sluigi		 * Some code in the kernel assumes that first element
138128315Sluigi		 * of the list has type AF_LINK, and contains sockaddr_dl
139128315Sluigi		 * addresses which store the link-level address and the name
140128291Sluigi		 * of the interface.
141128315Sluigi		 * However, access to the AF_LINK address through this
142128315Sluigi		 * field is deprecated. Use ifaddr_byindex() instead.
143128291Sluigi		 */
14483130Sjlemon	struct	klist if_klist;		/* events attached to this if */
14583130Sjlemon	int	if_pcount;		/* number of promiscuous listeners */
14621259Swollman	struct	bpf_if *if_bpf;		/* packet filter structure */
14721259Swollman	u_short	if_index;		/* numeric abbreviation for this if  */
14821259Swollman	short	if_timer;		/* time 'til if_watchdog called */
149106931Ssam	u_short	if_nvlans;		/* number of active vlans */
150102052Ssobomax	int	if_flags;		/* up/down, broadcast, etc. */
15183624Sjlemon	int	if_capabilities;	/* interface capabilities */
15283624Sjlemon	int	if_capenable;		/* enabled features */
15321259Swollman	void	*if_linkmib;		/* link-type-specific MIB data */
15421259Swollman	size_t	if_linkmiblen;		/* length of above data */
15521259Swollman	struct	if_data if_data;
15621404Swollman	struct	ifmultihead if_multiaddrs; /* multicast addresses configured */
15721404Swollman	int	if_amcount;		/* number of all-multicast requests */
15821259Swollman/* procedure handles */
15921259Swollman	int	(*if_output)		/* output routine (enqueue) */
16092725Salfred		(struct ifnet *, struct mbuf *, struct sockaddr *,
16192725Salfred		     struct rtentry *);
162106931Ssam	void	(*if_input)		/* input routine (from h/w driver) */
163106931Ssam		(struct ifnet *, struct mbuf *);
16421259Swollman	void	(*if_start)		/* initiate output routine */
16592725Salfred		(struct ifnet *);
16621259Swollman	int	(*if_ioctl)		/* ioctl routine */
16792725Salfred		(struct ifnet *, u_long, caddr_t);
16821259Swollman	void	(*if_watchdog)		/* timer routine */
16992725Salfred		(struct ifnet *);
17021259Swollman	void	(*if_init)		/* Init routine */
17192725Salfred		(void *);
17221404Swollman	int	(*if_resolvemulti)	/* validate/resolve multicast */
17392725Salfred		(struct ifnet *, struct sockaddr **, struct sockaddr *);
17421259Swollman	struct	ifqueue if_snd;		/* output queue */
175123220Simp	const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */
176127828Sluigi
177127828Sluigi	struct	lltable *lltables;	/* list of L3-L2 resolution tables */
178127828Sluigi
179122524Srwatson	struct	label *if_label;	/* interface MAC label */
180121161Sume
181127828Sluigi	/* these are only used by IPv6 */
182127828Sluigi	struct	ifprefixhead if_prefixhead; /* list of prefixes per if */
183121161Sume	void	*if_afdata[AF_MAX];
184121470Sume	int	if_afdata_initialized;
185121470Sume	struct	mtx if_afdata_mtx;
18621259Swollman};
18769152Sjlemon
18892725Salfredtypedef void if_init_f_t(void *);
18921259Swollman
190128376Sluigi/*
191128376Sluigi * XXX These aliases are terribly dangerous because they could apply
192128376Sluigi * to anything.
193128376Sluigi */
19421259Swollman#define	if_mtu		if_data.ifi_mtu
19521259Swollman#define	if_type		if_data.ifi_type
19621259Swollman#define if_physical	if_data.ifi_physical
19721259Swollman#define	if_addrlen	if_data.ifi_addrlen
19821259Swollman#define	if_hdrlen	if_data.ifi_hdrlen
19921259Swollman#define	if_metric	if_data.ifi_metric
20021259Swollman#define	if_baudrate	if_data.ifi_baudrate
20158698Sjlemon#define	if_hwassist	if_data.ifi_hwassist
20221259Swollman#define	if_ipackets	if_data.ifi_ipackets
20321259Swollman#define	if_ierrors	if_data.ifi_ierrors
20421259Swollman#define	if_opackets	if_data.ifi_opackets
20521259Swollman#define	if_oerrors	if_data.ifi_oerrors
20621259Swollman#define	if_collisions	if_data.ifi_collisions
20721259Swollman#define	if_ibytes	if_data.ifi_ibytes
20821259Swollman#define	if_obytes	if_data.ifi_obytes
20921259Swollman#define	if_imcasts	if_data.ifi_imcasts
21021259Swollman#define	if_omcasts	if_data.ifi_omcasts
21121259Swollman#define	if_iqdrops	if_data.ifi_iqdrops
21221259Swollman#define	if_noproto	if_data.ifi_noproto
21321259Swollman#define	if_lastchange	if_data.ifi_lastchange
21421259Swollman#define if_recvquota	if_data.ifi_recvquota
21521259Swollman#define	if_xmitquota	if_data.ifi_xmitquota
21621259Swollman#define if_rawoutput(if, m, sa) if_output(if, m, sa, (struct rtentry *)0)
21721259Swollman
21853541Sshin/* for compatibility with other BSDs */
21953541Sshin#define	if_addrlist	if_addrhead
22053541Sshin#define	if_list		if_link
22153541Sshin
22221259Swollman/*
22321259Swollman * Output queues (ifp->if_snd) and slow device input queues (*ifp->if_slowq)
22421259Swollman * are queues of messages stored on ifqueue structures
22521259Swollman * (defined above).  Entries are added to and deleted from these structures
22621259Swollman * by these macros, which should be called with ipl raised to splimp().
22721259Swollman */
22872200Sbmilekic#define IF_LOCK(ifq)		mtx_lock(&(ifq)->ifq_mtx)
22972200Sbmilekic#define IF_UNLOCK(ifq)		mtx_unlock(&(ifq)->ifq_mtx)
23069152Sjlemon#define	_IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
23169152Sjlemon#define	_IF_DROP(ifq)		((ifq)->ifq_drops++)
23269152Sjlemon#define	_IF_QLEN(ifq)		((ifq)->ifq_len)
23321259Swollman
23469152Sjlemon#define	_IF_ENQUEUE(ifq, m) do { 				\
23569152Sjlemon	(m)->m_nextpkt = NULL;					\
23669152Sjlemon	if ((ifq)->ifq_tail == NULL) 				\
23769152Sjlemon		(ifq)->ifq_head = m; 				\
23869152Sjlemon	else 							\
23969152Sjlemon		(ifq)->ifq_tail->m_nextpkt = m; 		\
24069152Sjlemon	(ifq)->ifq_tail = m; 					\
24169152Sjlemon	(ifq)->ifq_len++; 					\
24269152Sjlemon} while (0)
24369152Sjlemon
24469152Sjlemon#define IF_ENQUEUE(ifq, m) do {					\
24569152Sjlemon	IF_LOCK(ifq); 						\
24669152Sjlemon	_IF_ENQUEUE(ifq, m); 					\
24769152Sjlemon	IF_UNLOCK(ifq); 					\
24869152Sjlemon} while (0)
24969152Sjlemon
25069152Sjlemon#define	_IF_PREPEND(ifq, m) do {				\
25169152Sjlemon	(m)->m_nextpkt = (ifq)->ifq_head; 			\
25269152Sjlemon	if ((ifq)->ifq_tail == NULL) 				\
25369152Sjlemon		(ifq)->ifq_tail = (m); 				\
25469152Sjlemon	(ifq)->ifq_head = (m); 					\
25569152Sjlemon	(ifq)->ifq_len++; 					\
25669152Sjlemon} while (0)
25769152Sjlemon
25869152Sjlemon#define IF_PREPEND(ifq, m) do {		 			\
25969152Sjlemon	IF_LOCK(ifq); 						\
26069152Sjlemon	_IF_PREPEND(ifq, m); 					\
26169152Sjlemon	IF_UNLOCK(ifq); 					\
26269152Sjlemon} while (0)
26369152Sjlemon
26469152Sjlemon#define	_IF_DEQUEUE(ifq, m) do { 				\
26569152Sjlemon	(m) = (ifq)->ifq_head; 					\
26669152Sjlemon	if (m) { 						\
26769152Sjlemon		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) 	\
26869152Sjlemon			(ifq)->ifq_tail = NULL; 		\
26969152Sjlemon		(m)->m_nextpkt = NULL; 				\
27069152Sjlemon		(ifq)->ifq_len--; 				\
27169152Sjlemon	} 							\
27269152Sjlemon} while (0)
27369152Sjlemon
27469152Sjlemon#define IF_DEQUEUE(ifq, m) do { 				\
27569152Sjlemon	IF_LOCK(ifq); 						\
27669152Sjlemon	_IF_DEQUEUE(ifq, m); 					\
27769152Sjlemon	IF_UNLOCK(ifq); 					\
27869152Sjlemon} while (0)
27969152Sjlemon
28069152Sjlemon#define IF_DRAIN(ifq) do { 					\
28169152Sjlemon	struct mbuf *m; 					\
28269152Sjlemon	IF_LOCK(ifq); 						\
28369152Sjlemon	for (;;) { 						\
28469152Sjlemon		_IF_DEQUEUE(ifq, m); 				\
28569152Sjlemon		if (m == NULL) 					\
28669152Sjlemon			break; 					\
28769152Sjlemon		m_freem(m); 					\
28869152Sjlemon	} 							\
28969152Sjlemon	IF_UNLOCK(ifq); 					\
29069152Sjlemon} while (0)
29169152Sjlemon
29255205Speter#ifdef _KERNEL
293126264Smlaier/* interface address change event */
294126264Smlaiertypedef void (*ifaddr_event_handler_t)(void *, struct ifnet *);
295126264SmlaierEVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
296126264Smlaier/* new interface arrival event */
297126264Smlaiertypedef void (*ifnet_arrival_event_handler_t)(void *, struct ifnet *);
298126264SmlaierEVENTHANDLER_DECLARE(ifnet_arrival_event, ifnet_arrival_event_handler_t);
299126264Smlaier/* interface departure event */
300126264Smlaiertypedef void (*ifnet_departure_event_handler_t)(void *, struct ifnet *);
301126264SmlaierEVENTHANDLER_DECLARE(ifnet_departure_event, ifnet_departure_event_handler_t);
302126264Smlaier/* interface clone event */
303126264Smlaiertypedef void (*if_clone_event_handler_t)(void *, struct if_clone *);
304126264SmlaierEVENTHANDLER_DECLARE(if_clone_event, if_clone_event_handler_t);
305126264Smlaier
306121470Sume#define	IF_AFDATA_LOCK_INIT(ifp)	\
307121470Sume    mtx_init(&(ifp)->if_afdata_mtx, "if_afdata", NULL, MTX_DEF)
308121470Sume#define	IF_AFDATA_LOCK(ifp)	mtx_lock(&(ifp)->if_afdata_mtx)
309121470Sume#define	IF_AFDATA_TRYLOCK(ifp)	mtx_trylock(&(ifp)->if_afdata_mtx)
310121470Sume#define	IF_AFDATA_UNLOCK(ifp)	mtx_unlock(&(ifp)->if_afdata_mtx)
311121470Sume#define	IF_AFDATA_DESTROY(ifp)	mtx_destroy(&(ifp)->if_afdata_mtx)
312121470Sume
31369152Sjlemon#define	IF_HANDOFF(ifq, m, ifp)			if_handoff(ifq, m, ifp, 0)
31469152Sjlemon#define	IF_HANDOFF_ADJ(ifq, m, ifp, adj)	if_handoff(ifq, m, ifp, adj)
31521259Swollman
31635210Sbdestatic __inline int
31769152Sjlemonif_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
31821259Swollman{
31969152Sjlemon	int active = 0;
32021259Swollman
32169152Sjlemon	IF_LOCK(ifq);
32269152Sjlemon	if (_IF_QFULL(ifq)) {
32369152Sjlemon		_IF_DROP(ifq);
32469152Sjlemon		IF_UNLOCK(ifq);
32569152Sjlemon		m_freem(m);
32669152Sjlemon		return (0);
32769152Sjlemon	}
32869152Sjlemon	if (ifp != NULL) {
32969152Sjlemon		ifp->if_obytes += m->m_pkthdr.len + adjust;
330128157Sru		if (m->m_flags & (M_BCAST|M_MCAST))
33169152Sjlemon			ifp->if_omcasts++;
33269152Sjlemon		active = ifp->if_flags & IFF_OACTIVE;
33369152Sjlemon	}
33469152Sjlemon	_IF_ENQUEUE(ifq, m);
33569152Sjlemon	IF_UNLOCK(ifq);
33686364Sjhb	if (ifp != NULL && !active)
33796173Simp		(*ifp->if_start)(ifp);
33869152Sjlemon	return (1);
33921259Swollman}
34021259Swollman
34149459Sbrian/*
34249459Sbrian * 72 was chosen below because it is the size of a TCP/IP
34349459Sbrian * header (40) + the minimum mss (32).
34449459Sbrian */
34549459Sbrian#define	IF_MINMTU	72
34649459Sbrian#define	IF_MAXMTU	65535
34749459Sbrian
34855205Speter#endif /* _KERNEL */
34921259Swollman
35021259Swollman/*
35121259Swollman * The ifaddr structure contains information about one address
35221259Swollman * of an interface.  They are maintained by the different address families,
35321259Swollman * are allocated and attached when an address is set, and are linked
35421259Swollman * together so all addresses for an interface can be located.
355128291Sluigi *
356128291Sluigi * NOTE: a 'struct ifaddr' is always at the beginning of a larger
357128291Sluigi * chunk of malloc'ed memory, where we store the three addresses
358128291Sluigi * (ifa_addr, ifa_dstaddr and ifa_netmask) referenced here.
35921259Swollman */
36021259Swollmanstruct ifaddr {
36121259Swollman	struct	sockaddr *ifa_addr;	/* address of interface */
36221259Swollman	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
36321259Swollman#define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
36421259Swollman	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
36567334Sjoe	struct	if_data if_data;	/* not all members are meaningful */
36621259Swollman	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
36760938Sjake	TAILQ_ENTRY(ifaddr) ifa_link;	/* queue macro glue */
36821259Swollman	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
36992725Salfred		(int, struct rtentry *, struct rt_addrinfo *);
37021259Swollman	u_short	ifa_flags;		/* mostly rt_flags for cloning */
37147254Spb	u_int	ifa_refcnt;		/* references to this structure */
37221259Swollman	int	ifa_metric;		/* cost of going out this interface */
37328845Sjulian	int (*ifa_claim_addr)		/* check if an addr goes to this if */
37492725Salfred		(struct ifaddr *, struct sockaddr *);
375108033Shsu	struct mtx ifa_mtx;
37621259Swollman};
37721259Swollman#define	IFA_ROUTE	RTF_UP		/* route installed */
37821259Swollman
37953541Sshin/* for compatibility with other BSDs */
38053541Sshin#define	ifa_list	ifa_link
38153541Sshin
382108033Shsu#define	IFA_LOCK_INIT(ifa)	\
383108033Shsu    mtx_init(&(ifa)->ifa_mtx, "ifaddr", NULL, MTX_DEF)
384108033Shsu#define	IFA_LOCK(ifa)		mtx_lock(&(ifa)->ifa_mtx)
385108033Shsu#define	IFA_UNLOCK(ifa)		mtx_unlock(&(ifa)->ifa_mtx)
386108033Shsu#define	IFA_DESTROY(ifa)	mtx_destroy(&(ifa)->ifa_mtx)
387108033Shsu
38821404Swollman/*
38952904Sshin * The prefix structure contains information about one prefix
39052904Sshin * of an interface.  They are maintained by the different address families,
391108470Sschweikh * are allocated and attached when a prefix or an address is set,
39253541Sshin * and are linked together so all prefixes for an interface can be located.
39352904Sshin */
39452904Sshinstruct ifprefix {
39552904Sshin	struct	sockaddr *ifpr_prefix;	/* prefix of interface */
39652904Sshin	struct	ifnet *ifpr_ifp;	/* back-pointer to interface */
39760938Sjake	TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */
39852904Sshin	u_char	ifpr_plen;		/* prefix length in bits */
39952904Sshin	u_char	ifpr_type;		/* protocol dependent prefix type */
40052904Sshin};
40152904Sshin
40252904Sshin/*
40321404Swollman * Multicast address structure.  This is analogous to the ifaddr
40421404Swollman * structure except that it keeps track of multicast addresses.
40521404Swollman * Also, the reference count here is a count of requests for this
40621404Swollman * address, not a count of pointers to this structure.
40721404Swollman */
40821404Swollmanstruct ifmultiaddr {
40972084Sphk	TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
41021434Swollman	struct	sockaddr *ifma_addr; 	/* address this membership is for */
41121434Swollman	struct	sockaddr *ifma_lladdr;	/* link-layer translation, if any */
41221434Swollman	struct	ifnet *ifma_ifp;	/* back-pointer to interface */
41321434Swollman	u_int	ifma_refcount;		/* reference count */
41421434Swollman	void	*ifma_protospec;	/* protocol-specific state, if any */
41521404Swollman};
41621404Swollman
41755205Speter#ifdef _KERNEL
418108036Shsu#define	IFAFREE(ifa)					\
419108036Shsu	do {						\
420108036Shsu		IFA_LOCK(ifa);				\
421108036Shsu		KASSERT((ifa)->ifa_refcnt > 0,		\
422108036Shsu		    ("ifa %p !(ifa_refcnt > 0)", ifa));	\
423108036Shsu		if (--(ifa)->ifa_refcnt == 0) {		\
424108036Shsu			IFA_DESTROY(ifa);		\
425108036Shsu			free(ifa, M_IFADDR);		\
426108036Shsu		} else 					\
427108036Shsu			IFA_UNLOCK(ifa);		\
42846568Speter	} while (0)
42921259Swollman
430108036Shsu#define IFAREF(ifa)					\
431108036Shsu	do {						\
432108036Shsu		IFA_LOCK(ifa);				\
433108036Shsu		++(ifa)->ifa_refcnt;			\
434108036Shsu		IFA_UNLOCK(ifa);			\
435108033Shsu	} while (0)
436108033Shsu
437108172Shsuextern	struct mtx ifnet_lock;
438108298Shsu#define	IFNET_LOCK_INIT() \
439108298Shsu    mtx_init(&ifnet_lock, "ifnet", NULL, MTX_DEF | MTX_RECURSE)
440108172Shsu#define	IFNET_WLOCK()		mtx_lock(&ifnet_lock)
441108172Shsu#define	IFNET_WUNLOCK()		mtx_unlock(&ifnet_lock)
442108172Shsu#define	IFNET_RLOCK()		IFNET_WLOCK()
443108172Shsu#define	IFNET_RUNLOCK()		IFNET_WUNLOCK()
444108172Shsu
44583130Sjlemonstruct ifindex_entry {
44687914Sjlemon	struct	ifnet *ife_ifnet;
44783130Sjlemon	struct	ifaddr *ife_ifnet_addr;
44883130Sjlemon	dev_t	ife_dev;
44983130Sjlemon};
45083130Sjlemon
45183130Sjlemon#define ifnet_byindex(idx)	ifindex_table[(idx)].ife_ifnet
452128315Sluigi/*
453128315Sluigi * Given the index, ifaddr_byindex() returns the one and only
454128315Sluigi * link-level ifaddr for the interface. You are not supposed to use
455128315Sluigi * it to traverse the list of addresses associated to the interface.
456128315Sluigi */
45783130Sjlemon#define ifaddr_byindex(idx)	ifindex_table[(idx)].ife_ifnet_addr
45883130Sjlemon#define ifdev_byindex(idx)	ifindex_table[(idx)].ife_dev
45983130Sjlemon
46021259Swollmanextern	struct ifnethead ifnet;
46183130Sjlemonextern	struct ifindex_entry *ifindex_table;
46221259Swollmanextern	int ifqmaxlen;
46371791Speterextern	struct ifnet *loif;	/* first loopback interface */
46421259Swollmanextern	int if_index;
46521259Swollman
46692725Salfredint	if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
46792725Salfredint	if_allmulti(struct ifnet *, int);
46892725Salfredvoid	if_attach(struct ifnet *);
46992725Salfredint	if_delmulti(struct ifnet *, struct sockaddr *);
47092725Salfredvoid	if_detach(struct ifnet *);
47192725Salfredvoid	if_down(struct ifnet *);
472121816Sbrooksvoid	if_initname(struct ifnet *, const char *, int);
473103900Sbrooksint	if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
47492725Salfredvoid	if_route(struct ifnet *, int flag, int fam);
47592725Salfredint	if_setlladdr(struct ifnet *, const u_char *, int);
47692725Salfredvoid	if_unroute(struct ifnet *, int flag, int fam);
47792725Salfredvoid	if_up(struct ifnet *);
47892725Salfred/*void	ifinit(void);*/ /* declared in systm.h for main() */
47992725Salfredint	ifioctl(struct socket *, u_long, caddr_t, struct thread *);
48092725Salfredint	ifpromisc(struct ifnet *, int);
48192725Salfredstruct	ifnet *ifunit(const char *);
48221259Swollman
48392725Salfredstruct	ifaddr *ifa_ifwithaddr(struct sockaddr *);
48492725Salfredstruct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
48592725Salfredstruct	ifaddr *ifa_ifwithnet(struct sockaddr *);
48692725Salfredstruct	ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *);
48792725Salfredstruct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
48821259Swollman
48992725Salfredstruct	ifmultiaddr *ifmaof_ifpforaddr(struct sockaddr *, struct ifnet *);
49092725Salfredint	if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);
49121434Swollman
49292725Salfredvoid	if_clone_attach(struct if_clone *);
49392725Salfredvoid	if_clone_detach(struct if_clone *);
49479103Sbrooks
49592725Salfredint	if_clone_create(char *, int);
49692725Salfredint	if_clone_destroy(const char *);
49779103Sbrooks
49884931Sfjoe#define IF_LLADDR(ifp)							\
49984931Sfjoe    LLADDR((struct sockaddr_dl *) ifaddr_byindex((ifp)->if_index)->ifa_addr)
50084931Sfjoe
50187902Sluigi#ifdef DEVICE_POLLING
50287902Sluigienum poll_cmd {	POLL_ONLY, POLL_AND_CHECK_STATUS, POLL_DEREGISTER };
50387902Sluigi
50492725Salfredtypedef	void poll_handler_t(struct ifnet *ifp, enum poll_cmd cmd, int count);
50592725Salfredint    ether_poll_register(poll_handler_t *h, struct ifnet *ifp);
50692725Salfredint    ether_poll_deregister(struct ifnet *ifp);
50787902Sluigi#endif /* DEVICE_POLLING */
50887902Sluigi
50955205Speter#endif /* _KERNEL */
51021259Swollman
51121259Swollman#endif /* !_NET_IF_VAR_H_ */
512