2 * Berkeley Packet Filter based traffic classifier
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
8 * (C) 2013 Daniel Borkmann <dborkman@redhat.com>
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.
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>
21 #include <net/rtnetlink.h>
22 #include <net/pkt_cls.h>
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
27 MODULE_DESCRIPTION("TC BPF based classifier");
29 #define CLS_BPF_NAME_LEN 256
30 #define CLS_BPF_SUPPORTED_GEN_FLAGS \
31 (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)
34 struct list_head plist
;
40 struct bpf_prog
*filter
;
41 struct list_head link
;
42 struct tcf_result res
;
52 struct sock_filter
*bpf_ops
;
58 static const struct nla_policy bpf_policy
[TCA_BPF_MAX
+ 1] = {
59 [TCA_BPF_CLASSID
] = { .type
= NLA_U32
},
60 [TCA_BPF_FLAGS
] = { .type
= NLA_U32
},
61 [TCA_BPF_FLAGS_GEN
] = { .type
= NLA_U32
},
62 [TCA_BPF_FD
] = { .type
= NLA_U32
},
63 [TCA_BPF_NAME
] = { .type
= NLA_NUL_STRING
,
64 .len
= CLS_BPF_NAME_LEN
},
65 [TCA_BPF_OPS_LEN
] = { .type
= NLA_U16
},
66 [TCA_BPF_OPS
] = { .type
= NLA_BINARY
,
67 .len
= sizeof(struct sock_filter
) * BPF_MAXINSNS
},
70 static int cls_bpf_exec_opcode(int code
)
84 static int cls_bpf_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
85 struct tcf_result
*res
)
87 struct cls_bpf_head
*head
= rcu_dereference_bh(tp
->root
);
88 bool at_ingress
= skb_at_tc_ingress(skb
);
89 struct cls_bpf_prog
*prog
;
92 /* Needed here for accessing maps. */
94 list_for_each_entry_rcu(prog
, &head
->plist
, link
) {
97 qdisc_skb_cb(skb
)->tc_classid
= prog
->res
.classid
;
99 if (tc_skip_sw(prog
->gen_flags
)) {
100 filter_res
= prog
->exts_integrated
? TC_ACT_UNSPEC
: 0;
101 } else if (at_ingress
) {
102 /* It is safe to push/pull even if skb_shared() */
103 __skb_push(skb
, skb
->mac_len
);
104 bpf_compute_data_end(skb
);
105 filter_res
= BPF_PROG_RUN(prog
->filter
, skb
);
106 __skb_pull(skb
, skb
->mac_len
);
108 bpf_compute_data_end(skb
);
109 filter_res
= BPF_PROG_RUN(prog
->filter
, skb
);
112 if (prog
->exts_integrated
) {
114 res
->classid
= TC_H_MAJ(prog
->res
.classid
) |
115 qdisc_skb_cb(skb
)->tc_classid
;
117 ret
= cls_bpf_exec_opcode(filter_res
);
118 if (ret
== TC_ACT_UNSPEC
)
125 if (filter_res
!= -1) {
127 res
->classid
= filter_res
;
132 ret
= tcf_exts_exec(skb
, &prog
->exts
, res
);
143 static bool cls_bpf_is_ebpf(const struct cls_bpf_prog
*prog
)
145 return !prog
->bpf_ops
;
148 static int cls_bpf_offload_cmd(struct tcf_proto
*tp
, struct cls_bpf_prog
*prog
,
149 enum tc_clsbpf_command cmd
)
151 struct net_device
*dev
= tp
->q
->dev_queue
->dev
;
152 struct tc_cls_bpf_offload bpf_offload
= {};
153 struct tc_to_netdev offload
;
155 offload
.type
= TC_SETUP_CLSBPF
;
156 offload
.cls_bpf
= &bpf_offload
;
158 bpf_offload
.command
= cmd
;
159 bpf_offload
.exts
= &prog
->exts
;
160 bpf_offload
.prog
= prog
->filter
;
161 bpf_offload
.name
= prog
->bpf_name
;
162 bpf_offload
.exts_integrated
= prog
->exts_integrated
;
163 bpf_offload
.gen_flags
= prog
->gen_flags
;
165 return dev
->netdev_ops
->ndo_setup_tc(dev
, tp
->q
->handle
,
166 tp
->protocol
, &offload
);
169 static int cls_bpf_offload(struct tcf_proto
*tp
, struct cls_bpf_prog
*prog
,
170 struct cls_bpf_prog
*oldprog
)
172 struct net_device
*dev
= tp
->q
->dev_queue
->dev
;
173 struct cls_bpf_prog
*obj
= prog
;
174 enum tc_clsbpf_command cmd
;
178 skip_sw
= tc_skip_sw(prog
->gen_flags
) ||
179 (oldprog
&& tc_skip_sw(oldprog
->gen_flags
));
181 if (oldprog
&& oldprog
->offloaded
) {
182 if (tc_should_offload(dev
, tp
, prog
->gen_flags
)) {
183 cmd
= TC_CLSBPF_REPLACE
;
184 } else if (!tc_skip_sw(prog
->gen_flags
)) {
186 cmd
= TC_CLSBPF_DESTROY
;
191 if (!tc_should_offload(dev
, tp
, prog
->gen_flags
))
192 return skip_sw
? -EINVAL
: 0;
196 ret
= cls_bpf_offload_cmd(tp
, obj
, cmd
);
198 return skip_sw
? ret
: 0;
200 obj
->offloaded
= true;
202 oldprog
->offloaded
= false;
207 static void cls_bpf_stop_offload(struct tcf_proto
*tp
,
208 struct cls_bpf_prog
*prog
)
212 if (!prog
->offloaded
)
215 err
= cls_bpf_offload_cmd(tp
, prog
, TC_CLSBPF_DESTROY
);
217 pr_err("Stopping hardware offload failed: %d\n", err
);
221 prog
->offloaded
= false;
224 static void cls_bpf_offload_update_stats(struct tcf_proto
*tp
,
225 struct cls_bpf_prog
*prog
)
227 if (!prog
->offloaded
)
230 cls_bpf_offload_cmd(tp
, prog
, TC_CLSBPF_STATS
);
233 static int cls_bpf_init(struct tcf_proto
*tp
)
235 struct cls_bpf_head
*head
;
237 head
= kzalloc(sizeof(*head
), GFP_KERNEL
);
241 INIT_LIST_HEAD_RCU(&head
->plist
);
242 rcu_assign_pointer(tp
->root
, head
);
247 static void cls_bpf_delete_prog(struct tcf_proto
*tp
, struct cls_bpf_prog
*prog
)
249 tcf_exts_destroy(&prog
->exts
);
251 if (cls_bpf_is_ebpf(prog
))
252 bpf_prog_put(prog
->filter
);
254 bpf_prog_destroy(prog
->filter
);
256 kfree(prog
->bpf_name
);
257 kfree(prog
->bpf_ops
);
261 static void __cls_bpf_delete_prog(struct rcu_head
*rcu
)
263 struct cls_bpf_prog
*prog
= container_of(rcu
, struct cls_bpf_prog
, rcu
);
265 cls_bpf_delete_prog(prog
->tp
, prog
);
268 static int cls_bpf_delete(struct tcf_proto
*tp
, unsigned long arg
)
270 struct cls_bpf_prog
*prog
= (struct cls_bpf_prog
*) arg
;
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
);
280 static bool cls_bpf_destroy(struct tcf_proto
*tp
, bool force
)
282 struct cls_bpf_head
*head
= rtnl_dereference(tp
->root
);
283 struct cls_bpf_prog
*prog
, *tmp
;
285 if (!force
&& !list_empty(&head
->plist
))
288 list_for_each_entry_safe(prog
, tmp
, &head
->plist
, link
) {
289 cls_bpf_stop_offload(tp
, prog
);
290 list_del_rcu(&prog
->link
);
291 tcf_unbind_filter(tp
, &prog
->res
);
292 call_rcu(&prog
->rcu
, __cls_bpf_delete_prog
);
295 kfree_rcu(head
, rcu
);
299 static unsigned long cls_bpf_get(struct tcf_proto
*tp
, u32 handle
)
301 struct cls_bpf_head
*head
= rtnl_dereference(tp
->root
);
302 struct cls_bpf_prog
*prog
;
303 unsigned long ret
= 0UL;
305 list_for_each_entry(prog
, &head
->plist
, link
) {
306 if (prog
->handle
== handle
) {
307 ret
= (unsigned long) prog
;
315 static int cls_bpf_prog_from_ops(struct nlattr
**tb
, struct cls_bpf_prog
*prog
)
317 struct sock_filter
*bpf_ops
;
318 struct sock_fprog_kern fprog_tmp
;
320 u16 bpf_size
, bpf_num_ops
;
323 bpf_num_ops
= nla_get_u16(tb
[TCA_BPF_OPS_LEN
]);
324 if (bpf_num_ops
> BPF_MAXINSNS
|| bpf_num_ops
== 0)
327 bpf_size
= bpf_num_ops
* sizeof(*bpf_ops
);
328 if (bpf_size
!= nla_len(tb
[TCA_BPF_OPS
]))
331 bpf_ops
= kzalloc(bpf_size
, GFP_KERNEL
);
335 memcpy(bpf_ops
, nla_data(tb
[TCA_BPF_OPS
]), bpf_size
);
337 fprog_tmp
.len
= bpf_num_ops
;
338 fprog_tmp
.filter
= bpf_ops
;
340 ret
= bpf_prog_create(&fp
, &fprog_tmp
);
346 prog
->bpf_ops
= bpf_ops
;
347 prog
->bpf_num_ops
= bpf_num_ops
;
348 prog
->bpf_name
= NULL
;
354 static int cls_bpf_prog_from_efd(struct nlattr
**tb
, struct cls_bpf_prog
*prog
,
355 const struct tcf_proto
*tp
)
361 bpf_fd
= nla_get_u32(tb
[TCA_BPF_FD
]);
363 fp
= bpf_prog_get_type(bpf_fd
, BPF_PROG_TYPE_SCHED_CLS
);
367 if (tb
[TCA_BPF_NAME
]) {
368 name
= kmemdup(nla_data(tb
[TCA_BPF_NAME
]),
369 nla_len(tb
[TCA_BPF_NAME
]),
377 prog
->bpf_ops
= NULL
;
378 prog
->bpf_fd
= bpf_fd
;
379 prog
->bpf_name
= name
;
382 if (fp
->dst_needed
&& !(tp
->q
->flags
& TCQ_F_INGRESS
))
383 netif_keep_dst(qdisc_dev(tp
->q
));
388 static int cls_bpf_modify_existing(struct net
*net
, struct tcf_proto
*tp
,
389 struct cls_bpf_prog
*prog
,
390 unsigned long base
, struct nlattr
**tb
,
391 struct nlattr
*est
, bool ovr
)
393 bool is_bpf
, is_ebpf
, have_exts
= false;
394 struct tcf_exts exts
;
398 is_bpf
= tb
[TCA_BPF_OPS_LEN
] && tb
[TCA_BPF_OPS
];
399 is_ebpf
= tb
[TCA_BPF_FD
];
400 if ((!is_bpf
&& !is_ebpf
) || (is_bpf
&& is_ebpf
))
403 ret
= tcf_exts_init(&exts
, TCA_BPF_ACT
, TCA_BPF_POLICE
);
406 ret
= tcf_exts_validate(net
, tp
, tb
, est
, &exts
, ovr
);
410 if (tb
[TCA_BPF_FLAGS
]) {
411 u32 bpf_flags
= nla_get_u32(tb
[TCA_BPF_FLAGS
]);
413 if (bpf_flags
& ~TCA_BPF_FLAG_ACT_DIRECT
) {
418 have_exts
= bpf_flags
& TCA_BPF_FLAG_ACT_DIRECT
;
420 if (tb
[TCA_BPF_FLAGS_GEN
]) {
421 gen_flags
= nla_get_u32(tb
[TCA_BPF_FLAGS_GEN
]);
422 if (gen_flags
& ~CLS_BPF_SUPPORTED_GEN_FLAGS
||
423 !tc_flags_valid(gen_flags
)) {
429 prog
->exts_integrated
= have_exts
;
430 prog
->gen_flags
= gen_flags
;
432 ret
= is_bpf
? cls_bpf_prog_from_ops(tb
, prog
) :
433 cls_bpf_prog_from_efd(tb
, prog
, tp
);
437 if (tb
[TCA_BPF_CLASSID
]) {
438 prog
->res
.classid
= nla_get_u32(tb
[TCA_BPF_CLASSID
]);
439 tcf_bind_filter(tp
, &prog
->res
, base
);
442 tcf_exts_change(tp
, &prog
->exts
, &exts
);
446 tcf_exts_destroy(&exts
);
450 static u32
cls_bpf_grab_new_handle(struct tcf_proto
*tp
,
451 struct cls_bpf_head
*head
)
453 unsigned int i
= 0x80000000;
457 if (++head
->hgen
== 0x7FFFFFFF)
459 } while (--i
> 0 && cls_bpf_get(tp
, head
->hgen
));
461 if (unlikely(i
== 0)) {
462 pr_err("Insufficient number of handles\n");
471 static int cls_bpf_change(struct net
*net
, struct sk_buff
*in_skb
,
472 struct tcf_proto
*tp
, unsigned long base
,
473 u32 handle
, struct nlattr
**tca
,
474 unsigned long *arg
, bool ovr
)
476 struct cls_bpf_head
*head
= rtnl_dereference(tp
->root
);
477 struct cls_bpf_prog
*oldprog
= (struct cls_bpf_prog
*) *arg
;
478 struct nlattr
*tb
[TCA_BPF_MAX
+ 1];
479 struct cls_bpf_prog
*prog
;
482 if (tca
[TCA_OPTIONS
] == NULL
)
485 ret
= nla_parse_nested(tb
, TCA_BPF_MAX
, tca
[TCA_OPTIONS
], bpf_policy
);
489 prog
= kzalloc(sizeof(*prog
), GFP_KERNEL
);
493 ret
= tcf_exts_init(&prog
->exts
, TCA_BPF_ACT
, TCA_BPF_POLICE
);
498 if (handle
&& oldprog
->handle
!= handle
) {
505 prog
->handle
= cls_bpf_grab_new_handle(tp
, head
);
507 prog
->handle
= handle
;
508 if (prog
->handle
== 0) {
513 ret
= cls_bpf_modify_existing(net
, tp
, prog
, base
, tb
, tca
[TCA_RATE
],
518 ret
= cls_bpf_offload(tp
, prog
, oldprog
);
520 cls_bpf_delete_prog(tp
, prog
);
525 list_replace_rcu(&oldprog
->link
, &prog
->link
);
526 tcf_unbind_filter(tp
, &oldprog
->res
);
527 call_rcu(&oldprog
->rcu
, __cls_bpf_delete_prog
);
529 list_add_rcu(&prog
->link
, &head
->plist
);
532 *arg
= (unsigned long) prog
;
536 tcf_exts_destroy(&prog
->exts
);
541 static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog
*prog
,
546 if (nla_put_u16(skb
, TCA_BPF_OPS_LEN
, prog
->bpf_num_ops
))
549 nla
= nla_reserve(skb
, TCA_BPF_OPS
, prog
->bpf_num_ops
*
550 sizeof(struct sock_filter
));
554 memcpy(nla_data(nla
), prog
->bpf_ops
, nla_len(nla
));
559 static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog
*prog
,
562 if (nla_put_u32(skb
, TCA_BPF_FD
, prog
->bpf_fd
))
565 if (prog
->bpf_name
&&
566 nla_put_string(skb
, TCA_BPF_NAME
, prog
->bpf_name
))
572 static int cls_bpf_dump(struct net
*net
, struct tcf_proto
*tp
, unsigned long fh
,
573 struct sk_buff
*skb
, struct tcmsg
*tm
)
575 struct cls_bpf_prog
*prog
= (struct cls_bpf_prog
*) fh
;
583 tm
->tcm_handle
= prog
->handle
;
585 cls_bpf_offload_update_stats(tp
, prog
);
587 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
589 goto nla_put_failure
;
591 if (prog
->res
.classid
&&
592 nla_put_u32(skb
, TCA_BPF_CLASSID
, prog
->res
.classid
))
593 goto nla_put_failure
;
595 if (cls_bpf_is_ebpf(prog
))
596 ret
= cls_bpf_dump_ebpf_info(prog
, skb
);
598 ret
= cls_bpf_dump_bpf_info(prog
, skb
);
600 goto nla_put_failure
;
602 if (tcf_exts_dump(skb
, &prog
->exts
) < 0)
603 goto nla_put_failure
;
605 if (prog
->exts_integrated
)
606 bpf_flags
|= TCA_BPF_FLAG_ACT_DIRECT
;
607 if (bpf_flags
&& nla_put_u32(skb
, TCA_BPF_FLAGS
, bpf_flags
))
608 goto nla_put_failure
;
609 if (prog
->gen_flags
&&
610 nla_put_u32(skb
, TCA_BPF_FLAGS_GEN
, prog
->gen_flags
))
611 goto nla_put_failure
;
613 nla_nest_end(skb
, nest
);
615 if (tcf_exts_dump_stats(skb
, &prog
->exts
) < 0)
616 goto nla_put_failure
;
621 nla_nest_cancel(skb
, nest
);
625 static void cls_bpf_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
627 struct cls_bpf_head
*head
= rtnl_dereference(tp
->root
);
628 struct cls_bpf_prog
*prog
;
630 list_for_each_entry(prog
, &head
->plist
, link
) {
631 if (arg
->count
< arg
->skip
)
633 if (arg
->fn(tp
, (unsigned long) prog
, arg
) < 0) {
642 static struct tcf_proto_ops cls_bpf_ops __read_mostly
= {
644 .owner
= THIS_MODULE
,
645 .classify
= cls_bpf_classify
,
646 .init
= cls_bpf_init
,
647 .destroy
= cls_bpf_destroy
,
649 .change
= cls_bpf_change
,
650 .delete = cls_bpf_delete
,
651 .walk
= cls_bpf_walk
,
652 .dump
= cls_bpf_dump
,
655 static int __init
cls_bpf_init_mod(void)
657 return register_tcf_proto_ops(&cls_bpf_ops
);
660 static void __exit
cls_bpf_exit_mod(void)
662 unregister_tcf_proto_ops(&cls_bpf_ops
);
665 module_init(cls_bpf_init_mod
);
666 module_exit(cls_bpf_exit_mod
);