#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef unsigned char ui_08;
typedef unsigned short ui_16;
typedef unsigned int ui_32;
typedef char si_08;
typedef short si_16;
typedef int si_32;
#define netlink_test30
#define msg_len125
#define user_port100
struct sock *nlsock=null;
extern struct net init_net;
int send_umsg(char *buf, uint16_t len)
{
struct sk_buff *nl_skb;
struct nlmsghdr *nl_h;
int ret;
//创建sk_buff空间
nl_skb=nlmsg_new(len, gfp_atomic);
if(!nl_skb)
{
printk("netlink alloc failed\n");
return -1;
}
//设置netlink消息头部
nl_h=nlmsg_put(nl_skb,0,0,netlink_test,len,0);
if(nl_h==null)
{
printk("nlmsg_put failed\n");
nlmsg_free(nl_skb);
return -1;
}
memcpy(nlmsg_data(nl_h),buf,len);
ret=netlink_unicast(nlsock,nl_skb,user_port,msg_dontwait);
return ret;
}
static void recv_umsg(struct sk_buff *skb)
{
struct nlmsghdr *nl_h=null;
char *umsg=null;
char *kmsg="hello netlink users";
if(skb->len>=nlmsg_total_size(0))
{
nl_h=nlmsg_hdr(skb);
umsg=nlmsg_data(nl_h);
if(umsg)
{
printk("kernel recv from user:%s\n",umsg);
send_umsg(kmsg,strlen(kmsg));
}
}
}
struct netlink_kernel_cfg nl_cfg ={
.input=recv_umsg,
};
int nlink_demo_init(void)
{
nlsock=(struct sock *)netlink_kernel_create(&init_net,netlink_test,&nl_cfg);
if(nlsock==null)
{
printk("netlink_kernel_create error\n");
return -1;
}
printk("nlink demo init\n");
return 0;
}
void nlink_demo_exit(void)
{
if(nlsock)
{
netlink_kernel_release(nlsock);
nlsock=null;
}
printk("netlink demo exit\n");
}
module_init(nlink_demo_init);
module_exit(nlink_demo_exit);
module_license("gpl");
阅读(660) | 评论(0) | 转发(0) |