script: support IPv6 in simhostroute.sh

1. Change IP address format to addr/prefix, to be compatible with both IPv4/IPv6.
   - When adding address in CIDR type, netmask/route will be automatically added.
2. Since route of whole subnet is added automatically, not specifying NuttX's IP any more.
   - Multiple NuttX simulators (with IP 10.0.1.x) attached to same bridge can surf the net at same time.
3. NAT66 is used to make sure it works even if host has only one IPv6 address.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2023-01-04 11:56:59 +08:00 committed by Xiang Xiao
parent c02fea36d8
commit 8fb98bc9d1
3 changed files with 56 additions and 32 deletions

View File

@ -59,7 +59,9 @@ On Linux:
On the NuttX Simulator: On the NuttX Simulator:
nsh> ifconfig eth0 10.0.1.2 nsh> # replace or omit dns if needed, IPv6 line is optional
nsh> ifconfig eth0 inet6 fc00::2/112 dns 2001:4860:4860::8888
nsh> ifconfig eth0 10.0.1.2 dns 8.8.8.8
nsh> ifup eth0 nsh> ifup eth0
On Linux: On Linux:

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
#**************************************************************************** #****************************************************************************
# tools/simhostroute.sh # tools/netusb.sh
# #
# Licensed to the Apache Software Foundation (ASF) under one or more # Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with # contributor license agreements. See the NOTICE file distributed with

View File

@ -40,43 +40,65 @@ IF_HOST=$1
STATUS=$2 STATUS=$2
IF_BRIDGE=nuttx0 IF_BRIDGE=nuttx0
IP_NET="10.0.1.0/24" IPv4_HOST="10.0.1.1/24"
IP_NETMASK="255.255.255.0" IPv6_HOST="fc00::1/112"
IP_BROADCAST="10.0.0.255" IPv6_ENABLE=true
IP_HOST="10.0.1.1"
IP_NUTTX="10.0.1.2"
if [ "$STATUS" == "on" ]; then call_all() {
ip link add $IF_BRIDGE type bridge FUNC=$1
ifconfig $IF_BRIDGE $IP_HOST
ifconfig $IF_BRIDGE up
ifconfig -a
ip addr add $IP_HOST dev $IF_BRIDGE
ifconfig $IF_BRIDGE netmask $IP_NETMASK
ip route delete $IP_NET
ip route add $IP_NET dev $IF_BRIDGE src $IP_HOST
ip route add $IP_NUTTX/32 dev $IF_BRIDGE src $IP_HOST
# nat to allow NuttX to access the internet IPTABLES="iptables"
iptables -t nat -A POSTROUTING -o $IF_HOST -j MASQUERADE IP_HOST=$IPv4_HOST
iptables -A FORWARD -i $IF_HOST -o $IF_BRIDGE -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $IF_BRIDGE -o $IF_HOST -j ACCEPT # call function
$FUNC
# enable forward to make sure nat works # enable forward to make sure nat works
sysctl -w net.ipv4.ip_forward=1 sysctl -w net.ipv4.ip_forward=1
ip route show if [ "$IPv6_ENABLE" == "true" ]; then
else IPTABLES="ip6tables"
ip route delete $IP_NET IP_HOST=$IPv6_HOST
ip route delete $IP_NUTTX/32
# call function
$FUNC
# enable forward to make sure nat works
sysctl -w net.ipv6.conf.all.forwarding=1
fi
}
net_on() {
# add address to the bridge, with CIDR specified, netmask/route will be automatically added.
ip addr add $IP_HOST dev $IF_BRIDGE
# nat to allow NuttX to access the internet
$IPTABLES -t nat -A POSTROUTING -o $IF_HOST -j MASQUERADE
$IPTABLES -A FORWARD -i $IF_HOST -o $IF_BRIDGE -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPTABLES -A FORWARD -i $IF_BRIDGE -o $IF_HOST -j ACCEPT
}
net_off() {
ip addr del $IP_HOST dev $IF_BRIDGE
# delete nat rules to clean up # delete nat rules to clean up
iptables -t nat -D POSTROUTING -o $IF_HOST -j MASQUERADE $IPTABLES -t nat -D POSTROUTING -o $IF_HOST -j MASQUERADE
iptables -D FORWARD -i $IF_HOST -o $IF_BRIDGE -m state --state RELATED,ESTABLISHED -j ACCEPT $IPTABLES -D FORWARD -i $IF_HOST -o $IF_BRIDGE -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i $IF_BRIDGE -o $IF_HOST -j ACCEPT $IPTABLES -D FORWARD -i $IF_BRIDGE -o $IF_HOST -j ACCEPT
}
# remove all configs first to avoid double configure
call_all net_off
if [ "$STATUS" == "on" ]; then
ip link add $IF_BRIDGE type bridge
ifconfig $IF_BRIDGE up
ifconfig -a
call_all net_on
else
ip link delete $IF_BRIDGE type bridge ip link delete $IF_BRIDGE type bridge
ip route show
fi fi
ip route show
ip -6 route show