]>
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 DB |
19 | #include <linux/bpf.h> |
20 | ||
7d1d65cb DB |
21 | #include <net/rtnetlink.h> |
22 | #include <net/pkt_cls.h> | |
23 | #include <net/sock.h> | |
24 | ||
25 | MODULE_LICENSE("GPL"); | |
26 | MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>"); | |
27 | MODULE_DESCRIPTION("TC BPF based classifier"); | |
28 | ||
e2e9b654 | 29 | #define CLS_BPF_NAME_LEN 256 |
0d01d45f | 30 | #define CLS_BPF_SUPPORTED_GEN_FLAGS \ |
eadb4148 | 31 | (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW) |
e2e9b654 | 32 | |
7d1d65cb DB |
33 | struct cls_bpf_head { |
34 | struct list_head plist; | |
35 | u32 hgen; | |
1f947bf1 | 36 | struct rcu_head rcu; |
7d1d65cb DB |
37 | }; |
38 | ||
39 | struct cls_bpf_prog { | |
7ae457c1 | 40 | struct bpf_prog *filter; |
7d1d65cb | 41 | struct list_head link; |
e2e9b654 | 42 | struct tcf_result res; |
045efa82 | 43 | bool exts_integrated; |
332ae8e2 | 44 | bool offloaded; |
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); | |
db58ba45 | 105 | bpf_compute_data_end(skb); |
3431205e AS |
106 | filter_res = BPF_PROG_RUN(prog->filter, skb); |
107 | __skb_pull(skb, skb->mac_len); | |
108 | } else { | |
db58ba45 | 109 | bpf_compute_data_end(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 JK |
149 | static int cls_bpf_offload_cmd(struct tcf_proto *tp, struct cls_bpf_prog *prog, |
150 | enum tc_clsbpf_command cmd) | |
151 | { | |
152 | struct net_device *dev = tp->q->dev_queue->dev; | |
de4784ca | 153 | struct tc_cls_bpf_offload cls_bpf = {}; |
5cecb6cc | 154 | int err; |
332ae8e2 | 155 | |
de4784ca JP |
156 | tc_cls_common_offload_init(&cls_bpf.common, tp); |
157 | cls_bpf.command = cmd; | |
158 | cls_bpf.exts = &prog->exts; | |
159 | cls_bpf.prog = prog->filter; | |
160 | cls_bpf.name = prog->bpf_name; | |
161 | cls_bpf.exts_integrated = prog->exts_integrated; | |
162 | cls_bpf.gen_flags = prog->gen_flags; | |
332ae8e2 | 163 | |
de4784ca | 164 | err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSBPF, &cls_bpf); |
5cecb6cc OG |
165 | if (!err && (cmd == TC_CLSBPF_ADD || cmd == TC_CLSBPF_REPLACE)) |
166 | prog->gen_flags |= TCA_CLS_FLAGS_IN_HW; | |
167 | ||
168 | return err; | |
332ae8e2 JK |
169 | } |
170 | ||
eadb4148 JK |
171 | static int cls_bpf_offload(struct tcf_proto *tp, struct cls_bpf_prog *prog, |
172 | struct cls_bpf_prog *oldprog) | |
332ae8e2 JK |
173 | { |
174 | struct net_device *dev = tp->q->dev_queue->dev; | |
175 | struct cls_bpf_prog *obj = prog; | |
176 | enum tc_clsbpf_command cmd; | |
eadb4148 JK |
177 | bool skip_sw; |
178 | int ret; | |
179 | ||
180 | skip_sw = tc_skip_sw(prog->gen_flags) || | |
181 | (oldprog && tc_skip_sw(oldprog->gen_flags)); | |
332ae8e2 JK |
182 | |
183 | if (oldprog && oldprog->offloaded) { | |
7b06e8ae | 184 | if (tc_should_offload(dev, prog->gen_flags)) { |
332ae8e2 | 185 | cmd = TC_CLSBPF_REPLACE; |
eadb4148 | 186 | } else if (!tc_skip_sw(prog->gen_flags)) { |
332ae8e2 JK |
187 | obj = oldprog; |
188 | cmd = TC_CLSBPF_DESTROY; | |
eadb4148 JK |
189 | } else { |
190 | return -EINVAL; | |
332ae8e2 JK |
191 | } |
192 | } else { | |
7b06e8ae | 193 | if (!tc_should_offload(dev, prog->gen_flags)) |
eadb4148 | 194 | return skip_sw ? -EINVAL : 0; |
332ae8e2 JK |
195 | cmd = TC_CLSBPF_ADD; |
196 | } | |
197 | ||
eadb4148 JK |
198 | ret = cls_bpf_offload_cmd(tp, obj, cmd); |
199 | if (ret) | |
200 | return skip_sw ? ret : 0; | |
332ae8e2 JK |
201 | |
202 | obj->offloaded = true; | |
203 | if (oldprog) | |
204 | oldprog->offloaded = false; | |
eadb4148 JK |
205 | |
206 | return 0; | |
332ae8e2 JK |
207 | } |
208 | ||
209 | static void cls_bpf_stop_offload(struct tcf_proto *tp, | |
210 | struct cls_bpf_prog *prog) | |
211 | { | |
212 | int err; | |
213 | ||
214 | if (!prog->offloaded) | |
215 | return; | |
216 | ||
217 | err = cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_DESTROY); | |
218 | if (err) { | |
219 | pr_err("Stopping hardware offload failed: %d\n", err); | |
220 | return; | |
221 | } | |
222 | ||
223 | prog->offloaded = false; | |
224 | } | |
225 | ||
68d64063 JK |
226 | static void cls_bpf_offload_update_stats(struct tcf_proto *tp, |
227 | struct cls_bpf_prog *prog) | |
228 | { | |
229 | if (!prog->offloaded) | |
230 | return; | |
231 | ||
232 | cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_STATS); | |
233 | } | |
234 | ||
7d1d65cb DB |
235 | static int cls_bpf_init(struct tcf_proto *tp) |
236 | { | |
237 | struct cls_bpf_head *head; | |
238 | ||
239 | head = kzalloc(sizeof(*head), GFP_KERNEL); | |
240 | if (head == NULL) | |
241 | return -ENOBUFS; | |
242 | ||
1f947bf1 JF |
243 | INIT_LIST_HEAD_RCU(&head->plist); |
244 | rcu_assign_pointer(tp->root, head); | |
7d1d65cb DB |
245 | |
246 | return 0; | |
247 | } | |
248 | ||
8d829bdb | 249 | static void __cls_bpf_delete_prog(struct cls_bpf_prog *prog) |
7d1d65cb | 250 | { |
18d0264f | 251 | tcf_exts_destroy(&prog->exts); |
aae2c35e | 252 | tcf_exts_put_net(&prog->exts); |
7d1d65cb | 253 | |
e2e9b654 DB |
254 | if (cls_bpf_is_ebpf(prog)) |
255 | bpf_prog_put(prog->filter); | |
256 | else | |
257 | bpf_prog_destroy(prog->filter); | |
7d1d65cb | 258 | |
e2e9b654 | 259 | kfree(prog->bpf_name); |
7d1d65cb DB |
260 | kfree(prog->bpf_ops); |
261 | kfree(prog); | |
262 | } | |
263 | ||
e910af67 CW |
264 | static 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 | 273 | static 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 | 281 | static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog) |
7d1d65cb | 282 | { |
332ae8e2 | 283 | cls_bpf_stop_offload(tp, prog); |
472f5837 JP |
284 | list_del_rcu(&prog->link); |
285 | tcf_unbind_filter(tp, &prog->res); | |
aae2c35e CW |
286 | if (tcf_exts_get_net(&prog->exts)) |
287 | call_rcu(&prog->rcu, cls_bpf_delete_prog_rcu); | |
288 | else | |
289 | __cls_bpf_delete_prog(prog); | |
8d829bdb | 290 | } |
e2e9b654 | 291 | |
8113c095 | 292 | static int cls_bpf_delete(struct tcf_proto *tp, void *arg, bool *last) |
8d829bdb | 293 | { |
763dbf63 WC |
294 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
295 | ||
8113c095 | 296 | __cls_bpf_delete(tp, arg); |
763dbf63 | 297 | *last = list_empty(&head->plist); |
472f5837 | 298 | return 0; |
7d1d65cb DB |
299 | } |
300 | ||
763dbf63 | 301 | static void cls_bpf_destroy(struct tcf_proto *tp) |
7d1d65cb | 302 | { |
1f947bf1 | 303 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
304 | struct cls_bpf_prog *prog, *tmp; |
305 | ||
8d829bdb DB |
306 | list_for_each_entry_safe(prog, tmp, &head->plist, link) |
307 | __cls_bpf_delete(tp, prog); | |
7d1d65cb | 308 | |
1f947bf1 | 309 | kfree_rcu(head, rcu); |
7d1d65cb DB |
310 | } |
311 | ||
8113c095 | 312 | static void *cls_bpf_get(struct tcf_proto *tp, u32 handle) |
7d1d65cb | 313 | { |
1f947bf1 | 314 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb | 315 | struct cls_bpf_prog *prog; |
7d1d65cb | 316 | |
3fe6b49e | 317 | list_for_each_entry(prog, &head->plist, link) { |
8113c095 WC |
318 | if (prog->handle == handle) |
319 | return prog; | |
7d1d65cb DB |
320 | } |
321 | ||
8113c095 | 322 | return NULL; |
7d1d65cb DB |
323 | } |
324 | ||
045efa82 | 325 | static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog) |
7d1d65cb | 326 | { |
1f947bf1 | 327 | struct sock_filter *bpf_ops; |
e2e9b654 | 328 | struct sock_fprog_kern fprog_tmp; |
1f947bf1 | 329 | struct bpf_prog *fp; |
33e9fcc6 | 330 | u16 bpf_size, bpf_num_ops; |
7d1d65cb DB |
331 | int ret; |
332 | ||
33e9fcc6 | 333 | bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]); |
e2e9b654 DB |
334 | if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) |
335 | return -EINVAL; | |
7d1d65cb | 336 | |
33e9fcc6 | 337 | bpf_size = bpf_num_ops * sizeof(*bpf_ops); |
e2e9b654 DB |
338 | if (bpf_size != nla_len(tb[TCA_BPF_OPS])) |
339 | return -EINVAL; | |
7913ecf6 | 340 | |
7d1d65cb | 341 | bpf_ops = kzalloc(bpf_size, GFP_KERNEL); |
e2e9b654 DB |
342 | if (bpf_ops == NULL) |
343 | return -ENOMEM; | |
7d1d65cb DB |
344 | |
345 | memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); | |
346 | ||
e2e9b654 DB |
347 | fprog_tmp.len = bpf_num_ops; |
348 | fprog_tmp.filter = bpf_ops; | |
7d1d65cb | 349 | |
e2e9b654 DB |
350 | ret = bpf_prog_create(&fp, &fprog_tmp); |
351 | if (ret < 0) { | |
352 | kfree(bpf_ops); | |
353 | return ret; | |
354 | } | |
7d1d65cb | 355 | |
7d1d65cb | 356 | prog->bpf_ops = bpf_ops; |
e2e9b654 DB |
357 | prog->bpf_num_ops = bpf_num_ops; |
358 | prog->bpf_name = NULL; | |
7d1d65cb | 359 | prog->filter = fp; |
7d1d65cb | 360 | |
e2e9b654 DB |
361 | return 0; |
362 | } | |
363 | ||
c46646d0 DB |
364 | static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog, |
365 | const struct tcf_proto *tp) | |
e2e9b654 DB |
366 | { |
367 | struct bpf_prog *fp; | |
368 | char *name = NULL; | |
369 | u32 bpf_fd; | |
370 | ||
371 | bpf_fd = nla_get_u32(tb[TCA_BPF_FD]); | |
372 | ||
113214be | 373 | fp = bpf_prog_get_type(bpf_fd, BPF_PROG_TYPE_SCHED_CLS); |
e2e9b654 DB |
374 | if (IS_ERR(fp)) |
375 | return PTR_ERR(fp); | |
376 | ||
e2e9b654 | 377 | if (tb[TCA_BPF_NAME]) { |
b15ca182 | 378 | name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL); |
e2e9b654 DB |
379 | if (!name) { |
380 | bpf_prog_put(fp); | |
381 | return -ENOMEM; | |
382 | } | |
383 | } | |
384 | ||
385 | prog->bpf_ops = NULL; | |
e2e9b654 | 386 | prog->bpf_name = name; |
e2e9b654 | 387 | prog->filter = fp; |
e2e9b654 | 388 | |
1f211a1b | 389 | if (fp->dst_needed && !(tp->q->flags & TCQ_F_INGRESS)) |
c46646d0 DB |
390 | netif_keep_dst(qdisc_dev(tp->q)); |
391 | ||
e2e9b654 DB |
392 | return 0; |
393 | } | |
394 | ||
6a725c48 JP |
395 | static int cls_bpf_set_parms(struct net *net, struct tcf_proto *tp, |
396 | struct cls_bpf_prog *prog, unsigned long base, | |
397 | struct nlattr **tb, struct nlattr *est, bool ovr) | |
e2e9b654 | 398 | { |
045efa82 | 399 | bool is_bpf, is_ebpf, have_exts = false; |
0d01d45f | 400 | u32 gen_flags = 0; |
e2e9b654 DB |
401 | int ret; |
402 | ||
403 | is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS]; | |
404 | is_ebpf = tb[TCA_BPF_FD]; | |
ef146fa4 | 405 | if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) |
e2e9b654 DB |
406 | return -EINVAL; |
407 | ||
6839da32 | 408 | ret = tcf_exts_validate(net, tp, tb, est, &prog->exts, ovr); |
e2e9b654 DB |
409 | if (ret < 0) |
410 | return ret; | |
411 | ||
045efa82 DB |
412 | if (tb[TCA_BPF_FLAGS]) { |
413 | u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]); | |
414 | ||
6839da32 JP |
415 | if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT) |
416 | return -EINVAL; | |
045efa82 DB |
417 | |
418 | have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT; | |
419 | } | |
0d01d45f JK |
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 || | |
6839da32 JP |
423 | !tc_flags_valid(gen_flags)) |
424 | return -EINVAL; | |
0d01d45f | 425 | } |
045efa82 | 426 | |
045efa82 | 427 | prog->exts_integrated = have_exts; |
0d01d45f | 428 | prog->gen_flags = gen_flags; |
e2e9b654 | 429 | |
045efa82 | 430 | ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) : |
c46646d0 | 431 | cls_bpf_prog_from_efd(tb, prog, tp); |
b9a24bb7 | 432 | if (ret < 0) |
6839da32 | 433 | return ret; |
e2e9b654 | 434 | |
ef146fa4 DB |
435 | if (tb[TCA_BPF_CLASSID]) { |
436 | prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]); | |
437 | tcf_bind_filter(tp, &prog->res, base); | |
438 | } | |
7d1d65cb | 439 | |
7d1d65cb | 440 | return 0; |
7d1d65cb DB |
441 | } |
442 | ||
443 | static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp, | |
444 | struct cls_bpf_head *head) | |
445 | { | |
446 | unsigned int i = 0x80000000; | |
3f2ab135 | 447 | u32 handle; |
7d1d65cb DB |
448 | |
449 | do { | |
450 | if (++head->hgen == 0x7FFFFFFF) | |
451 | head->hgen = 1; | |
452 | } while (--i > 0 && cls_bpf_get(tp, head->hgen)); | |
3f2ab135 DB |
453 | |
454 | if (unlikely(i == 0)) { | |
7d1d65cb | 455 | pr_err("Insufficient number of handles\n"); |
3f2ab135 DB |
456 | handle = 0; |
457 | } else { | |
458 | handle = head->hgen; | |
459 | } | |
7d1d65cb | 460 | |
3f2ab135 | 461 | return handle; |
7d1d65cb DB |
462 | } |
463 | ||
464 | static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, | |
465 | struct tcf_proto *tp, unsigned long base, | |
466 | u32 handle, struct nlattr **tca, | |
8113c095 | 467 | void **arg, bool ovr) |
7d1d65cb | 468 | { |
1f947bf1 | 469 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
8113c095 | 470 | struct cls_bpf_prog *oldprog = *arg; |
7d1d65cb | 471 | struct nlattr *tb[TCA_BPF_MAX + 1]; |
1f947bf1 | 472 | struct cls_bpf_prog *prog; |
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 | ||
7d1d65cb DB |
498 | if (handle == 0) |
499 | prog->handle = cls_bpf_grab_new_handle(tp, head); | |
500 | else | |
501 | prog->handle = handle; | |
502 | if (prog->handle == 0) { | |
503 | ret = -EINVAL; | |
504 | goto errout; | |
505 | } | |
506 | ||
6a725c48 | 507 | ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr); |
7d1d65cb DB |
508 | if (ret < 0) |
509 | goto errout; | |
510 | ||
eadb4148 JK |
511 | ret = cls_bpf_offload(tp, prog, oldprog); |
512 | if (ret) { | |
8d829bdb | 513 | __cls_bpf_delete_prog(prog); |
eadb4148 JK |
514 | return ret; |
515 | } | |
332ae8e2 | 516 | |
5cecb6cc OG |
517 | if (!tc_in_hw(prog->gen_flags)) |
518 | prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW; | |
519 | ||
1f947bf1 | 520 | if (oldprog) { |
f6bfc46d | 521 | list_replace_rcu(&oldprog->link, &prog->link); |
18cdb37e | 522 | tcf_unbind_filter(tp, &oldprog->res); |
aae2c35e | 523 | tcf_exts_get_net(&oldprog->exts); |
8d829bdb | 524 | call_rcu(&oldprog->rcu, cls_bpf_delete_prog_rcu); |
1f947bf1 JF |
525 | } else { |
526 | list_add_rcu(&prog->link, &head->plist); | |
527 | } | |
7d1d65cb | 528 | |
8113c095 | 529 | *arg = prog; |
7d1d65cb | 530 | return 0; |
b9a24bb7 | 531 | |
7d1d65cb | 532 | errout: |
b9a24bb7 | 533 | tcf_exts_destroy(&prog->exts); |
1f947bf1 | 534 | kfree(prog); |
7d1d65cb DB |
535 | return ret; |
536 | } | |
537 | ||
e2e9b654 DB |
538 | static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog, |
539 | struct sk_buff *skb) | |
540 | { | |
541 | struct nlattr *nla; | |
542 | ||
543 | if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops)) | |
544 | return -EMSGSIZE; | |
545 | ||
546 | nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops * | |
547 | sizeof(struct sock_filter)); | |
548 | if (nla == NULL) | |
549 | return -EMSGSIZE; | |
550 | ||
551 | memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); | |
552 | ||
553 | return 0; | |
554 | } | |
555 | ||
556 | static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog, | |
557 | struct sk_buff *skb) | |
558 | { | |
7bd509e3 DB |
559 | struct nlattr *nla; |
560 | ||
e2e9b654 DB |
561 | if (prog->bpf_name && |
562 | nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name)) | |
563 | return -EMSGSIZE; | |
564 | ||
e8628307 DB |
565 | if (nla_put_u32(skb, TCA_BPF_ID, prog->filter->aux->id)) |
566 | return -EMSGSIZE; | |
567 | ||
f1f7714e | 568 | nla = nla_reserve(skb, TCA_BPF_TAG, sizeof(prog->filter->tag)); |
7bd509e3 DB |
569 | if (nla == NULL) |
570 | return -EMSGSIZE; | |
571 | ||
f1f7714e | 572 | memcpy(nla_data(nla), prog->filter->tag, nla_len(nla)); |
7bd509e3 | 573 | |
e2e9b654 DB |
574 | return 0; |
575 | } | |
576 | ||
8113c095 | 577 | static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, void *fh, |
7d1d65cb DB |
578 | struct sk_buff *skb, struct tcmsg *tm) |
579 | { | |
8113c095 | 580 | struct cls_bpf_prog *prog = fh; |
e2e9b654 | 581 | struct nlattr *nest; |
bf007d1c | 582 | u32 bpf_flags = 0; |
e2e9b654 | 583 | int ret; |
7d1d65cb DB |
584 | |
585 | if (prog == NULL) | |
586 | return skb->len; | |
587 | ||
588 | tm->tcm_handle = prog->handle; | |
589 | ||
68d64063 JK |
590 | cls_bpf_offload_update_stats(tp, prog); |
591 | ||
7d1d65cb DB |
592 | nest = nla_nest_start(skb, TCA_OPTIONS); |
593 | if (nest == NULL) | |
594 | goto nla_put_failure; | |
595 | ||
ef146fa4 DB |
596 | if (prog->res.classid && |
597 | nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) | |
7d1d65cb | 598 | goto nla_put_failure; |
7d1d65cb | 599 | |
e2e9b654 DB |
600 | if (cls_bpf_is_ebpf(prog)) |
601 | ret = cls_bpf_dump_ebpf_info(prog, skb); | |
602 | else | |
603 | ret = cls_bpf_dump_bpf_info(prog, skb); | |
604 | if (ret) | |
7d1d65cb DB |
605 | goto nla_put_failure; |
606 | ||
5da57f42 | 607 | if (tcf_exts_dump(skb, &prog->exts) < 0) |
7d1d65cb DB |
608 | goto nla_put_failure; |
609 | ||
bf007d1c DB |
610 | if (prog->exts_integrated) |
611 | bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT; | |
612 | if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags)) | |
613 | goto nla_put_failure; | |
0d01d45f JK |
614 | if (prog->gen_flags && |
615 | nla_put_u32(skb, TCA_BPF_FLAGS_GEN, prog->gen_flags)) | |
616 | goto nla_put_failure; | |
bf007d1c | 617 | |
7d1d65cb DB |
618 | nla_nest_end(skb, nest); |
619 | ||
5da57f42 | 620 | if (tcf_exts_dump_stats(skb, &prog->exts) < 0) |
7d1d65cb DB |
621 | goto nla_put_failure; |
622 | ||
623 | return skb->len; | |
624 | ||
625 | nla_put_failure: | |
626 | nla_nest_cancel(skb, nest); | |
627 | return -1; | |
628 | } | |
629 | ||
07d79fc7 CW |
630 | static void cls_bpf_bind_class(void *fh, u32 classid, unsigned long cl) |
631 | { | |
632 | struct cls_bpf_prog *prog = fh; | |
633 | ||
634 | if (prog && prog->res.classid == classid) | |
635 | prog->res.class = cl; | |
636 | } | |
637 | ||
7d1d65cb DB |
638 | static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
639 | { | |
1f947bf1 | 640 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
641 | struct cls_bpf_prog *prog; |
642 | ||
3fe6b49e | 643 | list_for_each_entry(prog, &head->plist, link) { |
7d1d65cb DB |
644 | if (arg->count < arg->skip) |
645 | goto skip; | |
8113c095 | 646 | if (arg->fn(tp, prog, arg) < 0) { |
7d1d65cb DB |
647 | arg->stop = 1; |
648 | break; | |
649 | } | |
650 | skip: | |
651 | arg->count++; | |
652 | } | |
653 | } | |
654 | ||
655 | static struct tcf_proto_ops cls_bpf_ops __read_mostly = { | |
656 | .kind = "bpf", | |
657 | .owner = THIS_MODULE, | |
658 | .classify = cls_bpf_classify, | |
659 | .init = cls_bpf_init, | |
660 | .destroy = cls_bpf_destroy, | |
661 | .get = cls_bpf_get, | |
7d1d65cb DB |
662 | .change = cls_bpf_change, |
663 | .delete = cls_bpf_delete, | |
664 | .walk = cls_bpf_walk, | |
665 | .dump = cls_bpf_dump, | |
07d79fc7 | 666 | .bind_class = cls_bpf_bind_class, |
7d1d65cb DB |
667 | }; |
668 | ||
669 | static int __init cls_bpf_init_mod(void) | |
670 | { | |
671 | return register_tcf_proto_ops(&cls_bpf_ops); | |
672 | } | |
673 | ||
674 | static void __exit cls_bpf_exit_mod(void) | |
675 | { | |
676 | unregister_tcf_proto_ops(&cls_bpf_ops); | |
677 | } | |
678 | ||
679 | module_init(cls_bpf_init_mod); | |
680 | module_exit(cls_bpf_exit_mod); |