大四写的Linux网络模块

来源:互联网 发布:mac cad中文版下载 编辑:程序博客网 时间:2024/05/29 08:53

以下是hello.c文件:该模块关闭80端口,及拒绝127.0.0.1的数据包。

#include<linux/kernel.h>

#include<linux/module.h>

#include<linux/netfilter.h>

#include<linux/netfilter_ipv4.h>

#include<linux/ip.h>

#include<linux/skbuff.h>

#include<linux/tcp.h>

#include<linux/if.h>

#include<linux/in.h>

/* 用于注册我们的函数的数据结构 */

staticstruct nf_hook_ops nfho_port;

staticstruct nf_hook_ops nfho_ip;

 

staticunsigned char *drop_ip = "\x7f\x00\x01\x01";

staticunsigned char *deny_port = "\x00\x50";   /* port 80 */

 

/* 注册的hook函数的实现 */

unsignedint hook_deny_port(unsigned int hooknum,struct sk_buff *skb,const structnet_device *in,const struct net_device *out, int(*okfn)(struct sk_buff*))

{

          struct sk_buff *sb = skb;

  struct iphdr *iph;

  struct tcphdr *tcph;

 

  if(!sb) return NF_ACCEPT;

  iph = ip_hdr(sb);

  if(!iph) return NF_ACCEPT;

 

  /*Make sure this is a TCP packet first*/

  if(iph->protocol != IPPROTO_TCP)

  {

    return NF_ACCEPT;

  }

  tcph = (struct tcphdr *)(sb->data +iph->ihl * 4);

 

  if(tcph->dest == *(__be16 *)deny_port)

  {

   pr_warning("Dropped packet from prot%d\n",ntohs(tcph->dest) );

    return NF_DROP;

  }

 

  return NF_ACCEPT;

 

}

}

 

unsignedint hook_deny_ip(unsigned int hooknum,struct sk_buff *skb,const structnet_device *in,const struct net_device *out,int(*okfn)(struct sk_buff*))

{

        struct iphdr     *iph ;

        if(!skb)

        {

                printk("skb isNULL\n");

                return NF_ACCEPT;

        }

 

        iph = ip_hdr(skb);

        if(!iph)

                return NF_ACCEPT;

         printk("iph->saddr:%d,drop_ip:%d.\n",iph->saddr,*(__be32*)drop_ip);

        if ( iph->saddr == *(__be32 *)drop_ip)

        {

               pr_info("Dropped packet from... %d.%d.%d.%d\n",*drop_ip, *(drop_ip+1), *(drop_ip+2), *(drop_ip+3) );

                printk("drop ipsucced\n");

                return NF_DROP;

        }else{

                return NF_ACCEPT;

       }

}

 

/* 初始化程序 */

intinit(void)

{

/* 填充我们的hook数据结构 */

        nfho_port.hook = hook_deny_port;   /* 处理函数 */

        nfho_port.hooknum = 1;             /* 使用IPv4的第一个hook*/

        nfho_port.pf = PF_INET;

        nfho_port.priority = NF_IP_PRI_FIRST -1;  /* 让我们的函数首先执行 */

        nf_register_hook(&nfho_port);

 

       nfho_ip.hook = hook_deny_ip;

       nfho_ip.hooknum = 1;

       nfho_ip.pf = PF_INET;

       nfho_ip.priority = NF_IP_PRI_FIRST;

       nf_register_hook(&nfho_ip);

        return 0;

}

/* 清除程序 */

voidexit(void)

{

        nf_unregister_hook(&nfho_port);

         nf_unregister_hook(&nfho_ip);

}

 

module_init(init);

module_exit(exit);

 

MODULE_LICENSE("GPL");

  

以下为hello.c的makefile文件:

obj-m:= hello.o

PWD= $(shell pwd)

KERNEL_SRC= /usr/src/kernels/2.6.32.11-99.fc12.i686.PAE/

all:

    $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules

clean:

    rm *.ko

    rm *.o

 

通过insmod hello.ko加载到内核 ,rmmod  hello.ko卸载

 $(obj-m)表示对象文件(object files)编译成可加载的内核模块,即ko。

$(MAKE) -C $(KERNEL_SRC)M=$(PWD) modules个命令开始是改变它的目录到用 -C选项提供的目录下( 就是说, 你的内核源码目录 ). 它在那里会发现内核的顶层 makefile. 这个 M= 选项使makefile 在试图建立模块目标前, 回到你的模块源码目录.这个目标, 依次地, 是指在 obj-m 变量中发现的模块列表, 在我们的例子里设成了 hello.o  建立ko模块分两步:


原创粉丝点击