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 
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 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
Network protected with CCMP (AES-based system)
Definition: net80211.h:174
Structure representing a probed network.
Definition: net80211.h:1056
enum net80211_crypto_alg crypto
Cryptographic algorithm used on the network.
Definition: net80211.h:1087
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
No security handshaking.
Definition: net80211.h:102
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:1290
u8 channel
The channel currently in use, as an index into the channels array.
Definition: net80211.h:812
Error codes.
Dummy value used when the cryptosystem can't be detected.
Definition: net80211.h:177
int iwlist(struct net80211_device *dev)
Scan for wireless networks using 802.11 device.
Definition: iwmgmt.c:120
char essid[IEEE80211_MAX_SSID_LEN+1]
The human-readable ESSID (network name)
Definition: net80211.h:1064
enum net80211_security_proto handshaking
Security handshaking method used on the network.
Definition: net80211.h:1084
Full EAP 802.1X handshaking.
Definition: net80211.h:121
unsigned signal_max
Maximum signal in arbitrary cases.
Definition: net80211.h:495
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
Network protected with WEP (awful RC4-based system)
Definition: net80211.h:145
Dummy value used when the handshaking type can't be detected.
Definition: net80211.h:124
Context for a probe operation.
Definition: net80211.c:63
char essid[IEEE80211_MAX_SSID_LEN+1]
SSID of the access point we are or will be associated with.
Definition: net80211.h:962
#define NET80211_WORKING
Whether the auto-association task is running.
Definition: net80211.h:212
A doubly-linked list entry (or list head)
Definition: list.h:18
static const char * auth_types[]
Identifiers for 802.11 authentication types, indexed by type number.
Definition: iwmgmt.c:98
u32 tx_beacon_interval
Time between AP sending beacons, microseconds.
Definition: net80211.h:971
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
#define ENOMEM
Not enough space.
Definition: errno.h:534
static int netdev_is_open(struct net_device *netdev)
Check whether or not network device is open.
Definition: netdevice.h:652
Pre-shared key handshaking.
Definition: net80211.h:112
Ethernet protocol.
u8 nr_channels
The number of channels in the channels array.
Definition: net80211.h:809
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
int last_signal
Signal strength of last received packet.
Definition: net80211.h:986
Wireless network interface management.
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:1585
u8 nr_rates
The number of transmission rates in the rates array.
Definition: net80211.h:821
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
The iPXE 802.11 MAC layer.
const char * eth_ntoa(const void *ll_addr)
Transcribe Ethernet address.
Definition: ethernet.c:175
#define NET80211_ASSOCIATED
Whether we have successfully associated with the network.
Definition: net80211.h:201
Structure encapsulating the complete state of an 802.11 device.
Definition: net80211.h:786
static const char * crypto_types[]
Identifiers for 802.11 cryptography types, indexed by type number.
Definition: iwmgmt.c:86
No security, an "Open" network.
Definition: net80211.h:131
int net80211_probe_step(struct net80211_probe_ctx *ctx)
Continue probe of 802.11 networks.
Definition: net80211.c:1363
void net80211_free_wlanlist(struct list_head *list)
Free list of WLAN structures.
Definition: net80211.c:1619
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
#define NET80211_NO_ASSOC
Whether the auto-association task should be suppressed.
Definition: net80211.h:223
struct net80211_hw_info * hw
Information about the hardware, provided to net80211_register()
Definition: net80211.h:801
#define NR_CRYPTO_TYPES
Number of 802.11 cryptography types defined.
Definition: iwmgmt.c:95
u16 rates[NET80211_MAX_RATES]
A list of all possible TX rates we might use.
Definition: net80211.h:818
struct net80211_wlan * associating
Network with which we are associating.
Definition: net80211.h:866
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
void netdev_close(struct net_device *netdev)
Close network device.
Definition: netdevice.c:801
enum net80211_hw_info::@548 signal_type
Signal strength information that can be provided by the device.
struct net_device * netdev
The net_device that wraps us.
Definition: net80211.h:789
FILE_LICENCE(GPL2_OR_LATER)
int channel
The channel on which that access point communicates.
Definition: net80211.h:1078
void iwstat(struct net80211_device *dev)
Print status of 802.11 device.
Definition: iwmgmt.c:41
#define NR_AUTH_TYPES
Number of 802.11 authentication types defined.
Definition: iwmgmt.c:106
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:358
int signal
Signal strength of beacon frame from that access point.
Definition: net80211.h:1070
void step(void)
Single-step a single process.
Definition: process.c:98
struct net80211_channel channels[NET80211_MAX_CHANNELS]
A list of all possible channels we might use.
Definition: net80211.h:806
u8 bssid[ETH_ALEN]
MAC address of the strongest-signal access point for this ESSID.
Definition: net80211.h:1067
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
Network interface management.
int bands
A bitwise OR of the bands on which this device can communicate.
Definition: net80211.h:453
void ifstat(struct net_device *netdev)
Print status of network device.
Definition: ifmgmt.c:110
Network protected with TKIP (better RC4-based system)
Definition: net80211.h:163
u16 state
State of our association to the network.
Definition: net80211.h:921
u8 channel_nr
A channel number interpreted according to the band.
Definition: net80211.h:405
u32 rx_beacon_interval
Smoothed average time between beacons, microseconds.
Definition: net80211.h:974
int net80211_change_channel(struct net80211_device *dev, int channel)
Configure 802.11 device to transmit on a certain channel.
Definition: net80211.c:2021
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:2051
String functions.
int netdev_open(struct net_device *netdev)
Open network device.
Definition: netdevice.c:767
u8 rate
The rate currently in use, as an index into the rates array.
Definition: net80211.h:824