You are not logged in.

#1 2009-01-30 16:08:37

ankan
Member
Registered: 2008-10-11
Posts: 2

netcfg: Different approach to wpa_supplicant configuration

I got bored of some functionality that was missing in netcfg for the configuration of wpa_supplicant, so decided to add it. It takes the configuration from a string in the netcfg config, not from a separate file or in a very limited format.

It actually allows you to also add a config file in the normal way. The contents of the file will be placed before what is put in the netcfg profile.

I also made some small tweaks, like removing the configuration for wpa_supplicant after it has been used. In the original code it is left lying around in /tmp.

A sample profile:

DESCRIPTION="Wireless connection to someplace"

IP="dhcp"
DHCP_TIMEOUT=20
DHCP_OPTIONS=""

CONNECTION="wireless-custom"
INTERFACE=wlan0

WPAOPTS="-D wext"
TIMEOUT=15

SECURITY="wpa-custom"
WPA_CUSTOM='
  ctrl_interface=/var/run/wpa_supplicant
  ctrl_interface_group=wheel

  ap_scan=1
  update_config=0

  network={
    ssid="myssid"
    bssid=00:00:00:00:00:00
    pairwise=CCMP
    group=TKIP
    key_mgmt=WPA-PSK
    auth_alg=OPEN
    psk="mypsk"
    priority=700
  }
'

Here is the code, place it as "/usr/lib/network/wireless-custom.subr". It could easily be incorporated in wireless.subr with minor modifications since it is based on it. But I put it as a separate file so I could keep the old one side by side for now.

#! /bin/bash

# Uses wpa_supplicant to check for association to a network
# wpa_check interface [timeout]
wpa_check()
{
    INTERFACE=$1; TIMEOUT=$2

    [[ -z "$TIMEOUT" ]] && TIMEOUT=15
    let timeout=0
    while [[ $timeout -ne $TIMEOUT ]]; do
        eval `wpa_cli status|grep wpa_state` 
        [[ "$wpa_state" = "COMPLETED" ]] && return 0
        sleep 1
        let timeout++
    done

    wpa_cli terminate >/dev/null 2>&1
    err_append "Wireless association failed."
    return 1 
}

# Uses wireless_tools, to check for association to a network.
# wep_check interface [timeout]
wep_check()
{
    INTERFACE=$1; TIMEOUT=$2
 
    [[ -z "$TIMEOUT" ]] && TIMEOUT=15
    let timeout=0
    while [[ $timeout -ne $TIMEOUT ]]; do
        bssid=`iwgetid $INTERFACE -ra`
        [[ ! "$bssid" = "00:00:00:00:00:00" ]] && return 0
        sleep 1
        let timeout++
    done
    
    err_append "Wireless association failed."
    return 1
}

# Check if a particular network is within range
# find_essid interface essid
find_essid()
{
    INTERFACE=$1; ESSID=$2; RETRIES=5
    try=0;
    while [[ $try -ne $RETRIES ]]; do
        if iwlist $INTERFACE scan|sed "s/ESSID://g"|grep -q "\"$ESSID\""; then
            return 0 # network found
        fi
        sleep 1
        let try++
    done
    return 1
}

# Return a filename containing a list of network ESSID's found.
# list_networks interface
list_networks()
{
    # temp file used, as keeping ESSID's with spaces in their name in arrays 
    # is hard, obscure and kinda nasty. This is simpler and clearer.
 
    [[ -z "$1" ]] && return 1
    essids=$(mktemp /tmp/essid.XXXXX)

    let try=0;
    RETRIES=6;
    while [[ $try -ne $RETRIES ]]; do        
        iwlist $1 scan 2> /dev/null|grep ESSID|sed 's/.*ESSID:"\([^"]\+\)".*/\1/' > $essids
        sleep 0.5; let try++
    done
    sort -u $essids -o $essids
  
    # File of 0 length, ie. no ssid's.
    if [[ ! -s $essids ]]; then
        return 1
    fi

    echo $essids
    return 0
}

start_wpa() 
{
    INTERFACE=$1; WPA_CONF=$2; WPA_OPTS=$3
     
    [[ "$WPA_OPTS" == "" ]] && WPA_OPTS="-Dwext"

    wpa_supplicant -wB -P/var/run/wpa_supplicant_${INTERFACE}.pid -i${INTERFACE} -c $WPA_CONF $WPA_OPTS    
    sleep 1
    
    if [[ ! -f /var/run/wpa_supplicant_${INTERFACE}.pid ]]; then
        err_append "wpa_supplicant did not start, possible configuration error"
        return 1
    fi
}

wireless-custom_up() {

    load_profile $1
    
    if [[ ! -d /sys/class/net/$INTERFACE/wireless ]]; then 
        err_append "Interface $INTERFACE is not a 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
    quirk "test" && echo "TESTWORKS!" 
    # 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 unavailable"
            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 open $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

        wep_check $INTERFACE $TIMEOUT|| return 1
        ;;    
    wpa)
        local WPA_TMPCONF=`mktemp /tmp/wpa.XXXXXXXX`
        
        # Quirk for broken drivers... http://bbs.archlinux.org/viewtopic.php?id=36384
        quirk "wpaessid" && eval iwconfig $INTERFACE mode managed essid "\"$ESSID\""
        
        # Create a random file to store configuration, make it root only. 
        chmod 600 $WPA_TMPCONF
        echo "ctrl_interface=/var/run/wpa_supplicant" >> $WPA_TMPCONF
        echo "ctrl_interface_group=0" >> $WPA_TMPCONF        
        
        # Generate configuration
        if [[ "${#KEY}" == "64" ]]; then
            echo -e 'network={ \nssid="$ESSID" \npsk=$KEY \n}'> $WPA_TMPCONF
        elif ! wpa_passphrase "$ESSID" "$KEY" >> $WPA_TMPCONF; then
            err_append "Configuration generation failed: `cat $WPA_TMPCONF`"
            return 1
        fi

        # Connect!
        if ! start_wpa $INTERFACE $WPA_TMPCONF $WPA_OPTS ; then
          rm $WPA_TMPCONF
          return 1
        fi
        rm $WPA_TMPCONF

        wpa_check $INTERFACE $TIMEOUT || return 1
        ;;
    wpa-config)
        # If user hasnt defined one, use stock config.
        [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf"
        start_wpa $INTERFACE $WPA_CONF $WPA_OPTS || return 1
        wpa_check $INTERFACE $TIMEOUT || return 1
        ;;
    wpa-custom)
        #Create temporary config file
        local WPA_TMPCONF=`mktemp /tmp/wpa.XXXXXXXX`
        
        # Quirk for broken drivers... http://bbs.archlinux.org/viewtopic.php?id=36384
        quirk "wpaessid" && eval iwconfig $INTERFACE mode managed essid "\"$ESSID\""
        
        # Create a random file to store configuration, make it root only. 
        chmod 600 $WPA_TMPCONF
        # Copy base config file
        [[ -z "$WPA_CONF" ]] || cat $WPA_CONF >> $WPA_TMPCONF

        # Add custom settings
        echo "$WPA_CUSTOM" >> $WPA_TMPCONF
        
        # Connect!
        if ! start_wpa $INTERFACE $WPA_TMPCONF $WPA_OPTS ; then
          rm $WPA_TMPCONF
          return 1
        fi
        rm $WPA_TMPCONF

        wpa_check $INTERFACE $TIMEOUT || return 1
        ;;
    esac
    
    . $SUBR_DIR/ethernet.subr
    if ! ethernet_up $1; then
        wireless-custom_down $1 YES
        return 1
    fi
}
 
wireless-custom_down() {
    PROFILE=$1 NOETHERNETDOWN=$2
    if ! checkyesno $2; then
        . $SUBR_DIR/ethernet.subr
        ethernet_down $1
       fi
    wpa_cli terminate &> /dev/null 
    iwconfig $INTERFACE essid off key off &> /dev/null 
    #ifconfig $INTERFACE down
}

wireless-custom_clean_scope() {
    unset INTERFACE CONNECTION
    unset TIMEOUT WPA_CONF WPA_OPTS KEY ESSID SECURITY WEP_OPTS WPA_TMPCONF WPA_CUSTOM
    . $SUBR_DIR/ethernet.subr
    ethernet_clean_scope
}


# vim: set ts=4 et sw=4:

Enjoy. Any criticism welcome.

Offline

Board footer

Powered by FluxBB