dst_metadata.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/macsec.h>
  7. #include <net/dst.h>
  8. enum metadata_type {
  9. METADATA_IP_TUNNEL,
  10. METADATA_HW_PORT_MUX,
  11. METADATA_MACSEC,
  12. METADATA_XFRM,
  13. };
  14. struct hw_port_info {
  15. struct net_device *lower_dev;
  16. u32 port_id;
  17. };
  18. struct macsec_info {
  19. sci_t sci;
  20. };
  21. struct xfrm_md_info {
  22. u32 if_id;
  23. int link;
  24. struct dst_entry *dst_orig;
  25. };
  26. struct metadata_dst {
  27. struct dst_entry dst;
  28. enum metadata_type type;
  29. union {
  30. struct ip_tunnel_info tun_info;
  31. struct hw_port_info port_info;
  32. struct macsec_info macsec_info;
  33. struct xfrm_md_info xfrm_info;
  34. } u;
  35. };
  36. static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
  37. {
  38. struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
  39. if (md_dst && md_dst->dst.flags & DST_METADATA)
  40. return md_dst;
  41. return NULL;
  42. }
  43. static inline struct ip_tunnel_info *
  44. skb_tunnel_info(const struct sk_buff *skb)
  45. {
  46. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  47. struct dst_entry *dst;
  48. if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
  49. return &md_dst->u.tun_info;
  50. dst = skb_dst(skb);
  51. if (dst && dst->lwtstate &&
  52. (dst->lwtstate->type == LWTUNNEL_ENCAP_IP ||
  53. dst->lwtstate->type == LWTUNNEL_ENCAP_IP6))
  54. return lwt_tun_info(dst->lwtstate);
  55. return NULL;
  56. }
  57. static inline struct xfrm_md_info *lwt_xfrm_info(struct lwtunnel_state *lwt)
  58. {
  59. return (struct xfrm_md_info *)lwt->data;
  60. }
  61. static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
  62. {
  63. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  64. struct dst_entry *dst;
  65. if (md_dst && md_dst->type == METADATA_XFRM)
  66. return &md_dst->u.xfrm_info;
  67. dst = skb_dst(skb);
  68. if (dst && dst->lwtstate &&
  69. dst->lwtstate->type == LWTUNNEL_ENCAP_XFRM)
  70. return lwt_xfrm_info(dst->lwtstate);
  71. return NULL;
  72. }
  73. static inline bool skb_valid_dst(const struct sk_buff *skb)
  74. {
  75. struct dst_entry *dst = skb_dst(skb);
  76. return dst && !(dst->flags & DST_METADATA);
  77. }
  78. static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
  79. const struct sk_buff *skb_b)
  80. {
  81. const struct metadata_dst *a, *b;
  82. if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
  83. return 0;
  84. a = (const struct metadata_dst *) skb_dst(skb_a);
  85. b = (const struct metadata_dst *) skb_dst(skb_b);
  86. if (!a != !b || a->type != b->type)
  87. return 1;
  88. switch (a->type) {
  89. case METADATA_HW_PORT_MUX:
  90. return memcmp(&a->u.port_info, &b->u.port_info,
  91. sizeof(a->u.port_info));
  92. case METADATA_IP_TUNNEL:
  93. return memcmp(&a->u.tun_info, &b->u.tun_info,
  94. sizeof(a->u.tun_info) +
  95. a->u.tun_info.options_len);
  96. case METADATA_MACSEC:
  97. return memcmp(&a->u.macsec_info, &b->u.macsec_info,
  98. sizeof(a->u.macsec_info));
  99. case METADATA_XFRM:
  100. return memcmp(&a->u.xfrm_info, &b->u.xfrm_info,
  101. sizeof(a->u.xfrm_info));
  102. default:
  103. return 1;
  104. }
  105. }
  106. void metadata_dst_free(struct metadata_dst *);
  107. struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
  108. gfp_t flags);
  109. void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst);
  110. struct metadata_dst __percpu *
  111. metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
  112. static inline struct metadata_dst *tun_rx_dst(int md_size)
  113. {
  114. struct metadata_dst *tun_dst;
  115. tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
  116. if (!tun_dst)
  117. return NULL;
  118. tun_dst->u.tun_info.options_len = 0;
  119. tun_dst->u.tun_info.mode = 0;
  120. return tun_dst;
  121. }
  122. static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
  123. {
  124. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  125. int md_size;
  126. struct metadata_dst *new_md;
  127. if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
  128. return ERR_PTR(-EINVAL);
  129. md_size = md_dst->u.tun_info.options_len;
  130. new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
  131. if (!new_md)
  132. return ERR_PTR(-ENOMEM);
  133. unsafe_memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
  134. sizeof(struct ip_tunnel_info) + md_size,
  135. /* metadata_dst_alloc() reserves room (md_size bytes) for
  136. * options right after the ip_tunnel_info struct.
  137. */);
  138. #ifdef CONFIG_DST_CACHE
  139. /* Unclone the dst cache if there is one */
  140. if (new_md->u.tun_info.dst_cache.cache) {
  141. int ret;
  142. ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC);
  143. if (ret) {
  144. metadata_dst_free(new_md);
  145. return ERR_PTR(ret);
  146. }
  147. }
  148. #endif
  149. skb_dst_drop(skb);
  150. skb_dst_set(skb, &new_md->dst);
  151. return new_md;
  152. }
  153. static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
  154. {
  155. struct metadata_dst *dst;
  156. dst = tun_dst_unclone(skb);
  157. if (IS_ERR(dst))
  158. return NULL;
  159. return &dst->u.tun_info;
  160. }
  161. static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
  162. __be32 daddr,
  163. __u8 tos, __u8 ttl,
  164. __be16 tp_dst,
  165. const unsigned long *flags,
  166. __be64 tunnel_id,
  167. int md_size)
  168. {
  169. struct metadata_dst *tun_dst;
  170. tun_dst = tun_rx_dst(md_size);
  171. if (!tun_dst)
  172. return NULL;
  173. ip_tunnel_key_init(&tun_dst->u.tun_info.key,
  174. saddr, daddr, tos, ttl,
  175. 0, 0, tp_dst, tunnel_id, flags);
  176. return tun_dst;
  177. }
  178. static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
  179. const unsigned long *flags,
  180. __be64 tunnel_id,
  181. int md_size)
  182. {
  183. const struct iphdr *iph = ip_hdr(skb);
  184. return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
  185. 0, flags, tunnel_id, md_size);
  186. }
  187. static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
  188. const struct in6_addr *daddr,
  189. __u8 tos, __u8 ttl,
  190. __be16 tp_dst,
  191. __be32 label,
  192. const unsigned long *flags,
  193. __be64 tunnel_id,
  194. int md_size)
  195. {
  196. struct metadata_dst *tun_dst;
  197. struct ip_tunnel_info *info;
  198. tun_dst = tun_rx_dst(md_size);
  199. if (!tun_dst)
  200. return NULL;
  201. info = &tun_dst->u.tun_info;
  202. info->mode = IP_TUNNEL_INFO_IPV6;
  203. ip_tunnel_flags_copy(info->key.tun_flags, flags);
  204. info->key.tun_id = tunnel_id;
  205. info->key.tp_src = 0;
  206. info->key.tp_dst = tp_dst;
  207. info->key.u.ipv6.src = *saddr;
  208. info->key.u.ipv6.dst = *daddr;
  209. info->key.tos = tos;
  210. info->key.ttl = ttl;
  211. info->key.label = label;
  212. return tun_dst;
  213. }
  214. static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
  215. const unsigned long *flags,
  216. __be64 tunnel_id,
  217. int md_size)
  218. {
  219. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  220. return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
  221. ipv6_get_dsfield(ip6h), ip6h->hop_limit,
  222. 0, ip6_flowlabel(ip6h), flags, tunnel_id,
  223. md_size);
  224. }
  225. #endif /* __NET_DST_METADATA_H */