]>
Commit | Line | Data |
---|---|---|
1 | #!/bin/sh | |
2 | ||
3 | PATH=/usr/sbin:/usr/bin:/sbin:/bin | |
4 | ||
5 | type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh | |
6 | type ip_to_var > /dev/null 2>&1 || . /lib/net-lib.sh | |
7 | ||
8 | # We already need a set netif here | |
9 | netif=$interface | |
10 | ||
11 | setup_interface() { | |
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 | |
18 | # get rid of control chars | |
19 | search=$(printf -- "%s" "$new_domain_search" | tr -d '[:cntrl:]') | |
20 | namesrv=$new_domain_name_servers | |
21 | hostname=$new_host_name | |
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 | |
26 | ||
27 | # shellcheck disable=SC1090 | |
28 | [ -f /tmp/net."$netif".override ] && . /tmp/net."$netif".override | |
29 | ||
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). | |
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" | |
41 | fi | |
42 | fi | |
43 | ||
44 | ip addr add "$ip"${mask:+/$mask} ${bcast:+broadcast $bcast} dev "$netif" \ | |
45 | ${lease_time:+valid_lft $lease_time} \ | |
46 | ${preferred_lft:+preferred_lft ${preferred_lft}} | |
47 | ||
48 | if [ -n "$gw" ]; then | |
49 | if [ "$mask" = "255.255.255.255" ]; then | |
50 | # point-to-point connection => set explicit route to gateway | |
51 | echo ip route add "$gw" dev "$netif" > /tmp/net."$netif".gw | |
52 | fi | |
53 | ||
54 | echo "$gw" | { | |
55 | IFS=' ' read -r main_gw other_gw | |
56 | echo ip route replace default via "$main_gw" dev "$netif" >> /tmp/net."$netif".gw | |
57 | if [ -n "$other_gw" ]; then | |
58 | for g in $other_gw; do | |
59 | echo ip route add default via "$g" dev "$netif" >> /tmp/net."$netif".gw | |
60 | done | |
61 | fi | |
62 | } | |
63 | fi | |
64 | ||
65 | if getargbool 1 rd.peerdns; then | |
66 | [ -n "${search}${domain}" ] && echo "search $search $domain" > /tmp/net."$netif".resolv.conf | |
67 | if [ -n "$namesrv" ]; then | |
68 | for s in $namesrv; do | |
69 | echo nameserver "$s" | |
70 | done | |
71 | fi >> /tmp/net."$netif".resolv.conf | |
72 | fi | |
73 | # Note: hostname can be fqdn OR short hostname, so chop off any | |
74 | # trailing domain name and explicitly add any domain if set. | |
75 | [ -n "$hostname" ] && echo "echo ${hostname%."$domain"}${domain:+.$domain} > /proc/sys/kernel/hostname" > /tmp/net."$netif".hostname | |
76 | } | |
77 | ||
78 | setup_interface6() { | |
79 | domain=$new_domain_name | |
80 | # get rid of control chars | |
81 | search=$(printf -- "%s" "$new_dhcp6_domain_search" | tr -d '[:cntrl:]') | |
82 | namesrv=$new_dhcp6_name_servers | |
83 | hostname=$new_host_name | |
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 | |
88 | ||
89 | # shellcheck disable=SC1090 | |
90 | [ -f /tmp/net."$netif".override ] && . /tmp/net."$netif".override | |
91 | ||
92 | ip -6 addr add "${new_ip6_address}"/"${new_ip6_prefixlen}" \ | |
93 | dev "${netif}" scope global \ | |
94 | ${lease_time:+valid_lft $lease_time} \ | |
95 | ${preferred_lft:+preferred_lft ${preferred_lft}} | |
96 | ||
97 | if getargbool 1 rd.peerdns; then | |
98 | [ -n "${search}${domain}" ] && echo "search $search $domain" > /tmp/net."$netif".resolv.conf | |
99 | if [ -n "$namesrv" ]; then | |
100 | for s in $namesrv; do | |
101 | echo nameserver "$s" | |
102 | done | |
103 | fi >> /tmp/net."$netif".resolv.conf | |
104 | fi | |
105 | ||
106 | # Note: hostname can be fqdn OR short hostname, so chop off any | |
107 | # trailing domain name and explicitly add any domain if set. | |
108 | [ -n "$hostname" ] && echo "echo ${hostname%."$domain"}${domain:+.$domain} > /proc/sys/kernel/hostname" > /tmp/net."$netif".hostname | |
109 | } | |
110 | ||
111 | parse_option_121() { | |
112 | while [ $# -ne 0 ]; do | |
113 | mask="$1" | |
114 | shift | |
115 | ||
116 | # Is the destination a multicast group? | |
117 | if [ "$1" -ge 224 -a "$1" -lt 240 ]; then | |
118 | multicast=1 | |
119 | else | |
120 | multicast=0 | |
121 | fi | |
122 | ||
123 | # Parse the arguments into a CIDR net/mask string | |
124 | if [ "$mask" -gt 24 ]; then | |
125 | destination="$1.$2.$3.$4/$mask" | |
126 | shift | |
127 | shift | |
128 | shift | |
129 | shift | |
130 | elif [ "$mask" -gt 16 ]; then | |
131 | destination="$1.$2.$3.0/$mask" | |
132 | shift | |
133 | shift | |
134 | shift | |
135 | elif [ "$mask" -gt 8 ]; then | |
136 | destination="$1.$2.0.0/$mask" | |
137 | shift | |
138 | shift | |
139 | elif [ "$mask" -gt 0 ]; then | |
140 | destination="$1.0.0.0/$mask" | |
141 | shift | |
142 | else | |
143 | destination="0.0.0.0/$mask" | |
144 | fi | |
145 | ||
146 | # Read the gateway | |
147 | gateway="$1.$2.$3.$4" | |
148 | shift | |
149 | shift | |
150 | shift | |
151 | shift | |
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. | |
156 | if [ $multicast -eq 1 -o "$gateway" = "0.0.0.0" ]; then | |
157 | temp_result="$destination dev $interface" | |
158 | else | |
159 | temp_result="$destination via $gateway dev $interface" | |
160 | fi | |
161 | ||
162 | echo "/sbin/ip route replace $temp_result" | |
163 | done | |
164 | } | |
165 | ||
166 | case $reason in | |
167 | PREINIT) | |
168 | echo "dhcp: PREINIT $netif up" | |
169 | linkup "$netif" | |
170 | ;; | |
171 | ||
172 | PREINIT6) | |
173 | echo "dhcp: PREINIT6 $netif up" | |
174 | linkup "$netif" | |
175 | wait_for_ipv6_dad_link "$netif" | |
176 | ;; | |
177 | ||
178 | BOUND) | |
179 | echo "dhcp: BOUND setting up $netif" | |
180 | unset layer2 | |
181 | if [ -f /sys/class/net/"$netif"/device/layer2 ]; then | |
182 | read -r layer2 < /sys/class/net/"$netif"/device/layer2 | |
183 | fi | |
184 | if [ "$layer2" != "0" ]; then | |
185 | if command -v arping2 > /dev/null; then | |
186 | if arping2 -q -C 1 -c 2 -I "$netif" -0 "$new_ip_address"; then | |
187 | warn "Duplicate address detected for $new_ip_address while doing dhcp. retrying" | |
188 | exit 1 | |
189 | fi | |
190 | else | |
191 | if ! arping -f -q -D -c 2 -I "$netif" "$new_ip_address"; then | |
192 | warn "Duplicate address detected for $new_ip_address while doing dhcp. retrying" | |
193 | exit 1 | |
194 | fi | |
195 | fi | |
196 | fi | |
197 | unset layer2 | |
198 | setup_interface | |
199 | set | while read -r line || [ -n "$line" ]; do | |
200 | [ "${line#new_}" = "$line" ] && continue | |
201 | echo "$line" | |
202 | done > /tmp/dhclient."$netif".dhcpopts | |
203 | ||
204 | { | |
205 | echo '. /lib/net-lib.sh' | |
206 | echo "setup_net $netif" | |
207 | if [ -n "$new_classless_static_routes" ]; then | |
208 | OLDIFS="$IFS" | |
209 | IFS=".$IFS" | |
210 | parse_option_121 "$new_classless_static_routes" | |
211 | IFS="$OLDIFS" | |
212 | fi | |
213 | echo "source_hook initqueue/online $netif" | |
214 | [ -e /tmp/net."$netif".manualup ] || echo "/sbin/netroot $netif" | |
215 | echo "rm -f -- $hookdir/initqueue/setup_net_$netif.sh" | |
216 | } > "$hookdir"/initqueue/setup_net_"$netif".sh | |
217 | ||
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" | |
222 | fi | |
223 | ||
224 | ;; | |
225 | ||
226 | RENEW | REBIND) | |
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 | |
232 | ip -4 addr change "${new_ip_address}"/"${new_subnet_mask}" broadcast "${new_broadcast_address}" dev "${interface}" \ | |
233 | ${lease_time:+valid_lft $lease_time} ${preferred_lft:+preferred_lft ${preferred_lft}} \ | |
234 | > /dev/null 2>&1 | |
235 | ;; | |
236 | ||
237 | BOUND6) | |
238 | echo "dhcp: BOUND6 setting up $netif" | |
239 | setup_interface6 | |
240 | ||
241 | set | while read -r line || [ -n "$line" ]; do | |
242 | [ "${line#new_}" = "$line" ] && continue | |
243 | echo "$line" | |
244 | done > /tmp/dhclient."$netif".dhcpopts | |
245 | ||
246 | { | |
247 | echo '. /lib/net-lib.sh' | |
248 | echo "setup_net $netif" | |
249 | echo "source_hook initqueue/online $netif" | |
250 | [ -e /tmp/net."$netif".manualup ] || echo "/sbin/netroot $netif" | |
251 | echo "rm -f -- $hookdir/initqueue/setup_net_$netif.sh" | |
252 | } > "$hookdir"/initqueue/setup_net_"$netif".sh | |
253 | ||
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" | |
258 | fi | |
259 | ;; | |
260 | ||
261 | RENEW6 | REBIND6) | |
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 | |
267 | ip -6 addr change "${new_ip6_address}"/"${new_ip6_prefixlen}" dev "${interface}" scope global \ | |
268 | ${lease_time:+valid_lft $lease_time} ${preferred_lft:+preferred_lft ${preferred_lft}} \ | |
269 | > /dev/null 2>&1 | |
270 | ;; | |
271 | ||
272 | *) echo "dhcp: $reason" ;; | |
273 | esac | |
274 | ||
275 | exit 0 |