简体   繁体   中英

How to find owner socket of sk_buff in Linux kernel?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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