blob: 2edd389e7c92731310e5a7ab7f41421591153037 [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
89 if (!nla)
90 return -EINVAL;
91
Johannes Bergfceb6432017-04-12 14:34:07 +020092 err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy,
93 NULL);
Amir Vadaid0f6dd82016-09-08 16:23:48 +030094 if (err < 0)
95 return err;
96
97 if (!tb[TCA_TUNNEL_KEY_PARMS])
98 return -EINVAL;
99
100 parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
Chris Mi65a206c2017-08-30 02:31:59 -0400101 exists = tcf_idr_check(tn, parm->index, a, bind);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300102 if (exists && bind)
103 return 0;
104
105 switch (parm->t_action) {
106 case TCA_TUNNEL_KEY_ACT_RELEASE:
107 break;
108 case TCA_TUNNEL_KEY_ACT_SET:
109 if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
110 ret = -EINVAL;
111 goto err_out;
112 }
113
114 key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]));
115
Jiri Benc86087e12017-06-14 21:19:31 +0200116 flags = TUNNEL_KEY | TUNNEL_CSUM;
117 if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
118 nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
119 flags &= ~TUNNEL_CSUM;
120
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +0200121 if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
122 dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
123
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300124 if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
125 tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
126 __be32 saddr;
127 __be32 daddr;
128
129 saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
130 daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
131
132 metadata = __ip_tun_set_dst(saddr, daddr, 0, 0,
Jiri Benc86087e12017-06-14 21:19:31 +0200133 dst_port, flags,
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +0200134 key_id, 0);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300135 } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
136 tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
137 struct in6_addr saddr;
138 struct in6_addr daddr;
139
140 saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
141 daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
142
Or Gerlitzdc594ec2016-12-22 14:28:14 +0200143 metadata = __ipv6_tun_set_dst(&saddr, &daddr, 0, 0, dst_port,
Jiri Benc86087e12017-06-14 21:19:31 +0200144 0, flags,
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +0200145 key_id, 0);
Simon Hormana1165b52018-06-26 21:39:34 -0700146 } else {
147 ret = -EINVAL;
148 goto err_out;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300149 }
150
151 if (!metadata) {
Simon Hormana1165b52018-06-26 21:39:34 -0700152 ret = -ENOMEM;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300153 goto err_out;
154 }
155
156 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
157 break;
158 default:
Roman Mashak51d47402018-03-12 16:20:58 -0400159 ret = -EINVAL;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300160 goto err_out;
161 }
162
163 if (!exists) {
Chris Mi65a206c2017-08-30 02:31:59 -0400164 ret = tcf_idr_create(tn, parm->index, est, a,
165 &act_tunnel_key_ops, bind, true);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300166 if (ret)
167 return ret;
168
169 ret = ACT_P_CREATED;
170 } else {
Chris Mi65a206c2017-08-30 02:31:59 -0400171 tcf_idr_release(*a, bind);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300172 if (!ovr)
173 return -EEXIST;
174 }
175
176 t = to_tunnel_key(*a);
177
178 ASSERT_RTNL();
179 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
180 if (unlikely(!params_new)) {
181 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400182 tcf_idr_release(*a, bind);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300183 return -ENOMEM;
184 }
185
186 params_old = rtnl_dereference(t->params);
187
188 params_new->action = parm->action;
189 params_new->tcft_action = parm->t_action;
190 params_new->tcft_enc_metadata = metadata;
191
192 rcu_assign_pointer(t->params, params_new);
193
194 if (params_old)
195 kfree_rcu(params_old, rcu);
196
197 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400198 tcf_idr_insert(tn, *a);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300199
200 return ret;
201
202err_out:
203 if (exists)
Chris Mi65a206c2017-08-30 02:31:59 -0400204 tcf_idr_release(*a, bind);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300205 return ret;
206}
207
Cong Wang9a63b252017-12-05 12:53:07 -0800208static void tunnel_key_release(struct tc_action *a)
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300209{
210 struct tcf_tunnel_key *t = to_tunnel_key(a);
211 struct tcf_tunnel_key_params *params;
212
Hadar Hen Zion07c0f092016-09-12 15:19:21 +0300213 params = rcu_dereference_protected(t->params, 1);
Davide Carattiabdadd32018-03-16 00:00:55 +0100214 if (params) {
215 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
216 dst_release(&params->tcft_enc_metadata->dst);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300217
Davide Carattiabdadd32018-03-16 00:00:55 +0100218 kfree_rcu(params, rcu);
219 }
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300220}
221
222static int tunnel_key_dump_addresses(struct sk_buff *skb,
223 const struct ip_tunnel_info *info)
224{
225 unsigned short family = ip_tunnel_info_af(info);
226
227 if (family == AF_INET) {
228 __be32 saddr = info->key.u.ipv4.src;
229 __be32 daddr = info->key.u.ipv4.dst;
230
231 if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
232 !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
233 return 0;
234 }
235
236 if (family == AF_INET6) {
237 const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
238 const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
239
240 if (!nla_put_in6_addr(skb,
241 TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
242 !nla_put_in6_addr(skb,
243 TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
244 return 0;
245 }
246
247 return -EINVAL;
248}
249
250static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
251 int bind, int ref)
252{
253 unsigned char *b = skb_tail_pointer(skb);
254 struct tcf_tunnel_key *t = to_tunnel_key(a);
255 struct tcf_tunnel_key_params *params;
256 struct tc_tunnel_key opt = {
257 .index = t->tcf_index,
258 .refcnt = t->tcf_refcnt - ref,
259 .bindcnt = t->tcf_bindcnt - bind,
260 };
261 struct tcf_t tm;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300262
Hadar Hen Zion07c0f092016-09-12 15:19:21 +0300263 params = rtnl_dereference(t->params);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300264
265 opt.t_action = params->tcft_action;
266 opt.action = params->action;
267
268 if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
269 goto nla_put_failure;
270
271 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
272 struct ip_tunnel_key *key =
273 &params->tcft_enc_metadata->u.tun_info.key;
274 __be32 key_id = tunnel_id_to_key32(key->tun_id);
275
276 if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
277 tunnel_key_dump_addresses(skb,
Hadar Hen Zion75bfbca2016-11-07 15:14:41 +0200278 &params->tcft_enc_metadata->u.tun_info) ||
Jiri Benc86087e12017-06-14 21:19:31 +0200279 nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) ||
280 nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
281 !(key->tun_flags & TUNNEL_CSUM)))
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300282 goto nla_put_failure;
283 }
284
285 tcf_tm_dump(&tm, &t->tcf_tm);
286 if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
287 &tm, TCA_TUNNEL_KEY_PAD))
288 goto nla_put_failure;
289
Hadar Hen Zion07c0f092016-09-12 15:19:21 +0300290 return skb->len;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300291
292nla_put_failure:
293 nlmsg_trim(skb, b);
Hadar Hen Zion07c0f092016-09-12 15:19:21 +0300294 return -1;
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300295}
296
297static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
298 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500299 const struct tc_action_ops *ops,
300 struct netlink_ext_ack *extack)
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300301{
302 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
303
Alexander Aringb3620142018-02-15 10:54:59 -0500304 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300305}
306
Alexander Aring331a9292018-02-15 10:54:57 -0500307static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index,
308 struct netlink_ext_ack *extack)
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300309{
310 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
311
Chris Mi65a206c2017-08-30 02:31:59 -0400312 return tcf_idr_search(tn, a, index);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300313}
314
315static struct tc_action_ops act_tunnel_key_ops = {
316 .kind = "tunnel_key",
317 .type = TCA_ACT_TUNNEL_KEY,
318 .owner = THIS_MODULE,
319 .act = tunnel_key_act,
320 .dump = tunnel_key_dump,
321 .init = tunnel_key_init,
322 .cleanup = tunnel_key_release,
323 .walk = tunnel_key_walker,
324 .lookup = tunnel_key_search,
325 .size = sizeof(struct tcf_tunnel_key),
326};
327
328static __net_init int tunnel_key_init_net(struct net *net)
329{
330 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
331
Cong Wangc7e460c2017-11-06 13:47:18 -0800332 return tc_action_net_init(tn, &act_tunnel_key_ops);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300333}
334
Cong Wang039af9c2017-12-11 15:35:03 -0800335static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300336{
Cong Wang039af9c2017-12-11 15:35:03 -0800337 tc_action_net_exit(net_list, tunnel_key_net_id);
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300338}
339
340static struct pernet_operations tunnel_key_net_ops = {
341 .init = tunnel_key_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800342 .exit_batch = tunnel_key_exit_net,
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300343 .id = &tunnel_key_net_id,
344 .size = sizeof(struct tc_action_net),
Amir Vadaid0f6dd82016-09-08 16:23:48 +0300345};
346
347static int __init tunnel_key_init_module(void)
348{
349 return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
350}
351
352static void __exit tunnel_key_cleanup_module(void)
353{
354 tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
355}
356
357module_init(tunnel_key_init_module);
358module_exit(tunnel_key_cleanup_module);
359
360MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
361MODULE_DESCRIPTION("ip tunnel manipulation actions");
362MODULE_LICENSE("GPL v2");