• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __BPF_SCM__
2 #define __BPF_SCM__
3 
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 
7 #include "utils.h"
8 #include "bpf_elf.h"
9 
10 #define BPF_SCM_AUX_VER		1
11 #define BPF_SCM_MAX_FDS		ELF_MAX_MAPS
12 #define BPF_SCM_MSG_SIZE	1024
13 
14 struct bpf_elf_st {
15 	dev_t st_dev;
16 	ino_t st_ino;
17 };
18 
19 struct bpf_map_aux {
20 	unsigned short uds_ver;
21 	unsigned short num_ent;
22 	char obj_name[64];
23 	struct bpf_elf_st obj_st;
24 	struct bpf_elf_map ent[BPF_SCM_MAX_FDS];
25 };
26 
27 struct bpf_map_set_msg {
28 	struct msghdr hdr;
29 	struct iovec iov;
30 	char msg_buf[BPF_SCM_MSG_SIZE];
31 	struct bpf_map_aux aux;
32 };
33 
bpf_map_set_init(struct bpf_map_set_msg * msg,struct sockaddr_un * addr,unsigned int addr_len)34 static inline int *bpf_map_set_init(struct bpf_map_set_msg *msg,
35 				    struct sockaddr_un *addr,
36 				    unsigned int addr_len)
37 {
38 	const unsigned int cmsg_ctl_len = sizeof(int) * BPF_SCM_MAX_FDS;
39 	struct cmsghdr *cmsg;
40 
41 	msg->iov.iov_base = &msg->aux;
42 	msg->iov.iov_len = sizeof(msg->aux);
43 
44 	msg->hdr.msg_iov = &msg->iov;
45 	msg->hdr.msg_iovlen = 1;
46 
47 	msg->hdr.msg_name = (struct sockaddr *)addr;
48 	msg->hdr.msg_namelen = addr_len;
49 
50 	BUILD_BUG_ON(sizeof(msg->msg_buf) < cmsg_ctl_len);
51 	msg->hdr.msg_control = &msg->msg_buf;
52 	msg->hdr.msg_controllen	= cmsg_ctl_len;
53 
54 	cmsg = CMSG_FIRSTHDR(&msg->hdr);
55 	cmsg->cmsg_len = msg->hdr.msg_controllen;
56 	cmsg->cmsg_level = SOL_SOCKET;
57 	cmsg->cmsg_type	= SCM_RIGHTS;
58 
59 	return (int *)CMSG_DATA(cmsg);
60 }
61 
bpf_map_set_init_single(struct bpf_map_set_msg * msg,int num)62 static inline void bpf_map_set_init_single(struct bpf_map_set_msg *msg,
63 					   int num)
64 {
65 	struct cmsghdr *cmsg;
66 
67 	msg->hdr.msg_controllen = CMSG_LEN(sizeof(int) * num);
68 	msg->iov.iov_len = offsetof(struct bpf_map_aux, ent) +
69 			   sizeof(struct bpf_elf_map) * num;
70 
71 	cmsg = CMSG_FIRSTHDR(&msg->hdr);
72 	cmsg->cmsg_len = msg->hdr.msg_controllen;
73 }
74 
75 #endif /* __BPF_SCM__ */
76