ppp: add unconditional autoipv6-trigger
[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_string 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 if [ "$ipv6" = 0 ]; then
30 ipv6=""
31 elif [ -z "$ipv6" -o "$ipv6" = auto ]; then
32 ipv6=1
33 proto_export "AUTOIPV6=1"
34 fi
35
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 [ "${keepalive:-0}" -lt 1 ] && keepalive=""
42 [ -n "$mtu" ] || json_get_var mtu mtu
43 [ -n "$pppname" ] || pppname="${proto:-ppp}-$config"
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 "$pppname" \
53 ${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}} \
54 ${ipv6:++ipv6} \
55 nodefaultroute \
56 usepeerdns \
57 $demand maxfail 1 \
58 ${username:+user "$username" password "$password"} \
59 ${connect:+connect "$connect"} \
60 ${disconnect:+disconnect "$disconnect"} \
61 ip-up-script /lib/netifd/ppp-up \
62 ipv6-up-script /lib/netifd/ppp-up \
63 ip-down-script /lib/netifd/ppp-down \
64 ipv6-down-script /lib/netifd/ppp-down \
65 ${mtu:+mtu $mtu mru $mtu} \
66 "$@" $pppd_options
67 }
68
69 ppp_generic_teardown() {
70 local interface="$1"
71
72 case "$ERROR" in
73 11|19)
74 proto_notify_error "$interface" AUTH_FAILED
75 json_get_var authfail authfail
76 if [ "${authfail:-0}" -gt 0 ]; then
77 proto_block_restart "$interface"
78 fi
79 ;;
80 2)
81 proto_notify_error "$interface" INVALID_OPTIONS
82 proto_block_restart "$interface"
83 ;;
84 esac
85 proto_kill_command "$interface"
86 }
87
88 # PPP on serial device
89
90 proto_ppp_init_config() {
91 proto_config_add_string "device"
92 ppp_generic_init_config
93 no_device=1
94 available=1
95 }
96
97 proto_ppp_setup() {
98 local config="$1"
99
100 json_get_var device device
101 ppp_generic_setup "$config" "$device"
102 }
103
104 proto_ppp_teardown() {
105 ppp_generic_teardown "$@"
106 }
107
108 proto_pppoe_init_config() {
109 ppp_generic_init_config
110 proto_config_add_string "ac"
111 proto_config_add_string "service"
112 }
113
114 proto_pppoe_setup() {
115 local config="$1"
116 local iface="$2"
117
118 for module in slhc ppp_generic pppox pppoe; do
119 /sbin/insmod $module 2>&- >&-
120 done
121
122 json_get_var mtu mtu
123 mtu="${mtu:-1492}"
124
125 json_get_var ac ac
126 json_get_var service service
127
128 ppp_generic_setup "$config" \
129 plugin rp-pppoe.so \
130 ${ac:+rp_pppoe_ac "$ac"} \
131 ${service:+rp_pppoe_service "$service"} \
132 "nic-$iface"
133 }
134
135 proto_pppoe_teardown() {
136 ppp_generic_teardown "$@"
137 }
138
139 proto_pppoa_init_config() {
140 ppp_generic_init_config
141 proto_config_add_int "atmdev"
142 proto_config_add_int "vci"
143 proto_config_add_int "vpi"
144 proto_config_add_string "encaps"
145 no_device=1
146 available=1
147 }
148
149 proto_pppoa_setup() {
150 local config="$1"
151 local iface="$2"
152
153 for module in slhc ppp_generic pppox pppoatm; do
154 /sbin/insmod $module 2>&- >&-
155 done
156
157 json_get_vars atmdev vci vpi encaps
158
159 case "$encaps" in
160 1|vc) encaps="vc-encaps" ;;
161 *) encaps="llc-encaps" ;;
162 esac
163
164 ppp_generic_setup "$config" \
165 plugin pppoatm.so \
166 ${atmdev:+$atmdev.}${vpi:-8}.${vci:-35} \
167 ${encaps}
168 }
169
170 proto_pppoa_teardown() {
171 ppp_generic_teardown "$@"
172 }
173
174 proto_pptp_init_config() {
175 ppp_generic_init_config
176 proto_config_add_string "server"
177 available=1
178 no_device=1
179 }
180
181 proto_pptp_setup() {
182 local config="$1"
183 local iface="$2"
184
185 local ip serv_addr server
186 json_get_var server server && {
187 for ip in $(resolveip -t 5 "$server"); do
188 ( proto_add_host_dependency "$config" "$ip" )
189 serv_addr=1
190 done
191 }
192 [ -n "$serv_addr" ] || {
193 echo "Could not resolve server address"
194 sleep 5
195 proto_setup_failed "$config"
196 exit 1
197 }
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