简体   繁体   English

从内核模块发送带有套接字的数据包

[英]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. 当程序和模块位于同一台计算机上并且使用IP 127.0.0.1 ,一切正常。 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. 但是,当我在不同的机器上尝试并使用真实的网络IP时(如192.168.3.146它只能以一种方式工作。

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. 我使用sock_sendmsg函数从内核发送消息,并且不返回任何错误。 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 . 这里有类似的问题和示例,但是它们对我来说不够用,或者示例使用的内核版本太旧。对于骨架,我使用了这个,来自UDP套接字: 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);
    }
}

I found an alternative solution, using netpoll sockets. 我找到了使用netpoll套接字的替代解决方案。 It is more easier than sockets, I used before and it works. 它比我以前使用的套接字更容易,并且可以工作。 The answer and proper code is here , on another StackOverflow question. 答案和正确的代码在这里 ,关于另一个StackOverflow问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM