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

wireless_up() {

    load_profile $1
    
    . ${SUBR_DIR}/wireless

    if [[ ! -d /sys/class/net/$INTERFACE/wireless ]]; then 
        err_append "$INTERFACE is not a valid wireless interface."
        return 1
    fi
 
    # Required by atheros and others (mac80211?) to enable device
    ifconfig $INTERFACE up 

    # Hack that has been required by some broadcom
    quirk "prescan" && iwlist $INTERFACE scan &> /dev/null

    # Required by ipw3945 to properly re-associate
    quirk "preessid" && eval "iwconfig $INTERFACE mode managed essid \"$ESSID\""

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

    # Default scan off as it won't see hidden networks and some hardware's scanning is dodgy
    [[ ! "$SCAN" ]] && SCAN="no" 
    if checkyesno $SCAN; then
        if ! find_essid $INTERFACE "$ESSID"; then
            err_append "Network not present."
            return 1
        fi 
    fi
  
    # Manually set iwconfig options
    if [[ "$IWCONFIG" ]]; then
        iwconfig $INTERFACE $IWCONFIG
    fi

    case $SECURITY in
    wep|none)
        # 'none' security uses iwconfig, like wep, so use same code, minus keysetting.
        # Use sane default if no alternative is specified
        if [[ "$SECURITY" = "wep" && "$WEP_OPTS" = "" ]]; then 
            WEP_OPTS="mode managed essid \"$ESSID\" key $KEY"
        elif [[ "$SECURITY" = "none" && "$WEP_OPTS" = "" ]]; then
            WEP_OPTS="mode managed essid \"$ESSID\""
        fi 
        
        # Add wierd quirk for some Atheros in response to FS#10585
        quirk "predown" && ifconfig $INTERFACE down

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

        quirk "postsleep" && sleep 1
        quirk "postscan" && sleep 1 && iwlist $INTERFACE scan &>/dev/null
        quirk "predown" && ifconfig $INTERFACE up
        
        # Some devices appear to not associate until DHCP is run.
        if quirk "noacheck"; then
            sleep ${WIRELESS_TIMEOUT:-20}
        else
            wep_check $INTERFACE $TIMEOUT|| return 1
        fi
        ;;    
    wpa)
        . ${SUBR_DIR}/8021x

        # Quirk for broken drivers... http://bbs.archlinux.org/viewtopic.php?id=36384
        quirk "wpaessid" && eval iwconfig $INTERFACE mode managed 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!
        [[ "$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
        # If user hasnt defined one, use stock config.
        [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf"
        [[ "$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 
}

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