]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Device/LowLevel/RNDISEthernet/Lib/Ethernet.c
Squashed 'tmk_core/' changes from d5c5ac6..8da1898
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Device / LowLevel / RNDISEthernet / Lib / Ethernet.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaims all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Ethernet frame packet handling routines. This protocol handles the processing of raw Ethernet
34 * frames sent and received, deferring the processing of sub-packet protocols to the appropriate
35 * protocol handlers, such as DHCP or ARP.
36 */
37
38 #include "Ethernet.h"
39
40 /** Ethernet Frame buffer structure, to hold the incoming Ethernet frame from the host. */
41 Ethernet_Frame_Info_t FrameIN;
42
43 /** Ethernet Frame buffer structure, to hold the outgoing Ethernet frame to the host. */
44 Ethernet_Frame_Info_t FrameOUT;
45
46 /** Constant for convenience when checking against or setting a MAC address to the virtual server MAC address. */
47 const MAC_Address_t ServerMACAddress = {SERVER_MAC_ADDRESS};
48
49 /** Constant for convenience when checking against or setting an IP address to the virtual server IP address. */
50 const IP_Address_t ServerIPAddress = {SERVER_IP_ADDRESS};
51
52 /** Constant for convenience when checking against or setting a MAC address to the broadcast MAC address. */
53 const MAC_Address_t BroadcastMACAddress = {BROADCAST_MAC_ADDRESS};
54
55 /** Constant for convenience when checking against or setting a IP address to the broadcast IP address. */
56 const IP_Address_t BroadcastIPAddress = {BROADCAST_IP_ADDRESS};
57
58 /** Constant for convenience when checking against or setting an IP address to the client (host) IP address. */
59 const IP_Address_t ClientIPAddress = {CLIENT_IP_ADDRESS};
60
61
62 /** Processes an incoming Ethernet frame, and writes the appropriate response to the output Ethernet
63 * frame buffer if the sub protocol handlers create a valid response.
64 */
65 void Ethernet_ProcessPacket(void)
66 {
67 DecodeEthernetFrameHeader(FrameIN.FrameData);
68
69 /* Cast the incoming Ethernet frame to the Ethernet header type */
70 Ethernet_Frame_Header_t* FrameINHeader = (Ethernet_Frame_Header_t*)&FrameIN.FrameData;
71 Ethernet_Frame_Header_t* FrameOUTHeader = (Ethernet_Frame_Header_t*)&FrameOUT.FrameData;
72
73 int16_t RetSize = NO_RESPONSE;
74
75 /* Ensure frame is addressed to either all (broadcast) or the virtual webserver, and is a type II frame */
76 if ((MAC_COMPARE(&FrameINHeader->Destination, &ServerMACAddress) ||
77 MAC_COMPARE(&FrameINHeader->Destination, &BroadcastMACAddress)) &&
78 (SwapEndian_16(FrameIN.FrameLength) > ETHERNET_VER2_MINSIZE))
79 {
80 /* Process the packet depending on its protocol */
81 switch (SwapEndian_16(FrameINHeader->EtherType))
82 {
83 case ETHERTYPE_ARP:
84 RetSize = ARP_ProcessARPPacket(&FrameIN.FrameData[sizeof(Ethernet_Frame_Header_t)],
85 &FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t)]);
86 break;
87 case ETHERTYPE_IPV4:
88 RetSize = IP_ProcessIPPacket(&FrameIN.FrameData[sizeof(Ethernet_Frame_Header_t)],
89 &FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t)]);
90 break;
91 }
92
93 /* Protocol processing routine has filled a response, complete the ethernet frame header */
94 if (RetSize > 0)
95 {
96 /* Fill out the response Ethernet frame header */
97 FrameOUTHeader->Source = ServerMACAddress;
98 FrameOUTHeader->Destination = FrameINHeader->Source;
99 FrameOUTHeader->EtherType = FrameINHeader->EtherType;
100
101 /* Set the response length in the buffer and indicate that a response is ready to be sent */
102 FrameOUT.FrameLength = (sizeof(Ethernet_Frame_Header_t) + RetSize);
103 }
104 }
105
106 /* Check if the packet was processed */
107 if (RetSize != NO_PROCESS)
108 {
109 /* Clear the frame buffer */
110 FrameIN.FrameLength = 0;
111 }
112 }
113
114 /** Calculates the appropriate ethernet checksum, consisting of the addition of the one's
115 * compliment of each word, complimented.
116 *
117 * \param[in] Data Pointer to the packet buffer data whose checksum must be calculated
118 * \param[in] Bytes Number of bytes in the data buffer to process
119 *
120 * \return A 16-bit Ethernet checksum value
121 */
122 uint16_t Ethernet_Checksum16(void* Data,
123 uint16_t Bytes)
124 {
125 uint16_t* Words = (uint16_t*)Data;
126 uint32_t Checksum = 0;
127
128 for (uint16_t CurrWord = 0; CurrWord < (Bytes >> 1); CurrWord++)
129 Checksum += Words[CurrWord];
130
131 while (Checksum & 0xFFFF0000)
132 Checksum = ((Checksum & 0xFFFF) + (Checksum >> 16));
133
134 return ~Checksum;
135 }
136
Imprint / Impressum