Beaglebone as a USB router (Share ethernet connection over USB)

I have found hundreds of tutorials over the internet on how to share your own computers internet connection over USB with the beaglebone. However I want to do the reverse. I want to have the Beaglebone connected to the PC with USB and to the router via ethernet. The PC is not connected to the router directly. I would then like the Beaglebone to share the internet it is getting from the router to the PC over USB.

So far I am doing the following:

sysctl net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT 

sudo iptables -A FORWARD -i usb0 -o eth0 -j ACCEPT

However I am stuck as to what else I am missing for this to be able to work.

Any help would be appreciated.

Perhaps try this?

1. edit /etc/sysctl.conf; add (or edit) the line:
   net.ipv4.ip_forward = 1
2. sysctl -p /etc/sysctl.conf

By default, the Linux kernel disables packet forwarding; you need to
turn it on.

Gah, never mind, just saw that was the first thing you ran. Chalk one
up for responding to an email whilst half asleep.

Try checking with tcpdump that the packets are being properly masqueraded.

Another option that might help debug this:
iptables -I FORWARD 1 -j LOG --log-prefix 'IP FORWARD:'

Then watch /var/log/syslog; you should see what's being passed through
the packet filter. You might also find iptables-restore and
iptables-save useful.

On Wed, 21 Feb 2018 04:45:37 +1000, Stuart Longland
<stuartl@longlandclan.id.au> declaimed the
following:

Another option that might help debug this:
iptables -I FORWARD 1 -j LOG --log-prefix 'IP FORWARD:'

Then watch /var/log/syslog; you should see what's being passed through
the packet filter. You might also find iptables-restore and
iptables-save useful.

  Just a silly thought... But nowhere in this thread have I seen anything
mention that the problem may be on the "PC" side. (If it was there, it
wasn't clearly segregated from the BBB stuff)

  Having the BBB acting as a gateway and forwarding packets to the PC
isn't going to go very far if said PC is not configured to use the USB AS
its network connection. Firewall, at the least.

Indeed, it is a valid argument: I don't know what OS the PC is running,
so it's hard to provide guidance. I assumed that end of the equation
was correctly configured.

Thanks for the suggestions. I’ll have a look at the suggested logging and see if that provides any clues.

In terms of the PC side, I am using Windows 10. Everything looks like it is ready to receive internet, but internet is never detected.

Looking here: Internet sharing - ArchWiki I can see there is some talk of DHCP on the client PC but my assumption is that this is always provided as 192.168.7.1 via the Ethernet over USB connection.

On Wed, 21 Feb 2018 02:46:02 -0800 (PST),
grantjohnbagwell@gmail.com declaimed the
following:

Looking here: Internet sharing - ArchWiki I can
see there is some talk of DHCP on the client PC but my assumption is that
this is always provided as 192.168.7.1 via the Ethernet over USB
connection.

  That IS what either the BBB provides to the rndis connection (or the
rndis driver provides a value to the BBB).

  I've not tried to set up one of mine for such sharing -- normally I
don't even use the USB connection for anything other than power when at my
computer; I've got a CAT-5 running from the switch to the top of the desk
for use with BBB and RPi boards.

  With the USB connected, and looking at connection details from the
network connections settings on Win10 (comparing my NIC to the USB device):

        NIC USB
        Realtek PCIe GBE Linux USB Ethernet/RNDIS Gadget
DHCP Enabled YES YES
IPv4 Address 192.168.2.109 192.168.7.1
IPv4 Gateway 192.168.2.1 <blank> <===========
IPv4 DHCP 192.168.2.1 192.168.7.2
IPv4 DNS Server 8.8.8.8 <blank> <===========
        192.168.0.1
        192.168.2.1

  My DNS servers are coming from:
    8.8.8.8 static configuration set up in my WiFi router
    192.168.0.1 my DSL adapter apparently sends itself to the WiFi
router as a DNS server
    192.168.2.1 is the router itself -- apparently it passes on the
DSL address, along with itself as a name server to Windows

  Lack of a Gateway and DNS servers might inhibit Windows from accessing
anything except the BBB -- since the gateway specifies the next machine
down the line to handle any non-direct IP addresses (192.168.7.1 can talk
to 192.168.7.2, but needs to send any other address to the gateway for
handling; lack of DNS means it can't even get an IP address from a domain
name).

  This might be part of the DHCP configuration on the BBB -- it needs to
provide the gateway and DNS server information to the Win10 box. OR you may
need to manually set those using the Windows network properties (and hope
they stay set between connections of the BBB). That might be the first
thing to try:

  Right click the network icon on the lower right of the task bar:
    Open Network & Internet Settings
  Change Adapter Options

  Right click the RNDIS device
    Properties

  Highlight
    Internet Protocol Version 4
    [Properties]

    * Use the following IP address
      192.168.7.1
      255.255.255.0
      192.168.7.2
    * Use the following DNS server addresses
      8.8.8.8 {Google's DNS}
      ?.?.?.? {pick one from your ISP connection}

  If manually setting the RNDIS device such doesn't make Windows find the
Internet, there may be some Linux stuff on the BBB -- again for setting
gateway and DNS information to Windows and for forwarding packets.

Ah that is very helpful indeed. I didn’t really consider such a basic setting wouldn’t be included but then I guess that is really not what the USB connection is designed for.

I have managed to get this to work with almost your suggestion. Instead of using Google’s or my ISP DNS I instead just set this value to the BBB itself. So:

IP: 192.168.7.1
Subnet: 255.255.255.0
Gateway: 192.168.7.2

DNS Server: 192.168.7.2

et voila… combined with the other iptables commands I now have internet being supplied through my beaglebone.

Now I just have to try and tinker with the USB/Ethernet settings on the BBB to see if I can automatically set that DNS setting. Probably a tricky task, but at least the concept works.

Thanks again to all responders. I’m really happy this is working.