1 /* vi: set sw=4 ts=4: */
4 * Packet generation and dispatching functions for the DHCP client.
6 * Russ Dill <Russ.Dill@asu.edu> July 2001
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 #include <sys/socket.h>
14 #if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
15 #include <netpacket/packet.h>
16 #include <net/ethernet.h>
18 #include <asm/types.h>
19 #include <linux/if_packet.h>
20 #include <linux/if_ether.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
31 #include "clientpacket.h"
37 /* Create a random xid */
38 unsigned long random_xid(void)
40 static int initialized;
45 fd = open("/dev/urandom", 0);
46 if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
47 bb_info_msg("Could not load seed "
48 "from /dev/urandom: %s", strerror(errno));
51 if (fd >= 0) close(fd);
59 /* initialize a packet with the proper defaults */
60 static void init_packet(struct dhcpMessage *packet, char type)
62 udhcp_init_header(packet, type);
63 memcpy(packet->chaddr, client_config.arp, 6);
64 if (client_config.clientid)
65 add_option_string(packet->options, client_config.clientid);
66 if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
67 if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
68 add_option_string(packet->options, client_config.vendorclass);
72 /* Add a parameter request list for stubborn DHCP servers. Pull the data
73 * from the struct in options.c. Don't do bounds checking here because it
74 * goes towards the head of the packet. */
75 static void add_requests(struct dhcpMessage *packet)
77 int end = end_option(packet->options);
80 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
81 for (i = 0; dhcp_options[i].code; i++)
82 if (dhcp_options[i].flags & OPTION_REQ)
83 packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
84 packet->options[end + OPT_LEN] = len;
85 packet->options[end + OPT_DATA + len] = DHCP_END;
90 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
91 int send_discover(unsigned long xid, unsigned long requested)
93 struct dhcpMessage packet;
95 init_packet(&packet, DHCPDISCOVER);
98 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
100 add_requests(&packet);
101 bb_info_msg("Sending discover...");
102 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
103 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
107 /* Broadcasts a DHCP request message */
108 int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
110 struct dhcpMessage packet;
113 init_packet(&packet, DHCPREQUEST);
116 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
117 add_simple_option(packet.options, DHCP_SERVER_ID, server);
119 add_requests(&packet);
120 addr.s_addr = requested;
121 bb_info_msg("Sending select for %s...", inet_ntoa(addr));
122 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
123 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
127 /* Unicasts or broadcasts a DHCP renew message */
128 int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
130 struct dhcpMessage packet;
133 init_packet(&packet, DHCPREQUEST);
135 packet.ciaddr = ciaddr;
137 add_requests(&packet);
138 bb_info_msg("Sending renew...");
140 ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
141 else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
142 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
147 /* Unicasts a DHCP release message */
148 int send_release(unsigned long server, unsigned long ciaddr)
150 struct dhcpMessage packet;
152 init_packet(&packet, DHCPRELEASE);
153 packet.xid = random_xid();
154 packet.ciaddr = ciaddr;
156 add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
157 add_simple_option(packet.options, DHCP_SERVER_ID, server);
159 bb_info_msg("Sending release...");
160 return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
164 /* return -1 on errors that are fatal for the socket, -2 for those that aren't */
165 int get_raw_packet(struct dhcpMessage *payload, int fd)
168 struct udp_dhcp_packet packet;
169 uint32_t source, dest;
172 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
173 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
175 DEBUG("Couldn't read on raw listening socket - ignoring");
176 usleep(500000); /* possible down interface, looping condition */
180 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
181 DEBUG("Message too short, ignoring");
185 if (bytes < ntohs(packet.ip.tot_len)) {
186 DEBUG("Truncated packet");
190 /* ignore any extra garbage bytes */
191 bytes = ntohs(packet.ip.tot_len);
193 /* Make sure its the right packet for us, and that it passes sanity checks */
194 if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
195 packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
196 bytes > (int) sizeof(struct udp_dhcp_packet) ||
197 ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
198 DEBUG("Unrelated/bogus packet");
202 /* check IP checksum */
203 check = packet.ip.check;
205 if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
206 DEBUG("bad IP header checksum, ignoring");
210 /* verify the UDP checksum by replacing the header with a psuedo header */
211 source = packet.ip.saddr;
212 dest = packet.ip.daddr;
213 check = packet.udp.check;
214 packet.udp.check = 0;
215 memset(&packet.ip, 0, sizeof(packet.ip));
217 packet.ip.protocol = IPPROTO_UDP;
218 packet.ip.saddr = source;
219 packet.ip.daddr = dest;
220 packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
221 if (check && check != udhcp_checksum(&packet, bytes)) {
222 bb_error_msg("Packet with bad UDP checksum received, ignoring");
226 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
228 if (ntohl(payload->cookie) != DHCP_MAGIC) {
229 bb_error_msg("Received bogus message (bad magic) - ignoring");
232 DEBUG("oooooh!!! got some!");
233 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));