2 * The gPXE 802.11 MAC layer.
4 * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
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.
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.
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.
21 FILE_LICENCE ( GPL2_OR_LATER );
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/timer.h>
39 * 802.11 device management
42 /* Disambiguate the EINVAL's a bit */
43 #define EINVAL_PKT_TOO_SHORT ( EINVAL | EUNIQ_01 )
44 #define EINVAL_PKT_VERSION ( EINVAL | EUNIQ_02 )
45 #define EINVAL_PKT_NOT_DATA ( EINVAL | EUNIQ_03 )
46 #define EINVAL_PKT_NOT_FROMDS ( EINVAL | EUNIQ_04 )
47 #define EINVAL_PKT_LLC_HEADER ( EINVAL | EUNIQ_05 )
48 #define EINVAL_CRYPTO_REQUEST ( EINVAL | EUNIQ_06 )
49 #define EINVAL_ACTIVE_SCAN ( EINVAL | EUNIQ_07 )
52 * 802.11 error codes: The AP can give us a status code explaining why
53 * authentication failed, or a reason code explaining why we were
54 * deauthenticated/disassociated. These codes range from 0-63 (the
55 * field is 16 bits wide, but only up to 45 or so are defined yet; we
56 * allow up to 63 for extensibility). This is encoded into an error
59 * status & 0x1f goes here --vv--
60 * Status code 0-31: ECONNREFUSED | EUNIQ_(status & 0x1f) (0e1a6038)
61 * Status code 32-63: EHOSTUNREACH | EUNIQ_(status & 0x1f) (171a6011)
62 * Reason code 0-31: ECONNRESET | EUNIQ_(reason & 0x1f) (0f1a6039)
63 * Reason code 32-63: ENETRESET | EUNIQ_(reason & 0x1f) (271a6001)
65 * The POSIX error codes more or less convey the appropriate message
66 * (status codes occur when we can't associate at all, reason codes
67 * when we lose association unexpectedly) and let us extract the
68 * complete 802.11 error code from the rc value.
71 /** Make return status code from 802.11 status code */
72 #define E80211_STATUS( stat ) ( ((stat & 0x20)? EHOSTUNREACH : ECONNREFUSED) \
73 | ((stat & 0x1f) << 8) )
75 /** Make return status code from 802.11 reason code */
76 #define E80211_REASON( reas ) ( ((reas & 0x20)? ENETRESET : ECONNRESET) \
77 | ((reas & 0x1f) << 8) )
80 /** List of 802.11 devices */
81 static struct list_head net80211_devices = LIST_HEAD_INIT ( net80211_devices );
83 /** Set of device operations that does nothing */
84 static struct net80211_device_operations net80211_null_ops;
86 /** Information associated with a received management packet
88 * This is used to keep beacon signal strengths in a parallel queue to
89 * the beacons themselves.
91 struct net80211_rx_info {
93 struct list_head list;
96 /** Context for a probe operation */
97 struct net80211_probe_ctx {
98 /** 802.11 device to probe on */
99 struct net80211_device *dev;
101 /** Value of keep_mgmt before probe was started */
104 /** If scanning actively, pointer to probe packet to send */
105 struct io_buffer *probe;
107 /** If non-"", the ESSID to limit ourselves to */
110 /** Time probe was started */
113 /** Time last useful beacon was received */
116 /** Time channel was last changed */
119 /** Time to stay on each channel */
122 /** Channels to hop by when changing channel */
125 /** List of best beacons for each network found so far */
126 struct list_head *beacons;
129 /** Context for the association task */
130 struct net80211_assoc_ctx {
131 /** Next authentication method to try using */
134 /** Time (in ticks) of the last sent association-related packet */
137 /** Number of times we have tried sending it */
142 * @defgroup net80211_netdev Network device interface functions
145 static int net80211_netdev_open ( struct net_device *netdev );
146 static void net80211_netdev_close ( struct net_device *netdev );
147 static int net80211_netdev_transmit ( struct net_device *netdev,
148 struct io_buffer *iobuf );
149 static void net80211_netdev_poll ( struct net_device *netdev );
150 static void net80211_netdev_irq ( struct net_device *netdev, int enable );
154 * @defgroup net80211_linklayer 802.11 link-layer protocol functions
157 static int net80211_ll_push ( struct net_device *netdev,
158 struct io_buffer *iobuf, const void *ll_dest,
159 const void *ll_source, uint16_t net_proto );
160 static int net80211_ll_pull ( struct net_device *netdev,
161 struct io_buffer *iobuf, const void **ll_dest,
162 const void **ll_source, uint16_t * net_proto );
163 static int net80211_ll_mc_hash ( unsigned int af, const void *net_addr,
168 * @defgroup net80211_help 802.11 helper functions
171 static void net80211_add_channels ( struct net80211_device *dev, int start,
172 int len, int txpower );
173 static void net80211_filter_hw_channels ( struct net80211_device *dev );
174 static void net80211_set_rtscts_rate ( struct net80211_device *dev );
175 static int net80211_process_capab ( struct net80211_device *dev,
177 static int net80211_process_ie ( struct net80211_device *dev,
178 union ieee80211_ie *ie, void *ie_end );
179 static union ieee80211_ie *
180 net80211_marshal_request_info ( struct net80211_device *dev,
181 union ieee80211_ie *ie );
185 * @defgroup net80211_assoc_ll 802.11 association handling functions
188 static void net80211_step_associate ( struct process *proc );
189 static void net80211_handle_auth ( struct net80211_device *dev,
190 struct io_buffer *iob );
191 static void net80211_handle_assoc_reply ( struct net80211_device *dev,
192 struct io_buffer *iob );
193 static int net80211_send_disassoc ( struct net80211_device *dev, int reason );
194 static void net80211_handle_mgmt ( struct net80211_device *dev,
195 struct io_buffer *iob, int signal );
199 * @defgroup net80211_frag 802.11 fragment handling functions
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 );
210 * @defgroup net80211_settings 802.11 settings handlers
213 static int net80211_check_ssid_update ( void );
215 /** 802.11 settings applicator
217 * When the SSID is changed, this will cause any open devices to
220 struct settings_applicator net80211_ssid_applicator __settings_applicator = {
221 .apply = net80211_check_ssid_update,
224 /** The network name to associate with
226 * If this is blank, we scan for all networks and use the one with the
227 * greatest signal strength.
229 struct setting net80211_ssid_setting __setting = {
231 .description = "802.11 SSID (network name)",
232 .type = &setting_type_string,
235 /** Whether to use active scanning
237 * In order to associate with a hidden SSID, it's necessary to use an
238 * active scan (send probe packets). If this setting is nonzero, an
239 * active scan on the 2.4GHz band will be used to associate.
241 struct setting net80211_active_setting __setting = {
242 .name = "active-scan",
243 .description = "Use an active scan during 802.11 association",
244 .type = &setting_type_int8,
250 /* ---------- net_device wrapper ---------- */
253 * Open 802.11 device and start association
255 * @v netdev Wrapping network device
256 * @ret rc Return status code
258 * This sets up a default conservative set of channels for probing,
259 * and starts the auto-association task unless the @c
260 * NET80211_NO_ASSOC flag is set in the wrapped 802.11 device's @c
263 static int net80211_netdev_open ( struct net_device *netdev )
265 struct net80211_device *dev = netdev->priv;
268 if ( dev->op == &net80211_null_ops )
272 rc = dev->op->open ( dev );
277 if ( ! ( dev->state & NET80211_NO_ASSOC ) )
278 net80211_autoassociate ( dev );
284 * Close 802.11 device
286 * @v netdev Wrapping network device.
288 * If the association task is running, this will stop it.
290 static void net80211_netdev_close ( struct net_device *netdev )
292 struct net80211_device *dev = netdev->priv;
294 if ( dev->state & NET80211_WORKING )
295 process_del ( &dev->proc_assoc );
297 /* Send disassociation frame to AP, to be polite */
298 if ( dev->state & NET80211_ASSOCIATED )
299 net80211_send_disassoc ( dev, IEEE80211_REASON_LEAVING );
301 netdev_link_down ( netdev );
304 if ( dev->op->close )
305 dev->op->close ( dev );
309 * Transmit packet on 802.11 device
311 * @v netdev Wrapping network device
312 * @v iobuf I/O buffer
313 * @ret rc Return status code
315 * If encryption is enabled for the currently associated network, the
316 * packet will be encrypted prior to transmission.
318 static int net80211_netdev_transmit ( struct net_device *netdev,
319 struct io_buffer *iobuf )
321 struct net80211_device *dev = netdev->priv;
325 struct io_buffer *niob = dev->crypto->encrypt ( dev->crypto,
328 return -ENOMEM; /* only reason encryption could fail */
334 if ( dev->op->transmit )
335 rc = dev->op->transmit ( dev, iobuf );
341 * Poll 802.11 device for received packets and completed transmissions
343 * @v netdev Wrapping network device
345 static void net80211_netdev_poll ( struct net_device *netdev )
347 struct net80211_device *dev = netdev->priv;
350 dev->op->poll ( dev );
354 * Enable or disable interrupts for 802.11 device
356 * @v netdev Wrapping network device
357 * @v enable Whether to enable interrupts
359 static void net80211_netdev_irq ( struct net_device *netdev, int enable )
361 struct net80211_device *dev = netdev->priv;
364 dev->op->irq ( dev, enable );
367 /** Network device operations for a wrapped 802.11 device */
368 static struct net_device_operations net80211_netdev_ops = {
369 .open = net80211_netdev_open,
370 .close = net80211_netdev_close,
371 .transmit = net80211_netdev_transmit,
372 .poll = net80211_netdev_poll,
373 .irq = net80211_netdev_irq,
377 /* ---------- 802.11 link-layer protocol ---------- */
379 /** 802.11 broadcast MAC address */
380 static u8 net80211_ll_broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
383 * Determine whether a transmission rate uses ERP/OFDM
385 * @v rate Rate in 100 kbps units
386 * @ret is_erp TRUE if the rate is an ERP/OFDM rate
388 * 802.11b supports rates of 1.0, 2.0, 5.5, and 11.0 Mbps; any other
389 * rate than these on the 2.4GHz spectrum is an ERP (802.11g) rate.
391 static inline int net80211_rate_is_erp ( u16 rate )
393 if ( rate == 10 || rate == 20 || rate == 55 || rate == 110 )
400 * Calculate one frame's contribution to 802.11 duration field
402 * @v dev 802.11 device
403 * @v bytes Amount of data to calculate duration for
404 * @ret dur Duration field in microseconds
406 * To avoid multiple stations attempting to transmit at once, 802.11
407 * provides that every packet shall include a duration field
408 * specifying a length of time for which the wireless medium will be
409 * reserved after it is transmitted. The duration is measured in
410 * microseconds and is calculated with respect to the current
411 * physical-layer parameters of the 802.11 device.
413 * For an unfragmented data or management frame, or the last fragment
414 * of a fragmented frame, the duration captures only the 10 data bytes
415 * of one ACK; call once with bytes = 10.
417 * For a fragment of a data or management rame that will be followed
418 * by more fragments, the duration captures an ACK, the following
419 * fragment, and its ACK; add the results of three calls, two with
420 * bytes = 10 and one with bytes set to the next fragment's size.
422 * For an RTS control frame, the duration captures the responding CTS,
423 * the frame being sent, and its ACK; add the results of three calls,
424 * two with bytes = 10 and one with bytes set to the next frame's size
425 * (assuming unfragmented).
427 * For a CTS-to-self control frame, the duration captures the frame
428 * being protected and its ACK; add the results of two calls, one with
429 * bytes = 10 and one with bytes set to the next frame's size.
431 * No other frame types are currently supported by gPXE.
433 u16 net80211_duration ( struct net80211_device *dev, int bytes, u16 rate )
435 struct net80211_channel *chan = &dev->channels[dev->channel];
436 u32 kbps = rate * 100;
438 if ( chan->band == NET80211_BAND_5GHZ || net80211_rate_is_erp ( rate ) ) {
439 /* OFDM encoding (802.11a/g) */
440 int bits_per_symbol = ( kbps * 4 ) / 1000; /* 4us/symbol */
441 int bits = 22 + ( bytes << 3 ); /* 22-bit PLCP */
442 int symbols = ( bits + bits_per_symbol - 1 ) / bits_per_symbol;
444 return 16 + 20 + ( symbols * 4 ); /* 16us SIFS, 20us preamble */
446 /* CCK encoding (802.11b) */
447 int phy_time = 144 + 48; /* preamble + PLCP */
448 int bits = bytes << 3;
449 int data_time = ( bits * 1000 + kbps - 1 ) / kbps;
451 if ( dev->phy_flags & NET80211_PHY_USE_SHORT_PREAMBLE )
454 return 10 + phy_time + data_time; /* 10us SIFS */
459 * Add 802.11 link-layer header
461 * @v netdev Wrapping network device
462 * @v iobuf I/O buffer
463 * @v ll_dest Link-layer destination address
464 * @v ll_source Link-layer source address
465 * @v net_proto Network-layer protocol, in network byte order
466 * @ret rc Return status code
468 * This adds both the 802.11 frame header and the 802.2 LLC/SNAP
469 * header used on data packets.
471 * We also check here for state of the link that would make it invalid
472 * to send a data packet; every data packet must pass through here,
473 * and no non-data packet (e.g. management frame) should.
475 static int net80211_ll_push ( struct net_device *netdev,
476 struct io_buffer *iobuf, const void *ll_dest,
477 const void *ll_source, uint16_t net_proto )
479 struct net80211_device *dev = netdev->priv;
480 struct ieee80211_frame *hdr = iob_push ( iobuf,
481 IEEE80211_LLC_HEADER_LEN +
482 IEEE80211_TYP_FRAME_HEADER_LEN );
483 struct ieee80211_llc_snap_header *lhdr =
484 ( void * ) hdr + IEEE80211_TYP_FRAME_HEADER_LEN;
486 /* We can't send data packets if we're not associated. */
487 if ( ! netdev_link_ok ( netdev ) ) {
489 return dev->assoc_rc;
493 hdr->fc = IEEE80211_THIS_VERSION | IEEE80211_TYPE_DATA |
494 IEEE80211_STYPE_DATA | IEEE80211_FC_TODS;
496 /* We don't send fragmented frames, so duration is the time
497 for an SIFS + 10-byte ACK. */
498 hdr->duration = net80211_duration ( dev, 10, dev->rates[dev->rate] );
500 memcpy ( hdr->addr1, dev->bssid, ETH_ALEN );
501 memcpy ( hdr->addr2, ll_source, ETH_ALEN );
502 memcpy ( hdr->addr3, ll_dest, ETH_ALEN );
504 hdr->seq = IEEE80211_MAKESEQ ( ++dev->last_tx_seqnr, 0 );
506 lhdr->dsap = IEEE80211_LLC_DSAP;
507 lhdr->ssap = IEEE80211_LLC_SSAP;
508 lhdr->ctrl = IEEE80211_LLC_CTRL;
509 memset ( lhdr->oui, 0x00, 3 );
510 lhdr->ethertype = net_proto;
516 * Remove 802.11 link-layer header
518 * @v netdev Wrapping network device
519 * @v iobuf I/O buffer
520 * @ret ll_dest Link-layer destination address
521 * @ret ll_source Link-layer source
522 * @ret net_proto Network-layer protocol, in network byte order
523 * @ret rc Return status code
525 * This expects and removes both the 802.11 frame header and the 802.2
526 * LLC/SNAP header that are used on data packets.
528 static int net80211_ll_pull ( struct net_device *netdev __unused,
529 struct io_buffer *iobuf,
530 const void **ll_dest, const void **ll_source,
531 uint16_t * net_proto )
533 struct ieee80211_frame *hdr = iobuf->data;
534 struct ieee80211_llc_snap_header *lhdr =
535 ( void * ) hdr + IEEE80211_TYP_FRAME_HEADER_LEN;
537 /* Bunch of sanity checks */
538 if ( iob_len ( iobuf ) < IEEE80211_TYP_FRAME_HEADER_LEN +
539 IEEE80211_LLC_HEADER_LEN ) {
540 DBGC ( netdev->priv, "802.11 %p packet too short (%zd bytes)\n",
541 netdev->priv, iob_len ( iobuf ) );
542 return -EINVAL_PKT_TOO_SHORT;
545 if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION ) {
546 DBGC ( netdev->priv, "802.11 %p packet invalid version %04x\n",
547 netdev->priv, hdr->fc & IEEE80211_FC_VERSION );
548 return -EINVAL_PKT_VERSION;
551 if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_DATA ||
552 ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA ) {
553 DBGC ( netdev->priv, "802.11 %p packet not data/data (fc=%04x)\n",
554 netdev->priv, hdr->fc );
555 return -EINVAL_PKT_NOT_DATA;
558 if ( ( hdr->fc & ( IEEE80211_FC_TODS | IEEE80211_FC_FROMDS ) ) !=
559 IEEE80211_FC_FROMDS ) {
560 DBGC ( netdev->priv, "802.11 %p packet not from DS (fc=%04x)\n",
561 netdev->priv, hdr->fc );
562 return -EINVAL_PKT_NOT_FROMDS;
565 if ( lhdr->dsap != IEEE80211_LLC_DSAP || lhdr->ssap != IEEE80211_LLC_SSAP ||
566 lhdr->ctrl != IEEE80211_LLC_CTRL || lhdr->oui[0] || lhdr->oui[1] ||
568 DBGC ( netdev->priv, "802.11 %p LLC header is not plain EtherType "
569 "encapsulator: %02x->%02x [%02x] %02x:%02x:%02x %04x\n",
570 netdev->priv, lhdr->dsap, lhdr->ssap, lhdr->ctrl,
571 lhdr->oui[0], lhdr->oui[1], lhdr->oui[2], lhdr->ethertype );
572 return -EINVAL_PKT_LLC_HEADER;
575 iob_pull ( iobuf, sizeof ( *hdr ) + sizeof ( *lhdr ) );
577 *ll_dest = hdr->addr1;
578 *ll_source = hdr->addr3;
579 *net_proto = lhdr->ethertype;
584 * Hash 802.11 multicast address
586 * @v af Address family
587 * @v net_addr Network-layer address
588 * @ret ll_addr Filled link-layer address
589 * @ret rc Return status code
591 * Currently unimplemented.
593 static int net80211_ll_mc_hash ( unsigned int af __unused,
594 const void *net_addr __unused,
595 void *ll_addr __unused )
600 /** 802.11 link-layer protocol */
601 static struct ll_protocol net80211_ll_protocol __ll_protocol = {
603 .push = net80211_ll_push,
604 .pull = net80211_ll_pull,
606 .mc_hash = net80211_ll_mc_hash,
607 .ll_proto = htons ( ARPHRD_ETHER ), /* "encapsulated Ethernet" */
608 .ll_addr_len = ETH_ALEN,
609 .ll_header_len = IEEE80211_TYP_FRAME_HEADER_LEN +
610 IEEE80211_LLC_HEADER_LEN,
614 /* ---------- 802.11 network management API ---------- */
617 * Get 802.11 device from wrapping network device
619 * @v netdev Wrapping network device
620 * @ret dev 802.11 device wrapped by network device, or NULL
622 * Returns NULL if the network device does not wrap an 802.11 device.
624 struct net80211_device * net80211_get ( struct net_device *netdev )
626 struct net80211_device *dev;
628 list_for_each_entry ( dev, &net80211_devices, list ) {
629 if ( netdev->priv == dev )
637 * Set state of 802.11 device keeping management frames
639 * @v dev 802.11 device
640 * @v enable Whether to keep management frames
641 * @ret oldenab Whether management frames were enabled before this call
643 * If enable is TRUE, beacon, probe, and action frames will be kept
644 * and may be retrieved by calling net80211_mgmt_dequeue().
646 int net80211_keep_mgmt ( struct net80211_device *dev, int enable )
648 int oldenab = dev->keep_mgmt;
650 dev->keep_mgmt = enable;
655 * Get 802.11 management frame
657 * @v dev 802.11 device
658 * @ret signal Signal strength of returned management frame
659 * @ret iob I/O buffer, or NULL if no management frame is queued
661 * Frames will only be returned by this function if
662 * net80211_keep_mgmt() has been previously called with enable set to
665 * The calling function takes ownership of the returned I/O buffer.
667 struct io_buffer * net80211_mgmt_dequeue ( struct net80211_device *dev,
670 struct io_buffer *iobuf;
671 struct net80211_rx_info *rxi;
673 list_for_each_entry ( rxi, &dev->mgmt_info_queue, list ) {
674 list_del ( &rxi->list );
676 *signal = rxi->signal;
679 list_for_each_entry ( iobuf, &dev->mgmt_queue, list ) {
680 list_del ( &iobuf->list );
690 * Transmit 802.11 management frame
692 * @v dev 802.11 device
693 * @v fc Frame Control flags for management frame
694 * @v dest Destination access point
696 * @ret rc Return status code
698 * The @a fc argument must contain at least an IEEE 802.11 management
699 * subtype number (e.g. IEEE80211_STYPE_PROBE_REQ). If it contains
700 * IEEE80211_FC_PROTECTED, the frame will be encrypted prior to
703 * It is required that @a iob have at least 24 bytes of headroom
704 * reserved before its data start.
706 int net80211_tx_mgmt ( struct net80211_device *dev, u16 fc, u8 dest[6],
707 struct io_buffer *iob )
709 struct ieee80211_frame *hdr = iob_push ( iob,
710 IEEE80211_TYP_FRAME_HEADER_LEN );
712 hdr->fc = IEEE80211_THIS_VERSION | IEEE80211_TYPE_MGMT |
713 ( fc & ~IEEE80211_FC_PROTECTED );
714 hdr->duration = net80211_duration ( dev, 10, dev->rates[dev->rate] );
715 hdr->seq = IEEE80211_MAKESEQ ( ++dev->last_tx_seqnr, 0 );
717 memcpy ( hdr->addr1, dest, ETH_ALEN ); /* DA = RA */
718 memcpy ( hdr->addr2, dev->netdev->ll_addr, ETH_ALEN ); /* SA = TA */
719 memcpy ( hdr->addr3, dest, ETH_ALEN ); /* BSSID */
721 if ( fc & IEEE80211_FC_PROTECTED ) {
723 return -EINVAL_CRYPTO_REQUEST;
725 struct io_buffer *eiob = dev->crypto->encrypt ( dev->crypto,
731 return netdev_tx ( dev->netdev, iob );
735 /* ---------- Driver API ---------- */
738 * Allocate 802.11 device
740 * @v priv_size Size of driver-private allocation area
741 * @ret dev Newly allocated 802.11 device
743 * This function allocates a net_device with space in its private area
744 * for both the net80211_device it will wrap and the driver-private
745 * data space requested. It initializes the link-layer-specific parts
746 * of the net_device, and links the net80211_device to the net_device
749 struct net80211_device * net80211_alloc ( size_t priv_size )
751 struct net80211_device *dev;
752 struct net_device *netdev =
753 alloc_netdev ( sizeof ( *dev ) + priv_size );
758 netdev->ll_protocol = &net80211_ll_protocol;
759 netdev->ll_broadcast = net80211_ll_broadcast;
760 netdev->max_pkt_len = IEEE80211_MAX_DATA_LEN;
761 netdev_init ( netdev, &net80211_netdev_ops );
764 dev->netdev = netdev;
765 dev->priv = ( u8 * ) dev + sizeof ( *dev );
766 dev->op = &net80211_null_ops;
768 dev->proc_assoc.step = net80211_step_associate;
769 INIT_LIST_HEAD ( &dev->mgmt_queue );
770 INIT_LIST_HEAD ( &dev->mgmt_info_queue );
776 * Register 802.11 device with network stack
778 * @v dev 802.11 device
779 * @v ops 802.11 device operations
780 * @v hw 802.11 hardware information
782 * This also registers the wrapping net_device with the higher network
785 int net80211_register ( struct net80211_device *dev,
786 struct net80211_device_operations *ops,
787 struct net80211_hw_info *hw )
790 dev->hw = malloc ( sizeof ( *hw ) );
794 memcpy ( dev->hw, hw, sizeof ( *hw ) );
795 memcpy ( dev->netdev->ll_addr, hw->hwaddr, ETH_ALEN );
797 /* Set some sensible channel defaults for driver's open() function */
798 memcpy ( dev->channels, dev->hw->channels,
799 NET80211_MAX_CHANNELS * sizeof ( dev->channels[0] ) );
802 list_add_tail ( &dev->list, &net80211_devices );
803 return register_netdev ( dev->netdev );
807 * Unregister 802.11 device from network stack
809 * @v dev 802.11 device
811 * After this call, the device operations are cleared so that they
812 * will not be called.
814 void net80211_unregister ( struct net80211_device *dev )
816 unregister_netdev ( dev->netdev );
817 list_del ( &dev->list );
818 dev->op = &net80211_null_ops;
824 * @v dev 802.11 device
826 * The device should be unregistered before this function is called.
828 void net80211_free ( struct net80211_device *dev )
831 rc80211_free ( dev->rctl );
832 netdev_nullify ( dev->netdev );
833 netdev_put ( dev->netdev );
837 /* ---------- 802.11 network management workhorse code ---------- */
840 * Set state of 802.11 device
842 * @v dev 802.11 device
843 * @v clear Bitmask of flags to clear
844 * @v set Bitmask of flags to set
845 * @v status Status or reason code for most recent operation
847 * If @a status represents a reason code, it should be OR'ed with
848 * NET80211_IS_REASON.
850 * Clearing authentication also clears association; clearing
851 * association also clears security handshaking state. Clearing
852 * association removes the link-up flag from the wrapping net_device,
853 * but setting it does not automatically set the flag; that is left to
854 * the judgment of higher-level code.
856 static inline void net80211_set_state ( struct net80211_device *dev,
857 short clear, short set,
860 /* The conditions in this function are deliberately formulated
861 to be decidable at compile-time in most cases. Since clear
862 and set are generally passed as constants, the body of this
863 function can be reduced down to a few statements by the
866 const int statmsk = NET80211_STATUS_MASK | NET80211_IS_REASON;
868 if ( clear & NET80211_PROBED )
869 clear |= NET80211_AUTHENTICATED;
871 if ( clear & NET80211_AUTHENTICATED )
872 clear |= NET80211_ASSOCIATED;
874 if ( clear & NET80211_ASSOCIATED )
875 clear |= NET80211_CRYPTO_SYNCED;
877 dev->state = ( dev->state & ~clear ) | set;
878 dev->state = ( dev->state & ~statmsk ) | ( status & statmsk );
880 if ( clear & NET80211_ASSOCIATED )
881 netdev_link_down ( dev->netdev );
883 if ( ( clear | set ) & NET80211_ASSOCIATED )
884 dev->op->config ( dev, NET80211_CFG_ASSOC );
887 if ( status & NET80211_IS_REASON )
888 dev->assoc_rc = -E80211_REASON ( status );
890 dev->assoc_rc = -E80211_STATUS ( status );
891 netdev_link_err ( dev->netdev, dev->assoc_rc );
896 * Add channels to 802.11 device
898 * @v dev 802.11 device
899 * @v start First channel number to add
900 * @v len Number of channels to add
901 * @v txpower TX power (dBm) to allow on added channels
903 * To replace the current list of channels instead of adding to it,
904 * set the nr_channels field of the 802.11 device to 0 before calling
907 static void net80211_add_channels ( struct net80211_device *dev, int start,
908 int len, int txpower )
912 for ( i = dev->nr_channels; len-- && i < NET80211_MAX_CHANNELS; i++ ) {
913 dev->channels[i].channel_nr = chan;
914 dev->channels[i].maxpower = txpower;
915 dev->channels[i].hw_value = 0;
917 if ( chan >= 1 && chan <= 14 ) {
918 dev->channels[i].band = NET80211_BAND_2GHZ;
920 dev->channels[i].center_freq = 2484;
922 dev->channels[i].center_freq = 2407 + 5 * chan;
925 dev->channels[i].band = NET80211_BAND_5GHZ;
926 dev->channels[i].center_freq = 5000 + 5 * chan;
931 dev->nr_channels = i;
935 * Filter 802.11 device channels for hardware capabilities
937 * @v dev 802.11 device
939 * Hardware may support fewer channels than regulatory restrictions
940 * allow; this function filters out channels in dev->channels that are
941 * not supported by the hardware list in dev->hwinfo. It also copies
942 * over the net80211_channel::hw_value and limits maximum TX power
945 * Channels are matched based on center frequency, ignoring band and
948 * If the driver specifies no supported channels, the effect will be
949 * as though all were supported.
951 static void net80211_filter_hw_channels ( struct net80211_device *dev )
953 int delta = 0, i = 0;
954 int old_freq = dev->channels[dev->channel].center_freq;
955 struct net80211_channel *chan, *hwchan;
957 if ( ! dev->hw->nr_channels )
961 for ( chan = dev->channels; chan < dev->channels + dev->nr_channels;
964 for ( hwchan = dev->hw->channels;
965 hwchan < dev->hw->channels + dev->hw->nr_channels;
967 if ( hwchan->center_freq == chan->center_freq ) {
976 chan->hw_value = hwchan->hw_value;
977 if ( hwchan->maxpower != 0 &&
978 chan->maxpower > hwchan->maxpower )
979 chan->maxpower = hwchan->maxpower;
980 if ( old_freq == chan->center_freq )
981 dev->channel = i - delta;
983 chan[-delta] = *chan;
987 dev->nr_channels -= delta;
989 if ( dev->channels[dev->channel].center_freq != old_freq )
990 dev->op->config ( dev, NET80211_CFG_CHANNEL );
994 * Update 802.11 device state to reflect received capabilities field
996 * @v dev 802.11 device
997 * @v capab Capabilities field in beacon, probe, or association frame
998 * @ret rc Return status code
1000 static int net80211_process_capab ( struct net80211_device *dev,
1003 u16 old_phy = dev->phy_flags;
1005 if ( ( capab & ( IEEE80211_CAPAB_MANAGED | IEEE80211_CAPAB_ADHOC ) ) !=
1006 IEEE80211_CAPAB_MANAGED ) {
1007 DBGC ( dev, "802.11 %p cannot handle IBSS network\n", dev );
1011 if ( capab & IEEE80211_CAPAB_SPECTRUM_MGMT ) {
1012 DBGC ( dev, "802.11 %p cannot handle spectrum managed "
1017 dev->phy_flags &= ~( NET80211_PHY_USE_SHORT_PREAMBLE |
1018 NET80211_PHY_USE_SHORT_SLOT );
1020 if ( capab & IEEE80211_CAPAB_SHORT_PMBL )
1021 dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE;
1023 if ( capab & IEEE80211_CAPAB_SHORT_SLOT )
1024 dev->phy_flags |= NET80211_PHY_USE_SHORT_SLOT;
1026 if ( old_phy != dev->phy_flags )
1027 dev->op->config ( dev, NET80211_CFG_PHY_PARAMS );
1033 * Update 802.11 device state to reflect received information elements
1035 * @v dev 802.11 device
1036 * @v ie Pointer to first information element
1037 * @v ie_end Pointer to tail of packet I/O buffer
1038 * @ret rc Return status code
1040 static int net80211_process_ie ( struct net80211_device *dev,
1041 union ieee80211_ie *ie, void *ie_end )
1043 u16 old_rate = dev->rates[dev->rate];
1044 u16 old_phy = dev->phy_flags;
1045 int have_rates = 0, i;
1048 int band = dev->channels[dev->channel].band;
1050 if ( ( void * ) ie >= ie_end )
1053 for ( ; ie; ie = ieee80211_next_ie ( ie, ie_end ) ) {
1055 case IEEE80211_IE_SSID:
1056 if ( ie->len <= 32 ) {
1057 memcpy ( dev->essid, ie->ssid, ie->len );
1058 dev->essid[ie->len] = 0;
1062 case IEEE80211_IE_RATES:
1063 case IEEE80211_IE_EXT_RATES:
1064 if ( ! have_rates ) {
1066 dev->basic_rates = 0;
1069 for ( i = 0; i < ie->len &&
1070 dev->nr_rates < NET80211_MAX_RATES; i++ ) {
1071 u8 rid = ie->rates[i];
1072 u16 rate = ( rid & 0x7f ) * 5;
1076 ( 1 << dev->nr_rates );
1078 dev->rates[dev->nr_rates++] = rate;
1083 case IEEE80211_IE_DS_PARAM:
1084 if ( dev->channel < dev->nr_channels && ds_channel ==
1085 dev->channels[dev->channel].channel_nr )
1087 ds_channel = ie->ds_param.current_channel;
1088 net80211_change_channel ( dev, ds_channel );
1091 case IEEE80211_IE_COUNTRY:
1092 dev->nr_channels = 0;
1094 DBGC ( dev, "802.11 %p setting country regulations "
1095 "for %c%c\n", dev, ie->country.name[0],
1096 ie->country.name[1] );
1097 for ( i = 0; i < ( ie->len - 3 ) / 3; i++ ) {
1098 union ieee80211_ie_country_triplet *t =
1099 &ie->country.triplet[i];
1100 if ( t->first > 200 ) {
1101 DBGC ( dev, "802.11 %p ignoring regulatory "
1102 "extension information\n", dev );
1104 net80211_add_channels ( dev,
1105 t->band.first_channel,
1106 t->band.nr_channels,
1107 t->band.max_txpower );
1110 net80211_filter_hw_channels ( dev );
1113 case IEEE80211_IE_ERP_INFO:
1114 dev->phy_flags &= ~( NET80211_PHY_USE_PROTECTION |
1115 NET80211_PHY_USE_SHORT_PREAMBLE );
1116 if ( ie->erp_info & IEEE80211_ERP_USE_PROTECTION )
1117 dev->phy_flags |= NET80211_PHY_USE_PROTECTION;
1118 if ( ! ( ie->erp_info & IEEE80211_ERP_BARKER_LONG ) )
1119 dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE;
1122 case IEEE80211_IE_RSN:
1123 /* XXX need to implement WPA stuff */
1129 /* Allow only those rates that are also supported by
1134 for ( i = 0; i < dev->nr_rates; i++ ) {
1136 for ( j = 0; j < dev->hw->nr_rates[band]; j++ ) {
1137 if ( dev->hw->rates[band][j] == dev->rates[i] ){
1146 dev->rates[i - delta] = dev->rates[i];
1147 if ( old_rate == dev->rates[i] )
1148 dev->rate = i - delta;
1152 dev->nr_rates -= delta;
1154 /* Sort available rates - sorted subclumps tend to already
1155 exist, so insertion sort works well. */
1156 for ( i = 1; i < dev->nr_rates; i++ ) {
1157 u16 rate = dev->rates[i];
1159 for ( j = i - 1; j >= 0 && dev->rates[j] >= rate; j-- )
1160 dev->rates[j + 1] = dev->rates[j];
1161 dev->rates[j + 1] = rate;
1164 net80211_set_rtscts_rate ( dev );
1166 if ( dev->rates[dev->rate] != old_rate )
1167 changed |= NET80211_CFG_RATE;
1170 if ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE )
1171 dev->phy_flags &= ~NET80211_PHY_USE_SHORT_PREAMBLE;
1172 if ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT )
1173 dev->phy_flags &= ~NET80211_PHY_USE_SHORT_SLOT;
1175 if ( old_phy != dev->phy_flags )
1176 changed |= NET80211_CFG_PHY_PARAMS;
1179 dev->op->config ( dev, changed );
1185 * Create information elements for outgoing probe or association packet
1187 * @v dev 802.11 device
1188 * @v ie Pointer to start of information element area
1189 * @ret next_ie Pointer to first byte after added information elements
1191 static union ieee80211_ie *
1192 net80211_marshal_request_info ( struct net80211_device *dev,
1193 union ieee80211_ie *ie )
1197 ie->id = IEEE80211_IE_SSID;
1198 ie->len = strlen ( dev->essid );
1199 memcpy ( ie->ssid, dev->essid, ie->len );
1201 ie = ieee80211_next_ie ( ie, NULL );
1203 ie->id = IEEE80211_IE_RATES;
1204 ie->len = dev->nr_rates;
1205 for ( i = 0; i < ie->len; i++ ) {
1206 ie->rates[i] = dev->rates[i] / 5;
1207 if ( dev->basic_rates & ( 1 << i ) )
1208 ie->rates[i] |= 0x80;
1211 if ( ie->len > 8 ) {
1212 /* 802.11 requires we use an Extended Basic Rates IE
1213 for the rates beyond the eighth. */
1214 int rates = ie->len;
1216 memmove ( ( void * ) ie + 2 + 8 + 2, ( void * ) ie + 2 + 8,
1220 ie = ieee80211_next_ie ( ie, NULL );
1222 ie->id = IEEE80211_IE_EXT_RATES;
1223 ie->len = rates - 8;
1226 ie = ieee80211_next_ie ( ie, NULL );
1231 /** Seconds to wait after finding a network, to possibly find better APs for it
1233 * This is used when a specific SSID to scan for is specified.
1235 #define NET80211_PROBE_GATHER 1
1237 /** Seconds to wait after finding a network, to possibly find other networks
1239 * This is used when an empty SSID is specified, to scan for all
1242 #define NET80211_PROBE_GATHER_ALL 2
1244 /** Seconds to allow a probe to take if no network has been found */
1245 #define NET80211_PROBE_TIMEOUT 6
1248 * Begin probe of 802.11 networks
1250 * @v dev 802.11 device
1251 * @v essid SSID to probe for, or "" to accept any (may not be NULL)
1252 * @v active Whether to use active scanning
1253 * @ret ctx Probe context
1255 * Active scanning may only be used on channels 1-11 in the 2.4GHz
1256 * band, due to gPXE's lack of a complete regulatory database. If
1257 * active scanning is used, probe packets will be sent on each
1258 * channel; this can allow association with hidden-SSID networks if
1259 * the SSID is properly specified.
1261 * A @c NULL return indicates an out-of-memory condition.
1263 * The returned context must be periodically passed to
1264 * net80211_probe_step() until that function returns zero.
1266 struct net80211_probe_ctx * net80211_probe_start ( struct net80211_device *dev,
1270 struct net80211_probe_ctx *ctx = zalloc ( sizeof ( *ctx ) );
1275 assert ( dev->netdev->state & NETDEV_OPEN );
1278 ctx->old_keep_mgmt = net80211_keep_mgmt ( dev, 1 );
1280 if ( dev->essid != ctx->essid )
1281 strcpy ( dev->essid, ctx->essid );
1284 struct ieee80211_probe_req *probe_req;
1285 union ieee80211_ie *ie;
1287 ctx->probe = alloc_iob ( 128 );
1288 iob_reserve ( ctx->probe, IEEE80211_TYP_FRAME_HEADER_LEN );
1289 probe_req = ctx->probe->data;
1291 ie = net80211_marshal_request_info ( dev,
1292 probe_req->info_element );
1293 ie->id = IEEE80211_IE_REQUEST;
1295 ie->request[0] = IEEE80211_IE_COUNTRY;
1296 ie->request[1] = IEEE80211_IE_ERP_INFO;
1297 ie->request[2] = IEEE80211_IE_RSN;
1299 ie = ieee80211_next_ie ( ie, NULL );
1301 iob_put ( ctx->probe, ( void * ) ie - ctx->probe->data );
1304 ctx->ticks_start = currticks();
1305 ctx->ticks_beacon = 0;
1306 ctx->ticks_channel = currticks();
1307 ctx->hop_time = ticks_per_sec() / ( active ? 2 : 6 );
1310 * Channels on 2.4GHz overlap, and the most commonly used
1311 * are 1, 6, and 11. We'll get a result faster if we check
1312 * every 5 channels, but in order to hit all of them the
1313 * number of channels must be relatively prime to 5. If it's
1314 * not, tweak the hop.
1317 while ( dev->nr_channels % ctx->hop_step == 0 && ctx->hop_step > 1 )
1320 ctx->beacons = malloc ( sizeof ( *ctx->beacons ) );
1321 INIT_LIST_HEAD ( ctx->beacons );
1324 dev->op->config ( dev, NET80211_CFG_CHANNEL );
1330 * Continue probe of 802.11 networks
1332 * @v ctx Probe context returned by net80211_probe_start()
1333 * @ret rc Probe status
1335 * The return code will be 0 if the probe is still going on (and this
1336 * function should be called again), a positive number if the probe
1337 * completed successfully, or a negative error code if the probe
1338 * failed for that reason.
1340 * Whether the probe succeeded or failed, you must call
1341 * net80211_probe_finish_all() or net80211_probe_finish_best()
1342 * (depending on whether you want information on all networks or just
1343 * the best-signal one) in order to release the probe context. A
1344 * failed probe may still have acquired some valid data.
1346 int net80211_probe_step ( struct net80211_probe_ctx *ctx )
1348 struct net80211_device *dev = ctx->dev;
1349 u32 start_timeout = NET80211_PROBE_TIMEOUT * ticks_per_sec();
1350 u32 gather_timeout = ticks_per_sec();
1351 u32 now = currticks();
1352 struct io_buffer *iob;
1355 char ssid[IEEE80211_MAX_SSID_LEN + 1];
1357 gather_timeout *= ( ctx->essid[0] ? NET80211_PROBE_GATHER :
1358 NET80211_PROBE_GATHER_ALL );
1360 /* Time out if necessary */
1361 if ( now >= ctx->ticks_start + start_timeout )
1362 return list_empty ( ctx->beacons ) ? -ETIMEDOUT : +1;
1364 if ( ctx->ticks_beacon > 0 && now >= ctx->ticks_start + gather_timeout )
1367 /* Change channels if necessary */
1368 if ( now >= ctx->ticks_channel + ctx->hop_time ) {
1369 dev->channel = ( dev->channel + ctx->hop_step )
1371 dev->op->config ( dev, NET80211_CFG_CHANNEL );
1372 udelay ( dev->hw->channel_change_time );
1374 ctx->ticks_channel = now;
1377 struct io_buffer *siob = ctx->probe; /* to send */
1379 /* make a copy for future use */
1380 iob = alloc_iob ( siob->tail - siob->head );
1381 iob_reserve ( iob, iob_headroom ( siob ) );
1382 memcpy ( iob_put ( iob, iob_len ( siob ) ),
1383 siob->data, iob_len ( siob ) );
1386 rc = net80211_tx_mgmt ( dev, IEEE80211_STYPE_PROBE_REQ,
1387 net80211_ll_broadcast,
1388 iob_disown ( siob ) );
1390 DBGC ( dev, "802.11 %p send probe failed: "
1391 "%s\n", dev, strerror ( rc ) );
1397 /* Check for new management packets */
1398 while ( ( iob = net80211_mgmt_dequeue ( dev, &signal ) ) != NULL ) {
1399 struct ieee80211_frame *hdr;
1400 struct ieee80211_beacon *beacon;
1401 union ieee80211_ie *ie;
1402 struct net80211_wlan *wlan;
1406 type = hdr->fc & IEEE80211_FC_SUBTYPE;
1407 beacon = ( struct ieee80211_beacon * ) hdr->data;
1409 if ( type != IEEE80211_STYPE_BEACON &&
1410 type != IEEE80211_STYPE_PROBE_RESP ) {
1411 DBGC2 ( dev, "802.11 %p probe: non-beacon\n", dev );
1415 if ( ( void * ) beacon->info_element >= iob->tail ) {
1416 DBGC ( dev, "802.11 %p probe: beacon with no IEs\n",
1421 ie = beacon->info_element;
1422 while ( ie && ie->id != IEEE80211_IE_SSID )
1423 ie = ieee80211_next_ie ( ie, iob->tail );
1426 DBGC ( dev, "802.11 %p probe: beacon with no SSID\n",
1431 memcpy ( ssid, ie->ssid, ie->len );
1434 if ( ctx->essid[0] && strcmp ( ctx->essid, ssid ) != 0 ) {
1435 DBGC2 ( dev, "802.11 %p probe: beacon with wrong SSID "
1436 "(%s)\n", dev, ssid );
1440 /* See if we've got an entry for this network */
1441 list_for_each_entry ( wlan, ctx->beacons, list ) {
1442 if ( strcmp ( wlan->essid, ssid ) != 0 )
1445 if ( signal < wlan->signal ) {
1446 DBGC2 ( dev, "802.11 %p probe: beacon for %s "
1447 "(%s) with weaker signal %d\n", dev,
1448 ssid, eth_ntoa ( hdr->addr3 ), signal );
1455 /* No entry yet - make one */
1456 wlan = zalloc ( sizeof ( *wlan ) );
1457 strcpy ( wlan->essid, ssid );
1458 list_add_tail ( &wlan->list, ctx->beacons );
1460 /* Whether we're using an old entry or a new one, fill
1461 it with new data. */
1463 memcpy ( wlan->bssid, hdr->addr3, ETH_ALEN );
1464 wlan->signal = signal;
1465 wlan->channel = dev->channels[dev->channel].channel_nr;
1467 /* Copy this I/O buffer into a new wlan->beacon; the
1468 * iob we've got probably came from the device driver
1469 * and may have the full 2.4k allocation, which we
1470 * don't want to keep around wasting memory.
1472 free_iob ( wlan->beacon );
1473 wlan->beacon = alloc_iob ( iob_len ( iob ) );
1474 memcpy ( iob_put ( wlan->beacon, iob_len ( iob ) ),
1475 iob->data, iob_len ( iob ) );
1477 /* XXX actually check capab and RSN ie to
1479 wlan->handshaking = NET80211_SECPROT_NONE;
1480 wlan->crypto = NET80211_CRYPT_NONE;
1482 ctx->ticks_beacon = now;
1484 DBGC2 ( dev, "802.11 %p probe: good beacon for %s (%s)\n",
1485 dev, wlan->essid, eth_ntoa ( wlan->bssid ) );
1496 * Finish probe of 802.11 networks, returning best-signal network found
1498 * @v ctx Probe context
1499 * @ret wlan Best-signal network found, or @c NULL if none were found
1501 * If net80211_probe_start() was called with a particular SSID
1502 * parameter as filter, only a network with that SSID (matching
1503 * case-sensitively) can be returned from this function.
1505 struct net80211_wlan *
1506 net80211_probe_finish_best ( struct net80211_probe_ctx *ctx )
1508 struct net80211_wlan *best = NULL, *wlan;
1513 list_for_each_entry ( wlan, ctx->beacons, list ) {
1514 if ( ! best || best->signal < wlan->signal )
1519 list_del ( &best->list );
1521 DBGC ( ctx->dev, "802.11 %p probe: found nothing for '%s'\n",
1522 ctx->dev, ctx->essid );
1524 net80211_free_wlanlist ( ctx->beacons );
1526 net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
1529 free_iob ( ctx->probe );
1538 * Finish probe of 802.11 networks, returning all networks found
1540 * @v ctx Probe context
1541 * @ret list List of net80211_wlan detailing networks found
1543 * If net80211_probe_start() was called with a particular SSID
1544 * parameter as filter, this will always return either an empty or a
1547 struct list_head *net80211_probe_finish_all ( struct net80211_probe_ctx *ctx )
1549 struct list_head *beacons = ctx->beacons;
1554 net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
1557 free_iob ( ctx->probe );
1566 * Free WLAN structure
1568 * @v wlan WLAN structure to free
1570 void net80211_free_wlan ( struct net80211_wlan *wlan )
1573 free_iob ( wlan->beacon );
1580 * Free list of WLAN structures
1582 * @v list List of WLAN structures to free
1584 void net80211_free_wlanlist ( struct list_head *list )
1586 struct net80211_wlan *wlan, *tmp;
1591 list_for_each_entry_safe ( wlan, tmp, list, list ) {
1592 list_del ( &wlan->list );
1593 net80211_free_wlan ( wlan );
1600 /** Number of ticks to wait for replies to association management frames */
1601 #define ASSOC_TIMEOUT TICKS_PER_SEC
1603 /** Number of times to try sending a particular association management frame */
1604 #define ASSOC_RETRIES 2
1607 * Step 802.11 association process
1609 * @v proc Association process
1611 static void net80211_step_associate ( struct process *proc )
1613 struct net80211_device *dev =
1614 container_of ( proc, struct net80211_device, proc_assoc );
1616 int status = dev->state & NET80211_STATUS_MASK;
1619 * We use a sort of state machine implemented using bits in
1620 * the dev->state variable. At each call, we take the
1621 * logically first step that has not yet succeeded; either it
1622 * has not been tried yet, it's being retried, or it failed.
1623 * If it failed, we return an error indication; otherwise we
1624 * perform the step. If it succeeds, RX handling code will set
1625 * the appropriate status bit for us.
1627 * Probe works a bit differently, since we have to step it
1628 * on every call instead of waiting for a packet to arrive
1629 * that will set the completion bit for us.
1632 /* If we're waiting for a reply, check for timeout condition */
1633 if ( dev->state & NET80211_WAITING ) {
1635 if ( ! dev->associating )
1638 if ( currticks() - dev->ctx.assoc->last_packet > ASSOC_TIMEOUT ) {
1639 /* Timed out - fail if too many retries, or retry */
1640 dev->ctx.assoc->times_tried++;
1641 if ( ++dev->ctx.assoc->times_tried > ASSOC_RETRIES ) {
1646 /* Didn't time out - let it keep going */
1650 if ( dev->state & NET80211_PROBED )
1651 dev->ctx.assoc->times_tried = 0;
1654 if ( ! ( dev->state & NET80211_PROBED ) ) {
1657 if ( ! dev->ctx.probe ) {
1659 int active = fetch_intz_setting ( NULL,
1660 &net80211_active_setting );
1661 int band = dev->hw->bands;
1664 band &= ~NET80211_BAND_BIT_5GHZ;
1666 rc = net80211_prepare_probe ( dev, band, active );
1670 dev->ctx.probe = net80211_probe_start ( dev, dev->essid,
1672 if ( ! dev->ctx.probe ) {
1673 dev->assoc_rc = -ENOMEM;
1678 rc = net80211_probe_step ( dev->ctx.probe );
1680 return; /* still going */
1683 dev->associating = net80211_probe_finish_best ( dev->ctx.probe );
1684 dev->ctx.probe = NULL;
1685 if ( ! dev->associating ) {
1686 if ( rc > 0 ) /* "successful" probe found nothing */
1691 /* If we probed using a broadcast SSID, record that
1692 fact for the settings applicator before we clobber
1693 it with the specific SSID we've chosen. */
1694 if ( ! dev->essid[0] )
1695 dev->state |= NET80211_AUTO_SSID;
1697 DBGC ( dev, "802.11 %p found network %s (%s)\n", dev,
1698 dev->associating->essid,
1699 eth_ntoa ( dev->associating->bssid ) );
1701 dev->ctx.assoc = zalloc ( sizeof ( *dev->ctx.assoc ) );
1702 if ( ! dev->ctx.assoc ) {
1707 dev->state |= NET80211_PROBED;
1708 dev->ctx.assoc->method = IEEE80211_AUTH_OPEN_SYSTEM;
1713 /* Record time of sending the packet we're about to send, for timeout */
1714 dev->ctx.assoc->last_packet = currticks();
1716 if ( ! ( dev->state & NET80211_AUTHENTICATED ) ) {
1717 /* state: prepare and authenticate */
1719 if ( status != IEEE80211_STATUS_SUCCESS ) {
1720 /* we tried authenticating already, but failed */
1721 int method = dev->ctx.assoc->method;
1723 if ( method == IEEE80211_AUTH_OPEN_SYSTEM &&
1724 ( status == IEEE80211_STATUS_AUTH_CHALL_INVALID ||
1725 status == IEEE80211_STATUS_AUTH_ALGO_UNSUPP ) ) {
1726 /* Maybe this network uses Shared Key? */
1727 dev->ctx.assoc->method =
1728 IEEE80211_AUTH_SHARED_KEY;
1734 DBGC ( dev, "802.11 %p authenticating with method %d\n", dev,
1735 dev->ctx.assoc->method );
1737 rc = net80211_prepare_assoc ( dev, dev->associating );
1741 rc = net80211_send_auth ( dev, dev->associating,
1742 dev->ctx.assoc->method );
1749 if ( ! ( dev->state & NET80211_ASSOCIATED ) ) {
1750 /* state: associate */
1752 if ( status != IEEE80211_STATUS_SUCCESS )
1755 DBGC ( dev, "802.11 %p associating\n", dev );
1757 rc = net80211_send_assoc ( dev, dev->associating );
1764 if ( ! ( dev->state & NET80211_CRYPTO_SYNCED ) ) {
1765 /* state: crypto sync */
1766 DBGC ( dev, "802.11 %p security handshaking\n", dev );
1768 dev->state |= NET80211_CRYPTO_SYNCED;
1769 /* XXX need to actually do something here once we
1775 netdev_link_up ( dev->netdev );
1777 dev->state &= ~NET80211_WORKING;
1779 free ( dev->ctx.assoc );
1780 dev->ctx.assoc = NULL;
1782 net80211_free_wlan ( dev->associating );
1783 dev->associating = NULL;
1785 dev->rctl = rc80211_init ( dev );
1787 process_del ( proc );
1789 DBGC ( dev, "802.11 %p associated with %s (%s)\n", dev,
1790 dev->essid, eth_ntoa ( dev->bssid ) );
1795 dev->state &= ~( NET80211_WORKING | NET80211_WAITING );
1799 netdev_link_err ( dev->netdev, dev->assoc_rc );
1801 /* We never reach here from the middle of a probe, so we don't
1802 need to worry about freeing dev->ctx.probe. */
1804 if ( dev->state & NET80211_PROBED ) {
1805 free ( dev->ctx.assoc );
1806 dev->ctx.assoc = NULL;
1809 net80211_free_wlan ( dev->associating );
1810 dev->associating = NULL;
1812 process_del ( proc );
1814 DBGC ( dev, "802.11 %p association failed (state=%04x): "
1815 "%s\n", dev, dev->state, strerror ( dev->assoc_rc ) );
1818 net80211_autoassociate ( dev );
1822 * Check for 802.11 SSID updates
1824 * This acts as a settings applicator; if the user changes netX/ssid,
1825 * and netX is currently open, the association task will be invoked
1828 static int net80211_check_ssid_update ( void )
1830 struct net80211_device *dev;
1831 char ssid[IEEE80211_MAX_SSID_LEN + 1];
1833 list_for_each_entry ( dev, &net80211_devices, list ) {
1834 if ( ! ( dev->netdev->state & NETDEV_OPEN ) )
1837 fetch_string_setting ( netdev_settings ( dev->netdev ),
1838 &net80211_ssid_setting, ssid,
1839 IEEE80211_MAX_SSID_LEN + 1 );
1841 if ( strcmp ( ssid, dev->essid ) != 0 &&
1842 ! ( ! ssid[0] && ( dev->state & NET80211_AUTO_SSID ) ) ) {
1843 DBGC ( dev, "802.11 %p updating association: "
1844 "%s -> %s\n", dev, dev->essid, ssid );
1845 net80211_autoassociate ( dev );
1853 * Start 802.11 association process
1855 * @v dev 802.11 device
1857 * If the association process is running, it will be restarted.
1859 void net80211_autoassociate ( struct net80211_device *dev )
1861 if ( ! ( dev->state & NET80211_WORKING ) ) {
1862 DBGC2 ( dev, "802.11 %p spawning association process\n", dev );
1863 process_add ( &dev->proc_assoc );
1866 /* Clean up everything an earlier association process might
1867 have been in the middle of using */
1868 if ( dev->associating )
1869 net80211_free_wlan ( dev->associating );
1871 if ( ! ( dev->state & NET80211_PROBED ) )
1872 net80211_free_wlan (
1873 net80211_probe_finish_best ( dev->ctx.probe ) );
1875 free ( dev->ctx.assoc );
1877 /* Reset to a clean state */
1878 fetch_string_setting ( netdev_settings ( dev->netdev ),
1879 &net80211_ssid_setting, dev->essid,
1880 IEEE80211_MAX_SSID_LEN + 1 );
1881 dev->ctx.probe = NULL;
1882 dev->associating = NULL;
1883 net80211_set_state ( dev, NET80211_PROBED, NET80211_WORKING, 0 );
1887 * Pick TX rate for RTS/CTS packets based on data rate
1889 * @v dev 802.11 device
1891 * The RTS/CTS rate is the fastest TX rate marked as "basic" that is
1892 * not faster than the data rate.
1894 static void net80211_set_rtscts_rate ( struct net80211_device *dev )
1896 u16 datarate = dev->rates[dev->rate];
1901 for ( i = 0; i < dev->nr_rates; i++ ) {
1902 u16 rate = dev->rates[i];
1904 if ( ! ( dev->basic_rates & ( 1 << i ) ) || rate > datarate )
1907 if ( rate > rtsrate ) {
1913 /* If this is in initialization, we might not have any basic
1914 rates; just use the first data rate in that case. */
1918 dev->rtscts_rate = rts_idx;
1922 * Set data transmission rate for 802.11 device
1924 * @v dev 802.11 device
1925 * @v rate Rate to set, as index into @c dev->rates array
1927 void net80211_set_rate_idx ( struct net80211_device *dev, int rate )
1929 assert ( dev->netdev->state & NETDEV_OPEN );
1931 if ( rate >= 0 && rate < dev->nr_rates && rate != dev->rate ) {
1932 DBGC2 ( dev, "802.11 %p changing rate from %d->%d Mbps\n",
1933 dev, dev->rates[dev->rate] / 10,
1934 dev->rates[rate] / 10 );
1937 net80211_set_rtscts_rate ( dev );
1938 dev->op->config ( dev, NET80211_CFG_RATE );
1943 * Configure 802.11 device to transmit on a certain channel
1945 * @v dev 802.11 device
1946 * @v channel Channel number (1-11 for 2.4GHz) to transmit on
1948 int net80211_change_channel ( struct net80211_device *dev, int channel )
1950 int i, oldchan = dev->channel;
1952 assert ( dev->netdev->state & NETDEV_OPEN );
1954 for ( i = 0; i < dev->nr_channels; i++ ) {
1955 if ( dev->channels[i].channel_nr == channel ) {
1961 if ( i == dev->nr_channels )
1965 return dev->op->config ( dev, NET80211_CFG_CHANNEL );
1971 * Prepare 802.11 device channel and rate set for scanning
1973 * @v dev 802.11 device
1974 * @v band RF band(s) on which to prepare for scanning
1975 * @v active Whether the scanning will be active
1976 * @ret rc Return status code
1978 int net80211_prepare_probe ( struct net80211_device *dev, int band,
1981 assert ( dev->netdev->state & NETDEV_OPEN );
1983 if ( active && ( band & NET80211_BAND_BIT_5GHZ ) ) {
1984 DBGC ( dev, "802.11 %p cannot perform active scanning on "
1985 "5GHz band\n", dev );
1986 return -EINVAL_ACTIVE_SCAN;
1990 /* This can happen for a 5GHz-only card with 5GHz
1991 scanning masked out by an active request. */
1992 DBGC ( dev, "802.11 %p asked to prepare for scanning nothing\n",
1994 return -EINVAL_ACTIVE_SCAN;
1997 dev->nr_channels = 0;
2000 net80211_add_channels ( dev, 1, 11, NET80211_REG_TXPOWER );
2002 if ( band & NET80211_BAND_BIT_2GHZ )
2003 net80211_add_channels ( dev, 1, 14,
2004 NET80211_REG_TXPOWER );
2005 if ( band & NET80211_BAND_BIT_5GHZ )
2006 net80211_add_channels ( dev, 36, 8,
2007 NET80211_REG_TXPOWER );
2010 net80211_filter_hw_channels ( dev );
2012 /* Use channel 1 for now */
2014 dev->op->config ( dev, NET80211_CFG_CHANNEL );
2016 /* Always do active probes at lowest (presumably first) speed */
2019 dev->rates[0] = dev->hw->rates[dev->channels[0].band][0];
2020 dev->op->config ( dev, NET80211_CFG_RATE );
2026 * Prepare 802.11 device channel and rate set for communication
2028 * @v dev 802.11 device
2029 * @v wlan WLAN to prepare for communication with
2030 * @ret rc Return status code
2032 int net80211_prepare_assoc ( struct net80211_device *dev,
2033 struct net80211_wlan *wlan )
2035 struct ieee80211_frame *hdr = wlan->beacon->data;
2036 struct ieee80211_beacon *beacon =
2037 ( struct ieee80211_beacon * ) hdr->data;
2040 assert ( dev->netdev->state & NETDEV_OPEN );
2042 net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
2043 memcpy ( dev->bssid, wlan->bssid, ETH_ALEN );
2044 strcpy ( dev->essid, wlan->essid );
2046 dev->last_beacon_timestamp = beacon->timestamp;
2047 dev->tx_beacon_interval = 1024 * beacon->beacon_interval;
2049 /* XXX do crypto setup here */
2051 /* Barring an IE that tells us the channel outright, assume
2052 the channel we heard this AP best on is the channel it's
2053 communicating on. */
2054 net80211_change_channel ( dev, wlan->channel );
2056 rc = net80211_process_capab ( dev, beacon->capability );
2060 rc = net80211_process_ie ( dev, beacon->info_element,
2061 wlan->beacon->tail );
2065 /* Associate at the lowest rate so we know it'll get through */
2067 dev->op->config ( dev, NET80211_CFG_RATE );
2073 * Send 802.11 initial authentication frame
2075 * @v dev 802.11 device
2076 * @v wlan WLAN to authenticate with
2077 * @v method Authentication method
2078 * @ret rc Return status code
2080 * @a method may be 0 for Open System authentication or 1 for Shared
2081 * Key authentication. Open System provides no security in association
2082 * whatsoever, relying on encryption for confidentiality, but Shared
2083 * Key actively introduces security problems and is very rarely used.
2085 int net80211_send_auth ( struct net80211_device *dev,
2086 struct net80211_wlan *wlan, int method )
2088 struct io_buffer *iob = alloc_iob ( 64 );
2089 struct ieee80211_auth *auth;
2091 net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
2092 iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
2093 auth = iob_put ( iob, sizeof ( *auth ) );
2094 auth->algorithm = method;
2098 return net80211_tx_mgmt ( dev, IEEE80211_STYPE_AUTH, wlan->bssid, iob );
2102 * Handle receipt of 802.11 authentication frame
2104 * @v dev 802.11 device
2107 * If the authentication method being used is Shared Key, and the
2108 * frame that was received included challenge text, the frame is
2109 * encrypted using the cryptographic algorithm currently in effect and
2110 * sent back to the AP to complete the authentication.
2112 static void net80211_handle_auth ( struct net80211_device *dev,
2113 struct io_buffer *iob )
2115 struct ieee80211_frame *hdr = iob->data;
2116 struct ieee80211_auth *auth =
2117 ( struct ieee80211_auth * ) hdr->data;
2119 if ( auth->tx_seq & 1 ) {
2120 DBGC ( dev, "802.11 %p authentication received improperly "
2121 "directed frame (seq. %d)\n", dev, auth->tx_seq );
2122 net80211_set_state ( dev, NET80211_WAITING, 0,
2123 IEEE80211_STATUS_FAILURE );
2127 if ( auth->status != IEEE80211_STATUS_SUCCESS ) {
2128 DBGC ( dev, "802.11 %p authentication failed: status %d\n",
2129 dev, auth->status );
2130 net80211_set_state ( dev, NET80211_WAITING, 0,
2135 if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY && ! dev->crypto ) {
2136 DBGC ( dev, "802.11 %p can't perform shared-key authentication "
2137 "without a cryptosystem\n", dev );
2138 net80211_set_state ( dev, NET80211_WAITING, 0,
2139 IEEE80211_STATUS_FAILURE );
2143 if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY &&
2144 auth->tx_seq == 2 ) {
2145 /* Since the iob we got is going to be freed as soon
2146 as we return, we can do some in-place
2151 memcpy ( hdr->addr2, hdr->addr1, ETH_ALEN );
2152 memcpy ( hdr->addr1, hdr->addr3, ETH_ALEN );
2154 netdev_tx ( dev->netdev,
2155 dev->crypto->encrypt ( dev->crypto, iob ) );
2159 net80211_set_state ( dev, NET80211_WAITING, NET80211_AUTHENTICATED,
2160 IEEE80211_STATUS_SUCCESS );
2166 * Send 802.11 association frame
2168 * @v dev 802.11 device
2169 * @v wlan WLAN to associate with
2170 * @ret rc Return status code
2172 int net80211_send_assoc ( struct net80211_device *dev,
2173 struct net80211_wlan *wlan )
2175 struct io_buffer *iob = alloc_iob ( 128 );
2176 struct ieee80211_assoc_req *assoc;
2177 union ieee80211_ie *ie;
2179 net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
2181 iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
2184 assoc->capability = IEEE80211_CAPAB_MANAGED;
2185 if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE ) )
2186 assoc->capability |= IEEE80211_CAPAB_SHORT_PMBL;
2187 if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT ) )
2188 assoc->capability |= IEEE80211_CAPAB_SHORT_SLOT;
2190 assoc->capability |= IEEE80211_CAPAB_PRIVACY;
2192 assoc->listen_interval = 1;
2194 ie = net80211_marshal_request_info ( dev, assoc->info_element );
2196 DBGP ( "802.11 %p about to send association request:\n", dev );
2197 DBGP_HD ( iob->data, ( void * ) ie - iob->data );
2199 /* XXX add RSN ie for WPA support */
2201 iob_put ( iob, ( void * ) ie - iob->data );
2203 return net80211_tx_mgmt ( dev, IEEE80211_STYPE_ASSOC_REQ,
2208 * Handle receipt of 802.11 association reply frame
2210 * @v dev 802.11 device
2213 static void net80211_handle_assoc_reply ( struct net80211_device *dev,
2214 struct io_buffer *iob )
2216 struct ieee80211_frame *hdr = iob->data;
2217 struct ieee80211_assoc_resp *assoc =
2218 ( struct ieee80211_assoc_resp * ) hdr->data;
2220 net80211_process_capab ( dev, assoc->capability );
2221 net80211_process_ie ( dev, assoc->info_element, iob->tail );
2223 if ( assoc->status != IEEE80211_STATUS_SUCCESS ) {
2224 DBGC ( dev, "802.11 %p association failed: status %d\n",
2225 dev, assoc->status );
2226 net80211_set_state ( dev, NET80211_WAITING, 0,
2231 /* ESSID was filled before the association request was sent */
2232 memcpy ( dev->bssid, hdr->addr3, ETH_ALEN );
2233 dev->aid = assoc->aid;
2235 net80211_set_state ( dev, NET80211_WAITING, NET80211_ASSOCIATED,
2236 IEEE80211_STATUS_SUCCESS );
2241 * Send 802.11 disassociation frame
2243 * @v dev 802.11 device
2244 * @v reason Reason for disassociation
2245 * @ret rc Return status code
2247 static int net80211_send_disassoc ( struct net80211_device *dev, int reason )
2249 struct io_buffer *iob = alloc_iob ( 64 );
2250 struct ieee80211_disassoc *disassoc;
2252 if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2255 net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
2256 iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
2257 disassoc = iob_put ( iob, sizeof ( *disassoc ) );
2258 disassoc->reason = reason;
2260 return net80211_tx_mgmt ( dev, IEEE80211_STYPE_DISASSOC, dev->bssid,
2265 /** Smoothing factor (1-7) for link quality calculation */
2269 * Update link quality information based on received beacon
2271 * @v dev 802.11 device
2272 * @v iob I/O buffer containing beacon
2273 * @ret rc Return status code
2275 static void net80211_update_link_quality ( struct net80211_device *dev,
2276 struct io_buffer *iob )
2278 struct ieee80211_frame *hdr = iob->data;
2279 struct ieee80211_beacon *beacon;
2282 if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2285 beacon = ( struct ieee80211_beacon * ) hdr->data;
2286 dt = ( u32 ) ( beacon->timestamp - dev->last_beacon_timestamp );
2287 rxi = dev->rx_beacon_interval;
2289 rxi = ( LQ_SMOOTH * rxi ) + ( ( 8 - LQ_SMOOTH ) * dt );
2290 dev->rx_beacon_interval = rxi >> 3;
2292 dev->last_beacon_timestamp = beacon->timestamp;
2297 * Handle receipt of 802.11 management frame
2299 * @v dev 802.11 device
2301 * @v signal Signal strength of received frame
2303 static void net80211_handle_mgmt ( struct net80211_device *dev,
2304 struct io_buffer *iob, int signal )
2306 struct ieee80211_frame *hdr = iob->data;
2307 struct ieee80211_disassoc *disassoc;
2308 u16 stype = hdr->fc & IEEE80211_FC_SUBTYPE;
2310 int is_deauth = ( stype == IEEE80211_STYPE_DEAUTH );
2312 if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_MGMT ) {
2314 return; /* only handle management frames */
2318 /* We reconnect on deauthentication and disassociation. */
2319 case IEEE80211_STYPE_DEAUTH:
2320 case IEEE80211_STYPE_DISASSOC:
2321 disassoc = ( struct ieee80211_disassoc * ) hdr->data;
2322 net80211_set_state ( dev, is_deauth ? NET80211_AUTHENTICATED :
2323 NET80211_ASSOCIATED, 0,
2324 NET80211_IS_REASON | disassoc->reason );
2325 DBGC ( dev, "802.11 %p %s: reason %d\n",
2326 dev, is_deauth ? "deauthenticated" : "disassociated",
2329 /* Try to reassociate, in case it's transient. */
2330 net80211_autoassociate ( dev );
2334 /* We handle authentication and association. */
2335 case IEEE80211_STYPE_AUTH:
2336 if ( ! ( dev->state & NET80211_AUTHENTICATED ) )
2337 net80211_handle_auth ( dev, iob );
2340 case IEEE80211_STYPE_ASSOC_RESP:
2341 case IEEE80211_STYPE_REASSOC_RESP:
2342 if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2343 net80211_handle_assoc_reply ( dev, iob );
2346 /* We pass probes and beacons onto network scanning
2347 code. Pass actions for future extensibility. */
2348 case IEEE80211_STYPE_BEACON:
2349 net80211_update_link_quality ( dev, iob );
2351 case IEEE80211_STYPE_PROBE_RESP:
2352 case IEEE80211_STYPE_ACTION:
2353 if ( dev->keep_mgmt ) {
2354 struct net80211_rx_info *rxinf;
2355 rxinf = zalloc ( sizeof ( *rxinf ) );
2357 DBGC ( dev, "802.11 %p out of memory\n", dev );
2360 rxinf->signal = signal;
2361 list_add_tail ( &iob->list, &dev->mgmt_queue );
2362 list_add_tail ( &rxinf->list, &dev->mgmt_info_queue );
2367 case IEEE80211_STYPE_PROBE_REQ:
2368 /* Some nodes send these broadcast. Ignore them. */
2371 case IEEE80211_STYPE_ASSOC_REQ:
2372 case IEEE80211_STYPE_REASSOC_REQ:
2373 /* We should never receive these, only send them. */
2374 DBGC ( dev, "802.11 %p received strange management request "
2375 "(%04x)\n", dev, stype );
2379 DBGC ( dev, "802.11 %p received unimplemented management "
2380 "packet (%04x)\n", dev, stype );
2388 /* ---------- Packet handling functions ---------- */
2391 * Free buffers used by 802.11 fragment cache entry
2393 * @v dev 802.11 device
2394 * @v fcid Fragment cache entry index
2396 * After this function, the referenced entry will be marked unused.
2398 static void net80211_free_frags ( struct net80211_device *dev, int fcid )
2401 struct net80211_frag_cache *frag = &dev->frags[fcid];
2403 for ( j = 0; j < 16; j++ ) {
2404 if ( frag->iob[j] ) {
2405 free_iob ( frag->iob[j] );
2406 frag->iob[j] = NULL;
2411 frag->start_ticks = 0;
2416 * Accumulate 802.11 fragments into one I/O buffer
2418 * @v dev 802.11 device
2419 * @v fcid Fragment cache entry index
2420 * @v nfrags Number of fragments received
2421 * @v size Sum of sizes of all fragments, including headers
2422 * @ret iob I/O buffer containing reassembled packet
2424 * This function does not free the fragment buffers.
2426 static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev,
2427 int fcid, int nfrags, int size )
2429 struct net80211_frag_cache *frag = &dev->frags[fcid];
2430 int hdrsize = IEEE80211_TYP_FRAME_HEADER_LEN;
2431 int nsize = size - hdrsize * ( nfrags - 1 );
2434 struct io_buffer *niob = alloc_iob ( nsize );
2435 struct ieee80211_frame *hdr;
2437 /* Add the header from the first one... */
2438 memcpy ( iob_put ( niob, hdrsize ), frag->iob[0]->data, hdrsize );
2440 /* ... and all the data from all of them. */
2441 for ( i = 0; i < nfrags; i++ ) {
2442 int len = iob_len ( frag->iob[i] ) - hdrsize;
2443 memcpy ( iob_put ( niob, len ),
2444 frag->iob[i]->data + hdrsize, len );
2447 /* Turn off the fragment bit. */
2449 hdr->fc &= ~IEEE80211_FC_MORE_FRAG;
2455 * Handle receipt of 802.11 fragment
2457 * @v dev 802.11 device
2458 * @v iob I/O buffer containing fragment
2459 * @v signal Signal strength with which fragment was received
2461 static void net80211_rx_frag ( struct net80211_device *dev,
2462 struct io_buffer *iob, int signal )
2464 struct ieee80211_frame *hdr = iob->data;
2465 int fragnr = IEEE80211_FRAG ( hdr->seq );
2467 if ( fragnr == 0 && ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2468 /* start a frag cache entry */
2470 u32 curr_ticks = currticks(), newest_ticks = 0;
2471 u32 timeout = ticks_per_sec() * NET80211_FRAG_TIMEOUT;
2473 for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
2474 if ( dev->frags[i].in_use == 0 )
2477 if ( dev->frags[i].start_ticks + timeout >=
2479 net80211_free_frags ( dev, i );
2483 if ( dev->frags[i].start_ticks > newest_ticks ) {
2485 newest_ticks = dev->frags[i].start_ticks;
2489 /* If we're being sent more concurrent fragmented
2490 packets than we can handle, drop the newest so the
2491 older ones have time to complete. */
2492 if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
2494 net80211_free_frags ( dev, i );
2497 dev->frags[i].in_use = 1;
2498 dev->frags[i].seqnr = IEEE80211_SEQNR ( hdr->seq );
2499 dev->frags[i].start_ticks = currticks();
2500 dev->frags[i].iob[0] = iob;
2504 for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
2505 if ( dev->frags[i].in_use && dev->frags[i].seqnr ==
2506 IEEE80211_SEQNR ( hdr->seq ) )
2509 if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
2510 /* Drop non-first not-in-cache fragments */
2511 DBGC ( dev, "802.11 %p dropped fragment fc=%04x "
2512 "seq=%04x\n", dev, hdr->fc, hdr->seq );
2517 dev->frags[i].iob[fragnr] = iob;
2519 if ( ! ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2521 for ( j = 0; j < fragnr; j++ ) {
2522 size += iob_len ( dev->frags[i].iob[j] );
2523 if ( dev->frags[i].iob[j] == NULL )
2526 if ( j == fragnr ) {
2527 /* We've got everything */
2528 struct io_buffer *niob =
2529 net80211_accum_frags ( dev, i, fragnr,
2531 net80211_free_frags ( dev, i );
2532 net80211_rx ( dev, niob, signal, 0 );
2534 DBGC ( dev, "802.11 %p dropping fragmented "
2535 "packet due to out-of-order arrival, "
2536 "fc=%04x seq=%04x\n", dev, hdr->fc,
2538 net80211_free_frags ( dev, i );
2545 * Handle receipt of 802.11 frame
2547 * @v dev 802.11 device
2549 * @v signal Received signal strength
2550 * @v rate Bitrate at which frame was received, in 100 kbps units
2552 * If the rate or signal is unknown, 0 should be passed.
2554 void net80211_rx ( struct net80211_device *dev, struct io_buffer *iob,
2555 int signal, u16 rate )
2557 struct ieee80211_frame *hdr = iob->data;
2558 u16 type = hdr->fc & IEEE80211_FC_TYPE;
2559 if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION )
2560 goto drop; /* drop invalid-version packets */
2562 if ( type == IEEE80211_TYPE_CTRL )
2563 goto drop; /* we don't handle control packets,
2564 the hardware does */
2566 if ( dev->last_rx_seq == hdr->seq )
2567 goto drop; /* avoid duplicate packet */
2568 dev->last_rx_seq = hdr->seq;
2570 if ( dev->hw->flags & NET80211_HW_RX_HAS_FCS ) {
2571 /* discard the FCS */
2572 iob_unput ( iob, 4 );
2575 if ( hdr->fc & IEEE80211_FC_PROTECTED ) {
2576 struct io_buffer *niob;
2577 if ( ! dev->crypto )
2578 goto drop; /* can't decrypt packets on an open network */
2580 niob = dev->crypto->decrypt ( dev->crypto, iob );
2582 goto drop; /* drop failed decryption */
2587 dev->last_signal = signal;
2589 /* Fragments go into the frag cache or get dropped. */
2590 if ( IEEE80211_FRAG ( hdr->seq ) != 0
2591 || ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2592 net80211_rx_frag ( dev, iob, signal );
2596 /* Management frames get handled, enqueued, or dropped. */
2597 if ( type == IEEE80211_TYPE_MGMT ) {
2598 net80211_handle_mgmt ( dev, iob, signal );
2602 /* Data frames get dropped or sent to the net_device. */
2603 if ( ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA )
2604 goto drop; /* drop QoS, CFP, or null data packets */
2606 /* Update rate-control algorithm */
2608 rc80211_update_rx ( dev, hdr->fc & IEEE80211_FC_RETRY, rate );
2610 /* Pass packet onward */
2611 if ( netdev_link_ok ( dev->netdev ) ) {
2612 netdev_rx ( dev->netdev, iob );
2617 DBGC2 ( dev, "802.11 %p dropped packet fc=%04x seq=%04x\n", dev,
2618 hdr->fc, hdr->seq );
2623 /** Indicate an error in receiving a packet
2625 * @v dev 802.11 device
2626 * @v iob I/O buffer with received packet, or NULL
2629 * This logs the error with the wrapping net_device, and frees iob if
2632 void net80211_rx_err ( struct net80211_device *dev,
2633 struct io_buffer *iob, int rc )
2635 netdev_rx_err ( dev->netdev, iob, rc );
2638 /** Indicate the completed transmission of a packet
2640 * @v dev 802.11 device
2641 * @v iob I/O buffer of transmitted packet
2642 * @v retries Number of times this packet was retransmitted
2643 * @v rc Error code, or 0 for success
2645 * This logs an error with the wrapping net_device if one occurred,
2646 * and removes and frees the I/O buffer from its TX queue. The
2647 * provided retry information is used to tune our transmission rate.
2649 * If the packet did not need to be retransmitted because it was
2650 * properly ACKed the first time, @a retries should be 0.
2652 void net80211_tx_complete ( struct net80211_device *dev,
2653 struct io_buffer *iob, int retries, int rc )
2655 /* Update rate-control algorithm */
2657 rc80211_update_tx ( dev, retries, rc );
2659 /* Pass completion onward */
2660 netdev_tx_complete_err ( dev->netdev, iob, rc );