#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_TEST 30
#define MSG_LEN 125
#define USER_PORT 100
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");
阅读(828) | 评论(0) | 转发(0) |