ip_tunnels.h 9.6 KB

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