1 | /* |
---|
2 | * The gPXE 802.11 MAC layer. |
---|
3 | * |
---|
4 | * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>. |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the GNU General Public License as |
---|
8 | * published by the Free Software Foundation; either version 2 of the |
---|
9 | * License, or any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, but |
---|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | * General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
19 | */ |
---|
20 | |
---|
21 | FILE_LICENCE ( GPL2_OR_LATER ); |
---|
22 | |
---|
23 | #include <string.h> |
---|
24 | #include <byteswap.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <gpxe/settings.h> |
---|
27 | #include <gpxe/if_arp.h> |
---|
28 | #include <gpxe/ethernet.h> |
---|
29 | #include <gpxe/ieee80211.h> |
---|
30 | #include <gpxe/netdevice.h> |
---|
31 | #include <gpxe/net80211.h> |
---|
32 | #include <gpxe/sec80211.h> |
---|
33 | #include <gpxe/timer.h> |
---|
34 | #include <gpxe/nap.h> |
---|
35 | #include <unistd.h> |
---|
36 | #include <errno.h> |
---|
37 | |
---|
38 | /** @file |
---|
39 | * |
---|
40 | * 802.11 device management |
---|
41 | */ |
---|
42 | |
---|
43 | /* Disambiguate the EINVAL's a bit */ |
---|
44 | #define EINVAL_PKT_TOO_SHORT ( EINVAL | EUNIQ_01 ) |
---|
45 | #define EINVAL_PKT_VERSION ( EINVAL | EUNIQ_02 ) |
---|
46 | #define EINVAL_PKT_NOT_DATA ( EINVAL | EUNIQ_03 ) |
---|
47 | #define EINVAL_PKT_NOT_FROMDS ( EINVAL | EUNIQ_04 ) |
---|
48 | #define EINVAL_PKT_LLC_HEADER ( EINVAL | EUNIQ_05 ) |
---|
49 | #define EINVAL_CRYPTO_REQUEST ( EINVAL | EUNIQ_06 ) |
---|
50 | #define EINVAL_ACTIVE_SCAN ( EINVAL | EUNIQ_07 ) |
---|
51 | |
---|
52 | /* |
---|
53 | * 802.11 error codes: The AP can give us a status code explaining why |
---|
54 | * authentication failed, or a reason code explaining why we were |
---|
55 | * deauthenticated/disassociated. These codes range from 0-63 (the |
---|
56 | * field is 16 bits wide, but only up to 45 or so are defined yet; we |
---|
57 | * allow up to 63 for extensibility). This is encoded into an error |
---|
58 | * code as such: |
---|
59 | * |
---|
60 | * status & 0x1f goes here --vv-- |
---|
61 | * Status code 0-31: ECONNREFUSED | EUNIQ_(status & 0x1f) (0e1a6038) |
---|
62 | * Status code 32-63: EHOSTUNREACH | EUNIQ_(status & 0x1f) (171a6011) |
---|
63 | * Reason code 0-31: ECONNRESET | EUNIQ_(reason & 0x1f) (0f1a6039) |
---|
64 | * Reason code 32-63: ENETRESET | EUNIQ_(reason & 0x1f) (271a6001) |
---|
65 | * |
---|
66 | * The POSIX error codes more or less convey the appropriate message |
---|
67 | * (status codes occur when we can't associate at all, reason codes |
---|
68 | * when we lose association unexpectedly) and let us extract the |
---|
69 | * complete 802.11 error code from the rc value. |
---|
70 | */ |
---|
71 | |
---|
72 | /** Make return status code from 802.11 status code */ |
---|
73 | #define E80211_STATUS( stat ) ( ((stat & 0x20)? EHOSTUNREACH : ECONNREFUSED) \ |
---|
74 | | ((stat & 0x1f) << 8) ) |
---|
75 | |
---|
76 | /** Make return status code from 802.11 reason code */ |
---|
77 | #define E80211_REASON( reas ) ( ((reas & 0x20)? ENETRESET : ECONNRESET) \ |
---|
78 | | ((reas & 0x1f) << 8) ) |
---|
79 | |
---|
80 | |
---|
81 | /** List of 802.11 devices */ |
---|
82 | static struct list_head net80211_devices = LIST_HEAD_INIT ( net80211_devices ); |
---|
83 | |
---|
84 | /** Set of device operations that does nothing */ |
---|
85 | static struct net80211_device_operations net80211_null_ops; |
---|
86 | |
---|
87 | /** Information associated with a received management packet |
---|
88 | * |
---|
89 | * This is used to keep beacon signal strengths in a parallel queue to |
---|
90 | * the beacons themselves. |
---|
91 | */ |
---|
92 | struct net80211_rx_info { |
---|
93 | int signal; |
---|
94 | struct list_head list; |
---|
95 | }; |
---|
96 | |
---|
97 | /** Context for a probe operation */ |
---|
98 | struct net80211_probe_ctx { |
---|
99 | /** 802.11 device to probe on */ |
---|
100 | struct net80211_device *dev; |
---|
101 | |
---|
102 | /** Value of keep_mgmt before probe was started */ |
---|
103 | int old_keep_mgmt; |
---|
104 | |
---|
105 | /** If scanning actively, pointer to probe packet to send */ |
---|
106 | struct io_buffer *probe; |
---|
107 | |
---|
108 | /** If non-"", the ESSID to limit ourselves to */ |
---|
109 | const char *essid; |
---|
110 | |
---|
111 | /** Time probe was started */ |
---|
112 | u32 ticks_start; |
---|
113 | |
---|
114 | /** Time last useful beacon was received */ |
---|
115 | u32 ticks_beacon; |
---|
116 | |
---|
117 | /** Time channel was last changed */ |
---|
118 | u32 ticks_channel; |
---|
119 | |
---|
120 | /** Time to stay on each channel */ |
---|
121 | u32 hop_time; |
---|
122 | |
---|
123 | /** Channels to hop by when changing channel */ |
---|
124 | int hop_step; |
---|
125 | |
---|
126 | /** List of best beacons for each network found so far */ |
---|
127 | struct list_head *beacons; |
---|
128 | }; |
---|
129 | |
---|
130 | /** Context for the association task */ |
---|
131 | struct net80211_assoc_ctx { |
---|
132 | /** Next authentication method to try using */ |
---|
133 | int method; |
---|
134 | |
---|
135 | /** Time (in ticks) of the last sent association-related packet */ |
---|
136 | int last_packet; |
---|
137 | |
---|
138 | /** Number of times we have tried sending it */ |
---|
139 | int times_tried; |
---|
140 | }; |
---|
141 | |
---|
142 | /** |
---|
143 | * @defgroup net80211_netdev Network device interface functions |
---|
144 | * @{ |
---|
145 | */ |
---|
146 | static int net80211_netdev_open ( struct net_device *netdev ); |
---|
147 | static void net80211_netdev_close ( struct net_device *netdev ); |
---|
148 | static int net80211_netdev_transmit ( struct net_device *netdev, |
---|
149 | struct io_buffer *iobuf ); |
---|
150 | static void net80211_netdev_poll ( struct net_device *netdev ); |
---|
151 | static void net80211_netdev_irq ( struct net_device *netdev, int enable ); |
---|
152 | /** @} */ |
---|
153 | |
---|
154 | /** |
---|
155 | * @defgroup net80211_linklayer 802.11 link-layer protocol functions |
---|
156 | * @{ |
---|
157 | */ |
---|
158 | static int net80211_ll_push ( struct net_device *netdev, |
---|
159 | struct io_buffer *iobuf, const void *ll_dest, |
---|
160 | const void *ll_source, uint16_t net_proto ); |
---|
161 | static int net80211_ll_pull ( struct net_device *netdev, |
---|
162 | struct io_buffer *iobuf, const void **ll_dest, |
---|
163 | const void **ll_source, uint16_t * net_proto ); |
---|
164 | /** @} */ |
---|
165 | |
---|
166 | /** |
---|
167 | * @defgroup net80211_help 802.11 helper functions |
---|
168 | * @{ |
---|
169 | */ |
---|
170 | static void net80211_add_channels ( struct net80211_device *dev, int start, |
---|
171 | int len, int txpower ); |
---|
172 | static void net80211_filter_hw_channels ( struct net80211_device *dev ); |
---|
173 | static void net80211_set_rtscts_rate ( struct net80211_device *dev ); |
---|
174 | static int net80211_process_capab ( struct net80211_device *dev, |
---|
175 | u16 capab ); |
---|
176 | static int net80211_process_ie ( struct net80211_device *dev, |
---|
177 | union ieee80211_ie *ie, void *ie_end ); |
---|
178 | static union ieee80211_ie * |
---|
179 | net80211_marshal_request_info ( struct net80211_device *dev, |
---|
180 | union ieee80211_ie *ie ); |
---|
181 | /** @} */ |
---|
182 | |
---|
183 | /** |
---|
184 | * @defgroup net80211_assoc_ll 802.11 association handling functions |
---|
185 | * @{ |
---|
186 | */ |
---|
187 | static void net80211_step_associate ( struct process *proc ); |
---|
188 | static void net80211_handle_auth ( struct net80211_device *dev, |
---|
189 | struct io_buffer *iob ); |
---|
190 | static void net80211_handle_assoc_reply ( struct net80211_device *dev, |
---|
191 | struct io_buffer *iob ); |
---|
192 | static int net80211_send_disassoc ( struct net80211_device *dev, int reason, |
---|
193 | int deauth ); |
---|
194 | static void net80211_handle_mgmt ( struct net80211_device *dev, |
---|
195 | struct io_buffer *iob, int signal ); |
---|
196 | /** @} */ |
---|
197 | |
---|
198 | /** |
---|
199 | * @defgroup net80211_frag 802.11 fragment handling functions |
---|
200 | * @{ |
---|
201 | */ |
---|
202 | static void net80211_free_frags ( struct net80211_device *dev, int fcid ); |
---|
203 | static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev, |
---|
204 | int fcid, int nfrags, int size ); |
---|
205 | static void net80211_rx_frag ( struct net80211_device *dev, |
---|
206 | struct io_buffer *iob, int signal ); |
---|
207 | /** @} */ |
---|
208 | |
---|
209 | /** |
---|
210 | * @defgroup net80211_settings 802.11 settings handlers |
---|
211 | * @{ |
---|
212 | */ |
---|
213 | static int net80211_check_settings_update ( void ); |
---|
214 | |
---|
215 | /** 802.11 settings applicator |
---|
216 | * |
---|
217 | * When the SSID is changed, this will cause any open devices to |
---|
218 | * re-associate; when the encryption key is changed, we similarly |
---|
219 | * update their state. |
---|
220 | */ |
---|
221 | struct settings_applicator net80211_applicator __settings_applicator = { |
---|
222 | .apply = net80211_check_settings_update, |
---|
223 | }; |
---|
224 | |
---|
225 | /** The network name to associate with |
---|
226 | * |
---|
227 | * If this is blank, we scan for all networks and use the one with the |
---|
228 | * greatest signal strength. |
---|
229 | */ |
---|
230 | struct setting net80211_ssid_setting __setting = { |
---|
231 | .name = "ssid", |
---|
232 | .description = "802.11 SSID (network name)", |
---|
233 | .type = &setting_type_string, |
---|
234 | }; |
---|
235 | |
---|
236 | /** Whether to use active scanning |
---|
237 | * |
---|
238 | * In order to associate with a hidden SSID, it's necessary to use an |
---|
239 | * active scan (send probe packets). If this setting is nonzero, an |
---|
240 | * active scan on the 2.4GHz band will be used to associate. |
---|
241 | */ |
---|
242 | struct setting net80211_active_setting __setting = { |
---|
243 | .name = "active-scan", |
---|
244 | .description = "Use an active scan during 802.11 association", |
---|
245 | .type = &setting_type_int8, |
---|
246 | }; |
---|
247 | |
---|
248 | /** The cryptographic key to use |
---|
249 | * |
---|
250 | * For hex WEP keys, as is common, this must be entered using the |
---|
251 | * normal gPXE method for entering hex settings; an ASCII string of |
---|
252 | * hex characters will not behave as expected. |
---|
253 | */ |
---|
254 | struct setting net80211_key_setting __setting = { |
---|
255 | .name = "key", |
---|
256 | .description = "Encryption key for protected 802.11 networks", |
---|
257 | .type = &setting_type_string, |
---|
258 | }; |
---|
259 | |
---|
260 | /** @} */ |
---|
261 | |
---|
262 | |
---|
263 | /* ---------- net_device wrapper ---------- */ |
---|
264 | |
---|
265 | /** |
---|
266 | * Open 802.11 device and start association |
---|
267 | * |
---|
268 | * @v netdev Wrapping network device |
---|
269 | * @ret rc Return status code |
---|
270 | * |
---|
271 | * This sets up a default conservative set of channels for probing, |
---|
272 | * and starts the auto-association task unless the @c |
---|
273 | * NET80211_NO_ASSOC flag is set in the wrapped 802.11 device's @c |
---|
274 | * state field. |
---|
275 | */ |
---|
276 | static int net80211_netdev_open ( struct net_device *netdev ) |
---|
277 | { |
---|
278 | struct net80211_device *dev = netdev->priv; |
---|
279 | int rc = 0; |
---|
280 | |
---|
281 | if ( dev->op == &net80211_null_ops ) |
---|
282 | return -EFAULT; |
---|
283 | |
---|
284 | if ( dev->op->open ) |
---|
285 | rc = dev->op->open ( dev ); |
---|
286 | |
---|
287 | if ( rc < 0 ) |
---|
288 | return rc; |
---|
289 | |
---|
290 | if ( ! ( dev->state & NET80211_NO_ASSOC ) ) |
---|
291 | net80211_autoassociate ( dev ); |
---|
292 | |
---|
293 | return 0; |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * Close 802.11 device |
---|
298 | * |
---|
299 | * @v netdev Wrapping network device. |
---|
300 | * |
---|
301 | * If the association task is running, this will stop it. |
---|
302 | */ |
---|
303 | static void net80211_netdev_close ( struct net_device *netdev ) |
---|
304 | { |
---|
305 | struct net80211_device *dev = netdev->priv; |
---|
306 | |
---|
307 | if ( dev->state & NET80211_WORKING ) |
---|
308 | process_del ( &dev->proc_assoc ); |
---|
309 | |
---|
310 | /* Send disassociation frame to AP, to be polite */ |
---|
311 | if ( dev->state & NET80211_ASSOCIATED ) |
---|
312 | net80211_send_disassoc ( dev, IEEE80211_REASON_LEAVING, 0 ); |
---|
313 | |
---|
314 | if ( dev->handshaker && dev->handshaker->stop && |
---|
315 | dev->handshaker->started ) |
---|
316 | dev->handshaker->stop ( dev ); |
---|
317 | |
---|
318 | free ( dev->crypto ); |
---|
319 | free ( dev->handshaker ); |
---|
320 | dev->crypto = NULL; |
---|
321 | dev->handshaker = NULL; |
---|
322 | |
---|
323 | netdev_link_down ( netdev ); |
---|
324 | dev->state = 0; |
---|
325 | |
---|
326 | if ( dev->op->close ) |
---|
327 | dev->op->close ( dev ); |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * Transmit packet on 802.11 device |
---|
332 | * |
---|
333 | * @v netdev Wrapping network device |
---|
334 | * @v iobuf I/O buffer |
---|
335 | * @ret rc Return status code |
---|
336 | * |
---|
337 | * If encryption is enabled for the currently associated network, the |
---|
338 | * packet will be encrypted prior to transmission. |
---|
339 | */ |
---|
340 | static int net80211_netdev_transmit ( struct net_device *netdev, |
---|
341 | struct io_buffer *iobuf ) |
---|
342 | { |
---|
343 | struct net80211_device *dev = netdev->priv; |
---|
344 | struct ieee80211_frame *hdr = iobuf->data; |
---|
345 | int rc = -ENOSYS; |
---|
346 | |
---|
347 | if ( dev->crypto && ! ( hdr->fc & IEEE80211_FC_PROTECTED ) && |
---|
348 | ( ( hdr->fc & IEEE80211_FC_TYPE ) == IEEE80211_TYPE_DATA ) ) { |
---|
349 | struct io_buffer *niob = dev->crypto->encrypt ( dev->crypto, |
---|
350 | iobuf ); |
---|
351 | if ( ! niob ) |
---|
352 | return -ENOMEM; /* only reason encryption could fail */ |
---|
353 | |
---|
354 | /* Free the non-encrypted iob */ |
---|
355 | netdev_tx_complete ( netdev, iobuf ); |
---|
356 | |
---|
357 | /* Transmit the encrypted iob; the Protected flag is |
---|
358 | set, so we won't recurse into here again */ |
---|
359 | netdev_tx ( netdev, niob ); |
---|
360 | |
---|
361 | /* Don't transmit the freed packet */ |
---|
362 | return 0; |
---|
363 | } |
---|
364 | |
---|
365 | if ( dev->op->transmit ) |
---|
366 | rc = dev->op->transmit ( dev, iobuf ); |
---|
367 | |
---|
368 | return rc; |
---|
369 | } |
---|
370 | |
---|
371 | /** |
---|
372 | * Poll 802.11 device for received packets and completed transmissions |
---|
373 | * |
---|
374 | * @v netdev Wrapping network device |
---|
375 | */ |
---|
376 | static void net80211_netdev_poll ( struct net_device *netdev ) |
---|
377 | { |
---|
378 | struct net80211_device *dev = netdev->priv; |
---|
379 | |
---|
380 | if ( dev->op->poll ) |
---|
381 | dev->op->poll ( dev ); |
---|
382 | } |
---|
383 | |
---|
384 | /** |
---|
385 | * Enable or disable interrupts for 802.11 device |
---|
386 | * |
---|
387 | * @v netdev Wrapping network device |
---|
388 | * @v enable Whether to enable interrupts |
---|
389 | */ |
---|
390 | static void net80211_netdev_irq ( struct net_device *netdev, int enable ) |
---|
391 | { |
---|
392 | struct net80211_device *dev = netdev->priv; |
---|
393 | |
---|
394 | if ( dev->op->irq ) |
---|
395 | dev->op->irq ( dev, enable ); |
---|
396 | } |
---|
397 | |
---|
398 | /** Network device operations for a wrapped 802.11 device */ |
---|
399 | static struct net_device_operations net80211_netdev_ops = { |
---|
400 | .open = net80211_netdev_open, |
---|
401 | .close = net80211_netdev_close, |
---|
402 | .transmit = net80211_netdev_transmit, |
---|
403 | .poll = net80211_netdev_poll, |
---|
404 | .irq = net80211_netdev_irq, |
---|
405 | }; |
---|
406 | |
---|
407 | |
---|
408 | /* ---------- 802.11 link-layer protocol ---------- */ |
---|
409 | |
---|
410 | /** 802.11 broadcast MAC address */ |
---|
411 | static u8 net80211_ll_broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
---|
412 | |
---|
413 | /** |
---|
414 | * Determine whether a transmission rate uses ERP/OFDM |
---|
415 | * |
---|
416 | * @v rate Rate in 100 kbps units |
---|
417 | * @ret is_erp TRUE if the rate is an ERP/OFDM rate |
---|
418 | * |
---|
419 | * 802.11b supports rates of 1.0, 2.0, 5.5, and 11.0 Mbps; any other |
---|
420 | * rate than these on the 2.4GHz spectrum is an ERP (802.11g) rate. |
---|
421 | */ |
---|
422 | static inline int net80211_rate_is_erp ( u16 rate ) |
---|
423 | { |
---|
424 | if ( rate == 10 || rate == 20 || rate == 55 || rate == 110 ) |
---|
425 | return 0; |
---|
426 | return 1; |
---|
427 | } |
---|
428 | |
---|
429 | |
---|
430 | /** |
---|
431 | * Calculate one frame's contribution to 802.11 duration field |
---|
432 | * |
---|
433 | * @v dev 802.11 device |
---|
434 | * @v bytes Amount of data to calculate duration for |
---|
435 | * @ret dur Duration field in microseconds |
---|
436 | * |
---|
437 | * To avoid multiple stations attempting to transmit at once, 802.11 |
---|
438 | * provides that every packet shall include a duration field |
---|
439 | * specifying a length of time for which the wireless medium will be |
---|
440 | * reserved after it is transmitted. The duration is measured in |
---|
441 | * microseconds and is calculated with respect to the current |
---|
442 | * physical-layer parameters of the 802.11 device. |
---|
443 | * |
---|
444 | * For an unfragmented data or management frame, or the last fragment |
---|
445 | * of a fragmented frame, the duration captures only the 10 data bytes |
---|
446 | * of one ACK; call once with bytes = 10. |
---|
447 | * |
---|
448 | * For a fragment of a data or management rame that will be followed |
---|
449 | * by more fragments, the duration captures an ACK, the following |
---|
450 | * fragment, and its ACK; add the results of three calls, two with |
---|
451 | * bytes = 10 and one with bytes set to the next fragment's size. |
---|
452 | * |
---|
453 | * For an RTS control frame, the duration captures the responding CTS, |
---|
454 | * the frame being sent, and its ACK; add the results of three calls, |
---|
455 | * two with bytes = 10 and one with bytes set to the next frame's size |
---|
456 | * (assuming unfragmented). |
---|
457 | * |
---|
458 | * For a CTS-to-self control frame, the duration captures the frame |
---|
459 | * being protected and its ACK; add the results of two calls, one with |
---|
460 | * bytes = 10 and one with bytes set to the next frame's size. |
---|
461 | * |
---|
462 | * No other frame types are currently supported by gPXE. |
---|
463 | */ |
---|
464 | u16 net80211_duration ( struct net80211_device *dev, int bytes, u16 rate ) |
---|
465 | { |
---|
466 | struct net80211_channel *chan = &dev->channels[dev->channel]; |
---|
467 | u32 kbps = rate * 100; |
---|
468 | |
---|
469 | if ( chan->band == NET80211_BAND_5GHZ || net80211_rate_is_erp ( rate ) ) { |
---|
470 | /* OFDM encoding (802.11a/g) */ |
---|
471 | int bits_per_symbol = ( kbps * 4 ) / 1000; /* 4us/symbol */ |
---|
472 | int bits = 22 + ( bytes << 3 ); /* 22-bit PLCP */ |
---|
473 | int symbols = ( bits + bits_per_symbol - 1 ) / bits_per_symbol; |
---|
474 | |
---|
475 | return 16 + 20 + ( symbols * 4 ); /* 16us SIFS, 20us preamble */ |
---|
476 | } else { |
---|
477 | /* CCK encoding (802.11b) */ |
---|
478 | int phy_time = 144 + 48; /* preamble + PLCP */ |
---|
479 | int bits = bytes << 3; |
---|
480 | int data_time = ( bits * 1000 + kbps - 1 ) / kbps; |
---|
481 | |
---|
482 | if ( dev->phy_flags & NET80211_PHY_USE_SHORT_PREAMBLE ) |
---|
483 | phy_time >>= 1; |
---|
484 | |
---|
485 | return 10 + phy_time + data_time; /* 10us SIFS */ |
---|
486 | } |
---|
487 | } |
---|
488 | |
---|
489 | /** |
---|
490 | * Add 802.11 link-layer header |
---|
491 | * |
---|
492 | * @v netdev Wrapping network device |
---|
493 | * @v iobuf I/O buffer |
---|
494 | * @v ll_dest Link-layer destination address |
---|
495 | * @v ll_source Link-layer source address |
---|
496 | * @v net_proto Network-layer protocol, in network byte order |
---|
497 | * @ret rc Return status code |
---|
498 | * |
---|
499 | * This adds both the 802.11 frame header and the 802.2 LLC/SNAP |
---|
500 | * header used on data packets. |
---|
501 | * |
---|
502 | * We also check here for state of the link that would make it invalid |
---|
503 | * to send a data packet; every data packet must pass through here, |
---|
504 | * and no non-data packet (e.g. management frame) should. |
---|
505 | */ |
---|
506 | static int net80211_ll_push ( struct net_device *netdev, |
---|
507 | struct io_buffer *iobuf, const void *ll_dest, |
---|
508 | const void *ll_source, uint16_t net_proto ) |
---|
509 | { |
---|
510 | struct net80211_device *dev = netdev->priv; |
---|
511 | struct ieee80211_frame *hdr = iob_push ( iobuf, |
---|
512 | IEEE80211_LLC_HEADER_LEN + |
---|
513 | IEEE80211_TYP_FRAME_HEADER_LEN ); |
---|
514 | struct ieee80211_llc_snap_header *lhdr = |
---|
515 | ( void * ) hdr + IEEE80211_TYP_FRAME_HEADER_LEN; |
---|
516 | |
---|
517 | /* We can't send data packets if we're not associated. */ |
---|
518 | if ( ! ( dev->state & NET80211_ASSOCIATED ) ) { |
---|
519 | if ( dev->assoc_rc ) |
---|
520 | return dev->assoc_rc; |
---|
521 | return -ENETUNREACH; |
---|
522 | } |
---|
523 | |
---|
524 | hdr->fc = IEEE80211_THIS_VERSION | IEEE80211_TYPE_DATA | |
---|
525 | IEEE80211_STYPE_DATA | IEEE80211_FC_TODS; |
---|
526 | |
---|
527 | /* We don't send fragmented frames, so duration is the time |
---|
528 | for an SIFS + 10-byte ACK. */ |
---|
529 | hdr->duration = net80211_duration ( dev, 10, dev->rates[dev->rate] ); |
---|
530 | |
---|
531 | memcpy ( hdr->addr1, dev->bssid, ETH_ALEN ); |
---|
532 | memcpy ( hdr->addr2, ll_source, ETH_ALEN ); |
---|
533 | memcpy ( hdr->addr3, ll_dest, ETH_ALEN ); |
---|
534 | |
---|
535 | hdr->seq = IEEE80211_MAKESEQ ( ++dev->last_tx_seqnr, 0 ); |
---|
536 | |
---|
537 | lhdr->dsap = IEEE80211_LLC_DSAP; |
---|
538 | lhdr->ssap = IEEE80211_LLC_SSAP; |
---|
539 | lhdr->ctrl = IEEE80211_LLC_CTRL; |
---|
540 | memset ( lhdr->oui, 0x00, 3 ); |
---|
541 | lhdr->ethertype = net_proto; |
---|
542 | |
---|
543 | return 0; |
---|
544 | } |
---|
545 | |
---|
546 | /** |
---|
547 | * Remove 802.11 link-layer header |
---|
548 | * |
---|
549 | * @v netdev Wrapping network device |
---|
550 | * @v iobuf I/O buffer |
---|
551 | * @ret ll_dest Link-layer destination address |
---|
552 | * @ret ll_source Link-layer source |
---|
553 | * @ret net_proto Network-layer protocol, in network byte order |
---|
554 | * @ret rc Return status code |
---|
555 | * |
---|
556 | * This expects and removes both the 802.11 frame header and the 802.2 |
---|
557 | * LLC/SNAP header that are used on data packets. |
---|
558 | */ |
---|
559 | static int net80211_ll_pull ( struct net_device *netdev __unused, |
---|
560 | struct io_buffer *iobuf, |
---|
561 | const void **ll_dest, const void **ll_source, |
---|
562 | uint16_t * net_proto ) |
---|
563 | { |
---|
564 | struct ieee80211_frame *hdr = iobuf->data; |
---|
565 | struct ieee80211_llc_snap_header *lhdr = |
---|
566 | ( void * ) hdr + IEEE80211_TYP_FRAME_HEADER_LEN; |
---|
567 | |
---|
568 | /* Bunch of sanity checks */ |
---|
569 | if ( iob_len ( iobuf ) < IEEE80211_TYP_FRAME_HEADER_LEN + |
---|
570 | IEEE80211_LLC_HEADER_LEN ) { |
---|
571 | DBGC ( netdev->priv, "802.11 %p packet too short (%zd bytes)\n", |
---|
572 | netdev->priv, iob_len ( iobuf ) ); |
---|
573 | return -EINVAL_PKT_TOO_SHORT; |
---|
574 | } |
---|
575 | |
---|
576 | if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION ) { |
---|
577 | DBGC ( netdev->priv, "802.11 %p packet invalid version %04x\n", |
---|
578 | netdev->priv, hdr->fc & IEEE80211_FC_VERSION ); |
---|
579 | return -EINVAL_PKT_VERSION; |
---|
580 | } |
---|
581 | |
---|
582 | if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_DATA || |
---|
583 | ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA ) { |
---|
584 | DBGC ( netdev->priv, "802.11 %p packet not data/data (fc=%04x)\n", |
---|
585 | netdev->priv, hdr->fc ); |
---|
586 | return -EINVAL_PKT_NOT_DATA; |
---|
587 | } |
---|
588 | |
---|
589 | if ( ( hdr->fc & ( IEEE80211_FC_TODS | IEEE80211_FC_FROMDS ) ) != |
---|
590 | IEEE80211_FC_FROMDS ) { |
---|
591 | DBGC ( netdev->priv, "802.11 %p packet not from DS (fc=%04x)\n", |
---|
592 | netdev->priv, hdr->fc ); |
---|
593 | return -EINVAL_PKT_NOT_FROMDS; |
---|
594 | } |
---|
595 | |
---|
596 | if ( lhdr->dsap != IEEE80211_LLC_DSAP || lhdr->ssap != IEEE80211_LLC_SSAP || |
---|
597 | lhdr->ctrl != IEEE80211_LLC_CTRL || lhdr->oui[0] || lhdr->oui[1] || |
---|
598 | lhdr->oui[2] ) { |
---|
599 | DBGC ( netdev->priv, "802.11 %p LLC header is not plain EtherType " |
---|
600 | "encapsulator: %02x->%02x [%02x] %02x:%02x:%02x %04x\n", |
---|
601 | netdev->priv, lhdr->dsap, lhdr->ssap, lhdr->ctrl, |
---|
602 | lhdr->oui[0], lhdr->oui[1], lhdr->oui[2], lhdr->ethertype ); |
---|
603 | return -EINVAL_PKT_LLC_HEADER; |
---|
604 | } |
---|
605 | |
---|
606 | iob_pull ( iobuf, sizeof ( *hdr ) + sizeof ( *lhdr ) ); |
---|
607 | |
---|
608 | *ll_dest = hdr->addr1; |
---|
609 | *ll_source = hdr->addr3; |
---|
610 | *net_proto = lhdr->ethertype; |
---|
611 | return 0; |
---|
612 | } |
---|
613 | |
---|
614 | /** 802.11 link-layer protocol */ |
---|
615 | static struct ll_protocol net80211_ll_protocol __ll_protocol = { |
---|
616 | .name = "802.11", |
---|
617 | .push = net80211_ll_push, |
---|
618 | .pull = net80211_ll_pull, |
---|
619 | .init_addr = eth_init_addr, |
---|
620 | .ntoa = eth_ntoa, |
---|
621 | .mc_hash = eth_mc_hash, |
---|
622 | .eth_addr = eth_eth_addr, |
---|
623 | .ll_proto = htons ( ARPHRD_ETHER ), /* "encapsulated Ethernet" */ |
---|
624 | .hw_addr_len = ETH_ALEN, |
---|
625 | .ll_addr_len = ETH_ALEN, |
---|
626 | .ll_header_len = IEEE80211_TYP_FRAME_HEADER_LEN + |
---|
627 | IEEE80211_LLC_HEADER_LEN, |
---|
628 | }; |
---|
629 | |
---|
630 | |
---|
631 | /* ---------- 802.11 network management API ---------- */ |
---|
632 | |
---|
633 | /** |
---|
634 | * Get 802.11 device from wrapping network device |
---|
635 | * |
---|
636 | * @v netdev Wrapping network device |
---|
637 | * @ret dev 802.11 device wrapped by network device, or NULL |
---|
638 | * |
---|
639 | * Returns NULL if the network device does not wrap an 802.11 device. |
---|
640 | */ |
---|
641 | struct net80211_device * net80211_get ( struct net_device *netdev ) |
---|
642 | { |
---|
643 | struct net80211_device *dev; |
---|
644 | |
---|
645 | list_for_each_entry ( dev, &net80211_devices, list ) { |
---|
646 | if ( netdev->priv == dev ) |
---|
647 | return netdev->priv; |
---|
648 | } |
---|
649 | |
---|
650 | return NULL; |
---|
651 | } |
---|
652 | |
---|
653 | /** |
---|
654 | * Set state of 802.11 device keeping management frames |
---|
655 | * |
---|
656 | * @v dev 802.11 device |
---|
657 | * @v enable Whether to keep management frames |
---|
658 | * @ret oldenab Whether management frames were enabled before this call |
---|
659 | * |
---|
660 | * If enable is TRUE, beacon, probe, and action frames will be kept |
---|
661 | * and may be retrieved by calling net80211_mgmt_dequeue(). |
---|
662 | */ |
---|
663 | int net80211_keep_mgmt ( struct net80211_device *dev, int enable ) |
---|
664 | { |
---|
665 | int oldenab = dev->keep_mgmt; |
---|
666 | |
---|
667 | dev->keep_mgmt = enable; |
---|
668 | return oldenab; |
---|
669 | } |
---|
670 | |
---|
671 | /** |
---|
672 | * Get 802.11 management frame |
---|
673 | * |
---|
674 | * @v dev 802.11 device |
---|
675 | * @ret signal Signal strength of returned management frame |
---|
676 | * @ret iob I/O buffer, or NULL if no management frame is queued |
---|
677 | * |
---|
678 | * Frames will only be returned by this function if |
---|
679 | * net80211_keep_mgmt() has been previously called with enable set to |
---|
680 | * TRUE. |
---|
681 | * |
---|
682 | * The calling function takes ownership of the returned I/O buffer. |
---|
683 | */ |
---|
684 | struct io_buffer * net80211_mgmt_dequeue ( struct net80211_device *dev, |
---|
685 | int *signal ) |
---|
686 | { |
---|
687 | struct io_buffer *iobuf; |
---|
688 | struct net80211_rx_info *rxi; |
---|
689 | |
---|
690 | list_for_each_entry ( rxi, &dev->mgmt_info_queue, list ) { |
---|
691 | list_del ( &rxi->list ); |
---|
692 | if ( signal ) |
---|
693 | *signal = rxi->signal; |
---|
694 | free ( rxi ); |
---|
695 | |
---|
696 | list_for_each_entry ( iobuf, &dev->mgmt_queue, list ) { |
---|
697 | list_del ( &iobuf->list ); |
---|
698 | return iobuf; |
---|
699 | } |
---|
700 | assert ( 0 ); |
---|
701 | } |
---|
702 | |
---|
703 | return NULL; |
---|
704 | } |
---|
705 | |
---|
706 | /** |
---|
707 | * Transmit 802.11 management frame |
---|
708 | * |
---|
709 | * @v dev 802.11 device |
---|
710 | * @v fc Frame Control flags for management frame |
---|
711 | * @v dest Destination access point |
---|
712 | * @v iob I/O buffer |
---|
713 | * @ret rc Return status code |
---|
714 | * |
---|
715 | * The @a fc argument must contain at least an IEEE 802.11 management |
---|
716 | * subtype number (e.g. IEEE80211_STYPE_PROBE_REQ). If it contains |
---|
717 | * IEEE80211_FC_PROTECTED, the frame will be encrypted prior to |
---|
718 | * transmission. |
---|
719 | * |
---|
720 | * It is required that @a iob have at least 24 bytes of headroom |
---|
721 | * reserved before its data start. |
---|
722 | */ |
---|
723 | int net80211_tx_mgmt ( struct net80211_device *dev, u16 fc, u8 dest[6], |
---|
724 | struct io_buffer *iob ) |
---|
725 | { |
---|
726 | struct ieee80211_frame *hdr = iob_push ( iob, |
---|
727 | IEEE80211_TYP_FRAME_HEADER_LEN ); |
---|
728 | |
---|
729 | hdr->fc = IEEE80211_THIS_VERSION | IEEE80211_TYPE_MGMT | |
---|
730 | ( fc & ~IEEE80211_FC_PROTECTED ); |
---|
731 | hdr->duration = net80211_duration ( dev, 10, dev->rates[dev->rate] ); |
---|
732 | hdr->seq = IEEE80211_MAKESEQ ( ++dev->last_tx_seqnr, 0 ); |
---|
733 | |
---|
734 | memcpy ( hdr->addr1, dest, ETH_ALEN ); /* DA = RA */ |
---|
735 | memcpy ( hdr->addr2, dev->netdev->ll_addr, ETH_ALEN ); /* SA = TA */ |
---|
736 | memcpy ( hdr->addr3, dest, ETH_ALEN ); /* BSSID */ |
---|
737 | |
---|
738 | if ( fc & IEEE80211_FC_PROTECTED ) { |
---|
739 | if ( ! dev->crypto ) |
---|
740 | return -EINVAL_CRYPTO_REQUEST; |
---|
741 | |
---|
742 | struct io_buffer *eiob = dev->crypto->encrypt ( dev->crypto, |
---|
743 | iob ); |
---|
744 | free_iob ( iob ); |
---|
745 | iob = eiob; |
---|
746 | } |
---|
747 | |
---|
748 | return netdev_tx ( dev->netdev, iob ); |
---|
749 | } |
---|
750 | |
---|
751 | |
---|
752 | /* ---------- Driver API ---------- */ |
---|
753 | |
---|
754 | /** |
---|
755 | * Allocate 802.11 device |
---|
756 | * |
---|
757 | * @v priv_size Size of driver-private allocation area |
---|
758 | * @ret dev Newly allocated 802.11 device |
---|
759 | * |
---|
760 | * This function allocates a net_device with space in its private area |
---|
761 | * for both the net80211_device it will wrap and the driver-private |
---|
762 | * data space requested. It initializes the link-layer-specific parts |
---|
763 | * of the net_device, and links the net80211_device to the net_device |
---|
764 | * appropriately. |
---|
765 | */ |
---|
766 | struct net80211_device * net80211_alloc ( size_t priv_size ) |
---|
767 | { |
---|
768 | struct net80211_device *dev; |
---|
769 | struct net_device *netdev = |
---|
770 | alloc_netdev ( sizeof ( *dev ) + priv_size ); |
---|
771 | |
---|
772 | if ( ! netdev ) |
---|
773 | return NULL; |
---|
774 | |
---|
775 | netdev->ll_protocol = &net80211_ll_protocol; |
---|
776 | netdev->ll_broadcast = net80211_ll_broadcast; |
---|
777 | netdev->max_pkt_len = IEEE80211_MAX_DATA_LEN; |
---|
778 | netdev_init ( netdev, &net80211_netdev_ops ); |
---|
779 | |
---|
780 | dev = netdev->priv; |
---|
781 | dev->netdev = netdev; |
---|
782 | dev->priv = ( u8 * ) dev + sizeof ( *dev ); |
---|
783 | dev->op = &net80211_null_ops; |
---|
784 | |
---|
785 | process_init_stopped ( &dev->proc_assoc, net80211_step_associate, |
---|
786 | &netdev->refcnt ); |
---|
787 | INIT_LIST_HEAD ( &dev->mgmt_queue ); |
---|
788 | INIT_LIST_HEAD ( &dev->mgmt_info_queue ); |
---|
789 | |
---|
790 | return dev; |
---|
791 | } |
---|
792 | |
---|
793 | /** |
---|
794 | * Register 802.11 device with network stack |
---|
795 | * |
---|
796 | * @v dev 802.11 device |
---|
797 | * @v ops 802.11 device operations |
---|
798 | * @v hw 802.11 hardware information |
---|
799 | * |
---|
800 | * This also registers the wrapping net_device with the higher network |
---|
801 | * layers. |
---|
802 | */ |
---|
803 | int net80211_register ( struct net80211_device *dev, |
---|
804 | struct net80211_device_operations *ops, |
---|
805 | struct net80211_hw_info *hw ) |
---|
806 | { |
---|
807 | dev->op = ops; |
---|
808 | dev->hw = malloc ( sizeof ( *hw ) ); |
---|
809 | if ( ! dev->hw ) |
---|
810 | return -ENOMEM; |
---|
811 | |
---|
812 | memcpy ( dev->hw, hw, sizeof ( *hw ) ); |
---|
813 | memcpy ( dev->netdev->hw_addr, hw->hwaddr, ETH_ALEN ); |
---|
814 | |
---|
815 | /* Set some sensible channel defaults for driver's open() function */ |
---|
816 | memcpy ( dev->channels, dev->hw->channels, |
---|
817 | NET80211_MAX_CHANNELS * sizeof ( dev->channels[0] ) ); |
---|
818 | dev->channel = 0; |
---|
819 | |
---|
820 | list_add_tail ( &dev->list, &net80211_devices ); |
---|
821 | return register_netdev ( dev->netdev ); |
---|
822 | } |
---|
823 | |
---|
824 | /** |
---|
825 | * Unregister 802.11 device from network stack |
---|
826 | * |
---|
827 | * @v dev 802.11 device |
---|
828 | * |
---|
829 | * After this call, the device operations are cleared so that they |
---|
830 | * will not be called. |
---|
831 | */ |
---|
832 | void net80211_unregister ( struct net80211_device *dev ) |
---|
833 | { |
---|
834 | unregister_netdev ( dev->netdev ); |
---|
835 | list_del ( &dev->list ); |
---|
836 | dev->op = &net80211_null_ops; |
---|
837 | } |
---|
838 | |
---|
839 | /** |
---|
840 | * Free 802.11 device |
---|
841 | * |
---|
842 | * @v dev 802.11 device |
---|
843 | * |
---|
844 | * The device should be unregistered before this function is called. |
---|
845 | */ |
---|
846 | void net80211_free ( struct net80211_device *dev ) |
---|
847 | { |
---|
848 | free ( dev->hw ); |
---|
849 | rc80211_free ( dev->rctl ); |
---|
850 | netdev_nullify ( dev->netdev ); |
---|
851 | netdev_put ( dev->netdev ); |
---|
852 | } |
---|
853 | |
---|
854 | |
---|
855 | /* ---------- 802.11 network management workhorse code ---------- */ |
---|
856 | |
---|
857 | /** |
---|
858 | * Set state of 802.11 device |
---|
859 | * |
---|
860 | * @v dev 802.11 device |
---|
861 | * @v clear Bitmask of flags to clear |
---|
862 | * @v set Bitmask of flags to set |
---|
863 | * @v status Status or reason code for most recent operation |
---|
864 | * |
---|
865 | * If @a status represents a reason code, it should be OR'ed with |
---|
866 | * NET80211_IS_REASON. |
---|
867 | * |
---|
868 | * Clearing authentication also clears association; clearing |
---|
869 | * association also clears security handshaking state. Clearing |
---|
870 | * association removes the link-up flag from the wrapping net_device, |
---|
871 | * but setting it does not automatically set the flag; that is left to |
---|
872 | * the judgment of higher-level code. |
---|
873 | */ |
---|
874 | static inline void net80211_set_state ( struct net80211_device *dev, |
---|
875 | short clear, short set, |
---|
876 | u16 status ) |
---|
877 | { |
---|
878 | /* The conditions in this function are deliberately formulated |
---|
879 | to be decidable at compile-time in most cases. Since clear |
---|
880 | and set are generally passed as constants, the body of this |
---|
881 | function can be reduced down to a few statements by the |
---|
882 | compiler. */ |
---|
883 | |
---|
884 | const int statmsk = NET80211_STATUS_MASK | NET80211_IS_REASON; |
---|
885 | |
---|
886 | if ( clear & NET80211_PROBED ) |
---|
887 | clear |= NET80211_AUTHENTICATED; |
---|
888 | |
---|
889 | if ( clear & NET80211_AUTHENTICATED ) |
---|
890 | clear |= NET80211_ASSOCIATED; |
---|
891 | |
---|
892 | if ( clear & NET80211_ASSOCIATED ) |
---|
893 | clear |= NET80211_CRYPTO_SYNCED; |
---|
894 | |
---|
895 | dev->state = ( dev->state & ~clear ) | set; |
---|
896 | dev->state = ( dev->state & ~statmsk ) | ( status & statmsk ); |
---|
897 | |
---|
898 | if ( clear & NET80211_ASSOCIATED ) |
---|
899 | netdev_link_down ( dev->netdev ); |
---|
900 | |
---|
901 | if ( ( clear | set ) & NET80211_ASSOCIATED ) |
---|
902 | dev->op->config ( dev, NET80211_CFG_ASSOC ); |
---|
903 | |
---|
904 | if ( status != 0 ) { |
---|
905 | if ( status & NET80211_IS_REASON ) |
---|
906 | dev->assoc_rc = -E80211_REASON ( status ); |
---|
907 | else |
---|
908 | dev->assoc_rc = -E80211_STATUS ( status ); |
---|
909 | netdev_link_err ( dev->netdev, dev->assoc_rc ); |
---|
910 | } |
---|
911 | } |
---|
912 | |
---|
913 | /** |
---|
914 | * Add channels to 802.11 device |
---|
915 | * |
---|
916 | * @v dev 802.11 device |
---|
917 | * @v start First channel number to add |
---|
918 | * @v len Number of channels to add |
---|
919 | * @v txpower TX power (dBm) to allow on added channels |
---|
920 | * |
---|
921 | * To replace the current list of channels instead of adding to it, |
---|
922 | * set the nr_channels field of the 802.11 device to 0 before calling |
---|
923 | * this function. |
---|
924 | */ |
---|
925 | static void net80211_add_channels ( struct net80211_device *dev, int start, |
---|
926 | int len, int txpower ) |
---|
927 | { |
---|
928 | int i, chan = start; |
---|
929 | |
---|
930 | for ( i = dev->nr_channels; len-- && i < NET80211_MAX_CHANNELS; i++ ) { |
---|
931 | dev->channels[i].channel_nr = chan; |
---|
932 | dev->channels[i].maxpower = txpower; |
---|
933 | dev->channels[i].hw_value = 0; |
---|
934 | |
---|
935 | if ( chan >= 1 && chan <= 14 ) { |
---|
936 | dev->channels[i].band = NET80211_BAND_2GHZ; |
---|
937 | if ( chan == 14 ) |
---|
938 | dev->channels[i].center_freq = 2484; |
---|
939 | else |
---|
940 | dev->channels[i].center_freq = 2407 + 5 * chan; |
---|
941 | chan++; |
---|
942 | } else { |
---|
943 | dev->channels[i].band = NET80211_BAND_5GHZ; |
---|
944 | dev->channels[i].center_freq = 5000 + 5 * chan; |
---|
945 | chan += 4; |
---|
946 | } |
---|
947 | } |
---|
948 | |
---|
949 | dev->nr_channels = i; |
---|
950 | } |
---|
951 | |
---|
952 | /** |
---|
953 | * Filter 802.11 device channels for hardware capabilities |
---|
954 | * |
---|
955 | * @v dev 802.11 device |
---|
956 | * |
---|
957 | * Hardware may support fewer channels than regulatory restrictions |
---|
958 | * allow; this function filters out channels in dev->channels that are |
---|
959 | * not supported by the hardware list in dev->hwinfo. It also copies |
---|
960 | * over the net80211_channel::hw_value and limits maximum TX power |
---|
961 | * appropriately. |
---|
962 | * |
---|
963 | * Channels are matched based on center frequency, ignoring band and |
---|
964 | * channel number. |
---|
965 | * |
---|
966 | * If the driver specifies no supported channels, the effect will be |
---|
967 | * as though all were supported. |
---|
968 | */ |
---|
969 | static void net80211_filter_hw_channels ( struct net80211_device *dev ) |
---|
970 | { |
---|
971 | int delta = 0, i = 0; |
---|
972 | int old_freq = dev->channels[dev->channel].center_freq; |
---|
973 | struct net80211_channel *chan, *hwchan; |
---|
974 | |
---|
975 | if ( ! dev->hw->nr_channels ) |
---|
976 | return; |
---|
977 | |
---|
978 | dev->channel = 0; |
---|
979 | for ( chan = dev->channels; chan < dev->channels + dev->nr_channels; |
---|
980 | chan++, i++ ) { |
---|
981 | int ok = 0; |
---|
982 | for ( hwchan = dev->hw->channels; |
---|
983 | hwchan < dev->hw->channels + dev->hw->nr_channels; |
---|
984 | hwchan++ ) { |
---|
985 | if ( hwchan->center_freq == chan->center_freq ) { |
---|
986 | ok = 1; |
---|
987 | break; |
---|
988 | } |
---|
989 | } |
---|
990 | |
---|
991 | if ( ! ok ) |
---|
992 | delta++; |
---|
993 | else { |
---|
994 | chan->hw_value = hwchan->hw_value; |
---|
995 | if ( hwchan->maxpower != 0 && |
---|
996 | chan->maxpower > hwchan->maxpower ) |
---|
997 | chan->maxpower = hwchan->maxpower; |
---|
998 | if ( old_freq == chan->center_freq ) |
---|
999 | dev->channel = i - delta; |
---|
1000 | if ( delta ) |
---|
1001 | chan[-delta] = *chan; |
---|
1002 | } |
---|
1003 | } |
---|
1004 | |
---|
1005 | dev->nr_channels -= delta; |
---|
1006 | |
---|
1007 | if ( dev->channels[dev->channel].center_freq != old_freq ) |
---|
1008 | dev->op->config ( dev, NET80211_CFG_CHANNEL ); |
---|
1009 | } |
---|
1010 | |
---|
1011 | /** |
---|
1012 | * Update 802.11 device state to reflect received capabilities field |
---|
1013 | * |
---|
1014 | * @v dev 802.11 device |
---|
1015 | * @v capab Capabilities field in beacon, probe, or association frame |
---|
1016 | * @ret rc Return status code |
---|
1017 | */ |
---|
1018 | static int net80211_process_capab ( struct net80211_device *dev, |
---|
1019 | u16 capab ) |
---|
1020 | { |
---|
1021 | u16 old_phy = dev->phy_flags; |
---|
1022 | |
---|
1023 | if ( ( capab & ( IEEE80211_CAPAB_MANAGED | IEEE80211_CAPAB_ADHOC ) ) != |
---|
1024 | IEEE80211_CAPAB_MANAGED ) { |
---|
1025 | DBGC ( dev, "802.11 %p cannot handle IBSS network\n", dev ); |
---|
1026 | return -ENOSYS; |
---|
1027 | } |
---|
1028 | |
---|
1029 | dev->phy_flags &= ~( NET80211_PHY_USE_SHORT_PREAMBLE | |
---|
1030 | NET80211_PHY_USE_SHORT_SLOT ); |
---|
1031 | |
---|
1032 | if ( capab & IEEE80211_CAPAB_SHORT_PMBL ) |
---|
1033 | dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE; |
---|
1034 | |
---|
1035 | if ( capab & IEEE80211_CAPAB_SHORT_SLOT ) |
---|
1036 | dev->phy_flags |= NET80211_PHY_USE_SHORT_SLOT; |
---|
1037 | |
---|
1038 | if ( old_phy != dev->phy_flags ) |
---|
1039 | dev->op->config ( dev, NET80211_CFG_PHY_PARAMS ); |
---|
1040 | |
---|
1041 | return 0; |
---|
1042 | } |
---|
1043 | |
---|
1044 | /** |
---|
1045 | * Update 802.11 device state to reflect received information elements |
---|
1046 | * |
---|
1047 | * @v dev 802.11 device |
---|
1048 | * @v ie Pointer to first information element |
---|
1049 | * @v ie_end Pointer to tail of packet I/O buffer |
---|
1050 | * @ret rc Return status code |
---|
1051 | */ |
---|
1052 | static int net80211_process_ie ( struct net80211_device *dev, |
---|
1053 | union ieee80211_ie *ie, void *ie_end ) |
---|
1054 | { |
---|
1055 | u16 old_rate = dev->rates[dev->rate]; |
---|
1056 | u16 old_phy = dev->phy_flags; |
---|
1057 | int have_rates = 0, i; |
---|
1058 | int ds_channel = 0; |
---|
1059 | int changed = 0; |
---|
1060 | int band = dev->channels[dev->channel].band; |
---|
1061 | |
---|
1062 | if ( ! ieee80211_ie_bound ( ie, ie_end ) ) |
---|
1063 | return 0; |
---|
1064 | |
---|
1065 | for ( ; ie; ie = ieee80211_next_ie ( ie, ie_end ) ) { |
---|
1066 | switch ( ie->id ) { |
---|
1067 | case IEEE80211_IE_SSID: |
---|
1068 | if ( ie->len <= 32 ) { |
---|
1069 | memcpy ( dev->essid, ie->ssid, ie->len ); |
---|
1070 | dev->essid[ie->len] = 0; |
---|
1071 | } |
---|
1072 | break; |
---|
1073 | |
---|
1074 | case IEEE80211_IE_RATES: |
---|
1075 | case IEEE80211_IE_EXT_RATES: |
---|
1076 | if ( ! have_rates ) { |
---|
1077 | dev->nr_rates = 0; |
---|
1078 | dev->basic_rates = 0; |
---|
1079 | have_rates = 1; |
---|
1080 | } |
---|
1081 | for ( i = 0; i < ie->len && |
---|
1082 | dev->nr_rates < NET80211_MAX_RATES; i++ ) { |
---|
1083 | u8 rid = ie->rates[i]; |
---|
1084 | u16 rate = ( rid & 0x7f ) * 5; |
---|
1085 | |
---|
1086 | if ( rid & 0x80 ) |
---|
1087 | dev->basic_rates |= |
---|
1088 | ( 1 << dev->nr_rates ); |
---|
1089 | |
---|
1090 | dev->rates[dev->nr_rates++] = rate; |
---|
1091 | } |
---|
1092 | |
---|
1093 | break; |
---|
1094 | |
---|
1095 | case IEEE80211_IE_DS_PARAM: |
---|
1096 | if ( dev->channel < dev->nr_channels && ds_channel == |
---|
1097 | dev->channels[dev->channel].channel_nr ) |
---|
1098 | break; |
---|
1099 | ds_channel = ie->ds_param.current_channel; |
---|
1100 | net80211_change_channel ( dev, ds_channel ); |
---|
1101 | break; |
---|
1102 | |
---|
1103 | case IEEE80211_IE_COUNTRY: |
---|
1104 | dev->nr_channels = 0; |
---|
1105 | |
---|
1106 | DBGC ( dev, "802.11 %p setting country regulations " |
---|
1107 | "for %c%c\n", dev, ie->country.name[0], |
---|
1108 | ie->country.name[1] ); |
---|
1109 | for ( i = 0; i < ( ie->len - 3 ) / 3; i++ ) { |
---|
1110 | union ieee80211_ie_country_triplet *t = |
---|
1111 | &ie->country.triplet[i]; |
---|
1112 | if ( t->first > 200 ) { |
---|
1113 | DBGC ( dev, "802.11 %p ignoring regulatory " |
---|
1114 | "extension information\n", dev ); |
---|
1115 | } else { |
---|
1116 | net80211_add_channels ( dev, |
---|
1117 | t->band.first_channel, |
---|
1118 | t->band.nr_channels, |
---|
1119 | t->band.max_txpower ); |
---|
1120 | } |
---|
1121 | } |
---|
1122 | net80211_filter_hw_channels ( dev ); |
---|
1123 | break; |
---|
1124 | |
---|
1125 | case IEEE80211_IE_ERP_INFO: |
---|
1126 | dev->phy_flags &= ~( NET80211_PHY_USE_PROTECTION | |
---|
1127 | NET80211_PHY_USE_SHORT_PREAMBLE ); |
---|
1128 | if ( ie->erp_info & IEEE80211_ERP_USE_PROTECTION ) |
---|
1129 | dev->phy_flags |= NET80211_PHY_USE_PROTECTION; |
---|
1130 | if ( ! ( ie->erp_info & IEEE80211_ERP_BARKER_LONG ) ) |
---|
1131 | dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE; |
---|
1132 | break; |
---|
1133 | } |
---|
1134 | } |
---|
1135 | |
---|
1136 | if ( have_rates ) { |
---|
1137 | /* Allow only those rates that are also supported by |
---|
1138 | the hardware. */ |
---|
1139 | int delta = 0, j; |
---|
1140 | |
---|
1141 | dev->rate = 0; |
---|
1142 | for ( i = 0; i < dev->nr_rates; i++ ) { |
---|
1143 | int ok = 0; |
---|
1144 | for ( j = 0; j < dev->hw->nr_rates[band]; j++ ) { |
---|
1145 | if ( dev->hw->rates[band][j] == dev->rates[i] ){ |
---|
1146 | ok = 1; |
---|
1147 | break; |
---|
1148 | } |
---|
1149 | } |
---|
1150 | |
---|
1151 | if ( ! ok ) |
---|
1152 | delta++; |
---|
1153 | else { |
---|
1154 | dev->rates[i - delta] = dev->rates[i]; |
---|
1155 | if ( old_rate == dev->rates[i] ) |
---|
1156 | dev->rate = i - delta; |
---|
1157 | } |
---|
1158 | } |
---|
1159 | |
---|
1160 | dev->nr_rates -= delta; |
---|
1161 | |
---|
1162 | /* Sort available rates - sorted subclumps tend to already |
---|
1163 | exist, so insertion sort works well. */ |
---|
1164 | for ( i = 1; i < dev->nr_rates; i++ ) { |
---|
1165 | u16 rate = dev->rates[i]; |
---|
1166 | u32 tmp, br, mask; |
---|
1167 | |
---|
1168 | for ( j = i - 1; j >= 0 && dev->rates[j] >= rate; j-- ) |
---|
1169 | dev->rates[j + 1] = dev->rates[j]; |
---|
1170 | dev->rates[j + 1] = rate; |
---|
1171 | |
---|
1172 | /* Adjust basic_rates to match by rotating the |
---|
1173 | bits from bit j+1 to bit i left one position. */ |
---|
1174 | mask = ( ( 1 << i ) - 1 ) & ~( ( 1 << ( j + 1 ) ) - 1 ); |
---|
1175 | br = dev->basic_rates; |
---|
1176 | tmp = br & ( 1 << i ); |
---|
1177 | br = ( br & ~( mask | tmp ) ) | ( ( br & mask ) << 1 ); |
---|
1178 | br |= ( tmp >> ( i - j - 1 ) ); |
---|
1179 | dev->basic_rates = br; |
---|
1180 | } |
---|
1181 | |
---|
1182 | net80211_set_rtscts_rate ( dev ); |
---|
1183 | |
---|
1184 | if ( dev->rates[dev->rate] != old_rate ) |
---|
1185 | changed |= NET80211_CFG_RATE; |
---|
1186 | } |
---|
1187 | |
---|
1188 | if ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE ) |
---|
1189 | dev->phy_flags &= ~NET80211_PHY_USE_SHORT_PREAMBLE; |
---|
1190 | if ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT ) |
---|
1191 | dev->phy_flags &= ~NET80211_PHY_USE_SHORT_SLOT; |
---|
1192 | |
---|
1193 | if ( old_phy != dev->phy_flags ) |
---|
1194 | changed |= NET80211_CFG_PHY_PARAMS; |
---|
1195 | |
---|
1196 | if ( changed ) |
---|
1197 | dev->op->config ( dev, changed ); |
---|
1198 | |
---|
1199 | return 0; |
---|
1200 | } |
---|
1201 | |
---|
1202 | /** |
---|
1203 | * Create information elements for outgoing probe or association packet |
---|
1204 | * |
---|
1205 | * @v dev 802.11 device |
---|
1206 | * @v ie Pointer to start of information element area |
---|
1207 | * @ret next_ie Pointer to first byte after added information elements |
---|
1208 | */ |
---|
1209 | static union ieee80211_ie * |
---|
1210 | net80211_marshal_request_info ( struct net80211_device *dev, |
---|
1211 | union ieee80211_ie *ie ) |
---|
1212 | { |
---|
1213 | int i; |
---|
1214 | |
---|
1215 | ie->id = IEEE80211_IE_SSID; |
---|
1216 | ie->len = strlen ( dev->essid ); |
---|
1217 | memcpy ( ie->ssid, dev->essid, ie->len ); |
---|
1218 | |
---|
1219 | ie = ieee80211_next_ie ( ie, NULL ); |
---|
1220 | |
---|
1221 | ie->id = IEEE80211_IE_RATES; |
---|
1222 | ie->len = dev->nr_rates; |
---|
1223 | if ( ie->len > 8 ) |
---|
1224 | ie->len = 8; |
---|
1225 | |
---|
1226 | for ( i = 0; i < ie->len; i++ ) { |
---|
1227 | ie->rates[i] = dev->rates[i] / 5; |
---|
1228 | if ( dev->basic_rates & ( 1 << i ) ) |
---|
1229 | ie->rates[i] |= 0x80; |
---|
1230 | } |
---|
1231 | |
---|
1232 | ie = ieee80211_next_ie ( ie, NULL ); |
---|
1233 | |
---|
1234 | if ( dev->rsn_ie && dev->rsn_ie->id == IEEE80211_IE_RSN ) { |
---|
1235 | memcpy ( ie, dev->rsn_ie, dev->rsn_ie->len + 2 ); |
---|
1236 | ie = ieee80211_next_ie ( ie, NULL ); |
---|
1237 | } |
---|
1238 | |
---|
1239 | if ( dev->nr_rates > 8 ) { |
---|
1240 | /* 802.11 requires we use an Extended Basic Rates IE |
---|
1241 | for the rates beyond the eighth. */ |
---|
1242 | |
---|
1243 | ie->id = IEEE80211_IE_EXT_RATES; |
---|
1244 | ie->len = dev->nr_rates - 8; |
---|
1245 | |
---|
1246 | for ( ; i < dev->nr_rates; i++ ) { |
---|
1247 | ie->rates[i - 8] = dev->rates[i] / 5; |
---|
1248 | if ( dev->basic_rates & ( 1 << i ) ) |
---|
1249 | ie->rates[i - 8] |= 0x80; |
---|
1250 | } |
---|
1251 | |
---|
1252 | ie = ieee80211_next_ie ( ie, NULL ); |
---|
1253 | } |
---|
1254 | |
---|
1255 | if ( dev->rsn_ie && dev->rsn_ie->id == IEEE80211_IE_VENDOR ) { |
---|
1256 | memcpy ( ie, dev->rsn_ie, dev->rsn_ie->len + 2 ); |
---|
1257 | ie = ieee80211_next_ie ( ie, NULL ); |
---|
1258 | } |
---|
1259 | |
---|
1260 | return ie; |
---|
1261 | } |
---|
1262 | |
---|
1263 | /** Seconds to wait after finding a network, to possibly find better APs for it |
---|
1264 | * |
---|
1265 | * This is used when a specific SSID to scan for is specified. |
---|
1266 | */ |
---|
1267 | #define NET80211_PROBE_GATHER 1 |
---|
1268 | |
---|
1269 | /** Seconds to wait after finding a network, to possibly find other networks |
---|
1270 | * |
---|
1271 | * This is used when an empty SSID is specified, to scan for all |
---|
1272 | * networks. |
---|
1273 | */ |
---|
1274 | #define NET80211_PROBE_GATHER_ALL 2 |
---|
1275 | |
---|
1276 | /** Seconds to allow a probe to take if no network has been found */ |
---|
1277 | #define NET80211_PROBE_TIMEOUT 6 |
---|
1278 | |
---|
1279 | /** |
---|
1280 | * Begin probe of 802.11 networks |
---|
1281 | * |
---|
1282 | * @v dev 802.11 device |
---|
1283 | * @v essid SSID to probe for, or "" to accept any (may not be NULL) |
---|
1284 | * @v active Whether to use active scanning |
---|
1285 | * @ret ctx Probe context |
---|
1286 | * |
---|
1287 | * Active scanning may only be used on channels 1-11 in the 2.4GHz |
---|
1288 | * band, due to gPXE's lack of a complete regulatory database. If |
---|
1289 | * active scanning is used, probe packets will be sent on each |
---|
1290 | * channel; this can allow association with hidden-SSID networks if |
---|
1291 | * the SSID is properly specified. |
---|
1292 | * |
---|
1293 | * A @c NULL return indicates an out-of-memory condition. |
---|
1294 | * |
---|
1295 | * The returned context must be periodically passed to |
---|
1296 | * net80211_probe_step() until that function returns zero. |
---|
1297 | */ |
---|
1298 | struct net80211_probe_ctx * net80211_probe_start ( struct net80211_device *dev, |
---|
1299 | const char *essid, |
---|
1300 | int active ) |
---|
1301 | { |
---|
1302 | struct net80211_probe_ctx *ctx = zalloc ( sizeof ( *ctx ) ); |
---|
1303 | |
---|
1304 | if ( ! ctx ) |
---|
1305 | return NULL; |
---|
1306 | |
---|
1307 | assert ( dev->netdev->state & NETDEV_OPEN ); |
---|
1308 | |
---|
1309 | ctx->dev = dev; |
---|
1310 | ctx->old_keep_mgmt = net80211_keep_mgmt ( dev, 1 ); |
---|
1311 | ctx->essid = essid; |
---|
1312 | if ( dev->essid != ctx->essid ) |
---|
1313 | strcpy ( dev->essid, ctx->essid ); |
---|
1314 | |
---|
1315 | if ( active ) { |
---|
1316 | struct ieee80211_probe_req *probe_req; |
---|
1317 | union ieee80211_ie *ie; |
---|
1318 | |
---|
1319 | ctx->probe = alloc_iob ( 128 ); |
---|
1320 | iob_reserve ( ctx->probe, IEEE80211_TYP_FRAME_HEADER_LEN ); |
---|
1321 | probe_req = ctx->probe->data; |
---|
1322 | |
---|
1323 | ie = net80211_marshal_request_info ( dev, |
---|
1324 | probe_req->info_element ); |
---|
1325 | |
---|
1326 | iob_put ( ctx->probe, ( void * ) ie - ctx->probe->data ); |
---|
1327 | } |
---|
1328 | |
---|
1329 | ctx->ticks_start = currticks(); |
---|
1330 | ctx->ticks_beacon = 0; |
---|
1331 | ctx->ticks_channel = currticks(); |
---|
1332 | ctx->hop_time = ticks_per_sec() / ( active ? 2 : 6 ); |
---|
1333 | |
---|
1334 | /* |
---|
1335 | * Channels on 2.4GHz overlap, and the most commonly used |
---|
1336 | * are 1, 6, and 11. We'll get a result faster if we check |
---|
1337 | * every 5 channels, but in order to hit all of them the |
---|
1338 | * number of channels must be relatively prime to 5. If it's |
---|
1339 | * not, tweak the hop. |
---|
1340 | */ |
---|
1341 | ctx->hop_step = 5; |
---|
1342 | while ( dev->nr_channels % ctx->hop_step == 0 && ctx->hop_step > 1 ) |
---|
1343 | ctx->hop_step--; |
---|
1344 | |
---|
1345 | ctx->beacons = malloc ( sizeof ( *ctx->beacons ) ); |
---|
1346 | INIT_LIST_HEAD ( ctx->beacons ); |
---|
1347 | |
---|
1348 | dev->channel = 0; |
---|
1349 | dev->op->config ( dev, NET80211_CFG_CHANNEL ); |
---|
1350 | |
---|
1351 | return ctx; |
---|
1352 | } |
---|
1353 | |
---|
1354 | /** |
---|
1355 | * Continue probe of 802.11 networks |
---|
1356 | * |
---|
1357 | * @v ctx Probe context returned by net80211_probe_start() |
---|
1358 | * @ret rc Probe status |
---|
1359 | * |
---|
1360 | * The return code will be 0 if the probe is still going on (and this |
---|
1361 | * function should be called again), a positive number if the probe |
---|
1362 | * completed successfully, or a negative error code if the probe |
---|
1363 | * failed for that reason. |
---|
1364 | * |
---|
1365 | * Whether the probe succeeded or failed, you must call |
---|
1366 | * net80211_probe_finish_all() or net80211_probe_finish_best() |
---|
1367 | * (depending on whether you want information on all networks or just |
---|
1368 | * the best-signal one) in order to release the probe context. A |
---|
1369 | * failed probe may still have acquired some valid data. |
---|
1370 | */ |
---|
1371 | int net80211_probe_step ( struct net80211_probe_ctx *ctx ) |
---|
1372 | { |
---|
1373 | struct net80211_device *dev = ctx->dev; |
---|
1374 | u32 start_timeout = NET80211_PROBE_TIMEOUT * ticks_per_sec(); |
---|
1375 | u32 gather_timeout = ticks_per_sec(); |
---|
1376 | u32 now = currticks(); |
---|
1377 | struct io_buffer *iob; |
---|
1378 | int signal; |
---|
1379 | int rc; |
---|
1380 | char ssid[IEEE80211_MAX_SSID_LEN + 1]; |
---|
1381 | |
---|
1382 | gather_timeout *= ( ctx->essid[0] ? NET80211_PROBE_GATHER : |
---|
1383 | NET80211_PROBE_GATHER_ALL ); |
---|
1384 | |
---|
1385 | /* Time out if necessary */ |
---|
1386 | if ( now >= ctx->ticks_start + start_timeout ) |
---|
1387 | return list_empty ( ctx->beacons ) ? -ETIMEDOUT : +1; |
---|
1388 | |
---|
1389 | if ( ctx->ticks_beacon > 0 && now >= ctx->ticks_start + gather_timeout ) |
---|
1390 | return +1; |
---|
1391 | |
---|
1392 | /* Change channels if necessary */ |
---|
1393 | if ( now >= ctx->ticks_channel + ctx->hop_time ) { |
---|
1394 | dev->channel = ( dev->channel + ctx->hop_step ) |
---|
1395 | % dev->nr_channels; |
---|
1396 | dev->op->config ( dev, NET80211_CFG_CHANNEL ); |
---|
1397 | udelay ( dev->hw->channel_change_time ); |
---|
1398 | |
---|
1399 | ctx->ticks_channel = now; |
---|
1400 | |
---|
1401 | if ( ctx->probe ) { |
---|
1402 | struct io_buffer *siob = ctx->probe; /* to send */ |
---|
1403 | |
---|
1404 | /* make a copy for future use */ |
---|
1405 | iob = alloc_iob ( siob->tail - siob->head ); |
---|
1406 | iob_reserve ( iob, iob_headroom ( siob ) ); |
---|
1407 | memcpy ( iob_put ( iob, iob_len ( siob ) ), |
---|
1408 | siob->data, iob_len ( siob ) ); |
---|
1409 | |
---|
1410 | ctx->probe = iob; |
---|
1411 | rc = net80211_tx_mgmt ( dev, IEEE80211_STYPE_PROBE_REQ, |
---|
1412 | net80211_ll_broadcast, |
---|
1413 | iob_disown ( siob ) ); |
---|
1414 | if ( rc ) { |
---|
1415 | DBGC ( dev, "802.11 %p send probe failed: " |
---|
1416 | "%s\n", dev, strerror ( rc ) ); |
---|
1417 | return rc; |
---|
1418 | } |
---|
1419 | } |
---|
1420 | } |
---|
1421 | |
---|
1422 | /* Check for new management packets */ |
---|
1423 | while ( ( iob = net80211_mgmt_dequeue ( dev, &signal ) ) != NULL ) { |
---|
1424 | struct ieee80211_frame *hdr; |
---|
1425 | struct ieee80211_beacon *beacon; |
---|
1426 | union ieee80211_ie *ie; |
---|
1427 | struct net80211_wlan *wlan; |
---|
1428 | u16 type; |
---|
1429 | |
---|
1430 | hdr = iob->data; |
---|
1431 | type = hdr->fc & IEEE80211_FC_SUBTYPE; |
---|
1432 | beacon = ( struct ieee80211_beacon * ) hdr->data; |
---|
1433 | |
---|
1434 | if ( type != IEEE80211_STYPE_BEACON && |
---|
1435 | type != IEEE80211_STYPE_PROBE_RESP ) { |
---|
1436 | DBGC2 ( dev, "802.11 %p probe: non-beacon\n", dev ); |
---|
1437 | goto drop; |
---|
1438 | } |
---|
1439 | |
---|
1440 | if ( ( void * ) beacon->info_element >= iob->tail ) { |
---|
1441 | DBGC ( dev, "802.11 %p probe: beacon with no IEs\n", |
---|
1442 | dev ); |
---|
1443 | goto drop; |
---|
1444 | } |
---|
1445 | |
---|
1446 | ie = beacon->info_element; |
---|
1447 | |
---|
1448 | if ( ! ieee80211_ie_bound ( ie, iob->tail ) ) |
---|
1449 | ie = NULL; |
---|
1450 | |
---|
1451 | while ( ie && ie->id != IEEE80211_IE_SSID ) |
---|
1452 | ie = ieee80211_next_ie ( ie, iob->tail ); |
---|
1453 | |
---|
1454 | if ( ! ie ) { |
---|
1455 | DBGC ( dev, "802.11 %p probe: beacon with no SSID\n", |
---|
1456 | dev ); |
---|
1457 | goto drop; |
---|
1458 | } |
---|
1459 | |
---|
1460 | memcpy ( ssid, ie->ssid, ie->len ); |
---|
1461 | ssid[ie->len] = 0; |
---|
1462 | |
---|
1463 | if ( ctx->essid[0] && strcmp ( ctx->essid, ssid ) != 0 ) { |
---|
1464 | DBGC2 ( dev, "802.11 %p probe: beacon with wrong SSID " |
---|
1465 | "(%s)\n", dev, ssid ); |
---|
1466 | goto drop; |
---|
1467 | } |
---|
1468 | |
---|
1469 | /* See if we've got an entry for this network */ |
---|
1470 | list_for_each_entry ( wlan, ctx->beacons, list ) { |
---|
1471 | if ( strcmp ( wlan->essid, ssid ) != 0 ) |
---|
1472 | continue; |
---|
1473 | |
---|
1474 | if ( signal < wlan->signal ) { |
---|
1475 | DBGC2 ( dev, "802.11 %p probe: beacon for %s " |
---|
1476 | "(%s) with weaker signal %d\n", dev, |
---|
1477 | ssid, eth_ntoa ( hdr->addr3 ), signal ); |
---|
1478 | goto drop; |
---|
1479 | } |
---|
1480 | |
---|
1481 | goto fill; |
---|
1482 | } |
---|
1483 | |
---|
1484 | /* No entry yet - make one */ |
---|
1485 | wlan = zalloc ( sizeof ( *wlan ) ); |
---|
1486 | strcpy ( wlan->essid, ssid ); |
---|
1487 | list_add_tail ( &wlan->list, ctx->beacons ); |
---|
1488 | |
---|
1489 | /* Whether we're using an old entry or a new one, fill |
---|
1490 | it with new data. */ |
---|
1491 | fill: |
---|
1492 | memcpy ( wlan->bssid, hdr->addr3, ETH_ALEN ); |
---|
1493 | wlan->signal = signal; |
---|
1494 | wlan->channel = dev->channels[dev->channel].channel_nr; |
---|
1495 | |
---|
1496 | /* Copy this I/O buffer into a new wlan->beacon; the |
---|
1497 | * iob we've got probably came from the device driver |
---|
1498 | * and may have the full 2.4k allocation, which we |
---|
1499 | * don't want to keep around wasting memory. |
---|
1500 | */ |
---|
1501 | free_iob ( wlan->beacon ); |
---|
1502 | wlan->beacon = alloc_iob ( iob_len ( iob ) ); |
---|
1503 | memcpy ( iob_put ( wlan->beacon, iob_len ( iob ) ), |
---|
1504 | iob->data, iob_len ( iob ) ); |
---|
1505 | |
---|
1506 | if ( ( rc = sec80211_detect ( wlan->beacon, &wlan->handshaking, |
---|
1507 | &wlan->crypto ) ) == -ENOTSUP ) { |
---|
1508 | struct ieee80211_beacon *beacon = |
---|
1509 | ( struct ieee80211_beacon * ) hdr->data; |
---|
1510 | |
---|
1511 | if ( beacon->capability & IEEE80211_CAPAB_PRIVACY ) { |
---|
1512 | DBG ( "802.11 %p probe: secured network %s but " |
---|
1513 | "encryption support not compiled in\n", |
---|
1514 | dev, wlan->essid ); |
---|
1515 | wlan->handshaking = NET80211_SECPROT_UNKNOWN; |
---|
1516 | wlan->crypto = NET80211_CRYPT_UNKNOWN; |
---|
1517 | } else { |
---|
1518 | wlan->handshaking = NET80211_SECPROT_NONE; |
---|
1519 | wlan->crypto = NET80211_CRYPT_NONE; |
---|
1520 | } |
---|
1521 | } else if ( rc != 0 ) { |
---|
1522 | DBGC ( dev, "802.11 %p probe warning: network " |
---|
1523 | "%s with unidentifiable security " |
---|
1524 | "settings: %s\n", dev, wlan->essid, |
---|
1525 | strerror ( rc ) ); |
---|
1526 | } |
---|
1527 | |
---|
1528 | ctx->ticks_beacon = now; |
---|
1529 | |
---|
1530 | DBGC2 ( dev, "802.11 %p probe: good beacon for %s (%s)\n", |
---|
1531 | dev, wlan->essid, eth_ntoa ( wlan->bssid ) ); |
---|
1532 | |
---|
1533 | drop: |
---|
1534 | free_iob ( iob ); |
---|
1535 | } |
---|
1536 | |
---|
1537 | return 0; |
---|
1538 | } |
---|
1539 | |
---|
1540 | |
---|
1541 | /** |
---|
1542 | * Finish probe of 802.11 networks, returning best-signal network found |
---|
1543 | * |
---|
1544 | * @v ctx Probe context |
---|
1545 | * @ret wlan Best-signal network found, or @c NULL if none were found |
---|
1546 | * |
---|
1547 | * If net80211_probe_start() was called with a particular SSID |
---|
1548 | * parameter as filter, only a network with that SSID (matching |
---|
1549 | * case-sensitively) can be returned from this function. |
---|
1550 | */ |
---|
1551 | struct net80211_wlan * |
---|
1552 | net80211_probe_finish_best ( struct net80211_probe_ctx *ctx ) |
---|
1553 | { |
---|
1554 | struct net80211_wlan *best = NULL, *wlan; |
---|
1555 | |
---|
1556 | if ( ! ctx ) |
---|
1557 | return NULL; |
---|
1558 | |
---|
1559 | list_for_each_entry ( wlan, ctx->beacons, list ) { |
---|
1560 | if ( ! best || best->signal < wlan->signal ) |
---|
1561 | best = wlan; |
---|
1562 | } |
---|
1563 | |
---|
1564 | if ( best ) |
---|
1565 | list_del ( &best->list ); |
---|
1566 | else |
---|
1567 | DBGC ( ctx->dev, "802.11 %p probe: found nothing for '%s'\n", |
---|
1568 | ctx->dev, ctx->essid ); |
---|
1569 | |
---|
1570 | net80211_free_wlanlist ( ctx->beacons ); |
---|
1571 | |
---|
1572 | net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt ); |
---|
1573 | |
---|
1574 | if ( ctx->probe ) |
---|
1575 | free_iob ( ctx->probe ); |
---|
1576 | |
---|
1577 | free ( ctx ); |
---|
1578 | |
---|
1579 | return best; |
---|
1580 | } |
---|
1581 | |
---|
1582 | |
---|
1583 | /** |
---|
1584 | * Finish probe of 802.11 networks, returning all networks found |
---|
1585 | * |
---|
1586 | * @v ctx Probe context |
---|
1587 | * @ret list List of net80211_wlan detailing networks found |
---|
1588 | * |
---|
1589 | * If net80211_probe_start() was called with a particular SSID |
---|
1590 | * parameter as filter, this will always return either an empty or a |
---|
1591 | * one-element list. |
---|
1592 | */ |
---|
1593 | struct list_head *net80211_probe_finish_all ( struct net80211_probe_ctx *ctx ) |
---|
1594 | { |
---|
1595 | struct list_head *beacons = ctx->beacons; |
---|
1596 | |
---|
1597 | if ( ! ctx ) |
---|
1598 | return NULL; |
---|
1599 | |
---|
1600 | net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt ); |
---|
1601 | |
---|
1602 | if ( ctx->probe ) |
---|
1603 | free_iob ( ctx->probe ); |
---|
1604 | |
---|
1605 | free ( ctx ); |
---|
1606 | |
---|
1607 | return beacons; |
---|
1608 | } |
---|
1609 | |
---|
1610 | |
---|
1611 | /** |
---|
1612 | * Free WLAN structure |
---|
1613 | * |
---|
1614 | * @v wlan WLAN structure to free |
---|
1615 | */ |
---|
1616 | void net80211_free_wlan ( struct net80211_wlan *wlan ) |
---|
1617 | { |
---|
1618 | if ( wlan ) { |
---|
1619 | free_iob ( wlan->beacon ); |
---|
1620 | free ( wlan ); |
---|
1621 | } |
---|
1622 | } |
---|
1623 | |
---|
1624 | |
---|
1625 | /** |
---|
1626 | * Free list of WLAN structures |
---|
1627 | * |
---|
1628 | * @v list List of WLAN structures to free |
---|
1629 | */ |
---|
1630 | void net80211_free_wlanlist ( struct list_head *list ) |
---|
1631 | { |
---|
1632 | struct net80211_wlan *wlan, *tmp; |
---|
1633 | |
---|
1634 | if ( ! list ) |
---|
1635 | return; |
---|
1636 | |
---|
1637 | list_for_each_entry_safe ( wlan, tmp, list, list ) { |
---|
1638 | list_del ( &wlan->list ); |
---|
1639 | net80211_free_wlan ( wlan ); |
---|
1640 | } |
---|
1641 | |
---|
1642 | free ( list ); |
---|
1643 | } |
---|
1644 | |
---|
1645 | |
---|
1646 | /** Number of ticks to wait for replies to association management frames */ |
---|
1647 | #define ASSOC_TIMEOUT TICKS_PER_SEC |
---|
1648 | |
---|
1649 | /** Number of times to try sending a particular association management frame */ |
---|
1650 | #define ASSOC_RETRIES 2 |
---|
1651 | |
---|
1652 | /** |
---|
1653 | * Step 802.11 association process |
---|
1654 | * |
---|
1655 | * @v proc Association process |
---|
1656 | */ |
---|
1657 | static void net80211_step_associate ( struct process *proc ) |
---|
1658 | { |
---|
1659 | struct net80211_device *dev = |
---|
1660 | container_of ( proc, struct net80211_device, proc_assoc ); |
---|
1661 | int rc = 0; |
---|
1662 | int status = dev->state & NET80211_STATUS_MASK; |
---|
1663 | |
---|
1664 | /* |
---|
1665 | * We use a sort of state machine implemented using bits in |
---|
1666 | * the dev->state variable. At each call, we take the |
---|
1667 | * logically first step that has not yet succeeded; either it |
---|
1668 | * has not been tried yet, it's being retried, or it failed. |
---|
1669 | * If it failed, we return an error indication; otherwise we |
---|
1670 | * perform the step. If it succeeds, RX handling code will set |
---|
1671 | * the appropriate status bit for us. |
---|
1672 | * |
---|
1673 | * Probe works a bit differently, since we have to step it |
---|
1674 | * on every call instead of waiting for a packet to arrive |
---|
1675 | * that will set the completion bit for us. |
---|
1676 | */ |
---|
1677 | |
---|
1678 | /* If we're waiting for a reply, check for timeout condition */ |
---|
1679 | if ( dev->state & NET80211_WAITING ) { |
---|
1680 | /* Sanity check */ |
---|
1681 | if ( ! dev->associating ) |
---|
1682 | return; |
---|
1683 | |
---|
1684 | if ( currticks() - dev->ctx.assoc->last_packet > ASSOC_TIMEOUT ) { |
---|
1685 | /* Timed out - fail if too many retries, or retry */ |
---|
1686 | dev->ctx.assoc->times_tried++; |
---|
1687 | if ( ++dev->ctx.assoc->times_tried > ASSOC_RETRIES ) { |
---|
1688 | rc = -ETIMEDOUT; |
---|
1689 | goto fail; |
---|
1690 | } |
---|
1691 | } else { |
---|
1692 | /* Didn't time out - let it keep going */ |
---|
1693 | return; |
---|
1694 | } |
---|
1695 | } else { |
---|
1696 | if ( dev->state & NET80211_PROBED ) |
---|
1697 | dev->ctx.assoc->times_tried = 0; |
---|
1698 | } |
---|
1699 | |
---|
1700 | if ( ! ( dev->state & NET80211_PROBED ) ) { |
---|
1701 | /* state: probe */ |
---|
1702 | |
---|
1703 | if ( ! dev->ctx.probe ) { |
---|
1704 | /* start probe */ |
---|
1705 | int active = fetch_intz_setting ( NULL, |
---|
1706 | &net80211_active_setting ); |
---|
1707 | int band = dev->hw->bands; |
---|
1708 | |
---|
1709 | if ( active ) |
---|
1710 | band &= ~NET80211_BAND_BIT_5GHZ; |
---|
1711 | |
---|
1712 | rc = net80211_prepare_probe ( dev, band, active ); |
---|
1713 | if ( rc ) |
---|
1714 | goto fail; |
---|
1715 | |
---|
1716 | dev->ctx.probe = net80211_probe_start ( dev, dev->essid, |
---|
1717 | active ); |
---|
1718 | if ( ! dev->ctx.probe ) { |
---|
1719 | dev->assoc_rc = -ENOMEM; |
---|
1720 | goto fail; |
---|
1721 | } |
---|
1722 | } |
---|
1723 | |
---|
1724 | rc = net80211_probe_step ( dev->ctx.probe ); |
---|
1725 | if ( ! rc ) { |
---|
1726 | return; /* still going */ |
---|
1727 | } |
---|
1728 | |
---|
1729 | dev->associating = net80211_probe_finish_best ( dev->ctx.probe ); |
---|
1730 | dev->ctx.probe = NULL; |
---|
1731 | if ( ! dev->associating ) { |
---|
1732 | if ( rc > 0 ) /* "successful" probe found nothing */ |
---|
1733 | rc = -ETIMEDOUT; |
---|
1734 | goto fail; |
---|
1735 | } |
---|
1736 | |
---|
1737 | /* If we probed using a broadcast SSID, record that |
---|
1738 | fact for the settings applicator before we clobber |
---|
1739 | it with the specific SSID we've chosen. */ |
---|
1740 | if ( ! dev->essid[0] ) |
---|
1741 | dev->state |= NET80211_AUTO_SSID; |
---|
1742 | |
---|
1743 | DBGC ( dev, "802.11 %p found network %s (%s)\n", dev, |
---|
1744 | dev->associating->essid, |
---|
1745 | eth_ntoa ( dev->associating->bssid ) ); |
---|
1746 | |
---|
1747 | dev->ctx.assoc = zalloc ( sizeof ( *dev->ctx.assoc ) ); |
---|
1748 | if ( ! dev->ctx.assoc ) { |
---|
1749 | rc = -ENOMEM; |
---|
1750 | goto fail; |
---|
1751 | } |
---|
1752 | |
---|
1753 | dev->state |= NET80211_PROBED; |
---|
1754 | dev->ctx.assoc->method = IEEE80211_AUTH_OPEN_SYSTEM; |
---|
1755 | |
---|
1756 | return; |
---|
1757 | } |
---|
1758 | |
---|
1759 | /* Record time of sending the packet we're about to send, for timeout */ |
---|
1760 | dev->ctx.assoc->last_packet = currticks(); |
---|
1761 | |
---|
1762 | if ( ! ( dev->state & NET80211_AUTHENTICATED ) ) { |
---|
1763 | /* state: prepare and authenticate */ |
---|
1764 | |
---|
1765 | if ( status != IEEE80211_STATUS_SUCCESS ) { |
---|
1766 | /* we tried authenticating already, but failed */ |
---|
1767 | int method = dev->ctx.assoc->method; |
---|
1768 | |
---|
1769 | if ( method == IEEE80211_AUTH_OPEN_SYSTEM && |
---|
1770 | ( status == IEEE80211_STATUS_AUTH_CHALL_INVALID || |
---|
1771 | status == IEEE80211_STATUS_AUTH_ALGO_UNSUPP ) ) { |
---|
1772 | /* Maybe this network uses Shared Key? */ |
---|
1773 | dev->ctx.assoc->method = |
---|
1774 | IEEE80211_AUTH_SHARED_KEY; |
---|
1775 | } else { |
---|
1776 | goto fail; |
---|
1777 | } |
---|
1778 | } |
---|
1779 | |
---|
1780 | DBGC ( dev, "802.11 %p authenticating with method %d\n", dev, |
---|
1781 | dev->ctx.assoc->method ); |
---|
1782 | |
---|
1783 | rc = net80211_prepare_assoc ( dev, dev->associating ); |
---|
1784 | if ( rc ) |
---|
1785 | goto fail; |
---|
1786 | |
---|
1787 | rc = net80211_send_auth ( dev, dev->associating, |
---|
1788 | dev->ctx.assoc->method ); |
---|
1789 | if ( rc ) |
---|
1790 | goto fail; |
---|
1791 | |
---|
1792 | return; |
---|
1793 | } |
---|
1794 | |
---|
1795 | if ( ! ( dev->state & NET80211_ASSOCIATED ) ) { |
---|
1796 | /* state: associate */ |
---|
1797 | |
---|
1798 | if ( status != IEEE80211_STATUS_SUCCESS ) |
---|
1799 | goto fail; |
---|
1800 | |
---|
1801 | DBGC ( dev, "802.11 %p associating\n", dev ); |
---|
1802 | |
---|
1803 | if ( dev->handshaker && dev->handshaker->start && |
---|
1804 | ! dev->handshaker->started ) { |
---|
1805 | rc = dev->handshaker->start ( dev ); |
---|
1806 | if ( rc < 0 ) |
---|
1807 | goto fail; |
---|
1808 | dev->handshaker->started = 1; |
---|
1809 | } |
---|
1810 | |
---|
1811 | rc = net80211_send_assoc ( dev, dev->associating ); |
---|
1812 | if ( rc ) |
---|
1813 | goto fail; |
---|
1814 | |
---|
1815 | return; |
---|
1816 | } |
---|
1817 | |
---|
1818 | if ( ! ( dev->state & NET80211_CRYPTO_SYNCED ) ) { |
---|
1819 | /* state: crypto sync */ |
---|
1820 | DBGC ( dev, "802.11 %p security handshaking\n", dev ); |
---|
1821 | |
---|
1822 | if ( ! dev->handshaker || ! dev->handshaker->step ) { |
---|
1823 | dev->state |= NET80211_CRYPTO_SYNCED; |
---|
1824 | return; |
---|
1825 | } |
---|
1826 | |
---|
1827 | rc = dev->handshaker->step ( dev ); |
---|
1828 | |
---|
1829 | if ( rc < 0 ) { |
---|
1830 | /* Only record the returned error if we're |
---|
1831 | still marked as associated, because an |
---|
1832 | asynchronous error will have already been |
---|
1833 | reported to net80211_deauthenticate() and |
---|
1834 | assoc_rc thereby set. */ |
---|
1835 | if ( dev->state & NET80211_ASSOCIATED ) |
---|
1836 | dev->assoc_rc = rc; |
---|
1837 | rc = 0; |
---|
1838 | goto fail; |
---|
1839 | } |
---|
1840 | |
---|
1841 | if ( rc > 0 ) { |
---|
1842 | dev->assoc_rc = 0; |
---|
1843 | dev->state |= NET80211_CRYPTO_SYNCED; |
---|
1844 | } |
---|
1845 | return; |
---|
1846 | } |
---|
1847 | |
---|
1848 | /* state: done! */ |
---|
1849 | netdev_link_up ( dev->netdev ); |
---|
1850 | dev->assoc_rc = 0; |
---|
1851 | dev->state &= ~NET80211_WORKING; |
---|
1852 | |
---|
1853 | free ( dev->ctx.assoc ); |
---|
1854 | dev->ctx.assoc = NULL; |
---|
1855 | |
---|
1856 | net80211_free_wlan ( dev->associating ); |
---|
1857 | dev->associating = NULL; |
---|
1858 | |
---|
1859 | dev->rctl = rc80211_init ( dev ); |
---|
1860 | |
---|
1861 | process_del ( proc ); |
---|
1862 | |
---|
1863 | DBGC ( dev, "802.11 %p associated with %s (%s)\n", dev, |
---|
1864 | dev->essid, eth_ntoa ( dev->bssid ) ); |
---|
1865 | |
---|
1866 | return; |
---|
1867 | |
---|
1868 | fail: |
---|
1869 | dev->state &= ~( NET80211_WORKING | NET80211_WAITING ); |
---|
1870 | if ( rc ) |
---|
1871 | dev->assoc_rc = rc; |
---|
1872 | |
---|
1873 | netdev_link_err ( dev->netdev, dev->assoc_rc ); |
---|
1874 | |
---|
1875 | /* We never reach here from the middle of a probe, so we don't |
---|
1876 | need to worry about freeing dev->ctx.probe. */ |
---|
1877 | |
---|
1878 | if ( dev->state & NET80211_PROBED ) { |
---|
1879 | free ( dev->ctx.assoc ); |
---|
1880 | dev->ctx.assoc = NULL; |
---|
1881 | } |
---|
1882 | |
---|
1883 | net80211_free_wlan ( dev->associating ); |
---|
1884 | dev->associating = NULL; |
---|
1885 | |
---|
1886 | process_del ( proc ); |
---|
1887 | |
---|
1888 | DBGC ( dev, "802.11 %p association failed (state=%04x): " |
---|
1889 | "%s\n", dev, dev->state, strerror ( dev->assoc_rc ) ); |
---|
1890 | |
---|
1891 | /* Try it again: */ |
---|
1892 | net80211_autoassociate ( dev ); |
---|
1893 | } |
---|
1894 | |
---|
1895 | /** |
---|
1896 | * Check for 802.11 SSID or key updates |
---|
1897 | * |
---|
1898 | * This acts as a settings applicator; if the user changes netX/ssid, |
---|
1899 | * and netX is currently open, the association task will be invoked |
---|
1900 | * again. If the user changes the encryption key, the current security |
---|
1901 | * handshaker will be asked to update its state to match; if that is |
---|
1902 | * impossible without reassociation, we reassociate. |
---|
1903 | */ |
---|
1904 | static int net80211_check_settings_update ( void ) |
---|
1905 | { |
---|
1906 | struct net80211_device *dev; |
---|
1907 | char ssid[IEEE80211_MAX_SSID_LEN + 1]; |
---|
1908 | int key_reassoc; |
---|
1909 | |
---|
1910 | list_for_each_entry ( dev, &net80211_devices, list ) { |
---|
1911 | if ( ! ( dev->netdev->state & NETDEV_OPEN ) ) |
---|
1912 | continue; |
---|
1913 | |
---|
1914 | key_reassoc = 0; |
---|
1915 | if ( dev->handshaker && dev->handshaker->change_key && |
---|
1916 | dev->handshaker->change_key ( dev ) < 0 ) |
---|
1917 | key_reassoc = 1; |
---|
1918 | |
---|
1919 | fetch_string_setting ( netdev_settings ( dev->netdev ), |
---|
1920 | &net80211_ssid_setting, ssid, |
---|
1921 | IEEE80211_MAX_SSID_LEN + 1 ); |
---|
1922 | |
---|
1923 | if ( key_reassoc || |
---|
1924 | ( ! ( ! ssid[0] && ( dev->state & NET80211_AUTO_SSID ) ) && |
---|
1925 | strcmp ( ssid, dev->essid ) != 0 ) ) { |
---|
1926 | DBGC ( dev, "802.11 %p updating association: " |
---|
1927 | "%s -> %s\n", dev, dev->essid, ssid ); |
---|
1928 | net80211_autoassociate ( dev ); |
---|
1929 | } |
---|
1930 | } |
---|
1931 | |
---|
1932 | return 0; |
---|
1933 | } |
---|
1934 | |
---|
1935 | /** |
---|
1936 | * Start 802.11 association process |
---|
1937 | * |
---|
1938 | * @v dev 802.11 device |
---|
1939 | * |
---|
1940 | * If the association process is running, it will be restarted. |
---|
1941 | */ |
---|
1942 | void net80211_autoassociate ( struct net80211_device *dev ) |
---|
1943 | { |
---|
1944 | if ( ! ( dev->state & NET80211_WORKING ) ) { |
---|
1945 | DBGC2 ( dev, "802.11 %p spawning association process\n", dev ); |
---|
1946 | process_add ( &dev->proc_assoc ); |
---|
1947 | } else { |
---|
1948 | DBGC2 ( dev, "802.11 %p restarting association\n", dev ); |
---|
1949 | } |
---|
1950 | |
---|
1951 | /* Clean up everything an earlier association process might |
---|
1952 | have been in the middle of using */ |
---|
1953 | if ( dev->associating ) |
---|
1954 | net80211_free_wlan ( dev->associating ); |
---|
1955 | |
---|
1956 | if ( ! ( dev->state & NET80211_PROBED ) ) |
---|
1957 | net80211_free_wlan ( |
---|
1958 | net80211_probe_finish_best ( dev->ctx.probe ) ); |
---|
1959 | else |
---|
1960 | free ( dev->ctx.assoc ); |
---|
1961 | |
---|
1962 | /* Reset to a clean state */ |
---|
1963 | fetch_string_setting ( netdev_settings ( dev->netdev ), |
---|
1964 | &net80211_ssid_setting, dev->essid, |
---|
1965 | IEEE80211_MAX_SSID_LEN + 1 ); |
---|
1966 | dev->ctx.probe = NULL; |
---|
1967 | dev->associating = NULL; |
---|
1968 | dev->assoc_rc = 0; |
---|
1969 | net80211_set_state ( dev, NET80211_PROBED, NET80211_WORKING, 0 ); |
---|
1970 | } |
---|
1971 | |
---|
1972 | /** |
---|
1973 | * Pick TX rate for RTS/CTS packets based on data rate |
---|
1974 | * |
---|
1975 | * @v dev 802.11 device |
---|
1976 | * |
---|
1977 | * The RTS/CTS rate is the fastest TX rate marked as "basic" that is |
---|
1978 | * not faster than the data rate. |
---|
1979 | */ |
---|
1980 | static void net80211_set_rtscts_rate ( struct net80211_device *dev ) |
---|
1981 | { |
---|
1982 | u16 datarate = dev->rates[dev->rate]; |
---|
1983 | u16 rtsrate = 0; |
---|
1984 | int rts_idx = -1; |
---|
1985 | int i; |
---|
1986 | |
---|
1987 | for ( i = 0; i < dev->nr_rates; i++ ) { |
---|
1988 | u16 rate = dev->rates[i]; |
---|
1989 | |
---|
1990 | if ( ! ( dev->basic_rates & ( 1 << i ) ) || rate > datarate ) |
---|
1991 | continue; |
---|
1992 | |
---|
1993 | if ( rate > rtsrate ) { |
---|
1994 | rtsrate = rate; |
---|
1995 | rts_idx = i; |
---|
1996 | } |
---|
1997 | } |
---|
1998 | |
---|
1999 | /* If this is in initialization, we might not have any basic |
---|
2000 | rates; just use the first data rate in that case. */ |
---|
2001 | if ( rts_idx < 0 ) |
---|
2002 | rts_idx = 0; |
---|
2003 | |
---|
2004 | dev->rtscts_rate = rts_idx; |
---|
2005 | } |
---|
2006 | |
---|
2007 | /** |
---|
2008 | * Set data transmission rate for 802.11 device |
---|
2009 | * |
---|
2010 | * @v dev 802.11 device |
---|
2011 | * @v rate Rate to set, as index into @c dev->rates array |
---|
2012 | */ |
---|
2013 | void net80211_set_rate_idx ( struct net80211_device *dev, int rate ) |
---|
2014 | { |
---|
2015 | assert ( dev->netdev->state & NETDEV_OPEN ); |
---|
2016 | |
---|
2017 | if ( rate >= 0 && rate < dev->nr_rates && rate != dev->rate ) { |
---|
2018 | DBGC2 ( dev, "802.11 %p changing rate from %d->%d Mbps\n", |
---|
2019 | dev, dev->rates[dev->rate] / 10, |
---|
2020 | dev->rates[rate] / 10 ); |
---|
2021 | |
---|
2022 | dev->rate = rate; |
---|
2023 | net80211_set_rtscts_rate ( dev ); |
---|
2024 | dev->op->config ( dev, NET80211_CFG_RATE ); |
---|
2025 | } |
---|
2026 | } |
---|
2027 | |
---|
2028 | /** |
---|
2029 | * Configure 802.11 device to transmit on a certain channel |
---|
2030 | * |
---|
2031 | * @v dev 802.11 device |
---|
2032 | * @v channel Channel number (1-11 for 2.4GHz) to transmit on |
---|
2033 | */ |
---|
2034 | int net80211_change_channel ( struct net80211_device *dev, int channel ) |
---|
2035 | { |
---|
2036 | int i, oldchan = dev->channel; |
---|
2037 | |
---|
2038 | assert ( dev->netdev->state & NETDEV_OPEN ); |
---|
2039 | |
---|
2040 | for ( i = 0; i < dev->nr_channels; i++ ) { |
---|
2041 | if ( dev->channels[i].channel_nr == channel ) { |
---|
2042 | dev->channel = i; |
---|
2043 | break; |
---|
2044 | } |
---|
2045 | } |
---|
2046 | |
---|
2047 | if ( i == dev->nr_channels ) |
---|
2048 | return -ENOENT; |
---|
2049 | |
---|
2050 | if ( i != oldchan ) |
---|
2051 | return dev->op->config ( dev, NET80211_CFG_CHANNEL ); |
---|
2052 | |
---|
2053 | return 0; |
---|
2054 | } |
---|
2055 | |
---|
2056 | /** |
---|
2057 | * Prepare 802.11 device channel and rate set for scanning |
---|
2058 | * |
---|
2059 | * @v dev 802.11 device |
---|
2060 | * @v band RF band(s) on which to prepare for scanning |
---|
2061 | * @v active Whether the scanning will be active |
---|
2062 | * @ret rc Return status code |
---|
2063 | */ |
---|
2064 | int net80211_prepare_probe ( struct net80211_device *dev, int band, |
---|
2065 | int active ) |
---|
2066 | { |
---|
2067 | assert ( dev->netdev->state & NETDEV_OPEN ); |
---|
2068 | |
---|
2069 | if ( active && ( band & NET80211_BAND_BIT_5GHZ ) ) { |
---|
2070 | DBGC ( dev, "802.11 %p cannot perform active scanning on " |
---|
2071 | "5GHz band\n", dev ); |
---|
2072 | return -EINVAL_ACTIVE_SCAN; |
---|
2073 | } |
---|
2074 | |
---|
2075 | if ( band == 0 ) { |
---|
2076 | /* This can happen for a 5GHz-only card with 5GHz |
---|
2077 | scanning masked out by an active request. */ |
---|
2078 | DBGC ( dev, "802.11 %p asked to prepare for scanning nothing\n", |
---|
2079 | dev ); |
---|
2080 | return -EINVAL_ACTIVE_SCAN; |
---|
2081 | } |
---|
2082 | |
---|
2083 | dev->nr_channels = 0; |
---|
2084 | |
---|
2085 | if ( active ) |
---|
2086 | net80211_add_channels ( dev, 1, 11, NET80211_REG_TXPOWER ); |
---|
2087 | else { |
---|
2088 | if ( band & NET80211_BAND_BIT_2GHZ ) |
---|
2089 | net80211_add_channels ( dev, 1, 14, |
---|
2090 | NET80211_REG_TXPOWER ); |
---|
2091 | if ( band & NET80211_BAND_BIT_5GHZ ) |
---|
2092 | net80211_add_channels ( dev, 36, 8, |
---|
2093 | NET80211_REG_TXPOWER ); |
---|
2094 | } |
---|
2095 | |
---|
2096 | net80211_filter_hw_channels ( dev ); |
---|
2097 | |
---|
2098 | /* Use channel 1 for now */ |
---|
2099 | dev->channel = 0; |
---|
2100 | dev->op->config ( dev, NET80211_CFG_CHANNEL ); |
---|
2101 | |
---|
2102 | /* Always do active probes at lowest (presumably first) speed */ |
---|
2103 | dev->rate = 0; |
---|
2104 | dev->nr_rates = 1; |
---|
2105 | dev->rates[0] = dev->hw->rates[dev->channels[0].band][0]; |
---|
2106 | dev->op->config ( dev, NET80211_CFG_RATE ); |
---|
2107 | |
---|
2108 | return 0; |
---|
2109 | } |
---|
2110 | |
---|
2111 | /** |
---|
2112 | * Prepare 802.11 device channel and rate set for communication |
---|
2113 | * |
---|
2114 | * @v dev 802.11 device |
---|
2115 | * @v wlan WLAN to prepare for communication with |
---|
2116 | * @ret rc Return status code |
---|
2117 | */ |
---|
2118 | int net80211_prepare_assoc ( struct net80211_device *dev, |
---|
2119 | struct net80211_wlan *wlan ) |
---|
2120 | { |
---|
2121 | struct ieee80211_frame *hdr = wlan->beacon->data; |
---|
2122 | struct ieee80211_beacon *beacon = |
---|
2123 | ( struct ieee80211_beacon * ) hdr->data; |
---|
2124 | struct net80211_handshaker *handshaker; |
---|
2125 | int rc; |
---|
2126 | |
---|
2127 | assert ( dev->netdev->state & NETDEV_OPEN ); |
---|
2128 | |
---|
2129 | net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 ); |
---|
2130 | memcpy ( dev->bssid, wlan->bssid, ETH_ALEN ); |
---|
2131 | strcpy ( dev->essid, wlan->essid ); |
---|
2132 | |
---|
2133 | free ( dev->rsn_ie ); |
---|
2134 | dev->rsn_ie = NULL; |
---|
2135 | |
---|
2136 | dev->last_beacon_timestamp = beacon->timestamp; |
---|
2137 | dev->tx_beacon_interval = 1024 * beacon->beacon_interval; |
---|
2138 | |
---|
2139 | /* Barring an IE that tells us the channel outright, assume |
---|
2140 | the channel we heard this AP best on is the channel it's |
---|
2141 | communicating on. */ |
---|
2142 | net80211_change_channel ( dev, wlan->channel ); |
---|
2143 | |
---|
2144 | rc = net80211_process_capab ( dev, beacon->capability ); |
---|
2145 | if ( rc ) |
---|
2146 | return rc; |
---|
2147 | |
---|
2148 | rc = net80211_process_ie ( dev, beacon->info_element, |
---|
2149 | wlan->beacon->tail ); |
---|
2150 | if ( rc ) |
---|
2151 | return rc; |
---|
2152 | |
---|
2153 | /* Associate at the lowest rate so we know it'll get through */ |
---|
2154 | dev->rate = 0; |
---|
2155 | dev->op->config ( dev, NET80211_CFG_RATE ); |
---|
2156 | |
---|
2157 | /* Free old handshaker and crypto, if they exist */ |
---|
2158 | if ( dev->handshaker && dev->handshaker->stop && |
---|
2159 | dev->handshaker->started ) |
---|
2160 | dev->handshaker->stop ( dev ); |
---|
2161 | free ( dev->handshaker ); |
---|
2162 | dev->handshaker = NULL; |
---|
2163 | free ( dev->crypto ); |
---|
2164 | free ( dev->gcrypto ); |
---|
2165 | dev->crypto = dev->gcrypto = NULL; |
---|
2166 | |
---|
2167 | /* Find new security handshaker to use */ |
---|
2168 | for_each_table_entry ( handshaker, NET80211_HANDSHAKERS ) { |
---|
2169 | if ( handshaker->protocol == wlan->handshaking ) { |
---|
2170 | dev->handshaker = zalloc ( sizeof ( *handshaker ) + |
---|
2171 | handshaker->priv_len ); |
---|
2172 | if ( ! dev->handshaker ) |
---|
2173 | return -ENOMEM; |
---|
2174 | |
---|
2175 | memcpy ( dev->handshaker, handshaker, |
---|
2176 | sizeof ( *handshaker ) ); |
---|
2177 | dev->handshaker->priv = ( ( void * ) dev->handshaker + |
---|
2178 | sizeof ( *handshaker ) ); |
---|
2179 | break; |
---|
2180 | } |
---|
2181 | } |
---|
2182 | |
---|
2183 | if ( ( wlan->handshaking != NET80211_SECPROT_NONE ) && |
---|
2184 | ! dev->handshaker ) { |
---|
2185 | DBGC ( dev, "802.11 %p no support for handshaking scheme %d\n", |
---|
2186 | dev, wlan->handshaking ); |
---|
2187 | return -( ENOTSUP | ( wlan->handshaking << 8 ) ); |
---|
2188 | } |
---|
2189 | |
---|
2190 | /* Initialize security handshaker */ |
---|
2191 | if ( dev->handshaker ) { |
---|
2192 | rc = dev->handshaker->init ( dev ); |
---|
2193 | if ( rc < 0 ) |
---|
2194 | return rc; |
---|
2195 | } |
---|
2196 | |
---|
2197 | return 0; |
---|
2198 | } |
---|
2199 | |
---|
2200 | /** |
---|
2201 | * Send 802.11 initial authentication frame |
---|
2202 | * |
---|
2203 | * @v dev 802.11 device |
---|
2204 | * @v wlan WLAN to authenticate with |
---|
2205 | * @v method Authentication method |
---|
2206 | * @ret rc Return status code |
---|
2207 | * |
---|
2208 | * @a method may be 0 for Open System authentication or 1 for Shared |
---|
2209 | * Key authentication. Open System provides no security in association |
---|
2210 | * whatsoever, relying on encryption for confidentiality, but Shared |
---|
2211 | * Key actively introduces security problems and is very rarely used. |
---|
2212 | */ |
---|
2213 | int net80211_send_auth ( struct net80211_device *dev, |
---|
2214 | struct net80211_wlan *wlan, int method ) |
---|
2215 | { |
---|
2216 | struct io_buffer *iob = alloc_iob ( 64 ); |
---|
2217 | struct ieee80211_auth *auth; |
---|
2218 | |
---|
2219 | net80211_set_state ( dev, 0, NET80211_WAITING, 0 ); |
---|
2220 | iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN ); |
---|
2221 | auth = iob_put ( iob, sizeof ( *auth ) ); |
---|
2222 | auth->algorithm = method; |
---|
2223 | auth->tx_seq = 1; |
---|
2224 | auth->status = 0; |
---|
2225 | |
---|
2226 | return net80211_tx_mgmt ( dev, IEEE80211_STYPE_AUTH, wlan->bssid, iob ); |
---|
2227 | } |
---|
2228 | |
---|
2229 | /** |
---|
2230 | * Handle receipt of 802.11 authentication frame |
---|
2231 | * |
---|
2232 | * @v dev 802.11 device |
---|
2233 | * @v iob I/O buffer |
---|
2234 | * |
---|
2235 | * If the authentication method being used is Shared Key, and the |
---|
2236 | * frame that was received included challenge text, the frame is |
---|
2237 | * encrypted using the cryptosystem currently in effect and sent back |
---|
2238 | * to the AP to complete the authentication. |
---|
2239 | */ |
---|
2240 | static void net80211_handle_auth ( struct net80211_device *dev, |
---|
2241 | struct io_buffer *iob ) |
---|
2242 | { |
---|
2243 | struct ieee80211_frame *hdr = iob->data; |
---|
2244 | struct ieee80211_auth *auth = |
---|
2245 | ( struct ieee80211_auth * ) hdr->data; |
---|
2246 | |
---|
2247 | if ( auth->tx_seq & 1 ) { |
---|
2248 | DBGC ( dev, "802.11 %p authentication received improperly " |
---|
2249 | "directed frame (seq. %d)\n", dev, auth->tx_seq ); |
---|
2250 | net80211_set_state ( dev, NET80211_WAITING, 0, |
---|
2251 | IEEE80211_STATUS_FAILURE ); |
---|
2252 | return; |
---|
2253 | } |
---|
2254 | |
---|
2255 | if ( auth->status != IEEE80211_STATUS_SUCCESS ) { |
---|
2256 | DBGC ( dev, "802.11 %p authentication failed: status %d\n", |
---|
2257 | dev, auth->status ); |
---|
2258 | net80211_set_state ( dev, NET80211_WAITING, 0, |
---|
2259 | auth->status ); |
---|
2260 | return; |
---|
2261 | } |
---|
2262 | |
---|
2263 | if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY && ! dev->crypto ) { |
---|
2264 | DBGC ( dev, "802.11 %p can't perform shared-key authentication " |
---|
2265 | "without a cryptosystem\n", dev ); |
---|
2266 | net80211_set_state ( dev, NET80211_WAITING, 0, |
---|
2267 | IEEE80211_STATUS_FAILURE ); |
---|
2268 | return; |
---|
2269 | } |
---|
2270 | |
---|
2271 | if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY && |
---|
2272 | auth->tx_seq == 2 ) { |
---|
2273 | /* Since the iob we got is going to be freed as soon |
---|
2274 | as we return, we can do some in-place |
---|
2275 | modification. */ |
---|
2276 | auth->tx_seq = 3; |
---|
2277 | auth->status = 0; |
---|
2278 | |
---|
2279 | memcpy ( hdr->addr2, hdr->addr1, ETH_ALEN ); |
---|
2280 | memcpy ( hdr->addr1, hdr->addr3, ETH_ALEN ); |
---|
2281 | |
---|
2282 | netdev_tx ( dev->netdev, |
---|
2283 | dev->crypto->encrypt ( dev->crypto, iob ) ); |
---|
2284 | return; |
---|
2285 | } |
---|
2286 | |
---|
2287 | net80211_set_state ( dev, NET80211_WAITING, NET80211_AUTHENTICATED, |
---|
2288 | IEEE80211_STATUS_SUCCESS ); |
---|
2289 | |
---|
2290 | return; |
---|
2291 | } |
---|
2292 | |
---|
2293 | /** |
---|
2294 | * Send 802.11 association frame |
---|
2295 | * |
---|
2296 | * @v dev 802.11 device |
---|
2297 | * @v wlan WLAN to associate with |
---|
2298 | * @ret rc Return status code |
---|
2299 | */ |
---|
2300 | int net80211_send_assoc ( struct net80211_device *dev, |
---|
2301 | struct net80211_wlan *wlan ) |
---|
2302 | { |
---|
2303 | struct io_buffer *iob = alloc_iob ( 128 ); |
---|
2304 | struct ieee80211_assoc_req *assoc; |
---|
2305 | union ieee80211_ie *ie; |
---|
2306 | |
---|
2307 | net80211_set_state ( dev, 0, NET80211_WAITING, 0 ); |
---|
2308 | |
---|
2309 | iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN ); |
---|
2310 | assoc = iob->data; |
---|
2311 | |
---|
2312 | assoc->capability = IEEE80211_CAPAB_MANAGED; |
---|
2313 | if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE ) ) |
---|
2314 | assoc->capability |= IEEE80211_CAPAB_SHORT_PMBL; |
---|
2315 | if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT ) ) |
---|
2316 | assoc->capability |= IEEE80211_CAPAB_SHORT_SLOT; |
---|
2317 | if ( wlan->crypto ) |
---|
2318 | assoc->capability |= IEEE80211_CAPAB_PRIVACY; |
---|
2319 | |
---|
2320 | assoc->listen_interval = 1; |
---|
2321 | |
---|
2322 | ie = net80211_marshal_request_info ( dev, assoc->info_element ); |
---|
2323 | |
---|
2324 | DBGP ( "802.11 %p about to send association request:\n", dev ); |
---|
2325 | DBGP_HD ( iob->data, ( void * ) ie - iob->data ); |
---|
2326 | |
---|
2327 | iob_put ( iob, ( void * ) ie - iob->data ); |
---|
2328 | |
---|
2329 | return net80211_tx_mgmt ( dev, IEEE80211_STYPE_ASSOC_REQ, |
---|
2330 | wlan->bssid, iob ); |
---|
2331 | } |
---|
2332 | |
---|
2333 | /** |
---|
2334 | * Handle receipt of 802.11 association reply frame |
---|
2335 | * |
---|
2336 | * @v dev 802.11 device |
---|
2337 | * @v iob I/O buffer |
---|
2338 | */ |
---|
2339 | static void net80211_handle_assoc_reply ( struct net80211_device *dev, |
---|
2340 | struct io_buffer *iob ) |
---|
2341 | { |
---|
2342 | struct ieee80211_frame *hdr = iob->data; |
---|
2343 | struct ieee80211_assoc_resp *assoc = |
---|
2344 | ( struct ieee80211_assoc_resp * ) hdr->data; |
---|
2345 | |
---|
2346 | net80211_process_capab ( dev, assoc->capability ); |
---|
2347 | net80211_process_ie ( dev, assoc->info_element, iob->tail ); |
---|
2348 | |
---|
2349 | if ( assoc->status != IEEE80211_STATUS_SUCCESS ) { |
---|
2350 | DBGC ( dev, "802.11 %p association failed: status %d\n", |
---|
2351 | dev, assoc->status ); |
---|
2352 | net80211_set_state ( dev, NET80211_WAITING, 0, |
---|
2353 | assoc->status ); |
---|
2354 | return; |
---|
2355 | } |
---|
2356 | |
---|
2357 | /* ESSID was filled before the association request was sent */ |
---|
2358 | memcpy ( dev->bssid, hdr->addr3, ETH_ALEN ); |
---|
2359 | dev->aid = assoc->aid; |
---|
2360 | |
---|
2361 | net80211_set_state ( dev, NET80211_WAITING, NET80211_ASSOCIATED, |
---|
2362 | IEEE80211_STATUS_SUCCESS ); |
---|
2363 | } |
---|
2364 | |
---|
2365 | |
---|
2366 | /** |
---|
2367 | * Send 802.11 disassociation frame |
---|
2368 | * |
---|
2369 | * @v dev 802.11 device |
---|
2370 | * @v reason Reason for disassociation |
---|
2371 | * @v deauth If TRUE, send deauthentication instead of disassociation |
---|
2372 | * @ret rc Return status code |
---|
2373 | */ |
---|
2374 | static int net80211_send_disassoc ( struct net80211_device *dev, int reason, |
---|
2375 | int deauth ) |
---|
2376 | { |
---|
2377 | struct io_buffer *iob = alloc_iob ( 64 ); |
---|
2378 | struct ieee80211_disassoc *disassoc; |
---|
2379 | |
---|
2380 | if ( ! ( dev->state & NET80211_ASSOCIATED ) ) |
---|
2381 | return -EINVAL; |
---|
2382 | |
---|
2383 | net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 ); |
---|
2384 | iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN ); |
---|
2385 | disassoc = iob_put ( iob, sizeof ( *disassoc ) ); |
---|
2386 | disassoc->reason = reason; |
---|
2387 | |
---|
2388 | return net80211_tx_mgmt ( dev, deauth ? IEEE80211_STYPE_DEAUTH : |
---|
2389 | IEEE80211_STYPE_DISASSOC, dev->bssid, iob ); |
---|
2390 | } |
---|
2391 | |
---|
2392 | |
---|
2393 | /** |
---|
2394 | * Deauthenticate from current network and try again |
---|
2395 | * |
---|
2396 | * @v dev 802.11 device |
---|
2397 | * @v rc Return status code indicating reason |
---|
2398 | * |
---|
2399 | * The deauthentication will be sent using an 802.11 "unspecified |
---|
2400 | * reason", as is common, but @a rc will be set as a link-up |
---|
2401 | * error to aid the user in debugging. |
---|
2402 | */ |
---|
2403 | void net80211_deauthenticate ( struct net80211_device *dev, int rc ) |
---|
2404 | { |
---|
2405 | net80211_send_disassoc ( dev, IEEE80211_REASON_UNSPECIFIED, 1 ); |
---|
2406 | dev->assoc_rc = rc; |
---|
2407 | netdev_link_err ( dev->netdev, rc ); |
---|
2408 | |
---|
2409 | net80211_autoassociate ( dev ); |
---|
2410 | } |
---|
2411 | |
---|
2412 | |
---|
2413 | /** Smoothing factor (1-7) for link quality calculation */ |
---|
2414 | #define LQ_SMOOTH 7 |
---|
2415 | |
---|
2416 | /** |
---|
2417 | * Update link quality information based on received beacon |
---|
2418 | * |
---|
2419 | * @v dev 802.11 device |
---|
2420 | * @v iob I/O buffer containing beacon |
---|
2421 | * @ret rc Return status code |
---|
2422 | */ |
---|
2423 | static void net80211_update_link_quality ( struct net80211_device *dev, |
---|
2424 | struct io_buffer *iob ) |
---|
2425 | { |
---|
2426 | struct ieee80211_frame *hdr = iob->data; |
---|
2427 | struct ieee80211_beacon *beacon; |
---|
2428 | u32 dt, rxi; |
---|
2429 | |
---|
2430 | if ( ! ( dev->state & NET80211_ASSOCIATED ) ) |
---|
2431 | return; |
---|
2432 | |
---|
2433 | beacon = ( struct ieee80211_beacon * ) hdr->data; |
---|
2434 | dt = ( u32 ) ( beacon->timestamp - dev->last_beacon_timestamp ); |
---|
2435 | rxi = dev->rx_beacon_interval; |
---|
2436 | |
---|
2437 | rxi = ( LQ_SMOOTH * rxi ) + ( ( 8 - LQ_SMOOTH ) * dt ); |
---|
2438 | dev->rx_beacon_interval = rxi >> 3; |
---|
2439 | |
---|
2440 | dev->last_beacon_timestamp = beacon->timestamp; |
---|
2441 | } |
---|
2442 | |
---|
2443 | |
---|
2444 | /** |
---|
2445 | * Handle receipt of 802.11 management frame |
---|
2446 | * |
---|
2447 | * @v dev 802.11 device |
---|
2448 | * @v iob I/O buffer |
---|
2449 | * @v signal Signal strength of received frame |
---|
2450 | */ |
---|
2451 | static void net80211_handle_mgmt ( struct net80211_device *dev, |
---|
2452 | struct io_buffer *iob, int signal ) |
---|
2453 | { |
---|
2454 | struct ieee80211_frame *hdr = iob->data; |
---|
2455 | struct ieee80211_disassoc *disassoc; |
---|
2456 | u16 stype = hdr->fc & IEEE80211_FC_SUBTYPE; |
---|
2457 | int keep = 0; |
---|
2458 | int is_deauth = ( stype == IEEE80211_STYPE_DEAUTH ); |
---|
2459 | |
---|
2460 | if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_MGMT ) { |
---|
2461 | free_iob ( iob ); |
---|
2462 | return; /* only handle management frames */ |
---|
2463 | } |
---|
2464 | |
---|
2465 | switch ( stype ) { |
---|
2466 | /* We reconnect on deauthentication and disassociation. */ |
---|
2467 | case IEEE80211_STYPE_DEAUTH: |
---|
2468 | case IEEE80211_STYPE_DISASSOC: |
---|
2469 | disassoc = ( struct ieee80211_disassoc * ) hdr->data; |
---|
2470 | net80211_set_state ( dev, is_deauth ? NET80211_AUTHENTICATED : |
---|
2471 | NET80211_ASSOCIATED, 0, |
---|
2472 | NET80211_IS_REASON | disassoc->reason ); |
---|
2473 | DBGC ( dev, "802.11 %p %s: reason %d\n", |
---|
2474 | dev, is_deauth ? "deauthenticated" : "disassociated", |
---|
2475 | disassoc->reason ); |
---|
2476 | |
---|
2477 | /* Try to reassociate, in case it's transient. */ |
---|
2478 | net80211_autoassociate ( dev ); |
---|
2479 | |
---|
2480 | break; |
---|
2481 | |
---|
2482 | /* We handle authentication and association. */ |
---|
2483 | case IEEE80211_STYPE_AUTH: |
---|
2484 | if ( ! ( dev->state & NET80211_AUTHENTICATED ) ) |
---|
2485 | net80211_handle_auth ( dev, iob ); |
---|
2486 | break; |
---|
2487 | |
---|
2488 | case IEEE80211_STYPE_ASSOC_RESP: |
---|
2489 | case IEEE80211_STYPE_REASSOC_RESP: |
---|
2490 | if ( ! ( dev->state & NET80211_ASSOCIATED ) ) |
---|
2491 | net80211_handle_assoc_reply ( dev, iob ); |
---|
2492 | break; |
---|
2493 | |
---|
2494 | /* We pass probes and beacons onto network scanning |
---|
2495 | code. Pass actions for future extensibility. */ |
---|
2496 | case IEEE80211_STYPE_BEACON: |
---|
2497 | net80211_update_link_quality ( dev, iob ); |
---|
2498 | /* fall through */ |
---|
2499 | case IEEE80211_STYPE_PROBE_RESP: |
---|
2500 | case IEEE80211_STYPE_ACTION: |
---|
2501 | if ( dev->keep_mgmt ) { |
---|
2502 | struct net80211_rx_info *rxinf; |
---|
2503 | rxinf = zalloc ( sizeof ( *rxinf ) ); |
---|
2504 | if ( ! rxinf ) { |
---|
2505 | DBGC ( dev, "802.11 %p out of memory\n", dev ); |
---|
2506 | break; |
---|
2507 | } |
---|
2508 | rxinf->signal = signal; |
---|
2509 | list_add_tail ( &iob->list, &dev->mgmt_queue ); |
---|
2510 | list_add_tail ( &rxinf->list, &dev->mgmt_info_queue ); |
---|
2511 | keep = 1; |
---|
2512 | } |
---|
2513 | break; |
---|
2514 | |
---|
2515 | case IEEE80211_STYPE_PROBE_REQ: |
---|
2516 | /* Some nodes send these broadcast. Ignore them. */ |
---|
2517 | break; |
---|
2518 | |
---|
2519 | case IEEE80211_STYPE_ASSOC_REQ: |
---|
2520 | case IEEE80211_STYPE_REASSOC_REQ: |
---|
2521 | /* We should never receive these, only send them. */ |
---|
2522 | DBGC ( dev, "802.11 %p received strange management request " |
---|
2523 | "(%04x)\n", dev, stype ); |
---|
2524 | break; |
---|
2525 | |
---|
2526 | default: |
---|
2527 | DBGC ( dev, "802.11 %p received unimplemented management " |
---|
2528 | "packet (%04x)\n", dev, stype ); |
---|
2529 | break; |
---|
2530 | } |
---|
2531 | |
---|
2532 | if ( ! keep ) |
---|
2533 | free_iob ( iob ); |
---|
2534 | } |
---|
2535 | |
---|
2536 | /* ---------- Packet handling functions ---------- */ |
---|
2537 | |
---|
2538 | /** |
---|
2539 | * Free buffers used by 802.11 fragment cache entry |
---|
2540 | * |
---|
2541 | * @v dev 802.11 device |
---|
2542 | * @v fcid Fragment cache entry index |
---|
2543 | * |
---|
2544 | * After this function, the referenced entry will be marked unused. |
---|
2545 | */ |
---|
2546 | static void net80211_free_frags ( struct net80211_device *dev, int fcid ) |
---|
2547 | { |
---|
2548 | int j; |
---|
2549 | struct net80211_frag_cache *frag = &dev->frags[fcid]; |
---|
2550 | |
---|
2551 | for ( j = 0; j < 16; j++ ) { |
---|
2552 | if ( frag->iob[j] ) { |
---|
2553 | free_iob ( frag->iob[j] ); |
---|
2554 | frag->iob[j] = NULL; |
---|
2555 | } |
---|
2556 | } |
---|
2557 | |
---|
2558 | frag->seqnr = 0; |
---|
2559 | frag->start_ticks = 0; |
---|
2560 | frag->in_use = 0; |
---|
2561 | } |
---|
2562 | |
---|
2563 | /** |
---|
2564 | * Accumulate 802.11 fragments into one I/O buffer |
---|
2565 | * |
---|
2566 | * @v dev 802.11 device |
---|
2567 | * @v fcid Fragment cache entry index |
---|
2568 | * @v nfrags Number of fragments received |
---|
2569 | * @v size Sum of sizes of all fragments, including headers |
---|
2570 | * @ret iob I/O buffer containing reassembled packet |
---|
2571 | * |
---|
2572 | * This function does not free the fragment buffers. |
---|
2573 | */ |
---|
2574 | static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev, |
---|
2575 | int fcid, int nfrags, int size ) |
---|
2576 | { |
---|
2577 | struct net80211_frag_cache *frag = &dev->frags[fcid]; |
---|
2578 | int hdrsize = IEEE80211_TYP_FRAME_HEADER_LEN; |
---|
2579 | int nsize = size - hdrsize * ( nfrags - 1 ); |
---|
2580 | int i; |
---|
2581 | |
---|
2582 | struct io_buffer *niob = alloc_iob ( nsize ); |
---|
2583 | struct ieee80211_frame *hdr; |
---|
2584 | |
---|
2585 | /* Add the header from the first one... */ |
---|
2586 | memcpy ( iob_put ( niob, hdrsize ), frag->iob[0]->data, hdrsize ); |
---|
2587 | |
---|
2588 | /* ... and all the data from all of them. */ |
---|
2589 | for ( i = 0; i < nfrags; i++ ) { |
---|
2590 | int len = iob_len ( frag->iob[i] ) - hdrsize; |
---|
2591 | memcpy ( iob_put ( niob, len ), |
---|
2592 | frag->iob[i]->data + hdrsize, len ); |
---|
2593 | } |
---|
2594 | |
---|
2595 | /* Turn off the fragment bit. */ |
---|
2596 | hdr = niob->data; |
---|
2597 | hdr->fc &= ~IEEE80211_FC_MORE_FRAG; |
---|
2598 | |
---|
2599 | return niob; |
---|
2600 | } |
---|
2601 | |
---|
2602 | /** |
---|
2603 | * Handle receipt of 802.11 fragment |
---|
2604 | * |
---|
2605 | * @v dev 802.11 device |
---|
2606 | * @v iob I/O buffer containing fragment |
---|
2607 | * @v signal Signal strength with which fragment was received |
---|
2608 | */ |
---|
2609 | static void net80211_rx_frag ( struct net80211_device *dev, |
---|
2610 | struct io_buffer *iob, int signal ) |
---|
2611 | { |
---|
2612 | struct ieee80211_frame *hdr = iob->data; |
---|
2613 | int fragnr = IEEE80211_FRAG ( hdr->seq ); |
---|
2614 | |
---|
2615 | if ( fragnr == 0 && ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) { |
---|
2616 | /* start a frag cache entry */ |
---|
2617 | int i, newest = -1; |
---|
2618 | u32 curr_ticks = currticks(), newest_ticks = 0; |
---|
2619 | u32 timeout = ticks_per_sec() * NET80211_FRAG_TIMEOUT; |
---|
2620 | |
---|
2621 | for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) { |
---|
2622 | if ( dev->frags[i].in_use == 0 ) |
---|
2623 | break; |
---|
2624 | |
---|
2625 | if ( dev->frags[i].start_ticks + timeout >= |
---|
2626 | curr_ticks ) { |
---|
2627 | net80211_free_frags ( dev, i ); |
---|
2628 | break; |
---|
2629 | } |
---|
2630 | |
---|
2631 | if ( dev->frags[i].start_ticks > newest_ticks ) { |
---|
2632 | newest = i; |
---|
2633 | newest_ticks = dev->frags[i].start_ticks; |
---|
2634 | } |
---|
2635 | } |
---|
2636 | |
---|
2637 | /* If we're being sent more concurrent fragmented |
---|
2638 | packets than we can handle, drop the newest so the |
---|
2639 | older ones have time to complete. */ |
---|
2640 | if ( i == NET80211_NR_CONCURRENT_FRAGS ) { |
---|
2641 | i = newest; |
---|
2642 | net80211_free_frags ( dev, i ); |
---|
2643 | } |
---|
2644 | |
---|
2645 | dev->frags[i].in_use = 1; |
---|
2646 | dev->frags[i].seqnr = IEEE80211_SEQNR ( hdr->seq ); |
---|
2647 | dev->frags[i].start_ticks = currticks(); |
---|
2648 | dev->frags[i].iob[0] = iob; |
---|
2649 | return; |
---|
2650 | } else { |
---|
2651 | int i; |
---|
2652 | for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) { |
---|
2653 | if ( dev->frags[i].in_use && dev->frags[i].seqnr == |
---|
2654 | IEEE80211_SEQNR ( hdr->seq ) ) |
---|
2655 | break; |
---|
2656 | } |
---|
2657 | if ( i == NET80211_NR_CONCURRENT_FRAGS ) { |
---|
2658 | /* Drop non-first not-in-cache fragments */ |
---|
2659 | DBGC ( dev, "802.11 %p dropped fragment fc=%04x " |
---|
2660 | "seq=%04x\n", dev, hdr->fc, hdr->seq ); |
---|
2661 | free_iob ( iob ); |
---|
2662 | return; |
---|
2663 | } |
---|
2664 | |
---|
2665 | dev->frags[i].iob[fragnr] = iob; |
---|
2666 | |
---|
2667 | if ( ! ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) { |
---|
2668 | int j, size = 0; |
---|
2669 | for ( j = 0; j < fragnr; j++ ) { |
---|
2670 | size += iob_len ( dev->frags[i].iob[j] ); |
---|
2671 | if ( dev->frags[i].iob[j] == NULL ) |
---|
2672 | break; |
---|
2673 | } |
---|
2674 | if ( j == fragnr ) { |
---|
2675 | /* We've got everything */ |
---|
2676 | struct io_buffer *niob = |
---|
2677 | net80211_accum_frags ( dev, i, fragnr, |
---|
2678 | size ); |
---|
2679 | net80211_free_frags ( dev, i ); |
---|
2680 | net80211_rx ( dev, niob, signal, 0 ); |
---|
2681 | } else { |
---|
2682 | DBGC ( dev, "802.11 %p dropping fragmented " |
---|
2683 | "packet due to out-of-order arrival, " |
---|
2684 | "fc=%04x seq=%04x\n", dev, hdr->fc, |
---|
2685 | hdr->seq ); |
---|
2686 | net80211_free_frags ( dev, i ); |
---|
2687 | } |
---|
2688 | } |
---|
2689 | } |
---|
2690 | } |
---|
2691 | |
---|
2692 | /** |
---|
2693 | * Handle receipt of 802.11 frame |
---|
2694 | * |
---|
2695 | * @v dev 802.11 device |
---|
2696 | * @v iob I/O buffer |
---|
2697 | * @v signal Received signal strength |
---|
2698 | * @v rate Bitrate at which frame was received, in 100 kbps units |
---|
2699 | * |
---|
2700 | * If the rate or signal is unknown, 0 should be passed. |
---|
2701 | */ |
---|
2702 | void net80211_rx ( struct net80211_device *dev, struct io_buffer *iob, |
---|
2703 | int signal, u16 rate ) |
---|
2704 | { |
---|
2705 | struct ieee80211_frame *hdr = iob->data; |
---|
2706 | u16 type = hdr->fc & IEEE80211_FC_TYPE; |
---|
2707 | if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION ) |
---|
2708 | goto drop; /* drop invalid-version packets */ |
---|
2709 | |
---|
2710 | if ( type == IEEE80211_TYPE_CTRL ) |
---|
2711 | goto drop; /* we don't handle control packets, |
---|
2712 | the hardware does */ |
---|
2713 | |
---|
2714 | if ( dev->last_rx_seq == hdr->seq ) |
---|
2715 | goto drop; /* avoid duplicate packet */ |
---|
2716 | dev->last_rx_seq = hdr->seq; |
---|
2717 | |
---|
2718 | if ( dev->hw->flags & NET80211_HW_RX_HAS_FCS ) { |
---|
2719 | /* discard the FCS */ |
---|
2720 | iob_unput ( iob, 4 ); |
---|
2721 | } |
---|
2722 | |
---|
2723 | /* Only decrypt packets from our BSSID, to avoid spurious errors */ |
---|
2724 | if ( ( hdr->fc & IEEE80211_FC_PROTECTED ) && |
---|
2725 | ! memcmp ( hdr->addr2, dev->bssid, ETH_ALEN ) ) { |
---|
2726 | /* Decrypt packet; record and drop if it fails */ |
---|
2727 | struct io_buffer *niob; |
---|
2728 | struct net80211_crypto *crypto = dev->crypto; |
---|
2729 | |
---|
2730 | if ( ! dev->crypto ) { |
---|
2731 | DBGC ( dev, "802.11 %p cannot decrypt packet " |
---|
2732 | "without a cryptosystem\n", dev ); |
---|
2733 | goto drop_crypt; |
---|
2734 | } |
---|
2735 | |
---|
2736 | if ( ( hdr->addr1[0] & 1 ) && dev->gcrypto ) { |
---|
2737 | /* Use group decryption if needed */ |
---|
2738 | crypto = dev->gcrypto; |
---|
2739 | } |
---|
2740 | |
---|
2741 | niob = crypto->decrypt ( crypto, iob ); |
---|
2742 | if ( ! niob ) { |
---|
2743 | DBGC ( dev, "802.11 %p decryption error\n", dev ); |
---|
2744 | goto drop_crypt; |
---|
2745 | } |
---|
2746 | free_iob ( iob ); |
---|
2747 | iob = niob; |
---|
2748 | } |
---|
2749 | |
---|
2750 | dev->last_signal = signal; |
---|
2751 | |
---|
2752 | /* Fragments go into the frag cache or get dropped. */ |
---|
2753 | if ( IEEE80211_FRAG ( hdr->seq ) != 0 |
---|
2754 | || ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) { |
---|
2755 | net80211_rx_frag ( dev, iob, signal ); |
---|
2756 | return; |
---|
2757 | } |
---|
2758 | |
---|
2759 | /* Management frames get handled, enqueued, or dropped. */ |
---|
2760 | if ( type == IEEE80211_TYPE_MGMT ) { |
---|
2761 | net80211_handle_mgmt ( dev, iob, signal ); |
---|
2762 | return; |
---|
2763 | } |
---|
2764 | |
---|
2765 | /* Data frames get dropped or sent to the net_device. */ |
---|
2766 | if ( ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA ) |
---|
2767 | goto drop; /* drop QoS, CFP, or null data packets */ |
---|
2768 | |
---|
2769 | /* Update rate-control algorithm */ |
---|
2770 | if ( dev->rctl ) |
---|
2771 | rc80211_update_rx ( dev, hdr->fc & IEEE80211_FC_RETRY, rate ); |
---|
2772 | |
---|
2773 | /* Pass packet onward */ |
---|
2774 | if ( dev->state & NET80211_ASSOCIATED ) { |
---|
2775 | netdev_rx ( dev->netdev, iob ); |
---|
2776 | return; |
---|
2777 | } |
---|
2778 | |
---|
2779 | /* No association? Drop it. */ |
---|
2780 | goto drop; |
---|
2781 | |
---|
2782 | drop_crypt: |
---|
2783 | netdev_rx_err ( dev->netdev, NULL, EINVAL_CRYPTO_REQUEST ); |
---|
2784 | drop: |
---|
2785 | DBGC2 ( dev, "802.11 %p dropped packet fc=%04x seq=%04x\n", dev, |
---|
2786 | hdr->fc, hdr->seq ); |
---|
2787 | free_iob ( iob ); |
---|
2788 | return; |
---|
2789 | } |
---|
2790 | |
---|
2791 | /** Indicate an error in receiving a packet |
---|
2792 | * |
---|
2793 | * @v dev 802.11 device |
---|
2794 | * @v iob I/O buffer with received packet, or NULL |
---|
2795 | * @v rc Error code |
---|
2796 | * |
---|
2797 | * This logs the error with the wrapping net_device, and frees iob if |
---|
2798 | * it is passed. |
---|
2799 | */ |
---|
2800 | void net80211_rx_err ( struct net80211_device *dev, |
---|
2801 | struct io_buffer *iob, int rc ) |
---|
2802 | { |
---|
2803 | netdev_rx_err ( dev->netdev, iob, rc ); |
---|
2804 | } |
---|
2805 | |
---|
2806 | /** Indicate the completed transmission of a packet |
---|
2807 | * |
---|
2808 | * @v dev 802.11 device |
---|
2809 | * @v iob I/O buffer of transmitted packet |
---|
2810 | * @v retries Number of times this packet was retransmitted |
---|
2811 | * @v rc Error code, or 0 for success |
---|
2812 | * |
---|
2813 | * This logs an error with the wrapping net_device if one occurred, |
---|
2814 | * and removes and frees the I/O buffer from its TX queue. The |
---|
2815 | * provided retry information is used to tune our transmission rate. |
---|
2816 | * |
---|
2817 | * If the packet did not need to be retransmitted because it was |
---|
2818 | * properly ACKed the first time, @a retries should be 0. |
---|
2819 | */ |
---|
2820 | void net80211_tx_complete ( struct net80211_device *dev, |
---|
2821 | struct io_buffer *iob, int retries, int rc ) |
---|
2822 | { |
---|
2823 | /* Update rate-control algorithm */ |
---|
2824 | if ( dev->rctl ) |
---|
2825 | rc80211_update_tx ( dev, retries, rc ); |
---|
2826 | |
---|
2827 | /* Pass completion onward */ |
---|
2828 | netdev_tx_complete_err ( dev->netdev, iob, rc ); |
---|
2829 | } |
---|