blob: dc6a2d324bd8163841e7c9e6a0defebd87792ae5 [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;
30 char *bpf_name;
31 u32 bpf_fd;
32 u16 bpf_num_ops;
33};
34
35static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act,
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010036 struct tcf_result *res)
37{
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010038 struct tcf_bpf *prog = act->priv;
Daniel Borkmannced585c2015-03-17 20:25:57 +010039 int action, filter_res;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010040
Alexei Starovoitova1661512015-04-15 12:55:45 -070041 if (unlikely(!skb_mac_header_was_set(skb)))
42 return TC_ACT_UNSPEC;
43
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010044 spin_lock(&prog->tcf_lock);
Daniel Borkmannced585c2015-03-17 20:25:57 +010045
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010046 prog->tcf_tm.lastuse = jiffies;
47 bstats_update(&prog->tcf_bstats, skb);
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010048
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010049 /* Needed here for accessing maps. */
50 rcu_read_lock();
51 filter_res = BPF_PROG_RUN(prog->filter, skb);
52 rcu_read_unlock();
Daniel Borkmannced585c2015-03-17 20:25:57 +010053
54 /* A BPF program may overwrite the default action opcode.
55 * Similarly as in cls_bpf, if filter_res == -1 we use the
56 * default action specified from tc.
57 *
58 * In case a different well-known TC_ACT opcode has been
59 * returned, it will overwrite the default one.
60 *
61 * For everything else that is unkown, TC_ACT_UNSPEC is
62 * returned.
63 */
64 switch (filter_res) {
65 case TC_ACT_PIPE:
66 case TC_ACT_RECLASSIFY:
67 case TC_ACT_OK:
68 action = filter_res;
69 break;
70 case TC_ACT_SHOT:
71 action = filter_res;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010072 prog->tcf_qstats.drops++;
Daniel Borkmannced585c2015-03-17 20:25:57 +010073 break;
74 case TC_ACT_UNSPEC:
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010075 action = prog->tcf_action;
Daniel Borkmannced585c2015-03-17 20:25:57 +010076 break;
77 default:
78 action = TC_ACT_UNSPEC;
79 break;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010080 }
81
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010082 spin_unlock(&prog->tcf_lock);
Jiri Pirkod23b8ad2015-01-15 09:52:39 +010083 return action;
84}
85
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +010086static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
87{
88 return !prog->bpf_ops;
89}
90
91static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog,
92 struct sk_buff *skb)
93{
94 struct nlattr *nla;
95
96 if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops))
97 return -EMSGSIZE;
98
99 nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops *
100 sizeof(struct sock_filter));
101 if (nla == NULL)
102 return -EMSGSIZE;
103
104 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
105
106 return 0;
107}
108
109static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog,
110 struct sk_buff *skb)
111{
112 if (nla_put_u32(skb, TCA_ACT_BPF_FD, prog->bpf_fd))
113 return -EMSGSIZE;
114
115 if (prog->bpf_name &&
116 nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name))
117 return -EMSGSIZE;
118
119 return 0;
120}
121
122static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100123 int bind, int ref)
124{
125 unsigned char *tp = skb_tail_pointer(skb);
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100126 struct tcf_bpf *prog = act->priv;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100127 struct tc_act_bpf opt = {
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100128 .index = prog->tcf_index,
129 .refcnt = prog->tcf_refcnt - ref,
130 .bindcnt = prog->tcf_bindcnt - bind,
131 .action = prog->tcf_action,
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100132 };
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100133 struct tcf_t tm;
134 int ret;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100135
136 if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
137 goto nla_put_failure;
138
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100139 if (tcf_bpf_is_ebpf(prog))
140 ret = tcf_bpf_dump_ebpf_info(prog, skb);
141 else
142 ret = tcf_bpf_dump_bpf_info(prog, skb);
143 if (ret)
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100144 goto nla_put_failure;
145
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100146 tm.install = jiffies_to_clock_t(jiffies - prog->tcf_tm.install);
147 tm.lastuse = jiffies_to_clock_t(jiffies - prog->tcf_tm.lastuse);
148 tm.expires = jiffies_to_clock_t(prog->tcf_tm.expires);
149
150 if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm))
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100151 goto nla_put_failure;
152
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100153 return skb->len;
154
155nla_put_failure:
156 nlmsg_trim(skb, tp);
157 return -1;
158}
159
160static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
161 [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) },
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100162 [TCA_ACT_BPF_FD] = { .type = NLA_U32 },
163 [TCA_ACT_BPF_NAME] = { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN },
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100164 [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 },
165 [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY,
166 .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
167};
168
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100169static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100170{
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100171 struct sock_filter *bpf_ops;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100172 struct sock_fprog_kern fprog_tmp;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100173 struct bpf_prog *fp;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100174 u16 bpf_size, bpf_num_ops;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100175 int ret;
176
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100177 bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
178 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
179 return -EINVAL;
180
181 bpf_size = bpf_num_ops * sizeof(*bpf_ops);
Daniel Borkmannfd3e6462015-01-22 10:58:19 +0100182 if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
183 return -EINVAL;
184
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100185 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100186 if (bpf_ops == NULL)
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100187 return -ENOMEM;
188
189 memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
190
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100191 fprog_tmp.len = bpf_num_ops;
192 fprog_tmp.filter = bpf_ops;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100193
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100194 ret = bpf_prog_create(&fp, &fprog_tmp);
195 if (ret < 0) {
196 kfree(bpf_ops);
197 return ret;
198 }
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100199
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100200 cfg->bpf_ops = bpf_ops;
201 cfg->bpf_num_ops = bpf_num_ops;
202 cfg->filter = fp;
203
204 return 0;
205}
206
207static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
208{
209 struct bpf_prog *fp;
210 char *name = NULL;
211 u32 bpf_fd;
212
213 bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]);
214
215 fp = bpf_prog_get(bpf_fd);
216 if (IS_ERR(fp))
217 return PTR_ERR(fp);
218
219 if (fp->type != BPF_PROG_TYPE_SCHED_ACT) {
220 bpf_prog_put(fp);
221 return -EINVAL;
222 }
223
224 if (tb[TCA_ACT_BPF_NAME]) {
225 name = kmemdup(nla_data(tb[TCA_ACT_BPF_NAME]),
226 nla_len(tb[TCA_ACT_BPF_NAME]),
227 GFP_KERNEL);
228 if (!name) {
229 bpf_prog_put(fp);
230 return -ENOMEM;
231 }
232 }
233
234 cfg->bpf_fd = bpf_fd;
235 cfg->bpf_name = name;
236 cfg->filter = fp;
237
238 return 0;
239}
240
241static int tcf_bpf_init(struct net *net, struct nlattr *nla,
242 struct nlattr *est, struct tc_action *act,
243 int replace, int bind)
244{
245 struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
246 struct tc_act_bpf *parm;
247 struct tcf_bpf *prog;
248 struct tcf_bpf_cfg cfg;
249 bool is_bpf, is_ebpf;
250 int ret;
251
252 if (!nla)
253 return -EINVAL;
254
255 ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
256 if (ret < 0)
257 return ret;
258
259 is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
260 is_ebpf = tb[TCA_ACT_BPF_FD];
261
262 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) ||
263 !tb[TCA_ACT_BPF_PARMS])
264 return -EINVAL;
265
266 parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
267
268 memset(&cfg, 0, sizeof(cfg));
269
270 ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
271 tcf_bpf_init_from_efd(tb, &cfg);
272 if (ret < 0)
273 return ret;
274
275 if (!tcf_hash_check(parm->index, act, bind)) {
276 ret = tcf_hash_create(parm->index, est, act,
277 sizeof(*prog), bind);
278 if (ret < 0)
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100279 goto destroy_fp;
280
281 ret = ACT_P_CREATED;
282 } else {
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100283 /* Don't override defaults. */
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100284 if (bind)
285 goto destroy_fp;
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100286
287 tcf_hash_release(act, bind);
288 if (!replace) {
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100289 ret = -EEXIST;
290 goto destroy_fp;
291 }
292 }
293
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100294 prog = to_bpf(act);
295 spin_lock_bh(&prog->tcf_lock);
296
297 prog->bpf_ops = cfg.bpf_ops;
298 prog->bpf_name = cfg.bpf_name;
299
300 if (cfg.bpf_num_ops)
301 prog->bpf_num_ops = cfg.bpf_num_ops;
302 if (cfg.bpf_fd)
303 prog->bpf_fd = cfg.bpf_fd;
304
305 prog->tcf_action = parm->action;
306 prog->filter = cfg.filter;
307
308 spin_unlock_bh(&prog->tcf_lock);
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100309
310 if (ret == ACT_P_CREATED)
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100311 tcf_hash_insert(act);
312
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100313 return ret;
314
315destroy_fp:
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100316 if (is_ebpf)
317 bpf_prog_put(cfg.filter);
318 else
319 bpf_prog_destroy(cfg.filter);
320
321 kfree(cfg.bpf_ops);
322 kfree(cfg.bpf_name);
323
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100324 return ret;
325}
326
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100327static void tcf_bpf_cleanup(struct tc_action *act, int bind)
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100328{
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100329 const struct tcf_bpf *prog = act->priv;
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100330
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100331 if (tcf_bpf_is_ebpf(prog))
332 bpf_prog_put(prog->filter);
333 else
334 bpf_prog_destroy(prog->filter);
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100335}
336
Daniel Borkmanna8cb5f52015-03-20 15:11:12 +0100337static struct tc_action_ops act_bpf_ops __read_mostly = {
338 .kind = "bpf",
339 .type = TCA_ACT_BPF,
340 .owner = THIS_MODULE,
341 .act = tcf_bpf,
342 .dump = tcf_bpf_dump,
343 .cleanup = tcf_bpf_cleanup,
344 .init = tcf_bpf_init,
Jiri Pirkod23b8ad2015-01-15 09:52:39 +0100345};
346
347static int __init bpf_init_module(void)
348{
349 return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
350}
351
352static void __exit bpf_cleanup_module(void)
353{
354 tcf_unregister_action(&act_bpf_ops);
355}
356
357module_init(bpf_init_module);
358module_exit(bpf_cleanup_module);
359
360MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
361MODULE_DESCRIPTION("TC BPF based action");
362MODULE_LICENSE("GPL v2");