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