Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: 1994, 1995, 2000 Neil Russell |
3 | | // SPDX-FileCopyrightText: 2000 Roland Borde |
4 | | // SPDX-FileCopyrightText: 2000 Paolo Scaffardi |
5 | | // SPDX-FileCopyrightText: 2000-2004 Wolfgang Denk, wd@denx.de |
6 | | /* |
7 | | * dhcp.c - barebox dhcp support |
8 | | * |
9 | | * based on U-Boot (LiMon) code |
10 | | */ |
11 | | |
12 | | #include <common.h> |
13 | | #include <command.h> |
14 | | #include <complete.h> |
15 | | #include <environment.h> |
16 | | #include <clock.h> |
17 | | #include <net.h> |
18 | | #include <libbb.h> |
19 | | #include <errno.h> |
20 | | #include <magicvar.h> |
21 | | #include <linux/err.h> |
22 | | #include <getopt.h> |
23 | | #include <globalvar.h> |
24 | | #include <init.h> |
25 | | #include <dhcp.h> |
26 | | |
27 | 0 | #define OPT_SIZE 312 /* Minimum DHCP Options size per RFC2131 - results in 576 byte pkt */ |
28 | | |
29 | | struct bootp { |
30 | | uint8_t bp_op; /* Operation */ |
31 | 0 | #define OP_BOOTREQUEST 1 |
32 | 0 | #define OP_BOOTREPLY 2 |
33 | | uint8_t bp_htype; /* Hardware type */ |
34 | 0 | #define HWT_ETHER 1 |
35 | | uint8_t bp_hlen; /* Hardware address length */ |
36 | 0 | #define HWL_ETHER 6 |
37 | | uint8_t bp_hops; /* Hop count (gateway thing) */ |
38 | | uint32_t bp_id; /* Transaction ID */ |
39 | | uint16_t bp_secs; /* Seconds since boot */ |
40 | | uint16_t bp_spare1; /* Alignment */ |
41 | | IPaddr_t bp_ciaddr; /* Client IP address */ |
42 | | IPaddr_t bp_yiaddr; /* Your (client) IP address */ |
43 | | IPaddr_t bp_siaddr; /* Server IP address */ |
44 | | IPaddr_t bp_giaddr; /* Gateway IP address */ |
45 | | uint8_t bp_chaddr[16]; /* Client hardware address */ |
46 | | char bp_sname[64]; /* Server host name */ |
47 | | char bp_file[128]; /* Boot file name */ |
48 | | char bp_vend[0]; /* Vendor information */ |
49 | | } __packed; |
50 | | |
51 | | /* DHCP States */ |
52 | | typedef enum { |
53 | | INIT, |
54 | | INIT_REBOOT, |
55 | | REBOOTING, |
56 | | SELECTING, |
57 | | REQUESTING, |
58 | | REBINDING, |
59 | | BOUND, |
60 | | RENEWING, |
61 | | } dhcp_state_t; |
62 | | |
63 | 0 | #define DHCP_DISCOVER 1 |
64 | 0 | #define DHCP_OFFER 2 |
65 | 0 | #define DHCP_REQUEST 3 |
66 | | #define DHCP_DECLINE 4 |
67 | 0 | #define DHCP_ACK 5 |
68 | 0 | #define DHCP_NAK 6 |
69 | | #define DHCP_RELEASE 7 |
70 | | |
71 | | #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */ |
72 | | |
73 | 0 | #define PORT_BOOTPS 67 /* BOOTP server UDP port */ |
74 | 0 | #define PORT_BOOTPC 68 /* BOOTP client UDP port */ |
75 | | |
76 | 0 | #define DHCP_MIN_EXT_LEN 64 /* minimal length of extension list */ |
77 | | |
78 | | static uint32_t Bootp_id; |
79 | | static dhcp_state_t dhcp_state; |
80 | | static uint64_t dhcp_start; |
81 | | static struct eth_device *dhcp_edev; |
82 | | struct dhcp_req_param dhcp_param; |
83 | | struct dhcp_result *dhcp_result; |
84 | | |
85 | | struct dhcp_receivce_opts { |
86 | | IPaddr_t netmask; |
87 | | IPaddr_t gateway; |
88 | | IPaddr_t nameserver; |
89 | | IPaddr_t serverip; |
90 | | char *hostname; |
91 | | char *domainname; |
92 | | char *rootpath; |
93 | | char *devicetree; |
94 | | char *bootfile; |
95 | | }; |
96 | | |
97 | 0 | #define DHCP_HOSTNAME 12 |
98 | 0 | #define DHCP_VENDOR_ID 60 |
99 | 0 | #define DHCP_CLIENT_ID 61 |
100 | 0 | #define DHCP_USER_CLASS 77 |
101 | 0 | #define DHCP_CLIENT_UUID 97 |
102 | 0 | #define DHCP_OPTION224 224 |
103 | | |
104 | | static int dhcp_set_ip_options(int option, u8 *e, IPaddr_t ip) |
105 | 0 | { |
106 | 0 | int tmp; |
107 | |
|
108 | 0 | if (!ip) |
109 | 0 | return 0; |
110 | | |
111 | 0 | tmp = ntohl(ip); |
112 | |
|
113 | 0 | *e++ = option; |
114 | 0 | *e++ = 4; |
115 | 0 | *e++ = tmp >> 24; |
116 | 0 | *e++ = tmp >> 16; |
117 | 0 | *e++ = tmp >> 8; |
118 | 0 | *e++ = tmp & 0xff; |
119 | |
|
120 | 0 | return 6; |
121 | 0 | } |
122 | | |
123 | | static int bootp_check_packet(unsigned char *pkt, unsigned src, unsigned len) |
124 | 0 | { |
125 | 0 | struct bootp *bp = (struct bootp *) pkt; |
126 | 0 | int retval = 0; |
127 | |
|
128 | 0 | if (src != PORT_BOOTPS) |
129 | 0 | retval = -1; |
130 | 0 | else if (len < sizeof(struct bootp)) |
131 | 0 | retval = -2; |
132 | 0 | else if (bp->bp_op != OP_BOOTREQUEST && |
133 | 0 | bp->bp_op != OP_BOOTREPLY && |
134 | 0 | bp->bp_op != DHCP_OFFER && |
135 | 0 | bp->bp_op != DHCP_ACK && |
136 | 0 | bp->bp_op != DHCP_NAK ) { |
137 | 0 | retval = -3; |
138 | 0 | } |
139 | 0 | else if (bp->bp_htype != HWT_ETHER) |
140 | 0 | retval = -4; |
141 | 0 | else if (bp->bp_hlen != HWL_ETHER) |
142 | 0 | retval = -5; |
143 | 0 | else if (net_read_uint32(&bp->bp_id) != Bootp_id) { |
144 | 0 | retval = -6; |
145 | 0 | } |
146 | |
|
147 | 0 | debug("Filtering pkt = %d\n", retval); |
148 | |
|
149 | 0 | return retval; |
150 | 0 | } |
151 | | |
152 | | /* |
153 | | * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet |
154 | | */ |
155 | | static void bootp_copy_net_params(struct bootp *bp) |
156 | 0 | { |
157 | |
|
158 | 0 | dhcp_result->ip = net_read_ip(&bp->bp_yiaddr); |
159 | 0 | dhcp_result->serverip = net_read_ip(&bp->bp_siaddr); |
160 | |
|
161 | 0 | if (bp->bp_file[0] != '\0') |
162 | 0 | dhcp_result->bootfile = xstrndup(bp->bp_file, sizeof(bp->bp_file)); |
163 | 0 | } |
164 | | |
165 | | static int dhcp_set_string_options(int option, const char *str, u8 *e) |
166 | 0 | { |
167 | 0 | int str_len; |
168 | |
|
169 | 0 | if (!str) |
170 | 0 | return 0; |
171 | | |
172 | 0 | str_len = strlen(str); |
173 | 0 | if (!str_len) |
174 | 0 | return 0; |
175 | | |
176 | | /* DHCP option length field is a single byte per RFC2132 */ |
177 | 0 | if (str_len > 255) |
178 | 0 | str_len = 255; |
179 | |
|
180 | 0 | *e++ = option; |
181 | 0 | *e++ = str_len; |
182 | 0 | memcpy(e, str, str_len); |
183 | |
|
184 | 0 | return str_len + 2; |
185 | 0 | } |
186 | | |
187 | | /* |
188 | | * Initialize BOOTP extension fields in the request. |
189 | | */ |
190 | | static int dhcp_extended(u8 *e, int message_type, IPaddr_t ServerID, |
191 | | IPaddr_t RequestedIP) |
192 | 0 | { |
193 | 0 | int i; |
194 | 0 | u8 *start = e; |
195 | 0 | u8 *cnt; |
196 | 0 | u8 dhcp_options[] = {1, 3, 6, DHCP_HOSTNAME, 15, 17, 51, DHCP_VENDOR_ID, 66, 67, DHCP_CLIENT_ID, |
197 | 0 | DHCP_USER_CLASS, DHCP_CLIENT_UUID, 224}; |
198 | |
|
199 | 0 | *e++ = 99; /* RFC1048 Magic Cookie */ |
200 | 0 | *e++ = 130; |
201 | 0 | *e++ = 83; |
202 | 0 | *e++ = 99; |
203 | |
|
204 | 0 | *e++ = 53; /* DHCP Message Type */ |
205 | 0 | *e++ = 1; |
206 | 0 | *e++ = message_type; |
207 | |
|
208 | 0 | *e++ = 57; /* Maximum DHCP Message Size */ |
209 | 0 | *e++ = 2; |
210 | 0 | *e++ = (576 - 312 + OPT_SIZE) >> 8; |
211 | 0 | *e++ = (576 - 312 + OPT_SIZE) & 0xff; |
212 | |
|
213 | 0 | e += dhcp_set_ip_options(50, e, RequestedIP); |
214 | 0 | e += dhcp_set_ip_options(54, e, ServerID); |
215 | |
|
216 | 0 | e += dhcp_set_string_options(DHCP_HOSTNAME, dhcp_param.hostname, e); |
217 | 0 | e += dhcp_set_string_options(DHCP_VENDOR_ID, dhcp_param.vendor_id, e); |
218 | 0 | e += dhcp_set_string_options(DHCP_CLIENT_ID, dhcp_param.client_id, e); |
219 | 0 | e += dhcp_set_string_options(DHCP_USER_CLASS, dhcp_param.user_class, e); |
220 | 0 | e += dhcp_set_string_options(DHCP_CLIENT_UUID, dhcp_param.client_uuid, e); |
221 | 0 | e += dhcp_set_string_options(DHCP_OPTION224, dhcp_param.option224, e); |
222 | |
|
223 | 0 | *e++ = 55; /* Parameter Request List */ |
224 | 0 | cnt = e++; /* Pointer to count of requested items */ |
225 | |
|
226 | 0 | for (i = 0; i < ARRAY_SIZE(dhcp_options); i++) |
227 | 0 | *e++ = dhcp_options[i]; |
228 | |
|
229 | 0 | *cnt = ARRAY_SIZE(dhcp_options); |
230 | |
|
231 | 0 | *e++ = 255; /* End of the list */ |
232 | | |
233 | | /* Pad to minimal length */ |
234 | 0 | while ((e - start) <= DHCP_MIN_EXT_LEN) |
235 | 0 | *e++ = 0; |
236 | |
|
237 | 0 | return e - start; |
238 | 0 | } |
239 | | |
240 | | static struct net_connection *dhcp_con; |
241 | | |
242 | | static int bootp_request(void) |
243 | 0 | { |
244 | 0 | struct bootp *bp; |
245 | 0 | int ext_len; |
246 | 0 | int ret; |
247 | 0 | const char *bfile; |
248 | |
|
249 | 0 | dhcp_state = INIT; |
250 | |
|
251 | 0 | debug("BOOTP broadcast\n"); |
252 | |
|
253 | 0 | bp = net_udp_get_payload(dhcp_con);; |
254 | 0 | bp->bp_op = OP_BOOTREQUEST; |
255 | 0 | bp->bp_htype = HWT_ETHER; |
256 | 0 | bp->bp_hlen = HWL_ETHER; |
257 | 0 | bp->bp_hops = 0; |
258 | 0 | bp->bp_secs = htons(get_time_ns() >> 30); |
259 | 0 | net_write_ip(&bp->bp_ciaddr, 0); |
260 | 0 | net_write_ip(&bp->bp_yiaddr, 0); |
261 | 0 | net_write_ip(&bp->bp_siaddr, 0); |
262 | 0 | net_write_ip(&bp->bp_giaddr, 0); |
263 | 0 | memcpy(bp->bp_chaddr, dhcp_con->et->et_src, 6); |
264 | |
|
265 | 0 | bfile = getenv("bootfile"); |
266 | 0 | if (bfile) |
267 | 0 | safe_strncpy(bp->bp_file, bfile, sizeof(bp->bp_file)); |
268 | | |
269 | | /* Request additional information from the BOOTP/DHCP server */ |
270 | 0 | ext_len = dhcp_extended(bp->bp_vend, DHCP_DISCOVER, 0, 0); |
271 | |
|
272 | 0 | Bootp_id = (uint32_t)get_time_ns(); |
273 | 0 | net_copy_uint32(&bp->bp_id, &Bootp_id); |
274 | |
|
275 | 0 | dhcp_state = SELECTING; |
276 | |
|
277 | 0 | ret = net_udp_send(dhcp_con, sizeof(*bp) + ext_len); |
278 | |
|
279 | 0 | return ret; |
280 | 0 | } |
281 | | |
282 | | static void dhcp_options_handle(unsigned char option, void *popt, |
283 | | int optlen, struct bootp *bp) |
284 | 0 | { |
285 | | /* |
286 | | * The options read with a fixed size below are skipped when the |
287 | | * server announces less than that, as reading them would run past |
288 | | * the packet. |
289 | | */ |
290 | 0 | switch (option) { |
291 | 0 | case 1: |
292 | 0 | if (optlen < 4) |
293 | 0 | break; |
294 | 0 | dhcp_result->netmask = net_read_ip(popt); |
295 | 0 | break; |
296 | 0 | case 3: |
297 | 0 | if (optlen < 4) |
298 | 0 | break; |
299 | 0 | dhcp_result->gateway = net_read_ip(popt); |
300 | 0 | break; |
301 | 0 | case 6: |
302 | 0 | if (optlen < 4) |
303 | 0 | break; |
304 | 0 | dhcp_result->nameserver = net_read_ip(popt); |
305 | 0 | break; |
306 | 0 | case DHCP_HOSTNAME: |
307 | 0 | dhcp_result->hostname = xstrndup(popt, optlen); |
308 | 0 | break; |
309 | 0 | case 15: |
310 | 0 | dhcp_result->domainname = xstrndup(popt, optlen); |
311 | 0 | break; |
312 | 0 | case 17: |
313 | 0 | dhcp_result->rootpath = xstrndup(popt, optlen); |
314 | 0 | break; |
315 | 0 | case 51: |
316 | 0 | if (optlen < 4) |
317 | 0 | break; |
318 | 0 | net_copy_uint32(&dhcp_result->leasetime, popt); |
319 | 0 | break; |
320 | 0 | case 54: |
321 | 0 | if (optlen < 4) |
322 | 0 | break; |
323 | 0 | dhcp_result->dhcp_serverip = net_read_ip(popt); |
324 | 0 | break; |
325 | 0 | case 66: |
326 | 0 | dhcp_result->tftp_server_name = xstrndup(popt, optlen); |
327 | 0 | break; |
328 | 0 | case 67: |
329 | 0 | if (!dhcp_result->bootfile) |
330 | 0 | dhcp_result->bootfile = xstrndup(popt, optlen); |
331 | 0 | break; |
332 | 0 | case 224: |
333 | 0 | dhcp_result->devicetree = xstrndup(popt, optlen); |
334 | 0 | break; |
335 | 0 | default: |
336 | 0 | debug("*** Unhandled DHCP Option in OFFER/ACK: %d\n", option); |
337 | 0 | } |
338 | 0 | } |
339 | | |
340 | | /* |
341 | | * The BOOTP header is followed by a variable length vendor area, which |
342 | | * may be empty: check it holds the magic cookie before reading it. |
343 | | */ |
344 | | static bool bootp_has_vendor_magic(struct bootp *bp, unsigned int len) |
345 | 0 | { |
346 | 0 | if (len < offsetof(struct bootp, bp_vend) + 4) |
347 | 0 | return false; |
348 | | |
349 | 0 | return net_read_uint32(&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC); |
350 | 0 | } |
351 | | |
352 | | static void dhcp_options_process(unsigned char *popt, struct bootp *bp, |
353 | | unsigned int len) |
354 | 0 | { |
355 | 0 | unsigned char *end = (unsigned char *)bp + len; |
356 | 0 | int oplen; |
357 | 0 | unsigned char option; |
358 | |
|
359 | 0 | while (popt + 1 < end && *popt != 0xff) { |
360 | 0 | oplen = *(popt + 1); |
361 | 0 | option = *popt; |
362 | |
|
363 | 0 | if (popt + 2 + oplen > end) |
364 | 0 | break; |
365 | | |
366 | 0 | dhcp_options_handle(option, popt + 2, oplen, bp); |
367 | |
|
368 | 0 | popt += oplen + 2; /* Process next option */ |
369 | 0 | } |
370 | 0 | } |
371 | | |
372 | | static int dhcp_message_type(unsigned char *popt, unsigned int len) |
373 | 0 | { |
374 | 0 | unsigned char *end = popt + len; |
375 | |
|
376 | 0 | if (len < 4) |
377 | 0 | return -1; |
378 | | |
379 | 0 | if (net_read_uint32((uint32_t *)popt) != htonl(BOOTP_VENDOR_MAGIC)) |
380 | 0 | return -1; |
381 | | |
382 | 0 | popt += 4; |
383 | 0 | while (popt + 1 < end && *popt != 0xff) { |
384 | 0 | if (*popt == 53 && popt + 2 < end) |
385 | 0 | return *(popt + 2); |
386 | 0 | popt += *(popt + 1) + 2; /* Scan through all options */ |
387 | 0 | } |
388 | 0 | return -1; |
389 | 0 | } |
390 | | |
391 | | static void dhcp_send_request_packet(struct bootp *bp_offer) |
392 | 0 | { |
393 | 0 | struct bootp *bp; |
394 | 0 | int extlen; |
395 | |
|
396 | 0 | debug("%s: Sending DHCPREQUEST\n", __func__); |
397 | |
|
398 | 0 | bp = net_udp_get_payload(dhcp_con); |
399 | 0 | bp->bp_op = OP_BOOTREQUEST; |
400 | 0 | bp->bp_htype = HWT_ETHER; |
401 | 0 | bp->bp_hlen = HWL_ETHER; |
402 | 0 | bp->bp_hops = 0; |
403 | 0 | bp->bp_secs = htons(get_time_ns() >> 30); |
404 | | |
405 | | /* |
406 | | * RFC3046 requires Relay Agents to discard packets with |
407 | | * nonzero and offered giaddr |
408 | | */ |
409 | 0 | net_write_ip(&bp->bp_giaddr, 0); |
410 | |
|
411 | 0 | memcpy(bp->bp_chaddr, dhcp_con->et->et_src, 6); |
412 | | |
413 | | /* |
414 | | * ID is the id of the OFFER packet |
415 | | */ |
416 | |
|
417 | 0 | net_copy_uint32(&bp->bp_id, &bp_offer->bp_id); |
418 | | |
419 | | /* |
420 | | * Copy options from OFFER packet if present |
421 | | */ |
422 | 0 | extlen = dhcp_extended(bp->bp_vend, DHCP_REQUEST, |
423 | 0 | dhcp_result->dhcp_serverip, dhcp_result->ip); |
424 | |
|
425 | 0 | debug("Transmitting DHCPREQUEST packet\n"); |
426 | 0 | net_udp_send(dhcp_con, sizeof(*bp) + extlen); |
427 | 0 | } |
428 | | |
429 | | /* |
430 | | * Handle DHCP received packets. |
431 | | */ |
432 | | static void dhcp_handler(void *ctx, char *packet, unsigned int len) |
433 | 0 | { |
434 | 0 | struct net_udp_pkt udp; |
435 | 0 | struct bootp *bp; |
436 | |
|
437 | 0 | if (net_eth_to_udp(packet, len, &udp)) |
438 | 0 | return; |
439 | | |
440 | 0 | bp = udp.payload; |
441 | |
|
442 | 0 | debug("DHCPHandler: got packet: (len=%u) state: %d\n", |
443 | 0 | udp.len, dhcp_state); |
444 | |
|
445 | 0 | if (bootp_check_packet(udp.payload, ntohs(udp.udp->uh_sport), udp.len)) |
446 | 0 | return; |
447 | | |
448 | 0 | switch (dhcp_state) { |
449 | 0 | case SELECTING: |
450 | | /* |
451 | | * Wait an appropriate time for any potential DHCPOFFER packets |
452 | | * to arrive. Then select one, and generate DHCPREQUEST response. |
453 | | * If filename is in format we recognize, assume it is a valid |
454 | | * OFFER from a server we want. |
455 | | */ |
456 | 0 | debug("%s: state SELECTING, bp_file: \"%s\"\n", __func__, bp->bp_file); |
457 | 0 | dhcp_state = REQUESTING; |
458 | |
|
459 | 0 | if (bootp_has_vendor_magic(bp, udp.len)) |
460 | 0 | dhcp_options_process((u8 *)&bp->bp_vend[4], bp, udp.len); |
461 | |
|
462 | 0 | bootp_copy_net_params(bp); /* Store net params from reply */ |
463 | |
|
464 | 0 | dhcp_start = get_time_ns(); |
465 | 0 | dhcp_send_request_packet(bp); |
466 | |
|
467 | 0 | break; |
468 | 0 | case REQUESTING: |
469 | 0 | debug("%s: State REQUESTING\n", __func__); |
470 | |
|
471 | 0 | if (dhcp_message_type((u8 *)bp->bp_vend, |
472 | 0 | udp.len - offsetof(struct bootp, bp_vend)) == DHCP_ACK) { |
473 | 0 | if (bootp_has_vendor_magic(bp, udp.len)) |
474 | 0 | dhcp_options_process(&bp->bp_vend[4], bp, udp.len); |
475 | 0 | bootp_copy_net_params(bp); /* Store net params from reply */ |
476 | 0 | dhcp_state = BOUND; |
477 | 0 | dev_info(&dhcp_edev->dev, "DHCP client bound to address %pI4\n", &dhcp_result->ip); |
478 | 0 | return; |
479 | 0 | } |
480 | 0 | break; |
481 | 0 | default: |
482 | 0 | debug("%s: INVALID STATE\n", __func__); |
483 | 0 | break; |
484 | 0 | } |
485 | 0 | } |
486 | | |
487 | | static char *global_dhcp_user_class; |
488 | | static char *global_dhcp_vendor_id; |
489 | | static char *global_dhcp_client_uuid; |
490 | | static char *global_dhcp_client_id; |
491 | | static char *global_dhcp_hostname; |
492 | | static char *global_dhcp_bootfile; |
493 | | static char *global_dhcp_oftree_file; |
494 | | static char *global_dhcp_rootpath; |
495 | | static char *global_dhcp_tftp_server_name; |
496 | | static int global_dhcp_retries = DHCP_DEFAULT_RETRY; |
497 | | static char *global_dhcp_option224; |
498 | | |
499 | | static void set_res(char **var, const char *res) |
500 | 0 | { |
501 | 0 | free(*var); |
502 | |
|
503 | 0 | if (res) |
504 | 0 | *var = xstrdup(res); |
505 | 0 | else |
506 | 0 | *var = xstrdup(""); |
507 | 0 | } |
508 | | |
509 | | int dhcp_request(struct eth_device *edev, const struct dhcp_req_param *param, |
510 | | struct dhcp_result **res) |
511 | 0 | { |
512 | 0 | int ret = 0; |
513 | |
|
514 | 0 | dhcp_edev = edev; |
515 | 0 | if (param) |
516 | 0 | dhcp_param = *param; |
517 | 0 | else |
518 | 0 | memset(&dhcp_param, 0, sizeof(dhcp_param)); |
519 | |
|
520 | 0 | dhcp_result = xzalloc(sizeof(*dhcp_result)); |
521 | |
|
522 | 0 | if (!dhcp_param.user_class) |
523 | 0 | dhcp_param.user_class = global_dhcp_user_class; |
524 | 0 | if (!dhcp_param.vendor_id) |
525 | 0 | dhcp_param.vendor_id = global_dhcp_vendor_id; |
526 | 0 | if (!dhcp_param.client_uuid) |
527 | 0 | dhcp_param.client_uuid = global_dhcp_client_uuid; |
528 | 0 | if (!dhcp_param.client_id) |
529 | 0 | dhcp_param.client_id = global_dhcp_client_id; |
530 | 0 | if (!dhcp_param.hostname) { |
531 | 0 | if (global_dhcp_hostname && strlen(global_dhcp_hostname)) |
532 | 0 | dhcp_param.hostname = global_dhcp_hostname; |
533 | 0 | else |
534 | 0 | dhcp_param.hostname = (char *)barebox_get_hostname(); |
535 | 0 | } |
536 | 0 | if (!dhcp_param.option224) |
537 | 0 | dhcp_param.option224 = global_dhcp_option224; |
538 | 0 | if (!dhcp_param.retries) |
539 | 0 | dhcp_param.retries = global_dhcp_retries; |
540 | |
|
541 | 0 | dhcp_con = net_udp_eth_new(edev, IP_BROADCAST, PORT_BOOTPS, dhcp_handler, NULL); |
542 | 0 | if (IS_ERR(dhcp_con)) { |
543 | 0 | ret = PTR_ERR(dhcp_con); |
544 | 0 | goto out; |
545 | 0 | } |
546 | | |
547 | 0 | ret = net_udp_bind(dhcp_con, PORT_BOOTPC); |
548 | 0 | if (ret) |
549 | 0 | goto out1; |
550 | | |
551 | 0 | net_set_ip(edev, 0); |
552 | |
|
553 | 0 | dhcp_start = get_time_ns(); |
554 | 0 | ret = bootp_request(); /* Basically same as BOOTP */ |
555 | 0 | if (ret) |
556 | 0 | goto out1; |
557 | | |
558 | 0 | while (dhcp_state != BOUND) { |
559 | 0 | if (ctrlc()) { |
560 | 0 | ret = -EINTR; |
561 | 0 | goto out1; |
562 | 0 | } |
563 | 0 | if (!dhcp_param.retries) { |
564 | 0 | ret = -ETIMEDOUT; |
565 | 0 | goto out1; |
566 | 0 | } |
567 | 0 | net_poll(); |
568 | 0 | if (is_timeout(dhcp_start, 3 * SECOND)) { |
569 | 0 | dhcp_start = get_time_ns(); |
570 | 0 | printf("T "); |
571 | 0 | ret = bootp_request(); |
572 | | /* no need to check if retries > 0 as we check if != 0 */ |
573 | 0 | dhcp_param.retries--; |
574 | 0 | if (ret) |
575 | 0 | goto out1; |
576 | 0 | } |
577 | 0 | } |
578 | | |
579 | 0 | pr_debug("DHCP result:\n" |
580 | 0 | " ip: %pI4\n" |
581 | 0 | " netmask: %pI4\n" |
582 | 0 | " gateway: %pI4\n" |
583 | 0 | " serverip: %pI4\n" |
584 | 0 | " nameserver: %pI4\n" |
585 | 0 | " hostname: %s\n" |
586 | 0 | " domainname: %s\n" |
587 | 0 | " rootpath: %s\n" |
588 | 0 | " devicetree: %s\n" |
589 | 0 | " tftp_server_name: %s\n", |
590 | 0 | &dhcp_result->ip, |
591 | 0 | &dhcp_result->netmask, |
592 | 0 | &dhcp_result->gateway, |
593 | 0 | &dhcp_result->serverip, |
594 | 0 | &dhcp_result->nameserver, |
595 | 0 | dhcp_result->hostname ? dhcp_result->hostname : "", |
596 | 0 | dhcp_result->domainname ? dhcp_result->domainname : "", |
597 | 0 | dhcp_result->rootpath ? dhcp_result->rootpath : "", |
598 | 0 | dhcp_result->devicetree ? dhcp_result->devicetree : "", |
599 | 0 | dhcp_result->tftp_server_name ? dhcp_result->tftp_server_name : ""); |
600 | |
|
601 | 0 | out1: |
602 | 0 | net_unregister(dhcp_con); |
603 | 0 | out: |
604 | 0 | if (ret) { |
605 | 0 | debug("dhcp failed: %pe\n", ERR_PTR(ret)); |
606 | 0 | free(dhcp_result); |
607 | 0 | } else { |
608 | 0 | *res = dhcp_result; |
609 | 0 | } |
610 | |
|
611 | 0 | return ret; |
612 | 0 | } |
613 | | |
614 | | int dhcp_set_result(struct eth_device *edev, struct dhcp_result *res) |
615 | 0 | { |
616 | 0 | int ret; |
617 | |
|
618 | 0 | net_set_ip(edev, res->ip); |
619 | 0 | net_set_netmask(edev, res->netmask); |
620 | 0 | net_set_gateway(edev, res->gateway); |
621 | 0 | net_set_nameserver(res->nameserver); |
622 | |
|
623 | 0 | set_res(&global_dhcp_bootfile, res->bootfile); |
624 | 0 | set_res(&global_dhcp_oftree_file, res->devicetree); |
625 | 0 | set_res(&global_dhcp_rootpath, res->rootpath); |
626 | 0 | set_res(&global_dhcp_tftp_server_name, res->tftp_server_name); |
627 | |
|
628 | 0 | if (res->hostname) |
629 | 0 | barebox_set_hostname(res->hostname); |
630 | 0 | if (res->domainname) |
631 | 0 | net_set_domainname(res->domainname); |
632 | |
|
633 | 0 | if (res->tftp_server_name) { |
634 | 0 | IPaddr_t ip; |
635 | |
|
636 | 0 | ret = resolv(res->tftp_server_name, &ip); |
637 | 0 | if (!ret) |
638 | 0 | net_set_serverip_empty(ip); |
639 | 0 | } else if (res->serverip) { |
640 | 0 | net_set_serverip_empty(res->serverip); |
641 | 0 | } |
642 | |
|
643 | 0 | return 0; |
644 | 0 | } |
645 | | |
646 | | void dhcp_result_free(struct dhcp_result *res) |
647 | 0 | { |
648 | 0 | free(res->hostname); |
649 | 0 | free(res->domainname); |
650 | 0 | free(res->rootpath); |
651 | 0 | free(res->devicetree); |
652 | 0 | free(res->bootfile); |
653 | 0 | free(res->tftp_server_name); |
654 | |
|
655 | 0 | free(res); |
656 | 0 | } |
657 | | |
658 | | int dhcp(struct eth_device *edev, const struct dhcp_req_param *param) |
659 | 0 | { |
660 | 0 | struct dhcp_result *res; |
661 | 0 | int ret; |
662 | |
|
663 | 0 | ret = eth_open(edev); |
664 | 0 | if (ret) |
665 | 0 | return ret; |
666 | | |
667 | 0 | ret = dhcp_request(edev, param, &res); |
668 | 0 | if (ret) |
669 | 0 | return ret; |
670 | | |
671 | 0 | ret = dhcp_set_result(edev, res); |
672 | |
|
673 | 0 | dhcp_result_free(res); |
674 | |
|
675 | 0 | if (!ret) |
676 | 0 | edev->ifup = true; |
677 | |
|
678 | 0 | return ret; |
679 | 0 | } |
680 | | |
681 | | #ifdef CONFIG_GLOBALVAR |
682 | | |
683 | | static int dhcp_global_init(void) |
684 | 30 | { |
685 | 30 | globalvar_add_simple_string("dhcp.bootfile", &global_dhcp_bootfile); |
686 | 30 | globalvar_add_simple_string("dhcp.rootpath", &global_dhcp_rootpath); |
687 | | |
688 | 30 | global_dhcp_vendor_id = strdup("barebox"); |
689 | 30 | globalvar_add_simple_string("dhcp.vendor_id", &global_dhcp_vendor_id); |
690 | 30 | globalvar_add_simple_string("dhcp.client_uuid", &global_dhcp_client_uuid); |
691 | 30 | globalvar_add_simple_string("dhcp.client_id", &global_dhcp_client_id); |
692 | 30 | globalvar_add_simple_string("dhcp.hostname", &global_dhcp_hostname); |
693 | 30 | globalvar_add_simple_string("dhcp.user_class", &global_dhcp_user_class); |
694 | 30 | globalvar_add_simple_string("dhcp.oftree_file", &global_dhcp_oftree_file); |
695 | 30 | globalvar_add_simple_string("dhcp.tftp_server_name", &global_dhcp_tftp_server_name); |
696 | 30 | globalvar_add_simple_int("dhcp.retries", &global_dhcp_retries, "%u"); |
697 | 30 | globalvar_add_simple_string("dhcp.option224", &global_dhcp_option224); |
698 | | |
699 | 30 | return 0; |
700 | 30 | } |
701 | | late_initcall(dhcp_global_init); |
702 | | #endif |
703 | | |
704 | | BAREBOX_MAGICVAR(global.dhcp.bootfile, "bootfile returned from DHCP request"); |
705 | | BAREBOX_MAGICVAR(global.dhcp.rootpath, "rootpath returned from DHCP request"); |
706 | | BAREBOX_MAGICVAR(global.dhcp.vendor_id, "vendor id to send to the DHCP server"); |
707 | | BAREBOX_MAGICVAR(global.dhcp.client_uuid, "client uuid to send to the DHCP server"); |
708 | | BAREBOX_MAGICVAR(global.dhcp.client_id, "client id to send to the DHCP server"); |
709 | | BAREBOX_MAGICVAR(global.dhcp.hostname, "hostname to send to the DHCP server (default: global.hostname)"); |
710 | | BAREBOX_MAGICVAR(global.dhcp.user_class, "user class to send to the DHCP server"); |
711 | | BAREBOX_MAGICVAR(global.dhcp.tftp_server_name, "TFTP server Name returned from DHCP request"); |
712 | | BAREBOX_MAGICVAR(global.dhcp.oftree_file, "OF tree returned from DHCP request (option 224)"); |
713 | | BAREBOX_MAGICVAR(global.dhcp.retries, "retry limit"); |
714 | | BAREBOX_MAGICVAR(global.dhcp.option224, "private data to send to the DHCP server (option 224)"); |
715 | | BAREBOX_MAGICVAR(bootfile, "boot file name to request from the DHCP server"); |