xref: /openbmc/linux/net/sched/act_ife.c (revision 93f5715e)
1 /*
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 #include <net/ife.h>
36 
37 static unsigned int ife_net_id;
38 static int max_metacnt = IFE_META_MAX + 1;
39 static struct tc_action_ops act_ife_ops;
40 
41 static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = {
42 	[TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)},
43 	[TCA_IFE_DMAC] = { .len = ETH_ALEN},
44 	[TCA_IFE_SMAC] = { .len = ETH_ALEN},
45 	[TCA_IFE_TYPE] = { .type = NLA_U16},
46 };
47 
48 int ife_encode_meta_u16(u16 metaval, void *skbdata, struct tcf_meta_info *mi)
49 {
50 	u16 edata = 0;
51 
52 	if (mi->metaval)
53 		edata = *(u16 *)mi->metaval;
54 	else if (metaval)
55 		edata = metaval;
56 
57 	if (!edata) /* will not encode */
58 		return 0;
59 
60 	edata = htons(edata);
61 	return ife_tlv_meta_encode(skbdata, mi->metaid, 2, &edata);
62 }
63 EXPORT_SYMBOL_GPL(ife_encode_meta_u16);
64 
65 int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi)
66 {
67 	if (mi->metaval)
68 		return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval);
69 	else
70 		return nla_put(skb, mi->metaid, 0, NULL);
71 }
72 EXPORT_SYMBOL_GPL(ife_get_meta_u32);
73 
74 int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi)
75 {
76 	if (metaval || mi->metaval)
77 		return 8; /* T+L+V == 2+2+4 */
78 
79 	return 0;
80 }
81 EXPORT_SYMBOL_GPL(ife_check_meta_u32);
82 
83 int ife_check_meta_u16(u16 metaval, struct tcf_meta_info *mi)
84 {
85 	if (metaval || mi->metaval)
86 		return 8; /* T+L+(V) == 2+2+(2+2bytepad) */
87 
88 	return 0;
89 }
90 EXPORT_SYMBOL_GPL(ife_check_meta_u16);
91 
92 int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi)
93 {
94 	u32 edata = metaval;
95 
96 	if (mi->metaval)
97 		edata = *(u32 *)mi->metaval;
98 	else if (metaval)
99 		edata = metaval;
100 
101 	if (!edata) /* will not encode */
102 		return 0;
103 
104 	edata = htonl(edata);
105 	return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata);
106 }
107 EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
108 
109 int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi)
110 {
111 	if (mi->metaval)
112 		return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval);
113 	else
114 		return nla_put(skb, mi->metaid, 0, NULL);
115 }
116 EXPORT_SYMBOL_GPL(ife_get_meta_u16);
117 
118 int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
119 {
120 	mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
121 	if (!mi->metaval)
122 		return -ENOMEM;
123 
124 	return 0;
125 }
126 EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
127 
128 int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
129 {
130 	mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
131 	if (!mi->metaval)
132 		return -ENOMEM;
133 
134 	return 0;
135 }
136 EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
137 
138 void ife_release_meta_gen(struct tcf_meta_info *mi)
139 {
140 	kfree(mi->metaval);
141 }
142 EXPORT_SYMBOL_GPL(ife_release_meta_gen);
143 
144 int ife_validate_meta_u32(void *val, int len)
145 {
146 	if (len == sizeof(u32))
147 		return 0;
148 
149 	return -EINVAL;
150 }
151 EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
152 
153 int ife_validate_meta_u16(void *val, int len)
154 {
155 	/* length will not include padding */
156 	if (len == sizeof(u16))
157 		return 0;
158 
159 	return -EINVAL;
160 }
161 EXPORT_SYMBOL_GPL(ife_validate_meta_u16);
162 
163 static LIST_HEAD(ifeoplist);
164 static DEFINE_RWLOCK(ife_mod_lock);
165 
166 static struct tcf_meta_ops *find_ife_oplist(u16 metaid)
167 {
168 	struct tcf_meta_ops *o;
169 
170 	read_lock_bh(&ife_mod_lock);
171 	list_for_each_entry(o, &ifeoplist, list) {
172 		if (o->metaid == metaid) {
173 			if (!try_module_get(o->owner))
174 				o = NULL;
175 			read_unlock_bh(&ife_mod_lock);
176 			return o;
177 		}
178 	}
179 	read_unlock_bh(&ife_mod_lock);
180 
181 	return NULL;
182 }
183 
184 int register_ife_op(struct tcf_meta_ops *mops)
185 {
186 	struct tcf_meta_ops *m;
187 
188 	if (!mops->metaid || !mops->metatype || !mops->name ||
189 	    !mops->check_presence || !mops->encode || !mops->decode ||
190 	    !mops->get || !mops->alloc)
191 		return -EINVAL;
192 
193 	write_lock_bh(&ife_mod_lock);
194 
195 	list_for_each_entry(m, &ifeoplist, list) {
196 		if (m->metaid == mops->metaid ||
197 		    (strcmp(mops->name, m->name) == 0)) {
198 			write_unlock_bh(&ife_mod_lock);
199 			return -EEXIST;
200 		}
201 	}
202 
203 	if (!mops->release)
204 		mops->release = ife_release_meta_gen;
205 
206 	list_add_tail(&mops->list, &ifeoplist);
207 	write_unlock_bh(&ife_mod_lock);
208 	return 0;
209 }
210 EXPORT_SYMBOL_GPL(unregister_ife_op);
211 
212 int unregister_ife_op(struct tcf_meta_ops *mops)
213 {
214 	struct tcf_meta_ops *m;
215 	int err = -ENOENT;
216 
217 	write_lock_bh(&ife_mod_lock);
218 	list_for_each_entry(m, &ifeoplist, list) {
219 		if (m->metaid == mops->metaid) {
220 			list_del(&mops->list);
221 			err = 0;
222 			break;
223 		}
224 	}
225 	write_unlock_bh(&ife_mod_lock);
226 
227 	return err;
228 }
229 EXPORT_SYMBOL_GPL(register_ife_op);
230 
231 static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len)
232 {
233 	int ret = 0;
234 	/* XXX: unfortunately cant use nla_policy at this point
235 	* because a length of 0 is valid in the case of
236 	* "allow". "use" semantics do enforce for proper
237 	* length and i couldve use nla_policy but it makes it hard
238 	* to use it just for that..
239 	*/
240 	if (ops->validate)
241 		return ops->validate(val, len);
242 
243 	if (ops->metatype == NLA_U32)
244 		ret = ife_validate_meta_u32(val, len);
245 	else if (ops->metatype == NLA_U16)
246 		ret = ife_validate_meta_u16(val, len);
247 
248 	return ret;
249 }
250 
251 #ifdef CONFIG_MODULES
252 static const char *ife_meta_id2name(u32 metaid)
253 {
254 	switch (metaid) {
255 	case IFE_META_SKBMARK:
256 		return "skbmark";
257 	case IFE_META_PRIO:
258 		return "skbprio";
259 	case IFE_META_TCINDEX:
260 		return "tcindex";
261 	default:
262 		return "unknown";
263 	}
264 }
265 #endif
266 
267 /* called when adding new meta information
268  * under ife->tcf_lock for existing action
269 */
270 static int load_metaops_and_vet(struct tcf_ife_info *ife, u32 metaid,
271 				void *val, int len, bool exists,
272 				bool rtnl_held)
273 {
274 	struct tcf_meta_ops *ops = find_ife_oplist(metaid);
275 	int ret = 0;
276 
277 	if (!ops) {
278 		ret = -ENOENT;
279 #ifdef CONFIG_MODULES
280 		if (exists)
281 			spin_unlock_bh(&ife->tcf_lock);
282 		if (rtnl_held)
283 			rtnl_unlock();
284 		request_module("ife-meta-%s", ife_meta_id2name(metaid));
285 		if (rtnl_held)
286 			rtnl_lock();
287 		if (exists)
288 			spin_lock_bh(&ife->tcf_lock);
289 		ops = find_ife_oplist(metaid);
290 #endif
291 	}
292 
293 	if (ops) {
294 		ret = 0;
295 		if (len)
296 			ret = ife_validate_metatype(ops, val, len);
297 
298 		module_put(ops->owner);
299 	}
300 
301 	return ret;
302 }
303 
304 /* called when adding new meta information
305  * under ife->tcf_lock for existing action
306 */
307 static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval,
308 			int len, bool atomic)
309 {
310 	struct tcf_meta_info *mi = NULL;
311 	struct tcf_meta_ops *ops = find_ife_oplist(metaid);
312 	int ret = 0;
313 
314 	if (!ops)
315 		return -ENOENT;
316 
317 	mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL);
318 	if (!mi) {
319 		/*put back what find_ife_oplist took */
320 		module_put(ops->owner);
321 		return -ENOMEM;
322 	}
323 
324 	mi->metaid = metaid;
325 	mi->ops = ops;
326 	if (len > 0) {
327 		ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
328 		if (ret != 0) {
329 			kfree(mi);
330 			module_put(ops->owner);
331 			return ret;
332 		}
333 	}
334 
335 	list_add_tail(&mi->metalist, &ife->metalist);
336 
337 	return ret;
338 }
339 
340 static int use_all_metadata(struct tcf_ife_info *ife)
341 {
342 	struct tcf_meta_ops *o;
343 	int rc = 0;
344 	int installed = 0;
345 
346 	read_lock_bh(&ife_mod_lock);
347 	list_for_each_entry(o, &ifeoplist, list) {
348 		rc = add_metainfo(ife, o->metaid, NULL, 0, true);
349 		if (rc == 0)
350 			installed += 1;
351 	}
352 	read_unlock_bh(&ife_mod_lock);
353 
354 	if (installed)
355 		return 0;
356 	else
357 		return -EINVAL;
358 }
359 
360 static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife)
361 {
362 	struct tcf_meta_info *e;
363 	struct nlattr *nest;
364 	unsigned char *b = skb_tail_pointer(skb);
365 	int total_encoded = 0;
366 
367 	/*can only happen on decode */
368 	if (list_empty(&ife->metalist))
369 		return 0;
370 
371 	nest = nla_nest_start(skb, TCA_IFE_METALST);
372 	if (!nest)
373 		goto out_nlmsg_trim;
374 
375 	list_for_each_entry(e, &ife->metalist, metalist) {
376 		if (!e->ops->get(skb, e))
377 			total_encoded += 1;
378 	}
379 
380 	if (!total_encoded)
381 		goto out_nlmsg_trim;
382 
383 	nla_nest_end(skb, nest);
384 
385 	return 0;
386 
387 out_nlmsg_trim:
388 	nlmsg_trim(skb, b);
389 	return -1;
390 }
391 
392 /* under ife->tcf_lock */
393 static void _tcf_ife_cleanup(struct tc_action *a)
394 {
395 	struct tcf_ife_info *ife = to_ife(a);
396 	struct tcf_meta_info *e, *n;
397 
398 	list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
399 		module_put(e->ops->owner);
400 		list_del(&e->metalist);
401 		if (e->metaval) {
402 			if (e->ops->release)
403 				e->ops->release(e);
404 			else
405 				kfree(e->metaval);
406 		}
407 		kfree(e);
408 	}
409 }
410 
411 static void tcf_ife_cleanup(struct tc_action *a)
412 {
413 	struct tcf_ife_info *ife = to_ife(a);
414 	struct tcf_ife_params *p;
415 
416 	spin_lock_bh(&ife->tcf_lock);
417 	_tcf_ife_cleanup(a);
418 	spin_unlock_bh(&ife->tcf_lock);
419 
420 	p = rcu_dereference_protected(ife->params, 1);
421 	if (p)
422 		kfree_rcu(p, rcu);
423 }
424 
425 /* under ife->tcf_lock for existing action */
426 static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
427 			     bool exists, bool rtnl_held)
428 {
429 	int len = 0;
430 	int rc = 0;
431 	int i = 0;
432 	void *val;
433 
434 	for (i = 1; i < max_metacnt; i++) {
435 		if (tb[i]) {
436 			val = nla_data(tb[i]);
437 			len = nla_len(tb[i]);
438 
439 			rc = load_metaops_and_vet(ife, i, val, len, exists,
440 						  rtnl_held);
441 			if (rc != 0)
442 				return rc;
443 
444 			rc = add_metainfo(ife, i, val, len, exists);
445 			if (rc)
446 				return rc;
447 		}
448 	}
449 
450 	return rc;
451 }
452 
453 static int tcf_ife_init(struct net *net, struct nlattr *nla,
454 			struct nlattr *est, struct tc_action **a,
455 			int ovr, int bind, bool rtnl_held,
456 			struct netlink_ext_ack *extack)
457 {
458 	struct tc_action_net *tn = net_generic(net, ife_net_id);
459 	struct nlattr *tb[TCA_IFE_MAX + 1];
460 	struct nlattr *tb2[IFE_META_MAX + 1];
461 	struct tcf_ife_params *p;
462 	struct tcf_ife_info *ife;
463 	u16 ife_type = ETH_P_IFE;
464 	struct tc_ife *parm;
465 	u8 *daddr = NULL;
466 	u8 *saddr = NULL;
467 	bool exists = false;
468 	int ret = 0;
469 	int err;
470 
471 	err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy, NULL);
472 	if (err < 0)
473 		return err;
474 
475 	if (!tb[TCA_IFE_PARMS])
476 		return -EINVAL;
477 
478 	parm = nla_data(tb[TCA_IFE_PARMS]);
479 
480 	/* IFE_DECODE is 0 and indicates the opposite of IFE_ENCODE because
481 	 * they cannot run as the same time. Check on all other values which
482 	 * are not supported right now.
483 	 */
484 	if (parm->flags & ~IFE_ENCODE)
485 		return -EINVAL;
486 
487 	p = kzalloc(sizeof(*p), GFP_KERNEL);
488 	if (!p)
489 		return -ENOMEM;
490 
491 	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
492 	if (err < 0) {
493 		kfree(p);
494 		return err;
495 	}
496 	exists = err;
497 	if (exists && bind) {
498 		kfree(p);
499 		return 0;
500 	}
501 
502 	if (!exists) {
503 		ret = tcf_idr_create(tn, parm->index, est, a, &act_ife_ops,
504 				     bind, true);
505 		if (ret) {
506 			tcf_idr_cleanup(tn, parm->index);
507 			kfree(p);
508 			return ret;
509 		}
510 		ret = ACT_P_CREATED;
511 	} else if (!ovr) {
512 		tcf_idr_release(*a, bind);
513 		kfree(p);
514 		return -EEXIST;
515 	}
516 
517 	ife = to_ife(*a);
518 	p->flags = parm->flags;
519 
520 	if (parm->flags & IFE_ENCODE) {
521 		if (tb[TCA_IFE_TYPE])
522 			ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
523 		if (tb[TCA_IFE_DMAC])
524 			daddr = nla_data(tb[TCA_IFE_DMAC]);
525 		if (tb[TCA_IFE_SMAC])
526 			saddr = nla_data(tb[TCA_IFE_SMAC]);
527 	}
528 
529 	if (parm->flags & IFE_ENCODE) {
530 		if (daddr)
531 			ether_addr_copy(p->eth_dst, daddr);
532 		else
533 			eth_zero_addr(p->eth_dst);
534 
535 		if (saddr)
536 			ether_addr_copy(p->eth_src, saddr);
537 		else
538 			eth_zero_addr(p->eth_src);
539 
540 		p->eth_type = ife_type;
541 	}
542 
543 	if (exists)
544 		spin_lock_bh(&ife->tcf_lock);
545 
546 	if (ret == ACT_P_CREATED)
547 		INIT_LIST_HEAD(&ife->metalist);
548 
549 	if (tb[TCA_IFE_METALST]) {
550 		err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST],
551 				       NULL, NULL);
552 		if (err) {
553 metadata_parse_err:
554 			if (exists)
555 				spin_unlock_bh(&ife->tcf_lock);
556 			tcf_idr_release(*a, bind);
557 
558 			kfree(p);
559 			return err;
560 		}
561 
562 		err = populate_metalist(ife, tb2, exists, rtnl_held);
563 		if (err)
564 			goto metadata_parse_err;
565 
566 	} else {
567 		/* if no passed metadata allow list or passed allow-all
568 		 * then here we process by adding as many supported metadatum
569 		 * as we can. You better have at least one else we are
570 		 * going to bail out
571 		 */
572 		err = use_all_metadata(ife);
573 		if (err) {
574 			if (exists)
575 				spin_unlock_bh(&ife->tcf_lock);
576 			tcf_idr_release(*a, bind);
577 
578 			kfree(p);
579 			return err;
580 		}
581 	}
582 
583 	ife->tcf_action = parm->action;
584 	/* protected by tcf_lock when modifying existing action */
585 	rcu_swap_protected(ife->params, p, 1);
586 
587 	if (exists)
588 		spin_unlock_bh(&ife->tcf_lock);
589 	if (p)
590 		kfree_rcu(p, rcu);
591 
592 	if (ret == ACT_P_CREATED)
593 		tcf_idr_insert(tn, *a);
594 
595 	return ret;
596 }
597 
598 static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
599 			int ref)
600 {
601 	unsigned char *b = skb_tail_pointer(skb);
602 	struct tcf_ife_info *ife = to_ife(a);
603 	struct tcf_ife_params *p;
604 	struct tc_ife opt = {
605 		.index = ife->tcf_index,
606 		.refcnt = refcount_read(&ife->tcf_refcnt) - ref,
607 		.bindcnt = atomic_read(&ife->tcf_bindcnt) - bind,
608 	};
609 	struct tcf_t t;
610 
611 	spin_lock_bh(&ife->tcf_lock);
612 	opt.action = ife->tcf_action;
613 	p = rcu_dereference_protected(ife->params,
614 				      lockdep_is_held(&ife->tcf_lock));
615 	opt.flags = p->flags;
616 
617 	if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
618 		goto nla_put_failure;
619 
620 	tcf_tm_dump(&t, &ife->tcf_tm);
621 	if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
622 		goto nla_put_failure;
623 
624 	if (!is_zero_ether_addr(p->eth_dst)) {
625 		if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, p->eth_dst))
626 			goto nla_put_failure;
627 	}
628 
629 	if (!is_zero_ether_addr(p->eth_src)) {
630 		if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, p->eth_src))
631 			goto nla_put_failure;
632 	}
633 
634 	if (nla_put(skb, TCA_IFE_TYPE, 2, &p->eth_type))
635 		goto nla_put_failure;
636 
637 	if (dump_metalist(skb, ife)) {
638 		/*ignore failure to dump metalist */
639 		pr_info("Failed to dump metalist\n");
640 	}
641 
642 	spin_unlock_bh(&ife->tcf_lock);
643 	return skb->len;
644 
645 nla_put_failure:
646 	spin_unlock_bh(&ife->tcf_lock);
647 	nlmsg_trim(skb, b);
648 	return -1;
649 }
650 
651 static int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
652 			      u16 metaid, u16 mlen, void *mdata)
653 {
654 	struct tcf_meta_info *e;
655 
656 	/* XXX: use hash to speed up */
657 	list_for_each_entry(e, &ife->metalist, metalist) {
658 		if (metaid == e->metaid) {
659 			if (e->ops) {
660 				/* We check for decode presence already */
661 				return e->ops->decode(skb, mdata, mlen);
662 			}
663 		}
664 	}
665 
666 	return -ENOENT;
667 }
668 
669 static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
670 			  struct tcf_result *res)
671 {
672 	struct tcf_ife_info *ife = to_ife(a);
673 	int action = ife->tcf_action;
674 	u8 *ifehdr_end;
675 	u8 *tlv_data;
676 	u16 metalen;
677 
678 	bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb);
679 	tcf_lastuse_update(&ife->tcf_tm);
680 
681 	if (skb_at_tc_ingress(skb))
682 		skb_push(skb, skb->dev->hard_header_len);
683 
684 	tlv_data = ife_decode(skb, &metalen);
685 	if (unlikely(!tlv_data)) {
686 		qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
687 		return TC_ACT_SHOT;
688 	}
689 
690 	ifehdr_end = tlv_data + metalen;
691 	for (; tlv_data < ifehdr_end; tlv_data = ife_tlv_meta_next(tlv_data)) {
692 		u8 *curr_data;
693 		u16 mtype;
694 		u16 dlen;
695 
696 		curr_data = ife_tlv_meta_decode(tlv_data, ifehdr_end, &mtype,
697 						&dlen, NULL);
698 		if (!curr_data) {
699 			qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
700 			return TC_ACT_SHOT;
701 		}
702 
703 		if (find_decode_metaid(skb, ife, mtype, dlen, curr_data)) {
704 			/* abuse overlimits to count when we receive metadata
705 			 * but dont have an ops for it
706 			 */
707 			pr_info_ratelimited("Unknown metaid %d dlen %d\n",
708 					    mtype, dlen);
709 			qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats));
710 		}
711 	}
712 
713 	if (WARN_ON(tlv_data != ifehdr_end)) {
714 		qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
715 		return TC_ACT_SHOT;
716 	}
717 
718 	skb->protocol = eth_type_trans(skb, skb->dev);
719 	skb_reset_network_header(skb);
720 
721 	return action;
722 }
723 
724 /*XXX: check if we can do this at install time instead of current
725  * send data path
726 **/
727 static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
728 {
729 	struct tcf_meta_info *e, *n;
730 	int tot_run_sz = 0, run_sz = 0;
731 
732 	list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
733 		if (e->ops->check_presence) {
734 			run_sz = e->ops->check_presence(skb, e);
735 			tot_run_sz += run_sz;
736 		}
737 	}
738 
739 	return tot_run_sz;
740 }
741 
742 static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
743 			  struct tcf_result *res, struct tcf_ife_params *p)
744 {
745 	struct tcf_ife_info *ife = to_ife(a);
746 	int action = ife->tcf_action;
747 	struct ethhdr *oethh;	/* outer ether header */
748 	struct tcf_meta_info *e;
749 	/*
750 	   OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
751 	   where ORIGDATA = original ethernet header ...
752 	 */
753 	u16 metalen = ife_get_sz(skb, ife);
754 	int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
755 	unsigned int skboff = 0;
756 	int new_len = skb->len + hdrm;
757 	bool exceed_mtu = false;
758 	void *ife_meta;
759 	int err = 0;
760 
761 	if (!skb_at_tc_ingress(skb)) {
762 		if (new_len > skb->dev->mtu)
763 			exceed_mtu = true;
764 	}
765 
766 	bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb);
767 	tcf_lastuse_update(&ife->tcf_tm);
768 
769 	if (!metalen) {		/* no metadata to send */
770 		/* abuse overlimits to count when we allow packet
771 		 * with no metadata
772 		 */
773 		qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats));
774 		return action;
775 	}
776 	/* could be stupid policy setup or mtu config
777 	 * so lets be conservative.. */
778 	if ((action == TC_ACT_SHOT) || exceed_mtu) {
779 		qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
780 		return TC_ACT_SHOT;
781 	}
782 
783 	if (skb_at_tc_ingress(skb))
784 		skb_push(skb, skb->dev->hard_header_len);
785 
786 	ife_meta = ife_encode(skb, metalen);
787 
788 	spin_lock(&ife->tcf_lock);
789 
790 	/* XXX: we dont have a clever way of telling encode to
791 	 * not repeat some of the computations that are done by
792 	 * ops->presence_check...
793 	 */
794 	list_for_each_entry(e, &ife->metalist, metalist) {
795 		if (e->ops->encode) {
796 			err = e->ops->encode(skb, (void *)(ife_meta + skboff),
797 					     e);
798 		}
799 		if (err < 0) {
800 			/* too corrupt to keep around if overwritten */
801 			spin_unlock(&ife->tcf_lock);
802 			qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats));
803 			return TC_ACT_SHOT;
804 		}
805 		skboff += err;
806 	}
807 	spin_unlock(&ife->tcf_lock);
808 	oethh = (struct ethhdr *)skb->data;
809 
810 	if (!is_zero_ether_addr(p->eth_src))
811 		ether_addr_copy(oethh->h_source, p->eth_src);
812 	if (!is_zero_ether_addr(p->eth_dst))
813 		ether_addr_copy(oethh->h_dest, p->eth_dst);
814 	oethh->h_proto = htons(p->eth_type);
815 
816 	if (skb_at_tc_ingress(skb))
817 		skb_pull(skb, skb->dev->hard_header_len);
818 
819 	return action;
820 }
821 
822 static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
823 		       struct tcf_result *res)
824 {
825 	struct tcf_ife_info *ife = to_ife(a);
826 	struct tcf_ife_params *p;
827 	int ret;
828 
829 	p = rcu_dereference_bh(ife->params);
830 	if (p->flags & IFE_ENCODE) {
831 		ret = tcf_ife_encode(skb, a, res, p);
832 		return ret;
833 	}
834 
835 	return tcf_ife_decode(skb, a, res);
836 }
837 
838 static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
839 			  struct netlink_callback *cb, int type,
840 			  const struct tc_action_ops *ops,
841 			  struct netlink_ext_ack *extack)
842 {
843 	struct tc_action_net *tn = net_generic(net, ife_net_id);
844 
845 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
846 }
847 
848 static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index,
849 			  struct netlink_ext_ack *extack)
850 {
851 	struct tc_action_net *tn = net_generic(net, ife_net_id);
852 
853 	return tcf_idr_search(tn, a, index);
854 }
855 
856 static int tcf_ife_delete(struct net *net, u32 index)
857 {
858 	struct tc_action_net *tn = net_generic(net, ife_net_id);
859 
860 	return tcf_idr_delete_index(tn, index);
861 }
862 
863 static struct tc_action_ops act_ife_ops = {
864 	.kind = "ife",
865 	.type = TCA_ACT_IFE,
866 	.owner = THIS_MODULE,
867 	.act = tcf_ife_act,
868 	.dump = tcf_ife_dump,
869 	.cleanup = tcf_ife_cleanup,
870 	.init = tcf_ife_init,
871 	.walk = tcf_ife_walker,
872 	.lookup = tcf_ife_search,
873 	.delete = tcf_ife_delete,
874 	.size =	sizeof(struct tcf_ife_info),
875 };
876 
877 static __net_init int ife_init_net(struct net *net)
878 {
879 	struct tc_action_net *tn = net_generic(net, ife_net_id);
880 
881 	return tc_action_net_init(tn, &act_ife_ops);
882 }
883 
884 static void __net_exit ife_exit_net(struct list_head *net_list)
885 {
886 	tc_action_net_exit(net_list, ife_net_id);
887 }
888 
889 static struct pernet_operations ife_net_ops = {
890 	.init = ife_init_net,
891 	.exit_batch = ife_exit_net,
892 	.id   = &ife_net_id,
893 	.size = sizeof(struct tc_action_net),
894 };
895 
896 static int __init ife_init_module(void)
897 {
898 	return tcf_register_action(&act_ife_ops, &ife_net_ops);
899 }
900 
901 static void __exit ife_cleanup_module(void)
902 {
903 	tcf_unregister_action(&act_ife_ops, &ife_net_ops);
904 }
905 
906 module_init(ife_init_module);
907 module_exit(ife_cleanup_module);
908 
909 MODULE_AUTHOR("Jamal Hadi Salim(2015)");
910 MODULE_DESCRIPTION("Inter-FE LFB action");
911 MODULE_LICENSE("GPL");
912