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