cybersecurity · intermediate · ~12 min · safe pentest lab

Classify an 802.11 frame as mgmt/ctrl/data

Bit-field decoding from a single header byte.

Challenge

Your job

Implement:

#include <stdint.h>
int classify_frame(const uint8_t *hdr);

The Frame Control field's first byte has:

bits 7..6 : subtype
bits 5..4 : also subtype
bits 3..2 : TYPE  <- this is what we extract
bits 1..0 : protocol version

So type = (hdr[0] >> 2) & 0x3. Map:

  • 0 → return 0 (management)
  • 1 → return 1 (control)
  • 2 → return 2 (data)
  • 3 → return -1 (reserved)

NULL hdr → -1.

Hints

  1. (concept) Bit-shift and mask is enough. No loops, no string work.
  2. (common bug) Reading the wrong bit positions. The bottom two bits are the protocol version, not the type.

Why this matters

Every Wi-Fi forensics tool starts by classifying the frame type before it knows the layout of the rest of the bytes.

Input format

A non-NULL pointer to at least one byte (we only read hdr[0]).

Output format

0, 1, 2 (valid types) or -1 (reserved / NULL).

Constraints

Two operations (shift + mask). No loops.

Starter code

#include <stdint.h>
int classify_frame(const uint8_t *hdr) {
    /* TODO */
    (void)hdr;
    return -1;
}

Common mistakes

Reading the wrong bit position. Forgetting the NULL check. Allowing the reserved type 3 through.

Edge cases to handle

All-zeros byte (type 0). Top bits set (subtype) should not affect classification.

Complexity

O(1).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.