Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # add private ipv4 and ipv6 addresses to loopback
0005 
0006 readonly V6_INNER='100::a/128'
0007 readonly V4_INNER='192.168.0.1/32'
0008 
0009 if getopts ":s" opt; then
0010   readonly SIT_DEV_NAME='sixtofourtest0'
0011   readonly V6_SIT='2::/64'
0012   readonly V4_SIT='172.17.0.1/32'
0013   shift
0014 fi
0015 
0016 fail() {
0017   echo "error: $*" 1>&2
0018   exit 1
0019 }
0020 
0021 setup() {
0022   ip -6 addr add "${V6_INNER}" dev lo || fail 'failed to setup v6 address'
0023   ip -4 addr add "${V4_INNER}" dev lo || fail 'failed to setup v4 address'
0024 
0025   if [[ -n "${V6_SIT}" ]]; then
0026     ip link add "${SIT_DEV_NAME}" type sit remote any local any \
0027             || fail 'failed to add sit'
0028     ip link set dev "${SIT_DEV_NAME}" up \
0029             || fail 'failed to bring sit device up'
0030     ip -6 addr add "${V6_SIT}" dev "${SIT_DEV_NAME}" \
0031             || fail 'failed to setup v6 SIT address'
0032     ip -4 addr add "${V4_SIT}" dev "${SIT_DEV_NAME}" \
0033             || fail 'failed to setup v4 SIT address'
0034   fi
0035 
0036   sleep 2       # avoid race causing bind to fail
0037 }
0038 
0039 cleanup() {
0040   if [[ -n "${V6_SIT}" ]]; then
0041     ip -4 addr del "${V4_SIT}" dev "${SIT_DEV_NAME}"
0042     ip -6 addr del "${V6_SIT}" dev "${SIT_DEV_NAME}"
0043     ip link del "${SIT_DEV_NAME}"
0044   fi
0045 
0046   ip -4 addr del "${V4_INNER}" dev lo
0047   ip -6 addr del "${V6_INNER}" dev lo
0048 }
0049 
0050 trap cleanup EXIT
0051 
0052 setup
0053 "$@"
0054 exit "$?"