add chaos_calmer branch
[15.05/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         . /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         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 }
83
84 ppp_generic_setup() {
85         local config="$1"; shift
86         local localip
87
88         json_get_vars ipv6 demand keepalive keepalive_adaptive username password pppd_options pppname unnumbered
89         if [ "$ipv6" = 0 ]; then
90                 ipv6=""
91         elif [ -z "$ipv6" -o "$ipv6" = auto ]; then
92                 ipv6=1
93                 proto_export "AUTOIPV6=1"
94         fi
95
96         if [ "${demand:-0}" -gt 0 ]; then
97                 demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
98         else
99                 demand=""
100         fi
101         [ -n "$mtu" ] || json_get_var mtu mtu
102         [ -n "$pppname" ] || pppname="${proto:-ppp}-$config"
103         [ -n "$unnumbered" ] && {
104                 local subnets
105                 ( proto_add_host_dependency "$config" "" "$unnumbered" )
106                 network_get_subnets subnets "$unnumbered"
107                 localip=$(ppp_select_ipaddr "$subnets")
108                 [ -n "$localip" ] || {
109                         proto_block_restart "$config"
110                         return
111                 }
112         }
113
114         local lcp_failure="${keepalive%%[, ]*}"
115         local lcp_interval="${keepalive##*[, ]}"
116         local lcp_adaptive="lcp-echo-adaptive"
117         [ "${lcp_failure:-0}" -lt 1 ] && lcp_failure=""
118         [ "$lcp_interval" != "$keepalive" ] || lcp_interval=5
119         [ "${keepalive_adaptive:-1}" -lt 1 ] && lcp_adaptive=""
120         [ -n "$connect" ] || json_get_var connect connect
121         [ -n "$disconnect" ] || json_get_var disconnect disconnect
122
123         proto_run_command "$config" /usr/sbin/pppd \
124                 nodetach ipparam "$config" \
125                 ifname "$pppname" \
126                 ${localip:+$localip:} \
127                 ${lcp_failure:+lcp-echo-interval $lcp_interval lcp-echo-failure $lcp_failure $lcp_adaptive} \
128                 ${ipv6:++ipv6} \
129                 nodefaultroute \
130                 usepeerdns \
131                 $demand maxfail 1 \
132                 ${username:+user "$username" password "$password"} \
133                 ${connect:+connect "$connect"} \
134                 ${disconnect:+disconnect "$disconnect"} \
135                 ip-up-script /lib/netifd/ppp-up \
136                 ipv6-up-script /lib/netifd/ppp-up \
137                 ip-down-script /lib/netifd/ppp-down \
138                 ipv6-down-script /lib/netifd/ppp-down \
139                 ${mtu:+mtu $mtu mru $mtu} \
140                 "$@" $pppd_options
141 }
142
143 ppp_generic_teardown() {
144         local interface="$1"
145         local errorstring=$(ppp_exitcode_tostring $ERROR)
146
147         case "$ERROR" in
148                 0)
149                 ;;
150                 2)
151                         proto_notify_error "$interface" "$errorstring"
152                         proto_block_restart "$interface"
153                 ;;
154                 11|19)
155                         json_get_var authfail authfail
156                         proto_notify_error "$interface" "$errorstring"
157                         if [ "${authfail:-0}" -gt 0 ]; then
158                                 proto_block_restart "$interface"
159                         fi
160                 ;;
161                 *)
162                         proto_notify_error "$interface" "$errorstring"
163                 ;;
164         esac
165
166         proto_kill_command "$interface"
167 }
168
169 # PPP on serial device
170
171 proto_ppp_init_config() {
172         proto_config_add_string "device"
173         ppp_generic_init_config
174         no_device=1
175         available=1
176         lasterror=1
177 }
178
179 proto_ppp_setup() {
180         local config="$1"
181
182         json_get_var device device
183         ppp_generic_setup "$config" "$device"
184 }
185
186 proto_ppp_teardown() {
187         ppp_generic_teardown "$@"
188 }
189
190 proto_pppoe_init_config() {
191         ppp_generic_init_config
192         proto_config_add_string "ac"
193         proto_config_add_string "service"
194         proto_config_add_string "host_uniq"
195         lasterror=1
196 }
197
198 proto_pppoe_setup() {
199         local config="$1"
200         local iface="$2"
201
202         for module in slhc ppp_generic pppox pppoe; do
203                 /sbin/insmod $module 2>&- >&-
204         done
205
206         json_get_var mtu mtu
207         mtu="${mtu:-1492}"
208
209         json_get_var ac ac
210         json_get_var service service
211         json_get_var host_uniq host_uniq
212
213         ppp_generic_setup "$config" \
214                 plugin rp-pppoe.so \
215                 ${ac:+rp_pppoe_ac "$ac"} \
216                 ${service:+rp_pppoe_service "$service"} \
217                 ${host_uniq:+host-uniq "$host_uniq"} \
218                 "nic-$iface"
219 }
220
221 proto_pppoe_teardown() {
222         ppp_generic_teardown "$@"
223 }
224
225 proto_pppoa_init_config() {
226         ppp_generic_init_config
227         proto_config_add_int "atmdev"
228         proto_config_add_int "vci"
229         proto_config_add_int "vpi"
230         proto_config_add_string "encaps"
231         no_device=1
232         available=1
233         lasterror=1
234 }
235
236 proto_pppoa_setup() {
237         local config="$1"
238         local iface="$2"
239
240         for module in slhc ppp_generic pppox pppoatm; do
241                 /sbin/insmod $module 2>&- >&-
242         done
243
244         json_get_vars atmdev vci vpi encaps
245
246         case "$encaps" in
247                 1|vc) encaps="vc-encaps" ;;
248                 *) encaps="llc-encaps" ;;
249         esac
250
251         ppp_generic_setup "$config" \
252                 plugin pppoatm.so \
253                 ${atmdev:+$atmdev.}${vpi:-8}.${vci:-35} \
254                 ${encaps}
255 }
256
257 proto_pppoa_teardown() {
258         ppp_generic_teardown "$@"
259 }
260
261 proto_pptp_init_config() {
262         ppp_generic_init_config
263         proto_config_add_string "server"
264         proto_config_add_string "interface"
265         available=1
266         no_device=1
267         lasterror=1
268 }
269
270 proto_pptp_setup() {
271         local config="$1"
272         local iface="$2"
273
274         local ip serv_addr server interface
275         json_get_vars interface server
276         [ -n "$server" ] && {
277                 for ip in $(resolveip -t 5 "$server"); do
278                         ( proto_add_host_dependency "$config" "$ip" $interface )
279                         serv_addr=1
280                 done
281         }
282         [ -n "$serv_addr" ] || {
283                 echo "Could not resolve server address"
284                 sleep 5
285                 proto_setup_failed "$config"
286                 exit 1
287         }
288
289         local load
290         for module in slhc ppp_generic ppp_async ppp_mppe ip_gre gre pptp; do
291                 grep -q "^$module " /proc/modules && continue
292                 /sbin/insmod $module 2>&- >&-
293                 load=1
294         done
295         [ "$load" = "1" ] && sleep 1
296
297         ppp_generic_setup "$config" \
298                 plugin pptp.so \
299                 pptp_server $server \
300                 file /etc/ppp/options.pptp
301 }
302
303 proto_pptp_teardown() {
304         ppp_generic_teardown "$@"
305 }
306
307 [ -n "$INCLUDE_ONLY" ] || {
308         add_protocol ppp
309         [ -f /usr/lib/pppd/*/rp-pppoe.so ] && add_protocol pppoe
310         [ -f /usr/lib/pppd/*/pppoatm.so ] && add_protocol pppoa
311         [ -f /usr/lib/pppd/*/pptp.so ] && add_protocol pptp
312 }
313