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;
44 if (open_read_close("/dev/urandom", &seed, sizeof(seed)) < 0) {
45 bb_info_msg("Cannot load seed "
46 "from /dev/urandom: %s", strerror(errno));
56 /* initialize a packet with the proper defaults */
57 static void init_packet(struct dhcpMessage *packet, char type)
59 udhcp_init_header(packet, type);
60 memcpy(packet->chaddr, client_config.arp, 6);
61 if (client_config.clientid)
62 add_option_string(packet->options, client_config.clientid);
63 if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
64 if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
65 add_option_string(packet->options, client_config.vendorclass);
69 /* Add a parameter request list for stubborn DHCP servers. Pull the data
70 * from the struct in options.c. Don't do bounds checking here because it
71 * goes towards the head of the packet. */
72 static void add_requests(struct dhcpMessage *packet)
74 int end = end_option(packet->options);
77 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
78 for (i = 0; dhcp_options[i].code; i++)
79 if (dhcp_options[i].flags & OPTION_REQ)
80 packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
81 packet->options[end + OPT_LEN] = len;
82 packet->options[end + OPT_DATA + len] = DHCP_END;
87 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
88 int send_discover(unsigned long xid, unsigned long requested)
90 struct dhcpMessage packet;
92 init_packet(&packet, DHCPDISCOVER);
95 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
97 add_requests(&packet);
98 bb_info_msg("Sending discover...");
99 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
100 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
104 /* Broadcasts a DHCP request message */
105 int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
107 struct dhcpMessage packet;
110 init_packet(&packet, DHCPREQUEST);
113 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
114 add_simple_option(packet.options, DHCP_SERVER_ID, server);
116 add_requests(&packet);
117 addr.s_addr = requested;
118 bb_info_msg("Sending select for %s...", inet_ntoa(addr));
119 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
120 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
124 /* Unicasts or broadcasts a DHCP renew message */
125 int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
127 struct dhcpMessage packet;
130 init_packet(&packet, DHCPREQUEST);
132 packet.ciaddr = ciaddr;
134 add_requests(&packet);
135 bb_info_msg("Sending renew...");
137 ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
138 else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
139 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
144 /* Unicasts a DHCP release message */
145 int send_release(unsigned long server, unsigned long ciaddr)
147 struct dhcpMessage packet;
149 init_packet(&packet, DHCPRELEASE);
150 packet.xid = random_xid();
151 packet.ciaddr = ciaddr;
153 add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
154 add_simple_option(packet.options, DHCP_SERVER_ID, server);
156 bb_info_msg("Sending release...");
157 return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
161 /* return -1 on errors that are fatal for the socket, -2 for those that aren't */
162 int get_raw_packet(struct dhcpMessage *payload, int fd)
165 struct udp_dhcp_packet packet;
166 uint32_t source, dest;
169 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
170 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
172 DEBUG("Cannot read on raw listening socket - ignoring");
173 usleep(500000); /* possible down interface, looping condition */
177 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
178 DEBUG("Message too short, ignoring");
182 if (bytes < ntohs(packet.ip.tot_len)) {
183 DEBUG("Truncated packet");
187 /* ignore any extra garbage bytes */
188 bytes = ntohs(packet.ip.tot_len);
190 /* Make sure its the right packet for us, and that it passes sanity checks */
191 if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
192 packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
193 bytes > (int) sizeof(struct udp_dhcp_packet) ||
194 ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
195 DEBUG("Unrelated/bogus packet");
199 /* check IP checksum */
200 check = packet.ip.check;
202 if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
203 DEBUG("bad IP header checksum, ignoring");
207 /* verify the UDP checksum by replacing the header with a psuedo header */
208 source = packet.ip.saddr;
209 dest = packet.ip.daddr;
210 check = packet.udp.check;
211 packet.udp.check = 0;
212 memset(&packet.ip, 0, sizeof(packet.ip));
214 packet.ip.protocol = IPPROTO_UDP;
215 packet.ip.saddr = source;
216 packet.ip.daddr = dest;
217 packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
218 if (check && check != udhcp_checksum(&packet, bytes)) {
219 bb_error_msg("Packet with bad UDP checksum received, ignoring");
223 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
225 if (ntohl(payload->cookie) != DHCP_MAGIC) {
226 bb_error_msg("Received bogus message (bad magic) - ignoring");
229 DEBUG("oooooh!!! got some!");
230 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));