ip6_tunnel.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _NET_IP6_TUNNEL_H
  3. #define _NET_IP6_TUNNEL_H
  4. #include <linux/ipv6.h>
  5. #include <linux/netdevice.h>
  6. #include <linux/if_tunnel.h>
  7. #include <linux/ip6_tunnel.h>
  8. #include <net/ip_tunnels.h>
  9. #include <net/dst_cache.h>
  10. #define IP6TUNNEL_ERR_TIMEO (30*HZ)
  11. /* capable of sending packets */
  12. #define IP6_TNL_F_CAP_XMIT 0x10000
  13. /* capable of receiving packets */
  14. #define IP6_TNL_F_CAP_RCV 0x20000
  15. /* determine capability on a per-packet basis */
  16. #define IP6_TNL_F_CAP_PER_PACKET 0x40000
  17. struct __ip6_tnl_parm {
  18. char name[IFNAMSIZ]; /* name of tunnel device */
  19. int link; /* ifindex of underlying L2 interface */
  20. __u8 proto; /* tunnel protocol */
  21. __u8 encap_limit; /* encapsulation limit for tunnel */
  22. __u8 hop_limit; /* hop limit for tunnel */
  23. bool collect_md;
  24. __be32 flowinfo; /* traffic class and flowlabel for tunnel */
  25. __u32 flags; /* tunnel flags */
  26. struct in6_addr laddr; /* local tunnel end-point address */
  27. struct in6_addr raddr; /* remote tunnel end-point address */
  28. __be16 i_flags;
  29. __be16 o_flags;
  30. __be32 i_key;
  31. __be32 o_key;
  32. __u32 fwmark;
  33. __u32 index; /* ERSPAN type II index */
  34. __u8 erspan_ver; /* ERSPAN version */
  35. __u8 dir; /* direction */
  36. __u16 hwid; /* hwid */
  37. };
  38. /* IPv6 tunnel */
  39. struct ip6_tnl {
  40. struct ip6_tnl __rcu *next; /* next tunnel in list */
  41. struct net_device *dev; /* virtual device associated with tunnel */
  42. struct net *net; /* netns for packet i/o */
  43. struct __ip6_tnl_parm parms; /* tunnel configuration parameters */
  44. struct flowi fl; /* flowi template for xmit */
  45. struct dst_cache dst_cache; /* cached dst */
  46. struct gro_cells gro_cells;
  47. int err_count;
  48. unsigned long err_time;
  49. /* These fields used only by GRE */
  50. __u32 i_seqno; /* The last seen seqno */
  51. __u32 o_seqno; /* The last output seqno */
  52. int hlen; /* tun_hlen + encap_hlen */
  53. int tun_hlen; /* Precalculated header length */
  54. int encap_hlen; /* Encap header length (FOU,GUE) */
  55. struct ip_tunnel_encap encap;
  56. int mlink;
  57. };
  58. struct ip6_tnl_encap_ops {
  59. size_t (*encap_hlen)(struct ip_tunnel_encap *e);
  60. int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
  61. u8 *protocol, struct flowi6 *fl6);
  62. };
  63. #ifdef CONFIG_INET
  64. extern const struct ip6_tnl_encap_ops __rcu *
  65. ip6tun_encaps[MAX_IPTUN_ENCAP_OPS];
  66. int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
  67. unsigned int num);
  68. int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
  69. unsigned int num);
  70. int ip6_tnl_encap_setup(struct ip6_tnl *t,
  71. struct ip_tunnel_encap *ipencap);
  72. static inline int ip6_encap_hlen(struct ip_tunnel_encap *e)
  73. {
  74. const struct ip6_tnl_encap_ops *ops;
  75. int hlen = -EINVAL;
  76. if (e->type == TUNNEL_ENCAP_NONE)
  77. return 0;
  78. if (e->type >= MAX_IPTUN_ENCAP_OPS)
  79. return -EINVAL;
  80. rcu_read_lock();
  81. ops = rcu_dereference(ip6tun_encaps[e->type]);
  82. if (likely(ops && ops->encap_hlen))
  83. hlen = ops->encap_hlen(e);
  84. rcu_read_unlock();
  85. return hlen;
  86. }
  87. static inline int ip6_tnl_encap(struct sk_buff *skb, struct ip6_tnl *t,
  88. u8 *protocol, struct flowi6 *fl6)
  89. {
  90. const struct ip6_tnl_encap_ops *ops;
  91. int ret = -EINVAL;
  92. if (t->encap.type == TUNNEL_ENCAP_NONE)
  93. return 0;
  94. if (t->encap.type >= MAX_IPTUN_ENCAP_OPS)
  95. return -EINVAL;
  96. rcu_read_lock();
  97. ops = rcu_dereference(ip6tun_encaps[t->encap.type]);
  98. if (likely(ops && ops->build_header))
  99. ret = ops->build_header(skb, &t->encap, protocol, fl6);
  100. rcu_read_unlock();
  101. return ret;
  102. }
  103. /* Tunnel encapsulation limit destination sub-option */
  104. struct ipv6_tlv_tnl_enc_lim {
  105. __u8 type; /* type-code for option */
  106. __u8 length; /* option length */
  107. __u8 encap_limit; /* tunnel encapsulation limit */
  108. } __packed;
  109. int ip6_tnl_rcv_ctl(struct ip6_tnl *t, const struct in6_addr *laddr,
  110. const struct in6_addr *raddr);
  111. int ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
  112. const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
  113. bool log_ecn_error);
  114. int ip6_tnl_xmit_ctl(struct ip6_tnl *t, const struct in6_addr *laddr,
  115. const struct in6_addr *raddr);
  116. int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
  117. struct flowi6 *fl6, int encap_limit, __u32 *pmtu, __u8 proto);
  118. __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw);
  119. __u32 ip6_tnl_get_cap(struct ip6_tnl *t, const struct in6_addr *laddr,
  120. const struct in6_addr *raddr);
  121. struct net *ip6_tnl_get_link_net(const struct net_device *dev);
  122. int ip6_tnl_get_iflink(const struct net_device *dev);
  123. int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu);
  124. static inline void ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb,
  125. struct net_device *dev)
  126. {
  127. int pkt_len, err;
  128. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  129. pkt_len = skb->len - skb_inner_network_offset(skb);
  130. err = ip6_local_out(dev_net(skb_dst(skb)->dev), sk, skb);
  131. if (dev) {
  132. if (unlikely(net_xmit_eval(err)))
  133. pkt_len = -1;
  134. iptunnel_xmit_stats(dev, pkt_len);
  135. }
  136. }
  137. #endif
  138. #endif