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

Archive Download this file



interactive