DHCP.c 5.17 KB
Newer Older
1 2
/*
             LUFA Library
3
     Copyright (C) Dean Camera, 2011.
4

5
  dean [at] fourwalledcubicle [dot] com
6
           www.lufa-lib.org
7 8 9
*/

/*
10
  Copyright 2011  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 17 18
  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 34 35 36
  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
 *
 *  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.
 */
37

38 39 40 41 42 43 44 45 46 47 48
#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
 */
49 50 51
int16_t DHCP_ProcessDHCPPacket(void* IPHeaderInStart,
                               void* DHCPHeaderInStart,
                               void* DHCPHeaderOutStart)
52 53 54 55
{
	IP_Header_t*   IPHeaderIN    = (IP_Header_t*)IPHeaderInStart;
	DHCP_Header_t* DHCPHeaderIN  = (DHCP_Header_t*)DHCPHeaderInStart;
	DHCP_Header_t* DHCPHeaderOUT = (DHCP_Header_t*)DHCPHeaderOutStart;
56

57 58 59 60 61
	uint8_t* DHCPOptionsINStart  = (uint8_t*)(DHCPHeaderInStart  + sizeof(DHCP_Header_t));
	uint8_t* DHCPOptionsOUTStart = (uint8_t*)(DHCPHeaderOutStart + sizeof(DHCP_Header_t));

	DecodeDHCPHeader(DHCPHeaderInStart);

62
	/* Zero out the response DHCP packet, as much of it is legacy and left at 0 */
63 64 65 66 67 68 69 70 71 72 73
	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;
74
	memmove(&DHCPHeaderOUT->ClientHardwareAddress, &DHCPHeaderIN->ClientHardwareAddress, sizeof(MAC_Address_t));
75
	DHCPHeaderOUT->Cookie                = SwapEndian_32(DHCP_MAGIC_COOKIE);
76

77 78 79 80 81 82 83
	/* 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)
84
	{
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		/* 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++) = 4;
				*(DHCPOptionsOUTStart++) = 0xFF;
				*(DHCPOptionsOUTStart++) = 0xFF;
				*(DHCPOptionsOUTStart++) = 0xFF;
				*(DHCPOptionsOUTStart++) = 0x00;

				*(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;
110

111 112 113
				return (sizeof(DHCP_Header_t) + 12 + sizeof(IP_Address_t));
			}
		}
114

115 116 117
		/* 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));
	}
118

119 120
	return NO_RESPONSE;
}
121