1 #ifndef __NET_DST_METADATA_H
2 #define __NET_DST_METADATA_H 1
4 #include <linux/skbuff.h>
5 #include <net/ip_tunnels.h>
11 struct ip_tunnel_info tun_info;
15 static inline struct metadata_dst *skb_metadata_dst(struct sk_buff *skb)
17 struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
19 if (md_dst && md_dst->dst.flags & DST_METADATA)
25 static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
27 struct metadata_dst *md_dst = skb_metadata_dst(skb);
28 struct dst_entry *dst;
31 return &md_dst->u.tun_info;
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);
42 static inline bool skb_valid_dst(const struct sk_buff *skb)
44 struct dst_entry *dst = skb_dst(skb);
46 return dst && !(dst->flags & DST_METADATA);
49 static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
50 const struct sk_buff *skb_b)
52 const struct metadata_dst *a, *b;
54 if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
57 a = (const struct metadata_dst *) skb_dst(skb_a);
58 b = (const struct metadata_dst *) skb_dst(skb_b);
60 if (!a != !b || a->u.tun_info.options_len != b->u.tun_info.options_len)
63 return memcmp(&a->u.tun_info, &b->u.tun_info,
64 sizeof(a->u.tun_info) + a->u.tun_info.options_len);
67 void metadata_dst_free(struct metadata_dst *);
68 struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
69 struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
71 static inline struct metadata_dst *tun_rx_dst(int md_size)
73 struct metadata_dst *tun_dst;
75 tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
79 tun_dst->u.tun_info.options_len = 0;
80 tun_dst->u.tun_info.mode = 0;
84 static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
86 struct metadata_dst *md_dst = skb_metadata_dst(skb);
88 struct metadata_dst *new_md;
91 return ERR_PTR(-EINVAL);
93 md_size = md_dst->u.tun_info.options_len;
94 new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
96 return ERR_PTR(-ENOMEM);
98 memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
99 sizeof(struct ip_tunnel_info) + md_size);
100 #ifdef CONFIG_DST_CACHE
101 /* Unclone the dst cache if there is one */
102 if (new_md->u.tun_info.dst_cache.cache) {
105 ret = dst_cache_init(&new_md->u.tun_info.dst_cache, GFP_ATOMIC);
107 metadata_dst_free(new_md);
114 skb_dst_set(skb, &new_md->dst);
118 static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
120 struct metadata_dst *dst;
122 dst = tun_dst_unclone(skb);
126 return &dst->u.tun_info;
129 static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
136 struct metadata_dst *tun_dst;
138 tun_dst = tun_rx_dst(md_size);
142 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
143 saddr, daddr, tos, ttl,
144 0, 0, 0, tunnel_id, flags);
148 static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
153 const struct iphdr *iph = ip_hdr(skb);
155 return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
156 flags, tunnel_id, md_size);
159 static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
160 const struct in6_addr *daddr,
167 struct metadata_dst *tun_dst;
168 struct ip_tunnel_info *info;
170 tun_dst = tun_rx_dst(md_size);
174 info = &tun_dst->u.tun_info;
175 info->mode = IP_TUNNEL_INFO_IPV6;
176 info->key.tun_flags = flags;
177 info->key.tun_id = tunnel_id;
178 info->key.tp_src = 0;
179 info->key.tp_dst = 0;
181 info->key.u.ipv6.src = *saddr;
182 info->key.u.ipv6.dst = *daddr;
186 info->key.label = label;
191 static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
196 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
198 return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
199 ipv6_get_dsfield(ip6h), ip6h->hop_limit,
200 ip6_flowlabel(ip6h), flags, tunnel_id,
203 #endif /* __NET_DST_METADATA_H */