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