如何在Linux内核中找到sk_buff的所有者套接字? [英] How to find owner socket of sk_buff in Linux kernel?
问题描述
我正在尝试找到 sk_buff
实例的所有者套接字,例如 skb
.我的最终目标是找到一个特定的TCP选项,并以某种方式让用户空间应用程序知道.我计划在找到TCP选项时设置一个套接字选项,并让用户空间应用程序调用 getsockopt()
.因此,我需要知道 sk_buff
和 sock
之间的所有权.
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中有一个字段:
I find there is a field in sk_buff:
struct sock *sk;
但是,当我尝试在 tcp_parse_options
,我总是得到 skb-> sk == NULL
.
However, when I try to retrieve this field at tcp_parse_options
in tcp_input.c
, I always get skb->sk == NULL
.
所以我想知道如何在这里找到所有者?
So I'm wondering how can I find the owner here?
此外,我发现3个地方似乎在设置所有者套接字:
Also, I find 3 places that seems to set the owner socket:
http://lxr.free-electronics.com/source/net/ipv4/tcp_input.c?v=3.11#L4181
http://lxr.free-electronics.com/source/net/ipv4/tcp_input.c?v=3.11#L4196
http://lxr.free-electronics.com/source/net/ipv4/tcp_input.c?v=3.11#L4456
我还在 sk_buff
中添加了一个新标记,用于指示并将其设置为 tcp_parse_options
.然后我在这三个地方检查这个标志.但是它们都不显示标志已设置,因此我无法确定是否设置套接字选项.
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.
对这个问题有任何想法或建议吗?
Any idea or suggestion for this problem?
提前谢谢!
推荐答案
从sk_buff(skb)中,您可以通过以下方式获得袜子(sk):
From the sk_buff (skb) you can get the sock (sk) with something like this:
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.
这篇关于如何在Linux内核中找到sk_buff的所有者套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!