iPXE
iwmgmt.c
Go to the documentation of this file.
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
20FILE_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 */
41void 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 */
86static 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 */
98static const char *auth_types[] = {
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 */
120int 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 ) {
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}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
Error codes.
const char * eth_ntoa(const void *ll_addr)
Transcribe Ethernet address.
Definition ethernet.c:176
Ethernet protocol.
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
int net80211_change_channel(struct net80211_device *dev, int channel)
Configure 802.11 device to transmit on a certain channel.
Definition net80211.c:2022
void net80211_free_wlanlist(struct list_head *list)
Free list of WLAN structures.
Definition net80211.c:1620
struct list_head * net80211_probe_finish_all(struct net80211_probe_ctx *ctx)
Finish probe of 802.11 networks, returning all networks found.
Definition net80211.c:1586
int net80211_prepare_probe(struct net80211_device *dev, int band, int active)
Prepare 802.11 device channel and rate set for scanning.
Definition net80211.c:2052
struct net80211_probe_ctx * net80211_probe_start(struct net80211_device *dev, const char *essid, int active)
Begin probe of 802.11 networks.
Definition net80211.c:1291
int net80211_probe_step(struct net80211_probe_ctx *ctx)
Continue probe of 802.11 networks.
Definition net80211.c:1364
#define NET80211_WORKING
Whether the auto-association task is running.
Definition net80211.h:212
#define NET80211_NO_ASSOC
Whether the auto-association task should be suppressed.
Definition net80211.h:223
#define NET80211_ASSOCIATED
Whether we have successfully associated with the network.
Definition net80211.h:201
#define EINVAL
Invalid argument.
Definition errno.h:429
#define ENOMEM
Not enough space.
Definition errno.h:535
void ifstat(struct net_device *netdev)
Print status of network device.
Definition ifmgmt.c:111
Network interface management.
String functions.
#define NR_AUTH_TYPES
Number of 802.11 authentication types defined.
Definition iwmgmt.c:106
int iwlist(struct net80211_device *dev)
Scan for wireless networks using 802.11 device.
Definition iwmgmt.c:120
static const char * crypto_types[]
Identifiers for 802.11 cryptography types, indexed by type number.
Definition iwmgmt.c:86
void iwstat(struct net80211_device *dev)
Print status of 802.11 device.
Definition iwmgmt.c:41
static const char * auth_types[]
Identifiers for 802.11 authentication types, indexed by type number.
Definition iwmgmt.c:98
#define NR_CRYPTO_TYPES
Number of 802.11 cryptography types defined.
Definition iwmgmt.c:95
Wireless network interface management.
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
#define list_empty(list)
Test whether a list is empty.
Definition list.h:137
The iPXE 802.11 MAC layer.
@ NET80211_SECPROT_UNKNOWN
Dummy value used when the handshaking type can't be detected.
Definition net80211.h:124
@ NET80211_SECPROT_NONE
No security handshaking.
Definition net80211.h:102
@ NET80211_SECPROT_PSK
Pre-shared key handshaking.
Definition net80211.h:112
@ NET80211_SECPROT_EAP
Full EAP 802.1X handshaking.
Definition net80211.h:121
@ NET80211_CRYPT_NONE
No security, an "Open" network.
Definition net80211.h:131
@ NET80211_CRYPT_CCMP
Network protected with CCMP (AES-based system)
Definition net80211.h:174
@ NET80211_CRYPT_TKIP
Network protected with TKIP (better RC4-based system)
Definition net80211.h:163
@ NET80211_CRYPT_UNKNOWN
Dummy value used when the cryptosystem can't be detected.
Definition net80211.h:177
@ NET80211_CRYPT_WEP
Network protected with WEP (awful RC4-based system)
Definition net80211.h:145
int netdev_open(struct net_device *netdev)
Open network device.
Definition netdevice.c:862
void netdev_close(struct net_device *netdev)
Close network device.
Definition netdevice.c:896
static int netdev_is_open(struct net_device *netdev)
Check whether or not network device is open.
Definition netdevice.h:662
void step(void)
Single-step a single process.
Definition process.c:99
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
A doubly-linked list entry (or list head)
Definition list.h:19
u8 channel_nr
A channel number interpreted according to the band.
Definition net80211.h:405
Structure encapsulating the complete state of an 802.11 device.
Definition net80211.h:787
struct net_device * netdev
The net_device that wraps us.
Definition net80211.h:789
u16 rates[NET80211_MAX_RATES]
A list of all possible TX rates we might use.
Definition net80211.h:818
struct net80211_channel channels[NET80211_MAX_CHANNELS]
A list of all possible channels we might use.
Definition net80211.h:806
char essid[IEEE80211_MAX_SSID_LEN+1]
SSID of the access point we are or will be associated with.
Definition net80211.h:962
u32 tx_beacon_interval
Time between AP sending beacons, microseconds.
Definition net80211.h:971
u16 state
State of our association to the network.
Definition net80211.h:921
int last_signal
Signal strength of last received packet.
Definition net80211.h:986
u8 channel
The channel currently in use, as an index into the channels array.
Definition net80211.h:812
u32 rx_beacon_interval
Smoothed average time between beacons, microseconds.
Definition net80211.h:974
struct net80211_wlan * associating
Network with which we are associating.
Definition net80211.h:866
u8 nr_channels
The number of channels in the channels array.
Definition net80211.h:809
u8 rate
The rate currently in use, as an index into the rates array.
Definition net80211.h:824
struct net80211_hw_info * hw
Information about the hardware, provided to net80211_register()
Definition net80211.h:801
u8 nr_rates
The number of transmission rates in the rates array.
Definition net80211.h:821
int bands
A bitwise OR of the bands on which this device can communicate.
Definition net80211.h:453
unsigned signal_max
Maximum signal in arbitrary cases.
Definition net80211.h:495
enum net80211_hw_info::@160150106134320172247032061203111226300065160315 signal_type
Signal strength information that can be provided by the device.
Context for a probe operation.
Definition net80211.c:64
Structure representing a probed network.
Definition net80211.h:1057
struct list_head list
Link to allow chaining multiple structures into a list to be returned from net80211_probe_finish_all(...
Definition net80211.h:1091
u8 bssid[ETH_ALEN]
MAC address of the strongest-signal access point for this ESSID.
Definition net80211.h:1067
char essid[IEEE80211_MAX_SSID_LEN+1]
The human-readable ESSID (network name)
Definition net80211.h:1064
int channel
The channel on which that access point communicates.
Definition net80211.h:1078
enum net80211_crypto_alg crypto
Cryptographic algorithm used on the network.
Definition net80211.h:1087
int signal
Signal strength of beacon frame from that access point.
Definition net80211.h:1070
enum net80211_security_proto handshaking
Security handshaking method used on the network.
Definition net80211.h:1084
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition netdevice.h:363
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:465
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383