選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

iwmgmt.c 6.8KB

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