Hi,

I got a problem about netlink socket.
I want to broadcast a message from kernel to user space.

Here is the kernel code:

struct sock *nl_sk = NULL;
struct sk_buff *skb = NULL;
struct nlmsghdr *nlh;
nl_sk = netlink_kernel_create(NETLINK_UNUSED,
1,
NULL,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
NULL,
#endif
THIS_MODULE);
if(nl_sk != NULL) {
skb = alloc_skb(NLMSG_SPACE(MAX_PAYLOAD), GFP_ATOMIC);
if(skb != NULL) {
nlh = (struct nlmsghdr *)skb->data;
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = 0;
nlh->nlmsg_flags = 0;
skb_put(skb, NLMSG_SPACE(MAX_PAYLOAD));
strcpy(NLMSG_DATA(nlh), "Greeting from kernel!\n");
NETLINK_CB(skb).pid = 0;
NETLINK_CB(skb).dst_group = 1;
netlink_broadcast(nl_sk, skb, 0, 1, GFP_ATOMIC);
} else if (skb) {
kfree_skb(skb);
}
sock_release(nl_sk->sk_socket);
}

and here is the user space code:

struct sockaddr_nl src_addr;
struct msghdr msg;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sk_nl;
sk_nl = socket(PF_NETLINK, SOCK_RAW, NETLINK_UNUSED);
if (sk_nl < 0)
return -1;
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
src_addr.nl_groups = 1;
bind(sk_nl, (struct sockaddr *)&src_addr, sizeof(src_addr));
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
iov.iov_base = (void *)nlh;
iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
while (1) {
recvmsg(sk_nl, &msg, 0);
printf("Received from kernel broadcast: %s\n", NLMSG_DATA(nlh));
}
close(sk_nl);

I compiled and run, but no messages were received at the user space.
Can anybody help me?
Thanks!