title: Why does SOCKS5 require to relay UDP over UDP?
body: <p>The <a href="https://en.wikipedia.org/wiki/SOCKS#SOCKS5" rel="nofollow noreferrer">SOCKS5</a> protocol, described by <a href="https://www.rfc-editor.org/rfc/rfc1928" rel="nofollow noreferrer">RFC1928</a> provides support for UDP.</p>
  <p>To summarize, a client wishing to relay UDP packets through a SOCKS5 server must, at least:</p>
  <ul>
  <li>open a TCP connection to the SOCKS5 server;</li>
  <li>send a <code>UDP ASSOCIATE</code> request (cf <a href="https://www.rfc-editor.org/rfc/rfc1928#section-4" rel="nofollow noreferrer">section 4</a>);</li>
  <li>receive from the server the address and port where it must send UDP packets to be relayed;</li>
  <li>send datagrams (UDP) to that address, encapsulated with some headers (cf <a href="https://www.rfc-editor.org/rfc/rfc1928#section-7" rel="nofollow noreferrer">section 7</a>).</li>
  </ul>
  <p>Here are some relevant quotations, from <a href="https://www.rfc-editor.org/rfc/rfc1928#section-6" rel="nofollow noreferrer">section 6</a>:</p>
  <blockquote>
  <p>A UDP association terminates when the TCP connection that the UDP ASSOCIATE request arrived on terminates.</p>
  <p>In the reply to a UDP ASSOCIATE request, the BND.PORT and BND.ADDR fields indicate the port number/address where the client MUST send UDP request messages to be relayed.</p>
  </blockquote>
  <p>and <a href="https://www.rfc-editor.org/rfc/rfc1928#section-7" rel="nofollow noreferrer">section 7</a>:</p>
  <blockquote>
  <p>A UDP-based client MUST send its datagrams to the UDP relay server at the UDP port indicated by BND.PORT in the reply to the UDP ASSOCIATE request.</p>
  </blockquote>
  <p>Why so much complexity? <strong>Why not just sending UDP packets in the pre-existing TCP connection?</strong></p>
  <p><strong>EDIT:</strong> To <a href="https://stackoverflow.com/questions/41967217/why-socks5-requires-to-relay-udp-over-udp#comment71119123_41967217">clarify</a>, I am expecting the SOCKS proxy to receive UDP packets over a TCP stream and then transmit them to the target using actual UDP. And then receive UDP packets from the target and send them back down the TCP stream.</p>
  <hr />
  <p>Here is some context.</p>
  <p>My goal is to implement reverse tethering, so that an Android device may use the internet connection of the computer it is plugged on, without requiring <em>root</em> access both on the device and the computer (<a href="https://github.com/vvviperrr/SimpleRT" rel="nofollow noreferrer">SimpleRT</a> works but requires <em>root</em> access on the computer).</p>
  <p>My first idea was to start a simple SOCKS5 server with <code>ssh -D</code> on the computer, so that I only needed to implement the client. The packets would be transmitted from the device to the computer over <code>adb</code>, thanks to <em>remote port forwarding</em> provided by <code>adb reverse</code>…</p>
  <p>Unfortunately, <em>OpenSSH</em> SOCKS server <a href="http://lists.mindrot.org/pipermail/openssh-unix-dev/2017-January/035662.html" rel="nofollow noreferrer">does not support UDP</a>. But it was just a limitation from the implementation, I could have used another SOCKS server instead.</p>
  <p>However, <code>adb reverse</code> does not support UDP forwarding either.</p>
  <p>Hence my question about SOCKS5 protocol.</p>
  <p>I am currently writing a PoC implementing my own (simple) protocol over TCP, which is able to relay UDP packets, but I am disappointed not to be able to use a standard protocol (and benefit from existing implementations).</p>