Skip to content

Commit adbd20c

Browse files
committed
bug 8655: add security group for direct tagged network
1 parent 0c8b86f commit adbd20c

24 files changed

Lines changed: 237 additions & 132 deletions

‎agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,12 +1401,21 @@ protected Answer execute(ModifyStoragePoolCommand cmd) {
14011401
}
14021402

14031403
private Answer execute(SecurityIngressRulesCmd cmd) {
1404+
String vif = null;
1405+
try {
1406+
Connect conn = LibvirtConnection.getConnection();
1407+
List<InterfaceDef> nics = getInterfaces(conn, cmd.getVmName());
1408+
vif = nics.get(0).getBrName();
1409+
} catch (LibvirtException e) {
1410+
1411+
}
1412+
14041413
boolean result = add_network_rules(cmd.getVmName(),
14051414
Long.toString(cmd.getVmId()),
14061415
cmd.getGuestIp(),cmd.getSignature(),
14071416
Long.toString(cmd.getSeqNum()),
14081417
cmd.getGuestMac(),
1409-
cmd.stringifyRules());
1418+
cmd.stringifyRules(), vif);
14101419

14111420
if (!result) {
14121421
s_logger.warn("Failed to program network rules for vm " + cmd.getVmName());
@@ -2046,23 +2055,7 @@ protected synchronized StartAnswer execute(StartCommand cmd) {
20462055

20472056
if (vmSpec.getType() != VirtualMachine.Type.User) {
20482057
default_network_rules_for_systemvm(vmName);
2049-
} else {
2050-
NicTO[] nics = vmSpec.getNics();
2051-
List<InterfaceDef> vifs = getInterfaces(conn, vmName);
2052-
StringBuilder rules = new StringBuilder();
2053-
2054-
for (NicTO nic : nics) {
2055-
if (nic.getIsolationUri() != null ) {
2056-
if (nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString()) ||
2057-
nic.getType() == TrafficType.Public) {
2058-
rules.append(nic.getIp() + "," + nic.getMac() + "," + vifs.get(nic.getDeviceId()).getDevName() + ";") ;
2059-
}
2060-
}
2061-
}
2062-
2063-
if (rules.toString() != null)
2064-
default_network_rules(vmName, vmSpec.getId(), rules.toString());
2065-
}
2058+
}
20662059

20672060
// Attach each data volume to the VM, if there is a deferred attached disk
20682061
for (DiskDef disk : vm.getDevices().getDisks()) {
@@ -3179,7 +3172,7 @@ private boolean default_network_rules_for_systemvm(String vmName) {
31793172
return true;
31803173
}
31813174

3182-
private boolean add_network_rules(String vmName, String vmId, String guestIP, String sig, String seq, String mac, String rules) {
3175+
private boolean add_network_rules(String vmName, String vmId, String guestIP, String sig, String seq, String mac, String rules, String vif) {
31833176
if (!_can_bridge_firewall) {
31843177
return false;
31853178
}
@@ -3192,6 +3185,7 @@ private boolean add_network_rules(String vmName, String vmId, String guestIP, St
31923185
cmd.add("--sig", sig);
31933186
cmd.add("--seq", seq);
31943187
cmd.add("--vmmac", mac);
3188+
cmd.add("--vif", vif);
31953189
if (rules != null)
31963190
cmd.add("--rules", newRules);
31973191
String result = cmd.execute();

‎api/src/com/cloud/api/ApiConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public class ApiConstants {
138138
public static final String SECURITY_GROUP_IDS = "securitygroupids";
139139
public static final String SECURITY_GROUP_NAME = "securitygroupname";
140140
public static final String SECURITY_GROUP_ID = "securitygroupid";
141+
public static final String SECURITY_GROUP_EANBLED = "securitygroupEnabled";
141142
public static final String SENT = "sent";
142143
public static final String SENT_BYTES = "sentbytes";
143144
public static final String SERVICE_OFFERING_ID = "serviceofferingid";

‎api/src/com/cloud/api/commands/CreateNetworkCmd.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.cloud.api.Implementation;
2626
import com.cloud.api.Parameter;
2727
import com.cloud.api.ServerApiException;
28+
import com.cloud.api.BaseCmd.CommandType;
2829
import com.cloud.api.response.NetworkResponse;
2930
import com.cloud.exception.ConcurrentOperationException;
3031
import com.cloud.exception.InsufficientCapacityException;
@@ -81,6 +82,9 @@ public class CreateNetworkCmd extends BaseCmd {
8182

8283
@Parameter(name=ApiConstants.NETWORK_DOMAIN, type=CommandType.STRING, description="network domain")
8384
private String networkDomain;
85+
86+
@Parameter(name=ApiConstants.SECURITY_GROUP_EANBLED, type=CommandType.BOOLEAN, description="true if network is security group enabled, false otherwise")
87+
private Boolean is_security_group_enabled;
8488

8589
/////////////////////////////////////////////////////
8690
/////////////////// Accessors ///////////////////////
@@ -140,6 +144,10 @@ public Boolean isDefault() {
140144
public String getNetworkDomain() {
141145
return networkDomain;
142146
}
147+
148+
public boolean isSecurityGroupEnabled() {
149+
return is_security_group_enabled == null ? false : true;
150+
}
143151
/////////////////////////////////////////////////////
144152
/////////////// API Implementation///////////////////
145153
/////////////////////////////////////////////////////

‎api/src/com/cloud/api/commands/CreateVlanIpRangeCmd.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public String getEndIp() {
9191
}
9292

9393
public Boolean isForVirtualNetwork() {
94-
return forVirtualNetwork == null ? true : forVirtualNetwork;
94+
return false;
95+
//return forVirtualNetwork == null ? true : forVirtualNetwork;
9596
}
9697

9798
public String getGateway() {

‎api/src/com/cloud/api/commands/CreateZoneCmd.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public class CreateZoneCmd extends BaseCmd {
6767

6868
@Parameter(name=ApiConstants.NETWORK_TYPE, type=CommandType.STRING, required=true, description="network type of the zone, can be Basic or Advanced")
6969
private String networkType;
70+
71+
@Parameter(name=ApiConstants.SECURITY_GROUP_EANBLED, type=CommandType.BOOLEAN, description="true if network is security group enabled, false otherwise")
72+
private Boolean is_security_group_enabled;
7073

7174
/////////////////////////////////////////////////////
7275
/////////////////// Accessors ///////////////////////
@@ -112,6 +115,11 @@ public String getNetworkType(){
112115
return networkType;
113116
}
114117

118+
public boolean isSecurityGroupEnabled() {
119+
return true;
120+
//return is_security_group_enabled == null ? false : true;
121+
}
122+
115123
/////////////////////////////////////////////////////
116124
/////////////// API Implementation///////////////////
117125

‎api/src/com/cloud/api/response/NetworkResponse.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public class NetworkResponse extends BaseResponse{
9595
@SerializedName("networkdomain") @Param(description="the network domain")
9696
private String networkDomain;
9797

98+
@SerializedName(ApiConstants.SECURITY_GROUP_EANBLED) @Param(description="true if security group is enabled, false otherwise")
99+
private Boolean isSecurityGroupEnabled;
100+
98101
public Long getId() {
99102
return id;
100103
}
@@ -327,4 +330,12 @@ public void setNetworkDomain(String networkDomain) {
327330
this.networkDomain = networkDomain;
328331
}
329332

333+
public Boolean getIsSecurityGroupEnabled() {
334+
return this.isSecurityGroupEnabled;
335+
}
336+
337+
public void setIsSecurityGroupEnabled(Boolean sgEnabled) {
338+
this.isSecurityGroupEnabled = sgEnabled;
339+
}
340+
330341
}

‎api/src/com/cloud/network/Network.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,6 @@ private State(String description) {
205205

206206
String getNetworkDomain();
207207

208+
boolean isSecurityGroupEnabled();
209+
208210
}

‎scripts/vm/network/security_group.py‎

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def can_bridge_firewall(privnic):
5454
print "failed to turn on bridge netfilter"
5555
exit(3)
5656

57+
'''
5758
try:
5859
execute("iptables -N BRIDGE-FIREWALL")
5960
execute("iptables -N BRIDGE-FIREWALL")
@@ -72,10 +73,8 @@ def can_bridge_firewall(privnic):
7273
execute("iptables -A FORWARD -j DROP")
7374
except:
7475
result = 1
76+
'''
7577

76-
77-
if result == 1:
78-
print "br firewall is not supported"
7978
if not os.path.exists('/var/run/cloud'):
8079
os.makedirs('/var/run/cloud')
8180

@@ -155,10 +154,9 @@ def destroy_ebtables_rules(vm_name):
155154
except:
156155
logging.debug("Ignoring failure to delete ebtables chain for vm " + vm_name)
157156

158-
def default_ebtables_rules(vm_name, rules):
157+
def default_ebtables_rules(vm_name, vm_ip, vm_mac, vif):
159158
vmchain_in = vm_name + "-in"
160159
vmchain_out = vm_name + "-out"
161-
rule = rules.split(";")[:-1]
162160

163161
for chain in [vmchain_in, vmchain_out]:
164162
try:
@@ -168,40 +166,30 @@ def default_ebtables_rules(vm_name, rules):
168166

169167
try:
170168
# -s ! 52:54:0:56:44:32 -j DROP
171-
for r in rule:
172-
vif = r.split(",")[2]
173-
execute("ebtables -t nat -A PREROUTING -i " + vif + " -j " + vmchain_in)
174-
execute("ebtables -t nat -A POSTROUTING -o " + vif + " -j " + vmchain_out)
169+
execute("ebtables -t nat -A PREROUTING -i " + vif + " -j " + vmchain_in)
170+
execute("ebtables -t nat -A POSTROUTING -o " + vif + " -j " + vmchain_out)
175171
except:
176172
logging.debug("Failed to program default rules")
177173
return 'false'
178174

179175
try:
180-
for r in rule:
181-
vm_ip = r.split(",")[0]
182-
vm_mac = r.split(",")[1]
183-
vif = r.split(",")[2]
184-
execute("ebtables -t nat -A " + vmchain_in + " -i " + vif + " -s ! " + vm_mac + " -j DROP")
185-
execute("ebtables -t nat -A " + vmchain_in + " -i " + vif + " -p ARP -s ! " + vm_mac + " -j DROP")
186-
execute("ebtables -t nat -A " + vmchain_in + " -i " + vif + " -p ARP --arp-mac-src ! " + vm_mac + " -j DROP")
187-
execute("ebtables -t nat -A " + vmchain_in + " -i " + vif + " -p ARP --arp-ip-src ! " + vm_ip + " -j DROP")
188-
execute("ebtables -t nat -A " + vmchain_in + " -i " + vif + " -p ARP --arp-op Request -j ACCEPT")
189-
execute("ebtables -t nat -A " + vmchain_in + " -i " + vif + " -p ARP --arp-op Reply -j ACCEPT")
190-
execute("ebtables -t nat -A " + vmchain_in + " -i " + vif + " -p ARP -j DROP")
176+
execute("ebtables -t nat -A " + vmchain_in + " -s ! " + vm_mac + " -j DROP")
177+
execute("ebtables -t nat -A " + vmchain_in + " -p ARP -s ! " + vm_mac + " -j DROP")
178+
execute("ebtables -t nat -A " + vmchain_in + " -p ARP --arp-mac-src ! " + vm_mac + " -j DROP")
179+
execute("ebtables -t nat -A " + vmchain_in + " -p ARP --arp-ip-src ! " + vm_ip + " -j DROP")
180+
execute("ebtables -t nat -A " + vmchain_in + " -p ARP --arp-op Request -j ACCEPT")
181+
execute("ebtables -t nat -A " + vmchain_in + " -p ARP --arp-op Reply -j ACCEPT")
182+
execute("ebtables -t nat -A " + vmchain_in + " -p ARP -j DROP")
191183
except:
192184
logging.exception("Failed to program default ebtables IN rules")
193185
return 'false'
194186

195187
try:
196-
for r in rule:
197-
vm_ip = r.split(",")[0]
198-
vm_mac = r.split(",")[1]
199-
vif = r.split(",")[2]
200-
execute("ebtables -t nat -A " + vmchain_out + " -i " + vif + " -p ARP --arp-op Reply --arp-mac-dst ! " + vm_mac + " -j DROP")
201-
execute("ebtables -t nat -A " + vmchain_out + " -i " + vif + " -p ARP --arp-ip-dst ! " + vm_ip + " -j DROP")
202-
execute("ebtables -t nat -A " + vmchain_out + " -i " + vif + " -p ARP --arp-op Request -j ACCEPT")
203-
execute("ebtables -t nat -A " + vmchain_out + " -i " + vif + " -p ARP --arp-op Reply -j ACCEPT")
204-
execute("ebtables -t nat -A " + vmchain_out + " -i " + vif + " -p ARP -j DROP")
188+
execute("ebtables -t nat -A " + vmchain_out + " -p ARP --arp-op Reply --arp-mac-dst ! " + vm_mac + " -j DROP")
189+
execute("ebtables -t nat -A " + vmchain_out + " -p ARP --arp-ip-dst ! " + vm_ip + " -j DROP")
190+
execute("ebtables -t nat -A " + vmchain_out + " -p ARP --arp-op Request -j ACCEPT")
191+
execute("ebtables -t nat -A " + vmchain_out + " -p ARP --arp-op Reply -j ACCEPT")
192+
execute("ebtables -t nat -A " + vmchain_out + " -p ARP -j DROP")
205193
except:
206194
logging.debug("Failed to program default ebtables OUT rules")
207195
return 'false'
@@ -235,7 +223,7 @@ def default_network_rules_systemvm(vm_name):
235223
return 'true'
236224

237225

238-
def default_network_rules(vm_name, vm_id, rules):
226+
def default_network_rules(vm_name, vm_id, vm_ip, vm_mac, vif):
239227
vmName = vm_name
240228
domID = getvmId(vm_name)
241229
delete_rules_for_vm_in_bridge_firewall_chain(vmName)
@@ -254,35 +242,25 @@ def default_network_rules(vm_name, vm_id, rules):
254242
except:
255243
execute("iptables -F " + vmchain_default)
256244

257-
rule = rules.split(";")[:-1]
258245
try:
259-
for r in rule:
260-
vif = r.split(",")[2]
261-
execute("iptables -A BRIDGE-FIREWALL -m physdev --physdev-is-bridged --physdev-out " + vif + " -j " + vmchain_default)
262-
execute("iptables -A BRIDGE-FIREWALL -m physdev --physdev-is-bridged --physdev-in " + vif + " -j " + vmchain_default)
246+
execute("iptables -A BRIDGE-FIREWALL -m physdev --physdev-is-bridged --physdev-out " + vif + " -j " + vmchain_default)
247+
execute("iptables -A BRIDGE-FIREWALL -m physdev --physdev-is-bridged --physdev-in " + vif + " -j " + vmchain_default)
263248
execute("iptables -A " + vmchain_default + " -m state --state RELATED,ESTABLISHED -j ACCEPT")
264249
#allow dhcp
265-
for r in rule:
266-
vif = r.split(",")[2]
267-
execute("iptables -A " + vmchain_default + " -m physdev --physdev-is-bridged --physdev-in " + vif + " -p udp --dport 67 --sport 68 -j ACCEPT")
268-
execute("iptables -A " + vmchain_default + " -m physdev --physdev-is-bridged --physdev-out " + vif + " -p udp --dport 68 --sport 67 -j ACCEPT")
250+
execute("iptables -A " + vmchain_default + " -m physdev --physdev-is-bridged --physdev-in " + vif + " -p udp --dport 67 --sport 68 -j ACCEPT")
251+
execute("iptables -A " + vmchain_default + " -m physdev --physdev-is-bridged --physdev-out " + vif + " -p udp --dport 68 --sport 67 -j ACCEPT")
269252

270253
#don't let vm spoof its ip address
271-
for r in rule:
272-
vmip = r.split(",")[0]
273-
vif = r.split(",")[2]
274-
execute("iptables -A " + vmchain_default + " -m physdev --physdev-is-bridged --physdev-in " + vif + " --source " + vmip + " -j ACCEPT")
254+
execute("iptables -A " + vmchain_default + " -m physdev --physdev-is-bridged --physdev-in " + vif + " --source " + vm_ip + " -j ACCEPT")
275255
execute("iptables -A " + vmchain_default + " -j " + vmchain)
276256
except:
277257
logging.debug("Failed to program default rules for vm " + vm_name)
278258
return 'false'
279259

280-
default_ebtables_rules(vmchain, rules)
260+
default_ebtables_rules(vmchain, vm_ip, vm_mac, vif)
281261

282-
for r in rule:
283-
vm_ip = r.split(",")[0]
284-
if write_rule_log_for_vm(vmName, vm_id, vm_ip, domID, '_initial_', '-1') == False:
285-
logging.debug("Failed to log default network rules, ignoring")
262+
if write_rule_log_for_vm(vmName, vm_id, vm_ip, domID, '_initial_', '-1') == False:
263+
logging.debug("Failed to log default network rules, ignoring")
286264

287265
logging.debug("Programmed default rules for vm " + vm_name)
288266
return 'true'
@@ -441,7 +419,7 @@ def remove_rule_log_for_vm(vmName):
441419

442420
return result
443421

444-
def add_network_rules(vm_name, vm_id, vm_ip, signature, seqno, vmMac, rules):
422+
def add_network_rules(vm_name, vm_id, vm_ip, signature, seqno, vmMac, rules, vif):
445423
try:
446424
vmName = vm_name
447425
domId = getvmId(vmName)
@@ -458,6 +436,9 @@ def add_network_rules(vm_name, vm_id, vm_ip, signature, seqno, vmMac, rules):
458436
write_rule_log_for_vm(vmName, vm_id, vm_ip, domId, signature, seqno)
459437
return 'true'
460438

439+
if changes[0] or changes[2]:
440+
default_network_rules(vmName, vm_id, vm_ip, vmMac, vif)
441+
461442
lines = rules.split(';')[:-1]
462443

463444
logging.debug(" programming network rules for IP: " + vm_ip + " vmname=" + vm_name)
@@ -549,14 +530,14 @@ def getvmId(vmName):
549530
if cmd == "can_bridge_firewall":
550531
can_bridge_firewall(args[1])
551532
elif cmd == "default_network_rules":
552-
default_network_rules(option.vmName, option.vmID, option.rules)
533+
default_network_rules(option.vmName, option.vmID, option.vmIP, option.vmMAC, option.vif)
553534
elif cmd == "destroy_network_rules_for_vm":
554535
destroy_network_rules_for_vm(option.vmName)
555536
elif cmd == "default_network_rules_systemvm":
556537
default_network_rules_systemvm(option.vmName)
557538
elif cmd == "get_rule_logs_for_vms":
558539
get_rule_logs_for_vms()
559540
elif cmd == "add_network_rules":
560-
add_network_rules(option.vmName, option.vmID, option.vmIP, option.sig, option.seq, option.vmMAC, option.rules)
541+
add_network_rules(option.vmName, option.vmID, option.vmIP, option.sig, option.seq, option.vmMAC, option.rules, option.vif)
561542
elif cmd == "cleanup_rules":
562543
cleanup_rules()

‎server/src/com/cloud/api/ApiResponseHelper.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,8 @@ public NetworkResponse createNetworkResponse(Network network) {
23012301
response.setDns1(profile.getDns1());
23022302
response.setDns2(profile.getDns2());
23032303

2304+
response.setIsSecurityGroupEnabled(network.isSecurityGroupEnabled());
2305+
23042306
//populate capability
23052307
Map<Service, Map<Capability, String>> serviceCapabilitiesMap = ApiDBUtils.getNetworkCapabilities(network.getId());
23062308
List<ServiceResponse> serviceResponses = new ArrayList<ServiceResponse>();

‎server/src/com/cloud/configuration/ConfigurationManager.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public interface ConfigurationManager extends ConfigurationService, Manager {
112112
* @throws
113113
* @throws
114114
*/
115-
DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String internalDns1, String internalDns2, String vnetRange, String guestCidr, String domain, Long domainId, NetworkType zoneType);
115+
DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String internalDns1, String internalDns2, String vnetRange, String guestCidr, String domain, Long domainId, NetworkType zoneType, boolean isSecurityGroupEnabled);
116116

117117
/**
118118
* Deletes a VLAN from the database, along with all of its IP addresses. Will not delete VLANs that have allocated IP addresses.
@@ -173,7 +173,7 @@ void checkDiskOfferingAccess(Account caller, DiskOffering dof)
173173

174174
Vlan createVlanAndPublicIpRange(Long userId, Long zoneId, Long podId, String startIP, String endIP, String vlanGateway, String vlanNetmask, boolean forVirtualNetwork, String vlanId, Account account, Long networkId) throws InsufficientCapacityException, ConcurrentOperationException, InvalidParameterValueException;
175175

176-
void createDefaultNetworks(long zoneId) throws ConcurrentOperationException;
176+
void createDefaultNetworks(long zoneId, boolean isSecurityGroupEnabled) throws ConcurrentOperationException;
177177

178178
DataCenterVO getZone(long id);
179179

0 commit comments

Comments
 (0)