]>
Commit | Line | Data |
---|---|---|
71ea0d68 | 1 | #!/bin/sh |
66c36198 PM |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
71ea0d68 SS |
21 | |
22 | . /etc/sysconfig/rc | |
23 | . $rc_functions | |
24 | ||
71ea0d68 SS |
25 | eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings) |
26 | eval $(/usr/local/bin/readhash /var/ipfire/dns/settings) | |
27 | ||
28 | dhcpcd_get_pid() { | |
29 | # This function returns the pid of a dhcpcd by a given | |
30 | # network device, if a pidfile exists. | |
31 | ||
32 | local device="$1" | |
18136c5c | 33 | local pidfile="/var/run/dhcpcd/${device}.pid" |
71ea0d68 SS |
34 | |
35 | # Check if a pid file exists. | |
36 | if [ -f "${pidfile}" ] ; then | |
37 | ||
38 | # Get the pid from the file. | |
39 | local pid="$(<"${pidfile}")" | |
40 | ||
41 | echo "${pid}" | |
42 | fi | |
43 | } | |
44 | ||
45 | dhcpcd_is_running() { | |
46 | # This functions checks if a dhcpcd is running by a given pid. | |
47 | ||
48 | local pid="$1" | |
49 | ||
50 | # Check if a dhcpcd is running. | |
51 | if [ -n "${pid}" -a -d "/proc/${pid}" ]; then | |
52 | # Return "0" (True) if a dhcpcd is running. | |
53 | return 0 | |
54 | fi | |
55 | ||
56 | # Return 1 (False) no dhcpcd is running. | |
57 | return 1 | |
58 | } | |
59 | ||
60 | dhcpcd_start() { | |
61 | # This function will start a dhcpcd on a speciefied device. | |
71ea0d68 | 62 | local device="$1" |
c6551e73 MT |
63 | shift |
64 | ||
5d0d1144 | 65 | local dhcp_start=() |
71ea0d68 SS |
66 | |
67 | boot_mesg -n "Starting dhcpcd on the ${device} interface..." | |
68 | ||
69 | # Check if a dhcpcd is already running. | |
70 | local pid="$(dhcpcd_get_pid "${device}")" | |
71 | ||
72 | if dhcpcd_is_running "${pid}"; then | |
73 | boot_mesg "dhcpcd already running!" ${WARNING} | |
74 | echo_warning | |
75 | exit 2 | |
76 | fi | |
77 | ||
78 | # Check if a DHCP hostname has been set. | |
79 | if [ -n "${RED_DHCP_HOSTNAME}" ]; then | |
5d0d1144 MT |
80 | dhcp_start+=( "-h" "${RED_DHCP_HOSTNAME}" ) |
81 | fi | |
82 | ||
83 | # Tell dhcpcd to use the configured MTU | |
84 | if [ -n "${RED_DHCP_FORCE_MTU}" ]; then | |
85 | dhcp_start+=( "--static" "mtu=${RED_DHCP_FORCE_MTU}" ) | |
71ea0d68 SS |
86 | fi |
87 | ||
c6551e73 MT |
88 | # Append any further command line options |
89 | dhcp_start+=( $@ ) | |
90 | ||
71ea0d68 | 91 | # Start dhcpcd. |
5d0d1144 | 92 | /sbin/dhcpcd "${dhcp_start[@]}" ${device} >/dev/null 2>&1 |
71ea0d68 SS |
93 | ret="$?" |
94 | ||
95 | if [ "${ret}" -eq 0 ]; then | |
96 | . /var/ipfire/dhcpc/dhcpcd-"${device}".info | |
71ea0d68 | 97 | |
f938083f AF |
98 | if [ $ip_address ]; then |
99 | echo "" | |
100 | echo_ok | |
101 | boot_mesg " DHCP Assigned Settings for ${device}:" | |
71ea0d68 | 102 | boot_mesg_flush |
f938083f AF |
103 | boot_mesg " IP Address: $ip_address" |
104 | boot_mesg_flush | |
105 | ||
106 | if [ -n "${RED_DHCP_HOSTNAME}" ]; then | |
107 | boot_mesg " Hostname: $RED_DHCP_HOSTNAME" | |
108 | boot_mesg_flush | |
109 | fi | |
71ea0d68 | 110 | |
f938083f AF |
111 | boot_mesg " Subnet Mask: $subnet_mask" |
112 | boot_mesg_flush | |
113 | boot_mesg " Default Gateway: $routers" | |
114 | boot_mesg_flush | |
115 | boot_mesg " DNS Server: $domain_name_servers" | |
116 | boot_mesg_flush | |
117 | else | |
118 | echo "" | |
119 | echo_ok | |
120 | boot_mesg "DHCP for ${device} still running..." | |
121 | boot_mesg_flush | |
122 | fi | |
71ea0d68 SS |
123 | else |
124 | echo "" | |
125 | $(exit "${ret}") | |
126 | evaluate_retval | |
127 | fi | |
128 | } | |
129 | ||
130 | dhcpcd_stop() { | |
131 | # This function stops a previously started dhcpcd on a given device. | |
132 | ||
133 | local device="$1" | |
134 | local dhcp_stop="-k" | |
135 | local leaseinfo="/var/ipfire/dhcpc/dhcpcd-${device}.info" | |
136 | ||
137 | boot_mesg -n "Stopping dhcpcd on the ${device} interface..." | |
138 | ||
139 | # Check if a dhcpcd is running. | |
140 | local pid="$(dhcpcd_get_pid "${device}")" | |
141 | ||
142 | if ! dhcpcd_is_running "${pid}"; then | |
02d67e75 | 143 | boot_mesg " Not running." ${WARNING} |
71ea0d68 SS |
144 | echo_warning |
145 | exit 1 | |
146 | fi | |
147 | ||
d43bb759 | 148 | # Stop dhcpcd. |
2e28ecea | 149 | /sbin/dhcpcd ${dhcp_stop} ${device} &> /dev/null |
d43bb759 AF |
150 | ret="$?" |
151 | ||
152 | # Wait until dhcpd has stopped. | |
153 | while [ -d "/proc/${pid}" ]; do | |
154 | sleep 1 | |
66acb7f1 AF |
155 | # repeat stop if dhcp was still running |
156 | /sbin/dhcpcd ${dhcp_stop} ${device} &> /dev/null | |
d43bb759 AF |
157 | done |
158 | ||
159 | # Display console message, depended on the exit code | |
160 | # of the stopped dhcpcd. | |
161 | if [ "${ret}" -eq 0 ]; then | |
162 | boot_mesg | |
163 | echo_ok | |
164 | elif [ "${ret}" -eq 1 ]; then | |
165 | boot_mesg "failed to stop dhcpcd!" ${WARNING} | |
166 | echo_warning | |
167 | else | |
168 | boot_mesg | |
169 | echo_failure | |
71ea0d68 SS |
170 | fi |
171 | } | |
957863f7 MT |
172 | |
173 | # QMI stuff | |
174 | ||
175 | qmi_find_device() { | |
176 | local intf="${1}" | |
177 | local _intf | |
178 | ||
179 | local path | |
180 | for path in /dev/cdc-*; do | |
181 | if [ -c "${path}" ]; then | |
182 | _intf="$(qmicli --device="${path}" --device-open-proxy --get-wwan-iface)" | |
183 | ||
184 | # Check if the interface matches | |
185 | if [ "${intf}" = "${_intf}" ]; then | |
186 | echo "${path}" | |
187 | return 0 | |
188 | fi | |
189 | fi | |
190 | done | |
191 | ||
192 | # Nothing found | |
193 | return 1 | |
194 | } | |
195 | ||
196 | qmi_enable_rawip_mode() { | |
197 | local intf="${1}" | |
198 | ||
199 | # Shut down the device first | |
200 | ip link set "${intf}" down &>/dev/null | |
201 | ||
202 | echo "Y" > "/sys/class/net/${intf}/qmi/raw_ip" | |
203 | } | |
204 | ||
205 | qmi_configure_apn() { | |
206 | local device="${1}" | |
207 | ||
208 | # APN settings | |
209 | local apn="${2}" | |
210 | local auth="${3}" | |
211 | local username="${4}" | |
212 | local password="${5}" | |
213 | ||
214 | local args=( | |
215 | # We only support IPv4 right now | |
216 | "ip-type=4" | |
217 | ) | |
218 | ||
219 | # Set APN | |
220 | if [ -n "${apn}" ]; then | |
221 | args+=( "apn=${apn}" ) | |
222 | fi | |
223 | ||
224 | # Set auth | |
225 | case "${auth}" in | |
226 | PAP|CHAP) | |
227 | args+=( "auth=${auth}" ) | |
228 | ;; | |
229 | esac | |
230 | ||
231 | # Set username | |
232 | if [ -n "${username}" ]; then | |
233 | args+=( "username=${username}" ) | |
234 | fi | |
235 | ||
236 | # Set password | |
237 | if [ -n "${password}" ]; then | |
238 | args+=( "password=${password}" ) | |
239 | fi | |
240 | ||
241 | local _args | |
242 | ||
243 | local arg | |
244 | for arg in ${args[@]}; do | |
245 | if [ -n "${_args}" ]; then | |
246 | _args="${_args}," | |
247 | fi | |
248 | _args="${_args}${arg}" | |
249 | done | |
250 | ||
251 | qmicli --device="${device}" --device-open-proxy \ | |
252 | --wds-start-network="${_args}" \ | |
253 | --client-no-release-cid | |
254 | } | |
255 | ||
256 | qmi_reset() { | |
257 | local device="${1}" | |
258 | ||
259 | qmicli --device="${device}" --device-open-proxy \ | |
260 | --wds-reset | |
261 | } |