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