Look into DD-WRT, OpenWRT, and Tomato as the open source implementations that can be run on a variety of hardware. There are also hardware projects that are open source as well.
As to security, it depends how you define it, I guess. Routers are incredibly giant attack surfaces.