Java Code Examples for org.onlab.packet.Ethernet#getEtherType()

The following examples show how to use org.onlab.packet.Ethernet#getEtherType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenstackRoutingArpHandler.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void process(PacketContext context) {
    if (context.isHandled()) {
        return;
    }

    InboundPacket pkt = context.inPacket();
    Ethernet ethernet = pkt.parsed();
    if (ethernet != null && ethernet.getEtherType() == Ethernet.TYPE_ARP) {
        eventExecutor.execute(() -> {

            if (!isRelevantHelper(context)) {
                return;
            }

            processArpPacket(context, ethernet);
        });
    }
}
 
Example 2
Source File: Icmp6PacketClassifier.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public boolean match(PacketContext packet) {
    Ethernet eth = packet.inPacket().parsed();

    if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
        IPv6 ipv6Packet = (IPv6) eth.getPayload();

        if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
            ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
            if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST) {
                return true;
            }
        }
    }
    return false;
}
 
Example 3
Source File: OpenstackSwitchingDhcpHandler.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void process(PacketContext context) {
    if (context.isHandled()) {
        return;
    }

    Ethernet ethPacket = context.inPacket().parsed();
    if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_IPV4) {
        return;
    }
    IPv4 ipv4Packet = (IPv4) ethPacket.getPayload();
    if (ipv4Packet.getProtocol() != IPv4.PROTOCOL_UDP) {
        return;
    }
    UDP udpPacket = (UDP) ipv4Packet.getPayload();
    if (udpPacket.getDestinationPort() != UDP.DHCP_SERVER_PORT ||
            udpPacket.getSourcePort() != UDP.DHCP_CLIENT_PORT) {
        return;
    }

    DHCP dhcpPacket = (DHCP) udpPacket.getPayload();

    eventExecutor.execute(() -> processDhcp(context, dhcpPacket));
}
 
Example 4
Source File: Dhcp6DirectPacketClassifier.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public boolean match(PacketContext packet) {

    Ethernet eth = packet.inPacket().parsed();

    if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
        IPv6 ipv6Packet = (IPv6) eth.getPayload();

        if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_UDP) {
            UDP udpPacket = (UDP) ipv6Packet.getPayload();
            //Directly connected host
            if (udpPacket.getDestinationPort() == UDP.DHCP_V6_SERVER_PORT &&
                    udpPacket.getSourcePort() == UDP.DHCP_V6_CLIENT_PORT) {
                DHCP6 dhcp6 = (DHCP6) udpPacket.getPayload();
                if (dhcp6.getMsgType() == DHCP6.MsgType.SOLICIT.value()) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 5
Source File: NSPacketClassifier.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public boolean match(PacketContext packet) {

    Ethernet eth = packet.inPacket().parsed();

    if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
        IPv6 ipv6Packet = (IPv6) eth.getPayload();
        if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
            ICMP6 icmp6 = (ICMP6) ipv6Packet.getPayload();
            if (icmp6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
                return true;
            }
        }
    }
    return false;
}
 
Example 6
Source File: RouterAdvertisementManager.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void process(PacketContext context) {
    if (context.isHandled()) {
        return;
    }

    // Ensure packet is IPv6 Solicited RA
    InboundPacket pkt = context.inPacket();
    Ethernet ethernet = pkt.parsed();
    if ((ethernet == null) || (ethernet.getEtherType() != Ethernet.TYPE_IPV6)) {
        return;
    }
    IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
    if (ipv6Packet.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
        return;
    }
    ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
    if (icmp6Packet.getIcmpType() != ICMP6.ROUTER_SOLICITATION) {
        return;
    }

    // Start solicited-RA handling thread
    SolicitedRAWorkerThread sraWorkerThread = new SolicitedRAWorkerThread(pkt);
    executors.schedule(sraWorkerThread, 0, TimeUnit.SECONDS);
}
 
Example 7
Source File: DhcpPacketClassifier.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public boolean match(PacketContext packet) {

    Ethernet eth = packet.inPacket().parsed();

    if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
        IPv4 ipv4Packet = (IPv4) eth.getPayload();

        if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
            UDP udpPacket = (UDP) ipv4Packet.getPayload();

            if (udpPacket.getDestinationPort() == UDP.DHCP_SERVER_PORT &&
                    udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT) {
                DHCP dhcp = (DHCP) udpPacket.getPayload();
                if (dhcp.getPacketType() == DHCP.MsgType.DHCPDISCOVER) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 8
Source File: TunnellingConnectivityManager.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void process(PacketContext context) {
    // Stop processing if the packet has been handled, since we
    // can't do any more to it.
    if (context.isHandled()) {
        return;
    }

    Ethernet packet = context.inPacket().parsed();

    if (packet == null) {
        return;
    }

    if (packet.getEtherType() == Ethernet.TYPE_IPV4) {
        IPv4 ipv4Packet = (IPv4) packet.getPayload();
        if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_TCP) {
            TCP tcpPacket = (TCP) ipv4Packet.getPayload();

            if (tcpPacket.getDestinationPort() == BGP_PORT ||
                    tcpPacket.getSourcePort() == BGP_PORT) {
                forward(context);
            }
        }
    }
}
 
Example 9
Source File: IcmpHandler.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void process(PacketContext context) {
    // Stop processing if the packet has been handled, since we
    // can't do any more to it.

    if (context.isHandled()) {
        return;
    }

    Ethernet packet = context.inPacket().parsed();

    if (packet == null) {
        return;
    }

    if (packet.getEtherType() == Ethernet.TYPE_IPV4) {
        IPv4 ipv4Packet = (IPv4) packet.getPayload();
        if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
            processPacketIn(context.inPacket());
        }
    }
}
 
Example 10
Source File: IcmpPacketClassifier.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public boolean match(PacketContext packet) {

    Ethernet eth = packet.inPacket().parsed();

    if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
        IPv4 ipv4Packet = (IPv4) eth.getPayload();

        if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
            ICMP icmpPacket = (ICMP) ipv4Packet.getPayload();
            if (icmpPacket.getIcmpType() == ICMP.TYPE_ECHO_REQUEST) {
                return true;
            }
        }
    }
    return false;
}
 
Example 11
Source File: PimPacketHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 12
Source File: OpenstackSwitchingArpHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(PacketContext context) {
    if (context.isHandled()) {
        return;
    }

    Ethernet ethPacket = context.inPacket().parsed();
    if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) {
        return;
    }

    eventExecutor.execute(() -> processPacketIn(context, ethPacket));
}
 
Example 13
Source File: K8sRoutingArpHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(PacketContext context) {
    if (context.isHandled()) {
        return;
    }

    InboundPacket pkt = context.inPacket();
    Ethernet ethernet = pkt.parsed();
    if (ethernet != null && ethernet.getEtherType() == Ethernet.TYPE_ARP) {
        eventExecutor.execute(() -> processArpPacket(context, ethernet));
    }
}
 
Example 14
Source File: OFChannelHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Classify the Ethernet packet for membership on one of the queues.
 *
 * @param packet ethernet packet
 * @return Id of destination Queue
 */
private int classifyEthernetPacket(Ethernet packet) {
    for (Set<OpenFlowClassifier> classifiers : this.messageClassifiersMapProducer) {
        for (OpenFlowClassifier classifier : classifiers) {
            if (classifier.ethernetType() == packet.getEtherType()) {
                return classifier.idQueue();
            }
        }
    }
    return NUM_OF_QUEUES - 1;
}
 
Example 15
Source File: VirtualPublicHosts.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void process(PacketContext context) {

    InboundPacket pkt = context.inPacket();
    Ethernet ethPkt = pkt.parsed();

    // Only handle the ARP packets
    if (ethPkt == null || ethPkt.getEtherType() != Ethernet.TYPE_ARP) {
        return;
    }
    ARP arpPacket = (ARP) ethPkt.getPayload();
    // Only handle ARP request packets
    if (arpPacket.getOpCode() != ARP.OP_REQUEST) {
        return;
    }

    Ip4Address targetIpAddress = Ip4Address
            .valueOf(arpPacket.getTargetProtocolAddress());

    // Only handle an ARP request when the target IP address inside is
    // an assigned public IP address
    if (!vbngConfigService.isAssignedPublicIpAddress(targetIpAddress)) {
        return;
    }

    MacAddress virtualHostMac =
            vbngConfigService.getPublicFacingMac();
    if (virtualHostMac == null) {
        return;
    }

    ConnectPoint srcConnectPoint = pkt.receivedFrom();
    Ethernet eth = ARP.buildArpReply(targetIpAddress,
                                     virtualHostMac,
                                     ethPkt);

    TrafficTreatment.Builder builder =
            DefaultTrafficTreatment.builder();
    builder.setOutput(srcConnectPoint.port());
    packetService.emit(new DefaultOutboundPacket(
            srcConnectPoint.deviceId(),
            builder.build(),
            ByteBuffer.wrap(eth.serialize())));
}
 
Example 16
Source File: LinkDiscovery.java    From onos with Apache License 2.0 4 votes vote down vote up
private boolean processLldp(PacketContext packetContext, Ethernet eth) {
    ONOSLLDP onoslldp = ONOSLLDP.parseLLDP(eth);
    if (onoslldp != null) {
        Type lt = eth.getEtherType() == Ethernet.TYPE_LLDP ?
                Type.DIRECT : Type.INDIRECT;

        DeviceService deviceService = context.deviceService();
        MacAddress srcChassisId = onoslldp.getChassisIdByMac();
        String srcPortName = onoslldp.getPortNameString();
        String srcPortDesc = onoslldp.getPortDescString();

        log.debug("srcChassisId:{}, srcPortName:{}, srcPortDesc:{}", srcChassisId, srcPortName, srcPortDesc);

        if (srcChassisId == null && srcPortDesc == null) {
            log.warn("there are no valid port id");
            return false;
        }

        Optional<Device> srcDevice = findSourceDeviceByChassisId(deviceService, srcChassisId);

        if (!srcDevice.isPresent()) {
            log.warn("source device not found. srcChassisId value: {}", srcChassisId);
            return false;
        }
        Optional<Port> sourcePort = findSourcePortByName(
                srcPortName == null ? srcPortDesc : srcPortName,
                deviceService,
                srcDevice.get());

        if (!sourcePort.isPresent()) {
            log.warn("source port not found. sourcePort value: {}", sourcePort);
            return false;
        }

        PortNumber srcPort = sourcePort.get().number();
        PortNumber dstPort = packetContext.inPacket().receivedFrom().port();

        DeviceId srcDeviceId = srcDevice.get().id();
        DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();

        if (!sourcePort.get().isEnabled()) {
            log.debug("Ports are disabled. Cannot create a link between {}/{} and {}/{}",
                    srcDeviceId, sourcePort.get(), dstDeviceId, dstPort);
            return false;
        }

        ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
        ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);

        DefaultAnnotations annotations = DefaultAnnotations.builder()
                .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase())
                .set(AnnotationKeys.LAYER, ETHERNET)
                .build();

        LinkDescription ld = new DefaultLinkDescription(src, dst, lt, true, annotations);
        try {
            context.providerService().linkDetected(ld);
            context.setTtl(LinkKey.linkKey(src, dst), onoslldp.getTtlBySeconds());
        } catch (IllegalStateException e) {
            log.debug("There is a exception during link creation: {}", e);
            return true;
        }
        return true;
    }
    return false;
}
 
Example 17
Source File: ReactiveForwarding.java    From onos with Apache License 2.0 4 votes vote down vote up
private boolean isIpv6Multicast(Ethernet eth) {
    return eth.getEtherType() == Ethernet.TYPE_IPV6 && eth.isMulticast();
}
 
Example 18
Source File: OpenstackMetadataProxyHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void process(PacketContext context) {

    if (context.isHandled()) {
        return;
    }

    // FIXME: need to find a way to spawn a new thread to check metadata proxy mode
    if (!useMetadataProxy()) {
        return;
    }

    Ethernet ethPacket = context.inPacket().parsed();
    if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_IPV4) {
        return;
    }

    IPv4 ipv4Packet = (IPv4) ethPacket.getPayload();
    if (ipv4Packet.getProtocol() != IPv4.PROTOCOL_TCP ||
            !IpAddress.valueOf(ipv4Packet.getDestinationAddress()).
                    equals(IpAddress.valueOf(METADATA_SERVER_IP))) {
        return;
    }

    TCP tcpPacket = (TCP) ipv4Packet.getPayload();
    if (tcpPacket.getDestinationPort() != HTTP_SERVER_PORT) {
        return;
    }

    // (three-way handshaking)
    // reply TCP SYN-ACK packet with receiving TCP SYN packet
    if (tcpPacket.getFlags() == SYN_FLAG) {
        Ethernet ethReply = buildTcpSynAckPacket(ethPacket, ipv4Packet, tcpPacket);
        sendReply(context, ethReply);
        return;
    }

    // (four-way handshaking)
    // reply TCP ACK and TCP FIN-ACK packets with receiving TCP FIN-ACK packet
    if (tcpPacket.getFlags() == FIN_ACK_FLAG) {
        Ethernet ackReply = buildTcpAckPacket(ethPacket, ipv4Packet, tcpPacket);
        sendReply(context, ackReply);
        Ethernet finAckReply = buildTcpFinAckPacket(ethPacket, ipv4Packet, tcpPacket);
        sendReply(context, finAckReply);
        return;
    }

    // normal TCP data transmission
    Data data = (Data) tcpPacket.getPayload();
    byte[] byteData = data.getData();

    if (byteData.length != 0) {
        eventExecutor.execute(() -> {
            processHttpRequest(context, ethPacket, ipv4Packet, tcpPacket, byteData);
        });
    }
}
 
Example 19
Source File: SegmentRoutingManager.java    From onos with Apache License 2.0 4 votes vote down vote up
private void processPacketInternal(PacketContext context) {
    if (context.isHandled()) {
        return;
    }

    InboundPacket pkt = context.inPacket();
    Ethernet ethernet = pkt.parsed();

    if (ethernet == null) {
        return;
    }

    log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
              ethernet);
    if (ethernet.getEtherType() == TYPE_ARP) {
        log.warn("Received unexpected ARP packet on {}",
                 context.inPacket().receivedFrom());
        log.trace("{}", ethernet);
        return;
    } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
        IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
        //ipHandler.addToPacketBuffer(ipv4Packet);
        if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
            icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
        } else {
            // NOTE: We don't support IP learning at this moment so this
            //       is not necessary. Also it causes duplication of DHCP packets.
            // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
        }
    } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
        IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
        //ipHandler.addToPacketBuffer(ipv6Packet);
        // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
        if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
            ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
            if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
                    icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
                icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
            } else {
                log.trace("Received ICMPv6 0x{} - not handled",
                        Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
            }
        } else {
           // NOTE: We don't support IP learning at this moment so this
           //       is not necessary. Also it causes duplication of DHCPv6 packets.
           // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
        }
    }
}
 
Example 20
Source File: MaoRoutingManager.java    From ONOS_LoadBalance_Routing_Forward with Apache License 2.0 4 votes vote down vote up
@Override
        public void process(PacketContext context) {

            if (context.isHandled()) {
                return;
            }

            Ethernet pkt = context.inPacket().parsed();
            if (pkt.getEtherType() == Ethernet.TYPE_IPV4) {

                HostId srcHostId = HostId.hostId(pkt.getSourceMAC());
                HostId dstHostId = HostId.hostId(pkt.getDestinationMAC());

                Set<Path> paths = getLoadBalancePaths(srcHostId, dstHostId);
                if (paths.isEmpty()) {
                    log.warn("paths is Empty !!! no Path is available");
                    context.block();
                    return;
                }

                IPv4 ipPkt = (IPv4) pkt.getPayload();
                TrafficSelector selector = DefaultTrafficSelector.builder()
                        .matchEthType(Ethernet.TYPE_IPV4)
                        .matchIPSrc(IpPrefix.valueOf(ipPkt.getSourceAddress(), 32))
                        .matchIPDst(IpPrefix.valueOf(ipPkt.getDestinationAddress(), 32))
                        .build();

                boolean isContain;
//                synchronized (intentMap) {
                isContain = intentMap.containsKey(selector.criteria());
//                }
                if (isContain) {
                    context.block();
                    return;
                }


                Path result = paths.iterator().next();
                log.info("\n------ Mao Path Info ------\nSrc:{}, Dst:{}\n{}",
                        IpPrefix.valueOf(ipPkt.getSourceAddress(), 32).toString(),
                        IpPrefix.valueOf(ipPkt.getDestinationAddress(), 32),
                        result.links().toString().replace("Default", "\n"));

                PathIntent pathIntent = PathIntent.builder()
                        .path(result)
                        .appId(appId)
                        .priority(65432)
                        .selector(selector)
                        .treatment(DefaultTrafficTreatment.emptyTreatment())
                        .build();

                intentService.submit(pathIntent);

//                synchronized (intentMap) {
                intentMap.put(selector.criteria(), pathIntent);
//                }

                context.block();
            }
        }