1a50ffc2imp/*-
24736ccfpfg * SPDX-License-Identifier: BSD-3-Clause
34736ccfpfg *
44d20b5bwollman * Copyright (c) 1982, 1986, 1989, 1993
54d20b5bwollman *	The Regents of the University of California.  All rights reserved.
64d20b5bwollman *
74d20b5bwollman * Redistribution and use in source and binary forms, with or without
84d20b5bwollman * modification, are permitted provided that the following conditions
94d20b5bwollman * are met:
104d20b5bwollman * 1. Redistributions of source code must retain the above copyright
114d20b5bwollman *    notice, this list of conditions and the following disclaimer.
124d20b5bwollman * 2. Redistributions in binary form must reproduce the above copyright
134d20b5bwollman *    notice, this list of conditions and the following disclaimer in the
144d20b5bwollman *    documentation and/or other materials provided with the distribution.
157e6cabdimp * 3. Neither the name of the University nor the names of its contributors
164d20b5bwollman *    may be used to endorse or promote products derived from this software
174d20b5bwollman *    without specific prior written permission.
184d20b5bwollman *
194d20b5bwollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
204d20b5bwollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214d20b5bwollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224d20b5bwollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
234d20b5bwollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244d20b5bwollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254d20b5bwollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264d20b5bwollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274d20b5bwollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284d20b5bwollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294d20b5bwollman * SUCH DAMAGE.
304d20b5bwollman *
314d20b5bwollman *	From: @(#)if.h	8.1 (Berkeley) 6/10/93
323b842d3peter * $FreeBSD$
334d20b5bwollman */
344d20b5bwollman
354d20b5bwollman#ifndef	_NET_IF_VAR_H_
364d20b5bwollman#define	_NET_IF_VAR_H_
374d20b5bwollman
384d20b5bwollman/*
394d20b5bwollman * Structures defining a network interface, providing a packet
404d20b5bwollman * transport mechanism (ala level 0 of the PUP protocols).
414d20b5bwollman *
424d20b5bwollman * Each interface accepts output datagrams of a specified maximum
434d20b5bwollman * length, and provides higher level routines with input datagrams
444d20b5bwollman * received from its medium.
454d20b5bwollman *
464d20b5bwollman * Output occurs when the routine if_output is called, with three parameters:
47ebeaf16melifaro *	(*ifp->if_output)(ifp, m, dst, ro)
484d20b5bwollman * Here m is the mbuf chain to be sent and dst is the destination address.
494d20b5bwollman * The output routine encapsulates the supplied datagram if necessary,
504d20b5bwollman * and then transmits it on its medium.
514d20b5bwollman *
524d20b5bwollman * On input, each interface unwraps the data received by it, and either
53d3367c5schweikh * places it on the input queue of an internetwork datagram routine
544d20b5bwollman * and posts the associated software interrupt, or passes the datagram to a raw
554d20b5bwollman * packet input routine.
564d20b5bwollman *
574d20b5bwollman * Routines exist for locating interfaces by their addresses
58d3367c5schweikh * or for locating an interface on a certain network, as well as more general
594d20b5bwollman * routing and gateway routines maintaining information used to locate
604d20b5bwollman * interfaces.  These routines live in the files if.c and route.c
614d20b5bwollman */
624d20b5bwollman
63c829949glebiusstruct	rtentry;		/* ifa_rtrequest */
644d20b5bwollmanstruct	socket;
65e1d2263glebiusstruct	carp_if;
6627a36f6glebiusstruct	carp_softc;
6719f8b36glebiusstruct  ifvlantrunk;
68c829949glebiusstruct	route;			/* if_output */
698b1f382zecstruct	vnet;
70916c700marcelstruct	ifmedia;
713b5ede5glebiusstruct	netmap_adapter;
72f3a0ee4cemstruct	debugnet_methods;
734d20b5bwollman
745544076jlemon#ifdef _KERNEL
75250e158cem#include <sys/_eventhandler.h>
76c829949glebius#include <sys/mbuf.h>		/* ifqueue only? */
774b4aad0kmacy#include <sys/buf_ring.h>
7857ca458rwatson#include <net/vnet.h>
795544076jlemon#endif /* _KERNEL */
808e61308mmacy#include <sys/ck.h>
81790225cglebius#include <sys/counter.h>
828e61308mmacy#include <sys/epoch.h>
83b47bfbejhb#include <sys/lock.h>		/* XXX */
84c829949glebius#include <sys/mutex.h>		/* struct ifqueue */
85d0147f2kmacy#include <sys/rwlock.h>		/* XXX */
86ef8d755rwatson#include <sys/sx.h>		/* XXX */
87c829949glebius#include <sys/_task.h>		/* if_link_task */
88f1e94c6brooks#define	IF_DUNIT_NONE	-1
89f1e94c6brooks
90a29f5e7glebius#include <net/altq/if_altq.h>
91977d97bmlaier
92ecd6e9dmmacyCK_STAILQ_HEAD(ifnethead, ifnet);	/* we use TAILQs so that the order of */
937aeac9emmacyCK_STAILQ_HEAD(ifaddrhead, ifaddr);	/* instantiation is preserved in the list */
947aeac9emmacyCK_STAILQ_HEAD(ifmultihead, ifmultiaddr);
95ecd6e9dmmacyCK_STAILQ_HEAD(ifgrouphead, ifg_group);
964d20b5bwollman
97b490e05melifaro#ifdef _KERNEL
989978a7dglebiusVNET_DECLARE(struct pfil_head *, link_pfil_head);
999978a7dglebius#define	V_link_pfil_head	VNET(link_pfil_head)
1009978a7dglebius#define	PFIL_ETHER_NAME		"ethernet"
101d81208cae
102d81208cae#define	HHOOK_IPSEC_INET	0
103d81208cae#define	HHOOK_IPSEC_INET6	1
104d81208cae#define	HHOOK_IPSEC_COUNT	2
105d81208caeVNET_DECLARE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
106d81208caeVNET_DECLARE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
107d81208cae#define	V_ipsec_hhh_in	VNET(ipsec_hhh_in)
108d81208cae#define	V_ipsec_hhh_out	VNET(ipsec_hhh_out)
109b490e05melifaro#endif /* _KERNEL */
1101fbae66melifaro
11170b7c46glebiustypedef enum {
1122cb6078glebius	IFCOUNTER_IPACKETS = 0,
11370b7c46glebius	IFCOUNTER_IERRORS,
11470b7c46glebius	IFCOUNTER_OPACKETS,
11570b7c46glebius	IFCOUNTER_OERRORS,
11670b7c46glebius	IFCOUNTER_COLLISIONS,
11770b7c46glebius	IFCOUNTER_IBYTES,
11870b7c46glebius	IFCOUNTER_OBYTES,
11970b7c46glebius	IFCOUNTER_IMCASTS,
12070b7c46glebius	IFCOUNTER_OMCASTS,
12170b7c46glebius	IFCOUNTER_IQDROPS,
12270b7c46glebius	IFCOUNTER_OQDROPS,
12370b7c46glebius	IFCOUNTER_NOPROTO,
1242cb6078glebius	IFCOUNTERS /* Array size. */
125c2d27a8glebius} ift_counter;
12670b7c46glebius
127833eb3cglebiustypedef struct ifnet * if_t;
128916c700marcel
129833eb3cglebiustypedef	void (*if_start_fn_t)(if_t);
130833eb3cglebiustypedef	int (*if_ioctl_fn_t)(if_t, u_long, caddr_t);
131833eb3cglebiustypedef	void (*if_init_fn_t)(void *);
132833eb3cglebiustypedef void (*if_qflush_fn_t)(if_t);
133833eb3cglebiustypedef int (*if_transmit_fn_t)(if_t, struct mbuf *);
134c2d27a8glebiustypedef	uint64_t (*if_get_counter_t)(if_t, ift_counter);
135916c700marcel
136bdacf9bhselaskystruct ifnet_hw_tsomax {
137bdacf9bhselasky	u_int	tsomaxbytes;	/* TSO total burst length limit in bytes */
138bdacf9bhselasky	u_int	tsomaxsegcount;	/* TSO maximum segment count */
139bdacf9bhselasky	u_int	tsomaxsegsize;	/* TSO maximum segment size in bytes */
140bdacf9bhselasky};
141bdacf9bhselasky
14293152c6melifaro/* Interface encap request types */
14393152c6melifarotypedef enum {
14493152c6melifaro	IFENCAP_LL = 1			/* pre-calculate link-layer header */
14593152c6melifaro} ife_type;
14693152c6melifaro
14793152c6melifaro/*
14893152c6melifaro * The structure below allows to request various pre-calculated L2/L3 headers
14993152c6melifaro * for different media. Requests varies by type (rtype field).
15093152c6melifaro *
15193152c6melifaro * IFENCAP_LL type: pre-calculates link header based on address family
15293152c6melifaro *   and destination lladdr.
15393152c6melifaro *
15493152c6melifaro *   Input data fields:
15593152c6melifaro *     buf: pointer to destination buffer
15693152c6melifaro *     bufsize: buffer size
15793152c6melifaro *     flags: IFENCAP_FLAG_BROADCAST if destination is broadcast
15893152c6melifaro *     family: address family defined by AF_ constant.
15993152c6melifaro *     lladdr: pointer to link-layer address
16093152c6melifaro *     lladdr_len: length of link-layer address
16193152c6melifaro *     hdata: pointer to L3 header (optional, used for ARP requests).
16293152c6melifaro *   Output data fields:
16393152c6melifaro *     buf: encap data is stored here
16493152c6melifaro *     bufsize: resulting encap length is stored here
16593152c6melifaro *     lladdr_off: offset of link-layer address from encap hdr start
16693152c6melifaro *     hdata: L3 header may be altered if necessary
16793152c6melifaro */
16893152c6melifaro
16993152c6melifarostruct if_encap_req {
17093152c6melifaro	u_char		*buf;		/* Destination buffer (w) */
17193152c6melifaro	size_t		bufsize;	/* size of provided buffer (r) */
17293152c6melifaro	ife_type	rtype;		/* request type (r) */
17393152c6melifaro	uint32_t	flags;		/* Request flags (r) */
17493152c6melifaro	int		family;		/* Address family AF_* (r) */
17593152c6melifaro	int		lladdr_off;	/* offset from header start (w) */
17693152c6melifaro	int		lladdr_len;	/* lladdr length (r) */
17793152c6melifaro	char		*lladdr;	/* link-level address pointer (r) */
17893152c6melifaro	char		*hdata;		/* Upper layer header data (rw) */
17993152c6melifaro};
18093152c6melifaro
18193152c6melifaro#define	IFENCAP_FLAG_BROADCAST	0x02	/* Destination is broadcast */
18293152c6melifaro
183efa6326hselasky/*
184efa6326hselasky * Network interface send tag support. The storage of "struct
185efa6326hselasky * m_snd_tag" comes from the network driver and it is free to allocate
186efa6326hselasky * as much additional space as it wants for its own use.
187efa6326hselasky */
1881cf3162jhbstruct ktls_session;
189efa6326hselaskystruct m_snd_tag;
190efa6326hselasky
191efa6326hselasky#define	IF_SND_TAG_TYPE_RATE_LIMIT 0
1927c2ab1dhselasky#define	IF_SND_TAG_TYPE_UNLIMITED 1
1931cf3162jhb#define	IF_SND_TAG_TYPE_TLS 2
1941cf3162jhb#define	IF_SND_TAG_TYPE_MAX 3
195efa6326hselasky
196efa6326hselaskystruct if_snd_tag_alloc_header {
197efa6326hselasky	uint32_t type;		/* send tag type, see IF_SND_TAG_XXX */
198efa6326hselasky	uint32_t flowid;	/* mbuf hash value */
199efa6326hselasky	uint32_t flowtype;	/* mbuf hash type */
2003ea844bgallatin	uint8_t numa_domain;	/* numa domain of associated inp */
201efa6326hselasky};
202efa6326hselasky
203efa6326hselaskystruct if_snd_tag_alloc_rate_limit {
204efa6326hselasky	struct if_snd_tag_alloc_header hdr;
205efa6326hselasky	uint64_t max_rate;	/* in bytes/s */
2068a34b17rrs	uint32_t flags;		/* M_NOWAIT or M_WAITOK */
2078a34b17rrs	uint32_t reserved;	/* alignment */
208efa6326hselasky};
209efa6326hselasky
2101cf3162jhbstruct if_snd_tag_alloc_tls {
2111cf3162jhb	struct if_snd_tag_alloc_header hdr;
2121cf3162jhb	struct inpcb *inp;
2131cf3162jhb	const struct ktls_session *tls;
2141cf3162jhb};
2151cf3162jhb
216efa6326hselaskystruct if_snd_tag_rate_limit_params {
217efa6326hselasky	uint64_t max_rate;	/* in bytes/s */
2187c2ab1dhselasky	uint32_t queue_level;	/* 0 (empty) .. 65535 (full) */
2197c2ab1dhselasky#define	IF_SND_QUEUE_LEVEL_MIN 0
2207c2ab1dhselasky#define	IF_SND_QUEUE_LEVEL_MAX 65535
2218a34b17rrs	uint32_t flags;		/* M_NOWAIT or M_WAITOK */
222efa6326hselasky};
223efa6326hselasky
224efa6326hselaskyunion if_snd_tag_alloc_params {
225efa6326hselasky	struct if_snd_tag_alloc_header hdr;
226efa6326hselasky	struct if_snd_tag_alloc_rate_limit rate_limit;
2277c2ab1dhselasky	struct if_snd_tag_alloc_rate_limit unlimited;
2281cf3162jhb	struct if_snd_tag_alloc_tls tls;
229efa6326hselasky};
230efa6326hselasky
231efa6326hselaskyunion if_snd_tag_modify_params {
232efa6326hselasky	struct if_snd_tag_rate_limit_params rate_limit;
2337c2ab1dhselasky	struct if_snd_tag_rate_limit_params unlimited;
234efa6326hselasky};
235efa6326hselasky
236efa6326hselaskyunion if_snd_tag_query_params {
237efa6326hselasky	struct if_snd_tag_rate_limit_params rate_limit;
2387c2ab1dhselasky	struct if_snd_tag_rate_limit_params unlimited;
239efa6326hselasky};
240efa6326hselasky
2418a34b17rrs/* Query return flags */
2428a34b17rrs#define RT_NOSUPPORT	  0x00000000	/* Not supported */
2438a34b17rrs#define RT_IS_INDIRECT    0x00000001	/*
2448a34b17rrs					 * Interface like a lagg, select
2458a34b17rrs					 * the actual interface for
2468a34b17rrs					 * capabilities.
2478a34b17rrs					 */
2488a34b17rrs#define RT_IS_SELECTABLE  0x00000002	/*
2498a34b17rrs					 * No rate table, you select
2508a34b17rrs					 * rates and the first
2518a34b17rrs					 * number_of_rates are created.
2528a34b17rrs					 */
2538a34b17rrs#define RT_IS_FIXED_TABLE 0x00000004	/* A fixed table is attached */
2548a34b17rrs#define RT_IS_UNUSABLE	  0x00000008	/* It is not usable for this */
255cac424brrs#define RT_IS_SETUP_REQ	  0x00000010	/* The interface setup must be called before use */
2568a34b17rrs
2578a34b17rrsstruct if_ratelimit_query_results {
2588a34b17rrs	const uint64_t *rate_table;	/* Pointer to table if present */
2598a34b17rrs	uint32_t flags;			/* Flags indicating results */
2608a34b17rrs	uint32_t max_flows;		/* Max flows using, 0=unlimited */
2618a34b17rrs	uint32_t number_of_rates;	/* How many unique rates can be created */
2628a34b17rrs	uint32_t min_segment_burst;	/* The amount the adapter bursts at each send */
2638a34b17rrs};
2648a34b17rrs
265efa6326hselaskytypedef int (if_snd_tag_alloc_t)(struct ifnet *, union if_snd_tag_alloc_params *,
266efa6326hselasky    struct m_snd_tag **);
267efa6326hselaskytypedef int (if_snd_tag_modify_t)(struct m_snd_tag *, union if_snd_tag_modify_params *);
268efa6326hselaskytypedef int (if_snd_tag_query_t)(struct m_snd_tag *, union if_snd_tag_query_params *);
269efa6326hselaskytypedef void (if_snd_tag_free_t)(struct m_snd_tag *);
2708a34b17rrstypedef void (if_ratelimit_query_t)(struct ifnet *,
2718a34b17rrs    struct if_ratelimit_query_results *);
272cac424brrstypedef int (if_ratelimit_setup_t)(struct ifnet *, uint64_t, uint32_t);
27393152c6melifaro
2744d20b5bwollman/*
2754d20b5bwollman * Structure defining a network interface.
2764d20b5bwollman */
2774d20b5bwollmanstruct ifnet {
278d480a68andre	/* General book keeping of interface lists. */
279c937b51mmacy	CK_STAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained (CK_) */
280d480a68andre	LIST_ENTRY(ifnet) if_clones;	/* interfaces of a cloner */
281c937b51mmacy	CK_STAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if (CK_) */
282d480a68andre					/* protected by if_addr_lock */
283d480a68andre	u_char	if_alloctype;		/* if_type at time of allocation */
284accdb38gallatin	uint8_t	if_numa_domain;		/* NUMA domain of device */
285d480a68andre	/* Driver and protocol specific information that remains stable. */
2864d20b5bwollman	void	*if_softc;		/* pointer to driver state */
287d480a68andre	void	*if_llsoftc;		/* link layer softc */
288567ba9bbrooks	void	*if_l2com;		/* pointer to protocol bits */
289f1e94c6brooks	const char *if_dname;		/* driver name */
290f1e94c6brooks	int	if_dunit;		/* unit or IF_DUNIT_NONE */
291d480a68andre	u_short	if_index;		/* numeric abbreviation for this if  */
292d480a68andre	short	if_index_reserved;	/* spare space to grow if_index */
293d480a68andre	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
294d480a68andre	char	*if_description;	/* interface description */
295d480a68andre
296d480a68andre	/* Variable fields that are touched by the stack and drivers. */
297d480a68andre	int	if_flags;		/* up/down, broadcast, etc. */
2989dfcf3eglebius	int	if_drv_flags;		/* driver-managed status flags */
299d480a68andre	int	if_capabilities;	/* interface features & capabilities */
300d480a68andre	int	if_capenable;		/* enabled features & capabilities */
301d480a68andre	void	*if_linkmib;		/* link-type-specific MIB data */
302d480a68andre	size_t	if_linkmiblen;		/* length of above data */
303