Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 1 | #ifndef __NET_DST_METADATA_H |
| 2 | #define __NET_DST_METADATA_H 1 |
| 3 | |
| 4 | #include <linux/skbuff.h> |
| 5 | #include <net/ip_tunnels.h> |
| 6 | #include <net/dst.h> |
| 7 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 8 | enum metadata_type { |
| 9 | METADATA_IP_TUNNEL, |
| 10 | METADATA_HW_PORT_MUX, |
| 11 | }; |
| 12 | |
| 13 | struct hw_port_info { |
| 14 | struct net_device *lower_dev; |
| 15 | u32 port_id; |
| 16 | }; |
| 17 | |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 18 | struct metadata_dst { |
| 19 | struct dst_entry dst; |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 20 | enum metadata_type type; |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 21 | union { |
| 22 | struct ip_tunnel_info tun_info; |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 23 | struct hw_port_info port_info; |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 24 | } u; |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
Simon Horman | 32f1636 | 2017-10-02 10:41:15 +0200 | [diff] [blame] | 27 | 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] | 28 | { |
| 29 | struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb); |
| 30 | |
| 31 | if (md_dst && md_dst->dst.flags & DST_METADATA) |
| 32 | return md_dst; |
| 33 | |
| 34 | return NULL; |
| 35 | } |
| 36 | |
Simon Horman | 32f1636 | 2017-10-02 10:41:15 +0200 | [diff] [blame] | 37 | static inline struct ip_tunnel_info * |
| 38 | skb_tunnel_info(const struct sk_buff *skb) |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 39 | { |
| 40 | struct metadata_dst *md_dst = skb_metadata_dst(skb); |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 41 | struct dst_entry *dst; |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 42 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 43 | if (md_dst && md_dst->type == METADATA_IP_TUNNEL) |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 44 | return &md_dst->u.tun_info; |
| 45 | |
Jiri Benc | 61adedf | 2015-08-20 13:56:25 +0200 | [diff] [blame] | 46 | dst = skb_dst(skb); |
| 47 | if (dst && dst->lwtstate) |
| 48 | return lwt_tun_info(dst->lwtstate); |
Thomas Graf | 3093fbe | 2015-07-21 10:44:00 +0200 | [diff] [blame] | 49 | |
Thomas Graf | ee122c7 | 2015-07-21 10:43:58 +0200 | [diff] [blame] | 50 | return NULL; |
| 51 | } |
| 52 | |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 53 | static 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 | |
Jesse Gross | ce87fc6 | 2016-01-20 17:59:49 -0800 | [diff] [blame] | 60 | static 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 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 71 | if (!a != !b || a->type != b->type) |
Jesse Gross | ce87fc6 | 2016-01-20 17:59:49 -0800 | [diff] [blame] | 72 | return 1; |
| 73 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 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 | } |
Jesse Gross | ce87fc6 | 2016-01-20 17:59:49 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Paolo Abeni | d71785f | 2016-02-12 15:43:57 +0100 | [diff] [blame] | 87 | void metadata_dst_free(struct metadata_dst *); |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 88 | struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type, |
| 89 | gfp_t flags); |
Jakub Kicinski | d66f2b9 | 2017-10-09 10:30:14 -0700 | [diff] [blame^] | 90 | void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst); |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 91 | struct metadata_dst __percpu * |
| 92 | 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] | 93 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 94 | static inline struct metadata_dst *tun_rx_dst(int md_size) |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 95 | { |
| 96 | struct metadata_dst *tun_dst; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 97 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 98 | 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] | 99 | if (!tun_dst) |
| 100 | return NULL; |
| 101 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 102 | tun_dst->u.tun_info.options_len = 0; |
| 103 | tun_dst->u.tun_info.mode = 0; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 104 | return tun_dst; |
| 105 | } |
| 106 | |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 107 | static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb) |
| 108 | { |
| 109 | struct metadata_dst *md_dst = skb_metadata_dst(skb); |
Tobias Klauser | f63ce5b | 2015-11-04 13:49:49 +0100 | [diff] [blame] | 110 | int md_size; |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 111 | struct metadata_dst *new_md; |
| 112 | |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 113 | if (!md_dst || md_dst->type != METADATA_IP_TUNNEL) |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 114 | return ERR_PTR(-EINVAL); |
| 115 | |
Tobias Klauser | f63ce5b | 2015-11-04 13:49:49 +0100 | [diff] [blame] | 116 | md_size = md_dst->u.tun_info.options_len; |
Jakub Kicinski | 3fcece1 | 2017-06-23 22:11:58 +0200 | [diff] [blame] | 117 | 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] | 118 | if (!new_md) |
| 119 | return ERR_PTR(-ENOMEM); |
| 120 | |
| 121 | memcpy(&new_md->u.tun_info, &md_dst->u.tun_info, |
| 122 | sizeof(struct ip_tunnel_info) + md_size); |
| 123 | skb_dst_drop(skb); |
| 124 | dst_hold(&new_md->dst); |
| 125 | skb_dst_set(skb, &new_md->dst); |
| 126 | return new_md; |
| 127 | } |
| 128 | |
| 129 | static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb) |
| 130 | { |
| 131 | struct metadata_dst *dst; |
| 132 | |
| 133 | dst = tun_dst_unclone(skb); |
| 134 | if (IS_ERR(dst)) |
| 135 | return NULL; |
| 136 | |
| 137 | return &dst->u.tun_info; |
| 138 | } |
| 139 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 140 | static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr, |
| 141 | __be32 daddr, |
| 142 | __u8 tos, __u8 ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 143 | __be16 tp_dst, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 144 | __be16 flags, |
| 145 | __be64 tunnel_id, |
| 146 | int md_size) |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 147 | { |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 148 | struct metadata_dst *tun_dst; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 149 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 150 | tun_dst = tun_rx_dst(md_size); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 151 | if (!tun_dst) |
| 152 | return NULL; |
| 153 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 154 | ip_tunnel_key_init(&tun_dst->u.tun_info.key, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 155 | saddr, daddr, tos, ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 156 | 0, 0, tp_dst, tunnel_id, flags); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 157 | return tun_dst; |
| 158 | } |
| 159 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 160 | 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] | 161 | __be16 flags, |
| 162 | __be64 tunnel_id, |
| 163 | int md_size) |
| 164 | { |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 165 | const struct iphdr *iph = ip_hdr(skb); |
| 166 | |
| 167 | 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] | 168 | 0, flags, tunnel_id, md_size); |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr, |
| 172 | const struct in6_addr *daddr, |
| 173 | __u8 tos, __u8 ttl, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 174 | __be16 tp_dst, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 175 | __be32 label, |
| 176 | __be16 flags, |
| 177 | __be64 tunnel_id, |
| 178 | int md_size) |
| 179 | { |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 180 | struct metadata_dst *tun_dst; |
| 181 | struct ip_tunnel_info *info; |
| 182 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 183 | tun_dst = tun_rx_dst(md_size); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 184 | if (!tun_dst) |
| 185 | return NULL; |
| 186 | |
| 187 | info = &tun_dst->u.tun_info; |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 188 | info->mode = IP_TUNNEL_INFO_IPV6; |
| 189 | info->key.tun_flags = flags; |
| 190 | info->key.tun_id = tunnel_id; |
| 191 | info->key.tp_src = 0; |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 192 | info->key.tp_dst = tp_dst; |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 193 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 194 | info->key.u.ipv6.src = *saddr; |
| 195 | info->key.u.ipv6.dst = *daddr; |
Daniel Borkmann | 1346114 | 2016-03-09 03:00:02 +0100 | [diff] [blame] | 196 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 197 | info->key.tos = tos; |
| 198 | info->key.ttl = ttl; |
| 199 | info->key.label = label; |
Daniel Borkmann | 1346114 | 2016-03-09 03:00:02 +0100 | [diff] [blame] | 200 | |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 201 | return tun_dst; |
| 202 | } |
| 203 | |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 204 | static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb, |
| 205 | __be16 flags, |
| 206 | __be64 tunnel_id, |
| 207 | int md_size) |
| 208 | { |
| 209 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
| 210 | |
| 211 | return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr, |
| 212 | ipv6_get_dsfield(ip6h), ip6h->hop_limit, |
Hadar Hen Zion | 24ba898 | 2016-11-07 15:14:40 +0200 | [diff] [blame] | 213 | 0, ip6_flowlabel(ip6h), flags, tunnel_id, |
Amir Vadai | 2ff378b | 2016-09-08 16:23:46 +0300 | [diff] [blame] | 214 | md_size); |
| 215 | } |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 216 | #endif /* __NET_DST_METADATA_H */ |