]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/networking/dhcpcd.exe
dhcpcd.exe: Flush any configured IP addresses on shutdown
[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
5ba06eb8 37 # Only if RED_TYPE=DHCP update /var/ipfire/red
33d2aa6d
MT
38 # Check if we have to restart the services at update
39 [ ! -e "/var/ipfire/red/active" ] && update=1;
40 if [ "$old_domain_name_service" != "$new_domain_name_service" ]; then
41 update=1;
42 fi
43 if [ "$old_ip_address" != "$new_ip_address" ]; then
44 update=1;
26617b09 45 fi
33d2aa6d
MT
46 if [ "$old_routers" != "$new_routers" ]; then
47 update=1;
48 fi
49
50 # Get DNS from dhcp
51 /etc/rc.d/helper/getdnsfromdhcpc.pl 1 > /var/run/dns1
52 /etc/rc.d/helper/getdnsfromdhcpc.pl 2 > /var/run/dns2
53
54 #Get IP Address
55 echo -n "$new_ip_address" > /var/ipfire/red/local-ipaddress
56
57 #Get default gateway
58 grep -v -E "\<gateway\>" /etc/hosts > /tmp/hosts
59 echo "$new_routers gateway" >> /tmp/hosts
60 mv /tmp/hosts /etc/hosts
61
26617b09 62 if [ $update ]; then
1393a0c8 63 [ -e "/var/ipfire/red/active" ] || touch /var/ipfire/red/active
14e30bab 64 echo -n "$new_routers" > /var/ipfire/red/remote-ipaddress
26617b09 65 logger -p local0.info -t dhcpcd.exe[$$] "$interface has been (re)configured with IP=$new_ip_address"
0e42072a 66 run_subdir ${rc_base}/init.d/networking/red.up/
26617b09
AF
67 touch /var/ipfire/red/active
68 fi
69}
70
21d1c72e 71dhcpcd_down()
26617b09 72{
21d1c72e
AF
73 set | grep "^new_" | sed "s|^new_||g" | \
74 sort > /var/ipfire/dhcpc/dhcpcd-$interface.info
5ba06eb8 75
1434fa0d
MT
76 # Remove DNS servers
77 rm -f /var/run/dns1 /var/run/dns2
78
5ba06eb8
AF
79 # Only if RED_TYPE=DHCP update /var/ipfire/red
80 if [ "$RED_TYPE" == "DHCP" ]; then
21d1c72e 81 rm -f /var/ipfire/red/active
455291f9 82 if [ ! $reason == "PREINIT" ]; then
bedfda83 83 logger -p local0.info -t dhcpcd.exe[$$] "${interface} has been brought down ($reason)"
455291f9
AF
84 run_subdir ${rc_base}/init.d/networking/red.down/
85 fi
21d1c72e 86 fi
a22e3c8e
MT
87
88 # Remove any configured IP address from the device
89 ip addr flush dev "${interface}" &>/dev/null
90
91 return 0
26617b09 92}
1393a0c8 93
957863f7
MT
94# Called when dhcpcd relies on a third party to configure an IP address
95dhcpcd_3rdparty() {
96 local qmi_device="$(qmi_find_device "${interface}")"
97
98 if [ -n "${qmi_device}" ]; then
99 setup_qmi "${qmi_device}" || return $?
100 fi
101
102 return 0
103}
104
105setup_qmi() {
106 local device="${1}"
107
108 local address
109 local netmask
110 local gateway
111 local mtu=1500
112
113 local line
114 while read -r line; do
115 # Extract the value
116 value="${line#*: }"
117
118 case "${line}" in
119 *IPv4\ address:*)
120 address="${value}"
121 ;;
122 *IPv4\ subnet\ mask:*)
123 netmask="${value}"
124 ;;
125 *IPv4\ gateway\ address:*)
126 gateway="${value}"
127 ;;
128 *MTU:*)
129 mtu="${value}"
130 ;;
131 esac
132 done <<< "$(qmicli --device="${device}" --wds-get-current-settings)"
133
134 if [ -z "${address}" ] || [ -z "${netmask}" ] || [ -z "${gateway}" ]; then
135 logger -p "local0.info" -t "dhcpcd.exe[$$]" \
136 "Could not retrieve all information from the QMI interface"
137 return 1
138 fi
139
140 # Flush any previous configuration
141 ip addr flush dev "${interface}"
142
143 # Configure the IP address
144 ip addr add "${address}/${netmask}" dev "${interface}"
145
146 # Configure the default route
147 ip route add default via "${gateway}" #mtu "${mtu}"
148
149 return 0
150}
151
26617b09 152case "$reason" in
21d1c72e 153BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT|STATIC) dhcpcd_up;;
bedfda83 154PREINIT|EXPIRE|FAIL|IPV4LL|NAK|RELEASE|STOP) dhcpcd_down;;
957863f7
MT
1553RDPARTY)
156 dhcpcd_3rdparty
157 ;;
177ec167
MT
158*)
159 logger -p "local0.info" -t "dhcpcd.exe[$$]" "Unhandled DHCP event: ${reason}"
160 ;;
26617b09 161esac