Linux 6.12-rc1
[linux-2.6-block.git] / include / net / dst_metadata.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
f38a9eb1
TG
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>
0a28bfd4 7#include <net/macsec.h>
f38a9eb1
TG
8#include <net/dst.h>
9
3fcece12
JK
10enum metadata_type {
11 METADATA_IP_TUNNEL,
12 METADATA_HW_PORT_MUX,
0a28bfd4 13 METADATA_MACSEC,
5182a5d4 14 METADATA_XFRM,
3fcece12
JK
15};
16
17struct hw_port_info {
18 struct net_device *lower_dev;
19 u32 port_id;
20};
21
0a28bfd4
LN
22struct macsec_info {
23 sci_t sci;
24};
25
5182a5d4
EB
26struct xfrm_md_info {
27 u32 if_id;
28 int link;
94151f5a 29 struct dst_entry *dst_orig;
5182a5d4
EB
30};
31
f38a9eb1
TG
32struct metadata_dst {
33 struct dst_entry dst;
3fcece12 34 enum metadata_type type;
ee122c79
TG
35 union {
36 struct ip_tunnel_info tun_info;
3fcece12 37 struct hw_port_info port_info;
0a28bfd4 38 struct macsec_info macsec_info;
5182a5d4 39 struct xfrm_md_info xfrm_info;
ee122c79 40 } u;
f38a9eb1
TG
41};
42
32f16369 43static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
f38a9eb1
TG
44{
45 struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
46
47 if (md_dst && md_dst->dst.flags & DST_METADATA)
48 return md_dst;
49
50 return NULL;
51}
52
32f16369
SH
53static inline struct ip_tunnel_info *
54skb_tunnel_info(const struct sk_buff *skb)
ee122c79
TG
55{
56 struct metadata_dst *md_dst = skb_metadata_dst(skb);
61adedf3 57 struct dst_entry *dst;
ee122c79 58
3fcece12 59 if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
ee122c79
TG
60 return &md_dst->u.tun_info;
61
61adedf3 62 dst = skb_dst(skb);
67a9c943
TY
63 if (dst && dst->lwtstate &&
64 (dst->lwtstate->type == LWTUNNEL_ENCAP_IP ||
65 dst->lwtstate->type == LWTUNNEL_ENCAP_IP6))
61adedf3 66 return lwt_tun_info(dst->lwtstate);
3093fbe7 67
ee122c79
TG
68 return NULL;
69}
70
2c2493b9
EB
71static inline struct xfrm_md_info *lwt_xfrm_info(struct lwtunnel_state *lwt)
72{
73 return (struct xfrm_md_info *)lwt->data;
74}
75
5182a5d4
EB
76static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
77{
78 struct metadata_dst *md_dst = skb_metadata_dst(skb);
2c2493b9 79 struct dst_entry *dst;
5182a5d4
EB
80
81 if (md_dst && md_dst->type == METADATA_XFRM)
82 return &md_dst->u.xfrm_info;
83
2c2493b9
EB
84 dst = skb_dst(skb);
85 if (dst && dst->lwtstate &&
86 dst->lwtstate->type == LWTUNNEL_ENCAP_XFRM)
87 return lwt_xfrm_info(dst->lwtstate);
88
5182a5d4
EB
89 return NULL;
90}
91
f38a9eb1
TG
92static inline bool skb_valid_dst(const struct sk_buff *skb)
93{
94 struct dst_entry *dst = skb_dst(skb);
95
96 return dst && !(dst->flags & DST_METADATA);
97}
98
ce87fc6c
JG
99static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
100 const struct sk_buff *skb_b)
101{
102 const struct metadata_dst *a, *b;
103
104 if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
105 return 0;
106
107 a = (const struct metadata_dst *) skb_dst(skb_a);
108 b = (const struct metadata_dst *) skb_dst(skb_b);
109
3fcece12 110 if (!a != !b || a->type != b->type)
ce87fc6c
JG
111 return 1;
112
3fcece12
JK
113 switch (a->type) {
114 case METADATA_HW_PORT_MUX:
115 return memcmp(&a->u.port_info, &b->u.port_info,
116 sizeof(a->u.port_info));
117 case METADATA_IP_TUNNEL:
118 return memcmp(&a->u.tun_info, &b->u.tun_info,
119 sizeof(a->u.tun_info) +
120 a->u.tun_info.options_len);
0a28bfd4
LN
121 case METADATA_MACSEC:
122 return memcmp(&a->u.macsec_info, &b->u.macsec_info,
123 sizeof(a->u.macsec_info));
5182a5d4
EB
124 case METADATA_XFRM:
125 return memcmp(&a->u.xfrm_info, &b->u.xfrm_info,
126 sizeof(a->u.xfrm_info));
3fcece12
JK
127 default:
128 return 1;
129 }
ce87fc6c
JG
130}
131
d71785ff 132void metadata_dst_free(struct metadata_dst *);
3fcece12
JK
133struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
134 gfp_t flags);
d66f2b91 135void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst);
3fcece12
JK
136struct metadata_dst __percpu *
137metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
f38a9eb1 138
4c222798 139static inline struct metadata_dst *tun_rx_dst(int md_size)
c29a70d2
PS
140{
141 struct metadata_dst *tun_dst;
c29a70d2 142
3fcece12 143 tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
c29a70d2
PS
144 if (!tun_dst)
145 return NULL;
146
4c222798
PS
147 tun_dst->u.tun_info.options_len = 0;
148 tun_dst->u.tun_info.mode = 0;
c29a70d2
PS
149 return tun_dst;
150}
151
fc4099f1
PS
152static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
153{
154 struct metadata_dst *md_dst = skb_metadata_dst(skb);
f63ce5b6 155 int md_size;
fc4099f1
PS
156 struct metadata_dst *new_md;
157
3fcece12 158 if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
fc4099f1
PS
159 return ERR_PTR(-EINVAL);
160
f63ce5b6 161 md_size = md_dst->u.tun_info.options_len;
3fcece12 162 new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
fc4099f1
PS
163 if (!new_md)
164 return ERR_PTR(-ENOMEM);
165
13cfd6a6
GP
166 unsafe_memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
167 sizeof(struct ip_tunnel_info) + md_size,
168 /* metadata_dst_alloc() reserves room (md_size bytes) for
169 * options right after the ip_tunnel_info struct.
170 */);
cfc56f85
AT
171#ifdef CONFIG_DST_CACHE
172 /* Unclone the dst cache if there is one */
173 if (new_md->u.tun_info.dst_cache.cache) {
174 int ret;
175
176 ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC);
177 if (ret) {
178 metadata_dst_free(new_md);
179 return ERR_PTR(ret);
180 }
181 }
182#endif
183
fc4099f1 184 skb_dst_drop(skb);
fc4099f1
PS
185 skb_dst_set(skb, &new_md->dst);
186 return new_md;
187}
188
189static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
190{
191 struct metadata_dst *dst;
192
193 dst = tun_dst_unclone(skb);
194 if (IS_ERR(dst))
195 return NULL;
196
197 return &dst->u.tun_info;
198}
199
2ff378b7
AV
200static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
201 __be32 daddr,
202 __u8 tos, __u8 ttl,
24ba898d 203 __be16 tp_dst,
5832c4a7 204 const unsigned long *flags,
2ff378b7
AV
205 __be64 tunnel_id,
206 int md_size)
c29a70d2 207{
c29a70d2 208 struct metadata_dst *tun_dst;
c29a70d2 209
4c222798 210 tun_dst = tun_rx_dst(md_size);
c29a70d2
PS
211 if (!tun_dst)
212 return NULL;
213
4c222798 214 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
2ff378b7 215 saddr, daddr, tos, ttl,
24ba898d 216 0, 0, tp_dst, tunnel_id, flags);
c29a70d2
PS
217 return tun_dst;
218}
219
2ff378b7 220static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
5832c4a7 221 const unsigned long *flags,
c29a70d2
PS
222 __be64 tunnel_id,
223 int md_size)
224{
2ff378b7
AV
225 const struct iphdr *iph = ip_hdr(skb);
226
227 return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
24ba898d 228 0, flags, tunnel_id, md_size);
2ff378b7
AV
229}
230
231static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
232 const struct in6_addr *daddr,
233 __u8 tos, __u8 ttl,
24ba898d 234 __be16 tp_dst,
2ff378b7 235 __be32 label,
5832c4a7 236 const unsigned long *flags,
2ff378b7
AV
237 __be64 tunnel_id,
238 int md_size)
239{
c29a70d2
PS
240 struct metadata_dst *tun_dst;
241 struct ip_tunnel_info *info;
242
4c222798 243 tun_dst = tun_rx_dst(md_size);
c29a70d2
PS
244 if (!tun_dst)
245 return NULL;
246
247 info = &tun_dst->u.tun_info;
4c222798 248 info->mode = IP_TUNNEL_INFO_IPV6;
5832c4a7 249 ip_tunnel_flags_copy(info->key.tun_flags, flags);
4c222798
PS
250 info->key.tun_id = tunnel_id;
251 info->key.tp_src = 0;
24ba898d 252 info->key.tp_dst = tp_dst;
4c222798 253
2ff378b7
AV
254 info->key.u.ipv6.src = *saddr;
255 info->key.u.ipv6.dst = *daddr;
13461144 256
2ff378b7
AV
257 info->key.tos = tos;
258 info->key.ttl = ttl;
259 info->key.label = label;
13461144 260
c29a70d2
PS
261 return tun_dst;
262}
263
2ff378b7 264static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
5832c4a7 265 const unsigned long *flags,
2ff378b7
AV
266 __be64 tunnel_id,
267 int md_size)
268{
269 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
270
271 return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
272 ipv6_get_dsfield(ip6h), ip6h->hop_limit,
24ba898d 273 0, ip6_flowlabel(ip6h), flags, tunnel_id,
2ff378b7
AV
274 md_size);
275}
f38a9eb1 276#endif /* __NET_DST_METADATA_H */