| 1 | /*- |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * |
| 4 | * Copyright (c) 1982, 1986, 1989, 1993 |
| 5 | *	The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. Neither the name of the University nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef	_NET_IF_PRIVATE_H_ |
| 33 | #define	_NET_IF_PRIVATE_H_ |
| 34 | |
| 35 | #ifdef	_KERNEL |
| 36 | /* |
| 37 | * Structure defining a network interface. |
| 38 | */ |
| 39 | struct ifnet { |
| 40 | 	/* General book keeping of interface lists. */ |
| 41 | 	CK_STAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained (CK_) */ |
| 42 | 	LIST_ENTRY(ifnet) if_clones;	/* interfaces of a cloner */ |
| 43 | 	CK_STAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if (CK_) */ |
| 44 | 					/* protected by if_addr_lock */ |
| 45 | 	u_char	if_alloctype;		/* if_type at time of allocation */ |
| 46 | 	uint8_t	if_numa_domain;		/* NUMA domain of device */ |
| 47 | 	/* Driver and protocol specific information that remains stable. */ |
| 48 | 	void	*if_softc;		/* pointer to driver state */ |
| 49 | 	void	*if_llsoftc;		/* link layer softc */ |
| 50 | 	void	*if_l2com;		/* pointer to protocol bits */ |
| 51 | 	const char *if_dname;		/* driver name */ |
| 52 | 	int	if_dunit;		/* unit or IF_DUNIT_NONE */ |
| 53 | 	u_short	if_index;		/* numeric abbreviation for this if */ |
| 54 | 	u_short	if_idxgen;		/* ... and its generation count */ |
| 55 | 	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */ |
| 56 | 	char	*if_description;	/* interface description */ |
| 57 | |
| 58 | 	/* Variable fields that are touched by the stack and drivers. */ |
| 59 | 	int	if_flags;		/* up/down, broadcast, etc. */ |
| 60 | 	int	if_drv_flags;		/* driver-managed status flags */ |
| 61 | 	int	if_capabilities;	/* interface features & capabilities */ |
| 62 | 	int	if_capabilities2;	/* part 2 */ |
| 63 | 	int	if_capenable;		/* enabled features & capabilities */ |
| 64 | 	int	if_capenable2;		/* part 2 */ |
| 65 | 	void	*if_linkmib;		/* link-type-specific MIB data */ |
| 66 | 	size_t	if_linkmiblen;		/* length of above data */ |
| 67 | 	u_int	if_refcount;		/* reference count */ |
| 68 | |
| 69 | 	/* These fields are shared with struct if_data. */ |
| 70 | 	uint8_t		if_type;	/* ethernet, tokenring, etc */ |
| 71 | 	uint8_t		if_addrlen;	/* media address length */ |
| 72 | 	uint8_t		if_hdrlen;	/* media header length */ |
| 73 | 	uint8_t		if_link_state;	/* current link state */ |
| 74 | 	uint32_t	if_mtu;		/* maximum transmission unit */ |
| 75 | 	uint32_t	if_metric;	/* routing metric (external only) */ |
| 76 | 	uint64_t	if_baudrate;	/* linespeed */ |
| 77 | 	uint64_t	if_hwassist;	/* HW offload capabilities, see IFCAP */ |
| 78 | 	time_t		if_epoch;	/* uptime at attach or stat reset */ |
| 79 | 	struct timeval	if_lastchange;	/* time of last administrative change */ |
| 80 | |
| 81 | 	struct ifaltq if_snd;		/* output queue (includes altq) */ |
| 82 | 	struct	task if_linktask;	/* task for link change events */ |
| 83 | 	struct	task if_addmultitask;	/* task for SIOCADDMULTI */ |
| 84 | |
| 85 | 	/* Addresses of different protocol families assigned to this if. */ |
| 86 | 	struct mtx if_addr_lock;	/* lock to protect address lists */ |
| 87 | 		/* |
| 88 | 		 * if_addrhead is the list of all addresses associated to |
| 89 | 		 * an interface. |
| 90 | 		 * Some code in the kernel assumes that first element |
| 91 | 		 * of the list has type AF_LINK, and contains sockaddr_dl |
| 92 | 		 * addresses which store the link-level address and the name |
| 93 | 		 * of the interface. |
| 94 | 		 * However, access to the AF_LINK address through this |
| 95 | 		 * field is deprecated. Use if_addr instead. |
| 96 | 		 */ |
| 97 | 	struct	ifaddrhead if_addrhead;	/* linked list of addresses per if */ |
| 98 | 	struct	ifmultihead if_multiaddrs; /* multicast addresses configured */ |
| 99 | 	int	if_amcount;		/* number of all-multicast requests */ |
| 100 | 	struct	ifaddr	*if_addr;	/* pointer to link-level address */ |
| 101 | 	void	*if_hw_addr;		/* hardware link-level address */ |
| 102 | 	const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */ |
| 103 | 	struct	mtx if_afdata_lock; |
| 104 | 	void	*if_afdata[AF_MAX]; |
| 105 | 	int	if_afdata_initialized; |
| 106 | |
| 107 | 	/* Additional features hung off the interface. */ |
| 108 | 	u_int	if_fib;			/* interface FIB */ |
| 109 | 	struct	vnet *if_vnet;		/* pointer to network stack instance */ |
| 110 | 	struct	vnet *if_home_vnet;	/* where this ifnet originates from */ |
| 111 | 	struct ifvlantrunk *if_vlantrunk; /* pointer to 802.1q data */ |
| 112 | 	struct	bpf_if *if_bpf;		/* packet filter structure */ |
| 113 | 	int	if_pcount;		/* number of promiscuous listeners */ |
| 114 | 	void	*if_bridge;		/* bridge glue */ |
| 115 | 	void	*if_lagg;		/* lagg glue */ |
| 116 | 	void	*if_pf_kif;		/* pf glue */ |
| 117 | 	struct	carp_if *if_carp;	/* carp interface structure */ |
| 118 | 	struct	label *if_label;	/* interface MAC label */ |
| 119 | 	struct	netmap_adapter *if_netmap; /* netmap(4) softc */ |
| 120 | |
| 121 | 	/* Various procedures of the layer2 encapsulation and drivers. */ |
| 122 | 	if_output_fn_t if_output;	/* output routine (enqueue) */ |
| 123 | 	if_input_fn_t if_input;		/* input routine (from h/w driver) */ |
| 124 | 	struct mbuf *(*if_bridge_input)(struct ifnet *, struct mbuf *); |
| 125 | 	int	(*if_bridge_output)(struct ifnet *, struct mbuf *, struct sockaddr *, |
| 126 | 		 struct rtentry *); |
| 127 | 	void (*if_bridge_linkstate)(struct ifnet *ifp); |
| 128 | 	if_start_fn_t	if_start;	/* initiate output routine */ |
| 129 | 	if_ioctl_fn_t	if_ioctl;	/* ioctl routine */ |
| 130 | 	if_init_fn_t	if_init;	/* Init routine */ |
| 131 | 	int	(*if_resolvemulti)	/* validate/resolve multicast */ |
| 132 | 		(struct ifnet *, struct sockaddr **, struct sockaddr *); |
| 133 | 	if_qflush_fn_t	if_qflush;	/* flush any queue */ |
| 134 | 	if_transmit_fn_t if_transmit; /* initiate output routine */ |
| 135 | |
| 136 | 	if_reassign_fn_t if_reassign;		/* reassign to vnet routine */ |
| 137 | 	if_get_counter_t if_get_counter; /* get counter values */ |
| 138 | 	int	(*if_requestencap)	/* make link header from request */ |
| 139 | 		(struct ifnet *, struct if_encap_req *); |
| 140 | |
| 141 | 	const struct if_ipsec_accel_methods *if_ipsec_accel_m; |
| 142 | |
| 143 | 	/* Statistics. */ |
| 144 | 	counter_u64_t	if_counters[IFCOUNTERS]; |
| 145 | |
| 146 | 	/* Stuff that's only temporary and doesn't belong here. */ |
| 147 | |
| 148 | 	/* |
| 149 | 	 * Network adapter TSO limits: |
| 150 | 	 * =========================== |
| 151 | 	 * |
| 152 | 	 * If the "if_hw_tsomax" field is zero the maximum segment |
| 153 | 	 * length limit does not apply. If the "if_hw_tsomaxsegcount" |
| 154 | 	 * or the "if_hw_tsomaxsegsize" field is zero the TSO segment |
| 155 | 	 * count limit does not apply. If all three fields are zero, |
| 156 | 	 * there is no TSO limit. |
| 157 | 	 * |
| 158 | 	 * NOTE: The TSO limits should reflect the values used in the |
| 159 | 	 * BUSDMA tag a network adapter is using to load a mbuf chain |
| 160 | 	 * for transmission. The TCP/IP network stack will subtract |
| 161 | 	 * space for all linklevel and protocol level headers and |
| 162 | 	 * ensure that the full mbuf chain passed to the network |
| 163 | 	 * adapter fits within the given limits. |
| 164 | 	 */ |
| 165 | 	u_int	if_hw_tsomax;		/* TSO maximum size in bytes */ |
| 166 | 	u_int	if_hw_tsomaxsegcount;	/* TSO maximum segment count */ |
| 167 | 	u_int	if_hw_tsomaxsegsize;	/* TSO maximum segment size in bytes */ |
| 168 | |
| 169 | 	/* |
| 170 | 	 * Network adapter send tag support: |
| 171 | 	 */ |
| 172 | 	if_snd_tag_alloc_t *if_snd_tag_alloc; |
| 173 | |
| 174 | 	/* Ratelimit (packet pacing) */ |
| 175 | 	if_ratelimit_query_t *if_ratelimit_query; |
| 176 | 	if_ratelimit_setup_t *if_ratelimit_setup; |
| 177 | |
| 178 | 	/* Ethernet PCP */ |
| 179 | 	uint8_t if_pcp; |
| 180 | |
| 181 | 	/* |
| 182 | 	 * Debugnet (Netdump) hooks to be called while in db/panic. |
| 183 | 	 */ |
| 184 | 	struct debugnet_methods *if_debugnet_methods; |
| 185 | 	struct epoch_context	if_epoch_ctx; |
| 186 | |
| 187 | 	/* |
| 188 | 	 * Spare fields to be added before branching a stable branch, so |
| 189 | 	 * that structure can be enhanced without changing the kernel |
| 190 | 	 * binary interface. |
| 191 | 	 */ |
| 192 | 	int	if_ispare[4];		/* general use */ |
| 193 | }; |
| 194 | |
| 195 | #define	IF_AFDATA_LOCK_INIT(ifp)	\ |
| 196 | 	mtx_init(&(ifp)->if_afdata_lock, "if_afdata", NULL, MTX_DEF) |
| 197 | |
| 198 | #define	IF_AFDATA_WLOCK(ifp)	mtx_lock(&(ifp)->if_afdata_lock) |
| 199 | #define	IF_AFDATA_WUNLOCK(ifp)	mtx_unlock(&(ifp)->if_afdata_lock) |
| 200 | #define	IF_AFDATA_LOCK(ifp)	IF_AFDATA_WLOCK(ifp) |
| 201 | #define	IF_AFDATA_UNLOCK(ifp)	IF_AFDATA_WUNLOCK(ifp) |
| 202 | #define	IF_AFDATA_TRYLOCK(ifp)	mtx_trylock(&(ifp)->if_afdata_lock) |
| 203 | #define	IF_AFDATA_DESTROY(ifp)	mtx_destroy(&(ifp)->if_afdata_lock) |
| 204 | |
| 205 | #define	IF_AFDATA_LOCK_ASSERT(ifp)	MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(ifp)->if_afdata_lock)) |
| 206 | #define	IF_AFDATA_WLOCK_ASSERT(ifp)	mtx_assert(&(ifp)->if_afdata_lock, MA_OWNED) |
| 207 | #define	IF_AFDATA_UNLOCK_ASSERT(ifp)	mtx_assert(&(ifp)->if_afdata_lock, MA_NOTOWNED) |
| 208 | |
| 209 | #define IF_LLADDR(ifp)							\ |
| 210 | LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr)) |
| 211 | |
| 212 | #endif	/* _KERNEL */ |
| 213 | |
| 214 | #endif	/* _NET_IF_PRIVATE_H_ */ |