blob: 458cf647e698ae46ec8f394fa6c94e0d77345d68 [file] [log] [blame]
Jiri Pirkod23b8ad2015-01-15 09:52:39 +01001/*
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>
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010016#include <linux/bpf.h>
17
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010018#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
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010024#define BPF_TAB_MASK 15
25#define ACT_BPF_NAME_LEN 256
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010026
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010027struct tcf_bpf_cfg {
28 struct bpf_prog *filter;
29 struct sock_filter *bpf_ops;
Daniel Borkmannf4eaed22015-07-29 18:40:56 +020030 const char *bpf_name;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010031 u32 bpf_fd;
32 u16 bpf_num_ops;
Daniel Borkmannf4eaed22015-07-29 18:40:56 +020033 bool is_ebpf;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010034};
35
36static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act,
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010037 struct tcf_result *res)
38{
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010039 struct tcf_bpf *prog = act->priv;
Daniel Borkmannced585c2015-03-17 20:25:57 +010040 int action, filter_res;
Alexei Starovoitov34312052015-06-04 10:11:53 -070041 bool at_ingress = G_TC_AT(skb->tc_verd) & AT_INGRESS;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010042
Alexei Starovoitova1661512015-04-15 12:55:45 -070043 if (unlikely(!skb_mac_header_was_set(skb)))
44 return TC_ACT_UNSPEC;
45
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010046 spin_lock(&prog->tcf_lock);
Daniel Borkmannced585c2015-03-17 20:25:57 +010047
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010048 prog->tcf_tm.lastuse = jiffies;
49 bstats_update(&prog->tcf_bstats, skb);
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010050
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010051 /* Needed here for accessing maps. */
52 rcu_read_lock();
Alexei Starovoitov34312052015-06-04 10:11:53 -070053 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 }
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010060 rcu_read_unlock();
Daniel Borkmannced585c2015-03-17 20:25:57 +010061
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;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010080 prog->tcf_qstats.drops++;
Daniel Borkmannced585c2015-03-17 20:25:57 +010081 break;
82 case TC_ACT_UNSPEC:
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010083 action = prog->tcf_action;
Daniel Borkmannced585c2015-03-17 20:25:57 +010084 break;
85 default:
86 action = TC_ACT_UNSPEC;
87 break;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010088 }
89
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010090 spin_unlock(&prog->tcf_lock);
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010091 return action;
92}
93
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010094static 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,
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100131 int bind, int ref)
132{
133 unsigned char *tp = skb_tail_pointer(skb);
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100134 struct tcf_bpf *prog = act->priv;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100135 struct tc_act_bpf opt = {
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100136 .index = prog->tcf_index,
137 .refcnt = prog->tcf_refcnt - ref,
138 .bindcnt = prog->tcf_bindcnt - bind,
139 .action = prog->tcf_action,
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100140 };
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100141 struct tcf_t tm;
142 int ret;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100143
144 if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
145 goto nla_put_failure;
146
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100147 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)
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100152 goto nla_put_failure;
153
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100154 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);
157
158 if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm))
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100159 goto nla_put_failure;
160
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100161 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) },
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100170 [TCA_ACT_BPF_FD] = { .type = NLA_U32 },
171 [TCA_ACT_BPF_NAME] = { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN },
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100172 [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
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100177static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100178{
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100179 struct sock_filter *bpf_ops;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100180 struct sock_fprog_kern fprog_tmp;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100181 struct bpf_prog *fp;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100182 u16 bpf_size, bpf_num_ops;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100183 int ret;
184
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100185 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);
Daniel Borkmannfd3e6462015-01-22 10:58:19 +0100190 if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
191 return -EINVAL;
192
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100193 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100194 if (bpf_ops == NULL)
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100195 return -ENOMEM;
196
197 memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
198
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100199 fprog_tmp.len = bpf_num_ops;
200 fprog_tmp.filter = bpf_ops;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100201
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100202 ret = bpf_prog_create(&fp, &fprog_tmp);
203 if (ret < 0) {
204 kfree(bpf_ops);
205 return ret;
206 }
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100207
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100208 cfg->bpf_ops = bpf_ops;
209 cfg->bpf_num_ops = bpf_num_ops;
210 cfg->filter = fp;
Daniel Borkmannf4eaed22015-07-29 18:40:56 +0200211 cfg->is_ebpf = false;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100212
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;
Daniel Borkmannf4eaed22015-07-29 18:40:56 +0200246 cfg->is_ebpf = true;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100247
248 return 0;
249}
250
Daniel Borkmannf4eaed22015-07-29 18:40:56 +0200251static 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
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100272static 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];
Daniel Borkmannf4eaed22015-07-29 18:40:56 +0200277 struct tcf_bpf_cfg cfg, old;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100278 struct tc_act_bpf *parm;
279 struct tcf_bpf *prog;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100280 bool is_bpf, is_ebpf;
Daniel Borkmanna5c90b22015-08-03 16:21:57 +0200281 int ret, res = 0;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100282
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
Daniel Borkmanna5c90b22015-08-03 16:21:57 +0200290 if (!tb[TCA_ACT_BPF_PARMS])
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100291 return -EINVAL;
292
293 parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
294
Daniel Borkmanna5c90b22015-08-03 16:21:57 +0200295 if (!tcf_hash_check(parm->index, act, bind)) {
296 ret = tcf_hash_create(parm->index, est, act,
297 sizeof(*prog), bind, false);
298 if (ret < 0)
299 return ret;
300
301 res = ACT_P_CREATED;
302 } else {
303 /* Don't override defaults. */
304 if (bind)
305 return 0;
306
307 tcf_hash_release(act, bind);
308 if (!replace)
309 return -EEXIST;
310 }
311
312 is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
313 is_ebpf = tb[TCA_ACT_BPF_FD];
314
315 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) {
316 ret = -EINVAL;
317 goto out;
318 }
319
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100320 memset(&cfg, 0, sizeof(cfg));
321
322 ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
323 tcf_bpf_init_from_efd(tb, &cfg);
324 if (ret < 0)
Daniel Borkmanna5c90b22015-08-03 16:21:57 +0200325 goto out;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100326
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100327 prog = to_bpf(act);
328 spin_lock_bh(&prog->tcf_lock);
329
Alexei Starovoitovfaa54be2015-08-25 20:06:32 -0700330 if (res != ACT_P_CREATED)
Daniel Borkmannf4eaed22015-07-29 18:40:56 +0200331 tcf_bpf_prog_fill_cfg(prog, &old);
332
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100333 prog->bpf_ops = cfg.bpf_ops;
334 prog->bpf_name = cfg.bpf_name;
335
336 if (cfg.bpf_num_ops)
337 prog->bpf_num_ops = cfg.bpf_num_ops;
338 if (cfg.bpf_fd)
339 prog->bpf_fd = cfg.bpf_fd;
340
341 prog->tcf_action = parm->action;
342 prog->filter = cfg.filter;
343
344 spin_unlock_bh(&prog->tcf_lock);
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100345
Daniel Borkmanna5c90b22015-08-03 16:21:57 +0200346 if (res == ACT_P_CREATED)
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100347 tcf_hash_insert(act);
Daniel Borkmannf4eaed22015-07-29 18:40:56 +0200348 else
349 tcf_bpf_cfg_cleanup(&old);
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100350
Daniel Borkmanna5c90b22015-08-03 16:21:57 +0200351 return res;
352out:
353 if (res == ACT_P_CREATED)
354 tcf_hash_cleanup(act, est);
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100355
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100356 return ret;
357}
358
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100359static void tcf_bpf_cleanup(struct tc_action *act, int bind)
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100360{
Daniel Borkmannf4eaed22015-07-29 18:40:56 +0200361 struct tcf_bpf_cfg tmp;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100362
Daniel Borkmannf4eaed22015-07-29 18:40:56 +0200363 tcf_bpf_prog_fill_cfg(act->priv, &tmp);
364 tcf_bpf_cfg_cleanup(&tmp);
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100365}
366
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100367static struct tc_action_ops act_bpf_ops __read_mostly = {
368 .kind = "bpf",
369 .type = TCA_ACT_BPF,
370 .owner = THIS_MODULE,
371 .act = tcf_bpf,
372 .dump = tcf_bpf_dump,
373 .cleanup = tcf_bpf_cleanup,
374 .init = tcf_bpf_init,
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100375};
376
377static int __init bpf_init_module(void)
378{
379 return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
380}
381
382static void __exit bpf_cleanup_module(void)
383{
384 tcf_unregister_action(&act_bpf_ops);
385}
386
387module_init(bpf_init_module);
388module_exit(bpf_cleanup_module);
389
390MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
391MODULE_DESCRIPTION("TC BPF based action");
392MODULE_LICENSE("GPL v2");