ip_tunnels.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/ip.h>
  12. #include <net/netns/generic.h>
  13. #include <net/rtnetlink.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. /* 6rd prefix/relay information */
  22. #ifdef CONFIG_IPV6_SIT_6RD
  23. struct ip_tunnel_6rd_parm {
  24. struct in6_addr prefix;
  25. __be32 relay_prefix;
  26. u16 prefixlen;
  27. u16 relay_prefixlen;
  28. };
  29. #endif
  30. struct ip_tunnel_encap {
  31. __u16 type;
  32. __u16 flags;
  33. __be16 sport;
  34. __be16 dport;
  35. };
  36. struct ip_tunnel_prl_entry {
  37. struct ip_tunnel_prl_entry __rcu *next;
  38. __be32 addr;
  39. u16 flags;
  40. struct rcu_head rcu_head;
  41. };
  42. struct ip_tunnel_dst {
  43. struct dst_entry __rcu *dst;
  44. __be32 saddr;
  45. };
  46. struct ip_tunnel {
  47. struct ip_tunnel __rcu *next;
  48. struct hlist_node hash_node;
  49. struct net_device *dev;
  50. struct net *net; /* netns for packet i/o */
  51. int err_count; /* Number of arrived ICMP errors */
  52. unsigned long err_time; /* Time when the last ICMP error
  53. * arrived */
  54. /* These four fields used only by GRE */
  55. __u32 i_seqno; /* The last seen seqno */
  56. __u32 o_seqno; /* The last output seqno */
  57. int tun_hlen; /* Precalculated header length */
  58. int mlink;
  59. struct ip_tunnel_dst __percpu *dst_cache;
  60. struct ip_tunnel_parm parms;
  61. int encap_hlen; /* Encap header length (FOU,GUE) */
  62. struct ip_tunnel_encap encap;
  63. int hlen; /* tun_hlen + encap_hlen */
  64. /* for SIT */
  65. #ifdef CONFIG_IPV6_SIT_6RD
  66. struct ip_tunnel_6rd_parm ip6rd;
  67. #endif
  68. struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */
  69. unsigned int prl_count; /* # of entries in PRL */
  70. int ip_tnl_net_id;
  71. struct gro_cells gro_cells;
  72. };
  73. #define TUNNEL_CSUM __cpu_to_be16(0x01)
  74. #define TUNNEL_ROUTING __cpu_to_be16(0x02)
  75. #define TUNNEL_KEY __cpu_to_be16(0x04)
  76. #define TUNNEL_SEQ __cpu_to_be16(0x08)
  77. #define TUNNEL_STRICT __cpu_to_be16(0x10)
  78. #define TUNNEL_REC __cpu_to_be16(0x20)
  79. #define TUNNEL_VERSION __cpu_to_be16(0x40)
  80. #define TUNNEL_NO_KEY __cpu_to_be16(0x80)
  81. #define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100)
  82. #define TUNNEL_OAM __cpu_to_be16(0x0200)
  83. #define TUNNEL_CRIT_OPT __cpu_to_be16(0x0400)
  84. #define TUNNEL_GENEVE_OPT __cpu_to_be16(0x0800)
  85. #define TUNNEL_VXLAN_OPT __cpu_to_be16(0x1000)
  86. #define TUNNEL_OPTIONS_PRESENT (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT)
  87. struct tnl_ptk_info {
  88. __be16 flags;
  89. __be16 proto;
  90. __be32 key;
  91. __be32 seq;
  92. };
  93. #define PACKET_RCVD 0
  94. #define PACKET_REJECT 1
  95. #define IP_TNL_HASH_BITS 7
  96. #define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS)
  97. struct ip_tunnel_net {
  98. struct net_device *fb_tunnel_dev;
  99. struct hlist_head tunnels[IP_TNL_HASH_SIZE];
  100. };
  101. struct ip_tunnel_encap_ops {
  102. size_t (*encap_hlen)(struct ip_tunnel_encap *e);
  103. int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
  104. u8 *protocol, struct flowi4 *fl4);
  105. };
  106. #define MAX_IPTUN_ENCAP_OPS 8
  107. extern const struct ip_tunnel_encap_ops __rcu *
  108. iptun_encaps[MAX_IPTUN_ENCAP_OPS];
  109. int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
  110. unsigned int num);
  111. int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
  112. unsigned int num);
  113. #ifdef CONFIG_INET
  114. int ip_tunnel_init(struct net_device *dev);
  115. void ip_tunnel_uninit(struct net_device *dev);
  116. void ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
  117. struct net *ip_tunnel_get_link_net(const struct net_device *dev);
  118. int ip_tunnel_get_iflink(const struct net_device *dev);
  119. int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
  120. struct rtnl_link_ops *ops, char *devname);
  121. void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
  122. void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  123. const struct iphdr *tnl_params, const u8 protocol);
  124. int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
  125. int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
  126. u8 *protocol, struct flowi4 *fl4);
  127. int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
  128. struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
  129. struct rtnl_link_stats64 *tot);
  130. struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
  131. int link, __be16 flags,
  132. __be32 remote, __be32 local,
  133. __be32 key);
  134. int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
  135. const struct tnl_ptk_info *tpi, bool log_ecn_error);
  136. int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
  137. struct ip_tunnel_parm *p);
  138. int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
  139. struct ip_tunnel_parm *p);
  140. void ip_tunnel_setup(struct net_device *dev, int net_id);
  141. void ip_tunnel_dst_reset_all(struct ip_tunnel *t);
  142. int ip_tunnel_encap_setup(struct ip_tunnel *t,
  143. struct ip_tunnel_encap *ipencap);
  144. /* Extract dsfield from inner protocol */
  145. static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
  146. const struct sk_buff *skb)
  147. {
  148. if (skb->protocol == htons(ETH_P_IP))
  149. return iph->tos;
  150. else if (skb->protocol == htons(ETH_P_IPV6))
  151. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  152. else
  153. return 0;
  154. }
  155. /* Propogate ECN bits out */
  156. static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
  157. const struct sk_buff *skb)
  158. {
  159. u8 inner = ip_tunnel_get_dsfield(iph, skb);
  160. return INET_ECN_encapsulate(tos, inner);
  161. }
  162. int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto);
  163. int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  164. __be32 src, __be32 dst, __u8 proto,
  165. __u8 tos, __u8 ttl, __be16 df, bool xnet);
  166. struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, bool gre_csum,
  167. int gso_type_mask);
  168. static inline void iptunnel_xmit_stats(int err,
  169. struct net_device_stats *err_stats,
  170. struct pcpu_sw_netstats __percpu *stats)
  171. {
  172. if (err > 0) {
  173. struct pcpu_sw_netstats *tstats = this_cpu_ptr(stats);
  174. u64_stats_update_begin(&tstats->syncp);
  175. tstats->tx_bytes += err;
  176. tstats->tx_packets++;
  177. u64_stats_update_end(&tstats->syncp);
  178. } else if (err < 0) {
  179. err_stats->tx_errors++;
  180. err_stats->tx_aborted_errors++;
  181. } else {
  182. err_stats->tx_dropped++;
  183. }
  184. }
  185. #endif /* CONFIG_INET */
  186. #endif /* __NET_IP_TUNNELS_H */