Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Ray Milkey | 8526700 | 2016-11-16 11:06:35 -0800 | [diff] [blame] | 15 | * |
| 16 | * Originally created by Pengfei Lu, Network and Cloud Computing Laboratory, Dalian University of Technology, China |
| 17 | * Advisers: Keqiu Li, Heng Qi and Haisheng Yu |
| 18 | * This work is supported by the State Key Program of National Natural Science of China(Grant No. 61432002) |
| 19 | * and Prospective Research Project on Future Networks in Jiangsu Future Networks Innovation Institute. |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 20 | */ |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 21 | package org.onosproject.acl.impl; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 22 | |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ethernet; |
| 24 | import org.onlab.packet.IPv4; |
| 25 | import org.onlab.packet.Ip4Address; |
| 26 | import org.onlab.packet.Ip4Prefix; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.TpPort; |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 29 | import org.onosproject.acl.AclRule; |
| 30 | import org.onosproject.acl.AclService; |
| 31 | import org.onosproject.acl.AclStore; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Component; |
| 34 | import org.apache.felix.scr.annotations.Deactivate; |
| 35 | import org.apache.felix.scr.annotations.Reference; |
| 36 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 37 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 38 | import org.onosproject.acl.RuleId; |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 39 | import org.onosproject.core.ApplicationId; |
| 40 | import org.onosproject.core.CoreService; |
| 41 | import org.onosproject.core.IdGenerator; |
| 42 | import org.onosproject.mastership.MastershipService; |
| 43 | import org.onosproject.net.DeviceId; |
| 44 | import org.onosproject.net.Host; |
| 45 | import org.onosproject.net.MastershipRole; |
| 46 | import org.onosproject.net.PortNumber; |
| 47 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 48 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 49 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 50 | import org.onosproject.net.flow.FlowEntry; |
| 51 | import org.onosproject.net.flow.FlowRule; |
| 52 | import org.onosproject.net.flow.FlowRuleService; |
| 53 | import org.onosproject.net.flow.TrafficSelector; |
| 54 | import org.onosproject.net.flow.TrafficTreatment; |
| 55 | import org.onosproject.net.flow.instructions.Instructions; |
| 56 | import org.onosproject.net.host.HostEvent; |
| 57 | import org.onosproject.net.host.HostListener; |
| 58 | import org.onosproject.net.host.HostService; |
| 59 | import org.slf4j.Logger; |
| 60 | |
| 61 | import java.util.HashSet; |
| 62 | import java.util.List; |
| 63 | import java.util.Set; |
| 64 | |
| 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
| 67 | /** |
| 68 | * Implementation of the ACL service. |
| 69 | */ |
| 70 | @Component(immediate = true) |
| 71 | @Service |
| 72 | public class AclManager implements AclService { |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected CoreService coreService; |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected FlowRuleService flowRuleService; |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected HostService hostService; |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected MastershipService mastershipService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected AclStore aclStore; |
| 85 | |
| 86 | private final Logger log = getLogger(getClass()); |
| 87 | private ApplicationId appId; |
| 88 | private final HostListener hostListener = new InternalHostListener(); |
| 89 | private IdGenerator idGenerator; |
| 90 | |
| 91 | /** |
| 92 | * Checks if the given IP address is in the given CIDR address. |
| 93 | */ |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 94 | private boolean checkIpInCidr(Ip4Address ip, Ip4Prefix cidr) { |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 95 | int offset = 32 - cidr.prefixLength(); |
| 96 | int cidrPrefix = cidr.address().toInt(); |
| 97 | int ipIntValue = ip.toInt(); |
| 98 | cidrPrefix = cidrPrefix >> offset; |
| 99 | ipIntValue = ipIntValue >> offset; |
| 100 | cidrPrefix = cidrPrefix << offset; |
| 101 | ipIntValue = ipIntValue << offset; |
| 102 | |
| 103 | return (cidrPrefix == ipIntValue); |
| 104 | } |
| 105 | |
| 106 | private class InternalHostListener implements HostListener { |
| 107 | |
| 108 | /** |
| 109 | * Generate new ACL flow rules for new host following the given ACL rule. |
| 110 | */ |
| 111 | private void processHostAddedEvent(HostEvent event, AclRule rule) { |
| 112 | DeviceId deviceId = event.subject().location().deviceId(); |
| 113 | for (IpAddress address : event.subject().ipAddresses()) { |
| 114 | if ((rule.srcIp() != null) ? |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 115 | (checkIpInCidr(address.getIp4Address(), rule.srcIp())) : |
| 116 | (checkIpInCidr(address.getIp4Address(), rule.dstIp()))) { |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 117 | if (!aclStore.checkIfRuleWorksInDevice(rule.id(), deviceId)) { |
| 118 | List<RuleId> allowingRuleList = aclStore |
| 119 | .getAllowingRuleByDenyingRule(rule.id()); |
| 120 | if (allowingRuleList != null) { |
| 121 | for (RuleId allowingRuleId : allowingRuleList) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 122 | generateAclFlow(aclStore.getAclRule(allowingRuleId), deviceId); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 123 | } |
| 124 | } |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 125 | generateAclFlow(rule, deviceId); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void event(HostEvent event) { |
| 133 | // if a new host appears and an existing rule denies |
| 134 | // its traffic, a new ACL flow rule is generated. |
| 135 | if (event.type() == HostEvent.Type.HOST_ADDED) { |
| 136 | DeviceId deviceId = event.subject().location().deviceId(); |
| 137 | if (mastershipService.getLocalRole(deviceId) == MastershipRole.MASTER) { |
| 138 | for (AclRule rule : aclStore.getAclRules()) { |
| 139 | if (rule.action() != AclRule.Action.ALLOW) { |
| 140 | processHostAddedEvent(event, rule); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | @Activate |
| 149 | public void activate() { |
Thomas Vachuska | 1b1537c | 2015-12-17 09:06:52 -0800 | [diff] [blame] | 150 | appId = coreService.registerApplication("org.onosproject.acl"); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 151 | hostService.addListener(hostListener); |
| 152 | idGenerator = coreService.getIdGenerator("acl-ids"); |
| 153 | AclRule.bindIdGenerator(idGenerator); |
| 154 | log.info("Started"); |
| 155 | } |
| 156 | |
| 157 | @Deactivate |
| 158 | public void deactivate() { |
| 159 | hostService.removeListener(hostListener); |
sangyun-han | fd286c9 | 2017-01-26 16:24:44 +0900 | [diff] [blame] | 160 | this.clearAcl(); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 161 | log.info("Stopped"); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public List<AclRule> getAclRules() { |
| 166 | return aclStore.getAclRules(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Checks if the new ACL rule matches an existing rule. |
| 171 | * If existing allowing rules matches the new denying rule, store the mappings. |
Thomas Vachuska | 9bb3235 | 2015-09-25 11:31:22 -0700 | [diff] [blame] | 172 | * |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 173 | * @return true if the new ACL rule matches an existing rule, false otherwise |
| 174 | */ |
| 175 | private boolean matchCheck(AclRule newRule) { |
| 176 | for (AclRule existingRule : aclStore.getAclRules()) { |
| 177 | if (newRule.checkMatch(existingRule)) { |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | if (existingRule.action() == AclRule.Action.ALLOW |
| 182 | && newRule.action() == AclRule.Action.DENY) { |
| 183 | if (existingRule.checkMatch(newRule)) { |
| 184 | aclStore.addDenyToAllowMapping(newRule.id(), existingRule.id()); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public boolean addAclRule(AclRule rule) { |
| 193 | if (matchCheck(rule)) { |
| 194 | return false; |
| 195 | } |
| 196 | aclStore.addAclRule(rule); |
| 197 | log.info("ACL rule(id:{}) is added.", rule.id()); |
| 198 | if (rule.action() != AclRule.Action.ALLOW) { |
| 199 | enforceRuleAdding(rule); |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Gets a set containing all devices connecting with the hosts |
| 206 | * whose IP address is in the given CIDR IP address. |
| 207 | */ |
| 208 | private Set<DeviceId> getDeviceIdSet(Ip4Prefix cidrAddr) { |
| 209 | Set<DeviceId> deviceIdSet = new HashSet<>(); |
| 210 | final Iterable<Host> hosts = hostService.getHosts(); |
| 211 | |
| 212 | if (cidrAddr.prefixLength() != 32) { |
| 213 | for (Host h : hosts) { |
| 214 | for (IpAddress a : h.ipAddresses()) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 215 | if (checkIpInCidr(a.getIp4Address(), cidrAddr)) { |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 216 | deviceIdSet.add(h.location().deviceId()); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } else { |
| 221 | for (Host h : hosts) { |
| 222 | for (IpAddress a : h.ipAddresses()) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 223 | if (checkIpInCidr(a.getIp4Address(), cidrAddr)) { |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 224 | deviceIdSet.add(h.location().deviceId()); |
| 225 | return deviceIdSet; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | return deviceIdSet; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Enforces denying ACL rule by ACL flow rules. |
| 235 | */ |
| 236 | private void enforceRuleAdding(AclRule rule) { |
| 237 | Set<DeviceId> dpidSet; |
| 238 | if (rule.srcIp() != null) { |
| 239 | dpidSet = getDeviceIdSet(rule.srcIp()); |
| 240 | } else { |
| 241 | dpidSet = getDeviceIdSet(rule.dstIp()); |
| 242 | } |
| 243 | |
| 244 | for (DeviceId deviceId : dpidSet) { |
| 245 | List<RuleId> allowingRuleList = aclStore.getAllowingRuleByDenyingRule(rule.id()); |
| 246 | if (allowingRuleList != null) { |
| 247 | for (RuleId allowingRuleId : allowingRuleList) { |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 248 | generateAclFlow(aclStore.getAclRule(allowingRuleId), deviceId); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 249 | } |
| 250 | } |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 251 | generateAclFlow(rule, deviceId); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Generates ACL flow rule according to ACL rule |
| 257 | * and install it into related device. |
| 258 | */ |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 259 | private void generateAclFlow(AclRule rule, DeviceId deviceId) { |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 260 | if (rule == null || aclStore.checkIfRuleWorksInDevice(rule.id(), deviceId)) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 265 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 266 | FlowEntry.Builder flowEntry = DefaultFlowEntry.builder(); |
| 267 | |
| 268 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 269 | if (rule.srcIp() != null) { |
| 270 | selectorBuilder.matchIPSrc(rule.srcIp()); |
| 271 | if (rule.dstIp() != null) { |
| 272 | selectorBuilder.matchIPDst(rule.dstIp()); |
| 273 | } |
| 274 | } else { |
| 275 | selectorBuilder.matchIPDst(rule.dstIp()); |
| 276 | } |
| 277 | if (rule.ipProto() != 0) { |
| 278 | selectorBuilder.matchIPProtocol(Integer.valueOf(rule.ipProto()).byteValue()); |
| 279 | } |
| 280 | if (rule.dstTpPort() != 0) { |
| 281 | switch (rule.ipProto()) { |
| 282 | case IPv4.PROTOCOL_TCP: |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 283 | selectorBuilder.matchTcpDst(TpPort.tpPort(rule.dstTpPort())); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 284 | break; |
| 285 | case IPv4.PROTOCOL_UDP: |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 286 | selectorBuilder.matchUdpDst(TpPort.tpPort(rule.dstTpPort())); |
Pengfei Lu | e0c02e2 | 2015-07-07 15:41:31 +0800 | [diff] [blame] | 287 | break; |
| 288 | default: |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | if (rule.action() == AclRule.Action.ALLOW) { |
| 293 | treatment.add(Instructions.createOutput(PortNumber.CONTROLLER)); |
| 294 | } |
| 295 | flowEntry.forDevice(deviceId); |
| 296 | flowEntry.withPriority(aclStore.getPriorityByDevice(deviceId)); |
| 297 | flowEntry.withSelector(selectorBuilder.build()); |
| 298 | flowEntry.withTreatment(treatment.build()); |
| 299 | flowEntry.fromApp(appId); |
| 300 | flowEntry.makePermanent(); |
| 301 | // install flow rule |
| 302 | flowRuleService.applyFlowRules(flowEntry.build()); |
| 303 | log.debug("ACL flow rule {} is installed in {}.", flowEntry.build(), deviceId); |
| 304 | aclStore.addRuleToFlowMapping(rule.id(), flowEntry.build()); |
| 305 | aclStore.addRuleToDeviceMapping(rule.id(), deviceId); |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void removeAclRule(RuleId ruleId) { |
| 310 | aclStore.removeAclRule(ruleId); |
| 311 | log.info("ACL rule(id:{}) is removed.", ruleId); |
| 312 | enforceRuleRemoving(ruleId); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Enforces removing an existing ACL rule. |
| 317 | */ |
| 318 | private void enforceRuleRemoving(RuleId ruleId) { |
| 319 | Set<FlowRule> flowSet = aclStore.getFlowByRule(ruleId); |
| 320 | if (flowSet != null) { |
| 321 | for (FlowRule flowRule : flowSet) { |
| 322 | flowRuleService.removeFlowRules(flowRule); |
| 323 | log.debug("ACL flow rule {} is removed from {}.", flowRule.toString(), flowRule.deviceId().toString()); |
| 324 | } |
| 325 | } |
| 326 | aclStore.removeRuleToFlowMapping(ruleId); |
| 327 | aclStore.removeRuleToDeviceMapping(ruleId); |
| 328 | aclStore.removeDenyToAllowMapping(ruleId); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void clearAcl() { |
| 333 | aclStore.clearAcl(); |
| 334 | flowRuleService.removeFlowRulesById(appId); |
| 335 | log.info("ACL is cleared."); |
| 336 | } |
| 337 | |
| 338 | } |