dst_metadata.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. void metadata_dst_free(struct metadata_dst *);
  49. struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
  50. struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
  51. static inline struct metadata_dst *tun_rx_dst(int md_size)
  52. {
  53. struct metadata_dst *tun_dst;
  54. tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
  55. if (!tun_dst)
  56. return NULL;
  57. tun_dst->u.tun_info.options_len = 0;
  58. tun_dst->u.tun_info.mode = 0;
  59. return tun_dst;
  60. }
  61. static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
  62. {
  63. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  64. int md_size;
  65. struct metadata_dst *new_md;
  66. if (!md_dst)
  67. return ERR_PTR(-EINVAL);
  68. md_size = md_dst->u.tun_info.options_len;
  69. new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
  70. if (!new_md)
  71. return ERR_PTR(-ENOMEM);
  72. memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
  73. sizeof(struct ip_tunnel_info) + md_size);
  74. skb_dst_drop(skb);
  75. dst_hold(&new_md->dst);
  76. skb_dst_set(skb, &new_md->dst);
  77. return new_md;
  78. }
  79. static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
  80. {
  81. struct metadata_dst *dst;
  82. dst = tun_dst_unclone(skb);
  83. if (IS_ERR(dst))
  84. return NULL;
  85. return &dst->u.tun_info;
  86. }
  87. static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
  88. __be32 daddr,
  89. __u8 tos, __u8 ttl,
  90. __be16 flags,
  91. __be64 tunnel_id,
  92. int md_size)
  93. {
  94. struct metadata_dst *tun_dst;
  95. tun_dst = tun_rx_dst(md_size);
  96. if (!tun_dst)
  97. return NULL;
  98. ip_tunnel_key_init(&tun_dst->u.tun_info.key,
  99. saddr, daddr, tos, ttl,
  100. 0, 0, 0, tunnel_id, flags);
  101. return tun_dst;
  102. }
  103. static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
  104. __be16 flags,
  105. __be64 tunnel_id,
  106. int md_size)
  107. {
  108. const struct iphdr *iph = ip_hdr(skb);
  109. return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
  110. flags, tunnel_id, md_size);
  111. }
  112. static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
  113. const struct in6_addr *daddr,
  114. __u8 tos, __u8 ttl,
  115. __be32 label,
  116. __be16 flags,
  117. __be64 tunnel_id,
  118. int md_size)
  119. {
  120. struct metadata_dst *tun_dst;
  121. struct ip_tunnel_info *info;
  122. tun_dst = tun_rx_dst(md_size);
  123. if (!tun_dst)
  124. return NULL;
  125. info = &tun_dst->u.tun_info;
  126. info->mode = IP_TUNNEL_INFO_IPV6;
  127. info->key.tun_flags = flags;
  128. info->key.tun_id = tunnel_id;
  129. info->key.tp_src = 0;
  130. info->key.tp_dst = 0;
  131. info->key.u.ipv6.src = *saddr;
  132. info->key.u.ipv6.dst = *daddr;
  133. info->key.tos = tos;
  134. info->key.ttl = ttl;
  135. info->key.label = label;
  136. return tun_dst;
  137. }
  138. static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
  139. __be16 flags,
  140. __be64 tunnel_id,
  141. int md_size)
  142. {
  143. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  144. return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
  145. ipv6_get_dsfield(ip6h), ip6h->hop_limit,
  146. ip6_flowlabel(ip6h), flags, tunnel_id,
  147. md_size);
  148. }
  149. #endif /* __NET_DST_METADATA_H */