]> git.ipfire.org Git - people/arne_f/kernel.git/blame - net/sched/act_bpf.c
net: dsa: mv88e6xxx: call _mv88e6xxx_stats_wait with SMI lock held
[people/arne_f/kernel.git] / net / sched / act_bpf.c
CommitLineData
d23b8ad8
JP
1/*
2 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/skbuff.h>
14#include <linux/rtnetlink.h>
15#include <linux/filter.h>
a8cb5f55
DB
16#include <linux/bpf.h>
17
d23b8ad8
JP
18#include <net/netlink.h>
19#include <net/pkt_sched.h>
20
21#include <linux/tc_act/tc_bpf.h>
22#include <net/tc_act/tc_bpf.h>
23
a8cb5f55
DB
24#define BPF_TAB_MASK 15
25#define ACT_BPF_NAME_LEN 256
26
27struct tcf_bpf_cfg {
28 struct bpf_prog *filter;
29 struct sock_filter *bpf_ops;
f4eaed28 30 const char *bpf_name;
a8cb5f55
DB
31 u32 bpf_fd;
32 u16 bpf_num_ops;
f4eaed28 33 bool is_ebpf;
a8cb5f55 34};
d23b8ad8 35
a8cb5f55 36static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act,
d23b8ad8
JP
37 struct tcf_result *res)
38{
a8cb5f55 39 struct tcf_bpf *prog = act->priv;
ced585c8 40 int action, filter_res;
3431205e 41 bool at_ingress = G_TC_AT(skb->tc_verd) & AT_INGRESS;
d23b8ad8 42
a166151c
AS
43 if (unlikely(!skb_mac_header_was_set(skb)))
44 return TC_ACT_UNSPEC;
45
a8cb5f55 46 spin_lock(&prog->tcf_lock);
ced585c8 47
a8cb5f55
DB
48 prog->tcf_tm.lastuse = jiffies;
49 bstats_update(&prog->tcf_bstats, skb);
d23b8ad8 50
a8cb5f55
DB
51 /* Needed here for accessing maps. */
52 rcu_read_lock();
3431205e
AS
53 if (at_ingress) {
54 __skb_push(skb, skb->mac_len);
55 filter_res = BPF_PROG_RUN(prog->filter, skb);
56 __skb_pull(skb, skb->mac_len);
57 } else {
58 filter_res = BPF_PROG_RUN(prog->filter, skb);
59 }
a8cb5f55 60 rcu_read_unlock();
ced585c8
DB
61
62 /* A BPF program may overwrite the default action opcode.
63 * Similarly as in cls_bpf, if filter_res == -1 we use the
64 * default action specified from tc.
65 *
66 * In case a different well-known TC_ACT opcode has been
67 * returned, it will overwrite the default one.
68 *
69 * For everything else that is unkown, TC_ACT_UNSPEC is
70 * returned.
71 */
72 switch (filter_res) {
73 case TC_ACT_PIPE:
74 case TC_ACT_RECLASSIFY:
75 case TC_ACT_OK:
76 action = filter_res;
77 break;
78 case TC_ACT_SHOT:
79 action = filter_res;
a8cb5f55 80 prog->tcf_qstats.drops++;
ced585c8
DB
81 break;
82 case TC_ACT_UNSPEC:
a8cb5f55 83 action = prog->tcf_action;
ced585c8
DB
84 break;
85 default:
86 action = TC_ACT_UNSPEC;
87 break;
d23b8ad8
JP
88 }
89
a8cb5f55 90 spin_unlock(&prog->tcf_lock);
d23b8ad8
JP
91 return action;
92}
93
a8cb5f55
DB
94static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
95{
96 return !prog->bpf_ops;
97}
98
99static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog,
100 struct sk_buff *skb)
101{
102 struct nlattr *nla;
103
104 if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops))
105 return -EMSGSIZE;
106
107 nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops *
108 sizeof(struct sock_filter));
109 if (nla == NULL)
110 return -EMSGSIZE;
111
112 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
113
114 return 0;
115}
116
117static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog,
118 struct sk_buff *skb)
119{
120 if (nla_put_u32(skb, TCA_ACT_BPF_FD, prog->bpf_fd))
121 return -EMSGSIZE;
122
123 if (prog->bpf_name &&
124 nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name))
125 return -EMSGSIZE;
126
127 return 0;
128}
129
130static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
d23b8ad8
JP
131 int bind, int ref)
132{
133 unsigned char *tp = skb_tail_pointer(skb);
a8cb5f55 134 struct tcf_bpf *prog = act->priv;
d23b8ad8 135 struct tc_act_bpf opt = {
a8cb5f55
DB
136 .index = prog->tcf_index,
137 .refcnt = prog->tcf_refcnt - ref,
138 .bindcnt = prog->tcf_bindcnt - bind,
139 .action = prog->tcf_action,
d23b8ad8 140 };
a8cb5f55
DB
141 struct tcf_t tm;
142 int ret;
d23b8ad8
JP
143
144 if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
145 goto nla_put_failure;
146
a8cb5f55
DB
147 if (tcf_bpf_is_ebpf(prog))
148 ret = tcf_bpf_dump_ebpf_info(prog, skb);
149 else
150 ret = tcf_bpf_dump_bpf_info(prog, skb);
151 if (ret)
d23b8ad8
JP
152 goto nla_put_failure;
153
a8cb5f55
DB
154 tm.install = jiffies_to_clock_t(jiffies - prog->tcf_tm.install);
155 tm.lastuse = jiffies_to_clock_t(jiffies - prog->tcf_tm.lastuse);
156 tm.expires = jiffies_to_clock_t(prog->tcf_tm.expires);
d23b8ad8 157
a8cb5f55 158 if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm))
d23b8ad8 159 goto nla_put_failure;
a8cb5f55 160
d23b8ad8
JP
161 return skb->len;
162
163nla_put_failure:
164 nlmsg_trim(skb, tp);
165 return -1;
166}
167
168static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
169 [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) },
a8cb5f55
DB
170 [TCA_ACT_BPF_FD] = { .type = NLA_U32 },
171 [TCA_ACT_BPF_NAME] = { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN },
d23b8ad8
JP
172 [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 },
173 [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY,
174 .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
175};
176
a8cb5f55 177static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
d23b8ad8 178{
d23b8ad8 179 struct sock_filter *bpf_ops;
a8cb5f55 180 struct sock_fprog_kern fprog_tmp;
d23b8ad8 181 struct bpf_prog *fp;
a8cb5f55 182 u16 bpf_size, bpf_num_ops;
d23b8ad8
JP
183 int ret;
184
d23b8ad8
JP
185 bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
186 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
187 return -EINVAL;
188
189 bpf_size = bpf_num_ops * sizeof(*bpf_ops);
fd3e646c
DB
190 if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
191 return -EINVAL;
192
d23b8ad8 193 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
a8cb5f55 194 if (bpf_ops == NULL)
d23b8ad8
JP
195 return -ENOMEM;
196
197 memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
198
a8cb5f55
DB
199 fprog_tmp.len = bpf_num_ops;
200 fprog_tmp.filter = bpf_ops;
d23b8ad8 201
a8cb5f55
DB
202 ret = bpf_prog_create(&fp, &fprog_tmp);
203 if (ret < 0) {
204 kfree(bpf_ops);
205 return ret;
206 }
d23b8ad8 207
a8cb5f55
DB
208 cfg->bpf_ops = bpf_ops;
209 cfg->bpf_num_ops = bpf_num_ops;
210 cfg->filter = fp;
f4eaed28 211 cfg->is_ebpf = false;
a8cb5f55
DB
212
213 return 0;
214}
215
216static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
217{
218 struct bpf_prog *fp;
219 char *name = NULL;
220 u32 bpf_fd;
221
222 bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]);
223
224 fp = bpf_prog_get(bpf_fd);
225 if (IS_ERR(fp))
226 return PTR_ERR(fp);
227
228 if (fp->type != BPF_PROG_TYPE_SCHED_ACT) {
229 bpf_prog_put(fp);
230 return -EINVAL;
231 }
232
233 if (tb[TCA_ACT_BPF_NAME]) {
234 name = kmemdup(nla_data(tb[TCA_ACT_BPF_NAME]),
235 nla_len(tb[TCA_ACT_BPF_NAME]),
236 GFP_KERNEL);
237 if (!name) {
238 bpf_prog_put(fp);
239 return -ENOMEM;
240 }
241 }
242
243 cfg->bpf_fd = bpf_fd;
244 cfg->bpf_name = name;
245 cfg->filter = fp;
f4eaed28 246 cfg->is_ebpf = true;
a8cb5f55
DB
247
248 return 0;
249}
250
f4eaed28
DB
251static void tcf_bpf_cfg_cleanup(const struct tcf_bpf_cfg *cfg)
252{
253 if (cfg->is_ebpf)
254 bpf_prog_put(cfg->filter);
255 else
256 bpf_prog_destroy(cfg->filter);
257
258 kfree(cfg->bpf_ops);
259 kfree(cfg->bpf_name);
260}
261
262static void tcf_bpf_prog_fill_cfg(const struct tcf_bpf *prog,
263 struct tcf_bpf_cfg *cfg)
264{
265 cfg->is_ebpf = tcf_bpf_is_ebpf(prog);
266 cfg->filter = prog->filter;
267
268 cfg->bpf_ops = prog->bpf_ops;
269 cfg->bpf_name = prog->bpf_name;
270}
271
a8cb5f55
DB
272static int tcf_bpf_init(struct net *net, struct nlattr *nla,
273 struct nlattr *est, struct tc_action *act,
274 int replace, int bind)
275{
276 struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
f4eaed28 277 struct tcf_bpf_cfg cfg, old;
a8cb5f55
DB
278 struct tc_act_bpf *parm;
279 struct tcf_bpf *prog;
a8cb5f55
DB
280 bool is_bpf, is_ebpf;
281 int ret;
282
283 if (!nla)
284 return -EINVAL;
285
286 ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
287 if (ret < 0)
288 return ret;
289
290 is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
291 is_ebpf = tb[TCA_ACT_BPF_FD];
292
293 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) ||
294 !tb[TCA_ACT_BPF_PARMS])
295 return -EINVAL;
296
297 parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
298
299 memset(&cfg, 0, sizeof(cfg));
300
301 ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
302 tcf_bpf_init_from_efd(tb, &cfg);
303 if (ret < 0)
304 return ret;
305
306 if (!tcf_hash_check(parm->index, act, bind)) {
307 ret = tcf_hash_create(parm->index, est, act,
519c818e 308 sizeof(*prog), bind, false);
a8cb5f55 309 if (ret < 0)
d23b8ad8
JP
310 goto destroy_fp;
311
312 ret = ACT_P_CREATED;
313 } else {
a8cb5f55 314 /* Don't override defaults. */
d23b8ad8
JP
315 if (bind)
316 goto destroy_fp;
a8cb5f55
DB
317
318 tcf_hash_release(act, bind);
319 if (!replace) {
d23b8ad8
JP
320 ret = -EEXIST;
321 goto destroy_fp;
322 }
323 }
324
a8cb5f55
DB
325 prog = to_bpf(act);
326 spin_lock_bh(&prog->tcf_lock);
327
f4eaed28
DB
328 if (ret != ACT_P_CREATED)
329 tcf_bpf_prog_fill_cfg(prog, &old);
330
a8cb5f55
DB
331 prog->bpf_ops = cfg.bpf_ops;
332 prog->bpf_name = cfg.bpf_name;
333
334 if (cfg.bpf_num_ops)
335 prog->bpf_num_ops = cfg.bpf_num_ops;
336 if (cfg.bpf_fd)
337 prog->bpf_fd = cfg.bpf_fd;
338
339 prog->tcf_action = parm->action;
340 prog->filter = cfg.filter;
341
342 spin_unlock_bh(&prog->tcf_lock);
d23b8ad8
JP
343
344 if (ret == ACT_P_CREATED)
a8cb5f55 345 tcf_hash_insert(act);
f4eaed28
DB
346 else
347 tcf_bpf_cfg_cleanup(&old);
a8cb5f55 348
d23b8ad8
JP
349 return ret;
350
351destroy_fp:
f4eaed28 352 tcf_bpf_cfg_cleanup(&cfg);
d23b8ad8
JP
353 return ret;
354}
355
a8cb5f55 356static void tcf_bpf_cleanup(struct tc_action *act, int bind)
d23b8ad8 357{
f4eaed28 358 struct tcf_bpf_cfg tmp;
ddf06c1e 359
f4eaed28
DB
360 tcf_bpf_prog_fill_cfg(act->priv, &tmp);
361 tcf_bpf_cfg_cleanup(&tmp);
d23b8ad8
JP
362}
363
a8cb5f55
DB
364static struct tc_action_ops act_bpf_ops __read_mostly = {
365 .kind = "bpf",
366 .type = TCA_ACT_BPF,
367 .owner = THIS_MODULE,
368 .act = tcf_bpf,
369 .dump = tcf_bpf_dump,
370 .cleanup = tcf_bpf_cleanup,
371 .init = tcf_bpf_init,
d23b8ad8
JP
372};
373
374static int __init bpf_init_module(void)
375{
376 return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
377}
378
379static void __exit bpf_cleanup_module(void)
380{
381 tcf_unregister_action(&act_bpf_ops);
382}
383
384module_init(bpf_init_module);
385module_exit(bpf_cleanup_module);
386
387MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
388MODULE_DESCRIPTION("TC BPF based action");
389MODULE_LICENSE("GPL v2");