Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ethernet.c 5.2KB

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