Depending on the crime it's neither of those things. First and foremost it's simply segregation from the rest of society. If you go around killing people, then segregated stops you from continuing to do so (in theory).
As for smaller crimes which carry time, then that begs the question of whether its for punishment or rehabilitation. Perhaps it's both, being stripped of your freedom to participate in society for years is a form of punishment. There is likely an outdated precedent that if you have those freedoms taken away for considerable time, people will not look to break the law again.
The problem is that there are systems in society which people are brought into which perpetuate crime.
As for smaller crimes which carry time, then that begs the question of whether its for punishment or rehabilitation. Perhaps it's both, being stripped of your freedom to participate in society for years is a form of punishment. There is likely an outdated precedent that if you have those freedoms taken away for considerable time, people will not look to break the law again.
The problem is that there are systems in society which people are brought into which perpetuate crime.