xref: /openbmc/linux/net/sched/act_bpf.c (revision 65ee8aeb)
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 	char *bpf_name;
31 	u32 bpf_fd;
32 	u16 bpf_num_ops;
33 };
34 
35 static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act,
36 		   struct tcf_result *res)
37 {
38 	struct tcf_bpf *prog = act->priv;
39 	int action, filter_res;
40 
41 	if (unlikely(!skb_mac_header_was_set(skb)))
42 		return TC_ACT_UNSPEC;
43 
44 	spin_lock(&prog->tcf_lock);
45 
46 	prog->tcf_tm.lastuse = jiffies;
47 	bstats_update(&prog->tcf_bstats, skb);
48 
49 	/* Needed here for accessing maps. */
50 	rcu_read_lock();
51 	filter_res = BPF_PROG_RUN(prog->filter, skb);
52 	rcu_read_unlock();
53 
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;
72 		prog->tcf_qstats.drops++;
73 		break;
74 	case TC_ACT_UNSPEC:
75 		action = prog->tcf_action;
76 		break;
77 	default:
78 		action = TC_ACT_UNSPEC;
79 		break;
80 	}
81 
82 	spin_unlock(&prog->tcf_lock);
83 	return action;
84 }
85 
86 static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
87 {
88 	return !prog->bpf_ops;
89 }
90 
91 static 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 
109 static 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 
122 static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
123 			int bind, int ref)
124 {
125 	unsigned char *tp = skb_tail_pointer(skb);
126 	struct tcf_bpf *prog = act->priv;
127 	struct tc_act_bpf opt = {
128 		.index   = prog->tcf_index,
129 		.refcnt  = prog->tcf_refcnt - ref,
130 		.bindcnt = prog->tcf_bindcnt - bind,
131 		.action  = prog->tcf_action,
132 	};
133 	struct tcf_t tm;
134 	int ret;
135 
136 	if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
137 		goto nla_put_failure;
138 
139 	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)
144 		goto nla_put_failure;
145 
146 	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))
151 		goto nla_put_failure;
152 
153 	return skb->len;
154 
155 nla_put_failure:
156 	nlmsg_trim(skb, tp);
157 	return -1;
158 }
159 
160 static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
161 	[TCA_ACT_BPF_PARMS]	= { .len = sizeof(struct tc_act_bpf) },
162 	[TCA_ACT_BPF_FD]	= { .type = NLA_U32 },
163 	[TCA_ACT_BPF_NAME]	= { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN },
164 	[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 
169 static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
170 {
171 	struct sock_filter *bpf_ops;
172 	struct sock_fprog_kern fprog_tmp;
173 	struct bpf_prog *fp;
174 	u16 bpf_size, bpf_num_ops;
175 	int ret;
176 
177 	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);
182 	if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
183 		return -EINVAL;
184 
185 	bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
186 	if (bpf_ops == NULL)
187 		return -ENOMEM;
188 
189 	memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
190 
191 	fprog_tmp.len = bpf_num_ops;
192 	fprog_tmp.filter = bpf_ops;
193 
194 	ret = bpf_prog_create(&fp, &fprog_tmp);
195 	if (ret < 0) {
196 		kfree(bpf_ops);
197 		return ret;
198 	}
199 
200 	cfg->bpf_ops = bpf_ops;
201 	cfg->bpf_num_ops = bpf_num_ops;
202 	cfg->filter = fp;
203 
204 	return 0;
205 }
206 
207 static 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 
241 static 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)
279 			goto destroy_fp;
280 
281 		ret = ACT_P_CREATED;
282 	} else {
283 		/* Don't override defaults. */
284 		if (bind)
285 			goto destroy_fp;
286 
287 		tcf_hash_release(act, bind);
288 		if (!replace) {
289 			ret = -EEXIST;
290 			goto destroy_fp;
291 		}
292 	}
293 
294 	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);
309 
310 	if (ret == ACT_P_CREATED)
311 		tcf_hash_insert(act);
312 
313 	return ret;
314 
315 destroy_fp:
316 	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 
324 	return ret;
325 }
326 
327 static void tcf_bpf_cleanup(struct tc_action *act, int bind)
328 {
329 	const struct tcf_bpf *prog = act->priv;
330 
331 	if (tcf_bpf_is_ebpf(prog))
332 		bpf_prog_put(prog->filter);
333 	else
334 		bpf_prog_destroy(prog->filter);
335 }
336 
337 static 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,
345 };
346 
347 static int __init bpf_init_module(void)
348 {
349 	return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
350 }
351 
352 static void __exit bpf_cleanup_module(void)
353 {
354 	tcf_unregister_action(&act_bpf_ops);
355 }
356 
357 module_init(bpf_init_module);
358 module_exit(bpf_cleanup_module);
359 
360 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
361 MODULE_DESCRIPTION("TC BPF based action");
362 MODULE_LICENSE("GPL v2");
363