The Anti-aircraft guns
At the beginning of the war the standard U-boat anti-aircraft (AA) weapon was a single 2cm gun. The Germans knew these as Flak from Flugabwehrkanone. The 2cm gun was most often the C/30 version in LC 30/37 mountings. From 1941 the 3,7cm gun SK C/30U became standard on type IX boats.
More power added to the AA defences
In 1939 and 1940 the weapon was located aft of the tower and it served its purpose up to a point. But it was quickly apparent that this was no an effective defence. In the following years the conning tower (Germ. Turmumbau) was rebuilt several times for better AA defence.
There was widening of the bridge to put on two double-barrelled 13,2mm Breda-Fla-MG and one traditional 2cm gun. That version was mainly used on the Mediterranean-boats from autumn 1942 to autumn 1943. Below you see U-81 with this layout.
Now two 2cm C/38 were fitted on bridge and on the additional platform which became known as the Wintergarten. The Turmumbau II was only an interim solution and was first installed in November 1942. The first boats which were rebuilt were the IXC40 boat U-193 and the VIIC boats U-237 and U-955.
Here we also have two 2cm C/38 but only on a widened bridge. The type VIID mine-layer boats had the mine-shafts aft of the tower they could not accommodarte the larger Wintergarden of Turmumbau II.
The VIIC (which was the most common U-boat type) was from 1944 on wards typically fitted with one 3.7cm automatic M42 U on gun-carriage LM 42 U and two double-barrelled 2cm 38 M II on double-LM 43 U.
Some boats had the 3.7cm gun removed and featured the quadruple 2cm 38/43 U with shield (see the photo of U-745 below).
U-338 (Oblt. Manfred Kinzel) was fitted with a small bandstand to the front of the tower for a twin 20mm MG151. The configuration was tested before U-338 went on operations. The MG151 guns had the advantage of being small and easy to fit in odd locations. At the same time however they proved ineffective (due to low muzzle velocity resulting in poor range and penetrating power) and were thus never widely adopted.
U-345 carried extended AA weaponry of four 20mm twins (1 mounting forward of the bridge and 3 in an extended bridge of Turm IV type) - the configuration was called Turm V.
U-673 (Kptlt. Gerhard Haelbich) and U-973 (Oblt. Klaus Paepenmoeller) had Turmumbau VI installed - standard Turm IV (one 37mm cannon and two 20mm twins) and an additional 37mm M42 cannon on LM42U mounting forward of the bridge (alternatively it was planned to use twin 20mm 38MII).
U-362 (Oblt. Ludwig Franz) is also known to have a forward gun platform. It is sometimes reported that U-362, U-673 and U-973 had a quadruple 20mm mounting in front. The actual setup is certainly quite unclear as there is a photo showing U-673 with the 37mm cannon on the forward platform in April 44 (referred to as Turm VI). Perhaps the boat was used for tests of various AA configurations. As the addition made the boats rather unstable, it quickly abandoned.
The various changes in AA weaponry on the U-boats resulted in the interesting variations of the conning tower. The most impressive example is of course the 4 U-Flak boats (U-441, U-256, U-621 and U-953).
Some Milk cow supply boats got an extra platform for the quadruple 2cm AA gun (see page for U-462 for photo)
On the type XXI there were 2 double-barrelled 3cm C/38 in two separate Flak-towers (earlier models had the 2cm model since the larger variant was not available in time), her smaller sister, the XXIII had no AA guns at all.
When one considers that during the war more than half of all the U-boats sunk at sea were sunk by aircraft it's no wonder the AA defence was a priority, not to mention the fact that the U-boats (until the Schnorchel and the Elektro boats came along) had to spend many hours on the surface recharging batteries and getting fresh air into the boat. Often the commander elected to fight it out and sometimes succeeded in shooting down the aircraft.
But even the example above was not enough of a defence against a determined attack from many aircraft and eventually the only real option the U-boat had was to dive immediately and hope for the best. From late 1944 when most combat boats had been fitted with the Schnorchel device they spent more and more time beneath the surface and the AA guns went mostly silent.
U-boat-successes against aircraft
Around 120 aircraft were shot down by U-boats for the loss of roughly 30 U-boats either sunk during the attack or due to being located by other forces shortly afterwards and sunk.
One source says that RAF Coastal Command (U-boat hunters) lost 700 aircraft (badly damaged, shot down and paid off) and sank 220 U-boats during the war. I've been unable to verify the RAF losses but the U-boat figure is about right it seems. These figures show the immense effort put out by the British to hunt down the U-boats and almost all the aircraft successes took place in 1942 and later. In 1939-1941 only some 2 U-boats were sunk by aircraft (31 in 1942 alone).