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