Share your knowledge at the LQ Wiki.
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 09-02-2004, 03:41 AM   #1
LQ Newbie
Registered: Jan 2004
Posts: 6

Rep: Reputation: 0
modifying TCP packets using kernel module

[Any pointer would be appreciated...]

I am working on a kernel module that modifies the source port number of the TCP packets passing through the Netfilter hook. Currently, I am trying to print out the information about each TCP packet before I try to modify anything in the packets. My problem is that the port numbers the program captures are much different from what Ethereal (traffic monitor for those who are not familiar) shows. For instance, I constantly get 69 as the source port when I know for sure that it's something like 30573. After struggling for two whole days, I highly suspect that the use of a function called htons() is the key to the solution. According to the unix man page, htons does some conversion between different forms of addresses, but for some odd reason, it does not like being included in the module. My code, which compiles fine, shows the following as soon as I try to insert the module.

$>gcc -c modify.c
$>insmod modify.o
modify.o: unresolved symbol htons
Hint: You are trying to load a module without a GPL compatible license
and it has unresolved symbols. Contact the module supplier for
assistance, only they can help you.

My code follows.

#define __KERNEL__
#define MODULE

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/init.h> // Needed for the macros
#include <linux/tcp.h>
#include <linux/byteorder/generic.h>
//generic.h includes htons()

/* This is the structure we shall use to register our function */
static struct nf_hook_ops nfho;

/* This is the hook function itself */
unsigned int hook_func(unsigned int hooknum,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
// the trouble makers start
// two lines below are supposed to show the source and the destination port
printk(KERN_ALERT "source port: %u\n",htons((*skb)->>source));
printk(KERN_ALERT "destination port: %u\n",htons((*skb)->>dest));
// the trouble makers end

printk(KERN_ALERT "syn: %d\n",(*skb)->>syn);
printk(KERN_ALERT "ack: %d\n",(*skb)->>ack);
return NF_ACCEPT;

/* Initialisation routine */
int init_module()
/* Fill in our hook structure */
nfho.hook = hook_func; /* Handler function */
nfho.hooknum = NF_IP_PRE_ROUTING; /* First hook for IPv4 */ = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our function first */


return 0;

/* Cleanup routine */
void cleanup_module()

If htons() works, I believe the program will function properly. If someone knows what I am doing wrong, please let me know. Thanks a lot.
Old 09-02-2004, 04:26 AM   #2
Registered: Mar 2003
Location: Belgium
Distribution: Hardened gentoo
Posts: 323

Rep: Reputation: 30
Try to see if it helps by including <asm/byteorder.h> ?
Old 09-02-2004, 10:37 AM   #3
Senior Member
Registered: Jan 2004
Location: Montpellier (France)
Distribution: Gentoo
Posts: 1,014

Rep: Reputation: 36

or try
#include <netinet/in.h>


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
Info on TCP Packets CICA Linux - Networking 15 10-13-2005 03:58 PM
Modifying / replacing a kernel module dplazz Linux - General 1 07-22-2005 04:14 PM
encapsulating TCP packets in UDP packets... yoshi95 Programming 3 06-03-2004 03:53 PM
tcp packets wedgeworth Linux - Software 7 05-10-2004 05:40 PM
tcp/ip packets lackluster Programming 4 07-07-2002 06:57 PM > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 11:04 PM.

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