大四写的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模块分两步:
- 大四写的Linux网络模块
- 一个我大四写的俄罗斯方块的小游戏
- Linux--写一个简单的模块
- 开始写博客,记录一下从大四开始的生活
- Linux模块试写
- 基于Linux的复杂网络模块构建
- 写于大四,工作一周
- 一起写RPC框架(七)RPC网络模块的搭建五 Netty模块
- 一起写RPC框架(三)RPC网络模块的搭建一 网络传输模型
- 如何写Linux下编译驱动模块的Makefile
- 我的大学---大四
- 大四的心情
- 不一样的大四
- 大四的迷茫
- 大四的迷茫
- 大四的迷茫
- 大四的学生
- 疯狂的大四
- [Linux/Ubuntu] vi/vim 使用方法讲解
- 职业态度
- Android操作系统安全
- linux socket网络编程 常用函数及头文件
- 计算机开机过程笔记
- 大四写的Linux网络模块
- 单元测试(1)--对xml进行对比
- 为天地立心,为生民立命,为往圣继绝学,为万世开太平。
- 滚动条设计:基本算法
- 关于mysql插入int空值的处理
- hdu 1158
- 使用TileMap制作游戏,在cocos2d-x中使用(三)
- 笔试题
- UVa 10154 - Weights and Measures dp 降维