summaryrefslogtreecommitdiff
path: root/include/bpf_scm.h
blob: 669f053855fd31517b0ccfb489ebc2d78a536862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __BPF_SCM__
#define __BPF_SCM__

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "utils.h"
#include "bpf_elf.h"

#define BPF_SCM_AUX_VER		1
#define BPF_SCM_MAX_FDS		ELF_MAX_MAPS
#define BPF_SCM_MSG_SIZE	1024

struct bpf_elf_st {
	dev_t st_dev;
	ino_t st_ino;
};

struct bpf_map_aux {
	unsigned short uds_ver;
	unsigned short num_ent;
	char obj_name[64];
	struct bpf_elf_st obj_st;
	struct bpf_elf_map ent[BPF_SCM_MAX_FDS];
};

struct bpf_map_set_msg {
	struct msghdr hdr;
	struct iovec iov;
	char msg_buf[BPF_SCM_MSG_SIZE];
	struct bpf_map_aux aux;
};

static inline int *bpf_map_set_init(struct bpf_map_set_msg *msg,
				    struct sockaddr_un *addr,
				    unsigned int addr_len)
{
	const unsigned int cmsg_ctl_len = sizeof(int) * BPF_SCM_MAX_FDS;
	struct cmsghdr *cmsg;

	msg->iov.iov_base = &msg->aux;
	msg->iov.iov_len = sizeof(msg->aux);

	msg->hdr.msg_iov = &msg->iov;
	msg->hdr.msg_iovlen = 1;

	msg->hdr.msg_name = (struct sockaddr *)addr;
	msg->hdr.msg_namelen = addr_len;

	BUILD_BUG_ON(sizeof(msg->msg_buf) < cmsg_ctl_len);
	msg->hdr.msg_control = &msg->msg_buf;
	msg->hdr.msg_controllen	= cmsg_ctl_len;

	cmsg = CMSG_FIRSTHDR(&msg->hdr);
	cmsg->cmsg_len = msg->hdr.msg_controllen;
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type	= SCM_RIGHTS;

	return (int *)CMSG_DATA(cmsg);
}

static inline void bpf_map_set_init_single(struct bpf_map_set_msg *msg,
					   int num)
{
	struct cmsghdr *cmsg;

	msg->hdr.msg_controllen = CMSG_LEN(sizeof(int) * num);
	msg->iov.iov_len = offsetof(struct bpf_map_aux, ent) +
			   sizeof(struct bpf_elf_map) * num;

	cmsg = CMSG_FIRSTHDR(&msg->hdr);
	cmsg->cmsg_len = msg->hdr.msg_controllen;
}

#endif /* __BPF_SCM__ */