Skip to content

Wire the MACsec SecY into the Ethernet datapath - #207

Open
Frauschi wants to merge 5 commits into
wolfSSL:masterfrom
Frauschi:macsec-datapath
Open

Frauschi wants to merge 5 commits into
wolfSSL:masterfrom
Frauschi:macsec-datapath

Conversation

@Frauschi

@Frauschi Frauschi commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Wires the software MACsec SecY in src/macsec/ into the wolfIP datapath, so a static-key or MKA-driven SecY can protect every Ethernet frame on an interface. The SecY and SA layers now build without the WPA supplicant.

Changes

  • Cache the AES-GCM key schedule per SA; mka_wolfmka_free() now frees its channels
  • Add macsec_{tx,rx}_sc_set_devid() to run an SA on a wolfCrypt crypto callback device
  • Drop the supplicant dependency from macsec_secy.c and macsec_sa.c; WOLFIP_ENABLE_MACSEC moves into MACSEC_CFLAGS
  • Add wolfIP_macsec_attach(), _detach() and _get_stats(): TX protects plain, VLAN and ESP output; RX validates 0x88E5 frames and demuxes them as plain; EAPOL bypasses both
  • Reduce the frame MTU by 32 bytes while attached
  • Packet sockets reject frames above the egress frame MTU at sendto
  • Reserve outbound ESP SA overhead in TCP MSS, UDP sendto and ICMP datagram sizing
  • Add docs/macsec_howto.md

Testing

  • New unit-macsec suite covering each TX and RX branch, run in the Linux workflow
  • New packet-socket MTU case in the main unit suite
  • New ESP unit cases for the ESP-aware TCP MSS and UDP sendto limits
  • New SA tests for re-keying a live AN and crypto callback dispatch
  • New run_macsec_link_test.sh: wolfIP on TAP against kernel macsec0 (ping, full-size ping, TCP echo), run in the supplicant workflow

Limitations

  • No test reaches the MACSEC_RX_CRYPTO_ERR key-schedule failure path; it needs a crypto device that rejects keys
  • Wired MKA (an L2 handler for EAPOL on Ethernet) is a follow-up PR

Copilot AI balanced review requested due to automatic review settings December 4, 2026 19:00
@Frauschi Frauschi self-assigned this Dec 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical issues remain: missing ESP overhead causes silent packet drops and stalled TCP traffic, while reusing a static SAK after reset repeats AES-GCM nonces and resets replay protection.

2 open issues
What changed in this PR

Integrates the software MACsec SecY into wolfIP’s Ethernet datapath, enabling interface-level protection with application-managed or MKA-provided keys.

Changes:

  • Adds MACsec attachment, statistics, frame protection and validation, and MTU handling.
  • Caches AES-GCM contexts and decouples SecY/SA layers from the supplicant.
  • Adds unit tests, Linux interoperability testing, CI integration, and documentation.
File Description
wolfip.h Declares MACsec interface APIs and statistics.
tools/​macsec/​run_macsec_link_test.sh Adds kernel interoperability checks.
tools/​macsec/​macsec_sta.c Frees channel crypto contexts.
src/​wolfip.c Integrates MACsec into Ethernet traffic handling.
src/​wolfesp.c Routes ESP output through MACsec protection.
src/​test/​unit/​unit.c Registers packet-socket MTU test.
src/​test/​unit/​unit_tests_socket_api_arms.c Tests oversized packet-socket sends.
src/​test/​unit/​unit_macsec.c Adds MACsec datapath unit coverage.
src/​test/​test_macsec_link.c Adds TAP-based protected echo server.
src/​macsec/​test_macsec_sa.c Tests rekeying and crypto-device dispatch.
src/​macsec/​test_macsec_mka.c Releases test channel resources.
src/​macsec/​mka_wolfmka.h Clarifies channel lifecycle documentation.
src/​macsec/​mka_wolfmka.c Updates status after key-install failure.
src/​macsec/​macsec_secy.h Declares cached AES transform APIs.
src/​macsec/​macsec_secy.c Reuses AES contexts and removes supplicant dependencies.
src/​macsec/​macsec_sa.h Adds cached contexts and device configuration.
src/​macsec/​macsec_sa.c Manages cached per-SA crypto resources.
src/​macsec/​fuzz_macsec_secy.c Frees receive-channel resources.
README.md Links the MACsec guide.
Makefile Adds datapath and test build targets.
docs/​macsec_howto.md Documents setup, key rotation, and interoperability.
docs/​API.md Lists MACsec support and guide.
.github/​workflows/​supplicant.yml Runs kernel datapath interoperability testing.
.github/​workflows/​linux.yml Builds and runs MACsec unit tests.

Review effort: Balanced

Comment thread docs/macsec_howto.md Resolved
Comment thread src/wolfesp.c Resolved
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

amd-zynq7000

  • OCM: .text +16 B (+0.0%, 234,696 B / 262,144 B, total: 90% used)

linux-x64-libwolfip

  • Code: .eh_frame +104 B, .eh_frame_hdr +24 B, .text +212 B (+0.2%, 165,629 B)

stm32h563-echo

  • FLASH: .text +4 B (+0.0%, 40,020 B / 2,064,384 B, total: 2% used)

stm32h563-tzen-ssh

  • FLASH: .text +8 B (+0.0%, 321,444 B / 654,336 B, total: 49% used)

stm32h563-wolfhal-nucleo

  • FLASH: .text +4 B (+0.0%, 40,496 B / 2,097,152 B, total: 2% used)

stm32n6

  • SRAM: .text +4 B (+0.0%, 129,860 B / 524,288 B, total: 25% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

macsec_protect() and macsec_validate() rebuilt the AES key schedule and
GHASH table for every frame. Each SA now owns an Aes context scheduled
once in set_key and freed in delete_sa or sc_free.

- Add macsec_{tx,rx}_sc_set_devid() to run an SA on a crypto callback
  device
- Re-initialising a keyed channel leaks; reset it with sc_free
macsec_secy.c and macsec_sa.c pulled in the WPA supplicant only for
wpa_secure_zero(). They now use macsec_force_zero(), since older wolfSSL
lacks wc_ForceZero(), and need just AES-GCM. -DWOLFIP_ENABLE_MACSEC=1
moves from the global CFLAGS into MACSEC_CFLAGS, so build/wolfip.o
stays MACsec-free, and the MACsec dependency files are now included.
TCP MSS, UDP sendto and ICMP datagram sockets sized packets to the
full IP MTU, so with an outbound ESP SA a full-size packet grew past
the link MTU once wrapped. Size them to the largest packet that still
fits after the matching SA adds its header, padding and ICV.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical issue lets the first SA installation on an MKA-managed channel enable transmission before the enable callback and peer readiness; activation handling must be corrected and covered by an attached-port regression test.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread src/macsec/mka_wolfmka.c Resolved
wolfIP_macsec_attach() binds caller-owned transmit and receive Secure
Channels to an Ethernet interface. Every sent frame, including VLAN and
ESP output, is protected; received 0x88E5 frames are validated and
demultiplexed as plain. EAPOL bypasses protection. The frame MTU drops
by 32 bytes so protected frames stay at 1514. unit-macsec covers each
branch.
test-macsec-link runs wolfIP with a static-key SecY on a TAP device
against a kernel macsec0, and run_macsec_link_test.sh requires ping,
full-size ping and a TCP echo to pass. The supplicant workflow runs it
when the macsec module is available. docs/macsec_howto.md covers setup,
key rotation and counters.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the MACsec datapath integration.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@Frauschi

Frauschi commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

@claude review

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #207

Scan targets checked: wolfip-src, wolfip-bugs
Not reviewed (no scan target covers them): .github/workflows/linux.yml, .github/workflows/supplicant.yml, Makefile, README.md, docs/API.md, docs/ipsec_esp_howto.md, docs/macsec_howto.md, src/test/test_macsec_link.c, src/test/unit/unit.c, src/test/unit/unit_esp.c, src/test/unit/unit_macsec.c, src/test/unit/unit_tests_socket_api_arms.c, tools/macsec/run_macsec_link_test.sh, wolfip.h
Coverage: 11 of 11 in-scope changed file(s) opened by the reviewer

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Max

@Frauschi Frauschi assigned wolfSSL-Bot and unassigned Frauschi Dec 4, 2026

@dgarske dgarske left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the PR head and a local merge onto master with wolfSSL v5.9.1 (CI config): unit, unit-esp, unit-macsec, the ASan/UBSan variants, the MACsec supplicant tests and run_macsec_link_test.sh against a kernel macsec0 all pass. Non-MACsec builds are unchanged in size (+8 B on Cortex-M33).

Main concern is RAM on embedded targets. Caching an Aes per SA takes a channel from 120 B to 2.3 KB (GCM_TABLE_4BIT) or 9.5 KB (GCM_TABLE). Please either add a build option to keep the per-frame key schedule, or document the GCM table choice in docs/macsec_howto.md. The other notes are inline.

Comment thread src/wolfip.c
mtu = wolfIP_ll_link_mtu(ll);
#if WOLFIP_ENABLE_MACSEC
if (ll && ll->macsec) {
if (mtu > ETH_HEADER_LEN + IP_MTU_MAX)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If frames are already queued at attach, a 1514 B frame now fails len > frame_mtu in wolfIP_ll_xmit with -WOLFIP_EINVAL, and the UDP/TCP drain loops break without popping it, so the socket wedges. Please drop and pop oversize frames rather than retrying them forever. The same applies to a wolfIP_mtu_set shrink.

Comment thread src/macsec/macsec_sa.h
/* One transmit Secure Association: a SAK under one Association Number, with
* its own packet-number counter. */
struct macsec_tx_sa {
Aes aes; /* SAK scheduled for GCM */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the schedule cached, sak[] and sak_len are no longer read by the library (only macsec_sta.c uses them). Please remove them so we don't keep a second plaintext copy of the SAK. Saves 40 B per SA.

Comment thread src/wolfip.c
#if WOLFIP_ENABLE_MACSEC
struct wolfIP_macsec_port macsec[WOLFIP_MAX_INTERFACES];
uint8_t macsec_tx_buf[LINK_MTU];
uint8_t macsec_rx_buf[LINK_MTU];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider decrypting in place in the rx buffer and moving the MAC pair forward over the SecTAG. That drops the 1514 B macsec_rx_buf from struct wolfIP.

Comment thread wolfip.h
const struct wolfIP_wifi_ops *wifi_ops;
#if WOLFIP_ENABLE_MACSEC
/* Set while a MACsec SecY is attached; see wolfIP_macsec_attach(). */
struct wolfIP_macsec_port *macsec;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making macsec unconditional like wifi_ops, so the ll_dev layout doesn't depend on a build flag.

Comment thread src/wolfip.c

if (!ll || !ll->send || !tx || !rx || rx_count == 0 || ll->non_ethernet
|| wolfIP_is_loopback_if(if_idx)
|| (flags & ~WOLFIP_MACSEC_ACCEPT_UNPROTECTED) != 0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 0x01UL (or cast to uint32_t) so ~ covers 32 bits on 16-bit int targets.

Comment thread src/wolfip.c

if (len < ETH_HEADER_LEN)
return -WOLFIP_EINVAL;
if (((struct wolfIP_eth_frame *)buf)->type == ee16(0x888E)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an ETH_TYPE_EAPOL define. 0x888E is now bare in three places.

Comment thread src/wolfip.c
struct wolfIP_macsec_stats stats;
struct macsec_tx_sc *tx;
struct macsec_rx_sc *rx;
uint8_t *tx_buf;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tx_buf is always s->macsec_tx_buf. Drop the field.

Comment thread src/macsec/macsec_secy.c
#endif

/* wc_ForceZero() is missing from older wolfSSL releases such as 5.6.6. */
void macsec_force_zero(void *mem, size_t len)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider sharing one force-zero helper with wolfesp.c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants