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

wireless_up() {

    load_profile $1
    
    . ${SUBR_DIR}/wireless

    # Kill any lingering wpa_supplicants.
    if [[ -f /var/run/wpa_supplicant_$INTERFACE.pid ]]; then
        kill $(cat /var/run/wpa_supplicant_$INTERFACE.pid)
    fi

    # 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 

    # Some hardware is not immediately ready after ifconfig up
    while ip link show $INTERFACE|grep -q "state UNKNOWN"; do
        sleep 0.5
    done

    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

    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)
        . ${SUBR_DIR}/8021x

        # 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=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
        wpa_check $INTERFACE $TIMEOUT || return 1
        ;;
    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
        wpa_check $INTERFACE $TIMEOUT || return 1
        ;;        
    esac
    
    if ! ${CONN_DIR}/ethernet up $1; then
        wireless_down $1 YES
        return 1
    fi
}
 
wireless_down() {
    load_profile $1
    PROFILE=$1 NOETHERNETDOWN=$2
    if ! checkyesno $2; then
        ${CONN_DIR}/ethernet down $1    
    fi
    wpa_cli terminate &> /dev/null 
	[[ "$SECURITY" == "wpa" ]] && rm -f "/tmp/wpa.${1// /}" # remove wpa config
    iwconfig $INTERFACE essid off key off &> /dev/null 
    ifconfig $INTERFACE down
}

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