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); |
| 48 | if (dst && dst->lwtstate) |
| 49 | return lwt_tun_info(dst->lwtstate); |
Thomas Graf | 3093fbe | 2015-07-21 10:44:00 +0200 | [diff] [blame] | 50 | |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 51 | return NULL; |
| 52 | } |
| 53 | |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 54 | static inline bool skb_valid_dst(const struct sk_buff *skb) |
| 55 | { |
| 56 | struct dst_entry *dst = skb_dst(skb); |
| 57 | |
| 58 | return dst && !(dst->flags & DST_METADATA); |
| 59 | } |
| 60 | |
Jesse Gross | ce87fc6 | 2016-01-20 17:59:49 -0800 | [diff] [blame] | 61 | static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a, |
| 62 | const struct sk_buff *skb_b) |
| 63 | { |
| 64 | const struct metadata_dst *a, *b; |
| 65 | |
| 66 | if (!(skb_a->_skb_refdst | skb_b->_skb_refdst)) |
| 67 | return 0; |
| 68 | |
| 69 | a = (const struct metadata_dst *) skb_dst(skb_a); |
| 70 | b = (const struct metadata_dst *) skb_dst(skb_b); |
| 71 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 72 | if (!a != !b || a->type != b->type) |
Jesse Gross | ce87fc6 | 2016-01-20 17:59:49 -0800 | [diff] [blame] | 73 | return 1; |
| 74 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 75 | switch (a->type) { |
| 76 | case METADATA_HW_PORT_MUX: |
| 77 | return memcmp(&a->u.port_info, &b->u.port_info, |
| 78 | sizeof(a->u.port_info)); |
| 79 | case METADATA_IP_TUNNEL: |
| 80 | return memcmp(&a->u.tun_info, &b->u.tun_info, |
| 81 | sizeof(a->u.tun_info) + |
| 82 | a->u.tun_info.options_len); |
| 83 | default: |
| 84 | return 1; |
| 85 | } |
Jesse Gross | ce87fc6 | 2016-01-20 17:59:49 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Paolo Abeni | d71785f | 2016-02-12 15:43:57 +0100 | [diff] [blame] | 88 | void metadata_dst_free(struct metadata_dst *); |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 89 | struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type, |
| 90 | gfp_t flags); |
Jakub Kicinski | d66f2b9 | 2017-10-09 10:30:14 -0700 | [diff] [blame] | 91 | void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst); |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 92 | struct metadata_dst __percpu * |
| 93 | 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] | 94 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 95 | static inline struct metadata_dst *tun_rx_dst(int md_size) |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 96 | { |
| 97 | struct metadata_dst *tun_dst; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 98 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 99 | 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] | 100 | if (!tun_dst) |
| 101 | return NULL; |
| 102 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 103 | tun_dst->u.tun_info.options_len = 0; |
| 104 | tun_dst->u.tun_info.mode = 0; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 105 | return tun_dst; |
| 106 | } |
| 107 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 108 | static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb) |
| 109 | { |
| 110 | struct metadata_dst *md_dst = skb_metadata_dst(skb); |
Tobias Klauser | f63ce5b | 2015-11-04 13:49:49 +0100 | [diff] [blame] | 111 | int md_size; |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 112 | struct metadata_dst *new_md; |
| 113 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 114 | if (!md_dst || md_dst->type != METADATA_IP_TUNNEL) |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 115 | return ERR_PTR(-EINVAL); |
| 116 | |
Tobias Klauser | f63ce5b | 2015-11-04 13:49:49 +0100 | [diff] [blame] | 117 | md_size = md_dst->u.tun_info.options_len; |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 118 | 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] | 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 | skb_dst_drop(skb); |
| 125 | dst_hold(&new_md->dst); |
| 126 | skb_dst_set(skb, &new_md->dst); |
| 127 | return new_md; |
| 128 | } |
| 129 | |
| 130 | static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb) |
| 131 | { |
| 132 | struct metadata_dst *dst; |
| 133 | |
| 134 | dst = tun_dst_unclone(skb); |
| 135 | if (IS_ERR(dst)) |
| 136 | return NULL; |
| 137 | |
| 138 | return &dst->u.tun_info; |
| 139 | } |
| 140 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 141 | static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr, |
| 142 | __be32 daddr, |
| 143 | __u8 tos, __u8 ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 144 | __be16 tp_dst, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 145 | __be16 flags, |
| 146 | __be64 tunnel_id, |
| 147 | int md_size) |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 148 | { |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 149 | struct metadata_dst *tun_dst; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 150 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 151 | tun_dst = tun_rx_dst(md_size); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 152 | if (!tun_dst) |
| 153 | return NULL; |
| 154 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 155 | ip_tunnel_key_init(&tun_dst->u.tun_info.key, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 156 | saddr, daddr, tos, ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 157 | 0, 0, tp_dst, tunnel_id, flags); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 158 | return tun_dst; |
| 159 | } |
| 160 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 161 | 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] | 162 | __be16 flags, |
| 163 | __be64 tunnel_id, |
| 164 | int md_size) |
| 165 | { |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 166 | const struct iphdr *iph = ip_hdr(skb); |
| 167 | |
| 168 | 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] | 169 | 0, flags, tunnel_id, md_size); |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr, |
| 173 | const struct in6_addr *daddr, |
| 174 | __u8 tos, __u8 ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 175 | __be16 tp_dst, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 176 | __be32 label, |
| 177 | __be16 flags, |
| 178 | __be64 tunnel_id, |
| 179 | int md_size) |
| 180 | { |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 181 | struct metadata_dst *tun_dst; |
| 182 | struct ip_tunnel_info *info; |
| 183 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 184 | tun_dst = tun_rx_dst(md_size); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 185 | if (!tun_dst) |
| 186 | return NULL; |
| 187 | |
| 188 | info = &tun_dst->u.tun_info; |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 189 | info->mode = IP_TUNNEL_INFO_IPV6; |
| 190 | info->key.tun_flags = flags; |
| 191 | info->key.tun_id = tunnel_id; |
| 192 | info->key.tp_src = 0; |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 193 | info->key.tp_dst = tp_dst; |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 194 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 195 | info->key.u.ipv6.src = *saddr; |
| 196 | info->key.u.ipv6.dst = *daddr; |
Daniel Borkmann | 1346114 | 2016-03-09 03:00:02 +0100 | [diff] [blame] | 197 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 198 | info->key.tos = tos; |
| 199 | info->key.ttl = ttl; |
| 200 | info->key.label = label; |
Daniel Borkmann | 1346114 | 2016-03-09 03:00:02 +0100 | [diff] [blame] | 201 | |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 202 | return tun_dst; |
| 203 | } |
| 204 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 205 | static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb, |
| 206 | __be16 flags, |
| 207 | __be64 tunnel_id, |
| 208 | int md_size) |
| 209 | { |
| 210 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
| 211 | |
| 212 | return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr, |
| 213 | ipv6_get_dsfield(ip6h), ip6h->hop_limit, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 214 | 0, ip6_flowlabel(ip6h), flags, tunnel_id, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 215 | md_size); |
| 216 | } |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 217 | #endif /* __NET_DST_METADATA_H */ |