source: MondoRescue/branches/stable/mindi-busybox/networking/udhcp/clientpacket.c@ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 6.6 KB
Line 
1/* clientpacket.c
2 *
3 * Packet generation and dispatching functions for the DHCP client.
4 *
5 * Russ Dill <Russ.Dill@asu.edu> July 2001
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include <string.h>
11#include <sys/socket.h>
12#include <features.h>
13#if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
14#include <netpacket/packet.h>
15#include <net/ethernet.h>
16#else
17#include <asm/types.h>
18#include <linux/if_packet.h>
19#include <linux/if_ether.h>
20#endif
21#include <stdlib.h>
22#include <time.h>
23#include <unistd.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <fcntl.h>
27
28
29#include "dhcpd.h"
30#include "clientpacket.h"
31#include "options.h"
32#include "dhcpc.h"
33#include "common.h"
34
35
36/* Create a random xid */
37unsigned long random_xid(void)
38{
39 static int initialized;
40 if (!initialized) {
41 int fd;
42 unsigned long seed;
43
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");
47 seed = time(0);
48 }
49 if (fd >= 0) close(fd);
50 srand(seed);
51 initialized++;
52 }
53 return rand();
54}
55
56
57/* initialize a packet with the proper defaults */
58static void init_packet(struct dhcpMessage *packet, char type)
59{
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);
67}
68
69
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. */
73static void add_requests(struct dhcpMessage *packet)
74{
75 int end = end_option(packet->options);
76 int i, len = 0;
77
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;
84
85}
86
87
88/* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
89int send_discover(unsigned long xid, unsigned long requested)
90{
91 struct dhcpMessage packet;
92
93 init_packet(&packet, DHCPDISCOVER);
94 packet.xid = xid;
95 if (requested)
96 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
97
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);
102}
103
104
105/* Broadcasts a DHCP request message */
106int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
107{
108 struct dhcpMessage packet;
109 struct in_addr addr;
110
111 init_packet(&packet, DHCPREQUEST);
112 packet.xid = xid;
113
114 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
115 add_simple_option(packet.options, DHCP_SERVER_ID, server);
116
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);
122}
123
124
125/* Unicasts or broadcasts a DHCP renew message */
126int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
127{
128 struct dhcpMessage packet;
129 int ret = 0;
130
131 init_packet(&packet, DHCPREQUEST);
132 packet.xid = xid;
133 packet.ciaddr = ciaddr;
134
135 add_requests(&packet);
136 LOG(LOG_DEBUG, "Sending renew...");
137 if (server)
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);
141 return ret;
142}
143
144
145/* Unicasts a DHCP release message */
146int send_release(unsigned long server, unsigned long ciaddr)
147{
148 struct dhcpMessage packet;
149
150 init_packet(&packet, DHCPRELEASE);
151 packet.xid = random_xid();
152 packet.ciaddr = ciaddr;
153
154 add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
155 add_simple_option(packet.options, DHCP_SERVER_ID, server);
156
157 LOG(LOG_DEBUG, "Sending release...");
158 return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
159}
160
161
162/* return -1 on errors that are fatal for the socket, -2 for those that aren't */
163int get_raw_packet(struct dhcpMessage *payload, int fd)
164{
165 int bytes;
166 struct udp_dhcp_packet packet;
167 uint32_t source, dest;
168 uint16_t check;
169
170 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
171 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
172 if (bytes < 0) {
173 DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
174 usleep(500000); /* possible down interface, looping condition */
175 return -1;
176 }
177
178 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
179 DEBUG(LOG_INFO, "message too short, ignoring");
180 return -2;
181 }
182
183 if (bytes < ntohs(packet.ip.tot_len)) {
184 DEBUG(LOG_INFO, "Truncated packet");
185 return -2;
186 }
187
188 /* ignore any extra garbage bytes */
189 bytes = ntohs(packet.ip.tot_len);
190
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");
197 return -2;
198 }
199
200 /* check IP checksum */
201 check = packet.ip.check;
202 packet.ip.check = 0;
203 if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
204 DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
205 return -1;
206 }
207
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));
214
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");
221 return -2;
222 }
223
224 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
225
226 if (ntohl(payload->cookie) != DHCP_MAGIC) {
227 LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
228 return -2;
229 }
230 DEBUG(LOG_INFO, "oooooh!!! got some!");
231 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
232
233}
Note: See TracBrowser for help on using the repository browser.