1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	From: @(#)if.h	8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/net/if_var.h,v 1.98.2.6 2006/10/06 20:26:05 andre Exp $
31 */
32
33#ifndef	_FBSD_COMPAT_NET_IF_VAR_H_
34#define	_FBSD_COMPAT_NET_IF_VAR_H_
35
36/*
37 * Structures defining a network interface, providing a packet
38 * transport mechanism (ala level 0 of the PUP protocols).
39 *
40 * Each interface accepts output datagrams of a specified maximum
41 * length, and provides higher level routines with input datagrams
42 * received from its medium.
43 *
44 * Output occurs when the routine if_output is called, with three parameters:
45 *	(*ifp->if_output)(ifp, m, dst, rt)
46 * Here m is the mbuf chain to be sent and dst is the destination address.
47 * The output routine encapsulates the supplied datagram if necessary,
48 * and then transmits it on its medium.
49 *
50 * On input, each interface unwraps the data received by it, and either
51 * places it on the input queue of an internetwork datagram routine
52 * and posts the associated software interrupt, or passes the datagram to a raw
53 * packet input routine.
54 *
55 * Routines exist for locating interfaces by their addresses
56 * or for locating an interface on a certain network, as well as more general
57 * routing and gateway routines maintaining information used to locate
58 * interfaces.  These routines live in the files if.c and route.c
59 */
60
61#ifdef __STDC__
62/*
63 * Forward structure declarations for function prototypes [sic].
64 */
65struct	mbuf;
66struct	thread;
67struct	rtentry;
68struct	rt_addrinfo;
69struct	socket;
70struct	ether_header;
71struct	carp_if;
72struct	route;
73#endif
74
75#include <posix/net/if_dl.h>
76
77#include <sys/queue.h>		/* get TAILQ macros */
78
79#ifdef _KERNEL
80#include <sys/mbuf.h>
81#include <sys/eventhandler.h>
82#endif /* _KERNEL */
83#include <sys/counter.h>
84#include <sys/lock.h>		/* XXX */
85#include <sys/mutex.h>		/* XXX */
86#include <sys/event.h>		/* XXX */
87#include <sys/_task.h>
88
89#define	IF_DUNIT_NONE	-1
90
91#include <altq/if_altq.h>
92
93typedef enum {
94	IFCOUNTER_IPACKETS = 0,
95	IFCOUNTER_IERRORS,
96	IFCOUNTER_OPACKETS,
97	IFCOUNTER_OERRORS,
98	IFCOUNTER_COLLISIONS,
99	IFCOUNTER_IBYTES,
100	IFCOUNTER_OBYTES,
101	IFCOUNTER_IMCASTS,
102	IFCOUNTER_OMCASTS,
103	IFCOUNTER_IQDROPS,
104	IFCOUNTER_OQDROPS,
105	IFCOUNTER_NOPROTO,
106	IFCOUNTERS /* Array size. */
107} ift_counter;
108
109TAILQ_HEAD(ifnethead, ifnet);	/* we use TAILQs so that the order of */
110TAILQ_HEAD(ifaddrhead, ifaddr);	/* instantiation is preserved in the list */
111TAILQ_HEAD(ifprefixhead, ifprefix);
112TAILQ_HEAD(ifmultihead, ifmultiaddr);
113
114typedef struct ifnet * if_t;
115
116typedef	void (*if_start_fn_t)(if_t);
117typedef	int (*if_ioctl_fn_t)(if_t, u_long, caddr_t);
118typedef	void (*if_init_fn_t)(void *);
119typedef void (*if_qflush_fn_t)(if_t);
120typedef int (*if_transmit_fn_t)(if_t, struct mbuf *);
121typedef	uint64_t (*if_get_counter_t)(if_t, ift_counter);
122
123struct ifnet_hw_tsomax {
124	u_int	tsomaxbytes;	/* TSO total burst length limit in bytes */
125	u_int	tsomaxsegcount;	/* TSO maximum segment count */
126	u_int	tsomaxsegsize;	/* TSO maximum segment size in bytes */
127};
128
129/* Interface encap request types */
130typedef enum {
131	IFENCAP_LL = 1			/* pre-calculate link-layer header */
132} ife_type;
133
134/*
135 * The structure below allows to request various pre-calculated L2/L3 headers
136 * for different media. Requests varies by type (rtype field).
137 *
138 * IFENCAP_LL type: pre-calculates link header based on address family
139 *   and destination lladdr.
140 *
141 *   Input data fields:
142 *     buf: pointer to destination buffer
143 *     bufsize: buffer size
144 *     flags: IFENCAP_FLAG_BROADCAST if destination is broadcast
145 *     family: address family defined by AF_ constant.
146 *     lladdr: pointer to link-layer address
147 *     lladdr_len: length of link-layer address
148 *     hdata: pointer to L3 header (optional, used for ARP requests).
149 *   Output data fields:
150 *     buf: encap data is stored here
151 *     bufsize: resulting encap length is stored here
152 *     lladdr_off: offset of link-layer address from encap hdr start
153 *     hdata: L3 header may be altered if necessary
154 */
155
156struct if_encap_req {
157	u_char		*buf;		/* Destination buffer (w) */
158	size_t		bufsize;	/* size of provided buffer (r) */
159	ife_type	rtype;		/* request type (r) */
160	uint32_t	flags;		/* Request flags (r) */
161	int		family;		/* Address family AF_* (r) */
162	int		lladdr_off;	/* offset from header start (w) */
163	int		lladdr_len;	/* lladdr length (r) */
164	char		*lladdr;	/* link-level address pointer (r) */
165	char		*hdata;		/* Upper layer header data (rw) */
166};
167
168
169/*
170 * Structure defining a queue for a network interface.
171 */
172struct	ifqueue {
173	struct	mbuf *ifq_head;
174	struct	mbuf *ifq_tail;
175	int	ifq_len;
176	int	ifq_maxlen;
177	int	ifq_drops;
178	struct	mtx ifq_mtx;
179};
180
181struct device;
182
183/*
184 * Structure defining a network interface.
185 *
186 * (Would like to call this struct ``if'', but C isn't PL/1.)
187 */
188
189struct ifnet {
190	void	*if_softc;		/* pointer to driver state */
191	void	*if_l2com;		/* pointer to protocol bits */
192	TAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained */
193	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
194	const char *if_dname;		/* driver name */
195	int	if_dunit;		/* unit or IF_DUNIT_NONE */
196	struct	ifaddrhead if_addrhead;	/* linked list of addresses per if */
197		/*
198		 * if_addrhead is the list of all addresses associated to
199		 * an interface.
200		 * Some code in the kernel assumes that first element
201		 * of the list has type AF_LINK, and contains sockaddr_dl
202		 * addresses which store the link-level address and the name
203		 * of the interface.
204		 * However, access to the AF_LINK address through this
205		 * field is deprecated. Use ifaddr_byindex() instead.
206		 */
207	struct	knlist if_klist;	/* events attached to this if */
208	int	if_pcount;		/* number of promiscuous listeners */
209	struct	carp_if *if_carp;	/* carp interface structure */
210	struct	bpf_if *if_bpf;		/* packet filter structure */
211	u_short	if_index;		/* numeric abbreviation for this if  */
212	short	if_timer;		/* time 'til if_watchdog called */
213	struct  ifvlantrunk *if_vlantrunk; /* pointer to 802.1q data */
214	int	if_flags;		/* up/down, broadcast, etc. */
215	int	if_capabilities;	/* interface capabilities */
216	int	if_capenable;		/* enabled features */
217	void	*if_linkmib;		/* link-type-specific MIB data */
218	size_t	if_linkmiblen;		/* length of above data */
219	struct	if_data if_data;
220	struct	ifmultihead if_multiaddrs; /* multicast addresses configured */
221	int	if_amcount;		/* number of all-multicast requests */
222	struct	ifaddr	*if_addr;	/* pointer to link-level address */
223/* procedure handles */
224	int	(*if_output)		/* output routine (enqueue) */
225		(struct ifnet *, struct mbuf *, struct sockaddr *,
226		     struct route *);
227	void	(*if_input)		/* input routine (from h/w driver) */
228		(struct ifnet *, struct mbuf *);
229	void	(*if_start)		/* initiate output routine */
230		(struct ifnet *);
231	int	(*if_ioctl)		/* ioctl routine */
232		(struct ifnet *, u_long, caddr_t);
233	void	(*if_watchdog)		/* timer routine */
234		(struct ifnet *);
235	void	(*if_init)		/* Init routine */
236		(void *);
237	int	(*if_resolvemulti)	/* validate/resolve multicast */
238		(struct ifnet *, struct sockaddr **, struct sockaddr *);
239	int	(*if_transmit)		/* initiate output routine */
240		(struct ifnet *, struct mbuf *);
241	void	*if_spare1;		/* spare pointer 1 */
242	void	*if_spare2;		/* spare pointer 2 */
243	void	*if_spare3;		/* spare pointer 3 */
244	int	if_drv_flags;		/* driver-managed status flags */
245	u_int	if_spare_flags2;	/* spare flags 2 */
246	struct  ifaltq if_snd;		/* output queue (includes altq) */
247	const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */
248
249	void	*if_bridge;		/* bridge glue */
250
251	struct	lltable *lltables;	/* list of L3-L2 resolution tables */
252
253	struct	label *if_label;	/* interface MAC label */
254
255	/* these are only used by IPv6 */
256	struct	ifprefixhead if_prefixhead; /* list of prefixes per if */
257	void	*if_afdata[AF_MAX];
258	int	if_afdata_initialized;
259	struct	mtx if_afdata_mtx;
260	struct	task if_linktask;	/* task for link change events */
261	struct	mtx if_addr_mtx;	/* mutex to protect address lists */
262
263	if_qflush_fn_t	if_qflush;	/* flush any queue */
264	if_get_counter_t if_get_counter; /* get counter values */
265	int	(*if_requestencap)	/* make link header from request */
266		(struct ifnet *, struct if_encap_req *);
267
268	/*
269	 * Network adapter TSO limits:
270	 * ===========================
271	 *
272	 * If the "if_hw_tsomax" field is zero the maximum segment
273	 * length limit does not apply. If the "if_hw_tsomaxsegcount"
274	 * or the "if_hw_tsomaxsegsize" field is zero the TSO segment
275	 * count limit does not apply. If all three fields are zero,
276	 * there is no TSO limit.
277	 *
278	 * NOTE: The TSO limits should reflect the values used in the
279	 * BUSDMA tag a network adapter is using to load a mbuf chain
280	 * for transmission. The TCP/IP network stack will subtract
281	 * space for all linklevel and protocol level headers and
282	 * ensure that the full mbuf chain passed to the network
283	 * adapter fits within the given limits.
284	 */
285	u_int	if_hw_tsomax;		/* TSO maximum size in bytes */
286	u_int	if_hw_tsomaxsegcount;	/* TSO maximum segment count */
287	u_int	if_hw_tsomaxsegsize;	/* TSO maximum segment size in bytes */
288
289	/* Haiku additions */
290	struct sockaddr_dl	if_lladdr;
291	char				device_name[128];
292	struct device		*root_device;
293	struct ifqueue		receive_queue;
294	sem_id				receive_sem;
295	sem_id				link_state_sem;
296	int32				open_count;
297	int32				flags;
298
299	/* WLAN specific additions */
300	sem_id				scan_done_sem;
301};
302
303typedef void if_init_f_t(void *);
304
305/*
306 * XXX These aliases are terribly dangerous because they could apply
307 * to anything.
308 */
309#define	if_mtu		if_data.ifi_mtu
310#define	if_type		if_data.ifi_type
311#define if_physical	if_data.ifi_physical
312#define	if_addrlen	if_data.ifi_addrlen
313#define	if_hdrlen	if_data.ifi_hdrlen
314#define	if_metric	if_data.ifi_metric
315#define	if_link_state	if_data.ifi_link_state
316#define	if_baudrate	if_data.ifi_baudrate
317#define	if_hwassist	if_data.ifi_hwassist
318#define	if_ipackets	if_data.ifi_ipackets
319#define	if_ierrors	if_data.ifi_ierrors
320#define	if_opackets	if_data.ifi_opackets
321#define	if_oerrors	if_data.ifi_oerrors
322#define	if_collisions	if_data.ifi_collisions
323#define	if_ibytes	if_data.ifi_ibytes
324#define	if_obytes	if_data.ifi_obytes
325#define	if_imcasts	if_data.ifi_imcasts
326#define	if_omcasts	if_data.ifi_omcasts
327#define	if_iqdrops	if_data.ifi_iqdrops
328#define	if_oqdrops	if_data.ifi_oqdrops
329#define	if_noproto	if_data.ifi_noproto
330#define	if_lastchange	if_data.ifi_lastchange
331#define if_recvquota	if_data.ifi_recvquota
332#define	if_xmitquota	if_data.ifi_xmitquota
333#define if_rawoutput(if, m, sa) if_output(if, m, sa, (struct route *)NULL)
334
335/* for compatibility with other BSDs */
336#define	if_addrlist	if_addrhead
337#define	if_list		if_link
338
339/*
340 * Locks for address lists on the network interface.
341 */
342#define	IF_ADDR_LOCK_INIT(if)	mtx_init(&(if)->if_addr_mtx,		\
343				    "if_addr_mtx", NULL, MTX_DEF)
344#define	IF_ADDR_LOCK_DESTROY(if)	mtx_destroy(&(if)->if_addr_mtx)
345#define	IF_ADDR_LOCK(if)	mtx_lock(&(if)->if_addr_mtx)
346#define	IF_ADDR_UNLOCK(if)	mtx_unlock(&(if)->if_addr_mtx)
347#define	IF_ADDR_LOCK_ASSERT(if)	mtx_assert(&(if)->if_addr_mtx, MA_OWNED)
348
349void	if_addr_rlock(struct ifnet *ifp);	/* if_addrhead */
350void	if_addr_runlock(struct ifnet *ifp);	/* if_addrhead */
351void	if_maddr_rlock(struct ifnet *ifp);	/* if_multiaddrs */
352void	if_maddr_runlock(struct ifnet *ifp);	/* if_multiaddrs */
353
354/*
355 * Output queues (ifp->if_snd) and slow device input queues (*ifp->if_slowq)
356 * are queues of messages stored on ifqueue structures
357 * (defined above).  Entries are added to and deleted from these structures
358 * by these macros, which should be called with ipl raised to splimp().
359 */
360#define IF_LOCK(ifq)		mtx_lock(&(ifq)->ifq_mtx)
361#define IF_UNLOCK(ifq)		mtx_unlock(&(ifq)->ifq_mtx)
362#define	IF_LOCK_ASSERT(ifq)	mtx_assert(&(ifq)->ifq_mtx, MA_OWNED)
363#define	_IF_QFULL(ifq)		((ifq)->ifq_len >= (ifq)->ifq_maxlen)
364#define	_IF_DROP(ifq)		((ifq)->ifq_drops++)
365#define	_IF_QLEN(ifq)		((ifq)->ifq_len)
366
367#define	_IF_ENQUEUE(ifq, m) do { 				\
368	(m)->m_nextpkt = NULL;					\
369	if ((ifq)->ifq_tail == NULL) 				\
370		(ifq)->ifq_head = m; 				\
371	else 							\
372		(ifq)->ifq_tail->m_nextpkt = m; 		\
373	(ifq)->ifq_tail = m; 					\
374	(ifq)->ifq_len++; 					\
375} while (0)
376
377#define IF_ENQUEUE(ifq, m) do {					\
378	IF_LOCK(ifq); 						\
379	_IF_ENQUEUE(ifq, m); 					\
380	IF_UNLOCK(ifq); 					\
381} while (0)
382
383#define	_IF_PREPEND(ifq, m) do {				\
384	(m)->m_nextpkt = (ifq)->ifq_head; 			\
385	if ((ifq)->ifq_tail == NULL) 				\
386		(ifq)->ifq_tail = (m); 				\
387	(ifq)->ifq_head = (m); 					\
388	(ifq)->ifq_len++; 					\
389} while (0)
390
391#define IF_PREPEND(ifq, m) do {		 			\
392	IF_LOCK(ifq); 						\
393	_IF_PREPEND(ifq, m); 					\
394	IF_UNLOCK(ifq); 					\
395} while (0)
396
397#define	_IF_DEQUEUE(ifq, m) do { 				\
398	(m) = (ifq)->ifq_head; 					\
399	if (m) { 						\
400		if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL)	\
401			(ifq)->ifq_tail = NULL; 		\
402		(m)->m_nextpkt = NULL; 				\
403		(ifq)->ifq_len--; 				\
404	} 							\
405} while (0)
406
407#define IF_DEQUEUE(ifq, m) do { 				\
408	IF_LOCK(ifq); 						\
409	_IF_DEQUEUE(ifq, m); 					\
410	IF_UNLOCK(ifq); 					\
411} while (0)
412
413#define	_IF_POLL(ifq, m)	((m) = (ifq)->ifq_head)
414#define	IF_POLL(ifq, m)		_IF_POLL(ifq, m)
415
416#define _IF_DRAIN(ifq) do { 					\
417	struct mbuf *m; 					\
418	for (;;) { 						\
419		_IF_DEQUEUE(ifq, m); 				\
420		if (m == NULL) 					\
421			break; 					\
422		m_freem(m); 					\
423	} 							\
424} while (0)
425
426#define IF_DRAIN(ifq) do {					\
427	IF_LOCK(ifq);						\
428	_IF_DRAIN(ifq);						\
429	IF_UNLOCK(ifq);						\
430} while(0)
431
432#ifdef _KERNEL
433/* interface address change event */
434typedef void (*ifaddr_event_handler_t)(void *, struct ifnet *);
435EVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
436/* new interface arrival event */
437typedef void (*ifnet_arrival_event_handler_t)(void *, struct ifnet *);
438EVENTHANDLER_DECLARE(ifnet_arrival_event, ifnet_arrival_event_handler_t);
439/* interface departure event */
440typedef void (*ifnet_departure_event_handler_t)(void *, struct ifnet *);
441EVENTHANDLER_DECLARE(ifnet_departure_event, ifnet_departure_event_handler_t);
442
443#define	IF_AFDATA_LOCK_INIT(ifp)	\
444    mtx_init(&(ifp)->if_afdata_mtx, "if_afdata", NULL, MTX_DEF)
445#define	IF_AFDATA_LOCK(ifp)	mtx_lock(&(ifp)->if_afdata_mtx)
446#define	IF_AFDATA_TRYLOCK(ifp)	mtx_trylock(&(ifp)->if_afdata_mtx)
447#define	IF_AFDATA_UNLOCK(ifp)	mtx_unlock(&(ifp)->if_afdata_mtx)
448#define	IF_AFDATA_DESTROY(ifp)	mtx_destroy(&(ifp)->if_afdata_mtx)
449
450#define	IFF_LOCKGIANT(ifp) do {						\
451	if ((ifp)->if_flags & IFF_NEEDSGIANT)				\
452		mtx_lock(&Giant);					\
453} while (0)
454
455#define	IFF_UNLOCKGIANT(ifp) do {					\
456	if ((ifp)->if_flags & IFF_NEEDSGIANT)				\
457		mtx_unlock(&Giant);					\
458} while (0)
459
460int	if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp,
461	    int adjust);
462#define	IF_HANDOFF(ifq, m, ifp)			\
463	if_handoff((struct ifqueue *)ifq, m, ifp, 0)
464#define	IF_HANDOFF_ADJ(ifq, m, ifp, adj)	\
465	if_handoff((struct ifqueue *)ifq, m, ifp, adj)
466
467void	if_start(struct ifnet *);
468
469#define	IFQ_ENQUEUE(ifq, m, err)					\
470do {									\
471	IF_LOCK(ifq);							\
472	if (ALTQ_IS_ENABLED(ifq))					\
473		ALTQ_ENQUEUE(ifq, m, NULL, err);			\
474	else {								\
475		if (_IF_QFULL(ifq)) {					\
476			m_freem(m);					\
477			(err) = ENOBUFS;				\
478		} else {						\
479			_IF_ENQUEUE(ifq, m);				\
480			(err) = 0;					\
481		}							\
482	}								\
483	if (err)							\
484		(ifq)->ifq_drops++;					\
485	IF_UNLOCK(ifq);							\
486} while (0)
487
488#define	IFQ_DEQUEUE_NOLOCK(ifq, m)					\
489do {									\
490	if (TBR_IS_ENABLED(ifq))					\
491		(m) = tbr_dequeue_ptr(ifq, ALTDQ_REMOVE);		\
492	else if (ALTQ_IS_ENABLED(ifq))					\
493		ALTQ_DEQUEUE(ifq, m);					\
494	else								\
495		_IF_DEQUEUE(ifq, m);					\
496} while (0)
497
498#define	IFQ_DEQUEUE(ifq, m)						\
499do {									\
500	IF_LOCK(ifq);							\
501	IFQ_DEQUEUE_NOLOCK(ifq, m);					\
502	IF_UNLOCK(ifq);							\
503} while (0)
504
505#define	IFQ_POLL_NOLOCK(ifq, m)						\
506do {									\
507	if (TBR_IS_ENABLED(ifq))					\
508		(m) = tbr_dequeue_ptr(ifq, ALTDQ_POLL);			\
509	else if (ALTQ_IS_ENABLED(ifq))					\
510		ALTQ_POLL(ifq, m);					\
511	else								\
512		_IF_POLL(ifq, m);					\
513} while (0)
514
515#define	IFQ_POLL(ifq, m)						\
516do {									\
517	IF_LOCK(ifq);							\
518	IFQ_POLL_NOLOCK(ifq, m);					\
519	IF_UNLOCK(ifq);							\
520} while (0)
521
522#define	IFQ_PURGE_NOLOCK(ifq)						\
523do {									\
524	if (ALTQ_IS_ENABLED(ifq)) {					\
525		ALTQ_PURGE(ifq);					\
526	} else								\
527		_IF_DRAIN(ifq);						\
528} while (0)
529
530#define	IFQ_PURGE(ifq)							\
531do {									\
532	IF_LOCK(ifq);							\
533	IFQ_PURGE_NOLOCK(ifq);						\
534	IF_UNLOCK(ifq);							\
535} while (0)
536
537#define	IFQ_SET_READY(ifq)						\
538	do { ((ifq)->altq_flags |= ALTQF_READY); } while (0)
539
540#define	IFQ_LOCK(ifq)			IF_LOCK(ifq)
541#define	IFQ_UNLOCK(ifq)			IF_UNLOCK(ifq)
542#define	IFQ_LOCK_ASSERT(ifq)		IF_LOCK_ASSERT(ifq)
543#define	IFQ_IS_EMPTY(ifq)		((ifq)->ifq_len == 0)
544#define	IFQ_INC_LEN(ifq)		((ifq)->ifq_len++)
545#define	IFQ_DEC_LEN(ifq)		(--(ifq)->ifq_len)
546#define	IFQ_INC_DROPS(ifq)		((ifq)->ifq_drops++)
547#define	IFQ_SET_MAXLEN(ifq, len)	((ifq)->ifq_maxlen = (len))
548
549/*
550 * The IFF_DRV_OACTIVE test should really occur in the device driver, not in
551 * the handoff logic, as that flag is locked by the device driver.
552 */
553#define	IFQ_HANDOFF_ADJ(ifp, m, adj, err)				\
554do {									\
555	int len;							\
556	short mflags;							\
557									\
558	len = (m)->m_pkthdr.len;					\
559	mflags = (m)->m_flags;						\
560	IFQ_ENQUEUE(&(ifp)->if_snd, m, err);				\
561	if ((err) == 0) {						\
562		(ifp)->if_obytes += len + (adj);			\
563		if (mflags & M_MCAST)					\
564			(ifp)->if_omcasts++;				\
565		if (((ifp)->if_drv_flags & IFF_DRV_OACTIVE) == 0)	\
566			if_start(ifp);					\
567	}								\
568} while (0)
569
570#define	IFQ_HANDOFF(ifp, m, err)					\
571	IFQ_HANDOFF_ADJ(ifp, m, 0, err)
572
573#define	IFQ_DRV_DEQUEUE(ifq, m)						\
574do {									\
575	(m) = (ifq)->ifq_drv_head;					\
576	if (m) {							\
577		if (((ifq)->ifq_drv_head = (m)->m_nextpkt) == NULL)	\
578			(ifq)->ifq_drv_tail = NULL;			\
579		(m)->m_nextpkt = NULL;					\
580		(ifq)->ifq_drv_len--;					\
581	} else {							\
582		IFQ_LOCK(ifq);						\
583		IFQ_DEQUEUE_NOLOCK(ifq, m);				\
584		while ((ifq)->ifq_drv_len < (ifq)->ifq_drv_maxlen) {	\
585			struct mbuf *m0;				\
586			IFQ_DEQUEUE_NOLOCK(ifq, m0);			\
587			if (m0 == NULL)					\
588				break;					\
589			m0->m_nextpkt = NULL;				\
590			if ((ifq)->ifq_drv_tail == NULL)		\
591				(ifq)->ifq_drv_head = m0;		\
592			else						\
593				(ifq)->ifq_drv_tail->m_nextpkt = m0;	\
594			(ifq)->ifq_drv_tail = m0;			\
595			(ifq)->ifq_drv_len++;				\
596		}							\
597		IFQ_UNLOCK(ifq);					\
598	}								\
599} while (0)
600
601#define	IFQ_DRV_PREPEND(ifq, m)						\
602do {									\
603	(m)->m_nextpkt = (ifq)->ifq_drv_head;				\
604	if ((ifq)->ifq_drv_tail == NULL)				\
605		(ifq)->ifq_drv_tail = (m);				\
606	(ifq)->ifq_drv_head = (m);					\
607	(ifq)->ifq_drv_len++;						\
608} while (0)
609
610#define	IFQ_DRV_IS_EMPTY(ifq)						\
611	(((ifq)->ifq_drv_len == 0) && ((ifq)->ifq_len == 0))
612
613#define	IFQ_DRV_PURGE(ifq)						\
614do {									\
615	struct mbuf *m, *n = (ifq)->ifq_drv_head;			\
616	while((m = n) != NULL) {					\
617		n = m->m_nextpkt;					\
618		m_freem(m);						\
619	}								\
620	(ifq)->ifq_drv_head = (ifq)->ifq_drv_tail = NULL;		\
621	(ifq)->ifq_drv_len = 0;						\
622	IFQ_PURGE(ifq);							\
623} while (0)
624
625/*
626 * 72 was chosen below because it is the size of a TCP/IP
627 * header (40) + the minimum mss (32).
628 */
629#define	IF_MINMTU	72
630#define	IF_MAXMTU	65535
631
632#endif /* _KERNEL */
633
634/*
635 * The ifaddr structure contains information about one address
636 * of an interface.  They are maintained by the different address families,
637 * are allocated and attached when an address is set, and are linked
638 * together so all addresses for an interface can be located.
639 *
640 * NOTE: a 'struct ifaddr' is always at the beginning of a larger
641 * chunk of malloc'ed memory, where we store the three addresses
642 * (ifa_addr, ifa_dstaddr and ifa_netmask) referenced here.
643 */
644struct ifaddr {
645	struct	sockaddr *ifa_addr;	/* address of interface */
646	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
647#define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
648	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
649	struct	if_data if_data;	/* not all members are meaningful */
650	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
651	TAILQ_ENTRY(ifaddr) ifa_link;	/* queue macro glue */
652	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
653		(int, struct rtentry *, struct rt_addrinfo *);
654	u_short	ifa_flags;		/* mostly rt_flags for cloning */
655	u_int	ifa_refcnt;		/* references to this structure */
656	int	ifa_metric;		/* cost of going out this interface */
657	int (*ifa_claim_addr)		/* check if an addr goes to this if */
658		(struct ifaddr *, struct sockaddr *);
659	struct mtx ifa_mtx;
660};
661#define	IFA_ROUTE	RTF_UP		/* route installed */
662
663/* for compatibility with other BSDs */
664#define	ifa_list	ifa_link
665
666
667struct ifaddr *	ifa_alloc(size_t size, int flags);
668void	ifa_free(struct ifaddr *ifa);
669void	ifa_ref(struct ifaddr *ifa);
670
671
672#define	IFA_LOCK_INIT(ifa)	\
673    mtx_init(&(ifa)->ifa_mtx, "ifaddr", NULL, MTX_DEF)
674#define	IFA_LOCK(ifa)		mtx_lock(&(ifa)->ifa_mtx)
675#define	IFA_UNLOCK(ifa)		mtx_unlock(&(ifa)->ifa_mtx)
676#define	IFA_DESTROY(ifa)	mtx_destroy(&(ifa)->ifa_mtx)
677
678/*
679 * The prefix structure contains information about one prefix
680 * of an interface.  They are maintained by the different address families,
681 * are allocated and attached when a prefix or an address is set,
682 * and are linked together so all prefixes for an interface can be located.
683 */
684struct ifprefix {
685	struct	sockaddr *ifpr_prefix;	/* prefix of interface */
686	struct	ifnet *ifpr_ifp;	/* back-pointer to interface */
687	TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */
688	u_char	ifpr_plen;		/* prefix length in bits */
689	u_char	ifpr_type;		/* protocol dependent prefix type */
690};
691
692/*
693 * Multicast address structure.  This is analogous to the ifaddr
694 * structure except that it keeps track of multicast addresses.
695 * Also, the reference count here is a count of requests for this
696 * address, not a count of pointers to this structure.
697 */
698struct ifmultiaddr {
699	TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
700	struct	sockaddr *ifma_addr; 	/* address this membership is for */
701	struct	sockaddr *ifma_lladdr;	/* link-layer translation, if any */
702	struct	ifnet *ifma_ifp;	/* back-pointer to interface */
703	u_int	ifma_refcount;		/* reference count */
704	void	*ifma_protospec;	/* protocol-specific state, if any */
705
706	/* haiku additions, save a allocation -hugo */
707	struct sockaddr_dl ifma_addr_storage;
708};
709
710#ifdef _KERNEL
711#define	IFA_LOCK(ifa)		mtx_lock(&(ifa)->ifa_mtx)
712#define	IFA_UNLOCK(ifa)		mtx_unlock(&(ifa)->ifa_mtx)
713
714extern	struct rw_lock ifnet_rwlock;
715#define	IFNET_LOCK_INIT()		rw_lock_init(&ifnet_rwlock, "ifnet rwlock")
716#define	IFNET_WLOCK()			rw_lock_write_lock(&ifnet_rwlock)
717#define	IFNET_WUNLOCK()			rw_lock_write_unlock(&ifnet_rwlock)
718#define	IFNET_RLOCK()			rw_lock_read_lock(&ifnet_rwlock)
719#define	IFNET_RLOCK_NOSLEEP()	rw_lock_read_lock(&ifnet_rwlock)
720#define	IFNET_RUNLOCK()			rw_lock_read_unlock(&ifnet_rwlock)
721#define	IFNET_RUNLOCK_NOSLEEP()	rw_lock_read_unlock(&ifnet_rwlock)
722
723struct ifnet	*ifnet_byindex(u_short idx);
724struct ifnet	*ifnet_byindex_locked(u_short idx);
725
726extern	struct ifnethead ifnet;
727extern	int ifqmaxlen;
728extern	struct ifnet *loif;	/* first loopback interface */
729extern	int if_index;
730
731int	if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
732int	if_allmulti(struct ifnet *, int);
733struct	ifnet* if_alloc(u_char);
734void	if_attach(struct ifnet *);
735int	if_delmulti(struct ifnet *, struct sockaddr *);
736void	if_detach(struct ifnet *);
737void	if_purgeaddrs(struct ifnet *);
738void    if_delallmulti(struct ifnet *);
739void	if_purgemaddrs(struct ifnet *);
740void	if_down(struct ifnet *);
741void	if_free(struct ifnet *);
742void	if_free_type(struct ifnet *, u_char);
743void	if_initname(struct ifnet *, const char *, int);
744void	if_link_state_change(struct ifnet *, int);
745int	if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
746int	if_setlladdr(struct ifnet *, const u_char *, int);
747void	if_up(struct ifnet *);
748/*void	ifinit(void);*/ /* declared in systm.h for main() */
749int	ifioctl(struct socket *, u_long, caddr_t, struct thread *);
750int	ifpromisc(struct ifnet *, int);
751struct	ifnet *ifunit(const char *);
752
753/* Haiku extension for OpenBSD compat */
754int if_alloc_inplace(struct ifnet *ifp, u_char type);
755void if_free_inplace(struct ifnet *ifp);
756
757struct	ifaddr *ifa_ifwithaddr(struct sockaddr *);
758struct	ifaddr *ifa_ifwithbroadaddr(struct sockaddr *);
759struct	ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
760struct	ifaddr *ifa_ifwithnet(struct sockaddr *);
761struct	ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *);
762struct	ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
763
764int	if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);
765
766typedef	void *if_com_alloc_t(u_char type, struct ifnet *ifp);
767typedef	void if_com_free_t(void *com, u_char type);
768void	if_register_com_alloc(u_char type, if_com_alloc_t *a, if_com_free_t *f);
769void	if_deregister_com_alloc(u_char type);
770void	if_data_copy(struct ifnet *, struct if_data *);
771uint64_t if_get_counter_default(struct ifnet *, ift_counter);
772void	if_inc_counter(struct ifnet *, ift_counter, int64_t);
773
774#define IF_LLADDR(ifp)							\
775    LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr))
776
777uint64_t if_setbaudrate(if_t ifp, uint64_t baudrate);
778uint64_t if_getbaudrate(if_t ifp);
779int if_setcapabilities(if_t ifp, int capabilities);
780int if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit);
781int if_getcapabilities(if_t ifp);
782int if_togglecapenable(if_t ifp, int togglecap);
783int if_setcapenable(if_t ifp, int capenable);
784int if_setcapenablebit(if_t ifp, int setcap, int clearcap);
785int if_getcapenable(if_t ifp);
786const char *if_getdname(if_t ifp);
787int if_setdev(if_t ifp, void *dev);
788int if_setdrvflagbits(if_t ifp, int if_setflags, int clear_flags);
789int if_getdrvflags(if_t ifp);
790int if_setdrvflags(if_t ifp, int flags);
791int if_clearhwassist(if_t ifp);
792int if_sethwassistbits(if_t ifp, int toset, int toclear);
793int if_sethwassist(if_t ifp, int hwassist_bit);
794int if_gethwassist(if_t ifp);
795int if_setsoftc(if_t ifp, void *softc);
796void *if_getsoftc(if_t ifp);
797int if_setflags(if_t ifp, int flags);
798int if_gethwaddr(if_t ifp, struct ifreq *);
799int if_setmtu(if_t ifp, int mtu);
800int if_getmtu(if_t ifp);
801int if_getmtu_family(if_t ifp, int family);
802int if_setflagbits(if_t ifp, int set, int clear);
803int if_getflags(if_t ifp);
804int if_sendq_empty(if_t ifp);
805int if_setsendqready(if_t ifp);
806int if_setsendqlen(if_t ifp, int tx_desc_count);
807int if_input(if_t ifp, struct mbuf* sendmp);
808int if_sendq_prepend(if_t ifp, struct mbuf *m);
809struct mbuf *if_dequeue(if_t ifp);
810int if_setifheaderlen(if_t ifp, int len);
811void if_setrcvif(struct mbuf *m, if_t ifp);
812
813void if_setvtag(struct mbuf *m, u_int16_t tag);
814u_int16_t if_getvtag(struct mbuf *m);
815int if_vlantrunkinuse(if_t ifp);
816caddr_t if_getlladdr(if_t ifp);
817void *if_gethandle(u_char);
818void if_bpfmtap(if_t ifp, struct mbuf *m);
819void if_etherbpfmtap(if_t ifp, struct mbuf *m);
820void if_vlancap(if_t ifp);
821
822int if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max);
823int if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max);
824int if_multiaddr_count(if_t ifp, int max);
825
826/*
827 * Traversing through interface address lists.
828 */
829struct sockaddr_dl;
830typedef u_int iflladdr_cb_t(void *, struct sockaddr_dl *, u_int);
831u_int if_foreach_lladdr(if_t, iflladdr_cb_t, void *);
832u_int if_foreach_llmaddr(if_t, iflladdr_cb_t, void *);
833u_int if_lladdr_count(if_t);
834u_int if_llmaddr_count(if_t);
835
836/* Functions */
837void if_setinitfn(if_t ifp, void (*)(void *));
838void if_setioctlfn(if_t ifp, int (*)(if_t, u_long, caddr_t));
839void if_setstartfn(if_t ifp, void (*)(if_t));
840void if_settransmitfn(if_t ifp, if_transmit_fn_t);
841void if_setqflushfn(if_t ifp, if_qflush_fn_t);
842void if_setgetcounterfn(if_t ifp, if_get_counter_t);
843
844/* accessors for struct ifreq */
845static inline void*
846ifr_data_get_ptr(void* ifrp)
847{
848	struct ifreq* ifr = (struct ifreq *)ifrp;
849	return ifr->ifr_data;
850}
851
852#ifdef DEVICE_POLLING
853enum poll_cmd {	POLL_ONLY, POLL_AND_CHECK_STATUS };
854
855typedef	void poll_handler_t(struct ifnet *ifp, enum poll_cmd cmd, int count);
856int    ether_poll_register(poll_handler_t *h, struct ifnet *ifp);
857int    ether_poll_deregister(struct ifnet *ifp);
858#endif /* DEVICE_POLLING */
859
860#endif /* _KERNEL */
861
862#endif /* _FBSD_COMPAT_NET_IF_VAR_H_ */
863