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