clientpacket.c

Go to the documentation of this file.
00001 /* clientpacket.c
00002  *
00003  * Packet generation and dispatching functions for the DHCP client.
00004  *
00005  * Russ Dill <Russ.Dill@asu.edu> July 2001
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021  
00022 #include <string.h>
00023 #include <sys/socket.h>
00024 #include <features.h>
00025 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
00026 #include <netpacket/packet.h>
00027 #include <net/ethernet.h>
00028 #else
00029 #include <asm/types.h>
00030 #include <linux/if_packet.h>
00031 #include <linux/if_ether.h>
00032 #endif
00033 #include <stdlib.h>
00034 #include <time.h>
00035 #include <unistd.h>
00036 #include <netinet/in.h>
00037 #include <arpa/inet.h>
00038 #include <errno.h>
00039 #include <sys/types.h>
00040 #include <sys/stat.h>
00041 #include <fcntl.h>
00042 
00043 
00044 #include "dhcpd.h"
00045 #include "packet.h"
00046 #include "options.h"
00047 #include "dhcpc.h"
00048 #include "debug.h"
00049 
00050 
00051 /* Create a random xid */
00052 unsigned long random_xid(void)
00053 {
00054         static int initialized;
00055         if (!initialized) {
00056                 int fd;
00057                 unsigned long seed;
00058 
00059                 fd = open("/dev/urandom", 0);
00060                 if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
00061                         LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %s",
00062                                 strerror(errno));
00063                         seed = time(0);
00064                 }
00065                 if (fd >= 0) close(fd);
00066                 srand(seed);
00067                 initialized++;
00068         }
00069         return rand();
00070 }
00071 
00072 
00073 /* initialize a packet with the proper defaults */
00074 static void init_packet(struct dhcpMessage *packet, char type)
00075 {
00076         struct vendor  {
00077                 char vendor, length;
00078                 char str[sizeof("udhcp "VERSION)];
00079         } vendor_id = { DHCP_VENDOR,  sizeof("udhcp "VERSION) - 1, "udhcp "VERSION};
00080         
00081         init_header(packet, type);
00082         memcpy(packet->chaddr, client_config.arp, 6);
00083         add_option_string(packet->options, client_config.clientid);
00084         if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
00085         add_option_string(packet->options, (unsigned char *) &vendor_id);
00086 }
00087 
00088 
00089 /* Add a paramater request list for stubborn DHCP servers. Pull the data
00090  * from the struct in options.c. Don't do bounds checking here because it
00091  * goes towards the head of the packet. */
00092 static void add_requests(struct dhcpMessage *packet)
00093 {
00094         int end = end_option(packet->options);
00095         int i, len = 0;
00096 
00097         packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
00098         for (i = 0; options[i].code; i++)
00099                 if (options[i].flags & OPTION_REQ)
00100                         packet->options[end + OPT_DATA + len++] = options[i].code;
00101         packet->options[end + OPT_LEN] = len;
00102         packet->options[end + OPT_DATA + len] = DHCP_END;
00103 
00104 }
00105 
00106 
00107 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
00108 int send_discover(unsigned long xid, unsigned long requested)
00109 {
00110         struct dhcpMessage packet;
00111 
00112         init_packet(&packet, DHCPDISCOVER);
00113         packet.xid = xid;
00114         if (requested)
00115                 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
00116 
00117         add_requests(&packet);
00118         LOG(LOG_DEBUG, "Sending discover...");
00119         return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 
00120                                 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
00121 }
00122 
00123 
00124 /* Broadcasts a DHCP request message */
00125 int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
00126 {
00127         struct dhcpMessage packet;
00128         struct in_addr addr;
00129 
00130         init_packet(&packet, DHCPREQUEST);
00131         packet.xid = xid;
00132 
00133         add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
00134         add_simple_option(packet.options, DHCP_SERVER_ID, server);
00135         
00136         add_requests(&packet);
00137         addr.s_addr = requested;
00138         LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
00139         return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 
00140                                 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
00141 }
00142 
00143 
00144 /* Unicasts or broadcasts a DHCP renew message */
00145 int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
00146 {
00147         struct dhcpMessage packet;
00148         int ret = 0;
00149 
00150         init_packet(&packet, DHCPREQUEST);
00151         packet.xid = xid;
00152         packet.ciaddr = ciaddr;
00153 
00154         add_requests(&packet);
00155         LOG(LOG_DEBUG, "Sending renew...");
00156         if (server) 
00157                 ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
00158         else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
00159                                 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
00160         return ret;
00161 }       
00162 
00163 
00164 /* Unicasts a DHCP release message */
00165 int send_release(unsigned long server, unsigned long ciaddr)
00166 {
00167         struct dhcpMessage packet;
00168 
00169         init_packet(&packet, DHCPRELEASE);
00170         packet.xid = random_xid();
00171         packet.ciaddr = ciaddr;
00172         
00173         add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
00174         add_simple_option(packet.options, DHCP_SERVER_ID, server);
00175 
00176         LOG(LOG_DEBUG, "Sending release...");
00177         return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
00178 }
00179 
00180 
00181 /* return -1 on errors that are fatal for the socket, -2 for those that aren't */
00182 int get_raw_packet(struct dhcpMessage *payload, int fd)
00183 {
00184         int bytes;
00185         struct udp_dhcp_packet packet;
00186         u_int32_t source, dest;
00187         u_int16_t check;
00188 
00189         memset(&packet, 0, sizeof(struct udp_dhcp_packet));
00190         bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
00191         if (bytes < 0) {
00192                 DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
00193                 usleep(500000); /* possible down interface, looping condition */
00194                 return -1;
00195         }
00196         
00197         if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
00198                 DEBUG(LOG_INFO, "message too short, ignoring");
00199                 return -2;
00200         }
00201         
00202         if (bytes < ntohs(packet.ip.tot_len)) {
00203                 DEBUG(LOG_INFO, "Truncated packet");
00204                 return -2;
00205         }
00206         
00207         /* ignore any extra garbage bytes */
00208         bytes = ntohs(packet.ip.tot_len);
00209         
00210         /* Make sure its the right packet for us, and that it passes sanity checks */
00211         if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
00212             packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
00213             bytes > (int) sizeof(struct udp_dhcp_packet) ||
00214             ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) {
00215                 DEBUG(LOG_INFO, "unrelated/bogus packet");
00216                 return -2;
00217         }
00218 
00219         /* check IP checksum */
00220         check = packet.ip.check;
00221         packet.ip.check = 0;
00222         if (check != checksum(&(packet.ip), sizeof(packet.ip))) {
00223                 DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
00224                 return -1;
00225         }
00226         
00227         /* verify the UDP checksum by replacing the header with a psuedo header */
00228         source = packet.ip.saddr;
00229         dest = packet.ip.daddr;
00230         check = packet.udp.check;
00231         packet.udp.check = 0;
00232         memset(&packet.ip, 0, sizeof(packet.ip));
00233 
00234         packet.ip.protocol = IPPROTO_UDP;
00235         packet.ip.saddr = source;
00236         packet.ip.daddr = dest;
00237         packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
00238         if (check && check != checksum(&packet, bytes)) {
00239                 DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
00240                 return -2;
00241         }
00242         
00243         memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
00244         
00245         if (ntohl(payload->cookie) != DHCP_MAGIC) {
00246                 LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
00247                 return -2;
00248         }
00249         DEBUG(LOG_INFO, "oooooh!!! got some!");
00250         return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
00251         
00252 }
00253 

Generated on Tue Jan 15 12:24:45 2008 for Dynamics 0.8.1.Dynamo.1 by  doxygen 1.5.1