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