GNU Linux-libre 4.9.309-gnu1
[releases.git] / net / sched / act_ife.c
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
36 #define IFE_TAB_MASK 15
37
38 static int ife_net_id;
39 static int max_metacnt = IFE_META_MAX + 1;
40 static struct tc_action_ops act_ife_ops;
41
42 static 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 */
51 int 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;
56         u32 htlv = attrtype << 16 | (dlen + NLA_HDRLEN);
57
58         *tlv = htonl(htlv);
59         memset(dptr, 0, totlen - NLA_HDRLEN);
60         memcpy(dptr, dval, dlen);
61
62         return totlen;
63 }
64 EXPORT_SYMBOL_GPL(ife_tlv_meta_encode);
65
66 int 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 }
81 EXPORT_SYMBOL_GPL(ife_encode_meta_u16);
82
83 int 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 }
90 EXPORT_SYMBOL_GPL(ife_get_meta_u32);
91
92 int 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 }
99 EXPORT_SYMBOL_GPL(ife_check_meta_u32);
100
101 int 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 }
108 EXPORT_SYMBOL_GPL(ife_check_meta_u16);
109
110 int 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 }
125 EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
126
127 int 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 }
134 EXPORT_SYMBOL_GPL(ife_get_meta_u16);
135
136 int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
137 {
138         mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
139         if (!mi->metaval)
140                 return -ENOMEM;
141
142         return 0;
143 }
144 EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
145
146 int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
147 {
148         mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
149         if (!mi->metaval)
150                 return -ENOMEM;
151
152         return 0;
153 }
154 EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
155
156 void ife_release_meta_gen(struct tcf_meta_info *mi)
157 {
158         kfree(mi->metaval);
159 }
160 EXPORT_SYMBOL_GPL(ife_release_meta_gen);
161
162 int ife_validate_meta_u32(void *val, int len)
163 {
164         if (len == sizeof(u32))
165                 return 0;
166
167         return -EINVAL;
168 }
169 EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
170
171 int ife_validate_meta_u16(void *val, int len)
172 {
173         /* length will not include padding */
174         if (len == sizeof(u16))
175                 return 0;
176
177         return -EINVAL;
178 }
179 EXPORT_SYMBOL_GPL(ife_validate_meta_u16);
180
181 static LIST_HEAD(ifeoplist);
182 static DEFINE_RWLOCK(ife_mod_lock);
183
184 static 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
202 int 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 }
228 EXPORT_SYMBOL_GPL(unregister_ife_op);
229
230 int 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 }
247 EXPORT_SYMBOL_GPL(register_ife_op);
248
249 static 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
270 */
271 static int load_metaops_and_vet(u32 metaid, void *val, int len)
272 {
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
279                 rtnl_unlock();
280                 request_module("ifemeta%u", metaid);
281                 rtnl_lock();
282                 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
298 */
299 static 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)
302 {
303         struct tcf_meta_info *mi = NULL;
304         int ret = 0;
305
306         mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL);
307         if (!mi)
308                 return -ENOMEM;
309
310         mi->metaid = metaid;
311         mi->ops = ops;
312         if (len > 0) {
313                 ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
314                 if (ret != 0) {
315                         kfree(mi);
316                         return ret;
317                 }
318         }
319
320         if (exists)
321                 spin_lock_bh(&ife->tcf_lock);
322         list_add_tail(&mi->metalist, &ife->metalist);
323         if (exists)
324                 spin_unlock_bh(&ife->tcf_lock);
325
326         return ret;
327 }
328
329 static 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
343 static 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
358 static int use_all_metadata(struct tcf_ife_info *ife, bool exists)
359 {
360         struct tcf_meta_ops *o;
361         int rc = 0;
362         int installed = 0;
363
364         read_lock(&ife_mod_lock);
365         list_for_each_entry(o, &ifeoplist, list) {
366                 rc = add_metainfo_and_get_ops(o, ife, o->metaid, exists);
367                 if (rc == 0)
368                         installed += 1;
369         }
370         read_unlock(&ife_mod_lock);
371
372         if (installed)
373                 return 0;
374         else
375                 return -EINVAL;
376 }
377
378 static 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
405 out_nlmsg_trim:
406         nlmsg_trim(skb, b);
407         return -1;
408 }
409
410 /* under ife->tcf_lock */
411 static void _tcf_ife_cleanup(struct tc_action *a, int bind)
412 {
413         struct tcf_ife_info *ife = to_ife(a);
414         struct tcf_meta_info *e, *n;
415
416         list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
417                 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                 }
424                 module_put(e->ops->owner);
425                 kfree(e);
426         }
427 }
428
429 static void tcf_ife_cleanup(struct tc_action *a, int bind)
430 {
431         struct tcf_ife_info *ife = to_ife(a);
432
433         spin_lock_bh(&ife->tcf_lock);
434         _tcf_ife_cleanup(a, bind);
435         spin_unlock_bh(&ife->tcf_lock);
436 }
437
438 static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
439                              bool exists)
440 {
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
451                         rc = load_metaops_and_vet(i, val, len);
452                         if (rc != 0)
453                                 return rc;
454
455                         rc = add_metainfo(ife, i, val, len, exists);
456                         if (rc)
457                                 return rc;
458                 }
459         }
460
461         return rc;
462 }
463
464 static int tcf_ife_init(struct net *net, struct nlattr *nla,
465                         struct nlattr *est, struct tc_action **a,
466                         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;
476         bool exists = false;
477         int ret = 0;
478         int err;
479
480         if (!nla)
481                 return -EINVAL;
482
483         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
492         exists = tcf_hash_check(tn, parm->index, a, bind);
493         if (exists && bind)
494                 return 0;
495
496         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]) {
501                         if (exists)
502                                 tcf_hash_release(*a, bind);
503                         pr_info("You MUST pass etherype for encoding\n");
504                         return -EINVAL;
505                 }
506         }
507
508         if (!exists) {
509                 ret = tcf_hash_create(tn, parm->index, est, a, &act_ife_ops,
510                                       bind, false);
511                 if (ret)
512                         return ret;
513                 ret = ACT_P_CREATED;
514         } else {
515                 tcf_hash_release(*a, bind);
516                 if (!ovr)
517                         return -EEXIST;
518         }
519
520         ife = to_ife(*a);
521         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
531         if (exists)
532                 spin_lock_bh(&ife->tcf_lock);
533         ife->tcf_action = parm->action;
534         if (exists)
535                 spin_unlock_bh(&ife->tcf_lock);
536
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) {
558 metadata_parse_err:
559                         if (exists)
560                                 tcf_hash_release(*a, bind);
561                         if (ret == ACT_P_CREATED)
562                                 _tcf_ife_cleanup(*a, bind);
563                         return err;
564                 }
565
566                 err = populate_metalist(ife, tb2, exists);
567                 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                  */
576                 err = use_all_metadata(ife, exists);
577                 if (err) {
578                         if (ret == ACT_P_CREATED)
579                                 _tcf_ife_cleanup(*a, bind);
580                         return err;
581                 }
582         }
583
584         if (ret == ACT_P_CREATED)
585                 tcf_hash_insert(tn, *a);
586
587         return ret;
588 }
589
590 static 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);
594         struct tcf_ife_info *ife = to_ife(a);
595         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
607         tcf_tm_dump(&t, &ife->tcf_tm);
608         if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
609                 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
631 nla_put_failure:
632         nlmsg_trim(skb, b);
633         return -1;
634 }
635
636 int 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
651         return -ENOENT;
652 }
653
654 struct ifeheadr {
655         __be16 metalen;
656         u8 tlv_data[];
657 };
658
659 struct meta_tlvhdr {
660         __be16 type;
661         __be16 len;
662 };
663
664 static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
665                           struct tcf_result *res)
666 {
667         struct tcf_ife_info *ife = to_ife(a);
668         int action = ife->tcf_action;
669         struct ifeheadr *ifehdr = (struct ifeheadr *)skb->data;
670         int ifehdrln = (int)ifehdr->metalen;
671         struct meta_tlvhdr *tlv = (struct meta_tlvhdr *)(ifehdr->tlv_data);
672
673         spin_lock(&ife->tcf_lock);
674         bstats_update(&ife->tcf_bstats, skb);
675         tcf_lastuse_update(&ife->tcf_tm);
676         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;
695                 u16 alen;
696
697                 mtype = ntohs(mtype);
698                 mlen = ntohs(mlen);
699                 alen = NLA_ALIGN(mlen);
700
701                 if (find_decode_metaid(skb, ife, mtype, (mlen - NLA_HDRLEN),
702                                        (void *)(tlvdata + NLA_HDRLEN))) {
703                         /* 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
711                 tlvdata += alen;
712                 ifehdrln -= alen;
713                 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 **/
723 static 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
738 static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
739                           struct tcf_result *res)
740 {
741         struct tcf_ife_info *ife = to_ife(a);
742         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);
765         tcf_lastuse_update(&ife->tcf_tm);
766
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
783         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
793         iethh = (struct ethhdr *)skb->data;
794         __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
841 static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
842                        struct tcf_result *res)
843 {
844         struct tcf_ife_info *ife = to_ife(a);
845
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);
855         tcf_lastuse_update(&ife->tcf_tm);
856         ife->tcf_qstats.drops++;
857         spin_unlock(&ife->tcf_lock);
858
859         return TC_ACT_SHOT;
860 }
861
862 static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
863                           struct netlink_callback *cb, int type,
864                           const struct tc_action_ops *ops)
865 {
866         struct tc_action_net *tn = net_generic(net, ife_net_id);
867
868         return tcf_generic_walker(tn, skb, cb, type, ops);
869 }
870
871 static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index)
872 {
873         struct tc_action_net *tn = net_generic(net, ife_net_id);
874
875         return tcf_hash_search(tn, a, index);
876 }
877
878 static 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,
888         .size = sizeof(struct tcf_ife_info),
889 };
890
891 static __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
898 static 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
905 static 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
912 static int __init ife_init_module(void)
913 {
914         return tcf_register_action(&act_ife_ops, &ife_net_ops);
915 }
916
917 static void __exit ife_cleanup_module(void)
918 {
919         tcf_unregister_action(&act_ife_ops, &ife_net_ops);
920 }
921
922 module_init(ife_init_module);
923 module_exit(ife_cleanup_module);
924
925 MODULE_AUTHOR("Jamal Hadi Salim(2015)");
926 MODULE_DESCRIPTION("Inter-FE LFB action");
927 MODULE_LICENSE("GPL");