On a Fermat principle in general relativity. A Ljusternik--Schnirelmann theory for light rays