title: Confusion with AF_INET, with SOCK_RAW as the socket type, V/S AF_PACKET, with SOCK_DGRAM and SOCK_RAW as the socket type
body: <p>I am quite new to network programming and have been trying to wrap my head around this for quite sometime now. After going through numerous resources over the internet, I have the below conclusion and following it the confusion. </p>
  
  <p><strong>Conclusion 1:</strong>
  When we are talking about creating a socket as :</p>
  
  <pre><code>s = socket(AF_INET, SOCK_RAW, 0);
  </code></pre>
  
  <p>we are basically trying to create a raw socket. With a raw socket created this way one would be able to bypass TCP/UDP layer in the OSI stack. Meaning, when the packet is received by the application over this socket, the application would have the packet containing the network layer (layer 3) headers wrapping the layer 2 headers wrapping the actual data. So the application is free to process this packet, beyond layer 3, in anyway it wants to. </p>
  
  <p>Similarly, when sending a packet through this socket also, the application is free to handle the packet creation till layer 4 and then pass it down to layer 3, from which point on the kernel would handle things. </p>
  
  <p><strong>Conclusion 2:</strong> When we are talking about creating a socket as :</p>
  
  <pre><code>s = socket(AF_PACKET, SOCK_RAW, 0);
  </code></pre>
  
  <p>we are again trying to create a raw socket.  With a raw socket created this way one would be able to bypass all the layers of the OSI altogether. 
  A pure raw packet would be available to the user land application and it is free to do whatever it wants with that packet. A packets received over such a socket would have all the headers intact and the application would also have access to all of those headers. </p>
  
  <p>Similarly, when sending data over such a socket as well, the user application is the one that would have to handle everything with regards to the creation of the packet and the wrapping of the actual data with the headers of each layer before it is actually placed on the physical medium to be transmitted across. </p>
  
  <p><strong>Conclusion 3:</strong> When we are talking about creating a socket as :</p>
  
  <pre><code>s = socket(AF_PACKET, SOCK_DGRAM, 0);
  </code></pre>
  
  <p>we are again trying to create a raw socket. With a raw socket created this way one would be able to bypass data link layer (layer 2) in the OSI stack. Meaning, when a packet over such a socket is received by the user land application, data link layer header is removed from the packet. </p>
  
  <p>Similarly, while sending a packet through this socket, a suitable data link layer header is added to the packet, based on the information in the sockaddr_ll destination address.</p>
  
  <p>Now below are my queries/points of confusion:</p>
  
  <ol>
  <li>Are the conclusions that I have drawn above about raw sockets correct ?</li>
  <li>I did not quite clearly understand the <em>conclusion 3</em> above. Can someone please explain ? Like, does it mean that when the user land application receives a packet through this socket, it is only the data link layer headers that would have been handled by the kernel? And so the packet would be like the message wrapped with directly the layer 3 headers and wrapped subsequently by the layers above it?</li>
  <li>If the conclusions drawn above are correct, <em>conclusion 1</em> and <em>conclusion 2</em> still make sense. But if <em>conclusion 3</em> above (and the speculations around it in 2 above) are correct, when exactly would any application ever need to do that ?</li>
  </ol>
  
  <p>Some resources that I have referred to trying to understand the above:</p>
  
  <p><a href="https://docs.freebsd.org/44doc/psd/21.ipc/paper.pdf" rel="noreferrer">https://docs.freebsd.org/44doc/psd/21.ipc/paper.pdf</a></p>
  
  <p><a href="https://sock-raw.org/papers/sock_raw" rel="noreferrer">https://sock-raw.org/papers/sock_raw</a></p>
  
  <p><a href="https://www.quora.com/in/Whats-the-difference-between-the-AF_PACKET-and-AF_INET-in-python-socket" rel="noreferrer">https://www.quora.com/in/Whats-the-difference-between-the-AF_PACKET-and-AF_INET-in-python-socket
  </a></p>
  
  <p><a href="http://www.linuxcertif.com/man/7/PF_PACKET/" rel="noreferrer">http://www.linuxcertif.com/man/7/PF_PACKET/</a></p>
  
  <p><a href="http://opensourceforu.com/2015/03/a-guide-to-using-raw-sockets/" rel="noreferrer">http://opensourceforu.com/2015/03/a-guide-to-using-raw-sockets/</a></p>
  
  <p><a href="https://stackoverflow.com/questions/30780082/sock-raw-option-in-socket-system-call">&#39;SOCK_RAW&#39; option in &#39;socket&#39; system call</a></p>
  
  <p><a href="http://stevendanna.github.io/blog/2013/06/23/a-short-sock-raw-adventure/" rel="noreferrer">http://stevendanna.github.io/blog/2013/06/23/a-short-sock-raw-adventure/
  </a></p>
  
  <p><a href="https://www.intervalzero.com/library/RTX/WebHelp/Content/PROJECTS/Application%20Development/Understanding_Network/Using_RAW_Sockets.htm" rel="noreferrer">https://www.intervalzero.com/library/RTX/WebHelp/Content/PROJECTS/Application%20Development/Understanding_Network/Using_RAW_Sockets.htm</a></p>