Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 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 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 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 | |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 19 | struct metadata_dst { |
| 20 | struct dst_entry dst; |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 21 | enum metadata_type type; |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 22 | union { |
| 23 | struct ip_tunnel_info tun_info; |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 24 | struct hw_port_info port_info; |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 25 | } u; |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Simon Horman | 32f1636 | 2017-10-02 10:41:15 +0200 | [diff] [blame] | 28 | static inline struct metadata_dst *skb_metadata_dst(const struct sk_buff *skb) |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 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 | |
Simon Horman | 32f1636 | 2017-10-02 10:41:15 +0200 | [diff] [blame] | 38 | static inline struct ip_tunnel_info * |
| 39 | skb_tunnel_info(const struct sk_buff *skb) |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 40 | { |
| 41 | struct metadata_dst *md_dst = skb_metadata_dst(skb); |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 42 | struct dst_entry *dst; |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 43 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 44 | if (md_dst && md_dst->type == METADATA_IP_TUNNEL) |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 45 | return &md_dst->u.tun_info; |
| 46 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 47 | dst = skb_dst(skb); |
Taehee Yoo | 67a9c94 | 2021-07-09 17:35:18 +0000 | [diff] [blame] | 48 | if (dst && dst->lwtstate && |
| 49 | (dst->lwtstate->type == LWTUNNEL_ENCAP_IP || |
| 50 | dst->lwtstate->type == LWTUNNEL_ENCAP_IP6)) |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 51 | return lwt_tun_info(dst->lwtstate); |
Thomas Graf | 3093fbe | 2015-07-21 10:44:00 +0200 | [diff] [blame] | 52 | |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 53 | return NULL; |
| 54 | } |
| 55 | |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 56 | static 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 Gross | ce87fc6 | 2016-01-20 17:59:49 -0800 | [diff] [blame] | 63 | static 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 Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 74 | if (!a != !b || a->type != b->type) |
Jesse Gross | ce87fc6 | 2016-01-20 17:59:49 -0800 | [diff] [blame] | 75 | return 1; |
| 76 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 77 | 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 Gross | ce87fc6 | 2016-01-20 17:59:49 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Paolo Abeni | d71785f | 2016-02-12 15:43:57 +0100 | [diff] [blame] | 90 | void metadata_dst_free(struct metadata_dst *); |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 91 | struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type, |
| 92 | gfp_t flags); |
Jakub Kicinski | d66f2b9 | 2017-10-09 10:30:14 -0700 | [diff] [blame] | 93 | void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst); |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 94 | struct metadata_dst __percpu * |
| 95 | metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags); |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 96 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 97 | static inline struct metadata_dst *tun_rx_dst(int md_size) |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 98 | { |
| 99 | struct metadata_dst *tun_dst; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 100 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 101 | tun_dst = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 102 | if (!tun_dst) |
| 103 | return NULL; |
| 104 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 105 | tun_dst->u.tun_info.options_len = 0; |
| 106 | tun_dst->u.tun_info.mode = 0; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 107 | return tun_dst; |
| 108 | } |
| 109 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 110 | static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb) |
| 111 | { |
| 112 | struct metadata_dst *md_dst = skb_metadata_dst(skb); |
Tobias Klauser | f63ce5b | 2015-11-04 13:49:49 +0100 | [diff] [blame] | 113 | int md_size; |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 114 | struct metadata_dst *new_md; |
| 115 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 116 | if (!md_dst || md_dst->type != METADATA_IP_TUNNEL) |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 117 | return ERR_PTR(-EINVAL); |
| 118 | |
Tobias Klauser | f63ce5b | 2015-11-04 13:49:49 +0100 | [diff] [blame] | 119 | md_size = md_dst->u.tun_info.options_len; |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 120 | new_md = metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC); |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 121 | 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); |
Antoine Tenart | cfc56f8 | 2022-02-07 18:13:18 +0100 | [diff] [blame] | 126 | #ifdef CONFIG_DST_CACHE |
| 127 | /* Unclone the dst cache if there is one */ |
| 128 | if (new_md->u.tun_info.dst_cache.cache) { |
| 129 | int ret; |
| 130 | |
| 131 | ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC); |
| 132 | if (ret) { |
| 133 | metadata_dst_free(new_md); |
| 134 | return ERR_PTR(ret); |
| 135 | } |
| 136 | } |
| 137 | #endif |
| 138 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 139 | skb_dst_drop(skb); |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 140 | skb_dst_set(skb, &new_md->dst); |
| 141 | return new_md; |
| 142 | } |
| 143 | |
| 144 | static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb) |
| 145 | { |
| 146 | struct metadata_dst *dst; |
| 147 | |
| 148 | dst = tun_dst_unclone(skb); |
| 149 | if (IS_ERR(dst)) |
| 150 | return NULL; |
| 151 | |
| 152 | return &dst->u.tun_info; |
| 153 | } |
| 154 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 155 | static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr, |
| 156 | __be32 daddr, |
| 157 | __u8 tos, __u8 ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 158 | __be16 tp_dst, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 159 | __be16 flags, |
| 160 | __be64 tunnel_id, |
| 161 | int md_size) |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 162 | { |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 163 | struct metadata_dst *tun_dst; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 164 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 165 | tun_dst = tun_rx_dst(md_size); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 166 | if (!tun_dst) |
| 167 | return NULL; |
| 168 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 169 | ip_tunnel_key_init(&tun_dst->u.tun_info.key, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 170 | saddr, daddr, tos, ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 171 | 0, 0, tp_dst, tunnel_id, flags); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 172 | return tun_dst; |
| 173 | } |
| 174 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 175 | static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb, |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 176 | __be16 flags, |
| 177 | __be64 tunnel_id, |
| 178 | int md_size) |
| 179 | { |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 180 | const struct iphdr *iph = ip_hdr(skb); |
| 181 | |
| 182 | return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 183 | 0, flags, tunnel_id, md_size); |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr, |
| 187 | const struct in6_addr *daddr, |
| 188 | __u8 tos, __u8 ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 189 | __be16 tp_dst, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 190 | __be32 label, |
| 191 | __be16 flags, |
| 192 | __be64 tunnel_id, |
| 193 | int md_size) |
| 194 | { |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 195 | struct metadata_dst *tun_dst; |
| 196 | struct ip_tunnel_info *info; |
| 197 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 198 | tun_dst = tun_rx_dst(md_size); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 199 | if (!tun_dst) |
| 200 | return NULL; |
| 201 | |
| 202 | info = &tun_dst->u.tun_info; |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 203 | info->mode = IP_TUNNEL_INFO_IPV6; |
| 204 | info->key.tun_flags = flags; |
| 205 | info->key.tun_id = tunnel_id; |
| 206 | info->key.tp_src = 0; |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 207 | info->key.tp_dst = tp_dst; |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 208 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 209 | info->key.u.ipv6.src = *saddr; |
| 210 | info->key.u.ipv6.dst = *daddr; |
Daniel Borkmann | 1346114 | 2016-03-09 03:00:02 +0100 | [diff] [blame] | 211 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 212 | info->key.tos = tos; |
| 213 | info->key.ttl = ttl; |
| 214 | info->key.label = label; |
Daniel Borkmann | 1346114 | 2016-03-09 03:00:02 +0100 | [diff] [blame] | 215 | |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 216 | return tun_dst; |
| 217 | } |
| 218 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 219 | static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb, |
| 220 | __be16 flags, |
| 221 | __be64 tunnel_id, |
| 222 | int md_size) |
| 223 | { |
| 224 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
| 225 | |
| 226 | return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr, |
| 227 | ipv6_get_dsfield(ip6h), ip6h->hop_limit, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 228 | 0, ip6_flowlabel(ip6h), flags, tunnel_id, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 229 | md_size); |
| 230 | } |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 231 | #endif /* __NET_DST_METADATA_H */ |