ip_tunnels.h 9.9 KB

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