[package] pppd:
[openwrt.git] / package / ppp / files / ppp.sh
1 scan_ppp() {
2 config_get ifname "$1" ifname
3 pppdev="${pppdev:-0}"
4 config_get devunit "$1" unit
5 {
6 unit=
7 pppif=
8 if [ ! -d /tmp/.ppp-counter ]; then
9 mkdir -p /tmp/.ppp-counter
10 fi
11 local maxunit
12 maxunit="$(cat /tmp/.ppp-counter/max-unit 2>/dev/null)"
13 if [ -z "$maxunit" ]; then
14 maxunit=-1
15 fi
16 local i
17 i=0
18 while [ $i -le $maxunit ]; do
19 local unitdev
20 unitdev="$(cat /tmp/.ppp-counter/ppp${i} 2>/dev/null)"
21 if [ "$unitdev" = "$1" ]; then
22 unit="$i"
23 pppif="ppp${i}"
24 break
25 fi
26 i="$(($i + 1))"
27 done
28 if [ -z "$unit" ] || [ -z "$pppif" ]; then
29 maxunit="$(($maxunit + 1))"
30 if [ -n "$devunit" ]; then
31 unit="$devunit"
32 elif [ "${ifname%%[0-9]*}" = ppp ]; then
33 unit="${ifname##ppp}"
34 else
35 unit="$maxunit"
36 fi
37 [ "$maxunit" -lt "$unit" ] && maxunit="$unit"
38 pppif="ppp${unit}"
39 echo "$1" >/tmp/.ppp-counter/$pppif 2>/dev/null
40 echo "$maxunit" >/tmp/.ppp-counter/max-unit 2>/dev/null
41 fi
42 config_set "$1" ifname "ppp$unit"
43 config_set "$1" unit "$unit"
44 }
45 }
46
47 start_pppd() {
48 local cfg="$1"; shift
49 local ifname
50
51 # make sure the network state references the correct ifname
52 scan_ppp "$cfg"
53 config_get ifname "$cfg" ifname
54 set_interface_ifname "$cfg" "$ifname"
55
56 # make sure only one pppd process is started
57 lock "/var/lock/ppp-${cfg}"
58 local pid="$(head -n1 /var/run/ppp-${cfg}.pid 2>/dev/null)"
59 [ -d "/proc/$pid" ] && grep pppd "/proc/$pid/cmdline" 2>/dev/null >/dev/null && {
60 lock -u "/var/lock/ppp-${cfg}"
61 return 0
62 }
63
64 # Workaround: sometimes hotplug2 doesn't deliver the hotplug event for creating
65 # /dev/ppp fast enough to be used here
66 [ -e /dev/ppp ] || mknod /dev/ppp c 108 0
67
68 local device
69 config_get device "$cfg" device
70
71 local unit
72 config_get unit "$cfg" unit
73
74 local username
75 config_get username "$cfg" username
76
77 local password
78 config_get password "$cfg" password
79
80 local keepalive
81 config_get keepalive "$cfg" keepalive
82
83 local connect
84 config_get connect "$cfg" connect
85
86 local disconnect
87 config_get disconnect "$cfg" disconnect
88
89 local pppd_options
90 config_get pppd_options "$cfg" pppd_options
91
92 local defaultroute
93 config_get_bool defaultroute "$cfg" defaultroute 1
94 [ "$defaultroute" -eq 1 ] && defaultroute="defaultroute replacedefaultroute" || defaultroute=""
95
96 local interval="${keepalive##*[, ]}"
97 [ "$interval" != "$keepalive" ] || interval=5
98
99 local dns
100 config_get dns "$config" dns
101
102 local has_dns=0
103 local peer_default=1
104 [ -n "$dns" ] && {
105 has_dns=1
106 peer_default=0
107 }
108
109 local peerdns
110 config_get_bool peerdns "$cfg" peerdns $peer_default
111
112 echo -n "" > /tmp/resolv.conf.auto
113
114 [ "$peerdns" -eq 1 ] && {
115 peerdns="usepeerdns"
116 } || {
117 peerdns=""
118 for dns in $dns; do
119 echo "nameserver $dns" >> /tmp/resolv.conf.auto
120 done
121 }
122
123 local demand
124 config_get_bool demand "$cfg" demand 0
125
126 local demandargs
127 [ "$demand" -eq 1 ] && {
128 demandargs="precompiled-active-filter /etc/ppp/filter demand idle"
129 [ "$has_dns" -eq 0 ] && \
130 echo "nameserver 1.1.1.1" > /tmp/resolv.conf.auto
131 } || {
132 demandargs="persist"
133 }
134
135 local ipv6
136 config_get_bool ipv6 "$cfg" ipv6 0
137 [ "$ipv6" -eq 1 ] && ipv6="+ipv6" || ipv6=""
138
139 /usr/sbin/pppd "$@" \
140 ${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}} \
141 $demandargs \
142 $peerdns \
143 $defaultroute \
144 ${username:+user "$username" password "$password"} \
145 unit "$unit" \
146 linkname "$cfg" \
147 ipparam "$cfg" \
148 ${connect:+connect "$connect"} \
149 ${disconnect:+disconnect "$disconnect"} \
150 ${ipv6} \
151 ${pppd_options}
152
153 lock -u "/var/lock/ppp-${cfg}"
154 }
155
156 setup_interface_ppp() {
157 local iface="$1"
158 local config="$2"
159
160 local device
161 config_get device "$config" device
162
163 local mtu
164 config_get mtu "$config" mtu
165
166 mtu=${mtu:-1492}
167 start_pppd "$config" \
168 mtu $mtu mru $mtu \
169 "$device"
170 }
171
This page took 0.058388 seconds and 5 git commands to generate.