dst_metadata.h 5.0 KB

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