blob: 20e98ed8d498b3e384b0fa275dfe6d622b534d0f [file] [log] [blame]
Amir Vadaid0f6dd82016-09-08 16:23:48 +03001/*
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>
16#include <net/netlink.h>
17#include <net/pkt_sched.h>
18#include <net/dst.h>
Amir Vadaid0f6dd82016-09-08 16:23:48 +030019
20#include <linux/tc_act/tc_tunnel_key.h>
21#include <net/tc_act/tc_tunnel_key.h>
22
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030023static unsigned int tunnel_key_net_id;
Amir Vadaid0f6dd82016-09-08 16:23:48 +030024static struct tc_action_ops act_tunnel_key_ops;
25
26static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
27 struct tcf_result *res)
28{
29 struct tcf_tunnel_key *t = to_tunnel_key(a);
30 struct tcf_tunnel_key_params *params;
31 int action;
32
33 rcu_read_lock();
34
35 params = rcu_dereference(t->params);
36
37 tcf_lastuse_update(&t->tcf_tm);
38 bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb);
39 action = params->action;
40
41 switch (params->tcft_action) {
42 case TCA_TUNNEL_KEY_ACT_RELEASE:
43 skb_dst_drop(skb);
44 break;
45 case TCA_TUNNEL_KEY_ACT_SET:
46 skb_dst_drop(skb);
47 skb_dst_set(skb, dst_clone(&params->tcft_enc_metadata->dst));
48 break;
49 default:
50 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
51 params->tcft_action);
52 break;
53 }
54
55 rcu_read_unlock();
56
57 return action;
58}
59
60static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
61 [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) },
62 [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
63 [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
64 [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
65 [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
66 [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +020067 [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
Jiri Benc86087e12017-06-14 21:19:31 +020068 [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 },
Amir Vadaid0f6dd82016-09-08 16:23:48 +030069};
70
71static int tunnel_key_init(struct net *net, struct nlattr *nla,
72 struct nlattr *est, struct tc_action **a,
Alexander Aring589dad62018-02-15 10:54:56 -050073 int ovr, int bind, struct netlink_ext_ack *extack)
Amir Vadaid0f6dd82016-09-08 16:23:48 +030074{
75 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
76 struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
77 struct tcf_tunnel_key_params *params_old;
78 struct tcf_tunnel_key_params *params_new;
79 struct metadata_dst *metadata = NULL;
80 struct tc_tunnel_key *parm;
81 struct tcf_tunnel_key *t;
82 bool exists = false;
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +020083 __be16 dst_port = 0;
Amir Vadaid0f6dd82016-09-08 16:23:48 +030084 __be64 key_id;
Jiri Benc86087e12017-06-14 21:19:31 +020085 __be16 flags;
Amir Vadaid0f6dd82016-09-08 16:23:48 +030086 int ret = 0;
87 int err;
88
Simon Horman9d7298c2018-06-26 21:39:35 -070089 if (!nla) {
90 NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
Amir Vadaid0f6dd82016-09-08 16:23:48 +030091 return -EINVAL;
Simon Horman9d7298c2018-06-26 21:39:35 -070092 }
Amir Vadaid0f6dd82016-09-08 16:23:48 +030093
Johannes Bergfceb6432017-04-12 14:34:07 +020094 err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy,
Simon Horman9d7298c2018-06-26 21:39:35 -070095 extack);
96 if (err < 0) {
97 NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
Amir Vadaid0f6dd82016-09-08 16:23:48 +030098 return err;
Simon Horman9d7298c2018-06-26 21:39:35 -070099 }
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300100
Simon Horman9d7298c2018-06-26 21:39:35 -0700101 if (!tb[TCA_TUNNEL_KEY_PARMS]) {
102 NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300103 return -EINVAL;
Simon Horman9d7298c2018-06-26 21:39:35 -0700104 }
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300105
106 parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
Chris Mi65a206c2017-08-30 02:31:59 -0400107 exists = tcf_idr_check(tn, parm->index, a, bind);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300108 if (exists && bind)
109 return 0;
110
111 switch (parm->t_action) {
112 case TCA_TUNNEL_KEY_ACT_RELEASE:
113 break;
114 case TCA_TUNNEL_KEY_ACT_SET:
115 if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
Simon Horman9d7298c2018-06-26 21:39:35 -0700116 NL_SET_ERR_MSG(extack, "Missing tunnel key id");
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300117 ret = -EINVAL;
118 goto err_out;
119 }
120
121 key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]));
122
Jiri Benc86087e12017-06-14 21:19:31 +0200123 flags = TUNNEL_KEY | TUNNEL_CSUM;
124 if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
125 nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
126 flags &= ~TUNNEL_CSUM;
127
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +0200128 if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
129 dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
130
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300131 if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
132 tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
133 __be32 saddr;
134 __be32 daddr;
135
136 saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
137 daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
138
139 metadata = __ip_tun_set_dst(saddr, daddr, 0, 0,
Jiri Benc86087e12017-06-14 21:19:31 +0200140 dst_port, flags,
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +0200141 key_id, 0);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300142 } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
143 tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
144 struct in6_addr saddr;
145 struct in6_addr daddr;
146
147 saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
148 daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
149
Or Gerlitzdc594ec2016-12-22 14:28:14 +0200150 metadata = __ipv6_tun_set_dst(&saddr, &daddr, 0, 0, dst_port,
Jiri Benc86087e12017-06-14 21:19:31 +0200151 0, flags,
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +0200152 key_id, 0);
Simon Hormana1165b52018-06-26 21:39:34 -0700153 } else {
Simon Horman9d7298c2018-06-26 21:39:35 -0700154 NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
Simon Hormana1165b52018-06-26 21:39:34 -0700155 ret = -EINVAL;
156 goto err_out;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300157 }
158
159 if (!metadata) {
Simon Horman9d7298c2018-06-26 21:39:35 -0700160 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
Simon Hormana1165b52018-06-26 21:39:34 -0700161 ret = -ENOMEM;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300162 goto err_out;
163 }
164
165 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
166 break;
167 default:
Simon Horman9d7298c2018-06-26 21:39:35 -0700168 NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
Roman Mashak51d47402018-03-12 16:20:58 -0400169 ret = -EINVAL;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300170 goto err_out;
171 }
172
173 if (!exists) {
Chris Mi65a206c2017-08-30 02:31:59 -0400174 ret = tcf_idr_create(tn, parm->index, est, a,
175 &act_tunnel_key_ops, bind, true);
Simon Horman9d7298c2018-06-26 21:39:35 -0700176 if (ret) {
177 NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300178 return ret;
Simon Horman9d7298c2018-06-26 21:39:35 -0700179 }
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300180
181 ret = ACT_P_CREATED;
182 } else {
Chris Mi65a206c2017-08-30 02:31:59 -0400183 tcf_idr_release(*a, bind);
Simon Horman9d7298c2018-06-26 21:39:35 -0700184 if (!ovr) {
185 NL_SET_ERR_MSG(extack, "TC IDR already exists");
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300186 return -EEXIST;
Simon Horman9d7298c2018-06-26 21:39:35 -0700187 }
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300188 }
189
190 t = to_tunnel_key(*a);
191
192 ASSERT_RTNL();
193 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
194 if (unlikely(!params_new)) {
195 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400196 tcf_idr_release(*a, bind);
Simon Horman9d7298c2018-06-26 21:39:35 -0700197 NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300198 return -ENOMEM;
199 }
200
201 params_old = rtnl_dereference(t->params);
202
203 params_new->action = parm->action;
204 params_new->tcft_action = parm->t_action;
205 params_new->tcft_enc_metadata = metadata;
206
207 rcu_assign_pointer(t->params, params_new);
208
209 if (params_old)
210 kfree_rcu(params_old, rcu);
211
212 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400213 tcf_idr_insert(tn, *a);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300214
215 return ret;
216
217err_out:
218 if (exists)
Chris Mi65a206c2017-08-30 02:31:59 -0400219 tcf_idr_release(*a, bind);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300220 return ret;
221}
222
Cong Wang9a63b252017-12-05 12:53:07 -0800223static void tunnel_key_release(struct tc_action *a)
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300224{
225 struct tcf_tunnel_key *t = to_tunnel_key(a);
226 struct tcf_tunnel_key_params *params;
227
Hadar Hen Zion07c0f092016-09-12 15:19:21 +0300228 params = rcu_dereference_protected(t->params, 1);
Davide Carattiabdadd32018-03-16 00:00:55 +0100229 if (params) {
230 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
231 dst_release(&params->tcft_enc_metadata->dst);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300232
Davide Carattiabdadd32018-03-16 00:00:55 +0100233 kfree_rcu(params, rcu);
234 }
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300235}
236
237static int tunnel_key_dump_addresses(struct sk_buff *skb,
238 const struct ip_tunnel_info *info)
239{
240 unsigned short family = ip_tunnel_info_af(info);
241
242 if (family == AF_INET) {
243 __be32 saddr = info->key.u.ipv4.src;
244 __be32 daddr = info->key.u.ipv4.dst;
245
246 if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
247 !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
248 return 0;
249 }
250
251 if (family == AF_INET6) {
252 const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
253 const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
254
255 if (!nla_put_in6_addr(skb,
256 TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
257 !nla_put_in6_addr(skb,
258 TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
259 return 0;
260 }
261
262 return -EINVAL;
263}
264
265static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
266 int bind, int ref)
267{
268 unsigned char *b = skb_tail_pointer(skb);
269 struct tcf_tunnel_key *t = to_tunnel_key(a);
270 struct tcf_tunnel_key_params *params;
271 struct tc_tunnel_key opt = {
272 .index = t->tcf_index,
273 .refcnt = t->tcf_refcnt - ref,
274 .bindcnt = t->tcf_bindcnt - bind,
275 };
276 struct tcf_t tm;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300277
Hadar Hen Zion07c0f092016-09-12 15:19:21 +0300278 params = rtnl_dereference(t->params);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300279
280 opt.t_action = params->tcft_action;
281 opt.action = params->action;
282
283 if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
284 goto nla_put_failure;
285
286 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
287 struct ip_tunnel_key *key =
288 &params->tcft_enc_metadata->u.tun_info.key;
289 __be32 key_id = tunnel_id_to_key32(key->tun_id);
290
291 if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
292 tunnel_key_dump_addresses(skb,
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +0200293 &params->tcft_enc_metadata->u.tun_info) ||
Jiri Benc86087e12017-06-14 21:19:31 +0200294 nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) ||
295 nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
296 !(key->tun_flags & TUNNEL_CSUM)))
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300297 goto nla_put_failure;
298 }
299
300 tcf_tm_dump(&tm, &t->tcf_tm);
301 if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
302 &tm, TCA_TUNNEL_KEY_PAD))
303 goto nla_put_failure;
304
Hadar Hen Zion07c0f092016-09-12 15:19:21 +0300305 return skb->len;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300306
307nla_put_failure:
308 nlmsg_trim(skb, b);
Hadar Hen Zion07c0f092016-09-12 15:19:21 +0300309 return -1;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300310}
311
312static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
313 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500314 const struct tc_action_ops *ops,
315 struct netlink_ext_ack *extack)
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300316{
317 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
318
Alexander Aringb3620142018-02-15 10:54:59 -0500319 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300320}
321
Alexander Aring331a9292018-02-15 10:54:57 -0500322static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index,
323 struct netlink_ext_ack *extack)
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300324{
325 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
326
Chris Mi65a206c2017-08-30 02:31:59 -0400327 return tcf_idr_search(tn, a, index);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300328}
329
330static struct tc_action_ops act_tunnel_key_ops = {
331 .kind = "tunnel_key",
332 .type = TCA_ACT_TUNNEL_KEY,
333 .owner = THIS_MODULE,
334 .act = tunnel_key_act,
335 .dump = tunnel_key_dump,
336 .init = tunnel_key_init,
337 .cleanup = tunnel_key_release,
338 .walk = tunnel_key_walker,
339 .lookup = tunnel_key_search,
340 .size = sizeof(struct tcf_tunnel_key),
341};
342
343static __net_init int tunnel_key_init_net(struct net *net)
344{
345 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
346
Cong Wangc7e460c2017-11-06 13:47:18 -0800347 return tc_action_net_init(tn, &act_tunnel_key_ops);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300348}
349
Cong Wang039af9c2017-12-11 15:35:03 -0800350static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300351{
Cong Wang039af9c2017-12-11 15:35:03 -0800352 tc_action_net_exit(net_list, tunnel_key_net_id);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300353}
354
355static struct pernet_operations tunnel_key_net_ops = {
356 .init = tunnel_key_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800357 .exit_batch = tunnel_key_exit_net,
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300358 .id = &tunnel_key_net_id,
359 .size = sizeof(struct tc_action_net),
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300360};
361
362static int __init tunnel_key_init_module(void)
363{
364 return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
365}
366
367static void __exit tunnel_key_cleanup_module(void)
368{
369 tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
370}
371
372module_init(tunnel_key_init_module);
373module_exit(tunnel_key_cleanup_module);
374
375MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
376MODULE_DESCRIPTION("ip tunnel manipulation actions");
377MODULE_LICENSE("GPL v2");