Skip to content

Instantly share code, notes, and snippets.

@MisakaMikoto-35c5
Last active November 3, 2021 16:06
Embed
What would you like to do?
#!/bin/sh
INTERNAL_IPV6_ADDRESS_RANGE=2001:db8::/64
TARGET_INTERFACE=wan
REAL_INTERFACE=pppoe-$TARGET_INTERFACE
if [ "${INTERFACE}" = "$TARGET_INTERFACE" ]; then
case "${ACTION}" in
ifup|ifupdate)
ip route del default dev $REAL_INTERFACE proto static metric 0
sh -x /usr/sbin/nptv6.sh $REAL_INTERFACE $INTERNAL_IPV6_ADDRESS_RANGE connect
ip -6 ro ad default dev $REAL_INTERFACE proto static metric 512 pref medium
;;
ifdown)
sh -x /usr/sbin/nptv6.sh $REAL_INTERFACE $INTERNAL_IPV6_ADDRESS_RANGE
;;
*) ;;
esac
fi
#!/bin/sh
INTERFACE=$1
TARGET_IP_RANGE=$2
OPERATION=$3
function show_help {
echo 'Usage: nptv6.sh Interface LAN_IPv6_Range [Operation]'
echo 'Example: nptv6.sh pppoe-wan fd00::/64'
echo ' nptv6.sh pppoe-wan fd00::/64 connect'
echo 'Operation only have one option: connect.'
echo ' If Operation is connect, this script will find IP'
echo ' address for user define interface. Only the first IP'
echo ' of this interface will be used.'
}
if [ ! $INTERFACE ]
then
show_help
exit 1
fi
if [ ! $TARGET_IP_RANGE ]
then
show_help
exit 1
fi
touch /tmp/nptv6-$INTERFACE
LAST_IP=`cat /tmp/nptv6-$INTERFACE`
if [ "$LAST_IP" != "" ]
then
echo Remove history iptables rule
ip6tables -t nat -D POSTROUTING -o $INTERFACE -j NETMAP \
--to $LAST_IP -s $TARGET_IP_RANGE
ip6tables -t nat -D PREROUTING -i $INTERFACE -j NETMAP \
-d $LAST_IP --to $TARGET_IP_RANGE
echo '' > /tmp/nptv6-$INTERFACE
fi
if [ $OPERATION == "connect" ]
then
CURRENT_IP=`ip address show dev $INTERFACE to ::/0 scope global | grep inet | sed 's/.*inet6 \([a-f0-9:]*\/\d*\).*/\1/g'`
echo Current IP is: $CURRENT_IP
echo $CURRENT_IP > /tmp/nptv6-$INTERFACE
ip6tables -t nat -A POSTROUTING -o $INTERFACE -j NETMAP \
--to $CURRENT_IP -s $TARGET_IP_RANGE
ip6tables -t nat -A PREROUTING -i $INTERFACE -j NETMAP \
-d $CURRENT_IP --to $TARGET_IP_RANGE
fi

OpenWRT-19.07-NPTv6-scripts

LICENSE

WTFPL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment