-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsk_lookup_attach.c
72 lines (57 loc) · 1.75 KB
/
sk_lookup_attach.c
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
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright (c) 2020 Cloudflare */
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/bpf.h>
#include "syscall.h"
int main(int argc, char **argv)
{
const char *prog_path;
const char *link_path;
union bpf_attr attr;
int prog_fd, netns_fd, link_fd, err;
if (argc != 3) {
fprintf(stderr, "Usage: %s <prog path> <link path>\n", argv[0]);
exit(EXIT_SUCCESS);
}
prog_path = argv[1];
link_path = argv[2];
/* 1. Open the pinned BPF program */
memset(&attr, 0, sizeof(attr));
attr.pathname = (uint64_t) prog_path;
attr.file_flags = BPF_F_RDONLY;
prog_fd = bpf(BPF_OBJ_GET, &attr, sizeof(attr));
if (prog_fd == -1)
error(EXIT_FAILURE, errno, "bpf(OBJ_GET)");
/* 2. Get an FD for this process network namespace (netns) */
netns_fd = open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
if (netns_fd == -1)
error(EXIT_FAILURE, errno, "open");
/* 3. Attach BPF sk_lookup program to the (netns) with a BPF link */
memset(&attr, 0, sizeof(attr));
attr.link_create.prog_fd = prog_fd;
attr.link_create.target_fd = netns_fd;
attr.link_create.attach_type = BPF_SK_LOOKUP;
attr.link_create.flags = 0;
link_fd = bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
if (link_fd == -1)
error(EXIT_FAILURE, errno, "bpf(LINK_CREATE)");
/* 4. Pin the BPF link (otherwise would be destroyed on FD close) */
memset(&attr, 0, sizeof(attr));
attr.pathname = (uint64_t) link_path;
attr.bpf_fd = link_fd;
attr.file_flags = 0;
err = bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
if (err)
error(EXIT_FAILURE, errno, "bpf(OBJ_PIN)");
close(link_fd);
close(netns_fd);
close(prog_fd);
exit(EXIT_SUCCESS);
}