-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
l2tp.sh
121 lines (106 loc) · 2.9 KB
/
l2tp.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/sh
[ -x /usr/sbin/xl2tpd ] || exit 0
[ -n "$INCLUDE_ONLY" ] || {
. /lib/functions.sh
. ../netifd-proto.sh
init_proto "$@"
}
proto_l2tp_init_config() {
proto_config_add_string "username"
proto_config_add_string "password"
proto_config_add_string "keepalive"
proto_config_add_string "pppd_options"
proto_config_add_boolean "ipv6"
proto_config_add_int "mtu"
proto_config_add_int "checkup_interval"
proto_config_add_string "server"
available=1
no_device=1
no_proto_task=1
teardown_on_l3_link_down=1
}
proto_l2tp_setup() {
local interface="$1"
local optfile="/tmp/l2tp/options.${interface}"
local ip serv_addr server host
json_get_var server server
host="${server%:*}"
for ip in $(resolveip -t 5 "$host"); do
( proto_add_host_dependency "$interface" "$ip" )
serv_addr=1
done
[ -n "$serv_addr" ] || {
echo "Could not resolve server address" >&2
sleep 5
proto_setup_failed "$interface"
exit 1
}
# Start and wait for xl2tpd
if [ ! -p /var/run/xl2tpd/l2tp-control -o -z "$(pidof xl2tpd)" ]; then
/etc/init.d/xl2tpd restart
local wait_timeout=0
while [ ! -p /var/run/xl2tpd/l2tp-control ]; do
wait_timeout=$(($wait_timeout + 1))
[ "$wait_timeout" -gt 5 ] && {
echo "Cannot find xl2tpd control file." >&2
proto_setup_failed "$interface"
exit 1
}
sleep 1
done
fi
local ipv6 keepalive username password pppd_options mtu
json_get_vars ipv6 keepalive username password pppd_options mtu
[ "$ipv6" = 1 ] || ipv6=""
local interval="${keepalive##*[, ]}"
[ "$interval" != "$keepalive" ] || interval=5
keepalive="${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}}"
username="${username:+user \"$username\" password \"$password\"}"
ipv6="${ipv6:++ipv6}"
mtu="${mtu:+mtu $mtu mru $mtu}"
mkdir -p /tmp/l2tp
cat <<EOF >"$optfile"
usepeerdns
nodefaultroute
ipparam "$interface"
ifname "l2tp-$interface"
ip-up-script /lib/netifd/ppp-up
ipv6-up-script /lib/netifd/ppp-up
ip-down-script /lib/netifd/ppp-down
ipv6-down-script /lib/netifd/ppp-down
# Don't wait for LCP term responses; exit immediately when killed.
lcp-max-terminate 0
$keepalive
$username
$ipv6
$mtu
$pppd_options
EOF
xl2tpd-control add-lac l2tp-${interface} pppoptfile=${optfile} lns=${server} || {
echo "xl2tpd-control: Add l2tp-$interface failed" >&2
proto_setup_failed "$interface"
exit 1
}
xl2tpd-control connect-lac l2tp-${interface} || {
echo "xl2tpd-control: Connect l2tp-$interface failed" >&2
proto_setup_failed "$interface"
exit 1
}
}
proto_l2tp_teardown() {
local interface="$1"
local optfile="/tmp/l2tp/options.${interface}"
rm -f ${optfile}
if [ -p /var/run/xl2tpd/l2tp-control ]; then
xl2tpd-control remove-lac l2tp-${interface} || {
echo "xl2tpd-control: Remove l2tp-$interface failed" >&2
}
fi
# Wait for interface to go down
while [ -d /sys/class/net/l2tp-${interface} ]; do
sleep 1
done
}
[ -n "$INCLUDE_ONLY" ] || {
add_protocol l2tp
}