Example #1
0
bool MacAddress::operator<(const MacAddress &b) const {
    for (size_t i = 0; i < getMacArray().size(); ++i) {
        if (getMacArray().at(i) != b.getMacArray().at(i)) {
            return getMacArray().at(i) < b.getMacArray().at(i);
        }
    }
    return false;
}
Example #2
0
int RawSocketDevice::sendPackets(const RawPacketVector& packetVec)
{
#if defined(WIN32) || defined(WINx64) || defined(PCAPPP_MINGW_ENV)

	LOG_ERROR("Sending packets with raw socket are not supported on Windows");
	return false;

#elif LINUX

	if (!isOpened())
	{
		LOG_ERROR("Device is not open");
		return 0;
	}

	int fd = ((SocketContainer*)m_Socket)->fd;

	sockaddr_ll addr;
	memset(&addr, 0, sizeof(struct sockaddr_ll));
	addr.sll_family = htons(PF_PACKET);
	addr.sll_protocol = htons(ETH_P_ALL);
	addr.sll_halen = 6;
	addr.sll_ifindex = ((SocketContainer*)m_Socket)->interfaceIndex;

	int sendCount = 0;

	for (RawPacketVector::ConstVectorIterator iter = packetVec.begin(); iter != packetVec.end(); iter++)
	{
		Packet packet(*iter, OsiModelDataLinkLayer);
		if (!packet.isPacketOfType(pcpp::Ethernet))
		{
			LOG_DEBUG("Can't send non-Ethernet packets");
			continue;
		}

		EthLayer* ethLayer = packet.getLayerOfType<EthLayer>();
		MacAddress dstMac = ethLayer->getDestMac();
		dstMac.copyTo((uint8_t*)&(addr.sll_addr));

		if (::sendto(fd, (*iter)->getRawData(), (*iter)->getRawDataLen(), 0, (struct sockaddr*)&addr, sizeof(addr)) == -1)
		{
			LOG_DEBUG("Failed to send packet. Error was: '%s'", strerror(errno));
			continue;
		}

		sendCount++;
	}

	return sendCount;

#else

	LOG_ERROR("Raw socket are not supported on this platform");
	return false;

#endif
}
Example #3
0
EthLayer::EthLayer(MacAddress& sourceMac, MacAddress& destMac, uint16_t etherType) : Layer()
{
	m_DataLen = sizeof(ether_header);
	m_Data = new uint8_t[m_DataLen];
	memset(m_Data, 0, m_DataLen);
	ether_header* ethHdr = (ether_header*)m_Data;
	destMac.copyTo(ethHdr->dstMac);
	sourceMac.copyTo(ethHdr->srcMac);
	ethHdr->etherType = htons(etherType);
	m_Protocol = Ethernet;
}
Example #4
0
bool SllLayer::setMacAddressAsLinkLayer(MacAddress macAddr)
{
	if (!macAddr.isValid())
	{
		LOG_ERROR("MAC address is not valid");
		return false;
	}

	uint8_t macAddrAsArr[6];
	macAddr.copyTo(macAddrAsArr);
	return setLinkLayerAddr(macAddrAsArr, 6);
}
Example #5
0
ArpLayer::ArpLayer(ArpOpcode opCode, const MacAddress& senderMacAddr, const MacAddress& targetMacAddr, const IPv4Address senderIpAddr, const IPv4Address& targetIpAddr)
{
	m_DataLen = sizeof(arphdr);
	m_Data = new uint8_t[m_DataLen];
	memset(m_Data, 0, sizeof(m_DataLen));
	m_Protocol = ARP;

	arphdr* arpHeader = getArpHeader();
	arpHeader->opcode = htons(opCode);
	targetMacAddr.copyTo(arpHeader->targetMacAddr);
	senderMacAddr.copyTo(arpHeader->senderMacAddr);
	arpHeader->targetIpAddr = targetIpAddr.toInt();
	arpHeader->senderIpAddr = senderIpAddr.toInt();
}
Example #6
0
bool doArpSpoofing(PcapLiveDevice* pDevice, const IPv4Address& gatewayAddr, const IPv4Address& victimAddr)
{
	// Get the gateway MAC address
	MacAddress gatewayMacAddr = getMacAddress(gatewayAddr, pDevice);
	if (!gatewayMacAddr.isValid())
	{
		printf("Failed to find gateway MAC address. Exiting...\n");
		return false;
	}
	printf("Got gateway MAC address: %s\n", gatewayMacAddr.toString().c_str());

	// Get the victim MAC address
	MacAddress victimMacAddr = getMacAddress(victimAddr, pDevice);
	if (!victimMacAddr.isValid())
	{
		printf("Failed to find victim MAC address. Exiting...\n");
		return false;
	}
	printf("Got victim MAC address: %s\n", victimMacAddr.toString().c_str());

	MacAddress deviceMacAddress = pDevice->getMacAddress();

	// Create ARP reply for the gateway
	Packet gwArpReply(500);
	EthLayer gwEthLayer(deviceMacAddress, gatewayMacAddr, (uint16_t)PCPP_ETHERTYPE_ARP);
	ArpLayer gwArpLayer(ARP_REPLY,
						pDevice->getMacAddress(),
						gatewayMacAddr,
						victimAddr,
						gatewayAddr);
	gwArpReply.addLayer(&gwEthLayer);
	gwArpReply.addLayer(&gwArpLayer);
	gwArpReply.computeCalculateFields();

	// Create ARP reply for the victim
	Packet victimArpReply(500);
	EthLayer victimEthLayer(deviceMacAddress, victimMacAddr, (uint16_t)PCPP_ETHERTYPE_ARP);
	ArpLayer victimArpLayer(ARP_REPLY,
							pDevice->getMacAddress(),
							victimMacAddr,
							gatewayAddr,
							victimAddr);
	victimArpReply.addLayer(&victimEthLayer);
	victimArpReply.addLayer(&victimArpLayer);
	victimArpReply.computeCalculateFields();

	// Send ARP replies to gateway and to victim every 5 seconds
	printf("Sending ARP replies to victim and to gateway every 5 seconds...\n\n");
	while(true)
	{
		pDevice->sendPacket(&gwArpReply);
		printf("Sent ARP reply: %s [gateway] is at MAC address %s [me]\n", gatewayAddr.toString().c_str(), deviceMacAddress.toString().c_str());
		pDevice->sendPacket(&victimArpReply);
		printf("Sent ARP reply: %s [victim] is at MAC address %s [me]\n\n", victimAddr.toString().c_str(), deviceMacAddress.toString().c_str());
		PCAP_SLEEP(5);
	}

	return true;
}
void DescribePacket(const u_int8_t* data, const char* io)
{
    struct ether_header* header = (struct ether_header*)data;
    unsigned int type = ntohs(header->ether_type);
    char summary[256];
    MacAddress mac;

    switch (type)
    {
        case ETHERTYPE_IP:
        case ETHERTYPE_ARP:
            if (type == ETHERTYPE_IP)
            {
                GetIPv4PacketSummary(data, summary);
                log_info("%s IPv4: %s", io, summary);
            }
            else
            {
                struct ether_arp* arpreq = (struct ether_arp*)(data + sizeof(struct ether_header));
                mac = MacAddress::CopyFrom(arpreq->arp_sha);
                log_info("%s ARP: op: %d - sender: %s", io, ntohs(arpreq->arp_op), mac.ToString().c_str());
                char srcip[INET_ADDRSTRLEN], dstip[INET_ADDRSTRLEN];
                strcpy(srcip, inet_ntoa(*(in_addr*)arpreq->arp_spa));
                strcpy(dstip, inet_ntoa(*(in_addr*)arpreq->arp_tpa));
                log_info("       For: %s->%s", srcip, dstip);
            }
            break;
        case ETHERTYPE_IPV6:
            GetIPv6PacketSummary(data, summary);
            log_info("%s IPv6: %s", io, summary);
            break;
        default:
            log_info("%s 0x%X packet", io, type);
            break;
    }
    
    mac = MacAddress::CopyFrom(header->ether_shost);
    log_info("    Source: %s", mac.ToString().c_str());
    mac = MacAddress::CopyFrom(header->ether_dhost);
    log_info("    Target: %s", mac.ToString().c_str());

    printf("\n");
}
Example #8
0
IPAddressV6::AddressStorage::AddressStorage(MacAddress mac) {
    // The link-local address uses modified EUI-64 format,
    // See RFC 4291 sections 2.5.1, 2.5.6, and Appendix A
    const auto* macBytes = mac.bytes();
    memcpy(&bytes_.front(), "\xfe\x80\x00\x00\x00\x00\x00\x00", 8);
    bytes_[8] = macBytes[0] ^ 0x02;
    bytes_[9] = macBytes[1];
    bytes_[10] = macBytes[2];
    bytes_[11] = 0xff;
    bytes_[12] = 0xfe;
    bytes_[13] = macBytes[3];
    bytes_[14] = macBytes[4];
    bytes_[15] = macBytes[5];
}
Example #9
0
bool VlanMgr::addHostVlan(int vlan_id)
{
    SWSS_LOG_ENTER();

    // The command should be generated as:
    // /bin/bash -c "/sbin/bridge vlan add vid {{vlan_id}} dev Bridge self &&
    //               /sbin/ip link add link Bridge up name Vlan{{vlan_id}} address {{gMacAddress}} type vlan id {{vlan_id}}"
    const std::string cmds = std::string("")
      + BASH_CMD + " -c \""
      + BRIDGE_CMD + " vlan add vid " + std::to_string(vlan_id) + " dev " + DOT1Q_BRIDGE_NAME + " self && "
      + IP_CMD + " link add link " + DOT1Q_BRIDGE_NAME
               + " up"
               + " name " + VLAN_PREFIX + std::to_string(vlan_id)
               + " address " + gMacAddress.to_string()
               + " type vlan id " + std::to_string(vlan_id) + "\"";

    std::string res;
    EXEC_WITH_ERROR_THROW(cmds, res);

    return true;
}
	/// Write-only accessor to _dstMac.
	void setDstMAC(const MacAddress& newVal) { _dstMAC = newVal.addr2str();	_updateTemplate(); }
Example #11
0
    static void SetUpTestCase(void)
    {
        mac = MacAddress("00:F2:F2:F2:F2:F2");

        LOGG(TEST_INFO, FRAMEWORK, "init mac %s\n", mac.to_string().c_str());

        LOGG(TEST_INFO, FRAMEWORK, "sai_api_initialize\n");
        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_initialize(0, (service_method_table_t *)&test_services));

        LOGG(TEST_INFO, FRAMEWORK, "sai_api_query SAI_API_SWITCH, SAI_API_PORT, ...\n");
        //query API methods of all types
        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_SWITCH, (void**)&sai_switch_api));
        ASSERT_TRUE(sai_switch_api != NULL);

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_PORT, (void**)&sai_port_api));
        ASSERT_TRUE(sai_port_api != NULL);

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_VLAN, (void**)&sai_vlan_api));
        ASSERT_TRUE(sai_vlan_api != NULL);

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_VIRTUAL_ROUTER, (void**)&sai_vr_api));
        ASSERT_TRUE(sai_vr_api != NULL);

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_ROUTER_INTERFACE, (void**)&sai_rif_api));
        ASSERT_TRUE(sai_rif_api != NULL);

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_HOST_INTERFACE, (void**)&sai_hif_api));
        ASSERT_TRUE(sai_hif_api != NULL);

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_NEIGHBOR, (void**)&sai_neighbor_api));
        ASSERT_TRUE(sai_neighbor_api != NULL);
        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_ROUTE, (void**)&sai_route_api));
        ASSERT_TRUE(sai_route_api != NULL);

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_NEXT_HOP, (void**)&sai_next_hop_api));
        ASSERT_TRUE(sai_next_hop_api != NULL);

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_NEXT_HOP_GROUP,
                                (void**)&sai_next_hop_group_api));
        ASSERT_TRUE(sai_next_hop_group_api != NULL);

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_api_query(SAI_API_FDB,
                                (void**)&sai_fdb_api));
        ASSERT_TRUE(sai_fdb_api != NULL);


        LOGG(TEST_INFO, FRAMEWORK, "sai_log_set SAI_API_SWITCH, SAI_API_PORT, ...\n");
        //set log
        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_SWITCH, SAI_LOG_DEBUG));

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_PORT, SAI_LOG_DEBUG));

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_VLAN, SAI_LOG_DEBUG));

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_VIRTUAL_ROUTER, SAI_LOG_DEBUG));

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_ROUTER_INTERFACE, SAI_LOG_DEBUG));


        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_HOST_INTERFACE, SAI_LOG_DEBUG));

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_NEIGHBOR, SAI_LOG_DEBUG));

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_ROUTE, SAI_LOG_DEBUG));

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_NEXT_HOP, SAI_LOG_DEBUG));

        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_log_set(SAI_API_NEXT_HOP_GROUP, SAI_LOG_DEBUG));


        LOGG(TEST_INFO, FRAMEWORK, "sai_switch_api->initialize_switch \n");
        ASSERT_TRUE(sai_switch_api->initialize_switch);
        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_switch_api->initialize_switch(0, "0xb850", "",
                          &plat_switch_notification_handlers));


        sai_attribute_t     attr;

        attr.id = SAI_SWITCH_ATTR_SRC_MAC_ADDRESS;
        memcpy(attr.value.mac, mac.to_bytes(), 6);
        ASSERT_TRUE(sai_switch_api->set_switch_attribute != NULL);

        LOGG(TEST_INFO, FRAMEWORK, "sai_switch_api->set_switch_attribute SAI_SWITCH_ATTR_SRC_MAC_ADDRESS %s\n",
             mac.to_string().c_str());
        ASSERT_EQ(SAI_STATUS_SUCCESS,
                  sai_switch_api->set_switch_attribute(&attr));

        LOGG(TEST_INFO, FRAMEWORK, "Create neighbor_mgr, nexthopgrp_mgr and route_mgr\n");

        nexthop_mgr = new NextHopMgr();
        neighbor_mgr = new NeighborMgr(nexthop_mgr);
        nexthopgrp_mgr = new NextHopGrpMgr(neighbor_mgr);
        route_mgr = new RouteMgr(neighbor_mgr, nexthopgrp_mgr);
        fdb_mgr = new FdbMgr();


        LOGG(TEST_INFO, FRAMEWORK, "Set Up L3 Interfaces\n");
        ASSERT_EQ(true, basic_router_setup());
    }
Example #12
0
//L3 Interface Initialization
static bool setup_one_l3_interface(sai_vlan_id_t vlanid,
                                   int port_count,
                                   const sai_object_id_t *port_list,
                                   const MacAddress mac,
                                   const IpAddress ipaddr,
                                   const IpAddress ipmask,
                                   sai_object_id_t &rif_id)
{

    LOGG(TEST_INFO, SETL3, "sai_vlan_api->create_vlan, create vlan %hu.\n", vlanid);
    sai_status_t status = sai_vlan_api->create_vlan(vlanid);

    if (status != SAI_STATUS_SUCCESS && status != SAI_STATUS_ITEM_ALREADY_EXISTS)
    {
        LOGG(TEST_ERR, SETL3, "fail to create vlan %hu. status=0x%x\n", vlanid, -status);
        return false;
    }

    std::vector<sai_vlan_port_t> vlan_port_list;

    for (int i = 0; i < port_count; ++i)
    {
        sai_vlan_port_t vlan_port;
        vlan_port.port_id = port_list[i];
        vlan_port.tagging_mode = SAI_VLAN_PORT_UNTAGGED;
        vlan_port_list.push_back(vlan_port);
    }

    LOGG(TEST_INFO, SETL3, "sai_vlan_api->add_ports_to_vlan, add ports to vlan %d.\n", vlanid);
    status = sai_vlan_api->add_ports_to_vlan(vlanid, port_count, vlan_port_list.data());

    if (status != SAI_STATUS_SUCCESS)
    {
        LOGG(TEST_ERR, SETL3, "fail to add ports to vlan %hu. status=0x%x\n",  vlanid, -status);
        return false;
    }

    sai_attribute_t attr;
    attr.id = SAI_PORT_ATTR_PORT_VLAN_ID;
    attr.value.u16 = vlanid;

    for (int i = 0; i < port_count; ++i)
    {
        LOGG(TEST_INFO, SETL3, "sai_port_api->set_port_attribute SAI_PORT_ATTR_PORT_VLAN_ID %hu to port 0x%lx\n",
             vlanid,  port_list[i]);
        status = sai_port_api->set_port_attribute(port_list[i], &attr);

        if (status != SAI_STATUS_SUCCESS)
        {
            LOGG(TEST_ERR, SETL3, "fail to set port %lu untagged vlan %hu. status=0x%x\n", port_list[i], vlanid, -status);
            return false;
        }
    }

    // create router interface
    std::vector<sai_attribute_t> rif_attrs;
    sai_attribute_t rif_attr;

    rif_attr.id = SAI_ROUTER_INTERFACE_ATTR_VIRTUAL_ROUTER_ID;
    rif_attr.value.oid = g_vr_id;
    rif_attrs.push_back(rif_attr);

    rif_attr.id = SAI_ROUTER_INTERFACE_ATTR_TYPE;
    rif_attr.value.s32 = SAI_ROUTER_INTERFACE_TYPE_VLAN;
    rif_attrs.push_back(rif_attr);

    rif_attr.id = SAI_ROUTER_INTERFACE_ATTR_SRC_MAC_ADDRESS;
    memcpy(rif_attr.value.mac, mac.to_bytes(), sizeof(sai_mac_t));
    rif_attrs.push_back(rif_attr);

    rif_attr.id = SAI_ROUTER_INTERFACE_ATTR_VLAN_ID;
    rif_attr.value.u16 = vlanid;
    rif_attrs.push_back(rif_attr);

    LOGG(TEST_INFO, SETL3, "sai_rif_api->create_router_interface\n");
    status = sai_rif_api->create_router_interface(&rif_id, rif_attrs.size(), rif_attrs.data());

    if (status != SAI_STATUS_SUCCESS)
    {
        LOGG(TEST_ERR, SETL3, "fail to create router interface. status=0x%x\n", -status);
        return false;
    }

    if (!SAI_OID_TYPE_CHECK(rif_id, SAI_OBJECT_TYPE_ROUTER_INTERFACE))
    {
        LOGG(TEST_ERR, SETL3, "router interface oid generated is not the right type\n");
        return false;
    }

    LOGG(TEST_DEBUG, SETL3, "router_interface created, rif_id 0x%lx\n", rif_id);

    // add interface ip to l3 host table
    LOGG(TEST_INFO, SETL3, "sai_route_api->create_route, SAI_ROUTE_ATTR_PACKET_ACTION, SAI_PACKET_ACTION_TRAP\n");
    sai_unicast_route_entry_t unicast_route_entry;
    unicast_route_entry.vr_id = g_vr_id;
    unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
    unicast_route_entry.destination.addr.ip4 = ipaddr.addr();
    unicast_route_entry.destination.mask.ip4 = 0xffffffff;
    sai_attribute_t route_attr;
    route_attr.id = SAI_ROUTE_ATTR_PACKET_ACTION;
    route_attr.value.s32 = SAI_PACKET_ACTION_TRAP;
    status = sai_route_api->create_route(&unicast_route_entry, 1, &route_attr);

    if (status != SAI_STATUS_SUCCESS)
    {
        LOGG(TEST_ERR, SETL3, "fail to add route for l3 interface to cpu. status=0x%x\n", -status);
        return false;
    }

    // by default, drop all the traffic destined to the the ip subnet.
    // if we learn some of the neighbors, add them explicitly to the l3 host table.
    LOGG(TEST_INFO, SETL3, "sai_route_api->create_route, SAI_ROUTE_ATTR_PACKET_ACTION, SAI_PACKET_ACTION_DROP\n");
    unicast_route_entry.vr_id = g_vr_id;
    unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
    unicast_route_entry.destination.addr.ip4 = ipaddr.addr() & ipmask.addr();
    unicast_route_entry.destination.mask.ip4 = ipmask.addr();
    route_attr.id = SAI_ROUTE_ATTR_PACKET_ACTION;
    route_attr.value.s32 = SAI_PACKET_ACTION_DROP;
    status = sai_route_api->create_route(&unicast_route_entry, 1, &route_attr);

    if (status != SAI_STATUS_SUCCESS)
    {
        LOGG(TEST_ERR, SETL3, "fail to add l3 intf subnet to blackhole. status=0x%x", -status);
        return false;
    }

    return true;
}
Example #13
0
void SetMacAddress(const MacAddress& addr) {
    std::memcpy(shared_page.wifi_macaddr, addr.data(), sizeof(MacAddress));
}