xref: /openbmc/linux/net/ipv4/ip_tunnel_core.c (revision 2f1d370b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2013 Nicira, Inc.
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/skbuff.h>
11 #include <linux/netdevice.h>
12 #include <linux/in.h>
13 #include <linux/if_arp.h>
14 #include <linux/init.h>
15 #include <linux/in6.h>
16 #include <linux/inetdevice.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <linux/etherdevice.h>
19 #include <linux/if_ether.h>
20 #include <linux/if_vlan.h>
21 #include <linux/static_key.h>
22 
23 #include <net/ip.h>
24 #include <net/icmp.h>
25 #include <net/protocol.h>
26 #include <net/ip_tunnels.h>
27 #include <net/ip6_tunnel.h>
28 #include <net/arp.h>
29 #include <net/checksum.h>
30 #include <net/dsfield.h>
31 #include <net/inet_ecn.h>
32 #include <net/xfrm.h>
33 #include <net/net_namespace.h>
34 #include <net/netns/generic.h>
35 #include <net/rtnetlink.h>
36 #include <net/dst_metadata.h>
37 #include <net/geneve.h>
38 #include <net/vxlan.h>
39 #include <net/erspan.h>
40 
41 const struct ip_tunnel_encap_ops __rcu *
42 		iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
43 EXPORT_SYMBOL(iptun_encaps);
44 
45 const struct ip6_tnl_encap_ops __rcu *
46 		ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
47 EXPORT_SYMBOL(ip6tun_encaps);
48 
49 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
50 		   __be32 src, __be32 dst, __u8 proto,
51 		   __u8 tos, __u8 ttl, __be16 df, bool xnet)
52 {
53 	int pkt_len = skb->len - skb_inner_network_offset(skb);
54 	struct net *net = dev_net(rt->dst.dev);
55 	struct net_device *dev = skb->dev;
56 	struct iphdr *iph;
57 	int err;
58 
59 	skb_scrub_packet(skb, xnet);
60 
61 	skb_clear_hash_if_not_l4(skb);
62 	skb_dst_set(skb, &rt->dst);
63 	memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
64 
65 	/* Push down and install the IP header. */
66 	skb_push(skb, sizeof(struct iphdr));
67 	skb_reset_network_header(skb);
68 
69 	iph = ip_hdr(skb);
70 
71 	iph->version	=	4;
72 	iph->ihl	=	sizeof(struct iphdr) >> 2;
73 	iph->frag_off	=	ip_mtu_locked(&rt->dst) ? 0 : df;
74 	iph->protocol	=	proto;
75 	iph->tos	=	tos;
76 	iph->daddr	=	dst;
77 	iph->saddr	=	src;
78 	iph->ttl	=	ttl;
79 	__ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
80 
81 	err = ip_local_out(net, sk, skb);
82 
83 	if (dev) {
84 		if (unlikely(net_xmit_eval(err)))
85 			pkt_len = 0;
86 		iptunnel_xmit_stats(dev, pkt_len);
87 	}
88 }
89 EXPORT_SYMBOL_GPL(iptunnel_xmit);
90 
91 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
92 			   __be16 inner_proto, bool raw_proto, bool xnet)
93 {
94 	if (unlikely(!pskb_may_pull(skb, hdr_len)))
95 		return -ENOMEM;
96 
97 	skb_pull_rcsum(skb, hdr_len);
98 
99 	if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
100 		struct ethhdr *eh;
101 
102 		if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
103 			return -ENOMEM;
104 
105 		eh = (struct ethhdr *)skb->data;
106 		if (likely(eth_proto_is_802_3(eh->h_proto)))
107 			skb->protocol = eh->h_proto;
108 		else
109 			skb->protocol = htons(ETH_P_802_2);
110 
111 	} else {
112 		skb->protocol = inner_proto;
113 	}
114 
115 	skb_clear_hash_if_not_l4(skb);
116 	__vlan_hwaccel_clear_tag(skb);
117 	skb_set_queue_mapping(skb, 0);
118 	skb_scrub_packet(skb, xnet);
119 
120 	return iptunnel_pull_offloads(skb);
121 }
122 EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
123 
124 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
125 					     gfp_t flags)
126 {
127 	struct metadata_dst *res;
128 	struct ip_tunnel_info *dst, *src;
129 
130 	if (!md || md->type != METADATA_IP_TUNNEL ||
131 	    md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
132 		return NULL;
133 
134 	src = &md->u.tun_info;
135 	res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
136 	if (!res)
137 		return NULL;
138 
139 	dst = &res->u.tun_info;
140 	dst->key.tun_id = src->key.tun_id;
141 	if (src->mode & IP_TUNNEL_INFO_IPV6)
142 		memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
143 		       sizeof(struct in6_addr));
144 	else
145 		dst->key.u.ipv4.dst = src->key.u.ipv4.src;
146 	dst->key.tun_flags = src->key.tun_flags;
147 	dst->mode = src->mode | IP_TUNNEL_INFO_TX;
148 	ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
149 				src->options_len, 0);
150 
151 	return res;
152 }
153 EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
154 
155 int iptunnel_handle_offloads(struct sk_buff *skb,
156 			     int gso_type_mask)
157 {
158 	int err;
159 
160 	if (likely(!skb->encapsulation)) {
161 		skb_reset_inner_headers(skb);
162 		skb->encapsulation = 1;
163 	}
164 
165 	if (skb_is_gso(skb)) {
166 		err = skb_header_unclone(skb, GFP_ATOMIC);
167 		if (unlikely(err))
168 			return err;
169 		skb_shinfo(skb)->gso_type |= gso_type_mask;
170 		return 0;
171 	}
172 
173 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
174 		skb->ip_summed = CHECKSUM_NONE;
175 		/* We clear encapsulation here to prevent badly-written
176 		 * drivers potentially deciding to offload an inner checksum
177 		 * if we set CHECKSUM_PARTIAL on the outer header.
178 		 * This should go away when the drivers are all fixed.
179 		 */
180 		skb->encapsulation = 0;
181 	}
182 
183 	return 0;
184 }
185 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
186 
187 /* Often modified stats are per cpu, other are shared (netdev->stats) */
188 void ip_tunnel_get_stats64(struct net_device *dev,
189 			   struct rtnl_link_stats64 *tot)
190 {
191 	int i;
192 
193 	netdev_stats_to_stats64(tot, &dev->stats);
194 
195 	for_each_possible_cpu(i) {
196 		const struct pcpu_sw_netstats *tstats =
197 						   per_cpu_ptr(dev->tstats, i);
198 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
199 		unsigned int start;
200 
201 		do {
202 			start = u64_stats_fetch_begin_irq(&tstats->syncp);
203 			rx_packets = tstats->rx_packets;
204 			tx_packets = tstats->tx_packets;
205 			rx_bytes = tstats->rx_bytes;
206 			tx_bytes = tstats->tx_bytes;
207 		} while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
208 
209 		tot->rx_packets += rx_packets;
210 		tot->tx_packets += tx_packets;
211 		tot->rx_bytes   += rx_bytes;
212 		tot->tx_bytes   += tx_bytes;
213 	}
214 }
215 EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
216 
217 static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
218 	[LWTUNNEL_IP_ID]	= { .type = NLA_U64 },
219 	[LWTUNNEL_IP_DST]	= { .type = NLA_U32 },
220 	[LWTUNNEL_IP_SRC]	= { .type = NLA_U32 },
221 	[LWTUNNEL_IP_TTL]	= { .type = NLA_U8 },
222 	[LWTUNNEL_IP_TOS]	= { .type = NLA_U8 },
223 	[LWTUNNEL_IP_FLAGS]	= { .type = NLA_U16 },
224 	[LWTUNNEL_IP_OPTS]	= { .type = NLA_NESTED },
225 };
226 
227 static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = {
228 	[LWTUNNEL_IP_OPTS_GENEVE]	= { .type = NLA_NESTED },
229 	[LWTUNNEL_IP_OPTS_VXLAN]	= { .type = NLA_NESTED },
230 	[LWTUNNEL_IP_OPTS_ERSPAN]	= { .type = NLA_NESTED },
231 };
232 
233 static const struct nla_policy
234 geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = {
235 	[LWTUNNEL_IP_OPT_GENEVE_CLASS]	= { .type = NLA_U16 },
236 	[LWTUNNEL_IP_OPT_GENEVE_TYPE]	= { .type = NLA_U8 },
237 	[LWTUNNEL_IP_OPT_GENEVE_DATA]	= { .type = NLA_BINARY, .len = 128 },
238 };
239 
240 static const struct nla_policy
241 vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = {
242 	[LWTUNNEL_IP_OPT_VXLAN_GBP]	= { .type = NLA_U32 },
243 };
244 
245 static const struct nla_policy
246 erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = {
247 	[LWTUNNEL_IP_OPT_ERSPAN_VER]	= { .type = NLA_U8 },
248 	[LWTUNNEL_IP_OPT_ERSPAN_INDEX]	= { .type = NLA_U32 },
249 	[LWTUNNEL_IP_OPT_ERSPAN_DIR]	= { .type = NLA_U8 },
250 	[LWTUNNEL_IP_OPT_ERSPAN_HWID]	= { .type = NLA_U8 },
251 };
252 
253 static int ip_tun_parse_opts_geneve(struct nlattr *attr,
254 				    struct ip_tunnel_info *info, int opts_len,
255 				    struct netlink_ext_ack *extack)
256 {
257 	struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
258 	int data_len, err;
259 
260 	err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr,
261 			       geneve_opt_policy, extack);
262 	if (err)
263 		return err;
264 
265 	if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] ||
266 	    !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] ||
267 	    !tb[LWTUNNEL_IP_OPT_GENEVE_DATA])
268 		return -EINVAL;
269 
270 	attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA];
271 	data_len = nla_len(attr);
272 	if (data_len % 4)
273 		return -EINVAL;
274 
275 	if (info) {
276 		struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len;
277 
278 		memcpy(opt->opt_data, nla_data(attr), data_len);
279 		opt->length = data_len / 4;
280 		attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS];
281 		opt->opt_class = nla_get_be16(attr);
282 		attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE];
283 		opt->type = nla_get_u8(attr);
284 		info->key.tun_flags |= TUNNEL_GENEVE_OPT;
285 	}
286 
287 	return sizeof(struct geneve_opt) + data_len;
288 }
289 
290 static int ip_tun_parse_opts_vxlan(struct nlattr *attr,
291 				   struct ip_tunnel_info *info, int opts_len,
292 				   struct netlink_ext_ack *extack)
293 {
294 	struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1];
295 	int err;
296 
297 	err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr,
298 			       vxlan_opt_policy, extack);
299 	if (err)
300 		return err;
301 
302 	if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP])
303 		return -EINVAL;
304 
305 	if (info) {
306 		struct vxlan_metadata *md =
307 			ip_tunnel_info_opts(info) + opts_len;
308 
309 		attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP];
310 		md->gbp = nla_get_u32(attr);
311 		info->key.tun_flags |= TUNNEL_VXLAN_OPT;
312 	}
313 
314 	return sizeof(struct vxlan_metadata);
315 }
316 
317 static int ip_tun_parse_opts_erspan(struct nlattr *attr,
318 				    struct ip_tunnel_info *info, int opts_len,
319 				    struct netlink_ext_ack *extack)
320 {
321 	struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
322 	int err;
323 
324 	err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr,
325 			       erspan_opt_policy, extack);
326 	if (err)
327 		return err;
328 
329 	if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER])
330 		return -EINVAL;
331 
332 	if (info) {
333 		struct erspan_metadata *md =
334 			ip_tunnel_info_opts(info) + opts_len;
335 
336 		attr = tb[LWTUNNEL_IP_OPT_ERSPAN_VER];
337 		md->version = nla_get_u8(attr);
338 
339 		if (md->version == 1 && tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX]) {
340 			attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX];
341 			md->u.index = nla_get_be32(attr);
342 		} else if (md->version == 2 && tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] &&
343 			   tb[LWTUNNEL_IP_OPT_ERSPAN_HWID]) {
344 			attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR];
345 			md->u.md2.dir = nla_get_u8(attr);
346 			attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID];
347 			set_hwid(&md->u.md2, nla_get_u8(attr));
348 		} else {
349 			return -EINVAL;
350 		}
351 
352 		info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
353 	}
354 
355 	return sizeof(struct erspan_metadata);
356 }
357 
358 static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info,
359 			     struct netlink_ext_ack *extack)
360 {
361 	int err, rem, opt_len, opts_len = 0, type = 0;
362 	struct nlattr *nla;
363 
364 	if (!attr)
365 		return 0;
366 
367 	err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX,
368 			   ip_opts_policy, extack);
369 	if (err)
370 		return err;
371 
372 	nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) {
373 		switch (nla_type(nla)) {
374 		case LWTUNNEL_IP_OPTS_GENEVE:
375 			if (type && type != TUNNEL_GENEVE_OPT)
376 				return -EINVAL;
377 			opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len,
378 							   extack);
379 			if (opt_len < 0)
380 				return opt_len;
381 			opts_len += opt_len;
382 			if (opts_len > IP_TUNNEL_OPTS_MAX)
383 				return -EINVAL;
384 			type = TUNNEL_GENEVE_OPT;
385 			break;
386 		case LWTUNNEL_IP_OPTS_VXLAN:
387 			if (type)
388 				return -EINVAL;
389 			opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len,
390 							  extack);
391 			if (opt_len < 0)
392 				return opt_len;
393 			opts_len += opt_len;
394 			type = TUNNEL_VXLAN_OPT;
395 			break;
396 		case LWTUNNEL_IP_OPTS_ERSPAN:
397 			if (type)
398 				return -EINVAL;
399 			opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len,
400 							   extack);
401 			if (opt_len < 0)
402 				return opt_len;
403 			opts_len += opt_len;
404 			type = TUNNEL_ERSPAN_OPT;
405 			break;
406 		default:
407 			return -EINVAL;
408 		}
409 	}
410 
411 	return opts_len;
412 }
413 
414 static int ip_tun_get_optlen(struct nlattr *attr,
415 			     struct netlink_ext_ack *extack)
416 {
417 	return ip_tun_parse_opts(attr, NULL, extack);
418 }
419 
420 static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info,
421 			   struct netlink_ext_ack *extack)
422 {
423 	return ip_tun_parse_opts(attr, info, extack);
424 }
425 
426 static int ip_tun_build_state(struct nlattr *attr,
427 			      unsigned int family, const void *cfg,
428 			      struct lwtunnel_state **ts,
429 			      struct netlink_ext_ack *extack)
430 {
431 	struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
432 	struct lwtunnel_state *new_state;
433 	struct ip_tunnel_info *tun_info;
434 	int err, opt_len;
435 
436 	err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
437 					  ip_tun_policy, extack);
438 	if (err < 0)
439 		return err;
440 
441 	opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack);
442 	if (opt_len < 0)
443 		return opt_len;
444 
445 	new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
446 	if (!new_state)
447 		return -ENOMEM;
448 
449 	new_state->type = LWTUNNEL_ENCAP_IP;
450 
451 	tun_info = lwt_tun_info(new_state);
452 
453 	err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack);
454 	if (err < 0) {
455 		lwtstate_free(new_state);
456 		return err;
457 	}
458 
459 #ifdef CONFIG_DST_CACHE
460 	err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
461 	if (err) {
462 		lwtstate_free(new_state);
463 		return err;
464 	}
465 #endif
466 
467 	if (tb[LWTUNNEL_IP_ID])
468 		tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
469 
470 	if (tb[LWTUNNEL_IP_DST])
471 		tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
472 
473 	if (tb[LWTUNNEL_IP_SRC])
474 		tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
475 
476 	if (tb[LWTUNNEL_IP_TTL])
477 		tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
478 
479 	if (tb[LWTUNNEL_IP_TOS])
480 		tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
481 
482 	if (tb[LWTUNNEL_IP_FLAGS])
483 		tun_info->key.tun_flags |=
484 				(nla_get_be16(tb[LWTUNNEL_IP_FLAGS]) &
485 				 ~TUNNEL_OPTIONS_PRESENT);
486 
487 	tun_info->mode = IP_TUNNEL_INFO_TX;
488 	tun_info->options_len = opt_len;
489 
490 	*ts = new_state;
491 
492 	return 0;
493 }
494 
495 static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
496 {
497 #ifdef CONFIG_DST_CACHE
498 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
499 
500 	dst_cache_destroy(&tun_info->dst_cache);
501 #endif
502 }
503 
504 static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb,
505 					 struct ip_tunnel_info *tun_info)
506 {
507 	struct geneve_opt *opt;
508 	struct nlattr *nest;
509 	int offset = 0;
510 
511 	nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE);
512 	if (!nest)
513 		return -ENOMEM;
514 
515 	while (tun_info->options_len > offset) {
516 		opt = ip_tunnel_info_opts(tun_info) + offset;
517 		if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS,
518 				 opt->opt_class) ||
519 		    nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) ||
520 		    nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4,
521 			    opt->opt_data)) {
522 			nla_nest_cancel(skb, nest);
523 			return -ENOMEM;
524 		}
525 		offset += sizeof(*opt) + opt->length * 4;
526 	}
527 
528 	nla_nest_end(skb, nest);
529 	return 0;
530 }
531 
532 static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb,
533 					struct ip_tunnel_info *tun_info)
534 {
535 	struct vxlan_metadata *md;
536 	struct nlattr *nest;
537 
538 	nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN);
539 	if (!nest)
540 		return -ENOMEM;
541 
542 	md = ip_tunnel_info_opts(tun_info);
543 	if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) {
544 		nla_nest_cancel(skb, nest);
545 		return -ENOMEM;
546 	}
547 
548 	nla_nest_end(skb, nest);
549 	return 0;
550 }
551 
552 static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb,
553 					 struct ip_tunnel_info *tun_info)
554 {
555 	struct erspan_metadata *md;
556 	struct nlattr *nest;
557 
558 	nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN);
559 	if (!nest)
560 		return -ENOMEM;
561 
562 	md = ip_tunnel_info_opts(tun_info);
563 	if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version))
564 		goto err;
565 
566 	if (md->version == 1 &&
567 	    nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index))
568 		goto err;
569 
570 	if (md->version == 2 &&
571 	    (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) ||
572 	     nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID,
573 			get_hwid(&md->u.md2))))
574 		goto err;
575 
576 	nla_nest_end(skb, nest);
577 	return 0;
578 err:
579 	nla_nest_cancel(skb, nest);
580 	return -ENOMEM;
581 }
582 
583 static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type,
584 				  struct ip_tunnel_info *tun_info)
585 {
586 	struct nlattr *nest;
587 	int err = 0;
588 
589 	if (!(tun_info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
590 		return 0;
591 
592 	nest = nla_nest_start_noflag(skb, type);
593 	if (!nest)
594 		return -ENOMEM;
595 
596 	if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT)
597 		err = ip_tun_fill_encap_opts_geneve(skb, tun_info);
598 	else if (tun_info->key.tun_flags & TUNNEL_VXLAN_OPT)
599 		err = ip_tun_fill_encap_opts_vxlan(skb, tun_info);
600 	else if (tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT)
601 		err = ip_tun_fill_encap_opts_erspan(skb, tun_info);
602 
603 	if (err) {
604 		nla_nest_cancel(skb, nest);
605 		return err;
606 	}
607 
608 	nla_nest_end(skb, nest);
609 	return 0;
610 }
611 
612 static int ip_tun_fill_encap_info(struct sk_buff *skb,
613 				  struct lwtunnel_state *lwtstate)
614 {
615 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
616 
617 	if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
618 			 LWTUNNEL_IP_PAD) ||
619 	    nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
620 	    nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
621 	    nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
622 	    nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
623 	    nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) ||
624 	    ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info))
625 		return -ENOMEM;
626 
627 	return 0;
628 }
629 
630 static int ip_tun_opts_nlsize(struct ip_tunnel_info *info)
631 {
632 	int opt_len;
633 
634 	if (!(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
635 		return 0;
636 
637 	opt_len = nla_total_size(0);		/* LWTUNNEL_IP_OPTS */
638 	if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
639 		struct geneve_opt *opt;
640 		int offset = 0;
641 
642 		opt_len += nla_total_size(0);	/* LWTUNNEL_IP_OPTS_GENEVE */
643 		while (info->options_len > offset) {
644 			opt = ip_tunnel_info_opts(info) + offset;
645 			opt_len += nla_total_size(2)	/* OPT_GENEVE_CLASS */
646 				   + nla_total_size(1)	/* OPT_GENEVE_TYPE */
647 				   + nla_total_size(opt->length * 4);
648 							/* OPT_GENEVE_DATA */
649 			offset += sizeof(*opt) + opt->length * 4;
650 		}
651 	} else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
652 		opt_len += nla_total_size(0)	/* LWTUNNEL_IP_OPTS_VXLAN */
653 			   + nla_total_size(4);	/* OPT_VXLAN_GBP */
654 	} else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) {
655 		struct erspan_metadata *md = ip_tunnel_info_opts(info);
656 
657 		opt_len += nla_total_size(0)	/* LWTUNNEL_IP_OPTS_ERSPAN */
658 			   + nla_total_size(1)	/* OPT_ERSPAN_VER */
659 			   + (md->version == 1 ? nla_total_size(4)
660 						/* OPT_ERSPAN_INDEX (v1) */
661 					       : nla_total_size(1) +
662 						 nla_total_size(1));
663 						/* OPT_ERSPAN_DIR + HWID (v2) */
664 	}
665 
666 	return opt_len;
667 }
668 
669 static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
670 {
671 	return nla_total_size_64bit(8)	/* LWTUNNEL_IP_ID */
672 		+ nla_total_size(4)	/* LWTUNNEL_IP_DST */
673 		+ nla_total_size(4)	/* LWTUNNEL_IP_SRC */
674 		+ nla_total_size(1)	/* LWTUNNEL_IP_TOS */
675 		+ nla_total_size(1)	/* LWTUNNEL_IP_TTL */
676 		+ nla_total_size(2)	/* LWTUNNEL_IP_FLAGS */
677 		+ ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
678 					/* LWTUNNEL_IP_OPTS */
679 }
680 
681 static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
682 {
683 	struct ip_tunnel_info *info_a = lwt_tun_info(a);
684 	struct ip_tunnel_info *info_b = lwt_tun_info(b);
685 
686 	return memcmp(info_a, info_b, sizeof(info_a->key)) ||
687 	       info_a->mode != info_b->mode ||
688 	       info_a->options_len != info_b->options_len ||
689 	       memcmp(ip_tunnel_info_opts(info_a),
690 		      ip_tunnel_info_opts(info_b), info_a->options_len);
691 }
692 
693 static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
694 	.build_state = ip_tun_build_state,
695 	.destroy_state = ip_tun_destroy_state,
696 	.fill_encap = ip_tun_fill_encap_info,
697 	.get_encap_size = ip_tun_encap_nlsize,
698 	.cmp_encap = ip_tun_cmp_encap,
699 	.owner = THIS_MODULE,
700 };
701 
702 static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
703 	[LWTUNNEL_IP6_ID]		= { .type = NLA_U64 },
704 	[LWTUNNEL_IP6_DST]		= { .len = sizeof(struct in6_addr) },
705 	[LWTUNNEL_IP6_SRC]		= { .len = sizeof(struct in6_addr) },
706 	[LWTUNNEL_IP6_HOPLIMIT]		= { .type = NLA_U8 },
707 	[LWTUNNEL_IP6_TC]		= { .type = NLA_U8 },
708 	[LWTUNNEL_IP6_FLAGS]		= { .type = NLA_U16 },
709 };
710 
711 static int ip6_tun_build_state(struct nlattr *attr,
712 			       unsigned int family, const void *cfg,
713 			       struct lwtunnel_state **ts,
714 			       struct netlink_ext_ack *extack)
715 {
716 	struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
717 	struct lwtunnel_state *new_state;
718 	struct ip_tunnel_info *tun_info;
719 	int err, opt_len;
720 
721 	err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
722 					  ip6_tun_policy, extack);
723 	if (err < 0)
724 		return err;
725 
726 	opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack);
727 	if (opt_len < 0)
728 		return opt_len;
729 
730 	new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
731 	if (!new_state)
732 		return -ENOMEM;
733 
734 	new_state->type = LWTUNNEL_ENCAP_IP6;
735 
736 	tun_info = lwt_tun_info(new_state);
737 
738 	err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack);
739 	if (err < 0) {
740 		lwtstate_free(new_state);
741 		return err;
742 	}
743 
744 	if (tb[LWTUNNEL_IP6_ID])
745 		tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
746 
747 	if (tb[LWTUNNEL_IP6_DST])
748 		tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
749 
750 	if (tb[LWTUNNEL_IP6_SRC])
751 		tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
752 
753 	if (tb[LWTUNNEL_IP6_HOPLIMIT])
754 		tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
755 
756 	if (tb[LWTUNNEL_IP6_TC])
757 		tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
758 
759 	if (tb[LWTUNNEL_IP6_FLAGS])
760 		tun_info->key.tun_flags |=
761 				(nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]) &
762 				 ~TUNNEL_OPTIONS_PRESENT);
763 
764 	tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
765 	tun_info->options_len = opt_len;
766 
767 	*ts = new_state;
768 
769 	return 0;
770 }
771 
772 static int ip6_tun_fill_encap_info(struct sk_buff *skb,
773 				   struct lwtunnel_state *lwtstate)
774 {
775 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
776 
777 	if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
778 			 LWTUNNEL_IP6_PAD) ||
779 	    nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
780 	    nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
781 	    nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
782 	    nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
783 	    nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) ||
784 	    ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info))
785 		return -ENOMEM;
786 
787 	return 0;
788 }
789 
790 static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
791 {
792 	return nla_total_size_64bit(8)	/* LWTUNNEL_IP6_ID */
793 		+ nla_total_size(16)	/* LWTUNNEL_IP6_DST */
794 		+ nla_total_size(16)	/* LWTUNNEL_IP6_SRC */
795 		+ nla_total_size(1)	/* LWTUNNEL_IP6_HOPLIMIT */
796 		+ nla_total_size(1)	/* LWTUNNEL_IP6_TC */
797 		+ nla_total_size(2)	/* LWTUNNEL_IP6_FLAGS */
798 		+ ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
799 					/* LWTUNNEL_IP6_OPTS */
800 }
801 
802 static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
803 	.build_state = ip6_tun_build_state,
804 	.fill_encap = ip6_tun_fill_encap_info,
805 	.get_encap_size = ip6_tun_encap_nlsize,
806 	.cmp_encap = ip_tun_cmp_encap,
807 	.owner = THIS_MODULE,
808 };
809 
810 void __init ip_tunnel_core_init(void)
811 {
812 	/* If you land here, make sure whether increasing ip_tunnel_info's
813 	 * options_len is a reasonable choice with its usage in front ends
814 	 * (f.e., it's part of flow keys, etc).
815 	 */
816 	BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
817 
818 	lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
819 	lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
820 }
821 
822 DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
823 EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
824 
825 void ip_tunnel_need_metadata(void)
826 {
827 	static_branch_inc(&ip_tunnel_metadata_cnt);
828 }
829 EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
830 
831 void ip_tunnel_unneed_metadata(void)
832 {
833 	static_branch_dec(&ip_tunnel_metadata_cnt);
834 }
835 EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);
836