Ethernet.c 5.29 KB
Newer Older
1 2
/*
             LUFA Library
Dean Camera's avatar
Dean Camera committed
3
     Copyright (C) Dean Camera, 2010.
4 5 6 7 8 9
              
  dean [at] fourwalledcubicle [dot] com
      www.fourwalledcubicle.com
*/

/*
Dean Camera's avatar
Dean Camera committed
10
  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11

12 13 14 15 16 17 18
  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 
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  software without specific, written prior permission.

  The author disclaim 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
34
 *  frames sent and received, deferring the processing of subpacket protocols to the appropriate
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 *  protocol handlers, such as DHCP or ARP.
 */
 
#include "Ethernet.h"

/** 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};


56
/** Processes an incoming Ethernet frame, and writes the appropriate response to the output Ethernet
57 58
 *  frame buffer if the sub protocol handlers create a valid response.
 */
59
void Ethernet_ProcessPacket(Ethernet_Frame_Info_t* FrameIN, Ethernet_Frame_Info_t* FrameOUT)
60
{
61
	DecodeEthernetFrameHeader(FrameIN);
62

63
	/* Cast the incoming Ethernet frame to the Ethernet header type */
64 65
	Ethernet_Frame_Header_t* FrameINHeader  = (Ethernet_Frame_Header_t*)&FrameIN->FrameData;
	Ethernet_Frame_Header_t* FrameOUTHeader = (Ethernet_Frame_Header_t*)&FrameOUT->FrameData;
66 67 68 69 70
	
	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) ||
71 72
	     MAC_COMPARE(&FrameINHeader->Destination, &BroadcastMACAddress)) &&
		 (SwapEndian_16(FrameIN->FrameLength) > ETHERNET_VER2_MINSIZE))
73 74 75 76 77
	{
		/* Process the packet depending on its protocol */
		switch (SwapEndian_16(FrameINHeader->EtherType))
		{
			case ETHERTYPE_ARP:
78 79
				RetSize = ARP_ProcessARPPacket(&FrameIN->FrameData[sizeof(Ethernet_Frame_Header_t)],
				                               &FrameOUT->FrameData[sizeof(Ethernet_Frame_Header_t)]);
80 81
				break;		
			case ETHERTYPE_IPV4:
82 83 84
				RetSize = IP_ProcessIPPacket(FrameIN,
				                             &FrameIN->FrameData[sizeof(Ethernet_Frame_Header_t)],
				                             &FrameOUT->FrameData[sizeof(Ethernet_Frame_Header_t)]);
85 86 87
				break;
		}
		
88
		/* Protocol processing routine has filled a response, complete the ethernet frame header */
89 90 91 92 93 94 95 96
		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 */
97 98
			FrameOUT->FrameLength           = (sizeof(Ethernet_Frame_Header_t) + RetSize);
			FrameOUT->FrameInBuffer         = true;
99 100 101 102 103 104 105
		}
	}

	/* Check if the packet was processed */
	if (RetSize != NO_PROCESS)
	{
		/* Clear the frame buffer */
106
		FrameIN->FrameInBuffer = false;
107 108 109 110 111 112
	}
}

/** Calculates the appropriate ethernet checksum, consisting of the addition of the one's
 *  compliment of each word, complimented.
 *
113 114
 *  \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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
 *
 *  \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 (uint8_t CurrWord = 0; CurrWord < (Bytes >> 1); CurrWord++)
	  Checksum += Words[CurrWord];
	  
	while (Checksum & 0xFFFF0000)
	  Checksum = ((Checksum & 0xFFFF) + (Checksum >> 16));
	
	return ~Checksum;
}