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.
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>
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;
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:
44 case TCA_TUNNEL_KEY_ACT_SET:
46 skb_dst_set(skb, dst_clone(¶ms->tcft_enc_metadata->dst));
49 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
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,
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;
78 err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX,
79 nla, geneve_opt_policy, extack);
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");
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]);
93 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
97 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
101 opt_len = sizeof(struct geneve_opt) + data_len;
103 struct geneve_opt *opt = dst;
105 WARN_ON(dst_len < opt_len);
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 */
115 memcpy(opt + 1, data, data_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);
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,
149 NL_SET_ERR_MSG(extack, "Empty list of tunnel options");
154 NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes");
161 static int tunnel_key_get_opts_len(struct nlattr *nla,
162 struct netlink_ext_ack *extack)
164 return tunnel_key_copy_opts(nla, NULL, 0, extack);
167 static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info,
168 int opts_len, struct netlink_ext_ack *extack)
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),
178 return -EAFNOSUPPORT;
181 NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type");
186 static 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 },
193 [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
194 [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 },
195 [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NLA_NESTED },
196 [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NLA_U8 },
197 [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NLA_U8 },
200 static void tunnel_key_release_params(struct tcf_tunnel_key_params *p)
204 if (p->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
205 dst_release(&p->tcft_enc_metadata->dst);
210 static int tunnel_key_init(struct net *net, struct nlattr *nla,
211 struct nlattr *est, struct tc_action **a,
212 int ovr, int bind, bool rtnl_held,
213 struct netlink_ext_ack *extack)
215 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
216 struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
217 struct tcf_tunnel_key_params *params_new;
218 struct metadata_dst *metadata = NULL;
219 struct tc_tunnel_key *parm;
220 struct tcf_tunnel_key *t;
231 NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
235 err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy,
238 NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
242 if (!tb[TCA_TUNNEL_KEY_PARMS]) {
243 NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
247 parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
248 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
255 switch (parm->t_action) {
256 case TCA_TUNNEL_KEY_ACT_RELEASE:
258 case TCA_TUNNEL_KEY_ACT_SET:
259 if (tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
262 key32 = nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]);
263 key_id = key32_to_tunnel_id(key32);
267 flags |= TUNNEL_CSUM;
268 if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
269 nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
270 flags &= ~TUNNEL_CSUM;
272 if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
273 dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
275 if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
276 opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
285 if (tb[TCA_TUNNEL_KEY_ENC_TOS])
286 tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]);
288 if (tb[TCA_TUNNEL_KEY_ENC_TTL])
289 ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]);
291 if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
292 tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
296 saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
297 daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
299 metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl,
302 } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
303 tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
304 struct in6_addr saddr;
305 struct in6_addr daddr;
307 saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
308 daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
310 metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port,
314 NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
320 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
325 #ifdef CONFIG_DST_CACHE
326 ret = dst_cache_init(&metadata->u.tun_info.dst_cache, GFP_KERNEL);
328 goto release_tun_meta;
332 ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
333 &metadata->u.tun_info,
336 goto release_tun_meta;
339 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
342 NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
348 ret = tcf_idr_create(tn, parm->index, est, a,
349 &act_tunnel_key_ops, bind, true);
351 NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
352 goto release_tun_meta;
357 NL_SET_ERR_MSG(extack, "TC IDR already exists");
359 goto release_tun_meta;
362 t = to_tunnel_key(*a);
364 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
365 if (unlikely(!params_new)) {
366 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
369 goto release_tun_meta;
371 params_new->tcft_action = parm->t_action;
372 params_new->tcft_enc_metadata = metadata;
374 spin_lock_bh(&t->tcf_lock);
375 t->tcf_action = parm->action;
376 rcu_swap_protected(t->params, params_new,
377 lockdep_is_held(&t->tcf_lock));
378 spin_unlock_bh(&t->tcf_lock);
379 tunnel_key_release_params(params_new);
381 if (ret == ACT_P_CREATED)
382 tcf_idr_insert(tn, *a);
388 dst_release(&metadata->dst);
392 tcf_idr_release(*a, bind);
394 tcf_idr_cleanup(tn, parm->index);
398 static void tunnel_key_release(struct tc_action *a)
400 struct tcf_tunnel_key *t = to_tunnel_key(a);
401 struct tcf_tunnel_key_params *params;
403 params = rcu_dereference_protected(t->params, 1);
404 tunnel_key_release_params(params);
407 static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
408 const struct ip_tunnel_info *info)
410 int len = info->options_len;
411 u8 *src = (u8 *)(info + 1);
412 struct nlattr *start;
414 start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
419 struct geneve_opt *opt = (struct geneve_opt *)src;
421 if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
423 nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
425 nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
426 opt->length * 4, opt + 1)) {
427 nla_nest_cancel(skb, start);
431 len -= sizeof(struct geneve_opt) + opt->length * 4;
432 src += sizeof(struct geneve_opt) + opt->length * 4;
435 nla_nest_end(skb, start);
439 static int tunnel_key_opts_dump(struct sk_buff *skb,
440 const struct ip_tunnel_info *info)
442 struct nlattr *start;
445 if (!info->options_len)
448 start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS);
452 if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
453 err = tunnel_key_geneve_opts_dump(skb, info);
458 nla_nest_cancel(skb, start);
462 nla_nest_end(skb, start);
466 static int tunnel_key_dump_addresses(struct sk_buff *skb,
467 const struct ip_tunnel_info *info)
469 unsigned short family = ip_tunnel_info_af(info);
471 if (family == AF_INET) {
472 __be32 saddr = info->key.u.ipv4.src;
473 __be32 daddr = info->key.u.ipv4.dst;
475 if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
476 !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
480 if (family == AF_INET6) {
481 const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
482 const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
484 if (!nla_put_in6_addr(skb,
485 TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
486 !nla_put_in6_addr(skb,
487 TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
494 static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
497 unsigned char *b = skb_tail_pointer(skb);
498 struct tcf_tunnel_key *t = to_tunnel_key(a);
499 struct tcf_tunnel_key_params *params;
500 struct tc_tunnel_key opt = {
501 .index = t->tcf_index,
502 .refcnt = refcount_read(&t->tcf_refcnt) - ref,
503 .bindcnt = atomic_read(&t->tcf_bindcnt) - bind,
507 spin_lock_bh(&t->tcf_lock);
508 params = rcu_dereference_protected(t->params,
509 lockdep_is_held(&t->tcf_lock));
510 opt.action = t->tcf_action;
511 opt.t_action = params->tcft_action;
513 if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
514 goto nla_put_failure;
516 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
517 struct ip_tunnel_info *info =
518 ¶ms->tcft_enc_metadata->u.tun_info;
519 struct ip_tunnel_key *key = &info->key;
520 __be32 key_id = tunnel_id_to_key32(key->tun_id);
522 if (((key->tun_flags & TUNNEL_KEY) &&
523 nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id)) ||
524 tunnel_key_dump_addresses(skb,
525 ¶ms->tcft_enc_metadata->u.tun_info) ||
527 nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT,
529 nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
530 !(key->tun_flags & TUNNEL_CSUM)) ||
531 tunnel_key_opts_dump(skb, info))
532 goto nla_put_failure;
534 if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos))
535 goto nla_put_failure;
537 if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl))
538 goto nla_put_failure;
541 tcf_tm_dump(&tm, &t->tcf_tm);
542 if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
543 &tm, TCA_TUNNEL_KEY_PAD))
544 goto nla_put_failure;
545 spin_unlock_bh(&t->tcf_lock);
550 spin_unlock_bh(&t->tcf_lock);
555 static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
556 struct netlink_callback *cb, int type,
557 const struct tc_action_ops *ops,
558 struct netlink_ext_ack *extack)
560 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
562 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
565 static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index)
567 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
569 return tcf_idr_search(tn, a, index);
572 static struct tc_action_ops act_tunnel_key_ops = {
573 .kind = "tunnel_key",
574 .id = TCA_ID_TUNNEL_KEY,
575 .owner = THIS_MODULE,
576 .act = tunnel_key_act,
577 .dump = tunnel_key_dump,
578 .init = tunnel_key_init,
579 .cleanup = tunnel_key_release,
580 .walk = tunnel_key_walker,
581 .lookup = tunnel_key_search,
582 .size = sizeof(struct tcf_tunnel_key),
585 static __net_init int tunnel_key_init_net(struct net *net)
587 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
589 return tc_action_net_init(tn, &act_tunnel_key_ops);
592 static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
594 tc_action_net_exit(net_list, tunnel_key_net_id);
597 static struct pernet_operations tunnel_key_net_ops = {
598 .init = tunnel_key_init_net,
599 .exit_batch = tunnel_key_exit_net,
600 .id = &tunnel_key_net_id,
601 .size = sizeof(struct tc_action_net),
604 static int __init tunnel_key_init_module(void)
606 return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
609 static void __exit tunnel_key_cleanup_module(void)
611 tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
614 module_init(tunnel_key_init_module);
615 module_exit(tunnel_key_cleanup_module);
617 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
618 MODULE_DESCRIPTION("ip tunnel manipulation actions");
619 MODULE_LICENSE("GPL v2");