| 1 | package App::SilverSplash::IPTables; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use base 'App::SilverSplash'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Data::Dumper qw(Dumper); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Config::SL (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use URI::Escape (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use constant DEBUG => $ENV{SL_DEBUG} || 0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'SL_DEBUG'} || 0
MC/DC0%0/1
$ENV{'SL_DEBUG'} || 0
$ENV{'SL_DEBUG'}
Truth table50%1/2
A
A= $ENV{'SL_DEBUG'}
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | our ( | ||||||||||||||||||||||||||||||||
| 16 | 1 | $Config, $Iptables, $Wan_if, %tables_chains, | |||||||||||||||||||||||||||||||
| 17 | $Lan_if, $Perlbal_port, $Mark_op, $Lease_file, | ||||||||||||||||||||||||||||||||
| 18 | $Gateway_ip, $Wan_ip, $Wan_mac, | ||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=7 SCAR=20.6
| |||||||||||||||||||||||||||||||||
| 22 | 1 | $Config = Config::SL->new; | |||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | $Iptables = $Config->sl_iptables || die 'oops'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$App::SilverSplash::IPTables::Config->sl_iptables || die('oops')
MC/DC0%0/1
$App::SilverSplash::IPTables::Config->sl_iptables || die('oops')
$App::SilverSplash::IPTables::Config->sl_iptables
Truth table50%1/2
A
A= $App::SilverSplash::IPTables::Config->sl_iptables
| |||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | $Perlbal_port = $Config->sl_perlbal_port || die 'oops'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$App::SilverSplash::IPTables::Config->sl_perlbal_port || die('oops')
MC/DC0%0/1
$App::SilverSplash::IPTables::Config->sl_perlbal_port || die('oops')
$App::SilverSplash::IPTables::Config->sl_perlbal_port
Truth table50%1/2
A
A= $App::SilverSplash::IPTables::Config->sl_perlbal_port
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # wan and lan interfaces | ||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | $Wan_if = $Config->sl_wan_if || die 'oops'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$App::SilverSplash::IPTables::Config->sl_wan_if || die('oops')
MC/DC0%0/1
$App::SilverSplash::IPTables::Config->sl_wan_if || die('oops')
$App::SilverSplash::IPTables::Config->sl_wan_if
Truth table50%1/2
A
A= $App::SilverSplash::IPTables::Config->sl_wan_if
| |||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | $Lan_if = $Config->sl_lan_if || die 'oops'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$App::SilverSplash::IPTables::Config->sl_lan_if || die('oops')
MC/DC0%0/1
$App::SilverSplash::IPTables::Config->sl_lan_if || die('oops')
$App::SilverSplash::IPTables::Config->sl_lan_if
Truth table50%1/2
A
A= $App::SilverSplash::IPTables::Config->sl_lan_if
| |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 1 | ($Gateway_ip) = `/sbin/ifconfig $Lan_if` =~ m/inet addr:(\S+)/; | |||||||||||||||||||||||||||||||
| 31 | 1 | ($Wan_ip) = `/sbin/ifconfig $Wan_if` =~ m/inet addr:(\S+)/; | |||||||||||||||||||||||||||||||
| 32 | 1 | ($Wan_mac) = `/sbin/ifconfig $Wan_if` =~ m/HWaddr\s(\S+)/; | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $Mark_op = $Config->sl_mark_op || die 'oops'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$App::SilverSplash::IPTables::Config->sl_mark_op || die('oops')
MC/DC0%0/1
$App::SilverSplash::IPTables::Config->sl_mark_op || die('oops')
$App::SilverSplash::IPTables::Config->sl_mark_op
Truth table50%1/2
A
A= $App::SilverSplash::IPTables::Config->sl_mark_op
| |||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | $Lease_file = $Config->sl_dhcp_lease_file || die 'oops'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$App::SilverSplash::IPTables::Config->sl_dhcp_lease_file || die('oops')
MC/DC0%0/1
$App::SilverSplash::IPTables::Config->sl_dhcp_lease_file || die('oops')
$App::SilverSplash::IPTables::Config->sl_dhcp_lease_file
Truth table50%1/2
A
A= $App::SilverSplash::IPTables::Config->sl_dhcp_lease_file
| |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 1 | %tables_chains = ( | |||||||||||||||||||||||||||||||
| 38 | filter => [qw( slAUT slAUTads slNET slRTR )], | ||||||||||||||||||||||||||||||||
| 39 | mangle => [qw( slBLK slINC slOUT slTRU )], | ||||||||||||||||||||||||||||||||
| 40 | nat => [qw( slOUT )], | ||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 1 | our $Blocked_mark = '0x100'; | |||||||||||||||||||||||||||||||
| 46 | 1 | our $Trusted_mark = '0x200'; | |||||||||||||||||||||||||||||||
| 47 | 1 | our $Paid_mark = '0x400'; | |||||||||||||||||||||||||||||||
| 48 | 1 | our $Ads_mark = '0x500'; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub load_allows { | ||||||||||||||||||||||||||||||
|
Subroutine
load_allows: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 51 | 0 | my ( $class, $file ) = @_; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 0 | my $fh; | |||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | open( $fh, '<', $Config->sl_root . "/conf/$file" ) or die $!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 0 | my $ct = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | close($fh) or die $!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | my @lines = split( /\n/, $ct ); | |||||||||||||||||||||||||||||||
| 59 | 0 | @lines = grep { $_ =~ m/\S/ } | |||||||||||||||||||||||||||||||
| 60 | 0 | grep { $_ !~ /#/ } # skip comments | |||||||||||||||||||||||||||||||
| 61 | 0 | grep { defined $_ } @lines; # skip undef | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 0 | return \@lines; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub init_firewall { | ||||||||||||||||||||||||||||||
|
Subroutine
init_firewall: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 67 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 0 | `echo 1 > /proc/sys/net/ipv4/ip_forward`; | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | # flush the existing firewall | ||||||||||||||||||||||||||||||||
| 72 | 0 | $class->clear_firewall(); | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | # create the chains | ||||||||||||||||||||||||||||||||
| 75 | 0 | foreach my $table ( sort keys %tables_chains ) { | |||||||||||||||||||||||||||||||
| 76 | 0 | foreach my $chain ( @{ $tables_chains{$table} } ) { | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 0 | iptables("-t $table -N $chain"); | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | # walled garden exceptions | ||||||||||||||||||||||||||||||||
| 83 | 0 | my $hosts_allow = $class->load_allows('cp_hosts_allow.txt'); | |||||||||||||||||||||||||||||||
| 84 | 0 | my $sslhosts_allow = $class->load_allows('cp_sslhosts_allow.txt'); | |||||||||||||||||||||||||||||||
| 85 | 0 | my $accept = "slNET -d %s -p tcp -m tcp --dport %d -j ACCEPT"; | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 0 | my $slout = "slOUT -d %s -p tcp -m tcp --dport %d -j ACCEPT"; | |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | my $hosts_accept = | ||||||||||||||||||||||||||||||||
| 90 | 0 | join( "\n", map { sprintf( $accept, $_, 80 ) } @{$hosts_allow} ); | |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | my $sslhosts_accept = | ||||||||||||||||||||||||||||||||
| 93 | 0 | join( "\n", map { sprintf( $accept, $_, 443 ) } @{$sslhosts_allow} ); | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | my $hosts_slout = | ||||||||||||||||||||||||||||||||
| 96 | 0 | join( "\n", map { sprintf( $slout, $_, 80 ) } @{$hosts_allow} ); | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | my $sslhosts_slout = | ||||||||||||||||||||||||||||||||
| 99 | 0 | join( "\n", map { sprintf( $slout, $_, 443 ) } @{$sslhosts_allow} ); | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | ############################## | ||||||||||||||||||||||||||||||||
| 102 | # add the filter default chains | ||||||||||||||||||||||||||||||||
| 103 | 0 | my $filters = <<"FILTERS"; | |||||||||||||||||||||||||||||||
| 104 | INPUT -i $Lan_if -j slRTR | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | FORWARD -i $Lan_if -j slNET | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | slAUT --protocol tcp --source-port ! 25 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | slAUTads -m state --state RELATED,ESTABLISHED -j ACCEPT | ||||||||||||||||||||||||||||||||
| 111 | slAUTads -p tcp -m tcp --dport 22 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 112 | slAUTads -p tcp -m tcp --dport 80 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 113 | slAUTads -p tcp -m tcp --dport 110 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 114 | slAUTads -p tcp -m tcp --dport 143 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 115 | slAUTads -p tcp -m tcp --dport 443 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 116 | slAUTads -p tcp -m tcp --dport 465 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 117 | slAUTads -p udp -m udp --dport 500 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 118 | slAUTads -p tcp -m tcp --dport 587 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 119 | slAUTads -p tcp -m tcp --dport 993 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 120 | slAUTads -p tcp -m tcp --dport 995 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 121 | slAUTads -p tcp -m tcp --dport 1723 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 122 | slAUTads -p udp -m udp --dport 1701 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 123 | slAUTads -p tcp -m tcp --dport 3389 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 124 | slAUTads -p tcp -m tcp --dport 5050 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 125 | slAUTads -p tcp -m tcp --dport 5190 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 126 | slAUTads -p tcp -m tcp --dport 5222 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 127 | slAUTads -p tcp -m tcp --dport 5223 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | slNET -m mark --mark $Blocked_mark/0x700 -j DROP | ||||||||||||||||||||||||||||||||
| 130 | slNET -m state --state INVALID -j DROP | ||||||||||||||||||||||||||||||||
| 131 | slNET -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu | ||||||||||||||||||||||||||||||||
| 132 | slNET -m mark --mark $Trusted_mark/0x700 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 133 | slNET -m mark --mark $Paid_mark/0x700 -j slAUT | ||||||||||||||||||||||||||||||||
| 134 | slNET -m mark --mark $Ads_mark/0x700 -j slAUTads | ||||||||||||||||||||||||||||||||
| 135 | slNET -p icmp -j REJECT --reject-with icmp-port-unreachable | ||||||||||||||||||||||||||||||||
| 136 | $hosts_accept | ||||||||||||||||||||||||||||||||
| 137 | $sslhosts_accept | ||||||||||||||||||||||||||||||||
| 138 | slNET -j DROP | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | slRTR -m mark --mark $Blocked_mark/0x700 -j DROP | ||||||||||||||||||||||||||||||||
| 141 | slRTR -m state --state INVALID -j DROP | ||||||||||||||||||||||||||||||||
| 142 | slRTR -m state --state RELATED,ESTABLISHED -j ACCEPT | ||||||||||||||||||||||||||||||||
| 143 | slRTR -p tcp -m tcp ! --tcp-option 2 --tcp-flags SYN SYN -j DROP | ||||||||||||||||||||||||||||||||
| 144 | slRTR -p tcp -m tcp --dport $Perlbal_port -j ACCEPT | ||||||||||||||||||||||||||||||||
| 145 | slRTR -m mark --mark $Trusted_mark/0x700 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 146 | slRTR -p udp -m udp -s 10.69.0.1/16 --dport 53 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 147 | slRTR -p udp -m udp -s 10.69.0.1/16 --dport 67 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 148 | slRTR -p udp -m udp -s 10.69.0.1/16 --dport 68 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 149 | slRTR -p tcp -m tcp -s 10.69.0.1/16 --dport 20022 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 150 | slRTR -p icmp -s 10.69.0.1/16 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 151 | FILTERS | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 0 | add_rules( 'filter', $filters ); | |||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | ############################# | ||||||||||||||||||||||||||||||||
| 156 | # default mangle chains | ||||||||||||||||||||||||||||||||
| 157 | 0 | my $mangles = <<"MANGLES"; | |||||||||||||||||||||||||||||||
| 158 | PREROUTING -i $Lan_if -j slOUT | ||||||||||||||||||||||||||||||||
| 159 | PREROUTING -i $Lan_if -j slBLK | ||||||||||||||||||||||||||||||||
| 160 | PREROUTING -i $Lan_if -j slTRU | ||||||||||||||||||||||||||||||||
| 161 | POSTROUTING -o $Lan_if -j slINC | ||||||||||||||||||||||||||||||||
| 162 | MANGLES | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 0 | add_rules( 'mangle', $mangles ); | |||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | ############################# | ||||||||||||||||||||||||||||||||
| 167 | # default nat chains | ||||||||||||||||||||||||||||||||
| 168 | 0 | my $nats = <<"NATS"; | |||||||||||||||||||||||||||||||
| 169 | PREROUTING -i $Lan_if -j slOUT | ||||||||||||||||||||||||||||||||
| 170 | POSTROUTING -o $Wan_if -j MASQUERADE | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | slOUT -m mark --mark $Trusted_mark/0x700 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 173 | slOUT -m mark --mark $Paid_mark/0x700 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 174 | slOUT -m mark --mark $Ads_mark/0x700 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 175 | $hosts_slout | ||||||||||||||||||||||||||||||||
| 176 | $sslhosts_slout | ||||||||||||||||||||||||||||||||
| 177 | slOUT -p tcp -m tcp --dport 80 -j DNAT --to-destination $Gateway_ip:$Perlbal_port | ||||||||||||||||||||||||||||||||
| 178 | slOUT -p tcp -m tcp --dport 443 -j DNAT --to-destination $Gateway_ip:$Perlbal_port | ||||||||||||||||||||||||||||||||
| 179 | slOUT -p udp --dport 53 -d $Gateway_ip -j ACCEPT | ||||||||||||||||||||||||||||||||
| 180 | slOUT -p udp --dport 67 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 181 | slOUT -p udp --dport 68 -j ACCEPT | ||||||||||||||||||||||||||||||||
| 182 | slOUT -p tcp --dport 20022 -d $Gateway_ip -j ACCEPT | ||||||||||||||||||||||||||||||||
| 183 | slOUT -p tcp --dport $Perlbal_port -d $Gateway_ip -j ACCEPT | ||||||||||||||||||||||||||||||||
| 184 | slOUT -j DROP | ||||||||||||||||||||||||||||||||
| 185 | NATS | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 0 | add_rules( 'nat', $nats ); | |||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | # trusted hosts | ||||||||||||||||||||||||||||||||
| 190 | 0 | my $trusted_hosts = $class->load_allows('trusted_hosts.txt'); | |||||||||||||||||||||||||||||||
| 191 | 0 | my $out_rule = | |||||||||||||||||||||||||||||||
| 192 | "-t mangle -A slTRU -m mac --mac-source %s -j MARK $Mark_op $Trusted_mark"; | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | # TODO - arp translation | ||||||||||||||||||||||||||||||||
| 195 | #my $out_rule = "-t mangle -A slTRU -s %s -m mac --mac-source %s -j MARK $Mark_op $Trusted_mark"; | ||||||||||||||||||||||||||||||||
| 196 | #my $in_rule = "-t mangle -A slINC -d %s -j ACCEPT"; | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 0 | foreach my $mac ( @{$trusted_hosts} ) { | |||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | # TODO - arp translation | ||||||||||||||||||||||||||||||||
| 201 | # my $ip = App::SilverSplash->ip_from_mac($mac); | ||||||||||||||||||||||||||||||||
| 202 | # iptables($in_rule, $ip); | ||||||||||||||||||||||||||||||||
| 203 | 0 | iptables( sprintf( $out_rule, $mac ) ); | |||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | sub add_rules { | ||||||||||||||||||||||||||||||
|
Subroutine
add_rules: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 209 | 0 | my ( $table, $rules ) = @_; | |||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | 0 | foreach my $rule ( split( /\n/, $rules ) ) { | |||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 0 | chomp($rule); | |||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | next unless $rule =~ m/\S/; # skip blanks | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | 0 | warn("$$ Adding rule $rule to table $table\n") if DEBUG; | |||||||||||||||||||||||||||||||
| 216 | 0 | iptables("-t $table -A $rule"); | |||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | sub clear_firewall { | ||||||||||||||||||||||||||||||
|
Subroutine
clear_firewall: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 221 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | # clear all tables | ||||||||||||||||||||||||||||||||
| 224 | 0 | iptables("-t $_ -F") for keys %tables_chains; | |||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | # clear all chains | ||||||||||||||||||||||||||||||||
| 227 | 0 | iptables("-t $_ -X") for keys %tables_chains; | |||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | # reset the postrouting rule - unsure if this is needed | ||||||||||||||||||||||||||||||||
| 230 | # iptables("-t nat -A POSTROUTING -o $Wan_if -j MASQUERADE"); | ||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | sub iptables { | ||||||||||||||||||||||||||||||
|
Subroutine
iptables: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 234 | 0 | my $cmd = shift; | |||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | system("sudo $Iptables $cmd") == 0 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
system("sudo $App::SilverSplash::IPTables::Iptables $cmd") == 0 || require Carp && Carp::confess("could not $App::SilverSplash::IPTables::Iptables '${cmd}', err: $!, ret: $?\n")
require Carp and Carp::confess("could not $App::SilverSplash::IPTables::Iptables '${cmd}', err: $!, ret: $?\n")
MC/DC0%0/2
system("sudo $App::SilverSplash::IPTables::Iptables $cmd") == 0 || require Carp && Carp::confess("could not $App::SilverSplash::IPTables::Iptables '${cmd}', err: $!, ret: $?\n")
system("sudo $App::SilverSplash::IPTables::Iptables $cmd") == 0
require Carp
Truth table0%0/3
A || B
A= system("sudo $App::SilverSplash::IPTables::Iptables $cmd") == 0B= require Carp
| |||||||||||||||||||||||||||||||||
| 236 | or require Carp | ||||||||||||||||||||||||||||||||
| 237 | && Carp::confess "could not $Iptables '$cmd', err: $!, ret: $?\n"; | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | sub fixup_access { | ||||||||||||||||||||||||||||||
|
Subroutine
fixup_access: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 243 | 0 | my ( $class, $mac, $ip, $type ) = @_; | |||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | 0 | my $uc_mac = uc($mac); | |||||||||||||||||||||||||||||||
| 246 | 0 | my $iptables_rule = `sudo $Iptables -t mangle -L -v`; | |||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||
| 248 | # see if the mac address is in a rule | ||||||||||||||||||||||||||||||||
| 249 | 0 | my ($iptables_ip) = $iptables_rule =~ | |||||||||||||||||||||||||||||||
| 250 | m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?MAC\s+$uc_mac/i; | ||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | 0 | my $chain = "_$type\_chain"; | |||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | if ( !$iptables_ip ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | 0 | warn("no rule for authed mac $mac, adding") if DEBUG; | |||||||||||||||||||||||||||||||
| 256 | 0 | $class->$chain( 'A', $mac, $ip ); | |||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||
| 259 | elsif ( $ip ne $iptables_ip ) { | ||||||||||||||||||||||||||||||||
| 260 | 0 | warn("iptables rules don't match, updating") if DEBUG; | |||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | # dhcp lease probably expired, delete old rule, create new rule | ||||||||||||||||||||||||||||||||
| 263 | 0 | my $delete = "delete_from_$type\_chain"; | |||||||||||||||||||||||||||||||
| 264 | 0 | $class->$delete( $mac, $iptables_ip ); | |||||||||||||||||||||||||||||||
| 265 | 0 | $class->$chain( 'A', $mac, $ip ); | |||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||
| 267 | elsif ( $ip eq $iptables_ip ) { | ||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | # no-op | ||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||
| 271 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | sub paid_users { | ||||||||||||||||||||||||||||||
|
Subroutine
paid_users: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 275 | 0 | my ($class) = @_; | |||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | 0 | return $class->users($Paid_mark); | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | sub ads_users { | ||||||||||||||||||||||||||||||
|
Subroutine
ads_users: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 281 | 0 | my ($class) = @_; | |||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | 0 | return $class->users($Ads_mark); | |||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | sub users { | ||||||||||||||||||||||||||||||
|
Subroutine
users: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 287 | 0 | my ( $class, $mark ) = @_; | |||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | my @users = | ||||||||||||||||||||||||||||||||
| 290 | 0 | map { [ $_ =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?MAC\s(\S+)\s/ ] } | |||||||||||||||||||||||||||||||
| 291 | 0 | grep { $_ =~ m/(?:$mark)/ } | |||||||||||||||||||||||||||||||
| 292 | split( '\n', `sudo $Iptables -t mangle --list` ); | ||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | 0 | return @users; | |||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | sub _paid_chain { | ||||||||||||||||||||||||||||||
|
Subroutine
_paid_chain: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 298 | 0 | my ( $class, $op, $mac, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 299 | 0 | iptables( | |||||||||||||||||||||||||||||||
| 300 | "-t mangle -$op slOUT -s $ip -m mac --mac-source $mac -j MARK $Mark_op $Paid_mark" | ||||||||||||||||||||||||||||||||
| 301 | ); | ||||||||||||||||||||||||||||||||
| 302 | 0 | iptables("-t mangle -$op slINC -d $ip -j ACCEPT"); | |||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | sub add_to_paid_chain { | ||||||||||||||||||||||||||||||
|
Subroutine
add_to_paid_chain: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 306 | 0 | my ( $class, $mac, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | 0 | my $esc_mac = URI::Escape::uri_escape($mac); | |||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | # convert minutes to seconds | ||||||||||||||||||||||||||||||||
| 311 | 0 | my $stay = time() + 240 * 60; # 4 hours | |||||||||||||||||||||||||||||||
| 312 | 0 | $class->set( $mac => "$stay|paid" ); | |||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||
| 314 | 0 | warn("cache set $mac => $stay") if DEBUG; | |||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||
| 316 | # add the mac to the paid chain | ||||||||||||||||||||||||||||||||
| 317 | 0 | return $class->_paid_chain( 'A', $mac, $ip ); | |||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | sub delete_from_paid_chain { | ||||||||||||||||||||||||||||||
|
Subroutine
delete_from_paid_chain: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 321 | 0 | my ( $class, $mac, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | 0 | return $class->_paid_chain( 'D', $mac, $ip ); | |||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | sub check_paid_chain_for_mac { | ||||||||||||||||||||||||||||||
|
Subroutine
check_paid_chain_for_mac: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 327 | 0 | my ( $class, $mac ) = @_; | |||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | 0 | return $class->_check_chain_for_mac( $Paid_mark, $mac ); | |||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | sub _check_chain_for_mac { | ||||||||||||||||||||||||||||||
|
Subroutine
_check_chain_for_mac: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 333 | 0 | my ( $class, $mark, $mac ) = @_; | |||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | 0 | $mac = uc($mac); | |||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | 0 | my @lines = split( '\n', `sudo $Iptables -t mangle --list` ); | |||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | 0 | my $ip; | |||||||||||||||||||||||||||||||
| 340 | 0 | foreach my $line (@lines) { | |||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | next unless $line =~ m/^MARK/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 343 | last | ||||||||||||||||||||||||||||||||
| 344 | 0 | ▶ | if ($ip) = | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 345 | $line =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?MAC\s+$mac/i; | ||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | return unless $ip; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 349 | 0 | return $ip; | |||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | 0 | ▶ | sub check_ads_chain_for_mac { | ||||||||||||||||||||||||||||||
|
Subroutine
check_ads_chain_for_mac: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 353 | 0 | my ( $class, $mac ) = @_; | |||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||
| 355 | 0 | return $class->_check_chain_for_mac( $Ads_mark, $mac ); | |||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | sub add_to_ads_chain { | ||||||||||||||||||||||||||||||
|
Subroutine
add_to_ads_chain: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 359 | 0 | my ( $class, $mac, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||
| 361 | 0 | my $esc_mac = URI::Escape::uri_escape($mac); | |||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||
| 363 | # convert minutes to seconds | ||||||||||||||||||||||||||||||||
| 364 | 0 | my $stay = time() + $Config->sl_visitor_limit * 60; | |||||||||||||||||||||||||||||||
| 365 | 0 | $class->set( $mac => "$stay|ads" ); | |||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||
| 367 | 0 | warn("cache set $mac => $stay") if DEBUG; | |||||||||||||||||||||||||||||||
| 368 | 0 | return $class->_ads_chain( 'A', $mac, $ip ); | |||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | sub delete_from_ads_chain { | ||||||||||||||||||||||||||||||
|
Subroutine
delete_from_ads_chain: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 372 | 0 | my ( $class, $mac, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | 0 | return $class->_ads_chain( 'D', $mac, $ip ); | |||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | sub _ads_chain { | ||||||||||||||||||||||||||||||
|
Subroutine
_ads_chain: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 378 | 0 | my ( $class, $op, $mac, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||
| 380 | 0 | iptables( | |||||||||||||||||||||||||||||||
| 381 | "-t mangle -$op slOUT -s $ip -m mac --mac-source $mac -j MARK $Mark_op $Ads_mark" | ||||||||||||||||||||||||||||||||
| 382 | ); | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | 0 | iptables("-t mangle -$op slINC -d $ip -j ACCEPT"); | |||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | sub check_overage { | ||||||||||||||||||||||||||||||
|
Subroutine
check_overage: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 388 | 0 | my ( $class, $mac, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||
| 390 | 0 | my $in = `$Iptables -t mangle -n -v -x -L slINC`; | |||||||||||||||||||||||||||||||
| 391 | 0 | my $out = `$Iptables -t mangle -n -v -x -L slOUT`; | |||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | # check the megabyte limits first | ||||||||||||||||||||||||||||||||
| 394 | 0 | my ($bytes_in) = $in =~ m/\d+\s+(\d+).*?$ip/; | |||||||||||||||||||||||||||||||
| 395 | 0 | ▶ | return 1 if $bytes_in > $Config->sl_down_overage; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | 0 | my ($bytes_out) = $out =~ m/\d+\s+(\d+).*?$ip/; | |||||||||||||||||||||||||||||||
| 398 | 0 | ▶ | return 1 if $bytes_out > $Config->sl_up_overage; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | 0 | return; | |||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||
| 403 | 1 | 1; | |||||||||||||||||||||||||||||||