ppp: Allow PPTP over a specified interface
[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         proto_config_add_string "host_uniq"
117 }
118
119 proto_pppoe_setup() {
120         local config="$1"
121         local iface="$2"
122
123         for module in slhc ppp_generic pppox pppoe; do
124                 /sbin/insmod $module 2>&- >&-
125         done
126
127         json_get_var mtu mtu
128         mtu="${mtu:-1492}"
129
130         json_get_var ac ac
131         json_get_var service service
132         json_get_var host_uniq host_uniq
133
134         ppp_generic_setup "$config" \
135                 plugin rp-pppoe.so \
136                 ${ac:+rp_pppoe_ac "$ac"} \
137                 ${service:+rp_pppoe_service "$service"} \
138                 ${host_uniq:+host-uniq "$host_uniq"} \
139                 "nic-$iface"
140 }
141
142 proto_pppoe_teardown() {
143         ppp_generic_teardown "$@"
144 }
145
146 proto_pppoa_init_config() {
147         ppp_generic_init_config
148         proto_config_add_int "atmdev"
149         proto_config_add_int "vci"
150         proto_config_add_int "vpi"
151         proto_config_add_string "encaps"
152         no_device=1
153         available=1
154 }
155
156 proto_pppoa_setup() {
157         local config="$1"
158         local iface="$2"
159
160         for module in slhc ppp_generic pppox pppoatm; do
161                 /sbin/insmod $module 2>&- >&-
162         done
163
164         json_get_vars atmdev vci vpi encaps
165
166         case "$encaps" in
167                 1|vc) encaps="vc-encaps" ;;
168                 *) encaps="llc-encaps" ;;
169         esac
170
171         ppp_generic_setup "$config" \
172                 plugin pppoatm.so \
173                 ${atmdev:+$atmdev.}${vpi:-8}.${vci:-35} \
174                 ${encaps}
175 }
176
177 proto_pppoa_teardown() {
178         ppp_generic_teardown "$@"
179 }
180
181 proto_pptp_init_config() {
182         ppp_generic_init_config
183         proto_config_add_string "server"
184         proto_config_add_string "interface"
185         available=1
186         no_device=1
187 }
188
189 proto_pptp_setup() {
190         local config="$1"
191         local iface="$2"
192
193         local ip serv_addr server interface
194         json_get_vars interface server
195         [ -n "$server" ] && {
196                 for ip in $(resolveip -t 5 "$server"); do
197                         ( proto_add_host_dependency "$config" "$ip" $interface )
198                         serv_addr=1
199                 done
200         }
201         [ -n "$serv_addr" ] || {
202                 echo "Could not resolve server address"
203                 sleep 5
204                 proto_setup_failed "$config"
205                 exit 1
206         }
207
208         local load
209         for module in slhc ppp_generic ppp_async ppp_mppe ip_gre gre pptp; do
210                 grep -q "^$module " /proc/modules && continue
211                 /sbin/insmod $module 2>&- >&-
212                 load=1
213         done
214         [ "$load" = "1" ] && sleep 1
215
216         ppp_generic_setup "$config" \
217                 plugin pptp.so \
218                 pptp_server $server \
219                 file /etc/ppp/options.pptp
220 }
221
222 proto_pptp_teardown() {
223         ppp_generic_teardown "$@"
224 }
225
226 [ -n "$INCLUDE_ONLY" ] || {
227         add_protocol ppp
228         [ -f /usr/lib/pppd/*/rp-pppoe.so ] && add_protocol pppoe
229         [ -f /usr/lib/pppd/*/pppoatm.so ] && add_protocol pppoa
230         [ -f /usr/lib/pppd/*/pptp.so ] && add_protocol pptp
231 }
232