code cleanup
[pve-firewall.git] / PVE / Firewall.pm
1 package PVE::Firewall;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use PVE::Tools;
7 use PVE::QemuServer;
8
9 # we need complete VM configuration of all VMs (openvz/qemu)
10 # in vmdata
11
12 sub compile {
13 my ($targetdir, $vmdata) = @_;
14
15 my $netinfo;
16
17 my $zoneinfo = {
18 fw => { type => 'firewall' },
19 };
20
21 my $register_bridge;
22
23 $register_bridge = sub {
24 my ($bridge, $vlan) = @_;
25
26 my $zone = 'z' . $bridge;
27
28 return $zone if $zoneinfo->{$zone};
29
30 $zoneinfo->{$zone} = {
31 type => 'bridge',
32 bridge => $bridge,
33 };
34
35 return &$register_bridge("${bridge}v${vlan}") if defined($vlan);
36
37 return $zone;
38 };
39
40 my $register_bridge_port = sub {
41 my ($bridge, $vlan, $vmzone, $tap) = @_;
42
43 my $bridge_zone = &$register_bridge($bridge, $vlan);
44 my $zone = $bridge_zone . $vmzone;
45
46 if (!$zoneinfo->{$zone}) {
47 $zoneinfo->{$zone} = {
48 type => 'bport',
49 bridge_zone => $bridge_zone,
50 ifaces => {},
51 };
52 }
53
54 $zoneinfo->{$zone}->{ifaces}->{$tap} = 1;
55
56 return $zone;
57 };
58
59 foreach my $vmid (keys %{$vmdata->{qemu}}) {
60 $netinfo->{$vmid} = {};
61 my $conf = $vmdata->{qemu}->{$vmid};
62 foreach my $opt (keys %$conf) {
63 next if $opt !~ m/^net(\d+)$/;
64 my $netid = $1;
65 my $net = PVE::QemuServer::parse_net($conf->{$opt});
66 next if !$net;
67 die "implement me" if !$net->{bridge};
68
69 my $vmzone = $conf->{zone} || "vm$vmid";
70 $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, "tap${vmid}i${netid}");
71 $netinfo->{$vmid}->{$netid} = $net;
72 }
73 }
74
75 #print Dumper($netinfo);
76
77 # NOTE: zone names have length limit, so we need to
78 # translate them into shorter names
79
80 my $zoneid = 0;
81 my $zonemap = { fw => 'fw' };
82
83 my $lookup_zonename = sub {
84 my ($zone) = @_;
85
86 return $zonemap->{$zone} if defined($zonemap->{$zone});
87 $zonemap->{$zone} = 'z' . $zoneid++;
88
89 return $zonemap->{$zone};
90 };
91
92 # dump zone file
93
94 my $out;
95
96 my $format = "%-15s %-10s %s\n";
97 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS');
98
99 foreach my $z (sort keys %$zoneinfo) {
100 my $zid = &$lookup_zonename($z);
101 if ($zoneinfo->{$z}->{type} eq 'firewall') {
102 $out .= sprintf($format, $zid, $zoneinfo->{$z}->{type}, '');
103 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
104 $out .= sprintf($format, &$lookup_zonename($z), 'ipv4', '');
105 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
106 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
107 my $bzid = &$lookup_zonename($bridge_zone);
108 $out .= sprintf($format, "$zid:$bzid", 'bport', '');
109 } else {
110 die "internal error";
111 }
112 }
113
114 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
115
116 PVE::Tools::file_set_contents("$targetdir/zones", $out);
117
118 # dump interfaces
119
120 $format = "%-15s %-20s %-10s %s\n";
121 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS');
122
123 foreach my $z (sort keys %$zoneinfo) {
124 my $zid = &$lookup_zonename($z);
125 if ($zoneinfo->{$z}->{type} eq 'firewall') {
126 # do nothing;
127 } elsif ($zoneinfo->{$z}->{type} eq 'bridge') {
128 my $bridge = $zoneinfo->{$z}->{bridge} || die "internal error";
129 $out .= sprintf($format, $zid, $bridge, 'detect', 'bridge');
130
131 } elsif ($zoneinfo->{$z}->{type} eq 'bport') {
132 my $ifaces = $zoneinfo->{$z}->{ifaces};
133 foreach my $iface (sort keys %$ifaces) {
134 my $bridge_zone = $zoneinfo->{$z}->{bridge_zone} || die "internal error";
135 my $bridge = $zoneinfo->{$bridge_zone}->{bridge} || die "internal error";
136 my $iftxt = "$bridge:$iface";
137 $out .= sprintf($format, $zid, $iftxt, '', '');
138 }
139 } else {
140 die "internal error";
141 }
142 }
143
144 $out .= sprintf("#LAST LINE - ADD YOUR ENTRIES ABOVE THIS ONE - DO NOT REMOVE\n");
145
146 PVE::Tools::file_set_contents("$targetdir/interfaces", $out);
147
148 # dump policy
149
150 $format = "%-15s %-15s %-15s %s\n";
151 $out = sprintf($format, '#SOURCE', 'DEST', 'POLICY', 'LOG');
152 $out .= sprintf($format, 'all', 'all', 'REJECT', 'info');
153
154 PVE::Tools::file_set_contents("$targetdir/policy", $out);
155
156
157 }
158
159
160 sub activate {
161
162 }
163
164
165 1;