ip_tunnels.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. int err_count; /* Number of arrived ICMP errors */
  93. unsigned long err_time; /* Time when the last ICMP error
  94. * arrived */
  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. int mlink;
  100. struct dst_cache dst_cache;
  101. struct ip_tunnel_parm parms;
  102. int encap_hlen; /* Encap header length (FOU,GUE) */
  103. struct ip_tunnel_encap encap;
  104. int hlen; /* tun_hlen + encap_hlen */
  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. };
  115. #define TUNNEL_CSUM __cpu_to_be16(0x01)
  116. #define TUNNEL_ROUTING __cpu_to_be16(0x02)
  117. #define TUNNEL_KEY __cpu_to_be16(0x04)
  118. #define TUNNEL_SEQ __cpu_to_be16(0x08)
  119. #define TUNNEL_STRICT __cpu_to_be16(0x10)
  120. #define TUNNEL_REC __cpu_to_be16(0x20)
  121. #define TUNNEL_VERSION __cpu_to_be16(0x40)
  122. #define TUNNEL_NO_KEY __cpu_to_be16(0x80)
  123. #define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100)
  124. #define TUNNEL_OAM __cpu_to_be16(0x0200)
  125. #define TUNNEL_CRIT_OPT __cpu_to_be16(0x0400)
  126. #define TUNNEL_GENEVE_OPT __cpu_to_be16(0x0800)
  127. #define TUNNEL_VXLAN_OPT __cpu_to_be16(0x1000)
  128. #define TUNNEL_NOCACHE __cpu_to_be16(0x2000)
  129. #define TUNNEL_OPTIONS_PRESENT (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT)
  130. struct tnl_ptk_info {
  131. __be16 flags;
  132. __be16 proto;
  133. __be32 key;
  134. __be32 seq;
  135. };
  136. #define PACKET_RCVD 0
  137. #define PACKET_REJECT 1
  138. #define IP_TNL_HASH_BITS 7
  139. #define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS)
  140. struct ip_tunnel_net {
  141. struct net_device *fb_tunnel_dev;
  142. struct hlist_head tunnels[IP_TNL_HASH_SIZE];
  143. struct ip_tunnel __rcu *collect_md_tun;
  144. };
  145. struct ip_tunnel_encap_ops {
  146. size_t (*encap_hlen)(struct ip_tunnel_encap *e);
  147. int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
  148. u8 *protocol, struct flowi4 *fl4);
  149. };
  150. #define MAX_IPTUN_ENCAP_OPS 8
  151. extern const struct ip_tunnel_encap_ops __rcu *
  152. iptun_encaps[MAX_IPTUN_ENCAP_OPS];
  153. int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
  154. unsigned int num);
  155. int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
  156. unsigned int num);
  157. static inline void ip_tunnel_key_init(struct ip_tunnel_key *key,
  158. __be32 saddr, __be32 daddr,
  159. u8 tos, u8 ttl, __be32 label,
  160. __be16 tp_src, __be16 tp_dst,
  161. __be64 tun_id, __be16 tun_flags)
  162. {
  163. key->tun_id = tun_id;
  164. key->u.ipv4.src = saddr;
  165. key->u.ipv4.dst = daddr;
  166. memset((unsigned char *)key + IP_TUNNEL_KEY_IPV4_PAD,
  167. 0, IP_TUNNEL_KEY_IPV4_PAD_LEN);
  168. key->tos = tos;
  169. key->ttl = ttl;
  170. key->label = label;
  171. key->tun_flags = tun_flags;
  172. /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
  173. * the upper tunnel are used.
  174. * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
  175. */
  176. key->tp_src = tp_src;
  177. key->tp_dst = tp_dst;
  178. /* Clear struct padding. */
  179. if (sizeof(*key) != IP_TUNNEL_KEY_SIZE)
  180. memset((unsigned char *)key + IP_TUNNEL_KEY_SIZE,
  181. 0, sizeof(*key) - IP_TUNNEL_KEY_SIZE);
  182. }
  183. static inline bool
  184. ip_tunnel_dst_cache_usable(const struct sk_buff *skb,
  185. const struct ip_tunnel_info *info)
  186. {
  187. if (skb->mark)
  188. return false;
  189. if (!info)
  190. return true;
  191. if (info->key.tun_flags & TUNNEL_NOCACHE)
  192. return false;
  193. return true;
  194. }
  195. static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info
  196. *tun_info)
  197. {
  198. return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET;
  199. }
  200. #ifdef CONFIG_INET
  201. int ip_tunnel_init(struct net_device *dev);
  202. void ip_tunnel_uninit(struct net_device *dev);
  203. void ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
  204. struct net *ip_tunnel_get_link_net(const struct net_device *dev);
  205. int ip_tunnel_get_iflink(const struct net_device *dev);
  206. int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
  207. struct rtnl_link_ops *ops, char *devname);
  208. void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
  209. void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  210. const struct iphdr *tnl_params, const u8 protocol);
  211. int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
  212. int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
  213. u8 *protocol, struct flowi4 *fl4);
  214. int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict);
  215. int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
  216. struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
  217. struct rtnl_link_stats64 *tot);
  218. struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
  219. int link, __be16 flags,
  220. __be32 remote, __be32 local,
  221. __be32 key);
  222. int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
  223. const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
  224. bool log_ecn_error);
  225. int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
  226. struct ip_tunnel_parm *p);
  227. int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
  228. struct ip_tunnel_parm *p);
  229. void ip_tunnel_setup(struct net_device *dev, int net_id);
  230. int ip_tunnel_encap_setup(struct ip_tunnel *t,
  231. struct ip_tunnel_encap *ipencap);
  232. /* Extract dsfield from inner protocol */
  233. static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
  234. const struct sk_buff *skb)
  235. {
  236. if (skb->protocol == htons(ETH_P_IP))
  237. return iph->tos;
  238. else if (skb->protocol == htons(ETH_P_IPV6))
  239. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  240. else
  241. return 0;
  242. }
  243. /* Propogate ECN bits out */
  244. static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
  245. const struct sk_buff *skb)
  246. {
  247. u8 inner = ip_tunnel_get_dsfield(iph, skb);
  248. return INET_ECN_encapsulate(tos, inner);
  249. }
  250. int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto,
  251. bool xnet);
  252. void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  253. __be32 src, __be32 dst, u8 proto,
  254. u8 tos, u8 ttl, __be16 df, bool xnet);
  255. struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
  256. gfp_t flags);
  257. struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, int gso_type_mask);
  258. static inline int iptunnel_pull_offloads(struct sk_buff *skb)
  259. {
  260. if (skb_is_gso(skb)) {
  261. int err;
  262. err = skb_unclone(skb, GFP_ATOMIC);
  263. if (unlikely(err))
  264. return err;
  265. skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
  266. NETIF_F_GSO_SHIFT);
  267. }
  268. skb->encapsulation = 0;
  269. return 0;
  270. }
  271. static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len)
  272. {
  273. if (pkt_len > 0) {
  274. struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats);
  275. u64_stats_update_begin(&tstats->syncp);
  276. tstats->tx_bytes += pkt_len;
  277. tstats->tx_packets++;
  278. u64_stats_update_end(&tstats->syncp);
  279. put_cpu_ptr(tstats);
  280. } else {
  281. struct net_device_stats *err_stats = &dev->stats;
  282. if (pkt_len < 0) {
  283. err_stats->tx_errors++;
  284. err_stats->tx_aborted_errors++;
  285. } else {
  286. err_stats->tx_dropped++;
  287. }
  288. }
  289. }
  290. static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info)
  291. {
  292. return info + 1;
  293. }
  294. static inline void ip_tunnel_info_opts_get(void *to,
  295. const struct ip_tunnel_info *info)
  296. {
  297. memcpy(to, info + 1, info->options_len);
  298. }
  299. static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
  300. const void *from, int len)
  301. {
  302. memcpy(ip_tunnel_info_opts(info), from, len);
  303. info->options_len = len;
  304. }
  305. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  306. {
  307. return (struct ip_tunnel_info *)lwtstate->data;
  308. }
  309. extern struct static_key ip_tunnel_metadata_cnt;
  310. /* Returns > 0 if metadata should be collected */
  311. static inline int ip_tunnel_collect_metadata(void)
  312. {
  313. return static_key_false(&ip_tunnel_metadata_cnt);
  314. }
  315. void __init ip_tunnel_core_init(void);
  316. void ip_tunnel_need_metadata(void);
  317. void ip_tunnel_unneed_metadata(void);
  318. #else /* CONFIG_INET */
  319. static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
  320. {
  321. return NULL;
  322. }
  323. static inline void ip_tunnel_need_metadata(void)
  324. {
  325. }
  326. static inline void ip_tunnel_unneed_metadata(void)
  327. {
  328. }
  329. static inline void ip_tunnel_info_opts_get(void *to,
  330. const struct ip_tunnel_info *info)
  331. {
  332. }
  333. static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
  334. const void *from, int len)
  335. {
  336. info->options_len = 0;
  337. }
  338. #endif /* CONFIG_INET */
  339. #endif /* __NET_IP_TUNNELS_H */