Fwctl/RuleSet.pm untested

filter: Statementstmt 0.0% 0 / 528 Branchbran 0.0% 0 / 148 Conditioncond 0.0% 0 / 144 MC/DCmcdc 0.0% 0 / 96 Subroutinesub 0.0% 0 / 42 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 984 CC 75 SCAR 42.8
CC
75
Cov
0%
acct_ip_ruleset
210.0
accept_ip_ruleset
182.0
determine_base
110.0
CRAP 72.2
1 #
2 # RuleSet.pm - Module to add sets of rules to the linux firewall.
3 #
4 # This file is part of Fwctl.
5 #
6 # Author: Francis J. Lacoste <francis@iNsu.COM>
7 #
8 # Copyright (c) 1999, 2000 iNsu Innovations Inc.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 package Fwctl::RuleSet;
16  
17 use strict;
18  
19 use Net::IPv4Addr qw( ipv4_in_network ipv4_broadcast ipv4_parse );
20 use IPChains;
21 use Carp;
22  
23 use constant NOMASQ => 0;
24 use constant MASQ => 1;
25 use constant UNMASQ => 2;
26 use constant MASQNOHIGH => 4;
27 use constant PORTFW => 8;
28 use constant UNPORTFW => MASQ|MASQNOHIGH;
29  
30 use constant RESERVED_PORTS => "1:1023";
31 use constant UNPRIVILEGED_PORTS => "1024:65535";
32 use constant MASQ_PORTS => "61000:65096";
33  
34 use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS @ISA);
35  
36 BEGIN {
37 require Exporter;
38  
39 @ISA = ("Exporter");
40 @EXPORT = ();
41 @EXPORT_OK = ();
42  
43 %EXPORT_TAGS = (
44 masq => [ qw( NOMASQ MASQ UNMASQ PORTFW UNPORTFW MASQNOHIGH ) ],
45 ports => [ qw( RESERVED_PORTS UNPRIVILEGED_PORTS MASQ_PORTS ) ],
46 tcp_rulesets => [ qw( accept_tcp_ruleset block_tcp_ruleset
47 acct_tcp_ruleset ) ],
48 udp_rulesets => [ qw( accept_udp_ruleset block_udp_ruleset
49 acct_udp_ruleset) ],
50 ip_rulesets => [ qw( accept_ip_ruleset block_ip_ruleset
51 acct_ip_ruleset) ],
52 );
53  
54 Exporter::export_ok_tags( keys %EXPORT_TAGS );
55  
56 # Optional module
57 eval { use IPChains::PortFW };
58 }
59  
60  
61 # Determine the address spec and interface case
62 sub determine_case {
63 my ( $ip, $if ) = @_;
64  
65 my ($ip,$msklen) = ipv4_parse( $ip );
66  
67 return "ANY" if $if->{name} eq "ANY";
68 return "LOCAL_IP" if $if->{ip} eq $ip;
69 return "LOCAL_IMPLIED" if $if->{broadcast} eq $ip;
70 return "LOCAL_IMPLIED" if $ip eq "255.255.255.255";
71 return "REMOTE" if $ip eq "0.0.0.0" and $if->{name} eq 'EXT';
72 return "LOCAL_IMPLIED" if $ip eq "0.0.0.0";
73  
74 return "REMOTE" unless defined $msklen;
75  
76 return "LOCAL_IMPLIED" if ipv4_in_network( $ip, $msklen, $if->{ip} );
77 return "REMOTE";
78 }
79  
80 sub determine_base {
81 my ($ipchain) = shift;
82  
83 my $proto = $ipchain->attribute( 'Prot' );
84 return "all" if not defined $proto or $proto =~ /all/i;
85  
86 # Handle numeric protocol specification
87 my $proto_name = getprotobynumber $proto if $proto =~ /\d+/;
88 $proto = $proto_name if defined $proto_name;
89  
90 my $syn = 0;
91 my $ack = 0;
92 if ($proto =~ /tcp/i and defined $ipchain->attribute( 'SYN' ) ) {
93 $syn = $ipchain->attribute( 'SYN' ) eq '1';
94 $ack = $ipchain->attribute( 'SYN' ) eq '!';
95 }
96  
97 return "icmp" if $proto =~ /icmp/i;
98 return "udp" if $proto =~ /udp/i;
99 return "syn" if $syn;
100 return "ack" if $ack;
101 return "tcp" if $proto =~ /tcp/i;
102 return "oth";
103 }
104  
105 sub accept_tcp_ruleset {
106 my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw ) = @_;
107  
108 croak __PACKAGE__, ": protocol isn't TCP"
109 unless $ipchain->attribute( 'Prot' ) =~ /tcp/i;
110  
111 $masq = NOMASQ unless defined $masq;
112  
113 croak __PACKAGE__, ": can't use invalid \$masq and \$portfw combination"
114 if ( defined $portfw && ! $masq & PORTFW) ||
115 ( ! defined $portfw && $masq & PORTFW );
116  
117 my $syn = $ipchain->attribute( 'SYN' );
118 my $src_port = $ipchain->attribute( 'SourcePort' );
119 my $dst_port = $ipchain->attribute( 'DestPort' );
120  
121 # Client side
122 $ipchain->attribute( SYN => undef );
123 accept_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw );
124  
125 # Server side
126 $ipchain->attribute( SourcePort => $dst_port );
127 $ipchain->attribute( DestPort => $src_port );
128 $ipchain->attribute( SYN => '!' );
129  
130 # Switch from MASQ to UNMASQ
131 if ( $masq & MASQ ) {
132 $masq &= $masq ^ MASQ;
133 $masq |= UNMASQ;
134 } elsif ( $masq & PORTFW ) {
135 # Packets that were port forwarded should be masqueraded back
136 $masq &= $masq ^ PORTFW;
137 $masq |= UNPORTFW;
138 }
139 accept_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if, $masq, $portfw );
140  
141 # Restore params
142 $ipchain->attribute( SourcePort => $src_port );
143 $ipchain->attribute( DestPort => $dst_port );
144 $ipchain->attribute( SYN => $syn );
145 }
146  
147 sub accept_udp_ruleset {
148 my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw ) = @_;
149  
150 croak __PACKAGE__, ": protocol isn't UDP"
151 unless $ipchain->attribute( 'Prot' ) =~ /UDP/i;
152  
153 $masq = NOMASQ unless defined $masq;
154  
155 croak __PACKAGE__, ": can't use invalid \$masq and \$portfw combination"
156 if ( defined $portfw && ! $masq & PORTFW) ||
157 ( ! defined $portfw && $masq & PORTFW );
158  
159 my $src_port = $ipchain->attribute( 'SourcePort' );
160 my $dst_port = $ipchain->attribute( 'DestPort' );
161  
162 # Client side
163 accept_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw );
164  
165 # Server side
166 $ipchain->attribute( SourcePort => $dst_port );
167 $ipchain->attribute( DestPort => $src_port );
168 # Switch from MASQ to UNMASQ
169 if ( $masq & MASQ ) {
170 $masq &= $masq ^ MASQ;
171 $masq |= UNMASQ;
172 } elsif ( $masq & PORTFW ) {
173 # Packets that were port forwarded should be masqueraded back
174 $masq &= $masq ^ PORTFW;
175 $masq |= UNPORTFW;
176 }
177 accept_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if, $masq, $portfw );
178  
179 # Restore params
180 $ipchain->attribute( SourcePort => $src_port );
181 $ipchain->attribute( DestPort => $dst_port );
182 }
183  
184 # Adds protocol necessary ruleset for a type of packet
185 # to pass through the interface of the firewall.
186 sub accept_ip_ruleset {
187 my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq, $portfw ) = @_;
188  
189 croak __PACKAGE__, ": rule target must be ACCEPT: ", $ipchain->attribute( 'Rule' )
190 unless $ipchain->attribute( 'Rule' ) =~ /ACCEPT/;
191  
192 my $src_kind = determine_case( $src, $src_if );
193 my $dst_kind = determine_case( $dst, $dst_if );
194  
195 $ipchain->attribute( Source => $src );
196 $ipchain->attribute( Dest => $dst );
197  
198 my $base = determine_base( $ipchain );
199  
200 # Check masquerading parameter.
201 $masq = NOMASQ unless defined $masq;
202  
203 SWITCH:
204 for ("$src_kind-$dst_kind" ) {
205 /ANY-ANY|ANY-REMOTE|REMOTE-ANY/ && do {
206 ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
207 if ( $masq & MASQ ) {
208 ip_local_out_ruleset( @_[0..4], $base );
209 } elsif ( $masq & UNMASQ ) {
210 ip_local_in_ruleset( @_[0..4], $base );
211 }
212 last SWITCH;
213 };
214 /LOCAL_IMPLIED-LOCAL_IMPLIED/ && do {
215 # name handle the INTERNET or 0.0.0.0/0 case
216 # and ipv4_in_network handle the case of two routers scenario
217 if ( $src_if->{name} ne $dst_if->{name} || ! ipv4_in_network( $src, $dst ) ) {
218 # Src and dst networks are different
219 ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
220 if ( $masq & MASQ ) {
221 ip_local_out_ruleset( @_[0..4], $base );
222 } elsif ( $masq & UNMASQ ) {
223 ip_local_in_ruleset( @_[0..4], $base );
224 }
225 } else {
226 # No forwarding will take place
227 ip_local_in_ruleset( @_[0..4], $base );
228 }
229 last SWITCH;
230 };
231 /REMOTE-LOCAL_IMPLIED|LOCAL_IMPLIED-REMOTE/ && do {
232 # name handle the INTERNET or 0.0.0.0/0 case
233 # and ipv4_in_network handle the case of two routers scenario
234 if ( $src_if->{name} ne $dst_if->{name} || ! ipv4_in_network( $src, $dst ) ) {
235 # Src and dst networks are different
236 ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
237 } else {
238 # No forwarding will take place
239 ip_local_in_ruleset( @_[0..4], $base );
240 }
241 last SWITCH;
242 };
243 /ANY-LOCAL_IMPLIED/ && do {
244 ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
245 ip_loopback_out_ruleset( @_[0..4], $base );
246 if ( $masq & MASQ ) {
247 ip_local_out_ruleset( @_[0..4], $base );
248 } elsif ( $masq & UNMASQ ) {
249 ip_local_in_ruleset( @_[0..4], $base );
250 }
251 last SWITCH;
252 };
253 /REMOTE-REMOTE/ && do {
254 ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
255 last SWITCH;
256 };
257 /LOCAL_IMPLIED-ANY/ && do {
258 ip_forward_ruleset( @_[0..4], $base, $masq, $portfw );
259 ip_loopback_in_ruleset( @_[0..4], $base );
260 if ( $masq & MASQ ) {
261 ip_local_out_ruleset( @_[0..4], $base );
262 } elsif ( $masq & UNMASQ ) {
263 ip_local_in_ruleset( @_[0..4], $base );
264 }
265 last SWITCH;
266 };
267 /LOCAL_IP-ANY/ && do {
268 ip_local_out_ruleset( @_[0..4], $base );
269 ip_loopback_in_ruleset( @_[0..4], $base );
270 last SWITCH;
271 };
272 /LOCAL_IP-LOCAL_IMPLIED/ && do {
273 ip_local_out_ruleset( @_[0..4], $base );
274 ip_loopback_ruleset( @_[0..4], $base );
275 last SWITCH;
276 };
277 /ANY-LOCAL_IP/ && do {
278 ip_local_in_ruleset( @_[0..4], $base );
279 ip_loopback_out_ruleset( @_[0..4], $base );
280 last SWITCH;
281 };
282 /LOCAL_IMPLIED-LOCAL_IP/ && do {
283 ip_local_in_ruleset( @_[0..4], $base );
284 ip_loopback_ruleset( @_[0..4], $base );
285 last SWITCH;
286 };
287 /LOCAL_IP-LOCAL_IP/ && do {
288 ip_loopback_ruleset( @_[0..4], $base );
289 last SWITCH;
290 };
291 /REMOTE-LOCAL_IP/ && do {
292 ip_local_in_ruleset( @_[0..4], $base );
293 last SWITCH;
294 };
295 /LOCAL_IP-REMOTE/ && do {
296 ip_local_out_ruleset( @_[0..4], $base );
297 last SWITCH;
298 };
299 croak __PACKAGE__, ": unknown src/dst combination: $src/$dst\n";
300 }
301 }
302  
303  
304 # Add rule to necessary chains to handle
305 # loopback connection. Local process to
306 # local process.
307 sub ip_loopback_ruleset {
308 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;
309  
310 $ipchain->attribute( Interface => "lo" );
311 $ipchain->append( "$base-out" );
312 $ipchain->append( "$base-in" );
313 }
314  
315 sub ip_loopback_out_ruleset {
316 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;
317  
318 $ipchain->attribute( Interface => "lo" );
319 $ipchain->append( "$base-out" );
320  
321 }
322  
323 sub ip_loopback_in_ruleset {
324 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;
325  
326 $ipchain->attribute( Interface => "lo" );
327 $ipchain->append( "$base-in" );
328  
329 }
330  
331 # Add rule to necessary chains to handle
332 # an incoming packet destined to a local
333 # process
334 sub ip_local_out_ruleset {
335 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;
336  
337 $ipchain->attribute( Interface => $dst_if->{interface} );
338 $ipchain->append( "$base-out" );
339 }
340  
341 # Add rule to necessary chains to handle
342 # a outgoing local originating packet.
343 sub ip_local_in_ruleset {
344 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;
345  
346 $ipchain->attribute( Interface => $src_if->{interface} );
347 $ipchain->append( "$base-in" );
348 }
349  
350 # Add rule to necessary chains to handle
351 # a routed packet. Packet that aren't
352 # destined locally.
353 #
354 # src_if and dst_if should be different
355 # then lo.
356 sub ip_forward_ruleset {
357 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $portfw ) = @_;
358  
359 if ( $masq & MASQ ) {
360 SWITCH:
361 for ($base) {
362 /tcp|ack|syn/ && do {
363 tcp_masq_forward_ruleset( @_ );
364 last SWITCH;
365 };
366 /udp/ && do {
367 udp_masq_forward_ruleset( @_ );
368 last SWITCH;
369 };
370 /icmp|oth|all/ && do {
371 ip_masq_forward_ruleset( @_ );
372 last SWITCH;
373 };
374 }
375 } elsif ($masq & UNMASQ ) {
376 SWITCH:
377 for ($base) {
378 /tcp|ack|syn/ && do {
379 tcp_unmasq_forward_ruleset( @_ );
380 last SWITCH;
381 };
382 /udp/ && do {
383 udp_unmasq_forward_ruleset( @_ );
384 last SWITCH;
385 };
386 /icmp|oth|all/ && do {
387 ip_unmasq_forward_ruleset( @_ );
388 last SWITCH;
389 };
390 }
391 } elsif ( $masq & PORTFW ) {
392 if ($base =~ /tcp|ack|syn|udp/ ) {
393 tcpudp_portfw_forward_ruleset( @_ );
394 } else {
395 ip_portfw_forward_ruleset( @_ );
396 }
397 } else {
398 $ipchain->attribute( Interface => $src_if->{interface} );
399 $ipchain->append( "$base-in" );
400 $ipchain->attribute( Interface => $dst_if->{interface} );
401 $ipchain->append( "$base-fwd" );
402 $ipchain->append( "$base-out" );
403 }
404 }
405  
406 # Add rule to necessary chains to handle
407 # a route TCP masqueraded packet.
408 sub tcp_masq_forward_ruleset {
409 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $masqip ) = @_;
410  
411 $ipchain->attribute( Interface => $src_if->{interface} );
412 $ipchain->append( "$base-in" );
413  
414 my $rule = $ipchain->attribute( 'Rule' );
415 $ipchain->attribute( Rule => 'MASQ' );
416 $ipchain->attribute( Interface => $dst_if->{interface} );
417 $ipchain->append( "$base-fwd" );
418 $ipchain->attribute( Rule => $rule );
419  
420 my $saved_ports = $ipchain->attribute('SourcePort');
421 my $ip = $masqip ? $masqip : $dst_if->{ip};
422 $ipchain->attribute( Source => $ip );
423 $ipchain->attribute( SourcePort => MASQ_PORTS ) unless $masq & MASQNOHIGH;
424 $ipchain->append( "$base-out" );
425 $ipchain->attribute( Source => $src );
426 $ipchain->attribute( SourcePort => $saved_ports );
427 }
428  
429  
430 sub udp_masq_forward_ruleset {
431 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $masqip ) = @_;
432  
433 $ipchain->attribute( Interface => $src_if->{interface} );
434 $ipchain->append( "$base-in" );
435 my $rule = $ipchain->attribute( 'Rule' );
436 $ipchain->attribute( Rule => 'MASQ' );
437 $ipchain->attribute( Interface => $dst_if->{interface} );
438 $ipchain->append( "$base-fwd" );
439 $ipchain->attribute( Rule => $rule );
440  
441 my $saved_ports = $ipchain->attribute('SourcePort');
442 my $ip = $masqip ? $masqip : $dst_if->{ip};
443 $ipchain->attribute( Source => $ip );
444 $ipchain->attribute( SourcePort => MASQ_PORTS ) unless $masq & MASQNOHIGH;
445 $ipchain->append( "$base-out" );
446 $ipchain->attribute( Source => $src );
447 $ipchain->attribute( SourcePort => $saved_ports );
448 }
449  
450 sub ip_masq_forward_ruleset {
451 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;
452  
453 $ipchain->attribute( Interface => $src_if->{interface} );
454 $ipchain->append( "$base-in" );
455 my $rule = $ipchain->attribute( 'Rule' );
456 $ipchain->attribute( Rule => 'MASQ' );
457 $ipchain->attribute( Interface => $dst_if->{interface} );
458 $ipchain->append( "$base-fwd" );
459 $ipchain->attribute( Rule => $rule );
460  
461 $ipchain->attribute( Source => $dst_if->{ip} );
462 $ipchain->append( "$base-out" );
463 $ipchain->attribute( Source => $src );
464 }
465  
466 # Add rule to necessary chains to handle
467 # a route TCP packet which was masqueraded.
468 sub tcp_unmasq_forward_ruleset {
469 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq ) = @_;
470  
471 my $saved_ports = $ipchain->attribute('DestPort');
472 $ipchain->attribute( Interface => $src_if->{interface} );
473 $ipchain->attribute( Dest => $src_if->{ip} );
474 $ipchain->attribute( DestPort => MASQ_PORTS ) unless $masq & MASQNOHIGH;
475 $ipchain->append( "$base-in" );
476 $ipchain->attribute( Dest => $dst );
477 $ipchain->attribute( DestPort => $saved_ports );
478  
479 $ipchain->attribute( Interface => $dst_if->{interface} );
480 $ipchain->append( "$base-out" );
481 }
482  
483 sub udp_unmasq_forward_ruleset {
484 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq ) = @_;
485  
486 my $saved_ports = $ipchain->attribute('DestPort');
487 $ipchain->attribute( Interface => $src_if->{interface} );
488 $ipchain->attribute( Dest => $src_if->{ip} );
489 $ipchain->attribute( DestPort => MASQ_PORTS ) unless $masq & MASQNOHIGH;
490 $ipchain->append( "$base-in" );
491 $ipchain->attribute( Dest => $dst );
492 $ipchain->attribute( DestPort => $saved_ports );
493  
494 $ipchain->attribute( Interface => $dst_if->{interface} );
495 $ipchain->append( "$base-out" );
496 }
497  
498 sub ip_unmasq_forward_ruleset {
499 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base ) = @_;
500  
501 my $saved_ports = $ipchain->attribute('DestPort');
502 $ipchain->attribute( Interface => $src_if->{interface} );
503 $ipchain->attribute( Dest => $src_if->{ip} );
504 $ipchain->append( "$base-in" );
505 $ipchain->attribute( Dest => $dst );
506  
507 $ipchain->attribute( Interface => $dst_if->{interface} );
508 $ipchain->append( "$base-out" );
509 }
510  
511  
512 sub tcpudp_portfw_forward_ruleset {
513 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $portfw ) = @_;
514 $portfw ||= $src_if->{ip};
515  
516 # Incoming address is in $portfw
517 $ipchain->attribute( Interface => $src_if->{interface} );
518 $ipchain->attribute( Dest => $portfw );
519 $ipchain->append( "$base-in" );
520  
521 $ipchain->attribute( Dest => $dst );
522  
523 $ipchain->attribute( Interface => $dst_if->{interface} );
524 $ipchain->append( "$base-fwd" );
525 $ipchain->append( "$base-out" );
526  
527 # Add port forwarding rule
528 my $proto = $ipchain->attribute( 'Prot' );
529 my $port = $ipchain->attribute( 'DestPort' );
530  
531 my $portfw_chain = new IPChains::PortFW( Proto => $proto,
532 LocalAddr => $portfw,
533 LocalPort => $port,
534 RemAddr => $dst,
535 RemPort => $port
536 );
537 $portfw_chain->append;
538  
539 }
540  
541 # We just set up the rules here. The user will have
542 # to set the ipfwd daemon manually
543 sub ip_portfw_forward_ruleset {
544 my ( $ipchain, $src, $src_if, $dst, $dst_if, $base, $masq, $portfw ) = @_;
545 $portfw ||= $src_if->{ip};
546  
547 # Incoming address is in $portfw
548 $ipchain->attribute( Interface => $src_if->{interface} );
549 $ipchain->attribute( Dest => $portfw );
550 $ipchain->append( "$base-in" );
551  
552 $ipchain->attribute( Dest => $dst );
553  
554 $ipchain->attribute( Interface => $dst_if->{interface} );
555 $ipchain->append( "$base-fwd" );
556 $ipchain->append( "$base-out" );
557 }
558  
559 sub block_tcp_ruleset {
560 my ( $ipchain, $src, $src_if, $dst, $dst_if ) = @_;
561  
562 croak __PACKAGE__, ": protocol isn't TCP"
563 unless $ipchain->attribute( 'Prot' ) =~ /tcp/i;
564  
565 my $syn = $ipchain->attribute( 'SYN' );
566 my $src_port = $ipchain->attribute( 'SourcePort' );
567 my $dst_port = $ipchain->attribute( 'DestPort' );
568  
569 # Client side
570 $ipchain->attribute( SYN => undef );
571 block_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if );
572  
573 # Server side
574 $ipchain->attribute( SourcePort => $dst_port );
575 $ipchain->attribute( DestPort => $src_port );
576 $ipchain->attribute( SYN => '!' );
577 block_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if );
578  
579 # Restore params
580 $ipchain->attribute( SourcePort => $src_port );
581 $ipchain->attribute( DestPort => $dst_port );
582 $ipchain->attribute( SYN => $syn );
583  
584 }
585  
586 sub block_udp_ruleset {
587 my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq ) = @_;
588  
589 croak __PACKAGE__, ": protocol isn't UDP"
590 unless $ipchain->attribute( 'Prot' ) =~ /UDP/i;
591  
592 $masq = NOMASQ unless defined $masq;
593  
594 my $src_port = $ipchain->attribute( 'SourcePort' );
595 my $dst_port = $ipchain->attribute( 'DestPort' );
596  
597 # Client side
598 block_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if );
599  
600 # Server side
601 $ipchain->attribute( SourcePort => $dst_port );
602 $ipchain->attribute( DestPort => $src_port );
603 block_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if );
604  
605 # Restore params
606 $ipchain->attribute( SourcePort => $src_port );
607 $ipchain->attribute( DestPort => $dst_port );
608 }
609  
610 sub block_ip_ruleset {
611 my ( $ipchain, $src, $src_if, $dst, $dst_if ) = @_;
612  
613 my $src_kind = determine_case( $src, $src_if );
614 my $dst_kind = determine_case( $dst, $dst_if );
615  
616 croak __PACKAGE__, ": rule target must be REJECT or DENY: ", $ipchain->attribute( 'Rule' )
617 unless $ipchain->attribute( 'Rule' ) =~ /DENY|REJECT/;
618  
619 $ipchain->attribute( Source => $src );
620 $ipchain->attribute( Dest => $dst );
621  
622 my $base = determine_base( $ipchain );
623  
624 SWITCH:
625 for ("$src_kind-$dst_kind" ) {
626 /ANY-ANY|LOCAL_IMPLIED-ANY|ANY-REMOTE|LOCAL_IMPLIED-REMOTE/ && do {
627 $ipchain->attribute( Interface => $src_if->{interface} );
628 $ipchain->append( "$base-in" );
629 $ipchain->attribute( Interface => $dst_if->{interface} );
630 $ipchain->append( "$base-out" );
631 last SWITCH;
632 };
633 /ANY-LOCAL_IMPLIED|LOCAL_IMPLIED-LOCAL_IMPLIED/ && do {
634 $ipchain->attribute( Interface => $src_if->{interface} );
635 $ipchain->append( "$base-in" );
636 $ipchain->attribute( Interface => $dst_if->{interface} );
637 $ipchain->append( "$base-out" );
638 $ipchain->attribute( Interface => "lo" );
639 $ipchain->append( "$base-out" );
640 last SWITCH;
641 };
642 /LOCAL_IP-ANY|LOCAL_IP-LOCAL_IMPLIED/ && do {
643 $ipchain->attribute( Interface => $dst_if->{interface} );
644 $ipchain->append( "$base-out" );
645 $ipchain->attribute( Interface => "lo" );
646 $ipchain->append( "$base-out" );
647 last SWITCH;
648 };
649 /ANY-LOCAL_IP|LOCAL_IMPLIED-LOCAL_IP/ && do {
650 $ipchain->attribute( Interface => $src_if->{interface} );
651 $ipchain->append( "$base-in" );
652 $ipchain->attribute( Interface => "lo" );
653 $ipchain->append( "$base-out" );
654 last SWITCH;
655 };
656 /LOCAL_IP-LOCAL_IP/ && do {
657 $ipchain->attribute( Interface => "lo" );
658 $ipchain->append( "$base-out" );
659 last SWITCH;
660 };
661 /LOCAL_IP-REMOTE/ && do {
662 $ipchain->attribute( Interface => $dst_if->{interface} );
663 $ipchain->append( "$base-out" );
664 last SWITCH;
665 };
666 /REMOTE-.*/ && do {
667 $ipchain->attribute( Interface => $src_if->{interface} );
668 $ipchain->append( "$base-in" );
669 last SWITCH;
670 };
671 croak __PACKAGE__, ": unknown src/dst combination: $src/$dst\n";
672 }
673 }
674  
675 sub acct_tcp_ruleset {
676 my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq ) = @_;
677  
678 croak __PACKAGE__, ": protocol isn't TCP"
679 unless $ipchain->attribute( 'Prot' ) =~ /tcp/i;
680  
681 $masq = NOMASQ unless defined $masq;
682  
683 my $syn = $ipchain->attribute( 'SYN' );
684 my $src_port = $ipchain->attribute( 'SourcePort' );
685 my $dst_port = $ipchain->attribute( 'DestPort' );
686  
687 # Client side
688 $ipchain->attribute( SYN => undef );
689 acct_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if, $masq );
690  
691 # Server side
692 $ipchain->attribute( SourcePort => $dst_port );
693 $ipchain->attribute( DestPort => $src_port );
694 $ipchain->attribute( SYN => '!' );
695 # Switch from MASQ to UNMASQ
696 if ( $masq & MASQ ) {
697 $masq &= $masq ^ MASQ;
698 $masq |= UNMASQ;
699 } elsif ( $masq & PORTFW ) {
700 # Packets that were port forwarded should be masqueraded back
701 $masq &= $masq ^ PORTFW;
702 $masq |= UNPORTFW;
703 }
704 acct_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if, $masq );
705  
706 # Restore params
707 $ipchain->attribute( SourcePort => $src_port );
708 $ipchain->attribute( DestPort => $dst_port );
709 $ipchain->attribute( SYN => $syn );
710  
711 }
712  
713 sub acct_udp_ruleset {
714 my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq ) = @_;
715  
716 croak __PACKAGE__, ": protocol isn't UDP"
717 unless $ipchain->attribute( 'Prot' ) =~ /UDP/i;
718  
719 $masq = NOMASQ unless defined $masq;
720  
721 my $src_port = $ipchain->attribute( 'SourcePort' );
722 my $dst_port = $ipchain->attribute( 'DestPort' );
723  
724 # Client side
725 acct_ip_ruleset( $ipchain, $src, $src_if, $dst, $dst_if, $masq );
726  
727 # Server side
728 $ipchain->attribute( SourcePort => $dst_port );
729 $ipchain->attribute( DestPort => $src_port );
730  
731 # Switch from MASQ to UNMASQ
732 if ( $masq & MASQ ) {
733 $masq &= $masq ^ MASQ;
734 $masq |= UNMASQ;
735 } elsif ( $masq & PORTFW ) {
736 # Packets that were port forwarded should be masqueraded back
737 $masq &= $masq ^ PORTFW;
738 $masq |= UNPORTFW;
739 }
740 acct_ip_ruleset( $ipchain, $dst, $dst_if, $src, $src_if, $masq );
741  
742 # Restore params
743 $ipchain->attribute( SourcePort => $src_port );
744 $ipchain->attribute( DestPort => $dst_port );
745 }
746  
747 sub acct_ip_ruleset {
748 my ( $ipchain, $src, $src_if, $dst, $dst_if, $masq ) = @_;
749  
750 croak __PACKAGE__, ": bad rule target: ", $ipchain->attribute( 'Rule' )
751 unless $ipchain->attribute( 'Rule' ) =~ /acct\d{4}/;
752  
753 my $src_kind = determine_case( $src, $src_if );
754 my $dst_kind = determine_case( $dst, $dst_if );
755  
756 $ipchain->attribute( Source => $src );
757 $ipchain->attribute( Dest => $dst );
758  
759 my $base = determine_base( $ipchain );
760  
761 # Check masquerading parameter.
762 $masq = NOMASQ unless defined $masq;
763  
764 SWITCH:
765 for ("$src_kind-$dst_kind" ) {
766 /LOCAL_IP-ANY|LOCAL_IP-REMOTE/ && do {
767 $ipchain->attribute( Interface => $dst_if->{interface} );
768 $ipchain->append( "acct-out" );
769 last SWITCH;
770 };
771 /ANY-LOCAL_IP|REMOTE-LOCAL_IP/ && do {
772 $ipchain->attribute( Interface => $src_if->{interface} );
773 $ipchain->append( "acct-in" );
774 last SWITCH;
775 };
776 /LOCAL_IMPLIED-LOCAL_IP/ && do {
777 $ipchain->attribute( Interface => $src_if->{interface} );
778 $ipchain->append( "acct-in" );
779 $ipchain->attribute( Interface => "lo" );
780 $ipchain->append( "acct-out" );
781 last SWITCH;
782 };
783 /LOCAL_IP-LOCAL_IMPLIED/ && do {
784 $ipchain->attribute( Interface => $dst_if->{interface} );
785 $ipchain->append( "acct-out" );
786 $ipchain->attribute( Interface => "lo" );
787 $ipchain->append( "acct-out" );
788 last SWITCH;
789 };
790 /LOCAL_IP-LOCAL_IP/ && do {
791 $ipchain->attribute( Interface => "lo" );
792 $ipchain->append( "acct-out" );
793 last SWITCH;
794 };
795 /ANY-REMOTE|LOCAL_IMPLIED-REMOTE/ && do {
796 $ipchain->attribute( Interface => $dst_if->{interface} );
797 $ipchain->append( "acct-out" );
798 if ($masq & MASQ ) {
799 $ipchain->attribute( Source => $dst_if->{ip} );
800  
801 # UDP and TCP gets their ports rewritten
802 if ( $base =~ /tcp|udp|syn|ack/ ) {
803 my $saved_ports = $ipchain->attribute( 'SourcePort' );
804 $ipchain->attribute( SourcePort => MASQ_PORTS )
805 unless $masq & MASQNOHIGH;
806 $ipchain->append( "acct-out" );
807 $ipchain->attribute( SourcePort => $saved_ports );
808 } else {
809 $ipchain->append( "acct-out" );
810 }
811 $ipchain->attribute( Source => $src );
812 }
813 last SWITCH;
814 };
815 /REMOTE-REMOTE/ && do {
816 $ipchain->attribute( Interface => $dst_if->{interface} );
817 if ($masq & MASQ ) {
818 $ipchain->attribute( Source => $dst_if->{ip} );
819 # UDP and TCP gets their ports rewritten
820 if ( $base =~ /tcp|udp|syn|ack/ ) {
821 my $saved_ports = $ipchain->attribute( 'SourcePort' );
822 $ipchain->attribute( SourcePort => MASQ_PORTS )
823 unless $masq & MASQNOHIGH;
824 $ipchain->append( "acct-out" );
825 $ipchain->attribute( SourcePort => $saved_ports );
826 } else {
827 $ipchain->append( "acct-out" );
828 }
829 $ipchain->attribute( Source => $src );
830 } else {
831 $ipchain->append( "acct-out" );
832 }
833 last SWITCH;
834 };
835 /REMOTE-ANY|REMOTE-LOCAL_IMPLIED/ && do {
836 $ipchain->attribute( Interface => $src_if->{interface} );
837 $ipchain->append( "acct-in" );
838 if ($masq & UNMASQ ) {
839 $ipchain->attribute( Dest => $src_if->{ip} );
840 # UDP and TCP gets their ports rewritten
841 if ( $base =~ /tcp|udp|syn|ack/ ) {
842 my $saved_ports = $ipchain->attribute( 'DestPort' );
843 $ipchain->attribute( DestPort => MASQ_PORTS );
844 $ipchain->append( "acct-in" );
845 $ipchain->attribute( DestPort => $saved_ports );
846 } else {
847 $ipchain->append( "acct-in" );
848 }
849 $ipchain->attribute( Dest => $dst );
850 } else {
851 }
852 last SWITCH;
853 };
854 /ANY-ANY|ANY-LOCAL_IMPLIED|LOCAL_IMPLIED-ANY|LOCAL_IMPLIED-LOCAL_IMPLIED/ && do {
855 # Out packet
856 $ipchain->attribute( Interface => "lo" ); # Loopback
857 $ipchain->append( "acct-out" );
858 $ipchain->attribute( Interface => $dst_if->{interface} );
859 if ($masq & MASQ ) {
860 $ipchain->attribute ( Source => $dst_if->{ip} );
861 if ($base =~ /tcp|syn|ack|udp/) {
862 my $saved_ports = $ipchain->attribute( 'SourcePort' );
863 $ipchain->attribute( SourcePort => MASQ_PORTS )
864 unless $masq & MASQ_PORTS;
865 $ipchain->append( "acct-out" );
866 $ipchain->attribute( SourcePort => $saved_ports );
867 } else {
868 $ipchain->append( "acct-out" );
869 }
870 $ipchain->attribute( Source => $src );
871 } else {
872 $ipchain->append( "acct-out" );
873 }
874 last SWITCH;
875 };
876 croak __PACKAGE__, ": unknown src/dst combination: $src/$dst\n";
877 }
878 }
879  
880 1;
881  
882 =pod
883  
884 =head1 NAME
885  
886 Fwctl::RuleSet - Module to add sets of rules to the linux firewall.
887  
888 =head1 SYNOPSIS
889  
890 use IPChains;
891 use Fwctl::RuleSet qw(:masq :tcp_rulesets :ports);
892  
893 my $chain = new IPChains( Prot => 'tcp',
894 SourcePort => UNPRIVILEGED_PORTS,
895 DestPort => 23,
896 )
897 accept_tcp_ruleset( $chain, $src, $src_if, $dst, $dst_if, NOMASQ );
898  
899 =head1 DESCRIPTION
900  
901 This module contains primitives to add sets of rules to the Linux
902 packet filtering firewall implementing a particular policy. It is used
903 primarly by service modules. The module handle all the special cases
904 for when the src or dst interface is ANY, when masquerading is involved,
905 when a local ip is implied by the src or dst address. All this logic
906 has not to be implemented by the service modules, which only have to
907 specify the kind of packets and the direction of traffic (using the src and
908 dst paremeter).
909  
910 There are 5 tags that can be imported from the modules.
911  
912 =over
913  
914 =item :masq
915  
916 Constant used to specify how to handle masquerade.
917  
918 =item :ports
919  
920 Constants that refers to range of ports.
921  
922 =item :tcp_rulesets
923  
924 Functions that implements policy rulesets for TCP connection.
925  
926 =item :udp_rulesets
927  
928 Functions that implements policy rulesets for bidirectional UDP traffic.
929  
930 =item :ip_rulesets
931  
932 Funtions that implements policy rulesets for IP traffic. This are the
933 primitives on which the tcp and udp rulesets are built.
934  
935 =back
936  
937 =head1 :masq
938  
939 =over
940  
941 =item NOMASQ
942  
943 Constant used to represent that the traffic shouldn't be masqueraded.
944  
945 =item MASQ
946  
947 Constant use to denote that this traffic will be masqueraded when
948 going throught the forward chain.
949  
950 =item UNMASQ
951  
952 Constant use to denote that traffic should be unmasqueraded when passing
953 the input chain.
954  
955 =back
956  
957 To better understand the way the MASQ and UNMASQ constants works together
958 lets look at how they would be use to handle a TCP connection.
959  
960 accept_ip_rulesets( $chain, $src, $src_if, $dst, $dst_if, MASQ );
961 $chain->attribute( SYN => '!' );
962 accept_ip_rulesets( $chain, $dst, $dst_if, $src, $src_if, UNMASQ);
963  
964 =head1 :ports
965  
966 =over
967  
968 =item RESERVED_PORTS
969  
970 Constant that represents the ports 1 through 1023.
971  
972 =item UNPRIVILEGED_PORTS
973  
974 Constant that represents the ports 1024 through 65535.
975  
976 =item MASQ_PORTS
977  
978 Constant that represents the ports used when masquerading a
979 connection : 61000 through 65096.
980  
981 =back
982  
983 =head1 :ip_rulesets
984  
985 This tags imports three functions that are the primitives on which
986 the others are built. All src or dst can be classified in one of
987 four category.
988 =over
989  
990 =item ANY
991  
992 Source or destination is any address on any interface.
993  
994 =item LOCAL_IP
995  
996 Source or destination is a local interface
997  
998 =item LOCAL_IMPLIED
999  
1000 Source or destination implied a local interface. Example of those
1001 includes a broadcast address of a local interface or network address
1002 of a local interface.
1003  
1004 =item REMOTE
1005  
1006 Source or destination doesn't imply a local IP.
1007  
1008 =back
1009  
1010 So this means a total of 16 combination of source and destination address.
1011 Add the parameter MASQ,UNMASQ and NOMASQ and you got 48 possibilities. Those
1012 usually can be reduced to between 7 and 16 cases depending on the policy
1013 you want to handle. (REJECT, DENY, ACCEPT or ACCOUNT). The following
1014 functions handle all those possibilities for you, and adds the appropriate
1015 rules with address and interface specification to the appropriate chains.
1016  
1017 =over
1018  
1019 =item accept_ip_ruleset($chain,$src,$src_if,$dst,$dst_if,$masq)
1020  
1021 Adds the necessary rules to accept the kind of traffic specified by
1022 the $chain parameter.
1023  
1024 =over
1025  
1026 =item $chain
1027  
1028 IPChains objects that contains the prototypes of the rules to add to the
1029 firewall. Source, Dest and Interface parameter are overwritten by the
1030 function.
1031  
1032 =item $src
1033  
1034 The source address of the packet.
1035  
1036 =item $src_if
1037  
1038 The interface associated to the $src address.
1039  
1040 =item $dst
1041  
1042 The destination address of the packet.
1043  
1044 =item $dst_if
1045  
1046 The interface associated to the $dst address.
1047  
1048 =item $masq
1049  
1050 How the packet should be masqueraded.
1051  
1052 =back
1053  
1054 Usually the $src, $src_if, $dst and $dst_if packets are not modified
1055 by the service modules and are those passed by the Fwctl module. Or
1056 the module will switch them (dst becomes src), or change them because
1057 the protocol uses broadcast or other stuff.
1058  
1059 =item block_ip_ruleset( $chain, $src, $src_if, $dst, $dst_if )
1060  
1061 This primitive handles both REJECT and DENY policies. The parameter
1062 have the same meaning as in the accept_ip_ruleset() function.
1063  
1064 =item account_ip_ruleset( $chain, $src, $src_if, $dst, $dst_if )
1065  
1066 This primitive handles the ACCOUNT policy. The parameter
1067 have the same meaning as in the accept_ip_ruleset() function.
1068  
1069 =back
1070  
1071 =head1 :tcp_rulesets
1072  
1073 This tags imports three functions: accept_tcp_ruleset(),
1074 block_tcp_ruleset() and account_tcp_ruleset() which have the same
1075 parameters and semantics as their *_ip_ruleset() counterpart.
1076 They are indeed implemented in terms of these.
1077  
1078 The difference is that the $chain parameter can only be used to
1079 represent a TCP connection. The functions will add rules for the
1080 client and server side of the connection with the SYN and ACK flags
1081 handled properly.
1082  
1083 =head1 :udp_rulesets
1084  
1085 This tags imports three functions: accept_udp_ruleset(),
1086 block_udp_ruleset() and account_udp_ruleset() which have the same
1087 parameters and semantics as their *_ip_ruleset() counterpart.
1088 They are indeed implemented in terms of these.
1089  
1090 These functions will add rules to handle client / server UDP connection.
1091 It like calling the *_ip_ruleset() functions two times with the
1092 src and dst inversed (the SourcePort and DestPort are naturally also
1093 inversed).
1094  
1095  
1096 =head1 AUTHOR
1097  
1098 Francis J. Lacoste <francis.lacoste@iNsu.COM>
1099  
1100 =head1 COPYRIGHT
1101  
1102 Copyright (c) 1999,2000 iNsu Innovations Inc.
1103 All rights reserved.
1104  
1105 This program is free software; you can redistribute it and/or modify
1106 it under the terms of the GNU General Public License as published by
1107 the Free Software Foundation; either version 2 of the License, or
1108 (at your option) any later version.
1109  
1110 =head1 SEE ALSO
1111  
1112 fwctl(8) Fwctl(3) IPChains(3)
1113  
1114 =cut
1115