aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/dhcpc.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/udhcp/dhcpc.c')
-rw-r--r--networking/udhcp/dhcpc.c41
1 files changed, 25 insertions, 16 deletions
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index ab669d2b5..5fb96c2d8 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -1122,6 +1122,8 @@ static void perform_release(uint32_t server_addr, uint32_t requested_ip)
1122 char buffer[sizeof("255.255.255.255")]; 1122 char buffer[sizeof("255.255.255.255")];
1123 struct in_addr temp_addr; 1123 struct in_addr temp_addr;
1124 1124
1125 change_listen_mode(LISTEN_NONE);
1126
1125 /* send release packet */ 1127 /* send release packet */
1126 if (client_data.state == BOUND 1128 if (client_data.state == BOUND
1127 || client_data.state == RENEWING 1129 || client_data.state == RENEWING
@@ -1143,8 +1145,6 @@ static void perform_release(uint32_t server_addr, uint32_t requested_ip)
1143 * of the states above. 1145 * of the states above.
1144 */ 1146 */
1145 udhcp_run_script(NULL, "deconfig"); 1147 udhcp_run_script(NULL, "deconfig");
1146
1147 change_listen_mode(LISTEN_NONE);
1148 client_data.state = RELEASED; 1148 client_data.state = RELEASED;
1149} 1149}
1150 1150
@@ -1343,6 +1343,10 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1343 clientid_mac_ptr += OPT_DATA + 1; /* skip option code, len, ethernet */ 1343 clientid_mac_ptr += OPT_DATA + 1; /* skip option code, len, ethernet */
1344 } 1344 }
1345 1345
1346 /* Not really necessary (we redo it on every iteration)
1347 * but allows early (before daemonization) detection
1348 * of bad interface name.
1349 */
1346 if (udhcp_read_interface(client_data.interface, 1350 if (udhcp_read_interface(client_data.interface,
1347 &client_data.ifindex, 1351 &client_data.ifindex,
1348 NULL, 1352 NULL,
@@ -1372,7 +1376,6 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1372 1376
1373 client_data.state = INIT_SELECTING; 1377 client_data.state = INIT_SELECTING;
1374 udhcp_run_script(NULL, "deconfig"); 1378 udhcp_run_script(NULL, "deconfig");
1375 change_listen_mode(LISTEN_RAW);
1376 packet_num = 0; 1379 packet_num = 0;
1377 timeout = 0; 1380 timeout = 0;
1378 lease_remaining = 0; 1381 lease_remaining = 0;
@@ -1446,8 +1449,10 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1446 switch (client_data.state) { 1449 switch (client_data.state) {
1447 case INIT_SELECTING: 1450 case INIT_SELECTING:
1448 if (!discover_retries || packet_num < discover_retries) { 1451 if (!discover_retries || packet_num < discover_retries) {
1449 if (packet_num == 0) 1452 if (packet_num == 0) {
1453 change_listen_mode(LISTEN_RAW);
1450 xid = random_xid(); 1454 xid = random_xid();
1455 }
1451 /* broadcast */ 1456 /* broadcast */
1452 send_discover(xid, requested_ip); 1457 send_discover(xid, requested_ip);
1453 timeout = discover_timeout; 1458 timeout = discover_timeout;
@@ -1455,6 +1460,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1455 continue; 1460 continue;
1456 } 1461 }
1457 leasefail: 1462 leasefail:
1463 change_listen_mode(LISTEN_NONE);
1458 udhcp_run_script(NULL, "leasefail"); 1464 udhcp_run_script(NULL, "leasefail");
1459#if BB_MMU /* -b is not supported on NOMMU */ 1465#if BB_MMU /* -b is not supported on NOMMU */
1460 if (opt & OPT_b) { /* background if no lease */ 1466 if (opt & OPT_b) { /* background if no lease */
@@ -1491,7 +1497,6 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1491 * "discover...select...discover..." loops 1497 * "discover...select...discover..." loops
1492 * were seen in the wild. Treat them similarly 1498 * were seen in the wild. Treat them similarly
1493 * to "no response to discover" case */ 1499 * to "no response to discover" case */
1494 change_listen_mode(LISTEN_RAW);
1495 client_data.state = INIT_SELECTING; 1500 client_data.state = INIT_SELECTING;
1496 goto leasefail; 1501 goto leasefail;
1497 case BOUND: 1502 case BOUND:
@@ -1528,17 +1533,17 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1528 } 1533 }
1529//TODO: if 3 renew's failed (no reply) but remaining lease is large enough, 1534//TODO: if 3 renew's failed (no reply) but remaining lease is large enough,
1530//it might make sense to go back to BOUND and try later? a-la 1535//it might make sense to go back to BOUND and try later? a-la
1531// if (lease_remaining > 30) goto BOUND_for_half_lease; 1536// if (lease_remaining > 30) change_listen_mode(LISTEN_NONE) + goto BOUND_for_half_lease;
1532//If we do that, "packet_num < 3" test below might be superfluous 1537//If we do that, "packet_num < 3" test below might be superfluous
1533//(lease_remaining will run out anyway) 1538//(lease_remaining will run out anyway)
1534 /* Timed out or error, enter rebinding state */ 1539 /* Timed out or error, enter rebinding state */
1535 log1s("entering rebinding state"); 1540 log1s("entering rebinding state");
1536 client_data.state = REBINDING; 1541 client_data.state = REBINDING;
1542 /* Switch to bcast receive */
1543 change_listen_mode(LISTEN_RAW);
1537 packet_num = 0; 1544 packet_num = 0;
1538 /* fall right through */ 1545 /* fall right through */
1539 case REBINDING: 1546 case REBINDING:
1540 /* Switch to bcast receive */
1541 change_listen_mode(LISTEN_RAW);
1542 /* Lease is *really* about to run out, 1547 /* Lease is *really* about to run out,
1543 * try to find DHCP server using broadcast */ 1548 * try to find DHCP server using broadcast */
1544 if (lease_remaining > 0 && packet_num < 3) { 1549 if (lease_remaining > 0 && packet_num < 3) {
@@ -1549,6 +1554,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1549 continue; 1554 continue;
1550 } 1555 }
1551 /* Timed out, enter init state */ 1556 /* Timed out, enter init state */
1557 change_listen_mode(LISTEN_NONE);
1552 bb_simple_info_msg("lease lost, entering init state"); 1558 bb_simple_info_msg("lease lost, entering init state");
1553 udhcp_run_script(NULL, "deconfig"); 1559 udhcp_run_script(NULL, "deconfig");
1554 client_data.state = INIT_SELECTING; 1560 client_data.state = INIT_SELECTING;
@@ -1584,12 +1590,15 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1584 1590
1585 /* Start things over */ 1591 /* Start things over */
1586 case RENEW_REQUESTED: /* two or more SIGUSR1 received */ 1592 case RENEW_REQUESTED: /* two or more SIGUSR1 received */
1593 change_listen_mode(LISTEN_NONE);
1587 udhcp_run_script(NULL, "deconfig"); 1594 udhcp_run_script(NULL, "deconfig");
1588 /* case REQUESTING: break; */ 1595
1589 /* case RELEASED: break; */ 1596 default:
1590 /* case INIT_SELECTING: break; */ 1597 /* case REQUESTING: */
1598 /* case RELEASED: */
1599 /* case INIT_SELECTING: */
1600 change_listen_mode(LISTEN_NONE);
1591 } 1601 }
1592 change_listen_mode(LISTEN_RAW);
1593 client_data.state = INIT_SELECTING; 1602 client_data.state = INIT_SELECTING;
1594 packet_num = 0; 1603 packet_num = 0;
1595 /* Kill any timeouts, user wants this to hurry along */ 1604 /* Kill any timeouts, user wants this to hurry along */
@@ -1597,6 +1606,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1597 continue; 1606 continue;
1598 case SIGUSR2: 1607 case SIGUSR2:
1599 perform_release(server_addr, requested_ip); 1608 perform_release(server_addr, requested_ip);
1609 /* ^^^ switches to LISTEN_NONE */
1600 timeout = INT_MAX; 1610 timeout = INT_MAX;
1601 continue; 1611 continue;
1602 case SIGTERM: 1612 case SIGTERM:
@@ -1706,6 +1716,8 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1706 char server_str[sizeof("255.255.255.255")]; 1716 char server_str[sizeof("255.255.255.255")];
1707 uint8_t *temp; 1717 uint8_t *temp;
1708 1718
1719 change_listen_mode(LISTEN_NONE);
1720
1709 temp_addr.s_addr = server_addr; 1721 temp_addr.s_addr = server_addr;
1710 strcpy(server_str, inet_ntoa(temp_addr)); 1722 strcpy(server_str, inet_ntoa(temp_addr));
1711 temp_addr.s_addr = packet.yiaddr; 1723 temp_addr.s_addr = packet.yiaddr;
@@ -1758,7 +1770,6 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1758 1770
1759 if (client_data.state != REQUESTING) 1771 if (client_data.state != REQUESTING)
1760 udhcp_run_script(NULL, "deconfig"); 1772 udhcp_run_script(NULL, "deconfig");
1761 change_listen_mode(LISTEN_RAW);
1762 client_data.state = INIT_SELECTING; 1773 client_data.state = INIT_SELECTING;
1763 client_data.first_secs = 0; /* make secs field count from 0 */ 1774 client_data.first_secs = 0; /* make secs field count from 0 */
1764 requested_ip = 0; 1775 requested_ip = 0;
@@ -1768,7 +1779,6 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1768 } 1779 }
1769 } 1780 }
1770#endif 1781#endif
1771
1772 /* enter bound state */ 1782 /* enter bound state */
1773 start = monotonic_sec(); 1783 start = monotonic_sec();
1774 udhcp_run_script(&packet, client_data.state == REQUESTING ? "bound" : "renew"); 1784 udhcp_run_script(&packet, client_data.state == REQUESTING ? "bound" : "renew");
@@ -1791,7 +1801,6 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1791// BOUND_for_half_lease: 1801// BOUND_for_half_lease:
1792 timeout = (unsigned)lease_remaining / 2; 1802 timeout = (unsigned)lease_remaining / 2;
1793 client_data.state = BOUND; 1803 client_data.state = BOUND;
1794 change_listen_mode(LISTEN_NONE);
1795 /* make future renew packets use different xid */ 1804 /* make future renew packets use different xid */
1796 /* xid = random_xid(); ...but why bother? */ 1805 /* xid = random_xid(); ...but why bother? */
1797 packet_num = 0; 1806 packet_num = 0;
@@ -1818,11 +1827,11 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1818 goto non_matching_svid; 1827 goto non_matching_svid;
1819 } 1828 }
1820 /* return to init state */ 1829 /* return to init state */
1830 change_listen_mode(LISTEN_NONE);
1821 bb_info_msg("received %s", "DHCP NAK"); 1831 bb_info_msg("received %s", "DHCP NAK");
1822 udhcp_run_script(&packet, "nak"); 1832 udhcp_run_script(&packet, "nak");
1823 if (client_data.state != REQUESTING) 1833 if (client_data.state != REQUESTING)
1824 udhcp_run_script(NULL, "deconfig"); 1834 udhcp_run_script(NULL, "deconfig");
1825 change_listen_mode(LISTEN_RAW);
1826 sleep(3); /* avoid excessive network traffic */ 1835 sleep(3); /* avoid excessive network traffic */
1827 client_data.state = INIT_SELECTING; 1836 client_data.state = INIT_SELECTING;
1828 client_data.first_secs = 0; /* make secs field count from 0 */ 1837 client_data.first_secs = 0; /* make secs field count from 0 */