2016-03-30 12:30:40 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
2016-04-19 14:33:37 +02:00
|
|
|
ETHOS_DIR="$(dirname $(readlink -f $0))"
|
|
|
|
|
2016-03-30 12:30:40 +02:00
|
|
|
create_tap() {
|
|
|
|
ip tuntap add ${TAP} mode tap user ${USER}
|
|
|
|
sysctl -w net.ipv6.conf.${TAP}.forwarding=1
|
|
|
|
sysctl -w net.ipv6.conf.${TAP}.accept_ra=0
|
|
|
|
ip link set ${TAP} up
|
|
|
|
ip a a fe80::1/64 dev ${TAP}
|
|
|
|
ip a a fd00:dead:beef::1/128 dev lo
|
|
|
|
ip route add ${PREFIX} via fe80::2 dev ${TAP}
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_tap() {
|
|
|
|
ip tuntap del ${TAP} mode tap
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
echo "Cleaning up..."
|
|
|
|
remove_tap
|
|
|
|
ip a d fd00:dead:beef::1/128 dev lo
|
2020-03-10 15:44:49 +01:00
|
|
|
if [ -n "${UHCPD_PID}" ]; then
|
2020-02-26 12:40:03 +01:00
|
|
|
kill ${UHCPD_PID}
|
|
|
|
fi
|
2020-03-10 15:44:49 +01:00
|
|
|
if [ -n "${DHCPD_PIDFILE}" ]; then
|
|
|
|
kill "$(cat ${DHCPD_PIDFILE})"
|
|
|
|
rm "${DHCPD_PIDFILE}"
|
|
|
|
fi
|
2016-04-05 12:57:54 +02:00
|
|
|
trap "" INT QUIT TERM EXIT
|
2016-03-30 12:30:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start_uhcpd() {
|
|
|
|
${UHCPD} ${TAP} ${PREFIX} > /dev/null &
|
|
|
|
UHCPD_PID=$!
|
|
|
|
}
|
|
|
|
|
2020-03-10 15:44:49 +01:00
|
|
|
start_dhcpd() {
|
|
|
|
DHCPD_PIDFILE=$(mktemp)
|
|
|
|
${DHCPD} -d -p ${DHCPD_PIDFILE} ${TAP} ${PREFIX} 2> /dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$1" = "-d" ] || [ "$1" = "--use-dhcpv6" ]; then
|
|
|
|
USE_DHCPV6=1
|
|
|
|
shift 1
|
|
|
|
else
|
|
|
|
USE_DHCPV6=0
|
|
|
|
fi
|
|
|
|
|
2020-02-26 12:40:03 +01:00
|
|
|
if [ "$1" = "-e" ] || [ "$1" = "--ethos-only" ]; then
|
|
|
|
ETHOS_ONLY=1
|
|
|
|
shift 1
|
|
|
|
else
|
|
|
|
ETHOS_ONLY=0
|
|
|
|
fi
|
|
|
|
|
2016-03-30 12:30:40 +02:00
|
|
|
PORT=$1
|
|
|
|
TAP=$2
|
|
|
|
PREFIX=$3
|
2016-07-14 17:01:02 +02:00
|
|
|
BAUDRATE=115200
|
2020-04-03 13:39:09 +02:00
|
|
|
START_ETHOS=1
|
2016-03-30 12:30:40 +02:00
|
|
|
|
2016-04-19 14:33:37 +02:00
|
|
|
[ -z "${PORT}" -o -z "${TAP}" -o -z "${PREFIX}" ] && {
|
2020-03-10 15:44:49 +01:00
|
|
|
echo "usage: $0 [-d|--use-dhcp] [-e|--ethos-only] " \
|
|
|
|
"<serial-port> <tap-device> <prefix> " \
|
2020-02-26 12:40:03 +01:00
|
|
|
"[baudrate]"
|
2016-03-30 12:30:40 +02:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:01:02 +02:00
|
|
|
[ ! -z $4 ] && {
|
|
|
|
BAUDRATE=$4
|
|
|
|
}
|
|
|
|
|
2016-04-05 12:57:54 +02:00
|
|
|
trap "cleanup" INT QUIT TERM EXIT
|
2016-03-30 12:30:40 +02:00
|
|
|
|
2016-04-19 14:33:37 +02:00
|
|
|
|
2020-02-26 12:40:03 +01:00
|
|
|
create_tap && \
|
|
|
|
if [ ${ETHOS_ONLY} -ne 1 ]; then
|
2020-03-10 15:44:49 +01:00
|
|
|
if [ ${USE_DHCPV6} -eq 1 ]; then
|
|
|
|
DHCPD="$(readlink -f "${ETHOS_DIR}/../dhcpv6-pd_ia/")/dhcpv6-pd_ia.py"
|
|
|
|
start_dhcpd
|
|
|
|
START_ETHOS=$?
|
|
|
|
else
|
|
|
|
UHCPD="$(readlink -f "${ETHOS_DIR}/../uhcpd/bin")/uhcpd"
|
|
|
|
start_uhcpd
|
|
|
|
START_ETHOS=$?
|
|
|
|
fi
|
2020-02-26 12:40:03 +01:00
|
|
|
else
|
|
|
|
START_ETHOS=0
|
|
|
|
fi
|
|
|
|
|
|
|
|
[ ${START_ETHOS} -eq 0 ] && "${ETHOS_DIR}/ethos" ${TAP} ${PORT} ${BAUDRATE}
|