File: RuleSet.pm

package info (click to toggle)
fwctl 0.25-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 988 kB
  • ctags: 327
  • sloc: perl: 5,042; sh: 98; makefile: 57
file content (1182 lines) | stat: -rw-r--r-- 35,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
#
#    RuleSet.pm - Module to add sets of rules to the linux firewall.
#
#    This file is part of Fwctl.
#
#    Author: Francis J. Lacoste <francis@iNsu.COM>
#
#    Copyright (c) 1999,2000 Francis J. Lacoste, iNsu Innovations Inc.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms same terms as perl itself.
#
package Fwctl::RuleSet;

use strict;

use Net::IPv4Addr qw( ipv4_in_network ipv4_broadcast ipv4_parse );
use IPChains;
use Carp;

use constant NOMASQ	=> 0;
use constant MASQ	=> 1;
use constant UNMASQ	=> 2;
use constant MASQNOHIGH => 4;
use constant PORTFW	=> 8;
use constant UNPORTFW	=> MASQ|MASQNOHIGH;

use constant RESERVED_PORTS	=> "1:1023";
use constant UNPRIVILEGED_PORTS => "1024:65535";
use constant MASQ_PORTS => "61000:65096";

use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS @ISA);

BEGIN {
  require Exporter;

  @ISA		= ("Exporter");
  @EXPORT	= ();
  @EXPORT_OK	= ();

  %EXPORT_TAGS = (
		  masq	       => [ qw( NOMASQ MASQ UNMASQ PORTFW UNPORTFW MASQNOHIGH ) ],
		  ports	       => [ qw( RESERVED_PORTS UNPRIVILEGED_PORTS MASQ_PORTS ) ],
		  tcp_rulesets => [ qw( accept_tcp_ruleset block_tcp_ruleset
					acct_tcp_ruleset ) ],
		  udp_rulesets => [ qw( accept_udp_ruleset block_udp_ruleset
					acct_udp_ruleset) ],
		  ip_rulesets  => [ qw( accept_ip_ruleset block_ip_ruleset
					acct_ip_ruleset) ],
		 );

  Exporter::export_ok_tags( keys %EXPORT_TAGS );

  # Optional module
  eval { use IPChains::PortFW };
}


# Determine the address spec and interface case
sub determine_case {
  my ( $ip, $if ) = @_;

  my ($ip,$msklen) = ipv4_parse( $ip );

  return "ANY"		    if $if->{name} eq "ANY";
  return "LOCAL_IP"	    if $if->{ip} eq $ip;
  return "LOCAL_IMPLIED"    if $if->{broadcast} eq $ip;
  return "LOCAL_IMPLIED"    if $ip eq "255.255.255.255";
  return "REMOTE"	    if $ip eq "0.0.0.0" and $if->{name} eq 'EXT';
  return "LOCAL_IMPLIED"    if $ip eq "0.0.0.0";

  return "REMOTE"	    unless defined $msklen;

  return "LOCAL_IMPLIED"    if ipv4_in_network( $ip, $msklen, $if->{ip} );
  return "REMOTE";
}

sub determine_base {
  my ($ipchain) = shift;

  my $proto  = $ipchain->attribute( 'Prot' );
  return "all" if not defined $proto or $proto =~ /all/i;

  # Handle numeric protocol specification
  my $proto_name = getprotobynumber $proto if $proto =~ /\d+/;
  $proto = $proto_name if defined $proto_name;

  my $syn = 0;
  my $ack = 0;
  if ($proto =~ /tcp/i and defined $ipchain->attribute( 'SYN' ) ) {
    $syn = $ipchain->attribute( 'SYN' ) eq '1';
    $ack = $ipchain->attribute( 'SYN' ) eq '!';
  }

  return "icmp" if $proto =~ /icmp/i;
  return "udp"  if $proto =~ /udp/i;
  return "syn"  if $syn;
  return "ack"  if $ack;
  return "tcp"  if $proto =~ /tcp/i;
  return "oth";
}

sub accept_tcp_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw ) = @_;

  croak __PACKAGE__, ": protocol isn't TCP"
    unless $ipchain->attribute( 'Prot' ) =~ /tcp/i;

  $masq = NOMASQ unless defined $masq;

  croak __PACKAGE__, ": can't use invalid \$masq and \$portfw combination"
    if (   defined $portfw && ! $masq & PORTFW) ||
       ( ! defined $portfw &&   $masq & PORTFW );

  my $syn      = $ipchain->attribute( 'SYN' );
  my $src_port = $ipchain->attribute( 'SourcePort' );
  my $dst_port = $ipchain->attribute( 'DestPort' );

  # Client side
  $ipchain->attribute( SYN => undef );
  accept_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw );

  # Server side
  $ipchain->attribute( SourcePort => $dst_port );
  $ipchain->attribute( DestPort   => $src_port );
  $ipchain->attribute( SYN => '!' );

  # Switch from MASQ to UNMASQ
  if ( $masq & MASQ ) {
      $masq &= $masq ^ MASQ;
      $masq |= UNMASQ;
  } elsif ( $masq & PORTFW ) {
      # Packets that were port forwarded should be masqueraded back
      $masq &= $masq ^ PORTFW;
      $masq |= UNPORTFW;
  }
  accept_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if, $masq, $portfw );

  # Restore params
  $ipchain->attribute( SourcePort => $src_port );
  $ipchain->attribute( DestPort   => $dst_port );
  $ipchain->attribute( SYN => $syn );
}

sub accept_udp_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw ) = @_;

  croak __PACKAGE__, ": protocol isn't UDP"
    unless $ipchain->attribute( 'Prot' ) =~ /UDP/i;

  $masq = NOMASQ unless defined $masq;

  croak __PACKAGE__, ": can't use invalid \$masq and \$portfw combination"
    if (   defined $portfw && ! $masq & PORTFW) ||
       ( ! defined $portfw &&   $masq & PORTFW );

  my $src_port = $ipchain->attribute( 'SourcePort' );
  my $dst_port = $ipchain->attribute( 'DestPort' );

  # Client side
  accept_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw );

  # Server side
  $ipchain->attribute( SourcePort => $dst_port );
  $ipchain->attribute( DestPort   => $src_port );
  # Switch from MASQ to UNMASQ
  if ( $masq & MASQ ) {
      $masq &= $masq ^ MASQ;
      $masq |= UNMASQ;
  } elsif ( $masq & PORTFW ) {
      # Packets that were port forwarded should be masqueraded back
      $masq &= $masq ^ PORTFW;
      $masq |= UNPORTFW;
  }
  accept_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if, $masq, $portfw );

  # Restore params
  $ipchain->attribute( SourcePort => $src_port );
  $ipchain->attribute( DestPort   => $dst_port );
}

# Adds protocol necessary ruleset for a type of packet
# to pass through the interface of the firewall.
sub accept_ip_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw ) = @_;

  croak __PACKAGE__, ": rule target must be ACCEPT: ", $ipchain->attribute( 'Rule' )
    unless $ipchain->attribute( 'Rule' ) =~ /ACCEPT/;

  my $src_kind = determine_case( $src, $src_if );
  my $dst_kind = determine_case( $dst, $dst_if );

  $ipchain->attribute( Source => $src );
  $ipchain->attribute( Dest   => $dst );

  my $base = determine_base( $ipchain );

  # Check masquerading parameter.
  $masq = NOMASQ unless defined $masq;

 SWITCH:
  for ("$src_kind-$dst_kind" ) {
    /ANY-ANY|ANY-REMOTE|REMOTE-ANY/ && do {
      ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
      if ( $masq & MASQ ) {
	ip_local_out_ruleset( @_[0..4], $base );
      } elsif ( $masq & UNMASQ ) {
	ip_local_in_ruleset( @_[0..4], $base );
      }
      last SWITCH;
    };
    /LOCAL_IMPLIED-LOCAL_IMPLIED/ && do {
	# name handle the INTERNET or 0.0.0.0/0 case 
	# and ipv4_in_network handle the case of two routers scenario
      if ( $src_if->{name} ne $dst_if->{name} || ! ipv4_in_network( $src, $dst ) ) {
	# Src and dst networks are different
	ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
	if ( $masq & MASQ ) {
	  ip_local_out_ruleset( @_[0..4], $base );
	} elsif ( $masq & UNMASQ ) {
	  ip_local_in_ruleset( @_[0..4], $base );
	}
      } else {
	# No forwarding will take place
	ip_local_in_ruleset( @_[0..4], $base );
      }
      last SWITCH;
    };
    /REMOTE-LOCAL_IMPLIED|LOCAL_IMPLIED-REMOTE/ && do {
	# name handle the INTERNET or 0.0.0.0/0 case 
	# and ipv4_in_network handle the case of two routers scenario
      if ( $src_if->{name} ne $dst_if->{name} || ! ipv4_in_network( $src, $dst ) ) {
	# Src and dst networks are different
	ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
      } else {
	# No forwarding will take place
	ip_local_in_ruleset( @_[0..4], $base );
      }
      last SWITCH;
    };
    /ANY-LOCAL_IMPLIED/ && do {
      ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
      ip_loopback_out_ruleset( @_[0..4], $base );
      if ( $masq & MASQ ) {
	ip_local_out_ruleset( @_[0..4], $base );
      } elsif ( $masq & UNMASQ ) {
	ip_local_in_ruleset( @_[0..4], $base );
      }
      last SWITCH;
    };
    /REMOTE-REMOTE/ && do {
      ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
      last SWITCH;
    };
    /LOCAL_IMPLIED-ANY/ && do {
      ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
      ip_loopback_in_ruleset( @_[0..4], $base );
      if ( $masq & MASQ ) {
	ip_local_out_ruleset( @_[0..4], $base );
      } elsif ( $masq & UNMASQ ) {
	ip_local_in_ruleset( @_[0..4], $base );
      }
      last SWITCH;
    };
    /LOCAL_IP-ANY/ && do {
      ip_local_out_ruleset( @_[0..4], $base );
      ip_loopback_in_ruleset( @_[0..4], $base );
      last SWITCH;
    };
    /LOCAL_IP-LOCAL_IMPLIED/ && do {
      ip_local_out_ruleset( @_[0..4], $base );
      ip_loopback_ruleset( @_[0..4], $base );
      last SWITCH;
    };
    /ANY-LOCAL_IP/ && do {
      ip_local_in_ruleset( @_[0..4], $base );
      ip_loopback_out_ruleset( @_[0..4], $base );
      last SWITCH;
    };
    /LOCAL_IMPLIED-LOCAL_IP/ && do {
      ip_local_in_ruleset( @_[0..4], $base );
      ip_loopback_ruleset( @_[0..4], $base );
      last SWITCH;
    };
    /LOCAL_IP-LOCAL_IP/ && do {
      ip_loopback_ruleset( @_[0..4], $base );
      last SWITCH;
    };
    /REMOTE-LOCAL_IP/ && do {
      ip_local_in_ruleset( @_[0..4], $base );
      last SWITCH;
    };
    /LOCAL_IP-REMOTE/ && do {
      ip_local_out_ruleset( @_[0..4], $base );
      last SWITCH;
    };
    croak __PACKAGE__, ": unknown src/dst combination: $src/$dst\n";
  }
}


# Add rule to necessary chains to handle
# loopback connection. Local process to
# local process.
sub ip_loopback_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;

  $ipchain->attribute( Interface => "lo" );
  $ipchain->append( "$base-out" );
  $ipchain->append( "$base-in" );
}

sub ip_loopback_out_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;

  $ipchain->attribute( Interface => "lo" );
  $ipchain->append( "$base-out" );

}

sub ip_loopback_in_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;

  $ipchain->attribute( Interface => "lo" );
  $ipchain->append( "$base-in" );

}

# Add rule to necessary chains to handle
# an incoming packet destined to a local
# process
sub ip_local_out_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;

  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-out" );
}

# Add rule to necessary chains to handle
# a outgoing local originating packet.
sub ip_local_in_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;

  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->append( "$base-in" );
}

# Add rule to necessary chains to handle
# a routed packet. Packet that aren't
# destined locally.
#
# src_if and dst_if should be different
# then lo.
sub ip_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $portfw ) = @_;

  if ( $masq & MASQ ) {
  SWITCH:
    for ($base) {
      /tcp|ack|syn/ && do {
	tcp_masq_forward_ruleset( @_ );
	last SWITCH;
      };
      /udp/ && do {
	udp_masq_forward_ruleset( @_ );
	last SWITCH;
      };
      /icmp|oth/ && do {
	ip_masq_forward_ruleset( @_ );
	last SWITCH;
      };
      /all/ && do {
	# Well ALL with masq only means TCP,UDP,IP
	# I don't think we would like to see
	# other protocols pass through w/o masquerading.
	all_masq_forward_ruleset(  @_ );
	last SWITCH;
      };
  }
  } elsif ($masq & UNMASQ ) {
  SWITCH:
    for ($base) {
      /tcp|ack|syn/ && do {
	tcp_unmasq_forward_ruleset( @_ );
	last SWITCH;
      };
      /udp/ && do {
	udp_unmasq_forward_ruleset( @_ );
	last SWITCH;
      };
      /icmp|oth/ && do {
	ip_unmasq_forward_ruleset( @_ );
	last SWITCH;
      };
      /all/ && do {
	all_unmasq_forward_ruleset( @_ );
	last SWITCH;
      };
  }
  } elsif ( $masq & PORTFW ) {
      if ($base =~ /tcp|ack|syn|udp/ ) {
	  tcpudp_portfw_forward_ruleset( @_ );
      } else {
	  ip_portfw_forward_ruleset( @_ );
      }
  } else {
    $ipchain->attribute( Interface => $src_if->{interface} );
    $ipchain->append( "$base-in" );
    $ipchain->attribute( Interface => $dst_if->{interface} );
    $ipchain->append( "$base-fwd" );
    $ipchain->append( "$base-out" );
  }
}

# Add rule to necessary chains to handle
# a route TCP masqueraded packet.
sub tcp_masq_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $masqip ) = @_;

  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->append( "$base-in" );

  my $rule = $ipchain->attribute( 'Rule' );
  $ipchain->attribute( Rule => 'MASQ' );
  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-fwd" );
  $ipchain->attribute( Rule => $rule );

  my $saved_ports = $ipchain->attribute('SourcePort');
  my $ip =  $masqip ? $masqip : $dst_if->{ip};
  $ipchain->attribute( Source	    => $ip );
  $ipchain->attribute( SourcePort   => MASQ_PORTS ) unless $masq & MASQNOHIGH;
  $ipchain->append( "$base-out" );
  $ipchain->attribute( Source	    => $src );
  $ipchain->attribute( SourcePort   => $saved_ports );
}


sub udp_masq_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $masqip ) = @_;

  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->append( "$base-in" );
  my $rule = $ipchain->attribute( 'Rule' );
  $ipchain->attribute( Rule => 'MASQ' );
  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-fwd" );
  $ipchain->attribute( Rule => $rule );

  my $saved_ports = $ipchain->attribute('SourcePort');
  my $ip = $masqip ? $masqip : $dst_if->{ip};
  $ipchain->attribute( Source	    => $ip );
  $ipchain->attribute( SourcePort   => MASQ_PORTS ) unless $masq & MASQNOHIGH;
  $ipchain->append( "$base-out" );
  $ipchain->attribute( Source	    => $src );
  $ipchain->attribute( SourcePort   => $saved_ports );
}

sub ip_masq_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;

  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->append( "$base-in" );
  my $rule = $ipchain->attribute( 'Rule' );
  $ipchain->attribute( Rule => 'MASQ' );
  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-fwd" );
  $ipchain->attribute( Rule => $rule );

  $ipchain->attribute( Source	    => $dst_if->{ip} );
  $ipchain->append( "$base-out" );
  $ipchain->attribute( Source	    => $src );
}

sub all_masq_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq ) = @_;

  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->append( "$base-in" );

  my $rule = $ipchain->attribute( 'Rule' );
  $ipchain->attribute( Rule => 'MASQ' );
  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-fwd" );
  $ipchain->attribute( Rule => $rule );

  # Masquerade rewrite packets
  $ipchain->attribute( Source	    => $dst_if->{ip} );

  # ICMP
  $ipchain->attribute( Prot => "icmp" );
  $ipchain->append( "icmp-out" );

  # UDP and TCP
  # All shouldn't have a saved port.
  $ipchain->attribute( SourcePort   => MASQ_PORTS ) unless $masq & MASQNOHIGH;

  $ipchain->attribute( Prot => "tcp" );
  $ipchain->append( "tcp-out" );

  $ipchain->attribute( Prot => "udp" );
  $ipchain->append( "udp-out" );

  # Restore
  $ipchain->attribute( Prot => undef );
  $ipchain->attribute( SourcePort => undef );
}

# Add rule to necessary chains to handle
# a route TCP packet which was masqueraded.
sub tcp_unmasq_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq ) = @_;

  my $saved_ports = $ipchain->attribute('DestPort');
  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->attribute( Dest	 => $src_if->{ip} );
  $ipchain->attribute( DestPort  => MASQ_PORTS ) unless $masq & MASQNOHIGH;
  $ipchain->append( "$base-in" );
  $ipchain->attribute( Dest	 => $dst );
  $ipchain->attribute( DestPort  => $saved_ports );

  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-out" );
}

sub udp_unmasq_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq ) = @_;

  my $saved_ports = $ipchain->attribute('DestPort');
  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->attribute( Dest	 => $src_if->{ip} );
  $ipchain->attribute( DestPort  => MASQ_PORTS ) unless $masq & MASQNOHIGH;
  $ipchain->append( "$base-in" );
  $ipchain->attribute( Dest	 => $dst );
  $ipchain->attribute( DestPort  => $saved_ports );

  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-out" );
}

sub ip_unmasq_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;

  my $saved_ports = $ipchain->attribute('DestPort');
  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->attribute( Dest	 => $src_if->{ip} );
  $ipchain->append( "$base-in" );
  $ipchain->attribute( Dest	 => $dst );

  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-out" );
}

sub all_unmasq_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq ) = @_;

  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->attribute( Dest	 => $src_if->{ip} );

  # ICMP
  $ipchain->attribute( Prot => 'icmp' );
  $ipchain->append( "icmp-in" );

  # UDP
  $ipchain->attribute( DestPort  => MASQ_PORTS ) unless $masq & MASQNOHIGH;
  $ipchain->attribute( Prot => 'udp' );
  $ipchain->append( "udp-in" );

  # TCP
  $ipchain->attribute( Prot => 'tcp' );
  $ipchain->attribute( SYN => '!' );
  $ipchain->append( "tcp-in" );

  # Restore
  $ipchain->attribute( Prot => undef );
  $ipchain->attribute( DestPort => undef );
  $ipchain->attribute( SYN => undef );

  $ipchain->attribute( Dest	 => $dst );
  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-out" );

}

sub tcpudp_portfw_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $portfw ) = @_;
  $portfw ||= $src_if->{ip};

  # Incoming address is in $portfw
  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->attribute( Dest => $portfw );
  $ipchain->append( "$base-in" );

  $ipchain->attribute( Dest => $dst );

  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-fwd" );
  $ipchain->append( "$base-out" );

  # Add port forwarding rule
  my $proto = $ipchain->attribute( 'Prot' );
  my $port  = $ipchain->attribute( 'DestPort' );

  my $portfw_chain = new IPChains::PortFW( Proto     => $proto,
					   LocalAddr => $portfw,
					   LocalPort => $port,
					   RemAddr   => $dst,
					   RemPort   => $port
					 );
  $portfw_chain->append;

}

# We just set up the rules here. The user will have
# to set the ipfwd daemon manually
sub ip_portfw_forward_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $portfw ) = @_;
  $portfw ||= $src_if->{ip};

  # Incoming address is in $portfw
  $ipchain->attribute( Interface => $src_if->{interface} );
  $ipchain->attribute( Dest => $portfw );
  $ipchain->append( "$base-in" );

  $ipchain->attribute( Dest => $dst );

  $ipchain->attribute( Interface => $dst_if->{interface} );
  $ipchain->append( "$base-fwd" );
  $ipchain->append( "$base-out" );
}

sub block_tcp_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if ) = @_;

  croak __PACKAGE__, ": protocol isn't TCP"
    unless $ipchain->attribute( 'Prot' ) =~ /tcp/i;

  my $syn      = $ipchain->attribute( 'SYN' );
  my $src_port = $ipchain->attribute( 'SourcePort' );
  my $dst_port = $ipchain->attribute( 'DestPort' );

  # Client side
  $ipchain->attribute( SYN => undef );
  block_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if );

  # Server side
  $ipchain->attribute( SourcePort => $dst_port );
  $ipchain->attribute( DestPort   => $src_port );
  $ipchain->attribute( SYN => '!' );
  block_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if );

  # Restore params
  $ipchain->attribute( SourcePort => $src_port );
  $ipchain->attribute( DestPort   => $dst_port );
  $ipchain->attribute( SYN => $syn );

}

sub block_udp_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq ) = @_;

  croak __PACKAGE__, ": protocol isn't UDP"
    unless $ipchain->attribute( 'Prot' ) =~ /UDP/i;

  $masq = NOMASQ unless defined $masq;

  my $src_port = $ipchain->attribute( 'SourcePort' );
  my $dst_port = $ipchain->attribute( 'DestPort' );

  # Client side
  block_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if );

  # Server side
  $ipchain->attribute( SourcePort => $dst_port );
  $ipchain->attribute( DestPort   => $src_port );
  block_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if );

  # Restore params
  $ipchain->attribute( SourcePort => $src_port );
  $ipchain->attribute( DestPort   => $dst_port );
}

sub block_ip_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if ) = @_;

  my $src_kind = determine_case( $src, $src_if );
  my $dst_kind = determine_case( $dst, $dst_if );

  croak __PACKAGE__, ": rule target must be REJECT or DENY: ", $ipchain->attribute( 'Rule' )
    unless $ipchain->attribute( 'Rule' ) =~ /DENY|REJECT/;

  $ipchain->attribute( Source => $src );
  $ipchain->attribute( Dest   => $dst );

  my $base = determine_base( $ipchain );

 SWITCH:
  for ("$src_kind-$dst_kind" ) {
    /ANY-ANY|LOCAL_IMPLIED-ANY|ANY-REMOTE|LOCAL_IMPLIED-REMOTE/ && do {
      $ipchain->attribute( Interface => $src_if->{interface} );
      $ipchain->append( "$base-in" );
      $ipchain->attribute( Interface => $dst_if->{interface} );
      $ipchain->append( "$base-out" );
      last SWITCH;
    };
    /ANY-LOCAL_IMPLIED|LOCAL_IMPLIED-LOCAL_IMPLIED/ && do {
      $ipchain->attribute( Interface => $src_if->{interface} );
      $ipchain->append( "$base-in" );
      $ipchain->attribute( Interface => $dst_if->{interface} );
      $ipchain->append( "$base-out" );
      $ipchain->attribute( Interface => "lo" );
      $ipchain->append( "$base-out" );
      last SWITCH;
    };
    /LOCAL_IP-ANY|LOCAL_IP-LOCAL_IMPLIED/ && do {
      $ipchain->attribute( Interface => $dst_if->{interface} );
      $ipchain->append( "$base-out" );
      $ipchain->attribute( Interface => "lo" );
      $ipchain->append( "$base-out" );
      last SWITCH;
    };
    /ANY-LOCAL_IP|LOCAL_IMPLIED-LOCAL_IP/ && do {
      $ipchain->attribute( Interface => $src_if->{interface} );
      $ipchain->append( "$base-in" );
      $ipchain->attribute( Interface => "lo" );
      $ipchain->append( "$base-out" );
      last SWITCH;
    };
    /LOCAL_IP-LOCAL_IP/ && do {
      $ipchain->attribute( Interface => "lo" );
      $ipchain->append( "$base-out" );
      last SWITCH;
    };
    /LOCAL_IP-REMOTE/ && do {
      $ipchain->attribute( Interface => $dst_if->{interface} );
      $ipchain->append( "$base-out" );
      last SWITCH;
    };
    /REMOTE-.*/ && do {
      $ipchain->attribute( Interface => $src_if->{interface} );
      $ipchain->append( "$base-in" );
      last SWITCH;
    };
    croak __PACKAGE__, ": unknown src/dst combination: $src/$dst\n";
  }
}

sub acct_tcp_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq ) = @_;

  croak __PACKAGE__, ": protocol isn't TCP"
    unless $ipchain->attribute( 'Prot' ) =~ /tcp/i;

  $masq = NOMASQ unless defined $masq;

  my $syn      = $ipchain->attribute( 'SYN' );
  my $src_port = $ipchain->attribute( 'SourcePort' );
  my $dst_port = $ipchain->attribute( 'DestPort' );

  # Client side
  $ipchain->attribute( SYN => undef );
  acct_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if, $masq );

  # Server side
  $ipchain->attribute( SourcePort => $dst_port );
  $ipchain->attribute( DestPort   => $src_port );
  $ipchain->attribute( SYN => '!' );
  # Switch from MASQ to UNMASQ
  if ( $masq & MASQ ) {
      $masq &= $masq ^ MASQ;
      $masq |= UNMASQ;
  } elsif ( $masq & PORTFW ) {
      # Packets that were port forwarded should be masqueraded back
      $masq &= $masq ^ PORTFW;
      $masq |= UNPORTFW;
  }
  acct_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if, $masq );

  # Restore params
  $ipchain->attribute( SourcePort => $src_port );
  $ipchain->attribute( DestPort   => $dst_port );
  $ipchain->attribute( SYN => $syn );

}

sub acct_udp_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq ) = @_;

  croak __PACKAGE__, ": protocol isn't UDP"
    unless $ipchain->attribute( 'Prot' ) =~ /UDP/i;

  $masq = NOMASQ unless defined $masq;

  my $src_port = $ipchain->attribute( 'SourcePort' );
  my $dst_port = $ipchain->attribute( 'DestPort' );

  # Client side
  acct_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if, $masq );

  # Server side
  $ipchain->attribute( SourcePort => $dst_port );
  $ipchain->attribute( DestPort   => $src_port );

  # Switch from MASQ to UNMASQ
  if ( $masq & MASQ ) {
      $masq &= $masq ^ MASQ;
      $masq |= UNMASQ;
  } elsif ( $masq & PORTFW ) {
      # Packets that were port forwarded should be masqueraded back
      $masq &= $masq ^ PORTFW;
      $masq |= UNPORTFW;
  }
  acct_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if, $masq );

  # Restore params
  $ipchain->attribute( SourcePort => $src_port );
  $ipchain->attribute( DestPort   => $dst_port );
}

sub acct_ip_ruleset {
  my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq ) = @_;

  croak __PACKAGE__, ": bad rule target: ", $ipchain->attribute( 'Rule' )
    unless $ipchain->attribute( 'Rule' ) =~ /acct\d{4}/;

  my $src_kind = determine_case( $src, $src_if );
  my $dst_kind = determine_case( $dst, $dst_if );

  $ipchain->attribute( Source => $src );
  $ipchain->attribute( Dest   => $dst );

  my $base = determine_base( $ipchain );

  # Check masquerading parameter.
  $masq = NOMASQ unless defined $masq;

 SWITCH:
  for ("$src_kind-$dst_kind" ) {
    /LOCAL_IP-ANY|LOCAL_IP-REMOTE/ && do {
      $ipchain->attribute( Interface => $dst_if->{interface} );
      $ipchain->append( "acct-out" );
      last SWITCH;
    };
    /ANY-LOCAL_IP|REMOTE-LOCAL_IP/ && do {
      $ipchain->attribute( Interface => $src_if->{interface} );
      $ipchain->append( "acct-in" );
      last SWITCH;
    };
    /LOCAL_IMPLIED-LOCAL_IP/ && do {
      $ipchain->attribute( Interface => $src_if->{interface} );
      $ipchain->append( "acct-in" );
      $ipchain->attribute( Interface => "lo" );
      $ipchain->append( "acct-out" );
      last SWITCH;
    };
    /LOCAL_IP-LOCAL_IMPLIED/ && do {
      $ipchain->attribute( Interface => $dst_if->{interface} );
      $ipchain->append( "acct-out" );
      $ipchain->attribute( Interface => "lo" );
      $ipchain->append( "acct-out" );
      last SWITCH;
    };
    /LOCAL_IP-LOCAL_IP/ && do {
      $ipchain->attribute( Interface => "lo" );
      $ipchain->append( "acct-out" );
      last SWITCH;
    };
    /ANY-REMOTE|LOCAL_IMPLIED-REMOTE/ && do {
      $ipchain->attribute( Interface => $dst_if->{interface} );
      $ipchain->append( "acct-out" );
      if ($masq & MASQ ) {
	$ipchain->attribute( Source => $dst_if->{ip} );

	# UDP and TCP gets their ports rewritten
	if ( $base =~ /tcp|udp|syn|ack/ ) {
	  my $saved_ports = $ipchain->attribute( 'SourcePort' );
	  $ipchain->attribute( SourcePort => MASQ_PORTS )
	    unless $masq & MASQNOHIGH;
	  $ipchain->append( "acct-out" );
	  $ipchain->attribute( SourcePort => $saved_ports );
	} else {
	  $ipchain->append( "acct-out" );
	}
	$ipchain->attribute( Source => $src );
      }
      last SWITCH;
    };
    /REMOTE-REMOTE/ && do {
      $ipchain->attribute( Interface => $dst_if->{interface} );
      if ($masq & MASQ ) {
	$ipchain->attribute( Source => $dst_if->{ip} );
	# UDP and TCP gets their ports rewritten
	if ( $base =~ /tcp|udp|syn|ack/ ) {
	  my $saved_ports = $ipchain->attribute( 'SourcePort' );
	  $ipchain->attribute( SourcePort => MASQ_PORTS )
	    unless $masq & MASQNOHIGH;
	  $ipchain->append( "acct-out" );
	  $ipchain->attribute( SourcePort => $saved_ports );
	} else {
	  $ipchain->append( "acct-out" );
	}
	$ipchain->attribute( Source => $src );
      } else {
	$ipchain->append( "acct-out" );
      }
      last SWITCH;
    };
    /REMOTE-ANY|REMOTE-LOCAL_IMPLIED/ && do {
      $ipchain->attribute( Interface => $src_if->{interface} );
      $ipchain->append( "acct-in" );
      if ($masq & UNMASQ ) {
	$ipchain->attribute( Dest => $src_if->{ip} );
	# UDP and TCP gets their ports rewritten
	if ( $base =~ /tcp|udp|syn|ack/ ) {
	  my $saved_ports = $ipchain->attribute( 'DestPort' );
	  $ipchain->attribute( DestPort => MASQ_PORTS );
	  $ipchain->append( "acct-in" );
	  $ipchain->attribute( DestPort => $saved_ports );
	} else {
	  $ipchain->append( "acct-in" );
	}
	$ipchain->attribute( Dest => $dst );
      } else {
      }
      last SWITCH;
    };
    /ANY-ANY|ANY-LOCAL_IMPLIED|LOCAL_IMPLIED-ANY|LOCAL_IMPLIED-LOCAL_IMPLIED/ && do {
      # Out packet
      $ipchain->attribute( Interface => "lo" ); # Loopback
      $ipchain->append( "acct-out" );
      $ipchain->attribute( Interface => $dst_if->{interface} );
      if ($masq & MASQ ) {
	$ipchain->attribute ( Source => $dst_if->{ip} );
	if ($base =~ /tcp|syn|ack|udp/) {
	  my $saved_ports = $ipchain->attribute( 'SourcePort' );
	  $ipchain->attribute( SourcePort => MASQ_PORTS )
	    unless $masq & MASQ_PORTS;
	  $ipchain->append( "acct-out" );
	  $ipchain->attribute( SourcePort => $saved_ports );
	} else {
	  $ipchain->append( "acct-out" );
	}
	$ipchain->attribute( Source => $src );
      } else {
	$ipchain->append( "acct-out" );
      }
      last SWITCH;
    };
    croak __PACKAGE__, ": unknown src/dst combination: $src/$dst\n";
  }
}

1;

=pod

=head1 NAME

Fwctl::RuleSet - Module to add sets of rules to the linux firewall.

=head1 SYNOPSIS

  use IPChains;
  use Fwctl::RuleSet qw(:masq :tcp_rulesets :ports);

  my $chain = new IPChains( Prot       => 'tcp',
			    SourcePort => UNPRIVILEGED_PORTS,
			    DestPort   => 23,
			    )
  accept_tcp_ruleset( $chain, $src, $src_if, $dst, $dst_if, NOMASQ );

=head1 DESCRIPTION

This module contains primitives to add sets of rules to the Linux
packet filtering firewall implementing a particular policy. It is used
primarly by service modules. The module handle all the special cases
for when the src or dst interface is ANY, when masquerading is involved,
when a local ip is implied by the src or dst address. All this logic
has not to be implemented by the service modules, which only have to
specify the kind of packets and the direction of traffic (using the src and
dst paremeter).

There are 5 tags that can be imported from the modules.

=over

=item :masq

Constant used to specify how to handle masquerade.

=item :ports

Constants that refers to range of ports.

=item :tcp_rulesets

Functions that implements policy rulesets for TCP connection.

=item :udp_rulesets

Functions that implements policy rulesets for bidirectional UDP traffic.

=item :ip_rulesets

Funtions that implements policy rulesets for IP traffic. This are the 
primitives on which the tcp and udp rulesets are built.

=back

=head1 :masq

=over

=item NOMASQ

Constant used to represent that the traffic shouldn't be masqueraded.

=item MASQ

Constant use to denote that this traffic will be masqueraded when
going throught the forward chain.

=item UNMASQ

Constant use to denote that traffic should be unmasqueraded when passing
the input chain.

=back

To better understand the way the MASQ and UNMASQ constants works together
lets look at how they would be use to handle a TCP connection.

    accept_ip_rulesets( $chain, $src, $src_if, $dst, $dst_if, MASQ );
    $chain->attribute( SYN => '!' );
    accept_ip_rulesets( $chain, $dst, $dst_if, $src, $src_if, UNMASQ);

=head1 :ports

=over

=item RESERVED_PORTS

Constant that represents the ports 1 through 1023.

=item UNPRIVILEGED_PORTS

Constant that represents the ports 1024 through 65535.

=item MASQ_PORTS

Constant that represents the ports used when masquerading a
connection : 61000 through 65096.

=back

=head1 :ip_rulesets

This tags imports three functions that are the primitives on which
the  others are built. All src or dst can be classified in one of
four category.
=over

=item ANY

Source or destination is any address on any interface.

=item LOCAL_IP

Source or destination is a local interface

=item LOCAL_IMPLIED

Source or destination implied a local interface. Example of those
includes a broadcast address of a local interface or network address
of a local interface.

=item REMOTE

Source or destination doesn't imply a local IP.

=back

So this means a total of 16 combination of source and destination address.
Add the parameter MASQ,UNMASQ and NOMASQ and you got 48 possibilities. Those
usually can be reduced to between 7 and 16 cases depending on the policy
you want to handle. (REJECT, DENY, ACCEPT or ACCOUNT). The following
functions handle all those possibilities for you, and adds the appropriate
rules with address and interface specification to the appropriate chains.

=over 

=item accept_ip_ruleset($chain,$src,$src_if,$dst,$dst_if,$masq)

Adds the necessary rules to accept the kind of traffic specified by
the $chain parameter.

=over

=item $chain

IPChains objects that contains the prototypes of the rules to add to the
firewall. Source, Dest and Interface parameter are overwritten by the
function.

=item $src

The source address of the packet.

=item $src_if

The interface associated to the $src address.

=item $dst

The destination address of the packet.

=item $dst_if

The interface associated to the $dst address.

=item $masq

How the packet should be masqueraded.

=back

Usually the $src, $src_if, $dst and $dst_if packets are not modified
by the service modules and are those passed by the Fwctl module. Or
the module will switch them (dst becomes src), or change them because
the protocol uses broadcast or other stuff.

=item block_ip_ruleset( $chain, $src, $src_if, $dst, $dst_if )

This primitive handles both REJECT and DENY policies. The parameter
have the same meaning as in the accept_ip_ruleset() function.

=item account_ip_ruleset( $chain, $src, $src_if, $dst, $dst_if )

This primitive handles the ACCOUNT policy. The parameter
have the same meaning as in the accept_ip_ruleset() function.

=back

=head1 :tcp_rulesets

This tags imports three functions: accept_tcp_ruleset(),
block_tcp_ruleset() and account_tcp_ruleset() which have the same
parameters and semantics as their *_ip_ruleset() counterpart.
They are indeed implemented in terms of these.

The difference is that the $chain parameter can only be used to
represent a TCP connection. The functions will add rules for the
client and server side of the connection with the SYN and ACK flags
handled properly.

=head1 :udp_rulesets

This tags imports three functions: accept_udp_ruleset(),
block_udp_ruleset() and account_udp_ruleset() which have the same
parameters and semantics as their *_ip_ruleset() counterpart.
They are indeed implemented in terms of these.

These functions will add rules to handle client / server UDP connection.
It like calling the *_ip_ruleset() functions two times with the
src and dst inversed (the SourcePort and DestPort are naturally also
inversed).


=head1 AUTHOR

Copyright (c) 1999,2000 Francis J. Lacoste and iNsu Innovations Inc.
All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms as perl itself.

=head1 SEE ALSO

fwctl(8) Fwctl(3) IPChains(3)

=cut