The Pedigree Project 0.1
dhcp.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
46/*
47 * Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
48 * Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
49 * All rights reserved.
50 *
51 * Redistribution and use in source and binary forms, with or without modification,
52 * are permitted provided that the following conditions are met:
53 *
54 * 1. Redistributions of source code must retain the above copyright notice,
55 * this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright notice,
57 * this list of conditions and the following disclaimer in the documentation
58 * and/or other materials provided with the distribution.
59 * 3. The name of the author may not be used to endorse or promote products
60 * derived from this software without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
63 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
64 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
65 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
66 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
67 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
68 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
69 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
70 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
71 * OF SUCH DAMAGE.
72 *
73 * This file is part of the lwIP TCP/IP stack.
74 * The Swedish Institute of Computer Science and Adam Dunkels
75 * are specifically granted permission to redistribute this
76 * source code.
77 *
78 * Author: Leon Woestenberg <leon.woestenberg@gmx.net>
79 *
80 */
81
82#include "lwip/opt.h"
83
84#if LWIP_IPV4 && LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
85
86#include "lwip/stats.h"
87#include "lwip/mem.h"
88#include "lwip/udp.h"
89#include "lwip/ip_addr.h"
90#include "lwip/netif.h"
91#include "lwip/def.h"
92#include "lwip/dhcp.h"
93#include "lwip/autoip.h"
94#include "lwip/dns.h"
95#include "lwip/etharp.h"
96#include "lwip/prot/dhcp.h"
97
98#include <string.h>
99
103#ifndef DHCP_CREATE_RAND_XID
104#define DHCP_CREATE_RAND_XID 1
105#endif
106
112#ifdef DHCP_GLOBAL_XID_HEADER
113#include DHCP_GLOBAL_XID_HEADER /* include optional starting XID generation prototypes */
114#endif
115
118#define DHCP_MAX_MSG_LEN(netif) (netif->mtu)
119#define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576
121#define DHCP_MIN_REPLY_LEN 44
122
123#define REBOOT_TRIES 2
124
125#if LWIP_DNS && LWIP_DHCP_MAX_DNS_SERVERS
126#if DNS_MAX_SERVERS > LWIP_DHCP_MAX_DNS_SERVERS
127#define LWIP_DHCP_PROVIDE_DNS_SERVERS LWIP_DHCP_MAX_DNS_SERVERS
128#else
129#define LWIP_DHCP_PROVIDE_DNS_SERVERS DNS_MAX_SERVERS
130#endif
131#else
132#define LWIP_DHCP_PROVIDE_DNS_SERVERS 0
133#endif
134
140enum dhcp_option_idx {
141 DHCP_OPTION_IDX_OVERLOAD = 0,
142 DHCP_OPTION_IDX_MSG_TYPE,
143 DHCP_OPTION_IDX_SERVER_ID,
144 DHCP_OPTION_IDX_LEASE_TIME,
145 DHCP_OPTION_IDX_T1,
146 DHCP_OPTION_IDX_T2,
147 DHCP_OPTION_IDX_SUBNET_MASK,
148 DHCP_OPTION_IDX_ROUTER,
149#if LWIP_DHCP_PROVIDE_DNS_SERVERS
150 DHCP_OPTION_IDX_DNS_SERVER,
151 DHCP_OPTION_IDX_DNS_SERVER_LAST = DHCP_OPTION_IDX_DNS_SERVER + LWIP_DHCP_PROVIDE_DNS_SERVERS - 1,
152#endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
153#if LWIP_DHCP_GET_NTP_SRV
154 DHCP_OPTION_IDX_NTP_SERVER,
155 DHCP_OPTION_IDX_NTP_SERVER_LAST = DHCP_OPTION_IDX_NTP_SERVER + LWIP_DHCP_MAX_NTP_SERVERS - 1,
156#endif /* LWIP_DHCP_GET_NTP_SRV */
157 DHCP_OPTION_IDX_MAX
158};
159
162u32_t dhcp_rx_options_val[DHCP_OPTION_IDX_MAX];
166u8_t dhcp_rx_options_given[DHCP_OPTION_IDX_MAX];
167
168static u8_t dhcp_discover_request_options[] = {
169 DHCP_OPTION_SUBNET_MASK,
170 DHCP_OPTION_ROUTER,
171 DHCP_OPTION_BROADCAST
172#if LWIP_DHCP_PROVIDE_DNS_SERVERS
173 , DHCP_OPTION_DNS_SERVER
174#endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
175#if LWIP_DHCP_GET_NTP_SRV
176 , DHCP_OPTION_NTP
177#endif /* LWIP_DHCP_GET_NTP_SRV */
178 };
179
180#ifdef DHCP_GLOBAL_XID
181static u32_t xid;
182static u8_t xid_initialised;
183#endif /* DHCP_GLOBAL_XID */
184
185#define dhcp_option_given(dhcp, idx) (dhcp_rx_options_given[idx] != 0)
186#define dhcp_got_option(dhcp, idx) (dhcp_rx_options_given[idx] = 1)
187#define dhcp_clear_option(dhcp, idx) (dhcp_rx_options_given[idx] = 0)
188#define dhcp_clear_all_options(dhcp) (memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given)))
189#define dhcp_get_option_value(dhcp, idx) (dhcp_rx_options_val[idx])
190#define dhcp_set_option_value(dhcp, idx, val) (dhcp_rx_options_val[idx] = (val))
191
192static struct udp_pcb *dhcp_pcb;
193static u8_t dhcp_pcb_refcount;
194
195/* DHCP client state machine functions */
196static err_t dhcp_discover(struct netif *netif);
197static err_t dhcp_select(struct netif *netif);
198static void dhcp_bind(struct netif *netif);
199#if DHCP_DOES_ARP_CHECK
200static err_t dhcp_decline(struct netif *netif);
201#endif /* DHCP_DOES_ARP_CHECK */
202static err_t dhcp_rebind(struct netif *netif);
203static err_t dhcp_reboot(struct netif *netif);
204static void dhcp_set_state(struct dhcp *dhcp, u8_t new_state);
205
206/* receive, unfold, parse and free incoming messages */
207static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
208
209/* set the DHCP timers */
210static void dhcp_timeout(struct netif *netif);
211static void dhcp_t1_timeout(struct netif *netif);
212static void dhcp_t2_timeout(struct netif *netif);
213
214/* build outgoing messages */
215/* create a DHCP message, fill in common headers */
216static err_t dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type);
217/* free a DHCP request */
218static void dhcp_delete_msg(struct dhcp *dhcp);
219/* add a DHCP option (type, then length in bytes) */
220static void dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len);
221/* add option values */
222static void dhcp_option_byte(struct dhcp *dhcp, u8_t value);
223static void dhcp_option_short(struct dhcp *dhcp, u16_t value);
224static void dhcp_option_long(struct dhcp *dhcp, u32_t value);
225#if LWIP_NETIF_HOSTNAME
226static void dhcp_option_hostname(struct dhcp *dhcp, struct netif *netif);
227#endif /* LWIP_NETIF_HOSTNAME */
228/* always add the DHCP options trailer to end and pad */
229static void dhcp_option_trailer(struct dhcp *dhcp);
230
232static err_t
233dhcp_inc_pcb_refcount(void)
234{
235 if (dhcp_pcb_refcount == 0) {
236 LWIP_ASSERT("dhcp_inc_pcb_refcount(): memory leak", dhcp_pcb == NULL);
237
238 /* allocate UDP PCB */
239 dhcp_pcb = udp_new();
240
241 if (dhcp_pcb == NULL) {
242 return ERR_MEM;
243 }
244
245 ip_set_option(dhcp_pcb, SOF_BROADCAST);
246
247 /* set up local and remote port for the pcb -> listen on all interfaces on all src/dest IPs */
248 udp_bind(dhcp_pcb, IP4_ADDR_ANY, DHCP_CLIENT_PORT);
249 udp_connect(dhcp_pcb, IP4_ADDR_ANY, DHCP_SERVER_PORT);
250 udp_recv(dhcp_pcb, dhcp_recv, NULL);
251 }
252
253 dhcp_pcb_refcount++;
254
255 return ERR_OK;
256}
257
259static void
260dhcp_dec_pcb_refcount(void)
261{
262 LWIP_ASSERT("dhcp_pcb_refcount(): refcount error", (dhcp_pcb_refcount > 0));
263 dhcp_pcb_refcount--;
264
265 if (dhcp_pcb_refcount == 0) {
266 udp_remove(dhcp_pcb);
267 dhcp_pcb = NULL;
268 }
269}
270
283static void
284dhcp_handle_nak(struct netif *netif)
285{
286 struct dhcp *dhcp = netif_dhcp_data(netif);
287
288 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_nak(netif=%p) %c%c%"U16_F"\n",
289 (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
290 /* Change to a defined state - set this before assigning the address
291 to ensure the callback can use dhcp_supplied_address() */
292 dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF);
293 /* remove IP address from interface (must no longer be used, as per RFC2131) */
294 netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
295 /* We can immediately restart discovery */
296 dhcp_discover(netif);
297}
298
299#if DHCP_DOES_ARP_CHECK
309static void
310dhcp_check(struct netif *netif)
311{
312 struct dhcp *dhcp = netif_dhcp_data(netif);
313 err_t result;
314 u16_t msecs;
315 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0],
316 (s16_t)netif->name[1]));
317 dhcp_set_state(dhcp, DHCP_STATE_CHECKING);
318 /* create an ARP query for the offered IP address, expecting that no host
319 responds, as the IP address should not be in use. */
320 result = etharp_query(netif, &dhcp->offered_ip_addr, NULL);
321 if (result != ERR_OK) {
322 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("dhcp_check: could not perform ARP query\n"));
323 }
324 if (dhcp->tries < 255) {
325 dhcp->tries++;
326 }
327 msecs = 500;
328 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
329 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_check(): set request timeout %"U16_F" msecs\n", msecs));
330}
331#endif /* DHCP_DOES_ARP_CHECK */
332
338static void
339dhcp_handle_offer(struct netif *netif)
340{
341 struct dhcp *dhcp = netif_dhcp_data(netif);
342
343 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_offer(netif=%p) %c%c%"U16_F"\n",
344 (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
345 /* obtain the server address */
346 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SERVER_ID)) {
347 ip_addr_set_ip4_u32(&dhcp->server_ip_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SERVER_ID)));
348 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): server 0x%08"X32_F"\n",
349 ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr))));
350 /* remember offered address */
351 ip4_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
352 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): offer for 0x%08"X32_F"\n",
353 ip4_addr_get_u32(&dhcp->offered_ip_addr)));
354
355 dhcp_select(netif);
356 } else {
358 ("dhcp_handle_offer(netif=%p) did not get server ID!\n", (void*)netif));
359 }
360}
361
370static err_t
371dhcp_select(struct netif *netif)
372{
373 struct dhcp *dhcp = netif_dhcp_data(netif);
374 err_t result;
375 u16_t msecs;
376 u8_t i;
377
378 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_select(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
379 dhcp_set_state(dhcp, DHCP_STATE_REQUESTING);
380
381 /* create and initialize the DHCP message header */
382 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
383 if (result == ERR_OK) {
384 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
385 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
386
387 /* MUST request the offered IP address */
388 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
389 dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
390
391 dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
392 dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr))));
393
394 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
395 for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
396 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
397 }
398
399#if LWIP_NETIF_HOSTNAME
400 dhcp_option_hostname(dhcp, netif);
401#endif /* LWIP_NETIF_HOSTNAME */
402
403 dhcp_option_trailer(dhcp);
404 /* shrink the pbuf to the actual content length */
405 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
406
407 /* send broadcast to any DHCP server */
408 udp_sendto_if_src(dhcp_pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif, IP4_ADDR_ANY);
409 dhcp_delete_msg(dhcp);
410 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_select: REQUESTING\n"));
411 } else {
412 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("dhcp_select: could not allocate DHCP request\n"));
413 }
414 if (dhcp->tries < 255) {
415 dhcp->tries++;
416 }
417 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
418 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
419 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %"U16_F" msecs\n", msecs));
420 return result;
421}
422
427void
428dhcp_coarse_tmr(void)
429{
430 struct netif *netif = netif_list;
431 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_coarse_tmr()\n"));
432 /* iterate through all network interfaces */
433 while (netif != NULL) {
434 /* only act on DHCP configured interfaces */
435 struct dhcp *dhcp = netif_dhcp_data(netif);
436 if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF)) {
437 /* compare lease time to expire timeout */
438 if (dhcp->t0_timeout && (++dhcp->lease_used == dhcp->t0_timeout)) {
439 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t0 timeout\n"));
440 /* this clients' lease time has expired */
441 dhcp_release(netif);
442 dhcp_discover(netif);
443 /* timer is active (non zero), and triggers (zeroes) now? */
444 } else if (dhcp->t2_rebind_time && (dhcp->t2_rebind_time-- == 1)) {
445 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t2 timeout\n"));
446 /* this clients' rebind timeout triggered */
447 dhcp_t2_timeout(netif);
448 /* timer is active (non zero), and triggers (zeroes) now */
449 } else if (dhcp->t1_renew_time && (dhcp->t1_renew_time-- == 1)) {
450 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t1 timeout\n"));
451 /* this clients' renewal timeout triggered */
452 dhcp_t1_timeout(netif);
453 }
454 }
455 /* proceed to next netif */
456 netif = netif->next;
457 }
458}
459
467void
468dhcp_fine_tmr(void)
469{
470 struct netif *netif = netif_list;
471 /* loop through netif's */
472 while (netif != NULL) {
473 struct dhcp *dhcp = netif_dhcp_data(netif);
474 /* only act on DHCP configured interfaces */
475 if (dhcp != NULL) {
476 /* timer is active (non zero), and is about to trigger now */
477 if (dhcp->request_timeout > 1) {
478 dhcp->request_timeout--;
479 }
480 else if (dhcp->request_timeout == 1) {
481 dhcp->request_timeout--;
482 /* { netif->dhcp->request_timeout == 0 } */
483 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_fine_tmr(): request timeout\n"));
484 /* this client's request timeout triggered */
485 dhcp_timeout(netif);
486 }
487 }
488 /* proceed to next network interface */
489 netif = netif->next;
490 }
491}
492
501static void
502dhcp_timeout(struct netif *netif)
503{
504 struct dhcp *dhcp = netif_dhcp_data(netif);
505
506 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout()\n"));
507 /* back-off period has passed, or server selection timed out */
508 if ((dhcp->state == DHCP_STATE_BACKING_OFF) || (dhcp->state == DHCP_STATE_SELECTING)) {
509 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout(): restarting discovery\n"));
510 dhcp_discover(netif);
511 /* receiving the requested lease timed out */
512 } else if (dhcp->state == DHCP_STATE_REQUESTING) {
513 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n"));
514 if (dhcp->tries <= 5) {
515 dhcp_select(netif);
516 } else {
517 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, releasing, restarting\n"));
518 dhcp_release(netif);
519 dhcp_discover(netif);
520 }
521#if DHCP_DOES_ARP_CHECK
522 /* received no ARP reply for the offered address (which is good) */
523 } else if (dhcp->state == DHCP_STATE_CHECKING) {
524 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): CHECKING, ARP request timed out\n"));
525 if (dhcp->tries <= 1) {
526 dhcp_check(netif);
527 /* no ARP replies on the offered address,
528 looks like the IP address is indeed free */
529 } else {
530 /* bind the interface to the offered address */
531 dhcp_bind(netif);
532 }
533#endif /* DHCP_DOES_ARP_CHECK */
534 } else if (dhcp->state == DHCP_STATE_REBOOTING) {
535 if (dhcp->tries < REBOOT_TRIES) {
536 dhcp_reboot(netif);
537 } else {
538 dhcp_discover(netif);
539 }
540 }
541}
542
548static void
549dhcp_t1_timeout(struct netif *netif)
550{
551 struct dhcp *dhcp = netif_dhcp_data(netif);
552
553 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n"));
554 if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) ||
555 (dhcp->state == DHCP_STATE_RENEWING)) {
556 /* just retry to renew - note that the rebind timer (t2) will
557 * eventually time-out if renew tries fail. */
559 ("dhcp_t1_timeout(): must renew\n"));
560 /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
561 DHCP_STATE_RENEWING, not DHCP_STATE_BOUND */
562 dhcp_renew(netif);
563 /* Calculate next timeout */
564 if (((dhcp->t2_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
565 {
566 dhcp->t1_renew_time = ((dhcp->t2_timeout - dhcp->lease_used) / 2);
567 }
568 }
569}
570
576static void
577dhcp_t2_timeout(struct netif *netif)
578{
579 struct dhcp *dhcp = netif_dhcp_data(netif);
580
581 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout()\n"));
582 if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) ||
583 (dhcp->state == DHCP_STATE_RENEWING) || (dhcp->state == DHCP_STATE_REBINDING)) {
584 /* just retry to rebind */
586 ("dhcp_t2_timeout(): must rebind\n"));
587 /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
588 DHCP_STATE_REBINDING, not DHCP_STATE_BOUND */
589 dhcp_rebind(netif);
590 /* Calculate next timeout */
591 if (((dhcp->t0_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
592 {
593 dhcp->t2_rebind_time = ((dhcp->t0_timeout - dhcp->lease_used) / 2);
594 }
595 }
596}
597
603static void
604dhcp_handle_ack(struct netif *netif)
605{
606 struct dhcp *dhcp = netif_dhcp_data(netif);
607
608#if LWIP_DHCP_PROVIDE_DNS_SERVERS || LWIP_DHCP_GET_NTP_SRV
609 u8_t n;
610#endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS || LWIP_DHCP_GET_NTP_SRV */
611#if LWIP_DHCP_GET_NTP_SRV
612 ip4_addr_t ntp_server_addrs[LWIP_DHCP_MAX_NTP_SERVERS];
613#endif
614
615 /* clear options we might not get from the ACK */
616 ip4_addr_set_zero(&dhcp->offered_sn_mask);
617 ip4_addr_set_zero(&dhcp->offered_gw_addr);
618#if LWIP_DHCP_BOOTP_FILE
619 ip4_addr_set_zero(&dhcp->offered_si_addr);
620#endif /* LWIP_DHCP_BOOTP_FILE */
621
622 /* lease time given? */
623 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_LEASE_TIME)) {
624 /* remember offered lease time */
625 dhcp->offered_t0_lease = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_LEASE_TIME);
626 }
627 /* renewal period given? */
628 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T1)) {
629 /* remember given renewal period */
630 dhcp->offered_t1_renew = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T1);
631 } else {
632 /* calculate safe periods for renewal */
633 dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
634 }
635
636 /* renewal period given? */
637 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T2)) {
638 /* remember given rebind period */
639 dhcp->offered_t2_rebind = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T2);
640 } else {
641 /* calculate safe periods for rebinding (offered_t0_lease * 0.875 -> 87.5%)*/
642 dhcp->offered_t2_rebind = (dhcp->offered_t0_lease * 7U) / 8U;
643 }
644
645 /* (y)our internet address */
646 ip4_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
647
648#if LWIP_DHCP_BOOTP_FILE
649 /* copy boot server address,
650 boot file name copied in dhcp_parse_reply if not overloaded */
651 ip4_addr_copy(dhcp->offered_si_addr, dhcp->msg_in->siaddr);
652#endif /* LWIP_DHCP_BOOTP_FILE */
653
654 /* subnet mask given? */
655 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)) {
656 /* remember given subnet mask */
657 ip4_addr_set_u32(&dhcp->offered_sn_mask, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)));
658 dhcp->subnet_mask_given = 1;
659 } else {
660 dhcp->subnet_mask_given = 0;
661 }
662
663 /* gateway router */
664 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_ROUTER)) {
665 ip4_addr_set_u32(&dhcp->offered_gw_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_ROUTER)));
666 }
667
668#if LWIP_DHCP_GET_NTP_SRV
669 /* NTP servers */
670 for (n = 0; (n < LWIP_DHCP_MAX_NTP_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n); n++) {
671 ip4_addr_set_u32(&ntp_server_addrs[n], lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n)));
672 }
673 dhcp_set_ntp_servers(n, ntp_server_addrs);
674#endif /* LWIP_DHCP_GET_NTP_SRV */
675
676#if LWIP_DHCP_PROVIDE_DNS_SERVERS
677 /* DNS servers */
678 for (n = 0; (n < LWIP_DHCP_PROVIDE_DNS_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n); n++) {
679 ip_addr_t dns_addr;
680 ip_addr_set_ip4_u32(&dns_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
681 dns_setserver(n, &dns_addr);
682 }
683#endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
684}
685
694void
695dhcp_set_struct(struct netif *netif, struct dhcp *dhcp)
696{
697 LWIP_ASSERT("netif != NULL", netif != NULL);
698 LWIP_ASSERT("dhcp != NULL", dhcp != NULL);
699 LWIP_ASSERT("netif already has a struct dhcp set", netif_dhcp_data(netif) == NULL);
700
701 /* clear data structure */
702 memset(dhcp, 0, sizeof(struct dhcp));
703 /* dhcp_set_state(&dhcp, DHCP_STATE_OFF); */
704 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, dhcp);
705}
706
716void dhcp_cleanup(struct netif *netif)
717{
718 LWIP_ASSERT("netif != NULL", netif != NULL);
719
720 if (netif_dhcp_data(netif) != NULL) {
721 mem_free(netif_dhcp_data(netif));
722 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, NULL);
723 }
724}
725
739err_t
740dhcp_start(struct netif *netif)
741{
742 struct dhcp *dhcp;
743 err_t result;
744
745 LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;);
746 LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;);
747 dhcp = netif_dhcp_data(netif);
748 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
749
750 /* check MTU of the netif */
751 if (netif->mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
752 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): Cannot use this netif with DHCP: MTU is too small\n"));
753 return ERR_MEM;
754 }
755
756 /* no DHCP client attached yet? */
757 if (dhcp == NULL) {
758 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting new DHCP client\n"));
759 dhcp = (struct dhcp *)mem_malloc(sizeof(struct dhcp));
760 if (dhcp == NULL) {
761 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not allocate dhcp\n"));
762 return ERR_MEM;
763 }
764
765 /* store this dhcp client in the netif */
766 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, dhcp);
767 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): allocated dhcp"));
768 /* already has DHCP client attached */
769 } else {
770 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(): restarting DHCP configuration\n"));
771 LWIP_ASSERT("pbuf p_out wasn't freed", dhcp->p_out == NULL);
772 LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL );
773
774 if (dhcp->pcb_allocated != 0) {
775 dhcp_dec_pcb_refcount(); /* free DHCP PCB if not needed any more */
776 }
777 /* dhcp is cleared below, no need to reset flag*/
778 }
779
780 /* clear data structure */
781 memset(dhcp, 0, sizeof(struct dhcp));
782 /* dhcp_set_state(&dhcp, DHCP_STATE_OFF); */
783
784 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n"));
785
786 if (dhcp_inc_pcb_refcount() != ERR_OK) { /* ensure DHCP PCB is allocated */
787 return ERR_MEM;
788 }
789 dhcp->pcb_allocated = 1;
790
791#if LWIP_DHCP_CHECK_LINK_UP
792 if (!netif_is_link_up(netif)) {
793 /* set state INIT and wait for dhcp_network_changed() to call dhcp_discover() */
794 dhcp_set_state(dhcp, DHCP_STATE_INIT);
795 return ERR_OK;
796 }
797#endif /* LWIP_DHCP_CHECK_LINK_UP */
798
799
800 /* (re)start the DHCP negotiation */
801 result = dhcp_discover(netif);
802 if (result != ERR_OK) {
803 /* free resources allocated above */
804 dhcp_stop(netif);
805 return ERR_MEM;
806 }
807 return result;
808}
809
820void
821dhcp_inform(struct netif *netif)
822{
823 struct dhcp dhcp;
824 err_t result = ERR_OK;
825
826 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
827
828 if (dhcp_inc_pcb_refcount() != ERR_OK) { /* ensure DHCP PCB is allocated */
829 return;
830 }
831
832 memset(&dhcp, 0, sizeof(struct dhcp));
833 dhcp_set_state(&dhcp, DHCP_STATE_INFORMING);
834
835 /* create and initialize the DHCP message header */
836 result = dhcp_create_msg(netif, &dhcp, DHCP_INFORM);
837 if (result == ERR_OK) {
838 dhcp_option(&dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
839 dhcp_option_short(&dhcp, DHCP_MAX_MSG_LEN(netif));
840
841 dhcp_option_trailer(&dhcp);
842
843 pbuf_realloc(dhcp.p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp.options_out_len);
844
845 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_inform: INFORMING\n"));
846
847 udp_sendto_if(dhcp_pcb, dhcp.p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
848
849 dhcp_delete_msg(&dhcp);
850 } else {
851 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_inform: could not allocate DHCP request\n"));
852 }
853
854 dhcp_dec_pcb_refcount(); /* delete DHCP PCB if not needed any more */
855}
856
862void
863dhcp_network_changed(struct netif *netif)
864{
865 struct dhcp *dhcp = netif_dhcp_data(netif);
866
867 if (!dhcp)
868 return;
869 switch (dhcp->state) {
870 case DHCP_STATE_REBINDING:
871 case DHCP_STATE_RENEWING:
872 case DHCP_STATE_BOUND:
873 case DHCP_STATE_REBOOTING:
874 dhcp->tries = 0;
875 dhcp_reboot(netif);
876 break;
877 case DHCP_STATE_OFF:
878 /* stay off */
879 break;
880 default:
881 /* INIT/REQUESTING/CHECKING/BACKING_OFF restart with new 'rid' because the
882 state changes, SELECTING: continue with current 'rid' as we stay in the
883 same state */
884#if LWIP_DHCP_AUTOIP_COOP
885 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
886 autoip_stop(netif);
887 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
888 }
889#endif /* LWIP_DHCP_AUTOIP_COOP */
890 /* ensure we start with short timeouts, even if already discovering */
891 dhcp->tries = 0;
892 dhcp_discover(netif);
893 break;
894 }
895}
896
897#if DHCP_DOES_ARP_CHECK
905void
906dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr)
907{
908 struct dhcp *dhcp;
909
910 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
911 dhcp = netif_dhcp_data(netif);
912 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_arp_reply()\n"));
913 /* is a DHCP client doing an ARP check? */
914 if ((dhcp != NULL) && (dhcp->state == DHCP_STATE_CHECKING)) {
915 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n",
916 ip4_addr_get_u32(addr)));
917 /* did a host respond with the address we
918 were offered by the DHCP server? */
919 if (ip4_addr_cmp(addr, &dhcp->offered_ip_addr)) {
920 /* we will not accept the offered address */
922 ("dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
923 dhcp_decline(netif);
924 }
925 }
926}
927
937static err_t
938dhcp_decline(struct netif *netif)
939{
940 struct dhcp *dhcp = netif_dhcp_data(netif);
941 err_t result = ERR_OK;
942 u16_t msecs;
943 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline()\n"));
944 dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF);
945 /* create and initialize the DHCP message header */
946 result = dhcp_create_msg(netif, dhcp, DHCP_DECLINE);
947 if (result == ERR_OK) {
948 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
949 dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
950
951 dhcp_option_trailer(dhcp);
952 /* resize pbuf to reflect true size of options */
953 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
954
955 /* per section 4.4.4, broadcast DECLINE messages */
956 udp_sendto_if_src(dhcp_pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif, IP4_ADDR_ANY);
957 dhcp_delete_msg(dhcp);
958 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_decline: BACKING OFF\n"));
959 } else {
961 ("dhcp_decline: could not allocate DHCP request\n"));
962 }
963 if (dhcp->tries < 255) {
964 dhcp->tries++;
965 }
966 msecs = 10*1000;
967 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
968 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
969 return result;
970}
971#endif /* DHCP_DOES_ARP_CHECK */
972
973
979static err_t
980dhcp_discover(struct netif *netif)
981{
982 struct dhcp *dhcp = netif_dhcp_data(netif);
983 err_t result = ERR_OK;
984 u16_t msecs;
985 u8_t i;
986 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover()\n"));
987 ip4_addr_set_any(&dhcp->offered_ip_addr);
988 dhcp_set_state(dhcp, DHCP_STATE_SELECTING);
989 /* create and initialize the DHCP message header */
990 result = dhcp_create_msg(netif, dhcp, DHCP_DISCOVER);
991 if (result == ERR_OK) {
992 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n"));
993
994 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
995 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
996
997 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
998 for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
999 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1000 }
1001 dhcp_option_trailer(dhcp);
1002
1003 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: realloc()ing\n"));
1004 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1005
1006 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, DHCP_SERVER_PORT)\n"));
1007 udp_sendto_if_src(dhcp_pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif, IP4_ADDR_ANY);
1008 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: deleting()ing\n"));
1009 dhcp_delete_msg(dhcp);
1010 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover: SELECTING\n"));
1011 } else {
1012 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_discover: could not allocate DHCP request\n"));
1013 }
1014 if (dhcp->tries < 255) {
1015 dhcp->tries++;
1016 }
1017#if LWIP_DHCP_AUTOIP_COOP
1018 if (dhcp->tries >= LWIP_DHCP_AUTOIP_COOP_TRIES && dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_OFF) {
1019 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;
1020 autoip_start(netif);
1021 }
1022#endif /* LWIP_DHCP_AUTOIP_COOP */
1023 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
1024 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1025 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
1026 return result;
1027}
1028
1029
1035static void
1036dhcp_bind(struct netif *netif)
1037{
1038 u32_t timeout;
1039 struct dhcp *dhcp;
1040 ip4_addr_t sn_mask, gw_addr;
1041 LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;);
1042 dhcp = netif_dhcp_data(netif);
1043 LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;);
1044 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
1045
1046 /* reset time used of lease */
1047 dhcp->lease_used = 0;
1048
1049 if (dhcp->offered_t0_lease != 0xffffffffUL) {
1050 /* set renewal period timer */
1051 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t0 renewal timer %"U32_F" secs\n", dhcp->offered_t0_lease));
1052 timeout = (dhcp->offered_t0_lease + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1053 if (timeout > 0xffff) {
1054 timeout = 0xffff;
1055 }
1056 dhcp->t0_timeout = (u16_t)timeout;
1057 if (dhcp->t0_timeout == 0) {
1058 dhcp->t0_timeout = 1;
1059 }
1060 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t0_lease*1000));
1061 }
1062
1063 /* temporary DHCP lease? */
1064 if (dhcp->offered_t1_renew != 0xffffffffUL) {
1065 /* set renewal period timer */
1066 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));
1067 timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1068 if (timeout > 0xffff) {
1069 timeout = 0xffff;
1070 }
1071 dhcp->t1_timeout = (u16_t)timeout;
1072 if (dhcp->t1_timeout == 0) {
1073 dhcp->t1_timeout = 1;
1074 }
1075 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew*1000));
1076 dhcp->t1_renew_time = dhcp->t1_timeout;
1077 }
1078 /* set renewal period timer */
1079 if (dhcp->offered_t2_rebind != 0xffffffffUL) {
1080 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));
1081 timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1082 if (timeout > 0xffff) {
1083 timeout = 0xffff;
1084 }
1085 dhcp->t2_timeout = (u16_t)timeout;
1086 if (dhcp->t2_timeout == 0) {
1087 dhcp->t2_timeout = 1;
1088 }
1089 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind*1000));
1090 dhcp->t2_rebind_time = dhcp->t2_timeout;
1091 }
1092
1093 /* If we have sub 1 minute lease, t2 and t1 will kick in at the same time. */
1094 if ((dhcp->t1_timeout >= dhcp->t2_timeout) && (dhcp->t2_timeout > 0)) {
1095 dhcp->t1_timeout = 0;
1096 }
1097
1098 if (dhcp->subnet_mask_given) {
1099 /* copy offered network mask */
1100 ip4_addr_copy(sn_mask, dhcp->offered_sn_mask);
1101 } else {
1102 /* subnet mask not given, choose a safe subnet mask given the network class */
1103 u8_t first_octet = ip4_addr1(&dhcp->offered_ip_addr);
1104 if (first_octet <= 127) {
1105 ip4_addr_set_u32(&sn_mask, PP_HTONL(0xff000000UL));
1106 } else if (first_octet >= 192) {
1107 ip4_addr_set_u32(&sn_mask, PP_HTONL(0xffffff00UL));
1108 } else {
1109 ip4_addr_set_u32(&sn_mask, PP_HTONL(0xffff0000UL));
1110 }
1111 }
1112
1113 ip4_addr_copy(gw_addr, dhcp->offered_gw_addr);
1114 /* gateway address not given? */
1115 if (ip4_addr_isany_val(gw_addr)) {
1116 /* copy network address */
1117 ip4_addr_get_network(&gw_addr, &dhcp->offered_ip_addr, &sn_mask);
1118 /* use first host address on network as gateway */
1119 ip4_addr_set_u32(&gw_addr, ip4_addr_get_u32(&gw_addr) | PP_HTONL(0x00000001UL));
1120 }
1121
1122#if LWIP_DHCP_AUTOIP_COOP
1123 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1124 autoip_stop(netif);
1125 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1126 }
1127#endif /* LWIP_DHCP_AUTOIP_COOP */
1128
1129 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F" SN: 0x%08"X32_F" GW: 0x%08"X32_F"\n",
1130 ip4_addr_get_u32(&dhcp->offered_ip_addr), ip4_addr_get_u32(&sn_mask), ip4_addr_get_u32(&gw_addr)));
1131 /* netif is now bound to DHCP leased address - set this before assigning the address
1132 to ensure the callback can use dhcp_supplied_address() */
1133 dhcp_set_state(dhcp, DHCP_STATE_BOUND);
1134
1135 netif_set_addr(netif, &dhcp->offered_ip_addr, &sn_mask, &gw_addr);
1136 /* interface is used by routing now that an address is set */
1137}
1138
1145err_t
1146dhcp_renew(struct netif *netif)
1147{
1148 struct dhcp *dhcp = netif_dhcp_data(netif);
1149 err_t result;
1150 u16_t msecs;
1151 u8_t i;
1152 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_renew()\n"));
1153 dhcp_set_state(dhcp, DHCP_STATE_RENEWING);
1154
1155 /* create and initialize the DHCP message header */
1156 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1157 if (result == ERR_OK) {
1158 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1159 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1160
1161 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1162 for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1163 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1164 }
1165
1166#if LWIP_NETIF_HOSTNAME
1167 dhcp_option_hostname(dhcp, netif);
1168#endif /* LWIP_NETIF_HOSTNAME */
1169
1170 /* append DHCP message trailer */
1171 dhcp_option_trailer(dhcp);
1172
1173 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1174
1175 udp_sendto_if(dhcp_pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
1176 dhcp_delete_msg(dhcp);
1177
1178 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew: RENEWING\n"));
1179 } else {
1180 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_renew: could not allocate DHCP request\n"));
1181 }
1182 if (dhcp->tries < 255) {
1183 dhcp->tries++;
1184 }
1185 /* back-off on retries, but to a maximum of 20 seconds */
1186 msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
1187 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1188 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
1189 return result;
1190}
1191
1197static err_t
1198dhcp_rebind(struct netif *netif)
1199{
1200 struct dhcp *dhcp = netif_dhcp_data(netif);
1201 err_t result;
1202 u16_t msecs;
1203 u8_t i;
1204 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind()\n"));
1205 dhcp_set_state(dhcp, DHCP_STATE_REBINDING);
1206
1207 /* create and initialize the DHCP message header */
1208 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1209 if (result == ERR_OK) {
1210 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1211 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1212
1213 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1214 for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1215 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1216 }
1217
1218#if LWIP_NETIF_HOSTNAME
1219 dhcp_option_hostname(dhcp, netif);
1220#endif /* LWIP_NETIF_HOSTNAME */
1221
1222 dhcp_option_trailer(dhcp);
1223
1224 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1225
1226 /* broadcast to server */
1227 udp_sendto_if(dhcp_pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1228 dhcp_delete_msg(dhcp);
1229 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind: REBINDING\n"));
1230 } else {
1231 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_rebind: could not allocate DHCP request\n"));
1232 }
1233 if (dhcp->tries < 255) {
1234 dhcp->tries++;
1235 }
1236 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1237 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1238 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
1239 return result;
1240}
1241
1247static err_t
1248dhcp_reboot(struct netif *netif)
1249{
1250 struct dhcp *dhcp = netif_dhcp_data(netif);
1251 err_t result;
1252 u16_t msecs;
1253 u8_t i;
1254 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot()\n"));
1255 dhcp_set_state(dhcp, DHCP_STATE_REBOOTING);
1256
1257 /* create and initialize the DHCP message header */
1258 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1259 if (result == ERR_OK) {
1260 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1261 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN_MIN_REQUIRED);
1262
1263 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
1264 dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
1265
1266 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1267 for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1268 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1269 }
1270
1271 dhcp_option_trailer(dhcp);
1272
1273 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1274
1275 /* broadcast to server */
1276 udp_sendto_if(dhcp_pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1277 dhcp_delete_msg(dhcp);
1278 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot: REBOOTING\n"));
1279 } else {
1280 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_reboot: could not allocate DHCP request\n"));
1281 }
1282 if (dhcp->tries < 255) {
1283 dhcp->tries++;
1284 }
1285 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1286 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1287 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot(): set request timeout %"U16_F" msecs\n", msecs));
1288 return result;
1289}
1290
1291
1298err_t
1299dhcp_release(struct netif *netif)
1300{
1301 struct dhcp *dhcp = netif_dhcp_data(netif);
1302 err_t result;
1303 ip_addr_t server_ip_addr;
1304 u8_t is_dhcp_supplied_address;
1305
1306 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_release()\n"));
1307 if (dhcp == NULL) {
1308 return ERR_ARG;
1309 }
1310 ip_addr_copy(server_ip_addr, dhcp->server_ip_addr);
1311
1312 is_dhcp_supplied_address = dhcp_supplied_address(netif);
1313
1314 /* idle DHCP client */
1315 dhcp_set_state(dhcp, DHCP_STATE_OFF);
1316 /* clean old DHCP offer */
1317 ip_addr_set_zero_ip4(&dhcp->server_ip_addr);
1318 ip4_addr_set_zero(&dhcp->offered_ip_addr);
1319 ip4_addr_set_zero(&dhcp->offered_sn_mask);
1320 ip4_addr_set_zero(&dhcp->offered_gw_addr);
1321#if LWIP_DHCP_BOOTP_FILE
1322 ip4_addr_set_zero(&dhcp->offered_si_addr);
1323#endif /* LWIP_DHCP_BOOTP_FILE */
1324 dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
1325 dhcp->t1_renew_time = dhcp->t2_rebind_time = dhcp->lease_used = dhcp->t0_timeout = 0;
1326
1327 if (!is_dhcp_supplied_address) {
1328 /* don't issue release message when address is not dhcp-assigned */
1329 return ERR_OK;
1330 }
1331
1332 /* create and initialize the DHCP message header */
1333 result = dhcp_create_msg(netif, dhcp, DHCP_RELEASE);
1334 if (result == ERR_OK) {
1335 dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
1336 dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&server_ip_addr))));
1337
1338 dhcp_option_trailer(dhcp);
1339
1340 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1341
1342 udp_sendto_if(dhcp_pcb, dhcp->p_out, &server_ip_addr, DHCP_SERVER_PORT, netif);
1343 dhcp_delete_msg(dhcp);
1344 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_STATE_OFF\n"));
1345 } else {
1346 /* sending release failed, but that's not a problem since the correct behaviour of dhcp does not rely on release */
1347 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_release: could not allocate DHCP request\n"));
1348 }
1349 /* remove IP address from interface (prevents routing from selecting this interface) */
1350 netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
1351
1352 return result;
1353}
1354
1361void
1362dhcp_stop(struct netif *netif)
1363{
1364 struct dhcp *dhcp;
1365 LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;);
1366 dhcp = netif_dhcp_data(netif);
1367
1368 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_stop()\n"));
1369 /* netif is DHCP configured? */
1370 if (dhcp != NULL) {
1371#if LWIP_DHCP_AUTOIP_COOP
1372 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1373 autoip_stop(netif);
1374 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1375 }
1376#endif /* LWIP_DHCP_AUTOIP_COOP */
1377
1378 LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL);
1379 dhcp_set_state(dhcp, DHCP_STATE_OFF);
1380
1381 if (dhcp->pcb_allocated != 0) {
1382 dhcp_dec_pcb_refcount(); /* free DHCP PCB if not needed any more */
1383 dhcp->pcb_allocated = 0;
1384 }
1385 }
1386}
1387
1388/*
1389 * Set the DHCP state of a DHCP client.
1390 *
1391 * If the state changed, reset the number of tries.
1392 */
1393static void
1394dhcp_set_state(struct dhcp *dhcp, u8_t new_state)
1395{
1396 if (new_state != dhcp->state) {
1397 dhcp->state = new_state;
1398 dhcp->tries = 0;
1399 dhcp->request_timeout = 0;
1400 }
1401}
1402
1403/*
1404 * Concatenate an option type and length field to the outgoing
1405 * DHCP message.
1406 *
1407 */
1408static void
1409dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len)
1410{
1411 LWIP_ASSERT("dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN);
1412 dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
1413 dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
1414}
1415/*
1416 * Concatenate a single byte to the outgoing DHCP message.
1417 *
1418 */
1419static void
1420dhcp_option_byte(struct dhcp *dhcp, u8_t value)
1421{
1422 LWIP_ASSERT("dhcp_option_byte: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1423 dhcp->msg_out->options[dhcp->options_out_len++] = value;
1424}
1425
1426static void
1427dhcp_option_short(struct dhcp *dhcp, u16_t value)
1428{
1429 LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U <= DHCP_OPTIONS_LEN);
1430 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff00U) >> 8);
1431 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t) (value & 0x00ffU);
1432}
1433
1434static void
1435dhcp_option_long(struct dhcp *dhcp, u32_t value)
1436{
1437 LWIP_ASSERT("dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4U <= DHCP_OPTIONS_LEN);
1438 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff000000UL) >> 24);
1439 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x00ff0000UL) >> 16);
1440 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x0000ff00UL) >> 8);
1441 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x000000ffUL));
1442}
1443
1444#if LWIP_NETIF_HOSTNAME
1445static void
1446dhcp_option_hostname(struct dhcp *dhcp, struct netif *netif)
1447{
1448 if (netif->hostname != NULL) {
1449 size_t namelen = strlen(netif->hostname);
1450 if (namelen > 0) {
1451 size_t len;
1452 const char *p = netif->hostname;
1453 /* Shrink len to available bytes (need 2 bytes for OPTION_HOSTNAME
1454 and 1 byte for trailer) */
1455 size_t available = DHCP_OPTIONS_LEN - dhcp->options_out_len - 3;
1456 LWIP_ASSERT("DHCP: hostname is too long!", namelen <= available);
1457 len = LWIP_MIN(namelen, available);
1458 LWIP_ASSERT("DHCP: hostname is too long!", len <= 0xFF);
1459 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, (u8_t)len);
1460 while (len--) {
1461 dhcp_option_byte(dhcp, *p++);
1462 }
1463 }
1464 }
1465}
1466#endif /* LWIP_NETIF_HOSTNAME */
1467
1478static err_t
1479dhcp_parse_reply(struct dhcp *dhcp, struct pbuf *p)
1480{
1481 u8_t *options;
1482 u16_t offset;
1483 u16_t offset_max;
1484 u16_t options_idx;
1485 u16_t options_idx_max;
1486 struct pbuf *q;
1487 int parse_file_as_options = 0;
1488 int parse_sname_as_options = 0;
1489
1490 /* clear received options */
1491 dhcp_clear_all_options(dhcp);
1492 /* check that beginning of dhcp_msg (up to and including chaddr) is in first pbuf */
1493 if (p->len < DHCP_SNAME_OFS) {
1494 return ERR_BUF;
1495 }
1496 dhcp->msg_in = (struct dhcp_msg *)p->payload;
1497#if LWIP_DHCP_BOOTP_FILE
1498 /* clear boot file name */
1499 dhcp->boot_file_name[0] = 0;
1500#endif /* LWIP_DHCP_BOOTP_FILE */
1501
1502 /* parse options */
1503
1504 /* start with options field */
1505 options_idx = DHCP_OPTIONS_OFS;
1506 /* parse options to the end of the received packet */
1507 options_idx_max = p->tot_len;
1508again:
1509 q = p;
1510 while ((q != NULL) && (options_idx >= q->len)) {
1511 options_idx -= q->len;
1512 options_idx_max -= q->len;
1513 q = q->next;
1514 }
1515 if (q == NULL) {
1516 return ERR_BUF;
1517 }
1518 offset = options_idx;
1519 offset_max = options_idx_max;
1520 options = (u8_t*)q->payload;
1521 /* at least 1 byte to read and no end marker, then at least 3 bytes to read? */
1522 while ((q != NULL) && (options[offset] != DHCP_OPTION_END) && (offset < offset_max)) {
1523 u8_t op = options[offset];
1524 u8_t len;
1525 u8_t decode_len = 0;
1526 int decode_idx = -1;
1527 u16_t val_offset = offset + 2;
1528 /* len byte might be in the next pbuf */
1529 if ((offset + 1) < q->len) {
1530 len = options[offset + 1];
1531 } else {
1532 len = (q->next != NULL ? ((u8_t*)q->next->payload)[0] : 0);
1533 }
1534 /* LWIP_DEBUGF(DHCP_DEBUG, ("msg_offset=%"U16_F", q->len=%"U16_F, msg_offset, q->len)); */
1535 decode_len = len;
1536 switch(op) {
1537 /* case(DHCP_OPTION_END): handled above */
1538 case(DHCP_OPTION_PAD):
1539 /* special option: no len encoded */
1540 decode_len = len = 0;
1541 /* will be increased below */
1542 offset--;
1543 break;
1544 case(DHCP_OPTION_SUBNET_MASK):
1545 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1546 decode_idx = DHCP_OPTION_IDX_SUBNET_MASK;
1547 break;
1548 case(DHCP_OPTION_ROUTER):
1549 decode_len = 4; /* only copy the first given router */
1550 LWIP_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
1551 decode_idx = DHCP_OPTION_IDX_ROUTER;
1552 break;
1553#if LWIP_DHCP_PROVIDE_DNS_SERVERS
1554 case(DHCP_OPTION_DNS_SERVER):
1555 /* special case: there might be more than one server */
1556 LWIP_ERROR("len %% 4 == 0", len % 4 == 0, return ERR_VAL;);
1557 /* limit number of DNS servers */
1558 decode_len = LWIP_MIN(len, 4 * DNS_MAX_SERVERS);
1559 LWIP_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
1560 decode_idx = DHCP_OPTION_IDX_DNS_SERVER;
1561 break;
1562#endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
1563 case(DHCP_OPTION_LEASE_TIME):
1564 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1565 decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
1566 break;
1567#if LWIP_DHCP_GET_NTP_SRV
1568 case(DHCP_OPTION_NTP):
1569 /* special case: there might be more than one server */
1570 LWIP_ERROR("len %% 4 == 0", len % 4 == 0, return ERR_VAL;);
1571 /* limit number of NTP servers */
1572 decode_len = LWIP_MIN(len, 4 * LWIP_DHCP_MAX_NTP_SERVERS);
1573 LWIP_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
1574 decode_idx = DHCP_OPTION_IDX_NTP_SERVER;
1575 break;
1576#endif /* LWIP_DHCP_GET_NTP_SRV*/
1577 case(DHCP_OPTION_OVERLOAD):
1578 LWIP_ERROR("len == 1", len == 1, return ERR_VAL;);
1579 /* decode overload only in options, not in file/sname: invalid packet */
1580 LWIP_ERROR("overload in file/sname", options_idx == DHCP_OPTIONS_OFS, return ERR_VAL;);
1581 decode_idx = DHCP_OPTION_IDX_OVERLOAD;
1582 break;
1583 case(DHCP_OPTION_MESSAGE_TYPE):
1584 LWIP_ERROR("len == 1", len == 1, return ERR_VAL;);
1585 decode_idx = DHCP_OPTION_IDX_MSG_TYPE;
1586 break;
1587 case(DHCP_OPTION_SERVER_ID):
1588 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1589 decode_idx = DHCP_OPTION_IDX_SERVER_ID;
1590 break;
1591 case(DHCP_OPTION_T1):
1592 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1593 decode_idx = DHCP_OPTION_IDX_T1;
1594 break;
1595 case(DHCP_OPTION_T2):
1596 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1597 decode_idx = DHCP_OPTION_IDX_T2;
1598 break;
1599 default:
1600 decode_len = 0;
1601 LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %"U16_F" in options\n", (u16_t)op));
1602 break;
1603 }
1604 offset += len + 2;
1605 if (decode_len > 0) {
1606 u32_t value = 0;
1607 u16_t copy_len;
1608decode_next:
1609 LWIP_ASSERT("check decode_idx", decode_idx >= 0 && decode_idx < DHCP_OPTION_IDX_MAX);
1610 if (!dhcp_option_given(dhcp, decode_idx)) {
1611 copy_len = LWIP_MIN(decode_len, 4);
1612 if (pbuf_copy_partial(q, &value, copy_len, val_offset) != copy_len) {
1613 return ERR_BUF;
1614 }
1615 if (decode_len > 4) {
1616 /* decode more than one u32_t */
1617 LWIP_ERROR("decode_len %% 4 == 0", decode_len % 4 == 0, return ERR_VAL;);
1618 dhcp_got_option(dhcp, decode_idx);
1619 dhcp_set_option_value(dhcp, decode_idx, lwip_htonl(value));
1620 decode_len -= 4;
1621 val_offset += 4;
1622 decode_idx++;
1623 goto decode_next;
1624 } else if (decode_len == 4) {
1625 value = lwip_ntohl(value);
1626 } else {
1627 LWIP_ERROR("invalid decode_len", decode_len == 1, return ERR_VAL;);
1628 value = ((u8_t*)&value)[0];
1629 }
1630 dhcp_got_option(dhcp, decode_idx);
1631 dhcp_set_option_value(dhcp, decode_idx, value);
1632 }
1633 }
1634 if (offset >= q->len) {
1635 offset -= q->len;
1636 offset_max -= q->len;
1637 if ((offset < offset_max) && offset_max) {
1638 q = q->next;
1639 LWIP_ASSERT("next pbuf was null", q);
1640 options = (u8_t*)q->payload;
1641 } else {
1642 /* We've run out of bytes, probably no end marker. Don't proceed. */
1643 break;
1644 }
1645 }
1646 }
1647 /* is this an overloaded message? */
1648 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_OVERLOAD)) {
1649 u32_t overload = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1650 dhcp_clear_option(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1651 if (overload == DHCP_OVERLOAD_FILE) {
1652 parse_file_as_options = 1;
1653 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded file field\n"));
1654 } else if (overload == DHCP_OVERLOAD_SNAME) {
1655 parse_sname_as_options = 1;
1656 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname field\n"));
1657 } else if (overload == DHCP_OVERLOAD_SNAME_FILE) {
1658 parse_sname_as_options = 1;
1659 parse_file_as_options = 1;
1660 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname and file field\n"));
1661 } else {
1662 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("invalid overload option: %d\n", (int)overload));
1663 }
1664#if LWIP_DHCP_BOOTP_FILE
1665 if (!parse_file_as_options) {
1666 /* only do this for ACK messages */
1667 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) &&
1668 (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) == DHCP_ACK))
1669 /* copy bootp file name, don't care for sname (server hostname) */
1670 if (pbuf_copy_partial(p, dhcp->boot_file_name, DHCP_FILE_LEN-1, DHCP_FILE_OFS) != (DHCP_FILE_LEN-1)) {
1671 return ERR_BUF;
1672 }
1673 /* make sure the string is really NULL-terminated */
1674 dhcp->boot_file_name[DHCP_FILE_LEN-1] = 0;
1675 }
1676#endif /* LWIP_DHCP_BOOTP_FILE */
1677 }
1678 if (parse_file_as_options) {
1679 /* if both are overloaded, parse file first and then sname (RFC 2131 ch. 4.1) */
1680 parse_file_as_options = 0;
1681 options_idx = DHCP_FILE_OFS;
1682 options_idx_max = DHCP_FILE_OFS + DHCP_FILE_LEN;
1683 goto again;
1684 } else if (parse_sname_as_options) {
1685 parse_sname_as_options = 0;
1686 options_idx = DHCP_SNAME_OFS;
1687 options_idx_max = DHCP_SNAME_OFS + DHCP_SNAME_LEN;
1688 goto again;
1689 }
1690 return ERR_OK;
1691}
1692
1696static void
1697dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
1698{
1700 struct dhcp *dhcp = netif_dhcp_data(netif);
1701 struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload;
1702 u8_t msg_type;
1703 u8_t i;
1704
1705 LWIP_UNUSED_ARG(arg);
1706
1707 /* Caught DHCP message from netif that does not have DHCP enabled? -> not interested */
1708 if ((dhcp == NULL) || (dhcp->pcb_allocated == 0)) {
1709 goto free_pbuf_and_return;
1710 }
1711
1712 LWIP_ASSERT("invalid server address type", IP_IS_V4(addr));
1713
1714 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_recv(pbuf = %p) from DHCP server %"U16_F".%"U16_F".%"U16_F".%"U16_F" port %"U16_F"\n", (void*)p,
1715 ip4_addr1_16(ip_2_ip4(addr)), ip4_addr2_16(ip_2_ip4(addr)), ip4_addr3_16(ip_2_ip4(addr)), ip4_addr4_16(ip_2_ip4(addr)), port));
1716 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len));
1717 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len));
1718 /* prevent warnings about unused arguments */
1719 LWIP_UNUSED_ARG(pcb);
1720 LWIP_UNUSED_ARG(addr);
1721 LWIP_UNUSED_ARG(port);
1722
1723 LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL);
1724
1725 if (p->len < DHCP_MIN_REPLY_LEN) {
1726 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP reply message or pbuf too short\n"));
1727 goto free_pbuf_and_return;
1728 }
1729
1730 if (reply_msg->op != DHCP_BOOTREPLY) {
1731 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("not a DHCP reply message, but type %"U16_F"\n", (u16_t)reply_msg->op));
1732 goto free_pbuf_and_return;
1733 }
1734 /* iterate through hardware address and match against DHCP message */
1735 for (i = 0; i < netif->hwaddr_len && i < NETIF_MAX_HWADDR_LEN && i < DHCP_CHADDR_LEN; i++) {
1736 if (netif->hwaddr[i] != reply_msg->chaddr[i]) {
1738 ("netif->hwaddr[%"U16_F"]==%02"X16_F" != reply_msg->chaddr[%"U16_F"]==%02"X16_F"\n",
1739 (u16_t)i, (u16_t)netif->hwaddr[i], (u16_t)i, (u16_t)reply_msg->chaddr[i]));
1740 goto free_pbuf_and_return;
1741 }
1742 }
1743 /* match transaction ID against what we expected */
1744 if (lwip_ntohl(reply_msg->xid) != dhcp->xid) {
1746 ("transaction id mismatch reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n",lwip_ntohl(reply_msg->xid),dhcp->xid));
1747 goto free_pbuf_and_return;
1748 }
1749 /* option fields could be unfold? */
1750 if (dhcp_parse_reply(dhcp, p) != ERR_OK) {
1752 ("problem unfolding DHCP message - too short on memory?\n"));
1753 goto free_pbuf_and_return;
1754 }
1755
1756 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("searching DHCP_OPTION_MESSAGE_TYPE\n"));
1757 /* obtain pointer to DHCP message type */
1758 if (!dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE)) {
1759 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP_OPTION_MESSAGE_TYPE option not found\n"));
1760 goto free_pbuf_and_return;
1761 }
1762
1763 /* read DHCP message type */
1764 msg_type = (u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE);
1765 /* message type is DHCP ACK? */
1766 if (msg_type == DHCP_ACK) {
1767 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_ACK received\n"));
1768 /* in requesting state? */
1769 if (dhcp->state == DHCP_STATE_REQUESTING) {
1770 dhcp_handle_ack(netif);
1771#if DHCP_DOES_ARP_CHECK
1772 if ((netif->flags & NETIF_FLAG_ETHARP) != 0) {
1773 /* check if the acknowledged lease address is already in use */
1774 dhcp_check(netif);
1775 } else {
1776 /* bind interface to the acknowledged lease address */
1777 dhcp_bind(netif);
1778 }
1779#else
1780 /* bind interface to the acknowledged lease address */
1781 dhcp_bind(netif);
1782#endif
1783 }
1784 /* already bound to the given lease address? */
1785 else if ((dhcp->state == DHCP_STATE_REBOOTING) || (dhcp->state == DHCP_STATE_REBINDING) ||
1786 (dhcp->state == DHCP_STATE_RENEWING)) {
1787 dhcp_handle_ack(netif);
1788 dhcp_bind(netif);
1789 }
1790 }
1791 /* received a DHCP_NAK in appropriate state? */
1792 else if ((msg_type == DHCP_NAK) &&
1793 ((dhcp->state == DHCP_STATE_REBOOTING) || (dhcp->state == DHCP_STATE_REQUESTING) ||
1794 (dhcp->state == DHCP_STATE_REBINDING) || (dhcp->state == DHCP_STATE_RENEWING ))) {
1795 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_NAK received\n"));
1796 dhcp_handle_nak(netif);
1797 }
1798 /* received a DHCP_OFFER in DHCP_STATE_SELECTING state? */
1799 else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_STATE_SELECTING)) {
1800 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_OFFER received in DHCP_STATE_SELECTING state\n"));
1801 dhcp->request_timeout = 0;
1802 /* remember offered lease */
1803 dhcp_handle_offer(netif);
1804 }
1805
1806free_pbuf_and_return:
1807 if (dhcp != NULL) {
1808 dhcp->msg_in = NULL;
1809 }
1810 pbuf_free(p);
1811}
1812
1820static err_t
1821dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type)
1822{
1823 u16_t i;
1824#ifndef DHCP_GLOBAL_XID
1829#if DHCP_CREATE_RAND_XID && defined(LWIP_RAND)
1830 static u32_t xid;
1831#else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1832 static u32_t xid = 0xABCD0000;
1833#endif /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1834#else
1835 if (!xid_initialised) {
1836 xid = DHCP_GLOBAL_XID;
1837 xid_initialised = !xid_initialised;
1838 }
1839#endif
1840 LWIP_ERROR("dhcp_create_msg: netif != NULL", (netif != NULL), return ERR_ARG;);
1841 LWIP_ERROR("dhcp_create_msg: dhcp != NULL", (dhcp != NULL), return ERR_VAL;);
1842 LWIP_ASSERT("dhcp_create_msg: dhcp->p_out == NULL", dhcp->p_out == NULL);
1843 LWIP_ASSERT("dhcp_create_msg: dhcp->msg_out == NULL", dhcp->msg_out == NULL);
1844 dhcp->p_out = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcp_msg), PBUF_RAM);
1845 if (dhcp->p_out == NULL) {
1847 ("dhcp_create_msg(): could not allocate pbuf\n"));
1848 return ERR_MEM;
1849 }
1850 LWIP_ASSERT("dhcp_create_msg: check that first pbuf can hold struct dhcp_msg",
1851 (dhcp->p_out->len >= sizeof(struct dhcp_msg)));
1852
1853 /* DHCP_REQUEST should reuse 'xid' from DHCPOFFER */
1854 if (message_type != DHCP_REQUEST) {
1855 /* reuse transaction identifier in retransmissions */
1856 if (dhcp->tries == 0) {
1857#if DHCP_CREATE_RAND_XID && defined(LWIP_RAND)
1858 xid = LWIP_RAND();
1859#else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1860 xid++;
1861#endif /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1862 }
1863 dhcp->xid = xid;
1864 }
1866 ("transaction id xid(%"X32_F")\n", xid));
1867
1868 dhcp->msg_out = (struct dhcp_msg *)dhcp->p_out->payload;
1869
1870 dhcp->msg_out->op = DHCP_BOOTREQUEST;
1871 /* @todo: make link layer independent */
1872 dhcp->msg_out->htype = DHCP_HTYPE_ETH;
1873 dhcp->msg_out->hlen = netif->hwaddr_len;
1874 dhcp->msg_out->hops = 0;
1875 dhcp->msg_out->xid = lwip_htonl(dhcp->xid);
1876 dhcp->msg_out->secs = 0;
1877 /* we don't need the broadcast flag since we can receive unicast traffic
1878 before being fully configured! */
1879 dhcp->msg_out->flags = 0;
1880 ip4_addr_set_zero(&dhcp->msg_out->ciaddr);
1881 /* set ciaddr to netif->ip_addr based on message_type and state */
1882 if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) || (message_type == DHCP_RELEASE) ||
1883 ((message_type == DHCP_REQUEST) && /* DHCP_STATE_BOUND not used for sending! */
1884 ((dhcp->state== DHCP_STATE_RENEWING) || dhcp->state== DHCP_STATE_REBINDING))) {
1885 ip4_addr_copy(dhcp->msg_out->ciaddr, *netif_ip4_addr(netif));
1886 }
1887 ip4_addr_set_zero(&dhcp->msg_out->yiaddr);
1888 ip4_addr_set_zero(&dhcp->msg_out->siaddr);
1889 ip4_addr_set_zero(&dhcp->msg_out->giaddr);
1890 for (i = 0; i < DHCP_CHADDR_LEN; i++) {
1891 /* copy netif hardware address, pad with zeroes */
1892 dhcp->msg_out->chaddr[i] = (i < netif->hwaddr_len && i < NETIF_MAX_HWADDR_LEN) ? netif->hwaddr[i] : 0/* pad byte*/;
1893 }
1894 for (i = 0; i < DHCP_SNAME_LEN; i++) {
1895 dhcp->msg_out->sname[i] = 0;
1896 }
1897 for (i = 0; i < DHCP_FILE_LEN; i++) {
1898 dhcp->msg_out->file[i] = 0;
1899 }
1900 dhcp->msg_out->cookie = PP_HTONL(DHCP_MAGIC_COOKIE);
1901 dhcp->options_out_len = 0;
1902 /* fill options field with an incrementing array (for debugging purposes) */
1903 for (i = 0; i < DHCP_OPTIONS_LEN; i++) {
1904 dhcp->msg_out->options[i] = (u8_t)i; /* for debugging only, no matter if truncated */
1905 }
1906 /* Add option MESSAGE_TYPE */
1907 dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
1908 dhcp_option_byte(dhcp, message_type);
1909 return ERR_OK;
1910}
1911
1917static void
1918dhcp_delete_msg(struct dhcp *dhcp)
1919{
1920 LWIP_ERROR("dhcp_delete_msg: dhcp != NULL", (dhcp != NULL), return;);
1921 LWIP_ASSERT("dhcp_delete_msg: dhcp->p_out != NULL", dhcp->p_out != NULL);
1922 LWIP_ASSERT("dhcp_delete_msg: dhcp->msg_out != NULL", dhcp->msg_out != NULL);
1923 if (dhcp->p_out != NULL) {
1924 pbuf_free(dhcp->p_out);
1925 }
1926 dhcp->p_out = NULL;
1927 dhcp->msg_out = NULL;
1928}
1929
1938static void
1939dhcp_option_trailer(struct dhcp *dhcp)
1940{
1941 LWIP_ERROR("dhcp_option_trailer: dhcp != NULL", (dhcp != NULL), return;);
1942 LWIP_ASSERT("dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out != NULL);
1943 LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1944 dhcp->msg_out->options[dhcp->options_out_len++] = DHCP_OPTION_END;
1945 /* packet is too small, or not 4 byte aligned? */
1946 while (((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) &&
1947 (dhcp->options_out_len < DHCP_OPTIONS_LEN)) {
1948 /* add a fill/padding byte */
1949 dhcp->msg_out->options[dhcp->options_out_len++] = 0;
1950 }
1951}
1952
1959u8_t
1960dhcp_supplied_address(const struct netif *netif)
1961{
1962 if ((netif != NULL) && (netif_dhcp_data(netif) != NULL)) {
1963 struct dhcp* dhcp = netif_dhcp_data(netif);
1964 return (dhcp->state == DHCP_STATE_BOUND) || (dhcp->state == DHCP_STATE_RENEWING);
1965 }
1966 return 0;
1967}
1968
1969#endif /* LWIP_IPV4 && LWIP_DHCP */
#define LWIP_DEBUGF(debug, message)
Definition debug.h:183
#define LWIP_UNUSED_ARG(x)
Definition arch.h:327
#define LWIP_DBG_LEVEL_SERIOUS
Definition debug.h:76
#define LWIP_DBG_STATE
Definition debug.h:104
#define LWIP_DBG_LEVEL_WARNING
Definition debug.h:74
#define LWIP_DBG_TRACE
Definition debug.h:102
s8_t err_t
Definition err.h:76
@ ERR_BUF
Definition err.h:86
@ ERR_OK
Definition err.h:82
@ ERR_VAL
Definition err.h:94
@ ERR_MEM
Definition err.h:84
@ ERR_ARG
Definition err.h:115
#define LWIP_DHCP_AUTOIP_COOP_TRIES
Definition opt.h:955
#define DHCP_DEBUG
Definition opt.h:2847
#define LWIP_DHCP_MAX_NTP_SERVERS
Definition opt.h:902
#define DNS_MAX_SERVERS
Definition opt.h:1048
#define NETIF_FLAG_ETHARP
Definition netif.h:110
#define netif_is_up(netif)
Definition netif.h:420
void pbuf_realloc(struct pbuf *p, u16_t new_len)
Definition pbuf.c:512
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition pbuf.c:267
u8_t pbuf_free(struct pbuf *p)
Definition pbuf.c:734
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition pbuf.c:1034
@ PBUF_RAM
Definition pbuf.h:127
@ PBUF_TRANSPORT
Definition pbuf.h:95
#define ip_set_option(pcb, opt)
Definition ip.h:236
#define ip_current_input_netif()
Definition ip.h:156
void * mem_malloc(mem_size_t size)
Definition mem.c:622
void mem_free(void *rmem)
Definition mem.c:438
struct netif * netif_list
Definition netif.c:123
#define netif_is_link_up(netif)
Definition netif.h:432
#define NETIF_MAX_HWADDR_LEN
Definition netif.h:82
#define DHCP_HTYPE_ETH
Definition prot/dhcp.h:151
#define DHCP_OPTIONS_LEN
Definition prot/dhcp.h:109
Definition netif.h:244
u8_t flags
Definition netif.h:324
char name[2]
Definition netif.h:326
u8_t num
Definition netif.h:328
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
Definition netif.h:322
u16_t mtu
Definition netif.h:318
struct netif * next
Definition netif.h:246
u8_t hwaddr_len
Definition netif.h:320
Definition pbuf.h:161
u16_t tot_len
Definition pbuf.h:175
struct pbuf * next
Definition pbuf.h:163
u16_t len
Definition pbuf.h:178
void * payload
Definition pbuf.h:166