]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/networking/dhcpcd.exe
acd083b6f186e37159a8bdd83525f730c26fa5a7
[people/pmueller/ipfire-2.x.git] / src / initscripts / networking / dhcpcd.exe
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 ###############################################################################
20
21 . /etc/sysconfig/rc
22 . $rc_functions
23 . /etc/init.d/networking/functions.network
24
25 eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
26
27 dhcpcd_up()
28 {
29 set | grep "^new_" | sed "s|^new_||g" | \
30 sort > /var/ipfire/dhcpc/dhcpcd-$interface.info
31
32 #Check if this was the Red device...
33 if [ ! "$interface" == "$RED_DEV" ]; then
34 exit 0;
35 fi
36
37 # Only if RED_TYPE=DHCP update /var/ipfire/red
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;
45 fi
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 if [ -n "${new_routers}" ]; then
59 grep -v -E "\<gateway\>" /etc/hosts > /tmp/hosts
60 echo "$new_routers gateway" >> /tmp/hosts
61 mv /tmp/hosts /etc/hosts
62 fi
63
64 if [ $update ]; then
65 [ -e "/var/ipfire/red/active" ] || touch /var/ipfire/red/active
66 echo -n "$new_routers" > /var/ipfire/red/remote-ipaddress
67 logger -p local0.info -t dhcpcd.exe[$$] "$interface has been (re)configured with IP=$new_ip_address"
68 run_subdir ${rc_base}/init.d/networking/red.up/
69 touch /var/ipfire/red/active
70 fi
71 }
72
73 dhcpcd_down()
74 {
75 set | grep "^new_" | sed "s|^new_||g" | \
76 sort > /var/ipfire/dhcpc/dhcpcd-$interface.info
77
78 # Remove DNS servers
79 rm -f /var/run/dns1 /var/run/dns2
80
81 # Only if RED_TYPE=DHCP update /var/ipfire/red
82 if [ "$RED_TYPE" == "DHCP" ]; then
83 rm -f /var/ipfire/red/active
84 if [ ! $reason == "PREINIT" ]; then
85 logger -p local0.info -t dhcpcd.exe[$$] "${interface} has been brought down ($reason)"
86 run_subdir ${rc_base}/init.d/networking/red.down/
87 fi
88 fi
89
90 # Remove any configured IP address from the device
91 ip addr flush dev "${interface}" &>/dev/null
92
93 return 0
94 }
95
96 # Called when dhcpcd relies on a third party to configure an IP address
97 dhcpcd_3rdparty() {
98 local qmi_device="$(qmi_find_device "${interface}")"
99
100 if [ -n "${qmi_device}" ]; then
101 setup_qmi "${qmi_device}" || return $?
102 fi
103
104 return 0
105 }
106
107 setup_qmi() {
108 local device="${1}"
109
110 local address
111 local netmask
112 local gateway
113 local mtu=1500
114 local dns1
115 local dns2
116
117 local line
118 while read -r line; do
119 # Extract the value
120 value="${line#*: }"
121
122 case "${line}" in
123 *IPv4\ address:*)
124 address="${value}"
125 ;;
126 *IPv4\ subnet\ mask:*)
127 netmask="${value}"
128 ;;
129 *IPv4\ gateway\ address:*)
130 gateway="${value}"
131 ;;
132 *IPv4\ primary\ DNS:*)
133 dns1="${value}"
134 ;;
135 *IPv4\ secondary\ DNS:*)
136 dns2="${value}"
137 ;;
138 *MTU:*)
139 mtu="${value}"
140 ;;
141 esac
142 done <<< "$(qmicli --device="${device}" --wds-get-current-settings)"
143
144 if [ -z "${address}" ] || [ -z "${netmask}" ] || [ -z "${gateway}" ]; then
145 logger -p "local0.info" -t "dhcpcd.exe[$$]" \
146 "Could not retrieve all information from the QMI interface"
147 return 1
148 fi
149
150 # Flush any previous configuration
151 ip addr flush dev "${interface}"
152
153 # Configure the IP address
154 ip addr add "${address}/${netmask}" dev "${interface}"
155
156 # Configure the default route
157 if [ -n "${gateway}" ]; then
158 # Store the default gateway
159 echo -n "${gateway}" > /var/ipfire/red/remote-ipaddress
160
161 # Configure the default route
162 ip route add default via "${gateway}" mtu "${mtu}"
163 fi
164
165 # Store and DNS servers
166 if [ -n "${dns1}" ]; then
167 echo -n "${dns1}" > /var/ipfire/red/dns1
168 fi
169 if [ -n "${dns2}" ]; then
170 echo n "${dns2}" > /var/ipfire/red/dns2
171 fi
172
173 return 0
174 }
175
176 case "$reason" in
177 BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT|STATIC) dhcpcd_up;;
178 PREINIT|EXPIRE|FAIL|IPV4LL|NAK|RELEASE|STOP) dhcpcd_down;;
179 3RDPARTY)
180 dhcpcd_3rdparty
181 ;;
182 *)
183 logger -p "local0.info" -t "dhcpcd.exe[$$]" "Unhandled DHCP event: ${reason}"
184 ;;
185 esac