I think it's a case of penalty vs. liquidated damages for breach of contract. Imposing a penalty for breaking a contract, generally, is not legal and usually unenforceable. However, if you provide some sort of formula for calculating damages in the event of a breach then you can argue it's actually a liquidated damages clause. Liquidated damages, provided they are reasonable and not punitive, are often upheld.
Lloyd Duhaime has some good (and accessible) information on contract law on his website (there's also a brief albeit concise discussion of the relevant principles from
the Alberta Court of Appeal here).
From what I read earlier, Sprint Nextel was charging customers $150-200 for breaking their contract earlier. There was no connection between that fee and actual revenue lost (i.e., damages) as a result of the breach. This is probably why many providers charge you a fee based on how many months you might have left on your contract.