ip_tunnels.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. /* These four fields used only by ERSPAN */
  102. u32 index; /* ERSPAN type II index */
  103. u8 erspan_ver; /* ERSPAN version */
  104. u8 dir; /* ERSPAN direction */
  105. u16 hwid; /* ERSPAN hardware ID */
  106. struct dst_cache dst_cache;
  107. struct ip_tunnel_parm parms;
  108. int mlink;
  109. int encap_hlen; /* Encap header length (FOU,GUE) */
  110. int hlen; /* tun_hlen + encap_hlen */
  111. struct ip_tunnel_encap encap;
  112. /* for SIT */
  113. #ifdef CONFIG_IPV6_SIT_6RD
  114. struct ip_tunnel_6rd_parm ip6rd;
  115. #endif
  116. struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */
  117. unsigned int prl_count; /* # of entries in PRL */
  118. unsigned int ip_tnl_net_id;
  119. struct gro_cells gro_cells;
  120. __u32 fwmark;
  121. bool collect_md;
  122. bool ignore_df;
  123. };
  124. #define TUNNEL_CSUM __cpu_to_be16(0x01)
  125. #define TUNNEL_ROUTING __cpu_to_be16(0x02)
  126. #define TUNNEL_KEY __cpu_to_be16(0x04)
  127. #define TUNNEL_SEQ __cpu_to_be16(0x08)
  128. #define TUNNEL_STRICT __cpu_to_be16(0x10)
  129. #define TUNNEL_REC __cpu_to_be16(0x20)
  130. #define TUNNEL_VERSION __cpu_to_be16(0x40)
  131. #define TUNNEL_NO_KEY __cpu_to_be16(0x80)
  132. #define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100)
  133. #define TUNNEL_OAM __cpu_to_be16(0x0200)
  134. #define TUNNEL_CRIT_OPT __cpu_to_be16(0x0400)
  135. #define TUNNEL_GENEVE_OPT __cpu_to_be16(0x0800)
  136. #define TUNNEL_VXLAN_OPT __cpu_to_be16(0x1000)
  137. #define TUNNEL_NOCACHE __cpu_to_be16(0x2000)
  138. #define TUNNEL_ERSPAN_OPT __cpu_to_be16(0x4000)
  139. #define TUNNEL_OPTIONS_PRESENT \
  140. (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT | TUNNEL_ERSPAN_OPT)
  141. struct tnl_ptk_info {
  142. __be16 flags;
  143. __be16 proto;
  144. __be32 key;
  145. __be32 seq;
  146. int hdr_len;
  147. };
  148. #define PACKET_RCVD 0
  149. #define PACKET_REJECT 1
  150. #define PACKET_NEXT 2
  151. #define IP_TNL_HASH_BITS 7
  152. #define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS)
  153. struct ip_tunnel_net {
  154. struct net_device *fb_tunnel_dev;
  155. struct rtnl_link_ops *rtnl_link_ops;
  156. struct hlist_head tunnels[IP_TNL_HASH_SIZE];
  157. struct ip_tunnel __rcu *collect_md_tun;
  158. int type;
  159. };
  160. static inline void ip_tunnel_key_init(struct ip_tunnel_key *key,
  161. __be32 saddr, __be32 daddr,
  162. u8 tos, u8 ttl, __be32 label,
  163. __be16 tp_src, __be16 tp_dst,
  164. __be64 tun_id, __be16 tun_flags)
  165. {
  166. key->tun_id = tun_id;
  167. key->u.ipv4.src = saddr;
  168. key->u.ipv4.dst = daddr;
  169. memset((unsigned char *)key + IP_TUNNEL_KEY_IPV4_PAD,
  170. 0, IP_TUNNEL_KEY_IPV4_PAD_LEN);
  171. key->tos = tos;
  172. key->ttl = ttl;
  173. key->label = label;
  174. key->tun_flags = tun_flags;
  175. /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
  176. * the upper tunnel are used.
  177. * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
  178. */
  179. key->tp_src = tp_src;
  180. key->tp_dst = tp_dst;
  181. /* Clear struct padding. */
  182. if (sizeof(*key) != IP_TUNNEL_KEY_SIZE)
  183. memset((unsigned char *)key + IP_TUNNEL_KEY_SIZE,
  184. 0, sizeof(*key) - IP_TUNNEL_KEY_SIZE);
  185. }
  186. static inline bool
  187. ip_tunnel_dst_cache_usable(const struct sk_buff *skb,
  188. const struct ip_tunnel_info *info)
  189. {
  190. if (skb->mark)
  191. return false;
  192. if (!info)
  193. return true;
  194. if (info->key.tun_flags & TUNNEL_NOCACHE)
  195. return false;
  196. return true;
  197. }
  198. static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info
  199. *tun_info)
  200. {
  201. return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET;
  202. }
  203. static inline __be64 key32_to_tunnel_id(__be32 key)
  204. {
  205. #ifdef __BIG_ENDIAN
  206. return (__force __be64)key;
  207. #else
  208. return (__force __be64)((__force u64)key << 32);
  209. #endif
  210. }
  211. /* Returns the least-significant 32 bits of a __be64. */
  212. static inline __be32 tunnel_id_to_key32(__be64 tun_id)
  213. {
  214. #ifdef __BIG_ENDIAN
  215. return (__force __be32)tun_id;
  216. #else
  217. return (__force __be32)((__force u64)tun_id >> 32);
  218. #endif
  219. }
  220. #ifdef CONFIG_INET
  221. static inline void ip_tunnel_init_flow(struct flowi4 *fl4,
  222. int proto,
  223. __be32 daddr, __be32 saddr,
  224. __be32 key, __u8 tos, int oif,
  225. __u32 mark)
  226. {
  227. memset(fl4, 0, sizeof(*fl4));
  228. fl4->flowi4_oif = oif;
  229. fl4->daddr = daddr;
  230. fl4->saddr = saddr;
  231. fl4->flowi4_tos = tos;
  232. fl4->flowi4_proto = proto;
  233. fl4->fl4_gre_key = key;
  234. fl4->flowi4_mark = mark;
  235. }
  236. int ip_tunnel_init(struct net_device *dev);
  237. void ip_tunnel_uninit(struct net_device *dev);
  238. void ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
  239. struct net *ip_tunnel_get_link_net(const struct net_device *dev);
  240. int ip_tunnel_get_iflink(const struct net_device *dev);
  241. int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id,
  242. struct rtnl_link_ops *ops, char *devname);
  243. void ip_tunnel_delete_nets(struct list_head *list_net, unsigned int id,
  244. struct rtnl_link_ops *ops);
  245. void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  246. const struct iphdr *tnl_params, const u8 protocol);
  247. void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  248. const u8 proto);
  249. int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
  250. int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict);
  251. int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
  252. void ip_tunnel_get_stats64(struct net_device *dev,
  253. struct rtnl_link_stats64 *tot);
  254. struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
  255. int link, __be16 flags,
  256. __be32 remote, __be32 local,
  257. __be32 key);
  258. int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
  259. const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
  260. bool log_ecn_error);
  261. int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
  262. struct ip_tunnel_parm *p, __u32 fwmark);
  263. int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
  264. struct ip_tunnel_parm *p, __u32 fwmark);
  265. void ip_tunnel_setup(struct net_device *dev, unsigned int net_id);
  266. struct ip_tunnel_encap_ops {
  267. size_t (*encap_hlen)(struct ip_tunnel_encap *e);
  268. int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
  269. u8 *protocol, struct flowi4 *fl4);
  270. };
  271. #define MAX_IPTUN_ENCAP_OPS 8
  272. extern const struct ip_tunnel_encap_ops __rcu *
  273. iptun_encaps[MAX_IPTUN_ENCAP_OPS];
  274. int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
  275. unsigned int num);
  276. int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
  277. unsigned int num);
  278. int ip_tunnel_encap_setup(struct ip_tunnel *t,
  279. struct ip_tunnel_encap *ipencap);
  280. static inline bool pskb_inet_may_pull(struct sk_buff *skb)
  281. {
  282. int nhlen;
  283. switch (skb->protocol) {
  284. #if IS_ENABLED(CONFIG_IPV6)
  285. case htons(ETH_P_IPV6):
  286. nhlen = sizeof(struct ipv6hdr);
  287. break;
  288. #endif
  289. case htons(ETH_P_IP):
  290. nhlen = sizeof(struct iphdr);
  291. break;
  292. default:
  293. nhlen = 0;
  294. }
  295. return pskb_network_may_pull(skb, nhlen);
  296. }
  297. static inline int ip_encap_hlen(struct ip_tunnel_encap *e)
  298. {
  299. const struct ip_tunnel_encap_ops *ops;
  300. int hlen = -EINVAL;
  301. if (e->type == TUNNEL_ENCAP_NONE)
  302. return 0;
  303. if (e->type >= MAX_IPTUN_ENCAP_OPS)
  304. return -EINVAL;
  305. rcu_read_lock();
  306. ops = rcu_dereference(iptun_encaps[e->type]);
  307. if (likely(ops && ops->encap_hlen))
  308. hlen = ops->encap_hlen(e);
  309. rcu_read_unlock();
  310. return hlen;
  311. }
  312. static inline int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
  313. u8 *protocol, struct flowi4 *fl4)
  314. {
  315. const struct ip_tunnel_encap_ops *ops;
  316. int ret = -EINVAL;
  317. if (t->encap.type == TUNNEL_ENCAP_NONE)
  318. return 0;
  319. if (t->encap.type >= MAX_IPTUN_ENCAP_OPS)
  320. return -EINVAL;
  321. rcu_read_lock();
  322. ops = rcu_dereference(iptun_encaps[t->encap.type]);
  323. if (likely(ops && ops->build_header))
  324. ret = ops->build_header(skb, &t->encap, protocol, fl4);
  325. rcu_read_unlock();
  326. return ret;
  327. }
  328. /* Extract dsfield from inner protocol */
  329. static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
  330. const struct sk_buff *skb)
  331. {
  332. if (skb->protocol == htons(ETH_P_IP))
  333. return iph->tos;
  334. else if (skb->protocol == htons(ETH_P_IPV6))
  335. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  336. else
  337. return 0;
  338. }
  339. static inline u8 ip_tunnel_get_ttl(const struct iphdr *iph,
  340. const struct sk_buff *skb)
  341. {
  342. if (skb->protocol == htons(ETH_P_IP))
  343. return iph->ttl;
  344. else if (skb->protocol == htons(ETH_P_IPV6))
  345. return ((const struct ipv6hdr *)iph)->hop_limit;
  346. else
  347. return 0;
  348. }
  349. /* Propogate ECN bits out */
  350. static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
  351. const struct sk_buff *skb)
  352. {
  353. u8 inner = ip_tunnel_get_dsfield(iph, skb);
  354. return INET_ECN_encapsulate(tos, inner);
  355. }
  356. int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
  357. __be16 inner_proto, bool raw_proto, bool xnet);
  358. static inline int iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
  359. __be16 inner_proto, bool xnet)
  360. {
  361. return __iptunnel_pull_header(skb, hdr_len, inner_proto, false, xnet);
  362. }
  363. void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  364. __be32 src, __be32 dst, u8 proto,
  365. u8 tos, u8 ttl, __be16 df, bool xnet);
  366. struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
  367. gfp_t flags);
  368. int iptunnel_handle_offloads(struct sk_buff *skb, int gso_type_mask);
  369. static inline int iptunnel_pull_offloads(struct sk_buff *skb)
  370. {
  371. if (skb_is_gso(skb)) {
  372. int err;
  373. err = skb_unclone(skb, GFP_ATOMIC);
  374. if (unlikely(err))
  375. return err;
  376. skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
  377. NETIF_F_GSO_SHIFT);
  378. }
  379. skb->encapsulation = 0;
  380. return 0;
  381. }
  382. static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len)
  383. {
  384. if (pkt_len > 0) {
  385. struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats);
  386. u64_stats_update_begin(&tstats->syncp);
  387. tstats->tx_bytes += pkt_len;
  388. tstats->tx_packets++;
  389. u64_stats_update_end(&tstats->syncp);
  390. put_cpu_ptr(tstats);
  391. } else {
  392. struct net_device_stats *err_stats = &dev->stats;
  393. if (pkt_len < 0) {
  394. err_stats->tx_errors++;
  395. err_stats->tx_aborted_errors++;
  396. } else {
  397. err_stats->tx_dropped++;
  398. }
  399. }
  400. }
  401. static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info)
  402. {
  403. return info + 1;
  404. }
  405. static inline void ip_tunnel_info_opts_get(void *to,
  406. const struct ip_tunnel_info *info)
  407. {
  408. memcpy(to, info + 1, info->options_len);
  409. }
  410. static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
  411. const void *from, int len,
  412. __be16 flags)
  413. {
  414. memcpy(ip_tunnel_info_opts(info), from, len);
  415. info->options_len = len;
  416. info->key.tun_flags |= flags;
  417. }
  418. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  419. {
  420. return (struct ip_tunnel_info *)lwtstate->data;
  421. }
  422. DECLARE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
  423. /* Returns > 0 if metadata should be collected */
  424. static inline int ip_tunnel_collect_metadata(void)
  425. {
  426. return static_branch_unlikely(&ip_tunnel_metadata_cnt);
  427. }
  428. void __init ip_tunnel_core_init(void);
  429. void ip_tunnel_need_metadata(void);
  430. void ip_tunnel_unneed_metadata(void);
  431. #else /* CONFIG_INET */
  432. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  433. {
  434. return NULL;
  435. }
  436. static inline void ip_tunnel_need_metadata(void)
  437. {
  438. }
  439. static inline void ip_tunnel_unneed_metadata(void)
  440. {
  441. }
  442. static inline void ip_tunnel_info_opts_get(void *to,
  443. const struct ip_tunnel_info *info)
  444. {
  445. }
  446. static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
  447. const void *from, int len,
  448. __be16 flags)
  449. {
  450. info->options_len = 0;
  451. info->key.tun_flags |= flags;
  452. }
  453. #endif /* CONFIG_INET */
  454. #endif /* __NET_IP_TUNNELS_H */