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