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 | |
| 47 | struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags); |
Alexei Starovoitov | d3aa45c | 2015-07-30 15:36:57 -0700 | [diff] [blame] | 48 | 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] | 49 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 50 | static inline struct metadata_dst *tun_rx_dst(int md_size) |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 51 | { |
| 52 | struct metadata_dst *tun_dst; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 53 | |
| 54 | tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC); |
| 55 | if (!tun_dst) |
| 56 | return NULL; |
| 57 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 58 | tun_dst->u.tun_info.options_len = 0; |
| 59 | tun_dst->u.tun_info.mode = 0; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 60 | return tun_dst; |
| 61 | } |
| 62 | |
| 63 | static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb, |
| 64 | __be16 flags, |
| 65 | __be64 tunnel_id, |
| 66 | int md_size) |
| 67 | { |
| 68 | const struct iphdr *iph = ip_hdr(skb); |
| 69 | struct metadata_dst *tun_dst; |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 70 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 71 | tun_dst = tun_rx_dst(md_size); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 72 | if (!tun_dst) |
| 73 | return NULL; |
| 74 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 75 | ip_tunnel_key_init(&tun_dst->u.tun_info.key, |
| 76 | iph->saddr, iph->daddr, iph->tos, iph->ttl, |
| 77 | 0, 0, tunnel_id, flags); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 78 | return tun_dst; |
| 79 | } |
| 80 | |
| 81 | static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb, |
| 82 | __be16 flags, |
| 83 | __be64 tunnel_id, |
| 84 | int md_size) |
| 85 | { |
| 86 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
| 87 | struct metadata_dst *tun_dst; |
| 88 | struct ip_tunnel_info *info; |
| 89 | |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 90 | tun_dst = tun_rx_dst(md_size); |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 91 | if (!tun_dst) |
| 92 | return NULL; |
| 93 | |
| 94 | info = &tun_dst->u.tun_info; |
Pravin B Shelar | 4c22279 | 2015-08-30 18:09:38 -0700 | [diff] [blame] | 95 | info->mode = IP_TUNNEL_INFO_IPV6; |
| 96 | info->key.tun_flags = flags; |
| 97 | info->key.tun_id = tunnel_id; |
| 98 | info->key.tp_src = 0; |
| 99 | info->key.tp_dst = 0; |
| 100 | |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 101 | info->key.u.ipv6.src = ip6h->saddr; |
| 102 | info->key.u.ipv6.dst = ip6h->daddr; |
| 103 | info->key.tos = ipv6_get_dsfield(ip6h); |
| 104 | info->key.ttl = ip6h->hop_limit; |
| 105 | return tun_dst; |
| 106 | } |
| 107 | |
Thomas Graf | f38a9eb | 2015-07-21 10:43:56 +0200 | [diff] [blame] | 108 | #endif /* __NET_DST_METADATA_H */ |