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