  We derive conditions under which f(G) gravity models, whose Lagrangian
densities f are written in terms of a Gauss-Bonnet term G, are cosmologically
viable. The most crucial condition to be satisfied is that f_GG, the second
derivative of f with respect to G, must be positive, which is required to
ensure the stability of a late-time de-Sitter solution as well as the existence
of standard radiation/matter dominated epochs. We present a number of explicit
f(G) models in which a cosmic acceleration is followed by the matter era. We
find that the equation of state of dark energy can cross the phantom divide
before reaching the present Universe. The viable models have asymptotic
behavior f_GG goes to +0 when |G| goes to infinity, in which case a rapid
oscillation of perturbations occurs unless such an oscillating degree of
freedom is suppressed relative to a homogeneous mode in the early universe. We
also introduce an iterative method to avoid numerical instabilities associated
with a large mass of the oscillating mode.
