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