1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_DST_METADATA_H
3 #define __NET_DST_METADATA_H 1
5 #include <linux/skbuff.h>
6 #include <net/ip_tunnels.h>
15 struct net_device
*lower_dev
;
21 enum metadata_type type
;
23 struct ip_tunnel_info tun_info
;
24 struct hw_port_info port_info
;
28 static inline struct metadata_dst
*skb_metadata_dst(struct sk_buff
*skb
)
30 struct metadata_dst
*md_dst
= (struct metadata_dst
*) skb_dst(skb
);
32 if (md_dst
&& md_dst
->dst
.flags
& DST_METADATA
)
38 static inline struct ip_tunnel_info
*skb_tunnel_info(struct sk_buff
*skb
)
40 struct metadata_dst
*md_dst
= skb_metadata_dst(skb
);
41 struct dst_entry
*dst
;
43 if (md_dst
&& md_dst
->type
== METADATA_IP_TUNNEL
)
44 return &md_dst
->u
.tun_info
;
47 if (dst
&& dst
->lwtstate
)
48 return lwt_tun_info(dst
->lwtstate
);
53 static inline bool skb_valid_dst(const struct sk_buff
*skb
)
55 struct dst_entry
*dst
= skb_dst(skb
);
57 return dst
&& !(dst
->flags
& DST_METADATA
);
60 static inline int skb_metadata_dst_cmp(const struct sk_buff
*skb_a
,
61 const struct sk_buff
*skb_b
)
63 const struct metadata_dst
*a
, *b
;
65 if (!(skb_a
->_skb_refdst
| skb_b
->_skb_refdst
))
68 a
= (const struct metadata_dst
*) skb_dst(skb_a
);
69 b
= (const struct metadata_dst
*) skb_dst(skb_b
);
71 if (!a
!= !b
|| a
->type
!= b
->type
)
75 case METADATA_HW_PORT_MUX
:
76 return memcmp(&a
->u
.port_info
, &b
->u
.port_info
,
77 sizeof(a
->u
.port_info
));
78 case METADATA_IP_TUNNEL
:
79 return memcmp(&a
->u
.tun_info
, &b
->u
.tun_info
,
80 sizeof(a
->u
.tun_info
) +
81 a
->u
.tun_info
.options_len
);
87 void metadata_dst_free(struct metadata_dst
*);
88 struct metadata_dst
*metadata_dst_alloc(u8 optslen
, enum metadata_type type
,
90 struct metadata_dst __percpu
*
91 metadata_dst_alloc_percpu(u8 optslen
, enum metadata_type type
, gfp_t flags
);
93 static inline struct metadata_dst
*tun_rx_dst(int md_size
)
95 struct metadata_dst
*tun_dst
;
97 tun_dst
= metadata_dst_alloc(md_size
, METADATA_IP_TUNNEL
, GFP_ATOMIC
);
101 tun_dst
->u
.tun_info
.options_len
= 0;
102 tun_dst
->u
.tun_info
.mode
= 0;
106 static inline struct metadata_dst
*tun_dst_unclone(struct sk_buff
*skb
)
108 struct metadata_dst
*md_dst
= skb_metadata_dst(skb
);
110 struct metadata_dst
*new_md
;
112 if (!md_dst
|| md_dst
->type
!= METADATA_IP_TUNNEL
)
113 return ERR_PTR(-EINVAL
);
115 md_size
= md_dst
->u
.tun_info
.options_len
;
116 new_md
= metadata_dst_alloc(md_size
, METADATA_IP_TUNNEL
, GFP_ATOMIC
);
118 return ERR_PTR(-ENOMEM
);
120 memcpy(&new_md
->u
.tun_info
, &md_dst
->u
.tun_info
,
121 sizeof(struct ip_tunnel_info
) + md_size
);
123 dst_hold(&new_md
->dst
);
124 skb_dst_set(skb
, &new_md
->dst
);
128 static inline struct ip_tunnel_info
*skb_tunnel_info_unclone(struct sk_buff
*skb
)
130 struct metadata_dst
*dst
;
132 dst
= tun_dst_unclone(skb
);
136 return &dst
->u
.tun_info
;
139 static inline struct metadata_dst
*__ip_tun_set_dst(__be32 saddr
,
147 struct metadata_dst
*tun_dst
;
149 tun_dst
= tun_rx_dst(md_size
);
153 ip_tunnel_key_init(&tun_dst
->u
.tun_info
.key
,
154 saddr
, daddr
, tos
, ttl
,
155 0, 0, tp_dst
, tunnel_id
, flags
);
159 static inline struct metadata_dst
*ip_tun_rx_dst(struct sk_buff
*skb
,
164 const struct iphdr
*iph
= ip_hdr(skb
);
166 return __ip_tun_set_dst(iph
->saddr
, iph
->daddr
, iph
->tos
, iph
->ttl
,
167 0, flags
, tunnel_id
, md_size
);
170 static inline struct metadata_dst
*__ipv6_tun_set_dst(const struct in6_addr
*saddr
,
171 const struct in6_addr
*daddr
,
179 struct metadata_dst
*tun_dst
;
180 struct ip_tunnel_info
*info
;
182 tun_dst
= tun_rx_dst(md_size
);
186 info
= &tun_dst
->u
.tun_info
;
187 info
->mode
= IP_TUNNEL_INFO_IPV6
;
188 info
->key
.tun_flags
= flags
;
189 info
->key
.tun_id
= tunnel_id
;
190 info
->key
.tp_src
= 0;
191 info
->key
.tp_dst
= tp_dst
;
193 info
->key
.u
.ipv6
.src
= *saddr
;
194 info
->key
.u
.ipv6
.dst
= *daddr
;
198 info
->key
.label
= label
;
203 static inline struct metadata_dst
*ipv6_tun_rx_dst(struct sk_buff
*skb
,
208 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
210 return __ipv6_tun_set_dst(&ip6h
->saddr
, &ip6h
->daddr
,
211 ipv6_get_dsfield(ip6h
), ip6h
->hop_limit
,
212 0, ip6_flowlabel(ip6h
), flags
, tunnel_id
,
215 #endif /* __NET_DST_METADATA_H */