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