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