xref: /openbmc/linux/include/net/dst_metadata.h (revision 2596e07a)
1 #ifndef __NET_DST_METADATA_H
2 #define __NET_DST_METADATA_H 1
3 
4 #include <linux/skbuff.h>
5 #include <net/ip_tunnels.h>
6 #include <net/dst.h>
7 
8 struct metadata_dst {
9 	struct dst_entry		dst;
10 	union {
11 		struct ip_tunnel_info	tun_info;
12 	} u;
13 };
14 
15 static inline struct metadata_dst *skb_metadata_dst(struct sk_buff *skb)
16 {
17 	struct metadata_dst *md_dst = (struct metadata_dst *) skb_dst(skb);
18 
19 	if (md_dst && md_dst->dst.flags & DST_METADATA)
20 		return md_dst;
21 
22 	return NULL;
23 }
24 
25 static inline struct ip_tunnel_info *skb_tunnel_info(struct sk_buff *skb)
26 {
27 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
28 	struct dst_entry *dst;
29 
30 	if (md_dst)
31 		return &md_dst->u.tun_info;
32 
33 	dst = skb_dst(skb);
34 	if (dst && dst->lwtstate)
35 		return lwt_tun_info(dst->lwtstate);
36 
37 	return NULL;
38 }
39 
40 static inline bool skb_valid_dst(const struct sk_buff *skb)
41 {
42 	struct dst_entry *dst = skb_dst(skb);
43 
44 	return dst && !(dst->flags & DST_METADATA);
45 }
46 
47 static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
48 				       const struct sk_buff *skb_b)
49 {
50 	const struct metadata_dst *a, *b;
51 
52 	if (!(skb_a->_skb_refdst | skb_b->_skb_refdst))
53 		return 0;
54 
55 	a = (const struct metadata_dst *) skb_dst(skb_a);
56 	b = (const struct metadata_dst *) skb_dst(skb_b);
57 
58 	if (!a != !b || a->u.tun_info.options_len != b->u.tun_info.options_len)
59 		return 1;
60 
61 	return memcmp(&a->u.tun_info, &b->u.tun_info,
62 		      sizeof(a->u.tun_info) + a->u.tun_info.options_len);
63 }
64 
65 struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags);
66 struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags);
67 
68 static inline struct metadata_dst *tun_rx_dst(int md_size)
69 {
70 	struct metadata_dst *tun_dst;
71 
72 	tun_dst = metadata_dst_alloc(md_size, GFP_ATOMIC);
73 	if (!tun_dst)
74 		return NULL;
75 
76 	tun_dst->u.tun_info.options_len = 0;
77 	tun_dst->u.tun_info.mode = 0;
78 	return tun_dst;
79 }
80 
81 static inline struct metadata_dst *tun_dst_unclone(struct sk_buff *skb)
82 {
83 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
84 	int md_size;
85 	struct metadata_dst *new_md;
86 
87 	if (!md_dst)
88 		return ERR_PTR(-EINVAL);
89 
90 	md_size = md_dst->u.tun_info.options_len;
91 	new_md = metadata_dst_alloc(md_size, GFP_ATOMIC);
92 	if (!new_md)
93 		return ERR_PTR(-ENOMEM);
94 
95 	memcpy(&new_md->u.tun_info, &md_dst->u.tun_info,
96 	       sizeof(struct ip_tunnel_info) + md_size);
97 	skb_dst_drop(skb);
98 	dst_hold(&new_md->dst);
99 	skb_dst_set(skb, &new_md->dst);
100 	return new_md;
101 }
102 
103 static inline struct ip_tunnel_info *skb_tunnel_info_unclone(struct sk_buff *skb)
104 {
105 	struct metadata_dst *dst;
106 
107 	dst = tun_dst_unclone(skb);
108 	if (IS_ERR(dst))
109 		return NULL;
110 
111 	return &dst->u.tun_info;
112 }
113 
114 static inline struct metadata_dst *ip_tun_rx_dst(struct sk_buff *skb,
115 						 __be16 flags,
116 						 __be64 tunnel_id,
117 						 int md_size)
118 {
119 	const struct iphdr *iph = ip_hdr(skb);
120 	struct metadata_dst *tun_dst;
121 
122 	tun_dst = tun_rx_dst(md_size);
123 	if (!tun_dst)
124 		return NULL;
125 
126 	ip_tunnel_key_init(&tun_dst->u.tun_info.key,
127 			   iph->saddr, iph->daddr, iph->tos, iph->ttl,
128 			   0, 0, tunnel_id, flags);
129 	return tun_dst;
130 }
131 
132 static inline struct metadata_dst *ipv6_tun_rx_dst(struct sk_buff *skb,
133 						 __be16 flags,
134 						 __be64 tunnel_id,
135 						 int md_size)
136 {
137 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
138 	struct metadata_dst *tun_dst;
139 	struct ip_tunnel_info *info;
140 
141 	tun_dst = tun_rx_dst(md_size);
142 	if (!tun_dst)
143 		return NULL;
144 
145 	info = &tun_dst->u.tun_info;
146 	info->mode = IP_TUNNEL_INFO_IPV6;
147 	info->key.tun_flags = flags;
148 	info->key.tun_id = tunnel_id;
149 	info->key.tp_src = 0;
150 	info->key.tp_dst = 0;
151 
152 	info->key.u.ipv6.src = ip6h->saddr;
153 	info->key.u.ipv6.dst = ip6h->daddr;
154 	info->key.tos = ipv6_get_dsfield(ip6h);
155 	info->key.ttl = ip6h->hop_limit;
156 	return tun_dst;
157 }
158 
159 #endif /* __NET_DST_METADATA_H */
160