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