Just to get something cleared up: The public does not know how much Apple is getting from AT&T. Estimates are all over the map, with the $18/month used often accepted as fact since it came from Gene Munster, one of the more credible aapl analysts. But again, just one guy's guess. It may or may not be accurate, but I seriously doubt it's that simple--probably different amounts for new customers vs. existing, shifting amounts as different sales figures are reached, different for different plans, etc..
As far as the additional amount to be paid for unlocked iPhone (beyond that to Apple), the carrier was required to make up front expenditures to ready the network for the iPhone. Even advertising. Those costs need to be amortized/recovered over an expected number of iPhones to be sold/used on the network. No real profit until those costs recovered. The additional @$400 may sound extremely steep, but probably can be justified on paper, with a straight face, at this stage. That amount should decline as more are sold/activated, and costs are recovered.