1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/bpf_insn.h>
5 #include <linux/if_ether.h>
7 #include <netinet/ip.h>
8 #include <netinet/ip6.h>
12 #include "alloc-util.h"
13 #include "bpf-firewall.h"
14 #include "bpf-program.h"
15 #include "errno-util.h"
17 #include "in-addr-prefix-util.h"
19 #include "memory-util.h"
21 #include "string-util.h"
36 /* Compile instructions for one list of addresses, one direction and one specific verdict on matches. */
38 static int add_lookup_instructions(
45 int r
, addr_offset
, addr_size
;
53 addr_size
= sizeof(uint32_t);
54 addr_offset
= is_ingress
?
55 offsetof(struct iphdr
, saddr
) :
56 offsetof(struct iphdr
, daddr
);
60 addr_size
= 4 * sizeof(uint32_t);
61 addr_offset
= is_ingress
?
62 offsetof(struct ip6_hdr
, ip6_src
.s6_addr
) :
63 offsetof(struct ip6_hdr
, ip6_dst
.s6_addr
);
71 /* Compare IPv4 with one word instruction (32-bit) */
72 struct bpf_insn insn
[] = {
73 /* If skb->protocol != ETH_P_IP, skip this whole block. The offset will be set later. */
74 BPF_JMP_IMM(BPF_JNE
, BPF_REG_7
, htobe16(protocol
), 0),
77 * Call into BPF_FUNC_skb_load_bytes to load the dst/src IP address
79 * R1: Pointer to the skb
81 * R3: Destination buffer on the stack (r10 - 4)
82 * R4: Number of bytes to read (4)
85 BPF_MOV64_REG(BPF_REG_1
, BPF_REG_6
),
86 BPF_MOV32_IMM(BPF_REG_2
, addr_offset
),
88 BPF_MOV64_REG(BPF_REG_3
, BPF_REG_10
),
89 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_3
, -addr_size
),
91 BPF_MOV32_IMM(BPF_REG_4
, addr_size
),
92 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_skb_load_bytes
),
95 * Call into BPF_FUNC_map_lookup_elem to see if the address matches any entry in the
96 * LPM trie map. For this to work, the prefixlen field of 'struct bpf_lpm_trie_key'
97 * has to be set to the maximum possible value.
99 * On success, the looked up value is stored in R0. For this application, the actual
100 * value doesn't matter, however; we just set the bit in @verdict in R8 if we found any
104 BPF_LD_MAP_FD(BPF_REG_1
, map_fd
),
105 BPF_MOV64_REG(BPF_REG_2
, BPF_REG_10
),
106 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, -addr_size
- sizeof(uint32_t)),
107 BPF_ST_MEM(BPF_W
, BPF_REG_2
, 0, addr_size
* 8),
109 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_map_lookup_elem
),
110 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 1),
111 BPF_ALU32_IMM(BPF_OR
, BPF_REG_8
, verdict
),
114 /* Jump label fixup */
115 insn
[0].off
= ELEMENTSOF(insn
) - 1;
117 r
= bpf_program_add_instructions(p
, insn
, ELEMENTSOF(insn
));
126 static int add_instructions_for_ip_any(
133 const struct bpf_insn insn
[] = {
134 BPF_ALU32_IMM(BPF_OR
, BPF_REG_8
, verdict
),
137 r
= bpf_program_add_instructions(p
, insn
, 1);
144 static int bpf_firewall_compile_bpf(
146 const char *prog_name
,
152 const struct bpf_insn pre_insn
[] = {
154 * When the eBPF program is entered, R1 contains the address of the skb.
155 * However, R1-R5 are scratch registers that are not preserved when calling
156 * into kernel functions, so we need to save anything that's supposed to
157 * stay around to R6-R9. Save the skb to R6.
159 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
162 * Although we cannot access the skb data directly from eBPF programs used in this
163 * scenario, the kernel has prepared some fields for us to access through struct __sk_buff.
164 * Load the protocol (IPv4, IPv6) used by the packet in flight once and cache it in R7
167 BPF_LDX_MEM(BPF_W
, BPF_REG_7
, BPF_REG_6
, offsetof(struct __sk_buff
, protocol
)),
170 * R8 is used to keep track of whether any address check has explicitly allowed or denied the packet
171 * through ACCESS_DENIED or ACCESS_ALLOWED bits. Reset them both to 0 in the beginning.
173 BPF_MOV32_IMM(BPF_REG_8
, 0),
177 * The access checkers compiled for the configured allowance and denial lists
178 * write to R8 at runtime. The following code prepares for an early exit that
179 * skip the accounting if the packet is denied.
182 * if (R8 == ACCESS_DENIED)
185 * This means that if both ACCESS_DENIED and ACCESS_ALLOWED are set, the packet
186 * is allowed to pass.
188 const struct bpf_insn post_insn
[] = {
189 BPF_MOV64_IMM(BPF_REG_0
, 1),
190 BPF_JMP_IMM(BPF_JNE
, BPF_REG_8
, ACCESS_DENIED
, 1),
191 BPF_MOV64_IMM(BPF_REG_0
, 0),
194 _cleanup_(bpf_program_freep
) BPFProgram
*p
= NULL
;
195 int accounting_map_fd
, r
;
202 crt
= unit_get_cgroup_runtime(u
);
208 accounting_map_fd
= is_ingress
?
209 crt
->ip_accounting_ingress_map_fd
:
210 crt
->ip_accounting_egress_map_fd
;
213 crt
->ipv4_allow_map_fd
>= 0 ||
214 crt
->ipv6_allow_map_fd
>= 0 ||
215 crt
->ipv4_deny_map_fd
>= 0 ||
216 crt
->ipv6_deny_map_fd
>= 0 ||
220 if (accounting_map_fd
< 0 && !access_enabled
) {
225 r
= bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB
, prog_name
, &p
);
229 r
= bpf_program_add_instructions(p
, pre_insn
, ELEMENTSOF(pre_insn
));
233 if (access_enabled
) {
235 * The simple rule this function translates into eBPF instructions is:
237 * - Access will be granted when an address matches an entry in @list_allow
238 * - Otherwise, access will be denied when an address matches an entry in @list_deny
239 * - Otherwise, access will be granted
242 if (crt
->ipv4_deny_map_fd
>= 0) {
243 r
= add_lookup_instructions(p
, crt
->ipv4_deny_map_fd
, ETH_P_IP
, is_ingress
, ACCESS_DENIED
);
248 if (crt
->ipv6_deny_map_fd
>= 0) {
249 r
= add_lookup_instructions(p
, crt
->ipv6_deny_map_fd
, ETH_P_IPV6
, is_ingress
, ACCESS_DENIED
);
254 if (crt
->ipv4_allow_map_fd
>= 0) {
255 r
= add_lookup_instructions(p
, crt
->ipv4_allow_map_fd
, ETH_P_IP
, is_ingress
, ACCESS_ALLOWED
);
260 if (crt
->ipv6_allow_map_fd
>= 0) {
261 r
= add_lookup_instructions(p
, crt
->ipv6_allow_map_fd
, ETH_P_IPV6
, is_ingress
, ACCESS_ALLOWED
);
267 r
= add_instructions_for_ip_any(p
, ACCESS_ALLOWED
);
273 r
= add_instructions_for_ip_any(p
, ACCESS_DENIED
);
279 r
= bpf_program_add_instructions(p
, post_insn
, ELEMENTSOF(post_insn
));
283 if (accounting_map_fd
>= 0) {
284 struct bpf_insn insn
[] = {
286 * If R0 == 0, the packet will be denied; skip the accounting instructions in this case.
287 * The jump label will be fixed up later.
289 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 0),
292 BPF_MOV64_IMM(BPF_REG_0
, MAP_KEY_PACKETS
), /* r0 = 0 */
293 BPF_STX_MEM(BPF_W
, BPF_REG_10
, BPF_REG_0
, -4), /* *(u32 *)(fp - 4) = r0 */
294 BPF_MOV64_REG(BPF_REG_2
, BPF_REG_10
),
295 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, -4), /* r2 = fp - 4 */
296 BPF_LD_MAP_FD(BPF_REG_1
, accounting_map_fd
), /* load map fd to r1 */
297 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_map_lookup_elem
),
298 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 2),
299 BPF_MOV64_IMM(BPF_REG_1
, 1), /* r1 = 1 */
300 BPF_RAW_INSN(BPF_STX
| BPF_XADD
| BPF_DW
, BPF_REG_0
, BPF_REG_1
, 0, 0), /* xadd r0 += r1 */
303 BPF_MOV64_IMM(BPF_REG_0
, MAP_KEY_BYTES
), /* r0 = 1 */
304 BPF_STX_MEM(BPF_W
, BPF_REG_10
, BPF_REG_0
, -4), /* *(u32 *)(fp - 4) = r0 */
305 BPF_MOV64_REG(BPF_REG_2
, BPF_REG_10
),
306 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, -4), /* r2 = fp - 4 */
307 BPF_LD_MAP_FD(BPF_REG_1
, accounting_map_fd
),
308 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_map_lookup_elem
),
309 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 2),
310 BPF_LDX_MEM(BPF_W
, BPF_REG_1
, BPF_REG_6
, offsetof(struct __sk_buff
, len
)), /* r1 = skb->len */
311 BPF_RAW_INSN(BPF_STX
| BPF_XADD
| BPF_DW
, BPF_REG_0
, BPF_REG_1
, 0, 0), /* xadd r0 += r1 */
313 /* Allow the packet to pass */
314 BPF_MOV64_IMM(BPF_REG_0
, 1),
317 /* Jump label fixup */
318 insn
[0].off
= ELEMENTSOF(insn
) - 1;
320 r
= bpf_program_add_instructions(p
, insn
, ELEMENTSOF(insn
));
327 * Exit from the eBPF program, R0 contains the verdict.
328 * 0 means the packet is denied, 1 means the packet may pass.
330 const struct bpf_insn insn
[] = {
334 r
= bpf_program_add_instructions(p
, insn
, ELEMENTSOF(insn
));
344 static int bpf_firewall_count_access_items(Set
*prefixes
, size_t *n_ipv4
, size_t *n_ipv6
) {
345 struct in_addr_prefix
*a
;
350 SET_FOREACH(a
, prefixes
)
362 return -EAFNOSUPPORT
;
368 static int bpf_firewall_add_access_items(
374 struct bpf_lpm_trie_key
*key_ipv4
, *key_ipv6
;
375 struct in_addr_prefix
*a
;
376 uint64_t value
= verdict
;
379 key_ipv4
= alloca0(offsetof(struct bpf_lpm_trie_key
, data
) + sizeof(uint32_t));
380 key_ipv6
= alloca0(offsetof(struct bpf_lpm_trie_key
, data
) + sizeof(uint32_t) * 4);
382 SET_FOREACH(a
, prefixes
)
386 key_ipv4
->prefixlen
= a
->prefixlen
;
387 memcpy(key_ipv4
->data
, &a
->address
, sizeof(uint32_t));
389 r
= bpf_map_update_element(ipv4_map_fd
, key_ipv4
, &value
);
396 key_ipv6
->prefixlen
= a
->prefixlen
;
397 memcpy(key_ipv6
->data
, &a
->address
, 4 * sizeof(uint32_t));
399 r
= bpf_map_update_element(ipv6_map_fd
, key_ipv6
, &value
);
406 return -EAFNOSUPPORT
;
412 static int bpf_firewall_prepare_access_maps(
415 int *ret_ipv4_map_fd
,
416 int *ret_ipv6_map_fd
,
419 _cleanup_close_
int ipv4_map_fd
= -EBADF
, ipv6_map_fd
= -EBADF
;
420 size_t n_ipv4
= 0, n_ipv6
= 0;
423 assert(ret_ipv4_map_fd
);
424 assert(ret_ipv6_map_fd
);
427 for (Unit
*p
= u
; p
; p
= UNIT_GET_SLICE(p
)) {
432 cc
= unit_get_cgroup_context(p
);
436 prefixes
= verdict
== ACCESS_ALLOWED
? cc
->ip_address_allow
: cc
->ip_address_deny
;
437 reduced
= verdict
== ACCESS_ALLOWED
? &cc
->ip_address_allow_reduced
: &cc
->ip_address_deny_reduced
;
440 r
= in_addr_prefixes_reduce(prefixes
);
447 bpf_firewall_count_access_items(prefixes
, &n_ipv4
, &n_ipv6
);
449 /* Skip making the LPM trie map in cases where we are using "any" in order to hack around
450 * needing CAP_SYS_ADMIN for allocating LPM trie map. */
451 if (in_addr_prefixes_is_any(prefixes
)) {
458 const char *name
= strjoina("4_", u
->id
);
459 ipv4_map_fd
= bpf_map_new(
461 BPF_MAP_TYPE_LPM_TRIE
,
462 offsetof(struct bpf_lpm_trie_key
, data
) + sizeof(uint32_t),
471 const char *name
= strjoina("6_", u
->id
);
472 ipv6_map_fd
= bpf_map_new(
474 BPF_MAP_TYPE_LPM_TRIE
,
475 offsetof(struct bpf_lpm_trie_key
, data
) + sizeof(uint32_t)*4,
483 for (Unit
*p
= u
; p
; p
= UNIT_GET_SLICE(p
)) {
486 cc
= unit_get_cgroup_context(p
);
490 r
= bpf_firewall_add_access_items(verdict
== ACCESS_ALLOWED
? cc
->ip_address_allow
: cc
->ip_address_deny
,
491 ipv4_map_fd
, ipv6_map_fd
, verdict
);
496 *ret_ipv4_map_fd
= TAKE_FD(ipv4_map_fd
);
497 *ret_ipv6_map_fd
= TAKE_FD(ipv6_map_fd
);
498 *ret_has_any
= false;
502 static int bpf_firewall_prepare_accounting_maps(Unit
*u
, bool enabled
, CGroupRuntime
*crt
) {
509 if (crt
->ip_accounting_ingress_map_fd
< 0) {
510 const char *name
= strjoina("I_", u
->id
);
511 r
= bpf_map_new(name
, BPF_MAP_TYPE_ARRAY
, sizeof(int), sizeof(uint64_t), 2, 0);
515 crt
->ip_accounting_ingress_map_fd
= r
;
518 if (crt
->ip_accounting_egress_map_fd
< 0) {
519 const char *name
= strjoina("E_", u
->id
);
520 r
= bpf_map_new(name
, BPF_MAP_TYPE_ARRAY
, sizeof(int), sizeof(uint64_t), 2, 0);
524 crt
->ip_accounting_egress_map_fd
= r
;
528 crt
->ip_accounting_ingress_map_fd
= safe_close(crt
->ip_accounting_ingress_map_fd
);
529 crt
->ip_accounting_egress_map_fd
= safe_close(crt
->ip_accounting_egress_map_fd
);
531 zero(crt
->ip_accounting_extra
);
537 int bpf_firewall_compile(Unit
*u
) {
538 const char *ingress_name
= NULL
, *egress_name
= NULL
;
539 bool ip_allow_any
= false, ip_deny_any
= false;
546 cc
= unit_get_cgroup_context(u
);
550 crt
= unit_setup_cgroup_runtime(u
);
554 if (bpf_program_supported() <= 0)
555 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
),
556 "bpf-firewall: BPF firewalling not supported, proceeding without.");
558 ingress_name
= "sd_fw_ingress";
559 egress_name
= "sd_fw_egress";
561 /* Note that when we compile a new firewall we first flush out the access maps and the BPF programs themselves,
562 * but we reuse the accounting maps. That way the firewall in effect always maps to the actual
563 * configuration, but we don't flush out the accounting unnecessarily */
565 crt
->ip_bpf_ingress
= bpf_program_free(crt
->ip_bpf_ingress
);
566 crt
->ip_bpf_egress
= bpf_program_free(crt
->ip_bpf_egress
);
568 crt
->ipv4_allow_map_fd
= safe_close(crt
->ipv4_allow_map_fd
);
569 crt
->ipv4_deny_map_fd
= safe_close(crt
->ipv4_deny_map_fd
);
571 crt
->ipv6_allow_map_fd
= safe_close(crt
->ipv6_allow_map_fd
);
572 crt
->ipv6_deny_map_fd
= safe_close(crt
->ipv6_deny_map_fd
);
574 if (u
->type
!= UNIT_SLICE
) {
575 /* In inner nodes we only do accounting, we do not actually bother with access control. However, leaf
576 * nodes will incorporate all IP access rules set on all their parent nodes. This has the benefit that
577 * they can optionally cancel out system-wide rules. Since inner nodes can't contain processes this
578 * means that all configure IP access rules *will* take effect on processes, even though we never
579 * compile them for inner nodes. */
581 r
= bpf_firewall_prepare_access_maps(u
, ACCESS_ALLOWED
, &crt
->ipv4_allow_map_fd
, &crt
->ipv6_allow_map_fd
, &ip_allow_any
);
583 return log_unit_error_errno(u
, r
, "bpf-firewall: Preparation of BPF allow maps failed: %m");
585 r
= bpf_firewall_prepare_access_maps(u
, ACCESS_DENIED
, &crt
->ipv4_deny_map_fd
, &crt
->ipv6_deny_map_fd
, &ip_deny_any
);
587 return log_unit_error_errno(u
, r
, "bpf-firewall: Preparation of BPF deny maps failed: %m");
590 r
= bpf_firewall_prepare_accounting_maps(u
, cc
->ip_accounting
, crt
);
592 return log_unit_error_errno(u
, r
, "bpf-firewall: Preparation of BPF accounting maps failed: %m");
594 r
= bpf_firewall_compile_bpf(u
, ingress_name
, true, &crt
->ip_bpf_ingress
, ip_allow_any
, ip_deny_any
);
596 return log_unit_error_errno(u
, r
, "bpf-firewall: Compilation of ingress BPF program failed: %m");
598 r
= bpf_firewall_compile_bpf(u
, egress_name
, false, &crt
->ip_bpf_egress
, ip_allow_any
, ip_deny_any
);
600 return log_unit_error_errno(u
, r
, "bpf-firewall: Compilation of egress BPF program failed: %m");
605 static int load_bpf_progs_from_fs_to_set(Unit
*u
, char **filter_paths
, Set
**set
) {
608 STRV_FOREACH(bpf_fs_path
, filter_paths
) {
609 _cleanup_(bpf_program_freep
) BPFProgram
*prog
= NULL
;
612 r
= bpf_program_new(BPF_PROG_TYPE_CGROUP_SKB
, NULL
, &prog
);
614 return log_unit_error_errno(u
, r
, "bpf-firewall: Allocation of SKB BPF program failed: %m");
616 r
= bpf_program_load_from_bpf_fs(prog
, *bpf_fs_path
);
618 return log_unit_error_errno(u
, r
, "bpf-firewall: Loading of ingress BPF program %s failed: %m", *bpf_fs_path
);
620 r
= set_ensure_consume(set
, &bpf_program_hash_ops
, TAKE_PTR(prog
));
628 int bpf_firewall_load_custom(Unit
*u
) {
635 cc
= unit_get_cgroup_context(u
);
638 crt
= unit_get_cgroup_runtime(u
);
642 if (!(cc
->ip_filters_ingress
|| cc
->ip_filters_egress
))
645 if (bpf_program_supported() <= 0)
646 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
),
647 "bpf-firewall: BPF firewalling not supported, cannot attach custom BPF programs.");
649 r
= load_bpf_progs_from_fs_to_set(u
, cc
->ip_filters_ingress
, &crt
->ip_bpf_custom_ingress
);
652 r
= load_bpf_progs_from_fs_to_set(u
, cc
->ip_filters_egress
, &crt
->ip_bpf_custom_egress
);
659 static int attach_custom_bpf_progs(Unit
*u
, const char *path
, int attach_type
, Set
**set
, Set
**set_installed
) {
665 set_clear(*set_installed
);
666 r
= set_ensure_allocated(set_installed
, &bpf_program_hash_ops
);
670 SET_FOREACH_MOVE(prog
, *set_installed
, *set
) {
671 r
= bpf_program_cgroup_attach(prog
, attach_type
, path
, BPF_F_ALLOW_MULTI
);
673 return log_unit_error_errno(u
, r
, "bpf-firewall: Attaching custom egress BPF program to cgroup %s failed: %m", path
);
678 int bpf_firewall_install(Unit
*u
) {
679 _cleanup_(bpf_program_freep
) BPFProgram
*ip_bpf_ingress_uninstall
= NULL
, *ip_bpf_egress_uninstall
= NULL
;
680 _cleanup_free_
char *path
= NULL
;
687 cc
= unit_get_cgroup_context(u
);
691 crt
= unit_get_cgroup_runtime(u
);
692 if (!crt
|| !crt
->cgroup_path
)
695 if (bpf_program_supported() <= 0)
696 return log_unit_debug_errno(u
, SYNTHETIC_ERRNO(EOPNOTSUPP
),
697 "bpf-firewall: BPF firewalling not supported, proceeding without.");
699 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, crt
->cgroup_path
, NULL
, &path
);
701 return log_unit_error_errno(u
, r
, "bpf-firewall: Failed to determine cgroup path: %m");
703 /* Let's clear the fields, but destroy the programs only after attaching the new programs, so that
704 * there's no time window where neither program is attached. (There will be a program where both are
705 * attached, but that's OK, since this is a security feature where we rather want to lock down too
706 * much than too little. */
707 ip_bpf_egress_uninstall
= TAKE_PTR(crt
->ip_bpf_egress_installed
);
708 ip_bpf_ingress_uninstall
= TAKE_PTR(crt
->ip_bpf_ingress_installed
);
710 if (crt
->ip_bpf_egress
) {
711 r
= bpf_program_cgroup_attach(crt
->ip_bpf_egress
, BPF_CGROUP_INET_EGRESS
, path
, BPF_F_ALLOW_MULTI
);
713 return log_unit_error_errno(u
, r
,
714 "bpf-firewall: Attaching egress BPF program to cgroup %s failed: %m", path
);
716 /* Remember that this BPF program is installed now. */
717 crt
->ip_bpf_egress_installed
= TAKE_PTR(crt
->ip_bpf_egress
);
720 if (crt
->ip_bpf_ingress
) {
721 r
= bpf_program_cgroup_attach(crt
->ip_bpf_ingress
, BPF_CGROUP_INET_INGRESS
, path
, BPF_F_ALLOW_MULTI
);
723 return log_unit_error_errno(u
, r
,
724 "bpf-firewall: Attaching ingress BPF program to cgroup %s failed: %m", path
);
726 crt
->ip_bpf_ingress_installed
= TAKE_PTR(crt
->ip_bpf_ingress
);
729 /* And now, definitely get rid of the old programs, and detach them */
730 ip_bpf_egress_uninstall
= bpf_program_free(ip_bpf_egress_uninstall
);
731 ip_bpf_ingress_uninstall
= bpf_program_free(ip_bpf_ingress_uninstall
);
733 r
= attach_custom_bpf_progs(u
, path
, BPF_CGROUP_INET_EGRESS
, &crt
->ip_bpf_custom_egress
, &crt
->ip_bpf_custom_egress_installed
);
737 r
= attach_custom_bpf_progs(u
, path
, BPF_CGROUP_INET_INGRESS
, &crt
->ip_bpf_custom_ingress
, &crt
->ip_bpf_custom_ingress_installed
);
744 int bpf_firewall_read_accounting(int map_fd
, uint64_t *ret_bytes
, uint64_t *ret_packets
) {
745 uint64_t key
, packets
;
752 key
= MAP_KEY_PACKETS
;
753 r
= bpf_map_lookup_element(map_fd
, &key
, &packets
);
760 r
= bpf_map_lookup_element(map_fd
, &key
, ret_bytes
);
766 *ret_packets
= packets
;
771 int bpf_firewall_reset_accounting(int map_fd
) {
772 uint64_t key
, value
= 0;
778 key
= MAP_KEY_PACKETS
;
779 r
= bpf_map_update_element(map_fd
, &key
, &value
);
784 return bpf_map_update_element(map_fd
, &key
, &value
);
787 void emit_bpf_firewall_warning(Unit
*u
) {
788 static bool warned
= false;
794 if (warned
|| MANAGER_IS_TEST_RUN(u
->manager
))
797 r
= bpf_program_supported();
800 bool quiet
= ERRNO_IS_NEG_PRIVILEGE(r
) && detect_container() > 0;
802 log_unit_full_errno(u
, quiet
? LOG_DEBUG
: LOG_WARNING
, r
,
803 "unit configures an IP firewall, but %s.\n"
804 "(This warning is only shown for the first unit using IP firewalling.)",
805 getuid() != 0 ? "not running as root" :
806 "the local system does not support BPF/cgroup firewalling");
810 void bpf_firewall_close(CGroupRuntime
*crt
) {
813 crt
->ip_accounting_ingress_map_fd
= safe_close(crt
->ip_accounting_ingress_map_fd
);
814 crt
->ip_accounting_egress_map_fd
= safe_close(crt
->ip_accounting_egress_map_fd
);
816 crt
->ipv4_allow_map_fd
= safe_close(crt
->ipv4_allow_map_fd
);
817 crt
->ipv6_allow_map_fd
= safe_close(crt
->ipv6_allow_map_fd
);
818 crt
->ipv4_deny_map_fd
= safe_close(crt
->ipv4_deny_map_fd
);
819 crt
->ipv6_deny_map_fd
= safe_close(crt
->ipv6_deny_map_fd
);
821 crt
->ip_bpf_ingress
= bpf_program_free(crt
->ip_bpf_ingress
);
822 crt
->ip_bpf_ingress_installed
= bpf_program_free(crt
->ip_bpf_ingress_installed
);
823 crt
->ip_bpf_egress
= bpf_program_free(crt
->ip_bpf_egress
);
824 crt
->ip_bpf_egress_installed
= bpf_program_free(crt
->ip_bpf_egress_installed
);
826 crt
->ip_bpf_custom_ingress
= set_free(crt
->ip_bpf_custom_ingress
);
827 crt
->ip_bpf_custom_egress
= set_free(crt
->ip_bpf_custom_egress
);
828 crt
->ip_bpf_custom_ingress_installed
= set_free(crt
->ip_bpf_custom_ingress_installed
);
829 crt
->ip_bpf_custom_egress_installed
= set_free(crt
->ip_bpf_custom_egress_installed
);