00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
00090
00091
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
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
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
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
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
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);
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
00208 bytes = ntohs(packet.ip.tot_len);
00209
00210
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
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
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;
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