Re: Questions about Linux kernel network programming
From: Ben Hutchings <hidden>
Date: 2008-08-29 10:28:17
James King wrote:
On Thu, Aug 28, 2008 at 10:02 AM, Ben Hutchings [off-list ref] wrote:quoted
Thiago Lacerda wrote:quoted
BTW, anyone knows the right way of get a tcp port number in human readable form? I'm doing like this: struct tcphdr* tcp = tcp_hdr(my_sk_buff) unsigned short src_port = ntohs(tcp->source) And it isn't working, the numbers that I get are not right.Until the packet has gone through the network protocol handler (IP), the transport header pointer will not be set correctly and tcp_hdr() will return a pointer to the start of the packet.Thanks for the explanation Ben, I was having the same problem with a toy netfilter module hooked in prerouting a while ago and never bothered to find out why. Thiago, here's how I got it working: struct tcphdr *th = NULL, _tcph; struct iphdr *iph = ip_hdr(skb); if (iph && iph->protocol == IPPROTO_TCP) th = (struct tcphdr *)(skb->data + (iph->ihl * 4)); Alternatively, I think this will work too: th = skb_header_pointer(skb, (iph->ihl * 4), sizeof(_tcph), &_tcph);
What makes you think ihl is valid? Please read <http://www.cpni.gov.uk/Docs/InternetProtocol.pdf> before trying to parse untrusted packets. Ben. -- Ben Hutchings, Senior Software Engineer, Solarflare Communications Not speaking for my employer; that's the marketing department's job. They asked us to note that Solarflare product names are trademarked.