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