packages/net/xl2tpd: netifd support
[openwrt/svn-archive/archive.git] / net / xl2tpd / files / l2tp.sh
1 #!/bin/sh
2
3 [ -x /usr/sbin/xl2tpd ] || exit 0
4
5 [ -n "$INCLUDE_ONLY" ] || {
6 . /lib/functions.sh
7 . ../netifd-proto.sh
8 init_proto "$@"
9 }
10
11 proto_l2tp_init_config() {
12 proto_config_add_string "username"
13 proto_config_add_string "password"
14 proto_config_add_string "keepalive"
15 proto_config_add_string "pppd_options"
16 proto_config_add_boolean "defaultroute"
17 proto_config_add_boolean "peerdns"
18 proto_config_add_boolean "ipv6"
19 proto_config_add_int "mtu"
20 proto_config_add_string "server"
21 available=1
22 no_device=1
23 }
24
25 proto_l2tp_setup() {
26 local config="$1"
27 local iface="$2"
28 local optfile="/tmp/l2tp/options.${config}"
29
30 local ip serv_addr server
31 json_get_var server server && {
32 for ip in $(resolveip -t 5 "$server"); do
33 ( proto_add_host_dependency "$config" "$ip" )
34 serv_addr=1
35 done
36 }
37 [ -n "$serv_addr" ] || {
38 echo "Could not resolve server address"
39 sleep 5
40 proto_setup_failed "$config"
41 exit 1
42 }
43
44 if [ ! -p /var/run/xl2tpd/l2tp-control ]; then
45 /etc/init.d/xl2tpd start
46 fi
47
48 json_get_vars ipv6 peerdns defaultroute demand keepalive username password pppd_options
49 [ "$ipv6" = 1 ] || ipv6=""
50 [ "$peerdns" = 0 ] && peerdns="" || peerdns="1"
51 if [ "$defaultroute" = 1 ]; then
52 defaultroute="defaultroute replacedefaultroute";
53 else
54 defaultroute="nodefaultroute"
55 fi
56 if [ "${demand:-0}" -gt 0 ]; then
57 demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
58 else
59 demand="persist"
60 fi
61
62 [ -n "$mtu" ] || json_get_var mtu mtu
63
64 local interval="${keepalive##*[, ]}"
65 [ "$interval" != "$keepalive" ] || interval=5
66
67 mkdir -p /tmp/l2tp
68
69 echo ${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}} > "${optfile}"
70 echo "${peerdns:+usepeerdns}" >> "${optfile}"
71 echo "$defaultroute" >> "${optfile}"
72 echo "${username:+user \"$username\" password \"$password\"}" >> "${optfile}"
73 echo "ipparam \"$config\"" >> "${optfile}"
74 echo "ifname \"l2tp-$config\"" >> "${optfile}"
75 echo "ip-up-script /lib/netifd/ppp-up" >> "${optfile}"
76 echo "ipv6-up-script /lib/netifd/ppp-up" >> "${optfile}"
77 echo "ip-down-script /lib/netifd/ppp-down" >> "${optfile}"
78 echo "ipv6-down-script /lib/netifd/ppp-down" >> "${optfile}"
79 # Don't wait for LCP term responses; exit immediately when killed.
80 echo "lcp-max-terminate 0" >> "${optfile}"
81 echo "${ipv6} ${pppd_options}" >> "${optfile}"
82 echo "${mtu:+mtu $mtu mru $mtu}" >> "${optfile}"
83
84 xl2tpd-control add l2tp-${config} pppoptfile=${optfile} lns=${server} redial=yes redial timeout=20
85 xl2tpd-control connect l2tp-${config}
86 }
87
88 proto_l2tp_teardown() {
89 local interface="$1"
90 local optfile="/tmp/l2tp/options.${interface}"
91
92 case "$ERROR" in
93 11|19)
94 proto_notify_error "$interface" AUTH_FAILED
95 proto_block_restart "$interface"
96 ;;
97 2)
98 proto_notify_error "$interface" INVALID_OPTIONS
99 proto_block_restart "$interface"
100 ;;
101 esac
102
103 xl2tpd-control disconnect l2tp-${interface}
104 # Wait for interface to go down
105 while [ -d /sys/class/net/l2tp-${interface} ]; do
106 sleep 1
107 done
108
109 xl2tpd-control remove l2tp-${interface}
110 rm -f ${optfile}
111 }
112
113 [ -n "$INCLUDE_ONLY" ] || {
114 add_protocol l2tp
115 }