]>
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; |
332ae8e2 | 45 | bool offloaded; |
0d01d45f | 46 | u32 gen_flags; |
e2e9b654 | 47 | struct tcf_exts exts; |
7d1d65cb | 48 | u32 handle; |
55556dd5 | 49 | u16 bpf_num_ops; |
e2e9b654 DB |
50 | struct sock_filter *bpf_ops; |
51 | const char *bpf_name; | |
1f947bf1 JF |
52 | struct tcf_proto *tp; |
53 | struct rcu_head rcu; | |
7d1d65cb DB |
54 | }; |
55 | ||
56 | static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = { | |
57 | [TCA_BPF_CLASSID] = { .type = NLA_U32 }, | |
045efa82 | 58 | [TCA_BPF_FLAGS] = { .type = NLA_U32 }, |
0d01d45f | 59 | [TCA_BPF_FLAGS_GEN] = { .type = NLA_U32 }, |
e2e9b654 | 60 | [TCA_BPF_FD] = { .type = NLA_U32 }, |
5a7a5555 JHS |
61 | [TCA_BPF_NAME] = { .type = NLA_NUL_STRING, |
62 | .len = CLS_BPF_NAME_LEN }, | |
7d1d65cb DB |
63 | [TCA_BPF_OPS_LEN] = { .type = NLA_U16 }, |
64 | [TCA_BPF_OPS] = { .type = NLA_BINARY, | |
65 | .len = sizeof(struct sock_filter) * BPF_MAXINSNS }, | |
66 | }; | |
67 | ||
045efa82 DB |
68 | static int cls_bpf_exec_opcode(int code) |
69 | { | |
70 | switch (code) { | |
71 | case TC_ACT_OK: | |
045efa82 | 72 | case TC_ACT_SHOT: |
045efa82 | 73 | case TC_ACT_STOLEN: |
e25ea21f | 74 | case TC_ACT_TRAP: |
27b29f63 | 75 | case TC_ACT_REDIRECT: |
045efa82 DB |
76 | case TC_ACT_UNSPEC: |
77 | return code; | |
78 | default: | |
79 | return TC_ACT_UNSPEC; | |
80 | } | |
81 | } | |
82 | ||
7d1d65cb DB |
83 | static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
84 | struct tcf_result *res) | |
85 | { | |
80dcbd12 | 86 | struct cls_bpf_head *head = rcu_dereference_bh(tp->root); |
fdc5432a | 87 | bool at_ingress = skb_at_tc_ingress(skb); |
7d1d65cb | 88 | struct cls_bpf_prog *prog; |
54720df1 | 89 | int ret = -1; |
7d1d65cb | 90 | |
54720df1 DB |
91 | /* Needed here for accessing maps. */ |
92 | rcu_read_lock(); | |
1f947bf1 | 93 | list_for_each_entry_rcu(prog, &head->plist, link) { |
3431205e AS |
94 | int filter_res; |
95 | ||
045efa82 DB |
96 | qdisc_skb_cb(skb)->tc_classid = prog->res.classid; |
97 | ||
eadb4148 JK |
98 | if (tc_skip_sw(prog->gen_flags)) { |
99 | filter_res = prog->exts_integrated ? TC_ACT_UNSPEC : 0; | |
100 | } else if (at_ingress) { | |
3431205e AS |
101 | /* It is safe to push/pull even if skb_shared() */ |
102 | __skb_push(skb, skb->mac_len); | |
6aaae2b6 | 103 | bpf_compute_data_pointers(skb); |
3431205e AS |
104 | filter_res = BPF_PROG_RUN(prog->filter, skb); |
105 | __skb_pull(skb, skb->mac_len); | |
106 | } else { | |
6aaae2b6 | 107 | bpf_compute_data_pointers(skb); |
3431205e AS |
108 | filter_res = BPF_PROG_RUN(prog->filter, skb); |
109 | } | |
7d1d65cb | 110 | |
045efa82 | 111 | if (prog->exts_integrated) { |
3a461da1 DB |
112 | res->class = 0; |
113 | res->classid = TC_H_MAJ(prog->res.classid) | | |
114 | qdisc_skb_cb(skb)->tc_classid; | |
045efa82 DB |
115 | |
116 | ret = cls_bpf_exec_opcode(filter_res); | |
117 | if (ret == TC_ACT_UNSPEC) | |
118 | continue; | |
119 | break; | |
120 | } | |
121 | ||
7d1d65cb DB |
122 | if (filter_res == 0) |
123 | continue; | |
3a461da1 DB |
124 | if (filter_res != -1) { |
125 | res->class = 0; | |
7d1d65cb | 126 | res->classid = filter_res; |
3a461da1 DB |
127 | } else { |
128 | *res = prog->res; | |
129 | } | |
7d1d65cb DB |
130 | |
131 | ret = tcf_exts_exec(skb, &prog->exts, res); | |
132 | if (ret < 0) | |
133 | continue; | |
134 | ||
54720df1 | 135 | break; |
7d1d65cb | 136 | } |
54720df1 | 137 | rcu_read_unlock(); |
7d1d65cb | 138 | |
54720df1 | 139 | return ret; |
7d1d65cb DB |
140 | } |
141 | ||
e2e9b654 DB |
142 | static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog) |
143 | { | |
144 | return !prog->bpf_ops; | |
145 | } | |
146 | ||
332ae8e2 JK |
147 | static int cls_bpf_offload_cmd(struct tcf_proto *tp, struct cls_bpf_prog *prog, |
148 | enum tc_clsbpf_command cmd) | |
149 | { | |
150 | struct net_device *dev = tp->q->dev_queue->dev; | |
de4784ca | 151 | struct tc_cls_bpf_offload cls_bpf = {}; |
5cecb6cc | 152 | int err; |
332ae8e2 | 153 | |
de4784ca JP |
154 | tc_cls_common_offload_init(&cls_bpf.common, tp); |
155 | cls_bpf.command = cmd; | |
156 | cls_bpf.exts = &prog->exts; | |
157 | cls_bpf.prog = prog->filter; | |
158 | cls_bpf.name = prog->bpf_name; | |
159 | cls_bpf.exts_integrated = prog->exts_integrated; | |
160 | cls_bpf.gen_flags = prog->gen_flags; | |
332ae8e2 | 161 | |
de4784ca | 162 | err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSBPF, &cls_bpf); |
5cecb6cc OG |
163 | if (!err && (cmd == TC_CLSBPF_ADD || cmd == TC_CLSBPF_REPLACE)) |
164 | prog->gen_flags |= TCA_CLS_FLAGS_IN_HW; | |
165 | ||
166 | return err; | |
332ae8e2 JK |
167 | } |
168 | ||
eadb4148 JK |
169 | static int cls_bpf_offload(struct tcf_proto *tp, struct cls_bpf_prog *prog, |
170 | struct cls_bpf_prog *oldprog) | |
332ae8e2 JK |
171 | { |
172 | struct net_device *dev = tp->q->dev_queue->dev; | |
173 | struct cls_bpf_prog *obj = prog; | |
174 | enum tc_clsbpf_command cmd; | |
eadb4148 JK |
175 | bool skip_sw; |
176 | int ret; | |
177 | ||
178 | skip_sw = tc_skip_sw(prog->gen_flags) || | |
179 | (oldprog && tc_skip_sw(oldprog->gen_flags)); | |
332ae8e2 JK |
180 | |
181 | if (oldprog && oldprog->offloaded) { | |
7b06e8ae | 182 | if (tc_should_offload(dev, prog->gen_flags)) { |
332ae8e2 | 183 | cmd = TC_CLSBPF_REPLACE; |
eadb4148 | 184 | } else if (!tc_skip_sw(prog->gen_flags)) { |
332ae8e2 JK |
185 | obj = oldprog; |
186 | cmd = TC_CLSBPF_DESTROY; | |
eadb4148 JK |
187 | } else { |
188 | return -EINVAL; | |
332ae8e2 JK |
189 | } |
190 | } else { | |
7b06e8ae | 191 | if (!tc_should_offload(dev, prog->gen_flags)) |
eadb4148 | 192 | return skip_sw ? -EINVAL : 0; |
332ae8e2 JK |
193 | cmd = TC_CLSBPF_ADD; |
194 | } | |
195 | ||
eadb4148 JK |
196 | ret = cls_bpf_offload_cmd(tp, obj, cmd); |
197 | if (ret) | |
198 | return skip_sw ? ret : 0; | |
332ae8e2 JK |
199 | |
200 | obj->offloaded = true; | |
201 | if (oldprog) | |
202 | oldprog->offloaded = false; | |
eadb4148 JK |
203 | |
204 | return 0; | |
332ae8e2 JK |
205 | } |
206 | ||
207 | static void cls_bpf_stop_offload(struct tcf_proto *tp, | |
208 | struct cls_bpf_prog *prog) | |
209 | { | |
210 | int err; | |
211 | ||
212 | if (!prog->offloaded) | |
213 | return; | |
214 | ||
215 | err = cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_DESTROY); | |
216 | if (err) { | |
217 | pr_err("Stopping hardware offload failed: %d\n", err); | |
218 | return; | |
219 | } | |
220 | ||
221 | prog->offloaded = false; | |
222 | } | |
223 | ||
68d64063 JK |
224 | static void cls_bpf_offload_update_stats(struct tcf_proto *tp, |
225 | struct cls_bpf_prog *prog) | |
226 | { | |
227 | if (!prog->offloaded) | |
228 | return; | |
229 | ||
230 | cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_STATS); | |
231 | } | |
232 | ||
7d1d65cb DB |
233 | static int cls_bpf_init(struct tcf_proto *tp) |
234 | { | |
235 | struct cls_bpf_head *head; | |
236 | ||
237 | head = kzalloc(sizeof(*head), GFP_KERNEL); | |
238 | if (head == NULL) | |
239 | return -ENOBUFS; | |
240 | ||
1f947bf1 | 241 | INIT_LIST_HEAD_RCU(&head->plist); |
76cf546c | 242 | idr_init(&head->handle_idr); |
1f947bf1 | 243 | rcu_assign_pointer(tp->root, head); |
7d1d65cb DB |
244 | |
245 | return 0; | |
246 | } | |
247 | ||
8d829bdb | 248 | static void __cls_bpf_delete_prog(struct cls_bpf_prog *prog) |
7d1d65cb | 249 | { |
18d0264f | 250 | tcf_exts_destroy(&prog->exts); |
7d1d65cb | 251 | |
e2e9b654 DB |
252 | if (cls_bpf_is_ebpf(prog)) |
253 | bpf_prog_put(prog->filter); | |
254 | else | |
255 | bpf_prog_destroy(prog->filter); | |
7d1d65cb | 256 | |
e2e9b654 | 257 | kfree(prog->bpf_name); |
7d1d65cb DB |
258 | kfree(prog->bpf_ops); |
259 | kfree(prog); | |
260 | } | |
261 | ||
8d829bdb | 262 | static void cls_bpf_delete_prog_rcu(struct rcu_head *rcu) |
1f947bf1 | 263 | { |
8d829bdb | 264 | __cls_bpf_delete_prog(container_of(rcu, struct cls_bpf_prog, rcu)); |
1f947bf1 JF |
265 | } |
266 | ||
8d829bdb | 267 | static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog) |
7d1d65cb | 268 | { |
76cf546c CW |
269 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
270 | ||
271 | idr_remove_ext(&head->handle_idr, prog->handle); | |
332ae8e2 | 272 | cls_bpf_stop_offload(tp, prog); |
472f5837 JP |
273 | list_del_rcu(&prog->link); |
274 | tcf_unbind_filter(tp, &prog->res); | |
8d829bdb DB |
275 | call_rcu(&prog->rcu, cls_bpf_delete_prog_rcu); |
276 | } | |
e2e9b654 | 277 | |
8113c095 | 278 | static int cls_bpf_delete(struct tcf_proto *tp, void *arg, bool *last) |
8d829bdb | 279 | { |
763dbf63 WC |
280 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
281 | ||
8113c095 | 282 | __cls_bpf_delete(tp, arg); |
763dbf63 | 283 | *last = list_empty(&head->plist); |
472f5837 | 284 | return 0; |
7d1d65cb DB |
285 | } |
286 | ||
763dbf63 | 287 | static void cls_bpf_destroy(struct tcf_proto *tp) |
7d1d65cb | 288 | { |
1f947bf1 | 289 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
290 | struct cls_bpf_prog *prog, *tmp; |
291 | ||
8d829bdb DB |
292 | list_for_each_entry_safe(prog, tmp, &head->plist, link) |
293 | __cls_bpf_delete(tp, prog); | |
7d1d65cb | 294 | |
76cf546c | 295 | idr_destroy(&head->handle_idr); |
1f947bf1 | 296 | kfree_rcu(head, rcu); |
7d1d65cb DB |
297 | } |
298 | ||
8113c095 | 299 | static void *cls_bpf_get(struct tcf_proto *tp, u32 handle) |
7d1d65cb | 300 | { |
1f947bf1 | 301 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb | 302 | struct cls_bpf_prog *prog; |
7d1d65cb | 303 | |
3fe6b49e | 304 | list_for_each_entry(prog, &head->plist, link) { |
8113c095 WC |
305 | if (prog->handle == handle) |
306 | return prog; | |
7d1d65cb DB |
307 | } |
308 | ||
8113c095 | 309 | return NULL; |
7d1d65cb DB |
310 | } |
311 | ||
045efa82 | 312 | static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog) |
7d1d65cb | 313 | { |
1f947bf1 | 314 | struct sock_filter *bpf_ops; |
e2e9b654 | 315 | struct sock_fprog_kern fprog_tmp; |
1f947bf1 | 316 | struct bpf_prog *fp; |
33e9fcc6 | 317 | u16 bpf_size, bpf_num_ops; |
7d1d65cb DB |
318 | int ret; |
319 | ||
33e9fcc6 | 320 | bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]); |
e2e9b654 DB |
321 | if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) |
322 | return -EINVAL; | |
7d1d65cb | 323 | |
33e9fcc6 | 324 | bpf_size = bpf_num_ops * sizeof(*bpf_ops); |
e2e9b654 DB |
325 | if (bpf_size != nla_len(tb[TCA_BPF_OPS])) |
326 | return -EINVAL; | |
7913ecf6 | 327 | |
7d1d65cb | 328 | bpf_ops = kzalloc(bpf_size, GFP_KERNEL); |
e2e9b654 DB |
329 | if (bpf_ops == NULL) |
330 | return -ENOMEM; | |
7d1d65cb DB |
331 | |
332 | memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); | |
333 | ||
e2e9b654 DB |
334 | fprog_tmp.len = bpf_num_ops; |
335 | fprog_tmp.filter = bpf_ops; | |
7d1d65cb | 336 | |
e2e9b654 DB |
337 | ret = bpf_prog_create(&fp, &fprog_tmp); |
338 | if (ret < 0) { | |
339 | kfree(bpf_ops); | |
340 | return ret; | |
341 | } | |
7d1d65cb | 342 | |
7d1d65cb | 343 | prog->bpf_ops = bpf_ops; |
e2e9b654 DB |
344 | prog->bpf_num_ops = bpf_num_ops; |
345 | prog->bpf_name = NULL; | |
7d1d65cb | 346 | prog->filter = fp; |
7d1d65cb | 347 | |
e2e9b654 DB |
348 | return 0; |
349 | } | |
350 | ||
c46646d0 DB |
351 | static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog, |
352 | const struct tcf_proto *tp) | |
e2e9b654 DB |
353 | { |
354 | struct bpf_prog *fp; | |
355 | char *name = NULL; | |
356 | u32 bpf_fd; | |
357 | ||
358 | bpf_fd = nla_get_u32(tb[TCA_BPF_FD]); | |
359 | ||
113214be | 360 | fp = bpf_prog_get_type(bpf_fd, BPF_PROG_TYPE_SCHED_CLS); |
e2e9b654 DB |
361 | if (IS_ERR(fp)) |
362 | return PTR_ERR(fp); | |
363 | ||
e2e9b654 | 364 | if (tb[TCA_BPF_NAME]) { |
b15ca182 | 365 | name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL); |
e2e9b654 DB |
366 | if (!name) { |
367 | bpf_prog_put(fp); | |
368 | return -ENOMEM; | |
369 | } | |
370 | } | |
371 | ||
372 | prog->bpf_ops = NULL; | |
e2e9b654 | 373 | prog->bpf_name = name; |
e2e9b654 | 374 | prog->filter = fp; |
e2e9b654 | 375 | |
1f211a1b | 376 | if (fp->dst_needed && !(tp->q->flags & TCQ_F_INGRESS)) |
c46646d0 DB |
377 | netif_keep_dst(qdisc_dev(tp->q)); |
378 | ||
e2e9b654 DB |
379 | return 0; |
380 | } | |
381 | ||
6a725c48 JP |
382 | static int cls_bpf_set_parms(struct net *net, struct tcf_proto *tp, |
383 | struct cls_bpf_prog *prog, unsigned long base, | |
384 | struct nlattr **tb, struct nlattr *est, bool ovr) | |
e2e9b654 | 385 | { |
045efa82 | 386 | bool is_bpf, is_ebpf, have_exts = false; |
0d01d45f | 387 | u32 gen_flags = 0; |
e2e9b654 DB |
388 | int ret; |
389 | ||
390 | is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS]; | |
391 | is_ebpf = tb[TCA_BPF_FD]; | |
ef146fa4 | 392 | if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) |
e2e9b654 DB |
393 | return -EINVAL; |
394 | ||
6839da32 | 395 | ret = tcf_exts_validate(net, tp, tb, est, &prog->exts, ovr); |
e2e9b654 DB |
396 | if (ret < 0) |
397 | return ret; | |
398 | ||
045efa82 DB |
399 | if (tb[TCA_BPF_FLAGS]) { |
400 | u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]); | |
401 | ||
6839da32 JP |
402 | if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT) |
403 | return -EINVAL; | |
045efa82 DB |
404 | |
405 | have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT; | |
406 | } | |
0d01d45f JK |
407 | if (tb[TCA_BPF_FLAGS_GEN]) { |
408 | gen_flags = nla_get_u32(tb[TCA_BPF_FLAGS_GEN]); | |
409 | if (gen_flags & ~CLS_BPF_SUPPORTED_GEN_FLAGS || | |
6839da32 JP |
410 | !tc_flags_valid(gen_flags)) |
411 | return -EINVAL; | |
0d01d45f | 412 | } |
045efa82 | 413 | |
045efa82 | 414 | prog->exts_integrated = have_exts; |
0d01d45f | 415 | prog->gen_flags = gen_flags; |
e2e9b654 | 416 | |
045efa82 | 417 | ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) : |
c46646d0 | 418 | cls_bpf_prog_from_efd(tb, prog, tp); |
b9a24bb7 | 419 | if (ret < 0) |
6839da32 | 420 | return ret; |
e2e9b654 | 421 | |
ef146fa4 DB |
422 | if (tb[TCA_BPF_CLASSID]) { |
423 | prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]); | |
424 | tcf_bind_filter(tp, &prog->res, base); | |
425 | } | |
7d1d65cb | 426 | |
7d1d65cb | 427 | return 0; |
7d1d65cb DB |
428 | } |
429 | ||
7d1d65cb DB |
430 | static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, |
431 | struct tcf_proto *tp, unsigned long base, | |
432 | u32 handle, struct nlattr **tca, | |
8113c095 | 433 | void **arg, bool ovr) |
7d1d65cb | 434 | { |
1f947bf1 | 435 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
8113c095 | 436 | struct cls_bpf_prog *oldprog = *arg; |
7d1d65cb | 437 | struct nlattr *tb[TCA_BPF_MAX + 1]; |
1f947bf1 | 438 | struct cls_bpf_prog *prog; |
76cf546c | 439 | unsigned long idr_index; |
7d1d65cb DB |
440 | int ret; |
441 | ||
442 | if (tca[TCA_OPTIONS] == NULL) | |
443 | return -EINVAL; | |
444 | ||
fceb6435 JB |
445 | ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy, |
446 | NULL); | |
7d1d65cb DB |
447 | if (ret < 0) |
448 | return ret; | |
449 | ||
7d1d65cb | 450 | prog = kzalloc(sizeof(*prog), GFP_KERNEL); |
1f947bf1 | 451 | if (!prog) |
7d1d65cb DB |
452 | return -ENOBUFS; |
453 | ||
b9a24bb7 WC |
454 | ret = tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
455 | if (ret < 0) | |
456 | goto errout; | |
1f947bf1 JF |
457 | |
458 | if (oldprog) { | |
459 | if (handle && oldprog->handle != handle) { | |
460 | ret = -EINVAL; | |
461 | goto errout; | |
462 | } | |
463 | } | |
464 | ||
76cf546c CW |
465 | if (handle == 0) { |
466 | ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index, | |
467 | 1, 0x7FFFFFFF, GFP_KERNEL); | |
468 | if (ret) | |
469 | goto errout; | |
470 | prog->handle = idr_index; | |
471 | } else { | |
472 | if (!oldprog) { | |
473 | ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index, | |
474 | handle, handle + 1, GFP_KERNEL); | |
475 | if (ret) | |
476 | goto errout; | |
477 | } | |
7d1d65cb | 478 | prog->handle = handle; |
7d1d65cb DB |
479 | } |
480 | ||
6a725c48 | 481 | ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr); |
7d1d65cb | 482 | if (ret < 0) |
76cf546c | 483 | goto errout_idr; |
7d1d65cb | 484 | |
eadb4148 JK |
485 | ret = cls_bpf_offload(tp, prog, oldprog); |
486 | if (ret) { | |
76cf546c CW |
487 | if (!oldprog) |
488 | idr_remove_ext(&head->handle_idr, prog->handle); | |
8d829bdb | 489 | __cls_bpf_delete_prog(prog); |
eadb4148 JK |
490 | return ret; |
491 | } | |
332ae8e2 | 492 | |
5cecb6cc OG |
493 | if (!tc_in_hw(prog->gen_flags)) |
494 | prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW; | |
495 | ||
1f947bf1 | 496 | if (oldprog) { |
76cf546c | 497 | idr_replace_ext(&head->handle_idr, prog, handle); |
f6bfc46d | 498 | list_replace_rcu(&oldprog->link, &prog->link); |
18cdb37e | 499 | tcf_unbind_filter(tp, &oldprog->res); |
8d829bdb | 500 | call_rcu(&oldprog->rcu, cls_bpf_delete_prog_rcu); |
1f947bf1 JF |
501 | } else { |
502 | list_add_rcu(&prog->link, &head->plist); | |
503 | } | |
7d1d65cb | 504 | |
8113c095 | 505 | *arg = prog; |
7d1d65cb | 506 | return 0; |
b9a24bb7 | 507 | |
76cf546c CW |
508 | errout_idr: |
509 | if (!oldprog) | |
510 | idr_remove_ext(&head->handle_idr, prog->handle); | |
7d1d65cb | 511 | errout: |
b9a24bb7 | 512 | tcf_exts_destroy(&prog->exts); |
1f947bf1 | 513 | kfree(prog); |
7d1d65cb DB |
514 | return ret; |
515 | } | |
516 | ||
e2e9b654 DB |
517 | static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog, |
518 | struct sk_buff *skb) | |
519 | { | |
520 | struct nlattr *nla; | |
521 | ||
522 | if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops)) | |
523 | return -EMSGSIZE; | |
524 | ||
525 | nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops * | |
526 | sizeof(struct sock_filter)); | |
527 | if (nla == NULL) | |
528 | return -EMSGSIZE; | |
529 | ||
530 | memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); | |
531 | ||
532 | return 0; | |
533 | } | |
534 | ||
535 | static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog, | |
536 | struct sk_buff *skb) | |
537 | { | |
7bd509e3 DB |
538 | struct nlattr *nla; |
539 | ||
e2e9b654 DB |
540 | if (prog->bpf_name && |
541 | nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name)) | |
542 | return -EMSGSIZE; | |
543 | ||
e8628307 DB |
544 | if (nla_put_u32(skb, TCA_BPF_ID, prog->filter->aux->id)) |
545 | return -EMSGSIZE; | |
546 | ||
f1f7714e | 547 | nla = nla_reserve(skb, TCA_BPF_TAG, sizeof(prog->filter->tag)); |
7bd509e3 DB |
548 | if (nla == NULL) |
549 | return -EMSGSIZE; | |
550 | ||
f1f7714e | 551 | memcpy(nla_data(nla), prog->filter->tag, nla_len(nla)); |
7bd509e3 | 552 | |
e2e9b654 DB |
553 | return 0; |
554 | } | |
555 | ||
8113c095 | 556 | static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, void *fh, |
7d1d65cb DB |
557 | struct sk_buff *skb, struct tcmsg *tm) |
558 | { | |
8113c095 | 559 | struct cls_bpf_prog *prog = fh; |
e2e9b654 | 560 | struct nlattr *nest; |
bf007d1c | 561 | u32 bpf_flags = 0; |
e2e9b654 | 562 | int ret; |
7d1d65cb DB |
563 | |
564 | if (prog == NULL) | |
565 | return skb->len; | |
566 | ||
567 | tm->tcm_handle = prog->handle; | |
568 | ||
68d64063 JK |
569 | cls_bpf_offload_update_stats(tp, prog); |
570 | ||
7d1d65cb DB |
571 | nest = nla_nest_start(skb, TCA_OPTIONS); |
572 | if (nest == NULL) | |
573 | goto nla_put_failure; | |
574 | ||
ef146fa4 DB |
575 | if (prog->res.classid && |
576 | nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) | |
7d1d65cb | 577 | goto nla_put_failure; |
7d1d65cb | 578 | |
e2e9b654 DB |
579 | if (cls_bpf_is_ebpf(prog)) |
580 | ret = cls_bpf_dump_ebpf_info(prog, skb); | |
581 | else | |
582 | ret = cls_bpf_dump_bpf_info(prog, skb); | |
583 | if (ret) | |
7d1d65cb DB |
584 | goto nla_put_failure; |
585 | ||
5da57f42 | 586 | if (tcf_exts_dump(skb, &prog->exts) < 0) |
7d1d65cb DB |
587 | goto nla_put_failure; |
588 | ||
bf007d1c DB |
589 | if (prog->exts_integrated) |
590 | bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT; | |
591 | if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags)) | |
592 | goto nla_put_failure; | |
0d01d45f JK |
593 | if (prog->gen_flags && |
594 | nla_put_u32(skb, TCA_BPF_FLAGS_GEN, prog->gen_flags)) | |
595 | goto nla_put_failure; | |
bf007d1c | 596 | |
7d1d65cb DB |
597 | nla_nest_end(skb, nest); |
598 | ||
5da57f42 | 599 | if (tcf_exts_dump_stats(skb, &prog->exts) < 0) |
7d1d65cb DB |
600 | goto nla_put_failure; |
601 | ||
602 | return skb->len; | |
603 | ||
604 | nla_put_failure: | |
605 | nla_nest_cancel(skb, nest); | |
606 | return -1; | |
607 | } | |
608 | ||
07d79fc7 CW |
609 | static void cls_bpf_bind_class(void *fh, u32 classid, unsigned long cl) |
610 | { | |
611 | struct cls_bpf_prog *prog = fh; | |
612 | ||
613 | if (prog && prog->res.classid == classid) | |
614 | prog->res.class = cl; | |
615 | } | |
616 | ||
7d1d65cb DB |
617 | static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
618 | { | |
1f947bf1 | 619 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
620 | struct cls_bpf_prog *prog; |
621 | ||
3fe6b49e | 622 | list_for_each_entry(prog, &head->plist, link) { |
7d1d65cb DB |
623 | if (arg->count < arg->skip) |
624 | goto skip; | |
8113c095 | 625 | if (arg->fn(tp, prog, arg) < 0) { |
7d1d65cb DB |
626 | arg->stop = 1; |
627 | break; | |
628 | } | |
629 | skip: | |
630 | arg->count++; | |
631 | } | |
632 | } | |
633 | ||
634 | static struct tcf_proto_ops cls_bpf_ops __read_mostly = { | |
635 | .kind = "bpf", | |
636 | .owner = THIS_MODULE, | |
637 | .classify = cls_bpf_classify, | |
638 | .init = cls_bpf_init, | |
639 | .destroy = cls_bpf_destroy, | |
640 | .get = cls_bpf_get, | |
7d1d65cb DB |
641 | .change = cls_bpf_change, |
642 | .delete = cls_bpf_delete, | |
643 | .walk = cls_bpf_walk, | |
644 | .dump = cls_bpf_dump, | |
07d79fc7 | 645 | .bind_class = cls_bpf_bind_class, |
7d1d65cb DB |
646 | }; |
647 | ||
648 | static int __init cls_bpf_init_mod(void) | |
649 | { | |
650 | return register_tcf_proto_ops(&cls_bpf_ops); | |
651 | } | |
652 | ||
653 | static void __exit cls_bpf_exit_mod(void) | |
654 | { | |
655 | unregister_tcf_proto_ops(&cls_bpf_ops); | |
656 | } | |
657 | ||
658 | module_init(cls_bpf_init_mod); | |
659 | module_exit(cls_bpf_exit_mod); |