1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-28 20:44:26 +01:00

LLC almost works

This commit is contained in:
Santiago Alessandri
2012-03-19 12:04:55 -03:00
parent 937a4d66cc
commit 01a7b812df
6 changed files with 710 additions and 60 deletions

221
include/ieee802-3.h Normal file
View File

@@ -0,0 +1,221 @@
/*
* libtins is a net packet wrapper library for crafting and
* interpreting sniffed packets.
*
* Copyright (C) 2011 Nasel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __IEEE802_3_H
#define __IEEE802_3_H
#include <stdint.h>
#include <stdexcept>
#include "pdu.h"
#include "utils.h"
namespace Tins {
/**
* \brief Class representing an Ethernet II PDU.
*/
class IEEE802_3 : public PDU {
public:
/**
* \brief Represents the IEEE802_3 broadcast address.
*/
static const uint8_t* BROADCAST;
/**
* \brief IEEE802_3 hardware address size.
*/
static const unsigned ADDR_SIZE = 6;
/**
* \brief Constructor for creating an IEEE802_3 PDU
*
* Constructor that builds an IEEE802_3 PDU 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).
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
*/
IEEE802_3(const std::string& iface, const uint8_t* dst_hw_addr = 0, const uint8_t* src_hw_addr = 0, PDU* child = 0) throw (std::runtime_error);
/**
* \brief Constructor for creating an IEEE802_3 PDU
*
* Constructor that builds an IEEE802_3 PDU taking the interface index,
* destination's and source's MAC.
*
* \param iface_index const uint32_t with the interface's index 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).
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
*/
IEEE802_3(uint32_t iface_index, const uint8_t* dst_hw_addr = 0, const uint8_t* src_hw_addr = 0, PDU* child = 0);
/**
* \brief Constructor which creates an IEEE802_3 object from a buffer and adds all identifiable
* PDUs found in the buffer as children of this one.
* \param buffer The buffer from which this PDU will be constructed.
* \param total_sz The total size of the buffer.
*/
IEEE802_3(const uint8_t *buffer, uint32_t total_sz);
/* Getters */
/**
* \brief Getter for the destination's mac address.
*
* \return Returns the destination's mac address as a constant uint8_t pointer.
*/
inline const uint8_t* dst_addr() const { return _eth.dst_mac; }
/**
* \brief Getter for the source's mac address.
*
* \return Returns the source's mac address as a constant uint8_t pointer.
*/
inline const uint8_t* src_addr() const { return _eth.src_mac; }
/**
* \brief Getter for the interface.
*
* \return Returns the interface's index as an uint32_t.
*/
inline uint32_t iface() const { return this->_iface_index; }
/**
* \brief Getter for the length field.
* \return The length field value.
*/
inline uint16_t length() const { return Utils::net_to_host_s(_eth.length); };
/* Setters */
/**
* \brief Setter for the destination's MAC.
*
* \param new_dst_mac uint8_t array of 6 bytes containing the new destination's MAC.
*/
void dst_addr(const uint8_t* new_dst_mac);
/**
* \brief Setter for the source's MAC.
*
* \param new_src_mac uint8_t array of 6 bytes containing the new source's MAC.
*/
void src_addr(const uint8_t* new_src_mac);
/**
* \brief Setter for the interface.
*
* \param new_iface_index uint32_t containing the new interface index.
*/
void iface(uint32_t new_iface_index);
/**
* \brief Setter for the interface.
*
* \param new_iface string reference containing the new interface name.
*/
void iface(const std::string& new_iface) throw (std::runtime_error);
/**
* \brief Setter for the length field.
*
* \param new_length uint16_t with the new value of the length field.
*/
void length(uint16_t new_length);
/* Virtual methods */
/**
* \brief Returns the IEEE802_3 frame's header length.
*
* \return An uint32_t with the header's size.
* \sa PDU::header_size()
*/
uint32_t header_size() const;
/**
* \sa PDU::send()
*/
bool send(PacketSender* sender);
/** \brief Check wether ptr points to a valid response for this PDU.
*
* \sa PDU::matches_response
* \param ptr The pointer to the buffer.
* \param total_sz The size of the buffer.
*/
bool matches_response(uint8_t *ptr, uint32_t total_sz);
/** \brief Receives a matching response for this packet.
*
* \sa PDU::recv_response
* \param sender The packet sender which will receive the packet.
*/
PDU *recv_response(PacketSender *sender);
/**
* \brief Getter for the PDU's type.
* \sa PDU::pdu_type
*/
PDUType pdu_type() const { return PDU::IEEE802_3; }
/** \brief Clones this pdu, filling the corresponding header with data
* extracted from a buffer.
*
* \param ptr The pointer to the from from which the data will be extracted.
* \param total_sz The size of the buffer.
* \return The cloned PDU.
* \sa PDU::clone_packet
*/
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
/**
* \brief Clones this PDU.
*
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const;
private:
/**
* Struct that represents the Ethernet II header
*/
struct ethhdr {
uint8_t dst_mac[ADDR_SIZE];
uint8_t src_mac[ADDR_SIZE];
uint16_t length;
} __attribute__((__packed__));
void copy_fields(const IEEE802_3 *other);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
ethhdr _eth;
uint32_t _iface_index;
};
};
#endif

View File

@@ -58,32 +58,38 @@ namespace Tins {
*/
enum ModifierFunctions {
UI = 0x00,
XID = 0xAC,
TEST = 0xE0,
SABME = 0x6C,
DISC = 0x40,
UA = 0x60,
DM = 0x0C,
FRMR = 0x84
XID = 0x1D,
TEST = 0x07,
SABME = 0x1E,
DISC = 0x02,
UA = 0x06,
DM = 0x18,
FRMR = 0x11
};
/**
* \brief LLC Supervisory functions
*/
enum SupervisoryFunctions {
RECEIVE_READY = 0x00,
REJECT = 0x08,
RECEIVE_NOT_READY = 0x04
RECEIVE_READY = 0,
REJECT = 2,
RECEIVE_NOT_READY = 1
};
/**
* \brief Creates an instance of LLC
* This constructor sets the dsap and ssap fields to 0xaa, and
* the id field to 3.
* \param child The child PDU.(optional)
*/
LLC(PDU *child = 0);
/**
* \brief Creates an instance of LLC, setting the dsap and ssap.
* The control field is set all to 0.
* @param dsap The dsap value to be set.
* @param ssap The ssap value to be set.
*/
LLC(uint8_t dsap, uint8_t ssap, PDU* child = 0);
/**
* \brief Constructor which creates a LLC object from a buffer and adds all identifiable
* PDUs found in the buffer as children of this one.
@@ -209,7 +215,7 @@ namespace Tins {
* \brief Getter for the LLC frame format type.
* \return The LLC frame format.
*/
inline uint8_t type() {return control_field & 0x03; }
inline uint8_t type() {return _type; }
/**
* \brief Getter for sender send sequence number.
@@ -217,7 +223,7 @@ namespace Tins {
* \return The sender send sequence number if format is INFORMATION else 0.
*/
inline uint8_t send_seq_number() {
return (type() == INFORMATION) ? (control_field & 0x00FE) >> 1 : 0;
return (type() == INFORMATION) ? (control_field.info.send_seq_num) : 0;
}
/**
@@ -227,10 +233,14 @@ namespace Tins {
* INFORMATION or SUPERVISORY else 0.
*/
inline uint8_t receive_seq_number() {
if (type() == INFORMATION || type() == SUPERVISORY)
return (control_field & 0xFE00) >> 9;
else
return 0;
switch (type()) {
case INFORMATION:
return control_field.info.recv_seq_num;
case SUPERVISORY:
return control_field.super.recv_seq_num;
case UNNUMBERED:
return 0;
}
}
/**
@@ -238,10 +248,14 @@ namespace Tins {
* \return Whether the poll/final flag is set.
*/
inline bool poll_final() {
if (type() == UNNUMBERED)
return control_field & 0x10;
else
return control_field & 0x0100;
switch (type()) {
case UNNUMBERED:
return control_field.unnumbered.poll_final_bit;
case INFORMATION:
return control_field.info.poll_final_bit;
case SUPERVISORY:
return control_field.super.poll_final_bit;
}
}
/**
@@ -251,7 +265,7 @@ namespace Tins {
*/
inline uint8_t supervisory_function() {
if (type() == SUPERVISORY)
return control_field & 0x0C;
return control_field.super.supervisory_func;
return 0;
}
@@ -262,7 +276,7 @@ namespace Tins {
*/
inline uint8_t modifier_function() {
if (type() == UNNUMBERED)
return control_field & 0xEC;
return (control_field.unnumbered.mod_func1 << 3) + control_field.unnumbered.mod_func2;
return 0;
}
@@ -297,12 +311,40 @@ namespace Tins {
uint8_t ssap;
} __attribute__((__packed__));
struct info_control_field {
uint16_t
type_bit:1,
send_seq_num:7,
poll_final_bit:1,
recv_seq_num:7;
} __attribute__((__packed__));
struct super_control_field {
uint16_t type_bit:2,
supervisory_func:2,
unused:4,
poll_final_bit:1,
recv_seq_num:7;
} __attribute__((__packed__));
struct un_control_field {
uint8_t type_bits:2,
mod_func1:2,
poll_final_bit:1,
mod_func2:3;
} __attribute__((__packed__));
void copy_fields(const LLC *other);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
llchdr _header;
uint8_t control_field_length;
uint16_t control_field;
union {
info_control_field info;
super_control_field super;
un_control_field unnumbered;
} control_field;
Format _type;
uint8_t information_field_length;
std::list<std::pair<uint8_t,uint8_t*> > information_fields;
};

View File

@@ -52,6 +52,7 @@ namespace Tins {
enum PDUType {
RAW,
ETHERNET_II,
IEEE802_3,
RADIOTAP,
DOT11,
DOT11_ACK,