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 #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
13 #include <netpacket/packet.h>
14 #include <net/ethernet.h>
16 #include <asm/types.h>
17 #include <linux/if_packet.h>
18 #include <linux/if_ether.h>
27 /* Create a random xid */
28 unsigned long random_xid(void)
30 static int initialized;
34 if (open_read_close("/dev/urandom", &seed, sizeof(seed)) < 0) {
35 bb_info_msg("Cannot load seed "
36 "from /dev/urandom: %s", strerror(errno));
46 /* initialize a packet with the proper defaults */
47 static void init_packet(struct dhcpMessage *packet, char type)
49 udhcp_init_header(packet, type);
50 memcpy(packet->chaddr, client_config.arp, 6);
51 if (client_config.clientid)
52 add_option_string(packet->options, client_config.clientid);
53 if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
54 if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
55 add_option_string(packet->options, client_config.vendorclass);
59 /* Add a parameter request list for stubborn DHCP servers. Pull the data
60 * from the struct in options.c. Don't do bounds checking here because it
61 * goes towards the head of the packet. */
62 static void add_requests(struct dhcpMessage *packet)
64 int end = end_option(packet->options);
67 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
68 for (i = 0; dhcp_options[i].code; i++)
69 if (dhcp_options[i].flags & OPTION_REQ)
70 packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
71 packet->options[end + OPT_LEN] = len;
72 packet->options[end + OPT_DATA + len] = DHCP_END;
77 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
78 int send_discover(unsigned long xid, unsigned long requested)
80 struct dhcpMessage packet;
82 init_packet(&packet, DHCPDISCOVER);
85 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
87 add_requests(&packet);
88 bb_info_msg("Sending discover...");
89 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
90 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
94 /* Broadcasts a DHCP request message */
95 int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
97 struct dhcpMessage packet;
100 init_packet(&packet, DHCPREQUEST);
103 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
104 add_simple_option(packet.options, DHCP_SERVER_ID, server);
106 add_requests(&packet);
107 addr.s_addr = requested;
108 bb_info_msg("Sending select for %s...", inet_ntoa(addr));
109 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
110 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
114 /* Unicasts or broadcasts a DHCP renew message */
115 int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
117 struct dhcpMessage packet;
120 init_packet(&packet, DHCPREQUEST);
122 packet.ciaddr = ciaddr;
124 add_requests(&packet);
125 bb_info_msg("Sending renew...");
127 ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
128 else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
129 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
134 /* Unicasts a DHCP release message */
135 int send_release(unsigned long server, unsigned long ciaddr)
137 struct dhcpMessage packet;
139 init_packet(&packet, DHCPRELEASE);
140 packet.xid = random_xid();
141 packet.ciaddr = ciaddr;
143 add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
144 add_simple_option(packet.options, DHCP_SERVER_ID, server);
146 bb_info_msg("Sending release...");
147 return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
151 /* return -1 on errors that are fatal for the socket, -2 for those that aren't */
152 int get_raw_packet(struct dhcpMessage *payload, int fd)
155 struct udp_dhcp_packet packet;
156 uint32_t source, dest;
159 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
160 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
162 DEBUG("Cannot read on raw listening socket - ignoring");
163 usleep(500000); /* possible down interface, looping condition */
167 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
168 DEBUG("Message too short, ignoring");
172 if (bytes < ntohs(packet.ip.tot_len)) {
173 DEBUG("Truncated packet");
177 /* ignore any extra garbage bytes */
178 bytes = ntohs(packet.ip.tot_len);
180 /* Make sure its the right packet for us, and that it passes sanity checks */
181 if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
182 || packet.ip.ihl != sizeof(packet.ip) >> 2
183 || packet.udp.dest != htons(CLIENT_PORT)
184 || bytes > (int) sizeof(struct udp_dhcp_packet)
185 || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
187 DEBUG("Unrelated/bogus packet");
191 /* check IP checksum */
192 check = packet.ip.check;
194 if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
195 DEBUG("bad IP header checksum, ignoring");
199 /* verify the UDP checksum by replacing the header with a psuedo header */
200 source = packet.ip.saddr;
201 dest = packet.ip.daddr;
202 check = packet.udp.check;
203 packet.udp.check = 0;
204 memset(&packet.ip, 0, sizeof(packet.ip));
206 packet.ip.protocol = IPPROTO_UDP;
207 packet.ip.saddr = source;
208 packet.ip.daddr = dest;
209 packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
210 if (check && check != udhcp_checksum(&packet, bytes)) {
211 bb_error_msg("packet with bad UDP checksum received, ignoring");
215 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
217 if (ntohl(payload->cookie) != DHCP_MAGIC) {
218 bb_error_msg("received bogus message (bad magic) - ignoring");
221 DEBUG("oooooh!!! got some!");
222 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));