ip_tunnels.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/types.h>
  7. #include <linux/u64_stats_sync.h>
  8. #include <net/dsfield.h>
  9. #include <net/gro_cells.h>
  10. #include <net/inet_ecn.h>
  11. #include <net/netns/generic.h>
  12. #include <net/rtnetlink.h>
  13. #include <net/lwtunnel.h>
  14. #if IS_ENABLED(CONFIG_IPV6)
  15. #include <net/ipv6.h>
  16. #include <net/ip6_fib.h>
  17. #include <net/ip6_route.h>
  18. #endif
  19. /* Keep error state on tunnel for 30 sec */
  20. #define IPTUNNEL_ERR_TIMEO (30*HZ)
  21. /* Used to memset ip_tunnel padding. */
  22. #define IP_TUNNEL_KEY_SIZE \
  23. (offsetof(struct ip_tunnel_key, tp_dst) + \
  24. FIELD_SIZEOF(struct ip_tunnel_key, tp_dst))
  25. struct ip_tunnel_key {
  26. __be64 tun_id;
  27. __be32 ipv4_src;
  28. __be32 ipv4_dst;
  29. __be16 tun_flags;
  30. __u8 ipv4_tos;
  31. __u8 ipv4_ttl;
  32. __be16 tp_src;
  33. __be16 tp_dst;
  34. } __packed __aligned(4); /* Minimize padding. */
  35. /* Indicates whether the tunnel info structure represents receive
  36. * or transmit tunnel parameters.
  37. */
  38. enum {
  39. IP_TUNNEL_INFO_RX,
  40. IP_TUNNEL_INFO_TX,
  41. };
  42. struct ip_tunnel_info {
  43. struct ip_tunnel_key key;
  44. const void *options;
  45. u8 options_len;
  46. u8 mode;
  47. };
  48. /* 6rd prefix/relay information */
  49. #ifdef CONFIG_IPV6_SIT_6RD
  50. struct ip_tunnel_6rd_parm {
  51. struct in6_addr prefix;
  52. __be32 relay_prefix;
  53. u16 prefixlen;
  54. u16 relay_prefixlen;
  55. };
  56. #endif
  57. struct ip_tunnel_encap {
  58. __u16 type;
  59. __u16 flags;
  60. __be16 sport;
  61. __be16 dport;
  62. };
  63. struct ip_tunnel_prl_entry {
  64. struct ip_tunnel_prl_entry __rcu *next;
  65. __be32 addr;
  66. u16 flags;
  67. struct rcu_head rcu_head;
  68. };
  69. struct ip_tunnel_dst {
  70. struct dst_entry __rcu *dst;
  71. __be32 saddr;
  72. };
  73. struct ip_tunnel {
  74. struct ip_tunnel __rcu *next;
  75. struct hlist_node hash_node;
  76. struct net_device *dev;
  77. struct net *net; /* netns for packet i/o */
  78. int err_count; /* Number of arrived ICMP errors */
  79. unsigned long err_time; /* Time when the last ICMP error
  80. * arrived */
  81. /* These four fields used only by GRE */
  82. __u32 i_seqno; /* The last seen seqno */
  83. __u32 o_seqno; /* The last output seqno */
  84. int tun_hlen; /* Precalculated header length */
  85. int mlink;
  86. struct ip_tunnel_dst __percpu *dst_cache;
  87. struct ip_tunnel_parm parms;
  88. int encap_hlen; /* Encap header length (FOU,GUE) */
  89. struct ip_tunnel_encap encap;
  90. int hlen; /* tun_hlen + encap_hlen */
  91. /* for SIT */
  92. #ifdef CONFIG_IPV6_SIT_6RD
  93. struct ip_tunnel_6rd_parm ip6rd;
  94. #endif
  95. struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */
  96. unsigned int prl_count; /* # of entries in PRL */
  97. int ip_tnl_net_id;
  98. struct gro_cells gro_cells;
  99. };
  100. #define TUNNEL_CSUM __cpu_to_be16(0x01)
  101. #define TUNNEL_ROUTING __cpu_to_be16(0x02)
  102. #define TUNNEL_KEY __cpu_to_be16(0x04)
  103. #define TUNNEL_SEQ __cpu_to_be16(0x08)
  104. #define TUNNEL_STRICT __cpu_to_be16(0x10)
  105. #define TUNNEL_REC __cpu_to_be16(0x20)
  106. #define TUNNEL_VERSION __cpu_to_be16(0x40)
  107. #define TUNNEL_NO_KEY __cpu_to_be16(0x80)
  108. #define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100)
  109. #define TUNNEL_OAM __cpu_to_be16(0x0200)
  110. #define TUNNEL_CRIT_OPT __cpu_to_be16(0x0400)
  111. #define TUNNEL_GENEVE_OPT __cpu_to_be16(0x0800)
  112. #define TUNNEL_VXLAN_OPT __cpu_to_be16(0x1000)
  113. #define TUNNEL_OPTIONS_PRESENT (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT)
  114. struct tnl_ptk_info {
  115. __be16 flags;
  116. __be16 proto;
  117. __be32 key;
  118. __be32 seq;
  119. };
  120. #define PACKET_RCVD 0
  121. #define PACKET_REJECT 1
  122. #define IP_TNL_HASH_BITS 7
  123. #define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS)
  124. struct ip_tunnel_net {
  125. struct net_device *fb_tunnel_dev;
  126. struct hlist_head tunnels[IP_TNL_HASH_SIZE];
  127. };
  128. struct ip_tunnel_encap_ops {
  129. size_t (*encap_hlen)(struct ip_tunnel_encap *e);
  130. int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
  131. u8 *protocol, struct flowi4 *fl4);
  132. };
  133. #define MAX_IPTUN_ENCAP_OPS 8
  134. extern const struct ip_tunnel_encap_ops __rcu *
  135. iptun_encaps[MAX_IPTUN_ENCAP_OPS];
  136. int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
  137. unsigned int num);
  138. int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
  139. unsigned int num);
  140. static inline void __ip_tunnel_info_init(struct ip_tunnel_info *tun_info,
  141. __be32 saddr, __be32 daddr,
  142. u8 tos, u8 ttl,
  143. __be16 tp_src, __be16 tp_dst,
  144. __be64 tun_id, __be16 tun_flags,
  145. const void *opts, u8 opts_len)
  146. {
  147. tun_info->key.tun_id = tun_id;
  148. tun_info->key.ipv4_src = saddr;
  149. tun_info->key.ipv4_dst = daddr;
  150. tun_info->key.ipv4_tos = tos;
  151. tun_info->key.ipv4_ttl = ttl;
  152. tun_info->key.tun_flags = tun_flags;
  153. /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
  154. * the upper tunnel are used.
  155. * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
  156. */
  157. tun_info->key.tp_src = tp_src;
  158. tun_info->key.tp_dst = tp_dst;
  159. /* Clear struct padding. */
  160. if (sizeof(tun_info->key) != IP_TUNNEL_KEY_SIZE)
  161. memset((unsigned char *)&tun_info->key + IP_TUNNEL_KEY_SIZE,
  162. 0, sizeof(tun_info->key) - IP_TUNNEL_KEY_SIZE);
  163. tun_info->options = opts;
  164. tun_info->options_len = opts_len;
  165. }
  166. static inline void ip_tunnel_info_init(struct ip_tunnel_info *tun_info,
  167. const struct iphdr *iph,
  168. __be16 tp_src, __be16 tp_dst,
  169. __be64 tun_id, __be16 tun_flags,
  170. const void *opts, u8 opts_len)
  171. {
  172. __ip_tunnel_info_init(tun_info, iph->saddr, iph->daddr,
  173. iph->tos, iph->ttl, tp_src, tp_dst,
  174. tun_id, tun_flags, opts, opts_len);
  175. }
  176. #ifdef CONFIG_INET
  177. int ip_tunnel_init(struct net_device *dev);
  178. void ip_tunnel_uninit(struct net_device *dev);
  179. void ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
  180. struct net *ip_tunnel_get_link_net(const struct net_device *dev);
  181. int ip_tunnel_get_iflink(const struct net_device *dev);
  182. int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
  183. struct rtnl_link_ops *ops, char *devname);
  184. void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
  185. void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  186. const struct iphdr *tnl_params, const u8 protocol);
  187. int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
  188. int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
  189. u8 *protocol, struct flowi4 *fl4);
  190. int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
  191. struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
  192. struct rtnl_link_stats64 *tot);
  193. struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
  194. int link, __be16 flags,
  195. __be32 remote, __be32 local,
  196. __be32 key);
  197. int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
  198. const struct tnl_ptk_info *tpi, bool log_ecn_error);
  199. int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
  200. struct ip_tunnel_parm *p);
  201. int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
  202. struct ip_tunnel_parm *p);
  203. void ip_tunnel_setup(struct net_device *dev, int net_id);
  204. void ip_tunnel_dst_reset_all(struct ip_tunnel *t);
  205. int ip_tunnel_encap_setup(struct ip_tunnel *t,
  206. struct ip_tunnel_encap *ipencap);
  207. /* Extract dsfield from inner protocol */
  208. static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
  209. const struct sk_buff *skb)
  210. {
  211. if (skb->protocol == htons(ETH_P_IP))
  212. return iph->tos;
  213. else if (skb->protocol == htons(ETH_P_IPV6))
  214. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  215. else
  216. return 0;
  217. }
  218. /* Propogate ECN bits out */
  219. static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
  220. const struct sk_buff *skb)
  221. {
  222. u8 inner = ip_tunnel_get_dsfield(iph, skb);
  223. return INET_ECN_encapsulate(tos, inner);
  224. }
  225. int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto);
  226. int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  227. __be32 src, __be32 dst, __u8 proto,
  228. __u8 tos, __u8 ttl, __be16 df, bool xnet);
  229. struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, bool gre_csum,
  230. int gso_type_mask);
  231. static inline void iptunnel_xmit_stats(int err,
  232. struct net_device_stats *err_stats,
  233. struct pcpu_sw_netstats __percpu *stats)
  234. {
  235. if (err > 0) {
  236. struct pcpu_sw_netstats *tstats = this_cpu_ptr(stats);
  237. u64_stats_update_begin(&tstats->syncp);
  238. tstats->tx_bytes += err;
  239. tstats->tx_packets++;
  240. u64_stats_update_end(&tstats->syncp);
  241. } else if (err < 0) {
  242. err_stats->tx_errors++;
  243. err_stats->tx_aborted_errors++;
  244. } else {
  245. err_stats->tx_dropped++;
  246. }
  247. }
  248. static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info, size_t n)
  249. {
  250. return info + 1;
  251. }
  252. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  253. {
  254. return (struct ip_tunnel_info *)lwtstate->data;
  255. }
  256. extern struct static_key ip_tunnel_metadata_cnt;
  257. /* Returns > 0 if metadata should be collected */
  258. static inline int ip_tunnel_collect_metadata(void)
  259. {
  260. return static_key_false(&ip_tunnel_metadata_cnt);
  261. }
  262. void __init ip_tunnel_core_init(void);
  263. void ip_tunnel_need_metadata(void);
  264. void ip_tunnel_unneed_metadata(void);
  265. #else /* CONFIG_INET */
  266. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  267. {
  268. return NULL;
  269. }
  270. static inline void ip_tunnel_need_metadata(void)
  271. {
  272. }
  273. static inline void ip_tunnel_unneed_metadata(void)
  274. {
  275. }
  276. #endif /* CONFIG_INET */
  277. #endif /* __NET_IP_TUNNELS_H */