1111 * of the License.
1212 */
1313
14+ #define _GNU_SOURCE
15+
1416#include <stdlib.h>
1517#include <stdio.h>
1618#include <string.h>
@@ -66,6 +68,26 @@ static void ext_ignore(char *fmt, ...)
6668 __attribute__((unused )) int ret = system (cmd );
6769}
6870
71+ static void reset_rules (void )
72+ {
73+ /* reset all rules in case the running fw changes */
74+ ext_ignore ("%s/firewall-cmd --permanent --direct --quiet --remove-rule ipv4 filter INPUT 1 -m set --match-set tallow src -j DROP" , ipt_path );
75+ ext_ignore ("%s/firewall-cmd --quiet --permanent --delete-ipset=tallow" , ipt_path );
76+
77+ /* delete iptables ref to set before the ipset! */
78+ ext_ignore ("%s/iptables -t filter -D INPUT -m set --match-set tallow src -j DROP 2> /dev/null" , ipt_path );
79+ ext_ignore ("%s/ipset destroy tallow 2> /dev/null" , ipt_path );
80+
81+ if (has_ipv6 ) {
82+ ext_ignore ("%s/firewall-cmd --permanent --direct --quiet --remove-rule ipv6 filter INPUT 1 -m set --match-set tallow6 src -j DROP" , ipt_path );
83+ ext_ignore ("%s/firewall-cmd --permanent --delete-ipset=tallow6 --quiet" , ipt_path );
84+
85+ /* delete iptables ref to set before the ipset! */
86+ ext_ignore ("%s/ip6tables -t filter -D INPUT -m set --match-set tallow6 src -j DROP 2> /dev/null" , ipt_path );
87+ ext_ignore ("%s/ipset destroy tallow6 2> /dev/null" , ipt_path );
88+ }
89+ }
90+
6991static void setup (void )
7092{
7193 static bool done = false;
@@ -76,31 +98,76 @@ static void setup(void)
7698 if (nocreate )
7799 return ;
78100
79- /* init ipset and iptables */
80- /* delete iptables ref to set before the ipset! */
81- ext_ignore ("%s/iptables -t filter -D INPUT -m set --match-set tallow src -j DROP 2> /dev/null" , ipt_path );
82- ext_ignore ("%s/ipset destroy tallow 2> /dev/null" , ipt_path );
83- if (ext ("%s/ipset create tallow hash:ip family inet timeout %d" , ipt_path , expires )) {
84- fprintf (stderr , "Unable to create ipv4 ipset.\n" );
101+ /* firewalld */
102+ char * fwd_path ;
103+ if (asprintf (& fwd_path , "%s/firewall-cmd" , ipt_path ) < 0 )
104+ {
105+ fprintf (stderr , "Unable to allocate buffer for path to firewall-cmd.\n" );
85106 exit (EXIT_FAILURE );
86107 }
87- if (ext ("%s/iptables -t filter -I INPUT 1 -m set --match-set tallow src -j DROP" , ipt_path )) {
88- fprintf (stderr , "Unable to create iptables rule.\n" );
89- exit (EXIT_FAILURE );
108+
109+ if ((access (fwd_path , X_OK ) == 0 ) && ext ("%s/firewall-cmd --state --quiet" , ipt_path ) == 0 ) {
110+ fprintf (stdout , "firewalld is running and will be used by tallow.\n" );
111+
112+ reset_rules ();
113+
114+ /* create ipv4 rule and ipset */
115+ if (ext ("%s/firewall-cmd --permanent --quiet --new-ipset=tallow --type=hash:ip --family=inet --option=timeout=%d" , ipt_path , expires )) {
116+ fprintf (stderr , "Unable to create ipv4 ipset with firewall-cmd.\n" );
117+ exit (EXIT_FAILURE );
118+ }
119+ if (ext ("%s/firewall-cmd --permanent --direct --quiet --add-rule ipv4 filter INPUT 1 -m set --match-set tallow src -j DROP" , ipt_path )) {
120+ fprintf (stderr , "Unable to create ipv4 firewalld rule.\n" );
121+ exit (EXIT_FAILURE );
122+ }
123+
124+ /* create ipv6 rule and ipset */
125+ if (has_ipv6 ) {
126+ if (ext ("%s/firewall-cmd --permanent --quiet --new-ipset=tallow6 --type=hash:ip --family=inet6 --option=timeout=%d" , ipt_path , expires )) {
127+ fprintf (stderr , "Unable to create ipv6 ipset with firewall-cmd.\n" );
128+ exit (EXIT_FAILURE );
129+ }
130+ if (ext ("%s/firewall-cmd --permanent --direct --quiet --add-rule ipv6 filter INPUT 1 -m set --match-set tallow6 src -j DROP " , ipt_path )) {
131+ fprintf (stderr , "Unable to create ipv6 firewalld rule.\n" );
132+ exit (EXIT_FAILURE );
133+ }
134+ }
135+
136+ /* reload firewalld for ipsets to load */
137+ if (ext ("%s/firewall-cmd --reload --quiet" , ipt_path , expires )) {
138+ fprintf (stderr , "Unable to reload firewalld rules.\n" );
139+ exit (EXIT_FAILURE );
140+ }
90141 }
142+ /* iptables */
143+ else {
91144
92- if ( has_ipv6 ) {
93- ext_ignore ( "%s/ip6tables -t filter -D INPUT -m set --match-set tallow6 src -j DROP 2> /dev/null" , ipt_path );
94- ext_ignore ( "%s/ipset destroy tallow6 2> /dev/null" , ipt_path );
95- if (ext ("%s/ipset create tallow6 hash:ip family inet6 timeout %d" , ipt_path , expires )) {
96- fprintf (stderr , "Unable to create ipv6 ipset.\n" );
145+ reset_rules ();
146+
147+ /* create ipv4 rule and ipset */
148+ if (ext ("%s/ipset create tallow hash:ip family inet timeout %d" , ipt_path , expires )) {
149+ fprintf (stderr , "Unable to create ipv4 ipset.\n" );
97150 exit (EXIT_FAILURE );
98151 }
99- if (ext ("%s/ip6tables -t filter -I INPUT 1 -m set --match-set tallow6 src -j DROP" , ipt_path )) {
100- fprintf (stderr , "Unable to create ipt6ables rule.\n" );
152+ if (ext ("%s/iptables -t filter -A INPUT -m set --match-set tallow src -j DROP" , ipt_path )) {
153+ fprintf (stderr , "Unable to create iptables rule.\n" );
101154 exit (EXIT_FAILURE );
102155 }
156+
157+ /* create ipv6 rule and ipset */
158+ if (has_ipv6 ) {
159+ if (ext ("%s/ipset create tallow6 hash:ip family inet6 timeout %d" , ipt_path , expires )) {
160+ fprintf (stderr , "Unable to create ipv6 ipset.\n" );
161+ exit (EXIT_FAILURE );
162+ }
163+ if (ext ("%s/ip6tables -t filter -A INPUT -m set --match-set tallow6 src -j DROP" , ipt_path )) {
164+ fprintf (stderr , "Unable to create ipt6ables rule.\n" );
165+ exit (EXIT_FAILURE );
166+ }
167+ }
103168 }
169+
170+ free (fwd_path );
104171}
105172
106173static void block (struct block_struct * s , int instant_block )
0 commit comments