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