ip_tunnels.h 9.6 KB

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