LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2009, 02:11 AM   #1
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Rep: Reputation: 15
Set iptables rules from Kernel Module


Hello,

I am developing kernel that set the iptables rules based on some data detection by my kernel module.

I could not figure out how to set the iptables rules from the kernel module itself.

Could you please help me to figure out how to set the iptables rules from the kernel module?


Best Regards,
---------------------------------------
Saurabh D Chokshi
 
Old 04-29-2009, 03:03 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by saurabhchokshi View Post
I could not figure out how to set the iptables rules from the kernel module itself.
As far as I am aware Linux does not use a pull but a push model. That is, the kernel does not (and should not ever) read userland data or exec arbitrary userland apps. Look at how iptables rule management or sysctls are handled for instance: userland reads from and supplies data to the kernel via /proc. Not the other way around. Linux Kernel Modules do not (and should not ever) muck with Netfilter Framework data the way you propose they should. That would be a grave violation.

Last edited by unSpawn; 04-29-2009 at 03:05 AM.
 
Old 05-01-2009, 03:11 PM   #3
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Original Poster
Rep: Reputation: 15
Actually, now I am trying to write the rules into some file and reading the rules from user space and set the ip tables rules.

However, I am getting problem in writing and reading from the file. In my code, I am not getting any error and but not able to read and write into the file.

Could you please let me know where I am going wrong in my code?

My code is

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/syscalls.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>

static void write_file(char *filename, char *data)
{
struct file *file;

// mm_segment_t old_fs = get_fs();
// set_fs(KERNEL_DS);

file = filp_open(filename, O_WRONLY|O_CREAT, 0777);
ssize_t wc;
wc = vfs_write(file, data, strlen(data),0);
if(wc < strlen(data))
{
printk(KERN_INFO "Problem in Writing Data\n");
}
filp_close(file,0);
/* if (fd >= 0) {
sys_write(fd, data, strlen(data));
file = fget(fd);
if (file) {
vfs_write(file, data, strlen(data), &pos);
fput(file);
}
// sys_close(fd);

} // set_fs(old_fs); */
}

static void read_file(char *filename)
{
struct file *fd;
// char buf[1];

// mm_segment_t old_fs = get_fs();
// set_fs(KERNEL_DS);

fd = filp_open(filename, O_RDONLY, 0);
char buf[80];

// if (fd >= 0) {
printk(KERN_DEBUG);
vfs_read(fd,buf, 5, 0);
printk("Data Read : %s\n",buf);
// while (sys_read(fd, buf, 1) == 1)
// printk("%c", buf[0]);
// printk("\n");
// sys_close(fd);
// }
filp_close(fd,0);
}


static int __init init(void)
{
write_file("/tmp/test","Hello World\n");
read_file("/tmp/test");
return 0;
}

static void __exit exit(void)
{

}

MODULE_LICENSE("GPL");
module_init(init);
module_exit(exit);
 
Old 05-01-2009, 03:39 PM   #4
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Rep: Reputation: 16
Looks like you are trying to work with netfilter in which rules are depending a file.

check this out:
http://www.linuxjournal.com/article/7660.

It has the source code link at the bottom, which is a ftp client implemented in kernel socket (associated with some file writing thing you may need).

For netfilter, You could check this one for a simple intro:
http://www.linuxjournal.com/article/7184

Hope it helps,

-Kun
 
Old 05-01-2009, 04:39 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...and to support arguments I submit http://www.linuxjournal.com/article/8110. Since you managed to crosspost your questions you would have seen that anyway. Note that crossposting (or necroposting for that matter) your questions, regardless of the motivation or reason, is considered unwanted at LQ. Please don't.
 
Old 05-01-2009, 11:10 PM   #6
saurabhchokshi
Member
 
Registered: May 2007
Posts: 35

Original Poster
Rep: Reputation: 15
Hi ,

I looked at the article you suggest me regarding the queries.

But, it did not work out.

Do you have any idea where this code could go wrong?

Thanks,
Saurabh Chokshi
 
  


Reply



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
[SOLVED] iptables: dissecting recent module rules anomie Linux - Security 3 03-27-2008 12:32 PM
netfilter/iptables in kernel: module vs static -Nw- neX Linux - Security 5 03-25-2005 06:44 PM
startup script to set up iptables rules doesn't run alexfittyfives Debian 2 06-01-2004 07:21 PM
kernel 2.6.1: iptables doesn't run..but module is loaded pablovschby Programming 4 02-06-2004 03:46 AM
iptables - module, or built into kernel? dunkyb Linux - General 3 02-16-2003 06:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration