]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/firewall/rules.pl
firewall: rules.pl: Introduce a more slink debugging mode.
[people/teissler/ipfire-2.x.git] / config / firewall / rules.pl
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23 use Time::Local;
24 no warnings 'uninitialized';
25
26 require '/var/ipfire/general-functions.pl';
27 require "${General::swroot}/lang.pl";
28 require "/usr/lib/firewall/firewall-lib.pl";
29
30 # Set to one to enable debugging mode.
31 my $DEBUG = 0;
32
33 my %fwdfwsettings=();
34 my %defaultNetworks=();
35 my %configfwdfw=();
36 my %color=();
37 my %icmptypes=();
38 my %ovpnSettings=();
39 my %customgrp=();
40 our %sourcehash=();
41 our %targethash=();
42 my @timeframe=();
43 my %configinputfw=();
44 my %configoutgoingfw=();
45 my %confignatfw=();
46 my %aliases=();
47 my @DPROT=();
48 my @p2ps=();
49
50 my $configfwdfw = "${General::swroot}/firewall/config";
51 my $configinput = "${General::swroot}/firewall/input";
52 my $configoutgoing = "${General::swroot}/firewall/outgoing";
53 my $p2pfile = "${General::swroot}/firewall/p2protocols";
54 my $configgrp = "${General::swroot}/fwhosts/customgroups";
55 my $netsettings = "${General::swroot}/ethernet/settings";
56 my $errormessage = '';
57 my $orange = '';
58 my $green = '';
59 my $blue = '';
60 my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
61 my $CHAIN = "FORWARDFW";
62 my $conexists = 'off';
63 my $command = 'iptables --wait -A';
64 my $dnat ='';
65 my $snat ='';
66
67 &General::readhash("${General::swroot}/firewall/settings", \%fwdfwsettings);
68 &General::readhash("$netsettings", \%defaultNetworks);
69 &General::readhasharray($configfwdfw, \%configfwdfw);
70 &General::readhasharray($configinput, \%configinputfw);
71 &General::readhasharray($configoutgoing, \%configoutgoingfw);
72 &General::readhasharray($configgrp, \%customgrp);
73 &General::get_aliases(\%aliases);
74
75 #check if we have an internetconnection
76 open (CONN,"/var/ipfire/red/iface");
77 my $con = <CONN>;
78 close(CONN);
79
80 if (-f "/var/ipfire/red/active"){
81 $conexists='on';
82 }
83
84 open (CONN1,"/var/ipfire/red/local-ipaddress");
85 my $redip = <CONN1>;
86 close(CONN1);
87
88 #################
89 # DEBUG/TEST #
90 #################
91 my $MODE=!$DEBUG; # 0 - normal operation
92 # 1 - print configline and rules to console
93 #
94 #################
95 my $param=shift;
96
97 if($param eq 'flush'){
98 if ($MODE eq '1'){
99 print " Flushing chains...\n";
100 }
101 &flush;
102 }else{
103 if ($MODE eq '1'){
104 print " Flushing chains...\n";
105 }
106 &flush;
107 if ($MODE eq '1'){
108 print " Preparing rules...\n";
109 }
110 &preparerules;
111 if($MODE eq '0'){
112 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
113 &p2pblock;
114 run("/usr/sbin/firewall-policy");
115 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
116 &p2pblock;
117 run("/usr/sbin/firewall-policy");
118 }
119 }
120 }
121
122 sub run {
123 # Executes or prints the given shell command.
124 my $command = shift;
125
126 if ($DEBUG) {
127 print "$command\n";
128 } else {
129 system "$command";
130 }
131 }
132
133 sub flush {
134 run("iptables --wait -F FORWARDFW");
135 run("iptables --wait -F INPUTFW");
136 run("iptables --wait -F OUTGOINGFW");
137 run("iptables --wait -t nat -F NAT_DESTINATION");
138 run("iptables --wait -t nat -F NAT_SOURCE");
139 }
140
141 sub preparerules {
142 if (! -z "${General::swroot}/firewall/config"){
143 &buildrules(\%configfwdfw);
144 }
145 if (! -z "${General::swroot}/firewall/input"){
146 &buildrules(\%configinputfw);
147 }
148 if (! -z "${General::swroot}/firewall/outgoing"){
149 &buildrules(\%configoutgoingfw);
150 }
151 }
152
153 sub buildrules {
154 my $hash=shift;
155 my $STAG;
156 my $natip;
157 my $snatport;
158 my $fireport;
159 my $nat;
160 my $fwaccessdport;
161 my $natchain;
162 my $icmptype;
163 foreach my $key (sort {$a <=> $b} keys %$hash){
164 next if (($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1') && $conexists eq 'off' );
165 $command="iptables --wait -A";
166 if ($$hash{$key}[28] eq 'ON'){
167 $command='iptables --wait -t nat -A';
168 $natip=&get_nat_ip($$hash{$key}[29],$$hash{$key}[31]);
169 if($$hash{$key}[31] eq 'dnat'){
170 $nat='DNAT';
171 if ($$hash{$key}[30] =~ /\|/){
172 $$hash{$key}[30]=~ tr/|/,/;
173 $fireport='-m multiport --dport '.$$hash{$key}[30];
174 }else{
175 $fireport='--dport '.$$hash{$key}[30] if ($$hash{$key}[30]>0);
176 }
177 }else{
178 $nat='SNAT';
179 }
180 }
181 $STAG='';
182 if($$hash{$key}[2] eq 'ON'){
183 #get source ip's
184 if ($$hash{$key}[3] eq 'cust_grp_src'){
185 foreach my $grp (sort {$a <=> $b} keys %customgrp){
186 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
187 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
188 }
189 }
190 }else{
191 &get_address($$hash{$key}[3],$$hash{$key}[4],"src");
192 }
193 #get target ip's
194 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
195 foreach my $grp (sort {$a <=> $b} keys %customgrp){
196 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
197 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
198 }
199 }
200 }elsif($$hash{$key}[5] eq 'ipfire' ){
201 if($$hash{$key}[6] eq 'GREEN'){
202 $targethash{$key}[0]=$defaultNetworks{'GREEN_ADDRESS'};
203 }
204 if($$hash{$key}[6] eq 'BLUE'){
205 $targethash{$key}[0]=$defaultNetworks{'BLUE_ADDRESS'};
206 }
207 if($$hash{$key}[6] eq 'ORANGE'){
208 $targethash{$key}[0]=$defaultNetworks{'ORANGE_ADDRESS'};
209 }
210 if($$hash{$key}[6] eq 'ALL'){
211 $targethash{$key}[0]='0.0.0.0/0';
212 }
213 if($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1'){
214 open(FILE, "/var/ipfire/red/local-ipaddress")or die "Couldn't open local-ipaddress";
215 $targethash{$key}[0]= <FILE>;
216 close(FILE);
217 }else{
218 foreach my $alias (sort keys %aliases){
219 if ($$hash{$key}[6] eq $alias){
220 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
221 }
222 }
223 }
224 }else{
225 &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt");
226 }
227 ##get source prot and port
228 $SRC_TGT='SRC';
229 $SPORT = &get_port($hash,$key);
230 $SRC_TGT='';
231
232 ##get target prot and port
233 $DPROT=&get_prot($hash,$key);
234
235 if ($DPROT eq ''){$DPROT=' ';}
236 @DPROT=split(",",$DPROT);
237
238 #get time if defined
239 if($$hash{$key}[18] eq 'ON'){
240 my ($time1,$time2,$daylight);
241 my $daylight=$$hash{$key}[28];
242 $time1=&get_time($$hash{$key}[26],$daylight);
243 $time2=&get_time($$hash{$key}[27],$daylight);
244 if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
245 if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
246 if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
247 if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");}
248 if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");}
249 if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
250 if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
251 $TIME=join(",",@timeframe);
252
253 $TIMEFROM="--timestart $time1 ";
254 $TIMETILL="--timestop $time2 ";
255 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
256 }
257 foreach my $DPROT (@DPROT){
258 $DPORT = &get_port($hash,$key,$DPROT);
259 $PROT=$DPROT;
260 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
261 if ($DPROT ne 'TCP' && $DPROT ne'UDP' && $DPROT ne 'ICMP' ){
262 $DPORT='';
263 }
264 foreach my $a (sort keys %sourcehash){
265 foreach my $b (sort keys %targethash){
266 if(! $sourcehash{$a}[0] || ! $targethash{$b}[0] || ($natip eq '-d ' && $$hash{$key}[28] eq 'ON') || (!$natip && $$hash{$key}[28] eq 'ON')){
267 #Skip rules when no RED IP is set (DHCP,DSL)
268 next;
269 }
270 next if ($targethash{$b}[0] eq 'none');
271 $STAG='';
272 if ($sourcehash{$a}[0] ne $targethash{$b}[0] && $targethash{$b}[0] ne 'none' || $sourcehash{$a}[0] eq '0.0.0.0/0.0.0.0'){
273 if($DPROT ne ''){
274 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
275 #Process ICMP RULE
276 if(substr($DPORT, 2, 4) eq 'icmp'){
277 my @icmprule= split(",",substr($DPORT, 12,));
278 foreach (@icmprule){
279 $icmptype="--icmp-type ";
280 if ($_ eq "BLANK") {
281 $icmptype="";
282 $_="";
283 }
284 if ($$hash{$key}[17] eq 'ON'){
285 run("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j LOG");
286 }
287 run("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j $$hash{$key}[0]");
288 }
289 #PROCESS DNAT RULE (Portforward)
290 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
291 $natchain='NAT_DESTINATION';
292 if ($$hash{$key}[17] eq 'ON'){
293 run("$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j LOG --log-prefix 'DNAT'");
294 }
295 my ($ip,$sub) =split("/",$targethash{$b}[0]);
296 #Process NAT with servicegroup used
297 if ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[14] eq 'cust_srvgrp'){
298 run("$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip $DPORT");
299 $fwaccessdport=$DPORT;
300 }else{
301 run("$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip$DPORT");
302 $DPORT =~ s/\-/:/g;
303 if ($DPORT){
304 $fwaccessdport="--dport ".substr($DPORT,1,);
305 }elsif(! $DPORT && $$hash{$key}[30] ne ''){
306 if ($$hash{$key}[30]=~m/|/i){
307 $$hash{$key}[30] =~ s/\|/,/g;
308 $fwaccessdport="-m multiport --dport $$hash{$key}[30]";
309 }else{
310 $fwaccessdport="--dport $$hash{$key}[30]";
311 }
312 }
313 }
314 run("iptables --wait -A FORWARDFW $PROT $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]");
315 next;
316 #PROCESS SNAT RULE
317 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
318 $natchain='NAT_SOURCE';
319 if ($$hash{$key}[17] eq 'ON' ){
320 run("$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG --log-prefix 'SNAT'");
321 }
322 run("$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to-source $natip");
323 }
324 #PROCESS EVERY OTHER RULE (If NOT ICMP, else the rule would be applied double)
325 if ($PROT ne '-p ICMP'){
326 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
327 run("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG");
328 }
329 run("iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]");
330 }
331 #PROCESS Prot ICMP and type = All ICMP-Types
332 if ($PROT eq '-p ICMP' && $$hash{$key}[9] eq 'All ICMP-Types'){
333 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
334 run("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG");
335 }
336 run("iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]");
337 }
338 }
339 }
340 }
341 }
342 }
343 }
344 %sourcehash=();
345 %targethash=();
346 undef $TIME;
347 undef $TIMEFROM;
348 undef $TIMETILL;
349 undef $fireport;
350 }
351 }
352
353 sub get_nat_ip {
354 my $val=shift;
355 my $type=shift;
356 my $result;
357 if($val eq 'RED' || $val eq 'GREEN' || $val eq 'ORANGE' || $val eq 'BLUE'){
358 $result=$defaultNetworks{$val.'_ADDRESS'};
359 }elsif($val eq 'ALL'){
360 $result='-i '.$con;
361 }elsif($val eq 'Default IP' && $type eq 'dnat'){
362 $result='-d '.$redip;
363 }elsif($val eq 'Default IP' && $type eq 'snat'){
364 $result=$redip;
365 }else{
366 foreach my $al (sort keys %aliases){
367 if($val eq $al && $type eq 'dnat'){
368 $result='-d '.$aliases{$al}{'IPT'};
369 }elsif($val eq $al && $type eq 'snat'){
370 $result=$aliases{$al}{'IPT'};
371 }
372 }
373 }
374 return $result;
375 }
376
377 sub get_time {
378 my $val=shift;
379 my $val1=shift;
380 my $time;
381 my $minutes;
382 my $ruletime;
383 $minutes = &utcmin($val);
384 $ruletime = $minutes + &time_get_utc($val);
385 if ($ruletime < 0){$ruletime +=1440;}
386 if ($ruletime > 1440){$ruletime -=1440;}
387 $time=sprintf "%02d:%02d", $ruletime / 60, $ruletime % 60;
388 return $time;
389 }
390
391 sub time_get_utc {
392 # Calculates the UTCtime from a given time
393 my $val=shift;
394 my @localtime=localtime(time);
395 my @gmtime=gmtime(time);
396 my $diff = ($gmtime[2]*60+$gmtime[1]%60)-($localtime[2]*60+$localtime[1]%60);
397 return $diff;
398 }
399
400 sub utcmin {
401 my $ruletime=shift;
402 my ($hrs,$min) = split(":",$ruletime);
403 my $newtime = $hrs*60+$min;
404 return $newtime;
405 }
406
407 sub p2pblock {
408 my $P2PSTRING;
409 my $DO;
410 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
411 @p2ps = <FILE>;
412 close FILE;
413 my $CMD = "-m ipp2p";
414 foreach my $p2pentry (sort @p2ps) {
415 my @p2pline = split( /\;/, $p2pentry );
416 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
417 $DO = "ACCEPT";
418 if ("$p2pline[2]" eq "on") {
419 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
420 }
421 }else {
422 $DO = "RETURN";
423 if ("$p2pline[2]" eq "off") {
424 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
425 }
426 }
427 }
428
429 if($P2PSTRING) {
430 run("/sbin/iptables --wait -A FORWARDFW $CMD $P2PSTRING -j $DO");
431 }
432 }
433
434 sub get_address {
435 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
436 my $base2=shift;
437 my $type=shift; #src or tgt
438 my $hash;
439 if ($type eq 'src'){
440 $hash=\%sourcehash;
441 }else{
442 $hash=\%targethash;
443 }
444 my $key = &General::findhasharraykey($hash);
445 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
446 if (&General::validmac($base2)){
447 $$hash{$key}[0] = "-m mac --mac-source $base2";
448 }else{
449 $$hash{$key}[0] = $base2;
450 }
451 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
452 $$hash{$key}[0]=&fwlib::get_std_net_ip($base2,$con);
453 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
454 $$hash{$key}[0]=&fwlib::get_net_ip($base2);
455 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
456 $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type);
457 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
458 $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1);
459 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
460 $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33);
461 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
462 $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,11);
463 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
464 $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11);
465 }elsif($base eq 'ipfire_src' ){
466 if($base2 eq 'GREEN'){
467 $$hash{$key}[0]=$defaultNetworks{'GREEN_ADDRESS'};
468 }
469 if($base2 eq 'BLUE'){
470 $$hash{$key}[0]=$defaultNetworks{'BLUE_ADDRESS'};
471 }
472 if($base2 eq 'ORANGE'){
473 $$hash{$key}[0]=$defaultNetworks{'ORANGE_ADDRESS'};
474 }
475 if($base2 eq 'ALL'){
476 $$hash{$key}[0]='0.0.0.0/0';
477 }
478 if($base2 eq 'RED' || $base2 eq 'RED1'){
479 open(FILE, "/var/ipfire/red/local-ipaddress");
480 $$hash{$key}[0]= <FILE>;
481 close(FILE);
482 }else{
483 foreach my $alias (sort keys %aliases){
484 if ($base2 eq $alias){
485 $$hash{$key}[0]=$aliases{$alias}{'IPT'};
486 }
487 }
488 }
489 }
490 }
491
492 sub get_prot {
493 my $hash=shift;
494 my $key=shift;
495 #check AH,GRE,ESP or ICMP
496 if ($$hash{$key}[7] ne 'ON' && $$hash{$key}[11] ne 'ON'){
497 return "$$hash{$key}[8]";
498 }
499 if ($$hash{$key}[7] eq 'ON' || $$hash{$key}[11] eq 'ON'){
500 #check if servicegroup or service
501 if($$hash{$key}[14] eq 'cust_srv'){
502 return &fwlib::get_srv_prot($$hash{$key}[15]);
503 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
504 return &fwlib::get_srvgrp_prot($$hash{$key}[15]);
505 }elsif (($$hash{$key}[10] ne '' || $$hash{$key}[15] ne '') && $$hash{$key}[8] eq ''){ #when ports are used and prot set to "all"
506 return "TCP,UDP";
507 }elsif (($$hash{$key}[10] ne '' || $$hash{$key}[15] ne '') && ($$hash{$key}[8] eq 'TCP' || $$hash{$key}[8] eq 'UDP')){ #when ports are used and prot set to "tcp" or "udp"
508 return "$$hash{$key}[8]";
509 }elsif (($$hash{$key}[10] eq '' && $$hash{$key}[15] eq '') && $$hash{$key}[8] ne 'ICMP'){ #when ports are NOT used and prot NOT set to "ICMP"
510 return "$$hash{$key}[8]";
511 }else{
512 return "$$hash{$key}[8]";
513 }
514 }
515 #DNAT
516 if ($SRC_TGT eq '' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[11] eq '' && $$hash{$key}[12] ne ''){
517 return "$$hash{$key}[8]";
518 }
519 }
520
521 sub get_port {
522 my $hash=shift;
523 my $key=shift;
524 my $prot=shift;
525 #Get manual defined Ports from SOURCE
526 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
527 if ($$hash{$key}[10] ne ''){
528 $$hash{$key}[10] =~ s/\|/,/g;
529 if(index($$hash{$key}[10],",") > 0){
530 return "-m multiport --sport $$hash{$key}[10] ";
531 }else{
532 if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ||($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat') ){
533 return "--sport $$hash{$key}[10] ";
534 }else{
535 return ":$$hash{$key}[10]";
536 }
537 }
538 }
539 #Get manual ports from TARGET
540 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
541 if($$hash{$key}[14] eq 'TGT_PORT'){
542 if ($$hash{$key}[15] ne ''){
543 $$hash{$key}[15] =~ s/\|/,/g;
544 if(index($$hash{$key}[15],",") > 0){
545 return "-m multiport --dport $$hash{$key}[15] ";
546 }else{
547 if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ){
548 return "--dport $$hash{$key}[15] ";
549 }else{
550 $$hash{$key}[15] =~ s/\:/-/g;
551 return ":$$hash{$key}[15]";
552 }
553 }
554 }
555 #Get ports defined in custom Service (firewall-groups)
556 }elsif($$hash{$key}[14] eq 'cust_srv'){
557 if ($prot ne 'ICMP'){
558 if($$hash{$key}[31] eq 'dnat' && $$hash{$key}[28] eq 'ON'){
559 my $ports =&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
560 $ports =~ s/\:/-/g;
561 return ":".$ports
562 }else{
563 return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
564 }
565 }elsif($prot eq 'ICMP' && $$hash{$key}[11] eq 'ON'){ #When PROT is ICMP and "use targetport is checked, this is an icmp-service
566 return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot);
567 }
568 #Get ports from services which are used in custom servicegroups (firewall-groups)
569 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
570 if ($prot ne 'ICMP'){
571 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
572 }
573 elsif($prot eq 'ICMP'){
574 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
575 }
576 }
577 }
578 #CHECK ICMP
579 if ($$hash{$key}[7] ne 'ON' && $$hash{$key}[11] ne 'ON' && $SRC_TGT eq ''){
580 if($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
581 return "--icmp-type $$hash{$key}[9] ";
582 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
583 return;
584 }
585 }
586 }