Skip to content

Commit

Permalink
Adding scripts that provide access MilkV to Internet throw USB RNDIS …
Browse files Browse the repository at this point in the history
…connection
  • Loading branch information
nzdorovylo committed Jan 11, 2024
1 parent de23f43 commit 8bbe519
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 1 deletion.
67 changes: 67 additions & 0 deletions MilkV-Duo-internet-throw-USB--MilkV.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#! /bin/bash

#### This script provide access BETWEEN Milk-V Duo (MilkV) that connected via USB to host device (e.g. RPi 4) AND private/public Network without RJ45 wires, directly just by USB connection
## You should run two scripts MilkV-Duo-internet-throw-USB--MilkV.sh and MilkV-Duo-internet-throw-USB--RPi4.sh, for one by each device: this script (MilkV-Duo-internet-throw-USB--MilkV.sh) on MilkV AND (MilkV-Duo-internet-throw-USB--RPi4.sh) on RPi4
## Scheme
## MilkV <--USB-- RPi4 <--RJ45/WiFi-- Internet --> PC
##
### Works on Linux Core (MilkV official image https://milkv.io/docs/duo/resources/image-sdk / https://github.com/milkv-duo/duo-buildroot-sdk/releases/)


### Preconditions:
## USB host should have static IP 192.168.42.21


### Add rc.local file
echo -e "#!/bin/sh
echo Adding route for 192.168.42.21
sleep 2
route add default gw 192.168.42.21
echo Add default finished
exit 0" >> /etc/rc.local
chmod +x /etc/rc.local

##
echo -e "
### Run user scripts
::sysinit:/etc/rc.local" >> /etc/inittab

## Instead above movigs with /etc/rc.local and /etc/inittab you can call below script, but it works juft until you not reboot the board
#route add default gw 192.168.42.21


### Add nameservers
echo -e "nameserver 8.8.8.8" >> /etc/resolv.conf.head


### Finish
echo Configuring Network thow USB on Milk-V Duo finished


### Reboot
echo Rebooting...
sleep 2
reboot
exit 0


#########################################################
### Now you albe reach MilkV via SSH:
## directly throw USB port - by usb0 interface
## ssh -p 22 [email protected]
## or throw another configured host with external IP like 10.0.0.11, but need previously run shell script to configure host device
##ssh -p 2222 [email protected]
##
### Test Internet connection:
## ping 8.8.8.8 -c 2
#########################################################


### To execute this script run on MilkV:
#su root ./MilkV-Duo-internet-throw-USB--MilkV.sh



### TODO: Set static MAC address, that will be same after rebooting
### TODO: Extract user variable data to bash variables. it can be IPs 192.168.42.1 / 192.168.42.21, usernames root / milkv
94 changes: 94 additions & 0 deletions MilkV-Duo-internet-throw-USB--RPi4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#! /bin/bash

#### This script provide access BETWEEN Milk-V Duo (MilkV) that connected via USB to host device (e.g. RPi 4) AND private/public Network without RJ45 wires, directly just by USB connection
## You should run two scripts MilkV-Duo-internet-throw-USB--MilkV.sh and MilkV-Duo-internet-throw-USB--RPi4.sh, for one by each device: this script (MilkV-Duo-internet-throw-USB--RPi4.sh) on RPi4 AND (MilkV-Duo-internet-throw-USB--MilkV.sh) on MilkV
## Scheme
## MilkV <--USB-- RPi4 <--RJ45/WiFi-- Internet --> PC
##
### Works on Debian/Ubuntu


### Preconditions:
## This script tested on Paspberry Pi 4 over Raspberry Pi OS
## RPi4 have two interfaces eth0 - own and usb0 - MilkV RNDIS
## RPi4 can have address like 10.0.0.11, but usb0 192.168.41.1 so this script works for different subnets and address spaces


### Network interfaces Forwarding btw eth0->usb0

sudo sysctl net.ipv4.ip_forward
sudo sysctl -w net.ipv4.ip_forward=1

#############################################################################
## replace #net.ipv4.ip_forward=1 -> net.ipv4.ip_forward=1
# net.ipv4.ip_forward=1 // uncomment in /etc/sysctl.conf
# #net.ipv4.ip_forward=1 -> net.ipv4.ip_forward=1

#echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
#sed 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf

sysctl_conf=/etc/sysctl.conf
sysctl_conf_row_commented=#net.ipv4.ip_forward=1
sysctl_conf_row_uncommented=net.ipv4.ip_forward=1

grep -q sysctl_conf_row_commented sysctl_conf && (sed -i 's/sysctl_conf_row_commented/sysctl_conf_row_uncommented/' sysctl_conf || echo "sysctl_conf_row_uncommented" >> sysctl_conf)
#############################################################################

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i usb0 -o eth0 -j ACCEPT

sudo apt-get update
#sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get -y install iptables-persistent
sudo netfilter-persistent save


### Make static IP for Rpi4 with address 192.168.42.21
echo -e "
interface usb0
static ip_address=192.168.42.21/24
static routers=192.168.42.1
static domain_name_servers=192.168.42.1" >> /etc/dhcpcd.conf

sudo systemctl restart dhcpcd


### IP forwarding for 2222 port.
## So you can connect to MiklV via SSH throw RPi:
## ssh -p 2222 [email protected]
## where 10.0.0.11 is RPi4 address
# Enable IP forwarding
sudo sysctl -w net.ipv4.ip_forward=1

# Set up port forwarding
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 2222 -j DNAT --to-destination 192.168.42.1:22
sudo iptables -A FORWARD -i eth0 -o usb0 -p tcp --dport 22 -d 192.168.42.1 -j ACCEPT
sudo iptables -A FORWARD -i usb0 -o eth0 -p tcp --sport 22 -s 192.168.42.1 -j ACCEPT


### Finish
echo Configuring Network thow USB on RPi4 for Milk-V Duo finished


### Reboot
#echo Rebooting...
#sleep 2
#reboot
exit 0


#########################################################
### Now you albe reach MilkV via SSH:
## directly throw USB port - by usb0 interface
## ssh -p 22 [email protected]
## or throw another configured host (e.g. RPi4) with external IP like 10.0.0.11, but need previously run shell script to configure MilkV device
##ssh -p 2222 [email protected]
#########################################################


### To execute this script run on RPi4 (replace root to your useraccount):
#su root ./MilkV-Duo-internet-throw-USB--RPi4.sh


### TODO: Extract user variable data to bash variables. it can be ports 2222, IPs 192.168.42.1 / 10.0.0.11, usernames root / milkv
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# Milk-V-Duo.-USB-Internet

# Milk-V Duo. USB Internet
Milk-V Duo. Access to Internet throw USB RNDIS connection to host machine

This script provide access *BETWEEN* **Milk-V Duo** (MilkV) that connected via USB to host device (e.g. RPi 4) *AND* **private/public Network without RJ45 wires**, directly just by USB connection

You should run two scripts **MilkV-Duo-internet-throw-USB--MilkV.sh** and **MilkV-Duo-internet-throw-USB--RPi4.sh**, for one by each device: this script (MilkV-Duo-internet-throw-USB--MilkV.sh) on MilkV AND (MilkV-Duo-internet-throw-USB--RPi4.sh) on RPi4

##
* Scheme
MilkV <--USB-- RPi4 <--RJ45/WiFi-- Internet --> PC

##
* Works on Linux Core (MilkV official image https://milkv.io/docs/duo/resources/image-sdk
https://github.com/milkv-duo/duo-buildroot-sdk/releases/)

##
**Preconditions**
This is not requied, but preferably to connect Milk-V Duo to host device by USB Type-C and

**Configurating:**
On Milk-V Duo run script: `su root ./MilkV-Duo-internet-throw-USB--MilkV.sh` - with autoreboot

On USB host device (e.g. RPi4) run script: `MilkV-Duo-internet-throw-USB--RPi4.sh` - it`s advisable to reboot after running the scripts.

**Testing:**

- From USB host device (e.g. RPi4):
`ssh -p 22 [email protected]` - test SSH connection
`sudo iptables -S | grep usb0 -i` - list IP / port forwadring

connect via UART interface:
> \> `ls /dev/ttyUSB*`
> \> `sudo apt-get install minicom`
> \> `sudo minicom -D /dev/ttyUSB0`
- From Internet / Local Network: `ssh -p 2222 [email protected]`, where 10.0.0.11 - is USB host device IP address

- From Milk-V Duo:
`ping 8.8.8.8 -c 2`
or `wgetcontent=$(wget api.myip.com -q -O -) && echo $wgetcontent`

- On Both machines (Milk-V Duo & RPi4):
`netstat -rn` or `route` - list routing table
`ifconfig -a` - check Network Interfaces & devices & PI & MAC & status
`ip a show usb0`

- How to copy file via SSH to Milc-V Duo: `scp test1.txt [email protected]:~/`

##
TODO:
- Set static MAC address, that will be same after rebooting
- Extract user variable data to bash variables

0 comments on commit 8bbe519

Please sign in to comment.