Configuration interaction in rare earth ions. II. Magnetic interactions