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