免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2596 | 回复: 3
打印 上一主题 下一主题

内核网络模块编程求救 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-19 20:23 |只看该作者 |倒序浏览
5可用积分
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <net/icmp.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/route.h>
#include <net/dst.h>
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#ifdef CONFIG_BRIDGE_NETFILTER
#include <linux/netfilter_bridge.h>
#endif

MODULE_LICENSE("GPL");
MODULE_AUTHOR("zj");
MODULE_DESCRIPTION("Xtables: packet \"winnuke\" target for IPv4");

static unsigned int
reject_tg(unsigned int hooknum,
                   struct sk_buff *skb,
                   const struct net_device *in,
                   const struct net_device *out,
                   int (*okfn)(struct sk_buff *))
{
    const struct iphdr *oiph;
    const struct tcphdr *oth;
    struct tcphdr _otcph;

    oiph = ip_hdr(skb);
    if (oiph->frag_off & htons(IP_OFFSET))
        return NF_DROP;
    switch (oiph->protocol)
        {
                case IPPROTO_TCP:
                        printk("TCP PACKET!!!\n");
                        break;
                case IPPROTO_UDP:
                      printk("UDP PACKET!!!\n");
                        break;
                case IPPROTO_ICMP:
                      break;
                default:
                        return NF_ACCEPT;
        }
    oth = skb_header_pointer(skb, ip_hdrlen(skb),
                 sizeof(_otcph), &_otcph);
    if (oth == NULL)
        return NF_DROP;

    if (oth->urg)
        return NF_DROP;
    else
        {
            printk("urg ok!!!!!");
            printk("s_port:%d,d_port:%d",oth->source,oth->dest);
        }
        return NF_ACCEPT;
}

static struct xt_target reject_tg_reg __read_mostly = {
    .name        = "reject",
    .family        = PF_INET,
    .target        = reject_tg,
    .hooks        = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
              (1 << NF_INET_LOCAL_OUT),
    .me        = THIS_MODULE,
};

static int __init reject_tg_init(void)
{
    return xt_register_target(&reject_tg_reg);
}

static void __exit reject_tg_exit(void)
{
    xt_unregister_target(&reject_tg_reg);
}

module_init(reject_tg_init);
module_exit(reject_tg_exit);


TARGET=ipt
ifneq ($(KERNELRELEASE),)
obj-m := ${TARGET}.o
else
    KDIR := /lib/modules/$(shell uname -r)/build
    WD := $(shell pwd)
default:
    $(MAKE) -C $(KDIR) M=$(PWD) modules
endif
clean:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
    rm *odule*


make的时候ipt.c:66: warning: initialization from incompatible pointer type
我ismod加载后根本没作用,我是防ipt_REJECT.c写的.内核是最新的2.6.27

论坛徽章:
0
2 [报告]
发表于 2008-12-19 20:23 |只看该作者
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/netdevice.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <net/tcp.h>
#include <linux/netfilter_ipv4.h>

unsigned int
winnuke_local_in_func (unsigned int hooknum,
&nbsp;&nbsp;&nbsp;&nbsp;   struct sk_buff *skb,
&nbsp;&nbsp;&nbsp;&nbsp;   const struct net_device *in,
&nbsp;&nbsp;&nbsp;&nbsp;   const struct net_device *out,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int (*okfn) (struct sk_buff *))
{
&nbsp;&nbsp;struct iphdr *iph ;
&nbsp;&nbsp;struct tcphdr *tcph ;
&nbsp;&nbsp;struct tcphdr _otcph ;
&nbsp;&nbsp;
&nbsp;&nbsp;iph = ip_hdr (skb);
&nbsp;&nbsp;switch (iph->protocol)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;case IPPROTO_TCP:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printk ("It's a TCP PACKET\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tcph = skb_header_pointer(skb,ip_hdrlen(skb),sizeof(_otcph),&_otcph);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;case IPPROTO_ICMP:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NF_ACCEPT;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;case IPPROTO_UDP:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NF_ACCEPT;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;default:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NF_ACCEPT;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;printk ("s_port=%d,d_port=%d!",(int)tcph->source,(int)tcph->dest);
&nbsp;&nbsp;&nbsp;&nbsp;printk ("urg=%d",(int)tcph->urg);
&nbsp;&nbsp;return NF_ACCEPT;
}

static struct nf_hook_ops winnuke_ops[] __read_mostly = {
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.hook   = winnuke_local_in_func,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.owner  = THIS_MODULE,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.pf     = PF_INET,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.hooknum    = NF_INET_LOCAL_IN,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.priority   = NF_IP_PRI_FIRST,
&nbsp;&nbsp;&nbsp;&nbsp;},
};

static int __init winnuke_init(void)
{
&nbsp;&nbsp;int ret;

&nbsp;&nbsp;printk("ins winnuke module\n");
&nbsp;&nbsp;ret = nf_register_hook (winnuke_ops);
&nbsp;&nbsp;if(ret < 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ret;
&nbsp;&nbsp;return 0;
}

static void __exit winnuke_fini(void)
{
&nbsp;&nbsp;&nbsp;&nbsp;printk("rm winnuke module\n");
&nbsp;&nbsp;&nbsp;&nbsp;nf_unregister_hook (winnuke_ops);
}

module_init(winnuke_init);
module_exit(winnuke_fini);

论坛徽章:
0
3 [报告]
发表于 2008-12-20 21:03 |只看该作者
对了,对于新的内核,我也出现过类似的情况。即所写的程序在编译时,只有警告可能连警告都没有,但就是编译后的程序,能运行,就是得不到自己想的结果。我仔细思考过这个问题与现象,出现这种现象只有二个可能。第一呢,新的内核对于向下的兼容性不是太好。出现了bug。第二,是我们对于新的内核文件了解不够,以至于我们所写的程序,没有达到新内核的新的规定而出现这种现象。

论坛徽章:
0
4 [报告]
发表于 2008-12-20 21:17 |只看该作者
如果要是这样的话,最好找2.6.27中的类似代码比较一下了,有可能真的改动了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP