From 6dcaa9ad492ffa43ebca7bc919b735cac131584c Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 20 Mar 2020 13:28:02 +0100 Subject: [PATCH] tools/radvd: Add script to run radvd on a tun interface This adds a small wrapper script to configure and start radvd on a tun interface. This allows the use of router advertisements and global adresses with the `native` target. Usage: - first create the tap interface as usual sudo dist/tools/tapsetup/tapsetup - now run radvd on the new tapbr0 interface sudo dist/tools/radvd/radvd.sh -c tapbr0 2001:db8::/64 - Now run the `gnrc_networking` example on native: make -C examples/gnrc_networking all term You should now see that the `native` node has received a global address in `ifconfig`. You should be able to reach this address from your host computer. It may take very long for the native node to obtain the address. If you are observing this, try turning off router advertisements of the native node on the upstream interface by running ifconfig 6 -rtr_adv Alternatively change `USEMODULE += gnrc_ipv6_router_default` to `USEMODULE += gnrc_ipv6_default` in the project's `Makefile`. --- dist/tools/radvd/radvd.conf | 9 +++ dist/tools/radvd/radvd.sh | 106 ++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 dist/tools/radvd/radvd.conf create mode 100755 dist/tools/radvd/radvd.sh diff --git a/dist/tools/radvd/radvd.conf b/dist/tools/radvd/radvd.conf new file mode 100644 index 0000000000..86edff0e8d --- /dev/null +++ b/dist/tools/radvd/radvd.conf @@ -0,0 +1,9 @@ +interface ${TAP} +{ + AdvSendAdvert on; + prefix ${PREFIX} + { + AdvOnLink on; + AdvAutonomous on; + }; +}; diff --git a/dist/tools/radvd/radvd.sh b/dist/tools/radvd/radvd.sh new file mode 100755 index 0000000000..c491e35dc9 --- /dev/null +++ b/dist/tools/radvd/radvd.sh @@ -0,0 +1,106 @@ +#!/bin/sh + +CURRENT_DIR="$(dirname $(readlink -f $0))" +PIDFILE="/tmp/radvd-${SUDO_USER}.pid" + +stop_radvd() { + if [ -f "${PIDFILE}" ]; then + PID=$(cat ${PIDFILE}) + fi + if [ -n "${PID}" ]; then + kill ${PID} + rm ${PIDFILE} + echo "radvd stopped" + fi +} + +start_radvd() { + export TAP + export PREFIX + cat ${CURRENT_DIR}/radvd.conf | envsubst | radvd -C /dev/stdin -u ${SUDO_USER} -p ${PIDFILE} + + if [ $? -ne 0 ]; then + echo "radvd failed to start on ${TAP} with prefix ${PREFIX}" + exit 1 + else + echo "radvd running on ${TAP}" + fi +} + +usage() { + echo "usage: $0 [options]" >&2 + echo "Options:" >&2 + echo " -c : Start radvd on , advertising " >&2 + echo " -d, --delete: Stop radvd" >&2 +} + +if ! command -v radvd > /dev/null; then + echo "Router Advertisement Daemon 'radvd' not found." >&2 + echo "Please install 'radvd' on your operating system." >&2 + exit 1 +fi + +while true ; do + case "$1" in + -c|--create) + if [ -n "${COMMAND}" ]; then + usage + exit 2 + fi + COMMAND="create" + shift + case "$1" in + "") + usage + exit 2 ;; + *) + TAP="$1" + shift 1 ;; + esac + case "$1" in + "") + usage + exit 2 ;; + *) + PREFIX="$1" + shift 1 ;; + esac ;; + -d|--delete) + if [ -n "${COMMAND}" ]; then + usage + exit 2 + fi + COMMAND="delete" + shift ;; + -h|--help) + usage + exit ;; + "") + break ;; + *) usage + exit 2 ;; + esac +done + +if [ -z "${SUDO_USER}" ]; then + echo 'Environment variable $SUDO_USER required; Please run with `sudo`' + exit 1 +fi + +if [ -z "${COMMAND}" ]; then + COMMAND="create" +fi + +if [ "${COMMAND}" = 'create' ]; then + [ -z "${TAP}" -o -z "${PREFIX}" ] && { + usage + exit 1 + } + stop_radvd + start_radvd || exit 1 +elif [ "${COMMAND}" = 'delete' ]; then + stop_radvd || exit 1 +else + echo 'unknown command' + exit 1 +fi