nuttx/tools/netusb.sh
Zhe Weng 8fb98bc9d1 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>
2023-01-05 23:36:43 +08:00

72 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
#****************************************************************************
# tools/netusb.sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
#****************************************************************************
# USB Ethernet Gadget interface helper script - sets up nuttx to access
# the internet via a host route and IP Tables.
IP_NET="10.0.0.0/24"
IP_NETMASK="255.255.255.0"
IP_BROADCAST="10.0.0.255"
IP_HOST="10.0.0.1"
IP_NUTTX="10.0.0.2"
if [ $# != 3 ]; then
echo "Usage: $0 <main-interface> <usb-net-interface> <on|off>"
exit 1
fi
IF_HOST=$1
IF_USB=$2
STATUS=$3
net_off() {
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_USB -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i $IF_USB -o $IF_HOST -j ACCEPT
ip route show
ifconfig $IF_USB down
}
if [ "$STATUS" == "on" ]; then
net_off
ifconfig $IF_USB up
ifconfig -a
ifconfig $IF_USB add $IP_HOST
ifconfig $IF_USB:0 broadcast $IP_BROADCAST netmask $IP_NETMASK
ip route add $IP_NET dev $IF_USB src $IP_HOST
ip route add $IP_NUTTX/32 dev $IF_USB 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_USB -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $IF_USB -o $IF_HOST -j ACCEPT
ip route show
else
net_off
fi