ppp: add sourcefilter option support
[openwrt/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 . /lib/functions/network.sh
8 . ../netifd-proto.sh
9 init_proto "$@"
10 }
11
12 ppp_select_ipaddr()
13 {
14 local subnets=$1
15 local res
16 local res_mask
17
18 for subnet in $subnets; do
19 local addr="${subnet%%/*}"
20 local mask="${subnet#*/}"
21
22 if [ -n "$res_mask" -a "$mask" != 32 ]; then
23 [ "$mask" -gt "$res_mask" ] || [ "$res_mask" = 32 ] && {
24 res="$addr"
25 res_mask="$mask"
26 }
27 elif [ -z "$res_mask" ]; then
28 res="$addr"
29 res_mask="$mask"
30 fi
31 done
32
33 echo "$res"
34 }
35
36 ppp_exitcode_tostring()
37 {
38 local errorcode=$1
39 [ -n "$errorcode" ] || errorcode=5
40
41 case "$errorcode" in
42 0) echo "OK" ;;
43 1) echo "FATAL_ERROR" ;;
44 2) echo "OPTION_ERROR" ;;
45 3) echo "NOT_ROOT" ;;
46 4) echo "NO_KERNEL_SUPPORT" ;;
47 5) echo "USER_REQUEST" ;;
48 6) echo "LOCK_FAILED" ;;
49 7) echo "OPEN_FAILED" ;;
50 8) echo "CONNECT_FAILED" ;;
51 9) echo "PTYCMD_FAILED" ;;
52 10) echo "NEGOTIATION_FAILED" ;;
53 11) echo "PEER_AUTH_FAILED" ;;
54 12) echo "IDLE_TIMEOUT" ;;
55 13) echo "CONNECT_TIME" ;;
56 14) echo "CALLBACK" ;;
57 15) echo "PEER_DEAD" ;;
58 16) echo "HANGUP" ;;
59 17) echo "LOOPBACK" ;;
60 18) echo "INIT_FAILED" ;;
61 19) echo "AUTH_TOPEER_FAILED" ;;
62 20) echo "TRAFFIC_LIMIT" ;;
63 21) echo "CNID_AUTH_FAILED";;
64 *) echo "UNKNOWN_ERROR" ;;
65 esac
66 }
67
68 ppp_generic_init_config() {
69 proto_config_add_string username
70 proto_config_add_string password
71 proto_config_add_string keepalive
72 proto_config_add_boolean keepalive_adaptive
73 proto_config_add_int demand
74 proto_config_add_string pppd_options
75 proto_config_add_string 'connect:file'
76 proto_config_add_string 'disconnect:file'
77 [ -e /proc/sys/net/ipv6 ] && proto_config_add_string ipv6
78 proto_config_add_boolean authfail
79 proto_config_add_int mtu
80 proto_config_add_string pppname
81 proto_config_add_string unnumbered
82 proto_config_add_boolean persist
83 proto_config_add_int maxfail
84 proto_config_add_int holdoff
85 proto_config_add_boolean sourcefilter
86 }
87
88 ppp_generic_setup() {
89 local config="$1"; shift
90 local localip
91
92 json_get_vars ip6table demand keepalive keepalive_adaptive username password pppd_options pppname unnumbered persist maxfail holdoff peerdns sourcefilter
93
94 [ ! -e /proc/sys/net/ipv6 ] && ipv6=0 || json_get_var ipv6 ipv6
95
96 if [ "$ipv6" = 0 ]; then
97 ipv6=""
98 elif [ -z "$ipv6" -o "$ipv6" = auto ]; then
99 ipv6=1
100 autoipv6=1
101 fi
102
103 if [ "${demand:-0}" -gt 0 ]; then
104 demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
105 else
106 demand=""
107 fi
108 if [ -n "$persist" ]; then
109 [ "${persist}" -lt 1 ] && persist="nopersist" || persist="persist"
110 fi
111 if [ -z "$maxfail" ]; then
112 [ "$persist" = "persist" ] && maxfail=0 || maxfail=1
113 fi
114 [ -n "$mtu" ] || json_get_var mtu mtu
115 [ -n "$pppname" ] || pppname="${proto:-ppp}-$config"
116 [ -n "$unnumbered" ] && {
117 local subnets
118 ( proto_add_host_dependency "$config" "" "$unnumbered" )
119 network_get_subnets subnets "$unnumbered"
120 localip=$(ppp_select_ipaddr "$subnets")
121 [ -n "$localip" ] || {
122 proto_block_restart "$config"
123 return
124 }
125 }
126
127 [ -n "$keepalive" ] || keepalive="5 1"
128
129 local lcp_failure="${keepalive%%[, ]*}"
130 local lcp_interval="${keepalive##*[, ]}"
131 local lcp_adaptive="lcp-echo-adaptive"
132 [ "${lcp_failure:-0}" -lt 1 ] && lcp_failure=""
133 [ "$lcp_interval" != "$keepalive" ] || lcp_interval=5
134 [ "${keepalive_adaptive:-1}" -lt 1 ] && lcp_adaptive=""
135 [ -n "$connect" ] || json_get_var connect connect
136 [ -n "$disconnect" ] || json_get_var disconnect disconnect
137 [ "$sourcefilter" = "0" ] || sourcefilter=""
138
139 proto_run_command "$config" /usr/sbin/pppd \
140 nodetach ipparam "$config" \
141 ifname "$pppname" \
142 ${localip:+$localip:} \
143 ${lcp_failure:+lcp-echo-interval $lcp_interval lcp-echo-failure $lcp_failure $lcp_adaptive} \
144 ${ipv6:++ipv6} \
145 ${autoipv6:+set AUTOIPV6=1} \
146 ${ip6table:+set IP6TABLE=$ip6table} \
147 ${peerdns:+set PEERDNS=$peerdns} \
148 ${sourcefilter:+set NOSOURCEFILTER=1} \
149 nodefaultroute \
150 usepeerdns \
151 $demand $persist maxfail $maxfail \
152 ${holdoff:+holdoff "$holdoff"} \
153 ${username:+user "$username" password "$password"} \
154 ${connect:+connect "$connect"} \
155 ${disconnect:+disconnect "$disconnect"} \
156 ip-up-script /lib/netifd/ppp-up \
157 ${ipv6:+ipv6-up-script /lib/netifd/ppp6-up} \
158 ip-down-script /lib/netifd/ppp-down \
159 ${ipv6:+ipv6-down-script /lib/netifd/ppp-down} \
160 ${mtu:+mtu $mtu mru $mtu} \
161 "$@" $pppd_options
162 }
163
164 ppp_generic_teardown() {
165 local interface="$1"
166 local errorstring=$(ppp_exitcode_tostring $ERROR)
167
168 case "$ERROR" in
169 0)
170 ;;
171 2)
172 proto_notify_error "$interface" "$errorstring"
173 proto_block_restart "$interface"
174 ;;
175 11|19)
176 json_get_var authfail authfail
177 proto_notify_error "$interface" "$errorstring"
178 if [ "${authfail:-0}" -gt 0 ]; then
179 proto_block_restart "$interface"
180 fi
181 ;;
182 *)
183 proto_notify_error "$interface" "$errorstring"
184 ;;
185 esac
186
187 proto_kill_command "$interface"
188 }
189
190 # PPP on serial device
191
192 proto_ppp_init_config() {
193 proto_config_add_string "device"
194 ppp_generic_init_config
195 no_device=1
196 available=1
197 lasterror=1
198 }
199
200 proto_ppp_setup() {
201 local config="$1"
202
203 json_get_var device device
204 ppp_generic_setup "$config" "$device"
205 }
206
207 proto_ppp_teardown() {
208 ppp_generic_teardown "$@"
209 }
210
211 proto_pppoe_init_config() {
212 ppp_generic_init_config
213 proto_config_add_string "ac"
214 proto_config_add_string "service"
215 proto_config_add_string "host_uniq"
216 proto_config_add_int "padi_attempts"
217 proto_config_add_int "padi_timeout"
218
219 lasterror=1
220 }
221
222 proto_pppoe_setup() {
223 local config="$1"
224 local iface="$2"
225
226 /sbin/modprobe -qa slhc ppp_generic pppox pppoe
227
228 json_get_var mtu mtu
229 mtu="${mtu:-1492}"
230
231 json_get_var ac ac
232 json_get_var service service
233 json_get_var host_uniq host_uniq
234 json_get_var padi_attempts padi_attempts
235 json_get_var padi_timeout padi_timeout
236
237 ppp_generic_setup "$config" \
238 plugin pppoe.so \
239 ${ac:+rp_pppoe_ac "$ac"} \
240 ${service:+rp_pppoe_service "$service"} \
241 ${host_uniq:+host-uniq "$host_uniq"} \
242 ${padi_attempts:+pppoe-padi-attempts $padi_attempts} \
243 ${padi_timeout:+pppoe-padi-timeout $padi_timeout} \
244 "nic-$iface"
245 }
246
247 proto_pppoe_teardown() {
248 ppp_generic_teardown "$@"
249 }
250
251 proto_pppoa_init_config() {
252 ppp_generic_init_config
253 proto_config_add_int "atmdev"
254 proto_config_add_int "vci"
255 proto_config_add_int "vpi"
256 proto_config_add_string "encaps"
257 no_device=1
258 available=1
259 lasterror=1
260 }
261
262 proto_pppoa_setup() {
263 local config="$1"
264 local iface="$2"
265
266 /sbin/modprobe -qa slhc ppp_generic pppox pppoatm
267
268 json_get_vars atmdev vci vpi encaps
269
270 case "$encaps" in
271 1|vc) encaps="vc-encaps" ;;
272 *) encaps="llc-encaps" ;;
273 esac
274
275 ppp_generic_setup "$config" \
276 plugin pppoatm.so \
277 ${atmdev:+$atmdev.}${vpi:-8}.${vci:-35} \
278 ${encaps}
279 }
280
281 proto_pppoa_teardown() {
282 ppp_generic_teardown "$@"
283 }
284
285 proto_pptp_init_config() {
286 ppp_generic_init_config
287 proto_config_add_string "server"
288 proto_config_add_string "interface"
289 available=1
290 no_device=1
291 lasterror=1
292 }
293
294 proto_pptp_setup() {
295 local config="$1"
296 local iface="$2"
297
298 local ip serv_addr server interface
299 json_get_vars interface server
300 [ -n "$server" ] && {
301 for ip in $(resolveip -t 5 "$server"); do
302 ( proto_add_host_dependency "$config" "$ip" $interface )
303 serv_addr=1
304 done
305 }
306 [ -n "$serv_addr" ] || {
307 echo "Could not resolve server address"
308 sleep 5
309 proto_setup_failed "$config"
310 exit 1
311 }
312
313 /sbin/modprobe -qa slhc ppp_generic ppp_async ppp_mppe ip_gre gre pptp
314 sleep 1
315
316 ppp_generic_setup "$config" \
317 plugin pptp.so \
318 pptp_server $server \
319 file /etc/ppp/options.pptp
320 }
321
322 proto_pptp_teardown() {
323 ppp_generic_teardown "$@"
324 }
325
326 [ -n "$INCLUDE_ONLY" ] || {
327 add_protocol ppp
328 [ -f /usr/lib/pppd/*/pppoe.so ] && add_protocol pppoe
329 [ -f /usr/lib/pppd/*/pppoatm.so ] && add_protocol pppoa
330 [ -f /usr/lib/pppd/*/pptp.so ] && add_protocol pptp
331 }