0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/types.h>
0014 #include <linux/skbuff.h>
0015 #include <linux/filter.h>
0016 #include <linux/bpf.h>
0017 #include <linux/idr.h>
0018
0019 #include <net/rtnetlink.h>
0020 #include <net/pkt_cls.h>
0021 #include <net/sock.h>
0022
0023 MODULE_LICENSE("GPL");
0024 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
0025 MODULE_DESCRIPTION("TC BPF based classifier");
0026
0027 #define CLS_BPF_NAME_LEN 256
0028 #define CLS_BPF_SUPPORTED_GEN_FLAGS \
0029 (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)
0030
0031 struct cls_bpf_head {
0032 struct list_head plist;
0033 struct idr handle_idr;
0034 struct rcu_head rcu;
0035 };
0036
0037 struct cls_bpf_prog {
0038 struct bpf_prog *filter;
0039 struct list_head link;
0040 struct tcf_result res;
0041 bool exts_integrated;
0042 u32 gen_flags;
0043 unsigned int in_hw_count;
0044 struct tcf_exts exts;
0045 u32 handle;
0046 u16 bpf_num_ops;
0047 struct sock_filter *bpf_ops;
0048 const char *bpf_name;
0049 struct tcf_proto *tp;
0050 struct rcu_work rwork;
0051 };
0052
0053 static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
0054 [TCA_BPF_CLASSID] = { .type = NLA_U32 },
0055 [TCA_BPF_FLAGS] = { .type = NLA_U32 },
0056 [TCA_BPF_FLAGS_GEN] = { .type = NLA_U32 },
0057 [TCA_BPF_FD] = { .type = NLA_U32 },
0058 [TCA_BPF_NAME] = { .type = NLA_NUL_STRING,
0059 .len = CLS_BPF_NAME_LEN },
0060 [TCA_BPF_OPS_LEN] = { .type = NLA_U16 },
0061 [TCA_BPF_OPS] = { .type = NLA_BINARY,
0062 .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
0063 };
0064
0065 static int cls_bpf_exec_opcode(int code)
0066 {
0067 switch (code) {
0068 case TC_ACT_OK:
0069 case TC_ACT_SHOT:
0070 case TC_ACT_STOLEN:
0071 case TC_ACT_TRAP:
0072 case TC_ACT_REDIRECT:
0073 case TC_ACT_UNSPEC:
0074 return code;
0075 default:
0076 return TC_ACT_UNSPEC;
0077 }
0078 }
0079
0080 static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
0081 struct tcf_result *res)
0082 {
0083 struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
0084 bool at_ingress = skb_at_tc_ingress(skb);
0085 struct cls_bpf_prog *prog;
0086 int ret = -1;
0087
0088 list_for_each_entry_rcu(prog, &head->plist, link) {
0089 int filter_res;
0090
0091 qdisc_skb_cb(skb)->tc_classid = prog->res.classid;
0092
0093 if (tc_skip_sw(prog->gen_flags)) {
0094 filter_res = prog->exts_integrated ? TC_ACT_UNSPEC : 0;
0095 } else if (at_ingress) {
0096
0097 __skb_push(skb, skb->mac_len);
0098 bpf_compute_data_pointers(skb);
0099 filter_res = bpf_prog_run(prog->filter, skb);
0100 __skb_pull(skb, skb->mac_len);
0101 } else {
0102 bpf_compute_data_pointers(skb);
0103 filter_res = bpf_prog_run(prog->filter, skb);
0104 }
0105 if (unlikely(!skb->tstamp && skb->mono_delivery_time))
0106 skb->mono_delivery_time = 0;
0107
0108 if (prog->exts_integrated) {
0109 res->class = 0;
0110 res->classid = TC_H_MAJ(prog->res.classid) |
0111 qdisc_skb_cb(skb)->tc_classid;
0112
0113 ret = cls_bpf_exec_opcode(filter_res);
0114 if (ret == TC_ACT_UNSPEC)
0115 continue;
0116 break;
0117 }
0118
0119 if (filter_res == 0)
0120 continue;
0121 if (filter_res != -1) {
0122 res->class = 0;
0123 res->classid = filter_res;
0124 } else {
0125 *res = prog->res;
0126 }
0127
0128 ret = tcf_exts_exec(skb, &prog->exts, res);
0129 if (ret < 0)
0130 continue;
0131
0132 break;
0133 }
0134
0135 return ret;
0136 }
0137
0138 static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog)
0139 {
0140 return !prog->bpf_ops;
0141 }
0142
0143 static int cls_bpf_offload_cmd(struct tcf_proto *tp, struct cls_bpf_prog *prog,
0144 struct cls_bpf_prog *oldprog,
0145 struct netlink_ext_ack *extack)
0146 {
0147 struct tcf_block *block = tp->chain->block;
0148 struct tc_cls_bpf_offload cls_bpf = {};
0149 struct cls_bpf_prog *obj;
0150 bool skip_sw;
0151 int err;
0152
0153 skip_sw = prog && tc_skip_sw(prog->gen_flags);
0154 obj = prog ?: oldprog;
0155
0156 tc_cls_common_offload_init(&cls_bpf.common, tp, obj->gen_flags, extack);
0157 cls_bpf.command = TC_CLSBPF_OFFLOAD;
0158 cls_bpf.exts = &obj->exts;
0159 cls_bpf.prog = prog ? prog->filter : NULL;
0160 cls_bpf.oldprog = oldprog ? oldprog->filter : NULL;
0161 cls_bpf.name = obj->bpf_name;
0162 cls_bpf.exts_integrated = obj->exts_integrated;
0163
0164 if (oldprog && prog)
0165 err = tc_setup_cb_replace(block, tp, TC_SETUP_CLSBPF, &cls_bpf,
0166 skip_sw, &oldprog->gen_flags,
0167 &oldprog->in_hw_count,
0168 &prog->gen_flags, &prog->in_hw_count,
0169 true);
0170 else if (prog)
0171 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSBPF, &cls_bpf,
0172 skip_sw, &prog->gen_flags,
0173 &prog->in_hw_count, true);
0174 else
0175 err = tc_setup_cb_destroy(block, tp, TC_SETUP_CLSBPF, &cls_bpf,
0176 skip_sw, &oldprog->gen_flags,
0177 &oldprog->in_hw_count, true);
0178
0179 if (prog && err) {
0180 cls_bpf_offload_cmd(tp, oldprog, prog, extack);
0181 return err;
0182 }
0183
0184 if (prog && skip_sw && !(prog->gen_flags & TCA_CLS_FLAGS_IN_HW))
0185 return -EINVAL;
0186
0187 return 0;
0188 }
0189
0190 static u32 cls_bpf_flags(u32 flags)
0191 {
0192 return flags & CLS_BPF_SUPPORTED_GEN_FLAGS;
0193 }
0194
0195 static int cls_bpf_offload(struct tcf_proto *tp, struct cls_bpf_prog *prog,
0196 struct cls_bpf_prog *oldprog,
0197 struct netlink_ext_ack *extack)
0198 {
0199 if (prog && oldprog &&
0200 cls_bpf_flags(prog->gen_flags) !=
0201 cls_bpf_flags(oldprog->gen_flags))
0202 return -EINVAL;
0203
0204 if (prog && tc_skip_hw(prog->gen_flags))
0205 prog = NULL;
0206 if (oldprog && tc_skip_hw(oldprog->gen_flags))
0207 oldprog = NULL;
0208 if (!prog && !oldprog)
0209 return 0;
0210
0211 return cls_bpf_offload_cmd(tp, prog, oldprog, extack);
0212 }
0213
0214 static void cls_bpf_stop_offload(struct tcf_proto *tp,
0215 struct cls_bpf_prog *prog,
0216 struct netlink_ext_ack *extack)
0217 {
0218 int err;
0219
0220 err = cls_bpf_offload_cmd(tp, NULL, prog, extack);
0221 if (err)
0222 pr_err("Stopping hardware offload failed: %d\n", err);
0223 }
0224
0225 static void cls_bpf_offload_update_stats(struct tcf_proto *tp,
0226 struct cls_bpf_prog *prog)
0227 {
0228 struct tcf_block *block = tp->chain->block;
0229 struct tc_cls_bpf_offload cls_bpf = {};
0230
0231 tc_cls_common_offload_init(&cls_bpf.common, tp, prog->gen_flags, NULL);
0232 cls_bpf.command = TC_CLSBPF_STATS;
0233 cls_bpf.exts = &prog->exts;
0234 cls_bpf.prog = prog->filter;
0235 cls_bpf.name = prog->bpf_name;
0236 cls_bpf.exts_integrated = prog->exts_integrated;
0237
0238 tc_setup_cb_call(block, TC_SETUP_CLSBPF, &cls_bpf, false, true);
0239 }
0240
0241 static int cls_bpf_init(struct tcf_proto *tp)
0242 {
0243 struct cls_bpf_head *head;
0244
0245 head = kzalloc(sizeof(*head), GFP_KERNEL);
0246 if (head == NULL)
0247 return -ENOBUFS;
0248
0249 INIT_LIST_HEAD_RCU(&head->plist);
0250 idr_init(&head->handle_idr);
0251 rcu_assign_pointer(tp->root, head);
0252
0253 return 0;
0254 }
0255
0256 static void cls_bpf_free_parms(struct cls_bpf_prog *prog)
0257 {
0258 if (cls_bpf_is_ebpf(prog))
0259 bpf_prog_put(prog->filter);
0260 else
0261 bpf_prog_destroy(prog->filter);
0262
0263 kfree(prog->bpf_name);
0264 kfree(prog->bpf_ops);
0265 }
0266
0267 static void __cls_bpf_delete_prog(struct cls_bpf_prog *prog)
0268 {
0269 tcf_exts_destroy(&prog->exts);
0270 tcf_exts_put_net(&prog->exts);
0271
0272 cls_bpf_free_parms(prog);
0273 kfree(prog);
0274 }
0275
0276 static void cls_bpf_delete_prog_work(struct work_struct *work)
0277 {
0278 struct cls_bpf_prog *prog = container_of(to_rcu_work(work),
0279 struct cls_bpf_prog,
0280 rwork);
0281 rtnl_lock();
0282 __cls_bpf_delete_prog(prog);
0283 rtnl_unlock();
0284 }
0285
0286 static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog,
0287 struct netlink_ext_ack *extack)
0288 {
0289 struct cls_bpf_head *head = rtnl_dereference(tp->root);
0290
0291 idr_remove(&head->handle_idr, prog->handle);
0292 cls_bpf_stop_offload(tp, prog, extack);
0293 list_del_rcu(&prog->link);
0294 tcf_unbind_filter(tp, &prog->res);
0295 if (tcf_exts_get_net(&prog->exts))
0296 tcf_queue_work(&prog->rwork, cls_bpf_delete_prog_work);
0297 else
0298 __cls_bpf_delete_prog(prog);
0299 }
0300
0301 static int cls_bpf_delete(struct tcf_proto *tp, void *arg, bool *last,
0302 bool rtnl_held, struct netlink_ext_ack *extack)
0303 {
0304 struct cls_bpf_head *head = rtnl_dereference(tp->root);
0305
0306 __cls_bpf_delete(tp, arg, extack);
0307 *last = list_empty(&head->plist);
0308 return 0;
0309 }
0310
0311 static void cls_bpf_destroy(struct tcf_proto *tp, bool rtnl_held,
0312 struct netlink_ext_ack *extack)
0313 {
0314 struct cls_bpf_head *head = rtnl_dereference(tp->root);
0315 struct cls_bpf_prog *prog, *tmp;
0316
0317 list_for_each_entry_safe(prog, tmp, &head->plist, link)
0318 __cls_bpf_delete(tp, prog, extack);
0319
0320 idr_destroy(&head->handle_idr);
0321 kfree_rcu(head, rcu);
0322 }
0323
0324 static void *cls_bpf_get(struct tcf_proto *tp, u32 handle)
0325 {
0326 struct cls_bpf_head *head = rtnl_dereference(tp->root);
0327 struct cls_bpf_prog *prog;
0328
0329 list_for_each_entry(prog, &head->plist, link) {
0330 if (prog->handle == handle)
0331 return prog;
0332 }
0333
0334 return NULL;
0335 }
0336
0337 static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
0338 {
0339 struct sock_filter *bpf_ops;
0340 struct sock_fprog_kern fprog_tmp;
0341 struct bpf_prog *fp;
0342 u16 bpf_size, bpf_num_ops;
0343 int ret;
0344
0345 bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
0346 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
0347 return -EINVAL;
0348
0349 bpf_size = bpf_num_ops * sizeof(*bpf_ops);
0350 if (bpf_size != nla_len(tb[TCA_BPF_OPS]))
0351 return -EINVAL;
0352
0353 bpf_ops = kmemdup(nla_data(tb[TCA_BPF_OPS]), bpf_size, GFP_KERNEL);
0354 if (bpf_ops == NULL)
0355 return -ENOMEM;
0356
0357 fprog_tmp.len = bpf_num_ops;
0358 fprog_tmp.filter = bpf_ops;
0359
0360 ret = bpf_prog_create(&fp, &fprog_tmp);
0361 if (ret < 0) {
0362 kfree(bpf_ops);
0363 return ret;
0364 }
0365
0366 prog->bpf_ops = bpf_ops;
0367 prog->bpf_num_ops = bpf_num_ops;
0368 prog->bpf_name = NULL;
0369 prog->filter = fp;
0370
0371 return 0;
0372 }
0373
0374 static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
0375 u32 gen_flags, const struct tcf_proto *tp)
0376 {
0377 struct bpf_prog *fp;
0378 char *name = NULL;
0379 bool skip_sw;
0380 u32 bpf_fd;
0381
0382 bpf_fd = nla_get_u32(tb[TCA_BPF_FD]);
0383 skip_sw = gen_flags & TCA_CLS_FLAGS_SKIP_SW;
0384
0385 fp = bpf_prog_get_type_dev(bpf_fd, BPF_PROG_TYPE_SCHED_CLS, skip_sw);
0386 if (IS_ERR(fp))
0387 return PTR_ERR(fp);
0388
0389 if (tb[TCA_BPF_NAME]) {
0390 name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL);
0391 if (!name) {
0392 bpf_prog_put(fp);
0393 return -ENOMEM;
0394 }
0395 }
0396
0397 prog->bpf_ops = NULL;
0398 prog->bpf_name = name;
0399 prog->filter = fp;
0400
0401 if (fp->dst_needed)
0402 tcf_block_netif_keep_dst(tp->chain->block);
0403
0404 return 0;
0405 }
0406
0407 static int cls_bpf_set_parms(struct net *net, struct tcf_proto *tp,
0408 struct cls_bpf_prog *prog, unsigned long base,
0409 struct nlattr **tb, struct nlattr *est, u32 flags,
0410 struct netlink_ext_ack *extack)
0411 {
0412 bool is_bpf, is_ebpf, have_exts = false;
0413 u32 gen_flags = 0;
0414 int ret;
0415
0416 is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
0417 is_ebpf = tb[TCA_BPF_FD];
0418 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf))
0419 return -EINVAL;
0420
0421 ret = tcf_exts_validate(net, tp, tb, est, &prog->exts, flags,
0422 extack);
0423 if (ret < 0)
0424 return ret;
0425
0426 if (tb[TCA_BPF_FLAGS]) {
0427 u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]);
0428
0429 if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT)
0430 return -EINVAL;
0431
0432 have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT;
0433 }
0434 if (tb[TCA_BPF_FLAGS_GEN]) {
0435 gen_flags = nla_get_u32(tb[TCA_BPF_FLAGS_GEN]);
0436 if (gen_flags & ~CLS_BPF_SUPPORTED_GEN_FLAGS ||
0437 !tc_flags_valid(gen_flags))
0438 return -EINVAL;
0439 }
0440
0441 prog->exts_integrated = have_exts;
0442 prog->gen_flags = gen_flags;
0443
0444 ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
0445 cls_bpf_prog_from_efd(tb, prog, gen_flags, tp);
0446 if (ret < 0)
0447 return ret;
0448
0449 if (tb[TCA_BPF_CLASSID]) {
0450 prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
0451 tcf_bind_filter(tp, &prog->res, base);
0452 }
0453
0454 return 0;
0455 }
0456
0457 static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
0458 struct tcf_proto *tp, unsigned long base,
0459 u32 handle, struct nlattr **tca,
0460 void **arg, u32 flags,
0461 struct netlink_ext_ack *extack)
0462 {
0463 struct cls_bpf_head *head = rtnl_dereference(tp->root);
0464 struct cls_bpf_prog *oldprog = *arg;
0465 struct nlattr *tb[TCA_BPF_MAX + 1];
0466 struct cls_bpf_prog *prog;
0467 int ret;
0468
0469 if (tca[TCA_OPTIONS] == NULL)
0470 return -EINVAL;
0471
0472 ret = nla_parse_nested_deprecated(tb, TCA_BPF_MAX, tca[TCA_OPTIONS],
0473 bpf_policy, NULL);
0474 if (ret < 0)
0475 return ret;
0476
0477 prog = kzalloc(sizeof(*prog), GFP_KERNEL);
0478 if (!prog)
0479 return -ENOBUFS;
0480
0481 ret = tcf_exts_init(&prog->exts, net, TCA_BPF_ACT, TCA_BPF_POLICE);
0482 if (ret < 0)
0483 goto errout;
0484
0485 if (oldprog) {
0486 if (handle && oldprog->handle != handle) {
0487 ret = -EINVAL;
0488 goto errout;
0489 }
0490 }
0491
0492 if (handle == 0) {
0493 handle = 1;
0494 ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
0495 INT_MAX, GFP_KERNEL);
0496 } else if (!oldprog) {
0497 ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
0498 handle, GFP_KERNEL);
0499 }
0500
0501 if (ret)
0502 goto errout;
0503 prog->handle = handle;
0504
0505 ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], flags,
0506 extack);
0507 if (ret < 0)
0508 goto errout_idr;
0509
0510 ret = cls_bpf_offload(tp, prog, oldprog, extack);
0511 if (ret)
0512 goto errout_parms;
0513
0514 if (!tc_in_hw(prog->gen_flags))
0515 prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW;
0516
0517 if (oldprog) {
0518 idr_replace(&head->handle_idr, prog, handle);
0519 list_replace_rcu(&oldprog->link, &prog->link);
0520 tcf_unbind_filter(tp, &oldprog->res);
0521 tcf_exts_get_net(&oldprog->exts);
0522 tcf_queue_work(&oldprog->rwork, cls_bpf_delete_prog_work);
0523 } else {
0524 list_add_rcu(&prog->link, &head->plist);
0525 }
0526
0527 *arg = prog;
0528 return 0;
0529
0530 errout_parms:
0531 cls_bpf_free_parms(prog);
0532 errout_idr:
0533 if (!oldprog)
0534 idr_remove(&head->handle_idr, prog->handle);
0535 errout:
0536 tcf_exts_destroy(&prog->exts);
0537 kfree(prog);
0538 return ret;
0539 }
0540
0541 static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog,
0542 struct sk_buff *skb)
0543 {
0544 struct nlattr *nla;
0545
0546 if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
0547 return -EMSGSIZE;
0548
0549 nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
0550 sizeof(struct sock_filter));
0551 if (nla == NULL)
0552 return -EMSGSIZE;
0553
0554 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
0555
0556 return 0;
0557 }
0558
0559 static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog,
0560 struct sk_buff *skb)
0561 {
0562 struct nlattr *nla;
0563
0564 if (prog->bpf_name &&
0565 nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name))
0566 return -EMSGSIZE;
0567
0568 if (nla_put_u32(skb, TCA_BPF_ID, prog->filter->aux->id))
0569 return -EMSGSIZE;
0570
0571 nla = nla_reserve(skb, TCA_BPF_TAG, sizeof(prog->filter->tag));
0572 if (nla == NULL)
0573 return -EMSGSIZE;
0574
0575 memcpy(nla_data(nla), prog->filter->tag, nla_len(nla));
0576
0577 return 0;
0578 }
0579
0580 static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, void *fh,
0581 struct sk_buff *skb, struct tcmsg *tm, bool rtnl_held)
0582 {
0583 struct cls_bpf_prog *prog = fh;
0584 struct nlattr *nest;
0585 u32 bpf_flags = 0;
0586 int ret;
0587
0588 if (prog == NULL)
0589 return skb->len;
0590
0591 tm->tcm_handle = prog->handle;
0592
0593 cls_bpf_offload_update_stats(tp, prog);
0594
0595 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
0596 if (nest == NULL)
0597 goto nla_put_failure;
0598
0599 if (prog->res.classid &&
0600 nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
0601 goto nla_put_failure;
0602
0603 if (cls_bpf_is_ebpf(prog))
0604 ret = cls_bpf_dump_ebpf_info(prog, skb);
0605 else
0606 ret = cls_bpf_dump_bpf_info(prog, skb);
0607 if (ret)
0608 goto nla_put_failure;
0609
0610 if (tcf_exts_dump(skb, &prog->exts) < 0)
0611 goto nla_put_failure;
0612
0613 if (prog->exts_integrated)
0614 bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
0615 if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags))
0616 goto nla_put_failure;
0617 if (prog->gen_flags &&
0618 nla_put_u32(skb, TCA_BPF_FLAGS_GEN, prog->gen_flags))
0619 goto nla_put_failure;
0620
0621 nla_nest_end(skb, nest);
0622
0623 if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
0624 goto nla_put_failure;
0625
0626 return skb->len;
0627
0628 nla_put_failure:
0629 nla_nest_cancel(skb, nest);
0630 return -1;
0631 }
0632
0633 static void cls_bpf_bind_class(void *fh, u32 classid, unsigned long cl,
0634 void *q, unsigned long base)
0635 {
0636 struct cls_bpf_prog *prog = fh;
0637
0638 if (prog && prog->res.classid == classid) {
0639 if (cl)
0640 __tcf_bind_filter(q, &prog->res, base);
0641 else
0642 __tcf_unbind_filter(q, &prog->res);
0643 }
0644 }
0645
0646 static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg,
0647 bool rtnl_held)
0648 {
0649 struct cls_bpf_head *head = rtnl_dereference(tp->root);
0650 struct cls_bpf_prog *prog;
0651
0652 list_for_each_entry(prog, &head->plist, link) {
0653 if (arg->count < arg->skip)
0654 goto skip;
0655 if (arg->fn(tp, prog, arg) < 0) {
0656 arg->stop = 1;
0657 break;
0658 }
0659 skip:
0660 arg->count++;
0661 }
0662 }
0663
0664 static int cls_bpf_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
0665 void *cb_priv, struct netlink_ext_ack *extack)
0666 {
0667 struct cls_bpf_head *head = rtnl_dereference(tp->root);
0668 struct tcf_block *block = tp->chain->block;
0669 struct tc_cls_bpf_offload cls_bpf = {};
0670 struct cls_bpf_prog *prog;
0671 int err;
0672
0673 list_for_each_entry(prog, &head->plist, link) {
0674 if (tc_skip_hw(prog->gen_flags))
0675 continue;
0676
0677 tc_cls_common_offload_init(&cls_bpf.common, tp, prog->gen_flags,
0678 extack);
0679 cls_bpf.command = TC_CLSBPF_OFFLOAD;
0680 cls_bpf.exts = &prog->exts;
0681 cls_bpf.prog = add ? prog->filter : NULL;
0682 cls_bpf.oldprog = add ? NULL : prog->filter;
0683 cls_bpf.name = prog->bpf_name;
0684 cls_bpf.exts_integrated = prog->exts_integrated;
0685
0686 err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSBPF,
0687 &cls_bpf, cb_priv, &prog->gen_flags,
0688 &prog->in_hw_count);
0689 if (err)
0690 return err;
0691 }
0692
0693 return 0;
0694 }
0695
0696 static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
0697 .kind = "bpf",
0698 .owner = THIS_MODULE,
0699 .classify = cls_bpf_classify,
0700 .init = cls_bpf_init,
0701 .destroy = cls_bpf_destroy,
0702 .get = cls_bpf_get,
0703 .change = cls_bpf_change,
0704 .delete = cls_bpf_delete,
0705 .walk = cls_bpf_walk,
0706 .reoffload = cls_bpf_reoffload,
0707 .dump = cls_bpf_dump,
0708 .bind_class = cls_bpf_bind_class,
0709 };
0710
0711 static int __init cls_bpf_init_mod(void)
0712 {
0713 return register_tcf_proto_ops(&cls_bpf_ops);
0714 }
0715
0716 static void __exit cls_bpf_exit_mod(void)
0717 {
0718 unregister_tcf_proto_ops(&cls_bpf_ops);
0719 }
0720
0721 module_init(cls_bpf_init_mod);
0722 module_exit(cls_bpf_exit_mod);