Commit Graph

32 Commits

Author SHA1 Message Date
Abtin Keshavarzian ac689dd9a4 [mac] redesign Information Element (IE) processing and generation (#13263)
This commit redesigns IEEE 802.15.4 Information Element (IE) handling by
introducing dedicated subclass definitions (`CslIe`, `TimeIe`,
`ConnectionIe`, `RendezvousTimeIe`, `LinkMetricsProbingIe`, etc.) inheriting
from `HeaderIe` or `VendorIe`.

In addition, `Mac::Frame` is updated with generic template methods:
- `Has<IeType>()`: Checks if the frame contains a well-formed instance of
  the specified IE subclass.
- `Find<IeType>()`: Finds, validates and returns a type-safe pointer to the
  IE.

Key benefits of this redesign:

- Streamlined IE Generation: Constructing and populating IEs is significantly
  simplified. Each subclass exposes a unified `Init()` method that cleanly
  computes the exact IE content length, sets the Element ID and known content
  (e.g. vendor OUI) in a single step.

- True Encapsulation: Each IE subclass serves as the single source of truth
  for its Element ID (`kId`), content layout, and structural validation logic
  (`IsValid()`). Callers no longer pass magic ID constants or perform
  error-prone pointer arithmetic.

- By implementing `HeaderIe::ValidateAs<IeType>()` as a static polymorphic
  matcher passed to `FindHeaderIe()`, the compiler devirtualizes
  and inlines ID match and content verifications directly into MAC frame
  parsing loops.

- Scalable Integration: Adding new Information Elements is trivial. Defining
  a new subclass automatically equips MAC frame handling with type-safe
  search without bloating `Mac::Frame` with ad-hoc getter methods.
2026-06-23 11:05:15 -07:00
Abtin Keshavarzian 4394c0944b [mac] simplify and enhance Header IE processing (#13261)
This commit refactors `Mac::HeaderIe` generation and parsing to
improve code readability, type safety, and bounds checking.

Key changes include:
- Adds `HeaderIe::GetSize()` and `HeaderIe::GetContent()` to
  encapsulate pointer arithmetic inside the class and eliminate ad-hoc
  pointer offset math across `Mac` modules.
- Updates `Frame::FindPayloadIndex()`, `Frame::GetHeaderIe()`,
  `Frame::GetThreadIe()`, and `Frame::GetTimeIe()` to utilize the
  new helper methods, simplifying boundary checks.
- Simplifies `HeaderIe::Init()` by replacing the union with a clearer
  `mLenIdType` field and updating the internal bitwise operations
  to use `bit_utils.hpp` helper methods.
2026-06-22 13:46:52 -07:00
Yakun Xu 0a6d0e793f [radio] remove unused mCslPresent (#13232)
This commit removes RadioContext.mCslPresent. Instead we directly
check whether the CSL is in the frame data, so there is not risk of
mismatch.
2026-06-15 09:38:43 -05:00
Yakun Xu 06e210fe89 [sub-mac] redo security processing for every (re)transmission (#13093)
Retransmissions of frames containing time-dependent header Information
Elements (IEs), such as CSL or Time Sync, require updates to these
IEs to reflect the exact time of sending. If the frame counter is not
incremented for these retransmissions, it leads to nonce reuse in
AES-CCM encryption, which is a security vulnerability.

This commit addresses this issue by ensuring that every transmission
attempt (initial or retry) uses a fresh frame counter:
- Deferred security processing from `SubMac::Send()` to
  `SubMac::BeginTransmit()`.
- Upon retransmission in `SubMac::HandleTransmitDone()`, the frame is
  restored to plaintext via `TxFrame::DecryptTransmitAesCcm()` and
  security flags are cleared.
- This allows time-dependent IEs to be updated and a new frame counter
  to be assigned for every attempt.

Added a Nexus test case `retransmission_security` to verify that both
CSL and standard MAC retransmissions use incrementing frame counters
and updated CSL phases.
2026-05-26 10:36:55 -07:00
Yakun Xu 31631142c2 [build] add format-nonliteral to missed code (#12251)
This commit enables the format-nonliteral check for code missed in
`#12236`. This commit also enables the format-nonliteral warnings in
CMake to catch such warnings in future in CMake build.
2026-01-06 14:53:38 -08:00
Zhanglong Xia fab01a7f0f [simulation] use the CSL receiver's peer address to include CSL IE (#11905)
The description of the method `otPlatRadioEnableCsl()` requires the platform
to include the CSL IE in the enhanced ACK when the received data frame's source
address matches the CSL receiver's peer address. This commit implements this
requirement in the simulation radio.
2025-09-29 18:24:38 -07:00
Zhanglong Xia c1c5db8f20 [utils] check whether mInfo.mTxInfo.mIeInfo is null before using it (#11238)
The simulation platform radio driver doesn't set the 'mInfo.mTxInfo.mIeInfo'
field of the ACK frame. And the function 'otMacFrameUpdateTimeIe()' directly
use the 'mInfo.mTxInfo.mIeInfo' field, this may cause the program crash.

This commit checks whether 'mInfo.mTxInfo.mIeInfo' is null before using it
and sets the 'mInfo.mTxInfo.mIeInfo' field of the ACK frame to null in
simulation platform.
2025-02-12 10:24:34 -08:00
Zhanglong Xia ea7676cd54 [mac_frame] process the key id mode 2 only when the wakeup coordinator is enabled (#11047)
The wakeup frame is sent from the wakeup coordinator rather than the
wakeup end device.  Current code processes the key id mode 2 only when
the device is the wakeup end device.  This commit corrects this issue.
2024-12-13 07:21:22 -08:00
Zhanglong Xia 9fefeedb0d [mac-frame] process the security of the wakeup frame (#11003)
If the radio driver supports the `OT_RADIO_CAPS_TRANSMIT_SEC`, the
radio driver should process the security of the send frame. But the
current method `otMacFrameProcessTransmitSecurity()` doesn't process
the security of the wakeup frame. Which causes sent wakeup frame is not
encrypted.

This commit enables the method `otMacFrameProcessTransmitSecurity()`
to process the security of the wakeup frame.
2024-12-12 15:32:41 -08:00
Abtin Keshavarzian 444d1dd6bc [mle] implement alternate RLOC16 usage during role transition (#10006)
This commit introduces a mechanism for a device transitioning from
child to router role to keep receiving frames addressed to its
previous short address for a brief period.

A new radio platform API, `otPlatRadioSetAlternateShortAddress()`, is
introduced. This allows the OT stack to configure an alternate short
address. Radio platform support for this function is indicated by the
`OT_RADIO_CAPS_ALT_SHORT_ADDR` capability in `otPlatRadioGetCaps()`
The same function can be used with `OT_RADIO_INVALID_SHORT_ADDR`
(`0xfffe`) to clear a previously set alternate short address. Support
for the new API is implemented in RCP and `RadioSpinel`, ensuring
backward compatibility by dynamically checking supported radio
capabilities.

MLE code is updated to instruct the radio to use the old child RLOC16
as an alternate address upon role transition. The MLE layer will
automatically clear the alternate address after eight seconds, or if
other state/mode changes occur. This eight-second window ensures the
new router can transmit four MLE Advertisement messages.
2024-10-30 16:38:10 -07:00
Yakun Xu 2ea0155129 [radio] fix ACK frame counter with previous key (#10816)
This commit tracks the frame counter used for the previous key index and
use it for the ACK frame to a frame still using previous key index.
2024-10-11 08:24:51 -07:00
Yakun Xu d0f7699110 [radio] add tx timestamp (#10688)
This commit adds tx timestamp so that the pcap callback can get the time
when a frame was transmitted.
2024-09-16 10:13:25 -07:00
Yakun Xu dccc5193af [radio] add a helper of SFD handler (#10670)
This commit adds a helper of SFD handler to deal with IEs and security
needs to be processed in SFD handler from ISR.
2024-09-12 23:02:07 -07:00
Yakun Xu e219fe92f1 [mac] support frames without sequence number (#10544)
This commit adds the capability to support frames without sequence
number.
2024-08-02 09:50:45 -07:00
Abtin Keshavarzian b146766e3a [plat-utils] enhance otMacFrameDoesAddrMatch() (#9997)
This commit updates `otMacFrameDoesAddrMatch()` to ensure it
does not match if frame is malformed (`Frame::GetDstAddr()`
fails).
2024-04-05 11:18:41 -07:00
Abtin Keshavarzian fe1cf3a294 [mac-frame] update InitMacHeader() (#8660)
This commit updates how we prepare the MAC headers:
- New `enum`s are defined in `Mac::Frame` to specify the frame
 `Type`,  `Version`, `SecurityLevel`, etc.
- `InitMacHeader()` will itself determine the Frame Control Field
  from the given address info.
- It now uses `FrameBuilder` to prepare the headers.
- New helper `MeshForwarder::PrepareMacHeaders()` is added which
  takes care of adding MAC (address, security header) along with
  any IE headers.
- These changes ensure that PAN ID Compression bit is properly
  set on MAC frames when using 2015 version (ensure that PAN
  IDs are not omitted when both addresses use Extended format).
2023-01-17 20:01:26 -08:00
Abtin Keshavarzian 99a615bec2 [clang-format] use AllowShortFunctionsOnASingleLine: All (#8502)
This commit updates `AllowShortFunctionsOnASingleLine` to `All`
from `InlineOnly`.
2022-12-08 09:34:05 -08:00
Jonathan Hui 9c467a23ae [clang-format] apply v14 changes (#8490) 2022-12-07 16:23:20 -08:00
canisLupus1313 126e232ad5 [link-metrics] split OPENTHREAD_CONFIG_MLE_LINK_METRICS_ENABLE macro (#6716)
This commit splits OPENTHREAD_CONFIG_MLE_LINK_METRICS_ENABLE macro
into two subsequent ones:
- OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE
- OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE
2021-06-22 10:49:53 -07:00
Li Cao 75ac125771 [mac] refactor code to add frame version and header IE in TX frames (#6189)
This commit refactors the code to set frame version, IE present field
in frame control field and to append Header IE.

- Split UpdateFrameControlField into two methods: CalcIePresent and
  CalcFrameVersion. Currently UpdateFrameControlField` is not
  easy-to-read and hard to extend the method to handle more cases.

- Move this code from Mac to MeshForwarder. I think it should be
  MeshForwarder's responsibility (PrepareDataFrame) to prepare the
  frame (though Mac sometimes prepare data frames itself). Since when
  preparing the frame, most information comes from the Message which
  is only in MeshForwarder, we can put this code for preparing frame
  in MeshForwarder.

- Enhance the way to append Header IEs to the frame. Currently in
  Mac::AppendHeaderIe, it generates a list of HeaderIe first and then
  call Frame::AppendHeaderIe. There are two problems here: 1. it uses
  another copy (copy HeaderIe to the frame's buffer). 2. it has to
  initialize Vendor IE (e.g. TimeSync IE) after Vendor IE header has
  been copied to the buffer. Currently it directly finds the first
  Vendor IE in the buffer and initialize it as TimeSync IE, which is
  not extensible. This commit shifts the responsibility of 'how to
  append the IE' to Frame, by providing AppendHeaderIeAt method. And
  MeshForwarder can decide which IEs to append and directly call the
  method to append IEs to the frame.
2021-02-25 08:02:52 -08:00
Li Cao b2f7bda6b4 [nrf52840] implement enh-ack probing (#6021) 2021-01-07 12:01:06 -08:00
Li Cao ae07fe27ff [low-power] implement enhanced-ack based probing (#5780)
This commit implements Enhanced-ACK Probing (on simulation platform).

- Add a new public API otLinkMetricsConfigEnhAckProbing which sends
  Link Metrics Management Request to configure the probing. This is
  called on Probing Initiator side.

- Add a new radio platform API otPlatRadioConfigureEnhAckProbing. This
  API should be called on Probing Subject side when handling the
  requests from Initiators. This API notifies the radio to start/stop
  aggregating link metrics info and include the data into Vendor IE in
  enhanced-ACK for the specific neighbor. As discussed in #5746, the
  code for doing such thing should be put in radio driver.

- Add a util module util/link_metrics, which provides a group of APIs
  to implement Probing Subject side logic for Enh-ACK Probing. Any
  platform could use this module to implement the feature easily in
  radio driver.

- Add new util APIs in util/mac_frame to generate Enh-ACK Probing IE
  (Vendor IE with Thread OUI and SubType = 0) and set value for this
  IE.

- Update the implementation in simulation/radio.c to support Probing
  Subject side logic for Enh-ACK Probing, using APIs in
  util/link_metrics and util/mac_frame.

- Add a test
  v1_2_LowPower_7_1_01_SingleProbeLinkMetricsWithEnhancedAcks.py for
  testing.

Misc:

  - Add check for all the public Link Metrics APIs (Initiator side) to
    ensure that the Subject is a neighbor of the Initiator. If the
    address is not link-local or the neighbor is not found, an error
    UNKNOWN_NEIGHBOR would be returned.

  - Update PrepareEmptyFrame so that when it's called for reference
    device, its frame version would be set to 2015.
2020-12-10 23:31:41 -08:00
Jintao Lin b6e2c2c970 [low-power] frame pending bit handling for enh-ack in simulation (#5853)
MAC command frame has its command ID encrypted in frame version
802.15.4-2015. Radio driver cannot decrypt this frame payload without
extended address. So the frame pending bit is not correctly set in the
ACK in response to data request command with version 2015. This commit
adds a special handling for frame version 2015 and only checks the
frame type when deciding if it needs to set the frame pending bit.
2020-11-24 17:37:21 -08:00
Jintao Lin 3f38068a71 [mac] change address filter and seek function to accept frame without PAN ID (#5389)
In 802.15.4-2015 spec Table 7-2, it is possible to receive frame with
destination address but without destination PAN ID. This commit
changes the address filter function to avoid dropping such frames.
2020-08-17 22:34:38 -07:00
Jintao Lin 5c722a770c [simulation] support transmit security at simulation radio (#5118)
This commit adds transmit security support for simulation radio so
that security Enh-ACK could be handled at radio layer. This commit
also fixes issue #5041.
2020-06-22 12:23:16 -07:00
Jonathan Hui 1326d64a64 [style] replace NULL with nullptr (#5109) 2020-06-17 22:44:54 -07:00
Yakun Xu f302796bd2 [simulation] send enh-ack for 802.15.4-2015 frames (#5039)
This commit changes the simulation radio to send secured enh-ack for
secured frames. This improves code coverage for testing.
2020-06-03 10:59:19 -07:00
Li Cao e3acd17466 [config] change macro define names for CSL feature (#4958) 2020-05-13 14:40:49 -07:00
Li Cao cd9558ca91 [mac-frame] add util functions to generate ack (#4884)
This commit adds mac frame util functions to generate acks (both Imm
and Enh acks) from a received frame. These funtions are utils APIs for
platforms which don't provide ack generation.

This commit also introduces compile options for CSL to control the CSL
IE related part.
2020-05-12 22:20:03 -07:00
Yakun Xu 56d0e7212b [mac] add enhanced frame pending (#4334)
This commit adds enhanced frame pending feature of Thread 1.2. This
requires the platform radio driver to correctly set frame pending bit
for Data frames and MAC Data Request frames.
2020-03-09 09:08:46 -07:00
Yakun Xu b95281666d [radio] process tx AES without otInstance (#4318)
`otPlatRadioFrameUpdated()` is declared not to access any state within
OpenThread. However, the current implementation does read the extended
address. This commit moves the AES process into `Mac::TxFrame`, which
is free of `otInstance` and eliminates the current deep callback
stacks when the radio driver wants to process tx AES from interrupt
context.
2019-11-13 07:36:18 +01:00
Yakun Xu f691ea3edf [examples] provide APIs to manipulate 802.15.4 frames (#4156)
This commit exposes utility functions for radio drivers to manipulate
MAC frames.
2019-10-11 09:50:20 -07:00