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_nets(struct list_head *list_net, unsigned int id,
  224. struct rtnl_link_ops *ops);
  225. void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  226. const struct iphdr *tnl_params, const u8 protocol);
  227. void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  228. const u8 proto);
  229. int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
  230. int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict);
  231. int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
  232. void ip_tunnel_get_stats64(struct net_device *dev,
  233. struct rtnl_link_stats64 *tot);
  234. struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
  235. int link, __be16 flags,
  236. __be32 remote, __be32 local,
  237. __be32 key);
  238. int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
  239. const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
  240. bool log_ecn_error);
  241. int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
  242. struct ip_tunnel_parm *p, __u32 fwmark);
  243. int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
  244. struct ip_tunnel_parm *p, __u32 fwmark);
  245. void ip_tunnel_setup(struct net_device *dev, unsigned int net_id);
  246. struct ip_tunnel_encap_ops {
  247. size_t (*encap_hlen)(struct ip_tunnel_encap *e);
  248. int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
  249. u8 *protocol, struct flowi4 *fl4);
  250. };
  251. #define MAX_IPTUN_ENCAP_OPS 8
  252. extern const struct ip_tunnel_encap_ops __rcu *
  253. iptun_encaps[MAX_IPTUN_ENCAP_OPS];
  254. int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
  255. unsigned int num);
  256. int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
  257. unsigned int num);
  258. int ip_tunnel_encap_setup(struct ip_tunnel *t,
  259. struct ip_tunnel_encap *ipencap);
  260. static inline int ip_encap_hlen(struct ip_tunnel_encap *e)
  261. {
  262. const struct ip_tunnel_encap_ops *ops;
  263. int hlen = -EINVAL;
  264. if (e->type == TUNNEL_ENCAP_NONE)
  265. return 0;
  266. if (e->type >= MAX_IPTUN_ENCAP_OPS)
  267. return -EINVAL;
  268. rcu_read_lock();
  269. ops = rcu_dereference(iptun_encaps[e->type]);
  270. if (likely(ops && ops->encap_hlen))
  271. hlen = ops->encap_hlen(e);
  272. rcu_read_unlock();
  273. return hlen;
  274. }
  275. static inline int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
  276. u8 *protocol, struct flowi4 *fl4)
  277. {
  278. const struct ip_tunnel_encap_ops *ops;
  279. int ret = -EINVAL;
  280. if (t->encap.type == TUNNEL_ENCAP_NONE)
  281. return 0;
  282. if (t->encap.type >= MAX_IPTUN_ENCAP_OPS)
  283. return -EINVAL;
  284. rcu_read_lock();
  285. ops = rcu_dereference(iptun_encaps[t->encap.type]);
  286. if (likely(ops && ops->build_header))
  287. ret = ops->build_header(skb, &t->encap, protocol, fl4);
  288. rcu_read_unlock();
  289. return ret;
  290. }
  291. /* Extract dsfield from inner protocol */
  292. static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
  293. const struct sk_buff *skb)
  294. {
  295. if (skb->protocol == htons(ETH_P_IP))
  296. return iph->tos;
  297. else if (skb->protocol == htons(ETH_P_IPV6))
  298. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  299. else
  300. return 0;
  301. }
  302. /* Propogate ECN bits out */
  303. static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
  304. const struct sk_buff *skb)
  305. {
  306. u8 inner = ip_tunnel_get_dsfield(iph, skb);
  307. return INET_ECN_encapsulate(tos, inner);
  308. }
  309. int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
  310. __be16 inner_proto, bool raw_proto, bool xnet);
  311. static inline int iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
  312. __be16 inner_proto, bool xnet)
  313. {
  314. return __iptunnel_pull_header(skb, hdr_len, inner_proto, false, xnet);
  315. }
  316. void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  317. __be32 src, __be32 dst, u8 proto,
  318. u8 tos, u8 ttl, __be16 df, bool xnet);
  319. struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
  320. gfp_t flags);
  321. int iptunnel_handle_offloads(struct sk_buff *skb, int gso_type_mask);
  322. static inline int iptunnel_pull_offloads(struct sk_buff *skb)
  323. {
  324. if (skb_is_gso(skb)) {
  325. int err;
  326. err = skb_unclone(skb, GFP_ATOMIC);
  327. if (unlikely(err))
  328. return err;
  329. skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
  330. NETIF_F_GSO_SHIFT);
  331. }
  332. skb->encapsulation = 0;
  333. return 0;
  334. }
  335. static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len)
  336. {
  337. if (pkt_len > 0) {
  338. struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats);
  339. u64_stats_update_begin(&tstats->syncp);
  340. tstats->tx_bytes += pkt_len;
  341. tstats->tx_packets++;
  342. u64_stats_update_end(&tstats->syncp);
  343. put_cpu_ptr(tstats);
  344. } else {
  345. struct net_device_stats *err_stats = &dev->stats;
  346. if (pkt_len < 0) {
  347. err_stats->tx_errors++;
  348. err_stats->tx_aborted_errors++;
  349. } else {
  350. err_stats->tx_dropped++;
  351. }
  352. }
  353. }
  354. static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info)
  355. {
  356. return info + 1;
  357. }
  358. static inline void ip_tunnel_info_opts_get(void *to,
  359. const struct ip_tunnel_info *info)
  360. {
  361. memcpy(to, info + 1, info->options_len);
  362. }
  363. static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
  364. const void *from, int len)
  365. {
  366. memcpy(ip_tunnel_info_opts(info), from, len);
  367. info->options_len = len;
  368. }
  369. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  370. {
  371. return (struct ip_tunnel_info *)lwtstate->data;
  372. }
  373. extern struct static_key ip_tunnel_metadata_cnt;
  374. /* Returns > 0 if metadata should be collected */
  375. static inline int ip_tunnel_collect_metadata(void)
  376. {
  377. return static_key_false(&ip_tunnel_metadata_cnt);
  378. }
  379. void __init ip_tunnel_core_init(void);
  380. void ip_tunnel_need_metadata(void);
  381. void ip_tunnel_unneed_metadata(void);
  382. #else /* CONFIG_INET */
  383. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  384. {
  385. return NULL;
  386. }
  387. static inline void ip_tunnel_need_metadata(void)
  388. {
  389. }
  390. static inline void ip_tunnel_unneed_metadata(void)
  391. {
  392. }
  393. static inline void ip_tunnel_info_opts_get(void *to,
  394. const struct ip_tunnel_info *info)
  395. {
  396. }
  397. static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
  398. const void *from, int len)
  399. {
  400. info->options_len = 0;
  401. }
  402. #endif /* CONFIG_INET */
  403. #endif /* __NET_IP_TUNNELS_H */