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!
__________________________

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
thierryD's picture

same problem

On April 7th, 2009 thierryD (not verified) says:

hi,

I have the same problem and I didn't succed to fix it.
I use separated functions for init and destroy function, and another function for the kernelcode (creating netlink socket and send msgs).
Do you have any ideas ?

Thierry

Anonymous's picture

I encountered similar

On October 4th, 2008 Anonymous (not verified) says:

I encountered similar situation,

I put the kernel code to a separate function. I called this function from function mymodule_init declared with the use of module_init(mymodule_init),

My client( user space ) program stuck with no response from kernel.

The solution was:
1) call netlink_kernel_create from init function
2) call sock_release from destroy function
3) put the remaining code to another function( lets say test_netlink ), call this function when a particular event occurs( for example when somebody reads /proc entry )

It seems that the bind function have to be called after netlink socket had been crated

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.


Read this issue