blob: 2695b06ed38f0135d020e64b51da60646bf9bdba [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001/* 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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000021
Russ Dill61fb4892002-10-14 21:41:28 +000022#include <string.h>
23#include <sys/socket.h>
24#include <features.h>
25#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
26#include <netpacket/packet.h>
27#include <net/ethernet.h>
28#else
29#include <asm/types.h>
30#include <linux/if_packet.h>
31#include <linux/if_ether.h>
32#endif
33#include <stdlib.h>
34#include <time.h>
35#include <unistd.h>
36#include <netinet/in.h>
37#include <arpa/inet.h>
Russ Dillf5ecd432002-10-31 19:21:27 +000038#include <fcntl.h>
Russ Dill61fb4892002-10-14 21:41:28 +000039
40
41#include "dhcpd.h"
Russ Dill76729b82003-12-16 20:44:15 +000042#include "clientpacket.h"
Russ Dill61fb4892002-10-14 21:41:28 +000043#include "options.h"
44#include "dhcpc.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000045#include "common.h"
Russ Dill61fb4892002-10-14 21:41:28 +000046
47
48/* Create a random xid */
49unsigned long random_xid(void)
50{
51 static int initialized;
52 if (!initialized) {
Russ Dillf5ecd432002-10-31 19:21:27 +000053 int fd;
54 unsigned long seed;
55
56 fd = open("/dev/urandom", 0);
57 if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +000058 LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m");
Russ Dillf5ecd432002-10-31 19:21:27 +000059 seed = time(0);
60 }
61 if (fd >= 0) close(fd);
62 srand(seed);
Russ Dill61fb4892002-10-14 21:41:28 +000063 initialized++;
64 }
65 return rand();
66}
67
68
69/* initialize a packet with the proper defaults */
70static void init_packet(struct dhcpMessage *packet, char type)
71{
72 struct vendor {
73 char vendor, length;
74 char str[sizeof("udhcp "VERSION)];
75 } vendor_id = { DHCP_VENDOR, sizeof("udhcp "VERSION) - 1, "udhcp "VERSION};
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076
Russ Dill61fb4892002-10-14 21:41:28 +000077 init_header(packet, type);
78 memcpy(packet->chaddr, client_config.arp, 6);
Paul Foxa39bba32005-08-01 14:31:13 +000079 if (client_config.clientid)
80 add_option_string(packet->options, client_config.clientid);
Russ Dill61fb4892002-10-14 21:41:28 +000081 if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
Mike Frysingerd8248532004-12-06 14:59:45 +000082 if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
Eric Andersenad953732004-01-30 23:45:53 +000083 add_option_string(packet->options, (uint8_t *) &vendor_id);
Russ Dill61fb4892002-10-14 21:41:28 +000084}
85
86
Eric Andersenaff114c2004-04-14 17:51:38 +000087/* Add a parameter request list for stubborn DHCP servers. Pull the data
Russ Dill61fb4892002-10-14 21:41:28 +000088 * from the struct in options.c. Don't do bounds checking here because it
89 * goes towards the head of the packet. */
90static void add_requests(struct dhcpMessage *packet)
91{
92 int end = end_option(packet->options);
93 int i, len = 0;
94
95 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
Russ Dill4a9e34c2003-12-15 22:09:36 +000096 for (i = 0; dhcp_options[i].code; i++)
97 if (dhcp_options[i].flags & OPTION_REQ)
98 packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
Russ Dill61fb4892002-10-14 21:41:28 +000099 packet->options[end + OPT_LEN] = len;
100 packet->options[end + OPT_DATA + len] = DHCP_END;
101
102}
103
104
105/* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
106int send_discover(unsigned long xid, unsigned long requested)
107{
108 struct dhcpMessage packet;
109
110 init_packet(&packet, DHCPDISCOVER);
111 packet.xid = xid;
112 if (requested)
113 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
114
115 add_requests(&packet);
116 LOG(LOG_DEBUG, "Sending discover...");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000117 return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
Russ Dill61fb4892002-10-14 21:41:28 +0000118 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
119}
120
121
122/* Broadcasts a DHCP request message */
123int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
124{
125 struct dhcpMessage packet;
126 struct in_addr addr;
127
128 init_packet(&packet, DHCPREQUEST);
129 packet.xid = xid;
130
131 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
132 add_simple_option(packet.options, DHCP_SERVER_ID, server);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000133
Russ Dill61fb4892002-10-14 21:41:28 +0000134 add_requests(&packet);
135 addr.s_addr = requested;
136 LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000137 return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
Russ Dill61fb4892002-10-14 21:41:28 +0000138 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
139}
140
141
142/* Unicasts or broadcasts a DHCP renew message */
143int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
144{
145 struct dhcpMessage packet;
146 int ret = 0;
147
148 init_packet(&packet, DHCPREQUEST);
149 packet.xid = xid;
150 packet.ciaddr = ciaddr;
151
152 add_requests(&packet);
153 LOG(LOG_DEBUG, "Sending renew...");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000154 if (server)
Russ Dill61fb4892002-10-14 21:41:28 +0000155 ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
156 else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
157 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
158 return ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000159}
Russ Dill61fb4892002-10-14 21:41:28 +0000160
161
162/* Unicasts a DHCP release message */
163int send_release(unsigned long server, unsigned long ciaddr)
164{
165 struct dhcpMessage packet;
166
167 init_packet(&packet, DHCPRELEASE);
168 packet.xid = random_xid();
169 packet.ciaddr = ciaddr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000170
Russ Dill61fb4892002-10-14 21:41:28 +0000171 add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
172 add_simple_option(packet.options, DHCP_SERVER_ID, server);
173
174 LOG(LOG_DEBUG, "Sending release...");
175 return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
176}
177
178
179/* return -1 on errors that are fatal for the socket, -2 for those that aren't */
180int get_raw_packet(struct dhcpMessage *payload, int fd)
181{
182 int bytes;
183 struct udp_dhcp_packet packet;
Eric Andersenad953732004-01-30 23:45:53 +0000184 uint32_t source, dest;
185 uint16_t check;
Russ Dill61fb4892002-10-14 21:41:28 +0000186
187 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
188 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
189 if (bytes < 0) {
190 DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
191 usleep(500000); /* possible down interface, looping condition */
192 return -1;
193 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000194
Russ Dill61fb4892002-10-14 21:41:28 +0000195 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
196 DEBUG(LOG_INFO, "message too short, ignoring");
197 return -2;
198 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000199
Russ Dill61fb4892002-10-14 21:41:28 +0000200 if (bytes < ntohs(packet.ip.tot_len)) {
201 DEBUG(LOG_INFO, "Truncated packet");
202 return -2;
203 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000204
Russ Dill61fb4892002-10-14 21:41:28 +0000205 /* ignore any extra garbage bytes */
206 bytes = ntohs(packet.ip.tot_len);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000207
Russ Dill61fb4892002-10-14 21:41:28 +0000208 /* Make sure its the right packet for us, and that it passes sanity checks */
209 if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
210 packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
211 bytes > (int) sizeof(struct udp_dhcp_packet) ||
Eric Andersenad953732004-01-30 23:45:53 +0000212 ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
Russ Dill61fb4892002-10-14 21:41:28 +0000213 DEBUG(LOG_INFO, "unrelated/bogus packet");
214 return -2;
215 }
216
217 /* check IP checksum */
218 check = packet.ip.check;
219 packet.ip.check = 0;
220 if (check != checksum(&(packet.ip), sizeof(packet.ip))) {
221 DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
222 return -1;
223 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000224
Russ Dill61fb4892002-10-14 21:41:28 +0000225 /* verify the UDP checksum by replacing the header with a psuedo header */
226 source = packet.ip.saddr;
227 dest = packet.ip.daddr;
228 check = packet.udp.check;
229 packet.udp.check = 0;
230 memset(&packet.ip, 0, sizeof(packet.ip));
231
232 packet.ip.protocol = IPPROTO_UDP;
233 packet.ip.saddr = source;
234 packet.ip.daddr = dest;
235 packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
236 if (check && check != checksum(&packet, bytes)) {
237 DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
238 return -2;
239 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000240
Russ Dill61fb4892002-10-14 21:41:28 +0000241 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000242
Russ Dill61fb4892002-10-14 21:41:28 +0000243 if (ntohl(payload->cookie) != DHCP_MAGIC) {
244 LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
245 return -2;
246 }
247 DEBUG(LOG_INFO, "oooooh!!! got some!");
248 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000249
Russ Dill61fb4892002-10-14 21:41:28 +0000250}