2012-03-19 21:10:04 +00:00
|
|
|
#!/bin/sh
|
2010-05-28 00:27:01 +00:00
|
|
|
# 6in4.sh - IPv6-in-IPv4 tunnel backend
|
2015-02-07 13:33:15 +00:00
|
|
|
# Copyright (c) 2010-2015 OpenWrt.org
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2012-03-19 21:10:04 +00:00
|
|
|
[ -n "$INCLUDE_ONLY" ] || {
|
2012-06-05 16:04:23 +00:00
|
|
|
. /lib/functions.sh
|
2012-06-17 12:18:39 +00:00
|
|
|
. /lib/functions/network.sh
|
2012-03-19 21:10:04 +00:00
|
|
|
. ../netifd-proto.sh
|
|
|
|
init_proto "$@"
|
|
|
|
}
|
|
|
|
|
2019-08-15 05:06:18 +00:00
|
|
|
# Function taken from 6to4 package (6to4.sh), flipped returns
|
|
|
|
test_6in4_rfc1918()
|
|
|
|
{
|
|
|
|
local oIFS="$IFS"; IFS="."; set -- $1; IFS="$oIFS"
|
|
|
|
[ $1 -eq 10 ] && return 1
|
|
|
|
[ $1 -eq 192 ] && [ $2 -eq 168 ] && return 1
|
|
|
|
[ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 1
|
|
|
|
|
|
|
|
# RFC 6598
|
|
|
|
[ $1 -eq 100 ] && [ $2 -ge 64 ] && [ $2 -le 127 ] && return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-02-08 22:20:38 +00:00
|
|
|
proto_6in4_update() {
|
|
|
|
sh -c '
|
2016-09-27 14:20:57 +00:00
|
|
|
timeout=5
|
2015-02-08 22:20:38 +00:00
|
|
|
|
|
|
|
(while [ $((timeout--)) -gt 0 ]; do
|
|
|
|
sleep 1
|
|
|
|
kill -0 $$ || exit 0
|
|
|
|
done; kill -9 $$) 2>/dev/null &
|
|
|
|
|
|
|
|
exec "$@"
|
|
|
|
' "$1" "$@"
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:58:08 +00:00
|
|
|
proto_6in4_add_prefix() {
|
|
|
|
append "$3" "$1"
|
|
|
|
}
|
|
|
|
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_6in4_setup() {
|
|
|
|
local cfg="$1"
|
|
|
|
local iface="$2"
|
|
|
|
local link="6in4-$cfg"
|
2024-07-16 13:44:35 +00:00
|
|
|
local remoteip
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2024-07-16 13:39:17 +00:00
|
|
|
local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix ip6prefixes tunlink tunnelid username password updatekey device
|
|
|
|
json_get_vars mtu ttl tos ipaddr peeraddr ip6addr tunlink tunnelid username password updatekey device
|
2018-02-14 13:58:08 +00:00
|
|
|
json_for_each_item proto_6in4_add_prefix ip6prefix ip6prefixes
|
2012-03-19 21:10:04 +00:00
|
|
|
|
2024-07-16 13:39:17 +00:00
|
|
|
[ -n "$device" ] && link="$device"
|
|
|
|
|
2012-10-14 09:17:14 +00:00
|
|
|
[ -z "$peeraddr" ] && {
|
2024-07-16 13:44:35 +00:00
|
|
|
proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
|
2012-06-18 21:40:34 +00:00
|
|
|
proto_block_restart "$cfg"
|
2012-03-19 21:10:04 +00:00
|
|
|
return
|
|
|
|
}
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2024-07-16 13:44:35 +00:00
|
|
|
remoteip=$(resolveip -t 10 -4 "$peeraddr")
|
|
|
|
|
|
|
|
if [ -z "$remoteip" ]; then
|
|
|
|
proto_notify_error "$cfg" "PEER_RESOLVE_FAIL"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
for ip in $remoteip; do
|
|
|
|
peeraddr=$ip
|
|
|
|
break
|
|
|
|
done
|
|
|
|
|
2015-10-15 08:56:59 +00:00
|
|
|
( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
|
2012-06-18 21:40:34 +00:00
|
|
|
|
2012-06-17 12:27:16 +00:00
|
|
|
[ -z "$ipaddr" ] && {
|
2015-10-15 08:56:59 +00:00
|
|
|
local wanif="$tunlink"
|
|
|
|
if [ -z "$wanif" ] && ! network_find_wan wanif; then
|
|
|
|
proto_notify_error "$cfg" "NO_WAN_LINK"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! network_get_ipaddr ipaddr "$wanif"; then
|
2012-06-18 21:40:34 +00:00
|
|
|
proto_notify_error "$cfg" "NO_WAN_LINK"
|
2012-03-19 21:10:04 +00:00
|
|
|
return
|
2012-06-17 12:18:39 +00:00
|
|
|
fi
|
2010-05-28 22:03:30 +00:00
|
|
|
}
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_init_update "$link" 1
|
2014-01-17 13:59:47 +00:00
|
|
|
|
2012-10-14 09:17:14 +00:00
|
|
|
[ -n "$ip6addr" ] && {
|
|
|
|
local local6="${ip6addr%%/*}"
|
|
|
|
local mask6="${ip6addr##*/}"
|
2019-12-25 17:43:45 +00:00
|
|
|
[ "$local6" = "$mask6" ] && mask6=
|
2012-10-14 09:17:14 +00:00
|
|
|
proto_add_ipv6_address "$local6" "$mask6"
|
2015-05-19 07:53:08 +00:00
|
|
|
proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
|
2012-10-14 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 13:58:08 +00:00
|
|
|
for ip6prefix in $ip6prefixes; do
|
2014-01-17 13:59:47 +00:00
|
|
|
proto_add_ipv6_prefix "$ip6prefix"
|
2015-05-19 07:53:08 +00:00
|
|
|
proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
|
2018-02-14 13:58:08 +00:00
|
|
|
done
|
2013-01-15 13:08:05 +00:00
|
|
|
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_add_tunnel
|
|
|
|
json_add_string mode sit
|
|
|
|
json_add_int mtu "${mtu:-1280}"
|
|
|
|
json_add_int ttl "${ttl:-64}"
|
2014-10-02 19:38:15 +00:00
|
|
|
[ -n "$tos" ] && json_add_string tos "$tos"
|
2012-06-17 12:27:16 +00:00
|
|
|
json_add_string local "$ipaddr"
|
2012-06-17 12:18:39 +00:00
|
|
|
json_add_string remote "$peeraddr"
|
2015-10-15 08:56:59 +00:00
|
|
|
[ -n "$tunlink" ] && json_add_string link "$tunlink"
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_close_tunnel
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_send_update "$cfg"
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2014-02-20 21:36:15 +00:00
|
|
|
[ -n "$tunnelid" -a -n "$username" -a \( -n "$password" -o -n "$updatekey" \) ] && {
|
|
|
|
[ -n "$updatekey" ] && password="$updatekey"
|
|
|
|
|
2014-10-30 13:15:18 +00:00
|
|
|
local http="http"
|
2016-01-20 10:15:29 +00:00
|
|
|
local urlget="uclient-fetch"
|
2015-02-07 13:33:15 +00:00
|
|
|
local urlget_opts="-qO-"
|
2017-01-23 12:39:52 +00:00
|
|
|
local ca_path="${SSL_CERT_DIR:-/etc/ssl/certs}"
|
2014-11-09 13:46:29 +00:00
|
|
|
|
2016-01-20 10:15:29 +00:00
|
|
|
[ -f /lib/libustream-ssl.so ] && http=https
|
2014-11-09 13:46:29 +00:00
|
|
|
[ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
|
2016-01-20 10:15:29 +00:00
|
|
|
urlget_opts="$urlget_opts --no-check-certificate"
|
2014-11-09 13:46:29 +00:00
|
|
|
}
|
2014-10-30 13:15:18 +00:00
|
|
|
|
2016-01-20 10:15:29 +00:00
|
|
|
local url="$http://ipv4.tunnelbroker.net/nic/update?hostname=$tunnelid"
|
2019-08-15 05:06:18 +00:00
|
|
|
|
|
|
|
test_6in4_rfc1918 "$ipaddr" && {
|
|
|
|
local url="${url}&myip=${ipaddr}"
|
|
|
|
}
|
|
|
|
|
2012-03-19 21:10:04 +00:00
|
|
|
local try=0
|
|
|
|
local max=3
|
|
|
|
|
2015-02-08 22:20:38 +00:00
|
|
|
(
|
|
|
|
set -o pipefail
|
|
|
|
while [ $((++try)) -le $max ]; do
|
2016-01-20 10:15:29 +00:00
|
|
|
if proto_6in4_update $urlget $urlget_opts --user="$username" --password="$password" "$url" 2>&1 | \
|
2015-02-08 22:20:38 +00:00
|
|
|
sed -e 's,^Killed$,timeout,' -e "s,^,update $try/$max: ," | \
|
|
|
|
logger -t "$link";
|
|
|
|
then
|
|
|
|
logger -t "$link" "updated"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
logger -t "$link" "update failed"
|
|
|
|
)
|
2010-05-28 22:03:30 +00:00
|
|
|
}
|
2010-05-28 00:27:01 +00:00
|
|
|
}
|
|
|
|
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_6in4_teardown() {
|
2010-05-28 00:27:01 +00:00
|
|
|
local cfg="$1"
|
2012-03-19 21:10:04 +00:00
|
|
|
}
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_6in4_init_config() {
|
2014-11-09 13:46:29 +00:00
|
|
|
no_device=1
|
2012-03-19 21:10:04 +00:00
|
|
|
available=1
|
|
|
|
|
|
|
|
proto_config_add_string "ipaddr"
|
|
|
|
proto_config_add_string "ip6addr"
|
2018-02-14 13:58:08 +00:00
|
|
|
proto_config_add_array "ip6prefix"
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_config_add_string "peeraddr"
|
2015-10-15 08:56:59 +00:00
|
|
|
proto_config_add_string "tunlink"
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_config_add_string "tunnelid"
|
|
|
|
proto_config_add_string "username"
|
|
|
|
proto_config_add_string "password"
|
2014-02-20 21:36:15 +00:00
|
|
|
proto_config_add_string "updatekey"
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_config_add_int "mtu"
|
|
|
|
proto_config_add_int "ttl"
|
2014-10-02 19:38:15 +00:00
|
|
|
proto_config_add_string "tos"
|
2024-07-16 13:39:17 +00:00
|
|
|
proto_config_add_string "device"
|
2012-03-19 21:10:04 +00:00
|
|
|
}
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2012-03-19 21:10:04 +00:00
|
|
|
[ -n "$INCLUDE_ONLY" ] || {
|
|
|
|
add_protocol 6in4
|
2010-05-28 00:27:01 +00:00
|
|
|
}
|