code cleanup
[pve-firewall.git] / PVE / Firewall.pm
CommitLineData
b6360c3f
DM
1package PVE::Firewall;
2
3use warnings;
4use strict;
5use Data::Dumper;
f789653a 6use PVE::Tools;
b6360c3f
DM
7use PVE::QemuServer;
8
9# we need complete VM configuration of all VMs (openvz/qemu)
10# in vmdata
11
12sub compile {
f789653a 13 my ($targetdir, $vmdata) = @_;
b6360c3f
DM
14
15 my $netinfo;
16
886aba9c
DM
17 my $zoneinfo = {
18 fw => { type => 'firewall' },
19 };
20
35081236
DM
21 my $register_bridge;
22
23 $register_bridge = sub {
24 my ($bridge, $vlan) = @_;
f789653a
DM
25
26 my $zone = 'z' . $bridge;
27
28 return $zone if $zoneinfo->{$zone};
29
30 $zoneinfo->{$zone} = {
31 type => 'bridge',
32 bridge => $bridge,
33 };
35081236
DM
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;
f789653a
DM
55
56 return $zone;
57 };
58
b6360c3f
DM
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+)$/;
886aba9c 64 my $netid = $1;
b6360c3f
DM
65 my $net = PVE::QemuServer::parse_net($conf->{$opt});
66 next if !$net;
886aba9c 67 die "implement me" if !$net->{bridge};
886aba9c 68
f789653a 69 my $vmzone = $conf->{zone} || "vm$vmid";
35081236 70 $net->{zone} = &$register_bridge_port($net->{bridge}, $net->{tag}, $vmzone, "tap${vmid}i${netid}");
886aba9c
DM
71 $netinfo->{$vmid}->{$netid} = $net;
72 }
73 }
74
75 #print Dumper($netinfo);
76
f789653a 77 # NOTE: zone names have length limit, so we need to
886aba9c
DM
78 # translate them into shorter names
79
f789653a
DM
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
886aba9c
DM
92 # dump zone file
93
f789653a 94 my $out;
886aba9c 95
f789653a
DM
96 my $format = "%-15s %-10s %s\n";
97 $out = sprintf($format, '#ZONE', 'TYPE', 'OPTIONS');
98
886aba9c 99 foreach my $z (sort keys %$zoneinfo) {
f789653a
DM
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 }
886aba9c
DM
112 }
113
f789653a
DM
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);
886aba9c 117
f789653a 118 # dump interfaces
886aba9c
DM
119
120 $format = "%-15s %-20s %-10s %s\n";
f789653a
DM
121 $out = sprintf($format, '#ZONE', 'INTERFACE', 'BROADCAST', 'OPTIONS');
122
886aba9c 123 foreach my $z (sort keys %$zoneinfo) {
f789653a
DM
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";
b6360c3f
DM
141 }
142 }
143
f789653a
DM
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');
886aba9c 153
f789653a 154 PVE::Tools::file_set_contents("$targetdir/policy", $out);
886aba9c 155
b6360c3f
DM
156
157}
158
886aba9c 159
b6360c3f
DM
160sub activate {
161
162}
163
164
1651;