From 8fb98bc9d1bc70f9d3fb82bc2b7d854d83430351 Mon Sep 17 00:00:00 2001 From: Zhe Weng Date: Wed, 4 Jan 2023 11:56:59 +0800 Subject: [PATCH] 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 --- boards/sim/sim/sim/NETWORK-LINUX.txt | 4 +- tools/netusb.sh | 2 +- tools/simhostroute.sh | 82 ++++++++++++++++++---------- 3 files changed, 56 insertions(+), 32 deletions(-) diff --git a/boards/sim/sim/sim/NETWORK-LINUX.txt b/boards/sim/sim/sim/NETWORK-LINUX.txt index 384d795540..5c3f4dd127 100644 --- a/boards/sim/sim/sim/NETWORK-LINUX.txt +++ b/boards/sim/sim/sim/NETWORK-LINUX.txt @@ -59,7 +59,9 @@ On Linux: 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 On Linux: diff --git a/tools/netusb.sh b/tools/netusb.sh index 1d9e959c93..07166b66a0 100755 --- a/tools/netusb.sh +++ b/tools/netusb.sh @@ -1,7 +1,7 @@ #!/bin/bash #**************************************************************************** -# tools/simhostroute.sh +# tools/netusb.sh # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with diff --git a/tools/simhostroute.sh b/tools/simhostroute.sh index 1ab55c07a6..e612d045ad 100755 --- a/tools/simhostroute.sh +++ b/tools/simhostroute.sh @@ -40,43 +40,65 @@ IF_HOST=$1 STATUS=$2 IF_BRIDGE=nuttx0 -IP_NET="10.0.1.0/24" -IP_NETMASK="255.255.255.0" -IP_BROADCAST="10.0.0.255" -IP_HOST="10.0.1.1" -IP_NUTTX="10.0.1.2" +IPv4_HOST="10.0.1.1/24" +IPv6_HOST="fc00::1/112" +IPv6_ENABLE=true + +call_all() { + FUNC=$1 + + IPTABLES="iptables" + IP_HOST=$IPv4_HOST + + # call function + $FUNC + + # enable forward to make sure nat works + sysctl -w net.ipv4.ip_forward=1 + + if [ "$IPv6_ENABLE" == "true" ]; then + IPTABLES="ip6tables" + IP_HOST=$IPv6_HOST + + # 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 + $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_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 $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 -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 - - # enable forward to make sure nat works - sysctl -w net.ipv4.ip_forward=1 - - ip route show + call_all net_on else - ip route delete $IP_NET - ip route delete $IP_NUTTX/32 - - # delete nat rules to clean up - 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_BRIDGE -o $IF_HOST -j ACCEPT - ip link delete $IF_BRIDGE type bridge - - ip route show fi +ip route show +ip -6 route show