dst_metadata.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef __NET_DST_METADATA_H
  2. #define __NET_DST_METADATA_H 1
  3. #include <linux/skbuff.h>
  4. #include <net/ip_tunnels.h>
  5. #include <net/dst.h>
  6. struct metadata_dst {
  7. struct dst_entry dst;
  8. union {
  9. struct ip_tunnel_info tun_info;
  10. } u;
  11. };
  12. static inline struct metadata_dst *skb_metadata_dst(struct sk_buff *skb)
  13. {
  14. struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
  15. if (md_dst && md_dst->dst.flags & DST_METADATA)
  16. return md_dst;
  17. return NULL;
  18. }
  19. static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
  20. {
  21. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  22. struct dst_entry *dst;
  23. if (md_dst)
  24. return &md_dst->u.tun_info;
  25. dst = skb_dst(skb);
  26. if (dst && dst->lwtstate)
  27. return lwt_tun_info(dst->lwtstate);
  28. return NULL;
  29. }
  30. static inline bool skb_valid_dst(const struct sk_buff *skb)
  31. {
  32. struct dst_entry *dst = skb_dst(skb);
  33. return dst && !(dst->flags & DST_METADATA);
  34. }
  35. struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
  36. struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
  37. static inline struct metadata_dst *tun_rx_dst(int md_size)
  38. {
  39. struct metadata_dst *tun_dst;
  40. tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
  41. if (!tun_dst)
  42. return NULL;
  43. tun_dst->u.tun_info.options_len = 0;
  44. tun_dst->u.tun_info.mode = 0;
  45. return tun_dst;
  46. }
  47. static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
  48. {
  49. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  50. int md_size = md_dst->u.tun_info.options_len;
  51. struct metadata_dst *new_md;
  52. if (!md_dst)
  53. return ERR_PTR(-EINVAL);
  54. new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
  55. if (!new_md)
  56. return ERR_PTR(-ENOMEM);
  57. memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
  58. sizeof(struct ip_tunnel_info) + md_size);
  59. skb_dst_drop(skb);
  60. dst_hold(&new_md->dst);
  61. skb_dst_set(skb, &new_md->dst);
  62. return new_md;
  63. }
  64. static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
  65. {
  66. struct metadata_dst *dst;
  67. dst = tun_dst_unclone(skb);
  68. if (IS_ERR(dst))
  69. return NULL;
  70. return &dst->u.tun_info;
  71. }
  72. static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
  73. __be16 flags,
  74. __be64 tunnel_id,
  75. int md_size)
  76. {
  77. const struct iphdr *iph = ip_hdr(skb);
  78. struct metadata_dst *tun_dst;
  79. tun_dst = tun_rx_dst(md_size);
  80. if (!tun_dst)
  81. return NULL;
  82. ip_tunnel_key_init(&tun_dst->u.tun_info.key,
  83. iph->saddr, iph->daddr, iph->tos, iph->ttl,
  84. 0, 0, tunnel_id, flags);
  85. return tun_dst;
  86. }
  87. static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
  88. __be16 flags,
  89. __be64 tunnel_id,
  90. int md_size)
  91. {
  92. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  93. struct metadata_dst *tun_dst;
  94. struct ip_tunnel_info *info;
  95. tun_dst = tun_rx_dst(md_size);
  96. if (!tun_dst)
  97. return NULL;
  98. info = &tun_dst->u.tun_info;
  99. info->mode = IP_TUNNEL_INFO_IPV6;
  100. info->key.tun_flags = flags;
  101. info->key.tun_id = tunnel_id;
  102. info->key.tp_src = 0;
  103. info->key.tp_dst = 0;
  104. info->key.u.ipv6.src = ip6h->saddr;
  105. info->key.u.ipv6.dst = ip6h->daddr;
  106. info->key.tos = ipv6_get_dsfield(ip6h);
  107. info->key.ttl = ip6h->hop_limit;
  108. return tun_dst;
  109. }
  110. #endif /* __NET_DST_METADATA_H */