kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: "Irfan Ullah (울라 이르판)" <irfan@dke.khu.ac.kr>
To: Greg KH <greg@kroah.com>
Cc: "Valdis Klētnieks" <valdis.kletnieks@vt.edu>,
	"Linux Kernel List" <kernelnewbies@kernelnewbies.org>
Subject: Re: Netlink socket returns NULL in vmx.c kernel file
Date: Tue, 5 Nov 2019 17:59:43 +0900	[thread overview]
Message-ID: <CA+mB8Oyh-y6WYTOt0YEXkXxg0ZY+c8FuBBmGwaQkgEeuEodrGg@mail.gmail.com> (raw)
In-Reply-To: <20191105072232.GC2587462@kroah.com>


[-- Attachment #1.1: Type: text/plain, Size: 981 bytes --]

Thank you for the response.
Attached are the files for kernel-user spaces communication.


On Tue, Nov 5, 2019 at 4:22 PM Greg KH <greg@kroah.com> wrote:

> On Tue, Nov 05, 2019 at 12:29:56PM +0900, Irfan Ullah (울라 이르판) wrote:
> > I have tested code from different aspects. I have searched a lot in two
> > weeks, but still I am facing the same problem. Can you please check out
> > what is the problem with my code. Code is in the attached zipped file.
>
> Random compressed files are not the easiest way to review code.  Just
> attach the files inline if you wish people to be able to review them
> easily, like all kernel development happens.
>
> thanks,
>
> greg k-h
>


-- 
*Best Regards,*


*Mr. Irfan Ullah*
PhD Candidate
Data and Knowledge Engineering(DKE) Lab
Department of Computer Science and Engineering
Kyung Hee University, South Korea.
 +82-010-591-51651 <+82%2010-3877-8867>
  sahibzada.iu@gmail.com
 sahibzada_irfanullah

[-- Attachment #1.2: Type: text/html, Size: 2969 bytes --]

[-- Attachment #2: Makefile --]
[-- Type: application/octet-stream, Size: 229 bytes --]

obj-m :=  netlink_kernel.o
netlink_kernel-y := netlink_kernel_space.o netlink_kernel_module.o
all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

[-- Attachment #3: netlink_kernel_space.h --]
[-- Type: text/x-chdr, Size: 487 bytes --]

#ifndef NETLINK_KERNEL_SPACE_H
#define NETLINK_KERNEL_SPACE_H
MODULE_LICENSE("GPL");

//static void data_update(unsigned long long int, char[100]);
void kernel_space_sender(unsigned long long);
void kernel_space_receiver(struct sk_buff*); 
void create_socket(unsigned long long int);



void prepare_buffer_msg(void);
void user_space_sender(unsigned long long);
void user_space_receiver(void);
/* This function update the data, ie, s_data which is transferred to the user space.*/
#endif

[-- Attachment #4: netlink_kernel_space.c --]
[-- Type: text/x-csrc, Size: 2306 bytes --]

#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include "netlink_kernel_space.h"
MODULE_LICENSE("GPL");
#define NETLINK_USER 31

struct sock *nl_sk = NULL;
struct nlmsghdr *nlh;
struct sk_buff *skb_out;

typedef struct data_pack{
    unsigned long long int int_address;
    char str_data[100];
} struct_data_pack;
struct_data_pack s_data; 
int msg_size = sizeof(s_data);


int pid  = -1;
int res = -1;


void data_update(unsigned long long int addr, char text[100])
{       /* 
         * updating s_data
        */

        strcpy(s_data.str_data, text);
        s_data.int_address = addr;

}


		/* 
		 * This function sends message to the user-space
        */
void kernel_space_sender(unsigned long long int addr)
{
		/* 
		 * updating s_data
        */
		data_update(addr, "KERNER SPACE MESSAGE"); 
		skb_out = nlmsg_new(msg_size,0);
		if(!skb_out) {
				printk(KERN_ERR "Failed to allocate new skb\n");
    			return;
		} 
		nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0);  
		/* not in mcast group */
		NETLINK_CB(skb_out).dst_group = 0; 
		memcpy(nlmsg_data(nlh), &s_data, msg_size);
		pid = 1;
		res=nlmsg_unicast(nl_sk,skb_out,pid);
		if(res<0)
    		printk(KERN_INFO "Error while sending to user\n");

}


		/* 
		 * This function receives message from the user-space.
        */
void kernel_space_receiver(struct sk_buff *skb) 
{
		/* 
		 * updating s_data
        */
		struct_data_pack *xy;
		nlh=(struct nlmsghdr*)skb->data;
 		xy = (struct_data_pack *)NLMSG_DATA(nlh);
		printk(KERN_INFO "Received message payload in Kernel  from USER: %lld - %s\n",  xy->int_address, xy->str_data);
		//msleep(1000);
		//kernel_space_sender(xy->int_address*2);
}

		/* 
		 * This function creates the socket for kernel-user spaces and communication.
        */
void create_socket(unsigned long long int addr)
{
		struct netlink_kernel_cfg cfg = {
    	.input = kernel_space_receiver,
		};
		nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
		if(!nl_sk) {
				printk(KERN_ALERT "Error creating socket.\n");
		}
		//when I remove this wait the code does not work
		msleep(3000);
		/* 
		 * sending the guest physical address to the user space by calling the following function
        */
		kernel_space_sender(addr);
		netlink_kernel_release(nl_sk);
		
		return;

}

[-- Attachment #5: netlink_kernel_module.c --]
[-- Type: text/x-csrc, Size: 484 bytes --]

#include <linux/module.h>
#include "netlink_kernel_space.h"
#include <linux/kallsyms.h>


MODULE_LICENSE("GPL");




int hello_init(void) 
{
		printk(KERN_INFO "entering module hello module\n");

		create_socket(10);
		return 0;
}

void hello_exit(void) 
{
	//netlink_kernel_release(nl_sk);
	printk(KERN_INFO "exiting hello module\n");


}
module_init(hello_init); 
module_exit(hello_exit);
MODULE_DESCRIPTION("Kernel-User Communication using Netlink");
MODULE_AUTHOR("Irfan Ullah");

[-- Attachment #6: netlink_user_module.c --]
[-- Type: text/x-csrc, Size: 2156 bytes --]

#include <sys/socket.h>
#include <linux/netlink.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define NETLINK_USER 31
#define MAX_PAYLOAD 1024 /* maximum payload size*/ 


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

typedef struct data_pack{
    unsigned long long int int_address;
    char str_data[100];
} struct_data_pack;
struct_data_pack s_data;

int msg_size = sizeof(s_data);

void data_update(unsigned long long int addr, char text[100])
{       /* 
         * updating s_data
        */

        strcpy(s_data.str_data, text);
        s_data.int_address = addr;

}


void prepare_buffer_msg()
{

		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));	
		memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
		nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
		//nlh->nlmsg_pid = getpid();
		nlh->nlmsg_pid = 1;
		nlh->nlmsg_flags = 0;
		memcpy(NLMSG_DATA(nlh), &s_data, msg_size);
		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;


}

void user_space_sender(unsigned long long int addr)
{
		data_update(addr, "USER SPACE MESSAGE");
		sendmsg(sock_fd,&msg,0);
}


void user_space_receiver()
{
		while(1) {
				recvmsg(sock_fd, &msg, 0);
 				struct_data_pack *xy = (struct_data_pack *)NLMSG_DATA(nlh);
				printf("Received message payload from KERNEL: %lld - %s\n", xy->int_address, xy->str_data);
				//user_space_sender(xy->int_address*2);
		}

		close(sock_fd);

}





void main()
{
		sock_fd=socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
		while(sock_fd<0) {
				sock_fd=socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
		}

		src_addr.nl_family = AF_NETLINK;
		//src_addr.nl_pid = getpid(); /* self pid */
		src_addr.nl_pid = 1; /* self pid */
		prepare_buffer_msg();
		user_space_receiver(sock_fd);

}

[-- Attachment #7: ReadMe --]
[-- Type: application/octet-stream, Size: 421 bytes --]

Please use the following commands to run the code:
To compile/clean the kernel module
> make
> make clean

To load/unload the kernel modue:
> sudo insmod netlink_Kernel.ko
> sudo rmmod netlink_Kernel
 
To compile and run the user space application:

> gcc netlink_user_module.c -o netlink_user_module
> ./netlink_user_module

(code generates some warnings, but it is not severe and could be ignored for the time being).


[-- Attachment #8: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

  reply	other threads:[~2019-11-05  9:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-23  2:43 Netlink socket returns NULL in vmx.c kernel file Irfan Ullah (울라 이르판)
2019-10-23  5:38 ` Valdis Klētnieks
2019-11-05  3:29   ` Irfan Ullah (울라 이르판)
2019-11-05  7:22     ` Greg KH
2019-11-05  8:59       ` Irfan Ullah (울라 이르판) [this message]
2019-11-05 10:43         ` Valdis Klētnieks
2019-11-08  4:54           ` Irfan Ullah (울라 이르판)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+mB8Oyh-y6WYTOt0YEXkXxg0ZY+c8FuBBmGwaQkgEeuEodrGg@mail.gmail.com \
    --to=irfan@dke.khu.ac.kr \
    --cc=greg@kroah.com \
    --cc=kernelnewbies@kernelnewbies.org \
    --cc=valdis.kletnieks@vt.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).