xref: /openbmc/linux/net/sched/act_bpf.c (revision 6189f1b0)
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>
16 #include <linux/bpf.h>
17 
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 
24 #define BPF_TAB_MASK		15
25 #define ACT_BPF_NAME_LEN	256
26 
27 struct tcf_bpf_cfg {
28 	struct bpf_prog *filter;
29 	struct sock_filter *bpf_ops;
30 	const char *bpf_name;
31 	u32 bpf_fd;
32 	u16 bpf_num_ops;
33 	bool is_ebpf;
34 };
35 
36 static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act,
37 		   struct tcf_result *res)
38 {
39 	struct tcf_bpf *prog = act->priv;
40 	int action, filter_res;
41 	bool at_ingress = G_TC_AT(skb->tc_verd) & AT_INGRESS;
42 
43 	if (unlikely(!skb_mac_header_was_set(skb)))
44 		return TC_ACT_UNSPEC;
45 
46 	spin_lock(&prog->tcf_lock);
47 
48 	prog->tcf_tm.lastuse = jiffies;
49 	bstats_update(&prog->tcf_bstats, skb);
50 
51 	/* Needed here for accessing maps. */
52 	rcu_read_lock();
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 	}
60 	rcu_read_unlock();
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;
80 		prog->tcf_qstats.drops++;
81 		break;
82 	case TC_ACT_UNSPEC:
83 		action = prog->tcf_action;
84 		break;
85 	default:
86 		action = TC_ACT_UNSPEC;
87 		break;
88 	}
89 
90 	spin_unlock(&prog->tcf_lock);
91 	return action;
92 }
93 
94 static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
95 {
96 	return !prog->bpf_ops;
97 }
98 
99 static 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 
117 static 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 
130 static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
131 			int bind, int ref)
132 {
133 	unsigned char *tp = skb_tail_pointer(skb);
134 	struct tcf_bpf *prog = act->priv;
135 	struct tc_act_bpf opt = {
136 		.index   = prog->tcf_index,
137 		.refcnt  = prog->tcf_refcnt - ref,
138 		.bindcnt = prog->tcf_bindcnt - bind,
139 		.action  = prog->tcf_action,
140 	};
141 	struct tcf_t tm;
142 	int ret;
143 
144 	if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
145 		goto nla_put_failure;
146 
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)
152 		goto nla_put_failure;
153 
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);
157 
158 	if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm))
159 		goto nla_put_failure;
160 
161 	return skb->len;
162 
163 nla_put_failure:
164 	nlmsg_trim(skb, tp);
165 	return -1;
166 }
167 
168 static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
169 	[TCA_ACT_BPF_PARMS]	= { .len = sizeof(struct tc_act_bpf) },
170 	[TCA_ACT_BPF_FD]	= { .type = NLA_U32 },
171 	[TCA_ACT_BPF_NAME]	= { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN },
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 
177 static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
178 {
179 	struct sock_filter *bpf_ops;
180 	struct sock_fprog_kern fprog_tmp;
181 	struct bpf_prog *fp;
182 	u16 bpf_size, bpf_num_ops;
183 	int ret;
184 
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);
190 	if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
191 		return -EINVAL;
192 
193 	bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
194 	if (bpf_ops == NULL)
195 		return -ENOMEM;
196 
197 	memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
198 
199 	fprog_tmp.len = bpf_num_ops;
200 	fprog_tmp.filter = bpf_ops;
201 
202 	ret = bpf_prog_create(&fp, &fprog_tmp);
203 	if (ret < 0) {
204 		kfree(bpf_ops);
205 		return ret;
206 	}
207 
208 	cfg->bpf_ops = bpf_ops;
209 	cfg->bpf_num_ops = bpf_num_ops;
210 	cfg->filter = fp;
211 	cfg->is_ebpf = false;
212 
213 	return 0;
214 }
215 
216 static 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;
246 	cfg->is_ebpf = true;
247 
248 	return 0;
249 }
250 
251 static 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 
262 static 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 
272 static 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];
277 	struct tcf_bpf_cfg cfg, old;
278 	struct tc_act_bpf *parm;
279 	struct tcf_bpf *prog;
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,
308 				      sizeof(*prog), bind);
309 		if (ret < 0)
310 			goto destroy_fp;
311 
312 		ret = ACT_P_CREATED;
313 	} else {
314 		/* Don't override defaults. */
315 		if (bind)
316 			goto destroy_fp;
317 
318 		tcf_hash_release(act, bind);
319 		if (!replace) {
320 			ret = -EEXIST;
321 			goto destroy_fp;
322 		}
323 	}
324 
325 	prog = to_bpf(act);
326 	spin_lock_bh(&prog->tcf_lock);
327 
328 	if (ret != ACT_P_CREATED)
329 		tcf_bpf_prog_fill_cfg(prog, &old);
330 
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);
343 
344 	if (ret == ACT_P_CREATED)
345 		tcf_hash_insert(act);
346 	else
347 		tcf_bpf_cfg_cleanup(&old);
348 
349 	return ret;
350 
351 destroy_fp:
352 	tcf_bpf_cfg_cleanup(&cfg);
353 	return ret;
354 }
355 
356 static void tcf_bpf_cleanup(struct tc_action *act, int bind)
357 {
358 	struct tcf_bpf_cfg tmp;
359 
360 	tcf_bpf_prog_fill_cfg(act->priv, &tmp);
361 	tcf_bpf_cfg_cleanup(&tmp);
362 }
363 
364 static 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,
372 };
373 
374 static int __init bpf_init_module(void)
375 {
376 	return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
377 }
378 
379 static void __exit bpf_cleanup_module(void)
380 {
381 	tcf_unregister_action(&act_bpf_ops);
382 }
383 
384 module_init(bpf_init_module);
385 module_exit(bpf_cleanup_module);
386 
387 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
388 MODULE_DESCRIPTION("TC BPF based action");
389 MODULE_LICENSE("GPL v2");
390