]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/networking/dhcpcd.exe
dhcpcd.exe: Add a safety net to make sure we always have a route
[ipfire-2.x.git] / src / initscripts / networking / dhcpcd.exe
CommitLineData
66c36198
PM
1###############################################################################
2# #
3# IPFire.org - A linux based firewall #
4# Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19###############################################################################
1393a0c8 20
26617b09
AF
21. /etc/sysconfig/rc
22. $rc_functions
957863f7 23. /etc/init.d/networking/functions.network
0e42072a 24
5ba06eb8 25eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
26617b09 26
21d1c72e 27dhcpcd_up()
26617b09
AF
28{
29 set | grep "^new_" | sed "s|^new_||g" | \
30 sort > /var/ipfire/dhcpc/dhcpcd-$interface.info
31
0c719f93
AF
32 #Check if this was the Red device...
33 if [ ! "$interface" == "$RED_DEV" ]; then
34 exit 0;
35 fi
36
33d2aa6d
MT
37 # Check if we have to restart the services at update
38 [ ! -e "/var/ipfire/red/active" ] && update=1;
39 if [ "$old_domain_name_service" != "$new_domain_name_service" ]; then
40 update=1;
41 fi
42 if [ "$old_ip_address" != "$new_ip_address" ]; then
43 update=1;
26617b09 44 fi
33d2aa6d
MT
45 if [ "$old_routers" != "$new_routers" ]; then
46 update=1;
47 fi
48
49 # Get DNS from dhcp
50 /etc/rc.d/helper/getdnsfromdhcpc.pl 1 > /var/run/dns1
51 /etc/rc.d/helper/getdnsfromdhcpc.pl 2 > /var/run/dns2
52
53 #Get IP Address
54 echo -n "$new_ip_address" > /var/ipfire/red/local-ipaddress
55
36914cce
MT
56 # Get default gateway
57 if [ -n "${new_routers}" ]; then
58 grep -v -E "\<gateway\>" /etc/hosts > /tmp/hosts
59 echo "$new_routers gateway" >> /tmp/hosts
60 mv /tmp/hosts /etc/hosts
61 fi
33d2aa6d 62
86ba5ce3
MT
63 # Try to set the default route if there is none
64 # This can happen when we are using the 3RD party configuration
65 # This should not be necessary, since dhcpcd says it will configure
66 # a default route, but that does not seem to happen:
67 # https://github.com/NetworkConfiguration/dhcpcd/issues/129
68 if ! ip route 2>/dev/null | grep ^default && [ -s "/var/ipfire/red/remote-ipaddress" ]; then
69 gateway=$(</var/ipfire/red/remote-ipaddress)
70
71 # XXX This is ignoring any MTU configuration
72 ip route add default via "${gateway}"
73 fi
74
26617b09 75 if [ $update ]; then
7a67f77f
MT
76 # Consider RED being active
77 touch /var/ipfire/red/active
4ffd18a7
MT
78
79 if [ -n "${new_routers}" ]; then
80 echo -n "${new_routers}" > /var/ipfire/red/remote-ipaddress
81 fi
82
26617b09 83 logger -p local0.info -t dhcpcd.exe[$$] "$interface has been (re)configured with IP=$new_ip_address"
0e42072a 84 run_subdir ${rc_base}/init.d/networking/red.up/
26617b09
AF
85 fi
86}
87
21d1c72e 88dhcpcd_down()
26617b09 89{
21d1c72e
AF
90 set | grep "^new_" | sed "s|^new_||g" | \
91 sort > /var/ipfire/dhcpc/dhcpcd-$interface.info
5ba06eb8 92
1434fa0d
MT
93 # Remove DNS servers
94 rm -f /var/run/dns1 /var/run/dns2
95
c2e2c282
MT
96 # Consider RED to be no longer active
97 rm -f /var/ipfire/red/active
98
99 if [ ! $reason == "PREINIT" ]; then
100 logger -p local0.info -t dhcpcd.exe[$$] "${interface} has been brought down ($reason)"
101 run_subdir ${rc_base}/init.d/networking/red.down/
21d1c72e 102 fi
a22e3c8e
MT
103
104 # Remove any configured IP address from the device
105 ip addr flush dev "${interface}" &>/dev/null
106
107 return 0
26617b09 108}
1393a0c8 109
957863f7
MT
110# Called when dhcpcd relies on a third party to configure an IP address
111dhcpcd_3rdparty() {
112 local qmi_device="$(qmi_find_device "${interface}")"
113
114 if [ -n "${qmi_device}" ]; then
115 setup_qmi "${qmi_device}" || return $?
116 fi
117
118 return 0
119}
120
121setup_qmi() {
122 local device="${1}"
123
124 local address
125 local netmask
126 local gateway
127 local mtu=1500
ccf36e9e
MT
128 local dns1
129 local dns2
957863f7
MT
130
131 local line
132 while read -r line; do
133 # Extract the value
134 value="${line#*: }"
135
136 case "${line}" in
137 *IPv4\ address:*)
138 address="${value}"
139 ;;
140 *IPv4\ subnet\ mask:*)
141 netmask="${value}"
142 ;;
143 *IPv4\ gateway\ address:*)
144 gateway="${value}"
145 ;;
ccf36e9e
MT
146 *IPv4\ primary\ DNS:*)
147 dns1="${value}"
148 ;;
149 *IPv4\ secondary\ DNS:*)
150 dns2="${value}"
151 ;;
957863f7
MT
152 *MTU:*)
153 mtu="${value}"
154 ;;
155 esac
156 done <<< "$(qmicli --device="${device}" --wds-get-current-settings)"
157
158 if [ -z "${address}" ] || [ -z "${netmask}" ] || [ -z "${gateway}" ]; then
159 logger -p "local0.info" -t "dhcpcd.exe[$$]" \
160 "Could not retrieve all information from the QMI interface"
161 return 1
162 fi
163
164 # Flush any previous configuration
165 ip addr flush dev "${interface}"
166
167 # Configure the IP address
168 ip addr add "${address}/${netmask}" dev "${interface}"
169
170 # Configure the default route
26172743
MT
171 if [ -n "${gateway}" ]; then
172 # Store the default gateway
173 echo -n "${gateway}" > /var/ipfire/red/remote-ipaddress
174
175 # Configure the default route
176 ip route add default via "${gateway}" mtu "${mtu}"
177 fi
957863f7 178
ccf36e9e
MT
179 # Store and DNS servers
180 if [ -n "${dns1}" ]; then
181 echo -n "${dns1}" > /var/ipfire/red/dns1
182 fi
183 if [ -n "${dns2}" ]; then
cb4b6f43 184 echo -n "${dns2}" > /var/ipfire/red/dns2
ccf36e9e
MT
185 fi
186
957863f7
MT
187 return 0
188}
189
e3da4912
MT
190case "${reason}" in
191 BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT|STATIC)
192 dhcpcd_up
193 ;;
194 PREINIT|EXPIRE|FAIL|IPV4LL|NAK|RELEASE|STOP)
195 dhcpcd_down
196 ;;
197 3RDPARTY)
198 dhcpcd_3rdparty
199 ;;
ece96c6f
MT
200 # Ignored events where we do not need to do anything
201 STOPPED|CARRIER|NOCARRIER)
202 ;;
e3da4912
MT
203 *)
204 logger -p "local0.info" -t "dhcpcd.exe[$$]" "Unhandled DHCP event: ${reason}"
205 ;;
26617b09 206esac