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 "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
proto_6in4_setup() {
|
|
|
|
local cfg="$1"
|
|
|
|
local iface="$2"
|
|
|
|
local link="6in4-$cfg"
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2014-10-02 19:38:15 +00:00
|
|
|
local mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey sourcerouting
|
|
|
|
json_get_vars mtu ttl tos ipaddr peeraddr ip6addr ip6prefix tunnelid username password updatekey sourcerouting
|
2012-03-19 21:10:04 +00:00
|
|
|
|
2012-10-14 09:17:14 +00:00
|
|
|
[ -z "$peeraddr" ] && {
|
2012-06-18 21:40:34 +00:00
|
|
|
proto_notify_error "$cfg" "MISSING_ADDRESS"
|
|
|
|
proto_block_restart "$cfg"
|
2012-03-19 21:10:04 +00:00
|
|
|
return
|
|
|
|
}
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2012-06-18 21:40:34 +00:00
|
|
|
( proto_add_host_dependency "$cfg" 0.0.0.0 )
|
|
|
|
|
2012-06-17 12:27:16 +00:00
|
|
|
[ -z "$ipaddr" ] && {
|
2012-06-17 12:18:39 +00:00
|
|
|
local wanif
|
2012-06-17 12:27:16 +00:00
|
|
|
if ! network_find_wan wanif || ! 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
|
|
|
|
|
|
|
local source=""
|
|
|
|
[ "$sourcerouting" != "0" ] && source="::/128"
|
|
|
|
proto_add_ipv6_route "::" 0 "" "" "" "$source"
|
2010-05-28 00:27:01 +00:00
|
|
|
|
2012-10-14 09:17:14 +00:00
|
|
|
[ -n "$ip6addr" ] && {
|
|
|
|
local local6="${ip6addr%%/*}"
|
|
|
|
local mask6="${ip6addr##*/}"
|
|
|
|
[[ "$local6" = "$mask6" ]] && mask6=
|
|
|
|
proto_add_ipv6_address "$local6" "$mask6"
|
2014-01-17 13:59:47 +00:00
|
|
|
[ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
|
2012-10-14 09:17:14 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 13:59:47 +00:00
|
|
|
[ -n "$ip6prefix" ] && {
|
|
|
|
proto_add_ipv6_prefix "$ip6prefix"
|
|
|
|
[ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
|
|
|
|
}
|
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"
|
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"
|
2014-11-09 13:46:29 +00:00
|
|
|
local urlget="wget"
|
2015-02-07 13:33:15 +00:00
|
|
|
local urlget_opts="-qO-"
|
2014-11-09 13:46:29 +00:00
|
|
|
local ca_path="${SSL_CERT_DIR-/etc/ssl/certs}"
|
|
|
|
|
|
|
|
if [ -n "$(which curl)" ]; then
|
|
|
|
urlget="curl"
|
|
|
|
urlget_opts="-s -S"
|
|
|
|
if curl -V | grep "Protocols:" | grep -qF "https"; then
|
|
|
|
http="https"
|
|
|
|
urlget_opts="$urlget_opts --capath $ca_path"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ "$http" = "http" ] &&
|
|
|
|
wget --version 2>&1 | grep -qF "+https"; then
|
|
|
|
urlget="wget"
|
2015-02-07 13:33:15 +00:00
|
|
|
urlget_opts="-qO- --ca-directory=$ca_path"
|
2014-10-30 13:15:18 +00:00
|
|
|
http="https"
|
|
|
|
fi
|
2014-11-09 13:46:29 +00:00
|
|
|
[ "$http" = "https" -a -z "$(find $ca_path -name "*.0" 2>/dev/null)" ] && {
|
|
|
|
if [ "$urlget" = "curl" ]; then
|
|
|
|
urlget_opts="$urlget_opts -k"
|
|
|
|
else
|
|
|
|
urlget_opts="$urlget_opts --no-check-certificate"
|
|
|
|
fi
|
|
|
|
}
|
2014-10-30 13:15:18 +00:00
|
|
|
|
|
|
|
local url="$http://ipv4.tunnelbroker.net/nic/update?username=$username&password=$password&hostname=$tunnelid"
|
2012-03-19 21:10:04 +00:00
|
|
|
local try=0
|
|
|
|
local max=3
|
|
|
|
|
|
|
|
while [ $((++try)) -le $max ]; do
|
2014-11-09 13:46:29 +00:00
|
|
|
( exec $urlget $urlget_opts "$url" | logger -t "$link" ) &
|
2013-01-30 09:05:53 +00:00
|
|
|
local pid=$!
|
2014-11-09 13:46:29 +00:00
|
|
|
( sleep 20; kill $pid 2>/dev/null ) &
|
2013-01-30 09:05:53 +00:00
|
|
|
wait $pid && break
|
2014-11-09 13:46:29 +00:00
|
|
|
sleep 20;
|
2012-03-19 21:10:04 +00:00
|
|
|
done
|
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"
|
2013-01-15 13:08:05 +00:00
|
|
|
proto_config_add_string "ip6prefix"
|
2012-03-19 21:10:04 +00:00
|
|
|
proto_config_add_string "peeraddr"
|
|
|
|
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"
|
2014-01-20 12:53:15 +00:00
|
|
|
proto_config_add_boolean "sourcerouting"
|
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
|
|
|
}
|