]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/networking/functions.network
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / src / initscripts / networking / functions.network
CommitLineData
8ae238a5 1#!/bin/bash
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
25eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
26eval $(/usr/local/bin/readhash /var/ipfire/dns/settings)
27
28dhcpcd_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
45dhcpcd_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
60dhcpcd_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
130dhcpcd_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
175qmi_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
580c249a 182 _intf="$(qmi_find_interface "${path}")"
957863f7
MT
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
580c249a
MT
196qmi_find_interface() {
197 local device="${1}"
198
199 qmicli --device="${device}" --device-open-proxy --get-wwan-iface
200}
201
957863f7
MT
202qmi_enable_rawip_mode() {
203 local intf="${1}"
204
205 # Shut down the device first
206 ip link set "${intf}" down &>/dev/null
207
208 echo "Y" > "/sys/class/net/${intf}/qmi/raw_ip"
209}
210
211qmi_configure_apn() {
212 local device="${1}"
213
214 # APN settings
215 local apn="${2}"
216 local auth="${3}"
217 local username="${4}"
218 local password="${5}"
219
220 local args=(
221 # We only support IPv4 right now
222 "ip-type=4"
223 )
224
225 # Set APN
226 if [ -n "${apn}" ]; then
227 args+=( "apn=${apn}" )
228 fi
229
230 # Set auth
231 case "${auth}" in
232 PAP|CHAP)
233 args+=( "auth=${auth}" )
234 ;;
235 esac
236
237 # Set username
238 if [ -n "${username}" ]; then
239 args+=( "username=${username}" )
240 fi
241
242 # Set password
243 if [ -n "${password}" ]; then
244 args+=( "password=${password}" )
245 fi
246
247 local _args
248
249 local arg
250 for arg in ${args[@]}; do
251 if [ -n "${_args}" ]; then
252 _args="${_args},"
253 fi
254 _args="${_args}${arg}"
255 done
256
257 qmicli --device="${device}" --device-open-proxy \
258 --wds-start-network="${_args}" \
b1ff8adb 259 --client-no-release-cid &>/dev/null
957863f7
MT
260}
261
262qmi_reset() {
263 local device="${1}"
264
265 qmicli --device="${device}" --device-open-proxy \
b1ff8adb 266 --wds-reset &>/dev/null
957863f7 267}
580c249a
MT
268
269# Assigns a "static" MAC address
270qmi_assign_address() {
271 local intf="${1}"
272
273 # Find the device
274 local device="$(qmi_find_device "${intf}")"
275
caef75c5
SS
276 # Switch off the raw_ip mode to be able to proper
277 # assign the generated MAC address.
278 echo "N" > "/sys/class/net/${intf}/qmi/raw_ip"
279
580c249a
MT
280 local address
281
282 # Generate a "random" MAC address using the device number
283 printf -v address "02:ff:ff:ff:ff:%02x" "${device:12}"
284
285 # Change the MAC address
286 ip link set "${intf}" address "${address}"
287}