Thread: DHCP Discover

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    DHCP Discover

    Hello all,

    For a school assignment we need to make a program in C that does a correct DHCP discover (and if that works, we need to catch the answer etc). However, we've been trying to get the program to work, but there are several problems:
    1. packetsize gets increased by 2 bytes (probably from allignment problems?) (should be 1500 but is 1502, the 2 extra bytes seem to come from the dhcp_packet struct).
    2. The IP header checksum is incorrect (in Wireshark)
    3. The UDP header checksum is incorrect (in Wireshark)
    4. We don't know how to set the options for the request.

    We're using a dhcp header file from ISC (dhcpd). The code we have so far is in dhcpreq.c.

    dhcp.h
    Code:
    /* dhcp.h
    
       Protocol structures... */
    
    /*
     * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     * Copyright (c) 1995-2003 by Internet Software Consortium
     *
     * Permission to use, copy, modify, and distribute this software for any
     * purpose with or without fee is hereby granted, provided that the above
     * copyright notice and this permission notice appear in all copies.
     *
     * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     * ANY SPECIAL, DIRECT, 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.
     *
     *   Internet Systems Consortium, Inc.
     *   950 Charter Street
     *   Redwood City, CA 94063
     *   <[email protected]>
     *   http://www.isc.org/
     *
     * This software has been written for Internet Systems Consortium
     * by Ted Lemon in cooperation with Vixie Enterprises.  To learn more
     * about Internet Systems Consortium, see ``http://www.isc.org''.
     * To learn more about Vixie Enterprises, see ``http://www.vix.com''.
     */
    
    #define DHCP_UDP_OVERHEAD	(14 + /* Ethernet header */		\
    							 20 + /* IP header */			\
    							  8)   /* UDP header */
    #define DHCP_SNAME_LEN		64
    #define DHCP_FILE_LEN		128
    #define DHCP_FIXED_NON_UDP	236
    #define DHCP_FIXED_LEN		(DHCP_FIXED_NON_UDP + DHCP_UDP_OVERHEAD)
    						/* Everything but options. */
    #define DHCP_MTU_MAX		1500
    #define DHCP_OPTION_LEN		(DHCP_MTU_MAX - DHCP_FIXED_LEN)
    
    #define BOOTP_MIN_LEN		300
    #define DHCP_MIN_LEN            548
    
    struct dhcp_packet {
      	u_int8_t  op;		/* 0: Message opcode/type */
    	u_int8_t  htype;	/* 1: Hardware addr type (net/if_types.h) */
    	u_int8_t  hlen;		/* 2: Hardware addr length */
    	u_int8_t  hops;		/* 3: Number of relay agent hops from client */
    	u_int32_t xid;		/* 4: Transaction ID */
    	u_int16_t secs;		/* 8: Seconds since client started looking */
    	u_int16_t flags;	/* 10: Flag bits */
    	struct in_addr ciaddr;	/* 12: Client IP address (if already in use) */
    	struct in_addr yiaddr;	/* 16: Client IP address */
    	struct in_addr siaddr;	/* 18: IP address of next server to talk to */
    	struct in_addr giaddr;	/* 20: DHCP relay agent IP address */
    	unsigned char chaddr [16];	/* 24: Client hardware address */
    	char sname [DHCP_SNAME_LEN];	/* 40: Server name */
    	char file [DHCP_FILE_LEN];	/* 104: Boot filename */
    	unsigned char options [DHCP_OPTION_LEN];
    				/* 212: Optional parameters
    				   (actual length dependent on MTU). */
    };
    
    /* BOOTP (rfc951) message types */
    #define	BOOTREQUEST	1
    #define BOOTREPLY	2
    
    /* Possible values for flags field... */
    #define BOOTP_BROADCAST 32768L
    
    /* Possible values for hardware type (htype) field... */
    #define HTYPE_ETHER	1               /* Ethernet 10Mbps              */
    #define HTYPE_IEEE802	6               /* IEEE 802.2 Token Ring...	*/
    #define HTYPE_FDDI	8		/* FDDI...			*/
    
    /* Magic cookie validating dhcp options field (and bootp vendor
       extensions field). */
    #define DHCP_OPTIONS_COOKIE	"\143\202\123\143"
    
    /* DHCP Option codes: */
    
    #define DHO_PAD				0
    #define DHO_SUBNET_MASK			1
    #define DHO_TIME_OFFSET			2
    #define DHO_ROUTERS			3
    #define DHO_TIME_SERVERS		4
    #define DHO_NAME_SERVERS		5
    #define DHO_DOMAIN_NAME_SERVERS		6
    #define DHO_LOG_SERVERS			7
    #define DHO_COOKIE_SERVERS		8
    #define DHO_LPR_SERVERS			9
    #define DHO_IMPRESS_SERVERS		10
    #define DHO_RESOURCE_LOCATION_SERVERS	11
    #define DHO_HOST_NAME			12
    #define DHO_BOOT_SIZE			13
    #define DHO_MERIT_DUMP			14
    #define DHO_DOMAIN_NAME			15
    #define DHO_SWAP_SERVER			16
    #define DHO_ROOT_PATH			17
    #define DHO_EXTENSIONS_PATH		18
    #define DHO_IP_FORWARDING		19
    #define DHO_NON_LOCAL_SOURCE_ROUTING	20
    #define DHO_POLICY_FILTER		21
    #define DHO_MAX_DGRAM_REASSEMBLY	22
    #define DHO_DEFAULT_IP_TTL		23
    #define DHO_PATH_MTU_AGING_TIMEOUT	24
    #define DHO_PATH_MTU_PLATEAU_TABLE	25
    #define DHO_INTERFACE_MTU		26
    #define DHO_ALL_SUBNETS_LOCAL		27
    #define DHO_BROADCAST_ADDRESS		28
    #define DHO_PERFORM_MASK_DISCOVERY	29
    #define DHO_MASK_SUPPLIER		30
    #define DHO_ROUTER_DISCOVERY		31
    #define DHO_ROUTER_SOLICITATION_ADDRESS	32
    #define DHO_STATIC_ROUTES		33
    #define DHO_TRAILER_ENCAPSULATION	34
    #define DHO_ARP_CACHE_TIMEOUT		35
    #define DHO_IEEE802_3_ENCAPSULATION	36
    #define DHO_DEFAULT_TCP_TTL		37
    #define DHO_TCP_KEEPALIVE_INTERVAL	38
    #define DHO_TCP_KEEPALIVE_GARBAGE	39
    #define DHO_NIS_DOMAIN			40
    #define DHO_NIS_SERVERS			41
    #define DHO_NTP_SERVERS			42
    #define DHO_VENDOR_ENCAPSULATED_OPTIONS	43
    #define DHO_NETBIOS_NAME_SERVERS	44
    #define DHO_NETBIOS_DD_SERVER		45
    #define DHO_NETBIOS_NODE_TYPE		46
    #define DHO_NETBIOS_SCOPE		47
    #define DHO_FONT_SERVERS		48
    #define DHO_X_DISPLAY_MANAGER		49
    #define DHO_DHCP_REQUESTED_ADDRESS	50
    #define DHO_DHCP_LEASE_TIME		51
    #define DHO_DHCP_OPTION_OVERLOAD	52
    #define DHO_DHCP_MESSAGE_TYPE		53
    #define DHO_DHCP_SERVER_IDENTIFIER	54
    #define DHO_DHCP_PARAMETER_REQUEST_LIST	55
    #define DHO_DHCP_MESSAGE		56
    #define DHO_DHCP_MAX_MESSAGE_SIZE	57
    #define DHO_DHCP_RENEWAL_TIME		58
    #define DHO_DHCP_REBINDING_TIME		59
    #define DHO_VENDOR_CLASS_IDENTIFIER	60
    #define DHO_DHCP_CLIENT_IDENTIFIER	61
    #define DHO_NWIP_DOMAIN_NAME		62
    #define DHO_NWIP_SUBOPTIONS		63
    #define DHO_USER_CLASS			77
    #define DHO_FQDN			81
    #define DHO_DHCP_AGENT_OPTIONS		82
    #define DHO_SUBNET_SELECTION		118 /* RFC3011! */
    /* The DHO_AUTHENTICATE option is not a standard yet, so I've
       allocated an option out of the "local" option space for it on a
       temporary basis.  Once an option code number is assigned, I will
       immediately and shamelessly break this, so don't count on it
       continuing to work. */
    #define DHO_AUTHENTICATE		210
    
    #define DHO_END				255
    
    /* DHCP message types. */
    #define DHCPDISCOVER	1
    #define DHCPOFFER	2
    #define DHCPREQUEST	3
    #define DHCPDECLINE	4
    #define DHCPACK		5
    #define DHCPNAK		6
    #define DHCPRELEASE	7
    #define DHCPINFORM	8
    
    /* Relay Agent Information option subtypes: */
    #define RAI_CIRCUIT_ID	1
    #define RAI_REMOTE_ID	2
    #define RAI_AGENT_ID	3
    
    /* FQDN suboptions: */
    #define FQDN_NO_CLIENT_UPDATE		1
    #define FQDN_SERVER_UPDATE		2
    #define FQDN_ENCODED			3
    #define FQDN_RCODE1			4
    #define FQDN_RCODE2			5
    #define FQDN_HOSTNAME			6
    #define FQDN_DOMAINNAME			7
    #define FQDN_FQDN			8
    #define FQDN_SUBOPTION_COUNT		8
    dhcpreq.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <sys/ioctl.h>
    
    #include <linux/if.h>
    #include <linux/if_packet.h>
    
    #include <net/ethernet.h>
    
    #include <netinet/in.h>
    #include <netinet/ether.h>
    #include <netinet/ip.h>
    #include <netinet/udp.h>
    
    #include <arpa/inet.h>
    
    #include "dhcp.h"
    
    int main(int argc, char *argv[])
    {
    	int sock;
    
    	unsigned int packetsize = (sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(struct dhcp_packet));
    	unsigned char packet[packetsize];
    
    	printf("Packetsize %d\n", packetsize);
    	printf("DHCP_OPTION_LEN %d\n", DHCP_OPTION_LEN);
    	printf("DHCP_FIXED_LEN %d\n", DHCP_FIXED_LEN);
    
    	printf("Packet size min dhcp %d\n", (sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct udphdr)));
    
    	struct sockaddr_ll addr;
    
    	struct ether_header *eth = (struct ether_header *) (packet);
    	struct iphdr *ip = (struct iphdr *) (packet + sizeof(struct ether_header));
    	struct udphdr *udp = (struct udphdr *) (packet + sizeof(struct iphdr) + sizeof(struct ether_header));
    	struct dhcp_packet *dhcp =  (struct dhcp_packet *) (packet + sizeof(struct udphdr) + sizeof(struct iphdr) + sizeof(struct ether_header));
    
    	struct ifreq ifreq;
    
    	char SourceHwaddr[17];
    
    	//If other than one argument is giving error!
    	if (argc != 2)
    	{
    		fprintf(stderr, "Usages: dhcpreq <INTERFACE>\n");
    		exit(1);
    	}
    
    	//Create a UDP Socket with IP Protocol
    	if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) == -1)
    	//if ((sock = socket(PF_PACKET, SOCK_DGRAM, IPPROTO_IP)) == -1)
    	{
    		perror("creating socket");
    		exit(0);
    	}
    
    	//Set the interface name to argv[1] to define what interface we have
    	strcpy(ifreq.ifr_name, argv[1]);
    
    	//Get the hardware address
    	if (ioctl(sock, SIOCGIFHWADDR, &ifreq) != 0)
    	{
    		perror("ioctl get hwaddr");
    		exit(0);
    	}
    
    	//Put the hardware address in a chararray
    	sprintf(SourceHwaddr, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char) ifreq.ifr_hwaddr.sa_data[0], (unsigned char) ifreq.ifr_hwaddr.sa_data[1],
    			(unsigned char) ifreq.ifr_hwaddr.sa_data[2], (unsigned char) ifreq.ifr_hwaddr.sa_data[3], (unsigned char) ifreq.ifr_hwaddr.sa_data[4],
    			(unsigned char) ifreq.ifr_hwaddr.sa_data[5]);
    
    	/*
    	 * Begin Ethernet Header
    	 */
    
    	//Destination ethernet address
    	memcpy(eth->ether_dhost, ether_aton("ff:ff:ff:ff:ff:ff"), ETH_ALEN);
    
    	//Source ethernet address
    	memcpy(eth->ether_shost, ether_aton(SourceHwaddr), ETH_ALEN);
    
    	//Ethernet type
    	eth->ether_type = htons(ETH_P_IP);
    
    	/*
    	 * End Ethernet Header
    	 */
    
    	/*
    	 * Begin IP Header
    	 */
    
    	//Set the type of service
    	ip->tos = 0;
    
    	//Set the IP version
    	ip->version = 4;
    
    	//Set the IP header length
    	ip->ihl = sizeof(struct iphdr) >> 2;
    
    	//Set the total length
    	ip->tot_len = htons(sizeof(struct iphdr) + sizeof(struct udphdr)  + sizeof(struct dhcp_packet));
    
    	//Set the id
    	ip->id = htons((int) (rand() / (((double) RAND_MAX + 1) / 14095)));
    
    	//Set the fragment offset
    	ip->frag_off = 0;
    
    	//Set the TTL
    	ip->ttl = 128;
    
    	//Set the protocol
    	ip->protocol = IPPROTO_UDP;
    
    	//Let IP set the checksum
    
    	//Set the source IP
    	ip->saddr = inet_addr("0.0.0.0");
    	//Set the destenations IP
    	ip->daddr = inet_addr("255.255.255.255");
    	/*
    	 * End IP Header
    	 */
    
    
    	/*
    	 * Begin UDP Header
    	 */
    
    	//Set the source port
    	udp->source = htons(67);
    
    	//Set the desentation port
    	udp->dest = htons(68);
    
    	//Set the UDP length (incl dhcp packet)
    	udp->len = htons(sizeof(struct udphdr) + sizeof(struct dhcp_packet));
    
    	/*
    	 * End UDP Header
    	 */
    
    	/*
    	 * Begin DHCP Packet
    	 */
    	dhcp->op = BOOTREQUEST;
    	dhcp->htype = HTYPE_ETHER;
    	dhcp->hlen = ETH_ALEN;
    	dhcp->hops = 0;
    
    	dhcp->secs = 0;
    	dhcp->flags = htons(BOOTP_BROADCAST);
    
    
    	inet_aton("0.0.0.0", (struct in_addr *) &dhcp->ciaddr);
    	inet_aton("0.0.0.0", (struct in_addr *) &dhcp->yiaddr);
    	inet_aton("0.0.0.0", (struct in_addr *) &dhcp->siaddr);
    	inet_aton("0.0.0.0", (struct in_addr *) &dhcp->giaddr);
    
    	//Copy the MAC address from ifreq
    	memcpy(dhcp->chaddr, &ifreq.ifr_addr, ETHER_ADDR_LEN);
    
    	//Servername must be null
    	bzero(dhcp->sname, sizeof(dhcp->sname));
    
    	//Filename must be null
    	bzero(dhcp->file, sizeof(dhcp->file));
    
    	//Must bu filled in
    	bzero(dhcp->options, sizeof(dhcp->options));
    
    	/*
    	 * End DHCP Packet
    	 */
    
    	//clear the addr and set the data in it
    	memset(&addr, 0, sizeof(struct sockaddr_ll));
    	addr.sll_family = PF_PACKET;
    	addr.sll_protocol = htons(ETH_P_ARP);
    
    	if (ioctl(sock, SIOCGIFINDEX, &ifreq) != 0)
    	{
    		perror("ioctl get index");
    		exit(0);
    	}
    	addr.sll_ifindex = ifreq.ifr_ifindex;
    	printf("Interface index: %d\n", ifreq.ifr_ifindex);
    
    	//Try to bind the socket on a address
    	if (bind(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_ll)) != 0)
    	{
    		perror("Socket Binding error");
    		exit(0);
    	}
    
    	//Write the packet to the socket
    	int n = 0;
    	if ((n = write(sock, packet, packetsize)) <= 0)
    	{
    		perror("Packet sending error");
    		exit(0);
    	}
    	printf("%d bytes sent\n", n);
    
    	close(sock);
    
    	return 0;
    
    }
    The main problem is that our teachers have no idea how to do this either because they haven't even made the program them selfs yet. There also is no information available on the internet that can help us any further (google has no results when you search for the things we want).
    So if anyone can point us into the right direction with one of the problems it would be great.

    Thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    checksums for outgoing packets in Wireshark could be shown as incorrect because they are calculated and put in place by driver AFTER the packet is captured by Wireshark.

    You should check the checksum of incoming packet. To do so run the Wireshark capture on the destination computer.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Xandux View Post
    4. We don't know how to set the options for the request.
    By options - do you mean the arguments to the program? If so they are listed in this line of code
    Code:
    if (argc != 2)
    {
        fprintf(stderr, "Usages: dhcpreq <INTERFACE>\n");
        exit(1);
    }

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Quote Originally Posted by itCbitC View Post
    By options - do you mean the arguments to the program? If so they are listed in this line of code
    Code:
    if (argc != 2)
    {
        fprintf(stderr, "Usages: dhcpreq <INTERFACE>\n");
        exit(1);
    }
    That's not the problem, since we made the dhcpreq.c ourselfs we know basic things like that. I was talking about the DHCP options that have to be set before the DHCP request can be send.

    Thanks for the answers so far .

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Xandux View Post
    1. packetsize gets increased by 2 bytes (probably from allignment problems?) (should be 1500 but is 1502, the 2 extra bytes seem to come from the dhcp_packet struct).
    So don't memcopy full struct. Copy it byte,byte...

    instead of
    Code:
    struct ether_header *eth = (struct ether_header *) (packet);
    use anattached struct
    Code:
    struct ether_header eth = {0};
    fill it not inplace
    and write function
    Code:
    PackEthHeader(struct ether_header* src, unsigned char* dst)
    which will convert each member of struct to buffer one-by-one avoiding alignment problems

    do it for each header...

    On receiving side - do the opposite operation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DHCP & routing table
    By Cactus_Hugger in forum Tech Board
    Replies: 6
    Last Post: 09-04-2009, 11:39 AM
  2. writing dhcp client code using sockets on Linux
    By dash in forum Linux Programming
    Replies: 3
    Last Post: 08-04-2009, 10:20 AM
  3. DHCP & Router
    By Cactus_Hugger in forum Tech Board
    Replies: 1
    Last Post: 07-26-2009, 10:05 PM

Tags for this Thread