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;
36 params = rcu_dereference(t->params);
38 tcf_lastuse_update(&t->tcf_tm);
39 bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb);
40 action = params->action;
42 switch (params->tcft_action) {
43 case TCA_TUNNEL_KEY_ACT_RELEASE:
46 case TCA_TUNNEL_KEY_ACT_SET:
48 skb_dst_set(skb, dst_clone(¶ms->tcft_enc_metadata->dst));
51 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
61 static const struct nla_policy
62 enc_opts_policy[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1] = {
63 [TCA_TUNNEL_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
66 static const struct nla_policy
67 geneve_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1] = {
68 [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
69 [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
70 [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
75 tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len,
76 struct netlink_ext_ack *extack)
78 struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
79 int err, data_len, opt_len;
82 err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX,
83 nla, geneve_opt_policy, extack);
87 if (!tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] ||
88 !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] ||
89 !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]) {
90 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
94 data = nla_data(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
95 data_len = nla_len(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
97 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
101 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
105 opt_len = sizeof(struct geneve_opt) + data_len;
107 struct geneve_opt *opt = dst;
109 WARN_ON(dst_len < opt_len);
112 nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]);
113 opt->type = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]);
114 opt->length = data_len / 4; /* length is in units of 4 bytes */
119 memcpy(opt + 1, data, data_len);
125 static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst,
126 int dst_len, struct netlink_ext_ack *extack)
128 int err, rem, opt_len, len = nla_len(nla), opts_len = 0;
129 const struct nlattr *attr, *head = nla_data(nla);
131 err = nla_validate(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX,
132 enc_opts_policy, extack);
136 nla_for_each_attr(attr, head, len, rem) {
137 switch (nla_type(attr)) {
138 case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
139 opt_len = tunnel_key_copy_geneve_opt(attr, dst,
153 NL_SET_ERR_MSG(extack, "Empty list of tunnel options");
158 NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes");
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),
182 return -EAFNOSUPPORT;
185 NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type");
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 },
202 static int tunnel_key_init(struct net *net, struct nlattr *nla,
203 struct nlattr *est, struct tc_action **a,
204 int ovr, int bind, struct netlink_ext_ack *extack)
206 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
207 struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
208 struct tcf_tunnel_key_params *params_old;
209 struct tcf_tunnel_key_params *params_new;
210 struct metadata_dst *metadata = NULL;
211 struct tc_tunnel_key *parm;
212 struct tcf_tunnel_key *t;
222 NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
226 err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy,
229 NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
233 if (!tb[TCA_TUNNEL_KEY_PARMS]) {
234 NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
238 parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
239 exists = tcf_idr_check(tn, parm->index, a, bind);
243 switch (parm->t_action) {
244 case TCA_TUNNEL_KEY_ACT_RELEASE:
246 case TCA_TUNNEL_KEY_ACT_SET:
247 if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
248 NL_SET_ERR_MSG(extack, "Missing tunnel key id");
253 key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]));
255 flags = TUNNEL_KEY | TUNNEL_CSUM;
256 if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
257 nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
258 flags &= ~TUNNEL_CSUM;
260 if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
261 dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
263 if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
264 opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
272 if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
273 tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
277 saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
278 daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
280 metadata = __ip_tun_set_dst(saddr, daddr, 0, 0,
283 } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
284 tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
285 struct in6_addr saddr;
286 struct in6_addr daddr;
288 saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
289 daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
291 metadata = __ipv6_tun_set_dst(&saddr, &daddr, 0, 0, dst_port,
295 NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
301 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
307 ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
308 &metadata->u.tun_info,
314 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
317 NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
323 ret = tcf_idr_create(tn, parm->index, est, a,
324 &act_tunnel_key_ops, bind, true);
326 NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
332 tcf_idr_release(*a, bind);
334 NL_SET_ERR_MSG(extack, "TC IDR already exists");
339 t = to_tunnel_key(*a);
342 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
343 if (unlikely(!params_new)) {
344 if (ret == ACT_P_CREATED)
345 tcf_idr_release(*a, bind);
346 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
350 params_old = rtnl_dereference(t->params);
352 params_new->action = parm->action;
353 params_new->tcft_action = parm->t_action;
354 params_new->tcft_enc_metadata = metadata;
356 rcu_assign_pointer(t->params, params_new);
359 kfree_rcu(params_old, rcu);
361 if (ret == ACT_P_CREATED)
362 tcf_idr_insert(tn, *a);
368 tcf_idr_release(*a, bind);
372 static void tunnel_key_release(struct tc_action *a)
374 struct tcf_tunnel_key *t = to_tunnel_key(a);
375 struct tcf_tunnel_key_params *params;
377 params = rcu_dereference_protected(t->params, 1);
379 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
380 dst_release(¶ms->tcft_enc_metadata->dst);
382 kfree_rcu(params, rcu);
386 static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
387 const struct ip_tunnel_info *info)
389 int len = info->options_len;
390 u8 *src = (u8 *)(info + 1);
391 struct nlattr *start;
393 start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
398 struct geneve_opt *opt = (struct geneve_opt *)src;
400 if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
402 nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
404 nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
405 opt->length * 4, opt + 1))
408 len -= sizeof(struct geneve_opt) + opt->length * 4;
409 src += sizeof(struct geneve_opt) + opt->length * 4;
412 nla_nest_end(skb, start);
416 static int tunnel_key_opts_dump(struct sk_buff *skb,
417 const struct ip_tunnel_info *info)
419 struct nlattr *start;
422 if (!info->options_len)
425 start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS);
429 if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
430 err = tunnel_key_geneve_opts_dump(skb, info);
437 nla_nest_end(skb, start);
441 static int tunnel_key_dump_addresses(struct sk_buff *skb,
442 const struct ip_tunnel_info *info)
444 unsigned short family = ip_tunnel_info_af(info);
446 if (family == AF_INET) {
447 __be32 saddr = info->key.u.ipv4.src;
448 __be32 daddr = info->key.u.ipv4.dst;
450 if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
451 !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
455 if (family == AF_INET6) {
456 const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
457 const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
459 if (!nla_put_in6_addr(skb,
460 TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
461 !nla_put_in6_addr(skb,
462 TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
469 static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
472 unsigned char *b = skb_tail_pointer(skb);
473 struct tcf_tunnel_key *t = to_tunnel_key(a);
474 struct tcf_tunnel_key_params *params;
475 struct tc_tunnel_key opt = {
476 .index = t->tcf_index,
477 .refcnt = t->tcf_refcnt - ref,
478 .bindcnt = t->tcf_bindcnt - bind,
482 params = rtnl_dereference(t->params);
484 opt.t_action = params->tcft_action;
485 opt.action = params->action;
487 if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
488 goto nla_put_failure;
490 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
491 struct ip_tunnel_info *info =
492 ¶ms->tcft_enc_metadata->u.tun_info;
493 struct ip_tunnel_key *key = &info->key;
494 __be32 key_id = tunnel_id_to_key32(key->tun_id);
496 if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
497 tunnel_key_dump_addresses(skb,
498 ¶ms->tcft_enc_metadata->u.tun_info) ||
499 nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) ||
500 nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
501 !(key->tun_flags & TUNNEL_CSUM)) ||
502 tunnel_key_opts_dump(skb, info))
503 goto nla_put_failure;
506 tcf_tm_dump(&tm, &t->tcf_tm);
507 if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
508 &tm, TCA_TUNNEL_KEY_PAD))
509 goto nla_put_failure;
518 static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
519 struct netlink_callback *cb, int type,
520 const struct tc_action_ops *ops,
521 struct netlink_ext_ack *extack)
523 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
525 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
528 static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index,
529 struct netlink_ext_ack *extack)
531 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
533 return tcf_idr_search(tn, a, index);
536 static struct tc_action_ops act_tunnel_key_ops = {
537 .kind = "tunnel_key",
538 .type = TCA_ACT_TUNNEL_KEY,
539 .owner = THIS_MODULE,
540 .act = tunnel_key_act,
541 .dump = tunnel_key_dump,
542 .init = tunnel_key_init,
543 .cleanup = tunnel_key_release,
544 .walk = tunnel_key_walker,
545 .lookup = tunnel_key_search,
546 .size = sizeof(struct tcf_tunnel_key),
549 static __net_init int tunnel_key_init_net(struct net *net)
551 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
553 return tc_action_net_init(tn, &act_tunnel_key_ops);
556 static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
558 tc_action_net_exit(net_list, tunnel_key_net_id);
561 static struct pernet_operations tunnel_key_net_ops = {
562 .init = tunnel_key_init_net,
563 .exit_batch = tunnel_key_exit_net,
564 .id = &tunnel_key_net_id,
565 .size = sizeof(struct tc_action_net),
568 static int __init tunnel_key_init_module(void)
570 return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
573 static void __exit tunnel_key_cleanup_module(void)
575 tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
578 module_init(tunnel_key_init_module);
579 module_exit(tunnel_key_cleanup_module);
581 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
582 MODULE_DESCRIPTION("ip tunnel manipulation actions");
583 MODULE_LICENSE("GPL v2");