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

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