2012-03-20 05:10:04 +08:00
|
|
|
#!/bin/sh
|
2010-05-28 08:27:01 +08:00
|
|
|
# 6in4.sh - IPv6-in-IPv4 tunnel backend
|
2012-06-17 20:18:39 +08:00
|
|
|
# Copyright (c) 2010-2012 OpenWrt.org
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2012-03-20 05:10:04 +08:00
|
|
|
[ -n "$INCLUDE_ONLY" ] || {
|
2012-06-06 00:04:23 +08:00
|
|
|
. /lib/functions.sh
|
2012-06-17 20:18:39 +08:00
|
|
|
. /lib/functions/network.sh
|
2012-03-20 05:10:04 +08:00
|
|
|
. ../netifd-proto.sh
|
|
|
|
init_proto "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
proto_6in4_setup() {
|
|
|
|
local cfg="$1"
|
|
|
|
local iface="$2"
|
|
|
|
local link="6in4-$cfg"
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2014-01-17 21:59:47 +08:00
|
|
|
local mtu ttl ipaddr peeraddr ip6addr ip6prefix tunnelid username password sourcerouting
|
|
|
|
json_get_vars mtu ttl ipaddr peeraddr ip6addr ip6prefix tunnelid username password sourcerouting
|
2012-03-20 05:10:04 +08:00
|
|
|
|
2012-10-14 17:17:14 +08:00
|
|
|
[ -z "$peeraddr" ] && {
|
2012-06-19 05:40:34 +08:00
|
|
|
proto_notify_error "$cfg" "MISSING_ADDRESS"
|
|
|
|
proto_block_restart "$cfg"
|
2012-03-20 05:10:04 +08:00
|
|
|
return
|
|
|
|
}
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2012-06-19 05:40:34 +08:00
|
|
|
( proto_add_host_dependency "$cfg" 0.0.0.0 )
|
|
|
|
|
2012-06-17 20:27:16 +08:00
|
|
|
[ -z "$ipaddr" ] && {
|
2012-06-17 20:18:39 +08:00
|
|
|
local wanif
|
2012-06-17 20:27:16 +08:00
|
|
|
if ! network_find_wan wanif || ! network_get_ipaddr ipaddr "$wanif"; then
|
2012-06-19 05:40:34 +08:00
|
|
|
proto_notify_error "$cfg" "NO_WAN_LINK"
|
2012-03-20 05:10:04 +08:00
|
|
|
return
|
2012-06-17 20:18:39 +08:00
|
|
|
fi
|
2010-05-29 06:03:30 +08:00
|
|
|
}
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2012-03-20 05:10:04 +08:00
|
|
|
proto_init_update "$link" 1
|
2014-01-17 21:59:47 +08:00
|
|
|
|
|
|
|
local source=""
|
|
|
|
[ "$sourcerouting" != "0" ] && source="::/128"
|
|
|
|
proto_add_ipv6_route "::" 0 "" "" "" "$source"
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2012-10-14 17:17:14 +08:00
|
|
|
[ -n "$ip6addr" ] && {
|
|
|
|
local local6="${ip6addr%%/*}"
|
|
|
|
local mask6="${ip6addr##*/}"
|
|
|
|
[[ "$local6" = "$mask6" ]] && mask6=
|
|
|
|
proto_add_ipv6_address "$local6" "$mask6"
|
2014-01-17 21:59:47 +08:00
|
|
|
[ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$local6/$mask6"
|
2012-10-14 17:17:14 +08:00
|
|
|
}
|
|
|
|
|
2014-01-17 21:59:47 +08:00
|
|
|
[ -n "$ip6prefix" ] && {
|
|
|
|
proto_add_ipv6_prefix "$ip6prefix"
|
|
|
|
[ "$sourcerouting" != "0" ] && proto_add_ipv6_route "::" 0 "" "" "" "$ip6prefix"
|
|
|
|
}
|
2013-01-15 21:08:05 +08:00
|
|
|
|
2012-03-20 05:10:04 +08:00
|
|
|
proto_add_tunnel
|
|
|
|
json_add_string mode sit
|
|
|
|
json_add_int mtu "${mtu:-1280}"
|
|
|
|
json_add_int ttl "${ttl:-64}"
|
2012-06-17 20:27:16 +08:00
|
|
|
json_add_string local "$ipaddr"
|
2012-06-17 20:18:39 +08:00
|
|
|
json_add_string remote "$peeraddr"
|
2012-03-20 05:10:04 +08:00
|
|
|
proto_close_tunnel
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2012-03-20 05:10:04 +08:00
|
|
|
proto_send_update "$cfg"
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2012-03-20 05:10:04 +08:00
|
|
|
[ -n "$tunnelid" -a -n "$username" -a -n "$password" ] && {
|
|
|
|
[ "${#password}" == 32 -a -z "${password//[a-fA-F0-9]/}" ] || {
|
|
|
|
password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
|
2010-05-29 06:03:30 +08:00
|
|
|
}
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2012-03-20 05:10:04 +08:00
|
|
|
local url="http://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&apikey=$username&pass=$password&tid=$tunnelid"
|
|
|
|
local try=0
|
|
|
|
local max=3
|
|
|
|
|
|
|
|
while [ $((++try)) -le $max ]; do
|
2013-01-30 17:07:23 +08:00
|
|
|
( exec wget -qO/dev/null "$url" 2>/dev/null ) &
|
2013-01-30 17:05:53 +08:00
|
|
|
local pid=$!
|
|
|
|
( sleep 5; kill $pid 2>/dev/null ) &
|
|
|
|
wait $pid && break
|
2012-03-20 05:10:04 +08:00
|
|
|
done
|
2010-05-29 06:03:30 +08:00
|
|
|
}
|
2010-05-28 08:27:01 +08:00
|
|
|
}
|
|
|
|
|
2012-03-20 05:10:04 +08:00
|
|
|
proto_6in4_teardown() {
|
2010-05-28 08:27:01 +08:00
|
|
|
local cfg="$1"
|
2012-03-20 05:10:04 +08:00
|
|
|
}
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2012-03-20 05:10:04 +08:00
|
|
|
proto_6in4_init_config() {
|
|
|
|
no_device=1
|
|
|
|
available=1
|
|
|
|
|
|
|
|
proto_config_add_string "ipaddr"
|
|
|
|
proto_config_add_string "ip6addr"
|
2013-01-15 21:08:05 +08:00
|
|
|
proto_config_add_string "ip6prefix"
|
2012-03-20 05:10:04 +08:00
|
|
|
proto_config_add_string "peeraddr"
|
|
|
|
proto_config_add_string "tunnelid"
|
|
|
|
proto_config_add_string "username"
|
|
|
|
proto_config_add_string "password"
|
|
|
|
proto_config_add_int "mtu"
|
|
|
|
proto_config_add_int "ttl"
|
2014-01-20 20:53:15 +08:00
|
|
|
proto_config_add_boolean "sourcerouting"
|
2012-03-20 05:10:04 +08:00
|
|
|
}
|
2010-05-28 08:27:01 +08:00
|
|
|
|
2012-03-20 05:10:04 +08:00
|
|
|
[ -n "$INCLUDE_ONLY" ] || {
|
|
|
|
add_protocol 6in4
|
2010-05-28 08:27:01 +08:00
|
|
|
}
|