LinuxQuestions.org
Have you heard the LinuxQuestions.org Podcast?
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Thread Tools
Old 10-08-2009, 04:17 AM   #1
laginagesh
LQ Newbie
 
Registered: Jul 2009
Posts: 19
Thanked: 1
how to read sk_buff


[Log in to get rid of this advertisement]
hi,

I have written a module to read sk_buff data length.

-------------------------------------------------------------------------

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/cdev.h>
#include<linux/skbuff.h>

MODULE_LICENSE("Dual BSD/GPL");

struct sk_buff *skb;

int receive_init(void)
{
printk("Module initialized");
printk("Data length = %d\n",skb->data_len);

return 0;
}

void receive_exit(void)
{
printk("Module exited\n");
}

module_init(receive_init);
module_exit(receive_exit);

-------------------------------------------------------------------------
insert module giving Segmentation error.

Dmesg:
-------------------------------------------------------------------------
<1>Unable to handle kernel NULL pointer dereference at virtual address 00000064
printing eip:
ca99f005
*pde = 00000000
Oops: 0000 [#3]
Modules linked in: test12(U) 1(U) test(U) nfsd exportfs lockd autofs4 i2c_dev i2c_core sunrpc iptable_filter ip_tables dm_mod button battery ac md5 ipv6 joydev uhci_hcd snd_intel8x0 snd_ac97_codec snd_pcm_oss snd_mixer_oss snd_pcm snd_timer snd_page_alloc snd_mpu401_uart snd_rawmidi snd_seq_device snd soundcore 8139too mii floppy ext3 jbd
CPU: 0
EIP: 0060:[<ca99f005>] Not tainted VLI
EFLAGS: 00010286 (2.6.9-5.EL)
EIP is at receive_init+0x5/0x2a [test12]
eax: 00000000 ebx: c034f200 ecx: c034f200 edx: 00000000
esi: ca99f480 edi: c034f1c0 ebp: c034f1c0 esp: c4933fac
ds: 007b es: 007b ss: 0068
Process insmod (pid: 4714, threadinfo=c4933000 task=c5fecdd0)
Stack: c013b331 09455018 00000000 00000000 c4933000 c0301bfb 09455018 00013e1e
09455008 00000000 00000000 bfea1738 00000080 0000007b 0000007b 00000080
00b5708e 00000073 00000206 bfea16e0 0000007b
Call Trace:
[<c013b331>] sys_init_module+0x1f1/0x2d9
[<c0301bfb>] syscall_call+0x7/0xb
Code: Bad EIP value.
-------------------------------------------------------------------------

cheers
Nagesh
linux laginagesh is offline     Reply With Quote
Old 10-29-2009, 09:30 AM   #2
SethsdadtheLinuxer
Member
 
Registered: Jun 2007
Posts: 66
Thanked: 4
it is good that you wrote a module. Is the point of the post to say that it does not work?
windows_xp_2003 SethsdadtheLinuxer is offline     Reply With Quote
Old 10-30-2009, 12:21 AM   #3
laginagesh
LQ Newbie
 
Registered: Jul 2009
Posts: 19
Thanked: 1

Original Poster
yup,unable to handle sk_buff pointer ...(insert module giving segmentation fault)
in our project we are reading sk_buff pointer from /net/ipV4/ip_input.c, here we are calling a function passing sk_buff pointer,rebuild the kernel and boot working perfectly.

Now my intention to read sk_buff directly(not from /net/ipv4/ip_input.c), but unable to read...How to handle sk_buff pointer directly?

cheers
Nagesh
windows_xp_2003 laginagesh is offline     Reply With Quote
Old 11-03-2009, 12:31 PM   #4
bluehaze92
LQ Newbie
 
Registered: Nov 2009
Location: Texas, USA
Posts: 2
Thanked: 0
You 'skb' variable has not been initialized to anything. This is causing a NULL reference error. Before you can use 'skb' you have to somehow initialize it to something.

Hope this helps.
windows_xp_2003 bluehaze92 is offline     Reply With Quote
Old 11-06-2009, 04:32 PM   #5
yaplej
Member
 
Registered: Apr 2009
Posts: 69
Thanked: 0
I spent a long time trying to figure this out also. You have to write a packet handler, and register it with dev_add_pack() in the module initialization function. Here is a simplified example of what I did.

Also when the packet handler is executed its run in "interupt context" so you cannot use any functions that may sleep. This includes all forms of locking except spinlocks, and you also cannot use vmalloc() you can use kmalloc(), kcalloc(), kzalloc() to assign memory, but you must do it atomicly else they might sleep, and cause kernel panic.

All of that learned the hard way.

Code:

/*
 * This is the function that handles the packets. 
 */
static __s32 
packetprocessor_func(struct sk_buff *skb, struct net_device *dev, 
struct packet_type *pt, struct net_device *orig_dev){

	//Do stuff here.

	kfree_skb(skb);
	return 0;

}


static struct packet_type pt =
{ // Specify the function to handle packets.
	.type = __constant_htons(ETH_P_ALL),
	.func = packetprocessor_func,
};

static __s32 __init 
packetprocessor_init(void){
	printk(KERN_ALERT "Loading Packet Processor...\n");
	dev_add_pack(&pt); // Register the packet handler.
	return 0;
}

static void __exit 
packetprocessor_exit(void){
	printk(KERN_ALERT "Unloading Packet Processor...\n");
	dev_remove_pack(&pt);

}

module_init(packetprocessor_init);
module_exit(packetprocessor_exit);

Last edited by yaplej; 11-06-2009 at 04:35 PM..
windows_98_nt_2000 yaplej is offline     Reply With Quote
Old 11-12-2009, 12:41 AM   #6
yaplej
Member
 
Registered: Apr 2009
Posts: 69
Thanked: 0
If you just want to access the IP packet and TCP data I just found that the netfilter hooks are much better location to tie into with your module. Its very similar to the packet handler hook too so porting code is pretty quick.

Using the netfilter hooks prevents the posibility of you receiving a packet with bad checksum or any other "junk" sk_buffs. It all gets filtered out at lower layers.

Code:


static __s32 
packetprocessor_func(unsigned int hooknum, 
struct sk_buff **skb, 
const struct net_device *in, 
const struct net_device *out, 
int (*okfn)(struct sk_buff *)){

// Do stuff.

return NF_ACCEPT;
}

static struct nf_hook_ops mynfhook = { // This is a netfilter hook.
	.hook = packetprocessor_func, // Function that executes.
	.hooknum = NF_IP_FORWARD, // For routed traffic only.
	.pf = PF_INET, // Only for IP packets.
	.priority = NF_IP_PRI_FIRST, // My hook executes first.
};

static __s32 __init 
packetprocessor_init(void){
	printk(KERN_ALERT "Loading Packet Processor...\n");
	nf_register_hook(&mynfhook);  
	return 0;
}

static void __exit 
packetprocessor_exit(void){
    printk(KERN_ALERT "Unloading Packet Processor...\n");
    nf_unregister_hook(&mynfhook);
   }
windows_98_nt_2000 yaplej is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
sk_buff allocation............. shrishailnk Linux - Networking 0 05-07-2006 09:38 AM
sk_buff append new shrishailnk Linux - Networking 0 04-09-2006 02:07 AM
sk_buff questions? nocturna_gr Programming 1 01-14-2006 10:36 PM
Help with sk_buff cstrask Programming 0 11-23-2005 10:55 PM
how to use sk_buff wangjinyi Programming 0 10-27-2005 03:28 AM


All times are GMT -5. The time now is 12:47 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration