3 * Packet generation and dispatching functions for the DHCP client.
5 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <sys/socket.h>
25 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
26 #include <netpacket/packet.h>
27 #include <net/ethernet.h>
29 #include <asm/types.h>
30 #include <linux/if_packet.h>
31 #include <linux/if_ether.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
42 #include "clientpacket.h"
48 /* Create a random xid */
49 unsigned long random_xid(void)
51 static int initialized;
56 fd = open("/dev/urandom", 0);
57 if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
58 LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m");
61 if (fd >= 0) close(fd);
69 /* initialize a packet with the proper defaults */
70 static void init_packet(struct dhcpMessage *packet, char type)
74 char str[sizeof("udhcp "VERSION)];
75 } vendor_id = { DHCP_VENDOR, sizeof("udhcp "VERSION) - 1, "udhcp "VERSION};
77 init_header(packet, type);
78 memcpy(packet->chaddr, client_config.arp, 6);
79 add_option_string(packet->options, client_config.clientid);
80 if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
81 add_option_string(packet->options, (uint8_t *) &vendor_id);
85 /* Add a paramater request list for stubborn DHCP servers. Pull the data
86 * from the struct in options.c. Don't do bounds checking here because it
87 * goes towards the head of the packet. */
88 static void add_requests(struct dhcpMessage *packet)
90 int end = end_option(packet->options);
93 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
94 for (i = 0; dhcp_options[i].code; i++)
95 if (dhcp_options[i].flags & OPTION_REQ)
96 packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
97 packet->options[end + OPT_LEN] = len;
98 packet->options[end + OPT_DATA + len] = DHCP_END;
103 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
104 int send_discover(unsigned long xid, unsigned long requested)
106 struct dhcpMessage packet;
108 init_packet(&packet, DHCPDISCOVER);
111 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
113 add_requests(&packet);
114 LOG(LOG_DEBUG, "Sending discover...");
115 return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
116 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
120 /* Broadcasts a DHCP request message */
121 int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
123 struct dhcpMessage packet;
126 init_packet(&packet, DHCPREQUEST);
129 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
130 add_simple_option(packet.options, DHCP_SERVER_ID, server);
132 add_requests(&packet);
133 addr.s_addr = requested;
134 LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
135 return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
136 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
140 /* Unicasts or broadcasts a DHCP renew message */
141 int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
143 struct dhcpMessage packet;
146 init_packet(&packet, DHCPREQUEST);
148 packet.ciaddr = ciaddr;
150 add_requests(&packet);
151 LOG(LOG_DEBUG, "Sending renew...");
153 ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
154 else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
155 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
160 /* Unicasts a DHCP release message */
161 int send_release(unsigned long server, unsigned long ciaddr)
163 struct dhcpMessage packet;
165 init_packet(&packet, DHCPRELEASE);
166 packet.xid = random_xid();
167 packet.ciaddr = ciaddr;
169 add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
170 add_simple_option(packet.options, DHCP_SERVER_ID, server);
172 LOG(LOG_DEBUG, "Sending release...");
173 return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
177 /* return -1 on errors that are fatal for the socket, -2 for those that aren't */
178 int get_raw_packet(struct dhcpMessage *payload, int fd)
181 struct udp_dhcp_packet packet;
182 uint32_t source, dest;
185 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
186 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
188 DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
189 usleep(500000); /* possible down interface, looping condition */
193 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
194 DEBUG(LOG_INFO, "message too short, ignoring");
198 if (bytes < ntohs(packet.ip.tot_len)) {
199 DEBUG(LOG_INFO, "Truncated packet");
203 /* ignore any extra garbage bytes */
204 bytes = ntohs(packet.ip.tot_len);
206 /* Make sure its the right packet for us, and that it passes sanity checks */
207 if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
208 packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
209 bytes > (int) sizeof(struct udp_dhcp_packet) ||
210 ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
211 DEBUG(LOG_INFO, "unrelated/bogus packet");
215 /* check IP checksum */
216 check = packet.ip.check;
218 if (check != checksum(&(packet.ip), sizeof(packet.ip))) {
219 DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
223 /* verify the UDP checksum by replacing the header with a psuedo header */
224 source = packet.ip.saddr;
225 dest = packet.ip.daddr;
226 check = packet.udp.check;
227 packet.udp.check = 0;
228 memset(&packet.ip, 0, sizeof(packet.ip));
230 packet.ip.protocol = IPPROTO_UDP;
231 packet.ip.saddr = source;
232 packet.ip.daddr = dest;
233 packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
234 if (check && check != checksum(&packet, bytes)) {
235 DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
239 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
241 if (ntohl(payload->cookie) != DHCP_MAGIC) {
242 LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
245 DEBUG(LOG_INFO, "oooooh!!! got some!");
246 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));