You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iwmgmt.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) 2009 Joshua Oreman <oremanj@rwcr.net>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <ipxe/net80211.h>
  23. #include <ipxe/ethernet.h>
  24. #include <usr/ifmgmt.h>
  25. #include <usr/iwmgmt.h>
  26. /** @file
  27. *
  28. * Wireless network interface management
  29. *
  30. */
  31. /**
  32. * Print status of 802.11 device
  33. *
  34. * @v dev 802.11 device
  35. */
  36. void iwstat ( struct net80211_device *dev ) {
  37. ifstat ( dev->netdev );
  38. printf ( " [802.11 ");
  39. if ( dev->state & NET80211_ASSOCIATED ) {
  40. printf ( "SSID '%s', ", dev->essid );
  41. } else {
  42. printf ( "not associated, " );
  43. }
  44. if ( dev->channel < dev->nr_channels && dev->rate < dev->nr_rates ) {
  45. printf ( "Ch:%d Sig:%d", dev->channels[dev->channel].channel_nr,
  46. dev->last_signal );
  47. switch ( dev->hw->signal_type ) {
  48. case NET80211_SIGNAL_NONE:
  49. printf ( "?" );
  50. break;
  51. case NET80211_SIGNAL_ARBITRARY:
  52. printf ( "/%d", dev->hw->signal_max );
  53. break;
  54. case NET80211_SIGNAL_DB:
  55. printf ( "/%d dB", dev->hw->signal_max );
  56. break;
  57. case NET80211_SIGNAL_DBM:
  58. printf ( " dBm" );
  59. break;
  60. }
  61. printf ( ", Qual:%d%% Rate:%d Mbps]\n",
  62. ( dev->rx_beacon_interval == 0 ? 0 :
  63. 100 * dev->tx_beacon_interval /
  64. dev->rx_beacon_interval ),
  65. dev->rates[dev->rate] / 10 );
  66. } else {
  67. printf ( "antenna off]\n" );
  68. }
  69. if ( dev->state & NET80211_WORKING ) {
  70. printf ( " [associating" );
  71. if ( dev->associating )
  72. printf ( " to '%s'", dev->associating->essid );
  73. printf ( "...]\n" );
  74. }
  75. }
  76. /** Identifiers for 802.11 cryptography types, indexed by type number */
  77. static const char *crypto_types[] = {
  78. [NET80211_CRYPT_NONE] = "Open",
  79. [NET80211_CRYPT_WEP] = "WEP ",
  80. [NET80211_CRYPT_TKIP] = "WPA ",
  81. [NET80211_CRYPT_CCMP] = "WPA2",
  82. [NET80211_CRYPT_UNKNOWN] = "UNK ",
  83. };
  84. /** Number of 802.11 cryptography types defined */
  85. #define NR_CRYPTO_TYPES ( sizeof ( crypto_types ) / sizeof ( crypto_types[0] ) )
  86. /** Identifiers for 802.11 authentication types, indexed by type number */
  87. static const char *auth_types[] = {
  88. [NET80211_SECPROT_NONE] = "",
  89. [NET80211_SECPROT_PSK] = "PSK",
  90. [NET80211_SECPROT_EAP] = "802.1X",
  91. [NET80211_SECPROT_UNKNOWN] = "UNK",
  92. };
  93. /** Number of 802.11 authentication types defined */
  94. #define NR_AUTH_TYPES ( sizeof ( auth_types ) / sizeof ( auth_types[0] ) )
  95. /**
  96. * Scan for wireless networks using 802.11 device
  97. *
  98. * @v dev 802.11 device
  99. * @v active Whether to use active scanning
  100. *
  101. * The list of networks found will be printed in tabular format.
  102. *
  103. * This function is safe to call at all times, whether the 802.11
  104. * device is open or not, but if called while the auto-association
  105. * task is running it will return an error indication.
  106. */
  107. int iwlist ( struct net80211_device *dev ) {
  108. struct net80211_probe_ctx *ctx;
  109. struct list_head *networks;
  110. struct net80211_wlan *wlan;
  111. char ssid_buf[22];
  112. int rc;
  113. unsigned i;
  114. int was_opened = netdev_is_open ( dev->netdev );
  115. int was_channel = dev->channels[dev->channel].channel_nr;
  116. if ( ! was_opened ) {
  117. dev->state |= NET80211_NO_ASSOC;
  118. rc = netdev_open ( dev->netdev );
  119. if ( rc < 0 )
  120. goto err;
  121. }
  122. if ( dev->state & NET80211_WORKING ) {
  123. rc = -EINVAL;
  124. goto err_close_netdev;
  125. }
  126. if ( ! was_opened ) {
  127. rc = net80211_prepare_probe ( dev, dev->hw->bands, 0 );
  128. if ( rc < 0 )
  129. goto err_close_netdev;
  130. }
  131. ctx = net80211_probe_start ( dev, "", 0 );
  132. if ( ! ctx ) {
  133. rc = -ENOMEM;
  134. goto err_close_netdev;
  135. }
  136. while ( ! ( rc = net80211_probe_step ( ctx ) ) ) {
  137. step();
  138. }
  139. networks = net80211_probe_finish_all ( ctx );
  140. if ( list_empty ( networks ) ) {
  141. goto err_free_networks;
  142. }
  143. rc = 0;
  144. printf ( "Networks on %s:\n\n", dev->netdev->name );
  145. /* Output format:
  146. * 0 1 2 3 4 5 6
  147. * 0123456789012345678901234567890123456789012345678901234567890
  148. * [Sig] SSID BSSID Ch Crypt/Auth
  149. * -------------------------------------------------------------
  150. * [ 15] abcdefghijklmnopqrst> 00:00:00:00:00:00 11 Open
  151. * ... or WPA PSK etc.
  152. */
  153. /* Quoting the dashes and spaces verbatim uses less code space
  154. than generating them programmatically. */
  155. printf ( "[Sig] SSID BSSID Ch Crypt/Auth\n"
  156. "-------------------------------------------------------------\n" );
  157. list_for_each_entry ( wlan, networks, list ) {
  158. /* Format SSID into 22-character string, space-padded,
  159. with '>' indicating truncation */
  160. snprintf ( ssid_buf, sizeof ( ssid_buf ), "%s", wlan->essid );
  161. for ( i = strlen ( ssid_buf ); i < sizeof ( ssid_buf ) - 1;
  162. i++ )
  163. ssid_buf[i] = ' ';
  164. if ( ssid_buf[sizeof ( ssid_buf ) - 2] != ' ' )
  165. ssid_buf[sizeof ( ssid_buf ) - 2] = '>';
  166. ssid_buf[sizeof ( ssid_buf ) - 1] = 0;
  167. /* Sanity check */
  168. if ( wlan->crypto >= NR_CRYPTO_TYPES ||
  169. wlan->handshaking >= NR_AUTH_TYPES )
  170. continue;
  171. printf ( "[%3d] %s %s %2d %s %s\n",
  172. wlan->signal < 0 ? 100 + wlan->signal : wlan->signal,
  173. ssid_buf, eth_ntoa ( wlan->bssid ), wlan->channel,
  174. crypto_types[wlan->crypto],
  175. auth_types[wlan->handshaking] );
  176. }
  177. printf ( "\n" );
  178. err_free_networks:
  179. net80211_free_wlanlist ( networks );
  180. err_close_netdev:
  181. if ( ! was_opened ) {
  182. dev->state &= ~NET80211_NO_ASSOC;
  183. netdev_close ( dev->netdev );
  184. } else {
  185. net80211_change_channel ( dev, was_channel );
  186. }
  187. if ( ! rc )
  188. return 0;
  189. err:
  190. printf ( "Scanning for networks on %s: %s\n",
  191. dev->netdev->name, strerror ( rc ) );
  192. return rc;
  193. }