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