ppp: remove the persist option, netifd handles reconnects
[openwrt.git] / package / network / services / ppp / files / ppp.sh
1 #!/bin/sh
2
3 [ -x /usr/sbin/pppd ] || exit 0
4
5 [ -n "$INCLUDE_ONLY" ] || {
6         . /lib/functions.sh
7         . ../netifd-proto.sh
8         init_proto "$@"
9 }
10
11 ppp_exitcode_tostring()
12 {
13         local errorcode=$1
14         [ -n "$errorcode" ] || errorcode=5
15
16         case "$errorcode" in
17                 0) echo "OK" ;;
18                 1) echo "FATAL_ERROR" ;;
19                 2) echo "OPTION_ERROR" ;;
20                 3) echo "NOT_ROOT" ;;
21                 4) echo "NO_KERNEL_SUPPORT" ;;
22                 5) echo "USER_REQUEST" ;;
23                 6) echo "LOCK_FAILED" ;;
24                 7) echo "OPEN_FAILED" ;;
25                 8) echo "CONNECT_FAILED" ;;
26                 9) echo "PTYCMD_FAILED" ;;
27                 10) echo "NEGOTIATION_FAILED" ;;
28                 11) echo "PEER_AUTH_FAILED" ;;
29                 12) echo "IDLE_TIMEOUT" ;;
30                 13) echo "CONNECT_TIME" ;;
31                 14) echo "CALLBACK" ;;
32                 15) echo "PEER_DEAD" ;;
33                 16) echo "HANGUP" ;;
34                 17) echo "LOOPBACK" ;;
35                 18) echo "INIT_FAILED" ;;
36                 19) echo "AUTH_TOPEER_FAILED" ;;
37                 20) echo "TRAFFIC_LIMIT" ;;
38                 21) echo "CNID_AUTH_FAILED";;
39                 *) echo "UNKNOWN_ERROR" ;;
40         esac
41 }
42
43 ppp_generic_init_config() {
44         proto_config_add_string username
45         proto_config_add_string password
46         proto_config_add_string keepalive
47         proto_config_add_boolean keepalive_adaptive
48         proto_config_add_int demand
49         proto_config_add_string pppd_options
50         proto_config_add_string 'connect:file'
51         proto_config_add_string 'disconnect:file'
52         proto_config_add_string ipv6
53         proto_config_add_boolean authfail
54         proto_config_add_int mtu
55         proto_config_add_string pppname
56 }
57
58 ppp_generic_setup() {
59         local config="$1"; shift
60
61         json_get_vars ipv6 demand keepalive keepalive_adaptive username password pppd_options pppname
62         if [ "$ipv6" = 0 ]; then
63                 ipv6=""
64         elif [ -z "$ipv6" -o "$ipv6" = auto ]; then
65                 ipv6=1
66                 proto_export "AUTOIPV6=1"
67         fi
68
69         if [ "${demand:-0}" -gt 0 ]; then
70                 demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
71         else
72                 demand=""
73         fi
74         [ -n "$mtu" ] || json_get_var mtu mtu
75         [ -n "$pppname" ] || pppname="${proto:-ppp}-$config"
76
77         local lcp_failure="${keepalive%%[, ]*}"
78         local lcp_interval="${keepalive##*[, ]}"
79         local lcp_adaptive="lcp-echo-adaptive"
80         [ "${lcp_failure:-0}" -lt 1 ] && lcp_failure=""
81         [ "$lcp_interval" != "$keepalive" ] || lcp_interval=5
82         [ "${keepalive_adaptive:-1}" -lt 1 ] && lcp_adaptive=""
83         [ -n "$connect" ] || json_get_var connect connect
84         [ -n "$disconnect" ] || json_get_var disconnect disconnect
85
86         proto_run_command "$config" /usr/sbin/pppd \
87                 nodetach ipparam "$config" \
88                 ifname "$pppname" \
89                 ${lcp_failure:+lcp-echo-interval $lcp_interval lcp-echo-failure $lcp_failure $lcp_adaptive} \
90                 ${ipv6:++ipv6} \
91                 nodefaultroute \
92                 usepeerdns \
93                 $demand maxfail 1 \
94                 ${username:+user "$username" password "$password"} \
95                 ${connect:+connect "$connect"} \
96                 ${disconnect:+disconnect "$disconnect"} \
97                 ip-up-script /lib/netifd/ppp-up \
98                 ipv6-up-script /lib/netifd/ppp-up \
99                 ip-down-script /lib/netifd/ppp-down \
100                 ipv6-down-script /lib/netifd/ppp-down \
101                 ${mtu:+mtu $mtu mru $mtu} \
102                 "$@" $pppd_options
103 }
104
105 ppp_generic_teardown() {
106         local interface="$1"
107         local errorstring=$(ppp_exitcode_tostring $ERROR)
108
109         case "$ERROR" in
110                 0)
111                 ;;
112                 2)
113                         proto_notify_error "$interface" "$errorstring"
114                         proto_block_restart "$interface"
115                 ;;
116                 11|19)
117                         json_get_var authfail authfail
118                         proto_notify_error "$interface" "$errorstring"
119                         if [ "${authfail:-0}" -gt 0 ]; then
120                                 proto_block_restart "$interface"
121                         fi
122                 ;;
123                 *)
124                         proto_notify_error "$interface" "$errorstring"
125                 ;;
126         esac
127
128         proto_kill_command "$interface"
129 }
130
131 # PPP on serial device
132
133 proto_ppp_init_config() {
134         proto_config_add_string "device"
135         ppp_generic_init_config
136         no_device=1
137         available=1
138         lasterror=1
139 }
140
141 proto_ppp_setup() {
142         local config="$1"
143
144         json_get_var device device
145         ppp_generic_setup "$config" "$device"
146 }
147
148 proto_ppp_teardown() {
149         ppp_generic_teardown "$@"
150 }
151
152 proto_pppoe_init_config() {
153         ppp_generic_init_config
154         proto_config_add_string "ac"
155         proto_config_add_string "service"
156         proto_config_add_string "host_uniq"
157         lasterror=1
158 }
159
160 proto_pppoe_setup() {
161         local config="$1"
162         local iface="$2"
163
164         for module in slhc ppp_generic pppox pppoe; do
165                 /sbin/insmod $module 2>&- >&-
166         done
167
168         json_get_var mtu mtu
169         mtu="${mtu:-1492}"
170
171         json_get_var ac ac
172         json_get_var service service
173         json_get_var host_uniq host_uniq
174
175         ppp_generic_setup "$config" \
176                 plugin rp-pppoe.so \
177                 ${ac:+rp_pppoe_ac "$ac"} \
178                 ${service:+rp_pppoe_service "$service"} \
179                 ${host_uniq:+host-uniq "$host_uniq"} \
180                 "nic-$iface"
181 }
182
183 proto_pppoe_teardown() {
184         ppp_generic_teardown "$@"
185 }
186
187 proto_pppoa_init_config() {
188         ppp_generic_init_config
189         proto_config_add_int "atmdev"
190         proto_config_add_int "vci"
191         proto_config_add_int "vpi"
192         proto_config_add_string "encaps"
193         no_device=1
194         available=1
195         lasterror=1
196 }
197
198 proto_pppoa_setup() {
199         local config="$1"
200         local iface="$2"
201
202         for module in slhc ppp_generic pppox pppoatm; do
203                 /sbin/insmod $module 2>&- >&-
204         done
205
206         json_get_vars atmdev vci vpi encaps
207
208         case "$encaps" in
209                 1|vc) encaps="vc-encaps" ;;
210                 *) encaps="llc-encaps" ;;
211         esac
212
213         ppp_generic_setup "$config" \
214                 plugin pppoatm.so \
215                 ${atmdev:+$atmdev.}${vpi:-8}.${vci:-35} \
216                 ${encaps}
217 }
218
219 proto_pppoa_teardown() {
220         ppp_generic_teardown "$@"
221 }
222
223 proto_pptp_init_config() {
224         ppp_generic_init_config
225         proto_config_add_string "server"
226         proto_config_add_string "interface"
227         available=1
228         no_device=1
229         lasterror=1
230 }
231
232 proto_pptp_setup() {
233         local config="$1"
234         local iface="$2"
235
236         local ip serv_addr server interface
237         json_get_vars interface server
238         [ -n "$server" ] && {
239                 for ip in $(resolveip -t 5 "$server"); do
240                         ( proto_add_host_dependency "$config" "$ip" $interface )
241                         serv_addr=1
242                 done
243         }
244         [ -n "$serv_addr" ] || {
245                 echo "Could not resolve server address"
246                 sleep 5
247                 proto_setup_failed "$config"
248                 exit 1
249         }
250
251         local load
252         for module in slhc ppp_generic ppp_async ppp_mppe ip_gre gre pptp; do
253                 grep -q "^$module " /proc/modules && continue
254                 /sbin/insmod $module 2>&- >&-
255                 load=1
256         done
257         [ "$load" = "1" ] && sleep 1
258
259         ppp_generic_setup "$config" \
260                 plugin pptp.so \
261                 pptp_server $server \
262                 file /etc/ppp/options.pptp
263 }
264
265 proto_pptp_teardown() {
266         ppp_generic_teardown "$@"
267 }
268
269 [ -n "$INCLUDE_ONLY" ] || {
270         add_protocol ppp
271         [ -f /usr/lib/pppd/*/rp-pppoe.so ] && add_protocol pppoe
272         [ -f /usr/lib/pppd/*/pppoatm.so ] && add_protocol pppoa
273         [ -f /usr/lib/pppd/*/pptp.so ] && add_protocol pptp
274 }
275