1 /**	$MirOS: src/sys/net/if.h,v 1.6 2011/11/08 22:57:43 bsiegert Exp $ */
2 /*	$OpenBSD: if.h,v 1.76 2005/06/14 04:00:39 henning Exp $	*/
3 /*	$NetBSD: if.h,v 1.23 1996/05/07 02:40:27 thorpej Exp $	*/
4 
5 /*
6  * Copyright (c) 1982, 1986, 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if.h	8.1 (Berkeley) 6/10/93
34  */
35 
36 #ifndef _NET_IF_H_
37 #define _NET_IF_H_
38 
39 #include <sys/queue.h>
40 
41 /*
42  * Always include ALTQ glue here -- we use the ALTQ interface queue
43  * structure even when ALTQ is not configured into the kernel so that
44  * the size of struct ifnet does not changed based on the option.  The
45  * ALTQ queue structure is API-compatible with the legacy ifqueue.
46  */
47 #include <altq/if_altq.h>
48 
49 /*
50  * Structures defining a network interface, providing a packet
51  * transport mechanism (ala level 0 of the PUP protocols).
52  *
53  * Each interface accepts output datagrams of a specified maximum
54  * length, and provides higher level routines with input datagrams
55  * received from its medium.
56  *
57  * Output occurs when the routine if_output is called, with four parameters:
58  *	(*ifp->if_output)(ifp, m, dst, rt)
59  * Here m is the mbuf chain to be sent and dst is the destination address.
60  * The output routine encapsulates the supplied datagram if necessary,
61  * and then transmits it on its medium.
62  *
63  * On input, each interface unwraps the data received by it, and either
64  * places it on the input queue of a internetwork datagram routine
65  * and posts the associated software interrupt, or passes the datagram to a raw
66  * packet input routine.
67  *
68  * Routines exist for locating interfaces by their addresses
69  * or for locating a interface on a certain network, as well as more general
70  * routing and gateway routines maintaining information used to locate
71  * interfaces.  These routines live in the files if.c and route.c
72  */
73 /*  XXX fast fix for SNMP, going away soon */
74 #include <sys/time.h>
75 
76 struct mbuf;
77 struct proc;
78 struct rtentry;
79 struct socket;
80 struct ether_header;
81 struct arpcom;
82 struct rt_addrinfo;
83 
84 /*
85  * Structure describing a `cloning' interface.
86  */
87 struct if_clone {
88 	LIST_ENTRY(if_clone) ifc_list;	/* on list of cloners */
89 	const char *ifc_name;		/* name of device, e.g. `gif' */
90 	size_t ifc_namelen;		/* length of name */
91 
92 	int	(*ifc_create)(struct if_clone *, int);
93 	int	(*ifc_destroy)(struct ifnet *);
94 };
95 
96 #define	IF_CLONE_INITIALIZER(name, create, destroy)			\
97 	{ { 0 }, name, sizeof(name) - 1, create, destroy }
98 
99 /*
100  * Structure used to query names of interface cloners.
101  */
102 struct if_clonereq {
103 	int	ifcr_total;		/* total cloners (out) */
104 	int	ifcr_count;		/* room for this many in user buffer */
105 	char	*ifcr_buffer;		/* buffer for cloner names */
106 };
107 
108 /*
109  * Structure defining statistics and other data kept regarding a network
110  * interface.
111  */
112 struct	if_data {
113 	/* generic interface information */
114 	u_char	ifi_type;		/* ethernet, tokenring, etc. */
115 	u_char	ifi_addrlen;		/* media address length */
116 	u_char	ifi_hdrlen;		/* media header length */
117 	u_char	ifi_link_state;		/* current link state */
118 	u_long	ifi_mtu;		/* maximum transmission unit */
119 	u_long	ifi_metric;		/* routing metric (external only) */
120 	u_long	ifi_baudrate;		/* linespeed */
121 	/* volatile statistics */
122 	u_long	ifi_ipackets;		/* packets received on interface */
123 	u_long	ifi_ierrors;		/* input errors on interface */
124 	u_long	ifi_opackets;		/* packets sent on interface */
125 	u_long	ifi_oerrors;		/* output errors on interface */
126 	u_long	ifi_collisions;		/* collisions on csma interfaces */
127 	u_long	ifi_ibytes;		/* total number of octets received */
128 	u_long	ifi_obytes;		/* total number of octets sent */
129 	u_long	ifi_imcasts;		/* packets received via multicast */
130 	u_long	ifi_omcasts;		/* packets sent via multicast */
131 	u_long	ifi_iqdrops;		/* dropped on input, this interface */
132 	u_long	ifi_noproto;		/* destined for unsupported protocol */
133 	struct	timeval ifi_lastchange;	/* last operational state change */
134 };
135 
136 /*
137  * Structure defining a queue for a network interface.
138  */
139 struct	ifqueue {
140 	struct	mbuf *ifq_head;
141 	struct	mbuf *ifq_tail;
142 	int	ifq_len;
143 	int	ifq_maxlen;
144 	int	ifq_drops;
145 	struct	timeout *ifq_congestion;
146 };
147 
148 /*
149  * Values for if_link_state.
150  */
151 #define	LINK_STATE_UNKNOWN	0	/* link invalid/unknown */
152 #define	LINK_STATE_DOWN		1	/* link is down */
153 #define	LINK_STATE_UP		2	/* link is up */
154 
155 /*
156  * Structure defining a queue for a network interface.
157  *
158  * (Would like to call this struct ``if'', but C isn't PL/1.)
159  */
160 TAILQ_HEAD(ifnet_head, ifnet);		/* the actual queue head */
161 
162 /*
163  * Length of interface external name, including terminating '\0'.
164  * Note: this is the same size as a generic device's external name.
165  */
166 #define	IFNAMSIZ	16
167 #define	IF_NAMESIZE	IFNAMSIZ
168 
169 /*
170  * Length of interface description, including terminating '\0'.
171  */
172 #define	IFDESCRSIZE	2
173 
174 struct ifnet {				/* and the entries */
175 	void	*if_softc;		/* lower-level data for this if */
176 	TAILQ_ENTRY(ifnet) if_list;	/* all struct ifnets are chained */
177 	TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */
178 	struct hook_desc_head *if_addrhooks; /* address change callbacks */
179 	struct hook_desc_head *if_linkstatehooks; /* link change callbacks */
180 	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
181 	int	if_pcount;		/* number of promiscuous listeners */
182 	caddr_t	if_bpf;			/* packet filter structure */
183 	caddr_t	if_bridge;		/* bridge structure */
184 	caddr_t	if_tp;			/* used by trunk ports */
185 	caddr_t	if_pf_kif;		/* pf interface abstraction */
186 	union {
187 		caddr_t	carp_s;		/* carp structure (used by !carp ifs) */
188 		struct ifnet *carp_d;	/* ptr to carpdev (used by carp ifs) */
189 	} if_carp_ptr;
190 #define if_carp		if_carp_ptr.carp_s
191 #define if_carpdev	if_carp_ptr.carp_d
192 	u_short	if_index;		/* numeric abbreviation for this if */
193 	short	if_timer;		/* time 'til if_watchdog called */
194 	short	if_flags;		/* up/down, broadcast, etc. */
195 	struct	if_data if_data;	/* stats and other data about if */
196 	int	if_capabilities;	/* interface capabilities */
197 	char	if_description[IFDESCRSIZE]; /* interface description */
198 
199 	/* procedure handles */
200 					/* output routine (enqueue) */
201 	int	(*if_output)(struct ifnet *, struct mbuf *, struct sockaddr *,
202 		     struct rtentry *);
203 					/* initiate output routine */
204 	void	(*if_start)(struct ifnet *);
205 					/* ioctl routine */
206 	int	(*if_ioctl)(struct ifnet *, u_long, caddr_t);
207 					/* init routine */
208 	int	(*if_init)(struct ifnet *);
209 					/* XXX bus reset routine */
210 	int	(*if_reset)(struct ifnet *);
211 					/* timer routine */
212 	void	(*if_watchdog)(struct ifnet *);
213 	struct	ifaltq if_snd;		/* output queue (includes altq) */
214 	struct sockaddr_dl *if_sadl;	/* pointer to our sockaddr_dl */
215 
216 	void	*if_afdata[AF_MAX];
217 };
218 #define	if_mtu		if_data.ifi_mtu
219 #define	if_type		if_data.ifi_type
220 #define	if_addrlen	if_data.ifi_addrlen
221 #define	if_hdrlen	if_data.ifi_hdrlen
222 #define	if_metric	if_data.ifi_metric
223 #define	if_link_state	if_data.ifi_link_state
224 #define	if_baudrate	if_data.ifi_baudrate
225 #define	if_ipackets	if_data.ifi_ipackets
226 #define	if_ierrors	if_data.ifi_ierrors
227 #define	if_opackets	if_data.ifi_opackets
228 #define	if_oerrors	if_data.ifi_oerrors
229 #define	if_collisions	if_data.ifi_collisions
230 #define	if_ibytes	if_data.ifi_ibytes
231 #define	if_obytes	if_data.ifi_obytes
232 #define	if_imcasts	if_data.ifi_imcasts
233 #define	if_omcasts	if_data.ifi_omcasts
234 #define	if_iqdrops	if_data.ifi_iqdrops
235 #define	if_noproto	if_data.ifi_noproto
236 #define	if_lastchange	if_data.ifi_lastchange
237 
238 #define	IFF_UP		0x1		/* interface is up */
239 #define	IFF_BROADCAST	0x2		/* broadcast address valid */
240 #define	IFF_DEBUG	0x4		/* turn on debugging */
241 #define	IFF_LOOPBACK	0x8		/* is a loopback net */
242 #define	IFF_POINTOPOINT	0x10		/* interface is point-to-point link */
243 #define	IFF_NOTRAILERS	0x20		/* avoid use of trailers */
244 #define	IFF_RUNNING	0x40		/* resources allocated */
245 #define	IFF_NOARP	0x80		/* no address resolution protocol */
246 #define	IFF_PROMISC	0x100		/* receive all packets */
247 #define	IFF_ALLMULTI	0x200		/* receive all multicast packets */
248 #define	IFF_OACTIVE	0x400		/* transmission in progress */
249 #define	IFF_SIMPLEX	0x800		/* can't hear own transmissions */
250 #define	IFF_LINK0	0x1000		/* per link layer defined bit */
251 #define	IFF_LINK1	0x2000		/* per link layer defined bit */
252 #define	IFF_LINK2	0x4000		/* per link layer defined bit */
253 #define	IFF_MULTICAST	0x8000		/* supports multicast */
254 
255 /* flags set internally only: */
256 #define	IFF_CANTCHANGE \
257 	(IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\
258 	    IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI)
259 
260 /*
261  * Some convenience macros used for setting ifi_baudrate.
262  */
263 #define	IF_Kbps(x)	((x) * 1000ULL)			/* kilobits/sec. */
264 #define	IF_Mbps(x)	(IF_Kbps((x) * 1000ULL))	/* megabits/sec. */
265 #define	IF_Gbps(x)	(IF_Mbps((x) * 1000ULL))	/* gigabits/sec. */
266 
267 /* Capabilities that interfaces can advertise. */
268 #define	IFCAP_CSUM_IPv4		0x00000001	/* can do IPv4 header csum */
269 #define	IFCAP_CSUM_TCPv4	0x00000002	/* can do IPv4/TCP csum */
270 #define	IFCAP_CSUM_UDPv4	0x00000004	/* can do IPv4/UDP csum */
271 #define	IFCAP_IPSEC		0x00000008	/* can do IPsec */
272 #define	IFCAP_VLAN_MTU		0x00000010	/* VLAN-compatible MTU */
273 #define	IFCAP_VLAN_HWTAGGING	0x00000020	/* hardware VLAN tag support */
274 #define	IFCAP_IPCOMP		0x00000040	/* can do IPcomp */
275 #define	IFCAP_JUMBO_MTU		0x00000080	/* 9000 byte MTU supported */
276 #define	IFCAP_CSUM_TCPv6	0x00000100	/* can do IPv6/TCP checksums */
277 #define	IFCAP_CSUM_UDPv6	0x00000200	/* can do IPv6/UDP checksums */
278 #define	IFCAP_CSUM_TCPv4_Rx	0x00000400	/* can do IPv4/TCP (Rx only) */
279 #define	IFCAP_CSUM_UDPv4_Rx	0x00000800	/* can do IPv4/UDP (Rx only) */
280 
281 /*
282  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
283  * input routines have queues of messages stored on ifqueue structures
284  * (defined above).  Entries are added to and deleted from these structures
285  * by these macros, which should be called with ipl raised to splimp().
286  */
287 #define	IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
288 #define	IF_DROP(ifq)		((ifq)->ifq_drops++)
289 #define	IF_ENQUEUE(ifq, m) { \
290 	(m)->m_nextpkt = 0; \
291 	if ((ifq)->ifq_tail == 0) \
292 		(ifq)->ifq_head = m; \
293 	else \
294 		(ifq)->ifq_tail->m_nextpkt = m; \
295 	(ifq)->ifq_tail = m; \
296 	(ifq)->ifq_len++; \
297 }
298 #define	IF_PREPEND(ifq, m) { \
299 	(m)->m_nextpkt = (ifq)->ifq_head; \
300 	if ((ifq)->ifq_tail == 0) \
301 		(ifq)->ifq_tail = (m); \
302 	(ifq)->ifq_head = (m); \
303 	(ifq)->ifq_len++; \
304 }
305 #define	IF_DEQUEUE(ifq, m) { \
306 	(m) = (ifq)->ifq_head; \
307 	if (m) { \
308 		if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \
309 			(ifq)->ifq_tail = 0; \
310 		(m)->m_nextpkt = 0; \
311 		(ifq)->ifq_len--; \
312 	} \
313 }
314 
315 #define	IF_INPUT_ENQUEUE(ifq, m) {			\
316 	if (IF_QFULL(ifq)) {				\
317 		IF_DROP(ifq);				\
318 		m_freem(m);				\
319 		if (!(ifq)->ifq_congestion)		\
320 			if_congestion(ifq);		\
321 	} else						\
322 		IF_ENQUEUE(ifq, m);			\
323 }
324 
325 #define	IF_POLL(ifq, m)		((m) = (ifq)->ifq_head)
326 #define	IF_PURGE(ifq)							\
327 do {									\
328 	struct mbuf *__m0;						\
329 									\
330 	for (;;) {							\
331 		IF_DEQUEUE((ifq), __m0);				\
332 		if (__m0 == NULL)					\
333 			break;						\
334 		else							\
335 			m_freem(__m0);					\
336 	}								\
337 } while (0)
338 #define	IF_IS_EMPTY(ifq)	((ifq)->ifq_len == 0)
339 
340 #define	IFQ_MAXLEN	50
341 #define	IFNET_SLOWHZ	1		/* granularity is 1 second */
342 
343 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */
344 #define IFQCTL_LEN 1
345 #define IFQCTL_MAXLEN 2
346 #define IFQCTL_DROPS 3
347 #define IFQCTL_CONGESTION 4
348 #define IFQCTL_MAXID 5
349 
350 /* sysctl for ifq (per-protocol packet input queue variant of ifqueue) */
351 #define CTL_IFQ_NAMES  { \
352 	{ 0, 0 }, \
353 	{ "len", CTLTYPE_INT }, \
354 	{ "maxlen", CTLTYPE_INT }, \
355 	{ "drops", CTLTYPE_INT }, \
356 	{ "congestion", CTLTYPE_INT }, \
357 }
358 
359 /*
360  * The ifaddr structure contains information about one address
361  * of an interface.  They are maintained by the different address families,
362  * are allocated and attached when an address is set, and are linked
363  * together so all addresses for an interface can be located.
364  */
365 struct ifaddr {
366 	struct	sockaddr *ifa_addr;	/* address of interface */
367 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
368 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
369 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
370 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
371 	TAILQ_ENTRY(ifaddr) ifa_list;	/* list of addresses for interface */
372 					/* check or clean routes (+ or -)'d */
373 	void	(*ifa_rtrequest)(int, struct rtentry *, struct rt_addrinfo *);
374 	u_int	ifa_flags;		/* mostly rt_flags for cloning */
375 	u_int	ifa_refcnt;		/* count of references */
376 	int	ifa_metric;		/* cost of going out this interface */
377 };
378 #define	IFA_ROUTE	RTF_UP		/* route installed */
379 
380 /*
381  * Message format for use in obtaining information about interfaces
382  * from sysctl and the routing socket.
383  */
384 struct if_msghdr {
385 	u_short	ifm_msglen;	/* to skip over non-understood messages */
386 	u_char	ifm_version;	/* future binary compatibility */
387 	u_char	ifm_type;	/* message type */
388 	int	ifm_addrs;	/* like rtm_addrs */
389 	int	ifm_flags;	/* value of if_flags */
390 	u_short	ifm_index;	/* index for associated ifp */
391 	struct	if_data ifm_data;/* statistics and other data about if */
392 };
393 
394 /*
395  * Message format for use in obtaining information about interface addresses
396  * from sysctl and the routing socket.
397  */
398 struct ifa_msghdr {
399 	u_short	ifam_msglen;	/* to skip over non-understood messages */
400 	u_char	ifam_version;	/* future binary compatibility */
401 	u_char	ifam_type;	/* message type */
402 	int	ifam_addrs;	/* like rtm_addrs */
403 	int	ifam_flags;	/* value of ifa_flags */
404 	u_short	ifam_index;	/* index for associated ifp */
405 	int	ifam_metric;	/* value of ifa_metric */
406 };
407 
408 
409 /*
410  * Message format announcing the arrival or departure of a network interface.
411  */
412 struct if_announcemsghdr {
413 	u_short	ifan_msglen;	/* to skip over non-understood messages */
414 	u_char	ifan_version;	/* future binary compatibility */
415 	u_char	ifan_type;	/* message type */
416 	u_short	ifan_index;	/* index for associated ifp */
417 	char	ifan_name[IFNAMSIZ];	/* if name, e.g. "en0" */
418 	u_short	ifan_what;	/* what type of announcement */
419 };
420 
421 #define IFAN_ARRIVAL	0	/* interface arrival */
422 #define IFAN_DEPARTURE	1	/* interface departure */
423 
424 /*
425  * Interface request structure used for socket
426  * ioctl's.  All interface ioctl's must have parameter
427  * definitions which begin with ifr_name.  The
428  * remainder may be interface specific.
429  */
430 struct	ifreq {
431 	char	ifr_name[IFNAMSIZ];		/* if name, e.g. "en0" */
432 	union {
433 		struct	sockaddr ifru_addr;
434 		struct	sockaddr ifru_dstaddr;
435 		struct	sockaddr ifru_broadaddr;
436 		short	ifru_flags;
437 		int	ifru_metric;
438 		caddr_t	ifru_data;
439 	} ifr_ifru;
440 #define	ifr_addr	ifr_ifru.ifru_addr	/* address */
441 #define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-to-p link */
442 #define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address */
443 #define	ifr_flags	ifr_ifru.ifru_flags	/* flags */
444 #define	ifr_metric	ifr_ifru.ifru_metric	/* metric */
445 #define	ifr_mtu		ifr_ifru.ifru_metric	/* mtu (overload) */
446 #define	ifr_media	ifr_ifru.ifru_metric	/* media options (overload) */
447 #define	ifr_data	ifr_ifru.ifru_data	/* for use by interface */
448 };
449 
450 struct ifaliasreq {
451 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
452 	struct	sockaddr ifra_addr;
453 	struct	sockaddr ifra_dstaddr;
454 #define	ifra_broadaddr	ifra_dstaddr
455 	struct	sockaddr ifra_mask;
456 };
457 
458 struct ifmediareq {
459 	char	ifm_name[IFNAMSIZ];		/* if name, e.g. "en0" */
460 	int	ifm_current;			/* current media options */
461 	int	ifm_mask;			/* don't care mask */
462 	int	ifm_status;			/* media status */
463 	int	ifm_active;			/* active options */
464 	int	ifm_count;			/* # entries in ifm_ulist
465 							array */
466 	int	*ifm_ulist;			/* media words */
467 };
468 
469 /*
470  * Structure used in SIOCGIFCONF request.
471  * Used to retrieve interface configuration
472  * for machine (useful for programs which
473  * must know all networks accessible).
474  */
475 struct	ifconf {
476 	int	ifc_len;		/* size of associated buffer */
477 	union {
478 		caddr_t	ifcu_buf;
479 		struct	ifreq *ifcu_req;
480 	} ifc_ifcu;
481 #define	ifc_buf	ifc_ifcu.ifcu_buf	/* buffer address */
482 #define	ifc_req	ifc_ifcu.ifcu_req	/* array of structures returned */
483 };
484 
485 /*
486  * Structure for SIOC[AGD]LIFADDR
487  */
488 struct if_laddrreq {
489 	char iflr_name[IFNAMSIZ];
490 	unsigned int flags;
491 #define IFLR_PREFIX	0x8000	/* in: prefix given  out: kernel fills id */
492 	unsigned int prefixlen;		/* in/out */
493 	struct sockaddr_storage addr;	/* in/out */
494 	struct sockaddr_storage dstaddr; /* out */
495 };
496 
497 struct if_nameindex {
498 	unsigned int	if_index;
499 	char		*if_name;
500 };
501 
502 #ifndef _KERNEL
503 __BEGIN_DECLS
504 unsigned int if_nametoindex(const char *);
505 char	*if_indextoname(unsigned int, char *);
506 struct	if_nameindex *if_nameindex(void);
507 __END_DECLS
508 #define if_freenameindex(x)	free(x)
509 #endif
510 
511 #include <net/if_arp.h>
512 
513 #ifdef _KERNEL
514 #define	IFAFREE(ifa) \
515 do { \
516 	if ((ifa)->ifa_refcnt <= 0) \
517 		ifafree(ifa); \
518 	else \
519 		(ifa)->ifa_refcnt--; \
520 } while (0)
521 
522 #ifdef ALTQ
523 #define	ALTQ_DECL(x)		x
524 
525 #define	IFQ_ENQUEUE(ifq, m, pattr, err)					\
526 do {									\
527 	if (ALTQ_IS_ENABLED((ifq)))					\
528 		ALTQ_ENQUEUE((ifq), (m), (pattr), (err));		\
529 	else {								\
530 		if (IF_QFULL((ifq))) {					\
531 			m_freem((m));					\
532 			(err) = ENOBUFS;				\
533 		} else {						\
534 			IF_ENQUEUE((ifq), (m));				\
535 			(err) = 0;					\
536 		}							\
537 	}								\
538 	if ((err))							\
539 		(ifq)->ifq_drops++;					\
540 } while (0)
541 
542 #define	IFQ_DEQUEUE(ifq, m)						\
543 do {									\
544 	if (TBR_IS_ENABLED((ifq)))					\
545 		(m) = tbr_dequeue((ifq), ALTDQ_REMOVE);			\
546 	else if (ALTQ_IS_ENABLED((ifq)))				\
547 		ALTQ_DEQUEUE((ifq), (m));				\
548 	else								\
549 		IF_DEQUEUE((ifq), (m));					\
550 } while (0)
551 
552 #define	IFQ_POLL(ifq, m)						\
553 do {									\
554 	if (TBR_IS_ENABLED((ifq)))					\
555 		(m) = tbr_dequeue((ifq), ALTDQ_POLL);			\
556 	else if (ALTQ_IS_ENABLED((ifq)))				\
557 		ALTQ_POLL((ifq), (m));					\
558 	else								\
559 		IF_POLL((ifq), (m));					\
560 } while (0)
561 
562 #define	IFQ_PURGE(ifq)							\
563 do {									\
564 	if (ALTQ_IS_ENABLED((ifq)))					\
565 		ALTQ_PURGE((ifq));					\
566 	else								\
567 		IF_PURGE((ifq));					\
568 } while (0)
569 
570 #define	IFQ_SET_READY(ifq)						\
571 	do { ((ifq)->altq_flags |= ALTQF_READY); } while (0)
572 
573 #define	IFQ_CLASSIFY(ifq, m, af, pa)					\
574 do {									\
575 	if (ALTQ_IS_ENABLED((ifq))) {					\
576 		if (ALTQ_NEEDS_CLASSIFY((ifq)))				\
577 			(pa)->pattr_class = (*(ifq)->altq_classify)	\
578 				((ifq)->altq_clfier, (m), (af));	\
579 		(pa)->pattr_af = (af);					\
580 		(pa)->pattr_hdr = mtod((m), caddr_t);			\
581 	}								\
582 } while (0)
583 
584 #else /* !ALTQ */
585 #define	ALTQ_DECL(x)		/* nothing */
586 
587 #define	IFQ_ENQUEUE(ifq, m, pattr, err)					\
588 do {									\
589 	if (IF_QFULL((ifq))) {						\
590 		m_freem((m));						\
591 		(err) = ENOBUFS;					\
592 	} else {							\
593 		IF_ENQUEUE((ifq), (m));					\
594 		(err) = 0;						\
595 	}								\
596 	if ((err))							\
597 		(ifq)->ifq_drops++;					\
598 } while (0)
599 
600 #define	IFQ_DEQUEUE(ifq, m)	IF_DEQUEUE((ifq), (m))
601 
602 #define	IFQ_POLL(ifq, m)	IF_POLL((ifq), (m))
603 
604 #define	IFQ_PURGE(ifq)		IF_PURGE((ifq))
605 
606 #define	IFQ_SET_READY(ifq)	/* nothing */
607 
608 #define	IFQ_CLASSIFY(ifq, m, af, pa) /* nothing */
609 
610 #endif /* ALTQ */
611 
612 #define	IFQ_IS_EMPTY(ifq)		((ifq)->ifq_len == 0)
613 #define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
614 #define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
615 #define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
616 #define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
617 
618 extern struct ifnet_head ifnet;
619 extern struct ifnet **ifindex2ifnet;
620 extern struct ifnet *lo0ifp;
621 extern int if_indexlim;
622 
623 void	ether_ifattach(struct ifnet *);
624 void	ether_ifdetach(struct ifnet *);
625 int	ether_ioctl(struct ifnet *, struct arpcom *, u_long, caddr_t);
626 void	ether_input_mbuf(struct ifnet *, struct mbuf *);
627 void	ether_input(struct ifnet *, struct ether_header *, struct mbuf *);
628 int	ether_output(struct ifnet *,
629 	    struct mbuf *, struct sockaddr *, struct rtentry *);
630 char	*ether_sprintf(u_char *);
631 
632 void	if_alloc_sadl(struct ifnet *);
633 void	if_free_sadl(struct ifnet *);
634 void	if_attach(struct ifnet *);
635 void	if_attachdomain(void);
636 void	if_attachtail(struct ifnet *);
637 void	if_attachhead(struct ifnet *);
638 void	if_detach(struct ifnet *);
639 void	if_down(struct ifnet *);
640 void	if_link_state_change(struct ifnet *);
641 void	if_qflush(struct ifqueue *);
642 void	if_slowtimo(void *);
643 void	if_up(struct ifnet *);
644 int	ifconf(u_long, caddr_t);
645 void	ifinit(void);
646 int	ifioctl(struct socket *, u_long, caddr_t, struct proc *);
647 int	ifpromisc(struct ifnet *, int);
648 struct	ifnet *ifunit(const char *);
649 
650 struct	ifaddr *ifa_ifwithaddr(struct sockaddr *);
651 struct	ifaddr *ifa_ifwithaf(int);
652 struct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
653 struct	ifaddr *ifa_ifwithnet(struct sockaddr *);
654 struct	ifaddr *ifa_ifwithroute(int, struct sockaddr *,
655 					struct sockaddr *);
656 struct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
657 void	ifafree(struct ifaddr *);
658 void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
659 
660 void	if_clone_attach(struct if_clone *);
661 void	if_clone_detach(struct if_clone *);
662 
663 int	if_clone_create(const char *);
664 int	if_clone_destroy(const char *);
665 
666 void	if_congestion(struct ifqueue *);
667 
668 int	loioctl(struct ifnet *, u_long, caddr_t);
669 void	loopattach(int);
670 int	looutput(struct ifnet *,
671 	    struct mbuf *, struct sockaddr *, struct rtentry *);
672 void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
673 #endif /* _KERNEL */
674 #endif /* _NET_IF_H_ */
675