dst_metadata.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_DST_METADATA_H
  3. #define __NET_DST_METADATA_H 1
  4. #include <linux/skbuff.h>
  5. #include <net/ip_tunnels.h>
  6. #include <net/dst.h>
  7. enum metadata_type {
  8. METADATA_IP_TUNNEL,
  9. METADATA_HW_PORT_MUX,
  10. };
  11. struct hw_port_info {
  12. struct net_device *lower_dev;
  13. u32 port_id;
  14. };
  15. struct metadata_dst {
  16. struct dst_entry dst;
  17. enum metadata_type type;
  18. union {
  19. struct ip_tunnel_info tun_info;
  20. struct hw_port_info port_info;
  21. } u;
  22. };
  23. static inline struct metadata_dst *skb_metadata_dst(struct sk_buff *skb)
  24. {
  25. struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
  26. if (md_dst && md_dst->dst.flags & DST_METADATA)
  27. return md_dst;
  28. return NULL;
  29. }
  30. static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
  31. {
  32. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  33. struct dst_entry *dst;
  34. if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
  35. return &md_dst->u.tun_info;
  36. dst = skb_dst(skb);
  37. if (dst && dst->lwtstate)
  38. return lwt_tun_info(dst->lwtstate);
  39. return NULL;
  40. }
  41. static inline bool skb_valid_dst(const struct sk_buff *skb)
  42. {
  43. struct dst_entry *dst = skb_dst(skb);
  44. return dst && !(dst->flags & DST_METADATA);
  45. }
  46. static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
  47. const struct sk_buff *skb_b)
  48. {
  49. const struct metadata_dst *a, *b;
  50. if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
  51. return 0;
  52. a = (const struct metadata_dst *) skb_dst(skb_a);
  53. b = (const struct metadata_dst *) skb_dst(skb_b);
  54. if (!a != !b || a->type != b->type)
  55. return 1;
  56. switch (a->type) {
  57. case METADATA_HW_PORT_MUX:
  58. return memcmp(&a->u.port_info, &b->u.port_info,
  59. sizeof(a->u.port_info));
  60. case METADATA_IP_TUNNEL:
  61. return memcmp(&a->u.tun_info, &b->u.tun_info,
  62. sizeof(a->u.tun_info) +
  63. a->u.tun_info.options_len);
  64. default:
  65. return 1;
  66. }
  67. }
  68. void metadata_dst_free(struct metadata_dst *);
  69. struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
  70. gfp_t flags);
  71. struct metadata_dst __percpu *
  72. metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
  73. static inline struct metadata_dst *tun_rx_dst(int md_size)
  74. {
  75. struct metadata_dst *tun_dst;
  76. tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
  77. if (!tun_dst)
  78. return NULL;
  79. tun_dst->u.tun_info.options_len = 0;
  80. tun_dst->u.tun_info.mode = 0;
  81. return tun_dst;
  82. }
  83. static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
  84. {
  85. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  86. int md_size;
  87. struct metadata_dst *new_md;
  88. if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
  89. return ERR_PTR(-EINVAL);
  90. md_size = md_dst->u.tun_info.options_len;
  91. new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
  92. if (!new_md)
  93. return ERR_PTR(-ENOMEM);
  94. memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
  95. sizeof(struct ip_tunnel_info) + md_size);
  96. skb_dst_drop(skb);
  97. dst_hold(&new_md->dst);
  98. skb_dst_set(skb, &new_md->dst);
  99. return new_md;
  100. }
  101. static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
  102. {
  103. struct metadata_dst *dst;
  104. dst = tun_dst_unclone(skb);
  105. if (IS_ERR(dst))
  106. return NULL;
  107. return &dst->u.tun_info;
  108. }
  109. static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
  110. __be32 daddr,
  111. __u8 tos, __u8 ttl,
  112. __be16 tp_dst,
  113. __be16 flags,
  114. __be64 tunnel_id,
  115. int md_size)
  116. {
  117. struct metadata_dst *tun_dst;
  118. tun_dst = tun_rx_dst(md_size);
  119. if (!tun_dst)
  120. return NULL;
  121. ip_tunnel_key_init(&tun_dst->u.tun_info.key,
  122. saddr, daddr, tos, ttl,
  123. 0, 0, tp_dst, tunnel_id, flags);
  124. return tun_dst;
  125. }
  126. static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
  127. __be16 flags,
  128. __be64 tunnel_id,
  129. int md_size)
  130. {
  131. const struct iphdr *iph = ip_hdr(skb);
  132. return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
  133. 0, flags, tunnel_id, md_size);
  134. }
  135. static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
  136. const struct in6_addr *daddr,
  137. __u8 tos, __u8 ttl,
  138. __be16 tp_dst,
  139. __be32 label,
  140. __be16 flags,
  141. __be64 tunnel_id,
  142. int md_size)
  143. {
  144. struct metadata_dst *tun_dst;
  145. struct ip_tunnel_info *info;
  146. tun_dst = tun_rx_dst(md_size);
  147. if (!tun_dst)
  148. return NULL;
  149. info = &tun_dst->u.tun_info;
  150. info->mode = IP_TUNNEL_INFO_IPV6;
  151. info->key.tun_flags = flags;
  152. info->key.tun_id = tunnel_id;
  153. info->key.tp_src = 0;
  154. info->key.tp_dst = tp_dst;
  155. info->key.u.ipv6.src = *saddr;
  156. info->key.u.ipv6.dst = *daddr;
  157. info->key.tos = tos;
  158. info->key.ttl = ttl;
  159. info->key.label = label;
  160. return tun_dst;
  161. }
  162. static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
  163. __be16 flags,
  164. __be64 tunnel_id,
  165. int md_size)
  166. {
  167. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  168. return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
  169. ipv6_get_dsfield(ip6h), ip6h->hop_limit,
  170. 0, ip6_flowlabel(ip6h), flags, tunnel_id,
  171. md_size);
  172. }
  173. #endif /* __NET_DST_METADATA_H */