Merge tag 'chrome-platform-for-linus-4.13' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / net / sched / act_tunnel_key.c
blobfd7e75679c69e050052f388675eb58060e15f493
1 /*
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.
9 */
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>
20 #include <linux/tc_act/tc_tunnel_key.h>
21 #include <net/tc_act/tc_tunnel_key.h>
23 #define TUNNEL_KEY_TAB_MASK 15
25 static unsigned int tunnel_key_net_id;
26 static struct tc_action_ops act_tunnel_key_ops;
28 static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
29 struct tcf_result *res)
31 struct tcf_tunnel_key *t = to_tunnel_key(a);
32 struct tcf_tunnel_key_params *params;
33 int action;
35 rcu_read_lock();
37 params = rcu_dereference(t->params);
39 tcf_lastuse_update(&t->tcf_tm);
40 bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb);
41 action = params->action;
43 switch (params->tcft_action) {
44 case TCA_TUNNEL_KEY_ACT_RELEASE:
45 skb_dst_drop(skb);
46 break;
47 case TCA_TUNNEL_KEY_ACT_SET:
48 skb_dst_drop(skb);
49 skb_dst_set(skb, dst_clone(&params->tcft_enc_metadata->dst));
50 break;
51 default:
52 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
53 params->tcft_action);
54 break;
57 rcu_read_unlock();
59 return action;
62 static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
63 [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) },
64 [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
65 [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
66 [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
67 [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
68 [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
69 [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
70 [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 },
73 static int tunnel_key_init(struct net *net, struct nlattr *nla,
74 struct nlattr *est, struct tc_action **a,
75 int ovr, int bind)
77 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
78 struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
79 struct tcf_tunnel_key_params *params_old;
80 struct tcf_tunnel_key_params *params_new;
81 struct metadata_dst *metadata = NULL;
82 struct tc_tunnel_key *parm;
83 struct tcf_tunnel_key *t;
84 bool exists = false;
85 __be16 dst_port = 0;
86 __be64 key_id;
87 __be16 flags;
88 int ret = 0;
89 int err;
91 if (!nla)
92 return -EINVAL;
94 err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy,
95 NULL);
96 if (err < 0)
97 return err;
99 if (!tb[TCA_TUNNEL_KEY_PARMS])
100 return -EINVAL;
102 parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
103 exists = tcf_hash_check(tn, parm->index, a, bind);
104 if (exists && bind)
105 return 0;
107 switch (parm->t_action) {
108 case TCA_TUNNEL_KEY_ACT_RELEASE:
109 break;
110 case TCA_TUNNEL_KEY_ACT_SET:
111 if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
112 ret = -EINVAL;
113 goto err_out;
116 key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]));
118 flags = TUNNEL_KEY | TUNNEL_CSUM;
119 if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
120 nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
121 flags &= ~TUNNEL_CSUM;
123 if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
124 dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
126 if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
127 tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
128 __be32 saddr;
129 __be32 daddr;
131 saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
132 daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
134 metadata = __ip_tun_set_dst(saddr, daddr, 0, 0,
135 dst_port, flags,
136 key_id, 0);
137 } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
138 tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
139 struct in6_addr saddr;
140 struct in6_addr daddr;
142 saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
143 daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
145 metadata = __ipv6_tun_set_dst(&saddr, &daddr, 0, 0, dst_port,
146 0, flags,
147 key_id, 0);
150 if (!metadata) {
151 ret = -EINVAL;
152 goto err_out;
155 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
156 break;
157 default:
158 goto err_out;
161 if (!exists) {
162 ret = tcf_hash_create(tn, parm->index, est, a,
163 &act_tunnel_key_ops, bind, true);
164 if (ret)
165 return ret;
167 ret = ACT_P_CREATED;
168 } else {
169 tcf_hash_release(*a, bind);
170 if (!ovr)
171 return -EEXIST;
174 t = to_tunnel_key(*a);
176 ASSERT_RTNL();
177 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
178 if (unlikely(!params_new)) {
179 if (ret == ACT_P_CREATED)
180 tcf_hash_release(*a, bind);
181 return -ENOMEM;
184 params_old = rtnl_dereference(t->params);
186 params_new->action = parm->action;
187 params_new->tcft_action = parm->t_action;
188 params_new->tcft_enc_metadata = metadata;
190 rcu_assign_pointer(t->params, params_new);
192 if (params_old)
193 kfree_rcu(params_old, rcu);
195 if (ret == ACT_P_CREATED)
196 tcf_hash_insert(tn, *a);
198 return ret;
200 err_out:
201 if (exists)
202 tcf_hash_release(*a, bind);
203 return ret;
206 static void tunnel_key_release(struct tc_action *a, int bind)
208 struct tcf_tunnel_key *t = to_tunnel_key(a);
209 struct tcf_tunnel_key_params *params;
211 params = rcu_dereference_protected(t->params, 1);
213 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
214 dst_release(&params->tcft_enc_metadata->dst);
216 kfree_rcu(params, rcu);
219 static int tunnel_key_dump_addresses(struct sk_buff *skb,
220 const struct ip_tunnel_info *info)
222 unsigned short family = ip_tunnel_info_af(info);
224 if (family == AF_INET) {
225 __be32 saddr = info->key.u.ipv4.src;
226 __be32 daddr = info->key.u.ipv4.dst;
228 if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
229 !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
230 return 0;
233 if (family == AF_INET6) {
234 const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
235 const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
237 if (!nla_put_in6_addr(skb,
238 TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
239 !nla_put_in6_addr(skb,
240 TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
241 return 0;
244 return -EINVAL;
247 static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
248 int bind, int ref)
250 unsigned char *b = skb_tail_pointer(skb);
251 struct tcf_tunnel_key *t = to_tunnel_key(a);
252 struct tcf_tunnel_key_params *params;
253 struct tc_tunnel_key opt = {
254 .index = t->tcf_index,
255 .refcnt = t->tcf_refcnt - ref,
256 .bindcnt = t->tcf_bindcnt - bind,
258 struct tcf_t tm;
260 params = rtnl_dereference(t->params);
262 opt.t_action = params->tcft_action;
263 opt.action = params->action;
265 if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
266 goto nla_put_failure;
268 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
269 struct ip_tunnel_key *key =
270 &params->tcft_enc_metadata->u.tun_info.key;
271 __be32 key_id = tunnel_id_to_key32(key->tun_id);
273 if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
274 tunnel_key_dump_addresses(skb,
275 &params->tcft_enc_metadata->u.tun_info) ||
276 nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) ||
277 nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
278 !(key->tun_flags & TUNNEL_CSUM)))
279 goto nla_put_failure;
282 tcf_tm_dump(&tm, &t->tcf_tm);
283 if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
284 &tm, TCA_TUNNEL_KEY_PAD))
285 goto nla_put_failure;
287 return skb->len;
289 nla_put_failure:
290 nlmsg_trim(skb, b);
291 return -1;
294 static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
295 struct netlink_callback *cb, int type,
296 const struct tc_action_ops *ops)
298 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
300 return tcf_generic_walker(tn, skb, cb, type, ops);
303 static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index)
305 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
307 return tcf_hash_search(tn, a, index);
310 static struct tc_action_ops act_tunnel_key_ops = {
311 .kind = "tunnel_key",
312 .type = TCA_ACT_TUNNEL_KEY,
313 .owner = THIS_MODULE,
314 .act = tunnel_key_act,
315 .dump = tunnel_key_dump,
316 .init = tunnel_key_init,
317 .cleanup = tunnel_key_release,
318 .walk = tunnel_key_walker,
319 .lookup = tunnel_key_search,
320 .size = sizeof(struct tcf_tunnel_key),
323 static __net_init int tunnel_key_init_net(struct net *net)
325 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
327 return tc_action_net_init(tn, &act_tunnel_key_ops, TUNNEL_KEY_TAB_MASK);
330 static void __net_exit tunnel_key_exit_net(struct net *net)
332 struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
334 tc_action_net_exit(tn);
337 static struct pernet_operations tunnel_key_net_ops = {
338 .init = tunnel_key_init_net,
339 .exit = tunnel_key_exit_net,
340 .id = &tunnel_key_net_id,
341 .size = sizeof(struct tc_action_net),
344 static int __init tunnel_key_init_module(void)
346 return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
349 static void __exit tunnel_key_cleanup_module(void)
351 tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
354 module_init(tunnel_key_init_module);
355 module_exit(tunnel_key_cleanup_module);
357 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
358 MODULE_DESCRIPTION("ip tunnel manipulation actions");
359 MODULE_LICENSE("GPL v2");