xref: /openbmc/linux/net/ipv4/ip_tunnel_core.c (revision 06a7a37b)
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/ip6_checksum.h>
29 #include <net/arp.h>
30 #include <net/checksum.h>
31 #include <net/dsfield.h>
32 #include <net/inet_ecn.h>
33 #include <net/xfrm.h>
34 #include <net/net_namespace.h>
35 #include <net/netns/generic.h>
36 #include <net/rtnetlink.h>
37 #include <net/dst_metadata.h>
38 #include <net/geneve.h>
39 #include <net/vxlan.h>
40 #include <net/erspan.h>
41 #include <net/ip6_checksum.h>
42 
43 const struct ip_tunnel_encap_ops __rcu *
44 		iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
45 EXPORT_SYMBOL(iptun_encaps);
46 
47 const struct ip6_tnl_encap_ops __rcu *
48 		ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
49 EXPORT_SYMBOL(ip6tun_encaps);
50 
51 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
52 		   __be32 src, __be32 dst, __u8 proto,
53 		   __u8 tos, __u8 ttl, __be16 df, bool xnet)
54 {
55 	int pkt_len = skb->len - skb_inner_network_offset(skb);
56 	struct net *net = dev_net(rt->dst.dev);
57 	struct net_device *dev = skb->dev;
58 	struct iphdr *iph;
59 	int err;
60 
61 	skb_scrub_packet(skb, xnet);
62 
63 	skb_clear_hash_if_not_l4(skb);
64 	skb_dst_set(skb, &rt->dst);
65 	memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
66 
67 	/* Push down and install the IP header. */
68 	skb_push(skb, sizeof(struct iphdr));
69 	skb_reset_network_header(skb);
70 
71 	iph = ip_hdr(skb);
72 
73 	iph->version	=	4;
74 	iph->ihl	=	sizeof(struct iphdr) >> 2;
75 	iph->frag_off	=	ip_mtu_locked(&rt->dst) ? 0 : df;
76 	iph->protocol	=	proto;
77 	iph->tos	=	tos;
78 	iph->daddr	=	dst;
79 	iph->saddr	=	src;
80 	iph->ttl	=	ttl;
81 	__ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
82 
83 	err = ip_local_out(net, sk, skb);
84 
85 	if (dev) {
86 		if (unlikely(net_xmit_eval(err)))
87 			pkt_len = 0;
88 		iptunnel_xmit_stats(dev, pkt_len);
89 	}
90 }
91 EXPORT_SYMBOL_GPL(iptunnel_xmit);
92 
93 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
94 			   __be16 inner_proto, bool raw_proto, bool xnet)
95 {
96 	if (unlikely(!pskb_may_pull(skb, hdr_len)))
97 		return -ENOMEM;
98 
99 	skb_pull_rcsum(skb, hdr_len);
100 
101 	if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
102 		struct ethhdr *eh;
103 
104 		if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
105 			return -ENOMEM;
106 
107 		eh = (struct ethhdr *)skb->data;
108 		if (likely(eth_proto_is_802_3(eh->h_proto)))
109 			skb->protocol = eh->h_proto;
110 		else
111 			skb->protocol = htons(ETH_P_802_2);
112 
113 	} else {
114 		skb->protocol = inner_proto;
115 	}
116 
117 	skb_clear_hash_if_not_l4(skb);
118 	__vlan_hwaccel_clear_tag(skb);
119 	skb_set_queue_mapping(skb, 0);
120 	skb_scrub_packet(skb, xnet);
121 
122 	return iptunnel_pull_offloads(skb);
123 }
124 EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
125 
126 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
127 					     gfp_t flags)
128 {
129 	struct metadata_dst *res;
130 	struct ip_tunnel_info *dst, *src;
131 
132 	if (!md || md->type != METADATA_IP_TUNNEL ||
133 	    md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
134 		return NULL;
135 
136 	src = &md->u.tun_info;
137 	res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
138 	if (!res)
139 		return NULL;
140 
141 	dst = &res->u.tun_info;
142 	dst->key.tun_id = src->key.tun_id;
143 	if (src->mode & IP_TUNNEL_INFO_IPV6)
144 		memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
145 		       sizeof(struct in6_addr));
146 	else
147 		dst->key.u.ipv4.dst = src->key.u.ipv4.src;
148 	dst->key.tun_flags = src->key.tun_flags;
149 	dst->mode = src->mode | IP_TUNNEL_INFO_TX;
150 	ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
151 				src->options_len, 0);
152 
153 	return res;
154 }
155 EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
156 
157 int iptunnel_handle_offloads(struct sk_buff *skb,
158 			     int gso_type_mask)
159 {
160 	int err;
161 
162 	if (likely(!skb->encapsulation)) {
163 		skb_reset_inner_headers(skb);
164 		skb->encapsulation = 1;
165 	}
166 
167 	if (skb_is_gso(skb)) {
168 		err = skb_header_unclone(skb, GFP_ATOMIC);
169 		if (unlikely(err))
170 			return err;
171 		skb_shinfo(skb)->gso_type |= gso_type_mask;
172 		return 0;
173 	}
174 
175 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
176 		skb->ip_summed = CHECKSUM_NONE;
177 		/* We clear encapsulation here to prevent badly-written
178 		 * drivers potentially deciding to offload an inner checksum
179 		 * if we set CHECKSUM_PARTIAL on the outer header.
180 		 * This should go away when the drivers are all fixed.
181 		 */
182 		skb->encapsulation = 0;
183 	}
184 
185 	return 0;
186 }
187 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
188 
189 /**
190  * iptunnel_pmtud_build_icmp() - Build ICMP error message for PMTUD
191  * @skb:	Original packet with L2 header
192  * @mtu:	MTU value for ICMP error
193  *
194  * Return: length on success, negative error code if message couldn't be built.
195  */
196 static int iptunnel_pmtud_build_icmp(struct sk_buff *skb, int mtu)
197 {
198 	const struct iphdr *iph = ip_hdr(skb);
199 	struct icmphdr *icmph;
200 	struct iphdr *niph;
201 	struct ethhdr eh;
202 	int len, err;
203 
204 	if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
205 		return -EINVAL;
206 
207 	skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
208 	pskb_pull(skb, ETH_HLEN);
209 	skb_reset_network_header(skb);
210 
211 	err = pskb_trim(skb, 576 - sizeof(*niph) - sizeof(*icmph));
212 	if (err)
213 		return err;
214 
215 	len = skb->len + sizeof(*icmph);
216 	err = skb_cow(skb, sizeof(*niph) + sizeof(*icmph) + ETH_HLEN);
217 	if (err)
218 		return err;
219 
220 	icmph = skb_push(skb, sizeof(*icmph));
221 	*icmph = (struct icmphdr) {
222 		.type			= ICMP_DEST_UNREACH,
223 		.code			= ICMP_FRAG_NEEDED,
224 		.checksum		= 0,
225 		.un.frag.__unused	= 0,
226 		.un.frag.mtu		= ntohs(mtu),
227 	};
228 	icmph->checksum = ip_compute_csum(icmph, len);
229 	skb_reset_transport_header(skb);
230 
231 	niph = skb_push(skb, sizeof(*niph));
232 	*niph = (struct iphdr) {
233 		.ihl			= sizeof(*niph) / 4u,
234 		.version 		= 4,
235 		.tos 			= 0,
236 		.tot_len		= htons(len + sizeof(*niph)),
237 		.id			= 0,
238 		.frag_off		= htons(IP_DF),
239 		.ttl			= iph->ttl,
240 		.protocol		= IPPROTO_ICMP,
241 		.saddr			= iph->daddr,
242 		.daddr			= iph->saddr,
243 	};
244 	ip_send_check(niph);
245 	skb_reset_network_header(skb);
246 
247 	skb->ip_summed = CHECKSUM_NONE;
248 
249 	eth_header(skb, skb->dev, htons(eh.h_proto), eh.h_source, eh.h_dest, 0);
250 	skb_reset_mac_header(skb);
251 
252 	return skb->len;
253 }
254 
255 /**
256  * iptunnel_pmtud_check_icmp() - Trigger ICMP reply if needed and allowed
257  * @skb:	Buffer being sent by encapsulation, L2 headers expected
258  * @mtu:	Network MTU for path
259  *
260  * Return: 0 for no ICMP reply, length if built, negative value on error.
261  */
262 static int iptunnel_pmtud_check_icmp(struct sk_buff *skb, int mtu)
263 {
264 	const struct icmphdr *icmph = icmp_hdr(skb);
265 	const struct iphdr *iph = ip_hdr(skb);
266 
267 	if (mtu <= 576 || iph->frag_off != htons(IP_DF))
268 		return 0;
269 
270 	if (ipv4_is_lbcast(iph->daddr)  || ipv4_is_multicast(iph->daddr) ||
271 	    ipv4_is_zeronet(iph->saddr) || ipv4_is_loopback(iph->saddr)  ||
272 	    ipv4_is_lbcast(iph->saddr)  || ipv4_is_multicast(iph->saddr))
273 		return 0;
274 
275 	if (iph->protocol == IPPROTO_ICMP && icmp_is_err(icmph->type))
276 		return 0;
277 
278 	return iptunnel_pmtud_build_icmp(skb, mtu);
279 }
280 
281 #if IS_ENABLED(CONFIG_IPV6)
282 /**
283  * iptunnel_pmtud_build_icmpv6() - Build ICMPv6 error message for PMTUD
284  * @skb:	Original packet with L2 header
285  * @mtu:	MTU value for ICMPv6 error
286  *
287  * Return: length on success, negative error code if message couldn't be built.
288  */
289 static int iptunnel_pmtud_build_icmpv6(struct sk_buff *skb, int mtu)
290 {
291 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
292 	struct icmp6hdr *icmp6h;
293 	struct ipv6hdr *nip6h;
294 	struct ethhdr eh;
295 	int len, err;
296 	__wsum csum;
297 
298 	if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
299 		return -EINVAL;
300 
301 	skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
302 	pskb_pull(skb, ETH_HLEN);
303 	skb_reset_network_header(skb);
304 
305 	err = pskb_trim(skb, IPV6_MIN_MTU - sizeof(*nip6h) - sizeof(*icmp6h));
306 	if (err)
307 		return err;
308 
309 	len = skb->len + sizeof(*icmp6h);
310 	err = skb_cow(skb, sizeof(*nip6h) + sizeof(*icmp6h) + ETH_HLEN);
311 	if (err)
312 		return err;
313 
314 	icmp6h = skb_push(skb, sizeof(*icmp6h));
315 	*icmp6h = (struct icmp6hdr) {
316 		.icmp6_type		= ICMPV6_PKT_TOOBIG,
317 		.icmp6_code		= 0,
318 		.icmp6_cksum		= 0,
319 		.icmp6_mtu		= htonl(mtu),
320 	};
321 	skb_reset_transport_header(skb);
322 
323 	nip6h = skb_push(skb, sizeof(*nip6h));
324 	*nip6h = (struct ipv6hdr) {
325 		.priority		= 0,
326 		.version		= 6,
327 		.flow_lbl		= { 0 },
328 		.payload_len		= htons(len),
329 		.nexthdr		= IPPROTO_ICMPV6,
330 		.hop_limit		= ip6h->hop_limit,
331 		.saddr			= ip6h->daddr,
332 		.daddr			= ip6h->saddr,
333 	};
334 	skb_reset_network_header(skb);
335 
336 	csum = csum_partial(icmp6h, len, 0);
337 	icmp6h->icmp6_cksum = csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr, len,
338 					      IPPROTO_ICMPV6, csum);
339 
340 	skb->ip_summed = CHECKSUM_NONE;
341 
342 	eth_header(skb, skb->dev, htons(eh.h_proto), eh.h_source, eh.h_dest, 0);
343 	skb_reset_mac_header(skb);
344 
345 	return skb->len;
346 }
347 
348 /**
349  * iptunnel_pmtud_check_icmpv6() - Trigger ICMPv6 reply if needed and allowed
350  * @skb:	Buffer being sent by encapsulation, L2 headers expected
351  * @mtu:	Network MTU for path
352  *
353  * Return: 0 for no ICMPv6 reply, length if built, negative value on error.
354  */
355 static int iptunnel_pmtud_check_icmpv6(struct sk_buff *skb, int mtu)
356 {
357 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
358 	int stype = ipv6_addr_type(&ip6h->saddr);
359 	u8 proto = ip6h->nexthdr;
360 	__be16 frag_off;
361 	int offset;
362 
363 	if (mtu <= IPV6_MIN_MTU)
364 		return 0;
365 
366 	if (stype == IPV6_ADDR_ANY || stype == IPV6_ADDR_MULTICAST ||
367 	    stype == IPV6_ADDR_LOOPBACK)
368 		return 0;
369 
370 	offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &proto,
371 				  &frag_off);
372 	if (offset < 0 || (frag_off & htons(~0x7)))
373 		return 0;
374 
375 	if (proto == IPPROTO_ICMPV6) {
376 		struct icmp6hdr *icmp6h;
377 
378 		if (!pskb_may_pull(skb, skb_network_header(skb) +
379 					offset + 1 - skb->data))
380 			return 0;
381 
382 		icmp6h = (struct icmp6hdr *)(skb_network_header(skb) + offset);
383 		if (icmpv6_is_err(icmp6h->icmp6_type) ||
384 		    icmp6h->icmp6_type == NDISC_REDIRECT)
385 			return 0;
386 	}
387 
388 	return iptunnel_pmtud_build_icmpv6(skb, mtu);
389 }
390 #endif /* IS_ENABLED(CONFIG_IPV6) */
391 
392 /**
393  * skb_tunnel_check_pmtu() - Check, update PMTU and trigger ICMP reply as needed
394  * @skb:	Buffer being sent by encapsulation, L2 headers expected
395  * @encap_dst:	Destination for tunnel encapsulation (outer IP)
396  * @headroom:	Encapsulation header size, bytes
397  * @reply:	Build matching ICMP or ICMPv6 message as a result
398  *
399  * L2 tunnel implementations that can carry IP and can be directly bridged
400  * (currently UDP tunnels) can't always rely on IP forwarding paths to handle
401  * PMTU discovery. In the bridged case, ICMP or ICMPv6 messages need to be built
402  * based on payload and sent back by the encapsulation itself.
403  *
404  * For routable interfaces, we just need to update the PMTU for the destination.
405  *
406  * Return: 0 if ICMP error not needed, length if built, negative value on error
407  */
408 int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst,
409 			  int headroom, bool reply)
410 {
411 	u32 mtu = dst_mtu(encap_dst) - headroom;
412 
413 	if ((skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) ||
414 	    (!skb_is_gso(skb) && (skb->len - skb_mac_header_len(skb)) <= mtu))
415 		return 0;
416 
417 	skb_dst_update_pmtu_no_confirm(skb, mtu);
418 
419 	if (!reply || skb->pkt_type == PACKET_HOST)
420 		return 0;
421 
422 	if (skb->protocol == htons(ETH_P_IP))
423 		return iptunnel_pmtud_check_icmp(skb, mtu);
424 
425 #if IS_ENABLED(CONFIG_IPV6)
426 	if (skb->protocol == htons(ETH_P_IPV6))
427 		return iptunnel_pmtud_check_icmpv6(skb, mtu);
428 #endif
429 	return 0;
430 }
431 EXPORT_SYMBOL(skb_tunnel_check_pmtu);
432 
433 /* Often modified stats are per cpu, other are shared (netdev->stats) */
434 void ip_tunnel_get_stats64(struct net_device *dev,
435 			   struct rtnl_link_stats64 *tot)
436 {
437 	int i;
438 
439 	netdev_stats_to_stats64(tot, &dev->stats);
440 
441 	for_each_possible_cpu(i) {
442 		const struct pcpu_sw_netstats *tstats =
443 						   per_cpu_ptr(dev->tstats, i);
444 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
445 		unsigned int start;
446 
447 		do {
448 			start = u64_stats_fetch_begin_irq(&tstats->syncp);
449 			rx_packets = tstats->rx_packets;
450 			tx_packets = tstats->tx_packets;
451 			rx_bytes = tstats->rx_bytes;
452 			tx_bytes = tstats->tx_bytes;
453 		} while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
454 
455 		tot->rx_packets += rx_packets;
456 		tot->tx_packets += tx_packets;
457 		tot->rx_bytes   += rx_bytes;
458 		tot->tx_bytes   += tx_bytes;
459 	}
460 }
461 EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
462 
463 static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
464 	[LWTUNNEL_IP_UNSPEC]	= { .strict_start_type = LWTUNNEL_IP_OPTS },
465 	[LWTUNNEL_IP_ID]	= { .type = NLA_U64 },
466 	[LWTUNNEL_IP_DST]	= { .type = NLA_U32 },
467 	[LWTUNNEL_IP_SRC]	= { .type = NLA_U32 },
468 	[LWTUNNEL_IP_TTL]	= { .type = NLA_U8 },
469 	[LWTUNNEL_IP_TOS]	= { .type = NLA_U8 },
470 	[LWTUNNEL_IP_FLAGS]	= { .type = NLA_U16 },
471 	[LWTUNNEL_IP_OPTS]	= { .type = NLA_NESTED },
472 };
473 
474 static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = {
475 	[LWTUNNEL_IP_OPTS_GENEVE]	= { .type = NLA_NESTED },
476 	[LWTUNNEL_IP_OPTS_VXLAN]	= { .type = NLA_NESTED },
477 	[LWTUNNEL_IP_OPTS_ERSPAN]	= { .type = NLA_NESTED },
478 };
479 
480 static const struct nla_policy
481 geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = {
482 	[LWTUNNEL_IP_OPT_GENEVE_CLASS]	= { .type = NLA_U16 },
483 	[LWTUNNEL_IP_OPT_GENEVE_TYPE]	= { .type = NLA_U8 },
484 	[LWTUNNEL_IP_OPT_GENEVE_DATA]	= { .type = NLA_BINARY, .len = 128 },
485 };
486 
487 static const struct nla_policy
488 vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = {
489 	[LWTUNNEL_IP_OPT_VXLAN_GBP]	= { .type = NLA_U32 },
490 };
491 
492 static const struct nla_policy
493 erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = {
494 	[LWTUNNEL_IP_OPT_ERSPAN_VER]	= { .type = NLA_U8 },
495 	[LWTUNNEL_IP_OPT_ERSPAN_INDEX]	= { .type = NLA_U32 },
496 	[LWTUNNEL_IP_OPT_ERSPAN_DIR]	= { .type = NLA_U8 },
497 	[LWTUNNEL_IP_OPT_ERSPAN_HWID]	= { .type = NLA_U8 },
498 };
499 
500 static int ip_tun_parse_opts_geneve(struct nlattr *attr,
501 				    struct ip_tunnel_info *info, int opts_len,
502 				    struct netlink_ext_ack *extack)
503 {
504 	struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
505 	int data_len, err;
506 
507 	err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr,
508 			       geneve_opt_policy, extack);
509 	if (err)
510 		return err;
511 
512 	if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] ||
513 	    !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] ||
514 	    !tb[LWTUNNEL_IP_OPT_GENEVE_DATA])
515 		return -EINVAL;
516 
517 	attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA];
518 	data_len = nla_len(attr);
519 	if (data_len % 4)
520 		return -EINVAL;
521 
522 	if (info) {
523 		struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len;
524 
525 		memcpy(opt->opt_data, nla_data(attr), data_len);
526 		opt->length = data_len / 4;
527 		attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS];
528 		opt->opt_class = nla_get_be16(attr);
529 		attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE];
530 		opt->type = nla_get_u8(attr);
531 		info->key.tun_flags |= TUNNEL_GENEVE_OPT;
532 	}
533 
534 	return sizeof(struct geneve_opt) + data_len;
535 }
536 
537 static int ip_tun_parse_opts_vxlan(struct nlattr *attr,
538 				   struct ip_tunnel_info *info, int opts_len,
539 				   struct netlink_ext_ack *extack)
540 {
541 	struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1];
542 	int err;
543 
544 	err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr,
545 			       vxlan_opt_policy, extack);
546 	if (err)
547 		return err;
548 
549 	if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP])
550 		return -EINVAL;
551 
552 	if (info) {
553 		struct vxlan_metadata *md =
554 			ip_tunnel_info_opts(info) + opts_len;
555 
556 		attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP];
557 		md->gbp = nla_get_u32(attr);
558 		info->key.tun_flags |= TUNNEL_VXLAN_OPT;
559 	}
560 
561 	return sizeof(struct vxlan_metadata);
562 }
563 
564 static int ip_tun_parse_opts_erspan(struct nlattr *attr,
565 				    struct ip_tunnel_info *info, int opts_len,
566 				    struct netlink_ext_ack *extack)
567 {
568 	struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
569 	int err;
570 	u8 ver;
571 
572 	err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr,
573 			       erspan_opt_policy, extack);
574 	if (err)
575 		return err;
576 
577 	if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER])
578 		return -EINVAL;
579 
580 	ver = nla_get_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]);
581 	if (ver == 1) {
582 		if (!tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX])
583 			return -EINVAL;
584 	} else if (ver == 2) {
585 		if (!tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] ||
586 		    !tb[LWTUNNEL_IP_OPT_ERSPAN_HWID])
587 			return -EINVAL;
588 	} else {
589 		return -EINVAL;
590 	}
591 
592 	if (info) {
593 		struct erspan_metadata *md =
594 			ip_tunnel_info_opts(info) + opts_len;
595 
596 		md->version = ver;
597 		if (ver == 1) {
598 			attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX];
599 			md->u.index = nla_get_be32(attr);
600 		} else {
601 			attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR];
602 			md->u.md2.dir = nla_get_u8(attr);
603 			attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID];
604 			set_hwid(&md->u.md2, nla_get_u8(attr));
605 		}
606 
607 		info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
608 	}
609 
610 	return sizeof(struct erspan_metadata);
611 }
612 
613 static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info,
614 			     struct netlink_ext_ack *extack)
615 {
616 	int err, rem, opt_len, opts_len = 0, type = 0;
617 	struct nlattr *nla;
618 
619 	if (!attr)
620 		return 0;
621 
622 	err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX,
623 			   ip_opts_policy, extack);
624 	if (err)
625 		return err;
626 
627 	nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) {
628 		switch (nla_type(nla)) {
629 		case LWTUNNEL_IP_OPTS_GENEVE:
630 			if (type && type != TUNNEL_GENEVE_OPT)
631 				return -EINVAL;
632 			opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len,
633 							   extack);
634 			if (opt_len < 0)
635 				return opt_len;
636 			opts_len += opt_len;
637 			if (opts_len > IP_TUNNEL_OPTS_MAX)
638 				return -EINVAL;
639 			type = TUNNEL_GENEVE_OPT;
640 			break;
641 		case LWTUNNEL_IP_OPTS_VXLAN:
642 			if (type)
643 				return -EINVAL;
644 			opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len,
645 							  extack);
646 			if (opt_len < 0)
647 				return opt_len;
648 			opts_len += opt_len;
649 			type = TUNNEL_VXLAN_OPT;
650 			break;
651 		case LWTUNNEL_IP_OPTS_ERSPAN:
652 			if (type)
653 				return -EINVAL;
654 			opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len,
655 							   extack);
656 			if (opt_len < 0)
657 				return opt_len;
658 			opts_len += opt_len;
659 			type = TUNNEL_ERSPAN_OPT;
660 			break;
661 		default:
662 			return -EINVAL;
663 		}
664 	}
665 
666 	return opts_len;
667 }
668 
669 static int ip_tun_get_optlen(struct nlattr *attr,
670 			     struct netlink_ext_ack *extack)
671 {
672 	return ip_tun_parse_opts(attr, NULL, extack);
673 }
674 
675 static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info,
676 			   struct netlink_ext_ack *extack)
677 {
678 	return ip_tun_parse_opts(attr, info, extack);
679 }
680 
681 static int ip_tun_build_state(struct net *net, struct nlattr *attr,
682 			      unsigned int family, const void *cfg,
683 			      struct lwtunnel_state **ts,
684 			      struct netlink_ext_ack *extack)
685 {
686 	struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
687 	struct lwtunnel_state *new_state;
688 	struct ip_tunnel_info *tun_info;
689 	int err, opt_len;
690 
691 	err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
692 					  ip_tun_policy, extack);
693 	if (err < 0)
694 		return err;
695 
696 	opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack);
697 	if (opt_len < 0)
698 		return opt_len;
699 
700 	new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
701 	if (!new_state)
702 		return -ENOMEM;
703 
704 	new_state->type = LWTUNNEL_ENCAP_IP;
705 
706 	tun_info = lwt_tun_info(new_state);
707 
708 	err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack);
709 	if (err < 0) {
710 		lwtstate_free(new_state);
711 		return err;
712 	}
713 
714 #ifdef CONFIG_DST_CACHE
715 	err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
716 	if (err) {
717 		lwtstate_free(new_state);
718 		return err;
719 	}
720 #endif
721 
722 	if (tb[LWTUNNEL_IP_ID])
723 		tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
724 
725 	if (tb[LWTUNNEL_IP_DST])
726 		tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
727 
728 	if (tb[LWTUNNEL_IP_SRC])
729 		tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
730 
731 	if (tb[LWTUNNEL_IP_TTL])
732 		tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
733 
734 	if (tb[LWTUNNEL_IP_TOS])
735 		tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
736 
737 	if (tb[LWTUNNEL_IP_FLAGS])
738 		tun_info->key.tun_flags |=
739 				(nla_get_be16(tb[LWTUNNEL_IP_FLAGS]) &
740 				 ~TUNNEL_OPTIONS_PRESENT);
741 
742 	tun_info->mode = IP_TUNNEL_INFO_TX;
743 	tun_info->options_len = opt_len;
744 
745 	*ts = new_state;
746 
747 	return 0;
748 }
749 
750 static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
751 {
752 #ifdef CONFIG_DST_CACHE
753 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
754 
755 	dst_cache_destroy(&tun_info->dst_cache);
756 #endif
757 }
758 
759 static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb,
760 					 struct ip_tunnel_info *tun_info)
761 {
762 	struct geneve_opt *opt;
763 	struct nlattr *nest;
764 	int offset = 0;
765 
766 	nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE);
767 	if (!nest)
768 		return -ENOMEM;
769 
770 	while (tun_info->options_len > offset) {
771 		opt = ip_tunnel_info_opts(tun_info) + offset;
772 		if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS,
773 				 opt->opt_class) ||
774 		    nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) ||
775 		    nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4,
776 			    opt->opt_data)) {
777 			nla_nest_cancel(skb, nest);
778 			return -ENOMEM;
779 		}
780 		offset += sizeof(*opt) + opt->length * 4;
781 	}
782 
783 	nla_nest_end(skb, nest);
784 	return 0;
785 }
786 
787 static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb,
788 					struct ip_tunnel_info *tun_info)
789 {
790 	struct vxlan_metadata *md;
791 	struct nlattr *nest;
792 
793 	nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN);
794 	if (!nest)
795 		return -ENOMEM;
796 
797 	md = ip_tunnel_info_opts(tun_info);
798 	if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) {
799 		nla_nest_cancel(skb, nest);
800 		return -ENOMEM;
801 	}
802 
803 	nla_nest_end(skb, nest);
804 	return 0;
805 }
806 
807 static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb,
808 					 struct ip_tunnel_info *tun_info)
809 {
810 	struct erspan_metadata *md;
811 	struct nlattr *nest;
812 
813 	nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN);
814 	if (!nest)
815 		return -ENOMEM;
816 
817 	md = ip_tunnel_info_opts(tun_info);
818 	if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version))
819 		goto err;
820 
821 	if (md->version == 1 &&
822 	    nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index))
823 		goto err;
824 
825 	if (md->version == 2 &&
826 	    (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) ||
827 	     nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID,
828 			get_hwid(&md->u.md2))))
829 		goto err;
830 
831 	nla_nest_end(skb, nest);
832 	return 0;
833 err:
834 	nla_nest_cancel(skb, nest);
835 	return -ENOMEM;
836 }
837 
838 static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type,
839 				  struct ip_tunnel_info *tun_info)
840 {
841 	struct nlattr *nest;
842 	int err = 0;
843 
844 	if (!(tun_info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
845 		return 0;
846 
847 	nest = nla_nest_start_noflag(skb, type);
848 	if (!nest)
849 		return -ENOMEM;
850 
851 	if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT)
852 		err = ip_tun_fill_encap_opts_geneve(skb, tun_info);
853 	else if (tun_info->key.tun_flags & TUNNEL_VXLAN_OPT)
854 		err = ip_tun_fill_encap_opts_vxlan(skb, tun_info);
855 	else if (tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT)
856 		err = ip_tun_fill_encap_opts_erspan(skb, tun_info);
857 
858 	if (err) {
859 		nla_nest_cancel(skb, nest);
860 		return err;
861 	}
862 
863 	nla_nest_end(skb, nest);
864 	return 0;
865 }
866 
867 static int ip_tun_fill_encap_info(struct sk_buff *skb,
868 				  struct lwtunnel_state *lwtstate)
869 {
870 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
871 
872 	if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
873 			 LWTUNNEL_IP_PAD) ||
874 	    nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
875 	    nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
876 	    nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
877 	    nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
878 	    nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) ||
879 	    ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info))
880 		return -ENOMEM;
881 
882 	return 0;
883 }
884 
885 static int ip_tun_opts_nlsize(struct ip_tunnel_info *info)
886 {
887 	int opt_len;
888 
889 	if (!(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
890 		return 0;
891 
892 	opt_len = nla_total_size(0);		/* LWTUNNEL_IP_OPTS */
893 	if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
894 		struct geneve_opt *opt;
895 		int offset = 0;
896 
897 		opt_len += nla_total_size(0);	/* LWTUNNEL_IP_OPTS_GENEVE */
898 		while (info->options_len > offset) {
899 			opt = ip_tunnel_info_opts(info) + offset;
900 			opt_len += nla_total_size(2)	/* OPT_GENEVE_CLASS */
901 				   + nla_total_size(1)	/* OPT_GENEVE_TYPE */
902 				   + nla_total_size(opt->length * 4);
903 							/* OPT_GENEVE_DATA */
904 			offset += sizeof(*opt) + opt->length * 4;
905 		}
906 	} else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
907 		opt_len += nla_total_size(0)	/* LWTUNNEL_IP_OPTS_VXLAN */
908 			   + nla_total_size(4);	/* OPT_VXLAN_GBP */
909 	} else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) {
910 		struct erspan_metadata *md = ip_tunnel_info_opts(info);
911 
912 		opt_len += nla_total_size(0)	/* LWTUNNEL_IP_OPTS_ERSPAN */
913 			   + nla_total_size(1)	/* OPT_ERSPAN_VER */
914 			   + (md->version == 1 ? nla_total_size(4)
915 						/* OPT_ERSPAN_INDEX (v1) */
916 					       : nla_total_size(1) +
917 						 nla_total_size(1));
918 						/* OPT_ERSPAN_DIR + HWID (v2) */
919 	}
920 
921 	return opt_len;
922 }
923 
924 static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
925 {
926 	return nla_total_size_64bit(8)	/* LWTUNNEL_IP_ID */
927 		+ nla_total_size(4)	/* LWTUNNEL_IP_DST */
928 		+ nla_total_size(4)	/* LWTUNNEL_IP_SRC */
929 		+ nla_total_size(1)	/* LWTUNNEL_IP_TOS */
930 		+ nla_total_size(1)	/* LWTUNNEL_IP_TTL */
931 		+ nla_total_size(2)	/* LWTUNNEL_IP_FLAGS */
932 		+ ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
933 					/* LWTUNNEL_IP_OPTS */
934 }
935 
936 static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
937 {
938 	struct ip_tunnel_info *info_a = lwt_tun_info(a);
939 	struct ip_tunnel_info *info_b = lwt_tun_info(b);
940 
941 	return memcmp(info_a, info_b, sizeof(info_a->key)) ||
942 	       info_a->mode != info_b->mode ||
943 	       info_a->options_len != info_b->options_len ||
944 	       memcmp(ip_tunnel_info_opts(info_a),
945 		      ip_tunnel_info_opts(info_b), info_a->options_len);
946 }
947 
948 static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
949 	.build_state = ip_tun_build_state,
950 	.destroy_state = ip_tun_destroy_state,
951 	.fill_encap = ip_tun_fill_encap_info,
952 	.get_encap_size = ip_tun_encap_nlsize,
953 	.cmp_encap = ip_tun_cmp_encap,
954 	.owner = THIS_MODULE,
955 };
956 
957 static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
958 	[LWTUNNEL_IP6_UNSPEC]	= { .strict_start_type = LWTUNNEL_IP6_OPTS },
959 	[LWTUNNEL_IP6_ID]		= { .type = NLA_U64 },
960 	[LWTUNNEL_IP6_DST]		= { .len = sizeof(struct in6_addr) },
961 	[LWTUNNEL_IP6_SRC]		= { .len = sizeof(struct in6_addr) },
962 	[LWTUNNEL_IP6_HOPLIMIT]		= { .type = NLA_U8 },
963 	[LWTUNNEL_IP6_TC]		= { .type = NLA_U8 },
964 	[LWTUNNEL_IP6_FLAGS]		= { .type = NLA_U16 },
965 	[LWTUNNEL_IP6_OPTS]		= { .type = NLA_NESTED },
966 };
967 
968 static int ip6_tun_build_state(struct net *net, struct nlattr *attr,
969 			       unsigned int family, const void *cfg,
970 			       struct lwtunnel_state **ts,
971 			       struct netlink_ext_ack *extack)
972 {
973 	struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
974 	struct lwtunnel_state *new_state;
975 	struct ip_tunnel_info *tun_info;
976 	int err, opt_len;
977 
978 	err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
979 					  ip6_tun_policy, extack);
980 	if (err < 0)
981 		return err;
982 
983 	opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack);
984 	if (opt_len < 0)
985 		return opt_len;
986 
987 	new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
988 	if (!new_state)
989 		return -ENOMEM;
990 
991 	new_state->type = LWTUNNEL_ENCAP_IP6;
992 
993 	tun_info = lwt_tun_info(new_state);
994 
995 	err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack);
996 	if (err < 0) {
997 		lwtstate_free(new_state);
998 		return err;
999 	}
1000 
1001 	if (tb[LWTUNNEL_IP6_ID])
1002 		tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
1003 
1004 	if (tb[LWTUNNEL_IP6_DST])
1005 		tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
1006 
1007 	if (tb[LWTUNNEL_IP6_SRC])
1008 		tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
1009 
1010 	if (tb[LWTUNNEL_IP6_HOPLIMIT])
1011 		tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
1012 
1013 	if (tb[LWTUNNEL_IP6_TC])
1014 		tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
1015 
1016 	if (tb[LWTUNNEL_IP6_FLAGS])
1017 		tun_info->key.tun_flags |=
1018 				(nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]) &
1019 				 ~TUNNEL_OPTIONS_PRESENT);
1020 
1021 	tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
1022 	tun_info->options_len = opt_len;
1023 
1024 	*ts = new_state;
1025 
1026 	return 0;
1027 }
1028 
1029 static int ip6_tun_fill_encap_info(struct sk_buff *skb,
1030 				   struct lwtunnel_state *lwtstate)
1031 {
1032 	struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
1033 
1034 	if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
1035 			 LWTUNNEL_IP6_PAD) ||
1036 	    nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
1037 	    nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
1038 	    nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
1039 	    nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
1040 	    nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) ||
1041 	    ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info))
1042 		return -ENOMEM;
1043 
1044 	return 0;
1045 }
1046 
1047 static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
1048 {
1049 	return nla_total_size_64bit(8)	/* LWTUNNEL_IP6_ID */
1050 		+ nla_total_size(16)	/* LWTUNNEL_IP6_DST */
1051 		+ nla_total_size(16)	/* LWTUNNEL_IP6_SRC */
1052 		+ nla_total_size(1)	/* LWTUNNEL_IP6_HOPLIMIT */
1053 		+ nla_total_size(1)	/* LWTUNNEL_IP6_TC */
1054 		+ nla_total_size(2)	/* LWTUNNEL_IP6_FLAGS */
1055 		+ ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
1056 					/* LWTUNNEL_IP6_OPTS */
1057 }
1058 
1059 static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
1060 	.build_state = ip6_tun_build_state,
1061 	.fill_encap = ip6_tun_fill_encap_info,
1062 	.get_encap_size = ip6_tun_encap_nlsize,
1063 	.cmp_encap = ip_tun_cmp_encap,
1064 	.owner = THIS_MODULE,
1065 };
1066 
1067 void __init ip_tunnel_core_init(void)
1068 {
1069 	/* If you land here, make sure whether increasing ip_tunnel_info's
1070 	 * options_len is a reasonable choice with its usage in front ends
1071 	 * (f.e., it's part of flow keys, etc).
1072 	 */
1073 	BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
1074 
1075 	lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
1076 	lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
1077 }
1078 
1079 DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
1080 EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
1081 
1082 void ip_tunnel_need_metadata(void)
1083 {
1084 	static_branch_inc(&ip_tunnel_metadata_cnt);
1085 }
1086 EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
1087 
1088 void ip_tunnel_unneed_metadata(void)
1089 {
1090 	static_branch_dec(&ip_tunnel_metadata_cnt);
1091 }
1092 EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);
1093 
1094 /* Returns either the correct skb->protocol value, or 0 if invalid. */
1095 __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb)
1096 {
1097 	if (skb_network_header(skb) >= skb->head &&
1098 	    (skb_network_header(skb) + sizeof(struct iphdr)) <= skb_tail_pointer(skb) &&
1099 	    ip_hdr(skb)->version == 4)
1100 		return htons(ETH_P_IP);
1101 	if (skb_network_header(skb) >= skb->head &&
1102 	    (skb_network_header(skb) + sizeof(struct ipv6hdr)) <= skb_tail_pointer(skb) &&
1103 	    ipv6_hdr(skb)->version == 6)
1104 		return htons(ETH_P_IPV6);
1105 	return 0;
1106 }
1107 EXPORT_SYMBOL(ip_tunnel_parse_protocol);
1108 
1109 const struct header_ops ip_tunnel_header_ops = { .parse_protocol = ip_tunnel_parse_protocol };
1110 EXPORT_SYMBOL(ip_tunnel_header_ops);
1111