LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

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

Rep: Reputation: 0
Human readable IP adresses from netfilter


Hi,
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
kristrev
LQ Newbie
 
Registered: Mar 2009
Posts: 10

Rep: Reputation: 0
Hi,

I recommend this tutorial, it contains the answers to both your questions (and lots more): http://jengelh.medozas.de/documents/...er_Modules.pdf

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 http://new.linuxfocus.org/English/Fe...ticle367.shtml)

Checksumming:
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
dividedby0
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
kristrev
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.
 
  


Reply

Tags
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


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

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