Both, and some other stuff. Traditionally, the justice system is supposed to have 5 recognized goals, they are: deterrence, incapacitation, rehabilitation, retribution, and restitution
deterrence is about preventing others from breaking the law (this one is not about the incarcerated individual, but about currently law-abiding citizens outside of prison)
incapacitation is about preventing harm to society by completely removing certain people from it
rehabilitation is reasonably obvious. That being said, there are traditionally considered some people who are "beyond rehabilitation". From this view, that's not ideal, but still not the end of the world, as prison in these cases has value for several of the other reasons described.
retribution is about making the victim (or victim's family) feel better by inflicting pain on the guilty. This is a combination of explicit revenge, and keeping buy-in from the local populace by making them feel like the system is just, and keeping them feeling secure/valued.
restitution is just about financial payments to make up what the victim has lost. If someone steals your car, having them buy you a new car, plus pay some amount extra for your time is generally seen as a reasonably fair solution. This one doesn't necessarily involve prison unless the defendant refuses to pay.