28cdb9cbd60ce9540a89e2e85680aaea21ba3792
[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         . ../netifd-proto.sh
8         init_proto "$@"
9 }
10
11 ppp_generic_init_config() {
12         proto_config_add_string username
13         proto_config_add_string password
14         proto_config_add_string keepalive
15         proto_config_add_boolean keepalive_adaptive
16         proto_config_add_int demand
17         proto_config_add_string pppd_options
18         proto_config_add_string 'connect:file'
19         proto_config_add_string 'disconnect:file'
20         proto_config_add_string ipv6
21         proto_config_add_boolean authfail
22         proto_config_add_int mtu
23         proto_config_add_string pppname
24 }
25
26 ppp_generic_setup() {
27         local config="$1"; shift
28
29         json_get_vars ipv6 demand keepalive keepalive_adaptive username password pppd_options pppname
30         if [ "$ipv6" = 0 ]; then
31                 ipv6=""
32         elif [ -z "$ipv6" -o "$ipv6" = auto ]; then
33                 ipv6=1
34                 proto_export "AUTOIPV6=1"
35         fi
36
37         if [ "${demand:-0}" -gt 0 ]; then
38                 demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
39         else
40                 demand="persist"
41         fi
42         [ -n "$mtu" ] || json_get_var mtu mtu
43         [ -n "$pppname" ] || pppname="${proto:-ppp}-$config"
44
45         local lcp_failure="${keepalive%%[, ]*}"
46         local lcp_interval="${keepalive##*[, ]}"
47         local lcp_adaptive="lcp-echo-adaptive"
48         [ "${lcp_failure:-0}" -lt 1 ] && lcp_failure=""
49         [ "$lcp_interval" != "$keepalive" ] || lcp_interval=5
50         [ "${keepalive_adaptive:-1}" -lt 1 ] && lcp_adaptive=""
51         [ -n "$connect" ] || json_get_var connect connect
52         [ -n "$disconnect" ] || json_get_var disconnect disconnect
53
54         proto_run_command "$config" /usr/sbin/pppd \
55                 nodetach ipparam "$config" \
56                 ifname "$pppname" \
57                 ${lcp_failure:+lcp-echo-interval $lcp_interval lcp-echo-failure $lcp_failure $lcp_adaptive} \
58                 ${ipv6:++ipv6} \
59                 nodefaultroute \
60                 usepeerdns \
61                 $demand maxfail 1 \
62                 ${username:+user "$username" password "$password"} \
63                 ${connect:+connect "$connect"} \
64                 ${disconnect:+disconnect "$disconnect"} \
65                 ip-up-script /lib/netifd/ppp-up \
66                 ipv6-up-script /lib/netifd/ppp-up \
67                 ip-down-script /lib/netifd/ppp-down \
68                 ipv6-down-script /lib/netifd/ppp-down \
69                 ${mtu:+mtu $mtu mru $mtu} \
70                 "$@" $pppd_options
71 }
72
73 ppp_generic_teardown() {
74         local interface="$1"
75
76         case "$ERROR" in
77                 11|19)
78                         proto_notify_error "$interface" AUTH_FAILED
79                         json_get_var authfail authfail
80                         if [ "${authfail:-0}" -gt 0 ]; then
81                                 proto_block_restart "$interface"
82                         fi
83                 ;;
84                 2)
85                         proto_notify_error "$interface" INVALID_OPTIONS
86                         proto_block_restart "$interface"
87                 ;;
88         esac
89         proto_kill_command "$interface"
90 }
91
92 # PPP on serial device
93
94 proto_ppp_init_config() {
95         proto_config_add_string "device"
96         ppp_generic_init_config
97         no_device=1
98         available=1
99 }
100
101 proto_ppp_setup() {
102         local config="$1"
103
104         json_get_var device device
105         ppp_generic_setup "$config" "$device"
106 }
107
108 proto_ppp_teardown() {
109         ppp_generic_teardown "$@"
110 }
111
112 proto_pppoe_init_config() {
113         ppp_generic_init_config
114         proto_config_add_string "ac"
115         proto_config_add_string "service"
116 }
117
118 proto_pppoe_setup() {
119         local config="$1"
120         local iface="$2"
121
122         for module in slhc ppp_generic pppox pppoe; do
123                 /sbin/insmod $module 2>&- >&-
124         done
125
126         json_get_var mtu mtu
127         mtu="${mtu:-1492}"
128
129         json_get_var ac ac
130         json_get_var service service
131
132         ppp_generic_setup "$config" \
133                 plugin rp-pppoe.so \
134                 ${ac:+rp_pppoe_ac "$ac"} \
135                 ${service:+rp_pppoe_service "$service"} \
136                 "nic-$iface"
137 }
138
139 proto_pppoe_teardown() {
140         ppp_generic_teardown "$@"
141 }
142
143 proto_pppoa_init_config() {
144         ppp_generic_init_config
145         proto_config_add_int "atmdev"
146         proto_config_add_int "vci"
147         proto_config_add_int "vpi"
148         proto_config_add_string "encaps"
149         no_device=1
150         available=1
151 }
152
153 proto_pppoa_setup() {
154         local config="$1"
155         local iface="$2"
156
157         for module in slhc ppp_generic pppox pppoatm; do
158                 /sbin/insmod $module 2>&- >&-
159         done
160
161         json_get_vars atmdev vci vpi encaps
162
163         case "$encaps" in
164                 1|vc) encaps="vc-encaps" ;;
165                 *) encaps="llc-encaps" ;;
166         esac
167
168         ppp_generic_setup "$config" \
169                 plugin pppoatm.so \
170                 ${atmdev:+$atmdev.}${vpi:-8}.${vci:-35} \
171                 ${encaps}
172 }
173
174 proto_pppoa_teardown() {
175         ppp_generic_teardown "$@"
176 }
177
178 proto_pptp_init_config() {
179         ppp_generic_init_config
180         proto_config_add_string "server"
181         available=1
182         no_device=1
183 }
184
185 proto_pptp_setup() {
186         local config="$1"
187         local iface="$2"
188
189         local ip serv_addr server
190         json_get_var server server && {
191                 for ip in $(resolveip -t 5 "$server"); do
192                         ( proto_add_host_dependency "$config" "$ip" )
193                         serv_addr=1
194                 done
195         }
196         [ -n "$serv_addr" ] || {
197                 echo "Could not resolve server address"
198                 sleep 5
199                 proto_setup_failed "$config"
200                 exit 1
201         }
202
203         local load
204         for module in slhc ppp_generic ppp_async ppp_mppe ip_gre gre pptp; do
205                 grep -q "^$module " /proc/modules && continue
206                 /sbin/insmod $module 2>&- >&-
207                 load=1
208         done
209         [ "$load" = "1" ] && sleep 1
210
211         ppp_generic_setup "$config" \
212                 plugin pptp.so \
213                 pptp_server $server \
214                 file /etc/ppp/options.pptp
215 }
216
217 proto_pptp_teardown() {
218         ppp_generic_teardown "$@"
219 }
220
221 [ -n "$INCLUDE_ONLY" ] || {
222         add_protocol ppp
223         [ -f /usr/lib/pppd/*/rp-pppoe.so ] && add_protocol pppoe
224         [ -f /usr/lib/pppd/*/pppoatm.so ] && add_protocol pppoa
225         [ -f /usr/lib/pppd/*/pptp.so ] && add_protocol pptp
226 }
227