iPXE
iwmgmt.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 Joshua Oreman <oremanj@rwcr.net>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  */
00019 
00020 FILE_LICENCE ( GPL2_OR_LATER );
00021 
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <errno.h>
00025 #include <ipxe/net80211.h>
00026 #include <ipxe/ethernet.h>
00027 #include <usr/ifmgmt.h>
00028 #include <usr/iwmgmt.h>
00029 
00030 /** @file
00031  *
00032  * Wireless network interface management
00033  *
00034  */
00035 
00036 /**
00037  * Print status of 802.11 device
00038  *
00039  * @v dev       802.11 device
00040  */
00041 void iwstat ( struct net80211_device *dev ) {
00042 
00043         ifstat ( dev->netdev );
00044 
00045         printf ( "  [802.11 ");
00046         if ( dev->state & NET80211_ASSOCIATED ) {
00047                 printf ( "SSID '%s', ", dev->essid );
00048         } else {
00049                 printf ( "not associated, " );
00050         }
00051         if ( dev->channel < dev->nr_channels && dev->rate < dev->nr_rates ) {
00052                 printf ( "Ch:%d Sig:%d", dev->channels[dev->channel].channel_nr,
00053                          dev->last_signal );
00054                 switch ( dev->hw->signal_type ) {
00055                 case NET80211_SIGNAL_NONE:
00056                         printf ( "?" );
00057                         break;
00058                 case NET80211_SIGNAL_ARBITRARY:
00059                         printf ( "/%d", dev->hw->signal_max );
00060                         break;
00061                 case NET80211_SIGNAL_DB:
00062                         printf ( "/%d dB", dev->hw->signal_max );
00063                         break;
00064                 case NET80211_SIGNAL_DBM:
00065                         printf ( " dBm" );
00066                         break;
00067                 }
00068                 printf ( ", Qual:%d%% Rate:%d Mbps]\n",
00069                          ( dev->rx_beacon_interval == 0 ? 0 :
00070                            100 * dev->tx_beacon_interval /
00071                            dev->rx_beacon_interval ),
00072                          dev->rates[dev->rate] / 10 );
00073         } else {
00074                 printf ( "antenna off]\n" );
00075         }
00076 
00077         if ( dev->state & NET80211_WORKING ) {
00078                 printf ( "  [associating" );
00079                 if ( dev->associating )
00080                         printf ( " to '%s'", dev->associating->essid );
00081                 printf ( "...]\n" );
00082         }
00083 }
00084 
00085 /** Identifiers for 802.11 cryptography types, indexed by type number */
00086 static const char *crypto_types[] = {
00087         [NET80211_CRYPT_NONE] = "Open",
00088         [NET80211_CRYPT_WEP] = "WEP ",
00089         [NET80211_CRYPT_TKIP] = "WPA ",
00090         [NET80211_CRYPT_CCMP] = "WPA2",
00091         [NET80211_CRYPT_UNKNOWN] = "UNK ",
00092 };
00093 
00094 /** Number of 802.11 cryptography types defined */
00095 #define NR_CRYPTO_TYPES ( sizeof ( crypto_types ) / sizeof ( crypto_types[0] ) )
00096 
00097 /** Identifiers for 802.11 authentication types, indexed by type number */
00098 static const char *auth_types[] = {
00099         [NET80211_SECPROT_NONE] = "",
00100         [NET80211_SECPROT_PSK] = "PSK",
00101         [NET80211_SECPROT_EAP] = "802.1X",
00102         [NET80211_SECPROT_UNKNOWN] = "UNK",
00103 };
00104 
00105 /** Number of 802.11 authentication types defined */
00106 #define NR_AUTH_TYPES ( sizeof ( auth_types ) / sizeof ( auth_types[0] ) )
00107 
00108 /**
00109  * Scan for wireless networks using 802.11 device
00110  *
00111  * @v dev       802.11 device
00112  * @v active    Whether to use active scanning
00113  *
00114  * The list of networks found will be printed in tabular format.
00115  *
00116  * This function is safe to call at all times, whether the 802.11
00117  * device is open or not, but if called while the auto-association
00118  * task is running it will return an error indication.
00119  */
00120 int iwlist ( struct net80211_device *dev ) {
00121         struct net80211_probe_ctx *ctx;
00122         struct list_head *networks;
00123         struct net80211_wlan *wlan;
00124         char ssid_buf[22];
00125         int rc;
00126         unsigned i;
00127         int was_opened = netdev_is_open ( dev->netdev );
00128         int was_channel = dev->channels[dev->channel].channel_nr;
00129 
00130         if ( ! was_opened ) {
00131                 dev->state |= NET80211_NO_ASSOC;
00132                 rc = netdev_open ( dev->netdev );
00133                 if ( rc < 0 )
00134                         goto err;
00135         }
00136 
00137         if ( dev->state & NET80211_WORKING ) {
00138                 rc = -EINVAL;
00139                 goto err_close_netdev;
00140         }
00141 
00142         if ( ! was_opened ) {
00143                 rc = net80211_prepare_probe ( dev, dev->hw->bands, 0 );
00144                 if ( rc < 0 )
00145                         goto err_close_netdev;
00146         }
00147 
00148         ctx = net80211_probe_start ( dev, "", 0 );
00149         if ( ! ctx ) {
00150                 rc = -ENOMEM;
00151                 goto err_close_netdev;
00152         }
00153 
00154         while ( ! ( rc = net80211_probe_step ( ctx ) ) ) {
00155                 step();
00156         }
00157 
00158         networks = net80211_probe_finish_all ( ctx );
00159 
00160         if ( list_empty ( networks ) ) {
00161                 goto err_free_networks;
00162         }
00163 
00164         rc = 0;
00165 
00166         printf ( "Networks on %s:\n\n", dev->netdev->name );
00167 
00168         /* Output format:
00169          * 0         1         2         3         4         5         6
00170          * 0123456789012345678901234567890123456789012345678901234567890
00171          * [Sig] SSID                  BSSID              Ch  Crypt/Auth
00172          * -------------------------------------------------------------
00173          * [ 15] abcdefghijklmnopqrst> 00:00:00:00:00:00  11  Open
00174          *                                             ... or WPA   PSK etc.
00175          */
00176 
00177         /* Quoting the dashes and spaces verbatim uses less code space
00178            than generating them programmatically. */
00179         printf ( "[Sig] SSID                  BSSID              Ch  Crypt/Auth\n"
00180                  "-------------------------------------------------------------\n" );
00181 
00182         list_for_each_entry ( wlan, networks, list ) {
00183 
00184                 /* Format SSID into 22-character string, space-padded,
00185                    with '>' indicating truncation */
00186 
00187                 snprintf ( ssid_buf, sizeof ( ssid_buf ), "%s", wlan->essid );
00188                 for ( i = strlen ( ssid_buf ); i < sizeof ( ssid_buf ) - 1;
00189                       i++ )
00190                         ssid_buf[i] = ' ';
00191                 if ( ssid_buf[sizeof ( ssid_buf ) - 2] != ' ' )
00192                         ssid_buf[sizeof ( ssid_buf ) - 2] = '>';
00193                 ssid_buf[sizeof ( ssid_buf ) - 1] = 0;
00194 
00195                 /* Sanity check */
00196                 if ( wlan->crypto >= NR_CRYPTO_TYPES ||
00197                      wlan->handshaking >= NR_AUTH_TYPES )
00198                         continue;
00199 
00200                 printf ( "[%3d] %s %s  %2d  %s  %s\n",
00201                          wlan->signal < 0 ? 100 + wlan->signal : wlan->signal,
00202                          ssid_buf, eth_ntoa ( wlan->bssid ), wlan->channel,
00203                          crypto_types[wlan->crypto],
00204                          auth_types[wlan->handshaking] );
00205         }
00206         printf ( "\n" );
00207 
00208  err_free_networks:
00209         net80211_free_wlanlist ( networks );
00210 
00211  err_close_netdev:
00212         if ( ! was_opened ) {
00213                 dev->state &= ~NET80211_NO_ASSOC;
00214                 netdev_close ( dev->netdev );
00215         } else {
00216                 net80211_change_channel ( dev, was_channel );
00217         }
00218 
00219         if ( ! rc )
00220                 return 0;
00221 
00222  err:
00223         printf ( "Scanning for networks on %s: %s\n",
00224                  dev->netdev->name, strerror ( rc ) );
00225         return rc;
00226 }