ip_tunnels.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_IP_TUNNELS_H
  3. #define __NET_IP_TUNNELS_H 1
  4. #include <linux/if_tunnel.h>
  5. #include <linux/netdevice.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/socket.h>
  8. #include <linux/types.h>
  9. #include <linux/u64_stats_sync.h>
  10. #include <linux/bitops.h>
  11. #include <net/dsfield.h>
  12. #include <net/gro_cells.h>
  13. #include <net/inet_ecn.h>
  14. #include <net/netns/generic.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/lwtunnel.h>
  17. #include <net/dst_cache.h>
  18. #if IS_ENABLED(CONFIG_IPV6)
  19. #include <net/ipv6.h>
  20. #include <net/ip6_fib.h>
  21. #include <net/ip6_route.h>
  22. #endif
  23. /* Keep error state on tunnel for 30 sec */
  24. #define IPTUNNEL_ERR_TIMEO (30*HZ)
  25. /* Used to memset ip_tunnel padding. */
  26. #define IP_TUNNEL_KEY_SIZE offsetofend(struct ip_tunnel_key, tp_dst)
  27. /* Used to memset ipv4 address padding. */
  28. #define IP_TUNNEL_KEY_IPV4_PAD offsetofend(struct ip_tunnel_key, u.ipv4.dst)
  29. #define IP_TUNNEL_KEY_IPV4_PAD_LEN \
  30. (FIELD_SIZEOF(struct ip_tunnel_key, u) - \
  31. FIELD_SIZEOF(struct ip_tunnel_key, u.ipv4))
  32. struct ip_tunnel_key {
  33. __be64 tun_id;
  34. union {
  35. struct {
  36. __be32 src;
  37. __be32 dst;
  38. } ipv4;
  39. struct {
  40. struct in6_addr src;
  41. struct in6_addr dst;
  42. } ipv6;
  43. } u;
  44. __be16 tun_flags;
  45. u8 tos; /* TOS for IPv4, TC for IPv6 */
  46. u8 ttl; /* TTL for IPv4, HL for IPv6 */
  47. __be32 label; /* Flow Label for IPv6 */
  48. __be16 tp_src;
  49. __be16 tp_dst;
  50. };
  51. /* Flags for ip_tunnel_info mode. */
  52. #define IP_TUNNEL_INFO_TX 0x01 /* represents tx tunnel parameters */
  53. #define IP_TUNNEL_INFO_IPV6 0x02 /* key contains IPv6 addresses */
  54. #define IP_TUNNEL_INFO_BRIDGE 0x04 /* represents a bridged tunnel id */
  55. /* Maximum tunnel options length. */
  56. #define IP_TUNNEL_OPTS_MAX \
  57. GENMASK((FIELD_SIZEOF(struct ip_tunnel_info, \
  58. options_len) * BITS_PER_BYTE) - 1, 0)
  59. struct ip_tunnel_info {
  60. struct ip_tunnel_key key;
  61. #ifdef CONFIG_DST_CACHE
  62. struct dst_cache dst_cache;
  63. #endif
  64. u8 options_len;
  65. u8 mode;
  66. };
  67. /* 6rd prefix/relay information */
  68. #ifdef CONFIG_IPV6_SIT_6RD
  69. struct ip_tunnel_6rd_parm {
  70. struct in6_addr prefix;
  71. __be32 relay_prefix;
  72. u16 prefixlen;
  73. u16 relay_prefixlen;
  74. };
  75. #endif
  76. struct ip_tunnel_encap {
  77. u16 type;
  78. u16 flags;
  79. __be16 sport;
  80. __be16 dport;
  81. };
  82. struct ip_tunnel_prl_entry {
  83. struct ip_tunnel_prl_entry __rcu *next;
  84. __be32 addr;
  85. u16 flags;
  86. struct rcu_head rcu_head;
  87. };
  88. struct metadata_dst;
  89. struct ip_tunnel {
  90. struct ip_tunnel __rcu *next;
  91. struct hlist_node hash_node;
  92. struct net_device *dev;
  93. struct net *net; /* netns for packet i/o */
  94. unsigned long err_time; /* Time when the last ICMP error
  95. * arrived */
  96. int err_count; /* Number of arrived ICMP errors */
  97. /* These four fields used only by GRE */
  98. u32 i_seqno; /* The last seen seqno */
  99. u32 o_seqno; /* The last output seqno */
  100. int tun_hlen; /* Precalculated header length */
  101. /* This field used only by ERSPAN */
  102. u32 index; /* ERSPAN type II index */
  103. struct dst_cache dst_cache;
  104. struct ip_tunnel_parm parms;
  105. int mlink;
  106. int encap_hlen; /* Encap header length (FOU,GUE) */
  107. int hlen; /* tun_hlen + encap_hlen */
  108. struct ip_tunnel_encap encap;
  109. /* for SIT */
  110. #ifdef CONFIG_IPV6_SIT_6RD
  111. struct ip_tunnel_6rd_parm ip6rd;
  112. #endif
  113. struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */
  114. unsigned int prl_count; /* # of entries in PRL */
  115. unsigned int ip_tnl_net_id;
  116. struct gro_cells gro_cells;
  117. __u32 fwmark;
  118. bool collect_md;
  119. bool ignore_df;
  120. };
  121. #define TUNNEL_CSUM __cpu_to_be16(0x01)
  122. #define TUNNEL_ROUTING __cpu_to_be16(0x02)
  123. #define TUNNEL_KEY __cpu_to_be16(0x04)
  124. #define TUNNEL_SEQ __cpu_to_be16(0x08)
  125. #define TUNNEL_STRICT __cpu_to_be16(0x10)
  126. #define TUNNEL_REC __cpu_to_be16(0x20)
  127. #define TUNNEL_VERSION __cpu_to_be16(0x40)
  128. #define TUNNEL_NO_KEY __cpu_to_be16(0x80)
  129. #define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100)
  130. #define TUNNEL_OAM __cpu_to_be16(0x0200)
  131. #define TUNNEL_CRIT_OPT __cpu_to_be16(0x0400)
  132. #define TUNNEL_GENEVE_OPT __cpu_to_be16(0x0800)
  133. #define TUNNEL_VXLAN_OPT __cpu_to_be16(0x1000)
  134. #define TUNNEL_NOCACHE __cpu_to_be16(0x2000)
  135. #define TUNNEL_ERSPAN_OPT __cpu_to_be16(0x4000)
  136. #define TUNNEL_OPTIONS_PRESENT \
  137. (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT | TUNNEL_ERSPAN_OPT)
  138. struct tnl_ptk_info {
  139. __be16 flags;
  140. __be16 proto;
  141. __be32 key;
  142. __be32 seq;
  143. int hdr_len;
  144. };
  145. #define PACKET_RCVD 0
  146. #define PACKET_REJECT 1
  147. #define PACKET_NEXT 2
  148. #define IP_TNL_HASH_BITS 7
  149. #define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS)
  150. struct ip_tunnel_net {
  151. struct net_device *fb_tunnel_dev;
  152. struct hlist_head tunnels[IP_TNL_HASH_SIZE];
  153. struct ip_tunnel __rcu *collect_md_tun;
  154. };
  155. static inline void ip_tunnel_key_init(struct ip_tunnel_key *key,
  156. __be32 saddr, __be32 daddr,
  157. u8 tos, u8 ttl, __be32 label,
  158. __be16 tp_src, __be16 tp_dst,
  159. __be64 tun_id, __be16 tun_flags)
  160. {
  161. key->tun_id = tun_id;
  162. key->u.ipv4.src = saddr;
  163. key->u.ipv4.dst = daddr;
  164. memset((unsigned char *)key + IP_TUNNEL_KEY_IPV4_PAD,
  165. 0, IP_TUNNEL_KEY_IPV4_PAD_LEN);
  166. key->tos = tos;
  167. key->ttl = ttl;
  168. key->label = label;
  169. key->tun_flags = tun_flags;
  170. /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
  171. * the upper tunnel are used.
  172. * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
  173. */
  174. key->tp_src = tp_src;
  175. key->tp_dst = tp_dst;
  176. /* Clear struct padding. */
  177. if (sizeof(*key) != IP_TUNNEL_KEY_SIZE)
  178. memset((unsigned char *)key + IP_TUNNEL_KEY_SIZE,
  179. 0, sizeof(*key) - IP_TUNNEL_KEY_SIZE);
  180. }
  181. static inline bool
  182. ip_tunnel_dst_cache_usable(const struct sk_buff *skb,
  183. const struct ip_tunnel_info *info)
  184. {
  185. if (skb->mark)
  186. return false;
  187. if (!info)
  188. return true;
  189. if (info->key.tun_flags & TUNNEL_NOCACHE)
  190. return false;
  191. return true;
  192. }
  193. static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info
  194. *tun_info)
  195. {
  196. return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET;
  197. }
  198. static inline __be64 key32_to_tunnel_id(__be32 key)
  199. {
  200. #ifdef __BIG_ENDIAN
  201. return (__force __be64)key;
  202. #else
  203. return (__force __be64)((__force u64)key << 32);
  204. #endif
  205. }
  206. /* Returns the least-significant 32 bits of a __be64. */
  207. static inline __be32 tunnel_id_to_key32(__be64 tun_id)
  208. {
  209. #ifdef __BIG_ENDIAN
  210. return (__force __be32)tun_id;
  211. #else
  212. return (__force __be32)((__force u64)tun_id >> 32);
  213. #endif
  214. }
  215. #ifdef CONFIG_INET
  216. int ip_tunnel_init(struct net_device *dev);
  217. void ip_tunnel_uninit(struct net_device *dev);
  218. void ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
  219. struct net *ip_tunnel_get_link_net(const struct net_device *dev);
  220. int ip_tunnel_get_iflink(const struct net_device *dev);
  221. int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id,
  222. struct rtnl_link_ops *ops, char *devname);
  223. void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
  224. void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  225. const struct iphdr *tnl_params, const u8 protocol);
  226. void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  227. const u8 proto);
  228. int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
  229. int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict);
  230. int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
  231. void ip_tunnel_get_stats64(struct net_device *dev,
  232. struct rtnl_link_stats64 *tot);
  233. struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
  234. int link, __be16 flags,
  235. __be32 remote, __be32 local,
  236. __be32 key);
  237. int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
  238. const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
  239. bool log_ecn_error);
  240. int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
  241. struct ip_tunnel_parm *p, __u32 fwmark);
  242. int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
  243. struct ip_tunnel_parm *p, __u32 fwmark);
  244. void ip_tunnel_setup(struct net_device *dev, unsigned int net_id);
  245. struct ip_tunnel_encap_ops {
  246. size_t (*encap_hlen)(struct ip_tunnel_encap *e);
  247. int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
  248. u8 *protocol, struct flowi4 *fl4);
  249. };
  250. #define MAX_IPTUN_ENCAP_OPS 8
  251. extern const struct ip_tunnel_encap_ops __rcu *
  252. iptun_encaps[MAX_IPTUN_ENCAP_OPS];
  253. int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
  254. unsigned int num);
  255. int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
  256. unsigned int num);
  257. int ip_tunnel_encap_setup(struct ip_tunnel *t,
  258. struct ip_tunnel_encap *ipencap);
  259. static inline int ip_encap_hlen(struct ip_tunnel_encap *e)
  260. {
  261. const struct ip_tunnel_encap_ops *ops;
  262. int hlen = -EINVAL;
  263. if (e->type == TUNNEL_ENCAP_NONE)
  264. return 0;
  265. if (e->type >= MAX_IPTUN_ENCAP_OPS)
  266. return -EINVAL;
  267. rcu_read_lock();
  268. ops = rcu_dereference(iptun_encaps[e->type]);
  269. if (likely(ops && ops->encap_hlen))
  270. hlen = ops->encap_hlen(e);
  271. rcu_read_unlock();
  272. return hlen;
  273. }
  274. static inline int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
  275. u8 *protocol, struct flowi4 *fl4)
  276. {
  277. const struct ip_tunnel_encap_ops *ops;
  278. int ret = -EINVAL;
  279. if (t->encap.type == TUNNEL_ENCAP_NONE)
  280. return 0;
  281. if (t->encap.type >= MAX_IPTUN_ENCAP_OPS)
  282. return -EINVAL;
  283. rcu_read_lock();
  284. ops = rcu_dereference(iptun_encaps[t->encap.type]);
  285. if (likely(ops && ops->build_header))
  286. ret = ops->build_header(skb, &t->encap, protocol, fl4);
  287. rcu_read_unlock();
  288. return ret;
  289. }
  290. /* Extract dsfield from inner protocol */
  291. static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
  292. const struct sk_buff *skb)
  293. {
  294. if (skb->protocol == htons(ETH_P_IP))
  295. return iph->tos;
  296. else if (skb->protocol == htons(ETH_P_IPV6))
  297. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  298. else
  299. return 0;
  300. }
  301. /* Propogate ECN bits out */
  302. static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
  303. const struct sk_buff *skb)
  304. {
  305. u8 inner = ip_tunnel_get_dsfield(iph, skb);
  306. return INET_ECN_encapsulate(tos, inner);
  307. }
  308. int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
  309. __be16 inner_proto, bool raw_proto, bool xnet);
  310. static inline int iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
  311. __be16 inner_proto, bool xnet)
  312. {
  313. return __iptunnel_pull_header(skb, hdr_len, inner_proto, false, xnet);
  314. }
  315. void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  316. __be32 src, __be32 dst, u8 proto,
  317. u8 tos, u8 ttl, __be16 df, bool xnet);
  318. struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
  319. gfp_t flags);
  320. int iptunnel_handle_offloads(struct sk_buff *skb, int gso_type_mask);
  321. static inline int iptunnel_pull_offloads(struct sk_buff *skb)
  322. {
  323. if (skb_is_gso(skb)) {
  324. int err;
  325. err = skb_unclone(skb, GFP_ATOMIC);
  326. if (unlikely(err))
  327. return err;
  328. skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
  329. NETIF_F_GSO_SHIFT);
  330. }
  331. skb->encapsulation = 0;
  332. return 0;
  333. }
  334. static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len)
  335. {
  336. if (pkt_len > 0) {
  337. struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats);
  338. u64_stats_update_begin(&tstats->syncp);
  339. tstats->tx_bytes += pkt_len;
  340. tstats->tx_packets++;
  341. u64_stats_update_end(&tstats->syncp);
  342. put_cpu_ptr(tstats);
  343. } else {
  344. struct net_device_stats *err_stats = &dev->stats;
  345. if (pkt_len < 0) {
  346. err_stats->tx_errors++;
  347. err_stats->tx_aborted_errors++;
  348. } else {
  349. err_stats->tx_dropped++;
  350. }
  351. }
  352. }
  353. static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info)
  354. {
  355. return info + 1;
  356. }
  357. static inline void ip_tunnel_info_opts_get(void *to,
  358. const struct ip_tunnel_info *info)
  359. {
  360. memcpy(to, info + 1, info->options_len);
  361. }
  362. static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
  363. const void *from, int len)
  364. {
  365. memcpy(ip_tunnel_info_opts(info), from, len);
  366. info->options_len = len;
  367. }
  368. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  369. {
  370. return (struct ip_tunnel_info *)lwtstate->data;
  371. }
  372. extern struct static_key ip_tunnel_metadata_cnt;
  373. /* Returns > 0 if metadata should be collected */
  374. static inline int ip_tunnel_collect_metadata(void)
  375. {
  376. return static_key_false(&ip_tunnel_metadata_cnt);
  377. }
  378. void __init ip_tunnel_core_init(void);
  379. void ip_tunnel_need_metadata(void);
  380. void ip_tunnel_unneed_metadata(void);
  381. #else /* CONFIG_INET */
  382. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  383. {
  384. return NULL;
  385. }
  386. static inline void ip_tunnel_need_metadata(void)
  387. {
  388. }
  389. static inline void ip_tunnel_unneed_metadata(void)
  390. {
  391. }
  392. static inline void ip_tunnel_info_opts_get(void *to,
  393. const struct ip_tunnel_info *info)
  394. {
  395. }
  396. static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
  397. const void *from, int len)
  398. {
  399. info->options_len = 0;
  400. }
  401. #endif /* CONFIG_INET */
  402. #endif /* __NET_IP_TUNNELS_H */