title: Send custom ethernet packet using raw socket
body: <p>I searched how to send a raw ethernet packet using a custom ethertype but a lot of people talks abouts about tcp and udp protocols.
  I need to open a raw socket, take all the packets that have my custom ether type, read the payload and send back a packet with a different custom ether type.</p>
  <pre><code>func main() {
      //set promiscuos mode
      cmd := exec.Command(&quot;ifconfig&quot;, &quot;eth0&quot;, &quot;promisc&quot;)
      err := cmd.Run()
      if err != nil {
          fmt.Println(&quot;Error: &quot; + err.Error())
          return
      }
      //open raw socket with custom ethertype_1 and bind to interface
      fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, Htons(ETHER_TYPE_1))
      if err != nil {
          fmt.Println(&quot;Error: &quot; + err.Error())
          return
      }
      err = syscall.BindToDevice(fd, &quot;eth0&quot;)
      if err != nil {
          fmt.Println(&quot;Error: &quot; + err.Error())
          return
      }
      f := os.NewFile(uintptr(fd), fmt.Sprintf(&quot;fd %d&quot;, fd))
  
      for {
          buf := make([]byte, 1024)
          numRead, err := f.Read(buf)
          if err != nil {
              fmt.Println(err)
          }
          go ReadSocket(buf, numRead)
      }
  
  func ReadSocket(buf []byte, numRead int) {
      packet := BufToEthPacket(buf, numRead)
  
      fmt.Printf(&quot;Destination MAC: % X\n&quot;, packet.dstMac)
      fmt.Printf(&quot;Source MAC:      % X\n&quot;, packet.srcMac)
      fmt.Printf(&quot;ether type:      %X\n&quot;, packet.ethType)
      fmt.Printf(&quot;Payload:         % X\n&quot;, packet.payload)
  
      var myPacket EthPacket
      myPacket.srcMac = packet.dstMac
      myPacket.dstMac = packet.srcMac
      myPacket.ethType = ETHER_TYPE_2
      myPacket.payload = packet.payload
  
      var myBuf = EthPacketToBuf(myPacket)
  
      //open raw socket with custom ethertype_2 and bind to interface
      fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, Htons(ETHER_TYPE_2))
      if err != nil {
          fmt.Println(&quot;Error: &quot; + err.Error())
          return
      }
      err = syscall.BindToDevice(fd, &quot;eth0&quot;)
      if err != nil {
          fmt.Println(&quot;Error: &quot; + err.Error())
          return
      }
      f := os.NewFile(uintptr(fd), fmt.Sprintf(&quot;fd %d&quot;, fd))
  
      n, err := f.Write(myBuf)
      if err != nil {
          fmt.Println(&quot;Error: &quot; + err.Error())
          return
      }
      if n != numRead {
          fmt.Println(&quot;Error: byte length not equal&quot;)
          return
      }
  }
  </code></pre>
  <p>I received the packet but the <code>f.Write(myBuf)</code> return me the following error: <code>Error: write fd 5: no such device or address</code></p>
  <p>What I'm doing wrong?</p>