title: linux raw ethernet socket bind to specific protocol
body: <p>I'm writing code to send raw Ethernet frames between two Linux boxes. To test this I just want to get a simple client-send and server-receive.</p>
  
  <p>I have the client correctly making packets (I can see them using a packet sniffer).</p>
  
  <p>On the server side I initialize the socket like so:</p>
  
  <pre><code>fd = socket(PF_PACKET, SOCK_RAW, htons(MY_ETH_PROTOCOL));
  </code></pre>
  
  <p>where <code>MY_ETH_PROTOCOL</code> is a 2 byte constant I use as an ethertype so I don't hear extraneous network traffic.</p>
  
  <p>when I bind this socket to my interface I must pass it a protocol again in the socket_addr struct:
      <code>socket_address.sll_protocol = htons(MY_ETH_PROTOCOL);</code><br>
  If I compile and run the code like this then it fails. My server does not see the packet. However if I change the code like so:<br>
      <code>socket_address.sll_protocol = htons(ETH_P_ALL);</code><br>
  The server then can see the packet sent from the client (as well as many other packets) so I have to do some checking of the packet to see that it matches <code>MY_ETH_PROTOCOL</code>.</p>
  
  <p>But I don't want my server to hear traffic that isn't being sent on the specified protocol so this isn't a solution. How do I do this?</p>