summaryrefslogtreecommitdiffstats
path: root/modules.d/35network-legacy/ifup.sh
blob: 9827e2e8ce1868e8f90e3aa20a2882a55d2519c3 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/bin/sh
#
# We don't need to check for ip= errors here, that is handled by the
# cmdline parser script
#
# without $2 means this is for real netroot case
# or it is for manually bring up network ie. for kdump scp vmcore
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

# Huh? No $1?
[ -z "$1" ] && exit 1

# $netif reads easier than $1
netif=$1

# loopback is always handled the same way
if [ "$netif" = "lo" ]; then
    ip link set lo up
    ip addr add 127.0.0.1/8 dev lo
    exit 0
fi

do_dhcp_parallel() {
    # dhclient-script will mark the netif up and generate the online
    # event for nfsroot
    # XXX add -V vendor class and option parsing per kernel

    [ -e "/tmp/dhclient.$netif.pid" ] && return 0

    if ! iface_has_carrier "$netif"; then
        warn "No carrier detected on interface $netif"
        return 1
    fi

    bootintf=$(readlink "$IFNETFILE")
    if [ -n "$bootintf" ] && [ -e "/tmp/dhclient.${bootintf}.lease" ]; then
        info "DHCP already succeeded for $bootintf, exiting for $netif"
        return 1
    fi

    if [ ! -e /run/NetworkManager/conf.d/10-dracut-dhclient.conf ]; then
        mkdir -p /run/NetworkManager/conf.d
        echo '[main]' > /run/NetworkManager/conf.d/10-dracut-dhclient.conf
        echo 'dhcp=dhclient' >> /run/NetworkManager/conf.d/10-dracut-dhclient.conf
    fi

    chmod +x /sbin/dhcp-multi.sh
    /sbin/dhcp-multi.sh "$netif" "$DO_VLAN" "$@" &
    return 0
}

# Run dhclient
do_dhcp() {
    # dhclient-script will mark the netif up and generate the online
    # event for nfsroot
    # XXX add -V vendor class and option parsing per kernel

    local _COUNT
    local _timeout
    local _DHCPRETRY

    _COUNT=0
    _timeout=$(getarg rd.net.timeout.dhcp=)
    _DHCPRETRY=$(getargnum 1 1 1000000000 rd.net.dhcp.retry=)

    [ -e "/tmp/dhclient.${netif}.pid" ] && return 0

    if ! iface_has_carrier "$netif"; then
        warn "No carrier detected on interface $netif"
        return 1
    fi

    if [ -n "$_timeout" ]; then
        if ! (dhclient --help 2>&1 | grep -q -F -- '--timeout' 2> /dev/null); then
            warn "rd.net.timeout.dhcp has no effect because dhclient does not implement the --timeout option"
            unset _timeout
        fi
    fi

    if [ ! -e /run/NetworkManager/conf.d/10-dracut-dhclient.conf ]; then
        mkdir -p /run/NetworkManager/conf.d
        echo '[main]' > /run/NetworkManager/conf.d/10-dracut-dhclient.conf
        echo 'dhcp=dhclient' >> /run/NetworkManager/conf.d/10-dracut-dhclient.conf
    fi

    while [ "$_COUNT" -lt "$_DHCPRETRY" ]; do
        info "Starting dhcp for interface $netif"
        dhclient "$@" \
            ${_timeout:+--timeout "$_timeout"} \
            -q \
            -1 \
            -cf /etc/dhclient.conf \
            -pf "/tmp/dhclient.${netif}.pid" \
            -lf "/tmp/dhclient.${netif}.lease" \
            "$netif" \
            && return 0
        _COUNT=$((_COUNT + 1))
        [ "$_COUNT" -lt "$_DHCPRETRY" ] && sleep 1
    done
    warn "dhcp for interface $netif failed"
    # nuke those files since we failed; we might retry dhcp again if it's e.g.
    # `ip=dhcp,dhcp6` and we check for the PID file at the top
    rm -f /tmp/dhclient."$netif".pid /tmp/dhclient."$netif".lease
    return 1
}

load_ipv6() {
    [ -d /proc/sys/net/ipv6 ] && return
    modprobe ipv6
    i=0
    while [ ! -d /proc/sys/net/ipv6 ]; do
        i=$((i + 1))
        [ $i -gt 10 ] && break
        sleep 0.1
    done
}

do_ipv6auto() {
    local ret
    load_ipv6
    echo 0 > /proc/sys/net/ipv6/conf/"${netif}"/forwarding
    echo 1 > /proc/sys/net/ipv6/conf/"${netif}"/accept_ra
    echo 1 > /proc/sys/net/ipv6/conf/"${netif}"/accept_redirects
    linkup "$netif"
    wait_for_ipv6_auto "$netif"
    ret=$?

    [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > "/tmp/net.${netif}.hostname"

    return "$ret"
}

do_ipv6link() {
    local ret
    load_ipv6
    echo 0 > /proc/sys/net/ipv6/conf/"${netif}"/forwarding
    echo 0 > /proc/sys/net/ipv6/conf/"${netif}"/accept_ra
    echo 0 > /proc/sys/net/ipv6/conf/"${netif}"/accept_redirects
    linkup "$netif"

    [ -n "$hostname" ] && echo "echo $hostname > /proc/sys/kernel/hostname" > "/tmp/net.${netif}.hostname"

    return "$ret"
}

# Handle static ip configuration
do_static() {
    strglobin "$ip" '*:*:*' && load_ipv6

    if ! iface_has_carrier "$netif"; then
        warn "No carrier detected on interface $netif"
        return 1
    elif ! linkup "$netif"; then
        warn "Could not bring interface $netif up!"
        return 1
    fi

    ip route get "$ip" 2> /dev/null | {
        read -r a rest
        if [ "$a" = "local" ]; then
            warn "Not assigning $ip to interface $netif, cause it is already assigned!"
            return 1
        fi
        return 0
    } || return 1

    [ -n "$macaddr" ] && ip link set address "$macaddr" dev "$netif"
    [ -n "$mtu" ] && ip link set mtu "$mtu" dev "$netif"
    if strglobin "$ip" '*:*:*'; then
        # note no ip addr flush for ipv6
        ip addr add "$ip/$mask" ${srv:+peer "$srv"} dev "$netif"
        echo 0 > /proc/sys/net/ipv6/conf/"${netif}"/forwarding
        echo 1 > /proc/sys/net/ipv6/conf/"${netif}"/accept_ra
        echo 1 > /proc/sys/net/ipv6/conf/"${netif}"/accept_redirects
        wait_for_ipv6_dad "$netif"
    else
        if [ -z "$srv" ]; then
            if command -v arping2 > /dev/null; then
                if arping2 -q -C 1 -c 2 -I "$netif" -0 "$ip"; then
                    warn "Duplicate address detected for $ip for interface $netif."
                    return 1
                fi
            else
                if ! arping -f -q -D -c 2 -I "$netif" "$ip"; then
                    warn "Duplicate address detected for $ip for interface $netif."
                    return 1
                fi
            fi
        fi
        ip addr flush dev "$netif"
        ip addr add "$ip/$mask" ${srv:+peer "$srv"} brd + dev "$netif"
    fi

    [ -n "$gw" ] && echo "ip route replace default via '$gw' dev '$netif'" > "/tmp/net.$netif.gw"
    [ -n "$hostname" ] && echo "echo '$hostname' > /proc/sys/kernel/hostname" > "/tmp/net.$netif.hostname"

    return 0
}

get_vid() {
    case "$1" in
        vlan*)
            echo "${1#vlan}"
            ;;
        *.*)
            echo "${1##*.}"
            ;;
    esac
}

# check, if we need VLAN's for this interface
if [ -z "$DO_VLAN_PHY" ] && [ -e "/tmp/vlan.${netif}.phy" ]; then
    unset DO_VLAN
    NO_AUTO_DHCP=yes DO_VLAN_PHY=yes ifup "$netif"
    modprobe -b -q 8021q

    for i in /tmp/vlan.*."${netif}"; do
        [ -e "$i" ] || continue
        unset vlanname
        unset phydevice
        # shellcheck disable=SC1090
        . "$i"
        if [ -n "$vlanname" ]; then
            linkup "$phydevice"
            ip link add dev "$vlanname" link "$phydevice" type vlan id "$(get_vid "$vlanname")"
            ifup "$vlanname"
        fi
    done
    exit 0
fi

# Check, if interface is VLAN interface
if ! [ -e "/tmp/vlan.${netif}.phy" ]; then
    for i in "/tmp/vlan.${netif}".*; do
        [ -e "$i" ] || continue
        export DO_VLAN=yes
        break
    done
fi

# bridge this interface?
if [ -z "$NO_BRIDGE_MASTER" ]; then
    for i in /tmp/bridge.*.info; do
        [ -e "$i" ] || continue
        unset bridgeslaves
        unset bridgename
        # shellcheck disable=SC1090
        . "$i"
        for ethname in $bridgeslaves; do
            [ "$netif" != "$ethname" ] && continue

            NO_BRIDGE_MASTER=yes NO_AUTO_DHCP=yes ifup "$ethname"
            linkup "$ethname"
            if [ ! -e "/tmp/bridge.$bridgename.up" ]; then
                ip link add name "$bridgename" type bridge
                echo 0 > "/sys/devices/virtual/net/$bridgename/bridge/forward_delay"
                : > "/tmp/bridge.$bridgename.up"
            fi
            ip link set dev "$ethname" master "$bridgename"
            ifup "$bridgename"
            exit 0
        done
    done
fi

# enslave this interface to bond?
if [ -z "$NO_BOND_MASTER" ]; then
    for i in /tmp/bond.*.info; do
        [ -e "$i" ] || continue
        unset bondslaves
        unset bondname
        # shellcheck disable=SC1090
        . "$i"
        for testslave in $bondslaves; do
            [ "$netif" != "$testslave" ] && continue

            # already setup
            [ -e "/tmp/bond.$bondname.up" ] && exit 0

            # wait for all slaves to show up
            for slave in $bondslaves; do
                # try to create the slave (maybe vlan or bridge)
                NO_BOND_MASTER=yes NO_AUTO_DHCP=yes ifup "$slave"

                if ! ip link show dev "$slave" > /dev/null 2>&1; then
                    # wait for the last slave to show up
                    exit 0
                fi
            done

            modprobe -q -b bonding
            echo "+$bondname" > /sys/class/net/bonding_masters 2> /dev/null
            ip link set "$bondname" down

            # Stolen from ifup-eth
            # add the bits to setup driver parameters here
            for arg in $bondoptions; do
                key=${arg%%=*}
                value=${arg##*=}
                # %{value:0:1} is replaced with non-bash specific construct
                if [ "${key}" = "arp_ip_target" ] && [ "${#value}" != "0" ] && [ "+${value%%+*}" != "+" ]; then
                    OLDIFS=$IFS
                    IFS=','
                    for arp_ip in $value; do
                        echo "+$arp_ip" > "/sys/class/net/${bondname}/bonding/$key"
                    done
                    IFS=$OLDIFS
                else
                    echo "$value" > "/sys/class/net/${bondname}/bonding/$key"
                fi
            done

            linkup "$bondname"

            for slave in $bondslaves; do
                cat "/sys/class/net/$slave/address" > "/tmp/net.${bondname}.${slave}.hwaddr"
                ip link set "$slave" down
                echo "+$slave" > "/sys/class/net/$bondname/bonding/slaves"
                linkup "$slave"
            done

            # Set mtu on bond master
            [ -n "$bondmtu" ] && ip link set mtu "$bondmtu" dev "$bondname"

            # add the bits to setup the needed post enslavement parameters
            for arg in $bondoptions; do
                key=${arg%%=*}
                value=${arg##*=}
                if [ "${key}" = "primary" ]; then
                    echo "$value" > "/sys/class/net/${bondname}/bonding/$key"
                fi
            done

            : > "/tmp/bond.$bondname.up"

            NO_BOND_MASTER=yes ifup "$bondname"
            exit $?
        done
    done
fi

if [ -z "$NO_TEAM_MASTER" ]; then
    for i in /tmp/team.*.info; do
        [ -e "$i" ] || continue
        unset teammaster
        unset teamslaves
        # shellcheck disable=SC1090
        . "$i"
        for testslave in $teamslaves; do
            [ "$netif" != "$testslave" ] && continue

            [ -e "/tmp/team.$teammaster.up" ] && exit 0

            # wait for all slaves to show up
            for slave in $teamslaves; do
                # try to create the slave (maybe vlan or bridge)
                NO_TEAM_MASTER=yes NO_AUTO_DHCP=yes ifup "$slave"

                if ! ip link show dev "$slave" > /dev/null 2>&1; then
                    # wait for the last slave to show up
                    exit 0
                fi
            done

            if [ ! -e "/tmp/team.$teammaster.up" ]; then
                # We shall only bring up those _can_ come up
                # in case of some slave is gone in active-backup mode
                working_slaves=""
                for slave in $teamslaves; do
                    teamdctl "${teammaster}" port present "${slave}" 2> /dev/null \
                        && continue
                    ip link set dev "$slave" up 2> /dev/null
                    if wait_for_if_up "$slave"; then
                        working_slaves="$working_slaves$slave "
                    fi
                done
                # Do not add slaves now
                teamd -d -U -n -N -t "$teammaster" -f "/etc/teamd/${teammaster}.conf"
                for slave in $working_slaves; do
                    # team requires the slaves to be down before joining team
                    ip link set dev "$slave" down
                    (
                        unset TEAM_PORT_CONFIG
                        read -r _hwaddr < "/sys/class/net/$slave/address"
                        _subchannels=$(iface_get_subchannels "$slave")
                        if [ -n "$_hwaddr" ] && [ -e "/etc/sysconfig/network-scripts/mac-${_hwaddr}.conf" ]; then
                            # shellcheck disable=SC1090
                            . "/etc/sysconfig/network-scripts/mac-${_hwaddr}.conf"
                        elif [ -n "$_subchannels" ] && [ -e "/etc/sysconfig/network-scripts/ccw-${_subchannels}.conf" ]; then
                            # shellcheck disable=SC1090
                            . "/etc/sysconfig/network-scripts/ccw-${_subchannels}.conf"
                        elif [ -e "/etc/sysconfig/network-scripts/ifcfg-${slave}" ]; then
                            # shellcheck disable=SC1090
                            . "/etc/sysconfig/network-scripts/ifcfg-${slave}"
                        fi

                        if [ -n "${TEAM_PORT_CONFIG}" ]; then
                            /usr/bin/teamdctl "${teammaster}" port config update "${slave}" "${TEAM_PORT_CONFIG}"
                        fi
                    )
                    teamdctl "$teammaster" port add "$slave"
                done

                ip link set dev "$teammaster" up

                : > "/tmp/team.$teammaster.up"
                NO_TEAM_MASTER=yes ifup "$teammaster"
                exit $?
            fi
        done
    done
fi

# all synthetic interfaces done.. now check if the interface is available
if ! ip link show dev "$netif" > /dev/null 2>&1; then
    exit 1
fi

# disable manual ifup while netroot is set for simplifying our logic
# in netroot case we prefer netroot to bringup $netif automatically
[ -n "$2" ] && [ "$2" = "-m" ] && [ -z "$netroot" ] && manualup="$2"

if [ -n "$manualup" ]; then
    : > "/tmp/net.$netif.manualup"
    rm -f "/tmp/net.${netif}.did-setup"
else
    [ -e "/tmp/net.${netif}.did-setup" ] && exit 0
    [ -z "$DO_VLAN" ] \
        && [ -e "/sys/class/net/$netif/address" ] \
        && [ -e "/tmp/net.$(cat "/sys/class/net/$netif/address").did-setup" ] && exit 0
fi

# Specific configuration, spin through the kernel command line
# looking for ip= lines
for p in $(getargs ip=); do
    ip_to_var "$p"
    # skip ibft
    [ "$autoconf" = "ibft" ] && continue

    case "$dev" in
        ??:??:??:??:??:??) # MAC address
            _dev=$(iface_for_mac "$dev")
            [ -n "$_dev" ] && dev="$_dev"
            ;;
        ??-??-??-??-??-??) # MAC address in BOOTIF form
            _dev=$(iface_for_mac "$(fix_bootif "$dev")")
            [ -n "$_dev" ] && dev="$_dev"
            ;;
    esac

    # If this option isn't directed at our interface, skip it
    if [ -n "$dev" ]; then
        if [ "$dev" != "$netif" ]; then
            [ ! -e "/sys/class/net/$dev" ] \
                && warn "Network interface '$dev' does not exist!"
            continue
        fi
    else
        iface_is_enslaved "$netif" && continue
    fi

    # Store config for later use
    for i in ip srv gw mask hostname macaddr mtu dns1 dns2; do
        eval '[ "$'$i'" ] && echo '$i'="$'$i'"'
    done > "/tmp/net.$netif.override"

    for autoopt in $(str_replace "$autoconf" "," " "); do
        case $autoopt in
            dhcp | on | any)
                do_dhcp -4
                ;;
            single-dhcp)
                do_dhcp_parallel -4
                exit 0
                ;;
            dhcp6)
                load_ipv6
                do_dhcp -6
                ;;
            auto6)
                do_ipv6auto
                ;;
            either6)
                do_ipv6auto || do_dhcp -6
                ;;
            link6)
                do_ipv6link
                ;;
            *)
                do_static
                ;;
        esac
    done
    ret=$?

    # setup nameserver
    for s in "$dns1" "$dns2" $(getargs nameserver); do
        [ -n "$s" ] || continue
        echo "nameserver $s" >> "/tmp/net.$netif.resolv.conf"
    done

    if [ $ret -eq 0 ]; then
        : > "/tmp/net.${netif}.up"

        if [ -z "$DO_VLAN" ] && [ -e "/sys/class/net/${netif}/address" ]; then
            : > "/tmp/net.$(cat "/sys/class/net/${netif}/address").up"
        fi

        # and finally, finish interface set up if there isn't already a script
        # to do so (which is the case in the dhcp path)
        if [ ! -e "$hookdir/initqueue/setup_net_$netif.sh" ]; then
            setup_net "$netif"
            source_hook initqueue/online "$netif"
            if [ -z "$manualup" ]; then
                /sbin/netroot "$netif"
            fi
        fi

        exit $ret
    fi
done

# no ip option directed at our interface?
if [ -z "$NO_AUTO_DHCP" ] && [ ! -e "/tmp/net.${netif}.up" ]; then
    ret=1
    if [ -e /tmp/net.bootdev ]; then
        read -r BOOTDEV < /tmp/net.bootdev
        if [ "$netif" = "$BOOTDEV" ] || [ "$BOOTDEV" = "$(cat "/sys/class/net/${netif}/address")" ]; then
            do_dhcp
            ret=$?
        fi
    else
        # No ip lines, no bootdev -> default to dhcp
        ip=$(getarg ip)

        if getargs 'ip=dhcp6' > /dev/null || [ -z "$ip" ] && [ "$netroot" = "dhcp6" ]; then
            load_ipv6
            do_dhcp -6
            ret=$?
        fi
        if getargs 'ip=dhcp' > /dev/null || [ -z "$ip" ] && [ "$netroot" != "dhcp6" ]; then
            do_dhcp -4
            ret=$?
        fi
    fi

    for s in $(getargs nameserver); do
        [ -n "$s" ] || continue
        echo "nameserver $s" >> "/tmp/net.$netif.resolv.conf"
    done

    if [ "$ret" -eq 0 ] && [ -n "$(ls "/tmp/leaseinfo.${netif}"* 2> /dev/null)" ]; then
        : > "/tmp/net.${netif}.did-setup"
        if [ -e "/sys/class/net/${netif}/address" ]; then
            : > "/tmp/net.$(cat "/sys/class/net/${netif}/address").did-setup"
        fi
    fi
fi

exit 0