blib/lib/BSD/Ipfwgen.pm untested

filter: Statementstmt 0.0% 0 / 616 Branchbran 0.0% 0 / 204 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 0.0% 0 / 57 Podpod 0.0% 0 / 53 totaltotal 0.0% 0 / 1025 CC 103 SCAR 42.6
CC
103
Cov
0%
get_netmask
306.0
get_routes
110.0
no_spoofing_us
110.0
CRAP 71.1
1  
2 #
3 # BSD::Ipfwgen
4 #
5 # Copyright (C) 1998, David Muir Sharnoff. All rights reserved.
6 # License hearby granted for anyone to use this module at their own risk.
7 # Please feed useful changes back to muir@idiom.com.
8 #
9  
10  
11 #
12 # For each of the sets (recv, xmit, network, protocol), build
13 # the ruleset like:
14 #
15 # =skiprule all from any to any recv interface
16 # =skipto end-section all from any to any
17 # do stuff
18 # =label end-section
19 #
20  
21 package BSD::Ipfwgen;
22  
23 my $ifconfig = "/sbin/ifconfig";
24 my $netstat = "/usr/bin/netstat";
25 my $ipfw = "/sbin/ipfw";
26  
27 use vars qw($VERSION);
28 $VERSION = 1.5;
29  
30 require Exporter;
31  
32 @ISA = qw(Exporter);
33 @EXPORT = qw(
34 outside leaf
35 us not_us consolidate symmetric
36 count_by_interface count_by_address
37 count_by_udp count_by_tcp
38 no_looping no_spoofing_us no_spoofing_by_us no_leaf_spoofing
39 generate
40 drop_unwanted
41 tcp_from_rules udp_from_rules
42 tcp_to_rules udp_to_rules
43 from_net_rules to_net_rules
44 to_me_rules from_me_rules
45 not_to_me_rules not_from_me_rules
46 in_interface_rules out_interface_rules
47 from_us to_us
48 );
49  
50 my @from_us;
51 my @to_us;
52 my @outside;
53 my @leaf;
54 my @us;
55 my @not_us;
56 my @symmetric;
57 my @interesting;
58 my %consolidate;
59 my $if0; # first interface
60  
61 use Carp;
62  
63 # BEGIN { use IO::Handle; open(DEBUG, ">&STDERR"); autoflush DEBUG 1; }
64 BEGIN { open(DEBUG, ">/dev/null"); };
65  
66 sub get_netmask
67 {
68 my ($net, $mask) = @_;
69 $mask = '' unless defined $mask;
70 if ($net =~ m,^(\d+\.\d+\.\d+\.\d+):(\d+\.\d+\.\d+\.\d+)$,) {
71 my $base = $1;
72 my $dotmask = $2;
73 my $bits = 0;
74 for $c (split(/\./, $dotmask)) {
75 $bits += 8-int(log(256-$c)/log(2));
76 last unless $c == 255;
77 }
78 return($base, $bits);
79 } elsif ($net =~ m,^(\d+\.\d+\.\d+\.\d+)/(\d+)$,) {
80 return ($1, $2);
81 } elsif (($net =~ m,^\d+\.\d+\.\d+\.\d+$,) &&
82 ($mask =~ m,0x[a-z0-9]+,i)) {
83 use integer;
84 my $nm = hex($mask);
85 my $bits = 32;
86 while ((($nm & 0x1) == 0) && ($bits > 0)) {
87 $bits--;
88 $nm >>= 1;
89 }
90 die if ($bits < 16);
91 die unless ($net =~ /^(\d+\.\d+)\.(\d+)\.(\d+)$/);
92 my ($b, $c, $a) = ($1, $2, $3);
93 if ($bits < 24) {
94 $a = 0;
95 $b &= 255-2**(23-$bits);
96 } elsif ($bits < 32) {
97 $a &= 256-2**(31-$bits);
98 }
99 my $base = "$b.$c.$a";
100 print DEBUG "get_netmask($net, $mask) = $base/$bits\n";
101 return ($base, $bits);
102 } elsif ($net =~ /^\d+\.\d+\.\d+\.\d+$/ && ! $mask) {
103 return ($net, 32);
104 } elsif ($net =~ /^\d+\.\d+\.\d+$/ && ! $mask) {
105 return ("$net.0", 24);
106 } elsif ($net =~ /^\d+\.\d+$/ && ! $mask) {
107 return ("$net.0.0", 16);
108 } elsif ($net =~ /^\d+$/ && ! $mask) {
109 return ("$net.0.0.0", 8);
110 } elsif ($net =~ m,^(\d+\.\d+\.\d+)/(\d+)$,) {
111 return ("$1.0", $2);
112 } elsif ($net =~ m,^(\d+\.\d+)/(\d+)$,) {
113 return ("$1.0.0", $2);
114 } elsif ($net eq 'default') {
115 return ("0.0.0.0", 0);
116 } else {
117 die "could not parse $net $mask";
118 }
119 }
120  
121  
122 sub watch
123 {
124 my (@net) = @_;
125 for my $net (@net) {
126 print DEBUG "watch $net\n";
127 confess unless defined $net;
128 my ($network, $bits) = get_netmask($net);
129 $network =~ /^(\d+\.\d+\.\d+)\.(\d+)$/
130 or die "could not grok netnum $network";
131 $watch{$1} = 1;
132 }
133 }
134  
135 # interfaces
136 sub outside { push(@outside, @_); }
137 sub leaf { push(@leaf, @_); };
138  
139 # networks
140 sub interesting { push(@interesting, @_); watch(@_); }
141 sub us { push(@us, @_); watch(@_); };
142 sub not_us { push(@not_us, @_); watch(@_); };
143 sub symmetric { push(@symmetric, @_); watch(@_); };
144 sub from_us { push(@from_us, @_); watch(@_); };
145 sub to_us { push(@to_us, @_); watch(@_); };
146  
147 sub mark_addresses
148 {
149 my ($aref, $network, $bits, $value, $rem) = @_;
150 die "can only consolidate class Bs or smaller"
151 if $bits < 16;
152  
153 if ($bits < 24) {
154 $network =~ /^(\d+\.\d+)\.(\d+)\.\d+$/
155 or die "could not grok netnum $network";
156 my ($base, $ext) = ($1, $2);
157 my $count = 2**(24-$bits);
158 while ($count > 0) {
159 for my $j (0..255) {
160 $aref->{"$base.$ext.$j"} = $value;
161 print DEBUG "$rem mark{$base.$ext.$j} = $value\n";
162 }
163 $ext++;
164 $count--;
165 }
166 } else {
167 $network =~ /^(\d+\.\d+\.\d+)\.(\d+)$/
168 or die "could not grok netnum $network";
169 my ($base, $ext) = ($1, $2);
170 my $count = 2**(32-$bits);
171 while ($count > 0) {
172 $aref->{"$base.$ext"} = $value;
173 print DEBUG "rem $rem\n";
174 print DEBUG "base $base\n";
175 print DEBUG "ext $ext\n";
176 print DEBUG "value $value\n";
177 confess unless $value;
178 print DEBUG "$rem mark{$base.$ext} = $value\n";
179 $ext++;
180 $count--;
181 }
182 }
183 }
184  
185 sub consolidate
186 {
187 my (@consolidate) = @_;
188 for my $c (@consolidate) {
189 print DEBUG "consolidate $c\n";
190 my ($network, $bits) = get_netmask($c);
191 mark_addresses(\%consolidate, $network, $bits, $c, 'consolidate');
192 }
193 }
194  
195 my %interfaces;
196  
197 sub get_direct_nets
198 {
199 my ($if) = @_;
200  
201 my @n;
202 for my $i (0..$#{$interfaces{$if}->{'IP'}}) {
203 my $ip = $interfaces{$if}->{'IP'}->[$i];
204 print DEBUG "ip($if) = $ip\n";
205 if (defined $interfaces{$if}->{'NETMASK'}) {
206 my ($base, $bits) = get_netmask(
207 $ip, $interfaces{$if}->{'NETMASK'}->[$i]);
208 push(@n, "$base/$bits");
209 } else {
210 push(@n, $ip);
211 }
212 }
213 print DEBUG "direct nets($if) = @n\n";
214 return @n;
215 }
216  
217 sub interface
218 {
219 my ($ifname, $ifaddr, $type, $flags, %datas) = @_;
220 if (exists $interfaces{$ifname}) {
221 die "$ifname not $type!"
222 unless $interfaces{$ifname}->{'TYPE'} eq $type;
223 } else {
224 $interfaces{$ifname} = {
225 'IP' => [],
226 'IPindex' => {},
227 'ROUTES' => [],
228 };
229 for my $k (keys %datas) {
230 $interfaces{$ifname}->{$k} = [];
231 }
232 $interfaces{$ifname}->{'TYPE'} = $type;
233 }
234 if ($consolidate{$ifaddr}) {
235 $ifaddr = $consolidate{$ifaddr};
236 return if exists ${$interfaces{$ifname}->{'IPindex'}}{$ifaddr};
237 }
238 push(@{$interfaces{$ifname}->{'IP'}}, $ifaddr);
239 for my $k (keys %datas) {
240 push(@{$interfaces{$ifname}->{$k}}, $datas{$k});
241 print DEBUG "DATA $ifname $k = $datas{$k}\n";
242 }
243 $interfaces{$ifname}->{'IPindex'}->{$ifaddr} = $data;
244 }
245  
246 sub get_interfaces
247 {
248 open(IFCONFIG, "$ifconfig -a|") or die "open $ifconfig|: $!";
249 my $interface;
250 my $flags;
251 while (<IFCONFIG>) {
252 if (/^([a-z]+\d+): flags=[\da-f]+\<([A-Z0-9,]+)\> mtu \d+/) {
253 $ifnam = $1;
254 $flags = $2;
255 $if0 = $ifnam unless defined $if0;
256 } elsif (/^\s+inet (\S+) netmask (\S+) broadcast (\S+)\s*$/) {
257 interface($ifnam, $1, 'BROADCAST', $flags, 'NETMASK', $2);
258 next;
259 } elsif (/^\s+inet (\S+) --\> (\S+)(?: netmask (\S+))?/) {
260 if ($3) {
261 interface($ifnam, $1, 'POINTTOPOINT', $flags, 'PEER', $2, NETMASK, $3);
262 } else {
263 interface($ifnam, $1, 'POINTTOPOINT', $flags, 'PEER', $2);
264 }
265 } elsif (/^\s+inet (\S+) netmask (\S+)\s*$/) {
266 interface($ifnam, $1, 'LOOPBACK', $flags);
267 } elsif (/^\s+ether\s+\S+/) {
268 # ignore
269 } elsif (/^\s+media:\s+\S+/) {
270 # ignore
271 } else {
272 warn "did not understand $ifconfig -a output: $_";
273 }
274 }
275 close(IFCONFIG);
276 }
277  
278 my %track_net;
279 my %track_interface;
280 my %route;
281  
282 sub route
283 {
284 my ($destnet, $gate, $interface) = @_;
285  
286 my ($dest, $mask) = get_netmask($destnet);
287 ($dest =~ m/^(\d+\.\d+\.\d+)\.\d+$/)
288 or die "parse route dest $dest ($destnet)";
289 my $base = $1;
290  
291 return unless (exists $track_net{$base}
292 || exists $track_interface{$interface});
293  
294 push(@{$interfaces{$interface}->{'ROUTES'}}, "$dest/$mask");
295 $route{"$dest/$mask"} = $interface;
296 print DEBUG "Added route for $dest/$mask over $interface\n";
297 }
298  
299 sub get_route
300 {
301 my ($net) = @_;
302 my ($dest, $mask) = get_netmask($net);
303 return $route{"$dest/$mask"} if exists $route{"$dest/$mask"};
304 warn "No route for $net ($dest/$mask) found";
305 }
306  
307 sub get_nets
308 {
309 my ($interface) = @_;
310  
311 my @r = @{$interfaces{$interface}->{'ROUTES'}};
312 my %r;
313 my @dn = get_direct_nets($interface);
314 @r{@r} = @r;
315 @r{@dn} = @dn;
316 return @r;
317 }
318  
319 sub get_routes
320 {
321 for my $net (@us, @symmetric, @interesting, @not_us, @from_us, @to_us) {
322 my ($base, $mask) = get_netmask($net);
323 if ($mask >= 24) {
324 $base =~ m/^(\d+\.\d+\.\d+)\.\d+$/
325 or die "parse dest $net ($base)";
326 $track_net{$1} = $net;
327 } else {
328 die if $mask < 16;
329 $base =~ /^(\d+\.\d+)\.(\d+)\.\d+$/
330 or die "parse dest $net ($base)";
331 my ($netbase, $ext) = ($1, $2);
332 my $count = 2**(24-$mask);
333 print DEBUG "Count: $count on $net ($mask)\n";
334 while ($count > 0) {
335 $track_net{"$netbase.$ext"} = $net;
336 print DEBUG "track_net{$netbase.$ext.0}\n";
337 $ext++;
338 $count--;
339 }
340 }
341 }
342 @track_interface{@leaf} = @leaf;
343  
344 open(NETSTAT, "$netstat -rn|") or die "open $netstat -rn|: $!";
345 while (<NETSTAT>) {
346 last if /^Internet/;
347 }
348 my ($dest, $gate, $flags, $refs, $use, $interface, $expire);
349 while (<NETSTAT>) {
350 last if /^$/;
351 next if /^Destination/;
352 ($dest, $gate, $flags, $refs, $use, $interface, $expire)
353 = split(' ',$_);
354 next unless $interface;
355 unless ($interface =~ /^[a-z]+\d+/) {
356 warn "Could not understat $netstat -rn: $_";
357 next;
358 }
359 next unless $gate =~ /^[\d\.]+$/;
360 route($dest, $gate, $interface);
361 }
362 close(NETSTAT);
363  
364 for my $i (keys %interfaces) {
365 my @r = @{$interfaces{$interface}->{'ROUTES'}};
366 my %r;
367 @r{@r} = @r;
368 for my $dn (get_direct_nets($i)) {
369 next if exists $r{$dn};
370 print DEBUG "Inserting $dn route over $i\n";
371 route($dn, "127.0.0.1", $i);
372 }
373 }
374 }
375  
376 #
377 # per-interface recv
378 # per-interface xmit
379 # per-protocol
380 # per-network/host
381 #
382 my %in_rules;
383 my %out_rules;
384 my %udp_from_rules;
385 my %tcp_from_rules;
386 my %udp_to_rules;
387 my %tcp_to_rules;
388 my %from_net_rules;
389 my %to_net_rules;
390 my $begun = 0;
391  
392 my @from_me_rules;
393 my @to_me_rules;
394  
395 my @not_from_me_rules;
396 my @not_to_me_rules;
397  
398 my %count_in;
399 my %count_out;
400 my %count_udp_from;
401 my %count_tcp_from;
402 my %count_udp_to;
403 my %count_tcp_to;
404 my @count;
405  
406 sub must_exist
407 {
408 my ($desc, $ar) = @_;
409  
410 my @n;
411 for my $i (@$ar) {
412 if (exists $interfaces{$i}) {
413 push(@n, $i);
414 } else {
415 warn "no $desc interface $i!";
416 }
417 }
418 @$ar = @n;
419 }
420  
421 sub begin
422 {
423 get_interfaces();
424
425 must_exist('leaf', \@leaf);
426 must_exist('outside', \@outside);
427  
428 get_routes();
429  
430 $begun = 1;
431 }
432  
433 sub count_by_interface
434 {
435 begin() unless $begun;
436 for $i (sort keys %interfaces) {
437 push(@{$count_in{$i}},
438 "count all from any to any in via $i # cbi");
439 push(@{$count_out{$i}},
440 "count all from any to any out via $i # cbi");
441 }
442 }
443  
444 sub count_by_tcp
445 {
446 my (@protos) = @_;
447 begin() unless $begun;
448 for $o (@outside) {
449 for $p (@protos) {
450 push(@{$count_tcp_from{$p}},
451 "count tcp from any $p to any in via $o # cbt",
452 "count tcp from any $p to any out via $o");
453 push(@{$count_tcp_to{$p}},
454 "count tcp from any to any $p in via $o # cbt",
455 "count tcp from any to any $p out via $o");
456 }
457 }
458 if (! @outside) {
459 for $p (@protos) {
460 push(@{$count_tcp_from{$p}},
461 "count tcp from any $p to any # cbt");
462 }
463 }
464 }
465  
466 sub count_by_udp
467 {
468 my (@protos) = @_;
469 begin() unless $begun;
470 for $p (@protos) {
471 for $o (@outside) {
472 push(@{$count_udp_from{$p}},
473 "count udp from any $p to any in via $o # cbu",
474 "count udp from any $p to any out via $o");
475 push(@{$count_udp_to{$p}},
476 "count udp from any to any $p in via $o # cbu",
477 "count udp from any to any $p out via $o");
478 }
479 if (! @outside) {
480 push(@{$count_udp_from{$p}},
481 "count udp from any $p to any # cbu");
482 }
483 }
484 }
485  
486 sub count_by_address
487 {
488 my (@addr) = @_;
489 begin() unless $begun;
490 for $a (@addr) {
491 push(@count,
492 "count all from $a to any # cba",
493 "count all from any to $a");
494 }
495 }
496  
497 sub no_looping
498 {
499 begin() unless $begun;
500 for my $o (@outside) {
501 push(@{$out_rules{$o}},
502 "=deny_nolog all from any to =US out xmit $o # nlo");
503 }
504  
505 for my $i (sort keys %interfaces) {
506 if ($interfaces{$i}->{'TYPE'} eq 'POINTTOPOINT') {
507 push(@{$out_rules{$i}},
508 "=deny_nolog tcp from any to any out recv $i xmit $i # nlnb",
509 "=deny_nolog udp from any to any out recv $i xmit $i # nlnb");
510 }
511 }
512  
513 for my $i (@leaf) {
514 next unless $interfaces{$i}->{'TYPE'} eq 'BROADCAST';
515 for my $r (get_nets{$i}) {
516 push(@{$out_rules{$i}},
517 "=deny_nolog all from $r to any out recv $i xmit $i # nlb");
518 }
519 }
520 }
521  
522 sub drop_unwanted
523 {
524 my (@unwanted) = @_;
525 begin() unless $begun;
526 for my $u (@unwanted) {
527 push(@{$from_net_rules{$u}},
528 "=deny_nolog all from $u to any # unwanted");
529 }
530 }
531  
532 sub no_spoofing_by_us
533 {
534 #
535 # We can't pretened to be others
536 #
537 begin() unless $begun;
538 for my $o (@outside) {
539 push(@{$out_rules{$o}},
540 "=skipto nso-ok all from =US to any out xmit $o # ns-o",
541 "=skipto nso-ok all from =FROMUS to any out xmit $o # ns-o",
542 "=skipto nso-ok icmp from 127.0.0.1 to any out xmit $o",
543 "=deny_log all from any to any out xmit $o",
544 "=label nso-ok");
545 }
546 }
547  
548 my %spoof_nets_done;
549  
550 sub no_spoofing_us
551 {
552 begin() unless $begun;
553  
554 # people outside can't spoof people inside
555 for my $o (@outside) {
556 push(@{$in_rules{$o}},
557 "=deny_log all from =US to any in recv $o # ns-o");
558 }
559  
560 # traffic from locally attached networks must come in via that
561 # network
562 for my $i (sort keys %interfaces) {
563 print DEBUG "making sure traffic from $i is really from $i\n";
564  
565 # traffic that claims to be from ourself will be taken
566 # care of with the from-self rules
567 for my $ip (@{$interfaces{$i}->{'IP'}}) {
568 my ($base, $bits) = get_netmask($ip);
569 next if exists $spoof_nets_done{$base};
570 mark_addresses(\%spoof_nets_done, $base, $bits, $ip, 'self') if $bits;
571 }
572
573 for my $net (get_direct_nets($i)) {
574 print DEBUG "r=$net\n";
575 my ($base, $bits) = get_netmask($net);
576 next if exists $spoof_nets_done{$base};
577 mark_addresses(\%spoof_nets_done, $base, $bits, $net, 'ns-la') if $bits;
578 push(@{$from_net_rules{$net}},
579 "=skipto okay-if-$net all from $net to any in via $i # ns-la",
580 "=skiprule all from $net to any in recv 'lo*'",
581 "=deny_log all from $net to any in",
582 "=label okay-if-$net");
583 }
584 }
585  
586 # traffic for networks that route symmetrically should always
587 # come in via the same interface
588 for my $net (@symmetric) {
589 next if exists $spoof_nets_done{$net};
590 my $i = get_route($net);
591 next unless $i;
592 my ($base, $bits) = get_netmask($net);
593 next if exists $spoof_nets_done{$base};
594 mark_addresses(\%spoof_nets_done, $base, $bits, $net, 'ns-s') if $bits;
595 push(@{$from_net_rules{$net}},
596 "=skiprule all from $net to any in via $i # ns-s",
597 "=deny_log all from $net to any in");
598 }
599 }
600  
601 sub no_leaf_spoofing
602 {
603 begin() unless $begun;
604  
605 # people who aren't in the leaf can't spoof the leaf
606 # people who are in the leaf can't pretend otherwise
607 for my $i (@leaf) {
608 for my $r (get_nets($i)) {
609 my ($base, $bits) = get_netmask($r);
610 next if exists $spoof_nets_done{$base};
611 mark_addresses(\%spoof_nets_done, $base, $bits, $r, 'ns-l') if $bits;
612 push(@{$from_net_rules{$r}},
613 "=skiprule all from $r to any in via $i # ns-l",
614 "=deny_log all from $r to any in");
615 push(@{$in_from{$i}},
616 "=skipto okay-outspoof-$i all from $r to any in via $i # ns-l");
617 }
618 push(@{$in_from{$i}},
619 "=deny_log all from any to any in via $i # ns-l",
620 "=label okay-outspoof-$i");
621 }
622 }
623  
624 sub clean_rules
625 {
626 my ($rules) = @_;
627 my @r = split("\n", $rules);
628 for my $r (@r) {
629 $r =~ s/^\s+//;
630 }
631 return (@r);
632 }
633  
634 sub in_interface_rules { my($in, $rules) = @_; push(@{$in_rules{$in}}, clean_rules($rules)); }
635 sub out_interface_rules { my($in, $rules) = @_; push(@{$out_rules{$in}}, clean_rules($rules)); }
636  
637 sub udp_from_rules { my($port, $rules) = @_; push(@{$udp_from_rules{$port}}, clean_rules($rules)); }
638 sub tcp_from_rules { my($port, $rules) = @_; push(@{$tcp_from_rules{$port}}, clean_rules($rules)); }
639 sub udp_to_rules { my($port, $rules) = @_; push(@{$udp_to_rules{$port}}, clean_rules($rules)); }
640 sub tcp_to_rules { my($port, $rules) = @_; push(@{$tcp_to_rules{$port}}, clean_rules($rules)); }
641  
642 sub from_net_rules { my($net, $rules) = @_; push(@{$from_net_rules{$net}}, clean_rules($rules)); }
643 sub to_net_rules { my($net, $rules) = @_; push(@{$to_net_rules{$net}}, clean_rules($rules)); }
644  
645 sub from_me_rules { my($rules) = @_; push(@from_me_rules, clean_rules($rules)); }
646 sub to_me_rules { my($rules) = @_; push(@to_me_rules, clean_rules($rules)); }
647 sub not_from_me_rules { my($rules) = @_; push(@not_from_me_rules, clean_rules($rules)); }
648 sub not_to_me_rules { my($rules) = @_; push(@not_to_me_rules, clean_rules($rules)); }
649  
650 #sub modload
651 # modload /lkm/ipfw_mod.o
652 # XXX
653 #
654  
655 my $genlabel = "genlabel00000";
656 my @rules;
657  
658 sub gensect
659 {
660 my ($required, $negative, $many, %set) = @_;
661 my $passlabel;
662 if ($required) {
663 push(@rules,
664 "=skiprule $required # gs-h ",
665 "=skipto $genlabel all from any to any");
666 $passlabel = $genlabel++;
667 }
668 for my $k (sort keys %set) {
669 my $control = $many;
670 $control =~ s/=KEY/$k/g;
671  
672 my @s = @{$set{$k}};
673 my $cando = 1;
674 if (@s < 4 && $control =~ /\</) {
675 my $re = $control;
676 $re =~ s/<(.*?)>/<\Q$1\E>/g;
677 $re =~ s/>.*?</.+/g;
678 $re =~ s/^.*?<//;
679 $re =~ s/>[^<]*$//;
680 print DEBUG "control($control) => '$re'\n";
681 for my $s (@s) {
682 next if $s =~ /$re/;
683 print DEBUG "NO MATCH on $s\n";
684 $cando = 0;
685 last;
686 }
687 if ($cando) {
688 push(@rules, @{$set{$k}});
689 next;
690 }
691 }
692  
693 $control =~ s/[<>]//g;
694  
695 if ($negative) {
696 push(@rules,
697 "=skipto $genlabel $control # $many",
698 @{$set{$k}},
699 "=label $genlabel");
700 } else {
701 push(@rules,
702 "=skiprule $control # $many",
703 "=skipto $genlabel all from any to any",
704 @{$set{$k}},
705 "=label $genlabel");
706 }
707 $genlabel++;
708 }
709 push(@rules, "=label $passlabel")
710 if $passlabel;
711 }
712  
713 my %options;
714  
715 sub pass1
716 {
717 push(@rules, "=countby 10", "=rulenum 1000");
718 gensect(undef, 0, "any to any <in via =KEY>", %count_in);
719 push(@rules, "=gap");
720 gensect(undef, 0, "any to any <out via =KEY>", %count_out);
721 push(@rules, "=gap");
722  
723 if (%count_udp_from || %count_udp_to) {
724 push(@rules, "=skiprule udp from any to any",
725 "=skipto not-counting-udp all from any to any");
726 push(@rules, "=gap");
727 gensect(undef, 0,
728 "<udp from> any <=KEY to> any",
729 %count_udp_from);
730 push(@rules, "=gap");
731 gensect(undef, 0,
732 "<udp from> any <to> any <=KEY>",
733 %count_udp_to);
734 push(@rules, "=gap");
735 push(@rules, "=label not-counting-udp");
736 push(@rules, "=gap");
737 }
738  
739 push(@rules, "=skiprule tcp from any to any # skipover tcp-from & to",
740 "=skipto not-counting-tcp all from any to any");
741 push(@rules, "=gap");
742 gensect(undef, 0,
743 "<tcp from> any <=KEY to> any",
744 %count_tcp_from);
745 push(@rules, "=gap");
746 gensect(undef, 0,
747 "<tcp from> any <to> any <=KEY>",
748 %count_tcp_to);
749 push(@rules, "=gap");
750 push(@rules, "=label not-counting-tcp");
751 push(@rules, "=gap");
752  
753 push(@rules, @count);
754 push(@rules, "=gap");
755  
756 push(@rules, "=rulenum 10000");
757 push(@rules, "=gap");
758  
759 # recv only happens on packets that we didn't generate
760 if (@from_us_fules || @not_from_me_rules) {
761 push(@rules,
762 "=skipto done-from-us all from any to any in recv =IN",
763 @from_me_rules,
764 "=skipto done-not-from-us all from any to any",
765 "=label done-from-us",
766 @not_from_me_rules,
767 "=label done-not-from-us");
768 push(@rules, "=gap");
769 }
770  
771 if (@to_me_rules || @not_to_me_rules) {
772 push(@rules,
773 "=skipto done-to-us all from any to =ME",
774 @not_to_me_rules,
775 "=skipto done-not-to-us all from any to any",
776 "=label done-to-us",
777 @to_me_rules,
778 "=label done-not-to-us");
779 push(@rules, "=gap");
780 }
781  
782 gensect(undef, 0, "all from any to any in <recv =KEY>", %in_rules);
783 push(@rules, "=gap");
784 gensect(undef, 0, "all from any to any out <xmit =KEY>", %out_rules);
785 push(@rules, "=gap");
786  
787 push(@rules, "pass tcp from any to any established");
788 push(@rules, "=gap");
789  
790 gensect(undef, 1, "all <from> not <=KEY to> any", %from_net_rules);
791 push(@rules, "=gap");
792 gensect(undef, 1, "all from any <to> not <=KEY>", %to_net_rules);
793 push(@rules, "=gap");
794  
795 push(@rules, "=rulenum 20000");
796  
797 if (%udp_from_rules || %udp_to_rules) {
798 push(@rules, "=skiprule udp from any to any",
799 "=skipto not-filtering-udp all from any to any")
800 if scalar(%udp_from_rules) + scalar(%udp_to_rules) > 4;
801 push(@rules, "=gap");
802 gensect(undef, 0,
803 "<udp from> any <=KEY to> any",
804 %udp_from_rules);
805 push(@rules, "=gap");
806 gensect(undef, 0,
807 "<udp from> any <to> any <=KEY>",
808 %udp_to_rules);
809 push(@rules, "=gap");
810 push(@rules, "=label not-filtering-udp");
811 }
812  
813 push(@rules, "=skiprule tcp from any to any",
814 "=skipto not-filtering-tcp all from any to any");
815 push(@rules, "=gap");
816 gensect(undef, 0,
817 "<tcp from> any <=KEY> to any",
818 %tcp_from_rules);
819 push(@rules, "=gap");
820 gensect(undef, 0,
821 "<tcp from> any <to> any <=KEY>",
822 %tcp_to_rules);
823 push(@rules, "=gap");
824 push(@rules, "=label not-filtering-tcp");
825  
826 push(@rules, "=rulenum 50000");
827  
828 if ($options{'DEFAULT-ACCEPT'}) {
829 push(@rules, "pass all from any to any");
830 } else {
831 push(@rules, "=deny all from any to any");
832 }
833  
834 push(@rules, @count);
835 }
836  
837 sub remove_action
838 {
839 my ($rule) = @_;
840 $rule =~ s/^(?:=skipto \S+|=skiprule|count|pass|deny|accpet|reject|unreach \S+|reset|divert \S+|tee \S+|skipto \S+)//
841 or die "Cannot remove action from $rule";
842 return $rule;
843 }
844  
845 sub ipdots
846 {
847 my ($addr) = @_;
848 return '' unless $addr;
849 return join('.',unpack('C4', $addr));
850 }
851  
852  
853 sub pass2
854 {
855 my @n;
856 my $waiting;
857 for my $r (@rules) {
858 my $x = $r;
859 my $l;
860 if ($x =~ s/^=skiprule/=skipto $genlabel/) {
861 $l = $genlabel++;
862 } else {
863 #$x =~ s/^=deny/=skipto deny-target/;
864 $x =~ s/^=deny_log/deny log/;
865 $x =~ s/^=deny_nolog/deny/;
866 if ($options{'LOG_DENIES'}) {
867 $x =~ s/^=deny/deny log/;
868 } else {
869 $x =~ s/^=deny/deny/;
870 }
871 }
872 while ($x =~ /=host:(\S+)/) {
873 my $hname = $1;
874 my ($net, $aliases, $addrtype, $length, $addr)
875 = gethostbyname($hname);
876 die "could not find $hname" unless $addr;
877 my $ip = ipdots($addr);
878 $x =~ s/=host:\Q$hname\E/$ip/g;
879 }
880 $x =~ s/=IF0/$if0/g;
881 if ($x =~ /=IN/) {
882 for my $i (sort keys %interfaces) {
883 my $y = $x;
884 $y =~ s/=IN/$i/g;
885 push(@n, $y);
886 }
887 } elsif ($x =~ /=FROMUS/) {
888 for my $n (@from_us) {
889 my $y = $x;
890 $y =~ s/=FROMUS/$n/g;
891 push(@n, $y);
892 }
893 } elsif ($x =~ /=TOUS/) {
894 for my $n (@to_us) {
895 my $y = $x;
896 $y =~ s/=TOUS/$n/g;
897 push(@n, $y);
898 }
899 } elsif ($x =~ /=US/) {
900 my $l2 = $genlabel++;
901 for my $n (@not_us) {
902 my $y = remove_action($x);
903 $y =~ s/=US/$n/g;
904 push(@n, "=skipto $l2 $y");
905 }
906 for my $n (@us) {
907 my $y = $x;
908 $y =~ s/=US/$n/g;
909 push(@n, $y);
910 }
911 push(@n, "=label $l2");
912 } elsif ($x =~ /=ME/) {
913 for my $i (sort keys %interfaces) {
914 for $ip (@{$interfaces{$i}->{'IP'}}) {
915 my $y = $x;
916 $y =~ s/=ME/$ip/;
917 push(@n, $y);
918 }
919 }
920 } else {
921 push(@n, $x);
922 }
923 push(@n, "=label $waiting") if $waiting;
924 $waiting = $l;
925 }
926 @rules = @n;
927 }
928  
929 my @numbers;
930 my %labels;
931 sub pass3
932 {
933 my $rulenum = 1;
934 my $inc = 1;
935 my $c = 0;
936 for my $r (@rules) {
937 if ($r =~ /^=rulenum (\d+)/) {
938 $rulenum = $1 - $inc if $1 > $rulenum;
939 $r = undef;
940 } elsif ($r =~ /^=gap\s*(\d*)/) {
941 my $gap = $1 || 1000;
942 $rulenum += $gap - $rulenum % $gap - $inc;
943 $r = undef;
944 } elsif ($r =~ /^=countby (\d+)/) {
945 $inc = $1;
946 $r = undef;
947 } elsif ($r =~ /^=label (\S+)/) {
948 die "duplicate label $1" if exists $labels{$1};
949 $labels{$1} = $c;
950 $r = undef;
951 }
952 $numbers[$c++] = $rulenum;
953 $rulenum += $inc;
954 }
955 }
956  
957 sub pass4
958 {
959 for (my $i = $#rules; $i >= 0; $i--) {
960 my $r = $rules[$i];
961 next unless $r;
962 if ($r =~ /^=skipto (\S+)/) {
963 die "no such label: $1"
964 unless exists $labels{$1};
965 my $bti = $labels{$1};
966 my $no = $numbers[$bti];
967 $r =~ s/=skipto \S+/skipto $no/;
968 my $j = $i+1;
969 $j++ while ($j <= $#rules && ! $rules[$j]);
970 if ($j >= $bti) {
971 # this rule can be skipped.
972 print DEBUG "optimizing away $numbers[$i]: $r\n";
973 $r = undef;
974 }
975 $rules[$i] = $r;
976 }
977 }
978 }
979  
980 sub pass5
981 {
982 print "# this was generated by $0\n";
983 print "$ipfw -f flush\n";
984 print "$ipfw add 1 pass all from any to any\n";
985 for (my $i = 0; $i <= $#rules; $i++) {
986 my $r = $rules[$i];
987 next unless $r;
988 my $n = $numbers[$i];
989 print "$ipfw add $n $r\n";
990 }
991 print "$ipfw delete 1\n";
992 print "$ipfw zero\n";
993 }
994  
995 sub generate
996 {
997 my (@opt) = @_;
998  
999 # don't allow others to pretend that they are us
1000 unshift(@not_from_me_rules,
1001 "=skiprule all from any to any in recv 'lo*'",
1002 "=deny_log all from =ME to any # ns-op");
1003  
1004 my @legal = qw(
1005 LOG_DENIES
1006 DEFAULT-ACCEPT
1007 INSECURE
1008 );
1009 my %legal;
1010 @legal{@legal} = @legal;
1011 for my $o (split(' ', "@opt")) {
1012 if ($legal{$o}) {
1013 $options{$o} = 1;
1014 } else {
1015 die "illegal option: $o";
1016 }
1017 }
1018  
1019 pass1();
1020 pass2();
1021 pass3();
1022 pass4();
1023 pass5();
1024 }
1025  
1026 1;
1027  
1028