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


rfkill_from_name() {
    local name=$1
    for rfkill in /sys/class/rfkill/*; do
        if [[ "$(cat $rfkill/name)" == $name ]]; then
            echo $rfkill
            return 0
        fi
    done
    echo "none"
    return 1
}

wireless_up() {

    load_profile $1
    . ${SUBR_DIR}/8021x
    . ${SUBR_DIR}/wireless

    # If rfkill is specified, enable device.
    if [[ -n "$RFKILL_NAME" ]]; then
        path=$(rfkill_from_name $RFKILL_NAME)
        if [[ $? -ne 0 ]]; then
            err_append "no rfkill switch with the name $RFKILL_NAME";
        fi 
        echo 1 > ${path}/state
    fi

    # Check if interface exists
    if [[ ! -e /sys/class/net/"$INTERFACE" ]]; then
        if ! echo "$INTERFACE"|grep ":"; then
            err_append "interface $INTERFACE does not exist"
            return 1
        fi
    fi  

    # Kill any lingering wpa_supplicants.
    stop_wpa $INTERFACE 
            
    # Most drivers (mac80211) need mode set before device is brought up
    # Drivers generally default to managed, but set this to be sure.
    if [[ "$(iwgetid -sm $INTERFACE)" -ne "Managed" ]]; then
        iwconfig $INTERFACE mode managed
    fi

    ifconfig $INTERFACE up 

    quirk "prescan" && iwlist $INTERFACE scan &> /dev/null # bcm43xx
    quirk "preessid" && eval "iwconfig $INTERFACE mode managed essid \"$ESSID\"" # ipw3945
 
    if checkyesno ${SCAN:-no}; then
        if ! find_essid $INTERFACE "$ESSID"; then
            err_append "Network not present."
            return 1
        fi 
    fi
  
    # Manually set iwconfig options
    [[ "$IWCONFIG" ]] && iwconfig $INTERFACE $IWCONFIG

    # Set to 'none' if not set
    [[ -z "$SECURITY" ]] && SECURITY="none"

    case $SECURITY in
    wep|none)
        # 'none' uses iwconfig like wep. Use sane default if WEP_OPTS=""
        if [[ "$SECURITY" = "wep" ]]; then 
            WEP_OPTS="essid \"$ESSID\" key $KEY"
        elif [[ "$SECURITY" = "none" ]]; then
            WEP_OPTS="essid \"$ESSID\""
        fi 
              
        quirk "predown" && ifconfig $INTERFACE down # madwifi FS#10585

        if ! eval iwconfig $INTERFACE $WEP_OPTS; then
            err_append "Could not set wireless configuration."
            return 1
        fi

        quirk "predown" && ifconfig $INTERFACE up # madwifi FS#10585
       
        wep_check $INTERFACE $TIMEOUT||return 1
        ;;    
    wpa)

        # Quirk for broken drivers... http://bbs.archlinux.org/viewtopic.php?id=36384
        quirk "wpaessid" && eval iwconfig $INTERFACE essid "\"$ESSID\""
         
        local WPA_CONF="/tmp/wpa.${1// /}" # substitute spaces out 
        echo "ctrl_interface=/var/run/wpa_supplicant" >> $WPA_CONF
        echo "ctrl_interface_group=${WPA_GROUP:-wheel}" >> $WPA_CONF        
        chmod 600 $WPA_CONF
        
        # Generate configuration
        if [[ "${#KEY}" == "64" ]]; then
            echo -e "network={ \nssid=\"$ESSID\" \npsk=$KEY \n}">> $WPA_CONF
        elif ! echo "$KEY" | wpa_passphrase "$ESSID" >> $WPA_CONF; then
            err_append "Configuration generation failed. $(cat $WPA_CONF)"
            return 1
        fi

        # Connect!
        [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwext"
        start_wpa $INTERFACE $WPA_CONF $WPA_OPTS || return 1
        if ! wpa_check $INTERFACE $TIMEOUT; then
            stop_wpa $INTERFACE
            return 1
        fi
        ;;
    wpa-config)
        . ${SUBR_DIR}/8021x 
        [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf" # defaults
        [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwext"
        start_wpa $INTERFACE $WPA_CONF $WPA_OPTS || return 1
        if ! wpa_check $INTERFACE $TIMEOUT; then
            stop_wpa $INTERFACE
            return 1
        fi
        ;;        
    esac
    
    if ! ${CONN_DIR}/ethernet up $1; then
        wireless_down $1 YES
        return 1
    fi
}
 
wireless_down() {
    load_profile $1
    . ${SUBR_DIR}/8021x
    PROFILE=$1 NOETHERNETDOWN=$2
    if ! checkyesno $2; then
        ${CONN_DIR}/ethernet down $1    
    fi
    stop_wpa $INTERFACE 
	[[ "$SECURITY" == "wpa" ]] && rm -f "/tmp/wpa.${1// /}" # remove wpa config
    iwconfig $INTERFACE essid off key off &> /dev/null 
    ifconfig $INTERFACE down
    
    # If rfkill is specified, disable device.
    if [[ -n "$RFKILL_NAME" ]]; then
        path=$(rfkill_from_name $RFKILL_NAME)
        if [[ $? -ne 0 ]]; then
            err_append "no rfkill switch with the name $RFKILL_NAME";
        fi 
        echo 0 > ${path}/state
    fi
    
}

# Returns status of profile - is it still functional?
wireless_status() {
    load_profile $1
    if [[ "$(iwgetid -r)" -ne $ESSID ]]; then
        return 1
    elif ! ip link show dev ra0|grep -q "state UP"; then
        return 1
    fi

}

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