Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.openstacknetworking.cli; |
| 17 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 21 | import org.onlab.packet.Ethernet; |
| 22 | import org.onlab.packet.IPv4; |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.TpPort; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
| 31 | import org.onosproject.net.flow.TrafficTreatment; |
| 32 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 33 | import org.onosproject.openstacknetworking.api.InstancePortService; |
| 34 | import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService; |
| 35 | |
| 36 | import java.util.Optional; |
| 37 | |
Jian Li | 5c09e21 | 2018-10-24 18:23:58 +0900 | [diff] [blame] | 38 | import static org.onosproject.openstacknetworking.api.Constants.DHCP_TABLE; |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 39 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
| 40 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_FORCED_ACL_RULE; |
| 41 | |
| 42 | /** |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 43 | * Adds a acl. |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 44 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 45 | @Service |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 46 | @Command(scope = "onos", name = "openstack-add-acl", |
| 47 | description = "Add acl rules to VM") |
| 48 | public class OpenstackAddAclCommand extends AbstractShellCommand { |
| 49 | @Argument(index = 0, name = "src ip", description = "src ip address", required = true) |
Daniel Park | cd91a07 | 2018-07-09 16:00:40 +0900 | [diff] [blame] | 50 | private String srcIpStr = null; |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 51 | |
Daniel Park | cd91a07 | 2018-07-09 16:00:40 +0900 | [diff] [blame] | 52 | @Argument(index = 1, name = "src ip", description = "src tcp port", required = true) |
| 53 | private int srcPort = 0; |
| 54 | |
| 55 | @Argument(index = 2, name = "dst ip", description = "dst ip address", required = true) |
| 56 | private String dstIpStr = null; |
| 57 | |
| 58 | @Argument(index = 3, name = "dst port", description = "dst tcp port", required = true) |
| 59 | private int dstPort = 0; |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 60 | |
| 61 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 62 | protected void doExecute() { |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 63 | |
| 64 | OpenstackFlowRuleService flowRuleService = AbstractShellCommand.get(OpenstackFlowRuleService.class); |
| 65 | CoreService coreService = AbstractShellCommand.get(CoreService.class); |
| 66 | |
| 67 | ApplicationId appId = coreService.getAppId(OPENSTACK_NETWORKING_APP_ID); |
| 68 | |
| 69 | InstancePortService instancePortService = AbstractShellCommand.get(InstancePortService.class); |
| 70 | |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 71 | IpAddress srcIpAddress; |
Daniel Park | cd91a07 | 2018-07-09 16:00:40 +0900 | [diff] [blame] | 72 | |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 73 | IpAddress dstIpAddress; |
Daniel Park | cd91a07 | 2018-07-09 16:00:40 +0900 | [diff] [blame] | 74 | |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 75 | try { |
Daniel Park | cd91a07 | 2018-07-09 16:00:40 +0900 | [diff] [blame] | 76 | srcIpAddress = IpAddress.valueOf(srcIpStr); |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 77 | |
Daniel Park | cd91a07 | 2018-07-09 16:00:40 +0900 | [diff] [blame] | 78 | dstIpAddress = IpAddress.valueOf(dstIpStr); |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 79 | } catch (IllegalArgumentException e) { |
Daniel Park | a3ffbdb | 2018-11-28 13:51:39 +0900 | [diff] [blame] | 80 | log.error("IllegalArgumentException occurred because of {}", e); |
Ray Milkey | f6911bd | 2018-07-10 08:50:10 -0700 | [diff] [blame] | 81 | return; |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 82 | } |
Daniel Park | cd91a07 | 2018-07-09 16:00:40 +0900 | [diff] [blame] | 83 | |
| 84 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder() |
| 85 | .matchEthType(Ethernet.TYPE_IPV4) |
| 86 | .matchIPSrc(srcIpAddress.toIpPrefix()) |
| 87 | .matchIPDst(dstIpAddress.toIpPrefix()); |
| 88 | |
| 89 | TrafficTreatment treatment = DefaultTrafficTreatment.builder(). |
| 90 | drop().build(); |
| 91 | |
| 92 | if (srcPort != 0 || dstPort != 0) { |
| 93 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP); |
| 94 | if (srcPort != 0) { |
| 95 | sBuilder.matchTcpSrc(TpPort.tpPort(srcPort)); |
| 96 | } |
| 97 | |
| 98 | if (dstPort != 0) { |
| 99 | sBuilder.matchTcpDst(TpPort.tpPort(dstPort)); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | log.info("Deny the packet from srcIp: {}, dstPort: {} to dstIp: {}, dstPort: {}", |
| 104 | srcIpAddress.toString(), |
| 105 | srcPort, |
| 106 | dstIpAddress.toString(), |
| 107 | dstPort); |
| 108 | |
| 109 | Optional<InstancePort> instancePort = instancePortService.instancePorts().stream() |
| 110 | .filter(port -> port.ipAddress().toString().equals(dstIpStr)) |
| 111 | .findAny(); |
| 112 | |
| 113 | if (!instancePort.isPresent()) { |
| 114 | log.info("Instance port that matches with the given dst ip address isn't present {}"); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | flowRuleService.setRule( |
| 119 | appId, |
| 120 | instancePort.get().deviceId(), |
| 121 | sBuilder.build(), |
| 122 | treatment, |
| 123 | PRIORITY_FORCED_ACL_RULE, |
Jian Li | 5c09e21 | 2018-10-24 18:23:58 +0900 | [diff] [blame] | 124 | DHCP_TABLE, |
Daniel Park | cd91a07 | 2018-07-09 16:00:40 +0900 | [diff] [blame] | 125 | true); |
Daniel Park | 0e1c7b5 | 2018-07-07 01:00:14 +0900 | [diff] [blame] | 126 | } |
| 127 | } |