udhcp: added some FIXMEs; code shrink. -49 bytes
[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 <features.h>
12 #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
13 #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
20
21 #include "common.h"
22 #include "dhcpd.h"
23 #include "dhcpc.h"
24 #include "options.h"
25
26
27 /* Create a random xid */
28 uint32_t FAST_FUNC random_xid(void)
29 {
30         static smallint initialized;
31
32         if (!initialized) {
33                 srand(monotonic_us());
34                 initialized = 1;
35         }
36         return rand();
37 }
38
39
40 /* initialize a packet with the proper defaults */
41 static void init_packet(struct dhcpMessage *packet, char type)
42 {
43         udhcp_init_header(packet, type);
44         memcpy(packet->chaddr, client_config.arp, 6);
45         if (client_config.clientid)
46                 add_option_string(packet->options, client_config.clientid);
47         if (client_config.hostname)
48                 add_option_string(packet->options, client_config.hostname);
49         if (client_config.fqdn)
50                 add_option_string(packet->options, client_config.fqdn);
51         if ((type != DHCPDECLINE) && (type != DHCPRELEASE))
52                 add_option_string(packet->options, client_config.vendorclass);
53 }
54
55
56 /* Add a parameter request list for stubborn DHCP servers. Pull the data
57  * from the struct in options.c. Don't do bounds checking here because it
58  * goes towards the head of the packet. */
59 static void add_param_req_option(struct dhcpMessage *packet)
60 {
61         uint8_t c;
62         int end = end_option(packet->options);
63         int i, len = 0;
64
65         for (i = 0; (c = dhcp_options[i].code) != 0; i++) {
66                 if (((dhcp_options[i].flags & OPTION_REQ)
67                      && !client_config.no_default_options)
68                  || (client_config.opt_mask[c >> 3] & (1 << (c & 7)))
69                 ) {
70                         packet->options[end + OPT_DATA + len] = c;
71                         len++;
72                 }
73         }
74         if (len) {
75                 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
76                 packet->options[end + OPT_LEN] = len;
77                 packet->options[end + OPT_DATA + len] = DHCP_END;
78         }
79 }
80
81
82 static int raw_bcast_from_client_config_ifindex(struct dhcpMessage *packet)
83 {
84         return udhcp_send_raw_packet(packet,
85                 /*src*/ INADDR_ANY, CLIENT_PORT,
86                 /*dst*/ INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
87                 client_config.ifindex);
88 }
89
90
91 #if ENABLE_FEATURE_UDHCPC_ARPING
92 /* Broadcast a DHCP decline message */
93 //FIXME: maybe it should be unicast?
94 int FAST_FUNC send_decline(uint32_t xid, uint32_t server, uint32_t requested)
95 {
96         struct dhcpMessage packet;
97
98         init_packet(&packet, DHCPDECLINE);
99         packet.xid = xid;
100         add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
101         add_simple_option(packet.options, DHCP_SERVER_ID, server);
102
103         bb_info_msg("Sending decline...");
104
105         return raw_bcast_from_client_config_ifindex(&packet);
106 }
107 #endif
108
109 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
110 int FAST_FUNC send_discover(uint32_t xid, uint32_t requested)
111 {
112         struct dhcpMessage packet;
113
114         init_packet(&packet, DHCPDISCOVER);
115         packet.xid = xid;
116         if (requested)
117                 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
118
119         /* Explicitly saying that we want RFC-compliant packets helps
120          * some buggy DHCP servers to NOT send bigger packets */
121         add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576));
122
123         add_param_req_option(&packet);
124
125         bb_info_msg("Sending discover...");
126         return raw_bcast_from_client_config_ifindex(&packet);
127 }
128
129
130 /* Broadcasts a DHCP request message */
131 //FIXME: maybe it should be unicast?
132 int FAST_FUNC send_select(uint32_t xid, uint32_t server, uint32_t requested)
133 {
134         struct dhcpMessage packet;
135         struct in_addr addr;
136
137         init_packet(&packet, DHCPREQUEST);
138         packet.xid = xid;
139
140         add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
141         add_simple_option(packet.options, DHCP_SERVER_ID, server);
142         add_param_req_option(&packet);
143
144         addr.s_addr = requested;
145         bb_info_msg("Sending select for %s...", inet_ntoa(addr));
146         return raw_bcast_from_client_config_ifindex(&packet);
147 }
148
149
150 /* Unicasts or broadcasts a DHCP renew message */
151 int FAST_FUNC send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
152 {
153         struct dhcpMessage packet;
154
155         init_packet(&packet, DHCPREQUEST);
156         packet.xid = xid;
157         packet.ciaddr = ciaddr;
158
159         add_param_req_option(&packet);
160         bb_info_msg("Sending renew...");
161         if (server)
162                 return udhcp_send_kernel_packet(&packet,
163                         ciaddr, CLIENT_PORT,
164                         server, SERVER_PORT);
165
166         return raw_bcast_from_client_config_ifindex(&packet);
167 }
168
169
170 /* Unicasts a DHCP release message */
171 int FAST_FUNC send_release(uint32_t server, uint32_t ciaddr)
172 {
173         struct dhcpMessage packet;
174
175         init_packet(&packet, DHCPRELEASE);
176         packet.xid = random_xid();
177         packet.ciaddr = ciaddr;
178
179         add_simple_option(packet.options, DHCP_SERVER_ID, server);
180
181         bb_info_msg("Sending release...");
182         return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
183 }
184
185
186 /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
187 int FAST_FUNC udhcp_recv_raw_packet(struct dhcpMessage *payload, int fd)
188 {
189         int bytes;
190         struct udp_dhcp_packet packet;
191         uint16_t check;
192
193         memset(&packet, 0, sizeof(packet));
194         bytes = safe_read(fd, &packet, sizeof(packet));
195         if (bytes < 0) {
196                 DEBUG("Cannot read on raw listening socket - ignoring");
197                 /* NB: possible down interface, etc. Caller should pause. */
198                 return bytes; /* returns -1 */
199         }
200
201         if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) {
202                 DEBUG("Packet is too short, ignoring");
203                 return -2;
204         }
205
206         if (bytes < ntohs(packet.ip.tot_len)) {
207                 /* packet is bigger than sizeof(packet), we did partial read */
208                 DEBUG("Oversized packet, ignoring");
209                 return -2;
210         }
211
212         /* ignore any extra garbage bytes */
213         bytes = ntohs(packet.ip.tot_len);
214
215         /* make sure its the right packet for us, and that it passes sanity checks */
216         if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
217          || packet.ip.ihl != (sizeof(packet.ip) >> 2)
218          || packet.udp.dest != htons(CLIENT_PORT)
219         /* || bytes > (int) sizeof(packet) - can't happen */
220          || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
221         ) {
222                 DEBUG("Unrelated/bogus packet");
223                 return -2;
224         }
225
226         /* verify IP checksum */
227         check = packet.ip.check;
228         packet.ip.check = 0;
229         if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
230                 DEBUG("Bad IP header checksum, ignoring");
231                 return -2;
232         }
233
234         /* verify UDP checksum. IP header has to be modified for this */
235         memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
236         /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
237         packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
238         check = packet.udp.check;
239         packet.udp.check = 0;
240         if (check && check != udhcp_checksum(&packet, bytes)) {
241                 bb_error_msg("packet with bad UDP checksum received, ignoring");
242                 return -2;
243         }
244
245         memcpy(payload, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
246
247         if (payload->cookie != htonl(DHCP_MAGIC)) {
248                 bb_error_msg("received bogus message (bad magic), ignoring");
249                 return -2;
250         }
251         DEBUG("Got valid DHCP packet");
252         return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
253 }