use of org.onlab.packet.IPv4 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 in project onos by opennetworkinglab.
the class DirectHostManager method handle.
private boolean handle(Ethernet eth) {
checkNotNull(eth);
// skip them.
if (!enabled || (eth.getEtherType() != Ethernet.TYPE_IPV6 && eth.getEtherType() != Ethernet.TYPE_IPV4)) {
return false;
}
// According to the type we set the destIp.
IpAddress dstIp;
if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
IPv4 ip = (IPv4) eth.getPayload();
dstIp = IpAddress.valueOf(ip.getDestinationAddress());
} else {
IPv6 ip = (IPv6) eth.getPayload();
dstIp = IpAddress.valueOf(INET6, ip.getDestinationAddress());
}
// Looking for a candidate output port.
Interface egressInterface = interfaceService.getMatchingInterface(dstIp);
if (egressInterface == null) {
log.info("No egress interface found for {}", dstIp);
return false;
}
// Looking for the destination mac.
Optional<Host> host = hostService.getHostsByIp(dstIp).stream().filter(h -> h.location().equals(egressInterface.connectPoint())).filter(h -> h.vlan().equals(egressInterface.vlan())).findAny();
// and we queue the packets waiting for a destination.
if (host.isPresent()) {
transformAndSend((IP) eth.getPayload(), eth.getEtherType(), egressInterface, host.get().mac());
} else {
hostService.startMonitoringIp(dstIp);
ipPacketCache.asMap().compute(dstIp, (ip, queue) -> {
if (queue == null) {
queue = new ConcurrentLinkedQueue<>();
}
queue.add((IP) eth.getPayload());
return queue;
});
}
return true;
}
use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.
the class PimInterface method processHello.
/**
* Process an incoming PIM Hello message. There are a few things going on in
* this method:
* <ul>
* <li>We <em>may</em> have to create a new neighbor if one does not already exist</li>
* <li>We <em>may</em> need to re-elect a new DR if new information is received</li>
* <li>We <em>may</em> need to send an existing neighbor all joins if the genid changed</li>
* <li>We will refresh the neighbor's timestamp</li>
* </ul>
*
* @param ethPkt the Ethernet packet header
*/
public void processHello(Ethernet ethPkt) {
if (log.isTraceEnabled()) {
log.trace("Received a PIM hello packet");
}
// We'll need to save our neighbors MAC address
MacAddress nbrmac = ethPkt.getSourceMAC();
// And we'll need to save neighbors IP Address.
IPv4 iphdr = (IPv4) ethPkt.getPayload();
IpAddress srcip = IpAddress.valueOf(iphdr.getSourceAddress());
PIM pimhdr = (PIM) iphdr.getPayload();
if (pimhdr.getPimMsgType() != PIM.TYPE_HELLO) {
log.error("process Hello has received a non hello packet type: " + pimhdr.getPimMsgType());
return;
}
// get the DR values for later calculation
PimNeighbor dr = pimNeighbors.get(drIpaddress);
checkNotNull(dr);
IpAddress drip = drIpaddress;
int drpri = dr.priority();
// Assume we do not need to run a DR election
boolean reElectDr = false;
boolean genidChanged = false;
PIMHello hello = (PIMHello) pimhdr.getPayload();
// Determine if we already have a PIMNeighbor
PimNeighbor nbr = pimNeighbors.getOrDefault(srcip, null);
PimNeighbor newNbr = PimNeighbor.createPimNeighbor(srcip, nbrmac, hello.getOptions().values());
if (nbr == null) {
pimNeighbors.putIfAbsent(srcip, newNbr);
nbr = newNbr;
} else if (!nbr.equals(newNbr)) {
if (newNbr.holdtime() == 0) {
// Neighbor has shut down. Remove them and clean up
pimNeighbors.remove(srcip, nbr);
return;
} else {
// Neighbor has changed one of their options.
pimNeighbors.put(srcip, newNbr);
nbr = newNbr;
}
}
// Refresh this neighbor's timestamp
nbr.refreshTimestamp();
/*
* the election method will first determine if an election
* needs to be run, if so it will run the election. The
* IP address of the DR will be returned. If the IP address
* of the DR is different from what we already have we know a
* new DR has been elected.
*/
IpAddress electedIp = election(nbr, drip, drpri);
if (!drip.equals(electedIp)) {
// we have a new DR.
drIpaddress = electedIp;
}
}
use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.
the class PimPacketHandler method processPacket.
/**
* Sanitize and process the packet.
* TODO: replace ConnectPoint with PIMInterface when PIMInterface has been added.
*
* @param ethPkt the packet starting with the Ethernet header.
* @param pimi the PIM Interface the packet arrived on.
*/
public void processPacket(Ethernet ethPkt, PimInterface pimi) {
checkNotNull(ethPkt);
checkNotNull(pimi);
// Sanitize the ethernet header to ensure it is IPv4. IPv6 we'll deal with later
if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4) {
return;
}
// Get the IP header
IPv4 ip = (IPv4) ethPkt.getPayload();
if (ip.getProtocol() != IPv4.PROTOCOL_PIM) {
return;
}
// Get the address of our the neighbor that sent this packet to us.
IpAddress nbraddr = IpAddress.valueOf(ip.getDestinationAddress());
if (log.isTraceEnabled()) {
log.trace("Packet {} received on port {}", nbraddr, pimi);
}
// Get the PIM header
PIM pim = (PIM) ip.getPayload();
checkNotNull(pim);
// Process the pim packet
switch(pim.getPimMsgType()) {
case PIM.TYPE_HELLO:
pimi.processHello(ethPkt);
break;
case PIM.TYPE_JOIN_PRUNE_REQUEST:
pimi.processJoinPrune(ethPkt);
log.debug("Received a PIM Join/Prune message");
break;
default:
log.debug("Received unsupported PIM type: {}", pim.getPimMsgType());
break;
}
}
use of org.onlab.packet.IPv4 in project onos by opennetworkinglab.
the class Dhcp4HandlerImpl method processLeaseQueryFromServer.
/**
* Build the DHCP offer/ack with proper client port.
*
* @param ethernetPacket the original packet comes from server
* @return new packet which will send to the client
*/
private InternalPacket processLeaseQueryFromServer(Ethernet ethernetPacket) {
// get dhcp header.
Ethernet etherReply = (Ethernet) ethernetPacket.clone();
IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
UDP udpPacket = (UDP) ipv4Packet.getPayload();
DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
// determine the vlanId of the client host - note that this vlan id
// could be different from the vlan in the packet from the server
Interface clientInterface = null;
MacAddress destinationMac = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
if (!learnRouteFromLeasequery) {
int giaddr = ipv4Packet.getDestinationAddress();
IpAddress destinationAddress = Ip4Address.valueOf(giaddr);
log.debug("DHCPLEASEQUERYRESP giaddr: {}({})", giaddr, destinationAddress);
Host destinationHost = hostService.getHostsByIp(destinationAddress).stream().findFirst().orElse(null);
if (destinationHost != null) {
destinationMac = destinationHost.mac();
log.trace("DHCPLEASEQUERYRESP destination mac is: {}", destinationMac);
ConnectPoint destinationLocation = destinationHost.location();
log.trace("Lookup for client interface by destination location {}", destinationLocation);
clientInterface = interfaceService.getInterfacesByPort(destinationLocation).stream().filter(iface -> interfaceContainsVlan(iface, VlanId.vlanId(etherReply.getVlanID()))).findFirst().orElse(null);
log.trace("Found Host {} by ip {}", destinationHost, destinationAddress);
log.debug("DHCPLEASEQUERYRESP Client interface: {}", (clientInterface != null ? clientInterface : "not resolved"));
}
} else {
clientInterface = getClientInterface(ethernetPacket, dhcpPayload).orElse(null);
}
if (clientInterface == null) {
log.warn("Cannot find the interface for the DHCP {}", dhcpPayload);
return null;
}
VlanId vlanId;
if (clientInterface.vlanTagged().isEmpty()) {
vlanId = clientInterface.vlan();
} else {
// might be multiple vlan in same interface
vlanId = getVlanIdFromRelayAgentOption(dhcpPayload);
}
if (vlanId == null) {
vlanId = VlanId.NONE;
}
etherReply.setVlanID(vlanId.toShort());
etherReply.setSourceMACAddress(clientInterface.mac());
if (!directlyConnected(dhcpPayload) && learnRouteFromLeasequery) {
// if client is indirectly connected, try use next hop mac address
MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
HostId hostId = HostId.hostId(macAddress, vlanId);
DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
if (record != null) {
// if next hop can be found, use mac address of next hop
Optional<MacAddress> nextHop = record.nextHopTemp();
if (!nextHop.isPresent()) {
nextHop = record.nextHop();
}
nextHop.ifPresent(etherReply::setDestinationMACAddress);
} else {
// otherwise, discard the packet
log.warn("Can't find record for host id {}, discard packet", hostId);
return null;
}
} else {
etherReply.setDestinationMACAddress(destinationMac);
}
udpPacket.setSourcePort(UDP.DHCP_SERVER_PORT);
if (directlyConnected(dhcpPayload)) {
udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT);
} else {
udpPacket.setDestinationPort(UDP.DHCP_SERVER_PORT);
}
udpPacket.setPayload(dhcpPayload);
ipv4Packet.setPayload(udpPacket);
etherReply.setPayload(ipv4Packet);
udpPacket.resetChecksum();
return InternalPacket.internalPacket(etherReply, clientInterface.connectPoint());
}
Aggregations