Search code examples
linuxlinux-kernelbpfebpf

How can I determine the direction of a packet in a BPF_PROG_TYPE_SK_SKB verdict program?


I am writing a BPF_PROG_TYPE_SK_SKB verdict program to make redirection decisions. How can I work out the direction of the packet (ingress/egress)?

I have access to the local and remote port of each packet, but I am unable to determine which one is the source or destination port and so cannot determine the direction. Should this be possible? And if not then why not?

uname -a: Linux ubuntu-bionic 4.18.0-16-generic #17~18.04.1-Ubuntu SMP Tue Feb 12 13:35:51 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux


Solution

  • Probably there are some very elegant solution. I solved this by using sockmap with only 2 elements: in that way, you dont have to deal with directions, because its trivial. Sadly, with this method, you have to make a sockmap for every socket pair.