1 /* 2 * Berkeley Packet Filter based traffic classifier 3 * 4 * Might be used to classify traffic through flexible, user-defined and 5 * possibly JIT-ed BPF filters for traffic control as an alternative to 6 * ematches. 7 * 8 * (C) 2013 Daniel Borkmann <dborkman@redhat.com> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 */ 14 15 #include <linux/module.h> 16 #include <linux/types.h> 17 #include <linux/skbuff.h> 18 #include <linux/filter.h> 19 #include <linux/bpf.h> 20 #include <linux/idr.h> 21 22 #include <net/rtnetlink.h> 23 #include <net/pkt_cls.h> 24 #include <net/sock.h> 25 26 MODULE_LICENSE("GPL"); 27 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>"); 28 MODULE_DESCRIPTION("TC BPF based classifier"); 29 30 #define CLS_BPF_NAME_LEN 256 31 #define CLS_BPF_SUPPORTED_GEN_FLAGS \ 32 (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW) 33 34 struct cls_bpf_head { 35 struct list_head plist; 36 struct idr handle_idr; 37 struct rcu_head rcu; 38 }; 39 40 struct cls_bpf_prog { 41 struct bpf_prog *filter; 42 struct list_head link; 43 struct tcf_result res; 44 bool exts_integrated; 45 bool offloaded; 46 u32 gen_flags; 47 struct tcf_exts exts; 48 u32 handle; 49 u16 bpf_num_ops; 50 struct sock_filter *bpf_ops; 51 const char *bpf_name; 52 struct tcf_proto *tp; 53 struct rcu_head rcu; 54 }; 55 56 static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = { 57 [TCA_BPF_CLASSID] = { .type = NLA_U32 }, 58 [TCA_BPF_FLAGS] = { .type = NLA_U32 }, 59 [TCA_BPF_FLAGS_GEN] = { .type = NLA_U32 }, 60 [TCA_BPF_FD] = { .type = NLA_U32 }, 61 [TCA_BPF_NAME] = { .type = NLA_NUL_STRING, 62 .len = CLS_BPF_NAME_LEN }, 63 [TCA_BPF_OPS_LEN] = { .type = NLA_U16 }, 64 [TCA_BPF_OPS] = { .type = NLA_BINARY, 65 .len = sizeof(struct sock_filter) * BPF_MAXINSNS }, 66 }; 67 68 static int cls_bpf_exec_opcode(int code) 69 { 70 switch (code) { 71 case TC_ACT_OK: 72 case TC_ACT_SHOT: 73 case TC_ACT_STOLEN: 74 case TC_ACT_TRAP: 75 case TC_ACT_REDIRECT: 76 case TC_ACT_UNSPEC: 77 return code; 78 default: 79 return TC_ACT_UNSPEC; 80 } 81 } 82 83 static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp, 84 struct tcf_result *res) 85 { 86 struct cls_bpf_head *head = rcu_dereference_bh(tp->root); 87 bool at_ingress = skb_at_tc_ingress(skb); 88 struct cls_bpf_prog *prog; 89 int ret = -1; 90 91 /* Needed here for accessing maps. */ 92 rcu_read_lock(); 93 list_for_each_entry_rcu(prog, &head->plist, link) { 94 int filter_res; 95 96 qdisc_skb_cb(skb)->tc_classid = prog->res.classid; 97 98 if (tc_skip_sw(prog->gen_flags)) { 99 filter_res = prog->exts_integrated ? TC_ACT_UNSPEC : 0; 100 } else if (at_ingress) { 101 /* It is safe to push/pull even if skb_shared() */ 102 __skb_push(skb, skb->mac_len); 103 bpf_compute_data_pointers(skb); 104 filter_res = BPF_PROG_RUN(prog->filter, skb); 105 __skb_pull(skb, skb->mac_len); 106 } else { 107 bpf_compute_data_pointers(skb); 108 filter_res = BPF_PROG_RUN(prog->filter, skb); 109 } 110 111 if (prog->exts_integrated) { 112 res->class = 0; 113 res->classid = TC_H_MAJ(prog->res.classid) | 114 qdisc_skb_cb(skb)->tc_classid; 115 116 ret = cls_bpf_exec_opcode(filter_res); 117 if (ret == TC_ACT_UNSPEC) 118 continue; 119 break; 120 } 121 122 if (filter_res == 0) 123 continue; 124 if (filter_res != -1) { 125 res->class = 0; 126 res->classid = filter_res; 127 } else { 128 *res = prog->res; 129 } 130 131 ret = tcf_exts_exec(skb, &prog->exts, res); 132 if (ret < 0) 133 continue; 134 135 break; 136 } 137 rcu_read_unlock(); 138 139 return ret; 140 } 141 142 static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog) 143 { 144 return !prog->bpf_ops; 145 } 146 147 static int cls_bpf_offload_cmd(struct tcf_proto *tp, struct cls_bpf_prog *prog, 148 enum tc_clsbpf_command cmd) 149 { 150 bool addorrep = cmd == TC_CLSBPF_ADD || cmd == TC_CLSBPF_REPLACE; 151 struct tcf_block *block = tp->chain->block; 152 bool skip_sw = tc_skip_sw(prog->gen_flags); 153 struct tc_cls_bpf_offload cls_bpf = {}; 154 int err; 155 156 tc_cls_common_offload_init(&cls_bpf.common, tp); 157 cls_bpf.command = cmd; 158 cls_bpf.exts = &prog->exts; 159 cls_bpf.prog = prog->filter; 160 cls_bpf.name = prog->bpf_name; 161 cls_bpf.exts_integrated = prog->exts_integrated; 162 cls_bpf.gen_flags = prog->gen_flags; 163 164 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSBPF, &cls_bpf, skip_sw); 165 if (addorrep) { 166 if (err < 0) { 167 cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_DESTROY); 168 return err; 169 } else if (err > 0) { 170 prog->gen_flags |= TCA_CLS_FLAGS_IN_HW; 171 } 172 } 173 174 if (addorrep && skip_sw && !(prog->gen_flags && TCA_CLS_FLAGS_IN_HW)) 175 return -EINVAL; 176 177 return 0; 178 } 179 180 static int cls_bpf_offload(struct tcf_proto *tp, struct cls_bpf_prog *prog, 181 struct cls_bpf_prog *oldprog) 182 { 183 struct cls_bpf_prog *obj = prog; 184 enum tc_clsbpf_command cmd; 185 bool skip_sw; 186 int ret; 187 188 skip_sw = tc_skip_sw(prog->gen_flags) || 189 (oldprog && tc_skip_sw(oldprog->gen_flags)); 190 191 if (oldprog && oldprog->offloaded) { 192 if (!tc_skip_hw(prog->gen_flags)) { 193 cmd = TC_CLSBPF_REPLACE; 194 } else if (!tc_skip_sw(prog->gen_flags)) { 195 obj = oldprog; 196 cmd = TC_CLSBPF_DESTROY; 197 } else { 198 return -EINVAL; 199 } 200 } else { 201 if (tc_skip_hw(prog->gen_flags)) 202 return skip_sw ? -EINVAL : 0; 203 cmd = TC_CLSBPF_ADD; 204 } 205 206 ret = cls_bpf_offload_cmd(tp, obj, cmd); 207 if (ret) 208 return ret; 209 210 obj->offloaded = true; 211 if (oldprog) 212 oldprog->offloaded = false; 213 214 return 0; 215 } 216 217 static void cls_bpf_stop_offload(struct tcf_proto *tp, 218 struct cls_bpf_prog *prog) 219 { 220 int err; 221 222 if (!prog->offloaded) 223 return; 224 225 err = cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_DESTROY); 226 if (err) { 227 pr_err("Stopping hardware offload failed: %d\n", err); 228 return; 229 } 230 231 prog->offloaded = false; 232 } 233 234 static void cls_bpf_offload_update_stats(struct tcf_proto *tp, 235 struct cls_bpf_prog *prog) 236 { 237 if (!prog->offloaded) 238 return; 239 240 cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_STATS); 241 } 242 243 static int cls_bpf_init(struct tcf_proto *tp) 244 { 245 struct cls_bpf_head *head; 246 247 head = kzalloc(sizeof(*head), GFP_KERNEL); 248 if (head == NULL) 249 return -ENOBUFS; 250 251 INIT_LIST_HEAD_RCU(&head->plist); 252 idr_init(&head->handle_idr); 253 rcu_assign_pointer(tp->root, head); 254 255 return 0; 256 } 257 258 static void __cls_bpf_delete_prog(struct cls_bpf_prog *prog) 259 { 260 tcf_exts_destroy(&prog->exts); 261 262 if (cls_bpf_is_ebpf(prog)) 263 bpf_prog_put(prog->filter); 264 else 265 bpf_prog_destroy(prog->filter); 266 267 kfree(prog->bpf_name); 268 kfree(prog->bpf_ops); 269 kfree(prog); 270 } 271 272 static void cls_bpf_delete_prog_rcu(struct rcu_head *rcu) 273 { 274 __cls_bpf_delete_prog(container_of(rcu, struct cls_bpf_prog, rcu)); 275 } 276 277 static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog) 278 { 279 struct cls_bpf_head *head = rtnl_dereference(tp->root); 280 281 idr_remove_ext(&head->handle_idr, prog->handle); 282 cls_bpf_stop_offload(tp, prog); 283 list_del_rcu(&prog->link); 284 tcf_unbind_filter(tp, &prog->res); 285 call_rcu(&prog->rcu, cls_bpf_delete_prog_rcu); 286 } 287 288 static int cls_bpf_delete(struct tcf_proto *tp, void *arg, bool *last) 289 { 290 struct cls_bpf_head *head = rtnl_dereference(tp->root); 291 292 __cls_bpf_delete(tp, arg); 293 *last = list_empty(&head->plist); 294 return 0; 295 } 296 297 static void cls_bpf_destroy(struct tcf_proto *tp) 298 { 299 struct cls_bpf_head *head = rtnl_dereference(tp->root); 300 struct cls_bpf_prog *prog, *tmp; 301 302 list_for_each_entry_safe(prog, tmp, &head->plist, link) 303 __cls_bpf_delete(tp, prog); 304 305 idr_destroy(&head->handle_idr); 306 kfree_rcu(head, rcu); 307 } 308 309 static void *cls_bpf_get(struct tcf_proto *tp, u32 handle) 310 { 311 struct cls_bpf_head *head = rtnl_dereference(tp->root); 312 struct cls_bpf_prog *prog; 313 314 list_for_each_entry(prog, &head->plist, link) { 315 if (prog->handle == handle) 316 return prog; 317 } 318 319 return NULL; 320 } 321 322 static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog) 323 { 324 struct sock_filter *bpf_ops; 325 struct sock_fprog_kern fprog_tmp; 326 struct bpf_prog *fp; 327 u16 bpf_size, bpf_num_ops; 328 int ret; 329 330 bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]); 331 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) 332 return -EINVAL; 333 334 bpf_size = bpf_num_ops * sizeof(*bpf_ops); 335 if (bpf_size != nla_len(tb[TCA_BPF_OPS])) 336 return -EINVAL; 337 338 bpf_ops = kzalloc(bpf_size, GFP_KERNEL); 339 if (bpf_ops == NULL) 340 return -ENOMEM; 341 342 memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); 343 344 fprog_tmp.len = bpf_num_ops; 345 fprog_tmp.filter = bpf_ops; 346 347 ret = bpf_prog_create(&fp, &fprog_tmp); 348 if (ret < 0) { 349 kfree(bpf_ops); 350 return ret; 351 } 352 353 prog->bpf_ops = bpf_ops; 354 prog->bpf_num_ops = bpf_num_ops; 355 prog->bpf_name = NULL; 356 prog->filter = fp; 357 358 return 0; 359 } 360 361 static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog, 362 const struct tcf_proto *tp) 363 { 364 struct bpf_prog *fp; 365 char *name = NULL; 366 u32 bpf_fd; 367 368 bpf_fd = nla_get_u32(tb[TCA_BPF_FD]); 369 370 fp = bpf_prog_get_type(bpf_fd, BPF_PROG_TYPE_SCHED_CLS); 371 if (IS_ERR(fp)) 372 return PTR_ERR(fp); 373 374 if (tb[TCA_BPF_NAME]) { 375 name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL); 376 if (!name) { 377 bpf_prog_put(fp); 378 return -ENOMEM; 379 } 380 } 381 382 prog->bpf_ops = NULL; 383 prog->bpf_name = name; 384 prog->filter = fp; 385 386 if (fp->dst_needed && !(tp->q->flags & TCQ_F_INGRESS)) 387 netif_keep_dst(qdisc_dev(tp->q)); 388 389 return 0; 390 } 391 392 static int cls_bpf_set_parms(struct net *net, struct tcf_proto *tp, 393 struct cls_bpf_prog *prog, unsigned long base, 394 struct nlattr **tb, struct nlattr *est, bool ovr) 395 { 396 bool is_bpf, is_ebpf, have_exts = false; 397 u32 gen_flags = 0; 398 int ret; 399 400 is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS]; 401 is_ebpf = tb[TCA_BPF_FD]; 402 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) 403 return -EINVAL; 404 405 ret = tcf_exts_validate(net, tp, tb, est, &prog->exts, ovr); 406 if (ret < 0) 407 return ret; 408 409 if (tb[TCA_BPF_FLAGS]) { 410 u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]); 411 412 if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT) 413 return -EINVAL; 414 415 have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT; 416 } 417 if (tb[TCA_BPF_FLAGS_GEN]) { 418 gen_flags = nla_get_u32(tb[TCA_BPF_FLAGS_GEN]); 419 if (gen_flags & ~CLS_BPF_SUPPORTED_GEN_FLAGS || 420 !tc_flags_valid(gen_flags)) 421 return -EINVAL; 422 } 423 424 prog->exts_integrated = have_exts; 425 prog->gen_flags = gen_flags; 426 427 ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) : 428 cls_bpf_prog_from_efd(tb, prog, tp); 429 if (ret < 0) 430 return ret; 431 432 if (tb[TCA_BPF_CLASSID]) { 433 prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]); 434 tcf_bind_filter(tp, &prog->res, base); 435 } 436 437 return 0; 438 } 439 440 static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, 441 struct tcf_proto *tp, unsigned long base, 442 u32 handle, struct nlattr **tca, 443 void **arg, bool ovr) 444 { 445 struct cls_bpf_head *head = rtnl_dereference(tp->root); 446 struct cls_bpf_prog *oldprog = *arg; 447 struct nlattr *tb[TCA_BPF_MAX + 1]; 448 struct cls_bpf_prog *prog; 449 unsigned long idr_index; 450 int ret; 451 452 if (tca[TCA_OPTIONS] == NULL) 453 return -EINVAL; 454 455 ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy, 456 NULL); 457 if (ret < 0) 458 return ret; 459 460 prog = kzalloc(sizeof(*prog), GFP_KERNEL); 461 if (!prog) 462 return -ENOBUFS; 463 464 ret = tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE); 465 if (ret < 0) 466 goto errout; 467 468 if (oldprog) { 469 if (handle && oldprog->handle != handle) { 470 ret = -EINVAL; 471 goto errout; 472 } 473 } 474 475 if (handle == 0) { 476 ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index, 477 1, 0x7FFFFFFF, GFP_KERNEL); 478 if (ret) 479 goto errout; 480 prog->handle = idr_index; 481 } else { 482 if (!oldprog) { 483 ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index, 484 handle, handle + 1, GFP_KERNEL); 485 if (ret) 486 goto errout; 487 } 488 prog->handle = handle; 489 } 490 491 ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr); 492 if (ret < 0) 493 goto errout_idr; 494 495 ret = cls_bpf_offload(tp, prog, oldprog); 496 if (ret) { 497 if (!oldprog) 498 idr_remove_ext(&head->handle_idr, prog->handle); 499 __cls_bpf_delete_prog(prog); 500 return ret; 501 } 502 503 if (!tc_in_hw(prog->gen_flags)) 504 prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW; 505 506 if (oldprog) { 507 idr_replace_ext(&head->handle_idr, prog, handle); 508 list_replace_rcu(&oldprog->link, &prog->link); 509 tcf_unbind_filter(tp, &oldprog->res); 510 call_rcu(&oldprog->rcu, cls_bpf_delete_prog_rcu); 511 } else { 512 list_add_rcu(&prog->link, &head->plist); 513 } 514 515 *arg = prog; 516 return 0; 517 518 errout_idr: 519 if (!oldprog) 520 idr_remove_ext(&head->handle_idr, prog->handle); 521 errout: 522 tcf_exts_destroy(&prog->exts); 523 kfree(prog); 524 return ret; 525 } 526 527 static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog, 528 struct sk_buff *skb) 529 { 530 struct nlattr *nla; 531 532 if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops)) 533 return -EMSGSIZE; 534 535 nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops * 536 sizeof(struct sock_filter)); 537 if (nla == NULL) 538 return -EMSGSIZE; 539 540 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); 541 542 return 0; 543 } 544 545 static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog, 546 struct sk_buff *skb) 547 { 548 struct nlattr *nla; 549 550 if (prog->bpf_name && 551 nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name)) 552 return -EMSGSIZE; 553 554 if (nla_put_u32(skb, TCA_BPF_ID, prog->filter->aux->id)) 555 return -EMSGSIZE; 556 557 nla = nla_reserve(skb, TCA_BPF_TAG, sizeof(prog->filter->tag)); 558 if (nla == NULL) 559 return -EMSGSIZE; 560 561 memcpy(nla_data(nla), prog->filter->tag, nla_len(nla)); 562 563 return 0; 564 } 565 566 static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, void *fh, 567 struct sk_buff *skb, struct tcmsg *tm) 568 { 569 struct cls_bpf_prog *prog = fh; 570 struct nlattr *nest; 571 u32 bpf_flags = 0; 572 int ret; 573 574 if (prog == NULL) 575 return skb->len; 576 577 tm->tcm_handle = prog->handle; 578 579 cls_bpf_offload_update_stats(tp, prog); 580 581 nest = nla_nest_start(skb, TCA_OPTIONS); 582 if (nest == NULL) 583 goto nla_put_failure; 584 585 if (prog->res.classid && 586 nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) 587 goto nla_put_failure; 588 589 if (cls_bpf_is_ebpf(prog)) 590 ret = cls_bpf_dump_ebpf_info(prog, skb); 591 else 592 ret = cls_bpf_dump_bpf_info(prog, skb); 593 if (ret) 594 goto nla_put_failure; 595 596 if (tcf_exts_dump(skb, &prog->exts) < 0) 597 goto nla_put_failure; 598 599 if (prog->exts_integrated) 600 bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT; 601 if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags)) 602 goto nla_put_failure; 603 if (prog->gen_flags && 604 nla_put_u32(skb, TCA_BPF_FLAGS_GEN, prog->gen_flags)) 605 goto nla_put_failure; 606 607 nla_nest_end(skb, nest); 608 609 if (tcf_exts_dump_stats(skb, &prog->exts) < 0) 610 goto nla_put_failure; 611 612 return skb->len; 613 614 nla_put_failure: 615 nla_nest_cancel(skb, nest); 616 return -1; 617 } 618 619 static void cls_bpf_bind_class(void *fh, u32 classid, unsigned long cl) 620 { 621 struct cls_bpf_prog *prog = fh; 622 623 if (prog && prog->res.classid == classid) 624 prog->res.class = cl; 625 } 626 627 static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg) 628 { 629 struct cls_bpf_head *head = rtnl_dereference(tp->root); 630 struct cls_bpf_prog *prog; 631 632 list_for_each_entry(prog, &head->plist, link) { 633 if (arg->count < arg->skip) 634 goto skip; 635 if (arg->fn(tp, prog, arg) < 0) { 636 arg->stop = 1; 637 break; 638 } 639 skip: 640 arg->count++; 641 } 642 } 643 644 static struct tcf_proto_ops cls_bpf_ops __read_mostly = { 645 .kind = "bpf", 646 .owner = THIS_MODULE, 647 .classify = cls_bpf_classify, 648 .init = cls_bpf_init, 649 .destroy = cls_bpf_destroy, 650 .get = cls_bpf_get, 651 .change = cls_bpf_change, 652 .delete = cls_bpf_delete, 653 .walk = cls_bpf_walk, 654 .dump = cls_bpf_dump, 655 .bind_class = cls_bpf_bind_class, 656 }; 657 658 static int __init cls_bpf_init_mod(void) 659 { 660 return register_tcf_proto_ops(&cls_bpf_ops); 661 } 662 663 static void __exit cls_bpf_exit_mod(void) 664 { 665 unregister_tcf_proto_ops(&cls_bpf_ops); 666 } 667 668 module_init(cls_bpf_init_mod); 669 module_exit(cls_bpf_exit_mod); 670