84a6765ee2eccb02c5306824bbb3df0f88c81e08
[platform/upstream/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 the packet with the proper defaults */
41 static void init_packet(struct dhcp_packet *packet, char type)
42 {
43         udhcp_init_header(packet, type);
44         memcpy(packet->chaddr, client_config.client_mac, 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 dhcp_packet *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 /* RFC 2131
82  * 4.4.4 Use of broadcast and unicast
83  *
84  * The DHCP client broadcasts DHCPDISCOVER, DHCPREQUEST and DHCPINFORM
85  * messages, unless the client knows the address of a DHCP server.
86  * The client unicasts DHCPRELEASE messages to the server. Because
87  * the client is declining the use of the IP address supplied by the server,
88  * the client broadcasts DHCPDECLINE messages.
89  *
90  * When the DHCP client knows the address of a DHCP server, in either
91  * INIT or REBOOTING state, the client may use that address
92  * in the DHCPDISCOVER or DHCPREQUEST rather than the IP broadcast address.
93  * The client may also use unicast to send DHCPINFORM messages
94  * to a known DHCP server. If the client receives no response to DHCP
95  * messages sent to the IP address of a known DHCP server, the DHCP
96  * client reverts to using the IP broadcast address.
97  */
98
99 static int raw_bcast_from_client_config_ifindex(struct dhcp_packet *packet)
100 {
101         return udhcp_send_raw_packet(packet,
102                 /*src*/ INADDR_ANY, CLIENT_PORT,
103                 /*dst*/ INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
104                 client_config.ifindex);
105 }
106
107
108 #if ENABLE_FEATURE_UDHCPC_ARPING
109 /* Broadcast a DHCP decline message */
110 int FAST_FUNC send_decline(uint32_t xid, uint32_t server, uint32_t requested)
111 {
112         struct dhcp_packet packet;
113
114         init_packet(&packet, DHCPDECLINE);
115         packet.xid = xid;
116         add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
117         add_simple_option(packet.options, DHCP_SERVER_ID, server);
118
119         bb_info_msg("Sending decline...");
120
121         return raw_bcast_from_client_config_ifindex(&packet);
122 }
123 #endif
124
125
126 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
127 int FAST_FUNC send_discover(uint32_t xid, uint32_t requested)
128 {
129         struct dhcp_packet packet;
130
131         init_packet(&packet, DHCPDISCOVER);
132         packet.xid = xid;
133         if (requested)
134                 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
135
136         /* Explicitly saying that we want RFC-compliant packets helps
137          * some buggy DHCP servers to NOT send bigger packets */
138         add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576));
139
140         add_param_req_option(&packet);
141
142         bb_info_msg("Sending discover...");
143         return raw_bcast_from_client_config_ifindex(&packet);
144 }
145
146
147 /* Broadcast a DHCP request message */
148 /* RFC 2131 3.1 paragraph 3:
149  * "The client _broadcasts_ a DHCPREQUEST message..."
150  */
151 int FAST_FUNC send_select(uint32_t xid, uint32_t server, uint32_t requested)
152 {
153         struct dhcp_packet packet;
154         struct in_addr addr;
155
156         init_packet(&packet, DHCPREQUEST);
157         packet.xid = xid;
158
159         add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
160         add_simple_option(packet.options, DHCP_SERVER_ID, server);
161         add_param_req_option(&packet);
162
163         addr.s_addr = requested;
164         bb_info_msg("Sending select for %s...", inet_ntoa(addr));
165         return raw_bcast_from_client_config_ifindex(&packet);
166 }
167
168
169 /* Unicast or broadcast a DHCP renew message */
170 int FAST_FUNC send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
171 {
172         struct dhcp_packet packet;
173
174         init_packet(&packet, DHCPREQUEST);
175         packet.xid = xid;
176         packet.ciaddr = ciaddr;
177
178         add_param_req_option(&packet);
179         bb_info_msg("Sending renew...");
180         if (server)
181                 return udhcp_send_kernel_packet(&packet,
182                         ciaddr, CLIENT_PORT,
183                         server, SERVER_PORT);
184
185         return raw_bcast_from_client_config_ifindex(&packet);
186 }
187
188
189 /* Unicast a DHCP release message */
190 int FAST_FUNC send_release(uint32_t server, uint32_t ciaddr)
191 {
192         struct dhcp_packet packet;
193
194         init_packet(&packet, DHCPRELEASE);
195         packet.xid = random_xid();
196         packet.ciaddr = ciaddr;
197
198         add_simple_option(packet.options, DHCP_SERVER_ID, server);
199
200         bb_info_msg("Sending release...");
201         return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
202 }
203
204
205 /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
206 int FAST_FUNC udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd)
207 {
208         int bytes;
209         struct ip_udp_dhcp_packet packet;
210         uint16_t check;
211
212         memset(&packet, 0, sizeof(packet));
213         bytes = safe_read(fd, &packet, sizeof(packet));
214         if (bytes < 0) {
215                 log1("Packet read error, ignoring");
216                 /* NB: possible down interface, etc. Caller should pause. */
217                 return bytes; /* returns -1 */
218         }
219
220         if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) {
221                 log1("Packet is too short, ignoring");
222                 return -2;
223         }
224
225         if (bytes < ntohs(packet.ip.tot_len)) {
226                 /* packet is bigger than sizeof(packet), we did partial read */
227                 log1("Oversized packet, ignoring");
228                 return -2;
229         }
230
231         /* ignore any extra garbage bytes */
232         bytes = ntohs(packet.ip.tot_len);
233
234         /* make sure its the right packet for us, and that it passes sanity checks */
235         if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
236          || packet.ip.ihl != (sizeof(packet.ip) >> 2)
237          || packet.udp.dest != htons(CLIENT_PORT)
238         /* || bytes > (int) sizeof(packet) - can't happen */
239          || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
240         ) {
241                 log1("Unrelated/bogus packet, ignoring");
242                 return -2;
243         }
244
245         /* verify IP checksum */
246         check = packet.ip.check;
247         packet.ip.check = 0;
248         if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
249                 log1("Bad IP header checksum, ignoring");
250                 return -2;
251         }
252
253         /* verify UDP checksum. IP header has to be modified for this */
254         memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
255         /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
256         packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
257         check = packet.udp.check;
258         packet.udp.check = 0;
259         if (check && check != udhcp_checksum(&packet, bytes)) {
260                 log1("Packet with bad UDP checksum received, ignoring");
261                 return -2;
262         }
263
264         memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
265
266         if (dhcp_pkt->cookie != htonl(DHCP_MAGIC)) {
267                 bb_info_msg("Packet with bad magic, ignoring");
268                 return -2;
269         }
270         log1("Got valid DHCP packet");
271         udhcp_dump_packet(dhcp_pkt);
272         return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
273 }