ip_tunnels.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/rtnetlink.h>
  13. #if IS_ENABLED(CONFIG_IPV6)
  14. #include <net/ipv6.h>
  15. #include <net/ip6_fib.h>
  16. #include <net/ip6_route.h>
  17. #endif
  18. /* Keep error state on tunnel for 30 sec */
  19. #define IPTUNNEL_ERR_TIMEO (30*HZ)
  20. /* 6rd prefix/relay information */
  21. #ifdef CONFIG_IPV6_SIT_6RD
  22. struct ip_tunnel_6rd_parm {
  23. struct in6_addr prefix;
  24. __be32 relay_prefix;
  25. u16 prefixlen;
  26. u16 relay_prefixlen;
  27. };
  28. #endif
  29. struct ip_tunnel_prl_entry {
  30. struct ip_tunnel_prl_entry __rcu *next;
  31. __be32 addr;
  32. u16 flags;
  33. struct rcu_head rcu_head;
  34. };
  35. struct ip_tunnel_dst {
  36. struct dst_entry __rcu *dst;
  37. };
  38. struct ip_tunnel {
  39. struct ip_tunnel __rcu *next;
  40. struct hlist_node hash_node;
  41. struct net_device *dev;
  42. struct net *net; /* netns for packet i/o */
  43. int err_count; /* Number of arrived ICMP errors */
  44. unsigned long err_time; /* Time when the last ICMP error
  45. * arrived */
  46. /* These four fields used only by GRE */
  47. __u32 i_seqno; /* The last seen seqno */
  48. __u32 o_seqno; /* The last output seqno */
  49. int hlen; /* Precalculated header length */
  50. int mlink;
  51. struct ip_tunnel_dst __percpu *dst_cache;
  52. struct ip_tunnel_parm parms;
  53. /* for SIT */
  54. #ifdef CONFIG_IPV6_SIT_6RD
  55. struct ip_tunnel_6rd_parm ip6rd;
  56. #endif
  57. struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */
  58. unsigned int prl_count; /* # of entries in PRL */
  59. int ip_tnl_net_id;
  60. struct gro_cells gro_cells;
  61. };
  62. #define TUNNEL_CSUM __cpu_to_be16(0x01)
  63. #define TUNNEL_ROUTING __cpu_to_be16(0x02)
  64. #define TUNNEL_KEY __cpu_to_be16(0x04)
  65. #define TUNNEL_SEQ __cpu_to_be16(0x08)
  66. #define TUNNEL_STRICT __cpu_to_be16(0x10)
  67. #define TUNNEL_REC __cpu_to_be16(0x20)
  68. #define TUNNEL_VERSION __cpu_to_be16(0x40)
  69. #define TUNNEL_NO_KEY __cpu_to_be16(0x80)
  70. #define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100)
  71. struct tnl_ptk_info {
  72. __be16 flags;
  73. __be16 proto;
  74. __be32 key;
  75. __be32 seq;
  76. };
  77. #define PACKET_RCVD 0
  78. #define PACKET_REJECT 1
  79. #define IP_TNL_HASH_BITS 7
  80. #define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS)
  81. struct ip_tunnel_net {
  82. struct net_device *fb_tunnel_dev;
  83. struct hlist_head tunnels[IP_TNL_HASH_SIZE];
  84. };
  85. #ifdef CONFIG_INET
  86. int ip_tunnel_init(struct net_device *dev);
  87. void ip_tunnel_uninit(struct net_device *dev);
  88. void ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
  89. int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
  90. struct rtnl_link_ops *ops, char *devname);
  91. void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
  92. void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
  93. const struct iphdr *tnl_params, const u8 protocol);
  94. int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
  95. int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
  96. struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
  97. struct rtnl_link_stats64 *tot);
  98. struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
  99. int link, __be16 flags,
  100. __be32 remote, __be32 local,
  101. __be32 key);
  102. int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
  103. const struct tnl_ptk_info *tpi, bool log_ecn_error);
  104. int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
  105. struct ip_tunnel_parm *p);
  106. int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
  107. struct ip_tunnel_parm *p);
  108. void ip_tunnel_setup(struct net_device *dev, int net_id);
  109. void ip_tunnel_dst_reset_all(struct ip_tunnel *t);
  110. /* Extract dsfield from inner protocol */
  111. static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
  112. const struct sk_buff *skb)
  113. {
  114. if (skb->protocol == htons(ETH_P_IP))
  115. return iph->tos;
  116. else if (skb->protocol == htons(ETH_P_IPV6))
  117. return ipv6_get_dsfield((const struct ipv6hdr *)iph);
  118. else
  119. return 0;
  120. }
  121. /* Propogate ECN bits out */
  122. static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
  123. const struct sk_buff *skb)
  124. {
  125. u8 inner = ip_tunnel_get_dsfield(iph, skb);
  126. return INET_ECN_encapsulate(tos, inner);
  127. }
  128. int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto);
  129. int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  130. __be32 src, __be32 dst, __u8 proto,
  131. __u8 tos, __u8 ttl, __be16 df, bool xnet);
  132. struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, bool gre_csum,
  133. int gso_type_mask);
  134. static inline void iptunnel_xmit_stats(int err,
  135. struct net_device_stats *err_stats,
  136. struct pcpu_sw_netstats __percpu *stats)
  137. {
  138. if (err > 0) {
  139. struct pcpu_sw_netstats *tstats = this_cpu_ptr(stats);
  140. u64_stats_update_begin(&tstats->syncp);
  141. tstats->tx_bytes += err;
  142. tstats->tx_packets++;
  143. u64_stats_update_end(&tstats->syncp);
  144. } else if (err < 0) {
  145. err_stats->tx_errors++;
  146. err_stats->tx_aborted_errors++;
  147. } else {
  148. err_stats->tx_dropped++;
  149. }
  150. }
  151. #endif /* CONFIG_INET */
  152. #endif /* __NET_IP_TUNNELS_H */