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