Host setup for using PocketBeagle

PocketBeagle is the best device to run OpenOCD with BBG-SWD. It can run as a USB device which offers multiple interfaces: USB Network, USB modem to allow access by /dev/ttyACM0, and USB Mass Storage to offer informaiton as a disk.

Tethering PocketBeagle to my PC with Debian, I need some configuration to enable internet access from PocketBeagle. Two things are needed: Routing with masquerading, and DNS.

First, to give a specific name to the network interface on host PC, I need to have a udev rules of /etc/udev/rules.d/72-pbgl.rules:

# Identify PocketBeagle device.
ACTION=="add", SUBSYSTEMS=="usb", \
ATTRS{manufacturer}=="BeagleBoard.org", ATTRS{product}=="PocketBeagle" \
    TAG="PocketBeagle"

# On the device, we search the network interface with RNDIS, then
# assign the name.
ACTION=="add", NAME=="", SUBSYSTEM=="net", TAG=="PocketBeagle", \
    ATTRS{interface}=="RNDIS Communications Control" \
    NAME="pbgl0"

The manual of systemd.link(5) says that we can have a link configuration file under /etc/systemd/network to change interface name, it is not true for USB device. In such a case, udev has precedence. With system's 73-usb-net-by-mac.rules, having a link configuration under /etc/systemd/network has no effect, unfortunately. Thus, I put /etc/udev/rules.d/72-pbgl.rules.

Second, having a specific network interface name, I write an ifupdown hook script of /etc/network/if-up.d/pbgl-config:

#! /bin/sh

OUT_IF=eno1

[ "$IFACE" = "pbgl0" ] || exit 0
[ "$ADDRFAM" = "inet" ] || exit 0

do_start () {
    sysctl net.ipv4.ip_forward=1
    iptables --table nat --append POSTROUTING \
             --out-interface $OUT_IF -j MASQUERADE
    iptables --append FORWARD --in-interface $IFACE -j ACCEPT
    dnsmasq -C /etc/network/dnsmasq-for-pbgl.conf \
            -x /var/run/dnsmasq-for-pbgl.pid      \
            -h -z -i $IFACE -I lo -2 $IFACE
}

do_stop () {
    kill -TERM $(cat /var/run/dnsmasq-for-pbgl.pid) \
    && rm -f /var/run/dnsmasq-for-pbgl.pid
    sysctl net.ipv4.ip_forward=0
    iptables --table nat --delete POSTROUTING \
             --out-interface $OUT_IF -j MASQUERADE
    iptables --delete FORWARD --in-interface $IFACE -j ACCEPT
}

case "$MODE" in 
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    *)
        echo "Unknown mode: \"$MODE\"" >2
        exit 1
        ;;
esac

exit 0

In the script, standard ethernet interface of PC is specified by the line "OUT_IF=eno1". The "up" hook configures routing packets from/to PocketBeagle with masquerading and starts dnsmasq. (You should change OUT_IF with your network interface.)

Here, my host PC offers DNS service to PocketBeagle with dnsmasq.

Then, I create a symbolic link to configure it as a "down" hook, and create an empty configuration for dnsmasq.

# cd /etc/network/if-post-down.d
# ln -s ../if-up.d/pbgl-config .
# echo '# Empty.  All params are by command-line args.' > \
       /etc/network/dnsmasq-for-pbgl.conf

Thirdly, I install dnsmasq by apt install dnsmasq but disable it running as a daemon by typing:

# systemctl disable dnsmasq

Lastly, while I am using NetworkManager, I specify the interface name in the configuration of /etc/NetworkManager/system-connections/PocketBeagle. It's like:

[connection]
interface-name=pbgl0

With the configuration, it runs the interface as IPv4 with DHCP from PocketBeagle.

That is, when PocketBeagle is connected, after 30sec or so, it enables USB interface. Then, by DHCP service of PocketBeagle, the interface gets the IPv4 address assigned. Detecting the network interface, host PC enables the routing and starts DNS service by dnsmasq for PocketBeagle.