udp_tunnel.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_UDP_TUNNEL_H
  3. #define __NET_UDP_TUNNEL_H
  4. #include <net/ip_tunnels.h>
  5. #include <net/udp.h>
  6. #if IS_ENABLED(CONFIG_IPV6)
  7. #include <net/ipv6.h>
  8. #include <net/ipv6_stubs.h>
  9. #endif
  10. struct udp_port_cfg {
  11. u8 family;
  12. /* Used only for kernel-created sockets */
  13. union {
  14. struct in_addr local_ip;
  15. #if IS_ENABLED(CONFIG_IPV6)
  16. struct in6_addr local_ip6;
  17. #endif
  18. };
  19. union {
  20. struct in_addr peer_ip;
  21. #if IS_ENABLED(CONFIG_IPV6)
  22. struct in6_addr peer_ip6;
  23. #endif
  24. };
  25. __be16 local_udp_port;
  26. __be16 peer_udp_port;
  27. int bind_ifindex;
  28. unsigned int use_udp_checksums:1,
  29. use_udp6_tx_checksums:1,
  30. use_udp6_rx_checksums:1,
  31. ipv6_v6only:1;
  32. };
  33. int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
  34. struct socket **sockp);
  35. #if IS_ENABLED(CONFIG_IPV6)
  36. int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
  37. struct socket **sockp);
  38. #else
  39. static inline int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
  40. struct socket **sockp)
  41. {
  42. return 0;
  43. }
  44. #endif
  45. static inline int udp_sock_create(struct net *net,
  46. struct udp_port_cfg *cfg,
  47. struct socket **sockp)
  48. {
  49. if (cfg->family == AF_INET)
  50. return udp_sock_create4(net, cfg, sockp);
  51. if (cfg->family == AF_INET6)
  52. return udp_sock_create6(net, cfg, sockp);
  53. return -EPFNOSUPPORT;
  54. }
  55. typedef int (*udp_tunnel_encap_rcv_t)(struct sock *sk, struct sk_buff *skb);
  56. typedef int (*udp_tunnel_encap_err_lookup_t)(struct sock *sk,
  57. struct sk_buff *skb);
  58. typedef void (*udp_tunnel_encap_err_rcv_t)(struct sock *sk,
  59. struct sk_buff *skb, int err,
  60. __be16 port, u32 info, u8 *payload);
  61. typedef void (*udp_tunnel_encap_destroy_t)(struct sock *sk);
  62. typedef struct sk_buff *(*udp_tunnel_gro_receive_t)(struct sock *sk,
  63. struct list_head *head,
  64. struct sk_buff *skb);
  65. typedef int (*udp_tunnel_gro_complete_t)(struct sock *sk, struct sk_buff *skb,
  66. int nhoff);
  67. struct udp_tunnel_sock_cfg {
  68. void *sk_user_data; /* user data used by encap_rcv call back */
  69. /* Used for setting up udp_sock fields, see udp.h for details */
  70. __u8 encap_type;
  71. udp_tunnel_encap_rcv_t encap_rcv;
  72. udp_tunnel_encap_err_lookup_t encap_err_lookup;
  73. udp_tunnel_encap_err_rcv_t encap_err_rcv;
  74. udp_tunnel_encap_destroy_t encap_destroy;
  75. udp_tunnel_gro_receive_t gro_receive;
  76. udp_tunnel_gro_complete_t gro_complete;
  77. };
  78. /* Setup the given (UDP) sock to receive UDP encapsulated packets */
  79. void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
  80. struct udp_tunnel_sock_cfg *sock_cfg);
  81. /* -- List of parsable UDP tunnel types --
  82. *
  83. * Adding to this list will result in serious debate. The main issue is
  84. * that this list is essentially a list of workarounds for either poorly
  85. * designed tunnels, or poorly designed device offloads.
  86. *
  87. * The parsing supported via these types should really be used for Rx
  88. * traffic only as the network stack will have already inserted offsets for
  89. * the location of the headers in the skb. In addition any ports that are
  90. * pushed should be kept within the namespace without leaking to other
  91. * devices such as VFs or other ports on the same device.
  92. *
  93. * It is strongly encouraged to use CHECKSUM_COMPLETE for Rx to avoid the
  94. * need to use this for Rx checksum offload. It should not be necessary to
  95. * call this function to perform Tx offloads on outgoing traffic.
  96. */
  97. enum udp_parsable_tunnel_type {
  98. UDP_TUNNEL_TYPE_VXLAN = BIT(0), /* RFC 7348 */
  99. UDP_TUNNEL_TYPE_GENEVE = BIT(1), /* draft-ietf-nvo3-geneve */
  100. UDP_TUNNEL_TYPE_VXLAN_GPE = BIT(2), /* draft-ietf-nvo3-vxlan-gpe */
  101. };
  102. struct udp_tunnel_info {
  103. unsigned short type;
  104. sa_family_t sa_family;
  105. __be16 port;
  106. u8 hw_priv;
  107. };
  108. /* Notify network devices of offloadable types */
  109. void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
  110. unsigned short type);
  111. void udp_tunnel_drop_rx_port(struct net_device *dev, struct socket *sock,
  112. unsigned short type);
  113. void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type);
  114. void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type);
  115. static inline void udp_tunnel_get_rx_info(struct net_device *dev)
  116. {
  117. ASSERT_RTNL();
  118. if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
  119. return;
  120. call_netdevice_notifiers(NETDEV_UDP_TUNNEL_PUSH_INFO, dev);
  121. }
  122. static inline void udp_tunnel_drop_rx_info(struct net_device *dev)
  123. {
  124. ASSERT_RTNL();
  125. if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
  126. return;
  127. call_netdevice_notifiers(NETDEV_UDP_TUNNEL_DROP_INFO, dev);
  128. }
  129. /* Transmit the skb using UDP encapsulation. */
  130. void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
  131. __be32 src, __be32 dst, __u8 tos, __u8 ttl,
  132. __be16 df, __be16 src_port, __be16 dst_port,
  133. bool xnet, bool nocheck);
  134. int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
  135. struct sk_buff *skb,
  136. struct net_device *dev,
  137. const struct in6_addr *saddr,
  138. const struct in6_addr *daddr,
  139. __u8 prio, __u8 ttl, __be32 label,
  140. __be16 src_port, __be16 dst_port, bool nocheck);
  141. void udp_tunnel_sock_release(struct socket *sock);
  142. struct rtable *udp_tunnel_dst_lookup(struct sk_buff *skb,
  143. struct net_device *dev,
  144. struct net *net, int oif,
  145. __be32 *saddr,
  146. const struct ip_tunnel_key *key,
  147. __be16 sport, __be16 dport, u8 tos,
  148. struct dst_cache *dst_cache);
  149. struct dst_entry *udp_tunnel6_dst_lookup(struct sk_buff *skb,
  150. struct net_device *dev,
  151. struct net *net,
  152. struct socket *sock, int oif,
  153. struct in6_addr *saddr,
  154. const struct ip_tunnel_key *key,
  155. __be16 sport, __be16 dport, u8 dsfield,
  156. struct dst_cache *dst_cache);
  157. struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
  158. const unsigned long *flags,
  159. __be64 tunnel_id, int md_size);
  160. #ifdef CONFIG_INET
  161. static inline int udp_tunnel_handle_offloads(struct sk_buff *skb, bool udp_csum)
  162. {
  163. int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  164. return iptunnel_handle_offloads(skb, type);
  165. }
  166. #endif
  167. static inline void udp_tunnel_encap_enable(struct sock *sk)
  168. {
  169. if (udp_test_and_set_bit(ENCAP_ENABLED, sk))
  170. return;
  171. #if IS_ENABLED(CONFIG_IPV6)
  172. if (READ_ONCE(sk->sk_family) == PF_INET6)
  173. ipv6_stub->udpv6_encap_enable();
  174. #endif
  175. udp_encap_enable();
  176. }
  177. #define UDP_TUNNEL_NIC_MAX_TABLES 4
  178. enum udp_tunnel_nic_info_flags {
  179. /* Device callbacks may sleep */
  180. UDP_TUNNEL_NIC_INFO_MAY_SLEEP = BIT(0),
  181. /* Device only supports offloads when it's open, all ports
  182. * will be removed before close and re-added after open.
  183. */
  184. UDP_TUNNEL_NIC_INFO_OPEN_ONLY = BIT(1),
  185. /* Device supports only IPv4 tunnels */
  186. UDP_TUNNEL_NIC_INFO_IPV4_ONLY = BIT(2),
  187. /* Device has hard-coded the IANA VXLAN port (4789) as VXLAN.
  188. * This port must not be counted towards n_entries of any table.
  189. * Driver will not receive any callback associated with port 4789.
  190. */
  191. UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN = BIT(3),
  192. };
  193. struct udp_tunnel_nic;
  194. #define UDP_TUNNEL_NIC_MAX_SHARING_DEVICES (U16_MAX / 2)
  195. struct udp_tunnel_nic_shared {
  196. struct udp_tunnel_nic *udp_tunnel_nic_info;
  197. struct list_head devices;
  198. };
  199. struct udp_tunnel_nic_shared_node {
  200. struct net_device *dev;
  201. struct list_head list;
  202. };
  203. /**
  204. * struct udp_tunnel_nic_info - driver UDP tunnel offload information
  205. * @set_port: callback for adding a new port
  206. * @unset_port: callback for removing a port
  207. * @sync_table: callback for syncing the entire port table at once
  208. * @shared: reference to device global state (optional)
  209. * @flags: device flags from enum udp_tunnel_nic_info_flags
  210. * @tables: UDP port tables this device has
  211. * @tables.n_entries: number of entries in this table
  212. * @tables.tunnel_types: types of tunnels this table accepts
  213. *
  214. * Drivers are expected to provide either @set_port and @unset_port callbacks
  215. * or the @sync_table callback. Callbacks are invoked with rtnl lock held.
  216. *
  217. * Devices which (misguidedly) share the UDP tunnel port table across multiple
  218. * netdevs should allocate an instance of struct udp_tunnel_nic_shared and
  219. * point @shared at it.
  220. * There must never be more than %UDP_TUNNEL_NIC_MAX_SHARING_DEVICES devices
  221. * sharing a table.
  222. *
  223. * Known limitations:
  224. * - UDP tunnel port notifications are fundamentally best-effort -
  225. * it is likely the driver will both see skbs which use a UDP tunnel port,
  226. * while not being a tunneled skb, and tunnel skbs from other ports -
  227. * drivers should only use these ports for non-critical RX-side offloads,
  228. * e.g. the checksum offload;
  229. * - none of the devices care about the socket family at present, so we don't
  230. * track it. Please extend this code if you care.
  231. */
  232. struct udp_tunnel_nic_info {
  233. /* one-by-one */
  234. int (*set_port)(struct net_device *dev,
  235. unsigned int table, unsigned int entry,
  236. struct udp_tunnel_info *ti);
  237. int (*unset_port)(struct net_device *dev,
  238. unsigned int table, unsigned int entry,
  239. struct udp_tunnel_info *ti);
  240. /* all at once */
  241. int (*sync_table)(struct net_device *dev, unsigned int table);
  242. struct udp_tunnel_nic_shared *shared;
  243. unsigned int flags;
  244. struct udp_tunnel_nic_table_info {
  245. unsigned int n_entries;
  246. unsigned int tunnel_types;
  247. } tables[UDP_TUNNEL_NIC_MAX_TABLES];
  248. };
  249. /* UDP tunnel module dependencies
  250. *
  251. * Tunnel drivers are expected to have a hard dependency on the udp_tunnel
  252. * module. NIC drivers are not, they just attach their
  253. * struct udp_tunnel_nic_info to the netdev and wait for callbacks to come.
  254. * Loading a tunnel driver will cause the udp_tunnel module to be loaded
  255. * and only then will all the required state structures be allocated.
  256. * Since we want a weak dependency from the drivers and the core to udp_tunnel
  257. * we call things through the following stubs.
  258. */
  259. struct udp_tunnel_nic_ops {
  260. void (*get_port)(struct net_device *dev, unsigned int table,
  261. unsigned int idx, struct udp_tunnel_info *ti);
  262. void (*set_port_priv)(struct net_device *dev, unsigned int table,
  263. unsigned int idx, u8 priv);
  264. void (*add_port)(struct net_device *dev, struct udp_tunnel_info *ti);
  265. void (*del_port)(struct net_device *dev, struct udp_tunnel_info *ti);
  266. void (*reset_ntf)(struct net_device *dev);
  267. size_t (*dump_size)(struct net_device *dev, unsigned int table);
  268. int (*dump_write)(struct net_device *dev, unsigned int table,
  269. struct sk_buff *skb);
  270. };
  271. #ifdef CONFIG_INET
  272. extern const struct udp_tunnel_nic_ops *udp_tunnel_nic_ops;
  273. #else
  274. #define udp_tunnel_nic_ops ((struct udp_tunnel_nic_ops *)NULL)
  275. #endif
  276. static inline void
  277. udp_tunnel_nic_get_port(struct net_device *dev, unsigned int table,
  278. unsigned int idx, struct udp_tunnel_info *ti)
  279. {
  280. /* This helper is used from .sync_table, we indicate empty entries
  281. * by zero'ed @ti. Drivers which need to know the details of a port
  282. * when it gets deleted should use the .set_port / .unset_port
  283. * callbacks.
  284. * Zero out here, otherwise !CONFIG_INET causes uninitilized warnings.
  285. */
  286. memset(ti, 0, sizeof(*ti));
  287. if (udp_tunnel_nic_ops)
  288. udp_tunnel_nic_ops->get_port(dev, table, idx, ti);
  289. }
  290. static inline void
  291. udp_tunnel_nic_set_port_priv(struct net_device *dev, unsigned int table,
  292. unsigned int idx, u8 priv)
  293. {
  294. if (udp_tunnel_nic_ops)
  295. udp_tunnel_nic_ops->set_port_priv(dev, table, idx, priv);
  296. }
  297. static inline void
  298. udp_tunnel_nic_add_port(struct net_device *dev, struct udp_tunnel_info *ti)
  299. {
  300. if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
  301. return;
  302. if (udp_tunnel_nic_ops)
  303. udp_tunnel_nic_ops->add_port(dev, ti);
  304. }
  305. static inline void
  306. udp_tunnel_nic_del_port(struct net_device *dev, struct udp_tunnel_info *ti)
  307. {
  308. if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
  309. return;
  310. if (udp_tunnel_nic_ops)
  311. udp_tunnel_nic_ops->del_port(dev, ti);
  312. }
  313. /**
  314. * udp_tunnel_nic_reset_ntf() - device-originating reset notification
  315. * @dev: network interface device structure
  316. *
  317. * Called by the driver to inform the core that the entire UDP tunnel port
  318. * state has been lost, usually due to device reset. Core will assume device
  319. * forgot all the ports and issue .set_port and .sync_table callbacks as
  320. * necessary.
  321. *
  322. * This function must be called with rtnl lock held, and will issue all
  323. * the callbacks before returning.
  324. */
  325. static inline void udp_tunnel_nic_reset_ntf(struct net_device *dev)
  326. {
  327. if (udp_tunnel_nic_ops)
  328. udp_tunnel_nic_ops->reset_ntf(dev);
  329. }
  330. static inline size_t
  331. udp_tunnel_nic_dump_size(struct net_device *dev, unsigned int table)
  332. {
  333. if (!udp_tunnel_nic_ops)
  334. return 0;
  335. return udp_tunnel_nic_ops->dump_size(dev, table);
  336. }
  337. static inline int
  338. udp_tunnel_nic_dump_write(struct net_device *dev, unsigned int table,
  339. struct sk_buff *skb)
  340. {
  341. if (!udp_tunnel_nic_ops)
  342. return 0;
  343. return udp_tunnel_nic_ops->dump_write(dev, table, skb);
  344. }
  345. #endif