# Wi-Fi MAC Layer

## Summary


IEEE 802.11 MAC sub-layer is responsible for coordinating access to the shared physical air interface so that the Access Point (AP) and Wi-Fi stations in range can communicate effectively. MAC takes data from a higher sub-layer called LLC, adds header and tail bytes, and sends them to lower physical layer for transmission. The reverse happens when receiving data from the physical layer. If a frame is received in error, MAC can retransmit it. 

Multiple access is based on carrier sensing, channel contention and random backoff. Due to contention, a Wi-Fi network with a large number of active stations can suffer from low throughput and high latency. IEEE 802.11e and its subset Wi-Fi Multimedia attempt to alleviate this problem.

## Discussion

### What's the purpose of MAC layer in 802.11?

MAC stands for "Medium Access Control", which implies that it's main function is to control access to a shared medium. The air interface is a shared medium through which all multiple Wi-Fi stations and access point (AP) attempt to transfer data. MAC implements the control mechanisms that allow multiple devices to reliably communicate by sharing the medium as specified in the standard.

Formally, MAC functions include scanning, authentication, association, power saving and fragmentation. 

When it comes to the actual handling of data, MAC sits as a sub-layer within Data Link Layer. It takes a packet of data called *MAC Service Data Unit (MSDU)* from Logical Link Control (LLC) sub-layer. MAC adds necessary header and tail bytes to form *MAC Protocol Data Unit (MPDU)*. MPDU is then sent to the physical layer for transmission. The reverse flow happens when MAC receives a packet from physical layer. 


### How does a Wi-Fi station discover and associate with an AP?

There are two modes of discovery and the station is at liberty to use either or both: 

    + **Passive scanning**: The station looks for beacon frames that are regularly sent by an AP. These frames contain essential information about the network. If there are multiple APs within range, it selects the strongest one. Station then attempts to connect to the AP. Communication happens on the channel in which the AP is operating.
    + **Active scanning**: The station sends a probe request either to a specific AP or to any AP within range. Either way, it expects a probe response from one or more APs, selects the strongest and attempts to connect to that AP. Since no prior beacon is decoded, station will send its probe on all channels.

### What multiple access scheme is used in 802.11?

MAC uses what's called Point Coordination Function (PCF) or Distributed Coordination Function (DCF). The latter is more prevalent in industry and employs *Carrier Sense Multiple Access with Collision Avoidance (CSMA/CA)*. 

There are two parts to CSMA/CA as suggested by the name: 

    + **Carrier Sensing**: Wi-Fi stations must first sense the air interface. Only if the channel is idle for a specified amount of time (of duration DIFS), they can transmit. DIFS stands for DCF InterFrame Spacing.
    + **Collision Avoidance**: If channel is sensed busy, the station waits till it's free for DIFS duration, then waits for a random interval before transmitting. This randomness reduces the chances that two waiting stations end up transmitting at the same time. "Collisions" can still occur and they are inferred when no acknowledgement is received by the sender. If ACK is not received, station backs off for a random duration and repeats the process again.

### What's the structure of 802.11 MAC frames?

There are three types of MAC frames: data, control and management. Only data frames contain higher layer data. Control frames include ACK, RTS, CTS, Power Save Poll, etc. Management frames help stations discover and connect to AP. 

All frames start with a 2-byte *Frame Control* that specifies the frame type and further details. All frames end with a 4-byte *Frame Check Sequence (FCS)* that's used to detect errors. Frame Control is part of what's called the MAC header, which can vary in length. For data frames, MAC header and FCS are added by MAC layer around the MSDU received from LLC. MSDU is often called MAC payload or frame body. Further details on MAC frame are available online. 


### Are there techniques in MAC to improve the reliability of communication?

MAC uses many techniques and here we describe the important ones:

    + **Error Detection**: Every MAC frame contains 4-byte FCS that's used to detect errors.
    + **Retransmission**: For a corrupted frame, receiver will not send an acknowledgment frame. Sender will therefore retransmit the frame.
    + **Fragmentation & Reassembly**: When frames are larger, there's a higher chance of collision. MAC can therefore fragment data/management frames and transmit them as individual MAC frames. Any errors, only fragments need to be retransmitted.
    + **RTS/CTS**: *Request-to-Send* and *Clear-to-Send* are used to solve the hidden-node problem. When stations are widely spread around an AP, some may be hidden from others on the wireless channel. RTS/CTS handshaking alleviates this problem and reduces collision. This is an optional feature.

### What are the MAC enhancements due to High Throughput (HT)?

IEEE 802.11n introduced changes to MAC that are part of High Throughput (HT). The idea was to reduce MAC overhead and increase overall throughput. This is done using *Frame Aggregation*. Two types are possible: 

    + **Aggregate MAC Service Data Unit (A-MSDU)**: Multiple MSDUs are packed into a single MPDU that in turn maps to a single transmission frame. In this case, all MSDUs share the same MAC header.
    + **Aggregate MAC Protocol Data Unit (A-MPDU)**: Multiple MPDUs are carried within a single transmission frame. Obviously, each MPDU has its own MAC header.These two are not mutually exclusive: a single transmission can have a combination of A-MSDUs and A-MPDUs, sometimes called *two level frame aggregation*. In fact, research has shown that a combination of the two gives best performance on both 802.11n and 802.11ac devices. 

MAC frame is extended: new header field called HT Control plus larger payload size. *Block Acknowledgments (BACK)* is introduced. 


### What are the MAC enhancements due to Very High Throughput (VHT)?

IEEE 802.11ac introduced changes to MAC that are part of Very High Throughput (VHT). VHT also impacts deployments in 60 GHz band such as for 802.11ad. 

Frame aggregation can happen like in 802.11n but in larger sizes: MPDU size of about 8KB (11n) vs 11KB (11ac); PSDU size of about 64 KB (11n) vs 4 MB (11ac). MAC frame is updated: HT Control is defined for 11ac plus larger payload size. All frames are sent as A-MPDUs even if they contain only one MPDU. 


### I've heard of the terms "lower MAC" and "upper MAC". What are these?

Part of MAC that interacts with LLC sub-layer is sometimes called *upper MAC* while the part that interacts with PHY layer is called *lower MAC*. Thus, MSDUs and A-MSDUs are processed in upper MAC while MPDUs and A-MPDUs are processed in lower MAC. In general, lower MAC deals with more time-critical operations such as MPDU transmissions and acknowledgements. For example, association of stations with AP is done by upper MAC but actual medium access (time-critical) is handled by lower MAC. 

It's common for lower MAC to be implemented in hardware such as in FPGA. 

## Milestones

2005

IEEE 802.11e introduces QoS enhancements to MAC. Hybrid Coordination Function (HCF) is introduced. MAC header includes a 2-byte QoS Control field. 

Oct  
2009

IEEE 802.11n is released and it includes changes to MAC. 

Jan  
2014

IEEE 802.11ac is released and it includes changes to MAC.