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