1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 13:04:28 +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

169
src/ieee802-3.cpp Normal file
View File

@@ -0,0 +1,169 @@
/*
* 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
*/
#include <cassert>
#include <cstring>
#include <stdexcept>
#ifndef WIN32
#include <net/ethernet.h>
#include <netpacket/packet.h>
#endif
#include "ieee802-3.h"
#include "llc.h"
#include "utils.h"
const uint8_t* Tins::IEEE802_3::BROADCAST = (const uint8_t*)"\xff\xff\xff\xff\xff\xff";
const uint32_t Tins::IEEE802_3::ADDR_SIZE;
Tins::IEEE802_3::IEEE802_3(const std::string& iface, const uint8_t* dst_hw_addr, const uint8_t* src_hw_addr, PDU* child) throw (std::runtime_error) : PDU(ETHERTYPE_IP, child) {
memset(&_eth, 0, sizeof(ethhdr));
if(dst_hw_addr)
this->dst_addr(dst_hw_addr);
if(src_hw_addr)
this->src_addr(src_hw_addr);
this->iface(iface);
this->_eth.length = 0;
}
Tins::IEEE802_3::IEEE802_3(uint32_t iface_index, const uint8_t* dst_hw_addr, const uint8_t* src_hw_addr, PDU* child) : PDU(ETHERTYPE_IP, child) {
memset(&_eth, 0, sizeof(ethhdr));
if(dst_hw_addr)
this->dst_addr(dst_hw_addr);
if(src_hw_addr)
this->src_addr(src_hw_addr);
this->iface(iface_index);
this->_eth.length = 0;
}
Tins::IEEE802_3::IEEE802_3(const uint8_t *buffer, uint32_t total_sz) : PDU(ETHERTYPE_IP) {
if(total_sz < sizeof(ethhdr))
throw std::runtime_error("Not enough size for an ethernetII header in the buffer.");
memcpy(&_eth, buffer, sizeof(ethhdr));
PDU *next = 0;
buffer += sizeof(ethhdr);
total_sz -= sizeof(ethhdr);
if(total_sz) {
next = new Tins::LLC(buffer, total_sz);
inner_pdu(next);
}
}
void Tins::IEEE802_3::dst_addr(const uint8_t* new_dst_mac) {
memcpy(_eth.dst_mac, new_dst_mac, sizeof(_eth.dst_mac));
}
void Tins::IEEE802_3::src_addr(const uint8_t* new_src_mac) {
memcpy(_eth.src_mac, new_src_mac, sizeof(_eth.src_mac));
}
void Tins::IEEE802_3::iface(uint32_t new_iface_index) {
_iface_index = new_iface_index;
}
void Tins::IEEE802_3::iface(const std::string& new_iface) throw (std::runtime_error) {
if (!Tins::Utils::interface_id(new_iface, this->_iface_index)) {
throw std::runtime_error("Invalid interface name!");
}
}
void Tins::IEEE802_3::length(uint16_t new_length) {
this->_eth.length = Utils::net_to_host_s(new_length);
}
uint32_t Tins::IEEE802_3::header_size() const {
return sizeof(ethhdr);
}
bool Tins::IEEE802_3::send(PacketSender* sender) {
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
addr.sll_halen = ADDR_SIZE;
addr.sll_ifindex = _iface_index;
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
}
bool Tins::IEEE802_3::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(ethhdr))
return false;
ethhdr *eth_ptr = (ethhdr*)ptr;
if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, ADDR_SIZE)) {
return true;
}
return false;
}
void Tins::IEEE802_3::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
uint32_t my_sz = header_size();
bool set_length = _eth.length == 0;
assert(total_sz >= my_sz);
if (set_length)
_eth.length = Utils::net_to_host_s(size() - sizeof(_eth));
memcpy(buffer, &_eth, sizeof(ethhdr));
if (set_length)
_eth.length = 0;
}
Tins::PDU *Tins::IEEE802_3::recv_response(PacketSender *sender) {
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
addr.sll_protocol = Utils::net_to_host_s(ETH_P_802_3);
addr.sll_halen = ADDR_SIZE;
addr.sll_ifindex = _iface_index;
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
return sender->recv_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
}
Tins::PDU *Tins::IEEE802_3::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(_eth))
return 0;
PDU *child = 0, *cloned;
if(total_sz > sizeof(_eth)) {
if((child = PDU::clone_inner_pdu(ptr + sizeof(_eth), total_sz - sizeof(_eth))) == 0)
return 0;
}
cloned = new IEEE802_3(ptr, std::min(total_sz, (uint32_t)sizeof(_eth)));
cloned->inner_pdu(child);
return cloned;
}
void Tins::IEEE802_3::copy_fields(const IEEE802_3 *other) {
memcpy(&_eth, &other->_eth, sizeof(_eth));
_iface_index = other->_iface_index;
}
Tins::PDU *Tins::IEEE802_3::clone_pdu() const {
IEEE802_3 *new_pdu = new IEEE802_3(_iface_index);
new_pdu->copy_fields(this);
return new_pdu;
}

View File

@@ -38,10 +38,18 @@ using Tins::LLC;
const uint8_t LLC::GLOBAL_DSAP_ADDR = 0xFF;
const uint8_t LLC::NULL_ADDR = 0x00;
LLC::LLC(PDU *child) : PDU(0xff, child) {
LLC::LLC(PDU *child) : PDU(0xff, child), _type(LLC::INFORMATION) {
memset(&_header, 0, sizeof(llchdr));
control_field_length = 2;
control_field = 0;
memset(&control_field, 0, sizeof(control_field));
information_field_length = 0;
}
LLC::LLC(uint8_t dsap, uint8_t ssap, PDU *child) : PDU(0xff, child), _type(LLC::INFORMATION) {
_header.dsap = dsap;
_header.ssap = ssap;
control_field_length = 2;
memset(&control_field, 0, sizeof(control_field));
information_field_length = 0;
}
@@ -52,15 +60,16 @@ LLC::LLC(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
buffer += sizeof(_header);
total_sz -= sizeof(_header);
if ((buffer[0] & 0x03) == LLC::UNNUMBERED) {
control_field_length = 1;
control_field = buffer[0];
type(LLC::UNNUMBERED);
std::memcpy(&control_field.unnumbered, buffer, sizeof(un_control_field));
buffer++;
total_sz -= 1;
//TODO: Create information fields if corresponding.
}
else {
type((Format)(buffer[0] & 0x03));
control_field_length = 2;
control_field = Utils::net_to_host_s(((uint16_t*)buffer)[0]);
std::memcpy(&control_field.info, buffer, sizeof(un_control_field));
buffer += 2;
total_sz -= 2;
}
@@ -104,20 +113,19 @@ void LLC::ssap(uint8_t new_ssap) {
}
void LLC::type(LLC::Format type) {
_type = type;
switch (type) {
case LLC::INFORMATION:
control_field_length = 2;
control_field &= 0xFFFE;
control_field.info.type_bit = 0;
break;
case LLC::SUPERVISORY:
control_field_length = 2;
control_field &= 0xFFFD;
control_field |= type;
control_field.super.type_bit = 1;
break;
case LLC::UNNUMBERED:
control_field_length = 1;
control_field &= 0xFD;
control_field |= type;
control_field.unnumbered.type_bits = 3;
break;
}
}
@@ -125,48 +133,48 @@ void LLC::type(LLC::Format type) {
void LLC::send_seq_number(uint8_t seq_number) {
if (type() != LLC::INFORMATION)
return;
control_field &= 0xFF01;
control_field |= seq_number;
control_field.info.send_seq_num = seq_number;
}
void LLC::receive_seq_number(uint8_t seq_number) {
if (type() == LLC::UNNUMBERED)
return;
uint16_t to_mask = seq_number <<= 9;
control_field &= 0x01FF;
control_field |= to_mask;
switch (type()) {
case LLC::UNNUMBERED:
return;
case LLC::INFORMATION:
control_field.info.recv_seq_num = seq_number;
break;
case LLC::SUPERVISORY:
control_field.super.recv_seq_num = seq_number;
break;
}
}
void LLC::poll_final(bool value) {
uint16_t mask = 0;
switch (type()) {
case LLC::UNNUMBERED:
mask = 0xFFEF;
control_field.unnumbered.poll_final_bit = value;
break;
case LLC::INFORMATION:
control_field.info.poll_final_bit = value;
return;
case LLC::SUPERVISORY:
mask = 0xFEFF;
control_field.super.poll_final_bit = value;
break;
}
if (value)
control_field |= ~mask;
else
control_field &= mask;
}
void LLC::supervisory_function(LLC::SupervisoryFunctions new_func) {
if (type() != LLC::SUPERVISORY)
return;
control_field &= 0xFFF3;
control_field |= new_func;
control_field.super.supervisory_func = new_func;
}
void LLC::modifier_function(LLC::ModifierFunctions mod_func) {
if (type() != LLC::UNNUMBERED)
return;
control_field &= 0xEC;
control_field |= mod_func;
control_field.unnumbered.mod_func1 = mod_func >> 3;
control_field.unnumbered.mod_func2 = mod_func & 0x07;
}
void LLC::add_xid_information(uint8_t xid_id, uint8_t llc_type_class, uint8_t receive_window) {
@@ -214,14 +222,21 @@ void LLC::write_serialization(uint8_t *buffer, uint32_t total_sz, const Tins::PD
assert(total_sz >= header_size());
std::memcpy(buffer, &_header, sizeof(_header));
buffer += sizeof(_header);
if (control_field_length == 1) {
*buffer = (uint8_t)control_field;
buffer++;
}
else {
*((uint16_t*)buffer) = Utils::net_to_host_s(control_field);
buffer += 2;
switch (type()) {
case LLC::UNNUMBERED:
std::memcpy(buffer, &(control_field.unnumbered), sizeof(un_control_field));
buffer += sizeof(un_control_field);
break;
case LLC::INFORMATION:
std::memcpy(buffer, &(control_field.info), sizeof(info_control_field));
buffer += sizeof(info_control_field);
break;
case LLC::SUPERVISORY:
std::memcpy(buffer, &(control_field.super), sizeof(super_control_field));
buffer += sizeof(super_control_field);
break;
}
for (list<pair<uint8_t, uint8_t*> >::iterator it = information_fields.begin(); it != information_fields.end(); it++) {
std::memcpy(buffer, it->second, it->first);
buffer += it->first;