xref: /openbmc/linux/include/net/dst_metadata.h (revision a2cab953)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_DST_METADATA_H
3 #define __NET_DST_METADATA_H 1
4 
5 #include <linux/skbuff.h>
6 #include <net/ip_tunnels.h>
7 #include <net/macsec.h>
8 #include <net/dst.h>
9 
10 enum metadata_type {
11 	METADATA_IP_TUNNEL,
12 	METADATA_HW_PORT_MUX,
13 	METADATA_MACSEC,
14 	METADATA_XFRM,
15 };
16 
17 struct hw_port_info {
18 	struct net_device *lower_dev;
19 	u32 port_id;
20 };
21 
22 struct macsec_info {
23 	sci_t sci;
24 };
25 
26 struct xfrm_md_info {
27 	u32 if_id;
28 	int link;
29 };
30 
31 struct metadata_dst {
32 	struct dst_entry		dst;
33 	enum metadata_type		type;
34 	union {
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;
39 	} u;
40 };
41 
42 static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
43 {
44 	struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
45 
46 	if (md_dst && md_dst->dst.flags & DST_METADATA)
47 		return md_dst;
48 
49 	return NULL;
50 }
51 
52 static inline struct ip_tunnel_info *
53 skb_tunnel_info(const struct sk_buff *skb)
54 {
55 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
56 	struct dst_entry *dst;
57 
58 	if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
59 		return &md_dst->u.tun_info;
60 
61 	dst = skb_dst(skb);
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);
66 
67 	return NULL;
68 }
69 
70 static inline struct xfrm_md_info *lwt_xfrm_info(struct lwtunnel_state *lwt)
71 {
72 	return (struct xfrm_md_info *)lwt->data;
73 }
74 
75 static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
76 {
77 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
78 	struct dst_entry *dst;
79 
80 	if (md_dst && md_dst->type == METADATA_XFRM)
81 		return &md_dst->u.xfrm_info;
82 
83 	dst = skb_dst(skb);
84 	if (dst && dst->lwtstate &&
85 	    dst->lwtstate->type == LWTUNNEL_ENCAP_XFRM)
86 		return lwt_xfrm_info(dst->lwtstate);
87 
88 	return NULL;
89 }
90 
91 static inline bool skb_valid_dst(const struct sk_buff *skb)
92 {
93 	struct dst_entry *dst = skb_dst(skb);
94 
95 	return dst && !(dst->flags & DST_METADATA);
96 }
97 
98 static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
99 				       const struct sk_buff *skb_b)
100 {
101 	const struct metadata_dst *a, *b;
102 
103 	if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
104 		return 0;
105 
106 	a = (const struct metadata_dst *) skb_dst(skb_a);
107 	b = (const struct metadata_dst *) skb_dst(skb_b);
108 
109 	if (!a != !b || a->type != b->type)
110 		return 1;
111 
112 	switch (a->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));
123 	case METADATA_XFRM:
124 		return memcmp(&a->u.xfrm_info, &b->u.xfrm_info,
125 			      sizeof(a->u.xfrm_info));
126 	default:
127 		return 1;
128 	}
129 }
130 
131 void metadata_dst_free(struct metadata_dst *);
132 struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
133 					gfp_t flags);
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);
137 
138 static inline struct metadata_dst *tun_rx_dst(int md_size)
139 {
140 	struct metadata_dst *tun_dst;
141 
142 	tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
143 	if (!tun_dst)
144 		return NULL;
145 
146 	tun_dst->u.tun_info.options_len = 0;
147 	tun_dst->u.tun_info.mode = 0;
148 	return tun_dst;
149 }
150 
151 static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
152 {
153 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
154 	int md_size;
155 	struct metadata_dst *new_md;
156 
157 	if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
158 		return ERR_PTR(-EINVAL);
159 
160 	md_size = md_dst->u.tun_info.options_len;
161 	new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
162 	if (!new_md)
163 		return ERR_PTR(-ENOMEM);
164 
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) {
170 		int ret;
171 
172 		ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC);
173 		if (ret) {
174 			metadata_dst_free(new_md);
175 			return ERR_PTR(ret);
176 		}
177 	}
178 #endif
179 
180 	skb_dst_drop(skb);
181 	skb_dst_set(skb, &new_md->dst);
182 	return new_md;
183 }
184 
185 static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
186 {
187 	struct metadata_dst *dst;
188 
189 	dst = tun_dst_unclone(skb);
190 	if (IS_ERR(dst))
191 		return NULL;
192 
193 	return &dst->u.tun_info;
194 }
195 
196 static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
197 						    __be32 daddr,
198 						    __u8 tos, __u8 ttl,
199 						    __be16 tp_dst,
200 						    __be16 flags,
201 						    __be64 tunnel_id,
202 						    int md_size)
203 {
204 	struct metadata_dst *tun_dst;
205 
206 	tun_dst = tun_rx_dst(md_size);
207 	if (!tun_dst)
208 		return NULL;
209 
210 	ip_tunnel_key_init(&tun_dst->u.tun_info.key,
211 			   saddr, daddr, tos, ttl,
212 			   0, 0, tp_dst, tunnel_id, flags);
213 	return tun_dst;
214 }
215 
216 static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
217 						 __be16 flags,
218 						 __be64 tunnel_id,
219 						 int md_size)
220 {
221 	const struct iphdr *iph = ip_hdr(skb);
222 
223 	return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
224 				0, flags, tunnel_id, md_size);
225 }
226 
227 static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
228 						      const struct in6_addr *daddr,
229 						      __u8 tos, __u8 ttl,
230 						      __be16 tp_dst,
231 						      __be32 label,
232 						      __be16 flags,
233 						      __be64 tunnel_id,
234 						      int md_size)
235 {
236 	struct metadata_dst *tun_dst;
237 	struct ip_tunnel_info *info;
238 
239 	tun_dst = tun_rx_dst(md_size);
240 	if (!tun_dst)
241 		return NULL;
242 
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;
249 
250 	info->key.u.ipv6.src = *saddr;
251 	info->key.u.ipv6.dst = *daddr;
252 
253 	info->key.tos = tos;
254 	info->key.ttl = ttl;
255 	info->key.label = label;
256 
257 	return tun_dst;
258 }
259 
260 static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
261 						   __be16 flags,
262 						   __be64 tunnel_id,
263 						   int md_size)
264 {
265 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
266 
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,
270 				  md_size);
271 }
272 #endif /* __NET_DST_METADATA_H */
273