xref: /openbmc/linux/kernel/bpf/offload.c (revision 675fc275a3a2d905535207237402c6d8dcb5fa4b)
1a39e17b2SJakub Kicinski /*
2a39e17b2SJakub Kicinski  * Copyright (C) 2017 Netronome Systems, Inc.
3a39e17b2SJakub Kicinski  *
4a39e17b2SJakub Kicinski  * This software is licensed under the GNU General License Version 2,
5a39e17b2SJakub Kicinski  * June 1991 as shown in the file COPYING in the top-level directory of this
6a39e17b2SJakub Kicinski  * source tree.
7a39e17b2SJakub Kicinski  *
8a39e17b2SJakub Kicinski  * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
9a39e17b2SJakub Kicinski  * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
10a39e17b2SJakub Kicinski  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11a39e17b2SJakub Kicinski  * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
12a39e17b2SJakub Kicinski  * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
13a39e17b2SJakub Kicinski  * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
14a39e17b2SJakub Kicinski  */
15a39e17b2SJakub Kicinski 
16ab3f0063SJakub Kicinski #include <linux/bpf.h>
17ab3f0063SJakub Kicinski #include <linux/bpf_verifier.h>
18ab3f0063SJakub Kicinski #include <linux/bug.h>
19*675fc275SJakub Kicinski #include <linux/kdev_t.h>
20ab3f0063SJakub Kicinski #include <linux/list.h>
21ab3f0063SJakub Kicinski #include <linux/netdevice.h>
22ab3f0063SJakub Kicinski #include <linux/printk.h>
23*675fc275SJakub Kicinski #include <linux/proc_ns.h>
24ab3f0063SJakub Kicinski #include <linux/rtnetlink.h>
25e0d3974aSJakub Kicinski #include <linux/rwsem.h>
26ab3f0063SJakub Kicinski 
27e0d3974aSJakub Kicinski /* Protects bpf_prog_offload_devs and offload members of all progs.
28e0d3974aSJakub Kicinski  * RTNL lock cannot be taken when holding this lock.
29e0d3974aSJakub Kicinski  */
30e0d3974aSJakub Kicinski static DECLARE_RWSEM(bpf_devs_lock);
31ab3f0063SJakub Kicinski static LIST_HEAD(bpf_prog_offload_devs);
32ab3f0063SJakub Kicinski 
33ab3f0063SJakub Kicinski int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
34ab3f0063SJakub Kicinski {
35ab3f0063SJakub Kicinski 	struct bpf_dev_offload *offload;
36ab3f0063SJakub Kicinski 
37649f11dcSJakub Kicinski 	if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS &&
38649f11dcSJakub Kicinski 	    attr->prog_type != BPF_PROG_TYPE_XDP)
39649f11dcSJakub Kicinski 		return -EINVAL;
40ab3f0063SJakub Kicinski 
41ab3f0063SJakub Kicinski 	if (attr->prog_flags)
42ab3f0063SJakub Kicinski 		return -EINVAL;
43ab3f0063SJakub Kicinski 
44ab3f0063SJakub Kicinski 	offload = kzalloc(sizeof(*offload), GFP_USER);
45ab3f0063SJakub Kicinski 	if (!offload)
46ab3f0063SJakub Kicinski 		return -ENOMEM;
47ab3f0063SJakub Kicinski 
48ab3f0063SJakub Kicinski 	offload->prog = prog;
49ab3f0063SJakub Kicinski 
50e0d3974aSJakub Kicinski 	offload->netdev = dev_get_by_index(current->nsproxy->net_ns,
51e0d3974aSJakub Kicinski 					   attr->prog_ifindex);
52e0d3974aSJakub Kicinski 	if (!offload->netdev)
53e0d3974aSJakub Kicinski 		goto err_free;
54ab3f0063SJakub Kicinski 
55e0d3974aSJakub Kicinski 	down_write(&bpf_devs_lock);
56e0d3974aSJakub Kicinski 	if (offload->netdev->reg_state != NETREG_REGISTERED)
57e0d3974aSJakub Kicinski 		goto err_unlock;
58ab3f0063SJakub Kicinski 	prog->aux->offload = offload;
59ab3f0063SJakub Kicinski 	list_add_tail(&offload->offloads, &bpf_prog_offload_devs);
60e0d3974aSJakub Kicinski 	dev_put(offload->netdev);
61e0d3974aSJakub Kicinski 	up_write(&bpf_devs_lock);
62ab3f0063SJakub Kicinski 
63ab3f0063SJakub Kicinski 	return 0;
64e0d3974aSJakub Kicinski err_unlock:
65e0d3974aSJakub Kicinski 	up_write(&bpf_devs_lock);
66e0d3974aSJakub Kicinski 	dev_put(offload->netdev);
67e0d3974aSJakub Kicinski err_free:
68e0d3974aSJakub Kicinski 	kfree(offload);
69e0d3974aSJakub Kicinski 	return -EINVAL;
70ab3f0063SJakub Kicinski }
71ab3f0063SJakub Kicinski 
72ab3f0063SJakub Kicinski static int __bpf_offload_ndo(struct bpf_prog *prog, enum bpf_netdev_command cmd,
73ab3f0063SJakub Kicinski 			     struct netdev_bpf *data)
74ab3f0063SJakub Kicinski {
75ce3b9db4SJakub Kicinski 	struct bpf_dev_offload *offload = prog->aux->offload;
76ce3b9db4SJakub Kicinski 	struct net_device *netdev;
77ab3f0063SJakub Kicinski 
78ab3f0063SJakub Kicinski 	ASSERT_RTNL();
79ab3f0063SJakub Kicinski 
80ce3b9db4SJakub Kicinski 	if (!offload)
81ab3f0063SJakub Kicinski 		return -ENODEV;
82ce3b9db4SJakub Kicinski 	netdev = offload->netdev;
83ab3f0063SJakub Kicinski 	if (!netdev->netdev_ops->ndo_bpf)
84ab3f0063SJakub Kicinski 		return -EOPNOTSUPP;
85ab3f0063SJakub Kicinski 
86ab3f0063SJakub Kicinski 	data->command = cmd;
87ab3f0063SJakub Kicinski 
88ab3f0063SJakub Kicinski 	return netdev->netdev_ops->ndo_bpf(netdev, data);
89ab3f0063SJakub Kicinski }
90ab3f0063SJakub Kicinski 
91ab3f0063SJakub Kicinski int bpf_prog_offload_verifier_prep(struct bpf_verifier_env *env)
92ab3f0063SJakub Kicinski {
93ab3f0063SJakub Kicinski 	struct netdev_bpf data = {};
94ab3f0063SJakub Kicinski 	int err;
95ab3f0063SJakub Kicinski 
96ab3f0063SJakub Kicinski 	data.verifier.prog = env->prog;
97ab3f0063SJakub Kicinski 
98ab3f0063SJakub Kicinski 	rtnl_lock();
99ab3f0063SJakub Kicinski 	err = __bpf_offload_ndo(env->prog, BPF_OFFLOAD_VERIFIER_PREP, &data);
100ab3f0063SJakub Kicinski 	if (err)
101ab3f0063SJakub Kicinski 		goto exit_unlock;
102ab3f0063SJakub Kicinski 
103cae1927cSJakub Kicinski 	env->prog->aux->offload->dev_ops = data.verifier.ops;
104ab3f0063SJakub Kicinski 	env->prog->aux->offload->dev_state = true;
105ab3f0063SJakub Kicinski exit_unlock:
106ab3f0063SJakub Kicinski 	rtnl_unlock();
107ab3f0063SJakub Kicinski 	return err;
108ab3f0063SJakub Kicinski }
109ab3f0063SJakub Kicinski 
110cae1927cSJakub Kicinski int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
111cae1927cSJakub Kicinski 				 int insn_idx, int prev_insn_idx)
112cae1927cSJakub Kicinski {
113cae1927cSJakub Kicinski 	struct bpf_dev_offload *offload;
114cae1927cSJakub Kicinski 	int ret = -ENODEV;
115cae1927cSJakub Kicinski 
116cae1927cSJakub Kicinski 	down_read(&bpf_devs_lock);
117cae1927cSJakub Kicinski 	offload = env->prog->aux->offload;
118ce3b9db4SJakub Kicinski 	if (offload)
119cae1927cSJakub Kicinski 		ret = offload->dev_ops->insn_hook(env, insn_idx, prev_insn_idx);
120cae1927cSJakub Kicinski 	up_read(&bpf_devs_lock);
121cae1927cSJakub Kicinski 
122cae1927cSJakub Kicinski 	return ret;
123cae1927cSJakub Kicinski }
124cae1927cSJakub Kicinski 
125ab3f0063SJakub Kicinski static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
126ab3f0063SJakub Kicinski {
127ab3f0063SJakub Kicinski 	struct bpf_dev_offload *offload = prog->aux->offload;
128ab3f0063SJakub Kicinski 	struct netdev_bpf data = {};
129ab3f0063SJakub Kicinski 
130ab3f0063SJakub Kicinski 	data.offload.prog = prog;
131ab3f0063SJakub Kicinski 
132ab3f0063SJakub Kicinski 	if (offload->dev_state)
133ab3f0063SJakub Kicinski 		WARN_ON(__bpf_offload_ndo(prog, BPF_OFFLOAD_DESTROY, &data));
134ab3f0063SJakub Kicinski 
135ad8ad79fSJakub Kicinski 	/* Make sure BPF_PROG_GET_NEXT_ID can't find this dead program */
136ad8ad79fSJakub Kicinski 	bpf_prog_free_id(prog, true);
137ad8ad79fSJakub Kicinski 
138ab3f0063SJakub Kicinski 	list_del_init(&offload->offloads);
139ce3b9db4SJakub Kicinski 	kfree(offload);
140ce3b9db4SJakub Kicinski 	prog->aux->offload = NULL;
141ab3f0063SJakub Kicinski }
142ab3f0063SJakub Kicinski 
143ab3f0063SJakub Kicinski void bpf_prog_offload_destroy(struct bpf_prog *prog)
144ab3f0063SJakub Kicinski {
145ab3f0063SJakub Kicinski 	rtnl_lock();
146e0d3974aSJakub Kicinski 	down_write(&bpf_devs_lock);
147ce3b9db4SJakub Kicinski 	if (prog->aux->offload)
148ab3f0063SJakub Kicinski 		__bpf_prog_offload_destroy(prog);
149e0d3974aSJakub Kicinski 	up_write(&bpf_devs_lock);
150ab3f0063SJakub Kicinski 	rtnl_unlock();
151ab3f0063SJakub Kicinski }
152ab3f0063SJakub Kicinski 
153ab3f0063SJakub Kicinski static int bpf_prog_offload_translate(struct bpf_prog *prog)
154ab3f0063SJakub Kicinski {
155ab3f0063SJakub Kicinski 	struct netdev_bpf data = {};
156ab3f0063SJakub Kicinski 	int ret;
157ab3f0063SJakub Kicinski 
158ab3f0063SJakub Kicinski 	data.offload.prog = prog;
159ab3f0063SJakub Kicinski 
160ab3f0063SJakub Kicinski 	rtnl_lock();
161ab3f0063SJakub Kicinski 	ret = __bpf_offload_ndo(prog, BPF_OFFLOAD_TRANSLATE, &data);
162ab3f0063SJakub Kicinski 	rtnl_unlock();
163ab3f0063SJakub Kicinski 
164ab3f0063SJakub Kicinski 	return ret;
165ab3f0063SJakub Kicinski }
166ab3f0063SJakub Kicinski 
167ab3f0063SJakub Kicinski static unsigned int bpf_prog_warn_on_exec(const void *ctx,
168ab3f0063SJakub Kicinski 					  const struct bpf_insn *insn)
169ab3f0063SJakub Kicinski {
170ab3f0063SJakub Kicinski 	WARN(1, "attempt to execute device eBPF program on the host!");
171ab3f0063SJakub Kicinski 	return 0;
172ab3f0063SJakub Kicinski }
173ab3f0063SJakub Kicinski 
174ab3f0063SJakub Kicinski int bpf_prog_offload_compile(struct bpf_prog *prog)
175ab3f0063SJakub Kicinski {
176ab3f0063SJakub Kicinski 	prog->bpf_func = bpf_prog_warn_on_exec;
177ab3f0063SJakub Kicinski 
178ab3f0063SJakub Kicinski 	return bpf_prog_offload_translate(prog);
179ab3f0063SJakub Kicinski }
180ab3f0063SJakub Kicinski 
181*675fc275SJakub Kicinski struct ns_get_path_bpf_prog_args {
182*675fc275SJakub Kicinski 	struct bpf_prog *prog;
183*675fc275SJakub Kicinski 	struct bpf_prog_info *info;
184*675fc275SJakub Kicinski };
185*675fc275SJakub Kicinski 
186*675fc275SJakub Kicinski static struct ns_common *bpf_prog_offload_info_fill_ns(void *private_data)
187*675fc275SJakub Kicinski {
188*675fc275SJakub Kicinski 	struct ns_get_path_bpf_prog_args *args = private_data;
189*675fc275SJakub Kicinski 	struct bpf_prog_aux *aux = args->prog->aux;
190*675fc275SJakub Kicinski 	struct ns_common *ns;
191*675fc275SJakub Kicinski 	struct net *net;
192*675fc275SJakub Kicinski 
193*675fc275SJakub Kicinski 	rtnl_lock();
194*675fc275SJakub Kicinski 	down_read(&bpf_devs_lock);
195*675fc275SJakub Kicinski 
196*675fc275SJakub Kicinski 	if (aux->offload) {
197*675fc275SJakub Kicinski 		args->info->ifindex = aux->offload->netdev->ifindex;
198*675fc275SJakub Kicinski 		net = dev_net(aux->offload->netdev);
199*675fc275SJakub Kicinski 		get_net(net);
200*675fc275SJakub Kicinski 		ns = &net->ns;
201*675fc275SJakub Kicinski 	} else {
202*675fc275SJakub Kicinski 		args->info->ifindex = 0;
203*675fc275SJakub Kicinski 		ns = NULL;
204*675fc275SJakub Kicinski 	}
205*675fc275SJakub Kicinski 
206*675fc275SJakub Kicinski 	up_read(&bpf_devs_lock);
207*675fc275SJakub Kicinski 	rtnl_unlock();
208*675fc275SJakub Kicinski 
209*675fc275SJakub Kicinski 	return ns;
210*675fc275SJakub Kicinski }
211*675fc275SJakub Kicinski 
212*675fc275SJakub Kicinski int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
213*675fc275SJakub Kicinski 			       struct bpf_prog *prog)
214*675fc275SJakub Kicinski {
215*675fc275SJakub Kicinski 	struct ns_get_path_bpf_prog_args args = {
216*675fc275SJakub Kicinski 		.prog	= prog,
217*675fc275SJakub Kicinski 		.info	= info,
218*675fc275SJakub Kicinski 	};
219*675fc275SJakub Kicinski 	struct inode *ns_inode;
220*675fc275SJakub Kicinski 	struct path ns_path;
221*675fc275SJakub Kicinski 	void *res;
222*675fc275SJakub Kicinski 
223*675fc275SJakub Kicinski 	res = ns_get_path_cb(&ns_path, bpf_prog_offload_info_fill_ns, &args);
224*675fc275SJakub Kicinski 	if (IS_ERR(res)) {
225*675fc275SJakub Kicinski 		if (!info->ifindex)
226*675fc275SJakub Kicinski 			return -ENODEV;
227*675fc275SJakub Kicinski 		return PTR_ERR(res);
228*675fc275SJakub Kicinski 	}
229*675fc275SJakub Kicinski 
230*675fc275SJakub Kicinski 	ns_inode = ns_path.dentry->d_inode;
231*675fc275SJakub Kicinski 	info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev);
232*675fc275SJakub Kicinski 	info->netns_ino = ns_inode->i_ino;
233*675fc275SJakub Kicinski 	path_put(&ns_path);
234*675fc275SJakub Kicinski 
235*675fc275SJakub Kicinski 	return 0;
236*675fc275SJakub Kicinski }
237*675fc275SJakub Kicinski 
238ab3f0063SJakub Kicinski const struct bpf_prog_ops bpf_offload_prog_ops = {
239ab3f0063SJakub Kicinski };
240ab3f0063SJakub Kicinski 
241ab3f0063SJakub Kicinski static int bpf_offload_notification(struct notifier_block *notifier,
242ab3f0063SJakub Kicinski 				    ulong event, void *ptr)
243ab3f0063SJakub Kicinski {
244ab3f0063SJakub Kicinski 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
245ab3f0063SJakub Kicinski 	struct bpf_dev_offload *offload, *tmp;
246ab3f0063SJakub Kicinski 
247ab3f0063SJakub Kicinski 	ASSERT_RTNL();
248ab3f0063SJakub Kicinski 
249ab3f0063SJakub Kicinski 	switch (event) {
250ab3f0063SJakub Kicinski 	case NETDEV_UNREGISTER:
25162c71b45SJakub Kicinski 		/* ignore namespace changes */
25262c71b45SJakub Kicinski 		if (netdev->reg_state != NETREG_UNREGISTERING)
25362c71b45SJakub Kicinski 			break;
25462c71b45SJakub Kicinski 
255e0d3974aSJakub Kicinski 		down_write(&bpf_devs_lock);
256ab3f0063SJakub Kicinski 		list_for_each_entry_safe(offload, tmp, &bpf_prog_offload_devs,
257ab3f0063SJakub Kicinski 					 offloads) {
258ab3f0063SJakub Kicinski 			if (offload->netdev == netdev)
259ab3f0063SJakub Kicinski 				__bpf_prog_offload_destroy(offload->prog);
260ab3f0063SJakub Kicinski 		}
261e0d3974aSJakub Kicinski 		up_write(&bpf_devs_lock);
262ab3f0063SJakub Kicinski 		break;
263ab3f0063SJakub Kicinski 	default:
264ab3f0063SJakub Kicinski 		break;
265ab3f0063SJakub Kicinski 	}
266ab3f0063SJakub Kicinski 	return NOTIFY_OK;
267ab3f0063SJakub Kicinski }
268ab3f0063SJakub Kicinski 
269ab3f0063SJakub Kicinski static struct notifier_block bpf_offload_notifier = {
270ab3f0063SJakub Kicinski 	.notifier_call = bpf_offload_notification,
271ab3f0063SJakub Kicinski };
272ab3f0063SJakub Kicinski 
273ab3f0063SJakub Kicinski static int __init bpf_offload_init(void)
274ab3f0063SJakub Kicinski {
275ab3f0063SJakub Kicinski 	register_netdevice_notifier(&bpf_offload_notifier);
276ab3f0063SJakub Kicinski 	return 0;
277ab3f0063SJakub Kicinski }
278ab3f0063SJakub Kicinski 
279ab3f0063SJakub Kicinski subsys_initcall(bpf_offload_init);
280