如何在Linux内核中找到sk_buff的主套接字?

时间:2022-07-24 23:54:18

I'm trying to find the owner socket of an sk_buff instance, say, skb. My ultimate goal is to find a specific TCP option and somehow let the user space application to know. I plan to set a socket option when I find the TCP option and let the user space app to call getsockopt(). Therefore I need to know the ownership between sk_buff and sock.

我试图找到sk_buff实例的所有者套接字,比如skb。我的最终目标是找到一个特定的TCP选项,并以某种方式让用户空间应用程序知道。我计划在找到TCP选项时设置一个套接字选项,并让用户空间应用程序调用getsockopt()。因此我需要知道sk_buff和sock之间的所有权。

I find there is a field in sk_buff:

我发现在sk_buff中有一个字段:

struct sock *sk;

However, when I try to retrieve this field at tcp_parse_options in tcp_input.c, I always get skb->sk == NULL.

但是,当我尝试在tcp_input中的tcp_parse_options中检索这个字段时。c,我总是得到skb->sk = NULL。

So I'm wondering how can I find the owner here?

我想知道我怎么才能找到这里的主人?

Also, I find 3 places that seems to set the owner socket:

另外,我发现有3个地方似乎设置了业主插座:

http://lxr.free-electrons.com/source/net/ipv4/tcp_input.c?v=3.11#L4181

http://lxr.free-electrons.com/source/net/ipv4/tcp_input.c?v=3.11 L4181

http://lxr.free-electrons.com/source/net/ipv4/tcp_input.c?v=3.11#L4196

http://lxr.free-electrons.com/source/net/ipv4/tcp_input.c?v=3.11 L4196

http://lxr.free-electrons.com/source/net/ipv4/tcp_input.c?v=3.11#L4456

http://lxr.free-electrons.com/source/net/ipv4/tcp_input.c?v=3.11 L4456

I also add a new flag in sk_buff for indicating and set it at tcp_parse_options. Then I check this flag at these three places. But none of them shows the flag is set so that I cannot determine whether to set the socket option.

我还在sk_buff中添加了一个新标志,用于指示并将其设置为tcp_parse_options。然后我在这三个地方检查这个标志。但它们都没有显示设置了标志,因此我无法确定是否设置套接字选项。

Any idea or suggestion for this problem?

对这个问题有什么想法或建议吗?

Thanks in advance!

提前谢谢!

1 个解决方案

#1


2  

From the sk_buff (skb) you can get the sock (sk) with something like this:

从sk_buff (skb)中,你可以得到这样的袜子:

const struct tcphdr *th = tcp_hdr(skb);
struct sock *sk = __inet_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
if (sk)
    struct socket* = sk->sk_socket;

That worked for me. Don't forget to add the right headers.

为我工作。不要忘记添加正确的标题。

#1


2  

From the sk_buff (skb) you can get the sock (sk) with something like this:

从sk_buff (skb)中,你可以得到这样的袜子:

const struct tcphdr *th = tcp_hdr(skb);
struct sock *sk = __inet_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
if (sk)
    struct socket* = sk->sk_socket;

That worked for me. Don't forget to add the right headers.

为我工作。不要忘记添加正确的标题。