net/bpf/bpf_dummy_struct_ops.c

Source file repositories/reference/linux-study-clean/net/bpf/bpf_dummy_struct_ops.c

File Facts

System
Linux kernel
Corpus path
net/bpf/bpf_dummy_struct_ops.c
Extension
.c
Size
7976 bytes
Lines
324
Domain
Networking Core
Bucket
Sockets, Protocols, Packet Path, And Network Policy
Inferred role
Networking Core: implementation source
Status
source implementation candidate

Why This File Exists

Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.

Dependency Surface

Detected Declarations

Annotated Snippet

struct bpf_dummy_ops_test_args {
	u64 args[MAX_BPF_FUNC_ARGS];
	struct bpf_dummy_ops_state state;
};

static struct btf *bpf_dummy_ops_btf;

static struct bpf_dummy_ops_test_args *
dummy_ops_init_args(const union bpf_attr *kattr, unsigned int nr)
{
	__u32 size_in;
	struct bpf_dummy_ops_test_args *args;
	void __user *ctx_in;
	void __user *u_state;

	size_in = kattr->test.ctx_size_in;
	if (size_in != sizeof(u64) * nr)
		return ERR_PTR(-EINVAL);

	args = kzalloc_obj(*args);
	if (!args)
		return ERR_PTR(-ENOMEM);

	ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
	if (copy_from_user(args->args, ctx_in, size_in))
		goto out;

	/* args[0] is 0 means state argument of test_N will be NULL */
	u_state = u64_to_user_ptr(args->args[0]);
	if (u_state && copy_from_user(&args->state, u_state,
				      sizeof(args->state)))
		goto out;

	return args;
out:
	kfree(args);
	return ERR_PTR(-EFAULT);
}

static int dummy_ops_copy_args(struct bpf_dummy_ops_test_args *args)
{
	void __user *u_state;

	u_state = u64_to_user_ptr(args->args[0]);
	if (u_state && copy_to_user(u_state, &args->state, sizeof(args->state)))
		return -EFAULT;

	return 0;
}

static int dummy_ops_call_op(void *image, struct bpf_dummy_ops_test_args *args)
{
	dummy_ops_test_ret_fn test = (void *)image + cfi_get_offset();
	struct bpf_dummy_ops_state *state = NULL;

	/* state needs to be NULL if args[0] is 0 */
	if (args->args[0])
		state = &args->state;
	return test(state, args->args[1], args->args[2],
		    args->args[3], args->args[4]);
}

static const struct bpf_ctx_arg_aux *find_ctx_arg_info(struct bpf_prog_aux *aux, int offset)
{
	int i;

	for (i = 0; i < aux->ctx_arg_info_size; i++)
		if (aux->ctx_arg_info[i].offset == offset)
			return &aux->ctx_arg_info[i];

	return NULL;
}

/* There is only one check at the moment:
 * - zero should not be passed for pointer parameters not marked as nullable.
 */
static int check_test_run_args(struct bpf_prog *prog, struct bpf_dummy_ops_test_args *args)
{
	const struct btf_type *func_proto = prog->aux->attach_func_proto;

	for (u32 arg_no = 0; arg_no < btf_type_vlen(func_proto) ; ++arg_no) {
		const struct btf_param *param = &btf_params(func_proto)[arg_no];
		const struct bpf_ctx_arg_aux *info;
		const struct btf_type *t;
		int offset;

		if (args->args[arg_no] != 0)
			continue;

		/* Program is validated already, so there is no need

Annotation

Implementation Notes