blob: 36e4dcdac8dcc4ba2e710a1970c63d1e597f4d5c [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
Cong Wang264e0202019-07-22 21:43:00 -0700480 if (!nla)
481 return -EINVAL;
482
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500483 err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy);
484 if (err < 0)
485 return err;
486
487 if (!tb[TCA_IFE_PARMS])
488 return -EINVAL;
489
490 parm = nla_data(tb[TCA_IFE_PARMS]);
491
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400492 exists = tcf_hash_check(tn, parm->index, a, bind);
493 if (exists && bind)
494 return 0;
495
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500496 if (parm->flags & IFE_ENCODE) {
497 /* Until we get issued the ethertype, we cant have
498 * a default..
499 **/
500 if (!tb[TCA_IFE_TYPE]) {
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400501 if (exists)
WANG Conga85a9702016-07-25 16:09:41 -0700502 tcf_hash_release(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500503 pr_info("You MUST pass etherype for encoding\n");
504 return -EINVAL;
505 }
506 }
507
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400508 if (!exists) {
WANG Conga85a9702016-07-25 16:09:41 -0700509 ret = tcf_hash_create(tn, parm->index, est, a, &act_ife_ops,
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500510 bind, false);
511 if (ret)
512 return ret;
513 ret = ACT_P_CREATED;
514 } else {
WANG Conga85a9702016-07-25 16:09:41 -0700515 tcf_hash_release(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500516 if (!ovr)
517 return -EEXIST;
518 }
519
WANG Conga85a9702016-07-25 16:09:41 -0700520 ife = to_ife(*a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500521 ife->flags = parm->flags;
522
523 if (parm->flags & IFE_ENCODE) {
524 ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
525 if (tb[TCA_IFE_DMAC])
526 daddr = nla_data(tb[TCA_IFE_DMAC]);
527 if (tb[TCA_IFE_SMAC])
528 saddr = nla_data(tb[TCA_IFE_SMAC]);
529 }
530
WANG Cong067a7cd2016-06-20 13:37:18 -0700531 if (exists)
532 spin_lock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500533 ife->tcf_action = parm->action;
Cong Wangc8925752018-08-19 12:22:12 -0700534 if (exists)
535 spin_unlock_bh(&ife->tcf_lock);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500536
537 if (parm->flags & IFE_ENCODE) {
538 if (daddr)
539 ether_addr_copy(ife->eth_dst, daddr);
540 else
541 eth_zero_addr(ife->eth_dst);
542
543 if (saddr)
544 ether_addr_copy(ife->eth_src, saddr);
545 else
546 eth_zero_addr(ife->eth_src);
547
548 ife->eth_type = ife_type;
549 }
550
551 if (ret == ACT_P_CREATED)
552 INIT_LIST_HEAD(&ife->metalist);
553
554 if (tb[TCA_IFE_METALST]) {
555 err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST],
556 NULL);
557 if (err) {
558metadata_parse_err:
Jamal Hadi Salim4e8c8612016-05-10 16:49:31 -0400559 if (exists)
WANG Conga85a9702016-07-25 16:09:41 -0700560 tcf_hash_release(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500561 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700562 _tcf_ife_cleanup(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500563 return err;
564 }
565
WANG Cong067a7cd2016-06-20 13:37:18 -0700566 err = populate_metalist(ife, tb2, exists);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500567 if (err)
568 goto metadata_parse_err;
569
570 } else {
571 /* if no passed metadata allow list or passed allow-all
572 * then here we process by adding as many supported metadatum
573 * as we can. You better have at least one else we are
574 * going to bail out
575 */
Cong Wangc8925752018-08-19 12:22:12 -0700576 err = use_all_metadata(ife, exists);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500577 if (err) {
578 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700579 _tcf_ife_cleanup(*a, bind);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500580 return err;
581 }
582 }
583
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500584 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700585 tcf_hash_insert(tn, *a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500586
587 return ret;
588}
589
590static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
591 int ref)
592{
593 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700594 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500595 struct tc_ife opt = {
596 .index = ife->tcf_index,
597 .refcnt = ife->tcf_refcnt - ref,
598 .bindcnt = ife->tcf_bindcnt - bind,
599 .action = ife->tcf_action,
600 .flags = ife->flags,
601 };
602 struct tcf_t t;
603
604 if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
605 goto nla_put_failure;
606
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400607 tcf_tm_dump(&t, &ife->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200608 if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500609 goto nla_put_failure;
610
611 if (!is_zero_ether_addr(ife->eth_dst)) {
612 if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst))
613 goto nla_put_failure;
614 }
615
616 if (!is_zero_ether_addr(ife->eth_src)) {
617 if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src))
618 goto nla_put_failure;
619 }
620
621 if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type))
622 goto nla_put_failure;
623
624 if (dump_metalist(skb, ife)) {
625 /*ignore failure to dump metalist */
626 pr_info("Failed to dump metalist\n");
627 }
628
629 return skb->len;
630
631nla_put_failure:
632 nlmsg_trim(skb, b);
633 return -1;
634}
635
636int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
637 u16 metaid, u16 mlen, void *mdata)
638{
639 struct tcf_meta_info *e;
640
641 /* XXX: use hash to speed up */
642 list_for_each_entry(e, &ife->metalist, metalist) {
643 if (metaid == e->metaid) {
644 if (e->ops) {
645 /* We check for decode presence already */
646 return e->ops->decode(skb, mdata, mlen);
647 }
648 }
649 }
650
Alexander Aringe5d0f6a62018-04-20 15:15:03 -0400651 return -ENOENT;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500652}
653
654struct ifeheadr {
655 __be16 metalen;
656 u8 tlv_data[];
657};
658
659struct meta_tlvhdr {
660 __be16 type;
661 __be16 len;
662};
663
664static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
665 struct tcf_result *res)
666{
WANG Conga85a9702016-07-25 16:09:41 -0700667 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500668 int action = ife->tcf_action;
669 struct ifeheadr *ifehdr = (struct ifeheadr *)skb->data;
Yotam Gigic006da02016-09-26 13:45:26 +0300670 int ifehdrln = (int)ifehdr->metalen;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500671 struct meta_tlvhdr *tlv = (struct meta_tlvhdr *)(ifehdr->tlv_data);
672
673 spin_lock(&ife->tcf_lock);
674 bstats_update(&ife->tcf_bstats, skb);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400675 tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500676 spin_unlock(&ife->tcf_lock);
677
678 ifehdrln = ntohs(ifehdrln);
679 if (unlikely(!pskb_may_pull(skb, ifehdrln))) {
680 spin_lock(&ife->tcf_lock);
681 ife->tcf_qstats.drops++;
682 spin_unlock(&ife->tcf_lock);
683 return TC_ACT_SHOT;
684 }
685
686 skb_set_mac_header(skb, ifehdrln);
687 __skb_pull(skb, ifehdrln);
688 skb->protocol = eth_type_trans(skb, skb->dev);
689 ifehdrln -= IFE_METAHDRLEN;
690
691 while (ifehdrln > 0) {
692 u8 *tlvdata = (u8 *)tlv;
693 u16 mtype = tlv->type;
694 u16 mlen = tlv->len;
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400695 u16 alen;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500696
697 mtype = ntohs(mtype);
698 mlen = ntohs(mlen);
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400699 alen = NLA_ALIGN(mlen);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500700
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400701 if (find_decode_metaid(skb, ife, mtype, (mlen - NLA_HDRLEN),
702 (void *)(tlvdata + NLA_HDRLEN))) {
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500703 /* abuse overlimits to count when we receive metadata
704 * but dont have an ops for it
705 */
706 pr_info_ratelimited("Unknown metaid %d alnlen %d\n",
707 mtype, mlen);
708 ife->tcf_qstats.overlimits++;
709 }
710
Jamal Hadi Salim28a10c42016-08-22 07:10:20 -0400711 tlvdata += alen;
712 ifehdrln -= alen;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500713 tlv = (struct meta_tlvhdr *)tlvdata;
714 }
715
716 skb_reset_network_header(skb);
717 return action;
718}
719
720/*XXX: check if we can do this at install time instead of current
721 * send data path
722**/
723static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
724{
725 struct tcf_meta_info *e, *n;
726 int tot_run_sz = 0, run_sz = 0;
727
728 list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
729 if (e->ops->check_presence) {
730 run_sz = e->ops->check_presence(skb, e);
731 tot_run_sz += run_sz;
732 }
733 }
734
735 return tot_run_sz;
736}
737
738static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
739 struct tcf_result *res)
740{
WANG Conga85a9702016-07-25 16:09:41 -0700741 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500742 int action = ife->tcf_action;
743 struct ethhdr *oethh; /* outer ether header */
744 struct ethhdr *iethh; /* inner eth header */
745 struct tcf_meta_info *e;
746 /*
747 OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
748 where ORIGDATA = original ethernet header ...
749 */
750 u16 metalen = ife_get_sz(skb, ife);
751 int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
752 unsigned int skboff = skb->dev->hard_header_len;
753 u32 at = G_TC_AT(skb->tc_verd);
754 int new_len = skb->len + hdrm;
755 bool exceed_mtu = false;
756 int err;
757
758 if (at & AT_EGRESS) {
759 if (new_len > skb->dev->mtu)
760 exceed_mtu = true;
761 }
762
763 spin_lock(&ife->tcf_lock);
764 bstats_update(&ife->tcf_bstats, skb);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400765 tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500766
767 if (!metalen) { /* no metadata to send */
768 /* abuse overlimits to count when we allow packet
769 * with no metadata
770 */
771 ife->tcf_qstats.overlimits++;
772 spin_unlock(&ife->tcf_lock);
773 return action;
774 }
775 /* could be stupid policy setup or mtu config
776 * so lets be conservative.. */
777 if ((action == TC_ACT_SHOT) || exceed_mtu) {
778 ife->tcf_qstats.drops++;
779 spin_unlock(&ife->tcf_lock);
780 return TC_ACT_SHOT;
781 }
782
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500783 err = skb_cow_head(skb, hdrm);
784 if (unlikely(err)) {
785 ife->tcf_qstats.drops++;
786 spin_unlock(&ife->tcf_lock);
787 return TC_ACT_SHOT;
788 }
789
790 if (!(at & AT_EGRESS))
791 skb_push(skb, skb->dev->hard_header_len);
792
Yotam Gigi4b1d4882016-09-26 13:45:25 +0300793 iethh = (struct ethhdr *)skb->data;
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500794 __skb_push(skb, hdrm);
795 memcpy(skb->data, iethh, skb->mac_len);
796 skb_reset_mac_header(skb);
797 oethh = eth_hdr(skb);
798
799 /*total metadata length */
800 metalen += IFE_METAHDRLEN;
801 metalen = htons(metalen);
802 memcpy((skb->data + skboff), &metalen, IFE_METAHDRLEN);
803 skboff += IFE_METAHDRLEN;
804
805 /* XXX: we dont have a clever way of telling encode to
806 * not repeat some of the computations that are done by
807 * ops->presence_check...
808 */
809 list_for_each_entry(e, &ife->metalist, metalist) {
810 if (e->ops->encode) {
811 err = e->ops->encode(skb, (void *)(skb->data + skboff),
812 e);
813 }
814 if (err < 0) {
815 /* too corrupt to keep around if overwritten */
816 ife->tcf_qstats.drops++;
817 spin_unlock(&ife->tcf_lock);
818 return TC_ACT_SHOT;
819 }
820 skboff += err;
821 }
822
823 if (!is_zero_ether_addr(ife->eth_src))
824 ether_addr_copy(oethh->h_source, ife->eth_src);
825 else
826 ether_addr_copy(oethh->h_source, iethh->h_source);
827 if (!is_zero_ether_addr(ife->eth_dst))
828 ether_addr_copy(oethh->h_dest, ife->eth_dst);
829 else
830 ether_addr_copy(oethh->h_dest, iethh->h_dest);
831 oethh->h_proto = htons(ife->eth_type);
832
833 if (!(at & AT_EGRESS))
834 skb_pull(skb, skb->dev->hard_header_len);
835
836 spin_unlock(&ife->tcf_lock);
837
838 return action;
839}
840
841static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
842 struct tcf_result *res)
843{
WANG Conga85a9702016-07-25 16:09:41 -0700844 struct tcf_ife_info *ife = to_ife(a);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500845
846 if (ife->flags & IFE_ENCODE)
847 return tcf_ife_encode(skb, a, res);
848
849 if (!(ife->flags & IFE_ENCODE))
850 return tcf_ife_decode(skb, a, res);
851
852 pr_info_ratelimited("unknown failure(policy neither de/encode\n");
853 spin_lock(&ife->tcf_lock);
854 bstats_update(&ife->tcf_bstats, skb);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400855 tcf_lastuse_update(&ife->tcf_tm);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500856 ife->tcf_qstats.drops++;
857 spin_unlock(&ife->tcf_lock);
858
859 return TC_ACT_SHOT;
860}
861
862static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
863 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700864 const struct tc_action_ops *ops)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500865{
866 struct tc_action_net *tn = net_generic(net, ife_net_id);
867
WANG Conga85a9702016-07-25 16:09:41 -0700868 return tcf_generic_walker(tn, skb, cb, type, ops);
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500869}
870
WANG Conga85a9702016-07-25 16:09:41 -0700871static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index)
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500872{
873 struct tc_action_net *tn = net_generic(net, ife_net_id);
874
875 return tcf_hash_search(tn, a, index);
876}
877
878static struct tc_action_ops act_ife_ops = {
879 .kind = "ife",
880 .type = TCA_ACT_IFE,
881 .owner = THIS_MODULE,
882 .act = tcf_ife_act,
883 .dump = tcf_ife_dump,
884 .cleanup = tcf_ife_cleanup,
885 .init = tcf_ife_init,
886 .walk = tcf_ife_walker,
887 .lookup = tcf_ife_search,
WANG Conga85a9702016-07-25 16:09:41 -0700888 .size = sizeof(struct tcf_ife_info),
Jamal Hadi Salimef6980b2016-02-27 08:08:54 -0500889};
890
891static __net_init int ife_init_net(struct net *net)
892{
893 struct tc_action_net *tn = net_generic(net, ife_net_id);
894
895 return tc_action_net_init(tn, &act_ife_ops, IFE_TAB_MASK);
896}
897
898static void __net_exit ife_exit_net(struct net *net)
899{
900 struct tc_action_net *tn = net_generic(net, ife_net_id);
901
902 tc_action_net_exit(tn);
903}
904
905static struct pernet_operations ife_net_ops = {
906 .init = ife_init_net,
907 .exit = ife_exit_net,
908 .id = &ife_net_id,
909 .size = sizeof(struct tc_action_net),
910};
911
912static int __init ife_init_module(void)
913{
914 return tcf_register_action(&act_ife_ops, &ife_net_ops);
915}
916
917static void __exit ife_cleanup_module(void)
918{
919 tcf_unregister_action(&act_ife_ops, &ife_net_ops);
920}
921
922module_init(ife_init_module);
923module_exit(ife_cleanup_module);
924
925MODULE_AUTHOR("Jamal Hadi Salim(2015)");
926MODULE_DESCRIPTION("Inter-FE LFB action");
927MODULE_LICENSE("GPL");