Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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);
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);
}
I would like to obtain the ip address from kernel space for one of my projects.
I am writing a virtual file system built off of ramfs, which is loaded as a module.
I want to deny the write operation to a file if the user on the filesystem is connected to an unknown ip address.
So in simple, my question is, what is the simplest way to obtain an ip address of a device from kernel space?
I have attempted yaplej's suggest of using nethooks, however, I do not understand how to invoke the operation .hook
I guess I really do not understand how netfilter works in the first place.
Any help on this would be greatly appreciated, or help with any other method of obtaining the ip address.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.