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