title: Why does the Linux kernel have `struct sock` and `struct socket`?
body: <p>This questions was <a href="https://www.linuxquestions.org/questions/programming-9/differences-between-struct-sock-and-struct-socket-715386/" rel="noreferrer">asked before</a> on the Internet, but I couldn't find a good answer.</p>
  
  <p>The Linux kernel networking stack features two structures:</p>
  
  <ul>
  <li><a href="http://elixir.free-electrons.com/linux/v4.13/source/include/linux/net.h#L111" rel="noreferrer"><code>struct socket</code></a>, generally stored in a variable <code>sock</code></li>
  <li><a href="http://elixir.free-electrons.com/linux/v4.13/source/include/net/sock.h#L316" rel="noreferrer"><code>struct sock</code></a>, generally stored in a variable <code>sk</code></li>
  </ul>
  
  <p>The two structures are essentially linked, but seem to have slightly different lifetimes. One can find an <code>sk</code> via <code>sock-&gt;sk</code>, or find a <code>sock</code> via <code>sk-&gt;sk_socket</code>.</p>
  
  <p>Why are there two structures to store information about sockets? Assuming I need to add a new field, when would I add it to <code>struct socket</code> and when to <code>struct sock</code>?</p>
  
  <p><strong>UPDATE</strong>: Please note that I refer to <code>struct socket</code> in <code>include/linux/net.h</code> inside the Linux source code, which is meant for kernel code only, and <strong>not</strong> <code>/usr/include/sys/socket.h</code> which is meant for userland.</p>