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