@@ -192,7 +192,7 @@ static int bpf_firewall_compile_bpf(
192192 BPF_MOV64_IMM (BPF_REG_0 , 0 ),
193193 };
194194
195- _cleanup_ (bpf_program_unrefp ) BPFProgram * p = NULL ;
195+ _cleanup_ (bpf_program_freep ) BPFProgram * p = NULL ;
196196 int accounting_map_fd , r ;
197197 bool access_enabled ;
198198
@@ -555,8 +555,8 @@ int bpf_firewall_compile(Unit *u) {
555555 * but we reuse the accounting maps. That way the firewall in effect always maps to the actual
556556 * configuration, but we don't flush out the accounting unnecessarily */
557557
558- u -> ip_bpf_ingress = bpf_program_unref (u -> ip_bpf_ingress );
559- u -> ip_bpf_egress = bpf_program_unref (u -> ip_bpf_egress );
558+ u -> ip_bpf_ingress = bpf_program_free (u -> ip_bpf_ingress );
559+ u -> ip_bpf_egress = bpf_program_free (u -> ip_bpf_egress );
560560
561561 u -> ipv4_allow_map_fd = safe_close (u -> ipv4_allow_map_fd );
562562 u -> ipv4_deny_map_fd = safe_close (u -> ipv4_deny_map_fd );
@@ -601,7 +601,7 @@ static int load_bpf_progs_from_fs_to_set(Unit *u, char **filter_paths, Set **set
601601 set_clear (* set );
602602
603603 STRV_FOREACH (bpf_fs_path , filter_paths ) {
604- _cleanup_ (bpf_program_unrefp ) BPFProgram * prog = NULL ;
604+ _cleanup_ (bpf_program_freep ) BPFProgram * prog = NULL ;
605605 int r ;
606606
607607 r = bpf_program_new (BPF_PROG_TYPE_CGROUP_SKB , & prog );
@@ -657,25 +657,18 @@ static int attach_custom_bpf_progs(Unit *u, const char *path, int attach_type, S
657657 assert (u );
658658
659659 set_clear (* set_installed );
660+ set_ensure_allocated (set_installed , & bpf_program_hash_ops );
660661
661- SET_FOREACH (prog , * set ) {
662+ SET_FOREACH_MOVE (prog , * set_installed , * set ) {
662663 r = bpf_program_cgroup_attach (prog , attach_type , path , BPF_F_ALLOW_MULTI );
663664 if (r < 0 )
664665 return log_unit_error_errno (u , r , "Attaching custom egress BPF program to cgroup %s failed: %m" , path );
665-
666- /* Remember that these BPF programs are installed now. */
667- r = set_ensure_put (set_installed , & bpf_program_hash_ops , prog );
668- if (r < 0 )
669- return log_unit_error_errno (u , r , "Can't add program to BPF program set: %m" );
670-
671- bpf_program_ref (prog );
672666 }
673-
674667 return 0 ;
675668}
676669
677670int bpf_firewall_install (Unit * u ) {
678- _cleanup_ (bpf_program_unrefp ) BPFProgram * ip_bpf_ingress_uninstall = NULL , * ip_bpf_egress_uninstall = NULL ;
671+ _cleanup_ (bpf_program_freep ) BPFProgram * ip_bpf_ingress_uninstall = NULL , * ip_bpf_egress_uninstall = NULL ;
679672 _cleanup_free_ char * path = NULL ;
680673 CGroupContext * cc ;
681674 int r , supported ;
@@ -719,8 +712,8 @@ int bpf_firewall_install(Unit *u) {
719712 /* If we don't have BPF_F_ALLOW_MULTI then unref the old BPF programs (which will implicitly
720713 * detach them) right before attaching the new program, to minimize the time window when we
721714 * don't account for IP traffic. */
722- u -> ip_bpf_egress_installed = bpf_program_unref (u -> ip_bpf_egress_installed );
723- u -> ip_bpf_ingress_installed = bpf_program_unref (u -> ip_bpf_ingress_installed );
715+ u -> ip_bpf_egress_installed = bpf_program_free (u -> ip_bpf_egress_installed );
716+ u -> ip_bpf_ingress_installed = bpf_program_free (u -> ip_bpf_ingress_installed );
724717 }
725718
726719 if (u -> ip_bpf_egress ) {
@@ -729,20 +722,20 @@ int bpf_firewall_install(Unit *u) {
729722 return log_unit_error_errno (u , r , "Attaching egress BPF program to cgroup %s failed: %m" , path );
730723
731724 /* Remember that this BPF program is installed now. */
732- u -> ip_bpf_egress_installed = bpf_program_ref (u -> ip_bpf_egress );
725+ u -> ip_bpf_egress_installed = TAKE_PTR (u -> ip_bpf_egress );
733726 }
734727
735728 if (u -> ip_bpf_ingress ) {
736729 r = bpf_program_cgroup_attach (u -> ip_bpf_ingress , BPF_CGROUP_INET_INGRESS , path , flags );
737730 if (r < 0 )
738731 return log_unit_error_errno (u , r , "Attaching ingress BPF program to cgroup %s failed: %m" , path );
739732
740- u -> ip_bpf_ingress_installed = bpf_program_ref (u -> ip_bpf_ingress );
733+ u -> ip_bpf_ingress_installed = TAKE_PTR (u -> ip_bpf_ingress );
741734 }
742735
743736 /* And now, definitely get rid of the old programs, and detach them */
744- ip_bpf_egress_uninstall = bpf_program_unref (ip_bpf_egress_uninstall );
745- ip_bpf_ingress_uninstall = bpf_program_unref (ip_bpf_ingress_uninstall );
737+ ip_bpf_egress_uninstall = bpf_program_free (ip_bpf_egress_uninstall );
738+ ip_bpf_ingress_uninstall = bpf_program_free (ip_bpf_ingress_uninstall );
746739
747740 r = attach_custom_bpf_progs (u , path , BPF_CGROUP_INET_EGRESS , & u -> ip_bpf_custom_egress , & u -> ip_bpf_custom_egress_installed );
748741 if (r < 0 )
@@ -806,7 +799,7 @@ int bpf_firewall_supported(void) {
806799 BPF_EXIT_INSN ()
807800 };
808801
809- _cleanup_ (bpf_program_unrefp ) BPFProgram * program = NULL ;
802+ _cleanup_ (bpf_program_freep ) BPFProgram * program = NULL ;
810803 static int supported = -1 ;
811804 union bpf_attr attr ;
812805 int r ;
@@ -936,10 +929,10 @@ void bpf_firewall_close(Unit *u) {
936929 u -> ipv4_deny_map_fd = safe_close (u -> ipv4_deny_map_fd );
937930 u -> ipv6_deny_map_fd = safe_close (u -> ipv6_deny_map_fd );
938931
939- u -> ip_bpf_ingress = bpf_program_unref (u -> ip_bpf_ingress );
940- u -> ip_bpf_ingress_installed = bpf_program_unref (u -> ip_bpf_ingress_installed );
941- u -> ip_bpf_egress = bpf_program_unref (u -> ip_bpf_egress );
942- u -> ip_bpf_egress_installed = bpf_program_unref (u -> ip_bpf_egress_installed );
932+ u -> ip_bpf_ingress = bpf_program_free (u -> ip_bpf_ingress );
933+ u -> ip_bpf_ingress_installed = bpf_program_free (u -> ip_bpf_ingress_installed );
934+ u -> ip_bpf_egress = bpf_program_free (u -> ip_bpf_egress );
935+ u -> ip_bpf_egress_installed = bpf_program_free (u -> ip_bpf_egress_installed );
943936
944937 u -> ip_bpf_custom_ingress = set_free (u -> ip_bpf_custom_ingress );
945938 u -> ip_bpf_custom_egress = set_free (u -> ip_bpf_custom_egress );
0 commit comments