udp_tunnel.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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,
  60. unsigned int udp_offset);
  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, struct in6_addr *saddr,
  137. struct in6_addr *daddr,
  138. __u8 prio, __u8 ttl, __be32 label,
  139. __be16 src_port, __be16 dst_port, bool nocheck);
  140. void udp_tunnel_sock_release(struct socket *sock);
  141. struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
  142. __be16 flags, __be64 tunnel_id,
  143. int md_size);
  144. #ifdef CONFIG_INET
  145. static inline int udp_tunnel_handle_offloads(struct sk_buff *skb, bool udp_csum)
  146. {
  147. int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  148. return iptunnel_handle_offloads(skb, type);
  149. }
  150. #endif
  151. static inline void udp_tunnel_encap_enable(struct socket *sock)
  152. {
  153. struct udp_sock *up = udp_sk(sock->sk);
  154. if (up->encap_enabled)
  155. return;
  156. up->encap_enabled = 1;
  157. #if IS_ENABLED(CONFIG_IPV6)
  158. if (sock->sk->sk_family == PF_INET6)
  159. ipv6_stub->udpv6_encap_enable();
  160. #endif
  161. udp_encap_enable();
  162. }
  163. #define UDP_TUNNEL_NIC_MAX_TABLES 4
  164. enum udp_tunnel_nic_info_flags {
  165. /* Device callbacks may sleep */
  166. UDP_TUNNEL_NIC_INFO_MAY_SLEEP = BIT(0),
  167. /* Device only supports offloads when it's open, all ports
  168. * will be removed before close and re-added after open.
  169. */
  170. UDP_TUNNEL_NIC_INFO_OPEN_ONLY = BIT(1),
  171. /* Device supports only IPv4 tunnels */
  172. UDP_TUNNEL_NIC_INFO_IPV4_ONLY = BIT(2),
  173. /* Device has hard-coded the IANA VXLAN port (4789) as VXLAN.
  174. * This port must not be counted towards n_entries of any table.
  175. * Driver will not receive any callback associated with port 4789.
  176. */
  177. UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN = BIT(3),
  178. };
  179. struct udp_tunnel_nic;
  180. #define UDP_TUNNEL_NIC_MAX_SHARING_DEVICES (U16_MAX / 2)
  181. struct udp_tunnel_nic_shared {
  182. struct udp_tunnel_nic *udp_tunnel_nic_info;
  183. struct list_head devices;
  184. };
  185. struct udp_tunnel_nic_shared_node {
  186. struct net_device *dev;
  187. struct list_head list;
  188. };
  189. /**
  190. * struct udp_tunnel_nic_info - driver UDP tunnel offload information
  191. * @set_port: callback for adding a new port
  192. * @unset_port: callback for removing a port
  193. * @sync_table: callback for syncing the entire port table at once
  194. * @shared: reference to device global state (optional)
  195. * @flags: device flags from enum udp_tunnel_nic_info_flags
  196. * @tables: UDP port tables this device has
  197. * @tables.n_entries: number of entries in this table
  198. * @tables.tunnel_types: types of tunnels this table accepts
  199. *
  200. * Drivers are expected to provide either @set_port and @unset_port callbacks
  201. * or the @sync_table callback. Callbacks are invoked with rtnl lock held.
  202. *
  203. * Devices which (misguidedly) share the UDP tunnel port table across multiple
  204. * netdevs should allocate an instance of struct udp_tunnel_nic_shared and
  205. * point @shared at it.
  206. * There must never be more than %UDP_TUNNEL_NIC_MAX_SHARING_DEVICES devices
  207. * sharing a table.
  208. *
  209. * Known limitations:
  210. * - UDP tunnel port notifications are fundamentally best-effort -
  211. * it is likely the driver will both see skbs which use a UDP tunnel port,
  212. * while not being a tunneled skb, and tunnel skbs from other ports -
  213. * drivers should only use these ports for non-critical RX-side offloads,
  214. * e.g. the checksum offload;
  215. * - none of the devices care about the socket family at present, so we don't
  216. * track it. Please extend this code if you care.
  217. */
  218. struct udp_tunnel_nic_info {
  219. /* one-by-one */
  220. int (*set_port)(struct net_device *dev,
  221. unsigned int table, unsigned int entry,
  222. struct udp_tunnel_info *ti);
  223. int (*unset_port)(struct net_device *dev,
  224. unsigned int table, unsigned int entry,
  225. struct udp_tunnel_info *ti);
  226. /* all at once */
  227. int (*sync_table)(struct net_device *dev, unsigned int table);
  228. struct udp_tunnel_nic_shared *shared;
  229. unsigned int flags;
  230. struct udp_tunnel_nic_table_info {
  231. unsigned int n_entries;
  232. unsigned int tunnel_types;
  233. } tables[UDP_TUNNEL_NIC_MAX_TABLES];
  234. };
  235. /* UDP tunnel module dependencies
  236. *
  237. * Tunnel drivers are expected to have a hard dependency on the udp_tunnel
  238. * module. NIC drivers are not, they just attach their
  239. * struct udp_tunnel_nic_info to the netdev and wait for callbacks to come.
  240. * Loading a tunnel driver will cause the udp_tunnel module to be loaded
  241. * and only then will all the required state structures be allocated.
  242. * Since we want a weak dependency from the drivers and the core to udp_tunnel
  243. * we call things through the following stubs.
  244. */
  245. struct udp_tunnel_nic_ops {
  246. void (*get_port)(struct net_device *dev, unsigned int table,
  247. unsigned int idx, struct udp_tunnel_info *ti);
  248. void (*set_port_priv)(struct net_device *dev, unsigned int table,
  249. unsigned int idx, u8 priv);
  250. void (*add_port)(struct net_device *dev, struct udp_tunnel_info *ti);
  251. void (*del_port)(struct net_device *dev, struct udp_tunnel_info *ti);
  252. void (*reset_ntf)(struct net_device *dev);
  253. size_t (*dump_size)(struct net_device *dev, unsigned int table);
  254. int (*dump_write)(struct net_device *dev, unsigned int table,
  255. struct sk_buff *skb);
  256. };
  257. #ifdef CONFIG_INET
  258. extern const struct udp_tunnel_nic_ops *udp_tunnel_nic_ops;
  259. #else
  260. #define udp_tunnel_nic_ops ((struct udp_tunnel_nic_ops *)NULL)
  261. #endif
  262. static inline void
  263. udp_tunnel_nic_get_port(struct net_device *dev, unsigned int table,
  264. unsigned int idx, struct udp_tunnel_info *ti)
  265. {
  266. /* This helper is used from .sync_table, we indicate empty entries
  267. * by zero'ed @ti. Drivers which need to know the details of a port
  268. * when it gets deleted should use the .set_port / .unset_port
  269. * callbacks.
  270. * Zero out here, otherwise !CONFIG_INET causes uninitilized warnings.
  271. */
  272. memset(ti, 0, sizeof(*ti));
  273. if (udp_tunnel_nic_ops)
  274. udp_tunnel_nic_ops->get_port(dev, table, idx, ti);
  275. }
  276. static inline void
  277. udp_tunnel_nic_set_port_priv(struct net_device *dev, unsigned int table,
  278. unsigned int idx, u8 priv)
  279. {
  280. if (udp_tunnel_nic_ops)
  281. udp_tunnel_nic_ops->set_port_priv(dev, table, idx, priv);
  282. }
  283. static inline void
  284. udp_tunnel_nic_add_port(struct net_device *dev, struct udp_tunnel_info *ti)
  285. {
  286. if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
  287. return;
  288. if (udp_tunnel_nic_ops)
  289. udp_tunnel_nic_ops->add_port(dev, ti);
  290. }
  291. static inline void
  292. udp_tunnel_nic_del_port(struct net_device *dev, struct udp_tunnel_info *ti)
  293. {
  294. if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
  295. return;
  296. if (udp_tunnel_nic_ops)
  297. udp_tunnel_nic_ops->del_port(dev, ti);
  298. }
  299. /**
  300. * udp_tunnel_nic_reset_ntf() - device-originating reset notification
  301. * @dev: network interface device structure
  302. *
  303. * Called by the driver to inform the core that the entire UDP tunnel port
  304. * state has been lost, usually due to device reset. Core will assume device
  305. * forgot all the ports and issue .set_port and .sync_table callbacks as
  306. * necessary.
  307. *
  308. * This function must be called with rtnl lock held, and will issue all
  309. * the callbacks before returning.
  310. */
  311. static inline void udp_tunnel_nic_reset_ntf(struct net_device *dev)
  312. {
  313. if (udp_tunnel_nic_ops)
  314. udp_tunnel_nic_ops->reset_ntf(dev);
  315. }
  316. static inline size_t
  317. udp_tunnel_nic_dump_size(struct net_device *dev, unsigned int table)
  318. {
  319. if (!udp_tunnel_nic_ops)
  320. return 0;
  321. return udp_tunnel_nic_ops->dump_size(dev, table);
  322. }
  323. static inline int
  324. udp_tunnel_nic_dump_write(struct net_device *dev, unsigned int table,
  325. struct sk_buff *skb)
  326. {
  327. if (!udp_tunnel_nic_ops)
  328. return 0;
  329. return udp_tunnel_nic_ops->dump_write(dev, table, skb);
  330. }
  331. #endif