1
2
3
4
5
6
7
8
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
28uint32_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
41static 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
57
58
59static 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99static int raw_bcast_from_client_config_ifindex(struct dhcpMessage *packet)
100{
101 return udhcp_send_raw_packet(packet,
102 INADDR_ANY, CLIENT_PORT,
103 INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
104 client_config.ifindex);
105}
106
107
108#if ENABLE_FEATURE_UDHCPC_ARPING
109
110int FAST_FUNC send_decline(uint32_t xid, uint32_t server, uint32_t requested)
111{
112 struct dhcpMessage 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
126int FAST_FUNC send_discover(uint32_t xid, uint32_t requested)
127{
128 struct dhcpMessage 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
136
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
147
148
149
150int FAST_FUNC send_select(uint32_t xid, uint32_t server, uint32_t requested)
151{
152 struct dhcpMessage 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
169int FAST_FUNC send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
170{
171 struct dhcpMessage 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
189int FAST_FUNC send_release(uint32_t server, uint32_t ciaddr)
190{
191 struct dhcpMessage 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
205int FAST_FUNC udhcp_recv_raw_packet(struct dhcpMessage *payload, int fd)
206{
207 int bytes;
208 struct 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 DEBUG("Cannot read on raw listening socket - ignoring");
215
216 return bytes;
217 }
218
219 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) {
220 DEBUG("Packet is too short, ignoring");
221 return -2;
222 }
223
224 if (bytes < ntohs(packet.ip.tot_len)) {
225
226 DEBUG("Oversized packet, ignoring");
227 return -2;
228 }
229
230
231 bytes = ntohs(packet.ip.tot_len);
232
233
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
238 || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
239 ) {
240 DEBUG("Unrelated/bogus packet");
241 return -2;
242 }
243
244
245 check = packet.ip.check;
246 packet.ip.check = 0;
247 if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
248 DEBUG("Bad IP header checksum, ignoring");
249 return -2;
250 }
251
252
253 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
254
255 packet.ip.tot_len = packet.udp.len;
256 check = packet.udp.check;
257 packet.udp.check = 0;
258 if (check && check != udhcp_checksum(&packet, bytes)) {
259 bb_error_msg("packet with bad UDP checksum received, ignoring");
260 return -2;
261 }
262
263 memcpy(payload, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
264
265 if (payload->cookie != htonl(DHCP_MAGIC)) {
266 bb_error_msg("received bogus message (bad magic), ignoring");
267 return -2;
268 }
269 DEBUG("Got valid DHCP packet");
270 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
271}
272