blob: 14efa0ded75dd93d6f63735a95feaf72de6fdef7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Thomas Graff38a9eb2015-07-21 10:43:56 +02002#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
Jakub Kicinski3fcece12017-06-23 22:11:58 +02009enum 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
Thomas Graff38a9eb2015-07-21 10:43:56 +020019struct metadata_dst {
20 struct dst_entry dst;
Jakub Kicinski3fcece12017-06-23 22:11:58 +020021 enum metadata_type type;
Thomas Grafee122c72015-07-21 10:43:58 +020022 union {
23 struct ip_tunnel_info tun_info;
Jakub Kicinski3fcece12017-06-23 22:11:58 +020024 struct hw_port_info port_info;
Thomas Grafee122c72015-07-21 10:43:58 +020025 } u;
Thomas Graff38a9eb2015-07-21 10:43:56 +020026};
27
Simon Horman32f16362017-10-02 10:41:15 +020028static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb)
Thomas Graff38a9eb2015-07-21 10:43:56 +020029{
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
Simon Horman32f16362017-10-02 10:41:15 +020038static inline struct ip_tunnel_info *
39skb_tunnel_info(const struct sk_buff *skb)
Thomas Grafee122c72015-07-21 10:43:58 +020040{
41 struct metadata_dst *md_dst = skb_metadata_dst(skb);
Jiri Benc61adedf2015-08-20 13:56:25 +020042 struct dst_entry *dst;
Thomas Grafee122c72015-07-21 10:43:58 +020043
Jakub Kicinski3fcece12017-06-23 22:11:58 +020044 if (md_dst && md_dst->type == METADATA_IP_TUNNEL)
Thomas Grafee122c72015-07-21 10:43:58 +020045 return &md_dst->u.tun_info;
46
Jiri Benc61adedf2015-08-20 13:56:25 +020047 dst = skb_dst(skb);
Taehee Yoo67a9c942021-07-09 17:35:18 +000048 if (dst && dst->lwtstate &&
49 (dst->lwtstate->type == LWTUNNEL_ENCAP_IP ||
50 dst->lwtstate->type == LWTUNNEL_ENCAP_IP6))
Jiri Benc61adedf2015-08-20 13:56:25 +020051 return lwt_tun_info(dst->lwtstate);
Thomas Graf3093fbe2015-07-21 10:44:00 +020052
Thomas Grafee122c72015-07-21 10:43:58 +020053 return NULL;
54}
55
Thomas Graff38a9eb2015-07-21 10:43:56 +020056static inline bool skb_valid_dst(const struct sk_buff *skb)
57{
58 struct dst_entry *dst = skb_dst(skb);
59
60 return dst && !(dst->flags & DST_METADATA);
61}
62
Jesse Grossce87fc62016-01-20 17:59:49 -080063static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
64 const struct sk_buff *skb_b)
65{
66 const struct metadata_dst *a, *b;
67
68 if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
69 return 0;
70
71 a = (const struct metadata_dst *) skb_dst(skb_a);
72 b = (const struct metadata_dst *) skb_dst(skb_b);
73
Jakub Kicinski3fcece12017-06-23 22:11:58 +020074 if (!a != !b || a->type != b->type)
Jesse Grossce87fc62016-01-20 17:59:49 -080075 return 1;
76
Jakub Kicinski3fcece12017-06-23 22:11:58 +020077 switch (a->type) {
78 case METADATA_HW_PORT_MUX:
79 return memcmp(&a->u.port_info, &b->u.port_info,
80 sizeof(a->u.port_info));
81 case METADATA_IP_TUNNEL:
82 return memcmp(&a->u.tun_info, &b->u.tun_info,
83 sizeof(a->u.tun_info) +
84 a->u.tun_info.options_len);
85 default:
86 return 1;
87 }
Jesse Grossce87fc62016-01-20 17:59:49 -080088}
89
Paolo Abenid71785f2016-02-12 15:43:57 +010090void metadata_dst_free(struct metadata_dst *);
Jakub Kicinski3fcece12017-06-23 22:11:58 +020091struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
92 gfp_t flags);
Jakub Kicinskid66f2b92017-10-09 10:30:14 -070093void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst);
Jakub Kicinski3fcece12017-06-23 22:11:58 +020094struct metadata_dst __percpu *
95metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
Thomas Graff38a9eb2015-07-21 10:43:56 +020096
Pravin B Shelar4c222792015-08-30 18:09:38 -070097static inline struct metadata_dst *tun_rx_dst(int md_size)
Pravin B Shelarc29a70d2015-08-26 23:46:50 -070098{
99 struct metadata_dst *tun_dst;
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700100
Jakub Kicinski3fcece12017-06-23 22:11:58 +0200101 tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700102 if (!tun_dst)
103 return NULL;
104
Pravin B Shelar4c222792015-08-30 18:09:38 -0700105 tun_dst->u.tun_info.options_len = 0;
106 tun_dst->u.tun_info.mode = 0;
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700107 return tun_dst;
108}
109
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700110static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
111{
112 struct metadata_dst *md_dst = skb_metadata_dst(skb);
Tobias Klauserf63ce5b2015-11-04 13:49:49 +0100113 int md_size;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700114 struct metadata_dst *new_md;
115
Jakub Kicinski3fcece12017-06-23 22:11:58 +0200116 if (!md_dst || md_dst->type != METADATA_IP_TUNNEL)
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700117 return ERR_PTR(-EINVAL);
118
Tobias Klauserf63ce5b2015-11-04 13:49:49 +0100119 md_size = md_dst->u.tun_info.options_len;
Jakub Kicinski3fcece12017-06-23 22:11:58 +0200120 new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700121 if (!new_md)
122 return ERR_PTR(-ENOMEM);
123
124 memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
125 sizeof(struct ip_tunnel_info) + md_size);
126 skb_dst_drop(skb);
127 dst_hold(&new_md->dst);
128 skb_dst_set(skb, &new_md->dst);
129 return new_md;
130}
131
132static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
133{
134 struct metadata_dst *dst;
135
136 dst = tun_dst_unclone(skb);
137 if (IS_ERR(dst))
138 return NULL;
139
140 return &dst->u.tun_info;
141}
142
Amir Vadai2ff378b2016-09-08 16:23:46 +0300143static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
144 __be32 daddr,
145 __u8 tos, __u8 ttl,
Hadar Hen Zion24ba8982016-11-07 15:14:40 +0200146 __be16 tp_dst,
Amir Vadai2ff378b2016-09-08 16:23:46 +0300147 __be16 flags,
148 __be64 tunnel_id,
149 int md_size)
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700150{
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700151 struct metadata_dst *tun_dst;
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700152
Pravin B Shelar4c222792015-08-30 18:09:38 -0700153 tun_dst = tun_rx_dst(md_size);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700154 if (!tun_dst)
155 return NULL;
156
Pravin B Shelar4c222792015-08-30 18:09:38 -0700157 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
Amir Vadai2ff378b2016-09-08 16:23:46 +0300158 saddr, daddr, tos, ttl,
Hadar Hen Zion24ba8982016-11-07 15:14:40 +0200159 0, 0, tp_dst, tunnel_id, flags);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700160 return tun_dst;
161}
162
Amir Vadai2ff378b2016-09-08 16:23:46 +0300163static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700164 __be16 flags,
165 __be64 tunnel_id,
166 int md_size)
167{
Amir Vadai2ff378b2016-09-08 16:23:46 +0300168 const struct iphdr *iph = ip_hdr(skb);
169
170 return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
Hadar Hen Zion24ba8982016-11-07 15:14:40 +0200171 0, flags, tunnel_id, md_size);
Amir Vadai2ff378b2016-09-08 16:23:46 +0300172}
173
174static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
175 const struct in6_addr *daddr,
176 __u8 tos, __u8 ttl,
Hadar Hen Zion24ba8982016-11-07 15:14:40 +0200177 __be16 tp_dst,
Amir Vadai2ff378b2016-09-08 16:23:46 +0300178 __be32 label,
179 __be16 flags,
180 __be64 tunnel_id,
181 int md_size)
182{
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700183 struct metadata_dst *tun_dst;
184 struct ip_tunnel_info *info;
185
Pravin B Shelar4c222792015-08-30 18:09:38 -0700186 tun_dst = tun_rx_dst(md_size);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700187 if (!tun_dst)
188 return NULL;
189
190 info = &tun_dst->u.tun_info;
Pravin B Shelar4c222792015-08-30 18:09:38 -0700191 info->mode = IP_TUNNEL_INFO_IPV6;
192 info->key.tun_flags = flags;
193 info->key.tun_id = tunnel_id;
194 info->key.tp_src = 0;
Hadar Hen Zion24ba8982016-11-07 15:14:40 +0200195 info->key.tp_dst = tp_dst;
Pravin B Shelar4c222792015-08-30 18:09:38 -0700196
Amir Vadai2ff378b2016-09-08 16:23:46 +0300197 info->key.u.ipv6.src = *saddr;
198 info->key.u.ipv6.dst = *daddr;
Daniel Borkmann13461142016-03-09 03:00:02 +0100199
Amir Vadai2ff378b2016-09-08 16:23:46 +0300200 info->key.tos = tos;
201 info->key.ttl = ttl;
202 info->key.label = label;
Daniel Borkmann13461142016-03-09 03:00:02 +0100203
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700204 return tun_dst;
205}
206
Amir Vadai2ff378b2016-09-08 16:23:46 +0300207static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
208 __be16 flags,
209 __be64 tunnel_id,
210 int md_size)
211{
212 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
213
214 return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
215 ipv6_get_dsfield(ip6h), ip6h->hop_limit,
Hadar Hen Zion24ba8982016-11-07 15:14:40 +0200216 0, ip6_flowlabel(ip6h), flags, tunnel_id,
Amir Vadai2ff378b2016-09-08 16:23:46 +0300217 md_size);
218}
Thomas Graff38a9eb2015-07-21 10:43:56 +0200219#endif /* __NET_DST_METADATA_H */