Lines Matching +full:switch +full:- +full:delay +full:- +full:us
10 * SPDX-License-Identifier: Apache-2.0
103 if (types[j] == cb->option) { in dhcpv4_option_callback_get_unique_types()
113 cb->option); in dhcpv4_option_callback_get_unique_types()
116 types[count] = cb->option; in dhcpv4_option_callback_get_unique_types()
122 unique_types_in_callbacks = count - ARRAY_SIZE(min_req_options); in dhcpv4_option_callback_get_unique_types()
175 4, addr->s4_addr); in dhcpv4_add_server_id()
182 4, addr->s4_addr); in dhcpv4_add_req_ipaddr()
251 const size_t vendor_class_id_size = sizeof(vendor_class_id) - 1; in dhcpv4_create_message()
305 msg->op = DHCPV4_MSG_BOOT_REQUEST; in dhcpv4_create_message()
306 msg->htype = HARDWARE_ETHERNET_TYPE; in dhcpv4_create_message()
307 msg->hlen = net_if_get_link_addr(iface)->len; in dhcpv4_create_message()
308 msg->xid = htonl(iface->config.dhcpv4.xid); in dhcpv4_create_message()
309 msg->flags = IS_ENABLED(CONFIG_NET_DHCPV4_ACCEPT_UNICAST) ? in dhcpv4_create_message()
317 memcpy(msg->ciaddr, ciaddr, 4); in dhcpv4_create_message()
320 memcpy(msg->chaddr, net_if_get_link_addr(iface)->addr, in dhcpv4_create_message()
321 net_if_get_link_addr(iface)->len); in dhcpv4_create_message()
335 !dhcpv4_add_server_id(pkt, &iface->config.dhcpv4.server_id)) || in dhcpv4_create_message()
337 !dhcpv4_add_req_ipaddr(pkt, &iface->config.dhcpv4.requested_ip))) { in dhcpv4_create_message()
380 dhcpv4->timer_start = k_uptime_get() - 1; in dhcpv4_immediate_timeout()
381 dhcpv4->request_time = 0U; in dhcpv4_immediate_timeout()
389 NET_DBG("sched timeout dhcpv4=%p timeout=%us", dhcpv4, timeout); in dhcpv4_set_timeout()
390 dhcpv4->timer_start = k_uptime_get(); in dhcpv4_set_timeout()
391 dhcpv4->request_time = timeout; in dhcpv4_set_timeout()
408 NET_DBG("sched timeout dhcpv4=%p timeout=%us", dhcpv4, timeout); in dhcpv4_set_timeout_inc()
410 timeout_ms = (now - dhcpv4->timer_start) + MSEC_PER_SEC * timeout; in dhcpv4_set_timeout_inc()
411 dhcpv4->request_time = (uint32_t)(timeout_ms / MSEC_PER_SEC); in dhcpv4_set_timeout_inc()
420 * rounded-up whole seconds until the deadline. in dhcpv4_get_timeleft()
423 ret = (uint32_t)DIV_ROUND_UP(deadline - now, MSEC_PER_SEC); in dhcpv4_get_timeleft()
431 uint32_t request_time = iface->config.dhcpv4.request_time; in dhcpv4_request_timeleft()
433 return dhcpv4_get_timeleft(iface->config.dhcpv4.timer_start, in dhcpv4_request_timeleft()
439 return dhcpv4_get_timeleft(iface->config.dhcpv4.timer_start, in dhcpv4_renewal_timeleft()
440 iface->config.dhcpv4.renewal_time, in dhcpv4_renewal_timeleft()
446 return dhcpv4_get_timeleft(iface->config.dhcpv4.timer_start, in dhcpv4_rebinding_timeleft()
447 iface->config.dhcpv4.rebinding_time, in dhcpv4_rebinding_timeleft()
453 return dhcpv4_get_timeleft(iface->config.dhcpv4.timer_start, in dhcpv4_lease_timeleft()
454 iface->config.dhcpv4.lease_time, in dhcpv4_lease_timeleft()
463 timeout = DHCPV4_INITIAL_RETRY_TIMEOUT << dhcpv4->attempts; in dhcpv4_update_message_timeout()
470 /* +1/-1 second randomization */ in dhcpv4_update_message_timeout()
471 timeout += (sys_rand8_get() % 3U) - 1; in dhcpv4_update_message_timeout()
473 dhcpv4->attempts++; in dhcpv4_update_message_timeout()
485 * response to its DHCPREQUEST message, the client SHOULD wait one-half in dhcpv4_calculate_renew_rebind_timeout()
486 * of the remaining time until T2 (in RENEWING state) and one-half of in dhcpv4_calculate_renew_rebind_timeout()
504 struct net_if_dhcpv4 *dhcpv4 = &iface->config.dhcpv4; in dhcpv4_update_renew_timeout()
511 dhcpv4->attempts++; in dhcpv4_update_renew_timeout()
519 struct net_if_dhcpv4 *dhcpv4 = &iface->config.dhcpv4; in dhcpv4_update_rebind_timeout()
526 dhcpv4->attempts++; in dhcpv4_update_rebind_timeout()
534 * Returns the number of seconds until the next time-triggered event,
547 iface->config.dhcpv4.xid++; in dhcpv4_send_request()
549 switch (iface->config.dhcpv4.state) { in dhcpv4_send_request()
557 net_dhcpv4_state_name(iface->config.dhcpv4.state)); in dhcpv4_send_request()
563 memcpy(&iface->config.dhcpv4.request_server_addr, &iface->config.dhcpv4.server_id, in dhcpv4_send_request()
565 timeout = dhcpv4_update_message_timeout(&iface->config.dhcpv4); in dhcpv4_send_request()
570 ciaddr = &iface->config.dhcpv4.requested_ip; in dhcpv4_send_request()
574 server_addr = &iface->config.dhcpv4.server_id; in dhcpv4_send_request()
584 ciaddr = &iface->config.dhcpv4.requested_ip; in dhcpv4_send_request()
604 NET_DBG("send request dst=%s xid=0x%x ciaddr=%s%s%s timeout=%us", in dhcpv4_send_request()
606 iface->config.dhcpv4.xid, in dhcpv4_send_request()
609 with_server_id ? " +server-id" : "", in dhcpv4_send_request()
610 with_requested_ip ? " +requested-ip" : "", in dhcpv4_send_request()
629 iface->config.dhcpv4.xid++; in dhcpv4_send_discover()
644 timeout = dhcpv4_update_message_timeout(&iface->config.dhcpv4); in dhcpv4_send_discover()
646 NET_DBG("send discover xid=0x%x timeout=%us", in dhcpv4_send_discover()
647 iface->config.dhcpv4.xid, timeout); in dhcpv4_send_discover()
656 return iface->config.dhcpv4.xid % in dhcpv4_send_discover()
657 (CONFIG_NET_DHCPV4_INITIAL_DELAY_MAX - in dhcpv4_send_discover()
666 iface->config.dhcpv4.xid++; in dhcpv4_send_decline()
691 iface->config.dhcpv4.attempts = 0U; in dhcpv4_enter_selecting()
693 iface->config.dhcpv4.lease_time = 0U; in dhcpv4_enter_selecting()
694 iface->config.dhcpv4.renewal_time = 0U; in dhcpv4_enter_selecting()
695 iface->config.dhcpv4.rebinding_time = 0U; in dhcpv4_enter_selecting()
697 iface->config.dhcpv4.server_id.s_addr = INADDR_ANY; in dhcpv4_enter_selecting()
698 iface->config.dhcpv4.requested_ip.s_addr = INADDR_ANY; in dhcpv4_enter_selecting()
700 iface->config.dhcpv4.state = NET_DHCPV4_SELECTING; in dhcpv4_enter_selecting()
702 net_dhcpv4_state_name(iface->config.dhcpv4.state)); in dhcpv4_enter_selecting()
707 iface->config.dhcpv4.attempts = 0U; in dhcpv4_enter_requesting()
708 iface->config.dhcpv4.state = NET_DHCPV4_REQUESTING; in dhcpv4_enter_requesting()
710 net_dhcpv4_state_name(iface->config.dhcpv4.state)); in dhcpv4_enter_requesting()
712 memcpy(iface->config.dhcpv4.requested_ip.s4_addr, in dhcpv4_enter_requesting()
713 msg->yiaddr, sizeof(msg->yiaddr)); in dhcpv4_enter_requesting()
721 struct net_if_dhcpv4 *dhcpv4 = &iface->config.dhcpv4; in dhcpv4_enter_bound()
724 if (dhcpv4->renewal_time == 0U) { in dhcpv4_enter_bound()
726 dhcpv4->renewal_time = dhcpv4->lease_time / 2U; in dhcpv4_enter_bound()
729 if (dhcpv4->rebinding_time == 0U) { in dhcpv4_enter_bound()
731 dhcpv4->rebinding_time = dhcpv4->lease_time * 875U / 1000U; in dhcpv4_enter_bound()
738 if ((dhcpv4->renewal_time >= dhcpv4->rebinding_time) || in dhcpv4_enter_bound()
739 (dhcpv4->rebinding_time >= dhcpv4->lease_time)) { in dhcpv4_enter_bound()
743 dhcpv4->renewal_time = dhcpv4->lease_time / 2U; in dhcpv4_enter_bound()
744 dhcpv4->rebinding_time = dhcpv4->lease_time * 875U / 1000U; in dhcpv4_enter_bound()
747 dhcpv4->state = NET_DHCPV4_BOUND; in dhcpv4_enter_bound()
748 NET_DBG("enter state=%s renewal=%us rebinding=%us", in dhcpv4_enter_bound()
749 net_dhcpv4_state_name(dhcpv4->state), in dhcpv4_enter_bound()
750 dhcpv4->renewal_time, dhcpv4->rebinding_time); in dhcpv4_enter_bound()
752 dhcpv4_set_timeout(dhcpv4, dhcpv4->renewal_time); in dhcpv4_enter_bound()
755 &iface->config.dhcpv4, in dhcpv4_enter_bound()
756 sizeof(iface->config.dhcpv4)); in dhcpv4_enter_bound()
761 iface->config.dhcpv4.state = NET_DHCPV4_RENEWING; in dhcpv4_enter_renewing()
762 iface->config.dhcpv4.attempts = 0U; in dhcpv4_enter_renewing()
764 net_dhcpv4_state_name(iface->config.dhcpv4.state)); in dhcpv4_enter_renewing()
769 iface->config.dhcpv4.state = NET_DHCPV4_REBINDING; in dhcpv4_enter_rebinding()
770 iface->config.dhcpv4.attempts = 0U; in dhcpv4_enter_rebinding()
772 net_dhcpv4_state_name(iface->config.dhcpv4.state)); in dhcpv4_enter_rebinding()
780 &iface->config.dhcpv4, in dhcpv4_manage_timers()
781 net_dhcpv4_state_name(iface->config.dhcpv4.state), timeleft); in dhcpv4_manage_timers()
794 switch (iface->config.dhcpv4.state) { in dhcpv4_manage_timers()
810 if (iface->config.dhcpv4.attempts >= in dhcpv4_manage_timers()
838 &iface->config.dhcpv4.requested_ip)) { in dhcpv4_manage_timers()
878 NET_DBG("Waiting for %us", timeout_update); in dhcpv4_timeout()
896 NET_ERR("Vendor-specific option parsing, length too short"); in dhcpv4_parse_option_vendor()
898 return -EBADMSG; in dhcpv4_parse_option_vendor()
903 NET_DBG("Vendor-specific options_end"); in dhcpv4_parse_option_vendor()
906 length--; in dhcpv4_parse_option_vendor()
909 NET_ERR("Vendor-specific option parsing, malformed option"); in dhcpv4_parse_option_vendor()
910 return -EBADMSG; in dhcpv4_parse_option_vendor()
914 NET_ERR("Vendor-specific option parsing, bad length"); in dhcpv4_parse_option_vendor()
915 return -ENOBUFS; in dhcpv4_parse_option_vendor()
917 length--; in dhcpv4_parse_option_vendor()
919 NET_ERR("Vendor-specific option parsing, length too long"); in dhcpv4_parse_option_vendor()
921 return -EBADMSG; in dhcpv4_parse_option_vendor()
926 if (cb->option == type) { in dhcpv4_parse_option_vendor()
927 NET_ASSERT(cb->handler, "No callback handler!"); in dhcpv4_parse_option_vendor()
929 if (net_pkt_read(pkt, cb->data, MIN(cb->max_length, len))) { in dhcpv4_parse_option_vendor()
931 return -ENOBUFS; in dhcpv4_parse_option_vendor()
934 cb->handler(cb, len, *msg_type, iface); in dhcpv4_parse_option_vendor()
939 length = length - len; in dhcpv4_parse_option_vendor()
941 NET_DBG("Vendor-specific options_end (no code 255)"); in dhcpv4_parse_option_vendor()
945 return -ENOBUFS; in dhcpv4_parse_option_vendor()
993 if (cb->option == type) { in dhcpv4_parse_options()
994 NET_ASSERT(cb->handler, "No callback handler!"); in dhcpv4_parse_options()
996 if (net_pkt_read(pkt, cb->data, in dhcpv4_parse_options()
997 MIN(cb->max_length, length))) { in dhcpv4_parse_options()
1002 cb->handler(cb, length, *msg_type, iface); in dhcpv4_parse_options()
1009 switch (type) { in dhcpv4_parse_options()
1023 iface->config.dhcpv4.netmask = netmask; in dhcpv4_parse_options()
1044 net_pkt_skip(pkt, length - 4U)) { in dhcpv4_parse_options()
1101 if (net_pkt_skip(pkt, length - dns_servers_cnt * addr_size)) { in dhcpv4_parse_options()
1136 net_pkt_skip(pkt, length - 4U) < 0) { in dhcpv4_parse_options()
1167 if (net_pkt_read(pkt, iface->config.dhcpv4.ntp_addr.s4_addr, 4) < 0 || in dhcpv4_parse_options()
1168 net_pkt_skip(pkt, length - 4U) < 0) { in dhcpv4_parse_options()
1174 net_sprint_ipv4_addr(&iface->config.dhcpv4.ntp_addr)); in dhcpv4_parse_options()
1184 -ENOBUFS) { in dhcpv4_parse_options()
1204 pkt, &iface->config.dhcpv4.lease_time) || in dhcpv4_parse_options()
1205 !iface->config.dhcpv4.lease_time) { in dhcpv4_parse_options()
1211 iface->config.dhcpv4.lease_time); in dhcpv4_parse_options()
1221 pkt, &iface->config.dhcpv4.renewal_time) || in dhcpv4_parse_options()
1222 !iface->config.dhcpv4.renewal_time) { in dhcpv4_parse_options()
1228 iface->config.dhcpv4.renewal_time); in dhcpv4_parse_options()
1241 &iface->config.dhcpv4.rebinding_time) || in dhcpv4_parse_options()
1242 !iface->config.dhcpv4.rebinding_time) { in dhcpv4_parse_options()
1248 iface->config.dhcpv4.rebinding_time); in dhcpv4_parse_options()
1261 iface->config.dhcpv4.server_id.s4_addr, in dhcpv4_parse_options()
1268 net_sprint_ipv4_addr(&iface->config.dhcpv4.server_id)); in dhcpv4_parse_options()
1314 enum net_dhcpv4_state state = iface->config.dhcpv4.state; in dhcpv4_parse_options()
1321 iface->config.dhcpv4.renewal_time = 0U; in dhcpv4_parse_options()
1325 iface->config.dhcpv4.rebinding_time = 0U; in dhcpv4_parse_options()
1336 switch (iface->config.dhcpv4.state) { in dhcpv4_handle_msg_offer()
1354 switch (iface->config.dhcpv4.state) { in dhcpv4_handle_msg_ack()
1363 net_sprint_ipv4_addr(&iface->config.dhcpv4.requested_ip)); in dhcpv4_handle_msg_ack()
1366 &iface->config.dhcpv4.requested_ip, in dhcpv4_handle_msg_ack()
1368 iface->config.dhcpv4.lease_time)) { in dhcpv4_handle_msg_ack()
1374 &iface->config.dhcpv4.requested_ip, in dhcpv4_handle_msg_ack()
1375 &iface->config.dhcpv4.netmask); in dhcpv4_handle_msg_ack()
1392 switch (iface->config.dhcpv4.state) { in dhcpv4_handle_msg_nak()
1397 if (memcmp(&iface->config.dhcpv4.request_server_addr, in dhcpv4_handle_msg_nak()
1398 &iface->config.dhcpv4.response_src_addr, in dhcpv4_handle_msg_nak()
1399 sizeof(iface->config.dhcpv4.request_server_addr)) == 0) { in dhcpv4_handle_msg_nak()
1401 net_sprint_ipv4_addr(&iface->config.dhcpv4.request_server_addr)); in dhcpv4_handle_msg_nak()
1404 LOG_DBG("NAK from non-requesting server %s, ignore it", in dhcpv4_handle_msg_nak()
1405 net_sprint_ipv4_addr(&iface->config.dhcpv4.response_src_addr)); in dhcpv4_handle_msg_nak()
1414 &iface->config.dhcpv4.requested_ip)) { in dhcpv4_handle_msg_nak()
1430 net_dhcpv4_state_name(iface->config.dhcpv4.state), in dhcpv4_handle_reply()
1433 switch (msg_type) { in dhcpv4_handle_reply()
1497 msg->op, msg->htype, msg->hlen, ntohl(msg->xid), in net_dhcpv4_input()
1498 msg->secs, msg->flags, in net_dhcpv4_input()
1499 net_sprint_ll_addr(msg->chaddr, 6)); in net_dhcpv4_input()
1501 msg->ciaddr[0], msg->ciaddr[1], msg->ciaddr[2], msg->ciaddr[3]); in net_dhcpv4_input()
1503 msg->yiaddr[0], msg->yiaddr[1], msg->yiaddr[2], msg->yiaddr[3]); in net_dhcpv4_input()
1505 msg->siaddr[0], msg->siaddr[1], msg->siaddr[2], msg->siaddr[3]); in net_dhcpv4_input()
1507 msg->giaddr[0], msg->giaddr[1], msg->giaddr[2], msg->giaddr[3]); in net_dhcpv4_input()
1511 if (!(msg->op == DHCPV4_MSG_BOOT_REPLY && in net_dhcpv4_input()
1512 iface->config.dhcpv4.xid == ntohl(msg->xid) && in net_dhcpv4_input()
1513 !memcmp(msg->chaddr, net_if_get_link_addr(iface)->addr, in net_dhcpv4_input()
1514 net_if_get_link_addr(iface)->len))) { in net_dhcpv4_input()
1517 msg->op, iface->config.dhcpv4.xid, ntohl(msg->xid)); in net_dhcpv4_input()
1521 if (msg->hlen != net_if_get_link_addr(iface)->len) { in net_dhcpv4_input()
1522 NET_DBG("Unexpected hlen (%d)", msg->hlen); in net_dhcpv4_input()
1538 memcpy(&iface->config.dhcpv4.response_src_addr, ip_hdr->ipv4->src, in net_dhcpv4_input()
1561 if (node == &iface->config.dhcpv4.node) { in dhcpv4_iface_event_handler()
1573 if (iface->config.dhcpv4.state == NET_DHCPV4_BOUND) { in dhcpv4_iface_event_handler()
1574 iface->config.dhcpv4.attempts = 0U; in dhcpv4_iface_event_handler()
1575 iface->config.dhcpv4.state = NET_DHCPV4_INIT; in dhcpv4_iface_event_handler()
1577 iface->config.dhcpv4.state)); in dhcpv4_iface_event_handler()
1579 if (!net_if_ipv4_addr_rm(iface, &iface->config.dhcpv4.requested_ip)) { in dhcpv4_iface_event_handler()
1591 dhcpv4_immediate_timeout(&iface->config.dhcpv4); in dhcpv4_iface_event_handler()
1609 if (node == &iface->config.dhcpv4.node) { in dhcpv4_acd_event_handler()
1623 if (cb->info_length != sizeof(struct in_addr)) { in dhcpv4_acd_event_handler()
1627 addr = (struct in_addr *)cb->info; in dhcpv4_acd_event_handler()
1629 if (!net_ipv4_addr_cmp(&iface->config.dhcpv4.requested_ip, addr)) { in dhcpv4_acd_event_handler()
1635 (void)net_if_ipv4_addr_rm(iface, &iface->config.dhcpv4.requested_ip); in dhcpv4_acd_event_handler()
1641 iface->config.dhcpv4.state = NET_DHCPV4_DECLINE; in dhcpv4_acd_event_handler()
1642 dhcpv4_immediate_timeout(&iface->config.dhcpv4); in dhcpv4_acd_event_handler()
1680 return name[msg_type - 1]; in net_dhcpv4_msg_type_name()
1692 switch (iface->config.dhcpv4.state) { in dhcpv4_start_internal()
1694 iface->config.dhcpv4.state = NET_DHCPV4_INIT; in dhcpv4_start_internal()
1696 net_dhcpv4_state_name(iface->config.dhcpv4.state)); in dhcpv4_start_internal()
1698 /* We need entropy for both an XID and a random delay in dhcpv4_start_internal()
1708 iface->config.dhcpv4.xid = entropy; in dhcpv4_start_internal()
1716 timeout = entropy % (CONFIG_NET_DHCPV4_INITIAL_DELAY_MAX - in dhcpv4_start_internal()
1720 NET_DBG("wait timeout=%us", timeout); in dhcpv4_start_internal()
1730 &iface->config.dhcpv4.node); in dhcpv4_start_internal()
1732 dhcpv4_set_timeout(&iface->config.dhcpv4, timeout); in dhcpv4_start_internal()
1752 if (cb == NULL || cb->handler == NULL) { in net_dhcpv4_add_option_callback()
1753 return -EINVAL; in net_dhcpv4_add_option_callback()
1757 sys_slist_prepend(&option_callbacks, &cb->node); in net_dhcpv4_add_option_callback()
1767 if (cb == NULL || cb->handler == NULL) { in net_dhcpv4_remove_option_callback()
1768 return -EINVAL; in net_dhcpv4_remove_option_callback()
1772 if (!sys_slist_find_and_remove(&option_callbacks, &cb->node)) { in net_dhcpv4_remove_option_callback()
1773 ret = -EINVAL; in net_dhcpv4_remove_option_callback()
1786 if (cb == NULL || cb->handler == NULL) { in net_dhcpv4_add_option_vendor_callback()
1787 return -EINVAL; in net_dhcpv4_add_option_vendor_callback()
1791 sys_slist_prepend(&option_vendor_callbacks, &cb->node); in net_dhcpv4_add_option_vendor_callback()
1800 if (cb == NULL || cb->handler == NULL) { in net_dhcpv4_remove_option_vendor_callback()
1801 return -EINVAL; in net_dhcpv4_remove_option_vendor_callback()
1805 if (!sys_slist_find_and_remove(&option_vendor_callbacks, &cb->node)) { in net_dhcpv4_remove_option_vendor_callback()
1806 ret = -EINVAL; in net_dhcpv4_remove_option_vendor_callback()
1823 switch (iface->config.dhcpv4.state) { in net_dhcpv4_stop()
1830 &iface->config.dhcpv4.requested_ip)) { in net_dhcpv4_stop()
1840 iface->config.dhcpv4.state = NET_DHCPV4_DISABLED; in net_dhcpv4_stop()
1842 net_dhcpv4_state_name(iface->config.dhcpv4.state)); in net_dhcpv4_stop()
1845 &iface->config.dhcpv4.node); in net_dhcpv4_stop()
1879 net_ipaddr_copy(&net_sin(&local_addr)->sin_addr, in net_dhcpv4_init()
1927 if (iface->config.dhcpv4.state != NET_DHCPV4_SELECTING && in net_dhcpv4_accept_unicast()
1928 iface->config.dhcpv4.state != NET_DHCPV4_REQUESTING && in net_dhcpv4_accept_unicast()
1929 iface->config.dhcpv4.state != NET_DHCPV4_RENEWING && in net_dhcpv4_accept_unicast()
1930 iface->config.dhcpv4.state != NET_DHCPV4_REBINDING) { in net_dhcpv4_accept_unicast()
1943 if (udp_hdr->dst_port != htons(DHCPV4_CLIENT_PORT)) { in net_dhcpv4_accept_unicast()