use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.
the class OpenstackRoutingSnatHandler method setStatelessSnatDownstreamRules.
private void setStatelessSnatDownstreamRules(InstancePort srcInstPort, String segmentId, Type networkType, IpAddress externalIp, ExternalPeerRouter externalPeerRouter, TpPort patPort, InboundPacket packetIn) {
IPv4 iPacket = (IPv4) packetIn.parsed().getPayload();
IpAddress internalIp = IpAddress.valueOf(iPacket.getSourceAddress());
TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPProtocol(iPacket.getProtocol()).matchIPDst(IpPrefix.valueOf(externalIp.getIp4Address(), VM_PREFIX)).matchIPSrc(IpPrefix.valueOf(iPacket.getDestinationAddress(), VM_PREFIX));
TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder().setEthDst(packetIn.parsed().getSourceMAC()).setIpDst(internalIp);
if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
sBuilder.matchVlanId(externalPeerRouter.vlanId());
tBuilder.popVlan();
}
switch(networkType) {
case VXLAN:
case GRE:
case GENEVE:
tBuilder.setTunnelId(Long.parseLong(segmentId));
break;
case VLAN:
tBuilder.pushVlan().setVlanId(VlanId.vlanId(segmentId));
break;
default:
final String error = String.format("%s %s", ERR_UNSUPPORTED_NET_TYPE, networkType.toString());
throw new IllegalStateException(error);
}
switch(iPacket.getProtocol()) {
case IPv4.PROTOCOL_TCP:
TCP tcpPacket = (TCP) iPacket.getPayload();
sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getDestinationPort())).matchTcpDst(patPort);
tBuilder.setTcpDst(TpPort.tpPort(tcpPacket.getSourcePort()));
break;
case IPv4.PROTOCOL_UDP:
UDP udpPacket = (UDP) iPacket.getPayload();
sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getDestinationPort())).matchUdpDst(patPort);
tBuilder.setUdpDst(TpPort.tpPort(udpPacket.getSourcePort()));
break;
default:
break;
}
OpenstackNode srcNode = osNodeService.node(srcInstPort.deviceId());
osNodeService.completeNodes(GATEWAY).forEach(gNode -> {
TrafficTreatment treatment = getDownstreamTreatment(networkType, tBuilder, gNode, srcNode);
osFlowRuleService.setRule(appId, gNode.intgBridge(), sBuilder.build(), treatment, PRIORITY_SNAT_RULE, GW_COMMON_TABLE, true);
});
}
use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.
the class OpenstackRemoveAclCommand method doExecute.
@Override
protected void doExecute() {
OpenstackFlowRuleService flowRuleService = get(OpenstackFlowRuleService.class);
CoreService coreService = get(CoreService.class);
ApplicationId appId = coreService.getAppId(OPENSTACK_NETWORKING_APP_ID);
InstancePortService instancePortService = get(InstancePortService.class);
IpAddress srcIpAddress = null;
IpAddress dstIpAddress = null;
try {
srcIpAddress = IpAddress.valueOf(srcIpStr);
dstIpAddress = IpAddress.valueOf(dstIpStr);
} catch (IllegalArgumentException e) {
log.error("IllegalArgumentException occurred because of {}", e);
return;
}
TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPSrc(srcIpAddress.toIpPrefix()).matchIPDst(dstIpAddress.toIpPrefix());
TrafficTreatment treatment = DefaultTrafficTreatment.builder().drop().build();
if (srcPort != 0 || dstPort != 0) {
sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP);
if (srcPort != 0) {
sBuilder.matchTcpSrc(TpPort.tpPort(srcPort));
}
if (dstPort != 0) {
sBuilder.matchTcpDst(TpPort.tpPort(dstPort));
}
}
log.info("Deny the packet from srcIp: {}, dstPort: {} to dstIp: {}, dstPort: {}", srcIpAddress.toString(), srcPort, dstIpAddress.toString(), dstPort);
Optional<InstancePort> instancePort = instancePortService.instancePorts().stream().filter(port -> port.ipAddress().toString().equals(dstIpStr)).findAny();
if (!instancePort.isPresent()) {
log.info("Instance port that matches with the given dst ip address isn't present {}");
return;
}
flowRuleService.setRule(appId, instancePort.get().deviceId(), sBuilder.build(), treatment, PRIORITY_FORCED_ACL_RULE, DHCP_TABLE, false);
}
use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.
the class OpenstackRoutingSnatIcmpHandlerTest method constructIcmpRequestPacket.
private Ethernet constructIcmpRequestPacket(IpAddress srcIp, MacAddress srcMac, IpAddress dstIp, MacAddress dstMac, byte icmpType) {
try {
ICMPEcho icmpEcho = new ICMPEcho();
icmpEcho.setIdentifier((short) 0).setSequenceNum((short) 0);
ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize());
ICMP icmp = new ICMP();
icmp.setIcmpType(icmpType).setIcmpCode(icmpType == TYPE_ECHO_REQUEST ? CODE_ECHO_REQUEST : CODE_ECHO_REPLY).setChecksum((short) 0);
icmp.setPayload(ICMPEcho.deserializer().deserialize(byteBufferIcmpEcho.array(), 0, ICMPEcho.ICMP_ECHO_HEADER_LENGTH));
ByteBuffer byteBufferIcmp = ByteBuffer.wrap(icmp.serialize());
IPv4 iPacket = new IPv4();
iPacket.setDestinationAddress(dstIp.toString());
iPacket.setSourceAddress(srcIp.toString());
iPacket.setTtl((byte) 64);
iPacket.setChecksum((short) 0);
iPacket.setDiffServ((byte) 0);
iPacket.setProtocol(IPv4.PROTOCOL_ICMP);
iPacket.setPayload(ICMP.deserializer().deserialize(byteBufferIcmp.array(), 0, 8));
Ethernet ethPacket = new Ethernet();
ethPacket.setEtherType(TYPE_IPV4);
ethPacket.setSourceMACAddress(srcMac);
ethPacket.setDestinationMACAddress(dstMac);
ethPacket.setPayload(iPacket);
return ethPacket;
} catch (DeserializationException e) {
return null;
}
}
use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.
the class OpenstackSwitchingDhcpHandlerTest method constructDhcpPacket.
/**
* Constructs an Ethernet packet containing a DHCP payload.
*
* @param msgType DHCP message type
* @return Ethernet packet
*/
private Ethernet constructDhcpPacket(DHCP.MsgType msgType) {
// Ethernet frame
Ethernet ethFrame = new Ethernet();
ethFrame.setSourceMACAddress(CLIENT_HOST.mac());
ethFrame.setDestinationMACAddress(MacAddress.BROADCAST);
ethFrame.setEtherType(Ethernet.TYPE_IPV4);
ethFrame.setVlanID((short) 2);
// IP packet
IPv4 ipv4Pkt = new IPv4();
ipv4Pkt.setSourceAddress(0);
ipv4Pkt.setDestinationAddress(BROADCAST.toInt());
ipv4Pkt.setTtl((byte) 127);
// UDP datagram
UDP udpDatagram = new UDP();
udpDatagram.setSourcePort((byte) UDP.DHCP_CLIENT_PORT);
udpDatagram.setDestinationPort((byte) UDP.DHCP_SERVER_PORT);
// DHCP payload
DHCP dhcp = new DHCP();
dhcp.setOpCode(DHCP.OPCODE_REQUEST);
dhcp.setYourIPAddress(0);
dhcp.setServerIPAddress(0);
dhcp.setTransactionId(TRANSACTION_ID);
dhcp.setClientHardwareAddress(CLIENT_HOST.mac().toBytes());
dhcp.setHardwareType(DHCP.HWTYPE_ETHERNET);
dhcp.setHardwareAddressLength((byte) 6);
// DHCP options start...
DhcpOption option = new DhcpOption();
List<DhcpOption> optionList = new ArrayList<>();
// DHCP message type
option.setCode(OptionCode_MessageType.getValue());
option.setLength((byte) 1);
byte[] optionData = { (byte) msgType.getValue() };
option.setData(optionData);
optionList.add(option);
// DHCP requested IP address
option = new DhcpOption();
option.setCode(OptionCode_RequestedIP.getValue());
option.setLength((byte) 4);
optionData = Ip4Address.valueOf(EXPECTED_IP).toOctets();
option.setData(optionData);
optionList.add(option);
// DHCP domain server
Subnet subnet = dhcpHandler.osNetworkService.subnet("subnet");
option = new DhcpOption();
option.setCode(OptionCode_DomainServer.getValue());
option.setLength((byte) 8);
ByteBuffer dnsByteBuf = ByteBuffer.allocate(8);
dnsByteBuf.put(DEFAULT_PRIMARY_DNS.toOctets());
dnsByteBuf.put(DEFAULT_SECONDARY_DNS.toOctets());
option.setData(dnsByteBuf.array());
optionList.add(option);
// MTU
option = new DhcpOption();
option.setCode(DHCP_OPTION_MTU);
option.setLength((byte) 2);
option.setData(ByteBuffer.allocate(2).putShort(MTU).array());
optionList.add(option);
// classless static route
option = new DhcpOption();
option.setCode(OptionCode_Classless_Static_Route.getValue());
option.setLength((byte) HOST_ROUTES_SIZE);
ByteBuffer hostRouteByteBuf = ByteBuffer.allocate(HOST_ROUTES_SIZE);
subnet.getHostRoutes().forEach(h -> {
IpPrefix ipPrefix = IpPrefix.valueOf(h.getDestination());
hostRouteByteBuf.put(bytesDestinationDescriptor(ipPrefix));
hostRouteByteBuf.put(Ip4Address.valueOf(h.getNexthop()).toOctets());
});
option.setData(hostRouteByteBuf.array());
optionList.add(option);
// default router address setup
option = new DhcpOption();
option.setCode(OptionCode_RouterAddress.getValue());
option.setLength((byte) 4);
option.setData(Ip4Address.valueOf(subnet.getGateway()).toOctets());
optionList.add(option);
// DHCP options end...
option = new DhcpOption();
option.setCode(OptionCode_END.getValue());
option.setLength((byte) 1);
optionList.add(option);
dhcp.setOptions(optionList);
udpDatagram.setPayload(dhcp);
ipv4Pkt.setPayload(udpDatagram);
ethFrame.setPayload(ipv4Pkt);
return ethFrame;
}
use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.
the class OpenstackTroubleshootManager method constructIcmpPacket.
/**
* Constructs an ICMP packet with given source and destination IP/MAC.
*
* @param srcIp source IP address
* @param dstIp destination IP address
* @param srcMac source MAC address
* @param dstMac destination MAC address
* @param icmpId ICMP identifier
* @param icmpSeq ICMP sequence number
* @return an ethernet frame which contains ICMP payload
*/
private Ethernet constructIcmpPacket(IpAddress srcIp, IpAddress dstIp, MacAddress srcMac, MacAddress dstMac, short icmpId, short icmpSeq) {
// Ethernet frame
Ethernet ethFrame = new Ethernet();
ethFrame.setEtherType(TYPE_IPV4);
ethFrame.setSourceMACAddress(srcMac);
ethFrame.setDestinationMACAddress(dstMac);
// IP packet
IPv4 iPacket = new IPv4();
iPacket.setDestinationAddress(dstIp.toString());
iPacket.setSourceAddress(srcIp.toString());
iPacket.setTtl(TTL);
iPacket.setProtocol(IPv4.PROTOCOL_ICMP);
// ICMP packet
ICMP icmp = new ICMP();
icmp.setIcmpType(TYPE_ECHO_REQUEST).setIcmpCode(TYPE_ECHO_REQUEST).resetChecksum();
// ICMP ECHO packet
ICMPEcho icmpEcho = new ICMPEcho();
icmpEcho.setIdentifier(icmpId).setSequenceNum(icmpSeq);
ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize());
try {
icmp.setPayload(ICMPEcho.deserializer().deserialize(byteBufferIcmpEcho.array(), 0, ICMPEcho.ICMP_ECHO_HEADER_LENGTH));
} catch (DeserializationException e) {
log.warn("Failed to deserialize ICMP ECHO REQUEST packet");
}
ByteBuffer byteBufferIcmp = ByteBuffer.wrap(icmp.serialize());
try {
iPacket.setPayload(ICMP.deserializer().deserialize(byteBufferIcmp.array(), 0, byteBufferIcmp.array().length));
} catch (DeserializationException e) {
log.warn("Failed to deserialize ICMP packet");
}
ethFrame.setPayload(iPacket);
return ethFrame;
}
Aggregations