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