]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - include/net/dst_metadata.h
Merge remote-tracking branches 'asoc/topic/ac97', 'asoc/topic/ac97-mfd', 'asoc/topic...
[mirror_ubuntu-focal-kernel.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>
7#include <net/dst.h>
8
3fcece12
JK
9enum metadata_type {
10 METADATA_IP_TUNNEL,
11 METADATA_HW_PORT_MUX,
12};
13
14struct hw_port_info {
15 struct net_device *lower_dev;
16 u32 port_id;
17};
18
f38a9eb1
TG
19struct metadata_dst {
20 struct dst_entry dst;
3fcece12 21 enum metadata_type type;
ee122c79
TG
22 union {
23 struct ip_tunnel_info tun_info;
3fcece12 24 struct hw_port_info port_info;
ee122c79 25 } u;
f38a9eb1
TG
26};
27
28static inline struct metadata_dst *skb_metadata_dst(struct sk_buff *skb)
29{
30 struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
31
32 if (md_dst && md_dst->dst.flags & DST_METADATA)
33 return md_dst;
34
35 return NULL;
36}
37
61adedf3 38static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
ee122c79
TG
39{
40 struct metadata_dst *md_dst = skb_metadata_dst(skb);
61adedf3 41 struct dst_entry *dst;
ee122c79 42
3fcece12 43 if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
ee122c79
TG
44 return &md_dst->u.tun_info;
45
61adedf3
JB
46 dst = skb_dst(skb);
47 if (dst && dst->lwtstate)
48 return lwt_tun_info(dst->lwtstate);
3093fbe7 49
ee122c79
TG
50 return NULL;
51}
52
f38a9eb1
TG
53static inline bool skb_valid_dst(const struct sk_buff *skb)
54{
55 struct dst_entry *dst = skb_dst(skb);
56
57 return dst && !(dst->flags & DST_METADATA);
58}
59
ce87fc6c
JG
60static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
61 const struct sk_buff *skb_b)
62{
63 const struct metadata_dst *a, *b;
64
65 if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
66 return 0;
67
68 a = (const struct metadata_dst *) skb_dst(skb_a);
69 b = (const struct metadata_dst *) skb_dst(skb_b);
70
3fcece12 71 if (!a != !b || a->type != b->type)
ce87fc6c
JG
72 return 1;
73
3fcece12
JK
74 switch (a->type) {
75 case METADATA_HW_PORT_MUX:
76 return memcmp(&a->u.port_info, &b->u.port_info,
77 sizeof(a->u.port_info));
78 case METADATA_IP_TUNNEL:
79 return memcmp(&a->u.tun_info, &b->u.tun_info,
80 sizeof(a->u.tun_info) +
81 a->u.tun_info.options_len);
82 default:
83 return 1;
84 }
ce87fc6c
JG
85}
86
d71785ff 87void metadata_dst_free(struct metadata_dst *);
3fcece12
JK
88struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
89 gfp_t flags);
90struct metadata_dst __percpu *
91metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
f38a9eb1 92
4c222798 93static inline struct metadata_dst *tun_rx_dst(int md_size)
c29a70d2
PS
94{
95 struct metadata_dst *tun_dst;
c29a70d2 96
3fcece12 97 tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
c29a70d2
PS
98 if (!tun_dst)
99 return NULL;
100
4c222798
PS
101 tun_dst->u.tun_info.options_len = 0;
102 tun_dst->u.tun_info.mode = 0;
c29a70d2
PS
103 return tun_dst;
104}
105
fc4099f1
PS
106static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
107{
108 struct metadata_dst *md_dst = skb_metadata_dst(skb);
f63ce5b6 109 int md_size;
fc4099f1
PS
110 struct metadata_dst *new_md;
111
3fcece12 112 if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
fc4099f1
PS
113 return ERR_PTR(-EINVAL);
114
f63ce5b6 115 md_size = md_dst->u.tun_info.options_len;
3fcece12 116 new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
fc4099f1
PS
117 if (!new_md)
118 return ERR_PTR(-ENOMEM);
119
120 memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
121 sizeof(struct ip_tunnel_info) + md_size);
122 skb_dst_drop(skb);
123 dst_hold(&new_md->dst);
124 skb_dst_set(skb, &new_md->dst);
125 return new_md;
126}
127
128static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
129{
130 struct metadata_dst *dst;
131
132 dst = tun_dst_unclone(skb);
133 if (IS_ERR(dst))
134 return NULL;
135
136 return &dst->u.tun_info;
137}
138
2ff378b7
AV
139static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
140 __be32 daddr,
141 __u8 tos, __u8 ttl,
24ba898d 142 __be16 tp_dst,
2ff378b7
AV
143 __be16 flags,
144 __be64 tunnel_id,
145 int md_size)
c29a70d2 146{
c29a70d2 147 struct metadata_dst *tun_dst;
c29a70d2 148
4c222798 149 tun_dst = tun_rx_dst(md_size);
c29a70d2
PS
150 if (!tun_dst)
151 return NULL;
152
4c222798 153 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
2ff378b7 154 saddr, daddr, tos, ttl,
24ba898d 155 0, 0, tp_dst, tunnel_id, flags);
c29a70d2
PS
156 return tun_dst;
157}
158
2ff378b7 159static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
c29a70d2
PS
160 __be16 flags,
161 __be64 tunnel_id,
162 int md_size)
163{
2ff378b7
AV
164 const struct iphdr *iph = ip_hdr(skb);
165
166 return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
24ba898d 167 0, flags, tunnel_id, md_size);
2ff378b7
AV
168}
169
170static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
171 const struct in6_addr *daddr,
172 __u8 tos, __u8 ttl,
24ba898d 173 __be16 tp_dst,
2ff378b7
AV
174 __be32 label,
175 __be16 flags,
176 __be64 tunnel_id,
177 int md_size)
178{
c29a70d2
PS
179 struct metadata_dst *tun_dst;
180 struct ip_tunnel_info *info;
181
4c222798 182 tun_dst = tun_rx_dst(md_size);
c29a70d2
PS
183 if (!tun_dst)
184 return NULL;
185
186 info = &tun_dst->u.tun_info;
4c222798
PS
187 info->mode = IP_TUNNEL_INFO_IPV6;
188 info->key.tun_flags = flags;
189 info->key.tun_id = tunnel_id;
190 info->key.tp_src = 0;
24ba898d 191 info->key.tp_dst = tp_dst;
4c222798 192
2ff378b7
AV
193 info->key.u.ipv6.src = *saddr;
194 info->key.u.ipv6.dst = *daddr;
13461144 195
2ff378b7
AV
196 info->key.tos = tos;
197 info->key.ttl = ttl;
198 info->key.label = label;
13461144 199
c29a70d2
PS
200 return tun_dst;
201}
202
2ff378b7
AV
203static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
204 __be16 flags,
205 __be64 tunnel_id,
206 int md_size)
207{
208 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
209
210 return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
211 ipv6_get_dsfield(ip6h), ip6h->hop_limit,
24ba898d 212 0, ip6_flowlabel(ip6h), flags, tunnel_id,
2ff378b7
AV
213 md_size);
214}
f38a9eb1 215#endif /* __NET_DST_METADATA_H */