1 #ifndef __NET_DST_METADATA_H
2 #define __NET_DST_METADATA_H 1
4 #include <linux/skbuff.h>
5 #include <net/ip_tunnels.h>
14 struct net_device
*lower_dev
;
20 enum metadata_type type
;
22 struct ip_tunnel_info tun_info
;
23 struct hw_port_info port_info
;
27 static inline struct metadata_dst
*skb_metadata_dst(struct sk_buff
*skb
)
29 struct metadata_dst
*md_dst
= (struct metadata_dst
*) skb_dst(skb
);
31 if (md_dst
&& md_dst
->dst
.flags
& DST_METADATA
)
37 static inline struct ip_tunnel_info
*skb_tunnel_info(struct sk_buff
*skb
)
39 struct metadata_dst
*md_dst
= skb_metadata_dst(skb
);
40 struct dst_entry
*dst
;
42 if (md_dst
&& md_dst
->type
== METADATA_IP_TUNNEL
)
43 return &md_dst
->u
.tun_info
;
46 if (dst
&& dst
->lwtstate
)
47 return lwt_tun_info(dst
->lwtstate
);
52 static inline bool skb_valid_dst(const struct sk_buff
*skb
)
54 struct dst_entry
*dst
= skb_dst(skb
);
56 return dst
&& !(dst
->flags
& DST_METADATA
);
59 static inline int skb_metadata_dst_cmp(const struct sk_buff
*skb_a
,
60 const struct sk_buff
*skb_b
)
62 const struct metadata_dst
*a
, *b
;
64 if (!(skb_a
->_skb_refdst
| skb_b
->_skb_refdst
))
67 a
= (const struct metadata_dst
*) skb_dst(skb_a
);
68 b
= (const struct metadata_dst
*) skb_dst(skb_b
);
70 if (!a
!= !b
|| a
->type
!= b
->type
)
74 case METADATA_HW_PORT_MUX
:
75 return memcmp(&a
->u
.port_info
, &b
->u
.port_info
,
76 sizeof(a
->u
.port_info
));
77 case METADATA_IP_TUNNEL
:
78 return memcmp(&a
->u
.tun_info
, &b
->u
.tun_info
,
79 sizeof(a
->u
.tun_info
) +
80 a
->u
.tun_info
.options_len
);
86 void metadata_dst_free(struct metadata_dst
*);
87 struct metadata_dst
*metadata_dst_alloc(u8 optslen
, enum metadata_type type
,
89 struct metadata_dst __percpu
*
90 metadata_dst_alloc_percpu(u8 optslen
, enum metadata_type type
, gfp_t flags
);
92 static inline struct metadata_dst
*tun_rx_dst(int md_size
)
94 struct metadata_dst
*tun_dst
;
96 tun_dst
= metadata_dst_alloc(md_size
, METADATA_IP_TUNNEL
, GFP_ATOMIC
);
100 tun_dst
->u
.tun_info
.options_len
= 0;
101 tun_dst
->u
.tun_info
.mode
= 0;
105 static inline struct metadata_dst
*tun_dst_unclone(struct sk_buff
*skb
)
107 struct metadata_dst
*md_dst
= skb_metadata_dst(skb
);
109 struct metadata_dst
*new_md
;
111 if (!md_dst
|| md_dst
->type
!= METADATA_IP_TUNNEL
)
112 return ERR_PTR(-EINVAL
);
114 md_size
= md_dst
->u
.tun_info
.options_len
;
115 new_md
= metadata_dst_alloc(md_size
, METADATA_IP_TUNNEL
, GFP_ATOMIC
);
117 return ERR_PTR(-ENOMEM
);
119 memcpy(&new_md
->u
.tun_info
, &md_dst
->u
.tun_info
,
120 sizeof(struct ip_tunnel_info
) + md_size
);
122 dst_hold(&new_md
->dst
);
123 skb_dst_set(skb
, &new_md
->dst
);
127 static inline struct ip_tunnel_info
*skb_tunnel_info_unclone(struct sk_buff
*skb
)
129 struct metadata_dst
*dst
;
131 dst
= tun_dst_unclone(skb
);
135 return &dst
->u
.tun_info
;
138 static inline struct metadata_dst
*__ip_tun_set_dst(__be32 saddr
,
146 struct metadata_dst
*tun_dst
;
148 tun_dst
= tun_rx_dst(md_size
);
152 ip_tunnel_key_init(&tun_dst
->u
.tun_info
.key
,
153 saddr
, daddr
, tos
, ttl
,
154 0, 0, tp_dst
, tunnel_id
, flags
);
158 static inline struct metadata_dst
*ip_tun_rx_dst(struct sk_buff
*skb
,
163 const struct iphdr
*iph
= ip_hdr(skb
);
165 return __ip_tun_set_dst(iph
->saddr
, iph
->daddr
, iph
->tos
, iph
->ttl
,
166 0, flags
, tunnel_id
, md_size
);
169 static inline struct metadata_dst
*__ipv6_tun_set_dst(const struct in6_addr
*saddr
,
170 const struct in6_addr
*daddr
,
178 struct metadata_dst
*tun_dst
;
179 struct ip_tunnel_info
*info
;
181 tun_dst
= tun_rx_dst(md_size
);
185 info
= &tun_dst
->u
.tun_info
;
186 info
->mode
= IP_TUNNEL_INFO_IPV6
;
187 info
->key
.tun_flags
= flags
;
188 info
->key
.tun_id
= tunnel_id
;
189 info
->key
.tp_src
= 0;
190 info
->key
.tp_dst
= tp_dst
;
192 info
->key
.u
.ipv6
.src
= *saddr
;
193 info
->key
.u
.ipv6
.dst
= *daddr
;
197 info
->key
.label
= label
;
202 static inline struct metadata_dst
*ipv6_tun_rx_dst(struct sk_buff
*skb
,
207 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
209 return __ipv6_tun_set_dst(&ip6h
->saddr
, &ip6h
->daddr
,
210 ipv6_get_dsfield(ip6h
), ip6h
->hop_limit
,
211 0, ip6_flowlabel(ip6h
), flags
, tunnel_id
,
214 #endif /* __NET_DST_METADATA_H */