summaryrefslogtreecommitdiffstats
path: root/modules.d/35network-legacy/dhclient-script.sh
blob: b3e5e7574d7b2f3642fd2ee3d434d7dc5e9c14d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/sh

PATH=/usr/sbin:/usr/bin:/sbin:/bin

type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
type ip_to_var > /dev/null 2>&1 || . /lib/net-lib.sh

# We already need a set netif here
netif=$interface

setup_interface() {
    ip=$new_ip_address
    mtu=$new_interface_mtu
    mask=$new_subnet_mask
    bcast=$new_broadcast_address
    gw=${new_routers%%,*}
    domain=$new_domain_name
    # get rid of control chars
    search=$(printf -- "%s" "$new_domain_search" | tr -d '[:cntrl:]')
    namesrv=$new_domain_name_servers
    hostname=$new_host_name
    [ -n "$new_dhcp_lease_time" ] && lease_time=$new_dhcp_lease_time
    [ -n "$new_max_life" ] && lease_time=$new_max_life
    preferred_lft=$lease_time
    [ -n "$new_preferred_life" ] && preferred_lft=$new_preferred_life

    # shellcheck disable=SC1090
    [ -f /tmp/net."$netif".override ] && . /tmp/net."$netif".override

    # Taken from debian dhclient-script:
    # The 576 MTU is only used for X.25 and dialup connections
    # where the admin wants low latency.  Such a low MTU can cause
    # problems with UDP traffic, among other things.  As such,
    # disallow MTUs from 576 and below by default, so that broken
    # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
    if [ -n "$mtu" ] && [ "$mtu" -gt 576 ]; then
        if ! ip link set "$netif" mtu "$mtu"; then
            ip link set "$netif" down
            ip link set "$netif" mtu "$mtu"
            linkup "$netif"
        fi
    fi

    ip addr add "$ip"${mask:+/$mask} ${bcast:+broadcast $bcast} dev "$netif" \
        ${lease_time:+valid_lft $lease_time} \
        ${preferred_lft:+preferred_lft ${preferred_lft}}

    if [ -n "$gw" ]; then
        if [ "$mask" = "255.255.255.255" ]; then
            # point-to-point connection => set explicit route to gateway
            echo ip route add "$gw" dev "$netif" > /tmp/net."$netif".gw
        fi

        echo "$gw" | {
            IFS=' ' read -r main_gw other_gw
            echo ip route replace default via "$main_gw" dev "$netif" >> /tmp/net."$netif".gw
            if [ -n "$other_gw" ]; then
                for g in $other_gw; do
                    echo ip route add default via "$g" dev "$netif" >> /tmp/net."$netif".gw
                done
            fi
        }
    fi

    if getargbool 1 rd.peerdns; then
        [ -n "${search}${domain}" ] && echo "search $search $domain" > /tmp/net."$netif".resolv.conf
        if [ -n "$namesrv" ]; then
            for s in $namesrv; do
                echo nameserver "$s"
            done
        fi >> /tmp/net."$netif".resolv.conf
    fi
    # Note: hostname can be fqdn OR short hostname, so chop off any
    # trailing domain name and explicitly add any domain if set.
    [ -n "$hostname" ] && echo "echo ${hostname%."$domain"}${domain:+.$domain} > /proc/sys/kernel/hostname" > /tmp/net."$netif".hostname
}

setup_interface6() {
    domain=$new_domain_name
    # get rid of control chars
    search=$(printf -- "%s" "$new_dhcp6_domain_search" | tr -d '[:cntrl:]')
    namesrv=$new_dhcp6_name_servers
    hostname=$new_host_name
    [ -n "$new_dhcp_lease_time" ] && lease_time=$new_dhcp_lease_time
    [ -n "$new_max_life" ] && lease_time=$new_max_life
    preferred_lft=$lease_time
    [ -n "$new_preferred_life" ] && preferred_lft=$new_preferred_life

    # shellcheck disable=SC1090
    [ -f /tmp/net."$netif".override ] && . /tmp/net."$netif".override

    ip -6 addr add "${new_ip6_address}"/"${new_ip6_prefixlen}" \
        dev "${netif}" scope global \
        ${lease_time:+valid_lft $lease_time} \
        ${preferred_lft:+preferred_lft ${preferred_lft}}

    if getargbool 1 rd.peerdns; then
        [ -n "${search}${domain}" ] && echo "search $search $domain" > /tmp/net."$netif".resolv.conf
        if [ -n "$namesrv" ]; then
            for s in $namesrv; do
                echo nameserver "$s"
            done
        fi >> /tmp/net."$netif".resolv.conf
    fi

    # Note: hostname can be fqdn OR short hostname, so chop off any
    # trailing domain name and explicitly add any domain if set.
    [ -n "$hostname" ] && echo "echo ${hostname%."$domain"}${domain:+.$domain} > /proc/sys/kernel/hostname" > /tmp/net."$netif".hostname
}

parse_option_121() {
    while [ $# -ne 0 ]; do
        mask="$1"
        shift

        # Is the destination a multicast group?
        if [ "$1" -ge 224 -a "$1" -lt 240 ]; then
            multicast=1
        else
            multicast=0
        fi

        # Parse the arguments into a CIDR net/mask string
        if [ "$mask" -gt 24 ]; then
            destination="$1.$2.$3.$4/$mask"
            shift
            shift
            shift
            shift
        elif [ "$mask" -gt 16 ]; then
            destination="$1.$2.$3.0/$mask"
            shift
            shift
            shift
        elif [ "$mask" -gt 8 ]; then
            destination="$1.$2.0.0/$mask"
            shift
            shift
        elif [ "$mask" -gt 0 ]; then
            destination="$1.0.0.0/$mask"
            shift
        else
            destination="0.0.0.0/$mask"
        fi

        # Read the gateway
        gateway="$1.$2.$3.$4"
        shift
        shift
        shift
        shift

        # Multicast routing on Linux
        #  - If you set a next-hop address for a multicast group, this breaks with Cisco switches
        #  - If you simply leave it link-local and attach it to an interface, it works fine.
        if [ $multicast -eq 1 -o "$gateway" = "0.0.0.0" ]; then
            temp_result="$destination dev $interface"
        else
            temp_result="$destination via $gateway dev $interface"
        fi

        echo "/sbin/ip route replace $temp_result"
    done
}

case $reason in
    PREINIT)
        echo "dhcp: PREINIT $netif up"
        linkup "$netif"
        ;;

    PREINIT6)
        echo "dhcp: PREINIT6 $netif up"
        linkup "$netif"
        wait_for_ipv6_dad_link "$netif"
        ;;

    BOUND)
        echo "dhcp: BOUND setting up $netif"
        unset layer2
        if [ -f /sys/class/net/"$netif"/device/layer2 ]; then
            read -r layer2 < /sys/class/net/"$netif"/device/layer2
        fi
        if [ "$layer2" != "0" ]; then
            if command -v arping2 > /dev/null; then
                if arping2 -q -C 1 -c 2 -I "$netif" -0 "$new_ip_address"; then
                    warn "Duplicate address detected for $new_ip_address while doing dhcp. retrying"
                    exit 1
                fi
            else
                if ! arping -f -q -D -c 2 -I "$netif" "$new_ip_address"; then
                    warn "Duplicate address detected for $new_ip_address while doing dhcp. retrying"
                    exit 1
                fi
            fi
        fi
        unset layer2
        setup_interface
        set | while read -r line || [ -n "$line" ]; do
            [ "${line#new_}" = "$line" ] && continue
            echo "$line"
        done > /tmp/dhclient."$netif".dhcpopts

        {
            echo '. /lib/net-lib.sh'
            echo "setup_net $netif"
            if [ -n "$new_classless_static_routes" ]; then
                OLDIFS="$IFS"
                IFS=".$IFS"
                parse_option_121 "$new_classless_static_routes"
                IFS="$OLDIFS"
            fi
            echo "source_hook initqueue/online $netif"
            [ -e /tmp/net."$netif".manualup ] || echo "/sbin/netroot $netif"
            echo "rm -f -- $hookdir/initqueue/setup_net_$netif.sh"
        } > "$hookdir"/initqueue/setup_net_"$netif".sh

        echo "[ -f /tmp/net.$netif.did-setup ]" > "$hookdir"/initqueue/finished/dhclient-"$netif".sh
        : > /tmp/net."$netif".up
        if [ -e /sys/class/net/"${netif}"/address ]; then
            : > "/tmp/net.$(cat /sys/class/net/"${netif}"/address).up"
        fi

        ;;

    RENEW | REBIND)
        unset lease_time
        [ -n "$new_dhcp_lease_time" ] && lease_time=$new_dhcp_lease_time
        [ -n "$new_max_life" ] && lease_time=$new_max_life
        preferred_lft=$lease_time
        [ -n "$new_preferred_life" ] && preferred_lft=$new_preferred_life
        ip -4 addr change "${new_ip_address}"/"${new_subnet_mask}" broadcast "${new_broadcast_address}" dev "${interface}" \
            ${lease_time:+valid_lft $lease_time} ${preferred_lft:+preferred_lft ${preferred_lft}} \
            > /dev/null 2>&1
        ;;

    BOUND6)
        echo "dhcp: BOUND6 setting up $netif"
        setup_interface6

        set | while read -r line || [ -n "$line" ]; do
            [ "${line#new_}" = "$line" ] && continue
            echo "$line"
        done > /tmp/dhclient."$netif".dhcpopts

        {
            echo '. /lib/net-lib.sh'
            echo "setup_net $netif"
            echo "source_hook initqueue/online $netif"
            [ -e /tmp/net."$netif".manualup ] || echo "/sbin/netroot $netif"
            echo "rm -f -- $hookdir/initqueue/setup_net_$netif.sh"
        } > "$hookdir"/initqueue/setup_net_"$netif".sh

        echo "[ -f /tmp/net.$netif.did-setup ]" > "$hookdir"/initqueue/finished/dhclient-"$netif".sh
        : > /tmp/net."$netif".up
        if [ -e /sys/class/net/"${netif}"/address ]; then
            : > "/tmp/net.$(cat /sys/class/net/"${netif}"/address).up"
        fi
        ;;

    RENEW6 | REBIND6)
        unset lease_time
        [ -n "$new_dhcp_lease_time" ] && lease_time=$new_dhcp_lease_time
        [ -n "$new_max_life" ] && lease_time=$new_max_life
        preferred_lft=$lease_time
        [ -n "$new_preferred_life" ] && preferred_lft=$new_preferred_life
        ip -6 addr change "${new_ip6_address}"/"${new_ip6_prefixlen}" dev "${interface}" scope global \
            ${lease_time:+valid_lft $lease_time} ${preferred_lft:+preferred_lft ${preferred_lft}} \
            > /dev/null 2>&1
        ;;

    *) echo "dhcp: $reason" ;;
esac

exit 0