interface 1.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
## /usr/lib/netctl/globals needs to be sourced before this file


## Check if a string represents a network interface
# $1: potential interface name
is_interface() {
    # Strip any old school alias specifier
    [[ -d "/sys/class/net/${1%%:?*}" ]]
}

## Add an interface
# $1: interface type
# $2: interface name
14
15
16
# $3: interface MAC address (optional)
# $4: interface link (optional)
# $5...: additional type related arguments
17
interface_add() {
18
19
20
21
22
    local type="$1" name="$2" address="$3" link="$4"
    if [[ -e "/sys/class/net/$address/address" ]]; then
        address=$(< "/sys/class/net/$address/address")
    fi
    do_debug ip link add ${link:+link "$link"} name "$name" ${address:+address "$address"} type "$type" "${@:5}" $LinkOptions || return
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
    load_interface_config "$name"
}

## Delete an interface
# $1: interface name
interface_delete() {
    bring_interface_down "$1"
    ip link delete "$1"
}

## Check if an interface is up
# $1: interface name
interface_is_up() {
    local flags
    read flags < "/sys/class/net/${1%%:?*}/flags"
    # IFF_UP is defined as 0x1 in linux/if.h
    (( flags & 0x1 ))
}

## Activate an interface
# $1: interface name
bring_interface_up() {
    local interface=$1
    ip link set dev "$interface" up &>/dev/null
    timeout_wait "${TimeoutUp:-5}" 'interface_is_up "$interface"'
}

## Deactivate an interface
# $1: interface name
bring_interface_down() {
    local interface=$1
    ip link set dev "$interface" down &>/dev/null
    # We reuse the up timeout (down normally is faster)
    timeout_wait "${TimeoutUp:-5}" '! interface_is_up "$interface"'
}


# vim: ft=sh ts=4 et sw=4: