pbxiaoliang
驱动牛犊
驱动牛犊
  • 注册日期2007-07-24
  • 最后登录2016-01-09
  • 粉丝1
  • 关注0
  • 积分60分
  • 威望19点
  • 贡献值0点
  • 好评度18点
  • 原创分0分
  • 专家分0分
阅读:2035回复:1

netlink问题,很奇怪

楼主#
更多 发布于:2008-01-29 16:37
写了个模块,其中用到了netlink,编译通过,没有错误和警告,可是就是加载不了,只是说操作不被允许,不知道为什么。请大家帮忙看看。谢谢
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/netfilter_ipv4.h>
#include <linux/inet.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/netlink.h>
#include <linux/spinlock.h>
#include <asm/semaphore.h>
#include <net/sock.h>
#include<asm/atomic.h>
DECLARE_MUTEX(receive_sem);
atomic_t check_configer=ATOMIC_INIT(0);
struct netlink_configer_packet
{
    struct nlmsghdr  configer_h;
    int configer;
};
static struct sock *nlfd;
static struct nf_hook_ops check_tor_ops;
static void kernel_receive(struct sock *sk,int len)
{
    do
    {
        struct sk_buff *skb;
        if(down_trylock(&receive_sem))
            return;
        while((skb=skb_dequeue(&sk->sk_receive_queue))!=NULL)
        {
            
                struct netlink_configer_packet *ncp=NULL;
                if(skb->len>=sizeof(struct netlink_configer_packet))
                {
                    ncp=(struct netlink_configer_packet *)skb->data;
                    if((ncp->configer_h.nlmsg_len>=sizeof(struct netlink_configer_packet))
                        &&(skb->len>=ncp->configer_h.nlmsg_len))
                    {
                        if(ncp->configer==1986)
                        {
                            atomic_set(&check_configer,1986);
                            
                        }
                    }
                }
        
            
                            kfree_skb(skb);
        }
        up(&receive_sem);
    }while(nlfd && nlfd->sk_receive_queue.qlen);
}
static unsigned int check_tor_hook(unsigned int hook,
                 struct sk_buff **pskb,
                 const struct net_device *in,
                 const struct net_device *out,
                 int (*okfn)(struct sk_buff *))
{  if(atomic_sub_and_test(1986,&check_configer))
    return NF_ACCEPT;
    printk(KERN_EMERG "ONE PACKET!");
    return NF_ACCEPT;
}
static int check_tor_init(void)
{
    nlfd=netlink_kernel_create(NETLINK_ROUTE,0,kernel_receive,THIS_MODULE);
    if(!nlfd)
    {
        printk(KERN_EMERG "CON'T CREAT A NETLINK SOCKET\n");
        return -1;
    }
    check_tor_ops.hook=check_tor_hook;
    check_tor_ops.pf=PF_INET;
    check_tor_ops.hooknum=NF_IP_PRE_ROUTING;
    check_tor_ops.priority=NF_IP_PRI_FILTER+2;    
   if(nf_register_hook(&check_tor_ops)!=0)
   printk(KERN_EMERG "THE HOOK CAN'T CREAT\n");
   else
   return 0;
}

static void check_tor_exit(void)
{
    if(nlfd)
    {
        sock_release(nlfd->sk_socket);
    }
    nf_unregister_hook(&check_tor_ops);
}
module_init(check_tor_init);
module_exit(check_tor_exit);
pbxiaoliang
驱动牛犊
驱动牛犊
  • 注册日期2007-07-24
  • 最后登录2016-01-09
  • 粉丝1
  • 关注0
  • 积分60分
  • 威望19点
  • 贡献值0点
  • 好评度18点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2008-01-29 18:18
解决了,是协议的问题,不过还是不清楚,学习中.........
游客

返回顶部