70 #if LWIP_IPV4 && LWIP_DHCP 88 #ifndef DHCP_CREATE_RAND_XID 89 #define DHCP_CREATE_RAND_XID 1 97 #ifdef DHCP_GLOBAL_XID_HEADER 98 #include DHCP_GLOBAL_XID_HEADER 103 #define DHCP_MAX_MSG_LEN(netif) (netif->mtu) 104 #define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576 106 #define DHCP_MIN_REPLY_LEN 44 108 #define REBOOT_TRIES 2 115 #define DHCP_OPTION_IDX_OVERLOAD 0 116 #define DHCP_OPTION_IDX_MSG_TYPE 1 117 #define DHCP_OPTION_IDX_SERVER_ID 2 118 #define DHCP_OPTION_IDX_LEASE_TIME 3 119 #define DHCP_OPTION_IDX_T1 4 120 #define DHCP_OPTION_IDX_T2 5 121 #define DHCP_OPTION_IDX_SUBNET_MASK 6 122 #define DHCP_OPTION_IDX_ROUTER 7 123 #define DHCP_OPTION_IDX_DNS_SERVER 8 124 #if LWIP_DHCP_GET_NTP_SRV 125 #define DHCP_OPTION_IDX_NTP_SERVER (DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS) 126 #define DHCP_OPTION_IDX_MAX (DHCP_OPTION_IDX_NTP_SERVER + LWIP_DHCP_MAX_NTP_SERVERS) 128 #define DHCP_OPTION_IDX_MAX (DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS) 133 u32_t dhcp_rx_options_val[DHCP_OPTION_IDX_MAX];
137 u8_t dhcp_rx_options_given[DHCP_OPTION_IDX_MAX];
139 static u8_t dhcp_discover_select_options[] = {
140 DHCP_OPTION_SUBNET_MASK,
142 DHCP_OPTION_BROADCAST,
143 DHCP_OPTION_DNS_SERVER
144 #if LWIP_DHCP_GET_NTP_SRV 149 #ifdef DHCP_GLOBAL_XID 151 static u8_t xid_initialised;
154 #define dhcp_option_given(dhcp, idx) (dhcp_rx_options_given[idx] != 0) 155 #define dhcp_got_option(dhcp, idx) (dhcp_rx_options_given[idx] = 1) 156 #define dhcp_clear_option(dhcp, idx) (dhcp_rx_options_given[idx] = 0) 157 #define dhcp_clear_all_options(dhcp) (memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given))) 158 #define dhcp_get_option_value(dhcp, idx) (dhcp_rx_options_val[idx]) 159 #define dhcp_set_option_value(dhcp, idx, val) (dhcp_rx_options_val[idx] = (val)) 166 #if DHCP_DOES_ARP_CHECK 171 static void dhcp_set_state(
struct dhcp *dhcp,
u8_t new_state);
174 static void dhcp_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr,
u16_t port);
177 static void dhcp_timeout(
struct netif *
netif);
178 static void dhcp_t1_timeout(
struct netif *
netif);
179 static void dhcp_t2_timeout(
struct netif *
netif);
185 static void dhcp_delete_msg(
struct dhcp *dhcp);
187 static void dhcp_option(
struct dhcp *dhcp,
u8_t option_type,
u8_t option_len);
189 static void dhcp_option_byte(
struct dhcp *dhcp,
u8_t value);
190 static void dhcp_option_short(
struct dhcp *dhcp,
u16_t value);
191 static void dhcp_option_long(
struct dhcp *dhcp,
u32_t value);
192 #if LWIP_NETIF_HOSTNAME 193 static void dhcp_option_hostname(
struct dhcp *dhcp,
struct netif *
netif);
196 static void dhcp_option_trailer(
struct dhcp *dhcp);
213 struct dhcp *dhcp = netif->dhcp;
217 netif_set_addr(netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY);
219 dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF);
221 dhcp_discover(netif);
224 #if DHCP_DOES_ARP_CHECK 235 dhcp_check(
struct netif *netif)
237 struct dhcp *dhcp = netif->dhcp;
242 dhcp_set_state(dhcp, DHCP_STATE_CHECKING);
245 result = etharp_query(netif, &dhcp->offered_ip_addr,
NULL);
249 if (dhcp->tries < 255) {
253 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
264 dhcp_handle_offer(
struct netif *netif)
266 struct dhcp *dhcp = netif->dhcp;
270 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SERVER_ID)) {
271 ip_addr_set_ip4_u32(&dhcp->server_ip_addr,
htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SERVER_ID)));
273 ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr))));
275 ip4_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
277 ip4_addr_get_u32(&dhcp->offered_ip_addr)));
282 (
"dhcp_handle_offer(netif=%p) did not get server ID!\n", (
void*)netif));
295 dhcp_select(
struct netif *netif)
297 struct dhcp *dhcp = netif->dhcp;
303 dhcp_set_state(dhcp, DHCP_STATE_REQUESTING);
306 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
308 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
309 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
312 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
313 dhcp_option_long(dhcp,
ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
315 dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
316 dhcp_option_long(dhcp,
ntohl(ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr))));
318 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST,
sizeof(dhcp_discover_select_options));
319 for (i = 0; i <
sizeof(dhcp_discover_select_options); i++) {
320 dhcp_option_byte(dhcp, dhcp_discover_select_options[i]);
323 #if LWIP_NETIF_HOSTNAME 324 dhcp_option_hostname(dhcp, netif);
327 dhcp_option_trailer(dhcp);
329 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
332 udp_sendto_if_src(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif, IP_ADDR_ANY);
333 dhcp_delete_msg(dhcp);
338 if (dhcp->tries < 255) {
341 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
342 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
351 dhcp_coarse_tmr(
void)
356 while (netif !=
NULL) {
358 struct dhcp* dhcp = netif->dhcp;
359 if ((dhcp !=
NULL) && (dhcp->state != DHCP_STATE_OFF)) {
361 if (dhcp->t0_timeout && (++dhcp->lease_used == dhcp->t0_timeout)) {
365 dhcp_discover(netif);
367 }
else if (dhcp->t2_rebind_time && (dhcp->t2_rebind_time-- == 1)) {
370 dhcp_t2_timeout(netif);
372 }
else if (dhcp->t1_renew_time && (dhcp->t1_renew_time-- == 1)) {
375 dhcp_t1_timeout(netif);
394 while (netif !=
NULL) {
396 if (netif->dhcp !=
NULL) {
398 if (netif->dhcp->request_timeout > 1) {
399 netif->dhcp->request_timeout--;
401 else if (netif->dhcp->request_timeout == 1) {
402 netif->dhcp->request_timeout--;
423 dhcp_timeout(
struct netif *netif)
425 struct dhcp *dhcp = netif->dhcp;
428 if ((dhcp->state == DHCP_STATE_BACKING_OFF) || (dhcp->state == DHCP_STATE_SELECTING)) {
430 dhcp_discover(netif);
432 }
else if (dhcp->state == DHCP_STATE_REQUESTING) {
434 if (dhcp->tries <= 5) {
439 dhcp_discover(netif);
441 #if DHCP_DOES_ARP_CHECK 443 }
else if (dhcp->state == DHCP_STATE_CHECKING) {
445 if (dhcp->tries <= 1) {
454 }
else if (dhcp->state == DHCP_STATE_REBOOTING) {
455 if (dhcp->tries < REBOOT_TRIES) {
458 dhcp_discover(netif);
469 dhcp_t1_timeout(
struct netif *netif)
471 struct dhcp *dhcp = netif->dhcp;
473 if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) ||
474 (dhcp->state == DHCP_STATE_RENEWING)) {
478 (
"dhcp_t1_timeout(): must renew\n"));
483 if (((netif->dhcp->t2_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
485 netif->dhcp->t1_renew_time = ((netif->dhcp->t2_timeout - dhcp->lease_used) / 2);
496 dhcp_t2_timeout(
struct netif *netif)
498 struct dhcp *dhcp = netif->dhcp;
500 if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) ||
501 (dhcp->state == DHCP_STATE_RENEWING) || (dhcp->state == DHCP_STATE_REBINDING)) {
504 (
"dhcp_t2_timeout(): must rebind\n"));
509 if (((netif->dhcp->t0_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
511 netif->dhcp->t2_rebind_time = ((netif->dhcp->t0_timeout - dhcp->lease_used) / 2);
522 dhcp_handle_ack(
struct netif *netif)
524 struct dhcp *dhcp = netif->dhcp;
525 #if LWIP_DNS || LWIP_DHCP_GET_NTP_SRV 528 #if LWIP_DHCP_GET_NTP_SRV 533 ip4_addr_set_zero(&dhcp->offered_sn_mask);
534 ip4_addr_set_zero(&dhcp->offered_gw_addr);
535 #if LWIP_DHCP_BOOTP_FILE 536 ip4_addr_set_zero(&dhcp->offered_si_addr);
540 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_LEASE_TIME)) {
542 dhcp->offered_t0_lease = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_LEASE_TIME);
545 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T1)) {
547 dhcp->offered_t1_renew = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T1);
550 dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
554 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T2)) {
556 dhcp->offered_t2_rebind = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T2);
559 dhcp->offered_t2_rebind = (dhcp->offered_t0_lease * 7U) / 8U;
563 ip4_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
565 #if LWIP_DHCP_BOOTP_FILE 568 ip_addr_copy(dhcp->offered_si_addr, dhcp->msg_in->siaddr);
572 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)) {
574 ip4_addr_set_u32(&dhcp->offered_sn_mask,
htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)));
575 dhcp->subnet_mask_given = 1;
577 dhcp->subnet_mask_given = 0;
581 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_ROUTER)) {
582 ip4_addr_set_u32(&dhcp->offered_gw_addr,
htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_ROUTER)));
585 #if LWIP_DHCP_GET_NTP_SRV 588 ip4_addr_set_u32(&ntp_server_addrs[n],
htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n)));
590 dhcp_set_ntp_servers(n, ntp_server_addrs);
595 for (n = 0; (n <
DNS_MAX_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n); n++) {
597 ip_addr_set_ip4_u32(&dns_addr,
htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
598 dns_setserver(n, &dns_addr);
610 dhcp_set_struct(
struct netif *netif,
struct dhcp *dhcp)
614 LWIP_ASSERT(
"netif already has a struct dhcp set", netif->dhcp ==
NULL);
617 memset(dhcp, 0,
sizeof(
struct dhcp));
629 void dhcp_cleanup(
struct netif *netif)
633 if (netif->dhcp !=
NULL) {
652 dhcp_start(
struct netif *netif)
669 if (netif->
mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
677 dhcp = (
struct dhcp *)
mem_malloc(
sizeof(
struct dhcp));
688 if (dhcp->pcb !=
NULL) {
689 udp_remove(dhcp->pcb);
696 memset(dhcp, 0,
sizeof(
struct dhcp));
699 dhcp->pcb = udp_new();
700 if (dhcp->pcb ==
NULL) {
706 udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
707 udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
709 udp_recv(dhcp->pcb, dhcp_recv, netif);
712 #if LWIP_DHCP_CHECK_LINK_UP 715 dhcp_set_state(dhcp, DHCP_STATE_INIT);
721 result = dhcp_discover(netif);
740 dhcp_inform(
struct netif *netif)
748 memset(&dhcp, 0,
sizeof(
struct dhcp));
749 dhcp_set_state(&dhcp, DHCP_STATE_INFORMING);
751 if ((netif->dhcp !=
NULL) && (netif->dhcp->pcb !=
NULL)) {
753 pcb = netif->dhcp->pcb;
762 udp_bind(dhcp.pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
766 result = dhcp_create_msg(netif, &dhcp, DHCP_INFORM);
768 dhcp_option(&dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
769 dhcp_option_short(&dhcp, DHCP_MAX_MSG_LEN(netif));
771 dhcp_option_trailer(&dhcp);
773 pbuf_realloc(dhcp.p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp.options_out_len);
776 udp_sendto_if(pcb, dhcp.p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
777 dhcp_delete_msg(&dhcp);
782 if (dhcp.pcb !=
NULL) {
784 udp_remove(dhcp.pcb);
794 dhcp_network_changed(
struct netif *netif)
796 struct dhcp *dhcp = netif->dhcp;
799 switch (dhcp->state) {
800 case DHCP_STATE_REBINDING:
801 case DHCP_STATE_RENEWING:
802 case DHCP_STATE_BOUND:
803 case DHCP_STATE_REBOOTING:
814 #if LWIP_DHCP_AUTOIP_COOP 815 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
817 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
822 dhcp_discover(netif);
827 #if DHCP_DOES_ARP_CHECK 834 void dhcp_arp_reply(
struct netif *netif,
const ip4_addr_t *addr)
839 if ((netif->dhcp !=
NULL) && (netif->dhcp->
state == DHCP_STATE_CHECKING)) {
841 ip4_addr_get_u32(addr)));
844 if (ip4_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) {
847 (
"dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
863 dhcp_decline(
struct netif *netif)
865 struct dhcp *dhcp = netif->dhcp;
869 dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF);
871 result = dhcp_create_msg(netif, dhcp, DHCP_DECLINE);
873 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
874 dhcp_option_long(dhcp,
ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
876 dhcp_option_trailer(dhcp);
878 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
881 udp_sendto_if_src(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif, IP_ADDR_ANY);
882 dhcp_delete_msg(dhcp);
886 (
"dhcp_decline: could not allocate DHCP request\n"));
888 if (dhcp->tries < 255) {
892 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
905 dhcp_discover(
struct netif *netif)
907 struct dhcp *dhcp = netif->dhcp;
912 ip4_addr_set_any(&dhcp->offered_ip_addr);
913 dhcp_set_state(dhcp, DHCP_STATE_SELECTING);
915 result = dhcp_create_msg(netif, dhcp, DHCP_DISCOVER);
919 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
920 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
922 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST,
sizeof(dhcp_discover_select_options));
923 for (i = 0; i <
sizeof(dhcp_discover_select_options); i++) {
924 dhcp_option_byte(dhcp, dhcp_discover_select_options[i]);
926 dhcp_option_trailer(dhcp);
929 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
932 udp_sendto_if_src(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif, IP_ADDR_ANY);
934 dhcp_delete_msg(dhcp);
939 if (dhcp->tries < 255) {
942 #if LWIP_DHCP_AUTOIP_COOP 944 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;
948 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
949 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
961 dhcp_bind(
struct netif *netif)
965 ip4_addr_t sn_mask, gw_addr;
972 dhcp->lease_used = 0;
974 if (dhcp->offered_t0_lease != 0xffffffffUL) {
977 timeout = (dhcp->offered_t0_lease + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
978 if (timeout > 0xffff) {
981 dhcp->t0_timeout = (
u16_t)timeout;
982 if (dhcp->t0_timeout == 0) {
983 dhcp->t0_timeout = 1;
989 if (dhcp->offered_t1_renew != 0xffffffffUL) {
992 timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
993 if (timeout > 0xffff) {
996 dhcp->t1_timeout = (
u16_t)timeout;
997 if (dhcp->t1_timeout == 0) {
998 dhcp->t1_timeout = 1;
1001 dhcp->t1_renew_time = dhcp->t1_timeout;
1004 if (dhcp->offered_t2_rebind != 0xffffffffUL) {
1006 timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1007 if (timeout > 0xffff) {
1010 dhcp->t2_timeout = (
u16_t)timeout;
1011 if (dhcp->t2_timeout == 0) {
1012 dhcp->t2_timeout = 1;
1015 dhcp->t2_rebind_time = dhcp->t2_timeout;
1019 if ((dhcp->t1_timeout >= dhcp->t2_timeout) && (dhcp->t2_timeout > 0)) {
1020 dhcp->t1_timeout = 0;
1023 if (dhcp->subnet_mask_given) {
1025 ip4_addr_copy(sn_mask, dhcp->offered_sn_mask);
1028 u8_t first_octet = ip4_addr1(&dhcp->offered_ip_addr);
1029 if (first_octet <= 127) {
1030 ip4_addr_set_u32(&sn_mask,
PP_HTONL(0xff000000UL));
1031 }
else if (first_octet >= 192) {
1032 ip4_addr_set_u32(&sn_mask,
PP_HTONL(0xffffff00UL));
1034 ip4_addr_set_u32(&sn_mask,
PP_HTONL(0xffff0000UL));
1038 ip4_addr_copy(gw_addr, dhcp->offered_gw_addr);
1040 if (ip4_addr_isany_val(gw_addr)) {
1042 ip4_addr_get_network(&gw_addr, &dhcp->offered_ip_addr, &sn_mask);
1044 ip4_addr_set_u32(&gw_addr, ip4_addr_get_u32(&gw_addr) |
PP_HTONL(0x00000001UL));
1047 #if LWIP_DHCP_AUTOIP_COOP 1048 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1050 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1055 ip4_addr_get_u32(&dhcp->offered_ip_addr), ip4_addr_get_u32(&sn_mask), ip4_addr_get_u32(&gw_addr)));
1056 netif_set_addr(netif, &dhcp->offered_ip_addr, &sn_mask, &gw_addr);
1060 dhcp_set_state(dhcp, DHCP_STATE_BOUND);
1069 dhcp_renew(
struct netif *netif)
1071 struct dhcp *dhcp = netif->dhcp;
1075 dhcp_set_state(dhcp, DHCP_STATE_RENEWING);
1078 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1080 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1081 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1083 #if LWIP_NETIF_HOSTNAME 1084 dhcp_option_hostname(dhcp, netif);
1088 dhcp_option_trailer(dhcp);
1090 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1092 udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
1093 dhcp_delete_msg(dhcp);
1099 if (dhcp->tries < 255) {
1103 msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
1104 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1115 dhcp_rebind(
struct netif *netif)
1117 struct dhcp *dhcp = netif->dhcp;
1121 dhcp_set_state(dhcp, DHCP_STATE_REBINDING);
1124 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1126 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1127 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1129 #if LWIP_NETIF_HOSTNAME 1130 dhcp_option_hostname(dhcp, netif);
1133 dhcp_option_trailer(dhcp);
1135 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1138 udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1139 dhcp_delete_msg(dhcp);
1144 if (dhcp->tries < 255) {
1147 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1148 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1159 dhcp_reboot(
struct netif *netif)
1161 struct dhcp *dhcp = netif->dhcp;
1165 dhcp_set_state(dhcp, DHCP_STATE_REBOOTING);
1168 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1170 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1171 dhcp_option_short(dhcp, 576);
1173 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
1174 dhcp_option_long(dhcp,
ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
1176 dhcp_option_trailer(dhcp);
1178 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1181 udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1182 dhcp_delete_msg(dhcp);
1187 if (dhcp->tries < 255) {
1190 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1191 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1203 dhcp_release(
struct netif *netif)
1205 struct dhcp *dhcp = netif->dhcp;
1208 u8_t is_dhcp_supplied_address;
1216 is_dhcp_supplied_address = dhcp_supplied_address(netif);
1219 dhcp_set_state(dhcp, DHCP_STATE_OFF);
1221 ip_addr_set_zero_ip4(&dhcp->server_ip_addr);
1222 ip4_addr_set_zero(&dhcp->offered_ip_addr);
1223 ip4_addr_set_zero(&dhcp->offered_sn_mask);
1224 ip4_addr_set_zero(&dhcp->offered_gw_addr);
1225 #if LWIP_DHCP_BOOTP_FILE 1226 ip4_addr_set_zero(&dhcp->offered_si_addr);
1228 dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
1229 dhcp->t1_renew_time = dhcp->t2_rebind_time = dhcp->lease_used = dhcp->t0_timeout = 0;
1231 if (!is_dhcp_supplied_address) {
1237 result = dhcp_create_msg(netif, dhcp, DHCP_RELEASE);
1239 dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
1240 dhcp_option_long(dhcp,
ntohl(ip4_addr_get_u32(ip_2_ip4(&server_ip_addr))));
1242 dhcp_option_trailer(dhcp);
1244 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1246 udp_sendto_if(dhcp->pcb, dhcp->p_out, &server_ip_addr, DHCP_SERVER_PORT, netif);
1247 dhcp_delete_msg(dhcp);
1254 netif_set_addr(netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY);
1265 dhcp_stop(
struct netif *netif)
1268 LWIP_ERROR(
"dhcp_stop: netif != NULL", (netif !=
NULL),
return;);
1274 #if LWIP_DHCP_AUTOIP_COOP 1275 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1277 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1281 if (dhcp->pcb !=
NULL) {
1282 udp_remove(dhcp->pcb);
1286 dhcp_set_state(dhcp, DHCP_STATE_OFF);
1296 dhcp_set_state(
struct dhcp *dhcp,
u8_t new_state)
1298 if (new_state != dhcp->state) {
1299 dhcp->state = new_state;
1301 dhcp->request_timeout = 0;
1311 dhcp_option(
struct dhcp *dhcp,
u8_t option_type,
u8_t option_len)
1313 LWIP_ASSERT(
"dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN);
1314 dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
1315 dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
1322 dhcp_option_byte(
struct dhcp *dhcp,
u8_t value)
1324 LWIP_ASSERT(
"dhcp_option_byte: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1325 dhcp->msg_out->options[dhcp->options_out_len++] = value;
1329 dhcp_option_short(
struct dhcp *dhcp,
u16_t value)
1331 LWIP_ASSERT(
"dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U <= DHCP_OPTIONS_LEN);
1332 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0xff00U) >> 8);
1333 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t) (value & 0x00ffU);
1337 dhcp_option_long(
struct dhcp *dhcp,
u32_t value)
1339 LWIP_ASSERT(
"dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4U <= DHCP_OPTIONS_LEN);
1340 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0xff000000UL) >> 24);
1341 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0x00ff0000UL) >> 16);
1342 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0x0000ff00UL) >> 8);
1343 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0x000000ffUL));
1346 #if LWIP_NETIF_HOSTNAME 1348 dhcp_option_hostname(
struct dhcp *dhcp,
struct netif *netif)
1350 if (netif->hostname !=
NULL) {
1351 size_t namelen = strlen(netif->hostname);
1354 const char *p = netif->hostname;
1357 size_t available = DHCP_OPTIONS_LEN - dhcp->options_out_len - 3;
1358 LWIP_ASSERT(
"DHCP: hostname is too long!", namelen <= available);
1359 len =
LWIP_MIN(namelen, available);
1360 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, len);
1362 dhcp_option_byte(dhcp, *p++);
1380 dhcp_parse_reply(
struct dhcp *dhcp,
struct pbuf *p)
1386 u16_t options_idx_max;
1388 int parse_file_as_options = 0;
1389 int parse_sname_as_options = 0;
1392 dhcp_clear_all_options(dhcp);
1394 if (p->
len < DHCP_SNAME_OFS) {
1397 dhcp->msg_in = (
struct dhcp_msg *)p->
payload;
1400 dhcp->boot_file_name[0] = 0;
1406 options_idx = DHCP_OPTIONS_OFS;
1411 while ((q !=
NULL) && (options_idx >= q->
len)) {
1412 options_idx -= q->
len;
1413 options_idx_max -= q->
len;
1419 offset = options_idx;
1420 offset_max = options_idx_max;
1423 while ((q !=
NULL) && (options[offset] != DHCP_OPTION_END) && (offset < offset_max)) {
1424 u8_t op = options[offset];
1426 u8_t decode_len = 0;
1427 int decode_idx = -1;
1428 u16_t val_offset = offset + 2;
1430 if (offset + 1 < q->
len) {
1431 len = options[offset + 1];
1439 case(DHCP_OPTION_PAD):
1441 decode_len = len = 0;
1445 case(DHCP_OPTION_SUBNET_MASK):
1447 decode_idx = DHCP_OPTION_IDX_SUBNET_MASK;
1449 case(DHCP_OPTION_ROUTER):
1452 decode_idx = DHCP_OPTION_IDX_ROUTER;
1454 case(DHCP_OPTION_DNS_SERVER):
1460 decode_idx = DHCP_OPTION_IDX_DNS_SERVER;
1462 case(DHCP_OPTION_LEASE_TIME):
1464 decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
1466 #if LWIP_DHCP_GET_NTP_SRV 1467 case(DHCP_OPTION_NTP):
1473 decode_idx = DHCP_OPTION_IDX_NTP_SERVER;
1476 case(DHCP_OPTION_OVERLOAD):
1478 decode_idx = DHCP_OPTION_IDX_OVERLOAD;
1480 case(DHCP_OPTION_MESSAGE_TYPE):
1482 decode_idx = DHCP_OPTION_IDX_MSG_TYPE;
1484 case(DHCP_OPTION_SERVER_ID):
1486 decode_idx = DHCP_OPTION_IDX_SERVER_ID;
1488 case(DHCP_OPTION_T1):
1490 decode_idx = DHCP_OPTION_IDX_T1;
1492 case(DHCP_OPTION_T2):
1494 decode_idx = DHCP_OPTION_IDX_T2;
1502 if (decode_len > 0) {
1506 LWIP_ASSERT(
"check decode_idx", decode_idx >= 0 && decode_idx < DHCP_OPTION_IDX_MAX);
1507 if (!dhcp_option_given(dhcp, decode_idx)) {
1508 copy_len =
LWIP_MIN(decode_len, 4);
1510 if (decode_len > 4) {
1513 dhcp_got_option(dhcp, decode_idx);
1514 dhcp_set_option_value(dhcp, decode_idx,
htonl(value));
1519 }
else if (decode_len == 4) {
1520 value =
ntohl(value);
1523 value = ((
u8_t*)&value)[0];
1525 dhcp_got_option(dhcp, decode_idx);
1526 dhcp_set_option_value(dhcp, decode_idx, value);
1529 if (offset >= q->
len) {
1531 offset_max -= q->
len;
1532 if ((offset < offset_max) && offset_max) {
1543 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_OVERLOAD)) {
1544 u32_t overload = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1545 dhcp_clear_option(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1546 if (overload == DHCP_OVERLOAD_FILE) {
1547 parse_file_as_options = 1;
1549 }
else if (overload == DHCP_OVERLOAD_SNAME) {
1550 parse_sname_as_options = 1;
1552 }
else if (overload == DHCP_OVERLOAD_SNAME_FILE) {
1553 parse_sname_as_options = 1;
1554 parse_file_as_options = 1;
1559 #if LWIP_DHCP_BOOTP_FILE 1560 if (!parse_file_as_options) {
1562 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) &&
1563 (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) == DHCP_ACK))
1567 dhcp->boot_file_name[DHCP_FILE_LEN-1] = 0;
1571 if (parse_file_as_options) {
1573 parse_file_as_options = 0;
1574 options_idx = DHCP_FILE_OFS;
1575 options_idx_max = DHCP_FILE_OFS + DHCP_FILE_LEN;
1577 }
else if (parse_sname_as_options) {
1578 parse_sname_as_options = 0;
1579 options_idx = DHCP_SNAME_OFS;
1580 options_idx_max = DHCP_SNAME_OFS + DHCP_SNAME_LEN;
1590 dhcp_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr,
u16_t port)
1592 struct netif *netif = (
struct netif *)arg;
1593 struct dhcp *dhcp = netif->dhcp;
1594 struct dhcp_msg *reply_msg = (
struct dhcp_msg *)p->
payload;
1603 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));
1613 if (p->
len < DHCP_MIN_REPLY_LEN) {
1615 goto free_pbuf_and_return;
1618 if (reply_msg->op != DHCP_BOOTREPLY) {
1620 goto free_pbuf_and_return;
1624 if (netif->
hwaddr[i] != reply_msg->chaddr[i]) {
1626 (
"netif->hwaddr[%"U16_F
"]==%02"X16_F" != reply_msg->chaddr[%"U16_F
"]==%02"X16_F"\n",
1628 goto free_pbuf_and_return;
1632 if (
ntohl(reply_msg->xid) != dhcp->xid) {
1634 (
"transaction id mismatch reply_msg->xid(%"X32_F
")!=dhcp->xid(%"X32_F
")\n",
ntohl(reply_msg->xid),dhcp->xid));
1635 goto free_pbuf_and_return;
1638 if (dhcp_parse_reply(dhcp, p) !=
ERR_OK) {
1640 (
"problem unfolding DHCP message - too short on memory?\n"));
1641 goto free_pbuf_and_return;
1646 if (!dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE)) {
1648 goto free_pbuf_and_return;
1652 msg_type = (
u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE);
1654 if (msg_type == DHCP_ACK) {
1657 if (dhcp->state == DHCP_STATE_REQUESTING) {
1658 dhcp_handle_ack(netif);
1659 #if DHCP_DOES_ARP_CHECK 1668 else if ((dhcp->state == DHCP_STATE_REBOOTING) || (dhcp->state == DHCP_STATE_REBINDING) ||
1669 (dhcp->state == DHCP_STATE_RENEWING)) {
1674 else if ((msg_type == DHCP_NAK) &&
1675 ((dhcp->state == DHCP_STATE_REBOOTING) || (dhcp->state == DHCP_STATE_REQUESTING) ||
1676 (dhcp->state == DHCP_STATE_REBINDING) || (dhcp->state == DHCP_STATE_RENEWING ))) {
1678 dhcp_handle_nak(netif);
1681 else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_STATE_SELECTING)) {
1683 dhcp->request_timeout = 0;
1685 dhcp_handle_offer(netif);
1687 free_pbuf_and_return:
1688 dhcp->msg_in =
NULL;
1700 dhcp_create_msg(
struct netif *netif,
struct dhcp *dhcp,
u8_t message_type)
1703 #ifndef DHCP_GLOBAL_XID 1708 #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND) 1711 static u32_t xid = 0xABCD0000;
1714 if (!xid_initialised) {
1715 xid = DHCP_GLOBAL_XID;
1716 xid_initialised = !xid_initialised;
1721 LWIP_ASSERT(
"dhcp_create_msg: dhcp->p_out == NULL", dhcp->p_out ==
NULL);
1722 LWIP_ASSERT(
"dhcp_create_msg: dhcp->msg_out == NULL", dhcp->msg_out ==
NULL);
1724 if (dhcp->p_out ==
NULL) {
1726 (
"dhcp_create_msg(): could not allocate pbuf\n"));
1729 LWIP_ASSERT(
"dhcp_create_msg: check that first pbuf can hold struct dhcp_msg",
1730 (dhcp->p_out->len >=
sizeof(
struct dhcp_msg)));
1733 if (message_type != DHCP_REQUEST) {
1735 if (dhcp->tries == 0) {
1736 #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND) 1745 (
"transaction id xid(%"X32_F
")\n", xid));
1747 dhcp->msg_out = (
struct dhcp_msg *)dhcp->p_out->payload;
1749 dhcp->msg_out->op = DHCP_BOOTREQUEST;
1751 dhcp->msg_out->htype = DHCP_HTYPE_ETH;
1753 dhcp->msg_out->hops = 0;
1754 dhcp->msg_out->xid =
htonl(dhcp->xid);
1755 dhcp->msg_out->secs = 0;
1758 dhcp->msg_out->flags = 0;
1759 ip4_addr_set_zero(&dhcp->msg_out->ciaddr);
1761 if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) || (message_type == DHCP_RELEASE) ||
1762 ((message_type == DHCP_REQUEST) &&
1763 ((dhcp->state== DHCP_STATE_RENEWING) || dhcp->state== DHCP_STATE_REBINDING))) {
1764 ip4_addr_copy(dhcp->msg_out->ciaddr, *netif_ip4_addr(netif));
1766 ip4_addr_set_zero(&dhcp->msg_out->yiaddr);
1767 ip4_addr_set_zero(&dhcp->msg_out->siaddr);
1768 ip4_addr_set_zero(&dhcp->msg_out->giaddr);
1769 for (i = 0; i < DHCP_CHADDR_LEN; i++) {
1773 for (i = 0; i < DHCP_SNAME_LEN; i++) {
1774 dhcp->msg_out->sname[i] = 0;
1776 for (i = 0; i < DHCP_FILE_LEN; i++) {
1777 dhcp->msg_out->file[i] = 0;
1779 dhcp->msg_out->cookie =
PP_HTONL(DHCP_MAGIC_COOKIE);
1780 dhcp->options_out_len = 0;
1782 for (i = 0; i < DHCP_OPTIONS_LEN; i++) {
1783 dhcp->msg_out->options[i] = (
u8_t)i;
1786 dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
1787 dhcp_option_byte(dhcp, message_type);
1797 dhcp_delete_msg(
struct dhcp *dhcp)
1799 LWIP_ERROR(
"dhcp_delete_msg: dhcp != NULL", (dhcp !=
NULL),
return;);
1800 LWIP_ASSERT(
"dhcp_delete_msg: dhcp->p_out != NULL", dhcp->p_out !=
NULL);
1801 LWIP_ASSERT(
"dhcp_delete_msg: dhcp->msg_out != NULL", dhcp->msg_out !=
NULL);
1802 if (dhcp->p_out !=
NULL) {
1806 dhcp->msg_out =
NULL;
1818 dhcp_option_trailer(
struct dhcp *dhcp)
1820 LWIP_ERROR(
"dhcp_option_trailer: dhcp != NULL", (dhcp !=
NULL),
return;);
1821 LWIP_ASSERT(
"dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out !=
NULL);
1822 LWIP_ASSERT(
"dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1823 dhcp->msg_out->options[dhcp->options_out_len++] = DHCP_OPTION_END;
1825 while (((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) &&
1826 (dhcp->options_out_len < DHCP_OPTIONS_LEN)) {
1828 dhcp->msg_out->options[dhcp->options_out_len++] = 0;
1839 dhcp_supplied_address(
struct netif *netif)
1841 if ((netif !=
NULL) && (netif->dhcp !=
NULL)) {
1842 if ((netif->dhcp->
state == DHCP_STATE_BOUND) ||
1843 (netif->dhcp->
state == DHCP_STATE_RENEWING)) {
#define LWIP_DHCP_AUTOIP_COOP_TRIES
#define LWIP_DHCP_MAX_NTP_SERVERS
struct netif * netif_list
#define LWIP_DBG_LEVEL_SERIOUS
void mem_free(void *rmem)
#define LWIP_DBG_LEVEL_WARNING
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
void pbuf_realloc(struct pbuf *p, u16_t new_len)
#define netif_is_link_up(netif)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
#define LWIP_DHCP_BOOTP_FILE
#define NETIF_MAX_HWADDR_LEN
u8_t pbuf_free(struct pbuf *p)
#define ip_addr_copy(dest, src)
#define NETIF_FLAG_ETHARP
#define LWIP_ASSERT(message, assertion)
u16_t pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
void * mem_malloc(mem_size_t size)
#define LWIP_ERROR(message, expression, handler)
#define netif_is_up(netif)
#define LWIP_DEBUGF(debug, message)
#define ip_set_option(pcb, opt)
#define LWIP_UNUSED_ARG(x)