blob: 15cbda2f5c895f50f9ae7c500969a02d1dd4b444 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/* clientpacket.c
3 *
4 * Packet generation and dispatching functions for the DHCP client.
5 *
6 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 *
Rob Landley3f785612006-05-28 01:06:36 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00009 */
10
Mike Frysinger7031f622006-05-08 03:20:50 +000011#include <features.h>
Denis Vlasenkoa7189f02006-11-17 20:29:00 +000012#if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
Mike Frysinger7031f622006-05-08 03:20:50 +000013#include <netpacket/packet.h>
14#include <net/ethernet.h>
15#else
16#include <asm/types.h>
17#include <linux/if_packet.h>
18#include <linux/if_ether.h>
19#endif
Mike Frysinger7031f622006-05-08 03:20:50 +000020
Mike Frysinger7031f622006-05-08 03:20:50 +000021#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000022#include "dhcpd.h"
23#include "dhcpc.h"
24#include "options.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000025
26
27/* Create a random xid */
28unsigned long random_xid(void)
29{
30 static int initialized;
31 if (!initialized) {
Mike Frysinger7031f622006-05-08 03:20:50 +000032 unsigned long seed;
33
Denis Vlasenkoea620772006-10-14 02:23:43 +000034 if (open_read_close("/dev/urandom", &seed, sizeof(seed)) < 0) {
35 bb_info_msg("Cannot load seed "
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000036 "from /dev/urandom: %s", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +000037 seed = time(0);
38 }
Mike Frysinger7031f622006-05-08 03:20:50 +000039 srand(seed);
40 initialized++;
41 }
42 return rand();
43}
44
45
46/* initialize a packet with the proper defaults */
47static void init_packet(struct dhcpMessage *packet, char type)
48{
Rob Landley3f785612006-05-28 01:06:36 +000049 udhcp_init_header(packet, type);
Mike Frysinger7031f622006-05-08 03:20:50 +000050 memcpy(packet->chaddr, client_config.arp, 6);
51 if (client_config.clientid)
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000052 add_option_string(packet->options, client_config.clientid);
Mike Frysinger7031f622006-05-08 03:20:50 +000053 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);
56}
57
58
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. */
62static void add_requests(struct dhcpMessage *packet)
63{
64 int end = end_option(packet->options);
65 int i, len = 0;
66
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;
73
74}
75
76
77/* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
78int send_discover(unsigned long xid, unsigned long requested)
79{
80 struct dhcpMessage packet;
81
82 init_packet(&packet, DHCPDISCOVER);
83 packet.xid = xid;
84 if (requested)
85 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
86
87 add_requests(&packet);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000088 bb_info_msg("Sending discover...");
Rob Landley3f785612006-05-28 01:06:36 +000089 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000090 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +000091}
92
93
94/* Broadcasts a DHCP request message */
95int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
96{
97 struct dhcpMessage packet;
98 struct in_addr addr;
99
100 init_packet(&packet, DHCPREQUEST);
101 packet.xid = xid;
102
103 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
104 add_simple_option(packet.options, DHCP_SERVER_ID, server);
105
106 add_requests(&packet);
107 addr.s_addr = requested;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000108 bb_info_msg("Sending select for %s...", inet_ntoa(addr));
Rob Landley3f785612006-05-28 01:06:36 +0000109 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
Mike Frysinger7031f622006-05-08 03:20:50 +0000110 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
111}
112
113
114/* Unicasts or broadcasts a DHCP renew message */
115int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
116{
117 struct dhcpMessage packet;
118 int ret = 0;
119
120 init_packet(&packet, DHCPREQUEST);
121 packet.xid = xid;
122 packet.ciaddr = ciaddr;
123
124 add_requests(&packet);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000125 bb_info_msg("Sending renew...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000126 if (server)
Rob Landley3f785612006-05-28 01:06:36 +0000127 ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
128 else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
Mike Frysinger7031f622006-05-08 03:20:50 +0000129 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
130 return ret;
131}
132
133
134/* Unicasts a DHCP release message */
135int send_release(unsigned long server, unsigned long ciaddr)
136{
137 struct dhcpMessage packet;
138
139 init_packet(&packet, DHCPRELEASE);
140 packet.xid = random_xid();
141 packet.ciaddr = ciaddr;
142
143 add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
144 add_simple_option(packet.options, DHCP_SERVER_ID, server);
145
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000146 bb_info_msg("Sending release...");
Rob Landley3f785612006-05-28 01:06:36 +0000147 return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
Mike Frysinger7031f622006-05-08 03:20:50 +0000148}
149
150
151/* return -1 on errors that are fatal for the socket, -2 for those that aren't */
152int get_raw_packet(struct dhcpMessage *payload, int fd)
153{
154 int bytes;
155 struct udp_dhcp_packet packet;
156 uint32_t source, dest;
157 uint16_t check;
158
159 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
160 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
161 if (bytes < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000162 DEBUG("Cannot read on raw listening socket - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000163 usleep(500000); /* possible down interface, looping condition */
164 return -1;
165 }
166
167 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000168 DEBUG("Message too short, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000169 return -2;
170 }
171
172 if (bytes < ntohs(packet.ip.tot_len)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000173 DEBUG("Truncated packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000174 return -2;
175 }
176
177 /* ignore any extra garbage bytes */
178 bytes = ntohs(packet.ip.tot_len);
179
180 /* Make sure its the right packet for us, and that it passes sanity checks */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000181 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))
186 ) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000187 DEBUG("Unrelated/bogus packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000188 return -2;
189 }
190
191 /* check IP checksum */
192 check = packet.ip.check;
193 packet.ip.check = 0;
Rob Landley3f785612006-05-28 01:06:36 +0000194 if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000195 DEBUG("bad IP header checksum, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000196 return -1;
197 }
198
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));
205
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 */
Rob Landley3f785612006-05-28 01:06:36 +0000210 if (check && check != udhcp_checksum(&packet, bytes)) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000211 bb_error_msg("packet with bad UDP checksum received, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000212 return -2;
213 }
214
215 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
216
217 if (ntohl(payload->cookie) != DHCP_MAGIC) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000218 bb_error_msg("received bogus message (bad magic) - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000219 return -2;
220 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000221 DEBUG("oooooh!!! got some!");
Mike Frysinger7031f622006-05-08 03:20:50 +0000222 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
223
224}