xref: /linux/net/sched/act_ife.c (revision c411ed854584a71b0e86ac3019b60e4789d88086)
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 #define IFE_TAB_MASK 15
38 
39 static unsigned int ife_net_id;
40 static int max_metacnt = IFE_META_MAX + 1;
41 static struct tc_action_ops act_ife_ops;
42 
43 static 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 
50 int 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 }
65 EXPORT_SYMBOL_GPL(ife_encode_meta_u16);
66 
67 int 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 }
74 EXPORT_SYMBOL_GPL(ife_get_meta_u32);
75 
76 int 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 }
83 EXPORT_SYMBOL_GPL(ife_check_meta_u32);
84 
85 int 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 }
92 EXPORT_SYMBOL_GPL(ife_check_meta_u16);
93 
94 int 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 }
109 EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
110 
111 int 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 }
118 EXPORT_SYMBOL_GPL(ife_get_meta_u16);
119 
120 int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
121 {
122 	mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
123 	if (!mi->metaval)
124 		return -ENOMEM;
125 
126 	return 0;
127 }
128 EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
129 
130 int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
131 {
132 	mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
133 	if (!mi->metaval)
134 		return -ENOMEM;
135 
136 	return 0;
137 }
138 EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
139 
140 void ife_release_meta_gen(struct tcf_meta_info *mi)
141 {
142 	kfree(mi->metaval);
143 }
144 EXPORT_SYMBOL_GPL(ife_release_meta_gen);
145 
146 int ife_validate_meta_u32(void *val, int len)
147 {
148 	if (len == sizeof(u32))
149 		return 0;
150 
151 	return -EINVAL;
152 }
153 EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
154 
155 int ife_validate_meta_u16(void *val, int len)
156 {
157 	/* length will not include padding */
158 	if (len == sizeof(u16))
159 		return 0;
160 
161 	return -EINVAL;
162 }
163 EXPORT_SYMBOL_GPL(ife_validate_meta_u16);
164 
165 static LIST_HEAD(ifeoplist);
166 static DEFINE_RWLOCK(ife_mod_lock);
167 
168 static 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 
186 int 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 }
212 EXPORT_SYMBOL_GPL(unregister_ife_op);
213 
214 int 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 }
231 EXPORT_SYMBOL_GPL(register_ife_op);
232 
233 static 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
254  * under ife->tcf_lock for existing action
255 */
256 static int load_metaops_and_vet(struct tcf_ife_info *ife, u32 metaid,
257 				void *val, int len, bool exists)
258 {
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
265 		if (exists)
266 			spin_unlock_bh(&ife->tcf_lock);
267 		rtnl_unlock();
268 		request_module("ifemeta%u", metaid);
269 		rtnl_lock();
270 		if (exists)
271 			spin_lock_bh(&ife->tcf_lock);
272 		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
288  * under ife->tcf_lock for existing action
289 */
290 static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval,
291 			int len, bool atomic)
292 {
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 
300 	mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL);
301 	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) {
310 		ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
311 		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 
323 static 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 
329 	read_lock(&ife_mod_lock);
330 	list_for_each_entry(o, &ifeoplist, list) {
331 		rc = add_metainfo(ife, o->metaid, NULL, 0, true);
332 		if (rc == 0)
333 			installed += 1;
334 	}
335 	read_unlock(&ife_mod_lock);
336 
337 	if (installed)
338 		return 0;
339 	else
340 		return -EINVAL;
341 }
342 
343 static 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 
370 out_nlmsg_trim:
371 	nlmsg_trim(skb, b);
372 	return -1;
373 }
374 
375 /* under ife->tcf_lock */
376 static void _tcf_ife_cleanup(struct tc_action *a, int bind)
377 {
378 	struct tcf_ife_info *ife = to_ife(a);
379 	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 
394 static void tcf_ife_cleanup(struct tc_action *a, int bind)
395 {
396 	struct tcf_ife_info *ife = to_ife(a);
397 
398 	spin_lock_bh(&ife->tcf_lock);
399 	_tcf_ife_cleanup(a, bind);
400 	spin_unlock_bh(&ife->tcf_lock);
401 }
402 
403 /* under ife->tcf_lock for existing action */
404 static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
405 			     bool exists)
406 {
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 
417 			rc = load_metaops_and_vet(ife, i, val, len, exists);
418 			if (rc != 0)
419 				return rc;
420 
421 			rc = add_metainfo(ife, i, val, len, exists);
422 			if (rc)
423 				return rc;
424 		}
425 	}
426 
427 	return rc;
428 }
429 
430 static int tcf_ife_init(struct net *net, struct nlattr *nla,
431 			struct nlattr *est, struct tc_action **a,
432 			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 	u16 ife_type = ETH_P_IFE;
439 	struct tc_ife *parm;
440 	u8 *daddr = NULL;
441 	u8 *saddr = NULL;
442 	bool exists = false;
443 	int ret = 0;
444 	int err;
445 
446 	err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy, NULL);
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 
455 	exists = tcf_hash_check(tn, parm->index, a, bind);
456 	if (exists && bind)
457 		return 0;
458 
459 	if (!exists) {
460 		ret = tcf_hash_create(tn, parm->index, est, a, &act_ife_ops,
461 				      bind, false);
462 		if (ret)
463 			return ret;
464 		ret = ACT_P_CREATED;
465 	} else {
466 		tcf_hash_release(*a, bind);
467 		if (!ovr)
468 			return -EEXIST;
469 	}
470 
471 	ife = to_ife(*a);
472 	ife->flags = parm->flags;
473 
474 	if (parm->flags & IFE_ENCODE) {
475 		if (tb[TCA_IFE_TYPE])
476 			ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
477 		if (tb[TCA_IFE_DMAC])
478 			daddr = nla_data(tb[TCA_IFE_DMAC]);
479 		if (tb[TCA_IFE_SMAC])
480 			saddr = nla_data(tb[TCA_IFE_SMAC]);
481 	}
482 
483 	if (exists)
484 		spin_lock_bh(&ife->tcf_lock);
485 	ife->tcf_action = parm->action;
486 
487 	if (parm->flags & IFE_ENCODE) {
488 		if (daddr)
489 			ether_addr_copy(ife->eth_dst, daddr);
490 		else
491 			eth_zero_addr(ife->eth_dst);
492 
493 		if (saddr)
494 			ether_addr_copy(ife->eth_src, saddr);
495 		else
496 			eth_zero_addr(ife->eth_src);
497 
498 		ife->eth_type = ife_type;
499 	}
500 
501 	if (ret == ACT_P_CREATED)
502 		INIT_LIST_HEAD(&ife->metalist);
503 
504 	if (tb[TCA_IFE_METALST]) {
505 		err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST],
506 				       NULL, NULL);
507 		if (err) {
508 metadata_parse_err:
509 			if (exists)
510 				tcf_hash_release(*a, bind);
511 			if (ret == ACT_P_CREATED)
512 				_tcf_ife_cleanup(*a, bind);
513 
514 			if (exists)
515 				spin_unlock_bh(&ife->tcf_lock);
516 			return err;
517 		}
518 
519 		err = populate_metalist(ife, tb2, exists);
520 		if (err)
521 			goto metadata_parse_err;
522 
523 	} else {
524 		/* if no passed metadata allow list or passed allow-all
525 		 * then here we process by adding as many supported metadatum
526 		 * as we can. You better have at least one else we are
527 		 * going to bail out
528 		 */
529 		err = use_all_metadata(ife);
530 		if (err) {
531 			if (ret == ACT_P_CREATED)
532 				_tcf_ife_cleanup(*a, bind);
533 
534 			if (exists)
535 				spin_unlock_bh(&ife->tcf_lock);
536 			return err;
537 		}
538 	}
539 
540 	if (exists)
541 		spin_unlock_bh(&ife->tcf_lock);
542 
543 	if (ret == ACT_P_CREATED)
544 		tcf_hash_insert(tn, *a);
545 
546 	return ret;
547 }
548 
549 static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
550 			int ref)
551 {
552 	unsigned char *b = skb_tail_pointer(skb);
553 	struct tcf_ife_info *ife = to_ife(a);
554 	struct tc_ife opt = {
555 		.index = ife->tcf_index,
556 		.refcnt = ife->tcf_refcnt - ref,
557 		.bindcnt = ife->tcf_bindcnt - bind,
558 		.action = ife->tcf_action,
559 		.flags = ife->flags,
560 	};
561 	struct tcf_t t;
562 
563 	if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
564 		goto nla_put_failure;
565 
566 	tcf_tm_dump(&t, &ife->tcf_tm);
567 	if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
568 		goto nla_put_failure;
569 
570 	if (!is_zero_ether_addr(ife->eth_dst)) {
571 		if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst))
572 			goto nla_put_failure;
573 	}
574 
575 	if (!is_zero_ether_addr(ife->eth_src)) {
576 		if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src))
577 			goto nla_put_failure;
578 	}
579 
580 	if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type))
581 		goto nla_put_failure;
582 
583 	if (dump_metalist(skb, ife)) {
584 		/*ignore failure to dump metalist */
585 		pr_info("Failed to dump metalist\n");
586 	}
587 
588 	return skb->len;
589 
590 nla_put_failure:
591 	nlmsg_trim(skb, b);
592 	return -1;
593 }
594 
595 static int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
596 			      u16 metaid, u16 mlen, void *mdata)
597 {
598 	struct tcf_meta_info *e;
599 
600 	/* XXX: use hash to speed up */
601 	list_for_each_entry(e, &ife->metalist, metalist) {
602 		if (metaid == e->metaid) {
603 			if (e->ops) {
604 				/* We check for decode presence already */
605 				return e->ops->decode(skb, mdata, mlen);
606 			}
607 		}
608 	}
609 
610 	return 0;
611 }
612 
613 static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
614 			  struct tcf_result *res)
615 {
616 	struct tcf_ife_info *ife = to_ife(a);
617 	int action = ife->tcf_action;
618 	u8 *ifehdr_end;
619 	u8 *tlv_data;
620 	u16 metalen;
621 
622 	spin_lock(&ife->tcf_lock);
623 	bstats_update(&ife->tcf_bstats, skb);
624 	tcf_lastuse_update(&ife->tcf_tm);
625 	spin_unlock(&ife->tcf_lock);
626 
627 	if (skb_at_tc_ingress(skb))
628 		skb_push(skb, skb->dev->hard_header_len);
629 
630 	tlv_data = ife_decode(skb, &metalen);
631 	if (unlikely(!tlv_data)) {
632 		spin_lock(&ife->tcf_lock);
633 		ife->tcf_qstats.drops++;
634 		spin_unlock(&ife->tcf_lock);
635 		return TC_ACT_SHOT;
636 	}
637 
638 	ifehdr_end = tlv_data + metalen;
639 	for (; tlv_data < ifehdr_end; tlv_data = ife_tlv_meta_next(tlv_data)) {
640 		u8 *curr_data;
641 		u16 mtype;
642 		u16 dlen;
643 
644 		curr_data = ife_tlv_meta_decode(tlv_data, &mtype, &dlen, NULL);
645 
646 		if (find_decode_metaid(skb, ife, mtype, dlen, curr_data)) {
647 			/* abuse overlimits to count when we receive metadata
648 			 * but dont have an ops for it
649 			 */
650 			pr_info_ratelimited("Unknown metaid %d dlen %d\n",
651 					    mtype, dlen);
652 			ife->tcf_qstats.overlimits++;
653 		}
654 	}
655 
656 	if (WARN_ON(tlv_data != ifehdr_end)) {
657 		spin_lock(&ife->tcf_lock);
658 		ife->tcf_qstats.drops++;
659 		spin_unlock(&ife->tcf_lock);
660 		return TC_ACT_SHOT;
661 	}
662 
663 	skb->protocol = eth_type_trans(skb, skb->dev);
664 	skb_reset_network_header(skb);
665 
666 	return action;
667 }
668 
669 /*XXX: check if we can do this at install time instead of current
670  * send data path
671 **/
672 static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
673 {
674 	struct tcf_meta_info *e, *n;
675 	int tot_run_sz = 0, run_sz = 0;
676 
677 	list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
678 		if (e->ops->check_presence) {
679 			run_sz = e->ops->check_presence(skb, e);
680 			tot_run_sz += run_sz;
681 		}
682 	}
683 
684 	return tot_run_sz;
685 }
686 
687 static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
688 			  struct tcf_result *res)
689 {
690 	struct tcf_ife_info *ife = to_ife(a);
691 	int action = ife->tcf_action;
692 	struct ethhdr *oethh;	/* outer ether header */
693 	struct tcf_meta_info *e;
694 	/*
695 	   OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
696 	   where ORIGDATA = original ethernet header ...
697 	 */
698 	u16 metalen = ife_get_sz(skb, ife);
699 	int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
700 	unsigned int skboff = 0;
701 	int new_len = skb->len + hdrm;
702 	bool exceed_mtu = false;
703 	void *ife_meta;
704 	int err = 0;
705 
706 	if (!skb_at_tc_ingress(skb)) {
707 		if (new_len > skb->dev->mtu)
708 			exceed_mtu = true;
709 	}
710 
711 	spin_lock(&ife->tcf_lock);
712 	bstats_update(&ife->tcf_bstats, skb);
713 	tcf_lastuse_update(&ife->tcf_tm);
714 
715 	if (!metalen) {		/* no metadata to send */
716 		/* abuse overlimits to count when we allow packet
717 		 * with no metadata
718 		 */
719 		ife->tcf_qstats.overlimits++;
720 		spin_unlock(&ife->tcf_lock);
721 		return action;
722 	}
723 	/* could be stupid policy setup or mtu config
724 	 * so lets be conservative.. */
725 	if ((action == TC_ACT_SHOT) || exceed_mtu) {
726 		ife->tcf_qstats.drops++;
727 		spin_unlock(&ife->tcf_lock);
728 		return TC_ACT_SHOT;
729 	}
730 
731 	if (skb_at_tc_ingress(skb))
732 		skb_push(skb, skb->dev->hard_header_len);
733 
734 	ife_meta = ife_encode(skb, metalen);
735 
736 	/* XXX: we dont have a clever way of telling encode to
737 	 * not repeat some of the computations that are done by
738 	 * ops->presence_check...
739 	 */
740 	list_for_each_entry(e, &ife->metalist, metalist) {
741 		if (e->ops->encode) {
742 			err = e->ops->encode(skb, (void *)(ife_meta + skboff),
743 					     e);
744 		}
745 		if (err < 0) {
746 			/* too corrupt to keep around if overwritten */
747 			ife->tcf_qstats.drops++;
748 			spin_unlock(&ife->tcf_lock);
749 			return TC_ACT_SHOT;
750 		}
751 		skboff += err;
752 	}
753 	oethh = (struct ethhdr *)skb->data;
754 
755 	if (!is_zero_ether_addr(ife->eth_src))
756 		ether_addr_copy(oethh->h_source, ife->eth_src);
757 	if (!is_zero_ether_addr(ife->eth_dst))
758 		ether_addr_copy(oethh->h_dest, ife->eth_dst);
759 	oethh->h_proto = htons(ife->eth_type);
760 
761 	if (skb_at_tc_ingress(skb))
762 		skb_pull(skb, skb->dev->hard_header_len);
763 
764 	spin_unlock(&ife->tcf_lock);
765 
766 	return action;
767 }
768 
769 static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
770 		       struct tcf_result *res)
771 {
772 	struct tcf_ife_info *ife = to_ife(a);
773 
774 	if (ife->flags & IFE_ENCODE)
775 		return tcf_ife_encode(skb, a, res);
776 
777 	if (!(ife->flags & IFE_ENCODE))
778 		return tcf_ife_decode(skb, a, res);
779 
780 	pr_info_ratelimited("unknown failure(policy neither de/encode\n");
781 	spin_lock(&ife->tcf_lock);
782 	bstats_update(&ife->tcf_bstats, skb);
783 	tcf_lastuse_update(&ife->tcf_tm);
784 	ife->tcf_qstats.drops++;
785 	spin_unlock(&ife->tcf_lock);
786 
787 	return TC_ACT_SHOT;
788 }
789 
790 static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
791 			  struct netlink_callback *cb, int type,
792 			  const struct tc_action_ops *ops)
793 {
794 	struct tc_action_net *tn = net_generic(net, ife_net_id);
795 
796 	return tcf_generic_walker(tn, skb, cb, type, ops);
797 }
798 
799 static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index)
800 {
801 	struct tc_action_net *tn = net_generic(net, ife_net_id);
802 
803 	return tcf_hash_search(tn, a, index);
804 }
805 
806 static struct tc_action_ops act_ife_ops = {
807 	.kind = "ife",
808 	.type = TCA_ACT_IFE,
809 	.owner = THIS_MODULE,
810 	.act = tcf_ife_act,
811 	.dump = tcf_ife_dump,
812 	.cleanup = tcf_ife_cleanup,
813 	.init = tcf_ife_init,
814 	.walk = tcf_ife_walker,
815 	.lookup = tcf_ife_search,
816 	.size =	sizeof(struct tcf_ife_info),
817 };
818 
819 static __net_init int ife_init_net(struct net *net)
820 {
821 	struct tc_action_net *tn = net_generic(net, ife_net_id);
822 
823 	return tc_action_net_init(tn, &act_ife_ops, IFE_TAB_MASK);
824 }
825 
826 static void __net_exit ife_exit_net(struct net *net)
827 {
828 	struct tc_action_net *tn = net_generic(net, ife_net_id);
829 
830 	tc_action_net_exit(tn);
831 }
832 
833 static struct pernet_operations ife_net_ops = {
834 	.init = ife_init_net,
835 	.exit = ife_exit_net,
836 	.id   = &ife_net_id,
837 	.size = sizeof(struct tc_action_net),
838 };
839 
840 static int __init ife_init_module(void)
841 {
842 	return tcf_register_action(&act_ife_ops, &ife_net_ops);
843 }
844 
845 static void __exit ife_cleanup_module(void)
846 {
847 	tcf_unregister_action(&act_ife_ops, &ife_net_ops);
848 }
849 
850 module_init(ife_init_module);
851 module_exit(ife_cleanup_module);
852 
853 MODULE_AUTHOR("Jamal Hadi Salim(2015)");
854 MODULE_DESCRIPTION("Inter-FE LFB action");
855 MODULE_LICENSE("GPL");
856