Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send packet with sockets from kernel module

I am writing a kernel module that should receive messages from user-space and send response back via socket.

When program and module are on the same machine and I use IP 127.0.0.1, everything works fine. But when I try it on different machines and use real network IP, something like 192.168.3.146 it works only in one way.

I receive message from user-space, but I can not receive it from kernel. I use sock_sendmsg function for sending message from kernel and it's not return any error. Also I am not get any messages from firewall that something is came up from another machine, from kernel module.

Here were similar questions and examples, but they were not useful enough for me or examples were used too old kernel version.For skeleton I used this one,from UDP sockets: http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-3.html. Any help?

Kernel module code for sending:

void send_data(unsigned char *data)
{
    if(!IS_ERR_OR_NULL(data))
    {
        int ret;
        mm_segment_t oldfs;
        struct msghdr message;
        struct iovec ioVector;
        struct sockaddr_in sendAddr;

        sendAddr.sin_family = AF_INET;
        sendAddr.sin_addr.s_addr = INADDR_ANY;
        //sendAddr.sin_addr.s_addr = in_aton("192.168.1.75");
        //here I get port from sk_buff structure that I received.
        sendAddr.sin_port = *((unsigned short*)skBuffer->data);

        memset(&message, 0, sizeof(message));
        message.msg_name = &sendAddr;
        message.msg_namelen = sizeof(sendAddr);

        /* send the message back */
        ioVector.iov_base = data;
        ioVector.iov_len  = strlen(data);
        message.msg_iov = &ioVector;
        message.msg_iovlen = 1;
        message.msg_control = NULL;
        message.msg_controllen = 0;

        oldfs = get_fs();
        set_fs(KERNEL_DS);
        ret = sock_sendmsg(sendSocket, &message, strlen(data));
        set_fs(oldfs);
    }
}
like image 920
Mykola Niemtsov Avatar asked Aug 08 '13 10:08

Mykola Niemtsov


1 Answers

I found an alternative solution, using netpoll sockets. It is more easier than sockets, I used before and it works. The answer and proper code is here, on another StackOverflow question.

like image 108
Mykola Niemtsov Avatar answered Oct 03 '22 09:10

Mykola Niemtsov