From 34864875b9c5feedb1a653617c15d05634926541 Mon Sep 17 00:00:00 2001 From: Santiago Alessandri Date: Thu, 25 Aug 2011 11:58:57 -0300 Subject: [PATCH 1/2] Added 802.11 Association Request PDU. --- include/ieee802-11.h | 123 +++++++++++++++++++++++++++++++++++++++++++ src/ieee802-11.cpp | 90 ++++++++++++++++++++++++++++++- 2 files changed, 211 insertions(+), 2 deletions(-) diff --git a/include/ieee802-11.h b/include/ieee802-11.h index 31804e9..d14cd8c 100644 --- a/include/ieee802-11.h +++ b/include/ieee802-11.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "pdu.h" #include "utils.h" @@ -1147,6 +1148,128 @@ namespace Tins { DisassocBody _body; }; + /** + * \brief Class representing an Association Request frame in the IEEE 802.11 Protocol. + * + */ + class IEEE802_11_Assoc_Request : public ManagementFrame { + + public: + + /** + * \brief Default constructor for the Association Request frame. + * + */ + IEEE802_11_Assoc_Request(); + + /** + * \brief Constructor for creating a 802.11 Association Request. + * + * Constructor that builds a 802.11 Association Request taking the interface name, + * destination's and source's MAC. + * + * \param iface string containing the interface's name from where to send the packet. + * \param dst_hw_addr uint8_t array of 6 bytes containing the destination's MAC(optional). + * \param src_hw_addr uint8_t array of 6 bytes containing the source's MAC(optional). + */ + IEEE802_11_Assoc_Request(const std::string& iface, const uint8_t* dst_hw_addr = 0, const uint8_t* src_hw_addr = 0) throw (std::runtime_error); + + /** + * \brief Getter for the Capabilities Information. + * + * \return CapabilityInformation Structure in a CapabilityInformation&. + */ + inline const CapabilityInformation& capabilities() const { return this->_body.capability;} + + /** + * \brief Getter for the Capabilities Information. + * + * \return CapabilityInformation Structure in a CapabilityInformation&. + */ + inline CapabilityInformation& capabilities() { return this->_body.capability;} + + /** + * \brief Getter for the listen interval. + * + * \return The listen interval in an uint16_t. + */ + inline uint16_t listen_interval() const { return this->_body.listen_interval; } + + /** + * \brief Setter for the listen interval. + * + * \param new_listen_interval uint16_t with the new listen interval. + */ + void listen_interval(uint16_t new_listen_interval); + + /** + * \brief Helper method to set the essid. + * + * \param new_ssid The ssid to be set. + */ + void ssid(const std::string &new_ssid); + + /** + * \brief Helper method to set the supported rates. + * + * \param new_rates A list of rates to be set. + */ + void supported_rates(const std::list &new_rates); + + /** + * \brief Helper method to set the extended supported rates. + * + * \param new_rates A list of rates to be set. + */ + void extended_supported_rates(const std::list &new_rates); + + /** + * \brief Helper method to set the power capabilities. + * + * \param min_power uint8_t indicating the minimum transmiting power capability. + * \param max_power uint8_t indicating the maximum transmiting power capability. + */ + void power_capabilities(uint8_t min_power, uint8_t max_power); + + /** + * \brief Helper method to set the supported channels. + * + * \param new_channels A list of channels to be set. + */ + void supported_channels(const std::list > &new_channels); + + /** + * \brief Helper method to set the RSN information option. + * + * \param info The RSNInformation structure to be set. + */ + void rsn_information(const RSNInformation& info); + + /** + * \brief Helper method to set the QoS capabilities. + * + * \param new_qos_capabilities uint8_t with the capabilities. + */ + void qos_capabilities(uint8_t new_qos_capabilities); + + /** + * \brief Returns the frame's header length. + * + * \return An uint32_t with the header's size. + * \sa PDU::header_size() + */ + uint32_t header_size() const; + private: + struct AssocReqBody { + CapabilityInformation capability; + uint16_t listen_interval; + }; + + uint32_t write_fixed_parameters(uint8_t *buffer, uint32_t total_sz); + + AssocReqBody _body; + }; + } #endif diff --git a/src/ieee802-11.cpp b/src/ieee802-11.cpp index 2b3ba1a..3e6f1fa 100644 --- a/src/ieee802-11.cpp +++ b/src/ieee802-11.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #ifndef WIN32 #include #include @@ -238,7 +239,7 @@ void Tins::IEEE802_11::write_serialization(uint8_t *buffer, uint32_t total_sz, c } Tins::PDU *Tins::IEEE802_11::from_bytes(const uint8_t *buffer, uint32_t total_sz) { - if(total_sz < sizeof(ieee80211_header)) + if(total_sz < sizeof(ieee80211_header)) throw std::runtime_error("Not enough size for a IEEE 802.11 header in the buffer."); const ieee80211_header *hdr = (const ieee80211_header*)buffer; PDU *ret = 0; @@ -253,7 +254,7 @@ Tins::PDU *Tins::IEEE802_11::from_bytes(const uint8_t *buffer, uint32_t total_sz */ Tins::ManagementFrame::ManagementFrame(const uint8_t *buffer, uint32_t total_sz) : IEEE802_11(buffer, total_sz) { - + } Tins::ManagementFrame::ManagementFrame(const uint8_t *dst_hw_addr, const uint8_t *src_hw_addr) : IEEE802_11(dst_hw_addr, src_hw_addr) { @@ -424,3 +425,88 @@ Tins::RSNInformation Tins::RSNInformation::wpa2_psk() { info.add_akm_cypher(RSNInformation::PSK); return info; } + +Tins::IEEE802_11_Assoc_Request::IEEE802_11_Assoc_Request() { + this->subtype(IEEE802_11::ASSOC_REQ); + memset(&_body, 0, sizeof(_body)); +} + +Tins::IEEE802_11_Assoc_Request::IEEE802_11_Assoc_Request(const std::string& iface, + const uint8_t* dst_hw_addr, + const uint8_t* src_hw_addr) throw (std::runtime_error) : ManagementFrame(iface, dst_hw_addr, src_hw_addr){ + this->subtype(IEEE802_11::ASSOC_REQ); + memset(&_body, 0, sizeof(_body)); +} + +void Tins::IEEE802_11_Assoc_Request::listen_interval(uint16_t new_listen_interval) { + this->_body.listen_interval = new_listen_interval; +} + +void Tins::IEEE802_11_Assoc_Request::ssid(const std::string &new_ssid) { + add_tagged_option(IEEE802_11::SSID, new_ssid.size(), (const uint8_t*)new_ssid.c_str()); +} + +void Tins::IEEE802_11_Assoc_Request::supported_rates(const std::list &new_rates) { + uint8_t *buffer = new uint8_t[new_rates.size()], *ptr = buffer; + for(std::list::const_iterator it = new_rates.begin(); it != new_rates.end(); ++it) { + uint8_t result = 0x80, left = *it / 0.5; + if(*it - left > 0) + left++; + *(ptr++) = (result | left); + } + add_tagged_option(SUPPORTED_RATES, new_rates.size(), buffer); + delete[] buffer; +} + +void Tins::IEEE802_11_Assoc_Request::extended_supported_rates(const std::list &new_rates) { + uint8_t *buffer = new uint8_t[new_rates.size()], *ptr = buffer; + for(std::list::const_iterator it = new_rates.begin(); it != new_rates.end(); ++it) { + uint8_t result = 0x80, left = *it / 0.5; + if(*it - left > 0) + left++; + *(ptr++) = (result | left); + } + add_tagged_option(EXT_SUPPORTED_RATES, new_rates.size(), buffer); + delete[] buffer; +} + +void Tins::IEEE802_11_Assoc_Request::power_capabilities(uint8_t min_power, uint8_t max_power) { + uint8_t buffer[2]; + buffer[0] = min_power; + buffer[1] = max_power; + add_tagged_option(POWER_CAPABILITY, 2, buffer); +} + +void Tins::IEEE802_11_Assoc_Request::supported_channels(const std::list > &new_channels) { + + uint8_t* buffer = new uint8_t[new_channels.size() * 2]; + uint8_t* ptr = buffer; + for(std::list >::const_iterator it = new_channels.begin(); it != new_channels.end(); ++it) { + *(ptr++) = it->first; + *(ptr++) = it->second; + } + add_tagged_option(SUPPORTED_CHANNELS, new_channels.size() * 2, buffer); + +} + +void Tins::IEEE802_11_Assoc_Request::rsn_information(const RSNInformation& info) { + uint32_t size; + uint8_t *buffer = info.serialize(size); + add_tagged_option(RSN, size, buffer); + delete[] buffer; +} + +void Tins::IEEE802_11_Assoc_Request::qos_capabilities(uint8_t new_qos_capabilities) { + add_tagged_option(QOS_CAPABILITY, 1, &new_qos_capabilities); +} + +uint32_t Tins::IEEE802_11_Assoc_Request::header_size() const { + return IEEE802_11::header_size() + sizeof(AssocReqBody); +} + +uint32_t Tins::IEEE802_11_Assoc_Request::write_fixed_parameters(uint8_t *buffer, uint32_t total_sz) { + uint32_t sz = sizeof(AssocReqBody); + assert(sz <= total_sz); + memcpy(buffer, &this->_body, sz); + return sz; +} From b4f1571ea38f87aa5c90d9a303e938a58fb9591f Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Thu, 25 Aug 2011 19:36:57 -0300 Subject: [PATCH 2/2] Fixed IEEE802_11 constructor from buffer to allow initialization from shorter 802.11 types. --- src/ieee802-11.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ieee802-11.cpp b/src/ieee802-11.cpp index e4c5ba2..2a8a018 100644 --- a/src/ieee802-11.cpp +++ b/src/ieee802-11.cpp @@ -22,7 +22,7 @@ #include #include #include -#include //borrame +#include #ifndef WIN32 #include #include @@ -70,11 +70,12 @@ Tins::IEEE802_11::IEEE802_11(const ieee80211_header *header_ptr) : PDU(ETHERTYPE } Tins::IEEE802_11::IEEE802_11(const uint8_t *buffer, uint32_t total_sz) : PDU(ETHERTYPE_IP), _options_size(0) { - if(total_sz < sizeof(_header)) + if(total_sz < sizeof(_header.control)) throw std::runtime_error("Not enough size for an IEEE 802.11 header in the buffer."); - std::memcpy(&_header, buffer, sizeof(_header)); - buffer += sizeof(_header); - total_sz -= sizeof(_header); + uint32_t sz = std::min(sizeof(_header), total_sz); + std::memcpy(&_header, buffer, sz); + buffer += sz; + total_sz -= sz; // subclass specific parsing missing too. } @@ -241,12 +242,16 @@ void Tins::IEEE802_11::write_serialization(uint8_t *buffer, uint32_t total_sz, c } Tins::PDU *Tins::IEEE802_11::from_bytes(const uint8_t *buffer, uint32_t total_sz) { - if(total_sz < sizeof(ieee80211_header)) + // We only need the control field, the length of the PDU will depend on the flags set. + if(total_sz < sizeof(ieee80211_header::control)) throw std::runtime_error("Not enough size for a IEEE 802.11 header in the buffer."); const ieee80211_header *hdr = (const ieee80211_header*)buffer; PDU *ret = 0; - if(hdr->control.type == 0 && hdr->control.subtype == 8) + if(hdr->control.type == 0 && hdr->control.subtype == 8) { + if(total_sz < sizeof(_header)) + throw std::runtime_error("Not enough size for an IEEE 802.11 header in the buffer."); ret = new IEEE802_11_Beacon(buffer, total_sz); + } else ret = new IEEE802_11(buffer, total_sz); return ret;