2

How do I receive network layer (OSI Layer 3) packets in Linux? More specifically, IPv4 packets regardless of transport layer protocol (UDP, TCP, ...). Preferably in C without libraries.

Using socket(AF_INET, SOCK_RAW, IPPROTO_RAW), I'm only able to send IPv4 packets; not receive.

jww
  • 97,681
  • 90
  • 411
  • 885
  • 3
    "I'm only able to send IPv4 packets; not receive". Please show your code. We can't point out any problems if we can't see it. – kaylum Nov 28 '19 at 18:21
  • Also see [Raw Socket promiscuous mode not sniffing what I write](https://stackoverflow.com/q/12177708/608639), [Filter packets in network stack while sniffing packets on Linux?](https://stackoverflow.com/q/9752583/608639) and [Read raw IPv4 in promiscuous mode on Linux?](https://stackoverflow.com/q/27069400/608639) – jww Nov 29 '19 at 05:31

1 Answers1

1

Did you try with recvfrom from socket library. Like in this example :

sock_raw = socket(AF_INET , SOCK_RAW , protocol);
while(1)
{
    data_size = recvfrom(sock_raw , buffer , 65536 , 0 , &addr , &addr_size);
}
Khinza
  • 31
  • 4
  • "regardless of transport layer protocol (UDP, TCP, ...)". OP wants to receive all IP packets. Does your code do that? – kaylum Nov 28 '19 at 18:29
  • @Victor Fonne I think your answer is correct, but with the wrong emphasis (so maybe just edit it a bit). The key is `SOCK_RAW`: http://man7.org/linux/man-pages/man7/raw.7.html – root Nov 28 '19 at 19:28