~ubuntu-branches/ubuntu/trusty/virtualbox-lts-xenial/trusty-proposed

« back to all changes in this revision

Viewing changes to src/VBox/Devices/PC/ipxe/src/usr/iwmgmt.c

  • Committer: Package Import Robot
  • Author(s): Gianfranco Costamagna
  • Date: 2016-02-23 14:28:26 UTC
  • Revision ID: package-import@ubuntu.com-20160223142826-bdu69el2z6wa2a44
Tags: upstream-4.3.36-dfsg
ImportĀ upstreamĀ versionĀ 4.3.36-dfsg

Show diffs side-by-side

added added

removed removed

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