dst_metadata.h 6.3 KB

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