summaryrefslogtreecommitdiffstats
path: root/src/connections/ethernet
blob: 5414c69744284126d8cc02f96e6067317dbc944f (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
#! /bin/bash
. /usr/lib/network/network

ethernet_up() {
    load_profile "$1"

    if [[ ! -e "/sys/class/net/$INTERFACE" ]]; then
        if ! echo "$INTERFACE" | fgrep -q ":"; then
            report_fail "interface $INTERFACE does not exist"
            return 1
        fi
    fi

    report_debug ethernet_up ifup
    set_interface up-old "$INTERFACE"

    # don't think it's possible to detect carrier using ifconfig alone (at least, not without ifdown/ifupping the interface)
    if [[ $(cat "/sys/class/net/$INTERFACE/carrier" 2>/dev/null) -ne 1 ]]; then   # gives err if iface inactive (i.e. ifdown)
       report_fail "No connection"
       return 1
    fi

    if checkyesno "${AUTH8021X:-no}"; then
        . "$SUBR_DIR/8021x"
        [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf"
        [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwired"
        report_debug ethernet_up start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS"
        if ! start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS"; then
            report_fail "wpa_supplicant did not start, possible configuration error"
            return 1
        fi
        if ! wpa_check "$INTERFACE"; then
            # ignore quirk nodown---is that appropriate?
            # this adds a flush call as well---is that appropriate?
            set_interface forcedown-old "$INTERFACE"
            report_fail "WPA Authentication/Association Failed"
            return 1
        fi
    fi

    case "$IP" in
    dhcp)
        if checkyesno "${DHCLIENT:-no}"; then
            rm -r "/var/run/dhclient-${INTERFACE}.pid" >/dev/null 2>&1
            report_debug ethernet_up dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf "/var/run/dhclient-$INTERFACE.pid" "$INTERFACE"
            if ! dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf "/var/run/dhclient-${INTERFACE}.pid" "$INTERFACE"; then
                report_fail "DHCP IP lease attempt failed."
                return 1
            fi
        else
            # Clear remaining pid files.
            rm -f "/var/run/dhcpcd-$INTERFACE".{pid,cache} >/dev/null 2>&1
            # If using own dns, tell dhcpcd to NOT replace resolv.conf
            [[ -n "$DNS1" || -n "$DNS" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS"
            # Start dhcpcd
            report_debug ethernet_up dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE"
            dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE" 2>&1 | report_debug "$(cat)"
            if [[ "$PIPESTATUS" -ne 0 ]]; then
                report_fail "DHCP IP lease attempt failed."
                return 1
            fi
        fi
        if [[ -n "$IFOPTS" ]]; then
            report_debug ethernet_up ifup $IFOPTS
            ifconfig "$INTERFACE" $IFOPTS
        fi
        ;;
    static)
        report_debug ethernet_up ifup $IFOPTS
        if ! ifconfig "$INTERFACE" $IFOPTS up; then
            report_fail "Bringing interface up failed."
            return 1
        fi

        # bring up the default route (gateway)
        if [[ -n "$GATEWAY" ]]; then
            report_debug ethernet_up route add default gw "$GATEWAY"
            if ! route add default gw "$GATEWAY"; then
                # JP: don't we want to add this to all the aborts?
                # ignore quirk nodown---is that appropriate?
                # this adds a flush call as well---is that appropriate?
                set_interface forcedown-old "$INTERFACE"
                report_fail "Adding gateway $GATEWAY failed."
                return 1
            fi
        fi
        ;;
      *)
        report_fail "IP must be either 'dhcp' or 'static'."
        return 1
        ;;
    esac

    # set the hostname
    if [[ -n "$HOSTNAME" ]]; then
        report_debug ethernet_up hostname "$HOSTNAME"
        if ! hostname "$HOSTNAME"; then
            report_fail "Setting hostname $HOSTNAME failed."
            return 1
        fi
    fi

    # Generate a new resolv.conf
    if [[ -n "$DNS1" || -n "$DNS" ]]; then

        : >/etc/resolv.conf
        [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN"   >>/etc/resolv.conf
        [[ -n "$SEARCH" ]] && echo "search $SEARCH"   >>/etc/resolv.conf
        [[ -n "$DNS1" ]]   && echo "nameserver $DNS1" >>/etc/resolv.conf
        [[ -n "$DNS2" ]]   && echo "nameserver $DNS2" >>/etc/resolv.conf

        if [[ -n "$DNS" ]]; then
            for dns in "${DNS[@]}"; do
                echo "nameserver $dns" >>/etc/resolv.conf
            done
        fi
    fi
    return 0
}

ethernet_down() {
    load_profile "$1"
    case "$IP" in
    dhcp)
        if checkyesno "${DHCLIENT:-no}"; then
            if [[ -f "/var/run/dhclient-${INTERFACE}.pid" ]]; then
                report_debug ethernet_down kill dhclient
                kill "$(cat "/var/run/dhclient-${INTERFACE}.pid")"
            fi
        else
            if [[ -f "/var/run/dhcpcd-${INTERFACE}.pid" ]]; then
                report_debug ethernet_down dhcpcd -qx "$INTERFACE"
                dhcpcd -qx "$INTERFACE" &>/dev/null
            fi
        fi
        ;;
    static)
        if [[ -n "$GATEWAY" ]]; then
            report_debug ethernet_down route del default gw "$GATEWAY"
            route del default gw "$GATEWAY"
        fi
        ;;
    esac

    report_debug ethernet_down ifdown
    # ignore quirk nodown---is that appropriate?
    # this adds a flush call as well---is that appropriate?
    set_interface forcedown-old "$INTERFACE"
}

# Returns status of profile - is it still functional?
ethernet_status() {
    if ! ip link show dev ra0 | fgrep -q "state UP"; then
        return 1
    fi
}

ethernet_$1 "$2"
exit $?
# vim: set ts=4 et sw=4: