ip_tunnels.h 12 KB

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