1 /* SPDX-License-Identifier: LGPL-2.1+ */
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_DEREF(p
->slice
)) {
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_DEREF(p
->slice
)) {
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 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(filter_prog_hash_ops
, void, trivial_hash_func
, trivial_compare_func
, BPFProgram
, bpf_program_unref
);
592 static int load_bpf_progs_from_fs_to_set(Unit
*u
, char **filter_paths
, Set
**set
) {
597 STRV_FOREACH(bpf_fs_path
, filter_paths
) {
598 _cleanup_(bpf_program_unrefp
) BPFProgram
*prog
= NULL
;
601 r
= bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB
, &prog
);
603 return log_unit_error_errno(u
, r
, "Can't allocate CGROUP SKB BPF program: %m");
605 r
= bpf_program_load_from_bpf_fs(prog
, *bpf_fs_path
);
607 return log_unit_error_errno(u
, r
, "Loading of ingress BPF program %s failed: %m", *bpf_fs_path
);
609 r
= set_ensure_consume(set
, &filter_prog_hash_ops
, TAKE_PTR(prog
));
611 return log_unit_error_errno(u
, r
, "Can't add program to BPF program set: %m");
617 int bpf_firewall_load_custom(Unit
*u
) {
623 cc
= unit_get_cgroup_context(u
);
627 if (!(cc
->ip_filters_ingress
|| cc
->ip_filters_egress
))
630 supported
= bpf_firewall_supported();
634 if (supported
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
)
635 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
), "BPF_F_ALLOW_MULTI not supported on this manager, cannot attach custom BPF programs.");
637 r
= load_bpf_progs_from_fs_to_set(u
, cc
->ip_filters_ingress
, &u
->ip_bpf_custom_ingress
);
640 r
= load_bpf_progs_from_fs_to_set(u
, cc
->ip_filters_egress
, &u
->ip_bpf_custom_egress
);
647 static int attach_custom_bpf_progs(Unit
*u
, const char *path
, int attach_type
, Set
**set
, Set
**set_installed
) {
653 set_clear(*set_installed
);
655 SET_FOREACH(prog
, *set
) {
656 r
= bpf_program_cgroup_attach(prog
, attach_type
, path
, BPF_F_ALLOW_MULTI
);
658 return log_unit_error_errno(u
, r
, "Attaching custom egress BPF program to cgroup %s failed: %m", path
);
660 /* Remember that these BPF programs are installed now. */
661 r
= set_ensure_put(set_installed
, &filter_prog_hash_ops
, prog
);
663 return log_unit_error_errno(u
, r
, "Can't add program to BPF program set: %m");
664 bpf_program_ref(prog
);
670 int bpf_firewall_install(Unit
*u
) {
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 log_unit_debug(u
, "BPF firewalling not supported on this manager, proceeding without.");
693 if (supported
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
&& u
->type
== UNIT_SLICE
) {
694 log_unit_debug(u
, "BPF_F_ALLOW_MULTI is not supported on this manager, not doing BPF firewall on slice units.");
697 if (supported
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
&&
698 (!set_isempty(u
->ip_bpf_custom_ingress
) || !set_isempty(u
->ip_bpf_custom_egress
)))
699 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
), "BPF_F_ALLOW_MULTI not supported on this manager, cannot attach custom BPF programs.");
701 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, u
->cgroup_path
, NULL
, &path
);
703 return log_unit_error_errno(u
, r
, "Failed to determine cgroup path: %m");
705 flags
= (supported
== BPF_FIREWALL_SUPPORTED_WITH_MULTI
&&
706 (u
->type
== UNIT_SLICE
|| unit_cgroup_delegate(u
))) ? BPF_F_ALLOW_MULTI
: 0;
708 /* Unref the old BPF program (which will implicitly detach it) right before attaching the new program, to
709 * minimize the time window when we don't account for IP traffic. */
710 u
->ip_bpf_egress_installed
= bpf_program_unref(u
->ip_bpf_egress_installed
);
711 u
->ip_bpf_ingress_installed
= bpf_program_unref(u
->ip_bpf_ingress_installed
);
713 if (u
->ip_bpf_egress
) {
714 r
= bpf_program_cgroup_attach(u
->ip_bpf_egress
, BPF_CGROUP_INET_EGRESS
, path
,
715 flags
| (set_isempty(u
->ip_bpf_custom_egress
) ? 0 : BPF_F_ALLOW_MULTI
));
717 return log_unit_error_errno(u
, r
, "Attaching egress BPF program to cgroup %s failed: %m", path
);
719 /* Remember that this BPF program is installed now. */
720 u
->ip_bpf_egress_installed
= bpf_program_ref(u
->ip_bpf_egress
);
723 if (u
->ip_bpf_ingress
) {
724 r
= bpf_program_cgroup_attach(u
->ip_bpf_ingress
, BPF_CGROUP_INET_INGRESS
, path
,
725 flags
| (set_isempty(u
->ip_bpf_custom_ingress
) ? 0 : BPF_F_ALLOW_MULTI
));
727 return log_unit_error_errno(u
, r
, "Attaching ingress BPF program to cgroup %s failed: %m", path
);
729 u
->ip_bpf_ingress_installed
= bpf_program_ref(u
->ip_bpf_ingress
);
732 r
= attach_custom_bpf_progs(u
, path
, BPF_CGROUP_INET_EGRESS
, &u
->ip_bpf_custom_egress
, &u
->ip_bpf_custom_egress_installed
);
736 r
= attach_custom_bpf_progs(u
, path
, BPF_CGROUP_INET_INGRESS
, &u
->ip_bpf_custom_ingress
, &u
->ip_bpf_custom_ingress_installed
);
743 int bpf_firewall_read_accounting(int map_fd
, uint64_t *ret_bytes
, uint64_t *ret_packets
) {
744 uint64_t key
, packets
;
751 key
= MAP_KEY_PACKETS
;
752 r
= bpf_map_lookup_element(map_fd
, &key
, &packets
);
759 r
= bpf_map_lookup_element(map_fd
, &key
, ret_bytes
);
765 *ret_packets
= packets
;
770 int bpf_firewall_reset_accounting(int map_fd
) {
771 uint64_t key
, value
= 0;
777 key
= MAP_KEY_PACKETS
;
778 r
= bpf_map_update_element(map_fd
, &key
, &value
);
783 return bpf_map_update_element(map_fd
, &key
, &value
);
786 static int bpf_firewall_unsupported_reason
= 0;
788 int bpf_firewall_supported(void) {
789 const struct bpf_insn trivial
[] = {
790 BPF_MOV64_IMM(BPF_REG_0
, 1),
794 _cleanup_(bpf_program_unrefp
) BPFProgram
*program
= NULL
;
795 static int supported
= -1;
799 /* Checks whether BPF firewalling is supported. For this, we check the following things:
801 * - whether the unified hierarchy is being used
802 * - the BPF implementation in the kernel supports BPF_PROG_TYPE_CGROUP_SKB programs, which we require
803 * - the BPF implementation in the kernel supports the BPF_PROG_DETACH call, which we require
808 r
= cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
);
810 return log_error_errno(r
, "Can't determine whether the unified hierarchy is used: %m");
812 bpf_firewall_unsupported_reason
=
813 log_debug_errno(SYNTHETIC_ERRNO(EUCLEAN
),
814 "Not running with unified cgroups, BPF firewalling is not supported.");
815 return supported
= BPF_FIREWALL_UNSUPPORTED
;
818 r
= bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB
, &program
);
820 bpf_firewall_unsupported_reason
=
821 log_debug_errno(r
, "Can't allocate CGROUP SKB BPF program, BPF firewalling is not supported: %m");
822 return supported
= BPF_FIREWALL_UNSUPPORTED
;
825 r
= bpf_program_add_instructions(program
, trivial
, ELEMENTSOF(trivial
));
827 bpf_firewall_unsupported_reason
=
828 log_debug_errno(r
, "Can't add trivial instructions to CGROUP SKB BPF program, BPF firewalling is not supported: %m");
829 return supported
= BPF_FIREWALL_UNSUPPORTED
;
832 r
= bpf_program_load_kernel(program
, NULL
, 0);
834 bpf_firewall_unsupported_reason
=
835 log_debug_errno(r
, "Can't load kernel CGROUP SKB BPF program, BPF firewalling is not supported: %m");
836 return supported
= BPF_FIREWALL_UNSUPPORTED
;
839 /* Unfortunately the kernel allows us to create BPF_PROG_TYPE_CGROUP_SKB programs even when CONFIG_CGROUP_BPF
840 * is turned off at kernel compilation time. This sucks of course: why does it allow us to create a cgroup BPF
841 * program if we can't do a thing with it later?
843 * We detect this case by issuing the BPF_PROG_DETACH bpf() call with invalid file descriptors: if
844 * CONFIG_CGROUP_BPF is turned off, then the call will fail early with EINVAL. If it is turned on the
845 * parameters are validated however, and that'll fail with EBADF then. */
847 attr
= (union bpf_attr
) {
848 .attach_type
= BPF_CGROUP_INET_EGRESS
,
853 if (bpf(BPF_PROG_DETACH
, &attr
, sizeof(attr
)) < 0) {
854 if (errno
!= EBADF
) {
855 bpf_firewall_unsupported_reason
=
856 log_debug_errno(errno
, "Didn't get EBADF from BPF_PROG_DETACH, BPF firewalling is not supported: %m");
857 return supported
= BPF_FIREWALL_UNSUPPORTED
;
862 log_debug("Wut? Kernel accepted our invalid BPF_PROG_DETACH call? Something is weird, assuming BPF firewalling is broken and hence not supported.");
863 return supported
= BPF_FIREWALL_UNSUPPORTED
;
866 /* So now we know that the BPF program is generally available, let's see if BPF_F_ALLOW_MULTI is also supported
867 * (which was added in kernel 4.15). We use a similar logic as before, but this time we use the BPF_PROG_ATTACH
868 * bpf() call and the BPF_F_ALLOW_MULTI flags value. Since the flags are checked early in the system call we'll
869 * get EINVAL if it's not supported, and EBADF as before if it is available. */
871 attr
= (union bpf_attr
) {
872 .attach_type
= BPF_CGROUP_INET_EGRESS
,
875 .attach_flags
= BPF_F_ALLOW_MULTI
,
878 if (bpf(BPF_PROG_ATTACH
, &attr
, sizeof(attr
)) < 0) {
879 if (errno
== EBADF
) {
880 log_debug_errno(errno
, "Got EBADF when using BPF_F_ALLOW_MULTI, which indicates it is supported. Yay!");
881 return supported
= BPF_FIREWALL_SUPPORTED_WITH_MULTI
;
885 log_debug_errno(errno
, "Got EINVAL error when using BPF_F_ALLOW_MULTI, which indicates it's not supported.");
887 log_debug_errno(errno
, "Got unexpected error when using BPF_F_ALLOW_MULTI, assuming it's not supported: %m");
889 return supported
= BPF_FIREWALL_SUPPORTED
;
891 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.");
892 return supported
= BPF_FIREWALL_UNSUPPORTED
;
896 void emit_bpf_firewall_warning(Unit
*u
) {
897 static bool warned
= false;
900 bool quiet
= bpf_firewall_unsupported_reason
== -EPERM
&& detect_container();
902 log_unit_full(u
, quiet
? LOG_DEBUG
: LOG_WARNING
, bpf_firewall_unsupported_reason
,
903 "unit configures an IP firewall, but %s.\n"
904 "(This warning is only shown for the first unit using IP firewalling.)",
905 getuid() != 0 ? "not running as root" :
906 "the local system does not support BPF/cgroup firewalling");