ip_tunnels.h 5.1 KB

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