]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/RNDISEthernet/Lib/Ethernet.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / ClassDriver / 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 /** Constant for convenience when checking against or setting a MAC address to the virtual server MAC address. */
41 const MAC_Address_t ServerMACAddress = {SERVER_MAC_ADDRESS};
42
43 /** Constant for convenience when checking against or setting an IP address to the virtual server IP address. */
44 const IP_Address_t ServerIPAddress = {SERVER_IP_ADDRESS};
45
46 /** Constant for convenience when checking against or setting a MAC address to the broadcast MAC address. */
47 const MAC_Address_t BroadcastMACAddress = {BROADCAST_MAC_ADDRESS};
48
49 /** Constant for convenience when checking against or setting a IP address to the broadcast IP address. */
50 const IP_Address_t BroadcastIPAddress = {BROADCAST_IP_ADDRESS};
51
52 /** Constant for convenience when checking against or setting an IP address to the client (host) IP address. */
53 const IP_Address_t ClientIPAddress = {CLIENT_IP_ADDRESS};
54
55
56 /** Processes an incoming Ethernet frame, and writes the appropriate response to the output Ethernet
57 * frame buffer if the sub protocol handlers create a valid response.
58 */
59 void Ethernet_ProcessPacket(Ethernet_Frame_Info_t* const FrameIN,
60 Ethernet_Frame_Info_t* const FrameOUT)
61 {
62 DecodeEthernetFrameHeader(FrameIN->FrameData);
63
64 /* Cast the incoming Ethernet frame to the Ethernet header type */
65 Ethernet_Frame_Header_t* FrameINHeader = (Ethernet_Frame_Header_t*)&FrameIN->FrameData;
66 Ethernet_Frame_Header_t* FrameOUTHeader = (Ethernet_Frame_Header_t*)&FrameOUT->FrameData;
67
68 int16_t RetSize = NO_RESPONSE;
69
70 /* Ensure frame is addressed to either all (broadcast) or the virtual webserver, and is a type II frame */
71 if ((MAC_COMPARE(&FrameINHeader->Destination, &ServerMACAddress) ||
72 MAC_COMPARE(&FrameINHeader->Destination, &BroadcastMACAddress)) &&
73 (SwapEndian_16(FrameIN->FrameLength) > ETHERNET_VER2_MINSIZE))
74 {
75 /* Process the packet depending on its protocol */
76 switch (SwapEndian_16(FrameINHeader->EtherType))
77 {
78 case ETHERTYPE_ARP:
79 RetSize = ARP_ProcessARPPacket(&FrameIN->FrameData[sizeof(Ethernet_Frame_Header_t)],
80 &FrameOUT->FrameData[sizeof(Ethernet_Frame_Header_t)]);
81 break;
82 case ETHERTYPE_IPV4:
83 RetSize = IP_ProcessIPPacket(FrameIN,
84 &FrameIN->FrameData[sizeof(Ethernet_Frame_Header_t)],
85 &FrameOUT->FrameData[sizeof(Ethernet_Frame_Header_t)]);
86 break;
87 }
88
89 /* Protocol processing routine has filled a response, complete the ethernet frame header */
90 if (RetSize > 0)
91 {
92 /* Fill out the response Ethernet frame header */
93 FrameOUTHeader->Source = ServerMACAddress;
94 FrameOUTHeader->Destination = FrameINHeader->Source;
95 FrameOUTHeader->EtherType = FrameINHeader->EtherType;
96
97 /* Set the response length in the buffer and indicate that a response is ready to be sent */
98 FrameOUT->FrameLength = (sizeof(Ethernet_Frame_Header_t) + RetSize);
99 }
100 }
101
102 /* Check if the packet was processed */
103 if (RetSize != NO_PROCESS)
104 {
105 /* Clear the frame buffer */
106 FrameIN->FrameLength = 0;
107 }
108 }
109
110 /** Calculates the appropriate ethernet checksum, consisting of the addition of the one's
111 * compliment of each word, complimented.
112 *
113 * \param[in] Data Pointer to the packet buffer data whose checksum must be calculated
114 * \param[in] Bytes Number of bytes in the data buffer to process
115 *
116 * \return A 16-bit Ethernet checksum value
117 */
118 uint16_t Ethernet_Checksum16(void* Data,
119 uint16_t Bytes)
120 {
121 uint16_t* Words = (uint16_t*)Data;
122 uint32_t Checksum = 0;
123
124 for (uint16_t CurrWord = 0; CurrWord < (Bytes >> 1); CurrWord++)
125 Checksum += Words[CurrWord];
126
127 while (Checksum & 0xFFFF0000)
128 Checksum = ((Checksum & 0xFFFF) + (Checksum >> 16));
129
130 return ~Checksum;
131 }
132
Imprint / Impressum