summaryrefslogtreecommitdiffstats
path: root/src/connections/ethernet
blob: 78f8240ddc3ebba80acf14092fa1d27c5c469728 (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
#! /bin/bash
# Source file for the 'ethernet' connection
#   ethernet_up $profile
#   ethernet_down $profile
#   ethernet_status

. /usr/lib/network/network

report_iproute()
{
    report_fail "$*"
    bring_interface down "$INTERFACE"
    exit 1
}

ethernet_up() {
    load_profile "$1"

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

    # Disable IPv6 before the interface to prevent SLAAC
    if [[ "$IP6" == "no"]]; then
        sysctl -q -w net.ipv6.conf.$INTERFACE.disable_ipv6=1
    fi

    report_debug ethernet_iproute_up ifup
    bring_interface up "$INTERFACE"


    if ! checkyesno "${SKIPNOCARRIER:-no}" && ip link show dev "$INTERFACE" | fgrep -q "NO-CARRIER"; then
        sleep ${CARRIER_TIMEOUT:-2} # Some cards are plain slow to come up. Don't fail immediately.
        if ip link show dev "$INTERFACE" | fgrep -q "NO-CARRIER"; then
            report_iproute "No connection"
        fi
    fi

    if checkyesno "${AUTH8021X:-no}"; then
        . "$SUBR_DIR/8021x"
        [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf"
        [[ -z "$WPA_DRIVER" ]] && WPA_DRIVER="wired"
        # Set wpa_supplicant control path (FS#25473)
        WPA_CTRL_PATH=/var/run/wpa_supplicant
        if grep "^ *ctrl_interface=" "$WPA_CONF" &>/dev/null; then
            WPA_CTRL_PATH=$(grep -m 1 "^ *ctrl_interface=" "$WPA_CONF" | tail -n 1 | cut -d= -f 2- | sed -r 's/DIR=(.*) +GROUP=.*/\1/')
        fi
 
        report_debug ethernet_iproute_up start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_DRIVER" "$WPA_OPTS"
        if ! start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_DRIVER" "$WPA_OPTS"; then
            report_fail "wpa_supplicant did not start, possible configuration error"
            return 1
        fi

        if ! wpa_check "$INTERFACE" 15 "ASSOCIATED"; then
            bring_interface down "$INTERFACE"
            report_fail "WPA Authentication/Association Failed"
            return 1
        fi
    fi

    if [[ -z "$IP" && -z "$IP6" ]]; then
        report_iproute "At least one of IP or IP6 should be specified"
        return 1
    fi

    case "$IP" in
    dhcp)
        if checkyesno "${DHCLIENT:-no}"; then
            rm -r "/run/dhclient-${INTERFACE}.pid" >/dev/null 2>&1
            report_debug ethernet_up dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf "/run/dhclient-$INTERFACE.pid" "$INTERFACE"
            if ! dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf "/run/dhclient-${INTERFACE}.pid" ${DHCLIENT_OPTIONS} "$INTERFACE"; then
                report_fail "DHCP IP lease attempt failed."
                stop_80211x
                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."
                stop_80211x
                return 1
            fi
        fi
        ;;
    static)
        if [[ -n "$ADDR" ]]; then
            [[ -z $NETMASK ]] && NETMASK=24
            report_debug ethernet_iproute_up ip addr add "$ADDR/$NETMASK" brd + dev "$INTERFACE" 
            if ! ip addr add "$ADDR/$NETMASK" brd + dev "$INTERFACE"; then
                report_iproute "Could not configure interface"
            fi
        fi
        if [[ -n "$IFOPTS" ]]; then
            if ! ifconfig "$INTERFACE" $IFOPTS up; then
                report_iproute "Bringing interface up failed."
                return 1
            fi
        fi
        if [[ -n "$ROUTES" ]]; then
            for route in "${ROUTES[@]}"; do
                report_debug ethernet_iproute_up ip route add $route dev "$INTERFACE"
                if ! ip route add $route dev "$INTERFACE" ; then
                    report_iproute "Adding route '$route' failed"
                fi
            done
        fi
        if [[ -n "$GATEWAY" ]]; then
            report_debug ethernet_iproute_up ip route add default via "$GATEWAY" dev "$INTERFACE"
            if ! ip route add default via "$GATEWAY" dev "$INTERFACE"; then
                report_iproute "Adding gateway $GATEWAY failed"
            fi
        fi
        ;;
    ""|no)
        ;;
      *)
        report_iproute "IP must be either 'dhcp', 'static' or 'no'"
        ;;
    esac

    if [[ -n "$IPCFG" ]]; then
        for line in "${IPCFG[@]}"; do

            report_debug ethernet_iproute_up ip "$line"
            if ! ip $line; then
                report_iproute "Could not configure interface ($line)."
            fi
        done
    fi

    # Load ipv6 module if necessary (FS#25530)
    case "$IP6" in
    dhcp*|stateless|static)
        [ -d "/proc/sys/net/ipv6" ] || modprobe ipv6
        ;;
    no)
        [ -d /proc/sys/net/ipv6 ] && sysctl -q -w net.ipv6.conf.$INTERFACE.accept_ra=0
        ;;
    *)
        report_iproute "IP6 must be 'dhcp', 'dhcp-noaddr', 'stateless', 'static' or 'no'"
        ;;
    esac

    case "$IP6" in
    dhcp*)
        if [[ -x /usr/sbin/dhclient ]]; then
            _DHCLIENT_PIDFILE=/run/dhclient6-${INTERFACE}.pid
            if [[ "$IP6" = "dhcp-noaddr" ]]; then
                sysctl -q -w net.ipv6.conf.$INTERFACE.accept_ra=1
                DHCLIENT6_OPTIONS="-S ${DHCLIENT6_OPTIONS}"
            else
                sysctl -q -w net.ipv6.conf.$INTERFACE.accept_ra=0
            fi
            rm -r ${_DHCLIENT_PIDFILE} &>/dev/null
            report_debug ethernet_up dhclient -6 -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf ${_DHCLIENT_PIDFILE} "$INTERFACE"
            if ! dhclient -6 -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf ${_DHCLIENT_PIDFILE} ${DHCLIENT6_OPTIONS} "$INTERFACE"; then
                report_fail "DHCPv6 IP lease attempt failed."
                stop_80211x
                return 1
            fi
        else
            report_fail "You need to install dhclient to use DHCPv6."
        fi
        ;;
    stateless)
        sysctl -q -w net.ipv6.conf.$INTERFACE.accept_ra=1
        ;;
    static)
        sysctl -q -w net.ipv6.conf.$INTERFACE.accept_ra=0
        if [[ -n "$ADDR6" ]]; then
            for addr in "${ADDR6[@]}"; do
                report_debug ethernet_iproute_up ip -6 addr add "$addr" dev "$INTERFACE"
                if ! ip -6 addr add "$addr" dev "$INTERFACE"; then
                    report_iproute "Could not add address $addr to interface"
                fi
            done
        fi
        if [[ -n "$GATEWAY6" ]]; then
            report_debug ethernet_iproute_up ip -6 route add default via "$GATEWAY6" dev "$INTERFACE"
            if ! ip -6 route add default via "$GATEWAY6" dev "$INTERFACE"; then
                report_iproute "Adding gateway $GATEWAY6 failed"
            fi
        fi

        # Add static IPv6 routes
        if [[ -n "$ROUTES6" ]]; then
            for route in "${ROUTES6[@]}"; do
            report_debug ethernet_iproute_up ip -6 route add $route dev $INTERFACE
                if ! ip -6 route add $route dev $INTERFACE ; then
                    report_iproute "Adding route '$route' failed"
                fi
            done
        fi
        ;;
    esac

    # Set hostname
    if [[ -n "$HOSTNAME" ]]; then
        report_debug ethernet_iproute_up hostname "$HOSTNAME"
        if ! echo "$HOSTNAME" > /proc/sys/kernel/hostname; then
            report_iproute "Cannot set hostname to $HOSTNAME"
        fi
    fi

    # Generate a new resolv.conf
    if [[ -n "$DNS" ]]; then
        : >/etc/resolv.conf
        [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN"   >>/etc/resolv.conf
        [[ -n "$SEARCH" ]] && echo "search $SEARCH"   >>/etc/resolv.conf

        for dns in "${DNS[@]}"; do
            echo "nameserver $dns" >>/etc/resolv.conf
        done
    elif [[ -n "$DNS1" ]]; then # support older 'ethernet' syntax.
        : >/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
    fi

    return 0
}

ethernet_down() {
    load_profile "$1"

    if [[ "$IP" == "dhcp" ]]; then
        if checkyesno "${DHCLIENT:-no}"; then
            if [[ -f "/run/dhclient-$INTERFACE.pid" ]]; then
                report_debug ethernet_down dhclient -q -r "$INTERFACE"
                dhclient -q -x "$INTERFACE" &>/dev/null
                #dhclient -q -r "$INTERFACE" &>/dev/null
                report_debug ethernet_down /bin/kill $(cat /run/dhclient-$INTERFACE.pid)
                /bin/kill $(cat /run/dhclient-$INTERFACE.pid) &>/dev/null
            fi
        else
            if [[ -f "/var/run/dhcpcd-$INTERFACE.pid" ]]; then
                report_debug ethernet_down dhcpcd -qk "$INTERFACE"
                dhcpcd -qk "$INTERFACE" &>/dev/null
            fi
        fi
    fi
    if [[ "$IP6" == dhcp* ]]; then
        if [[ -f "/run/dhclient6-$INTERFACE.pid" ]]; then
            report_debug ethernet_down dhclient -6 -q -x "$INTERFACE"
            dhclient -6 -q -x "$INTERFACE" &>/dev/null
            report_debug ethernet_down /bin/kill $(cat /run/dhclient6-$INTERFACE.pid)
            /bin/kill $(cat /run/dhclient6-$INTERFACE.pid) &>/dev/null
        fi
    fi

    stop_80211x

    report_debug ethernet_down if_down
    if [[ "$CONNECTION" == "wireless" ]]; then
        bring_interface flush "$INTERFACE"
    else
        bring_interface down "$INTERFACE"
    fi
    return 0 
}

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

# Stop wpa_supplicant if neccessary 
stop_80211x() {
    if checkyesno "${AUTH8021X:-no}"; then
        . "$SUBR_DIR/8021x"
        report_debug ethernet_down stop_wpa "$INTERFACE"
        stop_wpa "$INTERFACE"
    fi
}

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