Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Networking
User Name
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.


  Search this Thread
Old 03-20-2009, 06:47 AM   #1
LQ Newbie
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
Human readable IP adresses from netfilter

I am writing a simple netfilter kernel module and need to extract and display the IP addresses from incoming packets. In the headers, the address is defined to be __be32 which should be unsigned int. Is there a helper function which can allow me to convert these to a human readable dotted notation string and vice versa? Also, while we are on the subject, when you mangle a packet (change its destination port say) what is the correct way to regenerate the TCP or UDP checksum?

Last edited by dividedby0; 03-20-2009 at 07:04 AM.
Old 03-21-2009, 11:20 AM   #2
LQ Newbie
Registered: Mar 2009
Posts: 10

Rep: Reputation: 0

I recommend this tutorial, it contains the answers to both your questions (and lots more):

However, here is the solutions.

Output IP-address:
You use the macro NIPQUAD and feed it the saddr/daddr (for example NIPQUAD(iph->saddr). The matching part in your printk is ("%u.%u.%u.%u). (Found at

When changing the IP-header, you have to recalculate both IP and transport protocol checksum.
When changing the transport protocol header/payload, you only have to recalculate the transport protocol checksum.

Calculate IP-checksum:
Depending on if the value you changed was a __be16 or __be32, you use csum_replace2 and csum_replace4 respectively.

Calculating transport protocol checksum:
Depending on if the value you changed was a __be16 or __be32, you use inet_proto_csum_replace2 or inet_proto_csum_replace4 respectively

I am not sure what is the most effective way to recalculated the checksum if you have modified more then one field (these functions have to be called for every changed part).
Old 03-22-2009, 08:09 AM   #3
LQ Newbie
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks kristrev. Both your suggestions worked perfectly. I couldn't generate checksums for UDP packets though, but I just change them to 0(for now) since that is allowed.
Old 03-22-2009, 01:10 PM   #4
LQ Newbie
Registered: Mar 2009
Posts: 10

Rep: Reputation: 0
Glad to hear that it worked. To recalculated the UDP-checksum, you feed &udp->check to either inet...replace2 or inet...replace4. Remember that if you edit something in the IP-header, you have to recalculate the UDP checksum as well.


kernel, module, netfilter

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)? BrianK Programming 23 10-30-2010 05:20 AM
Converting a date in MySQL format to human readable in Perl resetreset Programming 7 02-11-2009 08:14 AM
Vista & Ext 3 HD Readable/Non-Readable HCour Linux - Newbie 1 01-19-2009 03:18 PM
Java Epoch time to Human Readable (using Date) true_atlantis Programming 1 05-09-2008 06:13 PM
Webalizer - howto convert the trafic into human readable? sys7em Linux - Server 3 09-25-2006 05:48 AM > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 02:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration