incubator-nuttx/tools/simhostroute.sh

105 lines
2.8 KiB
Bash
Raw Normal View History

simulator network host route helper script and docs Squashed commit of the following: commit 685951b5385062035ac558df1112353c9441c910 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:33:00 2020 -0800 fixed typo in readme commit ad1d3289b48af0de3095e3f365429017e57278b3 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:07:48 2020 -0800 simulator host route helper script and docs Squashed commit of the following: commit cf5cddcf55a155303cb5abb1aa026f6dcaf369ca Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:50:40 2020 -0800 syslog on console to fix compile error commit def1bb73fcfbc8b115c55d9f4544b97d583807f2 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:46:13 2020 -0800 formatting as per PR feedback; removed ping commit b179fd8831b77fbbe85527a4ab3161e1f1ca1e43 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:45:49 2020 -0800 formatting as per PR feedback commit e3280bede9798d9a00b118e126c02ceab497e33a Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 13:56:50 2020 -0800 add simhostroute.sh description to readme commit 09a6b0ca3bacf005c2a79102141ae8cc3eb91849 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:38:40 2020 -0800 fixed error in simhostroute.sh usage docs commit d838582119d43ee2002ce3808051c82b23e98c58 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:28:35 2020 -0800 added tcpblaster defconfig and updated docs commit af3d2d6591f12d1127027fdd363858052094e624 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:21:29 2020 -0800 added info about capabilities and running as root commit 6359cfdfedefc818b169455401942d3d33a59f41 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:20:42 2020 -0800 updated network linux readme commit 47feb08aa74e4b1fc6c802567bc777d31e7c9a83 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:09:04 2020 -0800 initial addition
2020-02-25 08:34:35 +08:00
#!/bin/bash
#****************************************************************************
# tools/simhostroute.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.
#
#****************************************************************************
# Helper script to set up host route to NuttX simulator
# and set up IP Tables to allow it to access the
simulator network host route helper script and docs Squashed commit of the following: commit 685951b5385062035ac558df1112353c9441c910 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:33:00 2020 -0800 fixed typo in readme commit ad1d3289b48af0de3095e3f365429017e57278b3 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:07:48 2020 -0800 simulator host route helper script and docs Squashed commit of the following: commit cf5cddcf55a155303cb5abb1aa026f6dcaf369ca Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:50:40 2020 -0800 syslog on console to fix compile error commit def1bb73fcfbc8b115c55d9f4544b97d583807f2 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:46:13 2020 -0800 formatting as per PR feedback; removed ping commit b179fd8831b77fbbe85527a4ab3161e1f1ca1e43 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:45:49 2020 -0800 formatting as per PR feedback commit e3280bede9798d9a00b118e126c02ceab497e33a Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 13:56:50 2020 -0800 add simhostroute.sh description to readme commit 09a6b0ca3bacf005c2a79102141ae8cc3eb91849 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:38:40 2020 -0800 fixed error in simhostroute.sh usage docs commit d838582119d43ee2002ce3808051c82b23e98c58 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:28:35 2020 -0800 added tcpblaster defconfig and updated docs commit af3d2d6591f12d1127027fdd363858052094e624 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:21:29 2020 -0800 added info about capabilities and running as root commit 6359cfdfedefc818b169455401942d3d33a59f41 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:20:42 2020 -0800 updated network linux readme commit 47feb08aa74e4b1fc6c802567bc777d31e7c9a83 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:09:04 2020 -0800 initial addition
2020-02-25 08:34:35 +08:00
# internet.
#
# This script needs to be run as root.
#
# Note that on Linux you may also have to set kernel capabilities
# on the nuttx executable to allow NuttX to access the tap device:
#
# sudo setcap cap_net_admin+ep ./nuttx
if [ $# != 2 ]; then
echo "Usage: $0 <interface> <on|off>"
exit 1
fi
IF_HOST=$1
STATUS=$2
IF_BRIDGE=nuttx0
IPv4_HOST="10.0.1.1/24"
IPv6_HOST="fc00::1/112"
IPv6_ENABLE=true
simulator network host route helper script and docs Squashed commit of the following: commit 685951b5385062035ac558df1112353c9441c910 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:33:00 2020 -0800 fixed typo in readme commit ad1d3289b48af0de3095e3f365429017e57278b3 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:07:48 2020 -0800 simulator host route helper script and docs Squashed commit of the following: commit cf5cddcf55a155303cb5abb1aa026f6dcaf369ca Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:50:40 2020 -0800 syslog on console to fix compile error commit def1bb73fcfbc8b115c55d9f4544b97d583807f2 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:46:13 2020 -0800 formatting as per PR feedback; removed ping commit b179fd8831b77fbbe85527a4ab3161e1f1ca1e43 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:45:49 2020 -0800 formatting as per PR feedback commit e3280bede9798d9a00b118e126c02ceab497e33a Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 13:56:50 2020 -0800 add simhostroute.sh description to readme commit 09a6b0ca3bacf005c2a79102141ae8cc3eb91849 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:38:40 2020 -0800 fixed error in simhostroute.sh usage docs commit d838582119d43ee2002ce3808051c82b23e98c58 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:28:35 2020 -0800 added tcpblaster defconfig and updated docs commit af3d2d6591f12d1127027fdd363858052094e624 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:21:29 2020 -0800 added info about capabilities and running as root commit 6359cfdfedefc818b169455401942d3d33a59f41 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:20:42 2020 -0800 updated network linux readme commit 47feb08aa74e4b1fc6c802567bc777d31e7c9a83 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:09:04 2020 -0800 initial addition
2020-02-25 08:34:35 +08:00
call_all() {
FUNC=$1
IPTABLES="iptables"
IP_HOST=$IPv4_HOST
simulator network host route helper script and docs Squashed commit of the following: commit 685951b5385062035ac558df1112353c9441c910 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:33:00 2020 -0800 fixed typo in readme commit ad1d3289b48af0de3095e3f365429017e57278b3 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:07:48 2020 -0800 simulator host route helper script and docs Squashed commit of the following: commit cf5cddcf55a155303cb5abb1aa026f6dcaf369ca Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:50:40 2020 -0800 syslog on console to fix compile error commit def1bb73fcfbc8b115c55d9f4544b97d583807f2 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:46:13 2020 -0800 formatting as per PR feedback; removed ping commit b179fd8831b77fbbe85527a4ab3161e1f1ca1e43 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:45:49 2020 -0800 formatting as per PR feedback commit e3280bede9798d9a00b118e126c02ceab497e33a Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 13:56:50 2020 -0800 add simhostroute.sh description to readme commit 09a6b0ca3bacf005c2a79102141ae8cc3eb91849 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:38:40 2020 -0800 fixed error in simhostroute.sh usage docs commit d838582119d43ee2002ce3808051c82b23e98c58 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:28:35 2020 -0800 added tcpblaster defconfig and updated docs commit af3d2d6591f12d1127027fdd363858052094e624 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:21:29 2020 -0800 added info about capabilities and running as root commit 6359cfdfedefc818b169455401942d3d33a59f41 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:20:42 2020 -0800 updated network linux readme commit 47feb08aa74e4b1fc6c802567bc777d31e7c9a83 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:09:04 2020 -0800 initial addition
2020-02-25 08:34:35 +08:00
# 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
simulator network host route helper script and docs Squashed commit of the following: commit 685951b5385062035ac558df1112353c9441c910 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:33:00 2020 -0800 fixed typo in readme commit ad1d3289b48af0de3095e3f365429017e57278b3 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:07:48 2020 -0800 simulator host route helper script and docs Squashed commit of the following: commit cf5cddcf55a155303cb5abb1aa026f6dcaf369ca Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:50:40 2020 -0800 syslog on console to fix compile error commit def1bb73fcfbc8b115c55d9f4544b97d583807f2 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:46:13 2020 -0800 formatting as per PR feedback; removed ping commit b179fd8831b77fbbe85527a4ab3161e1f1ca1e43 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:45:49 2020 -0800 formatting as per PR feedback commit e3280bede9798d9a00b118e126c02ceab497e33a Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 13:56:50 2020 -0800 add simhostroute.sh description to readme commit 09a6b0ca3bacf005c2a79102141ae8cc3eb91849 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:38:40 2020 -0800 fixed error in simhostroute.sh usage docs commit d838582119d43ee2002ce3808051c82b23e98c58 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:28:35 2020 -0800 added tcpblaster defconfig and updated docs commit af3d2d6591f12d1127027fdd363858052094e624 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:21:29 2020 -0800 added info about capabilities and running as root commit 6359cfdfedefc818b169455401942d3d33a59f41 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:20:42 2020 -0800 updated network linux readme commit 47feb08aa74e4b1fc6c802567bc777d31e7c9a83 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:09:04 2020 -0800 initial addition
2020-02-25 08:34:35 +08:00
# 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
simulator network host route helper script and docs Squashed commit of the following: commit 685951b5385062035ac558df1112353c9441c910 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:33:00 2020 -0800 fixed typo in readme commit ad1d3289b48af0de3095e3f365429017e57278b3 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:07:48 2020 -0800 simulator host route helper script and docs Squashed commit of the following: commit cf5cddcf55a155303cb5abb1aa026f6dcaf369ca Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:50:40 2020 -0800 syslog on console to fix compile error commit def1bb73fcfbc8b115c55d9f4544b97d583807f2 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:46:13 2020 -0800 formatting as per PR feedback; removed ping commit b179fd8831b77fbbe85527a4ab3161e1f1ca1e43 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:45:49 2020 -0800 formatting as per PR feedback commit e3280bede9798d9a00b118e126c02ceab497e33a Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 13:56:50 2020 -0800 add simhostroute.sh description to readme commit 09a6b0ca3bacf005c2a79102141ae8cc3eb91849 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:38:40 2020 -0800 fixed error in simhostroute.sh usage docs commit d838582119d43ee2002ce3808051c82b23e98c58 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:28:35 2020 -0800 added tcpblaster defconfig and updated docs commit af3d2d6591f12d1127027fdd363858052094e624 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:21:29 2020 -0800 added info about capabilities and running as root commit 6359cfdfedefc818b169455401942d3d33a59f41 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:20:42 2020 -0800 updated network linux readme commit 47feb08aa74e4b1fc6c802567bc777d31e7c9a83 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:09:04 2020 -0800 initial addition
2020-02-25 08:34:35 +08:00
# 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
}
simulator network host route helper script and docs Squashed commit of the following: commit 685951b5385062035ac558df1112353c9441c910 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:33:00 2020 -0800 fixed typo in readme commit ad1d3289b48af0de3095e3f365429017e57278b3 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:07:48 2020 -0800 simulator host route helper script and docs Squashed commit of the following: commit cf5cddcf55a155303cb5abb1aa026f6dcaf369ca Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:50:40 2020 -0800 syslog on console to fix compile error commit def1bb73fcfbc8b115c55d9f4544b97d583807f2 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:46:13 2020 -0800 formatting as per PR feedback; removed ping commit b179fd8831b77fbbe85527a4ab3161e1f1ca1e43 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:45:49 2020 -0800 formatting as per PR feedback commit e3280bede9798d9a00b118e126c02ceab497e33a Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 13:56:50 2020 -0800 add simhostroute.sh description to readme commit 09a6b0ca3bacf005c2a79102141ae8cc3eb91849 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:38:40 2020 -0800 fixed error in simhostroute.sh usage docs commit d838582119d43ee2002ce3808051c82b23e98c58 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:28:35 2020 -0800 added tcpblaster defconfig and updated docs commit af3d2d6591f12d1127027fdd363858052094e624 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:21:29 2020 -0800 added info about capabilities and running as root commit 6359cfdfedefc818b169455401942d3d33a59f41 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:20:42 2020 -0800 updated network linux readme commit 47feb08aa74e4b1fc6c802567bc777d31e7c9a83 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:09:04 2020 -0800 initial addition
2020-02-25 08:34:35 +08:00
net_off() {
ip addr del $IP_HOST dev $IF_BRIDGE
simulator network host route helper script and docs Squashed commit of the following: commit 685951b5385062035ac558df1112353c9441c910 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:33:00 2020 -0800 fixed typo in readme commit ad1d3289b48af0de3095e3f365429017e57278b3 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:07:48 2020 -0800 simulator host route helper script and docs Squashed commit of the following: commit cf5cddcf55a155303cb5abb1aa026f6dcaf369ca Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:50:40 2020 -0800 syslog on console to fix compile error commit def1bb73fcfbc8b115c55d9f4544b97d583807f2 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:46:13 2020 -0800 formatting as per PR feedback; removed ping commit b179fd8831b77fbbe85527a4ab3161e1f1ca1e43 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:45:49 2020 -0800 formatting as per PR feedback commit e3280bede9798d9a00b118e126c02ceab497e33a Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 13:56:50 2020 -0800 add simhostroute.sh description to readme commit 09a6b0ca3bacf005c2a79102141ae8cc3eb91849 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:38:40 2020 -0800 fixed error in simhostroute.sh usage docs commit d838582119d43ee2002ce3808051c82b23e98c58 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:28:35 2020 -0800 added tcpblaster defconfig and updated docs commit af3d2d6591f12d1127027fdd363858052094e624 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:21:29 2020 -0800 added info about capabilities and running as root commit 6359cfdfedefc818b169455401942d3d33a59f41 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:20:42 2020 -0800 updated network linux readme commit 47feb08aa74e4b1fc6c802567bc777d31e7c9a83 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:09:04 2020 -0800 initial addition
2020-02-25 08:34:35 +08:00
# 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 up
ifconfig -a
call_all net_on
else
ip link delete $IF_BRIDGE type bridge
simulator network host route helper script and docs Squashed commit of the following: commit 685951b5385062035ac558df1112353c9441c910 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:33:00 2020 -0800 fixed typo in readme commit ad1d3289b48af0de3095e3f365429017e57278b3 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 16:07:48 2020 -0800 simulator host route helper script and docs Squashed commit of the following: commit cf5cddcf55a155303cb5abb1aa026f6dcaf369ca Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:50:40 2020 -0800 syslog on console to fix compile error commit def1bb73fcfbc8b115c55d9f4544b97d583807f2 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:46:13 2020 -0800 formatting as per PR feedback; removed ping commit b179fd8831b77fbbe85527a4ab3161e1f1ca1e43 Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 15:45:49 2020 -0800 formatting as per PR feedback commit e3280bede9798d9a00b118e126c02ceab497e33a Author: Adam Feuer <adam@starcat.io> Date: Mon Feb 24 13:56:50 2020 -0800 add simhostroute.sh description to readme commit 09a6b0ca3bacf005c2a79102141ae8cc3eb91849 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:38:40 2020 -0800 fixed error in simhostroute.sh usage docs commit d838582119d43ee2002ce3808051c82b23e98c58 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:28:35 2020 -0800 added tcpblaster defconfig and updated docs commit af3d2d6591f12d1127027fdd363858052094e624 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:21:29 2020 -0800 added info about capabilities and running as root commit 6359cfdfedefc818b169455401942d3d33a59f41 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:20:42 2020 -0800 updated network linux readme commit 47feb08aa74e4b1fc6c802567bc777d31e7c9a83 Author: Adam Feuer <adam@starcat.io> Date: Sun Feb 23 17:09:04 2020 -0800 initial addition
2020-02-25 08:34:35 +08:00
fi
ip route show
ip -6 route show