LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-01-2007, 04:06 AM   #1
skpseqence
LQ Newbie
 
Registered: Jun 2007
Posts: 2

Rep: Reputation: 0
Netlink (Kernel 2.6.10) on ARM


Hi, everybody.

While developing a driver which has to run on an ARM platform I used the following very simple netlink example code :

Sender in userspace


Code:
#include <sys/socket.h>
#include >linux/netlink.h>
#define NETLINK_TEST 17
#define MAX_PAYLOAD 1024

struct sockaddr_nl src_addr, dest_addr;
struct msghdr msg;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
int sent;


int main() 
{
        sock_fd = socket(PF_NETLINK, SOCK_RAW,NETLINK_TEST);
        memset(&src_addr, 0, sizeof(src_addr));
        src_addr.nl_family = AF_NETLINK;      
        src_addr.nl_pid = getpid();  
        src_addr.nl_groups = 0;  
        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;   
        dest_addr.nl_groups = 0; 
        nlh=(struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));

        nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
        nlh->nlmsg_pid = getpid();  
        nlh->nlmsg_flags = 0;
        strcpy(NLMSG_DATA(nlh), "Hello from userspace!\n\0");
        iov.iov_base = (void *)nlh;
        iov.iov_len = nlh->nlmsg_len;
        msg.msg_name = (void *)&dest_addr;
        msg.msg_namelen = sizeof(dest_addr);
        msg.msg_iov = &iov;
        msg.msg_iovlen = 1;
        if ((sent = sendmsg(sock_fd, &msg, 0)) >= 0)
        {
                printf("%d characters sent.\n", sent);
        }
        else
        {
                perror("Send error");
        }
        close(sock_fd);
        return 0;
}

Receiver as kernel module

Code:
#include <linux/config.h>
#include <linux/socket.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netlink.h>
#include <net/sock.h>

#define NETLINK_TEST 17
#define MAX_PAYLOAD 1024

static struct sock *nl_sk;

void input(struct sock *sk, int len)
{
        struct sk_buff *skb = NULL;
        struct nlmsghdr *nlh = NULL;
        u8 *payload = NULL;
        u32 pid = 0;
        while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) 
        {
                nlh = (struct nlmsghdr *)skb->data;
                payload = NLMSG_DATA(nlh);
                printk("%s: received netlink message payload:%s\n", __FUNCTION__, payload);
        }
}

static int __init my_module_init(void)
{
        int err;
        struct sk_buff *skb;
        struct nlmsghdr *nlh = NULL;
        nl_sk = netlink_kernel_create(NETLINK_TEST, input);
        return (nl_sk !=0);
}

static void __exit my_module_exit(void)
{
        sock_release(nl_sk->sk_socket);
}

module_init(my_module_init);
module_exit(my_module_exit);
The code works perfectly on my x86 platform but fails on the ARM platform : The callback function is called but the skb->data contains just senseless data. Are there some basic kernel settings or something else which I could have forgotten ? I'm thankful for any solution hint!
 
Old 07-02-2007, 02:05 AM   #2
skpseqence
LQ Newbie
 
Registered: Jun 2007
Posts: 2

Original Poster
Rep: Reputation: 0
So, just to solve this thread :

The code above does not contain any errors.
I just didn't compile the kernel sources correctly. Although I thought my code just needs to include some headers from the kernel sources and

* EVEN my code compiled and linked through without any errors,
* EVEN it was executable on the target platform,
* EVEN it did not notify any runtime error

it just showed a wrong behaviour as described in the request. I've never experienced such a well hidden, scummy, beastly error! %$#@@ !!!. After caring for that the kernel sources itself compile without errors, my code executed without errors on the target platform after recompilation. I've always thought a semantic misbehaviour of a program always must be caused by a semantic error in the user's code - but got the painful counter evidence here.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
udev unable to receive kernel netlink message humbletech99 Linux - General 3 09-16-2008 11:00 AM
How to patch kernel 2.6.x.x with PXA2xx (ARM) deboom Linux - Kernel 1 05-08-2006 03:23 AM
extract kernel from arm cpu device sharadshankar Programming 1 04-01-2006 03:18 PM
Kernel module using netlink sockets arunka Programming 1 03-31-2005 07:42 AM
Netlink is not configured in the kernel ursus Linux - Newbie 0 02-26-2005 04:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration