  The X-ray light curves of the early afterglow phase from gamma-ray bursts
present a puzzling variability, including flares. The origin of these flares is
still debated, and often associated with a late activity of the central engine.
We discuss an alternative scenario where the central engine remains short-lived
and flares are produced by the propagation of a long-lived reverse shock in a
stratified ejecta. Here we focus on the hydrodynamics of the shock
interactions. We perform one-dimensional ultrarelativistic hydrodynamic
simulations with different initial internal structure in the gamma-ray burst
ejecta. We use them to extract bolometric light curves and compare with a
previous study based on a simplified ballistic model. We find a good agreement
between both approaches, with similar slopes and variability in the light
curves, but identify several weaknesses in the ballistic model: the density is
underestimated in the shocked regions, and more importantly, late shock
reflections are not captured. With accurate dynamics provided by our
hydrodynamic simulations, we confirm that internal shocks in the ejecta lead to
the formation of dense shells. The interaction of the long-lived reverse shock
with a dense shell then produces a fast and intense increase of the dissipated
power. Assuming that the emission is due to the synchrotron radiation from
shock-accelerated electrons, and that the external forward shock is radiatively
inefficient, we find that this results in a bright flare in the X-ray
lightcurve, with arrival times, shapes, and duration in agreement with the
observed properties of X-ray flares in GRB afterglows.
