The 'part of the hub the cassette mounts to' is the freehub body. That is evidently loose, or damaged.
5-8mm is a lot of movement. Don't ride the bike until you have fixed the problem. You would likely damage the freehub mechanism, and may suffer a jammed chain which could be dangerous.
Each manufacturers freehub disassembles in a different way. This Park Tool article will give you and idea of the types. First step is to get your cassette off and find out what type of hub you have, then find specific instructions for disassembling and inspecting it.