1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include <linux/bpf_insn.h>
8 #include <net/ethernet.h>
10 #include <netinet/ip.h>
11 #include <netinet/ip6.h>
17 #include "alloc-util.h"
18 #include "bpf-firewall.h"
19 #include "bpf-program.h"
21 #include "ip-address-access.h"
22 #include "memory-util.h"
23 #include "missing_syscall.h"
38 /* Compile instructions for one list of addresses, one direction and one specific verdict on matches. */
40 static int add_lookup_instructions(
47 int r
, addr_offset
, addr_size
;
55 addr_size
= sizeof(uint32_t);
56 addr_offset
= is_ingress
?
57 offsetof(struct iphdr
, saddr
) :
58 offsetof(struct iphdr
, daddr
);
62 addr_size
= 4 * sizeof(uint32_t);
63 addr_offset
= is_ingress
?
64 offsetof(struct ip6_hdr
, ip6_src
.s6_addr
) :
65 offsetof(struct ip6_hdr
, ip6_dst
.s6_addr
);
73 /* Compare IPv4 with one word instruction (32bit) */
74 struct bpf_insn insn
[] = {
75 /* If skb->protocol != ETH_P_IP, skip this whole block. The offset will be set later. */
76 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, htobe16(protocol
), 0),
79 * Call into BPF_FUNC_skb_load_bytes to load the dst/src IP address
81 * R1: Pointer to the skb
83 * R3: Destination buffer on the stack (r10 - 4)
84 * R4: Number of bytes to read (4)
87 BPF_MOV64_REG(BPF_REG_1
, BPF_REG_6
),
88 BPF_MOV32_IMM(BPF_REG_2
, addr_offset
),
90 BPF_MOV64_REG(BPF_REG_3
, BPF_REG_10
),
91 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_3
, -addr_size
),
93 BPF_MOV32_IMM(BPF_REG_4
, addr_size
),
94 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_skb_load_bytes
),
97 * Call into BPF_FUNC_map_lookup_elem to see if the address matches any entry in the
98 * LPM trie map. For this to work, the prefixlen field of 'struct bpf_lpm_trie_key'
99 * has to be set to the maximum possible value.
101 * On success, the looked up value is stored in R0. For this application, the actual
102 * value doesn't matter, however; we just set the bit in @verdict in R8 if we found any
106 BPF_LD_MAP_FD(BPF_REG_1
, map_fd
),
107 BPF_MOV64_REG(BPF_REG_2
, BPF_REG_10
),
108 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, -addr_size
- sizeof(uint32_t)),
109 BPF_ST_MEM(BPF_W
, BPF_REG_2
, 0, addr_size
* 8),
111 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_map_lookup_elem
),
112 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 1),
113 BPF_ALU32_IMM(BPF_OR
, BPF_REG_8
, verdict
),
116 /* Jump label fixup */
117 insn
[0].off
= ELEMENTSOF(insn
) - 1;
119 r
= bpf_program_add_instructions(p
, insn
, ELEMENTSOF(insn
));
128 static int add_instructions_for_ip_any(
135 const struct bpf_insn insn
[] = {
136 BPF_ALU32_IMM(BPF_OR
, BPF_REG_8
, verdict
),
139 r
= bpf_program_add_instructions(p
, insn
, 1);
146 static int bpf_firewall_compile_bpf(
153 const struct bpf_insn pre_insn
[] = {
155 * When the eBPF program is entered, R1 contains the address of the skb.
156 * However, R1-R5 are scratch registers that are not preserved when calling
157 * into kernel functions, so we need to save anything that's supposed to
158 * stay around to R6-R9. Save the skb to R6.
160 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
163 * Although we cannot access the skb data directly from eBPF programs used in this
164 * scenario, the kernel has prepared some fields for us to access through struct __sk_buff.
165 * Load the protocol (IPv4, IPv6) used by the packet in flight once and cache it in R7
168 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
, offsetof(struct __sk_buff
, protocol
)),
171 * R8 is used to keep track of whether any address check has explicitly allowed or denied the packet
172 * through ACCESS_DENIED or ACCESS_ALLOWED bits. Reset them both to 0 in the beginning.
174 BPF_MOV32_IMM(BPF_REG_8
, 0),
178 * The access checkers compiled for the configured allowance and denial lists
179 * write to R8 at runtime. The following code prepares for an early exit that
180 * skip the accounting if the packet is denied.
183 * if (R8 == ACCESS_DENIED)
186 * This means that if both ACCESS_DENIED and ACCESS_ALLOWED are set, the packet
187 * is allowed to pass.
189 const struct bpf_insn post_insn
[] = {
190 BPF_MOV64_IMM(BPF_REG_0
, 1),
191 BPF_JMP_IMM(BPF_JNE
, BPF_REG_8
, ACCESS_DENIED
, 1),
192 BPF_MOV64_IMM(BPF_REG_0
, 0),
195 _cleanup_(bpf_program_unrefp
) BPFProgram
*p
= NULL
;
196 int accounting_map_fd
, r
;
202 accounting_map_fd
= is_ingress
?
203 u
->ip_accounting_ingress_map_fd
:
204 u
->ip_accounting_egress_map_fd
;
207 u
->ipv4_allow_map_fd
>= 0 ||
208 u
->ipv6_allow_map_fd
>= 0 ||
209 u
->ipv4_deny_map_fd
>= 0 ||
210 u
->ipv6_deny_map_fd
>= 0 ||
214 if (accounting_map_fd
< 0 && !access_enabled
) {
219 r
= bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB
, &p
);
223 r
= bpf_program_add_instructions(p
, pre_insn
, ELEMENTSOF(pre_insn
));
227 if (access_enabled
) {
229 * The simple rule this function translates into eBPF instructions is:
231 * - Access will be granted when an address matches an entry in @list_allow
232 * - Otherwise, access will be denied when an address matches an entry in @list_deny
233 * - Otherwise, access will be granted
236 if (u
->ipv4_deny_map_fd
>= 0) {
237 r
= add_lookup_instructions(p
, u
->ipv4_deny_map_fd
, ETH_P_IP
, is_ingress
, ACCESS_DENIED
);
242 if (u
->ipv6_deny_map_fd
>= 0) {
243 r
= add_lookup_instructions(p
, u
->ipv6_deny_map_fd
, ETH_P_IPV6
, is_ingress
, ACCESS_DENIED
);
248 if (u
->ipv4_allow_map_fd
>= 0) {
249 r
= add_lookup_instructions(p
, u
->ipv4_allow_map_fd
, ETH_P_IP
, is_ingress
, ACCESS_ALLOWED
);
254 if (u
->ipv6_allow_map_fd
>= 0) {
255 r
= add_lookup_instructions(p
, u
->ipv6_allow_map_fd
, ETH_P_IPV6
, is_ingress
, ACCESS_ALLOWED
);
261 r
= add_instructions_for_ip_any(p
, ACCESS_ALLOWED
);
267 r
= add_instructions_for_ip_any(p
, ACCESS_DENIED
);
273 r
= bpf_program_add_instructions(p
, post_insn
, ELEMENTSOF(post_insn
));
277 if (accounting_map_fd
>= 0) {
278 struct bpf_insn insn
[] = {
280 * If R0 == 0, the packet will be denied; skip the accounting instructions in this case.
281 * The jump label will be fixed up later.
283 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 0),
286 BPF_MOV64_IMM(BPF_REG_0
, MAP_KEY_PACKETS
), /* r0 = 0 */
287 BPF_STX_MEM(BPF_W
, BPF_REG_10
, BPF_REG_0
, -4), /* *(u32 *)(fp - 4) = r0 */
288 BPF_MOV64_REG(BPF_REG_2
, BPF_REG_10
),
289 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, -4), /* r2 = fp - 4 */
290 BPF_LD_MAP_FD(BPF_REG_1
, accounting_map_fd
), /* load map fd to r1 */
291 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_map_lookup_elem
),
292 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 2),
293 BPF_MOV64_IMM(BPF_REG_1
, 1), /* r1 = 1 */
294 BPF_RAW_INSN(BPF_STX
| BPF_XADD
| BPF_DW
, BPF_REG_0
, BPF_REG_1
, 0, 0), /* xadd r0 += r1 */
297 BPF_MOV64_IMM(BPF_REG_0
, MAP_KEY_BYTES
), /* r0 = 1 */
298 BPF_STX_MEM(BPF_W
, BPF_REG_10
, BPF_REG_0
, -4), /* *(u32 *)(fp - 4) = r0 */
299 BPF_MOV64_REG(BPF_REG_2
, BPF_REG_10
),
300 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, -4), /* r2 = fp - 4 */
301 BPF_LD_MAP_FD(BPF_REG_1
, accounting_map_fd
),
302 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_map_lookup_elem
),
303 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 2),
304 BPF_LDX_MEM(BPF_W
, BPF_REG_1
, BPF_REG_6
, offsetof(struct __sk_buff
, len
)), /* r1 = skb->len */
305 BPF_RAW_INSN(BPF_STX
| BPF_XADD
| BPF_DW
, BPF_REG_0
, BPF_REG_1
, 0, 0), /* xadd r0 += r1 */
307 /* Allow the packet to pass */
308 BPF_MOV64_IMM(BPF_REG_0
, 1),
311 /* Jump label fixup */
312 insn
[0].off
= ELEMENTSOF(insn
) - 1;
314 r
= bpf_program_add_instructions(p
, insn
, ELEMENTSOF(insn
));
321 * Exit from the eBPF program, R0 contains the verdict.
322 * 0 means the packet is denied, 1 means the packet may pass.
324 const struct bpf_insn insn
[] = {
328 r
= bpf_program_add_instructions(p
, insn
, ELEMENTSOF(insn
));
338 static int bpf_firewall_count_access_items(IPAddressAccessItem
*list
, size_t *n_ipv4
, size_t *n_ipv6
) {
339 IPAddressAccessItem
*a
;
344 LIST_FOREACH(items
, a
, list
) {
356 return -EAFNOSUPPORT
;
363 static int bpf_firewall_add_access_items(
364 IPAddressAccessItem
*list
,
369 struct bpf_lpm_trie_key
*key_ipv4
, *key_ipv6
;
370 uint64_t value
= verdict
;
371 IPAddressAccessItem
*a
;
374 key_ipv4
= alloca0(offsetof(struct bpf_lpm_trie_key
, data
) + sizeof(uint32_t));
375 key_ipv6
= alloca0(offsetof(struct bpf_lpm_trie_key
, data
) + sizeof(uint32_t) * 4);
377 LIST_FOREACH(items
, a
, list
) {
381 key_ipv4
->prefixlen
= a
->prefixlen
;
382 memcpy(key_ipv4
->data
, &a
->address
, sizeof(uint32_t));
384 r
= bpf_map_update_element(ipv4_map_fd
, key_ipv4
, &value
);
391 key_ipv6
->prefixlen
= a
->prefixlen
;
392 memcpy(key_ipv6
->data
, &a
->address
, 4 * sizeof(uint32_t));
394 r
= bpf_map_update_element(ipv6_map_fd
, key_ipv6
, &value
);
401 return -EAFNOSUPPORT
;
408 static int bpf_firewall_prepare_access_maps(
411 int *ret_ipv4_map_fd
,
412 int *ret_ipv6_map_fd
,
415 _cleanup_close_
int ipv4_map_fd
= -1, ipv6_map_fd
= -1;
416 size_t n_ipv4
= 0, n_ipv6
= 0;
417 IPAddressAccessItem
*list
;
421 assert(ret_ipv4_map_fd
);
422 assert(ret_ipv6_map_fd
);
425 for (p
= u
; p
; p
= UNIT_GET_SLICE(p
)) {
428 cc
= unit_get_cgroup_context(p
);
432 list
= verdict
== ACCESS_ALLOWED
? cc
->ip_address_allow
: cc
->ip_address_deny
;
434 bpf_firewall_count_access_items(list
, &n_ipv4
, &n_ipv6
);
436 /* Skip making the LPM trie map in cases where we are using "any" in order to hack around
437 * needing CAP_SYS_ADMIN for allocating LPM trie map. */
438 if (ip_address_access_item_is_any(list
)) {
445 ipv4_map_fd
= bpf_map_new(
446 BPF_MAP_TYPE_LPM_TRIE
,
447 offsetof(struct bpf_lpm_trie_key
, data
) + sizeof(uint32_t),
456 ipv6_map_fd
= bpf_map_new(
457 BPF_MAP_TYPE_LPM_TRIE
,
458 offsetof(struct bpf_lpm_trie_key
, data
) + sizeof(uint32_t)*4,
466 for (p
= u
; p
; p
= UNIT_GET_SLICE(p
)) {
469 cc
= unit_get_cgroup_context(p
);
473 r
= bpf_firewall_add_access_items(verdict
== ACCESS_ALLOWED
? cc
->ip_address_allow
: cc
->ip_address_deny
,
474 ipv4_map_fd
, ipv6_map_fd
, verdict
);
479 *ret_ipv4_map_fd
= TAKE_FD(ipv4_map_fd
);
480 *ret_ipv6_map_fd
= TAKE_FD(ipv6_map_fd
);
481 *ret_has_any
= false;
485 static int bpf_firewall_prepare_accounting_maps(Unit
*u
, bool enabled
, int *fd_ingress
, int *fd_egress
) {
493 if (*fd_ingress
< 0) {
494 r
= bpf_map_new(BPF_MAP_TYPE_ARRAY
, sizeof(int), sizeof(uint64_t), 2, 0);
501 if (*fd_egress
< 0) {
503 r
= bpf_map_new(BPF_MAP_TYPE_ARRAY
, sizeof(int), sizeof(uint64_t), 2, 0);
511 *fd_ingress
= safe_close(*fd_ingress
);
512 *fd_egress
= safe_close(*fd_egress
);
514 zero(u
->ip_accounting_extra
);
520 int bpf_firewall_compile(Unit
*u
) {
523 bool ip_allow_any
= false, ip_deny_any
= false;
527 cc
= unit_get_cgroup_context(u
);
531 supported
= bpf_firewall_supported();
534 if (supported
== BPF_FIREWALL_UNSUPPORTED
)
535 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
),
536 "BPF firewalling not supported on this manager, proceeding without.");
537 if (supported
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
&& u
->type
== UNIT_SLICE
)
538 /* If BPF_F_ALLOW_MULTI is not supported we don't support any BPF magic on inner nodes (i.e. on slice
539 * units), since that would mean leaf nodes couldn't do any BPF anymore at all. Under the assumption
540 * that BPF is more interesting on leaf nodes we hence avoid it on inner nodes in that case. This is
541 * consistent with old systemd behaviour from before v238, where BPF wasn't supported in inner nodes at
543 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
),
544 "BPF_F_ALLOW_MULTI is not supported on this manager, not doing BPF firewall on slice units.");
546 /* Note that when we compile a new firewall we first flush out the access maps and the BPF programs themselves,
547 * but we reuse the accounting maps. That way the firewall in effect always maps to the actual
548 * configuration, but we don't flush out the accounting unnecessarily */
550 u
->ip_bpf_ingress
= bpf_program_unref(u
->ip_bpf_ingress
);
551 u
->ip_bpf_egress
= bpf_program_unref(u
->ip_bpf_egress
);
553 u
->ipv4_allow_map_fd
= safe_close(u
->ipv4_allow_map_fd
);
554 u
->ipv4_deny_map_fd
= safe_close(u
->ipv4_deny_map_fd
);
556 u
->ipv6_allow_map_fd
= safe_close(u
->ipv6_allow_map_fd
);
557 u
->ipv6_deny_map_fd
= safe_close(u
->ipv6_deny_map_fd
);
559 if (u
->type
!= UNIT_SLICE
) {
560 /* In inner nodes we only do accounting, we do not actually bother with access control. However, leaf
561 * nodes will incorporate all IP access rules set on all their parent nodes. This has the benefit that
562 * they can optionally cancel out system-wide rules. Since inner nodes can't contain processes this
563 * means that all configure IP access rules *will* take effect on processes, even though we never
564 * compile them for inner nodes. */
566 r
= bpf_firewall_prepare_access_maps(u
, ACCESS_ALLOWED
, &u
->ipv4_allow_map_fd
, &u
->ipv6_allow_map_fd
, &ip_allow_any
);
568 return log_unit_error_errno(u
, r
, "Preparation of eBPF allow maps failed: %m");
570 r
= bpf_firewall_prepare_access_maps(u
, ACCESS_DENIED
, &u
->ipv4_deny_map_fd
, &u
->ipv6_deny_map_fd
, &ip_deny_any
);
572 return log_unit_error_errno(u
, r
, "Preparation of eBPF deny maps failed: %m");
575 r
= bpf_firewall_prepare_accounting_maps(u
, cc
->ip_accounting
, &u
->ip_accounting_ingress_map_fd
, &u
->ip_accounting_egress_map_fd
);
577 return log_unit_error_errno(u
, r
, "Preparation of eBPF accounting maps failed: %m");
579 r
= bpf_firewall_compile_bpf(u
, true, &u
->ip_bpf_ingress
, ip_allow_any
, ip_deny_any
);
581 return log_unit_error_errno(u
, r
, "Compilation for ingress BPF program failed: %m");
583 r
= bpf_firewall_compile_bpf(u
, false, &u
->ip_bpf_egress
, ip_allow_any
, ip_deny_any
);
585 return log_unit_error_errno(u
, r
, "Compilation for egress BPF program failed: %m");
590 static int load_bpf_progs_from_fs_to_set(Unit
*u
, char **filter_paths
, Set
**set
) {
595 STRV_FOREACH(bpf_fs_path
, filter_paths
) {
596 _cleanup_(bpf_program_unrefp
) BPFProgram
*prog
= NULL
;
599 r
= bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB
, &prog
);
601 return log_unit_error_errno(u
, r
, "Can't allocate CGROUP SKB BPF program: %m");
603 r
= bpf_program_load_from_bpf_fs(prog
, *bpf_fs_path
);
605 return log_unit_error_errno(u
, r
, "Loading of ingress BPF program %s failed: %m", *bpf_fs_path
);
607 r
= set_ensure_consume(set
, &bpf_program_hash_ops
, TAKE_PTR(prog
));
609 return log_unit_error_errno(u
, r
, "Can't add program to BPF program set: %m");
615 int bpf_firewall_load_custom(Unit
*u
) {
621 cc
= unit_get_cgroup_context(u
);
625 if (!(cc
->ip_filters_ingress
|| cc
->ip_filters_egress
))
628 supported
= bpf_firewall_supported();
632 if (supported
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
)
633 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
), "BPF_F_ALLOW_MULTI not supported on this manager, cannot attach custom BPF programs.");
635 r
= load_bpf_progs_from_fs_to_set(u
, cc
->ip_filters_ingress
, &u
->ip_bpf_custom_ingress
);
638 r
= load_bpf_progs_from_fs_to_set(u
, cc
->ip_filters_egress
, &u
->ip_bpf_custom_egress
);
645 static int attach_custom_bpf_progs(Unit
*u
, const char *path
, int attach_type
, Set
**set
, Set
**set_installed
) {
651 set_clear(*set_installed
);
653 SET_FOREACH(prog
, *set
) {
654 r
= bpf_program_cgroup_attach(prog
, attach_type
, path
, BPF_F_ALLOW_MULTI
);
656 return log_unit_error_errno(u
, r
, "Attaching custom egress BPF program to cgroup %s failed: %m", path
);
658 /* Remember that these BPF programs are installed now. */
659 r
= set_ensure_put(set_installed
, &bpf_program_hash_ops
, prog
);
661 return log_unit_error_errno(u
, r
, "Can't add program to BPF program set: %m");
663 bpf_program_ref(prog
);
669 int bpf_firewall_install(Unit
*u
) {
670 _cleanup_(bpf_program_unrefp
) BPFProgram
*ip_bpf_ingress_uninstall
= NULL
, *ip_bpf_egress_uninstall
= NULL
;
671 _cleanup_free_
char *path
= NULL
;
678 cc
= unit_get_cgroup_context(u
);
683 if (!u
->cgroup_realized
)
686 supported
= bpf_firewall_supported();
689 if (supported
== BPF_FIREWALL_UNSUPPORTED
)
690 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
), "BPF firewalling not supported on this manager, proceeding without.");
691 if (supported
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
&& u
->type
== UNIT_SLICE
)
692 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
), "BPF_F_ALLOW_MULTI is not supported on this manager, not doing BPF firewall on slice units.");
693 if (supported
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
&&
694 (!set_isempty(u
->ip_bpf_custom_ingress
) || !set_isempty(u
->ip_bpf_custom_egress
)))
695 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
), "BPF_F_ALLOW_MULTI not supported on this manager, cannot attach custom BPF programs.");
697 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, u
->cgroup_path
, NULL
, &path
);
699 return log_unit_error_errno(u
, r
, "Failed to determine cgroup path: %m");
701 flags
= supported
== BPF_FIREWALL_SUPPORTED_WITH_MULTI
? BPF_F_ALLOW_MULTI
: 0;
703 if (FLAGS_SET(flags
, BPF_F_ALLOW_MULTI
)) {
704 /* If we have BPF_F_ALLOW_MULTI, then let's clear the fields, but destroy the programs only
705 * after attaching the new programs, so that there's no time window where neither program is
706 * attached. (There will be a program where both are attached, but that's OK, since this is a
707 * security feature where we rather want to lock down too much than too little */
708 ip_bpf_egress_uninstall
= TAKE_PTR(u
->ip_bpf_egress_installed
);
709 ip_bpf_ingress_uninstall
= TAKE_PTR(u
->ip_bpf_ingress_installed
);
711 /* If we don't have BPF_F_ALLOW_MULTI then unref the old BPF programs (which will implicitly
712 * detach them) right before attaching the new program, to minimize the time window when we
713 * don't account for IP traffic. */
714 u
->ip_bpf_egress_installed
= bpf_program_unref(u
->ip_bpf_egress_installed
);
715 u
->ip_bpf_ingress_installed
= bpf_program_unref(u
->ip_bpf_ingress_installed
);
718 if (u
->ip_bpf_egress
) {
719 r
= bpf_program_cgroup_attach(u
->ip_bpf_egress
, BPF_CGROUP_INET_EGRESS
, path
, flags
);
721 return log_unit_error_errno(u
, r
, "Attaching egress BPF program to cgroup %s failed: %m", path
);
723 /* Remember that this BPF program is installed now. */
724 u
->ip_bpf_egress_installed
= bpf_program_ref(u
->ip_bpf_egress
);
727 if (u
->ip_bpf_ingress
) {
728 r
= bpf_program_cgroup_attach(u
->ip_bpf_ingress
, BPF_CGROUP_INET_INGRESS
, path
, flags
);
730 return log_unit_error_errno(u
, r
, "Attaching ingress BPF program to cgroup %s failed: %m", path
);
732 u
->ip_bpf_ingress_installed
= bpf_program_ref(u
->ip_bpf_ingress
);
735 /* And now, definitely get rid of the old programs, and detach them */
736 ip_bpf_egress_uninstall
= bpf_program_unref(ip_bpf_egress_uninstall
);
737 ip_bpf_ingress_uninstall
= bpf_program_unref(ip_bpf_ingress_uninstall
);
739 r
= attach_custom_bpf_progs(u
, path
, BPF_CGROUP_INET_EGRESS
, &u
->ip_bpf_custom_egress
, &u
->ip_bpf_custom_egress_installed
);
743 r
= attach_custom_bpf_progs(u
, path
, BPF_CGROUP_INET_INGRESS
, &u
->ip_bpf_custom_ingress
, &u
->ip_bpf_custom_ingress_installed
);
750 int bpf_firewall_read_accounting(int map_fd
, uint64_t *ret_bytes
, uint64_t *ret_packets
) {
751 uint64_t key
, packets
;
758 key
= MAP_KEY_PACKETS
;
759 r
= bpf_map_lookup_element(map_fd
, &key
, &packets
);
766 r
= bpf_map_lookup_element(map_fd
, &key
, ret_bytes
);
772 *ret_packets
= packets
;
777 int bpf_firewall_reset_accounting(int map_fd
) {
778 uint64_t key
, value
= 0;
784 key
= MAP_KEY_PACKETS
;
785 r
= bpf_map_update_element(map_fd
, &key
, &value
);
790 return bpf_map_update_element(map_fd
, &key
, &value
);
793 static int bpf_firewall_unsupported_reason
= 0;
795 int bpf_firewall_supported(void) {
796 const struct bpf_insn trivial
[] = {
797 BPF_MOV64_IMM(BPF_REG_0
, 1),
801 _cleanup_(bpf_program_unrefp
) BPFProgram
*program
= NULL
;
802 static int supported
= -1;
806 /* Checks whether BPF firewalling is supported. For this, we check the following things:
808 * - whether the unified hierarchy is being used
809 * - the BPF implementation in the kernel supports BPF_PROG_TYPE_CGROUP_SKB programs, which we require
810 * - the BPF implementation in the kernel supports the BPF_PROG_DETACH call, which we require
815 r
= cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
);
817 return log_error_errno(r
, "Can't determine whether the unified hierarchy is used: %m");
819 bpf_firewall_unsupported_reason
=
820 log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN
),
821 "Not running with unified cgroups, BPF firewalling is not supported.");
822 return supported
= BPF_FIREWALL_UNSUPPORTED
;
825 r
= bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB
, &program
);
827 bpf_firewall_unsupported_reason
=
828 log_debug_errno(r
, "Can't allocate CGROUP SKB BPF program, BPF firewalling is not supported: %m");
829 return supported
= BPF_FIREWALL_UNSUPPORTED
;
832 r
= bpf_program_add_instructions(program
, trivial
, ELEMENTSOF(trivial
));
834 bpf_firewall_unsupported_reason
=
835 log_debug_errno(r
, "Can't add trivial instructions to CGROUP SKB BPF program, BPF firewalling is not supported: %m");
836 return supported
= BPF_FIREWALL_UNSUPPORTED
;
839 r
= bpf_program_load_kernel(program
, NULL
, 0);
841 bpf_firewall_unsupported_reason
=
842 log_debug_errno(r
, "Can't load kernel CGROUP SKB BPF program, BPF firewalling is not supported: %m");
843 return supported
= BPF_FIREWALL_UNSUPPORTED
;
846 /* Unfortunately the kernel allows us to create BPF_PROG_TYPE_CGROUP_SKB programs even when CONFIG_CGROUP_BPF
847 * is turned off at kernel compilation time. This sucks of course: why does it allow us to create a cgroup BPF
848 * program if we can't do a thing with it later?
850 * We detect this case by issuing the BPF_PROG_DETACH bpf() call with invalid file descriptors: if
851 * CONFIG_CGROUP_BPF is turned off, then the call will fail early with EINVAL. If it is turned on the
852 * parameters are validated however, and that'll fail with EBADF then. */
854 // FIXME: Clang doesn't 0-pad with structured initialization, causing
855 // the kernel to reject the bpf_attr as invalid. See:
856 // https://github.com/torvalds/linux/blob/v5.9/kernel/bpf/syscall.c#L65
857 // Ideally it should behave like GCC, so that we can remove these workarounds.
859 attr
.attach_type
= BPF_CGROUP_INET_EGRESS
;
861 attr
.attach_bpf_fd
= -1;
863 if (bpf(BPF_PROG_DETACH
, &attr
, sizeof(attr
)) < 0) {
864 if (errno
!= EBADF
) {
865 bpf_firewall_unsupported_reason
=
866 log_debug_errno(errno
, "Didn't get EBADF from BPF_PROG_DETACH, BPF firewalling is not supported: %m");
867 return supported
= BPF_FIREWALL_UNSUPPORTED
;
872 log_debug("Wut? Kernel accepted our invalid BPF_PROG_DETACH call? Something is weird, assuming BPF firewalling is broken and hence not supported.");
873 return supported
= BPF_FIREWALL_UNSUPPORTED
;
876 /* So now we know that the BPF program is generally available, let's see if BPF_F_ALLOW_MULTI is also supported
877 * (which was added in kernel 4.15). We use a similar logic as before, but this time we use the BPF_PROG_ATTACH
878 * bpf() call and the BPF_F_ALLOW_MULTI flags value. Since the flags are checked early in the system call we'll
879 * get EINVAL if it's not supported, and EBADF as before if it is available. */
882 attr
.attach_type
= BPF_CGROUP_INET_EGRESS
;
884 attr
.attach_bpf_fd
= -1;
885 attr
.attach_flags
= BPF_F_ALLOW_MULTI
;
887 if (bpf(BPF_PROG_ATTACH
, &attr
, sizeof(attr
)) < 0) {
888 if (errno
== EBADF
) {
889 log_debug_errno(errno
, "Got EBADF when using BPF_F_ALLOW_MULTI, which indicates it is supported. Yay!");
890 return supported
= BPF_FIREWALL_SUPPORTED_WITH_MULTI
;
894 log_debug_errno(errno
, "Got EINVAL error when using BPF_F_ALLOW_MULTI, which indicates it's not supported.");
896 log_debug_errno(errno
, "Got unexpected error when using BPF_F_ALLOW_MULTI, assuming it's not supported: %m");
898 return supported
= BPF_FIREWALL_SUPPORTED
;
900 log_debug("Wut? Kernel accepted our invalid BPF_PROG_ATTACH+BPF_F_ALLOW_MULTI call? Something is weird, assuming BPF firewalling is broken and hence not supported.");
901 return supported
= BPF_FIREWALL_UNSUPPORTED
;
905 void emit_bpf_firewall_warning(Unit
*u
) {
906 static bool warned
= false;
909 bool quiet
= bpf_firewall_unsupported_reason
== -EPERM
&& detect_container() > 0;
911 log_unit_full_errno(u
, quiet
? LOG_DEBUG
: LOG_WARNING
, bpf_firewall_unsupported_reason
,
912 "unit configures an IP firewall, but %s.\n"
913 "(This warning is only shown for the first unit using IP firewalling.)",
914 getuid() != 0 ? "not running as root" :
915 "the local system does not support BPF/cgroup firewalling");
920 void bpf_firewall_close(Unit
*u
) {
923 u
->ip_accounting_ingress_map_fd
= safe_close(u
->ip_accounting_ingress_map_fd
);
924 u
->ip_accounting_egress_map_fd
= safe_close(u
->ip_accounting_egress_map_fd
);
926 u
->ipv4_allow_map_fd
= safe_close(u
->ipv4_allow_map_fd
);
927 u
->ipv6_allow_map_fd
= safe_close(u
->ipv6_allow_map_fd
);
928 u
->ipv4_deny_map_fd
= safe_close(u
->ipv4_deny_map_fd
);
929 u
->ipv6_deny_map_fd
= safe_close(u
->ipv6_deny_map_fd
);
931 u
->ip_bpf_ingress
= bpf_program_unref(u
->ip_bpf_ingress
);
932 u
->ip_bpf_ingress_installed
= bpf_program_unref(u
->ip_bpf_ingress_installed
);
933 u
->ip_bpf_egress
= bpf_program_unref(u
->ip_bpf_egress
);
934 u
->ip_bpf_egress_installed
= bpf_program_unref(u
->ip_bpf_egress_installed
);
936 u
->ip_bpf_custom_ingress
= set_free(u
->ip_bpf_custom_ingress
);
937 u
->ip_bpf_custom_egress
= set_free(u
->ip_bpf_custom_egress
);
938 u
->ip_bpf_custom_ingress_installed
= set_free(u
->ip_bpf_custom_ingress_installed
);
939 u
->ip_bpf_custom_egress_installed
= set_free(u
->ip_bpf_custom_egress_installed
);