I am new to kernel development and have a problem in uni casting the data from the kernel space to user space over netlink sockets. send_nat() function is to be called from a kernel module to write a user defined struct nat_mntr to netlink socket. But nlmsg_unicast() failed all the time even I tried different configuration in cfg. Kindly help me find out what is wrong in my code.
userdefined.c
int no_data_request = 1;
EXPORT_SYMBOL(no_data_request);
int request_pid = 0; // PID of requesting process
EXPORT_SYMBOL(request_pid);
void send_nat(struct sock *nl_sk, struct nat_mntr *nat_data, int pid, int group, gfp_t flags, int *sock_closed){
struct nlmsghdr *nlh;
struct sk_buff *skb_out;
int msg_size;
int res = 200;
#define MYPROTO 31
printk(KERN_DEBUG "%s: Entered \n", __FUNCTION__);
if ( nat_data == NULL ){
printk(KERN_DEBUG "%s: nat_data is NULL: Leaving \n", __FUNCTION__);
return ;
}else {
printk(KERN_DEBUG "%s: nat_data is filled \n", __FUNCTION__);
}
if (nl_sk == NULL) {
printk(KERN_DEBUG "%s: nl_sk is NULL \n", __FUNCTION__);
}else
printk(KERN_DEBUG "%s: nl_sock is not null \n", __FUNCTION__);
if (*sock_closed == 1) {
printk(KERN_DEBUG "%s: sock_closed == 1, creating socket \n", __FUNCTION__);
struct netlink_kernel_cfg cfg = {
.groups = 1,
.input = rr,
};
nl_sk = netlink_kernel_create(&init_net, MYPROTO, &cfg);
if (!nl_sk) {
printk(KERN_DEBUG "%s: Error creating socket: sock_closed = %d: Leaving \n", __FUNCTION__ , *sock_closed);
return ;
}
else {
*sock_closed = 0;
printk(KERN_DEBUG "%s: Socket created successfully: sock_closed = %d \n", __FUNCTION__ , *sock_closed );
}
}
else if (*sock_closed == 0 ){
printk(KERN_DEBUG "%s:Already created socket. sock_closed = 0 \n", __FUNCTION__);
}
else {
printk(KERN_DEBUG "%s: sock_closed status is unknown: sock_closed = %d Leaving \n", __FUNCTION__ , *sock_closed);
return ;
}
if( no_data_request){
printk(KERN_DEBUG "%s: No one has requested data: Leaving \n", __FUNCTION__);
return ;
}else{
printk(KERN_DEBUG "%s: Process %d requested the data\n", __FUNCTION__, request_pid );
}
msg_size = sizeof(struct nat_mntr);
skb_out = nlmsg_new(msg_size, 0);
if ( !skb_out ) {
printk(KERN_DEBUG "%s: Failed to skb_out = nlmsg_new(msg_size, 0): Leaving \n", __FUNCTION__);
return;
}
nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_size, 0); /* NLMSG_DONE */
NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
if (!nlh) {
printk(KERN_DEBUG "%s: Failed nlh = nlmsg_put(skb_out, 0, 1, NLMSG_DONE, msg_size, 0): Leaving \n", __FUNCTION__);
return ;
}
else {
printk(KERN_DEBUG "%s: Successfull nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, msg_size, 0) \n", __FUNCTION__);
}
if(memcpy(nlmsg_data(nlh), nat_data , sizeof(nat_data) ) == NULL ) {
printk(KERN_DEBUG "%s: Failed to memcpy(nlmsg_data(nlh), nat_data , sizeof(struct nat_mntr)) Leaving \n", __FUNCTION__);
return ;
}
res = nlmsg_unicast(nl_sk, skb_out2, request_pid);
if (res < 0 ){
printk(KERN_DEBUG "%s: Failed to nlmsg_unicast(nl_sk, skb_out, request_pid): Leaving \n", __FUNCTION__);
return ;
}
printk(KERN_DEBUG "%s: Data sent successfully : Leaving \n", __FUNCTION__);
}
// Callback of kernel socket.
void rr(struct sk_buff *skb){
printk(KERN_DEBUG "%s: Entered \n", __FUNCTION__);
struct nlmsghdr *nlh;
nlh = (struct nlmsghdr *)skb->data;
printk(KERN_DEBUG "Request received \n");
request_pid = nlh->nlmsg_pid; /* pid of sending process */
no_data_request = 0; // Someone is out there
printk(KERN_DEBUG "%s: Leaving:\n", __FUNCTION__);
}
kernel_module.c
#define NAT_GROUP 21
struct sock *nl_sk_ud = NULL;
EXPORT_SYMBOL(nl_sk_ud);
int sock_closed = 1;
EXPORT_SYMBOL(sock_closed);
struct nat_mntr *data = NULL;
EXPORT_SYMBOL(data);
any_kernel_function(){
....
data = get_info(skb, 0, l3proto, l4proto, &target, mtype); // Returns pointer to struct nat_mntr
send_nat(nl_sk_ud, data, 0, NAT_GROUP, 0, &sock_closed);
....
}