summaryrefslogtreecommitdiffstats
path: root/src/ethernet.subr
blob: 88234713831f9c06513a18b78d37d7c39aaab0cc (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
#! /bin/bash

mii_check() {
    local conn_state=$(mii-tool $1 2> /dev/null)
    local ret=$?
    if echo $conn_state|grep "no link" &> /dev/null; then
        if [[ $ret -eq 0 ]]; then
            return 1
        fi
    fi
    return 0
}

ethernet_up() {
    if [[ ! -e /sys/class/net/$INTERFACE ]]; then
        if ! echo "$INTERFACE"|grep ":"; then
            err_append "Interface $INTERFACE does not exist"
        fi
    fi  

	if ! mii_check $INTERFACE; then
        err_append "No connection available"	
    	return 1
    fi

	ifconfig $INTERFACE up
     
    if checkyesno ${8021X:-no}; then
        . /usr/lib/network/wireless.subr
         [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf"
        [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwired"
        start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS"
        if ! wpa_check "$INTERFACE"; then
            ifconfig $INTERFACE down
            return 1
        fi 
    fi 

    case $IP in
    dhcp)
        # Check if DHCP_TIMEOUT was set if not set a default value
        [[ -z "$DHCP_TIMEOUT" ]] && DHCP_TIMEOUT=10
        
        if checkyesno $DHCLIENT; then
            rm -r /var/run/dhclient-${INTERFACE}.pid >/dev/null 2>&1
            if ! dhclient -q -e TIMEOUT=$DHCP_TIMEOUT -pf /var/run/dhclient-${INTERFACE}.pid $INTERFACE; then
                err_append "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" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS"
            # Start dhcpcd
            if ! dhcpcd -qL -t $DHCP_TIMEOUT $DHCP_OPTIONS $INTERFACE; then
                err_append "DHCP IP lease attempt failed"
                return 1
            fi 
        fi
        # TODO: add support for $section:ifconfig for other stuff, like mtu or mac?
        ;;
    static)
        if ! ifconfig $INTERFACE $IFOPTS up; then
            err_append "Could not bring interface up"
            return 1
        fi

        # bring up the default route (gateway)
        if [[ -n "$GATEWAY" ]]; then
            if ! route add default gw $GATEWAY; then
                err_append "Adding gateway $GATEWAY failed"
                return 1
            fi 
        fi
        ;;
      *)
        err_append "Profile error: IP must be either 'dhcp' or 'static'"
        return 1
        ;;
    esac

    # set the hostname
    if [[ -n "$HOSTNAME" ]]; then
        if ! hostname $HOSTNAME; then
            err_append "Cannot set hostname"
            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() {
    case $IP in
    dhcp)
        if checkyesno $DHCLIENT; then
            if [[ -f /var/run/dhclient-${INTERFACE}.pid ]]; then
                kill `cat /var/run/dhclient-${INTERFACE}.pid`
            fi
        else
            if [[ -f /var/run/dhcpcd-${INTERFACE}.pid ]]; then
                dhcpcd -qx $INTERFACE
            fi
        fi
        ;;
    static)
        [[ -n "$GATEWAY" ]] && route del default gw $GATEWAY
        ;;
    esac
    ifconfig $INTERFACE 0.0.0.0
    
    quirk "nodown" || ifconfig $INTERFACE down

}

ethernet_clean_scope() {
    unset DOMAIN SEARCH DNS1 DNS2
    unset GATEWAY IFOPTS HOSTNAME DHCP_OPTIONS DHCP_TIMEOUT DHCLIENT
    unset INTERFACE CONNECTION
}
# vim: set ts=4 et sw=4: