1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_DST_METADATA_H
3 #define __NET_DST_METADATA_H 1
5 #include <linux/skbuff.h>
6 #include <net/ip_tunnels.h>
7 #include <net/macsec.h>
18 struct net_device *lower_dev;
33 enum metadata_type type;
35 struct ip_tunnel_info tun_info;
36 struct hw_port_info port_info;
37 struct macsec_info macsec_info;
38 struct xfrm_md_info xfrm_info;
42 static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
44 struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
46 if (md_dst && md_dst->dst.flags & DST_METADATA)
52 static inline struct ip_tunnel_info *
53 skb_tunnel_info(const struct sk_buff *skb)
55 struct metadata_dst *md_dst = skb_metadata_dst(skb);
56 struct dst_entry *dst;
58 if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
59 return &md_dst->u.tun_info;
62 if (dst && dst->lwtstate &&
63 (dst->lwtstate->type == LWTUNNEL_ENCAP_IP ||
64 dst->lwtstate->type == LWTUNNEL_ENCAP_IP6))
65 return lwt_tun_info(dst->lwtstate);
70 static inline struct xfrm_md_info *lwt_xfrm_info(struct lwtunnel_state *lwt)
72 return (struct xfrm_md_info *)lwt->data;
75 static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
77 struct metadata_dst *md_dst = skb_metadata_dst(skb);
78 struct dst_entry *dst;
80 if (md_dst && md_dst->type == METADATA_XFRM)
81 return &md_dst->u.xfrm_info;
84 if (dst && dst->lwtstate &&
85 dst->lwtstate->type == LWTUNNEL_ENCAP_XFRM)
86 return lwt_xfrm_info(dst->lwtstate);
91 static inline bool skb_valid_dst(const struct sk_buff *skb)
93 struct dst_entry *dst = skb_dst(skb);
95 return dst && !(dst->flags & DST_METADATA);
98 static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
99 const struct sk_buff *skb_b)
101 const struct metadata_dst *a, *b;
103 if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
106 a = (const struct metadata_dst *) skb_dst(skb_a);
107 b = (const struct metadata_dst *) skb_dst(skb_b);
109 if (!a != !b || a->type != b->type)
113 case METADATA_HW_PORT_MUX:
114 return memcmp(&a->u.port_info, &b->u.port_info,
115 sizeof(a->u.port_info));
116 case METADATA_IP_TUNNEL:
117 return memcmp(&a->u.tun_info, &b->u.tun_info,
118 sizeof(a->u.tun_info) +
119 a->u.tun_info.options_len);
120 case METADATA_MACSEC:
121 return memcmp(&a->u.macsec_info, &b->u.macsec_info,
122 sizeof(a->u.macsec_info));
124 return memcmp(&a->u.xfrm_info, &b->u.xfrm_info,
125 sizeof(a->u.xfrm_info));
131 void metadata_dst_free(struct metadata_dst *);
132 struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
134 void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst);
135 struct metadata_dst __percpu *
136 metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
138 static inline struct metadata_dst *tun_rx_dst(int md_size)
140 struct metadata_dst *tun_dst;
142 tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
146 tun_dst->u.tun_info.options_len = 0;
147 tun_dst->u.tun_info.mode = 0;
151 static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
153 struct metadata_dst *md_dst = skb_metadata_dst(skb);
155 struct metadata_dst *new_md;
157 if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
158 return ERR_PTR(-EINVAL);
160 md_size = md_dst->u.tun_info.options_len;
161 new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
163 return ERR_PTR(-ENOMEM);
165 memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
166 sizeof(struct ip_tunnel_info) + md_size);
167 #ifdef CONFIG_DST_CACHE
168 /* Unclone the dst cache if there is one */
169 if (new_md->u.tun_info.dst_cache.cache) {
172 ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC);
174 metadata_dst_free(new_md);
181 skb_dst_set(skb, &new_md->dst);
185 static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
187 struct metadata_dst *dst;
189 dst = tun_dst_unclone(skb);
193 return &dst->u.tun_info;
196 static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
204 struct metadata_dst *tun_dst;
206 tun_dst = tun_rx_dst(md_size);
210 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
211 saddr, daddr, tos, ttl,
212 0, 0, tp_dst, tunnel_id, flags);
216 static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
221 const struct iphdr *iph = ip_hdr(skb);
223 return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
224 0, flags, tunnel_id, md_size);
227 static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
228 const struct in6_addr *daddr,
236 struct metadata_dst *tun_dst;
237 struct ip_tunnel_info *info;
239 tun_dst = tun_rx_dst(md_size);
243 info = &tun_dst->u.tun_info;
244 info->mode = IP_TUNNEL_INFO_IPV6;
245 info->key.tun_flags = flags;
246 info->key.tun_id = tunnel_id;
247 info->key.tp_src = 0;
248 info->key.tp_dst = tp_dst;
250 info->key.u.ipv6.src = *saddr;
251 info->key.u.ipv6.dst = *daddr;
255 info->key.label = label;
260 static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
265 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
267 return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
268 ipv6_get_dsfield(ip6h), ip6h->hop_limit,
269 0, ip6_flowlabel(ip6h), flags, tunnel_id,
272 #endif /* __NET_DST_METADATA_H */