- 论坛徽章:
- 0
|
- /****************************************************
- 名称: watch_out
- 描述: 判断数据包是否是需要的数据包。然后将需要判断的
- 如果是需要判断的数据包就返回NF_QUEUE。将数据包发送给
- 用户态。
- 返回值:1 判断为需要处理的数据包返回NF_QUEUE
- 2 判断为其他数据包返回NF_ACCEPT
- 3 根据用户态返回结果确定返回值
- ****************************************************/
- unsigned int watch_out(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;
- struct tcphdr *tcph;
- //轻易不要对原始的skb指针进行操作。
- sb=*skb;
- //将不是TCP协议的数据包放过
- if(sb->nh.iph->protocol!=IPPROTO_TCP){
- return NF_ACCEPT;
- }
-
- //将源地址和目的地址不是指定IP的数据包放行
- if(sb->nh.iph->saddr!=inet_addr(ker_conf.ip)&&sb->nh.iph->daddr!=inet_addr(ker_conf.ip)){
- return NF_ACCEPT;
- }
-
- //取得当前数据包的TCP头指针
- tcph=(struct tcphdr *)((sb->data)+(sb->nh.iph->ihl * 4));
- //将源端口和目的端口都不是指定端口的数据包放行
- if(tcph->source!=htons(ker_conf.port) && tcph->dest != htons(ker_conf.port)){
- return NF_ACCEPT;
- }
- /*
- ip_hl = 4 * sb->nh.iph->ihl;
- tcp_hl = 4 * (sb->data[ip_hl + 12] >> 4);
- data = sb->data + ip_hl + tcp_hl;
- len = (unsigned long) sb->tail - (unsigned long) data;
-
- printk("data=%s\n", data);
- */
- //返回NF_QUEUE将数据包加入队列。内核查找webwall_queue_handle实例.调用webwall_queue_handle->outfn
-
- printk("enqueue the packet\n");
- return NF_QUEUE;
- }
- int queue_fun(struct sk_buff *skb, struct nf_info *info,unsigned int queuenum, void *data){
- /************************
- to do
- ************************/
-
- return 1;
- }
- int init(void){
- int ret=0;
-
-
-
- //实验注册queue
- queue_handle.outfn=queue_fun;
- //data为outfn函数最后一个参数
- queue_handle.data=NULL;
- //用途不明
- queue_handle.name="webwall_queue";
- //初始化注册钩子结构
- post_hook.hook = watch_out; //watch_out为钩子函数
- post_hook.pf = PF_INET; //PF_INET为协议簇
- post_hook.priority = NF_IP_PRI_FIRST; //NF_IP_PRI_FIRST为钩子函数的优先级
- post_hook.hooknum = NF_IP_POST_ROUTING; //NF_IP_POST_ROUTING为要注册的钩子
- //读取本地配置文件
- ret=read_config(&ker_conf);
- if(ret==ERR){
- printk("webwall:Something are not ok in the function \"read_config\"\n\n");
- return -1;
- }
- //register the hook function
- nf_register_hook(&post_hook);
- //register the webwall queue
- nf_register_queue_handler(PF_INET, &queue_handle);
-
- return 1;
- }
- void exit(void){
- DEBUG("Now unregisting the Webwall kernel mod.\n");
-
- //unregister the hook function
- nf_unregister_hook(&post_hook);
- //unregister the webwall queue
- nf_unregister_queue_handler(PF_INET);
- }
复制代码 |
|