blob: d2932dc4c83dfef0820c8614ca12629ac0b94680 [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>
35
36#define IFE_TAB_MASK 15
37
38static int ife_net_id;
39static int max_metacnt = IFE_META_MAX + 1;
WANG Conga85a9702016-07-25 16:09:41 -070040static struct tc_action_ops act_ife_ops;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -050041
42static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = {
43 [TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)},
44 [TCA_IFE_DMAC] = { .len = ETH_ALEN},
45 [TCA_IFE_SMAC] = { .len = ETH_ALEN},
46 [TCA_IFE_TYPE] = { .type = NLA_U16},
47};
48
49/* Caller takes care of presenting data in network order
50*/
51int ife_tlv_meta_encode(void *skbdata, u16 attrtype, u16 dlen, const void *dval)
52{
53 u32 *tlv = (u32 *)(skbdata);
54 u16 totlen = nla_total_size(dlen); /*alignment + hdr */
55 char *dptr = (char *)tlv + NLA_HDRLEN;
Yotam Gigic006da02016-09-26 13:45:26 +030056 u32 htlv = attrtype << 16 | (dlen + NLA_HDRLEN);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -050057
58 *tlv = htonl(htlv);
59 memset(dptr, 0, totlen - NLA_HDRLEN);
60 memcpy(dptr, dval, dlen);
61
62 return totlen;
63}
64EXPORT_SYMBOL_GPL(ife_tlv_meta_encode);
65
Jamal Hadi Salim6a5d58b2016-09-18 07:31:42 -040066int ife_encode_meta_u16(u16 metaval, void *skbdata, struct tcf_meta_info *mi)
67{
68 u16 edata = 0;
69
70 if (mi->metaval)
71 edata = *(u16 *)mi->metaval;
72 else if (metaval)
73 edata = metaval;
74
75 if (!edata) /* will not encode */
76 return 0;
77
78 edata = htons(edata);
79 return ife_tlv_meta_encode(skbdata, mi->metaid, 2, &edata);
80}
81EXPORT_SYMBOL_GPL(ife_encode_meta_u16);
82
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -050083int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi)
84{
85 if (mi->metaval)
86 return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval);
87 else
88 return nla_put(skb, mi->metaid, 0, NULL);
89}
90EXPORT_SYMBOL_GPL(ife_get_meta_u32);
91
92int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi)
93{
94 if (metaval || mi->metaval)
95 return 8; /* T+L+V == 2+2+4 */
96
97 return 0;
98}
99EXPORT_SYMBOL_GPL(ife_check_meta_u32);
100
Jamal Hadi Salim6a5d58b2016-09-18 07:31:42 -0400101int ife_check_meta_u16(u16 metaval, struct tcf_meta_info *mi)
102{
103 if (metaval || mi->metaval)
104 return 8; /* T+L+(V) == 2+2+(2+2bytepad) */
105
106 return 0;
107}
108EXPORT_SYMBOL_GPL(ife_check_meta_u16);
109
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500110int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi)
111{
112 u32 edata = metaval;
113
114 if (mi->metaval)
115 edata = *(u32 *)mi->metaval;
116 else if (metaval)
117 edata = metaval;
118
119 if (!edata) /* will not encode */
120 return 0;
121
122 edata = htonl(edata);
123 return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata);
124}
125EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
126
127int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi)
128{
129 if (mi->metaval)
130 return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval);
131 else
132 return nla_put(skb, mi->metaid, 0, NULL);
133}
134EXPORT_SYMBOL_GPL(ife_get_meta_u16);
135
WANG Cong067a7cd2016-06-20 13:37:18 -0700136int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500137{
WANG Cong067a7cd2016-06-20 13:37:18 -0700138 mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500139 if (!mi->metaval)
140 return -ENOMEM;
141
142 return 0;
143}
144EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
145
WANG Cong067a7cd2016-06-20 13:37:18 -0700146int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500147{
WANG Cong067a7cd2016-06-20 13:37:18 -0700148 mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500149 if (!mi->metaval)
150 return -ENOMEM;
151
152 return 0;
153}
154EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
155
156void ife_release_meta_gen(struct tcf_meta_info *mi)
157{
158 kfree(mi->metaval);
159}
160EXPORT_SYMBOL_GPL(ife_release_meta_gen);
161
162int ife_validate_meta_u32(void *val, int len)
163{
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400164 if (len == sizeof(u32))
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500165 return 0;
166
167 return -EINVAL;
168}
169EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
170
171int ife_validate_meta_u16(void *val, int len)
172{
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400173 /* length will not include padding */
174 if (len == sizeof(u16))
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500175 return 0;
176
177 return -EINVAL;
178}
179EXPORT_SYMBOL_GPL(ife_validate_meta_u16);
180
181static LIST_HEAD(ifeoplist);
182static DEFINE_RWLOCK(ife_mod_lock);
183
184static struct tcf_meta_ops *find_ife_oplist(u16 metaid)
185{
186 struct tcf_meta_ops *o;
187
188 read_lock(&ife_mod_lock);
189 list_for_each_entry(o, &ifeoplist, list) {
190 if (o->metaid == metaid) {
191 if (!try_module_get(o->owner))
192 o = NULL;
193 read_unlock(&ife_mod_lock);
194 return o;
195 }
196 }
197 read_unlock(&ife_mod_lock);
198
199 return NULL;
200}
201
202int register_ife_op(struct tcf_meta_ops *mops)
203{
204 struct tcf_meta_ops *m;
205
206 if (!mops->metaid || !mops->metatype || !mops->name ||
207 !mops->check_presence || !mops->encode || !mops->decode ||
208 !mops->get || !mops->alloc)
209 return -EINVAL;
210
211 write_lock(&ife_mod_lock);
212
213 list_for_each_entry(m, &ifeoplist, list) {
214 if (m->metaid == mops->metaid ||
215 (strcmp(mops->name, m->name) == 0)) {
216 write_unlock(&ife_mod_lock);
217 return -EEXIST;
218 }
219 }
220
221 if (!mops->release)
222 mops->release = ife_release_meta_gen;
223
224 list_add_tail(&mops->list, &ifeoplist);
225 write_unlock(&ife_mod_lock);
226 return 0;
227}
228EXPORT_SYMBOL_GPL(unregister_ife_op);
229
230int unregister_ife_op(struct tcf_meta_ops *mops)
231{
232 struct tcf_meta_ops *m;
233 int err = -ENOENT;
234
235 write_lock(&ife_mod_lock);
236 list_for_each_entry(m, &ifeoplist, list) {
237 if (m->metaid == mops->metaid) {
238 list_del(&mops->list);
239 err = 0;
240 break;
241 }
242 }
243 write_unlock(&ife_mod_lock);
244
245 return err;
246}
247EXPORT_SYMBOL_GPL(register_ife_op);
248
249static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len)
250{
251 int ret = 0;
252 /* XXX: unfortunately cant use nla_policy at this point
253 * because a length of 0 is valid in the case of
254 * "allow". "use" semantics do enforce for proper
255 * length and i couldve use nla_policy but it makes it hard
256 * to use it just for that..
257 */
258 if (ops->validate)
259 return ops->validate(val, len);
260
261 if (ops->metatype == NLA_U32)
262 ret = ife_validate_meta_u32(val, len);
263 else if (ops->metatype == NLA_U16)
264 ret = ife_validate_meta_u16(val, len);
265
266 return ret;
267}
268
269/* called when adding new meta information
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500270*/
Cong Wangc8925752018-08-19 12:22:12 -0700271static int load_metaops_and_vet(u32 metaid, void *val, int len)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500272{
273 struct tcf_meta_ops *ops = find_ife_oplist(metaid);
274 int ret = 0;
275
276 if (!ops) {
277 ret = -ENOENT;
278#ifdef CONFIG_MODULES
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500279 rtnl_unlock();
280 request_module("ifemeta%u", metaid);
281 rtnl_lock();
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500282 ops = find_ife_oplist(metaid);
283#endif
284 }
285
286 if (ops) {
287 ret = 0;
288 if (len)
289 ret = ife_validate_metatype(ops, val, len);
290
291 module_put(ops->owner);
292 }
293
294 return ret;
295}
296
297/* called when adding new meta information
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500298*/
Cong Wanga93288c2018-08-19 12:22:13 -0700299static int __add_metainfo(const struct tcf_meta_ops *ops,
300 struct tcf_ife_info *ife, u32 metaid, void *metaval,
301 int len, bool atomic, bool exists)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500302{
303 struct tcf_meta_info *mi = NULL;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500304 int ret = 0;
305
WANG Cong817e9f22016-06-20 13:37:19 -0700306 mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL);
Cong Wanga93288c2018-08-19 12:22:13 -0700307 if (!mi)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500308 return -ENOMEM;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500309
310 mi->metaid = metaid;
311 mi->ops = ops;
312 if (len > 0) {
WANG Cong817e9f22016-06-20 13:37:19 -0700313 ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500314 if (ret != 0) {
315 kfree(mi);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500316 return ret;
317 }
318 }
319
Cong Wangc8925752018-08-19 12:22:12 -0700320 if (exists)
321 spin_lock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500322 list_add_tail(&mi->metalist, &ife->metalist);
Cong Wangc8925752018-08-19 12:22:12 -0700323 if (exists)
324 spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500325
326 return ret;
327}
328
Vlad Buslov947c2622018-09-04 00:44:42 +0300329static int add_metainfo_and_get_ops(const struct tcf_meta_ops *ops,
330 struct tcf_ife_info *ife, u32 metaid,
331 bool exists)
332{
333 int ret;
334
335 if (!try_module_get(ops->owner))
336 return -ENOENT;
337 ret = __add_metainfo(ops, ife, metaid, NULL, 0, true, exists);
338 if (ret)
339 module_put(ops->owner);
340 return ret;
341}
342
Cong Wanga93288c2018-08-19 12:22:13 -0700343static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval,
344 int len, bool exists)
345{
346 const struct tcf_meta_ops *ops = find_ife_oplist(metaid);
347 int ret;
348
349 if (!ops)
350 return -ENOENT;
351 ret = __add_metainfo(ops, ife, metaid, metaval, len, false, exists);
352 if (ret)
353 /*put back what find_ife_oplist took */
354 module_put(ops->owner);
355 return ret;
356}
357
Cong Wangc8925752018-08-19 12:22:12 -0700358static int use_all_metadata(struct tcf_ife_info *ife, bool exists)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500359{
360 struct tcf_meta_ops *o;
361 int rc = 0;
362 int installed = 0;
363
WANG Cong817e9f22016-06-20 13:37:19 -0700364 read_lock(&ife_mod_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500365 list_for_each_entry(o, &ifeoplist, list) {
Vlad Buslov947c2622018-09-04 00:44:42 +0300366 rc = add_metainfo_and_get_ops(o, ife, o->metaid, exists);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500367 if (rc == 0)
368 installed += 1;
369 }
WANG Cong817e9f22016-06-20 13:37:19 -0700370 read_unlock(&ife_mod_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500371
372 if (installed)
373 return 0;
374 else
375 return -EINVAL;
376}
377
378static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife)
379{
380 struct tcf_meta_info *e;
381 struct nlattr *nest;
382 unsigned char *b = skb_tail_pointer(skb);
383 int total_encoded = 0;
384
385 /*can only happen on decode */
386 if (list_empty(&ife->metalist))
387 return 0;
388
389 nest = nla_nest_start(skb, TCA_IFE_METALST);
390 if (!nest)
391 goto out_nlmsg_trim;
392
393 list_for_each_entry(e, &ife->metalist, metalist) {
394 if (!e->ops->get(skb, e))
395 total_encoded += 1;
396 }
397
398 if (!total_encoded)
399 goto out_nlmsg_trim;
400
401 nla_nest_end(skb, nest);
402
403 return 0;
404
405out_nlmsg_trim:
406 nlmsg_trim(skb, b);
407 return -1;
408}
409
410/* under ife->tcf_lock */
411static void _tcf_ife_cleanup(struct tc_action *a, int bind)
412{
WANG Conga85a9702016-07-25 16:09:41 -0700413 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500414 struct tcf_meta_info *e, *n;
415
416 list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500417 list_del(&e->metalist);
418 if (e->metaval) {
419 if (e->ops->release)
420 e->ops->release(e);
421 else
422 kfree(e->metaval);
423 }
Cong Wang4fb15ff2018-09-03 11:08:15 -0700424 module_put(e->ops->owner);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500425 kfree(e);
426 }
427}
428
429static void tcf_ife_cleanup(struct tc_action *a, int bind)
430{
WANG Conga85a9702016-07-25 16:09:41 -0700431 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500432
433 spin_lock_bh(&ife->tcf_lock);
434 _tcf_ife_cleanup(a, bind);
435 spin_unlock_bh(&ife->tcf_lock);
436}
437
WANG Cong067a7cd2016-06-20 13:37:18 -0700438static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
439 bool exists)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500440{
441 int len = 0;
442 int rc = 0;
443 int i = 0;
444 void *val;
445
446 for (i = 1; i < max_metacnt; i++) {
447 if (tb[i]) {
448 val = nla_data(tb[i]);
449 len = nla_len(tb[i]);
450
Cong Wangc8925752018-08-19 12:22:12 -0700451 rc = load_metaops_and_vet(i, val, len);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500452 if (rc != 0)
453 return rc;
454
Cong Wanga93288c2018-08-19 12:22:13 -0700455 rc = add_metainfo(ife, i, val, len, exists);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500456 if (rc)
457 return rc;
458 }
459 }
460
461 return rc;
462}
463
464static int tcf_ife_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -0700465 struct nlattr *est, struct tc_action **a,
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500466 int ovr, int bind)
467{
468 struct tc_action_net *tn = net_generic(net, ife_net_id);
469 struct nlattr *tb[TCA_IFE_MAX + 1];
470 struct nlattr *tb2[IFE_META_MAX + 1];
471 struct tcf_ife_info *ife;
472 struct tc_ife *parm;
473 u16 ife_type = 0;
474 u8 *daddr = NULL;
475 u8 *saddr = NULL;
WANG Congb2313072016-06-13 13:46:28 -0700476 bool exists = false;
477 int ret = 0;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500478 int err;
479
480 err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy);
481 if (err < 0)
482 return err;
483
484 if (!tb[TCA_IFE_PARMS])
485 return -EINVAL;
486
487 parm = nla_data(tb[TCA_IFE_PARMS]);
488
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400489 exists = tcf_hash_check(tn, parm->index, a, bind);
490 if (exists && bind)
491 return 0;
492
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500493 if (parm->flags & IFE_ENCODE) {
494 /* Until we get issued the ethertype, we cant have
495 * a default..
496 **/
497 if (!tb[TCA_IFE_TYPE]) {
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400498 if (exists)
WANG Conga85a9702016-07-25 16:09:41 -0700499 tcf_hash_release(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500500 pr_info("You MUST pass etherype for encoding\n");
501 return -EINVAL;
502 }
503 }
504
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400505 if (!exists) {
WANG Conga85a9702016-07-25 16:09:41 -0700506 ret = tcf_hash_create(tn, parm->index, est, a, &act_ife_ops,
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500507 bind, false);
508 if (ret)
509 return ret;
510 ret = ACT_P_CREATED;
511 } else {
WANG Conga85a9702016-07-25 16:09:41 -0700512 tcf_hash_release(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500513 if (!ovr)
514 return -EEXIST;
515 }
516
WANG Conga85a9702016-07-25 16:09:41 -0700517 ife = to_ife(*a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500518 ife->flags = parm->flags;
519
520 if (parm->flags & IFE_ENCODE) {
521 ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
522 if (tb[TCA_IFE_DMAC])
523 daddr = nla_data(tb[TCA_IFE_DMAC]);
524 if (tb[TCA_IFE_SMAC])
525 saddr = nla_data(tb[TCA_IFE_SMAC]);
526 }
527
WANG Cong067a7cd2016-06-20 13:37:18 -0700528 if (exists)
529 spin_lock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500530 ife->tcf_action = parm->action;
Cong Wangc8925752018-08-19 12:22:12 -0700531 if (exists)
532 spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500533
534 if (parm->flags & IFE_ENCODE) {
535 if (daddr)
536 ether_addr_copy(ife->eth_dst, daddr);
537 else
538 eth_zero_addr(ife->eth_dst);
539
540 if (saddr)
541 ether_addr_copy(ife->eth_src, saddr);
542 else
543 eth_zero_addr(ife->eth_src);
544
545 ife->eth_type = ife_type;
546 }
547
548 if (ret == ACT_P_CREATED)
549 INIT_LIST_HEAD(&ife->metalist);
550
551 if (tb[TCA_IFE_METALST]) {
552 err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST],
553 NULL);
554 if (err) {
555metadata_parse_err:
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400556 if (exists)
WANG Conga85a9702016-07-25 16:09:41 -0700557 tcf_hash_release(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500558 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700559 _tcf_ife_cleanup(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500560 return err;
561 }
562
WANG Cong067a7cd2016-06-20 13:37:18 -0700563 err = populate_metalist(ife, tb2, exists);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500564 if (err)
565 goto metadata_parse_err;
566
567 } else {
568 /* if no passed metadata allow list or passed allow-all
569 * then here we process by adding as many supported metadatum
570 * as we can. You better have at least one else we are
571 * going to bail out
572 */
Cong Wangc8925752018-08-19 12:22:12 -0700573 err = use_all_metadata(ife, exists);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500574 if (err) {
575 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700576 _tcf_ife_cleanup(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500577 return err;
578 }
579 }
580
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500581 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700582 tcf_hash_insert(tn, *a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500583
584 return ret;
585}
586
587static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
588 int ref)
589{
590 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700591 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500592 struct tc_ife opt = {
593 .index = ife->tcf_index,
594 .refcnt = ife->tcf_refcnt - ref,
595 .bindcnt = ife->tcf_bindcnt - bind,
596 .action = ife->tcf_action,
597 .flags = ife->flags,
598 };
599 struct tcf_t t;
600
601 if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
602 goto nla_put_failure;
603
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400604 tcf_tm_dump(&t, &ife->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200605 if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500606 goto nla_put_failure;
607
608 if (!is_zero_ether_addr(ife->eth_dst)) {
609 if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst))
610 goto nla_put_failure;
611 }
612
613 if (!is_zero_ether_addr(ife->eth_src)) {
614 if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src))
615 goto nla_put_failure;
616 }
617
618 if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type))
619 goto nla_put_failure;
620
621 if (dump_metalist(skb, ife)) {
622 /*ignore failure to dump metalist */
623 pr_info("Failed to dump metalist\n");
624 }
625
626 return skb->len;
627
628nla_put_failure:
629 nlmsg_trim(skb, b);
630 return -1;
631}
632
633int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
634 u16 metaid, u16 mlen, void *mdata)
635{
636 struct tcf_meta_info *e;
637
638 /* XXX: use hash to speed up */
639 list_for_each_entry(e, &ife->metalist, metalist) {
640 if (metaid == e->metaid) {
641 if (e->ops) {
642 /* We check for decode presence already */
643 return e->ops->decode(skb, mdata, mlen);
644 }
645 }
646 }
647
Alexander Aringe5d0f6a62018-04-20 15:15:03 -0400648 return -ENOENT;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500649}
650
651struct ifeheadr {
652 __be16 metalen;
653 u8 tlv_data[];
654};
655
656struct meta_tlvhdr {
657 __be16 type;
658 __be16 len;
659};
660
661static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
662 struct tcf_result *res)
663{
WANG Conga85a9702016-07-25 16:09:41 -0700664 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500665 int action = ife->tcf_action;
666 struct ifeheadr *ifehdr = (struct ifeheadr *)skb->data;
Yotam Gigic006da02016-09-26 13:45:26 +0300667 int ifehdrln = (int)ifehdr->metalen;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500668 struct meta_tlvhdr *tlv = (struct meta_tlvhdr *)(ifehdr->tlv_data);
669
670 spin_lock(&ife->tcf_lock);
671 bstats_update(&ife->tcf_bstats, skb);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400672 tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500673 spin_unlock(&ife->tcf_lock);
674
675 ifehdrln = ntohs(ifehdrln);
676 if (unlikely(!pskb_may_pull(skb, ifehdrln))) {
677 spin_lock(&ife->tcf_lock);
678 ife->tcf_qstats.drops++;
679 spin_unlock(&ife->tcf_lock);
680 return TC_ACT_SHOT;
681 }
682
683 skb_set_mac_header(skb, ifehdrln);
684 __skb_pull(skb, ifehdrln);
685 skb->protocol = eth_type_trans(skb, skb->dev);
686 ifehdrln -= IFE_METAHDRLEN;
687
688 while (ifehdrln > 0) {
689 u8 *tlvdata = (u8 *)tlv;
690 u16 mtype = tlv->type;
691 u16 mlen = tlv->len;
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400692 u16 alen;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500693
694 mtype = ntohs(mtype);
695 mlen = ntohs(mlen);
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400696 alen = NLA_ALIGN(mlen);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500697
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400698 if (find_decode_metaid(skb, ife, mtype, (mlen - NLA_HDRLEN),
699 (void *)(tlvdata + NLA_HDRLEN))) {
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500700 /* abuse overlimits to count when we receive metadata
701 * but dont have an ops for it
702 */
703 pr_info_ratelimited("Unknown metaid %d alnlen %d\n",
704 mtype, mlen);
705 ife->tcf_qstats.overlimits++;
706 }
707
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400708 tlvdata += alen;
709 ifehdrln -= alen;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500710 tlv = (struct meta_tlvhdr *)tlvdata;
711 }
712
713 skb_reset_network_header(skb);
714 return action;
715}
716
717/*XXX: check if we can do this at install time instead of current
718 * send data path
719**/
720static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
721{
722 struct tcf_meta_info *e, *n;
723 int tot_run_sz = 0, run_sz = 0;
724
725 list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
726 if (e->ops->check_presence) {
727 run_sz = e->ops->check_presence(skb, e);
728 tot_run_sz += run_sz;
729 }
730 }
731
732 return tot_run_sz;
733}
734
735static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
736 struct tcf_result *res)
737{
WANG Conga85a9702016-07-25 16:09:41 -0700738 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500739 int action = ife->tcf_action;
740 struct ethhdr *oethh; /* outer ether header */
741 struct ethhdr *iethh; /* inner eth header */
742 struct tcf_meta_info *e;
743 /*
744 OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
745 where ORIGDATA = original ethernet header ...
746 */
747 u16 metalen = ife_get_sz(skb, ife);
748 int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
749 unsigned int skboff = skb->dev->hard_header_len;
750 u32 at = G_TC_AT(skb->tc_verd);
751 int new_len = skb->len + hdrm;
752 bool exceed_mtu = false;
753 int err;
754
755 if (at & AT_EGRESS) {
756 if (new_len > skb->dev->mtu)
757 exceed_mtu = true;
758 }
759
760 spin_lock(&ife->tcf_lock);
761 bstats_update(&ife->tcf_bstats, skb);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400762 tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500763
764 if (!metalen) { /* no metadata to send */
765 /* abuse overlimits to count when we allow packet
766 * with no metadata
767 */
768 ife->tcf_qstats.overlimits++;
769 spin_unlock(&ife->tcf_lock);
770 return action;
771 }
772 /* could be stupid policy setup or mtu config
773 * so lets be conservative.. */
774 if ((action == TC_ACT_SHOT) || exceed_mtu) {
775 ife->tcf_qstats.drops++;
776 spin_unlock(&ife->tcf_lock);
777 return TC_ACT_SHOT;
778 }
779
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500780 err = skb_cow_head(skb, hdrm);
781 if (unlikely(err)) {
782 ife->tcf_qstats.drops++;
783 spin_unlock(&ife->tcf_lock);
784 return TC_ACT_SHOT;
785 }
786
787 if (!(at & AT_EGRESS))
788 skb_push(skb, skb->dev->hard_header_len);
789
Yotam Gigi4b1d4882016-09-26 13:45:25 +0300790 iethh = (struct ethhdr *)skb->data;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500791 __skb_push(skb, hdrm);
792 memcpy(skb->data, iethh, skb->mac_len);
793 skb_reset_mac_header(skb);
794 oethh = eth_hdr(skb);
795
796 /*total metadata length */
797 metalen += IFE_METAHDRLEN;
798 metalen = htons(metalen);
799 memcpy((skb->data + skboff), &metalen, IFE_METAHDRLEN);
800 skboff += IFE_METAHDRLEN;
801
802 /* XXX: we dont have a clever way of telling encode to
803 * not repeat some of the computations that are done by
804 * ops->presence_check...
805 */
806 list_for_each_entry(e, &ife->metalist, metalist) {
807 if (e->ops->encode) {
808 err = e->ops->encode(skb, (void *)(skb->data + skboff),
809 e);
810 }
811 if (err < 0) {
812 /* too corrupt to keep around if overwritten */
813 ife->tcf_qstats.drops++;
814 spin_unlock(&ife->tcf_lock);
815 return TC_ACT_SHOT;
816 }
817 skboff += err;
818 }
819
820 if (!is_zero_ether_addr(ife->eth_src))
821 ether_addr_copy(oethh->h_source, ife->eth_src);
822 else
823 ether_addr_copy(oethh->h_source, iethh->h_source);
824 if (!is_zero_ether_addr(ife->eth_dst))
825 ether_addr_copy(oethh->h_dest, ife->eth_dst);
826 else
827 ether_addr_copy(oethh->h_dest, iethh->h_dest);
828 oethh->h_proto = htons(ife->eth_type);
829
830 if (!(at & AT_EGRESS))
831 skb_pull(skb, skb->dev->hard_header_len);
832
833 spin_unlock(&ife->tcf_lock);
834
835 return action;
836}
837
838static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
839 struct tcf_result *res)
840{
WANG Conga85a9702016-07-25 16:09:41 -0700841 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500842
843 if (ife->flags & IFE_ENCODE)
844 return tcf_ife_encode(skb, a, res);
845
846 if (!(ife->flags & IFE_ENCODE))
847 return tcf_ife_decode(skb, a, res);
848
849 pr_info_ratelimited("unknown failure(policy neither de/encode\n");
850 spin_lock(&ife->tcf_lock);
851 bstats_update(&ife->tcf_bstats, skb);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400852 tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500853 ife->tcf_qstats.drops++;
854 spin_unlock(&ife->tcf_lock);
855
856 return TC_ACT_SHOT;
857}
858
859static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
860 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700861 const struct tc_action_ops *ops)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500862{
863 struct tc_action_net *tn = net_generic(net, ife_net_id);
864
WANG Conga85a9702016-07-25 16:09:41 -0700865 return tcf_generic_walker(tn, skb, cb, type, ops);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500866}
867
WANG Conga85a9702016-07-25 16:09:41 -0700868static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500869{
870 struct tc_action_net *tn = net_generic(net, ife_net_id);
871
872 return tcf_hash_search(tn, a, index);
873}
874
875static struct tc_action_ops act_ife_ops = {
876 .kind = "ife",
877 .type = TCA_ACT_IFE,
878 .owner = THIS_MODULE,
879 .act = tcf_ife_act,
880 .dump = tcf_ife_dump,
881 .cleanup = tcf_ife_cleanup,
882 .init = tcf_ife_init,
883 .walk = tcf_ife_walker,
884 .lookup = tcf_ife_search,
WANG Conga85a9702016-07-25 16:09:41 -0700885 .size = sizeof(struct tcf_ife_info),
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500886};
887
888static __net_init int ife_init_net(struct net *net)
889{
890 struct tc_action_net *tn = net_generic(net, ife_net_id);
891
892 return tc_action_net_init(tn, &act_ife_ops, IFE_TAB_MASK);
893}
894
895static void __net_exit ife_exit_net(struct net *net)
896{
897 struct tc_action_net *tn = net_generic(net, ife_net_id);
898
899 tc_action_net_exit(tn);
900}
901
902static struct pernet_operations ife_net_ops = {
903 .init = ife_init_net,
904 .exit = ife_exit_net,
905 .id = &ife_net_id,
906 .size = sizeof(struct tc_action_net),
907};
908
909static int __init ife_init_module(void)
910{
911 return tcf_register_action(&act_ife_ops, &ife_net_ops);
912}
913
914static void __exit ife_cleanup_module(void)
915{
916 tcf_unregister_action(&act_ife_ops, &ife_net_ops);
917}
918
919module_init(ife_init_module);
920module_exit(ife_cleanup_module);
921
922MODULE_AUTHOR("Jamal Hadi Salim(2015)");
923MODULE_DESCRIPTION("Inter-FE LFB action");
924MODULE_LICENSE("GPL");