ip6_tunnel.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. };
  34. /* IPv6 tunnel */
  35. struct ip6_tnl {
  36. struct ip6_tnl __rcu *next; /* next tunnel in list */
  37. struct net_device *dev; /* virtual device associated with tunnel */
  38. struct net *net; /* netns for packet i/o */
  39. struct __ip6_tnl_parm parms; /* tunnel configuration parameters */
  40. struct flowi fl; /* flowi template for xmit */
  41. struct dst_cache dst_cache; /* cached dst */
  42. struct gro_cells gro_cells;
  43. int err_count;
  44. unsigned long err_time;
  45. /* These fields used only by GRE */
  46. __u32 i_seqno; /* The last seen seqno */
  47. __u32 o_seqno; /* The last output seqno */
  48. int hlen; /* tun_hlen + encap_hlen */
  49. int tun_hlen; /* Precalculated header length */
  50. int encap_hlen; /* Encap header length (FOU,GUE) */
  51. struct ip_tunnel_encap encap;
  52. int mlink;
  53. };
  54. struct ip6_tnl_encap_ops {
  55. size_t (*encap_hlen)(struct ip_tunnel_encap *e);
  56. int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
  57. u8 *protocol, struct flowi6 *fl6);
  58. };
  59. #ifdef CONFIG_INET
  60. extern const struct ip6_tnl_encap_ops __rcu *
  61. ip6tun_encaps[MAX_IPTUN_ENCAP_OPS];
  62. int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
  63. unsigned int num);
  64. int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
  65. unsigned int num);
  66. int ip6_tnl_encap_setup(struct ip6_tnl *t,
  67. struct ip_tunnel_encap *ipencap);
  68. static inline int ip6_encap_hlen(struct ip_tunnel_encap *e)
  69. {
  70. const struct ip6_tnl_encap_ops *ops;
  71. int hlen = -EINVAL;
  72. if (e->type == TUNNEL_ENCAP_NONE)
  73. return 0;
  74. if (e->type >= MAX_IPTUN_ENCAP_OPS)
  75. return -EINVAL;
  76. rcu_read_lock();
  77. ops = rcu_dereference(ip6tun_encaps[e->type]);
  78. if (likely(ops && ops->encap_hlen))
  79. hlen = ops->encap_hlen(e);
  80. rcu_read_unlock();
  81. return hlen;
  82. }
  83. static inline int ip6_tnl_encap(struct sk_buff *skb, struct ip6_tnl *t,
  84. u8 *protocol, struct flowi6 *fl6)
  85. {
  86. const struct ip6_tnl_encap_ops *ops;
  87. int ret = -EINVAL;
  88. if (t->encap.type == TUNNEL_ENCAP_NONE)
  89. return 0;
  90. if (t->encap.type >= MAX_IPTUN_ENCAP_OPS)
  91. return -EINVAL;
  92. rcu_read_lock();
  93. ops = rcu_dereference(ip6tun_encaps[t->encap.type]);
  94. if (likely(ops && ops->build_header))
  95. ret = ops->build_header(skb, &t->encap, protocol, fl6);
  96. rcu_read_unlock();
  97. return ret;
  98. }
  99. /* Tunnel encapsulation limit destination sub-option */
  100. struct ipv6_tlv_tnl_enc_lim {
  101. __u8 type; /* type-code for option */
  102. __u8 length; /* option length */
  103. __u8 encap_limit; /* tunnel encapsulation limit */
  104. } __packed;
  105. int ip6_tnl_rcv_ctl(struct ip6_tnl *t, const struct in6_addr *laddr,
  106. const struct in6_addr *raddr);
  107. int ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
  108. const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
  109. bool log_ecn_error);
  110. int ip6_tnl_xmit_ctl(struct ip6_tnl *t, const struct in6_addr *laddr,
  111. const struct in6_addr *raddr);
  112. int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
  113. struct flowi6 *fl6, int encap_limit, __u32 *pmtu, __u8 proto);
  114. __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw);
  115. __u32 ip6_tnl_get_cap(struct ip6_tnl *t, const struct in6_addr *laddr,
  116. const struct in6_addr *raddr);
  117. struct net *ip6_tnl_get_link_net(const struct net_device *dev);
  118. int ip6_tnl_get_iflink(const struct net_device *dev);
  119. int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu);
  120. static inline void ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb,
  121. struct net_device *dev)
  122. {
  123. int pkt_len, err;
  124. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  125. pkt_len = skb->len - skb_inner_network_offset(skb);
  126. err = ip6_local_out(dev_net(skb_dst(skb)->dev), sk, skb);
  127. if (dev) {
  128. if (unlikely(net_xmit_eval(err)))
  129. pkt_len = -1;
  130. iptunnel_xmit_stats(dev, pkt_len);
  131. }
  132. }
  133. #endif
  134. #endif