blob: 71e7ff22f7c92a86cacad9a1b8d18d3d726f52fb [file] [log] [blame]
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -05001/*
2 * net/sched/ife.c Inter-FE action based on ForCES WG InterFE LFB
3 *
4 * Refer to:
5 * draft-ietf-forces-interfelfb-03
6 * and
7 * netdev01 paper:
8 * "Distributing Linux Traffic Control Classifier-Action
9 * Subsystem"
10 * Authors: Jamal Hadi Salim and Damascene M. Joachimpillai
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * copyright Jamal Hadi Salim (2015)
18 *
19*/
20
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/string.h>
24#include <linux/errno.h>
25#include <linux/skbuff.h>
26#include <linux/rtnetlink.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <net/net_namespace.h>
30#include <net/netlink.h>
31#include <net/pkt_sched.h>
32#include <uapi/linux/tc_act/tc_ife.h>
33#include <net/tc_act/tc_ife.h>
34#include <linux/etherdevice.h>
Yotam Gigi295a6e02017-02-01 15:30:03 +020035#include <net/ife.h>
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -050036
37#define IFE_TAB_MASK 15
38
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030039static unsigned int ife_net_id;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -050040static int max_metacnt = IFE_META_MAX + 1;
WANG Conga85a9702016-07-25 16:09:41 -070041static struct tc_action_ops act_ife_ops;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -050042
43static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = {
44 [TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)},
45 [TCA_IFE_DMAC] = { .len = ETH_ALEN},
46 [TCA_IFE_SMAC] = { .len = ETH_ALEN},
47 [TCA_IFE_TYPE] = { .type = NLA_U16},
48};
49
Jamal Hadi Salim6a5d58b2016-09-18 07:31:42 -040050int ife_encode_meta_u16(u16 metaval, void *skbdata, struct tcf_meta_info *mi)
51{
52 u16 edata = 0;
53
54 if (mi->metaval)
55 edata = *(u16 *)mi->metaval;
56 else if (metaval)
57 edata = metaval;
58
59 if (!edata) /* will not encode */
60 return 0;
61
62 edata = htons(edata);
63 return ife_tlv_meta_encode(skbdata, mi->metaid, 2, &edata);
64}
65EXPORT_SYMBOL_GPL(ife_encode_meta_u16);
66
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -050067int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi)
68{
69 if (mi->metaval)
70 return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval);
71 else
72 return nla_put(skb, mi->metaid, 0, NULL);
73}
74EXPORT_SYMBOL_GPL(ife_get_meta_u32);
75
76int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi)
77{
78 if (metaval || mi->metaval)
79 return 8; /* T+L+V == 2+2+4 */
80
81 return 0;
82}
83EXPORT_SYMBOL_GPL(ife_check_meta_u32);
84
Jamal Hadi Salim6a5d58b2016-09-18 07:31:42 -040085int ife_check_meta_u16(u16 metaval, struct tcf_meta_info *mi)
86{
87 if (metaval || mi->metaval)
88 return 8; /* T+L+(V) == 2+2+(2+2bytepad) */
89
90 return 0;
91}
92EXPORT_SYMBOL_GPL(ife_check_meta_u16);
93
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -050094int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi)
95{
96 u32 edata = metaval;
97
98 if (mi->metaval)
99 edata = *(u32 *)mi->metaval;
100 else if (metaval)
101 edata = metaval;
102
103 if (!edata) /* will not encode */
104 return 0;
105
106 edata = htonl(edata);
107 return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata);
108}
109EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
110
111int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi)
112{
113 if (mi->metaval)
114 return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval);
115 else
116 return nla_put(skb, mi->metaid, 0, NULL);
117}
118EXPORT_SYMBOL_GPL(ife_get_meta_u16);
119
WANG Cong067a7cd2016-06-20 13:37:18 -0700120int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500121{
WANG Cong067a7cd2016-06-20 13:37:18 -0700122 mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500123 if (!mi->metaval)
124 return -ENOMEM;
125
126 return 0;
127}
128EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
129
WANG Cong067a7cd2016-06-20 13:37:18 -0700130int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500131{
WANG Cong067a7cd2016-06-20 13:37:18 -0700132 mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500133 if (!mi->metaval)
134 return -ENOMEM;
135
136 return 0;
137}
138EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
139
140void ife_release_meta_gen(struct tcf_meta_info *mi)
141{
142 kfree(mi->metaval);
143}
144EXPORT_SYMBOL_GPL(ife_release_meta_gen);
145
146int ife_validate_meta_u32(void *val, int len)
147{
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400148 if (len == sizeof(u32))
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500149 return 0;
150
151 return -EINVAL;
152}
153EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
154
155int ife_validate_meta_u16(void *val, int len)
156{
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400157 /* length will not include padding */
158 if (len == sizeof(u16))
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500159 return 0;
160
161 return -EINVAL;
162}
163EXPORT_SYMBOL_GPL(ife_validate_meta_u16);
164
165static LIST_HEAD(ifeoplist);
166static DEFINE_RWLOCK(ife_mod_lock);
167
168static struct tcf_meta_ops *find_ife_oplist(u16 metaid)
169{
170 struct tcf_meta_ops *o;
171
172 read_lock(&ife_mod_lock);
173 list_for_each_entry(o, &ifeoplist, list) {
174 if (o->metaid == metaid) {
175 if (!try_module_get(o->owner))
176 o = NULL;
177 read_unlock(&ife_mod_lock);
178 return o;
179 }
180 }
181 read_unlock(&ife_mod_lock);
182
183 return NULL;
184}
185
186int register_ife_op(struct tcf_meta_ops *mops)
187{
188 struct tcf_meta_ops *m;
189
190 if (!mops->metaid || !mops->metatype || !mops->name ||
191 !mops->check_presence || !mops->encode || !mops->decode ||
192 !mops->get || !mops->alloc)
193 return -EINVAL;
194
195 write_lock(&ife_mod_lock);
196
197 list_for_each_entry(m, &ifeoplist, list) {
198 if (m->metaid == mops->metaid ||
199 (strcmp(mops->name, m->name) == 0)) {
200 write_unlock(&ife_mod_lock);
201 return -EEXIST;
202 }
203 }
204
205 if (!mops->release)
206 mops->release = ife_release_meta_gen;
207
208 list_add_tail(&mops->list, &ifeoplist);
209 write_unlock(&ife_mod_lock);
210 return 0;
211}
212EXPORT_SYMBOL_GPL(unregister_ife_op);
213
214int unregister_ife_op(struct tcf_meta_ops *mops)
215{
216 struct tcf_meta_ops *m;
217 int err = -ENOENT;
218
219 write_lock(&ife_mod_lock);
220 list_for_each_entry(m, &ifeoplist, list) {
221 if (m->metaid == mops->metaid) {
222 list_del(&mops->list);
223 err = 0;
224 break;
225 }
226 }
227 write_unlock(&ife_mod_lock);
228
229 return err;
230}
231EXPORT_SYMBOL_GPL(register_ife_op);
232
233static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len)
234{
235 int ret = 0;
236 /* XXX: unfortunately cant use nla_policy at this point
237 * because a length of 0 is valid in the case of
238 * "allow". "use" semantics do enforce for proper
239 * length and i couldve use nla_policy but it makes it hard
240 * to use it just for that..
241 */
242 if (ops->validate)
243 return ops->validate(val, len);
244
245 if (ops->metatype == NLA_U32)
246 ret = ife_validate_meta_u32(val, len);
247 else if (ops->metatype == NLA_U16)
248 ret = ife_validate_meta_u16(val, len);
249
250 return ret;
251}
252
253/* called when adding new meta information
WANG Cong067a7cd2016-06-20 13:37:18 -0700254 * under ife->tcf_lock for existing action
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500255*/
256static int load_metaops_and_vet(struct tcf_ife_info *ife, u32 metaid,
WANG Cong067a7cd2016-06-20 13:37:18 -0700257 void *val, int len, bool exists)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500258{
259 struct tcf_meta_ops *ops = find_ife_oplist(metaid);
260 int ret = 0;
261
262 if (!ops) {
263 ret = -ENOENT;
264#ifdef CONFIG_MODULES
WANG Cong067a7cd2016-06-20 13:37:18 -0700265 if (exists)
266 spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500267 rtnl_unlock();
268 request_module("ifemeta%u", metaid);
269 rtnl_lock();
WANG Cong067a7cd2016-06-20 13:37:18 -0700270 if (exists)
271 spin_lock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500272 ops = find_ife_oplist(metaid);
273#endif
274 }
275
276 if (ops) {
277 ret = 0;
278 if (len)
279 ret = ife_validate_metatype(ops, val, len);
280
281 module_put(ops->owner);
282 }
283
284 return ret;
285}
286
287/* called when adding new meta information
WANG Cong067a7cd2016-06-20 13:37:18 -0700288 * under ife->tcf_lock for existing action
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500289*/
290static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval,
WANG Cong817e9f22016-06-20 13:37:19 -0700291 int len, bool atomic)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500292{
293 struct tcf_meta_info *mi = NULL;
294 struct tcf_meta_ops *ops = find_ife_oplist(metaid);
295 int ret = 0;
296
297 if (!ops)
298 return -ENOENT;
299
WANG Cong817e9f22016-06-20 13:37:19 -0700300 mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500301 if (!mi) {
302 /*put back what find_ife_oplist took */
303 module_put(ops->owner);
304 return -ENOMEM;
305 }
306
307 mi->metaid = metaid;
308 mi->ops = ops;
309 if (len > 0) {
WANG Cong817e9f22016-06-20 13:37:19 -0700310 ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500311 if (ret != 0) {
312 kfree(mi);
313 module_put(ops->owner);
314 return ret;
315 }
316 }
317
318 list_add_tail(&mi->metalist, &ife->metalist);
319
320 return ret;
321}
322
323static int use_all_metadata(struct tcf_ife_info *ife)
324{
325 struct tcf_meta_ops *o;
326 int rc = 0;
327 int installed = 0;
328
WANG Cong817e9f22016-06-20 13:37:19 -0700329 read_lock(&ife_mod_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500330 list_for_each_entry(o, &ifeoplist, list) {
WANG Cong817e9f22016-06-20 13:37:19 -0700331 rc = add_metainfo(ife, o->metaid, NULL, 0, true);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500332 if (rc == 0)
333 installed += 1;
334 }
WANG Cong817e9f22016-06-20 13:37:19 -0700335 read_unlock(&ife_mod_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500336
337 if (installed)
338 return 0;
339 else
340 return -EINVAL;
341}
342
343static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife)
344{
345 struct tcf_meta_info *e;
346 struct nlattr *nest;
347 unsigned char *b = skb_tail_pointer(skb);
348 int total_encoded = 0;
349
350 /*can only happen on decode */
351 if (list_empty(&ife->metalist))
352 return 0;
353
354 nest = nla_nest_start(skb, TCA_IFE_METALST);
355 if (!nest)
356 goto out_nlmsg_trim;
357
358 list_for_each_entry(e, &ife->metalist, metalist) {
359 if (!e->ops->get(skb, e))
360 total_encoded += 1;
361 }
362
363 if (!total_encoded)
364 goto out_nlmsg_trim;
365
366 nla_nest_end(skb, nest);
367
368 return 0;
369
370out_nlmsg_trim:
371 nlmsg_trim(skb, b);
372 return -1;
373}
374
375/* under ife->tcf_lock */
376static void _tcf_ife_cleanup(struct tc_action *a, int bind)
377{
WANG Conga85a9702016-07-25 16:09:41 -0700378 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500379 struct tcf_meta_info *e, *n;
380
381 list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
382 module_put(e->ops->owner);
383 list_del(&e->metalist);
384 if (e->metaval) {
385 if (e->ops->release)
386 e->ops->release(e);
387 else
388 kfree(e->metaval);
389 }
390 kfree(e);
391 }
392}
393
394static void tcf_ife_cleanup(struct tc_action *a, int bind)
395{
WANG Conga85a9702016-07-25 16:09:41 -0700396 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500397
398 spin_lock_bh(&ife->tcf_lock);
399 _tcf_ife_cleanup(a, bind);
400 spin_unlock_bh(&ife->tcf_lock);
401}
402
WANG Cong067a7cd2016-06-20 13:37:18 -0700403/* under ife->tcf_lock for existing action */
404static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
405 bool exists)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500406{
407 int len = 0;
408 int rc = 0;
409 int i = 0;
410 void *val;
411
412 for (i = 1; i < max_metacnt; i++) {
413 if (tb[i]) {
414 val = nla_data(tb[i]);
415 len = nla_len(tb[i]);
416
WANG Cong067a7cd2016-06-20 13:37:18 -0700417 rc = load_metaops_and_vet(ife, i, val, len, exists);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500418 if (rc != 0)
419 return rc;
420
WANG Cong067a7cd2016-06-20 13:37:18 -0700421 rc = add_metainfo(ife, i, val, len, exists);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500422 if (rc)
423 return rc;
424 }
425 }
426
427 return rc;
428}
429
430static int tcf_ife_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -0700431 struct nlattr *est, struct tc_action **a,
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500432 int ovr, int bind)
433{
434 struct tc_action_net *tn = net_generic(net, ife_net_id);
435 struct nlattr *tb[TCA_IFE_MAX + 1];
436 struct nlattr *tb2[IFE_META_MAX + 1];
437 struct tcf_ife_info *ife;
438 struct tc_ife *parm;
439 u16 ife_type = 0;
440 u8 *daddr = NULL;
441 u8 *saddr = NULL;
WANG Congb2313072016-06-13 13:46:28 -0700442 bool exists = false;
443 int ret = 0;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500444 int err;
445
446 err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy);
447 if (err < 0)
448 return err;
449
450 if (!tb[TCA_IFE_PARMS])
451 return -EINVAL;
452
453 parm = nla_data(tb[TCA_IFE_PARMS]);
454
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400455 exists = tcf_hash_check(tn, parm->index, a, bind);
456 if (exists && bind)
457 return 0;
458
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500459 if (parm->flags & IFE_ENCODE) {
460 /* Until we get issued the ethertype, we cant have
461 * a default..
462 **/
463 if (!tb[TCA_IFE_TYPE]) {
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400464 if (exists)
WANG Conga85a9702016-07-25 16:09:41 -0700465 tcf_hash_release(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500466 pr_info("You MUST pass etherype for encoding\n");
467 return -EINVAL;
468 }
469 }
470
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400471 if (!exists) {
WANG Conga85a9702016-07-25 16:09:41 -0700472 ret = tcf_hash_create(tn, parm->index, est, a, &act_ife_ops,
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500473 bind, false);
474 if (ret)
475 return ret;
476 ret = ACT_P_CREATED;
477 } else {
WANG Conga85a9702016-07-25 16:09:41 -0700478 tcf_hash_release(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500479 if (!ovr)
480 return -EEXIST;
481 }
482
WANG Conga85a9702016-07-25 16:09:41 -0700483 ife = to_ife(*a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500484 ife->flags = parm->flags;
485
486 if (parm->flags & IFE_ENCODE) {
487 ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
488 if (tb[TCA_IFE_DMAC])
489 daddr = nla_data(tb[TCA_IFE_DMAC]);
490 if (tb[TCA_IFE_SMAC])
491 saddr = nla_data(tb[TCA_IFE_SMAC]);
492 }
493
WANG Cong067a7cd2016-06-20 13:37:18 -0700494 if (exists)
495 spin_lock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500496 ife->tcf_action = parm->action;
497
498 if (parm->flags & IFE_ENCODE) {
499 if (daddr)
500 ether_addr_copy(ife->eth_dst, daddr);
501 else
502 eth_zero_addr(ife->eth_dst);
503
504 if (saddr)
505 ether_addr_copy(ife->eth_src, saddr);
506 else
507 eth_zero_addr(ife->eth_src);
508
509 ife->eth_type = ife_type;
510 }
511
512 if (ret == ACT_P_CREATED)
513 INIT_LIST_HEAD(&ife->metalist);
514
515 if (tb[TCA_IFE_METALST]) {
516 err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST],
517 NULL);
518 if (err) {
519metadata_parse_err:
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400520 if (exists)
WANG Conga85a9702016-07-25 16:09:41 -0700521 tcf_hash_release(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500522 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700523 _tcf_ife_cleanup(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500524
WANG Cong067a7cd2016-06-20 13:37:18 -0700525 if (exists)
526 spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500527 return err;
528 }
529
WANG Cong067a7cd2016-06-20 13:37:18 -0700530 err = populate_metalist(ife, tb2, exists);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500531 if (err)
532 goto metadata_parse_err;
533
534 } else {
535 /* if no passed metadata allow list or passed allow-all
536 * then here we process by adding as many supported metadatum
537 * as we can. You better have at least one else we are
538 * going to bail out
539 */
540 err = use_all_metadata(ife);
541 if (err) {
542 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700543 _tcf_ife_cleanup(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500544
WANG Cong067a7cd2016-06-20 13:37:18 -0700545 if (exists)
546 spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500547 return err;
548 }
549 }
550
WANG Cong067a7cd2016-06-20 13:37:18 -0700551 if (exists)
552 spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500553
554 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700555 tcf_hash_insert(tn, *a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500556
557 return ret;
558}
559
560static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
561 int ref)
562{
563 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700564 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500565 struct tc_ife opt = {
566 .index = ife->tcf_index,
567 .refcnt = ife->tcf_refcnt - ref,
568 .bindcnt = ife->tcf_bindcnt - bind,
569 .action = ife->tcf_action,
570 .flags = ife->flags,
571 };
572 struct tcf_t t;
573
574 if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
575 goto nla_put_failure;
576
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400577 tcf_tm_dump(&t, &ife->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200578 if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500579 goto nla_put_failure;
580
581 if (!is_zero_ether_addr(ife->eth_dst)) {
582 if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst))
583 goto nla_put_failure;
584 }
585
586 if (!is_zero_ether_addr(ife->eth_src)) {
587 if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src))
588 goto nla_put_failure;
589 }
590
591 if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type))
592 goto nla_put_failure;
593
594 if (dump_metalist(skb, ife)) {
595 /*ignore failure to dump metalist */
596 pr_info("Failed to dump metalist\n");
597 }
598
599 return skb->len;
600
601nla_put_failure:
602 nlmsg_trim(skb, b);
603 return -1;
604}
605
606int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
607 u16 metaid, u16 mlen, void *mdata)
608{
609 struct tcf_meta_info *e;
610
611 /* XXX: use hash to speed up */
612 list_for_each_entry(e, &ife->metalist, metalist) {
613 if (metaid == e->metaid) {
614 if (e->ops) {
615 /* We check for decode presence already */
616 return e->ops->decode(skb, mdata, mlen);
617 }
618 }
619 }
620
621 return 0;
622}
623
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500624static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
625 struct tcf_result *res)
626{
WANG Conga85a9702016-07-25 16:09:41 -0700627 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500628 int action = ife->tcf_action;
Yotam Gigi295a6e02017-02-01 15:30:03 +0200629 u8 *ifehdr_end;
630 u8 *tlv_data;
631 u16 metalen;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500632
633 spin_lock(&ife->tcf_lock);
634 bstats_update(&ife->tcf_bstats, skb);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400635 tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500636 spin_unlock(&ife->tcf_lock);
637
Yotam Gigi295a6e02017-02-01 15:30:03 +0200638 if (skb_at_tc_ingress(skb))
639 skb_push(skb, skb->dev->hard_header_len);
640
641 tlv_data = ife_decode(skb, &metalen);
642 if (unlikely(!tlv_data)) {
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500643 spin_lock(&ife->tcf_lock);
644 ife->tcf_qstats.drops++;
645 spin_unlock(&ife->tcf_lock);
646 return TC_ACT_SHOT;
647 }
648
Yotam Gigi295a6e02017-02-01 15:30:03 +0200649 ifehdr_end = tlv_data + metalen;
650 for (; tlv_data < ifehdr_end; tlv_data = ife_tlv_meta_next(tlv_data)) {
651 u8 *curr_data;
652 u16 mtype;
653 u16 dlen;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500654
Yotam Gigi295a6e02017-02-01 15:30:03 +0200655 curr_data = ife_tlv_meta_decode(tlv_data, &mtype, &dlen, NULL);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500656
Yotam Gigi295a6e02017-02-01 15:30:03 +0200657 if (find_decode_metaid(skb, ife, mtype, dlen, curr_data)) {
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500658 /* abuse overlimits to count when we receive metadata
659 * but dont have an ops for it
660 */
Yotam Gigi295a6e02017-02-01 15:30:03 +0200661 pr_info_ratelimited("Unknown metaid %d dlen %d\n",
662 mtype, dlen);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500663 ife->tcf_qstats.overlimits++;
664 }
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500665 }
666
Yotam Gigi295a6e02017-02-01 15:30:03 +0200667 if (WARN_ON(tlv_data != ifehdr_end)) {
668 spin_lock(&ife->tcf_lock);
669 ife->tcf_qstats.drops++;
670 spin_unlock(&ife->tcf_lock);
671 return TC_ACT_SHOT;
672 }
673
674 skb->protocol = eth_type_trans(skb, skb->dev);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500675 skb_reset_network_header(skb);
Yotam Gigi295a6e02017-02-01 15:30:03 +0200676
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500677 return action;
678}
679
680/*XXX: check if we can do this at install time instead of current
681 * send data path
682**/
683static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
684{
685 struct tcf_meta_info *e, *n;
686 int tot_run_sz = 0, run_sz = 0;
687
688 list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
689 if (e->ops->check_presence) {
690 run_sz = e->ops->check_presence(skb, e);
691 tot_run_sz += run_sz;
692 }
693 }
694
695 return tot_run_sz;
696}
697
698static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
699 struct tcf_result *res)
700{
WANG Conga85a9702016-07-25 16:09:41 -0700701 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500702 int action = ife->tcf_action;
703 struct ethhdr *oethh; /* outer ether header */
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500704 struct tcf_meta_info *e;
705 /*
706 OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
707 where ORIGDATA = original ethernet header ...
708 */
709 u16 metalen = ife_get_sz(skb, ife);
710 int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
Yotam Gigi295a6e02017-02-01 15:30:03 +0200711 unsigned int skboff = 0;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500712 int new_len = skb->len + hdrm;
713 bool exceed_mtu = false;
Yotam Gigi295a6e02017-02-01 15:30:03 +0200714 void *ife_meta;
715 int err = 0;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500716
Willem de Bruijna5135bc2017-01-07 17:06:36 -0500717 if (!skb_at_tc_ingress(skb)) {
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500718 if (new_len > skb->dev->mtu)
719 exceed_mtu = true;
720 }
721
722 spin_lock(&ife->tcf_lock);
723 bstats_update(&ife->tcf_bstats, skb);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400724 tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500725
726 if (!metalen) { /* no metadata to send */
727 /* abuse overlimits to count when we allow packet
728 * with no metadata
729 */
730 ife->tcf_qstats.overlimits++;
731 spin_unlock(&ife->tcf_lock);
732 return action;
733 }
734 /* could be stupid policy setup or mtu config
735 * so lets be conservative.. */
736 if ((action == TC_ACT_SHOT) || exceed_mtu) {
737 ife->tcf_qstats.drops++;
738 spin_unlock(&ife->tcf_lock);
739 return TC_ACT_SHOT;
740 }
741
Willem de Bruijna5135bc2017-01-07 17:06:36 -0500742 if (skb_at_tc_ingress(skb))
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500743 skb_push(skb, skb->dev->hard_header_len);
744
Yotam Gigi295a6e02017-02-01 15:30:03 +0200745 ife_meta = ife_encode(skb, metalen);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500746
747 /* XXX: we dont have a clever way of telling encode to
748 * not repeat some of the computations that are done by
749 * ops->presence_check...
750 */
751 list_for_each_entry(e, &ife->metalist, metalist) {
752 if (e->ops->encode) {
Yotam Gigi295a6e02017-02-01 15:30:03 +0200753 err = e->ops->encode(skb, (void *)(ife_meta + skboff),
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500754 e);
755 }
756 if (err < 0) {
757 /* too corrupt to keep around if overwritten */
758 ife->tcf_qstats.drops++;
759 spin_unlock(&ife->tcf_lock);
760 return TC_ACT_SHOT;
761 }
762 skboff += err;
763 }
Yotam Gigi295a6e02017-02-01 15:30:03 +0200764 oethh = (struct ethhdr *)skb->data;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500765
766 if (!is_zero_ether_addr(ife->eth_src))
767 ether_addr_copy(oethh->h_source, ife->eth_src);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500768 if (!is_zero_ether_addr(ife->eth_dst))
769 ether_addr_copy(oethh->h_dest, ife->eth_dst);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500770 oethh->h_proto = htons(ife->eth_type);
771
Willem de Bruijna5135bc2017-01-07 17:06:36 -0500772 if (skb_at_tc_ingress(skb))
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500773 skb_pull(skb, skb->dev->hard_header_len);
774
775 spin_unlock(&ife->tcf_lock);
776
777 return action;
778}
779
780static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
781 struct tcf_result *res)
782{
WANG Conga85a9702016-07-25 16:09:41 -0700783 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500784
785 if (ife->flags & IFE_ENCODE)
786 return tcf_ife_encode(skb, a, res);
787
788 if (!(ife->flags & IFE_ENCODE))
789 return tcf_ife_decode(skb, a, res);
790
791 pr_info_ratelimited("unknown failure(policy neither de/encode\n");
792 spin_lock(&ife->tcf_lock);
793 bstats_update(&ife->tcf_bstats, skb);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400794 tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500795 ife->tcf_qstats.drops++;
796 spin_unlock(&ife->tcf_lock);
797
798 return TC_ACT_SHOT;
799}
800
801static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
802 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700803 const struct tc_action_ops *ops)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500804{
805 struct tc_action_net *tn = net_generic(net, ife_net_id);
806
WANG Conga85a9702016-07-25 16:09:41 -0700807 return tcf_generic_walker(tn, skb, cb, type, ops);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500808}
809
WANG Conga85a9702016-07-25 16:09:41 -0700810static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500811{
812 struct tc_action_net *tn = net_generic(net, ife_net_id);
813
814 return tcf_hash_search(tn, a, index);
815}
816
817static struct tc_action_ops act_ife_ops = {
818 .kind = "ife",
819 .type = TCA_ACT_IFE,
820 .owner = THIS_MODULE,
821 .act = tcf_ife_act,
822 .dump = tcf_ife_dump,
823 .cleanup = tcf_ife_cleanup,
824 .init = tcf_ife_init,
825 .walk = tcf_ife_walker,
826 .lookup = tcf_ife_search,
WANG Conga85a9702016-07-25 16:09:41 -0700827 .size = sizeof(struct tcf_ife_info),
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500828};
829
830static __net_init int ife_init_net(struct net *net)
831{
832 struct tc_action_net *tn = net_generic(net, ife_net_id);
833
834 return tc_action_net_init(tn, &act_ife_ops, IFE_TAB_MASK);
835}
836
837static void __net_exit ife_exit_net(struct net *net)
838{
839 struct tc_action_net *tn = net_generic(net, ife_net_id);
840
841 tc_action_net_exit(tn);
842}
843
844static struct pernet_operations ife_net_ops = {
845 .init = ife_init_net,
846 .exit = ife_exit_net,
847 .id = &ife_net_id,
848 .size = sizeof(struct tc_action_net),
849};
850
851static int __init ife_init_module(void)
852{
853 return tcf_register_action(&act_ife_ops, &ife_net_ops);
854}
855
856static void __exit ife_cleanup_module(void)
857{
858 tcf_unregister_action(&act_ife_ops, &ife_net_ops);
859}
860
861module_init(ife_init_module);
862module_exit(ife_cleanup_module);
863
864MODULE_AUTHOR("Jamal Hadi Salim(2015)");
865MODULE_DESCRIPTION("Inter-FE LFB action");
866MODULE_LICENSE("GPL");