I had the same problem writing a small firewall for a networking class I just found out the problem I was having. I was casting the tcp header wrong. Try casting to tcp then accessing the port.
Here is a code snippet of it working
struct iphdr *ip_header; // ip header struct
struct tcphdr *tcp_header; // tcp header struct
struct udphdr *udp_header; // udp header struct
struct sk_buff *sock_buff;
unsigned int sport ,
dport;
sock_buff = skb;
if (!sock_buff)
return NF_ACCEPT;
ip_header = (struct iphdr *)skb_network_header(sock_buff);
if (!ip_header)
return NF_ACCEPT;
//if TCP PACKET
if(ip_header->protocol==IPPROTO_TCP)
{
//tcp_header = (struct tcphdr *)skb_transport_header(sock_buff); //doing the cast this way gave me the same problem
tcp_header= (struct tcphdr *)((__u32 *)ip_header+ ip_header->ihl); //this fixed the problem
sport = htons((unsigned short int) tcp_header->source); //sport now has the source port
dport = htons((unsigned short int) tcp_header->dest); //dport now has the dest port
}