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