LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   modifying TCP packets using kernel module (https://www.linuxquestions.org/questions/linux-networking-3/modifying-tcp-packets-using-kernel-module-225572/)

shinkm 09-02-2004 02:41 AM

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
modify.o:
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)->h.th->source));
printk(KERN_ALERT "destination port: %u\n",htons((*skb)->h.th->dest));
// the trouble makers end

printk(KERN_ALERT "syn: %d\n",(*skb)->h.th->syn);
printk(KERN_ALERT "ack: %d\n",(*skb)->h.th->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 */
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our function first */

nf_register_hook(&nfho);

return 0;
}

/* Cleanup routine */
void cleanup_module()
{
nf_unregister_hook(&nfho);
}



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.

nukkel 09-02-2004 03:26 AM

Try to see if it helps by including <asm/byteorder.h> ?

Oliv' 09-02-2004 09:37 AM

Hi,

or try
Code:

#include <netinet/in.h>
Oliv'


All times are GMT -5. The time now is 05:46 PM.