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,
605 .init_addr = eth_init_addr,
607 .mc_hash = net80211_ll_mc_hash,
608 .ll_proto = htons ( ARPHRD_ETHER ), /* "encapsulated Ethernet" */
609 .hw_addr_len = ETH_ALEN,
610 .ll_addr_len = ETH_ALEN,
611 .ll_header_len = IEEE80211_TYP_FRAME_HEADER_LEN +
612 IEEE80211_LLC_HEADER_LEN,
616 /* ---------- 802.11 network management API ---------- */
619 * Get 802.11 device from wrapping network device
621 * @v netdev Wrapping network device
622 * @ret dev 802.11 device wrapped by network device, or NULL
624 * Returns NULL if the network device does not wrap an 802.11 device.
626 struct net80211_device * net80211_get ( struct net_device *netdev )
628 struct net80211_device *dev;
630 list_for_each_entry ( dev, &net80211_devices, list ) {
631 if ( netdev->priv == dev )
639 * Set state of 802.11 device keeping management frames
641 * @v dev 802.11 device
642 * @v enable Whether to keep management frames
643 * @ret oldenab Whether management frames were enabled before this call
645 * If enable is TRUE, beacon, probe, and action frames will be kept
646 * and may be retrieved by calling net80211_mgmt_dequeue().
648 int net80211_keep_mgmt ( struct net80211_device *dev, int enable )
650 int oldenab = dev->keep_mgmt;
652 dev->keep_mgmt = enable;
657 * Get 802.11 management frame
659 * @v dev 802.11 device
660 * @ret signal Signal strength of returned management frame
661 * @ret iob I/O buffer, or NULL if no management frame is queued
663 * Frames will only be returned by this function if
664 * net80211_keep_mgmt() has been previously called with enable set to
667 * The calling function takes ownership of the returned I/O buffer.
669 struct io_buffer * net80211_mgmt_dequeue ( struct net80211_device *dev,
672 struct io_buffer *iobuf;
673 struct net80211_rx_info *rxi;
675 list_for_each_entry ( rxi, &dev->mgmt_info_queue, list ) {
676 list_del ( &rxi->list );
678 *signal = rxi->signal;
681 list_for_each_entry ( iobuf, &dev->mgmt_queue, list ) {
682 list_del ( &iobuf->list );
692 * Transmit 802.11 management frame
694 * @v dev 802.11 device
695 * @v fc Frame Control flags for management frame
696 * @v dest Destination access point
698 * @ret rc Return status code
700 * The @a fc argument must contain at least an IEEE 802.11 management
701 * subtype number (e.g. IEEE80211_STYPE_PROBE_REQ). If it contains
702 * IEEE80211_FC_PROTECTED, the frame will be encrypted prior to
705 * It is required that @a iob have at least 24 bytes of headroom
706 * reserved before its data start.
708 int net80211_tx_mgmt ( struct net80211_device *dev, u16 fc, u8 dest[6],
709 struct io_buffer *iob )
711 struct ieee80211_frame *hdr = iob_push ( iob,
712 IEEE80211_TYP_FRAME_HEADER_LEN );
714 hdr->fc = IEEE80211_THIS_VERSION | IEEE80211_TYPE_MGMT |
715 ( fc & ~IEEE80211_FC_PROTECTED );
716 hdr->duration = net80211_duration ( dev, 10, dev->rates[dev->rate] );
717 hdr->seq = IEEE80211_MAKESEQ ( ++dev->last_tx_seqnr, 0 );
719 memcpy ( hdr->addr1, dest, ETH_ALEN ); /* DA = RA */
720 memcpy ( hdr->addr2, dev->netdev->ll_addr, ETH_ALEN ); /* SA = TA */
721 memcpy ( hdr->addr3, dest, ETH_ALEN ); /* BSSID */
723 if ( fc & IEEE80211_FC_PROTECTED ) {
725 return -EINVAL_CRYPTO_REQUEST;
727 struct io_buffer *eiob = dev->crypto->encrypt ( dev->crypto,
733 return netdev_tx ( dev->netdev, iob );
737 /* ---------- Driver API ---------- */
740 * Allocate 802.11 device
742 * @v priv_size Size of driver-private allocation area
743 * @ret dev Newly allocated 802.11 device
745 * This function allocates a net_device with space in its private area
746 * for both the net80211_device it will wrap and the driver-private
747 * data space requested. It initializes the link-layer-specific parts
748 * of the net_device, and links the net80211_device to the net_device
751 struct net80211_device * net80211_alloc ( size_t priv_size )
753 struct net80211_device *dev;
754 struct net_device *netdev =
755 alloc_netdev ( sizeof ( *dev ) + priv_size );
760 netdev->ll_protocol = &net80211_ll_protocol;
761 netdev->ll_broadcast = net80211_ll_broadcast;
762 netdev->max_pkt_len = IEEE80211_MAX_DATA_LEN;
763 netdev_init ( netdev, &net80211_netdev_ops );
766 dev->netdev = netdev;
767 dev->priv = ( u8 * ) dev + sizeof ( *dev );
768 dev->op = &net80211_null_ops;
770 process_init_stopped ( &dev->proc_assoc, net80211_step_associate,
772 INIT_LIST_HEAD ( &dev->mgmt_queue );
773 INIT_LIST_HEAD ( &dev->mgmt_info_queue );
779 * Register 802.11 device with network stack
781 * @v dev 802.11 device
782 * @v ops 802.11 device operations
783 * @v hw 802.11 hardware information
785 * This also registers the wrapping net_device with the higher network
788 int net80211_register ( struct net80211_device *dev,
789 struct net80211_device_operations *ops,
790 struct net80211_hw_info *hw )
793 dev->hw = malloc ( sizeof ( *hw ) );
797 memcpy ( dev->hw, hw, sizeof ( *hw ) );
798 memcpy ( dev->netdev->hw_addr, hw->hwaddr, ETH_ALEN );
800 /* Set some sensible channel defaults for driver's open() function */
801 memcpy ( dev->channels, dev->hw->channels,
802 NET80211_MAX_CHANNELS * sizeof ( dev->channels[0] ) );
805 list_add_tail ( &dev->list, &net80211_devices );
806 return register_netdev ( dev->netdev );
810 * Unregister 802.11 device from network stack
812 * @v dev 802.11 device
814 * After this call, the device operations are cleared so that they
815 * will not be called.
817 void net80211_unregister ( struct net80211_device *dev )
819 unregister_netdev ( dev->netdev );
820 list_del ( &dev->list );
821 dev->op = &net80211_null_ops;
827 * @v dev 802.11 device
829 * The device should be unregistered before this function is called.
831 void net80211_free ( struct net80211_device *dev )
834 rc80211_free ( dev->rctl );
835 netdev_nullify ( dev->netdev );
836 netdev_put ( dev->netdev );
840 /* ---------- 802.11 network management workhorse code ---------- */
843 * Set state of 802.11 device
845 * @v dev 802.11 device
846 * @v clear Bitmask of flags to clear
847 * @v set Bitmask of flags to set
848 * @v status Status or reason code for most recent operation
850 * If @a status represents a reason code, it should be OR'ed with
851 * NET80211_IS_REASON.
853 * Clearing authentication also clears association; clearing
854 * association also clears security handshaking state. Clearing
855 * association removes the link-up flag from the wrapping net_device,
856 * but setting it does not automatically set the flag; that is left to
857 * the judgment of higher-level code.
859 static inline void net80211_set_state ( struct net80211_device *dev,
860 short clear, short set,
863 /* The conditions in this function are deliberately formulated
864 to be decidable at compile-time in most cases. Since clear
865 and set are generally passed as constants, the body of this
866 function can be reduced down to a few statements by the
869 const int statmsk = NET80211_STATUS_MASK | NET80211_IS_REASON;
871 if ( clear & NET80211_PROBED )
872 clear |= NET80211_AUTHENTICATED;
874 if ( clear & NET80211_AUTHENTICATED )
875 clear |= NET80211_ASSOCIATED;
877 if ( clear & NET80211_ASSOCIATED )
878 clear |= NET80211_CRYPTO_SYNCED;
880 dev->state = ( dev->state & ~clear ) | set;
881 dev->state = ( dev->state & ~statmsk ) | ( status & statmsk );
883 if ( clear & NET80211_ASSOCIATED )
884 netdev_link_down ( dev->netdev );
886 if ( ( clear | set ) & NET80211_ASSOCIATED )
887 dev->op->config ( dev, NET80211_CFG_ASSOC );
890 if ( status & NET80211_IS_REASON )
891 dev->assoc_rc = -E80211_REASON ( status );
893 dev->assoc_rc = -E80211_STATUS ( status );
894 netdev_link_err ( dev->netdev, dev->assoc_rc );
899 * Add channels to 802.11 device
901 * @v dev 802.11 device
902 * @v start First channel number to add
903 * @v len Number of channels to add
904 * @v txpower TX power (dBm) to allow on added channels
906 * To replace the current list of channels instead of adding to it,
907 * set the nr_channels field of the 802.11 device to 0 before calling
910 static void net80211_add_channels ( struct net80211_device *dev, int start,
911 int len, int txpower )
915 for ( i = dev->nr_channels; len-- && i < NET80211_MAX_CHANNELS; i++ ) {
916 dev->channels[i].channel_nr = chan;
917 dev->channels[i].maxpower = txpower;
918 dev->channels[i].hw_value = 0;
920 if ( chan >= 1 && chan <= 14 ) {
921 dev->channels[i].band = NET80211_BAND_2GHZ;
923 dev->channels[i].center_freq = 2484;
925 dev->channels[i].center_freq = 2407 + 5 * chan;
928 dev->channels[i].band = NET80211_BAND_5GHZ;
929 dev->channels[i].center_freq = 5000 + 5 * chan;
934 dev->nr_channels = i;
938 * Filter 802.11 device channels for hardware capabilities
940 * @v dev 802.11 device
942 * Hardware may support fewer channels than regulatory restrictions
943 * allow; this function filters out channels in dev->channels that are
944 * not supported by the hardware list in dev->hwinfo. It also copies
945 * over the net80211_channel::hw_value and limits maximum TX power
948 * Channels are matched based on center frequency, ignoring band and
951 * If the driver specifies no supported channels, the effect will be
952 * as though all were supported.
954 static void net80211_filter_hw_channels ( struct net80211_device *dev )
956 int delta = 0, i = 0;
957 int old_freq = dev->channels[dev->channel].center_freq;
958 struct net80211_channel *chan, *hwchan;
960 if ( ! dev->hw->nr_channels )
964 for ( chan = dev->channels; chan < dev->channels + dev->nr_channels;
967 for ( hwchan = dev->hw->channels;
968 hwchan < dev->hw->channels + dev->hw->nr_channels;
970 if ( hwchan->center_freq == chan->center_freq ) {
979 chan->hw_value = hwchan->hw_value;
980 if ( hwchan->maxpower != 0 &&
981 chan->maxpower > hwchan->maxpower )
982 chan->maxpower = hwchan->maxpower;
983 if ( old_freq == chan->center_freq )
984 dev->channel = i - delta;
986 chan[-delta] = *chan;
990 dev->nr_channels -= delta;
992 if ( dev->channels[dev->channel].center_freq != old_freq )
993 dev->op->config ( dev, NET80211_CFG_CHANNEL );
997 * Update 802.11 device state to reflect received capabilities field
999 * @v dev 802.11 device
1000 * @v capab Capabilities field in beacon, probe, or association frame
1001 * @ret rc Return status code
1003 static int net80211_process_capab ( struct net80211_device *dev,
1006 u16 old_phy = dev->phy_flags;
1008 if ( ( capab & ( IEEE80211_CAPAB_MANAGED | IEEE80211_CAPAB_ADHOC ) ) !=
1009 IEEE80211_CAPAB_MANAGED ) {
1010 DBGC ( dev, "802.11 %p cannot handle IBSS network\n", dev );
1014 if ( capab & IEEE80211_CAPAB_SPECTRUM_MGMT ) {
1015 DBGC ( dev, "802.11 %p cannot handle spectrum managed "
1020 dev->phy_flags &= ~( NET80211_PHY_USE_SHORT_PREAMBLE |
1021 NET80211_PHY_USE_SHORT_SLOT );
1023 if ( capab & IEEE80211_CAPAB_SHORT_PMBL )
1024 dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE;
1026 if ( capab & IEEE80211_CAPAB_SHORT_SLOT )
1027 dev->phy_flags |= NET80211_PHY_USE_SHORT_SLOT;
1029 if ( old_phy != dev->phy_flags )
1030 dev->op->config ( dev, NET80211_CFG_PHY_PARAMS );
1036 * Update 802.11 device state to reflect received information elements
1038 * @v dev 802.11 device
1039 * @v ie Pointer to first information element
1040 * @v ie_end Pointer to tail of packet I/O buffer
1041 * @ret rc Return status code
1043 static int net80211_process_ie ( struct net80211_device *dev,
1044 union ieee80211_ie *ie, void *ie_end )
1046 u16 old_rate = dev->rates[dev->rate];
1047 u16 old_phy = dev->phy_flags;
1048 int have_rates = 0, i;
1051 int band = dev->channels[dev->channel].band;
1053 if ( ( void * ) ie >= ie_end )
1056 for ( ; ie; ie = ieee80211_next_ie ( ie, ie_end ) ) {
1058 case IEEE80211_IE_SSID:
1059 if ( ie->len <= 32 ) {
1060 memcpy ( dev->essid, ie->ssid, ie->len );
1061 dev->essid[ie->len] = 0;
1065 case IEEE80211_IE_RATES:
1066 case IEEE80211_IE_EXT_RATES:
1067 if ( ! have_rates ) {
1069 dev->basic_rates = 0;
1072 for ( i = 0; i < ie->len &&
1073 dev->nr_rates < NET80211_MAX_RATES; i++ ) {
1074 u8 rid = ie->rates[i];
1075 u16 rate = ( rid & 0x7f ) * 5;
1079 ( 1 << dev->nr_rates );
1081 dev->rates[dev->nr_rates++] = rate;
1086 case IEEE80211_IE_DS_PARAM:
1087 if ( dev->channel < dev->nr_channels && ds_channel ==
1088 dev->channels[dev->channel].channel_nr )
1090 ds_channel = ie->ds_param.current_channel;
1091 net80211_change_channel ( dev, ds_channel );
1094 case IEEE80211_IE_COUNTRY:
1095 dev->nr_channels = 0;
1097 DBGC ( dev, "802.11 %p setting country regulations "
1098 "for %c%c\n", dev, ie->country.name[0],
1099 ie->country.name[1] );
1100 for ( i = 0; i < ( ie->len - 3 ) / 3; i++ ) {
1101 union ieee80211_ie_country_triplet *t =
1102 &ie->country.triplet[i];
1103 if ( t->first > 200 ) {
1104 DBGC ( dev, "802.11 %p ignoring regulatory "
1105 "extension information\n", dev );
1107 net80211_add_channels ( dev,
1108 t->band.first_channel,
1109 t->band.nr_channels,
1110 t->band.max_txpower );
1113 net80211_filter_hw_channels ( dev );
1116 case IEEE80211_IE_ERP_INFO:
1117 dev->phy_flags &= ~( NET80211_PHY_USE_PROTECTION |
1118 NET80211_PHY_USE_SHORT_PREAMBLE );
1119 if ( ie->erp_info & IEEE80211_ERP_USE_PROTECTION )
1120 dev->phy_flags |= NET80211_PHY_USE_PROTECTION;
1121 if ( ! ( ie->erp_info & IEEE80211_ERP_BARKER_LONG ) )
1122 dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE;
1125 case IEEE80211_IE_RSN:
1126 /* XXX need to implement WPA stuff */
1132 /* Allow only those rates that are also supported by
1137 for ( i = 0; i < dev->nr_rates; i++ ) {
1139 for ( j = 0; j < dev->hw->nr_rates[band]; j++ ) {
1140 if ( dev->hw->rates[band][j] == dev->rates[i] ){
1149 dev->rates[i - delta] = dev->rates[i];
1150 if ( old_rate == dev->rates[i] )
1151 dev->rate = i - delta;
1155 dev->nr_rates -= delta;
1157 /* Sort available rates - sorted subclumps tend to already
1158 exist, so insertion sort works well. */
1159 for ( i = 1; i < dev->nr_rates; i++ ) {
1160 u16 rate = dev->rates[i];
1162 for ( j = i - 1; j >= 0 && dev->rates[j] >= rate; j-- )
1163 dev->rates[j + 1] = dev->rates[j];
1164 dev->rates[j + 1] = rate;
1167 net80211_set_rtscts_rate ( dev );
1169 if ( dev->rates[dev->rate] != old_rate )
1170 changed |= NET80211_CFG_RATE;
1173 if ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE )
1174 dev->phy_flags &= ~NET80211_PHY_USE_SHORT_PREAMBLE;
1175 if ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT )
1176 dev->phy_flags &= ~NET80211_PHY_USE_SHORT_SLOT;
1178 if ( old_phy != dev->phy_flags )
1179 changed |= NET80211_CFG_PHY_PARAMS;
1182 dev->op->config ( dev, changed );
1188 * Create information elements for outgoing probe or association packet
1190 * @v dev 802.11 device
1191 * @v ie Pointer to start of information element area
1192 * @ret next_ie Pointer to first byte after added information elements
1194 static union ieee80211_ie *
1195 net80211_marshal_request_info ( struct net80211_device *dev,
1196 union ieee80211_ie *ie )
1200 ie->id = IEEE80211_IE_SSID;
1201 ie->len = strlen ( dev->essid );
1202 memcpy ( ie->ssid, dev->essid, ie->len );
1204 ie = ieee80211_next_ie ( ie, NULL );
1206 ie->id = IEEE80211_IE_RATES;
1207 ie->len = dev->nr_rates;
1208 for ( i = 0; i < ie->len; i++ ) {
1209 ie->rates[i] = dev->rates[i] / 5;
1210 if ( dev->basic_rates & ( 1 << i ) )
1211 ie->rates[i] |= 0x80;
1214 if ( ie->len > 8 ) {
1215 /* 802.11 requires we use an Extended Basic Rates IE
1216 for the rates beyond the eighth. */
1217 int rates = ie->len;
1219 memmove ( ( void * ) ie + 2 + 8 + 2, ( void * ) ie + 2 + 8,
1223 ie = ieee80211_next_ie ( ie, NULL );
1225 ie->id = IEEE80211_IE_EXT_RATES;
1226 ie->len = rates - 8;
1229 ie = ieee80211_next_ie ( ie, NULL );
1234 /** Seconds to wait after finding a network, to possibly find better APs for it
1236 * This is used when a specific SSID to scan for is specified.
1238 #define NET80211_PROBE_GATHER 1
1240 /** Seconds to wait after finding a network, to possibly find other networks
1242 * This is used when an empty SSID is specified, to scan for all
1245 #define NET80211_PROBE_GATHER_ALL 2
1247 /** Seconds to allow a probe to take if no network has been found */
1248 #define NET80211_PROBE_TIMEOUT 6
1251 * Begin probe of 802.11 networks
1253 * @v dev 802.11 device
1254 * @v essid SSID to probe for, or "" to accept any (may not be NULL)
1255 * @v active Whether to use active scanning
1256 * @ret ctx Probe context
1258 * Active scanning may only be used on channels 1-11 in the 2.4GHz
1259 * band, due to gPXE's lack of a complete regulatory database. If
1260 * active scanning is used, probe packets will be sent on each
1261 * channel; this can allow association with hidden-SSID networks if
1262 * the SSID is properly specified.
1264 * A @c NULL return indicates an out-of-memory condition.
1266 * The returned context must be periodically passed to
1267 * net80211_probe_step() until that function returns zero.
1269 struct net80211_probe_ctx * net80211_probe_start ( struct net80211_device *dev,
1273 struct net80211_probe_ctx *ctx = zalloc ( sizeof ( *ctx ) );
1278 assert ( dev->netdev->state & NETDEV_OPEN );
1281 ctx->old_keep_mgmt = net80211_keep_mgmt ( dev, 1 );
1283 if ( dev->essid != ctx->essid )
1284 strcpy ( dev->essid, ctx->essid );
1287 struct ieee80211_probe_req *probe_req;
1288 union ieee80211_ie *ie;
1290 ctx->probe = alloc_iob ( 128 );
1291 iob_reserve ( ctx->probe, IEEE80211_TYP_FRAME_HEADER_LEN );
1292 probe_req = ctx->probe->data;
1294 ie = net80211_marshal_request_info ( dev,
1295 probe_req->info_element );
1296 ie->id = IEEE80211_IE_REQUEST;
1298 ie->request[0] = IEEE80211_IE_COUNTRY;
1299 ie->request[1] = IEEE80211_IE_ERP_INFO;
1300 ie->request[2] = IEEE80211_IE_RSN;
1302 ie = ieee80211_next_ie ( ie, NULL );
1304 iob_put ( ctx->probe, ( void * ) ie - ctx->probe->data );
1307 ctx->ticks_start = currticks();
1308 ctx->ticks_beacon = 0;
1309 ctx->ticks_channel = currticks();
1310 ctx->hop_time = ticks_per_sec() / ( active ? 2 : 6 );
1313 * Channels on 2.4GHz overlap, and the most commonly used
1314 * are 1, 6, and 11. We'll get a result faster if we check
1315 * every 5 channels, but in order to hit all of them the
1316 * number of channels must be relatively prime to 5. If it's
1317 * not, tweak the hop.
1320 while ( dev->nr_channels % ctx->hop_step == 0 && ctx->hop_step > 1 )
1323 ctx->beacons = malloc ( sizeof ( *ctx->beacons ) );
1324 INIT_LIST_HEAD ( ctx->beacons );
1327 dev->op->config ( dev, NET80211_CFG_CHANNEL );
1333 * Continue probe of 802.11 networks
1335 * @v ctx Probe context returned by net80211_probe_start()
1336 * @ret rc Probe status
1338 * The return code will be 0 if the probe is still going on (and this
1339 * function should be called again), a positive number if the probe
1340 * completed successfully, or a negative error code if the probe
1341 * failed for that reason.
1343 * Whether the probe succeeded or failed, you must call
1344 * net80211_probe_finish_all() or net80211_probe_finish_best()
1345 * (depending on whether you want information on all networks or just
1346 * the best-signal one) in order to release the probe context. A
1347 * failed probe may still have acquired some valid data.
1349 int net80211_probe_step ( struct net80211_probe_ctx *ctx )
1351 struct net80211_device *dev = ctx->dev;
1352 u32 start_timeout = NET80211_PROBE_TIMEOUT * ticks_per_sec();
1353 u32 gather_timeout = ticks_per_sec();
1354 u32 now = currticks();
1355 struct io_buffer *iob;
1358 char ssid[IEEE80211_MAX_SSID_LEN + 1];
1360 gather_timeout *= ( ctx->essid[0] ? NET80211_PROBE_GATHER :
1361 NET80211_PROBE_GATHER_ALL );
1363 /* Time out if necessary */
1364 if ( now >= ctx->ticks_start + start_timeout )
1365 return list_empty ( ctx->beacons ) ? -ETIMEDOUT : +1;
1367 if ( ctx->ticks_beacon > 0 && now >= ctx->ticks_start + gather_timeout )
1370 /* Change channels if necessary */
1371 if ( now >= ctx->ticks_channel + ctx->hop_time ) {
1372 dev->channel = ( dev->channel + ctx->hop_step )
1374 dev->op->config ( dev, NET80211_CFG_CHANNEL );
1375 udelay ( dev->hw->channel_change_time );
1377 ctx->ticks_channel = now;
1380 struct io_buffer *siob = ctx->probe; /* to send */
1382 /* make a copy for future use */
1383 iob = alloc_iob ( siob->tail - siob->head );
1384 iob_reserve ( iob, iob_headroom ( siob ) );
1385 memcpy ( iob_put ( iob, iob_len ( siob ) ),
1386 siob->data, iob_len ( siob ) );
1389 rc = net80211_tx_mgmt ( dev, IEEE80211_STYPE_PROBE_REQ,
1390 net80211_ll_broadcast,
1391 iob_disown ( siob ) );
1393 DBGC ( dev, "802.11 %p send probe failed: "
1394 "%s\n", dev, strerror ( rc ) );
1400 /* Check for new management packets */
1401 while ( ( iob = net80211_mgmt_dequeue ( dev, &signal ) ) != NULL ) {
1402 struct ieee80211_frame *hdr;
1403 struct ieee80211_beacon *beacon;
1404 union ieee80211_ie *ie;
1405 struct net80211_wlan *wlan;
1409 type = hdr->fc & IEEE80211_FC_SUBTYPE;
1410 beacon = ( struct ieee80211_beacon * ) hdr->data;
1412 if ( type != IEEE80211_STYPE_BEACON &&
1413 type != IEEE80211_STYPE_PROBE_RESP ) {
1414 DBGC2 ( dev, "802.11 %p probe: non-beacon\n", dev );
1418 if ( ( void * ) beacon->info_element >= iob->tail ) {
1419 DBGC ( dev, "802.11 %p probe: beacon with no IEs\n",
1424 ie = beacon->info_element;
1425 while ( ie && ie->id != IEEE80211_IE_SSID )
1426 ie = ieee80211_next_ie ( ie, iob->tail );
1429 DBGC ( dev, "802.11 %p probe: beacon with no SSID\n",
1434 memcpy ( ssid, ie->ssid, ie->len );
1437 if ( ctx->essid[0] && strcmp ( ctx->essid, ssid ) != 0 ) {
1438 DBGC2 ( dev, "802.11 %p probe: beacon with wrong SSID "
1439 "(%s)\n", dev, ssid );
1443 /* See if we've got an entry for this network */
1444 list_for_each_entry ( wlan, ctx->beacons, list ) {
1445 if ( strcmp ( wlan->essid, ssid ) != 0 )
1448 if ( signal < wlan->signal ) {
1449 DBGC2 ( dev, "802.11 %p probe: beacon for %s "
1450 "(%s) with weaker signal %d\n", dev,
1451 ssid, eth_ntoa ( hdr->addr3 ), signal );
1458 /* No entry yet - make one */
1459 wlan = zalloc ( sizeof ( *wlan ) );
1460 strcpy ( wlan->essid, ssid );
1461 list_add_tail ( &wlan->list, ctx->beacons );
1463 /* Whether we're using an old entry or a new one, fill
1464 it with new data. */
1466 memcpy ( wlan->bssid, hdr->addr3, ETH_ALEN );
1467 wlan->signal = signal;
1468 wlan->channel = dev->channels[dev->channel].channel_nr;
1470 /* Copy this I/O buffer into a new wlan->beacon; the
1471 * iob we've got probably came from the device driver
1472 * and may have the full 2.4k allocation, which we
1473 * don't want to keep around wasting memory.
1475 free_iob ( wlan->beacon );
1476 wlan->beacon = alloc_iob ( iob_len ( iob ) );
1477 memcpy ( iob_put ( wlan->beacon, iob_len ( iob ) ),
1478 iob->data, iob_len ( iob ) );
1480 /* XXX actually check capab and RSN ie to
1482 wlan->handshaking = NET80211_SECPROT_NONE;
1483 wlan->crypto = NET80211_CRYPT_NONE;
1485 ctx->ticks_beacon = now;
1487 DBGC2 ( dev, "802.11 %p probe: good beacon for %s (%s)\n",
1488 dev, wlan->essid, eth_ntoa ( wlan->bssid ) );
1499 * Finish probe of 802.11 networks, returning best-signal network found
1501 * @v ctx Probe context
1502 * @ret wlan Best-signal network found, or @c NULL if none were found
1504 * If net80211_probe_start() was called with a particular SSID
1505 * parameter as filter, only a network with that SSID (matching
1506 * case-sensitively) can be returned from this function.
1508 struct net80211_wlan *
1509 net80211_probe_finish_best ( struct net80211_probe_ctx *ctx )
1511 struct net80211_wlan *best = NULL, *wlan;
1516 list_for_each_entry ( wlan, ctx->beacons, list ) {
1517 if ( ! best || best->signal < wlan->signal )
1522 list_del ( &best->list );
1524 DBGC ( ctx->dev, "802.11 %p probe: found nothing for '%s'\n",
1525 ctx->dev, ctx->essid );
1527 net80211_free_wlanlist ( ctx->beacons );
1529 net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
1532 free_iob ( ctx->probe );
1541 * Finish probe of 802.11 networks, returning all networks found
1543 * @v ctx Probe context
1544 * @ret list List of net80211_wlan detailing networks found
1546 * If net80211_probe_start() was called with a particular SSID
1547 * parameter as filter, this will always return either an empty or a
1550 struct list_head *net80211_probe_finish_all ( struct net80211_probe_ctx *ctx )
1552 struct list_head *beacons = ctx->beacons;
1557 net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
1560 free_iob ( ctx->probe );
1569 * Free WLAN structure
1571 * @v wlan WLAN structure to free
1573 void net80211_free_wlan ( struct net80211_wlan *wlan )
1576 free_iob ( wlan->beacon );
1583 * Free list of WLAN structures
1585 * @v list List of WLAN structures to free
1587 void net80211_free_wlanlist ( struct list_head *list )
1589 struct net80211_wlan *wlan, *tmp;
1594 list_for_each_entry_safe ( wlan, tmp, list, list ) {
1595 list_del ( &wlan->list );
1596 net80211_free_wlan ( wlan );
1603 /** Number of ticks to wait for replies to association management frames */
1604 #define ASSOC_TIMEOUT TICKS_PER_SEC
1606 /** Number of times to try sending a particular association management frame */
1607 #define ASSOC_RETRIES 2
1610 * Step 802.11 association process
1612 * @v proc Association process
1614 static void net80211_step_associate ( struct process *proc )
1616 struct net80211_device *dev =
1617 container_of ( proc, struct net80211_device, proc_assoc );
1619 int status = dev->state & NET80211_STATUS_MASK;
1622 * We use a sort of state machine implemented using bits in
1623 * the dev->state variable. At each call, we take the
1624 * logically first step that has not yet succeeded; either it
1625 * has not been tried yet, it's being retried, or it failed.
1626 * If it failed, we return an error indication; otherwise we
1627 * perform the step. If it succeeds, RX handling code will set
1628 * the appropriate status bit for us.
1630 * Probe works a bit differently, since we have to step it
1631 * on every call instead of waiting for a packet to arrive
1632 * that will set the completion bit for us.
1635 /* If we're waiting for a reply, check for timeout condition */
1636 if ( dev->state & NET80211_WAITING ) {
1638 if ( ! dev->associating )
1641 if ( currticks() - dev->ctx.assoc->last_packet > ASSOC_TIMEOUT ) {
1642 /* Timed out - fail if too many retries, or retry */
1643 dev->ctx.assoc->times_tried++;
1644 if ( ++dev->ctx.assoc->times_tried > ASSOC_RETRIES ) {
1649 /* Didn't time out - let it keep going */
1653 if ( dev->state & NET80211_PROBED )
1654 dev->ctx.assoc->times_tried = 0;
1657 if ( ! ( dev->state & NET80211_PROBED ) ) {
1660 if ( ! dev->ctx.probe ) {
1662 int active = fetch_intz_setting ( NULL,
1663 &net80211_active_setting );
1664 int band = dev->hw->bands;
1667 band &= ~NET80211_BAND_BIT_5GHZ;
1669 rc = net80211_prepare_probe ( dev, band, active );
1673 dev->ctx.probe = net80211_probe_start ( dev, dev->essid,
1675 if ( ! dev->ctx.probe ) {
1676 dev->assoc_rc = -ENOMEM;
1681 rc = net80211_probe_step ( dev->ctx.probe );
1683 return; /* still going */
1686 dev->associating = net80211_probe_finish_best ( dev->ctx.probe );
1687 dev->ctx.probe = NULL;
1688 if ( ! dev->associating ) {
1689 if ( rc > 0 ) /* "successful" probe found nothing */
1694 /* If we probed using a broadcast SSID, record that
1695 fact for the settings applicator before we clobber
1696 it with the specific SSID we've chosen. */
1697 if ( ! dev->essid[0] )
1698 dev->state |= NET80211_AUTO_SSID;
1700 DBGC ( dev, "802.11 %p found network %s (%s)\n", dev,
1701 dev->associating->essid,
1702 eth_ntoa ( dev->associating->bssid ) );
1704 dev->ctx.assoc = zalloc ( sizeof ( *dev->ctx.assoc ) );
1705 if ( ! dev->ctx.assoc ) {
1710 dev->state |= NET80211_PROBED;
1711 dev->ctx.assoc->method = IEEE80211_AUTH_OPEN_SYSTEM;
1716 /* Record time of sending the packet we're about to send, for timeout */
1717 dev->ctx.assoc->last_packet = currticks();
1719 if ( ! ( dev->state & NET80211_AUTHENTICATED ) ) {
1720 /* state: prepare and authenticate */
1722 if ( status != IEEE80211_STATUS_SUCCESS ) {
1723 /* we tried authenticating already, but failed */
1724 int method = dev->ctx.assoc->method;
1726 if ( method == IEEE80211_AUTH_OPEN_SYSTEM &&
1727 ( status == IEEE80211_STATUS_AUTH_CHALL_INVALID ||
1728 status == IEEE80211_STATUS_AUTH_ALGO_UNSUPP ) ) {
1729 /* Maybe this network uses Shared Key? */
1730 dev->ctx.assoc->method =
1731 IEEE80211_AUTH_SHARED_KEY;
1737 DBGC ( dev, "802.11 %p authenticating with method %d\n", dev,
1738 dev->ctx.assoc->method );
1740 rc = net80211_prepare_assoc ( dev, dev->associating );
1744 rc = net80211_send_auth ( dev, dev->associating,
1745 dev->ctx.assoc->method );
1752 if ( ! ( dev->state & NET80211_ASSOCIATED ) ) {
1753 /* state: associate */
1755 if ( status != IEEE80211_STATUS_SUCCESS )
1758 DBGC ( dev, "802.11 %p associating\n", dev );
1760 rc = net80211_send_assoc ( dev, dev->associating );
1767 if ( ! ( dev->state & NET80211_CRYPTO_SYNCED ) ) {
1768 /* state: crypto sync */
1769 DBGC ( dev, "802.11 %p security handshaking\n", dev );
1771 dev->state |= NET80211_CRYPTO_SYNCED;
1772 /* XXX need to actually do something here once we
1778 netdev_link_up ( dev->netdev );
1780 dev->state &= ~NET80211_WORKING;
1782 free ( dev->ctx.assoc );
1783 dev->ctx.assoc = NULL;
1785 net80211_free_wlan ( dev->associating );
1786 dev->associating = NULL;
1788 dev->rctl = rc80211_init ( dev );
1790 process_del ( proc );
1792 DBGC ( dev, "802.11 %p associated with %s (%s)\n", dev,
1793 dev->essid, eth_ntoa ( dev->bssid ) );
1798 dev->state &= ~( NET80211_WORKING | NET80211_WAITING );
1802 netdev_link_err ( dev->netdev, dev->assoc_rc );
1804 /* We never reach here from the middle of a probe, so we don't
1805 need to worry about freeing dev->ctx.probe. */
1807 if ( dev->state & NET80211_PROBED ) {
1808 free ( dev->ctx.assoc );
1809 dev->ctx.assoc = NULL;
1812 net80211_free_wlan ( dev->associating );
1813 dev->associating = NULL;
1815 process_del ( proc );
1817 DBGC ( dev, "802.11 %p association failed (state=%04x): "
1818 "%s\n", dev, dev->state, strerror ( dev->assoc_rc ) );
1821 net80211_autoassociate ( dev );
1825 * Check for 802.11 SSID updates
1827 * This acts as a settings applicator; if the user changes netX/ssid,
1828 * and netX is currently open, the association task will be invoked
1831 static int net80211_check_ssid_update ( void )
1833 struct net80211_device *dev;
1834 char ssid[IEEE80211_MAX_SSID_LEN + 1];
1836 list_for_each_entry ( dev, &net80211_devices, list ) {
1837 if ( ! ( dev->netdev->state & NETDEV_OPEN ) )
1840 fetch_string_setting ( netdev_settings ( dev->netdev ),
1841 &net80211_ssid_setting, ssid,
1842 IEEE80211_MAX_SSID_LEN + 1 );
1844 if ( strcmp ( ssid, dev->essid ) != 0 &&
1845 ! ( ! ssid[0] && ( dev->state & NET80211_AUTO_SSID ) ) ) {
1846 DBGC ( dev, "802.11 %p updating association: "
1847 "%s -> %s\n", dev, dev->essid, ssid );
1848 net80211_autoassociate ( dev );
1856 * Start 802.11 association process
1858 * @v dev 802.11 device
1860 * If the association process is running, it will be restarted.
1862 void net80211_autoassociate ( struct net80211_device *dev )
1864 if ( ! ( dev->state & NET80211_WORKING ) ) {
1865 DBGC2 ( dev, "802.11 %p spawning association process\n", dev );
1866 process_add ( &dev->proc_assoc );
1869 /* Clean up everything an earlier association process might
1870 have been in the middle of using */
1871 if ( dev->associating )
1872 net80211_free_wlan ( dev->associating );
1874 if ( ! ( dev->state & NET80211_PROBED ) )
1875 net80211_free_wlan (
1876 net80211_probe_finish_best ( dev->ctx.probe ) );
1878 free ( dev->ctx.assoc );
1880 /* Reset to a clean state */
1881 fetch_string_setting ( netdev_settings ( dev->netdev ),
1882 &net80211_ssid_setting, dev->essid,
1883 IEEE80211_MAX_SSID_LEN + 1 );
1884 dev->ctx.probe = NULL;
1885 dev->associating = NULL;
1886 net80211_set_state ( dev, NET80211_PROBED, NET80211_WORKING, 0 );
1890 * Pick TX rate for RTS/CTS packets based on data rate
1892 * @v dev 802.11 device
1894 * The RTS/CTS rate is the fastest TX rate marked as "basic" that is
1895 * not faster than the data rate.
1897 static void net80211_set_rtscts_rate ( struct net80211_device *dev )
1899 u16 datarate = dev->rates[dev->rate];
1904 for ( i = 0; i < dev->nr_rates; i++ ) {
1905 u16 rate = dev->rates[i];
1907 if ( ! ( dev->basic_rates & ( 1 << i ) ) || rate > datarate )
1910 if ( rate > rtsrate ) {
1916 /* If this is in initialization, we might not have any basic
1917 rates; just use the first data rate in that case. */
1921 dev->rtscts_rate = rts_idx;
1925 * Set data transmission rate for 802.11 device
1927 * @v dev 802.11 device
1928 * @v rate Rate to set, as index into @c dev->rates array
1930 void net80211_set_rate_idx ( struct net80211_device *dev, int rate )
1932 assert ( dev->netdev->state & NETDEV_OPEN );
1934 if ( rate >= 0 && rate < dev->nr_rates && rate != dev->rate ) {
1935 DBGC2 ( dev, "802.11 %p changing rate from %d->%d Mbps\n",
1936 dev, dev->rates[dev->rate] / 10,
1937 dev->rates[rate] / 10 );
1940 net80211_set_rtscts_rate ( dev );
1941 dev->op->config ( dev, NET80211_CFG_RATE );
1946 * Configure 802.11 device to transmit on a certain channel
1948 * @v dev 802.11 device
1949 * @v channel Channel number (1-11 for 2.4GHz) to transmit on
1951 int net80211_change_channel ( struct net80211_device *dev, int channel )
1953 int i, oldchan = dev->channel;
1955 assert ( dev->netdev->state & NETDEV_OPEN );
1957 for ( i = 0; i < dev->nr_channels; i++ ) {
1958 if ( dev->channels[i].channel_nr == channel ) {
1964 if ( i == dev->nr_channels )
1968 return dev->op->config ( dev, NET80211_CFG_CHANNEL );
1974 * Prepare 802.11 device channel and rate set for scanning
1976 * @v dev 802.11 device
1977 * @v band RF band(s) on which to prepare for scanning
1978 * @v active Whether the scanning will be active
1979 * @ret rc Return status code
1981 int net80211_prepare_probe ( struct net80211_device *dev, int band,
1984 assert ( dev->netdev->state & NETDEV_OPEN );
1986 if ( active && ( band & NET80211_BAND_BIT_5GHZ ) ) {
1987 DBGC ( dev, "802.11 %p cannot perform active scanning on "
1988 "5GHz band\n", dev );
1989 return -EINVAL_ACTIVE_SCAN;
1993 /* This can happen for a 5GHz-only card with 5GHz
1994 scanning masked out by an active request. */
1995 DBGC ( dev, "802.11 %p asked to prepare for scanning nothing\n",
1997 return -EINVAL_ACTIVE_SCAN;
2000 dev->nr_channels = 0;
2003 net80211_add_channels ( dev, 1, 11, NET80211_REG_TXPOWER );
2005 if ( band & NET80211_BAND_BIT_2GHZ )
2006 net80211_add_channels ( dev, 1, 14,
2007 NET80211_REG_TXPOWER );
2008 if ( band & NET80211_BAND_BIT_5GHZ )
2009 net80211_add_channels ( dev, 36, 8,
2010 NET80211_REG_TXPOWER );
2013 net80211_filter_hw_channels ( dev );
2015 /* Use channel 1 for now */
2017 dev->op->config ( dev, NET80211_CFG_CHANNEL );
2019 /* Always do active probes at lowest (presumably first) speed */
2022 dev->rates[0] = dev->hw->rates[dev->channels[0].band][0];
2023 dev->op->config ( dev, NET80211_CFG_RATE );
2029 * Prepare 802.11 device channel and rate set for communication
2031 * @v dev 802.11 device
2032 * @v wlan WLAN to prepare for communication with
2033 * @ret rc Return status code
2035 int net80211_prepare_assoc ( struct net80211_device *dev,
2036 struct net80211_wlan *wlan )
2038 struct ieee80211_frame *hdr = wlan->beacon->data;
2039 struct ieee80211_beacon *beacon =
2040 ( struct ieee80211_beacon * ) hdr->data;
2043 assert ( dev->netdev->state & NETDEV_OPEN );
2045 net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
2046 memcpy ( dev->bssid, wlan->bssid, ETH_ALEN );
2047 strcpy ( dev->essid, wlan->essid );
2049 dev->last_beacon_timestamp = beacon->timestamp;
2050 dev->tx_beacon_interval = 1024 * beacon->beacon_interval;
2052 /* XXX do crypto setup here */
2054 /* Barring an IE that tells us the channel outright, assume
2055 the channel we heard this AP best on is the channel it's
2056 communicating on. */
2057 net80211_change_channel ( dev, wlan->channel );
2059 rc = net80211_process_capab ( dev, beacon->capability );
2063 rc = net80211_process_ie ( dev, beacon->info_element,
2064 wlan->beacon->tail );
2068 /* Associate at the lowest rate so we know it'll get through */
2070 dev->op->config ( dev, NET80211_CFG_RATE );
2076 * Send 802.11 initial authentication frame
2078 * @v dev 802.11 device
2079 * @v wlan WLAN to authenticate with
2080 * @v method Authentication method
2081 * @ret rc Return status code
2083 * @a method may be 0 for Open System authentication or 1 for Shared
2084 * Key authentication. Open System provides no security in association
2085 * whatsoever, relying on encryption for confidentiality, but Shared
2086 * Key actively introduces security problems and is very rarely used.
2088 int net80211_send_auth ( struct net80211_device *dev,
2089 struct net80211_wlan *wlan, int method )
2091 struct io_buffer *iob = alloc_iob ( 64 );
2092 struct ieee80211_auth *auth;
2094 net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
2095 iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
2096 auth = iob_put ( iob, sizeof ( *auth ) );
2097 auth->algorithm = method;
2101 return net80211_tx_mgmt ( dev, IEEE80211_STYPE_AUTH, wlan->bssid, iob );
2105 * Handle receipt of 802.11 authentication frame
2107 * @v dev 802.11 device
2110 * If the authentication method being used is Shared Key, and the
2111 * frame that was received included challenge text, the frame is
2112 * encrypted using the cryptographic algorithm currently in effect and
2113 * sent back to the AP to complete the authentication.
2115 static void net80211_handle_auth ( struct net80211_device *dev,
2116 struct io_buffer *iob )
2118 struct ieee80211_frame *hdr = iob->data;
2119 struct ieee80211_auth *auth =
2120 ( struct ieee80211_auth * ) hdr->data;
2122 if ( auth->tx_seq & 1 ) {
2123 DBGC ( dev, "802.11 %p authentication received improperly "
2124 "directed frame (seq. %d)\n", dev, auth->tx_seq );
2125 net80211_set_state ( dev, NET80211_WAITING, 0,
2126 IEEE80211_STATUS_FAILURE );
2130 if ( auth->status != IEEE80211_STATUS_SUCCESS ) {
2131 DBGC ( dev, "802.11 %p authentication failed: status %d\n",
2132 dev, auth->status );
2133 net80211_set_state ( dev, NET80211_WAITING, 0,
2138 if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY && ! dev->crypto ) {
2139 DBGC ( dev, "802.11 %p can't perform shared-key authentication "
2140 "without a cryptosystem\n", dev );
2141 net80211_set_state ( dev, NET80211_WAITING, 0,
2142 IEEE80211_STATUS_FAILURE );
2146 if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY &&
2147 auth->tx_seq == 2 ) {
2148 /* Since the iob we got is going to be freed as soon
2149 as we return, we can do some in-place
2154 memcpy ( hdr->addr2, hdr->addr1, ETH_ALEN );
2155 memcpy ( hdr->addr1, hdr->addr3, ETH_ALEN );
2157 netdev_tx ( dev->netdev,
2158 dev->crypto->encrypt ( dev->crypto, iob ) );
2162 net80211_set_state ( dev, NET80211_WAITING, NET80211_AUTHENTICATED,
2163 IEEE80211_STATUS_SUCCESS );
2169 * Send 802.11 association frame
2171 * @v dev 802.11 device
2172 * @v wlan WLAN to associate with
2173 * @ret rc Return status code
2175 int net80211_send_assoc ( struct net80211_device *dev,
2176 struct net80211_wlan *wlan )
2178 struct io_buffer *iob = alloc_iob ( 128 );
2179 struct ieee80211_assoc_req *assoc;
2180 union ieee80211_ie *ie;
2182 net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
2184 iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
2187 assoc->capability = IEEE80211_CAPAB_MANAGED;
2188 if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE ) )
2189 assoc->capability |= IEEE80211_CAPAB_SHORT_PMBL;
2190 if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT ) )
2191 assoc->capability |= IEEE80211_CAPAB_SHORT_SLOT;
2193 assoc->capability |= IEEE80211_CAPAB_PRIVACY;
2195 assoc->listen_interval = 1;
2197 ie = net80211_marshal_request_info ( dev, assoc->info_element );
2199 DBGP ( "802.11 %p about to send association request:\n", dev );
2200 DBGP_HD ( iob->data, ( void * ) ie - iob->data );
2202 /* XXX add RSN ie for WPA support */
2204 iob_put ( iob, ( void * ) ie - iob->data );
2206 return net80211_tx_mgmt ( dev, IEEE80211_STYPE_ASSOC_REQ,
2211 * Handle receipt of 802.11 association reply frame
2213 * @v dev 802.11 device
2216 static void net80211_handle_assoc_reply ( struct net80211_device *dev,
2217 struct io_buffer *iob )
2219 struct ieee80211_frame *hdr = iob->data;
2220 struct ieee80211_assoc_resp *assoc =
2221 ( struct ieee80211_assoc_resp * ) hdr->data;
2223 net80211_process_capab ( dev, assoc->capability );
2224 net80211_process_ie ( dev, assoc->info_element, iob->tail );
2226 if ( assoc->status != IEEE80211_STATUS_SUCCESS ) {
2227 DBGC ( dev, "802.11 %p association failed: status %d\n",
2228 dev, assoc->status );
2229 net80211_set_state ( dev, NET80211_WAITING, 0,
2234 /* ESSID was filled before the association request was sent */
2235 memcpy ( dev->bssid, hdr->addr3, ETH_ALEN );
2236 dev->aid = assoc->aid;
2238 net80211_set_state ( dev, NET80211_WAITING, NET80211_ASSOCIATED,
2239 IEEE80211_STATUS_SUCCESS );
2244 * Send 802.11 disassociation frame
2246 * @v dev 802.11 device
2247 * @v reason Reason for disassociation
2248 * @ret rc Return status code
2250 static int net80211_send_disassoc ( struct net80211_device *dev, int reason )
2252 struct io_buffer *iob = alloc_iob ( 64 );
2253 struct ieee80211_disassoc *disassoc;
2255 if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2258 net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
2259 iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
2260 disassoc = iob_put ( iob, sizeof ( *disassoc ) );
2261 disassoc->reason = reason;
2263 return net80211_tx_mgmt ( dev, IEEE80211_STYPE_DISASSOC, dev->bssid,
2268 /** Smoothing factor (1-7) for link quality calculation */
2272 * Update link quality information based on received beacon
2274 * @v dev 802.11 device
2275 * @v iob I/O buffer containing beacon
2276 * @ret rc Return status code
2278 static void net80211_update_link_quality ( struct net80211_device *dev,
2279 struct io_buffer *iob )
2281 struct ieee80211_frame *hdr = iob->data;
2282 struct ieee80211_beacon *beacon;
2285 if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2288 beacon = ( struct ieee80211_beacon * ) hdr->data;
2289 dt = ( u32 ) ( beacon->timestamp - dev->last_beacon_timestamp );
2290 rxi = dev->rx_beacon_interval;
2292 rxi = ( LQ_SMOOTH * rxi ) + ( ( 8 - LQ_SMOOTH ) * dt );
2293 dev->rx_beacon_interval = rxi >> 3;
2295 dev->last_beacon_timestamp = beacon->timestamp;
2300 * Handle receipt of 802.11 management frame
2302 * @v dev 802.11 device
2304 * @v signal Signal strength of received frame
2306 static void net80211_handle_mgmt ( struct net80211_device *dev,
2307 struct io_buffer *iob, int signal )
2309 struct ieee80211_frame *hdr = iob->data;
2310 struct ieee80211_disassoc *disassoc;
2311 u16 stype = hdr->fc & IEEE80211_FC_SUBTYPE;
2313 int is_deauth = ( stype == IEEE80211_STYPE_DEAUTH );
2315 if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_MGMT ) {
2317 return; /* only handle management frames */
2321 /* We reconnect on deauthentication and disassociation. */
2322 case IEEE80211_STYPE_DEAUTH:
2323 case IEEE80211_STYPE_DISASSOC:
2324 disassoc = ( struct ieee80211_disassoc * ) hdr->data;
2325 net80211_set_state ( dev, is_deauth ? NET80211_AUTHENTICATED :
2326 NET80211_ASSOCIATED, 0,
2327 NET80211_IS_REASON | disassoc->reason );
2328 DBGC ( dev, "802.11 %p %s: reason %d\n",
2329 dev, is_deauth ? "deauthenticated" : "disassociated",
2332 /* Try to reassociate, in case it's transient. */
2333 net80211_autoassociate ( dev );
2337 /* We handle authentication and association. */
2338 case IEEE80211_STYPE_AUTH:
2339 if ( ! ( dev->state & NET80211_AUTHENTICATED ) )
2340 net80211_handle_auth ( dev, iob );
2343 case IEEE80211_STYPE_ASSOC_RESP:
2344 case IEEE80211_STYPE_REASSOC_RESP:
2345 if ( ! ( dev->state & NET80211_ASSOCIATED ) )
2346 net80211_handle_assoc_reply ( dev, iob );
2349 /* We pass probes and beacons onto network scanning
2350 code. Pass actions for future extensibility. */
2351 case IEEE80211_STYPE_BEACON:
2352 net80211_update_link_quality ( dev, iob );
2354 case IEEE80211_STYPE_PROBE_RESP:
2355 case IEEE80211_STYPE_ACTION:
2356 if ( dev->keep_mgmt ) {
2357 struct net80211_rx_info *rxinf;
2358 rxinf = zalloc ( sizeof ( *rxinf ) );
2360 DBGC ( dev, "802.11 %p out of memory\n", dev );
2363 rxinf->signal = signal;
2364 list_add_tail ( &iob->list, &dev->mgmt_queue );
2365 list_add_tail ( &rxinf->list, &dev->mgmt_info_queue );
2370 case IEEE80211_STYPE_PROBE_REQ:
2371 /* Some nodes send these broadcast. Ignore them. */
2374 case IEEE80211_STYPE_ASSOC_REQ:
2375 case IEEE80211_STYPE_REASSOC_REQ:
2376 /* We should never receive these, only send them. */
2377 DBGC ( dev, "802.11 %p received strange management request "
2378 "(%04x)\n", dev, stype );
2382 DBGC ( dev, "802.11 %p received unimplemented management "
2383 "packet (%04x)\n", dev, stype );
2391 /* ---------- Packet handling functions ---------- */
2394 * Free buffers used by 802.11 fragment cache entry
2396 * @v dev 802.11 device
2397 * @v fcid Fragment cache entry index
2399 * After this function, the referenced entry will be marked unused.
2401 static void net80211_free_frags ( struct net80211_device *dev, int fcid )
2404 struct net80211_frag_cache *frag = &dev->frags[fcid];
2406 for ( j = 0; j < 16; j++ ) {
2407 if ( frag->iob[j] ) {
2408 free_iob ( frag->iob[j] );
2409 frag->iob[j] = NULL;
2414 frag->start_ticks = 0;
2419 * Accumulate 802.11 fragments into one I/O buffer
2421 * @v dev 802.11 device
2422 * @v fcid Fragment cache entry index
2423 * @v nfrags Number of fragments received
2424 * @v size Sum of sizes of all fragments, including headers
2425 * @ret iob I/O buffer containing reassembled packet
2427 * This function does not free the fragment buffers.
2429 static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev,
2430 int fcid, int nfrags, int size )
2432 struct net80211_frag_cache *frag = &dev->frags[fcid];
2433 int hdrsize = IEEE80211_TYP_FRAME_HEADER_LEN;
2434 int nsize = size - hdrsize * ( nfrags - 1 );
2437 struct io_buffer *niob = alloc_iob ( nsize );
2438 struct ieee80211_frame *hdr;
2440 /* Add the header from the first one... */
2441 memcpy ( iob_put ( niob, hdrsize ), frag->iob[0]->data, hdrsize );
2443 /* ... and all the data from all of them. */
2444 for ( i = 0; i < nfrags; i++ ) {
2445 int len = iob_len ( frag->iob[i] ) - hdrsize;
2446 memcpy ( iob_put ( niob, len ),
2447 frag->iob[i]->data + hdrsize, len );
2450 /* Turn off the fragment bit. */
2452 hdr->fc &= ~IEEE80211_FC_MORE_FRAG;
2458 * Handle receipt of 802.11 fragment
2460 * @v dev 802.11 device
2461 * @v iob I/O buffer containing fragment
2462 * @v signal Signal strength with which fragment was received
2464 static void net80211_rx_frag ( struct net80211_device *dev,
2465 struct io_buffer *iob, int signal )
2467 struct ieee80211_frame *hdr = iob->data;
2468 int fragnr = IEEE80211_FRAG ( hdr->seq );
2470 if ( fragnr == 0 && ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2471 /* start a frag cache entry */
2473 u32 curr_ticks = currticks(), newest_ticks = 0;
2474 u32 timeout = ticks_per_sec() * NET80211_FRAG_TIMEOUT;
2476 for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
2477 if ( dev->frags[i].in_use == 0 )
2480 if ( dev->frags[i].start_ticks + timeout >=
2482 net80211_free_frags ( dev, i );
2486 if ( dev->frags[i].start_ticks > newest_ticks ) {
2488 newest_ticks = dev->frags[i].start_ticks;
2492 /* If we're being sent more concurrent fragmented
2493 packets than we can handle, drop the newest so the
2494 older ones have time to complete. */
2495 if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
2497 net80211_free_frags ( dev, i );
2500 dev->frags[i].in_use = 1;
2501 dev->frags[i].seqnr = IEEE80211_SEQNR ( hdr->seq );
2502 dev->frags[i].start_ticks = currticks();
2503 dev->frags[i].iob[0] = iob;
2507 for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
2508 if ( dev->frags[i].in_use && dev->frags[i].seqnr ==
2509 IEEE80211_SEQNR ( hdr->seq ) )
2512 if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
2513 /* Drop non-first not-in-cache fragments */
2514 DBGC ( dev, "802.11 %p dropped fragment fc=%04x "
2515 "seq=%04x\n", dev, hdr->fc, hdr->seq );
2520 dev->frags[i].iob[fragnr] = iob;
2522 if ( ! ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2524 for ( j = 0; j < fragnr; j++ ) {
2525 size += iob_len ( dev->frags[i].iob[j] );
2526 if ( dev->frags[i].iob[j] == NULL )
2529 if ( j == fragnr ) {
2530 /* We've got everything */
2531 struct io_buffer *niob =
2532 net80211_accum_frags ( dev, i, fragnr,
2534 net80211_free_frags ( dev, i );
2535 net80211_rx ( dev, niob, signal, 0 );
2537 DBGC ( dev, "802.11 %p dropping fragmented "
2538 "packet due to out-of-order arrival, "
2539 "fc=%04x seq=%04x\n", dev, hdr->fc,
2541 net80211_free_frags ( dev, i );
2548 * Handle receipt of 802.11 frame
2550 * @v dev 802.11 device
2552 * @v signal Received signal strength
2553 * @v rate Bitrate at which frame was received, in 100 kbps units
2555 * If the rate or signal is unknown, 0 should be passed.
2557 void net80211_rx ( struct net80211_device *dev, struct io_buffer *iob,
2558 int signal, u16 rate )
2560 struct ieee80211_frame *hdr = iob->data;
2561 u16 type = hdr->fc & IEEE80211_FC_TYPE;
2562 if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION )
2563 goto drop; /* drop invalid-version packets */
2565 if ( type == IEEE80211_TYPE_CTRL )
2566 goto drop; /* we don't handle control packets,
2567 the hardware does */
2569 if ( dev->last_rx_seq == hdr->seq )
2570 goto drop; /* avoid duplicate packet */
2571 dev->last_rx_seq = hdr->seq;
2573 if ( dev->hw->flags & NET80211_HW_RX_HAS_FCS ) {
2574 /* discard the FCS */
2575 iob_unput ( iob, 4 );
2578 if ( hdr->fc & IEEE80211_FC_PROTECTED ) {
2579 struct io_buffer *niob;
2580 if ( ! dev->crypto )
2581 goto drop; /* can't decrypt packets on an open network */
2583 niob = dev->crypto->decrypt ( dev->crypto, iob );
2585 goto drop; /* drop failed decryption */
2590 dev->last_signal = signal;
2592 /* Fragments go into the frag cache or get dropped. */
2593 if ( IEEE80211_FRAG ( hdr->seq ) != 0
2594 || ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
2595 net80211_rx_frag ( dev, iob, signal );
2599 /* Management frames get handled, enqueued, or dropped. */
2600 if ( type == IEEE80211_TYPE_MGMT ) {
2601 net80211_handle_mgmt ( dev, iob, signal );
2605 /* Data frames get dropped or sent to the net_device. */
2606 if ( ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA )
2607 goto drop; /* drop QoS, CFP, or null data packets */
2609 /* Update rate-control algorithm */
2611 rc80211_update_rx ( dev, hdr->fc & IEEE80211_FC_RETRY, rate );
2613 /* Pass packet onward */
2614 if ( netdev_link_ok ( dev->netdev ) ) {
2615 netdev_rx ( dev->netdev, iob );
2620 DBGC2 ( dev, "802.11 %p dropped packet fc=%04x seq=%04x\n", dev,
2621 hdr->fc, hdr->seq );
2626 /** Indicate an error in receiving a packet
2628 * @v dev 802.11 device
2629 * @v iob I/O buffer with received packet, or NULL
2632 * This logs the error with the wrapping net_device, and frees iob if
2635 void net80211_rx_err ( struct net80211_device *dev,
2636 struct io_buffer *iob, int rc )
2638 netdev_rx_err ( dev->netdev, iob, rc );
2641 /** Indicate the completed transmission of a packet
2643 * @v dev 802.11 device
2644 * @v iob I/O buffer of transmitted packet
2645 * @v retries Number of times this packet was retransmitted
2646 * @v rc Error code, or 0 for success
2648 * This logs an error with the wrapping net_device if one occurred,
2649 * and removes and frees the I/O buffer from its TX queue. The
2650 * provided retry information is used to tune our transmission rate.
2652 * If the packet did not need to be retransmitted because it was
2653 * properly ACKed the first time, @a retries should be 0.
2655 void net80211_tx_complete ( struct net80211_device *dev,
2656 struct io_buffer *iob, int retries, int rc )
2658 /* Update rate-control algorithm */
2660 rc80211_update_tx ( dev, retries, rc );
2662 /* Pass completion onward */
2663 netdev_tx_complete_err ( dev->netdev, iob, rc );