红联Linux门户
Linux帮助

包过滤防火墙调试出错 帮我下!!!急!!!!

发布时间:2008-05-07 19:57:25来源:红联作者:ddwfish
makefile文件源码如下:


CC=gcc
MODFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX -I/usr/src/linux-2.4.20-8/include/

all: firewall.o user_interface.c
insmod firewall.o
$(CC) firewall user_interface.c

firewall.o:firewall.c
$(CC) $(MODFLAGS) -c firewall.c -o firewall.o
文章评论

共有 5 条评论

  1. caocao_love 于 2008-05-16 11:56:40发表:

    你加我QQ吧:401952673

  2. Arthas225 于 2008-05-09 12:40:13发表:

    0:5ty(

  3. 太平桥 于 2008-05-07 23:43:25发表:

    出错信息?

    q):-s

  4. 微宝贝 于 2008-05-07 23:10:26发表:

    :0L 可怕

  5. ddwfish 于 2008-05-07 19:58:23发表:

    firewall.c文件源码如下
    /* 基于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

    const int DEV_NUMBER=100; /* 设备号, 用来mknod */
    const int FTP_PORT=5376; /* 设默认端口号码为21 */

    static int in_use=0;
    static int major=0; /*用来判定注册设备是否成功 */

    static unsigned int defaultFTPPort; /* 用户设置的FTP端口, 默认21 */
    static unsigned int deny_ip[1000]; /* 被拒绝的IP列表 */
    static unsigned int deny_port[1000]; /* 被拒绝的port列表 */
    static unsigned int deny_ftp_ip[1000]; /* 被FTP拒绝的IP列表 */
    unsigned int current_deny_ip=0; /* 当前有多少个被拒绝的IP列表 */
    unsigned int current_deny_port=0; /* 当前有多少个被拒绝的port列表 */
    unsigned int current_deny_ftp_ip=0; /* 当前有多少个被FTP拒绝的IP列表 */

    /* 声明对于IP的操作 */
    static int add_ip(unsigned int ip); /* 增加一个IP到列表 */
    static int release_ip(unsigned int ip); /* 取消列表中的一个IP */
    static int check_ip(struct sk_buff* sb); /* 检查IP的函数 */
    static void show_current_ip_list(); /* 显示当前的IP列表 */

    /* 声明对port的操作 */
    static int add_port(unsigned int port); /* 增加一个port到列表 */
    static int release_port(unsigned int port); /* 取消列表中的一个port */
    static int check_port(struct sk_buff* sb); /* 检查port的函数 */
    static void show_current_port_list(); /* 显示当前的port列表 */

    /* 声明ftp上的操作 */
    static int add_ftp_ip(unsigned int ip); /* 增加一个IP到FTP列表 */
    static int release_ftp_ip(unsigned int ip); /* 取消FTP列表中的一个IP */
    static int check_ftp_ip(struct sk_buff* sb); /* 检查FTP的函数 */
    static void show_current_ftp_ip_list(); /* 显示当前的FTP拒绝列表 */
    static void show_current_ftp_port(); /* 显示当前的FTP端口 */
    static void set_ftp_port(unsigned int port); /* 改变默认的FTP端口 */

    /* 其他需要调用的函数 */
    static int fw_ioctl(struct inode*,struct file* file, /* 给外部调用的函数 */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, /* 钩子函数,核心函数 */
    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)
    {
    printk("----------------------\n");
    int i;
    for (i=0;i {
    if (ip==deny_ip[i])
    {
    printk("This ip is already in the ip list\n");
    return 0;
    }
    }
    deny_ip[current_deny_ip++]=ip;
    printk("Add ip %d.%d.%d.%d to the ip list successfully\n",ip&0x000000FF,
    (ip&0x0000FF00)>>8,(ip&0x00FF0000)>>16,(ip&0xFF000000)>>24);

    return 1;
    }

    /* 取消列表中的一个IP */
    static int release_ip(unsigned int ip)
    {
    printk("----------------------\n");
    int i;
    for (i=0;i {
    if (ip==deny_ip[i])
    {
    printk("Remove ip %d.%d.%d.%d from the ip list successfully\n",ip&0x000000FF,
    (ip&0x0000FF00)>>8,(ip&0x00FF0000)>>16,
    (ip&0xFF000000)>>24);
    deny_ip[i]=deny_ip[--current_deny_ip];
    return 1;
    }
    }
    printk("This ip is not in the ip list\n");
    return 0;
    }

    /* 显示当前的IP列表 */
    static void show_current_ip_list()
    {
    printk("----------------------\n");
    int i;
    printk("Here are the deny_ips!\nPlease check them!\n");
    for (i=0;i {
    printk("%d: %d.%d.%d.%d\n",i+1,
    deny_ip[i]&0x000000FF,(deny_ip[i]&0x0000FF00)>>8,
    (deny_ip[i]&0x00FF0000)>>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;
    for (i=0;i{
    if (sb->nh.iph->saddr==deny_ip[i])
    {
    printk("Firewall: Dropped packet from IP: %d.%d.%d.%d\n",
    deny_ip[i]&0x000000FF,(deny_ip[i]&0x0000FF00)>>8,
    (deny_ip[i]&0x00FF0000)>>16,(deny_ip[i]&0xFF000000)>>24);
    return NF_DROP;
    }
    }
    printk(“Firewall: Riceved a packet from IP: %d.%d.%d.%d\n”,
    sb->nh.iph->saddr&0x000000FF,( sb->nh.iph->saddr &0x0000FF00)>>8,
    (sb->nh.iph->saddr &0x00FF0000)>>16,( sb->nh.iph->saddr &0xFF000000)>>24);
    printk(“If you want drop the packet,please pess addip\n”);

    return NF_ACCEPT;
    }

    /* 增加一个port到列表 */
    static int add_port(unsigned int port)
    {
    printk("----------------------\n");
    int i;
    for (i=0;i {
    if (port==deny_port[i])
    {
    printk("This port is already in the port list\n");
    return 0;
    }
    }
    printk("Add port %d to the port list successfully\n",((port&0xFF00)>>8|(port&0x00FF)<<8));
    deny_port[current_deny_port++]=port;
    return 1;
    }

    /* 取消列表中的一个port */
    static int release_port(unsigned int port)
    {
    printk("----------------------\n");
    int i;
    for (i=0;i {
    if (port==deny_port[i])
    {
    printk("Remove port %d from the port list successfully\n",
    ((port&0xFF00)>>8|(port&0x00FF)<<8));
    deny_port[i]=deny_port[--current_deny_port];
    return 1;
    }
    }
    printk("This port is not in the port list\n");
    return 0;
    }

    /* 显示当前的port列表 */
    static void show_current_port_list()
    {
    printk("----------------------\n");
    printk("Here are the deny_ports!\nPlease check them!\n");
    int i;
    for (i=0;i {
    printk("%d: %d\n",i+1,
    ((deny_port[i]&0xFF00)>>8|(deny_port[i]&0x00FF)<<8));
    }
    }

    /* 检查port的函数 */
    static int check_port(struct sk_buff* sb)
    {
    if(!sb) return NF_ACCEPT;
    if(!(sb->nh.iph)) return NF_ACCEPT;

    struct tcphdr* thread=(struct tcphdr*)(sb->data+(sb->nh.iph->ihl*4));

    int i;

    for (i=0;i {
    if (thread->dest==deny_port[i])
    {
    printk("Firewall: Dropped packet from TCP port: %d\n",
    ((deny_port[i]&0xFF00)>>8|(deny_port[i]&0x00FF)<<8));
    return NF_DROP;
    }
    }
    printk(“Firewall: Riceved a packet from TCP port: %d\n”,
    ((thread->dest&0xFF00)>>8|(thread->dest&0x00FF)<<8));
    printk(“If you want drop the packet,please pess addport\n”);

    return NF_ACCEPT;
    }
    /* 增加一个IP到FTP列表 */
    static int add_ftp_ip(unsigned int ip)
    {
    printk("----------------------\n");
    int i;
    for (i=0;i {
    if (ip==deny_ftp_ip[i])
    {
    printk("This ip is already in the ftp ip list\n");
    return 0;
    }
    }
    deny_ftp_ip[current_deny_ftp_ip++]=ip;
    printk("Add ip %d.%d.%d.%d to the ftp ip list successfully\n",ip&0x000000FF,
    (ip&0x0000FF00)>>8,(ip&0x00FF0000)>>16,(ip&0xFF000000)>>24);

    return 1;
    }

    /* 取消FTP列表中的一个IP */
    static int release_ftp_ip(unsigned int ip)
    {
    printk("----------------------\n");
    int i;
    for (i=0;i {
    if (ip==deny_ftp_ip[i])
    {
    printk("Remove ip %d.%d.%d.%d from the ftp ip list successfully\n",ip&0x000000FF,
    (ip&0x0000FF00)>>8,(ip&0x00FF0000)>>16,
    (ip&0xFF000000)>>24);
    deny_ftp_ip[i]=deny_ftp_ip[--current_deny_ftp_ip];
    return 1;
    }
    }
    printk("This ip is not in the ftp ip list\n");
    return 0;
    }

    /* 检查FTP的函数 */
    static int check_ftp_ip(struct sk_buff* sb)
    {
    if(!sb) return NF_ACCEPT;
    if(!(sb->nh.iph)) return NF_ACCEPT;

    struct tcphdr* thread=(struct tcphdr*)(sb->data+(sb->nh.iph->ihl*4));

    int i;

    for (i=0;i {
    if (thread->dest==defaultFTPPort && sb->nh.iph->saddr==deny_ftp_ip[i])
    {
    printk("Firewall: Dropped packet from IP: %d.%d.%d.%d\n",
    deny_ftp_ip[i]&0x000000FF,(deny_ftp_ip[i]&0x0000FF00)>>8,
    (deny_ftp_ip[i]&0x00FF0000)>>16,(deny_ftp_ip[i]&0xFF000000)>>24);
    return NF_DROP;
    }
    }
    printk("Firewall: Recived packet from IP: %d.%d.%d.%d\n",
    sb->nh.iph &0x000000FF,( sb->nh.iph &0x0000FF00)>>8,
    (sb->nh.iph)&0x00FF0000)>>16,( sb->nh.iph &0xFF000000)>>24);
    printk(“If you want drop them ,please press addftpip\n”);
    return NF_ACCEPT;
    }

    /* 显示当前的FTP拒绝列表 */
    static void show_current_ftp_ip_list()
    {
    printk("----------------------\n");
    int i;
    printk("Here are the deny_ftp_ips!\nPlease check them!\n");
    for (i=0;i {
    printk("%d: %d.%d.%d.%d\n",i+1,
    deny_ftp_ip[i]&0x000000FF,(deny_ftp_ip[i]&0x0000FF00)>>8,
    (deny_ftp_ip[i]&0x00FF0000)>>16,(deny_ftp_ip[i]&0xFF000000)>>24);
    }
    }

    /* 改变默认的FTP端口 */
    static void set_ftp_port(unsigned int port)
    {
    defaultFTPPort=port;
    }

    /* 显示当前的FTP端口 */
    static void show_current_ftp_port()
    {
    printk("----------------------\n");
    printk("The current ftp port is %d\n",((defaultFTPPort&0xFF00)>>8|(defaultFTPPort&0x00FF)<<8));
    }

    /* 设备开启打开的函数 */
    static int fw_open(struct inode* inode,struct file* file)
    {
    if (in_use)
    {
    return -EBUSY;
    }
    else
    {
    MOD_INC_USE_COUNT;
    ++in_use;
    }
    return 0;
    }

    /* 设备关闭打开的函数 */
    static int fw_release(struct inode* inode,struct file* file)
    {
    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;
    switch(cmd)
    {
    case 1:
    {
    /* 增加一个IP到屏蔽列表 */
    add_ip((unsigned int)arg);
    break;
    }
    case 2:
    {
    /* 释放屏蔽列表中的一个IP */
    release_ip((unsigned int)arg);
    break;
    }
    case 3:
    {
    /* 显示当前被屏蔽的IP列表 */
    show_current_ip_list();
    break;
    }
    case 4:
    {
    /* 增加一个端口到屏蔽列表 */
    add_port((unsigned short)arg);
    break;
    }
    case 5:
    {
    /* 释放屏蔽列表中的一个端口 */
    release_port((unsigned short)arg);
    break;
    }
    case 6:
    {
    /* 显示当前被屏蔽的服务端的端口 */
    show_current_port_list();
    break;
    }
    case 7:
    {
    /* 增加一个IP到FTP屏蔽列表 */
    add_ftp_ip((unsigned int)arg);
    break;
    }
    case 8:
    {
    /* 释放FTP屏蔽列表中的一个IP */
    release_ftp_ip((unsigned int)arg);
    break;
    }
    case 9:
    {
    /* 显示FTP屏蔽列表 */
    show_current_ftp_ip_list();
    break;
    }
    case 10:
    {
    /* 显示当前设置的FTP端口 */
    show_current_ftp_port();
    break;
    }
    case 11:
    {
    /* 设置当前的FTP端口, 默认21 */
    set_ftp_port((unsigned short)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;

    /* 检查 ip */
    if (check_ip(sb)==NF_DROP)
    {
    return NF_DROP;
    }
    else
    {
    /* 检查 port */
    if (check_port(sb)==NF_DROP)
    {
    return NF_DROP;
    }
    else
    {
    /* 检查被FTP拒绝的IP列表 */
    if (check_ftp_ip(sb)==NF_DROP)
    {
    return NF_DROP;
    }
    }
    }
    return NF_ACCEPT;
    }

    int init_module()
    {
    SET_MODULE_OWNER(&fw_fops);
    /* 注册这个设备 */
    printk("----------------------\n");
    if ((major=register_chrdev(DEV_NUMBER,"firewall",&fw_fops))<0)
    {
    /* 注册设备失败, 打印错误消息, 返回 */
    printk("Firewall: Failed registering control device!\n");
    printk("Firewall: Module installation aborted\n");
    return 0;
    }
    /* 注册成功 */
    in_use^=in_use;
    printk("Firewall: Control device successfully registered.\n");
    printk("Author: KisokaFenG.\n");

    /* 填充我们的hook数据结构 */
    nfho.hook=hook_func; /* 处理函数 */
    nfho.hooknum=NF_IP_PRE_ROUTING; /* 使用IPV4的第一个hook */
    nfho.pf=PF_INET; /*IPV4*/
    nfho.priority=NF_IP_PRI_FIRST; /* 让我们的函数首先执行 */

    nf_register_hook(&nfho); /* 注册 */

    /* 初始化FTP的端口 */
    defaultFTPPort=FTP_PORT;

    return 0;
    }

    void cleanup_module()
    {
    nf_unregister_hook(&nfho); /* 注消 */

    int ret;

    printk("----------------------\n");
    /* 注消设备 */
    if((ret=unregister_chrdev(DEV_NUMBER,"firewall"))!=0)
    {
    /* 注消设备失败 */
    printk("Firewall: Removal of module failed!\n");
    }
    /* 注消设备成功 */
    printk("Firewall: Removal of module successfule\n");
    }
    /* End firewall.c */