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 if ( ! list_empty ( ctx->beacons ) )
1525 net80211_free_wlanlist ( ctx->beacons );
1527 net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
1530 free_iob ( ctx->probe );
1539 * Finish probe of 802.11 networks, returning all networks found
1541 * @v ctx Probe context
1542 * @ret list List of net80211_wlan detailing networks found
1544 * If net80211_probe_start() was called with a particular SSID
1545 * parameter as filter, this will always return either an empty or a
1548 struct list_head *net80211_probe_finish_all ( struct net80211_probe_ctx *ctx )
1550 struct list_head *beacons = ctx->beacons;
1555 net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
1558 free_iob ( ctx->probe );
1567 * Free WLAN structure
1569 * @v wlan WLAN structure to free
1571 void net80211_free_wlan ( struct net80211_wlan *wlan )
1574 free_iob ( wlan->beacon );
1581 * Free list of WLAN structures
1583 * @v list List of WLAN structures to free
1585 void net80211_free_wlanlist ( struct list_head *list )
1587 struct net80211_wlan *wlan, *tmp;
1592 list_for_each_entry_safe ( wlan, tmp, list, list ) {
1593 list_del ( &wlan->list );
1594 net80211_free_wlan ( wlan );
1601 /** Number of ticks to wait for replies to association management frames */
1602 #define ASSOC_TIMEOUT TICKS_PER_SEC
1604 /** Number of times to try sending a particular association management frame */
1605 #define ASSOC_RETRIES 2
1608 * Step 802.11 association process
1610 * @v proc Association process
1612 static void net80211_step_associate ( struct process *proc )
1614 struct net80211_device *dev =
1615 container_of ( proc, struct net80211_device, proc_assoc );
1617 int status = dev->state & NET80211_STATUS_MASK;
1620 * We use a sort of state machine implemented using bits in
1621 * the dev->state variable. At each call, we take the
1622 * logically first step that has not yet succeeded; either it
1623 * has not been tried yet, it's being retried, or it failed.
1624 * If it failed, we return an error indication; otherwise we
1625 * perform the step. If it succeeds, RX handling code will set
1626 * the appropriate status bit for us.
1628 * Probe works a bit differently, since we have to step it
1629 * on every call instead of waiting for a packet to arrive
1630 * that will set the completion bit for us.
1633 /* If we're waiting for a reply, check for timeout condition */
1634 if ( dev->state & NET80211_WAITING ) {
1636 if ( ! dev->associating )
1639 if ( currticks() - dev->ctx.assoc->last_packet > ASSOC_TIMEOUT ) {
1640 /* Timed out - fail if too many retries, or retry */
1641 dev->ctx.assoc->times_tried++;
1642 if ( ++dev->ctx.assoc->times_tried > ASSOC_RETRIES ) {
1647 /* Didn't time out - let it keep going */
1651 if ( dev->state & NET80211_PROBED )
1652 dev->ctx.assoc->times_tried = 0;
1655 if ( ! ( dev->state & NET80211_PROBED ) ) {
1658 if ( ! dev->ctx.probe ) {
1660 int active = fetch_intz_setting ( NULL,
1661 &net80211_active_setting );
1662 int band = dev->hw->bands;
1665 band &= ~NET80211_BAND_BIT_5GHZ;
1667 rc = net80211_prepare_probe ( dev, band, active );
1671 dev->ctx.probe = net80211_probe_start ( dev, dev->essid,
1673 if ( ! dev->ctx.probe ) {
1674 dev->assoc_rc = -ENOMEM;
1679 rc = net80211_probe_step ( dev->ctx.probe );
1681 return; /* still going */
1684 dev->associating = net80211_probe_finish_best ( dev->ctx.probe );
1685 dev->ctx.probe = NULL;
1686 if ( ! dev->associating ) {
1687 if ( rc > 0 ) /* "successful" probe found nothing */
1692 /* If we probed using a broadcast SSID, record that
1693 fact for the settings applicator before we clobber
1694 it with the specific SSID we've chosen. */
1695 if ( ! dev->essid[0] )
1696 dev->state |= NET80211_AUTO_SSID;
1698 DBGC ( dev, "802.11 %p found network %s (%s)\n", dev,
1699 dev->associating->essid,
1700 eth_ntoa ( dev->associating->bssid ) );
1702 dev->ctx.assoc = zalloc ( sizeof ( *dev->ctx.assoc ) );
1703 if ( ! dev->ctx.assoc ) {
1708 dev->state |= NET80211_PROBED;
1709 dev->ctx.assoc->method = IEEE80211_AUTH_OPEN_SYSTEM;
1714 /* Record time of sending the packet we're about to send, for timeout */
1715 dev->ctx.assoc->last_packet = currticks();
1717 if ( ! ( dev->state & NET80211_AUTHENTICATED ) ) {
1718 /* state: prepare and authenticate */
1720 if ( status != IEEE80211_STATUS_SUCCESS ) {
1721 /* we tried authenticating already, but failed */
1722 int method = dev->ctx.assoc->method;
1724 if ( method == IEEE80211_AUTH_OPEN_SYSTEM &&
1725 ( status == IEEE80211_STATUS_AUTH_CHALL_INVALID ||
1726 status == IEEE80211_STATUS_AUTH_ALGO_UNSUPP ) ) {
1727 /* Maybe this network uses Shared Key? */
1728 dev->ctx.assoc->method =
1729 IEEE80211_AUTH_SHARED_KEY;
1735 DBGC ( dev, "802.11 %p authenticating with method %d\n", dev,
1736 dev->ctx.assoc->method );
1738 rc = net80211_prepare_assoc ( dev, dev->associating );
1742 rc = net80211_send_auth ( dev, dev->associating,
1743 dev->ctx.assoc->method );
1750 if ( ! ( dev->state & NET80211_ASSOCIATED ) ) {
1751 /* state: associate */
1753 if ( status != IEEE80211_STATUS_SUCCESS )
1756 DBGC ( dev, "802.11 %p associating\n", dev );
1758 rc = net80211_send_assoc ( dev, dev->associating );
1765 if ( ! ( dev->state & NET80211_CRYPTO_SYNCED ) ) {
1766 /* state: crypto sync */
1767 DBGC ( dev, "802.11 %p security handshaking\n", dev );
1769 dev->state |= NET80211_CRYPTO_SYNCED;
1770 /* XXX need to actually do something here once we
1776 netdev_link_up ( dev->netdev );
1778 dev->state &= ~NET80211_WORKING;
1780 free ( dev->ctx.assoc );
1781 dev->ctx.assoc = NULL;
1783 net80211_free_wlan ( dev->associating );
1784 dev->associating = NULL;
1786 dev->rctl = rc80211_init ( dev );
1788 process_del ( proc );
1790 DBGC ( dev, "802.11 %p associated with %s (%s)\n", dev,
1791 dev->essid, eth_ntoa ( dev->bssid ) );
1796 dev->state &= ~( NET80211_WORKING | NET80211_WAITING );
1800 netdev_link_err ( dev->netdev, dev->assoc_rc );
1802 /* We never reach here from the middle of a probe, so we don't
1803 need to worry about freeing dev->ctx.probe. */
1805 if ( dev->state & NET80211_PROBED ) {
1806 free ( dev->ctx.assoc );
1807 dev->ctx.assoc = NULL;
1810 net80211_free_wlan ( dev->associating );
1811 dev->associating = NULL;
1813 process_del ( proc );
1815 DBGC ( dev, "802.11 %p association failed (state=%04x): "
1816 "%s\n", dev, dev->state, strerror ( dev->assoc_rc ) );
1819 net80211_autoassociate ( dev );
1823 * Check for 802.11 SSID updates
1825 * This acts as a settings applicator; if the user changes netX/ssid,
1826 * and netX is currently open, the association task will be invoked
1829 static int net80211_check_ssid_update ( void )
1831 struct net80211_device *dev;
1832 char ssid[IEEE80211_MAX_SSID_LEN + 1];
1834 list_for_each_entry ( dev, &net80211_devices, list ) {
1835 if ( ! ( dev->netdev->state & NETDEV_OPEN ) )
1838 fetch_string_setting ( netdev_settings ( dev->netdev ),
1839 &net80211_ssid_setting, ssid,
1840 IEEE80211_MAX_SSID_LEN + 1 );
1842 if ( strcmp ( ssid, dev->essid ) != 0 &&
1843 ! ( ! ssid[0] && ( dev->state & NET80211_AUTO_SSID ) ) ) {
1844 DBGC ( dev, "802.11 %p updating association: "
1845 "%s -> %s\n", dev, dev->essid, ssid );
1846 net80211_autoassociate ( dev );
1854 * Start 802.11 association process
1856 * @v dev 802.11 device
1858 * If the association process is running, it will be restarted.
1860 void net80211_autoassociate ( struct net80211_device *dev )
1862 if ( ! ( dev->state & NET80211_WORKING ) ) {
1863 DBGC2 ( dev, "802.11 %p spawning association process\n", dev );
1864 process_add ( &dev->proc_assoc );
1867 /* Clean up everything an earlier association process might
1868 have been in the middle of using */
1869 if ( dev->associating )
1870 net80211_free_wlan ( dev->associating );
1872 if ( ! ( dev->state & NET80211_PROBED ) )
1873 net80211_free_wlan (
1874 net80211_probe_finish_best ( dev->ctx.probe ) );
1876 free ( dev->ctx.assoc );
1878 /* Reset to a clean state */
1879 fetch_string_setting ( netdev_settings ( dev->netdev ),
1880 &net80211_ssid_setting, dev->essid,
1881 IEEE80211_MAX_SSID_LEN + 1 );
1882 dev->ctx.probe = NULL;
1883 dev->associating = NULL;
1884 net80211_set_state ( dev, NET80211_PROBED, NET80211_WORKING, 0 );
1888 * Pick TX rate for RTS/CTS packets based on data rate
1890 * @v dev 802.11 device
1892 * The RTS/CTS rate is the fastest TX rate marked as "basic" that is
1893 * not faster than the data rate.
1895 static void net80211_set_rtscts_rate ( struct net80211_device *dev )
1897 u16 datarate = dev->rates[dev->rate];
1902 for ( i = 0; i < dev->nr_rates; i++ ) {
1903 u16 rate = dev->rates[i];
1905 if ( ! ( dev->basic_rates & ( 1 << i ) ) || rate > datarate )
1908 if ( rate > rtsrate ) {
1914 /* If this is in initialization, we might not have any basic
1915 rates; just use the first data rate in that case. */
1919 dev->rtscts_rate = rts_idx;
1923 * Set data transmission rate for 802.11 device
1925 * @v dev 802.11 device
1926 * @v rate Rate to set, as index into @c dev->rates array
1928 void net80211_set_rate_idx ( struct net80211_device *dev, int rate )
1930 assert ( dev->netdev->state & NETDEV_OPEN );
1932 if ( rate >= 0 && rate < dev->nr_rates && rate != dev->rate ) {
1933 DBGC2 ( dev, "802.11 %p changing rate from %d->%d Mbps\n",
1934 dev, dev->rates[dev->rate] / 10,
1935 dev->rates[rate] / 10 );
1938 net80211_set_rtscts_rate ( dev );
1939 dev->op->config ( dev, NET80211_CFG_RATE );
1944 * Configure 802.11 device to transmit on a certain channel
1946 * @v dev 802.11 device
1947 * @v channel Channel number (1-11 for 2.4GHz) to transmit on
1949 int net80211_change_channel ( struct net80211_device *dev, int channel )
1951 int i, oldchan = dev->channel;
1953 assert ( dev->netdev->state & NETDEV_OPEN );
1955 for ( i = 0; i < dev->nr_channels; i++ ) {
1956 if ( dev->channels[i].channel_nr == channel ) {
1962 if ( i == dev->nr_channels )
1966 return dev->op->config ( dev, NET80211_CFG_CHANNEL );
1972 * Prepare 802.11 device channel and rate set for scanning
1974 * @v dev 802.11 device
1975 * @v band RF band(s) on which to prepare for scanning
1976 * @v active Whether the scanning will be active
1977 * @ret rc Return status code
1979 int net80211_prepare_probe ( struct net80211_device *dev, int band,
1982 assert ( dev->netdev->state & NETDEV_OPEN );
1984 if ( active && ( band & NET80211_BAND_BIT_5GHZ ) ) {
1985 DBGC ( dev, "802.11 %p cannot perform active scanning on "
1986 "5GHz band\n", dev );
1987 return -EINVAL_ACTIVE_SCAN;
1991 /* This can happen for a 5GHz-only card with 5GHz
1992 scanning masked out by an active request. */
1993 DBGC ( dev, "802.11 %p asked to prepare for scanning nothing\n",
1995 return -EINVAL_ACTIVE_SCAN;
1998 dev->nr_channels = 0;
2001 net80211_add_channels ( dev, 1, 11, NET80211_REG_TXPOWER );
2003 if ( band & NET80211_BAND_BIT_2GHZ )
2004 net80211_add_channels ( dev, 1, 14,
2005 NET80211_REG_TXPOWER );
2006 if ( band & NET80211_BAND_BIT_5GHZ )
2007 net80211_add_channels ( dev, 36, 8,
2008 NET80211_REG_TXPOWER );
2011 net80211_filter_hw_channels ( dev );
2013 /* Use channel 1 for now */
2015 dev->op->config ( dev, NET80211_CFG_CHANNEL );
2017 /* Always do active probes at lowest (presumably first) speed */
2020 dev->rates[0] = dev->hw->rates[dev->channels[0].band][0];
2021 dev->op->config ( dev, NET80211_CFG_RATE );
2027 * Prepare 802.11 device channel and rate set for communication
2029 * @v dev 802.11 device
2030 * @v wlan WLAN to prepare for communication with
2031 * @ret rc Return status code
2033 int net80211_prepare_assoc ( struct net80211_device *dev,
2034 struct net80211_wlan *wlan )
2036 struct ieee80211_frame *hdr = wlan->beacon->data;
2037 struct ieee80211_beacon *beacon =
2038 ( struct ieee80211_beacon * ) hdr->data;
2041 assert ( dev->netdev->state & NETDEV_OPEN );
2043 net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
2044 memcpy ( dev->bssid, wlan->bssid, ETH_ALEN );
2045 strcpy ( dev->essid, wlan->essid );
2047 dev->last_beacon_timestamp = beacon->timestamp;
2048 dev->tx_beacon_interval = 1024 * beacon->beacon_interval;
2050 /* XXX do crypto setup here */
2052 /* Barring an IE that tells us the channel outright, assume
2053 the channel we heard this AP best on is the channel it's
2054 communicating on. */
2055 net80211_change_channel ( dev, wlan->channel );
2057 rc = net80211_process_capab ( dev, beacon->capability );
2061 rc = net80211_process_ie ( dev, beacon->info_element,
2062 wlan->beacon->tail );
2066 /* Associate at the lowest rate so we know it'll get through */
2068 dev->op->config ( dev, NET80211_CFG_RATE );
2074 * Send 802.11 initial authentication frame
2076 * @v dev 802.11 device
2077 * @v wlan WLAN to authenticate with
2078 * @v method Authentication method
2079 * @ret rc Return status code
2081 * @a method may be 0 for Open System authentication or 1 for Shared
2082 * Key authentication. Open System provides no security in association
2083 * whatsoever, relying on encryption for confidentiality, but Shared
2084 * Key actively introduces security problems and is very rarely used.
2086 int net80211_send_auth ( struct net80211_device *dev,
2087 struct net80211_wlan *wlan, int method )
2089 struct io_buffer *iob = alloc_iob ( 64 );
2090 struct ieee80211_auth *auth;
2092 net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
2093 iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
2094 auth = iob_put ( iob, sizeof ( *auth ) );
2095 auth->algorithm = method;
2099 return net80211_tx_mgmt ( dev, IEEE80211_STYPE_AUTH, wlan->bssid, iob );
2103 * Handle receipt of 802.11 authentication frame
2105 * @v dev 802.11 device
2108 * If the authentication method being used is Shared Key, and the
2109 * frame that was received included challenge text, the frame is
2110 * encrypted using the cryptographic algorithm currently in effect and
2111 * sent back to the AP to complete the authentication.
2113 static void net80211_handle_auth ( struct net80211_device *dev,
2114 struct io_buffer *iob )
2116 struct ieee80211_frame *hdr = iob->data;
2117 struct ieee80211_auth *auth =
2118 ( struct ieee80211_auth * ) hdr->data;
2120 if ( auth->tx_seq & 1 ) {
2121 DBGC ( dev, "802.11 %p authentication received improperly "
2122 "directed frame (seq. %d)\n", dev, auth->tx_seq );
2123 net80211_set_state ( dev, NET80211_WAITING, 0,
2124 IEEE80211_STATUS_FAILURE );
2128 if ( auth->status != IEEE80211_STATUS_SUCCESS ) {
2129 DBGC ( dev, "802.11 %p authentication failed: status %d\n",
2130 dev, auth->status );
2131 net80211_set_state ( dev, NET80211_WAITING, 0,
2136 if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY && ! dev->crypto ) {
2137 DBGC ( dev, "802.11 %p can't perform shared-key authentication "
2138 "without a cryptosystem\n", dev );
2139 net80211_set_state ( dev, NET80211_WAITING, 0,
2140 IEEE80211_STATUS_FAILURE );
2144 if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY &&
2145 auth->tx_seq == 2 ) {
2146 /* Since the iob we got is going to be freed as soon
2147 as we return, we can do some in-place
2152 memcpy ( hdr->addr2, hdr->addr1, ETH_ALEN );
2153 memcpy ( hdr->addr1, hdr->addr3, ETH_ALEN );
2155 netdev_tx ( dev->netdev,
2156 dev->crypto->encrypt ( dev->crypto, iob ) );
2160 net80211_set_state ( dev, NET80211_WAITING, NET80211_AUTHENTICATED,
2161 IEEE80211_STATUS_SUCCESS );
2167 * Send 802.11 association frame
2169 * @v dev 802.11 device
2170 * @v wlan WLAN to associate with
2171 * @ret rc Return status code
2173 int net80211_send_assoc ( struct net80211_device *dev,
2174 struct net80211_wlan *wlan )
2176 struct io_buffer *iob = alloc_iob ( 128 );
2177 struct ieee80211_assoc_req *assoc;
2178 union ieee80211_ie *ie;
2180 net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
2182 iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
2185 assoc->capability = IEEE80211_CAPAB_MANAGED;
2186 if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE ) )
2187 assoc->capability |= IEEE80211_CAPAB_SHORT_PMBL;
2188 if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT ) )
2189 assoc->capability |= IEEE80211_CAPAB_SHORT_SLOT;
2191 assoc->capability |= IEEE80211_CAPAB_PRIVACY;
2193 assoc->listen_interval = 1;
2195 ie = net80211_marshal_request_info ( dev, assoc->info_element );
2197 DBGP ( "802.11 %p about to send association request:\n", dev );
2198 DBGP_HD ( iob->data, ( void * ) ie - iob->data );
2200 /* XXX add RSN ie for WPA support */
2202 iob_put ( iob, ( void * ) ie - iob->data );
2204 return net80211_tx_mgmt ( dev, IEEE80211_STYPE_ASSOC_REQ,
2209 * Handle receipt of 802.11 association reply frame
2211 * @v dev 802.11 device
2214 static void net80211_handle_assoc_reply ( struct net80211_device *dev,
2215 struct io_buffer *iob )
2217 struct ieee80211_frame *hdr = iob->data;
2218 struct ieee80211_assoc_resp *assoc =
2219 ( struct ieee80211_assoc_resp * ) hdr->data;
2221 net80211_process_capab ( dev, assoc->capability );
2222 net80211_process_ie ( dev, assoc->info_element, iob->tail );
2224 if ( assoc->status != IEEE80211_STATUS_SUCCESS ) {
2225 DBGC ( dev, "802.11 %p association failed: status %d\n",
2226 dev, assoc->status );
2227 net80211_set_state ( dev, NET80211_WAITING, 0,
2232 /* ESSID was filled before the association request was sent */
2233 memcpy ( dev->bssid, hdr->addr3, ETH_ALEN );
2234 dev->aid = assoc->aid;
2236 net80211_set_state ( dev, NET80211_WAITING, NET80211_ASSOCIATED,
2237 IEEE80211_STATUS_SUCCESS );
2242 * Send 802.11 disassociation frame
2244 * @v dev 802.11 device
2245 * @v reason Reason for disassociation
2246 * @ret rc Return status code
2248 static int net80211_send_disassoc ( struct net80211_device *dev, int reason )
2250 struct io_buffer *iob = alloc_iob ( 64 );
2251 struct ieee80211_disassoc *disassoc;
2253 if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2256 net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
2257 iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
2258 disassoc = iob_put ( iob, sizeof ( *disassoc ) );
2259 disassoc->reason = reason;
2261 return net80211_tx_mgmt ( dev, IEEE80211_STYPE_DISASSOC, dev->bssid,
2266 /** Smoothing factor (1-7) for link quality calculation */
2270 * Update link quality information based on received beacon
2272 * @v dev 802.11 device
2273 * @v iob I/O buffer containing beacon
2274 * @ret rc Return status code
2276 static void net80211_update_link_quality ( struct net80211_device *dev,
2277 struct io_buffer *iob )
2279 struct ieee80211_frame *hdr = iob->data;
2280 struct ieee80211_beacon *beacon;
2283 if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2286 beacon = ( struct ieee80211_beacon * ) hdr->data;
2287 dt = ( u32 ) ( beacon->timestamp - dev->last_beacon_timestamp );
2288 rxi = dev->rx_beacon_interval;
2290 rxi = ( LQ_SMOOTH * rxi ) + ( ( 8 - LQ_SMOOTH ) * dt );
2291 dev->rx_beacon_interval = rxi >> 3;
2293 dev->last_beacon_timestamp = beacon->timestamp;
2298 * Handle receipt of 802.11 management frame
2300 * @v dev 802.11 device
2302 * @v signal Signal strength of received frame
2304 static void net80211_handle_mgmt ( struct net80211_device *dev,
2305 struct io_buffer *iob, int signal )
2307 struct ieee80211_frame *hdr = iob->data;
2308 struct ieee80211_disassoc *disassoc;
2309 u16 stype = hdr->fc & IEEE80211_FC_SUBTYPE;
2311 int is_deauth = ( stype == IEEE80211_STYPE_DEAUTH );
2313 if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_MGMT ) {
2315 return; /* only handle management frames */
2319 /* We reconnect on deauthentication and disassociation. */
2320 case IEEE80211_STYPE_DEAUTH:
2321 case IEEE80211_STYPE_DISASSOC:
2322 disassoc = ( struct ieee80211_disassoc * ) hdr->data;
2323 net80211_set_state ( dev, is_deauth ? NET80211_AUTHENTICATED :
2324 NET80211_ASSOCIATED, 0,
2325 NET80211_IS_REASON | disassoc->reason );
2326 DBGC ( dev, "802.11 %p %s: reason %d\n",
2327 dev, is_deauth ? "deauthenticated" : "disassociated",
2330 /* Try to reassociate, in case it's transient. */
2331 net80211_autoassociate ( dev );
2335 /* We handle authentication and association. */
2336 case IEEE80211_STYPE_AUTH:
2337 if ( ! ( dev->state & NET80211_AUTHENTICATED ) )
2338 net80211_handle_auth ( dev, iob );
2341 case IEEE80211_STYPE_ASSOC_RESP:
2342 case IEEE80211_STYPE_REASSOC_RESP:
2343 if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2344 net80211_handle_assoc_reply ( dev, iob );
2347 /* We pass probes and beacons onto network scanning
2348 code. Pass actions for future extensibility. */
2349 case IEEE80211_STYPE_BEACON:
2350 net80211_update_link_quality ( dev, iob );
2352 case IEEE80211_STYPE_PROBE_RESP:
2353 case IEEE80211_STYPE_ACTION:
2354 if ( dev->keep_mgmt ) {
2355 struct net80211_rx_info *rxinf;
2356 rxinf = zalloc ( sizeof ( *rxinf ) );
2358 DBGC ( dev, "802.11 %p out of memory\n", dev );
2361 rxinf->signal = signal;
2362 list_add_tail ( &iob->list, &dev->mgmt_queue );
2363 list_add_tail ( &rxinf->list, &dev->mgmt_info_queue );
2368 case IEEE80211_STYPE_PROBE_REQ:
2369 /* Some nodes send these broadcast. Ignore them. */
2372 case IEEE80211_STYPE_ASSOC_REQ:
2373 case IEEE80211_STYPE_REASSOC_REQ:
2374 /* We should never receive these, only send them. */
2375 DBGC ( dev, "802.11 %p received strange management request "
2376 "(%04x)\n", dev, stype );
2380 DBGC ( dev, "802.11 %p received unimplemented management "
2381 "packet (%04x)\n", dev, stype );
2389 /* ---------- Packet handling functions ---------- */
2392 * Free buffers used by 802.11 fragment cache entry
2394 * @v dev 802.11 device
2395 * @v fcid Fragment cache entry index
2397 * After this function, the referenced entry will be marked unused.
2399 static void net80211_free_frags ( struct net80211_device *dev, int fcid )
2402 struct net80211_frag_cache *frag = &dev->frags[fcid];
2404 for ( j = 0; j < 16; j++ ) {
2405 if ( frag->iob[j] ) {
2406 free_iob ( frag->iob[j] );
2407 frag->iob[j] = NULL;
2412 frag->start_ticks = 0;
2417 * Accumulate 802.11 fragments into one I/O buffer
2419 * @v dev 802.11 device
2420 * @v fcid Fragment cache entry index
2421 * @v nfrags Number of fragments received
2422 * @v size Sum of sizes of all fragments, including headers
2423 * @ret iob I/O buffer containing reassembled packet
2425 * This function does not free the fragment buffers.
2427 static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev,
2428 int fcid, int nfrags, int size )
2430 struct net80211_frag_cache *frag = &dev->frags[fcid];
2431 int hdrsize = IEEE80211_TYP_FRAME_HEADER_LEN;
2432 int nsize = size - hdrsize * ( nfrags - 1 );
2435 struct io_buffer *niob = alloc_iob ( nsize );
2436 struct ieee80211_frame *hdr;
2438 /* Add the header from the first one... */
2439 memcpy ( iob_put ( niob, hdrsize ), frag->iob[0]->data, hdrsize );
2441 /* ... and all the data from all of them. */
2442 for ( i = 0; i < nfrags; i++ ) {
2443 int len = iob_len ( frag->iob[i] ) - hdrsize;
2444 memcpy ( iob_put ( niob, len ),
2445 frag->iob[i]->data + hdrsize, len );
2448 /* Turn off the fragment bit. */
2450 hdr->fc &= ~IEEE80211_FC_MORE_FRAG;
2456 * Handle receipt of 802.11 fragment
2458 * @v dev 802.11 device
2459 * @v iob I/O buffer containing fragment
2460 * @v signal Signal strength with which fragment was received
2462 static void net80211_rx_frag ( struct net80211_device *dev,
2463 struct io_buffer *iob, int signal )
2465 struct ieee80211_frame *hdr = iob->data;
2466 int fragnr = IEEE80211_FRAG ( hdr->seq );
2468 if ( fragnr == 0 && ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2469 /* start a frag cache entry */
2471 u32 curr_ticks = currticks(), newest_ticks = 0;
2472 u32 timeout = ticks_per_sec() * NET80211_FRAG_TIMEOUT;
2474 for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
2475 if ( dev->frags[i].in_use == 0 )
2478 if ( dev->frags[i].start_ticks + timeout >=
2480 net80211_free_frags ( dev, i );
2484 if ( dev->frags[i].start_ticks > newest_ticks ) {
2486 newest_ticks = dev->frags[i].start_ticks;
2490 /* If we're being sent more concurrent fragmented
2491 packets than we can handle, drop the newest so the
2492 older ones have time to complete. */
2493 if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
2495 net80211_free_frags ( dev, i );
2498 dev->frags[i].in_use = 1;
2499 dev->frags[i].seqnr = IEEE80211_SEQNR ( hdr->seq );
2500 dev->frags[i].start_ticks = currticks();
2501 dev->frags[i].iob[0] = iob;
2505 for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
2506 if ( dev->frags[i].in_use && dev->frags[i].seqnr ==
2507 IEEE80211_SEQNR ( hdr->seq ) )
2510 if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
2511 /* Drop non-first not-in-cache fragments */
2512 DBGC ( dev, "802.11 %p dropped fragment fc=%04x "
2513 "seq=%04x\n", dev, hdr->fc, hdr->seq );
2518 dev->frags[i].iob[fragnr] = iob;
2520 if ( ! ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2522 for ( j = 0; j < fragnr; j++ ) {
2523 size += iob_len ( dev->frags[i].iob[j] );
2524 if ( dev->frags[i].iob[j] == NULL )
2527 if ( j == fragnr ) {
2528 /* We've got everything */
2529 struct io_buffer *niob =
2530 net80211_accum_frags ( dev, i, fragnr,
2532 net80211_free_frags ( dev, i );
2533 net80211_rx ( dev, niob, signal, 0 );
2535 DBGC ( dev, "802.11 %p dropping fragmented "
2536 "packet due to out-of-order arrival, "
2537 "fc=%04x seq=%04x\n", dev, hdr->fc,
2539 net80211_free_frags ( dev, i );
2546 * Handle receipt of 802.11 frame
2548 * @v dev 802.11 device
2550 * @v signal Received signal strength
2551 * @v rate Bitrate at which frame was received, in 100 kbps units
2553 * If the rate or signal is unknown, 0 should be passed.
2555 void net80211_rx ( struct net80211_device *dev, struct io_buffer *iob,
2556 int signal, u16 rate )
2558 struct ieee80211_frame *hdr = iob->data;
2559 u16 type = hdr->fc & IEEE80211_FC_TYPE;
2560 if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION )
2561 goto drop; /* drop invalid-version packets */
2563 if ( type == IEEE80211_TYPE_CTRL )
2564 goto drop; /* we don't handle control packets,
2565 the hardware does */
2567 if ( dev->last_rx_seq == hdr->seq )
2568 goto drop; /* avoid duplicate packet */
2569 dev->last_rx_seq = hdr->seq;
2571 if ( dev->hw->flags & NET80211_HW_RX_HAS_FCS ) {
2572 /* discard the FCS */
2573 iob_unput ( iob, 4 );
2576 if ( hdr->fc & IEEE80211_FC_PROTECTED ) {
2577 struct io_buffer *niob;
2578 if ( ! dev->crypto )
2579 goto drop; /* can't decrypt packets on an open network */
2581 niob = dev->crypto->decrypt ( dev->crypto, iob );
2583 goto drop; /* drop failed decryption */
2588 dev->last_signal = signal;
2590 /* Fragments go into the frag cache or get dropped. */
2591 if ( IEEE80211_FRAG ( hdr->seq ) != 0
2592 || ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2593 net80211_rx_frag ( dev, iob, signal );
2597 /* Management frames get handled, enqueued, or dropped. */
2598 if ( type == IEEE80211_TYPE_MGMT ) {
2599 net80211_handle_mgmt ( dev, iob, signal );
2603 /* Data frames get dropped or sent to the net_device. */
2604 if ( ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA )
2605 goto drop; /* drop QoS, CFP, or null data packets */
2607 /* Update rate-control algorithm */
2609 rc80211_update_rx ( dev, hdr->fc & IEEE80211_FC_RETRY, rate );
2611 /* Pass packet onward */
2612 if ( netdev_link_ok ( dev->netdev ) ) {
2613 netdev_rx ( dev->netdev, iob );
2618 DBGC2 ( dev, "802.11 %p dropped packet fc=%04x seq=%04x\n", dev,
2619 hdr->fc, hdr->seq );
2624 /** Indicate an error in receiving a packet
2626 * @v dev 802.11 device
2627 * @v iob I/O buffer with received packet, or NULL
2630 * This logs the error with the wrapping net_device, and frees iob if
2633 void net80211_rx_err ( struct net80211_device *dev,
2634 struct io_buffer *iob, int rc )
2636 netdev_rx_err ( dev->netdev, iob, rc );
2639 /** Indicate the completed transmission of a packet
2641 * @v dev 802.11 device
2642 * @v iob I/O buffer of transmitted packet
2643 * @v retries Number of times this packet was retransmitted
2644 * @v rc Error code, or 0 for success
2646 * This logs an error with the wrapping net_device if one occurred,
2647 * and removes and frees the I/O buffer from its TX queue. The
2648 * provided retry information is used to tune our transmission rate.
2650 * If the packet did not need to be retransmitted because it was
2651 * properly ACKed the first time, @a retries should be 0.
2653 void net80211_tx_complete ( struct net80211_device *dev,
2654 struct io_buffer *iob, int retries, int rc )
2656 /* Update rate-control algorithm */
2658 rc80211_update_tx ( dev, retries, rc );
2660 /* Pass completion onward */
2661 netdev_tx_complete_err ( dev->netdev, iob, rc );