]> asedeno.scripts.mit.edu Git - linux.git/blob - net/ipv4/ip_tunnel_core.c
lwtunnel: add options setting and dumping for geneve
[linux.git] / net / ipv4 / ip_tunnel_core.c
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/arp.h>
29 #include <net/checksum.h>
30 #include <net/dsfield.h>
31 #include <net/inet_ecn.h>
32 #include <net/xfrm.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
39 const struct ip_tunnel_encap_ops __rcu *
40                 iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
41 EXPORT_SYMBOL(iptun_encaps);
42
43 const struct ip6_tnl_encap_ops __rcu *
44                 ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
45 EXPORT_SYMBOL(ip6tun_encaps);
46
47 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
48                    __be32 src, __be32 dst, __u8 proto,
49                    __u8 tos, __u8 ttl, __be16 df, bool xnet)
50 {
51         int pkt_len = skb->len - skb_inner_network_offset(skb);
52         struct net *net = dev_net(rt->dst.dev);
53         struct net_device *dev = skb->dev;
54         struct iphdr *iph;
55         int err;
56
57         skb_scrub_packet(skb, xnet);
58
59         skb_clear_hash_if_not_l4(skb);
60         skb_dst_set(skb, &rt->dst);
61         memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
62
63         /* Push down and install the IP header. */
64         skb_push(skb, sizeof(struct iphdr));
65         skb_reset_network_header(skb);
66
67         iph = ip_hdr(skb);
68
69         iph->version    =       4;
70         iph->ihl        =       sizeof(struct iphdr) >> 2;
71         iph->frag_off   =       ip_mtu_locked(&rt->dst) ? 0 : df;
72         iph->protocol   =       proto;
73         iph->tos        =       tos;
74         iph->daddr      =       dst;
75         iph->saddr      =       src;
76         iph->ttl        =       ttl;
77         __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
78
79         err = ip_local_out(net, sk, skb);
80
81         if (dev) {
82                 if (unlikely(net_xmit_eval(err)))
83                         pkt_len = 0;
84                 iptunnel_xmit_stats(dev, pkt_len);
85         }
86 }
87 EXPORT_SYMBOL_GPL(iptunnel_xmit);
88
89 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
90                            __be16 inner_proto, bool raw_proto, bool xnet)
91 {
92         if (unlikely(!pskb_may_pull(skb, hdr_len)))
93                 return -ENOMEM;
94
95         skb_pull_rcsum(skb, hdr_len);
96
97         if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
98                 struct ethhdr *eh;
99
100                 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
101                         return -ENOMEM;
102
103                 eh = (struct ethhdr *)skb->data;
104                 if (likely(eth_proto_is_802_3(eh->h_proto)))
105                         skb->protocol = eh->h_proto;
106                 else
107                         skb->protocol = htons(ETH_P_802_2);
108
109         } else {
110                 skb->protocol = inner_proto;
111         }
112
113         skb_clear_hash_if_not_l4(skb);
114         __vlan_hwaccel_clear_tag(skb);
115         skb_set_queue_mapping(skb, 0);
116         skb_scrub_packet(skb, xnet);
117
118         return iptunnel_pull_offloads(skb);
119 }
120 EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
121
122 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
123                                              gfp_t flags)
124 {
125         struct metadata_dst *res;
126         struct ip_tunnel_info *dst, *src;
127
128         if (!md || md->type != METADATA_IP_TUNNEL ||
129             md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
130                 return NULL;
131
132         src = &md->u.tun_info;
133         res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
134         if (!res)
135                 return NULL;
136
137         dst = &res->u.tun_info;
138         dst->key.tun_id = src->key.tun_id;
139         if (src->mode & IP_TUNNEL_INFO_IPV6)
140                 memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
141                        sizeof(struct in6_addr));
142         else
143                 dst->key.u.ipv4.dst = src->key.u.ipv4.src;
144         dst->key.tun_flags = src->key.tun_flags;
145         dst->mode = src->mode | IP_TUNNEL_INFO_TX;
146         ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
147                                 src->options_len, 0);
148
149         return res;
150 }
151 EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
152
153 int iptunnel_handle_offloads(struct sk_buff *skb,
154                              int gso_type_mask)
155 {
156         int err;
157
158         if (likely(!skb->encapsulation)) {
159                 skb_reset_inner_headers(skb);
160                 skb->encapsulation = 1;
161         }
162
163         if (skb_is_gso(skb)) {
164                 err = skb_header_unclone(skb, GFP_ATOMIC);
165                 if (unlikely(err))
166                         return err;
167                 skb_shinfo(skb)->gso_type |= gso_type_mask;
168                 return 0;
169         }
170
171         if (skb->ip_summed != CHECKSUM_PARTIAL) {
172                 skb->ip_summed = CHECKSUM_NONE;
173                 /* We clear encapsulation here to prevent badly-written
174                  * drivers potentially deciding to offload an inner checksum
175                  * if we set CHECKSUM_PARTIAL on the outer header.
176                  * This should go away when the drivers are all fixed.
177                  */
178                 skb->encapsulation = 0;
179         }
180
181         return 0;
182 }
183 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
184
185 /* Often modified stats are per cpu, other are shared (netdev->stats) */
186 void ip_tunnel_get_stats64(struct net_device *dev,
187                            struct rtnl_link_stats64 *tot)
188 {
189         int i;
190
191         netdev_stats_to_stats64(tot, &dev->stats);
192
193         for_each_possible_cpu(i) {
194                 const struct pcpu_sw_netstats *tstats =
195                                                    per_cpu_ptr(dev->tstats, i);
196                 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
197                 unsigned int start;
198
199                 do {
200                         start = u64_stats_fetch_begin_irq(&tstats->syncp);
201                         rx_packets = tstats->rx_packets;
202                         tx_packets = tstats->tx_packets;
203                         rx_bytes = tstats->rx_bytes;
204                         tx_bytes = tstats->tx_bytes;
205                 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
206
207                 tot->rx_packets += rx_packets;
208                 tot->tx_packets += tx_packets;
209                 tot->rx_bytes   += rx_bytes;
210                 tot->tx_bytes   += tx_bytes;
211         }
212 }
213 EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
214
215 static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
216         [LWTUNNEL_IP_ID]        = { .type = NLA_U64 },
217         [LWTUNNEL_IP_DST]       = { .type = NLA_U32 },
218         [LWTUNNEL_IP_SRC]       = { .type = NLA_U32 },
219         [LWTUNNEL_IP_TTL]       = { .type = NLA_U8 },
220         [LWTUNNEL_IP_TOS]       = { .type = NLA_U8 },
221         [LWTUNNEL_IP_FLAGS]     = { .type = NLA_U16 },
222         [LWTUNNEL_IP_OPTS]      = { .type = NLA_NESTED },
223 };
224
225 static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = {
226         [LWTUNNEL_IP_OPTS_GENEVE]       = { .type = NLA_NESTED },
227 };
228
229 static const struct nla_policy
230 geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = {
231         [LWTUNNEL_IP_OPT_GENEVE_CLASS]  = { .type = NLA_U16 },
232         [LWTUNNEL_IP_OPT_GENEVE_TYPE]   = { .type = NLA_U8 },
233         [LWTUNNEL_IP_OPT_GENEVE_DATA]   = { .type = NLA_BINARY, .len = 128 },
234 };
235
236 static int ip_tun_parse_opts_geneve(struct nlattr *attr,
237                                     struct ip_tunnel_info *info,
238                                     struct netlink_ext_ack *extack)
239 {
240         struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
241         int data_len, err;
242
243         err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_OPT_GENEVE_MAX,
244                                           attr, geneve_opt_policy, extack);
245         if (err)
246                 return err;
247
248         if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] ||
249             !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] ||
250             !tb[LWTUNNEL_IP_OPT_GENEVE_DATA])
251                 return -EINVAL;
252
253         attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA];
254         data_len = nla_len(attr);
255         if (data_len % 4)
256                 return -EINVAL;
257
258         if (info) {
259                 struct geneve_opt *opt = ip_tunnel_info_opts(info);
260
261                 memcpy(opt->opt_data, nla_data(attr), data_len);
262                 opt->length = data_len / 4;
263                 attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS];
264                 opt->opt_class = nla_get_be16(attr);
265                 attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE];
266                 opt->type = nla_get_u8(attr);
267                 info->key.tun_flags |= TUNNEL_GENEVE_OPT;
268         }
269
270         return sizeof(struct geneve_opt) + data_len;
271 }
272
273 static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info,
274                              struct netlink_ext_ack *extack)
275 {
276         struct nlattr *tb[LWTUNNEL_IP_OPTS_MAX + 1];
277         int err;
278
279         if (!attr)
280                 return 0;
281
282         err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_OPTS_MAX, attr,
283                                           ip_opts_policy, extack);
284         if (err)
285                 return err;
286
287         if (tb[LWTUNNEL_IP_OPTS_GENEVE])
288                 err = ip_tun_parse_opts_geneve(tb[LWTUNNEL_IP_OPTS_GENEVE],
289                                                info, extack);
290         else
291                 err = -EINVAL;
292
293         return err;
294 }
295
296 static int ip_tun_get_optlen(struct nlattr *attr,
297                              struct netlink_ext_ack *extack)
298 {
299         return ip_tun_parse_opts(attr, NULL, extack);
300 }
301
302 static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info,
303                            struct netlink_ext_ack *extack)
304 {
305         return ip_tun_parse_opts(attr, info, extack);
306 }
307
308 static int ip_tun_build_state(struct nlattr *attr,
309                               unsigned int family, const void *cfg,
310                               struct lwtunnel_state **ts,
311                               struct netlink_ext_ack *extack)
312 {
313         struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
314         struct lwtunnel_state *new_state;
315         struct ip_tunnel_info *tun_info;
316         int err, opt_len;
317
318         err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
319                                           ip_tun_policy, extack);
320         if (err < 0)
321                 return err;
322
323         opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack);
324         if (opt_len < 0)
325                 return opt_len;
326
327         new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
328         if (!new_state)
329                 return -ENOMEM;
330
331         new_state->type = LWTUNNEL_ENCAP_IP;
332
333         tun_info = lwt_tun_info(new_state);
334
335         err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack);
336         if (err < 0) {
337                 lwtstate_free(new_state);
338                 return err;
339         }
340
341 #ifdef CONFIG_DST_CACHE
342         err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
343         if (err) {
344                 lwtstate_free(new_state);
345                 return err;
346         }
347 #endif
348
349         if (tb[LWTUNNEL_IP_ID])
350                 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
351
352         if (tb[LWTUNNEL_IP_DST])
353                 tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
354
355         if (tb[LWTUNNEL_IP_SRC])
356                 tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
357
358         if (tb[LWTUNNEL_IP_TTL])
359                 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
360
361         if (tb[LWTUNNEL_IP_TOS])
362                 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
363
364         if (tb[LWTUNNEL_IP_FLAGS])
365                 tun_info->key.tun_flags |= nla_get_be16(tb[LWTUNNEL_IP_FLAGS]);
366
367         tun_info->mode = IP_TUNNEL_INFO_TX;
368         tun_info->options_len = opt_len;
369
370         *ts = new_state;
371
372         return 0;
373 }
374
375 static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
376 {
377 #ifdef CONFIG_DST_CACHE
378         struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
379
380         dst_cache_destroy(&tun_info->dst_cache);
381 #endif
382 }
383
384 static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb,
385                                          struct ip_tunnel_info *tun_info)
386 {
387         struct geneve_opt *opt;
388         struct nlattr *nest;
389
390         nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE);
391         if (!nest)
392                 return -ENOMEM;
393
394         opt = ip_tunnel_info_opts(tun_info);
395         if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS, opt->opt_class) ||
396             nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) ||
397             nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4,
398                     opt->opt_data)) {
399                 nla_nest_cancel(skb, nest);
400                 return -ENOMEM;
401         }
402
403         nla_nest_end(skb, nest);
404         return 0;
405 }
406
407 static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type,
408                                   struct ip_tunnel_info *tun_info)
409 {
410         struct nlattr *nest;
411         int err = 0;
412
413         if (!(tun_info->key.tun_flags & TUNNEL_GENEVE_OPT))
414                 return 0;
415
416         nest = nla_nest_start_noflag(skb, type);
417         if (!nest)
418                 return -ENOMEM;
419
420         if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT)
421                 err = ip_tun_fill_encap_opts_geneve(skb, tun_info);
422
423         if (err) {
424                 nla_nest_cancel(skb, nest);
425                 return err;
426         }
427
428         nla_nest_end(skb, nest);
429         return 0;
430 }
431
432 static int ip_tun_fill_encap_info(struct sk_buff *skb,
433                                   struct lwtunnel_state *lwtstate)
434 {
435         struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
436
437         if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
438                          LWTUNNEL_IP_PAD) ||
439             nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
440             nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
441             nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
442             nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
443             nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) ||
444             ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info))
445                 return -ENOMEM;
446
447         return 0;
448 }
449
450 static int ip_tun_opts_nlsize(struct ip_tunnel_info *info)
451 {
452         int opt_len;
453
454         if (!(info->key.tun_flags & TUNNEL_GENEVE_OPT))
455                 return 0;
456
457         opt_len = nla_total_size(0);            /* LWTUNNEL_IP_OPTS */
458         if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
459                 struct geneve_opt *opt = ip_tunnel_info_opts(info);
460
461                 opt_len += nla_total_size(0)    /* LWTUNNEL_IP_OPTS_GENEVE */
462                            + nla_total_size(2)  /* OPT_GENEVE_CLASS */
463                            + nla_total_size(1)  /* OPT_GENEVE_TYPE */
464                            + nla_total_size(opt->length * 4);
465                                                 /* OPT_GENEVE_DATA */
466         }
467
468         return opt_len;
469 }
470
471 static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
472 {
473         return nla_total_size_64bit(8)  /* LWTUNNEL_IP_ID */
474                 + nla_total_size(4)     /* LWTUNNEL_IP_DST */
475                 + nla_total_size(4)     /* LWTUNNEL_IP_SRC */
476                 + nla_total_size(1)     /* LWTUNNEL_IP_TOS */
477                 + nla_total_size(1)     /* LWTUNNEL_IP_TTL */
478                 + nla_total_size(2)     /* LWTUNNEL_IP_FLAGS */
479                 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
480                                         /* LWTUNNEL_IP_OPTS */
481 }
482
483 static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
484 {
485         struct ip_tunnel_info *info_a = lwt_tun_info(a);
486         struct ip_tunnel_info *info_b = lwt_tun_info(b);
487
488         return memcmp(info_a, info_b, sizeof(info_a->key)) ||
489                info_a->mode != info_b->mode ||
490                info_a->options_len != info_b->options_len ||
491                memcmp(ip_tunnel_info_opts(info_a),
492                       ip_tunnel_info_opts(info_b), info_a->options_len);
493 }
494
495 static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
496         .build_state = ip_tun_build_state,
497         .destroy_state = ip_tun_destroy_state,
498         .fill_encap = ip_tun_fill_encap_info,
499         .get_encap_size = ip_tun_encap_nlsize,
500         .cmp_encap = ip_tun_cmp_encap,
501         .owner = THIS_MODULE,
502 };
503
504 static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
505         [LWTUNNEL_IP6_ID]               = { .type = NLA_U64 },
506         [LWTUNNEL_IP6_DST]              = { .len = sizeof(struct in6_addr) },
507         [LWTUNNEL_IP6_SRC]              = { .len = sizeof(struct in6_addr) },
508         [LWTUNNEL_IP6_HOPLIMIT]         = { .type = NLA_U8 },
509         [LWTUNNEL_IP6_TC]               = { .type = NLA_U8 },
510         [LWTUNNEL_IP6_FLAGS]            = { .type = NLA_U16 },
511 };
512
513 static int ip6_tun_build_state(struct nlattr *attr,
514                                unsigned int family, const void *cfg,
515                                struct lwtunnel_state **ts,
516                                struct netlink_ext_ack *extack)
517 {
518         struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
519         struct lwtunnel_state *new_state;
520         struct ip_tunnel_info *tun_info;
521         int err, opt_len;
522
523         err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
524                                           ip6_tun_policy, extack);
525         if (err < 0)
526                 return err;
527
528         opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack);
529         if (opt_len < 0)
530                 return opt_len;
531
532         new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
533         if (!new_state)
534                 return -ENOMEM;
535
536         new_state->type = LWTUNNEL_ENCAP_IP6;
537
538         tun_info = lwt_tun_info(new_state);
539
540         err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack);
541         if (err < 0) {
542                 lwtstate_free(new_state);
543                 return err;
544         }
545
546         if (tb[LWTUNNEL_IP6_ID])
547                 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
548
549         if (tb[LWTUNNEL_IP6_DST])
550                 tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
551
552         if (tb[LWTUNNEL_IP6_SRC])
553                 tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
554
555         if (tb[LWTUNNEL_IP6_HOPLIMIT])
556                 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
557
558         if (tb[LWTUNNEL_IP6_TC])
559                 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
560
561         if (tb[LWTUNNEL_IP6_FLAGS])
562                 tun_info->key.tun_flags |= nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]);
563
564         tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
565         tun_info->options_len = opt_len;
566
567         *ts = new_state;
568
569         return 0;
570 }
571
572 static int ip6_tun_fill_encap_info(struct sk_buff *skb,
573                                    struct lwtunnel_state *lwtstate)
574 {
575         struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
576
577         if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
578                          LWTUNNEL_IP6_PAD) ||
579             nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
580             nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
581             nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
582             nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
583             nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) ||
584             ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info))
585                 return -ENOMEM;
586
587         return 0;
588 }
589
590 static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
591 {
592         return nla_total_size_64bit(8)  /* LWTUNNEL_IP6_ID */
593                 + nla_total_size(16)    /* LWTUNNEL_IP6_DST */
594                 + nla_total_size(16)    /* LWTUNNEL_IP6_SRC */
595                 + nla_total_size(1)     /* LWTUNNEL_IP6_HOPLIMIT */
596                 + nla_total_size(1)     /* LWTUNNEL_IP6_TC */
597                 + nla_total_size(2)     /* LWTUNNEL_IP6_FLAGS */
598                 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
599                                         /* LWTUNNEL_IP6_OPTS */
600 }
601
602 static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
603         .build_state = ip6_tun_build_state,
604         .fill_encap = ip6_tun_fill_encap_info,
605         .get_encap_size = ip6_tun_encap_nlsize,
606         .cmp_encap = ip_tun_cmp_encap,
607         .owner = THIS_MODULE,
608 };
609
610 void __init ip_tunnel_core_init(void)
611 {
612         /* If you land here, make sure whether increasing ip_tunnel_info's
613          * options_len is a reasonable choice with its usage in front ends
614          * (f.e., it's part of flow keys, etc).
615          */
616         BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
617
618         lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
619         lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
620 }
621
622 DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
623 EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
624
625 void ip_tunnel_need_metadata(void)
626 {
627         static_branch_inc(&ip_tunnel_metadata_cnt);
628 }
629 EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
630
631 void ip_tunnel_unneed_metadata(void)
632 {
633         static_branch_dec(&ip_tunnel_metadata_cnt);
634 }
635 EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);