ip_tunnels.h 4.8 KB

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