ip6_tunnel.h 5.0 KB

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