diff options
author | Jack Humbert <jack.humb@gmail.com> | 2017-07-07 11:55:23 -0400 |
---|---|---|
committer | Jack Humbert <jack.humb@gmail.com> | 2017-07-07 11:55:23 -0400 |
commit | 8655d4f4948b2deef7844503c8d690f23ac1a062 (patch) | |
tree | b2c6effc9d6cd5b5b43933a1e53b8bf17e9e82cf /lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib | |
parent | 1896c76a2928c96f9ab7947bec2ef8dd37623cff (diff) | |
parent | 60b30c036397cb5627fa374bb930794b225daa29 (diff) |
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
Diffstat (limited to 'lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib')
21 files changed, 3233 insertions, 0 deletions
diff --git a/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/ARP.c b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/ARP.c new file mode 100644 index 0000000000..24008705c1 --- /dev/null +++ b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/ARP.c @@ -0,0 +1,87 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Address Resolution Protocol (ARP) packet handling routines. This protocol handles the + * conversion of physical MAC addresses to protocol IP addresses between the host and the + * device. + */ + +#include "ARP.h" + +/** Processes an ARP packet inside an Ethernet frame, and writes the appropriate response + * to the output Ethernet frame if the host is requesting the IP or MAC address of the + * virtual server device on the network. + * + * \param[in] InDataStart Pointer to the start of the incoming packet's ARP header + * \param[out] OutDataStart Pointer to the start of the outgoing packet's ARP header + * + * \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE otherwise + */ +int16_t ARP_ProcessARPPacket(void* InDataStart, + void* OutDataStart) +{ + DecodeARPHeader(InDataStart); + + ARP_Header_t* ARPHeaderIN = (ARP_Header_t*)InDataStart; + ARP_Header_t* ARPHeaderOUT = (ARP_Header_t*)OutDataStart; + + /* Ensure that the ARP request is a IPv4 request packet */ + if ((SwapEndian_16(ARPHeaderIN->ProtocolType) == ETHERTYPE_IPV4) && + (SwapEndian_16(ARPHeaderIN->Operation) == ARP_OPERATION_REQUEST)) + { + /* If the ARP packet is requesting the MAC or IP of the virtual webserver, return the response */ + if (IP_COMPARE(&ARPHeaderIN->TPA, &ServerIPAddress) || + MAC_COMPARE(&ARPHeaderIN->THA, &ServerMACAddress)) + { + /* Fill out the ARP response header */ + ARPHeaderOUT->HardwareType = ARPHeaderIN->HardwareType; + ARPHeaderOUT->ProtocolType = ARPHeaderIN->ProtocolType; + ARPHeaderOUT->HLEN = ARPHeaderIN->HLEN; + ARPHeaderOUT->PLEN = ARPHeaderIN->PLEN; + ARPHeaderOUT->Operation = SwapEndian_16(ARP_OPERATION_REPLY); + + /* Copy over the sender MAC/IP to the target fields for the response */ + ARPHeaderOUT->THA = ARPHeaderIN->SHA; + ARPHeaderOUT->TPA = ARPHeaderIN->SPA; + + /* Copy over the new sender MAC/IP - MAC and IP addresses of the virtual webserver */ + ARPHeaderOUT->SHA = ServerMACAddress; + ARPHeaderOUT->SPA = ServerIPAddress; + + /* Return the size of the response so far */ + return sizeof(ARP_Header_t); + } + } + + return NO_RESPONSE; +} + diff --git a/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/ARP.h b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/ARP.h new file mode 100644 index 0000000000..c809cbf44a --- /dev/null +++ b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/ARP.h @@ -0,0 +1,78 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Header file for ARP.c. + */ + +#ifndef _ARP_H_ +#define _ARP_H_ + + /* Includes: */ + #include <avr/io.h> + #include <string.h> + + #include <LUFA/Drivers/USB/USB.h> + + #include "EthernetProtocols.h" + #include "Ethernet.h" + #include "ProtocolDecoders.h" + + /* Macros: */ + /** ARP header operation constant, indicating a request from a host for an address translation. */ + #define ARP_OPERATION_REQUEST 1 + + /** ARP header operation constant, indicating a reply from a host giving an address translation. */ + #define ARP_OPERATION_REPLY 2 + + /* Type Defines: */ + /** Type define for an ARP packet inside an Ethernet frame. */ + typedef struct + { + uint16_t HardwareType; /**< Hardware type constant, indicating the hardware used */ + uint16_t ProtocolType; /**< Protocol being resolved, usually ETHERTYPE_IPV4 */ + + uint8_t HLEN; /**< Length in bytes of the source/destination hardware addresses */ + uint8_t PLEN; /**< Length in bytes of the source/destination protocol addresses */ + uint16_t Operation; /**< Type of operation, either ARP_OPERATION_REQUEST or ARP_OPERATION_REPLY */ + + MAC_Address_t SHA; /**< Sender's hardware address */ + IP_Address_t SPA; /**< Sender's protocol address */ + MAC_Address_t THA; /**< Target's hardware address */ + IP_Address_t TPA; /**< Target's protocol address */ + } ARP_Header_t; + + /* Function Prototypes: */ + int16_t ARP_ProcessARPPacket(void* InDataStart, + void* OutDataStart); + +#endif + diff --git a/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/DHCP.c b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/DHCP.c new file mode 100644 index 0000000000..6f7b40af69 --- /dev/null +++ b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/DHCP.c @@ -0,0 +1,129 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Dynamic Host Configuration Protocol (DHCP) packet handling routines. This protocol + * handles the automatic IP negotiation to the host, so that the host will use the provided + * IP address given to it by the device. + */ + +#include "DHCP.h" + +/** Processes a DHCP packet inside an Ethernet frame, and writes the appropriate response + * to the output Ethernet frame if the host is requesting or accepting an IP address. + * + * \param[in] IPHeaderInStart Pointer to the start of the incoming packet's IP header + * \param[in] DHCPHeaderInStart Pointer to the start of the incoming packet's DHCP header + * \param[out] DHCPHeaderOutStart Pointer to the start of the outgoing packet's DHCP header + * + * \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE otherwise + */ +int16_t DHCP_ProcessDHCPPacket(void* IPHeaderInStart, + void* DHCPHeaderInStart, + void* DHCPHeaderOutStart) +{ + IP_Header_t* IPHeaderIN = (IP_Header_t*)IPHeaderInStart; + DHCP_Header_t* DHCPHeaderIN = (DHCP_Header_t*)DHCPHeaderInStart; + DHCP_Header_t* DHCPHeaderOUT = (DHCP_Header_t*)DHCPHeaderOutStart; + + uint8_t* DHCPOptionsINStart = (uint8_t*)(DHCPHeaderInStart + sizeof(DHCP_Header_t)); + uint8_t* DHCPOptionsOUTStart = (uint8_t*)(DHCPHeaderOutStart + sizeof(DHCP_Header_t)); + + DecodeDHCPHeader(DHCPHeaderInStart); + + /* Zero out the response DHCP packet, as much of it is legacy and left at 0 */ + memset(DHCPHeaderOUT, 0, sizeof(DHCP_Header_t)); + + /* Fill out the response DHCP packet */ + DHCPHeaderOUT->HardwareType = DHCPHeaderIN->HardwareType; + DHCPHeaderOUT->Operation = DHCP_OP_BOOTREPLY; + DHCPHeaderOUT->HardwareAddressLength = DHCPHeaderIN->HardwareAddressLength; + DHCPHeaderOUT->Hops = 0; + DHCPHeaderOUT->TransactionID = DHCPHeaderIN->TransactionID; + DHCPHeaderOUT->ElapsedSeconds = 0; + DHCPHeaderOUT->Flags = DHCPHeaderIN->Flags; + DHCPHeaderOUT->YourIP = ClientIPAddress; + memmove(&DHCPHeaderOUT->ClientHardwareAddress, &DHCPHeaderIN->ClientHardwareAddress, sizeof(MAC_Address_t)); + DHCPHeaderOUT->Cookie = SwapEndian_32(DHCP_MAGIC_COOKIE); + + /* Alter the incoming IP packet header so that the corrected IP source and destinations are used - this means that + when the response IP header is generated, it will use the corrected addresses and not the null/broatcast addresses */ + IPHeaderIN->SourceAddress = ClientIPAddress; + IPHeaderIN->DestinationAddress = ServerIPAddress; + + /* Process the incoming DHCP packet options */ + while (DHCPOptionsINStart[0] != DHCP_OPTION_END) + { + /* Find the Message Type DHCP option, to determine the type of DHCP packet */ + if (DHCPOptionsINStart[0] == DHCP_OPTION_MESSAGETYPE) + { + if ((DHCPOptionsINStart[2] == DHCP_MESSAGETYPE_DISCOVER) || (DHCPOptionsINStart[2] == DHCP_MESSAGETYPE_REQUEST)) + { + /* Fill out the response DHCP packet options for a DHCP OFFER or ACK response */ + + *(DHCPOptionsOUTStart++) = DHCP_OPTION_MESSAGETYPE; + *(DHCPOptionsOUTStart++) = 1; + *(DHCPOptionsOUTStart++) = (DHCPOptionsINStart[2] == DHCP_MESSAGETYPE_DISCOVER) ? DHCP_MESSAGETYPE_OFFER + : DHCP_MESSAGETYPE_ACK; + + *(DHCPOptionsOUTStart++) = DHCP_OPTION_SUBNETMASK; + *(DHCPOptionsOUTStart++) = sizeof(IP_Address_t); + *(DHCPOptionsOUTStart++) = 0xFF; + *(DHCPOptionsOUTStart++) = 0xFF; + *(DHCPOptionsOUTStart++) = 0xFF; + *(DHCPOptionsOUTStart++) = 0x00; + + *(DHCPOptionsOUTStart++) = DHCP_OPTION_LEASETIME; + *(DHCPOptionsOUTStart++) = sizeof(uint32_t); + /* Lease Time 86400s (ONE_DAY) */ + *(DHCPOptionsOUTStart++) = 0x00; + *(DHCPOptionsOUTStart++) = 0x01; + *(DHCPOptionsOUTStart++) = 0x51; + *(DHCPOptionsOUTStart++) = 0x80; + + *(DHCPOptionsOUTStart++) = DHCP_OPTION_DHCPSERVER; + *(DHCPOptionsOUTStart++) = sizeof(IP_Address_t); + memcpy(DHCPOptionsOUTStart, &ServerIPAddress, sizeof(IP_Address_t)); + DHCPOptionsOUTStart += sizeof(IP_Address_t); + + *(DHCPOptionsOUTStart++) = DHCP_OPTION_END; + + return (sizeof(DHCP_Header_t) + 18 + sizeof(IP_Address_t)); + } + } + + /* Go to the next DHCP option - skip one byte if option is a padding byte, else skip the complete option's size */ + DHCPOptionsINStart += ((DHCPOptionsINStart[0] == DHCP_OPTION_PAD) ? 1 : (DHCPOptionsINStart[1] + 2)); + } + + return NO_RESPONSE; +} + diff --git a/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/DHCP.h b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/DHCP.h new file mode 100644 index 0000000000..5ef78469ec --- /dev/null +++ b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/DHCP.h @@ -0,0 +1,131 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Header file for DHCP.c. + */ + +#ifndef _DHCP_H_ +#define _DHCP_H_ + + /* Includes: */ + #include <avr/io.h> + #include <string.h> + + #include "EthernetProtocols.h" + #include "Ethernet.h" + #include "ProtocolDecoders.h" + + /* Macros: */ + /** DHCP operation constant, indicating a request from a host to a DHCP server. */ + #define DHCP_OP_BOOTREQUEST 0x01 + + /** DHCP operation constant, indicating a reply from a DHCP server to a host. */ + #define DHCP_OP_BOOTREPLY 0x02 + + /** Hardware type constant, indicating Ethernet as a carrier. */ + #define DHCP_HTYPE_ETHERNET 0x01 + + /** Magic boot protocol "cookie", inserted into all BOOTP packets (BOOTP is the carrier of DHCP). */ + #define DHCP_MAGIC_COOKIE 0x63825363 + + /** DHCP option list entry header, indicating that a subnet mask will follow. */ + #define DHCP_OPTION_SUBNETMASK 1 + + /** DHCP option list entry header, indicating that the Lease Time will follow. */ + #define DHCP_OPTION_LEASETIME 51 + + /** DHCP option list entry header, indicating that the DHCP message type constant will follow. */ + #define DHCP_OPTION_MESSAGETYPE 53 + + /** DHCP option list entry header, indicating that the IP address of the DHCP server will follow. */ + #define DHCP_OPTION_DHCPSERVER 54 + + /** DHCP option list entry header, used to pad out option data. */ + #define DHCP_OPTION_PAD 0 + + /** DHCP option list entry header, indicating the end of option data. */ + #define DHCP_OPTION_END 255 + + /** Message type constant, used in the DHCP option data field, requesting that a DHCP server offer an IP address. */ + #define DHCP_MESSAGETYPE_DISCOVER 1 + + /** Message type constant, used in the DHCP option data field, indicating that a DHCP server is offering an IP address. */ + #define DHCP_MESSAGETYPE_OFFER 2 + + /** Message type constant, used in the DHCP option data field, requesting that a DHCP server lease a given IP address. */ + #define DHCP_MESSAGETYPE_REQUEST 3 + + /** Message type constant, used in the DHCP option data field, declining an offered DHCP server IP address lease. */ + #define DHCP_MESSAGETYPE_DECLINE 4 + + /** Message type constant, used in the DHCP option data field, ACKing a host IP lease request. */ + #define DHCP_MESSAGETYPE_ACK 5 + + /** Message type constant, used in the DHCP option data field, NACKing a host IP lease request. */ + #define DHCP_MESSAGETYPE_NACK 6 + + /** Message type constant, used in the DHCP option data field, indicating that a host is releasing a leased IP address. */ + #define DHCP_MESSAGETYPE_RELEASE 7 + + /* Type Defines: */ + /** Type define for a DHCP packet inside an Ethernet frame. */ + typedef struct + { + uint8_t Operation; /**< DHCP operation, either DHCP_OP_BOOTREQUEST or DHCP_OP_BOOTREPLY */ + uint8_t HardwareType; /**< Hardware carrier type constant */ + uint8_t HardwareAddressLength; /**< Length in bytes of a hardware (MAC) address on the network */ + uint8_t Hops; /**< Number of hops required to reach the server, unused */ + + uint32_t TransactionID; /**< Unique ID of the DHCP packet, for positive matching between sent and received packets */ + + uint16_t ElapsedSeconds; /**< Elapsed seconds since the request was made */ + uint16_t Flags; /**< BOOTP packet flags */ + + IP_Address_t ClientIP; /**< Client IP address, if already leased an IP */ + IP_Address_t YourIP; /**< Client IP address */ + IP_Address_t NextServerIP; /**< Legacy BOOTP protocol field, unused for DHCP */ + IP_Address_t RelayAgentIP; /**< Legacy BOOTP protocol field, unused for DHCP */ + + uint8_t ClientHardwareAddress[16]; /**< Hardware (MAC) address of the client making a request to the DHCP server */ + uint8_t ServerHostnameString[64]; /**< Legacy BOOTP protocol field, unused for DHCP */ + uint8_t BootFileName[128]; /**< Legacy BOOTP protocol field, unused for DHCP */ + + uint32_t Cookie; /**< Magic BOOTP protocol cookie to indicate a valid packet */ + } DHCP_Header_t; + + /* Function Prototypes: */ + int16_t DHCP_ProcessDHCPPacket(void* IPHeaderInStart, + void* DHCPHeaderInStart, + void* DHCPHeaderOutStart); + +#endif + diff --git a/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/Ethernet.c b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/Ethernet.c new file mode 100644 index 0000000000..a48de2c717 --- /dev/null +++ b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/Ethernet.c @@ -0,0 +1,136 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Ethernet frame packet handling routines. This protocol handles the processing of raw Ethernet + * frames sent and received, deferring the processing of sub-packet protocols to the appropriate + * protocol handlers, such as DHCP or ARP. + */ + +#include "Ethernet.h" + +/** Ethernet Frame buffer structure, to hold the incoming Ethernet frame from the host. */ +Ethernet_Frame_Info_t FrameIN; + +/** Ethernet Frame buffer structure, to hold the outgoing Ethernet frame to the host. */ +Ethernet_Frame_Info_t FrameOUT; + +/** Constant for convenience when checking against or setting a MAC address to the virtual server MAC address. */ +const MAC_Address_t ServerMACAddress = {SERVER_MAC_ADDRESS}; + +/** Constant for convenience when checking against or setting an IP address to the virtual server IP address. */ +const IP_Address_t ServerIPAddress = {SERVER_IP_ADDRESS}; + +/** Constant for convenience when checking against or setting a MAC address to the broadcast MAC address. */ +const MAC_Address_t BroadcastMACAddress = {BROADCAST_MAC_ADDRESS}; + +/** Constant for convenience when checking against or setting a IP address to the broadcast IP address. */ +const IP_Address_t BroadcastIPAddress = {BROADCAST_IP_ADDRESS}; + +/** Constant for convenience when checking against or setting an IP address to the client (host) IP address. */ +const IP_Address_t ClientIPAddress = {CLIENT_IP_ADDRESS}; + + +/** Processes an incoming Ethernet frame, and writes the appropriate response to the output Ethernet + * frame buffer if the sub protocol handlers create a valid response. + */ +void Ethernet_ProcessPacket(void) +{ + DecodeEthernetFrameHeader(FrameIN.FrameData); + + /* Cast the incoming Ethernet frame to the Ethernet header type */ + Ethernet_Frame_Header_t* FrameINHeader = (Ethernet_Frame_Header_t*)&FrameIN.FrameData; + Ethernet_Frame_Header_t* FrameOUTHeader = (Ethernet_Frame_Header_t*)&FrameOUT.FrameData; + + int16_t RetSize = NO_RESPONSE; + + /* Ensure frame is addressed to either all (broadcast) or the virtual webserver, and is a type II frame */ + if ((MAC_COMPARE(&FrameINHeader->Destination, &ServerMACAddress) || + MAC_COMPARE(&FrameINHeader->Destination, &BroadcastMACAddress)) && + (SwapEndian_16(FrameIN.FrameLength) > ETHERNET_VER2_MINSIZE)) + { + /* Process the packet depending on its protocol */ + switch (SwapEndian_16(FrameINHeader->EtherType)) + { + case ETHERTYPE_ARP: + RetSize = ARP_ProcessARPPacket(&FrameIN.FrameData[sizeof(Ethernet_Frame_Header_t)], + &FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t)]); + break; + case ETHERTYPE_IPV4: + RetSize = IP_ProcessIPPacket(&FrameIN.FrameData[sizeof(Ethernet_Frame_Header_t)], + &FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t)]); + break; + } + + /* Protocol processing routine has filled a response, complete the ethernet frame header */ + if (RetSize > 0) + { + /* Fill out the response Ethernet frame header */ + FrameOUTHeader->Source = ServerMACAddress; + FrameOUTHeader->Destination = FrameINHeader->Source; + FrameOUTHeader->EtherType = FrameINHeader->EtherType; + + /* Set the response length in the buffer and indicate that a response is ready to be sent */ + FrameOUT.FrameLength = (sizeof(Ethernet_Frame_Header_t) + RetSize); + } + } + + /* Check if the packet was processed */ + if (RetSize != NO_PROCESS) + { + /* Clear the frame buffer */ + FrameIN.FrameLength = 0; + } +} + +/** Calculates the appropriate ethernet checksum, consisting of the addition of the one's + * compliment of each word, complimented. + * + * \param[in] Data Pointer to the packet buffer data whose checksum must be calculated + * \param[in] Bytes Number of bytes in the data buffer to process + * + * \return A 16-bit Ethernet checksum value + */ +uint16_t Ethernet_Checksum16(void* Data, + uint16_t Bytes) +{ + uint16_t* Words = (uint16_t*)Data; + uint32_t Checksum = 0; + + for (uint16_t CurrWord = 0; CurrWord < (Bytes >> 1); CurrWord++) + Checksum += Words[CurrWord]; + + while (Checksum & 0xFFFF0000) + Checksum = ((Checksum & 0xFFFF) + (Checksum >> 16)); + + return ~Checksum; +} + diff --git a/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/Ethernet.h b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/Ethernet.h new file mode 100644 index 0000000000..8eaf64080b --- /dev/null +++ b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/Ethernet.h @@ -0,0 +1,111 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * Header file for Ethernet.c. + */ + +#ifndef _ETHERNET_H_ +#define _ETHERNET_H_ + + /* Includes: */ + #include <avr/io.h> + #include <string.h> + + #include "Config/AppConfig.h" + + #include "EthernetProtocols.h" + #include "ProtocolDecoders.h" + #include "ICMP.h" + #include "TCP.h" + #include "UDP.h" + #include "DHCP.h" + #include "ARP.h" + #include "IP.h" + + /* Macros: */ + /** Physical MAC address of the network broadcast address. */ + #define BROADCAST_MAC_ADDRESS {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} + + /** Performs a comparison between two MAC addresses, indicating if they are identical. + * + * \param[in] MAC1 First MAC address + * \param[in] MAC2 Second MAC address + * + * \return True if the addresses match, \c false otherwise + */ + #define MAC_COMPARE(MAC1, MAC2) (memcmp(MAC1, MAC2, sizeof(MAC_Address_t)) == 0) + + /** Maximum size of an incoming or outgoing Ethernet frame in bytes. */ + #define ETHERNET_FRAME_SIZE_MAX 1500 + + /** Minimum size of an Ethernet packet in bytes, to conform to the Ethernet V2 packet standard. */ + #define ETHERNET_VER2_MINSIZE 0x0600 + + /** Return value for all sub protocol handling routines, indicating that no response packet has been generated. */ + #define NO_RESPONSE 0 + + /** Return value for all sub protocol handling routines, indicating that the packet has not yet been handled. */ + #define NO_PROCESS -1 + + /* Type Defines: */ + /** Type define for an Ethernet frame buffer data and information structure. */ + typedef struct + { + uint8_t FrameData[ETHERNET_FRAME_SIZE_MAX]; /**< Ethernet frame contents. */ + uint16_t FrameLength; /**< Length in bytes of the Ethernet frame stored in the buffer. */ + } Ethernet_Frame_Info_t; + + /** Type define for an Ethernet frame header. */ + typedef struct + { + MAC_Address_t Destination; /**< Physical MAC address of the packet recipient */ + MAC_Address_t Source; /**< Physics MAC address of the packet source */ + uint16_t EtherType; /**< Ethernet packet sub-protocol type, for Ethernet V2 packets */ + } Ethernet_Frame_Header_t; + + /* External Variables: */ + extern Ethernet_Frame_Info_t FrameIN; + extern Ethernet_Frame_Info_t FrameOUT; + + extern const MAC_Address_t ServerMACAddress; + extern const IP_Address_t ServerIPAddress; + extern const MAC_Address_t BroadcastMACAddress; + extern const IP_Address_t BroadcastIPAddress; + extern const IP_Address_t ClientIPAddress; + + /* Function Prototypes: */ + void Ethernet_ProcessPacket(void); + uint16_t Ethernet_Checksum16(void* Data, + uint16_t Bytes); + +#endif + diff --git a/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/EthernetProtocols.h b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/EthernetProtocols.h new file mode 100644 index 0000000000..ca738cd0db --- /dev/null +++ b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/EthernetProtocols.h @@ -0,0 +1,88 @@ +/* + LUFA Library + Copyright (C) Dean Camera, 2017. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ + +/* + Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaims all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/** \file + * + * General Ethernet protocol constants and type defines, for use by + * all network protocol portions of the TCP/IP stack. + */ |