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 return lwt_tun_info(dst->lwtstate);
40 static inline bool skb_valid_dst(const struct sk_buff *skb)
42 struct dst_entry *dst = skb_dst(skb);
44 return dst && !(dst->flags & DST_METADATA);
47 static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
48 const struct sk_buff *skb_b)
50 const struct metadata_dst *a, *b;
52 if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
55 a = (const struct metadata_dst *) skb_dst(skb_a);
56 b = (const struct metadata_dst *) skb_dst(skb_b);
58 if (!a != !b || a->u.tun_info.options_len != b->u.tun_info.options_len)
61 return memcmp(&a->u.tun_info, &b->u.tun_info,
62 sizeof(a->u.tun_info) + a->u.tun_info.options_len);
65 void metadata_dst_free(struct metadata_dst *);
66 struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
67 struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
69 static inline struct metadata_dst *tun_rx_dst(int md_size)
71 struct metadata_dst *tun_dst;
73 tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
77 tun_dst->u.tun_info.options_len = 0;
78 tun_dst->u.tun_info.mode = 0;
82 static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
84 struct metadata_dst *md_dst = skb_metadata_dst(skb);
86 struct metadata_dst *new_md;
89 return ERR_PTR(-EINVAL);
91 md_size = md_dst->u.tun_info.options_len;
92 new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
94 return ERR_PTR(-ENOMEM);
96 memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
97 sizeof(struct ip_tunnel_info) + md_size);
99 dst_hold(&new_md->dst);
100 skb_dst_set(skb, &new_md->dst);
104 static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
106 struct metadata_dst *dst;
108 dst = tun_dst_unclone(skb);
112 return &dst->u.tun_info;
115 static inline struct metadata_dst *__ip_tun_set_dst(__be32 saddr,
123 struct metadata_dst *tun_dst;
125 tun_dst = tun_rx_dst(md_size);
129 ip_tunnel_key_init(&tun_dst->u.tun_info.key,
130 saddr, daddr, tos, ttl,
131 0, 0, tp_dst, tunnel_id, flags);
135 static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
140 const struct iphdr *iph = ip_hdr(skb);
142 return __ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl,
143 0, flags, tunnel_id, md_size);
146 static inline struct metadata_dst *__ipv6_tun_set_dst(const struct in6_addr *saddr,
147 const struct in6_addr *daddr,
155 struct metadata_dst *tun_dst;
156 struct ip_tunnel_info *info;
158 tun_dst = tun_rx_dst(md_size);
162 info = &tun_dst->u.tun_info;
163 info->mode = IP_TUNNEL_INFO_IPV6;
164 info->key.tun_flags = flags;
165 info->key.tun_id = tunnel_id;
166 info->key.tp_src = 0;
167 info->key.tp_dst = tp_dst;
169 info->key.u.ipv6.src = *saddr;
170 info->key.u.ipv6.dst = *daddr;
174 info->key.label = label;
179 static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
184 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
186 return __ipv6_tun_set_dst(&ip6h->saddr, &ip6h->daddr,
187 ipv6_get_dsfield(ip6h), ip6h->hop_limit,
188 0, ip6_flowlabel(ip6h), flags, tunnel_id,
191 #endif /* __NET_DST_METADATA_H */