Debian Codegraph @

src/core/bpf-bind-iface.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-netlink.h"
4
5 #include "alloc-util.h"
6 #include "bpf-bind-iface.h"
7 #include "cgroup.h"
8 #include "fd-util.h"
9 #include "netlink-util.h"
10 #include "string-util.h"
11 #include "unit.h"
12
13 #if BPF_FRAMEWORK
14 /* libbpf, clang, llvm and bpftool compile time dependencies are satisfied */
15 #include "bpf-util.h"
16 #include "bpf-link.h"
17 #include "bind-iface-skel.h"
18
19 static struct bind_iface_bpf *bind_iface_bpf_free(struct bind_iface_bpf *obj) {
20 bind_iface_bpf__destroy(obj);
21 return NULL;
22 }
23
24 DEFINE_TRIVIAL_CLEANUP_FUNC(struct bind_iface_bpf *, bind_iface_bpf_free);
25
26 int bpf_bind_network_interface_supported(void) {
27 _cleanup_(bind_iface_bpf_freep) struct bind_iface_bpf *obj = NULL;
28 static int supported = -1;
29 int r;
30
31 if (supported >= 0)
32 return supported;
33
34 if (DLOPEN_BPF(LOG_WARNING, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED) < 0)
35 return (supported = false);
36
37 obj = bind_iface_bpf__open();
38 if (!obj) {
39 log_debug_errno(errno, "bind-interface: Failed to open BPF object: %m");
40 return (supported = false);
41 }
42
43 r = bind_iface_bpf__load(obj);
44 if (r != 0) {
45 log_debug_errno(r, "bind-interface: Failed to load BPF object: %m");
46 return (supported = false);
47 }
48
49 return (supported = bpf_can_link_program(obj->progs.sd_bind_interface));
50 }
51
52 static int bind_network_interface_install_impl(Unit *u, CGroupRuntime *crt) {
53 _cleanup_(bpf_link_freep) struct bpf_link *link = NULL;
54 _cleanup_(bind_iface_bpf_freep) struct bind_iface_bpf *obj = NULL;
55 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
56 _cleanup_free_ char *cgroup_path = NULL;
57 _cleanup_close_ int cgroup_fd = -EBADF;
58 int r, ifindex;
59
60 assert(u);
61 assert(crt);
62
63 CGroupContext *cc = ASSERT_PTR(unit_get_cgroup_context(u));
64
65 if (isempty(cc->bind_network_interface))
66 return 0;
67
68 r = cg_get_path(crt->cgroup_path, /* suffix= */ NULL, &cgroup_path);
69 if (r < 0)
70 return log_unit_error_errno(u, r, "bind-interface: Failed to get cgroup path: %m");
71
72 ifindex = rtnl_resolve_interface(&rtnl, cc->bind_network_interface);
73 if (ifindex < 0) {
74 log_unit_warning_errno(u, ifindex,
75 "bind-interface: Couldn't find index of network interface '%s', ignoring: %m",
76 cc->bind_network_interface);
77 return 0;
78 }
79 log_unit_debug(u, "bind-interface: Found index %d for network interface '%s'", ifindex, cc->bind_network_interface);
80
81 /* Open the BPF skeleton */
82 obj = bind_iface_bpf__open();
83 if (!obj)
84 return log_unit_error_errno(u, errno, "bind-interface: Failed to open BPF object: %m");
85
86 /* Set the VRF interface index in rodata before loading */
87 obj->rodata->ifindex = ifindex;
88
89 /* Load the BPF program */
90 r = bind_iface_bpf__load(obj);
91 if (r != 0)
92 return log_unit_error_errno(u, r, "bind-interface: Failed to load BPF object: %m");
93
94 /* Open the cgroup directory */
95 cgroup_fd = open(cgroup_path, O_PATH | O_CLOEXEC | O_DIRECTORY, 0);
96 if (cgroup_fd < 0)
97 return log_unit_error_errno(u, errno, "bind-interface: Failed to open cgroup directory '%s': %m", cgroup_path);
98
99 /* Attach the BPF program to the cgroup */
100 link = sym_bpf_program__attach_cgroup(obj->progs.sd_bind_interface, cgroup_fd);
101 r = bpf_get_error_translated(link);
102 if (r != 0)
103 return log_unit_error_errno(u, r, "bind-interface: Failed to create cgroup link: %m");
104
105 /* Store the link in CGroupRuntime */
106 crt->bpf_bind_network_interface_link = TAKE_PTR(link);
107
108 log_unit_debug(u, "bind-interface: Successfully installed VRF binding for interface '%s' (ifindex=%d)",
109 cc->bind_network_interface, ifindex);
110
111 return 0;
112 }
113
114 int bpf_bind_network_interface_install(Unit *u) {
115 CGroupRuntime *crt;
116 int r;
117
118 assert(u);
119
120 crt = unit_get_cgroup_runtime(u);
121 if (!crt)
122 return 0;
123
124 r = bind_network_interface_install_impl(u, crt);
125 crt->initial_bind_network_interface_link_fd = safe_close(crt->initial_bind_network_interface_link_fd);
126 return r;
127 }
128
129 int bpf_bind_network_interface_serialize(Unit *u, FILE *f, FDSet *fds) {
130 CGroupRuntime *crt;
131
132 assert(u);
133
134 crt = unit_get_cgroup_runtime(u);
135 if (!crt)
136 return 0;
137
138 return bpf_serialize_link(f, fds, "bind-iface-bpf-fd", crt->bpf_bind_network_interface_link);
139 }
140
141 #else /* ! BPF_FRAMEWORK */
142 int bpf_bind_network_interface_supported(void) {
143 return 0;
144 }
145
146 int bpf_bind_network_interface_install(Unit *u) {
147 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EOPNOTSUPP),
148 "bind-interface: Failed to install; BPF framework is not supported");
149 }
150
151 int bpf_bind_network_interface_serialize(Unit *u, FILE *f, FDSet *fds) {
152 return 0;
153 }
154 #endif