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 struct metadata_dst
*metadata_dst_alloc(u8 optslen
, gfp_t flags
);
48 struct metadata_dst __percpu
*metadata_dst_alloc_percpu(u8 optslen
, gfp_t flags
);
50 static inline struct metadata_dst
*tun_rx_dst(int md_size
)
52 struct metadata_dst
*tun_dst
;
54 tun_dst
= metadata_dst_alloc(md_size
, GFP_ATOMIC
);
58 tun_dst
->u
.tun_info
.options_len
= 0;
59 tun_dst
->u
.tun_info
.mode
= 0;
63 static inline struct metadata_dst
*tun_dst_unclone(struct sk_buff
*skb
)
65 struct metadata_dst
*md_dst
= skb_metadata_dst(skb
);
67 struct metadata_dst
*new_md
;
70 return ERR_PTR(-EINVAL
);
72 md_size
= md_dst
->u
.tun_info
.options_len
;
73 new_md
= metadata_dst_alloc(md_size
, GFP_ATOMIC
);
75 return ERR_PTR(-ENOMEM
);
77 memcpy(&new_md
->u
.tun_info
, &md_dst
->u
.tun_info
,
78 sizeof(struct ip_tunnel_info
) + md_size
);
80 dst_hold(&new_md
->dst
);
81 skb_dst_set(skb
, &new_md
->dst
);
85 static inline struct ip_tunnel_info
*skb_tunnel_info_unclone(struct sk_buff
*skb
)
87 struct metadata_dst
*dst
;
89 dst
= tun_dst_unclone(skb
);
93 return &dst
->u
.tun_info
;
96 static inline struct metadata_dst
*ip_tun_rx_dst(struct sk_buff
*skb
,
101 const struct iphdr
*iph
= ip_hdr(skb
);
102 struct metadata_dst
*tun_dst
;
104 tun_dst
= tun_rx_dst(md_size
);
108 ip_tunnel_key_init(&tun_dst
->u
.tun_info
.key
,
109 iph
->saddr
, iph
->daddr
, iph
->tos
, iph
->ttl
,
110 0, 0, tunnel_id
, flags
);
114 static inline struct metadata_dst
*ipv6_tun_rx_dst(struct sk_buff
*skb
,
119 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
120 struct metadata_dst
*tun_dst
;
121 struct ip_tunnel_info
*info
;
123 tun_dst
= tun_rx_dst(md_size
);
127 info
= &tun_dst
->u
.tun_info
;
128 info
->mode
= IP_TUNNEL_INFO_IPV6
;
129 info
->key
.tun_flags
= flags
;
130 info
->key
.tun_id
= tunnel_id
;
131 info
->key
.tp_src
= 0;
132 info
->key
.tp_dst
= 0;
134 info
->key
.u
.ipv6
.src
= ip6h
->saddr
;
135 info
->key
.u
.ipv6
.dst
= ip6h
->daddr
;
136 info
->key
.tos
= ipv6_get_dsfield(ip6h
);
137 info
->key
.ttl
= ip6h
->hop_limit
;
141 #endif /* __NET_DST_METADATA_H */