Facing problem while using netlink sockets
*kernel space send application*/
#define MAX_PAYLOAD 1024
struct sock *nl_sk = NULL;
sturct sk_buff *skb = NULL;
struct nlmsghdr *nlh;
int err;
netlink_test()
{
nl_sk = netlink_kernel_create(NULL,NETLINK_TEST,
nl_data_ready,1,1);
if(nl_sk ==NULL)
printk("netlink create fail\n");
else
printk("netlink create pass\n");
skb=alloc_skb(NLMSG_SPACE(MAX_PAYLOAD),GFP_KERNEL);
if(skb == NULL)
printk("skb alloc fail\n");
else
printk("skb alloc pass\n");
nlh = (struct nlmsghdr *)skb->data;
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = 0;
nlh->nlmsg_flags = 0;
strcpy(NLMSG_DATA(nlh), "Msg from kernel!");
NETLINK_CB(skb).pid = 0; /* from kernel */
NETLINK_CB(skb).dst_groups = 0;
/*multicast the message to all listening processes*/
err = netlink_unicast(nl_sk, skb, 0,GFP_ATOMIC);
if(err < 0)
printk("netlink unicast fail\n");
else
printk("netlink unicast pass\n");
printk("message sent\n");
}
}
/*user space Receive application*/
#include <sys/socket.h>
#include <linux/netlink.h>
#define MAX_PAYLOAD 1024 /* maximum payload size*/
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
void main()
{
memset(&src_addr, 0, sizeof(src_addr));
src__addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */
src_addr.nl_groups = 0; /* not in mcast groups */
bind(sock_fd, (struct sockaddr*)&src_addr,
sizeof(src_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */
nlh=(struct nlmsghdr *)malloc(
NLMSG_SPACE(MAX_PAYLOAD));
iov.iov_base = (void *)nlh;
iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
printf("Waiting for message from kernel\n");
/* Read message from kernel */
recvmsg(fd, &msg, 0);
printf(" Received message payload: %s\n",
NLMSG_DATA(nlh));
close(sock_fd);
}
when i insert the user space application
./userapp &
waiting for message from kernel
when i insert the kernel module
insmod kermod.ko
netlink create pass
skb alloc pass
netlink unicast pass
message sent
the kernel snippet i pasted is only part of my module code?
the print messages i am getting is all pass but i am not able to get message in the user space ?
Can anyone tell me what is the problem?
|