ppp: enable no_device in the netifd pppoa handler to bring it up at boot time (#11631)
[openwrt.git] / package / 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"
18         proto_config_add_string "disconnect"
19         proto_config_add_boolean "defaultroute"
20         proto_config_add_boolean "peerdns"
21         proto_config_add_boolean "ipv6"
22         proto_config_add_int "mtu"
23 }
24
25 ppp_generic_setup() {
26         local config="$1"; shift
27
28         json_get_vars ipv6 peerdns defaultroute demand keepalive username password pppd_options
29         [ "$ipv6" = 1 ] || ipv6=""
30         [ "$peerdns" = 0 ] && peerdns="" || peerdns="1"
31         if [ "$defaultroute" = 1 ]; then
32                 defaultroute="defaultroute replacedefaultroute";
33         else
34                 defaultroute="nodefaultroute"
35         fi
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
42         [ -n "$mtu" ] || json_get_var mtu mtu
43
44         local interval="${keepalive##*[, ]}"
45         [ "$interval" != "$keepalive" ] || interval=5
46         [ -n "$connect" ] || json_get_var connect connect
47         [ -n "$disconnect" ] || json_get_var disconnect disconnect
48
49         proto_run_command "$config" /usr/sbin/pppd \
50                 nodetach ipparam "$config" \
51                 ifname "${proto:-ppp}-$config" \
52                 ${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}} \
53                 ${ipv6:++ipv6} $defaultroute \
54                 ${peerdns:+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                         proto_block_restart "$interface"
74                 ;;
75         esac
76         proto_kill_command "$interface"
77 }
78
79 # PPP on serial device
80
81 proto_ppp_init_config() {
82         proto_config_add_string "device"
83         ppp_generic_init_config
84         no_device=1
85         available=1
86 }
87
88 proto_ppp_setup() {
89         local config="$1"
90
91         json_get_var device device
92         ppp_generic_setup "$config" "$device"
93 }
94
95 proto_ppp_teardown() {
96         ppp_generic_teardown "$@"
97 }
98
99 proto_pppoe_init_config() {
100         ppp_generic_init_config
101         proto_config_add_string "ac"
102         proto_config_add_string "service"
103 }
104
105 proto_pppoe_setup() {
106         local config="$1"
107         local iface="$2"
108
109         for module in slhc ppp_generic pppox pppoe; do
110                 /sbin/insmod $module 2>&- >&-
111         done
112
113         json_get_var mtu mtu
114         mtu="${mtu:-1492}"
115
116         json_get_var ac ac
117         json_get_var service service
118
119         ppp_generic_setup "$config" \
120                 plugin rp-pppoe.so \
121                 ${ac:+rp_pppoe_ac "$ac"} \
122                 ${service:+rp_pppoe_service "$service"} \
123                 "nic-$iface"
124 }
125
126 proto_pppoe_teardown() {
127         ppp_generic_teardown "$@"
128 }
129
130 proto_pppoa_init_config() {
131         ppp_generic_init_config
132         proto_config_add_int "atmdev"
133         proto_config_add_int "vci"
134         proto_config_add_int "vpi"
135         proto_config_add_string "encaps"
136         no_device=1
137         available=1
138 }
139
140 proto_pppoa_setup() {
141         local config="$1"
142         local iface="$2"
143
144         for module in slhc ppp_generic pppox pppoatm; do
145                 /sbin/insmod $module 2>&- >&-
146         done
147
148         json_get_vars atmdev vci vpi encaps
149
150         case "$encaps" in
151                 1|vc) encaps="vc-encaps" ;;
152                 *) encaps="llc-encaps" ;;
153         esac
154
155         ppp_generic_setup "$config" \
156                 plugin pppoatm.so \
157                 ${atmdev:+$atmdev.}${vpi:-8}.${vci:-35} \
158                 ${encaps}
159 }
160
161 proto_pppoa_teardown() {
162         ppp_generic_teardown "$@"
163 }
164
165 [ -n "$INCLUDE_ONLY" ] || {
166         add_protocol ppp
167         [ -f /usr/lib/pppd/*/rp-pppoe.so ] && add_protocol pppoe
168         [ -f /usr/lib/pppd/*/pppoatm.so ] && add_protocol pppoa
169 }
170