dst_metadata.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
  36. const struct sk_buff *skb_b)
  37. {
  38. const struct metadata_dst *a, *b;
  39. if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
  40. return 0;
  41. a = (const struct metadata_dst *) skb_dst(skb_a);
  42. b = (const struct metadata_dst *) skb_dst(skb_b);
  43. if (!a != !b || a->u.tun_info.options_len != b->u.tun_info.options_len)
  44. return 1;
  45. return memcmp(&a->u.tun_info, &b->u.tun_info,
  46. sizeof(a->u.tun_info) + a->u.tun_info.options_len);
  47. }
  48. struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
  49. struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
  50. static inline struct metadata_dst *tun_rx_dst(int md_size)
  51. {
  52. struct metadata_dst *tun_dst;
  53. tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
  54. if (!tun_dst)
  55. return NULL;
  56. tun_dst->u.tun_info.options_len = 0;
  57. tun_dst->u.tun_info.mode = 0;
  58. return tun_dst;
  59. }
  60. static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
  61. {
  62. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  63. int md_size;
  64. struct metadata_dst *new_md;
  65. if (!md_dst)
  66. return ERR_PTR(-EINVAL);
  67. md_size = md_dst->u.tun_info.options_len;
  68. new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
  69. if (!new_md)
  70. return ERR_PTR(-ENOMEM);
  71. memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
  72. sizeof(struct ip_tunnel_info) + md_size);
  73. skb_dst_drop(skb);
  74. dst_hold(&new_md->dst);
  75. skb_dst_set(skb, &new_md->dst);
  76. return new_md;
  77. }
  78. static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
  79. {
  80. struct metadata_dst *dst;
  81. dst = tun_dst_unclone(skb);
  82. if (IS_ERR(dst))
  83. return NULL;
  84. return &dst->u.tun_info;
  85. }
  86. static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
  87. __be16 flags,
  88. __be64 tunnel_id,
  89. int md_size)
  90. {
  91. const struct iphdr *iph = ip_hdr(skb);
  92. struct metadata_dst *tun_dst;
  93. tun_dst = tun_rx_dst(md_size);
  94. if (!tun_dst)
  95. return NULL;
  96. ip_tunnel_key_init(&tun_dst->u.tun_info.key,
  97. iph->saddr, iph->daddr, iph->tos, iph->ttl,
  98. 0, 0, tunnel_id, flags);
  99. return tun_dst;
  100. }
  101. static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
  102. __be16 flags,
  103. __be64 tunnel_id,
  104. int md_size)
  105. {
  106. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  107. struct metadata_dst *tun_dst;
  108. struct ip_tunnel_info *info;
  109. tun_dst = tun_rx_dst(md_size);
  110. if (!tun_dst)
  111. return NULL;
  112. info = &tun_dst->u.tun_info;
  113. info->mode = IP_TUNNEL_INFO_IPV6;
  114. info->key.tun_flags = flags;
  115. info->key.tun_id = tunnel_id;
  116. info->key.tp_src = 0;
  117. info->key.tp_dst = 0;
  118. info->key.u.ipv6.src = ip6h->saddr;
  119. info->key.u.ipv6.dst = ip6h->daddr;
  120. info->key.tos = ipv6_get_dsfield(ip6h);
  121. info->key.ttl = ip6h->hop_limit;
  122. return tun_dst;
  123. }
  124. #endif /* __NET_DST_METADATA_H */