]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/sched/cls_bpf.c
net: sched: cls_u32: propagate extack support for filter offload
[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
ad9294db
DB
189static u32 cls_bpf_flags(u32 flags)
190{
191 return flags & CLS_BPF_SUPPORTED_GEN_FLAGS;
192}
193
eadb4148
JK
194static int cls_bpf_offload(struct tcf_proto *tp, struct cls_bpf_prog *prog,
195 struct cls_bpf_prog *oldprog)
332ae8e2 196{
ad9294db
DB
197 if (prog && oldprog &&
198 cls_bpf_flags(prog->gen_flags) !=
199 cls_bpf_flags(oldprog->gen_flags))
102740bd 200 return -EINVAL;
332ae8e2 201
102740bd
JK
202 if (prog && tc_skip_hw(prog->gen_flags))
203 prog = NULL;
204 if (oldprog && tc_skip_hw(oldprog->gen_flags))
205 oldprog = NULL;
206 if (!prog && !oldprog)
207 return 0;
eadb4148 208
102740bd 209 return cls_bpf_offload_cmd(tp, prog, oldprog);
332ae8e2
JK
210}
211
212static void cls_bpf_stop_offload(struct tcf_proto *tp,
213 struct cls_bpf_prog *prog)
214{
215 int err;
216
102740bd
JK
217 err = cls_bpf_offload_cmd(tp, NULL, prog);
218 if (err)
332ae8e2 219 pr_err("Stopping hardware offload failed: %d\n", err);
332ae8e2
JK
220}
221
68d64063
JK
222static void cls_bpf_offload_update_stats(struct tcf_proto *tp,
223 struct cls_bpf_prog *prog)
224{
102740bd
JK
225 struct tcf_block *block = tp->chain->block;
226 struct tc_cls_bpf_offload cls_bpf = {};
227
228 tc_cls_common_offload_init(&cls_bpf.common, tp);
229 cls_bpf.command = TC_CLSBPF_STATS;
230 cls_bpf.exts = &prog->exts;
231 cls_bpf.prog = prog->filter;
232 cls_bpf.name = prog->bpf_name;
233 cls_bpf.exts_integrated = prog->exts_integrated;
234 cls_bpf.gen_flags = prog->gen_flags;
68d64063 235
102740bd 236 tc_setup_cb_call(block, NULL, TC_SETUP_CLSBPF, &cls_bpf, false);
68d64063
JK
237}
238
7d1d65cb
DB
239static int cls_bpf_init(struct tcf_proto *tp)
240{
241 struct cls_bpf_head *head;
242
243 head = kzalloc(sizeof(*head), GFP_KERNEL);
244 if (head == NULL)
245 return -ENOBUFS;
246
1f947bf1 247 INIT_LIST_HEAD_RCU(&head->plist);
76cf546c 248 idr_init(&head->handle_idr);
1f947bf1 249 rcu_assign_pointer(tp->root, head);
7d1d65cb
DB
250
251 return 0;
252}
253
25415cec 254static void cls_bpf_free_parms(struct cls_bpf_prog *prog)
7d1d65cb 255{
e2e9b654
DB
256 if (cls_bpf_is_ebpf(prog))
257 bpf_prog_put(prog->filter);
258 else
259 bpf_prog_destroy(prog->filter);
7d1d65cb 260
e2e9b654 261 kfree(prog->bpf_name);
7d1d65cb 262 kfree(prog->bpf_ops);
25415cec
JK
263}
264
265static void __cls_bpf_delete_prog(struct cls_bpf_prog *prog)
266{
267 tcf_exts_destroy(&prog->exts);
268 tcf_exts_put_net(&prog->exts);
269
270 cls_bpf_free_parms(prog);
7d1d65cb
DB
271 kfree(prog);
272}
273
e910af67
CW
274static void cls_bpf_delete_prog_work(struct work_struct *work)
275{
276 struct cls_bpf_prog *prog = container_of(work, struct cls_bpf_prog, work);
277
278 rtnl_lock();
279 __cls_bpf_delete_prog(prog);
280 rtnl_unlock();
281}
282
8d829bdb 283static void cls_bpf_delete_prog_rcu(struct rcu_head *rcu)
1f947bf1 284{
e910af67
CW
285 struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu);
286
287 INIT_WORK(&prog->work, cls_bpf_delete_prog_work);
288 tcf_queue_work(&prog->work);
1f947bf1
JF
289}
290
8d829bdb 291static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog)
7d1d65cb 292{
76cf546c
CW
293 struct cls_bpf_head *head = rtnl_dereference(tp->root);
294
295 idr_remove_ext(&head->handle_idr, prog->handle);
332ae8e2 296 cls_bpf_stop_offload(tp, prog);
472f5837
JP
297 list_del_rcu(&prog->link);
298 tcf_unbind_filter(tp, &prog->res);
aae2c35e
CW
299 if (tcf_exts_get_net(&prog->exts))
300 call_rcu(&prog->rcu, cls_bpf_delete_prog_rcu);
301 else
302 __cls_bpf_delete_prog(prog);
8d829bdb 303}
e2e9b654 304
571acf21
AA
305static int cls_bpf_delete(struct tcf_proto *tp, void *arg, bool *last,
306 struct netlink_ext_ack *extack)
8d829bdb 307{
763dbf63
WC
308 struct cls_bpf_head *head = rtnl_dereference(tp->root);
309
8113c095 310 __cls_bpf_delete(tp, arg);
763dbf63 311 *last = list_empty(&head->plist);
472f5837 312 return 0;
7d1d65cb
DB
313}
314
763dbf63 315static void cls_bpf_destroy(struct tcf_proto *tp)
7d1d65cb 316{
1f947bf1 317 struct cls_bpf_head *head = rtnl_dereference(tp->root);
7d1d65cb
DB
318 struct cls_bpf_prog *prog, *tmp;
319
8d829bdb
DB
320 list_for_each_entry_safe(prog, tmp, &head->plist, link)
321 __cls_bpf_delete(tp, prog);
7d1d65cb 322
76cf546c 323 idr_destroy(&head->handle_idr);
1f947bf1 324 kfree_rcu(head, rcu);
7d1d65cb
DB
325}
326
8113c095 327static void *cls_bpf_get(struct tcf_proto *tp, u32 handle)
7d1d65cb 328{
1f947bf1 329 struct cls_bpf_head *head = rtnl_dereference(tp->root);
7d1d65cb 330 struct cls_bpf_prog *prog;
7d1d65cb 331
3fe6b49e 332 list_for_each_entry(prog, &head->plist, link) {
8113c095
WC
333 if (prog->handle == handle)
334 return prog;
7d1d65cb
DB
335 }
336
8113c095 337 return NULL;
7d1d65cb
DB
338}
339
045efa82 340static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
7d1d65cb 341{
1f947bf1 342 struct sock_filter *bpf_ops;
e2e9b654 343 struct sock_fprog_kern fprog_tmp;
1f947bf1 344 struct bpf_prog *fp;
33e9fcc6 345 u16 bpf_size, bpf_num_ops;
7d1d65cb
DB
346 int ret;
347
33e9fcc6 348 bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
e2e9b654
DB
349 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
350 return -EINVAL;
7d1d65cb 351
33e9fcc6 352 bpf_size = bpf_num_ops * sizeof(*bpf_ops);
e2e9b654
DB
353 if (bpf_size != nla_len(tb[TCA_BPF_OPS]))
354 return -EINVAL;
7913ecf6 355
7d1d65cb 356 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
e2e9b654
DB
357 if (bpf_ops == NULL)
358 return -ENOMEM;
7d1d65cb
DB
359
360 memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
361
e2e9b654
DB
362 fprog_tmp.len = bpf_num_ops;
363 fprog_tmp.filter = bpf_ops;
7d1d65cb 364
e2e9b654
DB
365 ret = bpf_prog_create(&fp, &fprog_tmp);
366 if (ret < 0) {
367 kfree(bpf_ops);
368 return ret;
369 }
7d1d65cb 370
7d1d65cb 371 prog->bpf_ops = bpf_ops;
e2e9b654
DB
372 prog->bpf_num_ops = bpf_num_ops;
373 prog->bpf_name = NULL;
7d1d65cb 374 prog->filter = fp;
7d1d65cb 375
e2e9b654
DB
376 return 0;
377}
378
c46646d0 379static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
6c8dfe21 380 u32 gen_flags, const struct tcf_proto *tp)
e2e9b654
DB
381{
382 struct bpf_prog *fp;
383 char *name = NULL;
288b3de5 384 bool skip_sw;
e2e9b654
DB
385 u32 bpf_fd;
386
387 bpf_fd = nla_get_u32(tb[TCA_BPF_FD]);
288b3de5 388 skip_sw = gen_flags & TCA_CLS_FLAGS_SKIP_SW;
e2e9b654 389
288b3de5 390 fp = bpf_prog_get_type_dev(bpf_fd, BPF_PROG_TYPE_SCHED_CLS, skip_sw);
e2e9b654
DB
391 if (IS_ERR(fp))
392 return PTR_ERR(fp);
393
e2e9b654 394 if (tb[TCA_BPF_NAME]) {
b15ca182 395 name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL);
e2e9b654
DB
396 if (!name) {
397 bpf_prog_put(fp);
398 return -ENOMEM;
399 }
400 }
401
402 prog->bpf_ops = NULL;
e2e9b654 403 prog->bpf_name = name;
e2e9b654 404 prog->filter = fp;
e2e9b654 405
f36fe1c4
JP
406 if (fp->dst_needed)
407 tcf_block_netif_keep_dst(tp->chain->block);
c46646d0 408
e2e9b654
DB
409 return 0;
410}
411
6a725c48
JP
412static int cls_bpf_set_parms(struct net *net, struct tcf_proto *tp,
413 struct cls_bpf_prog *prog, unsigned long base,
50a56190
AA
414 struct nlattr **tb, struct nlattr *est, bool ovr,
415 struct netlink_ext_ack *extack)
e2e9b654 416{
045efa82 417 bool is_bpf, is_ebpf, have_exts = false;
0d01d45f 418 u32 gen_flags = 0;
e2e9b654
DB
419 int ret;
420
421 is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
422 is_ebpf = tb[TCA_BPF_FD];
ef146fa4 423 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf))
e2e9b654
DB
424 return -EINVAL;
425
50a56190 426 ret = tcf_exts_validate(net, tp, tb, est, &prog->exts, ovr, extack);
e2e9b654
DB
427 if (ret < 0)
428 return ret;
429
045efa82
DB
430 if (tb[TCA_BPF_FLAGS]) {
431 u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]);
432
6839da32
JP
433 if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT)
434 return -EINVAL;
045efa82
DB
435
436 have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT;
437 }
0d01d45f
JK
438 if (tb[TCA_BPF_FLAGS_GEN]) {
439 gen_flags = nla_get_u32(tb[TCA_BPF_FLAGS_GEN]);
440 if (gen_flags & ~CLS_BPF_SUPPORTED_GEN_FLAGS ||
6839da32
JP
441 !tc_flags_valid(gen_flags))
442 return -EINVAL;
0d01d45f 443 }
045efa82 444
045efa82 445 prog->exts_integrated = have_exts;
0d01d45f 446 prog->gen_flags = gen_flags;
e2e9b654 447
045efa82 448 ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
6c8dfe21 449 cls_bpf_prog_from_efd(tb, prog, gen_flags, tp);
b9a24bb7 450 if (ret < 0)
6839da32 451 return ret;
e2e9b654 452
ef146fa4
DB
453 if (tb[TCA_BPF_CLASSID]) {
454 prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
455 tcf_bind_filter(tp, &prog->res, base);
456 }
7d1d65cb 457
7d1d65cb 458 return 0;
7d1d65cb
DB
459}
460
7d1d65cb
DB
461static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
462 struct tcf_proto *tp, unsigned long base,
463 u32 handle, struct nlattr **tca,
7306db38 464 void **arg, bool ovr, struct netlink_ext_ack *extack)
7d1d65cb 465{
1f947bf1 466 struct cls_bpf_head *head = rtnl_dereference(tp->root);
8113c095 467 struct cls_bpf_prog *oldprog = *arg;
7d1d65cb 468 struct nlattr *tb[TCA_BPF_MAX + 1];
1f947bf1 469 struct cls_bpf_prog *prog;
76cf546c 470 unsigned long idr_index;
7d1d65cb
DB
471 int ret;
472
473 if (tca[TCA_OPTIONS] == NULL)
474 return -EINVAL;
475
fceb6435
JB
476 ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy,
477 NULL);
7d1d65cb
DB
478 if (ret < 0)
479 return ret;
480
7d1d65cb 481 prog = kzalloc(sizeof(*prog), GFP_KERNEL);
1f947bf1 482 if (!prog)
7d1d65cb
DB
483 return -ENOBUFS;
484
b9a24bb7
WC
485 ret = tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
486 if (ret < 0)
487 goto errout;
1f947bf1
JF
488
489 if (oldprog) {
490 if (handle && oldprog->handle != handle) {
491 ret = -EINVAL;
492 goto errout;
493 }
494 }
495
76cf546c
CW
496 if (handle == 0) {
497 ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index,
498 1, 0x7FFFFFFF, GFP_KERNEL);
499 if (ret)
500 goto errout;
501 prog->handle = idr_index;
502 } else {
503 if (!oldprog) {
504 ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index,
505 handle, handle + 1, GFP_KERNEL);
506 if (ret)
507 goto errout;
508 }
7d1d65cb 509 prog->handle = handle;
7d1d65cb
DB
510 }
511
50a56190
AA
512 ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr,
513 extack);
7d1d65cb 514 if (ret < 0)
76cf546c 515 goto errout_idr;
7d1d65cb 516
eadb4148 517 ret = cls_bpf_offload(tp, prog, oldprog);
25415cec
JK
518 if (ret)
519 goto errout_parms;
332ae8e2 520
5cecb6cc
OG
521 if (!tc_in_hw(prog->gen_flags))
522 prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW;
523
1f947bf1 524 if (oldprog) {
76cf546c 525 idr_replace_ext(&head->handle_idr, prog, handle);
f6bfc46d 526 list_replace_rcu(&oldprog->link, &prog->link);
18cdb37e 527 tcf_unbind_filter(tp, &oldprog->res);
aae2c35e 528 tcf_exts_get_net(&oldprog->exts);
8d829bdb 529 call_rcu(&oldprog->rcu, cls_bpf_delete_prog_rcu);
1f947bf1
JF
530 } else {
531 list_add_rcu(&prog->link, &head->plist);
532 }
7d1d65cb 533
8113c095 534 *arg = prog;
7d1d65cb 535 return 0;
b9a24bb7 536
25415cec
JK
537errout_parms:
538 cls_bpf_free_parms(prog);
76cf546c
CW
539errout_idr:
540 if (!oldprog)
541 idr_remove_ext(&head->handle_idr, prog->handle);
7d1d65cb 542errout:
b9a24bb7 543 tcf_exts_destroy(&prog->exts);
1f947bf1 544 kfree(prog);
7d1d65cb
DB
545 return ret;
546}
547
e2e9b654
DB
548static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog,
549 struct sk_buff *skb)
550{
551 struct nlattr *nla;
552
553 if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
554 return -EMSGSIZE;
555
556 nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
557 sizeof(struct sock_filter));
558 if (nla == NULL)
559 return -EMSGSIZE;
560
561 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
562
563 return 0;
564}
565
566static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog,
567 struct sk_buff *skb)
568{
7bd509e3
DB
569 struct nlattr *nla;
570
e2e9b654
DB
571 if (prog->bpf_name &&
572 nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name))
573 return -EMSGSIZE;
574
e8628307
DB
575 if (nla_put_u32(skb, TCA_BPF_ID, prog->filter->aux->id))
576 return -EMSGSIZE;
577
f1f7714e 578 nla = nla_reserve(skb, TCA_BPF_TAG, sizeof(prog->filter->tag));
7bd509e3
DB
579 if (nla == NULL)
580 return -EMSGSIZE;
581
f1f7714e 582 memcpy(nla_data(nla), prog->filter->tag, nla_len(nla));
7bd509e3 583
e2e9b654
DB
584 return 0;
585}
586
8113c095 587static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, void *fh,
7d1d65cb
DB
588 struct sk_buff *skb, struct tcmsg *tm)
589{
8113c095 590 struct cls_bpf_prog *prog = fh;
e2e9b654 591 struct nlattr *nest;
bf007d1c 592 u32 bpf_flags = 0;
e2e9b654 593 int ret;
7d1d65cb
DB
594
595 if (prog == NULL)
596 return skb->len;
597
598 tm->tcm_handle = prog->handle;
599
68d64063
JK
600 cls_bpf_offload_update_stats(tp, prog);
601
7d1d65cb
DB
602 nest = nla_nest_start(skb, TCA_OPTIONS);
603 if (nest == NULL)
604 goto nla_put_failure;
605
ef146fa4
DB
606 if (prog->res.classid &&
607 nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
7d1d65cb 608 goto nla_put_failure;
7d1d65cb 609
e2e9b654
DB
610 if (cls_bpf_is_ebpf(prog))
611 ret = cls_bpf_dump_ebpf_info(prog, skb);
612 else
613 ret = cls_bpf_dump_bpf_info(prog, skb);
614 if (ret)
7d1d65cb
DB
615 goto nla_put_failure;
616
5da57f42 617 if (tcf_exts_dump(skb, &prog->exts) < 0)
7d1d65cb
DB
618 goto nla_put_failure;
619
bf007d1c
DB
620 if (prog->exts_integrated)
621 bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
622 if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags))
623 goto nla_put_failure;
0d01d45f
JK
624 if (prog->gen_flags &&
625 nla_put_u32(skb, TCA_BPF_FLAGS_GEN, prog->gen_flags))
626 goto nla_put_failure;
bf007d1c 627
7d1d65cb
DB
628 nla_nest_end(skb, nest);
629
5da57f42 630 if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
7d1d65cb
DB
631 goto nla_put_failure;
632
633 return skb->len;
634
635nla_put_failure:
636 nla_nest_cancel(skb, nest);
637 return -1;
638}
639
07d79fc7
CW
640static void cls_bpf_bind_class(void *fh, u32 classid, unsigned long cl)
641{
642 struct cls_bpf_prog *prog = fh;
643
644 if (prog && prog->res.classid == classid)
645 prog->res.class = cl;
646}
647
7d1d65cb
DB
648static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
649{
1f947bf1 650 struct cls_bpf_head *head = rtnl_dereference(tp->root);
7d1d65cb
DB
651 struct cls_bpf_prog *prog;
652
3fe6b49e 653 list_for_each_entry(prog, &head->plist, link) {
7d1d65cb
DB
654 if (arg->count < arg->skip)
655 goto skip;
8113c095 656 if (arg->fn(tp, prog, arg) < 0) {
7d1d65cb
DB
657 arg->stop = 1;
658 break;
659 }
660skip:
661 arg->count++;
662 }
663}
664
665static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
666 .kind = "bpf",
667 .owner = THIS_MODULE,
668 .classify = cls_bpf_classify,
669 .init = cls_bpf_init,
670 .destroy = cls_bpf_destroy,
671 .get = cls_bpf_get,
7d1d65cb
DB
672 .change = cls_bpf_change,
673 .delete = cls_bpf_delete,
674 .walk = cls_bpf_walk,
675 .dump = cls_bpf_dump,
07d79fc7 676 .bind_class = cls_bpf_bind_class,
7d1d65cb
DB
677};
678
679static int __init cls_bpf_init_mod(void)
680{
681 return register_tcf_proto_ops(&cls_bpf_ops);
682}
683
684static void __exit cls_bpf_exit_mod(void)
685{
686 unregister_tcf_proto_ops(&cls_bpf_ops);
687}
688
689module_init(cls_bpf_init_mod);
690module_exit(cls_bpf_exit_mod);