How to find owner socket of sk_buff in Linux kernel?

时光毁灭记忆、已成空白 提交于 2021-02-19 04:41:49

问题


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.

I find there is a field in 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.

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

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

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#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.

Any idea or suggestion for this problem?

Thanks in advance!


回答1:


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.



来源:https://stackoverflow.com/questions/37031711/how-to-find-owner-socket-of-sk-buff-in-linux-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!