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