Home > OS >  Because Linux netlink agreement number, the kernel socket is always create failure
Because Linux netlink agreement number, the kernel socket is always create failure

Time:10-04

Recently in learning, netlink is used to implement the user and the kernel layer of communication, when writing the kernel part of the code, a strange problems:
Every time after unloading the module, must modify the agreement number (NETLINK_TEST), netlink_kernel_create (& amp; Init_net NETLINK_TEST, & amp; CFG); To be successful, do not change the agreement number, has failed to create the kernel sock,
What a big help!!!!!!

Platform Ubuntu16.04 + Linux kernel 4.15.0-46 - generic

The kernel code below

 # include 
#include
#include
#include

# define MAX_MSG_LEN 1024
//# define USER_PID 66666
21//# define NETLINK_TEST proto number


The static struct sock * g_sk_nl_k=NULL;
The static struct NLMSGHDR * g_nlh=NULL;//will contain the user 's pid, data and so on

//static int g_nlmsg_pid=USER_PID;

//static void revFromApp (struct sk_buff * SKB);



//send MSG to app
{int sendToApp PMSG (char *)
Struct NLMSGHDR * NLH=NULL;
Struct sk_buff * nl_skb_out=NULL;
Int ret.
Ret=1;

if(! PMSG) {
Printk (" Msg is null, the send Msg failed! \n");
return -1;
}

if(! G_sk_nl_k) {
Printk (" the User 's pid is' n known, send MSG failed! \n");
return -1;
}

Nl_skb_out=nlmsg_new (MAX_MSG_LEN, 0);//the Allocate a new SKB to send a new netlink message to nl_APP
//nl_skb_out=alloc_skb (NLMSG_SPACE (MAX_MSG_LEN), either GFP_ATOMIC);
if(! Nl_skb_out) {
Printk (" the Allocate new SKB failed, send MSG failed! \n");
return -1;
}

NLH=nlmsg_put (MAX_MSG_LEN nl_skb_out, 0, 0, 0).
If (NLH==NULL) {
Printk (" nlmsg_put failaure! \n");
Nlmsg_free (nl_skb_out);
return -1;
}

NETLINK_CB (nl_skb_out). Creds. Pid=0;
//NETLINK_CB (nl_skb_out). Dst_group=0;
Memcpy (NLMSG_DATA (NLH), PMSG, strlen (PMSG));

//ret=NLMSG;
Ret=netlink_unicast (g_sk_nl_k nl_skb_out, g_nlh - & gt; Nlmsg_pid MSG_DONTWAIT);//USER_PID
If (ret & lt; 0 {
Printk (" nlmsg_unicast failaure! \n");
Nlmsg_free (nl_skb_out);
return -1;
}
Nlmsg_free (nl_skb_out);
return 0;
}



//the receive MSG from app (only run one time if no loop)
The static void revFromApp (struct sk_buff * SKB) {
Char * msgFromApp=NULL;
Char * str0="MSG from the kernel";
//struct NLMSGHDR * NLH; Turn to gloabe
G_nlh=(struct NLMSGHDR *) (SKB - & gt; The data);//NLH message from SHB 's data... : (sk_buff unsigned char * data)
//NLH=nlmsg_hdr (SKB);

MsgFromApp=nlmsg_data (g_nlh);//get data from struct NLMSGHDR * NLH;

//test
Printk (" MSG: % s \ n ", msgFromApp);


SendToApp (str0);
}

Struct netlink_kernel_cfg CFG={
Input=revFromApp,
};

//create a socket
Int netlinkCreate (void) {

G_sk_nl_k=netlink_kernel_create (& amp; Init_net NETLINK_TEST, & amp; CFG);
if(! G_sk_nl_k) {
Printk (" Error creating socket! \n");
return -1;
}
return 0;
}

//close the socket
Void netlinkClose (struct sock * sk_nl_k) {
if(! Sk_nl_k) {

//sock_release (sk_nl_k - & gt; Sk_socket);
Netlink_kernel_release (sk_nl_k);
//sk_nl_k=NULL;
}
}

//netlink open
Static int __init netlink_init (void) {
Int ret=1;
Ret=netlinkCreate ();//get struct sock * g_sk_nl_k
If (ret & lt; 0 {
Printk (" Netlink open failed! \n");
return -1;
}
Printk (" Netlink open! \n");
return 0;
}

//netlink close
The static void __exit netlink_exit (void) {
//netlink_kernel_release (g_sk_nl_k);
//printk (" Netlink close0! \n");
NetlinkClose (g_sk_nl_k);
Printk (" Netlink close1! \n");
}

The module_init (netlink_init);
Module_exit (netlink_exit);
MODULE_LICENSE (" GPL ");
Ysx MODULE_AUTHOR (" ");



The makefile is as follows:

 ifneq ($(KERNELRELEASE)) 

Obj - m:=NLDRV. O
# obj - m +=hello. O

The else

KDIR:=/lib/modules/$(shell uname -r)/build

All:
The make - C $(KDIR) M=$(PWD) modules
Clean:
The rm -f *. Ko * o *. Mod. O *. Mod. C *. Symvers *. Order

Endif

CodePudding user response:

After you can try to create the socket, set the two properties:
The setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, & amp; The state, sizeof (state));
The setsockopt (sock, SOL_SOCKET, SO_REUSEPORT, & amp; The state, sizeof (state));
Parameters:
Int state=1;

CodePudding user response:

Thank you, I found the problem, netlinkclose function in judgment was wrong
  • Related