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 (ret == ACT_P_CREATED) 555 tcf_idr_release(*a, bind); 556 557 if (exists) 558 spin_unlock_bh(&ife->tcf_lock); 559 tcf_idr_release(*a, bind); 560 561 kfree(p); 562 return err; 563 } 564 565 err = populate_metalist(ife, tb2, exists, rtnl_held); 566 if (err) 567 goto metadata_parse_err; 568 569 } else { 570 /* if no passed metadata allow list or passed allow-all 571 * then here we process by adding as many supported metadatum 572 * as we can. You better have at least one else we are 573 * going to bail out 574 */ 575 err = use_all_metadata(ife); 576 if (err) { 577 if (ret == ACT_P_CREATED) 578 tcf_idr_release(*a, bind); 579 580 if (exists) 581 spin_unlock_bh(&ife->tcf_lock); 582 kfree(p); 583 return err; 584 } 585 } 586 587 ife->tcf_action = parm->action; 588 /* protected by tcf_lock when modifying existing action */ 589 rcu_swap_protected(ife->params, p, 1); 590 591 if (exists) 592 spin_unlock_bh(&ife->tcf_lock); 593 if (p) 594 kfree_rcu(p, rcu); 595 596 if (ret == ACT_P_CREATED) 597 tcf_idr_insert(tn, *a); 598 599 return ret; 600 } 601 602 static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind, 603 int ref) 604 { 605 unsigned char *b = skb_tail_pointer(skb); 606 struct tcf_ife_info *ife = to_ife(a); 607 struct tcf_ife_params *p; 608 struct tc_ife opt = { 609 .index = ife->tcf_index, 610 .refcnt = refcount_read(&ife->tcf_refcnt) - ref, 611 .bindcnt = atomic_read(&ife->tcf_bindcnt) - bind, 612 }; 613 struct tcf_t t; 614 615 spin_lock_bh(&ife->tcf_lock); 616 opt.action = ife->tcf_action; 617 p = rcu_dereference_protected(ife->params, 618 lockdep_is_held(&ife->tcf_lock)); 619 opt.flags = p->flags; 620 621 if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt)) 622 goto nla_put_failure; 623 624 tcf_tm_dump(&t, &ife->tcf_tm); 625 if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD)) 626 goto nla_put_failure; 627 628 if (!is_zero_ether_addr(p->eth_dst)) { 629 if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, p->eth_dst)) 630 goto nla_put_failure; 631 } 632 633 if (!is_zero_ether_addr(p->eth_src)) { 634 if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, p->eth_src)) 635 goto nla_put_failure; 636 } 637 638 if (nla_put(skb, TCA_IFE_TYPE, 2, &p->eth_type)) 639 goto nla_put_failure; 640 641 if (dump_metalist(skb, ife)) { 642 /*ignore failure to dump metalist */ 643 pr_info("Failed to dump metalist\n"); 644 } 645 646 spin_unlock_bh(&ife->tcf_lock); 647 return skb->len; 648 649 nla_put_failure: 650 spin_unlock_bh(&ife->tcf_lock); 651 nlmsg_trim(skb, b); 652 return -1; 653 } 654 655 static int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife, 656 u16 metaid, u16 mlen, void *mdata) 657 { 658 struct tcf_meta_info *e; 659 660 /* XXX: use hash to speed up */ 661 list_for_each_entry(e, &ife->metalist, metalist) { 662 if (metaid == e->metaid) { 663 if (e->ops) { 664 /* We check for decode presence already */ 665 return e->ops->decode(skb, mdata, mlen); 666 } 667 } 668 } 669 670 return -ENOENT; 671 } 672 673 static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a, 674 struct tcf_result *res) 675 { 676 struct tcf_ife_info *ife = to_ife(a); 677 int action = ife->tcf_action; 678 u8 *ifehdr_end; 679 u8 *tlv_data; 680 u16 metalen; 681 682 bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb); 683 tcf_lastuse_update(&ife->tcf_tm); 684 685 if (skb_at_tc_ingress(skb)) 686 skb_push(skb, skb->dev->hard_header_len); 687 688 tlv_data = ife_decode(skb, &metalen); 689 if (unlikely(!tlv_data)) { 690 qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); 691 return TC_ACT_SHOT; 692 } 693 694 ifehdr_end = tlv_data + metalen; 695 for (; tlv_data < ifehdr_end; tlv_data = ife_tlv_meta_next(tlv_data)) { 696 u8 *curr_data; 697 u16 mtype; 698 u16 dlen; 699 700 curr_data = ife_tlv_meta_decode(tlv_data, ifehdr_end, &mtype, 701 &dlen, NULL); 702 if (!curr_data) { 703 qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); 704 return TC_ACT_SHOT; 705 } 706 707 if (find_decode_metaid(skb, ife, mtype, dlen, curr_data)) { 708 /* abuse overlimits to count when we receive metadata 709 * but dont have an ops for it 710 */ 711 pr_info_ratelimited("Unknown metaid %d dlen %d\n", 712 mtype, dlen); 713 qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats)); 714 } 715 } 716 717 if (WARN_ON(tlv_data != ifehdr_end)) { 718 qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); 719 return TC_ACT_SHOT; 720 } 721 722 skb->protocol = eth_type_trans(skb, skb->dev); 723 skb_reset_network_header(skb); 724 725 return action; 726 } 727 728 /*XXX: check if we can do this at install time instead of current 729 * send data path 730 **/ 731 static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife) 732 { 733 struct tcf_meta_info *e, *n; 734 int tot_run_sz = 0, run_sz = 0; 735 736 list_for_each_entry_safe(e, n, &ife->metalist, metalist) { 737 if (e->ops->check_presence) { 738 run_sz = e->ops->check_presence(skb, e); 739 tot_run_sz += run_sz; 740 } 741 } 742 743 return tot_run_sz; 744 } 745 746 static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a, 747 struct tcf_result *res, struct tcf_ife_params *p) 748 { 749 struct tcf_ife_info *ife = to_ife(a); 750 int action = ife->tcf_action; 751 struct ethhdr *oethh; /* outer ether header */ 752 struct tcf_meta_info *e; 753 /* 754 OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA 755 where ORIGDATA = original ethernet header ... 756 */ 757 u16 metalen = ife_get_sz(skb, ife); 758 int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN; 759 unsigned int skboff = 0; 760 int new_len = skb->len + hdrm; 761 bool exceed_mtu = false; 762 void *ife_meta; 763 int err = 0; 764 765 if (!skb_at_tc_ingress(skb)) { 766 if (new_len > skb->dev->mtu) 767 exceed_mtu = true; 768 } 769 770 bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb); 771 tcf_lastuse_update(&ife->tcf_tm); 772 773 if (!metalen) { /* no metadata to send */ 774 /* abuse overlimits to count when we allow packet 775 * with no metadata 776 */ 777 qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats)); 778 return action; 779 } 780 /* could be stupid policy setup or mtu config 781 * so lets be conservative.. */ 782 if ((action == TC_ACT_SHOT) || exceed_mtu) { 783 qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); 784 return TC_ACT_SHOT; 785 } 786 787 if (skb_at_tc_ingress(skb)) 788 skb_push(skb, skb->dev->hard_header_len); 789 790 ife_meta = ife_encode(skb, metalen); 791 792 spin_lock(&ife->tcf_lock); 793 794 /* XXX: we dont have a clever way of telling encode to 795 * not repeat some of the computations that are done by 796 * ops->presence_check... 797 */ 798 list_for_each_entry(e, &ife->metalist, metalist) { 799 if (e->ops->encode) { 800 err = e->ops->encode(skb, (void *)(ife_meta + skboff), 801 e); 802 } 803 if (err < 0) { 804 /* too corrupt to keep around if overwritten */ 805 spin_unlock(&ife->tcf_lock); 806 qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); 807 return TC_ACT_SHOT; 808 } 809 skboff += err; 810 } 811 spin_unlock(&ife->tcf_lock); 812 oethh = (struct ethhdr *)skb->data; 813 814 if (!is_zero_ether_addr(p->eth_src)) 815 ether_addr_copy(oethh->h_source, p->eth_src); 816 if (!is_zero_ether_addr(p->eth_dst)) 817 ether_addr_copy(oethh->h_dest, p->eth_dst); 818 oethh->h_proto = htons(p->eth_type); 819 820 if (skb_at_tc_ingress(skb)) 821 skb_pull(skb, skb->dev->hard_header_len); 822 823 return action; 824 } 825 826 static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a, 827 struct tcf_result *res) 828 { 829 struct tcf_ife_info *ife = to_ife(a); 830 struct tcf_ife_params *p; 831 int ret; 832 833 p = rcu_dereference_bh(ife->params); 834 if (p->flags & IFE_ENCODE) { 835 ret = tcf_ife_encode(skb, a, res, p); 836 return ret; 837 } 838 839 return tcf_ife_decode(skb, a, res); 840 } 841 842 static int tcf_ife_walker(struct net *net, struct sk_buff *skb, 843 struct netlink_callback *cb, int type, 844 const struct tc_action_ops *ops, 845 struct netlink_ext_ack *extack) 846 { 847 struct tc_action_net *tn = net_generic(net, ife_net_id); 848 849 return tcf_generic_walker(tn, skb, cb, type, ops, extack); 850 } 851 852 static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index, 853 struct netlink_ext_ack *extack) 854 { 855 struct tc_action_net *tn = net_generic(net, ife_net_id); 856 857 return tcf_idr_search(tn, a, index); 858 } 859 860 static int tcf_ife_delete(struct net *net, u32 index) 861 { 862 struct tc_action_net *tn = net_generic(net, ife_net_id); 863 864 return tcf_idr_delete_index(tn, index); 865 } 866 867 static struct tc_action_ops act_ife_ops = { 868 .kind = "ife", 869 .type = TCA_ACT_IFE, 870 .owner = THIS_MODULE, 871 .act = tcf_ife_act, 872 .dump = tcf_ife_dump, 873 .cleanup = tcf_ife_cleanup, 874 .init = tcf_ife_init, 875 .walk = tcf_ife_walker, 876 .lookup = tcf_ife_search, 877 .delete = tcf_ife_delete, 878 .size = sizeof(struct tcf_ife_info), 879 }; 880 881 static __net_init int ife_init_net(struct net *net) 882 { 883 struct tc_action_net *tn = net_generic(net, ife_net_id); 884 885 return tc_action_net_init(tn, &act_ife_ops); 886 } 887 888 static void __net_exit ife_exit_net(struct list_head *net_list) 889 { 890 tc_action_net_exit(net_list, ife_net_id); 891 } 892 893 static struct pernet_operations ife_net_ops = { 894 .init = ife_init_net, 895 .exit_batch = ife_exit_net, 896 .id = &ife_net_id, 897 .size = sizeof(struct tc_action_net), 898 }; 899 900 static int __init ife_init_module(void) 901 { 902 return tcf_register_action(&act_ife_ops, &ife_net_ops); 903 } 904 905 static void __exit ife_cleanup_module(void) 906 { 907 tcf_unregister_action(&act_ife_ops, &ife_net_ops); 908 } 909 910 module_init(ife_init_module); 911 module_exit(ife_cleanup_module); 912 913 MODULE_AUTHOR("Jamal Hadi Salim(2015)"); 914 MODULE_DESCRIPTION("Inter-FE LFB action"); 915 MODULE_LICENSE("GPL"); 916