Search in sources :

Example 11 with UDP

use of org.onlab.packet.UDP in project onos by opennetworkinglab.

the class Dhcp6HandlerUtil method processLQ6PacketFromServer.

/**
 * process the LQ reply packet from dhcp server.
 *
 * @param defaultServerInfoList default server list
 * @param indirectServerInfoList default indirect server list
 * @param serverInterface server interface
 * @param interfaceService interface service
 * @param hostService host service
 * @param context packet context
 * @param receivedPacket server ethernet packet
 * @param recevingInterfaces set of server side interfaces
 * @return a packet ready to be sent to relevant output interface
 */
public static InternalPacket processLQ6PacketFromServer(List<DhcpServerInfo> defaultServerInfoList, List<DhcpServerInfo> indirectServerInfoList, Interface serverInterface, InterfaceService interfaceService, HostService hostService, PacketContext context, Ethernet receivedPacket, Set<Interface> recevingInterfaces) {
    // get dhcp6 header.
    Ethernet etherReply = (Ethernet) receivedPacket.clone();
    IPv6 ipv6Packet = (IPv6) etherReply.getPayload();
    UDP udpPacket = (UDP) ipv6Packet.getPayload();
    DHCP6 lq6Reply = (DHCP6) udpPacket.getPayload();
    // TODO: refactor
    ConnectPoint receivedFrom = context.inPacket().receivedFrom();
    DeviceId receivedFromDevice = receivedFrom.deviceId();
    DhcpServerInfo serverInfo;
    Ip6Address dhcpServerIp = null;
    ConnectPoint dhcpServerConnectPoint = null;
    MacAddress dhcpConnectMac = null;
    VlanId dhcpConnectVlan = null;
    Ip6Address dhcpGatewayIp = null;
    // todo: refactor
    Ip6Address indirectDhcpServerIp = null;
    ConnectPoint indirectDhcpServerConnectPoint = null;
    MacAddress indirectDhcpConnectMac = null;
    VlanId indirectDhcpConnectVlan = null;
    Ip6Address indirectDhcpGatewayIp = null;
    Ip6Address indirectRelayAgentIpFromCfg = null;
    if (!defaultServerInfoList.isEmpty()) {
        serverInfo = defaultServerInfoList.get(0);
        dhcpConnectMac = serverInfo.getDhcpConnectMac().orElse(null);
        dhcpGatewayIp = serverInfo.getDhcpGatewayIp6().orElse(null);
        dhcpServerIp = serverInfo.getDhcpServerIp6().orElse(null);
        dhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null);
        dhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
    }
    if (!indirectServerInfoList.isEmpty()) {
        serverInfo = indirectServerInfoList.get(0);
        indirectDhcpConnectMac = serverInfo.getDhcpConnectMac().orElse(null);
        indirectDhcpGatewayIp = serverInfo.getDhcpGatewayIp6().orElse(null);
        indirectDhcpServerIp = serverInfo.getDhcpServerIp6().orElse(null);
        indirectDhcpServerConnectPoint = serverInfo.getDhcpServerConnectPoint().orElse(null);
        indirectDhcpConnectVlan = serverInfo.getDhcpConnectVlan().orElse(null);
        indirectRelayAgentIpFromCfg = serverInfo.getRelayAgentIp6(receivedFromDevice).orElse(null);
    }
    Boolean directConnFlag = directlyConnected(lq6Reply);
    ConnectPoint inPort = context.inPacket().receivedFrom();
    if ((directConnFlag || indirectDhcpServerIp == null) && !inPort.equals(dhcpServerConnectPoint)) {
        log.warn("Receiving port {} is not the same as server connect point {} for direct or indirect-null", inPort, dhcpServerConnectPoint);
        return null;
    }
    if (!directConnFlag && indirectDhcpServerIp != null && !inPort.equals(indirectDhcpServerConnectPoint)) {
        log.warn("Receiving port {} is not the same as server connect point {} for indirect", inPort, indirectDhcpServerConnectPoint);
        return null;
    }
    Ip6Address nextHopIP = Ip6Address.valueOf(ipv6Packet.getDestinationAddress());
    // use hosts store to find out the next hop mac and connection point
    Set<Host> hosts = hostService.getHostsByIp(nextHopIP);
    Host host;
    if (!hosts.isEmpty()) {
        host = hosts.iterator().next();
    } else {
        log.warn("Host {} is not in store", nextHopIP);
        return null;
    }
    HostLocation hl = host.location();
    // iterator().next());
    String clientConnectionPointStr = hl.toString();
    ConnectPoint clientConnectionPoint = ConnectPoint.deviceConnectPoint(clientConnectionPointStr);
    VlanId originalPacketVlanId = VlanId.vlanId(etherReply.getVlanID());
    Interface iface;
    iface = interfaceService.getInterfacesByPort(clientConnectionPoint).stream().filter(iface1 -> interfaceContainsVlan(iface1, originalPacketVlanId)).findFirst().orElse(null);
    etherReply.setSourceMACAddress(iface.mac());
    etherReply.setDestinationMACAddress(host.mac());
    // workaround for a bug where core sends src port as 547 (server)
    udpPacket.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
    udpPacket.setPayload(lq6Reply);
    udpPacket.resetChecksum();
    ipv6Packet.setPayload(udpPacket);
    etherReply.setPayload(ipv6Packet);
    return InternalPacket.internalPacket(etherReply, clientConnectionPoint);
}
Also used : UDP(org.onlab.packet.UDP) HostLocation(org.onosproject.net.HostLocation) Host(org.onosproject.net.Host) Interface(org.onosproject.net.intf.Interface) LoggerFactory(org.slf4j.LoggerFactory) InterfaceService(org.onosproject.net.intf.InterfaceService) HostService(org.onosproject.net.host.HostService) Dhcp6ClientIdOption(org.onlab.packet.dhcp.Dhcp6ClientIdOption) Dhcp6RelayOption(org.onlab.packet.dhcp.Dhcp6RelayOption) ConnectPoint(org.onosproject.net.ConnectPoint) ArrayList(java.util.ArrayList) Ethernet(org.onlab.packet.Ethernet) DhcpRelayCounters(org.onosproject.dhcprelay.store.DhcpRelayCounters) IpAddress(org.onlab.packet.IpAddress) BasePacket(org.onlab.packet.BasePacket) DhcpServerInfo(org.onosproject.dhcprelay.api.DhcpServerInfo) Ip6Address(org.onlab.packet.Ip6Address) Logger(org.slf4j.Logger) VlanId(org.onlab.packet.VlanId) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Dhcp6Option(org.onlab.packet.dhcp.Dhcp6Option) Set(java.util.Set) MsgType(org.onlab.packet.DHCP6.MsgType) InterfaceIpAddress(org.onosproject.net.host.InterfaceIpAddress) UDP(org.onlab.packet.UDP) IPv6(org.onlab.packet.IPv6) DHCP6(org.onlab.packet.DHCP6) List(java.util.List) HexString(org.onlab.util.HexString) PacketContext(org.onosproject.net.packet.PacketContext) MacAddress(org.onlab.packet.MacAddress) DeviceId(org.onosproject.net.DeviceId) IPv6(org.onlab.packet.IPv6) DeviceId(org.onosproject.net.DeviceId) Host(org.onosproject.net.Host) HexString(org.onlab.util.HexString) MacAddress(org.onlab.packet.MacAddress) ConnectPoint(org.onosproject.net.ConnectPoint) Ip6Address(org.onlab.packet.Ip6Address) Ethernet(org.onlab.packet.Ethernet) HostLocation(org.onosproject.net.HostLocation) DHCP6(org.onlab.packet.DHCP6) DhcpServerInfo(org.onosproject.dhcprelay.api.DhcpServerInfo) VlanId(org.onlab.packet.VlanId) Interface(org.onosproject.net.intf.Interface)

Example 12 with UDP

use of org.onlab.packet.UDP in project onos by opennetworkinglab.

the class Dhcp6HandlerUtil method buildDhcp6PacketFromClient.

/**
 * build the DHCP6 solicit/request packet with gatewayip.
 *
 * @param context packet context
 * @param clientPacket client ethernet packet
 * @param clientInterfaces set of client side interfaces
 * @param serverInfo target server which a packet is generated for
 * @param serverInterface target server interface
 * @return ethernet packet with dhcp6 packet info
 */
public static Ethernet buildDhcp6PacketFromClient(PacketContext context, Ethernet clientPacket, Set<Interface> clientInterfaces, DhcpServerInfo serverInfo, Interface serverInterface) {
    ConnectPoint receivedFrom = context.inPacket().receivedFrom();
    DeviceId receivedFromDevice = receivedFrom.deviceId();
    Ip6Address relayAgentIp = getRelayAgentIPv6Address(clientInterfaces);
    MacAddress relayAgentMac = clientInterfaces.iterator().next().mac();
    if (relayAgentIp == null || relayAgentMac == null) {
        log.warn("Missing DHCP relay agent interface Ipv6 addr config for " + "packet from client on port: {}. Aborting packet processing", clientInterfaces.iterator().next().connectPoint());
        return null;
    }
    IPv6 clientIpv6 = (IPv6) clientPacket.getPayload();
    UDP clientUdp = (UDP) clientIpv6.getPayload();
    DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload();
    boolean directConnFlag = directlyConnected(clientDhcp6);
    Ip6Address serverIpFacing = getFirstIpFromInterface(serverInterface);
    if (serverIpFacing == null || serverInterface.mac() == null) {
        log.warn("No IP v6 address for server Interface {}", serverInterface);
        return null;
    }
    Ethernet etherReply = clientPacket.duplicate();
    etherReply.setSourceMACAddress(serverInterface.mac());
    // set default info and replace with indirect if available later on.
    if (serverInfo.getDhcpConnectMac().isPresent()) {
        etherReply.setDestinationMACAddress(serverInfo.getDhcpConnectMac().get());
    }
    if (serverInfo.getDhcpConnectVlan().isPresent()) {
        etherReply.setVlanID(serverInfo.getDhcpConnectVlan().get().toShort());
    }
    IPv6 ipv6Packet = (IPv6) etherReply.getPayload();
    byte[] peerAddress = clientIpv6.getSourceAddress();
    ipv6Packet.setSourceAddress(serverIpFacing.toOctets());
    ipv6Packet.setDestinationAddress(serverInfo.getDhcpServerIp6().get().toOctets());
    UDP udpPacket = (UDP) ipv6Packet.getPayload();
    udpPacket.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
    DHCP6 dhcp6Packet = (DHCP6) udpPacket.getPayload();
    byte[] dhcp6PacketByte = dhcp6Packet.serialize();
    DHCP6 dhcp6Relay = new DHCP6();
    dhcp6Relay.setMsgType(DHCP6.MsgType.RELAY_FORW.value());
    if (directConnFlag) {
        dhcp6Relay.setLinkAddress(relayAgentIp.toOctets());
    } else {
        if (isServerIpEmpty(serverInfo)) {
            log.warn("indirect DhcpServerIp empty... use default server ");
        } else {
            // Check if mac is obtained for valid server ip
            if (isConnectMacEmpty(serverInfo, clientInterfaces)) {
                log.warn("indirect Dhcp ConnectMac empty ...");
                return null;
            }
            etherReply.setDestinationMACAddress(serverInfo.getDhcpConnectMac().get());
            etherReply.setVlanID(serverInfo.getDhcpConnectVlan().get().toShort());
            ipv6Packet.setDestinationAddress(serverInfo.getDhcpServerIp6().get().toOctets());
        }
        if (!serverInfo.getRelayAgentIp6(receivedFromDevice).isPresent()) {
            log.debug("indirect connection: relayAgentIp NOT availale from config file! Use dynamic. {}", HexString.toHexString(relayAgentIp.toOctets(), ":"));
            serverIpFacing = relayAgentIp;
        } else {
            serverIpFacing = serverInfo.getRelayAgentIp6(receivedFromDevice).get();
        }
        log.debug("Source IP address set as relay agent IP with value: {}", serverIpFacing);
        dhcp6Relay.setLinkAddress(serverIpFacing.toOctets());
        ipv6Packet.setSourceAddress(serverIpFacing.toOctets());
    }
    // peer address: address of the client or relay agent from which the message to be relayed was received.
    dhcp6Relay.setPeerAddress(peerAddress);
    // directly connected case, hop count is zero; otherwise, hop count + 1
    if (directConnFlag) {
        dhcp6Relay.setHopCount((byte) 0);
    } else {
        dhcp6Relay.setHopCount((byte) (dhcp6Packet.getHopCount() + 1));
    }
    List<Dhcp6Option> options = new ArrayList<>();
    addDhcp6OptionsFromClient(options, dhcp6PacketByte, context, clientPacket);
    dhcp6Relay.setOptions(options);
    udpPacket.setPayload(dhcp6Relay);
    udpPacket.resetChecksum();
    ipv6Packet.setPayload(udpPacket);
    ipv6Packet.setHopLimit((byte) 64);
    etherReply.setPayload(ipv6Packet);
    return etherReply;
}
Also used : UDP(org.onlab.packet.UDP) IPv6(org.onlab.packet.IPv6) DeviceId(org.onosproject.net.DeviceId) ArrayList(java.util.ArrayList) MacAddress(org.onlab.packet.MacAddress) ConnectPoint(org.onosproject.net.ConnectPoint) Ip6Address(org.onlab.packet.Ip6Address) Dhcp6Option(org.onlab.packet.dhcp.Dhcp6Option) Ethernet(org.onlab.packet.Ethernet) DHCP6(org.onlab.packet.DHCP6)

Example 13 with UDP

use of org.onlab.packet.UDP in project onos by opennetworkinglab.

the class OpenstackRoutingSnatHandler method buildPacketOutUdp.

private UDP buildPacketOutUdp(IPv4 iPacket, int patPort) {
    UDP udpPacket = (UDP) iPacket.getPayload();
    udpPacket.setSourcePort(patPort);
    udpPacket.resetChecksum();
    udpPacket.setParent(iPacket);
    return udpPacket;
}
Also used : UDP(org.onlab.packet.UDP)

Example 14 with UDP

use of org.onlab.packet.UDP in project onos by opennetworkinglab.

the class OpenstackRoutingSnatHandler method setStatelessSnatUpstreamRules.

private void setStatelessSnatUpstreamRules(String segmentId, Type networkType, IpAddress externalIp, ExternalPeerRouter externalPeerRouter, TpPort patPort, InboundPacket packetIn) {
    IPv4 iPacket = (IPv4) packetIn.parsed().getPayload();
    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPProtocol(iPacket.getProtocol()).matchIPSrc(IpPrefix.valueOf(iPacket.getSourceAddress(), VM_PREFIX)).matchIPDst(IpPrefix.valueOf(iPacket.getDestinationAddress(), VM_PREFIX));
    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
    switch(networkType) {
        case VXLAN:
        case GRE:
        case GENEVE:
            sBuilder.matchTunnelId(Long.parseLong(segmentId));
            break;
        case VLAN:
            sBuilder.matchVlanId(VlanId.vlanId(segmentId));
            tBuilder.popVlan();
            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.getSourcePort())).matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
            tBuilder.setTcpSrc(patPort).setEthDst(externalPeerRouter.macAddress());
            break;
        case IPv4.PROTOCOL_UDP:
            UDP udpPacket = (UDP) iPacket.getPayload();
            sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())).matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
            tBuilder.setUdpSrc(patPort).setEthDst(externalPeerRouter.macAddress());
            break;
        default:
            log.debug("Unsupported IPv4 protocol {}");
            break;
    }
    if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
        tBuilder.pushVlan().setVlanId(externalPeerRouter.vlanId());
    }
    tBuilder.setIpSrc(externalIp);
    osNodeService.completeNodes(GATEWAY).forEach(gNode -> {
        TrafficTreatment.Builder tmpBuilder = DefaultTrafficTreatment.builder(tBuilder.build());
        tmpBuilder.setOutput(gNode.uplinkPortNum());
        osFlowRuleService.setRule(appId, gNode.intgBridge(), sBuilder.build(), tmpBuilder.build(), PRIORITY_SNAT_RULE, GW_COMMON_TABLE, true);
    });
}
Also used : TCP(org.onlab.packet.TCP) UDP(org.onlab.packet.UDP) IPv4(org.onlab.packet.IPv4) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment)

Example 15 with UDP

use of org.onlab.packet.UDP in project onos by opennetworkinglab.

the class Dhcp6RelayTest method serializeRequest.

/**
 * Test serialize relay message with request message.
 *
 * @throws Exception exception while serialize the DHCPv6 payload
 */
@Test
public void serializeRequest() throws Exception {
    DHCP6 relayMsg = new DHCP6();
    relayMsg.setMsgType(DHCP6.MsgType.RELAY_FORW.value());
    relayMsg.setHopCount((byte) HOP_COUNT);
    relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
    relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());
    DHCP6 relaiedDhcp6 = new DHCP6();
    relaiedDhcp6.setMsgType(DHCP6.MsgType.REQUEST.value());
    relaiedDhcp6.setTransactionId(XID_2);
    List<Dhcp6Option> options = Lists.newArrayList();
    // Client ID
    Dhcp6Duid duid = new Dhcp6Duid();
    duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    duid.setHardwareType((short) 1);
    duid.setDuidTime(CLIENT_DUID_TIME);
    duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
    Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
    clientIdOption.setDuid(duid);
    options.add(clientIdOption);
    // Server ID
    Dhcp6Option option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.SERVERID.value());
    option.setLength((short) 14);
    Dhcp6Duid serverDuid = new Dhcp6Duid();
    serverDuid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
    serverDuid.setLinkLayerAddress(SERVER_MAC.toBytes());
    serverDuid.setHardwareType((short) 1);
    serverDuid.setDuidTime(0x211e5340);
    option.setData(serverDuid.serialize());
    options.add(option);
    // Option request
    option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.ORO.value());
    option.setLength((short) 8);
    option.setData(new byte[] { 0, 23, 0, 24, 0, 39, 0, 31 });
    options.add(option);
    // Elapsed Time
    option = new Dhcp6Option();
    option.setCode(DHCP6.OptionCode.ELAPSED_TIME.value());
    option.setLength((short) 2);
    option.setData(new byte[] { 0, 0 });
    options.add(option);
    // IA address
    Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
    iaAddressOption.setIp6Address(IA_ADDRESS);
    iaAddressOption.setPreferredLifetime(PREFFERRED_LT_REQ);
    iaAddressOption.setValidLifetime(VALID_LT_REQ_2);
    // IA NA
    Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
    iaNaOption.setIaId(IA_ID);
    iaNaOption.setT1(T1_CLIENT);
    iaNaOption.setT2(T2_CLIENT);
    iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
    options.add(iaNaOption);
    relaiedDhcp6.setOptions(options);
    Dhcp6Option subscriberId = new Dhcp6Option();
    subscriberId.setCode(DHCP6.OptionCode.SUBSCRIBER_ID.value());
    subscriberId.setLength((short) 10);
    subscriberId.setData(SERVER_IP.toString().getBytes(US_ASCII));
    Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
    relayOption.setPayload(relaiedDhcp6);
    relayMsg.setOptions(ImmutableList.of(subscriberId, relayOption));
    UDP udp = new UDP();
    udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
    udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
    udp.setPayload(relayMsg);
    udp.setChecksum((short) 0x9aab);
    IPv6 ipv6 = new IPv6();
    ipv6.setHopLimit((byte) 32);
    ipv6.setSourceAddress(DOWNSTREAM_LL.toOctets());
    ipv6.setDestinationAddress(DHCP6_BRC.toOctets());
    ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
    ipv6.setTrafficClass((byte) 0);
    ipv6.setFlowLabel(0x000cbf64);
    ipv6.setPayload(udp);
    Ethernet eth = new Ethernet();
    eth.setDestinationMACAddress(IPV6_MCAST);
    eth.setSourceMACAddress(DOWNSTREAM_MAC);
    eth.setEtherType(Ethernet.TYPE_IPV6);
    eth.setPayload(ipv6);
    assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(REQUEST)), eth.serialize());
}
Also used : UDP(org.onlab.packet.UDP) IPv6(org.onlab.packet.IPv6) Ethernet(org.onlab.packet.Ethernet) DHCP6(org.onlab.packet.DHCP6) Test(org.junit.Test)

Aggregations

UDP (org.onlab.packet.UDP)46 Ethernet (org.onlab.packet.Ethernet)36 IPv4 (org.onlab.packet.IPv4)24 DHCP (org.onlab.packet.DHCP)19 IPv6 (org.onlab.packet.IPv6)17 DHCP6 (org.onlab.packet.DHCP6)16 ConnectPoint (org.onosproject.net.ConnectPoint)12 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 MacAddress (org.onlab.packet.MacAddress)10 Interface (org.onosproject.net.intf.Interface)9 VlanId (org.onlab.packet.VlanId)8 DhcpOption (org.onlab.packet.dhcp.DhcpOption)8 DhcpServerInfo (org.onosproject.dhcprelay.api.DhcpServerInfo)8 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)8 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)8 IpAddress (org.onlab.packet.IpAddress)7 DeviceId (org.onosproject.net.DeviceId)7 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)7 TrafficSelector (org.onosproject.net.flow.TrafficSelector)7