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