Java Code Examples for org.onlab.packet.Ethernet#TYPE_ARP

The following examples show how to use org.onlab.packet.Ethernet#TYPE_ARP . 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: OpenstackTroubleshootManager.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) {
        return;
    }

    IPv4 iPacket = (IPv4) ethernet.getPayload();
    if (iPacket.getProtocol() == IPv4.PROTOCOL_ICMP) {
        eventExecutor.execute(() -> processIcmpPacket(context, ethernet));
    }
}
 
Example 2
Source File: DefaultNeighbourMessageContext.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts context information from ARP packets.
 *
 * @param eth input Ethernet frame that is thought to be ARP
 * @param inPort in port
 * @param actions actions to take
 * @return MessageContext object if the packet was a valid ARP packet,
 * otherwise null
 */
private static NeighbourMessageContext createArpContext(Ethernet eth,
                                                        ConnectPoint inPort,
                                                        NeighbourMessageActions actions) {
    if (eth.getEtherType() != Ethernet.TYPE_ARP) {
        return null;
    }

    ARP arp = (ARP) eth.getPayload();

    IpAddress target = Ip4Address.valueOf(arp.getTargetProtocolAddress());
    IpAddress sender = Ip4Address.valueOf(arp.getSenderProtocolAddress());

    NeighbourMessageType type;
    if (arp.getOpCode() == ARP.OP_REQUEST) {
        type = NeighbourMessageType.REQUEST;
    } else if (arp.getOpCode() == ARP.OP_REPLY) {
        type = NeighbourMessageType.REPLY;
    } else {
        return null;
    }

    return new DefaultNeighbourMessageContext(actions, eth, inPort,
            NeighbourProtocol.ARP, type, target, sender);
}
 
Example 3
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 4
Source File: DefaultNeighbourMessageContext.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to create a MessageContext for the given Ethernet frame. If the
 * frame is a valid ARP or NDP request or response, a context will be
 * created.
 *
 * @param eth input Ethernet frame
 * @param inPort in port
 * @param actions actions to take
 * @return MessageContext if the packet was ARP or NDP, otherwise null
 */
public static NeighbourMessageContext createContext(Ethernet eth,
                                                    ConnectPoint inPort,
                                                    NeighbourMessageActions actions) {
    if (eth.getEtherType() == Ethernet.TYPE_ARP) {
        return createArpContext(eth, inPort, actions);
    } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
        return createNdpContext(eth, inPort, actions);
    }

    return null;
}
 
Example 5
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 6
Source File: OpenstackRoutingSnatHandler.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 eth = pkt.parsed();
    if (eth == null || eth.getEtherType() == Ethernet.TYPE_ARP) {
        return;
    }

    IPv4 iPacket = (IPv4) eth.getPayload();
    switch (iPacket.getProtocol()) {
        case IPv4.PROTOCOL_ICMP:
            break;
        case IPv4.PROTOCOL_UDP:
            UDP udpPacket = (UDP) iPacket.getPayload();
            if (udpPacket.getDestinationPort() == UDP.DHCP_SERVER_PORT &&
                    udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT) {
                break; // don't process DHCP
            }
        default:
            eventExecutor.execute(() -> {
                if (!isRelevantHelper(context)) {
                    return;
                }
                processSnatPacket(context, eth);
            });
            break;
    }
}
 
Example 7
Source File: OpenstackSwitchingHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Configures the flow rule which is for using VLAN to tag the packet
 * based on the in_port number of a virtual instance.
 * Note that this rule will be inserted in vTag table.
 *
 * @param instPort instance port object
 * @param install install flag, add the rule if true, remove it otherwise
 */
private void setVlanTagFlowRules(InstancePort instPort,
                                 short ethType,
                                 boolean install) {
    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchEthType(ethType)
            .matchInPort(instPort.portNumber())
            .build();

    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder()
            .pushVlan()
            .setVlanId(getVlanId(instPort));

    if (ethType == Ethernet.TYPE_ARP) {
        tBuilder.transition(ARP_TABLE);
    } else if (ethType == Ethernet.TYPE_IPV4) {
        tBuilder.transition(ACL_EGRESS_TABLE);
    }

    osFlowRuleService.setRule(
            appId,
            instPort.deviceId(),
            selector,
            tBuilder.build(),
            PRIORITY_TUNNEL_TAG_RULE,
            VTAG_TABLE,
            install);
}
 
Example 8
Source File: OpenstackSwitchingHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Configures the flow rule which is for using VXLAN/GRE to tag the packet
 * based on the in_port number of a virtual instance.
 * Note that this rule will be inserted in vTag table.
 *
 * @param instPort instance port object
 * @param install install flag, add the rule if true, remove it otherwise
 */
private void setTunnelTagFlowRules(InstancePort instPort,
                                   short ethType,
                                   boolean install) {
    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchEthType(ethType)
            .matchInPort(instPort.portNumber())
            .build();

    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder()
            .setTunnelId(getVni(instPort));


    if (ethType == Ethernet.TYPE_ARP) {
        tBuilder.transition(ARP_TABLE);
    } else if (ethType == Ethernet.TYPE_IPV4) {
        tBuilder.transition(ACL_EGRESS_TABLE);
    }

    osFlowRuleService.setRule(
            appId,
            instPort.deviceId(),
            selector,
            tBuilder.build(),
            PRIORITY_TUNNEL_TAG_RULE,
            VTAG_TABLE,
            install);
}
 
Example 9
Source File: ArpPacketClassifier.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public boolean match(PacketContext packet) {

    Ethernet eth = packet.inPacket().parsed();
    if (eth != null && (eth.getEtherType() == Ethernet.TYPE_ARP)) {
        ARP arpPacket = (ARP) eth.getPayload();
        if (arpPacket.getOpCode() == ARP.OP_REQUEST) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: VtnManager.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(PacketContext context) {
    InboundPacket pkt = context.inPacket();
    ConnectPoint connectPoint = pkt.receivedFrom();
    DeviceId deviceId = connectPoint.deviceId();
    Ethernet ethPkt = pkt.parsed();
    if (ethPkt == null) {
        return;
    }
    if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
        ARP arpPacket = (ARP) ethPkt.getPayload();
        if ((arpPacket.getOpCode() == ARP.OP_REQUEST)) {
            arprequestProcess(arpPacket, deviceId);
        } else if (arpPacket.getOpCode() == ARP.OP_REPLY) {
            arpresponceProcess(arpPacket, deviceId);
        }
    } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV4) {
        if (ethPkt.getDestinationMAC().isMulticast()) {
            return;
        }
        IPv4 ip = (IPv4) ethPkt.getPayload();
        upStreamPacketProcessor(ip, deviceId);

    } else {
        return;
    }
}
 
Example 11
Source File: LearningSwitchTutorial.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures packet is of required type. Obtain the port number associated with the packet's source ID.
 * If this port has previously been learned (in the process method) build a flow using the packet's
 * out port, treatment, destination, and other properties.  Send the flow to the learned out port.
 * Otherwise, flood packet to all ports if out port has not been learned.
 *
 * @param pc the PacketContext object passed through from activate() method
 */
public void actLikeSwitch(PacketContext pc) {

    /*
     * Ensures the type of packet being processed is only of type IPV4 or ARP (not LLDP or BDDP).
     * If it is not, return and do nothing with the packet. actLikeSwitch can only process
     * IPV4 and ARP packets.
     */
    Short type = pc.inPacket().parsed().getEtherType();
    if (type != Ethernet.TYPE_IPV4 &&  type != Ethernet.TYPE_ARP) {
        return;
    }

    /*
     * Learn the destination, source, and output port of the packet using a ConnectPoint and the
     * associated macTable.  If there is a known port associated with the packet's destination MAC Address,
     * the output port will not be null.
     */
    //find the packets connect point
    //save the macTables port value for the deviceID
    //save the outPort as a variable
    //PortNumber outPort = ...

    /*
     * If port is known, set output port to the packet's learned output port and construct a
     * FlowRule using a source, destination, treatment and other properties. Send the FlowRule
     * to the designated output port.
     */
    //if outPort isn't null
        //construct FlowRule
        //FlowRule fr = ...
        //send the packet

    /*
     * else, the output port has not been learned yet.  Flood the packet to all ports using
     * the actLikeHub method
     */
    //else
    //  call actLikeHub method
}
 
Example 12
Source File: DhcpManager.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void process(PacketContext context) {
    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_UDP) {
            UDP udpPacket = (UDP) ipv4Packet.getPayload();

            if (udpPacket.getDestinationPort() == UDP.DHCP_SERVER_PORT &&
                    udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT) {
                // This is meant for the dhcp server so process the packet here.

                DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
                processDhcpPacket(context, dhcpPayload);
            }
        }
    } else if (packet.getEtherType() == Ethernet.TYPE_ARP) {
        ARP arpPacket = (ARP) packet.getPayload();

        if ((arpPacket.getOpCode() == ARP.OP_REQUEST) &&
                Objects.equals(myIP, Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()))) {

            processArpPacket(context, packet);

        }
    }
}
 
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: K8sSwitchingArpHandler.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 15
Source File: PicaPipeline.java    From onos with Apache License 2.0 5 votes vote down vote up
private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
    log.debug("Processing versatile forwarding objective");
    TrafficSelector selector = fwd.selector();
    TrafficTreatment treatment = fwd.treatment();
    Collection<FlowRule> flowrules = new ArrayList<FlowRule>();

    // first add this rule for basic single-table operation
    // or non-ARP related multi-table operation
    FlowRule rule = DefaultFlowRule.builder()
            .forDevice(deviceId)
            .withSelector(selector)
            .withTreatment(treatment)
            .withPriority(fwd.priority())
            .fromApp(fwd.appId())
            .makePermanent()
            .forTable(ACL_TABLE).build();
    flowrules.add(rule);

    EthTypeCriterion ethType =
            (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
    if (ethType == null) {
        log.warn("No ethType in versatile forwarding obj. Not processing further.");
        return flowrules;
    }

    // now deal with possible mix of ARP with filtering objectives
    // in multi-table scenarios
    if (ethType.ethType().toShort() == Ethernet.TYPE_ARP) {
        if (filters.isEmpty()) {
            pendingVersatiles.add(fwd);
            return flowrules;
        }
        for (Filter filter : filters) {
            flowrules.addAll(processVersatilesWithFilters(filter, fwd));
        }
    }
    return flowrules;
}
 
Example 16
Source File: DhcpRelayManager.java    From onos with Apache License 2.0 4 votes vote down vote up
private void processInternal(PacketContext context) {
    // process the packet and get the payload
    Ethernet packet = context.inPacket().parsed();
    if (packet == null) {
        return;
    }

    findDhcp(packet).ifPresent(dhcpPayload -> {
        v4Handler.processDhcpPacket(context, dhcpPayload);
    });

    findDhcp6(packet).ifPresent(dhcp6Payload -> {
        v6Handler.processDhcpPacket(context, dhcp6Payload);
    });

    if (packet.getEtherType() == Ethernet.TYPE_ARP && arpEnabled) {
        ARP arpPacket = (ARP) packet.getPayload();
        VlanId vlanId = VlanId.vlanId(packet.getVlanID());
        Set<Interface> interfaces = interfaceService.
                getInterfacesByPort(context.inPacket().receivedFrom());
        //ignore the packets if dhcp server interface is not configured on onos.
        if (interfaces.isEmpty()) {
            log.warn("server virtual interface not configured");
            return;
        }
        if ((arpPacket.getOpCode() != ARP.OP_REQUEST)) {
            // handle request only
            return;
        }
        MacAddress interfaceMac = interfaces.stream()
                .filter(iface -> iface.vlan().equals(vlanId))
                .map(Interface::mac)
                .filter(mac -> !mac.equals(MacAddress.NONE))
                .findFirst()
                .orElse(MacAddress.ONOS);
        if (interfaceMac == null) {
            // can't find interface mac address
            return;
        }
        processArpPacket(context, packet, interfaceMac);
    }
}
 
Example 17
Source File: LearningSwitchSolution.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures packet is of required type. Obtain the PortNumber associated with the inPackets DeviceId.
 * If this port has previously been learned (in initMacTable method) build a flow using the packet's
 * out port, treatment, destination, and other properties.  Send the flow to the learned out port.
 * Otherwise, flood packet to all ports if out port is not learned.
 *
 * @param pc the PacketContext object passed through from activate() method
 */
public void actLikeSwitch(PacketContext pc) {

    /*
     * Ensures the type of packet being processed is only of type IPV4 (not LLDP or BDDP).  If it is not, return
     * and do nothing with the packet. actLikeSwitch can only process IPV4 packets.
     */
    Short type = pc.inPacket().parsed().getEtherType();
    if (type != Ethernet.TYPE_IPV4 && type != Ethernet.TYPE_ARP) {
        return;
    }

    /*
     * Learn the destination, source, and output port of the packet using a ConnectPoint and the
     * associated macTable.  If there is a known port associated with the packet's destination MAC Address,
     * the output port will not be null.
     */
    ConnectPoint cp = pc.inPacket().receivedFrom();
    Map<MacAddress, PortNumber> macTable = macTables.get(cp.deviceId());
    MacAddress srcMac = pc.inPacket().parsed().getSourceMAC();
    MacAddress dstMac = pc.inPacket().parsed().getDestinationMAC();
    macTable.put(srcMac, cp.port());
    PortNumber outPort = macTable.get(dstMac);

    /*
     * If port is known, set pc's out port to the packet's learned output port and construct a
     * FlowRule using a source, destination, treatment and other properties. Send the FlowRule
     * to the designated output port.
     */
    if (outPort != null) {
        pc.treatmentBuilder().setOutput(outPort);
        FlowRule fr = DefaultFlowRule.builder()
                .withSelector(DefaultTrafficSelector.builder().matchEthDst(dstMac).build())
                .withTreatment(DefaultTrafficTreatment.builder().setOutput(outPort).build())
                .forDevice(cp.deviceId()).withPriority(PacketPriority.REACTIVE.priorityValue())
                .makeTemporary(60)
                .fromApp(appId).build();

        flowRuleService.applyFlowRules(fr);
        pc.send();
    } else {
    /*
     * else, the output port has not been learned yet.  Flood the packet to all ports using
     * the actLikeHub method
     */
        actLikeHub(pc);
    }
}
 
Example 18
Source File: PacketStatistics.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();

    //Indicates whether this is an ARP Packet
    if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
        arpCounter.inc();

    } else if (ethPkt.getEtherType() == Ethernet.TYPE_LLDP) {
        lldpCounter.inc();

    } else if (ethPkt.getEtherType() == Ethernet.TYPE_VLAN) {
        vlanCounter.inc();

    } else if (ethPkt.getEtherType() == Ethernet.TYPE_BSN) {
        bsnCounter.inc();

    } else if (ethPkt.getEtherType() == Ethernet.TYPE_RARP) {
        rarpCounter.inc();

    } else if (ethPkt.getEtherType() == Ethernet.MPLS_UNICAST
            || ethPkt.getEtherType() == Ethernet.MPLS_MULTICAST) {
        mplsCounter.inc();

    } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV4) {
        IPv4 ipv4Packet = (IPv4) ethPkt.getPayload();
        //Indicates whether this is a TCP Packet
        if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_TCP) {
            tcpCounter.inc();

        } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
            icmpCounter.inc();

        } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_IGMP) {
            igmpCounter.inc();

        } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_PIM) {
            pimCounter.inc();

        } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
            UDP udpPacket = (UDP) ipv4Packet.getPayload();
                //Indicates whether this packet is a DHCP Packet
                if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT
                        || udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) {
                    dhcpCounter.inc();
                }
            }
    } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV6) {
           IPv6 ipv6Pkt = (IPv6) ethPkt.getPayload();
           if (ipv6Pkt.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
               icmp6Counter.inc();
               ICMP6 icmpv6 = (ICMP6) ipv6Pkt.getPayload();
               if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
                  nbrSolicitCounter.inc();
               } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
                  nbrAdvertCounter.inc();
               }
           }
    } else {
            log.debug("Packet is unknown.");
            unknownCounter.inc();
    }
}
 
Example 19
Source File: CastorArpManager.java    From onos with Apache License 2.0 4 votes vote down vote up
private MessageContext createContext(Ethernet eth, ConnectPoint inPort) {
    if (eth.getEtherType() == Ethernet.TYPE_ARP) {
        return createArpContext(eth, inPort);
    }
    return null;
}
 
Example 20
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())));
}