xref: /haiku/src/libs/compat/freebsd_network/compat/net/if_var.h (revision 56c5e3428580142656455e96d2159e2338fb4ffe)
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  */
65 struct	mbuf;
66 struct	thread;
67 struct	rtentry;
68 struct	rt_addrinfo;
69 struct	socket;
70 struct	ether_header;
71 struct	carp_if;
72 struct	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 
93 typedef 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 
109 TAILQ_HEAD(ifnethead, ifnet);	/* we use TAILQs so that the order of */
110 TAILQ_HEAD(ifaddrhead, ifaddr);	/* instantiation is preserved in the list */
111 TAILQ_HEAD(ifprefixhead, ifprefix);
112 TAILQ_HEAD(ifmultihead, ifmultiaddr);
113 
114 typedef struct ifnet * if_t;
115 
116 typedef	void (*if_start_fn_t)(if_t);
117 typedef	int (*if_ioctl_fn_t)(if_t, u_long, caddr_t);
118 typedef	void (*if_init_fn_t)(void *);
119 typedef void (*if_qflush_fn_t)(if_t);
120 typedef int (*if_transmit_fn_t)(if_t, struct mbuf *);
121 typedef	uint64_t (*if_get_counter_t)(if_t, ift_counter);
122 
123 struct 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 */
130 typedef 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 
156 struct 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  */
172 struct	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 
181 struct 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 
189 struct 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 
303 typedef 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 
349 void	if_addr_rlock(struct ifnet *ifp);	/* if_addrhead */
350 void	if_addr_runlock(struct ifnet *ifp);	/* if_addrhead */
351 void	if_maddr_rlock(struct ifnet *ifp);	/* if_multiaddrs */
352 void	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 */
434 typedef void (*ifaddr_event_handler_t)(void *, struct ifnet *);
435 EVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
436 /* new interface arrival event */
437 typedef void (*ifnet_arrival_event_handler_t)(void *, struct ifnet *);
438 EVENTHANDLER_DECLARE(ifnet_arrival_event, ifnet_arrival_event_handler_t);
439 /* interface departure event */
440 typedef void (*ifnet_departure_event_handler_t)(void *, struct ifnet *);
441 EVENTHANDLER_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 
460 int	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 
467 void	if_start(struct ifnet *);
468 
469 #define	IFQ_ENQUEUE(ifq, m, err)					\
470 do {									\
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)					\
489 do {									\
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)						\
499 do {									\
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)						\
506 do {									\
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)						\
516 do {									\
517 	IF_LOCK(ifq);							\
518 	IFQ_POLL_NOLOCK(ifq, m);					\
519 	IF_UNLOCK(ifq);							\
520 } while (0)
521 
522 #define	IFQ_PURGE_NOLOCK(ifq)						\
523 do {									\
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)							\
531 do {									\
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)				\
554 do {									\
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 		if_inc_counter((ifp), IFCOUNTER_OBYTES, len + (adj));	\
563 		if (mflags & M_MCAST)					\
564 			if_inc_counter((ifp), IFCOUNTER_OMCASTS, 1);	\
565 		if (((ifp)->if_drv_flags & IFF_DRV_OACTIVE) == 0)	\
566 			if_start(ifp);					\
567 	} else								\
568 		if_inc_counter((ifp), IFCOUNTER_OQDROPS, 1);		\
569 } while (0)
570 
571 #define	IFQ_HANDOFF(ifp, m, err)					\
572 	IFQ_HANDOFF_ADJ(ifp, m, 0, err)
573 
574 #define	IFQ_DRV_DEQUEUE(ifq, m)						\
575 do {									\
576 	(m) = (ifq)->ifq_drv_head;					\
577 	if (m) {							\
578 		if (((ifq)->ifq_drv_head = (m)->m_nextpkt) == NULL)	\
579 			(ifq)->ifq_drv_tail = NULL;			\
580 		(m)->m_nextpkt = NULL;					\
581 		(ifq)->ifq_drv_len--;					\
582 	} else {							\
583 		IFQ_LOCK(ifq);						\
584 		IFQ_DEQUEUE_NOLOCK(ifq, m);				\
585 		while ((ifq)->ifq_drv_len < (ifq)->ifq_drv_maxlen) {	\
586 			struct mbuf *m0;				\
587 			IFQ_DEQUEUE_NOLOCK(ifq, m0);			\
588 			if (m0 == NULL)					\
589 				break;					\
590 			m0->m_nextpkt = NULL;				\
591 			if ((ifq)->ifq_drv_tail == NULL)		\
592 				(ifq)->ifq_drv_head = m0;		\
593 			else						\
594 				(ifq)->ifq_drv_tail->m_nextpkt = m0;	\
595 			(ifq)->ifq_drv_tail = m0;			\
596 			(ifq)->ifq_drv_len++;				\
597 		}							\
598 		IFQ_UNLOCK(ifq);					\
599 	}								\
600 } while (0)
601 
602 #define	IFQ_DRV_PREPEND(ifq, m)						\
603 do {									\
604 	(m)->m_nextpkt = (ifq)->ifq_drv_head;				\
605 	if ((ifq)->ifq_drv_tail == NULL)				\
606 		(ifq)->ifq_drv_tail = (m);				\
607 	(ifq)->ifq_drv_head = (m);					\
608 	(ifq)->ifq_drv_len++;						\
609 } while (0)
610 
611 #define	IFQ_DRV_IS_EMPTY(ifq)						\
612 	(((ifq)->ifq_drv_len == 0) && ((ifq)->ifq_len == 0))
613 
614 #define	IFQ_DRV_PURGE(ifq)						\
615 do {									\
616 	struct mbuf *m, *n = (ifq)->ifq_drv_head;			\
617 	while((m = n) != NULL) {					\
618 		n = m->m_nextpkt;					\
619 		m_freem(m);						\
620 	}								\
621 	(ifq)->ifq_drv_head = (ifq)->ifq_drv_tail = NULL;		\
622 	(ifq)->ifq_drv_len = 0;						\
623 	IFQ_PURGE(ifq);							\
624 } while (0)
625 
626 /*
627  * 72 was chosen below because it is the size of a TCP/IP
628  * header (40) + the minimum mss (32).
629  */
630 #define	IF_MINMTU	72
631 #define	IF_MAXMTU	65535
632 
633 #endif /* _KERNEL */
634 
635 /*
636  * The ifaddr structure contains information about one address
637  * of an interface.  They are maintained by the different address families,
638  * are allocated and attached when an address is set, and are linked
639  * together so all addresses for an interface can be located.
640  *
641  * NOTE: a 'struct ifaddr' is always at the beginning of a larger
642  * chunk of malloc'ed memory, where we store the three addresses
643  * (ifa_addr, ifa_dstaddr and ifa_netmask) referenced here.
644  */
645 struct ifaddr {
646 	struct	sockaddr *ifa_addr;	/* address of interface */
647 	struct	sockaddr *ifa_dstaddr;	/* other end of p-to-p link */
648 #define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
649 	struct	sockaddr *ifa_netmask;	/* used to determine subnet */
650 	struct	if_data if_data;	/* not all members are meaningful */
651 	struct	ifnet *ifa_ifp;		/* back-pointer to interface */
652 	TAILQ_ENTRY(ifaddr) ifa_link;	/* queue macro glue */
653 	void	(*ifa_rtrequest)	/* check or clean routes (+ or -)'d */
654 		(int, struct rtentry *, struct rt_addrinfo *);
655 	u_short	ifa_flags;		/* mostly rt_flags for cloning */
656 	u_int	ifa_refcnt;		/* references to this structure */
657 	int	ifa_metric;		/* cost of going out this interface */
658 	int (*ifa_claim_addr)		/* check if an addr goes to this if */
659 		(struct ifaddr *, struct sockaddr *);
660 };
661 #define	IFA_ROUTE	RTF_UP		/* route installed */
662 
663 /* for compatibility with other BSDs */
664 #define	ifa_list	ifa_link
665 
666 
667 struct ifaddr *	ifa_alloc(size_t size, int flags);
668 void	ifa_free(struct ifaddr *ifa);
669 void	ifa_ref(struct ifaddr *ifa);
670 
671 /*
672  * The prefix structure contains information about one prefix
673  * of an interface.  They are maintained by the different address families,
674  * are allocated and attached when a prefix or an address is set,
675  * and are linked together so all prefixes for an interface can be located.
676  */
677 struct ifprefix {
678 	struct	sockaddr *ifpr_prefix;	/* prefix of interface */
679 	struct	ifnet *ifpr_ifp;	/* back-pointer to interface */
680 	TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */
681 	u_char	ifpr_plen;		/* prefix length in bits */
682 	u_char	ifpr_type;		/* protocol dependent prefix type */
683 };
684 
685 /*
686  * Multicast address structure.  This is analogous to the ifaddr
687  * structure except that it keeps track of multicast addresses.
688  * Also, the reference count here is a count of requests for this
689  * address, not a count of pointers to this structure.
690  */
691 struct ifmultiaddr {
692 	TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
693 	struct	sockaddr *ifma_addr; 	/* address this membership is for */
694 	struct	sockaddr *ifma_lladdr;	/* link-layer translation, if any */
695 	struct	ifnet *ifma_ifp;	/* back-pointer to interface */
696 	u_int	ifma_refcount;		/* reference count */
697 	void	*ifma_protospec;	/* protocol-specific state, if any */
698 
699 	/* haiku additions, save a allocation -hugo */
700 	struct sockaddr_dl ifma_addr_storage;
701 };
702 
703 #ifdef _KERNEL
704 extern	struct rw_lock ifnet_rwlock;
705 #define	IFNET_LOCK_INIT()		rw_lock_init(&ifnet_rwlock, "ifnet rwlock")
706 #define	IFNET_WLOCK()			rw_lock_write_lock(&ifnet_rwlock)
707 #define	IFNET_WUNLOCK()			rw_lock_write_unlock(&ifnet_rwlock)
708 #define	IFNET_RLOCK()			rw_lock_read_lock(&ifnet_rwlock)
709 #define	IFNET_RLOCK_NOSLEEP()	rw_lock_read_lock(&ifnet_rwlock)
710 #define	IFNET_RUNLOCK()			rw_lock_read_unlock(&ifnet_rwlock)
711 #define	IFNET_RUNLOCK_NOSLEEP()	rw_lock_read_unlock(&ifnet_rwlock)
712 
713 if_t ifnet_byindex(u_int);
714 
715 extern	struct ifnethead ifnet;
716 extern	int ifqmaxlen;
717 extern	struct ifnet *loif;	/* first loopback interface */
718 extern	int if_index;
719 
720 int	if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
721 int	if_allmulti(struct ifnet *, int);
722 struct	ifnet* if_alloc(u_char);
723 void	if_attach(struct ifnet *);
724 int	if_delmulti(struct ifnet *, struct sockaddr *);
725 void	if_detach(struct ifnet *);
726 void	if_purgeaddrs(struct ifnet *);
727 void    if_delallmulti(struct ifnet *);
728 void	if_down(struct ifnet *);
729 void	if_free(struct ifnet *);
730 void	if_free_type(struct ifnet *, u_char);
731 void	if_initname(struct ifnet *, const char *, int);
732 void	if_link_state_change(struct ifnet *, int);
733 int	if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
734 int	if_setlladdr(struct ifnet *, const u_char *, int);
735 void	if_up(struct ifnet *);
736 /*void	ifinit(void);*/ /* declared in systm.h for main() */
737 int	ifioctl(struct socket *, u_long, caddr_t, struct thread *);
738 int	ifpromisc(struct ifnet *, int);
739 struct	ifnet *ifunit(const char *);
740 
741 /* Haiku extension for OpenBSD compat */
742 int if_alloc_inplace(struct ifnet *ifp, u_char type);
743 void if_free_inplace(struct ifnet *ifp);
744 
745 uint64_t if_get_counter_default(struct ifnet *, ift_counter);
746 void	if_inc_counter(struct ifnet *, ift_counter, int64_t);
747 
748 #define IF_LLADDR(ifp)							\
749 	LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr))
750 
751 uint64_t if_setbaudrate(if_t ifp, uint64_t baudrate);
752 uint64_t if_getbaudrate(const if_t ifp);
753 int if_setcapabilities(if_t ifp, int capabilities);
754 int if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit);
755 int if_getcapabilities(const if_t ifp);
756 int if_togglecapenable(if_t ifp, int togglecap);
757 int if_setcapenable(if_t ifp, int capenable);
758 int if_setcapenablebit(if_t ifp, int setcap, int clearcap);
759 int if_getcapenable(const if_t ifp);
760 int if_setcapabilities2(if_t ifp, int capabilities);
761 int if_setcapabilities2bit(if_t ifp, int setbit, int clearbit);
762 int if_getcapabilities2(const if_t ifp);
763 int if_togglecapenable2(if_t ifp, int togglecap);
764 int if_setcapenable2(if_t ifp, int capenable);
765 int if_setcapenable2bit(if_t ifp, int setcap, int clearcap);
766 int if_getcapenable2(const if_t ifp);
767 int if_getdunit(const if_t ifp);
768 int if_getindex(const if_t ifp);
769 int if_getidxgen(const if_t ifp);
770 const char *if_getdname(const if_t ifp);
771 void if_setdname(if_t ifp, const char *name);
772 const char *if_name(if_t ifp);
773 int if_setname(if_t ifp, const char *name);
774 int if_rename(if_t ifp, char *new_name);
775 const char *if_getdescr(if_t ifp);
776 void if_setdescr(if_t ifp, char *descrbuf);
777 char *if_allocdescr(size_t sz, int malloc_flag);
778 void if_freedescr(char *descrbuf);
779 void if_setlastchange(if_t ifp);
780 int if_getalloctype(const if_t ifp);
781 int if_gettype(const if_t ifp);
782 int if_setdev(if_t ifp, void *dev);
783 int if_setdrvflagbits(if_t ifp, int if_setflags, int clear_flags);
784 int if_getdrvflags(const if_t ifp);
785 int if_setdrvflags(if_t ifp, int flags);
786 int if_getlinkstate(if_t ifp);
787 int if_clearhwassist(if_t ifp);
788 int if_sethwassistbits(if_t ifp, int toset, int toclear);
789 int if_sethwassist(if_t ifp, int hwassist_bit);
790 int if_gethwassist(const if_t ifp);
791 int if_togglehwassist(if_t ifp, int toggle_bits);
792 int if_setsoftc(if_t ifp, void *softc);
793 void *if_getsoftc(if_t ifp);
794 int if_setflags(if_t ifp, int flags);
795 void if_setllsoftc(if_t ifp, void *softc);
796 void *if_getllsoftc(if_t ifp);
797 u_int if_getfib(if_t ifp);
798 uint8_t if_getaddrlen(if_t ifp);
799 int if_gethwaddr(const if_t ifp, struct ifreq *);
800 const uint8_t *if_getbroadcastaddr(const if_t ifp);
801 void if_setbroadcastaddr(if_t ifp, const uint8_t *);
802 int if_setmtu(if_t ifp, int mtu);
803 int if_getmtu(const if_t ifp);
804 int if_getmtu_family(const if_t ifp, int family);
805 void if_notifymtu(if_t ifp);
806 int if_setflagbits(if_t ifp, int set, int clear);
807 int if_setflags(if_t ifp, int flags);
808 int if_getflags(const if_t ifp);
809 int if_getnumadomain(if_t ifp);
810 int if_sendq_empty(if_t ifp);
811 int if_setsendqready(if_t ifp);
812 int if_setsendqlen(if_t ifp, int tx_desc_count);
813 int if_sethwtsomax(if_t ifp, u_int if_hw_tsomax);
814 int if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount);
815 int if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize);
816 u_int if_gethwtsomax(const if_t ifp);
817 u_int if_gethwtsomaxsegcount(const if_t ifp);
818 u_int if_gethwtsomaxsegsize(const if_t ifp);
819 void if_input(if_t ifp, struct mbuf* sendmp);
820 int if_sendq_prepend(if_t ifp, struct mbuf *m);
821 struct mbuf *if_dequeue(if_t ifp);
822 int if_setifheaderlen(if_t ifp, int len);
823 void if_setrcvif(struct mbuf *m, if_t ifp);
824 void if_setvtag(struct mbuf *m, u_int16_t tag);
825 u_int16_t if_getvtag(struct mbuf *m);
826 int if_vlantrunkinuse(if_t ifp);
827 caddr_t if_getlladdr(const if_t ifp);
828 struct vnet *if_getvnet(const if_t ifp);
829 void *if_gethandle(u_char);
830 void if_vlancap(if_t ifp);
831 int if_transmit(if_t ifp, struct mbuf *m);
832 void if_init(if_t ifp, void *ctx);
833 int if_ioctl(if_t ifp, u_long cmd, void *data);
834 int if_resolvemulti(if_t ifp, struct sockaddr **, struct sockaddr *);
835 uint64_t if_getcounter(if_t ifp, ift_counter counter);
836 struct label *if_getmaclabel(if_t ifp);
837 void if_setmaclabel(if_t ifp, struct label *label);
838 struct bpf_if *if_getbpf(if_t ifp);
839 uint8_t if_getpcp(if_t ifp);
840 void *if_getl2com(if_t ifp);
841 struct ifvlantrunk *if_getvlantrunk(if_t ifp);
842 bool if_altq_is_enabled(if_t ifp);
843 
844 void if_bpfmtap(if_t ifp, struct mbuf *m);
845 void if_etherbpfmtap(if_t ifp, struct mbuf *m);
846 
847 /*
848  * Traversing through interface address lists.
849  */
850 struct sockaddr_dl;
851 typedef u_int iflladdr_cb_t(void *, struct sockaddr_dl *, u_int);
852 u_int if_foreach_lladdr(if_t, iflladdr_cb_t, void *);
853 u_int if_foreach_llmaddr(if_t, iflladdr_cb_t, void *);
854 u_int if_lladdr_count(if_t);
855 u_int if_llmaddr_count(if_t);
856 
857 /* Functions */
858 void if_setinitfn(if_t ifp, void (*)(void *));
859 void if_setioctlfn(if_t ifp, int (*)(if_t, u_long, caddr_t));
860 void if_setstartfn(if_t ifp, void (*)(if_t));
861 void if_settransmitfn(if_t ifp, if_transmit_fn_t);
862 void if_setqflushfn(if_t ifp, if_qflush_fn_t);
863 void if_setgetcounterfn(if_t ifp, if_get_counter_t);
864 
865 /* accessors for struct ifreq */
866 static inline void*
ifr_data_get_ptr(void * ifrp)867 ifr_data_get_ptr(void* ifrp)
868 {
869 	struct ifreq* ifr = (struct ifreq *)ifrp;
870 	return ifr->ifr_data;
871 }
872 
873 #ifdef DEVICE_POLLING
874 enum poll_cmd {	POLL_ONLY, POLL_AND_CHECK_STATUS };
875 
876 typedef	void poll_handler_t(struct ifnet *ifp, enum poll_cmd cmd, int count);
877 int    ether_poll_register(poll_handler_t *h, struct ifnet *ifp);
878 int    ether_poll_deregister(struct ifnet *ifp);
879 #endif /* DEVICE_POLLING */
880 
881 #endif /* _KERNEL */
882 
883 #endif /* _FBSD_COMPAT_NET_IF_VAR_H_ */
884