Re: Netlink Socket: trouble while using netlink_broadcast

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I have tried to change groupId, with different value, but there should
be somethings wrong: the userspace prgm doesn't receive message.
I would like to know if there is a plugin for tcpdump or an way of
capture trafic on netlink socket, just like usual network socket in
order to see were is my problem: in kernel space code or userspace
code.
My program code is attached, with english commit (sorry for last mail).
I have also tried my code on differents kernel (redhat 4.4 with kernel
2.6.9-42.ELsmp, centos52 kernel 2.6.18-92.1.22).

If there is other ways to send information from kernel space to user space...
My aim is to capture data trafic from infiniband and dump trafic, for
example in a netlink socket. Then I will be able with a userspace code
to translate informations in .cap files.
Thats why I think netlink_broadcast is a good idea.

I will be always looking at kernelnewbie mailinglsit for several month,

Regards,

Thierry



--------------
kernel-level 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_IB 24
#define MAX_PAYLOAD 1024
#define GROUP_IB 59


static struct sock *nl_sk = NULL;

static void nl_ib_data_ready (struct sock *sk, int len)
{
	wake_up_interruptible(sk->sk_sleep);
}

static void netlink_ib_open()
{
	struct sk_buff *skb = NULL;
	struct nlmsghdr *nlh = NULL;
	int err;
	u32 pid;

	//Socket_create
	nl_sk = netlink_kernel_create(NETLINK_IB, GROUP_IB, nl_ib_data_ready,
THIS_MODULE);

	skb = alloc_skb(NLMSG_SPACE(MAX_PAYLOAD),GFP_KERNEL);
	nlh = (struct nlmsghdr *)skb->data;

	nlh->nlmsg_len =  NLMSG_SPACE(MAX_PAYLOAD);
	nlh->nlmsg_pid = 0; //from kernel
	nlh->nlmsg_flags = 0;

	strcpy(NLMSG_DATA(nlh), "Message from kernel ");
	NETLINK_CB(skb).pid = 0;
	NETLINK_CB(skb).dst_pid = 0; //Multicast
	NETLINK_CB(skb).dst_group = GROUP_IB;

	//data send
	printk( KERN_ALERT "data sent\n");
	netlink_broadcast(nl_sk, skb, 0, GROUP_IB, GFP_KERNEL);
	sock_release(nl_sk->sk_socket);
}

static int __init netlink_ib_module_init(void)
{
	printk(KERN_INFO "netlink_ib module: Init\n");
	netlink_ib_open();
	return 0;
}

static void __exit netlink_ib_module_exit(void)
{
	printk(KERN_INFO "Unloading netlink_ib module\n");
}

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Kernel/User socket for IB support");
module_init(netlink_ib_module_init);
module_exit(netlink_ib_module_exit);

------------------
userlevel code:

#include <sys/socket.h>
#include <linux/netlink.h>
#define NETLINK_IB 24
#define GROUP_IB 59

#define MAX_PAYLOAD 1024
struct sockaddr_nl src_addr, dst_addr;
struct nlmsghdr *nlh = NULL;
struct msghdr msg;
struct iovec iov;
int sock_fd;


int main()
{
	/* Socket create*/
	sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_IB);

	memset(&src_addr, 0, sizeof(src_addr));
	src_addr.nl_family = AF_NETLINK;
	src_addr.nl_pid = getpid();
	src_addr.nl_groups = GROUP_IB; // Multicast
	bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));

	memset(&dst_addr, 0, sizeof(dst_addr));
	nlh = (struct nlhmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
	memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));

	iov.iov_base = (void *)nlh;
	iov.iov_len = nlh->nlmsg_len;
	msg.msg_name = (void *)&dst_addr;
	msg.msg_namelen = sizeof(dst_addr);
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	printf("Listenning on netlink socket...\n");
	recvmsg(sock_fd, &msg, 0);
	printf("Message received in userspace: %s\n", NLMSG_DATA(nlh));
	close(sock_fd);

	return 0;

}


-----------


On Wed, Apr 8, 2009 at 5:51 PM, Sandeep K Sinha <sandeepksinha@xxxxxxxxx> wrote:
> Milind can you help Thierry on this.
>
>
> On Wed, Apr 8, 2009 at 7:14 PM, Thierry <chocapiiic.tiery@xxxxxxxxx> wrote:
>> Hi,
>>
>> I want to use netlink socket between kernel and userspace in order to
>> get some information from kernel code.
>> I use netlink_broadcast function in order to send information to a
>> specific group, and my user-level code connect to this socket with the
>> correct groupId but doesn't receive anythings.
>> Everythings works when I use netlink_unicast.
>>
>> I use centos5.2, kernel 2.6.18-92.1.22
>>
>> If someone can help me...
>>
>> Thierry
>>
>>
>>
>>
>>
>> ---------------------------
>>
>> kernel-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>
>> //#include <net/netlink.h>
>>
>> #define NETLINK_IB 24
>> #define MAX_PAYLOAD 1024
>> #define GROUP_IB 59
>>
>> static struct sock *nl_sk = NULL;
>>
>> static void nl_ib_data_ready (struct sock *sk, int len)
>> {
>>        wake_up_interruptible(sk->sk_sleep);
>> }
>>
>> static void netlink_ib_open()
>> {
>>        struct sk_buff *skb = NULL;
>>        struct nlmsghdr *nlh = NULL;
>>        int err;
>>        u32 pid;
>>
>>        //Creation de la netlink socket <=> socket()
>>        nl_sk = netlink_kernel_create(NETLINK_IB, GROUP_IB, nl_ib_data_ready,
>> THIS_MODULE);
>>
>>        skb = alloc_skb(NLMSG_SPACE(MAX_PAYLOAD),GFP_KERNEL);
>>        nlh = (struct nlmsghdr *)skb->data;
>>
>>        nlh->nlmsg_len =  NLMSG_SPACE(MAX_PAYLOAD);
>>        nlh->nlmsg_pid = 0; //Depuis le noyau
>>        nlh->nlmsg_flags = 0;
>>
>>        strcpy(NLMSG_DATA(nlh), "Message du kernel =(");
>>        NETLINK_CB(skb).pid = 0;
>>        NETLINK_CB(skb).dst_pid = 0; //Multicast
>>        NETLINK_CB(skb).dst_group = GROUP_IB;
>>
>>        //Envoi des donnees
>>        printk( KERN_ALERT "j'envoi els donnees\n");
>>        netlink_broadcast(nl_sk, skb, 0, GROUP_IB, GFP_KERNEL);
>>        sock_release(nl_sk->sk_socket);
>> }
>>
>> static int __init netlink_ib_module_init(void)
>> {
>>        printk(KERN_INFO "netlink_ib module: Initialisation de la Netlink Socket\n");
>>        netlink_ib_open();
>>        return 0;
>> }
>>
>> static void __exit netlink_ib_module_exit(void)
>> {
>>        printk(KERN_INFO "Dechargement de netlink_ib module\n");
>> }
>>
>> MODULE_DESCRIPTION("Kernel/User socket for IB support");
>> module_init(netlink_ib_module_init);
>> module_exit(netlink_ib_module_exit);
>>
>> ----------------------------
>>
>> and the user-level code:
>>
>> #include <sys/socket.h>
>> #include <linux/netlink.h>
>> #define NETLINK_IB 24
>> #define GROUP_IB 59
>>
>> #define MAX_PAYLOAD 1024
>>
>> struct sockaddr_nl src_addr, dst_addr;
>> struct nlmsghdr *nlh = NULL;
>> struct msghdr msg;
>> struct iovec iov;
>> int sock_fd;
>>
>>
>> int main()
>> {
>>        /* Ouverture de la socket Netlink*/
>>        sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_IB);
>>
>>        memset(&src_addr, 0, sizeof(src_addr));
>>        src_addr.nl_family = AF_NETLINK;
>>        src_addr.nl_pid = getpid();
>>        src_addr.nl_groups = GROUP_IB; // Multicast
>>        bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));
>>
>>        memset(&dst_addr, 0, sizeof(dst_addr));
>>        nlh = (struct nlhmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
>>        memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
>>
>>        iov.iov_base = (void *)nlh;
>>        iov.iov_len = nlh->nlmsg_len;
>>        msg.msg_name = (void *)&dst_addr;
>>        msg.msg_namelen = sizeof(dst_addr);
>>        msg.msg_iov = &iov;
>>        msg.msg_iovlen = 1;
>>
>>        printf("On ecoute...\n");
>>        /* On ecoute ce que le kernel renvoi */
>>        recvmsg(sock_fd, &msg, 0);
>> printf("jai recu");
>>        char * truc = NLMSG_DATA(nlh);
>>        printf("Message recu dans lespace utilisateur: %s\n", truc);
>>        close(sock_fd);
>>
>>        return 0;
>>
>> }
>>
>> --
>> To unsubscribe from this list: send an email with
>> "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
>> Please read the FAQ at http://kernelnewbies.org/FAQ
>>
>>
>
>
>
> --
> Regards,
> Sandeep.
>
>
>
>
>
>
> “To learn is to change. Education is a process that changes the learner.”
>

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux