- Timestamping management added - Errors due to reading uninitialized data in ARP fixed - EthInterface reworked, incoming packet notification and payload readout separated (through which fixing concurrent access problems) - RX and TX offloads added - Capability to add a packet sieve layer without prior registration of specific packet class added (this makes it possible to register arbitrary EtherType connection blocks, for example)
63 lines
2.7 KiB
C
63 lines
2.7 KiB
C
//
|
|
// Created by epagris on 2022.12.08..
|
|
//
|
|
|
|
#include "arp_packet.h"
|
|
#include "ethernet_frame.h"
|
|
#include "../../utils.h"
|
|
#include "../../eth_interface.h"
|
|
|
|
static EthernetAddress emptyEthAddr = { 0x00 };
|
|
|
|
static void arp_fetch_mapping(const ArpProps * arpProps, ArpCache * arpc) {
|
|
ArpEntry entry;
|
|
if ((memcmp(arpProps->SHA, emptyEthAddr, ETH_HW_ADDR_LEN) != 0) && (arpProps->SPA != 0)) {
|
|
memcpy(entry.eth, arpProps->SHA, ETH_HW_ADDR_LEN);
|
|
entry.ip = arpProps->SPA;
|
|
arpc_learn(arpc, &entry);
|
|
}
|
|
if ((memcmp(arpProps->THA, emptyEthAddr, ETH_HW_ADDR_LEN) != 0) && (arpProps->TPA != 0)) {
|
|
memcpy(entry.eth, arpProps->THA, ETH_HW_ADDR_LEN);
|
|
entry.ip = arpProps->TPA;
|
|
arpc_learn(arpc, &entry);
|
|
}
|
|
}
|
|
|
|
int parse_arp(const uint8_t *hdr, uint32_t size, PcktHeaderElement *pcktHdrLe, struct EthInterface_ *intf, PcktProcFnPassbackData *pb) {
|
|
// parse ARP packet
|
|
ArpProps *arpProps = HEADER_FETCH_PROPS(ArpProps, pcktHdrLe);
|
|
FETCH_WORD_H2N_ADVANCE(&arpProps->HTYPE, hdr); // hardware type
|
|
FETCH_WORD_H2N_ADVANCE(&arpProps->PTYPE, hdr); // protocol type
|
|
FETCH_BYTE_ADVANCE(&arpProps->HLEN, hdr); // hardware address size
|
|
FETCH_BYTE_ADVANCE(&arpProps->PLEN, hdr); // protocol address size
|
|
FETCH_WORD_H2N_ADVANCE(&arpProps->OPER, hdr); // operation code
|
|
FETCH_ADVANCE(&arpProps->SHA, hdr, ETH_HW_ADDR_LEN); // sender HW address
|
|
FETCH_ADVANCE(&arpProps->SPA, hdr, sizeof(ip4_addr)); // sender protocol address
|
|
FETCH_ADVANCE(&arpProps->THA, hdr, ETH_HW_ADDR_LEN); // target HW address
|
|
FETCH_ADVANCE(&arpProps->TPA, hdr, sizeof(ip4_addr)); // target protocol address
|
|
|
|
// fill-in common packet header fields
|
|
arpProps->validityOK = true; // TODO!
|
|
arpProps->containedPacketClass = 0;
|
|
arpProps->headerSize = ETH_ARP_HEADER_SIZE;
|
|
|
|
// learn new mapping
|
|
arp_fetch_mapping(arpProps, intf->arpc);
|
|
|
|
return arpProps->validityOK ? PROC_FN_RET_OK : PROC_FN_RET_ABORT;
|
|
}
|
|
|
|
void insert_arp_header(uint8_t *hdr, const PcktHeaderElement *headers, EthInterface *intf) {
|
|
ArpProps *arpProps = HEADER_FETCH_PROPS(ArpProps, headers);
|
|
|
|
FILL_WORD_H2N_ADVANCE(hdr, arpProps->HTYPE); // hardware type
|
|
FILL_WORD_H2N_ADVANCE(hdr, arpProps->PTYPE); // protocol type
|
|
FILL_BYTE_ADVANCE(hdr, &arpProps->HLEN); // hardware address size
|
|
FILL_BYTE_ADVANCE(hdr, &arpProps->PLEN); // protocol address size
|
|
FILL_WORD_H2N_ADVANCE(hdr, arpProps->OPER); // operation code
|
|
FILL_ADVANCE(hdr, &arpProps->SHA, ETH_HW_ADDR_LEN); // sender HW address
|
|
FILL_ADVANCE(hdr, &arpProps->SPA, sizeof(ip4_addr)); // sender protocol address
|
|
FILL_ADVANCE(hdr, &arpProps->THA, ETH_HW_ADDR_LEN); // target HW address
|
|
FILL_ADVANCE(hdr, &arpProps->TPA, sizeof(ip4_addr)); // target protocol address
|
|
}
|