1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013 Nicira, Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/skbuff.h>
11 #include <linux/netdevice.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>
25 #include <net/protocol.h>
26 #include <net/ip_tunnels.h>
27 #include <net/ip6_tunnel.h>
29 #include <net/checksum.h>
30 #include <net/dsfield.h>
31 #include <net/inet_ecn.h>
33 #include <net/net_namespace.h>
34 #include <net/netns/generic.h>
35 #include <net/rtnetlink.h>
36 #include <net/dst_metadata.h>
37 #include <net/geneve.h>
38 #include <net/vxlan.h>
39 #include <net/erspan.h>
41 const struct ip_tunnel_encap_ops __rcu *
42 iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
43 EXPORT_SYMBOL(iptun_encaps);
45 const struct ip6_tnl_encap_ops __rcu *
46 ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
47 EXPORT_SYMBOL(ip6tun_encaps);
49 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
50 __be32 src, __be32 dst, __u8 proto,
51 __u8 tos, __u8 ttl, __be16 df, bool xnet)
53 int pkt_len = skb->len - skb_inner_network_offset(skb);
54 struct net *net = dev_net(rt->dst.dev);
55 struct net_device *dev = skb->dev;
59 skb_scrub_packet(skb, xnet);
61 skb_clear_hash_if_not_l4(skb);
62 skb_dst_set(skb, &rt->dst);
63 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
65 /* Push down and install the IP header. */
66 skb_push(skb, sizeof(struct iphdr));
67 skb_reset_network_header(skb);
72 iph->ihl = sizeof(struct iphdr) >> 2;
73 iph->frag_off = ip_mtu_locked(&rt->dst) ? 0 : df;
74 iph->protocol = proto;
79 __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
81 err = ip_local_out(net, sk, skb);
84 if (unlikely(net_xmit_eval(err)))
86 iptunnel_xmit_stats(dev, pkt_len);
89 EXPORT_SYMBOL_GPL(iptunnel_xmit);
91 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
92 __be16 inner_proto, bool raw_proto, bool xnet)
94 if (unlikely(!pskb_may_pull(skb, hdr_len)))
97 skb_pull_rcsum(skb, hdr_len);
99 if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
102 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
105 eh = (struct ethhdr *)skb->data;
106 if (likely(eth_proto_is_802_3(eh->h_proto)))
107 skb->protocol = eh->h_proto;
109 skb->protocol = htons(ETH_P_802_2);
112 skb->protocol = inner_proto;
115 skb_clear_hash_if_not_l4(skb);
116 __vlan_hwaccel_clear_tag(skb);
117 skb_set_queue_mapping(skb, 0);
118 skb_scrub_packet(skb, xnet);
120 return iptunnel_pull_offloads(skb);
122 EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
124 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
127 struct metadata_dst *res;
128 struct ip_tunnel_info *dst, *src;
130 if (!md || md->type != METADATA_IP_TUNNEL ||
131 md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
134 src = &md->u.tun_info;
135 res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
139 dst = &res->u.tun_info;
140 dst->key.tun_id = src->key.tun_id;
141 if (src->mode & IP_TUNNEL_INFO_IPV6)
142 memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
143 sizeof(struct in6_addr));
145 dst->key.u.ipv4.dst = src->key.u.ipv4.src;
146 dst->key.tun_flags = src->key.tun_flags;
147 dst->mode = src->mode | IP_TUNNEL_INFO_TX;
148 ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
149 src->options_len, 0);
153 EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
155 int iptunnel_handle_offloads(struct sk_buff *skb,
160 if (likely(!skb->encapsulation)) {
161 skb_reset_inner_headers(skb);
162 skb->encapsulation = 1;
165 if (skb_is_gso(skb)) {
166 err = skb_header_unclone(skb, GFP_ATOMIC);
169 skb_shinfo(skb)->gso_type |= gso_type_mask;
173 if (skb->ip_summed != CHECKSUM_PARTIAL) {
174 skb->ip_summed = CHECKSUM_NONE;
175 /* We clear encapsulation here to prevent badly-written
176 * drivers potentially deciding to offload an inner checksum
177 * if we set CHECKSUM_PARTIAL on the outer header.
178 * This should go away when the drivers are all fixed.
180 skb->encapsulation = 0;
185 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
187 /* Often modified stats are per cpu, other are shared (netdev->stats) */
188 void ip_tunnel_get_stats64(struct net_device *dev,
189 struct rtnl_link_stats64 *tot)
193 netdev_stats_to_stats64(tot, &dev->stats);
195 for_each_possible_cpu(i) {
196 const struct pcpu_sw_netstats *tstats =
197 per_cpu_ptr(dev->tstats, i);
198 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
202 start = u64_stats_fetch_begin_irq(&tstats->syncp);
203 rx_packets = tstats->rx_packets;
204 tx_packets = tstats->tx_packets;
205 rx_bytes = tstats->rx_bytes;
206 tx_bytes = tstats->tx_bytes;
207 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
209 tot->rx_packets += rx_packets;
210 tot->tx_packets += tx_packets;
211 tot->rx_bytes += rx_bytes;
212 tot->tx_bytes += tx_bytes;
215 EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
217 static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
218 [LWTUNNEL_IP_UNSPEC] = { .strict_start_type = LWTUNNEL_IP_OPTS },
219 [LWTUNNEL_IP_ID] = { .type = NLA_U64 },
220 [LWTUNNEL_IP_DST] = { .type = NLA_U32 },
221 [LWTUNNEL_IP_SRC] = { .type = NLA_U32 },
222 [LWTUNNEL_IP_TTL] = { .type = NLA_U8 },
223 [LWTUNNEL_IP_TOS] = { .type = NLA_U8 },
224 [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 },
225 [LWTUNNEL_IP_OPTS] = { .type = NLA_NESTED },
228 static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = {
229 [LWTUNNEL_IP_OPTS_GENEVE] = { .type = NLA_NESTED },
230 [LWTUNNEL_IP_OPTS_VXLAN] = { .type = NLA_NESTED },
231 [LWTUNNEL_IP_OPTS_ERSPAN] = { .type = NLA_NESTED },
234 static const struct nla_policy
235 geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = {
236 [LWTUNNEL_IP_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
237 [LWTUNNEL_IP_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
238 [LWTUNNEL_IP_OPT_GENEVE_DATA] = { .type = NLA_BINARY, .len = 128 },
241 static const struct nla_policy
242 vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = {
243 [LWTUNNEL_IP_OPT_VXLAN_GBP] = { .type = NLA_U32 },
246 static const struct nla_policy
247 erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = {
248 [LWTUNNEL_IP_OPT_ERSPAN_VER] = { .type = NLA_U8 },
249 [LWTUNNEL_IP_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
250 [LWTUNNEL_IP_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
251 [LWTUNNEL_IP_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
254 static int ip_tun_parse_opts_geneve(struct nlattr *attr,
255 struct ip_tunnel_info *info, int opts_len,
256 struct netlink_ext_ack *extack)
258 struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
261 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr,
262 geneve_opt_policy, extack);
266 if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] ||
267 !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] ||
268 !tb[LWTUNNEL_IP_OPT_GENEVE_DATA])
271 attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA];
272 data_len = nla_len(attr);
277 struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len;
279 memcpy(opt->opt_data, nla_data(attr), data_len);
280 opt->length = data_len / 4;
281 attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS];
282 opt->opt_class = nla_get_be16(attr);
283 attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE];
284 opt->type = nla_get_u8(attr);
285 info->key.tun_flags |= TUNNEL_GENEVE_OPT;
288 return sizeof(struct geneve_opt) + data_len;
291 static int ip_tun_parse_opts_vxlan(struct nlattr *attr,
292 struct ip_tunnel_info *info, int opts_len,
293 struct netlink_ext_ack *extack)
295 struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1];
298 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr,
299 vxlan_opt_policy, extack);
303 if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP])
307 struct vxlan_metadata *md =
308 ip_tunnel_info_opts(info) + opts_len;
310 attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP];
311 md->gbp = nla_get_u32(attr);
312 info->key.tun_flags |= TUNNEL_VXLAN_OPT;
315 return sizeof(struct vxlan_metadata);
318 static int ip_tun_parse_opts_erspan(struct nlattr *attr,
319 struct ip_tunnel_info *info, int opts_len,
320 struct netlink_ext_ack *extack)
322 struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
326 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr,
327 erspan_opt_policy, extack);
331 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER])
334 ver = nla_get_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]);
336 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX])
338 } else if (ver == 2) {
339 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] ||
340 !tb[LWTUNNEL_IP_OPT_ERSPAN_HWID])
347 struct erspan_metadata *md =
348 ip_tunnel_info_opts(info) + opts_len;
352 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX];
353 md->u.index = nla_get_be32(attr);
355 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR];
356 md->u.md2.dir = nla_get_u8(attr);
357 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID];
358 set_hwid(&md->u.md2, nla_get_u8(attr));
361 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
364 return sizeof(struct erspan_metadata);
367 static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info,
368 struct netlink_ext_ack *extack)
370 int err, rem, opt_len, opts_len = 0, type = 0;
376 err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX,
377 ip_opts_policy, extack);
381 nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) {
382 switch (nla_type(nla)) {
383 case LWTUNNEL_IP_OPTS_GENEVE:
384 if (type && type != TUNNEL_GENEVE_OPT)
386 opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len,
391 if (opts_len > IP_TUNNEL_OPTS_MAX)
393 type = TUNNEL_GENEVE_OPT;
395 case LWTUNNEL_IP_OPTS_VXLAN:
398 opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len,
403 type = TUNNEL_VXLAN_OPT;
405 case LWTUNNEL_IP_OPTS_ERSPAN:
408 opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len,
413 type = TUNNEL_ERSPAN_OPT;
423 static int ip_tun_get_optlen(struct nlattr *attr,
424 struct netlink_ext_ack *extack)
426 return ip_tun_parse_opts(attr, NULL, extack);
429 static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info,
430 struct netlink_ext_ack *extack)
432 return ip_tun_parse_opts(attr, info, extack);
435 static int ip_tun_build_state(struct nlattr *attr,
436 unsigned int family, const void *cfg,
437 struct lwtunnel_state **ts,
438 struct netlink_ext_ack *extack)
440 struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
441 struct lwtunnel_state *new_state;
442 struct ip_tunnel_info *tun_info;
445 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
446 ip_tun_policy, extack);
450 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack);
454 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
458 new_state->type = LWTUNNEL_ENCAP_IP;
460 tun_info = lwt_tun_info(new_state);
462 err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack);
464 lwtstate_free(new_state);
468 #ifdef CONFIG_DST_CACHE
469 err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
471 lwtstate_free(new_state);
476 if (tb[LWTUNNEL_IP_ID])
477 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
479 if (tb[LWTUNNEL_IP_DST])
480 tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
482 if (tb[LWTUNNEL_IP_SRC])
483 tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
485 if (tb[LWTUNNEL_IP_TTL])
486 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
488 if (tb[LWTUNNEL_IP_TOS])
489 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
491 if (tb[LWTUNNEL_IP_FLAGS])
492 tun_info->key.tun_flags |=
493 (nla_get_be16(tb[LWTUNNEL_IP_FLAGS]) &
494 ~TUNNEL_OPTIONS_PRESENT);
496 tun_info->mode = IP_TUNNEL_INFO_TX;
497 tun_info->options_len = opt_len;
504 static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
506 #ifdef CONFIG_DST_CACHE
507 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
509 dst_cache_destroy(&tun_info->dst_cache);
513 static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb,
514 struct ip_tunnel_info *tun_info)
516 struct geneve_opt *opt;
520 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE);
524 while (tun_info->options_len > offset) {
525 opt = ip_tunnel_info_opts(tun_info) + offset;
526 if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS,
528 nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) ||
529 nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4,
531 nla_nest_cancel(skb, nest);
534 offset += sizeof(*opt) + opt->length * 4;
537 nla_nest_end(skb, nest);
541 static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb,
542 struct ip_tunnel_info *tun_info)
544 struct vxlan_metadata *md;
547 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN);
551 md = ip_tunnel_info_opts(tun_info);
552 if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) {
553 nla_nest_cancel(skb, nest);
557 nla_nest_end(skb, nest);
561 static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb,
562 struct ip_tunnel_info *tun_info)
564 struct erspan_metadata *md;
567 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN);
571 md = ip_tunnel_info_opts(tun_info);
572 if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version))
575 if (md->version == 1 &&
576 nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index))
579 if (md->version == 2 &&
580 (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) ||
581 nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID,
582 get_hwid(&md->u.md2))))
585 nla_nest_end(skb, nest);
588 nla_nest_cancel(skb, nest);
592 static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type,
593 struct ip_tunnel_info *tun_info)
598 if (!(tun_info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
601 nest = nla_nest_start_noflag(skb, type);
605 if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT)
606 err = ip_tun_fill_encap_opts_geneve(skb, tun_info);
607 else if (tun_info->key.tun_flags & TUNNEL_VXLAN_OPT)
608 err = ip_tun_fill_encap_opts_vxlan(skb, tun_info);
609 else if (tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT)
610 err = ip_tun_fill_encap_opts_erspan(skb, tun_info);
613 nla_nest_cancel(skb, nest);
617 nla_nest_end(skb, nest);
621 static int ip_tun_fill_encap_info(struct sk_buff *skb,
622 struct lwtunnel_state *lwtstate)
624 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
626 if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
628 nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
629 nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
630 nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
631 nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
632 nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) ||
633 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info))
639 static int ip_tun_opts_nlsize(struct ip_tunnel_info *info)
643 if (!(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
646 opt_len = nla_total_size(0); /* LWTUNNEL_IP_OPTS */
647 if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
648 struct geneve_opt *opt;
651 opt_len += nla_total_size(0); /* LWTUNNEL_IP_OPTS_GENEVE */
652 while (info->options_len > offset) {
653 opt = ip_tunnel_info_opts(info) + offset;
654 opt_len += nla_total_size(2) /* OPT_GENEVE_CLASS */
655 + nla_total_size(1) /* OPT_GENEVE_TYPE */
656 + nla_total_size(opt->length * 4);
657 /* OPT_GENEVE_DATA */
658 offset += sizeof(*opt) + opt->length * 4;
660 } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
661 opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_VXLAN */
662 + nla_total_size(4); /* OPT_VXLAN_GBP */
663 } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) {
664 struct erspan_metadata *md = ip_tunnel_info_opts(info);
666 opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_ERSPAN */
667 + nla_total_size(1) /* OPT_ERSPAN_VER */
668 + (md->version == 1 ? nla_total_size(4)
669 /* OPT_ERSPAN_INDEX (v1) */
670 : nla_total_size(1) +
672 /* OPT_ERSPAN_DIR + HWID (v2) */
678 static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
680 return nla_total_size_64bit(8) /* LWTUNNEL_IP_ID */
681 + nla_total_size(4) /* LWTUNNEL_IP_DST */
682 + nla_total_size(4) /* LWTUNNEL_IP_SRC */
683 + nla_total_size(1) /* LWTUNNEL_IP_TOS */
684 + nla_total_size(1) /* LWTUNNEL_IP_TTL */
685 + nla_total_size(2) /* LWTUNNEL_IP_FLAGS */
686 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
687 /* LWTUNNEL_IP_OPTS */
690 static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
692 struct ip_tunnel_info *info_a = lwt_tun_info(a);
693 struct ip_tunnel_info *info_b = lwt_tun_info(b);
695 return memcmp(info_a, info_b, sizeof(info_a->key)) ||
696 info_a->mode != info_b->mode ||
697 info_a->options_len != info_b->options_len ||
698 memcmp(ip_tunnel_info_opts(info_a),
699 ip_tunnel_info_opts(info_b), info_a->options_len);
702 static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
703 .build_state = ip_tun_build_state,
704 .destroy_state = ip_tun_destroy_state,
705 .fill_encap = ip_tun_fill_encap_info,
706 .get_encap_size = ip_tun_encap_nlsize,
707 .cmp_encap = ip_tun_cmp_encap,
708 .owner = THIS_MODULE,
711 static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
712 [LWTUNNEL_IP6_UNSPEC] = { .strict_start_type = LWTUNNEL_IP6_OPTS },
713 [LWTUNNEL_IP6_ID] = { .type = NLA_U64 },
714 [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) },
715 [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) },
716 [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 },
717 [LWTUNNEL_IP6_TC] = { .type = NLA_U8 },
718 [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 },
719 [LWTUNNEL_IP6_OPTS] = { .type = NLA_NESTED },
722 static int ip6_tun_build_state(struct nlattr *attr,
723 unsigned int family, const void *cfg,
724 struct lwtunnel_state **ts,
725 struct netlink_ext_ack *extack)
727 struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
728 struct lwtunnel_state *new_state;
729 struct ip_tunnel_info *tun_info;
732 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
733 ip6_tun_policy, extack);
737 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack);
741 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
745 new_state->type = LWTUNNEL_ENCAP_IP6;
747 tun_info = lwt_tun_info(new_state);
749 err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack);
751 lwtstate_free(new_state);
755 if (tb[LWTUNNEL_IP6_ID])
756 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
758 if (tb[LWTUNNEL_IP6_DST])
759 tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
761 if (tb[LWTUNNEL_IP6_SRC])
762 tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
764 if (tb[LWTUNNEL_IP6_HOPLIMIT])
765 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
767 if (tb[LWTUNNEL_IP6_TC])
768 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
770 if (tb[LWTUNNEL_IP6_FLAGS])
771 tun_info->key.tun_flags |=
772 (nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]) &
773 ~TUNNEL_OPTIONS_PRESENT);
775 tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
776 tun_info->options_len = opt_len;
783 static int ip6_tun_fill_encap_info(struct sk_buff *skb,
784 struct lwtunnel_state *lwtstate)
786 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
788 if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
790 nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
791 nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
792 nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
793 nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
794 nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) ||
795 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info))
801 static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
803 return nla_total_size_64bit(8) /* LWTUNNEL_IP6_ID */
804 + nla_total_size(16) /* LWTUNNEL_IP6_DST */
805 + nla_total_size(16) /* LWTUNNEL_IP6_SRC */
806 + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */
807 + nla_total_size(1) /* LWTUNNEL_IP6_TC */
808 + nla_total_size(2) /* LWTUNNEL_IP6_FLAGS */
809 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
810 /* LWTUNNEL_IP6_OPTS */
813 static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
814 .build_state = ip6_tun_build_state,
815 .fill_encap = ip6_tun_fill_encap_info,
816 .get_encap_size = ip6_tun_encap_nlsize,
817 .cmp_encap = ip_tun_cmp_encap,
818 .owner = THIS_MODULE,
821 void __init ip_tunnel_core_init(void)
823 /* If you land here, make sure whether increasing ip_tunnel_info's
824 * options_len is a reasonable choice with its usage in front ends
825 * (f.e., it's part of flow keys, etc).
827 BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
829 lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
830 lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
833 DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
834 EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
836 void ip_tunnel_need_metadata(void)
838 static_branch_inc(&ip_tunnel_metadata_cnt);
840 EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
842 void ip_tunnel_unneed_metadata(void)
844 static_branch_dec(&ip_tunnel_metadata_cnt);
846 EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);