3 * Packet generation and dispatching functions for the DHCP client.
5 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #include <sys/socket.h>
13 #if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
14 #include <netpacket/packet.h>
15 #include <net/ethernet.h>
17 #include <asm/types.h>
18 #include <linux/if_packet.h>
19 #include <linux/if_ether.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
30 #include "clientpacket.h"
36 /* Create a random xid */
37 unsigned long random_xid(void)
39 static int initialized;
44 fd = open("/dev/urandom", 0);
45 if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
46 LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m");
49 if (fd >= 0) close(fd);
57 /* initialize a packet with the proper defaults */
58 static void init_packet(struct dhcpMessage *packet, char type)
60 udhcp_init_header(packet, type);
61 memcpy(packet->chaddr, client_config.arp, 6);
62 if (client_config.clientid)
63 add_option_string(packet->options, client_config.clientid);
64 if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
65 if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
66 add_option_string(packet->options, client_config.vendorclass);
70 /* Add a parameter request list for stubborn DHCP servers. Pull the data
71 * from the struct in options.c. Don't do bounds checking here because it
72 * goes towards the head of the packet. */
73 static void add_requests(struct dhcpMessage *packet)
75 int end = end_option(packet->options);
78 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
79 for (i = 0; dhcp_options[i].code; i++)
80 if (dhcp_options[i].flags & OPTION_REQ)
81 packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
82 packet->options[end + OPT_LEN] = len;
83 packet->options[end + OPT_DATA + len] = DHCP_END;
88 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
89 int send_discover(unsigned long xid, unsigned long requested)
91 struct dhcpMessage packet;
93 init_packet(&packet, DHCPDISCOVER);
96 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
98 add_requests(&packet);
99 LOG(LOG_DEBUG, "Sending discover...");
100 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
101 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
105 /* Broadcasts a DHCP request message */
106 int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
108 struct dhcpMessage packet;
111 init_packet(&packet, DHCPREQUEST);
114 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
115 add_simple_option(packet.options, DHCP_SERVER_ID, server);
117 add_requests(&packet);
118 addr.s_addr = requested;
119 LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
120 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
121 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
125 /* Unicasts or broadcasts a DHCP renew message */
126 int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
128 struct dhcpMessage packet;
131 init_packet(&packet, DHCPREQUEST);
133 packet.ciaddr = ciaddr;
135 add_requests(&packet);
136 LOG(LOG_DEBUG, "Sending renew...");
138 ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
139 else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
140 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
145 /* Unicasts a DHCP release message */
146 int send_release(unsigned long server, unsigned long ciaddr)
148 struct dhcpMessage packet;
150 init_packet(&packet, DHCPRELEASE);
151 packet.xid = random_xid();
152 packet.ciaddr = ciaddr;
154 add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
155 add_simple_option(packet.options, DHCP_SERVER_ID, server);
157 LOG(LOG_DEBUG, "Sending release...");
158 return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
162 /* return -1 on errors that are fatal for the socket, -2 for those that aren't */
163 int get_raw_packet(struct dhcpMessage *payload, int fd)
166 struct udp_dhcp_packet packet;
167 uint32_t source, dest;
170 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
171 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
173 DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
174 usleep(500000); /* possible down interface, looping condition */
178 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
179 DEBUG(LOG_INFO, "message too short, ignoring");
183 if (bytes < ntohs(packet.ip.tot_len)) {
184 DEBUG(LOG_INFO, "Truncated packet");
188 /* ignore any extra garbage bytes */
189 bytes = ntohs(packet.ip.tot_len);
191 /* Make sure its the right packet for us, and that it passes sanity checks */
192 if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
193 packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
194 bytes > (int) sizeof(struct udp_dhcp_packet) ||
195 ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
196 DEBUG(LOG_INFO, "unrelated/bogus packet");
200 /* check IP checksum */
201 check = packet.ip.check;
203 if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
204 DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
208 /* verify the UDP checksum by replacing the header with a psuedo header */
209 source = packet.ip.saddr;
210 dest = packet.ip.daddr;
211 check = packet.udp.check;
212 packet.udp.check = 0;
213 memset(&packet.ip, 0, sizeof(packet.ip));
215 packet.ip.protocol = IPPROTO_UDP;
216 packet.ip.saddr = source;
217 packet.ip.daddr = dest;
218 packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
219 if (check && check != udhcp_checksum(&packet, bytes)) {
220 DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
224 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
226 if (ntohl(payload->cookie) != DHCP_MAGIC) {
227 LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
230 DEBUG(LOG_INFO, "oooooh!!! got some!");
231 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));