示例#1
0
文件: Vlan.cpp 项目: Gardenya/fboss
VlanFields VlanFields::fromFollyDynamic(const folly::dynamic& vlanJson) {
  VlanFields vlan(VlanID(vlanJson[kVlanId].asInt()),
      vlanJson[kVlanName].asString().toStdString());
  vlan.mtu = vlanJson[kVlanMtu].asInt();
  vlan.dhcpV4Relay = folly::IPAddressV4(
      vlanJson[kDhcpV4Relay].stringPiece());
  vlan.dhcpV6Relay = folly::IPAddressV6(
      vlanJson[kDhcpV6Relay].stringPiece());
  for (const auto& o: vlanJson[kDhcpV4RelayOverrides].items()) {
    vlan.dhcpRelayOverridesV4[MacAddress(o.first.asString().toStdString())] =
        folly::IPAddressV4(o.second.stringPiece());
  }
  for (const auto& o: vlanJson[kDhcpV6RelayOverrides].items()) {
    vlan.dhcpRelayOverridesV6[MacAddress(o.first.asString().toStdString())] =
        folly::IPAddressV6(o.second.stringPiece());
  }
  for (const auto& portInfo: vlanJson[kMemberPorts].items()) {
    vlan.ports.emplace(PortID(to<uint16_t>(portInfo.first.asString())),
          PortInfo::fromFollyDynamic(portInfo.second));
  }
  vlan.arpTable = ArpTable::fromFollyDynamic(vlanJson[kArpTable]);
  vlan.ndpTable = NdpTable::fromFollyDynamic(vlanJson[kNdpTable]);
  vlan.arpResponseTable = ArpResponseTable::fromFollyDynamic(
      vlanJson[kArpResponseTable]);
  vlan.ndpResponseTable = NdpResponseTable::fromFollyDynamic(
      vlanJson[kNdpResponseTable]);
  return vlan;
}
bool ThriftConfigApplier::updateDhcpOverrides(Vlan* vlan,
        const cfg::Vlan* config) {
    DhcpV4OverrideMap newDhcpV4OverrideMap;
    for (const auto& pair : config->dhcpRelayOverridesV4) {
        try {
            newDhcpV4OverrideMap[MacAddress(pair.first)] = IPAddressV4(pair.second);
        } catch (const IPAddressFormatException& ex) {
            throw FbossError("Invalid IPv4 address in DHCPv4 relay override map: ",
                             ex.what());
        }
    }

    DhcpV6OverrideMap newDhcpV6OverrideMap;
    for (const auto& pair : config->dhcpRelayOverridesV6) {
        try {
            newDhcpV6OverrideMap[MacAddress(pair.first)] = IPAddressV6(pair.second);
        } catch (const IPAddressFormatException& ex) {
            throw FbossError("Invalid IPv4 address in DHCPv4 relay override map: ",
                             ex.what());
        }
    }

    bool changed = false;
    auto oldDhcpV4OverrideMap = vlan->getDhcpV4RelayOverrides();
    if (oldDhcpV4OverrideMap != newDhcpV4OverrideMap) {
        vlan->setDhcpV4RelayOverrides(newDhcpV4OverrideMap);
        changed = true;
    }
    auto oldDhcpV6OverrideMap = vlan->getDhcpV6RelayOverrides();
    if (oldDhcpV6OverrideMap != newDhcpV6OverrideMap) {
        vlan->setDhcpV6RelayOverrides(newDhcpV6OverrideMap);
        changed = true;
    }
    return changed;
}
示例#3
0
void IPv6RAImpl::initPacket(const Interface* intf) {
  auto totalLength = IPv6RouteAdvertiser::getPacketSize(intf);
  buf_ = IOBuf(IOBuf::CREATE, totalLength);
  buf_.append(totalLength);
  RWPrivateCursor cursor(&buf_);
  IPv6RouteAdvertiser::createAdvertisementPacket(
    intf, &cursor, MacAddress("33:33:00:00:00:01"), IPAddressV6("ff02::1"));
}
示例#4
0
	MacAddress MacAddress::lookup_from_ip_with_arp_table(const IpAddress& ipAddress,
		bool ping) {

	  if(!ipAddress.isValid()) {
		return MacAddress();
	  }

	  char line[500];
	  char ip_address[500];
	  int hw_type;
	  int flags;
	  char mac_address[500];
	  char mask[500];
	  char device[500];

	  MacAddress foundMac;

	  if (ping) {
		ipAddress.ping(1000);
	  }

	  // ARP Table Reading Suggestion From
	  // http://stackoverflow.com/a/21031888

	  FILE *fp = fopen("/proc/net/arp", "r");
	  fgets(line, sizeof(line), fp);    // Skip the first line (column headers).

	  while(fgets(line, sizeof(line), fp)) {
		  sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
				ip_address,
				&hw_type,
				&flags,
				mac_address,
				mask,
				device);

		  if (ipAddress.toString().compare(ip_address) == 0) {
			foundMac = MacAddress(mac_address);
			break;
		  } 
	  }

	  fclose(fp);
	  return foundMac;
	}
void PfRingDevice::setPfRingDeviceAttributes()
{
	if (m_InterfaceIndex > -1)
		return;

	pfring* ring = NULL;
	bool closeRing = false;
	if (m_NumOfOpenedRxChannels > 0)
		ring = m_PfRingDescriptors[0];
	else
	{
		uint32_t flags = PF_RING_PROMISC | PF_RING_DNA_SYMMETRIC_RSS;
		ring = pfring_open(m_DeviceName, DEFAULT_PF_RING_SNAPLEN, flags);
		closeRing = true;
	}

	if (ring == NULL)
	{
		LOG_ERROR("Could not open a pfring for setting device attributes: MAC address, interface index and HW clock");
		return;
	}

	// set device MAC address

	uint8_t macAddress[6];
	if (pfring_get_bound_device_address(ring, macAddress) < 0)
		LOG_ERROR("Unable to read the device MAC address for interface '%s'", m_DeviceName);
	else
		m_MacAddress = MacAddress(macAddress);

	// set interface ID
	if (pfring_get_bound_device_ifindex(ring, &m_InterfaceIndex) < 0)
		LOG_ERROR("Unable to read interface index of device");

	// try to set hardware device clock
	m_HwClockEnabled = setPfRingDeviceClock(ring);

	// set interface MTU
	int mtu = pfring_get_mtu_size(ring);
	if (mtu < 0)
		LOG_ERROR("Could not get MTU. pfring_get_mtu_size returned an error: %d", mtu);
	else
		m_DeviceMTU = mtu + sizeof(ether_header) + sizeof(vlan_header);

	if (LoggerPP::getInstance().isDebugEnabled(PcapLogModulePfRingDevice))
	{
		std::string hwEnabled = (m_HwClockEnabled ? "enabled" : "disabled");
		LOG_DEBUG("Capturing from %s [%s][ifIndex: %d][MTU: %d], HW clock %s", m_DeviceName, m_MacAddress.toString().c_str(), m_InterfaceIndex, m_DeviceMTU, hwEnabled.c_str());
	}


	if (closeRing)
		pfring_close(ring);
}
示例#6
0
文件: Vlan.cpp 项目: Gardenya/fboss
Vlan::Vlan(const cfg::Vlan* config, uint32_t mtu, MemberPorts ports)
  : NodeBaseT(VlanID(config->id),
              config->name,
              mtu,
              (config->__isset.dhcpRelayAddressV4 ?
               IPAddressV4(config->dhcpRelayAddressV4) : IPAddressV4()),
              (config->__isset.dhcpRelayAddressV6 ?
               IPAddressV6(config->dhcpRelayAddressV6) : IPAddressV6()),
               std::move(ports)) {

  DhcpV4OverrideMap map4;
  for (const auto& o: config->dhcpRelayOverridesV4) {
    map4[MacAddress(o.first)] = folly::IPAddressV4(o.second);
  }
  setDhcpV4RelayOverrides(map4);

  DhcpV6OverrideMap map6;
  for (const auto& o: config->dhcpRelayOverridesV6) {
    map6[MacAddress(o.first)] = folly::IPAddressV6(o.second);
  }
  setDhcpV6RelayOverrides(map6);
}
示例#7
0
InterfaceFields InterfaceFields::fromFollyDynamic(const folly::dynamic& json) {
  auto intfFields =
    InterfaceFields(InterfaceID(json[kIntfId].asInt()),
      RouterID(json[kRouterId].asInt()), VlanID(json[kVlanId].asInt()),
      json[kName].asString(), MacAddress(json[kMac].asString()));
  ThriftSerializerJson<cfg::NdpConfig> serializer;
  for (const auto& addr: json[kAddresses]) {
    auto cidr = IPAddress::createNetwork(addr.asString(),
          -1 /*use /32 for v4 and /128 for v6*/,
        false /*don't apply mask*/);
    intfFields.addrs[cidr.first] = cidr.second;
  }
  serializer.deserialize(toJson(json[kNdpConfig]),
      &intfFields.ndp);
  return intfFields;
}
示例#8
0
AggregatePortFields AggregatePortFields::fromFollyDynamic(
    const folly::dynamic& json) {
  Subports ports;

  ports.reserve(json[kSubports].size());

  for (auto const& port : json[kSubports]) {
    ports.emplace_hint(ports.cend(), Subport::fromFollyDynamic(port));
  }

  return AggregatePortFields(
      AggregatePortID(json[kId].getInt()),
      json[kName].getString(),
      json[kDescription].getString(),
      json[kSystemPriority].getInt(),
      MacAddress(json[kSystemID].getString()),
      std::move(ports));
}
示例#9
0
void WedgePlatform::initLocalMac() {
  if (!FLAGS_mac.empty()) {
    localMac_ = MacAddress(FLAGS_mac);
    return;
  }

  // TODO(t4543375): Get the base MAC address from the BMC.
  //
  // For now, we take the MAC address from eth0, and enable the
  // "locally administered" bit.  This MAC should be unique, and it's fine for
  // us to use a locally administered address for now.
  std::vector<std::string> cmd{"/sbin/ip", "address", "ls", FLAGS_mgmt_if};
  Subprocess p(cmd, Subprocess::pipeStdout());
  auto out = p.communicate();
  p.waitChecked();
  auto idx = out.first.find("link/ether ");
  if (idx == std::string::npos) {
    throw std::runtime_error("unable to determine local mac address");
  }
  MacAddress eth0Mac(out.first.substr(idx + 11, 17));
  localMac_ = MacAddress::fromHBO(eth0Mac.u64HBO() | 0x0000020000000000);
}
示例#10
0
	/// Read-only accessor to _dstMac.
	MacAddress getDstMAC() const { std::string macStr = _dstMAC; return MacAddress(macStr); }
示例#11
0
	/// Read-only accessor to _srcMac.
	MacAddress getSrcMAC() const { std::string macStr = _srcMAC; return MacAddress(macStr); }
示例#12
0
	/// Returns a MacAddress initialized with broadcastStr().
	static MacAddress broadcast() { return MacAddress(broadcastStr()); }
示例#13
0
	/// Returns a MacAddress initialized to 00:00:00:00:00:00.
	static MacAddress empty() { return MacAddress(); }
示例#14
0
void PcapLiveDevice::setDeviceMacAddress()
{
#ifdef WIN32

	LPADAPTER adapter = PacketOpenAdapter((char*)m_Name);
	if (adapter == NULL)
	{
		LOG_ERROR("Error in retrieving MAC address: Adapter is NULL");
		return;
	}

	uint8_t buffer[512];
	PACKET_OID_DATA* oidData = (PACKET_OID_DATA*)buffer;
    oidData->Oid = OID_802_3_CURRENT_ADDRESS;
    oidData->Length = 6;
    oidData->Data[0] = 0;
    bool status = PacketRequest(adapter, false, oidData);
    if(status)
    {
        if(oidData->Length == 6)
        {
            /* copy value from driver */
        	m_MacAddress = MacAddress(oidData->Data[0], oidData->Data[1], oidData->Data[2], oidData->Data[3], oidData->Data[4], oidData->Data[5]);
        	LOG_DEBUG("   MAC address: %s", m_MacAddress.toString().c_str());
        } else
        {
            /* the driver returned a value that is longer than expected (and longer than the given buffer) */
        	LOG_DEBUG("Error in retrieving MAC address: Size of Oid larger than 6, OidLen:%lu", oidData->Length);
            return;
        }
    }
    else
    {
    	LOG_DEBUG("Error in retrieving MAC address: PacketRequest failed");
    }
#elif LINUX
	struct ifreq ifr;

	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, m_Name, sizeof(ifr.ifr_name));

	int socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
	if (ioctl(socketfd, SIOCGIFHWADDR, &ifr) == -1)
	{
		LOG_DEBUG("Error in retrieving MAC address: ioctl() returned -1");
		return;
	}

    m_MacAddress = MacAddress(ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2], ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
#elif MAC_OS_X
    int	mib[6];
    size_t len;

	mib[0] = CTL_NET;
	mib[1] = AF_ROUTE;
	mib[2] = 0;
	mib[3] = AF_LINK;
	mib[4] = NET_RT_IFLIST;
	mib[5] = if_nametoindex(m_Name);

	if ((mib[5] == 0)){
		LOG_ERROR("Error in retrieving MAC address: if_nametoindex error");
		return;
	}

	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
		LOG_ERROR("Error in retrieving MAC address: sysctl 1 error");
		return;
	}

	uint8_t buf[len];

	if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
		LOG_ERROR("Error in retrieving MAC address: sysctl 2 error");
		return;
	}

	struct if_msghdr*ifm = (struct if_msghdr *)buf;
	struct sockaddr_dl* sdl = (struct sockaddr_dl *)(ifm + 1);
	uint8_t* ptr = (uint8_t*)LLADDR(sdl);
	m_MacAddress = MacAddress(ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
#endif
}
示例#15
0
	MacAddress MacAddress::lookup_from_ip(const IpAddress& ipAddress) {
	  return MacAddress(); // TODO
	  /*struct ifreq ifr;
	  int sock;


	  int j, k;
	  char *p, addr[32], mask[32], mac[32];


	  //sock = socket(AF_INET, SOCK_STREAM, 0);
	  //sock = socket(AF_INET, SOCK_DGRAM, 0); // yes
	  //sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
	  sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ARP));

	  if (-1 == sock) {
		  perror("socket() ");
		  return MacAddress();
	  }

	   / *int ret = setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));

	  if (-1 == ret) {
		  cout << "ERROR ON SETOPT" << endl;
		  return MacAddress();
	  }*/


	  /*
        if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1)
                error("socket(SOCK_RAW): %m");
        if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on,
            sizeof(on)) == -1)
                error("setsockopt(IP_HDRINCL): %m");
[...]
and then
sendmsg(sock, somemmem....);*/

	  /*int port = DAC_PORT_COMMS;
	  sockaddr_in server;

	  server.sin_family = AF_INET;
	  server.sin_port = htons(port);
	  server.sin_addr.s_addr = inet_addr("169.254.97.13");

	  int ret = connect(sock, (sockaddr*)&server, sizeof(server)); 

	  if (-1 == ret) {
		cout << "Didn't connect on socket " << sock << " and port " << port << endl;
		return MacAddress();
	  }*/

	  //#ifr.ifr_addr = inet_addr("169.254.97.13");

	  //strncpy(ifr.ifr_name, "eth1", sizeof(ifr.ifr_name)-1);
	  //ifr.ifr_name[sizeof(ifr.ifr_name)-1]='\0';

	  /*if (-1==ioctl(sock, SIOCGIFADDR, &ifr)) {
		  perror("ioctl(SIOCGIFADDR) 2");
		  return MacAddress();
	  } 
	  p = inet_ntoa(((struct sockaddr_in *)(&ifr.ifr_addr))->sin_addr);
	  strncpy(addr, p, sizeof(addr)-1);
	  addr[sizeof(addr)-1]='\0';

	  if (-1==ioctl(sock, SIOCGIFNETMASK, &ifr)) {
		  perror("ioctl(SIOCGIFNETMASK) 3");
			  return MacAddress();
		  }
		  p = inet_ntoa(((struct sockaddr_in *)(&ifr.ifr_netmask))->sin_addr);
		  strncpy(mask,p,sizeof(mask)-1);
		  mask[sizeof(mask)-1]='\0';

		  if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
		  perror("ioctl(SIOCGIFHWADDR) 4");
		  return MacAddress();
	  }
	  for (j=0, k=0; j<6; j++) {
		  k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
			  (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
	  }
	  mac[sizeof(mac)-1]='\0';

	  printf("\n");
	  printf("name:    %s\n",ifr.ifr_name);
	  printf("address: %s\n",addr);
	  printf("netmask: %s\n",mask);
	  printf("macaddr: %s\n",mac);
	  printf("\n");

	  close(sock);

	  return MacAddress();

	  / *sock = socket(AF_INET, SOCK_DGRAM, 0);
  
	  strcpy(ifr.ifr_name, "eth0");
	  ifr.ifr_addr.sa_family = AF_INET;
	  
	  if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
		cout << "Error" << endl;
		return MacAddress();
	  }
	  
	  char mac[6];

	  memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);

	  cout << "Mac: " + string(mac) << endl;
		
	  close(sock);

	  return MacAddress();*/
	}
示例#16
0
MacAddress ThriftConfigApplier::getInterfaceMac(const cfg::Interface* config) {
    if (config->__isset.mac) {
        return MacAddress(config->mac);
    }
    return platform_->getLocalMac();
}