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 struct net_device *dev = tp->q->dev_queue->dev; 151 struct tc_cls_bpf_offload cls_bpf = {}; 152 int err; 153 154 tc_cls_common_offload_init(&cls_bpf.common, tp); 155 cls_bpf.command = cmd; 156 cls_bpf.exts = &prog->exts; 157 cls_bpf.prog = prog->filter; 158 cls_bpf.name = prog->bpf_name; 159 cls_bpf.exts_integrated = prog->exts_integrated; 160 cls_bpf.gen_flags = prog->gen_flags; 161 162 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSBPF, &cls_bpf); 163 if (!err && (cmd == TC_CLSBPF_ADD || cmd == TC_CLSBPF_REPLACE)) 164 prog->gen_flags |= TCA_CLS_FLAGS_IN_HW; 165 166 return err; 167 } 168 169 static int cls_bpf_offload(struct tcf_proto *tp, struct cls_bpf_prog *prog, 170 struct cls_bpf_prog *oldprog) 171 { 172 struct net_device *dev = tp->q->dev_queue->dev; 173 struct cls_bpf_prog *obj = prog; 174 enum tc_clsbpf_command cmd; 175 bool skip_sw; 176 int ret; 177 178 skip_sw = tc_skip_sw(prog->gen_flags) || 179 (oldprog && tc_skip_sw(oldprog->gen_flags)); 180 181 if (oldprog && oldprog->offloaded) { 182 if (tc_should_offload(dev, prog->gen_flags)) { 183 cmd = TC_CLSBPF_REPLACE; 184 } else if (!tc_skip_sw(prog->gen_flags)) { 185 obj = oldprog; 186 cmd = TC_CLSBPF_DESTROY; 187 } else { 188 return -EINVAL; 189 } 190 } else { 191 if (!tc_should_offload(dev, prog->gen_flags)) 192 return skip_sw ? -EINVAL : 0; 193 cmd = TC_CLSBPF_ADD; 194 } 195 196 ret = cls_bpf_offload_cmd(tp, obj, cmd); 197 if (ret) 198 return skip_sw ? ret : 0; 199 200 obj->offloaded = true; 201 if (oldprog) 202 oldprog->offloaded = false; 203 204 return 0; 205 } 206 207 static void cls_bpf_stop_offload(struct tcf_proto *tp, 208 struct cls_bpf_prog *prog) 209 { 210 int err; 211 212 if (!prog->offloaded) 213 return; 214 215 err = cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_DESTROY); 216 if (err) { 217 pr_err("Stopping hardware offload failed: %d\n", err); 218 return; 219 } 220 221 prog->offloaded = false; 222 } 223 224 static void cls_bpf_offload_update_stats(struct tcf_proto *tp, 225 struct cls_bpf_prog *prog) 226 { 227 if (!prog->offloaded) 228 return; 229 230 cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_STATS); 231 } 232 233 static int cls_bpf_init(struct tcf_proto *tp) 234 { 235 struct cls_bpf_head *head; 236 237 head = kzalloc(sizeof(*head), GFP_KERNEL); 238 if (head == NULL) 239 return -ENOBUFS; 240 241 INIT_LIST_HEAD_RCU(&head->plist); 242 idr_init(&head->handle_idr); 243 rcu_assign_pointer(tp->root, head); 244 245 return 0; 246 } 247 248 static void __cls_bpf_delete_prog(struct cls_bpf_prog *prog) 249 { 250 tcf_exts_destroy(&prog->exts); 251 252 if (cls_bpf_is_ebpf(prog)) 253 bpf_prog_put(prog->filter); 254 else 255 bpf_prog_destroy(prog->filter); 256 257 kfree(prog->bpf_name); 258 kfree(prog->bpf_ops); 259 kfree(prog); 260 } 261 262 static void cls_bpf_delete_prog_rcu(struct rcu_head *rcu) 263 { 264 __cls_bpf_delete_prog(container_of(rcu, struct cls_bpf_prog, rcu)); 265 } 266 267 static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog) 268 { 269 struct cls_bpf_head *head = rtnl_dereference(tp->root); 270 271 idr_remove_ext(&head->handle_idr, prog->handle); 272 cls_bpf_stop_offload(tp, prog); 273 list_del_rcu(&prog->link); 274 tcf_unbind_filter(tp, &prog->res); 275 call_rcu(&prog->rcu, cls_bpf_delete_prog_rcu); 276 } 277 278 static int cls_bpf_delete(struct tcf_proto *tp, void *arg, bool *last) 279 { 280 struct cls_bpf_head *head = rtnl_dereference(tp->root); 281 282 __cls_bpf_delete(tp, arg); 283 *last = list_empty(&head->plist); 284 return 0; 285 } 286 287 static void cls_bpf_destroy(struct tcf_proto *tp) 288 { 289 struct cls_bpf_head *head = rtnl_dereference(tp->root); 290 struct cls_bpf_prog *prog, *tmp; 291 292 list_for_each_entry_safe(prog, tmp, &head->plist, link) 293 __cls_bpf_delete(tp, prog); 294 295 idr_destroy(&head->handle_idr); 296 kfree_rcu(head, rcu); 297 } 298 299 static void *cls_bpf_get(struct tcf_proto *tp, u32 handle) 300 { 301 struct cls_bpf_head *head = rtnl_dereference(tp->root); 302 struct cls_bpf_prog *prog; 303 304 list_for_each_entry(prog, &head->plist, link) { 305 if (prog->handle == handle) 306 return prog; 307 } 308 309 return NULL; 310 } 311 312 static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog) 313 { 314 struct sock_filter *bpf_ops; 315 struct sock_fprog_kern fprog_tmp; 316 struct bpf_prog *fp; 317 u16 bpf_size, bpf_num_ops; 318 int ret; 319 320 bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]); 321 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) 322 return -EINVAL; 323 324 bpf_size = bpf_num_ops * sizeof(*bpf_ops); 325 if (bpf_size != nla_len(tb[TCA_BPF_OPS])) 326 return -EINVAL; 327 328 bpf_ops = kzalloc(bpf_size, GFP_KERNEL); 329 if (bpf_ops == NULL) 330 return -ENOMEM; 331 332 memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); 333 334 fprog_tmp.len = bpf_num_ops; 335 fprog_tmp.filter = bpf_ops; 336 337 ret = bpf_prog_create(&fp, &fprog_tmp); 338 if (ret < 0) { 339 kfree(bpf_ops); 340 return ret; 341 } 342 343 prog->bpf_ops = bpf_ops; 344 prog->bpf_num_ops = bpf_num_ops; 345 prog->bpf_name = NULL; 346 prog->filter = fp; 347 348 return 0; 349 } 350 351 static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog, 352 const struct tcf_proto *tp) 353 { 354 struct bpf_prog *fp; 355 char *name = NULL; 356 u32 bpf_fd; 357 358 bpf_fd = nla_get_u32(tb[TCA_BPF_FD]); 359 360 fp = bpf_prog_get_type(bpf_fd, BPF_PROG_TYPE_SCHED_CLS); 361 if (IS_ERR(fp)) 362 return PTR_ERR(fp); 363 364 if (tb[TCA_BPF_NAME]) { 365 name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL); 366 if (!name) { 367 bpf_prog_put(fp); 368 return -ENOMEM; 369 } 370 } 371 372 prog->bpf_ops = NULL; 373 prog->bpf_name = name; 374 prog->filter = fp; 375 376 if (fp->dst_needed && !(tp->q->flags & TCQ_F_INGRESS)) 377 netif_keep_dst(qdisc_dev(tp->q)); 378 379 return 0; 380 } 381 382 static int cls_bpf_set_parms(struct net *net, struct tcf_proto *tp, 383 struct cls_bpf_prog *prog, unsigned long base, 384 struct nlattr **tb, struct nlattr *est, bool ovr) 385 { 386 bool is_bpf, is_ebpf, have_exts = false; 387 u32 gen_flags = 0; 388 int ret; 389 390 is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS]; 391 is_ebpf = tb[TCA_BPF_FD]; 392 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) 393 return -EINVAL; 394 395 ret = tcf_exts_validate(net, tp, tb, est, &prog->exts, ovr); 396 if (ret < 0) 397 return ret; 398 399 if (tb[TCA_BPF_FLAGS]) { 400 u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]); 401 402 if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT) 403 return -EINVAL; 404 405 have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT; 406 } 407 if (tb[TCA_BPF_FLAGS_GEN]) { 408 gen_flags = nla_get_u32(tb[TCA_BPF_FLAGS_GEN]); 409 if (gen_flags & ~CLS_BPF_SUPPORTED_GEN_FLAGS || 410 !tc_flags_valid(gen_flags)) 411 return -EINVAL; 412 } 413 414 prog->exts_integrated = have_exts; 415 prog->gen_flags = gen_flags; 416 417 ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) : 418 cls_bpf_prog_from_efd(tb, prog, tp); 419 if (ret < 0) 420 return ret; 421 422 if (tb[TCA_BPF_CLASSID]) { 423 prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]); 424 tcf_bind_filter(tp, &prog->res, base); 425 } 426 427 return 0; 428 } 429 430 static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, 431 struct tcf_proto *tp, unsigned long base, 432 u32 handle, struct nlattr **tca, 433 void **arg, bool ovr) 434 { 435 struct cls_bpf_head *head = rtnl_dereference(tp->root); 436 struct cls_bpf_prog *oldprog = *arg; 437 struct nlattr *tb[TCA_BPF_MAX + 1]; 438 struct cls_bpf_prog *prog; 439 unsigned long idr_index; 440 int ret; 441 442 if (tca[TCA_OPTIONS] == NULL) 443 return -EINVAL; 444 445 ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy, 446 NULL); 447 if (ret < 0) 448 return ret; 449 450 prog = kzalloc(sizeof(*prog), GFP_KERNEL); 451 if (!prog) 452 return -ENOBUFS; 453 454 ret = tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE); 455 if (ret < 0) 456 goto errout; 457 458 if (oldprog) { 459 if (handle && oldprog->handle != handle) { 460 ret = -EINVAL; 461 goto errout; 462 } 463 } 464 465 if (handle == 0) { 466 ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index, 467 1, 0x7FFFFFFF, GFP_KERNEL); 468 if (ret) 469 goto errout; 470 prog->handle = idr_index; 471 } else { 472 if (!oldprog) { 473 ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index, 474 handle, handle + 1, GFP_KERNEL); 475 if (ret) 476 goto errout; 477 } 478 prog->handle = handle; 479 } 480 481 ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr); 482 if (ret < 0) 483 goto errout_idr; 484 485 ret = cls_bpf_offload(tp, prog, oldprog); 486 if (ret) { 487 if (!oldprog) 488 idr_remove_ext(&head->handle_idr, prog->handle); 489 __cls_bpf_delete_prog(prog); 490 return ret; 491 } 492 493 if (!tc_in_hw(prog->gen_flags)) 494 prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW; 495 496 if (oldprog) { 497 idr_replace_ext(&head->handle_idr, prog, handle); 498 list_replace_rcu(&oldprog->link, &prog->link); 499 tcf_unbind_filter(tp, &oldprog->res); 500 call_rcu(&oldprog->rcu, cls_bpf_delete_prog_rcu); 501 } else { 502 list_add_rcu(&prog->link, &head->plist); 503 } 504 505 *arg = prog; 506 return 0; 507 508 errout_idr: 509 if (!oldprog) 510 idr_remove_ext(&head->handle_idr, prog->handle); 511 errout: 512 tcf_exts_destroy(&prog->exts); 513 kfree(prog); 514 return ret; 515 } 516 517 static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog, 518 struct sk_buff *skb) 519 { 520 struct nlattr *nla; 521 522 if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops)) 523 return -EMSGSIZE; 524 525 nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops * 526 sizeof(struct sock_filter)); 527 if (nla == NULL) 528 return -EMSGSIZE; 529 530 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); 531 532 return 0; 533 } 534 535 static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog, 536 struct sk_buff *skb) 537 { 538 struct nlattr *nla; 539 540 if (prog->bpf_name && 541 nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name)) 542 return -EMSGSIZE; 543 544 if (nla_put_u32(skb, TCA_BPF_ID, prog->filter->aux->id)) 545 return -EMSGSIZE; 546 547 nla = nla_reserve(skb, TCA_BPF_TAG, sizeof(prog->filter->tag)); 548 if (nla == NULL) 549 return -EMSGSIZE; 550 551 memcpy(nla_data(nla), prog->filter->tag, nla_len(nla)); 552 553 return 0; 554 } 555 556 static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, void *fh, 557 struct sk_buff *skb, struct tcmsg *tm) 558 { 559 struct cls_bpf_prog *prog = fh; 560 struct nlattr *nest; 561 u32 bpf_flags = 0; 562 int ret; 563 564 if (prog == NULL) 565 return skb->len; 566 567 tm->tcm_handle = prog->handle; 568 569 cls_bpf_offload_update_stats(tp, prog); 570 571 nest = nla_nest_start(skb, TCA_OPTIONS); 572 if (nest == NULL) 573 goto nla_put_failure; 574 575 if (prog->res.classid && 576 nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) 577 goto nla_put_failure; 578 579 if (cls_bpf_is_ebpf(prog)) 580 ret = cls_bpf_dump_ebpf_info(prog, skb); 581 else 582 ret = cls_bpf_dump_bpf_info(prog, skb); 583 if (ret) 584 goto nla_put_failure; 585 586 if (tcf_exts_dump(skb, &prog->exts) < 0) 587 goto nla_put_failure; 588 589 if (prog->exts_integrated) 590 bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT; 591 if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags)) 592 goto nla_put_failure; 593 if (prog->gen_flags && 594 nla_put_u32(skb, TCA_BPF_FLAGS_GEN, prog->gen_flags)) 595 goto nla_put_failure; 596 597 nla_nest_end(skb, nest); 598 599 if (tcf_exts_dump_stats(skb, &prog->exts) < 0) 600 goto nla_put_failure; 601 602 return skb->len; 603 604 nla_put_failure: 605 nla_nest_cancel(skb, nest); 606 return -1; 607 } 608 609 static void cls_bpf_bind_class(void *fh, u32 classid, unsigned long cl) 610 { 611 struct cls_bpf_prog *prog = fh; 612 613 if (prog && prog->res.classid == classid) 614 prog->res.class = cl; 615 } 616 617 static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg) 618 { 619 struct cls_bpf_head *head = rtnl_dereference(tp->root); 620 struct cls_bpf_prog *prog; 621 622 list_for_each_entry(prog, &head->plist, link) { 623 if (arg->count < arg->skip) 624 goto skip; 625 if (arg->fn(tp, prog, arg) < 0) { 626 arg->stop = 1; 627 break; 628 } 629 skip: 630 arg->count++; 631 } 632 } 633 634 static struct tcf_proto_ops cls_bpf_ops __read_mostly = { 635 .kind = "bpf", 636 .owner = THIS_MODULE, 637 .classify = cls_bpf_classify, 638 .init = cls_bpf_init, 639 .destroy = cls_bpf_destroy, 640 .get = cls_bpf_get, 641 .change = cls_bpf_change, 642 .delete = cls_bpf_delete, 643 .walk = cls_bpf_walk, 644 .dump = cls_bpf_dump, 645 .bind_class = cls_bpf_bind_class, 646 }; 647 648 static int __init cls_bpf_init_mod(void) 649 { 650 return register_tcf_proto_ops(&cls_bpf_ops); 651 } 652 653 static void __exit cls_bpf_exit_mod(void) 654 { 655 unregister_tcf_proto_ops(&cls_bpf_ops); 656 } 657 658 module_init(cls_bpf_init_mod); 659 module_exit(cls_bpf_exit_mod); 660