GNU Linux-libre 4.4.290-gnu1
[releases.git] / include / net / dst_metadata.h
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;
10         union {
11                 struct ip_tunnel_info   tun_info;
12         } u;
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
25 static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
26 {
27         struct metadata_dst *md_dst = skb_metadata_dst(skb);
28         struct dst_entry *dst;
29
30         if (md_dst)
31                 return &md_dst->u.tun_info;
32
33         dst = skb_dst(skb);
34         if (dst && dst->lwtstate &&
35             (dst->lwtstate->type == LWTUNNEL_ENCAP_IP ||
36              dst->lwtstate->type == LWTUNNEL_ENCAP_IP6))
37                 return lwt_tun_info(dst->lwtstate);
38
39         return NULL;
40 }
41
42 static inline bool skb_valid_dst(const struct sk_buff *skb)
43 {
44         struct dst_entry *dst = skb_dst(skb);
45
46         return dst && !(dst->flags & DST_METADATA);
47 }
48
49 static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
50                                        const struct sk_buff *skb_b)
51 {
52         const struct metadata_dst *a, *b;
53
54         if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
55                 return 0;
56
57         a = (const struct metadata_dst *) skb_dst(skb_a);
58         b = (const struct metadata_dst *) skb_dst(skb_b);
59
60         if (!a != !b || a->u.tun_info.options_len != b->u.tun_info.options_len)
61                 return 1;
62
63         return memcmp(&a->u.tun_info, &b->u.tun_info,
64                       sizeof(a->u.tun_info) + a->u.tun_info.options_len);
65 }
66
67 struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
68 struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
69
70 static inline struct metadata_dst *tun_rx_dst(int md_size)
71 {
72         struct metadata_dst *tun_dst;
73
74         tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
75         if (!tun_dst)
76                 return NULL;
77
78         tun_dst->u.tun_info.options_len = 0;
79         tun_dst->u.tun_info.mode = 0;
80         return tun_dst;
81 }
82
83 static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
84 {
85         struct metadata_dst *md_dst = skb_metadata_dst(skb);
86         int md_size;
87         struct metadata_dst *new_md;
88
89         if (!md_dst)
90                 return ERR_PTR(-EINVAL);
91
92         md_size = md_dst->u.tun_info.options_len;
93         new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
94         if (!new_md)
95                 return ERR_PTR(-ENOMEM);
96
97         memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
98                sizeof(struct ip_tunnel_info) + md_size);
99         skb_dst_drop(skb);
100         dst_hold(&new_md->dst);
101         skb_dst_set(skb, &new_md->dst);
102         return new_md;
103 }
104
105 static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
106 {
107         struct metadata_dst *dst;
108
109         dst = tun_dst_unclone(skb);
110         if (IS_ERR(dst))
111                 return NULL;
112
113         return &dst->u.tun_info;
114 }
115
116 static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
117                                                  __be16 flags,
118                                                  __be64 tunnel_id,
119                                                  int md_size)
120 {
121         const struct iphdr *iph = ip_hdr(skb);
122         struct metadata_dst *tun_dst;
123
124         tun_dst = tun_rx_dst(md_size);
125         if (!tun_dst)
126                 return NULL;
127
128         ip_tunnel_key_init(&tun_dst->u.tun_info.key,
129                            iph->saddr, iph->daddr, iph->tos, iph->ttl,
130                            0, 0, tunnel_id, flags);
131         return tun_dst;
132 }
133
134 static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
135                                                  __be16 flags,
136                                                  __be64 tunnel_id,
137                                                  int md_size)
138 {
139         const struct ipv6hdr *ip6h = ipv6_hdr(skb);
140         struct metadata_dst *tun_dst;
141         struct ip_tunnel_info *info;
142
143         tun_dst = tun_rx_dst(md_size);
144         if (!tun_dst)
145                 return NULL;
146
147         info = &tun_dst->u.tun_info;
148         info->mode = IP_TUNNEL_INFO_IPV6;
149         info->key.tun_flags = flags;
150         info->key.tun_id = tunnel_id;
151         info->key.tp_src = 0;
152         info->key.tp_dst = 0;
153
154         info->key.u.ipv6.src = ip6h->saddr;
155         info->key.u.ipv6.dst = ip6h->daddr;
156         info->key.tos = ipv6_get_dsfield(ip6h);
157         info->key.ttl = ip6h->hop_limit;
158         return tun_dst;
159 }
160
161 #endif /* __NET_DST_METADATA_H */