当前位置: 技术问答>linux和unix
请大家帮帮忙,一个对我很重要的问题
来源: 互联网 发布时间:2015-08-17
本文导语: 我最近一直在做一个netfilter的防火墙,下面是代码 firewall.c: #include #include #include #include #include #include #include #include #if LINUX_VERSION_CODE>=KERNEL_VERSION(2,4,9) MODULE_LICENSE("GPL"); MODULE_AUTHOR("XXX@hotmail.com"); #endif st...
我最近一直在做一个netfilter的防火墙,下面是代码
firewall.c:
#include
#include
#include
#include
#include
#include
#include
#include
#if LINUX_VERSION_CODE>=KERNEL_VERSION(2,4,9)
MODULE_LICENSE("GPL");
MODULE_AUTHOR("XXX@hotmail.com");
#endif
static int in_use=0;
static int major=0;
static unsigned int deny_ip[1000]; /* 被拒绝的IP列表 */
static unsigned int deny_port[1000]; /* 被拒绝的port列表 */
unsigned int current_deny_ip=0; /* 当前有多少个被拒绝的IP列表 */
unsigned int current_deny_port=0; /* 当前有多少个被拒绝的port列表 */
static int add_ip(unsigned int ip); /* 增加一个IP到列表 */
static int release_ip(unsigned int ip); /* 取消列表中的一个IP */
static void list_current_deny_ip(); /* 打印当前被拒绝的IP列表 */
static int check_ip(struct sk_buff* sb); /* 检查IP的函数 */
static int add_port(unsigned int port); /* 增加一个port到列表 */
static int release_port(unsigned int port); /* 取消列表中的一个port */
static void list_current_deny_port(); /* 打印当前被拒绝的port列表 */
static int check_port(struct sk_buff* sb); /* 检查port的函数 */
static int fw_ioctl(struct inode*,struct file* file, /* 给fw_fops调用的函数 */
unsigned int cmd,unsigned long arg);
static int fw_open(struct inode* inode,struct file* file); /* 设备开启时调用的函数 */
static int fw_release(struct inode* inode,struct file* file); /* 设备关闭时调用的函数 */
unsigned int hook_func(unsigned int hooknum, /* hook_func,核心函数 */
struct sk_buff** skb,
const struct net_device* in,
const struct net_device* out,
int(*okfn)(struct sk_buff*));
/* 用于注册我们的函数的数据结构 */
static struct nf_hook_ops nfho;
/* 设备驱动接口 file_operations */
struct file_operations fw_fops={
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
fw_ioctl,
NULL,
fw_open,
NULL,
fw_release,
NULL
};
/* 增加一个IP到列表 */
static int add_ip(unsigned int ip)
{
int i;
for (i=0;i>16,
(deny_ip[i]&0xFF000000)>>24);
}
}
/* 检查IP的函数 */
static int check_ip(struct sk_buff* sb)
{
if(!sb) return NF_ACCEPT;
if(!(sb->nh.iph)) return NF_ACCEPT;
int i=0;
for (i=0;inh.iph->saddr==deny_ip[i])
{
return NF_DROP;
}
}
return NF_ACCEPT;
}
static int fw_open(struct inode* inode,struct file* file)
{
printk("openn");
if (in_use)
{
return -EBUSY;
}
else
{
MOD_INC_USE_COUNT;
++in_use;
return 0;
}
return 0;
}
static int fw_release(struct inode* inode,struct file* file)
{
printk("release!n");
in_use^=in_use;
MOD_DEC_USE_COUNT;
return 0;
}
static int fw_ioctl(struct inode* inode,struct file* file,
unsigned int cmd,unsigned long arg)
{
int ret=0;
printk("ioctln");
switch(cmd)
{
case 1:
{
add_ip((unsigned int)arg);
break;
}
case 2:
{
release_ip((unsigned int)arg);
break;
}
default:
ret=-EBADRQC;
}
return ret;
}
/* 注册的hook函数的实现 */
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*))
{
struct sk_buff* sb=*skb;
/* check ip */
if (check_ip(sb)==NF_DROP)
{
return NF_DROP;
}
else
{
return NF_ACCEPT;
}
}
int init_module()
{
SET_MODULE_OWNER(&fw_fops);
if ((major=register_chrdev(241,"Firewall",&fw_fops))
firewall.c:
#include
#include
#include
#include
#include
#include
#include
#include
#if LINUX_VERSION_CODE>=KERNEL_VERSION(2,4,9)
MODULE_LICENSE("GPL");
MODULE_AUTHOR("XXX@hotmail.com");
#endif
static int in_use=0;
static int major=0;
static unsigned int deny_ip[1000]; /* 被拒绝的IP列表 */
static unsigned int deny_port[1000]; /* 被拒绝的port列表 */
unsigned int current_deny_ip=0; /* 当前有多少个被拒绝的IP列表 */
unsigned int current_deny_port=0; /* 当前有多少个被拒绝的port列表 */
static int add_ip(unsigned int ip); /* 增加一个IP到列表 */
static int release_ip(unsigned int ip); /* 取消列表中的一个IP */
static void list_current_deny_ip(); /* 打印当前被拒绝的IP列表 */
static int check_ip(struct sk_buff* sb); /* 检查IP的函数 */
static int add_port(unsigned int port); /* 增加一个port到列表 */
static int release_port(unsigned int port); /* 取消列表中的一个port */
static void list_current_deny_port(); /* 打印当前被拒绝的port列表 */
static int check_port(struct sk_buff* sb); /* 检查port的函数 */
static int fw_ioctl(struct inode*,struct file* file, /* 给fw_fops调用的函数 */
unsigned int cmd,unsigned long arg);
static int fw_open(struct inode* inode,struct file* file); /* 设备开启时调用的函数 */
static int fw_release(struct inode* inode,struct file* file); /* 设备关闭时调用的函数 */
unsigned int hook_func(unsigned int hooknum, /* hook_func,核心函数 */
struct sk_buff** skb,
const struct net_device* in,
const struct net_device* out,
int(*okfn)(struct sk_buff*));
/* 用于注册我们的函数的数据结构 */
static struct nf_hook_ops nfho;
/* 设备驱动接口 file_operations */
struct file_operations fw_fops={
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
fw_ioctl,
NULL,
fw_open,
NULL,
fw_release,
NULL
};
/* 增加一个IP到列表 */
static int add_ip(unsigned int ip)
{
int i;
for (i=0;i>16,
(deny_ip[i]&0xFF000000)>>24);
}
}
/* 检查IP的函数 */
static int check_ip(struct sk_buff* sb)
{
if(!sb) return NF_ACCEPT;
if(!(sb->nh.iph)) return NF_ACCEPT;
int i=0;
for (i=0;inh.iph->saddr==deny_ip[i])
{
return NF_DROP;
}
}
return NF_ACCEPT;
}
static int fw_open(struct inode* inode,struct file* file)
{
printk("openn");
if (in_use)
{
return -EBUSY;
}
else
{
MOD_INC_USE_COUNT;
++in_use;
return 0;
}
return 0;
}
static int fw_release(struct inode* inode,struct file* file)
{
printk("release!n");
in_use^=in_use;
MOD_DEC_USE_COUNT;
return 0;
}
static int fw_ioctl(struct inode* inode,struct file* file,
unsigned int cmd,unsigned long arg)
{
int ret=0;
printk("ioctln");
switch(cmd)
{
case 1:
{
add_ip((unsigned int)arg);
break;
}
case 2:
{
release_ip((unsigned int)arg);
break;
}
default:
ret=-EBADRQC;
}
return ret;
}
/* 注册的hook函数的实现 */
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*))
{
struct sk_buff* sb=*skb;
/* check ip */
if (check_ip(sb)==NF_DROP)
{
return NF_DROP;
}
else
{
return NF_ACCEPT;
}
}
int init_module()
{
SET_MODULE_OWNER(&fw_fops);
if ((major=register_chrdev(241,"Firewall",&fw_fops))