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