Linux 4.19.168
[linux/fpc-iii.git] / net / sched / act_tunnel_key.c
blobf43234be5695e57eab0a1c71e22c3e0f92c60971
1 /*
2 * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
3 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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 */
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>
16 #include <net/geneve.h>
17 #include <net/netlink.h>
18 #include <net/pkt_sched.h>
19 #include <net/dst.h>
21 #include <linux/tc_act/tc_tunnel_key.h>
22 #include <net/tc_act/tc_tunnel_key.h>
24 static unsigned int tunnel_key_net_id;
25 static struct tc_action_ops act_tunnel_key_ops;
27 static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
28 struct tcf_result *res)
30 struct tcf_tunnel_key *t = to_tunnel_key(a);
31 struct tcf_tunnel_key_params *params;
32 int action;
34 params = rcu_dereference_bh(t->params);
36 tcf_lastuse_update(&t->tcf_tm);
37 bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb);
38 action = READ_ONCE(t->tcf_action);
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;
54 return action;
57 static const struct nla_policy
58 enc_opts_policy[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1] = {
59 [TCA_TUNNEL_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
62 static const struct nla_policy
63 geneve_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 },
70 static int
71 tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len,
72 struct netlink_ext_ack *extack)
74 struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
75 int err, data_len, opt_len;
76 u8 *data;
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;
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;
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;
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;
101 opt_len = sizeof(struct geneve_opt) + data_len;
102 if (dst) {
103 struct geneve_opt *opt = dst;
105 WARN_ON(dst_len < opt_len);
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;
115 memcpy(opt + 1, data, data_len);
118 return opt_len;
121 static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst,
122 int dst_len, struct netlink_ext_ack *extack)
124 int err, rem, opt_len, len = nla_len(nla), opts_len = 0;
125 const struct nlattr *attr, *head = nla_data(nla);
127 err = nla_validate(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX,
128 enc_opts_policy, extack);
129 if (err)
130 return err;
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 (opts_len > IP_TUNNEL_OPTS_MAX) {
141 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
142 return -EINVAL;
144 if (dst) {
145 dst_len -= opt_len;
146 dst += opt_len;
148 break;
152 if (!opts_len) {
153 NL_SET_ERR_MSG(extack, "Empty list of tunnel options");
154 return -EINVAL;
157 if (rem > 0) {
158 NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes");
159 return -EINVAL;
162 return opts_len;
165 static int tunnel_key_get_opts_len(struct nlattr *nla,
166 struct netlink_ext_ack *extack)
168 return tunnel_key_copy_opts(nla, NULL, 0, extack);
171 static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info,
172 int opts_len, struct netlink_ext_ack *extack)
174 info->options_len = opts_len;
175 switch (nla_type(nla_data(nla))) {
176 case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
177 #if IS_ENABLED(CONFIG_INET)
178 info->key.tun_flags |= TUNNEL_GENEVE_OPT;
179 return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
180 opts_len, extack);
181 #else
182 return -EAFNOSUPPORT;
183 #endif
184 default:
185 NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type");
186 return -EINVAL;
190 static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
191 [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) },
192 [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
193 [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
194 [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
195 [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
196 [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
197 [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
198 [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 },
199 [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NLA_NESTED },
200 [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NLA_U8 },
201 [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NLA_U8 },
204 static void tunnel_key_release_params(struct tcf_tunnel_key_params *p)
206 if (!p)
207 return;
208 if (p->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
209 dst_release(&p->tcft_enc_metadata->dst);
210 kfree_rcu(p, rcu);
213 static int tunnel_key_init(struct net *net, struct nlattr *nla,
214 struct nlattr *est, struct tc_action **a,
215 int ovr, int bind, bool rtnl_held,
216 struct netlink_ext_ack *extack)
218 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
219 struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
220 struct tcf_tunnel_key_params *params_new;
221 struct metadata_dst *metadata = NULL;
222 struct tc_tunnel_key *parm;
223 struct tcf_tunnel_key *t;
224 bool exists = false;
225 __be16 dst_port = 0;
226 int opts_len = 0;
227 __be64 key_id;
228 __be16 flags;
229 u8 tos, ttl;
230 int ret = 0;
231 u32 index;
232 int err;
234 if (!nla) {
235 NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
236 return -EINVAL;
239 err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy,
240 extack);
241 if (err < 0) {
242 NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
243 return err;
246 if (!tb[TCA_TUNNEL_KEY_PARMS]) {
247 NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
248 return -EINVAL;
251 parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
252 index = parm->index;
253 err = tcf_idr_check_alloc(tn, &index, a, bind);
254 if (err < 0)
255 return err;
256 exists = err;
257 if (exists && bind)
258 return 0;
260 switch (parm->t_action) {
261 case TCA_TUNNEL_KEY_ACT_RELEASE:
262 break;
263 case TCA_TUNNEL_KEY_ACT_SET:
264 if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
265 NL_SET_ERR_MSG(extack, "Missing tunnel key id");
266 ret = -EINVAL;
267 goto err_out;
270 key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]));
272 flags = TUNNEL_KEY | TUNNEL_CSUM;
273 if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
274 nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
275 flags &= ~TUNNEL_CSUM;
277 if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
278 dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
280 if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
281 opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
282 extack);
283 if (opts_len < 0) {
284 ret = opts_len;
285 goto err_out;
289 tos = 0;
290 if (tb[TCA_TUNNEL_KEY_ENC_TOS])
291 tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]);
292 ttl = 0;
293 if (tb[TCA_TUNNEL_KEY_ENC_TTL])
294 ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]);
296 if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
297 tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
298 __be32 saddr;
299 __be32 daddr;
301 saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
302 daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
304 metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl,
305 dst_port, flags,
306 key_id, opts_len);
307 } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
308 tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
309 struct in6_addr saddr;
310 struct in6_addr daddr;
312 saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
313 daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
315 metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port,
316 0, flags,
317 key_id, opts_len);
318 } else {
319 NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
320 ret = -EINVAL;
321 goto err_out;
324 if (!metadata) {
325 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
326 ret = -ENOMEM;
327 goto err_out;
330 if (opts_len) {
331 ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
332 &metadata->u.tun_info,
333 opts_len, extack);
334 if (ret < 0)
335 goto release_tun_meta;
338 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
339 break;
340 default:
341 NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
342 ret = -EINVAL;
343 goto err_out;
346 if (!exists) {
347 ret = tcf_idr_create(tn, index, est, a,
348 &act_tunnel_key_ops, bind, true);
349 if (ret) {
350 NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
351 goto release_tun_meta;
354 ret = ACT_P_CREATED;
355 } else if (!ovr) {
356 NL_SET_ERR_MSG(extack, "TC IDR already exists");
357 ret = -EEXIST;
358 goto release_tun_meta;
361 t = to_tunnel_key(*a);
363 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
364 if (unlikely(!params_new)) {
365 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
366 ret = -ENOMEM;
367 exists = true;
368 goto release_tun_meta;
370 params_new->tcft_action = parm->t_action;
371 params_new->tcft_enc_metadata = metadata;
373 spin_lock_bh(&t->tcf_lock);
374 t->tcf_action = parm->action;
375 rcu_swap_protected(t->params, params_new,
376 lockdep_is_held(&t->tcf_lock));
377 spin_unlock_bh(&t->tcf_lock);
378 tunnel_key_release_params(params_new);
380 if (ret == ACT_P_CREATED)
381 tcf_idr_insert(tn, *a);
383 return ret;
385 release_tun_meta:
386 if (metadata)
387 dst_release(&metadata->dst);
389 err_out:
390 if (exists)
391 tcf_idr_release(*a, bind);
392 else
393 tcf_idr_cleanup(tn, index);
394 return ret;
397 static void tunnel_key_release(struct tc_action *a)
399 struct tcf_tunnel_key *t = to_tunnel_key(a);
400 struct tcf_tunnel_key_params *params;
402 params = rcu_dereference_protected(t->params, 1);
403 tunnel_key_release_params(params);
406 static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
407 const struct ip_tunnel_info *info)
409 int len = info->options_len;
410 u8 *src = (u8 *)(info + 1);
411 struct nlattr *start;
413 start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
414 if (!start)
415 return -EMSGSIZE;
417 while (len > 0) {
418 struct geneve_opt *opt = (struct geneve_opt *)src;
420 if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
421 opt->opt_class) ||
422 nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
423 opt->type) ||
424 nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
425 opt->length * 4, opt + 1)) {
426 nla_nest_cancel(skb, start);
427 return -EMSGSIZE;
430 len -= sizeof(struct geneve_opt) + opt->length * 4;
431 src += sizeof(struct geneve_opt) + opt->length * 4;
434 nla_nest_end(skb, start);
435 return 0;
438 static int tunnel_key_opts_dump(struct sk_buff *skb,
439 const struct ip_tunnel_info *info)
441 struct nlattr *start;
442 int err = -EINVAL;
444 if (!info->options_len)
445 return 0;
447 start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS);
448 if (!start)
449 return -EMSGSIZE;
451 if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
452 err = tunnel_key_geneve_opts_dump(skb, info);
453 if (err)
454 goto err_out;
455 } else {
456 err_out:
457 nla_nest_cancel(skb, start);
458 return err;
461 nla_nest_end(skb, start);
462 return 0;
465 static int tunnel_key_dump_addresses(struct sk_buff *skb,
466 const struct ip_tunnel_info *info)
468 unsigned short family = ip_tunnel_info_af(info);
470 if (family == AF_INET) {
471 __be32 saddr = info->key.u.ipv4.src;
472 __be32 daddr = info->key.u.ipv4.dst;
474 if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
475 !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
476 return 0;
479 if (family == AF_INET6) {
480 const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
481 const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
483 if (!nla_put_in6_addr(skb,
484 TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
485 !nla_put_in6_addr(skb,
486 TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
487 return 0;
490 return -EINVAL;
493 static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
494 int bind, int ref)
496 unsigned char *b = skb_tail_pointer(skb);
497 struct tcf_tunnel_key *t = to_tunnel_key(a);
498 struct tcf_tunnel_key_params *params;
499 struct tc_tunnel_key opt = {
500 .index = t->tcf_index,
501 .refcnt = refcount_read(&t->tcf_refcnt) - ref,
502 .bindcnt = atomic_read(&t->tcf_bindcnt) - bind,
504 struct tcf_t tm;
506 spin_lock_bh(&t->tcf_lock);
507 params = rcu_dereference_protected(t->params,
508 lockdep_is_held(&t->tcf_lock));
509 opt.action = t->tcf_action;
510 opt.t_action = params->tcft_action;
512 if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
513 goto nla_put_failure;
515 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
516 struct ip_tunnel_info *info =
517 &params->tcft_enc_metadata->u.tun_info;
518 struct ip_tunnel_key *key = &info->key;
519 __be32 key_id = tunnel_id_to_key32(key->tun_id);
521 if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
522 tunnel_key_dump_addresses(skb,
523 &params->tcft_enc_metadata->u.tun_info) ||
524 nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) ||
525 nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
526 !(key->tun_flags & TUNNEL_CSUM)) ||
527 tunnel_key_opts_dump(skb, info))
528 goto nla_put_failure;
530 if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos))
531 goto nla_put_failure;
533 if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl))
534 goto nla_put_failure;
537 tcf_tm_dump(&tm, &t->tcf_tm);
538 if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
539 &tm, TCA_TUNNEL_KEY_PAD))
540 goto nla_put_failure;
541 spin_unlock_bh(&t->tcf_lock);
543 return skb->len;
545 nla_put_failure:
546 spin_unlock_bh(&t->tcf_lock);
547 nlmsg_trim(skb, b);
548 return -1;
551 static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
552 struct netlink_callback *cb, int type,
553 const struct tc_action_ops *ops,
554 struct netlink_ext_ack *extack)
556 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
558 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
561 static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index,
562 struct netlink_ext_ack *extack)
564 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
566 return tcf_idr_search(tn, a, index);
569 static struct tc_action_ops act_tunnel_key_ops = {
570 .kind = "tunnel_key",
571 .type = TCA_ACT_TUNNEL_KEY,
572 .owner = THIS_MODULE,
573 .act = tunnel_key_act,
574 .dump = tunnel_key_dump,
575 .init = tunnel_key_init,
576 .cleanup = tunnel_key_release,
577 .walk = tunnel_key_walker,
578 .lookup = tunnel_key_search,
579 .size = sizeof(struct tcf_tunnel_key),
582 static __net_init int tunnel_key_init_net(struct net *net)
584 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
586 return tc_action_net_init(net, tn, &act_tunnel_key_ops);
589 static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
591 tc_action_net_exit(net_list, tunnel_key_net_id);
594 static struct pernet_operations tunnel_key_net_ops = {
595 .init = tunnel_key_init_net,
596 .exit_batch = tunnel_key_exit_net,
597 .id = &tunnel_key_net_id,
598 .size = sizeof(struct tc_action_net),
601 static int __init tunnel_key_init_module(void)
603 return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
606 static void __exit tunnel_key_cleanup_module(void)
608 tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
611 module_init(tunnel_key_init_module);
612 module_exit(tunnel_key_cleanup_module);
614 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
615 MODULE_DESCRIPTION("ip tunnel manipulation actions");
616 MODULE_LICENSE("GPL v2");