xref: /linux/include/net/udp_tunnel.h (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
28024e028STom Herbert #ifndef __NET_UDP_TUNNEL_H
38024e028STom Herbert #define __NET_UDP_TUNNEL_H
48024e028STom Herbert 
56a93cc90SAndy Zhou #include <net/ip_tunnels.h>
66a93cc90SAndy Zhou #include <net/udp.h>
76a93cc90SAndy Zhou 
86a93cc90SAndy Zhou #if IS_ENABLED(CONFIG_IPV6)
96a93cc90SAndy Zhou #include <net/ipv6.h>
103616d08bSDavid Ahern #include <net/ipv6_stubs.h>
116a93cc90SAndy Zhou #endif
126a93cc90SAndy Zhou 
138024e028STom Herbert struct udp_port_cfg {
148024e028STom Herbert 	u8			family;
158024e028STom Herbert 
168024e028STom Herbert 	/* Used only for kernel-created sockets */
178024e028STom Herbert 	union {
188024e028STom Herbert 		struct in_addr		local_ip;
198024e028STom Herbert #if IS_ENABLED(CONFIG_IPV6)
208024e028STom Herbert 		struct in6_addr		local_ip6;
218024e028STom Herbert #endif
228024e028STom Herbert 	};
238024e028STom Herbert 
248024e028STom Herbert 	union {
258024e028STom Herbert 		struct in_addr		peer_ip;
268024e028STom Herbert #if IS_ENABLED(CONFIG_IPV6)
278024e028STom Herbert 		struct in6_addr		peer_ip6;
288024e028STom Herbert #endif
298024e028STom Herbert 	};
308024e028STom Herbert 
318024e028STom Herbert 	__be16			local_udp_port;
328024e028STom Herbert 	__be16			peer_udp_port;
33da5095d0SAlexis Bauvin 	int			bind_ifindex;
348024e028STom Herbert 	unsigned int		use_udp_checksums:1,
358024e028STom Herbert 				use_udp6_tx_checksums:1,
36a43a9ef6SJiri Benc 				use_udp6_rx_checksums:1,
37a43a9ef6SJiri Benc 				ipv6_v6only:1;
388024e028STom Herbert };
398024e028STom Herbert 
40fd384412SAndy Zhou int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
418024e028STom Herbert 		     struct socket **sockp);
428024e028STom Herbert 
43fd384412SAndy Zhou #if IS_ENABLED(CONFIG_IPV6)
44fd384412SAndy Zhou int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
45fd384412SAndy Zhou 		     struct socket **sockp);
46fd384412SAndy Zhou #else
udp_sock_create6(struct net * net,struct udp_port_cfg * cfg,struct socket ** sockp)47fd384412SAndy Zhou static inline int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
48fd384412SAndy Zhou 				   struct socket **sockp)
49fd384412SAndy Zhou {
50fd384412SAndy Zhou 	return 0;
51fd384412SAndy Zhou }
52fd384412SAndy Zhou #endif
53fd384412SAndy Zhou 
udp_sock_create(struct net * net,struct udp_port_cfg * cfg,struct socket ** sockp)54fd384412SAndy Zhou static inline int udp_sock_create(struct net *net,
55fd384412SAndy Zhou 				  struct udp_port_cfg *cfg,
56fd384412SAndy Zhou 				  struct socket **sockp)
57fd384412SAndy Zhou {
58fd384412SAndy Zhou 	if (cfg->family == AF_INET)
59fd384412SAndy Zhou 		return udp_sock_create4(net, cfg, sockp);
60fd384412SAndy Zhou 
61fd384412SAndy Zhou 	if (cfg->family == AF_INET6)
62fd384412SAndy Zhou 		return udp_sock_create6(net, cfg, sockp);
63fd384412SAndy Zhou 
64fd384412SAndy Zhou 	return -EPFNOSUPPORT;
65fd384412SAndy Zhou }
66fd384412SAndy Zhou 
676a93cc90SAndy Zhou typedef int (*udp_tunnel_encap_rcv_t)(struct sock *sk, struct sk_buff *skb);
68a36e185eSStefano Brivio typedef int (*udp_tunnel_encap_err_lookup_t)(struct sock *sk,
69a36e185eSStefano Brivio 					     struct sk_buff *skb);
70ac56a0b4SDavid Howells typedef void (*udp_tunnel_encap_err_rcv_t)(struct sock *sk,
7142fb06b3SDavid Howells 					   struct sk_buff *skb, int err,
7242fb06b3SDavid Howells 					   __be16 port, u32 info, u8 *payload);
736a93cc90SAndy Zhou typedef void (*udp_tunnel_encap_destroy_t)(struct sock *sk);
74d4546c25SDavid Miller typedef struct sk_buff *(*udp_tunnel_gro_receive_t)(struct sock *sk,
75d4546c25SDavid Miller 						    struct list_head *head,
7638fd2af2STom Herbert 						    struct sk_buff *skb);
7738fd2af2STom Herbert typedef int (*udp_tunnel_gro_complete_t)(struct sock *sk, struct sk_buff *skb,
7838fd2af2STom Herbert 					 int nhoff);
796a93cc90SAndy Zhou 
806a93cc90SAndy Zhou struct udp_tunnel_sock_cfg {
816a93cc90SAndy Zhou 	void *sk_user_data;     /* user data used by encap_rcv call back */
826a93cc90SAndy Zhou 	/* Used for setting up udp_sock fields, see udp.h for details */
836a93cc90SAndy Zhou 	__u8  encap_type;
846a93cc90SAndy Zhou 	udp_tunnel_encap_rcv_t encap_rcv;
85a36e185eSStefano Brivio 	udp_tunnel_encap_err_lookup_t encap_err_lookup;
86ac56a0b4SDavid Howells 	udp_tunnel_encap_err_rcv_t encap_err_rcv;
876a93cc90SAndy Zhou 	udp_tunnel_encap_destroy_t encap_destroy;
8838fd2af2STom Herbert 	udp_tunnel_gro_receive_t gro_receive;
8938fd2af2STom Herbert 	udp_tunnel_gro_complete_t gro_complete;
906a93cc90SAndy Zhou };
916a93cc90SAndy Zhou 
926a93cc90SAndy Zhou /* Setup the given (UDP) sock to receive UDP encapsulated packets */
936a93cc90SAndy Zhou void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
946a93cc90SAndy Zhou 			   struct udp_tunnel_sock_cfg *sock_cfg);
956a93cc90SAndy Zhou 
96e7b3db5eSAlexander Duyck /* -- List of parsable UDP tunnel types --
97e7b3db5eSAlexander Duyck  *
98e7b3db5eSAlexander Duyck  * Adding to this list will result in serious debate.  The main issue is
99e7b3db5eSAlexander Duyck  * that this list is essentially a list of workarounds for either poorly
100e7b3db5eSAlexander Duyck  * designed tunnels, or poorly designed device offloads.
101e7b3db5eSAlexander Duyck  *
102e7b3db5eSAlexander Duyck  * The parsing supported via these types should really be used for Rx
103e7b3db5eSAlexander Duyck  * traffic only as the network stack will have already inserted offsets for
104e7b3db5eSAlexander Duyck  * the location of the headers in the skb.  In addition any ports that are
105e7b3db5eSAlexander Duyck  * pushed should be kept within the namespace without leaking to other
106e7b3db5eSAlexander Duyck  * devices such as VFs or other ports on the same device.
107e7b3db5eSAlexander Duyck  *
108e7b3db5eSAlexander Duyck  * It is strongly encouraged to use CHECKSUM_COMPLETE for Rx to avoid the
109e7b3db5eSAlexander Duyck  * need to use this for Rx checksum offload.  It should not be necessary to
110e7b3db5eSAlexander Duyck  * call this function to perform Tx offloads on outgoing traffic.
111e7b3db5eSAlexander Duyck  */
112e7b3db5eSAlexander Duyck enum udp_parsable_tunnel_type {
11384a4160eSJakub Kicinski 	UDP_TUNNEL_TYPE_VXLAN	  = BIT(0), /* RFC 7348 */
11484a4160eSJakub Kicinski 	UDP_TUNNEL_TYPE_GENEVE	  = BIT(1), /* draft-ietf-nvo3-geneve */
11584a4160eSJakub Kicinski 	UDP_TUNNEL_TYPE_VXLAN_GPE = BIT(2), /* draft-ietf-nvo3-vxlan-gpe */
116e7b3db5eSAlexander Duyck };
117e7b3db5eSAlexander Duyck 
118e7b3db5eSAlexander Duyck struct udp_tunnel_info {
119e7b3db5eSAlexander Duyck 	unsigned short type;
120e7b3db5eSAlexander Duyck 	sa_family_t sa_family;
121e7b3db5eSAlexander Duyck 	__be16 port;
122cc4e3835SJakub Kicinski 	u8 hw_priv;
123e7b3db5eSAlexander Duyck };
124e7b3db5eSAlexander Duyck 
125e7b3db5eSAlexander Duyck /* Notify network devices of offloadable types */
126e7b3db5eSAlexander Duyck void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
127e7b3db5eSAlexander Duyck 			     unsigned short type);
128296d8ee3SSabrina Dubroca void udp_tunnel_drop_rx_port(struct net_device *dev, struct socket *sock,
129296d8ee3SSabrina Dubroca 			     unsigned short type);
130e7b3db5eSAlexander Duyck void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type);
131e7b3db5eSAlexander Duyck void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type);
132e7b3db5eSAlexander Duyck 
udp_tunnel_get_rx_info(struct net_device * dev)1337c46a640SAlexander Duyck static inline void udp_tunnel_get_rx_info(struct net_device *dev)
1347c46a640SAlexander Duyck {
1357c46a640SAlexander Duyck 	ASSERT_RTNL();
136b9ef3fecSJakub Kicinski 	if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
137b9ef3fecSJakub Kicinski 		return;
1387c46a640SAlexander Duyck 	call_netdevice_notifiers(NETDEV_UDP_TUNNEL_PUSH_INFO, dev);
1397c46a640SAlexander Duyck }
1407c46a640SAlexander Duyck 
udp_tunnel_drop_rx_info(struct net_device * dev)141296d8ee3SSabrina Dubroca static inline void udp_tunnel_drop_rx_info(struct net_device *dev)
142296d8ee3SSabrina Dubroca {
143296d8ee3SSabrina Dubroca 	ASSERT_RTNL();
144b9ef3fecSJakub Kicinski 	if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
145b9ef3fecSJakub Kicinski 		return;
146296d8ee3SSabrina Dubroca 	call_netdevice_notifiers(NETDEV_UDP_TUNNEL_DROP_INFO, dev);
147296d8ee3SSabrina Dubroca }
148296d8ee3SSabrina Dubroca 
1496a93cc90SAndy Zhou /* Transmit the skb using UDP encapsulation. */
150039f5062SPravin B Shelar void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
151d998f8efSTom Herbert 			 __be32 src, __be32 dst, __u8 tos, __u8 ttl,
152d998f8efSTom Herbert 			 __be16 df, __be16 src_port, __be16 dst_port,
153d998f8efSTom Herbert 			 bool xnet, bool nocheck);
1546a93cc90SAndy Zhou 
15579b16aadSDavid Miller int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
15679b16aadSDavid Miller 			 struct sk_buff *skb,
157f25e621fSBeniamino Galvani 			 struct net_device *dev,
158f25e621fSBeniamino Galvani 			 const struct in6_addr *saddr,
159f25e621fSBeniamino Galvani 			 const struct in6_addr *daddr,
16013461144SDaniel Borkmann 			 __u8 prio, __u8 ttl, __be32 label,
16113461144SDaniel Borkmann 			 __be16 src_port, __be16 dst_port, bool nocheck);
1626a93cc90SAndy Zhou 
1636a93cc90SAndy Zhou void udp_tunnel_sock_release(struct socket *sock);
1646a93cc90SAndy Zhou 
165bf3fcbf7SBeniamino Galvani struct rtable *udp_tunnel_dst_lookup(struct sk_buff *skb,
166bf3fcbf7SBeniamino Galvani 				     struct net_device *dev,
16772fc68c6SBeniamino Galvani 				     struct net *net, int oif,
16872fc68c6SBeniamino Galvani 				     __be32 *saddr,
16972fc68c6SBeniamino Galvani 				     const struct ip_tunnel_key *key,
17072fc68c6SBeniamino Galvani 				     __be16 sport, __be16 dport, u8 tos,
17172fc68c6SBeniamino Galvani 				     struct dst_cache *dst_cache);
172fc47e86dSBeniamino Galvani struct dst_entry *udp_tunnel6_dst_lookup(struct sk_buff *skb,
173fc47e86dSBeniamino Galvani 					 struct net_device *dev,
174fc47e86dSBeniamino Galvani 					 struct net *net,
175946fcfdbSBeniamino Galvani 					 struct socket *sock, int oif,
176fc47e86dSBeniamino Galvani 					 struct in6_addr *saddr,
177946fcfdbSBeniamino Galvani 					 const struct ip_tunnel_key *key,
178946fcfdbSBeniamino Galvani 					 __be16 sport, __be16 dport, u8 dsfield,
179946fcfdbSBeniamino Galvani 					 struct dst_cache *dst_cache);
180bf3fcbf7SBeniamino Galvani 
181c29a70d2SPravin B Shelar struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
182*5832c4a7SAlexander Lobakin 				    const unsigned long *flags,
183*5832c4a7SAlexander Lobakin 				    __be64 tunnel_id, int md_size);
184c29a70d2SPravin B Shelar 
18586a98057SAlexander Duyck #ifdef CONFIG_INET
udp_tunnel_handle_offloads(struct sk_buff * skb,bool udp_csum)186aed069dfSAlexander Duyck static inline int udp_tunnel_handle_offloads(struct sk_buff *skb, bool udp_csum)
1876a93cc90SAndy Zhou {
1886a93cc90SAndy Zhou 	int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1896a93cc90SAndy Zhou 
1906fa79666SEdward Cree 	return iptunnel_handle_offloads(skb, type);
1916a93cc90SAndy Zhou }
19286a98057SAlexander Duyck #endif
1936a93cc90SAndy Zhou 
udp_tunnel_encap_enable(struct sock * sk)194ac9a7f4cSEric Dumazet static inline void udp_tunnel_encap_enable(struct sock *sk)
1956a93cc90SAndy Zhou {
196ac9a7f4cSEric Dumazet 	if (udp_test_and_set_bit(ENCAP_ENABLED, sk))
19760fb9567SPaolo Abeni 		return;
19860fb9567SPaolo Abeni 
1996a93cc90SAndy Zhou #if IS_ENABLED(CONFIG_IPV6)
200ac9a7f4cSEric Dumazet 	if (READ_ONCE(sk->sk_family) == PF_INET6)
2016a93cc90SAndy Zhou 		ipv6_stub->udpv6_encap_enable();
2026a93cc90SAndy Zhou #endif
2036a93cc90SAndy Zhou 	udp_encap_enable();
2046a93cc90SAndy Zhou }
2056a93cc90SAndy Zhou 
206cc4e3835SJakub Kicinski #define UDP_TUNNEL_NIC_MAX_TABLES	4
207cc4e3835SJakub Kicinski 
208cc4e3835SJakub Kicinski enum udp_tunnel_nic_info_flags {
209cc4e3835SJakub Kicinski 	/* Device callbacks may sleep */
210cc4e3835SJakub Kicinski 	UDP_TUNNEL_NIC_INFO_MAY_SLEEP	= BIT(0),
211cc4e3835SJakub Kicinski 	/* Device only supports offloads when it's open, all ports
212cc4e3835SJakub Kicinski 	 * will be removed before close and re-added after open.
213cc4e3835SJakub Kicinski 	 */
214cc4e3835SJakub Kicinski 	UDP_TUNNEL_NIC_INFO_OPEN_ONLY	= BIT(1),
215cc4e3835SJakub Kicinski 	/* Device supports only IPv4 tunnels */
216cc4e3835SJakub Kicinski 	UDP_TUNNEL_NIC_INFO_IPV4_ONLY	= BIT(2),
217966e5059SJakub Kicinski 	/* Device has hard-coded the IANA VXLAN port (4789) as VXLAN.
218966e5059SJakub Kicinski 	 * This port must not be counted towards n_entries of any table.
219966e5059SJakub Kicinski 	 * Driver will not receive any callback associated with port 4789.
220966e5059SJakub Kicinski 	 */
221966e5059SJakub Kicinski 	UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN	= BIT(3),
222cc4e3835SJakub Kicinski };
223cc4e3835SJakub Kicinski 
22474cc6d18SJakub Kicinski struct udp_tunnel_nic;
22574cc6d18SJakub Kicinski 
22674cc6d18SJakub Kicinski #define UDP_TUNNEL_NIC_MAX_SHARING_DEVICES	(U16_MAX / 2)
22774cc6d18SJakub Kicinski 
22874cc6d18SJakub Kicinski struct udp_tunnel_nic_shared {
22974cc6d18SJakub Kicinski 	struct udp_tunnel_nic *udp_tunnel_nic_info;
23074cc6d18SJakub Kicinski 
23174cc6d18SJakub Kicinski 	struct list_head devices;
23274cc6d18SJakub Kicinski };
23374cc6d18SJakub Kicinski 
23474cc6d18SJakub Kicinski struct udp_tunnel_nic_shared_node {
23574cc6d18SJakub Kicinski 	struct net_device *dev;
23674cc6d18SJakub Kicinski 	struct list_head list;
23774cc6d18SJakub Kicinski };
23874cc6d18SJakub Kicinski 
239cc4e3835SJakub Kicinski /**
240cc4e3835SJakub Kicinski  * struct udp_tunnel_nic_info - driver UDP tunnel offload information
241cc4e3835SJakub Kicinski  * @set_port:	callback for adding a new port
242cc4e3835SJakub Kicinski  * @unset_port:	callback for removing a port
243cc4e3835SJakub Kicinski  * @sync_table:	callback for syncing the entire port table at once
24474cc6d18SJakub Kicinski  * @shared:	reference to device global state (optional)
245cc4e3835SJakub Kicinski  * @flags:	device flags from enum udp_tunnel_nic_info_flags
246cc4e3835SJakub Kicinski  * @tables:	UDP port tables this device has
247cc4e3835SJakub Kicinski  * @tables.n_entries:		number of entries in this table
248cc4e3835SJakub Kicinski  * @tables.tunnel_types:	types of tunnels this table accepts
249cc4e3835SJakub Kicinski  *
250cc4e3835SJakub Kicinski  * Drivers are expected to provide either @set_port and @unset_port callbacks
251cc4e3835SJakub Kicinski  * or the @sync_table callback. Callbacks are invoked with rtnl lock held.
252cc4e3835SJakub Kicinski  *
25374cc6d18SJakub Kicinski  * Devices which (misguidedly) share the UDP tunnel port table across multiple
25474cc6d18SJakub Kicinski  * netdevs should allocate an instance of struct udp_tunnel_nic_shared and
25574cc6d18SJakub Kicinski  * point @shared at it.
25674cc6d18SJakub Kicinski  * There must never be more than %UDP_TUNNEL_NIC_MAX_SHARING_DEVICES devices
25774cc6d18SJakub Kicinski  * sharing a table.
25874cc6d18SJakub Kicinski  *
259cc4e3835SJakub Kicinski  * Known limitations:
260cc4e3835SJakub Kicinski  *  - UDP tunnel port notifications are fundamentally best-effort -
261cc4e3835SJakub Kicinski  *    it is likely the driver will both see skbs which use a UDP tunnel port,
262cc4e3835SJakub Kicinski  *    while not being a tunneled skb, and tunnel skbs from other ports -
263cc4e3835SJakub Kicinski  *    drivers should only use these ports for non-critical RX-side offloads,
264cc4e3835SJakub Kicinski  *    e.g. the checksum offload;
265cc4e3835SJakub Kicinski  *  - none of the devices care about the socket family at present, so we don't
266cc4e3835SJakub Kicinski  *    track it. Please extend this code if you care.
267cc4e3835SJakub Kicinski  */
268cc4e3835SJakub Kicinski struct udp_tunnel_nic_info {
269cc4e3835SJakub Kicinski 	/* one-by-one */
270cc4e3835SJakub Kicinski 	int (*set_port)(struct net_device *dev,
271cc4e3835SJakub Kicinski 			unsigned int table, unsigned int entry,
272cc4e3835SJakub Kicinski 			struct udp_tunnel_info *ti);
273cc4e3835SJakub Kicinski 	int (*unset_port)(struct net_device *dev,
274cc4e3835SJakub Kicinski 			  unsigned int table, unsigned int entry,
275cc4e3835SJakub Kicinski 			  struct udp_tunnel_info *ti);
276cc4e3835SJakub Kicinski 
277cc4e3835SJakub Kicinski 	/* all at once */
278cc4e3835SJakub Kicinski 	int (*sync_table)(struct net_device *dev, unsigned int table);
279cc4e3835SJakub Kicinski 
28074cc6d18SJakub Kicinski 	struct udp_tunnel_nic_shared *shared;
28174cc6d18SJakub Kicinski 
282cc4e3835SJakub Kicinski 	unsigned int flags;
283cc4e3835SJakub Kicinski 
284cc4e3835SJakub Kicinski 	struct udp_tunnel_nic_table_info {
285cc4e3835SJakub Kicinski 		unsigned int n_entries;
286cc4e3835SJakub Kicinski 		unsigned int tunnel_types;
287cc4e3835SJakub Kicinski 	} tables[UDP_TUNNEL_NIC_MAX_TABLES];
288cc4e3835SJakub Kicinski };
289cc4e3835SJakub Kicinski 
290cc4e3835SJakub Kicinski /* UDP tunnel module dependencies
291cc4e3835SJakub Kicinski  *
292cc4e3835SJakub Kicinski  * Tunnel drivers are expected to have a hard dependency on the udp_tunnel
293cc4e3835SJakub Kicinski  * module. NIC drivers are not, they just attach their
294cc4e3835SJakub Kicinski  * struct udp_tunnel_nic_info to the netdev and wait for callbacks to come.
295cc4e3835SJakub Kicinski  * Loading a tunnel driver will cause the udp_tunnel module to be loaded
296cc4e3835SJakub Kicinski  * and only then will all the required state structures be allocated.
297cc4e3835SJakub Kicinski  * Since we want a weak dependency from the drivers and the core to udp_tunnel
298cc4e3835SJakub Kicinski  * we call things through the following stubs.
299cc4e3835SJakub Kicinski  */
300cc4e3835SJakub Kicinski struct udp_tunnel_nic_ops {
301cc4e3835SJakub Kicinski 	void (*get_port)(struct net_device *dev, unsigned int table,
302cc4e3835SJakub Kicinski 			 unsigned int idx, struct udp_tunnel_info *ti);
303cc4e3835SJakub Kicinski 	void (*set_port_priv)(struct net_device *dev, unsigned int table,
304cc4e3835SJakub Kicinski 			      unsigned int idx, u8 priv);
305cc4e3835SJakub Kicinski 	void (*add_port)(struct net_device *dev, struct udp_tunnel_info *ti);
306cc4e3835SJakub Kicinski 	void (*del_port)(struct net_device *dev, struct udp_tunnel_info *ti);
307cc4e3835SJakub Kicinski 	void (*reset_ntf)(struct net_device *dev);
308c7d759ebSJakub Kicinski 
309c7d759ebSJakub Kicinski 	size_t (*dump_size)(struct net_device *dev, unsigned int table);
310c7d759ebSJakub Kicinski 	int (*dump_write)(struct net_device *dev, unsigned int table,
311c7d759ebSJakub Kicinski 			  struct sk_buff *skb);
312cc4e3835SJakub Kicinski };
313cc4e3835SJakub Kicinski 
314cc4e3835SJakub Kicinski #ifdef CONFIG_INET
315cc4e3835SJakub Kicinski extern const struct udp_tunnel_nic_ops *udp_tunnel_nic_ops;
316cc4e3835SJakub Kicinski #else
317cc4e3835SJakub Kicinski #define udp_tunnel_nic_ops	((struct udp_tunnel_nic_ops *)NULL)
318cc4e3835SJakub Kicinski #endif
319cc4e3835SJakub Kicinski 
320cc4e3835SJakub Kicinski static inline void
udp_tunnel_nic_get_port(struct net_device * dev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)321cc4e3835SJakub Kicinski udp_tunnel_nic_get_port(struct net_device *dev, unsigned int table,
322cc4e3835SJakub Kicinski 			unsigned int idx, struct udp_tunnel_info *ti)
323cc4e3835SJakub Kicinski {
324cc4e3835SJakub Kicinski 	/* This helper is used from .sync_table, we indicate empty entries
325cc4e3835SJakub Kicinski 	 * by zero'ed @ti. Drivers which need to know the details of a port
326cc4e3835SJakub Kicinski 	 * when it gets deleted should use the .set_port / .unset_port
327cc4e3835SJakub Kicinski 	 * callbacks.
328cc4e3835SJakub Kicinski 	 * Zero out here, otherwise !CONFIG_INET causes uninitilized warnings.
329cc4e3835SJakub Kicinski 	 */
330cc4e3835SJakub Kicinski 	memset(ti, 0, sizeof(*ti));
331cc4e3835SJakub Kicinski 
332cc4e3835SJakub Kicinski 	if (udp_tunnel_nic_ops)
333cc4e3835SJakub Kicinski 		udp_tunnel_nic_ops->get_port(dev, table, idx, ti);
334cc4e3835SJakub Kicinski }
335cc4e3835SJakub Kicinski 
336cc4e3835SJakub Kicinski static inline void
udp_tunnel_nic_set_port_priv(struct net_device * dev,unsigned int table,unsigned int idx,u8 priv)337cc4e3835SJakub Kicinski udp_tunnel_nic_set_port_priv(struct net_device *dev, unsigned int table,
338cc4e3835SJakub Kicinski 			     unsigned int idx, u8 priv)
339cc4e3835SJakub Kicinski {
340cc4e3835SJakub Kicinski 	if (udp_tunnel_nic_ops)
341cc4e3835SJakub Kicinski 		udp_tunnel_nic_ops->set_port_priv(dev, table, idx, priv);
342cc4e3835SJakub Kicinski }
343cc4e3835SJakub Kicinski 
344cc4e3835SJakub Kicinski static inline void
udp_tunnel_nic_add_port(struct net_device * dev,struct udp_tunnel_info * ti)345cc4e3835SJakub Kicinski udp_tunnel_nic_add_port(struct net_device *dev, struct udp_tunnel_info *ti)
346cc4e3835SJakub Kicinski {
347b9ef3fecSJakub Kicinski 	if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
348b9ef3fecSJakub Kicinski 		return;
349cc4e3835SJakub Kicinski 	if (udp_tunnel_nic_ops)
350cc4e3835SJakub Kicinski 		udp_tunnel_nic_ops->add_port(dev, ti);
351cc4e3835SJakub Kicinski }
352cc4e3835SJakub Kicinski 
353cc4e3835SJakub Kicinski static inline void
udp_tunnel_nic_del_port(struct net_device * dev,struct udp_tunnel_info * ti)354cc4e3835SJakub Kicinski udp_tunnel_nic_del_port(struct net_device *dev, struct udp_tunnel_info *ti)
355cc4e3835SJakub Kicinski {
356b9ef3fecSJakub Kicinski 	if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
357b9ef3fecSJakub Kicinski 		return;
358cc4e3835SJakub Kicinski 	if (udp_tunnel_nic_ops)
359cc4e3835SJakub Kicinski 		udp_tunnel_nic_ops->del_port(dev, ti);
360cc4e3835SJakub Kicinski }
361cc4e3835SJakub Kicinski 
362cc4e3835SJakub Kicinski /**
363cc4e3835SJakub Kicinski  * udp_tunnel_nic_reset_ntf() - device-originating reset notification
364cc4e3835SJakub Kicinski  * @dev: network interface device structure
365cc4e3835SJakub Kicinski  *
366cc4e3835SJakub Kicinski  * Called by the driver to inform the core that the entire UDP tunnel port
367cc4e3835SJakub Kicinski  * state has been lost, usually due to device reset. Core will assume device
368cc4e3835SJakub Kicinski  * forgot all the ports and issue .set_port and .sync_table callbacks as
369cc4e3835SJakub Kicinski  * necessary.
370cc4e3835SJakub Kicinski  *
371cc4e3835SJakub Kicinski  * This function must be called with rtnl lock held, and will issue all
372cc4e3835SJakub Kicinski  * the callbacks before returning.
373cc4e3835SJakub Kicinski  */
udp_tunnel_nic_reset_ntf(struct net_device * dev)374cc4e3835SJakub Kicinski static inline void udp_tunnel_nic_reset_ntf(struct net_device *dev)
375cc4e3835SJakub Kicinski {
376cc4e3835SJakub Kicinski 	if (udp_tunnel_nic_ops)
377cc4e3835SJakub Kicinski 		udp_tunnel_nic_ops->reset_ntf(dev);
378cc4e3835SJakub Kicinski }
379c7d759ebSJakub Kicinski 
380c7d759ebSJakub Kicinski static inline size_t
udp_tunnel_nic_dump_size(struct net_device * dev,unsigned int table)381c7d759ebSJakub Kicinski udp_tunnel_nic_dump_size(struct net_device *dev, unsigned int table)
382c7d759ebSJakub Kicinski {
383c7d759ebSJakub Kicinski 	if (!udp_tunnel_nic_ops)
384c7d759ebSJakub Kicinski 		return 0;
385c7d759ebSJakub Kicinski 	return udp_tunnel_nic_ops->dump_size(dev, table);
386c7d759ebSJakub Kicinski }
387c7d759ebSJakub Kicinski 
388c7d759ebSJakub Kicinski static inline int
udp_tunnel_nic_dump_write(struct net_device * dev,unsigned int table,struct sk_buff * skb)389c7d759ebSJakub Kicinski udp_tunnel_nic_dump_write(struct net_device *dev, unsigned int table,
390c7d759ebSJakub Kicinski 			  struct sk_buff *skb)
391c7d759ebSJakub Kicinski {
392c7d759ebSJakub Kicinski 	if (!udp_tunnel_nic_ops)
393c7d759ebSJakub Kicinski 		return 0;
394c7d759ebSJakub Kicinski 	return udp_tunnel_nic_ops->dump_write(dev, table, skb);
395c7d759ebSJakub Kicinski }
3968024e028STom Herbert #endif
397