diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-11 19:09:00 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-11 19:30:14 -0700 |
commit | 5ae9edcf5eaaeafb637dc581136d9bd5b9154186 (patch) | |
tree | 10fcb812ffdfa50ee04ab6f02319edf1c7000363 | |
parent | 1e36c3801c8f31b96db51f4c22eae20bbb9c821d (diff) |
libdhcp: Correctly call ifup and ifdown
-rw-r--r-- | libdhcp/dhcp_client.c | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/libdhcp/dhcp_client.c b/libdhcp/dhcp_client.c index 4e2aaaf..814c22f 100644 --- a/libdhcp/dhcp_client.c +++ b/libdhcp/dhcp_client.c @@ -619,7 +619,7 @@ static bool dhcp_check_conflict(implements_net_packet_conn *sock, struct net_ip4 return VCALL(sock, sendto, "CHECK_IP_CONFLICT", 17, addr, 5000) != NET_ETIMEDOUT; } -static void dhcp_client_take_lease(struct dhcp_client *client, struct dhcp_recv_msg *msg) { +static void dhcp_client_take_lease(struct dhcp_client *client, struct dhcp_recv_msg *msg, bool ifup) { assert(client); assert(msg); @@ -649,6 +649,14 @@ static void dhcp_client_take_lease(struct dhcp_client *client, struct dhcp_recv_ client->lease_time_ns_t1 = (dur_ns_t1 == DHCP_INFINITY * NS_PER_S) ? 0 : client->time_ns_init + dur_ns_t1; client->lease_time_ns_t2 = (dur_ns_t2 == DHCP_INFINITY * NS_PER_S) ? 0 : client->time_ns_init + dur_ns_t2; client->lease_time_ns_end = (dur_ns_end == DHCP_INFINITY * NS_PER_S) ? 0 : client->time_ns_init + dur_ns_end; + + if (ifup) + VCALL(client->iface, ifup, (struct net_iface_config){ + .addr = client->lease_client_addr, + .gateway_addr = client->lease_auxdata.gateway_addr, + .subnet_mask = client->lease_auxdata.subnet_mask, + }); +} } static __attribute__((noreturn)) void dhcp_client_run(struct dhcp_client *client) { @@ -675,7 +683,7 @@ static __attribute__((noreturn)) void dhcp_client_run(struct dhcp_client *client switch (msg.options[DHCP_OPT_DHCP_MSG_TYPE].dat[0]) { case DHCP_MSGTYP_OFFER: /* Accept the first offer. */ - dhcp_client_take_lease(client, &msg); + dhcp_client_take_lease(client, &msg, false); if (dhcp_client_send(client, DHCP_MSGTYP_REQUEST, NULL)) break; VCALL(client->sock, set_read_deadline, 0); @@ -708,12 +716,7 @@ static __attribute__((noreturn)) void dhcp_client_run(struct dhcp_client *client (void)dhcp_client_send(client, DHCP_MSGTYP_DECLINE, "IP is already in use"); client->state = STATE_INIT; } else { - dhcp_client_take_lease(client, &msg); - VCALL(client->iface, ifup, (struct net_iface_config){ - .addr = client->lease_client_addr, - .gateway_addr = client->lease_auxdata.gateway_addr, - .subnet_mask = client->lease_auxdata.subnet_mask, - }); + dhcp_client_take_lease(client, &msg, true); } default: /* ignore */ @@ -752,15 +755,11 @@ static __attribute__((noreturn)) void dhcp_client_run(struct dhcp_client *client case 0: switch (msg.options[DHCP_OPT_DHCP_MSG_TYPE].dat[0]) { case DHCP_MSGTYP_NAK: + VCALL(client->iface, ifdown); client->state = STATE_INIT; break; case DHCP_MSGTYP_ACK: - dhcp_client_take_lease(client, &msg); - VCALL(client->iface, ifup, (struct net_iface_config){ - .addr = client->lease_client_addr, - .gateway_addr = client->lease_auxdata.gateway_addr, - .subnet_mask = client->lease_auxdata.subnet_mask, - }); + dhcp_client_take_lease(client, &msg, true); default: /* ignore */ } @@ -784,20 +783,17 @@ static __attribute__((noreturn)) void dhcp_client_run(struct dhcp_client *client case 0: switch (msg.options[DHCP_OPT_DHCP_MSG_TYPE].dat[0]) { case DHCP_MSGTYP_NAK: + VCALL(client->iface, ifdown); client->state = STATE_INIT; break; case DHCP_MSGTYP_ACK: - dhcp_client_take_lease(client, &msg); - VCALL(client->iface, ifup, (struct net_iface_config){ - .addr = client->lease_client_addr, - .gateway_addr = client->lease_auxdata.gateway_addr, - .subnet_mask = client->lease_auxdata.subnet_mask, - }); + dhcp_client_take_lease(client, &msg, true); default: /* ignore */ } break; case -NET_ETIMEDOUT: + VCALL(client->iface, ifdown); client->state = STATE_INIT; break; default: |