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