use of org.onlab.packet.IPv4.PROTOCOL_ICMP in project onos by opennetworkinglab.
the class ReactiveForwarding method installRule.
// Install a rule forwarding the packet to the specified port.
private void installRule(PacketContext context, PortNumber portNumber, ReactiveForwardMetrics macMetrics) {
//
// We don't support (yet) buffer IDs in the Flow Service so
// packet out first.
//
Ethernet inPkt = context.inPacket().parsed();
TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
// If PacketOutOnly or ARP packet than forward directly to output port
if (packetOutOnly || inPkt.getEtherType() == Ethernet.TYPE_ARP) {
packetOut(context, portNumber, macMetrics);
return;
}
//
if (matchDstMacOnly) {
selectorBuilder.matchEthDst(inPkt.getDestinationMAC());
} else {
selectorBuilder.matchInPort(context.inPacket().receivedFrom().port()).matchEthSrc(inPkt.getSourceMAC()).matchEthDst(inPkt.getDestinationMAC());
// If configured Match Vlan ID
if (matchVlanId && inPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
selectorBuilder.matchVlanId(VlanId.vlanId(inPkt.getVlanID()));
}
//
if (matchIpv4Address && inPkt.getEtherType() == Ethernet.TYPE_IPV4) {
IPv4 ipv4Packet = (IPv4) inPkt.getPayload();
byte ipv4Protocol = ipv4Packet.getProtocol();
Ip4Prefix matchIp4SrcPrefix = Ip4Prefix.valueOf(ipv4Packet.getSourceAddress(), Ip4Prefix.MAX_MASK_LENGTH);
Ip4Prefix matchIp4DstPrefix = Ip4Prefix.valueOf(ipv4Packet.getDestinationAddress(), Ip4Prefix.MAX_MASK_LENGTH);
selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPSrc(matchIp4SrcPrefix).matchIPDst(matchIp4DstPrefix);
if (matchIpv4Dscp) {
byte dscp = ipv4Packet.getDscp();
byte ecn = ipv4Packet.getEcn();
selectorBuilder.matchIPDscp(dscp).matchIPEcn(ecn);
}
if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) {
TCP tcpPacket = (TCP) ipv4Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv4Protocol).matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())).matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
}
if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_UDP) {
UDP udpPacket = (UDP) ipv4Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv4Protocol).matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())).matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
}
if (matchIcmpFields && ipv4Protocol == IPv4.PROTOCOL_ICMP) {
ICMP icmpPacket = (ICMP) ipv4Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv4Protocol).matchIcmpType(icmpPacket.getIcmpType()).matchIcmpCode(icmpPacket.getIcmpCode());
}
}
//
if (matchIpv6Address && inPkt.getEtherType() == Ethernet.TYPE_IPV6) {
IPv6 ipv6Packet = (IPv6) inPkt.getPayload();
byte ipv6NextHeader = ipv6Packet.getNextHeader();
Ip6Prefix matchIp6SrcPrefix = Ip6Prefix.valueOf(ipv6Packet.getSourceAddress(), Ip6Prefix.MAX_MASK_LENGTH);
Ip6Prefix matchIp6DstPrefix = Ip6Prefix.valueOf(ipv6Packet.getDestinationAddress(), Ip6Prefix.MAX_MASK_LENGTH);
selectorBuilder.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Src(matchIp6SrcPrefix).matchIPv6Dst(matchIp6DstPrefix);
if (matchIpv6FlowLabel) {
selectorBuilder.matchIPv6FlowLabel(ipv6Packet.getFlowLabel());
}
if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_TCP) {
TCP tcpPacket = (TCP) ipv6Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv6NextHeader).matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())).matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
}
if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_UDP) {
UDP udpPacket = (UDP) ipv6Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv6NextHeader).matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())).matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
}
if (matchIcmpFields && ipv6NextHeader == IPv6.PROTOCOL_ICMP6) {
ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv6NextHeader).matchIcmpv6Type(icmp6Packet.getIcmpType()).matchIcmpv6Code(icmp6Packet.getIcmpCode());
}
}
}
TrafficTreatment treatment;
if (inheritFlowTreatment) {
treatment = context.treatmentBuilder().setOutput(portNumber).build();
} else {
treatment = DefaultTrafficTreatment.builder().setOutput(portNumber).build();
}
ForwardingObjective forwardingObjective = DefaultForwardingObjective.builder().withSelector(selectorBuilder.build()).withTreatment(treatment).withPriority(flowPriority).withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makeTemporary(flowTimeout).add();
flowObjectiveService.forward(context.inPacket().receivedFrom().deviceId(), forwardingObjective);
forwardPacket(macMetrics);
//
if (packetOutOfppTable) {
packetOut(context, PortNumber.TABLE, macMetrics);
} else {
packetOut(context, portNumber, macMetrics);
}
}
use of org.onlab.packet.IPv4.PROTOCOL_ICMP 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.PROTOCOL_ICMP in project onos by opennetworkinglab.
the class SimpleFabricRouting method checkVirtualGatewayIpPacket.
/**
* handle Packet with dstIp=virtualGatewayIpAddresses.
* returns true(handled) or false(not for virtual gateway)
*/
private boolean checkVirtualGatewayIpPacket(InboundPacket pkt, IpAddress srcIp, IpAddress dstIp) {
// assume valid
Ethernet ethPkt = pkt.parsed();
MacAddress mac = simpleFabric.vMacForIp(dstIp);
if (mac == null || !simpleFabric.isVirtualGatewayMac(ethPkt.getDestinationMAC())) {
/* Destination MAC should be any of virtual gateway macs */
return false;
} else if (dstIp.isIp4()) {
IPv4 ipv4Packet = (IPv4) ethPkt.getPayload();
if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
ICMP icmpPacket = (ICMP) ipv4Packet.getPayload();
if (icmpPacket.getIcmpType() == ICMP.TYPE_ECHO_REQUEST) {
log.info("IPV4 ICMP ECHO request to virtual gateway: " + "srcIp={} dstIp={} proto={}", srcIp, dstIp, ipv4Packet.getProtocol());
TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(pkt.receivedFrom().port()).build();
OutboundPacket packet = new DefaultOutboundPacket(pkt.receivedFrom().deviceId(), treatment, ByteBuffer.wrap(icmpPacket.buildIcmpReply(pkt.parsed()).serialize()));
packetService.emit(packet);
return true;
}
}
log.warn("IPV4 packet to virtual gateway dropped: " + "srcIp={} dstIp={} proto={}", srcIp, dstIp, ipv4Packet.getProtocol());
return true;
} else if (dstIp.isIp6()) {
// TODO: not tested yet (2017-07-20)
IPv6 ipv6Packet = (IPv6) ethPkt.getPayload();
if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST) {
log.info("IPV6 ICMP6 ECHO request to virtual gateway: srcIp={} dstIp={} nextHeader={}", srcIp, dstIp, ipv6Packet.getNextHeader());
TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(pkt.receivedFrom().port()).build();
OutboundPacket packet = new DefaultOutboundPacket(pkt.receivedFrom().deviceId(), treatment, ByteBuffer.wrap(icmp6Packet.buildIcmp6Reply(pkt.parsed()).serialize()));
packetService.emit(packet);
return true;
}
}
log.warn("IPV6 packet to virtual gateway dropped: srcIp={} dstIp={} nextHeader={}", srcIp, dstIp, ipv6Packet.getNextHeader());
return true;
}
// unknown traffic
return false;
}
use of org.onlab.packet.IPv4.PROTOCOL_ICMP 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