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