package/ppp: use new service wrappers
[15.05/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 link="$proto-$cfg"
8         SERVICE_PID_FILE="/var/run/ppp-${link}.pid" \
9         service_stop /usr/sbin/pppd
10
11         remove_dns "$cfg"
12
13         local lock="/var/lock/ppp-$link"
14         [ -f "$lock" ] && lock -u "$lock"
15 }
16
17 start_pppd() {
18         local cfg="$1"; shift
19
20         # Workaround for PPPoE service and AC name options,
21         # filter out the nic-* argument and append it as last option
22         local nic=""
23         case "$1" in
24                 nic-*) nic="$1"; shift ;;
25         esac
26
27         local proto
28         config_get proto "$cfg" proto
29
30         # unique link identifier
31         local link="${proto:-ppp}-$cfg"
32
33         # make sure only one pppd process is started
34         lock "/var/lock/ppp-${link}"
35         SERVICE_PID_FILE="/var/run/ppp-${link}.pid" \
36         service_check /usr/sbin/ppd && {
37                 lock -u "/var/lock/ppp-${link}"
38                 return 0
39         }
40
41         # Workaround: sometimes hotplug2 doesn't deliver the hotplug event for creating
42         # /dev/ppp fast enough to be used here
43         [ -e /dev/ppp ] || mknod /dev/ppp c 108 0
44
45         local device
46         config_get device "$cfg" device
47
48         local username
49         config_get username "$cfg" username
50
51         local password
52         config_get password "$cfg" password
53
54         local keepalive
55         config_get keepalive "$cfg" keepalive
56
57         local connect
58         config_get connect "$cfg" connect
59
60         local disconnect
61         config_get disconnect "$cfg" disconnect
62
63         local pppd_options
64         config_get pppd_options "$cfg" pppd_options
65
66         local defaultroute
67         config_get_bool defaultroute "$cfg" defaultroute 1
68         [ "$defaultroute" -eq 1 ] && \
69                 defaultroute="defaultroute replacedefaultroute" || defaultroute="nodefaultroute"
70
71         local interval="${keepalive##*[, ]}"
72         [ "$interval" != "$keepalive" ] || interval=5
73
74         local dns
75         config_get dns "$config" dns
76
77         local has_dns=0
78         local peer_default=1
79         [ -n "$dns" ] && {
80                 has_dns=1
81                 peer_default=0
82         }
83
84         local peerdns
85         config_get_bool peerdns "$cfg" peerdns $peer_default
86
87         [ "$peerdns" -eq 1 ] && {
88                 peerdns="usepeerdns"
89         } || {
90                 peerdns=""
91                 add_dns "$cfg" $dns
92         }
93
94         local demand
95         config_get demand "$cfg" demand 0
96
97         local demandargs
98         [ "$demand" -gt 0 ] && {
99                 demandargs="precompiled-active-filter /etc/ppp/filter demand idle $demand"
100                 [ "$has_dns" -eq 0 ] && add_dns "$cfg" 1.1.1.1
101         } || {
102                 demandargs="persist"
103         }
104
105         local ipv6
106         config_get_bool ipv6 "$cfg" ipv6 0
107         [ "$ipv6" -eq 1 ] && ipv6="+ipv6" || ipv6=""
108
109         SERVICE_DAEMONIZE=1 \
110         SERVICE_WRITE_PID=1 \
111         SERVICE_PID_FILE="/var/run/ppp-$link.pid" \
112         service_start /usr/sbin/pppd "$@" \
113                 ${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}} \
114                 $demandargs \
115                 $peerdns \
116                 $defaultroute \
117                 ${username:+user "$username" password "$password"} \
118                 ipparam "$cfg" \
119                 ifname "$link" \
120                 ${connect:+connect "$connect"} \
121                 ${disconnect:+disconnect "$disconnect"} \
122                 ${ipv6} \
123                 ${pppd_options} \
124                 nodetach \
125                 ${nic}
126
127         lock -u "/var/lock/ppp-${link}"
128 }
129
130 setup_interface_ppp() {
131         local iface="$1"
132         local config="$2"
133
134         local device
135         config_get device "$config" device
136
137         local mtu
138         config_get mtu "$config" mtu
139
140         mtu=${mtu:-1492}
141         start_pppd "$config" \
142                 mtu $mtu mru $mtu \
143                 "$device"
144 }
145