]> git.ipfire.org Git - thirdparty/dracut.git/blame - modules.d/35network-legacy/dhclient-script.sh
fix(zfcp_rules): correct shellcheck regression when parsing ccw args
[thirdparty/dracut.git] / modules.d / 35network-legacy / dhclient-script.sh
CommitLineData
3b403b32 1#!/bin/sh
7e9919b9 2
a9d30a40
HH
3PATH=/usr/sbin:/usr/bin:/sbin:/bin
4
9a52c3fd
HH
5type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
6type ip_to_var > /dev/null 2>&1 || . /lib/net-lib.sh
a9d30a40
HH
7
8# We already need a set netif here
9netif=$interface
10
7e9919b9 11setup_interface() {
580bb541
PS
12 ip=$new_ip_address
13 mtu=$new_interface_mtu
14 mask=$new_subnet_mask
15 bcast=$new_broadcast_address
16 gw=${new_routers%%,*}
17 domain=$new_domain_name
8c0fcdd9
HH
18 # get rid of control chars
19 search=$(printf -- "%s" "$new_domain_search" | tr -d '[:cntrl:]')
580bb541
PS
20 namesrv=$new_domain_name_servers
21 hostname=$new_host_name
8d09f493
HH
22 [ -n "$new_dhcp_lease_time" ] && lease_time=$new_dhcp_lease_time
23 [ -n "$new_max_life" ] && lease_time=$new_max_life
24 preferred_lft=$lease_time
25 [ -n "$new_preferred_life" ] && preferred_lft=$new_preferred_life
580bb541 26
8c0fcdd9
HH
27 # shellcheck disable=SC1090
28 [ -f /tmp/net."$netif".override ] && . /tmp/net."$netif".override
580bb541 29
d1dd6bb7
PS
30 # Taken from debian dhclient-script:
31 # The 576 MTU is only used for X.25 and dialup connections
32 # where the admin wants low latency. Such a low MTU can cause
33 # problems with UDP traffic, among other things. As such,
34 # disallow MTUs from 576 and below by default, so that broken
35 # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
8c0fcdd9
HH
36 if [ -n "$mtu" ] && [ "$mtu" -gt 576 ]; then
37 if ! ip link set "$netif" mtu "$mtu"; then
38 ip link set "$netif" down
39 ip link set "$netif" mtu "$mtu"
40 linkup "$netif"
c6c704fd
HH
41 fi
42 fi
7e9919b9 43
8c0fcdd9 44 ip addr add "$ip"${mask:+/$mask} ${bcast:+broadcast $bcast} dev "$netif" \
0f89ec31
HH
45 ${lease_time:+valid_lft $lease_time} \
46 ${preferred_lft:+preferred_lft ${preferred_lft}}
db815843 47
9a52c3fd
HH
48 if [ -n "$gw" ]; then
49 if [ "$mask" = "255.255.255.255" ]; then
99ccbc30 50 # point-to-point connection => set explicit route to gateway
8c0fcdd9 51 echo ip route add "$gw" dev "$netif" > /tmp/net."$netif".gw
99ccbc30 52 fi
727e2a1d
DS
53
54 echo "$gw" | {
55 IFS=' ' read -r main_gw other_gw
8c0fcdd9 56 echo ip route replace default via "$main_gw" dev "$netif" >> /tmp/net."$netif".gw
9a52c3fd 57 if [ -n "$other_gw" ]; then
727e2a1d 58 for g in $other_gw; do
8c0fcdd9 59 echo ip route add default via "$g" dev "$netif" >> /tmp/net."$netif".gw
727e2a1d
DS
60 done
61 fi
62 }
99ccbc30 63 fi
db815843 64
df0bdd5a 65 if getargbool 1 rd.peerdns; then
8c0fcdd9 66 [ -n "${search}${domain}" ] && echo "search $search $domain" > /tmp/net."$netif".resolv.conf
9a52c3fd 67 if [ -n "$namesrv" ]; then
df0bdd5a 68 for s in $namesrv; do
8c0fcdd9 69 echo nameserver "$s"
df0bdd5a 70 done
8c0fcdd9 71 fi >> /tmp/net."$netif".resolv.conf
df0bdd5a 72 fi
da55af47 73 # Note: hostname can be fqdn OR short hostname, so chop off any
ddf63231 74 # trailing domain name and explicitly add any domain if set.
08b63a25 75 [ -n "$hostname" ] && echo "echo ${hostname%."$domain"}${domain:+.$domain} > /proc/sys/kernel/hostname" > /tmp/net."$netif".hostname
7e9919b9
DD
76}
77
2ac599dc
HH
78setup_interface6() {
79 domain=$new_domain_name
8c0fcdd9
HH
80 # get rid of control chars
81 search=$(printf -- "%s" "$new_dhcp6_domain_search" | tr -d '[:cntrl:]')
96231048 82 namesrv=$new_dhcp6_name_servers
2ac599dc 83 hostname=$new_host_name
ebe74116
HH
84 [ -n "$new_dhcp_lease_time" ] && lease_time=$new_dhcp_lease_time
85 [ -n "$new_max_life" ] && lease_time=$new_max_life
86 preferred_lft=$lease_time
87 [ -n "$new_preferred_life" ] && preferred_lft=$new_preferred_life
2ac599dc 88
8c0fcdd9
HH
89 # shellcheck disable=SC1090
90 [ -f /tmp/net."$netif".override ] && . /tmp/net."$netif".override
2ac599dc 91
8c0fcdd9
HH
92 ip -6 addr add "${new_ip6_address}"/"${new_ip6_prefixlen}" \
93 dev "${netif}" scope global \
ebe74116
HH
94 ${lease_time:+valid_lft $lease_time} \
95 ${preferred_lft:+preferred_lft ${preferred_lft}}
2ac599dc 96
df0bdd5a 97 if getargbool 1 rd.peerdns; then
8c0fcdd9 98 [ -n "${search}${domain}" ] && echo "search $search $domain" > /tmp/net."$netif".resolv.conf
9a52c3fd 99 if [ -n "$namesrv" ]; then
df0bdd5a 100 for s in $namesrv; do
8c0fcdd9 101 echo nameserver "$s"
df0bdd5a 102 done
8c0fcdd9 103 fi >> /tmp/net."$netif".resolv.conf
df0bdd5a 104 fi
2ac599dc
HH
105
106 # Note: hostname can be fqdn OR short hostname, so chop off any
ddf63231 107 # trailing domain name and explicitly add any domain if set.
08b63a25 108 [ -n "$hostname" ] && echo "echo ${hostname%."$domain"}${domain:+.$domain} > /proc/sys/kernel/hostname" > /tmp/net."$netif".hostname
2ac599dc
HH
109}
110
bcabe0fe 111parse_option_121() {
caf12d67
HH
112 while [ $# -ne 0 ]; do
113 mask="$1"
114 shift
115
116 # Is the destination a multicast group?
8c0fcdd9 117 if [ "$1" -ge 224 -a "$1" -lt 240 ]; then
caf12d67
HH
118 multicast=1
119 else
120 multicast=0
121 fi
122
123 # Parse the arguments into a CIDR net/mask string
8c0fcdd9 124 if [ "$mask" -gt 24 ]; then
caf12d67 125 destination="$1.$2.$3.$4/$mask"
9a52c3fd
HH
126 shift
127 shift
128 shift
129 shift
8c0fcdd9 130 elif [ "$mask" -gt 16 ]; then
caf12d67 131 destination="$1.$2.$3.0/$mask"
9a52c3fd
HH
132 shift
133 shift
134 shift
8c0fcdd9 135 elif [ "$mask" -gt 8 ]; then
caf12d67 136 destination="$1.$2.0.0/$mask"
9a52c3fd
HH
137 shift
138 shift
8c0fcdd9 139 elif [ "$mask" -gt 0 ]; then
caf12d67
HH
140 destination="$1.0.0.0/$mask"
141 shift
11e1f680
FD
142 else
143 destination="0.0.0.0/$mask"
caf12d67
HH
144 fi
145
146 # Read the gateway
147 gateway="$1.$2.$3.$4"
9a52c3fd
HH
148 shift
149 shift
150 shift
151 shift
caf12d67
HH
152
153 # Multicast routing on Linux
154 # - If you set a next-hop address for a multicast group, this breaks with Cisco switches
155 # - If you simply leave it link-local and attach it to an interface, it works fine.
11e1f680 156 if [ $multicast -eq 1 -o "$gateway" = "0.0.0.0" ]; then
caf12d67
HH
157 temp_result="$destination dev $interface"
158 else
159 temp_result="$destination via $gateway dev $interface"
160 fi
161
d5e818f3 162 echo "/sbin/ip route replace $temp_result"
caf12d67
HH
163 done
164}
165
7e9919b9
DD
166case $reason in
167 PREINIT)
cc02093d 168 echo "dhcp: PREINIT $netif up"
8c0fcdd9 169 linkup "$netif"
cc02093d 170 ;;
2ac599dc 171
a9d30a40 172 PREINIT6)
7c5ec0f5 173 echo "dhcp: PREINIT6 $netif up"
8c0fcdd9
HH
174 linkup "$netif"
175 wait_for_ipv6_dad_link "$netif"
a9d30a40
HH
176 ;;
177
7e9919b9 178 BOUND)
b68930ed 179 echo "dhcp: BOUND setting up $netif"
8b88dc7f 180 unset layer2
8c0fcdd9
HH
181 if [ -f /sys/class/net/"$netif"/device/layer2 ]; then
182 read -r layer2 < /sys/class/net/"$netif"/device/layer2
cc02093d 183 fi
8b88dc7f 184 if [ "$layer2" != "0" ]; then
9a52c3fd 185 if command -v arping2 > /dev/null; then
8c0fcdd9 186 if arping2 -q -C 1 -c 2 -I "$netif" -0 "$new_ip_address"; then
9853791d
HH
187 warn "Duplicate address detected for $new_ip_address while doing dhcp. retrying"
188 exit 1
189 fi
190 else
8c0fcdd9 191 if ! arping -f -q -D -c 2 -I "$netif" "$new_ip_address"; then
9853791d
HH
192 warn "Duplicate address detected for $new_ip_address while doing dhcp. retrying"
193 exit 1
194 fi
8b88dc7f
HH
195 fi
196 fi
197 unset layer2
3b403b32 198 setup_interface
8c0fcdd9 199 set | while read -r line || [ -n "$line" ]; do
cc02093d 200 [ "${line#new_}" = "$line" ] && continue
3b403b32 201 echo "$line"
8c0fcdd9 202 done > /tmp/dhclient."$netif".dhcpopts
957bc5c9 203
c6c704fd
HH
204 {
205 echo '. /lib/net-lib.sh'
206 echo "setup_net $netif"
caf12d67 207 if [ -n "$new_classless_static_routes" ]; then
337a55eb
LN
208 OLDIFS="$IFS"
209 IFS=".$IFS"
8c0fcdd9 210 parse_option_121 "$new_classless_static_routes"
337a55eb 211 IFS="$OLDIFS"
caf12d67 212 fi
c6c704fd 213 echo "source_hook initqueue/online $netif"
8c0fcdd9 214 [ -e /tmp/net."$netif".manualup ] || echo "/sbin/netroot $netif"
32bd2fbb 215 echo "rm -f -- $hookdir/initqueue/setup_net_$netif.sh"
8c0fcdd9 216 } > "$hookdir"/initqueue/setup_net_"$netif".sh
c6c704fd 217
8c0fcdd9
HH
218 echo "[ -f /tmp/net.$netif.did-setup ]" > "$hookdir"/initqueue/finished/dhclient-"$netif".sh
219 : > /tmp/net."$netif".up
220 if [ -e /sys/class/net/"${netif}"/address ]; then
221 : > "/tmp/net.$(cat /sys/class/net/"${netif}"/address).up"
26fbe97b
HH
222 fi
223
2ac599dc
HH
224 ;;
225
9a52c3fd 226 RENEW | REBIND)
cf627b20
HH
227 unset lease_time
228 [ -n "$new_dhcp_lease_time" ] && lease_time=$new_dhcp_lease_time
229 [ -n "$new_max_life" ] && lease_time=$new_max_life
230 preferred_lft=$lease_time
231 [ -n "$new_preferred_life" ] && preferred_lft=$new_preferred_life
8c0fcdd9 232 ip -4 addr change "${new_ip_address}"/"${new_subnet_mask}" broadcast "${new_broadcast_address}" dev "${interface}" \
9a52c3fd
HH
233 ${lease_time:+valid_lft $lease_time} ${preferred_lft:+preferred_lft ${preferred_lft}} \
234 > /dev/null 2>&1
cf627b20
HH
235 ;;
236
2ac599dc 237 BOUND6)
b68930ed 238 echo "dhcp: BOUND6 setting up $netif"
2ac599dc
HH
239 setup_interface6
240
8c0fcdd9 241 set | while read -r line || [ -n "$line" ]; do
2ac599dc
HH
242 [ "${line#new_}" = "$line" ] && continue
243 echo "$line"
8c0fcdd9 244 done > /tmp/dhclient."$netif".dhcpopts
2ac599dc
HH
245
246 {
247 echo '. /lib/net-lib.sh'
248 echo "setup_net $netif"
249 echo "source_hook initqueue/online $netif"
8c0fcdd9 250 [ -e /tmp/net."$netif".manualup ] || echo "/sbin/netroot $netif"
2ac599dc 251 echo "rm -f -- $hookdir/initqueue/setup_net_$netif.sh"
8c0fcdd9 252 } > "$hookdir"/initqueue/setup_net_"$netif".sh
2ac599dc 253
8c0fcdd9
HH
254 echo "[ -f /tmp/net.$netif.did-setup ]" > "$hookdir"/initqueue/finished/dhclient-"$netif".sh
255 : > /tmp/net."$netif".up
256 if [ -e /sys/class/net/"${netif}"/address ]; then
257 : > "/tmp/net.$(cat /sys/class/net/"${netif}"/address).up"
26fbe97b 258 fi
cc02093d 259 ;;
cf627b20 260
9a52c3fd 261 RENEW6 | REBIND6)
cf627b20
HH
262 unset lease_time
263 [ -n "$new_dhcp_lease_time" ] && lease_time=$new_dhcp_lease_time
264 [ -n "$new_max_life" ] && lease_time=$new_max_life
265 preferred_lft=$lease_time
266 [ -n "$new_preferred_life" ] && preferred_lft=$new_preferred_life
8c0fcdd9 267 ip -6 addr change "${new_ip6_address}"/"${new_ip6_prefixlen}" dev "${interface}" scope global \
9a52c3fd
HH
268 ${lease_time:+valid_lft $lease_time} ${preferred_lft:+preferred_lft ${preferred_lft}} \
269 > /dev/null 2>&1
cf627b20
HH
270 ;;
271
9a52c3fd 272 *) echo "dhcp: $reason" ;;
0ac9584d 273esac
7e9919b9 274
7e9919b9 275exit 0