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