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