net: sched: add vxlan option support to act_tunnel_key
[linux-2.6-microblaze.git] / net / sched / act_tunnel_key.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
4  * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
5  */
6
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/skbuff.h>
11 #include <linux/rtnetlink.h>
12 #include <net/geneve.h>
13 #include <net/vxlan.h>
14 #include <net/netlink.h>
15 #include <net/pkt_sched.h>
16 #include <net/dst.h>
17 #include <net/pkt_cls.h>
18
19 #include <linux/tc_act/tc_tunnel_key.h>
20 #include <net/tc_act/tc_tunnel_key.h>
21
22 static unsigned int tunnel_key_net_id;
23 static struct tc_action_ops act_tunnel_key_ops;
24
25 static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
26                           struct tcf_result *res)
27 {
28         struct tcf_tunnel_key *t = to_tunnel_key(a);
29         struct tcf_tunnel_key_params *params;
30         int action;
31
32         params = rcu_dereference_bh(t->params);
33
34         tcf_lastuse_update(&t->tcf_tm);
35         tcf_action_update_bstats(&t->common, skb);
36         action = READ_ONCE(t->tcf_action);
37
38         switch (params->tcft_action) {
39         case TCA_TUNNEL_KEY_ACT_RELEASE:
40                 skb_dst_drop(skb);
41                 break;
42         case TCA_TUNNEL_KEY_ACT_SET:
43                 skb_dst_drop(skb);
44                 skb_dst_set(skb, dst_clone(&params->tcft_enc_metadata->dst));
45                 break;
46         default:
47                 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
48                           params->tcft_action);
49                 break;
50         }
51
52         return action;
53 }
54
55 static const struct nla_policy
56 enc_opts_policy[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1] = {
57         [TCA_TUNNEL_KEY_ENC_OPTS_UNSPEC]        = {
58                 .strict_start_type = TCA_TUNNEL_KEY_ENC_OPTS_VXLAN },
59         [TCA_TUNNEL_KEY_ENC_OPTS_GENEVE]        = { .type = NLA_NESTED },
60         [TCA_TUNNEL_KEY_ENC_OPTS_VXLAN]         = { .type = NLA_NESTED },
61 };
62
63 static const struct nla_policy
64 geneve_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1] = {
65         [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]      = { .type = NLA_U16 },
66         [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]       = { .type = NLA_U8 },
67         [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]       = { .type = NLA_BINARY,
68                                                        .len = 128 },
69 };
70
71 static const struct nla_policy
72 vxlan_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1] = {
73         [TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]         = { .type = NLA_U32 },
74 };
75
76 static int
77 tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len,
78                            struct netlink_ext_ack *extack)
79 {
80         struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
81         int err, data_len, opt_len;
82         u8 *data;
83
84         err = nla_parse_nested_deprecated(tb,
85                                           TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX,
86                                           nla, geneve_opt_policy, extack);
87         if (err < 0)
88                 return err;
89
90         if (!tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] ||
91             !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] ||
92             !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]) {
93                 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
94                 return -EINVAL;
95         }
96
97         data = nla_data(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
98         data_len = nla_len(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
99         if (data_len < 4) {
100                 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
101                 return -ERANGE;
102         }
103         if (data_len % 4) {
104                 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
105                 return -ERANGE;
106         }
107
108         opt_len = sizeof(struct geneve_opt) + data_len;
109         if (dst) {
110                 struct geneve_opt *opt = dst;
111
112                 WARN_ON(dst_len < opt_len);
113
114                 opt->opt_class =
115                         nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]);
116                 opt->type = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]);
117                 opt->length = data_len / 4; /* length is in units of 4 bytes */
118                 opt->r1 = 0;
119                 opt->r2 = 0;
120                 opt->r3 = 0;
121
122                 memcpy(opt + 1, data, data_len);
123         }
124
125         return opt_len;
126 }
127
128 static int
129 tunnel_key_copy_vxlan_opt(const struct nlattr *nla, void *dst, int dst_len,
130                           struct netlink_ext_ack *extack)
131 {
132         struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1];
133         int err;
134
135         err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX, nla,
136                                vxlan_opt_policy, extack);
137         if (err < 0)
138                 return err;
139
140         if (!tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]) {
141                 NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
142                 return -EINVAL;
143         }
144
145         if (dst) {
146                 struct vxlan_metadata *md = dst;
147
148                 md->gbp = nla_get_u32(tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]);
149         }
150
151         return sizeof(struct vxlan_metadata);
152 }
153
154 static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst,
155                                 int dst_len, struct netlink_ext_ack *extack)
156 {
157         int err, rem, opt_len, len = nla_len(nla), opts_len = 0, type = 0;
158         const struct nlattr *attr, *head = nla_data(nla);
159
160         err = nla_validate_deprecated(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX,
161                                       enc_opts_policy, extack);
162         if (err)
163                 return err;
164
165         nla_for_each_attr(attr, head, len, rem) {
166                 switch (nla_type(attr)) {
167                 case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
168                         if (type && type != TUNNEL_GENEVE_OPT) {
169                                 NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
170                                 return -EINVAL;
171                         }
172                         opt_len = tunnel_key_copy_geneve_opt(attr, dst,
173                                                              dst_len, extack);
174                         if (opt_len < 0)
175                                 return opt_len;
176                         opts_len += opt_len;
177                         if (dst) {
178                                 dst_len -= opt_len;
179                                 dst += opt_len;
180                         }
181                         type = TUNNEL_GENEVE_OPT;
182                         break;
183                 case TCA_TUNNEL_KEY_ENC_OPTS_VXLAN:
184                         if (type) {
185                                 NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
186                                 return -EINVAL;
187                         }
188                         opt_len = tunnel_key_copy_vxlan_opt(attr, dst,
189                                                             dst_len, extack);
190                         if (opt_len < 0)
191                                 return opt_len;
192                         opts_len += opt_len;
193                         type = TUNNEL_VXLAN_OPT;
194                         break;
195                 }
196         }
197
198         if (!opts_len) {
199                 NL_SET_ERR_MSG(extack, "Empty list of tunnel options");
200                 return -EINVAL;
201         }
202
203         if (rem > 0) {
204                 NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes");
205                 return -EINVAL;
206         }
207
208         return opts_len;
209 }
210
211 static int tunnel_key_get_opts_len(struct nlattr *nla,
212                                    struct netlink_ext_ack *extack)
213 {
214         return tunnel_key_copy_opts(nla, NULL, 0, extack);
215 }
216
217 static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info,
218                                int opts_len, struct netlink_ext_ack *extack)
219 {
220         info->options_len = opts_len;
221         switch (nla_type(nla_data(nla))) {
222         case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
223 #if IS_ENABLED(CONFIG_INET)
224                 info->key.tun_flags |= TUNNEL_GENEVE_OPT;
225                 return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
226                                             opts_len, extack);
227 #else
228                 return -EAFNOSUPPORT;
229 #endif
230         case TCA_TUNNEL_KEY_ENC_OPTS_VXLAN:
231 #if IS_ENABLED(CONFIG_INET)
232                 info->key.tun_flags |= TUNNEL_VXLAN_OPT;
233                 return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
234                                             opts_len, extack);
235 #else
236                 return -EAFNOSUPPORT;
237 #endif
238         default:
239                 NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type");
240                 return -EINVAL;
241         }
242 }
243
244 static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
245         [TCA_TUNNEL_KEY_PARMS]      = { .len = sizeof(struct tc_tunnel_key) },
246         [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
247         [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
248         [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
249         [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
250         [TCA_TUNNEL_KEY_ENC_KEY_ID]   = { .type = NLA_U32 },
251         [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
252         [TCA_TUNNEL_KEY_NO_CSUM]      = { .type = NLA_U8 },
253         [TCA_TUNNEL_KEY_ENC_OPTS]     = { .type = NLA_NESTED },
254         [TCA_TUNNEL_KEY_ENC_TOS]      = { .type = NLA_U8 },
255         [TCA_TUNNEL_KEY_ENC_TTL]      = { .type = NLA_U8 },
256 };
257
258 static void tunnel_key_release_params(struct tcf_tunnel_key_params *p)
259 {
260         if (!p)
261                 return;
262         if (p->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
263                 dst_release(&p->tcft_enc_metadata->dst);
264
265         kfree_rcu(p, rcu);
266 }
267
268 static int tunnel_key_init(struct net *net, struct nlattr *nla,
269                            struct nlattr *est, struct tc_action **a,
270                            int ovr, int bind, bool rtnl_held,
271                            struct tcf_proto *tp, u32 act_flags,
272                            struct netlink_ext_ack *extack)
273 {
274         struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
275         struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
276         struct tcf_tunnel_key_params *params_new;
277         struct metadata_dst *metadata = NULL;
278         struct tcf_chain *goto_ch = NULL;
279         struct tc_tunnel_key *parm;
280         struct tcf_tunnel_key *t;
281         bool exists = false;
282         __be16 dst_port = 0;
283         __be64 key_id = 0;
284         int opts_len = 0;
285         __be16 flags = 0;
286         u8 tos, ttl;
287         int ret = 0;
288         u32 index;
289         int err;
290
291         if (!nla) {
292                 NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
293                 return -EINVAL;
294         }
295
296         err = nla_parse_nested_deprecated(tb, TCA_TUNNEL_KEY_MAX, nla,
297                                           tunnel_key_policy, extack);
298         if (err < 0) {
299                 NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
300                 return err;
301         }
302
303         if (!tb[TCA_TUNNEL_KEY_PARMS]) {
304                 NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
305                 return -EINVAL;
306         }
307
308         parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
309         index = parm->index;
310         err = tcf_idr_check_alloc(tn, &index, a, bind);
311         if (err < 0)
312                 return err;
313         exists = err;
314         if (exists && bind)
315                 return 0;
316
317         switch (parm->t_action) {
318         case TCA_TUNNEL_KEY_ACT_RELEASE:
319                 break;
320         case TCA_TUNNEL_KEY_ACT_SET:
321                 if (tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
322                         __be32 key32;
323
324                         key32 = nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]);
325                         key_id = key32_to_tunnel_id(key32);
326                         flags = TUNNEL_KEY;
327                 }
328
329                 flags |= TUNNEL_CSUM;
330                 if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
331                     nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
332                         flags &= ~TUNNEL_CSUM;
333
334                 if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
335                         dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
336
337                 if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
338                         opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
339                                                            extack);
340                         if (opts_len < 0) {
341                                 ret = opts_len;
342                                 goto err_out;
343                         }
344                 }
345
346                 tos = 0;
347                 if (tb[TCA_TUNNEL_KEY_ENC_TOS])
348                         tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]);
349                 ttl = 0;
350                 if (tb[TCA_TUNNEL_KEY_ENC_TTL])
351                         ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]);
352
353                 if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
354                     tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
355                         __be32 saddr;
356                         __be32 daddr;
357
358                         saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
359                         daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
360
361                         metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl,
362                                                     dst_port, flags,
363                                                     key_id, opts_len);
364                 } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
365                            tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
366                         struct in6_addr saddr;
367                         struct in6_addr daddr;
368
369                         saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
370                         daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
371
372                         metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port,
373                                                       0, flags,
374                                                       key_id, 0);
375                 } else {
376                         NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
377                         ret = -EINVAL;
378                         goto err_out;
379                 }
380
381                 if (!metadata) {
382                         NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
383                         ret = -ENOMEM;
384                         goto err_out;
385                 }
386
387 #ifdef CONFIG_DST_CACHE
388                 ret = dst_cache_init(&metadata->u.tun_info.dst_cache, GFP_KERNEL);
389                 if (ret)
390                         goto release_tun_meta;
391 #endif
392
393                 if (opts_len) {
394                         ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
395                                                   &metadata->u.tun_info,
396                                                   opts_len, extack);
397                         if (ret < 0)
398                                 goto release_tun_meta;
399                 }
400
401                 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
402                 break;
403         default:
404                 NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
405                 ret = -EINVAL;
406                 goto err_out;
407         }
408
409         if (!exists) {
410                 ret = tcf_idr_create_from_flags(tn, index, est, a,
411                                                 &act_tunnel_key_ops, bind,
412                                                 act_flags);
413                 if (ret) {
414                         NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
415                         goto release_tun_meta;
416                 }
417
418                 ret = ACT_P_CREATED;
419         } else if (!ovr) {
420                 NL_SET_ERR_MSG(extack, "TC IDR already exists");
421                 ret = -EEXIST;
422                 goto release_tun_meta;
423         }
424
425         err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
426         if (err < 0) {
427                 ret = err;
428                 exists = true;
429                 goto release_tun_meta;
430         }
431         t = to_tunnel_key(*a);
432
433         params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
434         if (unlikely(!params_new)) {
435                 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
436                 ret = -ENOMEM;
437                 exists = true;
438                 goto put_chain;
439         }
440         params_new->tcft_action = parm->t_action;
441         params_new->tcft_enc_metadata = metadata;
442
443         spin_lock_bh(&t->tcf_lock);
444         goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
445         rcu_swap_protected(t->params, params_new,
446                            lockdep_is_held(&t->tcf_lock));
447         spin_unlock_bh(&t->tcf_lock);
448         tunnel_key_release_params(params_new);
449         if (goto_ch)
450                 tcf_chain_put_by_act(goto_ch);
451
452         if (ret == ACT_P_CREATED)
453                 tcf_idr_insert(tn, *a);
454
455         return ret;
456
457 put_chain:
458         if (goto_ch)
459                 tcf_chain_put_by_act(goto_ch);
460
461 release_tun_meta:
462         if (metadata)
463                 dst_release(&metadata->dst);
464
465 err_out:
466         if (exists)
467                 tcf_idr_release(*a, bind);
468         else
469                 tcf_idr_cleanup(tn, index);
470         return ret;
471 }
472
473 static void tunnel_key_release(struct tc_action *a)
474 {
475         struct tcf_tunnel_key *t = to_tunnel_key(a);
476         struct tcf_tunnel_key_params *params;
477
478         params = rcu_dereference_protected(t->params, 1);
479         tunnel_key_release_params(params);
480 }
481
482 static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
483                                        const struct ip_tunnel_info *info)
484 {
485         int len = info->options_len;
486         u8 *src = (u8 *)(info + 1);
487         struct nlattr *start;
488
489         start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
490         if (!start)
491                 return -EMSGSIZE;
492
493         while (len > 0) {
494                 struct geneve_opt *opt = (struct geneve_opt *)src;
495
496                 if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
497                                  opt->opt_class) ||
498                     nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
499                                opt->type) ||
500                     nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
501                             opt->length * 4, opt + 1)) {
502                         nla_nest_cancel(skb, start);
503                         return -EMSGSIZE;
504                 }
505
506                 len -= sizeof(struct geneve_opt) + opt->length * 4;
507                 src += sizeof(struct geneve_opt) + opt->length * 4;
508         }
509
510         nla_nest_end(skb, start);
511         return 0;
512 }
513
514 static int tunnel_key_vxlan_opts_dump(struct sk_buff *skb,
515                                       const struct ip_tunnel_info *info)
516 {
517         struct vxlan_metadata *md = (struct vxlan_metadata *)(info + 1);
518         struct nlattr *start;
519
520         start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_VXLAN);
521         if (!start)
522                 return -EMSGSIZE;
523
524         if (nla_put_u32(skb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP, md->gbp)) {
525                 nla_nest_cancel(skb, start);
526                 return -EMSGSIZE;
527         }
528
529         nla_nest_end(skb, start);
530         return 0;
531 }
532
533 static int tunnel_key_opts_dump(struct sk_buff *skb,
534                                 const struct ip_tunnel_info *info)
535 {
536         struct nlattr *start;
537         int err = -EINVAL;
538
539         if (!info->options_len)
540                 return 0;
541
542         start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS);
543         if (!start)
544                 return -EMSGSIZE;
545
546         if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
547                 err = tunnel_key_geneve_opts_dump(skb, info);
548                 if (err)
549                         goto err_out;
550         } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
551                 err = tunnel_key_vxlan_opts_dump(skb, info);
552                 if (err)
553                         goto err_out;
554         } else {
555 err_out:
556                 nla_nest_cancel(skb, start);
557                 return err;
558         }
559
560         nla_nest_end(skb, start);
561         return 0;
562 }
563
564 static int tunnel_key_dump_addresses(struct sk_buff *skb,
565                                      const struct ip_tunnel_info *info)
566 {
567         unsigned short family = ip_tunnel_info_af(info);
568
569         if (family == AF_INET) {
570                 __be32 saddr = info->key.u.ipv4.src;
571                 __be32 daddr = info->key.u.ipv4.dst;
572
573                 if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
574                     !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
575                         return 0;
576         }
577
578         if (family == AF_INET6) {
579                 const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
580                 const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
581
582                 if (!nla_put_in6_addr(skb,
583                                       TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
584                     !nla_put_in6_addr(skb,
585                                       TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
586                         return 0;
587         }
588
589         return -EINVAL;
590 }
591
592 static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
593                            int bind, int ref)
594 {
595         unsigned char *b = skb_tail_pointer(skb);
596         struct tcf_tunnel_key *t = to_tunnel_key(a);
597         struct tcf_tunnel_key_params *params;
598         struct tc_tunnel_key opt = {
599                 .index    = t->tcf_index,
600                 .refcnt   = refcount_read(&t->tcf_refcnt) - ref,
601                 .bindcnt  = atomic_read(&t->tcf_bindcnt) - bind,
602         };
603         struct tcf_t tm;
604
605         spin_lock_bh(&t->tcf_lock);
606         params = rcu_dereference_protected(t->params,
607                                            lockdep_is_held(&t->tcf_lock));
608         opt.action   = t->tcf_action;
609         opt.t_action = params->tcft_action;
610
611         if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
612                 goto nla_put_failure;
613
614         if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
615                 struct ip_tunnel_info *info =
616                         &params->tcft_enc_metadata->u.tun_info;
617                 struct ip_tunnel_key *key = &info->key;
618                 __be32 key_id = tunnel_id_to_key32(key->tun_id);
619
620                 if (((key->tun_flags & TUNNEL_KEY) &&
621                      nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id)) ||
622                     tunnel_key_dump_addresses(skb,
623                                               &params->tcft_enc_metadata->u.tun_info) ||
624                     (key->tp_dst &&
625                       nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT,
626                                    key->tp_dst)) ||
627                     nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
628                                !(key->tun_flags & TUNNEL_CSUM)) ||
629                     tunnel_key_opts_dump(skb, info))
630                         goto nla_put_failure;
631
632                 if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos))
633                         goto nla_put_failure;
634
635                 if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl))
636                         goto nla_put_failure;
637         }
638
639         tcf_tm_dump(&tm, &t->tcf_tm);
640         if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
641                           &tm, TCA_TUNNEL_KEY_PAD))
642                 goto nla_put_failure;
643         spin_unlock_bh(&t->tcf_lock);
644
645         return skb->len;
646
647 nla_put_failure:
648         spin_unlock_bh(&t->tcf_lock);
649         nlmsg_trim(skb, b);
650         return -1;
651 }
652
653 static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
654                              struct netlink_callback *cb, int type,
655                              const struct tc_action_ops *ops,
656                              struct netlink_ext_ack *extack)
657 {
658         struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
659
660         return tcf_generic_walker(tn, skb, cb, type, ops, extack);
661 }
662
663 static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index)
664 {
665         struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
666
667         return tcf_idr_search(tn, a, index);
668 }
669
670 static struct tc_action_ops act_tunnel_key_ops = {
671         .kind           =       "tunnel_key",
672         .id             =       TCA_ID_TUNNEL_KEY,
673         .owner          =       THIS_MODULE,
674         .act            =       tunnel_key_act,
675         .dump           =       tunnel_key_dump,
676         .init           =       tunnel_key_init,
677         .cleanup        =       tunnel_key_release,
678         .walk           =       tunnel_key_walker,
679         .lookup         =       tunnel_key_search,
680         .size           =       sizeof(struct tcf_tunnel_key),
681 };
682
683 static __net_init int tunnel_key_init_net(struct net *net)
684 {
685         struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
686
687         return tc_action_net_init(net, tn, &act_tunnel_key_ops);
688 }
689
690 static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
691 {
692         tc_action_net_exit(net_list, tunnel_key_net_id);
693 }
694
695 static struct pernet_operations tunnel_key_net_ops = {
696         .init = tunnel_key_init_net,
697         .exit_batch = tunnel_key_exit_net,
698         .id   = &tunnel_key_net_id,
699         .size = sizeof(struct tc_action_net),
700 };
701
702 static int __init tunnel_key_init_module(void)
703 {
704         return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
705 }
706
707 static void __exit tunnel_key_cleanup_module(void)
708 {
709         tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
710 }
711
712 module_init(tunnel_key_init_module);
713 module_exit(tunnel_key_cleanup_module);
714
715 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
716 MODULE_DESCRIPTION("ip tunnel manipulation actions");
717 MODULE_LICENSE("GPL v2");