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