udp_tunnel.h 15 KB

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