1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
4 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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>
17 #include <net/pkt_cls.h>
19 #include <linux/tc_act/tc_tunnel_key.h>
20 #include <net/tc_act/tc_tunnel_key.h>
22 static unsigned int tunnel_key_net_id;
23 static struct tc_action_ops act_tunnel_key_ops;
25 static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
26 struct tcf_result *res)
28 struct tcf_tunnel_key *t = to_tunnel_key(a);
29 struct tcf_tunnel_key_params *params;
32 params = rcu_dereference_bh(t->params);
34 tcf_lastuse_update(&t->tcf_tm);
35 tcf_action_update_bstats(&t->common, skb);
36 action = READ_ONCE(t->tcf_action);
38 switch (params->tcft_action) {
39 case TCA_TUNNEL_KEY_ACT_RELEASE:
42 case TCA_TUNNEL_KEY_ACT_SET:
44 skb_dst_set(skb, dst_clone(¶ms->tcft_enc_metadata->dst));
47 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
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 },
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,
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 },
77 tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len,
78 struct netlink_ext_ack *extack)
80 struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
81 int err, data_len, opt_len;
84 err = nla_parse_nested_deprecated(tb,
85 TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX,
86 nla, geneve_opt_policy, extack);
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");
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]);
100 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
104 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
108 opt_len = sizeof(struct geneve_opt) + data_len;
110 struct geneve_opt *opt = dst;
112 WARN_ON(dst_len < opt_len);
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 */
122 memcpy(opt + 1, data, data_len);
129 tunnel_key_copy_vxlan_opt(const struct nlattr *nla, void *dst, int dst_len,
130 struct netlink_ext_ack *extack)
132 struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1];
135 err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX, nla,
136 vxlan_opt_policy, extack);
140 if (!tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]) {
141 NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
146 struct vxlan_metadata *md = dst;
148 md->gbp = nla_get_u32(tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]);
151 return sizeof(struct vxlan_metadata);
154 static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst,
155 int dst_len, struct netlink_ext_ack *extack)
157 int err, rem, opt_len, len = nla_len(nla), opts_len = 0, type = 0;
158 const struct nlattr *attr, *head = nla_data(nla);
160 err = nla_validate_deprecated(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX,
161 enc_opts_policy, extack);
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");
172 opt_len = tunnel_key_copy_geneve_opt(attr, dst,
181 type = TUNNEL_GENEVE_OPT;
183 case TCA_TUNNEL_KEY_ENC_OPTS_VXLAN:
185 NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
188 opt_len = tunnel_key_copy_vxlan_opt(attr, dst,
193 type = TUNNEL_VXLAN_OPT;
199 NL_SET_ERR_MSG(extack, "Empty list of tunnel options");
204 NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes");
211 static int tunnel_key_get_opts_len(struct nlattr *nla,
212 struct netlink_ext_ack *extack)
214 return tunnel_key_copy_opts(nla, NULL, 0, extack);
217 static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info,
218 int opts_len, struct netlink_ext_ack *extack)
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),
228 return -EAFNOSUPPORT;
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),
236 return -EAFNOSUPPORT;
239 NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type");
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 },
258 static void tunnel_key_release_params(struct tcf_tunnel_key_params *p)
262 if (p->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
263 dst_release(&p->tcft_enc_metadata->dst);
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)
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;
292 NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
296 err = nla_parse_nested_deprecated(tb, TCA_TUNNEL_KEY_MAX, nla,
297 tunnel_key_policy, extack);
299 NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
303 if (!tb[TCA_TUNNEL_KEY_PARMS]) {
304 NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
308 parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
310 err = tcf_idr_check_alloc(tn, &index, a, bind);
317 switch (parm->t_action) {
318 case TCA_TUNNEL_KEY_ACT_RELEASE:
320 case TCA_TUNNEL_KEY_ACT_SET:
321 if (tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
324 key32 = nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]);
325 key_id = key32_to_tunnel_id(key32);
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;
334 if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
335 dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
337 if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
338 opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
347 if (tb[TCA_TUNNEL_KEY_ENC_TOS])
348 tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]);
350 if (tb[TCA_TUNNEL_KEY_ENC_TTL])
351 ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]);
353 if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
354 tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
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]);
361 metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl,
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;
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]);
372 metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port,
376 NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
382 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
387 #ifdef CONFIG_DST_CACHE
388 ret = dst_cache_init(&metadata->u.tun_info.dst_cache, GFP_KERNEL);
390 goto release_tun_meta;
394 ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
395 &metadata->u.tun_info,
398 goto release_tun_meta;
401 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
404 NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
410 ret = tcf_idr_create_from_flags(tn, index, est, a,
411 &act_tunnel_key_ops, bind,
414 NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
415 goto release_tun_meta;
420 NL_SET_ERR_MSG(extack, "TC IDR already exists");
422 goto release_tun_meta;
425 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
429 goto release_tun_meta;
431 t = to_tunnel_key(*a);
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");
440 params_new->tcft_action = parm->t_action;
441 params_new->tcft_enc_metadata = metadata;
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);
450 tcf_chain_put_by_act(goto_ch);
452 if (ret == ACT_P_CREATED)
453 tcf_idr_insert(tn, *a);
459 tcf_chain_put_by_act(goto_ch);
463 dst_release(&metadata->dst);
467 tcf_idr_release(*a, bind);
469 tcf_idr_cleanup(tn, index);
473 static void tunnel_key_release(struct tc_action *a)
475 struct tcf_tunnel_key *t = to_tunnel_key(a);
476 struct tcf_tunnel_key_params *params;
478 params = rcu_dereference_protected(t->params, 1);
479 tunnel_key_release_params(params);
482 static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
483 const struct ip_tunnel_info *info)
485 int len = info->options_len;
486 u8 *src = (u8 *)(info + 1);
487 struct nlattr *start;
489 start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
494 struct geneve_opt *opt = (struct geneve_opt *)src;
496 if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
498 nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
500 nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
501 opt->length * 4, opt + 1)) {
502 nla_nest_cancel(skb, start);
506 len -= sizeof(struct geneve_opt) + opt->length * 4;
507 src += sizeof(struct geneve_opt) + opt->length * 4;
510 nla_nest_end(skb, start);
514 static int tunnel_key_vxlan_opts_dump(struct sk_buff *skb,
515 const struct ip_tunnel_info *info)
517 struct vxlan_metadata *md = (struct vxlan_metadata *)(info + 1);
518 struct nlattr *start;
520 start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_VXLAN);
524 if (nla_put_u32(skb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP, md->gbp)) {
525 nla_nest_cancel(skb, start);
529 nla_nest_end(skb, start);
533 static int tunnel_key_opts_dump(struct sk_buff *skb,
534 const struct ip_tunnel_info *info)
536 struct nlattr *start;
539 if (!info->options_len)
542 start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS);
546 if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
547 err = tunnel_key_geneve_opts_dump(skb, info);
550 } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
551 err = tunnel_key_vxlan_opts_dump(skb, info);
556 nla_nest_cancel(skb, start);
560 nla_nest_end(skb, start);
564 static int tunnel_key_dump_addresses(struct sk_buff *skb,
565 const struct ip_tunnel_info *info)
567 unsigned short family = ip_tunnel_info_af(info);
569 if (family == AF_INET) {
570 __be32 saddr = info->key.u.ipv4.src;
571 __be32 daddr = info->key.u.ipv4.dst;
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))
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;
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))
592 static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
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,
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;
611 if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
612 goto nla_put_failure;
614 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
615 struct ip_tunnel_info *info =
616 ¶ms->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);
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 ¶ms->tcft_enc_metadata->u.tun_info) ||
625 nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT,
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;
632 if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos))
633 goto nla_put_failure;
635 if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl))
636 goto nla_put_failure;
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);
648 spin_unlock_bh(&t->tcf_lock);
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)
658 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
660 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
663 static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index)
665 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
667 return tcf_idr_search(tn, a, index);
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),
683 static __net_init int tunnel_key_init_net(struct net *net)
685 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
687 return tc_action_net_init(net, tn, &act_tunnel_key_ops);
690 static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
692 tc_action_net_exit(net_list, tunnel_key_net_id);
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),
702 static int __init tunnel_key_init_module(void)
704 return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
707 static void __exit tunnel_key_cleanup_module(void)
709 tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
712 module_init(tunnel_key_init_module);
713 module_exit(tunnel_key_cleanup_module);
715 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
716 MODULE_DESCRIPTION("ip tunnel manipulation actions");
717 MODULE_LICENSE("GPL v2");